blob: 9dde20ee81a891d7a838ce3281754e7083a5f1e0 [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 */
886 if (lyd_compare(node, new_any, 0)) {
887 /* 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
1935lyd_compare(const struct lyd_node *node1, const struct lyd_node *node2, int options)
1936{
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) {
2037 if (lyd_compare(iter1, iter2, options)) {
2038 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) {
2053 if (lyd_compare(iter1, iter2, options | LYD_COMPARE_FULL_RECURSION)) {
2054 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
2100lyd_compare_meta(const struct lyd_meta *meta1, const struct lyd_meta *meta2)
2101{
2102 if (!meta1 || !meta2) {
2103 if (meta1 == meta2) {
2104 return LY_SUCCESS;
2105 } else {
2106 return LY_ENOT;
2107 }
2108 }
2109
2110 if ((LYD_NODE_CTX(meta1->parent) != LYD_NODE_CTX(meta2->parent)) || (meta1->annotation != meta2->annotation)) {
2111 return LY_ENOT;
2112 }
2113
2114 if (meta1->value.realtype != meta2->value.realtype) {
2115 return LY_ENOT;
2116 }
2117
2118 return meta1->value.realtype->plugin->compare(&meta1->value, &meta2->value);
2119}
2120
Radek Krejci22ebdba2019-07-25 13:59:43 +02002121/**
Michal Vasko52927e22020-03-16 17:26:14 +01002122 * @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 +02002123 *
2124 * 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 +02002125 *
2126 * @param[in] node Original node to duplicate
2127 * @param[in] parent Parent to insert into, NULL for top-level sibling.
2128 * @param[in,out] first First sibling, NULL if no top-level sibling exist yet. Can be also NULL if @p parent is set.
2129 * @param[in] options Bitmask of options flags, see @ref dupoptions.
2130 * @param[out] dup_p Pointer where the created duplicated node is placed (besides connecting it int @p parent / @p first sibling).
2131 * @return LY_ERR value
Radek Krejci22ebdba2019-07-25 13:59:43 +02002132 */
Michal Vasko52927e22020-03-16 17:26:14 +01002133static LY_ERR
Michal Vasko3a41dff2020-07-15 14:30:28 +02002134lyd_dup_r(const struct lyd_node *node, struct lyd_node *parent, struct lyd_node **first, int options,
2135 struct lyd_node **dup_p)
Radek Krejci22ebdba2019-07-25 13:59:43 +02002136{
Michal Vasko52927e22020-03-16 17:26:14 +01002137 LY_ERR ret;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002138 struct lyd_node *dup = NULL;
Michal Vasko61551fa2020-07-09 15:45:45 +02002139 struct lyd_meta *meta;
2140 struct lyd_node_any *any;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002141 LY_ARRAY_COUNT_TYPE u;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002142
Michal Vasko52927e22020-03-16 17:26:14 +01002143 LY_CHECK_ARG_RET(NULL, node, LY_EINVAL);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002144
Michal Vasko52927e22020-03-16 17:26:14 +01002145 if (!node->schema) {
2146 dup = calloc(1, sizeof(struct lyd_node_opaq));
2147 } else {
2148 switch (node->schema->nodetype) {
Michal Vasko1bf09392020-03-27 12:38:10 +01002149 case LYS_RPC:
Michal Vasko52927e22020-03-16 17:26:14 +01002150 case LYS_ACTION:
2151 case LYS_NOTIF:
2152 case LYS_CONTAINER:
2153 case LYS_LIST:
2154 dup = calloc(1, sizeof(struct lyd_node_inner));
2155 break;
2156 case LYS_LEAF:
2157 case LYS_LEAFLIST:
2158 dup = calloc(1, sizeof(struct lyd_node_term));
2159 break;
2160 case LYS_ANYDATA:
2161 case LYS_ANYXML:
2162 dup = calloc(1, sizeof(struct lyd_node_any));
2163 break;
2164 default:
2165 LOGINT(LYD_NODE_CTX(node));
2166 ret = LY_EINT;
2167 goto error;
2168 }
Radek Krejci22ebdba2019-07-25 13:59:43 +02002169 }
Michal Vasko52927e22020-03-16 17:26:14 +01002170 LY_CHECK_ERR_GOTO(!dup, LOGMEM(LYD_NODE_CTX(node)); ret = LY_EMEM, error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002171
Michal Vaskof6df0a02020-06-16 13:08:34 +02002172 if (options & LYD_DUP_WITH_FLAGS) {
2173 dup->flags = node->flags;
2174 } else {
2175 dup->flags = (node->flags & LYD_DEFAULT) | LYD_NEW;
2176 }
Radek Krejci22ebdba2019-07-25 13:59:43 +02002177 dup->schema = node->schema;
Michal Vasko52927e22020-03-16 17:26:14 +01002178 dup->prev = dup;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002179
Michal Vasko25a32822020-07-09 15:48:22 +02002180 /* duplicate metadata */
2181 if (!(options & LYD_DUP_NO_META)) {
2182 LY_LIST_FOR(node->meta, meta) {
Michal Vasko3a41dff2020-07-15 14:30:28 +02002183 LY_CHECK_GOTO(ret = lyd_dup_meta_single(meta, dup, NULL), error);
Michal Vasko25a32822020-07-09 15:48:22 +02002184 }
2185 }
Radek Krejci22ebdba2019-07-25 13:59:43 +02002186
2187 /* nodetype-specific work */
Michal Vasko52927e22020-03-16 17:26:14 +01002188 if (!dup->schema) {
2189 struct lyd_node_opaq *opaq = (struct lyd_node_opaq *)dup;
2190 struct lyd_node_opaq *orig = (struct lyd_node_opaq *)node;
2191 struct lyd_node *child;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002192
2193 if (options & LYD_DUP_RECURSIVE) {
2194 /* duplicate all the children */
2195 LY_LIST_FOR(orig->child, child) {
Michal Vasko3a41dff2020-07-15 14:30:28 +02002196 LY_CHECK_GOTO(ret = lyd_dup_r(child, dup, NULL, options, NULL), error);
Michal Vasko52927e22020-03-16 17:26:14 +01002197 }
2198 }
2199 opaq->name = lydict_insert(LYD_NODE_CTX(node), orig->name, 0);
2200 opaq->format = orig->format;
2201 if (orig->prefix.pref) {
2202 opaq->prefix.pref = lydict_insert(LYD_NODE_CTX(node), orig->prefix.pref, 0);
2203 }
2204 if (orig->prefix.ns) {
2205 opaq->prefix.ns = lydict_insert(LYD_NODE_CTX(node), orig->prefix.ns, 0);
2206 }
2207 if (orig->val_prefs) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002208 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 +01002209 LY_ARRAY_FOR(orig->val_prefs, u) {
2210 opaq->val_prefs[u].pref = lydict_insert(LYD_NODE_CTX(node), orig->val_prefs[u].pref, 0);
2211 opaq->val_prefs[u].ns = lydict_insert(LYD_NODE_CTX(node), orig->val_prefs[u].ns, 0);
2212 LY_ARRAY_INCREMENT(opaq->val_prefs);
2213 }
2214 }
2215 opaq->value = lydict_insert(LYD_NODE_CTX(node), orig->value, 0);
2216 opaq->ctx = orig->ctx;
2217 } else if (dup->schema->nodetype & LYD_NODE_TERM) {
2218 struct lyd_node_term *term = (struct lyd_node_term *)dup;
2219 struct lyd_node_term *orig = (struct lyd_node_term *)node;
2220
2221 term->hash = orig->hash;
2222 term->value.realtype = orig->value.realtype;
2223 LY_CHECK_ERR_GOTO(term->value.realtype->plugin->duplicate(LYD_NODE_CTX(node), &orig->value, &term->value),
2224 LOGERR(LYD_NODE_CTX(node), LY_EINT, "Value duplication failed."); ret = LY_EINT, error);
2225 } else if (dup->schema->nodetype & LYD_NODE_INNER) {
2226 struct lyd_node_inner *orig = (struct lyd_node_inner *)node;
2227 struct lyd_node *child;
2228
2229 if (options & LYD_DUP_RECURSIVE) {
2230 /* duplicate all the children */
2231 LY_LIST_FOR(orig->child, child) {
Michal Vasko3a41dff2020-07-15 14:30:28 +02002232 LY_CHECK_GOTO(ret = lyd_dup_r(child, dup, NULL, options, NULL), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002233 }
Radek Krejci0fe9b512019-07-26 17:51:05 +02002234 } else if (dup->schema->nodetype == LYS_LIST && !(dup->schema->flags & LYS_KEYLESS)) {
Radek Krejci22ebdba2019-07-25 13:59:43 +02002235 /* always duplicate keys of a list */
Radek Krejci22ebdba2019-07-25 13:59:43 +02002236 child = orig->child;
Michal Vasko52927e22020-03-16 17:26:14 +01002237 for (struct lysc_node *key = ((struct lysc_node_list *)dup->schema)->child;
Michal Vaskoc57d9a92020-06-23 13:28:27 +02002238 key && (key->flags & LYS_KEY);
Radek Krejci0fe9b512019-07-26 17:51:05 +02002239 key = key->next) {
Radek Krejci22ebdba2019-07-25 13:59:43 +02002240 if (!child) {
2241 /* possibly not keys are present in filtered tree */
2242 break;
Radek Krejci0fe9b512019-07-26 17:51:05 +02002243 } else if (child->schema != key) {
2244 /* possibly not all keys are present in filtered tree,
2245 * but there can be also some non-key nodes */
2246 continue;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002247 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02002248 LY_CHECK_GOTO(ret = lyd_dup_r(child, dup, NULL, options, NULL), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002249 child = child->next;
2250 }
2251 }
2252 lyd_hash(dup);
2253 } else if (dup->schema->nodetype & LYD_NODE_ANY) {
Michal Vasko61551fa2020-07-09 15:45:45 +02002254 dup->hash = node->hash;
2255 any = (struct lyd_node_any *)node;
2256 LY_CHECK_GOTO(ret = lyd_any_copy_value(dup, &any->value, any->value_type), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002257 }
2258
Michal Vasko52927e22020-03-16 17:26:14 +01002259 /* insert */
2260 lyd_insert_node(parent, first, dup);
Michal Vasko52927e22020-03-16 17:26:14 +01002261
2262 if (dup_p) {
2263 *dup_p = dup;
2264 }
2265 return LY_SUCCESS;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002266
2267error:
Michal Vasko52927e22020-03-16 17:26:14 +01002268 lyd_free_tree(dup);
2269 return ret;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002270}
2271
Michal Vasko3a41dff2020-07-15 14:30:28 +02002272static LY_ERR
2273lyd_dup_get_local_parent(const struct lyd_node *node, const struct lyd_node_inner *parent, struct lyd_node **dup_parent,
2274 struct lyd_node_inner **local_parent)
Radek Krejci22ebdba2019-07-25 13:59:43 +02002275{
Michal Vasko3a41dff2020-07-15 14:30:28 +02002276 const struct lyd_node_inner *orig_parent, *iter;
2277 int repeat = 1;
2278
2279 *dup_parent = NULL;
2280 *local_parent = NULL;
2281
2282 for (orig_parent = node->parent; repeat && orig_parent; orig_parent = orig_parent->parent) {
2283 if (parent && (parent->schema == orig_parent->schema)) {
2284 /* stop creating parents, connect what we have into the provided parent */
2285 iter = parent;
2286 repeat = 0;
2287 } else {
2288 iter = NULL;
2289 LY_CHECK_RET(lyd_dup_r((struct lyd_node *)orig_parent, NULL, (struct lyd_node **)&iter, 0,
2290 (struct lyd_node **)&iter));
2291 }
2292 if (!*local_parent) {
2293 *local_parent = (struct lyd_node_inner *)iter;
2294 }
2295 if (iter->child) {
2296 /* 1) list - add after keys
2297 * 2) provided parent with some children */
2298 iter->child->prev->next = *dup_parent;
2299 if (*dup_parent) {
2300 (*dup_parent)->prev = iter->child->prev;
2301 iter->child->prev = *dup_parent;
2302 }
2303 } else {
2304 ((struct lyd_node_inner *)iter)->child = *dup_parent;
2305 }
2306 if (*dup_parent) {
2307 (*dup_parent)->parent = (struct lyd_node_inner *)iter;
2308 }
2309 *dup_parent = (struct lyd_node *)iter;
2310 }
2311
2312 if (repeat && parent) {
2313 /* given parent and created parents chain actually do not interconnect */
2314 LOGERR(LYD_NODE_CTX(node), LY_EINVAL,
2315 "Invalid argument parent (%s()) - does not interconnect with the created node's parents chain.", __func__);
2316 return LY_EINVAL;
2317 }
2318
2319 return LY_SUCCESS;
2320}
2321
2322static LY_ERR
2323lyd_dup(const struct lyd_node *node, struct lyd_node_inner *parent, int options, int nosiblings, struct lyd_node **dup)
2324{
2325 LY_ERR rc;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002326 const struct lyd_node *orig; /* original node to be duplicated */
2327 struct lyd_node *first = NULL; /* the first duplicated node, this is returned */
Radek Krejci22ebdba2019-07-25 13:59:43 +02002328 struct lyd_node *top = NULL; /* the most higher created node */
2329 struct lyd_node_inner *local_parent = NULL; /* the direct parent node for the duplicated node(s) */
Radek Krejci22ebdba2019-07-25 13:59:43 +02002330
Michal Vasko3a41dff2020-07-15 14:30:28 +02002331 LY_CHECK_ARG_RET(NULL, node, LY_EINVAL);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002332
2333 if (options & LYD_DUP_WITH_PARENTS) {
Michal Vasko3a41dff2020-07-15 14:30:28 +02002334 LY_CHECK_GOTO(rc = lyd_dup_get_local_parent(node, parent, &top, &local_parent), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002335 } else {
2336 local_parent = parent;
2337 }
2338
Radek Krejci22ebdba2019-07-25 13:59:43 +02002339 LY_LIST_FOR(node, orig) {
Michal Vasko52927e22020-03-16 17:26:14 +01002340 /* if there is no local parent, it will be inserted into first */
Michal Vasko3a41dff2020-07-15 14:30:28 +02002341 LY_CHECK_GOTO(rc = lyd_dup_r(orig, (struct lyd_node *)local_parent, &first, options, first ? NULL : &first), error);
2342 if (nosiblings) {
Radek Krejci22ebdba2019-07-25 13:59:43 +02002343 break;
2344 }
2345 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02002346
2347 /* rehash if needed */
2348 for (; local_parent; local_parent = local_parent->parent) {
2349 if (local_parent->schema->nodetype == LYS_LIST && (local_parent->schema->flags & LYS_KEYLESS)) {
2350 lyd_hash((struct lyd_node *)local_parent);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002351 }
2352 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02002353
2354 if (dup) {
2355 *dup = first;
2356 }
2357 return LY_SUCCESS;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002358
2359error:
2360 if (top) {
2361 lyd_free_tree(top);
2362 } else {
Michal Vaskof03ed032020-03-04 13:31:44 +01002363 lyd_free_siblings(first);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002364 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02002365 return rc;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002366}
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002367
Michal Vasko3a41dff2020-07-15 14:30:28 +02002368API LY_ERR
2369lyd_dup_single(const struct lyd_node *node, struct lyd_node_inner *parent, int options, struct lyd_node **dup)
2370{
2371 return lyd_dup(node, parent, options, 1, dup);
2372}
2373
2374API LY_ERR
2375lyd_dup_siblings(const struct lyd_node *node, struct lyd_node_inner *parent, int options, struct lyd_node **dup)
2376{
2377 return lyd_dup(node, parent, options, 0, dup);
2378}
2379
2380API LY_ERR
2381lyd_dup_meta_single(const struct lyd_meta *meta, struct lyd_node *node, struct lyd_meta **dup)
Michal Vasko25a32822020-07-09 15:48:22 +02002382{
2383 LY_ERR ret;
2384 struct lyd_meta *mt, *last;
2385
Michal Vasko3a41dff2020-07-15 14:30:28 +02002386 LY_CHECK_ARG_RET(NULL, meta, node, LY_EINVAL);
Michal Vasko25a32822020-07-09 15:48:22 +02002387
2388 /* create a copy */
2389 mt = calloc(1, sizeof *mt);
Michal Vasko3a41dff2020-07-15 14:30:28 +02002390 LY_CHECK_ERR_RET(!mt, LOGMEM(LYD_NODE_CTX(node)), LY_EMEM);
Michal Vasko25a32822020-07-09 15:48:22 +02002391 mt->parent = node;
2392 mt->annotation = meta->annotation;
2393 mt->value.realtype = meta->value.realtype;
2394 ret = mt->value.realtype->plugin->duplicate(LYD_NODE_CTX(node), &meta->value, &mt->value);
Michal Vasko3a41dff2020-07-15 14:30:28 +02002395 LY_CHECK_ERR_RET(ret, LOGERR(LYD_NODE_CTX(node), LY_EINT, "Value duplication failed."), ret);
Michal Vasko25a32822020-07-09 15:48:22 +02002396 mt->name = lydict_insert(LYD_NODE_CTX(node), meta->name, 0);
2397
2398 /* insert as the last attribute */
2399 if (node->meta) {
2400 for (last = node->meta; last->next; last = last->next);
2401 last->next = mt;
2402 } else {
2403 node->meta = mt;
2404 }
2405
Michal Vasko3a41dff2020-07-15 14:30:28 +02002406 if (dup) {
2407 *dup = mt;
2408 }
2409 return LY_SUCCESS;
Michal Vasko25a32822020-07-09 15:48:22 +02002410}
2411
Michal Vasko4490d312020-06-16 13:08:55 +02002412/**
2413 * @brief Merge a source sibling into target siblings.
2414 *
2415 * @param[in,out] first_trg First target sibling, is updated if top-level.
2416 * @param[in] parent_trg Target parent.
2417 * @param[in,out] sibling_src Source sibling to merge, set to NULL if spent.
2418 * @param[in] options Merge options.
2419 * @return LY_ERR value.
2420 */
2421static LY_ERR
2422lyd_merge_sibling_r(struct lyd_node **first_trg, struct lyd_node *parent_trg, const struct lyd_node **sibling_src_p,
2423 int options)
2424{
2425 LY_ERR ret;
2426 const struct lyd_node *child_src, *tmp, *sibling_src;
2427 struct lyd_node *match_trg, *dup_src, *next, *elem;
2428 struct lysc_type *type;
Michal Vasko4490d312020-06-16 13:08:55 +02002429
2430 sibling_src = *sibling_src_p;
2431 if (sibling_src->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) {
2432 /* try to find the exact instance */
2433 ret = lyd_find_sibling_first(*first_trg, sibling_src, &match_trg);
2434 } else {
2435 /* try to simply find the node, there cannot be more instances */
2436 ret = lyd_find_sibling_val(*first_trg, sibling_src->schema, NULL, 0, &match_trg);
2437 }
2438
2439 if (!ret) {
2440 /* node found, make sure even value matches for all node types */
2441 if ((match_trg->schema->nodetype == LYS_LEAF) && lyd_compare(sibling_src, match_trg, LYD_COMPARE_DEFAULTS)) {
2442 /* since they are different, they cannot both be default */
2443 assert(!(sibling_src->flags & LYD_DEFAULT) || !(match_trg->flags & LYD_DEFAULT));
2444
Michal Vasko3a41dff2020-07-15 14:30:28 +02002445 /* update value (or only LYD_DEFAULT flag) only if flag set or the source node is not default */
2446 if ((options & LYD_MERGE_DEFAULTS) || !(sibling_src->flags & LYD_DEFAULT)) {
Michal Vasko4490d312020-06-16 13:08:55 +02002447 type = ((struct lysc_node_leaf *)match_trg->schema)->type;
2448 type->plugin->free(LYD_NODE_CTX(match_trg), &((struct lyd_node_term *)match_trg)->value);
2449 LY_CHECK_RET(type->plugin->duplicate(LYD_NODE_CTX(match_trg), &((struct lyd_node_term *)sibling_src)->value,
2450 &((struct lyd_node_term *)match_trg)->value));
2451
2452 /* copy flags and add LYD_NEW */
2453 match_trg->flags = sibling_src->flags | LYD_NEW;
2454 }
2455 } else if ((match_trg->schema->nodetype & LYS_ANYDATA) && lyd_compare(sibling_src, match_trg, 0)) {
Michal Vasko4c583e82020-07-17 12:16:14 +02002456 /* update value */
2457 LY_CHECK_RET(lyd_any_copy_value(match_trg, &((struct lyd_node_any *)sibling_src)->value,
2458 ((struct lyd_node_any *)sibling_src)->value_type));
Michal Vasko4490d312020-06-16 13:08:55 +02002459
2460 /* copy flags and add LYD_NEW */
2461 match_trg->flags = sibling_src->flags | LYD_NEW;
Michal Vasko4490d312020-06-16 13:08:55 +02002462 } else {
2463 /* check descendants, recursively */
Michal Vasko5bfd4be2020-06-23 13:26:19 +02002464 LY_LIST_FOR_SAFE(LYD_CHILD(sibling_src), tmp, child_src) {
Michal Vasko4490d312020-06-16 13:08:55 +02002465 LY_CHECK_RET(lyd_merge_sibling_r(lyd_node_children_p(match_trg), match_trg, &child_src, options));
2466 }
2467 }
2468 } else {
2469 /* node not found, merge it */
2470 if (options & LYD_MERGE_DESTRUCT) {
2471 dup_src = (struct lyd_node *)sibling_src;
2472 lyd_unlink_tree(dup_src);
2473 /* spend it */
2474 *sibling_src_p = NULL;
2475 } else {
Michal Vasko3a41dff2020-07-15 14:30:28 +02002476 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 +02002477 }
2478
2479 /* set LYD_NEW for all the new nodes, required for validation */
2480 LYD_TREE_DFS_BEGIN(dup_src, next, elem) {
2481 elem->flags |= LYD_NEW;
2482 LYD_TREE_DFS_END(dup_src, next, elem);
2483 }
2484
2485 lyd_insert_node(parent_trg, first_trg, dup_src);
2486 }
2487
2488 return LY_SUCCESS;
2489}
2490
Michal Vasko3a41dff2020-07-15 14:30:28 +02002491static LY_ERR
2492lyd_merge(struct lyd_node **target, const struct lyd_node *source, int options, int nosiblings)
Michal Vasko4490d312020-06-16 13:08:55 +02002493{
2494 const struct lyd_node *sibling_src, *tmp;
2495 int first;
2496
2497 LY_CHECK_ARG_RET(NULL, target, LY_EINVAL);
2498
2499 if (!source) {
2500 /* nothing to merge */
2501 return LY_SUCCESS;
2502 }
2503
2504 if (lysc_data_parent((*target)->schema) || lysc_data_parent(source->schema)) {
2505 LOGERR(LYD_NODE_CTX(source), LY_EINVAL, "Invalid arguments - can merge only 2 top-level subtrees (%s()).", __func__);
2506 return LY_EINVAL;
2507 }
2508
2509 LY_LIST_FOR_SAFE(source, tmp, sibling_src) {
2510 first = sibling_src == source ? 1 : 0;
2511 LY_CHECK_RET(lyd_merge_sibling_r(target, NULL, &sibling_src, options));
2512 if (first && !sibling_src) {
2513 /* source was spent (unlinked), move to the next node */
2514 source = tmp;
2515 }
2516
Michal Vasko3a41dff2020-07-15 14:30:28 +02002517 if (nosiblings) {
Michal Vasko4490d312020-06-16 13:08:55 +02002518 break;
2519 }
2520 }
2521
2522 if (options & LYD_MERGE_DESTRUCT) {
2523 /* free any leftover source data that were not merged */
2524 lyd_free_siblings((struct lyd_node *)source);
2525 }
2526
2527 return LY_SUCCESS;
2528}
2529
Michal Vasko3a41dff2020-07-15 14:30:28 +02002530API LY_ERR
2531lyd_merge_tree(struct lyd_node **target, const struct lyd_node *source, int options)
2532{
2533 return lyd_merge(target, source, options, 1);
2534}
2535
2536API LY_ERR
2537lyd_merge_siblings(struct lyd_node **target, const struct lyd_node *source, int options)
2538{
2539 return lyd_merge(target, source, options, 0);
2540}
2541
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002542static LY_ERR
2543lyd_path_str_enlarge(char **buffer, size_t *buflen, size_t reqlen, int is_static)
2544{
Michal Vasko14654712020-02-06 08:35:21 +01002545 /* ending \0 */
2546 ++reqlen;
2547
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002548 if (reqlen > *buflen) {
2549 if (is_static) {
2550 return LY_EINCOMPLETE;
2551 }
2552
2553 *buffer = ly_realloc(*buffer, reqlen * sizeof **buffer);
2554 if (!*buffer) {
2555 return LY_EMEM;
2556 }
2557
2558 *buflen = reqlen;
2559 }
2560
2561 return LY_SUCCESS;
2562}
2563
Michal Vaskod59035b2020-07-08 12:00:06 +02002564LY_ERR
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002565lyd_path_list_predicate(const struct lyd_node *node, char **buffer, size_t *buflen, size_t *bufused, int is_static)
2566{
2567 const struct lyd_node *key;
2568 int dynamic = 0;
2569 size_t len;
2570 const char *val;
2571 char quot;
2572 LY_ERR rc;
2573
Michal Vasko5bfd4be2020-06-23 13:26:19 +02002574 for (key = lyd_node_children(node, 0); key && (key->schema->flags & LYS_KEY); key = key->next) {
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002575 val = lyd_value2str((struct lyd_node_term *)key, &dynamic);
2576 len = 1 + strlen(key->schema->name) + 2 + strlen(val) + 2;
2577 rc = lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static);
2578 if (rc != LY_SUCCESS) {
2579 if (dynamic) {
2580 free((char *)val);
2581 }
2582 return rc;
2583 }
2584
2585 quot = '\'';
2586 if (strchr(val, '\'')) {
2587 quot = '"';
2588 }
2589 *bufused += sprintf(*buffer + *bufused, "[%s=%c%s%c]", key->schema->name, quot, val, quot);
2590
2591 if (dynamic) {
2592 free((char *)val);
2593 }
2594 }
2595
2596 return LY_SUCCESS;
2597}
2598
2599/**
2600 * @brief Append leaf-list value predicate to path.
2601 *
2602 * @param[in] node Node to print.
2603 * @param[in,out] buffer Buffer to print to.
2604 * @param[in,out] buflen Current buffer length.
2605 * @param[in,out] bufused Current number of characters used in @p buffer.
2606 * @param[in] is_static Whether buffer is static or can be reallocated.
2607 * @return LY_ERR
2608 */
2609static LY_ERR
2610lyd_path_leaflist_predicate(const struct lyd_node *node, char **buffer, size_t *buflen, size_t *bufused, int is_static)
2611{
2612 int dynamic = 0;
2613 size_t len;
2614 const char *val;
2615 char quot;
2616 LY_ERR rc;
2617
2618 val = lyd_value2str((struct lyd_node_term *)node, &dynamic);
2619 len = 4 + strlen(val) + 2;
2620 rc = lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static);
2621 if (rc != LY_SUCCESS) {
2622 goto cleanup;
2623 }
2624
2625 quot = '\'';
2626 if (strchr(val, '\'')) {
2627 quot = '"';
2628 }
2629 *bufused += sprintf(*buffer + *bufused, "[.=%c%s%c]", quot, val, quot);
2630
2631cleanup:
2632 if (dynamic) {
2633 free((char *)val);
2634 }
2635 return rc;
2636}
2637
2638/**
2639 * @brief Append node position (relative to its other instances) predicate to path.
2640 *
2641 * @param[in] node Node to print.
2642 * @param[in,out] buffer Buffer to print to.
2643 * @param[in,out] buflen Current buffer length.
2644 * @param[in,out] bufused Current number of characters used in @p buffer.
2645 * @param[in] is_static Whether buffer is static or can be reallocated.
2646 * @return LY_ERR
2647 */
2648static LY_ERR
2649lyd_path_position_predicate(const struct lyd_node *node, char **buffer, size_t *buflen, size_t *bufused, int is_static)
2650{
2651 const struct lyd_node *first, *iter;
2652 size_t len;
2653 int pos;
2654 char *val = NULL;
2655 LY_ERR rc;
2656
2657 if (node->parent) {
2658 first = node->parent->child;
2659 } else {
2660 for (first = node; node->prev->next; node = node->prev);
2661 }
2662 pos = 1;
2663 for (iter = first; iter != node; iter = iter->next) {
2664 if (iter->schema == node->schema) {
2665 ++pos;
2666 }
2667 }
2668 if (asprintf(&val, "%d", pos) == -1) {
2669 return LY_EMEM;
2670 }
2671
2672 len = 1 + strlen(val) + 1;
2673 rc = lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static);
2674 if (rc != LY_SUCCESS) {
2675 goto cleanup;
2676 }
2677
2678 *bufused += sprintf(*buffer + *bufused, "[%s]", val);
2679
2680cleanup:
2681 free(val);
2682 return rc;
2683}
2684
2685API char *
2686lyd_path(const struct lyd_node *node, LYD_PATH_TYPE pathtype, char *buffer, size_t buflen)
2687{
Michal Vasko14654712020-02-06 08:35:21 +01002688 int is_static = 0, i, depth;
2689 size_t bufused = 0, len;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002690 const struct lyd_node *iter;
2691 const struct lys_module *mod;
2692 LY_ERR rc;
2693
2694 LY_CHECK_ARG_RET(NULL, node, NULL);
2695 if (buffer) {
2696 LY_CHECK_ARG_RET(node->schema->module->ctx, buflen > 1, NULL);
2697 is_static = 1;
Michal Vasko14654712020-02-06 08:35:21 +01002698 } else {
2699 buflen = 0;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002700 }
2701
2702 switch (pathtype) {
Michal Vasko14654712020-02-06 08:35:21 +01002703 case LYD_PATH_LOG:
2704 depth = 1;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002705 for (iter = node; iter->parent; iter = (const struct lyd_node *)iter->parent) {
2706 ++depth;
2707 }
2708
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002709 goto iter_print;
Michal Vasko14654712020-02-06 08:35:21 +01002710 while (depth) {
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002711 /* find the right node */
Michal Vasko14654712020-02-06 08:35:21 +01002712 for (iter = node, i = 1; i < depth; iter = (const struct lyd_node *)iter->parent, ++i);
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002713iter_print:
2714 /* print prefix and name */
2715 mod = NULL;
2716 if (!iter->parent || (iter->schema->module != iter->parent->schema->module)) {
2717 mod = iter->schema->module;
2718 }
2719
2720 /* realloc string */
2721 len = 1 + (mod ? strlen(mod->name) + 1 : 0) + strlen(iter->schema->name);
2722 rc = lyd_path_str_enlarge(&buffer, &buflen, bufused + len, is_static);
2723 if (rc != LY_SUCCESS) {
2724 break;
2725 }
2726
2727 /* print next node */
2728 bufused += sprintf(buffer + bufused, "/%s%s%s", mod ? mod->name : "", mod ? ":" : "", iter->schema->name);
2729
2730 switch (iter->schema->nodetype) {
2731 case LYS_LIST:
2732 if (iter->schema->flags & LYS_KEYLESS) {
2733 /* print its position */
2734 rc = lyd_path_position_predicate(iter, &buffer, &buflen, &bufused, is_static);
2735 } else {
2736 /* print all list keys in predicates */
2737 rc = lyd_path_list_predicate(iter, &buffer, &buflen, &bufused, is_static);
2738 }
2739 break;
2740 case LYS_LEAFLIST:
2741 if (iter->schema->flags & LYS_CONFIG_W) {
2742 /* print leaf-list value */
2743 rc = lyd_path_leaflist_predicate(iter, &buffer, &buflen, &bufused, is_static);
2744 } else {
2745 /* print its position */
2746 rc = lyd_path_position_predicate(iter, &buffer, &buflen, &bufused, is_static);
2747 }
2748 break;
2749 default:
2750 /* nothing to print more */
2751 rc = LY_SUCCESS;
2752 break;
2753 }
2754 if (rc != LY_SUCCESS) {
2755 break;
2756 }
2757
Michal Vasko14654712020-02-06 08:35:21 +01002758 --depth;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002759 }
2760 break;
2761 }
2762
2763 return buffer;
2764}
Michal Vaskoe444f752020-02-10 12:20:06 +01002765
Michal Vasko25a32822020-07-09 15:48:22 +02002766API struct lyd_meta *
2767lyd_find_meta(const struct lyd_meta *first, const struct lys_module *module, const char *name)
2768{
2769 struct lyd_meta *ret = NULL;
2770 const struct ly_ctx *ctx;
2771 const char *prefix, *tmp;
2772 char *str;
2773 size_t pref_len, name_len;
2774
2775 LY_CHECK_ARG_RET(NULL, module || strchr(name, ':'), name, NULL);
2776
2777 if (!first) {
2778 return NULL;
2779 }
2780
2781 ctx = first->annotation->module->ctx;
2782
2783 /* parse the name */
2784 tmp = name;
2785 if (ly_parse_nodeid(&tmp, &prefix, &pref_len, &name, &name_len) || tmp[0]) {
2786 LOGERR(ctx, LY_EINVAL, "Metadata name \"%s\" is not valid.", name);
2787 return NULL;
2788 }
2789
2790 /* find the module */
2791 if (prefix) {
2792 str = strndup(prefix, pref_len);
2793 module = ly_ctx_get_module_latest(ctx, str);
2794 free(str);
2795 LY_CHECK_ERR_RET(!module, LOGERR(ctx, LY_EINVAL, "Module \"%.*s\" not found.", pref_len, prefix), NULL);
2796 }
2797
2798 /* find the metadata */
2799 LY_LIST_FOR(first, first) {
2800 if ((first->annotation->module == module) && !strcmp(first->name, name)) {
2801 ret = (struct lyd_meta *)first;
2802 break;
2803 }
2804 }
2805
2806 return ret;
2807}
2808
Michal Vasko9b368d32020-02-14 13:53:31 +01002809API LY_ERR
Michal Vaskoe444f752020-02-10 12:20:06 +01002810lyd_find_sibling_first(const struct lyd_node *siblings, const struct lyd_node *target, struct lyd_node **match)
2811{
2812 struct lyd_node **match_p;
2813 struct lyd_node_inner *parent;
2814
Michal Vaskof03ed032020-03-04 13:31:44 +01002815 LY_CHECK_ARG_RET(NULL, target, LY_EINVAL);
Michal Vaskoe444f752020-02-10 12:20:06 +01002816
Michal Vasko62ed12d2020-05-21 10:08:25 +02002817 if (!siblings || (lysc_data_parent(siblings->schema) != lysc_data_parent(target->schema))) {
2818 /* no data or schema mismatch */
Michal Vasko9b368d32020-02-14 13:53:31 +01002819 if (match) {
2820 *match = NULL;
2821 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002822 return LY_ENOTFOUND;
2823 }
2824
2825 /* find first sibling */
2826 if (siblings->parent) {
2827 siblings = siblings->parent->child;
2828 } else {
2829 while (siblings->prev->next) {
2830 siblings = siblings->prev;
2831 }
2832 }
2833
2834 parent = (struct lyd_node_inner *)siblings->parent;
2835 if (parent && parent->children_ht) {
2836 assert(target->hash);
2837
2838 /* find by hash */
2839 if (!lyht_find(parent->children_ht, &target, target->hash, (void **)&match_p)) {
Michal Vaskoda859032020-07-14 12:20:14 +02002840 /* check even value when needed */
2841 if (!(target->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) || !lyd_compare(target, *match_p, 0)) {
2842 siblings = *match_p;
2843 } else {
2844 siblings = NULL;
2845 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002846 } else {
2847 /* not found */
2848 siblings = NULL;
2849 }
2850 } else {
2851 /* no children hash table */
2852 for (; siblings; siblings = siblings->next) {
2853 if (!lyd_compare(siblings, target, 0)) {
2854 break;
2855 }
2856 }
2857 }
2858
2859 if (!siblings) {
Michal Vasko9b368d32020-02-14 13:53:31 +01002860 if (match) {
2861 *match = NULL;
2862 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002863 return LY_ENOTFOUND;
2864 }
2865
Michal Vasko9b368d32020-02-14 13:53:31 +01002866 if (match) {
2867 *match = (struct lyd_node *)siblings;
2868 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002869 return LY_SUCCESS;
2870}
2871
Michal Vasko90932a92020-02-12 14:33:03 +01002872static int
2873lyd_hash_table_schema_val_equal(void *val1_p, void *val2_p, int UNUSED(mod), void *UNUSED(cb_data))
2874{
2875 struct lysc_node *val1;
2876 struct lyd_node *val2;
2877
2878 val1 = *((struct lysc_node **)val1_p);
2879 val2 = *((struct lyd_node **)val2_p);
2880
Michal Vasko90932a92020-02-12 14:33:03 +01002881 if (val1 == val2->schema) {
2882 /* schema match is enough */
2883 return 1;
2884 } else {
2885 return 0;
2886 }
2887}
2888
2889static LY_ERR
2890lyd_find_sibling_schema(const struct lyd_node *siblings, const struct lysc_node *schema, struct lyd_node **match)
2891{
2892 struct lyd_node **match_p;
2893 struct lyd_node_inner *parent;
2894 uint32_t hash;
2895 values_equal_cb ht_cb;
2896
Michal Vaskob104f112020-07-17 09:54:54 +02002897 assert(siblings && schema);
Michal Vasko90932a92020-02-12 14:33:03 +01002898
2899 parent = (struct lyd_node_inner *)siblings->parent;
2900 if (parent && parent->children_ht) {
2901 /* calculate our hash */
2902 hash = dict_hash_multi(0, schema->module->name, strlen(schema->module->name));
2903 hash = dict_hash_multi(hash, schema->name, strlen(schema->name));
2904 hash = dict_hash_multi(hash, NULL, 0);
2905
2906 /* use special hash table function */
2907 ht_cb = lyht_set_cb(parent->children_ht, lyd_hash_table_schema_val_equal);
2908
2909 /* find by hash */
2910 if (!lyht_find(parent->children_ht, &schema, hash, (void **)&match_p)) {
2911 siblings = *match_p;
2912 } else {
2913 /* not found */
2914 siblings = NULL;
2915 }
2916
2917 /* set the original hash table compare function back */
2918 lyht_set_cb(parent->children_ht, ht_cb);
2919 } else {
Michal Vaskob104f112020-07-17 09:54:54 +02002920 /* find first sibling */
2921 if (siblings->parent) {
2922 siblings = siblings->parent->child;
2923 } else {
2924 while (siblings->prev->next) {
2925 siblings = siblings->prev;
2926 }
2927 }
2928
2929 /* search manually without hashes */
Michal Vasko90932a92020-02-12 14:33:03 +01002930 for (; siblings; siblings = siblings->next) {
2931 if (siblings->schema == schema) {
2932 /* schema match is enough */
2933 break;
2934 }
2935 }
2936 }
2937
2938 if (!siblings) {
Michal Vasko9b368d32020-02-14 13:53:31 +01002939 if (match) {
2940 *match = NULL;
2941 }
Michal Vasko90932a92020-02-12 14:33:03 +01002942 return LY_ENOTFOUND;
2943 }
2944
Michal Vasko9b368d32020-02-14 13:53:31 +01002945 if (match) {
2946 *match = (struct lyd_node *)siblings;
2947 }
Michal Vasko90932a92020-02-12 14:33:03 +01002948 return LY_SUCCESS;
2949}
2950
Michal Vaskoe444f752020-02-10 12:20:06 +01002951API LY_ERR
2952lyd_find_sibling_val(const struct lyd_node *siblings, const struct lysc_node *schema, const char *key_or_value,
2953 size_t val_len, struct lyd_node **match)
2954{
2955 LY_ERR rc;
2956 struct lyd_node *target = NULL;
2957
Michal Vasko4c583e82020-07-17 12:16:14 +02002958 LY_CHECK_ARG_RET(NULL, schema, !(schema->nodetype & (LYS_CHOICE | LYS_CASE)), LY_EINVAL);
Michal Vaskoe444f752020-02-10 12:20:06 +01002959
Michal Vasko62ed12d2020-05-21 10:08:25 +02002960 if (!siblings || (lysc_data_parent(siblings->schema) != lysc_data_parent(schema))) {
2961 /* no data or schema mismatch */
Michal Vasko9b368d32020-02-14 13:53:31 +01002962 if (match) {
2963 *match = NULL;
2964 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002965 return LY_ENOTFOUND;
2966 }
2967
Michal Vaskof03ed032020-03-04 13:31:44 +01002968 if (key_or_value && !val_len) {
2969 val_len = strlen(key_or_value);
2970 }
2971
Michal Vaskob104f112020-07-17 09:54:54 +02002972 if ((schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) && key_or_value) {
2973 /* create a data node and find the instance */
2974 if (schema->nodetype == LYS_LEAFLIST) {
2975 /* target used attributes: schema, hash, value */
2976 rc = lyd_create_term(schema, key_or_value, val_len, NULL, lydjson_resolve_prefix, NULL, LYD_JSON, &target);
2977 LY_CHECK_RET(rc && (rc != LY_EINCOMPLETE), rc);
2978 } else {
Michal Vasko90932a92020-02-12 14:33:03 +01002979 /* target used attributes: schema, hash, child (all keys) */
Michal Vasko004d3152020-06-11 19:59:22 +02002980 LY_CHECK_RET(lyd_create_list2(schema, key_or_value, val_len, &target));
Michal Vasko90932a92020-02-12 14:33:03 +01002981 }
2982
2983 /* find it */
2984 rc = lyd_find_sibling_first(siblings, target, match);
Michal Vaskob104f112020-07-17 09:54:54 +02002985 } else {
2986 /* find the first schema node instance */
2987 rc = lyd_find_sibling_schema(siblings, schema, match);
Michal Vaskoe444f752020-02-10 12:20:06 +01002988 }
2989
Michal Vaskoe444f752020-02-10 12:20:06 +01002990 lyd_free_tree(target);
2991 return rc;
2992}
Michal Vaskoccc02342020-05-21 10:09:21 +02002993
2994API LY_ERR
2995lyd_find_xpath(const struct lyd_node *ctx_node, const char *xpath, struct ly_set **set)
2996{
2997 LY_ERR ret = LY_SUCCESS;
2998 struct lyxp_set xp_set;
2999 struct lyxp_expr *exp;
3000 uint32_t i;
3001
3002 LY_CHECK_ARG_RET(NULL, ctx_node, xpath, set, LY_EINVAL);
3003
3004 memset(&xp_set, 0, sizeof xp_set);
3005
3006 /* compile expression */
Michal Vasko004d3152020-06-11 19:59:22 +02003007 exp = lyxp_expr_parse((struct ly_ctx *)LYD_NODE_CTX(ctx_node), xpath, 0, 1);
Michal Vaskoccc02342020-05-21 10:09:21 +02003008 LY_CHECK_ERR_GOTO(!exp, ret = LY_EINVAL, cleanup);
3009
3010 /* evaluate expression */
3011 ret = lyxp_eval(exp, LYD_JSON, ctx_node->schema->module, ctx_node, LYXP_NODE_ELEM, ctx_node, &xp_set, 0);
3012 LY_CHECK_GOTO(ret, cleanup);
3013
3014 /* allocate return set */
3015 *set = ly_set_new();
3016 LY_CHECK_ERR_GOTO(!*set, LOGMEM(LYD_NODE_CTX(ctx_node)); ret = LY_EMEM, cleanup);
3017
3018 /* transform into ly_set */
3019 if (xp_set.type == LYXP_SET_NODE_SET) {
3020 /* allocate memory for all the elements once (even though not all items must be elements but most likely will be) */
3021 (*set)->objs = malloc(xp_set.used * sizeof *(*set)->objs);
3022 LY_CHECK_ERR_GOTO(!(*set)->objs, LOGMEM(LYD_NODE_CTX(ctx_node)); ret = LY_EMEM, cleanup);
3023 (*set)->size = xp_set.used;
3024
3025 for (i = 0; i < xp_set.used; ++i) {
3026 if (xp_set.val.nodes[i].type == LYXP_NODE_ELEM) {
3027 ly_set_add(*set, xp_set.val.nodes[i].node, LY_SET_OPT_USEASLIST);
3028 }
3029 }
3030 }
3031
3032cleanup:
Michal Vasko0691d522020-05-21 13:21:47 +02003033 lyxp_set_free_content(&xp_set);
Michal Vaskoccc02342020-05-21 10:09:21 +02003034 lyxp_expr_free((struct ly_ctx *)LYD_NODE_CTX(ctx_node), exp);
3035 return ret;
3036}