blob: 410eebd8a15a063049f74fc57e51666c509670f4 [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 Vaskoe3ed7dc2022-11-30 11:39:44 +01004 * @author Michal Vasko <mvasko@cesnet.cz>
Michal Vasko6cd9b6b2020-06-22 10:05:22 +02005 * @brief Data tree functions
Radek Krejcie7b95092019-05-15 11:03:07 +02006 *
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +01007 * Copyright (c) 2015 - 2022 CESNET, z.s.p.o.
Radek Krejcie7b95092019-05-15 11:03:07 +02008 *
9 * This source code is licensed under BSD 3-Clause License (the "License").
10 * You may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
12 *
13 * https://opensource.org/licenses/BSD-3-Clause
14 */
15
Radek Krejci535ea9f2020-05-29 16:01:05 +020016#define _GNU_SOURCE
17
18#include "tree_data.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020019
Radek Krejci084289f2019-07-09 17:35:30 +020020#include <assert.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020021#include <ctype.h>
Radek Krejci47fab892020-11-05 17:02:41 +010022#include <inttypes.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020023#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>
Radek Krejcie7b95092019-05-15 11:03:07 +020028
Radek Krejci535ea9f2020-05-29 16:01:05 +020029#include "common.h"
Michal Vasko5aa44c02020-06-29 11:47:02 +020030#include "compat.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020031#include "context.h"
32#include "dict.h"
Michal Vaskoa6669ba2020-08-06 16:14:26 +020033#include "diff.h"
Michal Vasko90932a92020-02-12 14:33:03 +010034#include "hash_table.h"
Radek Krejci47fab892020-11-05 17:02:41 +010035#include "in.h"
Michal Vaskoafac7822020-10-20 14:22:26 +020036#include "in_internal.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020037#include "log.h"
Radek Krejci7931b192020-06-25 17:05:03 +020038#include "parser_data.h"
39#include "parser_internal.h"
Michal Vasko004d3152020-06-11 19:59:22 +020040#include "path.h"
Radek Krejci0aa1f702021-04-01 16:16:19 +020041#include "plugins.h"
Radek Krejcif1ca0ac2021-04-12 16:00:06 +020042#include "plugins_exts/metadata.h"
Radek Krejci3e6632f2021-03-22 22:08:21 +010043#include "plugins_internal.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020044#include "plugins_types.h"
45#include "set.h"
46#include "tree.h"
47#include "tree_data_internal.h"
Radek Krejci859a15a2021-03-05 20:56:59 +010048#include "tree_edit.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020049#include "tree_schema.h"
50#include "tree_schema_internal.h"
Michal Vaskoa6669ba2020-08-06 16:14:26 +020051#include "validation.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020052#include "xml.h"
53#include "xpath.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020054
Michal Vaskof277d362023-04-24 09:08:31 +020055static LY_ERR lyd_compare_siblings_(const struct lyd_node *node1, const struct lyd_node *node2, uint32_t options,
56 ly_bool parental_schemas_checked);
57
Radek Krejci7931b192020-06-25 17:05:03 +020058static LYD_FORMAT
Michal Vasko63f3d842020-07-08 10:10:14 +020059lyd_parse_get_format(const struct ly_in *in, LYD_FORMAT format)
Radek Krejcie7b95092019-05-15 11:03:07 +020060{
Michal Vasko69730152020-10-09 16:30:07 +020061 if (!format && (in->type == LY_IN_FILEPATH)) {
Radek Krejcie7b95092019-05-15 11:03:07 +020062 /* unknown format - try to detect it from filename's suffix */
Radek Krejci7931b192020-06-25 17:05:03 +020063 const char *path = in->method.fpath.filepath;
64 size_t len = strlen(path);
Radek Krejcie7b95092019-05-15 11:03:07 +020065
66 /* ignore trailing whitespaces */
Michal Vaskod989ba02020-08-24 10:59:24 +020067 for ( ; len > 0 && isspace(path[len - 1]); len--) {}
Radek Krejcie7b95092019-05-15 11:03:07 +020068
Radek Krejcif13b87b2020-12-01 22:02:17 +010069 if ((len >= LY_XML_SUFFIX_LEN + 1) &&
70 !strncmp(&path[len - LY_XML_SUFFIX_LEN], LY_XML_SUFFIX, LY_XML_SUFFIX_LEN)) {
Radek Krejcie7b95092019-05-15 11:03:07 +020071 format = LYD_XML;
Radek Krejcif13b87b2020-12-01 22:02:17 +010072 } else if ((len >= LY_JSON_SUFFIX_LEN + 1) &&
73 !strncmp(&path[len - LY_JSON_SUFFIX_LEN], LY_JSON_SUFFIX, LY_JSON_SUFFIX_LEN)) {
Radek Krejcie7b95092019-05-15 11:03:07 +020074 format = LYD_JSON;
Radek Krejcif13b87b2020-12-01 22:02:17 +010075 } else if ((len >= LY_LYB_SUFFIX_LEN + 1) &&
76 !strncmp(&path[len - LY_LYB_SUFFIX_LEN], LY_LYB_SUFFIX, LY_LYB_SUFFIX_LEN)) {
Radek Krejcie7b95092019-05-15 11:03:07 +020077 format = LYD_LYB;
Radek Krejci7931b192020-06-25 17:05:03 +020078 } /* else still unknown */
Radek Krejcie7b95092019-05-15 11:03:07 +020079 }
80
Radek Krejci7931b192020-06-25 17:05:03 +020081 return format;
82}
Radek Krejcie7b95092019-05-15 11:03:07 +020083
Michal Vaskoe0665742021-02-11 11:08:44 +010084/**
85 * @brief Parse YANG data into a data tree.
86 *
87 * @param[in] ctx libyang context.
Radek Krejcif16e2542021-02-17 15:39:23 +010088 * @param[in] ext Optional extenion instance to parse data following the schema tree specified in the extension instance
Michal Vaskoe0665742021-02-11 11:08:44 +010089 * @param[in] parent Parent to connect the parsed nodes to, if any.
90 * @param[in,out] first_p Pointer to the first top-level parsed node, used only if @p parent is NULL.
91 * @param[in] in Input handle to read the input from.
92 * @param[in] format Expected format of the data in @p in.
93 * @param[in] parse_opts Options for parser.
94 * @param[in] val_opts Options for validation.
95 * @param[out] op Optional pointer to the parsed operation, if any.
96 * @return LY_ERR value.
97 */
98static LY_ERR
Radek Krejcif16e2542021-02-17 15:39:23 +010099lyd_parse(const struct ly_ctx *ctx, const struct lysc_ext_instance *ext, struct lyd_node *parent, struct lyd_node **first_p,
100 struct ly_in *in, LYD_FORMAT format, uint32_t parse_opts, uint32_t val_opts, struct lyd_node **op)
Radek Krejci7931b192020-06-25 17:05:03 +0200101{
stewegd8e2fc92023-05-31 09:52:56 +0200102 LY_ERR r = LY_SUCCESS, rc = LY_SUCCESS;
Radek Krejci1798aae2020-07-14 13:26:06 +0200103 struct lyd_ctx *lydctx = NULL;
Michal Vaskoe0665742021-02-11 11:08:44 +0100104 struct ly_set parsed = {0};
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +0100105 uint32_t i, int_opts = 0;
Michal Vaskoddd76592022-01-17 13:34:48 +0100106 ly_bool subtree_sibling = 0;
Radek Krejci1798aae2020-07-14 13:26:06 +0200107
Michal Vaskoe0665742021-02-11 11:08:44 +0100108 assert(ctx && (parent || first_p));
Radek Krejci7931b192020-06-25 17:05:03 +0200109
110 format = lyd_parse_get_format(in, format);
Michal Vaskoe0665742021-02-11 11:08:44 +0100111 if (first_p) {
112 *first_p = NULL;
113 }
Radek Krejci1798aae2020-07-14 13:26:06 +0200114
Michal Vasko63f3d842020-07-08 10:10:14 +0200115 /* remember input position */
116 in->func_start = in->current;
Radek Krejci7931b192020-06-25 17:05:03 +0200117
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +0100118 /* set internal options */
119 if (!(parse_opts & LYD_PARSE_SUBTREE)) {
120 int_opts = LYD_INTOPT_WITH_SIBLINGS;
121 }
122
Michal Vaskoe0665742021-02-11 11:08:44 +0100123 /* parse the data */
Radek Krejci7931b192020-06-25 17:05:03 +0200124 switch (format) {
125 case LYD_XML:
Michal Vaskod027f382023-02-10 09:13:25 +0100126 r = lyd_parse_xml(ctx, ext, parent, first_p, in, parse_opts, val_opts, int_opts, &parsed,
Michal Vaskoddd76592022-01-17 13:34:48 +0100127 &subtree_sibling, &lydctx);
Radek Krejci1798aae2020-07-14 13:26:06 +0200128 break;
Radek Krejci7931b192020-06-25 17:05:03 +0200129 case LYD_JSON:
Michal Vaskod027f382023-02-10 09:13:25 +0100130 r = lyd_parse_json(ctx, ext, parent, first_p, in, parse_opts, val_opts, int_opts, &parsed,
Michal Vaskoddd76592022-01-17 13:34:48 +0100131 &subtree_sibling, &lydctx);
Radek Krejci1798aae2020-07-14 13:26:06 +0200132 break;
Radek Krejci7931b192020-06-25 17:05:03 +0200133 case LYD_LYB:
Michal Vaskod027f382023-02-10 09:13:25 +0100134 r = lyd_parse_lyb(ctx, ext, parent, first_p, in, parse_opts, val_opts, int_opts, &parsed,
Michal Vaskoddd76592022-01-17 13:34:48 +0100135 &subtree_sibling, &lydctx);
Radek Krejci1798aae2020-07-14 13:26:06 +0200136 break;
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200137 case LYD_UNKNOWN:
Michal Vaskod74978f2021-02-12 11:59:36 +0100138 LOGARG(ctx, format);
Michal Vaskod027f382023-02-10 09:13:25 +0100139 r = LY_EINVAL;
Michal Vaskoe0665742021-02-11 11:08:44 +0100140 break;
141 }
Michal Vasko50da8cd2023-03-10 08:38:59 +0100142 if (r) {
143 rc = r;
144 if ((r != LY_EVALID) || !lydctx || !(lydctx->val_opts & LYD_VALIDATE_MULTI_ERROR) ||
145 (ly_vecode(ctx) == LYVE_SYNTAX)) {
146 goto cleanup;
147 }
148 }
Michal Vaskoe0665742021-02-11 11:08:44 +0100149
Michal Vaskoa6139e02023-10-03 14:13:22 +0200150 if (parent && parsed.count) {
151 /* use the first parsed node */
152 first_p = &parsed.dnodes[0];
Radek Krejci7931b192020-06-25 17:05:03 +0200153 }
154
Michal Vaskoe0665742021-02-11 11:08:44 +0100155 if (!(parse_opts & LYD_PARSE_ONLY)) {
156 /* validate data */
Michal Vasko9f6dd7c2023-04-06 08:46:30 +0200157 r = lyd_validate(first_p, NULL, ctx, val_opts, 0, &lydctx->node_when, &lydctx->node_types, &lydctx->meta_types,
Michal Vasko135719f2022-08-25 12:18:17 +0200158 &lydctx->ext_node, &lydctx->ext_val, NULL);
Michal Vasko9f6dd7c2023-04-06 08:46:30 +0200159 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
Michal Vaskoe0665742021-02-11 11:08:44 +0100160 }
Radek Krejci7931b192020-06-25 17:05:03 +0200161
Michal Vaskoe0665742021-02-11 11:08:44 +0100162 /* set the operation node */
163 if (op) {
164 *op = lydctx->op_node;
Radek Krejci1798aae2020-07-14 13:26:06 +0200165 }
166
167cleanup:
Michal Vaskoe0665742021-02-11 11:08:44 +0100168 if (lydctx) {
169 lydctx->free(lydctx);
Radek Krejci1798aae2020-07-14 13:26:06 +0200170 }
Michal Vaskoe0665742021-02-11 11:08:44 +0100171 if (rc) {
172 if (parent) {
173 /* free all the parsed subtrees */
174 for (i = 0; i < parsed.count; ++i) {
175 lyd_free_tree(parsed.dnodes[i]);
176 }
177 } else {
178 /* free everything */
179 lyd_free_all(*first_p);
180 *first_p = NULL;
181 }
Michal Vaskoddd76592022-01-17 13:34:48 +0100182 } else if (subtree_sibling) {
183 rc = LY_ENOT;
Michal Vaskoe0665742021-02-11 11:08:44 +0100184 }
185 ly_set_erase(&parsed, NULL);
186 return rc;
Radek Krejci7931b192020-06-25 17:05:03 +0200187}
188
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100189LIBYANG_API_DEF LY_ERR
Radek Krejcif16e2542021-02-17 15:39:23 +0100190lyd_parse_ext_data(const struct lysc_ext_instance *ext, struct lyd_node *parent, struct ly_in *in, LYD_FORMAT format,
191 uint32_t parse_options, uint32_t validate_options, struct lyd_node **tree)
192{
193 const struct ly_ctx *ctx = ext ? ext->module->ctx : NULL;
194
195 LY_CHECK_ARG_RET(ctx, ext, in, parent || tree, LY_EINVAL);
196 LY_CHECK_ARG_RET(ctx, !(parse_options & ~LYD_PARSE_OPTS_MASK), LY_EINVAL);
197 LY_CHECK_ARG_RET(ctx, !(validate_options & ~LYD_VALIDATE_OPTS_MASK), LY_EINVAL);
198
199 return lyd_parse(ctx, ext, parent, tree, in, format, parse_options, validate_options, NULL);
200}
201
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100202LIBYANG_API_DEF LY_ERR
Michal Vaskoe0665742021-02-11 11:08:44 +0100203lyd_parse_data(const struct ly_ctx *ctx, struct lyd_node *parent, struct ly_in *in, LYD_FORMAT format,
204 uint32_t parse_options, uint32_t validate_options, struct lyd_node **tree)
205{
206 LY_CHECK_ARG_RET(ctx, ctx, in, parent || tree, LY_EINVAL);
207 LY_CHECK_ARG_RET(ctx, !(parse_options & ~LYD_PARSE_OPTS_MASK), LY_EINVAL);
208 LY_CHECK_ARG_RET(ctx, !(validate_options & ~LYD_VALIDATE_OPTS_MASK), LY_EINVAL);
209
Radek Krejcif16e2542021-02-17 15:39:23 +0100210 return lyd_parse(ctx, NULL, parent, tree, in, format, parse_options, validate_options, NULL);
Michal Vaskoe0665742021-02-11 11:08:44 +0100211}
212
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100213LIBYANG_API_DEF LY_ERR
Michal Vaskoe0665742021-02-11 11:08:44 +0100214lyd_parse_data_mem(const struct ly_ctx *ctx, const char *data, LYD_FORMAT format, uint32_t parse_options,
215 uint32_t validate_options, struct lyd_node **tree)
Radek Krejci7931b192020-06-25 17:05:03 +0200216{
217 LY_ERR ret;
218 struct ly_in *in;
219
220 LY_CHECK_RET(ly_in_new_memory(data, &in));
Michal Vaskoe0665742021-02-11 11:08:44 +0100221 ret = lyd_parse_data(ctx, NULL, in, format, parse_options, validate_options, tree);
Radek Krejci7931b192020-06-25 17:05:03 +0200222
223 ly_in_free(in, 0);
224 return ret;
225}
226
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100227LIBYANG_API_DEF LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200228lyd_parse_data_fd(const struct ly_ctx *ctx, int fd, LYD_FORMAT format, uint32_t parse_options, uint32_t validate_options,
Radek Krejci0f969882020-08-21 16:56:47 +0200229 struct lyd_node **tree)
Radek Krejci7931b192020-06-25 17:05:03 +0200230{
231 LY_ERR ret;
232 struct ly_in *in;
233
234 LY_CHECK_RET(ly_in_new_fd(fd, &in));
Michal Vaskoe0665742021-02-11 11:08:44 +0100235 ret = lyd_parse_data(ctx, NULL, in, format, parse_options, validate_options, tree);
Radek Krejci7931b192020-06-25 17:05:03 +0200236
237 ly_in_free(in, 0);
238 return ret;
239}
240
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100241LIBYANG_API_DEF LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200242lyd_parse_data_path(const struct ly_ctx *ctx, const char *path, LYD_FORMAT format, uint32_t parse_options,
243 uint32_t validate_options, struct lyd_node **tree)
Radek Krejci7931b192020-06-25 17:05:03 +0200244{
245 LY_ERR ret;
246 struct ly_in *in;
247
248 LY_CHECK_RET(ly_in_new_filepath(path, 0, &in));
Michal Vaskoe0665742021-02-11 11:08:44 +0100249 ret = lyd_parse_data(ctx, NULL, in, format, parse_options, validate_options, tree);
Radek Krejci7931b192020-06-25 17:05:03 +0200250
251 ly_in_free(in, 0);
252 return ret;
253}
254
Radek Krejcif16e2542021-02-17 15:39:23 +0100255/**
256 * @brief Parse YANG data into an operation data tree, in case the extension instance is specified, keep the searching
257 * for schema nodes locked inside the extension instance.
258 *
259 * At least one of @p parent, @p tree, or @p op must always be set.
260 *
Michal Vasko820efe82023-05-12 15:47:43 +0200261 * Specific @p data_type values have different parameter meaning as mentioned for ::lyd_parse_op().
Radek Krejcif16e2542021-02-17 15:39:23 +0100262 *
263 * @param[in] ctx libyang context.
264 * @param[in] ext Extension instance providing the specific schema tree to match with the data being parsed.
265 * @param[in] parent Optional parent to connect the parsed nodes to.
266 * @param[in] in Input handle to read the input from.
267 * @param[in] format Expected format of the data in @p in.
268 * @param[in] data_type Expected operation to parse (@ref datatype).
269 * @param[out] tree Optional full parsed data tree. If @p parent is set, set to NULL.
270 * @param[out] op Optional parsed operation node.
271 * @return LY_ERR value.
272 * @return LY_ENOT if @p data_type is a NETCONF message and the root XML element is not the expected one.
273 */
274static LY_ERR
275lyd_parse_op_(const struct ly_ctx *ctx, const struct lysc_ext_instance *ext, struct lyd_node *parent,
276 struct ly_in *in, LYD_FORMAT format, enum lyd_type data_type, struct lyd_node **tree, struct lyd_node **op)
Radek Krejci7931b192020-06-25 17:05:03 +0200277{
Michal Vaskoe0665742021-02-11 11:08:44 +0100278 LY_ERR rc = LY_SUCCESS;
279 struct lyd_ctx *lydctx = NULL;
280 struct ly_set parsed = {0};
281 struct lyd_node *first = NULL, *envp = NULL;
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +0100282 uint32_t i, parse_opts, val_opts, int_opts = 0;
Michal Vasko820efe82023-05-12 15:47:43 +0200283 ly_bool proto_msg = 0;
Radek Krejci7931b192020-06-25 17:05:03 +0200284
Michal Vasko27fb0262021-02-23 09:42:01 +0100285 if (!ctx) {
286 ctx = LYD_CTX(parent);
287 }
Radek Krejci1798aae2020-07-14 13:26:06 +0200288 if (tree) {
289 *tree = NULL;
290 }
291 if (op) {
292 *op = NULL;
293 }
294
Radek Krejci7931b192020-06-25 17:05:03 +0200295 format = lyd_parse_get_format(in, format);
Radek Krejci7931b192020-06-25 17:05:03 +0200296
Michal Vasko63f3d842020-07-08 10:10:14 +0200297 /* remember input position */
298 in->func_start = in->current;
299
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +0100300 /* set parse and validation opts */
Michal Vasko140ede92022-05-10 09:27:30 +0200301 parse_opts = LYD_PARSE_ONLY | LYD_PARSE_STRICT;
Michal Vaskoe0665742021-02-11 11:08:44 +0100302 val_opts = 0;
303
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +0100304 switch (data_type) {
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +0100305 case LYD_TYPE_RPC_NETCONF:
306 case LYD_TYPE_NOTIF_NETCONF:
307 LY_CHECK_ARG_RET(ctx, format == LYD_XML, !parent, tree, op, LY_EINVAL);
Michal Vasko820efe82023-05-12 15:47:43 +0200308 proto_msg = 1;
309 break;
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +0100310 case LYD_TYPE_REPLY_NETCONF:
Michal Vasko820efe82023-05-12 15:47:43 +0200311 LY_CHECK_ARG_RET(ctx, format == LYD_XML, parent, parent->schema, parent->schema->nodetype & (LYS_RPC | LYS_ACTION),
312 tree, !op, LY_EINVAL);
313 proto_msg = 1;
314 break;
315 case LYD_TYPE_RPC_RESTCONF:
316 case LYD_TYPE_REPLY_RESTCONF:
317 LY_CHECK_ARG_RET(ctx, parent, parent->schema, parent->schema->nodetype & (LYS_RPC | LYS_ACTION), tree, !op, LY_EINVAL);
318 proto_msg = 1;
319 break;
320 case LYD_TYPE_NOTIF_RESTCONF:
321 LY_CHECK_ARG_RET(ctx, format == LYD_JSON, !parent, tree, op, LY_EINVAL);
322 proto_msg = 1;
323 break;
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +0100324
Michal Vasko820efe82023-05-12 15:47:43 +0200325 /* set internal opts */
326 case LYD_TYPE_RPC_YANG:
Michal Vasko5df28522023-08-25 08:04:37 +0200327 int_opts = LYD_INTOPT_RPC | LYD_INTOPT_ACTION | (parent ? LYD_INTOPT_WITH_SIBLINGS : LYD_INTOPT_NO_SIBLINGS);
Michal Vasko820efe82023-05-12 15:47:43 +0200328 break;
329 case LYD_TYPE_NOTIF_YANG:
Michal Vasko5df28522023-08-25 08:04:37 +0200330 int_opts = LYD_INTOPT_NOTIF | (parent ? LYD_INTOPT_WITH_SIBLINGS : LYD_INTOPT_NO_SIBLINGS);
Michal Vasko820efe82023-05-12 15:47:43 +0200331 break;
332 case LYD_TYPE_REPLY_YANG:
Michal Vasko5df28522023-08-25 08:04:37 +0200333 int_opts = LYD_INTOPT_REPLY | (parent ? LYD_INTOPT_WITH_SIBLINGS : LYD_INTOPT_NO_SIBLINGS);
Michal Vasko820efe82023-05-12 15:47:43 +0200334 break;
335 case LYD_TYPE_DATA_YANG:
336 LOGINT(ctx);
337 rc = LY_EINT;
338 goto cleanup;
339 }
340
341 /* parse a full protocol message */
342 if (proto_msg) {
343 if (format == LYD_XML) {
344 /* parse the NETCONF (or RESTCONF XML) message */
345 rc = lyd_parse_xml_netconf(ctx, ext, parent, &first, in, parse_opts, val_opts, data_type, &envp, &parsed, &lydctx);
346 } else {
347 /* parse the RESTCONF message */
348 rc = lyd_parse_json_restconf(ctx, ext, parent, &first, in, parse_opts, val_opts, data_type, &envp, &parsed, &lydctx);
349 }
Michal Vasko3a163bc2023-01-10 14:23:56 +0100350 if (rc) {
351 if (envp) {
352 /* special situation when the envelopes were parsed successfully */
353 *tree = envp;
354 }
Michal Vasko73792a12022-05-10 09:28:45 +0200355 goto cleanup;
Michal Vasko299d5d12021-02-16 16:36:37 +0100356 }
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +0100357
358 /* set out params correctly */
Michal Vasko472baa82022-12-01 16:17:41 +0100359 if (envp) {
360 /* special out param meaning */
361 *tree = envp;
362 } else {
363 *tree = parent ? NULL : first;
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +0100364 }
365 if (op) {
366 *op = lydctx->op_node;
367 }
368 goto cleanup;
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +0100369 }
370
371 /* parse the data */
372 switch (format) {
373 case LYD_XML:
374 rc = lyd_parse_xml(ctx, ext, parent, &first, in, parse_opts, val_opts, int_opts, &parsed, NULL, &lydctx);
Michal Vaskoe0665742021-02-11 11:08:44 +0100375 break;
Radek Krejci7931b192020-06-25 17:05:03 +0200376 case LYD_JSON:
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +0100377 rc = lyd_parse_json(ctx, ext, parent, &first, in, parse_opts, val_opts, int_opts, &parsed, NULL, &lydctx);
Michal Vaskoe0665742021-02-11 11:08:44 +0100378 break;
Radek Krejci7931b192020-06-25 17:05:03 +0200379 case LYD_LYB:
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +0100380 rc = lyd_parse_lyb(ctx, ext, parent, &first, in, parse_opts, val_opts, int_opts, &parsed, NULL, &lydctx);
Michal Vaskoe0665742021-02-11 11:08:44 +0100381 break;
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200382 case LYD_UNKNOWN:
Michal Vaskod74978f2021-02-12 11:59:36 +0100383 LOGARG(ctx, format);
384 rc = LY_EINVAL;
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200385 break;
Radek Krejci7931b192020-06-25 17:05:03 +0200386 }
Michal Vaskoe0665742021-02-11 11:08:44 +0100387 LY_CHECK_GOTO(rc, cleanup);
Radek Krejci7931b192020-06-25 17:05:03 +0200388
Michal Vaskoe0665742021-02-11 11:08:44 +0100389 /* set out params correctly */
390 if (tree) {
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +0100391 *tree = parent ? NULL : first;
Michal Vaskoe0665742021-02-11 11:08:44 +0100392 }
393 if (op) {
394 *op = lydctx->op_node;
395 }
396
397cleanup:
398 if (lydctx) {
399 lydctx->free(lydctx);
400 }
401 if (rc) {
Michal Vasko73792a12022-05-10 09:28:45 +0200402 /* free all the parsed nodes */
403 if (parsed.count) {
404 i = parsed.count;
405 do {
406 --i;
Michal Vaskoe0665742021-02-11 11:08:44 +0100407 lyd_free_tree(parsed.dnodes[i]);
Michal Vasko73792a12022-05-10 09:28:45 +0200408 } while (i);
409 }
Michal Vasko6fbf07b2023-08-08 13:54:48 +0200410 if (tree && !envp) {
Michal Vasko73792a12022-05-10 09:28:45 +0200411 *tree = NULL;
412 }
413 if (op) {
414 *op = NULL;
Michal Vaskoe0665742021-02-11 11:08:44 +0100415 }
416 }
417 ly_set_erase(&parsed, NULL);
418 return rc;
Radek Krejcie7b95092019-05-15 11:03:07 +0200419}
Radek Krejci084289f2019-07-09 17:35:30 +0200420
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100421LIBYANG_API_DEF LY_ERR
Radek Krejcif16e2542021-02-17 15:39:23 +0100422lyd_parse_op(const struct ly_ctx *ctx, struct lyd_node *parent, struct ly_in *in, LYD_FORMAT format,
423 enum lyd_type data_type, struct lyd_node **tree, struct lyd_node **op)
424{
425 LY_CHECK_ARG_RET(ctx, ctx || parent, in, data_type, parent || tree || op, LY_EINVAL);
426
427 return lyd_parse_op_(ctx, NULL, parent, in, format, data_type, tree, op);
428}
429
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100430LIBYANG_API_DEF LY_ERR
Radek Krejcif16e2542021-02-17 15:39:23 +0100431lyd_parse_ext_op(const struct lysc_ext_instance *ext, struct lyd_node *parent, struct ly_in *in, LYD_FORMAT format,
432 enum lyd_type data_type, struct lyd_node **tree, struct lyd_node **op)
433{
434 const struct ly_ctx *ctx = ext ? ext->module->ctx : NULL;
435
436 LY_CHECK_ARG_RET(ctx, ext, in, data_type, parent || tree || op, LY_EINVAL);
437
438 return lyd_parse_op_(ctx, ext, parent, in, format, data_type, tree, op);
439}
440
Michal Vasko90932a92020-02-12 14:33:03 +0100441struct lyd_node *
Michal Vaskob104f112020-07-17 09:54:54 +0200442lyd_insert_get_next_anchor(const struct lyd_node *first_sibling, const struct lyd_node *new_node)
Michal Vasko90932a92020-02-12 14:33:03 +0100443{
Michal Vaskob104f112020-07-17 09:54:54 +0200444 const struct lysc_node *schema, *sparent;
Michal Vasko90932a92020-02-12 14:33:03 +0100445 struct lyd_node *match = NULL;
Radek Krejci857189e2020-09-01 13:26:36 +0200446 ly_bool found;
Michal Vasko93b16062020-12-09 18:14:18 +0100447 uint32_t getnext_opts;
Michal Vasko90932a92020-02-12 14:33:03 +0100448
Michal Vaskob104f112020-07-17 09:54:54 +0200449 assert(new_node);
450
Michal Vasko9beceb82022-04-05 12:14:15 +0200451 if (!first_sibling || !new_node->schema || (LYD_CTX(first_sibling) != LYD_CTX(new_node))) {
Michal Vaskob104f112020-07-17 09:54:54 +0200452 /* insert at the end, no next anchor */
Michal Vasko90932a92020-02-12 14:33:03 +0100453 return NULL;
454 }
455
Michal Vasko93b16062020-12-09 18:14:18 +0100456 getnext_opts = 0;
Michal Vaskod1e53b92021-01-28 13:11:06 +0100457 if (new_node->schema->flags & LYS_IS_OUTPUT) {
Michal Vasko93b16062020-12-09 18:14:18 +0100458 getnext_opts = LYS_GETNEXT_OUTPUT;
459 }
460
Michal Vaskoa2756f02021-07-09 13:20:28 +0200461 if (first_sibling->parent && first_sibling->parent->schema && first_sibling->parent->children_ht) {
Michal Vaskob104f112020-07-17 09:54:54 +0200462 /* find the anchor using hashes */
463 sparent = first_sibling->parent->schema;
Michal Vasko93b16062020-12-09 18:14:18 +0100464 schema = lys_getnext(new_node->schema, sparent, NULL, getnext_opts);
Michal Vaskob104f112020-07-17 09:54:54 +0200465 while (schema) {
466 /* keep trying to find the first existing instance of the closest following schema sibling,
467 * otherwise return NULL - inserting at the end */
468 if (!lyd_find_sibling_schema(first_sibling, schema, &match)) {
469 break;
470 }
471
Michal Vasko93b16062020-12-09 18:14:18 +0100472 schema = lys_getnext(schema, sparent, NULL, getnext_opts);
Michal Vaskob104f112020-07-17 09:54:54 +0200473 }
474 } else {
475 /* find the anchor without hashes */
476 match = (struct lyd_node *)first_sibling;
Michal Vasko3a708622021-07-15 14:20:10 +0200477 sparent = lysc_data_parent(new_node->schema);
478 if (!sparent) {
Michal Vaskob104f112020-07-17 09:54:54 +0200479 /* we are in top-level, skip all the data from preceding modules */
480 LY_LIST_FOR(match, match) {
481 if (!match->schema || (strcmp(lyd_owner_module(match)->name, lyd_owner_module(new_node)->name) >= 0)) {
482 break;
483 }
484 }
485 }
486
487 /* get the first schema sibling */
Michal Vasko93b16062020-12-09 18:14:18 +0100488 schema = lys_getnext(NULL, sparent, new_node->schema->module->compiled, getnext_opts);
Michal Vasko9a463472023-08-08 09:43:33 +0200489 if (!schema) {
Michal Vasko60d929e2023-08-21 11:54:42 +0200490 /* must be a top-level extension instance data, no anchor */
491 return NULL;
Michal Vasko9a463472023-08-08 09:43:33 +0200492 }
Michal Vaskob104f112020-07-17 09:54:54 +0200493
494 found = 0;
495 LY_LIST_FOR(match, match) {
496 if (!match->schema || (lyd_owner_module(match) != lyd_owner_module(new_node))) {
497 /* we have found an opaque node, which must be at the end, so use it OR
498 * modules do not match, so we must have traversed all the data from new_node module (if any),
499 * we have found the first node of the next module, that is what we want */
500 break;
501 }
502
503 /* skip schema nodes until we find the instantiated one */
504 while (!found) {
505 if (new_node->schema == schema) {
506 /* we have found the schema of the new node, continue search to find the first
507 * data node with a different schema (after our schema) */
508 found = 1;
509 break;
510 }
511 if (match->schema == schema) {
512 /* current node (match) is a data node still before the new node, continue search in data */
513 break;
514 }
Michal Vasko9a463472023-08-08 09:43:33 +0200515
Michal Vasko93b16062020-12-09 18:14:18 +0100516 schema = lys_getnext(schema, sparent, new_node->schema->module->compiled, getnext_opts);
Michal Vasko9a463472023-08-08 09:43:33 +0200517 if (!schema) {
Michal Vasko60d929e2023-08-21 11:54:42 +0200518 /* must be a top-level extension instance data, no anchor */
519 return NULL;
Michal Vasko9a463472023-08-08 09:43:33 +0200520 }
Michal Vaskob104f112020-07-17 09:54:54 +0200521 }
522
523 if (found && (match->schema != new_node->schema)) {
524 /* find the next node after we have found our node schema data instance */
525 break;
526 }
527 }
Michal Vasko90932a92020-02-12 14:33:03 +0100528 }
529
530 return match;
531}
532
Michal Vaskoc61dd062022-06-07 11:01:28 +0200533void
Michal Vaskof03ed032020-03-04 13:31:44 +0100534lyd_insert_after_node(struct lyd_node *sibling, struct lyd_node *node)
Michal Vasko90932a92020-02-12 14:33:03 +0100535{
Michal Vasko0249f7c2020-03-05 16:36:40 +0100536 struct lyd_node_inner *par;
537
Michal Vasko90932a92020-02-12 14:33:03 +0100538 assert(!node->next && (node->prev == node));
539
540 node->next = sibling->next;
541 node->prev = sibling;
542 sibling->next = node;
543 if (node->next) {
544 /* sibling had a succeeding node */
545 node->next->prev = node;
546 } else {
547 /* sibling was last, find first sibling and change its prev */
548 if (sibling->parent) {
549 sibling = sibling->parent->child;
550 } else {
Michal Vaskod989ba02020-08-24 10:59:24 +0200551 for ( ; sibling->prev->next != node; sibling = sibling->prev) {}
Michal Vasko90932a92020-02-12 14:33:03 +0100552 }
553 sibling->prev = node;
554 }
555 node->parent = sibling->parent;
Michal Vasko0249f7c2020-03-05 16:36:40 +0100556
Michal Vasko9f96a052020-03-10 09:41:45 +0100557 for (par = node->parent; par; par = par->parent) {
558 if ((par->flags & LYD_DEFAULT) && !(node->flags & LYD_DEFAULT)) {
559 /* remove default flags from NP containers */
Michal Vasko0249f7c2020-03-05 16:36:40 +0100560 par->flags &= ~LYD_DEFAULT;
561 }
562 }
Michal Vasko90932a92020-02-12 14:33:03 +0100563}
564
Michal Vaskoc61dd062022-06-07 11:01:28 +0200565void
Michal Vaskof03ed032020-03-04 13:31:44 +0100566lyd_insert_before_node(struct lyd_node *sibling, struct lyd_node *node)
Michal Vasko90932a92020-02-12 14:33:03 +0100567{
Michal Vasko0249f7c2020-03-05 16:36:40 +0100568 struct lyd_node_inner *par;
569
Michal Vasko90932a92020-02-12 14:33:03 +0100570 assert(!node->next && (node->prev == node));
571
572 node->next = sibling;
573 /* covers situation of sibling being first */
574 node->prev = sibling->prev;
575 sibling->prev = node;
576 if (node->prev->next) {
577 /* sibling had a preceding node */
578 node->prev->next = node;
579 } else if (sibling->parent) {
580 /* sibling was first and we must also change parent child pointer */
581 sibling->parent->child = node;
582 }
583 node->parent = sibling->parent;
Michal Vasko0249f7c2020-03-05 16:36:40 +0100584
Michal Vasko9f96a052020-03-10 09:41:45 +0100585 for (par = node->parent; par; par = par->parent) {
586 if ((par->flags & LYD_DEFAULT) && !(node->flags & LYD_DEFAULT)) {
587 /* remove default flags from NP containers */
Michal Vasko0249f7c2020-03-05 16:36:40 +0100588 par->flags &= ~LYD_DEFAULT;
589 }
590 }
Michal Vasko90932a92020-02-12 14:33:03 +0100591}
592
593/**
Michal Vaskob104f112020-07-17 09:54:54 +0200594 * @brief Insert node as the first and only child of a parent.
Michal Vasko90932a92020-02-12 14:33:03 +0100595 *
Michal Vasko9f96a052020-03-10 09:41:45 +0100596 * Handles inserting into NP containers and key-less lists.
597 *
Michal Vasko90932a92020-02-12 14:33:03 +0100598 * @param[in] parent Parent to insert into.
599 * @param[in] node Node to insert.
600 */
601static void
Michal Vaskob104f112020-07-17 09:54:54 +0200602lyd_insert_only_child(struct lyd_node *parent, struct lyd_node *node)
Michal Vasko90932a92020-02-12 14:33:03 +0100603{
604 struct lyd_node_inner *par;
605
Radek Krejcia1c1e542020-09-29 16:06:52 +0200606 assert(parent && !lyd_child(parent) && !node->next && (node->prev == node));
Michal Vasko52927e22020-03-16 17:26:14 +0100607 assert(!parent->schema || (parent->schema->nodetype & LYD_NODE_INNER));
Michal Vasko90932a92020-02-12 14:33:03 +0100608
609 par = (struct lyd_node_inner *)parent;
610
Michal Vaskob104f112020-07-17 09:54:54 +0200611 par->child = node;
Michal Vasko90932a92020-02-12 14:33:03 +0100612 node->parent = par;
Michal Vasko0249f7c2020-03-05 16:36:40 +0100613
Michal Vaskod989ba02020-08-24 10:59:24 +0200614 for ( ; par; par = par->parent) {
Michal Vasko9f96a052020-03-10 09:41:45 +0100615 if ((par->flags & LYD_DEFAULT) && !(node->flags & LYD_DEFAULT)) {
616 /* remove default flags from NP containers */
Michal Vasko0249f7c2020-03-05 16:36:40 +0100617 par->flags &= ~LYD_DEFAULT;
618 }
619 }
Michal Vasko751cb4d2020-07-14 12:25:28 +0200620}
Michal Vasko0249f7c2020-03-05 16:36:40 +0100621
Michal Vasko751cb4d2020-07-14 12:25:28 +0200622/**
623 * @brief Learn whether a list instance has all the keys.
624 *
625 * @param[in] list List instance to check.
626 * @return non-zero if all the keys were found,
627 * @return 0 otherwise.
628 */
629static int
630lyd_insert_has_keys(const struct lyd_node *list)
631{
632 const struct lyd_node *key;
633 const struct lysc_node *skey = NULL;
634
635 assert(list->schema->nodetype == LYS_LIST);
Radek Krejcia1c1e542020-09-29 16:06:52 +0200636 key = lyd_child(list);
Michal Vasko751cb4d2020-07-14 12:25:28 +0200637 while ((skey = lys_getnext(skey, list->schema, NULL, 0)) && (skey->flags & LYS_KEY)) {
638 if (!key || (key->schema != skey)) {
639 /* key missing */
640 return 0;
641 }
642
643 key = key->next;
644 }
645
646 /* all keys found */
647 return 1;
Michal Vasko90932a92020-02-12 14:33:03 +0100648}
649
650void
Michal Vasko6ee6f432021-07-16 09:49:14 +0200651lyd_insert_node(struct lyd_node *parent, struct lyd_node **first_sibling_p, struct lyd_node *node, ly_bool last)
Michal Vasko90932a92020-02-12 14:33:03 +0100652{
Michal Vaskob104f112020-07-17 09:54:54 +0200653 struct lyd_node *anchor, *first_sibling;
Michal Vasko90932a92020-02-12 14:33:03 +0100654
Michal Vaskob104f112020-07-17 09:54:54 +0200655 /* inserting list without its keys is not supported */
656 assert((parent || first_sibling_p) && node && (node->hash || !node->schema));
Michal Vaskof756c942020-11-06 17:21:37 +0100657 assert(!parent || !parent->schema ||
658 (parent->schema->nodetype & (LYS_CONTAINER | LYS_LIST | LYS_RPC | LYS_ACTION | LYS_NOTIF)));
Michal Vasko9b368d32020-02-14 13:53:31 +0100659
Michal Vaskob104f112020-07-17 09:54:54 +0200660 if (!parent && first_sibling_p && (*first_sibling_p) && (*first_sibling_p)->parent) {
Michal Vasko9e685082021-01-29 14:49:09 +0100661 parent = lyd_parent(*first_sibling_p);
Michal Vasko9b368d32020-02-14 13:53:31 +0100662 }
Michal Vasko90932a92020-02-12 14:33:03 +0100663
Michal Vaskob104f112020-07-17 09:54:54 +0200664 /* get first sibling */
Michal Vasko9e685082021-01-29 14:49:09 +0100665 first_sibling = parent ? lyd_child(parent) : *first_sibling_p;
Michal Vasko9f96a052020-03-10 09:41:45 +0100666
Michal Vasko9beceb82022-04-05 12:14:15 +0200667 if (last || (first_sibling && (first_sibling->flags & LYD_EXT))) {
Michal Vasko6ee6f432021-07-16 09:49:14 +0200668 /* no next anchor */
669 anchor = NULL;
670 } else {
671 /* find the anchor, our next node, so we can insert before it */
672 anchor = lyd_insert_get_next_anchor(first_sibling, node);
Michal Vasko6a0bb252023-08-21 11:55:11 +0200673
674 /* cannot insert data node after opaque nodes */
675 if (node->schema && first_sibling && !first_sibling->prev->schema) {
676 anchor = first_sibling->prev;
677 while ((anchor != first_sibling) && !anchor->prev->schema) {
678 anchor = anchor->prev;
679 }
680 }
Michal Vasko6ee6f432021-07-16 09:49:14 +0200681 }
682
Michal Vaskob104f112020-07-17 09:54:54 +0200683 if (anchor) {
Michal Vasko6ee6f432021-07-16 09:49:14 +0200684 /* insert before the anchor */
Michal Vaskob104f112020-07-17 09:54:54 +0200685 lyd_insert_before_node(anchor, node);
Michal Vasko26123192020-11-09 21:02:34 +0100686 if (!parent && (*first_sibling_p == anchor)) {
687 /* move first sibling */
688 *first_sibling_p = node;
689 }
Michal Vaskob104f112020-07-17 09:54:54 +0200690 } else if (first_sibling) {
Michal Vasko6ee6f432021-07-16 09:49:14 +0200691 /* insert as the last node */
Michal Vaskob104f112020-07-17 09:54:54 +0200692 lyd_insert_after_node(first_sibling->prev, node);
693 } else if (parent) {
Michal Vasko6ee6f432021-07-16 09:49:14 +0200694 /* insert as the only child */
Michal Vaskob104f112020-07-17 09:54:54 +0200695 lyd_insert_only_child(parent, node);
Michal Vasko90932a92020-02-12 14:33:03 +0100696 } else {
Michal Vasko6ee6f432021-07-16 09:49:14 +0200697 /* insert as the only sibling */
Michal Vaskob104f112020-07-17 09:54:54 +0200698 *first_sibling_p = node;
699 }
700
701 /* insert into parent HT */
702 lyd_insert_hash(node);
703
704 /* finish hashes for our parent, if needed and possible */
Michal Vaskoa878a892023-08-18 12:22:07 +0200705 if (node->schema && (node->schema->flags & LYS_KEY) && parent && parent->schema && lyd_insert_has_keys(parent)) {
Michal Vaskob104f112020-07-17 09:54:54 +0200706 lyd_hash(parent);
707
708 /* now we can insert even the list into its parent HT */
709 lyd_insert_hash(parent);
Michal Vasko90932a92020-02-12 14:33:03 +0100710 }
Michal Vasko90932a92020-02-12 14:33:03 +0100711}
712
Michal Vasko717a4c32020-12-07 09:40:10 +0100713/**
714 * @brief Check schema place of a node to be inserted.
715 *
716 * @param[in] parent Schema node of the parent data node.
717 * @param[in] sibling Schema node of a sibling data node.
718 * @param[in] schema Schema node if the data node to be inserted.
719 * @return LY_SUCCESS on success.
720 * @return LY_EINVAL if the place is invalid.
721 */
Michal Vaskof03ed032020-03-04 13:31:44 +0100722static LY_ERR
Michal Vasko717a4c32020-12-07 09:40:10 +0100723lyd_insert_check_schema(const struct lysc_node *parent, const struct lysc_node *sibling, const struct lysc_node *schema)
Michal Vaskof03ed032020-03-04 13:31:44 +0100724{
725 const struct lysc_node *par2;
726
Michal Vasko62ed12d2020-05-21 10:08:25 +0200727 assert(!parent || !(parent->nodetype & (LYS_CASE | LYS_CHOICE)));
Michal Vasko717a4c32020-12-07 09:40:10 +0100728 assert(!sibling || !(sibling->nodetype & (LYS_CASE | LYS_CHOICE)));
729 assert(!schema || !(schema->nodetype & (LYS_CASE | LYS_CHOICE)));
Michal Vaskof03ed032020-03-04 13:31:44 +0100730
Michal Vasko717a4c32020-12-07 09:40:10 +0100731 if (!schema || (!parent && !sibling)) {
Michal Vasko71e795e2020-12-04 16:27:37 +0100732 /* opaque nodes can be inserted wherever */
733 return LY_SUCCESS;
734 }
735
Michal Vasko717a4c32020-12-07 09:40:10 +0100736 if (!parent) {
737 parent = lysc_data_parent(sibling);
738 }
739
Michal Vaskof03ed032020-03-04 13:31:44 +0100740 /* find schema parent */
Michal Vasko62ed12d2020-05-21 10:08:25 +0200741 par2 = lysc_data_parent(schema);
Michal Vaskof03ed032020-03-04 13:31:44 +0100742
743 if (parent) {
744 /* inner node */
745 if (par2 != parent) {
Michal Vaskob104f112020-07-17 09:54:54 +0200746 LOGERR(schema->module->ctx, LY_EINVAL, "Cannot insert, parent of \"%s\" is not \"%s\".", schema->name,
Michal Vasko69730152020-10-09 16:30:07 +0200747 parent->name);
Michal Vaskof03ed032020-03-04 13:31:44 +0100748 return LY_EINVAL;
749 }
750 } else {
751 /* top-level node */
752 if (par2) {
Radek Krejcif6d14cb2020-07-02 16:11:45 +0200753 LOGERR(schema->module->ctx, LY_EINVAL, "Cannot insert, node \"%s\" is not top-level.", schema->name);
Michal Vaskof03ed032020-03-04 13:31:44 +0100754 return LY_EINVAL;
755 }
756 }
757
758 return LY_SUCCESS;
759}
760
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100761LIBYANG_API_DEF LY_ERR
Michal Vaskob104f112020-07-17 09:54:54 +0200762lyd_insert_child(struct lyd_node *parent, struct lyd_node *node)
Michal Vaskof03ed032020-03-04 13:31:44 +0100763{
764 struct lyd_node *iter;
765
Michal Vasko0ab974d2021-02-24 13:18:26 +0100766 LY_CHECK_ARG_RET(NULL, parent, node, !parent->schema || (parent->schema->nodetype & LYD_NODE_INNER), LY_EINVAL);
Michal Vasko892f5bf2021-11-24 10:41:05 +0100767 LY_CHECK_CTX_EQUAL_RET(LYD_CTX(parent), LYD_CTX(node), LY_EINVAL);
Michal Vaskof03ed032020-03-04 13:31:44 +0100768
Michal Vasko717a4c32020-12-07 09:40:10 +0100769 LY_CHECK_RET(lyd_insert_check_schema(parent->schema, NULL, node->schema));
Michal Vaskof03ed032020-03-04 13:31:44 +0100770
Michal Vasko0ab974d2021-02-24 13:18:26 +0100771 if (node->schema && (node->schema->flags & LYS_KEY)) {
Michal Vaskoddd76592022-01-17 13:34:48 +0100772 LOGERR(LYD_CTX(parent), LY_EINVAL, "Cannot insert key \"%s\".", node->schema->name);
Michal Vaskof03ed032020-03-04 13:31:44 +0100773 return LY_EINVAL;
774 }
775
776 if (node->parent || node->prev->next) {
777 lyd_unlink_tree(node);
778 }
779
780 while (node) {
781 iter = node->next;
782 lyd_unlink_tree(node);
Michal Vasko6ee6f432021-07-16 09:49:14 +0200783 lyd_insert_node(parent, NULL, node, 0);
Michal Vaskof03ed032020-03-04 13:31:44 +0100784 node = iter;
785 }
786 return LY_SUCCESS;
787}
788
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100789LIBYANG_API_DEF LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +0200790lyplg_ext_insert(struct lyd_node *parent, struct lyd_node *first)
Michal Vaskoddd76592022-01-17 13:34:48 +0100791{
792 struct lyd_node *iter;
793
794 LY_CHECK_ARG_RET(NULL, parent, first, !first->parent, !first->prev->next,
795 !parent->schema || (parent->schema->nodetype & LYD_NODE_INNER), LY_EINVAL);
796
797 if (first->schema && (first->schema->flags & LYS_KEY)) {
798 LOGERR(LYD_CTX(parent), LY_EINVAL, "Cannot insert key \"%s\".", first->schema->name);
799 return LY_EINVAL;
800 }
801
802 while (first) {
803 iter = first->next;
804 lyd_unlink_tree(first);
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100805 lyd_insert_node(parent, NULL, first, 1);
Michal Vaskoddd76592022-01-17 13:34:48 +0100806 first = iter;
807 }
808 return LY_SUCCESS;
809}
810
811LIBYANG_API_DEF LY_ERR
Michal Vaskob104f112020-07-17 09:54:54 +0200812lyd_insert_sibling(struct lyd_node *sibling, struct lyd_node *node, struct lyd_node **first)
Michal Vaskob1b5c262020-03-05 14:29:47 +0100813{
814 struct lyd_node *iter;
815
Michal Vaskob104f112020-07-17 09:54:54 +0200816 LY_CHECK_ARG_RET(NULL, node, LY_EINVAL);
Michal Vaskob1b5c262020-03-05 14:29:47 +0100817
Michal Vaskob104f112020-07-17 09:54:54 +0200818 if (sibling) {
Michal Vasko717a4c32020-12-07 09:40:10 +0100819 LY_CHECK_RET(lyd_insert_check_schema(NULL, sibling->schema, node->schema));
Michal Vaskob1b5c262020-03-05 14:29:47 +0100820 }
821
Michal Vasko553d0b52020-12-04 16:27:52 +0100822 if (sibling == node) {
823 /* we need to keep the connection to siblings so we can insert into them */
824 sibling = sibling->prev;
825 }
826
Michal Vaskob1b5c262020-03-05 14:29:47 +0100827 if (node->parent || node->prev->next) {
828 lyd_unlink_tree(node);
829 }
830
831 while (node) {
Michal Vasko71e795e2020-12-04 16:27:37 +0100832 if (lysc_is_key(node->schema)) {
Michal Vaskob7be7a82020-08-20 09:09:04 +0200833 LOGERR(LYD_CTX(node), LY_EINVAL, "Cannot insert key \"%s\".", node->schema->name);
Michal Vaskob104f112020-07-17 09:54:54 +0200834 return LY_EINVAL;
835 }
836
Michal Vaskob1b5c262020-03-05 14:29:47 +0100837 iter = node->next;
838 lyd_unlink_tree(node);
Michal Vasko6ee6f432021-07-16 09:49:14 +0200839 lyd_insert_node(NULL, &sibling, node, 0);
Michal Vaskob1b5c262020-03-05 14:29:47 +0100840 node = iter;
841 }
Michal Vaskob1b5c262020-03-05 14:29:47 +0100842
Michal Vaskob104f112020-07-17 09:54:54 +0200843 if (first) {
844 /* find the first sibling */
845 *first = sibling;
846 while ((*first)->prev->next) {
847 *first = (*first)->prev;
Michal Vasko0249f7c2020-03-05 16:36:40 +0100848 }
849 }
850
851 return LY_SUCCESS;
852}
853
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100854LIBYANG_API_DEF LY_ERR
Michal Vaskof03ed032020-03-04 13:31:44 +0100855lyd_insert_before(struct lyd_node *sibling, struct lyd_node *node)
856{
Michal Vaskobce230b2022-04-19 09:55:31 +0200857 LY_CHECK_ARG_RET(NULL, sibling, node, sibling != node, LY_EINVAL);
Michal Vasko892f5bf2021-11-24 10:41:05 +0100858 LY_CHECK_CTX_EQUAL_RET(LYD_CTX(sibling), LYD_CTX(node), LY_EINVAL);
Michal Vaskof03ed032020-03-04 13:31:44 +0100859
Michal Vasko717a4c32020-12-07 09:40:10 +0100860 LY_CHECK_RET(lyd_insert_check_schema(NULL, sibling->schema, node->schema));
Michal Vaskof03ed032020-03-04 13:31:44 +0100861
Michal Vaskoab7a2bf2022-04-01 14:37:54 +0200862 if (node->schema && (!(node->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) || !(node->schema->flags & LYS_ORDBY_USER))) {
Michal Vaskob7be7a82020-08-20 09:09:04 +0200863 LOGERR(LYD_CTX(sibling), LY_EINVAL, "Can be used only for user-ordered nodes.");
Michal Vaskof03ed032020-03-04 13:31:44 +0100864 return LY_EINVAL;
865 }
Michal Vasko5c0b27a2022-04-19 09:56:02 +0200866 if (node->schema && sibling->schema && (node->schema != sibling->schema)) {
867 LOGERR(LYD_CTX(sibling), LY_EINVAL, "Cannot insert before a different schema node instance.");
Michal Vasko7508ef22022-02-22 14:13:10 +0100868 return LY_EINVAL;
869 }
Michal Vaskof03ed032020-03-04 13:31:44 +0100870
Michal Vasko4bc2ce32020-12-08 10:06:16 +0100871 lyd_unlink_tree(node);
872 lyd_insert_before_node(sibling, node);
873 lyd_insert_hash(node);
Michal Vaskof03ed032020-03-04 13:31:44 +0100874
Michal Vaskof03ed032020-03-04 13:31:44 +0100875 return LY_SUCCESS;
876}
877
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100878LIBYANG_API_DEF LY_ERR
Michal Vaskof03ed032020-03-04 13:31:44 +0100879lyd_insert_after(struct lyd_node *sibling, struct lyd_node *node)
880{
Michal Vaskobce230b2022-04-19 09:55:31 +0200881 LY_CHECK_ARG_RET(NULL, sibling, node, sibling != node, LY_EINVAL);
Michal Vasko892f5bf2021-11-24 10:41:05 +0100882 LY_CHECK_CTX_EQUAL_RET(LYD_CTX(sibling), LYD_CTX(node), LY_EINVAL);
Michal Vaskof03ed032020-03-04 13:31:44 +0100883
Michal Vasko717a4c32020-12-07 09:40:10 +0100884 LY_CHECK_RET(lyd_insert_check_schema(NULL, sibling->schema, node->schema));
Michal Vaskof03ed032020-03-04 13:31:44 +0100885
Michal Vaskoab7a2bf2022-04-01 14:37:54 +0200886 if (node->schema && (!(node->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) || !(node->schema->flags & LYS_ORDBY_USER))) {
Michal Vaskob7be7a82020-08-20 09:09:04 +0200887 LOGERR(LYD_CTX(sibling), LY_EINVAL, "Can be used only for user-ordered nodes.");
Michal Vaskof03ed032020-03-04 13:31:44 +0100888 return LY_EINVAL;
889 }
Michal Vasko5c0b27a2022-04-19 09:56:02 +0200890 if (node->schema && sibling->schema && (node->schema != sibling->schema)) {
891 LOGERR(LYD_CTX(sibling), LY_EINVAL, "Cannot insert after a different schema node instance.");
Michal Vasko7508ef22022-02-22 14:13:10 +0100892 return LY_EINVAL;
893 }
Michal Vaskof03ed032020-03-04 13:31:44 +0100894
Michal Vasko4bc2ce32020-12-08 10:06:16 +0100895 lyd_unlink_tree(node);
896 lyd_insert_after_node(sibling, node);
897 lyd_insert_hash(node);
Michal Vaskof03ed032020-03-04 13:31:44 +0100898
Michal Vaskof03ed032020-03-04 13:31:44 +0100899 return LY_SUCCESS;
900}
901
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100902LIBYANG_API_DEF void
Michal Vasko66d508c2021-07-21 16:07:09 +0200903lyd_unlink_siblings(struct lyd_node *node)
904{
905 struct lyd_node *next, *elem, *first = NULL;
906
907 LY_LIST_FOR_SAFE(node, next, elem) {
908 lyd_unlink_tree(elem);
909 lyd_insert_node(NULL, &first, elem, 1);
910 }
911}
912
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100913LIBYANG_API_DEF void
Michal Vaskof03ed032020-03-04 13:31:44 +0100914lyd_unlink_tree(struct lyd_node *node)
915{
916 struct lyd_node *iter;
917
918 if (!node) {
919 return;
920 }
921
Michal Vaskob104f112020-07-17 09:54:54 +0200922 /* update hashes while still linked into the tree */
923 lyd_unlink_hash(node);
924
Michal Vaskof03ed032020-03-04 13:31:44 +0100925 /* unlink from siblings */
926 if (node->prev->next) {
927 node->prev->next = node->next;
928 }
929 if (node->next) {
930 node->next->prev = node->prev;
931 } else {
932 /* unlinking the last node */
933 if (node->parent) {
934 iter = node->parent->child;
935 } else {
936 iter = node->prev;
937 while (iter->prev != node) {
938 iter = iter->prev;
939 }
940 }
941 /* update the "last" pointer from the first node */
942 iter->prev = node->prev;
943 }
944
945 /* unlink from parent */
946 if (node->parent) {
947 if (node->parent->child == node) {
948 /* the node is the first child */
949 node->parent->child = node->next;
950 }
951
Michal Vaskoab49dbe2020-07-17 12:32:47 +0200952 /* check for NP container whether its last non-default node is not being unlinked */
Michal Vasko4754d4a2022-12-01 10:11:21 +0100953 lyd_cont_set_dflt(lyd_parent(node));
Michal Vaskoab49dbe2020-07-17 12:32:47 +0200954
Michal Vaskof03ed032020-03-04 13:31:44 +0100955 node->parent = NULL;
956 }
957
958 node->next = NULL;
959 node->prev = node;
960}
961
Michal Vaskoa5da3292020-08-12 13:10:50 +0200962void
Michal Vasko871a0252020-11-11 18:35:24 +0100963lyd_insert_meta(struct lyd_node *parent, struct lyd_meta *meta, ly_bool clear_dflt)
Radek Krejci1798aae2020-07-14 13:26:06 +0200964{
965 struct lyd_meta *last, *iter;
966
967 assert(parent);
Michal Vaskoa5da3292020-08-12 13:10:50 +0200968
969 if (!meta) {
970 return;
971 }
Radek Krejci1798aae2020-07-14 13:26:06 +0200972
973 for (iter = meta; iter; iter = iter->next) {
974 iter->parent = parent;
975 }
976
977 /* insert as the last attribute */
978 if (parent->meta) {
Radek Krejci1e008d22020-08-17 11:37:37 +0200979 for (last = parent->meta; last->next; last = last->next) {}
Radek Krejci1798aae2020-07-14 13:26:06 +0200980 last->next = meta;
981 } else {
982 parent->meta = meta;
983 }
984
985 /* remove default flags from NP containers */
Michal Vasko871a0252020-11-11 18:35:24 +0100986 while (clear_dflt && parent && (parent->schema->nodetype == LYS_CONTAINER) && (parent->flags & LYD_DEFAULT)) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200987 parent->flags &= ~LYD_DEFAULT;
Michal Vasko9e685082021-01-29 14:49:09 +0100988 parent = lyd_parent(parent);
Radek Krejci1798aae2020-07-14 13:26:06 +0200989 }
Radek Krejci1798aae2020-07-14 13:26:06 +0200990}
991
992LY_ERR
Michal Vasko9f96a052020-03-10 09:41:45 +0100993lyd_create_meta(struct lyd_node *parent, struct lyd_meta **meta, const struct lys_module *mod, const char *name,
Michal Vasko989cdb42023-10-06 15:32:37 +0200994 size_t name_len, const char *value, size_t value_len, ly_bool is_utf8, ly_bool *dynamic, LY_VALUE_FORMAT format,
Michal Vaskoddd76592022-01-17 13:34:48 +0100995 void *prefix_data, uint32_t hints, const struct lysc_node *ctx_node, ly_bool clear_dflt, ly_bool *incomplete)
Michal Vasko90932a92020-02-12 14:33:03 +0100996{
Radek Krejci2efc45b2020-12-22 16:25:44 +0100997 LY_ERR ret = LY_SUCCESS;
Michal Vasko90932a92020-02-12 14:33:03 +0100998 struct lysc_ext_instance *ant = NULL;
Michal Vasko193dacd2022-10-13 08:43:05 +0200999 const struct lysc_type *ant_type;
Michal Vasko9f96a052020-03-10 09:41:45 +01001000 struct lyd_meta *mt, *last;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001001 LY_ARRAY_COUNT_TYPE u;
Michal Vasko90932a92020-02-12 14:33:03 +01001002
Michal Vasko9f96a052020-03-10 09:41:45 +01001003 assert((parent || meta) && mod);
Michal Vasko6f4cbb62020-02-28 11:15:47 +01001004
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001005 LY_ARRAY_FOR(mod->compiled->exts, u) {
Michal Vasko193dacd2022-10-13 08:43:05 +02001006 if (!strncmp(mod->compiled->exts[u].def->plugin->id, "ly2 metadata", 12) &&
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001007 !ly_strncmp(mod->compiled->exts[u].argument, name, name_len)) {
Michal Vasko90932a92020-02-12 14:33:03 +01001008 /* we have the annotation definition */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001009 ant = &mod->compiled->exts[u];
Michal Vasko90932a92020-02-12 14:33:03 +01001010 break;
1011 }
1012 }
1013 if (!ant) {
1014 /* attribute is not defined as a metadata annotation (RFC 7952) */
Radek Krejci2efc45b2020-12-22 16:25:44 +01001015 LOGVAL(mod->ctx, LYVE_REFERENCE, "Annotation definition for attribute \"%s:%.*s\" not found.",
Radek Krejci422afb12021-03-04 16:38:16 +01001016 mod->name, (int)name_len, name);
Radek Krejci2efc45b2020-12-22 16:25:44 +01001017 ret = LY_EINVAL;
1018 goto cleanup;
Michal Vasko90932a92020-02-12 14:33:03 +01001019 }
1020
Michal Vasko9f96a052020-03-10 09:41:45 +01001021 mt = calloc(1, sizeof *mt);
Radek Krejci2efc45b2020-12-22 16:25:44 +01001022 LY_CHECK_ERR_GOTO(!mt, LOGMEM(mod->ctx); ret = LY_EMEM, cleanup);
Michal Vasko9f96a052020-03-10 09:41:45 +01001023 mt->parent = parent;
1024 mt->annotation = ant;
Michal Vaskofbd037c2022-11-08 10:34:20 +01001025 lyplg_ext_get_storage(ant, LY_STMT_TYPE, sizeof ant_type, (const void **)&ant_type);
Michal Vasko989cdb42023-10-06 15:32:37 +02001026 ret = lyd_value_store(mod->ctx, &mt->value, ant_type, value, value_len, is_utf8, dynamic, format, prefix_data, hints,
Michal Vaskoddd76592022-01-17 13:34:48 +01001027 ctx_node, incomplete);
Radek Krejci2efc45b2020-12-22 16:25:44 +01001028 LY_CHECK_ERR_GOTO(ret, free(mt), cleanup);
1029 ret = lydict_insert(mod->ctx, name, name_len, &mt->name);
1030 LY_CHECK_ERR_GOTO(ret, free(mt), cleanup);
Michal Vasko90932a92020-02-12 14:33:03 +01001031
Michal Vasko6f4cbb62020-02-28 11:15:47 +01001032 /* insert as the last attribute */
1033 if (parent) {
Michal Vasko871a0252020-11-11 18:35:24 +01001034 lyd_insert_meta(parent, mt, clear_dflt);
Michal Vasko9f96a052020-03-10 09:41:45 +01001035 } else if (*meta) {
Radek Krejci1e008d22020-08-17 11:37:37 +02001036 for (last = *meta; last->next; last = last->next) {}
Michal Vasko9f96a052020-03-10 09:41:45 +01001037 last->next = mt;
Michal Vasko90932a92020-02-12 14:33:03 +01001038 }
1039
Michal Vasko9f96a052020-03-10 09:41:45 +01001040 if (meta) {
1041 *meta = mt;
Michal Vasko90932a92020-02-12 14:33:03 +01001042 }
Radek Krejci2efc45b2020-12-22 16:25:44 +01001043
1044cleanup:
Radek Krejci2efc45b2020-12-22 16:25:44 +01001045 return ret;
Michal Vasko90932a92020-02-12 14:33:03 +01001046}
1047
Michal Vaskoa5da3292020-08-12 13:10:50 +02001048void
1049lyd_insert_attr(struct lyd_node *parent, struct lyd_attr *attr)
1050{
1051 struct lyd_attr *last, *iter;
1052 struct lyd_node_opaq *opaq;
1053
1054 assert(parent && !parent->schema);
1055
1056 if (!attr) {
1057 return;
1058 }
1059
1060 opaq = (struct lyd_node_opaq *)parent;
1061 for (iter = attr; iter; iter = iter->next) {
1062 iter->parent = opaq;
1063 }
1064
1065 /* insert as the last attribute */
1066 if (opaq->attr) {
Radek Krejci1e008d22020-08-17 11:37:37 +02001067 for (last = opaq->attr; last->next; last = last->next) {}
Michal Vaskoa5da3292020-08-12 13:10:50 +02001068 last->next = attr;
1069 } else {
1070 opaq->attr = attr;
1071 }
1072}
1073
Michal Vasko52927e22020-03-16 17:26:14 +01001074LY_ERR
Michal Vaskofeca4fb2020-10-05 08:58:40 +02001075lyd_create_attr(struct lyd_node *parent, struct lyd_attr **attr, const struct ly_ctx *ctx, const char *name, size_t name_len,
Michal Vasko501af032020-11-11 20:27:44 +01001076 const char *prefix, size_t prefix_len, const char *module_key, size_t module_key_len, const char *value,
Radek Krejci8df109d2021-04-23 12:19:08 +02001077 size_t value_len, ly_bool *dynamic, LY_VALUE_FORMAT format, void *val_prefix_data, uint32_t hints)
Michal Vasko52927e22020-03-16 17:26:14 +01001078{
Radek Krejci011e4aa2020-09-04 15:22:31 +02001079 LY_ERR ret = LY_SUCCESS;
Radek Krejci1798aae2020-07-14 13:26:06 +02001080 struct lyd_attr *at, *last;
Michal Vasko52927e22020-03-16 17:26:14 +01001081
1082 assert(ctx && (parent || attr) && (!parent || !parent->schema));
Michal Vaskofeca4fb2020-10-05 08:58:40 +02001083 assert(name && name_len && format);
Michal Vasko52927e22020-03-16 17:26:14 +01001084
Michal Vasko2a3722d2021-06-16 11:52:39 +02001085 if (!value_len && (!dynamic || !*dynamic)) {
Michal Vasko52927e22020-03-16 17:26:14 +01001086 value = "";
1087 }
1088
1089 at = calloc(1, sizeof *at);
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01001090 LY_CHECK_ERR_RET(!at, LOGMEM(ctx); ly_free_prefix_data(format, val_prefix_data), LY_EMEM);
Radek Krejcid46e46a2020-09-15 14:22:42 +02001091
Michal Vaskoad92b672020-11-12 13:11:31 +01001092 LY_CHECK_GOTO(ret = lydict_insert(ctx, name, name_len, &at->name.name), finish);
Michal Vasko501af032020-11-11 20:27:44 +01001093 if (prefix_len) {
Michal Vaskoad92b672020-11-12 13:11:31 +01001094 LY_CHECK_GOTO(ret = lydict_insert(ctx, prefix, prefix_len, &at->name.prefix), finish);
Michal Vasko501af032020-11-11 20:27:44 +01001095 }
1096 if (module_key_len) {
Michal Vaskoad92b672020-11-12 13:11:31 +01001097 LY_CHECK_GOTO(ret = lydict_insert(ctx, module_key, module_key_len, &at->name.module_ns), finish);
Michal Vasko501af032020-11-11 20:27:44 +01001098 }
1099
Michal Vasko52927e22020-03-16 17:26:14 +01001100 if (dynamic && *dynamic) {
Radek Krejci011e4aa2020-09-04 15:22:31 +02001101 ret = lydict_insert_zc(ctx, (char *)value, &at->value);
1102 LY_CHECK_GOTO(ret, finish);
Michal Vasko52927e22020-03-16 17:26:14 +01001103 *dynamic = 0;
1104 } else {
Radek Krejci011e4aa2020-09-04 15:22:31 +02001105 LY_CHECK_GOTO(ret = lydict_insert(ctx, value, value_len, &at->value), finish);
Michal Vasko52927e22020-03-16 17:26:14 +01001106 }
Michal Vasko501af032020-11-11 20:27:44 +01001107 at->format = format;
1108 at->val_prefix_data = val_prefix_data;
1109 at->hints = hints;
Michal Vasko52927e22020-03-16 17:26:14 +01001110
1111 /* insert as the last attribute */
1112 if (parent) {
Michal Vaskoa5da3292020-08-12 13:10:50 +02001113 lyd_insert_attr(parent, at);
Michal Vasko52927e22020-03-16 17:26:14 +01001114 } else if (*attr) {
Radek Krejci1e008d22020-08-17 11:37:37 +02001115 for (last = *attr; last->next; last = last->next) {}
Michal Vasko52927e22020-03-16 17:26:14 +01001116 last->next = at;
1117 }
1118
Radek Krejci011e4aa2020-09-04 15:22:31 +02001119finish:
1120 if (ret) {
1121 lyd_free_attr_single(ctx, at);
1122 } else if (attr) {
Michal Vasko52927e22020-03-16 17:26:14 +01001123 *attr = at;
1124 }
1125 return LY_SUCCESS;
1126}
1127
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001128LIBYANG_API_DEF const struct lyd_node_term *
Michal Vasko004d3152020-06-11 19:59:22 +02001129lyd_target(const struct ly_path *path, const struct lyd_node *tree)
Radek Krejci084289f2019-07-09 17:35:30 +02001130{
Michal Vasko90189962023-02-28 12:10:34 +01001131 struct lyd_node *target = NULL;
Radek Krejci084289f2019-07-09 17:35:30 +02001132
Michal Vasko90189962023-02-28 12:10:34 +01001133 lyd_find_target(path, tree, &target);
Radek Krejci084289f2019-07-09 17:35:30 +02001134
Michal Vasko004d3152020-06-11 19:59:22 +02001135 return (struct lyd_node_term *)target;
Radek Krejci084289f2019-07-09 17:35:30 +02001136}
1137
aPiecek2f63f952021-03-30 12:22:18 +02001138/**
1139 * @brief Check the equality of the two schemas from different contexts.
1140 *
1141 * @param schema1 of first node.
1142 * @param schema2 of second node.
1143 * @return 1 if the schemas are equal otherwise 0.
1144 */
1145static ly_bool
1146lyd_compare_schema_equal(const struct lysc_node *schema1, const struct lysc_node *schema2)
1147{
1148 if (!schema1 && !schema2) {
1149 return 1;
1150 } else if (!schema1 || !schema2) {
1151 return 0;
1152 }
1153
1154 assert(schema1->module->ctx != schema2->module->ctx);
1155
1156 if (schema1->nodetype != schema2->nodetype) {
1157 return 0;
1158 }
1159
1160 if (strcmp(schema1->name, schema2->name)) {
1161 return 0;
1162 }
1163
1164 if (strcmp(schema1->module->name, schema2->module->name)) {
1165 return 0;
1166 }
1167
aPiecek2f63f952021-03-30 12:22:18 +02001168 return 1;
1169}
1170
1171/**
1172 * @brief Check the equality of the schemas for all parent nodes.
1173 *
1174 * Both nodes must be from different contexts.
1175 *
1176 * @param node1 Data of first node.
1177 * @param node2 Data of second node.
1178 * @return 1 if the all related parental schemas are equal otherwise 0.
1179 */
1180static ly_bool
1181lyd_compare_schema_parents_equal(const struct lyd_node *node1, const struct lyd_node *node2)
1182{
1183 const struct lysc_node *parent1, *parent2;
1184
1185 assert(node1 && node2);
1186
1187 for (parent1 = node1->schema->parent, parent2 = node2->schema->parent;
1188 parent1 && parent2;
1189 parent1 = parent1->parent, parent2 = parent2->parent) {
1190 if (!lyd_compare_schema_equal(parent1, parent2)) {
1191 return 0;
1192 }
1193 }
1194
1195 if (parent1 || parent2) {
1196 return 0;
1197 }
1198
1199 return 1;
1200}
1201
1202/**
Michal Vaskodf8ebf62022-11-10 10:33:28 +01001203 * @brief Compare 2 nodes values including opaque node values.
1204 *
1205 * @param[in] node1 First node to compare.
1206 * @param[in] node2 Second node to compare.
1207 * @return LY_SUCCESS if equal.
1208 * @return LY_ENOT if not equal.
1209 * @return LY_ERR on error.
1210 */
1211static LY_ERR
1212lyd_compare_single_value(const struct lyd_node *node1, const struct lyd_node *node2)
1213{
1214 const struct lyd_node_opaq *opaq1 = NULL, *opaq2 = NULL;
1215 const char *val1, *val2, *col;
1216 const struct lys_module *mod;
1217 char *val_dyn = NULL;
1218 LY_ERR rc = LY_SUCCESS;
1219
1220 if (!node1->schema) {
1221 opaq1 = (struct lyd_node_opaq *)node1;
1222 }
1223 if (!node2->schema) {
1224 opaq2 = (struct lyd_node_opaq *)node2;
1225 }
1226
1227 if (opaq1 && opaq2 && (opaq1->format == LY_VALUE_XML) && (opaq2->format == LY_VALUE_XML)) {
1228 /* opaque XML and opaque XML node */
1229 if (lyxml_value_compare(LYD_CTX(node1), opaq1->value, opaq1->val_prefix_data, LYD_CTX(node2), opaq2->value,
1230 opaq2->val_prefix_data)) {
1231 return LY_ENOT;
1232 }
1233 return LY_SUCCESS;
1234 }
1235
1236 /* get their values */
1237 if (opaq1 && ((opaq1->format == LY_VALUE_XML) || (opaq1->format == LY_VALUE_STR_NS)) && (col = strchr(opaq1->value, ':'))) {
1238 /* XML value with a prefix, try to transform it into a JSON (canonical) value */
1239 mod = ly_resolve_prefix(LYD_CTX(node1), opaq1->value, col - opaq1->value, opaq1->format, opaq1->val_prefix_data);
1240 if (!mod) {
1241 /* unable to compare */
1242 return LY_ENOT;
1243 }
1244
1245 if (asprintf(&val_dyn, "%s%s", mod->name, col) == -1) {
1246 LOGMEM(LYD_CTX(node1));
1247 return LY_EMEM;
1248 }
1249 val1 = val_dyn;
1250 } else {
1251 val1 = lyd_get_value(node1);
1252 }
1253 if (opaq2 && ((opaq2->format == LY_VALUE_XML) || (opaq2->format == LY_VALUE_STR_NS)) && (col = strchr(opaq2->value, ':'))) {
1254 mod = ly_resolve_prefix(LYD_CTX(node2), opaq2->value, col - opaq2->value, opaq2->format, opaq2->val_prefix_data);
1255 if (!mod) {
1256 return LY_ENOT;
1257 }
1258
1259 assert(!val_dyn);
1260 if (asprintf(&val_dyn, "%s%s", mod->name, col) == -1) {
1261 LOGMEM(LYD_CTX(node2));
1262 return LY_EMEM;
1263 }
1264 val2 = val_dyn;
1265 } else {
1266 val2 = lyd_get_value(node2);
1267 }
1268
1269 /* compare values */
1270 if (strcmp(val1, val2)) {
1271 rc = LY_ENOT;
1272 }
1273
1274 free(val_dyn);
1275 return rc;
1276}
1277
1278/**
Michal Vaskof277d362023-04-24 09:08:31 +02001279 * @brief Compare 2 data nodes if they are equivalent regarding the schema tree.
1280 *
1281 * Works correctly even if @p node1 and @p node2 have different contexts.
1282 *
1283 * @param[in] node1 The first node to compare.
1284 * @param[in] node2 The second node to compare.
1285 * @param[in] options Various @ref datacompareoptions.
1286 * @param[in] parental_schemas_checked Flag set if parent schemas were checked for match.
1287 * @return LY_SUCCESS if the nodes are equivalent.
1288 * @return LY_ENOT if the nodes are not equivalent.
aPiecek2f63f952021-03-30 12:22:18 +02001289 */
1290static LY_ERR
Michal Vaskof277d362023-04-24 09:08:31 +02001291lyd_compare_single_schema(const struct lyd_node *node1, const struct lyd_node *node2, uint32_t options,
Michal Vaskodf8ebf62022-11-10 10:33:28 +01001292 ly_bool parental_schemas_checked)
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001293{
aPiecek2f63f952021-03-30 12:22:18 +02001294 if (LYD_CTX(node1) == LYD_CTX(node2)) {
1295 /* same contexts */
Michal Vaskodf8ebf62022-11-10 10:33:28 +01001296 if (options & LYD_COMPARE_OPAQ) {
1297 if (lyd_node_schema(node1) != lyd_node_schema(node2)) {
1298 return LY_ENOT;
1299 }
1300 } else {
1301 if (node1->schema != node2->schema) {
1302 return LY_ENOT;
1303 }
aPiecek2f63f952021-03-30 12:22:18 +02001304 }
1305 } else {
1306 /* different contexts */
1307 if (!lyd_compare_schema_equal(node1->schema, node2->schema)) {
1308 return LY_ENOT;
1309 }
1310 if (!parental_schemas_checked) {
1311 if (!lyd_compare_schema_parents_equal(node1, node2)) {
1312 return LY_ENOT;
1313 }
1314 parental_schemas_checked = 1;
1315 }
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001316 }
1317
Michal Vaskof277d362023-04-24 09:08:31 +02001318 return LY_SUCCESS;
1319}
1320
1321/**
1322 * @brief Compare 2 data nodes if they are equivalent regarding the data they contain.
1323 *
1324 * Works correctly even if @p node1 and @p node2 have different contexts.
1325 *
1326 * @param[in] node1 The first node to compare.
1327 * @param[in] node2 The second node to compare.
1328 * @param[in] options Various @ref datacompareoptions.
1329 * @return LY_SUCCESS if the nodes are equivalent.
1330 * @return LY_ENOT if the nodes are not equivalent.
1331 */
1332static LY_ERR
1333lyd_compare_single_data(const struct lyd_node *node1, const struct lyd_node *node2, uint32_t options)
1334{
1335 const struct lyd_node *iter1, *iter2;
1336 struct lyd_node_any *any1, *any2;
1337 int len1, len2;
1338 LY_ERR r;
1339
Michal Vaskodf8ebf62022-11-10 10:33:28 +01001340 if (!(options & LYD_COMPARE_OPAQ) && (node1->hash != node2->hash)) {
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001341 return LY_ENOT;
1342 }
aPiecek2f63f952021-03-30 12:22:18 +02001343 /* equal hashes do not mean equal nodes, they can be just in collision so the nodes must be checked explicitly */
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001344
Michal Vaskodf8ebf62022-11-10 10:33:28 +01001345 if (!node1->schema || !node2->schema) {
1346 if (!(options & LYD_COMPARE_OPAQ) && ((node1->schema && !node2->schema) || (!node1->schema && node2->schema))) {
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001347 return LY_ENOT;
1348 }
Michal Vasko7b0500d2023-03-20 15:22:46 +01001349 if ((!node1->schema && !node2->schema) || (node1->schema && (node1->schema->nodetype & LYD_NODE_TERM)) ||
1350 (node2->schema && (node2->schema->nodetype & LYD_NODE_TERM))) {
1351 /* compare values only if there are any to compare */
1352 if ((r = lyd_compare_single_value(node1, node2))) {
1353 return r;
1354 }
Michal Vasko52927e22020-03-16 17:26:14 +01001355 }
Michal Vaskodf8ebf62022-11-10 10:33:28 +01001356
Michal Vasko52927e22020-03-16 17:26:14 +01001357 if (options & LYD_COMPARE_FULL_RECURSION) {
Michal Vaskof277d362023-04-24 09:08:31 +02001358 return lyd_compare_siblings_(lyd_child(node1), lyd_child(node2), options, 1);
Michal Vasko52927e22020-03-16 17:26:14 +01001359 }
1360 return LY_SUCCESS;
1361 } else {
1362 switch (node1->schema->nodetype) {
1363 case LYS_LEAF:
1364 case LYS_LEAFLIST:
1365 if (options & LYD_COMPARE_DEFAULTS) {
1366 if ((node1->flags & LYD_DEFAULT) != (node2->flags & LYD_DEFAULT)) {
1367 return LY_ENOT;
1368 }
1369 }
Michal Vaskodf8ebf62022-11-10 10:33:28 +01001370 if ((r = lyd_compare_single_value(node1, node2))) {
1371 return r;
aPiecek2f63f952021-03-30 12:22:18 +02001372 }
1373
aPiecek2f63f952021-03-30 12:22:18 +02001374 return LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +01001375 case LYS_CONTAINER:
Michal Vaskoc8187dc2022-05-13 12:26:40 +02001376 case LYS_RPC:
1377 case LYS_ACTION:
1378 case LYS_NOTIF:
Michal Vaskoa38adc72023-04-25 10:14:28 +02001379 /* implicit container is always equal to a container with non-default descendants */
Michal Vasko52927e22020-03-16 17:26:14 +01001380 if (options & LYD_COMPARE_FULL_RECURSION) {
Michal Vaskof277d362023-04-24 09:08:31 +02001381 return lyd_compare_siblings_(lyd_child(node1), lyd_child(node2), options, 1);
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001382 }
1383 return LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +01001384 case LYS_LIST:
Michal Vasko9e685082021-01-29 14:49:09 +01001385 iter1 = lyd_child(node1);
1386 iter2 = lyd_child(node2);
Michal Vasko52927e22020-03-16 17:26:14 +01001387
Michal Vaskoee9b9482023-06-19 13:17:48 +02001388 if (options & LYD_COMPARE_FULL_RECURSION) {
Michal Vaskof277d362023-04-24 09:08:31 +02001389 return lyd_compare_siblings_(iter1, iter2, options, 1);
Michal Vaskoee9b9482023-06-19 13:17:48 +02001390 } else if (node1->schema->flags & LYS_KEYLESS) {
1391 /* always equal */
1392 return LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +01001393 }
Michal Vaskoee9b9482023-06-19 13:17:48 +02001394
1395 /* lists with keys, their equivalence is based on their keys */
1396 for (const struct lysc_node *key = lysc_node_child(node1->schema);
1397 key && (key->flags & LYS_KEY);
1398 key = key->next) {
1399 if (!iter1 || !iter2) {
1400 return (iter1 == iter2) ? LY_SUCCESS : LY_ENOT;
1401 }
1402 r = lyd_compare_single_schema(iter1, iter2, options, 1);
1403 LY_CHECK_RET(r);
1404 r = lyd_compare_single_data(iter1, iter2, options);
1405 LY_CHECK_RET(r);
1406
1407 iter1 = iter1->next;
1408 iter2 = iter2->next;
1409 }
1410
1411 return LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +01001412 case LYS_ANYXML:
1413 case LYS_ANYDATA:
Michal Vasko22df3f02020-08-24 13:29:22 +02001414 any1 = (struct lyd_node_any *)node1;
1415 any2 = (struct lyd_node_any *)node2;
Michal Vasko52927e22020-03-16 17:26:14 +01001416
1417 if (any1->value_type != any2->value_type) {
1418 return LY_ENOT;
1419 }
1420 switch (any1->value_type) {
1421 case LYD_ANYDATA_DATATREE:
Michal Vaskof277d362023-04-24 09:08:31 +02001422 return lyd_compare_siblings_(any1->value.tree, any2->value.tree, options, 1);
Michal Vasko52927e22020-03-16 17:26:14 +01001423 case LYD_ANYDATA_STRING:
1424 case LYD_ANYDATA_XML:
1425 case LYD_ANYDATA_JSON:
Michal Vasko3b4ec412022-09-22 15:24:14 +02001426 if ((!any1->value.str && any2->value.str) || (any1->value.str && !any2->value.str)) {
1427 return LY_ENOT;
1428 } else if (!any1->value.str && !any2->value.str) {
1429 return LY_SUCCESS;
1430 }
Michal Vasko52927e22020-03-16 17:26:14 +01001431 len1 = strlen(any1->value.str);
1432 len2 = strlen(any2->value.str);
Michal Vasko69730152020-10-09 16:30:07 +02001433 if ((len1 != len2) || strcmp(any1->value.str, any2->value.str)) {
Michal Vasko52927e22020-03-16 17:26:14 +01001434 return LY_ENOT;
1435 }
1436 return LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +01001437 case LYD_ANYDATA_LYB:
Michal Vasko60ea6352020-06-29 13:39:39 +02001438 len1 = lyd_lyb_data_length(any1->value.mem);
1439 len2 = lyd_lyb_data_length(any2->value.mem);
Michal Vasko2b97d472022-08-17 09:29:03 +02001440 if ((len1 == -1) || (len2 == -1) || (len1 != len2) || memcmp(any1->value.mem, any2->value.mem, len1)) {
Michal Vasko52927e22020-03-16 17:26:14 +01001441 return LY_ENOT;
1442 }
1443 return LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +01001444 }
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001445 }
1446 }
1447
Michal Vaskob7be7a82020-08-20 09:09:04 +02001448 LOGINT(LYD_CTX(node1));
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001449 return LY_EINT;
1450}
Radek Krejci22ebdba2019-07-25 13:59:43 +02001451
Michal Vaskof277d362023-04-24 09:08:31 +02001452/**
1453 * @brief Compare all siblings at a node level.
1454 *
1455 * @param[in] node1 First sibling list.
1456 * @param[in] node2 Second sibling list.
1457 * @param[in] options Various @ref datacompareoptions.
1458 * @param[in] parental_schemas_checked Flag set if parent schemas were checked for match.
1459 * @return LY_SUCCESS if equal.
1460 * @return LY_ENOT if not equal.
1461 * @return LY_ERR on error.
1462 */
1463static LY_ERR
1464lyd_compare_siblings_(const struct lyd_node *node1, const struct lyd_node *node2, uint32_t options,
1465 ly_bool parental_schemas_checked)
1466{
1467 LY_ERR r;
1468 const struct lyd_node *iter2;
1469
1470 while (node1 && node2) {
1471 /* schema match */
1472 r = lyd_compare_single_schema(node1, node2, options, parental_schemas_checked);
1473 LY_CHECK_RET(r);
1474
1475 if (node1->schema && (((node1->schema->nodetype == LYS_LIST) && !(node1->schema->flags & LYS_KEYLESS)) ||
1476 ((node1->schema->nodetype == LYS_LEAFLIST) && (node1->schema->flags & LYS_CONFIG_W))) &&
1477 (node1->schema->flags & LYS_ORDBY_SYSTEM)) {
1478 /* find a matching instance in case they are ordered differently */
1479 r = lyd_find_sibling_first(node2, node1, (struct lyd_node **)&iter2);
1480 if (r == LY_ENOTFOUND) {
1481 /* no matching instance, data not equal */
1482 r = LY_ENOT;
1483 }
1484 LY_CHECK_RET(r);
1485 } else {
1486 /* compare with the current node */
1487 iter2 = node2;
1488 }
1489
1490 /* data match */
1491 r = lyd_compare_single_data(node1, iter2, options | LYD_COMPARE_FULL_RECURSION);
1492 LY_CHECK_RET(r);
1493
1494 node1 = node1->next;
1495 node2 = node2->next;
1496 }
1497
1498 return (node1 || node2) ? LY_ENOT : LY_SUCCESS;
1499}
1500
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001501LIBYANG_API_DEF LY_ERR
aPiecek2f63f952021-03-30 12:22:18 +02001502lyd_compare_single(const struct lyd_node *node1, const struct lyd_node *node2, uint32_t options)
1503{
Michal Vaskof277d362023-04-24 09:08:31 +02001504 LY_ERR r;
1505
1506 if (!node1 || !node2) {
1507 return (node1 == node2) ? LY_SUCCESS : LY_ENOT;
1508 }
1509
1510 /* schema match */
1511 if ((r = lyd_compare_single_schema(node1, node2, options, 0))) {
1512 return r;
1513 }
1514
1515 /* data match */
1516 return lyd_compare_single_data(node1, node2, options);
aPiecek2f63f952021-03-30 12:22:18 +02001517}
1518
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001519LIBYANG_API_DEF LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02001520lyd_compare_siblings(const struct lyd_node *node1, const struct lyd_node *node2, uint32_t options)
Michal Vasko8f359bf2020-07-28 10:41:15 +02001521{
Michal Vaskof277d362023-04-24 09:08:31 +02001522 return lyd_compare_siblings_(node1, node2, options, 0);
Michal Vasko8f359bf2020-07-28 10:41:15 +02001523}
1524
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001525LIBYANG_API_DEF LY_ERR
Michal Vasko21725742020-06-29 11:49:25 +02001526lyd_compare_meta(const struct lyd_meta *meta1, const struct lyd_meta *meta2)
1527{
1528 if (!meta1 || !meta2) {
1529 if (meta1 == meta2) {
1530 return LY_SUCCESS;
1531 } else {
1532 return LY_ENOT;
1533 }
1534 }
1535
Michal Vaskoa8083062020-11-06 17:22:10 +01001536 if ((meta1->annotation->module->ctx != meta2->annotation->module->ctx) || (meta1->annotation != meta2->annotation)) {
Michal Vasko21725742020-06-29 11:49:25 +02001537 return LY_ENOT;
1538 }
1539
Michal Vasko21725742020-06-29 11:49:25 +02001540 return meta1->value.realtype->plugin->compare(&meta1->value, &meta2->value);
1541}
1542
Radek Krejci22ebdba2019-07-25 13:59:43 +02001543/**
Michal Vasko9cf62422021-07-01 13:29:32 +02001544 * @brief Create a copy of the attribute.
1545 *
1546 * @param[in] attr Attribute to copy.
1547 * @param[in] node Opaque where to append the new attribute.
1548 * @param[out] dup Optional created attribute copy.
1549 * @return LY_ERR value.
1550 */
1551static LY_ERR
1552lyd_dup_attr_single(const struct lyd_attr *attr, struct lyd_node *node, struct lyd_attr **dup)
1553{
1554 LY_ERR ret = LY_SUCCESS;
1555 struct lyd_attr *a, *last;
1556 struct lyd_node_opaq *opaq = (struct lyd_node_opaq *)node;
1557
1558 LY_CHECK_ARG_RET(NULL, attr, node, !node->schema, LY_EINVAL);
1559
1560 /* create a copy */
1561 a = calloc(1, sizeof *attr);
1562 LY_CHECK_ERR_RET(!a, LOGMEM(LYD_CTX(node)), LY_EMEM);
1563
1564 LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), attr->name.name, 0, &a->name.name), finish);
1565 LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), attr->name.prefix, 0, &a->name.prefix), finish);
1566 LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), attr->name.module_ns, 0, &a->name.module_ns), finish);
1567 LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), attr->value, 0, &a->value), finish);
1568 a->hints = attr->hints;
1569 a->format = attr->format;
1570 if (attr->val_prefix_data) {
1571 ret = ly_dup_prefix_data(LYD_CTX(node), attr->format, attr->val_prefix_data, &a->val_prefix_data);
1572 LY_CHECK_GOTO(ret, finish);
1573 }
1574
1575 /* insert as the last attribute */
1576 a->parent = opaq;
1577 if (opaq->attr) {
1578 for (last = opaq->attr; last->next; last = last->next) {}
1579 last->next = a;
1580 } else {
1581 opaq->attr = a;
1582 }
1583
1584finish:
1585 if (ret) {
1586 lyd_free_attr_single(LYD_CTX(node), a);
1587 } else if (dup) {
1588 *dup = a;
1589 }
1590 return LY_SUCCESS;
1591}
1592
1593/**
Michal Vaskoddd76592022-01-17 13:34:48 +01001594 * @brief Find @p schema equivalent in @p trg_ctx.
1595 *
1596 * @param[in] schema Schema node to find.
1597 * @param[in] trg_ctx Target context to search in.
1598 * @param[in] parent Data parent of @p schema, if any.
Michal Vaskoaf3df492022-12-02 14:03:52 +01001599 * @param[in] log Whether to log directly.
Michal Vaskoddd76592022-01-17 13:34:48 +01001600 * @param[out] trg_schema Found schema from @p trg_ctx to use.
1601 * @return LY_RRR value.
1602 */
1603static LY_ERR
Michal Vaskoaf3df492022-12-02 14:03:52 +01001604lyd_find_schema_ctx(const struct lysc_node *schema, const struct ly_ctx *trg_ctx, const struct lyd_node *parent,
1605 ly_bool log, const struct lysc_node **trg_schema)
Michal Vaskoddd76592022-01-17 13:34:48 +01001606{
Michal Vasko9beceb82022-04-05 12:14:15 +02001607 const struct lysc_node *src_parent = NULL, *trg_parent = NULL, *sp, *tp;
1608 const struct lys_module *trg_mod = NULL;
Michal Vaskoddd76592022-01-17 13:34:48 +01001609 char *path;
1610
1611 if (!schema) {
1612 /* opaque node */
1613 *trg_schema = NULL;
1614 return LY_SUCCESS;
1615 }
1616
Michal Vasko9beceb82022-04-05 12:14:15 +02001617 if (lysc_data_parent(schema) && parent && parent->schema) {
Michal Vaskoddd76592022-01-17 13:34:48 +01001618 /* start from schema parent */
Michal Vasko9beceb82022-04-05 12:14:15 +02001619 trg_parent = parent->schema;
1620 src_parent = lysc_data_parent(schema);
1621 }
1622
1623 do {
1624 /* find the next parent */
1625 sp = schema;
Michal Vaskob6808202022-12-02 14:04:23 +01001626 while (lysc_data_parent(sp) != src_parent) {
1627 sp = lysc_data_parent(sp);
Michal Vasko9beceb82022-04-05 12:14:15 +02001628 }
1629 src_parent = sp;
1630
1631 if (!src_parent->parent) {
1632 /* find the module first */
1633 trg_mod = ly_ctx_get_module_implemented(trg_ctx, src_parent->module->name);
1634 if (!trg_mod) {
Michal Vaskoaf3df492022-12-02 14:03:52 +01001635 if (log) {
1636 LOGERR(trg_ctx, LY_ENOTFOUND, "Module \"%s\" not present/implemented in the target context.",
1637 src_parent->module->name);
1638 }
Michal Vasko9beceb82022-04-05 12:14:15 +02001639 return LY_ENOTFOUND;
1640 }
1641 }
1642
1643 /* find the next parent */
1644 assert(trg_parent || trg_mod);
1645 tp = NULL;
1646 while ((tp = lys_getnext(tp, trg_parent, trg_mod ? trg_mod->compiled : NULL, 0))) {
1647 if (!strcmp(tp->name, src_parent->name) && !strcmp(tp->module->name, src_parent->module->name)) {
1648 break;
1649 }
1650 }
1651 if (!tp) {
1652 /* schema node not found */
Michal Vaskoaf3df492022-12-02 14:03:52 +01001653 if (log) {
1654 path = lysc_path(src_parent, LYSC_PATH_LOG, NULL, 0);
1655 LOGERR(trg_ctx, LY_ENOTFOUND, "Schema node \"%s\" not found in the target context.", path);
1656 free(path);
1657 }
Michal Vaskoddd76592022-01-17 13:34:48 +01001658 return LY_ENOTFOUND;
1659 }
Michal Vaskoddd76592022-01-17 13:34:48 +01001660
Michal Vasko9beceb82022-04-05 12:14:15 +02001661 trg_parent = tp;
1662 } while (schema != src_parent);
Michal Vaskoddd76592022-01-17 13:34:48 +01001663
Michal Vasko9beceb82022-04-05 12:14:15 +02001664 /* success */
1665 *trg_schema = trg_parent;
1666 return LY_SUCCESS;
Michal Vaskoddd76592022-01-17 13:34:48 +01001667}
1668
1669/**
Michal Vasko52927e22020-03-16 17:26:14 +01001670 * @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 +02001671 *
Michal Vaskoddd76592022-01-17 13:34:48 +01001672 * 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 +02001673 *
Michal Vaskoddd76592022-01-17 13:34:48 +01001674 * @param[in] node Node to duplicate.
1675 * @param[in] trg_ctx Target context for duplicated nodes.
Radek Krejcif8b95172020-05-15 14:51:06 +02001676 * @param[in] parent Parent to insert into, NULL for top-level sibling.
Michal Vasko67177e52021-08-25 11:15:15 +02001677 * @param[in] insert_last Whether the duplicated node can be inserted as the last child of @p parent. Set for
1678 * recursive duplication as an optimization.
Radek Krejcif8b95172020-05-15 14:51:06 +02001679 * @param[in,out] first First sibling, NULL if no top-level sibling exist yet. Can be also NULL if @p parent is set.
1680 * @param[in] options Bitmask of options flags, see @ref dupoptions.
Michal Vaskoddd76592022-01-17 13:34:48 +01001681 * @param[out] dup_p Pointer where the created duplicated node is placed (besides connecting it to @p parent / @p first).
1682 * @return LY_ERR value.
Radek Krejci22ebdba2019-07-25 13:59:43 +02001683 */
Michal Vasko52927e22020-03-16 17:26:14 +01001684static LY_ERR
Michal Vaskoddd76592022-01-17 13:34:48 +01001685lyd_dup_r(const struct lyd_node *node, const struct ly_ctx *trg_ctx, struct lyd_node *parent, ly_bool insert_last,
1686 struct lyd_node **first, uint32_t options, struct lyd_node **dup_p)
Radek Krejci22ebdba2019-07-25 13:59:43 +02001687{
Michal Vasko52927e22020-03-16 17:26:14 +01001688 LY_ERR ret;
Radek Krejci22ebdba2019-07-25 13:59:43 +02001689 struct lyd_node *dup = NULL;
Michal Vasko61551fa2020-07-09 15:45:45 +02001690 struct lyd_meta *meta;
Michal Vasko9cf62422021-07-01 13:29:32 +02001691 struct lyd_attr *attr;
Michal Vasko61551fa2020-07-09 15:45:45 +02001692 struct lyd_node_any *any;
Michal Vaskoddd76592022-01-17 13:34:48 +01001693 const struct lysc_type *type;
1694 const char *val_can;
Radek Krejci22ebdba2019-07-25 13:59:43 +02001695
Michal Vasko52927e22020-03-16 17:26:14 +01001696 LY_CHECK_ARG_RET(NULL, node, LY_EINVAL);
Radek Krejci22ebdba2019-07-25 13:59:43 +02001697
Michal Vasko19175b62022-04-01 09:17:07 +02001698 if (node->flags & LYD_EXT) {
Michal Vasko53ac4dd2022-06-07 10:56:08 +02001699 if (options & LYD_DUP_NO_EXT) {
1700 /* no not duplicate this subtree */
1701 return LY_SUCCESS;
1702 }
1703
Michal Vasko19175b62022-04-01 09:17:07 +02001704 /* we need to use the same context */
1705 trg_ctx = LYD_CTX(node);
1706 }
1707
Michal Vasko52927e22020-03-16 17:26:14 +01001708 if (!node->schema) {
1709 dup = calloc(1, sizeof(struct lyd_node_opaq));
Michal Vaskoddd76592022-01-17 13:34:48 +01001710 ((struct lyd_node_opaq *)dup)->ctx = trg_ctx;
Michal Vasko52927e22020-03-16 17:26:14 +01001711 } else {
1712 switch (node->schema->nodetype) {
Michal Vasko1bf09392020-03-27 12:38:10 +01001713 case LYS_RPC:
Michal Vasko52927e22020-03-16 17:26:14 +01001714 case LYS_ACTION:
1715 case LYS_NOTIF:
1716 case LYS_CONTAINER:
1717 case LYS_LIST:
1718 dup = calloc(1, sizeof(struct lyd_node_inner));
1719 break;
1720 case LYS_LEAF:
1721 case LYS_LEAFLIST:
1722 dup = calloc(1, sizeof(struct lyd_node_term));
1723 break;
1724 case LYS_ANYDATA:
1725 case LYS_ANYXML:
1726 dup = calloc(1, sizeof(struct lyd_node_any));
1727 break;
1728 default:
Michal Vaskoddd76592022-01-17 13:34:48 +01001729 LOGINT(trg_ctx);
Michal Vasko52927e22020-03-16 17:26:14 +01001730 ret = LY_EINT;
1731 goto error;
1732 }
Radek Krejci22ebdba2019-07-25 13:59:43 +02001733 }
Michal Vaskoddd76592022-01-17 13:34:48 +01001734 LY_CHECK_ERR_GOTO(!dup, LOGMEM(trg_ctx); ret = LY_EMEM, error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02001735
Michal Vaskof6df0a02020-06-16 13:08:34 +02001736 if (options & LYD_DUP_WITH_FLAGS) {
1737 dup->flags = node->flags;
1738 } else {
Michal Vasko19175b62022-04-01 09:17:07 +02001739 dup->flags = (node->flags & (LYD_DEFAULT | LYD_EXT)) | LYD_NEW;
Michal Vaskof6df0a02020-06-16 13:08:34 +02001740 }
Michal Vaskoddd76592022-01-17 13:34:48 +01001741 if (trg_ctx == LYD_CTX(node)) {
1742 dup->schema = node->schema;
1743 } else {
Michal Vaskoaf3df492022-12-02 14:03:52 +01001744 ret = lyd_find_schema_ctx(node->schema, trg_ctx, parent, 1, &dup->schema);
Michal Vasko27f536f2022-04-01 09:17:30 +02001745 if (ret) {
1746 /* has no schema but is not an opaque node */
1747 free(dup);
1748 dup = NULL;
1749 goto error;
1750 }
Michal Vaskoddd76592022-01-17 13:34:48 +01001751 }
Michal Vasko52927e22020-03-16 17:26:14 +01001752 dup->prev = dup;
Radek Krejci22ebdba2019-07-25 13:59:43 +02001753
Michal Vasko9cf62422021-07-01 13:29:32 +02001754 /* duplicate metadata/attributes */
Michal Vasko25a32822020-07-09 15:48:22 +02001755 if (!(options & LYD_DUP_NO_META)) {
Michal Vasko9cf62422021-07-01 13:29:32 +02001756 if (!node->schema) {
1757 LY_LIST_FOR(((struct lyd_node_opaq *)node)->attr, attr) {
1758 LY_CHECK_GOTO(ret = lyd_dup_attr_single(attr, dup, NULL), error);
1759 }
1760 } else {
1761 LY_LIST_FOR(node->meta, meta) {
1762 LY_CHECK_GOTO(ret = lyd_dup_meta_single(meta, dup, NULL), error);
1763 }
Michal Vasko25a32822020-07-09 15:48:22 +02001764 }
1765 }
Radek Krejci22ebdba2019-07-25 13:59:43 +02001766
1767 /* nodetype-specific work */
Michal Vasko52927e22020-03-16 17:26:14 +01001768 if (!dup->schema) {
1769 struct lyd_node_opaq *opaq = (struct lyd_node_opaq *)dup;
1770 struct lyd_node_opaq *orig = (struct lyd_node_opaq *)node;
1771 struct lyd_node *child;
Radek Krejci22ebdba2019-07-25 13:59:43 +02001772
1773 if (options & LYD_DUP_RECURSIVE) {
1774 /* duplicate all the children */
1775 LY_LIST_FOR(orig->child, child) {
Michal Vaskoddd76592022-01-17 13:34:48 +01001776 LY_CHECK_GOTO(ret = lyd_dup_r(child, trg_ctx, dup, 1, NULL, options, NULL), error);
Michal Vasko52927e22020-03-16 17:26:14 +01001777 }
1778 }
Michal Vaskoddd76592022-01-17 13:34:48 +01001779 LY_CHECK_GOTO(ret = lydict_insert(trg_ctx, orig->name.name, 0, &opaq->name.name), error);
1780 LY_CHECK_GOTO(ret = lydict_insert(trg_ctx, orig->name.prefix, 0, &opaq->name.prefix), error);
1781 LY_CHECK_GOTO(ret = lydict_insert(trg_ctx, orig->name.module_ns, 0, &opaq->name.module_ns), error);
1782 LY_CHECK_GOTO(ret = lydict_insert(trg_ctx, orig->value, 0, &opaq->value), error);
Michal Vasko9cf62422021-07-01 13:29:32 +02001783 opaq->hints = orig->hints;
1784 opaq->format = orig->format;
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01001785 if (orig->val_prefix_data) {
Michal Vaskoddd76592022-01-17 13:34:48 +01001786 ret = ly_dup_prefix_data(trg_ctx, opaq->format, orig->val_prefix_data, &opaq->val_prefix_data);
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01001787 LY_CHECK_GOTO(ret, error);
Michal Vasko52927e22020-03-16 17:26:14 +01001788 }
Michal Vasko52927e22020-03-16 17:26:14 +01001789 } else if (dup->schema->nodetype & LYD_NODE_TERM) {
1790 struct lyd_node_term *term = (struct lyd_node_term *)dup;
1791 struct lyd_node_term *orig = (struct lyd_node_term *)node;
1792
1793 term->hash = orig->hash;
Michal Vaskoddd76592022-01-17 13:34:48 +01001794 if (trg_ctx == LYD_CTX(node)) {
1795 ret = orig->value.realtype->plugin->duplicate(trg_ctx, &orig->value, &term->value);
1796 LY_CHECK_ERR_GOTO(ret, LOGERR(trg_ctx, ret, "Value duplication failed."), error);
1797 } else {
1798 /* store canonical value in the target context */
1799 val_can = lyd_get_value(node);
1800 type = ((struct lysc_node_leaf *)term->schema)->type;
Michal Vasko989cdb42023-10-06 15:32:37 +02001801 ret = lyd_value_store(trg_ctx, &term->value, type, val_can, strlen(val_can), 1, NULL, LY_VALUE_CANON, NULL,
Michal Vaskoddd76592022-01-17 13:34:48 +01001802 LYD_HINT_DATA, term->schema, NULL);
1803 LY_CHECK_GOTO(ret, error);
1804 }
Michal Vasko52927e22020-03-16 17:26:14 +01001805 } else if (dup->schema->nodetype & LYD_NODE_INNER) {
1806 struct lyd_node_inner *orig = (struct lyd_node_inner *)node;
1807 struct lyd_node *child;
1808
1809 if (options & LYD_DUP_RECURSIVE) {
1810 /* duplicate all the children */
1811 LY_LIST_FOR(orig->child, child) {
Michal Vaskoddd76592022-01-17 13:34:48 +01001812 LY_CHECK_GOTO(ret = lyd_dup_r(child, trg_ctx, dup, 1, NULL, options, NULL), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02001813 }
Michal Vasko69730152020-10-09 16:30:07 +02001814 } else if ((dup->schema->nodetype == LYS_LIST) && !(dup->schema->flags & LYS_KEYLESS)) {
Radek Krejci22ebdba2019-07-25 13:59:43 +02001815 /* always duplicate keys of a list */
Michal Vasko9beceb82022-04-05 12:14:15 +02001816 for (child = orig->child; child && lysc_is_key(child->schema); child = child->next) {
Michal Vaskoddd76592022-01-17 13:34:48 +01001817 LY_CHECK_GOTO(ret = lyd_dup_r(child, trg_ctx, dup, 1, NULL, options, NULL), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02001818 }
1819 }
1820 lyd_hash(dup);
1821 } else if (dup->schema->nodetype & LYD_NODE_ANY) {
Michal Vasko61551fa2020-07-09 15:45:45 +02001822 dup->hash = node->hash;
1823 any = (struct lyd_node_any *)node;
1824 LY_CHECK_GOTO(ret = lyd_any_copy_value(dup, &any->value, any->value_type), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02001825 }
1826
Michal Vasko52927e22020-03-16 17:26:14 +01001827 /* insert */
Michal Vasko67177e52021-08-25 11:15:15 +02001828 lyd_insert_node(parent, first, dup, insert_last);
Michal Vasko52927e22020-03-16 17:26:14 +01001829
1830 if (dup_p) {
1831 *dup_p = dup;
1832 }
1833 return LY_SUCCESS;
Radek Krejci22ebdba2019-07-25 13:59:43 +02001834
1835error:
Michal Vasko52927e22020-03-16 17:26:14 +01001836 lyd_free_tree(dup);
1837 return ret;
Radek Krejci22ebdba2019-07-25 13:59:43 +02001838}
1839
Michal Vasko29d674b2021-08-25 11:18:35 +02001840/**
1841 * @brief Get a parent node to connect duplicated subtree to.
1842 *
1843 * @param[in] node Node (subtree) to duplicate.
Michal Vaskoddd76592022-01-17 13:34:48 +01001844 * @param[in] trg_ctx Target context for duplicated nodes.
Michal Vasko29d674b2021-08-25 11:18:35 +02001845 * @param[in] parent Initial parent to connect to.
1846 * @param[in] options Bitmask of options flags, see @ref dupoptions.
1847 * @param[out] dup_parent First duplicated parent node, if any.
1848 * @param[out] local_parent Correct parent to directly connect duplicated @p node to.
1849 * @return LY_ERR value.
1850 */
Michal Vasko3a41dff2020-07-15 14:30:28 +02001851static LY_ERR
Michal Vasko58d89e92023-05-23 09:56:19 +02001852lyd_dup_get_local_parent(const struct lyd_node *node, const struct ly_ctx *trg_ctx, struct lyd_node *parent,
1853 uint32_t options, struct lyd_node **dup_parent, struct lyd_node **local_parent)
Radek Krejci22ebdba2019-07-25 13:59:43 +02001854{
Michal Vasko58d89e92023-05-23 09:56:19 +02001855 const struct lyd_node *orig_parent;
1856 struct lyd_node *iter;
Michal Vasko83522192022-07-20 08:07:34 +02001857 ly_bool repeat = 1, ext_parent = 0;
Michal Vasko3a41dff2020-07-15 14:30:28 +02001858
1859 *dup_parent = NULL;
1860 *local_parent = NULL;
1861
Michal Vasko83522192022-07-20 08:07:34 +02001862 if (node->flags & LYD_EXT) {
1863 ext_parent = 1;
1864 }
Michal Vasko58d89e92023-05-23 09:56:19 +02001865 for (orig_parent = lyd_parent(node); repeat && orig_parent; orig_parent = lyd_parent(orig_parent)) {
Michal Vasko83522192022-07-20 08:07:34 +02001866 if (ext_parent) {
1867 /* use the standard context */
1868 trg_ctx = LYD_CTX(orig_parent);
1869 }
Michal Vasko13c5b212023-02-14 10:03:01 +01001870 if (parent && (LYD_CTX(parent) == LYD_CTX(orig_parent)) && (parent->schema == orig_parent->schema)) {
Michal Vasko3a41dff2020-07-15 14:30:28 +02001871 /* stop creating parents, connect what we have into the provided parent */
1872 iter = parent;
1873 repeat = 0;
Michal Vasko13c5b212023-02-14 10:03:01 +01001874 } else if (parent && (LYD_CTX(parent) != LYD_CTX(orig_parent)) &&
1875 lyd_compare_schema_equal(parent->schema, orig_parent->schema) &&
Michal Vasko58d89e92023-05-23 09:56:19 +02001876 lyd_compare_schema_parents_equal(parent, orig_parent)) {
Michal Vasko13c5b212023-02-14 10:03:01 +01001877 iter = parent;
1878 repeat = 0;
Michal Vasko3a41dff2020-07-15 14:30:28 +02001879 } else {
1880 iter = NULL;
Michal Vasko58d89e92023-05-23 09:56:19 +02001881 LY_CHECK_RET(lyd_dup_r(orig_parent, trg_ctx, NULL, 0, &iter, options, &iter));
Michal Vasko3a41dff2020-07-15 14:30:28 +02001882 }
Michal Vasko58d89e92023-05-23 09:56:19 +02001883
Michal Vasko3a41dff2020-07-15 14:30:28 +02001884 if (!*local_parent) {
Michal Vasko58d89e92023-05-23 09:56:19 +02001885 /* update local parent (created parent) */
1886 *local_parent = iter;
Michal Vasko3a41dff2020-07-15 14:30:28 +02001887 }
Michal Vasko58d89e92023-05-23 09:56:19 +02001888
Michal Vasko3a41dff2020-07-15 14:30:28 +02001889 if (*dup_parent) {
Michal Vasko58d89e92023-05-23 09:56:19 +02001890 lyd_insert_node(iter, NULL, *dup_parent, 0);
Michal Vasko3a41dff2020-07-15 14:30:28 +02001891 }
Michal Vasko58d89e92023-05-23 09:56:19 +02001892 *dup_parent = iter;
Michal Vasko83522192022-07-20 08:07:34 +02001893 if (orig_parent->flags & LYD_EXT) {
1894 ext_parent = 1;
1895 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02001896 }
1897
1898 if (repeat && parent) {
1899 /* given parent and created parents chain actually do not interconnect */
Michal Vasko58d89e92023-05-23 09:56:19 +02001900 LOGERR(trg_ctx, LY_EINVAL, "None of the duplicated node \"%s\" schema parents match the provided parent \"%s\".",
1901 LYD_NAME(node), LYD_NAME(parent));
Michal Vasko3a41dff2020-07-15 14:30:28 +02001902 return LY_EINVAL;
1903 }
1904
1905 return LY_SUCCESS;
1906}
1907
1908static LY_ERR
Michal Vasko58d89e92023-05-23 09:56:19 +02001909lyd_dup(const struct lyd_node *node, const struct ly_ctx *trg_ctx, struct lyd_node *parent, uint32_t options,
Michal Vaskoddd76592022-01-17 13:34:48 +01001910 ly_bool nosiblings, struct lyd_node **dup)
Michal Vasko3a41dff2020-07-15 14:30:28 +02001911{
1912 LY_ERR rc;
Radek Krejci22ebdba2019-07-25 13:59:43 +02001913 const struct lyd_node *orig; /* original node to be duplicated */
1914 struct lyd_node *first = NULL; /* the first duplicated node, this is returned */
Radek Krejci22ebdba2019-07-25 13:59:43 +02001915 struct lyd_node *top = NULL; /* the most higher created node */
Michal Vasko58d89e92023-05-23 09:56:19 +02001916 struct lyd_node *local_parent = NULL; /* the direct parent node for the duplicated node(s) */
Radek Krejci22ebdba2019-07-25 13:59:43 +02001917
Michal Vasko1feb9bb2022-07-20 08:44:07 +02001918 assert(node && trg_ctx);
Radek Krejci22ebdba2019-07-25 13:59:43 +02001919
1920 if (options & LYD_DUP_WITH_PARENTS) {
Michal Vaskoddd76592022-01-17 13:34:48 +01001921 LY_CHECK_GOTO(rc = lyd_dup_get_local_parent(node, trg_ctx, parent, options & (LYD_DUP_WITH_FLAGS | LYD_DUP_NO_META),
Michal Vasko20909752021-05-18 16:13:38 +02001922 &top, &local_parent), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02001923 } else {
1924 local_parent = parent;
1925 }
1926
Radek Krejci22ebdba2019-07-25 13:59:43 +02001927 LY_LIST_FOR(node, orig) {
Michal Vasko35f4d772021-01-12 12:08:57 +01001928 if (lysc_is_key(orig->schema)) {
1929 if (local_parent) {
1930 /* the key must already exist in the parent */
Michal Vasko58d89e92023-05-23 09:56:19 +02001931 rc = lyd_find_sibling_schema(lyd_child(local_parent), orig->schema, first ? NULL : &first);
Michal Vaskoddd76592022-01-17 13:34:48 +01001932 LY_CHECK_ERR_GOTO(rc, LOGINT(trg_ctx), error);
Michal Vasko35f4d772021-01-12 12:08:57 +01001933 } else {
1934 assert(!(options & LYD_DUP_WITH_PARENTS));
1935 /* duplicating a single key, okay, I suppose... */
Michal Vaskoddd76592022-01-17 13:34:48 +01001936 rc = lyd_dup_r(orig, trg_ctx, NULL, 0, &first, options, first ? NULL : &first);
Michal Vasko35f4d772021-01-12 12:08:57 +01001937 LY_CHECK_GOTO(rc, error);
1938 }
1939 } else {
1940 /* if there is no local parent, it will be inserted into first */
Michal Vasko58d89e92023-05-23 09:56:19 +02001941 rc = lyd_dup_r(orig, trg_ctx, local_parent, 0, &first, options, first ? NULL : &first);
Michal Vasko35f4d772021-01-12 12:08:57 +01001942 LY_CHECK_GOTO(rc, error);
1943 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02001944 if (nosiblings) {
Radek Krejci22ebdba2019-07-25 13:59:43 +02001945 break;
1946 }
1947 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02001948
Michal Vasko3a41dff2020-07-15 14:30:28 +02001949 if (dup) {
1950 *dup = first;
1951 }
1952 return LY_SUCCESS;
Radek Krejci22ebdba2019-07-25 13:59:43 +02001953
1954error:
1955 if (top) {
1956 lyd_free_tree(top);
1957 } else {
Michal Vaskof03ed032020-03-04 13:31:44 +01001958 lyd_free_siblings(first);
Radek Krejci22ebdba2019-07-25 13:59:43 +02001959 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02001960 return rc;
Radek Krejci22ebdba2019-07-25 13:59:43 +02001961}
Michal Vasko5ec7cda2019-09-11 13:43:08 +02001962
Michal Vasko1feb9bb2022-07-20 08:44:07 +02001963/**
1964 * @brief Check the context of node and parent when duplicating nodes.
1965 *
1966 * @param[in] node Node to duplicate.
1967 * @param[in] parent Parent of the duplicated node(s).
1968 * @return LY_ERR value.
1969 */
1970static LY_ERR
1971lyd_dup_ctx_check(const struct lyd_node *node, const struct lyd_node_inner *parent)
1972{
1973 const struct lyd_node *iter;
1974
1975 if (!node || !parent) {
1976 return LY_SUCCESS;
1977 }
1978
1979 if ((LYD_CTX(node) != LYD_CTX(parent))) {
1980 /* try to find top-level ext data parent */
1981 for (iter = node; iter && !(iter->flags & LYD_EXT); iter = lyd_parent(iter)) {}
1982
1983 if (!iter || !lyd_parent(iter) || (LYD_CTX(lyd_parent(iter)) != LYD_CTX(parent))) {
Michal Vaskoce55b462023-02-14 10:03:32 +01001984 LOGERR(LYD_CTX(node), LY_EINVAL, "Different contexts used in node duplication.");
Michal Vasko1feb9bb2022-07-20 08:44:07 +02001985 return LY_EINVAL;
1986 }
1987 }
1988
1989 return LY_SUCCESS;
1990}
1991
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001992LIBYANG_API_DEF LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02001993lyd_dup_single(const struct lyd_node *node, struct lyd_node_inner *parent, uint32_t options, struct lyd_node **dup)
Michal Vasko3a41dff2020-07-15 14:30:28 +02001994{
Michal Vaskoddd76592022-01-17 13:34:48 +01001995 LY_CHECK_ARG_RET(NULL, node, LY_EINVAL);
Michal Vasko1feb9bb2022-07-20 08:44:07 +02001996 LY_CHECK_RET(lyd_dup_ctx_check(node, parent));
Michal Vaskoddd76592022-01-17 13:34:48 +01001997
Michal Vasko58d89e92023-05-23 09:56:19 +02001998 return lyd_dup(node, LYD_CTX(node), (struct lyd_node *)parent, options, 1, dup);
Michal Vaskoddd76592022-01-17 13:34:48 +01001999}
2000
2001LIBYANG_API_DEF LY_ERR
2002lyd_dup_single_to_ctx(const struct lyd_node *node, const struct ly_ctx *trg_ctx, struct lyd_node_inner *parent,
2003 uint32_t options, struct lyd_node **dup)
2004{
2005 LY_CHECK_ARG_RET(trg_ctx, node, trg_ctx, LY_EINVAL);
2006
Michal Vasko58d89e92023-05-23 09:56:19 +02002007 return lyd_dup(node, trg_ctx, (struct lyd_node *)parent, options, 1, dup);
Michal Vasko3a41dff2020-07-15 14:30:28 +02002008}
2009
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002010LIBYANG_API_DEF LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02002011lyd_dup_siblings(const struct lyd_node *node, struct lyd_node_inner *parent, uint32_t options, struct lyd_node **dup)
Michal Vasko3a41dff2020-07-15 14:30:28 +02002012{
Michal Vaskoddd76592022-01-17 13:34:48 +01002013 LY_CHECK_ARG_RET(NULL, node, LY_EINVAL);
Michal Vasko1feb9bb2022-07-20 08:44:07 +02002014 LY_CHECK_RET(lyd_dup_ctx_check(node, parent));
Michal Vaskoddd76592022-01-17 13:34:48 +01002015
Michal Vasko58d89e92023-05-23 09:56:19 +02002016 return lyd_dup(node, LYD_CTX(node), (struct lyd_node *)parent, options, 0, dup);
Michal Vaskoddd76592022-01-17 13:34:48 +01002017}
2018
2019LIBYANG_API_DEF LY_ERR
2020lyd_dup_siblings_to_ctx(const struct lyd_node *node, const struct ly_ctx *trg_ctx, struct lyd_node_inner *parent,
2021 uint32_t options, struct lyd_node **dup)
2022{
2023 LY_CHECK_ARG_RET(trg_ctx, node, trg_ctx, LY_EINVAL);
2024
Michal Vasko58d89e92023-05-23 09:56:19 +02002025 return lyd_dup(node, trg_ctx, (struct lyd_node *)parent, options, 0, dup);
Michal Vasko3a41dff2020-07-15 14:30:28 +02002026}
2027
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002028LIBYANG_API_DEF LY_ERR
Michal Vasko3a41dff2020-07-15 14:30:28 +02002029lyd_dup_meta_single(const struct lyd_meta *meta, struct lyd_node *node, struct lyd_meta **dup)
Michal Vasko25a32822020-07-09 15:48:22 +02002030{
Radek Krejci011e4aa2020-09-04 15:22:31 +02002031 LY_ERR ret = LY_SUCCESS;
Michal Vasko33c48972022-07-20 10:28:07 +02002032 const struct ly_ctx *ctx;
Michal Vasko25a32822020-07-09 15:48:22 +02002033 struct lyd_meta *mt, *last;
2034
Michal Vasko3a41dff2020-07-15 14:30:28 +02002035 LY_CHECK_ARG_RET(NULL, meta, node, LY_EINVAL);
Michal Vasko25a32822020-07-09 15:48:22 +02002036
Michal Vasko33c48972022-07-20 10:28:07 +02002037 /* log to node context but value must always use the annotation context */
2038 ctx = meta->annotation->module->ctx;
2039
Michal Vasko25a32822020-07-09 15:48:22 +02002040 /* create a copy */
2041 mt = calloc(1, sizeof *mt);
Michal Vaskob7be7a82020-08-20 09:09:04 +02002042 LY_CHECK_ERR_RET(!mt, LOGMEM(LYD_CTX(node)), LY_EMEM);
Michal Vasko25a32822020-07-09 15:48:22 +02002043 mt->annotation = meta->annotation;
Michal Vasko33c48972022-07-20 10:28:07 +02002044 ret = meta->value.realtype->plugin->duplicate(ctx, &meta->value, &mt->value);
Radek Krejci011e4aa2020-09-04 15:22:31 +02002045 LY_CHECK_ERR_GOTO(ret, LOGERR(LYD_CTX(node), LY_EINT, "Value duplication failed."), finish);
Michal Vasko33c48972022-07-20 10:28:07 +02002046 LY_CHECK_GOTO(ret = lydict_insert(ctx, meta->name, 0, &mt->name), finish);
Michal Vasko25a32822020-07-09 15:48:22 +02002047
2048 /* insert as the last attribute */
Radek Krejci011e4aa2020-09-04 15:22:31 +02002049 mt->parent = node;
Michal Vasko25a32822020-07-09 15:48:22 +02002050 if (node->meta) {
Radek Krejci1e008d22020-08-17 11:37:37 +02002051 for (last = node->meta; last->next; last = last->next) {}
Michal Vasko25a32822020-07-09 15:48:22 +02002052 last->next = mt;
2053 } else {
2054 node->meta = mt;
2055 }
2056
Radek Krejci011e4aa2020-09-04 15:22:31 +02002057finish:
2058 if (ret) {
2059 lyd_free_meta_single(mt);
2060 } else if (dup) {
Michal Vasko3a41dff2020-07-15 14:30:28 +02002061 *dup = mt;
2062 }
2063 return LY_SUCCESS;
Michal Vasko25a32822020-07-09 15:48:22 +02002064}
2065
Michal Vasko4490d312020-06-16 13:08:55 +02002066/**
2067 * @brief Merge a source sibling into target siblings.
2068 *
2069 * @param[in,out] first_trg First target sibling, is updated if top-level.
2070 * @param[in] parent_trg Target parent.
2071 * @param[in,out] sibling_src Source sibling to merge, set to NULL if spent.
Michal Vaskocd3f6172021-05-18 16:14:50 +02002072 * @param[in] merge_cb Optional merge callback.
2073 * @param[in] cb_data Arbitrary callback data.
Michal Vasko4490d312020-06-16 13:08:55 +02002074 * @param[in] options Merge options.
Michal Vaskocd3f6172021-05-18 16:14:50 +02002075 * @param[in,out] dup_inst Duplicate instance cache for all @p first_trg siblings.
Michal Vasko4490d312020-06-16 13:08:55 +02002076 * @return LY_ERR value.
2077 */
2078static LY_ERR
2079lyd_merge_sibling_r(struct lyd_node **first_trg, struct lyd_node *parent_trg, const struct lyd_node **sibling_src_p,
Michal Vasko8efac242023-03-30 08:24:56 +02002080 lyd_merge_cb merge_cb, void *cb_data, uint16_t options, struct ly_ht **dup_inst)
Michal Vasko4490d312020-06-16 13:08:55 +02002081{
Michal Vasko4490d312020-06-16 13:08:55 +02002082 const struct lyd_node *child_src, *tmp, *sibling_src;
Michal Vasko56daf732020-08-10 10:57:18 +02002083 struct lyd_node *match_trg, *dup_src, *elem;
Michal Vaskod1afa502022-01-27 16:18:12 +01002084 struct lyd_node_opaq *opaq_trg, *opaq_src;
Michal Vasko4490d312020-06-16 13:08:55 +02002085 struct lysc_type *type;
Michal Vasko8efac242023-03-30 08:24:56 +02002086 struct ly_ht *child_dup_inst = NULL;
Michal Vaskocd3f6172021-05-18 16:14:50 +02002087 LY_ERR ret;
2088 ly_bool first_inst = 0;
Michal Vasko4490d312020-06-16 13:08:55 +02002089
2090 sibling_src = *sibling_src_p;
Michal Vaskocd3f6172021-05-18 16:14:50 +02002091 if (!sibling_src->schema) {
2092 /* try to find the same opaque node */
2093 lyd_find_sibling_opaq_next(*first_trg, LYD_NAME(sibling_src), &match_trg);
2094 } else if (sibling_src->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) {
Michal Vasko4490d312020-06-16 13:08:55 +02002095 /* try to find the exact instance */
Michal Vaskocd3f6172021-05-18 16:14:50 +02002096 lyd_find_sibling_first(*first_trg, sibling_src, &match_trg);
Michal Vasko4490d312020-06-16 13:08:55 +02002097 } else {
2098 /* try to simply find the node, there cannot be more instances */
Michal Vaskocd3f6172021-05-18 16:14:50 +02002099 lyd_find_sibling_val(*first_trg, sibling_src->schema, NULL, 0, &match_trg);
Michal Vasko4490d312020-06-16 13:08:55 +02002100 }
2101
Michal Vaskocd3f6172021-05-18 16:14:50 +02002102 if (match_trg) {
2103 /* update match as needed */
2104 LY_CHECK_RET(lyd_dup_inst_next(&match_trg, *first_trg, dup_inst));
2105 } else {
2106 /* first instance of this node */
2107 first_inst = 1;
2108 }
2109
2110 if (match_trg) {
2111 /* call callback */
2112 if (merge_cb) {
2113 LY_CHECK_RET(merge_cb(match_trg, sibling_src, cb_data));
2114 }
2115
Michal Vasko4490d312020-06-16 13:08:55 +02002116 /* node found, make sure even value matches for all node types */
Michal Vaskod1afa502022-01-27 16:18:12 +01002117 if (!match_trg->schema) {
2118 if (lyd_compare_single(sibling_src, match_trg, 0)) {
2119 /* update value */
2120 opaq_trg = (struct lyd_node_opaq *)match_trg;
2121 opaq_src = (struct lyd_node_opaq *)sibling_src;
2122
2123 lydict_remove(LYD_CTX(opaq_trg), opaq_trg->value);
2124 lydict_insert(LYD_CTX(opaq_trg), opaq_src->value, 0, &opaq_trg->value);
2125 opaq_trg->hints = opaq_src->hints;
2126
2127 ly_free_prefix_data(opaq_trg->format, opaq_trg->val_prefix_data);
2128 opaq_trg->format = opaq_src->format;
2129 ly_dup_prefix_data(LYD_CTX(opaq_trg), opaq_src->format, opaq_src->val_prefix_data,
2130 &opaq_trg->val_prefix_data);
2131 }
2132 } else if ((match_trg->schema->nodetype == LYS_LEAF) &&
2133 lyd_compare_single(sibling_src, match_trg, LYD_COMPARE_DEFAULTS)) {
Michal Vasko4490d312020-06-16 13:08:55 +02002134 /* since they are different, they cannot both be default */
2135 assert(!(sibling_src->flags & LYD_DEFAULT) || !(match_trg->flags & LYD_DEFAULT));
2136
Michal Vasko3a41dff2020-07-15 14:30:28 +02002137 /* update value (or only LYD_DEFAULT flag) only if flag set or the source node is not default */
2138 if ((options & LYD_MERGE_DEFAULTS) || !(sibling_src->flags & LYD_DEFAULT)) {
Michal Vasko4490d312020-06-16 13:08:55 +02002139 type = ((struct lysc_node_leaf *)match_trg->schema)->type;
Michal Vaskob7be7a82020-08-20 09:09:04 +02002140 type->plugin->free(LYD_CTX(match_trg), &((struct lyd_node_term *)match_trg)->value);
2141 LY_CHECK_RET(type->plugin->duplicate(LYD_CTX(match_trg), &((struct lyd_node_term *)sibling_src)->value,
Michal Vasko69730152020-10-09 16:30:07 +02002142 &((struct lyd_node_term *)match_trg)->value));
Michal Vasko4490d312020-06-16 13:08:55 +02002143
2144 /* copy flags and add LYD_NEW */
Michal Vasko7e8315b2021-08-03 17:01:06 +02002145 match_trg->flags = sibling_src->flags | ((options & LYD_MERGE_WITH_FLAGS) ? 0 : LYD_NEW);
Michal Vasko4490d312020-06-16 13:08:55 +02002146 }
Michal Vasko8f359bf2020-07-28 10:41:15 +02002147 } else if ((match_trg->schema->nodetype & LYS_ANYDATA) && lyd_compare_single(sibling_src, match_trg, 0)) {
Michal Vasko4c583e82020-07-17 12:16:14 +02002148 /* update value */
2149 LY_CHECK_RET(lyd_any_copy_value(match_trg, &((struct lyd_node_any *)sibling_src)->value,
Radek Krejci0f969882020-08-21 16:56:47 +02002150 ((struct lyd_node_any *)sibling_src)->value_type));
Michal Vasko4490d312020-06-16 13:08:55 +02002151
2152 /* copy flags and add LYD_NEW */
Michal Vasko7e8315b2021-08-03 17:01:06 +02002153 match_trg->flags = sibling_src->flags | ((options & LYD_MERGE_WITH_FLAGS) ? 0 : LYD_NEW);
Michal Vaskocd3f6172021-05-18 16:14:50 +02002154 }
2155
2156 /* check descendants, recursively */
2157 ret = LY_SUCCESS;
2158 LY_LIST_FOR_SAFE(lyd_child_no_keys(sibling_src), tmp, child_src) {
2159 ret = lyd_merge_sibling_r(lyd_node_child_p(match_trg), match_trg, &child_src, merge_cb, cb_data, options,
2160 &child_dup_inst);
2161 if (ret) {
2162 break;
Michal Vasko4490d312020-06-16 13:08:55 +02002163 }
2164 }
Michal Vaskocd3f6172021-05-18 16:14:50 +02002165 lyd_dup_inst_free(child_dup_inst);
2166 LY_CHECK_RET(ret);
Michal Vasko4490d312020-06-16 13:08:55 +02002167 } else {
2168 /* node not found, merge it */
2169 if (options & LYD_MERGE_DESTRUCT) {
2170 dup_src = (struct lyd_node *)sibling_src;
2171 lyd_unlink_tree(dup_src);
2172 /* spend it */
2173 *sibling_src_p = NULL;
2174 } else {
Michal Vasko3a41dff2020-07-15 14:30:28 +02002175 LY_CHECK_RET(lyd_dup_single(sibling_src, NULL, LYD_DUP_RECURSIVE | LYD_DUP_WITH_FLAGS, &dup_src));
Michal Vasko4490d312020-06-16 13:08:55 +02002176 }
2177
Michal Vasko7e8315b2021-08-03 17:01:06 +02002178 if (!(options & LYD_MERGE_WITH_FLAGS)) {
2179 /* set LYD_NEW for all the new nodes, required for validation */
2180 LYD_TREE_DFS_BEGIN(dup_src, elem) {
2181 elem->flags |= LYD_NEW;
2182 LYD_TREE_DFS_END(dup_src, elem);
2183 }
Michal Vasko4490d312020-06-16 13:08:55 +02002184 }
2185
Michal Vaskocd3f6172021-05-18 16:14:50 +02002186 /* insert */
Michal Vasko6ee6f432021-07-16 09:49:14 +02002187 lyd_insert_node(parent_trg, first_trg, dup_src, 0);
Michal Vaskocd3f6172021-05-18 16:14:50 +02002188
2189 if (first_inst) {
2190 /* remember not to find this instance next time */
2191 LY_CHECK_RET(lyd_dup_inst_next(&dup_src, *first_trg, dup_inst));
2192 }
2193
2194 /* call callback, no source node */
2195 if (merge_cb) {
2196 LY_CHECK_RET(merge_cb(dup_src, NULL, cb_data));
2197 }
Michal Vasko4490d312020-06-16 13:08:55 +02002198 }
2199
2200 return LY_SUCCESS;
2201}
2202
Michal Vasko3a41dff2020-07-15 14:30:28 +02002203static LY_ERR
Michal Vaskocd3f6172021-05-18 16:14:50 +02002204lyd_merge(struct lyd_node **target, const struct lyd_node *source, const struct lys_module *mod,
2205 lyd_merge_cb merge_cb, void *cb_data, uint16_t options, ly_bool nosiblings)
Michal Vasko4490d312020-06-16 13:08:55 +02002206{
2207 const struct lyd_node *sibling_src, *tmp;
Michal Vasko8efac242023-03-30 08:24:56 +02002208 struct ly_ht *dup_inst = NULL;
Radek Krejci857189e2020-09-01 13:26:36 +02002209 ly_bool first;
Michal Vaskocd3f6172021-05-18 16:14:50 +02002210 LY_ERR ret = LY_SUCCESS;
Michal Vasko4490d312020-06-16 13:08:55 +02002211
2212 LY_CHECK_ARG_RET(NULL, target, LY_EINVAL);
Michal Vasko892f5bf2021-11-24 10:41:05 +01002213 LY_CHECK_CTX_EQUAL_RET(*target ? LYD_CTX(*target) : NULL, source ? LYD_CTX(source) : NULL, mod ? mod->ctx : NULL,
2214 LY_EINVAL);
Michal Vasko4490d312020-06-16 13:08:55 +02002215
2216 if (!source) {
2217 /* nothing to merge */
2218 return LY_SUCCESS;
2219 }
2220
Michal Vasko831422b2020-11-24 11:20:51 +01002221 if ((*target && lysc_data_parent((*target)->schema)) || lysc_data_parent(source->schema)) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02002222 LOGERR(LYD_CTX(source), LY_EINVAL, "Invalid arguments - can merge only 2 top-level subtrees (%s()).", __func__);
Michal Vasko4490d312020-06-16 13:08:55 +02002223 return LY_EINVAL;
2224 }
2225
2226 LY_LIST_FOR_SAFE(source, tmp, sibling_src) {
Michal Vaskocd3f6172021-05-18 16:14:50 +02002227 if (mod && (lyd_owner_module(sibling_src) != mod)) {
2228 /* skip data nodes from different modules */
2229 continue;
2230 }
2231
Radek Krejci857189e2020-09-01 13:26:36 +02002232 first = (sibling_src == source) ? 1 : 0;
Michal Vaskocd3f6172021-05-18 16:14:50 +02002233 ret = lyd_merge_sibling_r(target, NULL, &sibling_src, merge_cb, cb_data, options, &dup_inst);
2234 if (ret) {
2235 break;
2236 }
Michal Vasko4490d312020-06-16 13:08:55 +02002237 if (first && !sibling_src) {
2238 /* source was spent (unlinked), move to the next node */
2239 source = tmp;
2240 }
2241
Michal Vasko3a41dff2020-07-15 14:30:28 +02002242 if (nosiblings) {
Michal Vasko4490d312020-06-16 13:08:55 +02002243 break;
2244 }
2245 }
2246
2247 if (options & LYD_MERGE_DESTRUCT) {
2248 /* free any leftover source data that were not merged */
2249 lyd_free_siblings((struct lyd_node *)source);
2250 }
2251
Michal Vaskocd3f6172021-05-18 16:14:50 +02002252 lyd_dup_inst_free(dup_inst);
2253 return ret;
Michal Vasko4490d312020-06-16 13:08:55 +02002254}
2255
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002256LIBYANG_API_DEF LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02002257lyd_merge_tree(struct lyd_node **target, const struct lyd_node *source, uint16_t options)
Michal Vasko3a41dff2020-07-15 14:30:28 +02002258{
Michal Vaskocd3f6172021-05-18 16:14:50 +02002259 return lyd_merge(target, source, NULL, NULL, NULL, options, 1);
Michal Vasko3a41dff2020-07-15 14:30:28 +02002260}
2261
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002262LIBYANG_API_DEF LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02002263lyd_merge_siblings(struct lyd_node **target, const struct lyd_node *source, uint16_t options)
Michal Vasko3a41dff2020-07-15 14:30:28 +02002264{
Michal Vaskocd3f6172021-05-18 16:14:50 +02002265 return lyd_merge(target, source, NULL, NULL, NULL, options, 0);
2266}
2267
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002268LIBYANG_API_DEF LY_ERR
Michal Vaskocd3f6172021-05-18 16:14:50 +02002269lyd_merge_module(struct lyd_node **target, const struct lyd_node *source, const struct lys_module *mod,
2270 lyd_merge_cb merge_cb, void *cb_data, uint16_t options)
2271{
2272 return lyd_merge(target, source, mod, merge_cb, cb_data, options, 0);
Michal Vasko3a41dff2020-07-15 14:30:28 +02002273}
2274
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002275static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02002276lyd_path_str_enlarge(char **buffer, size_t *buflen, size_t reqlen, ly_bool is_static)
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002277{
Michal Vasko14654712020-02-06 08:35:21 +01002278 /* ending \0 */
2279 ++reqlen;
2280
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002281 if (reqlen > *buflen) {
2282 if (is_static) {
2283 return LY_EINCOMPLETE;
2284 }
2285
2286 *buffer = ly_realloc(*buffer, reqlen * sizeof **buffer);
2287 if (!*buffer) {
2288 return LY_EMEM;
2289 }
2290
2291 *buflen = reqlen;
2292 }
2293
2294 return LY_SUCCESS;
2295}
2296
Michal Vaskod59035b2020-07-08 12:00:06 +02002297LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02002298lyd_path_list_predicate(const struct lyd_node *node, char **buffer, size_t *buflen, size_t *bufused, ly_bool is_static)
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002299{
2300 const struct lyd_node *key;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002301 size_t len;
2302 const char *val;
2303 char quot;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002304
Michal Vasko824d0832021-11-04 15:36:51 +01002305 for (key = lyd_child(node); key && key->schema && (key->schema->flags & LYS_KEY); key = key->next) {
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02002306 val = lyd_get_value(key);
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002307 len = 1 + strlen(key->schema->name) + 2 + strlen(val) + 2;
Michal Vaskoba99a3e2020-08-18 15:50:05 +02002308 LY_CHECK_RET(lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static));
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002309
2310 quot = '\'';
2311 if (strchr(val, '\'')) {
2312 quot = '"';
2313 }
2314 *bufused += sprintf(*buffer + *bufused, "[%s=%c%s%c]", key->schema->name, quot, val, quot);
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002315 }
2316
2317 return LY_SUCCESS;
2318}
2319
2320/**
2321 * @brief Append leaf-list value predicate to path.
2322 *
2323 * @param[in] node Node to print.
2324 * @param[in,out] buffer Buffer to print to.
2325 * @param[in,out] buflen Current buffer length.
2326 * @param[in,out] bufused Current number of characters used in @p buffer.
2327 * @param[in] is_static Whether buffer is static or can be reallocated.
2328 * @return LY_ERR
2329 */
2330static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02002331lyd_path_leaflist_predicate(const struct lyd_node *node, char **buffer, size_t *buflen, size_t *bufused, ly_bool is_static)
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002332{
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002333 size_t len;
2334 const char *val;
2335 char quot;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002336
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02002337 val = lyd_get_value(node);
Radek Krejcif13b87b2020-12-01 22:02:17 +01002338 len = 4 + strlen(val) + 2; /* "[.='" + val + "']" */
Michal Vaskoba99a3e2020-08-18 15:50:05 +02002339 LY_CHECK_RET(lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static));
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002340
2341 quot = '\'';
2342 if (strchr(val, '\'')) {
2343 quot = '"';
2344 }
2345 *bufused += sprintf(*buffer + *bufused, "[.=%c%s%c]", quot, val, quot);
2346
Michal Vaskoba99a3e2020-08-18 15:50:05 +02002347 return LY_SUCCESS;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002348}
2349
2350/**
2351 * @brief Append node position (relative to its other instances) predicate to path.
2352 *
2353 * @param[in] node Node to print.
2354 * @param[in,out] buffer Buffer to print to.
2355 * @param[in,out] buflen Current buffer length.
2356 * @param[in,out] bufused Current number of characters used in @p buffer.
2357 * @param[in] is_static Whether buffer is static or can be reallocated.
2358 * @return LY_ERR
2359 */
2360static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02002361lyd_path_position_predicate(const struct lyd_node *node, char **buffer, size_t *buflen, size_t *bufused, ly_bool is_static)
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002362{
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002363 size_t len;
Michal Vasko50cc0562021-05-18 16:15:43 +02002364 uint32_t pos;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002365 char *val = NULL;
2366 LY_ERR rc;
2367
Michal Vasko50cc0562021-05-18 16:15:43 +02002368 pos = lyd_list_pos(node);
2369 if (asprintf(&val, "%" PRIu32, pos) == -1) {
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002370 return LY_EMEM;
2371 }
2372
2373 len = 1 + strlen(val) + 1;
2374 rc = lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static);
2375 if (rc != LY_SUCCESS) {
2376 goto cleanup;
2377 }
2378
2379 *bufused += sprintf(*buffer + *bufused, "[%s]", val);
2380
2381cleanup:
2382 free(val);
2383 return rc;
2384}
2385
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002386LIBYANG_API_DEF char *
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002387lyd_path(const struct lyd_node *node, LYD_PATH_TYPE pathtype, char *buffer, size_t buflen)
2388{
Radek Krejci857189e2020-09-01 13:26:36 +02002389 ly_bool is_static = 0;
Radek Krejci1deb5be2020-08-26 16:43:36 +02002390 uint32_t i, depth;
Michal Vasko14654712020-02-06 08:35:21 +01002391 size_t bufused = 0, len;
Michal Vasko12eb6222022-03-18 13:35:49 +01002392 const struct lyd_node *iter, *parent;
2393 const struct lys_module *mod, *prev_mod;
Michal Vasko790b2bc2020-08-03 13:35:06 +02002394 LY_ERR rc = LY_SUCCESS;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002395
2396 LY_CHECK_ARG_RET(NULL, node, NULL);
2397 if (buffer) {
Michal Vasko16385f42021-05-18 16:16:09 +02002398 LY_CHECK_ARG_RET(LYD_CTX(node), buflen > 1, NULL);
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002399 is_static = 1;
Michal Vasko14654712020-02-06 08:35:21 +01002400 } else {
2401 buflen = 0;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002402 }
2403
2404 switch (pathtype) {
Radek Krejci635d2b82021-01-04 11:26:51 +01002405 case LYD_PATH_STD:
2406 case LYD_PATH_STD_NO_LAST_PRED:
Michal Vasko14654712020-02-06 08:35:21 +01002407 depth = 1;
Michal Vasko9e685082021-01-29 14:49:09 +01002408 for (iter = node; iter->parent; iter = lyd_parent(iter)) {
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002409 ++depth;
2410 }
2411
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002412 goto iter_print;
Michal Vasko14654712020-02-06 08:35:21 +01002413 while (depth) {
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002414 /* find the right node */
Michal Vasko9e685082021-01-29 14:49:09 +01002415 for (iter = node, i = 1; i < depth; iter = lyd_parent(iter), ++i) {}
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002416iter_print:
Michal Vasko12eb6222022-03-18 13:35:49 +01002417 /* get the module */
Michal Vasko420cc252023-08-24 08:14:24 +02002418 mod = lyd_node_module(iter);
Michal Vasko12eb6222022-03-18 13:35:49 +01002419 parent = lyd_parent(iter);
Michal Vasko420cc252023-08-24 08:14:24 +02002420 prev_mod = lyd_node_module(parent);
Michal Vasko12eb6222022-03-18 13:35:49 +01002421 if (prev_mod == mod) {
2422 mod = NULL;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002423 }
2424
2425 /* realloc string */
Michal Vaskoad92b672020-11-12 13:11:31 +01002426 len = 1 + (mod ? strlen(mod->name) + 1 : 0) + (iter->schema ? strlen(iter->schema->name) :
2427 strlen(((struct lyd_node_opaq *)iter)->name.name));
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002428 rc = lyd_path_str_enlarge(&buffer, &buflen, bufused + len, is_static);
2429 if (rc != LY_SUCCESS) {
2430 break;
2431 }
2432
2433 /* print next node */
Michal Vaskodbf3e652022-10-21 08:46:25 +02002434 bufused += sprintf(buffer + bufused, "/%s%s%s", mod ? mod->name : "", mod ? ":" : "", LYD_NAME(iter));
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002435
Michal Vasko790b2bc2020-08-03 13:35:06 +02002436 /* do not always print the last (first) predicate */
Radek Krejci635d2b82021-01-04 11:26:51 +01002437 if (iter->schema && ((depth > 1) || (pathtype == LYD_PATH_STD))) {
Michal Vasko790b2bc2020-08-03 13:35:06 +02002438 switch (iter->schema->nodetype) {
2439 case LYS_LIST:
2440 if (iter->schema->flags & LYS_KEYLESS) {
2441 /* print its position */
2442 rc = lyd_path_position_predicate(iter, &buffer, &buflen, &bufused, is_static);
2443 } else {
2444 /* print all list keys in predicates */
2445 rc = lyd_path_list_predicate(iter, &buffer, &buflen, &bufused, is_static);
2446 }
2447 break;
2448 case LYS_LEAFLIST:
2449 if (iter->schema->flags & LYS_CONFIG_W) {
2450 /* print leaf-list value */
2451 rc = lyd_path_leaflist_predicate(iter, &buffer, &buflen, &bufused, is_static);
2452 } else {
2453 /* print its position */
2454 rc = lyd_path_position_predicate(iter, &buffer, &buflen, &bufused, is_static);
2455 }
2456 break;
2457 default:
2458 /* nothing to print more */
2459 break;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002460 }
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002461 }
2462 if (rc != LY_SUCCESS) {
2463 break;
2464 }
2465
Michal Vasko14654712020-02-06 08:35:21 +01002466 --depth;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002467 }
2468 break;
2469 }
2470
2471 return buffer;
2472}
Michal Vaskoe444f752020-02-10 12:20:06 +01002473
Michal Vaskodbf3e652022-10-21 08:46:25 +02002474char *
2475lyd_path_set(const struct ly_set *dnodes, LYD_PATH_TYPE pathtype)
2476{
2477 uint32_t depth;
2478 size_t bufused = 0, buflen = 0, len;
2479 char *buffer = NULL;
2480 const struct lyd_node *iter, *parent;
2481 const struct lys_module *mod, *prev_mod;
2482 LY_ERR rc = LY_SUCCESS;
2483
2484 switch (pathtype) {
2485 case LYD_PATH_STD:
2486 case LYD_PATH_STD_NO_LAST_PRED:
2487 for (depth = 1; depth <= dnodes->count; ++depth) {
2488 /* current node */
2489 iter = dnodes->dnodes[depth - 1];
Michal Vasko420cc252023-08-24 08:14:24 +02002490 mod = lyd_node_module(iter);
Michal Vaskodbf3e652022-10-21 08:46:25 +02002491
2492 /* parent */
2493 parent = (depth > 1) ? dnodes->dnodes[depth - 2] : NULL;
Michal Vasko85be65e2023-06-13 09:44:17 +02002494 assert(!parent || !iter->schema || !parent->schema || (parent->schema->nodetype & LYD_NODE_ANY) ||
2495 (lysc_data_parent(iter->schema) == parent->schema) ||
Michal Vasko539d6a92023-09-11 10:31:12 +02002496 (!lysc_data_parent(iter->schema) && (LYD_CTX(iter) != LYD_CTX(parent))) ||
2497 (parent->schema->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)));
Michal Vaskodbf3e652022-10-21 08:46:25 +02002498
2499 /* get module to print, if any */
Michal Vasko420cc252023-08-24 08:14:24 +02002500 prev_mod = lyd_node_module(parent);
Michal Vaskodbf3e652022-10-21 08:46:25 +02002501 if (prev_mod == mod) {
2502 mod = NULL;
2503 }
2504
2505 /* realloc string */
2506 len = 1 + (mod ? strlen(mod->name) + 1 : 0) + (iter->schema ? strlen(iter->schema->name) :
2507 strlen(((struct lyd_node_opaq *)iter)->name.name));
2508 if ((rc = lyd_path_str_enlarge(&buffer, &buflen, bufused + len, 0))) {
2509 break;
2510 }
2511
2512 /* print next node */
2513 bufused += sprintf(buffer + bufused, "/%s%s%s", mod ? mod->name : "", mod ? ":" : "", LYD_NAME(iter));
2514
2515 /* do not always print the last (first) predicate */
2516 if (iter->schema && ((depth > 1) || (pathtype == LYD_PATH_STD))) {
2517 switch (iter->schema->nodetype) {
2518 case LYS_LIST:
2519 if (iter->schema->flags & LYS_KEYLESS) {
2520 /* print its position */
2521 rc = lyd_path_position_predicate(iter, &buffer, &buflen, &bufused, 0);
2522 } else {
2523 /* print all list keys in predicates */
2524 rc = lyd_path_list_predicate(iter, &buffer, &buflen, &bufused, 0);
2525 }
2526 break;
2527 case LYS_LEAFLIST:
2528 if (iter->schema->flags & LYS_CONFIG_W) {
2529 /* print leaf-list value */
2530 rc = lyd_path_leaflist_predicate(iter, &buffer, &buflen, &bufused, 0);
2531 } else {
2532 /* print its position */
2533 rc = lyd_path_position_predicate(iter, &buffer, &buflen, &bufused, 0);
2534 }
2535 break;
2536 default:
2537 /* nothing to print more */
2538 break;
2539 }
2540 }
2541 if (rc) {
2542 break;
2543 }
2544 }
2545 break;
2546 }
2547
2548 return buffer;
2549}
2550
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002551LIBYANG_API_DEF struct lyd_meta *
Michal Vasko25a32822020-07-09 15:48:22 +02002552lyd_find_meta(const struct lyd_meta *first, const struct lys_module *module, const char *name)
2553{
2554 struct lyd_meta *ret = NULL;
2555 const struct ly_ctx *ctx;
2556 const char *prefix, *tmp;
2557 char *str;
2558 size_t pref_len, name_len;
2559
2560 LY_CHECK_ARG_RET(NULL, module || strchr(name, ':'), name, NULL);
Michal Vasko892f5bf2021-11-24 10:41:05 +01002561 LY_CHECK_CTX_EQUAL_RET(first ? first->annotation->module->ctx : NULL, module ? module->ctx : NULL, NULL);
Michal Vasko25a32822020-07-09 15:48:22 +02002562
2563 if (!first) {
2564 return NULL;
2565 }
2566
2567 ctx = first->annotation->module->ctx;
2568
2569 /* parse the name */
2570 tmp = name;
2571 if (ly_parse_nodeid(&tmp, &prefix, &pref_len, &name, &name_len) || tmp[0]) {
2572 LOGERR(ctx, LY_EINVAL, "Metadata name \"%s\" is not valid.", name);
2573 return NULL;
2574 }
2575
2576 /* find the module */
2577 if (prefix) {
2578 str = strndup(prefix, pref_len);
2579 module = ly_ctx_get_module_latest(ctx, str);
2580 free(str);
Radek Krejci422afb12021-03-04 16:38:16 +01002581 LY_CHECK_ERR_RET(!module, LOGERR(ctx, LY_EINVAL, "Module \"%.*s\" not found.", (int)pref_len, prefix), NULL);
Michal Vasko25a32822020-07-09 15:48:22 +02002582 }
2583
2584 /* find the metadata */
2585 LY_LIST_FOR(first, first) {
2586 if ((first->annotation->module == module) && !strcmp(first->name, name)) {
2587 ret = (struct lyd_meta *)first;
2588 break;
2589 }
2590 }
2591
2592 return ret;
2593}
2594
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002595LIBYANG_API_DEF LY_ERR
Michal Vaskoe444f752020-02-10 12:20:06 +01002596lyd_find_sibling_first(const struct lyd_node *siblings, const struct lyd_node *target, struct lyd_node **match)
2597{
Michal Vasko9beceb82022-04-05 12:14:15 +02002598 struct lyd_node **match_p, *iter, *dup = NULL;
Michal Vaskoe444f752020-02-10 12:20:06 +01002599 struct lyd_node_inner *parent;
Michal Vaskoe78faec2021-04-08 17:24:43 +02002600 ly_bool found;
Michal Vaskoe444f752020-02-10 12:20:06 +01002601
Michal Vaskof03ed032020-03-04 13:31:44 +01002602 LY_CHECK_ARG_RET(NULL, target, LY_EINVAL);
Michal Vasko9beceb82022-04-05 12:14:15 +02002603 if (!siblings) {
2604 /* no data */
2605 if (match) {
2606 *match = NULL;
2607 }
2608 return LY_ENOTFOUND;
2609 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002610
Michal Vasko9beceb82022-04-05 12:14:15 +02002611 if (LYD_CTX(siblings) != LYD_CTX(target)) {
2612 /* create a duplicate in this context */
2613 LY_CHECK_RET(lyd_dup_single_to_ctx(target, LYD_CTX(siblings), NULL, 0, &dup));
2614 target = dup;
2615 }
2616
2617 if ((siblings->schema && target->schema && (lysc_data_parent(siblings->schema) != lysc_data_parent(target->schema)))) {
2618 /* schema mismatch */
2619 lyd_free_tree(dup);
Michal Vasko9b368d32020-02-14 13:53:31 +01002620 if (match) {
2621 *match = NULL;
2622 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002623 return LY_ENOTFOUND;
2624 }
2625
Michal Vaskoe78faec2021-04-08 17:24:43 +02002626 /* get first sibling */
2627 siblings = lyd_first_sibling(siblings);
Michal Vaskoe444f752020-02-10 12:20:06 +01002628
Michal Vasko9e685082021-01-29 14:49:09 +01002629 parent = siblings->parent;
Michal Vasko39311152023-08-07 11:03:41 +02002630 if (target->schema && parent && parent->schema && parent->children_ht) {
Michal Vaskoe444f752020-02-10 12:20:06 +01002631 assert(target->hash);
2632
Michal Vaskoe78faec2021-04-08 17:24:43 +02002633 if (lysc_is_dup_inst_list(target->schema)) {
2634 /* we must search the instances from beginning to find the first matching one */
2635 found = 0;
2636 LYD_LIST_FOR_INST(siblings, target->schema, iter) {
Michal Vaskoee9b9482023-06-19 13:17:48 +02002637 if (!lyd_compare_single(target, iter, LYD_COMPARE_FULL_RECURSION)) {
Michal Vaskoe78faec2021-04-08 17:24:43 +02002638 found = 1;
2639 break;
2640 }
2641 }
2642 if (found) {
2643 siblings = iter;
Michal Vaskoda859032020-07-14 12:20:14 +02002644 } else {
2645 siblings = NULL;
2646 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002647 } else {
Michal Vaskoe78faec2021-04-08 17:24:43 +02002648 /* find by hash */
2649 if (!lyht_find(parent->children_ht, &target, target->hash, (void **)&match_p)) {
2650 siblings = *match_p;
2651 } else {
2652 /* not found */
2653 siblings = NULL;
2654 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002655 }
2656 } else {
Michal Vasko39311152023-08-07 11:03:41 +02002657 /* no children hash table or cannot be used */
Michal Vaskod989ba02020-08-24 10:59:24 +02002658 for ( ; siblings; siblings = siblings->next) {
Michal Vaskoee9b9482023-06-19 13:17:48 +02002659 if (lysc_is_dup_inst_list(target->schema)) {
2660 if (!lyd_compare_single(siblings, target, LYD_COMPARE_FULL_RECURSION)) {
2661 break;
2662 }
2663 } else {
Michal Vaskod8a52012023-08-15 11:38:10 +02002664 if (!lyd_compare_single(siblings, target, 0)) {
Michal Vaskoee9b9482023-06-19 13:17:48 +02002665 break;
2666 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002667 }
2668 }
2669 }
2670
Michal Vasko9beceb82022-04-05 12:14:15 +02002671 lyd_free_tree(dup);
Michal Vaskoe444f752020-02-10 12:20:06 +01002672 if (!siblings) {
Michal Vasko9b368d32020-02-14 13:53:31 +01002673 if (match) {
2674 *match = NULL;
2675 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002676 return LY_ENOTFOUND;
2677 }
2678
Michal Vasko9b368d32020-02-14 13:53:31 +01002679 if (match) {
2680 *match = (struct lyd_node *)siblings;
2681 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002682 return LY_SUCCESS;
2683}
2684
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002685LIBYANG_API_DEF LY_ERR
Michal Vaskoe444f752020-02-10 12:20:06 +01002686lyd_find_sibling_val(const struct lyd_node *siblings, const struct lysc_node *schema, const char *key_or_value,
Radek Krejci0f969882020-08-21 16:56:47 +02002687 size_t val_len, struct lyd_node **match)
Michal Vaskoe444f752020-02-10 12:20:06 +01002688{
2689 LY_ERR rc;
2690 struct lyd_node *target = NULL;
Michal Vasko9beceb82022-04-05 12:14:15 +02002691 const struct lyd_node *parent;
Michal Vaskoe444f752020-02-10 12:20:06 +01002692
Michal Vasko4c583e82020-07-17 12:16:14 +02002693 LY_CHECK_ARG_RET(NULL, schema, !(schema->nodetype & (LYS_CHOICE | LYS_CASE)), LY_EINVAL);
Michal Vasko9beceb82022-04-05 12:14:15 +02002694 if (!siblings) {
2695 /* no data */
2696 if (match) {
2697 *match = NULL;
2698 }
2699 return LY_ENOTFOUND;
2700 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002701
Michal Vasko9beceb82022-04-05 12:14:15 +02002702 if ((LYD_CTX(siblings) != schema->module->ctx)) {
2703 /* parent of ext nodes is useless */
2704 parent = (siblings->flags & LYD_EXT) ? NULL : lyd_parent(siblings);
Michal Vaskoaf3df492022-12-02 14:03:52 +01002705 if (lyd_find_schema_ctx(schema, LYD_CTX(siblings), parent, 0, &schema)) {
2706 /* no schema node in siblings so certainly no data node either */
2707 if (match) {
2708 *match = NULL;
2709 }
2710 return LY_ENOTFOUND;
2711 }
Michal Vasko9beceb82022-04-05 12:14:15 +02002712 }
2713
2714 if (siblings->schema && (lysc_data_parent(siblings->schema) != lysc_data_parent(schema))) {
2715 /* schema mismatch */
Michal Vasko9b368d32020-02-14 13:53:31 +01002716 if (match) {
2717 *match = NULL;
2718 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002719 return LY_ENOTFOUND;
2720 }
2721
Michal Vaskof03ed032020-03-04 13:31:44 +01002722 if (key_or_value && !val_len) {
2723 val_len = strlen(key_or_value);
2724 }
2725
Michal Vaskob104f112020-07-17 09:54:54 +02002726 if ((schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) && key_or_value) {
2727 /* create a data node and find the instance */
2728 if (schema->nodetype == LYS_LEAFLIST) {
2729 /* target used attributes: schema, hash, value */
Michal Vasko989cdb42023-10-06 15:32:37 +02002730 rc = lyd_create_term(schema, key_or_value, val_len, 0, NULL, LY_VALUE_JSON, NULL, LYD_HINT_DATA, NULL, &target);
Michal Vaskofeca4fb2020-10-05 08:58:40 +02002731 LY_CHECK_RET(rc);
Michal Vaskob104f112020-07-17 09:54:54 +02002732 } else {
Michal Vasko90932a92020-02-12 14:33:03 +01002733 /* target used attributes: schema, hash, child (all keys) */
Michal Vasko004d3152020-06-11 19:59:22 +02002734 LY_CHECK_RET(lyd_create_list2(schema, key_or_value, val_len, &target));
Michal Vasko90932a92020-02-12 14:33:03 +01002735 }
2736
2737 /* find it */
2738 rc = lyd_find_sibling_first(siblings, target, match);
Michal Vaskob104f112020-07-17 09:54:54 +02002739 } else {
2740 /* find the first schema node instance */
2741 rc = lyd_find_sibling_schema(siblings, schema, match);
Michal Vaskoe444f752020-02-10 12:20:06 +01002742 }
2743
Michal Vaskoe444f752020-02-10 12:20:06 +01002744 lyd_free_tree(target);
2745 return rc;
2746}
Michal Vaskoccc02342020-05-21 10:09:21 +02002747
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002748LIBYANG_API_DEF LY_ERR
Michal Vaskoe78faec2021-04-08 17:24:43 +02002749lyd_find_sibling_dup_inst_set(const struct lyd_node *siblings, const struct lyd_node *target, struct ly_set **set)
2750{
2751 struct lyd_node **match_p, *first, *iter;
2752 struct lyd_node_inner *parent;
Michal Vaskoee9b9482023-06-19 13:17:48 +02002753 uint32_t comp_opts;
Michal Vaskoe78faec2021-04-08 17:24:43 +02002754
Michal Vasko83ae7772022-06-08 10:01:55 +02002755 LY_CHECK_ARG_RET(NULL, target, set, LY_EINVAL);
Michal Vasko892f5bf2021-11-24 10:41:05 +01002756 LY_CHECK_CTX_EQUAL_RET(siblings ? LYD_CTX(siblings) : NULL, LYD_CTX(target), LY_EINVAL);
Michal Vaskoe78faec2021-04-08 17:24:43 +02002757
2758 LY_CHECK_RET(ly_set_new(set));
2759
2760 if (!siblings || (siblings->schema && (lysc_data_parent(siblings->schema) != lysc_data_parent(target->schema)))) {
2761 /* no data or schema mismatch */
2762 return LY_ENOTFOUND;
2763 }
2764
Michal Vaskoee9b9482023-06-19 13:17:48 +02002765 /* set options */
Michal Vaskod8a52012023-08-15 11:38:10 +02002766 comp_opts = (lysc_is_dup_inst_list(target->schema) ? LYD_COMPARE_FULL_RECURSION : 0);
Michal Vaskoee9b9482023-06-19 13:17:48 +02002767
Michal Vaskoe78faec2021-04-08 17:24:43 +02002768 /* get first sibling */
2769 siblings = lyd_first_sibling(siblings);
2770
2771 parent = siblings->parent;
2772 if (parent && parent->schema && parent->children_ht) {
2773 assert(target->hash);
2774
2775 /* find the first instance */
2776 lyd_find_sibling_first(siblings, target, &first);
2777 if (first) {
2778 /* add it so that it is the first in the set */
2779 if (ly_set_add(*set, first, 1, NULL)) {
2780 goto error;
2781 }
2782
2783 /* find by hash */
2784 if (!lyht_find(parent->children_ht, &target, target->hash, (void **)&match_p)) {
2785 iter = *match_p;
2786 } else {
2787 /* not found */
2788 iter = NULL;
2789 }
2790 while (iter) {
2791 /* add all found nodes into the set */
Michal Vaskoee9b9482023-06-19 13:17:48 +02002792 if ((iter != first) && !lyd_compare_single(iter, target, comp_opts) && ly_set_add(*set, iter, 1, NULL)) {
Michal Vaskoe78faec2021-04-08 17:24:43 +02002793 goto error;
2794 }
2795
2796 /* find next instance */
2797 if (lyht_find_next(parent->children_ht, &iter, iter->hash, (void **)&match_p)) {
2798 iter = NULL;
2799 } else {
2800 iter = *match_p;
2801 }
2802 }
2803 }
2804 } else {
2805 /* no children hash table */
2806 LY_LIST_FOR(siblings, siblings) {
Michal Vaskoee9b9482023-06-19 13:17:48 +02002807 if (!lyd_compare_single(target, siblings, comp_opts)) {
Michal Vaskoe78faec2021-04-08 17:24:43 +02002808 ly_set_add(*set, (void *)siblings, 1, NULL);
2809 }
2810 }
2811 }
2812
2813 if (!(*set)->count) {
2814 return LY_ENOTFOUND;
2815 }
2816 return LY_SUCCESS;
2817
2818error:
2819 ly_set_free(*set, NULL);
2820 *set = NULL;
2821 return LY_EMEM;
2822}
2823
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002824LIBYANG_API_DEF LY_ERR
Michal Vasko1d4af6c2021-02-22 13:31:26 +01002825lyd_find_sibling_opaq_next(const struct lyd_node *first, const char *name, struct lyd_node **match)
2826{
2827 LY_CHECK_ARG_RET(NULL, name, LY_EINVAL);
2828
Michal Vaskoe271a312023-08-15 11:46:30 +02002829 if (first && first->schema) {
2830 first = first->prev;
2831 if (first->schema) {
2832 /* no opaque nodes */
2833 first = NULL;
2834 } else {
2835 /* opaque nodes are at the end, find quickly the first */
2836 while (!first->prev->schema) {
2837 first = first->prev;
2838 }
2839 }
2840 }
2841
Michal Vasko1d4af6c2021-02-22 13:31:26 +01002842 for ( ; first; first = first->next) {
Michal Vaskoe271a312023-08-15 11:46:30 +02002843 assert(!first->schema);
2844 if (!strcmp(LYD_NAME(first), name)) {
Michal Vasko1d4af6c2021-02-22 13:31:26 +01002845 break;
2846 }
2847 }
2848
2849 if (match) {
2850 *match = (struct lyd_node *)first;
2851 }
2852 return first ? LY_SUCCESS : LY_ENOTFOUND;
2853}
2854
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002855LIBYANG_API_DEF LY_ERR
Michal Vaskod96e2372023-02-24 16:07:51 +01002856lyd_find_xpath(const struct lyd_node *ctx_node, const char *xpath, struct ly_set **set)
Michal Vaskoccc02342020-05-21 10:09:21 +02002857{
Michal Vaskod96e2372023-02-24 16:07:51 +01002858 LY_CHECK_ARG_RET(NULL, ctx_node, xpath, set, LY_EINVAL);
Michal Vaskoccc02342020-05-21 10:09:21 +02002859
Michal Vaskod96e2372023-02-24 16:07:51 +01002860 return lyd_find_xpath4(ctx_node, ctx_node, xpath, LY_VALUE_JSON, NULL, NULL, set);
2861}
Michal Vaskoccc02342020-05-21 10:09:21 +02002862
Michal Vaskod96e2372023-02-24 16:07:51 +01002863LIBYANG_API_DEF LY_ERR
2864lyd_find_xpath2(const struct lyd_node *ctx_node, const char *xpath, const struct lyxp_var *vars, struct ly_set **set)
2865{
2866 LY_CHECK_ARG_RET(NULL, ctx_node, xpath, set, LY_EINVAL);
Michal Vaskoccc02342020-05-21 10:09:21 +02002867
Michal Vaskod96e2372023-02-24 16:07:51 +01002868 return lyd_find_xpath4(ctx_node, ctx_node, xpath, LY_VALUE_JSON, NULL, vars, set);
Michal Vaskoccc02342020-05-21 10:09:21 +02002869}
Radek Krejcica989142020-11-05 11:32:22 +01002870
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002871LIBYANG_API_DEF LY_ERR
Michal Vaskoddd76592022-01-17 13:34:48 +01002872lyd_find_xpath3(const struct lyd_node *ctx_node, const struct lyd_node *tree, const char *xpath,
2873 const struct lyxp_var *vars, struct ly_set **set)
2874{
2875 LY_CHECK_ARG_RET(NULL, tree, xpath, set, LY_EINVAL);
2876
2877 return lyd_find_xpath4(ctx_node, tree, xpath, LY_VALUE_JSON, NULL, vars, set);
2878}
2879
2880LIBYANG_API_DEF LY_ERR
Michal Vaskod96e2372023-02-24 16:07:51 +01002881lyd_find_xpath4(const struct lyd_node *ctx_node, const struct lyd_node *tree, const char *xpath, LY_VALUE_FORMAT format,
2882 void *prefix_data, const struct lyxp_var *vars, struct ly_set **set)
Michal Vaskoe3716b32021-12-13 16:58:25 +01002883{
Michal Vaskod96e2372023-02-24 16:07:51 +01002884 LY_CHECK_ARG_RET(NULL, tree, xpath, set, LY_EINVAL);
Michal Vaskoe3716b32021-12-13 16:58:25 +01002885
Michal Vaskod96e2372023-02-24 16:07:51 +01002886 *set = NULL;
2887
2888 return lyd_eval_xpath4(ctx_node, tree, NULL, xpath, format, prefix_data, vars, NULL, set, NULL, NULL, NULL);
Michal Vaskoe3716b32021-12-13 16:58:25 +01002889}
2890
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002891LIBYANG_API_DEF LY_ERR
Michal Vaskod96e2372023-02-24 16:07:51 +01002892lyd_eval_xpath(const struct lyd_node *ctx_node, const char *xpath, ly_bool *result)
aPiecekfba75362021-10-07 12:39:48 +02002893{
Michal Vaskod96e2372023-02-24 16:07:51 +01002894 return lyd_eval_xpath3(ctx_node, NULL, xpath, LY_VALUE_JSON, NULL, NULL, result);
Michal Vaskoc9eb3ca2021-07-16 10:20:37 +02002895}
2896
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002897LIBYANG_API_DEF LY_ERR
Michal Vasko10fabfc2022-08-09 08:55:43 +02002898lyd_eval_xpath2(const struct lyd_node *ctx_node, const char *xpath, const struct lyxp_var *vars, ly_bool *result)
2899{
2900 return lyd_eval_xpath3(ctx_node, NULL, xpath, LY_VALUE_JSON, NULL, vars, result);
2901}
2902
2903LIBYANG_API_DEF LY_ERR
Michal Vaskod96e2372023-02-24 16:07:51 +01002904lyd_eval_xpath3(const struct lyd_node *ctx_node, const struct lys_module *cur_mod, const char *xpath,
2905 LY_VALUE_FORMAT format, void *prefix_data, const struct lyxp_var *vars, ly_bool *result)
aPiecekfba75362021-10-07 12:39:48 +02002906{
Michal Vaskod96e2372023-02-24 16:07:51 +01002907 return lyd_eval_xpath4(ctx_node, ctx_node, cur_mod, xpath, format, prefix_data, vars, NULL, NULL, NULL, NULL, result);
2908}
2909
2910LIBYANG_API_DEF LY_ERR
2911lyd_eval_xpath4(const struct lyd_node *ctx_node, const struct lyd_node *tree, const struct lys_module *cur_mod,
2912 const char *xpath, LY_VALUE_FORMAT format, void *prefix_data, const struct lyxp_var *vars, LY_XPATH_TYPE *ret_type,
2913 struct ly_set **node_set, char **string, long double *number, ly_bool *boolean)
2914{
2915 LY_ERR ret = LY_SUCCESS;
2916 struct lyxp_set xp_set = {0};
2917 struct lyxp_expr *exp = NULL;
2918 uint32_t i;
2919
2920 LY_CHECK_ARG_RET(NULL, tree, xpath, ((ret_type && node_set && string && number && boolean) ||
2921 (node_set && !string && !number && !boolean) || (!node_set && string && !number && !boolean) ||
2922 (!node_set && !string && number && !boolean) || (!node_set && !string && !number && boolean)), LY_EINVAL);
2923
2924 /* parse expression */
2925 ret = lyxp_expr_parse((struct ly_ctx *)LYD_CTX(tree), xpath, 0, 1, &exp);
2926 LY_CHECK_GOTO(ret, cleanup);
2927
2928 /* evaluate expression */
2929 ret = lyxp_eval(LYD_CTX(tree), exp, cur_mod, format, prefix_data, ctx_node, ctx_node, tree, vars, &xp_set,
2930 LYXP_IGNORE_WHEN);
2931 LY_CHECK_GOTO(ret, cleanup);
2932
2933 /* return expected result type without or with casting */
2934 if (node_set) {
2935 /* node set */
2936 if (xp_set.type == LYXP_SET_NODE_SET) {
2937 /* transform into a set */
2938 LY_CHECK_GOTO(ret = ly_set_new(node_set), cleanup);
2939 (*node_set)->objs = malloc(xp_set.used * sizeof *(*node_set)->objs);
2940 LY_CHECK_ERR_GOTO(!(*node_set)->objs, LOGMEM(LYD_CTX(tree)); ret = LY_EMEM, cleanup);
2941 (*node_set)->size = xp_set.used;
2942 for (i = 0; i < xp_set.used; ++i) {
2943 if (xp_set.val.nodes[i].type == LYXP_NODE_ELEM) {
2944 ret = ly_set_add(*node_set, xp_set.val.nodes[i].node, 1, NULL);
2945 LY_CHECK_GOTO(ret, cleanup);
2946 }
2947 }
2948 if (ret_type) {
2949 *ret_type = LY_XPATH_NODE_SET;
2950 }
2951 } else if (!string && !number && !boolean) {
2952 LOGERR(LYD_CTX(tree), LY_EINVAL, "XPath \"%s\" result is not a node set.", xpath);
2953 ret = LY_EINVAL;
2954 goto cleanup;
2955 }
2956 }
2957
2958 if (string) {
2959 if ((xp_set.type != LYXP_SET_STRING) && !node_set) {
2960 /* cast into string */
2961 LY_CHECK_GOTO(ret = lyxp_set_cast(&xp_set, LYXP_SET_STRING), cleanup);
2962 }
2963 if (xp_set.type == LYXP_SET_STRING) {
2964 /* string */
2965 *string = xp_set.val.str;
2966 xp_set.val.str = NULL;
2967 if (ret_type) {
2968 *ret_type = LY_XPATH_STRING;
2969 }
2970 }
2971 }
2972
2973 if (number) {
2974 if ((xp_set.type != LYXP_SET_NUMBER) && !node_set) {
2975 /* cast into number */
2976 LY_CHECK_GOTO(ret = lyxp_set_cast(&xp_set, LYXP_SET_NUMBER), cleanup);
2977 }
2978 if (xp_set.type == LYXP_SET_NUMBER) {
2979 /* number */
2980 *number = xp_set.val.num;
2981 if (ret_type) {
2982 *ret_type = LY_XPATH_NUMBER;
2983 }
2984 }
2985 }
2986
2987 if (boolean) {
2988 if ((xp_set.type != LYXP_SET_BOOLEAN) && !node_set) {
2989 /* cast into boolean */
2990 LY_CHECK_GOTO(ret = lyxp_set_cast(&xp_set, LYXP_SET_BOOLEAN), cleanup);
2991 }
2992 if (xp_set.type == LYXP_SET_BOOLEAN) {
2993 /* boolean */
2994 *boolean = xp_set.val.bln;
2995 if (ret_type) {
2996 *ret_type = LY_XPATH_BOOLEAN;
2997 }
2998 }
2999 }
3000
3001cleanup:
3002 lyxp_set_free_content(&xp_set);
3003 lyxp_expr_free((struct ly_ctx *)LYD_CTX(tree), exp);
3004 return ret;
aPiecekfba75362021-10-07 12:39:48 +02003005}
3006
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01003007LIBYANG_API_DEF LY_ERR
Michal Vasko3e1f6552021-01-14 09:27:55 +01003008lyd_find_path(const struct lyd_node *ctx_node, const char *path, ly_bool output, struct lyd_node **match)
3009{
3010 LY_ERR ret = LY_SUCCESS;
3011 struct lyxp_expr *expr = NULL;
3012 struct ly_path *lypath = NULL;
3013
3014 LY_CHECK_ARG_RET(NULL, ctx_node, ctx_node->schema, path, LY_EINVAL);
3015
3016 /* parse the path */
Michal Vaskoed725d72021-06-23 12:03:45 +02003017 ret = ly_path_parse(LYD_CTX(ctx_node), ctx_node->schema, path, strlen(path), 0, LY_PATH_BEGIN_EITHER,
Michal Vasko32ca49b2023-02-17 15:11:35 +01003018 LY_PATH_PREFIX_FIRST, LY_PATH_PRED_SIMPLE, &expr);
Michal Vasko3e1f6552021-01-14 09:27:55 +01003019 LY_CHECK_GOTO(ret, cleanup);
3020
3021 /* compile the path */
Michal Vaskoed725d72021-06-23 12:03:45 +02003022 ret = ly_path_compile(LYD_CTX(ctx_node), NULL, ctx_node->schema, NULL, expr,
Michal Vasko0884d212021-10-14 09:21:46 +02003023 output ? LY_PATH_OPER_OUTPUT : LY_PATH_OPER_INPUT, LY_PATH_TARGET_SINGLE, 0, LY_VALUE_JSON, NULL, &lypath);
Michal Vasko3e1f6552021-01-14 09:27:55 +01003024 LY_CHECK_GOTO(ret, cleanup);
3025
3026 /* evaluate the path */
Michal Vasko838829d2023-10-09 16:06:43 +02003027 ret = ly_path_eval_partial(lypath, ctx_node, NULL, 0, NULL, match);
Michal Vasko3e1f6552021-01-14 09:27:55 +01003028
3029cleanup:
3030 lyxp_expr_free(LYD_CTX(ctx_node), expr);
3031 ly_path_free(LYD_CTX(ctx_node), lypath);
3032 return ret;
3033}
3034
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01003035LIBYANG_API_DEF LY_ERR
Michal Vaskobb22b182021-06-14 08:14:21 +02003036lyd_find_target(const struct ly_path *path, const struct lyd_node *tree, struct lyd_node **match)
3037{
3038 LY_ERR ret;
3039 struct lyd_node *m;
3040
3041 LY_CHECK_ARG_RET(NULL, path, LY_EINVAL);
3042
Michal Vasko90189962023-02-28 12:10:34 +01003043 ret = ly_path_eval(path, tree, NULL, &m);
Michal Vaskobb22b182021-06-14 08:14:21 +02003044 if (ret) {
3045 if (match) {
3046 *match = NULL;
3047 }
3048 return LY_ENOTFOUND;
3049 }
3050
3051 if (match) {
3052 *match = m;
3053 }
3054 return LY_SUCCESS;
3055}
Irfand3b351a2023-09-14 14:52:15 +02003056
3057LIBYANG_API_DEF struct lyd_node *
3058lyd_parent(const struct lyd_node *node)
3059{
3060 if (!node || !node->parent) {
3061 return NULL;
3062 }
3063
3064 return &node->parent->node;
3065}
3066
3067LIBYANG_API_DEF struct lyd_node *
3068lyd_child(const struct lyd_node *node)
3069{
3070 if (!node) {
3071 return NULL;
3072 }
3073
3074 if (!node->schema) {
3075 /* opaq node */
3076 return ((const struct lyd_node_opaq *)node)->child;
3077 }
3078
3079 switch (node->schema->nodetype) {
3080 case LYS_CONTAINER:
3081 case LYS_LIST:
3082 case LYS_RPC:
3083 case LYS_ACTION:
3084 case LYS_NOTIF:
3085 return ((const struct lyd_node_inner *)node)->child;
3086 default:
3087 return NULL;
3088 }
3089}
3090
3091LIBYANG_API_DEF const char *
3092lyd_get_value(const struct lyd_node *node)
3093{
3094 if (!node) {
3095 return NULL;
3096 }
3097
3098 if (!node->schema) {
3099 return ((const struct lyd_node_opaq *)node)->value;
3100 } else if (node->schema->nodetype & LYD_NODE_TERM) {
3101 const struct lyd_value *value = &((const struct lyd_node_term *)node)->value;
3102
3103 return value->_canonical ? value->_canonical : lyd_value_get_canonical(LYD_CTX(node), value);
3104 }
3105
3106 return NULL;
3107}