blob: 60831e110cfff8b8e54a434fdde43b8c2b25302f [file] [log] [blame]
Radek Krejcie7b95092019-05-15 11:03:07 +02001/**
Michal Vaskob1b5c262020-03-05 14:29:47 +01002 * @file tree_data.c
Radek Krejcie7b95092019-05-15 11:03:07 +02003 * @author Radek Krejci <rkrejci@cesnet.cz>
Michal Vasko6cd9b6b2020-06-22 10:05:22 +02004 * @brief Data tree functions
Radek Krejcie7b95092019-05-15 11:03:07 +02005 *
Michal Vasko6cd9b6b2020-06-22 10:05:22 +02006 * Copyright (c) 2015 - 2020 CESNET, z.s.p.o.
Radek Krejcie7b95092019-05-15 11:03:07 +02007 *
8 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * https://opensource.org/licenses/BSD-3-Clause
13 */
14
Radek Krejci535ea9f2020-05-29 16:01:05 +020015#define _GNU_SOURCE
16
17#include "tree_data.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020018
Radek Krejci084289f2019-07-09 17:35:30 +020019#include <assert.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020020#include <ctype.h>
Radek Krejci47fab892020-11-05 17:02:41 +010021#include <inttypes.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020022#include <stdarg.h>
23#include <stdint.h>
Radek Krejci535ea9f2020-05-29 16:01:05 +020024#include <stdio.h>
25#include <stdlib.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020026#include <string.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020027
Radek Krejci535ea9f2020-05-29 16:01:05 +020028#include "common.h"
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"
Radek Krejci7931b192020-06-25 17:05:03 +020037#include "parser_data.h"
38#include "parser_internal.h"
Michal Vasko004d3152020-06-11 19:59:22 +020039#include "path.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020040#include "plugins_exts.h"
Michal Vasko90932a92020-02-12 14:33:03 +010041#include "plugins_exts_internal.h"
Michal Vasko69730152020-10-09 16:30:07 +020042#include "plugins_exts_metadata.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020043#include "plugins_types.h"
44#include "set.h"
45#include "tree.h"
46#include "tree_data_internal.h"
47#include "tree_schema.h"
48#include "tree_schema_internal.h"
Michal Vaskoa6669ba2020-08-06 16:14:26 +020049#include "validation.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020050#include "xml.h"
51#include "xpath.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020052
Michal Vaskob104f112020-07-17 09:54:54 +020053static LY_ERR lyd_find_sibling_schema(const struct lyd_node *siblings, const struct lysc_node *schema,
Radek Krejci0f969882020-08-21 16:56:47 +020054 struct lyd_node **match);
Michal Vaskob104f112020-07-17 09:54:54 +020055
Radek Krejci084289f2019-07-09 17:35:30 +020056LY_ERR
Michal Vaskofeca4fb2020-10-05 08:58:40 +020057lyd_value_store(const struct ly_ctx *ctx, struct lyd_value *val, const struct lysc_type *type, const char *value,
58 size_t value_len, ly_bool *dynamic, LY_PREFIX_FORMAT format, void *prefix_data, uint32_t hints,
Radek Krejci2efc45b2020-12-22 16:25:44 +010059 const struct lysc_node *ctx_node, ly_bool *incomplete)
Radek Krejci084289f2019-07-09 17:35:30 +020060{
Michal Vaskofeca4fb2020-10-05 08:58:40 +020061 LY_ERR ret;
Radek Krejci084289f2019-07-09 17:35:30 +020062 struct ly_err_item *err = NULL;
Michal Vasko25d6ad02020-10-22 12:20:22 +020063 uint32_t options = (dynamic && *dynamic ? LY_TYPE_STORE_DYNAMIC : 0);
Radek Krejci084289f2019-07-09 17:35:30 +020064
Michal Vaskofeca4fb2020-10-05 08:58:40 +020065 if (incomplete) {
66 *incomplete = 0;
Radek Krejci084289f2019-07-09 17:35:30 +020067 }
68
Michal Vasko405cc9e2020-12-01 12:01:27 +010069 ret = type->plugin->store(ctx, type, value, value_len, options, format, prefix_data, hints, ctx_node, val, NULL, &err);
Michal Vasko90932a92020-02-12 14:33:03 +010070 if (ret == LY_EINCOMPLETE) {
Michal Vaskofeca4fb2020-10-05 08:58:40 +020071 if (incomplete) {
72 *incomplete = 1;
73 }
74 } else if (ret) {
75 if (err) {
Radek Krejci2efc45b2020-12-22 16:25:44 +010076 LOGVAL(ctx, err->vecode, err->msg);
Michal Vaskofeca4fb2020-10-05 08:58:40 +020077 ly_err_free(err);
78 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +010079 LOGVAL(ctx, LYVE_OTHER, "Storing value \"%.*s\" failed.", (int)value_len, value);
Michal Vaskofeca4fb2020-10-05 08:58:40 +020080 }
81 return ret;
Michal Vasko90932a92020-02-12 14:33:03 +010082 }
83
Michal Vaskofeca4fb2020-10-05 08:58:40 +020084 if (dynamic) {
85 *dynamic = 0;
86 }
87 return LY_SUCCESS;
Michal Vasko90932a92020-02-12 14:33:03 +010088}
89
Radek Krejci38d85362019-09-05 16:26:38 +020090LY_ERR
Michal Vaskofeca4fb2020-10-05 08:58:40 +020091lyd_value_validate_incomplete(const struct ly_ctx *ctx, const struct lysc_type *type, struct lyd_value *val,
Radek Krejci2efc45b2020-12-22 16:25:44 +010092 const struct lyd_node *ctx_node, const struct lyd_node *tree)
Radek Krejci38d85362019-09-05 16:26:38 +020093{
Michal Vaskofeca4fb2020-10-05 08:58:40 +020094 LY_ERR ret;
Radek Krejci38d85362019-09-05 16:26:38 +020095 struct ly_err_item *err = NULL;
Radek Krejci38d85362019-09-05 16:26:38 +020096
Michal Vaskofeca4fb2020-10-05 08:58:40 +020097 assert(type->plugin->validate);
Michal Vasko8d544252020-03-02 10:19:52 +010098
Michal Vaskofeca4fb2020-10-05 08:58:40 +020099 ret = type->plugin->validate(ctx, type, ctx_node, tree, val, &err);
100 if (ret) {
Radek Krejci38d85362019-09-05 16:26:38 +0200101 if (err) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100102 LOGVAL(ctx, err->vecode, err->msg);
Radek Krejci38d85362019-09-05 16:26:38 +0200103 ly_err_free(err);
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200104 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100105 LOGVAL(ctx, LYVE_OTHER, "Resolving value \"%s\" failed.", val->canonical);
Radek Krejci38d85362019-09-05 16:26:38 +0200106 }
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200107 return ret;
Radek Krejci38d85362019-09-05 16:26:38 +0200108 }
109
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200110 return LY_SUCCESS;
Radek Krejci38d85362019-09-05 16:26:38 +0200111}
112
Michal Vaskof937cfe2020-08-03 16:07:12 +0200113LY_ERR
114_lys_value_validate(const struct ly_ctx *ctx, const struct lysc_node *node, const char *value, size_t value_len,
Radek Krejci0f969882020-08-21 16:56:47 +0200115 LY_PREFIX_FORMAT format, void *prefix_data)
Radek Krejci084289f2019-07-09 17:35:30 +0200116{
117 LY_ERR rc = LY_SUCCESS;
118 struct ly_err_item *err = NULL;
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200119 struct lyd_value storage;
Radek Krejci084289f2019-07-09 17:35:30 +0200120 struct lysc_type *type;
Radek Krejci084289f2019-07-09 17:35:30 +0200121
122 LY_CHECK_ARG_RET(ctx, node, value, LY_EINVAL);
123
124 if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
125 LOGARG(ctx, node);
126 return LY_EINVAL;
127 }
128
Michal Vasko22df3f02020-08-24 13:29:22 +0200129 type = ((struct lysc_node_leaf *)node)->type;
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200130 rc = type->plugin->store(ctx ? ctx : node->module->ctx, type, value, value_len, 0, format, prefix_data,
Michal Vasko405cc9e2020-12-01 12:01:27 +0100131 LYD_HINT_SCHEMA, node, &storage, NULL, &err);
Radek Krejci73dead22019-07-11 16:46:16 +0200132 if (rc == LY_EINCOMPLETE) {
133 /* actually success since we do not provide the context tree and call validation with
134 * LY_TYPE_OPTS_INCOMPLETE_DATA */
135 rc = LY_SUCCESS;
136 } else if (rc && err) {
137 if (ctx) {
138 /* log only in case the ctx was provided as input parameter */
Radek Krejciddace2c2021-01-08 11:30:56 +0100139 LOG_LOCSET(NULL, NULL, err->path, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100140 LOGVAL(ctx, err->vecode, err->msg);
Radek Krejciddace2c2021-01-08 11:30:56 +0100141 LOG_LOCBACK(0, 0, 1, 0);
Radek Krejci084289f2019-07-09 17:35:30 +0200142 }
Radek Krejci73dead22019-07-11 16:46:16 +0200143 ly_err_free(err);
Radek Krejci084289f2019-07-09 17:35:30 +0200144 }
145
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200146 if (!rc) {
147 type->plugin->free(ctx ? ctx : node->module->ctx, &storage);
148 }
Radek Krejci084289f2019-07-09 17:35:30 +0200149 return rc;
150}
151
152API LY_ERR
Michal Vaskof937cfe2020-08-03 16:07:12 +0200153lys_value_validate(const struct ly_ctx *ctx, const struct lysc_node *node, const char *value, size_t value_len)
154{
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200155 return _lys_value_validate(ctx, node, value, value_len, LY_PREF_JSON, NULL);
Michal Vaskof937cfe2020-08-03 16:07:12 +0200156}
157
158API LY_ERR
Michal Vasko44685da2020-03-17 15:38:06 +0100159lyd_value_validate(const struct ly_ctx *ctx, const struct lyd_node_term *node, const char *value, size_t value_len,
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200160 const struct lyd_node *tree, const struct lysc_type **realtype)
Radek Krejci084289f2019-07-09 17:35:30 +0200161{
162 LY_ERR rc;
163 struct ly_err_item *err = NULL;
164 struct lysc_type *type;
Michal Vasko3701af52020-08-03 14:29:38 +0200165 struct lyd_value val = {0};
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200166 ly_bool stored = 0;
Radek Krejci084289f2019-07-09 17:35:30 +0200167
168 LY_CHECK_ARG_RET(ctx, node, value, LY_EINVAL);
169
Michal Vasko22df3f02020-08-24 13:29:22 +0200170 type = ((struct lysc_node_leaf *)node->schema)->type;
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200171 /* store */
172 rc = type->plugin->store(ctx ? ctx : LYD_CTX(node), type, value, value_len, 0, LY_PREF_JSON, NULL,
Michal Vasko405cc9e2020-12-01 12:01:27 +0100173 LYD_HINT_DATA, node->schema, &val, NULL, &err);
Radek Krejci73dead22019-07-11 16:46:16 +0200174 if (rc == LY_EINCOMPLETE) {
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200175 stored = 1;
176
177 /* resolve */
Michal Vasko9e685082021-01-29 14:49:09 +0100178 rc = type->plugin->validate(ctx ? ctx : LYD_CTX(node), type, &node->node, tree, &val, &err);
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200179 }
180
181 if (rc) {
Radek Krejci73dead22019-07-11 16:46:16 +0200182 if (err) {
183 if (ctx) {
Michal Vasko9e685082021-01-29 14:49:09 +0100184 LOG_LOCSET(NULL, &node->node, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100185 LOGVAL(ctx, err->vecode, err->msg);
Radek Krejciddace2c2021-01-08 11:30:56 +0100186 LOG_LOCBACK(0, 1, 0, 0);
Radek Krejci084289f2019-07-09 17:35:30 +0200187 }
Radek Krejci73dead22019-07-11 16:46:16 +0200188 ly_err_free(err);
Radek Krejci084289f2019-07-09 17:35:30 +0200189 }
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200190 if (stored) {
191 type->plugin->free(ctx ? ctx : LYD_CTX(node), &val);
192 }
Radek Krejci73dead22019-07-11 16:46:16 +0200193 return rc;
Radek Krejci084289f2019-07-09 17:35:30 +0200194 }
195
Michal Vasko3701af52020-08-03 14:29:38 +0200196 if (realtype) {
Michal Vasko29134f82020-11-13 18:03:20 +0100197 if (val.realtype->basetype == LY_TYPE_UNION) {
198 *realtype = val.subvalue->value.realtype;
199 } else {
200 *realtype = val.realtype;
201 }
Michal Vasko3701af52020-08-03 14:29:38 +0200202 }
203
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200204 type->plugin->free(ctx ? ctx : LYD_CTX(node), &val);
Radek Krejci084289f2019-07-09 17:35:30 +0200205 return LY_SUCCESS;
206}
207
208API LY_ERR
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200209lyd_value_compare(const struct lyd_node_term *node, const char *value, size_t value_len)
Radek Krejci084289f2019-07-09 17:35:30 +0200210{
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200211 LY_ERR ret = LY_SUCCESS;
Radek Krejci084289f2019-07-09 17:35:30 +0200212 struct ly_ctx *ctx;
213 struct lysc_type *type;
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200214 struct lyd_value val = {0};
Radek Krejci084289f2019-07-09 17:35:30 +0200215
216 LY_CHECK_ARG_RET(node ? node->schema->module->ctx : NULL, node, value, LY_EINVAL);
217
218 ctx = node->schema->module->ctx;
Michal Vasko22df3f02020-08-24 13:29:22 +0200219 type = ((struct lysc_node_leaf *)node->schema)->type;
Radek Krejci084289f2019-07-09 17:35:30 +0200220
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200221 /* store the value */
Michal Vasko9e685082021-01-29 14:49:09 +0100222 LOG_LOCSET(node->schema, &node->node, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100223 ret = lyd_value_store(ctx, &val, type, value, value_len, NULL, LY_PREF_JSON, NULL, LYD_HINT_DATA, node->schema, NULL);
Radek Krejciddace2c2021-01-08 11:30:56 +0100224 LOG_LOCBACK(1, 1, 0, 0);
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200225 LY_CHECK_RET(ret);
Radek Krejci084289f2019-07-09 17:35:30 +0200226
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200227 /* compare values */
228 ret = type->plugin->compare(&node->value, &val);
Radek Krejci084289f2019-07-09 17:35:30 +0200229
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200230 type->plugin->free(ctx, &val);
Radek Krejci084289f2019-07-09 17:35:30 +0200231 return ret;
232}
233
Radek Krejci19611252020-10-04 13:54:53 +0200234API ly_bool
235lyd_is_default(const struct lyd_node *node)
236{
237 const struct lysc_node_leaf *leaf;
238 const struct lysc_node_leaflist *llist;
239 const struct lyd_node_term *term;
240 LY_ARRAY_COUNT_TYPE u;
241
242 assert(node->schema->nodetype & LYD_NODE_TERM);
243 term = (const struct lyd_node_term *)node;
244
245 if (node->schema->nodetype == LYS_LEAF) {
246 leaf = (const struct lysc_node_leaf *)node->schema;
247 if (!leaf->dflt) {
248 return 0;
249 }
250
251 /* compare with the default value */
252 if (leaf->type->plugin->compare(&term->value, leaf->dflt)) {
253 return 0;
254 }
255 } else {
256 llist = (const struct lysc_node_leaflist *)node->schema;
257 if (!llist->dflts) {
258 return 0;
259 }
260
261 LY_ARRAY_FOR(llist->dflts, u) {
262 /* compare with each possible default value */
263 if (llist->type->plugin->compare(&term->value, llist->dflts[u])) {
264 return 0;
265 }
266 }
267 }
268
269 return 1;
270}
271
Radek Krejci7931b192020-06-25 17:05:03 +0200272static LYD_FORMAT
Michal Vasko63f3d842020-07-08 10:10:14 +0200273lyd_parse_get_format(const struct ly_in *in, LYD_FORMAT format)
Radek Krejcie7b95092019-05-15 11:03:07 +0200274{
Michal Vasko69730152020-10-09 16:30:07 +0200275 if (!format && (in->type == LY_IN_FILEPATH)) {
Radek Krejcie7b95092019-05-15 11:03:07 +0200276 /* unknown format - try to detect it from filename's suffix */
Radek Krejci7931b192020-06-25 17:05:03 +0200277 const char *path = in->method.fpath.filepath;
278 size_t len = strlen(path);
Radek Krejcie7b95092019-05-15 11:03:07 +0200279
280 /* ignore trailing whitespaces */
Michal Vaskod989ba02020-08-24 10:59:24 +0200281 for ( ; len > 0 && isspace(path[len - 1]); len--) {}
Radek Krejcie7b95092019-05-15 11:03:07 +0200282
Radek Krejcif13b87b2020-12-01 22:02:17 +0100283 if ((len >= LY_XML_SUFFIX_LEN + 1) &&
284 !strncmp(&path[len - LY_XML_SUFFIX_LEN], LY_XML_SUFFIX, LY_XML_SUFFIX_LEN)) {
Radek Krejcie7b95092019-05-15 11:03:07 +0200285 format = LYD_XML;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100286 } else if ((len >= LY_JSON_SUFFIX_LEN + 1) &&
287 !strncmp(&path[len - LY_JSON_SUFFIX_LEN], LY_JSON_SUFFIX, LY_JSON_SUFFIX_LEN)) {
Radek Krejcie7b95092019-05-15 11:03:07 +0200288 format = LYD_JSON;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100289 } else if ((len >= LY_LYB_SUFFIX_LEN + 1) &&
290 !strncmp(&path[len - LY_LYB_SUFFIX_LEN], LY_LYB_SUFFIX, LY_LYB_SUFFIX_LEN)) {
Radek Krejcie7b95092019-05-15 11:03:07 +0200291 format = LYD_LYB;
Radek Krejci7931b192020-06-25 17:05:03 +0200292 } /* else still unknown */
Radek Krejcie7b95092019-05-15 11:03:07 +0200293 }
294
Radek Krejci7931b192020-06-25 17:05:03 +0200295 return format;
296}
Radek Krejcie7b95092019-05-15 11:03:07 +0200297
Michal Vaskoe0665742021-02-11 11:08:44 +0100298/**
299 * @brief Parse YANG data into a data tree.
300 *
301 * @param[in] ctx libyang context.
Radek Krejcif16e2542021-02-17 15:39:23 +0100302 * @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 +0100303 * @param[in] parent Parent to connect the parsed nodes to, if any.
304 * @param[in,out] first_p Pointer to the first top-level parsed node, used only if @p parent is NULL.
305 * @param[in] in Input handle to read the input from.
306 * @param[in] format Expected format of the data in @p in.
307 * @param[in] parse_opts Options for parser.
308 * @param[in] val_opts Options for validation.
309 * @param[out] op Optional pointer to the parsed operation, if any.
310 * @return LY_ERR value.
311 */
312static LY_ERR
Radek Krejcif16e2542021-02-17 15:39:23 +0100313lyd_parse(const struct ly_ctx *ctx, const struct lysc_ext_instance *ext, struct lyd_node *parent, struct lyd_node **first_p,
314 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 +0200315{
Michal Vaskoe0665742021-02-11 11:08:44 +0100316 LY_ERR rc = LY_SUCCESS;
Radek Krejci1798aae2020-07-14 13:26:06 +0200317 struct lyd_ctx *lydctx = NULL;
Michal Vaskoe0665742021-02-11 11:08:44 +0100318 struct ly_set parsed = {0};
319 struct lyd_node *first;
320 uint32_t i;
Radek Krejci1798aae2020-07-14 13:26:06 +0200321
Michal Vaskoe0665742021-02-11 11:08:44 +0100322 assert(ctx && (parent || first_p));
Radek Krejci7931b192020-06-25 17:05:03 +0200323
324 format = lyd_parse_get_format(in, format);
Michal Vaskoe0665742021-02-11 11:08:44 +0100325 if (first_p) {
326 *first_p = NULL;
327 }
Radek Krejci1798aae2020-07-14 13:26:06 +0200328
Michal Vasko63f3d842020-07-08 10:10:14 +0200329 /* remember input position */
330 in->func_start = in->current;
Radek Krejci7931b192020-06-25 17:05:03 +0200331
Michal Vaskoe0665742021-02-11 11:08:44 +0100332 /* parse the data */
Radek Krejci7931b192020-06-25 17:05:03 +0200333 switch (format) {
334 case LYD_XML:
Radek Krejcif16e2542021-02-17 15:39:23 +0100335 rc = lyd_parse_xml(ctx, ext, parent, first_p, in, parse_opts, val_opts, LYD_TYPE_DATA_YANG, NULL, &parsed, &lydctx);
Radek Krejci1798aae2020-07-14 13:26:06 +0200336 break;
Radek Krejci7931b192020-06-25 17:05:03 +0200337 case LYD_JSON:
Radek Krejcif16e2542021-02-17 15:39:23 +0100338 rc = lyd_parse_json(ctx, ext, parent, first_p, in, parse_opts, val_opts, LYD_TYPE_DATA_YANG, &parsed, &lydctx);
Radek Krejci1798aae2020-07-14 13:26:06 +0200339 break;
Radek Krejci7931b192020-06-25 17:05:03 +0200340 case LYD_LYB:
Radek Krejcif16e2542021-02-17 15:39:23 +0100341 rc = lyd_parse_lyb(ctx, ext, parent, first_p, in, parse_opts, val_opts, LYD_TYPE_DATA_YANG, &parsed, &lydctx);
Radek Krejci1798aae2020-07-14 13:26:06 +0200342 break;
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200343 case LYD_UNKNOWN:
Michal Vaskod74978f2021-02-12 11:59:36 +0100344 LOGARG(ctx, format);
345 rc = LY_EINVAL;
Michal Vaskoe0665742021-02-11 11:08:44 +0100346 break;
347 }
348 LY_CHECK_GOTO(rc, cleanup);
349
350 if (parent) {
351 /* get first top-level sibling */
352 for (first = parent; first->parent; first = lyd_parent(first)) {}
353 first = lyd_first_sibling(first);
354 first_p = &first;
Radek Krejci7931b192020-06-25 17:05:03 +0200355 }
356
Michal Vaskoe0665742021-02-11 11:08:44 +0100357 if (!(parse_opts & LYD_PARSE_ONLY)) {
358 /* validate data */
359 rc = lyd_validate(first_p, NULL, ctx, val_opts, 0, &lydctx->node_when, &lydctx->node_types,
360 &lydctx->meta_types, NULL);
361 LY_CHECK_GOTO(rc, cleanup);
362 }
Radek Krejci7931b192020-06-25 17:05:03 +0200363
Michal Vaskoe0665742021-02-11 11:08:44 +0100364 /* set the operation node */
365 if (op) {
366 *op = lydctx->op_node;
Radek Krejci1798aae2020-07-14 13:26:06 +0200367 }
368
369cleanup:
Michal Vaskoe0665742021-02-11 11:08:44 +0100370 if (lydctx) {
371 lydctx->free(lydctx);
Radek Krejci1798aae2020-07-14 13:26:06 +0200372 }
Michal Vaskoe0665742021-02-11 11:08:44 +0100373 if (rc) {
374 if (parent) {
375 /* free all the parsed subtrees */
376 for (i = 0; i < parsed.count; ++i) {
377 lyd_free_tree(parsed.dnodes[i]);
378 }
379 } else {
380 /* free everything */
381 lyd_free_all(*first_p);
382 *first_p = NULL;
383 }
384 }
385 ly_set_erase(&parsed, NULL);
386 return rc;
Radek Krejci7931b192020-06-25 17:05:03 +0200387}
388
389API LY_ERR
Radek Krejcif16e2542021-02-17 15:39:23 +0100390lyd_parse_ext_data(const struct lysc_ext_instance *ext, struct lyd_node *parent, struct ly_in *in, LYD_FORMAT format,
391 uint32_t parse_options, uint32_t validate_options, struct lyd_node **tree)
392{
393 const struct ly_ctx *ctx = ext ? ext->module->ctx : NULL;
394
395 LY_CHECK_ARG_RET(ctx, ext, in, parent || tree, LY_EINVAL);
396 LY_CHECK_ARG_RET(ctx, !(parse_options & ~LYD_PARSE_OPTS_MASK), LY_EINVAL);
397 LY_CHECK_ARG_RET(ctx, !(validate_options & ~LYD_VALIDATE_OPTS_MASK), LY_EINVAL);
398
399 return lyd_parse(ctx, ext, parent, tree, in, format, parse_options, validate_options, NULL);
400}
401
402API LY_ERR
Michal Vaskoe0665742021-02-11 11:08:44 +0100403lyd_parse_data(const struct ly_ctx *ctx, struct lyd_node *parent, struct ly_in *in, LYD_FORMAT format,
404 uint32_t parse_options, uint32_t validate_options, struct lyd_node **tree)
405{
406 LY_CHECK_ARG_RET(ctx, ctx, in, parent || tree, LY_EINVAL);
407 LY_CHECK_ARG_RET(ctx, !(parse_options & ~LYD_PARSE_OPTS_MASK), LY_EINVAL);
408 LY_CHECK_ARG_RET(ctx, !(validate_options & ~LYD_VALIDATE_OPTS_MASK), LY_EINVAL);
409
Radek Krejcif16e2542021-02-17 15:39:23 +0100410 return lyd_parse(ctx, NULL, parent, tree, in, format, parse_options, validate_options, NULL);
Michal Vaskoe0665742021-02-11 11:08:44 +0100411}
412
413API LY_ERR
414lyd_parse_data_mem(const struct ly_ctx *ctx, const char *data, LYD_FORMAT format, uint32_t parse_options,
415 uint32_t validate_options, struct lyd_node **tree)
Radek Krejci7931b192020-06-25 17:05:03 +0200416{
417 LY_ERR ret;
418 struct ly_in *in;
419
420 LY_CHECK_RET(ly_in_new_memory(data, &in));
Michal Vaskoe0665742021-02-11 11:08:44 +0100421 ret = lyd_parse_data(ctx, NULL, in, format, parse_options, validate_options, tree);
Radek Krejci7931b192020-06-25 17:05:03 +0200422
423 ly_in_free(in, 0);
424 return ret;
425}
426
427API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200428lyd_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 +0200429 struct lyd_node **tree)
Radek Krejci7931b192020-06-25 17:05:03 +0200430{
431 LY_ERR ret;
432 struct ly_in *in;
433
434 LY_CHECK_RET(ly_in_new_fd(fd, &in));
Michal Vaskoe0665742021-02-11 11:08:44 +0100435 ret = lyd_parse_data(ctx, NULL, in, format, parse_options, validate_options, tree);
Radek Krejci7931b192020-06-25 17:05:03 +0200436
437 ly_in_free(in, 0);
438 return ret;
439}
440
441API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200442lyd_parse_data_path(const struct ly_ctx *ctx, const char *path, LYD_FORMAT format, uint32_t parse_options,
443 uint32_t validate_options, struct lyd_node **tree)
Radek Krejci7931b192020-06-25 17:05:03 +0200444{
445 LY_ERR ret;
446 struct ly_in *in;
447
448 LY_CHECK_RET(ly_in_new_filepath(path, 0, &in));
Michal Vaskoe0665742021-02-11 11:08:44 +0100449 ret = lyd_parse_data(ctx, NULL, in, format, parse_options, validate_options, tree);
Radek Krejci7931b192020-06-25 17:05:03 +0200450
451 ly_in_free(in, 0);
452 return ret;
453}
454
Radek Krejcif16e2542021-02-17 15:39:23 +0100455/**
456 * @brief Parse YANG data into an operation data tree, in case the extension instance is specified, keep the searching
457 * for schema nodes locked inside the extension instance.
458 *
459 * At least one of @p parent, @p tree, or @p op must always be set.
460 *
461 * Specific @p data_type values have different parameter meaning as follows:
462 * - ::LYD_TYPE_RPC_NETCONF:
463 * - @p parent - must be NULL, the whole RPC is expected;
464 * - @p format - must be ::LYD_XML, NETCONF supports only this format;
465 * - @p tree - must be provided, all the NETCONF-specific XML envelopes will be returned here as
466 * a separate opaque data tree, even if the function fails, this may be returned;
467 * - @p op - must be provided, the RPC/action data tree itself will be returned here, pointing to the operation;
468 *
469 * - ::LYD_TYPE_NOTIF_NETCONF:
470 * - @p parent - must be NULL, the whole notification is expected;
471 * - @p format - must be ::LYD_XML, NETCONF supports only this format;
472 * - @p tree - must be provided, all the NETCONF-specific XML envelopes will be returned here as
473 * a separate opaque data tree, even if the function fails, this may be returned;
474 * - @p op - must be provided, the notification data tree itself will be returned here, pointing to the operation;
475 *
476 * - ::LYD_TYPE_REPLY_NETCONF:
477 * - @p parent - must be set, pointing to the invoked RPC operation (RPC or action) node;
478 * - @p format - must be ::LYD_XML, NETCONF supports only this format;
479 * - @p tree - must be provided, all the NETCONF-specific XML envelopes will be returned here as
480 * a separate opaque data tree, even if the function fails, this may be returned;
481 * - @p op - must be NULL, the reply is appended to the RPC;
482 * Note that there are 3 kinds of NETCONF replies - ok, error, and data. Only data reply appends any nodes to the RPC.
483 *
484 * @param[in] ctx libyang context.
485 * @param[in] ext Extension instance providing the specific schema tree to match with the data being parsed.
486 * @param[in] parent Optional parent to connect the parsed nodes to.
487 * @param[in] in Input handle to read the input from.
488 * @param[in] format Expected format of the data in @p in.
489 * @param[in] data_type Expected operation to parse (@ref datatype).
490 * @param[out] tree Optional full parsed data tree. If @p parent is set, set to NULL.
491 * @param[out] op Optional parsed operation node.
492 * @return LY_ERR value.
493 * @return LY_ENOT if @p data_type is a NETCONF message and the root XML element is not the expected one.
494 */
495static LY_ERR
496lyd_parse_op_(const struct ly_ctx *ctx, const struct lysc_ext_instance *ext, struct lyd_node *parent,
497 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 +0200498{
Michal Vaskoe0665742021-02-11 11:08:44 +0100499 LY_ERR rc = LY_SUCCESS;
500 struct lyd_ctx *lydctx = NULL;
501 struct ly_set parsed = {0};
502 struct lyd_node *first = NULL, *envp = NULL;
503 uint32_t i, parse_opts, val_opts;
Radek Krejci7931b192020-06-25 17:05:03 +0200504
Michal Vasko27fb0262021-02-23 09:42:01 +0100505 if (!ctx) {
506 ctx = LYD_CTX(parent);
507 }
Radek Krejci1798aae2020-07-14 13:26:06 +0200508 if (tree) {
509 *tree = NULL;
510 }
511 if (op) {
512 *op = NULL;
513 }
514
Radek Krejci7931b192020-06-25 17:05:03 +0200515 format = lyd_parse_get_format(in, format);
Radek Krejci7931b192020-06-25 17:05:03 +0200516
Michal Vasko63f3d842020-07-08 10:10:14 +0200517 /* remember input position */
518 in->func_start = in->current;
519
Michal Vaskoe0665742021-02-11 11:08:44 +0100520 /* check params based on the data type */
Michal Vasko1e4c68e2021-02-18 15:03:01 +0100521 if ((data_type == LYD_TYPE_RPC_NETCONF) || (data_type == LYD_TYPE_NOTIF_NETCONF)) {
Michal Vaskoe0665742021-02-11 11:08:44 +0100522 LY_CHECK_ARG_RET(ctx, format == LYD_XML, !parent, tree, op, LY_EINVAL);
Michal Vasko1e4c68e2021-02-18 15:03:01 +0100523 } else if (data_type == LYD_TYPE_REPLY_NETCONF) {
524 LY_CHECK_ARG_RET(ctx, format == LYD_XML, parent, parent->schema->nodetype & (LYS_RPC | LYS_ACTION), tree, !op,
525 LY_EINVAL);
Michal Vaskoe0665742021-02-11 11:08:44 +0100526 }
527 parse_opts = LYD_PARSE_ONLY | LYD_PARSE_STRICT;
528 val_opts = 0;
529
530 /* parse the data */
Radek Krejci7931b192020-06-25 17:05:03 +0200531 switch (format) {
532 case LYD_XML:
Radek Krejcif16e2542021-02-17 15:39:23 +0100533 rc = lyd_parse_xml(ctx, ext, parent, &first, in, parse_opts, val_opts, data_type, &envp, &parsed, &lydctx);
Michal Vasko299d5d12021-02-16 16:36:37 +0100534 if (rc && envp) {
535 /* special situation when the envelopes were parsed successfully */
536 if (tree) {
537 *tree = envp;
538 }
539 ly_set_erase(&parsed, NULL);
540 return rc;
541 }
Michal Vaskoe0665742021-02-11 11:08:44 +0100542 break;
Radek Krejci7931b192020-06-25 17:05:03 +0200543 case LYD_JSON:
Radek Krejcif16e2542021-02-17 15:39:23 +0100544 rc = lyd_parse_json(ctx, ext, parent, &first, in, parse_opts, val_opts, data_type, &parsed, &lydctx);
Michal Vaskoe0665742021-02-11 11:08:44 +0100545 break;
Radek Krejci7931b192020-06-25 17:05:03 +0200546 case LYD_LYB:
Radek Krejcif16e2542021-02-17 15:39:23 +0100547 rc = lyd_parse_lyb(ctx, ext, parent, &first, in, parse_opts, val_opts, data_type, &parsed, &lydctx);
Michal Vaskoe0665742021-02-11 11:08:44 +0100548 break;
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200549 case LYD_UNKNOWN:
Michal Vaskod74978f2021-02-12 11:59:36 +0100550 LOGARG(ctx, format);
551 rc = LY_EINVAL;
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200552 break;
Radek Krejci7931b192020-06-25 17:05:03 +0200553 }
Michal Vaskoe0665742021-02-11 11:08:44 +0100554 LY_CHECK_GOTO(rc, cleanup);
Radek Krejci7931b192020-06-25 17:05:03 +0200555
Michal Vaskoe0665742021-02-11 11:08:44 +0100556 /* set out params correctly */
557 if (tree) {
558 if (envp) {
559 /* special out param meaning */
560 *tree = envp;
561 } else {
562 *tree = parent ? NULL : first;
563 }
564 }
565 if (op) {
566 *op = lydctx->op_node;
567 }
568
569cleanup:
570 if (lydctx) {
571 lydctx->free(lydctx);
572 }
573 if (rc) {
574 if (parent) {
575 /* free all the parsed subtrees */
576 for (i = 0; i < parsed.count; ++i) {
577 lyd_free_tree(parsed.dnodes[i]);
578 }
579 } else {
580 /* free everything (cannot occur in the current code, a safety) */
581 lyd_free_all(first);
582 if (tree) {
583 *tree = NULL;
584 }
585 if (op) {
586 *op = NULL;
587 }
588 }
589 }
590 ly_set_erase(&parsed, NULL);
591 return rc;
Radek Krejcie7b95092019-05-15 11:03:07 +0200592}
Radek Krejci084289f2019-07-09 17:35:30 +0200593
Radek Krejcif16e2542021-02-17 15:39:23 +0100594API LY_ERR
595lyd_parse_op(const struct ly_ctx *ctx, struct lyd_node *parent, struct ly_in *in, LYD_FORMAT format,
596 enum lyd_type data_type, struct lyd_node **tree, struct lyd_node **op)
597{
598 LY_CHECK_ARG_RET(ctx, ctx || parent, in, data_type, parent || tree || op, LY_EINVAL);
599
600 return lyd_parse_op_(ctx, NULL, parent, in, format, data_type, tree, op);
601}
602
603API LY_ERR
604lyd_parse_ext_op(const struct lysc_ext_instance *ext, struct lyd_node *parent, struct ly_in *in, LYD_FORMAT format,
605 enum lyd_type data_type, struct lyd_node **tree, struct lyd_node **op)
606{
607 const struct ly_ctx *ctx = ext ? ext->module->ctx : NULL;
608
609 LY_CHECK_ARG_RET(ctx, ext, in, data_type, parent || tree || op, LY_EINVAL);
610
611 return lyd_parse_op_(ctx, ext, parent, in, format, data_type, tree, op);
612}
613
Michal Vasko90932a92020-02-12 14:33:03 +0100614LY_ERR
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200615lyd_create_term(const struct lysc_node *schema, const char *value, size_t value_len, ly_bool *dynamic,
616 LY_PREFIX_FORMAT format, void *prefix_data, uint32_t hints, ly_bool *incomplete, struct lyd_node **node)
Michal Vasko90932a92020-02-12 14:33:03 +0100617{
618 LY_ERR ret;
619 struct lyd_node_term *term;
620
Michal Vasko9b368d32020-02-14 13:53:31 +0100621 assert(schema->nodetype & LYD_NODE_TERM);
622
Michal Vasko90932a92020-02-12 14:33:03 +0100623 term = calloc(1, sizeof *term);
624 LY_CHECK_ERR_RET(!term, LOGMEM(schema->module->ctx), LY_EMEM);
625
626 term->schema = schema;
Michal Vasko9e685082021-01-29 14:49:09 +0100627 term->prev = &term->node;
Michal Vasko9b368d32020-02-14 13:53:31 +0100628 term->flags = LYD_NEW;
Michal Vasko90932a92020-02-12 14:33:03 +0100629
Radek Krejciddace2c2021-01-08 11:30:56 +0100630 LOG_LOCSET(schema, NULL, NULL, NULL);
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200631 ret = lyd_value_store(schema->module->ctx, &term->value, ((struct lysc_node_leaf *)term->schema)->type, value,
Radek Krejci2efc45b2020-12-22 16:25:44 +0100632 value_len, dynamic, format, prefix_data, hints, schema, incomplete);
Radek Krejciddace2c2021-01-08 11:30:56 +0100633 LOG_LOCBACK(1, 0, 0, 0);
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200634 LY_CHECK_ERR_RET(ret, free(term), ret);
Michal Vasko9e685082021-01-29 14:49:09 +0100635 lyd_hash(&term->node);
Michal Vasko9b368d32020-02-14 13:53:31 +0100636
Michal Vasko9e685082021-01-29 14:49:09 +0100637 *node = &term->node;
Michal Vasko9b368d32020-02-14 13:53:31 +0100638 return ret;
639}
640
641LY_ERR
642lyd_create_term2(const struct lysc_node *schema, const struct lyd_value *val, struct lyd_node **node)
643{
644 LY_ERR ret;
645 struct lyd_node_term *term;
646 struct lysc_type *type;
647
648 assert(schema->nodetype & LYD_NODE_TERM);
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200649 assert(val && val->canonical && val->realtype);
Michal Vasko9b368d32020-02-14 13:53:31 +0100650
651 term = calloc(1, sizeof *term);
652 LY_CHECK_ERR_RET(!term, LOGMEM(schema->module->ctx), LY_EMEM);
653
654 term->schema = schema;
Michal Vasko9e685082021-01-29 14:49:09 +0100655 term->prev = &term->node;
Michal Vasko9b368d32020-02-14 13:53:31 +0100656 term->flags = LYD_NEW;
657
658 type = ((struct lysc_node_leaf *)schema)->type;
659 ret = type->plugin->duplicate(schema->module->ctx, val, &term->value);
660 if (ret) {
661 LOGERR(schema->module->ctx, ret, "Value duplication failed.");
662 free(term);
663 return ret;
664 }
Michal Vasko9e685082021-01-29 14:49:09 +0100665 lyd_hash(&term->node);
Michal Vasko90932a92020-02-12 14:33:03 +0100666
Michal Vasko9e685082021-01-29 14:49:09 +0100667 *node = &term->node;
Michal Vasko90932a92020-02-12 14:33:03 +0100668 return ret;
669}
670
671LY_ERR
672lyd_create_inner(const struct lysc_node *schema, struct lyd_node **node)
673{
674 struct lyd_node_inner *in;
675
Michal Vasko9b368d32020-02-14 13:53:31 +0100676 assert(schema->nodetype & LYD_NODE_INNER);
677
Michal Vasko90932a92020-02-12 14:33:03 +0100678 in = calloc(1, sizeof *in);
679 LY_CHECK_ERR_RET(!in, LOGMEM(schema->module->ctx), LY_EMEM);
680
681 in->schema = schema;
Michal Vasko9e685082021-01-29 14:49:09 +0100682 in->prev = &in->node;
Michal Vasko9b368d32020-02-14 13:53:31 +0100683 in->flags = LYD_NEW;
Michal Vasko3383be72020-11-03 17:15:31 +0100684 if ((schema->nodetype == LYS_CONTAINER) && !(schema->flags & LYS_PRESENCE)) {
685 in->flags |= LYD_DEFAULT;
686 }
Michal Vasko90932a92020-02-12 14:33:03 +0100687
Michal Vasko9b368d32020-02-14 13:53:31 +0100688 /* do not hash list with keys, we need them for the hash */
689 if ((schema->nodetype != LYS_LIST) || (schema->flags & LYS_KEYLESS)) {
Michal Vasko9e685082021-01-29 14:49:09 +0100690 lyd_hash(&in->node);
Michal Vasko9b368d32020-02-14 13:53:31 +0100691 }
Michal Vasko90932a92020-02-12 14:33:03 +0100692
Michal Vasko9e685082021-01-29 14:49:09 +0100693 *node = &in->node;
Michal Vasko90932a92020-02-12 14:33:03 +0100694 return LY_SUCCESS;
695}
696
Michal Vasko90932a92020-02-12 14:33:03 +0100697LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +0200698lyd_create_list(const struct lysc_node *schema, const struct ly_path_predicate *predicates, struct lyd_node **node)
Michal Vasko90932a92020-02-12 14:33:03 +0100699{
700 LY_ERR ret = LY_SUCCESS;
Michal Vasko90932a92020-02-12 14:33:03 +0100701 struct lyd_node *list = NULL, *key;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200702 LY_ARRAY_COUNT_TYPE u;
Michal Vasko90932a92020-02-12 14:33:03 +0100703
Michal Vasko004d3152020-06-11 19:59:22 +0200704 assert((schema->nodetype == LYS_LIST) && !(schema->flags & LYS_KEYLESS));
Michal Vasko90932a92020-02-12 14:33:03 +0100705
706 /* create list */
707 LY_CHECK_GOTO(ret = lyd_create_inner(schema, &list), cleanup);
708
Radek Krejciddace2c2021-01-08 11:30:56 +0100709 LOG_LOCSET(NULL, list, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100710
Michal Vasko90932a92020-02-12 14:33:03 +0100711 /* create and insert all the keys */
Michal Vasko004d3152020-06-11 19:59:22 +0200712 LY_ARRAY_FOR(predicates, u) {
713 LY_CHECK_GOTO(ret = lyd_create_term2(predicates[u].key, &predicates[u].value, &key), cleanup);
Michal Vasko90932a92020-02-12 14:33:03 +0100714 lyd_insert_node(list, NULL, key);
715 }
716
Michal Vasko9b368d32020-02-14 13:53:31 +0100717 /* hash having all the keys */
718 lyd_hash(list);
719
Michal Vasko90932a92020-02-12 14:33:03 +0100720 /* success */
721 *node = list;
722 list = NULL;
723
724cleanup:
Radek Krejciddace2c2021-01-08 11:30:56 +0100725 LOG_LOCBACK(0, 1, 0, 0);
Michal Vasko90932a92020-02-12 14:33:03 +0100726 lyd_free_tree(list);
Michal Vasko004d3152020-06-11 19:59:22 +0200727 return ret;
728}
729
730static LY_ERR
731lyd_create_list2(const struct lysc_node *schema, const char *keys, size_t keys_len, struct lyd_node **node)
732{
733 LY_ERR ret = LY_SUCCESS;
734 struct lyxp_expr *expr = NULL;
735 uint16_t exp_idx = 0;
736 enum ly_path_pred_type pred_type = 0;
737 struct ly_path_predicate *predicates = NULL;
738
Radek Krejciddace2c2021-01-08 11:30:56 +0100739 LOG_LOCSET(schema, NULL, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100740
Michal Vasko004d3152020-06-11 19:59:22 +0200741 /* parse keys */
Michal Vasko6b26e742020-07-17 15:02:10 +0200742 LY_CHECK_GOTO(ret = ly_path_parse_predicate(schema->module->ctx, NULL, keys, keys_len, LY_PATH_PREFIX_OPTIONAL,
Michal Vasko69730152020-10-09 16:30:07 +0200743 LY_PATH_PRED_KEYS, &expr), cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +0200744
745 /* compile them */
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200746 LY_CHECK_GOTO(ret = ly_path_compile_predicate(schema->module->ctx, NULL, NULL, schema, expr, &exp_idx, LY_PREF_JSON,
747 NULL, &predicates, &pred_type), cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +0200748
749 /* create the list node */
750 LY_CHECK_GOTO(ret = lyd_create_list(schema, predicates, node), cleanup);
751
752cleanup:
Radek Krejciddace2c2021-01-08 11:30:56 +0100753 LOG_LOCBACK(1, 0, 0, 0);
Michal Vasko004d3152020-06-11 19:59:22 +0200754 lyxp_expr_free(schema->module->ctx, expr);
Michal Vaskof7e16e22020-10-21 09:24:39 +0200755 ly_path_predicates_free(schema->module->ctx, pred_type, predicates);
Michal Vasko90932a92020-02-12 14:33:03 +0100756 return ret;
757}
758
759LY_ERR
Michal Vasko366a4a12020-12-04 16:23:57 +0100760lyd_create_any(const struct lysc_node *schema, const void *value, LYD_ANYDATA_VALUETYPE value_type, ly_bool use_value,
761 struct lyd_node **node)
Michal Vasko90932a92020-02-12 14:33:03 +0100762{
Michal Vasko366a4a12020-12-04 16:23:57 +0100763 LY_ERR ret;
Michal Vasko90932a92020-02-12 14:33:03 +0100764 struct lyd_node_any *any;
Michal Vasko366a4a12020-12-04 16:23:57 +0100765 union lyd_any_value any_val;
Michal Vasko90932a92020-02-12 14:33:03 +0100766
Michal Vasko9b368d32020-02-14 13:53:31 +0100767 assert(schema->nodetype & LYD_NODE_ANY);
768
Michal Vasko90932a92020-02-12 14:33:03 +0100769 any = calloc(1, sizeof *any);
770 LY_CHECK_ERR_RET(!any, LOGMEM(schema->module->ctx), LY_EMEM);
771
772 any->schema = schema;
Michal Vasko9e685082021-01-29 14:49:09 +0100773 any->prev = &any->node;
Michal Vasko9b368d32020-02-14 13:53:31 +0100774 any->flags = LYD_NEW;
Michal Vasko90932a92020-02-12 14:33:03 +0100775
Radek Krejci1798aae2020-07-14 13:26:06 +0200776 /* TODO: convert XML/JSON strings into a opaq data tree */
Michal Vasko366a4a12020-12-04 16:23:57 +0100777
778 if (use_value) {
779 any->value.str = value;
780 any->value_type = value_type;
781 } else {
782 any_val.str = value;
Michal Vasko9e685082021-01-29 14:49:09 +0100783 ret = lyd_any_copy_value(&any->node, &any_val, value_type);
Michal Vasko366a4a12020-12-04 16:23:57 +0100784 LY_CHECK_ERR_RET(ret, free(any), ret);
785 }
Michal Vasko9e685082021-01-29 14:49:09 +0100786 lyd_hash(&any->node);
Michal Vasko90932a92020-02-12 14:33:03 +0100787
Michal Vasko9e685082021-01-29 14:49:09 +0100788 *node = &any->node;
Michal Vasko90932a92020-02-12 14:33:03 +0100789 return LY_SUCCESS;
790}
791
Michal Vasko52927e22020-03-16 17:26:14 +0100792LY_ERR
Michal Vasko501af032020-11-11 20:27:44 +0100793lyd_create_opaq(const struct ly_ctx *ctx, const char *name, size_t name_len, const char *prefix, size_t pref_len,
794 const char *module_key, size_t module_key_len, const char *value, size_t value_len, ly_bool *dynamic,
795 LY_PREFIX_FORMAT format, void *val_prefix_data, uint32_t hints, struct lyd_node **node)
Michal Vasko52927e22020-03-16 17:26:14 +0100796{
Radek Krejci011e4aa2020-09-04 15:22:31 +0200797 LY_ERR ret = LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +0100798 struct lyd_node_opaq *opaq;
799
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200800 assert(ctx && name && name_len && format);
Michal Vasko52927e22020-03-16 17:26:14 +0100801
802 if (!value_len) {
803 value = "";
804 }
805
806 opaq = calloc(1, sizeof *opaq);
Michal Vasko501af032020-11-11 20:27:44 +0100807 LY_CHECK_ERR_GOTO(!opaq, LOGMEM(ctx); ret = LY_EMEM, finish);
Michal Vasko52927e22020-03-16 17:26:14 +0100808
Michal Vasko9e685082021-01-29 14:49:09 +0100809 opaq->prev = &opaq->node;
Michal Vaskoad92b672020-11-12 13:11:31 +0100810 LY_CHECK_GOTO(ret = lydict_insert(ctx, name, name_len, &opaq->name.name), finish);
Radek Krejci011e4aa2020-09-04 15:22:31 +0200811
Michal Vasko52927e22020-03-16 17:26:14 +0100812 if (pref_len) {
Michal Vaskoad92b672020-11-12 13:11:31 +0100813 LY_CHECK_GOTO(ret = lydict_insert(ctx, prefix, pref_len, &opaq->name.prefix), finish);
Michal Vasko52927e22020-03-16 17:26:14 +0100814 }
Radek Krejci1798aae2020-07-14 13:26:06 +0200815 if (module_key_len) {
Michal Vaskoad92b672020-11-12 13:11:31 +0100816 LY_CHECK_GOTO(ret = lydict_insert(ctx, module_key, module_key_len, &opaq->name.module_ns), finish);
Radek Krejci1798aae2020-07-14 13:26:06 +0200817 }
Michal Vasko52927e22020-03-16 17:26:14 +0100818 if (dynamic && *dynamic) {
Radek Krejci011e4aa2020-09-04 15:22:31 +0200819 LY_CHECK_GOTO(ret = lydict_insert_zc(ctx, (char *)value, &opaq->value), finish);
Michal Vasko52927e22020-03-16 17:26:14 +0100820 *dynamic = 0;
821 } else {
Radek Krejci011e4aa2020-09-04 15:22:31 +0200822 LY_CHECK_GOTO(ret = lydict_insert(ctx, value, value_len, &opaq->value), finish);
Michal Vasko52927e22020-03-16 17:26:14 +0100823 }
Michal Vasko501af032020-11-11 20:27:44 +0100824
825 opaq->format = format;
826 opaq->val_prefix_data = val_prefix_data;
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200827 opaq->hints = hints;
Michal Vasko52927e22020-03-16 17:26:14 +0100828 opaq->ctx = ctx;
829
Radek Krejci011e4aa2020-09-04 15:22:31 +0200830finish:
831 if (ret) {
Michal Vasko9e685082021-01-29 14:49:09 +0100832 lyd_free_tree(&opaq->node);
Michal Vasko501af032020-11-11 20:27:44 +0100833 ly_free_prefix_data(format, val_prefix_data);
Radek Krejci011e4aa2020-09-04 15:22:31 +0200834 } else {
Michal Vasko9e685082021-01-29 14:49:09 +0100835 *node = &opaq->node;
Radek Krejci011e4aa2020-09-04 15:22:31 +0200836 }
837 return ret;
Michal Vasko52927e22020-03-16 17:26:14 +0100838}
839
Michal Vasko3a41dff2020-07-15 14:30:28 +0200840API LY_ERR
Michal Vasko65243892020-12-04 16:26:21 +0100841lyd_new_inner(struct lyd_node *parent, const struct lys_module *module, const char *name, ly_bool output,
842 struct lyd_node **node)
Michal Vasko013a8182020-03-03 10:46:53 +0100843{
844 struct lyd_node *ret = NULL;
845 const struct lysc_node *schema;
846 struct ly_ctx *ctx = parent ? parent->schema->module->ctx : (module ? module->ctx : NULL);
847
Michal Vasko6027eb92020-07-15 16:37:30 +0200848 LY_CHECK_ARG_RET(ctx, parent || module, parent || node, name, LY_EINVAL);
Michal Vasko013a8182020-03-03 10:46:53 +0100849
Michal Vaskof03ed032020-03-04 13:31:44 +0100850 if (!module) {
851 module = parent->schema->module;
852 }
853
Michal Vasko3a41dff2020-07-15 14:30:28 +0200854 schema = lys_find_child(parent ? parent->schema : NULL, module, name, 0,
Radek Krejci41ac9942020-11-02 14:47:56 +0100855 LYS_CONTAINER | LYS_NOTIF | LYS_RPC | LYS_ACTION, output ? LYS_GETNEXT_OUTPUT : 0);
Michal Vasko3a41dff2020-07-15 14:30:28 +0200856 LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "Inner node (and not a list) \"%s\" not found.", name), LY_ENOTFOUND);
Michal Vasko013a8182020-03-03 10:46:53 +0100857
Michal Vasko3a41dff2020-07-15 14:30:28 +0200858 LY_CHECK_RET(lyd_create_inner(schema, &ret));
859 if (parent) {
Michal Vasko013a8182020-03-03 10:46:53 +0100860 lyd_insert_node(parent, NULL, ret);
861 }
Michal Vasko3a41dff2020-07-15 14:30:28 +0200862
863 if (node) {
864 *node = ret;
865 }
866 return LY_SUCCESS;
Michal Vasko013a8182020-03-03 10:46:53 +0100867}
868
Michal Vasko3a41dff2020-07-15 14:30:28 +0200869API LY_ERR
Michal Vasko65243892020-12-04 16:26:21 +0100870lyd_new_list(struct lyd_node *parent, const struct lys_module *module, const char *name, ly_bool output,
871 struct lyd_node **node, ...)
Michal Vasko013a8182020-03-03 10:46:53 +0100872{
873 struct lyd_node *ret = NULL, *key;
874 const struct lysc_node *schema, *key_s;
875 struct ly_ctx *ctx = parent ? parent->schema->module->ctx : (module ? module->ctx : NULL);
876 va_list ap;
877 const char *key_val;
878 LY_ERR rc = LY_SUCCESS;
879
Michal Vasko6027eb92020-07-15 16:37:30 +0200880 LY_CHECK_ARG_RET(ctx, parent || module, parent || node, name, LY_EINVAL);
Michal Vasko013a8182020-03-03 10:46:53 +0100881
Michal Vaskof03ed032020-03-04 13:31:44 +0100882 if (!module) {
883 module = parent->schema->module;
884 }
885
Radek Krejci41ac9942020-11-02 14:47:56 +0100886 schema = lys_find_child(parent ? parent->schema : NULL, module, name, 0, LYS_LIST, output ? LYS_GETNEXT_OUTPUT : 0);
Michal Vasko3a41dff2020-07-15 14:30:28 +0200887 LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "List node \"%s\" not found.", name), LY_ENOTFOUND);
Michal Vasko013a8182020-03-03 10:46:53 +0100888
889 /* create list inner node */
Michal Vasko3a41dff2020-07-15 14:30:28 +0200890 LY_CHECK_RET(lyd_create_inner(schema, &ret));
Michal Vasko013a8182020-03-03 10:46:53 +0100891
Michal Vasko3a41dff2020-07-15 14:30:28 +0200892 va_start(ap, node);
Michal Vasko013a8182020-03-03 10:46:53 +0100893
894 /* create and insert all the keys */
Michal Vasko544e58a2021-01-28 14:33:41 +0100895 for (key_s = lysc_node_child(schema); key_s && (key_s->flags & LYS_KEY); key_s = key_s->next) {
Michal Vasko013a8182020-03-03 10:46:53 +0100896 key_val = va_arg(ap, const char *);
897
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200898 rc = lyd_create_term(key_s, key_val, key_val ? strlen(key_val) : 0, NULL, LY_PREF_JSON, NULL, LYD_HINT_DATA,
899 NULL, &key);
900 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko013a8182020-03-03 10:46:53 +0100901 lyd_insert_node(ret, NULL, key);
902 }
903
Michal Vasko013a8182020-03-03 10:46:53 +0100904 if (parent) {
905 lyd_insert_node(parent, NULL, ret);
906 }
907
908cleanup:
Michal Vasko3a41dff2020-07-15 14:30:28 +0200909 va_end(ap);
Michal Vasko013a8182020-03-03 10:46:53 +0100910 if (rc) {
911 lyd_free_tree(ret);
912 ret = NULL;
Michal Vasko3a41dff2020-07-15 14:30:28 +0200913 } else if (node) {
914 *node = ret;
Michal Vasko013a8182020-03-03 10:46:53 +0100915 }
Michal Vasko3a41dff2020-07-15 14:30:28 +0200916 return rc;
Michal Vasko013a8182020-03-03 10:46:53 +0100917}
918
Michal Vasko3a41dff2020-07-15 14:30:28 +0200919API LY_ERR
920lyd_new_list2(struct lyd_node *parent, const struct lys_module *module, const char *name, const char *keys,
Radek Krejci41ac9942020-11-02 14:47:56 +0100921 ly_bool output, struct lyd_node **node)
Michal Vasko013a8182020-03-03 10:46:53 +0100922{
923 struct lyd_node *ret = NULL;
924 const struct lysc_node *schema;
925 struct ly_ctx *ctx = parent ? parent->schema->module->ctx : (module ? module->ctx : NULL);
926
Michal Vasko6027eb92020-07-15 16:37:30 +0200927 LY_CHECK_ARG_RET(ctx, parent || module, parent || node, name, LY_EINVAL);
Michal Vasko013a8182020-03-03 10:46:53 +0100928
Michal Vaskof03ed032020-03-04 13:31:44 +0100929 if (!module) {
930 module = parent->schema->module;
931 }
Michal Vasko004d3152020-06-11 19:59:22 +0200932 if (!keys) {
933 keys = "";
934 }
Michal Vaskof03ed032020-03-04 13:31:44 +0100935
Michal Vasko004d3152020-06-11 19:59:22 +0200936 /* find schema node */
Radek Krejci41ac9942020-11-02 14:47:56 +0100937 schema = lys_find_child(parent ? parent->schema : NULL, module, name, 0, LYS_LIST, output ? LYS_GETNEXT_OUTPUT : 0);
Michal Vasko3a41dff2020-07-15 14:30:28 +0200938 LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "List node \"%s\" not found.", name), LY_ENOTFOUND);
Michal Vasko013a8182020-03-03 10:46:53 +0100939
Michal Vasko004d3152020-06-11 19:59:22 +0200940 if ((schema->flags & LYS_KEYLESS) && !keys[0]) {
941 /* key-less list */
Michal Vasko3a41dff2020-07-15 14:30:28 +0200942 LY_CHECK_RET(lyd_create_inner(schema, &ret));
Michal Vasko004d3152020-06-11 19:59:22 +0200943 } else {
944 /* create the list node */
Michal Vasko3a41dff2020-07-15 14:30:28 +0200945 LY_CHECK_RET(lyd_create_list2(schema, keys, strlen(keys), &ret));
Michal Vasko004d3152020-06-11 19:59:22 +0200946 }
Michal Vasko004d3152020-06-11 19:59:22 +0200947 if (parent) {
Michal Vasko013a8182020-03-03 10:46:53 +0100948 lyd_insert_node(parent, NULL, ret);
949 }
Michal Vasko3a41dff2020-07-15 14:30:28 +0200950
951 if (node) {
952 *node = ret;
953 }
954 return LY_SUCCESS;
Michal Vasko013a8182020-03-03 10:46:53 +0100955}
956
Michal Vasko3a41dff2020-07-15 14:30:28 +0200957API LY_ERR
958lyd_new_term(struct lyd_node *parent, const struct lys_module *module, const char *name, const char *val_str,
Radek Krejci41ac9942020-11-02 14:47:56 +0100959 ly_bool output, struct lyd_node **node)
Michal Vasko013a8182020-03-03 10:46:53 +0100960{
Michal Vaskocbff3e92020-05-27 12:56:41 +0200961 LY_ERR rc;
Michal Vasko013a8182020-03-03 10:46:53 +0100962 struct lyd_node *ret = NULL;
963 const struct lysc_node *schema;
964 struct ly_ctx *ctx = parent ? parent->schema->module->ctx : (module ? module->ctx : NULL);
965
Michal Vasko6027eb92020-07-15 16:37:30 +0200966 LY_CHECK_ARG_RET(ctx, parent || module, parent || node, name, LY_EINVAL);
Michal Vasko013a8182020-03-03 10:46:53 +0100967
Michal Vaskof03ed032020-03-04 13:31:44 +0100968 if (!module) {
969 module = parent->schema->module;
970 }
971
Radek Krejci41ac9942020-11-02 14:47:56 +0100972 schema = lys_find_child(parent ? parent->schema : NULL, module, name, 0, LYD_NODE_TERM, output ? LYS_GETNEXT_OUTPUT : 0);
Michal Vasko3a41dff2020-07-15 14:30:28 +0200973 LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "Term node \"%s\" not found.", name), LY_ENOTFOUND);
Michal Vasko013a8182020-03-03 10:46:53 +0100974
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200975 rc = lyd_create_term(schema, val_str, val_str ? strlen(val_str) : 0, NULL, LY_PREF_JSON, NULL, LYD_HINT_DATA, NULL,
976 &ret);
977 LY_CHECK_RET(rc);
Michal Vaskocbff3e92020-05-27 12:56:41 +0200978 if (parent) {
Michal Vasko013a8182020-03-03 10:46:53 +0100979 lyd_insert_node(parent, NULL, ret);
980 }
Michal Vasko3a41dff2020-07-15 14:30:28 +0200981
982 if (node) {
983 *node = ret;
984 }
985 return LY_SUCCESS;
Michal Vasko013a8182020-03-03 10:46:53 +0100986}
987
Michal Vasko3a41dff2020-07-15 14:30:28 +0200988API LY_ERR
Michal Vasko219006c2020-12-04 16:26:52 +0100989lyd_new_any(struct lyd_node *parent, const struct lys_module *module, const char *name, void *value,
Radek Krejci41ac9942020-11-02 14:47:56 +0100990 LYD_ANYDATA_VALUETYPE value_type, ly_bool output, struct lyd_node **node)
Michal Vasko013a8182020-03-03 10:46:53 +0100991{
992 struct lyd_node *ret = NULL;
993 const struct lysc_node *schema;
994 struct ly_ctx *ctx = parent ? parent->schema->module->ctx : (module ? module->ctx : NULL);
995
Michal Vasko6027eb92020-07-15 16:37:30 +0200996 LY_CHECK_ARG_RET(ctx, parent || module, parent || node, name, LY_EINVAL);
Michal Vasko013a8182020-03-03 10:46:53 +0100997
Michal Vaskof03ed032020-03-04 13:31:44 +0100998 if (!module) {
999 module = parent->schema->module;
1000 }
1001
Radek Krejci41ac9942020-11-02 14:47:56 +01001002 schema = lys_find_child(parent ? parent->schema : NULL, module, name, 0, LYD_NODE_ANY, output ? LYS_GETNEXT_OUTPUT : 0);
Michal Vasko3a41dff2020-07-15 14:30:28 +02001003 LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "Any node \"%s\" not found.", name), LY_ENOTFOUND);
Michal Vasko013a8182020-03-03 10:46:53 +01001004
Michal Vasko366a4a12020-12-04 16:23:57 +01001005 LY_CHECK_RET(lyd_create_any(schema, value, value_type, 1, &ret));
Michal Vasko3a41dff2020-07-15 14:30:28 +02001006 if (parent) {
Michal Vasko013a8182020-03-03 10:46:53 +01001007 lyd_insert_node(parent, NULL, ret);
1008 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02001009
1010 if (node) {
1011 *node = ret;
1012 }
1013 return LY_SUCCESS;
Michal Vasko013a8182020-03-03 10:46:53 +01001014}
1015
Michal Vasko4490d312020-06-16 13:08:55 +02001016/**
1017 * @brief Update node value.
1018 *
1019 * @param[in] node Node to update.
1020 * @param[in] value New value to set.
1021 * @param[in] value_type Type of @p value for any node.
1022 * @param[out] new_parent Set to @p node if the value was updated, otherwise set to NULL.
1023 * @param[out] new_node Set to @p node if the value was updated, otherwise set to NULL.
1024 * @return LY_ERR value.
1025 */
Michal Vasko00cbf532020-06-15 13:58:47 +02001026static LY_ERR
1027lyd_new_path_update(struct lyd_node *node, const void *value, LYD_ANYDATA_VALUETYPE value_type,
Radek Krejci0f969882020-08-21 16:56:47 +02001028 struct lyd_node **new_parent, struct lyd_node **new_node)
Michal Vasko00cbf532020-06-15 13:58:47 +02001029{
1030 LY_ERR ret = LY_SUCCESS;
1031 struct lyd_node *new_any;
1032
1033 switch (node->schema->nodetype) {
1034 case LYS_CONTAINER:
1035 case LYS_NOTIF:
1036 case LYS_RPC:
1037 case LYS_ACTION:
1038 case LYS_LIST:
1039 case LYS_LEAFLIST:
1040 /* if it exists, there is nothing to update */
1041 *new_parent = NULL;
1042 *new_node = NULL;
1043 break;
1044 case LYS_LEAF:
1045 ret = lyd_change_term(node, value);
1046 if ((ret == LY_SUCCESS) || (ret == LY_EEXIST)) {
1047 /* there was an actual change (at least of the default flag) */
1048 *new_parent = node;
1049 *new_node = node;
1050 ret = LY_SUCCESS;
1051 } else if (ret == LY_ENOT) {
1052 /* no change */
1053 *new_parent = NULL;
1054 *new_node = NULL;
1055 ret = LY_SUCCESS;
1056 } /* else error */
1057 break;
1058 case LYS_ANYDATA:
1059 case LYS_ANYXML:
1060 /* create a new any node */
Michal Vasko366a4a12020-12-04 16:23:57 +01001061 LY_CHECK_RET(lyd_create_any(node->schema, value, value_type, 0, &new_any));
Michal Vasko00cbf532020-06-15 13:58:47 +02001062
1063 /* compare with the existing one */
Michal Vasko8f359bf2020-07-28 10:41:15 +02001064 if (lyd_compare_single(node, new_any, 0)) {
Michal Vasko00cbf532020-06-15 13:58:47 +02001065 /* not equal, switch values (so that we can use generic node free) */
1066 ((struct lyd_node_any *)new_any)->value = ((struct lyd_node_any *)node)->value;
1067 ((struct lyd_node_any *)new_any)->value_type = ((struct lyd_node_any *)node)->value_type;
1068 ((struct lyd_node_any *)node)->value.str = value;
1069 ((struct lyd_node_any *)node)->value_type = value_type;
1070
1071 *new_parent = node;
1072 *new_node = node;
1073 } else {
1074 /* they are equal */
1075 *new_parent = NULL;
1076 *new_node = NULL;
1077 }
1078 lyd_free_tree(new_any);
1079 break;
1080 default:
Michal Vaskob7be7a82020-08-20 09:09:04 +02001081 LOGINT(LYD_CTX(node));
Michal Vasko00cbf532020-06-15 13:58:47 +02001082 ret = LY_EINT;
1083 break;
1084 }
1085
1086 return ret;
1087}
1088
Michal Vasko3a41dff2020-07-15 14:30:28 +02001089API LY_ERR
Michal Vasko871a0252020-11-11 18:35:24 +01001090lyd_new_meta(const struct ly_ctx *ctx, struct lyd_node *parent, const struct lys_module *module, const char *name,
1091 const char *val_str, ly_bool clear_dflt, struct lyd_meta **meta)
Michal Vaskod86997b2020-05-26 15:19:54 +02001092{
Michal Vaskod86997b2020-05-26 15:19:54 +02001093 const char *prefix, *tmp;
Michal Vaskod86997b2020-05-26 15:19:54 +02001094 size_t pref_len, name_len;
1095
Michal Vasko2b5344c2021-02-26 10:12:05 +01001096 LY_CHECK_ARG_RET(ctx, ctx || parent, name, module || strchr(name, ':'), parent || meta, LY_EINVAL);
1097 if (!ctx) {
1098 ctx = LYD_CTX(parent);
1099 }
Michal Vasko00cbf532020-06-15 13:58:47 +02001100
Michal Vasko871a0252020-11-11 18:35:24 +01001101 if (parent && !parent->schema) {
1102 LOGERR(ctx, LY_EINVAL, "Cannot add metadata to an opaque node \"%s\".", ((struct lyd_node_opaq *)parent)->name);
1103 return LY_EINVAL;
1104 }
Michal Vasko610553d2020-11-18 18:15:05 +01001105 if (meta) {
1106 *meta = NULL;
1107 }
Michal Vaskod86997b2020-05-26 15:19:54 +02001108
1109 /* parse the name */
1110 tmp = name;
1111 if (ly_parse_nodeid(&tmp, &prefix, &pref_len, &name, &name_len) || tmp[0]) {
1112 LOGERR(ctx, LY_EINVAL, "Metadata name \"%s\" is not valid.", name);
Michal Vasko871a0252020-11-11 18:35:24 +01001113 return LY_EINVAL;
Michal Vaskod86997b2020-05-26 15:19:54 +02001114 }
1115
1116 /* find the module */
1117 if (prefix) {
Radek Krejci0ad51f12020-07-16 12:08:12 +02001118 module = ly_ctx_get_module_implemented2(ctx, prefix, pref_len);
Michal Vasko3a41dff2020-07-15 14:30:28 +02001119 LY_CHECK_ERR_RET(!module, LOGERR(ctx, LY_EINVAL, "Module \"%.*s\" not found.", pref_len, prefix), LY_ENOTFOUND);
Michal Vaskod86997b2020-05-26 15:19:54 +02001120 }
1121
1122 /* set value if none */
1123 if (!val_str) {
1124 val_str = "";
1125 }
1126
Michal Vasko871a0252020-11-11 18:35:24 +01001127 return lyd_create_meta(parent, meta, module, name, name_len, val_str, strlen(val_str), NULL, LY_PREF_JSON,
1128 NULL, LYD_HINT_DATA, clear_dflt, NULL);
1129}
Michal Vasko3a41dff2020-07-15 14:30:28 +02001130
Michal Vaskoba696702020-11-11 19:12:45 +01001131API LY_ERR
1132lyd_new_meta2(const struct ly_ctx *ctx, struct lyd_node *parent, ly_bool clear_dflt, const struct lyd_attr *attr,
1133 struct lyd_meta **meta)
1134{
1135 const struct lys_module *mod;
1136
1137 LY_CHECK_ARG_RET(NULL, ctx, attr, parent || meta, LY_EINVAL);
1138
1139 if (parent && !parent->schema) {
1140 LOGERR(ctx, LY_EINVAL, "Cannot add metadata to an opaque node \"%s\".", ((struct lyd_node_opaq *)parent)->name);
1141 return LY_EINVAL;
Michal Vasko3a41dff2020-07-15 14:30:28 +02001142 }
Michal Vasko610553d2020-11-18 18:15:05 +01001143 if (meta) {
1144 *meta = NULL;
1145 }
Michal Vaskoba696702020-11-11 19:12:45 +01001146
1147 switch (attr->format) {
1148 case LY_PREF_XML:
Michal Vaskoad92b672020-11-12 13:11:31 +01001149 mod = ly_ctx_get_module_implemented_ns(ctx, attr->name.module_ns);
Michal Vaskoba696702020-11-11 19:12:45 +01001150 if (!mod) {
Michal Vaskoad92b672020-11-12 13:11:31 +01001151 LOGERR(ctx, LY_EINVAL, "Module with namespace \"%s\" not found.", attr->name.module_ns);
Michal Vaskoba696702020-11-11 19:12:45 +01001152 return LY_ENOTFOUND;
1153 }
1154 break;
1155 case LY_PREF_JSON:
Michal Vaskoad92b672020-11-12 13:11:31 +01001156 mod = ly_ctx_get_module_implemented(ctx, attr->name.module_name);
Michal Vaskoba696702020-11-11 19:12:45 +01001157 if (!mod) {
Michal Vaskoad92b672020-11-12 13:11:31 +01001158 LOGERR(ctx, LY_EINVAL, "Module \"%s\" not found.", attr->name.module_name);
Michal Vaskoba696702020-11-11 19:12:45 +01001159 return LY_ENOTFOUND;
1160 }
1161 break;
1162 default:
1163 LOGINT_RET(ctx);
1164 }
1165
Michal Vaskoad92b672020-11-12 13:11:31 +01001166 return lyd_create_meta(parent, meta, mod, attr->name.name, strlen(attr->name.name), attr->value, strlen(attr->value),
Michal Vaskoba696702020-11-11 19:12:45 +01001167 NULL, attr->format, attr->val_prefix_data, attr->hints, clear_dflt, NULL);
Michal Vaskod86997b2020-05-26 15:19:54 +02001168}
1169
Michal Vasko3a41dff2020-07-15 14:30:28 +02001170API LY_ERR
Michal Vasko00cbf532020-06-15 13:58:47 +02001171lyd_new_opaq(struct lyd_node *parent, const struct ly_ctx *ctx, const char *name, const char *value,
Michal Vasko0ab974d2021-02-24 13:18:26 +01001172 const char *prefix, const char *module_name, struct lyd_node **node)
Michal Vasko00cbf532020-06-15 13:58:47 +02001173{
1174 struct lyd_node *ret = NULL;
Michal Vasko0ab974d2021-02-24 13:18:26 +01001175 LY_CHECK_ARG_RET(ctx, parent || ctx, parent || node, name, module_name, !prefix || !strcmp(prefix, module_name), LY_EINVAL);
Michal Vasko00cbf532020-06-15 13:58:47 +02001176
1177 if (!ctx) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02001178 ctx = LYD_CTX(parent);
Michal Vasko00cbf532020-06-15 13:58:47 +02001179 }
1180 if (!value) {
1181 value = "";
1182 }
1183
Michal Vasko0ab974d2021-02-24 13:18:26 +01001184 LY_CHECK_RET(lyd_create_opaq(ctx, name, strlen(name), prefix, prefix ? strlen(prefix) : 0, module_name,
1185 strlen(module_name), value, strlen(value), NULL, LY_PREF_JSON, NULL, 0, &ret));
Michal Vasko3a41dff2020-07-15 14:30:28 +02001186 if (parent) {
Michal Vasko00cbf532020-06-15 13:58:47 +02001187 lyd_insert_node(parent, NULL, ret);
1188 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02001189
1190 if (node) {
1191 *node = ret;
1192 }
1193 return LY_SUCCESS;
Michal Vasko00cbf532020-06-15 13:58:47 +02001194}
1195
Michal Vasko3a41dff2020-07-15 14:30:28 +02001196API LY_ERR
Michal Vasko8d65f852021-02-17 11:28:13 +01001197lyd_new_opaq2(struct lyd_node *parent, const struct ly_ctx *ctx, const char *name, const char *value,
Michal Vasko0ab974d2021-02-24 13:18:26 +01001198 const char *prefix, const char *module_ns, struct lyd_node **node)
Michal Vasko8d65f852021-02-17 11:28:13 +01001199{
1200 struct lyd_node *ret = NULL;
1201
1202 LY_CHECK_ARG_RET(ctx, parent || ctx, parent || node, name, module_ns, LY_EINVAL);
1203
1204 if (!ctx) {
1205 ctx = LYD_CTX(parent);
1206 }
1207 if (!value) {
1208 value = "";
1209 }
1210
Michal Vasko0ab974d2021-02-24 13:18:26 +01001211 LY_CHECK_RET(lyd_create_opaq(ctx, name, strlen(name), prefix, prefix ? strlen(prefix) : 0, module_ns,
1212 strlen(module_ns), value, strlen(value), NULL, LY_PREF_XML, NULL, 0, &ret));
Michal Vasko8d65f852021-02-17 11:28:13 +01001213 if (parent) {
1214 lyd_insert_node(parent, NULL, ret);
1215 }
1216
1217 if (node) {
1218 *node = ret;
1219 }
1220 return LY_SUCCESS;
1221}
1222
1223API LY_ERR
1224lyd_new_attr(struct lyd_node *parent, const char *module_name, const char *name, const char *value,
Radek Krejci0f969882020-08-21 16:56:47 +02001225 struct lyd_attr **attr)
Michal Vasko00cbf532020-06-15 13:58:47 +02001226{
Radek Krejci1798aae2020-07-14 13:26:06 +02001227 struct lyd_attr *ret = NULL;
Michal Vasko00cbf532020-06-15 13:58:47 +02001228 const struct ly_ctx *ctx;
1229 const char *prefix, *tmp;
Michal Vasko51d21b82020-11-13 18:03:54 +01001230 size_t pref_len, name_len, mod_len;
Michal Vasko00cbf532020-06-15 13:58:47 +02001231
Michal Vasko3a41dff2020-07-15 14:30:28 +02001232 LY_CHECK_ARG_RET(NULL, parent, !parent->schema, name, LY_EINVAL);
Michal Vasko00cbf532020-06-15 13:58:47 +02001233
Michal Vaskob7be7a82020-08-20 09:09:04 +02001234 ctx = LYD_CTX(parent);
Michal Vasko00cbf532020-06-15 13:58:47 +02001235
1236 /* parse the name */
1237 tmp = name;
1238 if (ly_parse_nodeid(&tmp, &prefix, &pref_len, &name, &name_len) || tmp[0]) {
Michal Vaskob69b68c2021-02-17 11:18:16 +01001239 LOGERR(ctx, LY_EINVAL, "Attribute name \"%s\" is not valid.", name);
Michal Vasko3a41dff2020-07-15 14:30:28 +02001240 return LY_EVALID;
Michal Vasko00cbf532020-06-15 13:58:47 +02001241 }
1242
Michal Vasko51d21b82020-11-13 18:03:54 +01001243 /* get the module */
1244 if (module_name) {
1245 mod_len = strlen(module_name);
1246 } else {
1247 module_name = prefix;
1248 mod_len = pref_len;
1249 }
1250
Michal Vasko00cbf532020-06-15 13:58:47 +02001251 /* set value if none */
Michal Vasko8d65f852021-02-17 11:28:13 +01001252 if (!value) {
1253 value = "";
Michal Vasko00cbf532020-06-15 13:58:47 +02001254 }
1255
Michal Vasko8d65f852021-02-17 11:28:13 +01001256 LY_CHECK_RET(lyd_create_attr(parent, &ret, ctx, name, name_len, prefix, pref_len, module_name, mod_len, value,
1257 strlen(value), NULL, LY_PREF_JSON, NULL, LYD_HINT_DATA));
1258
1259 if (attr) {
1260 *attr = ret;
1261 }
1262 return LY_SUCCESS;
1263}
1264
1265API LY_ERR
1266lyd_new_attr2(struct lyd_node *parent, const char *module_ns, const char *name, const char *value,
1267 struct lyd_attr **attr)
1268{
1269 struct lyd_attr *ret = NULL;
1270 const struct ly_ctx *ctx;
1271 const char *prefix, *tmp;
1272 size_t pref_len, name_len;
1273
1274 LY_CHECK_ARG_RET(NULL, parent, !parent->schema, name, LY_EINVAL);
1275
1276 ctx = LYD_CTX(parent);
1277
1278 /* parse the name */
1279 tmp = name;
1280 if (ly_parse_nodeid(&tmp, &prefix, &pref_len, &name, &name_len) || tmp[0]) {
1281 LOGERR(ctx, LY_EINVAL, "Attribute name \"%s\" is not valid.", name);
1282 return LY_EVALID;
1283 }
1284
1285 /* set value if none */
1286 if (!value) {
1287 value = "";
1288 }
1289
1290 LY_CHECK_RET(lyd_create_attr(parent, &ret, ctx, name, name_len, prefix, pref_len, module_ns,
1291 module_ns ? strlen(module_ns) : 0, value, strlen(value), NULL, LY_PREF_XML, NULL, LYD_HINT_DATA));
Michal Vasko3a41dff2020-07-15 14:30:28 +02001292
1293 if (attr) {
1294 *attr = ret;
1295 }
1296 return LY_SUCCESS;
Michal Vasko00cbf532020-06-15 13:58:47 +02001297}
1298
1299API LY_ERR
1300lyd_change_term(struct lyd_node *term, const char *val_str)
1301{
1302 LY_ERR ret = LY_SUCCESS;
1303 struct lysc_type *type;
1304 struct lyd_node_term *t;
1305 struct lyd_node *parent;
1306 struct lyd_value val = {0};
Radek Krejci857189e2020-09-01 13:26:36 +02001307 ly_bool dflt_change, val_change;
Michal Vasko00cbf532020-06-15 13:58:47 +02001308
1309 LY_CHECK_ARG_RET(NULL, term, term->schema, term->schema->nodetype & LYD_NODE_TERM, LY_EINVAL);
1310
1311 if (!val_str) {
1312 val_str = "";
1313 }
1314 t = (struct lyd_node_term *)term;
1315 type = ((struct lysc_node_leaf *)term->schema)->type;
1316
1317 /* parse the new value */
Radek Krejciddace2c2021-01-08 11:30:56 +01001318 LOG_LOCSET(term->schema, term, NULL, NULL);
Michal Vaskofeca4fb2020-10-05 08:58:40 +02001319 ret = lyd_value_store(LYD_CTX(term), &val, type, val_str, strlen(val_str), NULL, LY_PREF_JSON, NULL, LYD_HINT_DATA,
Radek Krejci2efc45b2020-12-22 16:25:44 +01001320 term->schema, NULL);
Radek Krejciddace2c2021-01-08 11:30:56 +01001321 LOG_LOCBACK(term->schema ? 1 : 0, 1, 0, 0);
Michal Vaskofeca4fb2020-10-05 08:58:40 +02001322 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko00cbf532020-06-15 13:58:47 +02001323
1324 /* compare original and new value */
1325 if (type->plugin->compare(&t->value, &val)) {
1326 /* values differ, switch them */
Michal Vaskob7be7a82020-08-20 09:09:04 +02001327 type->plugin->free(LYD_CTX(term), &t->value);
Michal Vasko00cbf532020-06-15 13:58:47 +02001328 t->value = val;
1329 memset(&val, 0, sizeof val);
1330 val_change = 1;
1331 } else {
1332 val_change = 0;
1333 }
1334
1335 /* always clear the default flag */
1336 if (term->flags & LYD_DEFAULT) {
Michal Vasko9e685082021-01-29 14:49:09 +01001337 for (parent = term; parent; parent = lyd_parent(parent)) {
Michal Vasko00cbf532020-06-15 13:58:47 +02001338 parent->flags &= ~LYD_DEFAULT;
1339 }
1340 dflt_change = 1;
1341 } else {
1342 dflt_change = 0;
1343 }
1344
1345 if (val_change || dflt_change) {
1346 /* make the node non-validated */
1347 term->flags &= LYD_NEW;
1348 }
1349
1350 if (val_change) {
1351 if (term->schema->nodetype == LYS_LEAFLIST) {
1352 /* leaf-list needs to be hashed again and re-inserted into parent */
1353 lyd_unlink_hash(term);
1354 lyd_hash(term);
1355 LY_CHECK_GOTO(ret = lyd_insert_hash(term), cleanup);
1356 } else if ((term->schema->flags & LYS_KEY) && term->parent) {
1357 /* list needs to be updated if its key was changed */
1358 assert(term->parent->schema->nodetype == LYS_LIST);
Michal Vasko9e685082021-01-29 14:49:09 +01001359 lyd_unlink_hash(lyd_parent(term));
1360 lyd_hash(lyd_parent(term));
1361 LY_CHECK_GOTO(ret = lyd_insert_hash(lyd_parent(term)), cleanup);
Michal Vasko00cbf532020-06-15 13:58:47 +02001362 } /* else leaf that is not a key, its value is not used for its hash so it does not change */
1363 }
1364
1365 /* retrun value */
1366 if (!val_change) {
1367 if (dflt_change) {
1368 /* only default flag change */
1369 ret = LY_EEXIST;
1370 } else {
1371 /* no change */
1372 ret = LY_ENOT;
1373 }
1374 } /* else value changed, LY_SUCCESS */
1375
1376cleanup:
Michal Vasko59512fc2020-12-09 18:13:29 +01001377 if (val.realtype) {
1378 type->plugin->free(LYD_CTX(term), &val);
1379 }
Michal Vasko00cbf532020-06-15 13:58:47 +02001380 return ret;
1381}
1382
Michal Vasko41586352020-07-13 13:54:25 +02001383API LY_ERR
1384lyd_change_meta(struct lyd_meta *meta, const char *val_str)
1385{
1386 LY_ERR ret = LY_SUCCESS;
Radek Krejci2b18bf12020-11-06 11:20:20 +01001387 struct lyd_meta *m2 = NULL;
Michal Vasko41586352020-07-13 13:54:25 +02001388 struct lyd_value val;
Radek Krejci857189e2020-09-01 13:26:36 +02001389 ly_bool val_change;
Michal Vasko41586352020-07-13 13:54:25 +02001390
1391 LY_CHECK_ARG_RET(NULL, meta, LY_EINVAL);
1392
1393 if (!val_str) {
1394 val_str = "";
1395 }
1396
1397 /* parse the new value into a new meta structure */
1398 LY_CHECK_GOTO(ret = lyd_create_meta(NULL, &m2, meta->annotation->module, meta->name, strlen(meta->name), val_str,
Michal Vasko871a0252020-11-11 18:35:24 +01001399 strlen(val_str), NULL, LY_PREF_JSON, NULL, LYD_HINT_DATA, 0, NULL), cleanup);
Michal Vasko41586352020-07-13 13:54:25 +02001400
1401 /* compare original and new value */
1402 if (lyd_compare_meta(meta, m2)) {
1403 /* values differ, switch them */
1404 val = meta->value;
1405 meta->value = m2->value;
1406 m2->value = val;
1407 val_change = 1;
1408 } else {
1409 val_change = 0;
1410 }
1411
1412 /* retrun value */
1413 if (!val_change) {
1414 /* no change */
1415 ret = LY_ENOT;
1416 } /* else value changed, LY_SUCCESS */
1417
1418cleanup:
Michal Vasko1a66a5d2020-11-18 18:15:37 +01001419 lyd_free_meta_single(m2);
Michal Vasko41586352020-07-13 13:54:25 +02001420 return ret;
1421}
1422
Michal Vasko3a41dff2020-07-15 14:30:28 +02001423API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02001424lyd_new_path(struct lyd_node *parent, const struct ly_ctx *ctx, const char *path, const char *value, uint32_t options,
Radek Krejci0f969882020-08-21 16:56:47 +02001425 struct lyd_node **node)
Michal Vasko00cbf532020-06-15 13:58:47 +02001426{
Michal Vasko3a41dff2020-07-15 14:30:28 +02001427 return lyd_new_path2(parent, ctx, path, value, 0, options, node, NULL);
Michal Vasko00cbf532020-06-15 13:58:47 +02001428}
1429
Michal Vasko6741dc62021-02-04 09:27:45 +01001430static LY_ERR
1431lyd_new_path_check_find_lypath(struct ly_path *path, const char *str_path, const char *value, uint32_t options)
1432{
1433 LY_ERR ret = LY_SUCCESS, r;
1434 const struct lysc_node *schema;
1435 struct ly_path_predicate *pred;
Michal Vasko6f9b4262021-02-04 12:09:56 +01001436 LY_ARRAY_COUNT_TYPE u, new_count;
Michal Vasko6741dc62021-02-04 09:27:45 +01001437
1438 assert(path);
1439
1440 /* go through all the compiled nodes */
1441 LY_ARRAY_FOR(path, u) {
1442 schema = path[u].node;
Michal Vasko6f9b4262021-02-04 12:09:56 +01001443 new_count = u + 1;
Michal Vasko6741dc62021-02-04 09:27:45 +01001444 if ((schema->nodetype == LYS_LIST) && (path[u].pred_type == LY_PATH_PREDTYPE_NONE)) {
1445 if (schema->flags & LYS_KEYLESS) {
1446 /* creating a new keyless list instance */
1447 break;
1448 } else if ((u < LY_ARRAY_COUNT(path) - 1) || !(options & LYD_NEW_PATH_OPAQ)) {
1449 LOG_LOCSET(schema, NULL, NULL, NULL);
1450 LOGVAL(schema->module->ctx, LYVE_XPATH, "Predicate missing for %s \"%s\" in path \"%s\".",
1451 lys_nodetype2str(schema->nodetype), schema->name, str_path);
1452 LOG_LOCBACK(1, 0, 0, 0);
1453 return LY_EINVAL;
1454 } /* else creating an opaque list without keys */
1455 }
1456 }
1457
Michal Vasko6f9b4262021-02-04 12:09:56 +01001458 if ((schema->nodetype == LYS_LEAFLIST) && (path[new_count - 1].pred_type == LY_PATH_PREDTYPE_NONE)) {
Michal Vasko6741dc62021-02-04 09:27:45 +01001459 /* parse leafref value into a predicate, if not defined in the path */
1460 if (!value) {
1461 value = "";
1462 }
1463
1464 r = LY_SUCCESS;
1465 if (options & LYD_NEW_PATH_OPAQ) {
1466 r = lys_value_validate(NULL, schema, value, strlen(value));
1467 }
1468 if (!r) {
1469 /* store the new predicate */
Michal Vasko6f9b4262021-02-04 12:09:56 +01001470 path[new_count - 1].pred_type = LY_PATH_PREDTYPE_LEAFLIST;
1471 LY_ARRAY_NEW_GOTO(schema->module->ctx, path[new_count - 1].predicates, pred, ret, cleanup);
Michal Vasko6741dc62021-02-04 09:27:45 +01001472
1473 ret = lyd_value_store(schema->module->ctx, &pred->value, ((struct lysc_node_leaflist *)schema)->type, value,
1474 strlen(value), NULL, LY_PREF_JSON, NULL, LYD_HINT_DATA, schema, NULL);
1475 LY_CHECK_GOTO(ret, cleanup);
1476 ++((struct lysc_type *)pred->value.realtype)->refcount;
1477
1478 if (schema->flags & LYS_CONFIG_R) {
1479 /* creating a new state leaf-list instance */
Michal Vasko6f9b4262021-02-04 12:09:56 +01001480 --new_count;
Michal Vasko6741dc62021-02-04 09:27:45 +01001481 }
1482 } /* else we have opaq flag and the value is not valid, leave no predicate and then create an opaque node */
1483 }
1484
1485 /* hide the nodes that should always be created so they are not found */
Michal Vasko6f9b4262021-02-04 12:09:56 +01001486 while (new_count < LY_ARRAY_COUNT(path)) {
Michal Vasko6741dc62021-02-04 09:27:45 +01001487 LY_ARRAY_DECREMENT(path);
1488 }
1489
1490cleanup:
1491 return ret;
1492}
1493
Michal Vasko00cbf532020-06-15 13:58:47 +02001494API LY_ERR
1495lyd_new_path2(struct lyd_node *parent, const struct ly_ctx *ctx, const char *path, const void *value,
Radek Krejci1deb5be2020-08-26 16:43:36 +02001496 LYD_ANYDATA_VALUETYPE value_type, uint32_t options, struct lyd_node **new_parent, struct lyd_node **new_node)
Michal Vasko00cbf532020-06-15 13:58:47 +02001497{
1498 LY_ERR ret = LY_SUCCESS, r;
1499 struct lyxp_expr *exp = NULL;
1500 struct ly_path *p = NULL;
1501 struct lyd_node *nparent = NULL, *nnode = NULL, *node = NULL, *cur_parent;
1502 const struct lysc_node *schema;
Michal Vasko6741dc62021-02-04 09:27:45 +01001503 LY_ARRAY_COUNT_TYPE path_idx = 0, orig_count;
Michal Vasko00cbf532020-06-15 13:58:47 +02001504
1505 LY_CHECK_ARG_RET(ctx, parent || ctx, path, (path[0] == '/') || parent, LY_EINVAL);
1506
1507 if (!ctx) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02001508 ctx = LYD_CTX(parent);
Michal Vasko00cbf532020-06-15 13:58:47 +02001509 }
1510
1511 /* parse path */
Michal Vasko6b26e742020-07-17 15:02:10 +02001512 LY_CHECK_GOTO(ret = ly_path_parse(ctx, NULL, path, strlen(path), LY_PATH_BEGIN_EITHER, LY_PATH_LREF_FALSE,
Michal Vasko69730152020-10-09 16:30:07 +02001513 LY_PATH_PREFIX_OPTIONAL, LY_PATH_PRED_SIMPLE, &exp), cleanup);
Michal Vasko00cbf532020-06-15 13:58:47 +02001514
1515 /* compile path */
1516 LY_CHECK_GOTO(ret = ly_path_compile(ctx, NULL, parent ? parent->schema : NULL, exp, LY_PATH_LREF_FALSE,
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001517 options & LYD_NEW_PATH_OUTPUT ? LY_PATH_OPER_OUTPUT : LY_PATH_OPER_INPUT, LY_PATH_TARGET_MANY, LY_PREF_JSON,
Michal Vasko405cc9e2020-12-01 12:01:27 +01001518 NULL, NULL, &p), cleanup);
Michal Vasko00cbf532020-06-15 13:58:47 +02001519
Michal Vasko6741dc62021-02-04 09:27:45 +01001520 /* check the compiled path before searching existing nodes, it may be shortened */
1521 orig_count = LY_ARRAY_COUNT(p);
1522 LY_CHECK_GOTO(ret = lyd_new_path_check_find_lypath(p, path, value, options), cleanup);
Michal Vasko00cbf532020-06-15 13:58:47 +02001523
1524 /* try to find any existing nodes in the path */
1525 if (parent) {
1526 ret = ly_path_eval_partial(p, parent, &path_idx, &node);
1527 if (ret == LY_SUCCESS) {
Michal Vasko6741dc62021-02-04 09:27:45 +01001528 if (orig_count == LY_ARRAY_COUNT(p)) {
1529 /* the node exists, are we supposed to update it or is it just a default? */
1530 if (!(options & LYD_NEW_PATH_UPDATE) && !(node->flags & LYD_DEFAULT)) {
1531 LOG_LOCSET(NULL, node, NULL, NULL);
1532 LOGVAL(ctx, LYVE_REFERENCE, "Path \"%s\" already exists", path);
1533 LOG_LOCBACK(0, 1, 0, 0);
1534 ret = LY_EEXIST;
1535 goto cleanup;
1536 }
Michal Vasko00cbf532020-06-15 13:58:47 +02001537
Michal Vasko6741dc62021-02-04 09:27:45 +01001538 /* update the existing node */
1539 ret = lyd_new_path_update(node, value, value_type, &nparent, &nnode);
1540 goto cleanup;
1541 } /* else we were not searching for the whole path */
Michal Vasko00cbf532020-06-15 13:58:47 +02001542 } else if (ret == LY_EINCOMPLETE) {
1543 /* some nodes were found, adjust the iterator to the next segment */
1544 ++path_idx;
1545 } else if (ret == LY_ENOTFOUND) {
1546 /* we will create the nodes from top-level, default behavior (absolute path), or from the parent (relative path) */
Michal Vasko45b0d202020-11-06 17:20:46 +01001547 if (lysc_data_parent(p[0].node)) {
Michal Vasko00cbf532020-06-15 13:58:47 +02001548 node = parent;
1549 }
1550 } else {
1551 /* error */
1552 goto cleanup;
1553 }
1554 }
1555
Michal Vasko6741dc62021-02-04 09:27:45 +01001556 /* restore the full path for creating new nodes */
1557 while (orig_count > LY_ARRAY_COUNT(p)) {
1558 LY_ARRAY_INCREMENT(p);
1559 }
1560
Michal Vasko00cbf532020-06-15 13:58:47 +02001561 /* create all the non-existing nodes in a loop */
Michal Vaskod989ba02020-08-24 10:59:24 +02001562 for ( ; path_idx < LY_ARRAY_COUNT(p); ++path_idx) {
Michal Vasko00cbf532020-06-15 13:58:47 +02001563 cur_parent = node;
1564 schema = p[path_idx].node;
1565
1566 switch (schema->nodetype) {
1567 case LYS_LIST:
1568 if (!(schema->flags & LYS_KEYLESS)) {
Radek Krejci092e33c2020-10-12 15:33:10 +02001569 if ((options & LYD_NEW_PATH_OPAQ) && (p[path_idx].pred_type == LY_PATH_PREDTYPE_NONE)) {
Michal Vasko00cbf532020-06-15 13:58:47 +02001570 /* creating opaque list without keys */
Michal Vasko501af032020-11-11 20:27:44 +01001571 LY_CHECK_GOTO(ret = lyd_create_opaq(ctx, schema->name, strlen(schema->name), NULL, 0,
1572 schema->module->name, strlen(schema->module->name), NULL, 0, NULL, LY_PREF_JSON, NULL,
1573 LYD_NODEHINT_LIST, &node), cleanup);
Michal Vasko00cbf532020-06-15 13:58:47 +02001574 } else {
Michal Vasko8b776962021-01-12 12:21:49 +01001575 if (p[path_idx].pred_type != LY_PATH_PREDTYPE_LIST) {
1576 LOG_LOCSET(schema, NULL, NULL, NULL);
1577 LOGVAL(ctx, LYVE_XPATH, "Predicate missing for %s \"%s\" in path \"%s\".",
1578 lys_nodetype2str(schema->nodetype), schema->name, path);
1579 LOG_LOCBACK(1, 0, 0, 0);
1580 ret = LY_EINVAL;
1581 goto cleanup;
1582 }
1583
Michal Vasko00cbf532020-06-15 13:58:47 +02001584 LY_CHECK_GOTO(ret = lyd_create_list(schema, p[path_idx].predicates, &node), cleanup);
1585 }
1586 break;
1587 }
Radek Krejcif13b87b2020-12-01 22:02:17 +01001588 /* fall through */
Michal Vasko00cbf532020-06-15 13:58:47 +02001589 case LYS_CONTAINER:
1590 case LYS_NOTIF:
1591 case LYS_RPC:
1592 case LYS_ACTION:
1593 LY_CHECK_GOTO(ret = lyd_create_inner(schema, &node), cleanup);
1594 break;
1595 case LYS_LEAFLIST:
Radek Krejci092e33c2020-10-12 15:33:10 +02001596 if ((options & LYD_NEW_PATH_OPAQ) && (p[path_idx].pred_type == LY_PATH_PREDTYPE_NONE)) {
Michal Vasko00cbf532020-06-15 13:58:47 +02001597 /* creating opaque leaf-list without value */
Michal Vasko501af032020-11-11 20:27:44 +01001598 LY_CHECK_GOTO(ret = lyd_create_opaq(ctx, schema->name, strlen(schema->name), NULL, 0,
1599 schema->module->name, strlen(schema->module->name), NULL, 0, NULL, LY_PREF_JSON, NULL,
1600 LYD_NODEHINT_LEAFLIST, &node), cleanup);
Michal Vasko00cbf532020-06-15 13:58:47 +02001601 } else {
1602 assert(p[path_idx].pred_type == LY_PATH_PREDTYPE_LEAFLIST);
1603 LY_CHECK_GOTO(ret = lyd_create_term2(schema, &p[path_idx].predicates[0].value, &node), cleanup);
1604 }
1605 break;
1606 case LYS_LEAF:
Michal Vasko35880332020-12-08 13:08:12 +01001607 if (lysc_is_key(schema)) {
1608 /* it must have been already created or some error will occur later */
1609 assert(cur_parent);
1610 node = lyd_child(cur_parent);
1611 assert(node && (node->schema == schema));
1612 goto next_iter;
1613 }
1614
1615 /* make sure there is some value */
Michal Vasko00cbf532020-06-15 13:58:47 +02001616 if (!value) {
1617 value = "";
1618 }
1619
1620 r = LY_SUCCESS;
Radek Krejci092e33c2020-10-12 15:33:10 +02001621 if (options & LYD_NEW_PATH_OPAQ) {
Michal Vaskof937cfe2020-08-03 16:07:12 +02001622 r = lys_value_validate(NULL, schema, value, strlen(value));
Michal Vasko00cbf532020-06-15 13:58:47 +02001623 }
1624 if (!r) {
Michal Vaskofeca4fb2020-10-05 08:58:40 +02001625 ret = lyd_create_term(schema, value, strlen(value), NULL, LY_PREF_JSON, NULL, LYD_HINT_DATA, NULL, &node);
1626 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko00cbf532020-06-15 13:58:47 +02001627 } else {
1628 /* creating opaque leaf without value */
Michal Vasko501af032020-11-11 20:27:44 +01001629 LY_CHECK_GOTO(ret = lyd_create_opaq(ctx, schema->name, strlen(schema->name), NULL, 0, schema->module->name,
1630 strlen(schema->module->name), NULL, 0, NULL, LY_PREF_JSON, NULL, 0, &node), cleanup);
Michal Vasko00cbf532020-06-15 13:58:47 +02001631 }
1632 break;
1633 case LYS_ANYDATA:
1634 case LYS_ANYXML:
Michal Vasko366a4a12020-12-04 16:23:57 +01001635 LY_CHECK_GOTO(ret = lyd_create_any(schema, value, value_type, 0, &node), cleanup);
Michal Vasko00cbf532020-06-15 13:58:47 +02001636 break;
1637 default:
1638 LOGINT(ctx);
1639 ret = LY_EINT;
1640 goto cleanup;
1641 }
1642
1643 if (cur_parent) {
1644 /* connect to the parent */
1645 lyd_insert_node(cur_parent, NULL, node);
1646 } else if (parent) {
1647 /* connect to top-level siblings */
1648 lyd_insert_node(NULL, &parent, node);
1649 }
1650
Michal Vasko35880332020-12-08 13:08:12 +01001651next_iter:
Michal Vasko00cbf532020-06-15 13:58:47 +02001652 /* update remembered nodes */
1653 if (!nparent) {
1654 nparent = node;
1655 }
1656 nnode = node;
1657 }
1658
1659cleanup:
1660 lyxp_expr_free(ctx, exp);
Michal Vasko6741dc62021-02-04 09:27:45 +01001661 if (p) {
1662 while (orig_count > LY_ARRAY_COUNT(p)) {
1663 LY_ARRAY_INCREMENT(p);
1664 }
1665 }
Michal Vasko00cbf532020-06-15 13:58:47 +02001666 ly_path_free(ctx, p);
1667 if (!ret) {
1668 /* set out params only on success */
1669 if (new_parent) {
1670 *new_parent = nparent;
1671 }
1672 if (new_node) {
1673 *new_node = nnode;
1674 }
Michal Vaskof4b95ba2020-12-11 08:42:33 +01001675 } else {
1676 lyd_free_tree(nparent);
Michal Vasko00cbf532020-06-15 13:58:47 +02001677 }
1678 return ret;
1679}
1680
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001681LY_ERR
1682lyd_new_implicit_r(struct lyd_node *parent, struct lyd_node **first, const struct lysc_node *sparent,
Radek Krejci1deb5be2020-08-26 16:43:36 +02001683 const struct lys_module *mod, struct ly_set *node_types, struct ly_set *node_when, uint32_t impl_opts,
Radek Krejci0f969882020-08-21 16:56:47 +02001684 struct lyd_node **diff)
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001685{
1686 LY_ERR ret;
Michal Vaskod1e53b92021-01-28 13:11:06 +01001687 const struct lysc_node *iter = NULL;
Radek Krejci2b18bf12020-11-06 11:20:20 +01001688 struct lyd_node *node = NULL;
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001689 struct lyd_value **dflts;
1690 LY_ARRAY_COUNT_TYPE u;
Michal Vasko630d9892020-12-08 17:11:08 +01001691 uint32_t getnext_opts;
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001692
1693 assert(first && (parent || sparent || mod));
1694
1695 if (!sparent && parent) {
1696 sparent = parent->schema;
1697 }
1698
Michal Vasko630d9892020-12-08 17:11:08 +01001699 getnext_opts = LYS_GETNEXT_WITHCHOICE;
1700 if (impl_opts & LYD_IMPLICIT_OUTPUT) {
1701 getnext_opts |= LYS_GETNEXT_OUTPUT;
1702 }
1703
1704 while ((iter = lys_getnext(iter, sparent, mod ? mod->compiled : NULL, getnext_opts))) {
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001705 if ((impl_opts & LYD_IMPLICIT_NO_STATE) && (iter->flags & LYS_CONFIG_R)) {
1706 continue;
Michal Vasko44b19a12020-08-07 09:21:30 +02001707 } else if ((impl_opts & LYD_IMPLICIT_NO_CONFIG) && (iter->flags & LYS_CONFIG_W)) {
1708 continue;
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001709 }
1710
1711 switch (iter->nodetype) {
1712 case LYS_CHOICE:
Michal Vaskobcf4d7d2020-11-18 18:16:49 +01001713 node = lys_getnext_data(NULL, *first, NULL, iter, NULL);
1714 if (!node && ((struct lysc_node_choice *)iter)->dflt) {
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001715 /* create default case data */
Michal Vasko14ed9cd2021-01-28 14:16:25 +01001716 LY_CHECK_RET(lyd_new_implicit_r(parent, first, &((struct lysc_node_choice *)iter)->dflt->node,
Michal Vasko69730152020-10-09 16:30:07 +02001717 NULL, node_types, node_when, impl_opts, diff));
Michal Vaskobcf4d7d2020-11-18 18:16:49 +01001718 } else if (node) {
1719 /* create any default data in the existing case */
1720 assert(node->schema->parent->nodetype == LYS_CASE);
1721 LY_CHECK_RET(lyd_new_implicit_r(parent, first, node->schema->parent, NULL, node_types, node_when,
1722 impl_opts, diff));
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001723 }
1724 break;
1725 case LYS_CONTAINER:
1726 if (!(iter->flags & LYS_PRESENCE) && lyd_find_sibling_val(*first, iter, NULL, 0, NULL)) {
1727 /* create default NP container */
1728 LY_CHECK_RET(lyd_create_inner(iter, &node));
Radek Krejci9a3823e2021-01-27 20:26:46 +01001729 node->flags = LYD_DEFAULT | (lysc_node_when(node->schema) ? LYD_WHEN_TRUE : 0);
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001730 lyd_insert_node(parent, first, node);
1731
Michal Vaskoe16c7b72021-02-26 10:39:06 +01001732 if (lysc_node_when(iter) && node_when) {
1733 /* remember to resolve when */
1734 LY_CHECK_RET(ly_set_add(node_when, node, 1, NULL));
1735 }
Michal Vaskoe49b6322020-11-05 17:38:36 +01001736 if (diff) {
1737 /* add into diff */
1738 LY_CHECK_RET(lyd_val_diff_add(node, LYD_DIFF_OP_CREATE, diff));
1739 }
1740
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001741 /* create any default children */
Michal Vaskoe0665742021-02-11 11:08:44 +01001742 LY_CHECK_RET(lyd_new_implicit_r(node, lyd_node_child_p(node), NULL, NULL, node_types, node_when,
Michal Vasko69730152020-10-09 16:30:07 +02001743 impl_opts, diff));
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001744 }
1745 break;
1746 case LYS_LEAF:
Michal Vasko69730152020-10-09 16:30:07 +02001747 if (!(impl_opts & LYD_IMPLICIT_NO_DEFAULTS) && ((struct lysc_node_leaf *)iter)->dflt &&
1748 lyd_find_sibling_val(*first, iter, NULL, 0, NULL)) {
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001749 /* create default leaf */
1750 ret = lyd_create_term2(iter, ((struct lysc_node_leaf *)iter)->dflt, &node);
1751 if (ret == LY_EINCOMPLETE) {
1752 if (node_types) {
1753 /* remember to resolve type */
Radek Krejci3d92e442020-10-12 12:48:13 +02001754 LY_CHECK_RET(ly_set_add(node_types, node, 1, NULL));
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001755 }
1756 } else if (ret) {
1757 return ret;
1758 }
Radek Krejci9a3823e2021-01-27 20:26:46 +01001759 node->flags = LYD_DEFAULT | (lysc_node_when(node->schema) ? LYD_WHEN_TRUE : 0);
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001760 lyd_insert_node(parent, first, node);
1761
Radek Krejci9a3823e2021-01-27 20:26:46 +01001762 if (lysc_node_when(iter) && node_when) {
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001763 /* remember to resolve when */
Radek Krejci3d92e442020-10-12 12:48:13 +02001764 LY_CHECK_RET(ly_set_add(node_when, node, 1, NULL));
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001765 }
1766 if (diff) {
1767 /* add into diff */
1768 LY_CHECK_RET(lyd_val_diff_add(node, LYD_DIFF_OP_CREATE, diff));
1769 }
1770 }
1771 break;
1772 case LYS_LEAFLIST:
Michal Vasko69730152020-10-09 16:30:07 +02001773 if (!(impl_opts & LYD_IMPLICIT_NO_DEFAULTS) && ((struct lysc_node_leaflist *)iter)->dflts &&
1774 lyd_find_sibling_val(*first, iter, NULL, 0, NULL)) {
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001775 /* create all default leaf-lists */
1776 dflts = ((struct lysc_node_leaflist *)iter)->dflts;
1777 LY_ARRAY_FOR(dflts, u) {
1778 ret = lyd_create_term2(iter, dflts[u], &node);
1779 if (ret == LY_EINCOMPLETE) {
1780 if (node_types) {
1781 /* remember to resolve type */
Radek Krejci3d92e442020-10-12 12:48:13 +02001782 LY_CHECK_RET(ly_set_add(node_types, node, 1, NULL));
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001783 }
1784 } else if (ret) {
1785 return ret;
1786 }
Radek Krejci9a3823e2021-01-27 20:26:46 +01001787 node->flags = LYD_DEFAULT | (lysc_node_when(node->schema) ? LYD_WHEN_TRUE : 0);
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001788 lyd_insert_node(parent, first, node);
1789
Radek Krejci9a3823e2021-01-27 20:26:46 +01001790 if (lysc_node_when(iter) && node_when) {
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001791 /* remember to resolve when */
Radek Krejci3d92e442020-10-12 12:48:13 +02001792 LY_CHECK_RET(ly_set_add(node_when, node, 1, NULL));
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001793 }
1794 if (diff) {
1795 /* add into diff */
1796 LY_CHECK_RET(lyd_val_diff_add(node, LYD_DIFF_OP_CREATE, diff));
1797 }
1798 }
1799 }
1800 break;
1801 default:
1802 /* without defaults */
1803 break;
1804 }
1805 }
1806
1807 return LY_SUCCESS;
1808}
1809
1810API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02001811lyd_new_implicit_tree(struct lyd_node *tree, uint32_t implicit_options, struct lyd_node **diff)
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001812{
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001813 LY_ERR ret = LY_SUCCESS;
Michal Vasko3488ada2020-12-03 14:18:19 +01001814 struct lyd_node *node;
1815 struct ly_set node_when = {0};
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001816
1817 LY_CHECK_ARG_RET(NULL, tree, LY_EINVAL);
1818 if (diff) {
1819 *diff = NULL;
1820 }
1821
Michal Vasko56daf732020-08-10 10:57:18 +02001822 LYD_TREE_DFS_BEGIN(tree, node) {
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001823 /* skip added default nodes */
Michal Vasko69730152020-10-09 16:30:07 +02001824 if (((node->flags & (LYD_DEFAULT | LYD_NEW)) != (LYD_DEFAULT | LYD_NEW)) &&
1825 (node->schema->nodetype & LYD_NODE_INNER)) {
Michal Vaskoe0665742021-02-11 11:08:44 +01001826 LY_CHECK_GOTO(ret = lyd_new_implicit_r(node, lyd_node_child_p(node), NULL, NULL, NULL,
Michal Vasko3488ada2020-12-03 14:18:19 +01001827 &node_when, implicit_options, diff), cleanup);
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001828 }
1829
Michal Vasko56daf732020-08-10 10:57:18 +02001830 LYD_TREE_DFS_END(tree, node);
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001831 }
1832
Michal Vasko3488ada2020-12-03 14:18:19 +01001833 /* resolve when and remove any invalid defaults */
Michal Vaskod3bb12f2020-12-04 14:33:09 +01001834 LY_CHECK_GOTO(ret = lyd_validate_unres(&tree, NULL, &node_when, NULL, NULL, diff), cleanup);
Michal Vasko3488ada2020-12-03 14:18:19 +01001835
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001836cleanup:
Michal Vasko3488ada2020-12-03 14:18:19 +01001837 ly_set_erase(&node_when, NULL);
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001838 if (ret && diff) {
1839 lyd_free_all(*diff);
1840 *diff = NULL;
1841 }
1842 return ret;
1843}
1844
1845API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02001846lyd_new_implicit_all(struct lyd_node **tree, const struct ly_ctx *ctx, uint32_t implicit_options, struct lyd_node **diff)
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001847{
1848 const struct lys_module *mod;
1849 struct lyd_node *d = NULL;
1850 uint32_t i = 0;
1851 LY_ERR ret = LY_SUCCESS;
1852
1853 LY_CHECK_ARG_RET(ctx, tree, *tree || ctx, LY_EINVAL);
1854 if (diff) {
1855 *diff = NULL;
1856 }
1857 if (!ctx) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02001858 ctx = LYD_CTX(*tree);
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001859 }
1860
1861 /* add nodes for each module one-by-one */
1862 while ((mod = ly_ctx_get_module_iter(ctx, &i))) {
1863 if (!mod->implemented) {
1864 continue;
1865 }
1866
1867 LY_CHECK_GOTO(ret = lyd_new_implicit_module(tree, mod, implicit_options, diff ? &d : NULL), cleanup);
1868 if (d) {
1869 /* merge into one diff */
1870 lyd_insert_sibling(*diff, d, diff);
1871
1872 d = NULL;
1873 }
1874 }
1875
1876cleanup:
1877 if (ret && diff) {
1878 lyd_free_all(*diff);
1879 *diff = NULL;
1880 }
1881 return ret;
1882}
1883
1884API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02001885lyd_new_implicit_module(struct lyd_node **tree, const struct lys_module *module, uint32_t implicit_options, struct lyd_node **diff)
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001886{
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001887 LY_ERR ret = LY_SUCCESS;
Michal Vasko3488ada2020-12-03 14:18:19 +01001888 struct lyd_node *root, *d = NULL;
1889 struct ly_set node_when = {0};
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001890
1891 LY_CHECK_ARG_RET(NULL, tree, module, LY_EINVAL);
1892 if (diff) {
1893 *diff = NULL;
1894 }
1895
1896 /* add all top-level defaults for this module */
Michal Vasko3488ada2020-12-03 14:18:19 +01001897 LY_CHECK_GOTO(ret = lyd_new_implicit_r(NULL, tree, NULL, module, NULL, &node_when, implicit_options, diff), cleanup);
1898
1899 /* resolve when and remove any invalid defaults */
Michal Vaskod3bb12f2020-12-04 14:33:09 +01001900 LY_CHECK_GOTO(ret = lyd_validate_unres(tree, module, &node_when, NULL, NULL, diff), cleanup);
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001901
1902 /* process nested nodes */
1903 LY_LIST_FOR(*tree, root) {
1904 /* skip added default nodes */
1905 if ((root->flags & (LYD_DEFAULT | LYD_NEW)) != (LYD_DEFAULT | LYD_NEW)) {
1906 LY_CHECK_GOTO(ret = lyd_new_implicit_tree(root, implicit_options, diff ? &d : NULL), cleanup);
1907
1908 if (d) {
1909 /* merge into one diff */
1910 lyd_insert_sibling(*diff, d, diff);
1911
1912 d = NULL;
1913 }
1914 }
1915 }
1916
1917cleanup:
Michal Vasko3488ada2020-12-03 14:18:19 +01001918 ly_set_erase(&node_when, NULL);
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001919 if (ret && diff) {
1920 lyd_free_all(*diff);
1921 *diff = NULL;
1922 }
1923 return ret;
1924}
1925
Michal Vasko90932a92020-02-12 14:33:03 +01001926struct lyd_node *
Michal Vaskob104f112020-07-17 09:54:54 +02001927lyd_insert_get_next_anchor(const struct lyd_node *first_sibling, const struct lyd_node *new_node)
Michal Vasko90932a92020-02-12 14:33:03 +01001928{
Michal Vaskob104f112020-07-17 09:54:54 +02001929 const struct lysc_node *schema, *sparent;
Michal Vasko90932a92020-02-12 14:33:03 +01001930 struct lyd_node *match = NULL;
Radek Krejci857189e2020-09-01 13:26:36 +02001931 ly_bool found;
Michal Vasko93b16062020-12-09 18:14:18 +01001932 uint32_t getnext_opts;
Michal Vasko90932a92020-02-12 14:33:03 +01001933
Michal Vaskob104f112020-07-17 09:54:54 +02001934 assert(new_node);
1935
1936 if (!first_sibling || !new_node->schema) {
1937 /* insert at the end, no next anchor */
Michal Vasko90932a92020-02-12 14:33:03 +01001938 return NULL;
1939 }
1940
Michal Vasko93b16062020-12-09 18:14:18 +01001941 getnext_opts = 0;
Michal Vaskod1e53b92021-01-28 13:11:06 +01001942 if (new_node->schema->flags & LYS_IS_OUTPUT) {
Michal Vasko93b16062020-12-09 18:14:18 +01001943 getnext_opts = LYS_GETNEXT_OUTPUT;
1944 }
1945
Michal Vaskob104f112020-07-17 09:54:54 +02001946 if (first_sibling->parent && first_sibling->parent->children_ht) {
1947 /* find the anchor using hashes */
1948 sparent = first_sibling->parent->schema;
Michal Vasko93b16062020-12-09 18:14:18 +01001949 schema = lys_getnext(new_node->schema, sparent, NULL, getnext_opts);
Michal Vaskob104f112020-07-17 09:54:54 +02001950 while (schema) {
1951 /* keep trying to find the first existing instance of the closest following schema sibling,
1952 * otherwise return NULL - inserting at the end */
1953 if (!lyd_find_sibling_schema(first_sibling, schema, &match)) {
1954 break;
1955 }
1956
Michal Vasko93b16062020-12-09 18:14:18 +01001957 schema = lys_getnext(schema, sparent, NULL, getnext_opts);
Michal Vaskob104f112020-07-17 09:54:54 +02001958 }
1959 } else {
1960 /* find the anchor without hashes */
1961 match = (struct lyd_node *)first_sibling;
1962 if (!lysc_data_parent(new_node->schema)) {
1963 /* we are in top-level, skip all the data from preceding modules */
1964 LY_LIST_FOR(match, match) {
1965 if (!match->schema || (strcmp(lyd_owner_module(match)->name, lyd_owner_module(new_node)->name) >= 0)) {
1966 break;
1967 }
1968 }
1969 }
1970
1971 /* get the first schema sibling */
1972 sparent = lysc_data_parent(new_node->schema);
Michal Vasko93b16062020-12-09 18:14:18 +01001973 schema = lys_getnext(NULL, sparent, new_node->schema->module->compiled, getnext_opts);
Michal Vaskob104f112020-07-17 09:54:54 +02001974
1975 found = 0;
1976 LY_LIST_FOR(match, match) {
1977 if (!match->schema || (lyd_owner_module(match) != lyd_owner_module(new_node))) {
1978 /* we have found an opaque node, which must be at the end, so use it OR
1979 * modules do not match, so we must have traversed all the data from new_node module (if any),
1980 * we have found the first node of the next module, that is what we want */
1981 break;
1982 }
1983
1984 /* skip schema nodes until we find the instantiated one */
1985 while (!found) {
1986 if (new_node->schema == schema) {
1987 /* we have found the schema of the new node, continue search to find the first
1988 * data node with a different schema (after our schema) */
1989 found = 1;
1990 break;
1991 }
1992 if (match->schema == schema) {
1993 /* current node (match) is a data node still before the new node, continue search in data */
1994 break;
1995 }
Michal Vasko93b16062020-12-09 18:14:18 +01001996 schema = lys_getnext(schema, sparent, new_node->schema->module->compiled, getnext_opts);
Michal Vaskob104f112020-07-17 09:54:54 +02001997 assert(schema);
1998 }
1999
2000 if (found && (match->schema != new_node->schema)) {
2001 /* find the next node after we have found our node schema data instance */
2002 break;
2003 }
2004 }
Michal Vasko90932a92020-02-12 14:33:03 +01002005 }
2006
2007 return match;
2008}
2009
2010/**
2011 * @brief Insert node after a sibling.
2012 *
Michal Vasko9f96a052020-03-10 09:41:45 +01002013 * Handles inserting into NP containers and key-less lists.
2014 *
Michal Vasko90932a92020-02-12 14:33:03 +01002015 * @param[in] sibling Sibling to insert after.
2016 * @param[in] node Node to insert.
2017 */
2018static void
Michal Vaskof03ed032020-03-04 13:31:44 +01002019lyd_insert_after_node(struct lyd_node *sibling, struct lyd_node *node)
Michal Vasko90932a92020-02-12 14:33:03 +01002020{
Michal Vasko0249f7c2020-03-05 16:36:40 +01002021 struct lyd_node_inner *par;
2022
Michal Vasko90932a92020-02-12 14:33:03 +01002023 assert(!node->next && (node->prev == node));
2024
2025 node->next = sibling->next;
2026 node->prev = sibling;
2027 sibling->next = node;
2028 if (node->next) {
2029 /* sibling had a succeeding node */
2030 node->next->prev = node;
2031 } else {
2032 /* sibling was last, find first sibling and change its prev */
2033 if (sibling->parent) {
2034 sibling = sibling->parent->child;
2035 } else {
Michal Vaskod989ba02020-08-24 10:59:24 +02002036 for ( ; sibling->prev->next != node; sibling = sibling->prev) {}
Michal Vasko90932a92020-02-12 14:33:03 +01002037 }
2038 sibling->prev = node;
2039 }
2040 node->parent = sibling->parent;
Michal Vasko0249f7c2020-03-05 16:36:40 +01002041
Michal Vasko9f96a052020-03-10 09:41:45 +01002042 for (par = node->parent; par; par = par->parent) {
2043 if ((par->flags & LYD_DEFAULT) && !(node->flags & LYD_DEFAULT)) {
2044 /* remove default flags from NP containers */
Michal Vasko0249f7c2020-03-05 16:36:40 +01002045 par->flags &= ~LYD_DEFAULT;
2046 }
Michal Vaskob104f112020-07-17 09:54:54 +02002047 if (par->schema && (par->schema->nodetype == LYS_LIST) && (par->schema->flags & LYS_KEYLESS)) {
Michal Vasko9f96a052020-03-10 09:41:45 +01002048 /* rehash key-less list */
Michal Vasko9e685082021-01-29 14:49:09 +01002049 lyd_hash(&par->node);
Michal Vasko9f96a052020-03-10 09:41:45 +01002050 }
Michal Vasko0249f7c2020-03-05 16:36:40 +01002051 }
Michal Vasko90932a92020-02-12 14:33:03 +01002052}
2053
2054/**
2055 * @brief Insert node before a sibling.
2056 *
Michal Vasko9f96a052020-03-10 09:41:45 +01002057 * Handles inserting into NP containers and key-less lists.
2058 *
Michal Vasko90932a92020-02-12 14:33:03 +01002059 * @param[in] sibling Sibling to insert before.
2060 * @param[in] node Node to insert.
2061 */
2062static void
Michal Vaskof03ed032020-03-04 13:31:44 +01002063lyd_insert_before_node(struct lyd_node *sibling, struct lyd_node *node)
Michal Vasko90932a92020-02-12 14:33:03 +01002064{
Michal Vasko0249f7c2020-03-05 16:36:40 +01002065 struct lyd_node_inner *par;
2066
Michal Vasko90932a92020-02-12 14:33:03 +01002067 assert(!node->next && (node->prev == node));
2068
2069 node->next = sibling;
2070 /* covers situation of sibling being first */
2071 node->prev = sibling->prev;
2072 sibling->prev = node;
2073 if (node->prev->next) {
2074 /* sibling had a preceding node */
2075 node->prev->next = node;
2076 } else if (sibling->parent) {
2077 /* sibling was first and we must also change parent child pointer */
2078 sibling->parent->child = node;
2079 }
2080 node->parent = sibling->parent;
Michal Vasko0249f7c2020-03-05 16:36:40 +01002081
Michal Vasko9f96a052020-03-10 09:41:45 +01002082 for (par = node->parent; par; par = par->parent) {
2083 if ((par->flags & LYD_DEFAULT) && !(node->flags & LYD_DEFAULT)) {
2084 /* remove default flags from NP containers */
Michal Vasko0249f7c2020-03-05 16:36:40 +01002085 par->flags &= ~LYD_DEFAULT;
2086 }
Michal Vaskob104f112020-07-17 09:54:54 +02002087 if (par->schema && (par->schema->nodetype == LYS_LIST) && (par->schema->flags & LYS_KEYLESS)) {
Michal Vasko9f96a052020-03-10 09:41:45 +01002088 /* rehash key-less list */
Michal Vasko9e685082021-01-29 14:49:09 +01002089 lyd_hash(&par->node);
Michal Vasko9f96a052020-03-10 09:41:45 +01002090 }
Michal Vasko0249f7c2020-03-05 16:36:40 +01002091 }
Michal Vasko90932a92020-02-12 14:33:03 +01002092}
2093
2094/**
Michal Vaskob104f112020-07-17 09:54:54 +02002095 * @brief Insert node as the first and only child of a parent.
Michal Vasko90932a92020-02-12 14:33:03 +01002096 *
Michal Vasko9f96a052020-03-10 09:41:45 +01002097 * Handles inserting into NP containers and key-less lists.
2098 *
Michal Vasko90932a92020-02-12 14:33:03 +01002099 * @param[in] parent Parent to insert into.
2100 * @param[in] node Node to insert.
2101 */
2102static void
Michal Vaskob104f112020-07-17 09:54:54 +02002103lyd_insert_only_child(struct lyd_node *parent, struct lyd_node *node)
Michal Vasko90932a92020-02-12 14:33:03 +01002104{
2105 struct lyd_node_inner *par;
2106
Radek Krejcia1c1e542020-09-29 16:06:52 +02002107 assert(parent && !lyd_child(parent) && !node->next && (node->prev == node));
Michal Vasko52927e22020-03-16 17:26:14 +01002108 assert(!parent->schema || (parent->schema->nodetype & LYD_NODE_INNER));
Michal Vasko90932a92020-02-12 14:33:03 +01002109
2110 par = (struct lyd_node_inner *)parent;
2111
Michal Vaskob104f112020-07-17 09:54:54 +02002112 par->child = node;
Michal Vasko90932a92020-02-12 14:33:03 +01002113 node->parent = par;
Michal Vasko0249f7c2020-03-05 16:36:40 +01002114
Michal Vaskod989ba02020-08-24 10:59:24 +02002115 for ( ; par; par = par->parent) {
Michal Vasko9f96a052020-03-10 09:41:45 +01002116 if ((par->flags & LYD_DEFAULT) && !(node->flags & LYD_DEFAULT)) {
2117 /* remove default flags from NP containers */
Michal Vasko0249f7c2020-03-05 16:36:40 +01002118 par->flags &= ~LYD_DEFAULT;
2119 }
Michal Vasko52927e22020-03-16 17:26:14 +01002120 if (par->schema && (par->schema->nodetype == LYS_LIST) && (par->schema->flags & LYS_KEYLESS)) {
Michal Vasko9f96a052020-03-10 09:41:45 +01002121 /* rehash key-less list */
Michal Vasko9e685082021-01-29 14:49:09 +01002122 lyd_hash(&par->node);
Michal Vasko9f96a052020-03-10 09:41:45 +01002123 }
Michal Vasko0249f7c2020-03-05 16:36:40 +01002124 }
Michal Vasko751cb4d2020-07-14 12:25:28 +02002125}
Michal Vasko0249f7c2020-03-05 16:36:40 +01002126
Michal Vasko751cb4d2020-07-14 12:25:28 +02002127/**
2128 * @brief Learn whether a list instance has all the keys.
2129 *
2130 * @param[in] list List instance to check.
2131 * @return non-zero if all the keys were found,
2132 * @return 0 otherwise.
2133 */
2134static int
2135lyd_insert_has_keys(const struct lyd_node *list)
2136{
2137 const struct lyd_node *key;
2138 const struct lysc_node *skey = NULL;
2139
2140 assert(list->schema->nodetype == LYS_LIST);
Radek Krejcia1c1e542020-09-29 16:06:52 +02002141 key = lyd_child(list);
Michal Vasko751cb4d2020-07-14 12:25:28 +02002142 while ((skey = lys_getnext(skey, list->schema, NULL, 0)) && (skey->flags & LYS_KEY)) {
2143 if (!key || (key->schema != skey)) {
2144 /* key missing */
2145 return 0;
2146 }
2147
2148 key = key->next;
2149 }
2150
2151 /* all keys found */
2152 return 1;
Michal Vasko90932a92020-02-12 14:33:03 +01002153}
2154
2155void
Michal Vaskob104f112020-07-17 09:54:54 +02002156lyd_insert_node(struct lyd_node *parent, struct lyd_node **first_sibling_p, struct lyd_node *node)
Michal Vasko90932a92020-02-12 14:33:03 +01002157{
Michal Vaskob104f112020-07-17 09:54:54 +02002158 struct lyd_node *anchor, *first_sibling;
Michal Vasko90932a92020-02-12 14:33:03 +01002159
Michal Vaskob104f112020-07-17 09:54:54 +02002160 /* inserting list without its keys is not supported */
2161 assert((parent || first_sibling_p) && node && (node->hash || !node->schema));
Michal Vaskof756c942020-11-06 17:21:37 +01002162 assert(!parent || !parent->schema ||
2163 (parent->schema->nodetype & (LYS_CONTAINER | LYS_LIST | LYS_RPC | LYS_ACTION | LYS_NOTIF)));
Michal Vasko9b368d32020-02-14 13:53:31 +01002164
Michal Vaskob104f112020-07-17 09:54:54 +02002165 if (!parent && first_sibling_p && (*first_sibling_p) && (*first_sibling_p)->parent) {
Michal Vasko9e685082021-01-29 14:49:09 +01002166 parent = lyd_parent(*first_sibling_p);
Michal Vasko9b368d32020-02-14 13:53:31 +01002167 }
Michal Vasko90932a92020-02-12 14:33:03 +01002168
Michal Vaskob104f112020-07-17 09:54:54 +02002169 /* get first sibling */
Michal Vasko9e685082021-01-29 14:49:09 +01002170 first_sibling = parent ? lyd_child(parent) : *first_sibling_p;
Michal Vasko9f96a052020-03-10 09:41:45 +01002171
Michal Vaskob104f112020-07-17 09:54:54 +02002172 /* find the anchor, our next node, so we can insert before it */
2173 anchor = lyd_insert_get_next_anchor(first_sibling, node);
2174 if (anchor) {
2175 lyd_insert_before_node(anchor, node);
Michal Vasko26123192020-11-09 21:02:34 +01002176 if (!parent && (*first_sibling_p == anchor)) {
2177 /* move first sibling */
2178 *first_sibling_p = node;
2179 }
Michal Vaskob104f112020-07-17 09:54:54 +02002180 } else if (first_sibling) {
2181 lyd_insert_after_node(first_sibling->prev, node);
2182 } else if (parent) {
2183 lyd_insert_only_child(parent, node);
Michal Vasko90932a92020-02-12 14:33:03 +01002184 } else {
Michal Vaskob104f112020-07-17 09:54:54 +02002185 *first_sibling_p = node;
2186 }
2187
2188 /* insert into parent HT */
2189 lyd_insert_hash(node);
2190
2191 /* finish hashes for our parent, if needed and possible */
Michal Vasko9e8de2d2020-09-01 08:17:10 +02002192 if (node->schema && (node->schema->flags & LYS_KEY) && parent && lyd_insert_has_keys(parent)) {
Michal Vaskob104f112020-07-17 09:54:54 +02002193 lyd_hash(parent);
2194
2195 /* now we can insert even the list into its parent HT */
2196 lyd_insert_hash(parent);
Michal Vasko90932a92020-02-12 14:33:03 +01002197 }
Michal Vasko90932a92020-02-12 14:33:03 +01002198}
2199
Michal Vasko717a4c32020-12-07 09:40:10 +01002200/**
2201 * @brief Check schema place of a node to be inserted.
2202 *
2203 * @param[in] parent Schema node of the parent data node.
2204 * @param[in] sibling Schema node of a sibling data node.
2205 * @param[in] schema Schema node if the data node to be inserted.
2206 * @return LY_SUCCESS on success.
2207 * @return LY_EINVAL if the place is invalid.
2208 */
Michal Vaskof03ed032020-03-04 13:31:44 +01002209static LY_ERR
Michal Vasko717a4c32020-12-07 09:40:10 +01002210lyd_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 +01002211{
2212 const struct lysc_node *par2;
2213
Michal Vasko62ed12d2020-05-21 10:08:25 +02002214 assert(!parent || !(parent->nodetype & (LYS_CASE | LYS_CHOICE)));
Michal Vasko717a4c32020-12-07 09:40:10 +01002215 assert(!sibling || !(sibling->nodetype & (LYS_CASE | LYS_CHOICE)));
2216 assert(!schema || !(schema->nodetype & (LYS_CASE | LYS_CHOICE)));
Michal Vaskof03ed032020-03-04 13:31:44 +01002217
Michal Vasko717a4c32020-12-07 09:40:10 +01002218 if (!schema || (!parent && !sibling)) {
Michal Vasko71e795e2020-12-04 16:27:37 +01002219 /* opaque nodes can be inserted wherever */
2220 return LY_SUCCESS;
2221 }
2222
Michal Vasko717a4c32020-12-07 09:40:10 +01002223 if (!parent) {
2224 parent = lysc_data_parent(sibling);
2225 }
2226
Michal Vaskof03ed032020-03-04 13:31:44 +01002227 /* find schema parent */
Michal Vasko62ed12d2020-05-21 10:08:25 +02002228 par2 = lysc_data_parent(schema);
Michal Vaskof03ed032020-03-04 13:31:44 +01002229
2230 if (parent) {
2231 /* inner node */
2232 if (par2 != parent) {
Michal Vaskob104f112020-07-17 09:54:54 +02002233 LOGERR(schema->module->ctx, LY_EINVAL, "Cannot insert, parent of \"%s\" is not \"%s\".", schema->name,
Michal Vasko69730152020-10-09 16:30:07 +02002234 parent->name);
Michal Vaskof03ed032020-03-04 13:31:44 +01002235 return LY_EINVAL;
2236 }
2237 } else {
2238 /* top-level node */
2239 if (par2) {
Radek Krejcif6d14cb2020-07-02 16:11:45 +02002240 LOGERR(schema->module->ctx, LY_EINVAL, "Cannot insert, node \"%s\" is not top-level.", schema->name);
Michal Vaskof03ed032020-03-04 13:31:44 +01002241 return LY_EINVAL;
2242 }
2243 }
2244
2245 return LY_SUCCESS;
2246}
2247
2248API LY_ERR
Michal Vaskob104f112020-07-17 09:54:54 +02002249lyd_insert_child(struct lyd_node *parent, struct lyd_node *node)
Michal Vaskof03ed032020-03-04 13:31:44 +01002250{
2251 struct lyd_node *iter;
2252
Michal Vasko0ab974d2021-02-24 13:18:26 +01002253 LY_CHECK_ARG_RET(NULL, parent, node, !parent->schema || (parent->schema->nodetype & LYD_NODE_INNER), LY_EINVAL);
Michal Vaskof03ed032020-03-04 13:31:44 +01002254
Michal Vasko717a4c32020-12-07 09:40:10 +01002255 LY_CHECK_RET(lyd_insert_check_schema(parent->schema, NULL, node->schema));
Michal Vaskof03ed032020-03-04 13:31:44 +01002256
Michal Vasko0ab974d2021-02-24 13:18:26 +01002257 if (node->schema && (node->schema->flags & LYS_KEY)) {
Michal Vaskof03ed032020-03-04 13:31:44 +01002258 LOGERR(parent->schema->module->ctx, LY_EINVAL, "Cannot insert key \"%s\".", node->schema->name);
2259 return LY_EINVAL;
2260 }
2261
2262 if (node->parent || node->prev->next) {
2263 lyd_unlink_tree(node);
2264 }
2265
2266 while (node) {
2267 iter = node->next;
2268 lyd_unlink_tree(node);
2269 lyd_insert_node(parent, NULL, node);
2270 node = iter;
2271 }
2272 return LY_SUCCESS;
2273}
2274
2275API LY_ERR
Michal Vaskob104f112020-07-17 09:54:54 +02002276lyd_insert_sibling(struct lyd_node *sibling, struct lyd_node *node, struct lyd_node **first)
Michal Vaskob1b5c262020-03-05 14:29:47 +01002277{
2278 struct lyd_node *iter;
2279
Michal Vaskob104f112020-07-17 09:54:54 +02002280 LY_CHECK_ARG_RET(NULL, node, LY_EINVAL);
Michal Vaskob1b5c262020-03-05 14:29:47 +01002281
Michal Vaskob104f112020-07-17 09:54:54 +02002282 if (sibling) {
Michal Vasko717a4c32020-12-07 09:40:10 +01002283 LY_CHECK_RET(lyd_insert_check_schema(NULL, sibling->schema, node->schema));
Michal Vaskob1b5c262020-03-05 14:29:47 +01002284 }
2285
Michal Vasko553d0b52020-12-04 16:27:52 +01002286 if (sibling == node) {
2287 /* we need to keep the connection to siblings so we can insert into them */
2288 sibling = sibling->prev;
2289 }
2290
Michal Vaskob1b5c262020-03-05 14:29:47 +01002291 if (node->parent || node->prev->next) {
2292 lyd_unlink_tree(node);
2293 }
2294
2295 while (node) {
Michal Vasko71e795e2020-12-04 16:27:37 +01002296 if (lysc_is_key(node->schema)) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02002297 LOGERR(LYD_CTX(node), LY_EINVAL, "Cannot insert key \"%s\".", node->schema->name);
Michal Vaskob104f112020-07-17 09:54:54 +02002298 return LY_EINVAL;
2299 }
2300
Michal Vaskob1b5c262020-03-05 14:29:47 +01002301 iter = node->next;
2302 lyd_unlink_tree(node);
2303 lyd_insert_node(NULL, &sibling, node);
2304 node = iter;
2305 }
Michal Vaskob1b5c262020-03-05 14:29:47 +01002306
Michal Vaskob104f112020-07-17 09:54:54 +02002307 if (first) {
2308 /* find the first sibling */
2309 *first = sibling;
2310 while ((*first)->prev->next) {
2311 *first = (*first)->prev;
Michal Vasko0249f7c2020-03-05 16:36:40 +01002312 }
2313 }
2314
2315 return LY_SUCCESS;
2316}
2317
Michal Vaskob1b5c262020-03-05 14:29:47 +01002318API LY_ERR
Michal Vaskof03ed032020-03-04 13:31:44 +01002319lyd_insert_before(struct lyd_node *sibling, struct lyd_node *node)
2320{
Michal Vaskof03ed032020-03-04 13:31:44 +01002321 LY_CHECK_ARG_RET(NULL, sibling, node, LY_EINVAL);
2322
Michal Vasko717a4c32020-12-07 09:40:10 +01002323 LY_CHECK_RET(lyd_insert_check_schema(NULL, sibling->schema, node->schema));
Michal Vaskof03ed032020-03-04 13:31:44 +01002324
Michal Vaskob104f112020-07-17 09:54:54 +02002325 if (!(node->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) || !(node->schema->flags & LYS_ORDBY_USER)) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02002326 LOGERR(LYD_CTX(sibling), LY_EINVAL, "Can be used only for user-ordered nodes.");
Michal Vaskof03ed032020-03-04 13:31:44 +01002327 return LY_EINVAL;
2328 }
2329
Michal Vasko4bc2ce32020-12-08 10:06:16 +01002330 lyd_unlink_tree(node);
2331 lyd_insert_before_node(sibling, node);
2332 lyd_insert_hash(node);
Michal Vaskof03ed032020-03-04 13:31:44 +01002333
Michal Vaskof03ed032020-03-04 13:31:44 +01002334 return LY_SUCCESS;
2335}
2336
2337API LY_ERR
2338lyd_insert_after(struct lyd_node *sibling, struct lyd_node *node)
2339{
Michal Vaskof03ed032020-03-04 13:31:44 +01002340 LY_CHECK_ARG_RET(NULL, sibling, node, LY_EINVAL);
2341
Michal Vasko717a4c32020-12-07 09:40:10 +01002342 LY_CHECK_RET(lyd_insert_check_schema(NULL, sibling->schema, node->schema));
Michal Vaskof03ed032020-03-04 13:31:44 +01002343
Michal Vaskob104f112020-07-17 09:54:54 +02002344 if (!(node->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) || !(node->schema->flags & LYS_ORDBY_USER)) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02002345 LOGERR(LYD_CTX(sibling), LY_EINVAL, "Can be used only for user-ordered nodes.");
Michal Vaskof03ed032020-03-04 13:31:44 +01002346 return LY_EINVAL;
2347 }
2348
Michal Vasko4bc2ce32020-12-08 10:06:16 +01002349 lyd_unlink_tree(node);
2350 lyd_insert_after_node(sibling, node);
2351 lyd_insert_hash(node);
Michal Vaskof03ed032020-03-04 13:31:44 +01002352
Michal Vaskof03ed032020-03-04 13:31:44 +01002353 return LY_SUCCESS;
2354}
2355
2356API void
2357lyd_unlink_tree(struct lyd_node *node)
2358{
2359 struct lyd_node *iter;
2360
2361 if (!node) {
2362 return;
2363 }
2364
Michal Vaskob104f112020-07-17 09:54:54 +02002365 /* update hashes while still linked into the tree */
2366 lyd_unlink_hash(node);
2367
Michal Vaskof03ed032020-03-04 13:31:44 +01002368 /* unlink from siblings */
2369 if (node->prev->next) {
2370 node->prev->next = node->next;
2371 }
2372 if (node->next) {
2373 node->next->prev = node->prev;
2374 } else {
2375 /* unlinking the last node */
2376 if (node->parent) {
2377 iter = node->parent->child;
2378 } else {
2379 iter = node->prev;
2380 while (iter->prev != node) {
2381 iter = iter->prev;
2382 }
2383 }
2384 /* update the "last" pointer from the first node */
2385 iter->prev = node->prev;
2386 }
2387
2388 /* unlink from parent */
2389 if (node->parent) {
2390 if (node->parent->child == node) {
2391 /* the node is the first child */
2392 node->parent->child = node->next;
2393 }
2394
Michal Vaskoab49dbe2020-07-17 12:32:47 +02002395 /* check for NP container whether its last non-default node is not being unlinked */
Michal Vasko69730152020-10-09 16:30:07 +02002396 if (node->parent->schema && (node->parent->schema->nodetype == LYS_CONTAINER) &&
2397 !(node->parent->flags & LYD_DEFAULT) && !(node->parent->schema->flags & LYS_PRESENCE)) {
Michal Vaskoab49dbe2020-07-17 12:32:47 +02002398 LY_LIST_FOR(node->parent->child, iter) {
2399 if ((iter != node) && !(iter->flags & LYD_DEFAULT)) {
2400 break;
2401 }
2402 }
2403 if (!iter) {
2404 node->parent->flags |= LYD_DEFAULT;
2405 }
2406 }
2407
Michal Vaskof03ed032020-03-04 13:31:44 +01002408 /* check for keyless list and update its hash */
Michal Vasko9e685082021-01-29 14:49:09 +01002409 for (iter = lyd_parent(node); iter; iter = lyd_parent(iter)) {
Michal Vasko413c7f22020-05-05 12:34:06 +02002410 if (iter->schema && (iter->schema->flags & LYS_KEYLESS)) {
Michal Vasko47718292021-02-26 10:12:44 +01002411 lyd_unlink_hash(iter);
Michal Vaskof03ed032020-03-04 13:31:44 +01002412 lyd_hash(iter);
Michal Vasko47718292021-02-26 10:12:44 +01002413 lyd_insert_hash(iter);
Michal Vaskof03ed032020-03-04 13:31:44 +01002414 }
2415 }
2416
2417 node->parent = NULL;
2418 }
2419
2420 node->next = NULL;
2421 node->prev = node;
2422}
2423
Michal Vaskoa5da3292020-08-12 13:10:50 +02002424void
Michal Vasko871a0252020-11-11 18:35:24 +01002425lyd_insert_meta(struct lyd_node *parent, struct lyd_meta *meta, ly_bool clear_dflt)
Radek Krejci1798aae2020-07-14 13:26:06 +02002426{
2427 struct lyd_meta *last, *iter;
2428
2429 assert(parent);
Michal Vaskoa5da3292020-08-12 13:10:50 +02002430
2431 if (!meta) {
2432 return;
2433 }
Radek Krejci1798aae2020-07-14 13:26:06 +02002434
2435 for (iter = meta; iter; iter = iter->next) {
2436 iter->parent = parent;
2437 }
2438
2439 /* insert as the last attribute */
2440 if (parent->meta) {
Radek Krejci1e008d22020-08-17 11:37:37 +02002441 for (last = parent->meta; last->next; last = last->next) {}
Radek Krejci1798aae2020-07-14 13:26:06 +02002442 last->next = meta;
2443 } else {
2444 parent->meta = meta;
2445 }
2446
2447 /* remove default flags from NP containers */
Michal Vasko871a0252020-11-11 18:35:24 +01002448 while (clear_dflt && parent && (parent->schema->nodetype == LYS_CONTAINER) && (parent->flags & LYD_DEFAULT)) {
Radek Krejci1798aae2020-07-14 13:26:06 +02002449 parent->flags &= ~LYD_DEFAULT;
Michal Vasko9e685082021-01-29 14:49:09 +01002450 parent = lyd_parent(parent);
Radek Krejci1798aae2020-07-14 13:26:06 +02002451 }
Radek Krejci1798aae2020-07-14 13:26:06 +02002452}
2453
2454LY_ERR
Michal Vasko9f96a052020-03-10 09:41:45 +01002455lyd_create_meta(struct lyd_node *parent, struct lyd_meta **meta, const struct lys_module *mod, const char *name,
Michal Vaskofeca4fb2020-10-05 08:58:40 +02002456 size_t name_len, const char *value, size_t value_len, ly_bool *dynamic, LY_PREFIX_FORMAT format,
Michal Vasko871a0252020-11-11 18:35:24 +01002457 void *prefix_data, uint32_t hints, ly_bool clear_dflt, ly_bool *incomplete)
Michal Vasko90932a92020-02-12 14:33:03 +01002458{
Radek Krejci2efc45b2020-12-22 16:25:44 +01002459 LY_ERR ret = LY_SUCCESS;
Michal Vasko90932a92020-02-12 14:33:03 +01002460 struct lysc_ext_instance *ant = NULL;
Michal Vasko9f96a052020-03-10 09:41:45 +01002461 struct lyd_meta *mt, *last;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002462 LY_ARRAY_COUNT_TYPE u;
Michal Vasko90932a92020-02-12 14:33:03 +01002463
Michal Vasko9f96a052020-03-10 09:41:45 +01002464 assert((parent || meta) && mod);
Michal Vasko6f4cbb62020-02-28 11:15:47 +01002465
Radek Krejciddace2c2021-01-08 11:30:56 +01002466 LOG_LOCSET(parent ? parent->schema : NULL, parent, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +01002467
Radek Krejci7eb54ba2020-05-18 16:30:04 +02002468 LY_ARRAY_FOR(mod->compiled->exts, u) {
Michal Vasko69730152020-10-09 16:30:07 +02002469 if ((mod->compiled->exts[u].def->plugin == lyext_plugins_internal[LYEXT_PLUGIN_INTERNAL_ANNOTATION].plugin) &&
Radek Krejci7eb54ba2020-05-18 16:30:04 +02002470 !ly_strncmp(mod->compiled->exts[u].argument, name, name_len)) {
Michal Vasko90932a92020-02-12 14:33:03 +01002471 /* we have the annotation definition */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02002472 ant = &mod->compiled->exts[u];
Michal Vasko90932a92020-02-12 14:33:03 +01002473 break;
2474 }
2475 }
2476 if (!ant) {
2477 /* attribute is not defined as a metadata annotation (RFC 7952) */
Radek Krejci2efc45b2020-12-22 16:25:44 +01002478 LOGVAL(mod->ctx, LYVE_REFERENCE, "Annotation definition for attribute \"%s:%.*s\" not found.",
Michal Vasko90932a92020-02-12 14:33:03 +01002479 mod->name, name_len, name);
Radek Krejci2efc45b2020-12-22 16:25:44 +01002480 ret = LY_EINVAL;
2481 goto cleanup;
Michal Vasko90932a92020-02-12 14:33:03 +01002482 }
2483
Michal Vasko9f96a052020-03-10 09:41:45 +01002484 mt = calloc(1, sizeof *mt);
Radek Krejci2efc45b2020-12-22 16:25:44 +01002485 LY_CHECK_ERR_GOTO(!mt, LOGMEM(mod->ctx); ret = LY_EMEM, cleanup);
Michal Vasko9f96a052020-03-10 09:41:45 +01002486 mt->parent = parent;
2487 mt->annotation = ant;
Radek Krejci1b2eef82021-02-17 11:17:27 +01002488 ret = lyd_value_store(mod->ctx, &mt->value, *(const struct lysc_type **)ant->substmts[ANNOTATION_SUBSTMT_TYPE].storage,
2489 value, value_len, dynamic, format, prefix_data, hints, parent ? parent->schema : NULL, incomplete);
Radek Krejci2efc45b2020-12-22 16:25:44 +01002490 LY_CHECK_ERR_GOTO(ret, free(mt), cleanup);
2491 ret = lydict_insert(mod->ctx, name, name_len, &mt->name);
2492 LY_CHECK_ERR_GOTO(ret, free(mt), cleanup);
Michal Vasko90932a92020-02-12 14:33:03 +01002493
Michal Vasko6f4cbb62020-02-28 11:15:47 +01002494 /* insert as the last attribute */
2495 if (parent) {
Michal Vasko871a0252020-11-11 18:35:24 +01002496 lyd_insert_meta(parent, mt, clear_dflt);
Michal Vasko9f96a052020-03-10 09:41:45 +01002497 } else if (*meta) {
Radek Krejci1e008d22020-08-17 11:37:37 +02002498 for (last = *meta; last->next; last = last->next) {}
Michal Vasko9f96a052020-03-10 09:41:45 +01002499 last->next = mt;
Michal Vasko90932a92020-02-12 14:33:03 +01002500 }
2501
Michal Vasko9f96a052020-03-10 09:41:45 +01002502 if (meta) {
2503 *meta = mt;
Michal Vasko90932a92020-02-12 14:33:03 +01002504 }
Radek Krejci2efc45b2020-12-22 16:25:44 +01002505
2506cleanup:
Radek Krejciddace2c2021-01-08 11:30:56 +01002507 LOG_LOCBACK((parent && parent->schema) ? 1 : 0, parent ? 1 : 0, 0, 0);
Radek Krejci2efc45b2020-12-22 16:25:44 +01002508 return ret;
Michal Vasko90932a92020-02-12 14:33:03 +01002509}
2510
Michal Vaskoa5da3292020-08-12 13:10:50 +02002511void
2512lyd_insert_attr(struct lyd_node *parent, struct lyd_attr *attr)
2513{
2514 struct lyd_attr *last, *iter;
2515 struct lyd_node_opaq *opaq;
2516
2517 assert(parent && !parent->schema);
2518
2519 if (!attr) {
2520 return;
2521 }
2522
2523 opaq = (struct lyd_node_opaq *)parent;
2524 for (iter = attr; iter; iter = iter->next) {
2525 iter->parent = opaq;
2526 }
2527
2528 /* insert as the last attribute */
2529 if (opaq->attr) {
Radek Krejci1e008d22020-08-17 11:37:37 +02002530 for (last = opaq->attr; last->next; last = last->next) {}
Michal Vaskoa5da3292020-08-12 13:10:50 +02002531 last->next = attr;
2532 } else {
2533 opaq->attr = attr;
2534 }
2535}
2536
Michal Vasko52927e22020-03-16 17:26:14 +01002537LY_ERR
Michal Vaskofeca4fb2020-10-05 08:58:40 +02002538lyd_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 +01002539 const char *prefix, size_t prefix_len, const char *module_key, size_t module_key_len, const char *value,
2540 size_t value_len, ly_bool *dynamic, LY_PREFIX_FORMAT format, void *val_prefix_data, uint32_t hints)
Michal Vasko52927e22020-03-16 17:26:14 +01002541{
Radek Krejci011e4aa2020-09-04 15:22:31 +02002542 LY_ERR ret = LY_SUCCESS;
Radek Krejci1798aae2020-07-14 13:26:06 +02002543 struct lyd_attr *at, *last;
Michal Vasko52927e22020-03-16 17:26:14 +01002544
2545 assert(ctx && (parent || attr) && (!parent || !parent->schema));
Michal Vaskofeca4fb2020-10-05 08:58:40 +02002546 assert(name && name_len && format);
Michal Vasko52927e22020-03-16 17:26:14 +01002547
2548 if (!value_len) {
2549 value = "";
2550 }
2551
2552 at = calloc(1, sizeof *at);
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01002553 LY_CHECK_ERR_RET(!at, LOGMEM(ctx); ly_free_prefix_data(format, val_prefix_data), LY_EMEM);
Radek Krejcid46e46a2020-09-15 14:22:42 +02002554
Michal Vaskoad92b672020-11-12 13:11:31 +01002555 LY_CHECK_GOTO(ret = lydict_insert(ctx, name, name_len, &at->name.name), finish);
Michal Vasko501af032020-11-11 20:27:44 +01002556 if (prefix_len) {
Michal Vaskoad92b672020-11-12 13:11:31 +01002557 LY_CHECK_GOTO(ret = lydict_insert(ctx, prefix, prefix_len, &at->name.prefix), finish);
Michal Vasko501af032020-11-11 20:27:44 +01002558 }
2559 if (module_key_len) {
Michal Vaskoad92b672020-11-12 13:11:31 +01002560 LY_CHECK_GOTO(ret = lydict_insert(ctx, module_key, module_key_len, &at->name.module_ns), finish);
Michal Vasko501af032020-11-11 20:27:44 +01002561 }
2562
Michal Vasko52927e22020-03-16 17:26:14 +01002563 if (dynamic && *dynamic) {
Radek Krejci011e4aa2020-09-04 15:22:31 +02002564 ret = lydict_insert_zc(ctx, (char *)value, &at->value);
2565 LY_CHECK_GOTO(ret, finish);
Michal Vasko52927e22020-03-16 17:26:14 +01002566 *dynamic = 0;
2567 } else {
Radek Krejci011e4aa2020-09-04 15:22:31 +02002568 LY_CHECK_GOTO(ret = lydict_insert(ctx, value, value_len, &at->value), finish);
Michal Vasko52927e22020-03-16 17:26:14 +01002569 }
Michal Vasko501af032020-11-11 20:27:44 +01002570 at->format = format;
2571 at->val_prefix_data = val_prefix_data;
2572 at->hints = hints;
Michal Vasko52927e22020-03-16 17:26:14 +01002573
2574 /* insert as the last attribute */
2575 if (parent) {
Michal Vaskoa5da3292020-08-12 13:10:50 +02002576 lyd_insert_attr(parent, at);
Michal Vasko52927e22020-03-16 17:26:14 +01002577 } else if (*attr) {
Radek Krejci1e008d22020-08-17 11:37:37 +02002578 for (last = *attr; last->next; last = last->next) {}
Michal Vasko52927e22020-03-16 17:26:14 +01002579 last->next = at;
2580 }
2581
Radek Krejci011e4aa2020-09-04 15:22:31 +02002582finish:
2583 if (ret) {
2584 lyd_free_attr_single(ctx, at);
2585 } else if (attr) {
Michal Vasko52927e22020-03-16 17:26:14 +01002586 *attr = at;
2587 }
2588 return LY_SUCCESS;
2589}
2590
Radek Krejci084289f2019-07-09 17:35:30 +02002591API const struct lyd_node_term *
Michal Vasko004d3152020-06-11 19:59:22 +02002592lyd_target(const struct ly_path *path, const struct lyd_node *tree)
Radek Krejci084289f2019-07-09 17:35:30 +02002593{
Michal Vasko004d3152020-06-11 19:59:22 +02002594 struct lyd_node *target;
Radek Krejci084289f2019-07-09 17:35:30 +02002595
Michal Vasko004d3152020-06-11 19:59:22 +02002596 if (ly_path_eval(path, tree, &target)) {
2597 return NULL;
Radek Krejci084289f2019-07-09 17:35:30 +02002598 }
2599
Michal Vasko004d3152020-06-11 19:59:22 +02002600 return (struct lyd_node_term *)target;
Radek Krejci084289f2019-07-09 17:35:30 +02002601}
2602
Radek Krejci1f05b6a2019-07-18 16:15:06 +02002603API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02002604lyd_compare_single(const struct lyd_node *node1, const struct lyd_node *node2, uint32_t options)
Radek Krejci1f05b6a2019-07-18 16:15:06 +02002605{
2606 const struct lyd_node *iter1, *iter2;
2607 struct lyd_node_term *term1, *term2;
2608 struct lyd_node_any *any1, *any2;
Michal Vasko52927e22020-03-16 17:26:14 +01002609 struct lyd_node_opaq *opaq1, *opaq2;
Radek Krejci1f05b6a2019-07-18 16:15:06 +02002610 size_t len1, len2;
Radek Krejci084289f2019-07-09 17:35:30 +02002611
Radek Krejci1f05b6a2019-07-18 16:15:06 +02002612 if (!node1 || !node2) {
2613 if (node1 == node2) {
2614 return LY_SUCCESS;
2615 } else {
2616 return LY_ENOT;
2617 }
2618 }
2619
Michal Vaskob7be7a82020-08-20 09:09:04 +02002620 if ((LYD_CTX(node1) != LYD_CTX(node2)) || (node1->schema != node2->schema)) {
Radek Krejci1f05b6a2019-07-18 16:15:06 +02002621 return LY_ENOT;
2622 }
2623
2624 if (node1->hash != node2->hash) {
2625 return LY_ENOT;
2626 }
Michal Vasko52927e22020-03-16 17:26:14 +01002627 /* equal hashes do not mean equal nodes, they can be just in collision (or both be 0) so the nodes must be checked explicitly */
Radek Krejci1f05b6a2019-07-18 16:15:06 +02002628
Michal Vasko52927e22020-03-16 17:26:14 +01002629 if (!node1->schema) {
2630 opaq1 = (struct lyd_node_opaq *)node1;
2631 opaq2 = (struct lyd_node_opaq *)node2;
Michal Vaskoad92b672020-11-12 13:11:31 +01002632 if ((opaq1->name.name != opaq2->name.name) || (opaq1->format != opaq2->format) ||
2633 (opaq1->name.module_ns != opaq2->name.module_ns)) {
Radek Krejci1f05b6a2019-07-18 16:15:06 +02002634 return LY_ENOT;
2635 }
Michal Vasko52927e22020-03-16 17:26:14 +01002636 switch (opaq1->format) {
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01002637 case LY_PREF_XML:
2638 if (lyxml_value_compare(LYD_CTX(node1), opaq1->value, opaq1->val_prefix_data, opaq2->value, opaq2->val_prefix_data)) {
Michal Vasko52927e22020-03-16 17:26:14 +01002639 return LY_ENOT;
2640 }
2641 break;
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01002642 case LY_PREF_JSON:
Radek Krejci1798aae2020-07-14 13:26:06 +02002643 /* prefixes in JSON are unique, so it is not necessary to canonize the values */
2644 if (strcmp(opaq1->value, opaq2->value)) {
2645 return LY_ENOT;
2646 }
2647 break;
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01002648 default:
Michal Vasko52927e22020-03-16 17:26:14 +01002649 /* not allowed */
Michal Vaskob7be7a82020-08-20 09:09:04 +02002650 LOGINT(LYD_CTX(node1));
Michal Vasko52927e22020-03-16 17:26:14 +01002651 return LY_EINT;
2652 }
2653 if (options & LYD_COMPARE_FULL_RECURSION) {
2654 iter1 = opaq1->child;
2655 iter2 = opaq2->child;
Radek Krejci1f05b6a2019-07-18 16:15:06 +02002656 goto all_children_compare;
Michal Vasko52927e22020-03-16 17:26:14 +01002657 }
2658 return LY_SUCCESS;
2659 } else {
2660 switch (node1->schema->nodetype) {
2661 case LYS_LEAF:
2662 case LYS_LEAFLIST:
2663 if (options & LYD_COMPARE_DEFAULTS) {
2664 if ((node1->flags & LYD_DEFAULT) != (node2->flags & LYD_DEFAULT)) {
2665 return LY_ENOT;
2666 }
2667 }
2668
Michal Vasko1a2bf2e2020-06-16 13:07:48 +02002669 term1 = (struct lyd_node_term *)node1;
2670 term2 = (struct lyd_node_term *)node2;
Michal Vasko1a2bf2e2020-06-16 13:07:48 +02002671 return term1->value.realtype->plugin->compare(&term1->value, &term2->value);
Michal Vasko52927e22020-03-16 17:26:14 +01002672 case LYS_CONTAINER:
2673 if (options & LYD_COMPARE_DEFAULTS) {
2674 if ((node1->flags & LYD_DEFAULT) != (node2->flags & LYD_DEFAULT)) {
2675 return LY_ENOT;
2676 }
2677 }
2678 if (options & LYD_COMPARE_FULL_RECURSION) {
Michal Vasko9e685082021-01-29 14:49:09 +01002679 iter1 = lyd_child(node1);
2680 iter2 = lyd_child(node2);
Michal Vasko52927e22020-03-16 17:26:14 +01002681 goto all_children_compare;
Radek Krejci1f05b6a2019-07-18 16:15:06 +02002682 }
2683 return LY_SUCCESS;
Michal Vasko1bf09392020-03-27 12:38:10 +01002684 case LYS_RPC:
Michal Vasko52927e22020-03-16 17:26:14 +01002685 case LYS_ACTION:
2686 if (options & LYD_COMPARE_FULL_RECURSION) {
2687 /* TODO action/RPC
2688 goto all_children_compare;
2689 */
Radek Krejci1f05b6a2019-07-18 16:15:06 +02002690 }
2691 return LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +01002692 case LYS_NOTIF:
2693 if (options & LYD_COMPARE_FULL_RECURSION) {
2694 /* TODO Notification
2695 goto all_children_compare;
2696 */
2697 }
2698 return LY_SUCCESS;
2699 case LYS_LIST:
Michal Vasko9e685082021-01-29 14:49:09 +01002700 iter1 = lyd_child(node1);
2701 iter2 = lyd_child(node2);
Michal Vasko52927e22020-03-16 17:26:14 +01002702
2703 if (!(node1->schema->flags & LYS_KEYLESS) && !(options & LYD_COMPARE_FULL_RECURSION)) {
2704 /* lists with keys, their equivalence is based on their keys */
Michal Vasko544e58a2021-01-28 14:33:41 +01002705 for (const struct lysc_node *key = lysc_node_child(node1->schema);
Michal Vaskoc57d9a92020-06-23 13:28:27 +02002706 key && (key->flags & LYS_KEY);
Michal Vasko52927e22020-03-16 17:26:14 +01002707 key = key->next) {
Michal Vasko8f359bf2020-07-28 10:41:15 +02002708 if (lyd_compare_single(iter1, iter2, options)) {
Michal Vasko52927e22020-03-16 17:26:14 +01002709 return LY_ENOT;
2710 }
2711 iter1 = iter1->next;
2712 iter2 = iter2->next;
2713 }
2714 } else {
2715 /* lists without keys, their equivalence is based on equivalence of all the children (both direct and indirect) */
2716
Radek Krejci0f969882020-08-21 16:56:47 +02002717all_children_compare:
Michal Vasko52927e22020-03-16 17:26:14 +01002718 if (!iter1 && !iter2) {
2719 /* no children, nothing to compare */
2720 return LY_SUCCESS;
2721 }
2722
Michal Vaskod989ba02020-08-24 10:59:24 +02002723 for ( ; iter1 && iter2; iter1 = iter1->next, iter2 = iter2->next) {
Michal Vasko8f359bf2020-07-28 10:41:15 +02002724 if (lyd_compare_single(iter1, iter2, options | LYD_COMPARE_FULL_RECURSION)) {
Michal Vasko52927e22020-03-16 17:26:14 +01002725 return LY_ENOT;
2726 }
2727 }
2728 if (iter1 || iter2) {
2729 return LY_ENOT;
2730 }
2731 }
2732 return LY_SUCCESS;
2733 case LYS_ANYXML:
2734 case LYS_ANYDATA:
Michal Vasko22df3f02020-08-24 13:29:22 +02002735 any1 = (struct lyd_node_any *)node1;
2736 any2 = (struct lyd_node_any *)node2;
Michal Vasko52927e22020-03-16 17:26:14 +01002737
2738 if (any1->value_type != any2->value_type) {
2739 return LY_ENOT;
2740 }
2741 switch (any1->value_type) {
2742 case LYD_ANYDATA_DATATREE:
2743 iter1 = any1->value.tree;
2744 iter2 = any2->value.tree;
2745 goto all_children_compare;
2746 case LYD_ANYDATA_STRING:
2747 case LYD_ANYDATA_XML:
2748 case LYD_ANYDATA_JSON:
2749 len1 = strlen(any1->value.str);
2750 len2 = strlen(any2->value.str);
Michal Vasko69730152020-10-09 16:30:07 +02002751 if ((len1 != len2) || strcmp(any1->value.str, any2->value.str)) {
Michal Vasko52927e22020-03-16 17:26:14 +01002752 return LY_ENOT;
2753 }
2754 return LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +01002755 case LYD_ANYDATA_LYB:
Michal Vasko60ea6352020-06-29 13:39:39 +02002756 len1 = lyd_lyb_data_length(any1->value.mem);
2757 len2 = lyd_lyb_data_length(any2->value.mem);
Michal Vasko69730152020-10-09 16:30:07 +02002758 if ((len1 != len2) || memcmp(any1->value.mem, any2->value.mem, len1)) {
Michal Vasko52927e22020-03-16 17:26:14 +01002759 return LY_ENOT;
2760 }
2761 return LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +01002762 }
Radek Krejci1f05b6a2019-07-18 16:15:06 +02002763 }
2764 }
2765
Michal Vaskob7be7a82020-08-20 09:09:04 +02002766 LOGINT(LYD_CTX(node1));
Radek Krejci1f05b6a2019-07-18 16:15:06 +02002767 return LY_EINT;
2768}
Radek Krejci22ebdba2019-07-25 13:59:43 +02002769
Michal Vasko21725742020-06-29 11:49:25 +02002770API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02002771lyd_compare_siblings(const struct lyd_node *node1, const struct lyd_node *node2, uint32_t options)
Michal Vasko8f359bf2020-07-28 10:41:15 +02002772{
Michal Vaskod989ba02020-08-24 10:59:24 +02002773 for ( ; node1 && node2; node1 = node1->next, node2 = node2->next) {
Michal Vasko8f359bf2020-07-28 10:41:15 +02002774 LY_CHECK_RET(lyd_compare_single(node1, node2, options));
2775 }
2776
Michal Vasko11a81432020-07-28 16:31:29 +02002777 if (node1 == node2) {
2778 return LY_SUCCESS;
Michal Vasko8f359bf2020-07-28 10:41:15 +02002779 }
Michal Vasko11a81432020-07-28 16:31:29 +02002780 return LY_ENOT;
Michal Vasko8f359bf2020-07-28 10:41:15 +02002781}
2782
2783API LY_ERR
Michal Vasko21725742020-06-29 11:49:25 +02002784lyd_compare_meta(const struct lyd_meta *meta1, const struct lyd_meta *meta2)
2785{
2786 if (!meta1 || !meta2) {
2787 if (meta1 == meta2) {
2788 return LY_SUCCESS;
2789 } else {
2790 return LY_ENOT;
2791 }
2792 }
2793
Michal Vaskoa8083062020-11-06 17:22:10 +01002794 if ((meta1->annotation->module->ctx != meta2->annotation->module->ctx) || (meta1->annotation != meta2->annotation)) {
Michal Vasko21725742020-06-29 11:49:25 +02002795 return LY_ENOT;
2796 }
2797
Michal Vasko21725742020-06-29 11:49:25 +02002798 return meta1->value.realtype->plugin->compare(&meta1->value, &meta2->value);
2799}
2800
Radek Krejci22ebdba2019-07-25 13:59:43 +02002801/**
Michal Vasko52927e22020-03-16 17:26:14 +01002802 * @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 +02002803 *
2804 * Ignores LYD_DUP_WITH_PARENTS and LYD_DUP_WITH_SIBLINGS which are supposed to be handled by lyd_dup().
Radek Krejcif8b95172020-05-15 14:51:06 +02002805 *
2806 * @param[in] node Original node to duplicate
2807 * @param[in] parent Parent to insert into, NULL for top-level sibling.
2808 * @param[in,out] first First sibling, NULL if no top-level sibling exist yet. Can be also NULL if @p parent is set.
2809 * @param[in] options Bitmask of options flags, see @ref dupoptions.
2810 * @param[out] dup_p Pointer where the created duplicated node is placed (besides connecting it int @p parent / @p first sibling).
2811 * @return LY_ERR value
Radek Krejci22ebdba2019-07-25 13:59:43 +02002812 */
Michal Vasko52927e22020-03-16 17:26:14 +01002813static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02002814lyd_dup_r(const struct lyd_node *node, struct lyd_node *parent, struct lyd_node **first, uint32_t options,
Radek Krejci0f969882020-08-21 16:56:47 +02002815 struct lyd_node **dup_p)
Radek Krejci22ebdba2019-07-25 13:59:43 +02002816{
Michal Vasko52927e22020-03-16 17:26:14 +01002817 LY_ERR ret;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002818 struct lyd_node *dup = NULL;
Michal Vasko61551fa2020-07-09 15:45:45 +02002819 struct lyd_meta *meta;
2820 struct lyd_node_any *any;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002821
Michal Vasko52927e22020-03-16 17:26:14 +01002822 LY_CHECK_ARG_RET(NULL, node, LY_EINVAL);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002823
Michal Vasko52927e22020-03-16 17:26:14 +01002824 if (!node->schema) {
2825 dup = calloc(1, sizeof(struct lyd_node_opaq));
2826 } else {
2827 switch (node->schema->nodetype) {
Michal Vasko1bf09392020-03-27 12:38:10 +01002828 case LYS_RPC:
Michal Vasko52927e22020-03-16 17:26:14 +01002829 case LYS_ACTION:
2830 case LYS_NOTIF:
2831 case LYS_CONTAINER:
2832 case LYS_LIST:
2833 dup = calloc(1, sizeof(struct lyd_node_inner));
2834 break;
2835 case LYS_LEAF:
2836 case LYS_LEAFLIST:
2837 dup = calloc(1, sizeof(struct lyd_node_term));
2838 break;
2839 case LYS_ANYDATA:
2840 case LYS_ANYXML:
2841 dup = calloc(1, sizeof(struct lyd_node_any));
2842 break;
2843 default:
Michal Vaskob7be7a82020-08-20 09:09:04 +02002844 LOGINT(LYD_CTX(node));
Michal Vasko52927e22020-03-16 17:26:14 +01002845 ret = LY_EINT;
2846 goto error;
2847 }
Radek Krejci22ebdba2019-07-25 13:59:43 +02002848 }
Michal Vaskob7be7a82020-08-20 09:09:04 +02002849 LY_CHECK_ERR_GOTO(!dup, LOGMEM(LYD_CTX(node)); ret = LY_EMEM, error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002850
Michal Vaskof6df0a02020-06-16 13:08:34 +02002851 if (options & LYD_DUP_WITH_FLAGS) {
2852 dup->flags = node->flags;
2853 } else {
2854 dup->flags = (node->flags & LYD_DEFAULT) | LYD_NEW;
2855 }
Radek Krejci22ebdba2019-07-25 13:59:43 +02002856 dup->schema = node->schema;
Michal Vasko52927e22020-03-16 17:26:14 +01002857 dup->prev = dup;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002858
Michal Vasko25a32822020-07-09 15:48:22 +02002859 /* duplicate metadata */
2860 if (!(options & LYD_DUP_NO_META)) {
2861 LY_LIST_FOR(node->meta, meta) {
Michal Vasko3a41dff2020-07-15 14:30:28 +02002862 LY_CHECK_GOTO(ret = lyd_dup_meta_single(meta, dup, NULL), error);
Michal Vasko25a32822020-07-09 15:48:22 +02002863 }
2864 }
Radek Krejci22ebdba2019-07-25 13:59:43 +02002865
2866 /* nodetype-specific work */
Michal Vasko52927e22020-03-16 17:26:14 +01002867 if (!dup->schema) {
2868 struct lyd_node_opaq *opaq = (struct lyd_node_opaq *)dup;
2869 struct lyd_node_opaq *orig = (struct lyd_node_opaq *)node;
2870 struct lyd_node *child;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002871
2872 if (options & LYD_DUP_RECURSIVE) {
2873 /* duplicate all the children */
2874 LY_LIST_FOR(orig->child, child) {
Michal Vasko3a41dff2020-07-15 14:30:28 +02002875 LY_CHECK_GOTO(ret = lyd_dup_r(child, dup, NULL, options, NULL), error);
Michal Vasko52927e22020-03-16 17:26:14 +01002876 }
2877 }
Michal Vaskoad92b672020-11-12 13:11:31 +01002878 LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), orig->name.name, 0, &opaq->name.name), error);
Michal Vasko52927e22020-03-16 17:26:14 +01002879 opaq->format = orig->format;
Michal Vaskoad92b672020-11-12 13:11:31 +01002880 if (orig->name.prefix) {
2881 LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), orig->name.prefix, 0, &opaq->name.prefix), error);
Michal Vasko52927e22020-03-16 17:26:14 +01002882 }
Michal Vaskoad92b672020-11-12 13:11:31 +01002883 LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), orig->name.module_ns, 0, &opaq->name.module_ns), error);
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01002884 if (orig->val_prefix_data) {
2885 ret = ly_dup_prefix_data(LYD_CTX(node), opaq->format, orig->val_prefix_data, &opaq->val_prefix_data);
2886 LY_CHECK_GOTO(ret, error);
Michal Vasko52927e22020-03-16 17:26:14 +01002887 }
Radek Krejci011e4aa2020-09-04 15:22:31 +02002888 LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), orig->value, 0, &opaq->value), error);
Michal Vasko52927e22020-03-16 17:26:14 +01002889 opaq->ctx = orig->ctx;
2890 } else if (dup->schema->nodetype & LYD_NODE_TERM) {
2891 struct lyd_node_term *term = (struct lyd_node_term *)dup;
2892 struct lyd_node_term *orig = (struct lyd_node_term *)node;
2893
2894 term->hash = orig->hash;
Michal Vaskob7be7a82020-08-20 09:09:04 +02002895 LY_CHECK_ERR_GOTO(orig->value.realtype->plugin->duplicate(LYD_CTX(node), &orig->value, &term->value),
Michal Vasko69730152020-10-09 16:30:07 +02002896 LOGERR(LYD_CTX(node), LY_EINT, "Value duplication failed."); ret = LY_EINT, error);
Michal Vasko52927e22020-03-16 17:26:14 +01002897 } else if (dup->schema->nodetype & LYD_NODE_INNER) {
2898 struct lyd_node_inner *orig = (struct lyd_node_inner *)node;
2899 struct lyd_node *child;
2900
2901 if (options & LYD_DUP_RECURSIVE) {
2902 /* duplicate all the children */
2903 LY_LIST_FOR(orig->child, child) {
Michal Vasko3a41dff2020-07-15 14:30:28 +02002904 LY_CHECK_GOTO(ret = lyd_dup_r(child, dup, NULL, options, NULL), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002905 }
Michal Vasko69730152020-10-09 16:30:07 +02002906 } else if ((dup->schema->nodetype == LYS_LIST) && !(dup->schema->flags & LYS_KEYLESS)) {
Radek Krejci22ebdba2019-07-25 13:59:43 +02002907 /* always duplicate keys of a list */
Radek Krejci22ebdba2019-07-25 13:59:43 +02002908 child = orig->child;
Michal Vasko544e58a2021-01-28 14:33:41 +01002909 for (const struct lysc_node *key = lysc_node_child(dup->schema);
Michal Vaskoc57d9a92020-06-23 13:28:27 +02002910 key && (key->flags & LYS_KEY);
Radek Krejci0fe9b512019-07-26 17:51:05 +02002911 key = key->next) {
Radek Krejci22ebdba2019-07-25 13:59:43 +02002912 if (!child) {
2913 /* possibly not keys are present in filtered tree */
2914 break;
Radek Krejci0fe9b512019-07-26 17:51:05 +02002915 } else if (child->schema != key) {
2916 /* possibly not all keys are present in filtered tree,
2917 * but there can be also some non-key nodes */
2918 continue;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002919 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02002920 LY_CHECK_GOTO(ret = lyd_dup_r(child, dup, NULL, options, NULL), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002921 child = child->next;
2922 }
2923 }
2924 lyd_hash(dup);
2925 } else if (dup->schema->nodetype & LYD_NODE_ANY) {
Michal Vasko61551fa2020-07-09 15:45:45 +02002926 dup->hash = node->hash;
2927 any = (struct lyd_node_any *)node;
2928 LY_CHECK_GOTO(ret = lyd_any_copy_value(dup, &any->value, any->value_type), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002929 }
2930
Michal Vasko52927e22020-03-16 17:26:14 +01002931 /* insert */
2932 lyd_insert_node(parent, first, dup);
Michal Vasko52927e22020-03-16 17:26:14 +01002933
2934 if (dup_p) {
2935 *dup_p = dup;
2936 }
2937 return LY_SUCCESS;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002938
2939error:
Michal Vasko52927e22020-03-16 17:26:14 +01002940 lyd_free_tree(dup);
2941 return ret;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002942}
2943
Michal Vasko3a41dff2020-07-15 14:30:28 +02002944static LY_ERR
2945lyd_dup_get_local_parent(const struct lyd_node *node, const struct lyd_node_inner *parent, struct lyd_node **dup_parent,
Radek Krejci0f969882020-08-21 16:56:47 +02002946 struct lyd_node_inner **local_parent)
Radek Krejci22ebdba2019-07-25 13:59:43 +02002947{
Michal Vasko3a41dff2020-07-15 14:30:28 +02002948 const struct lyd_node_inner *orig_parent, *iter;
Radek Krejci857189e2020-09-01 13:26:36 +02002949 ly_bool repeat = 1;
Michal Vasko3a41dff2020-07-15 14:30:28 +02002950
2951 *dup_parent = NULL;
2952 *local_parent = NULL;
2953
2954 for (orig_parent = node->parent; repeat && orig_parent; orig_parent = orig_parent->parent) {
2955 if (parent && (parent->schema == orig_parent->schema)) {
2956 /* stop creating parents, connect what we have into the provided parent */
2957 iter = parent;
2958 repeat = 0;
2959 } else {
2960 iter = NULL;
2961 LY_CHECK_RET(lyd_dup_r((struct lyd_node *)orig_parent, NULL, (struct lyd_node **)&iter, 0,
Radek Krejci0f969882020-08-21 16:56:47 +02002962 (struct lyd_node **)&iter));
Michal Vasko3a41dff2020-07-15 14:30:28 +02002963 }
2964 if (!*local_parent) {
2965 *local_parent = (struct lyd_node_inner *)iter;
2966 }
2967 if (iter->child) {
2968 /* 1) list - add after keys
2969 * 2) provided parent with some children */
2970 iter->child->prev->next = *dup_parent;
2971 if (*dup_parent) {
2972 (*dup_parent)->prev = iter->child->prev;
2973 iter->child->prev = *dup_parent;
2974 }
2975 } else {
2976 ((struct lyd_node_inner *)iter)->child = *dup_parent;
2977 }
2978 if (*dup_parent) {
2979 (*dup_parent)->parent = (struct lyd_node_inner *)iter;
2980 }
2981 *dup_parent = (struct lyd_node *)iter;
2982 }
2983
2984 if (repeat && parent) {
2985 /* given parent and created parents chain actually do not interconnect */
Michal Vaskob7be7a82020-08-20 09:09:04 +02002986 LOGERR(LYD_CTX(node), LY_EINVAL,
Michal Vasko69730152020-10-09 16:30:07 +02002987 "Invalid argument parent (%s()) - does not interconnect with the created node's parents chain.", __func__);
Michal Vasko3a41dff2020-07-15 14:30:28 +02002988 return LY_EINVAL;
2989 }
2990
2991 return LY_SUCCESS;
2992}
2993
2994static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02002995lyd_dup(const struct lyd_node *node, struct lyd_node_inner *parent, uint32_t options, ly_bool nosiblings, struct lyd_node **dup)
Michal Vasko3a41dff2020-07-15 14:30:28 +02002996{
2997 LY_ERR rc;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002998 const struct lyd_node *orig; /* original node to be duplicated */
2999 struct lyd_node *first = NULL; /* the first duplicated node, this is returned */
Radek Krejci22ebdba2019-07-25 13:59:43 +02003000 struct lyd_node *top = NULL; /* the most higher created node */
3001 struct lyd_node_inner *local_parent = NULL; /* the direct parent node for the duplicated node(s) */
Radek Krejci22ebdba2019-07-25 13:59:43 +02003002
Michal Vasko3a41dff2020-07-15 14:30:28 +02003003 LY_CHECK_ARG_RET(NULL, node, LY_EINVAL);
Radek Krejci22ebdba2019-07-25 13:59:43 +02003004
3005 if (options & LYD_DUP_WITH_PARENTS) {
Michal Vasko3a41dff2020-07-15 14:30:28 +02003006 LY_CHECK_GOTO(rc = lyd_dup_get_local_parent(node, parent, &top, &local_parent), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02003007 } else {
3008 local_parent = parent;
3009 }
3010
Radek Krejci22ebdba2019-07-25 13:59:43 +02003011 LY_LIST_FOR(node, orig) {
Michal Vasko35f4d772021-01-12 12:08:57 +01003012 if (lysc_is_key(orig->schema)) {
3013 if (local_parent) {
3014 /* the key must already exist in the parent */
3015 rc = lyd_find_sibling_schema(local_parent->child, orig->schema, first ? NULL : &first);
3016 LY_CHECK_ERR_GOTO(rc, LOGINT(LYD_CTX(node)), error);
3017 } else {
3018 assert(!(options & LYD_DUP_WITH_PARENTS));
3019 /* duplicating a single key, okay, I suppose... */
3020 rc = lyd_dup_r(orig, NULL, &first, options, first ? NULL : &first);
3021 LY_CHECK_GOTO(rc, error);
3022 }
3023 } else {
3024 /* if there is no local parent, it will be inserted into first */
Michal Vasko9e685082021-01-29 14:49:09 +01003025 rc = lyd_dup_r(orig, &local_parent->node, &first, options, first ? NULL : &first);
Michal Vasko35f4d772021-01-12 12:08:57 +01003026 LY_CHECK_GOTO(rc, error);
3027 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02003028 if (nosiblings) {
Radek Krejci22ebdba2019-07-25 13:59:43 +02003029 break;
3030 }
3031 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02003032
3033 /* rehash if needed */
Michal Vaskod989ba02020-08-24 10:59:24 +02003034 for ( ; local_parent; local_parent = local_parent->parent) {
Michal Vasko69730152020-10-09 16:30:07 +02003035 if ((local_parent->schema->nodetype == LYS_LIST) && (local_parent->schema->flags & LYS_KEYLESS)) {
Michal Vasko9e685082021-01-29 14:49:09 +01003036 lyd_hash(&local_parent->node);
Radek Krejci22ebdba2019-07-25 13:59:43 +02003037 }
3038 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02003039
3040 if (dup) {
3041 *dup = first;
3042 }
3043 return LY_SUCCESS;
Radek Krejci22ebdba2019-07-25 13:59:43 +02003044
3045error:
3046 if (top) {
3047 lyd_free_tree(top);
3048 } else {
Michal Vaskof03ed032020-03-04 13:31:44 +01003049 lyd_free_siblings(first);
Radek Krejci22ebdba2019-07-25 13:59:43 +02003050 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02003051 return rc;
Radek Krejci22ebdba2019-07-25 13:59:43 +02003052}
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003053
Michal Vasko3a41dff2020-07-15 14:30:28 +02003054API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003055lyd_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 +02003056{
3057 return lyd_dup(node, parent, options, 1, dup);
3058}
3059
3060API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003061lyd_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 +02003062{
3063 return lyd_dup(node, parent, options, 0, dup);
3064}
3065
3066API LY_ERR
3067lyd_dup_meta_single(const struct lyd_meta *meta, struct lyd_node *node, struct lyd_meta **dup)
Michal Vasko25a32822020-07-09 15:48:22 +02003068{
Radek Krejci011e4aa2020-09-04 15:22:31 +02003069 LY_ERR ret = LY_SUCCESS;
Michal Vasko25a32822020-07-09 15:48:22 +02003070 struct lyd_meta *mt, *last;
3071
Michal Vasko3a41dff2020-07-15 14:30:28 +02003072 LY_CHECK_ARG_RET(NULL, meta, node, LY_EINVAL);
Michal Vasko25a32822020-07-09 15:48:22 +02003073
3074 /* create a copy */
3075 mt = calloc(1, sizeof *mt);
Michal Vaskob7be7a82020-08-20 09:09:04 +02003076 LY_CHECK_ERR_RET(!mt, LOGMEM(LYD_CTX(node)), LY_EMEM);
Michal Vasko25a32822020-07-09 15:48:22 +02003077 mt->annotation = meta->annotation;
Michal Vaskob7be7a82020-08-20 09:09:04 +02003078 ret = meta->value.realtype->plugin->duplicate(LYD_CTX(node), &meta->value, &mt->value);
Radek Krejci011e4aa2020-09-04 15:22:31 +02003079 LY_CHECK_ERR_GOTO(ret, LOGERR(LYD_CTX(node), LY_EINT, "Value duplication failed."), finish);
3080 LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), meta->name, 0, &mt->name), finish);
Michal Vasko25a32822020-07-09 15:48:22 +02003081
3082 /* insert as the last attribute */
Radek Krejci011e4aa2020-09-04 15:22:31 +02003083 mt->parent = node;
Michal Vasko25a32822020-07-09 15:48:22 +02003084 if (node->meta) {
Radek Krejci1e008d22020-08-17 11:37:37 +02003085 for (last = node->meta; last->next; last = last->next) {}
Michal Vasko25a32822020-07-09 15:48:22 +02003086 last->next = mt;
3087 } else {
3088 node->meta = mt;
3089 }
3090
Radek Krejci011e4aa2020-09-04 15:22:31 +02003091finish:
3092 if (ret) {
3093 lyd_free_meta_single(mt);
3094 } else if (dup) {
Michal Vasko3a41dff2020-07-15 14:30:28 +02003095 *dup = mt;
3096 }
3097 return LY_SUCCESS;
Michal Vasko25a32822020-07-09 15:48:22 +02003098}
3099
Michal Vasko4490d312020-06-16 13:08:55 +02003100/**
3101 * @brief Merge a source sibling into target siblings.
3102 *
3103 * @param[in,out] first_trg First target sibling, is updated if top-level.
3104 * @param[in] parent_trg Target parent.
3105 * @param[in,out] sibling_src Source sibling to merge, set to NULL if spent.
3106 * @param[in] options Merge options.
3107 * @return LY_ERR value.
3108 */
3109static LY_ERR
3110lyd_merge_sibling_r(struct lyd_node **first_trg, struct lyd_node *parent_trg, const struct lyd_node **sibling_src_p,
Radek Krejci1deb5be2020-08-26 16:43:36 +02003111 uint16_t options)
Michal Vasko4490d312020-06-16 13:08:55 +02003112{
3113 LY_ERR ret;
3114 const struct lyd_node *child_src, *tmp, *sibling_src;
Michal Vasko56daf732020-08-10 10:57:18 +02003115 struct lyd_node *match_trg, *dup_src, *elem;
Michal Vasko4490d312020-06-16 13:08:55 +02003116 struct lysc_type *type;
Michal Vasko4490d312020-06-16 13:08:55 +02003117
3118 sibling_src = *sibling_src_p;
3119 if (sibling_src->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) {
3120 /* try to find the exact instance */
3121 ret = lyd_find_sibling_first(*first_trg, sibling_src, &match_trg);
3122 } else {
3123 /* try to simply find the node, there cannot be more instances */
3124 ret = lyd_find_sibling_val(*first_trg, sibling_src->schema, NULL, 0, &match_trg);
3125 }
3126
3127 if (!ret) {
3128 /* node found, make sure even value matches for all node types */
Michal Vasko8f359bf2020-07-28 10:41:15 +02003129 if ((match_trg->schema->nodetype == LYS_LEAF) && lyd_compare_single(sibling_src, match_trg, LYD_COMPARE_DEFAULTS)) {
Michal Vasko4490d312020-06-16 13:08:55 +02003130 /* since they are different, they cannot both be default */
3131 assert(!(sibling_src->flags & LYD_DEFAULT) || !(match_trg->flags & LYD_DEFAULT));
3132
Michal Vasko3a41dff2020-07-15 14:30:28 +02003133 /* update value (or only LYD_DEFAULT flag) only if flag set or the source node is not default */
3134 if ((options & LYD_MERGE_DEFAULTS) || !(sibling_src->flags & LYD_DEFAULT)) {
Michal Vasko4490d312020-06-16 13:08:55 +02003135 type = ((struct lysc_node_leaf *)match_trg->schema)->type;
Michal Vaskob7be7a82020-08-20 09:09:04 +02003136 type->plugin->free(LYD_CTX(match_trg), &((struct lyd_node_term *)match_trg)->value);
3137 LY_CHECK_RET(type->plugin->duplicate(LYD_CTX(match_trg), &((struct lyd_node_term *)sibling_src)->value,
Michal Vasko69730152020-10-09 16:30:07 +02003138 &((struct lyd_node_term *)match_trg)->value));
Michal Vasko4490d312020-06-16 13:08:55 +02003139
3140 /* copy flags and add LYD_NEW */
3141 match_trg->flags = sibling_src->flags | LYD_NEW;
3142 }
Michal Vasko8f359bf2020-07-28 10:41:15 +02003143 } else if ((match_trg->schema->nodetype & LYS_ANYDATA) && lyd_compare_single(sibling_src, match_trg, 0)) {
Michal Vasko4c583e82020-07-17 12:16:14 +02003144 /* update value */
3145 LY_CHECK_RET(lyd_any_copy_value(match_trg, &((struct lyd_node_any *)sibling_src)->value,
Radek Krejci0f969882020-08-21 16:56:47 +02003146 ((struct lyd_node_any *)sibling_src)->value_type));
Michal Vasko4490d312020-06-16 13:08:55 +02003147
3148 /* copy flags and add LYD_NEW */
3149 match_trg->flags = sibling_src->flags | LYD_NEW;
Michal Vasko4490d312020-06-16 13:08:55 +02003150 } else {
3151 /* check descendants, recursively */
Radek Krejcia1c1e542020-09-29 16:06:52 +02003152 LY_LIST_FOR_SAFE(lyd_child_no_keys(sibling_src), tmp, child_src) {
Michal Vaskoe0665742021-02-11 11:08:44 +01003153 LY_CHECK_RET(lyd_merge_sibling_r(lyd_node_child_p(match_trg), match_trg, &child_src, options));
Michal Vasko4490d312020-06-16 13:08:55 +02003154 }
3155 }
3156 } else {
3157 /* node not found, merge it */
3158 if (options & LYD_MERGE_DESTRUCT) {
3159 dup_src = (struct lyd_node *)sibling_src;
3160 lyd_unlink_tree(dup_src);
3161 /* spend it */
3162 *sibling_src_p = NULL;
3163 } else {
Michal Vasko3a41dff2020-07-15 14:30:28 +02003164 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 +02003165 }
3166
3167 /* set LYD_NEW for all the new nodes, required for validation */
Michal Vasko56daf732020-08-10 10:57:18 +02003168 LYD_TREE_DFS_BEGIN(dup_src, elem) {
Michal Vasko4490d312020-06-16 13:08:55 +02003169 elem->flags |= LYD_NEW;
Michal Vasko56daf732020-08-10 10:57:18 +02003170 LYD_TREE_DFS_END(dup_src, elem);
Michal Vasko4490d312020-06-16 13:08:55 +02003171 }
3172
3173 lyd_insert_node(parent_trg, first_trg, dup_src);
3174 }
3175
3176 return LY_SUCCESS;
3177}
3178
Michal Vasko3a41dff2020-07-15 14:30:28 +02003179static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02003180lyd_merge(struct lyd_node **target, const struct lyd_node *source, uint16_t options, ly_bool nosiblings)
Michal Vasko4490d312020-06-16 13:08:55 +02003181{
3182 const struct lyd_node *sibling_src, *tmp;
Radek Krejci857189e2020-09-01 13:26:36 +02003183 ly_bool first;
Michal Vasko4490d312020-06-16 13:08:55 +02003184
3185 LY_CHECK_ARG_RET(NULL, target, LY_EINVAL);
3186
3187 if (!source) {
3188 /* nothing to merge */
3189 return LY_SUCCESS;
3190 }
3191
Michal Vasko831422b2020-11-24 11:20:51 +01003192 if ((*target && lysc_data_parent((*target)->schema)) || lysc_data_parent(source->schema)) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02003193 LOGERR(LYD_CTX(source), LY_EINVAL, "Invalid arguments - can merge only 2 top-level subtrees (%s()).", __func__);
Michal Vasko4490d312020-06-16 13:08:55 +02003194 return LY_EINVAL;
3195 }
3196
3197 LY_LIST_FOR_SAFE(source, tmp, sibling_src) {
Radek Krejci857189e2020-09-01 13:26:36 +02003198 first = (sibling_src == source) ? 1 : 0;
Michal Vasko4490d312020-06-16 13:08:55 +02003199 LY_CHECK_RET(lyd_merge_sibling_r(target, NULL, &sibling_src, options));
3200 if (first && !sibling_src) {
3201 /* source was spent (unlinked), move to the next node */
3202 source = tmp;
3203 }
3204
Michal Vasko3a41dff2020-07-15 14:30:28 +02003205 if (nosiblings) {
Michal Vasko4490d312020-06-16 13:08:55 +02003206 break;
3207 }
3208 }
3209
3210 if (options & LYD_MERGE_DESTRUCT) {
3211 /* free any leftover source data that were not merged */
3212 lyd_free_siblings((struct lyd_node *)source);
3213 }
3214
3215 return LY_SUCCESS;
3216}
3217
Michal Vasko3a41dff2020-07-15 14:30:28 +02003218API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003219lyd_merge_tree(struct lyd_node **target, const struct lyd_node *source, uint16_t options)
Michal Vasko3a41dff2020-07-15 14:30:28 +02003220{
3221 return lyd_merge(target, source, options, 1);
3222}
3223
3224API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003225lyd_merge_siblings(struct lyd_node **target, const struct lyd_node *source, uint16_t options)
Michal Vasko3a41dff2020-07-15 14:30:28 +02003226{
3227 return lyd_merge(target, source, options, 0);
3228}
3229
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003230static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02003231lyd_path_str_enlarge(char **buffer, size_t *buflen, size_t reqlen, ly_bool is_static)
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003232{
Michal Vasko14654712020-02-06 08:35:21 +01003233 /* ending \0 */
3234 ++reqlen;
3235
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003236 if (reqlen > *buflen) {
3237 if (is_static) {
3238 return LY_EINCOMPLETE;
3239 }
3240
3241 *buffer = ly_realloc(*buffer, reqlen * sizeof **buffer);
3242 if (!*buffer) {
3243 return LY_EMEM;
3244 }
3245
3246 *buflen = reqlen;
3247 }
3248
3249 return LY_SUCCESS;
3250}
3251
Michal Vaskod59035b2020-07-08 12:00:06 +02003252LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02003253lyd_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 +02003254{
3255 const struct lyd_node *key;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003256 size_t len;
3257 const char *val;
3258 char quot;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003259
Radek Krejcia1c1e542020-09-29 16:06:52 +02003260 for (key = lyd_child(node); key && (key->schema->flags & LYS_KEY); key = key->next) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02003261 val = LYD_CANON_VALUE(key);
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003262 len = 1 + strlen(key->schema->name) + 2 + strlen(val) + 2;
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003263 LY_CHECK_RET(lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static));
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003264
3265 quot = '\'';
3266 if (strchr(val, '\'')) {
3267 quot = '"';
3268 }
3269 *bufused += sprintf(*buffer + *bufused, "[%s=%c%s%c]", key->schema->name, quot, val, quot);
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003270 }
3271
3272 return LY_SUCCESS;
3273}
3274
3275/**
3276 * @brief Append leaf-list value predicate to path.
3277 *
3278 * @param[in] node Node to print.
3279 * @param[in,out] buffer Buffer to print to.
3280 * @param[in,out] buflen Current buffer length.
3281 * @param[in,out] bufused Current number of characters used in @p buffer.
3282 * @param[in] is_static Whether buffer is static or can be reallocated.
3283 * @return LY_ERR
3284 */
3285static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02003286lyd_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 +02003287{
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003288 size_t len;
3289 const char *val;
3290 char quot;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003291
Michal Vaskob7be7a82020-08-20 09:09:04 +02003292 val = LYD_CANON_VALUE(node);
Radek Krejcif13b87b2020-12-01 22:02:17 +01003293 len = 4 + strlen(val) + 2; /* "[.='" + val + "']" */
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003294 LY_CHECK_RET(lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static));
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003295
3296 quot = '\'';
3297 if (strchr(val, '\'')) {
3298 quot = '"';
3299 }
3300 *bufused += sprintf(*buffer + *bufused, "[.=%c%s%c]", quot, val, quot);
3301
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003302 return LY_SUCCESS;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003303}
3304
3305/**
3306 * @brief Append node position (relative to its other instances) predicate to path.
3307 *
3308 * @param[in] node Node to print.
3309 * @param[in,out] buffer Buffer to print to.
3310 * @param[in,out] buflen Current buffer length.
3311 * @param[in,out] bufused Current number of characters used in @p buffer.
3312 * @param[in] is_static Whether buffer is static or can be reallocated.
3313 * @return LY_ERR
3314 */
3315static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02003316lyd_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 +02003317{
3318 const struct lyd_node *first, *iter;
3319 size_t len;
Radek Krejci1deb5be2020-08-26 16:43:36 +02003320 uint64_t pos;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003321 char *val = NULL;
3322 LY_ERR rc;
3323
3324 if (node->parent) {
3325 first = node->parent->child;
3326 } else {
Michal Vaskoe070bfe2020-08-31 12:27:25 +02003327 for (first = node; first->prev->next; first = first->prev) {}
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003328 }
3329 pos = 1;
3330 for (iter = first; iter != node; iter = iter->next) {
3331 if (iter->schema == node->schema) {
3332 ++pos;
3333 }
3334 }
Radek Krejci1deb5be2020-08-26 16:43:36 +02003335 if (asprintf(&val, "%" PRIu64, pos) == -1) {
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003336 return LY_EMEM;
3337 }
3338
3339 len = 1 + strlen(val) + 1;
3340 rc = lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static);
3341 if (rc != LY_SUCCESS) {
3342 goto cleanup;
3343 }
3344
3345 *bufused += sprintf(*buffer + *bufused, "[%s]", val);
3346
3347cleanup:
3348 free(val);
3349 return rc;
3350}
3351
3352API char *
3353lyd_path(const struct lyd_node *node, LYD_PATH_TYPE pathtype, char *buffer, size_t buflen)
3354{
Radek Krejci857189e2020-09-01 13:26:36 +02003355 ly_bool is_static = 0;
Radek Krejci1deb5be2020-08-26 16:43:36 +02003356 uint32_t i, depth;
Michal Vasko14654712020-02-06 08:35:21 +01003357 size_t bufused = 0, len;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003358 const struct lyd_node *iter;
3359 const struct lys_module *mod;
Michal Vasko790b2bc2020-08-03 13:35:06 +02003360 LY_ERR rc = LY_SUCCESS;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003361
3362 LY_CHECK_ARG_RET(NULL, node, NULL);
3363 if (buffer) {
3364 LY_CHECK_ARG_RET(node->schema->module->ctx, buflen > 1, NULL);
3365 is_static = 1;
Michal Vasko14654712020-02-06 08:35:21 +01003366 } else {
3367 buflen = 0;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003368 }
3369
3370 switch (pathtype) {
Radek Krejci635d2b82021-01-04 11:26:51 +01003371 case LYD_PATH_STD:
3372 case LYD_PATH_STD_NO_LAST_PRED:
Michal Vasko14654712020-02-06 08:35:21 +01003373 depth = 1;
Michal Vasko9e685082021-01-29 14:49:09 +01003374 for (iter = node; iter->parent; iter = lyd_parent(iter)) {
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003375 ++depth;
3376 }
3377
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003378 goto iter_print;
Michal Vasko14654712020-02-06 08:35:21 +01003379 while (depth) {
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003380 /* find the right node */
Michal Vasko9e685082021-01-29 14:49:09 +01003381 for (iter = node, i = 1; i < depth; iter = lyd_parent(iter), ++i) {}
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003382iter_print:
3383 /* print prefix and name */
3384 mod = NULL;
Michal Vasko69730152020-10-09 16:30:07 +02003385 if (iter->schema && (!iter->parent || (iter->schema->module != iter->parent->schema->module))) {
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003386 mod = iter->schema->module;
3387 }
3388
3389 /* realloc string */
Michal Vaskoad92b672020-11-12 13:11:31 +01003390 len = 1 + (mod ? strlen(mod->name) + 1 : 0) + (iter->schema ? strlen(iter->schema->name) :
3391 strlen(((struct lyd_node_opaq *)iter)->name.name));
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003392 rc = lyd_path_str_enlarge(&buffer, &buflen, bufused + len, is_static);
3393 if (rc != LY_SUCCESS) {
3394 break;
3395 }
3396
3397 /* print next node */
Radek Krejci1798aae2020-07-14 13:26:06 +02003398 bufused += sprintf(buffer + bufused, "/%s%s%s", mod ? mod->name : "", mod ? ":" : "",
Michal Vaskoad92b672020-11-12 13:11:31 +01003399 iter->schema ? iter->schema->name : ((struct lyd_node_opaq *)iter)->name.name);
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003400
Michal Vasko790b2bc2020-08-03 13:35:06 +02003401 /* do not always print the last (first) predicate */
Radek Krejci635d2b82021-01-04 11:26:51 +01003402 if (iter->schema && ((depth > 1) || (pathtype == LYD_PATH_STD))) {
Michal Vasko790b2bc2020-08-03 13:35:06 +02003403 switch (iter->schema->nodetype) {
3404 case LYS_LIST:
3405 if (iter->schema->flags & LYS_KEYLESS) {
3406 /* print its position */
3407 rc = lyd_path_position_predicate(iter, &buffer, &buflen, &bufused, is_static);
3408 } else {
3409 /* print all list keys in predicates */
3410 rc = lyd_path_list_predicate(iter, &buffer, &buflen, &bufused, is_static);
3411 }
3412 break;
3413 case LYS_LEAFLIST:
3414 if (iter->schema->flags & LYS_CONFIG_W) {
3415 /* print leaf-list value */
3416 rc = lyd_path_leaflist_predicate(iter, &buffer, &buflen, &bufused, is_static);
3417 } else {
3418 /* print its position */
3419 rc = lyd_path_position_predicate(iter, &buffer, &buflen, &bufused, is_static);
3420 }
3421 break;
3422 default:
3423 /* nothing to print more */
3424 break;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003425 }
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003426 }
3427 if (rc != LY_SUCCESS) {
3428 break;
3429 }
3430
Michal Vasko14654712020-02-06 08:35:21 +01003431 --depth;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003432 }
3433 break;
3434 }
3435
3436 return buffer;
3437}
Michal Vaskoe444f752020-02-10 12:20:06 +01003438
Michal Vasko25a32822020-07-09 15:48:22 +02003439API struct lyd_meta *
3440lyd_find_meta(const struct lyd_meta *first, const struct lys_module *module, const char *name)
3441{
3442 struct lyd_meta *ret = NULL;
3443 const struct ly_ctx *ctx;
3444 const char *prefix, *tmp;
3445 char *str;
3446 size_t pref_len, name_len;
3447
3448 LY_CHECK_ARG_RET(NULL, module || strchr(name, ':'), name, NULL);
3449
3450 if (!first) {
3451 return NULL;
3452 }
3453
3454 ctx = first->annotation->module->ctx;
3455
3456 /* parse the name */
3457 tmp = name;
3458 if (ly_parse_nodeid(&tmp, &prefix, &pref_len, &name, &name_len) || tmp[0]) {
3459 LOGERR(ctx, LY_EINVAL, "Metadata name \"%s\" is not valid.", name);
3460 return NULL;
3461 }
3462
3463 /* find the module */
3464 if (prefix) {
3465 str = strndup(prefix, pref_len);
3466 module = ly_ctx_get_module_latest(ctx, str);
3467 free(str);
3468 LY_CHECK_ERR_RET(!module, LOGERR(ctx, LY_EINVAL, "Module \"%.*s\" not found.", pref_len, prefix), NULL);
3469 }
3470
3471 /* find the metadata */
3472 LY_LIST_FOR(first, first) {
3473 if ((first->annotation->module == module) && !strcmp(first->name, name)) {
3474 ret = (struct lyd_meta *)first;
3475 break;
3476 }
3477 }
3478
3479 return ret;
3480}
3481
Michal Vasko9b368d32020-02-14 13:53:31 +01003482API LY_ERR
Michal Vaskoe444f752020-02-10 12:20:06 +01003483lyd_find_sibling_first(const struct lyd_node *siblings, const struct lyd_node *target, struct lyd_node **match)
3484{
3485 struct lyd_node **match_p;
3486 struct lyd_node_inner *parent;
3487
Michal Vaskof03ed032020-03-04 13:31:44 +01003488 LY_CHECK_ARG_RET(NULL, target, LY_EINVAL);
Michal Vaskoe444f752020-02-10 12:20:06 +01003489
Michal Vasko6da1e952020-12-09 18:14:38 +01003490 if (!siblings || (siblings->schema && target->schema &&
3491 (lysc_data_parent(siblings->schema) != lysc_data_parent(target->schema)))) {
Michal Vasko62ed12d2020-05-21 10:08:25 +02003492 /* no data or schema mismatch */
Michal Vasko9b368d32020-02-14 13:53:31 +01003493 if (match) {
3494 *match = NULL;
3495 }
Michal Vaskoe444f752020-02-10 12:20:06 +01003496 return LY_ENOTFOUND;
3497 }
3498
3499 /* find first sibling */
3500 if (siblings->parent) {
3501 siblings = siblings->parent->child;
3502 } else {
3503 while (siblings->prev->next) {
3504 siblings = siblings->prev;
3505 }
3506 }
3507
Michal Vasko9e685082021-01-29 14:49:09 +01003508 parent = siblings->parent;
Michal Vasko6da1e952020-12-09 18:14:38 +01003509 if (parent && parent->schema && parent->children_ht) {
Michal Vaskoe444f752020-02-10 12:20:06 +01003510 assert(target->hash);
3511
3512 /* find by hash */
3513 if (!lyht_find(parent->children_ht, &target, target->hash, (void **)&match_p)) {
Michal Vaskoda859032020-07-14 12:20:14 +02003514 /* check even value when needed */
Michal Vasko8f359bf2020-07-28 10:41:15 +02003515 if (!(target->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) || !lyd_compare_single(target, *match_p, 0)) {
Michal Vaskoda859032020-07-14 12:20:14 +02003516 siblings = *match_p;
3517 } else {
3518 siblings = NULL;
3519 }
Michal Vaskoe444f752020-02-10 12:20:06 +01003520 } else {
3521 /* not found */
3522 siblings = NULL;
3523 }
3524 } else {
3525 /* no children hash table */
Michal Vaskod989ba02020-08-24 10:59:24 +02003526 for ( ; siblings; siblings = siblings->next) {
Michal Vasko8f359bf2020-07-28 10:41:15 +02003527 if (!lyd_compare_single(siblings, target, 0)) {
Michal Vaskoe444f752020-02-10 12:20:06 +01003528 break;
3529 }
3530 }
3531 }
3532
3533 if (!siblings) {
Michal Vasko9b368d32020-02-14 13:53:31 +01003534 if (match) {
3535 *match = NULL;
3536 }
Michal Vaskoe444f752020-02-10 12:20:06 +01003537 return LY_ENOTFOUND;
3538 }
3539
Michal Vasko9b368d32020-02-14 13:53:31 +01003540 if (match) {
3541 *match = (struct lyd_node *)siblings;
3542 }
Michal Vaskoe444f752020-02-10 12:20:06 +01003543 return LY_SUCCESS;
3544}
3545
Radek Krejci857189e2020-09-01 13:26:36 +02003546/**
3547 * @brief Comparison callback to match schema node with a schema of a data node.
3548 *
3549 * @param[in] val1_p Pointer to the schema node
3550 * @param[in] val2_p Pointer to the data node
Michal Vasko62524a92021-02-26 10:08:50 +01003551 * Implementation of ::lyht_value_equal_cb.
Radek Krejci857189e2020-09-01 13:26:36 +02003552 */
3553static ly_bool
3554lyd_hash_table_schema_val_equal(void *val1_p, void *val2_p, ly_bool UNUSED(mod), void *UNUSED(cb_data))
Michal Vasko90932a92020-02-12 14:33:03 +01003555{
3556 struct lysc_node *val1;
3557 struct lyd_node *val2;
3558
3559 val1 = *((struct lysc_node **)val1_p);
3560 val2 = *((struct lyd_node **)val2_p);
3561
Michal Vasko90932a92020-02-12 14:33:03 +01003562 if (val1 == val2->schema) {
3563 /* schema match is enough */
3564 return 1;
3565 } else {
3566 return 0;
3567 }
3568}
3569
Michal Vasko92239c72020-11-18 18:17:25 +01003570/**
3571 * @brief Search in the given siblings (NOT recursively) for the first schema node data instance.
3572 * Uses hashes - should be used whenever possible for best performance.
3573 *
3574 * @param[in] siblings Siblings to search in including preceding and succeeding nodes.
3575 * @param[in] schema Target data node schema to find.
3576 * @param[out] match Can be NULL, otherwise the found data node.
3577 * @return LY_SUCCESS on success, @p match set.
3578 * @return LY_ENOTFOUND if not found, @p match set to NULL.
3579 * @return LY_ERR value if another error occurred.
3580 */
Michal Vasko90932a92020-02-12 14:33:03 +01003581static LY_ERR
3582lyd_find_sibling_schema(const struct lyd_node *siblings, const struct lysc_node *schema, struct lyd_node **match)
3583{
3584 struct lyd_node **match_p;
3585 struct lyd_node_inner *parent;
3586 uint32_t hash;
Michal Vasko62524a92021-02-26 10:08:50 +01003587 lyht_value_equal_cb ht_cb;
Michal Vasko90932a92020-02-12 14:33:03 +01003588
Michal Vaskob104f112020-07-17 09:54:54 +02003589 assert(siblings && schema);
Michal Vasko90932a92020-02-12 14:33:03 +01003590
Michal Vasko9e685082021-01-29 14:49:09 +01003591 parent = siblings->parent;
Michal Vasko08fd6132020-11-18 18:17:45 +01003592 if (parent && parent->schema && parent->children_ht) {
Michal Vasko90932a92020-02-12 14:33:03 +01003593 /* calculate our hash */
3594 hash = dict_hash_multi(0, schema->module->name, strlen(schema->module->name));
3595 hash = dict_hash_multi(hash, schema->name, strlen(schema->name));
3596 hash = dict_hash_multi(hash, NULL, 0);
3597
3598 /* use special hash table function */
3599 ht_cb = lyht_set_cb(parent->children_ht, lyd_hash_table_schema_val_equal);
3600
3601 /* find by hash */
3602 if (!lyht_find(parent->children_ht, &schema, hash, (void **)&match_p)) {
3603 siblings = *match_p;
3604 } else {
3605 /* not found */
3606 siblings = NULL;
3607 }
3608
3609 /* set the original hash table compare function back */
3610 lyht_set_cb(parent->children_ht, ht_cb);
3611 } else {
Michal Vaskob104f112020-07-17 09:54:54 +02003612 /* find first sibling */
3613 if (siblings->parent) {
3614 siblings = siblings->parent->child;
3615 } else {
3616 while (siblings->prev->next) {
3617 siblings = siblings->prev;
3618 }
3619 }
3620
3621 /* search manually without hashes */
Michal Vaskod989ba02020-08-24 10:59:24 +02003622 for ( ; siblings; siblings = siblings->next) {
Michal Vasko90932a92020-02-12 14:33:03 +01003623 if (siblings->schema == schema) {
3624 /* schema match is enough */
3625 break;
3626 }
3627 }
3628 }
3629
3630 if (!siblings) {
Michal Vasko9b368d32020-02-14 13:53:31 +01003631 if (match) {
3632 *match = NULL;
3633 }
Michal Vasko90932a92020-02-12 14:33:03 +01003634 return LY_ENOTFOUND;
3635 }
3636
Michal Vasko9b368d32020-02-14 13:53:31 +01003637 if (match) {
3638 *match = (struct lyd_node *)siblings;
3639 }
Michal Vasko90932a92020-02-12 14:33:03 +01003640 return LY_SUCCESS;
3641}
3642
Michal Vaskoe444f752020-02-10 12:20:06 +01003643API LY_ERR
3644lyd_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 +02003645 size_t val_len, struct lyd_node **match)
Michal Vaskoe444f752020-02-10 12:20:06 +01003646{
3647 LY_ERR rc;
3648 struct lyd_node *target = NULL;
3649
Michal Vasko4c583e82020-07-17 12:16:14 +02003650 LY_CHECK_ARG_RET(NULL, schema, !(schema->nodetype & (LYS_CHOICE | LYS_CASE)), LY_EINVAL);
Michal Vaskoe444f752020-02-10 12:20:06 +01003651
Michal Vasko08fd6132020-11-18 18:17:45 +01003652 if (!siblings || (siblings->schema && (lysc_data_parent(siblings->schema) != lysc_data_parent(schema)))) {
Michal Vasko62ed12d2020-05-21 10:08:25 +02003653 /* no data or schema mismatch */
Michal Vasko9b368d32020-02-14 13:53:31 +01003654 if (match) {
3655 *match = NULL;
3656 }
Michal Vaskoe444f752020-02-10 12:20:06 +01003657 return LY_ENOTFOUND;
3658 }
3659
Michal Vaskof03ed032020-03-04 13:31:44 +01003660 if (key_or_value && !val_len) {
3661 val_len = strlen(key_or_value);
3662 }
3663
Michal Vaskob104f112020-07-17 09:54:54 +02003664 if ((schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) && key_or_value) {
3665 /* create a data node and find the instance */
3666 if (schema->nodetype == LYS_LEAFLIST) {
3667 /* target used attributes: schema, hash, value */
Michal Vaskofeca4fb2020-10-05 08:58:40 +02003668 rc = lyd_create_term(schema, key_or_value, val_len, NULL, LY_PREF_JSON, NULL, LYD_HINT_DATA, NULL, &target);
3669 LY_CHECK_RET(rc);
Michal Vaskob104f112020-07-17 09:54:54 +02003670 } else {
Michal Vasko90932a92020-02-12 14:33:03 +01003671 /* target used attributes: schema, hash, child (all keys) */
Michal Vasko004d3152020-06-11 19:59:22 +02003672 LY_CHECK_RET(lyd_create_list2(schema, key_or_value, val_len, &target));
Michal Vasko90932a92020-02-12 14:33:03 +01003673 }
3674
3675 /* find it */
3676 rc = lyd_find_sibling_first(siblings, target, match);
Michal Vaskob104f112020-07-17 09:54:54 +02003677 } else {
3678 /* find the first schema node instance */
3679 rc = lyd_find_sibling_schema(siblings, schema, match);
Michal Vaskoe444f752020-02-10 12:20:06 +01003680 }
3681
Michal Vaskoe444f752020-02-10 12:20:06 +01003682 lyd_free_tree(target);
3683 return rc;
3684}
Michal Vaskoccc02342020-05-21 10:09:21 +02003685
3686API LY_ERR
Michal Vasko1d4af6c2021-02-22 13:31:26 +01003687lyd_find_sibling_opaq_next(const struct lyd_node *first, const char *name, struct lyd_node **match)
3688{
3689 LY_CHECK_ARG_RET(NULL, name, LY_EINVAL);
3690
3691 for ( ; first; first = first->next) {
3692 if (!first->schema && !strcmp(LYD_NAME(first), name)) {
3693 break;
3694 }
3695 }
3696
3697 if (match) {
3698 *match = (struct lyd_node *)first;
3699 }
3700 return first ? LY_SUCCESS : LY_ENOTFOUND;
3701}
3702
3703API LY_ERR
Michal Vaskoccc02342020-05-21 10:09:21 +02003704lyd_find_xpath(const struct lyd_node *ctx_node, const char *xpath, struct ly_set **set)
3705{
3706 LY_ERR ret = LY_SUCCESS;
3707 struct lyxp_set xp_set;
Radek Krejcif03a9e22020-09-18 20:09:31 +02003708 struct lyxp_expr *exp = NULL;
Michal Vaskoccc02342020-05-21 10:09:21 +02003709 uint32_t i;
3710
3711 LY_CHECK_ARG_RET(NULL, ctx_node, xpath, set, LY_EINVAL);
3712
3713 memset(&xp_set, 0, sizeof xp_set);
3714
3715 /* compile expression */
Radek Krejcif03a9e22020-09-18 20:09:31 +02003716 ret = lyxp_expr_parse((struct ly_ctx *)LYD_CTX(ctx_node), xpath, 0, 1, &exp);
3717 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskoccc02342020-05-21 10:09:21 +02003718
3719 /* evaluate expression */
Michal Vasko400e9672021-01-11 13:39:17 +01003720 ret = lyxp_eval(LYD_CTX(ctx_node), exp, NULL, LY_PREF_JSON, NULL, ctx_node, ctx_node, &xp_set, LYXP_IGNORE_WHEN);
Michal Vaskoccc02342020-05-21 10:09:21 +02003721 LY_CHECK_GOTO(ret, cleanup);
3722
3723 /* allocate return set */
Radek Krejciba03a5a2020-08-27 14:40:41 +02003724 ret = ly_set_new(set);
3725 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskoccc02342020-05-21 10:09:21 +02003726
3727 /* transform into ly_set */
3728 if (xp_set.type == LYXP_SET_NODE_SET) {
3729 /* allocate memory for all the elements once (even though not all items must be elements but most likely will be) */
3730 (*set)->objs = malloc(xp_set.used * sizeof *(*set)->objs);
Michal Vaskob7be7a82020-08-20 09:09:04 +02003731 LY_CHECK_ERR_GOTO(!(*set)->objs, LOGMEM(LYD_CTX(ctx_node)); ret = LY_EMEM, cleanup);
Michal Vaskoccc02342020-05-21 10:09:21 +02003732 (*set)->size = xp_set.used;
3733
3734 for (i = 0; i < xp_set.used; ++i) {
3735 if (xp_set.val.nodes[i].type == LYXP_NODE_ELEM) {
Radek Krejci3d92e442020-10-12 12:48:13 +02003736 ret = ly_set_add(*set, xp_set.val.nodes[i].node, 1, NULL);
Radek Krejciba03a5a2020-08-27 14:40:41 +02003737 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskoccc02342020-05-21 10:09:21 +02003738 }
3739 }
3740 }
3741
3742cleanup:
Michal Vasko0691d522020-05-21 13:21:47 +02003743 lyxp_set_free_content(&xp_set);
Michal Vaskob7be7a82020-08-20 09:09:04 +02003744 lyxp_expr_free((struct ly_ctx *)LYD_CTX(ctx_node), exp);
Radek Krejci8f45abc2020-11-26 12:15:13 +01003745 if (ret) {
3746 ly_set_free(*set, NULL);
3747 *set = NULL;
3748 }
Michal Vaskoccc02342020-05-21 10:09:21 +02003749 return ret;
3750}
Radek Krejcica989142020-11-05 11:32:22 +01003751
Michal Vasko3e1f6552021-01-14 09:27:55 +01003752API LY_ERR
3753lyd_find_path(const struct lyd_node *ctx_node, const char *path, ly_bool output, struct lyd_node **match)
3754{
3755 LY_ERR ret = LY_SUCCESS;
3756 struct lyxp_expr *expr = NULL;
3757 struct ly_path *lypath = NULL;
3758
3759 LY_CHECK_ARG_RET(NULL, ctx_node, ctx_node->schema, path, LY_EINVAL);
3760
3761 /* parse the path */
3762 ret = ly_path_parse(LYD_CTX(ctx_node), ctx_node->schema, path, strlen(path), LY_PATH_BEGIN_EITHER, LY_PATH_LREF_FALSE,
3763 LY_PATH_PREFIX_OPTIONAL, LY_PATH_PRED_SIMPLE, &expr);
3764 LY_CHECK_GOTO(ret, cleanup);
3765
3766 /* compile the path */
3767 ret = ly_path_compile(LYD_CTX(ctx_node), NULL, ctx_node->schema, expr, LY_PATH_LREF_FALSE,
3768 output ? LY_PATH_OPER_OUTPUT : LY_PATH_OPER_INPUT, LY_PATH_TARGET_SINGLE, LY_PREF_JSON,
3769 (void *)LYD_CTX(ctx_node), NULL, &lypath);
3770 LY_CHECK_GOTO(ret, cleanup);
3771
3772 /* evaluate the path */
3773 ret = ly_path_eval_partial(lypath, ctx_node, NULL, match);
3774
3775cleanup:
3776 lyxp_expr_free(LYD_CTX(ctx_node), expr);
3777 ly_path_free(LYD_CTX(ctx_node), lypath);
3778 return ret;
3779}
3780
Radek Krejcica989142020-11-05 11:32:22 +01003781API uint32_t
3782lyd_list_pos(const struct lyd_node *instance)
3783{
3784 const struct lyd_node *iter = NULL;
3785 uint32_t pos = 0;
3786
3787 if (!instance || !(instance->schema->nodetype & (LYS_LIST | LYS_LEAFLIST))) {
3788 return 0;
3789 }
3790
3791 /* data instances are ordered, so we can stop when we found instance of other schema node */
3792 for (iter = instance; iter->schema == instance->schema; iter = iter->prev) {
Radek Krejcibb27df32020-11-13 15:39:16 +01003793 if (pos && (iter->next == NULL)) {
Radek Krejcica989142020-11-05 11:32:22 +01003794 /* overrun to the end of the siblings list */
3795 break;
3796 }
3797 ++pos;
3798 }
3799
3800 return pos;
3801}
Radek Krejci4233f9b2020-11-05 12:38:35 +01003802
3803API struct lyd_node *
3804lyd_first_sibling(const struct lyd_node *node)
3805{
3806 struct lyd_node *start;
3807
3808 if (!node) {
3809 return NULL;
3810 }
3811
3812 /* get the first sibling */
3813 if (node->parent) {
3814 start = node->parent->child;
3815 } else {
Radek Krejcibb27df32020-11-13 15:39:16 +01003816 for (start = (struct lyd_node *)node; start->prev->next; start = start->prev) {}
Radek Krejci4233f9b2020-11-05 12:38:35 +01003817 }
3818
3819 return start;
3820}