blob: 8862bec0514461f9ea65e9f1fb180d6359fd1bad [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 Vaskoe893ddd2020-06-23 13:35:20 +020052static LY_ERR lyd_path_list_predicate(const struct lyd_node *node, char **buffer, size_t *buflen, size_t *bufused,
53 int is_static);
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 Vasko9f96a052020-03-10 09:41:45 +0100128lyd_value_parse_meta(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 */
260 ret = LY_EVALID;
261 }
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
359lyd_parse_data_fd(const struct ly_ctx *ctx, int fd, LYD_FORMAT format, int parse_options, int validate_options, struct lyd_node **tree)
360{
361 LY_ERR ret;
362 struct ly_in *in;
363
364 LY_CHECK_RET(ly_in_new_fd(fd, &in));
365 ret = lyd_parse_data(ctx, in, format, parse_options, validate_options, tree);
366
367 ly_in_free(in, 0);
368 return ret;
369}
370
371API LY_ERR
372lyd_parse_data_path(const struct ly_ctx *ctx, const char *path, LYD_FORMAT format, int parse_options, int validate_options, struct lyd_node **tree)
373{
374 LY_ERR ret;
375 struct ly_in *in;
376
377 LY_CHECK_RET(ly_in_new_filepath(path, 0, &in));
378 ret = lyd_parse_data(ctx, in, format, parse_options, validate_options, tree);
379
380 ly_in_free(in, 0);
381 return ret;
382}
383
Radek Krejci7931b192020-06-25 17:05:03 +0200384API LY_ERR
385lyd_parse_rpc(const struct ly_ctx *ctx, struct ly_in *in, LYD_FORMAT format, struct lyd_node **tree, struct lyd_node **op)
386{
387 LY_CHECK_ARG_RET(ctx, ctx, in, tree, LY_EINVAL);
388
389 format = lyd_parse_get_format(in, format);
390 LY_CHECK_ARG_RET(ctx, format, LY_EINVAL);
391
Michal Vasko63f3d842020-07-08 10:10:14 +0200392 /* remember input position */
393 in->func_start = in->current;
394
Radek Krejci7931b192020-06-25 17:05:03 +0200395 switch (format) {
396 case LYD_XML:
Michal Vasko63f3d842020-07-08 10:10:14 +0200397 return lyd_parse_xml_rpc(ctx, in, tree, op);
Radek Krejci7931b192020-06-25 17:05:03 +0200398#if 0
399 case LYD_JSON:
Michal Vasko63f3d842020-07-08 10:10:14 +0200400 return lyd_parse_json_rpc(ctx, in, tree, op);
Radek Krejci7931b192020-06-25 17:05:03 +0200401#endif
402 case LYD_LYB:
Michal Vasko63f3d842020-07-08 10:10:14 +0200403 return lyd_parse_lyb_rpc(ctx, in, tree, op);
Radek Krejci7931b192020-06-25 17:05:03 +0200404 case LYD_SCHEMA:
405 LOGINT_RET(ctx);
406 }
407
408 LOGINT_RET(ctx);
409}
410
411API LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +0200412lyd_parse_reply(const struct lyd_node *request, struct ly_in *in, LYD_FORMAT format, struct lyd_node **tree,
413 struct lyd_node **op)
Radek Krejci7931b192020-06-25 17:05:03 +0200414{
415 LY_CHECK_ARG_RET(NULL, request, LY_EINVAL);
416 LY_CHECK_ARG_RET(LYD_NODE_CTX(request), in, tree, LY_EINVAL);
417
418 format = lyd_parse_get_format(in, format);
419 LY_CHECK_ARG_RET(LYD_NODE_CTX(request), format, LY_EINVAL);
420
Michal Vasko63f3d842020-07-08 10:10:14 +0200421 /* remember input position */
422 in->func_start = in->current;
423
Radek Krejci7931b192020-06-25 17:05:03 +0200424 switch (format) {
425 case LYD_XML:
Michal Vasko63f3d842020-07-08 10:10:14 +0200426 return lyd_parse_xml_reply(request, in, tree, op);
Radek Krejci7931b192020-06-25 17:05:03 +0200427#if 0
428 case LYD_JSON:
Michal Vasko63f3d842020-07-08 10:10:14 +0200429 return lyd_parse_json_reply(request, in, tree, op);
Radek Krejci7931b192020-06-25 17:05:03 +0200430#endif
431 case LYD_LYB:
Michal Vasko63f3d842020-07-08 10:10:14 +0200432 return lyd_parse_lyb_reply(request, in, tree, op);
Radek Krejci7931b192020-06-25 17:05:03 +0200433 case LYD_SCHEMA:
434 LOGINT_RET(LYD_NODE_CTX(request));
435 }
436
437 LOGINT_RET(LYD_NODE_CTX(request));
438}
439
440API LY_ERR
441lyd_parse_notif(const struct ly_ctx *ctx, struct ly_in *in, LYD_FORMAT format, struct lyd_node **tree, struct lyd_node **ntf)
442{
443 LY_CHECK_ARG_RET(ctx, ctx, in, tree, LY_EINVAL);
444
445 format = lyd_parse_get_format(in, format);
446 LY_CHECK_ARG_RET(ctx, format, LY_EINVAL);
447
Michal Vasko63f3d842020-07-08 10:10:14 +0200448 /* remember input position */
449 in->func_start = in->current;
450
Radek Krejci7931b192020-06-25 17:05:03 +0200451 switch (format) {
452 case LYD_XML:
Michal Vasko63f3d842020-07-08 10:10:14 +0200453 return lyd_parse_xml_notif(ctx, in, tree, ntf);
Radek Krejci7931b192020-06-25 17:05:03 +0200454#if 0
455 case LYD_JSON:
Michal Vasko63f3d842020-07-08 10:10:14 +0200456 return lyd_parse_json_notif(ctx, in, tree, ntf);
Radek Krejci7931b192020-06-25 17:05:03 +0200457#endif
458 case LYD_LYB:
Michal Vasko63f3d842020-07-08 10:10:14 +0200459 return lyd_parse_lyb_notif(ctx, in, tree, ntf);
Radek Krejci7931b192020-06-25 17:05:03 +0200460 case LYD_SCHEMA:
461 LOGINT_RET(ctx);
462 }
463
464 LOGINT_RET(ctx);
Radek Krejcie7b95092019-05-15 11:03:07 +0200465}
Radek Krejci084289f2019-07-09 17:35:30 +0200466
Michal Vasko90932a92020-02-12 14:33:03 +0100467LY_ERR
468lyd_create_term(const struct lysc_node *schema, const char *value, size_t value_len, int *dynamic,
469 ly_clb_resolve_prefix get_prefix, void *prefix_data, LYD_FORMAT format, struct lyd_node **node)
470{
471 LY_ERR ret;
472 struct lyd_node_term *term;
473
Michal Vasko9b368d32020-02-14 13:53:31 +0100474 assert(schema->nodetype & LYD_NODE_TERM);
475
Michal Vasko90932a92020-02-12 14:33:03 +0100476 term = calloc(1, sizeof *term);
477 LY_CHECK_ERR_RET(!term, LOGMEM(schema->module->ctx), LY_EMEM);
478
479 term->schema = schema;
480 term->prev = (struct lyd_node *)term;
Michal Vasko9b368d32020-02-14 13:53:31 +0100481 term->flags = LYD_NEW;
Michal Vasko90932a92020-02-12 14:33:03 +0100482
483 ret = lyd_value_parse(term, value, value_len, dynamic, 0, get_prefix, prefix_data, format, NULL);
484 if (ret && (ret != LY_EINCOMPLETE)) {
485 free(term);
486 return ret;
487 }
Michal Vasko9b368d32020-02-14 13:53:31 +0100488 lyd_hash((struct lyd_node *)term);
489
490 *node = (struct lyd_node *)term;
491 return ret;
492}
493
494LY_ERR
495lyd_create_term2(const struct lysc_node *schema, const struct lyd_value *val, struct lyd_node **node)
496{
497 LY_ERR ret;
498 struct lyd_node_term *term;
499 struct lysc_type *type;
500
501 assert(schema->nodetype & LYD_NODE_TERM);
Michal Vasko00cbf532020-06-15 13:58:47 +0200502 assert(val && val->realtype);
Michal Vasko9b368d32020-02-14 13:53:31 +0100503
504 term = calloc(1, sizeof *term);
505 LY_CHECK_ERR_RET(!term, LOGMEM(schema->module->ctx), LY_EMEM);
506
507 term->schema = schema;
508 term->prev = (struct lyd_node *)term;
509 term->flags = LYD_NEW;
510
511 type = ((struct lysc_node_leaf *)schema)->type;
512 ret = type->plugin->duplicate(schema->module->ctx, val, &term->value);
513 if (ret) {
514 LOGERR(schema->module->ctx, ret, "Value duplication failed.");
515 free(term);
516 return ret;
517 }
Michal Vasko00cbf532020-06-15 13:58:47 +0200518 term->value.realtype = val->realtype;
Michal Vasko9b368d32020-02-14 13:53:31 +0100519 lyd_hash((struct lyd_node *)term);
Michal Vasko90932a92020-02-12 14:33:03 +0100520
521 *node = (struct lyd_node *)term;
522 return ret;
523}
524
525LY_ERR
526lyd_create_inner(const struct lysc_node *schema, struct lyd_node **node)
527{
528 struct lyd_node_inner *in;
529
Michal Vasko9b368d32020-02-14 13:53:31 +0100530 assert(schema->nodetype & LYD_NODE_INNER);
531
Michal Vasko90932a92020-02-12 14:33:03 +0100532 in = calloc(1, sizeof *in);
533 LY_CHECK_ERR_RET(!in, LOGMEM(schema->module->ctx), LY_EMEM);
534
535 in->schema = schema;
536 in->prev = (struct lyd_node *)in;
Michal Vasko9b368d32020-02-14 13:53:31 +0100537 in->flags = LYD_NEW;
Michal Vasko90932a92020-02-12 14:33:03 +0100538
Michal Vasko9b368d32020-02-14 13:53:31 +0100539 /* do not hash list with keys, we need them for the hash */
540 if ((schema->nodetype != LYS_LIST) || (schema->flags & LYS_KEYLESS)) {
541 lyd_hash((struct lyd_node *)in);
542 }
Michal Vasko90932a92020-02-12 14:33:03 +0100543
544 *node = (struct lyd_node *)in;
545 return LY_SUCCESS;
546}
547
Michal Vasko90932a92020-02-12 14:33:03 +0100548LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +0200549lyd_create_list(const struct lysc_node *schema, const struct ly_path_predicate *predicates, struct lyd_node **node)
Michal Vasko90932a92020-02-12 14:33:03 +0100550{
551 LY_ERR ret = LY_SUCCESS;
Michal Vasko90932a92020-02-12 14:33:03 +0100552 struct lyd_node *list = NULL, *key;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200553 LY_ARRAY_COUNT_TYPE u;
Michal Vasko90932a92020-02-12 14:33:03 +0100554
Michal Vasko004d3152020-06-11 19:59:22 +0200555 assert((schema->nodetype == LYS_LIST) && !(schema->flags & LYS_KEYLESS));
Michal Vasko90932a92020-02-12 14:33:03 +0100556
557 /* create list */
558 LY_CHECK_GOTO(ret = lyd_create_inner(schema, &list), cleanup);
559
Michal Vasko90932a92020-02-12 14:33:03 +0100560 /* create and insert all the keys */
Michal Vasko004d3152020-06-11 19:59:22 +0200561 LY_ARRAY_FOR(predicates, u) {
562 LY_CHECK_GOTO(ret = lyd_create_term2(predicates[u].key, &predicates[u].value, &key), cleanup);
Michal Vasko90932a92020-02-12 14:33:03 +0100563 lyd_insert_node(list, NULL, key);
564 }
565
Michal Vasko9b368d32020-02-14 13:53:31 +0100566 /* hash having all the keys */
567 lyd_hash(list);
568
Michal Vasko90932a92020-02-12 14:33:03 +0100569 /* success */
570 *node = list;
571 list = NULL;
572
573cleanup:
574 lyd_free_tree(list);
Michal Vasko004d3152020-06-11 19:59:22 +0200575 return ret;
576}
577
578static LY_ERR
579lyd_create_list2(const struct lysc_node *schema, const char *keys, size_t keys_len, struct lyd_node **node)
580{
581 LY_ERR ret = LY_SUCCESS;
582 struct lyxp_expr *expr = NULL;
583 uint16_t exp_idx = 0;
584 enum ly_path_pred_type pred_type = 0;
585 struct ly_path_predicate *predicates = NULL;
586
587 /* parse keys */
588 LY_CHECK_GOTO(ret = ly_path_parse_predicate(schema->module->ctx, keys, keys_len, LY_PATH_PREFIX_OPTIONAL,
589 LY_PATH_PRED_KEYS, &expr), cleanup);
590
591 /* compile them */
Michal Vasko00cbf532020-06-15 13:58:47 +0200592 LY_CHECK_GOTO(ret = ly_path_compile_predicate(schema->module->ctx, NULL, schema, expr, &exp_idx, lydjson_resolve_prefix,
593 NULL, LYD_JSON, &predicates, &pred_type), cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +0200594
595 /* create the list node */
596 LY_CHECK_GOTO(ret = lyd_create_list(schema, predicates, node), cleanup);
597
598cleanup:
599 lyxp_expr_free(schema->module->ctx, expr);
600 ly_path_predicates_free(schema->module->ctx, pred_type, NULL, predicates);
Michal Vasko90932a92020-02-12 14:33:03 +0100601 return ret;
602}
603
604LY_ERR
605lyd_create_any(const struct lysc_node *schema, const void *value, LYD_ANYDATA_VALUETYPE value_type, struct lyd_node **node)
606{
607 struct lyd_node_any *any;
608
Michal Vasko9b368d32020-02-14 13:53:31 +0100609 assert(schema->nodetype & LYD_NODE_ANY);
610
Michal Vasko90932a92020-02-12 14:33:03 +0100611 any = calloc(1, sizeof *any);
612 LY_CHECK_ERR_RET(!any, LOGMEM(schema->module->ctx), LY_EMEM);
613
614 any->schema = schema;
615 any->prev = (struct lyd_node *)any;
Michal Vasko9b368d32020-02-14 13:53:31 +0100616 any->flags = LYD_NEW;
Michal Vasko90932a92020-02-12 14:33:03 +0100617
618 any->value.xml = value;
619 any->value_type = value_type;
Michal Vasko9b368d32020-02-14 13:53:31 +0100620 lyd_hash((struct lyd_node *)any);
Michal Vasko90932a92020-02-12 14:33:03 +0100621
622 *node = (struct lyd_node *)any;
623 return LY_SUCCESS;
624}
625
Michal Vasko52927e22020-03-16 17:26:14 +0100626LY_ERR
627lyd_create_opaq(const struct ly_ctx *ctx, const char *name, size_t name_len, const char *value, size_t value_len,
628 int *dynamic, LYD_FORMAT format, struct ly_prefix *val_prefs, const char *prefix, size_t pref_len,
629 const char *ns, struct lyd_node **node)
630{
631 struct lyd_node_opaq *opaq;
632
633 assert(ctx && name && name_len && ns);
634
635 if (!value_len) {
636 value = "";
637 }
638
639 opaq = calloc(1, sizeof *opaq);
640 LY_CHECK_ERR_RET(!opaq, LOGMEM(ctx), LY_EMEM);
641
642 opaq->prev = (struct lyd_node *)opaq;
643
644 opaq->name = lydict_insert(ctx, name, name_len);
645 opaq->format = format;
646 if (pref_len) {
647 opaq->prefix.pref = lydict_insert(ctx, prefix, pref_len);
648 }
649 opaq->prefix.ns = lydict_insert(ctx, ns, 0);
650 opaq->val_prefs = val_prefs;
651 if (dynamic && *dynamic) {
652 opaq->value = lydict_insert_zc(ctx, (char *)value);
653 *dynamic = 0;
654 } else {
655 opaq->value = lydict_insert(ctx, value, value_len);
656 }
657 opaq->ctx = ctx;
658
659 *node = (struct lyd_node *)opaq;
660 return LY_SUCCESS;
661}
662
Michal Vasko013a8182020-03-03 10:46:53 +0100663API struct lyd_node *
664lyd_new_inner(struct lyd_node *parent, const struct lys_module *module, const char *name)
665{
666 struct lyd_node *ret = NULL;
667 const struct lysc_node *schema;
668 struct ly_ctx *ctx = parent ? parent->schema->module->ctx : (module ? module->ctx : NULL);
669
670 LY_CHECK_ARG_RET(ctx, parent || module, name, NULL);
671
Michal Vaskof03ed032020-03-04 13:31:44 +0100672 if (!module) {
673 module = parent->schema->module;
674 }
675
Michal Vasko1bf09392020-03-27 12:38:10 +0100676 schema = lys_find_child(parent ? parent->schema : NULL, module, name, 0, LYS_CONTAINER | LYS_NOTIF | LYS_RPC | LYS_ACTION, 0);
Michal Vaskob00f3cf2020-05-27 11:20:13 +0200677 LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "Inner node (and not a list) \"%s\" not found.", name), NULL);
Michal Vasko013a8182020-03-03 10:46:53 +0100678
679 if (!lyd_create_inner(schema, &ret) && parent) {
680 lyd_insert_node(parent, NULL, ret);
681 }
682 return ret;
683}
684
685API struct lyd_node *
686lyd_new_list(struct lyd_node *parent, const struct lys_module *module, const char *name, ...)
687{
688 struct lyd_node *ret = NULL, *key;
689 const struct lysc_node *schema, *key_s;
690 struct ly_ctx *ctx = parent ? parent->schema->module->ctx : (module ? module->ctx : NULL);
691 va_list ap;
692 const char *key_val;
693 LY_ERR rc = LY_SUCCESS;
694
695 LY_CHECK_ARG_RET(ctx, parent || module, name, NULL);
696
Michal Vaskof03ed032020-03-04 13:31:44 +0100697 if (!module) {
698 module = parent->schema->module;
699 }
700
Michal Vasko013a8182020-03-03 10:46:53 +0100701 schema = lys_find_child(parent ? parent->schema : NULL, module, name, 0, LYS_LIST, 0);
702 LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "List node \"%s\" not found.", name), NULL);
703
704 /* create list inner node */
705 LY_CHECK_RET(lyd_create_inner(schema, &ret), NULL);
706
707 va_start(ap, name);
708
709 /* create and insert all the keys */
710 for (key_s = lysc_node_children(schema, 0); key_s && (key_s->flags & LYS_KEY); key_s = key_s->next) {
711 key_val = va_arg(ap, const char *);
712
Michal Vaskof03ed032020-03-04 13:31:44 +0100713 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 +0200714 LY_CHECK_GOTO(rc && (rc != LY_EINCOMPLETE), cleanup);
715 rc = LY_SUCCESS;
Michal Vasko013a8182020-03-03 10:46:53 +0100716 lyd_insert_node(ret, NULL, key);
717 }
718
719 /* hash having all the keys */
720 lyd_hash(ret);
721
722 if (parent) {
723 lyd_insert_node(parent, NULL, ret);
724 }
725
726cleanup:
727 if (rc) {
728 lyd_free_tree(ret);
729 ret = NULL;
730 }
731 va_end(ap);
732 return ret;
733}
734
735API struct lyd_node *
736lyd_new_list2(struct lyd_node *parent, const struct lys_module *module, const char *name, const char *keys)
737{
738 struct lyd_node *ret = NULL;
739 const struct lysc_node *schema;
740 struct ly_ctx *ctx = parent ? parent->schema->module->ctx : (module ? module->ctx : NULL);
741
742 LY_CHECK_ARG_RET(ctx, parent || module, name, NULL);
743
Michal Vaskof03ed032020-03-04 13:31:44 +0100744 if (!module) {
745 module = parent->schema->module;
746 }
Michal Vasko004d3152020-06-11 19:59:22 +0200747 if (!keys) {
748 keys = "";
749 }
Michal Vaskof03ed032020-03-04 13:31:44 +0100750
Michal Vasko004d3152020-06-11 19:59:22 +0200751 /* find schema node */
Michal Vasko013a8182020-03-03 10:46:53 +0100752 schema = lys_find_child(parent ? parent->schema : NULL, module, name, 0, LYS_LIST, 0);
753 LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "List node \"%s\" not found.", name), NULL);
754
Michal Vasko004d3152020-06-11 19:59:22 +0200755 if ((schema->flags & LYS_KEYLESS) && !keys[0]) {
756 /* key-less list */
757 LY_CHECK_RET(lyd_create_inner(schema, &ret), NULL);
758 } else {
759 /* create the list node */
760 LY_CHECK_RET(lyd_create_list2(schema, keys, strlen(keys), &ret), NULL);
761 }
762
763 if (parent) {
Michal Vasko013a8182020-03-03 10:46:53 +0100764 lyd_insert_node(parent, NULL, ret);
765 }
766 return ret;
767}
768
769API struct lyd_node *
770lyd_new_term(struct lyd_node *parent, const struct lys_module *module, const char *name, const char *val_str)
771{
Michal Vaskocbff3e92020-05-27 12:56:41 +0200772 LY_ERR rc;
Michal Vasko013a8182020-03-03 10:46:53 +0100773 struct lyd_node *ret = NULL;
774 const struct lysc_node *schema;
775 struct ly_ctx *ctx = parent ? parent->schema->module->ctx : (module ? module->ctx : NULL);
776
777 LY_CHECK_ARG_RET(ctx, parent || module, name, NULL);
778
Michal Vaskof03ed032020-03-04 13:31:44 +0100779 if (!module) {
780 module = parent->schema->module;
781 }
782
Michal Vasko013a8182020-03-03 10:46:53 +0100783 schema = lys_find_child(parent ? parent->schema : NULL, module, name, 0, LYD_NODE_TERM, 0);
784 LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "Term node \"%s\" not found.", name), NULL);
785
Michal Vaskocbff3e92020-05-27 12:56:41 +0200786 rc = lyd_create_term(schema, val_str, val_str ? strlen(val_str) : 0, NULL, lydjson_resolve_prefix, NULL, LYD_JSON, &ret);
787 LY_CHECK_RET(rc && (rc != LY_EINCOMPLETE), NULL);
788
789 if (parent) {
Michal Vasko013a8182020-03-03 10:46:53 +0100790 lyd_insert_node(parent, NULL, ret);
791 }
792 return ret;
793}
794
795API struct lyd_node *
796lyd_new_any(struct lyd_node *parent, const struct lys_module *module, const char *name, const void *value,
797 LYD_ANYDATA_VALUETYPE value_type)
798{
799 struct lyd_node *ret = NULL;
800 const struct lysc_node *schema;
801 struct ly_ctx *ctx = parent ? parent->schema->module->ctx : (module ? module->ctx : NULL);
802
803 LY_CHECK_ARG_RET(ctx, parent || module, name, NULL);
804
Michal Vaskof03ed032020-03-04 13:31:44 +0100805 if (!module) {
806 module = parent->schema->module;
807 }
808
Michal Vasko013a8182020-03-03 10:46:53 +0100809 schema = lys_find_child(parent ? parent->schema : NULL, module, name, 0, LYD_NODE_ANY, 0);
810 LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "Any node \"%s\" not found.", name), NULL);
811
812 if (!lyd_create_any(schema, value, value_type, &ret) && parent) {
813 lyd_insert_node(parent, NULL, ret);
814 }
815 return ret;
816}
817
Michal Vasko4490d312020-06-16 13:08:55 +0200818/**
819 * @brief Update node value.
820 *
821 * @param[in] node Node to update.
822 * @param[in] value New value to set.
823 * @param[in] value_type Type of @p value for any node.
824 * @param[out] new_parent Set to @p node if the value was updated, otherwise set to NULL.
825 * @param[out] new_node Set to @p node if the value was updated, otherwise set to NULL.
826 * @return LY_ERR value.
827 */
Michal Vasko00cbf532020-06-15 13:58:47 +0200828static LY_ERR
829lyd_new_path_update(struct lyd_node *node, const void *value, LYD_ANYDATA_VALUETYPE value_type,
830 struct lyd_node **new_parent, struct lyd_node **new_node)
831{
832 LY_ERR ret = LY_SUCCESS;
833 struct lyd_node *new_any;
834
835 switch (node->schema->nodetype) {
836 case LYS_CONTAINER:
837 case LYS_NOTIF:
838 case LYS_RPC:
839 case LYS_ACTION:
840 case LYS_LIST:
841 case LYS_LEAFLIST:
842 /* if it exists, there is nothing to update */
843 *new_parent = NULL;
844 *new_node = NULL;
845 break;
846 case LYS_LEAF:
847 ret = lyd_change_term(node, value);
848 if ((ret == LY_SUCCESS) || (ret == LY_EEXIST)) {
849 /* there was an actual change (at least of the default flag) */
850 *new_parent = node;
851 *new_node = node;
852 ret = LY_SUCCESS;
853 } else if (ret == LY_ENOT) {
854 /* no change */
855 *new_parent = NULL;
856 *new_node = NULL;
857 ret = LY_SUCCESS;
858 } /* else error */
859 break;
860 case LYS_ANYDATA:
861 case LYS_ANYXML:
862 /* create a new any node */
863 LY_CHECK_RET(lyd_create_any(node->schema, value, value_type, &new_any));
864
865 /* compare with the existing one */
866 if (lyd_compare(node, new_any, 0)) {
867 /* not equal, switch values (so that we can use generic node free) */
868 ((struct lyd_node_any *)new_any)->value = ((struct lyd_node_any *)node)->value;
869 ((struct lyd_node_any *)new_any)->value_type = ((struct lyd_node_any *)node)->value_type;
870 ((struct lyd_node_any *)node)->value.str = value;
871 ((struct lyd_node_any *)node)->value_type = value_type;
872
873 *new_parent = node;
874 *new_node = node;
875 } else {
876 /* they are equal */
877 *new_parent = NULL;
878 *new_node = NULL;
879 }
880 lyd_free_tree(new_any);
881 break;
882 default:
883 LOGINT(LYD_NODE_CTX(node));
884 ret = LY_EINT;
885 break;
886 }
887
888 return ret;
889}
890
Michal Vaskod86997b2020-05-26 15:19:54 +0200891API struct lyd_meta *
892lyd_new_meta(struct lyd_node *parent, const struct lys_module *module, const char *name, const char *val_str)
893{
894 struct lyd_meta *ret = NULL;
Michal Vasko00cbf532020-06-15 13:58:47 +0200895 const struct ly_ctx *ctx;
Michal Vaskod86997b2020-05-26 15:19:54 +0200896 const char *prefix, *tmp;
897 char *str;
898 size_t pref_len, name_len;
899
Michal Vasko00cbf532020-06-15 13:58:47 +0200900 LY_CHECK_ARG_RET(NULL, parent, name, module || strchr(name, ':'), NULL);
901
902 ctx = LYD_NODE_CTX(parent);
Michal Vaskod86997b2020-05-26 15:19:54 +0200903
904 /* parse the name */
905 tmp = name;
906 if (ly_parse_nodeid(&tmp, &prefix, &pref_len, &name, &name_len) || tmp[0]) {
907 LOGERR(ctx, LY_EINVAL, "Metadata name \"%s\" is not valid.", name);
908 return NULL;
909 }
910
911 /* find the module */
912 if (prefix) {
913 str = strndup(name, name_len);
914 module = ly_ctx_get_module_implemented(ctx, str);
915 free(str);
Michal Vasko004d3152020-06-11 19:59:22 +0200916 LY_CHECK_ERR_RET(!module, LOGERR(ctx, LY_EINVAL, "Module \"%.*s\" not found.", pref_len, prefix), NULL);
Michal Vaskod86997b2020-05-26 15:19:54 +0200917 }
918
919 /* set value if none */
920 if (!val_str) {
921 val_str = "";
922 }
923
924 lyd_create_meta(parent, &ret, module, name, name_len, val_str, strlen(val_str), NULL, lydjson_resolve_prefix, NULL,
925 LYD_JSON, parent->schema);
926 return ret;
927}
928
Michal Vasko00cbf532020-06-15 13:58:47 +0200929API struct lyd_node *
930lyd_new_opaq(struct lyd_node *parent, const struct ly_ctx *ctx, const char *name, const char *value,
931 const char *module_name)
932{
933 struct lyd_node *ret = NULL;
934
935 LY_CHECK_ARG_RET(ctx, parent || ctx, name, module_name, NULL);
936
937 if (!ctx) {
938 ctx = LYD_NODE_CTX(parent);
939 }
940 if (!value) {
941 value = "";
942 }
943
944 if (!lyd_create_opaq(ctx, name, strlen(name), value, strlen(value), NULL, LYD_JSON, NULL, NULL, 0, module_name, &ret)
945 && parent) {
946 lyd_insert_node(parent, NULL, ret);
947 }
948 return ret;
949}
950
951API struct ly_attr *
952lyd_new_attr(struct lyd_node *parent, const char *module_name, const char *name, const char *val_str)
953{
954 struct ly_attr *ret = NULL;
955 const struct ly_ctx *ctx;
956 const char *prefix, *tmp;
957 size_t pref_len, name_len;
958
959 LY_CHECK_ARG_RET(NULL, parent, !parent->schema, name, NULL);
960
961 ctx = LYD_NODE_CTX(parent);
962
963 /* parse the name */
964 tmp = name;
965 if (ly_parse_nodeid(&tmp, &prefix, &pref_len, &name, &name_len) || tmp[0]) {
966 LOGERR(ctx, LY_EINVAL, "Metadata name \"%s\" is not valid.", name);
967 return NULL;
968 }
969
970 /* set value if none */
971 if (!val_str) {
972 val_str = "";
973 }
974
975 ly_create_attr(parent, &ret, ctx, name, name_len, val_str, strlen(val_str), NULL, LYD_JSON, NULL, prefix,
976 pref_len, module_name);
977 return ret;
978}
979
980API LY_ERR
981lyd_change_term(struct lyd_node *term, const char *val_str)
982{
983 LY_ERR ret = LY_SUCCESS;
984 struct lysc_type *type;
985 struct lyd_node_term *t;
986 struct lyd_node *parent;
987 struct lyd_value val = {0};
988 int dflt_change, val_change;
989
990 LY_CHECK_ARG_RET(NULL, term, term->schema, term->schema->nodetype & LYD_NODE_TERM, LY_EINVAL);
991
992 if (!val_str) {
993 val_str = "";
994 }
995 t = (struct lyd_node_term *)term;
996 type = ((struct lysc_node_leaf *)term->schema)->type;
997
998 /* parse the new value */
999 LY_CHECK_GOTO(ret = lyd_value_store(&val, term->schema, val_str, strlen(val_str), NULL, lydjson_resolve_prefix, NULL,
1000 LYD_JSON), cleanup);
1001
1002 /* compare original and new value */
1003 if (type->plugin->compare(&t->value, &val)) {
1004 /* values differ, switch them */
1005 type->plugin->free(LYD_NODE_CTX(term), &t->value);
1006 t->value = val;
1007 memset(&val, 0, sizeof val);
1008 val_change = 1;
1009 } else {
1010 val_change = 0;
1011 }
1012
1013 /* always clear the default flag */
1014 if (term->flags & LYD_DEFAULT) {
1015 for (parent = term; parent; parent = (struct lyd_node *)parent->parent) {
1016 parent->flags &= ~LYD_DEFAULT;
1017 }
1018 dflt_change = 1;
1019 } else {
1020 dflt_change = 0;
1021 }
1022
1023 if (val_change || dflt_change) {
1024 /* make the node non-validated */
1025 term->flags &= LYD_NEW;
1026 }
1027
1028 if (val_change) {
1029 if (term->schema->nodetype == LYS_LEAFLIST) {
1030 /* leaf-list needs to be hashed again and re-inserted into parent */
1031 lyd_unlink_hash(term);
1032 lyd_hash(term);
1033 LY_CHECK_GOTO(ret = lyd_insert_hash(term), cleanup);
1034 } else if ((term->schema->flags & LYS_KEY) && term->parent) {
1035 /* list needs to be updated if its key was changed */
1036 assert(term->parent->schema->nodetype == LYS_LIST);
1037 lyd_unlink_hash((struct lyd_node *)term->parent);
1038 lyd_hash((struct lyd_node *)term->parent);
1039 LY_CHECK_GOTO(ret = lyd_insert_hash((struct lyd_node *)term->parent), cleanup);
1040 } /* else leaf that is not a key, its value is not used for its hash so it does not change */
1041 }
1042
1043 /* retrun value */
1044 if (!val_change) {
1045 if (dflt_change) {
1046 /* only default flag change */
1047 ret = LY_EEXIST;
1048 } else {
1049 /* no change */
1050 ret = LY_ENOT;
1051 }
1052 } /* else value changed, LY_SUCCESS */
1053
1054cleanup:
1055 type->plugin->free(LYD_NODE_CTX(term), &val);
1056 return ret;
1057}
1058
1059API struct lyd_node *
1060lyd_new_path(struct lyd_node *parent, const struct ly_ctx *ctx, const char *path, const char *value, int options)
1061{
Michal Vaskoa8f9eb32020-06-16 13:07:12 +02001062 struct lyd_node *new_parent = NULL;
Michal Vasko00cbf532020-06-15 13:58:47 +02001063
Michal Vaskoa8f9eb32020-06-16 13:07:12 +02001064 lyd_new_path2(parent, ctx, path, value, 0, options, &new_parent, NULL);
1065 return new_parent;
Michal Vasko00cbf532020-06-15 13:58:47 +02001066}
1067
1068API struct lyd_node *
1069lyd_new_path_any(struct lyd_node *parent, const struct ly_ctx *ctx, const char *path, const void *value,
1070 LYD_ANYDATA_VALUETYPE value_type, int options)
1071{
Michal Vaskoa8f9eb32020-06-16 13:07:12 +02001072 struct lyd_node *new_parent = NULL;
Michal Vasko00cbf532020-06-15 13:58:47 +02001073
Michal Vaskoa8f9eb32020-06-16 13:07:12 +02001074 lyd_new_path2(parent, ctx, path, value, value_type, options, &new_parent, NULL);
1075 return new_parent;
Michal Vasko00cbf532020-06-15 13:58:47 +02001076}
1077
1078API LY_ERR
1079lyd_new_path2(struct lyd_node *parent, const struct ly_ctx *ctx, const char *path, const void *value,
1080 LYD_ANYDATA_VALUETYPE value_type, int options, struct lyd_node **new_parent, struct lyd_node **new_node)
1081{
1082 LY_ERR ret = LY_SUCCESS, r;
1083 struct lyxp_expr *exp = NULL;
1084 struct ly_path *p = NULL;
1085 struct lyd_node *nparent = NULL, *nnode = NULL, *node = NULL, *cur_parent;
1086 const struct lysc_node *schema;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001087 LY_ARRAY_COUNT_TYPE path_idx = 0;
Michal Vasko00cbf532020-06-15 13:58:47 +02001088 struct ly_path_predicate *pred;
1089
1090 LY_CHECK_ARG_RET(ctx, parent || ctx, path, (path[0] == '/') || parent, LY_EINVAL);
1091
1092 if (!ctx) {
1093 ctx = LYD_NODE_CTX(parent);
1094 }
1095
1096 /* parse path */
1097 LY_CHECK_GOTO(ret = ly_path_parse(ctx, path, strlen(path), LY_PATH_BEGIN_EITHER, LY_PATH_LREF_FALSE,
1098 LY_PATH_PREFIX_OPTIONAL, LY_PATH_PRED_SIMPLE, &exp), cleanup);
1099
1100 /* compile path */
1101 LY_CHECK_GOTO(ret = ly_path_compile(ctx, NULL, parent ? parent->schema : NULL, exp, LY_PATH_LREF_FALSE,
1102 options & LYD_NEWOPT_OUTPUT ? LY_PATH_OPER_OUTPUT : LY_PATH_OPER_INPUT,
1103 LY_PATH_TARGET_MANY, lydjson_resolve_prefix, NULL, LYD_JSON, &p), cleanup);
1104
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001105 schema = p[LY_ARRAY_COUNT(p) - 1].node;
1106 if ((schema->nodetype == LYS_LIST) && (p[LY_ARRAY_COUNT(p) - 1].pred_type == LY_PATH_PREDTYPE_NONE)
Michal Vasko00cbf532020-06-15 13:58:47 +02001107 && !(options & LYD_NEWOPT_OPAQ)) {
1108 LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_XPATH, "Predicate missing for %s \"%s\" in path.",
1109 lys_nodetype2str(schema->nodetype), schema->name);
1110 ret = LY_EINVAL;
1111 goto cleanup;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001112 } 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 +02001113 /* parse leafref value into a predicate, if not defined in the path */
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001114 p[LY_ARRAY_COUNT(p) - 1].pred_type = LY_PATH_PREDTYPE_LEAFLIST;
1115 LY_ARRAY_NEW_GOTO(ctx, p[LY_ARRAY_COUNT(p) - 1].predicates, pred, ret, cleanup);
Michal Vasko00cbf532020-06-15 13:58:47 +02001116
1117 if (!value) {
1118 value = "";
1119 }
1120
1121 r = LY_SUCCESS;
1122 if (options & LYD_NEWOPT_OPAQ) {
1123 r = lys_value_validate(NULL, schema, value, strlen(value), lydjson_resolve_prefix, NULL, LYD_JSON);
1124 }
1125 if (!r) {
1126 LY_CHECK_GOTO(ret = lyd_value_store(&pred->value, schema, value, strlen(value), NULL, lydjson_resolve_prefix,
1127 NULL, LYD_JSON), cleanup);
1128 } /* else we have opaq flag and the value is not valid, leavne no predicate and then create an opaque node */
1129 }
1130
1131 /* try to find any existing nodes in the path */
1132 if (parent) {
1133 ret = ly_path_eval_partial(p, parent, &path_idx, &node);
1134 if (ret == LY_SUCCESS) {
1135 /* the node exists, are we supposed to update it or is it just a default? */
1136 if (!(options & LYD_NEWOPT_UPDATE) && !(node->flags & LYD_DEFAULT)) {
1137 LOGERR(ctx, LY_EEXIST, "Path \"%s\" already exists", path);
1138 ret = LY_EEXIST;
1139 goto cleanup;
1140 }
1141
1142 /* update the existing node */
1143 ret = lyd_new_path_update(node, value, value_type, &nparent, &nnode);
1144 goto cleanup;
1145 } else if (ret == LY_EINCOMPLETE) {
1146 /* some nodes were found, adjust the iterator to the next segment */
1147 ++path_idx;
1148 } else if (ret == LY_ENOTFOUND) {
1149 /* 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 +02001150 if (lysc_data_parent(p[LY_ARRAY_COUNT(p) - 1].node)) {
Michal Vasko00cbf532020-06-15 13:58:47 +02001151 node = parent;
1152 }
1153 } else {
1154 /* error */
1155 goto cleanup;
1156 }
1157 }
1158
1159 /* create all the non-existing nodes in a loop */
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001160 for (; path_idx < LY_ARRAY_COUNT(p); ++path_idx) {
Michal Vasko00cbf532020-06-15 13:58:47 +02001161 cur_parent = node;
1162 schema = p[path_idx].node;
1163
1164 switch (schema->nodetype) {
1165 case LYS_LIST:
1166 if (!(schema->flags & LYS_KEYLESS)) {
1167 if ((options & LYD_NEWOPT_OPAQ) && (p[path_idx].pred_type == LY_PATH_PREDTYPE_NONE)) {
1168 /* creating opaque list without keys */
1169 LY_CHECK_GOTO(ret = lyd_create_opaq(ctx, schema->name, strlen(schema->name), NULL, 0, NULL,
1170 LYD_JSON, NULL, NULL, 0, schema->module->name, &node), cleanup);
1171 } else {
1172 assert(p[path_idx].pred_type == LY_PATH_PREDTYPE_LIST);
1173 LY_CHECK_GOTO(ret = lyd_create_list(schema, p[path_idx].predicates, &node), cleanup);
1174 }
1175 break;
1176 }
1177 /* fallthrough */
1178 case LYS_CONTAINER:
1179 case LYS_NOTIF:
1180 case LYS_RPC:
1181 case LYS_ACTION:
1182 LY_CHECK_GOTO(ret = lyd_create_inner(schema, &node), cleanup);
1183 break;
1184 case LYS_LEAFLIST:
1185 if ((options & LYD_NEWOPT_OPAQ) && (p[path_idx].pred_type == LY_PATH_PREDTYPE_NONE)) {
1186 /* creating opaque leaf-list without value */
1187 LY_CHECK_GOTO(ret = lyd_create_opaq(ctx, schema->name, strlen(schema->name), NULL, 0, NULL,
1188 LYD_JSON, NULL, NULL, 0, schema->module->name, &node), cleanup);
1189 } else {
1190 assert(p[path_idx].pred_type == LY_PATH_PREDTYPE_LEAFLIST);
1191 LY_CHECK_GOTO(ret = lyd_create_term2(schema, &p[path_idx].predicates[0].value, &node), cleanup);
1192 }
1193 break;
1194 case LYS_LEAF:
1195 /* make there is some value */
1196 if (!value) {
1197 value = "";
1198 }
1199
1200 r = LY_SUCCESS;
1201 if (options & LYD_NEWOPT_OPAQ) {
1202 r = lys_value_validate(NULL, schema, value, strlen(value), lydjson_resolve_prefix, NULL, LYD_JSON);
1203 }
1204 if (!r) {
1205 LY_CHECK_GOTO(ret = lyd_create_term(schema, value, strlen(value), NULL, lydjson_resolve_prefix, NULL,
1206 LYD_JSON, &node), cleanup);
1207 } else {
1208 /* creating opaque leaf without value */
1209 LY_CHECK_GOTO(ret = lyd_create_opaq(ctx, schema->name, strlen(schema->name), NULL, 0, NULL,
1210 LYD_JSON, NULL, NULL, 0, schema->module->name, &node), cleanup);
1211 }
1212 break;
1213 case LYS_ANYDATA:
1214 case LYS_ANYXML:
1215 LY_CHECK_GOTO(ret = lyd_create_any(schema, value, value_type, &node), cleanup);
1216 break;
1217 default:
1218 LOGINT(ctx);
1219 ret = LY_EINT;
1220 goto cleanup;
1221 }
1222
1223 if (cur_parent) {
1224 /* connect to the parent */
1225 lyd_insert_node(cur_parent, NULL, node);
1226 } else if (parent) {
1227 /* connect to top-level siblings */
1228 lyd_insert_node(NULL, &parent, node);
1229 }
1230
1231 /* update remembered nodes */
1232 if (!nparent) {
1233 nparent = node;
1234 }
1235 nnode = node;
1236 }
1237
1238cleanup:
1239 lyxp_expr_free(ctx, exp);
1240 ly_path_free(ctx, p);
1241 if (!ret) {
1242 /* set out params only on success */
1243 if (new_parent) {
1244 *new_parent = nparent;
1245 }
1246 if (new_node) {
1247 *new_node = nnode;
1248 }
1249 }
1250 return ret;
1251}
1252
Michal Vasko90932a92020-02-12 14:33:03 +01001253struct lyd_node *
1254lyd_get_prev_key_anchor(const struct lyd_node *first_sibling, const struct lysc_node *new_key)
1255{
1256 const struct lysc_node *prev_key;
1257 struct lyd_node *match = NULL;
1258
1259 if (!first_sibling) {
1260 return NULL;
1261 }
1262
1263 for (prev_key = new_key->prev; !match && prev_key->next; prev_key = prev_key->prev) {
1264 lyd_find_sibling_val(first_sibling, prev_key, NULL, 0, &match);
1265 }
1266
1267 return match;
1268}
1269
1270/**
1271 * @brief Insert node after a sibling.
1272 *
Michal Vasko9f96a052020-03-10 09:41:45 +01001273 * Handles inserting into NP containers and key-less lists.
1274 *
Michal Vasko90932a92020-02-12 14:33:03 +01001275 * @param[in] sibling Sibling to insert after.
1276 * @param[in] node Node to insert.
1277 */
1278static void
Michal Vaskof03ed032020-03-04 13:31:44 +01001279lyd_insert_after_node(struct lyd_node *sibling, struct lyd_node *node)
Michal Vasko90932a92020-02-12 14:33:03 +01001280{
Michal Vasko0249f7c2020-03-05 16:36:40 +01001281 struct lyd_node_inner *par;
1282
Michal Vasko90932a92020-02-12 14:33:03 +01001283 assert(!node->next && (node->prev == node));
1284
1285 node->next = sibling->next;
1286 node->prev = sibling;
1287 sibling->next = node;
1288 if (node->next) {
1289 /* sibling had a succeeding node */
1290 node->next->prev = node;
1291 } else {
1292 /* sibling was last, find first sibling and change its prev */
1293 if (sibling->parent) {
1294 sibling = sibling->parent->child;
1295 } else {
1296 for (; sibling->prev->next != node; sibling = sibling->prev);
1297 }
1298 sibling->prev = node;
1299 }
1300 node->parent = sibling->parent;
Michal Vasko0249f7c2020-03-05 16:36:40 +01001301
Michal Vasko9f96a052020-03-10 09:41:45 +01001302 for (par = node->parent; par; par = par->parent) {
1303 if ((par->flags & LYD_DEFAULT) && !(node->flags & LYD_DEFAULT)) {
1304 /* remove default flags from NP containers */
Michal Vasko0249f7c2020-03-05 16:36:40 +01001305 par->flags &= ~LYD_DEFAULT;
1306 }
Michal Vasko9f96a052020-03-10 09:41:45 +01001307 if ((par->schema->nodetype == LYS_LIST) && (par->schema->flags & LYS_KEYLESS)) {
1308 /* rehash key-less list */
1309 lyd_hash((struct lyd_node *)par);
1310 }
Michal Vasko0249f7c2020-03-05 16:36:40 +01001311 }
1312
1313 /* insert into hash table */
1314 lyd_insert_hash(node);
Michal Vasko90932a92020-02-12 14:33:03 +01001315}
1316
1317/**
1318 * @brief Insert node before a sibling.
1319 *
Michal Vasko9f96a052020-03-10 09:41:45 +01001320 * Handles inserting into NP containers and key-less lists.
1321 *
Michal Vasko90932a92020-02-12 14:33:03 +01001322 * @param[in] sibling Sibling to insert before.
1323 * @param[in] node Node to insert.
1324 */
1325static void
Michal Vaskof03ed032020-03-04 13:31:44 +01001326lyd_insert_before_node(struct lyd_node *sibling, struct lyd_node *node)
Michal Vasko90932a92020-02-12 14:33:03 +01001327{
Michal Vasko0249f7c2020-03-05 16:36:40 +01001328 struct lyd_node_inner *par;
1329
Michal Vasko90932a92020-02-12 14:33:03 +01001330 assert(!node->next && (node->prev == node));
1331
1332 node->next = sibling;
1333 /* covers situation of sibling being first */
1334 node->prev = sibling->prev;
1335 sibling->prev = node;
1336 if (node->prev->next) {
1337 /* sibling had a preceding node */
1338 node->prev->next = node;
1339 } else if (sibling->parent) {
1340 /* sibling was first and we must also change parent child pointer */
1341 sibling->parent->child = node;
1342 }
1343 node->parent = sibling->parent;
Michal Vasko0249f7c2020-03-05 16:36:40 +01001344
Michal Vasko9f96a052020-03-10 09:41:45 +01001345 for (par = node->parent; par; par = par->parent) {
1346 if ((par->flags & LYD_DEFAULT) && !(node->flags & LYD_DEFAULT)) {
1347 /* remove default flags from NP containers */
Michal Vasko0249f7c2020-03-05 16:36:40 +01001348 par->flags &= ~LYD_DEFAULT;
1349 }
Michal Vasko9f96a052020-03-10 09:41:45 +01001350 if ((par->schema->nodetype == LYS_LIST) && (par->schema->flags & LYS_KEYLESS)) {
1351 /* rehash key-less list */
1352 lyd_hash((struct lyd_node *)par);
1353 }
Michal Vasko0249f7c2020-03-05 16:36:40 +01001354 }
1355
1356 /* insert into hash table */
1357 lyd_insert_hash(node);
Michal Vasko90932a92020-02-12 14:33:03 +01001358}
1359
1360/**
1361 * @brief Insert node as the last child of a parent.
1362 *
Michal Vasko9f96a052020-03-10 09:41:45 +01001363 * Handles inserting into NP containers and key-less lists.
1364 *
Michal Vasko90932a92020-02-12 14:33:03 +01001365 * @param[in] parent Parent to insert into.
1366 * @param[in] node Node to insert.
1367 */
1368static void
Michal Vaskof03ed032020-03-04 13:31:44 +01001369lyd_insert_last_node(struct lyd_node *parent, struct lyd_node *node)
Michal Vasko90932a92020-02-12 14:33:03 +01001370{
1371 struct lyd_node_inner *par;
1372
Michal Vasko0249f7c2020-03-05 16:36:40 +01001373 assert(parent && !node->next && (node->prev == node));
Michal Vasko52927e22020-03-16 17:26:14 +01001374 assert(!parent->schema || (parent->schema->nodetype & LYD_NODE_INNER));
Michal Vasko90932a92020-02-12 14:33:03 +01001375
1376 par = (struct lyd_node_inner *)parent;
1377
1378 if (!par->child) {
1379 par->child = node;
1380 } else {
1381 node->prev = par->child->prev;
1382 par->child->prev->next = node;
1383 par->child->prev = node;
1384 }
1385 node->parent = par;
Michal Vasko0249f7c2020-03-05 16:36:40 +01001386
Michal Vasko9f96a052020-03-10 09:41:45 +01001387 for (; par; par = par->parent) {
1388 if ((par->flags & LYD_DEFAULT) && !(node->flags & LYD_DEFAULT)) {
1389 /* remove default flags from NP containers */
Michal Vasko0249f7c2020-03-05 16:36:40 +01001390 par->flags &= ~LYD_DEFAULT;
1391 }
Michal Vasko52927e22020-03-16 17:26:14 +01001392 if (par->schema && (par->schema->nodetype == LYS_LIST) && (par->schema->flags & LYS_KEYLESS)) {
Michal Vasko9f96a052020-03-10 09:41:45 +01001393 /* rehash key-less list */
1394 lyd_hash((struct lyd_node *)par);
1395 }
Michal Vasko0249f7c2020-03-05 16:36:40 +01001396 }
1397
1398 /* insert into hash table */
1399 lyd_insert_hash(node);
Michal Vasko90932a92020-02-12 14:33:03 +01001400}
1401
1402void
Michal Vasko9b368d32020-02-14 13:53:31 +01001403lyd_insert_node(struct lyd_node *parent, struct lyd_node **first_sibling, struct lyd_node *node)
Michal Vasko90932a92020-02-12 14:33:03 +01001404{
Michal Vasko9b368d32020-02-14 13:53:31 +01001405 struct lyd_node *anchor;
Michal Vasko9f96a052020-03-10 09:41:45 +01001406 const struct lysc_node *skey = NULL;
1407 int has_keys;
Michal Vasko90932a92020-02-12 14:33:03 +01001408
Michal Vasko52927e22020-03-16 17:26:14 +01001409 assert((parent || first_sibling) && node && (node->hash || !node->schema));
Michal Vasko9b368d32020-02-14 13:53:31 +01001410
1411 if (!parent && first_sibling && (*first_sibling) && (*first_sibling)->parent) {
1412 parent = (struct lyd_node *)(*first_sibling)->parent;
1413 }
Michal Vasko90932a92020-02-12 14:33:03 +01001414
1415 if (parent) {
Michal Vasko52927e22020-03-16 17:26:14 +01001416 if (node->schema && (node->schema->flags & LYS_KEY)) {
Michal Vasko90932a92020-02-12 14:33:03 +01001417 /* it is key and we need to insert it at the correct place */
Michal Vasko5bfd4be2020-06-23 13:26:19 +02001418 anchor = lyd_get_prev_key_anchor(lyd_node_children(parent, 0), node->schema);
Michal Vasko9b368d32020-02-14 13:53:31 +01001419 if (anchor) {
Michal Vaskof03ed032020-03-04 13:31:44 +01001420 lyd_insert_after_node(anchor, node);
Michal Vasko5bfd4be2020-06-23 13:26:19 +02001421 } else if (lyd_node_children(parent, 0)) {
1422 lyd_insert_before_node(lyd_node_children(parent, 0), node);
Michal Vasko90932a92020-02-12 14:33:03 +01001423 } else {
Michal Vaskof03ed032020-03-04 13:31:44 +01001424 lyd_insert_last_node(parent, node);
Michal Vasko90932a92020-02-12 14:33:03 +01001425 }
Michal Vasko9f96a052020-03-10 09:41:45 +01001426
1427 /* hash list if all its keys were added */
1428 assert(parent->schema->nodetype == LYS_LIST);
Michal Vasko5bfd4be2020-06-23 13:26:19 +02001429 anchor = lyd_node_children(parent, 0);
Michal Vasko9f96a052020-03-10 09:41:45 +01001430 has_keys = 1;
1431 while ((skey = lys_getnext(skey, parent->schema, NULL, 0)) && (skey->flags & LYS_KEY)) {
1432 if (!anchor || (anchor->schema != skey)) {
1433 /* key missing */
1434 has_keys = 0;
1435 break;
1436 }
1437
1438 anchor = anchor->next;
1439 }
1440 if (has_keys) {
1441 lyd_hash(parent);
1442 }
1443
Michal Vasko90932a92020-02-12 14:33:03 +01001444 } else {
1445 /* last child */
Michal Vaskof03ed032020-03-04 13:31:44 +01001446 lyd_insert_last_node(parent, node);
Michal Vasko90932a92020-02-12 14:33:03 +01001447 }
Michal Vasko9b368d32020-02-14 13:53:31 +01001448 } else if (*first_sibling) {
Michal Vaskob1b5c262020-03-05 14:29:47 +01001449 /* top-level siblings */
Michal Vasko9b368d32020-02-14 13:53:31 +01001450 anchor = (*first_sibling)->prev;
Michal Vaskoc193ce92020-03-06 11:04:48 +01001451 while (anchor->prev->next && (lyd_owner_module(anchor) != lyd_owner_module(node))) {
Michal Vasko9b368d32020-02-14 13:53:31 +01001452 anchor = anchor->prev;
1453 }
1454
Michal Vaskoc193ce92020-03-06 11:04:48 +01001455 if (lyd_owner_module(anchor) == lyd_owner_module(node)) {
Michal Vaskob1b5c262020-03-05 14:29:47 +01001456 /* insert after last sibling from this module */
1457 lyd_insert_after_node(anchor, node);
1458 } else {
1459 /* no data from this module, insert at the last position */
1460 lyd_insert_after_node((*first_sibling)->prev, node);
1461 }
Michal Vasko90932a92020-02-12 14:33:03 +01001462 } else {
Michal Vasko9b368d32020-02-14 13:53:31 +01001463 /* the only sibling */
1464 *first_sibling = node;
Michal Vasko90932a92020-02-12 14:33:03 +01001465 }
Michal Vasko90932a92020-02-12 14:33:03 +01001466}
1467
Michal Vaskof03ed032020-03-04 13:31:44 +01001468static LY_ERR
1469lyd_insert_check_schema(const struct lysc_node *parent, const struct lysc_node *schema)
1470{
1471 const struct lysc_node *par2;
1472
1473 assert(schema);
Michal Vasko62ed12d2020-05-21 10:08:25 +02001474 assert(!parent || !(parent->nodetype & (LYS_CASE | LYS_CHOICE)));
Michal Vaskof03ed032020-03-04 13:31:44 +01001475
1476 /* find schema parent */
Michal Vasko62ed12d2020-05-21 10:08:25 +02001477 par2 = lysc_data_parent(schema);
Michal Vaskof03ed032020-03-04 13:31:44 +01001478
1479 if (parent) {
1480 /* inner node */
1481 if (par2 != parent) {
Radek Krejcif6d14cb2020-07-02 16:11:45 +02001482 LOGERR(schema->module->ctx, LY_EINVAL, "Cannot insert, parent of \"%s\" is not \"%s\".", schema->name, parent->name);
Michal Vaskof03ed032020-03-04 13:31:44 +01001483 return LY_EINVAL;
1484 }
1485 } else {
1486 /* top-level node */
1487 if (par2) {
Radek Krejcif6d14cb2020-07-02 16:11:45 +02001488 LOGERR(schema->module->ctx, LY_EINVAL, "Cannot insert, node \"%s\" is not top-level.", schema->name);
Michal Vaskof03ed032020-03-04 13:31:44 +01001489 return LY_EINVAL;
1490 }
1491 }
1492
1493 return LY_SUCCESS;
1494}
1495
1496API LY_ERR
1497lyd_insert(struct lyd_node *parent, struct lyd_node *node)
1498{
1499 struct lyd_node *iter;
1500
Michal Vasko654bc852020-06-23 13:28:06 +02001501 LY_CHECK_ARG_RET(NULL, parent, node, parent->schema->nodetype & LYD_NODE_INNER, LY_EINVAL);
Michal Vaskof03ed032020-03-04 13:31:44 +01001502
1503 LY_CHECK_RET(lyd_insert_check_schema(parent->schema, node->schema));
1504
1505 if (node->schema->flags & LYS_KEY) {
1506 LOGERR(parent->schema->module->ctx, LY_EINVAL, "Cannot insert key \"%s\".", node->schema->name);
1507 return LY_EINVAL;
1508 }
1509
1510 if (node->parent || node->prev->next) {
1511 lyd_unlink_tree(node);
1512 }
1513
1514 while (node) {
1515 iter = node->next;
1516 lyd_unlink_tree(node);
1517 lyd_insert_node(parent, NULL, node);
1518 node = iter;
1519 }
1520 return LY_SUCCESS;
1521}
1522
1523API LY_ERR
Michal Vaskob1b5c262020-03-05 14:29:47 +01001524lyd_insert_sibling(struct lyd_node *sibling, struct lyd_node *node)
1525{
1526 struct lyd_node *iter;
1527
1528 LY_CHECK_ARG_RET(NULL, sibling, node, LY_EINVAL);
1529
Michal Vasko62ed12d2020-05-21 10:08:25 +02001530 LY_CHECK_RET(lyd_insert_check_schema(lysc_data_parent(sibling->schema), node->schema));
Michal Vaskob1b5c262020-03-05 14:29:47 +01001531
1532 if (node->schema->flags & LYS_KEY) {
1533 LOGERR(sibling->schema->module->ctx, LY_EINVAL, "Cannot insert key \"%s\".", node->schema->name);
1534 return LY_EINVAL;
1535 }
1536
1537 if (node->parent || node->prev->next) {
1538 lyd_unlink_tree(node);
1539 }
1540
1541 while (node) {
1542 iter = node->next;
1543 lyd_unlink_tree(node);
1544 lyd_insert_node(NULL, &sibling, node);
1545 node = iter;
1546 }
1547 return LY_SUCCESS;
1548}
1549
Michal Vasko0249f7c2020-03-05 16:36:40 +01001550static LY_ERR
1551lyd_insert_after_check_place(struct lyd_node *anchor, struct lyd_node *sibling, struct lyd_node *node)
1552{
1553 if (sibling->parent) {
1554 /* nested, we do not care for the order */
1555 return LY_SUCCESS;
1556 }
1557
1558 if (anchor) {
Michal Vaskoc193ce92020-03-06 11:04:48 +01001559 if (anchor->next && (lyd_owner_module(anchor) == lyd_owner_module(anchor->next))
1560 && (lyd_owner_module(node) != lyd_owner_module(anchor))) {
Michal Vasko0249f7c2020-03-05 16:36:40 +01001561 LOGERR(sibling->schema->module->ctx, LY_EINVAL, "Cannot insert top-level module \"%s\" data into module \"%s\" data.",
Michal Vaskoc193ce92020-03-06 11:04:48 +01001562 lyd_owner_module(node)->name, lyd_owner_module(anchor)->name);
Michal Vasko0249f7c2020-03-05 16:36:40 +01001563 return LY_EINVAL;
1564 }
1565
Michal Vaskoc193ce92020-03-06 11:04:48 +01001566 if ((lyd_owner_module(node) == lyd_owner_module(anchor))
1567 || (anchor->next && (lyd_owner_module(node) == lyd_owner_module(anchor->next)))) {
Michal Vasko0249f7c2020-03-05 16:36:40 +01001568 /* inserting before/after its module data */
1569 return LY_SUCCESS;
1570 }
1571 }
1572
1573 /* find first sibling */
1574 while (sibling->prev->next) {
1575 sibling = sibling->prev;
1576 }
1577
1578 if (!anchor) {
Michal Vaskoc193ce92020-03-06 11:04:48 +01001579 if (lyd_owner_module(node) == lyd_owner_module(sibling)) {
Michal Vasko0249f7c2020-03-05 16:36:40 +01001580 /* inserting before its module data */
1581 return LY_SUCCESS;
1582 }
1583 }
1584
1585 /* check there are no data of this module */
1586 LY_LIST_FOR(sibling, sibling) {
Michal Vaskoc193ce92020-03-06 11:04:48 +01001587 if (lyd_owner_module(node) == lyd_owner_module(sibling)) {
Michal Vasko0249f7c2020-03-05 16:36:40 +01001588 /* some data of this module found */
1589 LOGERR(sibling->schema->module->ctx, LY_EINVAL, "Top-level data of module \"%s\" already exist,"
Michal Vaskoc193ce92020-03-06 11:04:48 +01001590 " they must be directly connected.", lyd_owner_module(node)->name);
Michal Vasko0249f7c2020-03-05 16:36:40 +01001591 return LY_EINVAL;
1592 }
1593 }
1594
1595 return LY_SUCCESS;
1596}
1597
Michal Vaskob1b5c262020-03-05 14:29:47 +01001598API LY_ERR
Michal Vaskof03ed032020-03-04 13:31:44 +01001599lyd_insert_before(struct lyd_node *sibling, struct lyd_node *node)
1600{
1601 struct lyd_node *iter;
1602
1603 LY_CHECK_ARG_RET(NULL, sibling, node, LY_EINVAL);
1604
Michal Vasko62ed12d2020-05-21 10:08:25 +02001605 LY_CHECK_RET(lyd_insert_check_schema(lysc_data_parent(sibling->schema), node->schema));
Michal Vaskof03ed032020-03-04 13:31:44 +01001606
1607 if (node->schema->flags & LYS_KEY) {
1608 LOGERR(sibling->schema->module->ctx, LY_EINVAL, "Cannot insert key \"%s\".", node->schema->name);
1609 return LY_EINVAL;
1610 } else if (sibling->schema->flags & LYS_KEY) {
1611 LOGERR(sibling->schema->module->ctx, LY_EINVAL, "Cannot insert into keys.");
1612 return LY_EINVAL;
1613 }
1614
Michal Vasko0249f7c2020-03-05 16:36:40 +01001615 LY_CHECK_RET(lyd_insert_after_check_place(sibling->prev->next ? sibling->prev : NULL, sibling, node));
1616
Michal Vaskof03ed032020-03-04 13:31:44 +01001617 if (node->parent || node->prev->next) {
1618 lyd_unlink_tree(node);
1619 }
1620
1621 /* insert in reverse order to get the original order */
1622 node = node->prev;
1623 while (node) {
1624 iter = node->prev;
1625 lyd_unlink_tree(node);
1626
1627 lyd_insert_before_node(sibling, node);
1628 /* move the anchor accordingly */
1629 sibling = node;
1630
1631 node = (iter == node) ? NULL : iter;
1632 }
1633 return LY_SUCCESS;
1634}
1635
1636API LY_ERR
1637lyd_insert_after(struct lyd_node *sibling, struct lyd_node *node)
1638{
1639 struct lyd_node *iter;
1640
1641 LY_CHECK_ARG_RET(NULL, sibling, node, LY_EINVAL);
1642
Michal Vasko62ed12d2020-05-21 10:08:25 +02001643 LY_CHECK_RET(lyd_insert_check_schema(lysc_data_parent(sibling->schema), node->schema));
Michal Vaskof03ed032020-03-04 13:31:44 +01001644
1645 if (node->schema->flags & LYS_KEY) {
1646 LOGERR(sibling->schema->module->ctx, LY_EINVAL, "Cannot insert key \"%s\".", node->schema->name);
1647 return LY_EINVAL;
1648 } else if (sibling->next && (sibling->next->schema->flags & LYS_KEY)) {
1649 LOGERR(sibling->schema->module->ctx, LY_EINVAL, "Cannot insert into keys.");
1650 return LY_EINVAL;
1651 }
1652
Michal Vasko0249f7c2020-03-05 16:36:40 +01001653 LY_CHECK_RET(lyd_insert_after_check_place(sibling, sibling, node));
1654
Michal Vaskof03ed032020-03-04 13:31:44 +01001655 if (node->parent || node->prev->next) {
1656 lyd_unlink_tree(node);
1657 }
1658
1659 while (node) {
1660 iter = node->next;
1661 lyd_unlink_tree(node);
1662
1663 lyd_insert_after_node(sibling, node);
1664 /* move the anchor accordingly */
1665 sibling = node;
1666
1667 node = iter;
1668 }
1669 return LY_SUCCESS;
1670}
1671
1672API void
1673lyd_unlink_tree(struct lyd_node *node)
1674{
1675 struct lyd_node *iter;
1676
1677 if (!node) {
1678 return;
1679 }
1680
1681 /* unlink from siblings */
1682 if (node->prev->next) {
1683 node->prev->next = node->next;
1684 }
1685 if (node->next) {
1686 node->next->prev = node->prev;
1687 } else {
1688 /* unlinking the last node */
1689 if (node->parent) {
1690 iter = node->parent->child;
1691 } else {
1692 iter = node->prev;
1693 while (iter->prev != node) {
1694 iter = iter->prev;
1695 }
1696 }
1697 /* update the "last" pointer from the first node */
1698 iter->prev = node->prev;
1699 }
1700
1701 /* unlink from parent */
1702 if (node->parent) {
1703 if (node->parent->child == node) {
1704 /* the node is the first child */
1705 node->parent->child = node->next;
1706 }
1707
1708 lyd_unlink_hash(node);
1709
1710 /* check for keyless list and update its hash */
1711 for (iter = (struct lyd_node *)node->parent; iter; iter = (struct lyd_node *)iter->parent) {
Michal Vasko413c7f22020-05-05 12:34:06 +02001712 if (iter->schema && (iter->schema->flags & LYS_KEYLESS)) {
Michal Vaskof03ed032020-03-04 13:31:44 +01001713 lyd_hash(iter);
1714 }
1715 }
1716
1717 node->parent = NULL;
1718 }
1719
1720 node->next = NULL;
1721 node->prev = node;
1722}
1723
Michal Vasko90932a92020-02-12 14:33:03 +01001724LY_ERR
Michal Vasko9f96a052020-03-10 09:41:45 +01001725lyd_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 +01001726 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 +01001727 void *prefix_data, LYD_FORMAT format, const struct lysc_node *ctx_snode)
Michal Vasko90932a92020-02-12 14:33:03 +01001728{
1729 LY_ERR ret;
1730 struct lysc_ext_instance *ant = NULL;
Michal Vasko9f96a052020-03-10 09:41:45 +01001731 struct lyd_meta *mt, *last;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001732 LY_ARRAY_COUNT_TYPE u;
Michal Vasko90932a92020-02-12 14:33:03 +01001733
Michal Vasko9f96a052020-03-10 09:41:45 +01001734 assert((parent || meta) && mod);
Michal Vasko6f4cbb62020-02-28 11:15:47 +01001735
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001736 LY_ARRAY_FOR(mod->compiled->exts, u) {
1737 if (mod->compiled->exts[u].def->plugin == lyext_plugins_internal[LYEXT_PLUGIN_INTERNAL_ANNOTATION].plugin &&
1738 !ly_strncmp(mod->compiled->exts[u].argument, name, name_len)) {
Michal Vasko90932a92020-02-12 14:33:03 +01001739 /* we have the annotation definition */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001740 ant = &mod->compiled->exts[u];
Michal Vasko90932a92020-02-12 14:33:03 +01001741 break;
1742 }
1743 }
1744 if (!ant) {
1745 /* attribute is not defined as a metadata annotation (RFC 7952) */
1746 LOGERR(mod->ctx, LY_EINVAL, "Annotation definition for attribute \"%s:%.*s\" not found.",
1747 mod->name, name_len, name);
1748 return LY_EINVAL;
1749 }
1750
Michal Vasko9f96a052020-03-10 09:41:45 +01001751 mt = calloc(1, sizeof *mt);
1752 LY_CHECK_ERR_RET(!mt, LOGMEM(mod->ctx), LY_EMEM);
1753 mt->parent = parent;
1754 mt->annotation = ant;
Michal Vasko52927e22020-03-16 17:26:14 +01001755 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 +01001756 if ((ret != LY_SUCCESS) && (ret != LY_EINCOMPLETE)) {
Michal Vasko9f96a052020-03-10 09:41:45 +01001757 free(mt);
Michal Vasko90932a92020-02-12 14:33:03 +01001758 return ret;
1759 }
Michal Vasko9f96a052020-03-10 09:41:45 +01001760 mt->name = lydict_insert(mod->ctx, name, name_len);
Michal Vasko90932a92020-02-12 14:33:03 +01001761
Michal Vasko6f4cbb62020-02-28 11:15:47 +01001762 /* insert as the last attribute */
1763 if (parent) {
Michal Vasko9f96a052020-03-10 09:41:45 +01001764 if (parent->meta) {
1765 for (last = parent->meta; last->next; last = last->next);
1766 last->next = mt;
Michal Vasko6f4cbb62020-02-28 11:15:47 +01001767 } else {
Michal Vasko9f96a052020-03-10 09:41:45 +01001768 parent->meta = mt;
Michal Vasko6f4cbb62020-02-28 11:15:47 +01001769 }
Michal Vasko9f96a052020-03-10 09:41:45 +01001770 } else if (*meta) {
1771 for (last = *meta; last->next; last = last->next);
1772 last->next = mt;
Michal Vasko90932a92020-02-12 14:33:03 +01001773 }
1774
1775 /* remove default flags from NP containers */
1776 while (parent && (parent->flags & LYD_DEFAULT)) {
1777 parent->flags &= ~LYD_DEFAULT;
1778 parent = (struct lyd_node *)parent->parent;
1779 }
1780
Michal Vasko9f96a052020-03-10 09:41:45 +01001781 if (meta) {
1782 *meta = mt;
Michal Vasko90932a92020-02-12 14:33:03 +01001783 }
1784 return ret;
1785}
1786
Michal Vasko52927e22020-03-16 17:26:14 +01001787LY_ERR
1788ly_create_attr(struct lyd_node *parent, struct ly_attr **attr, const struct ly_ctx *ctx, const char *name,
1789 size_t name_len, const char *value, size_t value_len, int *dynamic, LYD_FORMAT format,
1790 struct ly_prefix *val_prefs, const char *prefix, size_t prefix_len, const char *ns)
1791{
1792 struct ly_attr *at, *last;
1793 struct lyd_node_opaq *opaq;
1794
1795 assert(ctx && (parent || attr) && (!parent || !parent->schema));
1796 assert(name && name_len);
1797 assert((prefix_len && ns) || (!prefix_len && !ns));
1798
1799 if (!value_len) {
1800 value = "";
1801 }
1802
1803 at = calloc(1, sizeof *at);
1804 LY_CHECK_ERR_RET(!at, LOGMEM(ctx), LY_EMEM);
1805 at->parent = (struct lyd_node_opaq *)parent;
1806 at->name = lydict_insert(ctx, name, name_len);
1807 if (dynamic && *dynamic) {
1808 at->value = lydict_insert_zc(ctx, (char *)value);
1809 *dynamic = 0;
1810 } else {
1811 at->value = lydict_insert(ctx, value, value_len);
1812 }
1813
1814 at->format = format;
1815 at->val_prefs = val_prefs;
1816 if (ns) {
1817 at->prefix.pref = lydict_insert(ctx, prefix, prefix_len);
1818 at->prefix.ns = lydict_insert(ctx, ns, 0);
1819 }
1820
1821 /* insert as the last attribute */
1822 if (parent) {
1823 opaq = (struct lyd_node_opaq *)parent;
1824 if (opaq->attr) {
1825 for (last = opaq->attr; last->next; last = last->next);
1826 last->next = at;
1827 } else {
1828 opaq->attr = at;
1829 }
1830 } else if (*attr) {
1831 for (last = *attr; last->next; last = last->next);
1832 last->next = at;
1833 }
1834
1835 if (attr) {
1836 *attr = at;
1837 }
1838 return LY_SUCCESS;
1839}
1840
Radek Krejci084289f2019-07-09 17:35:30 +02001841API const struct lyd_node_term *
Michal Vasko004d3152020-06-11 19:59:22 +02001842lyd_target(const struct ly_path *path, const struct lyd_node *tree)
Radek Krejci084289f2019-07-09 17:35:30 +02001843{
Michal Vasko004d3152020-06-11 19:59:22 +02001844 struct lyd_node *target;
Radek Krejci084289f2019-07-09 17:35:30 +02001845
Michal Vasko004d3152020-06-11 19:59:22 +02001846 if (ly_path_eval(path, tree, &target)) {
1847 return NULL;
Radek Krejci084289f2019-07-09 17:35:30 +02001848 }
1849
Michal Vasko004d3152020-06-11 19:59:22 +02001850 return (struct lyd_node_term *)target;
Radek Krejci084289f2019-07-09 17:35:30 +02001851}
1852
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001853API LY_ERR
1854lyd_compare(const struct lyd_node *node1, const struct lyd_node *node2, int options)
1855{
1856 const struct lyd_node *iter1, *iter2;
1857 struct lyd_node_term *term1, *term2;
1858 struct lyd_node_any *any1, *any2;
Michal Vasko52927e22020-03-16 17:26:14 +01001859 struct lyd_node_opaq *opaq1, *opaq2;
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001860 size_t len1, len2;
Radek Krejci084289f2019-07-09 17:35:30 +02001861
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001862 if (!node1 || !node2) {
1863 if (node1 == node2) {
1864 return LY_SUCCESS;
1865 } else {
1866 return LY_ENOT;
1867 }
1868 }
1869
Michal Vasko52927e22020-03-16 17:26:14 +01001870 if ((LYD_NODE_CTX(node1) != LYD_NODE_CTX(node2)) || (node1->schema != node2->schema)) {
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001871 return LY_ENOT;
1872 }
1873
1874 if (node1->hash != node2->hash) {
1875 return LY_ENOT;
1876 }
Michal Vasko52927e22020-03-16 17:26:14 +01001877 /* 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 +02001878
Michal Vasko52927e22020-03-16 17:26:14 +01001879 if (!node1->schema) {
1880 opaq1 = (struct lyd_node_opaq *)node1;
1881 opaq2 = (struct lyd_node_opaq *)node2;
1882 if ((opaq1->name != opaq2->name) || (opaq1->prefix.ns != opaq2->prefix.ns) || (opaq1->format != opaq2->format)) {
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001883 return LY_ENOT;
1884 }
Michal Vasko52927e22020-03-16 17:26:14 +01001885 switch (opaq1->format) {
1886 case LYD_XML:
1887 if (lyxml_value_compare(opaq1->value, opaq1->val_prefs, opaq2->value, opaq2->val_prefs)) {
1888 return LY_ENOT;
1889 }
1890 break;
1891 case LYD_SCHEMA:
Michal Vasko60ea6352020-06-29 13:39:39 +02001892 case LYD_LYB:
Michal Vasko52927e22020-03-16 17:26:14 +01001893 /* not allowed */
1894 LOGINT(LYD_NODE_CTX(node1));
1895 return LY_EINT;
1896 }
1897 if (options & LYD_COMPARE_FULL_RECURSION) {
1898 iter1 = opaq1->child;
1899 iter2 = opaq2->child;
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001900 goto all_children_compare;
Michal Vasko52927e22020-03-16 17:26:14 +01001901 }
1902 return LY_SUCCESS;
1903 } else {
1904 switch (node1->schema->nodetype) {
1905 case LYS_LEAF:
1906 case LYS_LEAFLIST:
1907 if (options & LYD_COMPARE_DEFAULTS) {
1908 if ((node1->flags & LYD_DEFAULT) != (node2->flags & LYD_DEFAULT)) {
1909 return LY_ENOT;
1910 }
1911 }
1912
Michal Vasko1a2bf2e2020-06-16 13:07:48 +02001913 term1 = (struct lyd_node_term *)node1;
1914 term2 = (struct lyd_node_term *)node2;
1915 if (term1->value.realtype != term2->value.realtype) {
1916 return LY_ENOT;
1917 }
Michal Vasko52927e22020-03-16 17:26:14 +01001918
Michal Vasko1a2bf2e2020-06-16 13:07:48 +02001919 return term1->value.realtype->plugin->compare(&term1->value, &term2->value);
Michal Vasko52927e22020-03-16 17:26:14 +01001920 case LYS_CONTAINER:
1921 if (options & LYD_COMPARE_DEFAULTS) {
1922 if ((node1->flags & LYD_DEFAULT) != (node2->flags & LYD_DEFAULT)) {
1923 return LY_ENOT;
1924 }
1925 }
1926 if (options & LYD_COMPARE_FULL_RECURSION) {
1927 iter1 = ((struct lyd_node_inner*)node1)->child;
1928 iter2 = ((struct lyd_node_inner*)node2)->child;
1929 goto all_children_compare;
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001930 }
1931 return LY_SUCCESS;
Michal Vasko1bf09392020-03-27 12:38:10 +01001932 case LYS_RPC:
Michal Vasko52927e22020-03-16 17:26:14 +01001933 case LYS_ACTION:
1934 if (options & LYD_COMPARE_FULL_RECURSION) {
1935 /* TODO action/RPC
1936 goto all_children_compare;
1937 */
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001938 }
1939 return LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +01001940 case LYS_NOTIF:
1941 if (options & LYD_COMPARE_FULL_RECURSION) {
1942 /* TODO Notification
1943 goto all_children_compare;
1944 */
1945 }
1946 return LY_SUCCESS;
1947 case LYS_LIST:
1948 iter1 = ((struct lyd_node_inner*)node1)->child;
1949 iter2 = ((struct lyd_node_inner*)node2)->child;
1950
1951 if (!(node1->schema->flags & LYS_KEYLESS) && !(options & LYD_COMPARE_FULL_RECURSION)) {
1952 /* lists with keys, their equivalence is based on their keys */
1953 for (struct lysc_node *key = ((struct lysc_node_list*)node1->schema)->child;
Michal Vaskoc57d9a92020-06-23 13:28:27 +02001954 key && (key->flags & LYS_KEY);
Michal Vasko52927e22020-03-16 17:26:14 +01001955 key = key->next) {
1956 if (lyd_compare(iter1, iter2, options)) {
1957 return LY_ENOT;
1958 }
1959 iter1 = iter1->next;
1960 iter2 = iter2->next;
1961 }
1962 } else {
1963 /* lists without keys, their equivalence is based on equivalence of all the children (both direct and indirect) */
1964
1965 all_children_compare:
1966 if (!iter1 && !iter2) {
1967 /* no children, nothing to compare */
1968 return LY_SUCCESS;
1969 }
1970
1971 for (; iter1 && iter2; iter1 = iter1->next, iter2 = iter2->next) {
1972 if (lyd_compare(iter1, iter2, options | LYD_COMPARE_FULL_RECURSION)) {
1973 return LY_ENOT;
1974 }
1975 }
1976 if (iter1 || iter2) {
1977 return LY_ENOT;
1978 }
1979 }
1980 return LY_SUCCESS;
1981 case LYS_ANYXML:
1982 case LYS_ANYDATA:
1983 any1 = (struct lyd_node_any*)node1;
1984 any2 = (struct lyd_node_any*)node2;
1985
1986 if (any1->value_type != any2->value_type) {
1987 return LY_ENOT;
1988 }
1989 switch (any1->value_type) {
1990 case LYD_ANYDATA_DATATREE:
1991 iter1 = any1->value.tree;
1992 iter2 = any2->value.tree;
1993 goto all_children_compare;
1994 case LYD_ANYDATA_STRING:
1995 case LYD_ANYDATA_XML:
1996 case LYD_ANYDATA_JSON:
1997 len1 = strlen(any1->value.str);
1998 len2 = strlen(any2->value.str);
1999 if (len1 != len2 || strcmp(any1->value.str, any2->value.str)) {
2000 return LY_ENOT;
2001 }
2002 return LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +01002003 case LYD_ANYDATA_LYB:
Michal Vasko60ea6352020-06-29 13:39:39 +02002004 len1 = lyd_lyb_data_length(any1->value.mem);
2005 len2 = lyd_lyb_data_length(any2->value.mem);
Michal Vasko52927e22020-03-16 17:26:14 +01002006 if (len1 != len2 || memcmp(any1->value.mem, any2->value.mem, len1)) {
2007 return LY_ENOT;
2008 }
2009 return LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +01002010 }
Radek Krejci1f05b6a2019-07-18 16:15:06 +02002011 }
2012 }
2013
Michal Vasko52927e22020-03-16 17:26:14 +01002014 LOGINT(LYD_NODE_CTX(node1));
Radek Krejci1f05b6a2019-07-18 16:15:06 +02002015 return LY_EINT;
2016}
Radek Krejci22ebdba2019-07-25 13:59:43 +02002017
Michal Vasko21725742020-06-29 11:49:25 +02002018API LY_ERR
2019lyd_compare_meta(const struct lyd_meta *meta1, const struct lyd_meta *meta2)
2020{
2021 if (!meta1 || !meta2) {
2022 if (meta1 == meta2) {
2023 return LY_SUCCESS;
2024 } else {
2025 return LY_ENOT;
2026 }
2027 }
2028
2029 if ((LYD_NODE_CTX(meta1->parent) != LYD_NODE_CTX(meta2->parent)) || (meta1->annotation != meta2->annotation)) {
2030 return LY_ENOT;
2031 }
2032
2033 if (meta1->value.realtype != meta2->value.realtype) {
2034 return LY_ENOT;
2035 }
2036
2037 return meta1->value.realtype->plugin->compare(&meta1->value, &meta2->value);
2038}
2039
Radek Krejci22ebdba2019-07-25 13:59:43 +02002040/**
Michal Vasko52927e22020-03-16 17:26:14 +01002041 * @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 +02002042 *
2043 * 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 +02002044 *
2045 * @param[in] node Original node to duplicate
2046 * @param[in] parent Parent to insert into, NULL for top-level sibling.
2047 * @param[in,out] first First sibling, NULL if no top-level sibling exist yet. Can be also NULL if @p parent is set.
2048 * @param[in] options Bitmask of options flags, see @ref dupoptions.
2049 * @param[out] dup_p Pointer where the created duplicated node is placed (besides connecting it int @p parent / @p first sibling).
2050 * @return LY_ERR value
Radek Krejci22ebdba2019-07-25 13:59:43 +02002051 */
Michal Vasko52927e22020-03-16 17:26:14 +01002052static LY_ERR
2053lyd_dup_recursive(const struct lyd_node *node, struct lyd_node *parent, struct lyd_node **first, int options,
2054 struct lyd_node **dup_p)
Radek Krejci22ebdba2019-07-25 13:59:43 +02002055{
Michal Vasko52927e22020-03-16 17:26:14 +01002056 LY_ERR ret;
Michal Vasko60ea6352020-06-29 13:39:39 +02002057 int len;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002058 struct lyd_node *dup = NULL;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002059 LY_ARRAY_COUNT_TYPE u;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002060
Michal Vasko52927e22020-03-16 17:26:14 +01002061 LY_CHECK_ARG_RET(NULL, node, LY_EINVAL);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002062
Michal Vasko52927e22020-03-16 17:26:14 +01002063 if (!node->schema) {
2064 dup = calloc(1, sizeof(struct lyd_node_opaq));
2065 } else {
2066 switch (node->schema->nodetype) {
Michal Vasko1bf09392020-03-27 12:38:10 +01002067 case LYS_RPC:
Michal Vasko52927e22020-03-16 17:26:14 +01002068 case LYS_ACTION:
2069 case LYS_NOTIF:
2070 case LYS_CONTAINER:
2071 case LYS_LIST:
2072 dup = calloc(1, sizeof(struct lyd_node_inner));
2073 break;
2074 case LYS_LEAF:
2075 case LYS_LEAFLIST:
2076 dup = calloc(1, sizeof(struct lyd_node_term));
2077 break;
2078 case LYS_ANYDATA:
2079 case LYS_ANYXML:
2080 dup = calloc(1, sizeof(struct lyd_node_any));
2081 break;
2082 default:
2083 LOGINT(LYD_NODE_CTX(node));
2084 ret = LY_EINT;
2085 goto error;
2086 }
Radek Krejci22ebdba2019-07-25 13:59:43 +02002087 }
Michal Vasko52927e22020-03-16 17:26:14 +01002088 LY_CHECK_ERR_GOTO(!dup, LOGMEM(LYD_NODE_CTX(node)); ret = LY_EMEM, error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002089
Michal Vaskof6df0a02020-06-16 13:08:34 +02002090 if (options & LYD_DUP_WITH_FLAGS) {
2091 dup->flags = node->flags;
2092 } else {
2093 dup->flags = (node->flags & LYD_DEFAULT) | LYD_NEW;
2094 }
Radek Krejci22ebdba2019-07-25 13:59:43 +02002095 dup->schema = node->schema;
Michal Vasko52927e22020-03-16 17:26:14 +01002096 dup->prev = dup;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002097
2098 /* TODO duplicate attributes, implement LYD_DUP_NO_ATTR */
2099
2100 /* nodetype-specific work */
Michal Vasko52927e22020-03-16 17:26:14 +01002101 if (!dup->schema) {
2102 struct lyd_node_opaq *opaq = (struct lyd_node_opaq *)dup;
2103 struct lyd_node_opaq *orig = (struct lyd_node_opaq *)node;
2104 struct lyd_node *child;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002105
2106 if (options & LYD_DUP_RECURSIVE) {
2107 /* duplicate all the children */
2108 LY_LIST_FOR(orig->child, child) {
Michal Vasko52927e22020-03-16 17:26:14 +01002109 LY_CHECK_GOTO(ret = lyd_dup_recursive(child, dup, NULL, options, NULL), error);
2110 }
2111 }
2112 opaq->name = lydict_insert(LYD_NODE_CTX(node), orig->name, 0);
2113 opaq->format = orig->format;
2114 if (orig->prefix.pref) {
2115 opaq->prefix.pref = lydict_insert(LYD_NODE_CTX(node), orig->prefix.pref, 0);
2116 }
2117 if (orig->prefix.ns) {
2118 opaq->prefix.ns = lydict_insert(LYD_NODE_CTX(node), orig->prefix.ns, 0);
2119 }
2120 if (orig->val_prefs) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002121 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 +01002122 LY_ARRAY_FOR(orig->val_prefs, u) {
2123 opaq->val_prefs[u].pref = lydict_insert(LYD_NODE_CTX(node), orig->val_prefs[u].pref, 0);
2124 opaq->val_prefs[u].ns = lydict_insert(LYD_NODE_CTX(node), orig->val_prefs[u].ns, 0);
2125 LY_ARRAY_INCREMENT(opaq->val_prefs);
2126 }
2127 }
2128 opaq->value = lydict_insert(LYD_NODE_CTX(node), orig->value, 0);
2129 opaq->ctx = orig->ctx;
2130 } else if (dup->schema->nodetype & LYD_NODE_TERM) {
2131 struct lyd_node_term *term = (struct lyd_node_term *)dup;
2132 struct lyd_node_term *orig = (struct lyd_node_term *)node;
2133
2134 term->hash = orig->hash;
2135 term->value.realtype = orig->value.realtype;
2136 LY_CHECK_ERR_GOTO(term->value.realtype->plugin->duplicate(LYD_NODE_CTX(node), &orig->value, &term->value),
2137 LOGERR(LYD_NODE_CTX(node), LY_EINT, "Value duplication failed."); ret = LY_EINT, error);
2138 } else if (dup->schema->nodetype & LYD_NODE_INNER) {
2139 struct lyd_node_inner *orig = (struct lyd_node_inner *)node;
2140 struct lyd_node *child;
2141
2142 if (options & LYD_DUP_RECURSIVE) {
2143 /* duplicate all the children */
2144 LY_LIST_FOR(orig->child, child) {
2145 LY_CHECK_GOTO(ret = lyd_dup_recursive(child, dup, NULL, options, NULL), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002146 }
Radek Krejci0fe9b512019-07-26 17:51:05 +02002147 } else if (dup->schema->nodetype == LYS_LIST && !(dup->schema->flags & LYS_KEYLESS)) {
Radek Krejci22ebdba2019-07-25 13:59:43 +02002148 /* always duplicate keys of a list */
Radek Krejci22ebdba2019-07-25 13:59:43 +02002149 child = orig->child;
Michal Vasko52927e22020-03-16 17:26:14 +01002150 for (struct lysc_node *key = ((struct lysc_node_list *)dup->schema)->child;
Michal Vaskoc57d9a92020-06-23 13:28:27 +02002151 key && (key->flags & LYS_KEY);
Radek Krejci0fe9b512019-07-26 17:51:05 +02002152 key = key->next) {
Radek Krejci22ebdba2019-07-25 13:59:43 +02002153 if (!child) {
2154 /* possibly not keys are present in filtered tree */
2155 break;
Radek Krejci0fe9b512019-07-26 17:51:05 +02002156 } else if (child->schema != key) {
2157 /* possibly not all keys are present in filtered tree,
2158 * but there can be also some non-key nodes */
2159 continue;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002160 }
Michal Vasko52927e22020-03-16 17:26:14 +01002161 LY_CHECK_GOTO(ret = lyd_dup_recursive(child, dup, NULL, options, NULL), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002162 child = child->next;
2163 }
2164 }
2165 lyd_hash(dup);
2166 } else if (dup->schema->nodetype & LYD_NODE_ANY) {
Michal Vasko52927e22020-03-16 17:26:14 +01002167 struct lyd_node_any *any = (struct lyd_node_any *)dup;
2168 struct lyd_node_any *orig = (struct lyd_node_any *)node;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002169
2170 any->hash = orig->hash;
2171 any->value_type = orig->value_type;
2172 switch (any->value_type) {
2173 case LYD_ANYDATA_DATATREE:
2174 if (orig->value.tree) {
2175 any->value.tree = lyd_dup(orig->value.tree, NULL, LYD_DUP_RECURSIVE | LYD_DUP_WITH_SIBLINGS);
Radek Krejci25dc3432020-05-15 14:42:12 +02002176 if (!any->value.tree) {
2177 /* get the last error's error code recorded by lyd_dup */
2178 struct ly_err_item *ei = ly_err_first(LYD_NODE_CTX(node));
2179 ret = ei ? ei->prev->no : LY_EOTHER;
2180 goto error;
2181 }
Michal Vasko60ea6352020-06-29 13:39:39 +02002182 LY_CHECK_ERR_GOTO(!any->value.tree, ret = 0, error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002183 }
2184 break;
2185 case LYD_ANYDATA_STRING:
2186 case LYD_ANYDATA_XML:
2187 case LYD_ANYDATA_JSON:
2188 if (orig->value.str) {
Michal Vasko52927e22020-03-16 17:26:14 +01002189 any->value.str = lydict_insert(LYD_NODE_CTX(node), orig->value.str, strlen(orig->value.str));
Radek Krejci22ebdba2019-07-25 13:59:43 +02002190 }
2191 break;
Michal Vasko60ea6352020-06-29 13:39:39 +02002192 case LYD_ANYDATA_LYB:
2193 if (orig->value.mem) {
2194 len = lyd_lyb_data_length(orig->value.mem);
2195 any->value.mem = malloc(len);
2196 LY_CHECK_ERR_GOTO(!any->value.mem, LOGMEM(LYD_NODE_CTX(node)); ret = LY_EMEM, error);
2197 memcpy(any->value.mem, orig->value.mem, len);
2198 }
2199 break;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002200 }
2201 }
2202
Michal Vasko52927e22020-03-16 17:26:14 +01002203 /* insert */
2204 lyd_insert_node(parent, first, dup);
Michal Vasko52927e22020-03-16 17:26:14 +01002205
2206 if (dup_p) {
2207 *dup_p = dup;
2208 }
2209 return LY_SUCCESS;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002210
2211error:
Michal Vasko52927e22020-03-16 17:26:14 +01002212 lyd_free_tree(dup);
2213 return ret;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002214}
2215
2216API struct lyd_node *
2217lyd_dup(const struct lyd_node *node, struct lyd_node_inner *parent, int options)
2218{
2219 struct ly_ctx *ctx;
2220 const struct lyd_node *orig; /* original node to be duplicated */
2221 struct lyd_node *first = NULL; /* the first duplicated node, this is returned */
Radek Krejci22ebdba2019-07-25 13:59:43 +02002222 struct lyd_node *top = NULL; /* the most higher created node */
2223 struct lyd_node_inner *local_parent = NULL; /* the direct parent node for the duplicated node(s) */
2224 int keyless_parent_list = 0;
2225
2226 LY_CHECK_ARG_RET(NULL, node, NULL);
2227 ctx = node->schema->module->ctx;
2228
2229 if (options & LYD_DUP_WITH_PARENTS) {
2230 struct lyd_node_inner *orig_parent, *iter;
2231 int repeat = 1;
2232 for (top = NULL, orig_parent = node->parent; repeat && orig_parent; orig_parent = orig_parent->parent) {
2233 if (parent && parent->schema == orig_parent->schema) {
2234 /* stop creating parents, connect what we have into the provided parent */
2235 iter = parent;
2236 repeat = 0;
2237 /* get know if there is a keyless list which we will have to rehash */
2238 for (struct lyd_node_inner *piter = parent; piter; piter = piter->parent) {
Radek Krejci0fe9b512019-07-26 17:51:05 +02002239 if (piter->schema->nodetype == LYS_LIST && (piter->schema->flags & LYS_KEYLESS)) {
Radek Krejci22ebdba2019-07-25 13:59:43 +02002240 keyless_parent_list = 1;
2241 break;
2242 }
2243 }
2244 } else {
Michal Vasko52927e22020-03-16 17:26:14 +01002245 iter = NULL;
2246 LY_CHECK_GOTO(lyd_dup_recursive((struct lyd_node *)orig_parent, NULL, (struct lyd_node **)&iter, 0,
2247 (struct lyd_node **)&iter), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002248 }
2249 if (!local_parent) {
2250 local_parent = iter;
2251 }
2252 if (iter->child) {
2253 /* 1) list - add after keys
2254 * 2) provided parent with some children */
2255 iter->child->prev->next = top;
2256 if (top) {
2257 top->prev = iter->child->prev;
2258 iter->child->prev = top;
2259 }
2260 } else {
2261 iter->child = top;
2262 if (iter->schema->nodetype == LYS_LIST) {
2263 /* keyless list - we will need to rehash it since we are going to add nodes into it */
2264 keyless_parent_list = 1;
2265 }
2266 }
2267 if (top) {
2268 top->parent = iter;
2269 }
2270 top = (struct lyd_node*)iter;
2271 }
2272 if (repeat && parent) {
2273 /* given parent and created parents chain actually do not interconnect */
2274 LOGERR(ctx, LY_EINVAL, "Invalid argument parent (%s()) - does not interconnect with the created node's parents chain.", __func__);
2275 goto error;
2276 }
2277 } else {
2278 local_parent = parent;
2279 }
2280
Radek Krejci22ebdba2019-07-25 13:59:43 +02002281 LY_LIST_FOR(node, orig) {
Michal Vasko52927e22020-03-16 17:26:14 +01002282 /* if there is no local parent, it will be inserted into first */
2283 LY_CHECK_GOTO(lyd_dup_recursive(orig, (struct lyd_node *)local_parent, &first, options, first ? NULL : &first), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002284 if (!(options & LYD_DUP_WITH_SIBLINGS)) {
2285 break;
2286 }
2287 }
2288 if (keyless_parent_list) {
2289 /* rehash */
2290 for (; local_parent; local_parent = local_parent->parent) {
Radek Krejci0fe9b512019-07-26 17:51:05 +02002291 if (local_parent->schema->nodetype == LYS_LIST && (local_parent->schema->flags & LYS_KEYLESS)) {
Radek Krejci22ebdba2019-07-25 13:59:43 +02002292 lyd_hash((struct lyd_node*)local_parent);
2293 }
2294 }
2295 }
2296 return first;
2297
2298error:
2299 if (top) {
2300 lyd_free_tree(top);
2301 } else {
Michal Vaskof03ed032020-03-04 13:31:44 +01002302 lyd_free_siblings(first);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002303 }
2304 return NULL;
2305}
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002306
Michal Vasko4490d312020-06-16 13:08:55 +02002307/**
2308 * @brief Merge a source sibling into target siblings.
2309 *
2310 * @param[in,out] first_trg First target sibling, is updated if top-level.
2311 * @param[in] parent_trg Target parent.
2312 * @param[in,out] sibling_src Source sibling to merge, set to NULL if spent.
2313 * @param[in] options Merge options.
2314 * @return LY_ERR value.
2315 */
2316static LY_ERR
2317lyd_merge_sibling_r(struct lyd_node **first_trg, struct lyd_node *parent_trg, const struct lyd_node **sibling_src_p,
2318 int options)
2319{
2320 LY_ERR ret;
2321 const struct lyd_node *child_src, *tmp, *sibling_src;
2322 struct lyd_node *match_trg, *dup_src, *next, *elem;
2323 struct lysc_type *type;
2324 LYD_ANYDATA_VALUETYPE tmp_val_type;
2325 union lyd_any_value tmp_val;
2326
2327 sibling_src = *sibling_src_p;
2328 if (sibling_src->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) {
2329 /* try to find the exact instance */
2330 ret = lyd_find_sibling_first(*first_trg, sibling_src, &match_trg);
2331 } else {
2332 /* try to simply find the node, there cannot be more instances */
2333 ret = lyd_find_sibling_val(*first_trg, sibling_src->schema, NULL, 0, &match_trg);
2334 }
2335
2336 if (!ret) {
2337 /* node found, make sure even value matches for all node types */
2338 if ((match_trg->schema->nodetype == LYS_LEAF) && lyd_compare(sibling_src, match_trg, LYD_COMPARE_DEFAULTS)) {
2339 /* since they are different, they cannot both be default */
2340 assert(!(sibling_src->flags & LYD_DEFAULT) || !(match_trg->flags & LYD_DEFAULT));
2341
2342 /* update value (or only LYD_DEFAULT flag) only if no flag set or the source node is not default */
2343 if (!(options & LYD_MERGE_EXPLICIT) || !(sibling_src->flags & LYD_DEFAULT)) {
2344 type = ((struct lysc_node_leaf *)match_trg->schema)->type;
2345 type->plugin->free(LYD_NODE_CTX(match_trg), &((struct lyd_node_term *)match_trg)->value);
2346 LY_CHECK_RET(type->plugin->duplicate(LYD_NODE_CTX(match_trg), &((struct lyd_node_term *)sibling_src)->value,
2347 &((struct lyd_node_term *)match_trg)->value));
2348
2349 /* copy flags and add LYD_NEW */
2350 match_trg->flags = sibling_src->flags | LYD_NEW;
2351 }
2352 } else if ((match_trg->schema->nodetype & LYS_ANYDATA) && lyd_compare(sibling_src, match_trg, 0)) {
2353 if (options & LYD_MERGE_DESTRUCT) {
2354 dup_src = (struct lyd_node *)sibling_src;
2355 lyd_unlink_tree(dup_src);
2356 /* spend it */
2357 *sibling_src_p = NULL;
2358 } else {
2359 dup_src = lyd_dup(sibling_src, NULL, 0);
2360 LY_CHECK_RET(!dup_src, LY_EMEM);
2361 }
2362 /* just switch values */
2363 tmp_val_type = ((struct lyd_node_any *)match_trg)->value_type;
2364 tmp_val = ((struct lyd_node_any *)match_trg)->value;
2365 ((struct lyd_node_any *)match_trg)->value_type = ((struct lyd_node_any *)sibling_src)->value_type;
2366 ((struct lyd_node_any *)match_trg)->value = ((struct lyd_node_any *)sibling_src)->value;
2367 ((struct lyd_node_any *)sibling_src)->value_type = tmp_val_type;
2368 ((struct lyd_node_any *)sibling_src)->value = tmp_val;
2369
2370 /* copy flags and add LYD_NEW */
2371 match_trg->flags = sibling_src->flags | LYD_NEW;
2372
2373 /* dup_src is not needed, actually */
2374 lyd_free_tree(dup_src);
2375 } else {
2376 /* check descendants, recursively */
Michal Vasko5bfd4be2020-06-23 13:26:19 +02002377 LY_LIST_FOR_SAFE(LYD_CHILD(sibling_src), tmp, child_src) {
Michal Vasko4490d312020-06-16 13:08:55 +02002378 LY_CHECK_RET(lyd_merge_sibling_r(lyd_node_children_p(match_trg), match_trg, &child_src, options));
2379 }
2380 }
2381 } else {
2382 /* node not found, merge it */
2383 if (options & LYD_MERGE_DESTRUCT) {
2384 dup_src = (struct lyd_node *)sibling_src;
2385 lyd_unlink_tree(dup_src);
2386 /* spend it */
2387 *sibling_src_p = NULL;
2388 } else {
2389 dup_src = lyd_dup(sibling_src, NULL, LYD_DUP_RECURSIVE | LYD_DUP_WITH_FLAGS);
2390 LY_CHECK_RET(!dup_src, LY_EMEM);
2391 }
2392
2393 /* set LYD_NEW for all the new nodes, required for validation */
2394 LYD_TREE_DFS_BEGIN(dup_src, next, elem) {
2395 elem->flags |= LYD_NEW;
2396 LYD_TREE_DFS_END(dup_src, next, elem);
2397 }
2398
2399 lyd_insert_node(parent_trg, first_trg, dup_src);
2400 }
2401
2402 return LY_SUCCESS;
2403}
2404
2405API LY_ERR
2406lyd_merge(struct lyd_node **target, const struct lyd_node *source, int options)
2407{
2408 const struct lyd_node *sibling_src, *tmp;
2409 int first;
2410
2411 LY_CHECK_ARG_RET(NULL, target, LY_EINVAL);
2412
2413 if (!source) {
2414 /* nothing to merge */
2415 return LY_SUCCESS;
2416 }
2417
2418 if (lysc_data_parent((*target)->schema) || lysc_data_parent(source->schema)) {
2419 LOGERR(LYD_NODE_CTX(source), LY_EINVAL, "Invalid arguments - can merge only 2 top-level subtrees (%s()).", __func__);
2420 return LY_EINVAL;
2421 }
2422
2423 LY_LIST_FOR_SAFE(source, tmp, sibling_src) {
2424 first = sibling_src == source ? 1 : 0;
2425 LY_CHECK_RET(lyd_merge_sibling_r(target, NULL, &sibling_src, options));
2426 if (first && !sibling_src) {
2427 /* source was spent (unlinked), move to the next node */
2428 source = tmp;
2429 }
2430
2431 if (options & LYD_MERGE_NOSIBLINGS) {
2432 break;
2433 }
2434 }
2435
2436 if (options & LYD_MERGE_DESTRUCT) {
2437 /* free any leftover source data that were not merged */
2438 lyd_free_siblings((struct lyd_node *)source);
2439 }
2440
2441 return LY_SUCCESS;
2442}
2443
Michal Vaskoe893ddd2020-06-23 13:35:20 +02002444struct lyd_diff_userord {
2445 const struct lysc_node *schema; /**< User-ordered list/leaf-list schema node. */
2446 uint64_t pos; /**< Current position in the second tree. */
2447 const struct lyd_node **inst; /**< Sized array of current instance order. */
2448};
2449
2450enum lyd_diff_op {
2451 LYD_DIFF_OP_CREATE, /**< Subtree created. */
2452 LYD_DIFF_OP_DELETE, /**< Subtree deleted. */
2453 LYD_DIFF_OP_REPLACE, /**< Node value changed or (leaf-)list instance moved. */
2454 LYD_DIFF_OP_NONE, /**< No change of an existing inner node or default flag change of a term node. */
2455};
2456
2457static const char *
2458lyd_diff_op2str(enum lyd_diff_op op)
2459{
2460 switch (op) {
2461 case LYD_DIFF_OP_CREATE:
2462 return "create";
2463 case LYD_DIFF_OP_DELETE:
2464 return "delete";
2465 case LYD_DIFF_OP_REPLACE:
2466 return "replace";
2467 case LYD_DIFF_OP_NONE:
2468 return "none";
2469 }
2470
2471 LOGINT(NULL);
2472 return NULL;
2473}
2474
2475/**
2476 * @brief Add a new change into diff.
2477 *
2478 * @param[in] node Node (subtree) to add into diff.
2479 * @param[in] op Operation to set.
2480 * @param[in] orig_default Original default metadata to set.
2481 * @param[in] orig_value Original value metadata to set.
2482 * @param[in] key Key metadata to set.
2483 * @param[in] value Value metadata to set.
2484 * @param[in] orig_key Original key metadata to set.
2485 * @param[in,out] diff Diff to append to.
2486 * @return LY_ERR value.
2487 */
2488static LY_ERR
2489lyd_diff_add(const struct lyd_node *node, enum lyd_diff_op op, const char *orig_default, const char *orig_value,
2490 const char *key, const char *value, const char *orig_key, struct lyd_node **diff)
2491{
2492 struct lyd_node *dup, *siblings, *match = NULL, *diff_parent = NULL;
2493 const struct lyd_node *parent = NULL;
2494 const struct lys_module *yang_mod;
2495
2496 /* find the first existing parent */
2497 siblings = *diff;
2498 while (1) {
2499 /* find next node parent */
2500 parent = node;
2501 while (parent->parent && (!diff_parent || (parent->parent->schema != diff_parent->schema))) {
2502 parent = (struct lyd_node *)parent->parent;
2503 }
2504 if (parent == node) {
2505 /* no more parents to find */
2506 break;
2507 }
2508
2509 /* check whether it exists in the diff */
2510 if (lyd_find_sibling_first(siblings, parent, &match)) {
2511 break;
2512 }
2513
2514 /* another parent found */
2515 diff_parent = match;
2516
2517 /* move down in the diff */
2518 siblings = LYD_CHILD(match);
2519 }
2520
2521 /* duplicate the subtree (and connect to the diff if possible) */
2522 dup = lyd_dup(node, (struct lyd_node_inner *)diff_parent, LYD_DUP_RECURSIVE | LYD_DUP_NO_META | LYD_DUP_WITH_PARENTS);
2523 LY_CHECK_RET(!dup, LY_EMEM);
2524
2525 /* find the first duplicated parent */
2526 if (!diff_parent) {
2527 diff_parent = (struct lyd_node *)dup->parent;
2528 while (diff_parent && diff_parent->parent) {
2529 diff_parent = (struct lyd_node *)diff_parent->parent;
2530 }
2531 } else {
2532 diff_parent = (struct lyd_node *)dup;
2533 while (diff_parent->parent && (diff_parent->parent->schema == parent->schema)) {
2534 diff_parent = (struct lyd_node *)diff_parent->parent;
2535 }
2536 }
2537
2538 /* no parent existed, must be manually connected */
2539 if (!diff_parent) {
2540 /* there actually was no parent to duplicate */
2541 if (*diff) {
2542 lyd_insert_sibling(*diff, dup);
2543 } else {
2544 *diff = dup;
2545 }
2546 } else if (!diff_parent->parent) {
2547 if (*diff) {
2548 lyd_insert_sibling(*diff, diff_parent);
2549 } else {
2550 *diff = diff_parent;
2551 }
2552 }
2553
2554 /* get module with the operation metadata */
2555 yang_mod = LYD_NODE_CTX(node)->list.objs[1];
2556 assert(!strcmp(yang_mod->name, "yang"));
2557
2558 /* add parent operation, if any */
2559 if (diff_parent && (diff_parent != dup) && !lyd_new_meta(diff_parent, yang_mod, "operation", "none")) {
2560 return LY_EMEM;
2561 }
2562
2563 /* add subtree operation */
2564 if (!lyd_new_meta(dup, yang_mod, "operation", lyd_diff_op2str(op))) {
2565 return LY_EMEM;
2566 }
2567
2568 /* orig-default */
2569 if (orig_default && !lyd_new_meta(dup, yang_mod, "orig-default", orig_default)) {
2570 return LY_EMEM;
2571 }
2572
2573 /* orig-value */
2574 if (orig_value && !lyd_new_meta(dup, yang_mod, "orig-value", orig_value)) {
2575 return LY_EMEM;
2576 }
2577
2578 /* key */
2579 if (key && !lyd_new_meta(dup, yang_mod, "key", key)) {
2580 return LY_EMEM;
2581 }
2582
2583 /* value */
2584 if (value && !lyd_new_meta(dup, yang_mod, "value", value)) {
2585 return LY_EMEM;
2586 }
2587
2588 /* orig-key */
2589 if (orig_key && !lyd_new_meta(dup, yang_mod, "orig-key", orig_key)) {
2590 return LY_EMEM;
2591 }
2592
2593 return LY_SUCCESS;
2594}
2595
2596/**
2597 * @brief Get a userord entry for a specific user-ordered list/leaf-list. Create if does not exist yet.
2598 *
2599 * @param[in] first
2600 * @param[in] schema Schema node of the list/leaf-list.
2601 * @param[in,out] userord Sized array of userord items.
2602 * @return Userord item for all the user-ordered list/leaf-list instances.
2603 */
2604static struct lyd_diff_userord *
2605lyd_diff_userord_get(const struct lyd_node *first, const struct lysc_node *schema, struct lyd_diff_userord **userord)
2606{
2607 struct lyd_diff_userord *item;
2608 const struct lyd_node *iter, **node;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002609 LY_ARRAY_COUNT_TYPE u;
Michal Vaskoe893ddd2020-06-23 13:35:20 +02002610
2611 LY_ARRAY_FOR(*userord, u) {
2612 if ((*userord)[u].schema == schema) {
2613 return &(*userord)[u];
2614 }
2615 }
2616
2617 /* it was not added yet, add it now */
2618 LY_ARRAY_NEW_RET(schema->module->ctx, *userord, item, NULL);
2619
2620 item->schema = schema;
2621 item->pos = 0;
2622 item->inst = NULL;
2623
2624 /* store all the instance pointers in the current order */
2625 if (first) {
2626 if (first->parent) {
2627 iter = first->parent->child;
2628 } else {
2629 for (iter = first; iter->prev->next; iter = iter->prev);
2630 }
2631 for (; iter; iter = iter->next) {
2632 if (iter->schema == first->schema) {
2633 LY_ARRAY_NEW_RET(schema->module->ctx, item->inst, node, NULL);
2634 *node = iter;
2635 }
2636 }
2637 }
2638
2639 return item;
2640}
2641
2642/**
2643 * @brief Get all the metadata to be stored in a diff for the 2 nodes. Can be used only for user-ordered
2644 * lists/leaf-lists.
2645 *
2646 * @param[in] first Node from the first tree, can be NULL (on create).
2647 * @param[in] second Node from the second tree, can be NULL (on delete).
2648 * @param[in] options Diff options.
2649 * @param[in,out] userord Sized array of userord items for keeping the current node order.
2650 * @param[out] op Operation.
2651 * @param[out] orig_default Original default metadata.
2652 * @param[out] value Value metadata.
2653 * @param[out] orig_value Original value metadata
2654 * @param[out] key Key metadata.
2655 * @param[out] orig_key Original key metadata.
2656 * @return LY_SUCCESS on success,
2657 * @return LY_ENOT if there is no change to be added into diff,
2658 * @return LY_ERR value on other errors.
2659 */
2660static LY_ERR
2661lyd_diff_userord_attrs(const struct lyd_node *first, const struct lyd_node *second, int options,
2662 struct lyd_diff_userord **userord, enum lyd_diff_op *op, const char **orig_default, char **value,
2663 char **orig_value, char **key, char **orig_key)
2664{
2665 const struct lysc_node *schema;
2666 int dynamic;
2667 size_t buflen, bufused, first_pos, second_pos;
2668 struct lyd_diff_userord *userord_item;
2669
2670 assert(first || second);
2671
2672 *orig_default = NULL;
2673 *value = NULL;
2674 *orig_value = NULL;
2675 *key = NULL;
2676 *orig_key = NULL;
2677
2678 schema = first ? first->schema : second->schema;
2679 assert(lysc_is_userordered(schema));
2680
2681 /* get userord entry */
2682 userord_item = lyd_diff_userord_get(first, schema, userord);
2683 LY_CHECK_RET(!userord_item, LY_EMEM);
2684
2685 /* prepare position of the next instance */
2686 second_pos = userord_item->pos++;
2687
2688 /* find user-ordered first position */
2689 if (first) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002690 for (first_pos = second_pos; first_pos < LY_ARRAY_COUNT(userord_item->inst); ++first_pos) {
Michal Vaskoe893ddd2020-06-23 13:35:20 +02002691 if (userord_item->inst[first_pos] == first) {
2692 break;
2693 }
2694 }
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002695 assert(first_pos < LY_ARRAY_COUNT(userord_item->inst));
Michal Vaskoe893ddd2020-06-23 13:35:20 +02002696 }
2697
2698 /* learn operation first */
2699 if (!second) {
2700 *op = LYD_DIFF_OP_DELETE;
2701 } else if (!first) {
2702 *op = LYD_DIFF_OP_CREATE;
2703 } else {
2704 assert(schema->nodetype & (LYS_LIST | LYS_LEAFLIST));
2705 if (lyd_compare(second, userord_item->inst[second_pos], 0)) {
2706 /* in first, there is a different instance on the second position, we are going to move 'first' node */
2707 *op = LYD_DIFF_OP_REPLACE;
2708 } else if ((options & LYD_DIFF_WITHDEFAULTS) && ((first->flags & LYD_DEFAULT) != (second->flags & LYD_DEFAULT))) {
2709 /* default flag change */
2710 *op = LYD_DIFF_OP_NONE;
2711 } else {
2712 /* no changes */
2713 return LY_ENOT;
2714 }
2715 }
2716
2717 /*
2718 * set each attribute correctly based on the operation and node type
2719 */
2720
2721 /* orig-default */
2722 if ((options & LYD_DIFF_WITHDEFAULTS) && (schema->nodetype == LYS_LEAFLIST)
2723 && ((*op == LYD_DIFF_OP_REPLACE) || (*op == LYD_DIFF_OP_NONE))) {
2724 if (first->flags & LYD_DEFAULT) {
2725 *orig_default = "true";
2726 } else {
2727 *orig_default = "false";
2728 }
2729 }
2730
2731 /* value */
2732 if ((schema->nodetype == LYS_LEAFLIST) && ((*op == LYD_DIFF_OP_REPLACE) || (*op == LYD_DIFF_OP_CREATE))) {
2733 if (second_pos) {
2734 *value = (char *)lyd_value2str((struct lyd_node_term *)userord_item->inst[second_pos - 1], &dynamic);
2735 if (!dynamic) {
2736 *value = strdup(*value);
2737 LY_CHECK_ERR_RET(!*value, LOGMEM(schema->module->ctx), LY_EMEM);
2738 }
2739 } else {
2740 *value = strdup("");
2741 LY_CHECK_ERR_RET(!*value, LOGMEM(schema->module->ctx), LY_EMEM);
2742 }
2743 }
2744
2745 /* orig-value */
2746 if ((schema->nodetype == LYS_LEAFLIST) && ((*op == LYD_DIFF_OP_REPLACE) || (*op == LYD_DIFF_OP_DELETE))) {
2747 if (first_pos) {
2748 *orig_value = (char *)lyd_value2str((struct lyd_node_term *)userord_item->inst[first_pos - 1], &dynamic);
2749 if (!dynamic) {
2750 *orig_value = strdup(*orig_value);
2751 LY_CHECK_ERR_RET(!*orig_value, LOGMEM(schema->module->ctx), LY_EMEM);
2752 }
2753 } else {
2754 *orig_value = strdup("");
2755 LY_CHECK_ERR_RET(!*orig_value, LOGMEM(schema->module->ctx), LY_EMEM);
2756 }
2757 }
2758
2759 /* key */
2760 if ((schema->nodetype == LYS_LIST) && ((*op == LYD_DIFF_OP_REPLACE) || (*op ==LYD_DIFF_OP_CREATE))) {
2761 if (second_pos) {
2762 buflen = bufused = 0;
2763 LY_CHECK_RET(lyd_path_list_predicate(userord_item->inst[second_pos - 1], key, &buflen, &bufused, 0));
2764 } else {
2765 *key = strdup("");
2766 LY_CHECK_ERR_RET(!*key, LOGMEM(schema->module->ctx), LY_EMEM);
2767 }
2768 }
2769
2770 /* orig-key */
2771 if ((schema->nodetype == LYS_LIST) && ((*op == LYD_DIFF_OP_REPLACE) || (*op == LYD_DIFF_OP_DELETE))) {
2772 if (first_pos) {
2773 buflen = bufused = 0;
2774 LY_CHECK_RET(lyd_path_list_predicate(userord_item->inst[first_pos - 1], orig_key, &buflen, &bufused, 0));
2775 } else {
2776 *orig_key = strdup("");
2777 LY_CHECK_ERR_RET(!*orig_key, LOGMEM(schema->module->ctx), LY_EMEM);
2778 }
2779 }
2780
2781 /*
2782 * update our instances - apply the change
2783 */
2784 if (*op == LYD_DIFF_OP_CREATE) {
2785 /* insert the instance */
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002786 LY_ARRAY_RESIZE_ERR_RET(schema->module->ctx, userord_item->inst, LY_ARRAY_COUNT(userord_item->inst) + 1,
Michal Vaskoe893ddd2020-06-23 13:35:20 +02002787 ;, LY_EMEM);
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002788 if (second_pos < LY_ARRAY_COUNT(userord_item->inst)) {
Michal Vaskoe893ddd2020-06-23 13:35:20 +02002789 memmove(userord_item->inst + second_pos + 1, userord_item->inst + second_pos,
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002790 (LY_ARRAY_COUNT(userord_item->inst) - second_pos) * sizeof *userord_item->inst);
Michal Vaskoe893ddd2020-06-23 13:35:20 +02002791 }
2792 LY_ARRAY_INCREMENT(userord_item->inst);
2793 userord_item->inst[second_pos] = second;
2794
2795 } else if (*op == LYD_DIFF_OP_DELETE) {
2796 /* remove the instance */
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002797 if (first_pos + 1 < LY_ARRAY_COUNT(userord_item->inst)) {
Michal Vaskoe893ddd2020-06-23 13:35:20 +02002798 memmove(userord_item->inst + first_pos, userord_item->inst + first_pos + 1,
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002799 (LY_ARRAY_COUNT(userord_item->inst) - first_pos - 1) * sizeof *userord_item->inst);
Michal Vaskoe893ddd2020-06-23 13:35:20 +02002800 }
2801 LY_ARRAY_DECREMENT(userord_item->inst);
2802
2803 } else if (*op == LYD_DIFF_OP_REPLACE) {
2804 /* move the instances */
2805 memmove(userord_item->inst + second_pos + 1, userord_item->inst + second_pos,
2806 (first_pos - second_pos) * sizeof *userord_item->inst);
2807 userord_item->inst[second_pos] = first;
2808 }
2809
2810 return LY_SUCCESS;
2811}
2812
2813/**
2814 * @brief Get all the metadata to be stored in a diff for the 2 nodes. Cannot be used for user-ordered
2815 * lists/leaf-lists.
2816 *
2817 * @param[in] first Node from the first tree, can be NULL (on create).
2818 * @param[in] second Node from the second tree, can be NULL (on delete).
2819 * @param[in] options Diff options.
2820 * @param[out] op Operation.
2821 * @param[out] orig_default Original default metadata.
2822 * @param[out] orig_value Original value metadata.
2823 * @return LY_SUCCESS on success,
2824 * @return LY_ENOT if there is no change to be added into diff,
2825 * @return LY_ERR value on other errors.
2826 */
2827static LY_ERR
2828lyd_diff_attrs(const struct lyd_node *first, const struct lyd_node *second, int options, enum lyd_diff_op *op,
2829 const char **orig_default, char **orig_value)
2830{
2831 const struct lysc_node *schema;
2832 int dynamic;
2833
2834 assert(first || second);
2835
2836 *orig_default = NULL;
2837 *orig_value = NULL;
2838
2839 schema = first ? first->schema : second->schema;
2840 assert(!lysc_is_userordered(schema));
2841
2842 /* learn operation first */
2843 if (!second) {
2844 *op = LYD_DIFF_OP_DELETE;
2845 } else if (!first) {
2846 *op = LYD_DIFF_OP_CREATE;
2847 } else {
2848 switch (schema->nodetype) {
2849 case LYS_CONTAINER:
2850 case LYS_RPC:
2851 case LYS_ACTION:
2852 case LYS_NOTIF:
2853 /* no changes */
2854 return LY_ENOT;
2855 case LYS_LIST:
2856 case LYS_LEAFLIST:
2857 if ((options & LYD_DIFF_WITHDEFAULTS) && ((first->flags & LYD_DEFAULT) != (second->flags & LYD_DEFAULT))) {
2858 /* default flag change */
2859 *op = LYD_DIFF_OP_NONE;
2860 } else {
2861 /* no changes */
2862 return LY_ENOT;
2863 }
2864 break;
2865 case LYS_LEAF:
2866 case LYS_ANYXML:
2867 case LYS_ANYDATA:
2868 if (lyd_compare(first, second, 0)) {
2869 /* different values */
2870 *op = LYD_DIFF_OP_REPLACE;
2871 } else if ((options & LYD_DIFF_WITHDEFAULTS) && ((first->flags & LYD_DEFAULT) != (second->flags & LYD_DEFAULT))) {
2872 /* default flag change */
2873 *op = LYD_DIFF_OP_NONE;
2874 } else {
2875 /* no changes */
2876 return LY_ENOT;
2877 }
2878 break;
2879 default:
2880 LOGINT_RET(schema->module->ctx);
2881 }
2882 }
2883
2884 /*
2885 * set each attribute correctly based on the operation and node type
2886 */
2887
2888 /* orig-default */
2889 if ((options & LYD_DIFF_WITHDEFAULTS) && (schema->nodetype & LYD_NODE_TERM)
2890 && ((*op == LYD_DIFF_OP_REPLACE) || (*op == LYD_DIFF_OP_NONE))) {
2891 if (first->flags & LYD_DEFAULT) {
2892 *orig_default = "true";
2893 } else {
2894 *orig_default = "false";
2895 }
2896 }
2897
2898 /* orig-value */
2899 if ((schema->nodetype == LYS_LEAF) && (*op == LYD_DIFF_OP_REPLACE)) {
2900 /* leaf */
2901 *orig_value = (char *)lyd_value2str((struct lyd_node_term *)first, &dynamic);
2902 if (!dynamic) {
2903 *orig_value = strdup(*orig_value);
2904 LY_CHECK_ERR_RET(!*orig_value, LOGMEM(schema->module->ctx), LY_EMEM);
2905 }
2906 }
2907
2908 return LY_SUCCESS;
2909}
2910
2911/**
2912 * @brief Perform diff for all siblings at certain depth, recursively.
2913 *
2914 * For user-ordered lists/leaf-lists a specific structure is used for storing
2915 * the current order. The idea is to apply all the generated diff changes
2916 * virtually on the first tree so that we can continue to generate correct
2917 * changes after some were already generated.
2918 *
2919 * The algorithm then uses second tree position-based changes with a before
2920 * (preceding) item anchor.
2921 *
2922 * Example:
2923 *
2924 * Virtual first tree leaf-list order:
2925 * 1 2 [3] 4 5
2926 *
2927 * Second tree leaf-list order:
2928 * 1 2 [5] 3 4
2929 *
2930 * We are at the 3rd node now. We look at whether the nodes on the 3rd position
2931 * match - they do not - move nodes so that the 3rd position node is final ->
2932 * -> move node 5 to the 3rd position -> move node 5 after node 2.
2933 *
2934 * Required properties:
2935 * Stored operations (move) should not be affected by later operations -
2936 * - would cause a redundantly long list of operations, possibly inifinite.
2937 *
2938 * Implemenation justification:
2939 * First, all delete operations and only then move/create operations are stored.
2940 * Also, preceding anchor is used and after each iteration another node is
2941 * at its final position. That results in the invariant that all preceding
2942 * nodes are final and will not be changed by the later operations, meaning
2943 * they can safely be used as anchors for the later operations.
2944 *
2945 * @param[in] first First tree first sibling.
2946 * @param[in] second Second tree first sibling.
2947 * @param[in] options Diff options.
2948 * @param[in,out] diff Diff to append to.
2949 * @return LY_ERR value.
2950 */
2951static LY_ERR
2952lyd_diff_siblings_r(const struct lyd_node *first, const struct lyd_node *second, int options, struct lyd_node **diff)
2953{
2954 LY_ERR ret = LY_SUCCESS;
2955 const struct lyd_node *iter_first, *iter_second;
2956 struct lyd_node *match_second, *match_first;
2957 int nosiblings = 0;
2958 struct lyd_diff_userord *userord = NULL;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002959 LY_ARRAY_COUNT_TYPE u;
Michal Vaskoe893ddd2020-06-23 13:35:20 +02002960 enum lyd_diff_op op;
2961 const char *orig_default;
2962 char *orig_value, *key, *value, *orig_key;
2963
2964 if (options & LYD_DIFF_NOSIBLINGS) {
2965 /* remember it for this function call only, should not be passed recursively */
2966 nosiblings = 1;
2967 options &= ~LYD_DIFF_NOSIBLINGS;
2968 }
2969
2970 /* compare first tree to the second tree - delete, replace, none */
2971 LY_LIST_FOR(first, iter_first) {
2972 assert(!(iter_first->schema->flags & LYS_KEY));
2973 if ((iter_first->flags & LYD_DEFAULT) && !(options & LYD_DIFF_WITHDEFAULTS)) {
2974 /* skip default nodes */
2975 continue;
2976 }
2977
2978 if (iter_first->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) {
2979 /* try to find the exact instance */
2980 lyd_find_sibling_first(second, iter_first, &match_second);
2981 } else {
2982 /* try to simply find the node, there cannot be more instances */
2983 lyd_find_sibling_val(second, iter_first->schema, NULL, 0, &match_second);
2984 }
2985
2986 if (match_second && (match_second->flags & LYD_DEFAULT) && !(options & LYD_DIFF_WITHDEFAULTS)) {
2987 /* ignore default nodes */
2988 match_second = NULL;
2989 }
2990
2991 if (lysc_is_userordered(iter_first->schema)) {
2992 if (match_second) {
2993 /* we are handling only user-ordered node delete now */
2994 continue;
2995 }
2996
2997 /* get all the attributes */
2998 LY_CHECK_GOTO(lyd_diff_userord_attrs(iter_first, match_second, options, &userord, &op, &orig_default,
2999 &value, &orig_value, &key, &orig_key), cleanup);
3000
3001 /* there must be changes, it is deleted */
3002 assert(op == LYD_DIFF_OP_DELETE);
3003 ret = lyd_diff_add(iter_first, op, orig_default, orig_value, key, value, orig_key, diff);
3004
3005 free(orig_value);
3006 free(key);
3007 free(value);
3008 free(orig_key);
3009 LY_CHECK_GOTO(ret, cleanup);
3010 } else {
3011 /* get all the attributes */
3012 ret = lyd_diff_attrs(iter_first, match_second, options, &op, &orig_default, &orig_value);
3013
3014 /* add into diff if there are any changes */
3015 if (!ret) {
3016 if (op == LYD_DIFF_OP_DELETE) {
3017 ret = lyd_diff_add(iter_first, op, orig_default, orig_value, NULL, NULL, NULL, diff);
3018 } else {
3019 ret = lyd_diff_add(match_second, op, orig_default, orig_value, NULL, NULL, NULL, diff);
3020 }
3021
3022 free(orig_value);
3023 LY_CHECK_GOTO(ret, cleanup);
3024 } else if (ret == LY_ENOT) {
3025 ret = LY_SUCCESS;
3026 } else {
3027 goto cleanup;
3028 }
3029 }
3030
3031 /* check descendants, if any, recursively */
3032 if (match_second) {
3033 LY_CHECK_GOTO(lyd_diff_siblings_r(LYD_CHILD(iter_first), LYD_CHILD(match_second), options, diff), cleanup);
3034 }
3035
3036 if (nosiblings) {
3037 break;
3038 }
3039 }
3040
3041 /* reset all cached positions */
3042 LY_ARRAY_FOR(userord, u) {
3043 userord[u].pos = 0;
3044 }
3045
3046 /* compare second tree to the first tree - create, user-ordered move */
3047 LY_LIST_FOR(second, iter_second) {
3048 assert(!(iter_second->schema->flags & LYS_KEY));
3049 if ((iter_second->flags & LYD_DEFAULT) && !(options & LYD_DIFF_WITHDEFAULTS)) {
3050 /* skip default nodes */
3051 continue;
3052 }
3053
3054 if (iter_second->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) {
3055 lyd_find_sibling_first(first, iter_second, &match_first);
3056 } else {
3057 lyd_find_sibling_val(first, iter_second->schema, NULL, 0, &match_first);
3058 }
3059
3060 if (match_first && (match_first->flags & LYD_DEFAULT) && !(options & LYD_DIFF_WITHDEFAULTS)) {
3061 /* ignore default nodes */
3062 match_first = NULL;
3063 }
3064
3065 if (lysc_is_userordered(iter_second->schema)) {
3066 /* get all the attributes */
3067 ret = lyd_diff_userord_attrs(match_first, iter_second, options, &userord, &op, &orig_default,
3068 &value, &orig_value, &key, &orig_key);
3069
3070 /* add into diff if there are any changes */
3071 if (!ret) {
3072 ret = lyd_diff_add(iter_second, op, orig_default, orig_value, key, value, orig_key, diff);
3073
3074 free(orig_value);
3075 free(key);
3076 free(value);
3077 free(orig_key);
3078 LY_CHECK_GOTO(ret, cleanup);
3079 } else if (ret == LY_ENOT) {
3080 ret = LY_SUCCESS;
3081 } else {
3082 goto cleanup;
3083 }
3084 } else if (!match_first) {
3085 /* get all the attributes */
3086 LY_CHECK_GOTO(lyd_diff_attrs(match_first, iter_second, options, &op, &orig_default, &orig_value), cleanup);
3087
3088 /* there must be changes, it is created */
3089 assert(op == LYD_DIFF_OP_CREATE);
3090 ret = lyd_diff_add(iter_second, op, orig_default, orig_value, NULL, NULL, NULL, diff);
3091
3092 free(orig_value);
3093 LY_CHECK_GOTO(ret, cleanup);
3094 } /* else was handled */
3095
3096 if (nosiblings) {
3097 break;
3098 }
3099 }
3100
3101cleanup:
3102 LY_ARRAY_FOR(userord, u) {
3103 LY_ARRAY_FREE(userord[u].inst);
3104 }
3105 LY_ARRAY_FREE(userord);
3106 return ret;
3107}
3108
3109API LY_ERR
3110lyd_diff(const struct lyd_node *first, const struct lyd_node *second, int options, struct lyd_node **diff)
3111{
3112 const struct ly_ctx *ctx;
3113
3114 LY_CHECK_ARG_RET(NULL, diff, LY_EINVAL);
3115
3116 if (first) {
3117 ctx = LYD_NODE_CTX(first);
3118 } else if (second) {
3119 ctx = LYD_NODE_CTX(second);
3120 } else {
3121 ctx = NULL;
3122 }
3123
3124 if (first && second && (lysc_data_parent(first->schema) != lysc_data_parent(second->schema))) {
3125 LOGERR(ctx, LY_EINVAL, "Invalid arguments - cannot create diff for unrelated data (%s()).", __func__);
3126 return LY_EINVAL;
3127 }
3128
3129 *diff = NULL;
3130
3131 return lyd_diff_siblings_r(first, second, options, diff);
3132}
3133
3134/**
3135 * @brief Find a matching node in data tree for a diff node.
3136 *
3137 * @param[in] first_node First sibling in the data tree.
3138 * @param[in] diff_node Diff node to match.
3139 * @param[out] match_p Matching node.
3140 * @return LY_ERR value.
3141 */
3142static LY_ERR
3143lyd_diff_find_node(const struct lyd_node *first_node, const struct lyd_node *diff_node, struct lyd_node **match_p)
3144{
3145 if (diff_node->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) {
3146 /* try to find the exact instance */
3147 lyd_find_sibling_first(first_node, diff_node, match_p);
3148 } else {
3149 /* try to simply find the node, there cannot be more instances */
3150 lyd_find_sibling_val(first_node, diff_node->schema, NULL, 0, match_p);
3151 }
3152 LY_CHECK_ERR_RET(!*match_p, LOGINT(LYD_NODE_CTX(diff_node)), LY_EINT);
3153
3154 return LY_SUCCESS;
3155}
3156
3157/**
3158 * @brief Learn operation of a diff node.
3159 *
3160 * @param[in] diff_node Diff node.
3161 * @param[out] op Operation.
3162 * @param[out] key_or_value Optional list instance keys predicate or leaf-list value for move operation.
3163 * @return LY_ERR value.
3164 */
3165static LY_ERR
3166lyd_diff_get_op(const struct lyd_node *diff_node, const char **op, const char **key_or_value)
3167{
3168 struct lyd_meta *meta = NULL;
3169 const struct lyd_node *diff_parent;
3170 const char *meta_name, *str;
3171 int dynamic;
3172
3173 for (diff_parent = diff_node; diff_parent; diff_parent = (struct lyd_node *)diff_parent->parent) {
3174 LY_LIST_FOR(diff_parent->meta, meta) {
3175 if (!strcmp(meta->name, "operation") && !strcmp(meta->annotation->module->name, "yang")) {
3176 str = lyd_meta2str(meta, &dynamic);
3177 assert(!dynamic);
3178 if ((str[0] == 'r') && (diff_parent != diff_node)) {
3179 /* we do not care about this operation if it's in our parent */
3180 continue;
3181 }
3182 *op = str;
3183 break;
3184 }
3185 }
3186 if (meta) {
3187 break;
3188 }
3189 }
3190 LY_CHECK_ERR_RET(!meta, LOGINT(LYD_NODE_CTX(diff_node)), LY_EINT);
3191
3192 *key_or_value = NULL;
3193 if (lysc_is_userordered(diff_node->schema) && (((*op)[0] == 'c') || ((*op)[0] == 'r'))) {
3194 if (diff_node->schema->nodetype == LYS_LIST) {
3195 meta_name = "key";
3196 } else {
3197 meta_name = "value";
3198 }
3199
3200 LY_LIST_FOR(diff_node->meta, meta) {
3201 if (!strcmp(meta->name, meta_name) && !strcmp(meta->annotation->module->name, "yang")) {
3202 str = lyd_meta2str(meta, &dynamic);
3203 assert(!dynamic);
3204 *key_or_value = str;
3205 break;
3206 }
3207 }
3208 LY_CHECK_ERR_RET(!meta, LOGINT(LYD_NODE_CTX(diff_node)), LY_EINT);
3209 }
3210
3211 return LY_SUCCESS;
3212}
3213
3214/**
3215 * @brief Insert a diff node into a data tree.
3216 *
3217 * @param[in,out] first_node First sibling of the data tree.
3218 * @param[in] parent_node Data tree sibling parent node.
3219 * @param[in] new_node Node to insert.
3220 * @param[in] keys_or_value Optional predicate of relative (leaf-)list instance. If not set, the user-ordered
3221 * instance will be inserted at the first position.
3222 * @return err_info, NULL on success.
3223 */
3224static LY_ERR
3225lyd_diff_insert(struct lyd_node **first_node, struct lyd_node *parent_node, struct lyd_node *new_node,
3226 const char *key_or_value)
3227{
3228 LY_ERR ret;
3229 struct lyd_node *anchor;
3230
3231 assert(new_node);
3232
3233 if (!*first_node) {
3234 if (!parent_node) {
3235 /* no parent or siblings */
3236 *first_node = new_node;
3237 return LY_SUCCESS;
3238 }
3239
3240 /* simply insert into parent, no other children */
3241 if (key_or_value) {
3242 LOGERR(LYD_NODE_CTX(new_node), LY_EINVAL, "Node \"%s\" instance to insert next to not found.",
3243 new_node->schema->name);
3244 return LY_EINVAL;
3245 }
3246 return lyd_insert(parent_node, new_node);
3247 }
3248
3249 assert(!(*first_node)->parent || ((struct lyd_node *)(*first_node)->parent == parent_node));
3250
3251 /* simple insert */
3252 if (!lysc_is_userordered(new_node->schema)) {
3253 /* insert at the end */
3254 return lyd_insert_sibling(*first_node, new_node);
3255 }
3256
3257 if (key_or_value) {
3258 /* find the anchor sibling */
3259 ret = lyd_find_sibling_val(*first_node, new_node->schema, key_or_value, 0, &anchor);
3260 if (ret == LY_ENOTFOUND) {
3261 LOGERR(LYD_NODE_CTX(new_node), LY_EINVAL, "Node \"%s\" instance to insert next to not found.",
3262 new_node->schema->name);
3263 return LY_EINVAL;
3264 } else if (ret) {
3265 return ret;
3266 }
3267
3268 /* insert after */
3269 LY_CHECK_RET(lyd_insert_after(anchor, new_node));
3270 assert(new_node->prev == anchor);
3271 if (*first_node == new_node) {
3272 *first_node = anchor;
3273 }
3274 } else {
3275 if ((*first_node)->schema->flags & LYS_KEY) {
3276 assert(parent_node && (parent_node->schema->nodetype == LYS_LIST));
3277
3278 /* find last key */
3279 anchor = *first_node;
3280 while (anchor->next && (anchor->next->schema->flags & LYS_KEY)) {
3281 anchor = anchor->next;
3282 }
3283 /* insert after the last key */
3284 LY_CHECK_RET(lyd_insert_after(anchor, new_node));
3285 } else {
3286 /* insert at the beginning */
3287 LY_CHECK_RET(lyd_insert_before(*first_node, new_node));
3288 *first_node = new_node;
3289 }
3290 }
3291
3292 return LY_SUCCESS;
3293}
3294
3295/**
3296 * @brief Apply diff subtree on data tree nodes, recursively.
3297 *
3298 * @param[in,out] first_node First sibling of the data tree.
3299 * @param[in] parent_node Parent of the first sibling.
3300 * @param[in] diff_node Current diff node.
3301 * @return LY_ERR value.
3302 */
3303static LY_ERR
3304lyd_diff_apply_r(struct lyd_node **first_node, struct lyd_node *parent_node, const struct lyd_node *diff_node,
3305 lyd_diff_cb diff_cb, void *cb_data)
3306{
3307 LY_ERR ret;
3308 struct lyd_node *match, *diff_child;
3309 const char *op, *key_or_value, *str_val;
3310 int dynamic;
3311 const struct ly_ctx *ctx = LYD_NODE_CTX(diff_node);
3312
3313 /* read all the valid attributes */
3314 LY_CHECK_RET(lyd_diff_get_op(diff_node, &op, &key_or_value));
3315
3316 /* handle user-ordered (leaf-)lists separately */
3317 if (key_or_value) {
3318 assert((op[0] == 'c') || (op[0] == 'r'));
3319 if (op[0] == 'r') {
3320 /* find the node (we must have some siblings because the node was only moved) */
3321 LY_CHECK_RET(lyd_diff_find_node(*first_node, diff_node, &match));
3322 } else {
3323 /* duplicate the node(s) */
3324 match = lyd_dup(diff_node, NULL, LYD_DUP_NO_META);
3325 LY_CHECK_RET(!match, LY_EMEM);
3326 }
3327
3328 /* insert/move the node */
3329 if (key_or_value[0]) {
3330 ret = lyd_diff_insert(first_node, parent_node, match, key_or_value);
3331 } else {
3332 ret = lyd_diff_insert(first_node, parent_node, match, NULL);
3333 }
3334 if (ret) {
3335 if (op[0] == 'c') {
3336 lyd_free_tree(match);
3337 }
3338 return ret;
3339 }
3340
3341 goto next_iter_r;
3342 }
3343
3344 /* apply operation */
3345 switch (op[0]) {
3346 case 'n':
3347 /* find the node */
3348 LY_CHECK_RET(lyd_diff_find_node(*first_node, diff_node, &match));
3349
3350 if (match->schema->nodetype & LYD_NODE_TERM) {
3351 /* special case of only dflt flag change */
3352 if (diff_node->flags & LYD_DEFAULT) {
3353 match->flags |= LYD_DEFAULT;
3354 } else {
3355 match->flags &= ~LYD_DEFAULT;
3356 }
3357 } else {
3358 /* none operation on nodes without children is redundant and hence forbidden */
3359 if (!LYD_CHILD(diff_node)) {
3360 LOGINT_RET(ctx);
3361 }
3362 }
3363 break;
3364 case 'c':
3365 /* duplicate the node */
3366 match = lyd_dup(diff_node, NULL, LYD_DUP_NO_META);
3367 LY_CHECK_RET(!match, LY_EMEM);
3368
3369 /* insert it at the end */
3370 ret = 0;
3371 if (*first_node) {
3372 ret = lyd_insert_after((*first_node)->prev, match);
3373 } else if (parent_node) {
3374 ret = lyd_insert(parent_node, match);
3375 } else {
3376 *first_node = match;
3377 }
3378 if (ret) {
3379 lyd_free_tree(match);
3380 return ret;
3381 }
3382
3383 break;
3384 case 'd':
3385 /* find the node */
3386 LY_CHECK_RET(lyd_diff_find_node(*first_node, diff_node, &match));
3387
3388 /* remove it */
3389 if ((match == *first_node) && !match->parent) {
3390 assert(!parent_node);
3391 /* we have removed the top-level node */
3392 *first_node = (*first_node)->next;
3393 }
3394 lyd_free_tree(match);
3395
3396 /* we are not going recursively in this case, the whole subtree was already deleted */
3397 return LY_SUCCESS;
3398 case 'r':
3399 LY_CHECK_ERR_RET(diff_node->schema->nodetype != LYS_LEAF, LOGINT(ctx), LY_EINT);
3400
3401 /* find the node */
3402 LY_CHECK_RET(lyd_diff_find_node(*first_node, diff_node, &match));
3403
3404 /* update its value */
3405 str_val = lyd_value2str((struct lyd_node_term *)diff_node, &dynamic);
3406 ret = lyd_change_term(match, str_val);
3407 if (dynamic) {
3408 free((char *)str_val);
3409 }
3410 if (ret && (ret != LY_EEXIST)) {
3411 LOGINT_RET(ctx);
3412 }
3413
3414 /* with flags */
3415 match->flags = diff_node->flags;
3416 break;
3417 default:
3418 LOGINT_RET(ctx);
3419 }
3420
3421next_iter_r:
3422 if (diff_cb) {
3423 /* call callback */
3424 LY_CHECK_RET(diff_cb(diff_node, match, cb_data));
3425 }
3426
3427 /* apply diff recursively */
3428 LY_LIST_FOR(LYD_CHILD(diff_node), diff_child) {
3429 LY_CHECK_RET(lyd_diff_apply_r(lyd_node_children_p(match), match, diff_child, diff_cb, cb_data));
3430 }
3431
3432 return LY_SUCCESS;
3433}
3434
3435API LY_ERR
3436lyd_diff_apply_module(struct lyd_node **data, const struct lyd_node *diff, const struct lys_module *mod,
3437 lyd_diff_cb diff_cb, void *cb_data)
3438{
3439 const struct lyd_node *root;
3440
3441 LY_LIST_FOR(diff, root) {
3442 if (mod && (lyd_owner_module(root) != mod)) {
3443 /* skip data nodes from different modules */
3444 continue;
3445 }
3446
3447 /* apply relevant nodes from the diff datatree */
3448 LY_CHECK_RET(lyd_diff_apply_r(data, NULL, root, diff_cb, cb_data));
3449 }
3450
3451 return LY_SUCCESS;
3452}
3453
3454API LY_ERR
3455lyd_diff_apply(struct lyd_node **data, const struct lyd_node *diff)
3456{
3457 return lyd_diff_apply_module(data, diff, NULL, NULL, NULL);
3458}
3459
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003460static LY_ERR
3461lyd_path_str_enlarge(char **buffer, size_t *buflen, size_t reqlen, int is_static)
3462{
Michal Vasko14654712020-02-06 08:35:21 +01003463 /* ending \0 */
3464 ++reqlen;
3465
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003466 if (reqlen > *buflen) {
3467 if (is_static) {
3468 return LY_EINCOMPLETE;
3469 }
3470
3471 *buffer = ly_realloc(*buffer, reqlen * sizeof **buffer);
3472 if (!*buffer) {
3473 return LY_EMEM;
3474 }
3475
3476 *buflen = reqlen;
3477 }
3478
3479 return LY_SUCCESS;
3480}
3481
3482/**
3483 * @brief Append all list key predicates to path.
3484 *
3485 * @param[in] node Node with keys to print.
3486 * @param[in,out] buffer Buffer to print to.
3487 * @param[in,out] buflen Current buffer length.
3488 * @param[in,out] bufused Current number of characters used in @p buffer.
3489 * @param[in] is_static Whether buffer is static or can be reallocated.
3490 * @return LY_ERR
3491 */
3492static LY_ERR
3493lyd_path_list_predicate(const struct lyd_node *node, char **buffer, size_t *buflen, size_t *bufused, int is_static)
3494{
3495 const struct lyd_node *key;
3496 int dynamic = 0;
3497 size_t len;
3498 const char *val;
3499 char quot;
3500 LY_ERR rc;
3501
Michal Vasko5bfd4be2020-06-23 13:26:19 +02003502 for (key = lyd_node_children(node, 0); key && (key->schema->flags & LYS_KEY); key = key->next) {
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003503 val = lyd_value2str((struct lyd_node_term *)key, &dynamic);
3504 len = 1 + strlen(key->schema->name) + 2 + strlen(val) + 2;
3505 rc = lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static);
3506 if (rc != LY_SUCCESS) {
3507 if (dynamic) {
3508 free((char *)val);
3509 }
3510 return rc;
3511 }
3512
3513 quot = '\'';
3514 if (strchr(val, '\'')) {
3515 quot = '"';
3516 }
3517 *bufused += sprintf(*buffer + *bufused, "[%s=%c%s%c]", key->schema->name, quot, val, quot);
3518
3519 if (dynamic) {
3520 free((char *)val);
3521 }
3522 }
3523
3524 return LY_SUCCESS;
3525}
3526
3527/**
3528 * @brief Append leaf-list value predicate to path.
3529 *
3530 * @param[in] node Node to print.
3531 * @param[in,out] buffer Buffer to print to.
3532 * @param[in,out] buflen Current buffer length.
3533 * @param[in,out] bufused Current number of characters used in @p buffer.
3534 * @param[in] is_static Whether buffer is static or can be reallocated.
3535 * @return LY_ERR
3536 */
3537static LY_ERR
3538lyd_path_leaflist_predicate(const struct lyd_node *node, char **buffer, size_t *buflen, size_t *bufused, int is_static)
3539{
3540 int dynamic = 0;
3541 size_t len;
3542 const char *val;
3543 char quot;
3544 LY_ERR rc;
3545
3546 val = lyd_value2str((struct lyd_node_term *)node, &dynamic);
3547 len = 4 + strlen(val) + 2;
3548 rc = lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static);
3549 if (rc != LY_SUCCESS) {
3550 goto cleanup;
3551 }
3552
3553 quot = '\'';
3554 if (strchr(val, '\'')) {
3555 quot = '"';
3556 }
3557 *bufused += sprintf(*buffer + *bufused, "[.=%c%s%c]", quot, val, quot);
3558
3559cleanup:
3560 if (dynamic) {
3561 free((char *)val);
3562 }
3563 return rc;
3564}
3565
3566/**
3567 * @brief Append node position (relative to its other instances) predicate to path.
3568 *
3569 * @param[in] node Node to print.
3570 * @param[in,out] buffer Buffer to print to.
3571 * @param[in,out] buflen Current buffer length.
3572 * @param[in,out] bufused Current number of characters used in @p buffer.
3573 * @param[in] is_static Whether buffer is static or can be reallocated.
3574 * @return LY_ERR
3575 */
3576static LY_ERR
3577lyd_path_position_predicate(const struct lyd_node *node, char **buffer, size_t *buflen, size_t *bufused, int is_static)
3578{
3579 const struct lyd_node *first, *iter;
3580 size_t len;
3581 int pos;
3582 char *val = NULL;
3583 LY_ERR rc;
3584
3585 if (node->parent) {
3586 first = node->parent->child;
3587 } else {
3588 for (first = node; node->prev->next; node = node->prev);
3589 }
3590 pos = 1;
3591 for (iter = first; iter != node; iter = iter->next) {
3592 if (iter->schema == node->schema) {
3593 ++pos;
3594 }
3595 }
3596 if (asprintf(&val, "%d", pos) == -1) {
3597 return LY_EMEM;
3598 }
3599
3600 len = 1 + strlen(val) + 1;
3601 rc = lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static);
3602 if (rc != LY_SUCCESS) {
3603 goto cleanup;
3604 }
3605
3606 *bufused += sprintf(*buffer + *bufused, "[%s]", val);
3607
3608cleanup:
3609 free(val);
3610 return rc;
3611}
3612
3613API char *
3614lyd_path(const struct lyd_node *node, LYD_PATH_TYPE pathtype, char *buffer, size_t buflen)
3615{
Michal Vasko14654712020-02-06 08:35:21 +01003616 int is_static = 0, i, depth;
3617 size_t bufused = 0, len;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003618 const struct lyd_node *iter;
3619 const struct lys_module *mod;
3620 LY_ERR rc;
3621
3622 LY_CHECK_ARG_RET(NULL, node, NULL);
3623 if (buffer) {
3624 LY_CHECK_ARG_RET(node->schema->module->ctx, buflen > 1, NULL);
3625 is_static = 1;
Michal Vasko14654712020-02-06 08:35:21 +01003626 } else {
3627 buflen = 0;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003628 }
3629
3630 switch (pathtype) {
Michal Vasko14654712020-02-06 08:35:21 +01003631 case LYD_PATH_LOG:
3632 depth = 1;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003633 for (iter = node; iter->parent; iter = (const struct lyd_node *)iter->parent) {
3634 ++depth;
3635 }
3636
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003637 goto iter_print;
Michal Vasko14654712020-02-06 08:35:21 +01003638 while (depth) {
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003639 /* find the right node */
Michal Vasko14654712020-02-06 08:35:21 +01003640 for (iter = node, i = 1; i < depth; iter = (const struct lyd_node *)iter->parent, ++i);
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003641iter_print:
3642 /* print prefix and name */
3643 mod = NULL;
3644 if (!iter->parent || (iter->schema->module != iter->parent->schema->module)) {
3645 mod = iter->schema->module;
3646 }
3647
3648 /* realloc string */
3649 len = 1 + (mod ? strlen(mod->name) + 1 : 0) + strlen(iter->schema->name);
3650 rc = lyd_path_str_enlarge(&buffer, &buflen, bufused + len, is_static);
3651 if (rc != LY_SUCCESS) {
3652 break;
3653 }
3654
3655 /* print next node */
3656 bufused += sprintf(buffer + bufused, "/%s%s%s", mod ? mod->name : "", mod ? ":" : "", iter->schema->name);
3657
3658 switch (iter->schema->nodetype) {
3659 case LYS_LIST:
3660 if (iter->schema->flags & LYS_KEYLESS) {
3661 /* print its position */
3662 rc = lyd_path_position_predicate(iter, &buffer, &buflen, &bufused, is_static);
3663 } else {
3664 /* print all list keys in predicates */
3665 rc = lyd_path_list_predicate(iter, &buffer, &buflen, &bufused, is_static);
3666 }
3667 break;
3668 case LYS_LEAFLIST:
3669 if (iter->schema->flags & LYS_CONFIG_W) {
3670 /* print leaf-list value */
3671 rc = lyd_path_leaflist_predicate(iter, &buffer, &buflen, &bufused, is_static);
3672 } else {
3673 /* print its position */
3674 rc = lyd_path_position_predicate(iter, &buffer, &buflen, &bufused, is_static);
3675 }
3676 break;
3677 default:
3678 /* nothing to print more */
3679 rc = LY_SUCCESS;
3680 break;
3681 }
3682 if (rc != LY_SUCCESS) {
3683 break;
3684 }
3685
Michal Vasko14654712020-02-06 08:35:21 +01003686 --depth;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003687 }
3688 break;
3689 }
3690
3691 return buffer;
3692}
Michal Vaskoe444f752020-02-10 12:20:06 +01003693
Michal Vasko9b368d32020-02-14 13:53:31 +01003694LY_ERR
3695lyd_find_sibling_next2(const struct lyd_node *first, const struct lysc_node *schema, const char *key_or_value,
3696 size_t val_len, struct lyd_node **match)
Michal Vaskoe444f752020-02-10 12:20:06 +01003697{
3698 LY_ERR rc;
3699 const struct lyd_node *node = NULL;
3700 struct lyd_node_term *term;
Michal Vasko00cbf532020-06-15 13:58:47 +02003701 struct lyxp_expr *expr = NULL;
3702 uint16_t exp_idx = 0;
3703 struct ly_path_predicate *predicates = NULL;
3704 enum ly_path_pred_type pred_type = 0;
Michal Vasko90932a92020-02-12 14:33:03 +01003705 struct lyd_value val = {0};
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003706 LY_ARRAY_COUNT_TYPE u;
Michal Vaskoe444f752020-02-10 12:20:06 +01003707
Michal Vasko9b368d32020-02-14 13:53:31 +01003708 LY_CHECK_ARG_RET(NULL, schema, LY_EINVAL);
Michal Vaskoe444f752020-02-10 12:20:06 +01003709
3710 if (!first) {
3711 /* no data */
Michal Vasko9b368d32020-02-14 13:53:31 +01003712 if (match) {
3713 *match = NULL;
3714 }
Michal Vaskoe444f752020-02-10 12:20:06 +01003715 return LY_ENOTFOUND;
3716 }
3717
Michal Vaskoe444f752020-02-10 12:20:06 +01003718 if (key_or_value && !val_len) {
3719 val_len = strlen(key_or_value);
3720 }
3721
3722 if (key_or_value && (schema->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Michal Vasko90932a92020-02-12 14:33:03 +01003723 /* store the value */
Michal Vasko9b368d32020-02-14 13:53:31 +01003724 LY_CHECK_GOTO(rc = lyd_value_store(&val, schema, key_or_value, val_len, 0, lydjson_resolve_prefix, NULL, LYD_JSON), cleanup);
Michal Vaskoe444f752020-02-10 12:20:06 +01003725 } else if (key_or_value && (schema->nodetype == LYS_LIST)) {
Michal Vasko00cbf532020-06-15 13:58:47 +02003726 /* parse keys */
3727 LY_CHECK_GOTO(rc = ly_path_parse_predicate(schema->module->ctx, key_or_value, val_len, LY_PATH_PREFIX_OPTIONAL,
3728 LY_PATH_PRED_KEYS, &expr), cleanup);
3729
3730 /* compile them */
3731 LY_CHECK_GOTO(rc = ly_path_compile_predicate(schema->module->ctx, NULL, schema, expr, &exp_idx, lydjson_resolve_prefix,
3732 NULL, LYD_JSON, &predicates, &pred_type), cleanup);
Michal Vaskoe444f752020-02-10 12:20:06 +01003733 }
3734
3735 /* find first matching value */
3736 LY_LIST_FOR(first, node) {
3737 if (node->schema != schema) {
3738 continue;
3739 }
3740
Michal Vasko00cbf532020-06-15 13:58:47 +02003741 if ((schema->nodetype == LYS_LIST) && predicates) {
Michal Vaskoe444f752020-02-10 12:20:06 +01003742 /* compare all set keys */
Michal Vasko00cbf532020-06-15 13:58:47 +02003743 LY_ARRAY_FOR(predicates, u) {
Michal Vaskoe444f752020-02-10 12:20:06 +01003744 /* find key */
Michal Vasko5bfd4be2020-06-23 13:26:19 +02003745 rc = lyd_find_sibling_val(lyd_node_children(node, 0), predicates[u].key, NULL, 0, (struct lyd_node **)&term);
Michal Vaskoe444f752020-02-10 12:20:06 +01003746 if (rc == LY_ENOTFOUND) {
3747 /* all keys must always exist */
Michal Vasko9b368d32020-02-14 13:53:31 +01003748 LOGINT_RET(schema->module->ctx);
Michal Vaskoe444f752020-02-10 12:20:06 +01003749 }
3750 LY_CHECK_GOTO(rc, cleanup);
3751
3752 /* compare values */
Michal Vasko00cbf532020-06-15 13:58:47 +02003753 if (!term->value.realtype->plugin->compare(&term->value, &predicates[u].value)) {
Michal Vaskoe444f752020-02-10 12:20:06 +01003754 break;
3755 }
3756 }
3757
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003758 if (u < LY_ARRAY_COUNT(predicates)) {
Michal Vaskoe444f752020-02-10 12:20:06 +01003759 /* not a match */
3760 continue;
3761 }
Michal Vasko90932a92020-02-12 14:33:03 +01003762 } else if ((schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && val.realtype) {
Michal Vaskoe444f752020-02-10 12:20:06 +01003763 term = (struct lyd_node_term *)node;
3764
3765 /* compare values */
Michal Vasko90932a92020-02-12 14:33:03 +01003766 if (!term->value.realtype->plugin->compare(&term->value, &val)) {
Michal Vaskoe444f752020-02-10 12:20:06 +01003767 /* not a match */
3768 continue;
3769 }
3770 }
3771
3772 /* all criteria passed */
3773 break;
3774 }
3775
3776 if (!node) {
3777 rc = LY_ENOTFOUND;
Michal Vasko9b368d32020-02-14 13:53:31 +01003778 if (match) {
3779 *match = NULL;
3780 }
Michal Vaskoe444f752020-02-10 12:20:06 +01003781 goto cleanup;
3782 }
3783
3784 /* success */
Michal Vasko9b368d32020-02-14 13:53:31 +01003785 if (match) {
3786 *match = (struct lyd_node *)node;
3787 }
Michal Vaskoe444f752020-02-10 12:20:06 +01003788 rc = LY_SUCCESS;
3789
3790cleanup:
Michal Vasko00cbf532020-06-15 13:58:47 +02003791 ly_path_predicates_free(schema->module->ctx, pred_type, NULL, predicates);
3792 lyxp_expr_free(schema->module->ctx, expr);
Michal Vasko90932a92020-02-12 14:33:03 +01003793 if (val.realtype) {
Michal Vasko9b368d32020-02-14 13:53:31 +01003794 val.realtype->plugin->free(schema->module->ctx, &val);
Michal Vasko90932a92020-02-12 14:33:03 +01003795 }
Michal Vaskoe444f752020-02-10 12:20:06 +01003796 return rc;
3797}
3798
3799API LY_ERR
Michal Vasko9b368d32020-02-14 13:53:31 +01003800lyd_find_sibling_next(const struct lyd_node *first, const struct lys_module *module, const char *name, size_t name_len,
3801 const char *key_or_value, size_t val_len, struct lyd_node **match)
3802{
3803 const struct lysc_node *schema;
3804
3805 LY_CHECK_ARG_RET(NULL, module, name, match, LY_EINVAL);
3806
3807 if (!first) {
3808 /* no data */
3809 *match = NULL;
3810 return LY_ENOTFOUND;
3811 }
3812
3813 /* find schema */
3814 schema = lys_find_child(first->parent ? first->parent->schema : NULL, module, name, name_len, 0, 0);
3815 if (!schema) {
3816 LOGERR(module->ctx, LY_EINVAL, "Schema node not found.");
3817 return LY_EINVAL;
3818 }
3819
3820 return lyd_find_sibling_next2(first, schema, key_or_value, val_len, match);
3821}
3822
3823API LY_ERR
Michal Vaskoe444f752020-02-10 12:20:06 +01003824lyd_find_sibling_first(const struct lyd_node *siblings, const struct lyd_node *target, struct lyd_node **match)
3825{
3826 struct lyd_node **match_p;
3827 struct lyd_node_inner *parent;
3828
Michal Vaskof03ed032020-03-04 13:31:44 +01003829 LY_CHECK_ARG_RET(NULL, target, LY_EINVAL);
Michal Vaskoe444f752020-02-10 12:20:06 +01003830
Michal Vasko62ed12d2020-05-21 10:08:25 +02003831 if (!siblings || (lysc_data_parent(siblings->schema) != lysc_data_parent(target->schema))) {
3832 /* no data or schema mismatch */
Michal Vasko9b368d32020-02-14 13:53:31 +01003833 if (match) {
3834 *match = NULL;
3835 }
Michal Vaskoe444f752020-02-10 12:20:06 +01003836 return LY_ENOTFOUND;
3837 }
3838
3839 /* find first sibling */
3840 if (siblings->parent) {
3841 siblings = siblings->parent->child;
3842 } else {
3843 while (siblings->prev->next) {
3844 siblings = siblings->prev;
3845 }
3846 }
3847
3848 parent = (struct lyd_node_inner *)siblings->parent;
3849 if (parent && parent->children_ht) {
3850 assert(target->hash);
3851
3852 /* find by hash */
3853 if (!lyht_find(parent->children_ht, &target, target->hash, (void **)&match_p)) {
3854 siblings = *match_p;
3855 } else {
3856 /* not found */
3857 siblings = NULL;
3858 }
3859 } else {
3860 /* no children hash table */
3861 for (; siblings; siblings = siblings->next) {
3862 if (!lyd_compare(siblings, target, 0)) {
3863 break;
3864 }
3865 }
3866 }
3867
3868 if (!siblings) {
Michal Vasko9b368d32020-02-14 13:53:31 +01003869 if (match) {
3870 *match = NULL;
3871 }
Michal Vaskoe444f752020-02-10 12:20:06 +01003872 return LY_ENOTFOUND;
3873 }
3874
Michal Vasko9b368d32020-02-14 13:53:31 +01003875 if (match) {
3876 *match = (struct lyd_node *)siblings;
3877 }
Michal Vaskoe444f752020-02-10 12:20:06 +01003878 return LY_SUCCESS;
3879}
3880
3881API LY_ERR
3882lyd_find_sibling_set(const struct lyd_node *siblings, const struct lyd_node *target, struct ly_set **set)
3883{
3884 struct lyd_node_inner *parent;
3885 struct lyd_node *match;
3886 struct lyd_node **match_p;
3887 struct ly_set *ret;
3888
3889 LY_CHECK_ARG_RET(NULL, target, set, LY_EINVAL);
3890
Michal Vasko62ed12d2020-05-21 10:08:25 +02003891 if (!siblings || (lysc_data_parent(siblings->schema) != lysc_data_parent(target->schema))) {
3892 /* no data or schema mismatch */
Michal Vaskoe444f752020-02-10 12:20:06 +01003893 return LY_ENOTFOUND;
3894 }
3895
3896 ret = ly_set_new();
3897 LY_CHECK_ERR_RET(!ret, LOGMEM(target->schema->module->ctx), LY_EMEM);
3898
3899 /* find first sibling */
3900 if (siblings->parent) {
3901 siblings = siblings->parent->child;
3902 } else {
3903 while (siblings->prev->next) {
3904 siblings = siblings->prev;
3905 }
3906 }
3907
3908 parent = (struct lyd_node_inner *)siblings->parent;
3909 if (parent && parent->children_ht) {
3910 assert(target->hash);
3911
3912 /* find by hash */
3913 if (!lyht_find(parent->children_ht, &target, target->hash, (void **)&match_p)) {
3914 match = *match_p;
3915 } else {
3916 /* not found */
3917 match = NULL;
3918 }
3919 while (match) {
3920 /* add all found nodes into the return set */
3921 if (ly_set_add(ret, match, LY_SET_OPT_USEASLIST) == -1) {
3922 goto error;
3923 }
3924
3925 /* find next instance */
3926 if (lyht_find_next(parent->children_ht, &match, match->hash, (void **)&match_p)) {
3927 match = NULL;
3928 } else {
3929 match = *match_p;
3930 }
3931 }
3932 } else {
3933 /* no children hash table */
3934 for (; siblings; siblings = siblings->next) {
3935 if (!lyd_compare(siblings, target, 0)) {
3936 /* a match */
3937 if (ly_set_add(ret, (struct lyd_node *)siblings, LY_SET_OPT_USEASLIST) == -1) {
3938 goto error;
3939 }
3940 }
3941 }
3942 }
3943
3944 if (!ret->count) {
3945 ly_set_free(ret, NULL);
3946 return LY_ENOTFOUND;
3947 }
3948
3949 *set = ret;
3950 return LY_SUCCESS;
3951
3952error:
3953 ly_set_free(ret, NULL);
3954 return LY_EMEM;
3955}
3956
Michal Vasko90932a92020-02-12 14:33:03 +01003957static int
3958lyd_hash_table_schema_val_equal(void *val1_p, void *val2_p, int UNUSED(mod), void *UNUSED(cb_data))
3959{
3960 struct lysc_node *val1;
3961 struct lyd_node *val2;
3962
3963 val1 = *((struct lysc_node **)val1_p);
3964 val2 = *((struct lyd_node **)val2_p);
3965
3966 assert(val1->nodetype & (LYD_NODE_INNER | LYS_LEAF));
3967
3968 if (val1 == val2->schema) {
3969 /* schema match is enough */
3970 return 1;
3971 } else {
3972 return 0;
3973 }
3974}
3975
3976static LY_ERR
3977lyd_find_sibling_schema(const struct lyd_node *siblings, const struct lysc_node *schema, struct lyd_node **match)
3978{
3979 struct lyd_node **match_p;
3980 struct lyd_node_inner *parent;
3981 uint32_t hash;
3982 values_equal_cb ht_cb;
3983
3984 assert(siblings && schema && (schema->nodetype & (LYD_NODE_INNER | LYS_LEAF)));
3985
3986 /* find first sibling */
3987 if (siblings->parent) {
3988 siblings = siblings->parent->child;
3989 } else {
3990 while (siblings->prev->next) {
3991 siblings = siblings->prev;
3992 }
3993 }
3994
3995 parent = (struct lyd_node_inner *)siblings->parent;
3996 if (parent && parent->children_ht) {
3997 /* calculate our hash */
3998 hash = dict_hash_multi(0, schema->module->name, strlen(schema->module->name));
3999 hash = dict_hash_multi(hash, schema->name, strlen(schema->name));
4000 hash = dict_hash_multi(hash, NULL, 0);
4001
4002 /* use special hash table function */
4003 ht_cb = lyht_set_cb(parent->children_ht, lyd_hash_table_schema_val_equal);
4004
4005 /* find by hash */
4006 if (!lyht_find(parent->children_ht, &schema, hash, (void **)&match_p)) {
4007 siblings = *match_p;
4008 } else {
4009 /* not found */
4010 siblings = NULL;
4011 }
4012
4013 /* set the original hash table compare function back */
4014 lyht_set_cb(parent->children_ht, ht_cb);
4015 } else {
4016 /* no children hash table */
4017 for (; siblings; siblings = siblings->next) {
4018 if (siblings->schema == schema) {
4019 /* schema match is enough */
4020 break;
4021 }
4022 }
4023 }
4024
4025 if (!siblings) {
Michal Vasko9b368d32020-02-14 13:53:31 +01004026 if (match) {
4027 *match = NULL;
4028 }
Michal Vasko90932a92020-02-12 14:33:03 +01004029 return LY_ENOTFOUND;
4030 }
4031
Michal Vasko9b368d32020-02-14 13:53:31 +01004032 if (match) {
4033 *match = (struct lyd_node *)siblings;
4034 }
Michal Vasko90932a92020-02-12 14:33:03 +01004035 return LY_SUCCESS;
4036}
4037
Michal Vaskoe444f752020-02-10 12:20:06 +01004038API LY_ERR
4039lyd_find_sibling_val(const struct lyd_node *siblings, const struct lysc_node *schema, const char *key_or_value,
4040 size_t val_len, struct lyd_node **match)
4041{
4042 LY_ERR rc;
4043 struct lyd_node *target = NULL;
4044
4045 LY_CHECK_ARG_RET(NULL, schema, LY_EINVAL);
Michal Vasko62ed12d2020-05-21 10:08:25 +02004046 if ((schema->nodetype == LYS_LIST) && (schema->flags & LYS_KEYLESS)) {
Michal Vaskoe444f752020-02-10 12:20:06 +01004047 LOGERR(schema->module->ctx, LY_EINVAL, "Invalid arguments - key-less list (%s()).", __func__);
4048 return LY_EINVAL;
4049 } else if ((schema->nodetype & (LYS_LEAFLIST | LYS_LIST)) && !key_or_value) {
4050 LOGERR(schema->module->ctx, LY_EINVAL, "Invalid arguments - no value/keys for a (leaf-)list (%s()).", __func__);
4051 return LY_EINVAL;
4052 } else if (schema->nodetype & (LYS_CHOICE | LYS_CASE)) {
4053 LOGERR(schema->module->ctx, LY_EINVAL, "Invalid arguments - schema type %s (%s()).",
4054 lys_nodetype2str(schema->nodetype), __func__);
4055 return LY_EINVAL;
4056 }
4057
Michal Vasko62ed12d2020-05-21 10:08:25 +02004058 if (!siblings || (lysc_data_parent(siblings->schema) != lysc_data_parent(schema))) {
4059 /* no data or schema mismatch */
Michal Vasko9b368d32020-02-14 13:53:31 +01004060 if (match) {
4061 *match = NULL;
4062 }
Michal Vaskoe444f752020-02-10 12:20:06 +01004063 return LY_ENOTFOUND;
4064 }
4065
Michal Vaskof03ed032020-03-04 13:31:44 +01004066 if (key_or_value && !val_len) {
4067 val_len = strlen(key_or_value);
4068 }
4069
Michal Vasko90932a92020-02-12 14:33:03 +01004070 /* create data node if needed and find it */
Michal Vaskoe444f752020-02-10 12:20:06 +01004071 switch (schema->nodetype) {
4072 case LYS_CONTAINER:
4073 case LYS_ANYXML:
4074 case LYS_ANYDATA:
4075 case LYS_NOTIF:
Michal Vasko1bf09392020-03-27 12:38:10 +01004076 case LYS_RPC:
Michal Vasko9b368d32020-02-14 13:53:31 +01004077 case LYS_ACTION:
Michal Vaskoe444f752020-02-10 12:20:06 +01004078 case LYS_LEAF:
Michal Vasko004d3152020-06-11 19:59:22 +02004079 /* find it based on schema only, there cannot be more instances */
Michal Vasko90932a92020-02-12 14:33:03 +01004080 rc = lyd_find_sibling_schema(siblings, schema, match);
Michal Vaskoe444f752020-02-10 12:20:06 +01004081 break;
4082 case LYS_LEAFLIST:
4083 /* target used attributes: schema, hash, value */
Michal Vaskocbff3e92020-05-27 12:56:41 +02004084 rc = lyd_create_term(schema, key_or_value, val_len, NULL, lydjson_resolve_prefix, NULL, LYD_JSON, &target);
4085 LY_CHECK_RET(rc && (rc != LY_EINCOMPLETE), rc);
4086 rc = LY_SUCCESS;
Michal Vasko90932a92020-02-12 14:33:03 +01004087 /* fallthrough */
Michal Vaskoe444f752020-02-10 12:20:06 +01004088 case LYS_LIST:
Michal Vasko90932a92020-02-12 14:33:03 +01004089 if (schema->nodetype == LYS_LIST) {
4090 /* target used attributes: schema, hash, child (all keys) */
Michal Vasko004d3152020-06-11 19:59:22 +02004091 LY_CHECK_RET(lyd_create_list2(schema, key_or_value, val_len, &target));
Michal Vasko90932a92020-02-12 14:33:03 +01004092 }
4093
4094 /* find it */
4095 rc = lyd_find_sibling_first(siblings, target, match);
Michal Vaskoe444f752020-02-10 12:20:06 +01004096 break;
4097 default:
4098 /* unreachable */
4099 LOGINT(schema->module->ctx);
4100 return LY_EINT;
4101 }
4102
Michal Vaskoe444f752020-02-10 12:20:06 +01004103 lyd_free_tree(target);
4104 return rc;
4105}
Michal Vaskoccc02342020-05-21 10:09:21 +02004106
4107API LY_ERR
4108lyd_find_xpath(const struct lyd_node *ctx_node, const char *xpath, struct ly_set **set)
4109{
4110 LY_ERR ret = LY_SUCCESS;
4111 struct lyxp_set xp_set;
4112 struct lyxp_expr *exp;
4113 uint32_t i;
4114
4115 LY_CHECK_ARG_RET(NULL, ctx_node, xpath, set, LY_EINVAL);
4116
4117 memset(&xp_set, 0, sizeof xp_set);
4118
4119 /* compile expression */
Michal Vasko004d3152020-06-11 19:59:22 +02004120 exp = lyxp_expr_parse((struct ly_ctx *)LYD_NODE_CTX(ctx_node), xpath, 0, 1);
Michal Vaskoccc02342020-05-21 10:09:21 +02004121 LY_CHECK_ERR_GOTO(!exp, ret = LY_EINVAL, cleanup);
4122
4123 /* evaluate expression */
4124 ret = lyxp_eval(exp, LYD_JSON, ctx_node->schema->module, ctx_node, LYXP_NODE_ELEM, ctx_node, &xp_set, 0);
4125 LY_CHECK_GOTO(ret, cleanup);
4126
4127 /* allocate return set */
4128 *set = ly_set_new();
4129 LY_CHECK_ERR_GOTO(!*set, LOGMEM(LYD_NODE_CTX(ctx_node)); ret = LY_EMEM, cleanup);
4130
4131 /* transform into ly_set */
4132 if (xp_set.type == LYXP_SET_NODE_SET) {
4133 /* allocate memory for all the elements once (even though not all items must be elements but most likely will be) */
4134 (*set)->objs = malloc(xp_set.used * sizeof *(*set)->objs);
4135 LY_CHECK_ERR_GOTO(!(*set)->objs, LOGMEM(LYD_NODE_CTX(ctx_node)); ret = LY_EMEM, cleanup);
4136 (*set)->size = xp_set.used;
4137
4138 for (i = 0; i < xp_set.used; ++i) {
4139 if (xp_set.val.nodes[i].type == LYXP_NODE_ELEM) {
4140 ly_set_add(*set, xp_set.val.nodes[i].node, LY_SET_OPT_USEASLIST);
4141 }
4142 }
4143 }
4144
4145cleanup:
Michal Vasko0691d522020-05-21 13:21:47 +02004146 lyxp_set_free_content(&xp_set);
Michal Vaskoccc02342020-05-21 10:09:21 +02004147 lyxp_expr_free((struct ly_ctx *)LYD_NODE_CTX(ctx_node), exp);
4148 return ret;
4149}