blob: c2920371c00c6bac4940c2e07a14a9cd1d4dac05 [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 Vaskofeca4fb2020-10-05 08:58:40 +020064 uint32_t options = (dynamic && *dynamic ? LY_TYPE_OPTS_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);
641 ly_path_predicates_free(schema->module->ctx, pred_type, NULL, 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 Vasko00cbf532020-06-15 13:58:47 +02001240 } /* else we have opaq flag and the value is not valid, leavne no predicate and then create an opaque node */
1241 }
1242
1243 /* try to find any existing nodes in the path */
1244 if (parent) {
1245 ret = ly_path_eval_partial(p, parent, &path_idx, &node);
1246 if (ret == LY_SUCCESS) {
1247 /* the node exists, are we supposed to update it or is it just a default? */
Radek Krejci092e33c2020-10-12 15:33:10 +02001248 if (!(options & LYD_NEW_PATH_UPDATE) && !(node->flags & LYD_DEFAULT)) {
Michal Vasko00cbf532020-06-15 13:58:47 +02001249 LOGERR(ctx, LY_EEXIST, "Path \"%s\" already exists", path);
1250 ret = LY_EEXIST;
1251 goto cleanup;
1252 }
1253
1254 /* update the existing node */
1255 ret = lyd_new_path_update(node, value, value_type, &nparent, &nnode);
1256 goto cleanup;
1257 } else if (ret == LY_EINCOMPLETE) {
1258 /* some nodes were found, adjust the iterator to the next segment */
1259 ++path_idx;
1260 } else if (ret == LY_ENOTFOUND) {
1261 /* 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 +02001262 if (lysc_data_parent(p[LY_ARRAY_COUNT(p) - 1].node)) {
Michal Vasko00cbf532020-06-15 13:58:47 +02001263 node = parent;
1264 }
1265 } else {
1266 /* error */
1267 goto cleanup;
1268 }
1269 }
1270
1271 /* create all the non-existing nodes in a loop */
Michal Vaskod989ba02020-08-24 10:59:24 +02001272 for ( ; path_idx < LY_ARRAY_COUNT(p); ++path_idx) {
Michal Vasko00cbf532020-06-15 13:58:47 +02001273 cur_parent = node;
1274 schema = p[path_idx].node;
1275
1276 switch (schema->nodetype) {
1277 case LYS_LIST:
1278 if (!(schema->flags & LYS_KEYLESS)) {
Radek Krejci092e33c2020-10-12 15:33:10 +02001279 if ((options & LYD_NEW_PATH_OPAQ) && (p[path_idx].pred_type == LY_PATH_PREDTYPE_NONE)) {
Michal Vasko00cbf532020-06-15 13:58:47 +02001280 /* creating opaque list without keys */
Michal Vaskofeca4fb2020-10-05 08:58:40 +02001281 LY_CHECK_GOTO(ret = lyd_create_opaq(ctx, schema->name, strlen(schema->name), NULL, 0, NULL, LYD_JSON,
1282 LYD_NODEHINT_LIST, NULL, NULL, 0, schema->module->name, strlen(schema->module->name), &node),
Michal Vasko69730152020-10-09 16:30:07 +02001283 cleanup);
Michal Vasko00cbf532020-06-15 13:58:47 +02001284 } else {
1285 assert(p[path_idx].pred_type == LY_PATH_PREDTYPE_LIST);
1286 LY_CHECK_GOTO(ret = lyd_create_list(schema, p[path_idx].predicates, &node), cleanup);
1287 }
1288 break;
1289 }
Radek Krejci0f969882020-08-21 16:56:47 +02001290 /* fallthrough */
Michal Vasko00cbf532020-06-15 13:58:47 +02001291 case LYS_CONTAINER:
1292 case LYS_NOTIF:
1293 case LYS_RPC:
1294 case LYS_ACTION:
1295 LY_CHECK_GOTO(ret = lyd_create_inner(schema, &node), cleanup);
1296 break;
1297 case LYS_LEAFLIST:
Radek Krejci092e33c2020-10-12 15:33:10 +02001298 if ((options & LYD_NEW_PATH_OPAQ) && (p[path_idx].pred_type == LY_PATH_PREDTYPE_NONE)) {
Michal Vasko00cbf532020-06-15 13:58:47 +02001299 /* creating opaque leaf-list without value */
Michal Vaskofeca4fb2020-10-05 08:58:40 +02001300 LY_CHECK_GOTO(ret = lyd_create_opaq(ctx, schema->name, strlen(schema->name), NULL, 0, NULL, LYD_JSON,
1301 LYD_NODEHINT_LEAFLIST, NULL, NULL, 0, schema->module->name, strlen(schema->module->name), &node),
1302 cleanup);
Michal Vasko00cbf532020-06-15 13:58:47 +02001303 } else {
1304 assert(p[path_idx].pred_type == LY_PATH_PREDTYPE_LEAFLIST);
1305 LY_CHECK_GOTO(ret = lyd_create_term2(schema, &p[path_idx].predicates[0].value, &node), cleanup);
1306 }
1307 break;
1308 case LYS_LEAF:
1309 /* make there is some value */
1310 if (!value) {
1311 value = "";
1312 }
1313
1314 r = LY_SUCCESS;
Radek Krejci092e33c2020-10-12 15:33:10 +02001315 if (options & LYD_NEW_PATH_OPAQ) {
Michal Vaskof937cfe2020-08-03 16:07:12 +02001316 r = lys_value_validate(NULL, schema, value, strlen(value));
Michal Vasko00cbf532020-06-15 13:58:47 +02001317 }
1318 if (!r) {
Michal Vaskofeca4fb2020-10-05 08:58:40 +02001319 ret = lyd_create_term(schema, value, strlen(value), NULL, LY_PREF_JSON, NULL, LYD_HINT_DATA, NULL, &node);
1320 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko00cbf532020-06-15 13:58:47 +02001321 } else {
1322 /* creating opaque leaf without value */
Michal Vaskofeca4fb2020-10-05 08:58:40 +02001323 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 +02001324 0, NULL, NULL, 0, schema->module->name,
1325 strlen(schema->module->name), &node), cleanup);
Michal Vasko00cbf532020-06-15 13:58:47 +02001326 }
1327 break;
1328 case LYS_ANYDATA:
1329 case LYS_ANYXML:
1330 LY_CHECK_GOTO(ret = lyd_create_any(schema, value, value_type, &node), cleanup);
1331 break;
1332 default:
1333 LOGINT(ctx);
1334 ret = LY_EINT;
1335 goto cleanup;
1336 }
1337
1338 if (cur_parent) {
1339 /* connect to the parent */
1340 lyd_insert_node(cur_parent, NULL, node);
1341 } else if (parent) {
1342 /* connect to top-level siblings */
1343 lyd_insert_node(NULL, &parent, node);
1344 }
1345
1346 /* update remembered nodes */
1347 if (!nparent) {
1348 nparent = node;
1349 }
1350 nnode = node;
1351 }
1352
1353cleanup:
1354 lyxp_expr_free(ctx, exp);
1355 ly_path_free(ctx, p);
1356 if (!ret) {
1357 /* set out params only on success */
1358 if (new_parent) {
1359 *new_parent = nparent;
1360 }
1361 if (new_node) {
1362 *new_node = nnode;
1363 }
1364 }
1365 return ret;
1366}
1367
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001368LY_ERR
1369lyd_new_implicit_r(struct lyd_node *parent, struct lyd_node **first, const struct lysc_node *sparent,
Radek Krejci1deb5be2020-08-26 16:43:36 +02001370 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 +02001371 struct lyd_node **diff)
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001372{
1373 LY_ERR ret;
1374 const struct lysc_node *iter = NULL;
1375 struct lyd_node *node;
1376 struct lyd_value **dflts;
1377 LY_ARRAY_COUNT_TYPE u;
1378
1379 assert(first && (parent || sparent || mod));
1380
1381 if (!sparent && parent) {
1382 sparent = parent->schema;
1383 }
1384
1385 while ((iter = lys_getnext(iter, sparent, mod ? mod->compiled : NULL, LYS_GETNEXT_WITHCHOICE))) {
1386 if ((impl_opts & LYD_IMPLICIT_NO_STATE) && (iter->flags & LYS_CONFIG_R)) {
1387 continue;
Michal Vasko44b19a12020-08-07 09:21:30 +02001388 } else if ((impl_opts & LYD_IMPLICIT_NO_CONFIG) && (iter->flags & LYS_CONFIG_W)) {
1389 continue;
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001390 }
1391
1392 switch (iter->nodetype) {
1393 case LYS_CHOICE:
1394 if (((struct lysc_node_choice *)iter)->dflt && !lys_getnext_data(NULL, *first, NULL, iter, NULL)) {
1395 /* create default case data */
1396 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 +02001397 NULL, node_types, node_when, impl_opts, diff));
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001398 }
1399 break;
1400 case LYS_CONTAINER:
1401 if (!(iter->flags & LYS_PRESENCE) && lyd_find_sibling_val(*first, iter, NULL, 0, NULL)) {
1402 /* create default NP container */
1403 LY_CHECK_RET(lyd_create_inner(iter, &node));
1404 node->flags = LYD_DEFAULT;
1405 lyd_insert_node(parent, first, node);
1406
1407 /* cannot be a NP container with when */
1408 assert(!iter->when);
1409
1410 /* create any default children */
1411 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 +02001412 impl_opts, diff));
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001413 }
1414 break;
1415 case LYS_LEAF:
Michal Vasko69730152020-10-09 16:30:07 +02001416 if (!(impl_opts & LYD_IMPLICIT_NO_DEFAULTS) && ((struct lysc_node_leaf *)iter)->dflt &&
1417 lyd_find_sibling_val(*first, iter, NULL, 0, NULL)) {
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001418 /* create default leaf */
1419 ret = lyd_create_term2(iter, ((struct lysc_node_leaf *)iter)->dflt, &node);
1420 if (ret == LY_EINCOMPLETE) {
1421 if (node_types) {
1422 /* remember to resolve type */
Radek Krejci3d92e442020-10-12 12:48:13 +02001423 LY_CHECK_RET(ly_set_add(node_types, node, 1, NULL));
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001424 }
1425 } else if (ret) {
1426 return ret;
1427 }
1428 node->flags = LYD_DEFAULT;
1429 lyd_insert_node(parent, first, node);
1430
1431 if (iter->when && node_when) {
1432 /* remember to resolve when */
Radek Krejci3d92e442020-10-12 12:48:13 +02001433 LY_CHECK_RET(ly_set_add(node_when, node, 1, NULL));
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001434 }
1435 if (diff) {
1436 /* add into diff */
1437 LY_CHECK_RET(lyd_val_diff_add(node, LYD_DIFF_OP_CREATE, diff));
1438 }
1439 }
1440 break;
1441 case LYS_LEAFLIST:
Michal Vasko69730152020-10-09 16:30:07 +02001442 if (!(impl_opts & LYD_IMPLICIT_NO_DEFAULTS) && ((struct lysc_node_leaflist *)iter)->dflts &&
1443 lyd_find_sibling_val(*first, iter, NULL, 0, NULL)) {
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001444 /* create all default leaf-lists */
1445 dflts = ((struct lysc_node_leaflist *)iter)->dflts;
1446 LY_ARRAY_FOR(dflts, u) {
1447 ret = lyd_create_term2(iter, dflts[u], &node);
1448 if (ret == LY_EINCOMPLETE) {
1449 if (node_types) {
1450 /* remember to resolve type */
Radek Krejci3d92e442020-10-12 12:48:13 +02001451 LY_CHECK_RET(ly_set_add(node_types, node, 1, NULL));
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001452 }
1453 } else if (ret) {
1454 return ret;
1455 }
1456 node->flags = LYD_DEFAULT;
1457 lyd_insert_node(parent, first, node);
1458
1459 if (iter->when && node_when) {
1460 /* remember to resolve when */
Radek Krejci3d92e442020-10-12 12:48:13 +02001461 LY_CHECK_RET(ly_set_add(node_when, node, 1, NULL));
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001462 }
1463 if (diff) {
1464 /* add into diff */
1465 LY_CHECK_RET(lyd_val_diff_add(node, LYD_DIFF_OP_CREATE, diff));
1466 }
1467 }
1468 }
1469 break;
1470 default:
1471 /* without defaults */
1472 break;
1473 }
1474 }
1475
1476 return LY_SUCCESS;
1477}
1478
1479API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02001480lyd_new_implicit_tree(struct lyd_node *tree, uint32_t implicit_options, struct lyd_node **diff)
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001481{
Michal Vasko56daf732020-08-10 10:57:18 +02001482 struct lyd_node *node;
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001483 LY_ERR ret = LY_SUCCESS;
1484
1485 LY_CHECK_ARG_RET(NULL, tree, LY_EINVAL);
1486 if (diff) {
1487 *diff = NULL;
1488 }
1489
Michal Vasko56daf732020-08-10 10:57:18 +02001490 LYD_TREE_DFS_BEGIN(tree, node) {
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001491 /* skip added default nodes */
Michal Vasko69730152020-10-09 16:30:07 +02001492 if (((node->flags & (LYD_DEFAULT | LYD_NEW)) != (LYD_DEFAULT | LYD_NEW)) &&
1493 (node->schema->nodetype & LYD_NODE_INNER)) {
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001494 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 +02001495 NULL, implicit_options, diff), cleanup);
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001496 }
1497
Michal Vasko56daf732020-08-10 10:57:18 +02001498 LYD_TREE_DFS_END(tree, node);
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001499 }
1500
1501cleanup:
1502 if (ret && diff) {
1503 lyd_free_all(*diff);
1504 *diff = NULL;
1505 }
1506 return ret;
1507}
1508
1509API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02001510lyd_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 +02001511{
1512 const struct lys_module *mod;
1513 struct lyd_node *d = NULL;
1514 uint32_t i = 0;
1515 LY_ERR ret = LY_SUCCESS;
1516
1517 LY_CHECK_ARG_RET(ctx, tree, *tree || ctx, LY_EINVAL);
1518 if (diff) {
1519 *diff = NULL;
1520 }
1521 if (!ctx) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02001522 ctx = LYD_CTX(*tree);
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001523 }
1524
1525 /* add nodes for each module one-by-one */
1526 while ((mod = ly_ctx_get_module_iter(ctx, &i))) {
1527 if (!mod->implemented) {
1528 continue;
1529 }
1530
1531 LY_CHECK_GOTO(ret = lyd_new_implicit_module(tree, mod, implicit_options, diff ? &d : NULL), cleanup);
1532 if (d) {
1533 /* merge into one diff */
1534 lyd_insert_sibling(*diff, d, diff);
1535
1536 d = NULL;
1537 }
1538 }
1539
1540cleanup:
1541 if (ret && diff) {
1542 lyd_free_all(*diff);
1543 *diff = NULL;
1544 }
1545 return ret;
1546}
1547
1548API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02001549lyd_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 +02001550{
1551 struct lyd_node *root, *d = NULL;
1552 LY_ERR ret = LY_SUCCESS;
1553
1554 LY_CHECK_ARG_RET(NULL, tree, module, LY_EINVAL);
1555 if (diff) {
1556 *diff = NULL;
1557 }
1558
1559 /* add all top-level defaults for this module */
1560 LY_CHECK_GOTO(ret = lyd_new_implicit_r(NULL, tree, NULL, module, NULL, NULL, implicit_options, diff), cleanup);
1561
1562 /* process nested nodes */
1563 LY_LIST_FOR(*tree, root) {
1564 /* skip added default nodes */
1565 if ((root->flags & (LYD_DEFAULT | LYD_NEW)) != (LYD_DEFAULT | LYD_NEW)) {
1566 LY_CHECK_GOTO(ret = lyd_new_implicit_tree(root, implicit_options, diff ? &d : NULL), cleanup);
1567
1568 if (d) {
1569 /* merge into one diff */
1570 lyd_insert_sibling(*diff, d, diff);
1571
1572 d = NULL;
1573 }
1574 }
1575 }
1576
1577cleanup:
1578 if (ret && diff) {
1579 lyd_free_all(*diff);
1580 *diff = NULL;
1581 }
1582 return ret;
1583}
1584
Michal Vasko90932a92020-02-12 14:33:03 +01001585struct lyd_node *
Michal Vaskob104f112020-07-17 09:54:54 +02001586lyd_insert_get_next_anchor(const struct lyd_node *first_sibling, const struct lyd_node *new_node)
Michal Vasko90932a92020-02-12 14:33:03 +01001587{
Michal Vaskob104f112020-07-17 09:54:54 +02001588 const struct lysc_node *schema, *sparent;
Michal Vasko90932a92020-02-12 14:33:03 +01001589 struct lyd_node *match = NULL;
Radek Krejci857189e2020-09-01 13:26:36 +02001590 ly_bool found;
Michal Vasko90932a92020-02-12 14:33:03 +01001591
Michal Vaskob104f112020-07-17 09:54:54 +02001592 assert(new_node);
1593
1594 if (!first_sibling || !new_node->schema) {
1595 /* insert at the end, no next anchor */
Michal Vasko90932a92020-02-12 14:33:03 +01001596 return NULL;
1597 }
1598
Michal Vaskob104f112020-07-17 09:54:54 +02001599 if (first_sibling->parent && first_sibling->parent->children_ht) {
1600 /* find the anchor using hashes */
1601 sparent = first_sibling->parent->schema;
1602 schema = lys_getnext(new_node->schema, sparent, NULL, 0);
1603 while (schema) {
1604 /* keep trying to find the first existing instance of the closest following schema sibling,
1605 * otherwise return NULL - inserting at the end */
1606 if (!lyd_find_sibling_schema(first_sibling, schema, &match)) {
1607 break;
1608 }
1609
1610 schema = lys_getnext(schema, sparent, NULL, 0);
1611 }
1612 } else {
1613 /* find the anchor without hashes */
1614 match = (struct lyd_node *)first_sibling;
1615 if (!lysc_data_parent(new_node->schema)) {
1616 /* we are in top-level, skip all the data from preceding modules */
1617 LY_LIST_FOR(match, match) {
1618 if (!match->schema || (strcmp(lyd_owner_module(match)->name, lyd_owner_module(new_node)->name) >= 0)) {
1619 break;
1620 }
1621 }
1622 }
1623
1624 /* get the first schema sibling */
1625 sparent = lysc_data_parent(new_node->schema);
1626 schema = lys_getnext(NULL, sparent, new_node->schema->module->compiled, 0);
1627
1628 found = 0;
1629 LY_LIST_FOR(match, match) {
1630 if (!match->schema || (lyd_owner_module(match) != lyd_owner_module(new_node))) {
1631 /* we have found an opaque node, which must be at the end, so use it OR
1632 * modules do not match, so we must have traversed all the data from new_node module (if any),
1633 * we have found the first node of the next module, that is what we want */
1634 break;
1635 }
1636
1637 /* skip schema nodes until we find the instantiated one */
1638 while (!found) {
1639 if (new_node->schema == schema) {
1640 /* we have found the schema of the new node, continue search to find the first
1641 * data node with a different schema (after our schema) */
1642 found = 1;
1643 break;
1644 }
1645 if (match->schema == schema) {
1646 /* current node (match) is a data node still before the new node, continue search in data */
1647 break;
1648 }
1649 schema = lys_getnext(schema, sparent, new_node->schema->module->compiled, 0);
1650 assert(schema);
1651 }
1652
1653 if (found && (match->schema != new_node->schema)) {
1654 /* find the next node after we have found our node schema data instance */
1655 break;
1656 }
1657 }
Michal Vasko90932a92020-02-12 14:33:03 +01001658 }
1659
1660 return match;
1661}
1662
1663/**
1664 * @brief Insert node after a sibling.
1665 *
Michal Vasko9f96a052020-03-10 09:41:45 +01001666 * Handles inserting into NP containers and key-less lists.
1667 *
Michal Vasko90932a92020-02-12 14:33:03 +01001668 * @param[in] sibling Sibling to insert after.
1669 * @param[in] node Node to insert.
1670 */
1671static void
Michal Vaskof03ed032020-03-04 13:31:44 +01001672lyd_insert_after_node(struct lyd_node *sibling, struct lyd_node *node)
Michal Vasko90932a92020-02-12 14:33:03 +01001673{
Michal Vasko0249f7c2020-03-05 16:36:40 +01001674 struct lyd_node_inner *par;
1675
Michal Vasko90932a92020-02-12 14:33:03 +01001676 assert(!node->next && (node->prev == node));
1677
1678 node->next = sibling->next;
1679 node->prev = sibling;
1680 sibling->next = node;
1681 if (node->next) {
1682 /* sibling had a succeeding node */
1683 node->next->prev = node;
1684 } else {
1685 /* sibling was last, find first sibling and change its prev */
1686 if (sibling->parent) {
1687 sibling = sibling->parent->child;
1688 } else {
Michal Vaskod989ba02020-08-24 10:59:24 +02001689 for ( ; sibling->prev->next != node; sibling = sibling->prev) {}
Michal Vasko90932a92020-02-12 14:33:03 +01001690 }
1691 sibling->prev = node;
1692 }
1693 node->parent = sibling->parent;
Michal Vasko0249f7c2020-03-05 16:36:40 +01001694
Michal Vasko9f96a052020-03-10 09:41:45 +01001695 for (par = node->parent; par; par = par->parent) {
1696 if ((par->flags & LYD_DEFAULT) && !(node->flags & LYD_DEFAULT)) {
1697 /* remove default flags from NP containers */
Michal Vasko0249f7c2020-03-05 16:36:40 +01001698 par->flags &= ~LYD_DEFAULT;
1699 }
Michal Vaskob104f112020-07-17 09:54:54 +02001700 if (par->schema && (par->schema->nodetype == LYS_LIST) && (par->schema->flags & LYS_KEYLESS)) {
Michal Vasko9f96a052020-03-10 09:41:45 +01001701 /* rehash key-less list */
1702 lyd_hash((struct lyd_node *)par);
1703 }
Michal Vasko0249f7c2020-03-05 16:36:40 +01001704 }
Michal Vasko90932a92020-02-12 14:33:03 +01001705}
1706
1707/**
1708 * @brief Insert node before a sibling.
1709 *
Michal Vasko9f96a052020-03-10 09:41:45 +01001710 * Handles inserting into NP containers and key-less lists.
1711 *
Michal Vasko90932a92020-02-12 14:33:03 +01001712 * @param[in] sibling Sibling to insert before.
1713 * @param[in] node Node to insert.
1714 */
1715static void
Michal Vaskof03ed032020-03-04 13:31:44 +01001716lyd_insert_before_node(struct lyd_node *sibling, struct lyd_node *node)
Michal Vasko90932a92020-02-12 14:33:03 +01001717{
Michal Vasko0249f7c2020-03-05 16:36:40 +01001718 struct lyd_node_inner *par;
1719
Michal Vasko90932a92020-02-12 14:33:03 +01001720 assert(!node->next && (node->prev == node));
1721
1722 node->next = sibling;
1723 /* covers situation of sibling being first */
1724 node->prev = sibling->prev;
1725 sibling->prev = node;
1726 if (node->prev->next) {
1727 /* sibling had a preceding node */
1728 node->prev->next = node;
1729 } else if (sibling->parent) {
1730 /* sibling was first and we must also change parent child pointer */
1731 sibling->parent->child = node;
1732 }
1733 node->parent = sibling->parent;
Michal Vasko0249f7c2020-03-05 16:36:40 +01001734
Michal Vasko9f96a052020-03-10 09:41:45 +01001735 for (par = node->parent; par; par = par->parent) {
1736 if ((par->flags & LYD_DEFAULT) && !(node->flags & LYD_DEFAULT)) {
1737 /* remove default flags from NP containers */
Michal Vasko0249f7c2020-03-05 16:36:40 +01001738 par->flags &= ~LYD_DEFAULT;
1739 }
Michal Vaskob104f112020-07-17 09:54:54 +02001740 if (par->schema && (par->schema->nodetype == LYS_LIST) && (par->schema->flags & LYS_KEYLESS)) {
Michal Vasko9f96a052020-03-10 09:41:45 +01001741 /* rehash key-less list */
1742 lyd_hash((struct lyd_node *)par);
1743 }
Michal Vasko0249f7c2020-03-05 16:36:40 +01001744 }
Michal Vasko90932a92020-02-12 14:33:03 +01001745}
1746
1747/**
Michal Vaskob104f112020-07-17 09:54:54 +02001748 * @brief Insert node as the first and only child of a parent.
Michal Vasko90932a92020-02-12 14:33:03 +01001749 *
Michal Vasko9f96a052020-03-10 09:41:45 +01001750 * Handles inserting into NP containers and key-less lists.
1751 *
Michal Vasko90932a92020-02-12 14:33:03 +01001752 * @param[in] parent Parent to insert into.
1753 * @param[in] node Node to insert.
1754 */
1755static void
Michal Vaskob104f112020-07-17 09:54:54 +02001756lyd_insert_only_child(struct lyd_node *parent, struct lyd_node *node)
Michal Vasko90932a92020-02-12 14:33:03 +01001757{
1758 struct lyd_node_inner *par;
1759
Radek Krejcia1c1e542020-09-29 16:06:52 +02001760 assert(parent && !lyd_child(parent) && !node->next && (node->prev == node));
Michal Vasko52927e22020-03-16 17:26:14 +01001761 assert(!parent->schema || (parent->schema->nodetype & LYD_NODE_INNER));
Michal Vasko90932a92020-02-12 14:33:03 +01001762
1763 par = (struct lyd_node_inner *)parent;
1764
Michal Vaskob104f112020-07-17 09:54:54 +02001765 par->child = node;
Michal Vasko90932a92020-02-12 14:33:03 +01001766 node->parent = par;
Michal Vasko0249f7c2020-03-05 16:36:40 +01001767
Michal Vaskod989ba02020-08-24 10:59:24 +02001768 for ( ; par; par = par->parent) {
Michal Vasko9f96a052020-03-10 09:41:45 +01001769 if ((par->flags & LYD_DEFAULT) && !(node->flags & LYD_DEFAULT)) {
1770 /* remove default flags from NP containers */
Michal Vasko0249f7c2020-03-05 16:36:40 +01001771 par->flags &= ~LYD_DEFAULT;
1772 }
Michal Vasko52927e22020-03-16 17:26:14 +01001773 if (par->schema && (par->schema->nodetype == LYS_LIST) && (par->schema->flags & LYS_KEYLESS)) {
Michal Vasko9f96a052020-03-10 09:41:45 +01001774 /* rehash key-less list */
1775 lyd_hash((struct lyd_node *)par);
1776 }
Michal Vasko0249f7c2020-03-05 16:36:40 +01001777 }
Michal Vasko751cb4d2020-07-14 12:25:28 +02001778}
Michal Vasko0249f7c2020-03-05 16:36:40 +01001779
Michal Vasko751cb4d2020-07-14 12:25:28 +02001780/**
1781 * @brief Learn whether a list instance has all the keys.
1782 *
1783 * @param[in] list List instance to check.
1784 * @return non-zero if all the keys were found,
1785 * @return 0 otherwise.
1786 */
1787static int
1788lyd_insert_has_keys(const struct lyd_node *list)
1789{
1790 const struct lyd_node *key;
1791 const struct lysc_node *skey = NULL;
1792
1793 assert(list->schema->nodetype == LYS_LIST);
Radek Krejcia1c1e542020-09-29 16:06:52 +02001794 key = lyd_child(list);
Michal Vasko751cb4d2020-07-14 12:25:28 +02001795 while ((skey = lys_getnext(skey, list->schema, NULL, 0)) && (skey->flags & LYS_KEY)) {
1796 if (!key || (key->schema != skey)) {
1797 /* key missing */
1798 return 0;
1799 }
1800
1801 key = key->next;
1802 }
1803
1804 /* all keys found */
1805 return 1;
Michal Vasko90932a92020-02-12 14:33:03 +01001806}
1807
1808void
Michal Vaskob104f112020-07-17 09:54:54 +02001809lyd_insert_node(struct lyd_node *parent, struct lyd_node **first_sibling_p, struct lyd_node *node)
Michal Vasko90932a92020-02-12 14:33:03 +01001810{
Michal Vaskob104f112020-07-17 09:54:54 +02001811 struct lyd_node *anchor, *first_sibling;
Michal Vasko90932a92020-02-12 14:33:03 +01001812
Michal Vaskob104f112020-07-17 09:54:54 +02001813 /* inserting list without its keys is not supported */
1814 assert((parent || first_sibling_p) && node && (node->hash || !node->schema));
Michal Vasko9b368d32020-02-14 13:53:31 +01001815
Michal Vaskob104f112020-07-17 09:54:54 +02001816 if (!parent && first_sibling_p && (*first_sibling_p) && (*first_sibling_p)->parent) {
1817 parent = (struct lyd_node *)(*first_sibling_p)->parent;
Michal Vasko9b368d32020-02-14 13:53:31 +01001818 }
Michal Vasko90932a92020-02-12 14:33:03 +01001819
Michal Vaskob104f112020-07-17 09:54:54 +02001820 /* get first sibling */
1821 first_sibling = parent ? ((struct lyd_node_inner *)parent)->child : *first_sibling_p;
Michal Vasko9f96a052020-03-10 09:41:45 +01001822
Michal Vaskob104f112020-07-17 09:54:54 +02001823 /* find the anchor, our next node, so we can insert before it */
1824 anchor = lyd_insert_get_next_anchor(first_sibling, node);
1825 if (anchor) {
1826 lyd_insert_before_node(anchor, node);
1827 } else if (first_sibling) {
1828 lyd_insert_after_node(first_sibling->prev, node);
1829 } else if (parent) {
1830 lyd_insert_only_child(parent, node);
Michal Vasko90932a92020-02-12 14:33:03 +01001831 } else {
Michal Vaskob104f112020-07-17 09:54:54 +02001832 *first_sibling_p = node;
1833 }
1834
1835 /* insert into parent HT */
1836 lyd_insert_hash(node);
1837
1838 /* finish hashes for our parent, if needed and possible */
Michal Vasko9e8de2d2020-09-01 08:17:10 +02001839 if (node->schema && (node->schema->flags & LYS_KEY) && parent && lyd_insert_has_keys(parent)) {
Michal Vaskob104f112020-07-17 09:54:54 +02001840 lyd_hash(parent);
1841
1842 /* now we can insert even the list into its parent HT */
1843 lyd_insert_hash(parent);
Michal Vasko90932a92020-02-12 14:33:03 +01001844 }
Michal Vasko90932a92020-02-12 14:33:03 +01001845}
1846
Michal Vaskof03ed032020-03-04 13:31:44 +01001847static LY_ERR
1848lyd_insert_check_schema(const struct lysc_node *parent, const struct lysc_node *schema)
1849{
1850 const struct lysc_node *par2;
1851
1852 assert(schema);
Michal Vasko62ed12d2020-05-21 10:08:25 +02001853 assert(!parent || !(parent->nodetype & (LYS_CASE | LYS_CHOICE)));
Michal Vaskof03ed032020-03-04 13:31:44 +01001854
1855 /* find schema parent */
Michal Vasko62ed12d2020-05-21 10:08:25 +02001856 par2 = lysc_data_parent(schema);
Michal Vaskof03ed032020-03-04 13:31:44 +01001857
1858 if (parent) {
1859 /* inner node */
1860 if (par2 != parent) {
Michal Vaskob104f112020-07-17 09:54:54 +02001861 LOGERR(schema->module->ctx, LY_EINVAL, "Cannot insert, parent of \"%s\" is not \"%s\".", schema->name,
Michal Vasko69730152020-10-09 16:30:07 +02001862 parent->name);
Michal Vaskof03ed032020-03-04 13:31:44 +01001863 return LY_EINVAL;
1864 }
1865 } else {
1866 /* top-level node */
1867 if (par2) {
Radek Krejcif6d14cb2020-07-02 16:11:45 +02001868 LOGERR(schema->module->ctx, LY_EINVAL, "Cannot insert, node \"%s\" is not top-level.", schema->name);
Michal Vaskof03ed032020-03-04 13:31:44 +01001869 return LY_EINVAL;
1870 }
1871 }
1872
1873 return LY_SUCCESS;
1874}
1875
1876API LY_ERR
Michal Vaskob104f112020-07-17 09:54:54 +02001877lyd_insert_child(struct lyd_node *parent, struct lyd_node *node)
Michal Vaskof03ed032020-03-04 13:31:44 +01001878{
1879 struct lyd_node *iter;
1880
Michal Vasko654bc852020-06-23 13:28:06 +02001881 LY_CHECK_ARG_RET(NULL, parent, node, parent->schema->nodetype & LYD_NODE_INNER, LY_EINVAL);
Michal Vaskof03ed032020-03-04 13:31:44 +01001882
1883 LY_CHECK_RET(lyd_insert_check_schema(parent->schema, node->schema));
1884
1885 if (node->schema->flags & LYS_KEY) {
1886 LOGERR(parent->schema->module->ctx, LY_EINVAL, "Cannot insert key \"%s\".", node->schema->name);
1887 return LY_EINVAL;
1888 }
1889
1890 if (node->parent || node->prev->next) {
1891 lyd_unlink_tree(node);
1892 }
1893
1894 while (node) {
1895 iter = node->next;
1896 lyd_unlink_tree(node);
1897 lyd_insert_node(parent, NULL, node);
1898 node = iter;
1899 }
1900 return LY_SUCCESS;
1901}
1902
1903API LY_ERR
Michal Vaskob104f112020-07-17 09:54:54 +02001904lyd_insert_sibling(struct lyd_node *sibling, struct lyd_node *node, struct lyd_node **first)
Michal Vaskob1b5c262020-03-05 14:29:47 +01001905{
1906 struct lyd_node *iter;
1907
Michal Vaskob104f112020-07-17 09:54:54 +02001908 LY_CHECK_ARG_RET(NULL, node, LY_EINVAL);
Michal Vaskob1b5c262020-03-05 14:29:47 +01001909
Michal Vaskob104f112020-07-17 09:54:54 +02001910 if (sibling) {
1911 LY_CHECK_RET(lyd_insert_check_schema(lysc_data_parent(sibling->schema), node->schema));
Michal Vaskob1b5c262020-03-05 14:29:47 +01001912 }
1913
1914 if (node->parent || node->prev->next) {
1915 lyd_unlink_tree(node);
1916 }
1917
1918 while (node) {
Michal Vaskob104f112020-07-17 09:54:54 +02001919 if (node->schema->flags & LYS_KEY) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02001920 LOGERR(LYD_CTX(node), LY_EINVAL, "Cannot insert key \"%s\".", node->schema->name);
Michal Vaskob104f112020-07-17 09:54:54 +02001921 return LY_EINVAL;
1922 }
1923
Michal Vaskob1b5c262020-03-05 14:29:47 +01001924 iter = node->next;
1925 lyd_unlink_tree(node);
1926 lyd_insert_node(NULL, &sibling, node);
1927 node = iter;
1928 }
Michal Vaskob1b5c262020-03-05 14:29:47 +01001929
Michal Vaskob104f112020-07-17 09:54:54 +02001930 if (first) {
1931 /* find the first sibling */
1932 *first = sibling;
1933 while ((*first)->prev->next) {
1934 *first = (*first)->prev;
Michal Vasko0249f7c2020-03-05 16:36:40 +01001935 }
1936 }
1937
1938 return LY_SUCCESS;
1939}
1940
Michal Vaskob1b5c262020-03-05 14:29:47 +01001941API LY_ERR
Michal Vaskof03ed032020-03-04 13:31:44 +01001942lyd_insert_before(struct lyd_node *sibling, struct lyd_node *node)
1943{
1944 struct lyd_node *iter;
1945
1946 LY_CHECK_ARG_RET(NULL, sibling, node, LY_EINVAL);
1947
Michal Vasko62ed12d2020-05-21 10:08:25 +02001948 LY_CHECK_RET(lyd_insert_check_schema(lysc_data_parent(sibling->schema), node->schema));
Michal Vaskof03ed032020-03-04 13:31:44 +01001949
Michal Vaskob104f112020-07-17 09:54:54 +02001950 if (!(node->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) || !(node->schema->flags & LYS_ORDBY_USER)) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02001951 LOGERR(LYD_CTX(sibling), LY_EINVAL, "Can be used only for user-ordered nodes.");
Michal Vaskof03ed032020-03-04 13:31:44 +01001952 return LY_EINVAL;
1953 }
1954
1955 if (node->parent || node->prev->next) {
1956 lyd_unlink_tree(node);
1957 }
1958
1959 /* insert in reverse order to get the original order */
1960 node = node->prev;
1961 while (node) {
1962 iter = node->prev;
1963 lyd_unlink_tree(node);
1964
1965 lyd_insert_before_node(sibling, node);
Michal Vasko751cb4d2020-07-14 12:25:28 +02001966 lyd_insert_hash(node);
1967
Michal Vaskof03ed032020-03-04 13:31:44 +01001968 /* move the anchor accordingly */
1969 sibling = node;
1970
1971 node = (iter == node) ? NULL : iter;
1972 }
1973 return LY_SUCCESS;
1974}
1975
1976API LY_ERR
1977lyd_insert_after(struct lyd_node *sibling, struct lyd_node *node)
1978{
1979 struct lyd_node *iter;
1980
1981 LY_CHECK_ARG_RET(NULL, sibling, node, LY_EINVAL);
1982
Michal Vasko62ed12d2020-05-21 10:08:25 +02001983 LY_CHECK_RET(lyd_insert_check_schema(lysc_data_parent(sibling->schema), node->schema));
Michal Vaskof03ed032020-03-04 13:31:44 +01001984
Michal Vaskob104f112020-07-17 09:54:54 +02001985 if (!(node->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) || !(node->schema->flags & LYS_ORDBY_USER)) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02001986 LOGERR(LYD_CTX(sibling), LY_EINVAL, "Can be used only for user-ordered nodes.");
Michal Vaskof03ed032020-03-04 13:31:44 +01001987 return LY_EINVAL;
1988 }
1989
1990 if (node->parent || node->prev->next) {
1991 lyd_unlink_tree(node);
1992 }
1993
1994 while (node) {
1995 iter = node->next;
1996 lyd_unlink_tree(node);
1997
1998 lyd_insert_after_node(sibling, node);
Michal Vasko751cb4d2020-07-14 12:25:28 +02001999 lyd_insert_hash(node);
2000
Michal Vaskof03ed032020-03-04 13:31:44 +01002001 /* move the anchor accordingly */
2002 sibling = node;
2003
2004 node = iter;
2005 }
2006 return LY_SUCCESS;
2007}
2008
2009API void
2010lyd_unlink_tree(struct lyd_node *node)
2011{
2012 struct lyd_node *iter;
2013
2014 if (!node) {
2015 return;
2016 }
2017
Michal Vaskob104f112020-07-17 09:54:54 +02002018 /* update hashes while still linked into the tree */
2019 lyd_unlink_hash(node);
2020
Michal Vaskof03ed032020-03-04 13:31:44 +01002021 /* unlink from siblings */
2022 if (node->prev->next) {
2023 node->prev->next = node->next;
2024 }
2025 if (node->next) {
2026 node->next->prev = node->prev;
2027 } else {
2028 /* unlinking the last node */
2029 if (node->parent) {
2030 iter = node->parent->child;
2031 } else {
2032 iter = node->prev;
2033 while (iter->prev != node) {
2034 iter = iter->prev;
2035 }
2036 }
2037 /* update the "last" pointer from the first node */
2038 iter->prev = node->prev;
2039 }
2040
2041 /* unlink from parent */
2042 if (node->parent) {
2043 if (node->parent->child == node) {
2044 /* the node is the first child */
2045 node->parent->child = node->next;
2046 }
2047
Michal Vaskoab49dbe2020-07-17 12:32:47 +02002048 /* check for NP container whether its last non-default node is not being unlinked */
Michal Vasko69730152020-10-09 16:30:07 +02002049 if (node->parent->schema && (node->parent->schema->nodetype == LYS_CONTAINER) &&
2050 !(node->parent->flags & LYD_DEFAULT) && !(node->parent->schema->flags & LYS_PRESENCE)) {
Michal Vaskoab49dbe2020-07-17 12:32:47 +02002051 LY_LIST_FOR(node->parent->child, iter) {
2052 if ((iter != node) && !(iter->flags & LYD_DEFAULT)) {
2053 break;
2054 }
2055 }
2056 if (!iter) {
2057 node->parent->flags |= LYD_DEFAULT;
2058 }
2059 }
2060
Michal Vaskof03ed032020-03-04 13:31:44 +01002061 /* check for keyless list and update its hash */
2062 for (iter = (struct lyd_node *)node->parent; iter; iter = (struct lyd_node *)iter->parent) {
Michal Vasko413c7f22020-05-05 12:34:06 +02002063 if (iter->schema && (iter->schema->flags & LYS_KEYLESS)) {
Michal Vaskof03ed032020-03-04 13:31:44 +01002064 lyd_hash(iter);
2065 }
2066 }
2067
2068 node->parent = NULL;
2069 }
2070
2071 node->next = NULL;
2072 node->prev = node;
2073}
2074
Michal Vaskoa5da3292020-08-12 13:10:50 +02002075void
Radek Krejci1798aae2020-07-14 13:26:06 +02002076lyd_insert_meta(struct lyd_node *parent, struct lyd_meta *meta)
2077{
2078 struct lyd_meta *last, *iter;
2079
2080 assert(parent);
Michal Vaskoa5da3292020-08-12 13:10:50 +02002081
2082 if (!meta) {
2083 return;
2084 }
Radek Krejci1798aae2020-07-14 13:26:06 +02002085
2086 for (iter = meta; iter; iter = iter->next) {
2087 iter->parent = parent;
2088 }
2089
2090 /* insert as the last attribute */
2091 if (parent->meta) {
Radek Krejci1e008d22020-08-17 11:37:37 +02002092 for (last = parent->meta; last->next; last = last->next) {}
Radek Krejci1798aae2020-07-14 13:26:06 +02002093 last->next = meta;
2094 } else {
2095 parent->meta = meta;
2096 }
2097
2098 /* remove default flags from NP containers */
2099 while (parent && (parent->schema->nodetype == LYS_CONTAINER) && (parent->flags & LYD_DEFAULT)) {
2100 parent->flags &= ~LYD_DEFAULT;
2101 parent = (struct lyd_node *)parent->parent;
2102 }
Radek Krejci1798aae2020-07-14 13:26:06 +02002103}
2104
2105LY_ERR
Michal Vasko9f96a052020-03-10 09:41:45 +01002106lyd_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 +02002107 size_t name_len, const char *value, size_t value_len, ly_bool *dynamic, LY_PREFIX_FORMAT format,
2108 void *prefix_data, uint32_t hints, ly_bool *incomplete)
Michal Vasko90932a92020-02-12 14:33:03 +01002109{
Michal Vaskofeca4fb2020-10-05 08:58:40 +02002110 LY_ERR rc;
Michal Vasko90932a92020-02-12 14:33:03 +01002111 struct lysc_ext_instance *ant = NULL;
Michal Vasko9f96a052020-03-10 09:41:45 +01002112 struct lyd_meta *mt, *last;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002113 LY_ARRAY_COUNT_TYPE u;
Michal Vasko90932a92020-02-12 14:33:03 +01002114
Michal Vasko9f96a052020-03-10 09:41:45 +01002115 assert((parent || meta) && mod);
Michal Vasko6f4cbb62020-02-28 11:15:47 +01002116
Radek Krejci7eb54ba2020-05-18 16:30:04 +02002117 LY_ARRAY_FOR(mod->compiled->exts, u) {
Michal Vasko69730152020-10-09 16:30:07 +02002118 if ((mod->compiled->exts[u].def->plugin == lyext_plugins_internal[LYEXT_PLUGIN_INTERNAL_ANNOTATION].plugin) &&
Radek Krejci7eb54ba2020-05-18 16:30:04 +02002119 !ly_strncmp(mod->compiled->exts[u].argument, name, name_len)) {
Michal Vasko90932a92020-02-12 14:33:03 +01002120 /* we have the annotation definition */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02002121 ant = &mod->compiled->exts[u];
Michal Vasko90932a92020-02-12 14:33:03 +01002122 break;
2123 }
2124 }
2125 if (!ant) {
2126 /* attribute is not defined as a metadata annotation (RFC 7952) */
2127 LOGERR(mod->ctx, LY_EINVAL, "Annotation definition for attribute \"%s:%.*s\" not found.",
2128 mod->name, name_len, name);
2129 return LY_EINVAL;
2130 }
2131
Michal Vasko9f96a052020-03-10 09:41:45 +01002132 mt = calloc(1, sizeof *mt);
2133 LY_CHECK_ERR_RET(!mt, LOGMEM(mod->ctx), LY_EMEM);
2134 mt->parent = parent;
2135 mt->annotation = ant;
Michal Vaskofeca4fb2020-10-05 08:58:40 +02002136 rc = lyd_value_store(mod->ctx, &mt->value, ((struct lyext_metadata *)ant->data)->type, value, value_len, dynamic,
2137 format, prefix_data, hints, parent ? parent->schema : NULL, incomplete, LY_VLOG_NONE, NULL);
2138 LY_CHECK_ERR_RET(rc, free(mt), rc);
Radek Krejci011e4aa2020-09-04 15:22:31 +02002139 rc = lydict_insert(mod->ctx, name, name_len, &mt->name);
2140 LY_CHECK_ERR_RET(rc, free(mt), rc);
Michal Vasko90932a92020-02-12 14:33:03 +01002141
Michal Vasko6f4cbb62020-02-28 11:15:47 +01002142 /* insert as the last attribute */
2143 if (parent) {
Michal Vaskoa5da3292020-08-12 13:10:50 +02002144 lyd_insert_meta(parent, mt);
Michal Vasko9f96a052020-03-10 09:41:45 +01002145 } else if (*meta) {
Radek Krejci1e008d22020-08-17 11:37:37 +02002146 for (last = *meta; last->next; last = last->next) {}
Michal Vasko9f96a052020-03-10 09:41:45 +01002147 last->next = mt;
Michal Vasko90932a92020-02-12 14:33:03 +01002148 }
2149
Michal Vasko9f96a052020-03-10 09:41:45 +01002150 if (meta) {
2151 *meta = mt;
Michal Vasko90932a92020-02-12 14:33:03 +01002152 }
Michal Vaskofeca4fb2020-10-05 08:58:40 +02002153 return LY_SUCCESS;
Michal Vasko90932a92020-02-12 14:33:03 +01002154}
2155
Michal Vaskoa5da3292020-08-12 13:10:50 +02002156void
2157lyd_insert_attr(struct lyd_node *parent, struct lyd_attr *attr)
2158{
2159 struct lyd_attr *last, *iter;
2160 struct lyd_node_opaq *opaq;
2161
2162 assert(parent && !parent->schema);
2163
2164 if (!attr) {
2165 return;
2166 }
2167
2168 opaq = (struct lyd_node_opaq *)parent;
2169 for (iter = attr; iter; iter = iter->next) {
2170 iter->parent = opaq;
2171 }
2172
2173 /* insert as the last attribute */
2174 if (opaq->attr) {
Radek Krejci1e008d22020-08-17 11:37:37 +02002175 for (last = opaq->attr; last->next; last = last->next) {}
Michal Vaskoa5da3292020-08-12 13:10:50 +02002176 last->next = attr;
2177 } else {
2178 opaq->attr = attr;
2179 }
2180}
2181
Michal Vasko52927e22020-03-16 17:26:14 +01002182LY_ERR
Michal Vaskofeca4fb2020-10-05 08:58:40 +02002183lyd_create_attr(struct lyd_node *parent, struct lyd_attr **attr, const struct ly_ctx *ctx, const char *name, size_t name_len,
2184 const char *value, size_t value_len, ly_bool *dynamic, LYD_FORMAT format, uint32_t hints,
Radek Krejci0f969882020-08-21 16:56:47 +02002185 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 +01002186{
Radek Krejci011e4aa2020-09-04 15:22:31 +02002187 LY_ERR ret = LY_SUCCESS;
Radek Krejci1798aae2020-07-14 13:26:06 +02002188 struct lyd_attr *at, *last;
Michal Vasko52927e22020-03-16 17:26:14 +01002189
2190 assert(ctx && (parent || attr) && (!parent || !parent->schema));
Michal Vaskofeca4fb2020-10-05 08:58:40 +02002191 assert(name && name_len && format);
Michal Vasko52927e22020-03-16 17:26:14 +01002192
2193 if (!value_len) {
2194 value = "";
2195 }
2196
2197 at = calloc(1, sizeof *at);
Radek Krejcid46e46a2020-09-15 14:22:42 +02002198 LY_CHECK_ERR_RET(!at, LOGMEM(ctx); ly_free_val_prefs(ctx, val_prefs), LY_EMEM);
Michal Vaskofeca4fb2020-10-05 08:58:40 +02002199 at->hints = hints;
Radek Krejcid46e46a2020-09-15 14:22:42 +02002200 at->format = format;
2201 at->val_prefs = val_prefs;
2202
Radek Krejci011e4aa2020-09-04 15:22:31 +02002203 LY_CHECK_GOTO(ret = lydict_insert(ctx, name, name_len, &at->name), finish);
Michal Vasko52927e22020-03-16 17:26:14 +01002204 if (dynamic && *dynamic) {
Radek Krejci011e4aa2020-09-04 15:22:31 +02002205 ret = lydict_insert_zc(ctx, (char *)value, &at->value);
2206 LY_CHECK_GOTO(ret, finish);
Michal Vasko52927e22020-03-16 17:26:14 +01002207 *dynamic = 0;
2208 } else {
Radek Krejci011e4aa2020-09-04 15:22:31 +02002209 LY_CHECK_GOTO(ret = lydict_insert(ctx, value, value_len, &at->value), finish);
Michal Vasko52927e22020-03-16 17:26:14 +01002210 }
2211
Radek Krejci1798aae2020-07-14 13:26:06 +02002212 if (prefix_len) {
Radek Krejci011e4aa2020-09-04 15:22:31 +02002213 LY_CHECK_GOTO(ret = lydict_insert(ctx, prefix, prefix_len, &at->prefix.id), finish);
Radek Krejci1798aae2020-07-14 13:26:06 +02002214 }
2215 if (module_key_len) {
Radek Krejci011e4aa2020-09-04 15:22:31 +02002216 LY_CHECK_GOTO(ret = lydict_insert(ctx, module_key, module_key_len, &at->prefix.module_ns), finish);
Michal Vasko52927e22020-03-16 17:26:14 +01002217 }
2218
2219 /* insert as the last attribute */
2220 if (parent) {
Michal Vaskoa5da3292020-08-12 13:10:50 +02002221 lyd_insert_attr(parent, at);
Michal Vasko52927e22020-03-16 17:26:14 +01002222 } else if (*attr) {
Radek Krejci1e008d22020-08-17 11:37:37 +02002223 for (last = *attr; last->next; last = last->next) {}
Michal Vasko52927e22020-03-16 17:26:14 +01002224 last->next = at;
2225 }
2226
Radek Krejci011e4aa2020-09-04 15:22:31 +02002227finish:
2228 if (ret) {
2229 lyd_free_attr_single(ctx, at);
2230 } else if (attr) {
Michal Vasko52927e22020-03-16 17:26:14 +01002231 *attr = at;
2232 }
2233 return LY_SUCCESS;
2234}
2235
Radek Krejci084289f2019-07-09 17:35:30 +02002236API const struct lyd_node_term *
Michal Vasko004d3152020-06-11 19:59:22 +02002237lyd_target(const struct ly_path *path, const struct lyd_node *tree)
Radek Krejci084289f2019-07-09 17:35:30 +02002238{
Michal Vasko004d3152020-06-11 19:59:22 +02002239 struct lyd_node *target;
Radek Krejci084289f2019-07-09 17:35:30 +02002240
Michal Vasko004d3152020-06-11 19:59:22 +02002241 if (ly_path_eval(path, tree, &target)) {
2242 return NULL;
Radek Krejci084289f2019-07-09 17:35:30 +02002243 }
2244
Michal Vasko004d3152020-06-11 19:59:22 +02002245 return (struct lyd_node_term *)target;
Radek Krejci084289f2019-07-09 17:35:30 +02002246}
2247
Radek Krejci1f05b6a2019-07-18 16:15:06 +02002248API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02002249lyd_compare_single(const struct lyd_node *node1, const struct lyd_node *node2, uint32_t options)
Radek Krejci1f05b6a2019-07-18 16:15:06 +02002250{
2251 const struct lyd_node *iter1, *iter2;
2252 struct lyd_node_term *term1, *term2;
2253 struct lyd_node_any *any1, *any2;
Michal Vasko52927e22020-03-16 17:26:14 +01002254 struct lyd_node_opaq *opaq1, *opaq2;
Radek Krejci1f05b6a2019-07-18 16:15:06 +02002255 size_t len1, len2;
Radek Krejci084289f2019-07-09 17:35:30 +02002256
Radek Krejci1f05b6a2019-07-18 16:15:06 +02002257 if (!node1 || !node2) {
2258 if (node1 == node2) {
2259 return LY_SUCCESS;
2260 } else {
2261 return LY_ENOT;
2262 }
2263 }
2264
Michal Vaskob7be7a82020-08-20 09:09:04 +02002265 if ((LYD_CTX(node1) != LYD_CTX(node2)) || (node1->schema != node2->schema)) {
Radek Krejci1f05b6a2019-07-18 16:15:06 +02002266 return LY_ENOT;
2267 }
2268
2269 if (node1->hash != node2->hash) {
2270 return LY_ENOT;
2271 }
Michal Vasko52927e22020-03-16 17:26:14 +01002272 /* 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 +02002273
Michal Vasko52927e22020-03-16 17:26:14 +01002274 if (!node1->schema) {
2275 opaq1 = (struct lyd_node_opaq *)node1;
2276 opaq2 = (struct lyd_node_opaq *)node2;
Radek Krejci1798aae2020-07-14 13:26:06 +02002277 if ((opaq1->name != opaq2->name) || (opaq1->format != opaq2->format) || (opaq1->prefix.module_ns != opaq2->prefix.module_ns)) {
Radek Krejci1f05b6a2019-07-18 16:15:06 +02002278 return LY_ENOT;
2279 }
Michal Vasko52927e22020-03-16 17:26:14 +01002280 switch (opaq1->format) {
2281 case LYD_XML:
2282 if (lyxml_value_compare(opaq1->value, opaq1->val_prefs, opaq2->value, opaq2->val_prefs)) {
2283 return LY_ENOT;
2284 }
2285 break;
Radek Krejci1798aae2020-07-14 13:26:06 +02002286 case LYD_JSON:
2287 /* prefixes in JSON are unique, so it is not necessary to canonize the values */
2288 if (strcmp(opaq1->value, opaq2->value)) {
2289 return LY_ENOT;
2290 }
2291 break;
Michal Vasko60ea6352020-06-29 13:39:39 +02002292 case LYD_LYB:
Michal Vaskoc8a230d2020-08-14 12:17:10 +02002293 case LYD_UNKNOWN:
Michal Vasko52927e22020-03-16 17:26:14 +01002294 /* not allowed */
Michal Vaskob7be7a82020-08-20 09:09:04 +02002295 LOGINT(LYD_CTX(node1));
Michal Vasko52927e22020-03-16 17:26:14 +01002296 return LY_EINT;
2297 }
2298 if (options & LYD_COMPARE_FULL_RECURSION) {
2299 iter1 = opaq1->child;
2300 iter2 = opaq2->child;
Radek Krejci1f05b6a2019-07-18 16:15:06 +02002301 goto all_children_compare;
Michal Vasko52927e22020-03-16 17:26:14 +01002302 }
2303 return LY_SUCCESS;
2304 } else {
2305 switch (node1->schema->nodetype) {
2306 case LYS_LEAF:
2307 case LYS_LEAFLIST:
2308 if (options & LYD_COMPARE_DEFAULTS) {
2309 if ((node1->flags & LYD_DEFAULT) != (node2->flags & LYD_DEFAULT)) {
2310 return LY_ENOT;
2311 }
2312 }
2313
Michal Vasko1a2bf2e2020-06-16 13:07:48 +02002314 term1 = (struct lyd_node_term *)node1;
2315 term2 = (struct lyd_node_term *)node2;
2316 if (term1->value.realtype != term2->value.realtype) {
2317 return LY_ENOT;
2318 }
Michal Vasko52927e22020-03-16 17:26:14 +01002319
Michal Vasko1a2bf2e2020-06-16 13:07:48 +02002320 return term1->value.realtype->plugin->compare(&term1->value, &term2->value);
Michal Vasko52927e22020-03-16 17:26:14 +01002321 case LYS_CONTAINER:
2322 if (options & LYD_COMPARE_DEFAULTS) {
2323 if ((node1->flags & LYD_DEFAULT) != (node2->flags & LYD_DEFAULT)) {
2324 return LY_ENOT;
2325 }
2326 }
2327 if (options & LYD_COMPARE_FULL_RECURSION) {
Michal Vasko22df3f02020-08-24 13:29:22 +02002328 iter1 = ((struct lyd_node_inner *)node1)->child;
2329 iter2 = ((struct lyd_node_inner *)node2)->child;
Michal Vasko52927e22020-03-16 17:26:14 +01002330 goto all_children_compare;
Radek Krejci1f05b6a2019-07-18 16:15:06 +02002331 }
2332 return LY_SUCCESS;
Michal Vasko1bf09392020-03-27 12:38:10 +01002333 case LYS_RPC:
Michal Vasko52927e22020-03-16 17:26:14 +01002334 case LYS_ACTION:
2335 if (options & LYD_COMPARE_FULL_RECURSION) {
2336 /* TODO action/RPC
2337 goto all_children_compare;
2338 */
Radek Krejci1f05b6a2019-07-18 16:15:06 +02002339 }
2340 return LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +01002341 case LYS_NOTIF:
2342 if (options & LYD_COMPARE_FULL_RECURSION) {
2343 /* TODO Notification
2344 goto all_children_compare;
2345 */
2346 }
2347 return LY_SUCCESS;
2348 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +02002349 iter1 = ((struct lyd_node_inner *)node1)->child;
2350 iter2 = ((struct lyd_node_inner *)node2)->child;
Michal Vasko52927e22020-03-16 17:26:14 +01002351
2352 if (!(node1->schema->flags & LYS_KEYLESS) && !(options & LYD_COMPARE_FULL_RECURSION)) {
2353 /* lists with keys, their equivalence is based on their keys */
Michal Vasko22df3f02020-08-24 13:29:22 +02002354 for (struct lysc_node *key = ((struct lysc_node_list *)node1->schema)->child;
Michal Vaskoc57d9a92020-06-23 13:28:27 +02002355 key && (key->flags & LYS_KEY);
Michal Vasko52927e22020-03-16 17:26:14 +01002356 key = key->next) {
Michal Vasko8f359bf2020-07-28 10:41:15 +02002357 if (lyd_compare_single(iter1, iter2, options)) {
Michal Vasko52927e22020-03-16 17:26:14 +01002358 return LY_ENOT;
2359 }
2360 iter1 = iter1->next;
2361 iter2 = iter2->next;
2362 }
2363 } else {
2364 /* lists without keys, their equivalence is based on equivalence of all the children (both direct and indirect) */
2365
Radek Krejci0f969882020-08-21 16:56:47 +02002366all_children_compare:
Michal Vasko52927e22020-03-16 17:26:14 +01002367 if (!iter1 && !iter2) {
2368 /* no children, nothing to compare */
2369 return LY_SUCCESS;
2370 }
2371
Michal Vaskod989ba02020-08-24 10:59:24 +02002372 for ( ; iter1 && iter2; iter1 = iter1->next, iter2 = iter2->next) {
Michal Vasko8f359bf2020-07-28 10:41:15 +02002373 if (lyd_compare_single(iter1, iter2, options | LYD_COMPARE_FULL_RECURSION)) {
Michal Vasko52927e22020-03-16 17:26:14 +01002374 return LY_ENOT;
2375 }
2376 }
2377 if (iter1 || iter2) {
2378 return LY_ENOT;
2379 }
2380 }
2381 return LY_SUCCESS;
2382 case LYS_ANYXML:
2383 case LYS_ANYDATA:
Michal Vasko22df3f02020-08-24 13:29:22 +02002384 any1 = (struct lyd_node_any *)node1;
2385 any2 = (struct lyd_node_any *)node2;
Michal Vasko52927e22020-03-16 17:26:14 +01002386
2387 if (any1->value_type != any2->value_type) {
2388 return LY_ENOT;
2389 }
2390 switch (any1->value_type) {
2391 case LYD_ANYDATA_DATATREE:
2392 iter1 = any1->value.tree;
2393 iter2 = any2->value.tree;
2394 goto all_children_compare;
2395 case LYD_ANYDATA_STRING:
2396 case LYD_ANYDATA_XML:
2397 case LYD_ANYDATA_JSON:
2398 len1 = strlen(any1->value.str);
2399 len2 = strlen(any2->value.str);
Michal Vasko69730152020-10-09 16:30:07 +02002400 if ((len1 != len2) || strcmp(any1->value.str, any2->value.str)) {
Michal Vasko52927e22020-03-16 17:26:14 +01002401 return LY_ENOT;
2402 }
2403 return LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +01002404 case LYD_ANYDATA_LYB:
Michal Vasko60ea6352020-06-29 13:39:39 +02002405 len1 = lyd_lyb_data_length(any1->value.mem);
2406 len2 = lyd_lyb_data_length(any2->value.mem);
Michal Vasko69730152020-10-09 16:30:07 +02002407 if ((len1 != len2) || memcmp(any1->value.mem, any2->value.mem, len1)) {
Michal Vasko52927e22020-03-16 17:26:14 +01002408 return LY_ENOT;
2409 }
2410 return LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +01002411 }
Radek Krejci1f05b6a2019-07-18 16:15:06 +02002412 }
2413 }
2414
Michal Vaskob7be7a82020-08-20 09:09:04 +02002415 LOGINT(LYD_CTX(node1));
Radek Krejci1f05b6a2019-07-18 16:15:06 +02002416 return LY_EINT;
2417}
Radek Krejci22ebdba2019-07-25 13:59:43 +02002418
Michal Vasko21725742020-06-29 11:49:25 +02002419API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02002420lyd_compare_siblings(const struct lyd_node *node1, const struct lyd_node *node2, uint32_t options)
Michal Vasko8f359bf2020-07-28 10:41:15 +02002421{
Michal Vaskod989ba02020-08-24 10:59:24 +02002422 for ( ; node1 && node2; node1 = node1->next, node2 = node2->next) {
Michal Vasko8f359bf2020-07-28 10:41:15 +02002423 LY_CHECK_RET(lyd_compare_single(node1, node2, options));
2424 }
2425
Michal Vasko11a81432020-07-28 16:31:29 +02002426 if (node1 == node2) {
2427 return LY_SUCCESS;
Michal Vasko8f359bf2020-07-28 10:41:15 +02002428 }
Michal Vasko11a81432020-07-28 16:31:29 +02002429 return LY_ENOT;
Michal Vasko8f359bf2020-07-28 10:41:15 +02002430}
2431
2432API LY_ERR
Michal Vasko21725742020-06-29 11:49:25 +02002433lyd_compare_meta(const struct lyd_meta *meta1, const struct lyd_meta *meta2)
2434{
2435 if (!meta1 || !meta2) {
2436 if (meta1 == meta2) {
2437 return LY_SUCCESS;
2438 } else {
2439 return LY_ENOT;
2440 }
2441 }
2442
Michal Vaskob7be7a82020-08-20 09:09:04 +02002443 if ((LYD_CTX(meta1->parent) != LYD_CTX(meta2->parent)) || (meta1->annotation != meta2->annotation)) {
Michal Vasko21725742020-06-29 11:49:25 +02002444 return LY_ENOT;
2445 }
2446
2447 if (meta1->value.realtype != meta2->value.realtype) {
2448 return LY_ENOT;
2449 }
2450
2451 return meta1->value.realtype->plugin->compare(&meta1->value, &meta2->value);
2452}
2453
Radek Krejci22ebdba2019-07-25 13:59:43 +02002454/**
Michal Vasko52927e22020-03-16 17:26:14 +01002455 * @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 +02002456 *
2457 * 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 +02002458 *
2459 * @param[in] node Original node to duplicate
2460 * @param[in] parent Parent to insert into, NULL for top-level sibling.
2461 * @param[in,out] first First sibling, NULL if no top-level sibling exist yet. Can be also NULL if @p parent is set.
2462 * @param[in] options Bitmask of options flags, see @ref dupoptions.
2463 * @param[out] dup_p Pointer where the created duplicated node is placed (besides connecting it int @p parent / @p first sibling).
2464 * @return LY_ERR value
Radek Krejci22ebdba2019-07-25 13:59:43 +02002465 */
Michal Vasko52927e22020-03-16 17:26:14 +01002466static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02002467lyd_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 +02002468 struct lyd_node **dup_p)
Radek Krejci22ebdba2019-07-25 13:59:43 +02002469{
Michal Vasko52927e22020-03-16 17:26:14 +01002470 LY_ERR ret;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002471 struct lyd_node *dup = NULL;
Michal Vasko61551fa2020-07-09 15:45:45 +02002472 struct lyd_meta *meta;
2473 struct lyd_node_any *any;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002474 LY_ARRAY_COUNT_TYPE u;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002475
Michal Vasko52927e22020-03-16 17:26:14 +01002476 LY_CHECK_ARG_RET(NULL, node, LY_EINVAL);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002477
Michal Vasko52927e22020-03-16 17:26:14 +01002478 if (!node->schema) {
2479 dup = calloc(1, sizeof(struct lyd_node_opaq));
2480 } else {
2481 switch (node->schema->nodetype) {
Michal Vasko1bf09392020-03-27 12:38:10 +01002482 case LYS_RPC:
Michal Vasko52927e22020-03-16 17:26:14 +01002483 case LYS_ACTION:
2484 case LYS_NOTIF:
2485 case LYS_CONTAINER:
2486 case LYS_LIST:
2487 dup = calloc(1, sizeof(struct lyd_node_inner));
2488 break;
2489 case LYS_LEAF:
2490 case LYS_LEAFLIST:
2491 dup = calloc(1, sizeof(struct lyd_node_term));
2492 break;
2493 case LYS_ANYDATA:
2494 case LYS_ANYXML:
2495 dup = calloc(1, sizeof(struct lyd_node_any));
2496 break;
2497 default:
Michal Vaskob7be7a82020-08-20 09:09:04 +02002498 LOGINT(LYD_CTX(node));
Michal Vasko52927e22020-03-16 17:26:14 +01002499 ret = LY_EINT;
2500 goto error;
2501 }
Radek Krejci22ebdba2019-07-25 13:59:43 +02002502 }
Michal Vaskob7be7a82020-08-20 09:09:04 +02002503 LY_CHECK_ERR_GOTO(!dup, LOGMEM(LYD_CTX(node)); ret = LY_EMEM, error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002504
Michal Vaskof6df0a02020-06-16 13:08:34 +02002505 if (options & LYD_DUP_WITH_FLAGS) {
2506 dup->flags = node->flags;
2507 } else {
2508 dup->flags = (node->flags & LYD_DEFAULT) | LYD_NEW;
2509 }
Radek Krejci22ebdba2019-07-25 13:59:43 +02002510 dup->schema = node->schema;
Michal Vasko52927e22020-03-16 17:26:14 +01002511 dup->prev = dup;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002512
Michal Vasko25a32822020-07-09 15:48:22 +02002513 /* duplicate metadata */
2514 if (!(options & LYD_DUP_NO_META)) {
2515 LY_LIST_FOR(node->meta, meta) {
Michal Vasko3a41dff2020-07-15 14:30:28 +02002516 LY_CHECK_GOTO(ret = lyd_dup_meta_single(meta, dup, NULL), error);
Michal Vasko25a32822020-07-09 15:48:22 +02002517 }
2518 }
Radek Krejci22ebdba2019-07-25 13:59:43 +02002519
2520 /* nodetype-specific work */
Michal Vasko52927e22020-03-16 17:26:14 +01002521 if (!dup->schema) {
2522 struct lyd_node_opaq *opaq = (struct lyd_node_opaq *)dup;
2523 struct lyd_node_opaq *orig = (struct lyd_node_opaq *)node;
2524 struct lyd_node *child;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002525
2526 if (options & LYD_DUP_RECURSIVE) {
2527 /* duplicate all the children */
2528 LY_LIST_FOR(orig->child, child) {
Michal Vasko3a41dff2020-07-15 14:30:28 +02002529 LY_CHECK_GOTO(ret = lyd_dup_r(child, dup, NULL, options, NULL), error);
Michal Vasko52927e22020-03-16 17:26:14 +01002530 }
2531 }
Radek Krejci011e4aa2020-09-04 15:22:31 +02002532 LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), orig->name, 0, &opaq->name), error);
Michal Vasko52927e22020-03-16 17:26:14 +01002533 opaq->format = orig->format;
Radek Krejci1798aae2020-07-14 13:26:06 +02002534 if (orig->prefix.id) {
Radek Krejci011e4aa2020-09-04 15:22:31 +02002535 LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), orig->prefix.id, 0, &opaq->prefix.id), error);
Michal Vasko52927e22020-03-16 17:26:14 +01002536 }
Radek Krejci011e4aa2020-09-04 15:22:31 +02002537 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 +01002538 if (orig->val_prefs) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02002539 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 +01002540 LY_ARRAY_FOR(orig->val_prefs, u) {
Radek Krejci011e4aa2020-09-04 15:22:31 +02002541 LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), orig->val_prefs[u].id, 0, &opaq->val_prefs[u].id), error);
2542 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 +01002543 LY_ARRAY_INCREMENT(opaq->val_prefs);
2544 }
2545 }
Radek Krejci011e4aa2020-09-04 15:22:31 +02002546 LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), orig->value, 0, &opaq->value), error);
Michal Vasko52927e22020-03-16 17:26:14 +01002547 opaq->ctx = orig->ctx;
2548 } else if (dup->schema->nodetype & LYD_NODE_TERM) {
2549 struct lyd_node_term *term = (struct lyd_node_term *)dup;
2550 struct lyd_node_term *orig = (struct lyd_node_term *)node;
2551
2552 term->hash = orig->hash;
Michal Vaskob7be7a82020-08-20 09:09:04 +02002553 LY_CHECK_ERR_GOTO(orig->value.realtype->plugin->duplicate(LYD_CTX(node), &orig->value, &term->value),
Michal Vasko69730152020-10-09 16:30:07 +02002554 LOGERR(LYD_CTX(node), LY_EINT, "Value duplication failed."); ret = LY_EINT, error);
Michal Vasko52927e22020-03-16 17:26:14 +01002555 } else if (dup->schema->nodetype & LYD_NODE_INNER) {
2556 struct lyd_node_inner *orig = (struct lyd_node_inner *)node;
2557 struct lyd_node *child;
2558
2559 if (options & LYD_DUP_RECURSIVE) {
2560 /* duplicate all the children */
2561 LY_LIST_FOR(orig->child, child) {
Michal Vasko3a41dff2020-07-15 14:30:28 +02002562 LY_CHECK_GOTO(ret = lyd_dup_r(child, dup, NULL, options, NULL), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002563 }
Michal Vasko69730152020-10-09 16:30:07 +02002564 } else if ((dup->schema->nodetype == LYS_LIST) && !(dup->schema->flags & LYS_KEYLESS)) {
Radek Krejci22ebdba2019-07-25 13:59:43 +02002565 /* always duplicate keys of a list */
Radek Krejci22ebdba2019-07-25 13:59:43 +02002566 child = orig->child;
Michal Vasko52927e22020-03-16 17:26:14 +01002567 for (struct lysc_node *key = ((struct lysc_node_list *)dup->schema)->child;
Michal Vaskoc57d9a92020-06-23 13:28:27 +02002568 key && (key->flags & LYS_KEY);
Radek Krejci0fe9b512019-07-26 17:51:05 +02002569 key = key->next) {
Radek Krejci22ebdba2019-07-25 13:59:43 +02002570 if (!child) {
2571 /* possibly not keys are present in filtered tree */
2572 break;
Radek Krejci0fe9b512019-07-26 17:51:05 +02002573 } else if (child->schema != key) {
2574 /* possibly not all keys are present in filtered tree,
2575 * but there can be also some non-key nodes */
2576 continue;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002577 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02002578 LY_CHECK_GOTO(ret = lyd_dup_r(child, dup, NULL, options, NULL), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002579 child = child->next;
2580 }
2581 }
2582 lyd_hash(dup);
2583 } else if (dup->schema->nodetype & LYD_NODE_ANY) {
Michal Vasko61551fa2020-07-09 15:45:45 +02002584 dup->hash = node->hash;
2585 any = (struct lyd_node_any *)node;
2586 LY_CHECK_GOTO(ret = lyd_any_copy_value(dup, &any->value, any->value_type), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002587 }
2588
Michal Vasko52927e22020-03-16 17:26:14 +01002589 /* insert */
2590 lyd_insert_node(parent, first, dup);
Michal Vasko52927e22020-03-16 17:26:14 +01002591
2592 if (dup_p) {
2593 *dup_p = dup;
2594 }
2595 return LY_SUCCESS;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002596
2597error:
Michal Vasko52927e22020-03-16 17:26:14 +01002598 lyd_free_tree(dup);
2599 return ret;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002600}
2601
Michal Vasko3a41dff2020-07-15 14:30:28 +02002602static LY_ERR
2603lyd_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 +02002604 struct lyd_node_inner **local_parent)
Radek Krejci22ebdba2019-07-25 13:59:43 +02002605{
Michal Vasko3a41dff2020-07-15 14:30:28 +02002606 const struct lyd_node_inner *orig_parent, *iter;
Radek Krejci857189e2020-09-01 13:26:36 +02002607 ly_bool repeat = 1;
Michal Vasko3a41dff2020-07-15 14:30:28 +02002608
2609 *dup_parent = NULL;
2610 *local_parent = NULL;
2611
2612 for (orig_parent = node->parent; repeat && orig_parent; orig_parent = orig_parent->parent) {
2613 if (parent && (parent->schema == orig_parent->schema)) {
2614 /* stop creating parents, connect what we have into the provided parent */
2615 iter = parent;
2616 repeat = 0;
2617 } else {
2618 iter = NULL;
2619 LY_CHECK_RET(lyd_dup_r((struct lyd_node *)orig_parent, NULL, (struct lyd_node **)&iter, 0,
Radek Krejci0f969882020-08-21 16:56:47 +02002620 (struct lyd_node **)&iter));
Michal Vasko3a41dff2020-07-15 14:30:28 +02002621 }
2622 if (!*local_parent) {
2623 *local_parent = (struct lyd_node_inner *)iter;
2624 }
2625 if (iter->child) {
2626 /* 1) list - add after keys
2627 * 2) provided parent with some children */
2628 iter->child->prev->next = *dup_parent;
2629 if (*dup_parent) {
2630 (*dup_parent)->prev = iter->child->prev;
2631 iter->child->prev = *dup_parent;
2632 }
2633 } else {
2634 ((struct lyd_node_inner *)iter)->child = *dup_parent;
2635 }
2636 if (*dup_parent) {
2637 (*dup_parent)->parent = (struct lyd_node_inner *)iter;
2638 }
2639 *dup_parent = (struct lyd_node *)iter;
2640 }
2641
2642 if (repeat && parent) {
2643 /* given parent and created parents chain actually do not interconnect */
Michal Vaskob7be7a82020-08-20 09:09:04 +02002644 LOGERR(LYD_CTX(node), LY_EINVAL,
Michal Vasko69730152020-10-09 16:30:07 +02002645 "Invalid argument parent (%s()) - does not interconnect with the created node's parents chain.", __func__);
Michal Vasko3a41dff2020-07-15 14:30:28 +02002646 return LY_EINVAL;
2647 }
2648
2649 return LY_SUCCESS;
2650}
2651
2652static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02002653lyd_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 +02002654{
2655 LY_ERR rc;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002656 const struct lyd_node *orig; /* original node to be duplicated */
2657 struct lyd_node *first = NULL; /* the first duplicated node, this is returned */
Radek Krejci22ebdba2019-07-25 13:59:43 +02002658 struct lyd_node *top = NULL; /* the most higher created node */
2659 struct lyd_node_inner *local_parent = NULL; /* the direct parent node for the duplicated node(s) */
Radek Krejci22ebdba2019-07-25 13:59:43 +02002660
Michal Vasko3a41dff2020-07-15 14:30:28 +02002661 LY_CHECK_ARG_RET(NULL, node, LY_EINVAL);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002662
2663 if (options & LYD_DUP_WITH_PARENTS) {
Michal Vasko3a41dff2020-07-15 14:30:28 +02002664 LY_CHECK_GOTO(rc = lyd_dup_get_local_parent(node, parent, &top, &local_parent), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002665 } else {
2666 local_parent = parent;
2667 }
2668
Radek Krejci22ebdba2019-07-25 13:59:43 +02002669 LY_LIST_FOR(node, orig) {
Michal Vasko52927e22020-03-16 17:26:14 +01002670 /* if there is no local parent, it will be inserted into first */
Michal Vasko3a41dff2020-07-15 14:30:28 +02002671 LY_CHECK_GOTO(rc = lyd_dup_r(orig, (struct lyd_node *)local_parent, &first, options, first ? NULL : &first), error);
2672 if (nosiblings) {
Radek Krejci22ebdba2019-07-25 13:59:43 +02002673 break;
2674 }
2675 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02002676
2677 /* rehash if needed */
Michal Vaskod989ba02020-08-24 10:59:24 +02002678 for ( ; local_parent; local_parent = local_parent->parent) {
Michal Vasko69730152020-10-09 16:30:07 +02002679 if ((local_parent->schema->nodetype == LYS_LIST) && (local_parent->schema->flags & LYS_KEYLESS)) {
Michal Vasko3a41dff2020-07-15 14:30:28 +02002680 lyd_hash((struct lyd_node *)local_parent);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002681 }
2682 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02002683
2684 if (dup) {
2685 *dup = first;
2686 }
2687 return LY_SUCCESS;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002688
2689error:
2690 if (top) {
2691 lyd_free_tree(top);
2692 } else {
Michal Vaskof03ed032020-03-04 13:31:44 +01002693 lyd_free_siblings(first);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002694 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02002695 return rc;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002696}
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002697
Michal Vasko3a41dff2020-07-15 14:30:28 +02002698API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02002699lyd_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 +02002700{
2701 return lyd_dup(node, parent, options, 1, dup);
2702}
2703
2704API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02002705lyd_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 +02002706{
2707 return lyd_dup(node, parent, options, 0, dup);
2708}
2709
2710API LY_ERR
2711lyd_dup_meta_single(const struct lyd_meta *meta, struct lyd_node *node, struct lyd_meta **dup)
Michal Vasko25a32822020-07-09 15:48:22 +02002712{
Radek Krejci011e4aa2020-09-04 15:22:31 +02002713 LY_ERR ret = LY_SUCCESS;
Michal Vasko25a32822020-07-09 15:48:22 +02002714 struct lyd_meta *mt, *last;
2715
Michal Vasko3a41dff2020-07-15 14:30:28 +02002716 LY_CHECK_ARG_RET(NULL, meta, node, LY_EINVAL);
Michal Vasko25a32822020-07-09 15:48:22 +02002717
2718 /* create a copy */
2719 mt = calloc(1, sizeof *mt);
Michal Vaskob7be7a82020-08-20 09:09:04 +02002720 LY_CHECK_ERR_RET(!mt, LOGMEM(LYD_CTX(node)), LY_EMEM);
Michal Vasko25a32822020-07-09 15:48:22 +02002721 mt->annotation = meta->annotation;
Michal Vaskob7be7a82020-08-20 09:09:04 +02002722 ret = meta->value.realtype->plugin->duplicate(LYD_CTX(node), &meta->value, &mt->value);
Radek Krejci011e4aa2020-09-04 15:22:31 +02002723 LY_CHECK_ERR_GOTO(ret, LOGERR(LYD_CTX(node), LY_EINT, "Value duplication failed."), finish);
2724 LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), meta->name, 0, &mt->name), finish);
Michal Vasko25a32822020-07-09 15:48:22 +02002725
2726 /* insert as the last attribute */
Radek Krejci011e4aa2020-09-04 15:22:31 +02002727 mt->parent = node;
Michal Vasko25a32822020-07-09 15:48:22 +02002728 if (node->meta) {
Radek Krejci1e008d22020-08-17 11:37:37 +02002729 for (last = node->meta; last->next; last = last->next) {}
Michal Vasko25a32822020-07-09 15:48:22 +02002730 last->next = mt;
2731 } else {
2732 node->meta = mt;
2733 }
2734
Radek Krejci011e4aa2020-09-04 15:22:31 +02002735finish:
2736 if (ret) {
2737 lyd_free_meta_single(mt);
2738 } else if (dup) {
Michal Vasko3a41dff2020-07-15 14:30:28 +02002739 *dup = mt;
2740 }
2741 return LY_SUCCESS;
Michal Vasko25a32822020-07-09 15:48:22 +02002742}
2743
Michal Vasko4490d312020-06-16 13:08:55 +02002744/**
2745 * @brief Merge a source sibling into target siblings.
2746 *
2747 * @param[in,out] first_trg First target sibling, is updated if top-level.
2748 * @param[in] parent_trg Target parent.
2749 * @param[in,out] sibling_src Source sibling to merge, set to NULL if spent.
2750 * @param[in] options Merge options.
2751 * @return LY_ERR value.
2752 */
2753static LY_ERR
2754lyd_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 +02002755 uint16_t options)
Michal Vasko4490d312020-06-16 13:08:55 +02002756{
2757 LY_ERR ret;
2758 const struct lyd_node *child_src, *tmp, *sibling_src;
Michal Vasko56daf732020-08-10 10:57:18 +02002759 struct lyd_node *match_trg, *dup_src, *elem;
Michal Vasko4490d312020-06-16 13:08:55 +02002760 struct lysc_type *type;
Michal Vasko4490d312020-06-16 13:08:55 +02002761
2762 sibling_src = *sibling_src_p;
2763 if (sibling_src->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) {
2764 /* try to find the exact instance */
2765 ret = lyd_find_sibling_first(*first_trg, sibling_src, &match_trg);
2766 } else {
2767 /* try to simply find the node, there cannot be more instances */
2768 ret = lyd_find_sibling_val(*first_trg, sibling_src->schema, NULL, 0, &match_trg);
2769 }
2770
2771 if (!ret) {
2772 /* node found, make sure even value matches for all node types */
Michal Vasko8f359bf2020-07-28 10:41:15 +02002773 if ((match_trg->schema->nodetype == LYS_LEAF) && lyd_compare_single(sibling_src, match_trg, LYD_COMPARE_DEFAULTS)) {
Michal Vasko4490d312020-06-16 13:08:55 +02002774 /* since they are different, they cannot both be default */
2775 assert(!(sibling_src->flags & LYD_DEFAULT) || !(match_trg->flags & LYD_DEFAULT));
2776
Michal Vasko3a41dff2020-07-15 14:30:28 +02002777 /* update value (or only LYD_DEFAULT flag) only if flag set or the source node is not default */
2778 if ((options & LYD_MERGE_DEFAULTS) || !(sibling_src->flags & LYD_DEFAULT)) {
Michal Vasko4490d312020-06-16 13:08:55 +02002779 type = ((struct lysc_node_leaf *)match_trg->schema)->type;
Michal Vaskob7be7a82020-08-20 09:09:04 +02002780 type->plugin->free(LYD_CTX(match_trg), &((struct lyd_node_term *)match_trg)->value);
2781 LY_CHECK_RET(type->plugin->duplicate(LYD_CTX(match_trg), &((struct lyd_node_term *)sibling_src)->value,
Michal Vasko69730152020-10-09 16:30:07 +02002782 &((struct lyd_node_term *)match_trg)->value));
Michal Vasko4490d312020-06-16 13:08:55 +02002783
2784 /* copy flags and add LYD_NEW */
2785 match_trg->flags = sibling_src->flags | LYD_NEW;
2786 }
Michal Vasko8f359bf2020-07-28 10:41:15 +02002787 } else if ((match_trg->schema->nodetype & LYS_ANYDATA) && lyd_compare_single(sibling_src, match_trg, 0)) {
Michal Vasko4c583e82020-07-17 12:16:14 +02002788 /* update value */
2789 LY_CHECK_RET(lyd_any_copy_value(match_trg, &((struct lyd_node_any *)sibling_src)->value,
Radek Krejci0f969882020-08-21 16:56:47 +02002790 ((struct lyd_node_any *)sibling_src)->value_type));
Michal Vasko4490d312020-06-16 13:08:55 +02002791
2792 /* copy flags and add LYD_NEW */
2793 match_trg->flags = sibling_src->flags | LYD_NEW;
Michal Vasko4490d312020-06-16 13:08:55 +02002794 } else {
2795 /* check descendants, recursively */
Radek Krejcia1c1e542020-09-29 16:06:52 +02002796 LY_LIST_FOR_SAFE(lyd_child_no_keys(sibling_src), tmp, child_src) {
Michal Vasko4490d312020-06-16 13:08:55 +02002797 LY_CHECK_RET(lyd_merge_sibling_r(lyd_node_children_p(match_trg), match_trg, &child_src, options));
2798 }
2799 }
2800 } else {
2801 /* node not found, merge it */
2802 if (options & LYD_MERGE_DESTRUCT) {
2803 dup_src = (struct lyd_node *)sibling_src;
2804 lyd_unlink_tree(dup_src);
2805 /* spend it */
2806 *sibling_src_p = NULL;
2807 } else {
Michal Vasko3a41dff2020-07-15 14:30:28 +02002808 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 +02002809 }
2810
2811 /* set LYD_NEW for all the new nodes, required for validation */
Michal Vasko56daf732020-08-10 10:57:18 +02002812 LYD_TREE_DFS_BEGIN(dup_src, elem) {
Michal Vasko4490d312020-06-16 13:08:55 +02002813 elem->flags |= LYD_NEW;
Michal Vasko56daf732020-08-10 10:57:18 +02002814 LYD_TREE_DFS_END(dup_src, elem);
Michal Vasko4490d312020-06-16 13:08:55 +02002815 }
2816
2817 lyd_insert_node(parent_trg, first_trg, dup_src);
2818 }
2819
2820 return LY_SUCCESS;
2821}
2822
Michal Vasko3a41dff2020-07-15 14:30:28 +02002823static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02002824lyd_merge(struct lyd_node **target, const struct lyd_node *source, uint16_t options, ly_bool nosiblings)
Michal Vasko4490d312020-06-16 13:08:55 +02002825{
2826 const struct lyd_node *sibling_src, *tmp;
Radek Krejci857189e2020-09-01 13:26:36 +02002827 ly_bool first;
Michal Vasko4490d312020-06-16 13:08:55 +02002828
2829 LY_CHECK_ARG_RET(NULL, target, LY_EINVAL);
2830
2831 if (!source) {
2832 /* nothing to merge */
2833 return LY_SUCCESS;
2834 }
2835
2836 if (lysc_data_parent((*target)->schema) || lysc_data_parent(source->schema)) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02002837 LOGERR(LYD_CTX(source), LY_EINVAL, "Invalid arguments - can merge only 2 top-level subtrees (%s()).", __func__);
Michal Vasko4490d312020-06-16 13:08:55 +02002838 return LY_EINVAL;
2839 }
2840
2841 LY_LIST_FOR_SAFE(source, tmp, sibling_src) {
Radek Krejci857189e2020-09-01 13:26:36 +02002842 first = (sibling_src == source) ? 1 : 0;
Michal Vasko4490d312020-06-16 13:08:55 +02002843 LY_CHECK_RET(lyd_merge_sibling_r(target, NULL, &sibling_src, options));
2844 if (first && !sibling_src) {
2845 /* source was spent (unlinked), move to the next node */
2846 source = tmp;
2847 }
2848
Michal Vasko3a41dff2020-07-15 14:30:28 +02002849 if (nosiblings) {
Michal Vasko4490d312020-06-16 13:08:55 +02002850 break;
2851 }
2852 }
2853
2854 if (options & LYD_MERGE_DESTRUCT) {
2855 /* free any leftover source data that were not merged */
2856 lyd_free_siblings((struct lyd_node *)source);
2857 }
2858
2859 return LY_SUCCESS;
2860}
2861
Michal Vasko3a41dff2020-07-15 14:30:28 +02002862API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02002863lyd_merge_tree(struct lyd_node **target, const struct lyd_node *source, uint16_t options)
Michal Vasko3a41dff2020-07-15 14:30:28 +02002864{
2865 return lyd_merge(target, source, options, 1);
2866}
2867
2868API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02002869lyd_merge_siblings(struct lyd_node **target, const struct lyd_node *source, uint16_t options)
Michal Vasko3a41dff2020-07-15 14:30:28 +02002870{
2871 return lyd_merge(target, source, options, 0);
2872}
2873
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002874static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02002875lyd_path_str_enlarge(char **buffer, size_t *buflen, size_t reqlen, ly_bool is_static)
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002876{
Michal Vasko14654712020-02-06 08:35:21 +01002877 /* ending \0 */
2878 ++reqlen;
2879
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002880 if (reqlen > *buflen) {
2881 if (is_static) {
2882 return LY_EINCOMPLETE;
2883 }
2884
2885 *buffer = ly_realloc(*buffer, reqlen * sizeof **buffer);
2886 if (!*buffer) {
2887 return LY_EMEM;
2888 }
2889
2890 *buflen = reqlen;
2891 }
2892
2893 return LY_SUCCESS;
2894}
2895
Michal Vaskod59035b2020-07-08 12:00:06 +02002896LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02002897lyd_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 +02002898{
2899 const struct lyd_node *key;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002900 size_t len;
2901 const char *val;
2902 char quot;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002903
Radek Krejcia1c1e542020-09-29 16:06:52 +02002904 for (key = lyd_child(node); key && (key->schema->flags & LYS_KEY); key = key->next) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02002905 val = LYD_CANON_VALUE(key);
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002906 len = 1 + strlen(key->schema->name) + 2 + strlen(val) + 2;
Michal Vaskoba99a3e2020-08-18 15:50:05 +02002907 LY_CHECK_RET(lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static));
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002908
2909 quot = '\'';
2910 if (strchr(val, '\'')) {
2911 quot = '"';
2912 }
2913 *bufused += sprintf(*buffer + *bufused, "[%s=%c%s%c]", key->schema->name, quot, val, quot);
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002914 }
2915
2916 return LY_SUCCESS;
2917}
2918
2919/**
2920 * @brief Append leaf-list value predicate to path.
2921 *
2922 * @param[in] node Node to print.
2923 * @param[in,out] buffer Buffer to print to.
2924 * @param[in,out] buflen Current buffer length.
2925 * @param[in,out] bufused Current number of characters used in @p buffer.
2926 * @param[in] is_static Whether buffer is static or can be reallocated.
2927 * @return LY_ERR
2928 */
2929static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02002930lyd_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 +02002931{
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002932 size_t len;
2933 const char *val;
2934 char quot;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002935
Michal Vaskob7be7a82020-08-20 09:09:04 +02002936 val = LYD_CANON_VALUE(node);
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002937 len = 4 + strlen(val) + 2;
Michal Vaskoba99a3e2020-08-18 15:50:05 +02002938 LY_CHECK_RET(lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static));
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002939
2940 quot = '\'';
2941 if (strchr(val, '\'')) {
2942 quot = '"';
2943 }
2944 *bufused += sprintf(*buffer + *bufused, "[.=%c%s%c]", quot, val, quot);
2945
Michal Vaskoba99a3e2020-08-18 15:50:05 +02002946 return LY_SUCCESS;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002947}
2948
2949/**
2950 * @brief Append node position (relative to its other instances) predicate to path.
2951 *
2952 * @param[in] node Node to print.
2953 * @param[in,out] buffer Buffer to print to.
2954 * @param[in,out] buflen Current buffer length.
2955 * @param[in,out] bufused Current number of characters used in @p buffer.
2956 * @param[in] is_static Whether buffer is static or can be reallocated.
2957 * @return LY_ERR
2958 */
2959static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02002960lyd_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 +02002961{
2962 const struct lyd_node *first, *iter;
2963 size_t len;
Radek Krejci1deb5be2020-08-26 16:43:36 +02002964 uint64_t pos;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002965 char *val = NULL;
2966 LY_ERR rc;
2967
2968 if (node->parent) {
2969 first = node->parent->child;
2970 } else {
Michal Vaskoe070bfe2020-08-31 12:27:25 +02002971 for (first = node; first->prev->next; first = first->prev) {}
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002972 }
2973 pos = 1;
2974 for (iter = first; iter != node; iter = iter->next) {
2975 if (iter->schema == node->schema) {
2976 ++pos;
2977 }
2978 }
Radek Krejci1deb5be2020-08-26 16:43:36 +02002979 if (asprintf(&val, "%" PRIu64, pos) == -1) {
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002980 return LY_EMEM;
2981 }
2982
2983 len = 1 + strlen(val) + 1;
2984 rc = lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static);
2985 if (rc != LY_SUCCESS) {
2986 goto cleanup;
2987 }
2988
2989 *bufused += sprintf(*buffer + *bufused, "[%s]", val);
2990
2991cleanup:
2992 free(val);
2993 return rc;
2994}
2995
2996API char *
2997lyd_path(const struct lyd_node *node, LYD_PATH_TYPE pathtype, char *buffer, size_t buflen)
2998{
Radek Krejci857189e2020-09-01 13:26:36 +02002999 ly_bool is_static = 0;
Radek Krejci1deb5be2020-08-26 16:43:36 +02003000 uint32_t i, depth;
Michal Vasko14654712020-02-06 08:35:21 +01003001 size_t bufused = 0, len;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003002 const struct lyd_node *iter;
3003 const struct lys_module *mod;
Michal Vasko790b2bc2020-08-03 13:35:06 +02003004 LY_ERR rc = LY_SUCCESS;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003005
3006 LY_CHECK_ARG_RET(NULL, node, NULL);
3007 if (buffer) {
3008 LY_CHECK_ARG_RET(node->schema->module->ctx, buflen > 1, NULL);
3009 is_static = 1;
Michal Vasko14654712020-02-06 08:35:21 +01003010 } else {
3011 buflen = 0;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003012 }
3013
3014 switch (pathtype) {
Michal Vasko14654712020-02-06 08:35:21 +01003015 case LYD_PATH_LOG:
Michal Vasko790b2bc2020-08-03 13:35:06 +02003016 case LYD_PATH_LOG_NO_LAST_PRED:
Michal Vasko14654712020-02-06 08:35:21 +01003017 depth = 1;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003018 for (iter = node; iter->parent; iter = (const struct lyd_node *)iter->parent) {
3019 ++depth;
3020 }
3021
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003022 goto iter_print;
Michal Vasko14654712020-02-06 08:35:21 +01003023 while (depth) {
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003024 /* find the right node */
Radek Krejci1e008d22020-08-17 11:37:37 +02003025 for (iter = node, i = 1; i < depth; iter = (const struct lyd_node *)iter->parent, ++i) {}
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003026iter_print:
3027 /* print prefix and name */
3028 mod = NULL;
Michal Vasko69730152020-10-09 16:30:07 +02003029 if (iter->schema && (!iter->parent || (iter->schema->module != iter->parent->schema->module))) {
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003030 mod = iter->schema->module;
3031 }
3032
3033 /* realloc string */
Michal Vasko22df3f02020-08-24 13:29:22 +02003034 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 +02003035 rc = lyd_path_str_enlarge(&buffer, &buflen, bufused + len, is_static);
3036 if (rc != LY_SUCCESS) {
3037 break;
3038 }
3039
3040 /* print next node */
Radek Krejci1798aae2020-07-14 13:26:06 +02003041 bufused += sprintf(buffer + bufused, "/%s%s%s", mod ? mod->name : "", mod ? ":" : "",
Michal Vasko69730152020-10-09 16:30:07 +02003042 iter->schema ? iter->schema->name : ((struct lyd_node_opaq *)iter)->name);
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003043
Michal Vasko790b2bc2020-08-03 13:35:06 +02003044 /* do not always print the last (first) predicate */
Radek Krejci1798aae2020-07-14 13:26:06 +02003045 if (iter->schema && (bufused || (pathtype == LYD_PATH_LOG))) {
Michal Vasko790b2bc2020-08-03 13:35:06 +02003046 switch (iter->schema->nodetype) {
3047 case LYS_LIST:
3048 if (iter->schema->flags & LYS_KEYLESS) {
3049 /* print its position */
3050 rc = lyd_path_position_predicate(iter, &buffer, &buflen, &bufused, is_static);
3051 } else {
3052 /* print all list keys in predicates */
3053 rc = lyd_path_list_predicate(iter, &buffer, &buflen, &bufused, is_static);
3054 }
3055 break;
3056 case LYS_LEAFLIST:
3057 if (iter->schema->flags & LYS_CONFIG_W) {
3058 /* print leaf-list value */
3059 rc = lyd_path_leaflist_predicate(iter, &buffer, &buflen, &bufused, is_static);
3060 } else {
3061 /* print its position */
3062 rc = lyd_path_position_predicate(iter, &buffer, &buflen, &bufused, is_static);
3063 }
3064 break;
3065 default:
3066 /* nothing to print more */
3067 break;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003068 }
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003069 }
3070 if (rc != LY_SUCCESS) {
3071 break;
3072 }
3073
Michal Vasko14654712020-02-06 08:35:21 +01003074 --depth;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003075 }
3076 break;
3077 }
3078
3079 return buffer;
3080}
Michal Vaskoe444f752020-02-10 12:20:06 +01003081
Michal Vasko25a32822020-07-09 15:48:22 +02003082API struct lyd_meta *
3083lyd_find_meta(const struct lyd_meta *first, const struct lys_module *module, const char *name)
3084{
3085 struct lyd_meta *ret = NULL;
3086 const struct ly_ctx *ctx;
3087 const char *prefix, *tmp;
3088 char *str;
3089 size_t pref_len, name_len;
3090
3091 LY_CHECK_ARG_RET(NULL, module || strchr(name, ':'), name, NULL);
3092
3093 if (!first) {
3094 return NULL;
3095 }
3096
3097 ctx = first->annotation->module->ctx;
3098
3099 /* parse the name */
3100 tmp = name;
3101 if (ly_parse_nodeid(&tmp, &prefix, &pref_len, &name, &name_len) || tmp[0]) {
3102 LOGERR(ctx, LY_EINVAL, "Metadata name \"%s\" is not valid.", name);
3103 return NULL;
3104 }
3105
3106 /* find the module */
3107 if (prefix) {
3108 str = strndup(prefix, pref_len);
3109 module = ly_ctx_get_module_latest(ctx, str);
3110 free(str);
3111 LY_CHECK_ERR_RET(!module, LOGERR(ctx, LY_EINVAL, "Module \"%.*s\" not found.", pref_len, prefix), NULL);
3112 }
3113
3114 /* find the metadata */
3115 LY_LIST_FOR(first, first) {
3116 if ((first->annotation->module == module) && !strcmp(first->name, name)) {
3117 ret = (struct lyd_meta *)first;
3118 break;
3119 }
3120 }
3121
3122 return ret;
3123}
3124
Michal Vasko9b368d32020-02-14 13:53:31 +01003125API LY_ERR
Michal Vaskoe444f752020-02-10 12:20:06 +01003126lyd_find_sibling_first(const struct lyd_node *siblings, const struct lyd_node *target, struct lyd_node **match)
3127{
3128 struct lyd_node **match_p;
3129 struct lyd_node_inner *parent;
3130
Michal Vaskof03ed032020-03-04 13:31:44 +01003131 LY_CHECK_ARG_RET(NULL, target, LY_EINVAL);
Michal Vaskoe444f752020-02-10 12:20:06 +01003132
Michal Vasko62ed12d2020-05-21 10:08:25 +02003133 if (!siblings || (lysc_data_parent(siblings->schema) != lysc_data_parent(target->schema))) {
3134 /* no data or schema mismatch */
Michal Vasko9b368d32020-02-14 13:53:31 +01003135 if (match) {
3136 *match = NULL;
3137 }
Michal Vaskoe444f752020-02-10 12:20:06 +01003138 return LY_ENOTFOUND;
3139 }
3140
3141 /* find first sibling */
3142 if (siblings->parent) {
3143 siblings = siblings->parent->child;
3144 } else {
3145 while (siblings->prev->next) {
3146 siblings = siblings->prev;
3147 }
3148 }
3149
3150 parent = (struct lyd_node_inner *)siblings->parent;
3151 if (parent && parent->children_ht) {
3152 assert(target->hash);
3153
3154 /* find by hash */
3155 if (!lyht_find(parent->children_ht, &target, target->hash, (void **)&match_p)) {
Michal Vaskoda859032020-07-14 12:20:14 +02003156 /* check even value when needed */
Michal Vasko8f359bf2020-07-28 10:41:15 +02003157 if (!(target->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) || !lyd_compare_single(target, *match_p, 0)) {
Michal Vaskoda859032020-07-14 12:20:14 +02003158 siblings = *match_p;
3159 } else {
3160 siblings = NULL;
3161 }
Michal Vaskoe444f752020-02-10 12:20:06 +01003162 } else {
3163 /* not found */
3164 siblings = NULL;
3165 }
3166 } else {
3167 /* no children hash table */
Michal Vaskod989ba02020-08-24 10:59:24 +02003168 for ( ; siblings; siblings = siblings->next) {
Michal Vasko8f359bf2020-07-28 10:41:15 +02003169 if (!lyd_compare_single(siblings, target, 0)) {
Michal Vaskoe444f752020-02-10 12:20:06 +01003170 break;
3171 }
3172 }
3173 }
3174
3175 if (!siblings) {
Michal Vasko9b368d32020-02-14 13:53:31 +01003176 if (match) {
3177 *match = NULL;
3178 }
Michal Vaskoe444f752020-02-10 12:20:06 +01003179 return LY_ENOTFOUND;
3180 }
3181
Michal Vasko9b368d32020-02-14 13:53:31 +01003182 if (match) {
3183 *match = (struct lyd_node *)siblings;
3184 }
Michal Vaskoe444f752020-02-10 12:20:06 +01003185 return LY_SUCCESS;
3186}
3187
Radek Krejci857189e2020-09-01 13:26:36 +02003188/**
3189 * @brief Comparison callback to match schema node with a schema of a data node.
3190 *
3191 * @param[in] val1_p Pointer to the schema node
3192 * @param[in] val2_p Pointer to the data node
3193 * Implementation of ::values_equal_cb.
3194 */
3195static ly_bool
3196lyd_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 +01003197{
3198 struct lysc_node *val1;
3199 struct lyd_node *val2;
3200
3201 val1 = *((struct lysc_node **)val1_p);
3202 val2 = *((struct lyd_node **)val2_p);
3203
Michal Vasko90932a92020-02-12 14:33:03 +01003204 if (val1 == val2->schema) {
3205 /* schema match is enough */
3206 return 1;
3207 } else {
3208 return 0;
3209 }
3210}
3211
3212static LY_ERR
3213lyd_find_sibling_schema(const struct lyd_node *siblings, const struct lysc_node *schema, struct lyd_node **match)
3214{
3215 struct lyd_node **match_p;
3216 struct lyd_node_inner *parent;
3217 uint32_t hash;
3218 values_equal_cb ht_cb;
3219
Michal Vaskob104f112020-07-17 09:54:54 +02003220 assert(siblings && schema);
Michal Vasko90932a92020-02-12 14:33:03 +01003221
3222 parent = (struct lyd_node_inner *)siblings->parent;
3223 if (parent && parent->children_ht) {
3224 /* calculate our hash */
3225 hash = dict_hash_multi(0, schema->module->name, strlen(schema->module->name));
3226 hash = dict_hash_multi(hash, schema->name, strlen(schema->name));
3227 hash = dict_hash_multi(hash, NULL, 0);
3228
3229 /* use special hash table function */
3230 ht_cb = lyht_set_cb(parent->children_ht, lyd_hash_table_schema_val_equal);
3231
3232 /* find by hash */
3233 if (!lyht_find(parent->children_ht, &schema, hash, (void **)&match_p)) {
3234 siblings = *match_p;
3235 } else {
3236 /* not found */
3237 siblings = NULL;
3238 }
3239
3240 /* set the original hash table compare function back */
3241 lyht_set_cb(parent->children_ht, ht_cb);
3242 } else {
Michal Vaskob104f112020-07-17 09:54:54 +02003243 /* find first sibling */
3244 if (siblings->parent) {
3245 siblings = siblings->parent->child;
3246 } else {
3247 while (siblings->prev->next) {
3248 siblings = siblings->prev;
3249 }
3250 }
3251
3252 /* search manually without hashes */
Michal Vaskod989ba02020-08-24 10:59:24 +02003253 for ( ; siblings; siblings = siblings->next) {
Michal Vasko90932a92020-02-12 14:33:03 +01003254 if (siblings->schema == schema) {
3255 /* schema match is enough */
3256 break;
3257 }
3258 }
3259 }
3260
3261 if (!siblings) {
Michal Vasko9b368d32020-02-14 13:53:31 +01003262 if (match) {
3263 *match = NULL;
3264 }
Michal Vasko90932a92020-02-12 14:33:03 +01003265 return LY_ENOTFOUND;
3266 }
3267
Michal Vasko9b368d32020-02-14 13:53:31 +01003268 if (match) {
3269 *match = (struct lyd_node *)siblings;
3270 }
Michal Vasko90932a92020-02-12 14:33:03 +01003271 return LY_SUCCESS;
3272}
3273
Michal Vaskoe444f752020-02-10 12:20:06 +01003274API LY_ERR
3275lyd_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 +02003276 size_t val_len, struct lyd_node **match)
Michal Vaskoe444f752020-02-10 12:20:06 +01003277{
3278 LY_ERR rc;
3279 struct lyd_node *target = NULL;
3280
Michal Vasko4c583e82020-07-17 12:16:14 +02003281 LY_CHECK_ARG_RET(NULL, schema, !(schema->nodetype & (LYS_CHOICE | LYS_CASE)), LY_EINVAL);
Michal Vaskoe444f752020-02-10 12:20:06 +01003282
Michal Vasko62ed12d2020-05-21 10:08:25 +02003283 if (!siblings || (lysc_data_parent(siblings->schema) != lysc_data_parent(schema))) {
3284 /* no data or schema mismatch */
Michal Vasko9b368d32020-02-14 13:53:31 +01003285 if (match) {
3286 *match = NULL;
3287 }
Michal Vaskoe444f752020-02-10 12:20:06 +01003288 return LY_ENOTFOUND;
3289 }
3290
Michal Vaskof03ed032020-03-04 13:31:44 +01003291 if (key_or_value && !val_len) {
3292 val_len = strlen(key_or_value);
3293 }
3294
Michal Vaskob104f112020-07-17 09:54:54 +02003295 if ((schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) && key_or_value) {
3296 /* create a data node and find the instance */
3297 if (schema->nodetype == LYS_LEAFLIST) {
3298 /* target used attributes: schema, hash, value */
Michal Vaskofeca4fb2020-10-05 08:58:40 +02003299 rc = lyd_create_term(schema, key_or_value, val_len, NULL, LY_PREF_JSON, NULL, LYD_HINT_DATA, NULL, &target);
3300 LY_CHECK_RET(rc);
Michal Vaskob104f112020-07-17 09:54:54 +02003301 } else {
Michal Vasko90932a92020-02-12 14:33:03 +01003302 /* target used attributes: schema, hash, child (all keys) */
Michal Vasko004d3152020-06-11 19:59:22 +02003303 LY_CHECK_RET(lyd_create_list2(schema, key_or_value, val_len, &target));
Michal Vasko90932a92020-02-12 14:33:03 +01003304 }
3305
3306 /* find it */
3307 rc = lyd_find_sibling_first(siblings, target, match);
Michal Vaskob104f112020-07-17 09:54:54 +02003308 } else {
3309 /* find the first schema node instance */
3310 rc = lyd_find_sibling_schema(siblings, schema, match);
Michal Vaskoe444f752020-02-10 12:20:06 +01003311 }
3312
Michal Vaskoe444f752020-02-10 12:20:06 +01003313 lyd_free_tree(target);
3314 return rc;
3315}
Michal Vaskoccc02342020-05-21 10:09:21 +02003316
3317API LY_ERR
3318lyd_find_xpath(const struct lyd_node *ctx_node, const char *xpath, struct ly_set **set)
3319{
3320 LY_ERR ret = LY_SUCCESS;
3321 struct lyxp_set xp_set;
Radek Krejcif03a9e22020-09-18 20:09:31 +02003322 struct lyxp_expr *exp = NULL;
Michal Vaskoccc02342020-05-21 10:09:21 +02003323 uint32_t i;
3324
3325 LY_CHECK_ARG_RET(NULL, ctx_node, xpath, set, LY_EINVAL);
3326
3327 memset(&xp_set, 0, sizeof xp_set);
3328
3329 /* compile expression */
Radek Krejcif03a9e22020-09-18 20:09:31 +02003330 ret = lyxp_expr_parse((struct ly_ctx *)LYD_CTX(ctx_node), xpath, 0, 1, &exp);
3331 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskoccc02342020-05-21 10:09:21 +02003332
3333 /* evaluate expression */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003334 ret = lyxp_eval(exp, NULL, LY_PREF_JSON, NULL, ctx_node, ctx_node, &xp_set, 0);
Michal Vaskoccc02342020-05-21 10:09:21 +02003335 LY_CHECK_GOTO(ret, cleanup);
3336
3337 /* allocate return set */
Radek Krejciba03a5a2020-08-27 14:40:41 +02003338 ret = ly_set_new(set);
3339 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskoccc02342020-05-21 10:09:21 +02003340
3341 /* transform into ly_set */
3342 if (xp_set.type == LYXP_SET_NODE_SET) {
3343 /* allocate memory for all the elements once (even though not all items must be elements but most likely will be) */
3344 (*set)->objs = malloc(xp_set.used * sizeof *(*set)->objs);
Michal Vaskob7be7a82020-08-20 09:09:04 +02003345 LY_CHECK_ERR_GOTO(!(*set)->objs, LOGMEM(LYD_CTX(ctx_node)); ret = LY_EMEM, cleanup);
Michal Vaskoccc02342020-05-21 10:09:21 +02003346 (*set)->size = xp_set.used;
3347
3348 for (i = 0; i < xp_set.used; ++i) {
3349 if (xp_set.val.nodes[i].type == LYXP_NODE_ELEM) {
Radek Krejci3d92e442020-10-12 12:48:13 +02003350 ret = ly_set_add(*set, xp_set.val.nodes[i].node, 1, NULL);
Radek Krejciba03a5a2020-08-27 14:40:41 +02003351 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskoccc02342020-05-21 10:09:21 +02003352 }
3353 }
3354 }
3355
3356cleanup:
Michal Vasko0691d522020-05-21 13:21:47 +02003357 lyxp_set_free_content(&xp_set);
Michal Vaskob7be7a82020-08-20 09:09:04 +02003358 lyxp_expr_free((struct ly_ctx *)LYD_CTX(ctx_node), exp);
Michal Vaskoccc02342020-05-21 10:09:21 +02003359 return ret;
3360}