blob: 82a1ae87379e7f7eb71badd06293a4af171711b3 [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 */
Michal Vasko5c1d4c22023-12-14 10:45:58 +0100675 if (!anchor && node->schema && first_sibling && !first_sibling->prev->schema) {
Michal Vasko6a0bb252023-08-21 11:55:11 +0200676 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) {
Michal Vasko2e784f82024-01-11 09:51:22 +0100777 lyd_unlink(node);
Michal Vaskof03ed032020-03-04 13:31:44 +0100778 }
779
780 while (node) {
781 iter = node->next;
Michal Vasko2e784f82024-01-11 09:51:22 +0100782 lyd_unlink(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;
Michal Vasko2e784f82024-01-11 09:51:22 +0100804 lyd_unlink(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) {
Michal Vasko2e784f82024-01-11 09:51:22 +0100828 lyd_unlink(node);
Michal Vaskob1b5c262020-03-05 14:29:47 +0100829 }
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;
Michal Vasko2e784f82024-01-11 09:51:22 +0100838 lyd_unlink(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 Vasko2e784f82024-01-11 09:51:22 +0100871 lyd_unlink(node);
Michal Vasko4bc2ce32020-12-08 10:06:16 +0100872 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 Vasko2e784f82024-01-11 09:51:22 +0100895 lyd_unlink(node);
Michal Vasko4bc2ce32020-12-08 10:06:16 +0100896 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
Michal Vasko2e784f82024-01-11 09:51:22 +0100902void
903lyd_unlink(struct lyd_node *node)
Michal Vaskof03ed032020-03-04 13:31:44 +0100904{
905 struct lyd_node *iter;
906
907 if (!node) {
908 return;
909 }
910
Michal Vaskob104f112020-07-17 09:54:54 +0200911 /* update hashes while still linked into the tree */
912 lyd_unlink_hash(node);
913
Michal Vaskof03ed032020-03-04 13:31:44 +0100914 /* unlink from siblings */
915 if (node->prev->next) {
916 node->prev->next = node->next;
917 }
918 if (node->next) {
919 node->next->prev = node->prev;
920 } else {
921 /* unlinking the last node */
922 if (node->parent) {
923 iter = node->parent->child;
924 } else {
925 iter = node->prev;
926 while (iter->prev != node) {
927 iter = iter->prev;
928 }
929 }
930 /* update the "last" pointer from the first node */
931 iter->prev = node->prev;
932 }
933
934 /* unlink from parent */
935 if (node->parent) {
936 if (node->parent->child == node) {
937 /* the node is the first child */
938 node->parent->child = node->next;
939 }
940
Michal Vaskoab49dbe2020-07-17 12:32:47 +0200941 /* check for NP container whether its last non-default node is not being unlinked */
Michal Vasko4754d4a2022-12-01 10:11:21 +0100942 lyd_cont_set_dflt(lyd_parent(node));
Michal Vaskoab49dbe2020-07-17 12:32:47 +0200943
Michal Vaskof03ed032020-03-04 13:31:44 +0100944 node->parent = NULL;
945 }
946
947 node->next = NULL;
948 node->prev = node;
949}
950
Michal Vasko2e784f82024-01-11 09:51:22 +0100951LIBYANG_API_DEF void
952lyd_unlink_siblings(struct lyd_node *node)
953{
954 struct lyd_node *next, *elem, *first = NULL;
955
956 LY_LIST_FOR_SAFE(node, next, elem) {
957 if (lysc_is_key(elem->schema) && elem->parent) {
958 LOGERR(LYD_CTX(elem), LY_EINVAL, "Cannot unlink a list key \"%s\", unlink the list instance instead.",
959 LYD_NAME(elem));
960 return;
961 }
962
963 lyd_unlink(elem);
964 lyd_insert_node(NULL, &first, elem, 1);
965 }
966}
967
968LIBYANG_API_DEF void
969lyd_unlink_tree(struct lyd_node *node)
970{
971 if (node && lysc_is_key(node->schema) && node->parent) {
972 LOGERR(LYD_CTX(node), LY_EINVAL, "Cannot unlink a list key \"%s\", unlink the list instance instead.",
973 LYD_NAME(node));
974 return;
975 }
976
977 lyd_unlink(node);
978}
979
Michal Vaskoa5da3292020-08-12 13:10:50 +0200980void
Michal Vasko871a0252020-11-11 18:35:24 +0100981lyd_insert_meta(struct lyd_node *parent, struct lyd_meta *meta, ly_bool clear_dflt)
Radek Krejci1798aae2020-07-14 13:26:06 +0200982{
983 struct lyd_meta *last, *iter;
984
985 assert(parent);
Michal Vaskoa5da3292020-08-12 13:10:50 +0200986
987 if (!meta) {
988 return;
989 }
Radek Krejci1798aae2020-07-14 13:26:06 +0200990
991 for (iter = meta; iter; iter = iter->next) {
992 iter->parent = parent;
993 }
994
995 /* insert as the last attribute */
996 if (parent->meta) {
Radek Krejci1e008d22020-08-17 11:37:37 +0200997 for (last = parent->meta; last->next; last = last->next) {}
Radek Krejci1798aae2020-07-14 13:26:06 +0200998 last->next = meta;
999 } else {
1000 parent->meta = meta;
1001 }
1002
1003 /* remove default flags from NP containers */
Michal Vasko871a0252020-11-11 18:35:24 +01001004 while (clear_dflt && parent && (parent->schema->nodetype == LYS_CONTAINER) && (parent->flags & LYD_DEFAULT)) {
Radek Krejci1798aae2020-07-14 13:26:06 +02001005 parent->flags &= ~LYD_DEFAULT;
Michal Vasko9e685082021-01-29 14:49:09 +01001006 parent = lyd_parent(parent);
Radek Krejci1798aae2020-07-14 13:26:06 +02001007 }
Radek Krejci1798aae2020-07-14 13:26:06 +02001008}
1009
1010LY_ERR
Michal Vasko9f96a052020-03-10 09:41:45 +01001011lyd_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 +02001012 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 +01001013 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 +01001014{
Radek Krejci2efc45b2020-12-22 16:25:44 +01001015 LY_ERR ret = LY_SUCCESS;
Michal Vasko90932a92020-02-12 14:33:03 +01001016 struct lysc_ext_instance *ant = NULL;
Michal Vasko193dacd2022-10-13 08:43:05 +02001017 const struct lysc_type *ant_type;
Michal Vasko9f96a052020-03-10 09:41:45 +01001018 struct lyd_meta *mt, *last;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001019 LY_ARRAY_COUNT_TYPE u;
Michal Vasko90932a92020-02-12 14:33:03 +01001020
Michal Vasko9f96a052020-03-10 09:41:45 +01001021 assert((parent || meta) && mod);
Michal Vasko6f4cbb62020-02-28 11:15:47 +01001022
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001023 LY_ARRAY_FOR(mod->compiled->exts, u) {
Michal Vasko193dacd2022-10-13 08:43:05 +02001024 if (!strncmp(mod->compiled->exts[u].def->plugin->id, "ly2 metadata", 12) &&
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001025 !ly_strncmp(mod->compiled->exts[u].argument, name, name_len)) {
Michal Vasko90932a92020-02-12 14:33:03 +01001026 /* we have the annotation definition */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001027 ant = &mod->compiled->exts[u];
Michal Vasko90932a92020-02-12 14:33:03 +01001028 break;
1029 }
1030 }
1031 if (!ant) {
1032 /* attribute is not defined as a metadata annotation (RFC 7952) */
Radek Krejci2efc45b2020-12-22 16:25:44 +01001033 LOGVAL(mod->ctx, LYVE_REFERENCE, "Annotation definition for attribute \"%s:%.*s\" not found.",
Radek Krejci422afb12021-03-04 16:38:16 +01001034 mod->name, (int)name_len, name);
Radek Krejci2efc45b2020-12-22 16:25:44 +01001035 ret = LY_EINVAL;
1036 goto cleanup;
Michal Vasko90932a92020-02-12 14:33:03 +01001037 }
1038
Michal Vasko9f96a052020-03-10 09:41:45 +01001039 mt = calloc(1, sizeof *mt);
Radek Krejci2efc45b2020-12-22 16:25:44 +01001040 LY_CHECK_ERR_GOTO(!mt, LOGMEM(mod->ctx); ret = LY_EMEM, cleanup);
Michal Vasko9f96a052020-03-10 09:41:45 +01001041 mt->parent = parent;
1042 mt->annotation = ant;
Michal Vaskofbd037c2022-11-08 10:34:20 +01001043 lyplg_ext_get_storage(ant, LY_STMT_TYPE, sizeof ant_type, (const void **)&ant_type);
Michal Vasko989cdb42023-10-06 15:32:37 +02001044 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 +01001045 ctx_node, incomplete);
Radek Krejci2efc45b2020-12-22 16:25:44 +01001046 LY_CHECK_ERR_GOTO(ret, free(mt), cleanup);
1047 ret = lydict_insert(mod->ctx, name, name_len, &mt->name);
1048 LY_CHECK_ERR_GOTO(ret, free(mt), cleanup);
Michal Vasko90932a92020-02-12 14:33:03 +01001049
Michal Vasko6f4cbb62020-02-28 11:15:47 +01001050 /* insert as the last attribute */
1051 if (parent) {
Michal Vasko871a0252020-11-11 18:35:24 +01001052 lyd_insert_meta(parent, mt, clear_dflt);
Michal Vasko9f96a052020-03-10 09:41:45 +01001053 } else if (*meta) {
Radek Krejci1e008d22020-08-17 11:37:37 +02001054 for (last = *meta; last->next; last = last->next) {}
Michal Vasko9f96a052020-03-10 09:41:45 +01001055 last->next = mt;
Michal Vasko90932a92020-02-12 14:33:03 +01001056 }
1057
Michal Vasko9f96a052020-03-10 09:41:45 +01001058 if (meta) {
1059 *meta = mt;
Michal Vasko90932a92020-02-12 14:33:03 +01001060 }
Radek Krejci2efc45b2020-12-22 16:25:44 +01001061
1062cleanup:
Radek Krejci2efc45b2020-12-22 16:25:44 +01001063 return ret;
Michal Vasko90932a92020-02-12 14:33:03 +01001064}
1065
Michal Vaskoa5da3292020-08-12 13:10:50 +02001066void
1067lyd_insert_attr(struct lyd_node *parent, struct lyd_attr *attr)
1068{
1069 struct lyd_attr *last, *iter;
1070 struct lyd_node_opaq *opaq;
1071
1072 assert(parent && !parent->schema);
1073
1074 if (!attr) {
1075 return;
1076 }
1077
1078 opaq = (struct lyd_node_opaq *)parent;
1079 for (iter = attr; iter; iter = iter->next) {
1080 iter->parent = opaq;
1081 }
1082
1083 /* insert as the last attribute */
1084 if (opaq->attr) {
Radek Krejci1e008d22020-08-17 11:37:37 +02001085 for (last = opaq->attr; last->next; last = last->next) {}
Michal Vaskoa5da3292020-08-12 13:10:50 +02001086 last->next = attr;
1087 } else {
1088 opaq->attr = attr;
1089 }
1090}
1091
Michal Vasko52927e22020-03-16 17:26:14 +01001092LY_ERR
Michal Vaskofeca4fb2020-10-05 08:58:40 +02001093lyd_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 +01001094 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 +02001095 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 +01001096{
Radek Krejci011e4aa2020-09-04 15:22:31 +02001097 LY_ERR ret = LY_SUCCESS;
Radek Krejci1798aae2020-07-14 13:26:06 +02001098 struct lyd_attr *at, *last;
Michal Vasko52927e22020-03-16 17:26:14 +01001099
1100 assert(ctx && (parent || attr) && (!parent || !parent->schema));
Michal Vaskofeca4fb2020-10-05 08:58:40 +02001101 assert(name && name_len && format);
Michal Vasko52927e22020-03-16 17:26:14 +01001102
Michal Vasko2a3722d2021-06-16 11:52:39 +02001103 if (!value_len && (!dynamic || !*dynamic)) {
Michal Vasko52927e22020-03-16 17:26:14 +01001104 value = "";
1105 }
1106
1107 at = calloc(1, sizeof *at);
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01001108 LY_CHECK_ERR_RET(!at, LOGMEM(ctx); ly_free_prefix_data(format, val_prefix_data), LY_EMEM);
Radek Krejcid46e46a2020-09-15 14:22:42 +02001109
Michal Vaskoad92b672020-11-12 13:11:31 +01001110 LY_CHECK_GOTO(ret = lydict_insert(ctx, name, name_len, &at->name.name), finish);
Michal Vasko501af032020-11-11 20:27:44 +01001111 if (prefix_len) {
Michal Vaskoad92b672020-11-12 13:11:31 +01001112 LY_CHECK_GOTO(ret = lydict_insert(ctx, prefix, prefix_len, &at->name.prefix), finish);
Michal Vasko501af032020-11-11 20:27:44 +01001113 }
1114 if (module_key_len) {
Michal Vaskoad92b672020-11-12 13:11:31 +01001115 LY_CHECK_GOTO(ret = lydict_insert(ctx, module_key, module_key_len, &at->name.module_ns), finish);
Michal Vasko501af032020-11-11 20:27:44 +01001116 }
1117
Michal Vasko52927e22020-03-16 17:26:14 +01001118 if (dynamic && *dynamic) {
Radek Krejci011e4aa2020-09-04 15:22:31 +02001119 ret = lydict_insert_zc(ctx, (char *)value, &at->value);
1120 LY_CHECK_GOTO(ret, finish);
Michal Vasko52927e22020-03-16 17:26:14 +01001121 *dynamic = 0;
1122 } else {
Radek Krejci011e4aa2020-09-04 15:22:31 +02001123 LY_CHECK_GOTO(ret = lydict_insert(ctx, value, value_len, &at->value), finish);
Michal Vasko52927e22020-03-16 17:26:14 +01001124 }
Michal Vasko501af032020-11-11 20:27:44 +01001125 at->format = format;
1126 at->val_prefix_data = val_prefix_data;
1127 at->hints = hints;
Michal Vasko52927e22020-03-16 17:26:14 +01001128
1129 /* insert as the last attribute */
1130 if (parent) {
Michal Vaskoa5da3292020-08-12 13:10:50 +02001131 lyd_insert_attr(parent, at);
Michal Vasko52927e22020-03-16 17:26:14 +01001132 } else if (*attr) {
Radek Krejci1e008d22020-08-17 11:37:37 +02001133 for (last = *attr; last->next; last = last->next) {}
Michal Vasko52927e22020-03-16 17:26:14 +01001134 last->next = at;
1135 }
1136
Radek Krejci011e4aa2020-09-04 15:22:31 +02001137finish:
1138 if (ret) {
1139 lyd_free_attr_single(ctx, at);
1140 } else if (attr) {
Michal Vasko52927e22020-03-16 17:26:14 +01001141 *attr = at;
1142 }
1143 return LY_SUCCESS;
1144}
1145
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001146LIBYANG_API_DEF const struct lyd_node_term *
Michal Vasko004d3152020-06-11 19:59:22 +02001147lyd_target(const struct ly_path *path, const struct lyd_node *tree)
Radek Krejci084289f2019-07-09 17:35:30 +02001148{
Michal Vasko90189962023-02-28 12:10:34 +01001149 struct lyd_node *target = NULL;
Radek Krejci084289f2019-07-09 17:35:30 +02001150
Michal Vasko90189962023-02-28 12:10:34 +01001151 lyd_find_target(path, tree, &target);
Radek Krejci084289f2019-07-09 17:35:30 +02001152
Michal Vasko004d3152020-06-11 19:59:22 +02001153 return (struct lyd_node_term *)target;
Radek Krejci084289f2019-07-09 17:35:30 +02001154}
1155
aPiecek2f63f952021-03-30 12:22:18 +02001156/**
1157 * @brief Check the equality of the two schemas from different contexts.
1158 *
1159 * @param schema1 of first node.
1160 * @param schema2 of second node.
1161 * @return 1 if the schemas are equal otherwise 0.
1162 */
1163static ly_bool
1164lyd_compare_schema_equal(const struct lysc_node *schema1, const struct lysc_node *schema2)
1165{
1166 if (!schema1 && !schema2) {
1167 return 1;
1168 } else if (!schema1 || !schema2) {
1169 return 0;
1170 }
1171
1172 assert(schema1->module->ctx != schema2->module->ctx);
1173
1174 if (schema1->nodetype != schema2->nodetype) {
1175 return 0;
1176 }
1177
1178 if (strcmp(schema1->name, schema2->name)) {
1179 return 0;
1180 }
1181
1182 if (strcmp(schema1->module->name, schema2->module->name)) {
1183 return 0;
1184 }
1185
aPiecek2f63f952021-03-30 12:22:18 +02001186 return 1;
1187}
1188
1189/**
1190 * @brief Check the equality of the schemas for all parent nodes.
1191 *
1192 * Both nodes must be from different contexts.
1193 *
1194 * @param node1 Data of first node.
1195 * @param node2 Data of second node.
1196 * @return 1 if the all related parental schemas are equal otherwise 0.
1197 */
1198static ly_bool
1199lyd_compare_schema_parents_equal(const struct lyd_node *node1, const struct lyd_node *node2)
1200{
1201 const struct lysc_node *parent1, *parent2;
1202
1203 assert(node1 && node2);
1204
1205 for (parent1 = node1->schema->parent, parent2 = node2->schema->parent;
1206 parent1 && parent2;
1207 parent1 = parent1->parent, parent2 = parent2->parent) {
1208 if (!lyd_compare_schema_equal(parent1, parent2)) {
1209 return 0;
1210 }
1211 }
1212
1213 if (parent1 || parent2) {
1214 return 0;
1215 }
1216
1217 return 1;
1218}
1219
1220/**
Michal Vaskodf8ebf62022-11-10 10:33:28 +01001221 * @brief Compare 2 nodes values including opaque node values.
1222 *
1223 * @param[in] node1 First node to compare.
1224 * @param[in] node2 Second node to compare.
1225 * @return LY_SUCCESS if equal.
1226 * @return LY_ENOT if not equal.
1227 * @return LY_ERR on error.
1228 */
1229static LY_ERR
1230lyd_compare_single_value(const struct lyd_node *node1, const struct lyd_node *node2)
1231{
1232 const struct lyd_node_opaq *opaq1 = NULL, *opaq2 = NULL;
1233 const char *val1, *val2, *col;
1234 const struct lys_module *mod;
1235 char *val_dyn = NULL;
1236 LY_ERR rc = LY_SUCCESS;
1237
1238 if (!node1->schema) {
1239 opaq1 = (struct lyd_node_opaq *)node1;
1240 }
1241 if (!node2->schema) {
1242 opaq2 = (struct lyd_node_opaq *)node2;
1243 }
1244
1245 if (opaq1 && opaq2 && (opaq1->format == LY_VALUE_XML) && (opaq2->format == LY_VALUE_XML)) {
1246 /* opaque XML and opaque XML node */
1247 if (lyxml_value_compare(LYD_CTX(node1), opaq1->value, opaq1->val_prefix_data, LYD_CTX(node2), opaq2->value,
1248 opaq2->val_prefix_data)) {
1249 return LY_ENOT;
1250 }
1251 return LY_SUCCESS;
1252 }
1253
1254 /* get their values */
1255 if (opaq1 && ((opaq1->format == LY_VALUE_XML) || (opaq1->format == LY_VALUE_STR_NS)) && (col = strchr(opaq1->value, ':'))) {
1256 /* XML value with a prefix, try to transform it into a JSON (canonical) value */
1257 mod = ly_resolve_prefix(LYD_CTX(node1), opaq1->value, col - opaq1->value, opaq1->format, opaq1->val_prefix_data);
1258 if (!mod) {
1259 /* unable to compare */
1260 return LY_ENOT;
1261 }
1262
1263 if (asprintf(&val_dyn, "%s%s", mod->name, col) == -1) {
1264 LOGMEM(LYD_CTX(node1));
1265 return LY_EMEM;
1266 }
1267 val1 = val_dyn;
1268 } else {
1269 val1 = lyd_get_value(node1);
1270 }
1271 if (opaq2 && ((opaq2->format == LY_VALUE_XML) || (opaq2->format == LY_VALUE_STR_NS)) && (col = strchr(opaq2->value, ':'))) {
1272 mod = ly_resolve_prefix(LYD_CTX(node2), opaq2->value, col - opaq2->value, opaq2->format, opaq2->val_prefix_data);
1273 if (!mod) {
1274 return LY_ENOT;
1275 }
1276
1277 assert(!val_dyn);
1278 if (asprintf(&val_dyn, "%s%s", mod->name, col) == -1) {
1279 LOGMEM(LYD_CTX(node2));
1280 return LY_EMEM;
1281 }
1282 val2 = val_dyn;
1283 } else {
1284 val2 = lyd_get_value(node2);
1285 }
1286
1287 /* compare values */
1288 if (strcmp(val1, val2)) {
1289 rc = LY_ENOT;
1290 }
1291
1292 free(val_dyn);
1293 return rc;
1294}
1295
1296/**
Michal Vaskof277d362023-04-24 09:08:31 +02001297 * @brief Compare 2 data nodes if they are equivalent regarding the schema tree.
1298 *
1299 * Works correctly even if @p node1 and @p node2 have different contexts.
1300 *
1301 * @param[in] node1 The first node to compare.
1302 * @param[in] node2 The second node to compare.
1303 * @param[in] options Various @ref datacompareoptions.
1304 * @param[in] parental_schemas_checked Flag set if parent schemas were checked for match.
1305 * @return LY_SUCCESS if the nodes are equivalent.
1306 * @return LY_ENOT if the nodes are not equivalent.
aPiecek2f63f952021-03-30 12:22:18 +02001307 */
1308static LY_ERR
Michal Vaskof277d362023-04-24 09:08:31 +02001309lyd_compare_single_schema(const struct lyd_node *node1, const struct lyd_node *node2, uint32_t options,
Michal Vaskodf8ebf62022-11-10 10:33:28 +01001310 ly_bool parental_schemas_checked)
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001311{
aPiecek2f63f952021-03-30 12:22:18 +02001312 if (LYD_CTX(node1) == LYD_CTX(node2)) {
1313 /* same contexts */
Michal Vaskodf8ebf62022-11-10 10:33:28 +01001314 if (options & LYD_COMPARE_OPAQ) {
1315 if (lyd_node_schema(node1) != lyd_node_schema(node2)) {
1316 return LY_ENOT;
1317 }
1318 } else {
1319 if (node1->schema != node2->schema) {
1320 return LY_ENOT;
1321 }
aPiecek2f63f952021-03-30 12:22:18 +02001322 }
1323 } else {
1324 /* different contexts */
1325 if (!lyd_compare_schema_equal(node1->schema, node2->schema)) {
1326 return LY_ENOT;
1327 }
1328 if (!parental_schemas_checked) {
1329 if (!lyd_compare_schema_parents_equal(node1, node2)) {
1330 return LY_ENOT;
1331 }
1332 parental_schemas_checked = 1;
1333 }
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001334 }
1335
Michal Vaskof277d362023-04-24 09:08:31 +02001336 return LY_SUCCESS;
1337}
1338
1339/**
1340 * @brief Compare 2 data nodes if they are equivalent regarding the data they contain.
1341 *
1342 * Works correctly even if @p node1 and @p node2 have different contexts.
1343 *
1344 * @param[in] node1 The first node to compare.
1345 * @param[in] node2 The second node to compare.
1346 * @param[in] options Various @ref datacompareoptions.
1347 * @return LY_SUCCESS if the nodes are equivalent.
1348 * @return LY_ENOT if the nodes are not equivalent.
1349 */
1350static LY_ERR
1351lyd_compare_single_data(const struct lyd_node *node1, const struct lyd_node *node2, uint32_t options)
1352{
1353 const struct lyd_node *iter1, *iter2;
1354 struct lyd_node_any *any1, *any2;
1355 int len1, len2;
1356 LY_ERR r;
1357
Michal Vaskodf8ebf62022-11-10 10:33:28 +01001358 if (!(options & LYD_COMPARE_OPAQ) && (node1->hash != node2->hash)) {
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001359 return LY_ENOT;
1360 }
aPiecek2f63f952021-03-30 12:22:18 +02001361 /* 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 +02001362
Michal Vaskodf8ebf62022-11-10 10:33:28 +01001363 if (!node1->schema || !node2->schema) {
1364 if (!(options & LYD_COMPARE_OPAQ) && ((node1->schema && !node2->schema) || (!node1->schema && node2->schema))) {
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001365 return LY_ENOT;
1366 }
Michal Vasko7b0500d2023-03-20 15:22:46 +01001367 if ((!node1->schema && !node2->schema) || (node1->schema && (node1->schema->nodetype & LYD_NODE_TERM)) ||
1368 (node2->schema && (node2->schema->nodetype & LYD_NODE_TERM))) {
1369 /* compare values only if there are any to compare */
1370 if ((r = lyd_compare_single_value(node1, node2))) {
1371 return r;
1372 }
Michal Vasko52927e22020-03-16 17:26:14 +01001373 }
Michal Vaskodf8ebf62022-11-10 10:33:28 +01001374
Michal Vasko52927e22020-03-16 17:26:14 +01001375 if (options & LYD_COMPARE_FULL_RECURSION) {
Michal Vaskof277d362023-04-24 09:08:31 +02001376 return lyd_compare_siblings_(lyd_child(node1), lyd_child(node2), options, 1);
Michal Vasko52927e22020-03-16 17:26:14 +01001377 }
1378 return LY_SUCCESS;
1379 } else {
1380 switch (node1->schema->nodetype) {
1381 case LYS_LEAF:
1382 case LYS_LEAFLIST:
1383 if (options & LYD_COMPARE_DEFAULTS) {
1384 if ((node1->flags & LYD_DEFAULT) != (node2->flags & LYD_DEFAULT)) {
1385 return LY_ENOT;
1386 }
1387 }
Michal Vaskodf8ebf62022-11-10 10:33:28 +01001388 if ((r = lyd_compare_single_value(node1, node2))) {
1389 return r;
aPiecek2f63f952021-03-30 12:22:18 +02001390 }
1391
aPiecek2f63f952021-03-30 12:22:18 +02001392 return LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +01001393 case LYS_CONTAINER:
Michal Vaskoc8187dc2022-05-13 12:26:40 +02001394 case LYS_RPC:
1395 case LYS_ACTION:
1396 case LYS_NOTIF:
Michal Vaskoa38adc72023-04-25 10:14:28 +02001397 /* implicit container is always equal to a container with non-default descendants */
Michal Vasko52927e22020-03-16 17:26:14 +01001398 if (options & LYD_COMPARE_FULL_RECURSION) {
Michal Vaskof277d362023-04-24 09:08:31 +02001399 return lyd_compare_siblings_(lyd_child(node1), lyd_child(node2), options, 1);
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001400 }
1401 return LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +01001402 case LYS_LIST:
Michal Vasko9e685082021-01-29 14:49:09 +01001403 iter1 = lyd_child(node1);
1404 iter2 = lyd_child(node2);
Michal Vasko52927e22020-03-16 17:26:14 +01001405
Michal Vaskoee9b9482023-06-19 13:17:48 +02001406 if (options & LYD_COMPARE_FULL_RECURSION) {
Michal Vaskof277d362023-04-24 09:08:31 +02001407 return lyd_compare_siblings_(iter1, iter2, options, 1);
Michal Vaskoee9b9482023-06-19 13:17:48 +02001408 } else if (node1->schema->flags & LYS_KEYLESS) {
1409 /* always equal */
1410 return LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +01001411 }
Michal Vaskoee9b9482023-06-19 13:17:48 +02001412
1413 /* lists with keys, their equivalence is based on their keys */
1414 for (const struct lysc_node *key = lysc_node_child(node1->schema);
1415 key && (key->flags & LYS_KEY);
1416 key = key->next) {
1417 if (!iter1 || !iter2) {
1418 return (iter1 == iter2) ? LY_SUCCESS : LY_ENOT;
1419 }
1420 r = lyd_compare_single_schema(iter1, iter2, options, 1);
1421 LY_CHECK_RET(r);
1422 r = lyd_compare_single_data(iter1, iter2, options);
1423 LY_CHECK_RET(r);
1424
1425 iter1 = iter1->next;
1426 iter2 = iter2->next;
1427 }
1428
1429 return LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +01001430 case LYS_ANYXML:
1431 case LYS_ANYDATA:
Michal Vasko22df3f02020-08-24 13:29:22 +02001432 any1 = (struct lyd_node_any *)node1;
1433 any2 = (struct lyd_node_any *)node2;
Michal Vasko52927e22020-03-16 17:26:14 +01001434
1435 if (any1->value_type != any2->value_type) {
1436 return LY_ENOT;
1437 }
1438 switch (any1->value_type) {
1439 case LYD_ANYDATA_DATATREE:
Michal Vaskof277d362023-04-24 09:08:31 +02001440 return lyd_compare_siblings_(any1->value.tree, any2->value.tree, options, 1);
Michal Vasko52927e22020-03-16 17:26:14 +01001441 case LYD_ANYDATA_STRING:
1442 case LYD_ANYDATA_XML:
1443 case LYD_ANYDATA_JSON:
Michal Vasko3b4ec412022-09-22 15:24:14 +02001444 if ((!any1->value.str && any2->value.str) || (any1->value.str && !any2->value.str)) {
1445 return LY_ENOT;
1446 } else if (!any1->value.str && !any2->value.str) {
1447 return LY_SUCCESS;
1448 }
Michal Vasko52927e22020-03-16 17:26:14 +01001449 len1 = strlen(any1->value.str);
1450 len2 = strlen(any2->value.str);
Michal Vasko69730152020-10-09 16:30:07 +02001451 if ((len1 != len2) || strcmp(any1->value.str, any2->value.str)) {
Michal Vasko52927e22020-03-16 17:26:14 +01001452 return LY_ENOT;
1453 }
1454 return LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +01001455 case LYD_ANYDATA_LYB:
Michal Vasko60ea6352020-06-29 13:39:39 +02001456 len1 = lyd_lyb_data_length(any1->value.mem);
1457 len2 = lyd_lyb_data_length(any2->value.mem);
Michal Vasko2b97d472022-08-17 09:29:03 +02001458 if ((len1 == -1) || (len2 == -1) || (len1 != len2) || memcmp(any1->value.mem, any2->value.mem, len1)) {
Michal Vasko52927e22020-03-16 17:26:14 +01001459 return LY_ENOT;
1460 }
1461 return LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +01001462 }
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001463 }
1464 }
1465
Michal Vaskob7be7a82020-08-20 09:09:04 +02001466 LOGINT(LYD_CTX(node1));
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001467 return LY_EINT;
1468}
Radek Krejci22ebdba2019-07-25 13:59:43 +02001469
Michal Vaskof277d362023-04-24 09:08:31 +02001470/**
1471 * @brief Compare all siblings at a node level.
1472 *
1473 * @param[in] node1 First sibling list.
1474 * @param[in] node2 Second sibling list.
1475 * @param[in] options Various @ref datacompareoptions.
1476 * @param[in] parental_schemas_checked Flag set if parent schemas were checked for match.
1477 * @return LY_SUCCESS if equal.
1478 * @return LY_ENOT if not equal.
1479 * @return LY_ERR on error.
1480 */
1481static LY_ERR
1482lyd_compare_siblings_(const struct lyd_node *node1, const struct lyd_node *node2, uint32_t options,
1483 ly_bool parental_schemas_checked)
1484{
1485 LY_ERR r;
1486 const struct lyd_node *iter2;
1487
1488 while (node1 && node2) {
1489 /* schema match */
1490 r = lyd_compare_single_schema(node1, node2, options, parental_schemas_checked);
1491 LY_CHECK_RET(r);
1492
1493 if (node1->schema && (((node1->schema->nodetype == LYS_LIST) && !(node1->schema->flags & LYS_KEYLESS)) ||
1494 ((node1->schema->nodetype == LYS_LEAFLIST) && (node1->schema->flags & LYS_CONFIG_W))) &&
1495 (node1->schema->flags & LYS_ORDBY_SYSTEM)) {
1496 /* find a matching instance in case they are ordered differently */
1497 r = lyd_find_sibling_first(node2, node1, (struct lyd_node **)&iter2);
1498 if (r == LY_ENOTFOUND) {
1499 /* no matching instance, data not equal */
1500 r = LY_ENOT;
1501 }
1502 LY_CHECK_RET(r);
1503 } else {
1504 /* compare with the current node */
1505 iter2 = node2;
1506 }
1507
1508 /* data match */
1509 r = lyd_compare_single_data(node1, iter2, options | LYD_COMPARE_FULL_RECURSION);
1510 LY_CHECK_RET(r);
1511
1512 node1 = node1->next;
1513 node2 = node2->next;
1514 }
1515
1516 return (node1 || node2) ? LY_ENOT : LY_SUCCESS;
1517}
1518
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001519LIBYANG_API_DEF LY_ERR
aPiecek2f63f952021-03-30 12:22:18 +02001520lyd_compare_single(const struct lyd_node *node1, const struct lyd_node *node2, uint32_t options)
1521{
Michal Vaskof277d362023-04-24 09:08:31 +02001522 LY_ERR r;
1523
1524 if (!node1 || !node2) {
1525 return (node1 == node2) ? LY_SUCCESS : LY_ENOT;
1526 }
1527
1528 /* schema match */
1529 if ((r = lyd_compare_single_schema(node1, node2, options, 0))) {
1530 return r;
1531 }
1532
1533 /* data match */
1534 return lyd_compare_single_data(node1, node2, options);
aPiecek2f63f952021-03-30 12:22:18 +02001535}
1536
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001537LIBYANG_API_DEF LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02001538lyd_compare_siblings(const struct lyd_node *node1, const struct lyd_node *node2, uint32_t options)
Michal Vasko8f359bf2020-07-28 10:41:15 +02001539{
Michal Vaskof277d362023-04-24 09:08:31 +02001540 return lyd_compare_siblings_(node1, node2, options, 0);
Michal Vasko8f359bf2020-07-28 10:41:15 +02001541}
1542
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001543LIBYANG_API_DEF LY_ERR
Michal Vasko21725742020-06-29 11:49:25 +02001544lyd_compare_meta(const struct lyd_meta *meta1, const struct lyd_meta *meta2)
1545{
1546 if (!meta1 || !meta2) {
1547 if (meta1 == meta2) {
1548 return LY_SUCCESS;
1549 } else {
1550 return LY_ENOT;
1551 }
1552 }
1553
Michal Vaskoa8083062020-11-06 17:22:10 +01001554 if ((meta1->annotation->module->ctx != meta2->annotation->module->ctx) || (meta1->annotation != meta2->annotation)) {
Michal Vasko21725742020-06-29 11:49:25 +02001555 return LY_ENOT;
1556 }
1557
Michal Vasko21725742020-06-29 11:49:25 +02001558 return meta1->value.realtype->plugin->compare(&meta1->value, &meta2->value);
1559}
1560
Radek Krejci22ebdba2019-07-25 13:59:43 +02001561/**
Michal Vasko9cf62422021-07-01 13:29:32 +02001562 * @brief Create a copy of the attribute.
1563 *
1564 * @param[in] attr Attribute to copy.
1565 * @param[in] node Opaque where to append the new attribute.
1566 * @param[out] dup Optional created attribute copy.
1567 * @return LY_ERR value.
1568 */
1569static LY_ERR
1570lyd_dup_attr_single(const struct lyd_attr *attr, struct lyd_node *node, struct lyd_attr **dup)
1571{
1572 LY_ERR ret = LY_SUCCESS;
1573 struct lyd_attr *a, *last;
1574 struct lyd_node_opaq *opaq = (struct lyd_node_opaq *)node;
1575
1576 LY_CHECK_ARG_RET(NULL, attr, node, !node->schema, LY_EINVAL);
1577
1578 /* create a copy */
1579 a = calloc(1, sizeof *attr);
1580 LY_CHECK_ERR_RET(!a, LOGMEM(LYD_CTX(node)), LY_EMEM);
1581
1582 LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), attr->name.name, 0, &a->name.name), finish);
1583 LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), attr->name.prefix, 0, &a->name.prefix), finish);
1584 LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), attr->name.module_ns, 0, &a->name.module_ns), finish);
1585 LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), attr->value, 0, &a->value), finish);
1586 a->hints = attr->hints;
1587 a->format = attr->format;
1588 if (attr->val_prefix_data) {
1589 ret = ly_dup_prefix_data(LYD_CTX(node), attr->format, attr->val_prefix_data, &a->val_prefix_data);
1590 LY_CHECK_GOTO(ret, finish);
1591 }
1592
1593 /* insert as the last attribute */
1594 a->parent = opaq;
1595 if (opaq->attr) {
1596 for (last = opaq->attr; last->next; last = last->next) {}
1597 last->next = a;
1598 } else {
1599 opaq->attr = a;
1600 }
1601
1602finish:
1603 if (ret) {
1604 lyd_free_attr_single(LYD_CTX(node), a);
1605 } else if (dup) {
1606 *dup = a;
1607 }
1608 return LY_SUCCESS;
1609}
1610
1611/**
Michal Vaskoddd76592022-01-17 13:34:48 +01001612 * @brief Find @p schema equivalent in @p trg_ctx.
1613 *
1614 * @param[in] schema Schema node to find.
1615 * @param[in] trg_ctx Target context to search in.
1616 * @param[in] parent Data parent of @p schema, if any.
Michal Vaskoaf3df492022-12-02 14:03:52 +01001617 * @param[in] log Whether to log directly.
Michal Vaskoddd76592022-01-17 13:34:48 +01001618 * @param[out] trg_schema Found schema from @p trg_ctx to use.
1619 * @return LY_RRR value.
1620 */
1621static LY_ERR
Michal Vaskoaf3df492022-12-02 14:03:52 +01001622lyd_find_schema_ctx(const struct lysc_node *schema, const struct ly_ctx *trg_ctx, const struct lyd_node *parent,
1623 ly_bool log, const struct lysc_node **trg_schema)
Michal Vaskoddd76592022-01-17 13:34:48 +01001624{
Michal Vasko9beceb82022-04-05 12:14:15 +02001625 const struct lysc_node *src_parent = NULL, *trg_parent = NULL, *sp, *tp;
1626 const struct lys_module *trg_mod = NULL;
Michal Vaskoddd76592022-01-17 13:34:48 +01001627 char *path;
1628
1629 if (!schema) {
1630 /* opaque node */
1631 *trg_schema = NULL;
1632 return LY_SUCCESS;
1633 }
1634
Michal Vasko9beceb82022-04-05 12:14:15 +02001635 if (lysc_data_parent(schema) && parent && parent->schema) {
Michal Vaskoddd76592022-01-17 13:34:48 +01001636 /* start from schema parent */
Michal Vasko9beceb82022-04-05 12:14:15 +02001637 trg_parent = parent->schema;
1638 src_parent = lysc_data_parent(schema);
1639 }
1640
1641 do {
1642 /* find the next parent */
1643 sp = schema;
Michal Vaskob6808202022-12-02 14:04:23 +01001644 while (lysc_data_parent(sp) != src_parent) {
1645 sp = lysc_data_parent(sp);
Michal Vasko9beceb82022-04-05 12:14:15 +02001646 }
1647 src_parent = sp;
1648
1649 if (!src_parent->parent) {
1650 /* find the module first */
1651 trg_mod = ly_ctx_get_module_implemented(trg_ctx, src_parent->module->name);
1652 if (!trg_mod) {
Michal Vaskoaf3df492022-12-02 14:03:52 +01001653 if (log) {
1654 LOGERR(trg_ctx, LY_ENOTFOUND, "Module \"%s\" not present/implemented in the target context.",
1655 src_parent->module->name);
1656 }
Michal Vasko9beceb82022-04-05 12:14:15 +02001657 return LY_ENOTFOUND;
1658 }
1659 }
1660
1661 /* find the next parent */
1662 assert(trg_parent || trg_mod);
1663 tp = NULL;
1664 while ((tp = lys_getnext(tp, trg_parent, trg_mod ? trg_mod->compiled : NULL, 0))) {
1665 if (!strcmp(tp->name, src_parent->name) && !strcmp(tp->module->name, src_parent->module->name)) {
1666 break;
1667 }
1668 }
1669 if (!tp) {
1670 /* schema node not found */
Michal Vaskoaf3df492022-12-02 14:03:52 +01001671 if (log) {
1672 path = lysc_path(src_parent, LYSC_PATH_LOG, NULL, 0);
1673 LOGERR(trg_ctx, LY_ENOTFOUND, "Schema node \"%s\" not found in the target context.", path);
1674 free(path);
1675 }
Michal Vaskoddd76592022-01-17 13:34:48 +01001676 return LY_ENOTFOUND;
1677 }
Michal Vaskoddd76592022-01-17 13:34:48 +01001678
Michal Vasko9beceb82022-04-05 12:14:15 +02001679 trg_parent = tp;
1680 } while (schema != src_parent);
Michal Vaskoddd76592022-01-17 13:34:48 +01001681
Michal Vasko9beceb82022-04-05 12:14:15 +02001682 /* success */
1683 *trg_schema = trg_parent;
1684 return LY_SUCCESS;
Michal Vaskoddd76592022-01-17 13:34:48 +01001685}
1686
1687/**
Michal Vasko52927e22020-03-16 17:26:14 +01001688 * @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 +02001689 *
Michal Vaskoddd76592022-01-17 13:34:48 +01001690 * 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 +02001691 *
Michal Vaskoddd76592022-01-17 13:34:48 +01001692 * @param[in] node Node to duplicate.
1693 * @param[in] trg_ctx Target context for duplicated nodes.
Radek Krejcif8b95172020-05-15 14:51:06 +02001694 * @param[in] parent Parent to insert into, NULL for top-level sibling.
Michal Vasko67177e52021-08-25 11:15:15 +02001695 * @param[in] insert_last Whether the duplicated node can be inserted as the last child of @p parent. Set for
1696 * recursive duplication as an optimization.
Radek Krejcif8b95172020-05-15 14:51:06 +02001697 * @param[in,out] first First sibling, NULL if no top-level sibling exist yet. Can be also NULL if @p parent is set.
1698 * @param[in] options Bitmask of options flags, see @ref dupoptions.
Michal Vaskoddd76592022-01-17 13:34:48 +01001699 * @param[out] dup_p Pointer where the created duplicated node is placed (besides connecting it to @p parent / @p first).
1700 * @return LY_ERR value.
Radek Krejci22ebdba2019-07-25 13:59:43 +02001701 */
Michal Vasko52927e22020-03-16 17:26:14 +01001702static LY_ERR
Michal Vaskoddd76592022-01-17 13:34:48 +01001703lyd_dup_r(const struct lyd_node *node, const struct ly_ctx *trg_ctx, struct lyd_node *parent, ly_bool insert_last,
1704 struct lyd_node **first, uint32_t options, struct lyd_node **dup_p)
Radek Krejci22ebdba2019-07-25 13:59:43 +02001705{
Michal Vasko52927e22020-03-16 17:26:14 +01001706 LY_ERR ret;
Radek Krejci22ebdba2019-07-25 13:59:43 +02001707 struct lyd_node *dup = NULL;
Michal Vasko61551fa2020-07-09 15:45:45 +02001708 struct lyd_meta *meta;
Michal Vasko9cf62422021-07-01 13:29:32 +02001709 struct lyd_attr *attr;
Michal Vasko61551fa2020-07-09 15:45:45 +02001710 struct lyd_node_any *any;
Michal Vaskoddd76592022-01-17 13:34:48 +01001711 const struct lysc_type *type;
1712 const char *val_can;
Radek Krejci22ebdba2019-07-25 13:59:43 +02001713
Michal Vasko52927e22020-03-16 17:26:14 +01001714 LY_CHECK_ARG_RET(NULL, node, LY_EINVAL);
Radek Krejci22ebdba2019-07-25 13:59:43 +02001715
Michal Vasko19175b62022-04-01 09:17:07 +02001716 if (node->flags & LYD_EXT) {
Michal Vasko53ac4dd2022-06-07 10:56:08 +02001717 if (options & LYD_DUP_NO_EXT) {
1718 /* no not duplicate this subtree */
1719 return LY_SUCCESS;
1720 }
1721
Michal Vasko19175b62022-04-01 09:17:07 +02001722 /* we need to use the same context */
1723 trg_ctx = LYD_CTX(node);
1724 }
1725
Michal Vasko52927e22020-03-16 17:26:14 +01001726 if (!node->schema) {
1727 dup = calloc(1, sizeof(struct lyd_node_opaq));
Michal Vaskoddd76592022-01-17 13:34:48 +01001728 ((struct lyd_node_opaq *)dup)->ctx = trg_ctx;
Michal Vasko52927e22020-03-16 17:26:14 +01001729 } else {
1730 switch (node->schema->nodetype) {
Michal Vasko1bf09392020-03-27 12:38:10 +01001731 case LYS_RPC:
Michal Vasko52927e22020-03-16 17:26:14 +01001732 case LYS_ACTION:
1733 case LYS_NOTIF:
1734 case LYS_CONTAINER:
1735 case LYS_LIST:
1736 dup = calloc(1, sizeof(struct lyd_node_inner));
1737 break;
1738 case LYS_LEAF:
1739 case LYS_LEAFLIST:
1740 dup = calloc(1, sizeof(struct lyd_node_term));
1741 break;
1742 case LYS_ANYDATA:
1743 case LYS_ANYXML:
1744 dup = calloc(1, sizeof(struct lyd_node_any));
1745 break;
1746 default:
Michal Vaskoddd76592022-01-17 13:34:48 +01001747 LOGINT(trg_ctx);
Michal Vasko52927e22020-03-16 17:26:14 +01001748 ret = LY_EINT;
1749 goto error;
1750 }
Radek Krejci22ebdba2019-07-25 13:59:43 +02001751 }
Michal Vaskoddd76592022-01-17 13:34:48 +01001752 LY_CHECK_ERR_GOTO(!dup, LOGMEM(trg_ctx); ret = LY_EMEM, error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02001753
Michal Vaskof6df0a02020-06-16 13:08:34 +02001754 if (options & LYD_DUP_WITH_FLAGS) {
1755 dup->flags = node->flags;
1756 } else {
Michal Vasko19175b62022-04-01 09:17:07 +02001757 dup->flags = (node->flags & (LYD_DEFAULT | LYD_EXT)) | LYD_NEW;
Michal Vaskof6df0a02020-06-16 13:08:34 +02001758 }
Igor Ryzhov9e7af662023-10-31 13:27:56 +02001759 if (options & LYD_DUP_WITH_PRIV) {
1760 dup->priv = node->priv;
1761 }
Michal Vaskoddd76592022-01-17 13:34:48 +01001762 if (trg_ctx == LYD_CTX(node)) {
1763 dup->schema = node->schema;
1764 } else {
Michal Vaskoaf3df492022-12-02 14:03:52 +01001765 ret = lyd_find_schema_ctx(node->schema, trg_ctx, parent, 1, &dup->schema);
Michal Vasko27f536f2022-04-01 09:17:30 +02001766 if (ret) {
1767 /* has no schema but is not an opaque node */
1768 free(dup);
1769 dup = NULL;
1770 goto error;
1771 }
Michal Vaskoddd76592022-01-17 13:34:48 +01001772 }
Michal Vasko52927e22020-03-16 17:26:14 +01001773 dup->prev = dup;
Radek Krejci22ebdba2019-07-25 13:59:43 +02001774
Michal Vasko9cf62422021-07-01 13:29:32 +02001775 /* duplicate metadata/attributes */
Michal Vasko25a32822020-07-09 15:48:22 +02001776 if (!(options & LYD_DUP_NO_META)) {
Michal Vasko9cf62422021-07-01 13:29:32 +02001777 if (!node->schema) {
1778 LY_LIST_FOR(((struct lyd_node_opaq *)node)->attr, attr) {
1779 LY_CHECK_GOTO(ret = lyd_dup_attr_single(attr, dup, NULL), error);
1780 }
1781 } else {
1782 LY_LIST_FOR(node->meta, meta) {
1783 LY_CHECK_GOTO(ret = lyd_dup_meta_single(meta, dup, NULL), error);
1784 }
Michal Vasko25a32822020-07-09 15:48:22 +02001785 }
1786 }
Radek Krejci22ebdba2019-07-25 13:59:43 +02001787
1788 /* nodetype-specific work */
Michal Vasko52927e22020-03-16 17:26:14 +01001789 if (!dup->schema) {
1790 struct lyd_node_opaq *opaq = (struct lyd_node_opaq *)dup;
1791 struct lyd_node_opaq *orig = (struct lyd_node_opaq *)node;
1792 struct lyd_node *child;
Radek Krejci22ebdba2019-07-25 13:59:43 +02001793
1794 if (options & LYD_DUP_RECURSIVE) {
1795 /* duplicate all the children */
1796 LY_LIST_FOR(orig->child, child) {
Michal Vaskoddd76592022-01-17 13:34:48 +01001797 LY_CHECK_GOTO(ret = lyd_dup_r(child, trg_ctx, dup, 1, NULL, options, NULL), error);
Michal Vasko52927e22020-03-16 17:26:14 +01001798 }
1799 }
Michal Vaskoddd76592022-01-17 13:34:48 +01001800 LY_CHECK_GOTO(ret = lydict_insert(trg_ctx, orig->name.name, 0, &opaq->name.name), error);
1801 LY_CHECK_GOTO(ret = lydict_insert(trg_ctx, orig->name.prefix, 0, &opaq->name.prefix), error);
1802 LY_CHECK_GOTO(ret = lydict_insert(trg_ctx, orig->name.module_ns, 0, &opaq->name.module_ns), error);
1803 LY_CHECK_GOTO(ret = lydict_insert(trg_ctx, orig->value, 0, &opaq->value), error);
Michal Vasko9cf62422021-07-01 13:29:32 +02001804 opaq->hints = orig->hints;
1805 opaq->format = orig->format;
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01001806 if (orig->val_prefix_data) {
Michal Vaskoddd76592022-01-17 13:34:48 +01001807 ret = ly_dup_prefix_data(trg_ctx, opaq->format, orig->val_prefix_data, &opaq->val_prefix_data);
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01001808 LY_CHECK_GOTO(ret, error);
Michal Vasko52927e22020-03-16 17:26:14 +01001809 }
Michal Vasko52927e22020-03-16 17:26:14 +01001810 } else if (dup->schema->nodetype & LYD_NODE_TERM) {
1811 struct lyd_node_term *term = (struct lyd_node_term *)dup;
1812 struct lyd_node_term *orig = (struct lyd_node_term *)node;
1813
1814 term->hash = orig->hash;
Michal Vaskoddd76592022-01-17 13:34:48 +01001815 if (trg_ctx == LYD_CTX(node)) {
1816 ret = orig->value.realtype->plugin->duplicate(trg_ctx, &orig->value, &term->value);
1817 LY_CHECK_ERR_GOTO(ret, LOGERR(trg_ctx, ret, "Value duplication failed."), error);
1818 } else {
1819 /* store canonical value in the target context */
1820 val_can = lyd_get_value(node);
1821 type = ((struct lysc_node_leaf *)term->schema)->type;
Michal Vasko989cdb42023-10-06 15:32:37 +02001822 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 +01001823 LYD_HINT_DATA, term->schema, NULL);
1824 LY_CHECK_GOTO(ret, error);
1825 }
Michal Vasko52927e22020-03-16 17:26:14 +01001826 } else if (dup->schema->nodetype & LYD_NODE_INNER) {
1827 struct lyd_node_inner *orig = (struct lyd_node_inner *)node;
1828 struct lyd_node *child;
1829
1830 if (options & LYD_DUP_RECURSIVE) {
1831 /* duplicate all the children */
1832 LY_LIST_FOR(orig->child, child) {
Michal Vaskoddd76592022-01-17 13:34:48 +01001833 LY_CHECK_GOTO(ret = lyd_dup_r(child, trg_ctx, dup, 1, NULL, options, NULL), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02001834 }
Michal Vasko69730152020-10-09 16:30:07 +02001835 } else if ((dup->schema->nodetype == LYS_LIST) && !(dup->schema->flags & LYS_KEYLESS)) {
Radek Krejci22ebdba2019-07-25 13:59:43 +02001836 /* always duplicate keys of a list */
Michal Vasko9beceb82022-04-05 12:14:15 +02001837 for (child = orig->child; child && lysc_is_key(child->schema); child = child->next) {
Michal Vaskoddd76592022-01-17 13:34:48 +01001838 LY_CHECK_GOTO(ret = lyd_dup_r(child, trg_ctx, dup, 1, NULL, options, NULL), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02001839 }
1840 }
1841 lyd_hash(dup);
1842 } else if (dup->schema->nodetype & LYD_NODE_ANY) {
Michal Vasko61551fa2020-07-09 15:45:45 +02001843 dup->hash = node->hash;
1844 any = (struct lyd_node_any *)node;
1845 LY_CHECK_GOTO(ret = lyd_any_copy_value(dup, &any->value, any->value_type), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02001846 }
1847
Michal Vasko52927e22020-03-16 17:26:14 +01001848 /* insert */
Michal Vasko67177e52021-08-25 11:15:15 +02001849 lyd_insert_node(parent, first, dup, insert_last);
Michal Vasko52927e22020-03-16 17:26:14 +01001850
1851 if (dup_p) {
1852 *dup_p = dup;
1853 }
1854 return LY_SUCCESS;
Radek Krejci22ebdba2019-07-25 13:59:43 +02001855
1856error:
Michal Vasko52927e22020-03-16 17:26:14 +01001857 lyd_free_tree(dup);
1858 return ret;
Radek Krejci22ebdba2019-07-25 13:59:43 +02001859}
1860
Michal Vasko29d674b2021-08-25 11:18:35 +02001861/**
1862 * @brief Get a parent node to connect duplicated subtree to.
1863 *
1864 * @param[in] node Node (subtree) to duplicate.
Michal Vaskoddd76592022-01-17 13:34:48 +01001865 * @param[in] trg_ctx Target context for duplicated nodes.
Michal Vasko29d674b2021-08-25 11:18:35 +02001866 * @param[in] parent Initial parent to connect to.
1867 * @param[in] options Bitmask of options flags, see @ref dupoptions.
1868 * @param[out] dup_parent First duplicated parent node, if any.
1869 * @param[out] local_parent Correct parent to directly connect duplicated @p node to.
1870 * @return LY_ERR value.
1871 */
Michal Vasko3a41dff2020-07-15 14:30:28 +02001872static LY_ERR
Michal Vasko58d89e92023-05-23 09:56:19 +02001873lyd_dup_get_local_parent(const struct lyd_node *node, const struct ly_ctx *trg_ctx, struct lyd_node *parent,
1874 uint32_t options, struct lyd_node **dup_parent, struct lyd_node **local_parent)
Radek Krejci22ebdba2019-07-25 13:59:43 +02001875{
Michal Vasko58d89e92023-05-23 09:56:19 +02001876 const struct lyd_node *orig_parent;
Michal Vaskoeaef7c92023-10-17 10:06:15 +02001877 struct lyd_node *iter = NULL;
Michal Vasko83522192022-07-20 08:07:34 +02001878 ly_bool repeat = 1, ext_parent = 0;
Michal Vasko3a41dff2020-07-15 14:30:28 +02001879
1880 *dup_parent = NULL;
1881 *local_parent = NULL;
1882
Michal Vasko83522192022-07-20 08:07:34 +02001883 if (node->flags & LYD_EXT) {
1884 ext_parent = 1;
1885 }
Michal Vasko58d89e92023-05-23 09:56:19 +02001886 for (orig_parent = lyd_parent(node); repeat && orig_parent; orig_parent = lyd_parent(orig_parent)) {
Michal Vasko83522192022-07-20 08:07:34 +02001887 if (ext_parent) {
1888 /* use the standard context */
1889 trg_ctx = LYD_CTX(orig_parent);
1890 }
Michal Vasko13c5b212023-02-14 10:03:01 +01001891 if (parent && (LYD_CTX(parent) == LYD_CTX(orig_parent)) && (parent->schema == orig_parent->schema)) {
Michal Vasko3a41dff2020-07-15 14:30:28 +02001892 /* stop creating parents, connect what we have into the provided parent */
1893 iter = parent;
1894 repeat = 0;
Michal Vasko13c5b212023-02-14 10:03:01 +01001895 } else if (parent && (LYD_CTX(parent) != LYD_CTX(orig_parent)) &&
1896 lyd_compare_schema_equal(parent->schema, orig_parent->schema) &&
Michal Vasko58d89e92023-05-23 09:56:19 +02001897 lyd_compare_schema_parents_equal(parent, orig_parent)) {
Michal Vasko13c5b212023-02-14 10:03:01 +01001898 iter = parent;
1899 repeat = 0;
Michal Vasko3a41dff2020-07-15 14:30:28 +02001900 } else {
1901 iter = NULL;
Michal Vasko58d89e92023-05-23 09:56:19 +02001902 LY_CHECK_RET(lyd_dup_r(orig_parent, trg_ctx, NULL, 0, &iter, options, &iter));
Michal Vaskoeaef7c92023-10-17 10:06:15 +02001903
1904 /* insert into the previous duplicated parent */
1905 if (*dup_parent) {
1906 lyd_insert_node(iter, NULL, *dup_parent, 0);
1907 }
1908
1909 /* update the last duplicated parent */
1910 *dup_parent = iter;
Michal Vasko3a41dff2020-07-15 14:30:28 +02001911 }
Michal Vasko58d89e92023-05-23 09:56:19 +02001912
Michal Vaskoeaef7c92023-10-17 10:06:15 +02001913 /* set the first parent */
Michal Vasko3a41dff2020-07-15 14:30:28 +02001914 if (!*local_parent) {
Michal Vasko58d89e92023-05-23 09:56:19 +02001915 *local_parent = iter;
Michal Vasko3a41dff2020-07-15 14:30:28 +02001916 }
Michal Vasko58d89e92023-05-23 09:56:19 +02001917
Michal Vasko83522192022-07-20 08:07:34 +02001918 if (orig_parent->flags & LYD_EXT) {
1919 ext_parent = 1;
1920 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02001921 }
1922
1923 if (repeat && parent) {
1924 /* given parent and created parents chain actually do not interconnect */
Michal Vasko58d89e92023-05-23 09:56:19 +02001925 LOGERR(trg_ctx, LY_EINVAL, "None of the duplicated node \"%s\" schema parents match the provided parent \"%s\".",
1926 LYD_NAME(node), LYD_NAME(parent));
Michal Vasko3a41dff2020-07-15 14:30:28 +02001927 return LY_EINVAL;
1928 }
1929
Michal Vaskoeaef7c92023-10-17 10:06:15 +02001930 if (*dup_parent && parent) {
1931 /* last insert into a prevously-existing parent */
1932 lyd_insert_node(parent, NULL, *dup_parent, 0);
1933 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02001934 return LY_SUCCESS;
1935}
1936
1937static LY_ERR
Michal Vasko58d89e92023-05-23 09:56:19 +02001938lyd_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 +01001939 ly_bool nosiblings, struct lyd_node **dup)
Michal Vasko3a41dff2020-07-15 14:30:28 +02001940{
1941 LY_ERR rc;
Radek Krejci22ebdba2019-07-25 13:59:43 +02001942 const struct lyd_node *orig; /* original node to be duplicated */
1943 struct lyd_node *first = NULL; /* the first duplicated node, this is returned */
Radek Krejci22ebdba2019-07-25 13:59:43 +02001944 struct lyd_node *top = NULL; /* the most higher created node */
Michal Vasko58d89e92023-05-23 09:56:19 +02001945 struct lyd_node *local_parent = NULL; /* the direct parent node for the duplicated node(s) */
Radek Krejci22ebdba2019-07-25 13:59:43 +02001946
Michal Vasko1feb9bb2022-07-20 08:44:07 +02001947 assert(node && trg_ctx);
Radek Krejci22ebdba2019-07-25 13:59:43 +02001948
1949 if (options & LYD_DUP_WITH_PARENTS) {
Michal Vaskoddd76592022-01-17 13:34:48 +01001950 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 +02001951 &top, &local_parent), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02001952 } else {
1953 local_parent = parent;
1954 }
1955
Radek Krejci22ebdba2019-07-25 13:59:43 +02001956 LY_LIST_FOR(node, orig) {
Michal Vasko35f4d772021-01-12 12:08:57 +01001957 if (lysc_is_key(orig->schema)) {
1958 if (local_parent) {
1959 /* the key must already exist in the parent */
Michal Vasko58d89e92023-05-23 09:56:19 +02001960 rc = lyd_find_sibling_schema(lyd_child(local_parent), orig->schema, first ? NULL : &first);
Michal Vaskoddd76592022-01-17 13:34:48 +01001961 LY_CHECK_ERR_GOTO(rc, LOGINT(trg_ctx), error);
Michal Vasko35f4d772021-01-12 12:08:57 +01001962 } else {
1963 assert(!(options & LYD_DUP_WITH_PARENTS));
1964 /* duplicating a single key, okay, I suppose... */
Michal Vaskoddd76592022-01-17 13:34:48 +01001965 rc = lyd_dup_r(orig, trg_ctx, NULL, 0, &first, options, first ? NULL : &first);
Michal Vasko35f4d772021-01-12 12:08:57 +01001966 LY_CHECK_GOTO(rc, error);
1967 }
1968 } else {
1969 /* if there is no local parent, it will be inserted into first */
Michal Vasko58d89e92023-05-23 09:56:19 +02001970 rc = lyd_dup_r(orig, trg_ctx, local_parent, 0, &first, options, first ? NULL : &first);
Michal Vasko35f4d772021-01-12 12:08:57 +01001971 LY_CHECK_GOTO(rc, error);
1972 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02001973 if (nosiblings) {
Radek Krejci22ebdba2019-07-25 13:59:43 +02001974 break;
1975 }
1976 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02001977
Michal Vasko3a41dff2020-07-15 14:30:28 +02001978 if (dup) {
1979 *dup = first;
1980 }
1981 return LY_SUCCESS;
Radek Krejci22ebdba2019-07-25 13:59:43 +02001982
1983error:
1984 if (top) {
1985 lyd_free_tree(top);
1986 } else {
Michal Vaskof03ed032020-03-04 13:31:44 +01001987 lyd_free_siblings(first);
Radek Krejci22ebdba2019-07-25 13:59:43 +02001988 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02001989 return rc;
Radek Krejci22ebdba2019-07-25 13:59:43 +02001990}
Michal Vasko5ec7cda2019-09-11 13:43:08 +02001991
Michal Vasko1feb9bb2022-07-20 08:44:07 +02001992/**
1993 * @brief Check the context of node and parent when duplicating nodes.
1994 *
1995 * @param[in] node Node to duplicate.
1996 * @param[in] parent Parent of the duplicated node(s).
1997 * @return LY_ERR value.
1998 */
1999static LY_ERR
2000lyd_dup_ctx_check(const struct lyd_node *node, const struct lyd_node_inner *parent)
2001{
2002 const struct lyd_node *iter;
2003
2004 if (!node || !parent) {
2005 return LY_SUCCESS;
2006 }
2007
2008 if ((LYD_CTX(node) != LYD_CTX(parent))) {
2009 /* try to find top-level ext data parent */
2010 for (iter = node; iter && !(iter->flags & LYD_EXT); iter = lyd_parent(iter)) {}
2011
2012 if (!iter || !lyd_parent(iter) || (LYD_CTX(lyd_parent(iter)) != LYD_CTX(parent))) {
Michal Vaskoce55b462023-02-14 10:03:32 +01002013 LOGERR(LYD_CTX(node), LY_EINVAL, "Different contexts used in node duplication.");
Michal Vasko1feb9bb2022-07-20 08:44:07 +02002014 return LY_EINVAL;
2015 }
2016 }
2017
2018 return LY_SUCCESS;
2019}
2020
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002021LIBYANG_API_DEF LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02002022lyd_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 +02002023{
Michal Vaskoddd76592022-01-17 13:34:48 +01002024 LY_CHECK_ARG_RET(NULL, node, LY_EINVAL);
Michal Vasko1feb9bb2022-07-20 08:44:07 +02002025 LY_CHECK_RET(lyd_dup_ctx_check(node, parent));
Michal Vaskoddd76592022-01-17 13:34:48 +01002026
Michal Vasko58d89e92023-05-23 09:56:19 +02002027 return lyd_dup(node, LYD_CTX(node), (struct lyd_node *)parent, options, 1, dup);
Michal Vaskoddd76592022-01-17 13:34:48 +01002028}
2029
2030LIBYANG_API_DEF LY_ERR
2031lyd_dup_single_to_ctx(const struct lyd_node *node, const struct ly_ctx *trg_ctx, struct lyd_node_inner *parent,
2032 uint32_t options, struct lyd_node **dup)
2033{
2034 LY_CHECK_ARG_RET(trg_ctx, node, trg_ctx, LY_EINVAL);
2035
Michal Vasko58d89e92023-05-23 09:56:19 +02002036 return lyd_dup(node, trg_ctx, (struct lyd_node *)parent, options, 1, dup);
Michal Vasko3a41dff2020-07-15 14:30:28 +02002037}
2038
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002039LIBYANG_API_DEF LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02002040lyd_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 +02002041{
Michal Vaskoddd76592022-01-17 13:34:48 +01002042 LY_CHECK_ARG_RET(NULL, node, LY_EINVAL);
Michal Vasko1feb9bb2022-07-20 08:44:07 +02002043 LY_CHECK_RET(lyd_dup_ctx_check(node, parent));
Michal Vaskoddd76592022-01-17 13:34:48 +01002044
Michal Vasko58d89e92023-05-23 09:56:19 +02002045 return lyd_dup(node, LYD_CTX(node), (struct lyd_node *)parent, options, 0, dup);
Michal Vaskoddd76592022-01-17 13:34:48 +01002046}
2047
2048LIBYANG_API_DEF LY_ERR
2049lyd_dup_siblings_to_ctx(const struct lyd_node *node, const struct ly_ctx *trg_ctx, struct lyd_node_inner *parent,
2050 uint32_t options, struct lyd_node **dup)
2051{
2052 LY_CHECK_ARG_RET(trg_ctx, node, trg_ctx, LY_EINVAL);
2053
Michal Vasko58d89e92023-05-23 09:56:19 +02002054 return lyd_dup(node, trg_ctx, (struct lyd_node *)parent, options, 0, dup);
Michal Vasko3a41dff2020-07-15 14:30:28 +02002055}
2056
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002057LIBYANG_API_DEF LY_ERR
Michal Vasko3a41dff2020-07-15 14:30:28 +02002058lyd_dup_meta_single(const struct lyd_meta *meta, struct lyd_node *node, struct lyd_meta **dup)
Michal Vasko25a32822020-07-09 15:48:22 +02002059{
Radek Krejci011e4aa2020-09-04 15:22:31 +02002060 LY_ERR ret = LY_SUCCESS;
Michal Vasko33c48972022-07-20 10:28:07 +02002061 const struct ly_ctx *ctx;
Michal Vasko25a32822020-07-09 15:48:22 +02002062 struct lyd_meta *mt, *last;
2063
Michal Vasko3a41dff2020-07-15 14:30:28 +02002064 LY_CHECK_ARG_RET(NULL, meta, node, LY_EINVAL);
Michal Vasko25a32822020-07-09 15:48:22 +02002065
Michal Vasko33c48972022-07-20 10:28:07 +02002066 /* log to node context but value must always use the annotation context */
2067 ctx = meta->annotation->module->ctx;
2068
Michal Vasko25a32822020-07-09 15:48:22 +02002069 /* create a copy */
2070 mt = calloc(1, sizeof *mt);
Michal Vaskob7be7a82020-08-20 09:09:04 +02002071 LY_CHECK_ERR_RET(!mt, LOGMEM(LYD_CTX(node)), LY_EMEM);
Michal Vasko25a32822020-07-09 15:48:22 +02002072 mt->annotation = meta->annotation;
Michal Vasko33c48972022-07-20 10:28:07 +02002073 ret = meta->value.realtype->plugin->duplicate(ctx, &meta->value, &mt->value);
Radek Krejci011e4aa2020-09-04 15:22:31 +02002074 LY_CHECK_ERR_GOTO(ret, LOGERR(LYD_CTX(node), LY_EINT, "Value duplication failed."), finish);
Michal Vasko33c48972022-07-20 10:28:07 +02002075 LY_CHECK_GOTO(ret = lydict_insert(ctx, meta->name, 0, &mt->name), finish);
Michal Vasko25a32822020-07-09 15:48:22 +02002076
2077 /* insert as the last attribute */
Radek Krejci011e4aa2020-09-04 15:22:31 +02002078 mt->parent = node;
Michal Vasko25a32822020-07-09 15:48:22 +02002079 if (node->meta) {
Radek Krejci1e008d22020-08-17 11:37:37 +02002080 for (last = node->meta; last->next; last = last->next) {}
Michal Vasko25a32822020-07-09 15:48:22 +02002081 last->next = mt;
2082 } else {
2083 node->meta = mt;
2084 }
2085
Radek Krejci011e4aa2020-09-04 15:22:31 +02002086finish:
2087 if (ret) {
2088 lyd_free_meta_single(mt);
2089 } else if (dup) {
Michal Vasko3a41dff2020-07-15 14:30:28 +02002090 *dup = mt;
2091 }
2092 return LY_SUCCESS;
Michal Vasko25a32822020-07-09 15:48:22 +02002093}
2094
Michal Vasko4490d312020-06-16 13:08:55 +02002095/**
2096 * @brief Merge a source sibling into target siblings.
2097 *
2098 * @param[in,out] first_trg First target sibling, is updated if top-level.
2099 * @param[in] parent_trg Target parent.
2100 * @param[in,out] sibling_src Source sibling to merge, set to NULL if spent.
Michal Vaskocd3f6172021-05-18 16:14:50 +02002101 * @param[in] merge_cb Optional merge callback.
2102 * @param[in] cb_data Arbitrary callback data.
Michal Vasko4490d312020-06-16 13:08:55 +02002103 * @param[in] options Merge options.
Michal Vaskocd3f6172021-05-18 16:14:50 +02002104 * @param[in,out] dup_inst Duplicate instance cache for all @p first_trg siblings.
Michal Vasko4490d312020-06-16 13:08:55 +02002105 * @return LY_ERR value.
2106 */
2107static LY_ERR
2108lyd_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 +02002109 lyd_merge_cb merge_cb, void *cb_data, uint16_t options, struct ly_ht **dup_inst)
Michal Vasko4490d312020-06-16 13:08:55 +02002110{
Michal Vasko4490d312020-06-16 13:08:55 +02002111 const struct lyd_node *child_src, *tmp, *sibling_src;
Michal Vasko56daf732020-08-10 10:57:18 +02002112 struct lyd_node *match_trg, *dup_src, *elem;
Michal Vaskod1afa502022-01-27 16:18:12 +01002113 struct lyd_node_opaq *opaq_trg, *opaq_src;
Michal Vasko4490d312020-06-16 13:08:55 +02002114 struct lysc_type *type;
Michal Vasko8efac242023-03-30 08:24:56 +02002115 struct ly_ht *child_dup_inst = NULL;
Michal Vaskocd3f6172021-05-18 16:14:50 +02002116 LY_ERR ret;
2117 ly_bool first_inst = 0;
Michal Vasko4490d312020-06-16 13:08:55 +02002118
2119 sibling_src = *sibling_src_p;
Michal Vaskocd3f6172021-05-18 16:14:50 +02002120 if (!sibling_src->schema) {
2121 /* try to find the same opaque node */
2122 lyd_find_sibling_opaq_next(*first_trg, LYD_NAME(sibling_src), &match_trg);
2123 } else if (sibling_src->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) {
Michal Vasko4490d312020-06-16 13:08:55 +02002124 /* try to find the exact instance */
Michal Vaskocd3f6172021-05-18 16:14:50 +02002125 lyd_find_sibling_first(*first_trg, sibling_src, &match_trg);
Michal Vasko4490d312020-06-16 13:08:55 +02002126 } else {
2127 /* try to simply find the node, there cannot be more instances */
Michal Vaskocd3f6172021-05-18 16:14:50 +02002128 lyd_find_sibling_val(*first_trg, sibling_src->schema, NULL, 0, &match_trg);
Michal Vasko4490d312020-06-16 13:08:55 +02002129 }
2130
Michal Vaskocd3f6172021-05-18 16:14:50 +02002131 if (match_trg) {
2132 /* update match as needed */
2133 LY_CHECK_RET(lyd_dup_inst_next(&match_trg, *first_trg, dup_inst));
2134 } else {
2135 /* first instance of this node */
2136 first_inst = 1;
2137 }
2138
2139 if (match_trg) {
2140 /* call callback */
2141 if (merge_cb) {
2142 LY_CHECK_RET(merge_cb(match_trg, sibling_src, cb_data));
2143 }
2144
Michal Vasko4490d312020-06-16 13:08:55 +02002145 /* node found, make sure even value matches for all node types */
Michal Vaskod1afa502022-01-27 16:18:12 +01002146 if (!match_trg->schema) {
2147 if (lyd_compare_single(sibling_src, match_trg, 0)) {
2148 /* update value */
2149 opaq_trg = (struct lyd_node_opaq *)match_trg;
2150 opaq_src = (struct lyd_node_opaq *)sibling_src;
2151
2152 lydict_remove(LYD_CTX(opaq_trg), opaq_trg->value);
2153 lydict_insert(LYD_CTX(opaq_trg), opaq_src->value, 0, &opaq_trg->value);
2154 opaq_trg->hints = opaq_src->hints;
2155
2156 ly_free_prefix_data(opaq_trg->format, opaq_trg->val_prefix_data);
2157 opaq_trg->format = opaq_src->format;
2158 ly_dup_prefix_data(LYD_CTX(opaq_trg), opaq_src->format, opaq_src->val_prefix_data,
2159 &opaq_trg->val_prefix_data);
2160 }
2161 } else if ((match_trg->schema->nodetype == LYS_LEAF) &&
2162 lyd_compare_single(sibling_src, match_trg, LYD_COMPARE_DEFAULTS)) {
Michal Vasko4490d312020-06-16 13:08:55 +02002163 /* since they are different, they cannot both be default */
2164 assert(!(sibling_src->flags & LYD_DEFAULT) || !(match_trg->flags & LYD_DEFAULT));
2165
Michal Vasko3a41dff2020-07-15 14:30:28 +02002166 /* update value (or only LYD_DEFAULT flag) only if flag set or the source node is not default */
2167 if ((options & LYD_MERGE_DEFAULTS) || !(sibling_src->flags & LYD_DEFAULT)) {
Michal Vasko4490d312020-06-16 13:08:55 +02002168 type = ((struct lysc_node_leaf *)match_trg->schema)->type;
Michal Vaskob7be7a82020-08-20 09:09:04 +02002169 type->plugin->free(LYD_CTX(match_trg), &((struct lyd_node_term *)match_trg)->value);
2170 LY_CHECK_RET(type->plugin->duplicate(LYD_CTX(match_trg), &((struct lyd_node_term *)sibling_src)->value,
Michal Vasko69730152020-10-09 16:30:07 +02002171 &((struct lyd_node_term *)match_trg)->value));
Michal Vasko4490d312020-06-16 13:08:55 +02002172
2173 /* copy flags and add LYD_NEW */
Michal Vasko7e8315b2021-08-03 17:01:06 +02002174 match_trg->flags = sibling_src->flags | ((options & LYD_MERGE_WITH_FLAGS) ? 0 : LYD_NEW);
Michal Vasko4490d312020-06-16 13:08:55 +02002175 }
Michal Vasko8f359bf2020-07-28 10:41:15 +02002176 } else if ((match_trg->schema->nodetype & LYS_ANYDATA) && lyd_compare_single(sibling_src, match_trg, 0)) {
Michal Vasko4c583e82020-07-17 12:16:14 +02002177 /* update value */
2178 LY_CHECK_RET(lyd_any_copy_value(match_trg, &((struct lyd_node_any *)sibling_src)->value,
Radek Krejci0f969882020-08-21 16:56:47 +02002179 ((struct lyd_node_any *)sibling_src)->value_type));
Michal Vasko4490d312020-06-16 13:08:55 +02002180
2181 /* copy flags and add LYD_NEW */
Michal Vasko7e8315b2021-08-03 17:01:06 +02002182 match_trg->flags = sibling_src->flags | ((options & LYD_MERGE_WITH_FLAGS) ? 0 : LYD_NEW);
Michal Vaskocd3f6172021-05-18 16:14:50 +02002183 }
2184
2185 /* check descendants, recursively */
2186 ret = LY_SUCCESS;
2187 LY_LIST_FOR_SAFE(lyd_child_no_keys(sibling_src), tmp, child_src) {
2188 ret = lyd_merge_sibling_r(lyd_node_child_p(match_trg), match_trg, &child_src, merge_cb, cb_data, options,
2189 &child_dup_inst);
2190 if (ret) {
2191 break;
Michal Vasko4490d312020-06-16 13:08:55 +02002192 }
2193 }
Michal Vaskocd3f6172021-05-18 16:14:50 +02002194 lyd_dup_inst_free(child_dup_inst);
2195 LY_CHECK_RET(ret);
Michal Vasko4490d312020-06-16 13:08:55 +02002196 } else {
2197 /* node not found, merge it */
2198 if (options & LYD_MERGE_DESTRUCT) {
2199 dup_src = (struct lyd_node *)sibling_src;
Michal Vasko2e784f82024-01-11 09:51:22 +01002200 lyd_unlink(dup_src);
Michal Vasko4490d312020-06-16 13:08:55 +02002201 /* spend it */
2202 *sibling_src_p = NULL;
2203 } else {
Michal Vasko3a41dff2020-07-15 14:30:28 +02002204 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 +02002205 }
2206
Michal Vasko7e8315b2021-08-03 17:01:06 +02002207 if (!(options & LYD_MERGE_WITH_FLAGS)) {
2208 /* set LYD_NEW for all the new nodes, required for validation */
2209 LYD_TREE_DFS_BEGIN(dup_src, elem) {
2210 elem->flags |= LYD_NEW;
2211 LYD_TREE_DFS_END(dup_src, elem);
2212 }
Michal Vasko4490d312020-06-16 13:08:55 +02002213 }
2214
Michal Vaskocd3f6172021-05-18 16:14:50 +02002215 /* insert */
Michal Vasko6ee6f432021-07-16 09:49:14 +02002216 lyd_insert_node(parent_trg, first_trg, dup_src, 0);
Michal Vaskocd3f6172021-05-18 16:14:50 +02002217
2218 if (first_inst) {
2219 /* remember not to find this instance next time */
2220 LY_CHECK_RET(lyd_dup_inst_next(&dup_src, *first_trg, dup_inst));
2221 }
2222
2223 /* call callback, no source node */
2224 if (merge_cb) {
2225 LY_CHECK_RET(merge_cb(dup_src, NULL, cb_data));
2226 }
Michal Vasko4490d312020-06-16 13:08:55 +02002227 }
2228
2229 return LY_SUCCESS;
2230}
2231
Michal Vasko3a41dff2020-07-15 14:30:28 +02002232static LY_ERR
Michal Vaskocd3f6172021-05-18 16:14:50 +02002233lyd_merge(struct lyd_node **target, const struct lyd_node *source, const struct lys_module *mod,
2234 lyd_merge_cb merge_cb, void *cb_data, uint16_t options, ly_bool nosiblings)
Michal Vasko4490d312020-06-16 13:08:55 +02002235{
2236 const struct lyd_node *sibling_src, *tmp;
Michal Vasko8efac242023-03-30 08:24:56 +02002237 struct ly_ht *dup_inst = NULL;
Radek Krejci857189e2020-09-01 13:26:36 +02002238 ly_bool first;
Michal Vaskocd3f6172021-05-18 16:14:50 +02002239 LY_ERR ret = LY_SUCCESS;
Michal Vasko4490d312020-06-16 13:08:55 +02002240
2241 LY_CHECK_ARG_RET(NULL, target, LY_EINVAL);
Michal Vasko892f5bf2021-11-24 10:41:05 +01002242 LY_CHECK_CTX_EQUAL_RET(*target ? LYD_CTX(*target) : NULL, source ? LYD_CTX(source) : NULL, mod ? mod->ctx : NULL,
2243 LY_EINVAL);
Michal Vasko4490d312020-06-16 13:08:55 +02002244
2245 if (!source) {
2246 /* nothing to merge */
2247 return LY_SUCCESS;
2248 }
2249
Michal Vasko831422b2020-11-24 11:20:51 +01002250 if ((*target && lysc_data_parent((*target)->schema)) || lysc_data_parent(source->schema)) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02002251 LOGERR(LYD_CTX(source), LY_EINVAL, "Invalid arguments - can merge only 2 top-level subtrees (%s()).", __func__);
Michal Vasko4490d312020-06-16 13:08:55 +02002252 return LY_EINVAL;
2253 }
2254
2255 LY_LIST_FOR_SAFE(source, tmp, sibling_src) {
Michal Vaskocd3f6172021-05-18 16:14:50 +02002256 if (mod && (lyd_owner_module(sibling_src) != mod)) {
2257 /* skip data nodes from different modules */
2258 continue;
2259 }
2260
Radek Krejci857189e2020-09-01 13:26:36 +02002261 first = (sibling_src == source) ? 1 : 0;
Michal Vaskocd3f6172021-05-18 16:14:50 +02002262 ret = lyd_merge_sibling_r(target, NULL, &sibling_src, merge_cb, cb_data, options, &dup_inst);
2263 if (ret) {
2264 break;
2265 }
Michal Vasko4490d312020-06-16 13:08:55 +02002266 if (first && !sibling_src) {
2267 /* source was spent (unlinked), move to the next node */
2268 source = tmp;
2269 }
2270
Michal Vasko3a41dff2020-07-15 14:30:28 +02002271 if (nosiblings) {
Michal Vasko4490d312020-06-16 13:08:55 +02002272 break;
2273 }
2274 }
2275
2276 if (options & LYD_MERGE_DESTRUCT) {
2277 /* free any leftover source data that were not merged */
2278 lyd_free_siblings((struct lyd_node *)source);
2279 }
2280
Michal Vaskocd3f6172021-05-18 16:14:50 +02002281 lyd_dup_inst_free(dup_inst);
2282 return ret;
Michal Vasko4490d312020-06-16 13:08:55 +02002283}
2284
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002285LIBYANG_API_DEF LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02002286lyd_merge_tree(struct lyd_node **target, const struct lyd_node *source, uint16_t options)
Michal Vasko3a41dff2020-07-15 14:30:28 +02002287{
Michal Vaskocd3f6172021-05-18 16:14:50 +02002288 return lyd_merge(target, source, NULL, NULL, NULL, options, 1);
Michal Vasko3a41dff2020-07-15 14:30:28 +02002289}
2290
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002291LIBYANG_API_DEF LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02002292lyd_merge_siblings(struct lyd_node **target, const struct lyd_node *source, uint16_t options)
Michal Vasko3a41dff2020-07-15 14:30:28 +02002293{
Michal Vaskocd3f6172021-05-18 16:14:50 +02002294 return lyd_merge(target, source, NULL, NULL, NULL, options, 0);
2295}
2296
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002297LIBYANG_API_DEF LY_ERR
Michal Vaskocd3f6172021-05-18 16:14:50 +02002298lyd_merge_module(struct lyd_node **target, const struct lyd_node *source, const struct lys_module *mod,
2299 lyd_merge_cb merge_cb, void *cb_data, uint16_t options)
2300{
2301 return lyd_merge(target, source, mod, merge_cb, cb_data, options, 0);
Michal Vasko3a41dff2020-07-15 14:30:28 +02002302}
2303
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002304static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02002305lyd_path_str_enlarge(char **buffer, size_t *buflen, size_t reqlen, ly_bool is_static)
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002306{
Michal Vasko14654712020-02-06 08:35:21 +01002307 /* ending \0 */
2308 ++reqlen;
2309
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002310 if (reqlen > *buflen) {
2311 if (is_static) {
2312 return LY_EINCOMPLETE;
2313 }
2314
2315 *buffer = ly_realloc(*buffer, reqlen * sizeof **buffer);
2316 if (!*buffer) {
2317 return LY_EMEM;
2318 }
2319
2320 *buflen = reqlen;
2321 }
2322
2323 return LY_SUCCESS;
2324}
2325
Michal Vaskod59035b2020-07-08 12:00:06 +02002326LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02002327lyd_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 +02002328{
2329 const struct lyd_node *key;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002330 size_t len;
2331 const char *val;
2332 char quot;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002333
Michal Vasko824d0832021-11-04 15:36:51 +01002334 for (key = lyd_child(node); key && key->schema && (key->schema->flags & LYS_KEY); key = key->next) {
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02002335 val = lyd_get_value(key);
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002336 len = 1 + strlen(key->schema->name) + 2 + strlen(val) + 2;
Michal Vaskoba99a3e2020-08-18 15:50:05 +02002337 LY_CHECK_RET(lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static));
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002338
2339 quot = '\'';
2340 if (strchr(val, '\'')) {
2341 quot = '"';
2342 }
2343 *bufused += sprintf(*buffer + *bufused, "[%s=%c%s%c]", key->schema->name, quot, val, quot);
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002344 }
2345
2346 return LY_SUCCESS;
2347}
2348
2349/**
2350 * @brief Append leaf-list value predicate to path.
2351 *
2352 * @param[in] node Node to print.
2353 * @param[in,out] buffer Buffer to print to.
2354 * @param[in,out] buflen Current buffer length.
2355 * @param[in,out] bufused Current number of characters used in @p buffer.
2356 * @param[in] is_static Whether buffer is static or can be reallocated.
2357 * @return LY_ERR
2358 */
2359static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02002360lyd_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 +02002361{
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002362 size_t len;
2363 const char *val;
2364 char quot;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002365
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02002366 val = lyd_get_value(node);
Radek Krejcif13b87b2020-12-01 22:02:17 +01002367 len = 4 + strlen(val) + 2; /* "[.='" + val + "']" */
Michal Vaskoba99a3e2020-08-18 15:50:05 +02002368 LY_CHECK_RET(lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static));
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002369
2370 quot = '\'';
2371 if (strchr(val, '\'')) {
2372 quot = '"';
2373 }
2374 *bufused += sprintf(*buffer + *bufused, "[.=%c%s%c]", quot, val, quot);
2375
Michal Vaskoba99a3e2020-08-18 15:50:05 +02002376 return LY_SUCCESS;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002377}
2378
2379/**
2380 * @brief Append node position (relative to its other instances) predicate to path.
2381 *
2382 * @param[in] node Node to print.
2383 * @param[in,out] buffer Buffer to print to.
2384 * @param[in,out] buflen Current buffer length.
2385 * @param[in,out] bufused Current number of characters used in @p buffer.
2386 * @param[in] is_static Whether buffer is static or can be reallocated.
2387 * @return LY_ERR
2388 */
2389static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02002390lyd_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 +02002391{
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002392 size_t len;
Michal Vasko50cc0562021-05-18 16:15:43 +02002393 uint32_t pos;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002394 char *val = NULL;
2395 LY_ERR rc;
2396
Michal Vasko50cc0562021-05-18 16:15:43 +02002397 pos = lyd_list_pos(node);
2398 if (asprintf(&val, "%" PRIu32, pos) == -1) {
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002399 return LY_EMEM;
2400 }
2401
2402 len = 1 + strlen(val) + 1;
2403 rc = lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static);
2404 if (rc != LY_SUCCESS) {
2405 goto cleanup;
2406 }
2407
2408 *bufused += sprintf(*buffer + *bufused, "[%s]", val);
2409
2410cleanup:
2411 free(val);
2412 return rc;
2413}
2414
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002415LIBYANG_API_DEF char *
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002416lyd_path(const struct lyd_node *node, LYD_PATH_TYPE pathtype, char *buffer, size_t buflen)
2417{
Radek Krejci857189e2020-09-01 13:26:36 +02002418 ly_bool is_static = 0;
Radek Krejci1deb5be2020-08-26 16:43:36 +02002419 uint32_t i, depth;
Michal Vasko14654712020-02-06 08:35:21 +01002420 size_t bufused = 0, len;
Michal Vasko12eb6222022-03-18 13:35:49 +01002421 const struct lyd_node *iter, *parent;
2422 const struct lys_module *mod, *prev_mod;
Michal Vasko790b2bc2020-08-03 13:35:06 +02002423 LY_ERR rc = LY_SUCCESS;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002424
2425 LY_CHECK_ARG_RET(NULL, node, NULL);
2426 if (buffer) {
Michal Vasko16385f42021-05-18 16:16:09 +02002427 LY_CHECK_ARG_RET(LYD_CTX(node), buflen > 1, NULL);
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002428 is_static = 1;
Michal Vasko14654712020-02-06 08:35:21 +01002429 } else {
2430 buflen = 0;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002431 }
2432
2433 switch (pathtype) {
Radek Krejci635d2b82021-01-04 11:26:51 +01002434 case LYD_PATH_STD:
2435 case LYD_PATH_STD_NO_LAST_PRED:
Michal Vasko14654712020-02-06 08:35:21 +01002436 depth = 1;
Michal Vasko9e685082021-01-29 14:49:09 +01002437 for (iter = node; iter->parent; iter = lyd_parent(iter)) {
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002438 ++depth;
2439 }
2440
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002441 goto iter_print;
Michal Vasko14654712020-02-06 08:35:21 +01002442 while (depth) {
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002443 /* find the right node */
Michal Vasko9e685082021-01-29 14:49:09 +01002444 for (iter = node, i = 1; i < depth; iter = lyd_parent(iter), ++i) {}
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002445iter_print:
Michal Vasko12eb6222022-03-18 13:35:49 +01002446 /* get the module */
Michal Vasko420cc252023-08-24 08:14:24 +02002447 mod = lyd_node_module(iter);
Michal Vasko12eb6222022-03-18 13:35:49 +01002448 parent = lyd_parent(iter);
Michal Vasko420cc252023-08-24 08:14:24 +02002449 prev_mod = lyd_node_module(parent);
Michal Vasko12eb6222022-03-18 13:35:49 +01002450 if (prev_mod == mod) {
2451 mod = NULL;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002452 }
2453
2454 /* realloc string */
Michal Vaskoad92b672020-11-12 13:11:31 +01002455 len = 1 + (mod ? strlen(mod->name) + 1 : 0) + (iter->schema ? strlen(iter->schema->name) :
2456 strlen(((struct lyd_node_opaq *)iter)->name.name));
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002457 rc = lyd_path_str_enlarge(&buffer, &buflen, bufused + len, is_static);
2458 if (rc != LY_SUCCESS) {
2459 break;
2460 }
2461
2462 /* print next node */
Michal Vaskodbf3e652022-10-21 08:46:25 +02002463 bufused += sprintf(buffer + bufused, "/%s%s%s", mod ? mod->name : "", mod ? ":" : "", LYD_NAME(iter));
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002464
Michal Vasko790b2bc2020-08-03 13:35:06 +02002465 /* do not always print the last (first) predicate */
Radek Krejci635d2b82021-01-04 11:26:51 +01002466 if (iter->schema && ((depth > 1) || (pathtype == LYD_PATH_STD))) {
Michal Vasko790b2bc2020-08-03 13:35:06 +02002467 switch (iter->schema->nodetype) {
2468 case LYS_LIST:
2469 if (iter->schema->flags & LYS_KEYLESS) {
2470 /* print its position */
2471 rc = lyd_path_position_predicate(iter, &buffer, &buflen, &bufused, is_static);
2472 } else {
2473 /* print all list keys in predicates */
2474 rc = lyd_path_list_predicate(iter, &buffer, &buflen, &bufused, is_static);
2475 }
2476 break;
2477 case LYS_LEAFLIST:
2478 if (iter->schema->flags & LYS_CONFIG_W) {
2479 /* print leaf-list value */
2480 rc = lyd_path_leaflist_predicate(iter, &buffer, &buflen, &bufused, is_static);
2481 } else {
2482 /* print its position */
2483 rc = lyd_path_position_predicate(iter, &buffer, &buflen, &bufused, is_static);
2484 }
2485 break;
2486 default:
2487 /* nothing to print more */
2488 break;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002489 }
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002490 }
2491 if (rc != LY_SUCCESS) {
2492 break;
2493 }
2494
Michal Vasko14654712020-02-06 08:35:21 +01002495 --depth;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002496 }
2497 break;
2498 }
2499
2500 return buffer;
2501}
Michal Vaskoe444f752020-02-10 12:20:06 +01002502
Michal Vaskodbf3e652022-10-21 08:46:25 +02002503char *
2504lyd_path_set(const struct ly_set *dnodes, LYD_PATH_TYPE pathtype)
2505{
2506 uint32_t depth;
2507 size_t bufused = 0, buflen = 0, len;
2508 char *buffer = NULL;
2509 const struct lyd_node *iter, *parent;
2510 const struct lys_module *mod, *prev_mod;
2511 LY_ERR rc = LY_SUCCESS;
2512
2513 switch (pathtype) {
2514 case LYD_PATH_STD:
2515 case LYD_PATH_STD_NO_LAST_PRED:
2516 for (depth = 1; depth <= dnodes->count; ++depth) {
2517 /* current node */
2518 iter = dnodes->dnodes[depth - 1];
Michal Vasko420cc252023-08-24 08:14:24 +02002519 mod = lyd_node_module(iter);
Michal Vaskodbf3e652022-10-21 08:46:25 +02002520
2521 /* parent */
2522 parent = (depth > 1) ? dnodes->dnodes[depth - 2] : NULL;
Michal Vasko85be65e2023-06-13 09:44:17 +02002523 assert(!parent || !iter->schema || !parent->schema || (parent->schema->nodetype & LYD_NODE_ANY) ||
2524 (lysc_data_parent(iter->schema) == parent->schema) ||
Michal Vasko539d6a92023-09-11 10:31:12 +02002525 (!lysc_data_parent(iter->schema) && (LYD_CTX(iter) != LYD_CTX(parent))) ||
2526 (parent->schema->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)));
Michal Vaskodbf3e652022-10-21 08:46:25 +02002527
2528 /* get module to print, if any */
Michal Vasko420cc252023-08-24 08:14:24 +02002529 prev_mod = lyd_node_module(parent);
Michal Vaskodbf3e652022-10-21 08:46:25 +02002530 if (prev_mod == mod) {
2531 mod = NULL;
2532 }
2533
2534 /* realloc string */
2535 len = 1 + (mod ? strlen(mod->name) + 1 : 0) + (iter->schema ? strlen(iter->schema->name) :
2536 strlen(((struct lyd_node_opaq *)iter)->name.name));
2537 if ((rc = lyd_path_str_enlarge(&buffer, &buflen, bufused + len, 0))) {
2538 break;
2539 }
2540
2541 /* print next node */
2542 bufused += sprintf(buffer + bufused, "/%s%s%s", mod ? mod->name : "", mod ? ":" : "", LYD_NAME(iter));
2543
2544 /* do not always print the last (first) predicate */
2545 if (iter->schema && ((depth > 1) || (pathtype == LYD_PATH_STD))) {
2546 switch (iter->schema->nodetype) {
2547 case LYS_LIST:
2548 if (iter->schema->flags & LYS_KEYLESS) {
2549 /* print its position */
2550 rc = lyd_path_position_predicate(iter, &buffer, &buflen, &bufused, 0);
2551 } else {
2552 /* print all list keys in predicates */
2553 rc = lyd_path_list_predicate(iter, &buffer, &buflen, &bufused, 0);
2554 }
2555 break;
2556 case LYS_LEAFLIST:
2557 if (iter->schema->flags & LYS_CONFIG_W) {
2558 /* print leaf-list value */
2559 rc = lyd_path_leaflist_predicate(iter, &buffer, &buflen, &bufused, 0);
2560 } else {
2561 /* print its position */
2562 rc = lyd_path_position_predicate(iter, &buffer, &buflen, &bufused, 0);
2563 }
2564 break;
2565 default:
2566 /* nothing to print more */
2567 break;
2568 }
2569 }
2570 if (rc) {
2571 break;
2572 }
2573 }
2574 break;
2575 }
2576
2577 return buffer;
2578}
2579
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002580LIBYANG_API_DEF struct lyd_meta *
Michal Vasko25a32822020-07-09 15:48:22 +02002581lyd_find_meta(const struct lyd_meta *first, const struct lys_module *module, const char *name)
2582{
2583 struct lyd_meta *ret = NULL;
2584 const struct ly_ctx *ctx;
2585 const char *prefix, *tmp;
2586 char *str;
2587 size_t pref_len, name_len;
2588
2589 LY_CHECK_ARG_RET(NULL, module || strchr(name, ':'), name, NULL);
Michal Vasko892f5bf2021-11-24 10:41:05 +01002590 LY_CHECK_CTX_EQUAL_RET(first ? first->annotation->module->ctx : NULL, module ? module->ctx : NULL, NULL);
Michal Vasko25a32822020-07-09 15:48:22 +02002591
2592 if (!first) {
2593 return NULL;
2594 }
2595
2596 ctx = first->annotation->module->ctx;
2597
2598 /* parse the name */
2599 tmp = name;
2600 if (ly_parse_nodeid(&tmp, &prefix, &pref_len, &name, &name_len) || tmp[0]) {
2601 LOGERR(ctx, LY_EINVAL, "Metadata name \"%s\" is not valid.", name);
2602 return NULL;
2603 }
2604
2605 /* find the module */
2606 if (prefix) {
2607 str = strndup(prefix, pref_len);
2608 module = ly_ctx_get_module_latest(ctx, str);
2609 free(str);
Radek Krejci422afb12021-03-04 16:38:16 +01002610 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 +02002611 }
2612
2613 /* find the metadata */
2614 LY_LIST_FOR(first, first) {
2615 if ((first->annotation->module == module) && !strcmp(first->name, name)) {
2616 ret = (struct lyd_meta *)first;
2617 break;
2618 }
2619 }
2620
2621 return ret;
2622}
2623
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002624LIBYANG_API_DEF LY_ERR
Michal Vaskoe444f752020-02-10 12:20:06 +01002625lyd_find_sibling_first(const struct lyd_node *siblings, const struct lyd_node *target, struct lyd_node **match)
2626{
Michal Vasko9beceb82022-04-05 12:14:15 +02002627 struct lyd_node **match_p, *iter, *dup = NULL;
Michal Vaskoe444f752020-02-10 12:20:06 +01002628 struct lyd_node_inner *parent;
Michal Vaskoe78faec2021-04-08 17:24:43 +02002629 ly_bool found;
Michal Vaskoe444f752020-02-10 12:20:06 +01002630
Michal Vaskof03ed032020-03-04 13:31:44 +01002631 LY_CHECK_ARG_RET(NULL, target, LY_EINVAL);
Michal Vasko9beceb82022-04-05 12:14:15 +02002632 if (!siblings) {
2633 /* no data */
2634 if (match) {
2635 *match = NULL;
2636 }
2637 return LY_ENOTFOUND;
2638 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002639
Michal Vasko9beceb82022-04-05 12:14:15 +02002640 if (LYD_CTX(siblings) != LYD_CTX(target)) {
2641 /* create a duplicate in this context */
2642 LY_CHECK_RET(lyd_dup_single_to_ctx(target, LYD_CTX(siblings), NULL, 0, &dup));
2643 target = dup;
2644 }
2645
2646 if ((siblings->schema && target->schema && (lysc_data_parent(siblings->schema) != lysc_data_parent(target->schema)))) {
2647 /* schema mismatch */
2648 lyd_free_tree(dup);
Michal Vasko9b368d32020-02-14 13:53:31 +01002649 if (match) {
2650 *match = NULL;
2651 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002652 return LY_ENOTFOUND;
2653 }
2654
Michal Vaskoe78faec2021-04-08 17:24:43 +02002655 /* get first sibling */
2656 siblings = lyd_first_sibling(siblings);
Michal Vaskoe444f752020-02-10 12:20:06 +01002657
Michal Vasko9e685082021-01-29 14:49:09 +01002658 parent = siblings->parent;
Michal Vasko39311152023-08-07 11:03:41 +02002659 if (target->schema && parent && parent->schema && parent->children_ht) {
Michal Vaskoe444f752020-02-10 12:20:06 +01002660 assert(target->hash);
2661
Michal Vaskoe78faec2021-04-08 17:24:43 +02002662 if (lysc_is_dup_inst_list(target->schema)) {
2663 /* we must search the instances from beginning to find the first matching one */
2664 found = 0;
2665 LYD_LIST_FOR_INST(siblings, target->schema, iter) {
Michal Vaskoee9b9482023-06-19 13:17:48 +02002666 if (!lyd_compare_single(target, iter, LYD_COMPARE_FULL_RECURSION)) {
Michal Vaskoe78faec2021-04-08 17:24:43 +02002667 found = 1;
2668 break;
2669 }
2670 }
2671 if (found) {
2672 siblings = iter;
Michal Vaskoda859032020-07-14 12:20:14 +02002673 } else {
2674 siblings = NULL;
2675 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002676 } else {
Michal Vaskoe78faec2021-04-08 17:24:43 +02002677 /* find by hash */
2678 if (!lyht_find(parent->children_ht, &target, target->hash, (void **)&match_p)) {
2679 siblings = *match_p;
2680 } else {
2681 /* not found */
2682 siblings = NULL;
2683 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002684 }
2685 } else {
Michal Vasko39311152023-08-07 11:03:41 +02002686 /* no children hash table or cannot be used */
Michal Vaskod989ba02020-08-24 10:59:24 +02002687 for ( ; siblings; siblings = siblings->next) {
Michal Vaskoee9b9482023-06-19 13:17:48 +02002688 if (lysc_is_dup_inst_list(target->schema)) {
2689 if (!lyd_compare_single(siblings, target, LYD_COMPARE_FULL_RECURSION)) {
2690 break;
2691 }
2692 } else {
Michal Vaskod8a52012023-08-15 11:38:10 +02002693 if (!lyd_compare_single(siblings, target, 0)) {
Michal Vaskoee9b9482023-06-19 13:17:48 +02002694 break;
2695 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002696 }
2697 }
2698 }
2699
Michal Vasko9beceb82022-04-05 12:14:15 +02002700 lyd_free_tree(dup);
Michal Vaskoe444f752020-02-10 12:20:06 +01002701 if (!siblings) {
Michal Vasko9b368d32020-02-14 13:53:31 +01002702 if (match) {
2703 *match = NULL;
2704 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002705 return LY_ENOTFOUND;
2706 }
2707
Michal Vasko9b368d32020-02-14 13:53:31 +01002708 if (match) {
2709 *match = (struct lyd_node *)siblings;
2710 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002711 return LY_SUCCESS;
2712}
2713
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002714LIBYANG_API_DEF LY_ERR
Michal Vaskoe444f752020-02-10 12:20:06 +01002715lyd_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 +02002716 size_t val_len, struct lyd_node **match)
Michal Vaskoe444f752020-02-10 12:20:06 +01002717{
2718 LY_ERR rc;
2719 struct lyd_node *target = NULL;
Michal Vasko9beceb82022-04-05 12:14:15 +02002720 const struct lyd_node *parent;
Michal Vaskoe444f752020-02-10 12:20:06 +01002721
Michal Vasko4c583e82020-07-17 12:16:14 +02002722 LY_CHECK_ARG_RET(NULL, schema, !(schema->nodetype & (LYS_CHOICE | LYS_CASE)), LY_EINVAL);
Michal Vasko9beceb82022-04-05 12:14:15 +02002723 if (!siblings) {
2724 /* no data */
2725 if (match) {
2726 *match = NULL;
2727 }
2728 return LY_ENOTFOUND;
2729 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002730
Michal Vasko9beceb82022-04-05 12:14:15 +02002731 if ((LYD_CTX(siblings) != schema->module->ctx)) {
2732 /* parent of ext nodes is useless */
2733 parent = (siblings->flags & LYD_EXT) ? NULL : lyd_parent(siblings);
Michal Vaskoaf3df492022-12-02 14:03:52 +01002734 if (lyd_find_schema_ctx(schema, LYD_CTX(siblings), parent, 0, &schema)) {
2735 /* no schema node in siblings so certainly no data node either */
2736 if (match) {
2737 *match = NULL;
2738 }
2739 return LY_ENOTFOUND;
2740 }
Michal Vasko9beceb82022-04-05 12:14:15 +02002741 }
2742
2743 if (siblings->schema && (lysc_data_parent(siblings->schema) != lysc_data_parent(schema))) {
2744 /* schema mismatch */
Michal Vasko9b368d32020-02-14 13:53:31 +01002745 if (match) {
2746 *match = NULL;
2747 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002748 return LY_ENOTFOUND;
2749 }
2750
Michal Vaskof03ed032020-03-04 13:31:44 +01002751 if (key_or_value && !val_len) {
2752 val_len = strlen(key_or_value);
2753 }
2754
Michal Vaskob104f112020-07-17 09:54:54 +02002755 if ((schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) && key_or_value) {
2756 /* create a data node and find the instance */
2757 if (schema->nodetype == LYS_LEAFLIST) {
2758 /* target used attributes: schema, hash, value */
Michal Vasko989cdb42023-10-06 15:32:37 +02002759 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 +02002760 LY_CHECK_RET(rc);
Michal Vaskob104f112020-07-17 09:54:54 +02002761 } else {
Michal Vasko90932a92020-02-12 14:33:03 +01002762 /* target used attributes: schema, hash, child (all keys) */
Michal Vasko004d3152020-06-11 19:59:22 +02002763 LY_CHECK_RET(lyd_create_list2(schema, key_or_value, val_len, &target));
Michal Vasko90932a92020-02-12 14:33:03 +01002764 }
2765
2766 /* find it */
2767 rc = lyd_find_sibling_first(siblings, target, match);
Michal Vaskob104f112020-07-17 09:54:54 +02002768 } else {
2769 /* find the first schema node instance */
2770 rc = lyd_find_sibling_schema(siblings, schema, match);
Michal Vaskoe444f752020-02-10 12:20:06 +01002771 }
2772
Michal Vaskoe444f752020-02-10 12:20:06 +01002773 lyd_free_tree(target);
2774 return rc;
2775}
Michal Vaskoccc02342020-05-21 10:09:21 +02002776
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002777LIBYANG_API_DEF LY_ERR
Michal Vaskoe78faec2021-04-08 17:24:43 +02002778lyd_find_sibling_dup_inst_set(const struct lyd_node *siblings, const struct lyd_node *target, struct ly_set **set)
2779{
2780 struct lyd_node **match_p, *first, *iter;
2781 struct lyd_node_inner *parent;
Michal Vaskoee9b9482023-06-19 13:17:48 +02002782 uint32_t comp_opts;
Michal Vaskoe78faec2021-04-08 17:24:43 +02002783
Michal Vasko83ae7772022-06-08 10:01:55 +02002784 LY_CHECK_ARG_RET(NULL, target, set, LY_EINVAL);
Michal Vasko892f5bf2021-11-24 10:41:05 +01002785 LY_CHECK_CTX_EQUAL_RET(siblings ? LYD_CTX(siblings) : NULL, LYD_CTX(target), LY_EINVAL);
Michal Vaskoe78faec2021-04-08 17:24:43 +02002786
2787 LY_CHECK_RET(ly_set_new(set));
2788
2789 if (!siblings || (siblings->schema && (lysc_data_parent(siblings->schema) != lysc_data_parent(target->schema)))) {
2790 /* no data or schema mismatch */
2791 return LY_ENOTFOUND;
2792 }
2793
Michal Vaskoee9b9482023-06-19 13:17:48 +02002794 /* set options */
Michal Vaskod8a52012023-08-15 11:38:10 +02002795 comp_opts = (lysc_is_dup_inst_list(target->schema) ? LYD_COMPARE_FULL_RECURSION : 0);
Michal Vaskoee9b9482023-06-19 13:17:48 +02002796
Michal Vaskoe78faec2021-04-08 17:24:43 +02002797 /* get first sibling */
2798 siblings = lyd_first_sibling(siblings);
2799
2800 parent = siblings->parent;
2801 if (parent && parent->schema && parent->children_ht) {
2802 assert(target->hash);
2803
2804 /* find the first instance */
2805 lyd_find_sibling_first(siblings, target, &first);
2806 if (first) {
2807 /* add it so that it is the first in the set */
2808 if (ly_set_add(*set, first, 1, NULL)) {
2809 goto error;
2810 }
2811
2812 /* find by hash */
2813 if (!lyht_find(parent->children_ht, &target, target->hash, (void **)&match_p)) {
2814 iter = *match_p;
2815 } else {
2816 /* not found */
2817 iter = NULL;
2818 }
2819 while (iter) {
2820 /* add all found nodes into the set */
Michal Vaskoee9b9482023-06-19 13:17:48 +02002821 if ((iter != first) && !lyd_compare_single(iter, target, comp_opts) && ly_set_add(*set, iter, 1, NULL)) {
Michal Vaskoe78faec2021-04-08 17:24:43 +02002822 goto error;
2823 }
2824
2825 /* find next instance */
2826 if (lyht_find_next(parent->children_ht, &iter, iter->hash, (void **)&match_p)) {
2827 iter = NULL;
2828 } else {
2829 iter = *match_p;
2830 }
2831 }
2832 }
2833 } else {
2834 /* no children hash table */
2835 LY_LIST_FOR(siblings, siblings) {
Michal Vaskoee9b9482023-06-19 13:17:48 +02002836 if (!lyd_compare_single(target, siblings, comp_opts)) {
Michal Vaskoe78faec2021-04-08 17:24:43 +02002837 ly_set_add(*set, (void *)siblings, 1, NULL);
2838 }
2839 }
2840 }
2841
2842 if (!(*set)->count) {
2843 return LY_ENOTFOUND;
2844 }
2845 return LY_SUCCESS;
2846
2847error:
2848 ly_set_free(*set, NULL);
2849 *set = NULL;
2850 return LY_EMEM;
2851}
2852
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002853LIBYANG_API_DEF LY_ERR
Michal Vasko1d4af6c2021-02-22 13:31:26 +01002854lyd_find_sibling_opaq_next(const struct lyd_node *first, const char *name, struct lyd_node **match)
2855{
2856 LY_CHECK_ARG_RET(NULL, name, LY_EINVAL);
2857
Michal Vaskoe271a312023-08-15 11:46:30 +02002858 if (first && first->schema) {
2859 first = first->prev;
2860 if (first->schema) {
2861 /* no opaque nodes */
2862 first = NULL;
2863 } else {
2864 /* opaque nodes are at the end, find quickly the first */
2865 while (!first->prev->schema) {
2866 first = first->prev;
2867 }
2868 }
2869 }
2870
Michal Vasko1d4af6c2021-02-22 13:31:26 +01002871 for ( ; first; first = first->next) {
Michal Vaskoe271a312023-08-15 11:46:30 +02002872 assert(!first->schema);
2873 if (!strcmp(LYD_NAME(first), name)) {
Michal Vasko1d4af6c2021-02-22 13:31:26 +01002874 break;
2875 }
2876 }
2877
2878 if (match) {
2879 *match = (struct lyd_node *)first;
2880 }
2881 return first ? LY_SUCCESS : LY_ENOTFOUND;
2882}
2883
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002884LIBYANG_API_DEF LY_ERR
Michal Vaskod96e2372023-02-24 16:07:51 +01002885lyd_find_xpath(const struct lyd_node *ctx_node, const char *xpath, struct ly_set **set)
Michal Vaskoccc02342020-05-21 10:09:21 +02002886{
Michal Vaskod96e2372023-02-24 16:07:51 +01002887 LY_CHECK_ARG_RET(NULL, ctx_node, xpath, set, LY_EINVAL);
Michal Vaskoccc02342020-05-21 10:09:21 +02002888
Michal Vaskod96e2372023-02-24 16:07:51 +01002889 return lyd_find_xpath4(ctx_node, ctx_node, xpath, LY_VALUE_JSON, NULL, NULL, set);
2890}
Michal Vaskoccc02342020-05-21 10:09:21 +02002891
Michal Vaskod96e2372023-02-24 16:07:51 +01002892LIBYANG_API_DEF LY_ERR
2893lyd_find_xpath2(const struct lyd_node *ctx_node, const char *xpath, const struct lyxp_var *vars, struct ly_set **set)
2894{
2895 LY_CHECK_ARG_RET(NULL, ctx_node, xpath, set, LY_EINVAL);
Michal Vaskoccc02342020-05-21 10:09:21 +02002896
Michal Vaskod96e2372023-02-24 16:07:51 +01002897 return lyd_find_xpath4(ctx_node, ctx_node, xpath, LY_VALUE_JSON, NULL, vars, set);
Michal Vaskoccc02342020-05-21 10:09:21 +02002898}
Radek Krejcica989142020-11-05 11:32:22 +01002899
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002900LIBYANG_API_DEF LY_ERR
Michal Vaskoddd76592022-01-17 13:34:48 +01002901lyd_find_xpath3(const struct lyd_node *ctx_node, const struct lyd_node *tree, const char *xpath,
2902 const struct lyxp_var *vars, struct ly_set **set)
2903{
2904 LY_CHECK_ARG_RET(NULL, tree, xpath, set, LY_EINVAL);
2905
2906 return lyd_find_xpath4(ctx_node, tree, xpath, LY_VALUE_JSON, NULL, vars, set);
2907}
2908
2909LIBYANG_API_DEF LY_ERR
Michal Vaskod96e2372023-02-24 16:07:51 +01002910lyd_find_xpath4(const struct lyd_node *ctx_node, const struct lyd_node *tree, const char *xpath, LY_VALUE_FORMAT format,
2911 void *prefix_data, const struct lyxp_var *vars, struct ly_set **set)
Michal Vaskoe3716b32021-12-13 16:58:25 +01002912{
Michal Vaskod96e2372023-02-24 16:07:51 +01002913 LY_CHECK_ARG_RET(NULL, tree, xpath, set, LY_EINVAL);
Michal Vaskoe3716b32021-12-13 16:58:25 +01002914
Michal Vaskod96e2372023-02-24 16:07:51 +01002915 *set = NULL;
2916
2917 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 +01002918}
2919
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002920LIBYANG_API_DEF LY_ERR
Michal Vaskod96e2372023-02-24 16:07:51 +01002921lyd_eval_xpath(const struct lyd_node *ctx_node, const char *xpath, ly_bool *result)
aPiecekfba75362021-10-07 12:39:48 +02002922{
Michal Vaskod96e2372023-02-24 16:07:51 +01002923 return lyd_eval_xpath3(ctx_node, NULL, xpath, LY_VALUE_JSON, NULL, NULL, result);
Michal Vaskoc9eb3ca2021-07-16 10:20:37 +02002924}
2925
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002926LIBYANG_API_DEF LY_ERR
Michal Vasko10fabfc2022-08-09 08:55:43 +02002927lyd_eval_xpath2(const struct lyd_node *ctx_node, const char *xpath, const struct lyxp_var *vars, ly_bool *result)
2928{
2929 return lyd_eval_xpath3(ctx_node, NULL, xpath, LY_VALUE_JSON, NULL, vars, result);
2930}
2931
2932LIBYANG_API_DEF LY_ERR
Michal Vaskod96e2372023-02-24 16:07:51 +01002933lyd_eval_xpath3(const struct lyd_node *ctx_node, const struct lys_module *cur_mod, const char *xpath,
2934 LY_VALUE_FORMAT format, void *prefix_data, const struct lyxp_var *vars, ly_bool *result)
aPiecekfba75362021-10-07 12:39:48 +02002935{
Michal Vaskod96e2372023-02-24 16:07:51 +01002936 return lyd_eval_xpath4(ctx_node, ctx_node, cur_mod, xpath, format, prefix_data, vars, NULL, NULL, NULL, NULL, result);
2937}
2938
2939LIBYANG_API_DEF LY_ERR
2940lyd_eval_xpath4(const struct lyd_node *ctx_node, const struct lyd_node *tree, const struct lys_module *cur_mod,
2941 const char *xpath, LY_VALUE_FORMAT format, void *prefix_data, const struct lyxp_var *vars, LY_XPATH_TYPE *ret_type,
2942 struct ly_set **node_set, char **string, long double *number, ly_bool *boolean)
2943{
2944 LY_ERR ret = LY_SUCCESS;
2945 struct lyxp_set xp_set = {0};
2946 struct lyxp_expr *exp = NULL;
2947 uint32_t i;
2948
2949 LY_CHECK_ARG_RET(NULL, tree, xpath, ((ret_type && node_set && string && number && boolean) ||
2950 (node_set && !string && !number && !boolean) || (!node_set && string && !number && !boolean) ||
2951 (!node_set && !string && number && !boolean) || (!node_set && !string && !number && boolean)), LY_EINVAL);
2952
2953 /* parse expression */
2954 ret = lyxp_expr_parse((struct ly_ctx *)LYD_CTX(tree), xpath, 0, 1, &exp);
2955 LY_CHECK_GOTO(ret, cleanup);
2956
2957 /* evaluate expression */
2958 ret = lyxp_eval(LYD_CTX(tree), exp, cur_mod, format, prefix_data, ctx_node, ctx_node, tree, vars, &xp_set,
2959 LYXP_IGNORE_WHEN);
2960 LY_CHECK_GOTO(ret, cleanup);
2961
2962 /* return expected result type without or with casting */
2963 if (node_set) {
2964 /* node set */
2965 if (xp_set.type == LYXP_SET_NODE_SET) {
2966 /* transform into a set */
2967 LY_CHECK_GOTO(ret = ly_set_new(node_set), cleanup);
2968 (*node_set)->objs = malloc(xp_set.used * sizeof *(*node_set)->objs);
2969 LY_CHECK_ERR_GOTO(!(*node_set)->objs, LOGMEM(LYD_CTX(tree)); ret = LY_EMEM, cleanup);
2970 (*node_set)->size = xp_set.used;
2971 for (i = 0; i < xp_set.used; ++i) {
2972 if (xp_set.val.nodes[i].type == LYXP_NODE_ELEM) {
2973 ret = ly_set_add(*node_set, xp_set.val.nodes[i].node, 1, NULL);
2974 LY_CHECK_GOTO(ret, cleanup);
2975 }
2976 }
2977 if (ret_type) {
2978 *ret_type = LY_XPATH_NODE_SET;
2979 }
2980 } else if (!string && !number && !boolean) {
2981 LOGERR(LYD_CTX(tree), LY_EINVAL, "XPath \"%s\" result is not a node set.", xpath);
2982 ret = LY_EINVAL;
2983 goto cleanup;
2984 }
2985 }
2986
2987 if (string) {
2988 if ((xp_set.type != LYXP_SET_STRING) && !node_set) {
2989 /* cast into string */
2990 LY_CHECK_GOTO(ret = lyxp_set_cast(&xp_set, LYXP_SET_STRING), cleanup);
2991 }
2992 if (xp_set.type == LYXP_SET_STRING) {
2993 /* string */
2994 *string = xp_set.val.str;
2995 xp_set.val.str = NULL;
2996 if (ret_type) {
2997 *ret_type = LY_XPATH_STRING;
2998 }
2999 }
3000 }
3001
3002 if (number) {
3003 if ((xp_set.type != LYXP_SET_NUMBER) && !node_set) {
3004 /* cast into number */
3005 LY_CHECK_GOTO(ret = lyxp_set_cast(&xp_set, LYXP_SET_NUMBER), cleanup);
3006 }
3007 if (xp_set.type == LYXP_SET_NUMBER) {
3008 /* number */
3009 *number = xp_set.val.num;
3010 if (ret_type) {
3011 *ret_type = LY_XPATH_NUMBER;
3012 }
3013 }
3014 }
3015
3016 if (boolean) {
3017 if ((xp_set.type != LYXP_SET_BOOLEAN) && !node_set) {
3018 /* cast into boolean */
3019 LY_CHECK_GOTO(ret = lyxp_set_cast(&xp_set, LYXP_SET_BOOLEAN), cleanup);
3020 }
3021 if (xp_set.type == LYXP_SET_BOOLEAN) {
3022 /* boolean */
3023 *boolean = xp_set.val.bln;
3024 if (ret_type) {
3025 *ret_type = LY_XPATH_BOOLEAN;
3026 }
3027 }
3028 }
3029
3030cleanup:
3031 lyxp_set_free_content(&xp_set);
3032 lyxp_expr_free((struct ly_ctx *)LYD_CTX(tree), exp);
3033 return ret;
aPiecekfba75362021-10-07 12:39:48 +02003034}
3035
Michal Vasko99a77882024-01-04 14:50:51 +01003036/**
3037 * @brief Hash table node equal callback.
3038 */
3039static ly_bool
3040lyd_trim_equal_cb(void *val1_p, void *val2_p, ly_bool UNUSED(mod), void *UNUSED(cb_data))
3041{
3042 struct lyd_node *node1, *node2;
3043
3044 node1 = *(struct lyd_node **)val1_p;
3045 node2 = *(struct lyd_node **)val2_p;
3046
3047 return node1 == node2;
3048}
3049
3050LIBYANG_API_DEF LY_ERR
3051lyd_trim_xpath(struct lyd_node **tree, const char *xpath, const struct lyxp_var *vars)
3052{
3053 LY_ERR ret = LY_SUCCESS;
3054 struct ly_ctx *ctx;
3055 struct lyxp_set xp_set = {0};
3056 struct lyxp_expr *exp = NULL;
3057 struct lyd_node *node, *parent;
3058 struct lyxp_set_hash_node hnode;
3059 struct ly_ht *parent_ht = NULL;
3060 struct ly_set free_set = {0};
3061 uint32_t i, hash;
3062 ly_bool is_result;
3063
3064 LY_CHECK_ARG_RET(NULL, tree, xpath, LY_EINVAL);
3065
3066 if (!*tree) {
3067 /* nothing to do */
3068 goto cleanup;
3069 }
3070
3071 *tree = lyd_first_sibling(*tree);
3072 ctx = (struct ly_ctx *)LYD_CTX(*tree);
3073
3074 /* parse expression */
3075 ret = lyxp_expr_parse(ctx, xpath, 0, 1, &exp);
3076 LY_CHECK_GOTO(ret, cleanup);
3077
3078 /* evaluate expression */
3079 ret = lyxp_eval(ctx, exp, NULL, LY_VALUE_JSON, NULL, *tree, *tree, *tree, vars, &xp_set, LYXP_IGNORE_WHEN);
3080 LY_CHECK_GOTO(ret, cleanup);
3081
3082 /* create hash table for all the parents of results */
Michal Vasko0a0716d2024-01-04 15:02:12 +01003083 parent_ht = lyht_new(32, sizeof node, lyd_trim_equal_cb, NULL, 1);
Michal Vasko99a77882024-01-04 14:50:51 +01003084 LY_CHECK_GOTO(!parent_ht, cleanup);
3085
3086 for (i = 0; i < xp_set.used; ++i) {
3087 if (xp_set.val.nodes[i].type != LYXP_NODE_ELEM) {
3088 /* ignore */
3089 continue;
3090 }
3091
3092 for (parent = lyd_parent(xp_set.val.nodes[i].node); parent; parent = lyd_parent(parent)) {
3093 /* add the parent into parent_ht */
3094 ret = lyht_insert(parent_ht, &parent, parent->hash, NULL);
3095 if (ret == LY_EEXIST) {
3096 /* shared parent, we are done */
3097 break;
3098 }
3099 LY_CHECK_GOTO(ret, cleanup);
3100 }
3101 }
3102
3103 hnode.type = LYXP_NODE_ELEM;
3104 LY_LIST_FOR(*tree, parent) {
3105 LYD_TREE_DFS_BEGIN(parent, node) {
3106 if (lysc_is_key(node->schema)) {
3107 /* ignore */
3108 goto next_iter;
3109 }
3110
3111 /* check the results */
3112 is_result = 0;
3113 if (xp_set.ht) {
3114 hnode.node = node;
3115 hash = lyht_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
3116 hash = lyht_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
3117 hash = lyht_hash_multi(hash, NULL, 0);
3118
3119 if (!lyht_find(xp_set.ht, &hnode, hash, NULL)) {
3120 is_result = 1;
3121 }
3122 } else {
3123 /* not enough elements for a hash table */
3124 for (i = 0; i < xp_set.used; ++i) {
3125 if (xp_set.val.nodes[i].type != LYXP_NODE_ELEM) {
3126 /* ignore */
3127 continue;
3128 }
3129
3130 if (xp_set.val.nodes[i].node == node) {
3131 is_result = 1;
3132 break;
3133 }
3134 }
3135 }
3136
3137 if (is_result) {
3138 /* keep the whole subtree if the node is in the results */
3139 LYD_TREE_DFS_continue = 1;
3140 } else if (lyht_find(parent_ht, &node, node->hash, NULL)) {
3141 /* free the whole subtree if the node is not even among the selected parents */
3142 ret = ly_set_add(&free_set, node, 1, NULL);
3143 LY_CHECK_GOTO(ret, cleanup);
3144 LYD_TREE_DFS_continue = 1;
3145 } /* else keep the parent node because a subtree is in the results */
3146
3147next_iter:
3148 LYD_TREE_DFS_END(parent, node);
3149 }
3150 }
3151
3152 /* free */
3153 for (i = 0; i < free_set.count; ++i) {
3154 node = free_set.dnodes[i];
3155 if (*tree == node) {
3156 *tree = (*tree)->next;
3157 }
3158 lyd_free_tree(node);
3159 }
3160
3161cleanup:
3162 lyxp_set_free_content(&xp_set);
3163 lyxp_expr_free(ctx, exp);
3164 lyht_free(parent_ht, NULL);
3165 ly_set_erase(&free_set, NULL);
3166 return ret;
3167}
3168
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01003169LIBYANG_API_DEF LY_ERR
Michal Vasko3e1f6552021-01-14 09:27:55 +01003170lyd_find_path(const struct lyd_node *ctx_node, const char *path, ly_bool output, struct lyd_node **match)
3171{
3172 LY_ERR ret = LY_SUCCESS;
3173 struct lyxp_expr *expr = NULL;
3174 struct ly_path *lypath = NULL;
3175
3176 LY_CHECK_ARG_RET(NULL, ctx_node, ctx_node->schema, path, LY_EINVAL);
3177
3178 /* parse the path */
Michal Vaskoed725d72021-06-23 12:03:45 +02003179 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 +01003180 LY_PATH_PREFIX_FIRST, LY_PATH_PRED_SIMPLE, &expr);
Michal Vasko3e1f6552021-01-14 09:27:55 +01003181 LY_CHECK_GOTO(ret, cleanup);
3182
3183 /* compile the path */
Michal Vaskoed725d72021-06-23 12:03:45 +02003184 ret = ly_path_compile(LYD_CTX(ctx_node), NULL, ctx_node->schema, NULL, expr,
Michal Vasko0884d212021-10-14 09:21:46 +02003185 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 +01003186 LY_CHECK_GOTO(ret, cleanup);
3187
3188 /* evaluate the path */
Michal Vasko838829d2023-10-09 16:06:43 +02003189 ret = ly_path_eval_partial(lypath, ctx_node, NULL, 0, NULL, match);
Michal Vasko3e1f6552021-01-14 09:27:55 +01003190
3191cleanup:
3192 lyxp_expr_free(LYD_CTX(ctx_node), expr);
3193 ly_path_free(LYD_CTX(ctx_node), lypath);
3194 return ret;
3195}
3196
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01003197LIBYANG_API_DEF LY_ERR
Michal Vaskobb22b182021-06-14 08:14:21 +02003198lyd_find_target(const struct ly_path *path, const struct lyd_node *tree, struct lyd_node **match)
3199{
3200 LY_ERR ret;
3201 struct lyd_node *m;
3202
3203 LY_CHECK_ARG_RET(NULL, path, LY_EINVAL);
3204
Michal Vasko90189962023-02-28 12:10:34 +01003205 ret = ly_path_eval(path, tree, NULL, &m);
Michal Vaskobb22b182021-06-14 08:14:21 +02003206 if (ret) {
3207 if (match) {
3208 *match = NULL;
3209 }
3210 return LY_ENOTFOUND;
3211 }
3212
3213 if (match) {
3214 *match = m;
3215 }
3216 return LY_SUCCESS;
3217}
Irfand3b351a2023-09-14 14:52:15 +02003218
3219LIBYANG_API_DEF struct lyd_node *
3220lyd_parent(const struct lyd_node *node)
3221{
3222 if (!node || !node->parent) {
3223 return NULL;
3224 }
3225
3226 return &node->parent->node;
3227}
3228
3229LIBYANG_API_DEF struct lyd_node *
3230lyd_child(const struct lyd_node *node)
3231{
3232 if (!node) {
3233 return NULL;
3234 }
3235
3236 if (!node->schema) {
3237 /* opaq node */
3238 return ((const struct lyd_node_opaq *)node)->child;
3239 }
3240
3241 switch (node->schema->nodetype) {
3242 case LYS_CONTAINER:
3243 case LYS_LIST:
3244 case LYS_RPC:
3245 case LYS_ACTION:
3246 case LYS_NOTIF:
3247 return ((const struct lyd_node_inner *)node)->child;
3248 default:
3249 return NULL;
3250 }
3251}
3252
3253LIBYANG_API_DEF const char *
3254lyd_get_value(const struct lyd_node *node)
3255{
3256 if (!node) {
3257 return NULL;
3258 }
3259
3260 if (!node->schema) {
3261 return ((const struct lyd_node_opaq *)node)->value;
3262 } else if (node->schema->nodetype & LYD_NODE_TERM) {
3263 const struct lyd_value *value = &((const struct lyd_node_term *)node)->value;
3264
3265 return value->_canonical ? value->_canonical : lyd_value_get_canonical(LYD_CTX(node), value);
3266 }
3267
3268 return NULL;
3269}