blob: 0c56c00c5e7481f653788acae2f0f4a980535ec6 [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
Michal Vasko5aa44c02020-06-29 11:47:02 +020029#include "compat.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020030#include "context.h"
31#include "dict.h"
Michal Vaskoa6669ba2020-08-06 16:14:26 +020032#include "diff.h"
Michal Vasko90932a92020-02-12 14:33:03 +010033#include "hash_table.h"
Radek Krejci47fab892020-11-05 17:02:41 +010034#include "in.h"
Michal Vaskoafac7822020-10-20 14:22:26 +020035#include "in_internal.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020036#include "log.h"
Michal Vasko8f702ee2024-02-20 15:44:24 +010037#include "ly_common.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.
Michal Vaskob36c56f2024-01-31 12:50:07 +010090 * @param[in,out] first_p Pointer to the first parsed node.
Michal Vaskoe0665742021-02-11 11:08:44 +010091 * @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 Vasko7a266772024-01-23 11:02:38 +0100106 const struct ly_err_item *eitem;
Michal Vaskoddd76592022-01-17 13:34:48 +0100107 ly_bool subtree_sibling = 0;
Radek Krejci1798aae2020-07-14 13:26:06 +0200108
Michal Vaskoe0665742021-02-11 11:08:44 +0100109 assert(ctx && (parent || first_p));
Radek Krejci7931b192020-06-25 17:05:03 +0200110
111 format = lyd_parse_get_format(in, format);
Michal Vaskoe0665742021-02-11 11:08:44 +0100112 if (first_p) {
113 *first_p = NULL;
114 }
Radek Krejci1798aae2020-07-14 13:26:06 +0200115
Michal Vasko63f3d842020-07-08 10:10:14 +0200116 /* remember input position */
117 in->func_start = in->current;
Radek Krejci7931b192020-06-25 17:05:03 +0200118
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +0100119 /* set internal options */
120 if (!(parse_opts & LYD_PARSE_SUBTREE)) {
121 int_opts = LYD_INTOPT_WITH_SIBLINGS;
122 }
123
Michal Vaskoe0665742021-02-11 11:08:44 +0100124 /* parse the data */
Radek Krejci7931b192020-06-25 17:05:03 +0200125 switch (format) {
126 case LYD_XML:
Michal Vaskod027f382023-02-10 09:13:25 +0100127 r = lyd_parse_xml(ctx, ext, parent, first_p, in, parse_opts, val_opts, int_opts, &parsed,
Michal Vaskoddd76592022-01-17 13:34:48 +0100128 &subtree_sibling, &lydctx);
Radek Krejci1798aae2020-07-14 13:26:06 +0200129 break;
Radek Krejci7931b192020-06-25 17:05:03 +0200130 case LYD_JSON:
Michal Vaskod027f382023-02-10 09:13:25 +0100131 r = lyd_parse_json(ctx, ext, parent, first_p, in, parse_opts, val_opts, int_opts, &parsed,
Michal Vaskoddd76592022-01-17 13:34:48 +0100132 &subtree_sibling, &lydctx);
Radek Krejci1798aae2020-07-14 13:26:06 +0200133 break;
Radek Krejci7931b192020-06-25 17:05:03 +0200134 case LYD_LYB:
Michal Vaskod027f382023-02-10 09:13:25 +0100135 r = lyd_parse_lyb(ctx, ext, parent, first_p, in, parse_opts, val_opts, int_opts, &parsed,
Michal Vaskoddd76592022-01-17 13:34:48 +0100136 &subtree_sibling, &lydctx);
Radek Krejci1798aae2020-07-14 13:26:06 +0200137 break;
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200138 case LYD_UNKNOWN:
Michal Vaskod74978f2021-02-12 11:59:36 +0100139 LOGARG(ctx, format);
Michal Vaskod027f382023-02-10 09:13:25 +0100140 r = LY_EINVAL;
Michal Vaskoe0665742021-02-11 11:08:44 +0100141 break;
142 }
Michal Vasko50da8cd2023-03-10 08:38:59 +0100143 if (r) {
144 rc = r;
Michal Vasko7a266772024-01-23 11:02:38 +0100145 if ((r != LY_EVALID) || !lydctx || !(lydctx->val_opts & LYD_VALIDATE_MULTI_ERROR)) {
146 goto cleanup;
147 }
148
149 eitem = ly_err_last(ctx);
150 assert(eitem);
151 if (eitem->vecode == LYVE_SYNTAX) {
152 /* cannot get more errors on a syntax error */
Michal Vasko50da8cd2023-03-10 08:38:59 +0100153 goto cleanup;
154 }
155 }
Michal Vaskoe0665742021-02-11 11:08:44 +0100156
Michal Vaskoa6139e02023-10-03 14:13:22 +0200157 if (parent && parsed.count) {
158 /* use the first parsed node */
Michal Vaskob36c56f2024-01-31 12:50:07 +0100159 if (first_p) {
160 *first_p = parsed.dnodes[0];
161 } else {
162 first_p = &parsed.dnodes[0];
163 }
Radek Krejci7931b192020-06-25 17:05:03 +0200164 }
165
Michal Vaskoe0665742021-02-11 11:08:44 +0100166 if (!(parse_opts & LYD_PARSE_ONLY)) {
167 /* validate data */
Michal Vasko9f6dd7c2023-04-06 08:46:30 +0200168 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 +0200169 &lydctx->ext_node, &lydctx->ext_val, NULL);
Michal Vasko9f6dd7c2023-04-06 08:46:30 +0200170 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
Michal Vaskoe0665742021-02-11 11:08:44 +0100171 }
Radek Krejci7931b192020-06-25 17:05:03 +0200172
Michal Vaskoe0665742021-02-11 11:08:44 +0100173 /* set the operation node */
174 if (op) {
175 *op = lydctx->op_node;
Radek Krejci1798aae2020-07-14 13:26:06 +0200176 }
177
178cleanup:
Michal Vaskoe0665742021-02-11 11:08:44 +0100179 if (lydctx) {
180 lydctx->free(lydctx);
Radek Krejci1798aae2020-07-14 13:26:06 +0200181 }
Michal Vaskoe0665742021-02-11 11:08:44 +0100182 if (rc) {
183 if (parent) {
184 /* free all the parsed subtrees */
185 for (i = 0; i < parsed.count; ++i) {
186 lyd_free_tree(parsed.dnodes[i]);
187 }
188 } else {
189 /* free everything */
190 lyd_free_all(*first_p);
191 *first_p = NULL;
192 }
Michal Vaskoddd76592022-01-17 13:34:48 +0100193 } else if (subtree_sibling) {
194 rc = LY_ENOT;
Michal Vaskoe0665742021-02-11 11:08:44 +0100195 }
196 ly_set_erase(&parsed, NULL);
197 return rc;
Radek Krejci7931b192020-06-25 17:05:03 +0200198}
199
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100200LIBYANG_API_DEF LY_ERR
Radek Krejcif16e2542021-02-17 15:39:23 +0100201lyd_parse_ext_data(const struct lysc_ext_instance *ext, struct lyd_node *parent, struct ly_in *in, LYD_FORMAT format,
202 uint32_t parse_options, uint32_t validate_options, struct lyd_node **tree)
203{
204 const struct ly_ctx *ctx = ext ? ext->module->ctx : NULL;
205
206 LY_CHECK_ARG_RET(ctx, ext, 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
210 return lyd_parse(ctx, ext, parent, tree, in, format, parse_options, validate_options, NULL);
211}
212
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100213LIBYANG_API_DEF LY_ERR
Michal Vaskoe0665742021-02-11 11:08:44 +0100214lyd_parse_data(const struct ly_ctx *ctx, struct lyd_node *parent, struct ly_in *in, LYD_FORMAT format,
215 uint32_t parse_options, uint32_t validate_options, struct lyd_node **tree)
216{
217 LY_CHECK_ARG_RET(ctx, ctx, in, parent || tree, LY_EINVAL);
218 LY_CHECK_ARG_RET(ctx, !(parse_options & ~LYD_PARSE_OPTS_MASK), LY_EINVAL);
219 LY_CHECK_ARG_RET(ctx, !(validate_options & ~LYD_VALIDATE_OPTS_MASK), LY_EINVAL);
220
Radek Krejcif16e2542021-02-17 15:39:23 +0100221 return lyd_parse(ctx, NULL, parent, tree, in, format, parse_options, validate_options, NULL);
Michal Vaskoe0665742021-02-11 11:08:44 +0100222}
223
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100224LIBYANG_API_DEF LY_ERR
Michal Vaskoe0665742021-02-11 11:08:44 +0100225lyd_parse_data_mem(const struct ly_ctx *ctx, const char *data, LYD_FORMAT format, uint32_t parse_options,
226 uint32_t validate_options, struct lyd_node **tree)
Radek Krejci7931b192020-06-25 17:05:03 +0200227{
228 LY_ERR ret;
229 struct ly_in *in;
230
231 LY_CHECK_RET(ly_in_new_memory(data, &in));
Michal Vaskoe0665742021-02-11 11:08:44 +0100232 ret = lyd_parse_data(ctx, NULL, in, format, parse_options, validate_options, tree);
Radek Krejci7931b192020-06-25 17:05:03 +0200233
234 ly_in_free(in, 0);
235 return ret;
236}
237
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100238LIBYANG_API_DEF LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200239lyd_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 +0200240 struct lyd_node **tree)
Radek Krejci7931b192020-06-25 17:05:03 +0200241{
242 LY_ERR ret;
243 struct ly_in *in;
244
245 LY_CHECK_RET(ly_in_new_fd(fd, &in));
Michal Vaskoe0665742021-02-11 11:08:44 +0100246 ret = lyd_parse_data(ctx, NULL, in, format, parse_options, validate_options, tree);
Radek Krejci7931b192020-06-25 17:05:03 +0200247
248 ly_in_free(in, 0);
249 return ret;
250}
251
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100252LIBYANG_API_DEF LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200253lyd_parse_data_path(const struct ly_ctx *ctx, const char *path, LYD_FORMAT format, uint32_t parse_options,
254 uint32_t validate_options, struct lyd_node **tree)
Radek Krejci7931b192020-06-25 17:05:03 +0200255{
256 LY_ERR ret;
257 struct ly_in *in;
258
259 LY_CHECK_RET(ly_in_new_filepath(path, 0, &in));
Michal Vaskoe0665742021-02-11 11:08:44 +0100260 ret = lyd_parse_data(ctx, NULL, in, format, parse_options, validate_options, tree);
Radek Krejci7931b192020-06-25 17:05:03 +0200261
262 ly_in_free(in, 0);
263 return ret;
264}
265
Radek Krejcif16e2542021-02-17 15:39:23 +0100266/**
267 * @brief Parse YANG data into an operation data tree, in case the extension instance is specified, keep the searching
268 * for schema nodes locked inside the extension instance.
269 *
270 * At least one of @p parent, @p tree, or @p op must always be set.
271 *
Michal Vasko820efe82023-05-12 15:47:43 +0200272 * Specific @p data_type values have different parameter meaning as mentioned for ::lyd_parse_op().
Radek Krejcif16e2542021-02-17 15:39:23 +0100273 *
274 * @param[in] ctx libyang context.
275 * @param[in] ext Extension instance providing the specific schema tree to match with the data being parsed.
276 * @param[in] parent Optional parent to connect the parsed nodes to.
277 * @param[in] in Input handle to read the input from.
278 * @param[in] format Expected format of the data in @p in.
279 * @param[in] data_type Expected operation to parse (@ref datatype).
280 * @param[out] tree Optional full parsed data tree. If @p parent is set, set to NULL.
281 * @param[out] op Optional parsed operation node.
282 * @return LY_ERR value.
283 * @return LY_ENOT if @p data_type is a NETCONF message and the root XML element is not the expected one.
284 */
285static LY_ERR
286lyd_parse_op_(const struct ly_ctx *ctx, const struct lysc_ext_instance *ext, struct lyd_node *parent,
287 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 +0200288{
Michal Vaskoe0665742021-02-11 11:08:44 +0100289 LY_ERR rc = LY_SUCCESS;
290 struct lyd_ctx *lydctx = NULL;
291 struct ly_set parsed = {0};
292 struct lyd_node *first = NULL, *envp = NULL;
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +0100293 uint32_t i, parse_opts, val_opts, int_opts = 0;
Michal Vasko820efe82023-05-12 15:47:43 +0200294 ly_bool proto_msg = 0;
Radek Krejci7931b192020-06-25 17:05:03 +0200295
Michal Vasko27fb0262021-02-23 09:42:01 +0100296 if (!ctx) {
297 ctx = LYD_CTX(parent);
298 }
Radek Krejci1798aae2020-07-14 13:26:06 +0200299 if (tree) {
300 *tree = NULL;
301 }
302 if (op) {
303 *op = NULL;
304 }
305
Radek Krejci7931b192020-06-25 17:05:03 +0200306 format = lyd_parse_get_format(in, format);
Radek Krejci7931b192020-06-25 17:05:03 +0200307
Michal Vasko63f3d842020-07-08 10:10:14 +0200308 /* remember input position */
309 in->func_start = in->current;
310
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +0100311 /* set parse and validation opts */
Michal Vasko140ede92022-05-10 09:27:30 +0200312 parse_opts = LYD_PARSE_ONLY | LYD_PARSE_STRICT;
Michal Vaskoe0665742021-02-11 11:08:44 +0100313 val_opts = 0;
314
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +0100315 switch (data_type) {
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +0100316 case LYD_TYPE_RPC_NETCONF:
317 case LYD_TYPE_NOTIF_NETCONF:
318 LY_CHECK_ARG_RET(ctx, format == LYD_XML, !parent, tree, op, LY_EINVAL);
Michal Vasko820efe82023-05-12 15:47:43 +0200319 proto_msg = 1;
320 break;
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +0100321 case LYD_TYPE_REPLY_NETCONF:
Michal Vasko820efe82023-05-12 15:47:43 +0200322 LY_CHECK_ARG_RET(ctx, format == LYD_XML, parent, parent->schema, parent->schema->nodetype & (LYS_RPC | LYS_ACTION),
323 tree, !op, LY_EINVAL);
324 proto_msg = 1;
325 break;
326 case LYD_TYPE_RPC_RESTCONF:
327 case LYD_TYPE_REPLY_RESTCONF:
328 LY_CHECK_ARG_RET(ctx, parent, parent->schema, parent->schema->nodetype & (LYS_RPC | LYS_ACTION), tree, !op, LY_EINVAL);
329 proto_msg = 1;
330 break;
331 case LYD_TYPE_NOTIF_RESTCONF:
332 LY_CHECK_ARG_RET(ctx, format == LYD_JSON, !parent, tree, op, LY_EINVAL);
333 proto_msg = 1;
334 break;
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +0100335
Michal Vasko820efe82023-05-12 15:47:43 +0200336 /* set internal opts */
337 case LYD_TYPE_RPC_YANG:
Michal Vasko5df28522023-08-25 08:04:37 +0200338 int_opts = LYD_INTOPT_RPC | LYD_INTOPT_ACTION | (parent ? LYD_INTOPT_WITH_SIBLINGS : LYD_INTOPT_NO_SIBLINGS);
Michal Vasko820efe82023-05-12 15:47:43 +0200339 break;
340 case LYD_TYPE_NOTIF_YANG:
Michal Vasko5df28522023-08-25 08:04:37 +0200341 int_opts = LYD_INTOPT_NOTIF | (parent ? LYD_INTOPT_WITH_SIBLINGS : LYD_INTOPT_NO_SIBLINGS);
Michal Vasko820efe82023-05-12 15:47:43 +0200342 break;
343 case LYD_TYPE_REPLY_YANG:
Michal Vasko5df28522023-08-25 08:04:37 +0200344 int_opts = LYD_INTOPT_REPLY | (parent ? LYD_INTOPT_WITH_SIBLINGS : LYD_INTOPT_NO_SIBLINGS);
Michal Vasko820efe82023-05-12 15:47:43 +0200345 break;
346 case LYD_TYPE_DATA_YANG:
347 LOGINT(ctx);
348 rc = LY_EINT;
349 goto cleanup;
350 }
351
352 /* parse a full protocol message */
353 if (proto_msg) {
354 if (format == LYD_XML) {
355 /* parse the NETCONF (or RESTCONF XML) message */
356 rc = lyd_parse_xml_netconf(ctx, ext, parent, &first, in, parse_opts, val_opts, data_type, &envp, &parsed, &lydctx);
357 } else {
358 /* parse the RESTCONF message */
359 rc = lyd_parse_json_restconf(ctx, ext, parent, &first, in, parse_opts, val_opts, data_type, &envp, &parsed, &lydctx);
360 }
Michal Vasko3a163bc2023-01-10 14:23:56 +0100361 if (rc) {
362 if (envp) {
363 /* special situation when the envelopes were parsed successfully */
364 *tree = envp;
365 }
Michal Vasko73792a12022-05-10 09:28:45 +0200366 goto cleanup;
Michal Vasko299d5d12021-02-16 16:36:37 +0100367 }
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +0100368
369 /* set out params correctly */
Michal Vasko472baa82022-12-01 16:17:41 +0100370 if (envp) {
371 /* special out param meaning */
372 *tree = envp;
373 } else {
374 *tree = parent ? NULL : first;
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +0100375 }
376 if (op) {
377 *op = lydctx->op_node;
378 }
379 goto cleanup;
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +0100380 }
381
382 /* parse the data */
383 switch (format) {
384 case LYD_XML:
385 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 +0100386 break;
Radek Krejci7931b192020-06-25 17:05:03 +0200387 case LYD_JSON:
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +0100388 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 +0100389 break;
Radek Krejci7931b192020-06-25 17:05:03 +0200390 case LYD_LYB:
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +0100391 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 +0100392 break;
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200393 case LYD_UNKNOWN:
Michal Vaskod74978f2021-02-12 11:59:36 +0100394 LOGARG(ctx, format);
395 rc = LY_EINVAL;
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200396 break;
Radek Krejci7931b192020-06-25 17:05:03 +0200397 }
Michal Vaskoe0665742021-02-11 11:08:44 +0100398 LY_CHECK_GOTO(rc, cleanup);
Radek Krejci7931b192020-06-25 17:05:03 +0200399
Michal Vaskoe0665742021-02-11 11:08:44 +0100400 /* set out params correctly */
401 if (tree) {
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +0100402 *tree = parent ? NULL : first;
Michal Vaskoe0665742021-02-11 11:08:44 +0100403 }
404 if (op) {
405 *op = lydctx->op_node;
406 }
407
408cleanup:
409 if (lydctx) {
410 lydctx->free(lydctx);
411 }
412 if (rc) {
Michal Vasko73792a12022-05-10 09:28:45 +0200413 /* free all the parsed nodes */
414 if (parsed.count) {
415 i = parsed.count;
416 do {
417 --i;
Michal Vaskoe0665742021-02-11 11:08:44 +0100418 lyd_free_tree(parsed.dnodes[i]);
Michal Vasko73792a12022-05-10 09:28:45 +0200419 } while (i);
420 }
Michal Vasko6fbf07b2023-08-08 13:54:48 +0200421 if (tree && !envp) {
Michal Vasko73792a12022-05-10 09:28:45 +0200422 *tree = NULL;
423 }
424 if (op) {
425 *op = NULL;
Michal Vaskoe0665742021-02-11 11:08:44 +0100426 }
427 }
428 ly_set_erase(&parsed, NULL);
429 return rc;
Radek Krejcie7b95092019-05-15 11:03:07 +0200430}
Radek Krejci084289f2019-07-09 17:35:30 +0200431
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100432LIBYANG_API_DEF LY_ERR
Radek Krejcif16e2542021-02-17 15:39:23 +0100433lyd_parse_op(const struct ly_ctx *ctx, struct lyd_node *parent, struct ly_in *in, LYD_FORMAT format,
434 enum lyd_type data_type, struct lyd_node **tree, struct lyd_node **op)
435{
436 LY_CHECK_ARG_RET(ctx, ctx || parent, in, data_type, parent || tree || op, LY_EINVAL);
437
438 return lyd_parse_op_(ctx, NULL, parent, in, format, data_type, tree, op);
439}
440
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100441LIBYANG_API_DEF LY_ERR
Radek Krejcif16e2542021-02-17 15:39:23 +0100442lyd_parse_ext_op(const struct lysc_ext_instance *ext, struct lyd_node *parent, struct ly_in *in, LYD_FORMAT format,
443 enum lyd_type data_type, struct lyd_node **tree, struct lyd_node **op)
444{
445 const struct ly_ctx *ctx = ext ? ext->module->ctx : NULL;
446
447 LY_CHECK_ARG_RET(ctx, ext, in, data_type, parent || tree || op, LY_EINVAL);
448
449 return lyd_parse_op_(ctx, ext, parent, in, format, data_type, tree, op);
450}
451
Michal Vasko90932a92020-02-12 14:33:03 +0100452struct lyd_node *
Michal Vaskob104f112020-07-17 09:54:54 +0200453lyd_insert_get_next_anchor(const struct lyd_node *first_sibling, const struct lyd_node *new_node)
Michal Vasko90932a92020-02-12 14:33:03 +0100454{
Michal Vaskob104f112020-07-17 09:54:54 +0200455 const struct lysc_node *schema, *sparent;
Michal Vasko90932a92020-02-12 14:33:03 +0100456 struct lyd_node *match = NULL;
Radek Krejci857189e2020-09-01 13:26:36 +0200457 ly_bool found;
Michal Vasko93b16062020-12-09 18:14:18 +0100458 uint32_t getnext_opts;
Michal Vasko90932a92020-02-12 14:33:03 +0100459
Michal Vaskob104f112020-07-17 09:54:54 +0200460 assert(new_node);
461
Michal Vasko9beceb82022-04-05 12:14:15 +0200462 if (!first_sibling || !new_node->schema || (LYD_CTX(first_sibling) != LYD_CTX(new_node))) {
Michal Vaskob104f112020-07-17 09:54:54 +0200463 /* insert at the end, no next anchor */
Michal Vasko90932a92020-02-12 14:33:03 +0100464 return NULL;
465 }
466
Michal Vasko93b16062020-12-09 18:14:18 +0100467 getnext_opts = 0;
Michal Vaskod1e53b92021-01-28 13:11:06 +0100468 if (new_node->schema->flags & LYS_IS_OUTPUT) {
Michal Vasko93b16062020-12-09 18:14:18 +0100469 getnext_opts = LYS_GETNEXT_OUTPUT;
470 }
471
Michal Vaskoa2756f02021-07-09 13:20:28 +0200472 if (first_sibling->parent && first_sibling->parent->schema && first_sibling->parent->children_ht) {
Michal Vaskob104f112020-07-17 09:54:54 +0200473 /* find the anchor using hashes */
474 sparent = first_sibling->parent->schema;
Michal Vasko93b16062020-12-09 18:14:18 +0100475 schema = lys_getnext(new_node->schema, sparent, NULL, getnext_opts);
Michal Vaskob104f112020-07-17 09:54:54 +0200476 while (schema) {
477 /* keep trying to find the first existing instance of the closest following schema sibling,
478 * otherwise return NULL - inserting at the end */
479 if (!lyd_find_sibling_schema(first_sibling, schema, &match)) {
480 break;
481 }
482
Michal Vasko93b16062020-12-09 18:14:18 +0100483 schema = lys_getnext(schema, sparent, NULL, getnext_opts);
Michal Vaskob104f112020-07-17 09:54:54 +0200484 }
485 } else {
486 /* find the anchor without hashes */
487 match = (struct lyd_node *)first_sibling;
Michal Vasko3a708622021-07-15 14:20:10 +0200488 sparent = lysc_data_parent(new_node->schema);
489 if (!sparent) {
Michal Vaskob104f112020-07-17 09:54:54 +0200490 /* we are in top-level, skip all the data from preceding modules */
491 LY_LIST_FOR(match, match) {
492 if (!match->schema || (strcmp(lyd_owner_module(match)->name, lyd_owner_module(new_node)->name) >= 0)) {
493 break;
494 }
495 }
496 }
497
498 /* get the first schema sibling */
Michal Vasko93b16062020-12-09 18:14:18 +0100499 schema = lys_getnext(NULL, sparent, new_node->schema->module->compiled, getnext_opts);
Michal Vasko9a463472023-08-08 09:43:33 +0200500 if (!schema) {
Michal Vasko60d929e2023-08-21 11:54:42 +0200501 /* must be a top-level extension instance data, no anchor */
502 return NULL;
Michal Vasko9a463472023-08-08 09:43:33 +0200503 }
Michal Vaskob104f112020-07-17 09:54:54 +0200504
505 found = 0;
506 LY_LIST_FOR(match, match) {
507 if (!match->schema || (lyd_owner_module(match) != lyd_owner_module(new_node))) {
508 /* we have found an opaque node, which must be at the end, so use it OR
509 * modules do not match, so we must have traversed all the data from new_node module (if any),
510 * we have found the first node of the next module, that is what we want */
511 break;
512 }
513
514 /* skip schema nodes until we find the instantiated one */
515 while (!found) {
516 if (new_node->schema == schema) {
517 /* we have found the schema of the new node, continue search to find the first
518 * data node with a different schema (after our schema) */
519 found = 1;
520 break;
521 }
522 if (match->schema == schema) {
523 /* current node (match) is a data node still before the new node, continue search in data */
524 break;
525 }
Michal Vasko9a463472023-08-08 09:43:33 +0200526
Michal Vasko93b16062020-12-09 18:14:18 +0100527 schema = lys_getnext(schema, sparent, new_node->schema->module->compiled, getnext_opts);
Michal Vasko9a463472023-08-08 09:43:33 +0200528 if (!schema) {
Michal Vasko60d929e2023-08-21 11:54:42 +0200529 /* must be a top-level extension instance data, no anchor */
530 return NULL;
Michal Vasko9a463472023-08-08 09:43:33 +0200531 }
Michal Vaskob104f112020-07-17 09:54:54 +0200532 }
533
534 if (found && (match->schema != new_node->schema)) {
535 /* find the next node after we have found our node schema data instance */
536 break;
537 }
538 }
Michal Vasko90932a92020-02-12 14:33:03 +0100539 }
540
541 return match;
542}
543
Michal Vaskoc61dd062022-06-07 11:01:28 +0200544void
Michal Vaskof03ed032020-03-04 13:31:44 +0100545lyd_insert_after_node(struct lyd_node *sibling, struct lyd_node *node)
Michal Vasko90932a92020-02-12 14:33:03 +0100546{
Michal Vasko0249f7c2020-03-05 16:36:40 +0100547 struct lyd_node_inner *par;
548
Michal Vasko90932a92020-02-12 14:33:03 +0100549 assert(!node->next && (node->prev == node));
550
551 node->next = sibling->next;
552 node->prev = sibling;
553 sibling->next = node;
554 if (node->next) {
555 /* sibling had a succeeding node */
556 node->next->prev = node;
557 } else {
558 /* sibling was last, find first sibling and change its prev */
559 if (sibling->parent) {
560 sibling = sibling->parent->child;
561 } else {
Michal Vaskod989ba02020-08-24 10:59:24 +0200562 for ( ; sibling->prev->next != node; sibling = sibling->prev) {}
Michal Vasko90932a92020-02-12 14:33:03 +0100563 }
564 sibling->prev = node;
565 }
566 node->parent = sibling->parent;
Michal Vasko0249f7c2020-03-05 16:36:40 +0100567
Michal Vasko9f96a052020-03-10 09:41:45 +0100568 for (par = node->parent; par; par = par->parent) {
569 if ((par->flags & LYD_DEFAULT) && !(node->flags & LYD_DEFAULT)) {
570 /* remove default flags from NP containers */
Michal Vasko0249f7c2020-03-05 16:36:40 +0100571 par->flags &= ~LYD_DEFAULT;
572 }
573 }
Michal Vasko90932a92020-02-12 14:33:03 +0100574}
575
Michal Vaskoc61dd062022-06-07 11:01:28 +0200576void
Michal Vaskof03ed032020-03-04 13:31:44 +0100577lyd_insert_before_node(struct lyd_node *sibling, struct lyd_node *node)
Michal Vasko90932a92020-02-12 14:33:03 +0100578{
Michal Vasko0249f7c2020-03-05 16:36:40 +0100579 struct lyd_node_inner *par;
580
Michal Vasko90932a92020-02-12 14:33:03 +0100581 assert(!node->next && (node->prev == node));
582
583 node->next = sibling;
584 /* covers situation of sibling being first */
585 node->prev = sibling->prev;
586 sibling->prev = node;
587 if (node->prev->next) {
588 /* sibling had a preceding node */
589 node->prev->next = node;
590 } else if (sibling->parent) {
591 /* sibling was first and we must also change parent child pointer */
592 sibling->parent->child = node;
593 }
594 node->parent = sibling->parent;
Michal Vasko0249f7c2020-03-05 16:36:40 +0100595
Michal Vasko9f96a052020-03-10 09:41:45 +0100596 for (par = node->parent; par; par = par->parent) {
597 if ((par->flags & LYD_DEFAULT) && !(node->flags & LYD_DEFAULT)) {
598 /* remove default flags from NP containers */
Michal Vasko0249f7c2020-03-05 16:36:40 +0100599 par->flags &= ~LYD_DEFAULT;
600 }
601 }
Michal Vasko90932a92020-02-12 14:33:03 +0100602}
603
604/**
Michal Vaskob104f112020-07-17 09:54:54 +0200605 * @brief Insert node as the first and only child of a parent.
Michal Vasko90932a92020-02-12 14:33:03 +0100606 *
Michal Vasko9f96a052020-03-10 09:41:45 +0100607 * Handles inserting into NP containers and key-less lists.
608 *
Michal Vasko90932a92020-02-12 14:33:03 +0100609 * @param[in] parent Parent to insert into.
610 * @param[in] node Node to insert.
611 */
612static void
Michal Vaskob104f112020-07-17 09:54:54 +0200613lyd_insert_only_child(struct lyd_node *parent, struct lyd_node *node)
Michal Vasko90932a92020-02-12 14:33:03 +0100614{
615 struct lyd_node_inner *par;
616
Radek Krejcia1c1e542020-09-29 16:06:52 +0200617 assert(parent && !lyd_child(parent) && !node->next && (node->prev == node));
Michal Vasko52927e22020-03-16 17:26:14 +0100618 assert(!parent->schema || (parent->schema->nodetype & LYD_NODE_INNER));
Michal Vasko90932a92020-02-12 14:33:03 +0100619
620 par = (struct lyd_node_inner *)parent;
621
Michal Vaskob104f112020-07-17 09:54:54 +0200622 par->child = node;
Michal Vasko90932a92020-02-12 14:33:03 +0100623 node->parent = par;
Michal Vasko0249f7c2020-03-05 16:36:40 +0100624
Michal Vaskod989ba02020-08-24 10:59:24 +0200625 for ( ; par; par = par->parent) {
Michal Vasko9f96a052020-03-10 09:41:45 +0100626 if ((par->flags & LYD_DEFAULT) && !(node->flags & LYD_DEFAULT)) {
627 /* remove default flags from NP containers */
Michal Vasko0249f7c2020-03-05 16:36:40 +0100628 par->flags &= ~LYD_DEFAULT;
629 }
630 }
Michal Vasko751cb4d2020-07-14 12:25:28 +0200631}
Michal Vasko0249f7c2020-03-05 16:36:40 +0100632
Michal Vasko751cb4d2020-07-14 12:25:28 +0200633/**
634 * @brief Learn whether a list instance has all the keys.
635 *
636 * @param[in] list List instance to check.
637 * @return non-zero if all the keys were found,
638 * @return 0 otherwise.
639 */
640static int
641lyd_insert_has_keys(const struct lyd_node *list)
642{
643 const struct lyd_node *key;
644 const struct lysc_node *skey = NULL;
645
646 assert(list->schema->nodetype == LYS_LIST);
Radek Krejcia1c1e542020-09-29 16:06:52 +0200647 key = lyd_child(list);
Michal Vasko751cb4d2020-07-14 12:25:28 +0200648 while ((skey = lys_getnext(skey, list->schema, NULL, 0)) && (skey->flags & LYS_KEY)) {
649 if (!key || (key->schema != skey)) {
650 /* key missing */
651 return 0;
652 }
653
654 key = key->next;
655 }
656
657 /* all keys found */
658 return 1;
Michal Vasko90932a92020-02-12 14:33:03 +0100659}
660
661void
Michal Vasko6ee6f432021-07-16 09:49:14 +0200662lyd_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 +0100663{
Michal Vaskob104f112020-07-17 09:54:54 +0200664 struct lyd_node *anchor, *first_sibling;
Michal Vasko90932a92020-02-12 14:33:03 +0100665
Michal Vaskob104f112020-07-17 09:54:54 +0200666 /* inserting list without its keys is not supported */
667 assert((parent || first_sibling_p) && node && (node->hash || !node->schema));
Michal Vaskof756c942020-11-06 17:21:37 +0100668 assert(!parent || !parent->schema ||
669 (parent->schema->nodetype & (LYS_CONTAINER | LYS_LIST | LYS_RPC | LYS_ACTION | LYS_NOTIF)));
Michal Vasko9b368d32020-02-14 13:53:31 +0100670
Michal Vaskob104f112020-07-17 09:54:54 +0200671 if (!parent && first_sibling_p && (*first_sibling_p) && (*first_sibling_p)->parent) {
Michal Vasko9e685082021-01-29 14:49:09 +0100672 parent = lyd_parent(*first_sibling_p);
Michal Vasko9b368d32020-02-14 13:53:31 +0100673 }
Michal Vasko90932a92020-02-12 14:33:03 +0100674
Michal Vaskob104f112020-07-17 09:54:54 +0200675 /* get first sibling */
Michal Vasko9e685082021-01-29 14:49:09 +0100676 first_sibling = parent ? lyd_child(parent) : *first_sibling_p;
Michal Vasko9f96a052020-03-10 09:41:45 +0100677
Michal Vasko9beceb82022-04-05 12:14:15 +0200678 if (last || (first_sibling && (first_sibling->flags & LYD_EXT))) {
Michal Vasko6ee6f432021-07-16 09:49:14 +0200679 /* no next anchor */
680 anchor = NULL;
681 } else {
682 /* find the anchor, our next node, so we can insert before it */
683 anchor = lyd_insert_get_next_anchor(first_sibling, node);
Michal Vasko6a0bb252023-08-21 11:55:11 +0200684
685 /* cannot insert data node after opaque nodes */
Michal Vasko5c1d4c22023-12-14 10:45:58 +0100686 if (!anchor && node->schema && first_sibling && !first_sibling->prev->schema) {
Michal Vasko6a0bb252023-08-21 11:55:11 +0200687 anchor = first_sibling->prev;
688 while ((anchor != first_sibling) && !anchor->prev->schema) {
689 anchor = anchor->prev;
690 }
691 }
Michal Vasko6ee6f432021-07-16 09:49:14 +0200692 }
693
Michal Vaskob104f112020-07-17 09:54:54 +0200694 if (anchor) {
Michal Vasko6ee6f432021-07-16 09:49:14 +0200695 /* insert before the anchor */
Michal Vaskob104f112020-07-17 09:54:54 +0200696 lyd_insert_before_node(anchor, node);
Michal Vasko26123192020-11-09 21:02:34 +0100697 if (!parent && (*first_sibling_p == anchor)) {
698 /* move first sibling */
699 *first_sibling_p = node;
700 }
Michal Vaskob104f112020-07-17 09:54:54 +0200701 } else if (first_sibling) {
Michal Vasko6ee6f432021-07-16 09:49:14 +0200702 /* insert as the last node */
Michal Vaskob104f112020-07-17 09:54:54 +0200703 lyd_insert_after_node(first_sibling->prev, node);
704 } else if (parent) {
Michal Vasko6ee6f432021-07-16 09:49:14 +0200705 /* insert as the only child */
Michal Vaskob104f112020-07-17 09:54:54 +0200706 lyd_insert_only_child(parent, node);
Michal Vasko90932a92020-02-12 14:33:03 +0100707 } else {
Michal Vasko6ee6f432021-07-16 09:49:14 +0200708 /* insert as the only sibling */
Michal Vaskob104f112020-07-17 09:54:54 +0200709 *first_sibling_p = node;
710 }
711
712 /* insert into parent HT */
713 lyd_insert_hash(node);
714
715 /* finish hashes for our parent, if needed and possible */
Michal Vaskoa878a892023-08-18 12:22:07 +0200716 if (node->schema && (node->schema->flags & LYS_KEY) && parent && parent->schema && lyd_insert_has_keys(parent)) {
Michal Vaskob104f112020-07-17 09:54:54 +0200717 lyd_hash(parent);
718
719 /* now we can insert even the list into its parent HT */
720 lyd_insert_hash(parent);
Michal Vasko90932a92020-02-12 14:33:03 +0100721 }
Michal Vasko90932a92020-02-12 14:33:03 +0100722}
723
Michal Vasko717a4c32020-12-07 09:40:10 +0100724/**
725 * @brief Check schema place of a node to be inserted.
726 *
727 * @param[in] parent Schema node of the parent data node.
728 * @param[in] sibling Schema node of a sibling data node.
729 * @param[in] schema Schema node if the data node to be inserted.
730 * @return LY_SUCCESS on success.
731 * @return LY_EINVAL if the place is invalid.
732 */
Michal Vaskof03ed032020-03-04 13:31:44 +0100733static LY_ERR
Michal Vasko717a4c32020-12-07 09:40:10 +0100734lyd_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 +0100735{
736 const struct lysc_node *par2;
737
Michal Vasko62ed12d2020-05-21 10:08:25 +0200738 assert(!parent || !(parent->nodetype & (LYS_CASE | LYS_CHOICE)));
Michal Vasko717a4c32020-12-07 09:40:10 +0100739 assert(!sibling || !(sibling->nodetype & (LYS_CASE | LYS_CHOICE)));
740 assert(!schema || !(schema->nodetype & (LYS_CASE | LYS_CHOICE)));
Michal Vaskof03ed032020-03-04 13:31:44 +0100741
Michal Vasko717a4c32020-12-07 09:40:10 +0100742 if (!schema || (!parent && !sibling)) {
Michal Vasko71e795e2020-12-04 16:27:37 +0100743 /* opaque nodes can be inserted wherever */
744 return LY_SUCCESS;
745 }
746
Michal Vasko717a4c32020-12-07 09:40:10 +0100747 if (!parent) {
748 parent = lysc_data_parent(sibling);
749 }
750
Michal Vaskof03ed032020-03-04 13:31:44 +0100751 /* find schema parent */
Michal Vasko62ed12d2020-05-21 10:08:25 +0200752 par2 = lysc_data_parent(schema);
Michal Vaskof03ed032020-03-04 13:31:44 +0100753
754 if (parent) {
755 /* inner node */
756 if (par2 != parent) {
Michal Vaskob104f112020-07-17 09:54:54 +0200757 LOGERR(schema->module->ctx, LY_EINVAL, "Cannot insert, parent of \"%s\" is not \"%s\".", schema->name,
Michal Vasko69730152020-10-09 16:30:07 +0200758 parent->name);
Michal Vaskof03ed032020-03-04 13:31:44 +0100759 return LY_EINVAL;
760 }
761 } else {
762 /* top-level node */
763 if (par2) {
Radek Krejcif6d14cb2020-07-02 16:11:45 +0200764 LOGERR(schema->module->ctx, LY_EINVAL, "Cannot insert, node \"%s\" is not top-level.", schema->name);
Michal Vaskof03ed032020-03-04 13:31:44 +0100765 return LY_EINVAL;
766 }
767 }
768
769 return LY_SUCCESS;
770}
771
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100772LIBYANG_API_DEF LY_ERR
Michal Vaskob104f112020-07-17 09:54:54 +0200773lyd_insert_child(struct lyd_node *parent, struct lyd_node *node)
Michal Vaskof03ed032020-03-04 13:31:44 +0100774{
775 struct lyd_node *iter;
776
Michal Vasko0ab974d2021-02-24 13:18:26 +0100777 LY_CHECK_ARG_RET(NULL, parent, node, !parent->schema || (parent->schema->nodetype & LYD_NODE_INNER), LY_EINVAL);
Michal Vasko892f5bf2021-11-24 10:41:05 +0100778 LY_CHECK_CTX_EQUAL_RET(LYD_CTX(parent), LYD_CTX(node), LY_EINVAL);
Michal Vaskof03ed032020-03-04 13:31:44 +0100779
Michal Vasko717a4c32020-12-07 09:40:10 +0100780 LY_CHECK_RET(lyd_insert_check_schema(parent->schema, NULL, node->schema));
Michal Vaskof03ed032020-03-04 13:31:44 +0100781
Michal Vasko0ab974d2021-02-24 13:18:26 +0100782 if (node->schema && (node->schema->flags & LYS_KEY)) {
Michal Vaskoddd76592022-01-17 13:34:48 +0100783 LOGERR(LYD_CTX(parent), LY_EINVAL, "Cannot insert key \"%s\".", node->schema->name);
Michal Vaskof03ed032020-03-04 13:31:44 +0100784 return LY_EINVAL;
785 }
786
787 if (node->parent || node->prev->next) {
Michal Vasko2e784f82024-01-11 09:51:22 +0100788 lyd_unlink(node);
Michal Vaskof03ed032020-03-04 13:31:44 +0100789 }
790
791 while (node) {
792 iter = node->next;
Michal Vasko2e784f82024-01-11 09:51:22 +0100793 lyd_unlink(node);
Michal Vasko6ee6f432021-07-16 09:49:14 +0200794 lyd_insert_node(parent, NULL, node, 0);
Michal Vaskof03ed032020-03-04 13:31:44 +0100795 node = iter;
796 }
797 return LY_SUCCESS;
798}
799
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100800LIBYANG_API_DEF LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +0200801lyplg_ext_insert(struct lyd_node *parent, struct lyd_node *first)
Michal Vaskoddd76592022-01-17 13:34:48 +0100802{
803 struct lyd_node *iter;
804
805 LY_CHECK_ARG_RET(NULL, parent, first, !first->parent, !first->prev->next,
806 !parent->schema || (parent->schema->nodetype & LYD_NODE_INNER), LY_EINVAL);
807
808 if (first->schema && (first->schema->flags & LYS_KEY)) {
809 LOGERR(LYD_CTX(parent), LY_EINVAL, "Cannot insert key \"%s\".", first->schema->name);
810 return LY_EINVAL;
811 }
812
813 while (first) {
814 iter = first->next;
Michal Vasko2e784f82024-01-11 09:51:22 +0100815 lyd_unlink(first);
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100816 lyd_insert_node(parent, NULL, first, 1);
Michal Vaskoddd76592022-01-17 13:34:48 +0100817 first = iter;
818 }
819 return LY_SUCCESS;
820}
821
822LIBYANG_API_DEF LY_ERR
Michal Vaskob104f112020-07-17 09:54:54 +0200823lyd_insert_sibling(struct lyd_node *sibling, struct lyd_node *node, struct lyd_node **first)
Michal Vaskob1b5c262020-03-05 14:29:47 +0100824{
825 struct lyd_node *iter;
826
Michal Vaskob104f112020-07-17 09:54:54 +0200827 LY_CHECK_ARG_RET(NULL, node, LY_EINVAL);
Michal Vaskob1b5c262020-03-05 14:29:47 +0100828
Michal Vaskob104f112020-07-17 09:54:54 +0200829 if (sibling) {
Michal Vasko717a4c32020-12-07 09:40:10 +0100830 LY_CHECK_RET(lyd_insert_check_schema(NULL, sibling->schema, node->schema));
Michal Vaskob1b5c262020-03-05 14:29:47 +0100831 }
832
Michal Vasko553d0b52020-12-04 16:27:52 +0100833 if (sibling == node) {
834 /* we need to keep the connection to siblings so we can insert into them */
835 sibling = sibling->prev;
836 }
837
Michal Vaskob1b5c262020-03-05 14:29:47 +0100838 if (node->parent || node->prev->next) {
Michal Vasko2e784f82024-01-11 09:51:22 +0100839 lyd_unlink(node);
Michal Vaskob1b5c262020-03-05 14:29:47 +0100840 }
841
842 while (node) {
Michal Vasko71e795e2020-12-04 16:27:37 +0100843 if (lysc_is_key(node->schema)) {
Michal Vaskob7be7a82020-08-20 09:09:04 +0200844 LOGERR(LYD_CTX(node), LY_EINVAL, "Cannot insert key \"%s\".", node->schema->name);
Michal Vaskob104f112020-07-17 09:54:54 +0200845 return LY_EINVAL;
846 }
847
Michal Vaskob1b5c262020-03-05 14:29:47 +0100848 iter = node->next;
Michal Vasko2e784f82024-01-11 09:51:22 +0100849 lyd_unlink(node);
Michal Vasko6ee6f432021-07-16 09:49:14 +0200850 lyd_insert_node(NULL, &sibling, node, 0);
Michal Vaskob1b5c262020-03-05 14:29:47 +0100851 node = iter;
852 }
Michal Vaskob1b5c262020-03-05 14:29:47 +0100853
Michal Vaskob104f112020-07-17 09:54:54 +0200854 if (first) {
855 /* find the first sibling */
856 *first = sibling;
857 while ((*first)->prev->next) {
858 *first = (*first)->prev;
Michal Vasko0249f7c2020-03-05 16:36:40 +0100859 }
860 }
861
862 return LY_SUCCESS;
863}
864
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100865LIBYANG_API_DEF LY_ERR
Michal Vaskof03ed032020-03-04 13:31:44 +0100866lyd_insert_before(struct lyd_node *sibling, struct lyd_node *node)
867{
Michal Vaskobce230b2022-04-19 09:55:31 +0200868 LY_CHECK_ARG_RET(NULL, sibling, node, sibling != node, LY_EINVAL);
Michal Vasko892f5bf2021-11-24 10:41:05 +0100869 LY_CHECK_CTX_EQUAL_RET(LYD_CTX(sibling), LYD_CTX(node), LY_EINVAL);
Michal Vaskof03ed032020-03-04 13:31:44 +0100870
Michal Vasko717a4c32020-12-07 09:40:10 +0100871 LY_CHECK_RET(lyd_insert_check_schema(NULL, sibling->schema, node->schema));
Michal Vaskof03ed032020-03-04 13:31:44 +0100872
Michal Vaskoab7a2bf2022-04-01 14:37:54 +0200873 if (node->schema && (!(node->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) || !(node->schema->flags & LYS_ORDBY_USER))) {
Michal Vaskob7be7a82020-08-20 09:09:04 +0200874 LOGERR(LYD_CTX(sibling), LY_EINVAL, "Can be used only for user-ordered nodes.");
Michal Vaskof03ed032020-03-04 13:31:44 +0100875 return LY_EINVAL;
876 }
Michal Vasko5c0b27a2022-04-19 09:56:02 +0200877 if (node->schema && sibling->schema && (node->schema != sibling->schema)) {
878 LOGERR(LYD_CTX(sibling), LY_EINVAL, "Cannot insert before a different schema node instance.");
Michal Vasko7508ef22022-02-22 14:13:10 +0100879 return LY_EINVAL;
880 }
Michal Vaskof03ed032020-03-04 13:31:44 +0100881
Michal Vasko2e784f82024-01-11 09:51:22 +0100882 lyd_unlink(node);
Michal Vasko4bc2ce32020-12-08 10:06:16 +0100883 lyd_insert_before_node(sibling, node);
884 lyd_insert_hash(node);
Michal Vaskof03ed032020-03-04 13:31:44 +0100885
Michal Vaskof03ed032020-03-04 13:31:44 +0100886 return LY_SUCCESS;
887}
888
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100889LIBYANG_API_DEF LY_ERR
Michal Vaskof03ed032020-03-04 13:31:44 +0100890lyd_insert_after(struct lyd_node *sibling, struct lyd_node *node)
891{
Michal Vaskobce230b2022-04-19 09:55:31 +0200892 LY_CHECK_ARG_RET(NULL, sibling, node, sibling != node, LY_EINVAL);
Michal Vasko892f5bf2021-11-24 10:41:05 +0100893 LY_CHECK_CTX_EQUAL_RET(LYD_CTX(sibling), LYD_CTX(node), LY_EINVAL);
Michal Vaskof03ed032020-03-04 13:31:44 +0100894
Michal Vasko717a4c32020-12-07 09:40:10 +0100895 LY_CHECK_RET(lyd_insert_check_schema(NULL, sibling->schema, node->schema));
Michal Vaskof03ed032020-03-04 13:31:44 +0100896
Michal Vaskoab7a2bf2022-04-01 14:37:54 +0200897 if (node->schema && (!(node->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) || !(node->schema->flags & LYS_ORDBY_USER))) {
Michal Vaskob7be7a82020-08-20 09:09:04 +0200898 LOGERR(LYD_CTX(sibling), LY_EINVAL, "Can be used only for user-ordered nodes.");
Michal Vaskof03ed032020-03-04 13:31:44 +0100899 return LY_EINVAL;
900 }
Michal Vasko5c0b27a2022-04-19 09:56:02 +0200901 if (node->schema && sibling->schema && (node->schema != sibling->schema)) {
902 LOGERR(LYD_CTX(sibling), LY_EINVAL, "Cannot insert after a different schema node instance.");
Michal Vasko7508ef22022-02-22 14:13:10 +0100903 return LY_EINVAL;
904 }
Michal Vaskof03ed032020-03-04 13:31:44 +0100905
Michal Vasko2e784f82024-01-11 09:51:22 +0100906 lyd_unlink(node);
Michal Vasko4bc2ce32020-12-08 10:06:16 +0100907 lyd_insert_after_node(sibling, node);
908 lyd_insert_hash(node);
Michal Vaskof03ed032020-03-04 13:31:44 +0100909
Michal Vaskof03ed032020-03-04 13:31:44 +0100910 return LY_SUCCESS;
911}
912
Michal Vasko2e784f82024-01-11 09:51:22 +0100913void
914lyd_unlink(struct lyd_node *node)
Michal Vaskof03ed032020-03-04 13:31:44 +0100915{
916 struct lyd_node *iter;
917
918 if (!node) {
919 return;
920 }
921
Michal Vaskob104f112020-07-17 09:54:54 +0200922 /* update hashes while still linked into the tree */
923 lyd_unlink_hash(node);
924
stewegf9041a22024-01-18 13:29:12 +0100925 /* unlink leafref nodes */
926 if (node->schema && (node->schema->nodetype & LYD_NODE_TERM)) {
927 lyd_free_leafref_nodes((struct lyd_node_term *)node);
928 }
929
Michal Vaskof03ed032020-03-04 13:31:44 +0100930 /* unlink from siblings */
931 if (node->prev->next) {
932 node->prev->next = node->next;
933 }
934 if (node->next) {
935 node->next->prev = node->prev;
936 } else {
937 /* unlinking the last node */
938 if (node->parent) {
939 iter = node->parent->child;
940 } else {
941 iter = node->prev;
942 while (iter->prev != node) {
943 iter = iter->prev;
944 }
945 }
946 /* update the "last" pointer from the first node */
947 iter->prev = node->prev;
948 }
949
950 /* unlink from parent */
951 if (node->parent) {
952 if (node->parent->child == node) {
953 /* the node is the first child */
954 node->parent->child = node->next;
955 }
956
Michal Vaskoab49dbe2020-07-17 12:32:47 +0200957 /* check for NP container whether its last non-default node is not being unlinked */
Michal Vasko4754d4a2022-12-01 10:11:21 +0100958 lyd_cont_set_dflt(lyd_parent(node));
Michal Vaskoab49dbe2020-07-17 12:32:47 +0200959
Michal Vaskof03ed032020-03-04 13:31:44 +0100960 node->parent = NULL;
961 }
962
963 node->next = NULL;
964 node->prev = node;
965}
966
Michal Vasko2e784f82024-01-11 09:51:22 +0100967LIBYANG_API_DEF void
968lyd_unlink_siblings(struct lyd_node *node)
969{
970 struct lyd_node *next, *elem, *first = NULL;
971
972 LY_LIST_FOR_SAFE(node, next, elem) {
973 if (lysc_is_key(elem->schema) && elem->parent) {
974 LOGERR(LYD_CTX(elem), LY_EINVAL, "Cannot unlink a list key \"%s\", unlink the list instance instead.",
975 LYD_NAME(elem));
976 return;
977 }
978
979 lyd_unlink(elem);
980 lyd_insert_node(NULL, &first, elem, 1);
981 }
982}
983
984LIBYANG_API_DEF void
985lyd_unlink_tree(struct lyd_node *node)
986{
987 if (node && lysc_is_key(node->schema) && node->parent) {
988 LOGERR(LYD_CTX(node), LY_EINVAL, "Cannot unlink a list key \"%s\", unlink the list instance instead.",
989 LYD_NAME(node));
990 return;
991 }
992
993 lyd_unlink(node);
994}
995
Michal Vaskoa5da3292020-08-12 13:10:50 +0200996void
Michal Vasko871a0252020-11-11 18:35:24 +0100997lyd_insert_meta(struct lyd_node *parent, struct lyd_meta *meta, ly_bool clear_dflt)
Radek Krejci1798aae2020-07-14 13:26:06 +0200998{
999 struct lyd_meta *last, *iter;
1000
1001 assert(parent);
Michal Vaskoa5da3292020-08-12 13:10:50 +02001002
1003 if (!meta) {
1004 return;
1005 }
Radek Krejci1798aae2020-07-14 13:26:06 +02001006
1007 for (iter = meta; iter; iter = iter->next) {
1008 iter->parent = parent;
1009 }
1010
1011 /* insert as the last attribute */
1012 if (parent->meta) {
Radek Krejci1e008d22020-08-17 11:37:37 +02001013 for (last = parent->meta; last->next; last = last->next) {}
Radek Krejci1798aae2020-07-14 13:26:06 +02001014 last->next = meta;
1015 } else {
1016 parent->meta = meta;
1017 }
1018
1019 /* remove default flags from NP containers */
Michal Vasko871a0252020-11-11 18:35:24 +01001020 while (clear_dflt && parent && (parent->schema->nodetype == LYS_CONTAINER) && (parent->flags & LYD_DEFAULT)) {
Radek Krejci1798aae2020-07-14 13:26:06 +02001021 parent->flags &= ~LYD_DEFAULT;
Michal Vasko9e685082021-01-29 14:49:09 +01001022 parent = lyd_parent(parent);
Radek Krejci1798aae2020-07-14 13:26:06 +02001023 }
Radek Krejci1798aae2020-07-14 13:26:06 +02001024}
1025
1026LY_ERR
Michal Vasko9f96a052020-03-10 09:41:45 +01001027lyd_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 +02001028 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 +01001029 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 +01001030{
Radek Krejci2efc45b2020-12-22 16:25:44 +01001031 LY_ERR ret = LY_SUCCESS;
Michal Vasko90932a92020-02-12 14:33:03 +01001032 struct lysc_ext_instance *ant = NULL;
Michal Vasko193dacd2022-10-13 08:43:05 +02001033 const struct lysc_type *ant_type;
Michal Vasko9f96a052020-03-10 09:41:45 +01001034 struct lyd_meta *mt, *last;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001035 LY_ARRAY_COUNT_TYPE u;
Michal Vasko90932a92020-02-12 14:33:03 +01001036
Michal Vasko9f96a052020-03-10 09:41:45 +01001037 assert((parent || meta) && mod);
Michal Vasko6f4cbb62020-02-28 11:15:47 +01001038
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001039 LY_ARRAY_FOR(mod->compiled->exts, u) {
stewega9756fc2024-02-16 09:40:14 +01001040 if (mod->compiled->exts[u].def->plugin && !strncmp(mod->compiled->exts[u].def->plugin->id, "ly2 metadata", 12) &&
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001041 !ly_strncmp(mod->compiled->exts[u].argument, name, name_len)) {
Michal Vasko90932a92020-02-12 14:33:03 +01001042 /* we have the annotation definition */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001043 ant = &mod->compiled->exts[u];
Michal Vasko90932a92020-02-12 14:33:03 +01001044 break;
1045 }
1046 }
1047 if (!ant) {
1048 /* attribute is not defined as a metadata annotation (RFC 7952) */
Radek Krejci2efc45b2020-12-22 16:25:44 +01001049 LOGVAL(mod->ctx, LYVE_REFERENCE, "Annotation definition for attribute \"%s:%.*s\" not found.",
Radek Krejci422afb12021-03-04 16:38:16 +01001050 mod->name, (int)name_len, name);
Radek Krejci2efc45b2020-12-22 16:25:44 +01001051 ret = LY_EINVAL;
1052 goto cleanup;
Michal Vasko90932a92020-02-12 14:33:03 +01001053 }
1054
Michal Vasko9f96a052020-03-10 09:41:45 +01001055 mt = calloc(1, sizeof *mt);
Radek Krejci2efc45b2020-12-22 16:25:44 +01001056 LY_CHECK_ERR_GOTO(!mt, LOGMEM(mod->ctx); ret = LY_EMEM, cleanup);
Michal Vasko9f96a052020-03-10 09:41:45 +01001057 mt->parent = parent;
1058 mt->annotation = ant;
Michal Vaskofbd037c2022-11-08 10:34:20 +01001059 lyplg_ext_get_storage(ant, LY_STMT_TYPE, sizeof ant_type, (const void **)&ant_type);
Michal Vasko989cdb42023-10-06 15:32:37 +02001060 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 +01001061 ctx_node, incomplete);
Radek Krejci2efc45b2020-12-22 16:25:44 +01001062 LY_CHECK_ERR_GOTO(ret, free(mt), cleanup);
1063 ret = lydict_insert(mod->ctx, name, name_len, &mt->name);
1064 LY_CHECK_ERR_GOTO(ret, free(mt), cleanup);
Michal Vasko90932a92020-02-12 14:33:03 +01001065
Michal Vasko6f4cbb62020-02-28 11:15:47 +01001066 /* insert as the last attribute */
1067 if (parent) {
Michal Vasko871a0252020-11-11 18:35:24 +01001068 lyd_insert_meta(parent, mt, clear_dflt);
Michal Vasko9f96a052020-03-10 09:41:45 +01001069 } else if (*meta) {
Radek Krejci1e008d22020-08-17 11:37:37 +02001070 for (last = *meta; last->next; last = last->next) {}
Michal Vasko9f96a052020-03-10 09:41:45 +01001071 last->next = mt;
Michal Vasko90932a92020-02-12 14:33:03 +01001072 }
1073
Michal Vasko9f96a052020-03-10 09:41:45 +01001074 if (meta) {
1075 *meta = mt;
Michal Vasko90932a92020-02-12 14:33:03 +01001076 }
Radek Krejci2efc45b2020-12-22 16:25:44 +01001077
1078cleanup:
Radek Krejci2efc45b2020-12-22 16:25:44 +01001079 return ret;
Michal Vasko90932a92020-02-12 14:33:03 +01001080}
1081
Michal Vaskoa5da3292020-08-12 13:10:50 +02001082void
1083lyd_insert_attr(struct lyd_node *parent, struct lyd_attr *attr)
1084{
1085 struct lyd_attr *last, *iter;
1086 struct lyd_node_opaq *opaq;
1087
1088 assert(parent && !parent->schema);
1089
1090 if (!attr) {
1091 return;
1092 }
1093
1094 opaq = (struct lyd_node_opaq *)parent;
1095 for (iter = attr; iter; iter = iter->next) {
1096 iter->parent = opaq;
1097 }
1098
1099 /* insert as the last attribute */
1100 if (opaq->attr) {
Radek Krejci1e008d22020-08-17 11:37:37 +02001101 for (last = opaq->attr; last->next; last = last->next) {}
Michal Vaskoa5da3292020-08-12 13:10:50 +02001102 last->next = attr;
1103 } else {
1104 opaq->attr = attr;
1105 }
1106}
1107
Michal Vasko52927e22020-03-16 17:26:14 +01001108LY_ERR
Michal Vaskofeca4fb2020-10-05 08:58:40 +02001109lyd_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 +01001110 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 +02001111 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 +01001112{
Radek Krejci011e4aa2020-09-04 15:22:31 +02001113 LY_ERR ret = LY_SUCCESS;
Radek Krejci1798aae2020-07-14 13:26:06 +02001114 struct lyd_attr *at, *last;
Michal Vasko52927e22020-03-16 17:26:14 +01001115
1116 assert(ctx && (parent || attr) && (!parent || !parent->schema));
Michal Vaskofeca4fb2020-10-05 08:58:40 +02001117 assert(name && name_len && format);
Michal Vasko52927e22020-03-16 17:26:14 +01001118
Michal Vasko2a3722d2021-06-16 11:52:39 +02001119 if (!value_len && (!dynamic || !*dynamic)) {
Michal Vasko52927e22020-03-16 17:26:14 +01001120 value = "";
1121 }
1122
1123 at = calloc(1, sizeof *at);
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01001124 LY_CHECK_ERR_RET(!at, LOGMEM(ctx); ly_free_prefix_data(format, val_prefix_data), LY_EMEM);
Radek Krejcid46e46a2020-09-15 14:22:42 +02001125
Michal Vaskoad92b672020-11-12 13:11:31 +01001126 LY_CHECK_GOTO(ret = lydict_insert(ctx, name, name_len, &at->name.name), finish);
Michal Vasko501af032020-11-11 20:27:44 +01001127 if (prefix_len) {
Michal Vaskoad92b672020-11-12 13:11:31 +01001128 LY_CHECK_GOTO(ret = lydict_insert(ctx, prefix, prefix_len, &at->name.prefix), finish);
Michal Vasko501af032020-11-11 20:27:44 +01001129 }
1130 if (module_key_len) {
Michal Vaskoad92b672020-11-12 13:11:31 +01001131 LY_CHECK_GOTO(ret = lydict_insert(ctx, module_key, module_key_len, &at->name.module_ns), finish);
Michal Vasko501af032020-11-11 20:27:44 +01001132 }
1133
Michal Vasko52927e22020-03-16 17:26:14 +01001134 if (dynamic && *dynamic) {
Radek Krejci011e4aa2020-09-04 15:22:31 +02001135 ret = lydict_insert_zc(ctx, (char *)value, &at->value);
1136 LY_CHECK_GOTO(ret, finish);
Michal Vasko52927e22020-03-16 17:26:14 +01001137 *dynamic = 0;
1138 } else {
Radek Krejci011e4aa2020-09-04 15:22:31 +02001139 LY_CHECK_GOTO(ret = lydict_insert(ctx, value, value_len, &at->value), finish);
Michal Vasko52927e22020-03-16 17:26:14 +01001140 }
Michal Vasko501af032020-11-11 20:27:44 +01001141 at->format = format;
1142 at->val_prefix_data = val_prefix_data;
1143 at->hints = hints;
Michal Vasko52927e22020-03-16 17:26:14 +01001144
1145 /* insert as the last attribute */
1146 if (parent) {
Michal Vaskoa5da3292020-08-12 13:10:50 +02001147 lyd_insert_attr(parent, at);
Michal Vasko52927e22020-03-16 17:26:14 +01001148 } else if (*attr) {
Radek Krejci1e008d22020-08-17 11:37:37 +02001149 for (last = *attr; last->next; last = last->next) {}
Michal Vasko52927e22020-03-16 17:26:14 +01001150 last->next = at;
1151 }
1152
Radek Krejci011e4aa2020-09-04 15:22:31 +02001153finish:
1154 if (ret) {
1155 lyd_free_attr_single(ctx, at);
1156 } else if (attr) {
Michal Vasko52927e22020-03-16 17:26:14 +01001157 *attr = at;
1158 }
1159 return LY_SUCCESS;
1160}
1161
aPiecek2f63f952021-03-30 12:22:18 +02001162/**
1163 * @brief Check the equality of the two schemas from different contexts.
1164 *
1165 * @param schema1 of first node.
1166 * @param schema2 of second node.
1167 * @return 1 if the schemas are equal otherwise 0.
1168 */
1169static ly_bool
1170lyd_compare_schema_equal(const struct lysc_node *schema1, const struct lysc_node *schema2)
1171{
1172 if (!schema1 && !schema2) {
1173 return 1;
1174 } else if (!schema1 || !schema2) {
1175 return 0;
1176 }
1177
1178 assert(schema1->module->ctx != schema2->module->ctx);
1179
1180 if (schema1->nodetype != schema2->nodetype) {
1181 return 0;
1182 }
1183
1184 if (strcmp(schema1->name, schema2->name)) {
1185 return 0;
1186 }
1187
1188 if (strcmp(schema1->module->name, schema2->module->name)) {
1189 return 0;
1190 }
1191
aPiecek2f63f952021-03-30 12:22:18 +02001192 return 1;
1193}
1194
1195/**
1196 * @brief Check the equality of the schemas for all parent nodes.
1197 *
1198 * Both nodes must be from different contexts.
1199 *
1200 * @param node1 Data of first node.
1201 * @param node2 Data of second node.
1202 * @return 1 if the all related parental schemas are equal otherwise 0.
1203 */
1204static ly_bool
1205lyd_compare_schema_parents_equal(const struct lyd_node *node1, const struct lyd_node *node2)
1206{
1207 const struct lysc_node *parent1, *parent2;
1208
1209 assert(node1 && node2);
1210
1211 for (parent1 = node1->schema->parent, parent2 = node2->schema->parent;
1212 parent1 && parent2;
1213 parent1 = parent1->parent, parent2 = parent2->parent) {
1214 if (!lyd_compare_schema_equal(parent1, parent2)) {
1215 return 0;
1216 }
1217 }
1218
1219 if (parent1 || parent2) {
1220 return 0;
1221 }
1222
1223 return 1;
1224}
1225
1226/**
Michal Vaskodf8ebf62022-11-10 10:33:28 +01001227 * @brief Compare 2 nodes values including opaque node values.
1228 *
1229 * @param[in] node1 First node to compare.
1230 * @param[in] node2 Second node to compare.
1231 * @return LY_SUCCESS if equal.
1232 * @return LY_ENOT if not equal.
1233 * @return LY_ERR on error.
1234 */
1235static LY_ERR
1236lyd_compare_single_value(const struct lyd_node *node1, const struct lyd_node *node2)
1237{
1238 const struct lyd_node_opaq *opaq1 = NULL, *opaq2 = NULL;
1239 const char *val1, *val2, *col;
1240 const struct lys_module *mod;
1241 char *val_dyn = NULL;
1242 LY_ERR rc = LY_SUCCESS;
1243
1244 if (!node1->schema) {
1245 opaq1 = (struct lyd_node_opaq *)node1;
1246 }
1247 if (!node2->schema) {
1248 opaq2 = (struct lyd_node_opaq *)node2;
1249 }
1250
1251 if (opaq1 && opaq2 && (opaq1->format == LY_VALUE_XML) && (opaq2->format == LY_VALUE_XML)) {
1252 /* opaque XML and opaque XML node */
1253 if (lyxml_value_compare(LYD_CTX(node1), opaq1->value, opaq1->val_prefix_data, LYD_CTX(node2), opaq2->value,
1254 opaq2->val_prefix_data)) {
1255 return LY_ENOT;
1256 }
1257 return LY_SUCCESS;
1258 }
1259
1260 /* get their values */
1261 if (opaq1 && ((opaq1->format == LY_VALUE_XML) || (opaq1->format == LY_VALUE_STR_NS)) && (col = strchr(opaq1->value, ':'))) {
1262 /* XML value with a prefix, try to transform it into a JSON (canonical) value */
1263 mod = ly_resolve_prefix(LYD_CTX(node1), opaq1->value, col - opaq1->value, opaq1->format, opaq1->val_prefix_data);
1264 if (!mod) {
1265 /* unable to compare */
1266 return LY_ENOT;
1267 }
1268
1269 if (asprintf(&val_dyn, "%s%s", mod->name, col) == -1) {
1270 LOGMEM(LYD_CTX(node1));
1271 return LY_EMEM;
1272 }
1273 val1 = val_dyn;
1274 } else {
1275 val1 = lyd_get_value(node1);
1276 }
1277 if (opaq2 && ((opaq2->format == LY_VALUE_XML) || (opaq2->format == LY_VALUE_STR_NS)) && (col = strchr(opaq2->value, ':'))) {
1278 mod = ly_resolve_prefix(LYD_CTX(node2), opaq2->value, col - opaq2->value, opaq2->format, opaq2->val_prefix_data);
1279 if (!mod) {
1280 return LY_ENOT;
1281 }
1282
1283 assert(!val_dyn);
1284 if (asprintf(&val_dyn, "%s%s", mod->name, col) == -1) {
1285 LOGMEM(LYD_CTX(node2));
1286 return LY_EMEM;
1287 }
1288 val2 = val_dyn;
1289 } else {
1290 val2 = lyd_get_value(node2);
1291 }
1292
1293 /* compare values */
1294 if (strcmp(val1, val2)) {
1295 rc = LY_ENOT;
1296 }
1297
1298 free(val_dyn);
1299 return rc;
1300}
1301
1302/**
Michal Vaskof277d362023-04-24 09:08:31 +02001303 * @brief Compare 2 data nodes if they are equivalent regarding the schema tree.
1304 *
1305 * Works correctly even if @p node1 and @p node2 have different contexts.
1306 *
1307 * @param[in] node1 The first node to compare.
1308 * @param[in] node2 The second node to compare.
1309 * @param[in] options Various @ref datacompareoptions.
1310 * @param[in] parental_schemas_checked Flag set if parent schemas were checked for match.
1311 * @return LY_SUCCESS if the nodes are equivalent.
1312 * @return LY_ENOT if the nodes are not equivalent.
aPiecek2f63f952021-03-30 12:22:18 +02001313 */
1314static LY_ERR
Michal Vaskof277d362023-04-24 09:08:31 +02001315lyd_compare_single_schema(const struct lyd_node *node1, const struct lyd_node *node2, uint32_t options,
Michal Vaskodf8ebf62022-11-10 10:33:28 +01001316 ly_bool parental_schemas_checked)
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001317{
aPiecek2f63f952021-03-30 12:22:18 +02001318 if (LYD_CTX(node1) == LYD_CTX(node2)) {
1319 /* same contexts */
Michal Vaskodf8ebf62022-11-10 10:33:28 +01001320 if (options & LYD_COMPARE_OPAQ) {
1321 if (lyd_node_schema(node1) != lyd_node_schema(node2)) {
1322 return LY_ENOT;
1323 }
1324 } else {
1325 if (node1->schema != node2->schema) {
1326 return LY_ENOT;
1327 }
aPiecek2f63f952021-03-30 12:22:18 +02001328 }
1329 } else {
1330 /* different contexts */
1331 if (!lyd_compare_schema_equal(node1->schema, node2->schema)) {
1332 return LY_ENOT;
1333 }
1334 if (!parental_schemas_checked) {
1335 if (!lyd_compare_schema_parents_equal(node1, node2)) {
1336 return LY_ENOT;
1337 }
1338 parental_schemas_checked = 1;
1339 }
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001340 }
1341
Michal Vaskof277d362023-04-24 09:08:31 +02001342 return LY_SUCCESS;
1343}
1344
1345/**
1346 * @brief Compare 2 data nodes if they are equivalent regarding the data they contain.
1347 *
1348 * Works correctly even if @p node1 and @p node2 have different contexts.
1349 *
1350 * @param[in] node1 The first node to compare.
1351 * @param[in] node2 The second node to compare.
1352 * @param[in] options Various @ref datacompareoptions.
1353 * @return LY_SUCCESS if the nodes are equivalent.
1354 * @return LY_ENOT if the nodes are not equivalent.
1355 */
1356static LY_ERR
1357lyd_compare_single_data(const struct lyd_node *node1, const struct lyd_node *node2, uint32_t options)
1358{
1359 const struct lyd_node *iter1, *iter2;
1360 struct lyd_node_any *any1, *any2;
1361 int len1, len2;
1362 LY_ERR r;
1363
Michal Vaskodf8ebf62022-11-10 10:33:28 +01001364 if (!(options & LYD_COMPARE_OPAQ) && (node1->hash != node2->hash)) {
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001365 return LY_ENOT;
1366 }
aPiecek2f63f952021-03-30 12:22:18 +02001367 /* 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 +02001368
Michal Vaskodf8ebf62022-11-10 10:33:28 +01001369 if (!node1->schema || !node2->schema) {
1370 if (!(options & LYD_COMPARE_OPAQ) && ((node1->schema && !node2->schema) || (!node1->schema && node2->schema))) {
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001371 return LY_ENOT;
1372 }
Michal Vasko7b0500d2023-03-20 15:22:46 +01001373 if ((!node1->schema && !node2->schema) || (node1->schema && (node1->schema->nodetype & LYD_NODE_TERM)) ||
1374 (node2->schema && (node2->schema->nodetype & LYD_NODE_TERM))) {
1375 /* compare values only if there are any to compare */
1376 if ((r = lyd_compare_single_value(node1, node2))) {
1377 return r;
1378 }
Michal Vasko52927e22020-03-16 17:26:14 +01001379 }
Michal Vaskodf8ebf62022-11-10 10:33:28 +01001380
Michal Vasko52927e22020-03-16 17:26:14 +01001381 if (options & LYD_COMPARE_FULL_RECURSION) {
Michal Vaskof277d362023-04-24 09:08:31 +02001382 return lyd_compare_siblings_(lyd_child(node1), lyd_child(node2), options, 1);
Michal Vasko52927e22020-03-16 17:26:14 +01001383 }
1384 return LY_SUCCESS;
1385 } else {
1386 switch (node1->schema->nodetype) {
1387 case LYS_LEAF:
1388 case LYS_LEAFLIST:
1389 if (options & LYD_COMPARE_DEFAULTS) {
1390 if ((node1->flags & LYD_DEFAULT) != (node2->flags & LYD_DEFAULT)) {
1391 return LY_ENOT;
1392 }
1393 }
Michal Vaskodf8ebf62022-11-10 10:33:28 +01001394 if ((r = lyd_compare_single_value(node1, node2))) {
1395 return r;
aPiecek2f63f952021-03-30 12:22:18 +02001396 }
1397
aPiecek2f63f952021-03-30 12:22:18 +02001398 return LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +01001399 case LYS_CONTAINER:
Michal Vaskoc8187dc2022-05-13 12:26:40 +02001400 case LYS_RPC:
1401 case LYS_ACTION:
1402 case LYS_NOTIF:
Michal Vaskoa38adc72023-04-25 10:14:28 +02001403 /* implicit container is always equal to a container with non-default descendants */
Michal Vasko52927e22020-03-16 17:26:14 +01001404 if (options & LYD_COMPARE_FULL_RECURSION) {
Michal Vaskof277d362023-04-24 09:08:31 +02001405 return lyd_compare_siblings_(lyd_child(node1), lyd_child(node2), options, 1);
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001406 }
1407 return LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +01001408 case LYS_LIST:
Michal Vasko9e685082021-01-29 14:49:09 +01001409 iter1 = lyd_child(node1);
1410 iter2 = lyd_child(node2);
Michal Vasko52927e22020-03-16 17:26:14 +01001411
Michal Vaskoee9b9482023-06-19 13:17:48 +02001412 if (options & LYD_COMPARE_FULL_RECURSION) {
Michal Vaskof277d362023-04-24 09:08:31 +02001413 return lyd_compare_siblings_(iter1, iter2, options, 1);
Michal Vaskoee9b9482023-06-19 13:17:48 +02001414 } else if (node1->schema->flags & LYS_KEYLESS) {
1415 /* always equal */
1416 return LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +01001417 }
Michal Vaskoee9b9482023-06-19 13:17:48 +02001418
1419 /* lists with keys, their equivalence is based on their keys */
1420 for (const struct lysc_node *key = lysc_node_child(node1->schema);
1421 key && (key->flags & LYS_KEY);
1422 key = key->next) {
1423 if (!iter1 || !iter2) {
1424 return (iter1 == iter2) ? LY_SUCCESS : LY_ENOT;
1425 }
1426 r = lyd_compare_single_schema(iter1, iter2, options, 1);
1427 LY_CHECK_RET(r);
1428 r = lyd_compare_single_data(iter1, iter2, options);
1429 LY_CHECK_RET(r);
1430
1431 iter1 = iter1->next;
1432 iter2 = iter2->next;
1433 }
1434
1435 return LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +01001436 case LYS_ANYXML:
1437 case LYS_ANYDATA:
Michal Vasko22df3f02020-08-24 13:29:22 +02001438 any1 = (struct lyd_node_any *)node1;
1439 any2 = (struct lyd_node_any *)node2;
Michal Vasko52927e22020-03-16 17:26:14 +01001440
1441 if (any1->value_type != any2->value_type) {
1442 return LY_ENOT;
1443 }
1444 switch (any1->value_type) {
1445 case LYD_ANYDATA_DATATREE:
Michal Vaskof277d362023-04-24 09:08:31 +02001446 return lyd_compare_siblings_(any1->value.tree, any2->value.tree, options, 1);
Michal Vasko52927e22020-03-16 17:26:14 +01001447 case LYD_ANYDATA_STRING:
1448 case LYD_ANYDATA_XML:
1449 case LYD_ANYDATA_JSON:
Michal Vasko3b4ec412022-09-22 15:24:14 +02001450 if ((!any1->value.str && any2->value.str) || (any1->value.str && !any2->value.str)) {
1451 return LY_ENOT;
1452 } else if (!any1->value.str && !any2->value.str) {
1453 return LY_SUCCESS;
1454 }
Michal Vasko52927e22020-03-16 17:26:14 +01001455 len1 = strlen(any1->value.str);
1456 len2 = strlen(any2->value.str);
Michal Vasko69730152020-10-09 16:30:07 +02001457 if ((len1 != len2) || strcmp(any1->value.str, any2->value.str)) {
Michal Vasko52927e22020-03-16 17:26:14 +01001458 return LY_ENOT;
1459 }
1460 return LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +01001461 case LYD_ANYDATA_LYB:
Michal Vasko60ea6352020-06-29 13:39:39 +02001462 len1 = lyd_lyb_data_length(any1->value.mem);
1463 len2 = lyd_lyb_data_length(any2->value.mem);
Michal Vasko2b97d472022-08-17 09:29:03 +02001464 if ((len1 == -1) || (len2 == -1) || (len1 != len2) || memcmp(any1->value.mem, any2->value.mem, len1)) {
Michal Vasko52927e22020-03-16 17:26:14 +01001465 return LY_ENOT;
1466 }
1467 return LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +01001468 }
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001469 }
1470 }
1471
Michal Vaskob7be7a82020-08-20 09:09:04 +02001472 LOGINT(LYD_CTX(node1));
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001473 return LY_EINT;
1474}
Radek Krejci22ebdba2019-07-25 13:59:43 +02001475
Michal Vaskof277d362023-04-24 09:08:31 +02001476/**
1477 * @brief Compare all siblings at a node level.
1478 *
1479 * @param[in] node1 First sibling list.
1480 * @param[in] node2 Second sibling list.
1481 * @param[in] options Various @ref datacompareoptions.
1482 * @param[in] parental_schemas_checked Flag set if parent schemas were checked for match.
1483 * @return LY_SUCCESS if equal.
1484 * @return LY_ENOT if not equal.
1485 * @return LY_ERR on error.
1486 */
1487static LY_ERR
1488lyd_compare_siblings_(const struct lyd_node *node1, const struct lyd_node *node2, uint32_t options,
1489 ly_bool parental_schemas_checked)
1490{
1491 LY_ERR r;
1492 const struct lyd_node *iter2;
1493
1494 while (node1 && node2) {
1495 /* schema match */
1496 r = lyd_compare_single_schema(node1, node2, options, parental_schemas_checked);
1497 LY_CHECK_RET(r);
1498
1499 if (node1->schema && (((node1->schema->nodetype == LYS_LIST) && !(node1->schema->flags & LYS_KEYLESS)) ||
1500 ((node1->schema->nodetype == LYS_LEAFLIST) && (node1->schema->flags & LYS_CONFIG_W))) &&
1501 (node1->schema->flags & LYS_ORDBY_SYSTEM)) {
1502 /* find a matching instance in case they are ordered differently */
1503 r = lyd_find_sibling_first(node2, node1, (struct lyd_node **)&iter2);
1504 if (r == LY_ENOTFOUND) {
1505 /* no matching instance, data not equal */
1506 r = LY_ENOT;
1507 }
1508 LY_CHECK_RET(r);
1509 } else {
1510 /* compare with the current node */
1511 iter2 = node2;
1512 }
1513
1514 /* data match */
1515 r = lyd_compare_single_data(node1, iter2, options | LYD_COMPARE_FULL_RECURSION);
1516 LY_CHECK_RET(r);
1517
1518 node1 = node1->next;
1519 node2 = node2->next;
1520 }
1521
1522 return (node1 || node2) ? LY_ENOT : LY_SUCCESS;
1523}
1524
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001525LIBYANG_API_DEF LY_ERR
aPiecek2f63f952021-03-30 12:22:18 +02001526lyd_compare_single(const struct lyd_node *node1, const struct lyd_node *node2, uint32_t options)
1527{
Michal Vaskof277d362023-04-24 09:08:31 +02001528 LY_ERR r;
1529
1530 if (!node1 || !node2) {
1531 return (node1 == node2) ? LY_SUCCESS : LY_ENOT;
1532 }
1533
1534 /* schema match */
1535 if ((r = lyd_compare_single_schema(node1, node2, options, 0))) {
1536 return r;
1537 }
1538
1539 /* data match */
1540 return lyd_compare_single_data(node1, node2, options);
aPiecek2f63f952021-03-30 12:22:18 +02001541}
1542
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001543LIBYANG_API_DEF LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02001544lyd_compare_siblings(const struct lyd_node *node1, const struct lyd_node *node2, uint32_t options)
Michal Vasko8f359bf2020-07-28 10:41:15 +02001545{
Michal Vaskof277d362023-04-24 09:08:31 +02001546 return lyd_compare_siblings_(node1, node2, options, 0);
Michal Vasko8f359bf2020-07-28 10:41:15 +02001547}
1548
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001549LIBYANG_API_DEF LY_ERR
Michal Vasko21725742020-06-29 11:49:25 +02001550lyd_compare_meta(const struct lyd_meta *meta1, const struct lyd_meta *meta2)
1551{
1552 if (!meta1 || !meta2) {
1553 if (meta1 == meta2) {
1554 return LY_SUCCESS;
1555 } else {
1556 return LY_ENOT;
1557 }
1558 }
1559
Michal Vaskoa8083062020-11-06 17:22:10 +01001560 if ((meta1->annotation->module->ctx != meta2->annotation->module->ctx) || (meta1->annotation != meta2->annotation)) {
Michal Vasko21725742020-06-29 11:49:25 +02001561 return LY_ENOT;
1562 }
1563
Michal Vasko21725742020-06-29 11:49:25 +02001564 return meta1->value.realtype->plugin->compare(&meta1->value, &meta2->value);
1565}
1566
Radek Krejci22ebdba2019-07-25 13:59:43 +02001567/**
Michal Vasko9cf62422021-07-01 13:29:32 +02001568 * @brief Create a copy of the attribute.
1569 *
1570 * @param[in] attr Attribute to copy.
1571 * @param[in] node Opaque where to append the new attribute.
1572 * @param[out] dup Optional created attribute copy.
1573 * @return LY_ERR value.
1574 */
1575static LY_ERR
1576lyd_dup_attr_single(const struct lyd_attr *attr, struct lyd_node *node, struct lyd_attr **dup)
1577{
1578 LY_ERR ret = LY_SUCCESS;
1579 struct lyd_attr *a, *last;
1580 struct lyd_node_opaq *opaq = (struct lyd_node_opaq *)node;
1581
1582 LY_CHECK_ARG_RET(NULL, attr, node, !node->schema, LY_EINVAL);
1583
1584 /* create a copy */
1585 a = calloc(1, sizeof *attr);
1586 LY_CHECK_ERR_RET(!a, LOGMEM(LYD_CTX(node)), LY_EMEM);
1587
1588 LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), attr->name.name, 0, &a->name.name), finish);
1589 LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), attr->name.prefix, 0, &a->name.prefix), finish);
1590 LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), attr->name.module_ns, 0, &a->name.module_ns), finish);
1591 LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), attr->value, 0, &a->value), finish);
1592 a->hints = attr->hints;
1593 a->format = attr->format;
1594 if (attr->val_prefix_data) {
1595 ret = ly_dup_prefix_data(LYD_CTX(node), attr->format, attr->val_prefix_data, &a->val_prefix_data);
1596 LY_CHECK_GOTO(ret, finish);
1597 }
1598
1599 /* insert as the last attribute */
1600 a->parent = opaq;
1601 if (opaq->attr) {
1602 for (last = opaq->attr; last->next; last = last->next) {}
1603 last->next = a;
1604 } else {
1605 opaq->attr = a;
1606 }
1607
1608finish:
1609 if (ret) {
1610 lyd_free_attr_single(LYD_CTX(node), a);
1611 } else if (dup) {
1612 *dup = a;
1613 }
1614 return LY_SUCCESS;
1615}
1616
1617/**
Michal Vaskoddd76592022-01-17 13:34:48 +01001618 * @brief Find @p schema equivalent in @p trg_ctx.
1619 *
1620 * @param[in] schema Schema node to find.
1621 * @param[in] trg_ctx Target context to search in.
1622 * @param[in] parent Data parent of @p schema, if any.
Michal Vaskoaf3df492022-12-02 14:03:52 +01001623 * @param[in] log Whether to log directly.
Michal Vaskoddd76592022-01-17 13:34:48 +01001624 * @param[out] trg_schema Found schema from @p trg_ctx to use.
1625 * @return LY_RRR value.
1626 */
1627static LY_ERR
Michal Vaskoaf3df492022-12-02 14:03:52 +01001628lyd_find_schema_ctx(const struct lysc_node *schema, const struct ly_ctx *trg_ctx, const struct lyd_node *parent,
1629 ly_bool log, const struct lysc_node **trg_schema)
Michal Vaskoddd76592022-01-17 13:34:48 +01001630{
Michal Vasko9beceb82022-04-05 12:14:15 +02001631 const struct lysc_node *src_parent = NULL, *trg_parent = NULL, *sp, *tp;
1632 const struct lys_module *trg_mod = NULL;
Michal Vaskoddd76592022-01-17 13:34:48 +01001633 char *path;
1634
1635 if (!schema) {
1636 /* opaque node */
1637 *trg_schema = NULL;
1638 return LY_SUCCESS;
1639 }
1640
Michal Vasko9beceb82022-04-05 12:14:15 +02001641 if (lysc_data_parent(schema) && parent && parent->schema) {
Michal Vaskoddd76592022-01-17 13:34:48 +01001642 /* start from schema parent */
Michal Vasko9beceb82022-04-05 12:14:15 +02001643 trg_parent = parent->schema;
1644 src_parent = lysc_data_parent(schema);
1645 }
1646
1647 do {
1648 /* find the next parent */
1649 sp = schema;
Michal Vaskob6808202022-12-02 14:04:23 +01001650 while (lysc_data_parent(sp) != src_parent) {
1651 sp = lysc_data_parent(sp);
Michal Vasko9beceb82022-04-05 12:14:15 +02001652 }
1653 src_parent = sp;
1654
1655 if (!src_parent->parent) {
1656 /* find the module first */
1657 trg_mod = ly_ctx_get_module_implemented(trg_ctx, src_parent->module->name);
1658 if (!trg_mod) {
Michal Vaskoaf3df492022-12-02 14:03:52 +01001659 if (log) {
1660 LOGERR(trg_ctx, LY_ENOTFOUND, "Module \"%s\" not present/implemented in the target context.",
1661 src_parent->module->name);
1662 }
Michal Vasko9beceb82022-04-05 12:14:15 +02001663 return LY_ENOTFOUND;
1664 }
1665 }
1666
1667 /* find the next parent */
1668 assert(trg_parent || trg_mod);
1669 tp = NULL;
1670 while ((tp = lys_getnext(tp, trg_parent, trg_mod ? trg_mod->compiled : NULL, 0))) {
1671 if (!strcmp(tp->name, src_parent->name) && !strcmp(tp->module->name, src_parent->module->name)) {
1672 break;
1673 }
1674 }
1675 if (!tp) {
1676 /* schema node not found */
Michal Vaskoaf3df492022-12-02 14:03:52 +01001677 if (log) {
1678 path = lysc_path(src_parent, LYSC_PATH_LOG, NULL, 0);
1679 LOGERR(trg_ctx, LY_ENOTFOUND, "Schema node \"%s\" not found in the target context.", path);
1680 free(path);
1681 }
Michal Vaskoddd76592022-01-17 13:34:48 +01001682 return LY_ENOTFOUND;
1683 }
Michal Vaskoddd76592022-01-17 13:34:48 +01001684
Michal Vasko9beceb82022-04-05 12:14:15 +02001685 trg_parent = tp;
1686 } while (schema != src_parent);
Michal Vaskoddd76592022-01-17 13:34:48 +01001687
Michal Vasko9beceb82022-04-05 12:14:15 +02001688 /* success */
1689 *trg_schema = trg_parent;
1690 return LY_SUCCESS;
Michal Vaskoddd76592022-01-17 13:34:48 +01001691}
1692
1693/**
Michal Vasko52927e22020-03-16 17:26:14 +01001694 * @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 +02001695 *
aPiecek55653c92023-11-09 13:43:19 +01001696 * Ignores ::LYD_DUP_WITH_PARENTS which is supposed to be handled by lyd_dup().
Radek Krejcif8b95172020-05-15 14:51:06 +02001697 *
Michal Vaskoddd76592022-01-17 13:34:48 +01001698 * @param[in] node Node to duplicate.
1699 * @param[in] trg_ctx Target context for duplicated nodes.
Radek Krejcif8b95172020-05-15 14:51:06 +02001700 * @param[in] parent Parent to insert into, NULL for top-level sibling.
Michal Vasko67177e52021-08-25 11:15:15 +02001701 * @param[in] insert_last Whether the duplicated node can be inserted as the last child of @p parent. Set for
1702 * recursive duplication as an optimization.
Radek Krejcif8b95172020-05-15 14:51:06 +02001703 * @param[in,out] first First sibling, NULL if no top-level sibling exist yet. Can be also NULL if @p parent is set.
1704 * @param[in] options Bitmask of options flags, see @ref dupoptions.
Michal Vaskoddd76592022-01-17 13:34:48 +01001705 * @param[out] dup_p Pointer where the created duplicated node is placed (besides connecting it to @p parent / @p first).
1706 * @return LY_ERR value.
Radek Krejci22ebdba2019-07-25 13:59:43 +02001707 */
Michal Vasko52927e22020-03-16 17:26:14 +01001708static LY_ERR
Michal Vaskoddd76592022-01-17 13:34:48 +01001709lyd_dup_r(const struct lyd_node *node, const struct ly_ctx *trg_ctx, struct lyd_node *parent, ly_bool insert_last,
1710 struct lyd_node **first, uint32_t options, struct lyd_node **dup_p)
Radek Krejci22ebdba2019-07-25 13:59:43 +02001711{
Michal Vasko52927e22020-03-16 17:26:14 +01001712 LY_ERR ret;
Radek Krejci22ebdba2019-07-25 13:59:43 +02001713 struct lyd_node *dup = NULL;
Michal Vasko61551fa2020-07-09 15:45:45 +02001714 struct lyd_meta *meta;
Michal Vasko9cf62422021-07-01 13:29:32 +02001715 struct lyd_attr *attr;
Michal Vasko61551fa2020-07-09 15:45:45 +02001716 struct lyd_node_any *any;
Michal Vaskoddd76592022-01-17 13:34:48 +01001717 const struct lysc_type *type;
1718 const char *val_can;
Radek Krejci22ebdba2019-07-25 13:59:43 +02001719
Michal Vasko52927e22020-03-16 17:26:14 +01001720 LY_CHECK_ARG_RET(NULL, node, LY_EINVAL);
Radek Krejci22ebdba2019-07-25 13:59:43 +02001721
Michal Vasko19175b62022-04-01 09:17:07 +02001722 if (node->flags & LYD_EXT) {
Michal Vasko53ac4dd2022-06-07 10:56:08 +02001723 if (options & LYD_DUP_NO_EXT) {
1724 /* no not duplicate this subtree */
1725 return LY_SUCCESS;
1726 }
1727
Michal Vasko19175b62022-04-01 09:17:07 +02001728 /* we need to use the same context */
1729 trg_ctx = LYD_CTX(node);
1730 }
1731
Michal Vasko52927e22020-03-16 17:26:14 +01001732 if (!node->schema) {
1733 dup = calloc(1, sizeof(struct lyd_node_opaq));
Michal Vaskoddd76592022-01-17 13:34:48 +01001734 ((struct lyd_node_opaq *)dup)->ctx = trg_ctx;
Michal Vasko52927e22020-03-16 17:26:14 +01001735 } else {
1736 switch (node->schema->nodetype) {
Michal Vasko1bf09392020-03-27 12:38:10 +01001737 case LYS_RPC:
Michal Vasko52927e22020-03-16 17:26:14 +01001738 case LYS_ACTION:
1739 case LYS_NOTIF:
1740 case LYS_CONTAINER:
1741 case LYS_LIST:
1742 dup = calloc(1, sizeof(struct lyd_node_inner));
1743 break;
1744 case LYS_LEAF:
1745 case LYS_LEAFLIST:
1746 dup = calloc(1, sizeof(struct lyd_node_term));
1747 break;
1748 case LYS_ANYDATA:
1749 case LYS_ANYXML:
1750 dup = calloc(1, sizeof(struct lyd_node_any));
1751 break;
1752 default:
Michal Vaskoddd76592022-01-17 13:34:48 +01001753 LOGINT(trg_ctx);
Michal Vasko52927e22020-03-16 17:26:14 +01001754 ret = LY_EINT;
1755 goto error;
1756 }
Radek Krejci22ebdba2019-07-25 13:59:43 +02001757 }
Michal Vaskoddd76592022-01-17 13:34:48 +01001758 LY_CHECK_ERR_GOTO(!dup, LOGMEM(trg_ctx); ret = LY_EMEM, error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02001759
Michal Vaskof6df0a02020-06-16 13:08:34 +02001760 if (options & LYD_DUP_WITH_FLAGS) {
1761 dup->flags = node->flags;
1762 } else {
Michal Vasko19175b62022-04-01 09:17:07 +02001763 dup->flags = (node->flags & (LYD_DEFAULT | LYD_EXT)) | LYD_NEW;
Michal Vaskof6df0a02020-06-16 13:08:34 +02001764 }
Igor Ryzhov9e7af662023-10-31 13:27:56 +02001765 if (options & LYD_DUP_WITH_PRIV) {
1766 dup->priv = node->priv;
1767 }
Michal Vaskoddd76592022-01-17 13:34:48 +01001768 if (trg_ctx == LYD_CTX(node)) {
1769 dup->schema = node->schema;
1770 } else {
Michal Vaskoaf3df492022-12-02 14:03:52 +01001771 ret = lyd_find_schema_ctx(node->schema, trg_ctx, parent, 1, &dup->schema);
Michal Vasko27f536f2022-04-01 09:17:30 +02001772 if (ret) {
1773 /* has no schema but is not an opaque node */
1774 free(dup);
1775 dup = NULL;
1776 goto error;
1777 }
Michal Vaskoddd76592022-01-17 13:34:48 +01001778 }
Michal Vasko52927e22020-03-16 17:26:14 +01001779 dup->prev = dup;
Radek Krejci22ebdba2019-07-25 13:59:43 +02001780
Michal Vasko9cf62422021-07-01 13:29:32 +02001781 /* duplicate metadata/attributes */
Michal Vasko25a32822020-07-09 15:48:22 +02001782 if (!(options & LYD_DUP_NO_META)) {
Michal Vasko9cf62422021-07-01 13:29:32 +02001783 if (!node->schema) {
1784 LY_LIST_FOR(((struct lyd_node_opaq *)node)->attr, attr) {
1785 LY_CHECK_GOTO(ret = lyd_dup_attr_single(attr, dup, NULL), error);
1786 }
1787 } else {
1788 LY_LIST_FOR(node->meta, meta) {
1789 LY_CHECK_GOTO(ret = lyd_dup_meta_single(meta, dup, NULL), error);
1790 }
Michal Vasko25a32822020-07-09 15:48:22 +02001791 }
1792 }
Radek Krejci22ebdba2019-07-25 13:59:43 +02001793
1794 /* nodetype-specific work */
Michal Vasko52927e22020-03-16 17:26:14 +01001795 if (!dup->schema) {
1796 struct lyd_node_opaq *opaq = (struct lyd_node_opaq *)dup;
1797 struct lyd_node_opaq *orig = (struct lyd_node_opaq *)node;
1798 struct lyd_node *child;
Radek Krejci22ebdba2019-07-25 13:59:43 +02001799
1800 if (options & LYD_DUP_RECURSIVE) {
1801 /* duplicate all the children */
1802 LY_LIST_FOR(orig->child, child) {
Michal Vaskoddd76592022-01-17 13:34:48 +01001803 LY_CHECK_GOTO(ret = lyd_dup_r(child, trg_ctx, dup, 1, NULL, options, NULL), error);
Michal Vasko52927e22020-03-16 17:26:14 +01001804 }
1805 }
Michal Vaskoddd76592022-01-17 13:34:48 +01001806 LY_CHECK_GOTO(ret = lydict_insert(trg_ctx, orig->name.name, 0, &opaq->name.name), error);
1807 LY_CHECK_GOTO(ret = lydict_insert(trg_ctx, orig->name.prefix, 0, &opaq->name.prefix), error);
1808 LY_CHECK_GOTO(ret = lydict_insert(trg_ctx, orig->name.module_ns, 0, &opaq->name.module_ns), error);
1809 LY_CHECK_GOTO(ret = lydict_insert(trg_ctx, orig->value, 0, &opaq->value), error);
Michal Vasko9cf62422021-07-01 13:29:32 +02001810 opaq->hints = orig->hints;
1811 opaq->format = orig->format;
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01001812 if (orig->val_prefix_data) {
Michal Vaskoddd76592022-01-17 13:34:48 +01001813 ret = ly_dup_prefix_data(trg_ctx, opaq->format, orig->val_prefix_data, &opaq->val_prefix_data);
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01001814 LY_CHECK_GOTO(ret, error);
Michal Vasko52927e22020-03-16 17:26:14 +01001815 }
Michal Vasko52927e22020-03-16 17:26:14 +01001816 } else if (dup->schema->nodetype & LYD_NODE_TERM) {
1817 struct lyd_node_term *term = (struct lyd_node_term *)dup;
1818 struct lyd_node_term *orig = (struct lyd_node_term *)node;
1819
1820 term->hash = orig->hash;
Michal Vaskoddd76592022-01-17 13:34:48 +01001821 if (trg_ctx == LYD_CTX(node)) {
1822 ret = orig->value.realtype->plugin->duplicate(trg_ctx, &orig->value, &term->value);
1823 LY_CHECK_ERR_GOTO(ret, LOGERR(trg_ctx, ret, "Value duplication failed."), error);
1824 } else {
1825 /* store canonical value in the target context */
1826 val_can = lyd_get_value(node);
1827 type = ((struct lysc_node_leaf *)term->schema)->type;
Michal Vasko989cdb42023-10-06 15:32:37 +02001828 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 +01001829 LYD_HINT_DATA, term->schema, NULL);
1830 LY_CHECK_GOTO(ret, error);
1831 }
Michal Vasko52927e22020-03-16 17:26:14 +01001832 } else if (dup->schema->nodetype & LYD_NODE_INNER) {
1833 struct lyd_node_inner *orig = (struct lyd_node_inner *)node;
1834 struct lyd_node *child;
1835
1836 if (options & LYD_DUP_RECURSIVE) {
1837 /* duplicate all the children */
1838 LY_LIST_FOR(orig->child, child) {
Michal Vaskoddd76592022-01-17 13:34:48 +01001839 LY_CHECK_GOTO(ret = lyd_dup_r(child, trg_ctx, dup, 1, NULL, options, NULL), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02001840 }
Michal Vasko69730152020-10-09 16:30:07 +02001841 } else if ((dup->schema->nodetype == LYS_LIST) && !(dup->schema->flags & LYS_KEYLESS)) {
Radek Krejci22ebdba2019-07-25 13:59:43 +02001842 /* always duplicate keys of a list */
Michal Vasko9beceb82022-04-05 12:14:15 +02001843 for (child = orig->child; child && lysc_is_key(child->schema); child = child->next) {
Michal Vaskoddd76592022-01-17 13:34:48 +01001844 LY_CHECK_GOTO(ret = lyd_dup_r(child, trg_ctx, dup, 1, NULL, options, NULL), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02001845 }
1846 }
1847 lyd_hash(dup);
1848 } else if (dup->schema->nodetype & LYD_NODE_ANY) {
Michal Vasko61551fa2020-07-09 15:45:45 +02001849 dup->hash = node->hash;
1850 any = (struct lyd_node_any *)node;
1851 LY_CHECK_GOTO(ret = lyd_any_copy_value(dup, &any->value, any->value_type), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02001852 }
1853
Michal Vasko52927e22020-03-16 17:26:14 +01001854 /* insert */
Michal Vasko67177e52021-08-25 11:15:15 +02001855 lyd_insert_node(parent, first, dup, insert_last);
Michal Vasko52927e22020-03-16 17:26:14 +01001856
1857 if (dup_p) {
1858 *dup_p = dup;
1859 }
1860 return LY_SUCCESS;
Radek Krejci22ebdba2019-07-25 13:59:43 +02001861
1862error:
Michal Vasko52927e22020-03-16 17:26:14 +01001863 lyd_free_tree(dup);
1864 return ret;
Radek Krejci22ebdba2019-07-25 13:59:43 +02001865}
1866
Michal Vasko29d674b2021-08-25 11:18:35 +02001867/**
1868 * @brief Get a parent node to connect duplicated subtree to.
1869 *
1870 * @param[in] node Node (subtree) to duplicate.
Michal Vaskoddd76592022-01-17 13:34:48 +01001871 * @param[in] trg_ctx Target context for duplicated nodes.
Michal Vasko29d674b2021-08-25 11:18:35 +02001872 * @param[in] parent Initial parent to connect to.
1873 * @param[in] options Bitmask of options flags, see @ref dupoptions.
1874 * @param[out] dup_parent First duplicated parent node, if any.
1875 * @param[out] local_parent Correct parent to directly connect duplicated @p node to.
1876 * @return LY_ERR value.
1877 */
Michal Vasko3a41dff2020-07-15 14:30:28 +02001878static LY_ERR
Michal Vasko58d89e92023-05-23 09:56:19 +02001879lyd_dup_get_local_parent(const struct lyd_node *node, const struct ly_ctx *trg_ctx, struct lyd_node *parent,
1880 uint32_t options, struct lyd_node **dup_parent, struct lyd_node **local_parent)
Radek Krejci22ebdba2019-07-25 13:59:43 +02001881{
Michal Vasko58d89e92023-05-23 09:56:19 +02001882 const struct lyd_node *orig_parent;
Michal Vaskoeaef7c92023-10-17 10:06:15 +02001883 struct lyd_node *iter = NULL;
Michal Vasko83522192022-07-20 08:07:34 +02001884 ly_bool repeat = 1, ext_parent = 0;
Michal Vasko3a41dff2020-07-15 14:30:28 +02001885
1886 *dup_parent = NULL;
1887 *local_parent = NULL;
1888
Michal Vasko83522192022-07-20 08:07:34 +02001889 if (node->flags & LYD_EXT) {
1890 ext_parent = 1;
1891 }
Michal Vasko58d89e92023-05-23 09:56:19 +02001892 for (orig_parent = lyd_parent(node); repeat && orig_parent; orig_parent = lyd_parent(orig_parent)) {
Michal Vasko83522192022-07-20 08:07:34 +02001893 if (ext_parent) {
1894 /* use the standard context */
1895 trg_ctx = LYD_CTX(orig_parent);
1896 }
Michal Vasko13c5b212023-02-14 10:03:01 +01001897 if (parent && (LYD_CTX(parent) == LYD_CTX(orig_parent)) && (parent->schema == orig_parent->schema)) {
Michal Vasko3a41dff2020-07-15 14:30:28 +02001898 /* stop creating parents, connect what we have into the provided parent */
1899 iter = parent;
1900 repeat = 0;
Michal Vasko13c5b212023-02-14 10:03:01 +01001901 } else if (parent && (LYD_CTX(parent) != LYD_CTX(orig_parent)) &&
1902 lyd_compare_schema_equal(parent->schema, orig_parent->schema) &&
Michal Vasko58d89e92023-05-23 09:56:19 +02001903 lyd_compare_schema_parents_equal(parent, orig_parent)) {
Michal Vasko13c5b212023-02-14 10:03:01 +01001904 iter = parent;
1905 repeat = 0;
Michal Vasko3a41dff2020-07-15 14:30:28 +02001906 } else {
1907 iter = NULL;
Michal Vasko58d89e92023-05-23 09:56:19 +02001908 LY_CHECK_RET(lyd_dup_r(orig_parent, trg_ctx, NULL, 0, &iter, options, &iter));
Michal Vaskoeaef7c92023-10-17 10:06:15 +02001909
1910 /* insert into the previous duplicated parent */
1911 if (*dup_parent) {
1912 lyd_insert_node(iter, NULL, *dup_parent, 0);
1913 }
1914
1915 /* update the last duplicated parent */
1916 *dup_parent = iter;
Michal Vasko3a41dff2020-07-15 14:30:28 +02001917 }
Michal Vasko58d89e92023-05-23 09:56:19 +02001918
Michal Vaskoeaef7c92023-10-17 10:06:15 +02001919 /* set the first parent */
Michal Vasko3a41dff2020-07-15 14:30:28 +02001920 if (!*local_parent) {
Michal Vasko58d89e92023-05-23 09:56:19 +02001921 *local_parent = iter;
Michal Vasko3a41dff2020-07-15 14:30:28 +02001922 }
Michal Vasko58d89e92023-05-23 09:56:19 +02001923
Michal Vasko83522192022-07-20 08:07:34 +02001924 if (orig_parent->flags & LYD_EXT) {
1925 ext_parent = 1;
1926 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02001927 }
1928
1929 if (repeat && parent) {
1930 /* given parent and created parents chain actually do not interconnect */
Michal Vasko58d89e92023-05-23 09:56:19 +02001931 LOGERR(trg_ctx, LY_EINVAL, "None of the duplicated node \"%s\" schema parents match the provided parent \"%s\".",
1932 LYD_NAME(node), LYD_NAME(parent));
Michal Vasko3a41dff2020-07-15 14:30:28 +02001933 return LY_EINVAL;
1934 }
1935
Michal Vaskoeaef7c92023-10-17 10:06:15 +02001936 if (*dup_parent && parent) {
1937 /* last insert into a prevously-existing parent */
1938 lyd_insert_node(parent, NULL, *dup_parent, 0);
1939 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02001940 return LY_SUCCESS;
1941}
1942
1943static LY_ERR
Michal Vasko58d89e92023-05-23 09:56:19 +02001944lyd_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 +01001945 ly_bool nosiblings, struct lyd_node **dup)
Michal Vasko3a41dff2020-07-15 14:30:28 +02001946{
1947 LY_ERR rc;
Radek Krejci22ebdba2019-07-25 13:59:43 +02001948 const struct lyd_node *orig; /* original node to be duplicated */
1949 struct lyd_node *first = NULL; /* the first duplicated node, this is returned */
Radek Krejci22ebdba2019-07-25 13:59:43 +02001950 struct lyd_node *top = NULL; /* the most higher created node */
Michal Vasko58d89e92023-05-23 09:56:19 +02001951 struct lyd_node *local_parent = NULL; /* the direct parent node for the duplicated node(s) */
Radek Krejci22ebdba2019-07-25 13:59:43 +02001952
Michal Vasko1feb9bb2022-07-20 08:44:07 +02001953 assert(node && trg_ctx);
Radek Krejci22ebdba2019-07-25 13:59:43 +02001954
1955 if (options & LYD_DUP_WITH_PARENTS) {
Michal Vaskoddd76592022-01-17 13:34:48 +01001956 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 +02001957 &top, &local_parent), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02001958 } else {
1959 local_parent = parent;
1960 }
1961
Radek Krejci22ebdba2019-07-25 13:59:43 +02001962 LY_LIST_FOR(node, orig) {
Michal Vasko35f4d772021-01-12 12:08:57 +01001963 if (lysc_is_key(orig->schema)) {
1964 if (local_parent) {
1965 /* the key must already exist in the parent */
Michal Vasko58d89e92023-05-23 09:56:19 +02001966 rc = lyd_find_sibling_schema(lyd_child(local_parent), orig->schema, first ? NULL : &first);
Michal Vaskoddd76592022-01-17 13:34:48 +01001967 LY_CHECK_ERR_GOTO(rc, LOGINT(trg_ctx), error);
Michal Vasko35f4d772021-01-12 12:08:57 +01001968 } else {
1969 assert(!(options & LYD_DUP_WITH_PARENTS));
1970 /* duplicating a single key, okay, I suppose... */
Michal Vaskoddd76592022-01-17 13:34:48 +01001971 rc = lyd_dup_r(orig, trg_ctx, NULL, 0, &first, options, first ? NULL : &first);
Michal Vasko35f4d772021-01-12 12:08:57 +01001972 LY_CHECK_GOTO(rc, error);
1973 }
1974 } else {
1975 /* if there is no local parent, it will be inserted into first */
Michal Vasko58d89e92023-05-23 09:56:19 +02001976 rc = lyd_dup_r(orig, trg_ctx, local_parent, 0, &first, options, first ? NULL : &first);
Michal Vasko35f4d772021-01-12 12:08:57 +01001977 LY_CHECK_GOTO(rc, error);
1978 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02001979 if (nosiblings) {
Radek Krejci22ebdba2019-07-25 13:59:43 +02001980 break;
1981 }
1982 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02001983
Michal Vasko3a41dff2020-07-15 14:30:28 +02001984 if (dup) {
1985 *dup = first;
1986 }
1987 return LY_SUCCESS;
Radek Krejci22ebdba2019-07-25 13:59:43 +02001988
1989error:
1990 if (top) {
1991 lyd_free_tree(top);
1992 } else {
Michal Vaskof03ed032020-03-04 13:31:44 +01001993 lyd_free_siblings(first);
Radek Krejci22ebdba2019-07-25 13:59:43 +02001994 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02001995 return rc;
Radek Krejci22ebdba2019-07-25 13:59:43 +02001996}
Michal Vasko5ec7cda2019-09-11 13:43:08 +02001997
Michal Vasko1feb9bb2022-07-20 08:44:07 +02001998/**
1999 * @brief Check the context of node and parent when duplicating nodes.
2000 *
2001 * @param[in] node Node to duplicate.
2002 * @param[in] parent Parent of the duplicated node(s).
2003 * @return LY_ERR value.
2004 */
2005static LY_ERR
2006lyd_dup_ctx_check(const struct lyd_node *node, const struct lyd_node_inner *parent)
2007{
2008 const struct lyd_node *iter;
2009
2010 if (!node || !parent) {
2011 return LY_SUCCESS;
2012 }
2013
2014 if ((LYD_CTX(node) != LYD_CTX(parent))) {
2015 /* try to find top-level ext data parent */
2016 for (iter = node; iter && !(iter->flags & LYD_EXT); iter = lyd_parent(iter)) {}
2017
2018 if (!iter || !lyd_parent(iter) || (LYD_CTX(lyd_parent(iter)) != LYD_CTX(parent))) {
Michal Vaskoce55b462023-02-14 10:03:32 +01002019 LOGERR(LYD_CTX(node), LY_EINVAL, "Different contexts used in node duplication.");
Michal Vasko1feb9bb2022-07-20 08:44:07 +02002020 return LY_EINVAL;
2021 }
2022 }
2023
2024 return LY_SUCCESS;
2025}
2026
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002027LIBYANG_API_DEF LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02002028lyd_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 +02002029{
Michal Vaskoddd76592022-01-17 13:34:48 +01002030 LY_CHECK_ARG_RET(NULL, node, LY_EINVAL);
Michal Vasko1feb9bb2022-07-20 08:44:07 +02002031 LY_CHECK_RET(lyd_dup_ctx_check(node, parent));
Michal Vaskoddd76592022-01-17 13:34:48 +01002032
Michal Vasko58d89e92023-05-23 09:56:19 +02002033 return lyd_dup(node, LYD_CTX(node), (struct lyd_node *)parent, options, 1, dup);
Michal Vaskoddd76592022-01-17 13:34:48 +01002034}
2035
2036LIBYANG_API_DEF LY_ERR
2037lyd_dup_single_to_ctx(const struct lyd_node *node, const struct ly_ctx *trg_ctx, struct lyd_node_inner *parent,
2038 uint32_t options, struct lyd_node **dup)
2039{
2040 LY_CHECK_ARG_RET(trg_ctx, node, trg_ctx, LY_EINVAL);
2041
Michal Vasko58d89e92023-05-23 09:56:19 +02002042 return lyd_dup(node, trg_ctx, (struct lyd_node *)parent, options, 1, dup);
Michal Vasko3a41dff2020-07-15 14:30:28 +02002043}
2044
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002045LIBYANG_API_DEF LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02002046lyd_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 +02002047{
Michal Vaskoddd76592022-01-17 13:34:48 +01002048 LY_CHECK_ARG_RET(NULL, node, LY_EINVAL);
Michal Vasko1feb9bb2022-07-20 08:44:07 +02002049 LY_CHECK_RET(lyd_dup_ctx_check(node, parent));
Michal Vaskoddd76592022-01-17 13:34:48 +01002050
Michal Vasko58d89e92023-05-23 09:56:19 +02002051 return lyd_dup(node, LYD_CTX(node), (struct lyd_node *)parent, options, 0, dup);
Michal Vaskoddd76592022-01-17 13:34:48 +01002052}
2053
2054LIBYANG_API_DEF LY_ERR
2055lyd_dup_siblings_to_ctx(const struct lyd_node *node, const struct ly_ctx *trg_ctx, struct lyd_node_inner *parent,
2056 uint32_t options, struct lyd_node **dup)
2057{
2058 LY_CHECK_ARG_RET(trg_ctx, node, trg_ctx, LY_EINVAL);
2059
Michal Vasko58d89e92023-05-23 09:56:19 +02002060 return lyd_dup(node, trg_ctx, (struct lyd_node *)parent, options, 0, dup);
Michal Vasko3a41dff2020-07-15 14:30:28 +02002061}
2062
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002063LIBYANG_API_DEF LY_ERR
Michal Vasko3a41dff2020-07-15 14:30:28 +02002064lyd_dup_meta_single(const struct lyd_meta *meta, struct lyd_node *node, struct lyd_meta **dup)
Michal Vasko25a32822020-07-09 15:48:22 +02002065{
Radek Krejci011e4aa2020-09-04 15:22:31 +02002066 LY_ERR ret = LY_SUCCESS;
Michal Vasko33c48972022-07-20 10:28:07 +02002067 const struct ly_ctx *ctx;
Michal Vasko25a32822020-07-09 15:48:22 +02002068 struct lyd_meta *mt, *last;
2069
Michal Vasko3a41dff2020-07-15 14:30:28 +02002070 LY_CHECK_ARG_RET(NULL, meta, node, LY_EINVAL);
Michal Vasko25a32822020-07-09 15:48:22 +02002071
Michal Vasko33c48972022-07-20 10:28:07 +02002072 /* log to node context but value must always use the annotation context */
2073 ctx = meta->annotation->module->ctx;
2074
Michal Vasko25a32822020-07-09 15:48:22 +02002075 /* create a copy */
2076 mt = calloc(1, sizeof *mt);
Michal Vaskob7be7a82020-08-20 09:09:04 +02002077 LY_CHECK_ERR_RET(!mt, LOGMEM(LYD_CTX(node)), LY_EMEM);
Michal Vasko25a32822020-07-09 15:48:22 +02002078 mt->annotation = meta->annotation;
Michal Vasko33c48972022-07-20 10:28:07 +02002079 ret = meta->value.realtype->plugin->duplicate(ctx, &meta->value, &mt->value);
Radek Krejci011e4aa2020-09-04 15:22:31 +02002080 LY_CHECK_ERR_GOTO(ret, LOGERR(LYD_CTX(node), LY_EINT, "Value duplication failed."), finish);
Michal Vasko33c48972022-07-20 10:28:07 +02002081 LY_CHECK_GOTO(ret = lydict_insert(ctx, meta->name, 0, &mt->name), finish);
Michal Vasko25a32822020-07-09 15:48:22 +02002082
2083 /* insert as the last attribute */
Radek Krejci011e4aa2020-09-04 15:22:31 +02002084 mt->parent = node;
Michal Vasko25a32822020-07-09 15:48:22 +02002085 if (node->meta) {
Radek Krejci1e008d22020-08-17 11:37:37 +02002086 for (last = node->meta; last->next; last = last->next) {}
Michal Vasko25a32822020-07-09 15:48:22 +02002087 last->next = mt;
2088 } else {
2089 node->meta = mt;
2090 }
2091
Radek Krejci011e4aa2020-09-04 15:22:31 +02002092finish:
2093 if (ret) {
2094 lyd_free_meta_single(mt);
2095 } else if (dup) {
Michal Vasko3a41dff2020-07-15 14:30:28 +02002096 *dup = mt;
2097 }
2098 return LY_SUCCESS;
Michal Vasko25a32822020-07-09 15:48:22 +02002099}
2100
Michal Vasko4490d312020-06-16 13:08:55 +02002101/**
2102 * @brief Merge a source sibling into target siblings.
2103 *
2104 * @param[in,out] first_trg First target sibling, is updated if top-level.
2105 * @param[in] parent_trg Target parent.
aPiecek55653c92023-11-09 13:43:19 +01002106 * @param[in,out] sibling_src_p Source sibling to merge, set to NULL if spent.
Michal Vaskocd3f6172021-05-18 16:14:50 +02002107 * @param[in] merge_cb Optional merge callback.
2108 * @param[in] cb_data Arbitrary callback data.
Michal Vasko4490d312020-06-16 13:08:55 +02002109 * @param[in] options Merge options.
Michal Vaskocd3f6172021-05-18 16:14:50 +02002110 * @param[in,out] dup_inst Duplicate instance cache for all @p first_trg siblings.
Michal Vasko4490d312020-06-16 13:08:55 +02002111 * @return LY_ERR value.
2112 */
2113static LY_ERR
2114lyd_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 +02002115 lyd_merge_cb merge_cb, void *cb_data, uint16_t options, struct ly_ht **dup_inst)
Michal Vasko4490d312020-06-16 13:08:55 +02002116{
Michal Vasko4490d312020-06-16 13:08:55 +02002117 const struct lyd_node *child_src, *tmp, *sibling_src;
Michal Vasko56daf732020-08-10 10:57:18 +02002118 struct lyd_node *match_trg, *dup_src, *elem;
Michal Vaskod1afa502022-01-27 16:18:12 +01002119 struct lyd_node_opaq *opaq_trg, *opaq_src;
Michal Vasko4490d312020-06-16 13:08:55 +02002120 struct lysc_type *type;
Michal Vasko8efac242023-03-30 08:24:56 +02002121 struct ly_ht *child_dup_inst = NULL;
Michal Vasko42c96e22024-01-18 08:18:06 +01002122 LY_ERR r;
Michal Vaskocd3f6172021-05-18 16:14:50 +02002123 ly_bool first_inst = 0;
Michal Vasko4490d312020-06-16 13:08:55 +02002124
2125 sibling_src = *sibling_src_p;
Michal Vaskocd3f6172021-05-18 16:14:50 +02002126 if (!sibling_src->schema) {
2127 /* try to find the same opaque node */
Michal Vasko42c96e22024-01-18 08:18:06 +01002128 r = lyd_find_sibling_opaq_next(*first_trg, LYD_NAME(sibling_src), &match_trg);
Michal Vaskocd3f6172021-05-18 16:14:50 +02002129 } else if (sibling_src->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) {
Michal Vasko4490d312020-06-16 13:08:55 +02002130 /* try to find the exact instance */
Michal Vasko42c96e22024-01-18 08:18:06 +01002131 r = lyd_find_sibling_first(*first_trg, sibling_src, &match_trg);
Michal Vasko4490d312020-06-16 13:08:55 +02002132 } else {
2133 /* try to simply find the node, there cannot be more instances */
Michal Vasko42c96e22024-01-18 08:18:06 +01002134 r = lyd_find_sibling_val(*first_trg, sibling_src->schema, NULL, 0, &match_trg);
Michal Vasko4490d312020-06-16 13:08:55 +02002135 }
Michal Vasko42c96e22024-01-18 08:18:06 +01002136 LY_CHECK_RET(r && (r != LY_ENOTFOUND), r);
Michal Vasko4490d312020-06-16 13:08:55 +02002137
Michal Vaskocd3f6172021-05-18 16:14:50 +02002138 if (match_trg) {
2139 /* update match as needed */
2140 LY_CHECK_RET(lyd_dup_inst_next(&match_trg, *first_trg, dup_inst));
2141 } else {
2142 /* first instance of this node */
2143 first_inst = 1;
2144 }
2145
2146 if (match_trg) {
2147 /* call callback */
2148 if (merge_cb) {
2149 LY_CHECK_RET(merge_cb(match_trg, sibling_src, cb_data));
2150 }
2151
Michal Vasko4490d312020-06-16 13:08:55 +02002152 /* node found, make sure even value matches for all node types */
Michal Vaskod1afa502022-01-27 16:18:12 +01002153 if (!match_trg->schema) {
2154 if (lyd_compare_single(sibling_src, match_trg, 0)) {
2155 /* update value */
2156 opaq_trg = (struct lyd_node_opaq *)match_trg;
2157 opaq_src = (struct lyd_node_opaq *)sibling_src;
2158
2159 lydict_remove(LYD_CTX(opaq_trg), opaq_trg->value);
2160 lydict_insert(LYD_CTX(opaq_trg), opaq_src->value, 0, &opaq_trg->value);
2161 opaq_trg->hints = opaq_src->hints;
2162
2163 ly_free_prefix_data(opaq_trg->format, opaq_trg->val_prefix_data);
2164 opaq_trg->format = opaq_src->format;
2165 ly_dup_prefix_data(LYD_CTX(opaq_trg), opaq_src->format, opaq_src->val_prefix_data,
2166 &opaq_trg->val_prefix_data);
2167 }
2168 } else if ((match_trg->schema->nodetype == LYS_LEAF) &&
2169 lyd_compare_single(sibling_src, match_trg, LYD_COMPARE_DEFAULTS)) {
Michal Vasko4490d312020-06-16 13:08:55 +02002170 /* since they are different, they cannot both be default */
2171 assert(!(sibling_src->flags & LYD_DEFAULT) || !(match_trg->flags & LYD_DEFAULT));
2172
Michal Vasko3a41dff2020-07-15 14:30:28 +02002173 /* update value (or only LYD_DEFAULT flag) only if flag set or the source node is not default */
2174 if ((options & LYD_MERGE_DEFAULTS) || !(sibling_src->flags & LYD_DEFAULT)) {
Michal Vasko4490d312020-06-16 13:08:55 +02002175 type = ((struct lysc_node_leaf *)match_trg->schema)->type;
Michal Vaskob7be7a82020-08-20 09:09:04 +02002176 type->plugin->free(LYD_CTX(match_trg), &((struct lyd_node_term *)match_trg)->value);
2177 LY_CHECK_RET(type->plugin->duplicate(LYD_CTX(match_trg), &((struct lyd_node_term *)sibling_src)->value,
Michal Vasko69730152020-10-09 16:30:07 +02002178 &((struct lyd_node_term *)match_trg)->value));
Michal Vasko4490d312020-06-16 13:08:55 +02002179
2180 /* copy flags and add LYD_NEW */
Michal Vasko7e8315b2021-08-03 17:01:06 +02002181 match_trg->flags = sibling_src->flags | ((options & LYD_MERGE_WITH_FLAGS) ? 0 : LYD_NEW);
Michal Vasko4490d312020-06-16 13:08:55 +02002182 }
Michal Vasko8f359bf2020-07-28 10:41:15 +02002183 } else if ((match_trg->schema->nodetype & LYS_ANYDATA) && lyd_compare_single(sibling_src, match_trg, 0)) {
Michal Vasko4c583e82020-07-17 12:16:14 +02002184 /* update value */
2185 LY_CHECK_RET(lyd_any_copy_value(match_trg, &((struct lyd_node_any *)sibling_src)->value,
Radek Krejci0f969882020-08-21 16:56:47 +02002186 ((struct lyd_node_any *)sibling_src)->value_type));
Michal Vasko4490d312020-06-16 13:08:55 +02002187
2188 /* copy flags and add LYD_NEW */
Michal Vasko7e8315b2021-08-03 17:01:06 +02002189 match_trg->flags = sibling_src->flags | ((options & LYD_MERGE_WITH_FLAGS) ? 0 : LYD_NEW);
Michal Vaskocd3f6172021-05-18 16:14:50 +02002190 }
2191
2192 /* check descendants, recursively */
Michal Vasko42c96e22024-01-18 08:18:06 +01002193 r = LY_SUCCESS;
Michal Vaskocd3f6172021-05-18 16:14:50 +02002194 LY_LIST_FOR_SAFE(lyd_child_no_keys(sibling_src), tmp, child_src) {
Michal Vasko42c96e22024-01-18 08:18:06 +01002195 r = lyd_merge_sibling_r(lyd_node_child_p(match_trg), match_trg, &child_src, merge_cb, cb_data, options,
Michal Vaskocd3f6172021-05-18 16:14:50 +02002196 &child_dup_inst);
Michal Vasko42c96e22024-01-18 08:18:06 +01002197 if (r) {
Michal Vaskocd3f6172021-05-18 16:14:50 +02002198 break;
Michal Vasko4490d312020-06-16 13:08:55 +02002199 }
2200 }
Michal Vaskocd3f6172021-05-18 16:14:50 +02002201 lyd_dup_inst_free(child_dup_inst);
Michal Vasko42c96e22024-01-18 08:18:06 +01002202 LY_CHECK_RET(r);
Michal Vasko4490d312020-06-16 13:08:55 +02002203 } else {
2204 /* node not found, merge it */
2205 if (options & LYD_MERGE_DESTRUCT) {
2206 dup_src = (struct lyd_node *)sibling_src;
Michal Vasko2e784f82024-01-11 09:51:22 +01002207 lyd_unlink(dup_src);
Michal Vasko4490d312020-06-16 13:08:55 +02002208 /* spend it */
2209 *sibling_src_p = NULL;
2210 } else {
Michal Vasko3a41dff2020-07-15 14:30:28 +02002211 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 +02002212 }
2213
Michal Vasko7e8315b2021-08-03 17:01:06 +02002214 if (!(options & LYD_MERGE_WITH_FLAGS)) {
2215 /* set LYD_NEW for all the new nodes, required for validation */
2216 LYD_TREE_DFS_BEGIN(dup_src, elem) {
2217 elem->flags |= LYD_NEW;
2218 LYD_TREE_DFS_END(dup_src, elem);
2219 }
Michal Vasko4490d312020-06-16 13:08:55 +02002220 }
2221
Michal Vaskocd3f6172021-05-18 16:14:50 +02002222 /* insert */
Michal Vasko6ee6f432021-07-16 09:49:14 +02002223 lyd_insert_node(parent_trg, first_trg, dup_src, 0);
Michal Vaskocd3f6172021-05-18 16:14:50 +02002224
2225 if (first_inst) {
2226 /* remember not to find this instance next time */
2227 LY_CHECK_RET(lyd_dup_inst_next(&dup_src, *first_trg, dup_inst));
2228 }
2229
2230 /* call callback, no source node */
2231 if (merge_cb) {
2232 LY_CHECK_RET(merge_cb(dup_src, NULL, cb_data));
2233 }
Michal Vasko4490d312020-06-16 13:08:55 +02002234 }
2235
2236 return LY_SUCCESS;
2237}
2238
Michal Vasko3a41dff2020-07-15 14:30:28 +02002239static LY_ERR
Michal Vaskocd3f6172021-05-18 16:14:50 +02002240lyd_merge(struct lyd_node **target, const struct lyd_node *source, const struct lys_module *mod,
2241 lyd_merge_cb merge_cb, void *cb_data, uint16_t options, ly_bool nosiblings)
Michal Vasko4490d312020-06-16 13:08:55 +02002242{
2243 const struct lyd_node *sibling_src, *tmp;
Michal Vasko8efac242023-03-30 08:24:56 +02002244 struct ly_ht *dup_inst = NULL;
Radek Krejci857189e2020-09-01 13:26:36 +02002245 ly_bool first;
Michal Vaskocd3f6172021-05-18 16:14:50 +02002246 LY_ERR ret = LY_SUCCESS;
Michal Vasko4490d312020-06-16 13:08:55 +02002247
2248 LY_CHECK_ARG_RET(NULL, target, LY_EINVAL);
Michal Vasko892f5bf2021-11-24 10:41:05 +01002249 LY_CHECK_CTX_EQUAL_RET(*target ? LYD_CTX(*target) : NULL, source ? LYD_CTX(source) : NULL, mod ? mod->ctx : NULL,
2250 LY_EINVAL);
Michal Vasko4490d312020-06-16 13:08:55 +02002251
2252 if (!source) {
2253 /* nothing to merge */
2254 return LY_SUCCESS;
2255 }
2256
Michal Vasko831422b2020-11-24 11:20:51 +01002257 if ((*target && lysc_data_parent((*target)->schema)) || lysc_data_parent(source->schema)) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02002258 LOGERR(LYD_CTX(source), LY_EINVAL, "Invalid arguments - can merge only 2 top-level subtrees (%s()).", __func__);
Michal Vasko4490d312020-06-16 13:08:55 +02002259 return LY_EINVAL;
2260 }
2261
2262 LY_LIST_FOR_SAFE(source, tmp, sibling_src) {
Michal Vaskocd3f6172021-05-18 16:14:50 +02002263 if (mod && (lyd_owner_module(sibling_src) != mod)) {
2264 /* skip data nodes from different modules */
2265 continue;
2266 }
2267
Radek Krejci857189e2020-09-01 13:26:36 +02002268 first = (sibling_src == source) ? 1 : 0;
Michal Vaskocd3f6172021-05-18 16:14:50 +02002269 ret = lyd_merge_sibling_r(target, NULL, &sibling_src, merge_cb, cb_data, options, &dup_inst);
2270 if (ret) {
2271 break;
2272 }
Michal Vasko4490d312020-06-16 13:08:55 +02002273 if (first && !sibling_src) {
2274 /* source was spent (unlinked), move to the next node */
2275 source = tmp;
2276 }
2277
Michal Vasko3a41dff2020-07-15 14:30:28 +02002278 if (nosiblings) {
Michal Vasko4490d312020-06-16 13:08:55 +02002279 break;
2280 }
2281 }
2282
2283 if (options & LYD_MERGE_DESTRUCT) {
2284 /* free any leftover source data that were not merged */
2285 lyd_free_siblings((struct lyd_node *)source);
2286 }
2287
Michal Vaskocd3f6172021-05-18 16:14:50 +02002288 lyd_dup_inst_free(dup_inst);
2289 return ret;
Michal Vasko4490d312020-06-16 13:08:55 +02002290}
2291
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002292LIBYANG_API_DEF LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02002293lyd_merge_tree(struct lyd_node **target, const struct lyd_node *source, uint16_t options)
Michal Vasko3a41dff2020-07-15 14:30:28 +02002294{
Michal Vaskocd3f6172021-05-18 16:14:50 +02002295 return lyd_merge(target, source, NULL, NULL, NULL, options, 1);
Michal Vasko3a41dff2020-07-15 14:30:28 +02002296}
2297
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002298LIBYANG_API_DEF LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02002299lyd_merge_siblings(struct lyd_node **target, const struct lyd_node *source, uint16_t options)
Michal Vasko3a41dff2020-07-15 14:30:28 +02002300{
Michal Vaskocd3f6172021-05-18 16:14:50 +02002301 return lyd_merge(target, source, NULL, NULL, NULL, options, 0);
2302}
2303
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002304LIBYANG_API_DEF LY_ERR
Michal Vaskocd3f6172021-05-18 16:14:50 +02002305lyd_merge_module(struct lyd_node **target, const struct lyd_node *source, const struct lys_module *mod,
2306 lyd_merge_cb merge_cb, void *cb_data, uint16_t options)
2307{
2308 return lyd_merge(target, source, mod, merge_cb, cb_data, options, 0);
Michal Vasko3a41dff2020-07-15 14:30:28 +02002309}
2310
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002311static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02002312lyd_path_str_enlarge(char **buffer, size_t *buflen, size_t reqlen, ly_bool is_static)
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002313{
Michal Vasko14654712020-02-06 08:35:21 +01002314 /* ending \0 */
2315 ++reqlen;
2316
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002317 if (reqlen > *buflen) {
2318 if (is_static) {
2319 return LY_EINCOMPLETE;
2320 }
2321
2322 *buffer = ly_realloc(*buffer, reqlen * sizeof **buffer);
2323 if (!*buffer) {
2324 return LY_EMEM;
2325 }
2326
2327 *buflen = reqlen;
2328 }
2329
2330 return LY_SUCCESS;
2331}
2332
Michal Vaskod59035b2020-07-08 12:00:06 +02002333LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02002334lyd_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 +02002335{
2336 const struct lyd_node *key;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002337 size_t len;
2338 const char *val;
2339 char quot;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002340
Michal Vasko824d0832021-11-04 15:36:51 +01002341 for (key = lyd_child(node); key && key->schema && (key->schema->flags & LYS_KEY); key = key->next) {
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02002342 val = lyd_get_value(key);
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002343 len = 1 + strlen(key->schema->name) + 2 + strlen(val) + 2;
Michal Vaskoba99a3e2020-08-18 15:50:05 +02002344 LY_CHECK_RET(lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static));
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002345
2346 quot = '\'';
2347 if (strchr(val, '\'')) {
2348 quot = '"';
2349 }
2350 *bufused += sprintf(*buffer + *bufused, "[%s=%c%s%c]", key->schema->name, quot, val, quot);
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002351 }
2352
2353 return LY_SUCCESS;
2354}
2355
2356/**
2357 * @brief Append leaf-list value predicate to path.
2358 *
2359 * @param[in] node Node to print.
2360 * @param[in,out] buffer Buffer to print to.
2361 * @param[in,out] buflen Current buffer length.
2362 * @param[in,out] bufused Current number of characters used in @p buffer.
2363 * @param[in] is_static Whether buffer is static or can be reallocated.
2364 * @return LY_ERR
2365 */
2366static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02002367lyd_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 +02002368{
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002369 size_t len;
2370 const char *val;
2371 char quot;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002372
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02002373 val = lyd_get_value(node);
Radek Krejcif13b87b2020-12-01 22:02:17 +01002374 len = 4 + strlen(val) + 2; /* "[.='" + val + "']" */
Michal Vaskoba99a3e2020-08-18 15:50:05 +02002375 LY_CHECK_RET(lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static));
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002376
2377 quot = '\'';
2378 if (strchr(val, '\'')) {
2379 quot = '"';
2380 }
2381 *bufused += sprintf(*buffer + *bufused, "[.=%c%s%c]", quot, val, quot);
2382
Michal Vaskoba99a3e2020-08-18 15:50:05 +02002383 return LY_SUCCESS;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002384}
2385
2386/**
2387 * @brief Append node position (relative to its other instances) predicate to path.
2388 *
2389 * @param[in] node Node to print.
2390 * @param[in,out] buffer Buffer to print to.
2391 * @param[in,out] buflen Current buffer length.
2392 * @param[in,out] bufused Current number of characters used in @p buffer.
2393 * @param[in] is_static Whether buffer is static or can be reallocated.
2394 * @return LY_ERR
2395 */
2396static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02002397lyd_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 +02002398{
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002399 size_t len;
Michal Vasko50cc0562021-05-18 16:15:43 +02002400 uint32_t pos;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002401 char *val = NULL;
2402 LY_ERR rc;
2403
Michal Vasko50cc0562021-05-18 16:15:43 +02002404 pos = lyd_list_pos(node);
2405 if (asprintf(&val, "%" PRIu32, pos) == -1) {
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002406 return LY_EMEM;
2407 }
2408
2409 len = 1 + strlen(val) + 1;
2410 rc = lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static);
2411 if (rc != LY_SUCCESS) {
2412 goto cleanup;
2413 }
2414
2415 *bufused += sprintf(*buffer + *bufused, "[%s]", val);
2416
2417cleanup:
2418 free(val);
2419 return rc;
2420}
2421
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002422LIBYANG_API_DEF char *
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002423lyd_path(const struct lyd_node *node, LYD_PATH_TYPE pathtype, char *buffer, size_t buflen)
2424{
Radek Krejci857189e2020-09-01 13:26:36 +02002425 ly_bool is_static = 0;
Radek Krejci1deb5be2020-08-26 16:43:36 +02002426 uint32_t i, depth;
Michal Vasko14654712020-02-06 08:35:21 +01002427 size_t bufused = 0, len;
Michal Vasko12eb6222022-03-18 13:35:49 +01002428 const struct lyd_node *iter, *parent;
2429 const struct lys_module *mod, *prev_mod;
Michal Vasko790b2bc2020-08-03 13:35:06 +02002430 LY_ERR rc = LY_SUCCESS;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002431
2432 LY_CHECK_ARG_RET(NULL, node, NULL);
2433 if (buffer) {
Michal Vasko16385f42021-05-18 16:16:09 +02002434 LY_CHECK_ARG_RET(LYD_CTX(node), buflen > 1, NULL);
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002435 is_static = 1;
Michal Vasko14654712020-02-06 08:35:21 +01002436 } else {
2437 buflen = 0;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002438 }
2439
2440 switch (pathtype) {
Radek Krejci635d2b82021-01-04 11:26:51 +01002441 case LYD_PATH_STD:
2442 case LYD_PATH_STD_NO_LAST_PRED:
Michal Vasko14654712020-02-06 08:35:21 +01002443 depth = 1;
Michal Vasko9e685082021-01-29 14:49:09 +01002444 for (iter = node; iter->parent; iter = lyd_parent(iter)) {
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002445 ++depth;
2446 }
2447
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002448 goto iter_print;
Michal Vasko14654712020-02-06 08:35:21 +01002449 while (depth) {
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002450 /* find the right node */
Michal Vasko9e685082021-01-29 14:49:09 +01002451 for (iter = node, i = 1; i < depth; iter = lyd_parent(iter), ++i) {}
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002452iter_print:
Michal Vasko12eb6222022-03-18 13:35:49 +01002453 /* get the module */
Michal Vasko420cc252023-08-24 08:14:24 +02002454 mod = lyd_node_module(iter);
Michal Vasko12eb6222022-03-18 13:35:49 +01002455 parent = lyd_parent(iter);
Michal Vasko420cc252023-08-24 08:14:24 +02002456 prev_mod = lyd_node_module(parent);
Michal Vasko12eb6222022-03-18 13:35:49 +01002457 if (prev_mod == mod) {
2458 mod = NULL;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002459 }
2460
2461 /* realloc string */
Michal Vaskoad92b672020-11-12 13:11:31 +01002462 len = 1 + (mod ? strlen(mod->name) + 1 : 0) + (iter->schema ? strlen(iter->schema->name) :
2463 strlen(((struct lyd_node_opaq *)iter)->name.name));
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002464 rc = lyd_path_str_enlarge(&buffer, &buflen, bufused + len, is_static);
2465 if (rc != LY_SUCCESS) {
2466 break;
2467 }
2468
2469 /* print next node */
Michal Vaskodbf3e652022-10-21 08:46:25 +02002470 bufused += sprintf(buffer + bufused, "/%s%s%s", mod ? mod->name : "", mod ? ":" : "", LYD_NAME(iter));
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002471
Michal Vasko790b2bc2020-08-03 13:35:06 +02002472 /* do not always print the last (first) predicate */
Radek Krejci635d2b82021-01-04 11:26:51 +01002473 if (iter->schema && ((depth > 1) || (pathtype == LYD_PATH_STD))) {
Michal Vasko790b2bc2020-08-03 13:35:06 +02002474 switch (iter->schema->nodetype) {
2475 case LYS_LIST:
2476 if (iter->schema->flags & LYS_KEYLESS) {
2477 /* print its position */
2478 rc = lyd_path_position_predicate(iter, &buffer, &buflen, &bufused, is_static);
2479 } else {
2480 /* print all list keys in predicates */
2481 rc = lyd_path_list_predicate(iter, &buffer, &buflen, &bufused, is_static);
2482 }
2483 break;
2484 case LYS_LEAFLIST:
2485 if (iter->schema->flags & LYS_CONFIG_W) {
2486 /* print leaf-list value */
2487 rc = lyd_path_leaflist_predicate(iter, &buffer, &buflen, &bufused, is_static);
2488 } else {
2489 /* print its position */
2490 rc = lyd_path_position_predicate(iter, &buffer, &buflen, &bufused, is_static);
2491 }
2492 break;
2493 default:
2494 /* nothing to print more */
2495 break;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002496 }
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002497 }
2498 if (rc != LY_SUCCESS) {
2499 break;
2500 }
2501
Michal Vasko14654712020-02-06 08:35:21 +01002502 --depth;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002503 }
2504 break;
2505 }
2506
2507 return buffer;
2508}
Michal Vaskoe444f752020-02-10 12:20:06 +01002509
Michal Vaskodbf3e652022-10-21 08:46:25 +02002510char *
2511lyd_path_set(const struct ly_set *dnodes, LYD_PATH_TYPE pathtype)
2512{
2513 uint32_t depth;
2514 size_t bufused = 0, buflen = 0, len;
2515 char *buffer = NULL;
2516 const struct lyd_node *iter, *parent;
2517 const struct lys_module *mod, *prev_mod;
2518 LY_ERR rc = LY_SUCCESS;
2519
2520 switch (pathtype) {
2521 case LYD_PATH_STD:
2522 case LYD_PATH_STD_NO_LAST_PRED:
2523 for (depth = 1; depth <= dnodes->count; ++depth) {
2524 /* current node */
2525 iter = dnodes->dnodes[depth - 1];
Michal Vasko420cc252023-08-24 08:14:24 +02002526 mod = lyd_node_module(iter);
Michal Vaskodbf3e652022-10-21 08:46:25 +02002527
2528 /* parent */
2529 parent = (depth > 1) ? dnodes->dnodes[depth - 2] : NULL;
Michal Vasko85be65e2023-06-13 09:44:17 +02002530 assert(!parent || !iter->schema || !parent->schema || (parent->schema->nodetype & LYD_NODE_ANY) ||
2531 (lysc_data_parent(iter->schema) == parent->schema) ||
Michal Vasko539d6a92023-09-11 10:31:12 +02002532 (!lysc_data_parent(iter->schema) && (LYD_CTX(iter) != LYD_CTX(parent))) ||
2533 (parent->schema->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)));
Michal Vaskodbf3e652022-10-21 08:46:25 +02002534
2535 /* get module to print, if any */
Michal Vasko420cc252023-08-24 08:14:24 +02002536 prev_mod = lyd_node_module(parent);
Michal Vaskodbf3e652022-10-21 08:46:25 +02002537 if (prev_mod == mod) {
2538 mod = NULL;
2539 }
2540
2541 /* realloc string */
2542 len = 1 + (mod ? strlen(mod->name) + 1 : 0) + (iter->schema ? strlen(iter->schema->name) :
2543 strlen(((struct lyd_node_opaq *)iter)->name.name));
2544 if ((rc = lyd_path_str_enlarge(&buffer, &buflen, bufused + len, 0))) {
2545 break;
2546 }
2547
2548 /* print next node */
2549 bufused += sprintf(buffer + bufused, "/%s%s%s", mod ? mod->name : "", mod ? ":" : "", LYD_NAME(iter));
2550
2551 /* do not always print the last (first) predicate */
2552 if (iter->schema && ((depth > 1) || (pathtype == LYD_PATH_STD))) {
2553 switch (iter->schema->nodetype) {
2554 case LYS_LIST:
2555 if (iter->schema->flags & LYS_KEYLESS) {
2556 /* print its position */
2557 rc = lyd_path_position_predicate(iter, &buffer, &buflen, &bufused, 0);
2558 } else {
2559 /* print all list keys in predicates */
2560 rc = lyd_path_list_predicate(iter, &buffer, &buflen, &bufused, 0);
2561 }
2562 break;
2563 case LYS_LEAFLIST:
2564 if (iter->schema->flags & LYS_CONFIG_W) {
2565 /* print leaf-list value */
2566 rc = lyd_path_leaflist_predicate(iter, &buffer, &buflen, &bufused, 0);
2567 } else {
2568 /* print its position */
2569 rc = lyd_path_position_predicate(iter, &buffer, &buflen, &bufused, 0);
2570 }
2571 break;
2572 default:
2573 /* nothing to print more */
2574 break;
2575 }
2576 }
2577 if (rc) {
2578 break;
2579 }
2580 }
2581 break;
2582 }
2583
2584 return buffer;
2585}
2586
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002587LIBYANG_API_DEF struct lyd_meta *
Michal Vasko25a32822020-07-09 15:48:22 +02002588lyd_find_meta(const struct lyd_meta *first, const struct lys_module *module, const char *name)
2589{
2590 struct lyd_meta *ret = NULL;
2591 const struct ly_ctx *ctx;
2592 const char *prefix, *tmp;
2593 char *str;
2594 size_t pref_len, name_len;
2595
2596 LY_CHECK_ARG_RET(NULL, module || strchr(name, ':'), name, NULL);
Michal Vasko892f5bf2021-11-24 10:41:05 +01002597 LY_CHECK_CTX_EQUAL_RET(first ? first->annotation->module->ctx : NULL, module ? module->ctx : NULL, NULL);
Michal Vasko25a32822020-07-09 15:48:22 +02002598
2599 if (!first) {
2600 return NULL;
2601 }
2602
2603 ctx = first->annotation->module->ctx;
2604
2605 /* parse the name */
2606 tmp = name;
2607 if (ly_parse_nodeid(&tmp, &prefix, &pref_len, &name, &name_len) || tmp[0]) {
2608 LOGERR(ctx, LY_EINVAL, "Metadata name \"%s\" is not valid.", name);
2609 return NULL;
2610 }
2611
2612 /* find the module */
2613 if (prefix) {
2614 str = strndup(prefix, pref_len);
2615 module = ly_ctx_get_module_latest(ctx, str);
2616 free(str);
Radek Krejci422afb12021-03-04 16:38:16 +01002617 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 +02002618 }
2619
2620 /* find the metadata */
2621 LY_LIST_FOR(first, first) {
2622 if ((first->annotation->module == module) && !strcmp(first->name, name)) {
2623 ret = (struct lyd_meta *)first;
2624 break;
2625 }
2626 }
2627
2628 return ret;
2629}
2630
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002631LIBYANG_API_DEF LY_ERR
Michal Vaskoe444f752020-02-10 12:20:06 +01002632lyd_find_sibling_first(const struct lyd_node *siblings, const struct lyd_node *target, struct lyd_node **match)
2633{
Michal Vasko9beceb82022-04-05 12:14:15 +02002634 struct lyd_node **match_p, *iter, *dup = NULL;
Michal Vaskoe444f752020-02-10 12:20:06 +01002635 struct lyd_node_inner *parent;
Michal Vaskoe78faec2021-04-08 17:24:43 +02002636 ly_bool found;
Michal Vaskoe444f752020-02-10 12:20:06 +01002637
Michal Vaskof03ed032020-03-04 13:31:44 +01002638 LY_CHECK_ARG_RET(NULL, target, LY_EINVAL);
Michal Vasko9beceb82022-04-05 12:14:15 +02002639 if (!siblings) {
2640 /* no data */
2641 if (match) {
2642 *match = NULL;
2643 }
2644 return LY_ENOTFOUND;
2645 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002646
Michal Vasko9beceb82022-04-05 12:14:15 +02002647 if (LYD_CTX(siblings) != LYD_CTX(target)) {
2648 /* create a duplicate in this context */
2649 LY_CHECK_RET(lyd_dup_single_to_ctx(target, LYD_CTX(siblings), NULL, 0, &dup));
2650 target = dup;
2651 }
2652
2653 if ((siblings->schema && target->schema && (lysc_data_parent(siblings->schema) != lysc_data_parent(target->schema)))) {
2654 /* schema mismatch */
2655 lyd_free_tree(dup);
Michal Vasko9b368d32020-02-14 13:53:31 +01002656 if (match) {
2657 *match = NULL;
2658 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002659 return LY_ENOTFOUND;
2660 }
2661
Michal Vaskoe78faec2021-04-08 17:24:43 +02002662 /* get first sibling */
2663 siblings = lyd_first_sibling(siblings);
Michal Vaskoe444f752020-02-10 12:20:06 +01002664
Michal Vasko9e685082021-01-29 14:49:09 +01002665 parent = siblings->parent;
Michal Vasko39311152023-08-07 11:03:41 +02002666 if (target->schema && parent && parent->schema && parent->children_ht) {
Michal Vaskoe444f752020-02-10 12:20:06 +01002667 assert(target->hash);
2668
Michal Vaskoe78faec2021-04-08 17:24:43 +02002669 if (lysc_is_dup_inst_list(target->schema)) {
2670 /* we must search the instances from beginning to find the first matching one */
2671 found = 0;
2672 LYD_LIST_FOR_INST(siblings, target->schema, iter) {
Michal Vaskoee9b9482023-06-19 13:17:48 +02002673 if (!lyd_compare_single(target, iter, LYD_COMPARE_FULL_RECURSION)) {
Michal Vaskoe78faec2021-04-08 17:24:43 +02002674 found = 1;
2675 break;
2676 }
2677 }
2678 if (found) {
2679 siblings = iter;
Michal Vaskoda859032020-07-14 12:20:14 +02002680 } else {
2681 siblings = NULL;
2682 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002683 } else {
Michal Vaskoe78faec2021-04-08 17:24:43 +02002684 /* find by hash */
2685 if (!lyht_find(parent->children_ht, &target, target->hash, (void **)&match_p)) {
2686 siblings = *match_p;
2687 } else {
2688 /* not found */
2689 siblings = NULL;
2690 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002691 }
2692 } else {
Michal Vasko39311152023-08-07 11:03:41 +02002693 /* no children hash table or cannot be used */
Michal Vaskod989ba02020-08-24 10:59:24 +02002694 for ( ; siblings; siblings = siblings->next) {
Michal Vaskoee9b9482023-06-19 13:17:48 +02002695 if (lysc_is_dup_inst_list(target->schema)) {
2696 if (!lyd_compare_single(siblings, target, LYD_COMPARE_FULL_RECURSION)) {
2697 break;
2698 }
2699 } else {
Michal Vaskod8a52012023-08-15 11:38:10 +02002700 if (!lyd_compare_single(siblings, target, 0)) {
Michal Vaskoee9b9482023-06-19 13:17:48 +02002701 break;
2702 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002703 }
2704 }
2705 }
2706
Michal Vasko9beceb82022-04-05 12:14:15 +02002707 lyd_free_tree(dup);
Michal Vaskoe444f752020-02-10 12:20:06 +01002708 if (!siblings) {
Michal Vasko9b368d32020-02-14 13:53:31 +01002709 if (match) {
2710 *match = NULL;
2711 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002712 return LY_ENOTFOUND;
2713 }
2714
Michal Vasko9b368d32020-02-14 13:53:31 +01002715 if (match) {
2716 *match = (struct lyd_node *)siblings;
2717 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002718 return LY_SUCCESS;
2719}
2720
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002721LIBYANG_API_DEF LY_ERR
Michal Vaskoe444f752020-02-10 12:20:06 +01002722lyd_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 +02002723 size_t val_len, struct lyd_node **match)
Michal Vaskoe444f752020-02-10 12:20:06 +01002724{
2725 LY_ERR rc;
2726 struct lyd_node *target = NULL;
Michal Vasko9beceb82022-04-05 12:14:15 +02002727 const struct lyd_node *parent;
Michal Vaskoe444f752020-02-10 12:20:06 +01002728
Michal Vasko4c583e82020-07-17 12:16:14 +02002729 LY_CHECK_ARG_RET(NULL, schema, !(schema->nodetype & (LYS_CHOICE | LYS_CASE)), LY_EINVAL);
Michal Vasko9beceb82022-04-05 12:14:15 +02002730 if (!siblings) {
2731 /* no data */
2732 if (match) {
2733 *match = NULL;
2734 }
2735 return LY_ENOTFOUND;
2736 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002737
Michal Vasko9beceb82022-04-05 12:14:15 +02002738 if ((LYD_CTX(siblings) != schema->module->ctx)) {
2739 /* parent of ext nodes is useless */
2740 parent = (siblings->flags & LYD_EXT) ? NULL : lyd_parent(siblings);
Michal Vaskoaf3df492022-12-02 14:03:52 +01002741 if (lyd_find_schema_ctx(schema, LYD_CTX(siblings), parent, 0, &schema)) {
2742 /* no schema node in siblings so certainly no data node either */
2743 if (match) {
2744 *match = NULL;
2745 }
2746 return LY_ENOTFOUND;
2747 }
Michal Vasko9beceb82022-04-05 12:14:15 +02002748 }
2749
2750 if (siblings->schema && (lysc_data_parent(siblings->schema) != lysc_data_parent(schema))) {
2751 /* schema mismatch */
Michal Vasko9b368d32020-02-14 13:53:31 +01002752 if (match) {
2753 *match = NULL;
2754 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002755 return LY_ENOTFOUND;
2756 }
2757
Michal Vaskof03ed032020-03-04 13:31:44 +01002758 if (key_or_value && !val_len) {
2759 val_len = strlen(key_or_value);
2760 }
2761
Michal Vaskob104f112020-07-17 09:54:54 +02002762 if ((schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) && key_or_value) {
2763 /* create a data node and find the instance */
2764 if (schema->nodetype == LYS_LEAFLIST) {
2765 /* target used attributes: schema, hash, value */
Michal Vasko989cdb42023-10-06 15:32:37 +02002766 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 +02002767 LY_CHECK_RET(rc);
Michal Vaskob104f112020-07-17 09:54:54 +02002768 } else {
Michal Vasko90932a92020-02-12 14:33:03 +01002769 /* target used attributes: schema, hash, child (all keys) */
Michal Vasko004d3152020-06-11 19:59:22 +02002770 LY_CHECK_RET(lyd_create_list2(schema, key_or_value, val_len, &target));
Michal Vasko90932a92020-02-12 14:33:03 +01002771 }
2772
2773 /* find it */
2774 rc = lyd_find_sibling_first(siblings, target, match);
Michal Vaskob104f112020-07-17 09:54:54 +02002775 } else {
2776 /* find the first schema node instance */
2777 rc = lyd_find_sibling_schema(siblings, schema, match);
Michal Vaskoe444f752020-02-10 12:20:06 +01002778 }
2779
Michal Vaskoe444f752020-02-10 12:20:06 +01002780 lyd_free_tree(target);
2781 return rc;
2782}
Michal Vaskoccc02342020-05-21 10:09:21 +02002783
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002784LIBYANG_API_DEF LY_ERR
Michal Vaskoe78faec2021-04-08 17:24:43 +02002785lyd_find_sibling_dup_inst_set(const struct lyd_node *siblings, const struct lyd_node *target, struct ly_set **set)
2786{
2787 struct lyd_node **match_p, *first, *iter;
2788 struct lyd_node_inner *parent;
Michal Vaskoee9b9482023-06-19 13:17:48 +02002789 uint32_t comp_opts;
Michal Vaskoe78faec2021-04-08 17:24:43 +02002790
Michal Vasko83ae7772022-06-08 10:01:55 +02002791 LY_CHECK_ARG_RET(NULL, target, set, LY_EINVAL);
Michal Vasko892f5bf2021-11-24 10:41:05 +01002792 LY_CHECK_CTX_EQUAL_RET(siblings ? LYD_CTX(siblings) : NULL, LYD_CTX(target), LY_EINVAL);
Michal Vaskoe78faec2021-04-08 17:24:43 +02002793
2794 LY_CHECK_RET(ly_set_new(set));
2795
2796 if (!siblings || (siblings->schema && (lysc_data_parent(siblings->schema) != lysc_data_parent(target->schema)))) {
2797 /* no data or schema mismatch */
2798 return LY_ENOTFOUND;
2799 }
2800
Michal Vaskoee9b9482023-06-19 13:17:48 +02002801 /* set options */
Michal Vaskod8a52012023-08-15 11:38:10 +02002802 comp_opts = (lysc_is_dup_inst_list(target->schema) ? LYD_COMPARE_FULL_RECURSION : 0);
Michal Vaskoee9b9482023-06-19 13:17:48 +02002803
Michal Vaskoe78faec2021-04-08 17:24:43 +02002804 /* get first sibling */
2805 siblings = lyd_first_sibling(siblings);
2806
2807 parent = siblings->parent;
2808 if (parent && parent->schema && parent->children_ht) {
2809 assert(target->hash);
2810
2811 /* find the first instance */
2812 lyd_find_sibling_first(siblings, target, &first);
2813 if (first) {
2814 /* add it so that it is the first in the set */
2815 if (ly_set_add(*set, first, 1, NULL)) {
2816 goto error;
2817 }
2818
2819 /* find by hash */
2820 if (!lyht_find(parent->children_ht, &target, target->hash, (void **)&match_p)) {
2821 iter = *match_p;
2822 } else {
2823 /* not found */
2824 iter = NULL;
2825 }
2826 while (iter) {
2827 /* add all found nodes into the set */
Michal Vaskoee9b9482023-06-19 13:17:48 +02002828 if ((iter != first) && !lyd_compare_single(iter, target, comp_opts) && ly_set_add(*set, iter, 1, NULL)) {
Michal Vaskoe78faec2021-04-08 17:24:43 +02002829 goto error;
2830 }
2831
2832 /* find next instance */
2833 if (lyht_find_next(parent->children_ht, &iter, iter->hash, (void **)&match_p)) {
2834 iter = NULL;
2835 } else {
2836 iter = *match_p;
2837 }
2838 }
2839 }
2840 } else {
2841 /* no children hash table */
2842 LY_LIST_FOR(siblings, siblings) {
Michal Vaskoee9b9482023-06-19 13:17:48 +02002843 if (!lyd_compare_single(target, siblings, comp_opts)) {
Michal Vaskoe78faec2021-04-08 17:24:43 +02002844 ly_set_add(*set, (void *)siblings, 1, NULL);
2845 }
2846 }
2847 }
2848
2849 if (!(*set)->count) {
2850 return LY_ENOTFOUND;
2851 }
2852 return LY_SUCCESS;
2853
2854error:
2855 ly_set_free(*set, NULL);
2856 *set = NULL;
2857 return LY_EMEM;
2858}
2859
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002860LIBYANG_API_DEF LY_ERR
Michal Vasko1d4af6c2021-02-22 13:31:26 +01002861lyd_find_sibling_opaq_next(const struct lyd_node *first, const char *name, struct lyd_node **match)
2862{
2863 LY_CHECK_ARG_RET(NULL, name, LY_EINVAL);
2864
Michal Vaskoe271a312023-08-15 11:46:30 +02002865 if (first && first->schema) {
2866 first = first->prev;
2867 if (first->schema) {
2868 /* no opaque nodes */
2869 first = NULL;
2870 } else {
2871 /* opaque nodes are at the end, find quickly the first */
2872 while (!first->prev->schema) {
2873 first = first->prev;
2874 }
2875 }
2876 }
2877
Michal Vasko1d4af6c2021-02-22 13:31:26 +01002878 for ( ; first; first = first->next) {
Michal Vaskoe271a312023-08-15 11:46:30 +02002879 assert(!first->schema);
2880 if (!strcmp(LYD_NAME(first), name)) {
Michal Vasko1d4af6c2021-02-22 13:31:26 +01002881 break;
2882 }
2883 }
2884
2885 if (match) {
2886 *match = (struct lyd_node *)first;
2887 }
2888 return first ? LY_SUCCESS : LY_ENOTFOUND;
2889}
2890
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002891LIBYANG_API_DEF LY_ERR
Michal Vaskod96e2372023-02-24 16:07:51 +01002892lyd_find_xpath(const struct lyd_node *ctx_node, const char *xpath, struct ly_set **set)
Michal Vaskoccc02342020-05-21 10:09:21 +02002893{
Michal Vaskod96e2372023-02-24 16:07:51 +01002894 LY_CHECK_ARG_RET(NULL, ctx_node, xpath, set, LY_EINVAL);
Michal Vaskoccc02342020-05-21 10:09:21 +02002895
Michal Vaskobe1b0cb2024-01-22 14:32:15 +01002896 return lyd_find_xpath3(ctx_node, ctx_node, xpath, LY_VALUE_JSON, NULL, NULL, set);
Michal Vaskod96e2372023-02-24 16:07:51 +01002897}
Michal Vaskoccc02342020-05-21 10:09:21 +02002898
Michal Vaskod96e2372023-02-24 16:07:51 +01002899LIBYANG_API_DEF LY_ERR
2900lyd_find_xpath2(const struct lyd_node *ctx_node, const char *xpath, const struct lyxp_var *vars, struct ly_set **set)
2901{
2902 LY_CHECK_ARG_RET(NULL, ctx_node, xpath, set, LY_EINVAL);
Michal Vaskoccc02342020-05-21 10:09:21 +02002903
Michal Vaskobe1b0cb2024-01-22 14:32:15 +01002904 return lyd_find_xpath3(ctx_node, ctx_node, xpath, LY_VALUE_JSON, NULL, vars, set);
Michal Vaskoccc02342020-05-21 10:09:21 +02002905}
Radek Krejcica989142020-11-05 11:32:22 +01002906
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002907LIBYANG_API_DEF LY_ERR
Michal Vaskobe1b0cb2024-01-22 14:32:15 +01002908lyd_find_xpath3(const struct lyd_node *ctx_node, const struct lyd_node *tree, const char *xpath, LY_VALUE_FORMAT format,
Michal Vaskod96e2372023-02-24 16:07:51 +01002909 void *prefix_data, const struct lyxp_var *vars, struct ly_set **set)
Michal Vaskoe3716b32021-12-13 16:58:25 +01002910{
Michal Vaskod96e2372023-02-24 16:07:51 +01002911 LY_CHECK_ARG_RET(NULL, tree, xpath, set, LY_EINVAL);
Michal Vaskoe3716b32021-12-13 16:58:25 +01002912
Michal Vaskod96e2372023-02-24 16:07:51 +01002913 *set = NULL;
2914
2915 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 +01002916}
2917
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002918LIBYANG_API_DEF LY_ERR
Michal Vaskod96e2372023-02-24 16:07:51 +01002919lyd_eval_xpath(const struct lyd_node *ctx_node, const char *xpath, ly_bool *result)
aPiecekfba75362021-10-07 12:39:48 +02002920{
Michal Vaskod96e2372023-02-24 16:07:51 +01002921 return lyd_eval_xpath3(ctx_node, NULL, xpath, LY_VALUE_JSON, NULL, NULL, result);
Michal Vaskoc9eb3ca2021-07-16 10:20:37 +02002922}
2923
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002924LIBYANG_API_DEF LY_ERR
Michal Vasko10fabfc2022-08-09 08:55:43 +02002925lyd_eval_xpath2(const struct lyd_node *ctx_node, const char *xpath, const struct lyxp_var *vars, ly_bool *result)
2926{
2927 return lyd_eval_xpath3(ctx_node, NULL, xpath, LY_VALUE_JSON, NULL, vars, result);
2928}
2929
2930LIBYANG_API_DEF LY_ERR
Michal Vaskod96e2372023-02-24 16:07:51 +01002931lyd_eval_xpath3(const struct lyd_node *ctx_node, const struct lys_module *cur_mod, const char *xpath,
2932 LY_VALUE_FORMAT format, void *prefix_data, const struct lyxp_var *vars, ly_bool *result)
aPiecekfba75362021-10-07 12:39:48 +02002933{
Michal Vaskod96e2372023-02-24 16:07:51 +01002934 return lyd_eval_xpath4(ctx_node, ctx_node, cur_mod, xpath, format, prefix_data, vars, NULL, NULL, NULL, NULL, result);
2935}
2936
2937LIBYANG_API_DEF LY_ERR
2938lyd_eval_xpath4(const struct lyd_node *ctx_node, const struct lyd_node *tree, const struct lys_module *cur_mod,
2939 const char *xpath, LY_VALUE_FORMAT format, void *prefix_data, const struct lyxp_var *vars, LY_XPATH_TYPE *ret_type,
2940 struct ly_set **node_set, char **string, long double *number, ly_bool *boolean)
2941{
2942 LY_ERR ret = LY_SUCCESS;
2943 struct lyxp_set xp_set = {0};
2944 struct lyxp_expr *exp = NULL;
2945 uint32_t i;
2946
2947 LY_CHECK_ARG_RET(NULL, tree, xpath, ((ret_type && node_set && string && number && boolean) ||
2948 (node_set && !string && !number && !boolean) || (!node_set && string && !number && !boolean) ||
2949 (!node_set && !string && number && !boolean) || (!node_set && !string && !number && boolean)), LY_EINVAL);
2950
2951 /* parse expression */
2952 ret = lyxp_expr_parse((struct ly_ctx *)LYD_CTX(tree), xpath, 0, 1, &exp);
2953 LY_CHECK_GOTO(ret, cleanup);
2954
2955 /* evaluate expression */
2956 ret = lyxp_eval(LYD_CTX(tree), exp, cur_mod, format, prefix_data, ctx_node, ctx_node, tree, vars, &xp_set,
2957 LYXP_IGNORE_WHEN);
2958 LY_CHECK_GOTO(ret, cleanup);
2959
2960 /* return expected result type without or with casting */
2961 if (node_set) {
2962 /* node set */
2963 if (xp_set.type == LYXP_SET_NODE_SET) {
2964 /* transform into a set */
2965 LY_CHECK_GOTO(ret = ly_set_new(node_set), cleanup);
2966 (*node_set)->objs = malloc(xp_set.used * sizeof *(*node_set)->objs);
2967 LY_CHECK_ERR_GOTO(!(*node_set)->objs, LOGMEM(LYD_CTX(tree)); ret = LY_EMEM, cleanup);
2968 (*node_set)->size = xp_set.used;
2969 for (i = 0; i < xp_set.used; ++i) {
2970 if (xp_set.val.nodes[i].type == LYXP_NODE_ELEM) {
2971 ret = ly_set_add(*node_set, xp_set.val.nodes[i].node, 1, NULL);
2972 LY_CHECK_GOTO(ret, cleanup);
2973 }
2974 }
2975 if (ret_type) {
2976 *ret_type = LY_XPATH_NODE_SET;
2977 }
2978 } else if (!string && !number && !boolean) {
2979 LOGERR(LYD_CTX(tree), LY_EINVAL, "XPath \"%s\" result is not a node set.", xpath);
2980 ret = LY_EINVAL;
2981 goto cleanup;
2982 }
2983 }
2984
2985 if (string) {
2986 if ((xp_set.type != LYXP_SET_STRING) && !node_set) {
2987 /* cast into string */
2988 LY_CHECK_GOTO(ret = lyxp_set_cast(&xp_set, LYXP_SET_STRING), cleanup);
2989 }
2990 if (xp_set.type == LYXP_SET_STRING) {
2991 /* string */
2992 *string = xp_set.val.str;
2993 xp_set.val.str = NULL;
2994 if (ret_type) {
2995 *ret_type = LY_XPATH_STRING;
2996 }
2997 }
2998 }
2999
3000 if (number) {
3001 if ((xp_set.type != LYXP_SET_NUMBER) && !node_set) {
3002 /* cast into number */
3003 LY_CHECK_GOTO(ret = lyxp_set_cast(&xp_set, LYXP_SET_NUMBER), cleanup);
3004 }
3005 if (xp_set.type == LYXP_SET_NUMBER) {
3006 /* number */
3007 *number = xp_set.val.num;
3008 if (ret_type) {
3009 *ret_type = LY_XPATH_NUMBER;
3010 }
3011 }
3012 }
3013
3014 if (boolean) {
3015 if ((xp_set.type != LYXP_SET_BOOLEAN) && !node_set) {
3016 /* cast into boolean */
3017 LY_CHECK_GOTO(ret = lyxp_set_cast(&xp_set, LYXP_SET_BOOLEAN), cleanup);
3018 }
3019 if (xp_set.type == LYXP_SET_BOOLEAN) {
3020 /* boolean */
3021 *boolean = xp_set.val.bln;
3022 if (ret_type) {
3023 *ret_type = LY_XPATH_BOOLEAN;
3024 }
3025 }
3026 }
3027
3028cleanup:
3029 lyxp_set_free_content(&xp_set);
3030 lyxp_expr_free((struct ly_ctx *)LYD_CTX(tree), exp);
3031 return ret;
aPiecekfba75362021-10-07 12:39:48 +02003032}
3033
Michal Vasko99a77882024-01-04 14:50:51 +01003034/**
3035 * @brief Hash table node equal callback.
3036 */
3037static ly_bool
3038lyd_trim_equal_cb(void *val1_p, void *val2_p, ly_bool UNUSED(mod), void *UNUSED(cb_data))
3039{
3040 struct lyd_node *node1, *node2;
3041
3042 node1 = *(struct lyd_node **)val1_p;
3043 node2 = *(struct lyd_node **)val2_p;
3044
3045 return node1 == node2;
3046}
3047
3048LIBYANG_API_DEF LY_ERR
3049lyd_trim_xpath(struct lyd_node **tree, const char *xpath, const struct lyxp_var *vars)
3050{
3051 LY_ERR ret = LY_SUCCESS;
Michal Vaskoea4943c2024-01-16 15:27:29 +01003052 struct ly_ctx *ctx = NULL;
Michal Vasko99a77882024-01-04 14:50:51 +01003053 struct lyxp_set xp_set = {0};
3054 struct lyxp_expr *exp = NULL;
3055 struct lyd_node *node, *parent;
3056 struct lyxp_set_hash_node hnode;
3057 struct ly_ht *parent_ht = NULL;
3058 struct ly_set free_set = {0};
3059 uint32_t i, hash;
3060 ly_bool is_result;
3061
3062 LY_CHECK_ARG_RET(NULL, tree, xpath, LY_EINVAL);
3063
3064 if (!*tree) {
3065 /* nothing to do */
3066 goto cleanup;
3067 }
3068
3069 *tree = lyd_first_sibling(*tree);
3070 ctx = (struct ly_ctx *)LYD_CTX(*tree);
3071
3072 /* parse expression */
3073 ret = lyxp_expr_parse(ctx, xpath, 0, 1, &exp);
3074 LY_CHECK_GOTO(ret, cleanup);
3075
3076 /* evaluate expression */
3077 ret = lyxp_eval(ctx, exp, NULL, LY_VALUE_JSON, NULL, *tree, *tree, *tree, vars, &xp_set, LYXP_IGNORE_WHEN);
3078 LY_CHECK_GOTO(ret, cleanup);
3079
3080 /* create hash table for all the parents of results */
Michal Vasko0a0716d2024-01-04 15:02:12 +01003081 parent_ht = lyht_new(32, sizeof node, lyd_trim_equal_cb, NULL, 1);
Michal Vasko99a77882024-01-04 14:50:51 +01003082 LY_CHECK_GOTO(!parent_ht, cleanup);
3083
3084 for (i = 0; i < xp_set.used; ++i) {
3085 if (xp_set.val.nodes[i].type != LYXP_NODE_ELEM) {
3086 /* ignore */
3087 continue;
3088 }
3089
3090 for (parent = lyd_parent(xp_set.val.nodes[i].node); parent; parent = lyd_parent(parent)) {
3091 /* add the parent into parent_ht */
3092 ret = lyht_insert(parent_ht, &parent, parent->hash, NULL);
3093 if (ret == LY_EEXIST) {
3094 /* shared parent, we are done */
3095 break;
3096 }
3097 LY_CHECK_GOTO(ret, cleanup);
3098 }
3099 }
3100
3101 hnode.type = LYXP_NODE_ELEM;
3102 LY_LIST_FOR(*tree, parent) {
3103 LYD_TREE_DFS_BEGIN(parent, node) {
3104 if (lysc_is_key(node->schema)) {
3105 /* ignore */
3106 goto next_iter;
3107 }
3108
3109 /* check the results */
3110 is_result = 0;
3111 if (xp_set.ht) {
3112 hnode.node = node;
3113 hash = lyht_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
3114 hash = lyht_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
3115 hash = lyht_hash_multi(hash, NULL, 0);
3116
3117 if (!lyht_find(xp_set.ht, &hnode, hash, NULL)) {
3118 is_result = 1;
3119 }
3120 } else {
3121 /* not enough elements for a hash table */
3122 for (i = 0; i < xp_set.used; ++i) {
3123 if (xp_set.val.nodes[i].type != LYXP_NODE_ELEM) {
3124 /* ignore */
3125 continue;
3126 }
3127
3128 if (xp_set.val.nodes[i].node == node) {
3129 is_result = 1;
3130 break;
3131 }
3132 }
3133 }
3134
3135 if (is_result) {
3136 /* keep the whole subtree if the node is in the results */
3137 LYD_TREE_DFS_continue = 1;
3138 } else if (lyht_find(parent_ht, &node, node->hash, NULL)) {
3139 /* free the whole subtree if the node is not even among the selected parents */
3140 ret = ly_set_add(&free_set, node, 1, NULL);
3141 LY_CHECK_GOTO(ret, cleanup);
3142 LYD_TREE_DFS_continue = 1;
3143 } /* else keep the parent node because a subtree is in the results */
3144
3145next_iter:
3146 LYD_TREE_DFS_END(parent, node);
3147 }
3148 }
3149
3150 /* free */
3151 for (i = 0; i < free_set.count; ++i) {
3152 node = free_set.dnodes[i];
3153 if (*tree == node) {
3154 *tree = (*tree)->next;
3155 }
3156 lyd_free_tree(node);
3157 }
3158
3159cleanup:
3160 lyxp_set_free_content(&xp_set);
3161 lyxp_expr_free(ctx, exp);
3162 lyht_free(parent_ht, NULL);
3163 ly_set_erase(&free_set, NULL);
3164 return ret;
3165}
3166
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01003167LIBYANG_API_DEF LY_ERR
Michal Vasko3e1f6552021-01-14 09:27:55 +01003168lyd_find_path(const struct lyd_node *ctx_node, const char *path, ly_bool output, struct lyd_node **match)
3169{
3170 LY_ERR ret = LY_SUCCESS;
3171 struct lyxp_expr *expr = NULL;
3172 struct ly_path *lypath = NULL;
3173
3174 LY_CHECK_ARG_RET(NULL, ctx_node, ctx_node->schema, path, LY_EINVAL);
3175
3176 /* parse the path */
Michal Vaskoed725d72021-06-23 12:03:45 +02003177 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 +01003178 LY_PATH_PREFIX_FIRST, LY_PATH_PRED_SIMPLE, &expr);
Michal Vasko3e1f6552021-01-14 09:27:55 +01003179 LY_CHECK_GOTO(ret, cleanup);
3180
3181 /* compile the path */
Michal Vaskoed725d72021-06-23 12:03:45 +02003182 ret = ly_path_compile(LYD_CTX(ctx_node), NULL, ctx_node->schema, NULL, expr,
Michal Vasko0884d212021-10-14 09:21:46 +02003183 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 +01003184 LY_CHECK_GOTO(ret, cleanup);
3185
3186 /* evaluate the path */
Michal Vasko838829d2023-10-09 16:06:43 +02003187 ret = ly_path_eval_partial(lypath, ctx_node, NULL, 0, NULL, match);
Michal Vasko3e1f6552021-01-14 09:27:55 +01003188
3189cleanup:
3190 lyxp_expr_free(LYD_CTX(ctx_node), expr);
3191 ly_path_free(LYD_CTX(ctx_node), lypath);
3192 return ret;
3193}
3194
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01003195LIBYANG_API_DEF LY_ERR
Michal Vaskobb22b182021-06-14 08:14:21 +02003196lyd_find_target(const struct ly_path *path, const struct lyd_node *tree, struct lyd_node **match)
3197{
3198 LY_ERR ret;
3199 struct lyd_node *m;
3200
3201 LY_CHECK_ARG_RET(NULL, path, LY_EINVAL);
3202
Michal Vasko90189962023-02-28 12:10:34 +01003203 ret = ly_path_eval(path, tree, NULL, &m);
Michal Vaskobb22b182021-06-14 08:14:21 +02003204 if (ret) {
3205 if (match) {
3206 *match = NULL;
3207 }
3208 return LY_ENOTFOUND;
3209 }
3210
3211 if (match) {
3212 *match = m;
3213 }
3214 return LY_SUCCESS;
3215}
Irfand3b351a2023-09-14 14:52:15 +02003216
stewegf9041a22024-01-18 13:29:12 +01003217LY_ERR
3218lyd_get_or_create_leafref_links_record(const struct lyd_node_term *node, struct lyd_leafref_links_rec **record, ly_bool create)
3219{
3220 struct ly_ht *ht;
3221 uint32_t hash;
Michal Vaskob46061d2024-01-18 13:58:13 +01003222 struct lyd_leafref_links_rec rec = {0};
stewegf9041a22024-01-18 13:29:12 +01003223
3224 assert(node);
3225 assert(record);
3226
Michal Vaskob46061d2024-01-18 13:58:13 +01003227 *record = NULL;
3228
stewegf9041a22024-01-18 13:29:12 +01003229 if (!(ly_ctx_get_options(LYD_CTX(node)) & LY_CTX_LEAFREF_LINKING)) {
stewegf9041a22024-01-18 13:29:12 +01003230 return LY_EDENIED;
3231 }
3232
3233 rec.node = node;
stewegf9041a22024-01-18 13:29:12 +01003234 ht = LYD_CTX(node)->leafref_links_ht;
Michal Vasko67bf5872024-01-18 13:58:25 +01003235 hash = lyht_hash((const char *)&node, sizeof node);
3236
stewegf9041a22024-01-18 13:29:12 +01003237 if (lyht_find(ht, &rec, hash, (void **)record) == LY_ENOTFOUND) {
3238 if (create) {
3239 LY_CHECK_RET(lyht_insert_no_check(ht, &rec, hash, (void **)record));
3240 } else {
stewegf9041a22024-01-18 13:29:12 +01003241 return LY_ENOTFOUND;
3242 }
3243 }
3244
3245 return LY_SUCCESS;
3246}
3247
3248LIBYANG_API_DEF LY_ERR
3249lyd_leafref_get_links(const struct lyd_node_term *node, const struct lyd_leafref_links_rec **record)
3250{
3251 LY_CHECK_ARG_RET(NULL, node, record, LY_EINVAL);
3252
3253 return lyd_get_or_create_leafref_links_record(node, (struct lyd_leafref_links_rec **)record, 0);
3254}
3255
3256LY_ERR
3257lyd_link_leafref_node(const struct lyd_node_term *node, const struct lyd_node_term *leafref_node)
3258{
stewegf9041a22024-01-18 13:29:12 +01003259 const struct lyd_node_term **item = NULL;
3260 struct lyd_leafref_links_rec *rec;
steweg67388952024-01-25 12:14:50 +01003261 LY_ARRAY_COUNT_TYPE u;
stewegf9041a22024-01-18 13:29:12 +01003262
3263 assert(node);
3264 assert(leafref_node);
3265
3266 if (!(ly_ctx_get_options(LYD_CTX(node)) & LY_CTX_LEAFREF_LINKING)) {
3267 return LY_EDENIED;
3268 }
3269
steweg67388952024-01-25 12:14:50 +01003270 /* add leafref node into the list of target node */
stewegf9041a22024-01-18 13:29:12 +01003271 LY_CHECK_RET(lyd_get_or_create_leafref_links_record(node, &rec, 1));
3272 LY_ARRAY_FOR(rec->leafref_nodes, u) {
3273 if (rec->leafref_nodes[u] == leafref_node) {
3274 return LY_SUCCESS;
3275 }
3276 }
3277
3278 LY_ARRAY_NEW_RET(LYD_CTX(node), rec->leafref_nodes, item, LY_EMEM);
3279 *item = leafref_node;
steweg67388952024-01-25 12:14:50 +01003280
3281 /* add target node into the list of leafref node*/
stewegf9041a22024-01-18 13:29:12 +01003282 LY_CHECK_RET(lyd_get_or_create_leafref_links_record(leafref_node, &rec, 1));
steweg67388952024-01-25 12:14:50 +01003283 LY_ARRAY_FOR(rec->target_nodes, u) {
3284 if (rec->target_nodes[u] == node) {
3285 return LY_SUCCESS;
3286 }
3287 }
3288
3289 LY_ARRAY_NEW_RET(LYD_CTX(node), rec->target_nodes, item, LY_EMEM);
3290 *item = node;
3291
stewegf9041a22024-01-18 13:29:12 +01003292 return LY_SUCCESS;
3293}
3294
3295LIBYANG_API_DEF LY_ERR
3296lyd_leafref_link_node_tree(const struct lyd_node *tree)
3297{
3298 const struct lyd_node *sibling, *elem;
steweg67388952024-01-25 12:14:50 +01003299 struct ly_set *targets = NULL;
stewegf9041a22024-01-18 13:29:12 +01003300 char *errmsg;
3301 struct lyd_node_term *leafref_node;
3302 struct lysc_node_leaf *leaf_schema;
3303 struct lysc_type_leafref *lref;
steweg67388952024-01-25 12:14:50 +01003304 LY_ERR ret = LY_SUCCESS;
3305 uint32_t i;
stewegf9041a22024-01-18 13:29:12 +01003306
3307 LY_CHECK_ARG_RET(NULL, tree, LY_EINVAL);
3308
3309 if (!(ly_ctx_get_options(LYD_CTX(tree)) & LY_CTX_LEAFREF_LINKING)) {
3310 return LY_EDENIED;
3311 }
3312
3313 LY_LIST_FOR(tree, sibling) {
3314 LYD_TREE_DFS_BEGIN(sibling, elem) {
steweg0e1e5092024-02-12 09:06:04 +01003315 if (elem->schema && (elem->schema->nodetype & LYD_NODE_TERM)) {
stewegf9041a22024-01-18 13:29:12 +01003316 leafref_node = (struct lyd_node_term *)elem;
3317 leaf_schema = (struct lysc_node_leaf *)elem->schema;
Michal Vaskob46061d2024-01-18 13:58:13 +01003318
stewegf9041a22024-01-18 13:29:12 +01003319 if (leaf_schema->type->basetype == LY_TYPE_LEAFREF) {
3320 lref = (struct lysc_type_leafref *)leaf_schema->type;
steweg67388952024-01-25 12:14:50 +01003321 ly_set_free(targets, NULL);
3322 if (lyplg_type_resolve_leafref(lref, elem, &leafref_node->value, tree, &targets, &errmsg)) {
Michal Vaskob46061d2024-01-18 13:58:13 +01003323 /* leafref target not found */
stewegf9041a22024-01-18 13:29:12 +01003324 free(errmsg);
Michal Vaskob46061d2024-01-18 13:58:13 +01003325 } else {
3326 /* leafref target found, link it */
steweg67388952024-01-25 12:14:50 +01003327 for (i = 0; i < targets->count; ++i) {
3328 if (targets->dnodes[i]->schema->nodetype & LYD_NODE_TERM) {
3329 ret = lyd_link_leafref_node((struct lyd_node_term *)targets->dnodes[i], leafref_node);
3330 LY_CHECK_GOTO(ret, cleanup);
3331 }
3332 }
stewegf9041a22024-01-18 13:29:12 +01003333 }
3334 }
3335 }
3336 LYD_TREE_DFS_END(sibling, elem);
3337 }
3338 }
Michal Vaskob46061d2024-01-18 13:58:13 +01003339
steweg67388952024-01-25 12:14:50 +01003340cleanup:
3341 ly_set_free(targets, NULL);
3342 return ret;
stewegf9041a22024-01-18 13:29:12 +01003343}
3344
3345LY_ERR
3346lyd_unlink_leafref_node(const struct lyd_node_term *node, const struct lyd_node_term *leafref_node)
3347{
3348 LY_ERR ret;
3349 struct lyd_leafref_links_rec *rec;
3350
3351 assert(node);
3352 assert(leafref_node);
3353
3354 if (!(ly_ctx_get_options(LYD_CTX(node)) & LY_CTX_LEAFREF_LINKING)) {
3355 return LY_EDENIED;
3356 }
3357
steweg67388952024-01-25 12:14:50 +01003358 /* remove link from target node to leafref node */
stewegf9041a22024-01-18 13:29:12 +01003359 ret = lyd_get_or_create_leafref_links_record(node, &rec, 0);
3360 if (ret == LY_SUCCESS) {
3361 LY_ARRAY_REMOVE_VALUE(rec->leafref_nodes, leafref_node);
steweg67388952024-01-25 12:14:50 +01003362 if ((LY_ARRAY_COUNT(rec->leafref_nodes) == 0) && (LY_ARRAY_COUNT(rec->target_nodes) == 0)) {
stewegf9041a22024-01-18 13:29:12 +01003363 lyd_free_leafref_nodes(node);
3364 }
3365 } else if (ret != LY_ENOTFOUND) {
3366 return ret;
3367 }
3368
steweg67388952024-01-25 12:14:50 +01003369 /* remove link from leafref node to target node */
stewegf9041a22024-01-18 13:29:12 +01003370 ret = lyd_get_or_create_leafref_links_record(leafref_node, &rec, 0);
3371 if (ret == LY_SUCCESS) {
steweg67388952024-01-25 12:14:50 +01003372 LY_ARRAY_REMOVE_VALUE(rec->target_nodes, node);
3373 if ((LY_ARRAY_COUNT(rec->leafref_nodes) == 0) && (LY_ARRAY_COUNT(rec->target_nodes) == 0)) {
stewegf9041a22024-01-18 13:29:12 +01003374 lyd_free_leafref_nodes(leafref_node);
3375 }
3376 } else if (ret != LY_ENOTFOUND) {
3377 return ret;
3378 }
3379
3380 return LY_SUCCESS;
3381}