blob: 0d10d72c3169891cabb3ff05d37dc8a24b7510b4 [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
aPiecek32bd0d22023-11-16 14:47:04 +0100833 sibling = lyd_first_sibling(sibling);
Michal Vasko553d0b52020-12-04 16:27:52 +0100834
Michal Vaskob1b5c262020-03-05 14:29:47 +0100835 if (node->parent || node->prev->next) {
Michal Vasko2e784f82024-01-11 09:51:22 +0100836 lyd_unlink(node);
Michal Vaskob1b5c262020-03-05 14:29:47 +0100837 }
838
839 while (node) {
Michal Vasko71e795e2020-12-04 16:27:37 +0100840 if (lysc_is_key(node->schema)) {
Michal Vaskob7be7a82020-08-20 09:09:04 +0200841 LOGERR(LYD_CTX(node), LY_EINVAL, "Cannot insert key \"%s\".", node->schema->name);
Michal Vaskob104f112020-07-17 09:54:54 +0200842 return LY_EINVAL;
843 }
844
Michal Vaskob1b5c262020-03-05 14:29:47 +0100845 iter = node->next;
Michal Vasko2e784f82024-01-11 09:51:22 +0100846 lyd_unlink(node);
Michal Vasko6ee6f432021-07-16 09:49:14 +0200847 lyd_insert_node(NULL, &sibling, node, 0);
Michal Vaskob1b5c262020-03-05 14:29:47 +0100848 node = iter;
849 }
Michal Vaskob1b5c262020-03-05 14:29:47 +0100850
Michal Vaskob104f112020-07-17 09:54:54 +0200851 if (first) {
852 /* find the first sibling */
853 *first = sibling;
854 while ((*first)->prev->next) {
855 *first = (*first)->prev;
Michal Vasko0249f7c2020-03-05 16:36:40 +0100856 }
857 }
858
859 return LY_SUCCESS;
860}
861
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100862LIBYANG_API_DEF LY_ERR
Michal Vaskof03ed032020-03-04 13:31:44 +0100863lyd_insert_before(struct lyd_node *sibling, struct lyd_node *node)
864{
Michal Vaskobce230b2022-04-19 09:55:31 +0200865 LY_CHECK_ARG_RET(NULL, sibling, node, sibling != node, LY_EINVAL);
Michal Vasko892f5bf2021-11-24 10:41:05 +0100866 LY_CHECK_CTX_EQUAL_RET(LYD_CTX(sibling), LYD_CTX(node), LY_EINVAL);
Michal Vaskof03ed032020-03-04 13:31:44 +0100867
Michal Vasko717a4c32020-12-07 09:40:10 +0100868 LY_CHECK_RET(lyd_insert_check_schema(NULL, sibling->schema, node->schema));
Michal Vaskof03ed032020-03-04 13:31:44 +0100869
Michal Vaskoab7a2bf2022-04-01 14:37:54 +0200870 if (node->schema && (!(node->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) || !(node->schema->flags & LYS_ORDBY_USER))) {
Michal Vaskob7be7a82020-08-20 09:09:04 +0200871 LOGERR(LYD_CTX(sibling), LY_EINVAL, "Can be used only for user-ordered nodes.");
Michal Vaskof03ed032020-03-04 13:31:44 +0100872 return LY_EINVAL;
873 }
Michal Vasko5c0b27a2022-04-19 09:56:02 +0200874 if (node->schema && sibling->schema && (node->schema != sibling->schema)) {
875 LOGERR(LYD_CTX(sibling), LY_EINVAL, "Cannot insert before a different schema node instance.");
Michal Vasko7508ef22022-02-22 14:13:10 +0100876 return LY_EINVAL;
877 }
Michal Vaskof03ed032020-03-04 13:31:44 +0100878
Michal Vasko2e784f82024-01-11 09:51:22 +0100879 lyd_unlink(node);
Michal Vasko4bc2ce32020-12-08 10:06:16 +0100880 lyd_insert_before_node(sibling, node);
881 lyd_insert_hash(node);
Michal Vaskof03ed032020-03-04 13:31:44 +0100882
Michal Vaskof03ed032020-03-04 13:31:44 +0100883 return LY_SUCCESS;
884}
885
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100886LIBYANG_API_DEF LY_ERR
Michal Vaskof03ed032020-03-04 13:31:44 +0100887lyd_insert_after(struct lyd_node *sibling, struct lyd_node *node)
888{
Michal Vaskobce230b2022-04-19 09:55:31 +0200889 LY_CHECK_ARG_RET(NULL, sibling, node, sibling != node, LY_EINVAL);
Michal Vasko892f5bf2021-11-24 10:41:05 +0100890 LY_CHECK_CTX_EQUAL_RET(LYD_CTX(sibling), LYD_CTX(node), LY_EINVAL);
Michal Vaskof03ed032020-03-04 13:31:44 +0100891
Michal Vasko717a4c32020-12-07 09:40:10 +0100892 LY_CHECK_RET(lyd_insert_check_schema(NULL, sibling->schema, node->schema));
Michal Vaskof03ed032020-03-04 13:31:44 +0100893
Michal Vaskoab7a2bf2022-04-01 14:37:54 +0200894 if (node->schema && (!(node->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) || !(node->schema->flags & LYS_ORDBY_USER))) {
Michal Vaskob7be7a82020-08-20 09:09:04 +0200895 LOGERR(LYD_CTX(sibling), LY_EINVAL, "Can be used only for user-ordered nodes.");
Michal Vaskof03ed032020-03-04 13:31:44 +0100896 return LY_EINVAL;
897 }
Michal Vasko5c0b27a2022-04-19 09:56:02 +0200898 if (node->schema && sibling->schema && (node->schema != sibling->schema)) {
899 LOGERR(LYD_CTX(sibling), LY_EINVAL, "Cannot insert after a different schema node instance.");
Michal Vasko7508ef22022-02-22 14:13:10 +0100900 return LY_EINVAL;
901 }
Michal Vaskof03ed032020-03-04 13:31:44 +0100902
Michal Vasko2e784f82024-01-11 09:51:22 +0100903 lyd_unlink(node);
Michal Vasko4bc2ce32020-12-08 10:06:16 +0100904 lyd_insert_after_node(sibling, node);
905 lyd_insert_hash(node);
Michal Vaskof03ed032020-03-04 13:31:44 +0100906
Michal Vaskof03ed032020-03-04 13:31:44 +0100907 return LY_SUCCESS;
908}
909
Michal Vasko2e784f82024-01-11 09:51:22 +0100910void
911lyd_unlink(struct lyd_node *node)
Michal Vaskof03ed032020-03-04 13:31:44 +0100912{
913 struct lyd_node *iter;
914
915 if (!node) {
916 return;
917 }
918
Michal Vaskob104f112020-07-17 09:54:54 +0200919 /* update hashes while still linked into the tree */
920 lyd_unlink_hash(node);
921
stewegf9041a22024-01-18 13:29:12 +0100922 /* unlink leafref nodes */
923 if (node->schema && (node->schema->nodetype & LYD_NODE_TERM)) {
924 lyd_free_leafref_nodes((struct lyd_node_term *)node);
925 }
926
Michal Vaskof03ed032020-03-04 13:31:44 +0100927 /* unlink from siblings */
928 if (node->prev->next) {
929 node->prev->next = node->next;
930 }
931 if (node->next) {
932 node->next->prev = node->prev;
933 } else {
934 /* unlinking the last node */
935 if (node->parent) {
936 iter = node->parent->child;
937 } else {
938 iter = node->prev;
939 while (iter->prev != node) {
940 iter = iter->prev;
941 }
942 }
943 /* update the "last" pointer from the first node */
944 iter->prev = node->prev;
945 }
946
947 /* unlink from parent */
948 if (node->parent) {
949 if (node->parent->child == node) {
950 /* the node is the first child */
951 node->parent->child = node->next;
952 }
953
Michal Vaskoab49dbe2020-07-17 12:32:47 +0200954 /* check for NP container whether its last non-default node is not being unlinked */
Michal Vasko4754d4a2022-12-01 10:11:21 +0100955 lyd_cont_set_dflt(lyd_parent(node));
Michal Vaskoab49dbe2020-07-17 12:32:47 +0200956
Michal Vaskof03ed032020-03-04 13:31:44 +0100957 node->parent = NULL;
958 }
959
960 node->next = NULL;
961 node->prev = node;
962}
963
Michal Vasko2e784f82024-01-11 09:51:22 +0100964LIBYANG_API_DEF void
965lyd_unlink_siblings(struct lyd_node *node)
966{
967 struct lyd_node *next, *elem, *first = NULL;
968
969 LY_LIST_FOR_SAFE(node, next, elem) {
970 if (lysc_is_key(elem->schema) && elem->parent) {
971 LOGERR(LYD_CTX(elem), LY_EINVAL, "Cannot unlink a list key \"%s\", unlink the list instance instead.",
972 LYD_NAME(elem));
973 return;
974 }
975
976 lyd_unlink(elem);
977 lyd_insert_node(NULL, &first, elem, 1);
978 }
979}
980
981LIBYANG_API_DEF void
982lyd_unlink_tree(struct lyd_node *node)
983{
984 if (node && lysc_is_key(node->schema) && node->parent) {
985 LOGERR(LYD_CTX(node), LY_EINVAL, "Cannot unlink a list key \"%s\", unlink the list instance instead.",
986 LYD_NAME(node));
987 return;
988 }
989
990 lyd_unlink(node);
991}
992
Michal Vaskoa5da3292020-08-12 13:10:50 +0200993void
Michal Vasko871a0252020-11-11 18:35:24 +0100994lyd_insert_meta(struct lyd_node *parent, struct lyd_meta *meta, ly_bool clear_dflt)
Radek Krejci1798aae2020-07-14 13:26:06 +0200995{
996 struct lyd_meta *last, *iter;
997
998 assert(parent);
Michal Vaskoa5da3292020-08-12 13:10:50 +0200999
1000 if (!meta) {
1001 return;
1002 }
Radek Krejci1798aae2020-07-14 13:26:06 +02001003
1004 for (iter = meta; iter; iter = iter->next) {
1005 iter->parent = parent;
1006 }
1007
1008 /* insert as the last attribute */
1009 if (parent->meta) {
Radek Krejci1e008d22020-08-17 11:37:37 +02001010 for (last = parent->meta; last->next; last = last->next) {}
Radek Krejci1798aae2020-07-14 13:26:06 +02001011 last->next = meta;
1012 } else {
1013 parent->meta = meta;
1014 }
1015
1016 /* remove default flags from NP containers */
Michal Vasko871a0252020-11-11 18:35:24 +01001017 while (clear_dflt && parent && (parent->schema->nodetype == LYS_CONTAINER) && (parent->flags & LYD_DEFAULT)) {
Radek Krejci1798aae2020-07-14 13:26:06 +02001018 parent->flags &= ~LYD_DEFAULT;
Michal Vasko9e685082021-01-29 14:49:09 +01001019 parent = lyd_parent(parent);
Radek Krejci1798aae2020-07-14 13:26:06 +02001020 }
Radek Krejci1798aae2020-07-14 13:26:06 +02001021}
1022
aPiecek0e92afc2023-11-08 10:48:02 +01001023void
1024lyd_unlink_meta_single(struct lyd_meta *meta)
1025{
1026 struct lyd_meta *iter;
1027
1028 if (!meta) {
1029 return;
1030 }
1031
1032 if (meta->parent && (meta->parent->meta == meta)) {
1033 meta->parent->meta = meta->next;
1034 } else if (meta->parent) {
1035 for (iter = meta->parent->meta; iter->next && (iter->next != meta); iter = iter->next) {}
1036 if (iter->next) {
1037 iter->next = meta->next;
1038 }
1039 }
1040
1041 meta->next = NULL;
1042 meta->parent = NULL;
1043}
1044
aPiecek0b1df642023-11-06 16:15:33 +01001045/**
1046 * @brief Get the annotation definition in the module.
1047 *
1048 * @param[in] mod Metadata module (with the annotation definition).
1049 * @param[in] name Attribute name.
1050 * @param[in] name_len Length of @p name, must be set correctly.
1051 * @return compiled YANG extension instance on success.
1052 */
1053static struct lysc_ext_instance *
1054lyd_get_meta_annotation(const struct lys_module *mod, const char *name, size_t name_len)
1055{
1056 LY_ARRAY_COUNT_TYPE u;
1057 struct lyplg_ext *plugin;
1058
1059 if (!mod) {
1060 return NULL;
1061 }
1062
1063 LY_ARRAY_FOR(mod->compiled->exts, u) {
1064 plugin = mod->compiled->exts[u].def->plugin;
1065 if (plugin && !strncmp(plugin->id, "ly2 metadata", 12) &&
1066 !ly_strncmp(mod->compiled->exts[u].argument, name, name_len)) {
1067 return &mod->compiled->exts[u];
1068 }
1069 }
1070
1071 return NULL;
1072}
1073
Radek Krejci1798aae2020-07-14 13:26:06 +02001074LY_ERR
Michal Vasko9f96a052020-03-10 09:41:45 +01001075lyd_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 +02001076 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 +01001077 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 +01001078{
Radek Krejci2efc45b2020-12-22 16:25:44 +01001079 LY_ERR ret = LY_SUCCESS;
Michal Vasko90932a92020-02-12 14:33:03 +01001080 struct lysc_ext_instance *ant = NULL;
Michal Vasko193dacd2022-10-13 08:43:05 +02001081 const struct lysc_type *ant_type;
Michal Vasko9f96a052020-03-10 09:41:45 +01001082 struct lyd_meta *mt, *last;
Michal Vasko90932a92020-02-12 14:33:03 +01001083
Michal Vasko9f96a052020-03-10 09:41:45 +01001084 assert((parent || meta) && mod);
Michal Vasko6f4cbb62020-02-28 11:15:47 +01001085
aPiecek0b1df642023-11-06 16:15:33 +01001086 ant = lyd_get_meta_annotation(mod, name, name_len);
Michal Vasko90932a92020-02-12 14:33:03 +01001087 if (!ant) {
1088 /* attribute is not defined as a metadata annotation (RFC 7952) */
Radek Krejci2efc45b2020-12-22 16:25:44 +01001089 LOGVAL(mod->ctx, LYVE_REFERENCE, "Annotation definition for attribute \"%s:%.*s\" not found.",
Radek Krejci422afb12021-03-04 16:38:16 +01001090 mod->name, (int)name_len, name);
Radek Krejci2efc45b2020-12-22 16:25:44 +01001091 ret = LY_EINVAL;
1092 goto cleanup;
Michal Vasko90932a92020-02-12 14:33:03 +01001093 }
1094
Michal Vasko9f96a052020-03-10 09:41:45 +01001095 mt = calloc(1, sizeof *mt);
Radek Krejci2efc45b2020-12-22 16:25:44 +01001096 LY_CHECK_ERR_GOTO(!mt, LOGMEM(mod->ctx); ret = LY_EMEM, cleanup);
Michal Vasko9f96a052020-03-10 09:41:45 +01001097 mt->parent = parent;
1098 mt->annotation = ant;
Michal Vaskofbd037c2022-11-08 10:34:20 +01001099 lyplg_ext_get_storage(ant, LY_STMT_TYPE, sizeof ant_type, (const void **)&ant_type);
Michal Vasko989cdb42023-10-06 15:32:37 +02001100 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 +01001101 ctx_node, incomplete);
Radek Krejci2efc45b2020-12-22 16:25:44 +01001102 LY_CHECK_ERR_GOTO(ret, free(mt), cleanup);
1103 ret = lydict_insert(mod->ctx, name, name_len, &mt->name);
1104 LY_CHECK_ERR_GOTO(ret, free(mt), cleanup);
Michal Vasko90932a92020-02-12 14:33:03 +01001105
Michal Vasko6f4cbb62020-02-28 11:15:47 +01001106 /* insert as the last attribute */
1107 if (parent) {
Michal Vasko871a0252020-11-11 18:35:24 +01001108 lyd_insert_meta(parent, mt, clear_dflt);
Michal Vasko9f96a052020-03-10 09:41:45 +01001109 } else if (*meta) {
Radek Krejci1e008d22020-08-17 11:37:37 +02001110 for (last = *meta; last->next; last = last->next) {}
Michal Vasko9f96a052020-03-10 09:41:45 +01001111 last->next = mt;
Michal Vasko90932a92020-02-12 14:33:03 +01001112 }
1113
Michal Vasko9f96a052020-03-10 09:41:45 +01001114 if (meta) {
1115 *meta = mt;
Michal Vasko90932a92020-02-12 14:33:03 +01001116 }
Radek Krejci2efc45b2020-12-22 16:25:44 +01001117
1118cleanup:
Radek Krejci2efc45b2020-12-22 16:25:44 +01001119 return ret;
Michal Vasko90932a92020-02-12 14:33:03 +01001120}
1121
Michal Vaskoa5da3292020-08-12 13:10:50 +02001122void
1123lyd_insert_attr(struct lyd_node *parent, struct lyd_attr *attr)
1124{
1125 struct lyd_attr *last, *iter;
1126 struct lyd_node_opaq *opaq;
1127
1128 assert(parent && !parent->schema);
1129
1130 if (!attr) {
1131 return;
1132 }
1133
1134 opaq = (struct lyd_node_opaq *)parent;
1135 for (iter = attr; iter; iter = iter->next) {
1136 iter->parent = opaq;
1137 }
1138
1139 /* insert as the last attribute */
1140 if (opaq->attr) {
Radek Krejci1e008d22020-08-17 11:37:37 +02001141 for (last = opaq->attr; last->next; last = last->next) {}
Michal Vaskoa5da3292020-08-12 13:10:50 +02001142 last->next = attr;
1143 } else {
1144 opaq->attr = attr;
1145 }
1146}
1147
Michal Vasko52927e22020-03-16 17:26:14 +01001148LY_ERR
Michal Vaskofeca4fb2020-10-05 08:58:40 +02001149lyd_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 +01001150 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 +02001151 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 +01001152{
Radek Krejci011e4aa2020-09-04 15:22:31 +02001153 LY_ERR ret = LY_SUCCESS;
Radek Krejci1798aae2020-07-14 13:26:06 +02001154 struct lyd_attr *at, *last;
Michal Vasko52927e22020-03-16 17:26:14 +01001155
1156 assert(ctx && (parent || attr) && (!parent || !parent->schema));
Michal Vaskofeca4fb2020-10-05 08:58:40 +02001157 assert(name && name_len && format);
Michal Vasko52927e22020-03-16 17:26:14 +01001158
Michal Vasko2a3722d2021-06-16 11:52:39 +02001159 if (!value_len && (!dynamic || !*dynamic)) {
Michal Vasko52927e22020-03-16 17:26:14 +01001160 value = "";
1161 }
1162
1163 at = calloc(1, sizeof *at);
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01001164 LY_CHECK_ERR_RET(!at, LOGMEM(ctx); ly_free_prefix_data(format, val_prefix_data), LY_EMEM);
Radek Krejcid46e46a2020-09-15 14:22:42 +02001165
Michal Vaskoad92b672020-11-12 13:11:31 +01001166 LY_CHECK_GOTO(ret = lydict_insert(ctx, name, name_len, &at->name.name), finish);
Michal Vasko501af032020-11-11 20:27:44 +01001167 if (prefix_len) {
Michal Vaskoad92b672020-11-12 13:11:31 +01001168 LY_CHECK_GOTO(ret = lydict_insert(ctx, prefix, prefix_len, &at->name.prefix), finish);
Michal Vasko501af032020-11-11 20:27:44 +01001169 }
1170 if (module_key_len) {
Michal Vaskoad92b672020-11-12 13:11:31 +01001171 LY_CHECK_GOTO(ret = lydict_insert(ctx, module_key, module_key_len, &at->name.module_ns), finish);
Michal Vasko501af032020-11-11 20:27:44 +01001172 }
1173
Michal Vasko52927e22020-03-16 17:26:14 +01001174 if (dynamic && *dynamic) {
Radek Krejci011e4aa2020-09-04 15:22:31 +02001175 ret = lydict_insert_zc(ctx, (char *)value, &at->value);
1176 LY_CHECK_GOTO(ret, finish);
Michal Vasko52927e22020-03-16 17:26:14 +01001177 *dynamic = 0;
1178 } else {
Radek Krejci011e4aa2020-09-04 15:22:31 +02001179 LY_CHECK_GOTO(ret = lydict_insert(ctx, value, value_len, &at->value), finish);
Michal Vasko52927e22020-03-16 17:26:14 +01001180 }
Michal Vasko501af032020-11-11 20:27:44 +01001181 at->format = format;
1182 at->val_prefix_data = val_prefix_data;
1183 at->hints = hints;
Michal Vasko52927e22020-03-16 17:26:14 +01001184
1185 /* insert as the last attribute */
1186 if (parent) {
Michal Vaskoa5da3292020-08-12 13:10:50 +02001187 lyd_insert_attr(parent, at);
Michal Vasko52927e22020-03-16 17:26:14 +01001188 } else if (*attr) {
Radek Krejci1e008d22020-08-17 11:37:37 +02001189 for (last = *attr; last->next; last = last->next) {}
Michal Vasko52927e22020-03-16 17:26:14 +01001190 last->next = at;
1191 }
1192
Radek Krejci011e4aa2020-09-04 15:22:31 +02001193finish:
1194 if (ret) {
1195 lyd_free_attr_single(ctx, at);
1196 } else if (attr) {
Michal Vasko52927e22020-03-16 17:26:14 +01001197 *attr = at;
1198 }
1199 return LY_SUCCESS;
1200}
1201
aPiecek2f63f952021-03-30 12:22:18 +02001202/**
1203 * @brief Check the equality of the two schemas from different contexts.
1204 *
1205 * @param schema1 of first node.
1206 * @param schema2 of second node.
1207 * @return 1 if the schemas are equal otherwise 0.
1208 */
1209static ly_bool
1210lyd_compare_schema_equal(const struct lysc_node *schema1, const struct lysc_node *schema2)
1211{
1212 if (!schema1 && !schema2) {
1213 return 1;
1214 } else if (!schema1 || !schema2) {
1215 return 0;
1216 }
1217
1218 assert(schema1->module->ctx != schema2->module->ctx);
1219
1220 if (schema1->nodetype != schema2->nodetype) {
1221 return 0;
1222 }
1223
1224 if (strcmp(schema1->name, schema2->name)) {
1225 return 0;
1226 }
1227
1228 if (strcmp(schema1->module->name, schema2->module->name)) {
1229 return 0;
1230 }
1231
aPiecek2f63f952021-03-30 12:22:18 +02001232 return 1;
1233}
1234
1235/**
1236 * @brief Check the equality of the schemas for all parent nodes.
1237 *
1238 * Both nodes must be from different contexts.
1239 *
1240 * @param node1 Data of first node.
1241 * @param node2 Data of second node.
1242 * @return 1 if the all related parental schemas are equal otherwise 0.
1243 */
1244static ly_bool
1245lyd_compare_schema_parents_equal(const struct lyd_node *node1, const struct lyd_node *node2)
1246{
1247 const struct lysc_node *parent1, *parent2;
1248
1249 assert(node1 && node2);
1250
1251 for (parent1 = node1->schema->parent, parent2 = node2->schema->parent;
1252 parent1 && parent2;
1253 parent1 = parent1->parent, parent2 = parent2->parent) {
1254 if (!lyd_compare_schema_equal(parent1, parent2)) {
1255 return 0;
1256 }
1257 }
1258
1259 if (parent1 || parent2) {
1260 return 0;
1261 }
1262
1263 return 1;
1264}
1265
1266/**
Michal Vaskodf8ebf62022-11-10 10:33:28 +01001267 * @brief Compare 2 nodes values including opaque node values.
1268 *
1269 * @param[in] node1 First node to compare.
1270 * @param[in] node2 Second node to compare.
1271 * @return LY_SUCCESS if equal.
1272 * @return LY_ENOT if not equal.
1273 * @return LY_ERR on error.
1274 */
1275static LY_ERR
1276lyd_compare_single_value(const struct lyd_node *node1, const struct lyd_node *node2)
1277{
1278 const struct lyd_node_opaq *opaq1 = NULL, *opaq2 = NULL;
1279 const char *val1, *val2, *col;
1280 const struct lys_module *mod;
1281 char *val_dyn = NULL;
1282 LY_ERR rc = LY_SUCCESS;
1283
1284 if (!node1->schema) {
1285 opaq1 = (struct lyd_node_opaq *)node1;
1286 }
1287 if (!node2->schema) {
1288 opaq2 = (struct lyd_node_opaq *)node2;
1289 }
1290
1291 if (opaq1 && opaq2 && (opaq1->format == LY_VALUE_XML) && (opaq2->format == LY_VALUE_XML)) {
1292 /* opaque XML and opaque XML node */
1293 if (lyxml_value_compare(LYD_CTX(node1), opaq1->value, opaq1->val_prefix_data, LYD_CTX(node2), opaq2->value,
1294 opaq2->val_prefix_data)) {
1295 return LY_ENOT;
1296 }
1297 return LY_SUCCESS;
1298 }
1299
1300 /* get their values */
1301 if (opaq1 && ((opaq1->format == LY_VALUE_XML) || (opaq1->format == LY_VALUE_STR_NS)) && (col = strchr(opaq1->value, ':'))) {
1302 /* XML value with a prefix, try to transform it into a JSON (canonical) value */
1303 mod = ly_resolve_prefix(LYD_CTX(node1), opaq1->value, col - opaq1->value, opaq1->format, opaq1->val_prefix_data);
1304 if (!mod) {
1305 /* unable to compare */
1306 return LY_ENOT;
1307 }
1308
1309 if (asprintf(&val_dyn, "%s%s", mod->name, col) == -1) {
1310 LOGMEM(LYD_CTX(node1));
1311 return LY_EMEM;
1312 }
1313 val1 = val_dyn;
1314 } else {
1315 val1 = lyd_get_value(node1);
1316 }
1317 if (opaq2 && ((opaq2->format == LY_VALUE_XML) || (opaq2->format == LY_VALUE_STR_NS)) && (col = strchr(opaq2->value, ':'))) {
1318 mod = ly_resolve_prefix(LYD_CTX(node2), opaq2->value, col - opaq2->value, opaq2->format, opaq2->val_prefix_data);
1319 if (!mod) {
1320 return LY_ENOT;
1321 }
1322
1323 assert(!val_dyn);
1324 if (asprintf(&val_dyn, "%s%s", mod->name, col) == -1) {
1325 LOGMEM(LYD_CTX(node2));
1326 return LY_EMEM;
1327 }
1328 val2 = val_dyn;
1329 } else {
1330 val2 = lyd_get_value(node2);
1331 }
1332
1333 /* compare values */
1334 if (strcmp(val1, val2)) {
1335 rc = LY_ENOT;
1336 }
1337
1338 free(val_dyn);
1339 return rc;
1340}
1341
1342/**
Michal Vaskof277d362023-04-24 09:08:31 +02001343 * @brief Compare 2 data nodes if they are equivalent regarding the schema tree.
1344 *
1345 * Works correctly even if @p node1 and @p node2 have different contexts.
1346 *
1347 * @param[in] node1 The first node to compare.
1348 * @param[in] node2 The second node to compare.
1349 * @param[in] options Various @ref datacompareoptions.
1350 * @param[in] parental_schemas_checked Flag set if parent schemas were checked for match.
1351 * @return LY_SUCCESS if the nodes are equivalent.
1352 * @return LY_ENOT if the nodes are not equivalent.
aPiecek2f63f952021-03-30 12:22:18 +02001353 */
1354static LY_ERR
Michal Vaskof277d362023-04-24 09:08:31 +02001355lyd_compare_single_schema(const struct lyd_node *node1, const struct lyd_node *node2, uint32_t options,
Michal Vaskodf8ebf62022-11-10 10:33:28 +01001356 ly_bool parental_schemas_checked)
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001357{
aPiecek2f63f952021-03-30 12:22:18 +02001358 if (LYD_CTX(node1) == LYD_CTX(node2)) {
1359 /* same contexts */
Michal Vaskodf8ebf62022-11-10 10:33:28 +01001360 if (options & LYD_COMPARE_OPAQ) {
1361 if (lyd_node_schema(node1) != lyd_node_schema(node2)) {
1362 return LY_ENOT;
1363 }
1364 } else {
1365 if (node1->schema != node2->schema) {
1366 return LY_ENOT;
1367 }
aPiecek2f63f952021-03-30 12:22:18 +02001368 }
1369 } else {
1370 /* different contexts */
1371 if (!lyd_compare_schema_equal(node1->schema, node2->schema)) {
1372 return LY_ENOT;
1373 }
1374 if (!parental_schemas_checked) {
1375 if (!lyd_compare_schema_parents_equal(node1, node2)) {
1376 return LY_ENOT;
1377 }
1378 parental_schemas_checked = 1;
1379 }
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001380 }
1381
Michal Vaskof277d362023-04-24 09:08:31 +02001382 return LY_SUCCESS;
1383}
1384
1385/**
1386 * @brief Compare 2 data nodes if they are equivalent regarding the data they contain.
1387 *
1388 * Works correctly even if @p node1 and @p node2 have different contexts.
1389 *
1390 * @param[in] node1 The first node to compare.
1391 * @param[in] node2 The second node to compare.
1392 * @param[in] options Various @ref datacompareoptions.
1393 * @return LY_SUCCESS if the nodes are equivalent.
1394 * @return LY_ENOT if the nodes are not equivalent.
1395 */
1396static LY_ERR
1397lyd_compare_single_data(const struct lyd_node *node1, const struct lyd_node *node2, uint32_t options)
1398{
1399 const struct lyd_node *iter1, *iter2;
1400 struct lyd_node_any *any1, *any2;
1401 int len1, len2;
1402 LY_ERR r;
1403
Michal Vaskodf8ebf62022-11-10 10:33:28 +01001404 if (!(options & LYD_COMPARE_OPAQ) && (node1->hash != node2->hash)) {
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001405 return LY_ENOT;
1406 }
aPiecek2f63f952021-03-30 12:22:18 +02001407 /* 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 +02001408
Michal Vaskodf8ebf62022-11-10 10:33:28 +01001409 if (!node1->schema || !node2->schema) {
1410 if (!(options & LYD_COMPARE_OPAQ) && ((node1->schema && !node2->schema) || (!node1->schema && node2->schema))) {
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001411 return LY_ENOT;
1412 }
Michal Vasko7b0500d2023-03-20 15:22:46 +01001413 if ((!node1->schema && !node2->schema) || (node1->schema && (node1->schema->nodetype & LYD_NODE_TERM)) ||
1414 (node2->schema && (node2->schema->nodetype & LYD_NODE_TERM))) {
1415 /* compare values only if there are any to compare */
1416 if ((r = lyd_compare_single_value(node1, node2))) {
1417 return r;
1418 }
Michal Vasko52927e22020-03-16 17:26:14 +01001419 }
Michal Vaskodf8ebf62022-11-10 10:33:28 +01001420
Michal Vasko52927e22020-03-16 17:26:14 +01001421 if (options & LYD_COMPARE_FULL_RECURSION) {
Michal Vaskof277d362023-04-24 09:08:31 +02001422 return lyd_compare_siblings_(lyd_child(node1), lyd_child(node2), options, 1);
Michal Vasko52927e22020-03-16 17:26:14 +01001423 }
1424 return LY_SUCCESS;
1425 } else {
1426 switch (node1->schema->nodetype) {
1427 case LYS_LEAF:
1428 case LYS_LEAFLIST:
1429 if (options & LYD_COMPARE_DEFAULTS) {
1430 if ((node1->flags & LYD_DEFAULT) != (node2->flags & LYD_DEFAULT)) {
1431 return LY_ENOT;
1432 }
1433 }
Michal Vaskodf8ebf62022-11-10 10:33:28 +01001434 if ((r = lyd_compare_single_value(node1, node2))) {
1435 return r;
aPiecek2f63f952021-03-30 12:22:18 +02001436 }
1437
aPiecek2f63f952021-03-30 12:22:18 +02001438 return LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +01001439 case LYS_CONTAINER:
Michal Vaskoc8187dc2022-05-13 12:26:40 +02001440 case LYS_RPC:
1441 case LYS_ACTION:
1442 case LYS_NOTIF:
Michal Vaskoa38adc72023-04-25 10:14:28 +02001443 /* implicit container is always equal to a container with non-default descendants */
Michal Vasko52927e22020-03-16 17:26:14 +01001444 if (options & LYD_COMPARE_FULL_RECURSION) {
Michal Vaskof277d362023-04-24 09:08:31 +02001445 return lyd_compare_siblings_(lyd_child(node1), lyd_child(node2), options, 1);
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001446 }
1447 return LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +01001448 case LYS_LIST:
Michal Vasko9e685082021-01-29 14:49:09 +01001449 iter1 = lyd_child(node1);
1450 iter2 = lyd_child(node2);
Michal Vasko52927e22020-03-16 17:26:14 +01001451
Michal Vaskoee9b9482023-06-19 13:17:48 +02001452 if (options & LYD_COMPARE_FULL_RECURSION) {
Michal Vaskof277d362023-04-24 09:08:31 +02001453 return lyd_compare_siblings_(iter1, iter2, options, 1);
Michal Vaskoee9b9482023-06-19 13:17:48 +02001454 } else if (node1->schema->flags & LYS_KEYLESS) {
1455 /* always equal */
1456 return LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +01001457 }
Michal Vaskoee9b9482023-06-19 13:17:48 +02001458
1459 /* lists with keys, their equivalence is based on their keys */
1460 for (const struct lysc_node *key = lysc_node_child(node1->schema);
1461 key && (key->flags & LYS_KEY);
1462 key = key->next) {
1463 if (!iter1 || !iter2) {
1464 return (iter1 == iter2) ? LY_SUCCESS : LY_ENOT;
1465 }
1466 r = lyd_compare_single_schema(iter1, iter2, options, 1);
1467 LY_CHECK_RET(r);
1468 r = lyd_compare_single_data(iter1, iter2, options);
1469 LY_CHECK_RET(r);
1470
1471 iter1 = iter1->next;
1472 iter2 = iter2->next;
1473 }
1474
1475 return LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +01001476 case LYS_ANYXML:
1477 case LYS_ANYDATA:
Michal Vasko22df3f02020-08-24 13:29:22 +02001478 any1 = (struct lyd_node_any *)node1;
1479 any2 = (struct lyd_node_any *)node2;
Michal Vasko52927e22020-03-16 17:26:14 +01001480
1481 if (any1->value_type != any2->value_type) {
1482 return LY_ENOT;
1483 }
1484 switch (any1->value_type) {
1485 case LYD_ANYDATA_DATATREE:
Michal Vaskof277d362023-04-24 09:08:31 +02001486 return lyd_compare_siblings_(any1->value.tree, any2->value.tree, options, 1);
Michal Vasko52927e22020-03-16 17:26:14 +01001487 case LYD_ANYDATA_STRING:
1488 case LYD_ANYDATA_XML:
1489 case LYD_ANYDATA_JSON:
Michal Vasko3b4ec412022-09-22 15:24:14 +02001490 if ((!any1->value.str && any2->value.str) || (any1->value.str && !any2->value.str)) {
1491 return LY_ENOT;
1492 } else if (!any1->value.str && !any2->value.str) {
1493 return LY_SUCCESS;
1494 }
Michal Vasko52927e22020-03-16 17:26:14 +01001495 len1 = strlen(any1->value.str);
1496 len2 = strlen(any2->value.str);
Michal Vasko69730152020-10-09 16:30:07 +02001497 if ((len1 != len2) || strcmp(any1->value.str, any2->value.str)) {
Michal Vasko52927e22020-03-16 17:26:14 +01001498 return LY_ENOT;
1499 }
1500 return LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +01001501 case LYD_ANYDATA_LYB:
Michal Vasko60ea6352020-06-29 13:39:39 +02001502 len1 = lyd_lyb_data_length(any1->value.mem);
1503 len2 = lyd_lyb_data_length(any2->value.mem);
Michal Vasko2b97d472022-08-17 09:29:03 +02001504 if ((len1 == -1) || (len2 == -1) || (len1 != len2) || memcmp(any1->value.mem, any2->value.mem, len1)) {
Michal Vasko52927e22020-03-16 17:26:14 +01001505 return LY_ENOT;
1506 }
1507 return LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +01001508 }
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001509 }
1510 }
1511
Michal Vaskob7be7a82020-08-20 09:09:04 +02001512 LOGINT(LYD_CTX(node1));
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001513 return LY_EINT;
1514}
Radek Krejci22ebdba2019-07-25 13:59:43 +02001515
Michal Vaskof277d362023-04-24 09:08:31 +02001516/**
1517 * @brief Compare all siblings at a node level.
1518 *
1519 * @param[in] node1 First sibling list.
1520 * @param[in] node2 Second sibling list.
1521 * @param[in] options Various @ref datacompareoptions.
1522 * @param[in] parental_schemas_checked Flag set if parent schemas were checked for match.
1523 * @return LY_SUCCESS if equal.
1524 * @return LY_ENOT if not equal.
1525 * @return LY_ERR on error.
1526 */
1527static LY_ERR
1528lyd_compare_siblings_(const struct lyd_node *node1, const struct lyd_node *node2, uint32_t options,
1529 ly_bool parental_schemas_checked)
1530{
1531 LY_ERR r;
1532 const struct lyd_node *iter2;
1533
1534 while (node1 && node2) {
1535 /* schema match */
1536 r = lyd_compare_single_schema(node1, node2, options, parental_schemas_checked);
1537 LY_CHECK_RET(r);
1538
1539 if (node1->schema && (((node1->schema->nodetype == LYS_LIST) && !(node1->schema->flags & LYS_KEYLESS)) ||
1540 ((node1->schema->nodetype == LYS_LEAFLIST) && (node1->schema->flags & LYS_CONFIG_W))) &&
1541 (node1->schema->flags & LYS_ORDBY_SYSTEM)) {
1542 /* find a matching instance in case they are ordered differently */
1543 r = lyd_find_sibling_first(node2, node1, (struct lyd_node **)&iter2);
1544 if (r == LY_ENOTFOUND) {
1545 /* no matching instance, data not equal */
1546 r = LY_ENOT;
1547 }
1548 LY_CHECK_RET(r);
1549 } else {
1550 /* compare with the current node */
1551 iter2 = node2;
1552 }
1553
1554 /* data match */
1555 r = lyd_compare_single_data(node1, iter2, options | LYD_COMPARE_FULL_RECURSION);
1556 LY_CHECK_RET(r);
1557
1558 node1 = node1->next;
1559 node2 = node2->next;
1560 }
1561
1562 return (node1 || node2) ? LY_ENOT : LY_SUCCESS;
1563}
1564
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001565LIBYANG_API_DEF LY_ERR
aPiecek2f63f952021-03-30 12:22:18 +02001566lyd_compare_single(const struct lyd_node *node1, const struct lyd_node *node2, uint32_t options)
1567{
Michal Vaskof277d362023-04-24 09:08:31 +02001568 LY_ERR r;
1569
1570 if (!node1 || !node2) {
1571 return (node1 == node2) ? LY_SUCCESS : LY_ENOT;
1572 }
1573
1574 /* schema match */
1575 if ((r = lyd_compare_single_schema(node1, node2, options, 0))) {
1576 return r;
1577 }
1578
1579 /* data match */
1580 return lyd_compare_single_data(node1, node2, options);
aPiecek2f63f952021-03-30 12:22:18 +02001581}
1582
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001583LIBYANG_API_DEF LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02001584lyd_compare_siblings(const struct lyd_node *node1, const struct lyd_node *node2, uint32_t options)
Michal Vasko8f359bf2020-07-28 10:41:15 +02001585{
Michal Vaskof277d362023-04-24 09:08:31 +02001586 return lyd_compare_siblings_(node1, node2, options, 0);
Michal Vasko8f359bf2020-07-28 10:41:15 +02001587}
1588
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001589LIBYANG_API_DEF LY_ERR
Michal Vasko21725742020-06-29 11:49:25 +02001590lyd_compare_meta(const struct lyd_meta *meta1, const struct lyd_meta *meta2)
1591{
aPiecek0a6705b2023-11-14 14:20:58 +01001592 const struct ly_ctx *ctx;
1593
Michal Vasko21725742020-06-29 11:49:25 +02001594 if (!meta1 || !meta2) {
1595 if (meta1 == meta2) {
1596 return LY_SUCCESS;
1597 } else {
1598 return LY_ENOT;
1599 }
1600 }
1601
aPiecek0a6705b2023-11-14 14:20:58 +01001602 ctx = meta1->annotation->module->ctx;
1603 if ((ctx != meta2->annotation->module->ctx) || (meta1->annotation != meta2->annotation)) {
Michal Vasko21725742020-06-29 11:49:25 +02001604 return LY_ENOT;
1605 }
1606
aPiecek0a6705b2023-11-14 14:20:58 +01001607 return meta1->value.realtype->plugin->compare(ctx, &meta1->value, &meta2->value);
Michal Vasko21725742020-06-29 11:49:25 +02001608}
1609
Radek Krejci22ebdba2019-07-25 13:59:43 +02001610/**
Michal Vasko9cf62422021-07-01 13:29:32 +02001611 * @brief Create a copy of the attribute.
1612 *
1613 * @param[in] attr Attribute to copy.
1614 * @param[in] node Opaque where to append the new attribute.
1615 * @param[out] dup Optional created attribute copy.
1616 * @return LY_ERR value.
1617 */
1618static LY_ERR
1619lyd_dup_attr_single(const struct lyd_attr *attr, struct lyd_node *node, struct lyd_attr **dup)
1620{
1621 LY_ERR ret = LY_SUCCESS;
1622 struct lyd_attr *a, *last;
1623 struct lyd_node_opaq *opaq = (struct lyd_node_opaq *)node;
1624
1625 LY_CHECK_ARG_RET(NULL, attr, node, !node->schema, LY_EINVAL);
1626
1627 /* create a copy */
1628 a = calloc(1, sizeof *attr);
1629 LY_CHECK_ERR_RET(!a, LOGMEM(LYD_CTX(node)), LY_EMEM);
1630
1631 LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), attr->name.name, 0, &a->name.name), finish);
1632 LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), attr->name.prefix, 0, &a->name.prefix), finish);
1633 LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), attr->name.module_ns, 0, &a->name.module_ns), finish);
1634 LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), attr->value, 0, &a->value), finish);
1635 a->hints = attr->hints;
1636 a->format = attr->format;
1637 if (attr->val_prefix_data) {
1638 ret = ly_dup_prefix_data(LYD_CTX(node), attr->format, attr->val_prefix_data, &a->val_prefix_data);
1639 LY_CHECK_GOTO(ret, finish);
1640 }
1641
1642 /* insert as the last attribute */
1643 a->parent = opaq;
1644 if (opaq->attr) {
1645 for (last = opaq->attr; last->next; last = last->next) {}
1646 last->next = a;
1647 } else {
1648 opaq->attr = a;
1649 }
1650
1651finish:
1652 if (ret) {
1653 lyd_free_attr_single(LYD_CTX(node), a);
1654 } else if (dup) {
1655 *dup = a;
1656 }
1657 return LY_SUCCESS;
1658}
1659
1660/**
Michal Vaskoddd76592022-01-17 13:34:48 +01001661 * @brief Find @p schema equivalent in @p trg_ctx.
1662 *
1663 * @param[in] schema Schema node to find.
1664 * @param[in] trg_ctx Target context to search in.
1665 * @param[in] parent Data parent of @p schema, if any.
Michal Vaskoaf3df492022-12-02 14:03:52 +01001666 * @param[in] log Whether to log directly.
Michal Vaskoddd76592022-01-17 13:34:48 +01001667 * @param[out] trg_schema Found schema from @p trg_ctx to use.
1668 * @return LY_RRR value.
1669 */
1670static LY_ERR
Michal Vaskoaf3df492022-12-02 14:03:52 +01001671lyd_find_schema_ctx(const struct lysc_node *schema, const struct ly_ctx *trg_ctx, const struct lyd_node *parent,
1672 ly_bool log, const struct lysc_node **trg_schema)
Michal Vaskoddd76592022-01-17 13:34:48 +01001673{
Michal Vasko9beceb82022-04-05 12:14:15 +02001674 const struct lysc_node *src_parent = NULL, *trg_parent = NULL, *sp, *tp;
1675 const struct lys_module *trg_mod = NULL;
Michal Vaskoddd76592022-01-17 13:34:48 +01001676 char *path;
1677
1678 if (!schema) {
1679 /* opaque node */
1680 *trg_schema = NULL;
1681 return LY_SUCCESS;
1682 }
1683
Michal Vasko9beceb82022-04-05 12:14:15 +02001684 if (lysc_data_parent(schema) && parent && parent->schema) {
Michal Vaskoddd76592022-01-17 13:34:48 +01001685 /* start from schema parent */
Michal Vasko9beceb82022-04-05 12:14:15 +02001686 trg_parent = parent->schema;
1687 src_parent = lysc_data_parent(schema);
1688 }
1689
1690 do {
1691 /* find the next parent */
1692 sp = schema;
Michal Vaskob6808202022-12-02 14:04:23 +01001693 while (lysc_data_parent(sp) != src_parent) {
1694 sp = lysc_data_parent(sp);
Michal Vasko9beceb82022-04-05 12:14:15 +02001695 }
1696 src_parent = sp;
1697
1698 if (!src_parent->parent) {
1699 /* find the module first */
1700 trg_mod = ly_ctx_get_module_implemented(trg_ctx, src_parent->module->name);
1701 if (!trg_mod) {
Michal Vaskoaf3df492022-12-02 14:03:52 +01001702 if (log) {
1703 LOGERR(trg_ctx, LY_ENOTFOUND, "Module \"%s\" not present/implemented in the target context.",
1704 src_parent->module->name);
1705 }
Michal Vasko9beceb82022-04-05 12:14:15 +02001706 return LY_ENOTFOUND;
1707 }
1708 }
1709
1710 /* find the next parent */
1711 assert(trg_parent || trg_mod);
1712 tp = NULL;
1713 while ((tp = lys_getnext(tp, trg_parent, trg_mod ? trg_mod->compiled : NULL, 0))) {
1714 if (!strcmp(tp->name, src_parent->name) && !strcmp(tp->module->name, src_parent->module->name)) {
1715 break;
1716 }
1717 }
1718 if (!tp) {
1719 /* schema node not found */
Michal Vaskoaf3df492022-12-02 14:03:52 +01001720 if (log) {
1721 path = lysc_path(src_parent, LYSC_PATH_LOG, NULL, 0);
1722 LOGERR(trg_ctx, LY_ENOTFOUND, "Schema node \"%s\" not found in the target context.", path);
1723 free(path);
1724 }
Michal Vaskoddd76592022-01-17 13:34:48 +01001725 return LY_ENOTFOUND;
1726 }
Michal Vaskoddd76592022-01-17 13:34:48 +01001727
Michal Vasko9beceb82022-04-05 12:14:15 +02001728 trg_parent = tp;
1729 } while (schema != src_parent);
Michal Vaskoddd76592022-01-17 13:34:48 +01001730
Michal Vasko9beceb82022-04-05 12:14:15 +02001731 /* success */
1732 *trg_schema = trg_parent;
1733 return LY_SUCCESS;
Michal Vaskoddd76592022-01-17 13:34:48 +01001734}
1735
1736/**
Michal Vasko52927e22020-03-16 17:26:14 +01001737 * @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 +02001738 *
aPiecek55653c92023-11-09 13:43:19 +01001739 * Ignores ::LYD_DUP_WITH_PARENTS which is supposed to be handled by lyd_dup().
Radek Krejcif8b95172020-05-15 14:51:06 +02001740 *
Michal Vaskoddd76592022-01-17 13:34:48 +01001741 * @param[in] node Node to duplicate.
1742 * @param[in] trg_ctx Target context for duplicated nodes.
Radek Krejcif8b95172020-05-15 14:51:06 +02001743 * @param[in] parent Parent to insert into, NULL for top-level sibling.
Michal Vasko67177e52021-08-25 11:15:15 +02001744 * @param[in] insert_last Whether the duplicated node can be inserted as the last child of @p parent. Set for
1745 * recursive duplication as an optimization.
Radek Krejcif8b95172020-05-15 14:51:06 +02001746 * @param[in,out] first First sibling, NULL if no top-level sibling exist yet. Can be also NULL if @p parent is set.
1747 * @param[in] options Bitmask of options flags, see @ref dupoptions.
Michal Vaskoddd76592022-01-17 13:34:48 +01001748 * @param[out] dup_p Pointer where the created duplicated node is placed (besides connecting it to @p parent / @p first).
1749 * @return LY_ERR value.
Radek Krejci22ebdba2019-07-25 13:59:43 +02001750 */
Michal Vasko52927e22020-03-16 17:26:14 +01001751static LY_ERR
Michal Vaskoddd76592022-01-17 13:34:48 +01001752lyd_dup_r(const struct lyd_node *node, const struct ly_ctx *trg_ctx, struct lyd_node *parent, ly_bool insert_last,
1753 struct lyd_node **first, uint32_t options, struct lyd_node **dup_p)
Radek Krejci22ebdba2019-07-25 13:59:43 +02001754{
Michal Vasko52927e22020-03-16 17:26:14 +01001755 LY_ERR ret;
Radek Krejci22ebdba2019-07-25 13:59:43 +02001756 struct lyd_node *dup = NULL;
Michal Vasko61551fa2020-07-09 15:45:45 +02001757 struct lyd_meta *meta;
Michal Vasko9cf62422021-07-01 13:29:32 +02001758 struct lyd_attr *attr;
Michal Vasko61551fa2020-07-09 15:45:45 +02001759 struct lyd_node_any *any;
Michal Vaskoddd76592022-01-17 13:34:48 +01001760 const struct lysc_type *type;
1761 const char *val_can;
Radek Krejci22ebdba2019-07-25 13:59:43 +02001762
Michal Vasko52927e22020-03-16 17:26:14 +01001763 LY_CHECK_ARG_RET(NULL, node, LY_EINVAL);
Radek Krejci22ebdba2019-07-25 13:59:43 +02001764
Michal Vasko19175b62022-04-01 09:17:07 +02001765 if (node->flags & LYD_EXT) {
Michal Vasko53ac4dd2022-06-07 10:56:08 +02001766 if (options & LYD_DUP_NO_EXT) {
1767 /* no not duplicate this subtree */
1768 return LY_SUCCESS;
1769 }
1770
Michal Vasko19175b62022-04-01 09:17:07 +02001771 /* we need to use the same context */
1772 trg_ctx = LYD_CTX(node);
1773 }
1774
Michal Vasko52927e22020-03-16 17:26:14 +01001775 if (!node->schema) {
1776 dup = calloc(1, sizeof(struct lyd_node_opaq));
Michal Vaskoddd76592022-01-17 13:34:48 +01001777 ((struct lyd_node_opaq *)dup)->ctx = trg_ctx;
Michal Vasko52927e22020-03-16 17:26:14 +01001778 } else {
1779 switch (node->schema->nodetype) {
Michal Vasko1bf09392020-03-27 12:38:10 +01001780 case LYS_RPC:
Michal Vasko52927e22020-03-16 17:26:14 +01001781 case LYS_ACTION:
1782 case LYS_NOTIF:
1783 case LYS_CONTAINER:
1784 case LYS_LIST:
1785 dup = calloc(1, sizeof(struct lyd_node_inner));
1786 break;
1787 case LYS_LEAF:
1788 case LYS_LEAFLIST:
1789 dup = calloc(1, sizeof(struct lyd_node_term));
1790 break;
1791 case LYS_ANYDATA:
1792 case LYS_ANYXML:
1793 dup = calloc(1, sizeof(struct lyd_node_any));
1794 break;
1795 default:
Michal Vaskoddd76592022-01-17 13:34:48 +01001796 LOGINT(trg_ctx);
Michal Vasko52927e22020-03-16 17:26:14 +01001797 ret = LY_EINT;
1798 goto error;
1799 }
Radek Krejci22ebdba2019-07-25 13:59:43 +02001800 }
Michal Vaskoddd76592022-01-17 13:34:48 +01001801 LY_CHECK_ERR_GOTO(!dup, LOGMEM(trg_ctx); ret = LY_EMEM, error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02001802
Michal Vaskof6df0a02020-06-16 13:08:34 +02001803 if (options & LYD_DUP_WITH_FLAGS) {
1804 dup->flags = node->flags;
1805 } else {
Michal Vasko19175b62022-04-01 09:17:07 +02001806 dup->flags = (node->flags & (LYD_DEFAULT | LYD_EXT)) | LYD_NEW;
Michal Vaskof6df0a02020-06-16 13:08:34 +02001807 }
Igor Ryzhov9e7af662023-10-31 13:27:56 +02001808 if (options & LYD_DUP_WITH_PRIV) {
1809 dup->priv = node->priv;
1810 }
Michal Vaskoddd76592022-01-17 13:34:48 +01001811 if (trg_ctx == LYD_CTX(node)) {
1812 dup->schema = node->schema;
1813 } else {
Michal Vaskoaf3df492022-12-02 14:03:52 +01001814 ret = lyd_find_schema_ctx(node->schema, trg_ctx, parent, 1, &dup->schema);
Michal Vasko27f536f2022-04-01 09:17:30 +02001815 if (ret) {
1816 /* has no schema but is not an opaque node */
1817 free(dup);
1818 dup = NULL;
1819 goto error;
1820 }
Michal Vaskoddd76592022-01-17 13:34:48 +01001821 }
Michal Vasko52927e22020-03-16 17:26:14 +01001822 dup->prev = dup;
Radek Krejci22ebdba2019-07-25 13:59:43 +02001823
Michal Vasko9cf62422021-07-01 13:29:32 +02001824 /* duplicate metadata/attributes */
Michal Vasko25a32822020-07-09 15:48:22 +02001825 if (!(options & LYD_DUP_NO_META)) {
Michal Vasko9cf62422021-07-01 13:29:32 +02001826 if (!node->schema) {
1827 LY_LIST_FOR(((struct lyd_node_opaq *)node)->attr, attr) {
1828 LY_CHECK_GOTO(ret = lyd_dup_attr_single(attr, dup, NULL), error);
1829 }
1830 } else {
1831 LY_LIST_FOR(node->meta, meta) {
aPiecek41680342023-11-08 10:19:44 +01001832 LY_CHECK_GOTO(ret = lyd_dup_meta_single_to_ctx(trg_ctx, meta, dup, NULL), error);
Michal Vasko9cf62422021-07-01 13:29:32 +02001833 }
Michal Vasko25a32822020-07-09 15:48:22 +02001834 }
1835 }
Radek Krejci22ebdba2019-07-25 13:59:43 +02001836
1837 /* nodetype-specific work */
Michal Vasko52927e22020-03-16 17:26:14 +01001838 if (!dup->schema) {
1839 struct lyd_node_opaq *opaq = (struct lyd_node_opaq *)dup;
1840 struct lyd_node_opaq *orig = (struct lyd_node_opaq *)node;
1841 struct lyd_node *child;
Radek Krejci22ebdba2019-07-25 13:59:43 +02001842
1843 if (options & LYD_DUP_RECURSIVE) {
1844 /* duplicate all the children */
1845 LY_LIST_FOR(orig->child, child) {
Michal Vaskoddd76592022-01-17 13:34:48 +01001846 LY_CHECK_GOTO(ret = lyd_dup_r(child, trg_ctx, dup, 1, NULL, options, NULL), error);
Michal Vasko52927e22020-03-16 17:26:14 +01001847 }
1848 }
Michal Vaskoddd76592022-01-17 13:34:48 +01001849 LY_CHECK_GOTO(ret = lydict_insert(trg_ctx, orig->name.name, 0, &opaq->name.name), error);
1850 LY_CHECK_GOTO(ret = lydict_insert(trg_ctx, orig->name.prefix, 0, &opaq->name.prefix), error);
1851 LY_CHECK_GOTO(ret = lydict_insert(trg_ctx, orig->name.module_ns, 0, &opaq->name.module_ns), error);
1852 LY_CHECK_GOTO(ret = lydict_insert(trg_ctx, orig->value, 0, &opaq->value), error);
Michal Vasko9cf62422021-07-01 13:29:32 +02001853 opaq->hints = orig->hints;
1854 opaq->format = orig->format;
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01001855 if (orig->val_prefix_data) {
Michal Vaskoddd76592022-01-17 13:34:48 +01001856 ret = ly_dup_prefix_data(trg_ctx, opaq->format, orig->val_prefix_data, &opaq->val_prefix_data);
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01001857 LY_CHECK_GOTO(ret, error);
Michal Vasko52927e22020-03-16 17:26:14 +01001858 }
Michal Vasko52927e22020-03-16 17:26:14 +01001859 } else if (dup->schema->nodetype & LYD_NODE_TERM) {
1860 struct lyd_node_term *term = (struct lyd_node_term *)dup;
1861 struct lyd_node_term *orig = (struct lyd_node_term *)node;
1862
1863 term->hash = orig->hash;
Michal Vaskoddd76592022-01-17 13:34:48 +01001864 if (trg_ctx == LYD_CTX(node)) {
1865 ret = orig->value.realtype->plugin->duplicate(trg_ctx, &orig->value, &term->value);
1866 LY_CHECK_ERR_GOTO(ret, LOGERR(trg_ctx, ret, "Value duplication failed."), error);
1867 } else {
1868 /* store canonical value in the target context */
1869 val_can = lyd_get_value(node);
1870 type = ((struct lysc_node_leaf *)term->schema)->type;
Michal Vasko989cdb42023-10-06 15:32:37 +02001871 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 +01001872 LYD_HINT_DATA, term->schema, NULL);
1873 LY_CHECK_GOTO(ret, error);
1874 }
Michal Vasko52927e22020-03-16 17:26:14 +01001875 } else if (dup->schema->nodetype & LYD_NODE_INNER) {
1876 struct lyd_node_inner *orig = (struct lyd_node_inner *)node;
1877 struct lyd_node *child;
1878
1879 if (options & LYD_DUP_RECURSIVE) {
1880 /* duplicate all the children */
1881 LY_LIST_FOR(orig->child, child) {
Michal Vaskoddd76592022-01-17 13:34:48 +01001882 LY_CHECK_GOTO(ret = lyd_dup_r(child, trg_ctx, dup, 1, NULL, options, NULL), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02001883 }
Michal Vasko69730152020-10-09 16:30:07 +02001884 } else if ((dup->schema->nodetype == LYS_LIST) && !(dup->schema->flags & LYS_KEYLESS)) {
Radek Krejci22ebdba2019-07-25 13:59:43 +02001885 /* always duplicate keys of a list */
Michal Vasko9beceb82022-04-05 12:14:15 +02001886 for (child = orig->child; child && lysc_is_key(child->schema); child = child->next) {
Michal Vaskoddd76592022-01-17 13:34:48 +01001887 LY_CHECK_GOTO(ret = lyd_dup_r(child, trg_ctx, dup, 1, NULL, options, NULL), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02001888 }
1889 }
1890 lyd_hash(dup);
1891 } else if (dup->schema->nodetype & LYD_NODE_ANY) {
Michal Vasko61551fa2020-07-09 15:45:45 +02001892 dup->hash = node->hash;
1893 any = (struct lyd_node_any *)node;
1894 LY_CHECK_GOTO(ret = lyd_any_copy_value(dup, &any->value, any->value_type), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02001895 }
1896
Michal Vasko52927e22020-03-16 17:26:14 +01001897 /* insert */
Michal Vasko67177e52021-08-25 11:15:15 +02001898 lyd_insert_node(parent, first, dup, insert_last);
Michal Vasko52927e22020-03-16 17:26:14 +01001899
1900 if (dup_p) {
1901 *dup_p = dup;
1902 }
1903 return LY_SUCCESS;
Radek Krejci22ebdba2019-07-25 13:59:43 +02001904
1905error:
Michal Vasko52927e22020-03-16 17:26:14 +01001906 lyd_free_tree(dup);
1907 return ret;
Radek Krejci22ebdba2019-07-25 13:59:43 +02001908}
1909
Michal Vasko29d674b2021-08-25 11:18:35 +02001910/**
1911 * @brief Get a parent node to connect duplicated subtree to.
1912 *
1913 * @param[in] node Node (subtree) to duplicate.
Michal Vaskoddd76592022-01-17 13:34:48 +01001914 * @param[in] trg_ctx Target context for duplicated nodes.
Michal Vasko29d674b2021-08-25 11:18:35 +02001915 * @param[in] parent Initial parent to connect to.
1916 * @param[in] options Bitmask of options flags, see @ref dupoptions.
1917 * @param[out] dup_parent First duplicated parent node, if any.
1918 * @param[out] local_parent Correct parent to directly connect duplicated @p node to.
1919 * @return LY_ERR value.
1920 */
Michal Vasko3a41dff2020-07-15 14:30:28 +02001921static LY_ERR
Michal Vasko58d89e92023-05-23 09:56:19 +02001922lyd_dup_get_local_parent(const struct lyd_node *node, const struct ly_ctx *trg_ctx, struct lyd_node *parent,
1923 uint32_t options, struct lyd_node **dup_parent, struct lyd_node **local_parent)
Radek Krejci22ebdba2019-07-25 13:59:43 +02001924{
Michal Vasko58d89e92023-05-23 09:56:19 +02001925 const struct lyd_node *orig_parent;
Michal Vaskoeaef7c92023-10-17 10:06:15 +02001926 struct lyd_node *iter = NULL;
Michal Vasko83522192022-07-20 08:07:34 +02001927 ly_bool repeat = 1, ext_parent = 0;
Michal Vasko3a41dff2020-07-15 14:30:28 +02001928
1929 *dup_parent = NULL;
1930 *local_parent = NULL;
1931
Michal Vasko83522192022-07-20 08:07:34 +02001932 if (node->flags & LYD_EXT) {
1933 ext_parent = 1;
1934 }
Michal Vasko58d89e92023-05-23 09:56:19 +02001935 for (orig_parent = lyd_parent(node); repeat && orig_parent; orig_parent = lyd_parent(orig_parent)) {
Michal Vasko83522192022-07-20 08:07:34 +02001936 if (ext_parent) {
1937 /* use the standard context */
1938 trg_ctx = LYD_CTX(orig_parent);
1939 }
Michal Vasko13c5b212023-02-14 10:03:01 +01001940 if (parent && (LYD_CTX(parent) == LYD_CTX(orig_parent)) && (parent->schema == orig_parent->schema)) {
Michal Vasko3a41dff2020-07-15 14:30:28 +02001941 /* stop creating parents, connect what we have into the provided parent */
1942 iter = parent;
1943 repeat = 0;
Michal Vasko13c5b212023-02-14 10:03:01 +01001944 } else if (parent && (LYD_CTX(parent) != LYD_CTX(orig_parent)) &&
1945 lyd_compare_schema_equal(parent->schema, orig_parent->schema) &&
Michal Vasko58d89e92023-05-23 09:56:19 +02001946 lyd_compare_schema_parents_equal(parent, orig_parent)) {
Michal Vasko13c5b212023-02-14 10:03:01 +01001947 iter = parent;
1948 repeat = 0;
Michal Vasko3a41dff2020-07-15 14:30:28 +02001949 } else {
1950 iter = NULL;
Michal Vasko58d89e92023-05-23 09:56:19 +02001951 LY_CHECK_RET(lyd_dup_r(orig_parent, trg_ctx, NULL, 0, &iter, options, &iter));
Michal Vaskoeaef7c92023-10-17 10:06:15 +02001952
1953 /* insert into the previous duplicated parent */
1954 if (*dup_parent) {
1955 lyd_insert_node(iter, NULL, *dup_parent, 0);
1956 }
1957
1958 /* update the last duplicated parent */
1959 *dup_parent = iter;
Michal Vasko3a41dff2020-07-15 14:30:28 +02001960 }
Michal Vasko58d89e92023-05-23 09:56:19 +02001961
Michal Vaskoeaef7c92023-10-17 10:06:15 +02001962 /* set the first parent */
Michal Vasko3a41dff2020-07-15 14:30:28 +02001963 if (!*local_parent) {
Michal Vasko58d89e92023-05-23 09:56:19 +02001964 *local_parent = iter;
Michal Vasko3a41dff2020-07-15 14:30:28 +02001965 }
Michal Vasko58d89e92023-05-23 09:56:19 +02001966
Michal Vasko83522192022-07-20 08:07:34 +02001967 if (orig_parent->flags & LYD_EXT) {
1968 ext_parent = 1;
1969 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02001970 }
1971
1972 if (repeat && parent) {
1973 /* given parent and created parents chain actually do not interconnect */
Michal Vasko58d89e92023-05-23 09:56:19 +02001974 LOGERR(trg_ctx, LY_EINVAL, "None of the duplicated node \"%s\" schema parents match the provided parent \"%s\".",
1975 LYD_NAME(node), LYD_NAME(parent));
Michal Vasko3a41dff2020-07-15 14:30:28 +02001976 return LY_EINVAL;
1977 }
1978
Michal Vaskoeaef7c92023-10-17 10:06:15 +02001979 if (*dup_parent && parent) {
1980 /* last insert into a prevously-existing parent */
1981 lyd_insert_node(parent, NULL, *dup_parent, 0);
1982 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02001983 return LY_SUCCESS;
1984}
1985
1986static LY_ERR
Michal Vasko58d89e92023-05-23 09:56:19 +02001987lyd_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 +01001988 ly_bool nosiblings, struct lyd_node **dup)
Michal Vasko3a41dff2020-07-15 14:30:28 +02001989{
1990 LY_ERR rc;
Radek Krejci22ebdba2019-07-25 13:59:43 +02001991 const struct lyd_node *orig; /* original node to be duplicated */
1992 struct lyd_node *first = NULL; /* the first duplicated node, this is returned */
Radek Krejci22ebdba2019-07-25 13:59:43 +02001993 struct lyd_node *top = NULL; /* the most higher created node */
Michal Vasko58d89e92023-05-23 09:56:19 +02001994 struct lyd_node *local_parent = NULL; /* the direct parent node for the duplicated node(s) */
Radek Krejci22ebdba2019-07-25 13:59:43 +02001995
Michal Vasko1feb9bb2022-07-20 08:44:07 +02001996 assert(node && trg_ctx);
Radek Krejci22ebdba2019-07-25 13:59:43 +02001997
1998 if (options & LYD_DUP_WITH_PARENTS) {
Michal Vaskoddd76592022-01-17 13:34:48 +01001999 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 +02002000 &top, &local_parent), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002001 } else {
2002 local_parent = parent;
2003 }
2004
Radek Krejci22ebdba2019-07-25 13:59:43 +02002005 LY_LIST_FOR(node, orig) {
Michal Vasko35f4d772021-01-12 12:08:57 +01002006 if (lysc_is_key(orig->schema)) {
2007 if (local_parent) {
2008 /* the key must already exist in the parent */
Michal Vasko58d89e92023-05-23 09:56:19 +02002009 rc = lyd_find_sibling_schema(lyd_child(local_parent), orig->schema, first ? NULL : &first);
Michal Vaskoddd76592022-01-17 13:34:48 +01002010 LY_CHECK_ERR_GOTO(rc, LOGINT(trg_ctx), error);
Michal Vasko35f4d772021-01-12 12:08:57 +01002011 } else {
2012 assert(!(options & LYD_DUP_WITH_PARENTS));
2013 /* duplicating a single key, okay, I suppose... */
Michal Vaskoddd76592022-01-17 13:34:48 +01002014 rc = lyd_dup_r(orig, trg_ctx, NULL, 0, &first, options, first ? NULL : &first);
Michal Vasko35f4d772021-01-12 12:08:57 +01002015 LY_CHECK_GOTO(rc, error);
2016 }
2017 } else {
2018 /* if there is no local parent, it will be inserted into first */
Michal Vasko58d89e92023-05-23 09:56:19 +02002019 rc = lyd_dup_r(orig, trg_ctx, local_parent, 0, &first, options, first ? NULL : &first);
Michal Vasko35f4d772021-01-12 12:08:57 +01002020 LY_CHECK_GOTO(rc, error);
2021 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02002022 if (nosiblings) {
Radek Krejci22ebdba2019-07-25 13:59:43 +02002023 break;
2024 }
2025 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02002026
Michal Vasko3a41dff2020-07-15 14:30:28 +02002027 if (dup) {
2028 *dup = first;
2029 }
2030 return LY_SUCCESS;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002031
2032error:
2033 if (top) {
2034 lyd_free_tree(top);
2035 } else {
Michal Vaskof03ed032020-03-04 13:31:44 +01002036 lyd_free_siblings(first);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002037 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02002038 return rc;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002039}
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002040
Michal Vasko1feb9bb2022-07-20 08:44:07 +02002041/**
2042 * @brief Check the context of node and parent when duplicating nodes.
2043 *
2044 * @param[in] node Node to duplicate.
2045 * @param[in] parent Parent of the duplicated node(s).
2046 * @return LY_ERR value.
2047 */
2048static LY_ERR
2049lyd_dup_ctx_check(const struct lyd_node *node, const struct lyd_node_inner *parent)
2050{
2051 const struct lyd_node *iter;
2052
2053 if (!node || !parent) {
2054 return LY_SUCCESS;
2055 }
2056
2057 if ((LYD_CTX(node) != LYD_CTX(parent))) {
2058 /* try to find top-level ext data parent */
2059 for (iter = node; iter && !(iter->flags & LYD_EXT); iter = lyd_parent(iter)) {}
2060
2061 if (!iter || !lyd_parent(iter) || (LYD_CTX(lyd_parent(iter)) != LYD_CTX(parent))) {
Michal Vaskoce55b462023-02-14 10:03:32 +01002062 LOGERR(LYD_CTX(node), LY_EINVAL, "Different contexts used in node duplication.");
Michal Vasko1feb9bb2022-07-20 08:44:07 +02002063 return LY_EINVAL;
2064 }
2065 }
2066
2067 return LY_SUCCESS;
2068}
2069
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002070LIBYANG_API_DEF LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02002071lyd_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 +02002072{
Michal Vaskoddd76592022-01-17 13:34:48 +01002073 LY_CHECK_ARG_RET(NULL, node, LY_EINVAL);
Michal Vasko1feb9bb2022-07-20 08:44:07 +02002074 LY_CHECK_RET(lyd_dup_ctx_check(node, parent));
Michal Vaskoddd76592022-01-17 13:34:48 +01002075
Michal Vasko58d89e92023-05-23 09:56:19 +02002076 return lyd_dup(node, LYD_CTX(node), (struct lyd_node *)parent, options, 1, dup);
Michal Vaskoddd76592022-01-17 13:34:48 +01002077}
2078
2079LIBYANG_API_DEF LY_ERR
2080lyd_dup_single_to_ctx(const struct lyd_node *node, const struct ly_ctx *trg_ctx, struct lyd_node_inner *parent,
2081 uint32_t options, struct lyd_node **dup)
2082{
2083 LY_CHECK_ARG_RET(trg_ctx, node, trg_ctx, LY_EINVAL);
2084
Michal Vasko58d89e92023-05-23 09:56:19 +02002085 return lyd_dup(node, trg_ctx, (struct lyd_node *)parent, options, 1, dup);
Michal Vasko3a41dff2020-07-15 14:30:28 +02002086}
2087
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002088LIBYANG_API_DEF LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02002089lyd_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 +02002090{
Michal Vaskoddd76592022-01-17 13:34:48 +01002091 LY_CHECK_ARG_RET(NULL, node, LY_EINVAL);
Michal Vasko1feb9bb2022-07-20 08:44:07 +02002092 LY_CHECK_RET(lyd_dup_ctx_check(node, parent));
Michal Vaskoddd76592022-01-17 13:34:48 +01002093
Michal Vasko58d89e92023-05-23 09:56:19 +02002094 return lyd_dup(node, LYD_CTX(node), (struct lyd_node *)parent, options, 0, dup);
Michal Vaskoddd76592022-01-17 13:34:48 +01002095}
2096
2097LIBYANG_API_DEF LY_ERR
2098lyd_dup_siblings_to_ctx(const struct lyd_node *node, const struct ly_ctx *trg_ctx, struct lyd_node_inner *parent,
2099 uint32_t options, struct lyd_node **dup)
2100{
2101 LY_CHECK_ARG_RET(trg_ctx, node, trg_ctx, LY_EINVAL);
2102
Michal Vasko58d89e92023-05-23 09:56:19 +02002103 return lyd_dup(node, trg_ctx, (struct lyd_node *)parent, options, 0, dup);
Michal Vasko3a41dff2020-07-15 14:30:28 +02002104}
2105
aPiecek41680342023-11-08 10:19:44 +01002106LY_ERR
2107lyd_dup_meta_single_to_ctx(const struct ly_ctx *parent_ctx, const struct lyd_meta *meta, struct lyd_node *parent,
2108 struct lyd_meta **dup)
Michal Vasko25a32822020-07-09 15:48:22 +02002109{
Radek Krejci011e4aa2020-09-04 15:22:31 +02002110 LY_ERR ret = LY_SUCCESS;
Michal Vasko25a32822020-07-09 15:48:22 +02002111 struct lyd_meta *mt, *last;
aPiecek41680342023-11-08 10:19:44 +01002112 const struct lysc_type *ant_type;
2113 struct lys_module *mod;
2114 const char *val_can;
Michal Vasko25a32822020-07-09 15:48:22 +02002115
aPiecek41680342023-11-08 10:19:44 +01002116 LY_CHECK_ARG_RET(NULL, meta, parent, LY_EINVAL);
Michal Vasko33c48972022-07-20 10:28:07 +02002117
Michal Vasko25a32822020-07-09 15:48:22 +02002118 /* create a copy */
2119 mt = calloc(1, sizeof *mt);
aPiecek41680342023-11-08 10:19:44 +01002120 LY_CHECK_ERR_RET(!mt, LOGMEM(LYD_CTX(parent)), LY_EMEM);
2121
2122 if (parent_ctx != meta->annotation->module->ctx) {
2123 /* different contexts */
2124 mod = ly_ctx_get_module(parent_ctx, meta->annotation->module->name, meta->annotation->module->revision);
2125
2126 /* annotation */
2127 mt->annotation = lyd_get_meta_annotation(mod, meta->name, strlen(meta->name));
2128 lyplg_ext_get_storage(mt->annotation, LY_STMT_TYPE, sizeof ant_type, (const void **)&ant_type);
2129 LY_CHECK_ERR_GOTO((ret = mt->annotation ? LY_SUCCESS : LY_EINVAL), LOGERR(parent_ctx, LY_EINVAL,
2130 "Annotation for metadata %s not found, value duplication failed.", meta->name), finish);
2131
2132 /* duplicate callback expect only the same contexts, so use the store callback */
2133 val_can = lyd_value_get_canonical(meta->annotation->module->ctx, &meta->value);
2134 ret = lyd_value_store(parent_ctx, &mt->value, ant_type, val_can, strlen(val_can), 1, NULL,
2135 LY_VALUE_CANON, NULL, LYD_HINT_DATA, parent->schema, NULL);
2136 } else {
2137 /* annotation */
2138 mt->annotation = meta->annotation;
2139 /* duplication of value */
2140 ret = meta->value.realtype->plugin->duplicate(parent_ctx, &meta->value, &mt->value);
2141 }
2142 LY_CHECK_ERR_GOTO(ret, LOGERR(LYD_CTX(parent), LY_EINT, "Value duplication failed."), finish);
2143 LY_CHECK_GOTO(ret = lydict_insert(parent_ctx, meta->name, 0, &mt->name), finish);
Michal Vasko25a32822020-07-09 15:48:22 +02002144
2145 /* insert as the last attribute */
aPiecek41680342023-11-08 10:19:44 +01002146 mt->parent = parent;
2147 if (parent->meta) {
2148 for (last = parent->meta; last->next; last = last->next) {}
Michal Vasko25a32822020-07-09 15:48:22 +02002149 last->next = mt;
2150 } else {
aPiecek41680342023-11-08 10:19:44 +01002151 parent->meta = mt;
Michal Vasko25a32822020-07-09 15:48:22 +02002152 }
2153
Radek Krejci011e4aa2020-09-04 15:22:31 +02002154finish:
2155 if (ret) {
2156 lyd_free_meta_single(mt);
2157 } else if (dup) {
Michal Vasko3a41dff2020-07-15 14:30:28 +02002158 *dup = mt;
2159 }
2160 return LY_SUCCESS;
Michal Vasko25a32822020-07-09 15:48:22 +02002161}
2162
aPiecek41680342023-11-08 10:19:44 +01002163LIBYANG_API_DEF LY_ERR
2164lyd_dup_meta_single(const struct lyd_meta *meta, struct lyd_node *node, struct lyd_meta **dup)
2165{
2166 LY_CHECK_ARG_RET(NULL, meta, LY_EINVAL);
2167
2168 /* log to node context but value must always use the annotation context */
2169 return lyd_dup_meta_single_to_ctx(meta->annotation->module->ctx, meta, node, dup);
2170}
2171
Michal Vasko4490d312020-06-16 13:08:55 +02002172/**
2173 * @brief Merge a source sibling into target siblings.
2174 *
2175 * @param[in,out] first_trg First target sibling, is updated if top-level.
2176 * @param[in] parent_trg Target parent.
aPiecek55653c92023-11-09 13:43:19 +01002177 * @param[in,out] sibling_src_p Source sibling to merge, set to NULL if spent.
Michal Vaskocd3f6172021-05-18 16:14:50 +02002178 * @param[in] merge_cb Optional merge callback.
2179 * @param[in] cb_data Arbitrary callback data.
Michal Vasko4490d312020-06-16 13:08:55 +02002180 * @param[in] options Merge options.
Michal Vaskocd3f6172021-05-18 16:14:50 +02002181 * @param[in,out] dup_inst Duplicate instance cache for all @p first_trg siblings.
Michal Vasko4490d312020-06-16 13:08:55 +02002182 * @return LY_ERR value.
2183 */
2184static LY_ERR
2185lyd_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 +02002186 lyd_merge_cb merge_cb, void *cb_data, uint16_t options, struct ly_ht **dup_inst)
Michal Vasko4490d312020-06-16 13:08:55 +02002187{
Michal Vasko4490d312020-06-16 13:08:55 +02002188 const struct lyd_node *child_src, *tmp, *sibling_src;
Michal Vasko56daf732020-08-10 10:57:18 +02002189 struct lyd_node *match_trg, *dup_src, *elem;
Michal Vaskod1afa502022-01-27 16:18:12 +01002190 struct lyd_node_opaq *opaq_trg, *opaq_src;
Michal Vasko4490d312020-06-16 13:08:55 +02002191 struct lysc_type *type;
Michal Vasko8efac242023-03-30 08:24:56 +02002192 struct ly_ht *child_dup_inst = NULL;
Michal Vasko42c96e22024-01-18 08:18:06 +01002193 LY_ERR r;
Michal Vaskocd3f6172021-05-18 16:14:50 +02002194 ly_bool first_inst = 0;
Michal Vasko4490d312020-06-16 13:08:55 +02002195
2196 sibling_src = *sibling_src_p;
Michal Vaskocd3f6172021-05-18 16:14:50 +02002197 if (!sibling_src->schema) {
2198 /* try to find the same opaque node */
Michal Vasko42c96e22024-01-18 08:18:06 +01002199 r = lyd_find_sibling_opaq_next(*first_trg, LYD_NAME(sibling_src), &match_trg);
Michal Vaskocd3f6172021-05-18 16:14:50 +02002200 } else if (sibling_src->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) {
Michal Vasko4490d312020-06-16 13:08:55 +02002201 /* try to find the exact instance */
Michal Vasko42c96e22024-01-18 08:18:06 +01002202 r = lyd_find_sibling_first(*first_trg, sibling_src, &match_trg);
Michal Vasko4490d312020-06-16 13:08:55 +02002203 } else {
2204 /* try to simply find the node, there cannot be more instances */
Michal Vasko42c96e22024-01-18 08:18:06 +01002205 r = lyd_find_sibling_val(*first_trg, sibling_src->schema, NULL, 0, &match_trg);
Michal Vasko4490d312020-06-16 13:08:55 +02002206 }
Michal Vasko42c96e22024-01-18 08:18:06 +01002207 LY_CHECK_RET(r && (r != LY_ENOTFOUND), r);
Michal Vasko4490d312020-06-16 13:08:55 +02002208
Michal Vaskocd3f6172021-05-18 16:14:50 +02002209 if (match_trg) {
2210 /* update match as needed */
2211 LY_CHECK_RET(lyd_dup_inst_next(&match_trg, *first_trg, dup_inst));
2212 } else {
2213 /* first instance of this node */
2214 first_inst = 1;
2215 }
2216
2217 if (match_trg) {
2218 /* call callback */
2219 if (merge_cb) {
2220 LY_CHECK_RET(merge_cb(match_trg, sibling_src, cb_data));
2221 }
2222
Michal Vasko4490d312020-06-16 13:08:55 +02002223 /* node found, make sure even value matches for all node types */
Michal Vaskod1afa502022-01-27 16:18:12 +01002224 if (!match_trg->schema) {
2225 if (lyd_compare_single(sibling_src, match_trg, 0)) {
2226 /* update value */
2227 opaq_trg = (struct lyd_node_opaq *)match_trg;
2228 opaq_src = (struct lyd_node_opaq *)sibling_src;
2229
2230 lydict_remove(LYD_CTX(opaq_trg), opaq_trg->value);
2231 lydict_insert(LYD_CTX(opaq_trg), opaq_src->value, 0, &opaq_trg->value);
2232 opaq_trg->hints = opaq_src->hints;
2233
2234 ly_free_prefix_data(opaq_trg->format, opaq_trg->val_prefix_data);
2235 opaq_trg->format = opaq_src->format;
2236 ly_dup_prefix_data(LYD_CTX(opaq_trg), opaq_src->format, opaq_src->val_prefix_data,
2237 &opaq_trg->val_prefix_data);
2238 }
2239 } else if ((match_trg->schema->nodetype == LYS_LEAF) &&
2240 lyd_compare_single(sibling_src, match_trg, LYD_COMPARE_DEFAULTS)) {
Michal Vasko4490d312020-06-16 13:08:55 +02002241 /* since they are different, they cannot both be default */
2242 assert(!(sibling_src->flags & LYD_DEFAULT) || !(match_trg->flags & LYD_DEFAULT));
2243
Michal Vasko3a41dff2020-07-15 14:30:28 +02002244 /* update value (or only LYD_DEFAULT flag) only if flag set or the source node is not default */
2245 if ((options & LYD_MERGE_DEFAULTS) || !(sibling_src->flags & LYD_DEFAULT)) {
Michal Vasko4490d312020-06-16 13:08:55 +02002246 type = ((struct lysc_node_leaf *)match_trg->schema)->type;
Michal Vaskob7be7a82020-08-20 09:09:04 +02002247 type->plugin->free(LYD_CTX(match_trg), &((struct lyd_node_term *)match_trg)->value);
2248 LY_CHECK_RET(type->plugin->duplicate(LYD_CTX(match_trg), &((struct lyd_node_term *)sibling_src)->value,
Michal Vasko69730152020-10-09 16:30:07 +02002249 &((struct lyd_node_term *)match_trg)->value));
Michal Vasko4490d312020-06-16 13:08:55 +02002250
2251 /* copy flags and add LYD_NEW */
Michal Vasko7e8315b2021-08-03 17:01:06 +02002252 match_trg->flags = sibling_src->flags | ((options & LYD_MERGE_WITH_FLAGS) ? 0 : LYD_NEW);
Michal Vasko4490d312020-06-16 13:08:55 +02002253 }
Michal Vasko8f359bf2020-07-28 10:41:15 +02002254 } else if ((match_trg->schema->nodetype & LYS_ANYDATA) && lyd_compare_single(sibling_src, match_trg, 0)) {
Michal Vasko4c583e82020-07-17 12:16:14 +02002255 /* update value */
2256 LY_CHECK_RET(lyd_any_copy_value(match_trg, &((struct lyd_node_any *)sibling_src)->value,
Radek Krejci0f969882020-08-21 16:56:47 +02002257 ((struct lyd_node_any *)sibling_src)->value_type));
Michal Vasko4490d312020-06-16 13:08:55 +02002258
2259 /* copy flags and add LYD_NEW */
Michal Vasko7e8315b2021-08-03 17:01:06 +02002260 match_trg->flags = sibling_src->flags | ((options & LYD_MERGE_WITH_FLAGS) ? 0 : LYD_NEW);
Michal Vaskocd3f6172021-05-18 16:14:50 +02002261 }
2262
2263 /* check descendants, recursively */
Michal Vasko42c96e22024-01-18 08:18:06 +01002264 r = LY_SUCCESS;
Michal Vaskocd3f6172021-05-18 16:14:50 +02002265 LY_LIST_FOR_SAFE(lyd_child_no_keys(sibling_src), tmp, child_src) {
Michal Vasko42c96e22024-01-18 08:18:06 +01002266 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 +02002267 &child_dup_inst);
Michal Vasko42c96e22024-01-18 08:18:06 +01002268 if (r) {
Michal Vaskocd3f6172021-05-18 16:14:50 +02002269 break;
Michal Vasko4490d312020-06-16 13:08:55 +02002270 }
2271 }
Michal Vaskocd3f6172021-05-18 16:14:50 +02002272 lyd_dup_inst_free(child_dup_inst);
Michal Vasko42c96e22024-01-18 08:18:06 +01002273 LY_CHECK_RET(r);
Michal Vasko4490d312020-06-16 13:08:55 +02002274 } else {
2275 /* node not found, merge it */
2276 if (options & LYD_MERGE_DESTRUCT) {
2277 dup_src = (struct lyd_node *)sibling_src;
Michal Vasko2e784f82024-01-11 09:51:22 +01002278 lyd_unlink(dup_src);
Michal Vasko4490d312020-06-16 13:08:55 +02002279 /* spend it */
2280 *sibling_src_p = NULL;
2281 } else {
Michal Vasko3a41dff2020-07-15 14:30:28 +02002282 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 +02002283 }
2284
Michal Vasko7e8315b2021-08-03 17:01:06 +02002285 if (!(options & LYD_MERGE_WITH_FLAGS)) {
2286 /* set LYD_NEW for all the new nodes, required for validation */
2287 LYD_TREE_DFS_BEGIN(dup_src, elem) {
2288 elem->flags |= LYD_NEW;
2289 LYD_TREE_DFS_END(dup_src, elem);
2290 }
Michal Vasko4490d312020-06-16 13:08:55 +02002291 }
2292
Michal Vaskocd3f6172021-05-18 16:14:50 +02002293 /* insert */
Michal Vasko6ee6f432021-07-16 09:49:14 +02002294 lyd_insert_node(parent_trg, first_trg, dup_src, 0);
Michal Vaskocd3f6172021-05-18 16:14:50 +02002295
2296 if (first_inst) {
2297 /* remember not to find this instance next time */
2298 LY_CHECK_RET(lyd_dup_inst_next(&dup_src, *first_trg, dup_inst));
2299 }
2300
2301 /* call callback, no source node */
2302 if (merge_cb) {
2303 LY_CHECK_RET(merge_cb(dup_src, NULL, cb_data));
2304 }
Michal Vasko4490d312020-06-16 13:08:55 +02002305 }
2306
2307 return LY_SUCCESS;
2308}
2309
Michal Vasko3a41dff2020-07-15 14:30:28 +02002310static LY_ERR
Michal Vaskocd3f6172021-05-18 16:14:50 +02002311lyd_merge(struct lyd_node **target, const struct lyd_node *source, const struct lys_module *mod,
2312 lyd_merge_cb merge_cb, void *cb_data, uint16_t options, ly_bool nosiblings)
Michal Vasko4490d312020-06-16 13:08:55 +02002313{
2314 const struct lyd_node *sibling_src, *tmp;
Michal Vasko8efac242023-03-30 08:24:56 +02002315 struct ly_ht *dup_inst = NULL;
Radek Krejci857189e2020-09-01 13:26:36 +02002316 ly_bool first;
Michal Vaskocd3f6172021-05-18 16:14:50 +02002317 LY_ERR ret = LY_SUCCESS;
Michal Vasko4490d312020-06-16 13:08:55 +02002318
2319 LY_CHECK_ARG_RET(NULL, target, LY_EINVAL);
Michal Vasko892f5bf2021-11-24 10:41:05 +01002320 LY_CHECK_CTX_EQUAL_RET(*target ? LYD_CTX(*target) : NULL, source ? LYD_CTX(source) : NULL, mod ? mod->ctx : NULL,
2321 LY_EINVAL);
Michal Vasko4490d312020-06-16 13:08:55 +02002322
2323 if (!source) {
2324 /* nothing to merge */
2325 return LY_SUCCESS;
2326 }
2327
Michal Vasko831422b2020-11-24 11:20:51 +01002328 if ((*target && lysc_data_parent((*target)->schema)) || lysc_data_parent(source->schema)) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02002329 LOGERR(LYD_CTX(source), LY_EINVAL, "Invalid arguments - can merge only 2 top-level subtrees (%s()).", __func__);
Michal Vasko4490d312020-06-16 13:08:55 +02002330 return LY_EINVAL;
2331 }
2332
2333 LY_LIST_FOR_SAFE(source, tmp, sibling_src) {
Michal Vaskocd3f6172021-05-18 16:14:50 +02002334 if (mod && (lyd_owner_module(sibling_src) != mod)) {
2335 /* skip data nodes from different modules */
2336 continue;
2337 }
2338
Radek Krejci857189e2020-09-01 13:26:36 +02002339 first = (sibling_src == source) ? 1 : 0;
Michal Vaskocd3f6172021-05-18 16:14:50 +02002340 ret = lyd_merge_sibling_r(target, NULL, &sibling_src, merge_cb, cb_data, options, &dup_inst);
2341 if (ret) {
2342 break;
2343 }
Michal Vasko4490d312020-06-16 13:08:55 +02002344 if (first && !sibling_src) {
2345 /* source was spent (unlinked), move to the next node */
2346 source = tmp;
2347 }
2348
Michal Vasko3a41dff2020-07-15 14:30:28 +02002349 if (nosiblings) {
Michal Vasko4490d312020-06-16 13:08:55 +02002350 break;
2351 }
2352 }
2353
2354 if (options & LYD_MERGE_DESTRUCT) {
2355 /* free any leftover source data that were not merged */
2356 lyd_free_siblings((struct lyd_node *)source);
2357 }
2358
Michal Vaskocd3f6172021-05-18 16:14:50 +02002359 lyd_dup_inst_free(dup_inst);
2360 return ret;
Michal Vasko4490d312020-06-16 13:08:55 +02002361}
2362
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002363LIBYANG_API_DEF LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02002364lyd_merge_tree(struct lyd_node **target, const struct lyd_node *source, uint16_t options)
Michal Vasko3a41dff2020-07-15 14:30:28 +02002365{
Michal Vaskocd3f6172021-05-18 16:14:50 +02002366 return lyd_merge(target, source, NULL, NULL, NULL, options, 1);
Michal Vasko3a41dff2020-07-15 14:30:28 +02002367}
2368
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002369LIBYANG_API_DEF LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02002370lyd_merge_siblings(struct lyd_node **target, const struct lyd_node *source, uint16_t options)
Michal Vasko3a41dff2020-07-15 14:30:28 +02002371{
Michal Vaskocd3f6172021-05-18 16:14:50 +02002372 return lyd_merge(target, source, NULL, NULL, NULL, options, 0);
2373}
2374
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002375LIBYANG_API_DEF LY_ERR
Michal Vaskocd3f6172021-05-18 16:14:50 +02002376lyd_merge_module(struct lyd_node **target, const struct lyd_node *source, const struct lys_module *mod,
2377 lyd_merge_cb merge_cb, void *cb_data, uint16_t options)
2378{
2379 return lyd_merge(target, source, mod, merge_cb, cb_data, options, 0);
Michal Vasko3a41dff2020-07-15 14:30:28 +02002380}
2381
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002382static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02002383lyd_path_str_enlarge(char **buffer, size_t *buflen, size_t reqlen, ly_bool is_static)
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002384{
Michal Vasko14654712020-02-06 08:35:21 +01002385 /* ending \0 */
2386 ++reqlen;
2387
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002388 if (reqlen > *buflen) {
2389 if (is_static) {
2390 return LY_EINCOMPLETE;
2391 }
2392
2393 *buffer = ly_realloc(*buffer, reqlen * sizeof **buffer);
2394 if (!*buffer) {
2395 return LY_EMEM;
2396 }
2397
2398 *buflen = reqlen;
2399 }
2400
2401 return LY_SUCCESS;
2402}
2403
Michal Vaskod59035b2020-07-08 12:00:06 +02002404LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02002405lyd_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 +02002406{
2407 const struct lyd_node *key;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002408 size_t len;
2409 const char *val;
2410 char quot;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002411
Michal Vasko824d0832021-11-04 15:36:51 +01002412 for (key = lyd_child(node); key && key->schema && (key->schema->flags & LYS_KEY); key = key->next) {
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02002413 val = lyd_get_value(key);
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002414 len = 1 + strlen(key->schema->name) + 2 + strlen(val) + 2;
Michal Vaskoba99a3e2020-08-18 15:50:05 +02002415 LY_CHECK_RET(lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static));
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002416
2417 quot = '\'';
2418 if (strchr(val, '\'')) {
2419 quot = '"';
2420 }
2421 *bufused += sprintf(*buffer + *bufused, "[%s=%c%s%c]", key->schema->name, quot, val, quot);
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002422 }
2423
2424 return LY_SUCCESS;
2425}
2426
2427/**
2428 * @brief Append leaf-list value predicate to path.
2429 *
2430 * @param[in] node Node to print.
2431 * @param[in,out] buffer Buffer to print to.
2432 * @param[in,out] buflen Current buffer length.
2433 * @param[in,out] bufused Current number of characters used in @p buffer.
2434 * @param[in] is_static Whether buffer is static or can be reallocated.
2435 * @return LY_ERR
2436 */
2437static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02002438lyd_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 +02002439{
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002440 size_t len;
2441 const char *val;
2442 char quot;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002443
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02002444 val = lyd_get_value(node);
Radek Krejcif13b87b2020-12-01 22:02:17 +01002445 len = 4 + strlen(val) + 2; /* "[.='" + val + "']" */
Michal Vaskoba99a3e2020-08-18 15:50:05 +02002446 LY_CHECK_RET(lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static));
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002447
2448 quot = '\'';
2449 if (strchr(val, '\'')) {
2450 quot = '"';
2451 }
2452 *bufused += sprintf(*buffer + *bufused, "[.=%c%s%c]", quot, val, quot);
2453
Michal Vaskoba99a3e2020-08-18 15:50:05 +02002454 return LY_SUCCESS;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002455}
2456
2457/**
2458 * @brief Append node position (relative to its other instances) predicate to path.
2459 *
2460 * @param[in] node Node to print.
2461 * @param[in,out] buffer Buffer to print to.
2462 * @param[in,out] buflen Current buffer length.
2463 * @param[in,out] bufused Current number of characters used in @p buffer.
2464 * @param[in] is_static Whether buffer is static or can be reallocated.
2465 * @return LY_ERR
2466 */
2467static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02002468lyd_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 +02002469{
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002470 size_t len;
Michal Vasko50cc0562021-05-18 16:15:43 +02002471 uint32_t pos;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002472 char *val = NULL;
2473 LY_ERR rc;
2474
Michal Vasko50cc0562021-05-18 16:15:43 +02002475 pos = lyd_list_pos(node);
2476 if (asprintf(&val, "%" PRIu32, pos) == -1) {
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002477 return LY_EMEM;
2478 }
2479
2480 len = 1 + strlen(val) + 1;
2481 rc = lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static);
2482 if (rc != LY_SUCCESS) {
2483 goto cleanup;
2484 }
2485
2486 *bufused += sprintf(*buffer + *bufused, "[%s]", val);
2487
2488cleanup:
2489 free(val);
2490 return rc;
2491}
2492
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002493LIBYANG_API_DEF char *
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002494lyd_path(const struct lyd_node *node, LYD_PATH_TYPE pathtype, char *buffer, size_t buflen)
2495{
Radek Krejci857189e2020-09-01 13:26:36 +02002496 ly_bool is_static = 0;
Radek Krejci1deb5be2020-08-26 16:43:36 +02002497 uint32_t i, depth;
Michal Vasko14654712020-02-06 08:35:21 +01002498 size_t bufused = 0, len;
Michal Vasko12eb6222022-03-18 13:35:49 +01002499 const struct lyd_node *iter, *parent;
2500 const struct lys_module *mod, *prev_mod;
Michal Vasko790b2bc2020-08-03 13:35:06 +02002501 LY_ERR rc = LY_SUCCESS;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002502
2503 LY_CHECK_ARG_RET(NULL, node, NULL);
2504 if (buffer) {
Michal Vasko16385f42021-05-18 16:16:09 +02002505 LY_CHECK_ARG_RET(LYD_CTX(node), buflen > 1, NULL);
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002506 is_static = 1;
Michal Vasko14654712020-02-06 08:35:21 +01002507 } else {
2508 buflen = 0;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002509 }
2510
2511 switch (pathtype) {
Radek Krejci635d2b82021-01-04 11:26:51 +01002512 case LYD_PATH_STD:
2513 case LYD_PATH_STD_NO_LAST_PRED:
Michal Vasko14654712020-02-06 08:35:21 +01002514 depth = 1;
Michal Vasko9e685082021-01-29 14:49:09 +01002515 for (iter = node; iter->parent; iter = lyd_parent(iter)) {
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002516 ++depth;
2517 }
2518
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002519 goto iter_print;
Michal Vasko14654712020-02-06 08:35:21 +01002520 while (depth) {
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002521 /* find the right node */
Michal Vasko9e685082021-01-29 14:49:09 +01002522 for (iter = node, i = 1; i < depth; iter = lyd_parent(iter), ++i) {}
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002523iter_print:
Michal Vasko12eb6222022-03-18 13:35:49 +01002524 /* get the module */
Michal Vasko420cc252023-08-24 08:14:24 +02002525 mod = lyd_node_module(iter);
Michal Vasko12eb6222022-03-18 13:35:49 +01002526 parent = lyd_parent(iter);
Michal Vasko420cc252023-08-24 08:14:24 +02002527 prev_mod = lyd_node_module(parent);
Michal Vasko12eb6222022-03-18 13:35:49 +01002528 if (prev_mod == mod) {
2529 mod = NULL;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002530 }
2531
2532 /* realloc string */
Michal Vaskoad92b672020-11-12 13:11:31 +01002533 len = 1 + (mod ? strlen(mod->name) + 1 : 0) + (iter->schema ? strlen(iter->schema->name) :
2534 strlen(((struct lyd_node_opaq *)iter)->name.name));
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002535 rc = lyd_path_str_enlarge(&buffer, &buflen, bufused + len, is_static);
2536 if (rc != LY_SUCCESS) {
2537 break;
2538 }
2539
2540 /* print next node */
Michal Vaskodbf3e652022-10-21 08:46:25 +02002541 bufused += sprintf(buffer + bufused, "/%s%s%s", mod ? mod->name : "", mod ? ":" : "", LYD_NAME(iter));
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002542
Michal Vasko790b2bc2020-08-03 13:35:06 +02002543 /* do not always print the last (first) predicate */
Radek Krejci635d2b82021-01-04 11:26:51 +01002544 if (iter->schema && ((depth > 1) || (pathtype == LYD_PATH_STD))) {
Michal Vasko790b2bc2020-08-03 13:35:06 +02002545 switch (iter->schema->nodetype) {
2546 case LYS_LIST:
2547 if (iter->schema->flags & LYS_KEYLESS) {
2548 /* print its position */
2549 rc = lyd_path_position_predicate(iter, &buffer, &buflen, &bufused, is_static);
2550 } else {
2551 /* print all list keys in predicates */
2552 rc = lyd_path_list_predicate(iter, &buffer, &buflen, &bufused, is_static);
2553 }
2554 break;
2555 case LYS_LEAFLIST:
2556 if (iter->schema->flags & LYS_CONFIG_W) {
2557 /* print leaf-list value */
2558 rc = lyd_path_leaflist_predicate(iter, &buffer, &buflen, &bufused, is_static);
2559 } else {
2560 /* print its position */
2561 rc = lyd_path_position_predicate(iter, &buffer, &buflen, &bufused, is_static);
2562 }
2563 break;
2564 default:
2565 /* nothing to print more */
2566 break;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002567 }
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002568 }
2569 if (rc != LY_SUCCESS) {
2570 break;
2571 }
2572
Michal Vasko14654712020-02-06 08:35:21 +01002573 --depth;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002574 }
2575 break;
2576 }
2577
2578 return buffer;
2579}
Michal Vaskoe444f752020-02-10 12:20:06 +01002580
Michal Vaskodbf3e652022-10-21 08:46:25 +02002581char *
2582lyd_path_set(const struct ly_set *dnodes, LYD_PATH_TYPE pathtype)
2583{
2584 uint32_t depth;
2585 size_t bufused = 0, buflen = 0, len;
2586 char *buffer = NULL;
2587 const struct lyd_node *iter, *parent;
2588 const struct lys_module *mod, *prev_mod;
2589 LY_ERR rc = LY_SUCCESS;
2590
2591 switch (pathtype) {
2592 case LYD_PATH_STD:
2593 case LYD_PATH_STD_NO_LAST_PRED:
2594 for (depth = 1; depth <= dnodes->count; ++depth) {
2595 /* current node */
2596 iter = dnodes->dnodes[depth - 1];
Michal Vasko420cc252023-08-24 08:14:24 +02002597 mod = lyd_node_module(iter);
Michal Vaskodbf3e652022-10-21 08:46:25 +02002598
2599 /* parent */
2600 parent = (depth > 1) ? dnodes->dnodes[depth - 2] : NULL;
Michal Vasko85be65e2023-06-13 09:44:17 +02002601 assert(!parent || !iter->schema || !parent->schema || (parent->schema->nodetype & LYD_NODE_ANY) ||
2602 (lysc_data_parent(iter->schema) == parent->schema) ||
Michal Vasko539d6a92023-09-11 10:31:12 +02002603 (!lysc_data_parent(iter->schema) && (LYD_CTX(iter) != LYD_CTX(parent))) ||
2604 (parent->schema->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)));
Michal Vaskodbf3e652022-10-21 08:46:25 +02002605
2606 /* get module to print, if any */
Michal Vasko420cc252023-08-24 08:14:24 +02002607 prev_mod = lyd_node_module(parent);
Michal Vaskodbf3e652022-10-21 08:46:25 +02002608 if (prev_mod == mod) {
2609 mod = NULL;
2610 }
2611
2612 /* realloc string */
2613 len = 1 + (mod ? strlen(mod->name) + 1 : 0) + (iter->schema ? strlen(iter->schema->name) :
2614 strlen(((struct lyd_node_opaq *)iter)->name.name));
2615 if ((rc = lyd_path_str_enlarge(&buffer, &buflen, bufused + len, 0))) {
2616 break;
2617 }
2618
2619 /* print next node */
2620 bufused += sprintf(buffer + bufused, "/%s%s%s", mod ? mod->name : "", mod ? ":" : "", LYD_NAME(iter));
2621
2622 /* do not always print the last (first) predicate */
2623 if (iter->schema && ((depth > 1) || (pathtype == LYD_PATH_STD))) {
2624 switch (iter->schema->nodetype) {
2625 case LYS_LIST:
2626 if (iter->schema->flags & LYS_KEYLESS) {
2627 /* print its position */
2628 rc = lyd_path_position_predicate(iter, &buffer, &buflen, &bufused, 0);
2629 } else {
2630 /* print all list keys in predicates */
2631 rc = lyd_path_list_predicate(iter, &buffer, &buflen, &bufused, 0);
2632 }
2633 break;
2634 case LYS_LEAFLIST:
2635 if (iter->schema->flags & LYS_CONFIG_W) {
2636 /* print leaf-list value */
2637 rc = lyd_path_leaflist_predicate(iter, &buffer, &buflen, &bufused, 0);
2638 } else {
2639 /* print its position */
2640 rc = lyd_path_position_predicate(iter, &buffer, &buflen, &bufused, 0);
2641 }
2642 break;
2643 default:
2644 /* nothing to print more */
2645 break;
2646 }
2647 }
2648 if (rc) {
2649 break;
2650 }
2651 }
2652 break;
2653 }
2654
2655 return buffer;
2656}
2657
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002658LIBYANG_API_DEF struct lyd_meta *
Michal Vasko25a32822020-07-09 15:48:22 +02002659lyd_find_meta(const struct lyd_meta *first, const struct lys_module *module, const char *name)
2660{
2661 struct lyd_meta *ret = NULL;
2662 const struct ly_ctx *ctx;
2663 const char *prefix, *tmp;
2664 char *str;
2665 size_t pref_len, name_len;
2666
2667 LY_CHECK_ARG_RET(NULL, module || strchr(name, ':'), name, NULL);
Michal Vasko892f5bf2021-11-24 10:41:05 +01002668 LY_CHECK_CTX_EQUAL_RET(first ? first->annotation->module->ctx : NULL, module ? module->ctx : NULL, NULL);
Michal Vasko25a32822020-07-09 15:48:22 +02002669
2670 if (!first) {
2671 return NULL;
2672 }
2673
2674 ctx = first->annotation->module->ctx;
2675
2676 /* parse the name */
2677 tmp = name;
2678 if (ly_parse_nodeid(&tmp, &prefix, &pref_len, &name, &name_len) || tmp[0]) {
2679 LOGERR(ctx, LY_EINVAL, "Metadata name \"%s\" is not valid.", name);
2680 return NULL;
2681 }
2682
2683 /* find the module */
2684 if (prefix) {
2685 str = strndup(prefix, pref_len);
2686 module = ly_ctx_get_module_latest(ctx, str);
2687 free(str);
Radek Krejci422afb12021-03-04 16:38:16 +01002688 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 +02002689 }
2690
2691 /* find the metadata */
2692 LY_LIST_FOR(first, first) {
2693 if ((first->annotation->module == module) && !strcmp(first->name, name)) {
2694 ret = (struct lyd_meta *)first;
2695 break;
2696 }
2697 }
2698
2699 return ret;
2700}
2701
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002702LIBYANG_API_DEF LY_ERR
Michal Vaskoe444f752020-02-10 12:20:06 +01002703lyd_find_sibling_first(const struct lyd_node *siblings, const struct lyd_node *target, struct lyd_node **match)
2704{
Michal Vasko9beceb82022-04-05 12:14:15 +02002705 struct lyd_node **match_p, *iter, *dup = NULL;
Michal Vaskoe444f752020-02-10 12:20:06 +01002706 struct lyd_node_inner *parent;
Michal Vaskoe78faec2021-04-08 17:24:43 +02002707 ly_bool found;
Michal Vaskoe444f752020-02-10 12:20:06 +01002708
Michal Vaskof03ed032020-03-04 13:31:44 +01002709 LY_CHECK_ARG_RET(NULL, target, LY_EINVAL);
Michal Vasko9beceb82022-04-05 12:14:15 +02002710 if (!siblings) {
2711 /* no data */
2712 if (match) {
2713 *match = NULL;
2714 }
2715 return LY_ENOTFOUND;
2716 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002717
Michal Vasko9beceb82022-04-05 12:14:15 +02002718 if (LYD_CTX(siblings) != LYD_CTX(target)) {
2719 /* create a duplicate in this context */
2720 LY_CHECK_RET(lyd_dup_single_to_ctx(target, LYD_CTX(siblings), NULL, 0, &dup));
2721 target = dup;
2722 }
2723
2724 if ((siblings->schema && target->schema && (lysc_data_parent(siblings->schema) != lysc_data_parent(target->schema)))) {
2725 /* schema mismatch */
2726 lyd_free_tree(dup);
Michal Vasko9b368d32020-02-14 13:53:31 +01002727 if (match) {
2728 *match = NULL;
2729 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002730 return LY_ENOTFOUND;
2731 }
2732
Michal Vaskoe78faec2021-04-08 17:24:43 +02002733 /* get first sibling */
2734 siblings = lyd_first_sibling(siblings);
Michal Vaskoe444f752020-02-10 12:20:06 +01002735
Michal Vasko9e685082021-01-29 14:49:09 +01002736 parent = siblings->parent;
Michal Vasko39311152023-08-07 11:03:41 +02002737 if (target->schema && parent && parent->schema && parent->children_ht) {
Michal Vaskoe444f752020-02-10 12:20:06 +01002738 assert(target->hash);
2739
Michal Vaskoe78faec2021-04-08 17:24:43 +02002740 if (lysc_is_dup_inst_list(target->schema)) {
2741 /* we must search the instances from beginning to find the first matching one */
2742 found = 0;
2743 LYD_LIST_FOR_INST(siblings, target->schema, iter) {
Michal Vaskoee9b9482023-06-19 13:17:48 +02002744 if (!lyd_compare_single(target, iter, LYD_COMPARE_FULL_RECURSION)) {
Michal Vaskoe78faec2021-04-08 17:24:43 +02002745 found = 1;
2746 break;
2747 }
2748 }
2749 if (found) {
2750 siblings = iter;
Michal Vaskoda859032020-07-14 12:20:14 +02002751 } else {
2752 siblings = NULL;
2753 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002754 } else {
Michal Vaskoe78faec2021-04-08 17:24:43 +02002755 /* find by hash */
2756 if (!lyht_find(parent->children_ht, &target, target->hash, (void **)&match_p)) {
2757 siblings = *match_p;
2758 } else {
2759 /* not found */
2760 siblings = NULL;
2761 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002762 }
2763 } else {
Michal Vasko39311152023-08-07 11:03:41 +02002764 /* no children hash table or cannot be used */
Michal Vaskod989ba02020-08-24 10:59:24 +02002765 for ( ; siblings; siblings = siblings->next) {
Michal Vaskoee9b9482023-06-19 13:17:48 +02002766 if (lysc_is_dup_inst_list(target->schema)) {
2767 if (!lyd_compare_single(siblings, target, LYD_COMPARE_FULL_RECURSION)) {
2768 break;
2769 }
2770 } else {
Michal Vaskod8a52012023-08-15 11:38:10 +02002771 if (!lyd_compare_single(siblings, target, 0)) {
Michal Vaskoee9b9482023-06-19 13:17:48 +02002772 break;
2773 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002774 }
2775 }
2776 }
2777
Michal Vasko9beceb82022-04-05 12:14:15 +02002778 lyd_free_tree(dup);
Michal Vaskoe444f752020-02-10 12:20:06 +01002779 if (!siblings) {
Michal Vasko9b368d32020-02-14 13:53:31 +01002780 if (match) {
2781 *match = NULL;
2782 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002783 return LY_ENOTFOUND;
2784 }
2785
Michal Vasko9b368d32020-02-14 13:53:31 +01002786 if (match) {
2787 *match = (struct lyd_node *)siblings;
2788 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002789 return LY_SUCCESS;
2790}
2791
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002792LIBYANG_API_DEF LY_ERR
Michal Vaskoe444f752020-02-10 12:20:06 +01002793lyd_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 +02002794 size_t val_len, struct lyd_node **match)
Michal Vaskoe444f752020-02-10 12:20:06 +01002795{
2796 LY_ERR rc;
2797 struct lyd_node *target = NULL;
Michal Vasko9beceb82022-04-05 12:14:15 +02002798 const struct lyd_node *parent;
Michal Vaskoe444f752020-02-10 12:20:06 +01002799
Michal Vasko4c583e82020-07-17 12:16:14 +02002800 LY_CHECK_ARG_RET(NULL, schema, !(schema->nodetype & (LYS_CHOICE | LYS_CASE)), LY_EINVAL);
Michal Vasko9beceb82022-04-05 12:14:15 +02002801 if (!siblings) {
2802 /* no data */
2803 if (match) {
2804 *match = NULL;
2805 }
2806 return LY_ENOTFOUND;
2807 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002808
Michal Vasko9beceb82022-04-05 12:14:15 +02002809 if ((LYD_CTX(siblings) != schema->module->ctx)) {
2810 /* parent of ext nodes is useless */
2811 parent = (siblings->flags & LYD_EXT) ? NULL : lyd_parent(siblings);
Michal Vaskoaf3df492022-12-02 14:03:52 +01002812 if (lyd_find_schema_ctx(schema, LYD_CTX(siblings), parent, 0, &schema)) {
2813 /* no schema node in siblings so certainly no data node either */
2814 if (match) {
2815 *match = NULL;
2816 }
2817 return LY_ENOTFOUND;
2818 }
Michal Vasko9beceb82022-04-05 12:14:15 +02002819 }
2820
2821 if (siblings->schema && (lysc_data_parent(siblings->schema) != lysc_data_parent(schema))) {
2822 /* schema mismatch */
Michal Vasko9b368d32020-02-14 13:53:31 +01002823 if (match) {
2824 *match = NULL;
2825 }
Michal Vaskoe444f752020-02-10 12:20:06 +01002826 return LY_ENOTFOUND;
2827 }
2828
Michal Vaskof03ed032020-03-04 13:31:44 +01002829 if (key_or_value && !val_len) {
2830 val_len = strlen(key_or_value);
2831 }
2832
Michal Vaskob104f112020-07-17 09:54:54 +02002833 if ((schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) && key_or_value) {
2834 /* create a data node and find the instance */
2835 if (schema->nodetype == LYS_LEAFLIST) {
2836 /* target used attributes: schema, hash, value */
Michal Vasko989cdb42023-10-06 15:32:37 +02002837 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 +02002838 LY_CHECK_RET(rc);
Michal Vaskob104f112020-07-17 09:54:54 +02002839 } else {
Michal Vasko90932a92020-02-12 14:33:03 +01002840 /* target used attributes: schema, hash, child (all keys) */
Michal Vasko004d3152020-06-11 19:59:22 +02002841 LY_CHECK_RET(lyd_create_list2(schema, key_or_value, val_len, &target));
Michal Vasko90932a92020-02-12 14:33:03 +01002842 }
2843
2844 /* find it */
2845 rc = lyd_find_sibling_first(siblings, target, match);
Michal Vaskob104f112020-07-17 09:54:54 +02002846 } else {
2847 /* find the first schema node instance */
2848 rc = lyd_find_sibling_schema(siblings, schema, match);
Michal Vaskoe444f752020-02-10 12:20:06 +01002849 }
2850
Michal Vaskoe444f752020-02-10 12:20:06 +01002851 lyd_free_tree(target);
2852 return rc;
2853}
Michal Vaskoccc02342020-05-21 10:09:21 +02002854
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002855LIBYANG_API_DEF LY_ERR
Michal Vaskoe78faec2021-04-08 17:24:43 +02002856lyd_find_sibling_dup_inst_set(const struct lyd_node *siblings, const struct lyd_node *target, struct ly_set **set)
2857{
2858 struct lyd_node **match_p, *first, *iter;
2859 struct lyd_node_inner *parent;
Michal Vaskoee9b9482023-06-19 13:17:48 +02002860 uint32_t comp_opts;
Michal Vaskoe78faec2021-04-08 17:24:43 +02002861
Michal Vasko83ae7772022-06-08 10:01:55 +02002862 LY_CHECK_ARG_RET(NULL, target, set, LY_EINVAL);
Michal Vasko892f5bf2021-11-24 10:41:05 +01002863 LY_CHECK_CTX_EQUAL_RET(siblings ? LYD_CTX(siblings) : NULL, LYD_CTX(target), LY_EINVAL);
Michal Vaskoe78faec2021-04-08 17:24:43 +02002864
2865 LY_CHECK_RET(ly_set_new(set));
2866
2867 if (!siblings || (siblings->schema && (lysc_data_parent(siblings->schema) != lysc_data_parent(target->schema)))) {
2868 /* no data or schema mismatch */
2869 return LY_ENOTFOUND;
2870 }
2871
Michal Vaskoee9b9482023-06-19 13:17:48 +02002872 /* set options */
Michal Vaskod8a52012023-08-15 11:38:10 +02002873 comp_opts = (lysc_is_dup_inst_list(target->schema) ? LYD_COMPARE_FULL_RECURSION : 0);
Michal Vaskoee9b9482023-06-19 13:17:48 +02002874
Michal Vaskoe78faec2021-04-08 17:24:43 +02002875 /* get first sibling */
2876 siblings = lyd_first_sibling(siblings);
2877
2878 parent = siblings->parent;
2879 if (parent && parent->schema && parent->children_ht) {
2880 assert(target->hash);
2881
2882 /* find the first instance */
2883 lyd_find_sibling_first(siblings, target, &first);
2884 if (first) {
2885 /* add it so that it is the first in the set */
2886 if (ly_set_add(*set, first, 1, NULL)) {
2887 goto error;
2888 }
2889
2890 /* find by hash */
2891 if (!lyht_find(parent->children_ht, &target, target->hash, (void **)&match_p)) {
2892 iter = *match_p;
2893 } else {
2894 /* not found */
2895 iter = NULL;
2896 }
2897 while (iter) {
2898 /* add all found nodes into the set */
Michal Vaskoee9b9482023-06-19 13:17:48 +02002899 if ((iter != first) && !lyd_compare_single(iter, target, comp_opts) && ly_set_add(*set, iter, 1, NULL)) {
Michal Vaskoe78faec2021-04-08 17:24:43 +02002900 goto error;
2901 }
2902
2903 /* find next instance */
2904 if (lyht_find_next(parent->children_ht, &iter, iter->hash, (void **)&match_p)) {
2905 iter = NULL;
2906 } else {
2907 iter = *match_p;
2908 }
2909 }
2910 }
2911 } else {
2912 /* no children hash table */
2913 LY_LIST_FOR(siblings, siblings) {
Michal Vaskoee9b9482023-06-19 13:17:48 +02002914 if (!lyd_compare_single(target, siblings, comp_opts)) {
Michal Vaskoe78faec2021-04-08 17:24:43 +02002915 ly_set_add(*set, (void *)siblings, 1, NULL);
2916 }
2917 }
2918 }
2919
2920 if (!(*set)->count) {
2921 return LY_ENOTFOUND;
2922 }
2923 return LY_SUCCESS;
2924
2925error:
2926 ly_set_free(*set, NULL);
2927 *set = NULL;
2928 return LY_EMEM;
2929}
2930
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002931LIBYANG_API_DEF LY_ERR
Michal Vasko1d4af6c2021-02-22 13:31:26 +01002932lyd_find_sibling_opaq_next(const struct lyd_node *first, const char *name, struct lyd_node **match)
2933{
2934 LY_CHECK_ARG_RET(NULL, name, LY_EINVAL);
2935
Michal Vaskoe271a312023-08-15 11:46:30 +02002936 if (first && first->schema) {
2937 first = first->prev;
2938 if (first->schema) {
2939 /* no opaque nodes */
2940 first = NULL;
2941 } else {
2942 /* opaque nodes are at the end, find quickly the first */
2943 while (!first->prev->schema) {
2944 first = first->prev;
2945 }
2946 }
2947 }
2948
Michal Vasko1d4af6c2021-02-22 13:31:26 +01002949 for ( ; first; first = first->next) {
Michal Vaskoe271a312023-08-15 11:46:30 +02002950 assert(!first->schema);
2951 if (!strcmp(LYD_NAME(first), name)) {
Michal Vasko1d4af6c2021-02-22 13:31:26 +01002952 break;
2953 }
2954 }
2955
2956 if (match) {
2957 *match = (struct lyd_node *)first;
2958 }
2959 return first ? LY_SUCCESS : LY_ENOTFOUND;
2960}
2961
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002962LIBYANG_API_DEF LY_ERR
Michal Vaskod96e2372023-02-24 16:07:51 +01002963lyd_find_xpath(const struct lyd_node *ctx_node, const char *xpath, struct ly_set **set)
Michal Vaskoccc02342020-05-21 10:09:21 +02002964{
Michal Vaskod96e2372023-02-24 16:07:51 +01002965 LY_CHECK_ARG_RET(NULL, ctx_node, xpath, set, LY_EINVAL);
Michal Vaskoccc02342020-05-21 10:09:21 +02002966
Michal Vaskobe1b0cb2024-01-22 14:32:15 +01002967 return lyd_find_xpath3(ctx_node, ctx_node, xpath, LY_VALUE_JSON, NULL, NULL, set);
Michal Vaskod96e2372023-02-24 16:07:51 +01002968}
Michal Vaskoccc02342020-05-21 10:09:21 +02002969
Michal Vaskod96e2372023-02-24 16:07:51 +01002970LIBYANG_API_DEF LY_ERR
2971lyd_find_xpath2(const struct lyd_node *ctx_node, const char *xpath, const struct lyxp_var *vars, struct ly_set **set)
2972{
2973 LY_CHECK_ARG_RET(NULL, ctx_node, xpath, set, LY_EINVAL);
Michal Vaskoccc02342020-05-21 10:09:21 +02002974
Michal Vaskobe1b0cb2024-01-22 14:32:15 +01002975 return lyd_find_xpath3(ctx_node, ctx_node, xpath, LY_VALUE_JSON, NULL, vars, set);
Michal Vaskoccc02342020-05-21 10:09:21 +02002976}
Radek Krejcica989142020-11-05 11:32:22 +01002977
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002978LIBYANG_API_DEF LY_ERR
Michal Vaskobe1b0cb2024-01-22 14:32:15 +01002979lyd_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 +01002980 void *prefix_data, const struct lyxp_var *vars, struct ly_set **set)
Michal Vaskoe3716b32021-12-13 16:58:25 +01002981{
Michal Vaskod96e2372023-02-24 16:07:51 +01002982 LY_CHECK_ARG_RET(NULL, tree, xpath, set, LY_EINVAL);
Michal Vaskoe3716b32021-12-13 16:58:25 +01002983
Michal Vaskod96e2372023-02-24 16:07:51 +01002984 *set = NULL;
2985
2986 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 +01002987}
2988
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002989LIBYANG_API_DEF LY_ERR
Michal Vaskod96e2372023-02-24 16:07:51 +01002990lyd_eval_xpath(const struct lyd_node *ctx_node, const char *xpath, ly_bool *result)
aPiecekfba75362021-10-07 12:39:48 +02002991{
Michal Vaskod96e2372023-02-24 16:07:51 +01002992 return lyd_eval_xpath3(ctx_node, NULL, xpath, LY_VALUE_JSON, NULL, NULL, result);
Michal Vaskoc9eb3ca2021-07-16 10:20:37 +02002993}
2994
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002995LIBYANG_API_DEF LY_ERR
Michal Vasko10fabfc2022-08-09 08:55:43 +02002996lyd_eval_xpath2(const struct lyd_node *ctx_node, const char *xpath, const struct lyxp_var *vars, ly_bool *result)
2997{
2998 return lyd_eval_xpath3(ctx_node, NULL, xpath, LY_VALUE_JSON, NULL, vars, result);
2999}
3000
3001LIBYANG_API_DEF LY_ERR
Michal Vaskod96e2372023-02-24 16:07:51 +01003002lyd_eval_xpath3(const struct lyd_node *ctx_node, const struct lys_module *cur_mod, const char *xpath,
3003 LY_VALUE_FORMAT format, void *prefix_data, const struct lyxp_var *vars, ly_bool *result)
aPiecekfba75362021-10-07 12:39:48 +02003004{
Michal Vaskod96e2372023-02-24 16:07:51 +01003005 return lyd_eval_xpath4(ctx_node, ctx_node, cur_mod, xpath, format, prefix_data, vars, NULL, NULL, NULL, NULL, result);
3006}
3007
3008LIBYANG_API_DEF LY_ERR
3009lyd_eval_xpath4(const struct lyd_node *ctx_node, const struct lyd_node *tree, const struct lys_module *cur_mod,
3010 const char *xpath, LY_VALUE_FORMAT format, void *prefix_data, const struct lyxp_var *vars, LY_XPATH_TYPE *ret_type,
3011 struct ly_set **node_set, char **string, long double *number, ly_bool *boolean)
3012{
3013 LY_ERR ret = LY_SUCCESS;
3014 struct lyxp_set xp_set = {0};
3015 struct lyxp_expr *exp = NULL;
3016 uint32_t i;
3017
3018 LY_CHECK_ARG_RET(NULL, tree, xpath, ((ret_type && node_set && string && number && boolean) ||
3019 (node_set && !string && !number && !boolean) || (!node_set && string && !number && !boolean) ||
3020 (!node_set && !string && number && !boolean) || (!node_set && !string && !number && boolean)), LY_EINVAL);
3021
3022 /* parse expression */
3023 ret = lyxp_expr_parse((struct ly_ctx *)LYD_CTX(tree), xpath, 0, 1, &exp);
3024 LY_CHECK_GOTO(ret, cleanup);
3025
3026 /* evaluate expression */
3027 ret = lyxp_eval(LYD_CTX(tree), exp, cur_mod, format, prefix_data, ctx_node, ctx_node, tree, vars, &xp_set,
3028 LYXP_IGNORE_WHEN);
3029 LY_CHECK_GOTO(ret, cleanup);
3030
3031 /* return expected result type without or with casting */
3032 if (node_set) {
3033 /* node set */
3034 if (xp_set.type == LYXP_SET_NODE_SET) {
3035 /* transform into a set */
3036 LY_CHECK_GOTO(ret = ly_set_new(node_set), cleanup);
3037 (*node_set)->objs = malloc(xp_set.used * sizeof *(*node_set)->objs);
3038 LY_CHECK_ERR_GOTO(!(*node_set)->objs, LOGMEM(LYD_CTX(tree)); ret = LY_EMEM, cleanup);
3039 (*node_set)->size = xp_set.used;
3040 for (i = 0; i < xp_set.used; ++i) {
3041 if (xp_set.val.nodes[i].type == LYXP_NODE_ELEM) {
3042 ret = ly_set_add(*node_set, xp_set.val.nodes[i].node, 1, NULL);
3043 LY_CHECK_GOTO(ret, cleanup);
3044 }
3045 }
3046 if (ret_type) {
3047 *ret_type = LY_XPATH_NODE_SET;
3048 }
3049 } else if (!string && !number && !boolean) {
3050 LOGERR(LYD_CTX(tree), LY_EINVAL, "XPath \"%s\" result is not a node set.", xpath);
3051 ret = LY_EINVAL;
3052 goto cleanup;
3053 }
3054 }
3055
3056 if (string) {
3057 if ((xp_set.type != LYXP_SET_STRING) && !node_set) {
3058 /* cast into string */
3059 LY_CHECK_GOTO(ret = lyxp_set_cast(&xp_set, LYXP_SET_STRING), cleanup);
3060 }
3061 if (xp_set.type == LYXP_SET_STRING) {
3062 /* string */
3063 *string = xp_set.val.str;
3064 xp_set.val.str = NULL;
3065 if (ret_type) {
3066 *ret_type = LY_XPATH_STRING;
3067 }
3068 }
3069 }
3070
3071 if (number) {
3072 if ((xp_set.type != LYXP_SET_NUMBER) && !node_set) {
3073 /* cast into number */
3074 LY_CHECK_GOTO(ret = lyxp_set_cast(&xp_set, LYXP_SET_NUMBER), cleanup);
3075 }
3076 if (xp_set.type == LYXP_SET_NUMBER) {
3077 /* number */
3078 *number = xp_set.val.num;
3079 if (ret_type) {
3080 *ret_type = LY_XPATH_NUMBER;
3081 }
3082 }
3083 }
3084
3085 if (boolean) {
3086 if ((xp_set.type != LYXP_SET_BOOLEAN) && !node_set) {
3087 /* cast into boolean */
3088 LY_CHECK_GOTO(ret = lyxp_set_cast(&xp_set, LYXP_SET_BOOLEAN), cleanup);
3089 }
3090 if (xp_set.type == LYXP_SET_BOOLEAN) {
3091 /* boolean */
3092 *boolean = xp_set.val.bln;
3093 if (ret_type) {
3094 *ret_type = LY_XPATH_BOOLEAN;
3095 }
3096 }
3097 }
3098
3099cleanup:
3100 lyxp_set_free_content(&xp_set);
3101 lyxp_expr_free((struct ly_ctx *)LYD_CTX(tree), exp);
3102 return ret;
aPiecekfba75362021-10-07 12:39:48 +02003103}
3104
Michal Vasko99a77882024-01-04 14:50:51 +01003105/**
3106 * @brief Hash table node equal callback.
3107 */
3108static ly_bool
3109lyd_trim_equal_cb(void *val1_p, void *val2_p, ly_bool UNUSED(mod), void *UNUSED(cb_data))
3110{
3111 struct lyd_node *node1, *node2;
3112
3113 node1 = *(struct lyd_node **)val1_p;
3114 node2 = *(struct lyd_node **)val2_p;
3115
3116 return node1 == node2;
3117}
3118
3119LIBYANG_API_DEF LY_ERR
3120lyd_trim_xpath(struct lyd_node **tree, const char *xpath, const struct lyxp_var *vars)
3121{
3122 LY_ERR ret = LY_SUCCESS;
Michal Vaskoea4943c2024-01-16 15:27:29 +01003123 struct ly_ctx *ctx = NULL;
Michal Vasko99a77882024-01-04 14:50:51 +01003124 struct lyxp_set xp_set = {0};
3125 struct lyxp_expr *exp = NULL;
3126 struct lyd_node *node, *parent;
3127 struct lyxp_set_hash_node hnode;
3128 struct ly_ht *parent_ht = NULL;
3129 struct ly_set free_set = {0};
3130 uint32_t i, hash;
3131 ly_bool is_result;
3132
3133 LY_CHECK_ARG_RET(NULL, tree, xpath, LY_EINVAL);
3134
3135 if (!*tree) {
3136 /* nothing to do */
3137 goto cleanup;
3138 }
3139
3140 *tree = lyd_first_sibling(*tree);
3141 ctx = (struct ly_ctx *)LYD_CTX(*tree);
3142
3143 /* parse expression */
3144 ret = lyxp_expr_parse(ctx, xpath, 0, 1, &exp);
3145 LY_CHECK_GOTO(ret, cleanup);
3146
3147 /* evaluate expression */
3148 ret = lyxp_eval(ctx, exp, NULL, LY_VALUE_JSON, NULL, *tree, *tree, *tree, vars, &xp_set, LYXP_IGNORE_WHEN);
3149 LY_CHECK_GOTO(ret, cleanup);
3150
3151 /* create hash table for all the parents of results */
Michal Vasko0a0716d2024-01-04 15:02:12 +01003152 parent_ht = lyht_new(32, sizeof node, lyd_trim_equal_cb, NULL, 1);
Michal Vasko99a77882024-01-04 14:50:51 +01003153 LY_CHECK_GOTO(!parent_ht, cleanup);
3154
3155 for (i = 0; i < xp_set.used; ++i) {
3156 if (xp_set.val.nodes[i].type != LYXP_NODE_ELEM) {
3157 /* ignore */
3158 continue;
3159 }
3160
3161 for (parent = lyd_parent(xp_set.val.nodes[i].node); parent; parent = lyd_parent(parent)) {
3162 /* add the parent into parent_ht */
3163 ret = lyht_insert(parent_ht, &parent, parent->hash, NULL);
3164 if (ret == LY_EEXIST) {
3165 /* shared parent, we are done */
3166 break;
3167 }
3168 LY_CHECK_GOTO(ret, cleanup);
3169 }
3170 }
3171
3172 hnode.type = LYXP_NODE_ELEM;
3173 LY_LIST_FOR(*tree, parent) {
3174 LYD_TREE_DFS_BEGIN(parent, node) {
3175 if (lysc_is_key(node->schema)) {
3176 /* ignore */
3177 goto next_iter;
3178 }
3179
3180 /* check the results */
3181 is_result = 0;
3182 if (xp_set.ht) {
3183 hnode.node = node;
3184 hash = lyht_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
3185 hash = lyht_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
3186 hash = lyht_hash_multi(hash, NULL, 0);
3187
3188 if (!lyht_find(xp_set.ht, &hnode, hash, NULL)) {
3189 is_result = 1;
3190 }
3191 } else {
3192 /* not enough elements for a hash table */
3193 for (i = 0; i < xp_set.used; ++i) {
3194 if (xp_set.val.nodes[i].type != LYXP_NODE_ELEM) {
3195 /* ignore */
3196 continue;
3197 }
3198
3199 if (xp_set.val.nodes[i].node == node) {
3200 is_result = 1;
3201 break;
3202 }
3203 }
3204 }
3205
3206 if (is_result) {
3207 /* keep the whole subtree if the node is in the results */
3208 LYD_TREE_DFS_continue = 1;
3209 } else if (lyht_find(parent_ht, &node, node->hash, NULL)) {
3210 /* free the whole subtree if the node is not even among the selected parents */
3211 ret = ly_set_add(&free_set, node, 1, NULL);
3212 LY_CHECK_GOTO(ret, cleanup);
3213 LYD_TREE_DFS_continue = 1;
3214 } /* else keep the parent node because a subtree is in the results */
3215
3216next_iter:
3217 LYD_TREE_DFS_END(parent, node);
3218 }
3219 }
3220
3221 /* free */
3222 for (i = 0; i < free_set.count; ++i) {
3223 node = free_set.dnodes[i];
3224 if (*tree == node) {
3225 *tree = (*tree)->next;
3226 }
3227 lyd_free_tree(node);
3228 }
3229
3230cleanup:
3231 lyxp_set_free_content(&xp_set);
3232 lyxp_expr_free(ctx, exp);
3233 lyht_free(parent_ht, NULL);
3234 ly_set_erase(&free_set, NULL);
3235 return ret;
3236}
3237
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01003238LIBYANG_API_DEF LY_ERR
Michal Vasko3e1f6552021-01-14 09:27:55 +01003239lyd_find_path(const struct lyd_node *ctx_node, const char *path, ly_bool output, struct lyd_node **match)
3240{
3241 LY_ERR ret = LY_SUCCESS;
3242 struct lyxp_expr *expr = NULL;
3243 struct ly_path *lypath = NULL;
3244
3245 LY_CHECK_ARG_RET(NULL, ctx_node, ctx_node->schema, path, LY_EINVAL);
3246
3247 /* parse the path */
Michal Vaskoed725d72021-06-23 12:03:45 +02003248 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 +01003249 LY_PATH_PREFIX_FIRST, LY_PATH_PRED_SIMPLE, &expr);
Michal Vasko3e1f6552021-01-14 09:27:55 +01003250 LY_CHECK_GOTO(ret, cleanup);
3251
3252 /* compile the path */
Michal Vaskoed725d72021-06-23 12:03:45 +02003253 ret = ly_path_compile(LYD_CTX(ctx_node), NULL, ctx_node->schema, NULL, expr,
Michal Vasko0884d212021-10-14 09:21:46 +02003254 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 +01003255 LY_CHECK_GOTO(ret, cleanup);
3256
3257 /* evaluate the path */
Michal Vasko838829d2023-10-09 16:06:43 +02003258 ret = ly_path_eval_partial(lypath, ctx_node, NULL, 0, NULL, match);
Michal Vasko3e1f6552021-01-14 09:27:55 +01003259
3260cleanup:
3261 lyxp_expr_free(LYD_CTX(ctx_node), expr);
3262 ly_path_free(LYD_CTX(ctx_node), lypath);
3263 return ret;
3264}
3265
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01003266LIBYANG_API_DEF LY_ERR
Michal Vaskobb22b182021-06-14 08:14:21 +02003267lyd_find_target(const struct ly_path *path, const struct lyd_node *tree, struct lyd_node **match)
3268{
3269 LY_ERR ret;
3270 struct lyd_node *m;
3271
3272 LY_CHECK_ARG_RET(NULL, path, LY_EINVAL);
3273
Michal Vasko90189962023-02-28 12:10:34 +01003274 ret = ly_path_eval(path, tree, NULL, &m);
Michal Vaskobb22b182021-06-14 08:14:21 +02003275 if (ret) {
3276 if (match) {
3277 *match = NULL;
3278 }
3279 return LY_ENOTFOUND;
3280 }
3281
3282 if (match) {
3283 *match = m;
3284 }
3285 return LY_SUCCESS;
3286}
Irfand3b351a2023-09-14 14:52:15 +02003287
stewegf9041a22024-01-18 13:29:12 +01003288LY_ERR
3289lyd_get_or_create_leafref_links_record(const struct lyd_node_term *node, struct lyd_leafref_links_rec **record, ly_bool create)
3290{
3291 struct ly_ht *ht;
3292 uint32_t hash;
Michal Vaskob46061d2024-01-18 13:58:13 +01003293 struct lyd_leafref_links_rec rec = {0};
stewegf9041a22024-01-18 13:29:12 +01003294
3295 assert(node);
3296 assert(record);
3297
Michal Vaskob46061d2024-01-18 13:58:13 +01003298 *record = NULL;
3299
stewegf9041a22024-01-18 13:29:12 +01003300 if (!(ly_ctx_get_options(LYD_CTX(node)) & LY_CTX_LEAFREF_LINKING)) {
stewegf9041a22024-01-18 13:29:12 +01003301 return LY_EDENIED;
3302 }
3303
3304 rec.node = node;
stewegf9041a22024-01-18 13:29:12 +01003305 ht = LYD_CTX(node)->leafref_links_ht;
Michal Vasko67bf5872024-01-18 13:58:25 +01003306 hash = lyht_hash((const char *)&node, sizeof node);
3307
stewegf9041a22024-01-18 13:29:12 +01003308 if (lyht_find(ht, &rec, hash, (void **)record) == LY_ENOTFOUND) {
3309 if (create) {
3310 LY_CHECK_RET(lyht_insert_no_check(ht, &rec, hash, (void **)record));
3311 } else {
stewegf9041a22024-01-18 13:29:12 +01003312 return LY_ENOTFOUND;
3313 }
3314 }
3315
3316 return LY_SUCCESS;
3317}
3318
3319LIBYANG_API_DEF LY_ERR
3320lyd_leafref_get_links(const struct lyd_node_term *node, const struct lyd_leafref_links_rec **record)
3321{
3322 LY_CHECK_ARG_RET(NULL, node, record, LY_EINVAL);
3323
3324 return lyd_get_or_create_leafref_links_record(node, (struct lyd_leafref_links_rec **)record, 0);
3325}
3326
3327LY_ERR
3328lyd_link_leafref_node(const struct lyd_node_term *node, const struct lyd_node_term *leafref_node)
3329{
stewegf9041a22024-01-18 13:29:12 +01003330 const struct lyd_node_term **item = NULL;
3331 struct lyd_leafref_links_rec *rec;
steweg67388952024-01-25 12:14:50 +01003332 LY_ARRAY_COUNT_TYPE u;
stewegf9041a22024-01-18 13:29:12 +01003333
3334 assert(node);
3335 assert(leafref_node);
3336
3337 if (!(ly_ctx_get_options(LYD_CTX(node)) & LY_CTX_LEAFREF_LINKING)) {
3338 return LY_EDENIED;
3339 }
3340
steweg67388952024-01-25 12:14:50 +01003341 /* add leafref node into the list of target node */
stewegf9041a22024-01-18 13:29:12 +01003342 LY_CHECK_RET(lyd_get_or_create_leafref_links_record(node, &rec, 1));
3343 LY_ARRAY_FOR(rec->leafref_nodes, u) {
3344 if (rec->leafref_nodes[u] == leafref_node) {
3345 return LY_SUCCESS;
3346 }
3347 }
3348
3349 LY_ARRAY_NEW_RET(LYD_CTX(node), rec->leafref_nodes, item, LY_EMEM);
3350 *item = leafref_node;
steweg67388952024-01-25 12:14:50 +01003351
3352 /* add target node into the list of leafref node*/
stewegf9041a22024-01-18 13:29:12 +01003353 LY_CHECK_RET(lyd_get_or_create_leafref_links_record(leafref_node, &rec, 1));
steweg67388952024-01-25 12:14:50 +01003354 LY_ARRAY_FOR(rec->target_nodes, u) {
3355 if (rec->target_nodes[u] == node) {
3356 return LY_SUCCESS;
3357 }
3358 }
3359
3360 LY_ARRAY_NEW_RET(LYD_CTX(node), rec->target_nodes, item, LY_EMEM);
3361 *item = node;
3362
stewegf9041a22024-01-18 13:29:12 +01003363 return LY_SUCCESS;
3364}
3365
3366LIBYANG_API_DEF LY_ERR
3367lyd_leafref_link_node_tree(const struct lyd_node *tree)
3368{
3369 const struct lyd_node *sibling, *elem;
steweg67388952024-01-25 12:14:50 +01003370 struct ly_set *targets = NULL;
stewegf9041a22024-01-18 13:29:12 +01003371 char *errmsg;
3372 struct lyd_node_term *leafref_node;
3373 struct lysc_node_leaf *leaf_schema;
3374 struct lysc_type_leafref *lref;
steweg67388952024-01-25 12:14:50 +01003375 LY_ERR ret = LY_SUCCESS;
3376 uint32_t i;
stewegf9041a22024-01-18 13:29:12 +01003377
3378 LY_CHECK_ARG_RET(NULL, tree, LY_EINVAL);
3379
3380 if (!(ly_ctx_get_options(LYD_CTX(tree)) & LY_CTX_LEAFREF_LINKING)) {
3381 return LY_EDENIED;
3382 }
3383
3384 LY_LIST_FOR(tree, sibling) {
3385 LYD_TREE_DFS_BEGIN(sibling, elem) {
steweg0e1e5092024-02-12 09:06:04 +01003386 if (elem->schema && (elem->schema->nodetype & LYD_NODE_TERM)) {
stewegf9041a22024-01-18 13:29:12 +01003387 leafref_node = (struct lyd_node_term *)elem;
3388 leaf_schema = (struct lysc_node_leaf *)elem->schema;
Michal Vaskob46061d2024-01-18 13:58:13 +01003389
stewegf9041a22024-01-18 13:29:12 +01003390 if (leaf_schema->type->basetype == LY_TYPE_LEAFREF) {
3391 lref = (struct lysc_type_leafref *)leaf_schema->type;
steweg67388952024-01-25 12:14:50 +01003392 ly_set_free(targets, NULL);
3393 if (lyplg_type_resolve_leafref(lref, elem, &leafref_node->value, tree, &targets, &errmsg)) {
Michal Vaskob46061d2024-01-18 13:58:13 +01003394 /* leafref target not found */
stewegf9041a22024-01-18 13:29:12 +01003395 free(errmsg);
Michal Vaskob46061d2024-01-18 13:58:13 +01003396 } else {
3397 /* leafref target found, link it */
steweg67388952024-01-25 12:14:50 +01003398 for (i = 0; i < targets->count; ++i) {
3399 if (targets->dnodes[i]->schema->nodetype & LYD_NODE_TERM) {
3400 ret = lyd_link_leafref_node((struct lyd_node_term *)targets->dnodes[i], leafref_node);
3401 LY_CHECK_GOTO(ret, cleanup);
3402 }
3403 }
stewegf9041a22024-01-18 13:29:12 +01003404 }
3405 }
3406 }
3407 LYD_TREE_DFS_END(sibling, elem);
3408 }
3409 }
Michal Vaskob46061d2024-01-18 13:58:13 +01003410
steweg67388952024-01-25 12:14:50 +01003411cleanup:
3412 ly_set_free(targets, NULL);
3413 return ret;
stewegf9041a22024-01-18 13:29:12 +01003414}
3415
3416LY_ERR
3417lyd_unlink_leafref_node(const struct lyd_node_term *node, const struct lyd_node_term *leafref_node)
3418{
3419 LY_ERR ret;
3420 struct lyd_leafref_links_rec *rec;
3421
3422 assert(node);
3423 assert(leafref_node);
3424
3425 if (!(ly_ctx_get_options(LYD_CTX(node)) & LY_CTX_LEAFREF_LINKING)) {
3426 return LY_EDENIED;
3427 }
3428
steweg67388952024-01-25 12:14:50 +01003429 /* remove link from target node to leafref node */
stewegf9041a22024-01-18 13:29:12 +01003430 ret = lyd_get_or_create_leafref_links_record(node, &rec, 0);
3431 if (ret == LY_SUCCESS) {
3432 LY_ARRAY_REMOVE_VALUE(rec->leafref_nodes, leafref_node);
steweg67388952024-01-25 12:14:50 +01003433 if ((LY_ARRAY_COUNT(rec->leafref_nodes) == 0) && (LY_ARRAY_COUNT(rec->target_nodes) == 0)) {
stewegf9041a22024-01-18 13:29:12 +01003434 lyd_free_leafref_nodes(node);
3435 }
3436 } else if (ret != LY_ENOTFOUND) {
3437 return ret;
3438 }
3439
steweg67388952024-01-25 12:14:50 +01003440 /* remove link from leafref node to target node */
stewegf9041a22024-01-18 13:29:12 +01003441 ret = lyd_get_or_create_leafref_links_record(leafref_node, &rec, 0);
3442 if (ret == LY_SUCCESS) {
steweg67388952024-01-25 12:14:50 +01003443 LY_ARRAY_REMOVE_VALUE(rec->target_nodes, node);
3444 if ((LY_ARRAY_COUNT(rec->leafref_nodes) == 0) && (LY_ARRAY_COUNT(rec->target_nodes) == 0)) {
stewegf9041a22024-01-18 13:29:12 +01003445 lyd_free_leafref_nodes(leafref_node);
3446 }
3447 } else if (ret != LY_ENOTFOUND) {
3448 return ret;
3449 }
3450
3451 return LY_SUCCESS;
3452}