blob: 0ab3537ee35b01100558634422d1c4bf5866d945 [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 "config.h"
33#include "context.h"
34#include "dict.h"
Michal Vasko90932a92020-02-12 14:33:03 +010035#include "hash_table.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020036#include "log.h"
Radek Krejci7931b192020-06-25 17:05:03 +020037#include "parser_data.h"
38#include "parser_internal.h"
Michal Vasko004d3152020-06-11 19:59:22 +020039#include "path.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020040#include "plugins_exts.h"
Radek Krejci38d85362019-09-05 16:26:38 +020041#include "plugins_exts_metadata.h"
Michal Vasko90932a92020-02-12 14:33:03 +010042#include "plugins_exts_internal.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020043#include "plugins_types.h"
44#include "set.h"
45#include "tree.h"
46#include "tree_data_internal.h"
47#include "tree_schema.h"
48#include "tree_schema_internal.h"
49#include "xml.h"
50#include "xpath.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020051
Michal Vaskob104f112020-07-17 09:54:54 +020052static LY_ERR lyd_find_sibling_schema(const struct lyd_node *siblings, const struct lysc_node *schema,
53 struct lyd_node **match);
54
Radek Krejci084289f2019-07-09 17:35:30 +020055LY_ERR
Michal Vasko90932a92020-02-12 14:33:03 +010056lyd_value_parse(struct lyd_node_term *node, const char *value, size_t value_len, int *dynamic, int second,
Michal Vaskof03ed032020-03-04 13:31:44 +010057 ly_clb_resolve_prefix get_prefix, void *parser, LYD_FORMAT format, const struct lyd_node *tree)
Radek Krejci084289f2019-07-09 17:35:30 +020058{
Michal Vasko90932a92020-02-12 14:33:03 +010059 LY_ERR ret = LY_SUCCESS;
Radek Krejci084289f2019-07-09 17:35:30 +020060 struct ly_err_item *err = NULL;
61 struct ly_ctx *ctx;
62 struct lysc_type *type;
Radek Krejci3c9758d2019-07-11 16:49:10 +020063 int options = LY_TYPE_OPTS_STORE | (second ? LY_TYPE_OPTS_SECOND_CALL : 0) |
Michal Vaskof03ed032020-03-04 13:31:44 +010064 (dynamic && *dynamic ? LY_TYPE_OPTS_DYNAMIC : 0) | (tree ? 0 : LY_TYPE_OPTS_INCOMPLETE_DATA);
Radek Krejci084289f2019-07-09 17:35:30 +020065 assert(node);
66
67 ctx = node->schema->module->ctx;
Radek Krejci084289f2019-07-09 17:35:30 +020068
Radek Krejci73dead22019-07-11 16:46:16 +020069 type = ((struct lysc_node_leaf*)node->schema)->type;
Radek Krejci62903c32019-07-15 14:42:05 +020070 if (!second) {
71 node->value.realtype = type;
72 }
Michal Vasko90932a92020-02-12 14:33:03 +010073 ret = type->plugin->store(ctx, type, value, value_len, options, get_prefix, parser, format,
Michal Vasko004d3152020-06-11 19:59:22 +020074 tree ? (void *)node : (void *)node->schema, tree, &node->value, NULL, &err);
Michal Vasko90932a92020-02-12 14:33:03 +010075 if (ret && (ret != LY_EINCOMPLETE)) {
Radek Krejci73dead22019-07-11 16:46:16 +020076 if (err) {
Michal Vasko3544d1e2020-05-27 11:17:51 +020077 /* node may not be connected yet so use the schema node */
Michal Vaskof872e202020-05-27 11:49:06 +020078 if (!node->parent && lysc_data_parent(node->schema)) {
79 LOGVAL(ctx, LY_VLOG_LYSC, node->schema, err->vecode, err->msg);
80 } else {
81 LOGVAL(ctx, LY_VLOG_LYD, node, err->vecode, err->msg);
82 }
Radek Krejci73dead22019-07-11 16:46:16 +020083 ly_err_free(err);
Radek Krejci084289f2019-07-09 17:35:30 +020084 }
Radek Krejci73dead22019-07-11 16:46:16 +020085 goto error;
Michal Vasko90932a92020-02-12 14:33:03 +010086 } else if (dynamic) {
87 *dynamic = 0;
Radek Krejci084289f2019-07-09 17:35:30 +020088 }
89
90error:
91 return ret;
92}
93
Michal Vasko00cbf532020-06-15 13:58:47 +020094/* similar to lyd_value_parse except can be used just to store the value, hence also does not support a second call */
Michal Vasko004d3152020-06-11 19:59:22 +020095LY_ERR
Michal Vasko90932a92020-02-12 14:33:03 +010096lyd_value_store(struct lyd_value *val, const struct lysc_node *schema, const char *value, size_t value_len, int *dynamic,
97 ly_clb_resolve_prefix get_prefix, void *parser, LYD_FORMAT format)
98{
99 LY_ERR ret = LY_SUCCESS;
100 struct ly_err_item *err = NULL;
101 struct ly_ctx *ctx;
102 struct lysc_type *type;
103 int options = LY_TYPE_OPTS_STORE | LY_TYPE_OPTS_INCOMPLETE_DATA | (dynamic && *dynamic ? LY_TYPE_OPTS_DYNAMIC : 0);
104
105 assert(val && schema && (schema->nodetype & LYD_NODE_TERM));
106
107 ctx = schema->module->ctx;
108 type = ((struct lysc_node_leaf *)schema)->type;
109 val->realtype = type;
110 ret = type->plugin->store(ctx, type, value, value_len, options, get_prefix, parser, format, (void *)schema, NULL,
111 val, NULL, &err);
112 if (ret == LY_EINCOMPLETE) {
113 /* this is fine, we do not need it resolved */
114 ret = LY_SUCCESS;
115 } else if (ret && err) {
116 ly_err_print(err);
117 LOGVAL(ctx, LY_VLOG_STR, err->path, err->vecode, err->msg);
118 ly_err_free(err);
119 }
120 if (!ret && dynamic) {
121 *dynamic = 0;
122 }
123
124 return ret;
125}
126
Radek Krejci38d85362019-09-05 16:26:38 +0200127LY_ERR
Michal Vasko41586352020-07-13 13:54:25 +0200128lyd_value_parse_meta(const struct ly_ctx *ctx, struct lyd_meta *meta, const char *value, size_t value_len, int *dynamic,
Michal Vasko8d544252020-03-02 10:19:52 +0100129 int second, ly_clb_resolve_prefix get_prefix, void *parser, LYD_FORMAT format,
Michal Vaskof03ed032020-03-04 13:31:44 +0100130 const struct lysc_node *ctx_snode, const struct lyd_node *tree)
Radek Krejci38d85362019-09-05 16:26:38 +0200131{
Michal Vasko90932a92020-02-12 14:33:03 +0100132 LY_ERR ret = LY_SUCCESS;
Radek Krejci38d85362019-09-05 16:26:38 +0200133 struct ly_err_item *err = NULL;
Radek Krejci38d85362019-09-05 16:26:38 +0200134 struct lyext_metadata *ant;
135 int options = LY_TYPE_OPTS_STORE | (second ? LY_TYPE_OPTS_SECOND_CALL : 0) |
Michal Vaskof03ed032020-03-04 13:31:44 +0100136 (dynamic && *dynamic ? LY_TYPE_OPTS_DYNAMIC : 0) | (tree ? 0 : LY_TYPE_OPTS_INCOMPLETE_DATA);
Radek Krejci38d85362019-09-05 16:26:38 +0200137
Michal Vasko9f96a052020-03-10 09:41:45 +0100138 assert(ctx && meta && ((tree && meta->parent) || ctx_snode));
Michal Vasko8d544252020-03-02 10:19:52 +0100139
Michal Vasko9f96a052020-03-10 09:41:45 +0100140 ant = meta->annotation->data;
Radek Krejci38d85362019-09-05 16:26:38 +0200141
142 if (!second) {
Michal Vasko9f96a052020-03-10 09:41:45 +0100143 meta->value.realtype = ant->type;
Radek Krejci38d85362019-09-05 16:26:38 +0200144 }
Michal Vasko90932a92020-02-12 14:33:03 +0100145 ret = ant->type->plugin->store(ctx, ant->type, value, value_len, options, get_prefix, parser, format,
Michal Vasko9f96a052020-03-10 09:41:45 +0100146 tree ? (void *)meta->parent : (void *)ctx_snode, tree, &meta->value, NULL, &err);
Michal Vasko90932a92020-02-12 14:33:03 +0100147 if (ret && (ret != LY_EINCOMPLETE)) {
Radek Krejci38d85362019-09-05 16:26:38 +0200148 if (err) {
149 ly_err_print(err);
150 LOGVAL(ctx, LY_VLOG_STR, err->path, err->vecode, err->msg);
151 ly_err_free(err);
152 }
153 goto error;
Michal Vasko90932a92020-02-12 14:33:03 +0100154 } else if (dynamic) {
155 *dynamic = 0;
Radek Krejci38d85362019-09-05 16:26:38 +0200156 }
157
158error:
159 return ret;
160}
161
Radek Krejci084289f2019-07-09 17:35:30 +0200162API LY_ERR
Michal Vasko44685da2020-03-17 15:38:06 +0100163lys_value_validate(const struct ly_ctx *ctx, const struct lysc_node *node, const char *value, size_t value_len,
Radek Krejci084289f2019-07-09 17:35:30 +0200164 ly_clb_resolve_prefix get_prefix, void *get_prefix_data, LYD_FORMAT format)
165{
166 LY_ERR rc = LY_SUCCESS;
167 struct ly_err_item *err = NULL;
168 struct lysc_type *type;
Radek Krejci084289f2019-07-09 17:35:30 +0200169
170 LY_CHECK_ARG_RET(ctx, node, value, LY_EINVAL);
171
172 if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
173 LOGARG(ctx, node);
174 return LY_EINVAL;
175 }
176
177 type = ((struct lysc_node_leaf*)node)->type;
Radek Krejci73dead22019-07-11 16:46:16 +0200178 /* just validate, no storing of enything */
179 rc = type->plugin->store(ctx ? ctx : node->module->ctx, type, value, value_len, LY_TYPE_OPTS_INCOMPLETE_DATA,
180 get_prefix, get_prefix_data, format, node, NULL, NULL, NULL, &err);
181 if (rc == LY_EINCOMPLETE) {
182 /* actually success since we do not provide the context tree and call validation with
183 * LY_TYPE_OPTS_INCOMPLETE_DATA */
184 rc = LY_SUCCESS;
185 } else if (rc && err) {
186 if (ctx) {
187 /* log only in case the ctx was provided as input parameter */
188 ly_err_print(err);
189 LOGVAL(ctx, LY_VLOG_STR, err->path, err->vecode, err->msg);
Radek Krejci084289f2019-07-09 17:35:30 +0200190 }
Radek Krejci73dead22019-07-11 16:46:16 +0200191 ly_err_free(err);
Radek Krejci084289f2019-07-09 17:35:30 +0200192 }
193
194 return rc;
195}
196
197API LY_ERR
Michal Vasko44685da2020-03-17 15:38:06 +0100198lyd_value_validate(const struct ly_ctx *ctx, const struct lyd_node_term *node, const char *value, size_t value_len,
Michal Vaskof03ed032020-03-04 13:31:44 +0100199 ly_clb_resolve_prefix get_prefix, void *get_prefix_data, LYD_FORMAT format, const struct lyd_node *tree)
Radek Krejci084289f2019-07-09 17:35:30 +0200200{
201 LY_ERR rc;
202 struct ly_err_item *err = NULL;
203 struct lysc_type *type;
Michal Vaskof03ed032020-03-04 13:31:44 +0100204 int options = (tree ? 0 : LY_TYPE_OPTS_INCOMPLETE_DATA);
Radek Krejci084289f2019-07-09 17:35:30 +0200205
206 LY_CHECK_ARG_RET(ctx, node, value, LY_EINVAL);
207
208 type = ((struct lysc_node_leaf*)node->schema)->type;
Radek Krejci73dead22019-07-11 16:46:16 +0200209 rc = type->plugin->store(ctx ? ctx : node->schema->module->ctx, type, value, value_len, options,
Michal Vaskof03ed032020-03-04 13:31:44 +0100210 get_prefix, get_prefix_data, format, tree ? (void*)node : (void*)node->schema, tree,
Radek Krejci73dead22019-07-11 16:46:16 +0200211 NULL, NULL, &err);
212 if (rc == LY_EINCOMPLETE) {
213 return rc;
214 } else if (rc) {
215 if (err) {
216 if (ctx) {
217 ly_err_print(err);
218 LOGVAL(ctx, LY_VLOG_STR, err->path, err->vecode, err->msg);
Radek Krejci084289f2019-07-09 17:35:30 +0200219 }
Radek Krejci73dead22019-07-11 16:46:16 +0200220 ly_err_free(err);
Radek Krejci084289f2019-07-09 17:35:30 +0200221 }
Radek Krejci73dead22019-07-11 16:46:16 +0200222 return rc;
Radek Krejci084289f2019-07-09 17:35:30 +0200223 }
224
225 return LY_SUCCESS;
226}
227
228API LY_ERR
229lyd_value_compare(const struct lyd_node_term *node, const char *value, size_t value_len,
Michal Vaskof03ed032020-03-04 13:31:44 +0100230 ly_clb_resolve_prefix get_prefix, void *get_prefix_data, LYD_FORMAT format, const struct lyd_node *tree)
Radek Krejci084289f2019-07-09 17:35:30 +0200231{
232 LY_ERR ret = LY_SUCCESS, rc;
233 struct ly_err_item *err = NULL;
234 struct ly_ctx *ctx;
235 struct lysc_type *type;
Radek Krejci084289f2019-07-09 17:35:30 +0200236 struct lyd_value data = {0};
Michal Vaskof03ed032020-03-04 13:31:44 +0100237 int options = LY_TYPE_OPTS_STORE | (tree ? 0 : LY_TYPE_OPTS_INCOMPLETE_DATA);
Radek Krejci084289f2019-07-09 17:35:30 +0200238
239 LY_CHECK_ARG_RET(node ? node->schema->module->ctx : NULL, node, value, LY_EINVAL);
240
241 ctx = node->schema->module->ctx;
242 type = ((struct lysc_node_leaf*)node->schema)->type;
Radek Krejci73dead22019-07-11 16:46:16 +0200243 rc = type->plugin->store(ctx, type, value, value_len, options, get_prefix, get_prefix_data, format, (struct lyd_node*)node,
Michal Vaskof03ed032020-03-04 13:31:44 +0100244 tree, &data, NULL, &err);
Radek Krejci73dead22019-07-11 16:46:16 +0200245 if (rc == LY_EINCOMPLETE) {
246 ret = rc;
247 /* continue with comparing, just remember what to return if storing is ok */
248 } else if (rc) {
249 /* value to compare is invalid */
250 ret = LY_EINVAL;
251 if (err) {
252 ly_err_free(err);
Radek Krejci084289f2019-07-09 17:35:30 +0200253 }
Radek Krejci73dead22019-07-11 16:46:16 +0200254 goto cleanup;
Radek Krejci084289f2019-07-09 17:35:30 +0200255 }
256
257 /* compare data */
Radek Krejci5af04842019-07-12 11:32:07 +0200258 if (type->plugin->compare(&node->value, &data)) {
259 /* do not assign it directly from the compare callback to keep possible LY_EINCOMPLETE from validation */
Michal Vaskob3ddccb2020-07-09 15:43:05 +0200260 ret = LY_ENOT;
Radek Krejci5af04842019-07-12 11:32:07 +0200261 }
Radek Krejci084289f2019-07-09 17:35:30 +0200262
263cleanup:
Radek Krejci62903c32019-07-15 14:42:05 +0200264 type->plugin->free(ctx, &data);
Radek Krejci084289f2019-07-09 17:35:30 +0200265
266 return ret;
267}
268
Michal Vasko5ec7cda2019-09-11 13:43:08 +0200269API const char *
270lyd_value2str(const struct lyd_node_term *node, int *dynamic)
271{
272 LY_CHECK_ARG_RET(node ? node->schema->module->ctx : NULL, node, dynamic, NULL);
273
274 return node->value.realtype->plugin->print(&node->value, LYD_JSON, json_print_get_prefix, NULL, dynamic);
275}
276
277API const char *
Michal Vasko9f96a052020-03-10 09:41:45 +0100278lyd_meta2str(const struct lyd_meta *meta, int *dynamic)
Michal Vasko5ec7cda2019-09-11 13:43:08 +0200279{
Michal Vasko9f96a052020-03-10 09:41:45 +0100280 LY_CHECK_ARG_RET(meta ? meta->parent->schema->module->ctx : NULL, meta, dynamic, NULL);
Michal Vasko5ec7cda2019-09-11 13:43:08 +0200281
Michal Vasko9f96a052020-03-10 09:41:45 +0100282 return meta->value.realtype->plugin->print(&meta->value, LYD_JSON, json_print_get_prefix, NULL, dynamic);
Michal Vasko5ec7cda2019-09-11 13:43:08 +0200283}
284
Radek Krejci7931b192020-06-25 17:05:03 +0200285static LYD_FORMAT
Michal Vasko63f3d842020-07-08 10:10:14 +0200286lyd_parse_get_format(const struct ly_in *in, LYD_FORMAT format)
Radek Krejcie7b95092019-05-15 11:03:07 +0200287{
Radek Krejcie7b95092019-05-15 11:03:07 +0200288
Radek Krejci7931b192020-06-25 17:05:03 +0200289 if (!format && in->type == LY_IN_FILEPATH) {
Radek Krejcie7b95092019-05-15 11:03:07 +0200290 /* unknown format - try to detect it from filename's suffix */
Radek Krejci7931b192020-06-25 17:05:03 +0200291 const char *path = in->method.fpath.filepath;
292 size_t len = strlen(path);
Radek Krejcie7b95092019-05-15 11:03:07 +0200293
294 /* ignore trailing whitespaces */
295 for (; len > 0 && isspace(path[len - 1]); len--);
296
297 if (len >= 5 && !strncmp(&path[len - 4], ".xml", 4)) {
298 format = LYD_XML;
299#if 0
300 } else if (len >= 6 && !strncmp(&path[len - 5], ".json", 5)) {
301 format = LYD_JSON;
Radek Krejci7931b192020-06-25 17:05:03 +0200302#endif
Radek Krejcie7b95092019-05-15 11:03:07 +0200303 } else if (len >= 5 && !strncmp(&path[len - 4], ".lyb", 4)) {
304 format = LYD_LYB;
Radek Krejci7931b192020-06-25 17:05:03 +0200305 } /* else still unknown */
Radek Krejcie7b95092019-05-15 11:03:07 +0200306 }
307
Radek Krejci7931b192020-06-25 17:05:03 +0200308 return format;
309}
Radek Krejcie7b95092019-05-15 11:03:07 +0200310
Radek Krejci7931b192020-06-25 17:05:03 +0200311API LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +0200312lyd_parse_data(const struct ly_ctx *ctx, struct ly_in *in, LYD_FORMAT format, int parse_options, int validate_options,
313 struct lyd_node **tree)
Radek Krejci7931b192020-06-25 17:05:03 +0200314{
315 LY_CHECK_ARG_RET(ctx, ctx, in, tree, LY_EINVAL);
316 LY_CHECK_ARG_RET(ctx, !(parse_options & ~LYD_PARSE_OPTS_MASK), LY_EINVAL);
317 LY_CHECK_ARG_RET(ctx, !(validate_options & ~LYD_VALIDATE_OPTS_MASK), LY_EINVAL);
318
319 format = lyd_parse_get_format(in, format);
320 LY_CHECK_ARG_RET(ctx, format, LY_EINVAL);
321
Michal Vasko63f3d842020-07-08 10:10:14 +0200322 /* remember input position */
323 in->func_start = in->current;
Radek Krejci7931b192020-06-25 17:05:03 +0200324
325 switch (format) {
326 case LYD_XML:
Michal Vasko63f3d842020-07-08 10:10:14 +0200327 return lyd_parse_xml_data(ctx, in, parse_options, validate_options, tree);
Radek Krejci7931b192020-06-25 17:05:03 +0200328#if 0
329 case LYD_JSON:
Michal Vasko63f3d842020-07-08 10:10:14 +0200330 return lyd_parse_json_data(ctx, in, parse_options, validate_options, tree);
Radek Krejci7931b192020-06-25 17:05:03 +0200331#endif
332 case LYD_LYB:
Michal Vasko63f3d842020-07-08 10:10:14 +0200333 return lyd_parse_lyb_data(ctx, in, parse_options, validate_options, tree);
Radek Krejci7931b192020-06-25 17:05:03 +0200334 case LYD_SCHEMA:
335 LOGINT_RET(ctx);
336 }
337
338 /* TODO move here the top-level validation from parser_xml.c's lyd_parse_xml_data() and make
339 * it common for all the lyd_parse_*_data() functions */
340
341 LOGINT_RET(ctx);
342}
343
344API LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +0200345lyd_parse_data_mem(const struct ly_ctx *ctx, const char *data, LYD_FORMAT format, int parse_options, int validate_options,
346 struct lyd_node **tree)
Radek Krejci7931b192020-06-25 17:05:03 +0200347{
348 LY_ERR ret;
349 struct ly_in *in;
350
351 LY_CHECK_RET(ly_in_new_memory(data, &in));
352 ret = lyd_parse_data(ctx, in, format, parse_options, validate_options, tree);
353
354 ly_in_free(in, 0);
355 return ret;
356}
357
358API LY_ERR
Michal Vaskob104f112020-07-17 09:54:54 +0200359lyd_parse_data_fd(const struct ly_ctx *ctx, int fd, LYD_FORMAT format, int parse_options, int validate_options,
360 struct lyd_node **tree)
Radek Krejci7931b192020-06-25 17:05:03 +0200361{
362 LY_ERR ret;
363 struct ly_in *in;
364
365 LY_CHECK_RET(ly_in_new_fd(fd, &in));
366 ret = lyd_parse_data(ctx, in, format, parse_options, validate_options, tree);
367
368 ly_in_free(in, 0);
369 return ret;
370}
371
372API LY_ERR
Michal Vaskob104f112020-07-17 09:54:54 +0200373lyd_parse_data_path(const struct ly_ctx *ctx, const char *path, LYD_FORMAT format, int parse_options,
374 int validate_options, struct lyd_node **tree)
Radek Krejci7931b192020-06-25 17:05:03 +0200375{
376 LY_ERR ret;
377 struct ly_in *in;
378
379 LY_CHECK_RET(ly_in_new_filepath(path, 0, &in));
380 ret = lyd_parse_data(ctx, in, format, parse_options, validate_options, tree);
381
382 ly_in_free(in, 0);
383 return ret;
384}
385
Radek Krejci7931b192020-06-25 17:05:03 +0200386API LY_ERR
387lyd_parse_rpc(const struct ly_ctx *ctx, struct ly_in *in, LYD_FORMAT format, struct lyd_node **tree, struct lyd_node **op)
388{
389 LY_CHECK_ARG_RET(ctx, ctx, in, tree, LY_EINVAL);
390
391 format = lyd_parse_get_format(in, format);
392 LY_CHECK_ARG_RET(ctx, format, LY_EINVAL);
393
Michal Vasko63f3d842020-07-08 10:10:14 +0200394 /* remember input position */
395 in->func_start = in->current;
396
Radek Krejci7931b192020-06-25 17:05:03 +0200397 switch (format) {
398 case LYD_XML:
Michal Vasko63f3d842020-07-08 10:10:14 +0200399 return lyd_parse_xml_rpc(ctx, in, tree, op);
Radek Krejci7931b192020-06-25 17:05:03 +0200400#if 0
401 case LYD_JSON:
Michal Vasko63f3d842020-07-08 10:10:14 +0200402 return lyd_parse_json_rpc(ctx, in, tree, op);
Radek Krejci7931b192020-06-25 17:05:03 +0200403#endif
404 case LYD_LYB:
Michal Vasko63f3d842020-07-08 10:10:14 +0200405 return lyd_parse_lyb_rpc(ctx, in, tree, op);
Radek Krejci7931b192020-06-25 17:05:03 +0200406 case LYD_SCHEMA:
407 LOGINT_RET(ctx);
408 }
409
410 LOGINT_RET(ctx);
411}
412
413API LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +0200414lyd_parse_reply(const struct lyd_node *request, struct ly_in *in, LYD_FORMAT format, struct lyd_node **tree,
415 struct lyd_node **op)
Radek Krejci7931b192020-06-25 17:05:03 +0200416{
417 LY_CHECK_ARG_RET(NULL, request, LY_EINVAL);
418 LY_CHECK_ARG_RET(LYD_NODE_CTX(request), in, tree, LY_EINVAL);
419
420 format = lyd_parse_get_format(in, format);
421 LY_CHECK_ARG_RET(LYD_NODE_CTX(request), format, LY_EINVAL);
422
Michal Vasko63f3d842020-07-08 10:10:14 +0200423 /* remember input position */
424 in->func_start = in->current;
425
Radek Krejci7931b192020-06-25 17:05:03 +0200426 switch (format) {
427 case LYD_XML:
Michal Vasko63f3d842020-07-08 10:10:14 +0200428 return lyd_parse_xml_reply(request, in, tree, op);
Radek Krejci7931b192020-06-25 17:05:03 +0200429#if 0
430 case LYD_JSON:
Michal Vasko63f3d842020-07-08 10:10:14 +0200431 return lyd_parse_json_reply(request, in, tree, op);
Radek Krejci7931b192020-06-25 17:05:03 +0200432#endif
433 case LYD_LYB:
Michal Vasko63f3d842020-07-08 10:10:14 +0200434 return lyd_parse_lyb_reply(request, in, tree, op);
Radek Krejci7931b192020-06-25 17:05:03 +0200435 case LYD_SCHEMA:
436 LOGINT_RET(LYD_NODE_CTX(request));
437 }
438
439 LOGINT_RET(LYD_NODE_CTX(request));
440}
441
442API LY_ERR
443lyd_parse_notif(const struct ly_ctx *ctx, struct ly_in *in, LYD_FORMAT format, struct lyd_node **tree, struct lyd_node **ntf)
444{
445 LY_CHECK_ARG_RET(ctx, ctx, in, tree, LY_EINVAL);
446
447 format = lyd_parse_get_format(in, format);
448 LY_CHECK_ARG_RET(ctx, format, LY_EINVAL);
449
Michal Vasko63f3d842020-07-08 10:10:14 +0200450 /* remember input position */
451 in->func_start = in->current;
452
Radek Krejci7931b192020-06-25 17:05:03 +0200453 switch (format) {
454 case LYD_XML:
Michal Vasko63f3d842020-07-08 10:10:14 +0200455 return lyd_parse_xml_notif(ctx, in, tree, ntf);
Radek Krejci7931b192020-06-25 17:05:03 +0200456#if 0
457 case LYD_JSON:
Michal Vasko63f3d842020-07-08 10:10:14 +0200458 return lyd_parse_json_notif(ctx, in, tree, ntf);
Radek Krejci7931b192020-06-25 17:05:03 +0200459#endif
460 case LYD_LYB:
Michal Vasko63f3d842020-07-08 10:10:14 +0200461 return lyd_parse_lyb_notif(ctx, in, tree, ntf);
Radek Krejci7931b192020-06-25 17:05:03 +0200462 case LYD_SCHEMA:
463 LOGINT_RET(ctx);
464 }
465
466 LOGINT_RET(ctx);
Radek Krejcie7b95092019-05-15 11:03:07 +0200467}
Radek Krejci084289f2019-07-09 17:35:30 +0200468
Michal Vasko90932a92020-02-12 14:33:03 +0100469LY_ERR
470lyd_create_term(const struct lysc_node *schema, const char *value, size_t value_len, int *dynamic,
471 ly_clb_resolve_prefix get_prefix, void *prefix_data, LYD_FORMAT format, struct lyd_node **node)
472{
473 LY_ERR ret;
474 struct lyd_node_term *term;
475
Michal Vasko9b368d32020-02-14 13:53:31 +0100476 assert(schema->nodetype & LYD_NODE_TERM);
477
Michal Vasko90932a92020-02-12 14:33:03 +0100478 term = calloc(1, sizeof *term);
479 LY_CHECK_ERR_RET(!term, LOGMEM(schema->module->ctx), LY_EMEM);
480
481 term->schema = schema;
482 term->prev = (struct lyd_node *)term;
Michal Vasko9b368d32020-02-14 13:53:31 +0100483 term->flags = LYD_NEW;
Michal Vasko90932a92020-02-12 14:33:03 +0100484
485 ret = lyd_value_parse(term, value, value_len, dynamic, 0, get_prefix, prefix_data, format, NULL);
486 if (ret && (ret != LY_EINCOMPLETE)) {
487 free(term);
488 return ret;
489 }
Michal Vasko9b368d32020-02-14 13:53:31 +0100490 lyd_hash((struct lyd_node *)term);
491
492 *node = (struct lyd_node *)term;
493 return ret;
494}
495
496LY_ERR
497lyd_create_term2(const struct lysc_node *schema, const struct lyd_value *val, struct lyd_node **node)
498{
499 LY_ERR ret;
500 struct lyd_node_term *term;
501 struct lysc_type *type;
502
503 assert(schema->nodetype & LYD_NODE_TERM);
Michal Vasko00cbf532020-06-15 13:58:47 +0200504 assert(val && val->realtype);
Michal Vasko9b368d32020-02-14 13:53:31 +0100505
506 term = calloc(1, sizeof *term);
507 LY_CHECK_ERR_RET(!term, LOGMEM(schema->module->ctx), LY_EMEM);
508
509 term->schema = schema;
510 term->prev = (struct lyd_node *)term;
511 term->flags = LYD_NEW;
512
513 type = ((struct lysc_node_leaf *)schema)->type;
514 ret = type->plugin->duplicate(schema->module->ctx, val, &term->value);
515 if (ret) {
516 LOGERR(schema->module->ctx, ret, "Value duplication failed.");
517 free(term);
518 return ret;
519 }
Michal Vasko00cbf532020-06-15 13:58:47 +0200520 term->value.realtype = val->realtype;
Michal Vasko9b368d32020-02-14 13:53:31 +0100521 lyd_hash((struct lyd_node *)term);
Michal Vasko90932a92020-02-12 14:33:03 +0100522
523 *node = (struct lyd_node *)term;
524 return ret;
525}
526
527LY_ERR
528lyd_create_inner(const struct lysc_node *schema, struct lyd_node **node)
529{
530 struct lyd_node_inner *in;
531
Michal Vasko9b368d32020-02-14 13:53:31 +0100532 assert(schema->nodetype & LYD_NODE_INNER);
533
Michal Vasko90932a92020-02-12 14:33:03 +0100534 in = calloc(1, sizeof *in);
535 LY_CHECK_ERR_RET(!in, LOGMEM(schema->module->ctx), LY_EMEM);
536
537 in->schema = schema;
538 in->prev = (struct lyd_node *)in;
Michal Vasko9b368d32020-02-14 13:53:31 +0100539 in->flags = LYD_NEW;
Michal Vasko90932a92020-02-12 14:33:03 +0100540
Michal Vasko9b368d32020-02-14 13:53:31 +0100541 /* do not hash list with keys, we need them for the hash */
542 if ((schema->nodetype != LYS_LIST) || (schema->flags & LYS_KEYLESS)) {
543 lyd_hash((struct lyd_node *)in);
544 }
Michal Vasko90932a92020-02-12 14:33:03 +0100545
546 *node = (struct lyd_node *)in;
547 return LY_SUCCESS;
548}
549
Michal Vasko90932a92020-02-12 14:33:03 +0100550LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +0200551lyd_create_list(const struct lysc_node *schema, const struct ly_path_predicate *predicates, struct lyd_node **node)
Michal Vasko90932a92020-02-12 14:33:03 +0100552{
553 LY_ERR ret = LY_SUCCESS;
Michal Vasko90932a92020-02-12 14:33:03 +0100554 struct lyd_node *list = NULL, *key;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200555 LY_ARRAY_COUNT_TYPE u;
Michal Vasko90932a92020-02-12 14:33:03 +0100556
Michal Vasko004d3152020-06-11 19:59:22 +0200557 assert((schema->nodetype == LYS_LIST) && !(schema->flags & LYS_KEYLESS));
Michal Vasko90932a92020-02-12 14:33:03 +0100558
559 /* create list */
560 LY_CHECK_GOTO(ret = lyd_create_inner(schema, &list), cleanup);
561
Michal Vasko90932a92020-02-12 14:33:03 +0100562 /* create and insert all the keys */
Michal Vasko004d3152020-06-11 19:59:22 +0200563 LY_ARRAY_FOR(predicates, u) {
564 LY_CHECK_GOTO(ret = lyd_create_term2(predicates[u].key, &predicates[u].value, &key), cleanup);
Michal Vasko90932a92020-02-12 14:33:03 +0100565 lyd_insert_node(list, NULL, key);
566 }
567
Michal Vasko9b368d32020-02-14 13:53:31 +0100568 /* hash having all the keys */
569 lyd_hash(list);
570
Michal Vasko90932a92020-02-12 14:33:03 +0100571 /* success */
572 *node = list;
573 list = NULL;
574
575cleanup:
576 lyd_free_tree(list);
Michal Vasko004d3152020-06-11 19:59:22 +0200577 return ret;
578}
579
580static LY_ERR
581lyd_create_list2(const struct lysc_node *schema, const char *keys, size_t keys_len, struct lyd_node **node)
582{
583 LY_ERR ret = LY_SUCCESS;
584 struct lyxp_expr *expr = NULL;
585 uint16_t exp_idx = 0;
586 enum ly_path_pred_type pred_type = 0;
587 struct ly_path_predicate *predicates = NULL;
588
589 /* parse keys */
Michal Vasko6b26e742020-07-17 15:02:10 +0200590 LY_CHECK_GOTO(ret = ly_path_parse_predicate(schema->module->ctx, NULL, keys, keys_len, LY_PATH_PREFIX_OPTIONAL,
Michal Vasko004d3152020-06-11 19:59:22 +0200591 LY_PATH_PRED_KEYS, &expr), cleanup);
592
593 /* compile them */
Michal Vasko6b26e742020-07-17 15:02:10 +0200594 LY_CHECK_GOTO(ret = ly_path_compile_predicate(schema->module->ctx, NULL, NULL, schema, expr, &exp_idx,
595 lydjson_resolve_prefix, NULL, LYD_JSON, &predicates, &pred_type), cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +0200596
597 /* create the list node */
598 LY_CHECK_GOTO(ret = lyd_create_list(schema, predicates, node), cleanup);
599
600cleanup:
601 lyxp_expr_free(schema->module->ctx, expr);
602 ly_path_predicates_free(schema->module->ctx, pred_type, NULL, predicates);
Michal Vasko90932a92020-02-12 14:33:03 +0100603 return ret;
604}
605
606LY_ERR
607lyd_create_any(const struct lysc_node *schema, const void *value, LYD_ANYDATA_VALUETYPE value_type, struct lyd_node **node)
608{
609 struct lyd_node_any *any;
610
Michal Vasko9b368d32020-02-14 13:53:31 +0100611 assert(schema->nodetype & LYD_NODE_ANY);
612
Michal Vasko90932a92020-02-12 14:33:03 +0100613 any = calloc(1, sizeof *any);
614 LY_CHECK_ERR_RET(!any, LOGMEM(schema->module->ctx), LY_EMEM);
615
616 any->schema = schema;
617 any->prev = (struct lyd_node *)any;
Michal Vasko9b368d32020-02-14 13:53:31 +0100618 any->flags = LYD_NEW;
Michal Vasko90932a92020-02-12 14:33:03 +0100619
620 any->value.xml = value;
621 any->value_type = value_type;
Michal Vasko9b368d32020-02-14 13:53:31 +0100622 lyd_hash((struct lyd_node *)any);
Michal Vasko90932a92020-02-12 14:33:03 +0100623
624 *node = (struct lyd_node *)any;
625 return LY_SUCCESS;
626}
627
Michal Vasko52927e22020-03-16 17:26:14 +0100628LY_ERR
629lyd_create_opaq(const struct ly_ctx *ctx, const char *name, size_t name_len, const char *value, size_t value_len,
630 int *dynamic, LYD_FORMAT format, struct ly_prefix *val_prefs, const char *prefix, size_t pref_len,
631 const char *ns, struct lyd_node **node)
632{
633 struct lyd_node_opaq *opaq;
634
635 assert(ctx && name && name_len && ns);
636
637 if (!value_len) {
638 value = "";
639 }
640
641 opaq = calloc(1, sizeof *opaq);
642 LY_CHECK_ERR_RET(!opaq, LOGMEM(ctx), LY_EMEM);
643
644 opaq->prev = (struct lyd_node *)opaq;
645
646 opaq->name = lydict_insert(ctx, name, name_len);
647 opaq->format = format;
648 if (pref_len) {
649 opaq->prefix.pref = lydict_insert(ctx, prefix, pref_len);
650 }
651 opaq->prefix.ns = lydict_insert(ctx, ns, 0);
652 opaq->val_prefs = val_prefs;
653 if (dynamic && *dynamic) {
654 opaq->value = lydict_insert_zc(ctx, (char *)value);
655 *dynamic = 0;
656 } else {
657 opaq->value = lydict_insert(ctx, value, value_len);
658 }
659 opaq->ctx = ctx;
660
661 *node = (struct lyd_node *)opaq;
662 return LY_SUCCESS;
663}
664
Michal Vasko3a41dff2020-07-15 14:30:28 +0200665API LY_ERR
666lyd_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 +0100667{
668 struct lyd_node *ret = NULL;
669 const struct lysc_node *schema;
670 struct ly_ctx *ctx = parent ? parent->schema->module->ctx : (module ? module->ctx : NULL);
671
Michal Vasko6027eb92020-07-15 16:37:30 +0200672 LY_CHECK_ARG_RET(ctx, parent || module, parent || node, name, LY_EINVAL);
Michal Vasko013a8182020-03-03 10:46:53 +0100673
Michal Vaskof03ed032020-03-04 13:31:44 +0100674 if (!module) {
675 module = parent->schema->module;
676 }
677
Michal Vasko3a41dff2020-07-15 14:30:28 +0200678 schema = lys_find_child(parent ? parent->schema : NULL, module, name, 0,
679 LYS_CONTAINER | LYS_NOTIF | LYS_RPC | LYS_ACTION, 0);
680 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 +0100681
Michal Vasko3a41dff2020-07-15 14:30:28 +0200682 LY_CHECK_RET(lyd_create_inner(schema, &ret));
683 if (parent) {
Michal Vasko013a8182020-03-03 10:46:53 +0100684 lyd_insert_node(parent, NULL, ret);
685 }
Michal Vasko3a41dff2020-07-15 14:30:28 +0200686
687 if (node) {
688 *node = ret;
689 }
690 return LY_SUCCESS;
Michal Vasko013a8182020-03-03 10:46:53 +0100691}
692
Michal Vasko3a41dff2020-07-15 14:30:28 +0200693API LY_ERR
694lyd_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 +0100695{
696 struct lyd_node *ret = NULL, *key;
697 const struct lysc_node *schema, *key_s;
698 struct ly_ctx *ctx = parent ? parent->schema->module->ctx : (module ? module->ctx : NULL);
699 va_list ap;
700 const char *key_val;
701 LY_ERR rc = LY_SUCCESS;
702
Michal Vasko6027eb92020-07-15 16:37:30 +0200703 LY_CHECK_ARG_RET(ctx, parent || module, parent || node, name, LY_EINVAL);
Michal Vasko013a8182020-03-03 10:46:53 +0100704
Michal Vaskof03ed032020-03-04 13:31:44 +0100705 if (!module) {
706 module = parent->schema->module;
707 }
708
Michal Vasko013a8182020-03-03 10:46:53 +0100709 schema = lys_find_child(parent ? parent->schema : NULL, module, name, 0, LYS_LIST, 0);
Michal Vasko3a41dff2020-07-15 14:30:28 +0200710 LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "List node \"%s\" not found.", name), LY_ENOTFOUND);
Michal Vasko013a8182020-03-03 10:46:53 +0100711
712 /* create list inner node */
Michal Vasko3a41dff2020-07-15 14:30:28 +0200713 LY_CHECK_RET(lyd_create_inner(schema, &ret));
Michal Vasko013a8182020-03-03 10:46:53 +0100714
Michal Vasko3a41dff2020-07-15 14:30:28 +0200715 va_start(ap, node);
Michal Vasko013a8182020-03-03 10:46:53 +0100716
717 /* create and insert all the keys */
718 for (key_s = lysc_node_children(schema, 0); key_s && (key_s->flags & LYS_KEY); key_s = key_s->next) {
719 key_val = va_arg(ap, const char *);
720
Michal Vaskof03ed032020-03-04 13:31:44 +0100721 rc = lyd_create_term(key_s, key_val, key_val ? strlen(key_val) : 0, NULL, lydjson_resolve_prefix, NULL, LYD_JSON, &key);
Michal Vaskocbff3e92020-05-27 12:56:41 +0200722 LY_CHECK_GOTO(rc && (rc != LY_EINCOMPLETE), cleanup);
723 rc = LY_SUCCESS;
Michal Vasko013a8182020-03-03 10:46:53 +0100724 lyd_insert_node(ret, NULL, key);
725 }
726
Michal Vasko013a8182020-03-03 10:46:53 +0100727 if (parent) {
728 lyd_insert_node(parent, NULL, ret);
729 }
730
731cleanup:
Michal Vasko3a41dff2020-07-15 14:30:28 +0200732 va_end(ap);
Michal Vasko013a8182020-03-03 10:46:53 +0100733 if (rc) {
734 lyd_free_tree(ret);
735 ret = NULL;
Michal Vasko3a41dff2020-07-15 14:30:28 +0200736 } else if (node) {
737 *node = ret;
Michal Vasko013a8182020-03-03 10:46:53 +0100738 }
Michal Vasko3a41dff2020-07-15 14:30:28 +0200739 return rc;
Michal Vasko013a8182020-03-03 10:46:53 +0100740}
741
Michal Vasko3a41dff2020-07-15 14:30:28 +0200742API LY_ERR
743lyd_new_list2(struct lyd_node *parent, const struct lys_module *module, const char *name, const char *keys,
744 struct lyd_node **node)
Michal Vasko013a8182020-03-03 10:46:53 +0100745{
746 struct lyd_node *ret = NULL;
747 const struct lysc_node *schema;
748 struct ly_ctx *ctx = parent ? parent->schema->module->ctx : (module ? module->ctx : NULL);
749
Michal Vasko6027eb92020-07-15 16:37:30 +0200750 LY_CHECK_ARG_RET(ctx, parent || module, parent || node, name, LY_EINVAL);
Michal Vasko013a8182020-03-03 10:46:53 +0100751
Michal Vaskof03ed032020-03-04 13:31:44 +0100752 if (!module) {
753 module = parent->schema->module;
754 }
Michal Vasko004d3152020-06-11 19:59:22 +0200755 if (!keys) {
756 keys = "";
757 }
Michal Vaskof03ed032020-03-04 13:31:44 +0100758
Michal Vasko004d3152020-06-11 19:59:22 +0200759 /* find schema node */
Michal Vasko013a8182020-03-03 10:46:53 +0100760 schema = lys_find_child(parent ? parent->schema : NULL, module, name, 0, LYS_LIST, 0);
Michal Vasko3a41dff2020-07-15 14:30:28 +0200761 LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "List node \"%s\" not found.", name), LY_ENOTFOUND);
Michal Vasko013a8182020-03-03 10:46:53 +0100762
Michal Vasko004d3152020-06-11 19:59:22 +0200763 if ((schema->flags & LYS_KEYLESS) && !keys[0]) {
764 /* key-less list */
Michal Vasko3a41dff2020-07-15 14:30:28 +0200765 LY_CHECK_RET(lyd_create_inner(schema, &ret));
Michal Vasko004d3152020-06-11 19:59:22 +0200766 } else {
767 /* create the list node */
Michal Vasko3a41dff2020-07-15 14:30:28 +0200768 LY_CHECK_RET(lyd_create_list2(schema, keys, strlen(keys), &ret));
Michal Vasko004d3152020-06-11 19:59:22 +0200769 }
Michal Vasko004d3152020-06-11 19:59:22 +0200770 if (parent) {
Michal Vasko013a8182020-03-03 10:46:53 +0100771 lyd_insert_node(parent, NULL, ret);
772 }
Michal Vasko3a41dff2020-07-15 14:30:28 +0200773
774 if (node) {
775 *node = ret;
776 }
777 return LY_SUCCESS;
Michal Vasko013a8182020-03-03 10:46:53 +0100778}
779
Michal Vasko3a41dff2020-07-15 14:30:28 +0200780API LY_ERR
781lyd_new_term(struct lyd_node *parent, const struct lys_module *module, const char *name, const char *val_str,
782 struct lyd_node **node)
Michal Vasko013a8182020-03-03 10:46:53 +0100783{
Michal Vaskocbff3e92020-05-27 12:56:41 +0200784 LY_ERR rc;
Michal Vasko013a8182020-03-03 10:46:53 +0100785 struct lyd_node *ret = NULL;
786 const struct lysc_node *schema;
787 struct ly_ctx *ctx = parent ? parent->schema->module->ctx : (module ? module->ctx : NULL);
788
Michal Vasko6027eb92020-07-15 16:37:30 +0200789 LY_CHECK_ARG_RET(ctx, parent || module, parent || node, name, LY_EINVAL);
Michal Vasko013a8182020-03-03 10:46:53 +0100790
Michal Vaskof03ed032020-03-04 13:31:44 +0100791 if (!module) {
792 module = parent->schema->module;
793 }
794
Michal Vasko013a8182020-03-03 10:46:53 +0100795 schema = lys_find_child(parent ? parent->schema : NULL, module, name, 0, LYD_NODE_TERM, 0);
Michal Vasko3a41dff2020-07-15 14:30:28 +0200796 LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "Term node \"%s\" not found.", name), LY_ENOTFOUND);
Michal Vasko013a8182020-03-03 10:46:53 +0100797
Michal Vaskocbff3e92020-05-27 12:56:41 +0200798 rc = lyd_create_term(schema, val_str, val_str ? strlen(val_str) : 0, NULL, lydjson_resolve_prefix, NULL, LYD_JSON, &ret);
Michal Vasko3a41dff2020-07-15 14:30:28 +0200799 LY_CHECK_RET(rc && (rc != LY_EINCOMPLETE), rc);
Michal Vaskocbff3e92020-05-27 12:56:41 +0200800 if (parent) {
Michal Vasko013a8182020-03-03 10:46:53 +0100801 lyd_insert_node(parent, NULL, ret);
802 }
Michal Vasko3a41dff2020-07-15 14:30:28 +0200803
804 if (node) {
805 *node = ret;
806 }
807 return LY_SUCCESS;
Michal Vasko013a8182020-03-03 10:46:53 +0100808}
809
Michal Vasko3a41dff2020-07-15 14:30:28 +0200810API LY_ERR
Michal Vasko013a8182020-03-03 10:46:53 +0100811lyd_new_any(struct lyd_node *parent, const struct lys_module *module, const char *name, const void *value,
Michal Vasko3a41dff2020-07-15 14:30:28 +0200812 LYD_ANYDATA_VALUETYPE value_type, struct lyd_node **node)
Michal Vasko013a8182020-03-03 10:46:53 +0100813{
814 struct lyd_node *ret = NULL;
815 const struct lysc_node *schema;
816 struct ly_ctx *ctx = parent ? parent->schema->module->ctx : (module ? module->ctx : NULL);
817
Michal Vasko6027eb92020-07-15 16:37:30 +0200818 LY_CHECK_ARG_RET(ctx, parent || module, parent || node, name, LY_EINVAL);
Michal Vasko013a8182020-03-03 10:46:53 +0100819
Michal Vaskof03ed032020-03-04 13:31:44 +0100820 if (!module) {
821 module = parent->schema->module;
822 }
823
Michal Vasko013a8182020-03-03 10:46:53 +0100824 schema = lys_find_child(parent ? parent->schema : NULL, module, name, 0, LYD_NODE_ANY, 0);
Michal Vasko3a41dff2020-07-15 14:30:28 +0200825 LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "Any node \"%s\" not found.", name), LY_ENOTFOUND);
Michal Vasko013a8182020-03-03 10:46:53 +0100826
Michal Vasko3a41dff2020-07-15 14:30:28 +0200827 LY_CHECK_RET(lyd_create_any(schema, value, value_type, &ret));
828 if (parent) {
Michal Vasko013a8182020-03-03 10:46:53 +0100829 lyd_insert_node(parent, NULL, ret);
830 }
Michal Vasko3a41dff2020-07-15 14:30:28 +0200831
832 if (node) {
833 *node = ret;
834 }
835 return LY_SUCCESS;
Michal Vasko013a8182020-03-03 10:46:53 +0100836}
837
Michal Vasko4490d312020-06-16 13:08:55 +0200838/**
839 * @brief Update node value.
840 *
841 * @param[in] node Node to update.
842 * @param[in] value New value to set.
843 * @param[in] value_type Type of @p value for any node.
844 * @param[out] new_parent Set to @p node if the value was updated, otherwise set to NULL.
845 * @param[out] new_node Set to @p node if the value was updated, otherwise set to NULL.
846 * @return LY_ERR value.
847 */
Michal Vasko00cbf532020-06-15 13:58:47 +0200848static LY_ERR
849lyd_new_path_update(struct lyd_node *node, const void *value, LYD_ANYDATA_VALUETYPE value_type,
850 struct lyd_node **new_parent, struct lyd_node **new_node)
851{
852 LY_ERR ret = LY_SUCCESS;
853 struct lyd_node *new_any;
854
855 switch (node->schema->nodetype) {
856 case LYS_CONTAINER:
857 case LYS_NOTIF:
858 case LYS_RPC:
859 case LYS_ACTION:
860 case LYS_LIST:
861 case LYS_LEAFLIST:
862 /* if it exists, there is nothing to update */
863 *new_parent = NULL;
864 *new_node = NULL;
865 break;
866 case LYS_LEAF:
867 ret = lyd_change_term(node, value);
868 if ((ret == LY_SUCCESS) || (ret == LY_EEXIST)) {
869 /* there was an actual change (at least of the default flag) */
870 *new_parent = node;
871 *new_node = node;
872 ret = LY_SUCCESS;
873 } else if (ret == LY_ENOT) {
874 /* no change */
875 *new_parent = NULL;
876 *new_node = NULL;
877 ret = LY_SUCCESS;
878 } /* else error */
879 break;
880 case LYS_ANYDATA:
881 case LYS_ANYXML:
882 /* create a new any node */
883 LY_CHECK_RET(lyd_create_any(node->schema, value, value_type, &new_any));
884
885 /* compare with the existing one */
Michal Vasko8f359bf2020-07-28 10:41:15 +0200886 if (lyd_compare_single(node, new_any, 0)) {
Michal Vasko00cbf532020-06-15 13:58:47 +0200887 /* not equal, switch values (so that we can use generic node free) */
888 ((struct lyd_node_any *)new_any)->value = ((struct lyd_node_any *)node)->value;
889 ((struct lyd_node_any *)new_any)->value_type = ((struct lyd_node_any *)node)->value_type;
890 ((struct lyd_node_any *)node)->value.str = value;
891 ((struct lyd_node_any *)node)->value_type = value_type;
892
893 *new_parent = node;
894 *new_node = node;
895 } else {
896 /* they are equal */
897 *new_parent = NULL;
898 *new_node = NULL;
899 }
900 lyd_free_tree(new_any);
901 break;
902 default:
903 LOGINT(LYD_NODE_CTX(node));
904 ret = LY_EINT;
905 break;
906 }
907
908 return ret;
909}
910
Michal Vasko3a41dff2020-07-15 14:30:28 +0200911API LY_ERR
912lyd_new_meta(struct lyd_node *parent, const struct lys_module *module, const char *name, const char *val_str,
913 struct lyd_meta **meta)
Michal Vaskod86997b2020-05-26 15:19:54 +0200914{
915 struct lyd_meta *ret = NULL;
Michal Vasko00cbf532020-06-15 13:58:47 +0200916 const struct ly_ctx *ctx;
Michal Vaskod86997b2020-05-26 15:19:54 +0200917 const char *prefix, *tmp;
918 char *str;
919 size_t pref_len, name_len;
920
Michal Vasko3a41dff2020-07-15 14:30:28 +0200921 LY_CHECK_ARG_RET(NULL, parent, name, module || strchr(name, ':'), LY_EINVAL);
Michal Vasko00cbf532020-06-15 13:58:47 +0200922
923 ctx = LYD_NODE_CTX(parent);
Michal Vaskod86997b2020-05-26 15:19:54 +0200924
925 /* parse the name */
926 tmp = name;
927 if (ly_parse_nodeid(&tmp, &prefix, &pref_len, &name, &name_len) || tmp[0]) {
928 LOGERR(ctx, LY_EINVAL, "Metadata name \"%s\" is not valid.", name);
Michal Vasko3a41dff2020-07-15 14:30:28 +0200929 return LY_EVALID;
Michal Vaskod86997b2020-05-26 15:19:54 +0200930 }
931
932 /* find the module */
933 if (prefix) {
Michal Vasko0b245382020-07-09 15:43:52 +0200934 str = strndup(prefix, pref_len);
Michal Vaskod86997b2020-05-26 15:19:54 +0200935 module = ly_ctx_get_module_implemented(ctx, str);
936 free(str);
Michal Vasko3a41dff2020-07-15 14:30:28 +0200937 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 +0200938 }
939
940 /* set value if none */
941 if (!val_str) {
942 val_str = "";
943 }
944
Michal Vasko3a41dff2020-07-15 14:30:28 +0200945 LY_CHECK_RET(lyd_create_meta(parent, &ret, module, name, name_len, val_str, strlen(val_str), NULL,
946 lydjson_resolve_prefix, NULL, LYD_JSON, parent->schema));
947
948 if (meta) {
949 *meta = ret;
950 }
951 return LY_SUCCESS;
Michal Vaskod86997b2020-05-26 15:19:54 +0200952}
953
Michal Vasko3a41dff2020-07-15 14:30:28 +0200954API LY_ERR
Michal Vasko00cbf532020-06-15 13:58:47 +0200955lyd_new_opaq(struct lyd_node *parent, const struct ly_ctx *ctx, const char *name, const char *value,
Michal Vasko3a41dff2020-07-15 14:30:28 +0200956 const char *module_name, struct lyd_node **node)
Michal Vasko00cbf532020-06-15 13:58:47 +0200957{
958 struct lyd_node *ret = NULL;
959
Michal Vasko6027eb92020-07-15 16:37:30 +0200960 LY_CHECK_ARG_RET(ctx, parent || ctx, parent || node, name, module_name, LY_EINVAL);
Michal Vasko00cbf532020-06-15 13:58:47 +0200961
962 if (!ctx) {
963 ctx = LYD_NODE_CTX(parent);
964 }
965 if (!value) {
966 value = "";
967 }
968
Michal Vasko3a41dff2020-07-15 14:30:28 +0200969 LY_CHECK_RET(lyd_create_opaq(ctx, name, strlen(name), value, strlen(value), NULL, LYD_JSON, NULL, NULL, 0,
970 module_name, &ret));
971 if (parent) {
Michal Vasko00cbf532020-06-15 13:58:47 +0200972 lyd_insert_node(parent, NULL, ret);
973 }
Michal Vasko3a41dff2020-07-15 14:30:28 +0200974
975 if (node) {
976 *node = ret;
977 }
978 return LY_SUCCESS;
Michal Vasko00cbf532020-06-15 13:58:47 +0200979}
980
Michal Vasko3a41dff2020-07-15 14:30:28 +0200981API LY_ERR
982lyd_new_attr(struct lyd_node *parent, const char *module_name, const char *name, const char *val_str,
983 struct ly_attr **attr)
Michal Vasko00cbf532020-06-15 13:58:47 +0200984{
985 struct ly_attr *ret = NULL;
986 const struct ly_ctx *ctx;
987 const char *prefix, *tmp;
988 size_t pref_len, name_len;
989
Michal Vasko3a41dff2020-07-15 14:30:28 +0200990 LY_CHECK_ARG_RET(NULL, parent, !parent->schema, name, LY_EINVAL);
Michal Vasko00cbf532020-06-15 13:58:47 +0200991
992 ctx = LYD_NODE_CTX(parent);
993
994 /* parse the name */
995 tmp = name;
996 if (ly_parse_nodeid(&tmp, &prefix, &pref_len, &name, &name_len) || tmp[0]) {
997 LOGERR(ctx, LY_EINVAL, "Metadata name \"%s\" is not valid.", name);
Michal Vasko3a41dff2020-07-15 14:30:28 +0200998 return LY_EVALID;
Michal Vasko00cbf532020-06-15 13:58:47 +0200999 }
1000
1001 /* set value if none */
1002 if (!val_str) {
1003 val_str = "";
1004 }
1005
Michal Vasko3a41dff2020-07-15 14:30:28 +02001006 LY_CHECK_RET(ly_create_attr(parent, &ret, ctx, name, name_len, val_str, strlen(val_str), NULL, LYD_JSON, NULL,
1007 prefix, pref_len, module_name));
1008
1009 if (attr) {
1010 *attr = ret;
1011 }
1012 return LY_SUCCESS;
Michal Vasko00cbf532020-06-15 13:58:47 +02001013}
1014
1015API LY_ERR
1016lyd_change_term(struct lyd_node *term, const char *val_str)
1017{
1018 LY_ERR ret = LY_SUCCESS;
1019 struct lysc_type *type;
1020 struct lyd_node_term *t;
1021 struct lyd_node *parent;
1022 struct lyd_value val = {0};
1023 int dflt_change, val_change;
1024
1025 LY_CHECK_ARG_RET(NULL, term, term->schema, term->schema->nodetype & LYD_NODE_TERM, LY_EINVAL);
1026
1027 if (!val_str) {
1028 val_str = "";
1029 }
1030 t = (struct lyd_node_term *)term;
1031 type = ((struct lysc_node_leaf *)term->schema)->type;
1032
1033 /* parse the new value */
1034 LY_CHECK_GOTO(ret = lyd_value_store(&val, term->schema, val_str, strlen(val_str), NULL, lydjson_resolve_prefix, NULL,
1035 LYD_JSON), cleanup);
1036
1037 /* compare original and new value */
1038 if (type->plugin->compare(&t->value, &val)) {
1039 /* values differ, switch them */
1040 type->plugin->free(LYD_NODE_CTX(term), &t->value);
1041 t->value = val;
1042 memset(&val, 0, sizeof val);
1043 val_change = 1;
1044 } else {
1045 val_change = 0;
1046 }
1047
1048 /* always clear the default flag */
1049 if (term->flags & LYD_DEFAULT) {
1050 for (parent = term; parent; parent = (struct lyd_node *)parent->parent) {
1051 parent->flags &= ~LYD_DEFAULT;
1052 }
1053 dflt_change = 1;
1054 } else {
1055 dflt_change = 0;
1056 }
1057
1058 if (val_change || dflt_change) {
1059 /* make the node non-validated */
1060 term->flags &= LYD_NEW;
1061 }
1062
1063 if (val_change) {
1064 if (term->schema->nodetype == LYS_LEAFLIST) {
1065 /* leaf-list needs to be hashed again and re-inserted into parent */
1066 lyd_unlink_hash(term);
1067 lyd_hash(term);
1068 LY_CHECK_GOTO(ret = lyd_insert_hash(term), cleanup);
1069 } else if ((term->schema->flags & LYS_KEY) && term->parent) {
1070 /* list needs to be updated if its key was changed */
1071 assert(term->parent->schema->nodetype == LYS_LIST);
1072 lyd_unlink_hash((struct lyd_node *)term->parent);
1073 lyd_hash((struct lyd_node *)term->parent);
1074 LY_CHECK_GOTO(ret = lyd_insert_hash((struct lyd_node *)term->parent), cleanup);
1075 } /* else leaf that is not a key, its value is not used for its hash so it does not change */
1076 }
1077
1078 /* retrun value */
1079 if (!val_change) {
1080 if (dflt_change) {
1081 /* only default flag change */
1082 ret = LY_EEXIST;
1083 } else {
1084 /* no change */
1085 ret = LY_ENOT;
1086 }
1087 } /* else value changed, LY_SUCCESS */
1088
1089cleanup:
1090 type->plugin->free(LYD_NODE_CTX(term), &val);
1091 return ret;
1092}
1093
Michal Vasko41586352020-07-13 13:54:25 +02001094API LY_ERR
1095lyd_change_meta(struct lyd_meta *meta, const char *val_str)
1096{
1097 LY_ERR ret = LY_SUCCESS;
1098 struct lyd_meta *m2;
1099 struct lyd_value val;
1100 int val_change;
1101
1102 LY_CHECK_ARG_RET(NULL, meta, LY_EINVAL);
1103
1104 if (!val_str) {
1105 val_str = "";
1106 }
1107
1108 /* parse the new value into a new meta structure */
1109 LY_CHECK_GOTO(ret = lyd_create_meta(NULL, &m2, meta->annotation->module, meta->name, strlen(meta->name), val_str,
1110 strlen(val_str), NULL, lydjson_resolve_prefix, NULL, LYD_JSON, NULL), cleanup);
1111
1112 /* compare original and new value */
1113 if (lyd_compare_meta(meta, m2)) {
1114 /* values differ, switch them */
1115 val = meta->value;
1116 meta->value = m2->value;
1117 m2->value = val;
1118 val_change = 1;
1119 } else {
1120 val_change = 0;
1121 }
1122
1123 /* retrun value */
1124 if (!val_change) {
1125 /* no change */
1126 ret = LY_ENOT;
1127 } /* else value changed, LY_SUCCESS */
1128
1129cleanup:
1130 return ret;
1131}
1132
Michal Vasko3a41dff2020-07-15 14:30:28 +02001133API LY_ERR
1134lyd_new_path(struct lyd_node *parent, const struct ly_ctx *ctx, const char *path, const char *value, int options,
1135 struct lyd_node **node)
Michal Vasko00cbf532020-06-15 13:58:47 +02001136{
Michal Vasko3a41dff2020-07-15 14:30:28 +02001137 return lyd_new_path2(parent, ctx, path, value, 0, options, node, NULL);
Michal Vasko00cbf532020-06-15 13:58:47 +02001138}
1139
1140API LY_ERR
1141lyd_new_path2(struct lyd_node *parent, const struct ly_ctx *ctx, const char *path, const void *value,
1142 LYD_ANYDATA_VALUETYPE value_type, int options, struct lyd_node **new_parent, struct lyd_node **new_node)
1143{
1144 LY_ERR ret = LY_SUCCESS, r;
1145 struct lyxp_expr *exp = NULL;
1146 struct ly_path *p = NULL;
1147 struct lyd_node *nparent = NULL, *nnode = NULL, *node = NULL, *cur_parent;
1148 const struct lysc_node *schema;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001149 LY_ARRAY_COUNT_TYPE path_idx = 0;
Michal Vasko00cbf532020-06-15 13:58:47 +02001150 struct ly_path_predicate *pred;
1151
1152 LY_CHECK_ARG_RET(ctx, parent || ctx, path, (path[0] == '/') || parent, LY_EINVAL);
1153
1154 if (!ctx) {
1155 ctx = LYD_NODE_CTX(parent);
1156 }
1157
1158 /* parse path */
Michal Vasko6b26e742020-07-17 15:02:10 +02001159 LY_CHECK_GOTO(ret = ly_path_parse(ctx, NULL, path, strlen(path), LY_PATH_BEGIN_EITHER, LY_PATH_LREF_FALSE,
Michal Vasko00cbf532020-06-15 13:58:47 +02001160 LY_PATH_PREFIX_OPTIONAL, LY_PATH_PRED_SIMPLE, &exp), cleanup);
1161
1162 /* compile path */
1163 LY_CHECK_GOTO(ret = ly_path_compile(ctx, NULL, parent ? parent->schema : NULL, exp, LY_PATH_LREF_FALSE,
1164 options & LYD_NEWOPT_OUTPUT ? LY_PATH_OPER_OUTPUT : LY_PATH_OPER_INPUT,
1165 LY_PATH_TARGET_MANY, lydjson_resolve_prefix, NULL, LYD_JSON, &p), cleanup);
1166
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001167 schema = p[LY_ARRAY_COUNT(p) - 1].node;
1168 if ((schema->nodetype == LYS_LIST) && (p[LY_ARRAY_COUNT(p) - 1].pred_type == LY_PATH_PREDTYPE_NONE)
Michal Vasko00cbf532020-06-15 13:58:47 +02001169 && !(options & LYD_NEWOPT_OPAQ)) {
1170 LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_XPATH, "Predicate missing for %s \"%s\" in path.",
1171 lys_nodetype2str(schema->nodetype), schema->name);
1172 ret = LY_EINVAL;
1173 goto cleanup;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001174 } 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 +02001175 /* parse leafref value into a predicate, if not defined in the path */
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001176 p[LY_ARRAY_COUNT(p) - 1].pred_type = LY_PATH_PREDTYPE_LEAFLIST;
1177 LY_ARRAY_NEW_GOTO(ctx, p[LY_ARRAY_COUNT(p) - 1].predicates, pred, ret, cleanup);
Michal Vasko00cbf532020-06-15 13:58:47 +02001178
1179 if (!value) {
1180 value = "";
1181 }
1182
1183 r = LY_SUCCESS;
1184 if (options & LYD_NEWOPT_OPAQ) {
1185 r = lys_value_validate(NULL, schema, value, strlen(value), lydjson_resolve_prefix, NULL, LYD_JSON);
1186 }
1187 if (!r) {
1188 LY_CHECK_GOTO(ret = lyd_value_store(&pred->value, schema, value, strlen(value), NULL, lydjson_resolve_prefix,
1189 NULL, LYD_JSON), cleanup);
1190 } /* else we have opaq flag and the value is not valid, leavne no predicate and then create an opaque node */
1191 }
1192
1193 /* try to find any existing nodes in the path */
1194 if (parent) {
1195 ret = ly_path_eval_partial(p, parent, &path_idx, &node);
1196 if (ret == LY_SUCCESS) {
1197 /* the node exists, are we supposed to update it or is it just a default? */
1198 if (!(options & LYD_NEWOPT_UPDATE) && !(node->flags & LYD_DEFAULT)) {
1199 LOGERR(ctx, LY_EEXIST, "Path \"%s\" already exists", path);
1200 ret = LY_EEXIST;
1201 goto cleanup;
1202 }
1203
1204 /* update the existing node */
1205 ret = lyd_new_path_update(node, value, value_type, &nparent, &nnode);
1206 goto cleanup;
1207 } else if (ret == LY_EINCOMPLETE) {
1208 /* some nodes were found, adjust the iterator to the next segment */
1209 ++path_idx;
1210 } else if (ret == LY_ENOTFOUND) {
1211 /* 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 +02001212 if (lysc_data_parent(p[LY_ARRAY_COUNT(p) - 1].node)) {
Michal Vasko00cbf532020-06-15 13:58:47 +02001213 node = parent;
1214 }
1215 } else {
1216 /* error */
1217 goto cleanup;
1218 }
1219 }
1220
1221 /* create all the non-existing nodes in a loop */
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001222 for (; path_idx < LY_ARRAY_COUNT(p); ++path_idx) {
Michal Vasko00cbf532020-06-15 13:58:47 +02001223 cur_parent = node;
1224 schema = p[path_idx].node;
1225
1226 switch (schema->nodetype) {
1227 case LYS_LIST:
1228 if (!(schema->flags & LYS_KEYLESS)) {
1229 if ((options & LYD_NEWOPT_OPAQ) && (p[path_idx].pred_type == LY_PATH_PREDTYPE_NONE)) {
1230 /* creating opaque list without keys */
1231 LY_CHECK_GOTO(ret = lyd_create_opaq(ctx, schema->name, strlen(schema->name), NULL, 0, NULL,
1232 LYD_JSON, NULL, NULL, 0, schema->module->name, &node), cleanup);
1233 } else {
1234 assert(p[path_idx].pred_type == LY_PATH_PREDTYPE_LIST);
1235 LY_CHECK_GOTO(ret = lyd_create_list(schema, p[path_idx].predicates, &node), cleanup);
1236 }
1237 break;
1238 }
1239 /* fallthrough */
1240 case LYS_CONTAINER:
1241 case LYS_NOTIF:
1242 case LYS_RPC:
1243 case LYS_ACTION:
1244 LY_CHECK_GOTO(ret = lyd_create_inner(schema, &node), cleanup);
1245 break;
1246 case LYS_LEAFLIST:
1247 if ((options & LYD_NEWOPT_OPAQ) && (p[path_idx].pred_type == LY_PATH_PREDTYPE_NONE)) {
1248 /* creating opaque leaf-list without value */
1249 LY_CHECK_GOTO(ret = lyd_create_opaq(ctx, schema->name, strlen(schema->name), NULL, 0, NULL,
1250 LYD_JSON, NULL, NULL, 0, schema->module->name, &node), cleanup);
1251 } else {
1252 assert(p[path_idx].pred_type == LY_PATH_PREDTYPE_LEAFLIST);
1253 LY_CHECK_GOTO(ret = lyd_create_term2(schema, &p[path_idx].predicates[0].value, &node), cleanup);
1254 }
1255 break;
1256 case LYS_LEAF:
1257 /* make there is some value */
1258 if (!value) {
1259 value = "";
1260 }
1261
1262 r = LY_SUCCESS;
1263 if (options & LYD_NEWOPT_OPAQ) {
1264 r = lys_value_validate(NULL, schema, value, strlen(value), lydjson_resolve_prefix, NULL, LYD_JSON);
1265 }
1266 if (!r) {
1267 LY_CHECK_GOTO(ret = lyd_create_term(schema, value, strlen(value), NULL, lydjson_resolve_prefix, NULL,
1268 LYD_JSON, &node), cleanup);
1269 } else {
1270 /* creating opaque leaf without value */
1271 LY_CHECK_GOTO(ret = lyd_create_opaq(ctx, schema->name, strlen(schema->name), NULL, 0, NULL,
1272 LYD_JSON, NULL, NULL, 0, schema->module->name, &node), cleanup);
1273 }
1274 break;
1275 case LYS_ANYDATA:
1276 case LYS_ANYXML:
1277 LY_CHECK_GOTO(ret = lyd_create_any(schema, value, value_type, &node), cleanup);
1278 break;
1279 default:
1280 LOGINT(ctx);
1281 ret = LY_EINT;
1282 goto cleanup;
1283 }
1284
1285 if (cur_parent) {
1286 /* connect to the parent */
1287 lyd_insert_node(cur_parent, NULL, node);
1288 } else if (parent) {
1289 /* connect to top-level siblings */
1290 lyd_insert_node(NULL, &parent, node);
1291 }
1292
1293 /* update remembered nodes */
1294 if (!nparent) {
1295 nparent = node;
1296 }
1297 nnode = node;
1298 }
1299
1300cleanup:
1301 lyxp_expr_free(ctx, exp);
1302 ly_path_free(ctx, p);
1303 if (!ret) {
1304 /* set out params only on success */
1305 if (new_parent) {
1306 *new_parent = nparent;
1307 }
1308 if (new_node) {
1309 *new_node = nnode;
1310 }
1311 }
1312 return ret;
1313}
1314
Michal Vasko90932a92020-02-12 14:33:03 +01001315struct lyd_node *
Michal Vaskob104f112020-07-17 09:54:54 +02001316lyd_insert_get_next_anchor(const struct lyd_node *first_sibling, const struct lyd_node *new_node)
Michal Vasko90932a92020-02-12 14:33:03 +01001317{
Michal Vaskob104f112020-07-17 09:54:54 +02001318 const struct lysc_node *schema, *sparent;
Michal Vasko90932a92020-02-12 14:33:03 +01001319 struct lyd_node *match = NULL;
Michal Vaskob104f112020-07-17 09:54:54 +02001320 int found;
Michal Vasko90932a92020-02-12 14:33:03 +01001321
Michal Vaskob104f112020-07-17 09:54:54 +02001322 assert(new_node);
1323
1324 if (!first_sibling || !new_node->schema) {
1325 /* insert at the end, no next anchor */
Michal Vasko90932a92020-02-12 14:33:03 +01001326 return NULL;
1327 }
1328
Michal Vaskob104f112020-07-17 09:54:54 +02001329 if (first_sibling->parent && first_sibling->parent->children_ht) {
1330 /* find the anchor using hashes */
1331 sparent = first_sibling->parent->schema;
1332 schema = lys_getnext(new_node->schema, sparent, NULL, 0);
1333 while (schema) {
1334 /* keep trying to find the first existing instance of the closest following schema sibling,
1335 * otherwise return NULL - inserting at the end */
1336 if (!lyd_find_sibling_schema(first_sibling, schema, &match)) {
1337 break;
1338 }
1339
1340 schema = lys_getnext(schema, sparent, NULL, 0);
1341 }
1342 } else {
1343 /* find the anchor without hashes */
1344 match = (struct lyd_node *)first_sibling;
1345 if (!lysc_data_parent(new_node->schema)) {
1346 /* we are in top-level, skip all the data from preceding modules */
1347 LY_LIST_FOR(match, match) {
1348 if (!match->schema || (strcmp(lyd_owner_module(match)->name, lyd_owner_module(new_node)->name) >= 0)) {
1349 break;
1350 }
1351 }
1352 }
1353
1354 /* get the first schema sibling */
1355 sparent = lysc_data_parent(new_node->schema);
1356 schema = lys_getnext(NULL, sparent, new_node->schema->module->compiled, 0);
1357
1358 found = 0;
1359 LY_LIST_FOR(match, match) {
1360 if (!match->schema || (lyd_owner_module(match) != lyd_owner_module(new_node))) {
1361 /* we have found an opaque node, which must be at the end, so use it OR
1362 * modules do not match, so we must have traversed all the data from new_node module (if any),
1363 * we have found the first node of the next module, that is what we want */
1364 break;
1365 }
1366
1367 /* skip schema nodes until we find the instantiated one */
1368 while (!found) {
1369 if (new_node->schema == schema) {
1370 /* we have found the schema of the new node, continue search to find the first
1371 * data node with a different schema (after our schema) */
1372 found = 1;
1373 break;
1374 }
1375 if (match->schema == schema) {
1376 /* current node (match) is a data node still before the new node, continue search in data */
1377 break;
1378 }
1379 schema = lys_getnext(schema, sparent, new_node->schema->module->compiled, 0);
1380 assert(schema);
1381 }
1382
1383 if (found && (match->schema != new_node->schema)) {
1384 /* find the next node after we have found our node schema data instance */
1385 break;
1386 }
1387 }
Michal Vasko90932a92020-02-12 14:33:03 +01001388 }
1389
1390 return match;
1391}
1392
1393/**
1394 * @brief Insert node after a sibling.
1395 *
Michal Vasko9f96a052020-03-10 09:41:45 +01001396 * Handles inserting into NP containers and key-less lists.
1397 *
Michal Vasko90932a92020-02-12 14:33:03 +01001398 * @param[in] sibling Sibling to insert after.
1399 * @param[in] node Node to insert.
1400 */
1401static void
Michal Vaskof03ed032020-03-04 13:31:44 +01001402lyd_insert_after_node(struct lyd_node *sibling, struct lyd_node *node)
Michal Vasko90932a92020-02-12 14:33:03 +01001403{
Michal Vasko0249f7c2020-03-05 16:36:40 +01001404 struct lyd_node_inner *par;
1405
Michal Vasko90932a92020-02-12 14:33:03 +01001406 assert(!node->next && (node->prev == node));
1407
1408 node->next = sibling->next;
1409 node->prev = sibling;
1410 sibling->next = node;
1411 if (node->next) {
1412 /* sibling had a succeeding node */
1413 node->next->prev = node;
1414 } else {
1415 /* sibling was last, find first sibling and change its prev */
1416 if (sibling->parent) {
1417 sibling = sibling->parent->child;
1418 } else {
1419 for (; sibling->prev->next != node; sibling = sibling->prev);
1420 }
1421 sibling->prev = node;
1422 }
1423 node->parent = sibling->parent;
Michal Vasko0249f7c2020-03-05 16:36:40 +01001424
Michal Vasko9f96a052020-03-10 09:41:45 +01001425 for (par = node->parent; par; par = par->parent) {
1426 if ((par->flags & LYD_DEFAULT) && !(node->flags & LYD_DEFAULT)) {
1427 /* remove default flags from NP containers */
Michal Vasko0249f7c2020-03-05 16:36:40 +01001428 par->flags &= ~LYD_DEFAULT;
1429 }
Michal Vaskob104f112020-07-17 09:54:54 +02001430 if (par->schema && (par->schema->nodetype == LYS_LIST) && (par->schema->flags & LYS_KEYLESS)) {
Michal Vasko9f96a052020-03-10 09:41:45 +01001431 /* rehash key-less list */
1432 lyd_hash((struct lyd_node *)par);
1433 }
Michal Vasko0249f7c2020-03-05 16:36:40 +01001434 }
Michal Vasko90932a92020-02-12 14:33:03 +01001435}
1436
1437/**
1438 * @brief Insert node before a sibling.
1439 *
Michal Vasko9f96a052020-03-10 09:41:45 +01001440 * Handles inserting into NP containers and key-less lists.
1441 *
Michal Vasko90932a92020-02-12 14:33:03 +01001442 * @param[in] sibling Sibling to insert before.
1443 * @param[in] node Node to insert.
1444 */
1445static void
Michal Vaskof03ed032020-03-04 13:31:44 +01001446lyd_insert_before_node(struct lyd_node *sibling, struct lyd_node *node)
Michal Vasko90932a92020-02-12 14:33:03 +01001447{
Michal Vasko0249f7c2020-03-05 16:36:40 +01001448 struct lyd_node_inner *par;
1449
Michal Vasko90932a92020-02-12 14:33:03 +01001450 assert(!node->next && (node->prev == node));
1451
1452 node->next = sibling;
1453 /* covers situation of sibling being first */
1454 node->prev = sibling->prev;
1455 sibling->prev = node;
1456 if (node->prev->next) {
1457 /* sibling had a preceding node */
1458 node->prev->next = node;
1459 } else if (sibling->parent) {
1460 /* sibling was first and we must also change parent child pointer */
1461 sibling->parent->child = node;
1462 }
1463 node->parent = sibling->parent;
Michal Vasko0249f7c2020-03-05 16:36:40 +01001464
Michal Vasko9f96a052020-03-10 09:41:45 +01001465 for (par = node->parent; par; par = par->parent) {
1466 if ((par->flags & LYD_DEFAULT) && !(node->flags & LYD_DEFAULT)) {
1467 /* remove default flags from NP containers */
Michal Vasko0249f7c2020-03-05 16:36:40 +01001468 par->flags &= ~LYD_DEFAULT;
1469 }
Michal Vaskob104f112020-07-17 09:54:54 +02001470 if (par->schema && (par->schema->nodetype == LYS_LIST) && (par->schema->flags & LYS_KEYLESS)) {
Michal Vasko9f96a052020-03-10 09:41:45 +01001471 /* rehash key-less list */
1472 lyd_hash((struct lyd_node *)par);
1473 }
Michal Vasko0249f7c2020-03-05 16:36:40 +01001474 }
Michal Vasko90932a92020-02-12 14:33:03 +01001475}
1476
1477/**
Michal Vaskob104f112020-07-17 09:54:54 +02001478 * @brief Insert node as the first and only child of a parent.
Michal Vasko90932a92020-02-12 14:33:03 +01001479 *
Michal Vasko9f96a052020-03-10 09:41:45 +01001480 * Handles inserting into NP containers and key-less lists.
1481 *
Michal Vasko90932a92020-02-12 14:33:03 +01001482 * @param[in] parent Parent to insert into.
1483 * @param[in] node Node to insert.
1484 */
1485static void
Michal Vaskob104f112020-07-17 09:54:54 +02001486lyd_insert_only_child(struct lyd_node *parent, struct lyd_node *node)
Michal Vasko90932a92020-02-12 14:33:03 +01001487{
1488 struct lyd_node_inner *par;
1489
Michal Vaskob104f112020-07-17 09:54:54 +02001490 assert(parent && !lyd_node_children(parent, 0) && !node->next && (node->prev == node));
Michal Vasko52927e22020-03-16 17:26:14 +01001491 assert(!parent->schema || (parent->schema->nodetype & LYD_NODE_INNER));
Michal Vasko90932a92020-02-12 14:33:03 +01001492
1493 par = (struct lyd_node_inner *)parent;
1494
Michal Vaskob104f112020-07-17 09:54:54 +02001495 par->child = node;
Michal Vasko90932a92020-02-12 14:33:03 +01001496 node->parent = par;
Michal Vasko0249f7c2020-03-05 16:36:40 +01001497
Michal Vasko9f96a052020-03-10 09:41:45 +01001498 for (; par; par = par->parent) {
1499 if ((par->flags & LYD_DEFAULT) && !(node->flags & LYD_DEFAULT)) {
1500 /* remove default flags from NP containers */
Michal Vasko0249f7c2020-03-05 16:36:40 +01001501 par->flags &= ~LYD_DEFAULT;
1502 }
Michal Vasko52927e22020-03-16 17:26:14 +01001503 if (par->schema && (par->schema->nodetype == LYS_LIST) && (par->schema->flags & LYS_KEYLESS)) {
Michal Vasko9f96a052020-03-10 09:41:45 +01001504 /* rehash key-less list */
1505 lyd_hash((struct lyd_node *)par);
1506 }
Michal Vasko0249f7c2020-03-05 16:36:40 +01001507 }
Michal Vasko751cb4d2020-07-14 12:25:28 +02001508}
Michal Vasko0249f7c2020-03-05 16:36:40 +01001509
Michal Vasko751cb4d2020-07-14 12:25:28 +02001510/**
1511 * @brief Learn whether a list instance has all the keys.
1512 *
1513 * @param[in] list List instance to check.
1514 * @return non-zero if all the keys were found,
1515 * @return 0 otherwise.
1516 */
1517static int
1518lyd_insert_has_keys(const struct lyd_node *list)
1519{
1520 const struct lyd_node *key;
1521 const struct lysc_node *skey = NULL;
1522
1523 assert(list->schema->nodetype == LYS_LIST);
1524 key = lyd_node_children(list, 0);
1525 while ((skey = lys_getnext(skey, list->schema, NULL, 0)) && (skey->flags & LYS_KEY)) {
1526 if (!key || (key->schema != skey)) {
1527 /* key missing */
1528 return 0;
1529 }
1530
1531 key = key->next;
1532 }
1533
1534 /* all keys found */
1535 return 1;
Michal Vasko90932a92020-02-12 14:33:03 +01001536}
1537
1538void
Michal Vaskob104f112020-07-17 09:54:54 +02001539lyd_insert_node(struct lyd_node *parent, struct lyd_node **first_sibling_p, struct lyd_node *node)
Michal Vasko90932a92020-02-12 14:33:03 +01001540{
Michal Vaskob104f112020-07-17 09:54:54 +02001541 struct lyd_node *anchor, *first_sibling;
Michal Vasko90932a92020-02-12 14:33:03 +01001542
Michal Vaskob104f112020-07-17 09:54:54 +02001543 /* inserting list without its keys is not supported */
1544 assert((parent || first_sibling_p) && node && (node->hash || !node->schema));
Michal Vasko9b368d32020-02-14 13:53:31 +01001545
Michal Vaskob104f112020-07-17 09:54:54 +02001546 if (!parent && first_sibling_p && (*first_sibling_p) && (*first_sibling_p)->parent) {
1547 parent = (struct lyd_node *)(*first_sibling_p)->parent;
Michal Vasko9b368d32020-02-14 13:53:31 +01001548 }
Michal Vasko90932a92020-02-12 14:33:03 +01001549
Michal Vaskob104f112020-07-17 09:54:54 +02001550 /* get first sibling */
1551 first_sibling = parent ? ((struct lyd_node_inner *)parent)->child : *first_sibling_p;
Michal Vasko9f96a052020-03-10 09:41:45 +01001552
Michal Vaskob104f112020-07-17 09:54:54 +02001553 /* find the anchor, our next node, so we can insert before it */
1554 anchor = lyd_insert_get_next_anchor(first_sibling, node);
1555 if (anchor) {
1556 lyd_insert_before_node(anchor, node);
1557 } else if (first_sibling) {
1558 lyd_insert_after_node(first_sibling->prev, node);
1559 } else if (parent) {
1560 lyd_insert_only_child(parent, node);
Michal Vasko90932a92020-02-12 14:33:03 +01001561 } else {
Michal Vaskob104f112020-07-17 09:54:54 +02001562 *first_sibling_p = node;
1563 }
1564
1565 /* insert into parent HT */
1566 lyd_insert_hash(node);
1567
1568 /* finish hashes for our parent, if needed and possible */
1569 if (node->schema && (node->schema->flags & LYS_KEY) && lyd_insert_has_keys(parent)) {
1570 lyd_hash(parent);
1571
1572 /* now we can insert even the list into its parent HT */
1573 lyd_insert_hash(parent);
Michal Vasko90932a92020-02-12 14:33:03 +01001574 }
Michal Vasko90932a92020-02-12 14:33:03 +01001575}
1576
Michal Vaskof03ed032020-03-04 13:31:44 +01001577static LY_ERR
1578lyd_insert_check_schema(const struct lysc_node *parent, const struct lysc_node *schema)
1579{
1580 const struct lysc_node *par2;
1581
1582 assert(schema);
Michal Vasko62ed12d2020-05-21 10:08:25 +02001583 assert(!parent || !(parent->nodetype & (LYS_CASE | LYS_CHOICE)));
Michal Vaskof03ed032020-03-04 13:31:44 +01001584
1585 /* find schema parent */
Michal Vasko62ed12d2020-05-21 10:08:25 +02001586 par2 = lysc_data_parent(schema);
Michal Vaskof03ed032020-03-04 13:31:44 +01001587
1588 if (parent) {
1589 /* inner node */
1590 if (par2 != parent) {
Michal Vaskob104f112020-07-17 09:54:54 +02001591 LOGERR(schema->module->ctx, LY_EINVAL, "Cannot insert, parent of \"%s\" is not \"%s\".", schema->name,
1592 parent->name);
Michal Vaskof03ed032020-03-04 13:31:44 +01001593 return LY_EINVAL;
1594 }
1595 } else {
1596 /* top-level node */
1597 if (par2) {
Radek Krejcif6d14cb2020-07-02 16:11:45 +02001598 LOGERR(schema->module->ctx, LY_EINVAL, "Cannot insert, node \"%s\" is not top-level.", schema->name);
Michal Vaskof03ed032020-03-04 13:31:44 +01001599 return LY_EINVAL;
1600 }
1601 }
1602
1603 return LY_SUCCESS;
1604}
1605
1606API LY_ERR
Michal Vaskob104f112020-07-17 09:54:54 +02001607lyd_insert_child(struct lyd_node *parent, struct lyd_node *node)
Michal Vaskof03ed032020-03-04 13:31:44 +01001608{
1609 struct lyd_node *iter;
1610
Michal Vasko654bc852020-06-23 13:28:06 +02001611 LY_CHECK_ARG_RET(NULL, parent, node, parent->schema->nodetype & LYD_NODE_INNER, LY_EINVAL);
Michal Vaskof03ed032020-03-04 13:31:44 +01001612
1613 LY_CHECK_RET(lyd_insert_check_schema(parent->schema, node->schema));
1614
1615 if (node->schema->flags & LYS_KEY) {
1616 LOGERR(parent->schema->module->ctx, LY_EINVAL, "Cannot insert key \"%s\".", node->schema->name);
1617 return LY_EINVAL;
1618 }
1619
1620 if (node->parent || node->prev->next) {
1621 lyd_unlink_tree(node);
1622 }
1623
1624 while (node) {
1625 iter = node->next;
1626 lyd_unlink_tree(node);
1627 lyd_insert_node(parent, NULL, node);
1628 node = iter;
1629 }
1630 return LY_SUCCESS;
1631}
1632
1633API LY_ERR
Michal Vaskob104f112020-07-17 09:54:54 +02001634lyd_insert_sibling(struct lyd_node *sibling, struct lyd_node *node, struct lyd_node **first)
Michal Vaskob1b5c262020-03-05 14:29:47 +01001635{
1636 struct lyd_node *iter;
1637
Michal Vaskob104f112020-07-17 09:54:54 +02001638 LY_CHECK_ARG_RET(NULL, node, LY_EINVAL);
Michal Vaskob1b5c262020-03-05 14:29:47 +01001639
Michal Vaskob104f112020-07-17 09:54:54 +02001640 if (sibling) {
1641 LY_CHECK_RET(lyd_insert_check_schema(lysc_data_parent(sibling->schema), node->schema));
Michal Vaskob1b5c262020-03-05 14:29:47 +01001642 }
1643
1644 if (node->parent || node->prev->next) {
1645 lyd_unlink_tree(node);
1646 }
1647
1648 while (node) {
Michal Vaskob104f112020-07-17 09:54:54 +02001649 if (node->schema->flags & LYS_KEY) {
1650 LOGERR(LYD_NODE_CTX(node), LY_EINVAL, "Cannot insert key \"%s\".", node->schema->name);
1651 return LY_EINVAL;
1652 }
1653
Michal Vaskob1b5c262020-03-05 14:29:47 +01001654 iter = node->next;
1655 lyd_unlink_tree(node);
1656 lyd_insert_node(NULL, &sibling, node);
1657 node = iter;
1658 }
Michal Vaskob1b5c262020-03-05 14:29:47 +01001659
Michal Vaskob104f112020-07-17 09:54:54 +02001660 if (first) {
1661 /* find the first sibling */
1662 *first = sibling;
1663 while ((*first)->prev->next) {
1664 *first = (*first)->prev;
Michal Vasko0249f7c2020-03-05 16:36:40 +01001665 }
1666 }
1667
1668 return LY_SUCCESS;
1669}
1670
Michal Vaskob1b5c262020-03-05 14:29:47 +01001671API LY_ERR
Michal Vaskof03ed032020-03-04 13:31:44 +01001672lyd_insert_before(struct lyd_node *sibling, struct lyd_node *node)
1673{
1674 struct lyd_node *iter;
1675
1676 LY_CHECK_ARG_RET(NULL, sibling, node, LY_EINVAL);
1677
Michal Vasko62ed12d2020-05-21 10:08:25 +02001678 LY_CHECK_RET(lyd_insert_check_schema(lysc_data_parent(sibling->schema), node->schema));
Michal Vaskof03ed032020-03-04 13:31:44 +01001679
Michal Vaskob104f112020-07-17 09:54:54 +02001680 if (!(node->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) || !(node->schema->flags & LYS_ORDBY_USER)) {
1681 LOGERR(LYD_NODE_CTX(sibling), LY_EINVAL, "Can be used only for user-ordered nodes.");
Michal Vaskof03ed032020-03-04 13:31:44 +01001682 return LY_EINVAL;
1683 }
1684
1685 if (node->parent || node->prev->next) {
1686 lyd_unlink_tree(node);
1687 }
1688
1689 /* insert in reverse order to get the original order */
1690 node = node->prev;
1691 while (node) {
1692 iter = node->prev;
1693 lyd_unlink_tree(node);
1694
1695 lyd_insert_before_node(sibling, node);
Michal Vasko751cb4d2020-07-14 12:25:28 +02001696 lyd_insert_hash(node);
1697
Michal Vaskof03ed032020-03-04 13:31:44 +01001698 /* move the anchor accordingly */
1699 sibling = node;
1700
1701 node = (iter == node) ? NULL : iter;
1702 }
1703 return LY_SUCCESS;
1704}
1705
1706API LY_ERR
1707lyd_insert_after(struct lyd_node *sibling, struct lyd_node *node)
1708{
1709 struct lyd_node *iter;
1710
1711 LY_CHECK_ARG_RET(NULL, sibling, node, LY_EINVAL);
1712
Michal Vasko62ed12d2020-05-21 10:08:25 +02001713 LY_CHECK_RET(lyd_insert_check_schema(lysc_data_parent(sibling->schema), node->schema));
Michal Vaskof03ed032020-03-04 13:31:44 +01001714
Michal Vaskob104f112020-07-17 09:54:54 +02001715 if (!(node->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) || !(node->schema->flags & LYS_ORDBY_USER)) {
1716 LOGERR(LYD_NODE_CTX(sibling), LY_EINVAL, "Can be used only for user-ordered nodes.");
Michal Vaskof03ed032020-03-04 13:31:44 +01001717 return LY_EINVAL;
1718 }
1719
1720 if (node->parent || node->prev->next) {
1721 lyd_unlink_tree(node);
1722 }
1723
1724 while (node) {
1725 iter = node->next;
1726 lyd_unlink_tree(node);
1727
1728 lyd_insert_after_node(sibling, node);
Michal Vasko751cb4d2020-07-14 12:25:28 +02001729 lyd_insert_hash(node);
1730
Michal Vaskof03ed032020-03-04 13:31:44 +01001731 /* move the anchor accordingly */
1732 sibling = node;
1733
1734 node = iter;
1735 }
1736 return LY_SUCCESS;
1737}
1738
1739API void
1740lyd_unlink_tree(struct lyd_node *node)
1741{
1742 struct lyd_node *iter;
1743
1744 if (!node) {
1745 return;
1746 }
1747
Michal Vaskob104f112020-07-17 09:54:54 +02001748 /* update hashes while still linked into the tree */
1749 lyd_unlink_hash(node);
1750
Michal Vaskof03ed032020-03-04 13:31:44 +01001751 /* unlink from siblings */
1752 if (node->prev->next) {
1753 node->prev->next = node->next;
1754 }
1755 if (node->next) {
1756 node->next->prev = node->prev;
1757 } else {
1758 /* unlinking the last node */
1759 if (node->parent) {
1760 iter = node->parent->child;
1761 } else {
1762 iter = node->prev;
1763 while (iter->prev != node) {
1764 iter = iter->prev;
1765 }
1766 }
1767 /* update the "last" pointer from the first node */
1768 iter->prev = node->prev;
1769 }
1770
1771 /* unlink from parent */
1772 if (node->parent) {
1773 if (node->parent->child == node) {
1774 /* the node is the first child */
1775 node->parent->child = node->next;
1776 }
1777
Michal Vaskoab49dbe2020-07-17 12:32:47 +02001778 /* check for NP container whether its last non-default node is not being unlinked */
1779 if (node->parent->schema && (node->parent->schema->nodetype == LYS_CONTAINER)
1780 && !(node->parent->flags & LYD_DEFAULT) && !(node->parent->schema->flags & LYS_PRESENCE)) {
1781 LY_LIST_FOR(node->parent->child, iter) {
1782 if ((iter != node) && !(iter->flags & LYD_DEFAULT)) {
1783 break;
1784 }
1785 }
1786 if (!iter) {
1787 node->parent->flags |= LYD_DEFAULT;
1788 }
1789 }
1790
Michal Vaskof03ed032020-03-04 13:31:44 +01001791 /* check for keyless list and update its hash */
1792 for (iter = (struct lyd_node *)node->parent; iter; iter = (struct lyd_node *)iter->parent) {
Michal Vasko413c7f22020-05-05 12:34:06 +02001793 if (iter->schema && (iter->schema->flags & LYS_KEYLESS)) {
Michal Vaskof03ed032020-03-04 13:31:44 +01001794 lyd_hash(iter);
1795 }
1796 }
1797
1798 node->parent = NULL;
1799 }
1800
1801 node->next = NULL;
1802 node->prev = node;
1803}
1804
Michal Vasko90932a92020-02-12 14:33:03 +01001805LY_ERR
Michal Vasko9f96a052020-03-10 09:41:45 +01001806lyd_create_meta(struct lyd_node *parent, struct lyd_meta **meta, const struct lys_module *mod, const char *name,
Michal Vasko52927e22020-03-16 17:26:14 +01001807 size_t name_len, const char *value, size_t value_len, int *dynamic, ly_clb_resolve_prefix resolve_prefix,
Michal Vasko8d544252020-03-02 10:19:52 +01001808 void *prefix_data, LYD_FORMAT format, const struct lysc_node *ctx_snode)
Michal Vasko90932a92020-02-12 14:33:03 +01001809{
1810 LY_ERR ret;
1811 struct lysc_ext_instance *ant = NULL;
Michal Vasko9f96a052020-03-10 09:41:45 +01001812 struct lyd_meta *mt, *last;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001813 LY_ARRAY_COUNT_TYPE u;
Michal Vasko90932a92020-02-12 14:33:03 +01001814
Michal Vasko9f96a052020-03-10 09:41:45 +01001815 assert((parent || meta) && mod);
Michal Vasko6f4cbb62020-02-28 11:15:47 +01001816
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001817 LY_ARRAY_FOR(mod->compiled->exts, u) {
1818 if (mod->compiled->exts[u].def->plugin == lyext_plugins_internal[LYEXT_PLUGIN_INTERNAL_ANNOTATION].plugin &&
1819 !ly_strncmp(mod->compiled->exts[u].argument, name, name_len)) {
Michal Vasko90932a92020-02-12 14:33:03 +01001820 /* we have the annotation definition */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001821 ant = &mod->compiled->exts[u];
Michal Vasko90932a92020-02-12 14:33:03 +01001822 break;
1823 }
1824 }
1825 if (!ant) {
1826 /* attribute is not defined as a metadata annotation (RFC 7952) */
1827 LOGERR(mod->ctx, LY_EINVAL, "Annotation definition for attribute \"%s:%.*s\" not found.",
1828 mod->name, name_len, name);
1829 return LY_EINVAL;
1830 }
1831
Michal Vasko9f96a052020-03-10 09:41:45 +01001832 mt = calloc(1, sizeof *mt);
1833 LY_CHECK_ERR_RET(!mt, LOGMEM(mod->ctx), LY_EMEM);
1834 mt->parent = parent;
1835 mt->annotation = ant;
Michal Vasko52927e22020-03-16 17:26:14 +01001836 ret = lyd_value_parse_meta(mod->ctx, mt, value, value_len, dynamic, 0, resolve_prefix, prefix_data, format, ctx_snode, NULL);
Michal Vasko90932a92020-02-12 14:33:03 +01001837 if ((ret != LY_SUCCESS) && (ret != LY_EINCOMPLETE)) {
Michal Vasko9f96a052020-03-10 09:41:45 +01001838 free(mt);
Michal Vasko90932a92020-02-12 14:33:03 +01001839 return ret;
1840 }
Michal Vasko9f96a052020-03-10 09:41:45 +01001841 mt->name = lydict_insert(mod->ctx, name, name_len);
Michal Vasko90932a92020-02-12 14:33:03 +01001842
Michal Vasko6f4cbb62020-02-28 11:15:47 +01001843 /* insert as the last attribute */
1844 if (parent) {
Michal Vasko9f96a052020-03-10 09:41:45 +01001845 if (parent->meta) {
1846 for (last = parent->meta; last->next; last = last->next);
1847 last->next = mt;
Michal Vasko6f4cbb62020-02-28 11:15:47 +01001848 } else {
Michal Vasko9f96a052020-03-10 09:41:45 +01001849 parent->meta = mt;
Michal Vasko6f4cbb62020-02-28 11:15:47 +01001850 }
Michal Vasko9f96a052020-03-10 09:41:45 +01001851 } else if (*meta) {
1852 for (last = *meta; last->next; last = last->next);
1853 last->next = mt;
Michal Vasko90932a92020-02-12 14:33:03 +01001854 }
1855
1856 /* remove default flags from NP containers */
Michal Vasko4ba6aef2020-07-09 15:44:23 +02001857 while (parent && (parent->schema->nodetype == LYS_CONTAINER) && (parent->flags & LYD_DEFAULT)) {
Michal Vasko90932a92020-02-12 14:33:03 +01001858 parent->flags &= ~LYD_DEFAULT;
1859 parent = (struct lyd_node *)parent->parent;
1860 }
1861
Michal Vasko9f96a052020-03-10 09:41:45 +01001862 if (meta) {
1863 *meta = mt;
Michal Vasko90932a92020-02-12 14:33:03 +01001864 }
1865 return ret;
1866}
1867
Michal Vasko52927e22020-03-16 17:26:14 +01001868LY_ERR
1869ly_create_attr(struct lyd_node *parent, struct ly_attr **attr, const struct ly_ctx *ctx, const char *name,
1870 size_t name_len, const char *value, size_t value_len, int *dynamic, LYD_FORMAT format,
1871 struct ly_prefix *val_prefs, const char *prefix, size_t prefix_len, const char *ns)
1872{
1873 struct ly_attr *at, *last;
1874 struct lyd_node_opaq *opaq;
1875
1876 assert(ctx && (parent || attr) && (!parent || !parent->schema));
1877 assert(name && name_len);
1878 assert((prefix_len && ns) || (!prefix_len && !ns));
1879
1880 if (!value_len) {
1881 value = "";
1882 }
1883
1884 at = calloc(1, sizeof *at);
1885 LY_CHECK_ERR_RET(!at, LOGMEM(ctx), LY_EMEM);
1886 at->parent = (struct lyd_node_opaq *)parent;
1887 at->name = lydict_insert(ctx, name, name_len);
1888 if (dynamic && *dynamic) {
1889 at->value = lydict_insert_zc(ctx, (char *)value);
1890 *dynamic = 0;
1891 } else {
1892 at->value = lydict_insert(ctx, value, value_len);
1893 }
1894
1895 at->format = format;
1896 at->val_prefs = val_prefs;
1897 if (ns) {
1898 at->prefix.pref = lydict_insert(ctx, prefix, prefix_len);
1899 at->prefix.ns = lydict_insert(ctx, ns, 0);
1900 }
1901
1902 /* insert as the last attribute */
1903 if (parent) {
1904 opaq = (struct lyd_node_opaq *)parent;
1905 if (opaq->attr) {
1906 for (last = opaq->attr; last->next; last = last->next);
1907 last->next = at;
1908 } else {
1909 opaq->attr = at;
1910 }
1911 } else if (*attr) {
1912 for (last = *attr; last->next; last = last->next);
1913 last->next = at;
1914 }
1915
1916 if (attr) {
1917 *attr = at;
1918 }
1919 return LY_SUCCESS;
1920}
1921
Radek Krejci084289f2019-07-09 17:35:30 +02001922API const struct lyd_node_term *
Michal Vasko004d3152020-06-11 19:59:22 +02001923lyd_target(const struct ly_path *path, const struct lyd_node *tree)
Radek Krejci084289f2019-07-09 17:35:30 +02001924{
Michal Vasko004d3152020-06-11 19:59:22 +02001925 struct lyd_node *target;
Radek Krejci084289f2019-07-09 17:35:30 +02001926
Michal Vasko004d3152020-06-11 19:59:22 +02001927 if (ly_path_eval(path, tree, &target)) {
1928 return NULL;
Radek Krejci084289f2019-07-09 17:35:30 +02001929 }
1930
Michal Vasko004d3152020-06-11 19:59:22 +02001931 return (struct lyd_node_term *)target;
Radek Krejci084289f2019-07-09 17:35:30 +02001932}
1933
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001934API LY_ERR
Michal Vasko8f359bf2020-07-28 10:41:15 +02001935lyd_compare_single(const struct lyd_node *node1, const struct lyd_node *node2, int options)
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001936{
1937 const struct lyd_node *iter1, *iter2;
1938 struct lyd_node_term *term1, *term2;
1939 struct lyd_node_any *any1, *any2;
Michal Vasko52927e22020-03-16 17:26:14 +01001940 struct lyd_node_opaq *opaq1, *opaq2;
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001941 size_t len1, len2;
Radek Krejci084289f2019-07-09 17:35:30 +02001942
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001943 if (!node1 || !node2) {
1944 if (node1 == node2) {
1945 return LY_SUCCESS;
1946 } else {
1947 return LY_ENOT;
1948 }
1949 }
1950
Michal Vasko52927e22020-03-16 17:26:14 +01001951 if ((LYD_NODE_CTX(node1) != LYD_NODE_CTX(node2)) || (node1->schema != node2->schema)) {
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001952 return LY_ENOT;
1953 }
1954
1955 if (node1->hash != node2->hash) {
1956 return LY_ENOT;
1957 }
Michal Vasko52927e22020-03-16 17:26:14 +01001958 /* 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 +02001959
Michal Vasko52927e22020-03-16 17:26:14 +01001960 if (!node1->schema) {
1961 opaq1 = (struct lyd_node_opaq *)node1;
1962 opaq2 = (struct lyd_node_opaq *)node2;
1963 if ((opaq1->name != opaq2->name) || (opaq1->prefix.ns != opaq2->prefix.ns) || (opaq1->format != opaq2->format)) {
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001964 return LY_ENOT;
1965 }
Michal Vasko52927e22020-03-16 17:26:14 +01001966 switch (opaq1->format) {
1967 case LYD_XML:
1968 if (lyxml_value_compare(opaq1->value, opaq1->val_prefs, opaq2->value, opaq2->val_prefs)) {
1969 return LY_ENOT;
1970 }
1971 break;
1972 case LYD_SCHEMA:
Michal Vasko60ea6352020-06-29 13:39:39 +02001973 case LYD_LYB:
Michal Vasko52927e22020-03-16 17:26:14 +01001974 /* not allowed */
1975 LOGINT(LYD_NODE_CTX(node1));
1976 return LY_EINT;
1977 }
1978 if (options & LYD_COMPARE_FULL_RECURSION) {
1979 iter1 = opaq1->child;
1980 iter2 = opaq2->child;
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001981 goto all_children_compare;
Michal Vasko52927e22020-03-16 17:26:14 +01001982 }
1983 return LY_SUCCESS;
1984 } else {
1985 switch (node1->schema->nodetype) {
1986 case LYS_LEAF:
1987 case LYS_LEAFLIST:
1988 if (options & LYD_COMPARE_DEFAULTS) {
1989 if ((node1->flags & LYD_DEFAULT) != (node2->flags & LYD_DEFAULT)) {
1990 return LY_ENOT;
1991 }
1992 }
1993
Michal Vasko1a2bf2e2020-06-16 13:07:48 +02001994 term1 = (struct lyd_node_term *)node1;
1995 term2 = (struct lyd_node_term *)node2;
1996 if (term1->value.realtype != term2->value.realtype) {
1997 return LY_ENOT;
1998 }
Michal Vasko52927e22020-03-16 17:26:14 +01001999
Michal Vasko1a2bf2e2020-06-16 13:07:48 +02002000 return term1->value.realtype->plugin->compare(&term1->value, &term2->value);
Michal Vasko52927e22020-03-16 17:26:14 +01002001 case LYS_CONTAINER:
2002 if (options & LYD_COMPARE_DEFAULTS) {
2003 if ((node1->flags & LYD_DEFAULT) != (node2->flags & LYD_DEFAULT)) {
2004 return LY_ENOT;
2005 }
2006 }
2007 if (options & LYD_COMPARE_FULL_RECURSION) {
2008 iter1 = ((struct lyd_node_inner*)node1)->child;
2009 iter2 = ((struct lyd_node_inner*)node2)->child;
2010 goto all_children_compare;
Radek Krejci1f05b6a2019-07-18 16:15:06 +02002011 }
2012 return LY_SUCCESS;
Michal Vasko1bf09392020-03-27 12:38:10 +01002013 case LYS_RPC:
Michal Vasko52927e22020-03-16 17:26:14 +01002014 case LYS_ACTION:
2015 if (options & LYD_COMPARE_FULL_RECURSION) {
2016 /* TODO action/RPC
2017 goto all_children_compare;
2018 */
Radek Krejci1f05b6a2019-07-18 16:15:06 +02002019 }
2020 return LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +01002021 case LYS_NOTIF:
2022 if (options & LYD_COMPARE_FULL_RECURSION) {
2023 /* TODO Notification
2024 goto all_children_compare;
2025 */
2026 }
2027 return LY_SUCCESS;
2028 case LYS_LIST:
2029 iter1 = ((struct lyd_node_inner*)node1)->child;
2030 iter2 = ((struct lyd_node_inner*)node2)->child;
2031
2032 if (!(node1->schema->flags & LYS_KEYLESS) && !(options & LYD_COMPARE_FULL_RECURSION)) {
2033 /* lists with keys, their equivalence is based on their keys */
2034 for (struct lysc_node *key = ((struct lysc_node_list*)node1->schema)->child;
Michal Vaskoc57d9a92020-06-23 13:28:27 +02002035 key && (key->flags & LYS_KEY);
Michal Vasko52927e22020-03-16 17:26:14 +01002036 key = key->next) {
Michal Vasko8f359bf2020-07-28 10:41:15 +02002037 if (lyd_compare_single(iter1, iter2, options)) {
Michal Vasko52927e22020-03-16 17:26:14 +01002038 return LY_ENOT;
2039 }
2040 iter1 = iter1->next;
2041 iter2 = iter2->next;
2042 }
2043 } else {
2044 /* lists without keys, their equivalence is based on equivalence of all the children (both direct and indirect) */
2045
2046 all_children_compare:
2047 if (!iter1 && !iter2) {
2048 /* no children, nothing to compare */
2049 return LY_SUCCESS;
2050 }
2051
2052 for (; iter1 && iter2; iter1 = iter1->next, iter2 = iter2->next) {
Michal Vasko8f359bf2020-07-28 10:41:15 +02002053 if (lyd_compare_single(iter1, iter2, options | LYD_COMPARE_FULL_RECURSION)) {
Michal Vasko52927e22020-03-16 17:26:14 +01002054 return LY_ENOT;
2055 }
2056 }
2057 if (iter1 || iter2) {
2058 return LY_ENOT;
2059 }
2060 }
2061 return LY_SUCCESS;
2062 case LYS_ANYXML:
2063 case LYS_ANYDATA:
2064 any1 = (struct lyd_node_any*)node1;
2065 any2 = (struct lyd_node_any*)node2;
2066
2067 if (any1->value_type != any2->value_type) {
2068 return LY_ENOT;
2069 }
2070 switch (any1->value_type) {
2071 case LYD_ANYDATA_DATATREE:
2072 iter1 = any1->value.tree;
2073 iter2 = any2->value.tree;
2074 goto all_children_compare;
2075 case LYD_ANYDATA_STRING:
2076 case LYD_ANYDATA_XML:
2077 case LYD_ANYDATA_JSON:
2078 len1 = strlen(any1->value.str);
2079 len2 = strlen(any2->value.str);
2080 if (len1 != len2 || strcmp(any1->value.str, any2->value.str)) {
2081 return LY_ENOT;
2082 }
2083 return LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +01002084 case LYD_ANYDATA_LYB:
Michal Vasko60ea6352020-06-29 13:39:39 +02002085 len1 = lyd_lyb_data_length(any1->value.mem);
2086 len2 = lyd_lyb_data_length(any2->value.mem);
Michal Vasko52927e22020-03-16 17:26:14 +01002087 if (len1 != len2 || memcmp(any1->value.mem, any2->value.mem, len1)) {
2088 return LY_ENOT;
2089 }
2090 return LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +01002091 }
Radek Krejci1f05b6a2019-07-18 16:15:06 +02002092 }
2093 }
2094
Michal Vasko52927e22020-03-16 17:26:14 +01002095 LOGINT(LYD_NODE_CTX(node1));
Radek Krejci1f05b6a2019-07-18 16:15:06 +02002096 return LY_EINT;
2097}
Radek Krejci22ebdba2019-07-25 13:59:43 +02002098
Michal Vasko21725742020-06-29 11:49:25 +02002099API LY_ERR
Michal Vasko8f359bf2020-07-28 10:41:15 +02002100lyd_compare_siblings(const struct lyd_node *node1, const struct lyd_node *node2, int options)
2101{
2102 for (; node1 && node2; node1 = node1->next, node2 = node2->next) {
2103 LY_CHECK_RET(lyd_compare_single(node1, node2, options));
2104 }
2105
Michal Vasko11a81432020-07-28 16:31:29 +02002106 if (node1 == node2) {
2107 return LY_SUCCESS;
Michal Vasko8f359bf2020-07-28 10:41:15 +02002108 }
Michal Vasko11a81432020-07-28 16:31:29 +02002109 return LY_ENOT;
Michal Vasko8f359bf2020-07-28 10:41:15 +02002110}
2111
2112API LY_ERR
Michal Vasko21725742020-06-29 11:49:25 +02002113lyd_compare_meta(const struct lyd_meta *meta1, const struct lyd_meta *meta2)
2114{
2115 if (!meta1 || !meta2) {
2116 if (meta1 == meta2) {
2117 return LY_SUCCESS;
2118 } else {
2119 return LY_ENOT;
2120 }
2121 }
2122
2123 if ((LYD_NODE_CTX(meta1->parent) != LYD_NODE_CTX(meta2->parent)) || (meta1->annotation != meta2->annotation)) {
2124 return LY_ENOT;
2125 }
2126
2127 if (meta1->value.realtype != meta2->value.realtype) {
2128 return LY_ENOT;
2129 }
2130
2131 return meta1->value.realtype->plugin->compare(&meta1->value, &meta2->value);
2132}
2133
Radek Krejci22ebdba2019-07-25 13:59:43 +02002134/**
Michal Vasko52927e22020-03-16 17:26:14 +01002135 * @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 +02002136 *
2137 * 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 +02002138 *
2139 * @param[in] node Original node to duplicate
2140 * @param[in] parent Parent to insert into, NULL for top-level sibling.
2141 * @param[in,out] first First sibling, NULL if no top-level sibling exist yet. Can be also NULL if @p parent is set.
2142 * @param[in] options Bitmask of options flags, see @ref dupoptions.
2143 * @param[out] dup_p Pointer where the created duplicated node is placed (besides connecting it int @p parent / @p first sibling).
2144 * @return LY_ERR value
Radek Krejci22ebdba2019-07-25 13:59:43 +02002145 */
Michal Vasko52927e22020-03-16 17:26:14 +01002146static LY_ERR
Michal Vasko3a41dff2020-07-15 14:30:28 +02002147lyd_dup_r(const struct lyd_node *node, struct lyd_node *parent, struct lyd_node **first, int options,
2148 struct lyd_node **dup_p)
Radek Krejci22ebdba2019-07-25 13:59:43 +02002149{
Michal Vasko52927e22020-03-16 17:26:14 +01002150 LY_ERR ret;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002151 struct lyd_node *dup = NULL;
Michal Vasko61551fa2020-07-09 15:45:45 +02002152 struct lyd_meta *meta;
2153 struct lyd_node_any *any;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002154 LY_ARRAY_COUNT_TYPE u;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002155
Michal Vasko52927e22020-03-16 17:26:14 +01002156 LY_CHECK_ARG_RET(NULL, node, LY_EINVAL);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002157
Michal Vasko52927e22020-03-16 17:26:14 +01002158 if (!node->schema) {
2159 dup = calloc(1, sizeof(struct lyd_node_opaq));
2160 } else {
2161 switch (node->schema->nodetype) {
Michal Vasko1bf09392020-03-27 12:38:10 +01002162 case LYS_RPC:
Michal Vasko52927e22020-03-16 17:26:14 +01002163 case LYS_ACTION:
2164 case LYS_NOTIF:
2165 case LYS_CONTAINER:
2166 case LYS_LIST:
2167 dup = calloc(1, sizeof(struct lyd_node_inner));
2168 break;
2169 case LYS_LEAF:
2170 case LYS_LEAFLIST:
2171 dup = calloc(1, sizeof(struct lyd_node_term));
2172 break;
2173 case LYS_ANYDATA:
2174 case LYS_ANYXML:
2175 dup = calloc(1, sizeof(struct lyd_node_any));
2176 break;
2177 default:
2178 LOGINT(LYD_NODE_CTX(node));
2179 ret = LY_EINT;
2180 goto error;
2181 }
Radek Krejci22ebdba2019-07-25 13:59:43 +02002182 }
Michal Vasko52927e22020-03-16 17:26:14 +01002183 LY_CHECK_ERR_GOTO(!dup, LOGMEM(LYD_NODE_CTX(node)); ret = LY_EMEM, error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002184
Michal Vaskof6df0a02020-06-16 13:08:34 +02002185 if (options & LYD_DUP_WITH_FLAGS) {
2186 dup->flags = node->flags;
2187 } else {
2188 dup->flags = (node->flags & LYD_DEFAULT) | LYD_NEW;
2189 }
Radek Krejci22ebdba2019-07-25 13:59:43 +02002190 dup->schema = node->schema;
Michal Vasko52927e22020-03-16 17:26:14 +01002191 dup->prev = dup;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002192
Michal Vasko25a32822020-07-09 15:48:22 +02002193 /* duplicate metadata */
2194 if (!(options & LYD_DUP_NO_META)) {
2195 LY_LIST_FOR(node->meta, meta) {
Michal Vasko3a41dff2020-07-15 14:30:28 +02002196 LY_CHECK_GOTO(ret = lyd_dup_meta_single(meta, dup, NULL), error);
Michal Vasko25a32822020-07-09 15:48:22 +02002197 }
2198 }
Radek Krejci22ebdba2019-07-25 13:59:43 +02002199
2200 /* nodetype-specific work */
Michal Vasko52927e22020-03-16 17:26:14 +01002201 if (!dup->schema) {
2202 struct lyd_node_opaq *opaq = (struct lyd_node_opaq *)dup;
2203 struct lyd_node_opaq *orig = (struct lyd_node_opaq *)node;
2204 struct lyd_node *child;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002205
2206 if (options & LYD_DUP_RECURSIVE) {
2207 /* duplicate all the children */
2208 LY_LIST_FOR(orig->child, child) {
Michal Vasko3a41dff2020-07-15 14:30:28 +02002209 LY_CHECK_GOTO(ret = lyd_dup_r(child, dup, NULL, options, NULL), error);
Michal Vasko52927e22020-03-16 17:26:14 +01002210 }
2211 }
2212 opaq->name = lydict_insert(LYD_NODE_CTX(node), orig->name, 0);
2213 opaq->format = orig->format;
2214 if (orig->prefix.pref) {
2215 opaq->prefix.pref = lydict_insert(LYD_NODE_CTX(node), orig->prefix.pref, 0);
2216 }
2217 if (orig->prefix.ns) {
2218 opaq->prefix.ns = lydict_insert(LYD_NODE_CTX(node), orig->prefix.ns, 0);
2219 }
2220 if (orig->val_prefs) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002221 LY_ARRAY_CREATE_GOTO(LYD_NODE_CTX(node), opaq->val_prefs, LY_ARRAY_COUNT(orig->val_prefs), ret, error);
Michal Vasko52927e22020-03-16 17:26:14 +01002222 LY_ARRAY_FOR(orig->val_prefs, u) {
2223 opaq->val_prefs[u].pref = lydict_insert(LYD_NODE_CTX(node), orig->val_prefs[u].pref, 0);
2224 opaq->val_prefs[u].ns = lydict_insert(LYD_NODE_CTX(node), orig->val_prefs[u].ns, 0);
2225 LY_ARRAY_INCREMENT(opaq->val_prefs);
2226 }
2227 }
2228 opaq->value = lydict_insert(LYD_NODE_CTX(node), orig->value, 0);
2229 opaq->ctx = orig->ctx;
2230 } else if (dup->schema->nodetype & LYD_NODE_TERM) {
2231 struct lyd_node_term *term = (struct lyd_node_term *)dup;
2232 struct lyd_node_term *orig = (struct lyd_node_term *)node;
2233
2234 term->hash = orig->hash;
2235 term->value.realtype = orig->value.realtype;
2236 LY_CHECK_ERR_GOTO(term->value.realtype->plugin->duplicate(LYD_NODE_CTX(node), &orig->value, &term->value),
2237 LOGERR(LYD_NODE_CTX(node), LY_EINT, "Value duplication failed."); ret = LY_EINT, error);
2238 } else if (dup->schema->nodetype & LYD_NODE_INNER) {
2239 struct lyd_node_inner *orig = (struct lyd_node_inner *)node;
2240 struct lyd_node *child;
2241
2242 if (options & LYD_DUP_RECURSIVE) {
2243 /* duplicate all the children */
2244 LY_LIST_FOR(orig->child, child) {
Michal Vasko3a41dff2020-07-15 14:30:28 +02002245 LY_CHECK_GOTO(ret = lyd_dup_r(child, dup, NULL, options, NULL), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002246 }
Radek Krejci0fe9b512019-07-26 17:51:05 +02002247 } else if (dup->schema->nodetype == LYS_LIST && !(dup->schema->flags & LYS_KEYLESS)) {
Radek Krejci22ebdba2019-07-25 13:59:43 +02002248 /* always duplicate keys of a list */
Radek Krejci22ebdba2019-07-25 13:59:43 +02002249 child = orig->child;
Michal Vasko52927e22020-03-16 17:26:14 +01002250 for (struct lysc_node *key = ((struct lysc_node_list *)dup->schema)->child;
Michal Vaskoc57d9a92020-06-23 13:28:27 +02002251 key && (key->flags & LYS_KEY);
Radek Krejci0fe9b512019-07-26 17:51:05 +02002252 key = key->next) {
Radek Krejci22ebdba2019-07-25 13:59:43 +02002253 if (!child) {
2254 /* possibly not keys are present in filtered tree */
2255 break;
Radek Krejci0fe9b512019-07-26 17:51:05 +02002256 } else if (child->schema != key) {
2257 /* possibly not all keys are present in filtered tree,
2258 * but there can be also some non-key nodes */
2259 continue;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002260 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02002261 LY_CHECK_GOTO(ret = lyd_dup_r(child, dup, NULL, options, NULL), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002262 child = child->next;
2263 }
2264 }
2265 lyd_hash(dup);
2266 } else if (dup->schema->nodetype & LYD_NODE_ANY) {
Michal Vasko61551fa2020-07-09 15:45:45 +02002267 dup->hash = node->hash;
2268 any = (struct lyd_node_any *)node;
2269 LY_CHECK_GOTO(ret = lyd_any_copy_value(dup, &any->value, any->value_type), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002270 }
2271
Michal Vasko52927e22020-03-16 17:26:14 +01002272 /* insert */
2273 lyd_insert_node(parent, first, dup);
Michal Vasko52927e22020-03-16 17:26:14 +01002274
2275 if (dup_p) {
2276 *dup_p = dup;
2277 }
2278 return LY_SUCCESS;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002279
2280error:
Michal Vasko52927e22020-03-16 17:26:14 +01002281 lyd_free_tree(dup);
2282 return ret;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002283}
2284
Michal Vasko3a41dff2020-07-15 14:30:28 +02002285static LY_ERR
2286lyd_dup_get_local_parent(const struct lyd_node *node, const struct lyd_node_inner *parent, struct lyd_node **dup_parent,
2287 struct lyd_node_inner **local_parent)
Radek Krejci22ebdba2019-07-25 13:59:43 +02002288{
Michal Vasko3a41dff2020-07-15 14:30:28 +02002289 const struct lyd_node_inner *orig_parent, *iter;
2290 int repeat = 1;
2291
2292 *dup_parent = NULL;
2293 *local_parent = NULL;
2294
2295 for (orig_parent = node->parent; repeat && orig_parent; orig_parent = orig_parent->parent) {
2296 if (parent && (parent->schema == orig_parent->schema)) {
2297 /* stop creating parents, connect what we have into the provided parent */
2298 iter = parent;
2299 repeat = 0;
2300 } else {
2301 iter = NULL;
2302 LY_CHECK_RET(lyd_dup_r((struct lyd_node *)orig_parent, NULL, (struct lyd_node **)&iter, 0,
2303 (struct lyd_node **)&iter));
2304 }
2305 if (!*local_parent) {
2306 *local_parent = (struct lyd_node_inner *)iter;
2307 }
2308 if (iter->child) {
2309 /* 1) list - add after keys
2310 * 2) provided parent with some children */
2311 iter->child->prev->next = *dup_parent;
2312 if (*dup_parent) {
2313 (*dup_parent)->prev = iter->child->prev;
2314 iter->child->prev = *dup_parent;
2315 }
2316 } else {
2317 ((struct lyd_node_inner *)iter)->child = *dup_parent;
2318 }
2319 if (*dup_parent) {
2320 (*dup_parent)->parent = (struct lyd_node_inner *)iter;
2321 }
2322 *dup_parent = (struct lyd_node *)iter;
2323 }
2324
2325 if (repeat && parent) {
2326 /* given parent and created parents chain actually do not interconnect */
2327 LOGERR(LYD_NODE_CTX(node), LY_EINVAL,
2328 "Invalid argument parent (%s()) - does not interconnect with the created node's parents chain.", __func__);
2329 return LY_EINVAL;
2330 }
2331
2332 return LY_SUCCESS;
2333}
2334
2335static LY_ERR
2336lyd_dup(const struct lyd_node *node, struct lyd_node_inner *parent, int options, int nosiblings, struct lyd_node **dup)
2337{
2338 LY_ERR rc;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002339 const struct lyd_node *orig; /* original node to be duplicated */
2340 struct lyd_node *first = NULL; /* the first duplicated node, this is returned */
Radek Krejci22ebdba2019-07-25 13:59:43 +02002341 struct lyd_node *top = NULL; /* the most higher created node */
2342 struct lyd_node_inner *local_parent = NULL; /* the direct parent node for the duplicated node(s) */
Radek Krejci22ebdba2019-07-25 13:59:43 +02002343
Michal Vasko3a41dff2020-07-15 14:30:28 +02002344 LY_CHECK_ARG_RET(NULL, node, LY_EINVAL);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002345
2346 if (options & LYD_DUP_WITH_PARENTS) {
Michal Vasko3a41dff2020-07-15 14:30:28 +02002347 LY_CHECK_GOTO(rc = lyd_dup_get_local_parent(node, parent, &top, &local_parent), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002348 } else {
2349 local_parent = parent;
2350 }
2351
Radek Krejci22ebdba2019-07-25 13:59:43 +02002352 LY_LIST_FOR(node, orig) {
Michal Vasko52927e22020-03-16 17:26:14 +01002353 /* if there is no local parent, it will be inserted into first */
Michal Vasko3a41dff2020-07-15 14:30:28 +02002354 LY_CHECK_GOTO(rc = lyd_dup_r(orig, (struct lyd_node *)local_parent, &first, options, first ? NULL : &first), error);
2355 if (nosiblings) {
Radek Krejci22ebdba2019-07-25 13:59:43 +02002356 break;
2357 }
2358 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02002359
2360 /* rehash if needed */
2361 for (; local_parent; local_parent = local_parent->parent) {
2362 if (local_parent->schema->nodetype == LYS_LIST && (local_parent->schema->flags & LYS_KEYLESS)) {
2363 lyd_hash((struct lyd_node *)local_parent);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002364 }
2365 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02002366
2367 if (dup) {
2368 *dup = first;
2369 }
2370 return LY_SUCCESS;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002371
2372error:
2373 if (top) {
2374 lyd_free_tree(top);
2375 } else {
Michal Vaskof03ed032020-03-04 13:31:44 +01002376 lyd_free_siblings(first);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002377 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02002378 return rc;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002379}
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002380
Michal Vasko3a41dff2020-07-15 14:30:28 +02002381API LY_ERR
2382lyd_dup_single(const struct lyd_node *node, struct lyd_node_inner *parent, int options, struct lyd_node **dup)
2383{
2384 return lyd_dup(node, parent, options, 1, dup);
2385}
2386
2387API LY_ERR
2388lyd_dup_siblings(const struct lyd_node *node, struct lyd_node_inner *parent, int options, struct lyd_node **dup)
2389{
2390 return lyd_dup(node, parent, options, 0, dup);
2391}
2392
2393API LY_ERR
2394lyd_dup_meta_single(const struct lyd_meta *meta, struct lyd_node *node, struct lyd_meta **dup)
Michal Vasko25a32822020-07-09 15:48:22 +02002395{
2396 LY_ERR ret;
2397 struct lyd_meta *mt, *last;
2398
Michal Vasko3a41dff2020-07-15 14:30:28 +02002399 LY_CHECK_ARG_RET(NULL, meta, node, LY_EINVAL);
Michal Vasko25a32822020-07-09 15:48:22 +02002400
2401 /* create a copy */
2402 mt = calloc(1, sizeof *mt);
Michal Vasko3a41dff2020-07-15 14:30:28 +02002403 LY_CHECK_ERR_RET(!mt, LOGMEM(LYD_NODE_CTX(node)), LY_EMEM);
Michal Vasko25a32822020-07-09 15:48:22 +02002404 mt->parent = node;
2405 mt->annotation = meta->annotation;
2406 mt->value.realtype = meta->value.realtype;
2407 ret = mt->value.realtype->plugin->duplicate(LYD_NODE_CTX(node), &meta->value, &mt->value);
Michal Vasko3a41dff2020-07-15 14:30:28 +02002408 LY_CHECK_ERR_RET(ret, LOGERR(LYD_NODE_CTX(node), LY_EINT, "Value duplication failed."), ret);
Michal Vasko25a32822020-07-09 15:48:22 +02002409 mt->name = lydict_insert(LYD_NODE_CTX(node), meta->name, 0);
2410
2411 /* insert as the last attribute */
2412 if (node->meta) {
2413 for (last = node->meta; last->next; last = last->next);
2414 last->next = mt;
2415 } else {
2416 node->meta = mt;
2417 }
2418
Michal Vasko3a41dff2020-07-15 14:30:28 +02002419 if (dup) {
2420 *dup = mt;
2421 }
2422 return LY_SUCCESS;
Michal Vasko25a32822020-07-09 15:48:22 +02002423}
2424
Michal Vasko4490d312020-06-16 13:08:55 +02002425/**
2426 * @brief Merge a source sibling into target siblings.
2427 *
2428 * @param[in,out] first_trg First target sibling, is updated if top-level.
2429 * @param[in] parent_trg Target parent.
2430 * @param[in,out] sibling_src Source sibling to merge, set to NULL if spent.
2431 * @param[in] options Merge options.
2432 * @return LY_ERR value.
2433 */
2434static LY_ERR
2435lyd_merge_sibling_r(struct lyd_node **first_trg, struct lyd_node *parent_trg, const struct lyd_node **sibling_src_p,
2436 int options)
2437{
2438 LY_ERR ret;
2439 const struct lyd_node *child_src, *tmp, *sibling_src;
2440 struct lyd_node *match_trg, *dup_src, *next, *elem;
2441 struct lysc_type *type;
Michal Vasko4490d312020-06-16 13:08:55 +02002442
2443 sibling_src = *sibling_src_p;
2444 if (sibling_src->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) {
2445 /* try to find the exact instance */
2446 ret = lyd_find_sibling_first(*first_trg, sibling_src, &match_trg);
2447 } else {
2448 /* try to simply find the node, there cannot be more instances */
2449 ret = lyd_find_sibling_val(*first_trg, sibling_src->schema, NULL, 0, &match_trg);
2450 }
2451
2452 if (!ret) {
2453 /* node found, make sure even value matches for all node types */
Michal Vasko8f359bf2020-07-28 10:41:15 +02002454 if ((match_trg->schema->nodetype == LYS_LEAF) && lyd_compare_single(sibling_src, match_trg, LYD_COMPARE_DEFAULTS)) {
Michal Vasko4490d312020-06-16 13:08:55 +02002455 /* since they are different, they cannot both be default */
2456 assert(!(sibling_src->flags & LYD_DEFAULT) || !(match_trg->flags & LYD_DEFAULT));
2457
Michal Vasko3a41dff2020-07-15 14:30:28 +02002458 /* update value (or only LYD_DEFAULT flag) only if flag set or the source node is not default */
2459 if ((options & LYD_MERGE_DEFAULTS) || !(sibling_src->flags & LYD_DEFAULT)) {
Michal Vasko4490d312020-06-16 13:08:55 +02002460 type = ((struct lysc_node_leaf *)match_trg->schema)->type;
2461 type->plugin->free(LYD_NODE_CTX(match_trg), &((struct lyd_node_term *)match_trg)->value);
2462 LY_CHECK_RET(type->plugin->duplicate(LYD_NODE_CTX(match_trg), &((struct lyd_node_term *)sibling_src)->value,
2463 &((struct lyd_node_term *)match_trg)->value));
2464
2465 /* copy flags and add LYD_NEW */
2466 match_trg->flags = sibling_src->flags | LYD_NEW;
2467 }
Michal Vasko8f359bf2020-07-28 10:41:15 +02002468 } else if ((match_trg->schema->nodetype & LYS_ANYDATA) && lyd_compare_single(sibling_src, match_trg, 0)) {
Michal Vasko4c583e82020-07-17 12:16:14 +02002469 /* update value */
2470 LY_CHECK_RET(lyd_any_copy_value(match_trg, &((struct lyd_node_any *)sibling_src)->value,
2471 ((struct lyd_node_any *)sibling_src)->value_type));
Michal Vasko4490d312020-06-16 13:08:55 +02002472
2473 /* copy flags and add LYD_NEW */
2474 match_trg->flags = sibling_src->flags | LYD_NEW;
Michal Vasko4490d312020-06-16 13:08:55 +02002475 } else {
2476 /* check descendants, recursively */
Michal Vasko5bfd4be2020-06-23 13:26:19 +02002477 LY_LIST_FOR_SAFE(LYD_CHILD(sibling_src), tmp, child_src) {
Michal Vasko4490d312020-06-16 13:08:55 +02002478 LY_CHECK_RET(lyd_merge_sibling_r(lyd_node_children_p(match_trg), match_trg, &child_src, options));
2479 }
2480 }
2481 } else {
2482 /* node not found, merge it */
2483 if (options & LYD_MERGE_DESTRUCT) {
2484 dup_src = (struct lyd_node *)sibling_src;
2485 lyd_unlink_tree(dup_src);
2486 /* spend it */
2487 *sibling_src_p = NULL;
2488 } else {
Michal Vasko3a41dff2020-07-15 14:30:28 +02002489 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 +02002490 }
2491
2492 /* set LYD_NEW for all the new nodes, required for validation */
2493 LYD_TREE_DFS_BEGIN(dup_src, next, elem) {
2494 elem->flags |= LYD_NEW;
2495 LYD_TREE_DFS_END(dup_src, next, elem);
2496 }
2497
2498 lyd_insert_node(parent_trg, first_trg, dup_src);
2499 }
2500
2501 return LY_SUCCESS;
2502}
2503
Michal Vasko3a41dff2020-07-15 14:30:28 +02002504static LY_ERR
2505lyd_merge(struct lyd_node **target, const struct lyd_node *source, int options, int nosiblings)
Michal Vasko4490d312020-06-16 13:08:55 +02002506{
2507 const struct lyd_node *sibling_src, *tmp;
2508 int first;
2509
2510 LY_CHECK_ARG_RET(NULL, target, LY_EINVAL);
2511
2512 if (!source) {
2513 /* nothing to merge */
2514 return LY_SUCCESS;
2515 }
2516
2517 if (lysc_data_parent((*target)->schema) || lysc_data_parent(source->schema)) {
2518 LOGERR(LYD_NODE_CTX(source), LY_EINVAL, "Invalid arguments - can merge only 2 top-level subtrees (%s()).", __func__);
2519 return LY_EINVAL;
2520 }
2521
2522 LY_LIST_FOR_SAFE(source, tmp, sibling_src) {
2523 first = sibling_src == source ? 1 : 0;
2524 LY_CHECK_RET(lyd_merge_sibling_r(target, NULL, &sibling_src, options));
2525 if (first && !sibling_src) {
2526 /* source was spent (unlinked), move to the next node */
2527 source = tmp;
2528 }
2529
Michal Vasko3a41dff2020-07-15 14:30:28 +02002530 if (nosiblings) {
Michal Vasko4490d312020-06-16 13:08:55 +02002531 break;
2532 }
2533 }
2534
2535 if (options & LYD_MERGE_DESTRUCT) {
2536 /* free any leftover source data that were not merged */
2537 lyd_free_siblings((struct lyd_node *)source);
2538 }
2539
2540 return LY_SUCCESS;
2541}
2542
Michal Vasko3a41dff2020-07-15 14:30:28 +02002543API LY_ERR
2544lyd_merge_tree(struct lyd_node **target, const struct lyd_node *source, int options)
2545{
2546 return lyd_merge(target, source, options, 1);
2547}
2548
2549API LY_ERR
2550lyd_merge_siblings(struct lyd_node **target, const struct lyd_node *source, int options)
2551{
2552 return lyd_merge(target, source, options, 0);
2553}
2554
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002555static LY_ERR
2556lyd_path_str_enlarge(char **buffer, size_t *buflen, size_t reqlen, int is_static)
2557{
Michal Vasko14654712020-02-06 08:35:21 +01002558 /* ending \0 */
2559 ++reqlen;
2560
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002561 if (reqlen > *buflen) {
2562 if (is_static) {
2563 return LY_EINCOMPLETE;
2564 }
2565
2566 *buffer = ly_realloc(*buffer, reqlen * sizeof **buffer);
2567 if (!*buffer) {
2568 return LY_EMEM;
2569 }
2570
2571 *buflen = reqlen;
2572 }
2573
2574 return LY_SUCCESS;
2575}
2576
Michal Vaskod59035b2020-07-08 12:00:06 +02002577LY_ERR
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002578lyd_path_list_predicate(const struct lyd_node *node, char **buffer, size_t *buflen, size_t *bufused, int is_static)
2579{
2580 const struct lyd_node *key;
2581 int dynamic = 0;
2582 size_t len;
2583 const char *val;
2584 char quot;
2585 LY_ERR rc;
2586
Michal Vasko5bfd4be2020-06-23 13:26:19 +02002587 for (key = lyd_node_children(node, 0); key && (key->schema->flags & LYS_KEY); key = key->next) {
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002588 val = lyd_value2str((struct lyd_node_term *)key, &dynamic);
2589 len = 1 + strlen(key->schema->name) + 2 + strlen(val) + 2;
2590 rc = lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static);
2591 if (rc != LY_SUCCESS) {
2592 if (dynamic) {
2593 free((char *)val);
2594 }
2595 return rc;
2596 }
2597
2598 quot = '\'';
2599 if (strchr(val, '\'')) {
2600 quot = '"';
2601 }
2602 *bufused += sprintf(*buffer + *bufused, "[%s=%c%s%c]", key->schema->name, quot, val, quot);
2603
2604 if (dynamic) {
2605 free((char *)val);
2606 }
2607 }
2608
2609 return LY_SUCCESS;
2610}
2611
2612/**
2613 * @brief Append leaf-list value predicate to path.
2614 *
2615 * @param[in] node Node to print.
2616 * @param[in,out] buffer Buffer to print to.
2617 * @param[in,out] buflen Current buffer length.
2618 * @param[in,out] bufused Current number of characters used in @p buffer.
2619 * @param[in] is_static Whether buffer is static or can be reallocated.
2620 * @return LY_ERR
2621 */
2622static LY_ERR
2623lyd_path_leaflist_predicate(const struct lyd_node *node, char **buffer, size_t *buflen, size_t *bufused, int is_static)
2624{
2625 int dynamic = 0;
2626 size_t len;
2627 const char *val;
2628 char quot;
2629 LY_ERR rc;
2630
2631 val = lyd_value2str((struct lyd_node_term *)node, &dynamic);
2632 len = 4 + strlen(val) + 2;
2633 rc = lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static);
2634 if (rc != LY_SUCCESS) {
2635 goto cleanup;
2636 }
2637
2638 quot = '\'';
2639 if (strchr(val, '\'')) {
2640 quot = '"';
2641 }
2642 *bufused += sprintf(*buffer + *bufused, "[.=%c%s%c]", quot, val, quot);
2643
2644cleanup:
2645 if (dynamic) {
2646 free((char *)val);
2647 }
2648 return rc;
2649}
2650
2651/**
2652 * @brief Append node position (relative to its other instances) predicate to path.
2653 *
2654 * @param[in] node Node to print.
2655 * @param[in,out] buffer Buffer to print to.
2656 * @param[in,out] buflen Current buffer length.
2657 * @param[in,out] bufused Current number of characters used in @p buffer.
2658 * @param[in] is_static Whether buffer is static or can be reallocated.
2659 * @return LY_ERR
2660 */
2661static LY_ERR
2662lyd_path_position_predicate(const struct lyd_node *node, char **buffer, size_t *buflen, size_t *bufused, int is_static)
2663{
2664 const struct lyd_node *first, *iter;
2665 size_t len;
2666 int pos;
2667 char *val = NULL;
2668 LY_ERR rc;
2669
2670 if (node->parent) {
2671 first = node->parent->child;
2672 } else {
2673 for (first = node; node->prev->next; node = node->prev);
2674 }
2675 pos = 1;
2676 for (iter = first; iter != node; iter = iter->next) {
2677 if (iter->schema == node->schema) {
2678 ++pos;
2679 }
2680 }
2681 if (asprintf(&val, "%d", pos) == -1) {
2682 return LY_EMEM;
2683 }
2684
2685 len = 1 + strlen(val) + 1;
2686 rc = lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static);
2687 if (rc != LY_SUCCESS) {
2688 goto cleanup;
2689 }
2690
2691 *bufused += sprintf(*buffer + *bufused, "[%s]", val);
2692
2693cleanup:
2694 free(val);
2695 return rc;
2696}
2697
2698API char *
2699lyd_path(const struct lyd_node *node, LYD_PATH_TYPE pathtype, char *buffer, size_t buflen)
2700{
Michal Vasko14654712020-02-06 08:35:21 +01002701 int is_static = 0, i, depth;
2702 size_t bufused = 0, len;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002703 const struct lyd_node *iter;
2704 const struct lys_module *mod;
2705 LY_ERR rc;
2706
2707 LY_CHECK_ARG_RET(NULL, node, NULL);
2708 if (buffer) {
2709 LY_CHECK_ARG_RET(node->schema->module->ctx, buflen > 1, NULL);
2710 is_static = 1;
Michal Vasko14654712020-02-06 08:35:21 +01002711 } else {
2712 buflen = 0;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002713 }
2714
2715 switch (pathtype) {
Michal Vasko14654712020-02-06 08:35:21 +01002716 case LYD_PATH_LOG:
2717 depth = 1;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002718 for (iter = node; iter->parent; iter = (const struct lyd_node *)iter->parent) {
2719 ++depth;
2720 }
2721
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002722 goto iter_print;
Michal Vasko14654712020-02-06 08:35:21 +01002723 while (depth) {
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002724 /* find the right node */
Michal Vasko14654712020-02-06 08:35:21 +01002725 for (iter = node, i = 1; i < depth; iter = (const struct lyd_node *)iter->parent, ++i);
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002726iter_print:
2727 /* print prefix and name */
2728 mod = NULL;
2729 if (!iter->parent || (iter->schema->module != iter->parent->schema->module)) {
2730 mod = iter->schema->module;
2731 }
2732
2733 /* realloc string */
2734 len = 1 + (mod ? strlen(mod->name) + 1 : 0) + strlen(iter->schema->name);
2735 rc = lyd_path_str_enlarge(&buffer, &buflen, bufused + len, is_static);
2736 if (rc != LY_SUCCESS) {
2737 break;
2738 }
2739
2740 /* print next node */
2741 bufused += sprintf(buffer + bufused, "/%s%s%s", mod ? mod->name : "", mod ? ":" : "", iter->schema->name);
2742
2743 switch (iter->schema->nodetype) {
2744 case LYS_LIST:
2745 if (iter->schema->flags & LYS_KEYLESS) {
2746 /* print its position */
2747 rc = lyd_path_position_predicate(iter, &buffer, &buflen, &bufused, is_static);
2748 } else {
2749 /* print all list keys in predicates */
2750 rc = lyd_path_list_predicate(iter, &buffer, &buflen, &bufused, is_static);
2751 }
2752 break;
2753 case LYS_LEAFLIST:
2754 if (iter->schema->flags & LYS_CONFIG_W) {
2755 /* print leaf-list value */
2756 rc = lyd_path_leaflist_predicate(iter, &buffer, &buflen, &bufused, is_static);
2757 } else {
2758 /* print its position */
2759 rc = lyd_path_position_predicate(iter, &buffer, &buflen, &bufused, is_static);
2760 }
2761 break;
2762 default:
2763 /* nothing to print more */
2764 rc = LY_SUCCESS;
2765 break;
2766 }
2767 if (rc != LY_SUCCESS) {
2768 break;
2769 }
2770
Michal Vasko14654712020-02-06 08:35:21 +01002771 --depth;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002772 }
2773 break;
2774 }
2775
2776 return buffer;
2777}
Michal Vaskoe444f752020-02-10 12:20:06 +01002778
Michal Vasko25a32822020-07-09 15:48:22 +02002779API struct lyd_meta *
2780lyd_find_meta(const struct lyd_meta *first, const struct lys_module *module, const char *name)
2781{
2782 struct lyd_meta *ret = NULL;
2783 const struct ly_ctx *ctx;
2784 const char *prefix, *tmp;
2785 char *str;
2786 size_t pref_len, name_len;
2787
2788 LY_CHECK_ARG_RET(NULL, module || strchr(name, ':'), name, NULL);
2789
2790 if (!first) {
2791 return NULL;
2792 }
2793
2794 ctx = first->annotation->module->ctx;
2795
2796 /* parse the name */
2797 tmp = name;
2798 if (ly_parse_nodeid(&tmp, &prefix, &pref_len, &name, &name_len) || tmp[0]) {
2799 LOGERR(ctx, LY_EINVAL, "Metadata name \"%s\" is not valid.", name);
2800 return NULL;
2801 }
2802
2803 /* find the module */
2804 if (prefix) {
2805 str = strndup(prefix, pref_len);
2806 module = ly_ctx_get_module_latest(ctx, str);
2807 free(str);
2808 LY_CHECK_ERR_RET(!module, LOGERR(ctx, LY_EINVAL, "Module \"%.*s\" not found.", pref_len, prefix), NULL);
2809 }
2810
2811 /* find the metadata */
2812 LY_LIST_FOR(first, first) {
2813 if ((first->annotation->module == module) && !strcmp(first->name, name)) {
2814 ret = (struct lyd_meta *)first;
2815 break;
2816 }
2817 }
2818
2819 return ret;
2820}
2821
Michal Vasko9b368d32020-02-14 13:53:31 +01002822API LY_ERR
Michal Vaskoe444f752020-02-10 12:20:06 +01002823lyd_find_sibling_first(const struct lyd_node *siblings, const struct lyd_node *target, struct lyd_node **match)
2824{
2825 struct lyd_node **match_p;
2826 struct lyd_node_inner *parent;
2827
Michal Vaskof03ed032020-03-04 13:31:44 +01002828 LY_CHECK_ARG_RET(NULL, target, LY_EINVAL);
Michal Vaskoe444f752020-02-10 12:20:06 +01002829
Michal Vasko62ed12d2020-05-21 10:08:25 +02002830 if (!siblings || (lysc_data_parent(siblings->schema) != lysc_data_parent(target->schema))) {
2831 /* no data or schema mismatch */
Michal Vasko9b368d32020-02-14 13:53:31 +01002832 if (match) {
2833 *match = NULL;
2834 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002835 return LY_ENOTFOUND;
2836 }
2837
2838 /* find first sibling */
2839 if (siblings->parent) {
2840 siblings = siblings->parent->child;
2841 } else {
2842 while (siblings->prev->next) {
2843 siblings = siblings->prev;
2844 }
2845 }
2846
2847 parent = (struct lyd_node_inner *)siblings->parent;
2848 if (parent && parent->children_ht) {
2849 assert(target->hash);
2850
2851 /* find by hash */
2852 if (!lyht_find(parent->children_ht, &target, target->hash, (void **)&match_p)) {
Michal Vaskoda859032020-07-14 12:20:14 +02002853 /* check even value when needed */
Michal Vasko8f359bf2020-07-28 10:41:15 +02002854 if (!(target->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) || !lyd_compare_single(target, *match_p, 0)) {
Michal Vaskoda859032020-07-14 12:20:14 +02002855 siblings = *match_p;
2856 } else {
2857 siblings = NULL;
2858 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002859 } else {
2860 /* not found */
2861 siblings = NULL;
2862 }
2863 } else {
2864 /* no children hash table */
2865 for (; siblings; siblings = siblings->next) {
Michal Vasko8f359bf2020-07-28 10:41:15 +02002866 if (!lyd_compare_single(siblings, target, 0)) {
Michal Vaskoe444f752020-02-10 12:20:06 +01002867 break;
2868 }
2869 }
2870 }
2871
2872 if (!siblings) {
Michal Vasko9b368d32020-02-14 13:53:31 +01002873 if (match) {
2874 *match = NULL;
2875 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002876 return LY_ENOTFOUND;
2877 }
2878
Michal Vasko9b368d32020-02-14 13:53:31 +01002879 if (match) {
2880 *match = (struct lyd_node *)siblings;
2881 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002882 return LY_SUCCESS;
2883}
2884
Michal Vasko90932a92020-02-12 14:33:03 +01002885static int
2886lyd_hash_table_schema_val_equal(void *val1_p, void *val2_p, int UNUSED(mod), void *UNUSED(cb_data))
2887{
2888 struct lysc_node *val1;
2889 struct lyd_node *val2;
2890
2891 val1 = *((struct lysc_node **)val1_p);
2892 val2 = *((struct lyd_node **)val2_p);
2893
Michal Vasko90932a92020-02-12 14:33:03 +01002894 if (val1 == val2->schema) {
2895 /* schema match is enough */
2896 return 1;
2897 } else {
2898 return 0;
2899 }
2900}
2901
2902static LY_ERR
2903lyd_find_sibling_schema(const struct lyd_node *siblings, const struct lysc_node *schema, struct lyd_node **match)
2904{
2905 struct lyd_node **match_p;
2906 struct lyd_node_inner *parent;
2907 uint32_t hash;
2908 values_equal_cb ht_cb;
2909
Michal Vaskob104f112020-07-17 09:54:54 +02002910 assert(siblings && schema);
Michal Vasko90932a92020-02-12 14:33:03 +01002911
2912 parent = (struct lyd_node_inner *)siblings->parent;
2913 if (parent && parent->children_ht) {
2914 /* calculate our hash */
2915 hash = dict_hash_multi(0, schema->module->name, strlen(schema->module->name));
2916 hash = dict_hash_multi(hash, schema->name, strlen(schema->name));
2917 hash = dict_hash_multi(hash, NULL, 0);
2918
2919 /* use special hash table function */
2920 ht_cb = lyht_set_cb(parent->children_ht, lyd_hash_table_schema_val_equal);
2921
2922 /* find by hash */
2923 if (!lyht_find(parent->children_ht, &schema, hash, (void **)&match_p)) {
2924 siblings = *match_p;
2925 } else {
2926 /* not found */
2927 siblings = NULL;
2928 }
2929
2930 /* set the original hash table compare function back */
2931 lyht_set_cb(parent->children_ht, ht_cb);
2932 } else {
Michal Vaskob104f112020-07-17 09:54:54 +02002933 /* find first sibling */
2934 if (siblings->parent) {
2935 siblings = siblings->parent->child;
2936 } else {
2937 while (siblings->prev->next) {
2938 siblings = siblings->prev;
2939 }
2940 }
2941
2942 /* search manually without hashes */
Michal Vasko90932a92020-02-12 14:33:03 +01002943 for (; siblings; siblings = siblings->next) {
2944 if (siblings->schema == schema) {
2945 /* schema match is enough */
2946 break;
2947 }
2948 }
2949 }
2950
2951 if (!siblings) {
Michal Vasko9b368d32020-02-14 13:53:31 +01002952 if (match) {
2953 *match = NULL;
2954 }
Michal Vasko90932a92020-02-12 14:33:03 +01002955 return LY_ENOTFOUND;
2956 }
2957
Michal Vasko9b368d32020-02-14 13:53:31 +01002958 if (match) {
2959 *match = (struct lyd_node *)siblings;
2960 }
Michal Vasko90932a92020-02-12 14:33:03 +01002961 return LY_SUCCESS;
2962}
2963
Michal Vaskoe444f752020-02-10 12:20:06 +01002964API LY_ERR
2965lyd_find_sibling_val(const struct lyd_node *siblings, const struct lysc_node *schema, const char *key_or_value,
2966 size_t val_len, struct lyd_node **match)
2967{
2968 LY_ERR rc;
2969 struct lyd_node *target = NULL;
2970
Michal Vasko4c583e82020-07-17 12:16:14 +02002971 LY_CHECK_ARG_RET(NULL, schema, !(schema->nodetype & (LYS_CHOICE | LYS_CASE)), LY_EINVAL);
Michal Vaskoe444f752020-02-10 12:20:06 +01002972
Michal Vasko62ed12d2020-05-21 10:08:25 +02002973 if (!siblings || (lysc_data_parent(siblings->schema) != lysc_data_parent(schema))) {
2974 /* no data or schema mismatch */
Michal Vasko9b368d32020-02-14 13:53:31 +01002975 if (match) {
2976 *match = NULL;
2977 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002978 return LY_ENOTFOUND;
2979 }
2980
Michal Vaskof03ed032020-03-04 13:31:44 +01002981 if (key_or_value && !val_len) {
2982 val_len = strlen(key_or_value);
2983 }
2984
Michal Vaskob104f112020-07-17 09:54:54 +02002985 if ((schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) && key_or_value) {
2986 /* create a data node and find the instance */
2987 if (schema->nodetype == LYS_LEAFLIST) {
2988 /* target used attributes: schema, hash, value */
2989 rc = lyd_create_term(schema, key_or_value, val_len, NULL, lydjson_resolve_prefix, NULL, LYD_JSON, &target);
2990 LY_CHECK_RET(rc && (rc != LY_EINCOMPLETE), rc);
2991 } else {
Michal Vasko90932a92020-02-12 14:33:03 +01002992 /* target used attributes: schema, hash, child (all keys) */
Michal Vasko004d3152020-06-11 19:59:22 +02002993 LY_CHECK_RET(lyd_create_list2(schema, key_or_value, val_len, &target));
Michal Vasko90932a92020-02-12 14:33:03 +01002994 }
2995
2996 /* find it */
2997 rc = lyd_find_sibling_first(siblings, target, match);
Michal Vaskob104f112020-07-17 09:54:54 +02002998 } else {
2999 /* find the first schema node instance */
3000 rc = lyd_find_sibling_schema(siblings, schema, match);
Michal Vaskoe444f752020-02-10 12:20:06 +01003001 }
3002
Michal Vaskoe444f752020-02-10 12:20:06 +01003003 lyd_free_tree(target);
3004 return rc;
3005}
Michal Vaskoccc02342020-05-21 10:09:21 +02003006
3007API LY_ERR
3008lyd_find_xpath(const struct lyd_node *ctx_node, const char *xpath, struct ly_set **set)
3009{
3010 LY_ERR ret = LY_SUCCESS;
3011 struct lyxp_set xp_set;
3012 struct lyxp_expr *exp;
3013 uint32_t i;
3014
3015 LY_CHECK_ARG_RET(NULL, ctx_node, xpath, set, LY_EINVAL);
3016
3017 memset(&xp_set, 0, sizeof xp_set);
3018
3019 /* compile expression */
Michal Vasko004d3152020-06-11 19:59:22 +02003020 exp = lyxp_expr_parse((struct ly_ctx *)LYD_NODE_CTX(ctx_node), xpath, 0, 1);
Michal Vaskoccc02342020-05-21 10:09:21 +02003021 LY_CHECK_ERR_GOTO(!exp, ret = LY_EINVAL, cleanup);
3022
3023 /* evaluate expression */
3024 ret = lyxp_eval(exp, LYD_JSON, ctx_node->schema->module, ctx_node, LYXP_NODE_ELEM, ctx_node, &xp_set, 0);
3025 LY_CHECK_GOTO(ret, cleanup);
3026
3027 /* allocate return set */
3028 *set = ly_set_new();
3029 LY_CHECK_ERR_GOTO(!*set, LOGMEM(LYD_NODE_CTX(ctx_node)); ret = LY_EMEM, cleanup);
3030
3031 /* transform into ly_set */
3032 if (xp_set.type == LYXP_SET_NODE_SET) {
3033 /* allocate memory for all the elements once (even though not all items must be elements but most likely will be) */
3034 (*set)->objs = malloc(xp_set.used * sizeof *(*set)->objs);
3035 LY_CHECK_ERR_GOTO(!(*set)->objs, LOGMEM(LYD_NODE_CTX(ctx_node)); ret = LY_EMEM, cleanup);
3036 (*set)->size = xp_set.used;
3037
3038 for (i = 0; i < xp_set.used; ++i) {
3039 if (xp_set.val.nodes[i].type == LYXP_NODE_ELEM) {
3040 ly_set_add(*set, xp_set.val.nodes[i].node, LY_SET_OPT_USEASLIST);
3041 }
3042 }
3043 }
3044
3045cleanup:
Michal Vasko0691d522020-05-21 13:21:47 +02003046 lyxp_set_free_content(&xp_set);
Michal Vaskoccc02342020-05-21 10:09:21 +02003047 lyxp_expr_free((struct ly_ctx *)LYD_NODE_CTX(ctx_node), exp);
3048 return ret;
3049}