blob: f7404ef738fbacbdc6d0c913c4175a435956fbd8 [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 Krejci0aa1f702021-04-01 16:16:19 +020040#include "plugins.h"
Radek Krejcif1ca0ac2021-04-12 16:00:06 +020041#include "plugins_exts/metadata.h"
Radek Krejci3e6632f2021-03-22 22:08:21 +010042#include "plugins_internal.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"
Radek Krejci859a15a2021-03-05 20:56:59 +010047#include "tree_edit.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020048#include "tree_schema.h"
49#include "tree_schema_internal.h"
Michal Vaskoa6669ba2020-08-06 16:14:26 +020050#include "validation.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020051#include "xml.h"
52#include "xpath.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020053
Michal Vaskob104f112020-07-17 09:54:54 +020054static LY_ERR lyd_find_sibling_schema(const struct lyd_node *siblings, const struct lysc_node *schema,
Radek Krejci0f969882020-08-21 16:56:47 +020055 struct lyd_node **match);
Michal Vaskob104f112020-07-17 09:54:54 +020056
Radek Krejci084289f2019-07-09 17:35:30 +020057LY_ERR
Michal Vaskofeca4fb2020-10-05 08:58:40 +020058lyd_value_store(const struct ly_ctx *ctx, struct lyd_value *val, const struct lysc_type *type, const char *value,
Radek Krejci8df109d2021-04-23 12:19:08 +020059 size_t value_len, ly_bool *dynamic, LY_VALUE_FORMAT format, void *prefix_data, uint32_t hints,
Radek Krejci2efc45b2020-12-22 16:25:44 +010060 const struct lysc_node *ctx_node, ly_bool *incomplete)
Radek Krejci084289f2019-07-09 17:35:30 +020061{
Michal Vaskofeca4fb2020-10-05 08:58:40 +020062 LY_ERR ret;
Radek Krejci084289f2019-07-09 17:35:30 +020063 struct ly_err_item *err = NULL;
Radek Krejci0b013302021-03-29 15:22:32 +020064 uint32_t options = (dynamic && *dynamic ? LYPLG_TYPE_STORE_DYNAMIC : 0);
Radek Krejci084289f2019-07-09 17:35:30 +020065
Michal Vaskofeca4fb2020-10-05 08:58:40 +020066 if (incomplete) {
67 *incomplete = 0;
Radek Krejci084289f2019-07-09 17:35:30 +020068 }
69
Michal Vasko405cc9e2020-12-01 12:01:27 +010070 ret = type->plugin->store(ctx, type, value, value_len, options, format, prefix_data, hints, ctx_node, val, NULL, &err);
Radek Iša96b2de12021-02-16 10:32:57 +010071 if (dynamic) {
72 *dynamic = 0;
73 }
74
Michal Vasko90932a92020-02-12 14:33:03 +010075 if (ret == LY_EINCOMPLETE) {
Michal Vaskofeca4fb2020-10-05 08:58:40 +020076 if (incomplete) {
77 *incomplete = 1;
78 }
79 } else if (ret) {
80 if (err) {
Radek Krejci2efc45b2020-12-22 16:25:44 +010081 LOGVAL(ctx, err->vecode, err->msg);
Michal Vaskofeca4fb2020-10-05 08:58:40 +020082 ly_err_free(err);
83 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +010084 LOGVAL(ctx, LYVE_OTHER, "Storing value \"%.*s\" failed.", (int)value_len, value);
Michal Vaskofeca4fb2020-10-05 08:58:40 +020085 }
86 return ret;
Michal Vasko90932a92020-02-12 14:33:03 +010087 }
88
Michal Vaskofeca4fb2020-10-05 08:58:40 +020089 return LY_SUCCESS;
Michal Vasko90932a92020-02-12 14:33:03 +010090}
91
Radek Krejci38d85362019-09-05 16:26:38 +020092LY_ERR
Michal Vaskofeca4fb2020-10-05 08:58:40 +020093lyd_value_validate_incomplete(const struct ly_ctx *ctx, const struct lysc_type *type, struct lyd_value *val,
Radek Krejci2efc45b2020-12-22 16:25:44 +010094 const struct lyd_node *ctx_node, const struct lyd_node *tree)
Radek Krejci38d85362019-09-05 16:26:38 +020095{
Michal Vaskofeca4fb2020-10-05 08:58:40 +020096 LY_ERR ret;
Radek Krejci38d85362019-09-05 16:26:38 +020097 struct ly_err_item *err = NULL;
Radek Krejci38d85362019-09-05 16:26:38 +020098
Michal Vaskofeca4fb2020-10-05 08:58:40 +020099 assert(type->plugin->validate);
Michal Vasko8d544252020-03-02 10:19:52 +0100100
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200101 ret = type->plugin->validate(ctx, type, ctx_node, tree, val, &err);
102 if (ret) {
Radek Krejci38d85362019-09-05 16:26:38 +0200103 if (err) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100104 LOGVAL(ctx, err->vecode, err->msg);
Radek Krejci38d85362019-09-05 16:26:38 +0200105 ly_err_free(err);
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200106 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100107 LOGVAL(ctx, LYVE_OTHER, "Resolving value \"%s\" failed.", val->canonical);
Radek Krejci38d85362019-09-05 16:26:38 +0200108 }
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200109 return ret;
Radek Krejci38d85362019-09-05 16:26:38 +0200110 }
111
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200112 return LY_SUCCESS;
Radek Krejci38d85362019-09-05 16:26:38 +0200113}
114
Michal Vaskof937cfe2020-08-03 16:07:12 +0200115LY_ERR
Michal Vaskoaebbce02021-04-06 09:23:37 +0200116lys_value_validate(const struct ly_ctx *ctx, const struct lysc_node *node, const char *value, size_t value_len,
Radek Krejci8df109d2021-04-23 12:19:08 +0200117 LY_VALUE_FORMAT format, void *prefix_data)
Radek Krejci084289f2019-07-09 17:35:30 +0200118{
119 LY_ERR rc = LY_SUCCESS;
120 struct ly_err_item *err = NULL;
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200121 struct lyd_value storage;
Radek Krejci084289f2019-07-09 17:35:30 +0200122 struct lysc_type *type;
Radek Krejci084289f2019-07-09 17:35:30 +0200123
124 LY_CHECK_ARG_RET(ctx, node, value, LY_EINVAL);
125
126 if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
127 LOGARG(ctx, node);
128 return LY_EINVAL;
129 }
130
Michal Vasko22df3f02020-08-24 13:29:22 +0200131 type = ((struct lysc_node_leaf *)node)->type;
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200132 rc = type->plugin->store(ctx ? ctx : node->module->ctx, type, value, value_len, 0, format, prefix_data,
Michal Vasko405cc9e2020-12-01 12:01:27 +0100133 LYD_HINT_SCHEMA, node, &storage, NULL, &err);
Radek Krejci73dead22019-07-11 16:46:16 +0200134 if (rc == LY_EINCOMPLETE) {
135 /* actually success since we do not provide the context tree and call validation with
136 * LY_TYPE_OPTS_INCOMPLETE_DATA */
137 rc = LY_SUCCESS;
138 } else if (rc && err) {
139 if (ctx) {
140 /* log only in case the ctx was provided as input parameter */
Radek Krejciddace2c2021-01-08 11:30:56 +0100141 LOG_LOCSET(NULL, NULL, err->path, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100142 LOGVAL(ctx, err->vecode, err->msg);
Radek Krejciddace2c2021-01-08 11:30:56 +0100143 LOG_LOCBACK(0, 0, 1, 0);
Radek Krejci084289f2019-07-09 17:35:30 +0200144 }
Radek Krejci73dead22019-07-11 16:46:16 +0200145 ly_err_free(err);
Radek Krejci084289f2019-07-09 17:35:30 +0200146 }
147
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200148 if (!rc) {
149 type->plugin->free(ctx ? ctx : node->module->ctx, &storage);
150 }
Radek Krejci084289f2019-07-09 17:35:30 +0200151 return rc;
152}
153
154API LY_ERR
Michal Vaskoaebbce02021-04-06 09:23:37 +0200155lyd_value_validate(const struct ly_ctx *ctx, const struct lysc_node *schema, const char *value, size_t value_len,
156 const struct lyd_node *ctx_node, const struct lysc_type **realtype, const char **canonical)
Radek Krejci084289f2019-07-09 17:35:30 +0200157{
158 LY_ERR rc;
159 struct ly_err_item *err = NULL;
160 struct lysc_type *type;
Michal Vasko3701af52020-08-03 14:29:38 +0200161 struct lyd_value val = {0};
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200162 ly_bool stored = 0;
Radek Krejci084289f2019-07-09 17:35:30 +0200163
Michal Vaskoaebbce02021-04-06 09:23:37 +0200164 LY_CHECK_ARG_RET(ctx, schema, value, LY_EINVAL);
Radek Krejci084289f2019-07-09 17:35:30 +0200165
Michal Vaskoaebbce02021-04-06 09:23:37 +0200166 type = ((struct lysc_node_leaf *)schema)->type;
167
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200168 /* store */
Radek Krejci8df109d2021-04-23 12:19:08 +0200169 rc = type->plugin->store(ctx ? ctx : schema->module->ctx, type, value, value_len, 0, LY_VALUE_JSON, NULL,
Michal Vaskoaebbce02021-04-06 09:23:37 +0200170 LYD_HINT_DATA, schema, &val, NULL, &err);
171 if (!rc || (rc == LY_EINCOMPLETE)) {
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200172 stored = 1;
Michal Vaskoaebbce02021-04-06 09:23:37 +0200173 }
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200174
Michal Vaskoaebbce02021-04-06 09:23:37 +0200175 if (ctx_node && (rc == LY_EINCOMPLETE)) {
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200176 /* resolve */
Michal Vaskoaebbce02021-04-06 09:23:37 +0200177 rc = type->plugin->validate(ctx ? ctx : schema->module->ctx, type, ctx_node, ctx_node, &val, &err);
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200178 }
179
Michal Vaskoaebbce02021-04-06 09:23:37 +0200180 if (rc && (rc != LY_EINCOMPLETE) && err) {
181 if (ctx) {
182 /* log error */
183 if (err->path) {
184 LOG_LOCSET(NULL, NULL, err->path, NULL);
185 } else if (ctx_node) {
186 LOG_LOCSET(NULL, ctx_node, NULL, NULL);
187 } else {
188 LOG_LOCSET(schema, NULL, NULL, NULL);
Radek Krejci084289f2019-07-09 17:35:30 +0200189 }
Michal Vaskoaebbce02021-04-06 09:23:37 +0200190 LOGVAL(ctx, err->vecode, err->msg);
191 if (err->path) {
192 LOG_LOCBACK(0, 0, 1, 0);
193 } else if (ctx_node) {
194 LOG_LOCBACK(0, 1, 0, 0);
195 } else {
196 LOG_LOCBACK(1, 0, 0, 0);
197 }
Radek Krejci084289f2019-07-09 17:35:30 +0200198 }
Michal Vaskoaebbce02021-04-06 09:23:37 +0200199 ly_err_free(err);
Radek Krejci084289f2019-07-09 17:35:30 +0200200 }
201
Michal Vaskoaebbce02021-04-06 09:23:37 +0200202 if (!rc || (rc == LY_EINCOMPLETE)) {
203 if (realtype) {
204 /* return realtype */
205 if (val.realtype->basetype == LY_TYPE_UNION) {
206 *realtype = val.subvalue->value.realtype;
207 } else {
208 *realtype = val.realtype;
209 }
210 }
211
212 if (canonical) {
213 /* return canonical value */
214 lydict_insert(ctx ? ctx : schema->module->ctx, val.canonical, 0, canonical);
Michal Vasko29134f82020-11-13 18:03:20 +0100215 }
Michal Vasko3701af52020-08-03 14:29:38 +0200216 }
217
Michal Vaskoaebbce02021-04-06 09:23:37 +0200218 if (stored) {
219 /* free value */
220 type->plugin->free(ctx ? ctx : schema->module->ctx, &val);
221 }
222 return rc;
Radek Krejci084289f2019-07-09 17:35:30 +0200223}
224
225API LY_ERR
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200226lyd_value_compare(const struct lyd_node_term *node, const char *value, size_t value_len)
Radek Krejci084289f2019-07-09 17:35:30 +0200227{
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200228 LY_ERR ret = LY_SUCCESS;
Radek Krejci084289f2019-07-09 17:35:30 +0200229 struct ly_ctx *ctx;
230 struct lysc_type *type;
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200231 struct lyd_value val = {0};
Radek Krejci084289f2019-07-09 17:35:30 +0200232
233 LY_CHECK_ARG_RET(node ? node->schema->module->ctx : NULL, node, value, LY_EINVAL);
234
235 ctx = node->schema->module->ctx;
Michal Vasko22df3f02020-08-24 13:29:22 +0200236 type = ((struct lysc_node_leaf *)node->schema)->type;
Radek Krejci084289f2019-07-09 17:35:30 +0200237
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200238 /* store the value */
Michal Vasko9e685082021-01-29 14:49:09 +0100239 LOG_LOCSET(node->schema, &node->node, NULL, NULL);
Radek Krejci8df109d2021-04-23 12:19:08 +0200240 ret = lyd_value_store(ctx, &val, type, value, value_len, NULL, LY_VALUE_JSON, NULL, LYD_HINT_DATA, node->schema, NULL);
Radek Krejciddace2c2021-01-08 11:30:56 +0100241 LOG_LOCBACK(1, 1, 0, 0);
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200242 LY_CHECK_RET(ret);
Radek Krejci084289f2019-07-09 17:35:30 +0200243
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200244 /* compare values */
245 ret = type->plugin->compare(&node->value, &val);
Radek Krejci084289f2019-07-09 17:35:30 +0200246
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200247 type->plugin->free(ctx, &val);
Radek Krejci084289f2019-07-09 17:35:30 +0200248 return ret;
249}
250
Radek Krejci19611252020-10-04 13:54:53 +0200251API ly_bool
252lyd_is_default(const struct lyd_node *node)
253{
254 const struct lysc_node_leaf *leaf;
255 const struct lysc_node_leaflist *llist;
256 const struct lyd_node_term *term;
257 LY_ARRAY_COUNT_TYPE u;
258
259 assert(node->schema->nodetype & LYD_NODE_TERM);
260 term = (const struct lyd_node_term *)node;
261
262 if (node->schema->nodetype == LYS_LEAF) {
263 leaf = (const struct lysc_node_leaf *)node->schema;
264 if (!leaf->dflt) {
265 return 0;
266 }
267
268 /* compare with the default value */
269 if (leaf->type->plugin->compare(&term->value, leaf->dflt)) {
270 return 0;
271 }
272 } else {
273 llist = (const struct lysc_node_leaflist *)node->schema;
274 if (!llist->dflts) {
275 return 0;
276 }
277
278 LY_ARRAY_FOR(llist->dflts, u) {
279 /* compare with each possible default value */
280 if (llist->type->plugin->compare(&term->value, llist->dflts[u])) {
281 return 0;
282 }
283 }
284 }
285
286 return 1;
287}
288
Radek Krejci7931b192020-06-25 17:05:03 +0200289static LYD_FORMAT
Michal Vasko63f3d842020-07-08 10:10:14 +0200290lyd_parse_get_format(const struct ly_in *in, LYD_FORMAT format)
Radek Krejcie7b95092019-05-15 11:03:07 +0200291{
Michal Vasko69730152020-10-09 16:30:07 +0200292 if (!format && (in->type == LY_IN_FILEPATH)) {
Radek Krejcie7b95092019-05-15 11:03:07 +0200293 /* unknown format - try to detect it from filename's suffix */
Radek Krejci7931b192020-06-25 17:05:03 +0200294 const char *path = in->method.fpath.filepath;
295 size_t len = strlen(path);
Radek Krejcie7b95092019-05-15 11:03:07 +0200296
297 /* ignore trailing whitespaces */
Michal Vaskod989ba02020-08-24 10:59:24 +0200298 for ( ; len > 0 && isspace(path[len - 1]); len--) {}
Radek Krejcie7b95092019-05-15 11:03:07 +0200299
Radek Krejcif13b87b2020-12-01 22:02:17 +0100300 if ((len >= LY_XML_SUFFIX_LEN + 1) &&
301 !strncmp(&path[len - LY_XML_SUFFIX_LEN], LY_XML_SUFFIX, LY_XML_SUFFIX_LEN)) {
Radek Krejcie7b95092019-05-15 11:03:07 +0200302 format = LYD_XML;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100303 } else if ((len >= LY_JSON_SUFFIX_LEN + 1) &&
304 !strncmp(&path[len - LY_JSON_SUFFIX_LEN], LY_JSON_SUFFIX, LY_JSON_SUFFIX_LEN)) {
Radek Krejcie7b95092019-05-15 11:03:07 +0200305 format = LYD_JSON;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100306 } else if ((len >= LY_LYB_SUFFIX_LEN + 1) &&
307 !strncmp(&path[len - LY_LYB_SUFFIX_LEN], LY_LYB_SUFFIX, LY_LYB_SUFFIX_LEN)) {
Radek Krejcie7b95092019-05-15 11:03:07 +0200308 format = LYD_LYB;
Radek Krejci7931b192020-06-25 17:05:03 +0200309 } /* else still unknown */
Radek Krejcie7b95092019-05-15 11:03:07 +0200310 }
311
Radek Krejci7931b192020-06-25 17:05:03 +0200312 return format;
313}
Radek Krejcie7b95092019-05-15 11:03:07 +0200314
Michal Vaskoe0665742021-02-11 11:08:44 +0100315/**
316 * @brief Parse YANG data into a data tree.
317 *
318 * @param[in] ctx libyang context.
Radek Krejcif16e2542021-02-17 15:39:23 +0100319 * @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 +0100320 * @param[in] parent Parent to connect the parsed nodes to, if any.
321 * @param[in,out] first_p Pointer to the first top-level parsed node, used only if @p parent is NULL.
322 * @param[in] in Input handle to read the input from.
323 * @param[in] format Expected format of the data in @p in.
324 * @param[in] parse_opts Options for parser.
325 * @param[in] val_opts Options for validation.
326 * @param[out] op Optional pointer to the parsed operation, if any.
327 * @return LY_ERR value.
328 */
329static LY_ERR
Radek Krejcif16e2542021-02-17 15:39:23 +0100330lyd_parse(const struct ly_ctx *ctx, const struct lysc_ext_instance *ext, struct lyd_node *parent, struct lyd_node **first_p,
331 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 +0200332{
Michal Vaskoe0665742021-02-11 11:08:44 +0100333 LY_ERR rc = LY_SUCCESS;
Radek Krejci1798aae2020-07-14 13:26:06 +0200334 struct lyd_ctx *lydctx = NULL;
Michal Vaskoe0665742021-02-11 11:08:44 +0100335 struct ly_set parsed = {0};
336 struct lyd_node *first;
337 uint32_t i;
Radek Krejci1798aae2020-07-14 13:26:06 +0200338
Michal Vaskoe0665742021-02-11 11:08:44 +0100339 assert(ctx && (parent || first_p));
Radek Krejci7931b192020-06-25 17:05:03 +0200340
341 format = lyd_parse_get_format(in, format);
Michal Vaskoe0665742021-02-11 11:08:44 +0100342 if (first_p) {
343 *first_p = NULL;
344 }
Radek Krejci1798aae2020-07-14 13:26:06 +0200345
Michal Vasko63f3d842020-07-08 10:10:14 +0200346 /* remember input position */
347 in->func_start = in->current;
Radek Krejci7931b192020-06-25 17:05:03 +0200348
Michal Vaskoe0665742021-02-11 11:08:44 +0100349 /* parse the data */
Radek Krejci7931b192020-06-25 17:05:03 +0200350 switch (format) {
351 case LYD_XML:
Radek Krejcif16e2542021-02-17 15:39:23 +0100352 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 +0200353 break;
Radek Krejci7931b192020-06-25 17:05:03 +0200354 case LYD_JSON:
Radek Krejcif16e2542021-02-17 15:39:23 +0100355 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 +0200356 break;
Radek Krejci7931b192020-06-25 17:05:03 +0200357 case LYD_LYB:
Radek Krejcif16e2542021-02-17 15:39:23 +0100358 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 +0200359 break;
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200360 case LYD_UNKNOWN:
Michal Vaskod74978f2021-02-12 11:59:36 +0100361 LOGARG(ctx, format);
362 rc = LY_EINVAL;
Michal Vaskoe0665742021-02-11 11:08:44 +0100363 break;
364 }
365 LY_CHECK_GOTO(rc, cleanup);
366
367 if (parent) {
368 /* get first top-level sibling */
369 for (first = parent; first->parent; first = lyd_parent(first)) {}
370 first = lyd_first_sibling(first);
371 first_p = &first;
Radek Krejci7931b192020-06-25 17:05:03 +0200372 }
373
Michal Vaskoe0665742021-02-11 11:08:44 +0100374 if (!(parse_opts & LYD_PARSE_ONLY)) {
375 /* validate data */
Radek Krejci4f2e3e52021-03-30 14:20:28 +0200376 rc = lyd_validate(first_p, NULL, ctx, val_opts, 0, &lydctx->node_when, &lydctx->node_exts, &lydctx->node_types,
Michal Vaskoe0665742021-02-11 11:08:44 +0100377 &lydctx->meta_types, NULL);
378 LY_CHECK_GOTO(rc, cleanup);
379 }
Radek Krejci7931b192020-06-25 17:05:03 +0200380
Michal Vaskoe0665742021-02-11 11:08:44 +0100381 /* set the operation node */
382 if (op) {
383 *op = lydctx->op_node;
Radek Krejci1798aae2020-07-14 13:26:06 +0200384 }
385
386cleanup:
Michal Vaskoe0665742021-02-11 11:08:44 +0100387 if (lydctx) {
388 lydctx->free(lydctx);
Radek Krejci1798aae2020-07-14 13:26:06 +0200389 }
Michal Vaskoe0665742021-02-11 11:08:44 +0100390 if (rc) {
391 if (parent) {
392 /* free all the parsed subtrees */
393 for (i = 0; i < parsed.count; ++i) {
394 lyd_free_tree(parsed.dnodes[i]);
395 }
396 } else {
397 /* free everything */
398 lyd_free_all(*first_p);
399 *first_p = NULL;
400 }
401 }
402 ly_set_erase(&parsed, NULL);
403 return rc;
Radek Krejci7931b192020-06-25 17:05:03 +0200404}
405
406API LY_ERR
Radek Krejcif16e2542021-02-17 15:39:23 +0100407lyd_parse_ext_data(const struct lysc_ext_instance *ext, struct lyd_node *parent, struct ly_in *in, LYD_FORMAT format,
408 uint32_t parse_options, uint32_t validate_options, struct lyd_node **tree)
409{
410 const struct ly_ctx *ctx = ext ? ext->module->ctx : NULL;
411
412 LY_CHECK_ARG_RET(ctx, ext, in, parent || tree, LY_EINVAL);
413 LY_CHECK_ARG_RET(ctx, !(parse_options & ~LYD_PARSE_OPTS_MASK), LY_EINVAL);
414 LY_CHECK_ARG_RET(ctx, !(validate_options & ~LYD_VALIDATE_OPTS_MASK), LY_EINVAL);
415
416 return lyd_parse(ctx, ext, parent, tree, in, format, parse_options, validate_options, NULL);
417}
418
419API LY_ERR
Michal Vaskoe0665742021-02-11 11:08:44 +0100420lyd_parse_data(const struct ly_ctx *ctx, struct lyd_node *parent, struct ly_in *in, LYD_FORMAT format,
421 uint32_t parse_options, uint32_t validate_options, struct lyd_node **tree)
422{
423 LY_CHECK_ARG_RET(ctx, ctx, in, parent || tree, LY_EINVAL);
424 LY_CHECK_ARG_RET(ctx, !(parse_options & ~LYD_PARSE_OPTS_MASK), LY_EINVAL);
425 LY_CHECK_ARG_RET(ctx, !(validate_options & ~LYD_VALIDATE_OPTS_MASK), LY_EINVAL);
426
Radek Krejcif16e2542021-02-17 15:39:23 +0100427 return lyd_parse(ctx, NULL, parent, tree, in, format, parse_options, validate_options, NULL);
Michal Vaskoe0665742021-02-11 11:08:44 +0100428}
429
430API LY_ERR
431lyd_parse_data_mem(const struct ly_ctx *ctx, const char *data, LYD_FORMAT format, uint32_t parse_options,
432 uint32_t validate_options, struct lyd_node **tree)
Radek Krejci7931b192020-06-25 17:05:03 +0200433{
434 LY_ERR ret;
435 struct ly_in *in;
436
437 LY_CHECK_RET(ly_in_new_memory(data, &in));
Michal Vaskoe0665742021-02-11 11:08:44 +0100438 ret = lyd_parse_data(ctx, NULL, in, format, parse_options, validate_options, tree);
Radek Krejci7931b192020-06-25 17:05:03 +0200439
440 ly_in_free(in, 0);
441 return ret;
442}
443
444API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200445lyd_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 +0200446 struct lyd_node **tree)
Radek Krejci7931b192020-06-25 17:05:03 +0200447{
448 LY_ERR ret;
449 struct ly_in *in;
450
451 LY_CHECK_RET(ly_in_new_fd(fd, &in));
Michal Vaskoe0665742021-02-11 11:08:44 +0100452 ret = lyd_parse_data(ctx, NULL, in, format, parse_options, validate_options, tree);
Radek Krejci7931b192020-06-25 17:05:03 +0200453
454 ly_in_free(in, 0);
455 return ret;
456}
457
458API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200459lyd_parse_data_path(const struct ly_ctx *ctx, const char *path, LYD_FORMAT format, uint32_t parse_options,
460 uint32_t validate_options, struct lyd_node **tree)
Radek Krejci7931b192020-06-25 17:05:03 +0200461{
462 LY_ERR ret;
463 struct ly_in *in;
464
465 LY_CHECK_RET(ly_in_new_filepath(path, 0, &in));
Michal Vaskoe0665742021-02-11 11:08:44 +0100466 ret = lyd_parse_data(ctx, NULL, in, format, parse_options, validate_options, tree);
Radek Krejci7931b192020-06-25 17:05:03 +0200467
468 ly_in_free(in, 0);
469 return ret;
470}
471
Radek Krejcif16e2542021-02-17 15:39:23 +0100472/**
473 * @brief Parse YANG data into an operation data tree, in case the extension instance is specified, keep the searching
474 * for schema nodes locked inside the extension instance.
475 *
476 * At least one of @p parent, @p tree, or @p op must always be set.
477 *
478 * Specific @p data_type values have different parameter meaning as follows:
479 * - ::LYD_TYPE_RPC_NETCONF:
480 * - @p parent - must be NULL, the whole RPC is expected;
481 * - @p format - must be ::LYD_XML, NETCONF supports only this format;
482 * - @p tree - must be provided, all the NETCONF-specific XML envelopes will be returned here as
483 * a separate opaque data tree, even if the function fails, this may be returned;
484 * - @p op - must be provided, the RPC/action data tree itself will be returned here, pointing to the operation;
485 *
486 * - ::LYD_TYPE_NOTIF_NETCONF:
487 * - @p parent - must be NULL, the whole notification is expected;
488 * - @p format - must be ::LYD_XML, NETCONF supports only this format;
489 * - @p tree - must be provided, all the NETCONF-specific XML envelopes will be returned here as
490 * a separate opaque data tree, even if the function fails, this may be returned;
491 * - @p op - must be provided, the notification data tree itself will be returned here, pointing to the operation;
492 *
493 * - ::LYD_TYPE_REPLY_NETCONF:
494 * - @p parent - must be set, pointing to the invoked RPC operation (RPC or action) node;
495 * - @p format - must be ::LYD_XML, NETCONF supports only this format;
496 * - @p tree - must be provided, all the NETCONF-specific XML envelopes will be returned here as
497 * a separate opaque data tree, even if the function fails, this may be returned;
498 * - @p op - must be NULL, the reply is appended to the RPC;
499 * Note that there are 3 kinds of NETCONF replies - ok, error, and data. Only data reply appends any nodes to the RPC.
500 *
501 * @param[in] ctx libyang context.
502 * @param[in] ext Extension instance providing the specific schema tree to match with the data being parsed.
503 * @param[in] parent Optional parent to connect the parsed nodes to.
504 * @param[in] in Input handle to read the input from.
505 * @param[in] format Expected format of the data in @p in.
506 * @param[in] data_type Expected operation to parse (@ref datatype).
507 * @param[out] tree Optional full parsed data tree. If @p parent is set, set to NULL.
508 * @param[out] op Optional parsed operation node.
509 * @return LY_ERR value.
510 * @return LY_ENOT if @p data_type is a NETCONF message and the root XML element is not the expected one.
511 */
512static LY_ERR
513lyd_parse_op_(const struct ly_ctx *ctx, const struct lysc_ext_instance *ext, struct lyd_node *parent,
514 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 +0200515{
Michal Vaskoe0665742021-02-11 11:08:44 +0100516 LY_ERR rc = LY_SUCCESS;
517 struct lyd_ctx *lydctx = NULL;
518 struct ly_set parsed = {0};
519 struct lyd_node *first = NULL, *envp = NULL;
520 uint32_t i, parse_opts, val_opts;
Radek Krejci7931b192020-06-25 17:05:03 +0200521
Michal Vasko27fb0262021-02-23 09:42:01 +0100522 if (!ctx) {
523 ctx = LYD_CTX(parent);
524 }
Radek Krejci1798aae2020-07-14 13:26:06 +0200525 if (tree) {
526 *tree = NULL;
527 }
528 if (op) {
529 *op = NULL;
530 }
531
Radek Krejci7931b192020-06-25 17:05:03 +0200532 format = lyd_parse_get_format(in, format);
Radek Krejci7931b192020-06-25 17:05:03 +0200533
Michal Vasko63f3d842020-07-08 10:10:14 +0200534 /* remember input position */
535 in->func_start = in->current;
536
Michal Vaskoe0665742021-02-11 11:08:44 +0100537 /* check params based on the data type */
Michal Vasko1e4c68e2021-02-18 15:03:01 +0100538 if ((data_type == LYD_TYPE_RPC_NETCONF) || (data_type == LYD_TYPE_NOTIF_NETCONF)) {
Michal Vaskoe0665742021-02-11 11:08:44 +0100539 LY_CHECK_ARG_RET(ctx, format == LYD_XML, !parent, tree, op, LY_EINVAL);
Michal Vasko1e4c68e2021-02-18 15:03:01 +0100540 } else if (data_type == LYD_TYPE_REPLY_NETCONF) {
541 LY_CHECK_ARG_RET(ctx, format == LYD_XML, parent, parent->schema->nodetype & (LYS_RPC | LYS_ACTION), tree, !op,
542 LY_EINVAL);
Michal Vaskoe0665742021-02-11 11:08:44 +0100543 }
544 parse_opts = LYD_PARSE_ONLY | LYD_PARSE_STRICT;
545 val_opts = 0;
546
547 /* parse the data */
Radek Krejci7931b192020-06-25 17:05:03 +0200548 switch (format) {
549 case LYD_XML:
Radek Krejcif16e2542021-02-17 15:39:23 +0100550 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 +0100551 if (rc && envp) {
552 /* special situation when the envelopes were parsed successfully */
553 if (tree) {
554 *tree = envp;
555 }
556 ly_set_erase(&parsed, NULL);
557 return rc;
558 }
Michal Vaskoe0665742021-02-11 11:08:44 +0100559 break;
Radek Krejci7931b192020-06-25 17:05:03 +0200560 case LYD_JSON:
Radek Krejcif16e2542021-02-17 15:39:23 +0100561 rc = lyd_parse_json(ctx, ext, parent, &first, in, parse_opts, val_opts, data_type, &parsed, &lydctx);
Michal Vaskoe0665742021-02-11 11:08:44 +0100562 break;
Radek Krejci7931b192020-06-25 17:05:03 +0200563 case LYD_LYB:
Radek Krejcif16e2542021-02-17 15:39:23 +0100564 rc = lyd_parse_lyb(ctx, ext, parent, &first, in, parse_opts, val_opts, data_type, &parsed, &lydctx);
Michal Vaskoe0665742021-02-11 11:08:44 +0100565 break;
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200566 case LYD_UNKNOWN:
Michal Vaskod74978f2021-02-12 11:59:36 +0100567 LOGARG(ctx, format);
568 rc = LY_EINVAL;
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200569 break;
Radek Krejci7931b192020-06-25 17:05:03 +0200570 }
Michal Vaskoe0665742021-02-11 11:08:44 +0100571 LY_CHECK_GOTO(rc, cleanup);
Radek Krejci7931b192020-06-25 17:05:03 +0200572
Michal Vaskoe0665742021-02-11 11:08:44 +0100573 /* set out params correctly */
574 if (tree) {
575 if (envp) {
576 /* special out param meaning */
577 *tree = envp;
578 } else {
579 *tree = parent ? NULL : first;
580 }
581 }
582 if (op) {
583 *op = lydctx->op_node;
584 }
585
586cleanup:
587 if (lydctx) {
588 lydctx->free(lydctx);
589 }
590 if (rc) {
591 if (parent) {
592 /* free all the parsed subtrees */
593 for (i = 0; i < parsed.count; ++i) {
594 lyd_free_tree(parsed.dnodes[i]);
595 }
596 } else {
597 /* free everything (cannot occur in the current code, a safety) */
598 lyd_free_all(first);
599 if (tree) {
600 *tree = NULL;
601 }
602 if (op) {
603 *op = NULL;
604 }
605 }
606 }
607 ly_set_erase(&parsed, NULL);
608 return rc;
Radek Krejcie7b95092019-05-15 11:03:07 +0200609}
Radek Krejci084289f2019-07-09 17:35:30 +0200610
Radek Krejcif16e2542021-02-17 15:39:23 +0100611API LY_ERR
612lyd_parse_op(const struct ly_ctx *ctx, struct lyd_node *parent, struct ly_in *in, LYD_FORMAT format,
613 enum lyd_type data_type, struct lyd_node **tree, struct lyd_node **op)
614{
615 LY_CHECK_ARG_RET(ctx, ctx || parent, in, data_type, parent || tree || op, LY_EINVAL);
616
617 return lyd_parse_op_(ctx, NULL, parent, in, format, data_type, tree, op);
618}
619
620API LY_ERR
621lyd_parse_ext_op(const struct lysc_ext_instance *ext, struct lyd_node *parent, struct ly_in *in, LYD_FORMAT format,
622 enum lyd_type data_type, struct lyd_node **tree, struct lyd_node **op)
623{
624 const struct ly_ctx *ctx = ext ? ext->module->ctx : NULL;
625
626 LY_CHECK_ARG_RET(ctx, ext, in, data_type, parent || tree || op, LY_EINVAL);
627
628 return lyd_parse_op_(ctx, ext, parent, in, format, data_type, tree, op);
629}
630
Michal Vasko90932a92020-02-12 14:33:03 +0100631LY_ERR
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200632lyd_create_term(const struct lysc_node *schema, const char *value, size_t value_len, ly_bool *dynamic,
Radek Krejci8df109d2021-04-23 12:19:08 +0200633 LY_VALUE_FORMAT format, void *prefix_data, uint32_t hints, ly_bool *incomplete, struct lyd_node **node)
Michal Vasko90932a92020-02-12 14:33:03 +0100634{
635 LY_ERR ret;
636 struct lyd_node_term *term;
637
Michal Vasko9b368d32020-02-14 13:53:31 +0100638 assert(schema->nodetype & LYD_NODE_TERM);
639
Michal Vasko90932a92020-02-12 14:33:03 +0100640 term = calloc(1, sizeof *term);
641 LY_CHECK_ERR_RET(!term, LOGMEM(schema->module->ctx), LY_EMEM);
642
643 term->schema = schema;
Michal Vasko9e685082021-01-29 14:49:09 +0100644 term->prev = &term->node;
Michal Vasko9b368d32020-02-14 13:53:31 +0100645 term->flags = LYD_NEW;
Michal Vasko90932a92020-02-12 14:33:03 +0100646
Radek Krejciddace2c2021-01-08 11:30:56 +0100647 LOG_LOCSET(schema, NULL, NULL, NULL);
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200648 ret = lyd_value_store(schema->module->ctx, &term->value, ((struct lysc_node_leaf *)term->schema)->type, value,
Radek Krejci2efc45b2020-12-22 16:25:44 +0100649 value_len, dynamic, format, prefix_data, hints, schema, incomplete);
Radek Krejciddace2c2021-01-08 11:30:56 +0100650 LOG_LOCBACK(1, 0, 0, 0);
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200651 LY_CHECK_ERR_RET(ret, free(term), ret);
Michal Vasko9e685082021-01-29 14:49:09 +0100652 lyd_hash(&term->node);
Michal Vasko9b368d32020-02-14 13:53:31 +0100653
Michal Vasko9e685082021-01-29 14:49:09 +0100654 *node = &term->node;
Michal Vasko9b368d32020-02-14 13:53:31 +0100655 return ret;
656}
657
658LY_ERR
659lyd_create_term2(const struct lysc_node *schema, const struct lyd_value *val, struct lyd_node **node)
660{
661 LY_ERR ret;
662 struct lyd_node_term *term;
663 struct lysc_type *type;
664
665 assert(schema->nodetype & LYD_NODE_TERM);
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200666 assert(val && val->canonical && val->realtype);
Michal Vasko9b368d32020-02-14 13:53:31 +0100667
668 term = calloc(1, sizeof *term);
669 LY_CHECK_ERR_RET(!term, LOGMEM(schema->module->ctx), LY_EMEM);
670
671 term->schema = schema;
Michal Vasko9e685082021-01-29 14:49:09 +0100672 term->prev = &term->node;
Michal Vasko9b368d32020-02-14 13:53:31 +0100673 term->flags = LYD_NEW;
674
675 type = ((struct lysc_node_leaf *)schema)->type;
676 ret = type->plugin->duplicate(schema->module->ctx, val, &term->value);
677 if (ret) {
678 LOGERR(schema->module->ctx, ret, "Value duplication failed.");
679 free(term);
680 return ret;
681 }
Michal Vasko9e685082021-01-29 14:49:09 +0100682 lyd_hash(&term->node);
Michal Vasko90932a92020-02-12 14:33:03 +0100683
Michal Vasko9e685082021-01-29 14:49:09 +0100684 *node = &term->node;
Michal Vasko90932a92020-02-12 14:33:03 +0100685 return ret;
686}
687
688LY_ERR
689lyd_create_inner(const struct lysc_node *schema, struct lyd_node **node)
690{
691 struct lyd_node_inner *in;
692
Michal Vasko9b368d32020-02-14 13:53:31 +0100693 assert(schema->nodetype & LYD_NODE_INNER);
694
Michal Vasko90932a92020-02-12 14:33:03 +0100695 in = calloc(1, sizeof *in);
696 LY_CHECK_ERR_RET(!in, LOGMEM(schema->module->ctx), LY_EMEM);
697
698 in->schema = schema;
Michal Vasko9e685082021-01-29 14:49:09 +0100699 in->prev = &in->node;
Michal Vasko9b368d32020-02-14 13:53:31 +0100700 in->flags = LYD_NEW;
Michal Vasko3383be72020-11-03 17:15:31 +0100701 if ((schema->nodetype == LYS_CONTAINER) && !(schema->flags & LYS_PRESENCE)) {
702 in->flags |= LYD_DEFAULT;
703 }
Michal Vasko90932a92020-02-12 14:33:03 +0100704
Michal Vasko9b368d32020-02-14 13:53:31 +0100705 /* do not hash list with keys, we need them for the hash */
706 if ((schema->nodetype != LYS_LIST) || (schema->flags & LYS_KEYLESS)) {
Michal Vasko9e685082021-01-29 14:49:09 +0100707 lyd_hash(&in->node);
Michal Vasko9b368d32020-02-14 13:53:31 +0100708 }
Michal Vasko90932a92020-02-12 14:33:03 +0100709
Michal Vasko9e685082021-01-29 14:49:09 +0100710 *node = &in->node;
Michal Vasko90932a92020-02-12 14:33:03 +0100711 return LY_SUCCESS;
712}
713
Michal Vasko90932a92020-02-12 14:33:03 +0100714LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +0200715lyd_create_list(const struct lysc_node *schema, const struct ly_path_predicate *predicates, struct lyd_node **node)
Michal Vasko90932a92020-02-12 14:33:03 +0100716{
717 LY_ERR ret = LY_SUCCESS;
Michal Vasko90932a92020-02-12 14:33:03 +0100718 struct lyd_node *list = NULL, *key;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200719 LY_ARRAY_COUNT_TYPE u;
Michal Vasko90932a92020-02-12 14:33:03 +0100720
Michal Vasko004d3152020-06-11 19:59:22 +0200721 assert((schema->nodetype == LYS_LIST) && !(schema->flags & LYS_KEYLESS));
Michal Vasko90932a92020-02-12 14:33:03 +0100722
723 /* create list */
724 LY_CHECK_GOTO(ret = lyd_create_inner(schema, &list), cleanup);
725
Radek Krejciddace2c2021-01-08 11:30:56 +0100726 LOG_LOCSET(NULL, list, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100727
Michal Vasko90932a92020-02-12 14:33:03 +0100728 /* create and insert all the keys */
Michal Vasko004d3152020-06-11 19:59:22 +0200729 LY_ARRAY_FOR(predicates, u) {
730 LY_CHECK_GOTO(ret = lyd_create_term2(predicates[u].key, &predicates[u].value, &key), cleanup);
Michal Vasko90932a92020-02-12 14:33:03 +0100731 lyd_insert_node(list, NULL, key);
732 }
733
Michal Vasko9b368d32020-02-14 13:53:31 +0100734 /* hash having all the keys */
735 lyd_hash(list);
736
Michal Vasko90932a92020-02-12 14:33:03 +0100737 /* success */
738 *node = list;
739 list = NULL;
740
741cleanup:
Radek Krejciddace2c2021-01-08 11:30:56 +0100742 LOG_LOCBACK(0, 1, 0, 0);
Michal Vasko90932a92020-02-12 14:33:03 +0100743 lyd_free_tree(list);
Michal Vasko004d3152020-06-11 19:59:22 +0200744 return ret;
745}
746
747static LY_ERR
748lyd_create_list2(const struct lysc_node *schema, const char *keys, size_t keys_len, struct lyd_node **node)
749{
750 LY_ERR ret = LY_SUCCESS;
751 struct lyxp_expr *expr = NULL;
752 uint16_t exp_idx = 0;
753 enum ly_path_pred_type pred_type = 0;
754 struct ly_path_predicate *predicates = NULL;
755
Radek Krejciddace2c2021-01-08 11:30:56 +0100756 LOG_LOCSET(schema, NULL, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100757
Michal Vasko004d3152020-06-11 19:59:22 +0200758 /* parse keys */
Michal Vasko6b26e742020-07-17 15:02:10 +0200759 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 +0200760 LY_PATH_PRED_KEYS, &expr), cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +0200761
762 /* compile them */
Radek Krejci8df109d2021-04-23 12:19:08 +0200763 LY_CHECK_GOTO(ret = ly_path_compile_predicate(schema->module->ctx, NULL, NULL, schema, expr, &exp_idx, LY_VALUE_JSON,
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200764 NULL, &predicates, &pred_type), cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +0200765
766 /* create the list node */
767 LY_CHECK_GOTO(ret = lyd_create_list(schema, predicates, node), cleanup);
768
769cleanup:
Radek Krejciddace2c2021-01-08 11:30:56 +0100770 LOG_LOCBACK(1, 0, 0, 0);
Michal Vasko004d3152020-06-11 19:59:22 +0200771 lyxp_expr_free(schema->module->ctx, expr);
Michal Vaskof7e16e22020-10-21 09:24:39 +0200772 ly_path_predicates_free(schema->module->ctx, pred_type, predicates);
Michal Vasko90932a92020-02-12 14:33:03 +0100773 return ret;
774}
775
776LY_ERR
Michal Vasko366a4a12020-12-04 16:23:57 +0100777lyd_create_any(const struct lysc_node *schema, const void *value, LYD_ANYDATA_VALUETYPE value_type, ly_bool use_value,
778 struct lyd_node **node)
Michal Vasko90932a92020-02-12 14:33:03 +0100779{
Michal Vasko366a4a12020-12-04 16:23:57 +0100780 LY_ERR ret;
Michal Vasko90932a92020-02-12 14:33:03 +0100781 struct lyd_node_any *any;
Michal Vasko366a4a12020-12-04 16:23:57 +0100782 union lyd_any_value any_val;
Michal Vasko90932a92020-02-12 14:33:03 +0100783
Michal Vasko9b368d32020-02-14 13:53:31 +0100784 assert(schema->nodetype & LYD_NODE_ANY);
785
Michal Vasko90932a92020-02-12 14:33:03 +0100786 any = calloc(1, sizeof *any);
787 LY_CHECK_ERR_RET(!any, LOGMEM(schema->module->ctx), LY_EMEM);
788
789 any->schema = schema;
Michal Vasko9e685082021-01-29 14:49:09 +0100790 any->prev = &any->node;
Michal Vasko9b368d32020-02-14 13:53:31 +0100791 any->flags = LYD_NEW;
Michal Vasko90932a92020-02-12 14:33:03 +0100792
Radek Krejci1798aae2020-07-14 13:26:06 +0200793 /* TODO: convert XML/JSON strings into a opaq data tree */
Michal Vasko366a4a12020-12-04 16:23:57 +0100794
795 if (use_value) {
796 any->value.str = value;
797 any->value_type = value_type;
798 } else {
799 any_val.str = value;
Michal Vasko9e685082021-01-29 14:49:09 +0100800 ret = lyd_any_copy_value(&any->node, &any_val, value_type);
Michal Vasko366a4a12020-12-04 16:23:57 +0100801 LY_CHECK_ERR_RET(ret, free(any), ret);
802 }
Michal Vasko9e685082021-01-29 14:49:09 +0100803 lyd_hash(&any->node);
Michal Vasko90932a92020-02-12 14:33:03 +0100804
Michal Vasko9e685082021-01-29 14:49:09 +0100805 *node = &any->node;
Michal Vasko90932a92020-02-12 14:33:03 +0100806 return LY_SUCCESS;
807}
808
Michal Vasko52927e22020-03-16 17:26:14 +0100809LY_ERR
Michal Vasko501af032020-11-11 20:27:44 +0100810lyd_create_opaq(const struct ly_ctx *ctx, const char *name, size_t name_len, const char *prefix, size_t pref_len,
811 const char *module_key, size_t module_key_len, const char *value, size_t value_len, ly_bool *dynamic,
Radek Krejci8df109d2021-04-23 12:19:08 +0200812 LY_VALUE_FORMAT format, void *val_prefix_data, uint32_t hints, struct lyd_node **node)
Michal Vasko52927e22020-03-16 17:26:14 +0100813{
Radek Krejci011e4aa2020-09-04 15:22:31 +0200814 LY_ERR ret = LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +0100815 struct lyd_node_opaq *opaq;
816
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200817 assert(ctx && name && name_len && format);
Michal Vasko52927e22020-03-16 17:26:14 +0100818
819 if (!value_len) {
820 value = "";
821 }
822
823 opaq = calloc(1, sizeof *opaq);
Michal Vasko501af032020-11-11 20:27:44 +0100824 LY_CHECK_ERR_GOTO(!opaq, LOGMEM(ctx); ret = LY_EMEM, finish);
Michal Vasko52927e22020-03-16 17:26:14 +0100825
Michal Vasko9e685082021-01-29 14:49:09 +0100826 opaq->prev = &opaq->node;
Michal Vaskoad92b672020-11-12 13:11:31 +0100827 LY_CHECK_GOTO(ret = lydict_insert(ctx, name, name_len, &opaq->name.name), finish);
Radek Krejci011e4aa2020-09-04 15:22:31 +0200828
Michal Vasko52927e22020-03-16 17:26:14 +0100829 if (pref_len) {
Michal Vaskoad92b672020-11-12 13:11:31 +0100830 LY_CHECK_GOTO(ret = lydict_insert(ctx, prefix, pref_len, &opaq->name.prefix), finish);
Michal Vasko52927e22020-03-16 17:26:14 +0100831 }
Radek Krejci1798aae2020-07-14 13:26:06 +0200832 if (module_key_len) {
Michal Vaskoad92b672020-11-12 13:11:31 +0100833 LY_CHECK_GOTO(ret = lydict_insert(ctx, module_key, module_key_len, &opaq->name.module_ns), finish);
Radek Krejci1798aae2020-07-14 13:26:06 +0200834 }
Michal Vasko52927e22020-03-16 17:26:14 +0100835 if (dynamic && *dynamic) {
Radek Krejci011e4aa2020-09-04 15:22:31 +0200836 LY_CHECK_GOTO(ret = lydict_insert_zc(ctx, (char *)value, &opaq->value), finish);
Michal Vasko52927e22020-03-16 17:26:14 +0100837 *dynamic = 0;
838 } else {
Radek Krejci011e4aa2020-09-04 15:22:31 +0200839 LY_CHECK_GOTO(ret = lydict_insert(ctx, value, value_len, &opaq->value), finish);
Michal Vasko52927e22020-03-16 17:26:14 +0100840 }
Michal Vasko501af032020-11-11 20:27:44 +0100841
842 opaq->format = format;
843 opaq->val_prefix_data = val_prefix_data;
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200844 opaq->hints = hints;
Michal Vasko52927e22020-03-16 17:26:14 +0100845 opaq->ctx = ctx;
846
Radek Krejci011e4aa2020-09-04 15:22:31 +0200847finish:
848 if (ret) {
Michal Vasko9e685082021-01-29 14:49:09 +0100849 lyd_free_tree(&opaq->node);
Michal Vasko501af032020-11-11 20:27:44 +0100850 ly_free_prefix_data(format, val_prefix_data);
Radek Krejci011e4aa2020-09-04 15:22:31 +0200851 } else {
Michal Vasko9e685082021-01-29 14:49:09 +0100852 *node = &opaq->node;
Radek Krejci011e4aa2020-09-04 15:22:31 +0200853 }
854 return ret;
Michal Vasko52927e22020-03-16 17:26:14 +0100855}
856
Michal Vasko3a41dff2020-07-15 14:30:28 +0200857API LY_ERR
Michal Vasko65243892020-12-04 16:26:21 +0100858lyd_new_inner(struct lyd_node *parent, const struct lys_module *module, const char *name, ly_bool output,
859 struct lyd_node **node)
Michal Vasko013a8182020-03-03 10:46:53 +0100860{
861 struct lyd_node *ret = NULL;
862 const struct lysc_node *schema;
863 struct ly_ctx *ctx = parent ? parent->schema->module->ctx : (module ? module->ctx : NULL);
864
Michal Vasko6027eb92020-07-15 16:37:30 +0200865 LY_CHECK_ARG_RET(ctx, parent || module, parent || node, name, LY_EINVAL);
Michal Vasko013a8182020-03-03 10:46:53 +0100866
Michal Vaskof03ed032020-03-04 13:31:44 +0100867 if (!module) {
868 module = parent->schema->module;
869 }
870
Michal Vasko3a41dff2020-07-15 14:30:28 +0200871 schema = lys_find_child(parent ? parent->schema : NULL, module, name, 0,
Radek Krejci41ac9942020-11-02 14:47:56 +0100872 LYS_CONTAINER | LYS_NOTIF | LYS_RPC | LYS_ACTION, output ? LYS_GETNEXT_OUTPUT : 0);
Radek Krejcidd2a7662021-03-12 11:26:34 +0100873 LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "Inner node (not a list) \"%s\" not found.", name), LY_ENOTFOUND);
Michal Vasko013a8182020-03-03 10:46:53 +0100874
Michal Vasko3a41dff2020-07-15 14:30:28 +0200875 LY_CHECK_RET(lyd_create_inner(schema, &ret));
876 if (parent) {
Michal Vasko013a8182020-03-03 10:46:53 +0100877 lyd_insert_node(parent, NULL, ret);
878 }
Michal Vasko3a41dff2020-07-15 14:30:28 +0200879
880 if (node) {
881 *node = ret;
882 }
883 return LY_SUCCESS;
Michal Vasko013a8182020-03-03 10:46:53 +0100884}
885
Michal Vasko3a41dff2020-07-15 14:30:28 +0200886API LY_ERR
Radek Krejcidd2a7662021-03-12 11:26:34 +0100887lyd_new_ext_inner(const struct lysc_ext_instance *ext, const char *name, struct lyd_node **node)
888{
889 struct lyd_node *ret = NULL;
890 const struct lysc_node *schema;
891 struct ly_ctx *ctx = ext ? ext->module->ctx : NULL;
892
893 LY_CHECK_ARG_RET(ctx, ext, node, name, LY_EINVAL);
894
895 schema = lysc_ext_find_node(ext, NULL, name, 0, LYS_CONTAINER | LYS_NOTIF | LYS_RPC | LYS_ACTION, 0);
896 if (!schema) {
897 if (ext->argument) {
898 LOGERR(ctx, LY_EINVAL, "Inner node (not a list) \"%s\" not found in instance \"%s\" of extension %s.",
899 name, ext->argument, ext->def->name);
900 } else {
901 LOGERR(ctx, LY_EINVAL, "Inner node (not a list) \"%s\" not found in instance of extension %s.",
902 name, ext->def->name);
903 }
904 return LY_ENOTFOUND;
905 }
906 LY_CHECK_RET(lyd_create_inner(schema, &ret));
907
908 *node = ret;
909
910 return LY_SUCCESS;
911}
912
913API LY_ERR
Michal Vasko65243892020-12-04 16:26:21 +0100914lyd_new_list(struct lyd_node *parent, const struct lys_module *module, const char *name, ly_bool output,
915 struct lyd_node **node, ...)
Michal Vasko013a8182020-03-03 10:46:53 +0100916{
917 struct lyd_node *ret = NULL, *key;
918 const struct lysc_node *schema, *key_s;
919 struct ly_ctx *ctx = parent ? parent->schema->module->ctx : (module ? module->ctx : NULL);
920 va_list ap;
921 const char *key_val;
922 LY_ERR rc = LY_SUCCESS;
923
Michal Vasko6027eb92020-07-15 16:37:30 +0200924 LY_CHECK_ARG_RET(ctx, parent || module, parent || node, name, LY_EINVAL);
Michal Vasko013a8182020-03-03 10:46:53 +0100925
Michal Vaskof03ed032020-03-04 13:31:44 +0100926 if (!module) {
927 module = parent->schema->module;
928 }
929
Radek Krejci41ac9942020-11-02 14:47:56 +0100930 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 +0200931 LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "List node \"%s\" not found.", name), LY_ENOTFOUND);
Michal Vasko013a8182020-03-03 10:46:53 +0100932
933 /* create list inner node */
Michal Vasko3a41dff2020-07-15 14:30:28 +0200934 LY_CHECK_RET(lyd_create_inner(schema, &ret));
Michal Vasko013a8182020-03-03 10:46:53 +0100935
Michal Vasko3a41dff2020-07-15 14:30:28 +0200936 va_start(ap, node);
Michal Vasko013a8182020-03-03 10:46:53 +0100937
938 /* create and insert all the keys */
Michal Vasko544e58a2021-01-28 14:33:41 +0100939 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 +0100940 key_val = va_arg(ap, const char *);
941
Radek Krejci8df109d2021-04-23 12:19:08 +0200942 rc = lyd_create_term(key_s, key_val, key_val ? strlen(key_val) : 0, NULL, LY_VALUE_JSON, NULL, LYD_HINT_DATA,
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200943 NULL, &key);
944 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko013a8182020-03-03 10:46:53 +0100945 lyd_insert_node(ret, NULL, key);
946 }
947
Michal Vasko013a8182020-03-03 10:46:53 +0100948 if (parent) {
949 lyd_insert_node(parent, NULL, ret);
950 }
951
952cleanup:
Michal Vasko3a41dff2020-07-15 14:30:28 +0200953 va_end(ap);
Michal Vasko013a8182020-03-03 10:46:53 +0100954 if (rc) {
955 lyd_free_tree(ret);
956 ret = NULL;
Michal Vasko3a41dff2020-07-15 14:30:28 +0200957 } else if (node) {
958 *node = ret;
Michal Vasko013a8182020-03-03 10:46:53 +0100959 }
Michal Vasko3a41dff2020-07-15 14:30:28 +0200960 return rc;
Michal Vasko013a8182020-03-03 10:46:53 +0100961}
962
Michal Vasko3a41dff2020-07-15 14:30:28 +0200963API LY_ERR
Radek Krejci8247bae2021-03-12 11:47:02 +0100964lyd_new_ext_list(const struct lysc_ext_instance *ext, const char *name, struct lyd_node **node, ...)
965{
966 struct lyd_node *ret = NULL, *key;
967 const struct lysc_node *schema, *key_s;
968 struct ly_ctx *ctx = ext ? ext->module->ctx : NULL;
969 va_list ap;
970 const char *key_val;
971 LY_ERR rc = LY_SUCCESS;
972
973 LY_CHECK_ARG_RET(ctx, ext, node, name, LY_EINVAL);
974
975 schema = lysc_ext_find_node(ext, NULL, name, 0, LYS_LIST, 0);
976 if (!schema) {
977 if (ext->argument) {
978 LOGERR(ctx, LY_EINVAL, "List node \"%s\" not found in instance \"%s\" of extension %s.",
979 name, ext->argument, ext->def->name);
980 } else {
981 LOGERR(ctx, LY_EINVAL, "List node \"%s\" not found in instance of extension %s.", name, ext->def->name);
982 }
983 return LY_ENOTFOUND;
984 }
985 /* create list inner node */
986 LY_CHECK_RET(lyd_create_inner(schema, &ret));
987
988 va_start(ap, node);
989
990 /* create and insert all the keys */
991 for (key_s = lysc_node_child(schema); key_s && (key_s->flags & LYS_KEY); key_s = key_s->next) {
992 key_val = va_arg(ap, const char *);
993
Radek Krejci8df109d2021-04-23 12:19:08 +0200994 rc = lyd_create_term(key_s, key_val, key_val ? strlen(key_val) : 0, NULL, LY_VALUE_JSON, NULL, LYD_HINT_DATA,
Radek Krejci8247bae2021-03-12 11:47:02 +0100995 NULL, &key);
996 LY_CHECK_GOTO(rc, cleanup);
997 lyd_insert_node(ret, NULL, key);
998 }
999
1000cleanup:
1001 va_end(ap);
1002 if (rc) {
1003 lyd_free_tree(ret);
1004 ret = NULL;
Radek Krejci8247bae2021-03-12 11:47:02 +01001005 }
Radek Krejci8919fbd2021-03-15 09:35:25 +01001006 *node = ret;
Radek Krejci8247bae2021-03-12 11:47:02 +01001007 return rc;
1008}
1009
1010API LY_ERR
Michal Vasko3a41dff2020-07-15 14:30:28 +02001011lyd_new_list2(struct lyd_node *parent, const struct lys_module *module, const char *name, const char *keys,
Radek Krejci41ac9942020-11-02 14:47:56 +01001012 ly_bool output, struct lyd_node **node)
Michal Vasko013a8182020-03-03 10:46:53 +01001013{
1014 struct lyd_node *ret = NULL;
1015 const struct lysc_node *schema;
1016 struct ly_ctx *ctx = parent ? parent->schema->module->ctx : (module ? module->ctx : NULL);
1017
Michal Vasko6027eb92020-07-15 16:37:30 +02001018 LY_CHECK_ARG_RET(ctx, parent || module, parent || node, name, LY_EINVAL);
Michal Vasko013a8182020-03-03 10:46:53 +01001019
Michal Vaskof03ed032020-03-04 13:31:44 +01001020 if (!module) {
1021 module = parent->schema->module;
1022 }
Michal Vasko004d3152020-06-11 19:59:22 +02001023 if (!keys) {
1024 keys = "";
1025 }
Michal Vaskof03ed032020-03-04 13:31:44 +01001026
Michal Vasko004d3152020-06-11 19:59:22 +02001027 /* find schema node */
Radek Krejci41ac9942020-11-02 14:47:56 +01001028 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 +02001029 LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "List node \"%s\" not found.", name), LY_ENOTFOUND);
Michal Vasko013a8182020-03-03 10:46:53 +01001030
Michal Vasko004d3152020-06-11 19:59:22 +02001031 if ((schema->flags & LYS_KEYLESS) && !keys[0]) {
1032 /* key-less list */
Michal Vasko3a41dff2020-07-15 14:30:28 +02001033 LY_CHECK_RET(lyd_create_inner(schema, &ret));
Michal Vasko004d3152020-06-11 19:59:22 +02001034 } else {
1035 /* create the list node */
Michal Vasko3a41dff2020-07-15 14:30:28 +02001036 LY_CHECK_RET(lyd_create_list2(schema, keys, strlen(keys), &ret));
Michal Vasko004d3152020-06-11 19:59:22 +02001037 }
Michal Vasko004d3152020-06-11 19:59:22 +02001038 if (parent) {
Michal Vasko013a8182020-03-03 10:46:53 +01001039 lyd_insert_node(parent, NULL, ret);
1040 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02001041
1042 if (node) {
1043 *node = ret;
1044 }
1045 return LY_SUCCESS;
Michal Vasko013a8182020-03-03 10:46:53 +01001046}
1047
Michal Vasko3a41dff2020-07-15 14:30:28 +02001048API LY_ERR
1049lyd_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 +01001050 ly_bool output, struct lyd_node **node)
Michal Vasko013a8182020-03-03 10:46:53 +01001051{
Michal Vaskocbff3e92020-05-27 12:56:41 +02001052 LY_ERR rc;
Michal Vasko013a8182020-03-03 10:46:53 +01001053 struct lyd_node *ret = NULL;
1054 const struct lysc_node *schema;
1055 struct ly_ctx *ctx = parent ? parent->schema->module->ctx : (module ? module->ctx : NULL);
1056
Michal Vasko6027eb92020-07-15 16:37:30 +02001057 LY_CHECK_ARG_RET(ctx, parent || module, parent || node, name, LY_EINVAL);
Michal Vasko013a8182020-03-03 10:46:53 +01001058
Michal Vaskof03ed032020-03-04 13:31:44 +01001059 if (!module) {
1060 module = parent->schema->module;
1061 }
1062
Radek Krejci41ac9942020-11-02 14:47:56 +01001063 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 +02001064 LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "Term node \"%s\" not found.", name), LY_ENOTFOUND);
Michal Vasko013a8182020-03-03 10:46:53 +01001065
Radek Krejci8df109d2021-04-23 12:19:08 +02001066 rc = lyd_create_term(schema, val_str, val_str ? strlen(val_str) : 0, NULL, LY_VALUE_JSON, NULL, LYD_HINT_DATA, NULL, &ret);
Michal Vaskofeca4fb2020-10-05 08:58:40 +02001067 LY_CHECK_RET(rc);
Michal Vaskocbff3e92020-05-27 12:56:41 +02001068 if (parent) {
Michal Vasko013a8182020-03-03 10:46:53 +01001069 lyd_insert_node(parent, NULL, ret);
1070 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02001071
1072 if (node) {
1073 *node = ret;
1074 }
1075 return LY_SUCCESS;
Michal Vasko013a8182020-03-03 10:46:53 +01001076}
1077
Michal Vasko3a41dff2020-07-15 14:30:28 +02001078API LY_ERR
Radek Krejci8a5afc22021-03-12 11:10:47 +01001079lyd_new_ext_term(const struct lysc_ext_instance *ext, const char *name, const char *val_str, struct lyd_node **node)
1080{
1081 LY_ERR rc;
1082 struct lyd_node *ret = NULL;
1083 const struct lysc_node *schema;
1084 struct ly_ctx *ctx = ext ? ext->module->ctx : NULL;
1085
1086 LY_CHECK_ARG_RET(ctx, ext, node, name, LY_EINVAL);
1087
1088 schema = lysc_ext_find_node(ext, NULL, name, 0, LYD_NODE_TERM, 0);
1089 if (!schema) {
1090 if (ext->argument) {
1091 LOGERR(ctx, LY_EINVAL, "Term node \"%s\" not found in instance \"%s\" of extension %s.",
1092 name, ext->argument, ext->def->name);
1093 } else {
1094 LOGERR(ctx, LY_EINVAL, "Term node \"%s\" not found in instance of extension %s.", name, ext->def->name);
1095 }
1096 return LY_ENOTFOUND;
1097 }
Radek Krejci8df109d2021-04-23 12:19:08 +02001098 rc = lyd_create_term(schema, val_str, val_str ? strlen(val_str) : 0, NULL, LY_VALUE_JSON, NULL, LYD_HINT_DATA, NULL, &ret);
Radek Krejci8a5afc22021-03-12 11:10:47 +01001099 LY_CHECK_RET(rc);
1100
1101 *node = ret;
1102
1103 return LY_SUCCESS;
1104}
1105
1106API LY_ERR
Michal Vasko2a4ab2b2021-03-04 15:52:40 +01001107lyd_new_any(struct lyd_node *parent, const struct lys_module *module, const char *name, const void *value,
1108 ly_bool use_value, LYD_ANYDATA_VALUETYPE value_type, ly_bool output, struct lyd_node **node)
Michal Vasko013a8182020-03-03 10:46:53 +01001109{
1110 struct lyd_node *ret = NULL;
1111 const struct lysc_node *schema;
1112 struct ly_ctx *ctx = parent ? parent->schema->module->ctx : (module ? module->ctx : NULL);
1113
Michal Vasko6027eb92020-07-15 16:37:30 +02001114 LY_CHECK_ARG_RET(ctx, parent || module, parent || node, name, LY_EINVAL);
Michal Vasko013a8182020-03-03 10:46:53 +01001115
Michal Vaskof03ed032020-03-04 13:31:44 +01001116 if (!module) {
1117 module = parent->schema->module;
1118 }
1119
Radek Krejci41ac9942020-11-02 14:47:56 +01001120 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 +02001121 LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "Any node \"%s\" not found.", name), LY_ENOTFOUND);
Michal Vasko013a8182020-03-03 10:46:53 +01001122
Michal Vasko2a4ab2b2021-03-04 15:52:40 +01001123 LY_CHECK_RET(lyd_create_any(schema, value, value_type, use_value, &ret));
Michal Vasko3a41dff2020-07-15 14:30:28 +02001124 if (parent) {
Michal Vasko013a8182020-03-03 10:46:53 +01001125 lyd_insert_node(parent, NULL, ret);
1126 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02001127
1128 if (node) {
1129 *node = ret;
1130 }
1131 return LY_SUCCESS;
Michal Vasko013a8182020-03-03 10:46:53 +01001132}
1133
Radek Krejci0b963da2021-03-12 13:23:44 +01001134API LY_ERR
1135lyd_new_ext_any(const struct lysc_ext_instance *ext, const char *name, const void *value, ly_bool use_value,
1136 LYD_ANYDATA_VALUETYPE value_type, struct lyd_node **node)
1137{
1138 struct lyd_node *ret = NULL;
1139 const struct lysc_node *schema;
1140 struct ly_ctx *ctx = ext ? ext->module->ctx : NULL;
1141
1142 LY_CHECK_ARG_RET(ctx, ext, node, name, LY_EINVAL);
1143
1144 schema = lysc_ext_find_node(ext, NULL, name, 0, LYD_NODE_ANY, 0);
1145 if (!schema) {
1146 if (ext->argument) {
1147 LOGERR(ctx, LY_EINVAL, "Any node \"%s\" not found in instance \"%s\" of extension %s.",
1148 name, ext->argument, ext->def->name);
1149 } else {
1150 LOGERR(ctx, LY_EINVAL, "Any node \"%s\" not found in instance of extension %s.", name, ext->def->name);
1151 }
1152 return LY_ENOTFOUND;
1153 }
1154 LY_CHECK_RET(lyd_create_any(schema, value, value_type, use_value, &ret));
1155
1156 *node = ret;
1157
1158 return LY_SUCCESS;
1159}
1160
Michal Vasko4490d312020-06-16 13:08:55 +02001161/**
1162 * @brief Update node value.
1163 *
1164 * @param[in] node Node to update.
1165 * @param[in] value New value to set.
1166 * @param[in] value_type Type of @p value for any node.
1167 * @param[out] new_parent Set to @p node if the value was updated, otherwise set to NULL.
1168 * @param[out] new_node Set to @p node if the value was updated, otherwise set to NULL.
1169 * @return LY_ERR value.
1170 */
Michal Vasko00cbf532020-06-15 13:58:47 +02001171static LY_ERR
1172lyd_new_path_update(struct lyd_node *node, const void *value, LYD_ANYDATA_VALUETYPE value_type,
Radek Krejci0f969882020-08-21 16:56:47 +02001173 struct lyd_node **new_parent, struct lyd_node **new_node)
Michal Vasko00cbf532020-06-15 13:58:47 +02001174{
1175 LY_ERR ret = LY_SUCCESS;
1176 struct lyd_node *new_any;
1177
1178 switch (node->schema->nodetype) {
1179 case LYS_CONTAINER:
1180 case LYS_NOTIF:
1181 case LYS_RPC:
1182 case LYS_ACTION:
1183 case LYS_LIST:
Michal Vasko00cbf532020-06-15 13:58:47 +02001184 /* if it exists, there is nothing to update */
1185 *new_parent = NULL;
1186 *new_node = NULL;
1187 break;
Michal Vaskoe78faec2021-04-08 17:24:43 +02001188 case LYS_LEAFLIST:
1189 if (!lysc_is_dup_inst_list(node->schema)) {
1190 /* if it exists, there is nothing to update */
1191 *new_parent = NULL;
1192 *new_node = NULL;
1193 break;
1194 }
1195 /* fallthrough */
Michal Vasko00cbf532020-06-15 13:58:47 +02001196 case LYS_LEAF:
1197 ret = lyd_change_term(node, value);
1198 if ((ret == LY_SUCCESS) || (ret == LY_EEXIST)) {
1199 /* there was an actual change (at least of the default flag) */
1200 *new_parent = node;
1201 *new_node = node;
1202 ret = LY_SUCCESS;
1203 } else if (ret == LY_ENOT) {
1204 /* no change */
1205 *new_parent = NULL;
1206 *new_node = NULL;
1207 ret = LY_SUCCESS;
1208 } /* else error */
1209 break;
1210 case LYS_ANYDATA:
1211 case LYS_ANYXML:
1212 /* create a new any node */
Michal Vasko366a4a12020-12-04 16:23:57 +01001213 LY_CHECK_RET(lyd_create_any(node->schema, value, value_type, 0, &new_any));
Michal Vasko00cbf532020-06-15 13:58:47 +02001214
1215 /* compare with the existing one */
Michal Vasko8f359bf2020-07-28 10:41:15 +02001216 if (lyd_compare_single(node, new_any, 0)) {
Michal Vasko00cbf532020-06-15 13:58:47 +02001217 /* not equal, switch values (so that we can use generic node free) */
1218 ((struct lyd_node_any *)new_any)->value = ((struct lyd_node_any *)node)->value;
1219 ((struct lyd_node_any *)new_any)->value_type = ((struct lyd_node_any *)node)->value_type;
1220 ((struct lyd_node_any *)node)->value.str = value;
1221 ((struct lyd_node_any *)node)->value_type = value_type;
1222
1223 *new_parent = node;
1224 *new_node = node;
1225 } else {
1226 /* they are equal */
1227 *new_parent = NULL;
1228 *new_node = NULL;
1229 }
1230 lyd_free_tree(new_any);
1231 break;
1232 default:
Michal Vaskob7be7a82020-08-20 09:09:04 +02001233 LOGINT(LYD_CTX(node));
Michal Vasko00cbf532020-06-15 13:58:47 +02001234 ret = LY_EINT;
1235 break;
1236 }
1237
1238 return ret;
1239}
1240
Michal Vasko3a41dff2020-07-15 14:30:28 +02001241API LY_ERR
Michal Vasko871a0252020-11-11 18:35:24 +01001242lyd_new_meta(const struct ly_ctx *ctx, struct lyd_node *parent, const struct lys_module *module, const char *name,
1243 const char *val_str, ly_bool clear_dflt, struct lyd_meta **meta)
Michal Vaskod86997b2020-05-26 15:19:54 +02001244{
Michal Vaskod86997b2020-05-26 15:19:54 +02001245 const char *prefix, *tmp;
Michal Vaskod86997b2020-05-26 15:19:54 +02001246 size_t pref_len, name_len;
1247
Michal Vasko2b5344c2021-02-26 10:12:05 +01001248 LY_CHECK_ARG_RET(ctx, ctx || parent, name, module || strchr(name, ':'), parent || meta, LY_EINVAL);
1249 if (!ctx) {
1250 ctx = LYD_CTX(parent);
1251 }
Michal Vasko00cbf532020-06-15 13:58:47 +02001252
Michal Vasko871a0252020-11-11 18:35:24 +01001253 if (parent && !parent->schema) {
Michal Vasko33b4fab2021-03-04 15:56:12 +01001254 LOGERR(ctx, LY_EINVAL, "Cannot add metadata \"%s\" to an opaque node \"%s\".", name, LYD_NAME(parent));
Michal Vasko871a0252020-11-11 18:35:24 +01001255 return LY_EINVAL;
1256 }
Michal Vasko610553d2020-11-18 18:15:05 +01001257 if (meta) {
1258 *meta = NULL;
1259 }
Michal Vaskod86997b2020-05-26 15:19:54 +02001260
1261 /* parse the name */
1262 tmp = name;
1263 if (ly_parse_nodeid(&tmp, &prefix, &pref_len, &name, &name_len) || tmp[0]) {
1264 LOGERR(ctx, LY_EINVAL, "Metadata name \"%s\" is not valid.", name);
Michal Vasko871a0252020-11-11 18:35:24 +01001265 return LY_EINVAL;
Michal Vaskod86997b2020-05-26 15:19:54 +02001266 }
1267
1268 /* find the module */
1269 if (prefix) {
Radek Krejci0ad51f12020-07-16 12:08:12 +02001270 module = ly_ctx_get_module_implemented2(ctx, prefix, pref_len);
Radek Krejci422afb12021-03-04 16:38:16 +01001271 LY_CHECK_ERR_RET(!module, LOGERR(ctx, LY_EINVAL, "Module \"%.*s\" not found.", (int)pref_len, prefix), LY_ENOTFOUND);
Michal Vaskod86997b2020-05-26 15:19:54 +02001272 }
1273
1274 /* set value if none */
1275 if (!val_str) {
1276 val_str = "";
1277 }
1278
Radek Krejci8df109d2021-04-23 12:19:08 +02001279 return lyd_create_meta(parent, meta, module, name, name_len, val_str, strlen(val_str), NULL, LY_VALUE_JSON,
Michal Vasko871a0252020-11-11 18:35:24 +01001280 NULL, LYD_HINT_DATA, clear_dflt, NULL);
1281}
Michal Vasko3a41dff2020-07-15 14:30:28 +02001282
Michal Vaskoba696702020-11-11 19:12:45 +01001283API LY_ERR
1284lyd_new_meta2(const struct ly_ctx *ctx, struct lyd_node *parent, ly_bool clear_dflt, const struct lyd_attr *attr,
1285 struct lyd_meta **meta)
1286{
1287 const struct lys_module *mod;
1288
1289 LY_CHECK_ARG_RET(NULL, ctx, attr, parent || meta, LY_EINVAL);
1290
1291 if (parent && !parent->schema) {
1292 LOGERR(ctx, LY_EINVAL, "Cannot add metadata to an opaque node \"%s\".", ((struct lyd_node_opaq *)parent)->name);
1293 return LY_EINVAL;
Michal Vasko3a41dff2020-07-15 14:30:28 +02001294 }
Michal Vasko610553d2020-11-18 18:15:05 +01001295 if (meta) {
1296 *meta = NULL;
1297 }
Michal Vaskoba696702020-11-11 19:12:45 +01001298
1299 switch (attr->format) {
Radek Krejci8df109d2021-04-23 12:19:08 +02001300 case LY_VALUE_XML:
Michal Vaskoad92b672020-11-12 13:11:31 +01001301 mod = ly_ctx_get_module_implemented_ns(ctx, attr->name.module_ns);
Michal Vaskoba696702020-11-11 19:12:45 +01001302 if (!mod) {
Michal Vaskoad92b672020-11-12 13:11:31 +01001303 LOGERR(ctx, LY_EINVAL, "Module with namespace \"%s\" not found.", attr->name.module_ns);
Michal Vaskoba696702020-11-11 19:12:45 +01001304 return LY_ENOTFOUND;
1305 }
1306 break;
Radek Krejci8df109d2021-04-23 12:19:08 +02001307 case LY_VALUE_JSON:
Michal Vaskoad92b672020-11-12 13:11:31 +01001308 mod = ly_ctx_get_module_implemented(ctx, attr->name.module_name);
Michal Vaskoba696702020-11-11 19:12:45 +01001309 if (!mod) {
Michal Vaskoad92b672020-11-12 13:11:31 +01001310 LOGERR(ctx, LY_EINVAL, "Module \"%s\" not found.", attr->name.module_name);
Michal Vaskoba696702020-11-11 19:12:45 +01001311 return LY_ENOTFOUND;
1312 }
1313 break;
1314 default:
1315 LOGINT_RET(ctx);
1316 }
1317
Michal Vaskoad92b672020-11-12 13:11:31 +01001318 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 +01001319 NULL, attr->format, attr->val_prefix_data, attr->hints, clear_dflt, NULL);
Michal Vaskod86997b2020-05-26 15:19:54 +02001320}
1321
Michal Vasko3a41dff2020-07-15 14:30:28 +02001322API LY_ERR
Michal Vasko00cbf532020-06-15 13:58:47 +02001323lyd_new_opaq(struct lyd_node *parent, const struct ly_ctx *ctx, const char *name, const char *value,
Michal Vasko0ab974d2021-02-24 13:18:26 +01001324 const char *prefix, const char *module_name, struct lyd_node **node)
Michal Vasko00cbf532020-06-15 13:58:47 +02001325{
1326 struct lyd_node *ret = NULL;
Radek Krejci07a55962021-03-02 20:16:43 +01001327
Michal Vasko0ab974d2021-02-24 13:18:26 +01001328 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 +02001329
1330 if (!ctx) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02001331 ctx = LYD_CTX(parent);
Michal Vasko00cbf532020-06-15 13:58:47 +02001332 }
1333 if (!value) {
1334 value = "";
1335 }
1336
Michal Vasko0ab974d2021-02-24 13:18:26 +01001337 LY_CHECK_RET(lyd_create_opaq(ctx, name, strlen(name), prefix, prefix ? strlen(prefix) : 0, module_name,
Radek Krejci8df109d2021-04-23 12:19:08 +02001338 strlen(module_name), value, strlen(value), NULL, LY_VALUE_JSON, NULL, 0, &ret));
Michal Vasko3a41dff2020-07-15 14:30:28 +02001339 if (parent) {
Michal Vasko00cbf532020-06-15 13:58:47 +02001340 lyd_insert_node(parent, NULL, ret);
1341 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02001342
1343 if (node) {
1344 *node = ret;
1345 }
1346 return LY_SUCCESS;
Michal Vasko00cbf532020-06-15 13:58:47 +02001347}
1348
Michal Vasko3a41dff2020-07-15 14:30:28 +02001349API LY_ERR
Michal Vasko8d65f852021-02-17 11:28:13 +01001350lyd_new_opaq2(struct lyd_node *parent, const struct ly_ctx *ctx, const char *name, const char *value,
Michal Vasko0ab974d2021-02-24 13:18:26 +01001351 const char *prefix, const char *module_ns, struct lyd_node **node)
Michal Vasko8d65f852021-02-17 11:28:13 +01001352{
1353 struct lyd_node *ret = NULL;
1354
1355 LY_CHECK_ARG_RET(ctx, parent || ctx, parent || node, name, module_ns, LY_EINVAL);
1356
1357 if (!ctx) {
1358 ctx = LYD_CTX(parent);
1359 }
1360 if (!value) {
1361 value = "";
1362 }
1363
Michal Vasko0ab974d2021-02-24 13:18:26 +01001364 LY_CHECK_RET(lyd_create_opaq(ctx, name, strlen(name), prefix, prefix ? strlen(prefix) : 0, module_ns,
Radek Krejci8df109d2021-04-23 12:19:08 +02001365 strlen(module_ns), value, strlen(value), NULL, LY_VALUE_XML, NULL, 0, &ret));
Michal Vasko8d65f852021-02-17 11:28:13 +01001366 if (parent) {
1367 lyd_insert_node(parent, NULL, ret);
1368 }
1369
1370 if (node) {
1371 *node = ret;
1372 }
1373 return LY_SUCCESS;
1374}
1375
1376API LY_ERR
1377lyd_new_attr(struct lyd_node *parent, const char *module_name, const char *name, const char *value,
Radek Krejci0f969882020-08-21 16:56:47 +02001378 struct lyd_attr **attr)
Michal Vasko00cbf532020-06-15 13:58:47 +02001379{
Radek Krejci1798aae2020-07-14 13:26:06 +02001380 struct lyd_attr *ret = NULL;
Michal Vasko00cbf532020-06-15 13:58:47 +02001381 const struct ly_ctx *ctx;
1382 const char *prefix, *tmp;
Michal Vasko51d21b82020-11-13 18:03:54 +01001383 size_t pref_len, name_len, mod_len;
Michal Vasko00cbf532020-06-15 13:58:47 +02001384
Michal Vasko3a41dff2020-07-15 14:30:28 +02001385 LY_CHECK_ARG_RET(NULL, parent, !parent->schema, name, LY_EINVAL);
Michal Vasko00cbf532020-06-15 13:58:47 +02001386
Michal Vaskob7be7a82020-08-20 09:09:04 +02001387 ctx = LYD_CTX(parent);
Michal Vasko00cbf532020-06-15 13:58:47 +02001388
1389 /* parse the name */
1390 tmp = name;
1391 if (ly_parse_nodeid(&tmp, &prefix, &pref_len, &name, &name_len) || tmp[0]) {
Michal Vaskob69b68c2021-02-17 11:18:16 +01001392 LOGERR(ctx, LY_EINVAL, "Attribute name \"%s\" is not valid.", name);
Michal Vasko3a41dff2020-07-15 14:30:28 +02001393 return LY_EVALID;
Michal Vasko00cbf532020-06-15 13:58:47 +02001394 }
1395
Michal Vasko51d21b82020-11-13 18:03:54 +01001396 /* get the module */
1397 if (module_name) {
1398 mod_len = strlen(module_name);
1399 } else {
1400 module_name = prefix;
1401 mod_len = pref_len;
1402 }
1403
Michal Vasko00cbf532020-06-15 13:58:47 +02001404 /* set value if none */
Michal Vasko8d65f852021-02-17 11:28:13 +01001405 if (!value) {
1406 value = "";
Michal Vasko00cbf532020-06-15 13:58:47 +02001407 }
1408
Michal Vasko8d65f852021-02-17 11:28:13 +01001409 LY_CHECK_RET(lyd_create_attr(parent, &ret, ctx, name, name_len, prefix, pref_len, module_name, mod_len, value,
Radek Krejci8df109d2021-04-23 12:19:08 +02001410 strlen(value), NULL, LY_VALUE_JSON, NULL, LYD_HINT_DATA));
Michal Vasko8d65f852021-02-17 11:28:13 +01001411
1412 if (attr) {
1413 *attr = ret;
1414 }
1415 return LY_SUCCESS;
1416}
1417
1418API LY_ERR
1419lyd_new_attr2(struct lyd_node *parent, const char *module_ns, const char *name, const char *value,
1420 struct lyd_attr **attr)
1421{
1422 struct lyd_attr *ret = NULL;
1423 const struct ly_ctx *ctx;
1424 const char *prefix, *tmp;
1425 size_t pref_len, name_len;
1426
1427 LY_CHECK_ARG_RET(NULL, parent, !parent->schema, name, LY_EINVAL);
1428
1429 ctx = LYD_CTX(parent);
1430
1431 /* parse the name */
1432 tmp = name;
1433 if (ly_parse_nodeid(&tmp, &prefix, &pref_len, &name, &name_len) || tmp[0]) {
1434 LOGERR(ctx, LY_EINVAL, "Attribute name \"%s\" is not valid.", name);
1435 return LY_EVALID;
1436 }
1437
1438 /* set value if none */
1439 if (!value) {
1440 value = "";
1441 }
1442
1443 LY_CHECK_RET(lyd_create_attr(parent, &ret, ctx, name, name_len, prefix, pref_len, module_ns,
Radek Krejci8df109d2021-04-23 12:19:08 +02001444 module_ns ? strlen(module_ns) : 0, value, strlen(value), NULL, LY_VALUE_XML, NULL, LYD_HINT_DATA));
Michal Vasko3a41dff2020-07-15 14:30:28 +02001445
1446 if (attr) {
1447 *attr = ret;
1448 }
1449 return LY_SUCCESS;
Michal Vasko00cbf532020-06-15 13:58:47 +02001450}
1451
1452API LY_ERR
1453lyd_change_term(struct lyd_node *term, const char *val_str)
1454{
1455 LY_ERR ret = LY_SUCCESS;
1456 struct lysc_type *type;
1457 struct lyd_node_term *t;
1458 struct lyd_node *parent;
1459 struct lyd_value val = {0};
Radek Krejci857189e2020-09-01 13:26:36 +02001460 ly_bool dflt_change, val_change;
Michal Vasko00cbf532020-06-15 13:58:47 +02001461
1462 LY_CHECK_ARG_RET(NULL, term, term->schema, term->schema->nodetype & LYD_NODE_TERM, LY_EINVAL);
1463
1464 if (!val_str) {
1465 val_str = "";
1466 }
1467 t = (struct lyd_node_term *)term;
1468 type = ((struct lysc_node_leaf *)term->schema)->type;
1469
1470 /* parse the new value */
Radek Krejciddace2c2021-01-08 11:30:56 +01001471 LOG_LOCSET(term->schema, term, NULL, NULL);
Radek Krejci8df109d2021-04-23 12:19:08 +02001472 ret = lyd_value_store(LYD_CTX(term), &val, type, val_str, strlen(val_str), NULL, LY_VALUE_JSON, NULL, LYD_HINT_DATA,
Radek Krejci2efc45b2020-12-22 16:25:44 +01001473 term->schema, NULL);
Radek Krejciddace2c2021-01-08 11:30:56 +01001474 LOG_LOCBACK(term->schema ? 1 : 0, 1, 0, 0);
Michal Vaskofeca4fb2020-10-05 08:58:40 +02001475 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko00cbf532020-06-15 13:58:47 +02001476
1477 /* compare original and new value */
1478 if (type->plugin->compare(&t->value, &val)) {
1479 /* values differ, switch them */
Michal Vaskob7be7a82020-08-20 09:09:04 +02001480 type->plugin->free(LYD_CTX(term), &t->value);
Michal Vasko00cbf532020-06-15 13:58:47 +02001481 t->value = val;
1482 memset(&val, 0, sizeof val);
1483 val_change = 1;
1484 } else {
1485 val_change = 0;
1486 }
1487
1488 /* always clear the default flag */
1489 if (term->flags & LYD_DEFAULT) {
Michal Vasko9e685082021-01-29 14:49:09 +01001490 for (parent = term; parent; parent = lyd_parent(parent)) {
Michal Vasko00cbf532020-06-15 13:58:47 +02001491 parent->flags &= ~LYD_DEFAULT;
1492 }
1493 dflt_change = 1;
1494 } else {
1495 dflt_change = 0;
1496 }
1497
1498 if (val_change || dflt_change) {
1499 /* make the node non-validated */
1500 term->flags &= LYD_NEW;
1501 }
1502
1503 if (val_change) {
1504 if (term->schema->nodetype == LYS_LEAFLIST) {
1505 /* leaf-list needs to be hashed again and re-inserted into parent */
1506 lyd_unlink_hash(term);
1507 lyd_hash(term);
1508 LY_CHECK_GOTO(ret = lyd_insert_hash(term), cleanup);
1509 } else if ((term->schema->flags & LYS_KEY) && term->parent) {
1510 /* list needs to be updated if its key was changed */
1511 assert(term->parent->schema->nodetype == LYS_LIST);
Michal Vasko9e685082021-01-29 14:49:09 +01001512 lyd_unlink_hash(lyd_parent(term));
1513 lyd_hash(lyd_parent(term));
1514 LY_CHECK_GOTO(ret = lyd_insert_hash(lyd_parent(term)), cleanup);
Michal Vasko00cbf532020-06-15 13:58:47 +02001515 } /* else leaf that is not a key, its value is not used for its hash so it does not change */
1516 }
1517
1518 /* retrun value */
1519 if (!val_change) {
1520 if (dflt_change) {
1521 /* only default flag change */
1522 ret = LY_EEXIST;
1523 } else {
1524 /* no change */
1525 ret = LY_ENOT;
1526 }
1527 } /* else value changed, LY_SUCCESS */
1528
1529cleanup:
Michal Vasko59512fc2020-12-09 18:13:29 +01001530 if (val.realtype) {
1531 type->plugin->free(LYD_CTX(term), &val);
1532 }
Michal Vasko00cbf532020-06-15 13:58:47 +02001533 return ret;
1534}
1535
Michal Vasko41586352020-07-13 13:54:25 +02001536API LY_ERR
1537lyd_change_meta(struct lyd_meta *meta, const char *val_str)
1538{
1539 LY_ERR ret = LY_SUCCESS;
Radek Krejci2b18bf12020-11-06 11:20:20 +01001540 struct lyd_meta *m2 = NULL;
Michal Vasko41586352020-07-13 13:54:25 +02001541 struct lyd_value val;
Radek Krejci857189e2020-09-01 13:26:36 +02001542 ly_bool val_change;
Michal Vasko41586352020-07-13 13:54:25 +02001543
1544 LY_CHECK_ARG_RET(NULL, meta, LY_EINVAL);
1545
1546 if (!val_str) {
1547 val_str = "";
1548 }
1549
1550 /* parse the new value into a new meta structure */
1551 LY_CHECK_GOTO(ret = lyd_create_meta(NULL, &m2, meta->annotation->module, meta->name, strlen(meta->name), val_str,
Radek Krejci8df109d2021-04-23 12:19:08 +02001552 strlen(val_str), NULL, LY_VALUE_JSON, NULL, LYD_HINT_DATA, 0, NULL), cleanup);
Michal Vasko41586352020-07-13 13:54:25 +02001553
1554 /* compare original and new value */
1555 if (lyd_compare_meta(meta, m2)) {
1556 /* values differ, switch them */
1557 val = meta->value;
1558 meta->value = m2->value;
1559 m2->value = val;
1560 val_change = 1;
1561 } else {
1562 val_change = 0;
1563 }
1564
1565 /* retrun value */
1566 if (!val_change) {
1567 /* no change */
1568 ret = LY_ENOT;
1569 } /* else value changed, LY_SUCCESS */
1570
1571cleanup:
Michal Vasko1a66a5d2020-11-18 18:15:37 +01001572 lyd_free_meta_single(m2);
Michal Vasko41586352020-07-13 13:54:25 +02001573 return ret;
1574}
1575
Michal Vasko6741dc62021-02-04 09:27:45 +01001576static LY_ERR
1577lyd_new_path_check_find_lypath(struct ly_path *path, const char *str_path, const char *value, uint32_t options)
1578{
Michal Vaskoe78faec2021-04-08 17:24:43 +02001579 LY_ERR r;
Michal Vasko6741dc62021-02-04 09:27:45 +01001580 struct ly_path_predicate *pred;
Michal Vaskoe78faec2021-04-08 17:24:43 +02001581 const struct lysc_node *schema = NULL;
1582 LY_ARRAY_COUNT_TYPE u, new_count;
1583 int create = 0;
Michal Vasko6741dc62021-02-04 09:27:45 +01001584
1585 assert(path);
1586
1587 /* go through all the compiled nodes */
1588 LY_ARRAY_FOR(path, u) {
1589 schema = path[u].node;
Michal Vaskoe78faec2021-04-08 17:24:43 +02001590
1591 if (lysc_is_dup_inst_list(schema)) {
1592 if (path[u].pred_type == LY_PATH_PREDTYPE_NONE) {
1593 /* creating a new key-less list or state leaf-list instance */
1594 create = 1;
1595 new_count = u;
1596 } else if (path[u].pred_type != LY_PATH_PREDTYPE_POSITION) {
1597 LOG_LOCSET(schema, NULL, NULL, NULL);
1598 LOGVAL(schema->module->ctx, LYVE_XPATH, "Invalid predicate for %s \"%s\" in path \"%s\".",
1599 lys_nodetype2str(schema->nodetype), schema->name, str_path);
1600 LOG_LOCBACK(1, 0, 0, 0);
1601 return LY_EINVAL;
1602 }
1603 } else if ((schema->nodetype == LYS_LIST) && (path[u].pred_type != LY_PATH_PREDTYPE_LIST)) {
1604 if ((u < LY_ARRAY_COUNT(path) - 1) || !(options & LYD_NEW_PATH_OPAQ)) {
Michal Vasko6741dc62021-02-04 09:27:45 +01001605 LOG_LOCSET(schema, NULL, NULL, NULL);
1606 LOGVAL(schema->module->ctx, LYVE_XPATH, "Predicate missing for %s \"%s\" in path \"%s\".",
1607 lys_nodetype2str(schema->nodetype), schema->name, str_path);
1608 LOG_LOCBACK(1, 0, 0, 0);
1609 return LY_EINVAL;
Michal Vaskoe78faec2021-04-08 17:24:43 +02001610 } /* else creating an opaque list */
1611 } else if ((schema->nodetype == LYS_LEAFLIST) && (path[u].pred_type != LY_PATH_PREDTYPE_LEAFLIST)) {
1612 if (!value) {
1613 value = "";
Michal Vasko6741dc62021-02-04 09:27:45 +01001614 }
Michal Vaskoe78faec2021-04-08 17:24:43 +02001615
1616 r = LY_SUCCESS;
1617 if (options & LYD_NEW_PATH_OPAQ) {
1618 r = lyd_value_validate(NULL, schema, value, strlen(value), NULL, NULL, NULL);
1619 }
1620 if (!r) {
1621 /* store the new predicate so that it is used when searching for this instance */
1622 path[u].pred_type = LY_PATH_PREDTYPE_LEAFLIST;
1623 LY_ARRAY_NEW_RET(schema->module->ctx, path[u].predicates, pred, LY_EMEM);
1624
1625 LY_CHECK_RET(lyd_value_store(schema->module->ctx, &pred->value,
Radek Krejci8df109d2021-04-23 12:19:08 +02001626 ((struct lysc_node_leaflist *)schema)->type, value, strlen(value), NULL, LY_VALUE_JSON, NULL,
Michal Vaskoe78faec2021-04-08 17:24:43 +02001627 LYD_HINT_DATA, schema, NULL));
1628 ++((struct lysc_type *)pred->value.realtype)->refcount;
1629 } /* else we have opaq flag and the value is not valid, leave no predicate and then create an opaque node */
1630 }
Michal Vasko6741dc62021-02-04 09:27:45 +01001631 }
1632
Michal Vaskoe78faec2021-04-08 17:24:43 +02001633 if (create) {
1634 /* hide the nodes that should always be created so they are not found */
1635 while (new_count < LY_ARRAY_COUNT(path)) {
1636 LY_ARRAY_DECREMENT(path);
1637 }
Michal Vasko6741dc62021-02-04 09:27:45 +01001638 }
1639
Michal Vaskoe78faec2021-04-08 17:24:43 +02001640 return LY_SUCCESS;
Michal Vasko6741dc62021-02-04 09:27:45 +01001641}
1642
Radek Krejci95ccd1b2021-03-12 14:57:22 +01001643/**
1644 * @brief Create a new node in the data tree based on a path. All node types can be created.
1645 *
1646 * If @p path points to a list key and the list instance does not exist, the key value from the predicate is used
1647 * and @p value is ignored. Also, if a leaf-list is being created and both a predicate is defined in @p path
1648 * and @p value is set, the predicate is preferred.
1649 *
1650 * For key-less lists and state leaf-lists, positional predicates can be used. If no preciate is used for these
1651 * nodes, they are always created.
1652 *
1653 * @param[in] parent Data parent to add to/modify, can be NULL. Note that in case a first top-level sibling is used,
1654 * it may no longer be first if @p path is absolute and starts with a non-existing top-level node inserted
1655 * before @p parent. Use ::lyd_first_sibling() to adjust @p parent in these cases.
1656 * @param[in] ctx libyang context, must be set if @p parent is NULL.
1657 * @param[in] ext Extension instance where the node being created is defined. This argument takes effect only for absolute
1658 * path or when the relative paths touches document root (top-level). In such cases the present extension instance replaces
1659 * searching for the appropriate module.
1660 * @param[in] path [Path](@ref howtoXPath) to create.
1661 * @param[in] value Value of the new leaf/leaf-list (const char *) or anyxml/anydata (expected type depends on @p value_type).
1662 * For other node types, it is ignored.
1663 * @param[in] value_type Anyxml/anydata node @p value type.
1664 * @param[in] options Bitmask of options, see @ref pathoptions.
1665 * @param[out] new_parent Optional first parent node created. If only one node was created, equals to @p new_node.
1666 * @param[out] new_node Optional last node created.
1667 * @return LY_ERR value.
1668 */
1669static LY_ERR
1670lyd_new_path_(struct lyd_node *parent, const struct ly_ctx *ctx, const struct lysc_ext_instance *ext, const char *path,
1671 const void *value, LYD_ANYDATA_VALUETYPE value_type, uint32_t options,
1672 struct lyd_node **new_parent, struct lyd_node **new_node)
Michal Vasko00cbf532020-06-15 13:58:47 +02001673{
1674 LY_ERR ret = LY_SUCCESS, r;
1675 struct lyxp_expr *exp = NULL;
1676 struct ly_path *p = NULL;
1677 struct lyd_node *nparent = NULL, *nnode = NULL, *node = NULL, *cur_parent;
1678 const struct lysc_node *schema;
Michal Vaskoe78faec2021-04-08 17:24:43 +02001679 const struct lyd_value *val = NULL;
Radek Krejci910d0132021-03-05 14:19:31 +01001680 LY_ARRAY_COUNT_TYPE path_idx = 0, orig_count = 0;
Michal Vasko00cbf532020-06-15 13:58:47 +02001681
Radek Krejci95ccd1b2021-03-12 14:57:22 +01001682 assert(parent || ctx);
1683 assert(path);
1684 assert((path[0] == '/') || parent);
Michal Vasko00cbf532020-06-15 13:58:47 +02001685
1686 if (!ctx) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02001687 ctx = LYD_CTX(parent);
Michal Vasko00cbf532020-06-15 13:58:47 +02001688 }
1689
1690 /* parse path */
Michal Vasko6b26e742020-07-17 15:02:10 +02001691 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 +02001692 LY_PATH_PREFIX_OPTIONAL, LY_PATH_PRED_SIMPLE, &exp), cleanup);
Michal Vasko00cbf532020-06-15 13:58:47 +02001693
1694 /* compile path */
Radek Krejci95ccd1b2021-03-12 14:57:22 +01001695 LY_CHECK_GOTO(ret = ly_path_compile(ctx, NULL, parent ? parent->schema : NULL, ext, exp, LY_PATH_LREF_FALSE,
Radek Krejci8df109d2021-04-23 12:19:08 +02001696 options & LYD_NEW_PATH_OUTPUT ? LY_PATH_OPER_OUTPUT : LY_PATH_OPER_INPUT, LY_PATH_TARGET_MANY, LY_VALUE_JSON,
Michal Vasko405cc9e2020-12-01 12:01:27 +01001697 NULL, NULL, &p), cleanup);
Michal Vasko00cbf532020-06-15 13:58:47 +02001698
Michal Vasko6741dc62021-02-04 09:27:45 +01001699 /* check the compiled path before searching existing nodes, it may be shortened */
1700 orig_count = LY_ARRAY_COUNT(p);
1701 LY_CHECK_GOTO(ret = lyd_new_path_check_find_lypath(p, path, value, options), cleanup);
Michal Vasko00cbf532020-06-15 13:58:47 +02001702
1703 /* try to find any existing nodes in the path */
1704 if (parent) {
1705 ret = ly_path_eval_partial(p, parent, &path_idx, &node);
1706 if (ret == LY_SUCCESS) {
Michal Vasko6741dc62021-02-04 09:27:45 +01001707 if (orig_count == LY_ARRAY_COUNT(p)) {
1708 /* the node exists, are we supposed to update it or is it just a default? */
1709 if (!(options & LYD_NEW_PATH_UPDATE) && !(node->flags & LYD_DEFAULT)) {
1710 LOG_LOCSET(NULL, node, NULL, NULL);
1711 LOGVAL(ctx, LYVE_REFERENCE, "Path \"%s\" already exists", path);
1712 LOG_LOCBACK(0, 1, 0, 0);
1713 ret = LY_EEXIST;
1714 goto cleanup;
1715 }
Michal Vasko00cbf532020-06-15 13:58:47 +02001716
Michal Vasko6741dc62021-02-04 09:27:45 +01001717 /* update the existing node */
1718 ret = lyd_new_path_update(node, value, value_type, &nparent, &nnode);
1719 goto cleanup;
1720 } /* else we were not searching for the whole path */
Michal Vasko00cbf532020-06-15 13:58:47 +02001721 } else if (ret == LY_EINCOMPLETE) {
1722 /* some nodes were found, adjust the iterator to the next segment */
1723 ++path_idx;
1724 } else if (ret == LY_ENOTFOUND) {
1725 /* 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 +01001726 if (lysc_data_parent(p[0].node)) {
Michal Vasko00cbf532020-06-15 13:58:47 +02001727 node = parent;
1728 }
1729 } else {
1730 /* error */
1731 goto cleanup;
1732 }
1733 }
1734
Michal Vasko6741dc62021-02-04 09:27:45 +01001735 /* restore the full path for creating new nodes */
1736 while (orig_count > LY_ARRAY_COUNT(p)) {
1737 LY_ARRAY_INCREMENT(p);
1738 }
1739
Michal Vasko00cbf532020-06-15 13:58:47 +02001740 /* create all the non-existing nodes in a loop */
Michal Vaskod989ba02020-08-24 10:59:24 +02001741 for ( ; path_idx < LY_ARRAY_COUNT(p); ++path_idx) {
Michal Vasko00cbf532020-06-15 13:58:47 +02001742 cur_parent = node;
1743 schema = p[path_idx].node;
1744
1745 switch (schema->nodetype) {
1746 case LYS_LIST:
Michal Vaskoe78faec2021-04-08 17:24:43 +02001747 if (lysc_is_dup_inst_list(schema)) {
1748 /* create key-less list instance */
1749 LY_CHECK_GOTO(ret = lyd_create_inner(schema, &node), cleanup);
1750 } else if ((options & LYD_NEW_PATH_OPAQ) && (p[path_idx].pred_type == LY_PATH_PREDTYPE_NONE)) {
1751 /* creating opaque list without keys */
1752 LY_CHECK_GOTO(ret = lyd_create_opaq(ctx, schema->name, strlen(schema->name), NULL, 0,
Radek Krejci8df109d2021-04-23 12:19:08 +02001753 schema->module->name, strlen(schema->module->name), NULL, 0, NULL, LY_VALUE_JSON, NULL,
Michal Vaskoe78faec2021-04-08 17:24:43 +02001754 LYD_NODEHINT_LIST, &node), cleanup);
1755 } else {
1756 /* create standard list instance */
1757 assert(p[path_idx].pred_type == LY_PATH_PREDTYPE_LIST);
1758 LY_CHECK_GOTO(ret = lyd_create_list(schema, p[path_idx].predicates, &node), cleanup);
Michal Vasko00cbf532020-06-15 13:58:47 +02001759 }
Michal Vaskoe78faec2021-04-08 17:24:43 +02001760 break;
Michal Vasko00cbf532020-06-15 13:58:47 +02001761 case LYS_CONTAINER:
1762 case LYS_NOTIF:
1763 case LYS_RPC:
1764 case LYS_ACTION:
1765 LY_CHECK_GOTO(ret = lyd_create_inner(schema, &node), cleanup);
1766 break;
1767 case LYS_LEAFLIST:
Michal Vaskoe78faec2021-04-08 17:24:43 +02001768 if ((options & LYD_NEW_PATH_OPAQ) && (p[path_idx].pred_type != LY_PATH_PREDTYPE_LEAFLIST)) {
1769 /* we have not checked this only for dup-inst lists, otherwise it must be opaque */
1770 r = LY_EVALID;
1771 if (lysc_is_dup_inst_list(schema)) {
1772 /* validate value */
1773 r = lyd_value_validate(NULL, schema, value ? value : "", value ? strlen(value) : 0, NULL, NULL, NULL);
1774 }
1775 if (r && (r != LY_EINCOMPLETE)) {
1776 /* creating opaque leaf-list */
1777 LY_CHECK_GOTO(ret = lyd_create_opaq(ctx, schema->name, strlen(schema->name), value,
1778 value ? strlen(value) : 0, schema->module->name, strlen(schema->module->name), NULL, 0, NULL,
Radek Krejci8df109d2021-04-23 12:19:08 +02001779 LY_VALUE_JSON, NULL, LYD_NODEHINT_LEAFLIST, &node), cleanup);
Michal Vaskoe78faec2021-04-08 17:24:43 +02001780 break;
1781 }
1782 }
1783
1784 /* get value to set */
1785 if (p[path_idx].pred_type == LY_PATH_PREDTYPE_LEAFLIST) {
1786 val = &p[path_idx].predicates[0].value;
1787 } else if (!value) {
1788 value = "";
1789 }
1790
1791 /* create a leaf-list instance */
1792 if (val) {
Michal Vasko00cbf532020-06-15 13:58:47 +02001793 LY_CHECK_GOTO(ret = lyd_create_term2(schema, &p[path_idx].predicates[0].value, &node), cleanup);
Michal Vaskoe78faec2021-04-08 17:24:43 +02001794 } else {
Radek Krejci8df109d2021-04-23 12:19:08 +02001795 LY_CHECK_GOTO(ret = lyd_create_term(schema, value, strlen(value), NULL, LY_VALUE_JSON, NULL,
Michal Vaskoe78faec2021-04-08 17:24:43 +02001796 LYD_HINT_DATA, NULL, &node), cleanup);
Michal Vasko00cbf532020-06-15 13:58:47 +02001797 }
1798 break;
1799 case LYS_LEAF:
Michal Vasko35880332020-12-08 13:08:12 +01001800 if (lysc_is_key(schema)) {
1801 /* it must have been already created or some error will occur later */
1802 assert(cur_parent);
1803 node = lyd_child(cur_parent);
1804 assert(node && (node->schema == schema));
1805 goto next_iter;
1806 }
1807
Michal Vaskoe78faec2021-04-08 17:24:43 +02001808 if (options & LYD_NEW_PATH_OPAQ) {
1809 /* validate value */
1810 r = lyd_value_validate(NULL, schema, value ? value : "", value ? strlen(value) : 0, NULL, NULL, NULL);
1811 if (r && (r != LY_EINCOMPLETE)) {
1812 /* creating opaque leaf */
1813 LY_CHECK_GOTO(ret = lyd_create_opaq(ctx, schema->name, strlen(schema->name), value,
1814 value ? strlen(value) : 0, schema->module->name, strlen(schema->module->name), NULL, 0, NULL,
Radek Krejci8df109d2021-04-23 12:19:08 +02001815 LY_VALUE_JSON, NULL, 0, &node), cleanup);
Michal Vaskoe78faec2021-04-08 17:24:43 +02001816 break;
1817 }
1818 }
1819
1820 /* get value to set */
Michal Vasko00cbf532020-06-15 13:58:47 +02001821 if (!value) {
1822 value = "";
1823 }
1824
Michal Vaskoe78faec2021-04-08 17:24:43 +02001825 /* create a leaf instance */
Radek Krejci8df109d2021-04-23 12:19:08 +02001826 LY_CHECK_GOTO(ret = lyd_create_term(schema, value, strlen(value), NULL, LY_VALUE_JSON, NULL, LYD_HINT_DATA,
Michal Vaskoe78faec2021-04-08 17:24:43 +02001827 NULL, &node), cleanup);
Michal Vasko00cbf532020-06-15 13:58:47 +02001828 break;
1829 case LYS_ANYDATA:
1830 case LYS_ANYXML:
Michal Vasko366a4a12020-12-04 16:23:57 +01001831 LY_CHECK_GOTO(ret = lyd_create_any(schema, value, value_type, 0, &node), cleanup);
Michal Vasko00cbf532020-06-15 13:58:47 +02001832 break;
1833 default:
1834 LOGINT(ctx);
1835 ret = LY_EINT;
1836 goto cleanup;
1837 }
1838
1839 if (cur_parent) {
1840 /* connect to the parent */
1841 lyd_insert_node(cur_parent, NULL, node);
1842 } else if (parent) {
1843 /* connect to top-level siblings */
1844 lyd_insert_node(NULL, &parent, node);
1845 }
1846
Michal Vasko35880332020-12-08 13:08:12 +01001847next_iter:
Michal Vasko00cbf532020-06-15 13:58:47 +02001848 /* update remembered nodes */
1849 if (!nparent) {
1850 nparent = node;
1851 }
1852 nnode = node;
1853 }
1854
1855cleanup:
1856 lyxp_expr_free(ctx, exp);
Michal Vasko6741dc62021-02-04 09:27:45 +01001857 if (p) {
1858 while (orig_count > LY_ARRAY_COUNT(p)) {
1859 LY_ARRAY_INCREMENT(p);
1860 }
1861 }
Michal Vasko00cbf532020-06-15 13:58:47 +02001862 ly_path_free(ctx, p);
1863 if (!ret) {
1864 /* set out params only on success */
1865 if (new_parent) {
1866 *new_parent = nparent;
1867 }
1868 if (new_node) {
1869 *new_node = nnode;
1870 }
Michal Vaskof4b95ba2020-12-11 08:42:33 +01001871 } else {
1872 lyd_free_tree(nparent);
Michal Vasko00cbf532020-06-15 13:58:47 +02001873 }
1874 return ret;
1875}
1876
Radek Krejci95ccd1b2021-03-12 14:57:22 +01001877API LY_ERR
1878lyd_new_path(struct lyd_node *parent, const struct ly_ctx *ctx, const char *path, const char *value, uint32_t options,
1879 struct lyd_node **node)
1880{
1881 LY_CHECK_ARG_RET(ctx, parent || ctx, path, (path[0] == '/') || parent, LY_EINVAL);
1882 return lyd_new_path_(parent, ctx, NULL, path, value, LYD_ANYDATA_STRING, options, node, NULL);
1883}
1884
1885API LY_ERR
1886lyd_new_path2(struct lyd_node *parent, const struct ly_ctx *ctx, const char *path, const void *value,
1887 LYD_ANYDATA_VALUETYPE value_type, uint32_t options, struct lyd_node **new_parent, struct lyd_node **new_node)
1888{
1889 LY_CHECK_ARG_RET(ctx, parent || ctx, path, (path[0] == '/') || parent, LY_EINVAL);
1890 return lyd_new_path_(parent, ctx, NULL, path, value, value_type, options, new_parent, new_node);
1891}
1892
1893API LY_ERR
1894lyd_new_ext_path(struct lyd_node *parent, const struct lysc_ext_instance *ext, const char *path, const void *value,
1895 uint32_t options, struct lyd_node **node)
1896{
1897 const struct ly_ctx *ctx = ext ? ext->module->ctx : NULL;
1898
1899 LY_CHECK_ARG_RET(ctx, ext, path, (path[0] == '/') || parent, LY_EINVAL);
1900 return lyd_new_path_(parent, ctx, ext, path, value, LYD_ANYDATA_STRING, options, node, NULL);
1901}
1902
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001903LY_ERR
1904lyd_new_implicit_r(struct lyd_node *parent, struct lyd_node **first, const struct lysc_node *sparent,
Radek Krejci4f2e3e52021-03-30 14:20:28 +02001905 const struct lys_module *mod, struct ly_set *node_when, struct ly_set *node_exts, struct ly_set *node_types,
1906 uint32_t impl_opts, struct lyd_node **diff)
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001907{
1908 LY_ERR ret;
Michal Vaskod1e53b92021-01-28 13:11:06 +01001909 const struct lysc_node *iter = NULL;
Radek Krejci2b18bf12020-11-06 11:20:20 +01001910 struct lyd_node *node = NULL;
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001911 struct lyd_value **dflts;
1912 LY_ARRAY_COUNT_TYPE u;
Michal Vasko630d9892020-12-08 17:11:08 +01001913 uint32_t getnext_opts;
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001914
1915 assert(first && (parent || sparent || mod));
1916
1917 if (!sparent && parent) {
1918 sparent = parent->schema;
1919 }
1920
Michal Vasko630d9892020-12-08 17:11:08 +01001921 getnext_opts = LYS_GETNEXT_WITHCHOICE;
1922 if (impl_opts & LYD_IMPLICIT_OUTPUT) {
1923 getnext_opts |= LYS_GETNEXT_OUTPUT;
1924 }
1925
1926 while ((iter = lys_getnext(iter, sparent, mod ? mod->compiled : NULL, getnext_opts))) {
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001927 if ((impl_opts & LYD_IMPLICIT_NO_STATE) && (iter->flags & LYS_CONFIG_R)) {
1928 continue;
Michal Vasko44b19a12020-08-07 09:21:30 +02001929 } else if ((impl_opts & LYD_IMPLICIT_NO_CONFIG) && (iter->flags & LYS_CONFIG_W)) {
1930 continue;
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001931 }
1932
1933 switch (iter->nodetype) {
1934 case LYS_CHOICE:
Michal Vaskobcf4d7d2020-11-18 18:16:49 +01001935 node = lys_getnext_data(NULL, *first, NULL, iter, NULL);
1936 if (!node && ((struct lysc_node_choice *)iter)->dflt) {
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001937 /* create default case data */
Michal Vasko14ed9cd2021-01-28 14:16:25 +01001938 LY_CHECK_RET(lyd_new_implicit_r(parent, first, &((struct lysc_node_choice *)iter)->dflt->node,
Radek Krejci4f2e3e52021-03-30 14:20:28 +02001939 NULL, node_when, node_exts, node_types, impl_opts, diff));
Michal Vaskobcf4d7d2020-11-18 18:16:49 +01001940 } else if (node) {
1941 /* create any default data in the existing case */
1942 assert(node->schema->parent->nodetype == LYS_CASE);
Radek Krejci4f2e3e52021-03-30 14:20:28 +02001943 LY_CHECK_RET(lyd_new_implicit_r(parent, first, node->schema->parent, NULL, node_when, node_exts, node_types,
Michal Vaskobcf4d7d2020-11-18 18:16:49 +01001944 impl_opts, diff));
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001945 }
1946 break;
1947 case LYS_CONTAINER:
1948 if (!(iter->flags & LYS_PRESENCE) && lyd_find_sibling_val(*first, iter, NULL, 0, NULL)) {
1949 /* create default NP container */
1950 LY_CHECK_RET(lyd_create_inner(iter, &node));
Michal Vaskof4d67ea2021-03-31 13:53:21 +02001951 node->flags = LYD_DEFAULT | (lysc_has_when(iter) ? LYD_WHEN_TRUE : 0);
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001952 lyd_insert_node(parent, first, node);
1953
Michal Vaskof4d67ea2021-03-31 13:53:21 +02001954 if (lysc_has_when(iter) && node_when) {
Michal Vaskoe16c7b72021-02-26 10:39:06 +01001955 /* remember to resolve when */
1956 LY_CHECK_RET(ly_set_add(node_when, node, 1, NULL));
1957 }
Radek Krejci4f2e3e52021-03-30 14:20:28 +02001958 if (node_exts) {
1959 /* remember to call all the extension's validation callbacks */
1960 LY_CHECK_RET(lysc_node_ext_tovalidate(node_exts, node));
1961 }
Michal Vaskoe49b6322020-11-05 17:38:36 +01001962 if (diff) {
1963 /* add into diff */
1964 LY_CHECK_RET(lyd_val_diff_add(node, LYD_DIFF_OP_CREATE, diff));
1965 }
1966
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001967 /* create any default children */
Radek Krejci4f2e3e52021-03-30 14:20:28 +02001968 LY_CHECK_RET(lyd_new_implicit_r(node, lyd_node_child_p(node), NULL, NULL, node_when, node_exts, node_types,
Michal Vasko69730152020-10-09 16:30:07 +02001969 impl_opts, diff));
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001970 }
1971 break;
1972 case LYS_LEAF:
Michal Vasko69730152020-10-09 16:30:07 +02001973 if (!(impl_opts & LYD_IMPLICIT_NO_DEFAULTS) && ((struct lysc_node_leaf *)iter)->dflt &&
1974 lyd_find_sibling_val(*first, iter, NULL, 0, NULL)) {
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001975 /* create default leaf */
1976 ret = lyd_create_term2(iter, ((struct lysc_node_leaf *)iter)->dflt, &node);
1977 if (ret == LY_EINCOMPLETE) {
1978 if (node_types) {
1979 /* remember to resolve type */
Radek Krejci3d92e442020-10-12 12:48:13 +02001980 LY_CHECK_RET(ly_set_add(node_types, node, 1, NULL));
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001981 }
1982 } else if (ret) {
1983 return ret;
1984 }
Michal Vaskof4d67ea2021-03-31 13:53:21 +02001985 node->flags = LYD_DEFAULT | (lysc_has_when(iter) ? LYD_WHEN_TRUE : 0);
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001986 lyd_insert_node(parent, first, node);
1987
Michal Vaskof4d67ea2021-03-31 13:53:21 +02001988 if (lysc_has_when(iter) && node_when) {
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001989 /* remember to resolve when */
Radek Krejci3d92e442020-10-12 12:48:13 +02001990 LY_CHECK_RET(ly_set_add(node_when, node, 1, NULL));
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001991 }
Radek Krejci4f2e3e52021-03-30 14:20:28 +02001992 if (node_exts) {
1993 /* remember to call all the extension's validation callbacks */
1994 LY_CHECK_RET(lysc_node_ext_tovalidate(node_exts, node));
1995 }
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001996 if (diff) {
1997 /* add into diff */
1998 LY_CHECK_RET(lyd_val_diff_add(node, LYD_DIFF_OP_CREATE, diff));
1999 }
2000 }
2001 break;
2002 case LYS_LEAFLIST:
Michal Vasko69730152020-10-09 16:30:07 +02002003 if (!(impl_opts & LYD_IMPLICIT_NO_DEFAULTS) && ((struct lysc_node_leaflist *)iter)->dflts &&
2004 lyd_find_sibling_val(*first, iter, NULL, 0, NULL)) {
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002005 /* create all default leaf-lists */
2006 dflts = ((struct lysc_node_leaflist *)iter)->dflts;
2007 LY_ARRAY_FOR(dflts, u) {
2008 ret = lyd_create_term2(iter, dflts[u], &node);
2009 if (ret == LY_EINCOMPLETE) {
2010 if (node_types) {
2011 /* remember to resolve type */
Radek Krejci3d92e442020-10-12 12:48:13 +02002012 LY_CHECK_RET(ly_set_add(node_types, node, 1, NULL));
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002013 }
2014 } else if (ret) {
2015 return ret;
2016 }
Michal Vaskof4d67ea2021-03-31 13:53:21 +02002017 node->flags = LYD_DEFAULT | (lysc_has_when(iter) ? LYD_WHEN_TRUE : 0);
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002018 lyd_insert_node(parent, first, node);
2019
Michal Vaskof4d67ea2021-03-31 13:53:21 +02002020 if (lysc_has_when(iter) && node_when) {
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002021 /* remember to resolve when */
Radek Krejci3d92e442020-10-12 12:48:13 +02002022 LY_CHECK_RET(ly_set_add(node_when, node, 1, NULL));
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002023 }
Radek Krejci4f2e3e52021-03-30 14:20:28 +02002024 if (node_exts) {
2025 /* remember to call all the extension's validation callbacks */
2026 LY_CHECK_RET(lysc_node_ext_tovalidate(node_exts, node));
2027 }
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002028 if (diff) {
2029 /* add into diff */
2030 LY_CHECK_RET(lyd_val_diff_add(node, LYD_DIFF_OP_CREATE, diff));
2031 }
2032 }
2033 }
2034 break;
2035 default:
2036 /* without defaults */
2037 break;
2038 }
2039 }
2040
2041 return LY_SUCCESS;
2042}
2043
2044API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02002045lyd_new_implicit_tree(struct lyd_node *tree, uint32_t implicit_options, struct lyd_node **diff)
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002046{
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002047 LY_ERR ret = LY_SUCCESS;
Michal Vasko3488ada2020-12-03 14:18:19 +01002048 struct lyd_node *node;
Radek Krejci4f2e3e52021-03-30 14:20:28 +02002049 struct ly_set node_when = {0}, node_exts = {0};
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002050
2051 LY_CHECK_ARG_RET(NULL, tree, LY_EINVAL);
2052 if (diff) {
2053 *diff = NULL;
2054 }
2055
Michal Vasko56daf732020-08-10 10:57:18 +02002056 LYD_TREE_DFS_BEGIN(tree, node) {
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002057 /* skip added default nodes */
Michal Vasko69730152020-10-09 16:30:07 +02002058 if (((node->flags & (LYD_DEFAULT | LYD_NEW)) != (LYD_DEFAULT | LYD_NEW)) &&
2059 (node->schema->nodetype & LYD_NODE_INNER)) {
Radek Krejci4f2e3e52021-03-30 14:20:28 +02002060 LY_CHECK_GOTO(ret = lyd_new_implicit_r(node, lyd_node_child_p(node), NULL, NULL, &node_when, &node_exts,
Michal Vaskoc43c8ab2021-03-05 13:32:44 +01002061 NULL, implicit_options, diff), cleanup);
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002062 }
2063
Michal Vasko56daf732020-08-10 10:57:18 +02002064 LYD_TREE_DFS_END(tree, node);
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002065 }
2066
Michal Vasko3488ada2020-12-03 14:18:19 +01002067 /* resolve when and remove any invalid defaults */
Radek Krejci4f2e3e52021-03-30 14:20:28 +02002068 LY_CHECK_GOTO(ret = lyd_validate_unres(&tree, NULL, &node_when, &node_exts, NULL, NULL, diff), cleanup);
Michal Vasko3488ada2020-12-03 14:18:19 +01002069
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002070cleanup:
Michal Vasko3488ada2020-12-03 14:18:19 +01002071 ly_set_erase(&node_when, NULL);
Radek Krejci4f2e3e52021-03-30 14:20:28 +02002072 ly_set_erase(&node_exts, NULL);
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002073 if (ret && diff) {
2074 lyd_free_all(*diff);
2075 *diff = NULL;
2076 }
2077 return ret;
2078}
2079
2080API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02002081lyd_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 +02002082{
2083 const struct lys_module *mod;
2084 struct lyd_node *d = NULL;
2085 uint32_t i = 0;
2086 LY_ERR ret = LY_SUCCESS;
2087
2088 LY_CHECK_ARG_RET(ctx, tree, *tree || ctx, LY_EINVAL);
2089 if (diff) {
2090 *diff = NULL;
2091 }
2092 if (!ctx) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02002093 ctx = LYD_CTX(*tree);
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002094 }
2095
2096 /* add nodes for each module one-by-one */
2097 while ((mod = ly_ctx_get_module_iter(ctx, &i))) {
2098 if (!mod->implemented) {
2099 continue;
2100 }
2101
2102 LY_CHECK_GOTO(ret = lyd_new_implicit_module(tree, mod, implicit_options, diff ? &d : NULL), cleanup);
2103 if (d) {
2104 /* merge into one diff */
2105 lyd_insert_sibling(*diff, d, diff);
2106
2107 d = NULL;
2108 }
2109 }
2110
2111cleanup:
2112 if (ret && diff) {
2113 lyd_free_all(*diff);
2114 *diff = NULL;
2115 }
2116 return ret;
2117}
2118
2119API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02002120lyd_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 +02002121{
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002122 LY_ERR ret = LY_SUCCESS;
Michal Vasko3488ada2020-12-03 14:18:19 +01002123 struct lyd_node *root, *d = NULL;
Radek Krejci4f2e3e52021-03-30 14:20:28 +02002124 struct ly_set node_when = {0}, node_exts = {0};
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002125
2126 LY_CHECK_ARG_RET(NULL, tree, module, LY_EINVAL);
2127 if (diff) {
2128 *diff = NULL;
2129 }
2130
2131 /* add all top-level defaults for this module */
Radek Krejci4f2e3e52021-03-30 14:20:28 +02002132 LY_CHECK_GOTO(ret = lyd_new_implicit_r(NULL, tree, NULL, module, &node_when, &node_exts, NULL, implicit_options, diff), cleanup);
Michal Vasko3488ada2020-12-03 14:18:19 +01002133
2134 /* resolve when and remove any invalid defaults */
Radek Krejci4f2e3e52021-03-30 14:20:28 +02002135 LY_CHECK_GOTO(ret = lyd_validate_unres(tree, module, &node_when, &node_exts, NULL, NULL, diff), cleanup);
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002136
2137 /* process nested nodes */
2138 LY_LIST_FOR(*tree, root) {
2139 /* skip added default nodes */
2140 if ((root->flags & (LYD_DEFAULT | LYD_NEW)) != (LYD_DEFAULT | LYD_NEW)) {
2141 LY_CHECK_GOTO(ret = lyd_new_implicit_tree(root, implicit_options, diff ? &d : NULL), cleanup);
2142
2143 if (d) {
2144 /* merge into one diff */
2145 lyd_insert_sibling(*diff, d, diff);
2146
2147 d = NULL;
2148 }
2149 }
2150 }
2151
2152cleanup:
Michal Vasko3488ada2020-12-03 14:18:19 +01002153 ly_set_erase(&node_when, NULL);
Radek Krejci4f2e3e52021-03-30 14:20:28 +02002154 ly_set_erase(&node_exts, NULL);
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002155 if (ret && diff) {
2156 lyd_free_all(*diff);
2157 *diff = NULL;
2158 }
2159 return ret;
2160}
2161
Michal Vasko90932a92020-02-12 14:33:03 +01002162struct lyd_node *
Michal Vaskob104f112020-07-17 09:54:54 +02002163lyd_insert_get_next_anchor(const struct lyd_node *first_sibling, const struct lyd_node *new_node)
Michal Vasko90932a92020-02-12 14:33:03 +01002164{
Michal Vaskob104f112020-07-17 09:54:54 +02002165 const struct lysc_node *schema, *sparent;
Michal Vasko90932a92020-02-12 14:33:03 +01002166 struct lyd_node *match = NULL;
Radek Krejci857189e2020-09-01 13:26:36 +02002167 ly_bool found;
Michal Vasko93b16062020-12-09 18:14:18 +01002168 uint32_t getnext_opts;
Michal Vasko90932a92020-02-12 14:33:03 +01002169
Michal Vaskob104f112020-07-17 09:54:54 +02002170 assert(new_node);
2171
2172 if (!first_sibling || !new_node->schema) {
2173 /* insert at the end, no next anchor */
Michal Vasko90932a92020-02-12 14:33:03 +01002174 return NULL;
2175 }
2176
Michal Vasko93b16062020-12-09 18:14:18 +01002177 getnext_opts = 0;
Michal Vaskod1e53b92021-01-28 13:11:06 +01002178 if (new_node->schema->flags & LYS_IS_OUTPUT) {
Michal Vasko93b16062020-12-09 18:14:18 +01002179 getnext_opts = LYS_GETNEXT_OUTPUT;
2180 }
2181
Michal Vaskob104f112020-07-17 09:54:54 +02002182 if (first_sibling->parent && first_sibling->parent->children_ht) {
2183 /* find the anchor using hashes */
2184 sparent = first_sibling->parent->schema;
Michal Vasko93b16062020-12-09 18:14:18 +01002185 schema = lys_getnext(new_node->schema, sparent, NULL, getnext_opts);
Michal Vaskob104f112020-07-17 09:54:54 +02002186 while (schema) {
2187 /* keep trying to find the first existing instance of the closest following schema sibling,
2188 * otherwise return NULL - inserting at the end */
2189 if (!lyd_find_sibling_schema(first_sibling, schema, &match)) {
2190 break;
2191 }
2192
Michal Vasko93b16062020-12-09 18:14:18 +01002193 schema = lys_getnext(schema, sparent, NULL, getnext_opts);
Michal Vaskob104f112020-07-17 09:54:54 +02002194 }
2195 } else {
2196 /* find the anchor without hashes */
2197 match = (struct lyd_node *)first_sibling;
2198 if (!lysc_data_parent(new_node->schema)) {
2199 /* we are in top-level, skip all the data from preceding modules */
2200 LY_LIST_FOR(match, match) {
2201 if (!match->schema || (strcmp(lyd_owner_module(match)->name, lyd_owner_module(new_node)->name) >= 0)) {
2202 break;
2203 }
2204 }
2205 }
2206
2207 /* get the first schema sibling */
2208 sparent = lysc_data_parent(new_node->schema);
Michal Vasko93b16062020-12-09 18:14:18 +01002209 schema = lys_getnext(NULL, sparent, new_node->schema->module->compiled, getnext_opts);
Michal Vaskob104f112020-07-17 09:54:54 +02002210
2211 found = 0;
2212 LY_LIST_FOR(match, match) {
2213 if (!match->schema || (lyd_owner_module(match) != lyd_owner_module(new_node))) {
2214 /* we have found an opaque node, which must be at the end, so use it OR
2215 * modules do not match, so we must have traversed all the data from new_node module (if any),
2216 * we have found the first node of the next module, that is what we want */
2217 break;
2218 }
2219
2220 /* skip schema nodes until we find the instantiated one */
2221 while (!found) {
2222 if (new_node->schema == schema) {
2223 /* we have found the schema of the new node, continue search to find the first
2224 * data node with a different schema (after our schema) */
2225 found = 1;
2226 break;
2227 }
2228 if (match->schema == schema) {
2229 /* current node (match) is a data node still before the new node, continue search in data */
2230 break;
2231 }
Michal Vasko93b16062020-12-09 18:14:18 +01002232 schema = lys_getnext(schema, sparent, new_node->schema->module->compiled, getnext_opts);
Michal Vaskob104f112020-07-17 09:54:54 +02002233 assert(schema);
2234 }
2235
2236 if (found && (match->schema != new_node->schema)) {
2237 /* find the next node after we have found our node schema data instance */
2238 break;
2239 }
2240 }
Michal Vasko90932a92020-02-12 14:33:03 +01002241 }
2242
2243 return match;
2244}
2245
2246/**
2247 * @brief Insert node after a sibling.
2248 *
Michal Vasko9f96a052020-03-10 09:41:45 +01002249 * Handles inserting into NP containers and key-less lists.
2250 *
Michal Vasko90932a92020-02-12 14:33:03 +01002251 * @param[in] sibling Sibling to insert after.
2252 * @param[in] node Node to insert.
2253 */
2254static void
Michal Vaskof03ed032020-03-04 13:31:44 +01002255lyd_insert_after_node(struct lyd_node *sibling, struct lyd_node *node)
Michal Vasko90932a92020-02-12 14:33:03 +01002256{
Michal Vasko0249f7c2020-03-05 16:36:40 +01002257 struct lyd_node_inner *par;
2258
Michal Vasko90932a92020-02-12 14:33:03 +01002259 assert(!node->next && (node->prev == node));
2260
2261 node->next = sibling->next;
2262 node->prev = sibling;
2263 sibling->next = node;
2264 if (node->next) {
2265 /* sibling had a succeeding node */
2266 node->next->prev = node;
2267 } else {
2268 /* sibling was last, find first sibling and change its prev */
2269 if (sibling->parent) {
2270 sibling = sibling->parent->child;
2271 } else {
Michal Vaskod989ba02020-08-24 10:59:24 +02002272 for ( ; sibling->prev->next != node; sibling = sibling->prev) {}
Michal Vasko90932a92020-02-12 14:33:03 +01002273 }
2274 sibling->prev = node;
2275 }
2276 node->parent = sibling->parent;
Michal Vasko0249f7c2020-03-05 16:36:40 +01002277
Michal Vasko9f96a052020-03-10 09:41:45 +01002278 for (par = node->parent; par; par = par->parent) {
2279 if ((par->flags & LYD_DEFAULT) && !(node->flags & LYD_DEFAULT)) {
2280 /* remove default flags from NP containers */
Michal Vasko0249f7c2020-03-05 16:36:40 +01002281 par->flags &= ~LYD_DEFAULT;
2282 }
2283 }
Michal Vasko90932a92020-02-12 14:33:03 +01002284}
2285
2286/**
2287 * @brief Insert node before a sibling.
2288 *
Michal Vasko9f96a052020-03-10 09:41:45 +01002289 * Handles inserting into NP containers and key-less lists.
2290 *
Michal Vasko90932a92020-02-12 14:33:03 +01002291 * @param[in] sibling Sibling to insert before.
2292 * @param[in] node Node to insert.
2293 */
2294static void
Michal Vaskof03ed032020-03-04 13:31:44 +01002295lyd_insert_before_node(struct lyd_node *sibling, struct lyd_node *node)
Michal Vasko90932a92020-02-12 14:33:03 +01002296{
Michal Vasko0249f7c2020-03-05 16:36:40 +01002297 struct lyd_node_inner *par;
2298
Michal Vasko90932a92020-02-12 14:33:03 +01002299 assert(!node->next && (node->prev == node));
2300
2301 node->next = sibling;
2302 /* covers situation of sibling being first */
2303 node->prev = sibling->prev;
2304 sibling->prev = node;
2305 if (node->prev->next) {
2306 /* sibling had a preceding node */
2307 node->prev->next = node;
2308 } else if (sibling->parent) {
2309 /* sibling was first and we must also change parent child pointer */
2310 sibling->parent->child = node;
2311 }
2312 node->parent = sibling->parent;
Michal Vasko0249f7c2020-03-05 16:36:40 +01002313
Michal Vasko9f96a052020-03-10 09:41:45 +01002314 for (par = node->parent; par; par = par->parent) {
2315 if ((par->flags & LYD_DEFAULT) && !(node->flags & LYD_DEFAULT)) {
2316 /* remove default flags from NP containers */
Michal Vasko0249f7c2020-03-05 16:36:40 +01002317 par->flags &= ~LYD_DEFAULT;
2318 }
2319 }
Michal Vasko90932a92020-02-12 14:33:03 +01002320}
2321
2322/**
Michal Vaskob104f112020-07-17 09:54:54 +02002323 * @brief Insert node as the first and only child of a parent.
Michal Vasko90932a92020-02-12 14:33:03 +01002324 *
Michal Vasko9f96a052020-03-10 09:41:45 +01002325 * Handles inserting into NP containers and key-less lists.
2326 *
Michal Vasko90932a92020-02-12 14:33:03 +01002327 * @param[in] parent Parent to insert into.
2328 * @param[in] node Node to insert.
2329 */
2330static void
Michal Vaskob104f112020-07-17 09:54:54 +02002331lyd_insert_only_child(struct lyd_node *parent, struct lyd_node *node)
Michal Vasko90932a92020-02-12 14:33:03 +01002332{
2333 struct lyd_node_inner *par;
2334
Radek Krejcia1c1e542020-09-29 16:06:52 +02002335 assert(parent && !lyd_child(parent) && !node->next && (node->prev == node));
Michal Vasko52927e22020-03-16 17:26:14 +01002336 assert(!parent->schema || (parent->schema->nodetype & LYD_NODE_INNER));
Michal Vasko90932a92020-02-12 14:33:03 +01002337
2338 par = (struct lyd_node_inner *)parent;
2339
Michal Vaskob104f112020-07-17 09:54:54 +02002340 par->child = node;
Michal Vasko90932a92020-02-12 14:33:03 +01002341 node->parent = par;
Michal Vasko0249f7c2020-03-05 16:36:40 +01002342
Michal Vaskod989ba02020-08-24 10:59:24 +02002343 for ( ; par; par = par->parent) {
Michal Vasko9f96a052020-03-10 09:41:45 +01002344 if ((par->flags & LYD_DEFAULT) && !(node->flags & LYD_DEFAULT)) {
2345 /* remove default flags from NP containers */
Michal Vasko0249f7c2020-03-05 16:36:40 +01002346 par->flags &= ~LYD_DEFAULT;
2347 }
2348 }
Michal Vasko751cb4d2020-07-14 12:25:28 +02002349}
Michal Vasko0249f7c2020-03-05 16:36:40 +01002350
Michal Vasko751cb4d2020-07-14 12:25:28 +02002351/**
2352 * @brief Learn whether a list instance has all the keys.
2353 *
2354 * @param[in] list List instance to check.
2355 * @return non-zero if all the keys were found,
2356 * @return 0 otherwise.
2357 */
2358static int
2359lyd_insert_has_keys(const struct lyd_node *list)
2360{
2361 const struct lyd_node *key;
2362 const struct lysc_node *skey = NULL;
2363
2364 assert(list->schema->nodetype == LYS_LIST);
Radek Krejcia1c1e542020-09-29 16:06:52 +02002365 key = lyd_child(list);
Michal Vasko751cb4d2020-07-14 12:25:28 +02002366 while ((skey = lys_getnext(skey, list->schema, NULL, 0)) && (skey->flags & LYS_KEY)) {
2367 if (!key || (key->schema != skey)) {
2368 /* key missing */
2369 return 0;
2370 }
2371
2372 key = key->next;
2373 }
2374
2375 /* all keys found */
2376 return 1;
Michal Vasko90932a92020-02-12 14:33:03 +01002377}
2378
2379void
Michal Vaskob104f112020-07-17 09:54:54 +02002380lyd_insert_node(struct lyd_node *parent, struct lyd_node **first_sibling_p, struct lyd_node *node)
Michal Vasko90932a92020-02-12 14:33:03 +01002381{
Michal Vaskob104f112020-07-17 09:54:54 +02002382 struct lyd_node *anchor, *first_sibling;
Michal Vasko90932a92020-02-12 14:33:03 +01002383
Michal Vaskob104f112020-07-17 09:54:54 +02002384 /* inserting list without its keys is not supported */
2385 assert((parent || first_sibling_p) && node && (node->hash || !node->schema));
Michal Vaskof756c942020-11-06 17:21:37 +01002386 assert(!parent || !parent->schema ||
2387 (parent->schema->nodetype & (LYS_CONTAINER | LYS_LIST | LYS_RPC | LYS_ACTION | LYS_NOTIF)));
Michal Vasko9b368d32020-02-14 13:53:31 +01002388
Michal Vaskob104f112020-07-17 09:54:54 +02002389 if (!parent && first_sibling_p && (*first_sibling_p) && (*first_sibling_p)->parent) {
Michal Vasko9e685082021-01-29 14:49:09 +01002390 parent = lyd_parent(*first_sibling_p);
Michal Vasko9b368d32020-02-14 13:53:31 +01002391 }
Michal Vasko90932a92020-02-12 14:33:03 +01002392
Michal Vaskob104f112020-07-17 09:54:54 +02002393 /* get first sibling */
Michal Vasko9e685082021-01-29 14:49:09 +01002394 first_sibling = parent ? lyd_child(parent) : *first_sibling_p;
Michal Vasko9f96a052020-03-10 09:41:45 +01002395
Michal Vaskob104f112020-07-17 09:54:54 +02002396 /* find the anchor, our next node, so we can insert before it */
2397 anchor = lyd_insert_get_next_anchor(first_sibling, node);
2398 if (anchor) {
2399 lyd_insert_before_node(anchor, node);
Michal Vasko26123192020-11-09 21:02:34 +01002400 if (!parent && (*first_sibling_p == anchor)) {
2401 /* move first sibling */
2402 *first_sibling_p = node;
2403 }
Michal Vaskob104f112020-07-17 09:54:54 +02002404 } else if (first_sibling) {
2405 lyd_insert_after_node(first_sibling->prev, node);
2406 } else if (parent) {
2407 lyd_insert_only_child(parent, node);
Michal Vasko90932a92020-02-12 14:33:03 +01002408 } else {
Michal Vaskob104f112020-07-17 09:54:54 +02002409 *first_sibling_p = node;
2410 }
2411
2412 /* insert into parent HT */
2413 lyd_insert_hash(node);
2414
2415 /* finish hashes for our parent, if needed and possible */
Michal Vasko9e8de2d2020-09-01 08:17:10 +02002416 if (node->schema && (node->schema->flags & LYS_KEY) && parent && lyd_insert_has_keys(parent)) {
Michal Vaskob104f112020-07-17 09:54:54 +02002417 lyd_hash(parent);
2418
2419 /* now we can insert even the list into its parent HT */
2420 lyd_insert_hash(parent);
Michal Vasko90932a92020-02-12 14:33:03 +01002421 }
Michal Vasko90932a92020-02-12 14:33:03 +01002422}
2423
Michal Vasko717a4c32020-12-07 09:40:10 +01002424/**
2425 * @brief Check schema place of a node to be inserted.
2426 *
2427 * @param[in] parent Schema node of the parent data node.
2428 * @param[in] sibling Schema node of a sibling data node.
2429 * @param[in] schema Schema node if the data node to be inserted.
2430 * @return LY_SUCCESS on success.
2431 * @return LY_EINVAL if the place is invalid.
2432 */
Michal Vaskof03ed032020-03-04 13:31:44 +01002433static LY_ERR
Michal Vasko717a4c32020-12-07 09:40:10 +01002434lyd_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 +01002435{
2436 const struct lysc_node *par2;
2437
Michal Vasko62ed12d2020-05-21 10:08:25 +02002438 assert(!parent || !(parent->nodetype & (LYS_CASE | LYS_CHOICE)));
Michal Vasko717a4c32020-12-07 09:40:10 +01002439 assert(!sibling || !(sibling->nodetype & (LYS_CASE | LYS_CHOICE)));
2440 assert(!schema || !(schema->nodetype & (LYS_CASE | LYS_CHOICE)));
Michal Vaskof03ed032020-03-04 13:31:44 +01002441
Michal Vasko717a4c32020-12-07 09:40:10 +01002442 if (!schema || (!parent && !sibling)) {
Michal Vasko71e795e2020-12-04 16:27:37 +01002443 /* opaque nodes can be inserted wherever */
2444 return LY_SUCCESS;
2445 }
2446
Michal Vasko717a4c32020-12-07 09:40:10 +01002447 if (!parent) {
2448 parent = lysc_data_parent(sibling);
2449 }
2450
Michal Vaskof03ed032020-03-04 13:31:44 +01002451 /* find schema parent */
Michal Vasko62ed12d2020-05-21 10:08:25 +02002452 par2 = lysc_data_parent(schema);
Michal Vaskof03ed032020-03-04 13:31:44 +01002453
2454 if (parent) {
2455 /* inner node */
2456 if (par2 != parent) {
Michal Vaskob104f112020-07-17 09:54:54 +02002457 LOGERR(schema->module->ctx, LY_EINVAL, "Cannot insert, parent of \"%s\" is not \"%s\".", schema->name,
Michal Vasko69730152020-10-09 16:30:07 +02002458 parent->name);
Michal Vaskof03ed032020-03-04 13:31:44 +01002459 return LY_EINVAL;
2460 }
2461 } else {
2462 /* top-level node */
2463 if (par2) {
Radek Krejcif6d14cb2020-07-02 16:11:45 +02002464 LOGERR(schema->module->ctx, LY_EINVAL, "Cannot insert, node \"%s\" is not top-level.", schema->name);
Michal Vaskof03ed032020-03-04 13:31:44 +01002465 return LY_EINVAL;
2466 }
2467 }
2468
2469 return LY_SUCCESS;
2470}
2471
2472API LY_ERR
Michal Vaskob104f112020-07-17 09:54:54 +02002473lyd_insert_child(struct lyd_node *parent, struct lyd_node *node)
Michal Vaskof03ed032020-03-04 13:31:44 +01002474{
2475 struct lyd_node *iter;
2476
Michal Vasko0ab974d2021-02-24 13:18:26 +01002477 LY_CHECK_ARG_RET(NULL, parent, node, !parent->schema || (parent->schema->nodetype & LYD_NODE_INNER), LY_EINVAL);
Michal Vaskof03ed032020-03-04 13:31:44 +01002478
Michal Vasko717a4c32020-12-07 09:40:10 +01002479 LY_CHECK_RET(lyd_insert_check_schema(parent->schema, NULL, node->schema));
Michal Vaskof03ed032020-03-04 13:31:44 +01002480
Michal Vasko0ab974d2021-02-24 13:18:26 +01002481 if (node->schema && (node->schema->flags & LYS_KEY)) {
Michal Vaskof03ed032020-03-04 13:31:44 +01002482 LOGERR(parent->schema->module->ctx, LY_EINVAL, "Cannot insert key \"%s\".", node->schema->name);
2483 return LY_EINVAL;
2484 }
2485
2486 if (node->parent || node->prev->next) {
2487 lyd_unlink_tree(node);
2488 }
2489
2490 while (node) {
2491 iter = node->next;
2492 lyd_unlink_tree(node);
2493 lyd_insert_node(parent, NULL, node);
2494 node = iter;
2495 }
2496 return LY_SUCCESS;
2497}
2498
2499API LY_ERR
Michal Vaskob104f112020-07-17 09:54:54 +02002500lyd_insert_sibling(struct lyd_node *sibling, struct lyd_node *node, struct lyd_node **first)
Michal Vaskob1b5c262020-03-05 14:29:47 +01002501{
2502 struct lyd_node *iter;
2503
Michal Vaskob104f112020-07-17 09:54:54 +02002504 LY_CHECK_ARG_RET(NULL, node, LY_EINVAL);
Michal Vaskob1b5c262020-03-05 14:29:47 +01002505
Michal Vaskob104f112020-07-17 09:54:54 +02002506 if (sibling) {
Michal Vasko717a4c32020-12-07 09:40:10 +01002507 LY_CHECK_RET(lyd_insert_check_schema(NULL, sibling->schema, node->schema));
Michal Vaskob1b5c262020-03-05 14:29:47 +01002508 }
2509
Michal Vasko553d0b52020-12-04 16:27:52 +01002510 if (sibling == node) {
2511 /* we need to keep the connection to siblings so we can insert into them */
2512 sibling = sibling->prev;
2513 }
2514
Michal Vaskob1b5c262020-03-05 14:29:47 +01002515 if (node->parent || node->prev->next) {
2516 lyd_unlink_tree(node);
2517 }
2518
2519 while (node) {
Michal Vasko71e795e2020-12-04 16:27:37 +01002520 if (lysc_is_key(node->schema)) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02002521 LOGERR(LYD_CTX(node), LY_EINVAL, "Cannot insert key \"%s\".", node->schema->name);
Michal Vaskob104f112020-07-17 09:54:54 +02002522 return LY_EINVAL;
2523 }
2524
Michal Vaskob1b5c262020-03-05 14:29:47 +01002525 iter = node->next;
2526 lyd_unlink_tree(node);
2527 lyd_insert_node(NULL, &sibling, node);
2528 node = iter;
2529 }
Michal Vaskob1b5c262020-03-05 14:29:47 +01002530
Michal Vaskob104f112020-07-17 09:54:54 +02002531 if (first) {
2532 /* find the first sibling */
2533 *first = sibling;
2534 while ((*first)->prev->next) {
2535 *first = (*first)->prev;
Michal Vasko0249f7c2020-03-05 16:36:40 +01002536 }
2537 }
2538
2539 return LY_SUCCESS;
2540}
2541
Michal Vaskob1b5c262020-03-05 14:29:47 +01002542API LY_ERR
Michal Vaskof03ed032020-03-04 13:31:44 +01002543lyd_insert_before(struct lyd_node *sibling, struct lyd_node *node)
2544{
Michal Vaskof03ed032020-03-04 13:31:44 +01002545 LY_CHECK_ARG_RET(NULL, sibling, node, LY_EINVAL);
2546
Michal Vasko717a4c32020-12-07 09:40:10 +01002547 LY_CHECK_RET(lyd_insert_check_schema(NULL, sibling->schema, node->schema));
Michal Vaskof03ed032020-03-04 13:31:44 +01002548
Michal Vaskob104f112020-07-17 09:54:54 +02002549 if (!(node->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) || !(node->schema->flags & LYS_ORDBY_USER)) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02002550 LOGERR(LYD_CTX(sibling), LY_EINVAL, "Can be used only for user-ordered nodes.");
Michal Vaskof03ed032020-03-04 13:31:44 +01002551 return LY_EINVAL;
2552 }
2553
Michal Vasko4bc2ce32020-12-08 10:06:16 +01002554 lyd_unlink_tree(node);
2555 lyd_insert_before_node(sibling, node);
2556 lyd_insert_hash(node);
Michal Vaskof03ed032020-03-04 13:31:44 +01002557
Michal Vaskof03ed032020-03-04 13:31:44 +01002558 return LY_SUCCESS;
2559}
2560
2561API LY_ERR
2562lyd_insert_after(struct lyd_node *sibling, struct lyd_node *node)
2563{
Michal Vaskof03ed032020-03-04 13:31:44 +01002564 LY_CHECK_ARG_RET(NULL, sibling, node, LY_EINVAL);
2565
Michal Vasko717a4c32020-12-07 09:40:10 +01002566 LY_CHECK_RET(lyd_insert_check_schema(NULL, sibling->schema, node->schema));
Michal Vaskof03ed032020-03-04 13:31:44 +01002567
Michal Vaskob104f112020-07-17 09:54:54 +02002568 if (!(node->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) || !(node->schema->flags & LYS_ORDBY_USER)) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02002569 LOGERR(LYD_CTX(sibling), LY_EINVAL, "Can be used only for user-ordered nodes.");
Michal Vaskof03ed032020-03-04 13:31:44 +01002570 return LY_EINVAL;
2571 }
2572
Michal Vasko4bc2ce32020-12-08 10:06:16 +01002573 lyd_unlink_tree(node);
2574 lyd_insert_after_node(sibling, node);
2575 lyd_insert_hash(node);
Michal Vaskof03ed032020-03-04 13:31:44 +01002576
Michal Vaskof03ed032020-03-04 13:31:44 +01002577 return LY_SUCCESS;
2578}
2579
2580API void
2581lyd_unlink_tree(struct lyd_node *node)
2582{
2583 struct lyd_node *iter;
2584
2585 if (!node) {
2586 return;
2587 }
2588
Michal Vaskob104f112020-07-17 09:54:54 +02002589 /* update hashes while still linked into the tree */
2590 lyd_unlink_hash(node);
2591
Michal Vaskof03ed032020-03-04 13:31:44 +01002592 /* unlink from siblings */
2593 if (node->prev->next) {
2594 node->prev->next = node->next;
2595 }
2596 if (node->next) {
2597 node->next->prev = node->prev;
2598 } else {
2599 /* unlinking the last node */
2600 if (node->parent) {
2601 iter = node->parent->child;
2602 } else {
2603 iter = node->prev;
2604 while (iter->prev != node) {
2605 iter = iter->prev;
2606 }
2607 }
2608 /* update the "last" pointer from the first node */
2609 iter->prev = node->prev;
2610 }
2611
2612 /* unlink from parent */
2613 if (node->parent) {
2614 if (node->parent->child == node) {
2615 /* the node is the first child */
2616 node->parent->child = node->next;
2617 }
2618
Michal Vaskoab49dbe2020-07-17 12:32:47 +02002619 /* check for NP container whether its last non-default node is not being unlinked */
Michal Vasko69730152020-10-09 16:30:07 +02002620 if (node->parent->schema && (node->parent->schema->nodetype == LYS_CONTAINER) &&
2621 !(node->parent->flags & LYD_DEFAULT) && !(node->parent->schema->flags & LYS_PRESENCE)) {
Michal Vaskoab49dbe2020-07-17 12:32:47 +02002622 LY_LIST_FOR(node->parent->child, iter) {
2623 if ((iter != node) && !(iter->flags & LYD_DEFAULT)) {
2624 break;
2625 }
2626 }
2627 if (!iter) {
2628 node->parent->flags |= LYD_DEFAULT;
2629 }
2630 }
2631
Michal Vaskof03ed032020-03-04 13:31:44 +01002632 node->parent = NULL;
2633 }
2634
2635 node->next = NULL;
2636 node->prev = node;
2637}
2638
Michal Vaskoa5da3292020-08-12 13:10:50 +02002639void
Michal Vasko871a0252020-11-11 18:35:24 +01002640lyd_insert_meta(struct lyd_node *parent, struct lyd_meta *meta, ly_bool clear_dflt)
Radek Krejci1798aae2020-07-14 13:26:06 +02002641{
2642 struct lyd_meta *last, *iter;
2643
2644 assert(parent);
Michal Vaskoa5da3292020-08-12 13:10:50 +02002645
2646 if (!meta) {
2647 return;
2648 }
Radek Krejci1798aae2020-07-14 13:26:06 +02002649
2650 for (iter = meta; iter; iter = iter->next) {
2651 iter->parent = parent;
2652 }
2653
2654 /* insert as the last attribute */
2655 if (parent->meta) {
Radek Krejci1e008d22020-08-17 11:37:37 +02002656 for (last = parent->meta; last->next; last = last->next) {}
Radek Krejci1798aae2020-07-14 13:26:06 +02002657 last->next = meta;
2658 } else {
2659 parent->meta = meta;
2660 }
2661
2662 /* remove default flags from NP containers */
Michal Vasko871a0252020-11-11 18:35:24 +01002663 while (clear_dflt && parent && (parent->schema->nodetype == LYS_CONTAINER) && (parent->flags & LYD_DEFAULT)) {
Radek Krejci1798aae2020-07-14 13:26:06 +02002664 parent->flags &= ~LYD_DEFAULT;
Michal Vasko9e685082021-01-29 14:49:09 +01002665 parent = lyd_parent(parent);
Radek Krejci1798aae2020-07-14 13:26:06 +02002666 }
Radek Krejci1798aae2020-07-14 13:26:06 +02002667}
2668
2669LY_ERR
Michal Vasko9f96a052020-03-10 09:41:45 +01002670lyd_create_meta(struct lyd_node *parent, struct lyd_meta **meta, const struct lys_module *mod, const char *name,
Radek Krejci8df109d2021-04-23 12:19:08 +02002671 size_t name_len, const char *value, size_t value_len, ly_bool *dynamic, LY_VALUE_FORMAT format,
Michal Vasko871a0252020-11-11 18:35:24 +01002672 void *prefix_data, uint32_t hints, ly_bool clear_dflt, ly_bool *incomplete)
Michal Vasko90932a92020-02-12 14:33:03 +01002673{
Radek Krejci2efc45b2020-12-22 16:25:44 +01002674 LY_ERR ret = LY_SUCCESS;
Michal Vasko90932a92020-02-12 14:33:03 +01002675 struct lysc_ext_instance *ant = NULL;
Michal Vasko9f96a052020-03-10 09:41:45 +01002676 struct lyd_meta *mt, *last;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002677 LY_ARRAY_COUNT_TYPE u;
Michal Vasko90932a92020-02-12 14:33:03 +01002678
Michal Vasko9f96a052020-03-10 09:41:45 +01002679 assert((parent || meta) && mod);
Michal Vasko6f4cbb62020-02-28 11:15:47 +01002680
Radek Krejciddace2c2021-01-08 11:30:56 +01002681 LOG_LOCSET(parent ? parent->schema : NULL, parent, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +01002682
Radek Krejci7eb54ba2020-05-18 16:30:04 +02002683 LY_ARRAY_FOR(mod->compiled->exts, u) {
Radek Krejci3e6632f2021-03-22 22:08:21 +01002684 if ((mod->compiled->exts[u].def->plugin == lyplg_find(LYPLG_EXTENSION, LYEXT_PLUGIN_INTERNAL_ANNOTATION)) &&
Radek Krejci7eb54ba2020-05-18 16:30:04 +02002685 !ly_strncmp(mod->compiled->exts[u].argument, name, name_len)) {
Michal Vasko90932a92020-02-12 14:33:03 +01002686 /* we have the annotation definition */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02002687 ant = &mod->compiled->exts[u];
Michal Vasko90932a92020-02-12 14:33:03 +01002688 break;
2689 }
2690 }
2691 if (!ant) {
2692 /* attribute is not defined as a metadata annotation (RFC 7952) */
Radek Krejci2efc45b2020-12-22 16:25:44 +01002693 LOGVAL(mod->ctx, LYVE_REFERENCE, "Annotation definition for attribute \"%s:%.*s\" not found.",
Radek Krejci422afb12021-03-04 16:38:16 +01002694 mod->name, (int)name_len, name);
Radek Krejci2efc45b2020-12-22 16:25:44 +01002695 ret = LY_EINVAL;
2696 goto cleanup;
Michal Vasko90932a92020-02-12 14:33:03 +01002697 }
2698
Michal Vasko9f96a052020-03-10 09:41:45 +01002699 mt = calloc(1, sizeof *mt);
Radek Krejci2efc45b2020-12-22 16:25:44 +01002700 LY_CHECK_ERR_GOTO(!mt, LOGMEM(mod->ctx); ret = LY_EMEM, cleanup);
Michal Vasko9f96a052020-03-10 09:41:45 +01002701 mt->parent = parent;
2702 mt->annotation = ant;
Radek Krejci1b2eef82021-02-17 11:17:27 +01002703 ret = lyd_value_store(mod->ctx, &mt->value, *(const struct lysc_type **)ant->substmts[ANNOTATION_SUBSTMT_TYPE].storage,
2704 value, value_len, dynamic, format, prefix_data, hints, parent ? parent->schema : NULL, incomplete);
Radek Krejci2efc45b2020-12-22 16:25:44 +01002705 LY_CHECK_ERR_GOTO(ret, free(mt), cleanup);
2706 ret = lydict_insert(mod->ctx, name, name_len, &mt->name);
2707 LY_CHECK_ERR_GOTO(ret, free(mt), cleanup);
Michal Vasko90932a92020-02-12 14:33:03 +01002708
Michal Vasko6f4cbb62020-02-28 11:15:47 +01002709 /* insert as the last attribute */
2710 if (parent) {
Michal Vasko871a0252020-11-11 18:35:24 +01002711 lyd_insert_meta(parent, mt, clear_dflt);
Michal Vasko9f96a052020-03-10 09:41:45 +01002712 } else if (*meta) {
Radek Krejci1e008d22020-08-17 11:37:37 +02002713 for (last = *meta; last->next; last = last->next) {}
Michal Vasko9f96a052020-03-10 09:41:45 +01002714 last->next = mt;
Michal Vasko90932a92020-02-12 14:33:03 +01002715 }
2716
Michal Vasko9f96a052020-03-10 09:41:45 +01002717 if (meta) {
2718 *meta = mt;
Michal Vasko90932a92020-02-12 14:33:03 +01002719 }
Radek Krejci2efc45b2020-12-22 16:25:44 +01002720
2721cleanup:
Radek Krejciddace2c2021-01-08 11:30:56 +01002722 LOG_LOCBACK((parent && parent->schema) ? 1 : 0, parent ? 1 : 0, 0, 0);
Radek Krejci2efc45b2020-12-22 16:25:44 +01002723 return ret;
Michal Vasko90932a92020-02-12 14:33:03 +01002724}
2725
Michal Vaskoa5da3292020-08-12 13:10:50 +02002726void
2727lyd_insert_attr(struct lyd_node *parent, struct lyd_attr *attr)
2728{
2729 struct lyd_attr *last, *iter;
2730 struct lyd_node_opaq *opaq;
2731
2732 assert(parent && !parent->schema);
2733
2734 if (!attr) {
2735 return;
2736 }
2737
2738 opaq = (struct lyd_node_opaq *)parent;
2739 for (iter = attr; iter; iter = iter->next) {
2740 iter->parent = opaq;
2741 }
2742
2743 /* insert as the last attribute */
2744 if (opaq->attr) {
Radek Krejci1e008d22020-08-17 11:37:37 +02002745 for (last = opaq->attr; last->next; last = last->next) {}
Michal Vaskoa5da3292020-08-12 13:10:50 +02002746 last->next = attr;
2747 } else {
2748 opaq->attr = attr;
2749 }
2750}
2751
Michal Vasko52927e22020-03-16 17:26:14 +01002752LY_ERR
Michal Vaskofeca4fb2020-10-05 08:58:40 +02002753lyd_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 +01002754 const char *prefix, size_t prefix_len, const char *module_key, size_t module_key_len, const char *value,
Radek Krejci8df109d2021-04-23 12:19:08 +02002755 size_t value_len, ly_bool *dynamic, LY_VALUE_FORMAT format, void *val_prefix_data, uint32_t hints)
Michal Vasko52927e22020-03-16 17:26:14 +01002756{
Radek Krejci011e4aa2020-09-04 15:22:31 +02002757 LY_ERR ret = LY_SUCCESS;
Radek Krejci1798aae2020-07-14 13:26:06 +02002758 struct lyd_attr *at, *last;
Michal Vasko52927e22020-03-16 17:26:14 +01002759
2760 assert(ctx && (parent || attr) && (!parent || !parent->schema));
Michal Vaskofeca4fb2020-10-05 08:58:40 +02002761 assert(name && name_len && format);
Michal Vasko52927e22020-03-16 17:26:14 +01002762
2763 if (!value_len) {
2764 value = "";
2765 }
2766
2767 at = calloc(1, sizeof *at);
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01002768 LY_CHECK_ERR_RET(!at, LOGMEM(ctx); ly_free_prefix_data(format, val_prefix_data), LY_EMEM);
Radek Krejcid46e46a2020-09-15 14:22:42 +02002769
Michal Vaskoad92b672020-11-12 13:11:31 +01002770 LY_CHECK_GOTO(ret = lydict_insert(ctx, name, name_len, &at->name.name), finish);
Michal Vasko501af032020-11-11 20:27:44 +01002771 if (prefix_len) {
Michal Vaskoad92b672020-11-12 13:11:31 +01002772 LY_CHECK_GOTO(ret = lydict_insert(ctx, prefix, prefix_len, &at->name.prefix), finish);
Michal Vasko501af032020-11-11 20:27:44 +01002773 }
2774 if (module_key_len) {
Michal Vaskoad92b672020-11-12 13:11:31 +01002775 LY_CHECK_GOTO(ret = lydict_insert(ctx, module_key, module_key_len, &at->name.module_ns), finish);
Michal Vasko501af032020-11-11 20:27:44 +01002776 }
2777
Michal Vasko52927e22020-03-16 17:26:14 +01002778 if (dynamic && *dynamic) {
Radek Krejci011e4aa2020-09-04 15:22:31 +02002779 ret = lydict_insert_zc(ctx, (char *)value, &at->value);
2780 LY_CHECK_GOTO(ret, finish);
Michal Vasko52927e22020-03-16 17:26:14 +01002781 *dynamic = 0;
2782 } else {
Radek Krejci011e4aa2020-09-04 15:22:31 +02002783 LY_CHECK_GOTO(ret = lydict_insert(ctx, value, value_len, &at->value), finish);
Michal Vasko52927e22020-03-16 17:26:14 +01002784 }
Michal Vasko501af032020-11-11 20:27:44 +01002785 at->format = format;
2786 at->val_prefix_data = val_prefix_data;
2787 at->hints = hints;
Michal Vasko52927e22020-03-16 17:26:14 +01002788
2789 /* insert as the last attribute */
2790 if (parent) {
Michal Vaskoa5da3292020-08-12 13:10:50 +02002791 lyd_insert_attr(parent, at);
Michal Vasko52927e22020-03-16 17:26:14 +01002792 } else if (*attr) {
Radek Krejci1e008d22020-08-17 11:37:37 +02002793 for (last = *attr; last->next; last = last->next) {}
Michal Vasko52927e22020-03-16 17:26:14 +01002794 last->next = at;
2795 }
2796
Radek Krejci011e4aa2020-09-04 15:22:31 +02002797finish:
2798 if (ret) {
2799 lyd_free_attr_single(ctx, at);
2800 } else if (attr) {
Michal Vasko52927e22020-03-16 17:26:14 +01002801 *attr = at;
2802 }
2803 return LY_SUCCESS;
2804}
2805
Radek Krejci084289f2019-07-09 17:35:30 +02002806API const struct lyd_node_term *
Michal Vasko004d3152020-06-11 19:59:22 +02002807lyd_target(const struct ly_path *path, const struct lyd_node *tree)
Radek Krejci084289f2019-07-09 17:35:30 +02002808{
Michal Vasko004d3152020-06-11 19:59:22 +02002809 struct lyd_node *target;
Radek Krejci084289f2019-07-09 17:35:30 +02002810
Michal Vasko004d3152020-06-11 19:59:22 +02002811 if (ly_path_eval(path, tree, &target)) {
2812 return NULL;
Radek Krejci084289f2019-07-09 17:35:30 +02002813 }
2814
Michal Vasko004d3152020-06-11 19:59:22 +02002815 return (struct lyd_node_term *)target;
Radek Krejci084289f2019-07-09 17:35:30 +02002816}
2817
aPiecek2f63f952021-03-30 12:22:18 +02002818/**
2819 * @brief Check the equality of the two schemas from different contexts.
2820 *
2821 * @param schema1 of first node.
2822 * @param schema2 of second node.
2823 * @return 1 if the schemas are equal otherwise 0.
2824 */
2825static ly_bool
2826lyd_compare_schema_equal(const struct lysc_node *schema1, const struct lysc_node *schema2)
2827{
2828 if (!schema1 && !schema2) {
2829 return 1;
2830 } else if (!schema1 || !schema2) {
2831 return 0;
2832 }
2833
2834 assert(schema1->module->ctx != schema2->module->ctx);
2835
2836 if (schema1->nodetype != schema2->nodetype) {
2837 return 0;
2838 }
2839
2840 if (strcmp(schema1->name, schema2->name)) {
2841 return 0;
2842 }
2843
2844 if (strcmp(schema1->module->name, schema2->module->name)) {
2845 return 0;
2846 }
2847
2848 if (schema1->module->revision || schema2->module->revision) {
2849 if (!schema1->module->revision || !schema2->module->revision) {
2850 return 0;
2851 }
2852 if (strcmp(schema1->module->revision, schema2->module->revision)) {
2853 return 0;
2854 }
2855 }
2856
2857 return 1;
2858}
2859
2860/**
2861 * @brief Check the equality of the schemas for all parent nodes.
2862 *
2863 * Both nodes must be from different contexts.
2864 *
2865 * @param node1 Data of first node.
2866 * @param node2 Data of second node.
2867 * @return 1 if the all related parental schemas are equal otherwise 0.
2868 */
2869static ly_bool
2870lyd_compare_schema_parents_equal(const struct lyd_node *node1, const struct lyd_node *node2)
2871{
2872 const struct lysc_node *parent1, *parent2;
2873
2874 assert(node1 && node2);
2875
2876 for (parent1 = node1->schema->parent, parent2 = node2->schema->parent;
2877 parent1 && parent2;
2878 parent1 = parent1->parent, parent2 = parent2->parent) {
2879 if (!lyd_compare_schema_equal(parent1, parent2)) {
2880 return 0;
2881 }
2882 }
2883
2884 if (parent1 || parent2) {
2885 return 0;
2886 }
2887
2888 return 1;
2889}
2890
2891/**
2892 * @brief Internal implementation of @ref lyd_compare_single.
2893 * @copydoc lyd_compare_single
2894 * @param[in] parental_schemas_checked Flag used for optimization.
2895 * When this function is called for the first time, the flag must be set to 0.
2896 * The @ref lyd_compare_schema_parents_equal should be called only once during
2897 * recursive calls, and this is accomplished by setting to 1 in the lyd_compare_single_ body.
2898 */
2899static LY_ERR
2900lyd_compare_single_(const struct lyd_node *node1, const struct lyd_node *node2,
2901 uint32_t options, ly_bool parental_schemas_checked)
Radek Krejci1f05b6a2019-07-18 16:15:06 +02002902{
2903 const struct lyd_node *iter1, *iter2;
2904 struct lyd_node_term *term1, *term2;
2905 struct lyd_node_any *any1, *any2;
Michal Vasko52927e22020-03-16 17:26:14 +01002906 struct lyd_node_opaq *opaq1, *opaq2;
Radek Krejci1f05b6a2019-07-18 16:15:06 +02002907 size_t len1, len2;
Radek Krejci084289f2019-07-09 17:35:30 +02002908
Radek Krejci1f05b6a2019-07-18 16:15:06 +02002909 if (!node1 || !node2) {
2910 if (node1 == node2) {
2911 return LY_SUCCESS;
2912 } else {
2913 return LY_ENOT;
2914 }
2915 }
2916
aPiecek2f63f952021-03-30 12:22:18 +02002917 if (LYD_CTX(node1) == LYD_CTX(node2)) {
2918 /* same contexts */
2919 if (node1->schema != node2->schema) {
2920 return LY_ENOT;
2921 }
2922 } else {
2923 /* different contexts */
2924 if (!lyd_compare_schema_equal(node1->schema, node2->schema)) {
2925 return LY_ENOT;
2926 }
2927 if (!parental_schemas_checked) {
2928 if (!lyd_compare_schema_parents_equal(node1, node2)) {
2929 return LY_ENOT;
2930 }
2931 parental_schemas_checked = 1;
2932 }
Radek Krejci1f05b6a2019-07-18 16:15:06 +02002933 }
2934
2935 if (node1->hash != node2->hash) {
2936 return LY_ENOT;
2937 }
aPiecek2f63f952021-03-30 12:22:18 +02002938 /* equal hashes do not mean equal nodes, they can be just in collision so the nodes must be checked explicitly */
Radek Krejci1f05b6a2019-07-18 16:15:06 +02002939
Michal Vasko52927e22020-03-16 17:26:14 +01002940 if (!node1->schema) {
2941 opaq1 = (struct lyd_node_opaq *)node1;
2942 opaq2 = (struct lyd_node_opaq *)node2;
aPiecek2f63f952021-03-30 12:22:18 +02002943 if ((strcmp(opaq1->name.name, opaq2->name.name)) || (opaq1->format != opaq2->format) ||
2944 (strcmp(opaq1->name.module_ns, opaq2->name.module_ns))) {
Radek Krejci1f05b6a2019-07-18 16:15:06 +02002945 return LY_ENOT;
2946 }
Michal Vasko52927e22020-03-16 17:26:14 +01002947 switch (opaq1->format) {
Radek Krejci8df109d2021-04-23 12:19:08 +02002948 case LY_VALUE_XML:
aPiecek2f63f952021-03-30 12:22:18 +02002949 if (lyxml_value_compare(LYD_CTX(node1), opaq1->value, opaq1->val_prefix_data, LYD_CTX(node2), opaq2->value, opaq2->val_prefix_data)) {
Michal Vasko52927e22020-03-16 17:26:14 +01002950 return LY_ENOT;
2951 }
2952 break;
Radek Krejci8df109d2021-04-23 12:19:08 +02002953 case LY_VALUE_JSON:
Radek Krejci1798aae2020-07-14 13:26:06 +02002954 /* prefixes in JSON are unique, so it is not necessary to canonize the values */
2955 if (strcmp(opaq1->value, opaq2->value)) {
2956 return LY_ENOT;
2957 }
2958 break;
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01002959 default:
Michal Vasko52927e22020-03-16 17:26:14 +01002960 /* not allowed */
Michal Vaskob7be7a82020-08-20 09:09:04 +02002961 LOGINT(LYD_CTX(node1));
Michal Vasko52927e22020-03-16 17:26:14 +01002962 return LY_EINT;
2963 }
2964 if (options & LYD_COMPARE_FULL_RECURSION) {
2965 iter1 = opaq1->child;
2966 iter2 = opaq2->child;
Radek Krejci1f05b6a2019-07-18 16:15:06 +02002967 goto all_children_compare;
Michal Vasko52927e22020-03-16 17:26:14 +01002968 }
2969 return LY_SUCCESS;
2970 } else {
2971 switch (node1->schema->nodetype) {
2972 case LYS_LEAF:
2973 case LYS_LEAFLIST:
2974 if (options & LYD_COMPARE_DEFAULTS) {
2975 if ((node1->flags & LYD_DEFAULT) != (node2->flags & LYD_DEFAULT)) {
2976 return LY_ENOT;
2977 }
2978 }
2979
Michal Vasko1a2bf2e2020-06-16 13:07:48 +02002980 term1 = (struct lyd_node_term *)node1;
2981 term2 = (struct lyd_node_term *)node2;
aPiecek2f63f952021-03-30 12:22:18 +02002982
2983 /* same contexts */
2984 if (LYD_CTX(node1) == LYD_CTX(node2)) {
2985 return term1->value.realtype->plugin->compare(&term1->value, &term2->value);
2986 }
2987
2988 /* different contexts */
2989 if (strcmp(LYD_CANON_VALUE(node1), LYD_CANON_VALUE(node2))) {
2990 return LY_ENOT;
2991 }
2992 return LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +01002993 case LYS_CONTAINER:
2994 if (options & LYD_COMPARE_DEFAULTS) {
2995 if ((node1->flags & LYD_DEFAULT) != (node2->flags & LYD_DEFAULT)) {
2996 return LY_ENOT;
2997 }
2998 }
2999 if (options & LYD_COMPARE_FULL_RECURSION) {
Michal Vasko9e685082021-01-29 14:49:09 +01003000 iter1 = lyd_child(node1);
3001 iter2 = lyd_child(node2);
Michal Vasko52927e22020-03-16 17:26:14 +01003002 goto all_children_compare;
Radek Krejci1f05b6a2019-07-18 16:15:06 +02003003 }
3004 return LY_SUCCESS;
Michal Vasko1bf09392020-03-27 12:38:10 +01003005 case LYS_RPC:
Michal Vasko52927e22020-03-16 17:26:14 +01003006 case LYS_ACTION:
3007 if (options & LYD_COMPARE_FULL_RECURSION) {
3008 /* TODO action/RPC
3009 goto all_children_compare;
3010 */
Radek Krejci1f05b6a2019-07-18 16:15:06 +02003011 }
3012 return LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +01003013 case LYS_NOTIF:
3014 if (options & LYD_COMPARE_FULL_RECURSION) {
3015 /* TODO Notification
3016 goto all_children_compare;
3017 */
3018 }
3019 return LY_SUCCESS;
3020 case LYS_LIST:
Michal Vasko9e685082021-01-29 14:49:09 +01003021 iter1 = lyd_child(node1);
3022 iter2 = lyd_child(node2);
Michal Vasko52927e22020-03-16 17:26:14 +01003023
3024 if (!(node1->schema->flags & LYS_KEYLESS) && !(options & LYD_COMPARE_FULL_RECURSION)) {
3025 /* lists with keys, their equivalence is based on their keys */
Michal Vasko544e58a2021-01-28 14:33:41 +01003026 for (const struct lysc_node *key = lysc_node_child(node1->schema);
Michal Vaskoc57d9a92020-06-23 13:28:27 +02003027 key && (key->flags & LYS_KEY);
Michal Vasko52927e22020-03-16 17:26:14 +01003028 key = key->next) {
aPiecek2f63f952021-03-30 12:22:18 +02003029 if (lyd_compare_single_(iter1, iter2, options, parental_schemas_checked)) {
Michal Vasko52927e22020-03-16 17:26:14 +01003030 return LY_ENOT;
3031 }
3032 iter1 = iter1->next;
3033 iter2 = iter2->next;
3034 }
3035 } else {
3036 /* lists without keys, their equivalence is based on equivalence of all the children (both direct and indirect) */
3037
Radek Krejci0f969882020-08-21 16:56:47 +02003038all_children_compare:
Michal Vasko52927e22020-03-16 17:26:14 +01003039 if (!iter1 && !iter2) {
3040 /* no children, nothing to compare */
3041 return LY_SUCCESS;
3042 }
3043
Michal Vaskod989ba02020-08-24 10:59:24 +02003044 for ( ; iter1 && iter2; iter1 = iter1->next, iter2 = iter2->next) {
aPiecek2f63f952021-03-30 12:22:18 +02003045 if (lyd_compare_single_(iter1, iter2, options | LYD_COMPARE_FULL_RECURSION, parental_schemas_checked)) {
Michal Vasko52927e22020-03-16 17:26:14 +01003046 return LY_ENOT;
3047 }
3048 }
3049 if (iter1 || iter2) {
3050 return LY_ENOT;
3051 }
3052 }
3053 return LY_SUCCESS;
3054 case LYS_ANYXML:
3055 case LYS_ANYDATA:
Michal Vasko22df3f02020-08-24 13:29:22 +02003056 any1 = (struct lyd_node_any *)node1;
3057 any2 = (struct lyd_node_any *)node2;
Michal Vasko52927e22020-03-16 17:26:14 +01003058
3059 if (any1->value_type != any2->value_type) {
3060 return LY_ENOT;
3061 }
3062 switch (any1->value_type) {
3063 case LYD_ANYDATA_DATATREE:
3064 iter1 = any1->value.tree;
3065 iter2 = any2->value.tree;
3066 goto all_children_compare;
3067 case LYD_ANYDATA_STRING:
3068 case LYD_ANYDATA_XML:
3069 case LYD_ANYDATA_JSON:
3070 len1 = strlen(any1->value.str);
3071 len2 = strlen(any2->value.str);
Michal Vasko69730152020-10-09 16:30:07 +02003072 if ((len1 != len2) || strcmp(any1->value.str, any2->value.str)) {
Michal Vasko52927e22020-03-16 17:26:14 +01003073 return LY_ENOT;
3074 }
3075 return LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +01003076 case LYD_ANYDATA_LYB:
Michal Vasko60ea6352020-06-29 13:39:39 +02003077 len1 = lyd_lyb_data_length(any1->value.mem);
3078 len2 = lyd_lyb_data_length(any2->value.mem);
Michal Vasko69730152020-10-09 16:30:07 +02003079 if ((len1 != len2) || memcmp(any1->value.mem, any2->value.mem, len1)) {
Michal Vasko52927e22020-03-16 17:26:14 +01003080 return LY_ENOT;
3081 }
3082 return LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +01003083 }
Radek Krejci1f05b6a2019-07-18 16:15:06 +02003084 }
3085 }
3086
Michal Vaskob7be7a82020-08-20 09:09:04 +02003087 LOGINT(LYD_CTX(node1));
Radek Krejci1f05b6a2019-07-18 16:15:06 +02003088 return LY_EINT;
3089}
Radek Krejci22ebdba2019-07-25 13:59:43 +02003090
Michal Vasko21725742020-06-29 11:49:25 +02003091API LY_ERR
aPiecek2f63f952021-03-30 12:22:18 +02003092lyd_compare_single(const struct lyd_node *node1, const struct lyd_node *node2, uint32_t options)
3093{
3094 return lyd_compare_single_(node1, node2, options, 0);
3095}
3096
3097API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003098lyd_compare_siblings(const struct lyd_node *node1, const struct lyd_node *node2, uint32_t options)
Michal Vasko8f359bf2020-07-28 10:41:15 +02003099{
Michal Vaskod989ba02020-08-24 10:59:24 +02003100 for ( ; node1 && node2; node1 = node1->next, node2 = node2->next) {
Michal Vasko8f359bf2020-07-28 10:41:15 +02003101 LY_CHECK_RET(lyd_compare_single(node1, node2, options));
3102 }
3103
Michal Vasko11a81432020-07-28 16:31:29 +02003104 if (node1 == node2) {
3105 return LY_SUCCESS;
Michal Vasko8f359bf2020-07-28 10:41:15 +02003106 }
Michal Vasko11a81432020-07-28 16:31:29 +02003107 return LY_ENOT;
Michal Vasko8f359bf2020-07-28 10:41:15 +02003108}
3109
3110API LY_ERR
Michal Vasko21725742020-06-29 11:49:25 +02003111lyd_compare_meta(const struct lyd_meta *meta1, const struct lyd_meta *meta2)
3112{
3113 if (!meta1 || !meta2) {
3114 if (meta1 == meta2) {
3115 return LY_SUCCESS;
3116 } else {
3117 return LY_ENOT;
3118 }
3119 }
3120
Michal Vaskoa8083062020-11-06 17:22:10 +01003121 if ((meta1->annotation->module->ctx != meta2->annotation->module->ctx) || (meta1->annotation != meta2->annotation)) {
Michal Vasko21725742020-06-29 11:49:25 +02003122 return LY_ENOT;
3123 }
3124
Michal Vasko21725742020-06-29 11:49:25 +02003125 return meta1->value.realtype->plugin->compare(&meta1->value, &meta2->value);
3126}
3127
Radek Krejci22ebdba2019-07-25 13:59:43 +02003128/**
Michal Vasko52927e22020-03-16 17:26:14 +01003129 * @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 +02003130 *
3131 * 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 +02003132 *
3133 * @param[in] node Original node to duplicate
3134 * @param[in] parent Parent to insert into, NULL for top-level sibling.
3135 * @param[in,out] first First sibling, NULL if no top-level sibling exist yet. Can be also NULL if @p parent is set.
3136 * @param[in] options Bitmask of options flags, see @ref dupoptions.
3137 * @param[out] dup_p Pointer where the created duplicated node is placed (besides connecting it int @p parent / @p first sibling).
3138 * @return LY_ERR value
Radek Krejci22ebdba2019-07-25 13:59:43 +02003139 */
Michal Vasko52927e22020-03-16 17:26:14 +01003140static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003141lyd_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 +02003142 struct lyd_node **dup_p)
Radek Krejci22ebdba2019-07-25 13:59:43 +02003143{
Michal Vasko52927e22020-03-16 17:26:14 +01003144 LY_ERR ret;
Radek Krejci22ebdba2019-07-25 13:59:43 +02003145 struct lyd_node *dup = NULL;
Michal Vasko61551fa2020-07-09 15:45:45 +02003146 struct lyd_meta *meta;
3147 struct lyd_node_any *any;
Radek Krejci22ebdba2019-07-25 13:59:43 +02003148
Michal Vasko52927e22020-03-16 17:26:14 +01003149 LY_CHECK_ARG_RET(NULL, node, LY_EINVAL);
Radek Krejci22ebdba2019-07-25 13:59:43 +02003150
Michal Vasko52927e22020-03-16 17:26:14 +01003151 if (!node->schema) {
3152 dup = calloc(1, sizeof(struct lyd_node_opaq));
3153 } else {
3154 switch (node->schema->nodetype) {
Michal Vasko1bf09392020-03-27 12:38:10 +01003155 case LYS_RPC:
Michal Vasko52927e22020-03-16 17:26:14 +01003156 case LYS_ACTION:
3157 case LYS_NOTIF:
3158 case LYS_CONTAINER:
3159 case LYS_LIST:
3160 dup = calloc(1, sizeof(struct lyd_node_inner));
3161 break;
3162 case LYS_LEAF:
3163 case LYS_LEAFLIST:
3164 dup = calloc(1, sizeof(struct lyd_node_term));
3165 break;
3166 case LYS_ANYDATA:
3167 case LYS_ANYXML:
3168 dup = calloc(1, sizeof(struct lyd_node_any));
3169 break;
3170 default:
Michal Vaskob7be7a82020-08-20 09:09:04 +02003171 LOGINT(LYD_CTX(node));
Michal Vasko52927e22020-03-16 17:26:14 +01003172 ret = LY_EINT;
3173 goto error;
3174 }
Radek Krejci22ebdba2019-07-25 13:59:43 +02003175 }
Michal Vaskob7be7a82020-08-20 09:09:04 +02003176 LY_CHECK_ERR_GOTO(!dup, LOGMEM(LYD_CTX(node)); ret = LY_EMEM, error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02003177
Michal Vaskof6df0a02020-06-16 13:08:34 +02003178 if (options & LYD_DUP_WITH_FLAGS) {
3179 dup->flags = node->flags;
3180 } else {
3181 dup->flags = (node->flags & LYD_DEFAULT) | LYD_NEW;
3182 }
Radek Krejci22ebdba2019-07-25 13:59:43 +02003183 dup->schema = node->schema;
Michal Vasko52927e22020-03-16 17:26:14 +01003184 dup->prev = dup;
Radek Krejci22ebdba2019-07-25 13:59:43 +02003185
Michal Vasko25a32822020-07-09 15:48:22 +02003186 /* duplicate metadata */
3187 if (!(options & LYD_DUP_NO_META)) {
3188 LY_LIST_FOR(node->meta, meta) {
Michal Vasko3a41dff2020-07-15 14:30:28 +02003189 LY_CHECK_GOTO(ret = lyd_dup_meta_single(meta, dup, NULL), error);
Michal Vasko25a32822020-07-09 15:48:22 +02003190 }
3191 }
Radek Krejci22ebdba2019-07-25 13:59:43 +02003192
3193 /* nodetype-specific work */
Michal Vasko52927e22020-03-16 17:26:14 +01003194 if (!dup->schema) {
3195 struct lyd_node_opaq *opaq = (struct lyd_node_opaq *)dup;
3196 struct lyd_node_opaq *orig = (struct lyd_node_opaq *)node;
3197 struct lyd_node *child;
Radek Krejci22ebdba2019-07-25 13:59:43 +02003198
3199 if (options & LYD_DUP_RECURSIVE) {
3200 /* duplicate all the children */
3201 LY_LIST_FOR(orig->child, child) {
Michal Vasko3a41dff2020-07-15 14:30:28 +02003202 LY_CHECK_GOTO(ret = lyd_dup_r(child, dup, NULL, options, NULL), error);
Michal Vasko52927e22020-03-16 17:26:14 +01003203 }
3204 }
Michal Vaskoad92b672020-11-12 13:11:31 +01003205 LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), orig->name.name, 0, &opaq->name.name), error);
Michal Vasko52927e22020-03-16 17:26:14 +01003206 opaq->format = orig->format;
Michal Vaskoad92b672020-11-12 13:11:31 +01003207 if (orig->name.prefix) {
3208 LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), orig->name.prefix, 0, &opaq->name.prefix), error);
Michal Vasko52927e22020-03-16 17:26:14 +01003209 }
Michal Vaskoad92b672020-11-12 13:11:31 +01003210 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 +01003211 if (orig->val_prefix_data) {
3212 ret = ly_dup_prefix_data(LYD_CTX(node), opaq->format, orig->val_prefix_data, &opaq->val_prefix_data);
3213 LY_CHECK_GOTO(ret, error);
Michal Vasko52927e22020-03-16 17:26:14 +01003214 }
Radek Krejci011e4aa2020-09-04 15:22:31 +02003215 LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), orig->value, 0, &opaq->value), error);
Michal Vasko52927e22020-03-16 17:26:14 +01003216 opaq->ctx = orig->ctx;
3217 } else if (dup->schema->nodetype & LYD_NODE_TERM) {
3218 struct lyd_node_term *term = (struct lyd_node_term *)dup;
3219 struct lyd_node_term *orig = (struct lyd_node_term *)node;
3220
3221 term->hash = orig->hash;
Michal Vaskob7be7a82020-08-20 09:09:04 +02003222 LY_CHECK_ERR_GOTO(orig->value.realtype->plugin->duplicate(LYD_CTX(node), &orig->value, &term->value),
Michal Vasko69730152020-10-09 16:30:07 +02003223 LOGERR(LYD_CTX(node), LY_EINT, "Value duplication failed."); ret = LY_EINT, error);
Michal Vasko52927e22020-03-16 17:26:14 +01003224 } else if (dup->schema->nodetype & LYD_NODE_INNER) {
3225 struct lyd_node_inner *orig = (struct lyd_node_inner *)node;
3226 struct lyd_node *child;
3227
3228 if (options & LYD_DUP_RECURSIVE) {
3229 /* duplicate all the children */
3230 LY_LIST_FOR(orig->child, child) {
Michal Vasko3a41dff2020-07-15 14:30:28 +02003231 LY_CHECK_GOTO(ret = lyd_dup_r(child, dup, NULL, options, NULL), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02003232 }
Michal Vasko69730152020-10-09 16:30:07 +02003233 } else if ((dup->schema->nodetype == LYS_LIST) && !(dup->schema->flags & LYS_KEYLESS)) {
Radek Krejci22ebdba2019-07-25 13:59:43 +02003234 /* always duplicate keys of a list */
Radek Krejci22ebdba2019-07-25 13:59:43 +02003235 child = orig->child;
Michal Vasko544e58a2021-01-28 14:33:41 +01003236 for (const struct lysc_node *key = lysc_node_child(dup->schema);
Michal Vaskoc57d9a92020-06-23 13:28:27 +02003237 key && (key->flags & LYS_KEY);
Radek Krejci0fe9b512019-07-26 17:51:05 +02003238 key = key->next) {
Radek Krejci22ebdba2019-07-25 13:59:43 +02003239 if (!child) {
3240 /* possibly not keys are present in filtered tree */
3241 break;
Radek Krejci0fe9b512019-07-26 17:51:05 +02003242 } else if (child->schema != key) {
3243 /* possibly not all keys are present in filtered tree,
3244 * but there can be also some non-key nodes */
3245 continue;
Radek Krejci22ebdba2019-07-25 13:59:43 +02003246 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02003247 LY_CHECK_GOTO(ret = lyd_dup_r(child, dup, NULL, options, NULL), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02003248 child = child->next;
3249 }
3250 }
3251 lyd_hash(dup);
3252 } else if (dup->schema->nodetype & LYD_NODE_ANY) {
Michal Vasko61551fa2020-07-09 15:45:45 +02003253 dup->hash = node->hash;
3254 any = (struct lyd_node_any *)node;
3255 LY_CHECK_GOTO(ret = lyd_any_copy_value(dup, &any->value, any->value_type), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02003256 }
3257
Michal Vasko52927e22020-03-16 17:26:14 +01003258 /* insert */
3259 lyd_insert_node(parent, first, dup);
Michal Vasko52927e22020-03-16 17:26:14 +01003260
3261 if (dup_p) {
3262 *dup_p = dup;
3263 }
3264 return LY_SUCCESS;
Radek Krejci22ebdba2019-07-25 13:59:43 +02003265
3266error:
Michal Vasko52927e22020-03-16 17:26:14 +01003267 lyd_free_tree(dup);
3268 return ret;
Radek Krejci22ebdba2019-07-25 13:59:43 +02003269}
3270
Michal Vasko3a41dff2020-07-15 14:30:28 +02003271static LY_ERR
3272lyd_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 +02003273 struct lyd_node_inner **local_parent)
Radek Krejci22ebdba2019-07-25 13:59:43 +02003274{
Michal Vasko3a41dff2020-07-15 14:30:28 +02003275 const struct lyd_node_inner *orig_parent, *iter;
Radek Krejci857189e2020-09-01 13:26:36 +02003276 ly_bool repeat = 1;
Michal Vasko3a41dff2020-07-15 14:30:28 +02003277
3278 *dup_parent = NULL;
3279 *local_parent = NULL;
3280
3281 for (orig_parent = node->parent; repeat && orig_parent; orig_parent = orig_parent->parent) {
3282 if (parent && (parent->schema == orig_parent->schema)) {
3283 /* stop creating parents, connect what we have into the provided parent */
3284 iter = parent;
3285 repeat = 0;
3286 } else {
3287 iter = NULL;
3288 LY_CHECK_RET(lyd_dup_r((struct lyd_node *)orig_parent, NULL, (struct lyd_node **)&iter, 0,
Radek Krejci0f969882020-08-21 16:56:47 +02003289 (struct lyd_node **)&iter));
Michal Vasko3a41dff2020-07-15 14:30:28 +02003290 }
3291 if (!*local_parent) {
3292 *local_parent = (struct lyd_node_inner *)iter;
3293 }
3294 if (iter->child) {
3295 /* 1) list - add after keys
3296 * 2) provided parent with some children */
3297 iter->child->prev->next = *dup_parent;
3298 if (*dup_parent) {
3299 (*dup_parent)->prev = iter->child->prev;
3300 iter->child->prev = *dup_parent;
3301 }
3302 } else {
3303 ((struct lyd_node_inner *)iter)->child = *dup_parent;
3304 }
3305 if (*dup_parent) {
3306 (*dup_parent)->parent = (struct lyd_node_inner *)iter;
3307 }
3308 *dup_parent = (struct lyd_node *)iter;
3309 }
3310
3311 if (repeat && parent) {
3312 /* given parent and created parents chain actually do not interconnect */
Michal Vaskob7be7a82020-08-20 09:09:04 +02003313 LOGERR(LYD_CTX(node), LY_EINVAL,
Michal Vasko69730152020-10-09 16:30:07 +02003314 "Invalid argument parent (%s()) - does not interconnect with the created node's parents chain.", __func__);
Michal Vasko3a41dff2020-07-15 14:30:28 +02003315 return LY_EINVAL;
3316 }
3317
3318 return LY_SUCCESS;
3319}
3320
3321static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02003322lyd_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 +02003323{
3324 LY_ERR rc;
Radek Krejci22ebdba2019-07-25 13:59:43 +02003325 const struct lyd_node *orig; /* original node to be duplicated */
3326 struct lyd_node *first = NULL; /* the first duplicated node, this is returned */
Radek Krejci22ebdba2019-07-25 13:59:43 +02003327 struct lyd_node *top = NULL; /* the most higher created node */
3328 struct lyd_node_inner *local_parent = NULL; /* the direct parent node for the duplicated node(s) */
Radek Krejci22ebdba2019-07-25 13:59:43 +02003329
Michal Vasko3a41dff2020-07-15 14:30:28 +02003330 LY_CHECK_ARG_RET(NULL, node, LY_EINVAL);
Radek Krejci22ebdba2019-07-25 13:59:43 +02003331
3332 if (options & LYD_DUP_WITH_PARENTS) {
Michal Vasko3a41dff2020-07-15 14:30:28 +02003333 LY_CHECK_GOTO(rc = lyd_dup_get_local_parent(node, parent, &top, &local_parent), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02003334 } else {
3335 local_parent = parent;
3336 }
3337
Radek Krejci22ebdba2019-07-25 13:59:43 +02003338 LY_LIST_FOR(node, orig) {
Michal Vasko35f4d772021-01-12 12:08:57 +01003339 if (lysc_is_key(orig->schema)) {
3340 if (local_parent) {
3341 /* the key must already exist in the parent */
3342 rc = lyd_find_sibling_schema(local_parent->child, orig->schema, first ? NULL : &first);
3343 LY_CHECK_ERR_GOTO(rc, LOGINT(LYD_CTX(node)), error);
3344 } else {
3345 assert(!(options & LYD_DUP_WITH_PARENTS));
3346 /* duplicating a single key, okay, I suppose... */
3347 rc = lyd_dup_r(orig, NULL, &first, options, first ? NULL : &first);
3348 LY_CHECK_GOTO(rc, error);
3349 }
3350 } else {
3351 /* if there is no local parent, it will be inserted into first */
Michal Vasko9e685082021-01-29 14:49:09 +01003352 rc = lyd_dup_r(orig, &local_parent->node, &first, options, first ? NULL : &first);
Michal Vasko35f4d772021-01-12 12:08:57 +01003353 LY_CHECK_GOTO(rc, error);
3354 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02003355 if (nosiblings) {
Radek Krejci22ebdba2019-07-25 13:59:43 +02003356 break;
3357 }
3358 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02003359
Michal Vasko3a41dff2020-07-15 14:30:28 +02003360 if (dup) {
3361 *dup = first;
3362 }
3363 return LY_SUCCESS;
Radek Krejci22ebdba2019-07-25 13:59:43 +02003364
3365error:
3366 if (top) {
3367 lyd_free_tree(top);
3368 } else {
Michal Vaskof03ed032020-03-04 13:31:44 +01003369 lyd_free_siblings(first);
Radek Krejci22ebdba2019-07-25 13:59:43 +02003370 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02003371 return rc;
Radek Krejci22ebdba2019-07-25 13:59:43 +02003372}
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003373
Michal Vasko3a41dff2020-07-15 14:30:28 +02003374API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003375lyd_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 +02003376{
3377 return lyd_dup(node, parent, options, 1, dup);
3378}
3379
3380API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003381lyd_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 +02003382{
3383 return lyd_dup(node, parent, options, 0, dup);
3384}
3385
3386API LY_ERR
3387lyd_dup_meta_single(const struct lyd_meta *meta, struct lyd_node *node, struct lyd_meta **dup)
Michal Vasko25a32822020-07-09 15:48:22 +02003388{
Radek Krejci011e4aa2020-09-04 15:22:31 +02003389 LY_ERR ret = LY_SUCCESS;
Michal Vasko25a32822020-07-09 15:48:22 +02003390 struct lyd_meta *mt, *last;
3391
Michal Vasko3a41dff2020-07-15 14:30:28 +02003392 LY_CHECK_ARG_RET(NULL, meta, node, LY_EINVAL);
Michal Vasko25a32822020-07-09 15:48:22 +02003393
3394 /* create a copy */
3395 mt = calloc(1, sizeof *mt);
Michal Vaskob7be7a82020-08-20 09:09:04 +02003396 LY_CHECK_ERR_RET(!mt, LOGMEM(LYD_CTX(node)), LY_EMEM);
Michal Vasko25a32822020-07-09 15:48:22 +02003397 mt->annotation = meta->annotation;
Michal Vaskob7be7a82020-08-20 09:09:04 +02003398 ret = meta->value.realtype->plugin->duplicate(LYD_CTX(node), &meta->value, &mt->value);
Radek Krejci011e4aa2020-09-04 15:22:31 +02003399 LY_CHECK_ERR_GOTO(ret, LOGERR(LYD_CTX(node), LY_EINT, "Value duplication failed."), finish);
3400 LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), meta->name, 0, &mt->name), finish);
Michal Vasko25a32822020-07-09 15:48:22 +02003401
3402 /* insert as the last attribute */
Radek Krejci011e4aa2020-09-04 15:22:31 +02003403 mt->parent = node;
Michal Vasko25a32822020-07-09 15:48:22 +02003404 if (node->meta) {
Radek Krejci1e008d22020-08-17 11:37:37 +02003405 for (last = node->meta; last->next; last = last->next) {}
Michal Vasko25a32822020-07-09 15:48:22 +02003406 last->next = mt;
3407 } else {
3408 node->meta = mt;
3409 }
3410
Radek Krejci011e4aa2020-09-04 15:22:31 +02003411finish:
3412 if (ret) {
3413 lyd_free_meta_single(mt);
3414 } else if (dup) {
Michal Vasko3a41dff2020-07-15 14:30:28 +02003415 *dup = mt;
3416 }
3417 return LY_SUCCESS;
Michal Vasko25a32822020-07-09 15:48:22 +02003418}
3419
Michal Vasko4490d312020-06-16 13:08:55 +02003420/**
3421 * @brief Merge a source sibling into target siblings.
3422 *
3423 * @param[in,out] first_trg First target sibling, is updated if top-level.
3424 * @param[in] parent_trg Target parent.
3425 * @param[in,out] sibling_src Source sibling to merge, set to NULL if spent.
3426 * @param[in] options Merge options.
3427 * @return LY_ERR value.
3428 */
3429static LY_ERR
3430lyd_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 +02003431 uint16_t options)
Michal Vasko4490d312020-06-16 13:08:55 +02003432{
3433 LY_ERR ret;
3434 const struct lyd_node *child_src, *tmp, *sibling_src;
Michal Vasko56daf732020-08-10 10:57:18 +02003435 struct lyd_node *match_trg, *dup_src, *elem;
Michal Vasko4490d312020-06-16 13:08:55 +02003436 struct lysc_type *type;
Michal Vasko4490d312020-06-16 13:08:55 +02003437
3438 sibling_src = *sibling_src_p;
3439 if (sibling_src->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) {
3440 /* try to find the exact instance */
3441 ret = lyd_find_sibling_first(*first_trg, sibling_src, &match_trg);
3442 } else {
3443 /* try to simply find the node, there cannot be more instances */
3444 ret = lyd_find_sibling_val(*first_trg, sibling_src->schema, NULL, 0, &match_trg);
3445 }
3446
3447 if (!ret) {
3448 /* node found, make sure even value matches for all node types */
Michal Vasko8f359bf2020-07-28 10:41:15 +02003449 if ((match_trg->schema->nodetype == LYS_LEAF) && lyd_compare_single(sibling_src, match_trg, LYD_COMPARE_DEFAULTS)) {
Michal Vasko4490d312020-06-16 13:08:55 +02003450 /* since they are different, they cannot both be default */
3451 assert(!(sibling_src->flags & LYD_DEFAULT) || !(match_trg->flags & LYD_DEFAULT));
3452
Michal Vasko3a41dff2020-07-15 14:30:28 +02003453 /* update value (or only LYD_DEFAULT flag) only if flag set or the source node is not default */
3454 if ((options & LYD_MERGE_DEFAULTS) || !(sibling_src->flags & LYD_DEFAULT)) {
Michal Vasko4490d312020-06-16 13:08:55 +02003455 type = ((struct lysc_node_leaf *)match_trg->schema)->type;
Michal Vaskob7be7a82020-08-20 09:09:04 +02003456 type->plugin->free(LYD_CTX(match_trg), &((struct lyd_node_term *)match_trg)->value);
3457 LY_CHECK_RET(type->plugin->duplicate(LYD_CTX(match_trg), &((struct lyd_node_term *)sibling_src)->value,
Michal Vasko69730152020-10-09 16:30:07 +02003458 &((struct lyd_node_term *)match_trg)->value));
Michal Vasko4490d312020-06-16 13:08:55 +02003459
3460 /* copy flags and add LYD_NEW */
3461 match_trg->flags = sibling_src->flags | LYD_NEW;
3462 }
Michal Vasko8f359bf2020-07-28 10:41:15 +02003463 } else if ((match_trg->schema->nodetype & LYS_ANYDATA) && lyd_compare_single(sibling_src, match_trg, 0)) {
Michal Vasko4c583e82020-07-17 12:16:14 +02003464 /* update value */
3465 LY_CHECK_RET(lyd_any_copy_value(match_trg, &((struct lyd_node_any *)sibling_src)->value,
Radek Krejci0f969882020-08-21 16:56:47 +02003466 ((struct lyd_node_any *)sibling_src)->value_type));
Michal Vasko4490d312020-06-16 13:08:55 +02003467
3468 /* copy flags and add LYD_NEW */
3469 match_trg->flags = sibling_src->flags | LYD_NEW;
Michal Vasko4490d312020-06-16 13:08:55 +02003470 } else {
3471 /* check descendants, recursively */
Radek Krejcia1c1e542020-09-29 16:06:52 +02003472 LY_LIST_FOR_SAFE(lyd_child_no_keys(sibling_src), tmp, child_src) {
Michal Vaskoe0665742021-02-11 11:08:44 +01003473 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 +02003474 }
3475 }
3476 } else {
3477 /* node not found, merge it */
3478 if (options & LYD_MERGE_DESTRUCT) {
3479 dup_src = (struct lyd_node *)sibling_src;
3480 lyd_unlink_tree(dup_src);
3481 /* spend it */
3482 *sibling_src_p = NULL;
3483 } else {
Michal Vasko3a41dff2020-07-15 14:30:28 +02003484 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 +02003485 }
3486
3487 /* set LYD_NEW for all the new nodes, required for validation */
Michal Vasko56daf732020-08-10 10:57:18 +02003488 LYD_TREE_DFS_BEGIN(dup_src, elem) {
Michal Vasko4490d312020-06-16 13:08:55 +02003489 elem->flags |= LYD_NEW;
Michal Vasko56daf732020-08-10 10:57:18 +02003490 LYD_TREE_DFS_END(dup_src, elem);
Michal Vasko4490d312020-06-16 13:08:55 +02003491 }
3492
3493 lyd_insert_node(parent_trg, first_trg, dup_src);
3494 }
3495
3496 return LY_SUCCESS;
3497}
3498
Michal Vasko3a41dff2020-07-15 14:30:28 +02003499static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02003500lyd_merge(struct lyd_node **target, const struct lyd_node *source, uint16_t options, ly_bool nosiblings)
Michal Vasko4490d312020-06-16 13:08:55 +02003501{
3502 const struct lyd_node *sibling_src, *tmp;
Radek Krejci857189e2020-09-01 13:26:36 +02003503 ly_bool first;
Michal Vasko4490d312020-06-16 13:08:55 +02003504
3505 LY_CHECK_ARG_RET(NULL, target, LY_EINVAL);
3506
3507 if (!source) {
3508 /* nothing to merge */
3509 return LY_SUCCESS;
3510 }
3511
Michal Vasko831422b2020-11-24 11:20:51 +01003512 if ((*target && lysc_data_parent((*target)->schema)) || lysc_data_parent(source->schema)) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02003513 LOGERR(LYD_CTX(source), LY_EINVAL, "Invalid arguments - can merge only 2 top-level subtrees (%s()).", __func__);
Michal Vasko4490d312020-06-16 13:08:55 +02003514 return LY_EINVAL;
3515 }
3516
3517 LY_LIST_FOR_SAFE(source, tmp, sibling_src) {
Radek Krejci857189e2020-09-01 13:26:36 +02003518 first = (sibling_src == source) ? 1 : 0;
Michal Vasko4490d312020-06-16 13:08:55 +02003519 LY_CHECK_RET(lyd_merge_sibling_r(target, NULL, &sibling_src, options));
3520 if (first && !sibling_src) {
3521 /* source was spent (unlinked), move to the next node */
3522 source = tmp;
3523 }
3524
Michal Vasko3a41dff2020-07-15 14:30:28 +02003525 if (nosiblings) {
Michal Vasko4490d312020-06-16 13:08:55 +02003526 break;
3527 }
3528 }
3529
3530 if (options & LYD_MERGE_DESTRUCT) {
3531 /* free any leftover source data that were not merged */
3532 lyd_free_siblings((struct lyd_node *)source);
3533 }
3534
3535 return LY_SUCCESS;
3536}
3537
Michal Vasko3a41dff2020-07-15 14:30:28 +02003538API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003539lyd_merge_tree(struct lyd_node **target, const struct lyd_node *source, uint16_t options)
Michal Vasko3a41dff2020-07-15 14:30:28 +02003540{
3541 return lyd_merge(target, source, options, 1);
3542}
3543
3544API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003545lyd_merge_siblings(struct lyd_node **target, const struct lyd_node *source, uint16_t options)
Michal Vasko3a41dff2020-07-15 14:30:28 +02003546{
3547 return lyd_merge(target, source, options, 0);
3548}
3549
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003550static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02003551lyd_path_str_enlarge(char **buffer, size_t *buflen, size_t reqlen, ly_bool is_static)
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003552{
Michal Vasko14654712020-02-06 08:35:21 +01003553 /* ending \0 */
3554 ++reqlen;
3555
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003556 if (reqlen > *buflen) {
3557 if (is_static) {
3558 return LY_EINCOMPLETE;
3559 }
3560
3561 *buffer = ly_realloc(*buffer, reqlen * sizeof **buffer);
3562 if (!*buffer) {
3563 return LY_EMEM;
3564 }
3565
3566 *buflen = reqlen;
3567 }
3568
3569 return LY_SUCCESS;
3570}
3571
Michal Vaskod59035b2020-07-08 12:00:06 +02003572LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02003573lyd_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 +02003574{
3575 const struct lyd_node *key;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003576 size_t len;
3577 const char *val;
3578 char quot;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003579
Radek Krejcia1c1e542020-09-29 16:06:52 +02003580 for (key = lyd_child(node); key && (key->schema->flags & LYS_KEY); key = key->next) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02003581 val = LYD_CANON_VALUE(key);
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003582 len = 1 + strlen(key->schema->name) + 2 + strlen(val) + 2;
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003583 LY_CHECK_RET(lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static));
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003584
3585 quot = '\'';
3586 if (strchr(val, '\'')) {
3587 quot = '"';
3588 }
3589 *bufused += sprintf(*buffer + *bufused, "[%s=%c%s%c]", key->schema->name, quot, val, quot);
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003590 }
3591
3592 return LY_SUCCESS;
3593}
3594
3595/**
3596 * @brief Append leaf-list value predicate to path.
3597 *
3598 * @param[in] node Node to print.
3599 * @param[in,out] buffer Buffer to print to.
3600 * @param[in,out] buflen Current buffer length.
3601 * @param[in,out] bufused Current number of characters used in @p buffer.
3602 * @param[in] is_static Whether buffer is static or can be reallocated.
3603 * @return LY_ERR
3604 */
3605static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02003606lyd_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 +02003607{
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003608 size_t len;
3609 const char *val;
3610 char quot;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003611
Michal Vaskob7be7a82020-08-20 09:09:04 +02003612 val = LYD_CANON_VALUE(node);
Radek Krejcif13b87b2020-12-01 22:02:17 +01003613 len = 4 + strlen(val) + 2; /* "[.='" + val + "']" */
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003614 LY_CHECK_RET(lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static));
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003615
3616 quot = '\'';
3617 if (strchr(val, '\'')) {
3618 quot = '"';
3619 }
3620 *bufused += sprintf(*buffer + *bufused, "[.=%c%s%c]", quot, val, quot);
3621
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003622 return LY_SUCCESS;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003623}
3624
3625/**
3626 * @brief Append node position (relative to its other instances) predicate to path.
3627 *
3628 * @param[in] node Node to print.
3629 * @param[in,out] buffer Buffer to print to.
3630 * @param[in,out] buflen Current buffer length.
3631 * @param[in,out] bufused Current number of characters used in @p buffer.
3632 * @param[in] is_static Whether buffer is static or can be reallocated.
3633 * @return LY_ERR
3634 */
3635static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02003636lyd_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 +02003637{
3638 const struct lyd_node *first, *iter;
3639 size_t len;
Radek Krejci1deb5be2020-08-26 16:43:36 +02003640 uint64_t pos;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003641 char *val = NULL;
3642 LY_ERR rc;
3643
3644 if (node->parent) {
3645 first = node->parent->child;
3646 } else {
Michal Vaskoe070bfe2020-08-31 12:27:25 +02003647 for (first = node; first->prev->next; first = first->prev) {}
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003648 }
3649 pos = 1;
3650 for (iter = first; iter != node; iter = iter->next) {
3651 if (iter->schema == node->schema) {
3652 ++pos;
3653 }
3654 }
Radek Krejci1deb5be2020-08-26 16:43:36 +02003655 if (asprintf(&val, "%" PRIu64, pos) == -1) {
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003656 return LY_EMEM;
3657 }
3658
3659 len = 1 + strlen(val) + 1;
3660 rc = lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static);
3661 if (rc != LY_SUCCESS) {
3662 goto cleanup;
3663 }
3664
3665 *bufused += sprintf(*buffer + *bufused, "[%s]", val);
3666
3667cleanup:
3668 free(val);
3669 return rc;
3670}
3671
3672API char *
3673lyd_path(const struct lyd_node *node, LYD_PATH_TYPE pathtype, char *buffer, size_t buflen)
3674{
Radek Krejci857189e2020-09-01 13:26:36 +02003675 ly_bool is_static = 0;
Radek Krejci1deb5be2020-08-26 16:43:36 +02003676 uint32_t i, depth;
Michal Vasko14654712020-02-06 08:35:21 +01003677 size_t bufused = 0, len;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003678 const struct lyd_node *iter;
3679 const struct lys_module *mod;
Michal Vasko790b2bc2020-08-03 13:35:06 +02003680 LY_ERR rc = LY_SUCCESS;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003681
3682 LY_CHECK_ARG_RET(NULL, node, NULL);
3683 if (buffer) {
3684 LY_CHECK_ARG_RET(node->schema->module->ctx, buflen > 1, NULL);
3685 is_static = 1;
Michal Vasko14654712020-02-06 08:35:21 +01003686 } else {
3687 buflen = 0;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003688 }
3689
3690 switch (pathtype) {
Radek Krejci635d2b82021-01-04 11:26:51 +01003691 case LYD_PATH_STD:
3692 case LYD_PATH_STD_NO_LAST_PRED:
Michal Vasko14654712020-02-06 08:35:21 +01003693 depth = 1;
Michal Vasko9e685082021-01-29 14:49:09 +01003694 for (iter = node; iter->parent; iter = lyd_parent(iter)) {
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003695 ++depth;
3696 }
3697
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003698 goto iter_print;
Michal Vasko14654712020-02-06 08:35:21 +01003699 while (depth) {
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003700 /* find the right node */
Michal Vasko9e685082021-01-29 14:49:09 +01003701 for (iter = node, i = 1; i < depth; iter = lyd_parent(iter), ++i) {}
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003702iter_print:
3703 /* print prefix and name */
3704 mod = NULL;
Michal Vasko69730152020-10-09 16:30:07 +02003705 if (iter->schema && (!iter->parent || (iter->schema->module != iter->parent->schema->module))) {
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003706 mod = iter->schema->module;
3707 }
3708
3709 /* realloc string */
Michal Vaskoad92b672020-11-12 13:11:31 +01003710 len = 1 + (mod ? strlen(mod->name) + 1 : 0) + (iter->schema ? strlen(iter->schema->name) :
3711 strlen(((struct lyd_node_opaq *)iter)->name.name));
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003712 rc = lyd_path_str_enlarge(&buffer, &buflen, bufused + len, is_static);
3713 if (rc != LY_SUCCESS) {
3714 break;
3715 }
3716
3717 /* print next node */
Radek Krejci1798aae2020-07-14 13:26:06 +02003718 bufused += sprintf(buffer + bufused, "/%s%s%s", mod ? mod->name : "", mod ? ":" : "",
Michal Vaskoad92b672020-11-12 13:11:31 +01003719 iter->schema ? iter->schema->name : ((struct lyd_node_opaq *)iter)->name.name);
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003720
Michal Vasko790b2bc2020-08-03 13:35:06 +02003721 /* do not always print the last (first) predicate */
Radek Krejci635d2b82021-01-04 11:26:51 +01003722 if (iter->schema && ((depth > 1) || (pathtype == LYD_PATH_STD))) {
Michal Vasko790b2bc2020-08-03 13:35:06 +02003723 switch (iter->schema->nodetype) {
3724 case LYS_LIST:
3725 if (iter->schema->flags & LYS_KEYLESS) {
3726 /* print its position */
3727 rc = lyd_path_position_predicate(iter, &buffer, &buflen, &bufused, is_static);
3728 } else {
3729 /* print all list keys in predicates */
3730 rc = lyd_path_list_predicate(iter, &buffer, &buflen, &bufused, is_static);
3731 }
3732 break;
3733 case LYS_LEAFLIST:
3734 if (iter->schema->flags & LYS_CONFIG_W) {
3735 /* print leaf-list value */
3736 rc = lyd_path_leaflist_predicate(iter, &buffer, &buflen, &bufused, is_static);
3737 } else {
3738 /* print its position */
3739 rc = lyd_path_position_predicate(iter, &buffer, &buflen, &bufused, is_static);
3740 }
3741 break;
3742 default:
3743 /* nothing to print more */
3744 break;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003745 }
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003746 }
3747 if (rc != LY_SUCCESS) {
3748 break;
3749 }
3750
Michal Vasko14654712020-02-06 08:35:21 +01003751 --depth;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003752 }
3753 break;
3754 }
3755
3756 return buffer;
3757}
Michal Vaskoe444f752020-02-10 12:20:06 +01003758
Michal Vasko25a32822020-07-09 15:48:22 +02003759API struct lyd_meta *
3760lyd_find_meta(const struct lyd_meta *first, const struct lys_module *module, const char *name)
3761{
3762 struct lyd_meta *ret = NULL;
3763 const struct ly_ctx *ctx;
3764 const char *prefix, *tmp;
3765 char *str;
3766 size_t pref_len, name_len;
3767
3768 LY_CHECK_ARG_RET(NULL, module || strchr(name, ':'), name, NULL);
3769
3770 if (!first) {
3771 return NULL;
3772 }
3773
3774 ctx = first->annotation->module->ctx;
3775
3776 /* parse the name */
3777 tmp = name;
3778 if (ly_parse_nodeid(&tmp, &prefix, &pref_len, &name, &name_len) || tmp[0]) {
3779 LOGERR(ctx, LY_EINVAL, "Metadata name \"%s\" is not valid.", name);
3780 return NULL;
3781 }
3782
3783 /* find the module */
3784 if (prefix) {
3785 str = strndup(prefix, pref_len);
3786 module = ly_ctx_get_module_latest(ctx, str);
3787 free(str);
Radek Krejci422afb12021-03-04 16:38:16 +01003788 LY_CHECK_ERR_RET(!module, LOGERR(ctx, LY_EINVAL, "Module \"%.*s\" not found.", (int)pref_len, prefix), NULL);
Michal Vasko25a32822020-07-09 15:48:22 +02003789 }
3790
3791 /* find the metadata */
3792 LY_LIST_FOR(first, first) {
3793 if ((first->annotation->module == module) && !strcmp(first->name, name)) {
3794 ret = (struct lyd_meta *)first;
3795 break;
3796 }
3797 }
3798
3799 return ret;
3800}
3801
Michal Vasko9b368d32020-02-14 13:53:31 +01003802API LY_ERR
Michal Vaskoe444f752020-02-10 12:20:06 +01003803lyd_find_sibling_first(const struct lyd_node *siblings, const struct lyd_node *target, struct lyd_node **match)
3804{
Michal Vaskoe78faec2021-04-08 17:24:43 +02003805 struct lyd_node **match_p, *iter;
Michal Vaskoe444f752020-02-10 12:20:06 +01003806 struct lyd_node_inner *parent;
Michal Vaskoe78faec2021-04-08 17:24:43 +02003807 ly_bool found;
Michal Vaskoe444f752020-02-10 12:20:06 +01003808
Michal Vaskof03ed032020-03-04 13:31:44 +01003809 LY_CHECK_ARG_RET(NULL, target, LY_EINVAL);
Michal Vaskoe444f752020-02-10 12:20:06 +01003810
Michal Vasko6da1e952020-12-09 18:14:38 +01003811 if (!siblings || (siblings->schema && target->schema &&
3812 (lysc_data_parent(siblings->schema) != lysc_data_parent(target->schema)))) {
Michal Vasko62ed12d2020-05-21 10:08:25 +02003813 /* no data or schema mismatch */
Michal Vasko9b368d32020-02-14 13:53:31 +01003814 if (match) {
3815 *match = NULL;
3816 }
Michal Vaskoe444f752020-02-10 12:20:06 +01003817 return LY_ENOTFOUND;
3818 }
3819
Michal Vaskoe78faec2021-04-08 17:24:43 +02003820 /* get first sibling */
3821 siblings = lyd_first_sibling(siblings);
Michal Vaskoe444f752020-02-10 12:20:06 +01003822
Michal Vasko9e685082021-01-29 14:49:09 +01003823 parent = siblings->parent;
Michal Vasko6da1e952020-12-09 18:14:38 +01003824 if (parent && parent->schema && parent->children_ht) {
Michal Vaskoe444f752020-02-10 12:20:06 +01003825 assert(target->hash);
3826
Michal Vaskoe78faec2021-04-08 17:24:43 +02003827 if (lysc_is_dup_inst_list(target->schema)) {
3828 /* we must search the instances from beginning to find the first matching one */
3829 found = 0;
3830 LYD_LIST_FOR_INST(siblings, target->schema, iter) {
3831 if (!lyd_compare_single(target, iter, 0)) {
3832 found = 1;
3833 break;
3834 }
3835 }
3836 if (found) {
3837 siblings = iter;
Michal Vaskoda859032020-07-14 12:20:14 +02003838 } else {
3839 siblings = NULL;
3840 }
Michal Vaskoe444f752020-02-10 12:20:06 +01003841 } else {
Michal Vaskoe78faec2021-04-08 17:24:43 +02003842 /* find by hash */
3843 if (!lyht_find(parent->children_ht, &target, target->hash, (void **)&match_p)) {
3844 siblings = *match_p;
3845 } else {
3846 /* not found */
3847 siblings = NULL;
3848 }
Michal Vaskoe444f752020-02-10 12:20:06 +01003849 }
3850 } else {
3851 /* no children hash table */
Michal Vaskod989ba02020-08-24 10:59:24 +02003852 for ( ; siblings; siblings = siblings->next) {
Michal Vasko8f359bf2020-07-28 10:41:15 +02003853 if (!lyd_compare_single(siblings, target, 0)) {
Michal Vaskoe444f752020-02-10 12:20:06 +01003854 break;
3855 }
3856 }
3857 }
3858
3859 if (!siblings) {
Michal Vasko9b368d32020-02-14 13:53:31 +01003860 if (match) {
3861 *match = NULL;
3862 }
Michal Vaskoe444f752020-02-10 12:20:06 +01003863 return LY_ENOTFOUND;
3864 }
3865
Michal Vasko9b368d32020-02-14 13:53:31 +01003866 if (match) {
3867 *match = (struct lyd_node *)siblings;
3868 }
Michal Vaskoe444f752020-02-10 12:20:06 +01003869 return LY_SUCCESS;
3870}
3871
Radek Krejci857189e2020-09-01 13:26:36 +02003872/**
3873 * @brief Comparison callback to match schema node with a schema of a data node.
3874 *
3875 * @param[in] val1_p Pointer to the schema node
3876 * @param[in] val2_p Pointer to the data node
Michal Vasko62524a92021-02-26 10:08:50 +01003877 * Implementation of ::lyht_value_equal_cb.
Radek Krejci857189e2020-09-01 13:26:36 +02003878 */
3879static ly_bool
3880lyd_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 +01003881{
3882 struct lysc_node *val1;
3883 struct lyd_node *val2;
3884
3885 val1 = *((struct lysc_node **)val1_p);
3886 val2 = *((struct lyd_node **)val2_p);
3887
Michal Vasko90932a92020-02-12 14:33:03 +01003888 if (val1 == val2->schema) {
3889 /* schema match is enough */
3890 return 1;
3891 } else {
3892 return 0;
3893 }
3894}
3895
Michal Vasko92239c72020-11-18 18:17:25 +01003896/**
3897 * @brief Search in the given siblings (NOT recursively) for the first schema node data instance.
3898 * Uses hashes - should be used whenever possible for best performance.
3899 *
3900 * @param[in] siblings Siblings to search in including preceding and succeeding nodes.
3901 * @param[in] schema Target data node schema to find.
3902 * @param[out] match Can be NULL, otherwise the found data node.
3903 * @return LY_SUCCESS on success, @p match set.
3904 * @return LY_ENOTFOUND if not found, @p match set to NULL.
3905 * @return LY_ERR value if another error occurred.
3906 */
Michal Vasko90932a92020-02-12 14:33:03 +01003907static LY_ERR
3908lyd_find_sibling_schema(const struct lyd_node *siblings, const struct lysc_node *schema, struct lyd_node **match)
3909{
3910 struct lyd_node **match_p;
3911 struct lyd_node_inner *parent;
3912 uint32_t hash;
Michal Vasko62524a92021-02-26 10:08:50 +01003913 lyht_value_equal_cb ht_cb;
Michal Vasko90932a92020-02-12 14:33:03 +01003914
Michal Vaskob104f112020-07-17 09:54:54 +02003915 assert(siblings && schema);
Michal Vasko90932a92020-02-12 14:33:03 +01003916
Michal Vasko9e685082021-01-29 14:49:09 +01003917 parent = siblings->parent;
Michal Vasko08fd6132020-11-18 18:17:45 +01003918 if (parent && parent->schema && parent->children_ht) {
Michal Vasko90932a92020-02-12 14:33:03 +01003919 /* calculate our hash */
3920 hash = dict_hash_multi(0, schema->module->name, strlen(schema->module->name));
3921 hash = dict_hash_multi(hash, schema->name, strlen(schema->name));
3922 hash = dict_hash_multi(hash, NULL, 0);
3923
3924 /* use special hash table function */
3925 ht_cb = lyht_set_cb(parent->children_ht, lyd_hash_table_schema_val_equal);
3926
3927 /* find by hash */
3928 if (!lyht_find(parent->children_ht, &schema, hash, (void **)&match_p)) {
3929 siblings = *match_p;
3930 } else {
3931 /* not found */
3932 siblings = NULL;
3933 }
3934
3935 /* set the original hash table compare function back */
3936 lyht_set_cb(parent->children_ht, ht_cb);
3937 } else {
Michal Vaskob104f112020-07-17 09:54:54 +02003938 /* find first sibling */
3939 if (siblings->parent) {
3940 siblings = siblings->parent->child;
3941 } else {
3942 while (siblings->prev->next) {
3943 siblings = siblings->prev;
3944 }
3945 }
3946
3947 /* search manually without hashes */
Michal Vaskod989ba02020-08-24 10:59:24 +02003948 for ( ; siblings; siblings = siblings->next) {
Michal Vasko90932a92020-02-12 14:33:03 +01003949 if (siblings->schema == schema) {
3950 /* schema match is enough */
3951 break;
3952 }
3953 }
3954 }
3955
3956 if (!siblings) {
Michal Vasko9b368d32020-02-14 13:53:31 +01003957 if (match) {
3958 *match = NULL;
3959 }
Michal Vasko90932a92020-02-12 14:33:03 +01003960 return LY_ENOTFOUND;
3961 }
3962
Michal Vasko9b368d32020-02-14 13:53:31 +01003963 if (match) {
3964 *match = (struct lyd_node *)siblings;
3965 }
Michal Vasko90932a92020-02-12 14:33:03 +01003966 return LY_SUCCESS;
3967}
3968
Michal Vaskoe444f752020-02-10 12:20:06 +01003969API LY_ERR
3970lyd_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 +02003971 size_t val_len, struct lyd_node **match)
Michal Vaskoe444f752020-02-10 12:20:06 +01003972{
3973 LY_ERR rc;
3974 struct lyd_node *target = NULL;
3975
Michal Vasko4c583e82020-07-17 12:16:14 +02003976 LY_CHECK_ARG_RET(NULL, schema, !(schema->nodetype & (LYS_CHOICE | LYS_CASE)), LY_EINVAL);
Michal Vaskoe444f752020-02-10 12:20:06 +01003977
Michal Vasko08fd6132020-11-18 18:17:45 +01003978 if (!siblings || (siblings->schema && (lysc_data_parent(siblings->schema) != lysc_data_parent(schema)))) {
Michal Vasko62ed12d2020-05-21 10:08:25 +02003979 /* no data or schema mismatch */
Michal Vasko9b368d32020-02-14 13:53:31 +01003980 if (match) {
3981 *match = NULL;
3982 }
Michal Vaskoe444f752020-02-10 12:20:06 +01003983 return LY_ENOTFOUND;
3984 }
3985
Michal Vaskof03ed032020-03-04 13:31:44 +01003986 if (key_or_value && !val_len) {
3987 val_len = strlen(key_or_value);
3988 }
3989
Michal Vaskob104f112020-07-17 09:54:54 +02003990 if ((schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) && key_or_value) {
3991 /* create a data node and find the instance */
3992 if (schema->nodetype == LYS_LEAFLIST) {
3993 /* target used attributes: schema, hash, value */
Radek Krejci8df109d2021-04-23 12:19:08 +02003994 rc = lyd_create_term(schema, key_or_value, val_len, NULL, LY_VALUE_JSON, NULL, LYD_HINT_DATA, NULL, &target);
Michal Vaskofeca4fb2020-10-05 08:58:40 +02003995 LY_CHECK_RET(rc);
Michal Vaskob104f112020-07-17 09:54:54 +02003996 } else {
Michal Vasko90932a92020-02-12 14:33:03 +01003997 /* target used attributes: schema, hash, child (all keys) */
Michal Vasko004d3152020-06-11 19:59:22 +02003998 LY_CHECK_RET(lyd_create_list2(schema, key_or_value, val_len, &target));
Michal Vasko90932a92020-02-12 14:33:03 +01003999 }
4000
4001 /* find it */
4002 rc = lyd_find_sibling_first(siblings, target, match);
Michal Vaskob104f112020-07-17 09:54:54 +02004003 } else {
4004 /* find the first schema node instance */
4005 rc = lyd_find_sibling_schema(siblings, schema, match);
Michal Vaskoe444f752020-02-10 12:20:06 +01004006 }
4007
Michal Vaskoe444f752020-02-10 12:20:06 +01004008 lyd_free_tree(target);
4009 return rc;
4010}
Michal Vaskoccc02342020-05-21 10:09:21 +02004011
4012API LY_ERR
Michal Vaskoe78faec2021-04-08 17:24:43 +02004013lyd_find_sibling_dup_inst_set(const struct lyd_node *siblings, const struct lyd_node *target, struct ly_set **set)
4014{
4015 struct lyd_node **match_p, *first, *iter;
4016 struct lyd_node_inner *parent;
4017
4018 LY_CHECK_ARG_RET(NULL, target, lysc_is_dup_inst_list(target->schema), set, LY_EINVAL);
4019
4020 LY_CHECK_RET(ly_set_new(set));
4021
4022 if (!siblings || (siblings->schema && (lysc_data_parent(siblings->schema) != lysc_data_parent(target->schema)))) {
4023 /* no data or schema mismatch */
4024 return LY_ENOTFOUND;
4025 }
4026
4027 /* get first sibling */
4028 siblings = lyd_first_sibling(siblings);
4029
4030 parent = siblings->parent;
4031 if (parent && parent->schema && parent->children_ht) {
4032 assert(target->hash);
4033
4034 /* find the first instance */
4035 lyd_find_sibling_first(siblings, target, &first);
4036 if (first) {
4037 /* add it so that it is the first in the set */
4038 if (ly_set_add(*set, first, 1, NULL)) {
4039 goto error;
4040 }
4041
4042 /* find by hash */
4043 if (!lyht_find(parent->children_ht, &target, target->hash, (void **)&match_p)) {
4044 iter = *match_p;
4045 } else {
4046 /* not found */
4047 iter = NULL;
4048 }
4049 while (iter) {
4050 /* add all found nodes into the set */
4051 if ((iter != first) && !lyd_compare_single(iter, target, 0) && ly_set_add(*set, iter, 1, NULL)) {
4052 goto error;
4053 }
4054
4055 /* find next instance */
4056 if (lyht_find_next(parent->children_ht, &iter, iter->hash, (void **)&match_p)) {
4057 iter = NULL;
4058 } else {
4059 iter = *match_p;
4060 }
4061 }
4062 }
4063 } else {
4064 /* no children hash table */
4065 LY_LIST_FOR(siblings, siblings) {
4066 if (!lyd_compare_single(target, siblings, 0)) {
4067 ly_set_add(*set, (void *)siblings, 1, NULL);
4068 }
4069 }
4070 }
4071
4072 if (!(*set)->count) {
4073 return LY_ENOTFOUND;
4074 }
4075 return LY_SUCCESS;
4076
4077error:
4078 ly_set_free(*set, NULL);
4079 *set = NULL;
4080 return LY_EMEM;
4081}
4082
4083API LY_ERR
Michal Vasko1d4af6c2021-02-22 13:31:26 +01004084lyd_find_sibling_opaq_next(const struct lyd_node *first, const char *name, struct lyd_node **match)
4085{
4086 LY_CHECK_ARG_RET(NULL, name, LY_EINVAL);
4087
4088 for ( ; first; first = first->next) {
4089 if (!first->schema && !strcmp(LYD_NAME(first), name)) {
4090 break;
4091 }
4092 }
4093
4094 if (match) {
4095 *match = (struct lyd_node *)first;
4096 }
4097 return first ? LY_SUCCESS : LY_ENOTFOUND;
4098}
4099
4100API LY_ERR
Michal Vaskoccc02342020-05-21 10:09:21 +02004101lyd_find_xpath(const struct lyd_node *ctx_node, const char *xpath, struct ly_set **set)
4102{
4103 LY_ERR ret = LY_SUCCESS;
4104 struct lyxp_set xp_set;
Radek Krejcif03a9e22020-09-18 20:09:31 +02004105 struct lyxp_expr *exp = NULL;
Michal Vaskoccc02342020-05-21 10:09:21 +02004106 uint32_t i;
4107
4108 LY_CHECK_ARG_RET(NULL, ctx_node, xpath, set, LY_EINVAL);
4109
Michal Vasko37c69432021-04-12 14:48:24 +02004110 *set = NULL;
Michal Vaskoccc02342020-05-21 10:09:21 +02004111 memset(&xp_set, 0, sizeof xp_set);
4112
4113 /* compile expression */
Radek Krejcif03a9e22020-09-18 20:09:31 +02004114 ret = lyxp_expr_parse((struct ly_ctx *)LYD_CTX(ctx_node), xpath, 0, 1, &exp);
4115 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskoccc02342020-05-21 10:09:21 +02004116
4117 /* evaluate expression */
Radek Krejci8df109d2021-04-23 12:19:08 +02004118 ret = lyxp_eval(LYD_CTX(ctx_node), exp, NULL, LY_VALUE_JSON, NULL, ctx_node, ctx_node, &xp_set, LYXP_IGNORE_WHEN);
Michal Vaskoccc02342020-05-21 10:09:21 +02004119 LY_CHECK_GOTO(ret, cleanup);
4120
4121 /* allocate return set */
Radek Krejciba03a5a2020-08-27 14:40:41 +02004122 ret = ly_set_new(set);
4123 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskoccc02342020-05-21 10:09:21 +02004124
4125 /* transform into ly_set */
4126 if (xp_set.type == LYXP_SET_NODE_SET) {
4127 /* allocate memory for all the elements once (even though not all items must be elements but most likely will be) */
4128 (*set)->objs = malloc(xp_set.used * sizeof *(*set)->objs);
Michal Vaskob7be7a82020-08-20 09:09:04 +02004129 LY_CHECK_ERR_GOTO(!(*set)->objs, LOGMEM(LYD_CTX(ctx_node)); ret = LY_EMEM, cleanup);
Michal Vaskoccc02342020-05-21 10:09:21 +02004130 (*set)->size = xp_set.used;
4131
4132 for (i = 0; i < xp_set.used; ++i) {
4133 if (xp_set.val.nodes[i].type == LYXP_NODE_ELEM) {
Radek Krejci3d92e442020-10-12 12:48:13 +02004134 ret = ly_set_add(*set, xp_set.val.nodes[i].node, 1, NULL);
Radek Krejciba03a5a2020-08-27 14:40:41 +02004135 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskoccc02342020-05-21 10:09:21 +02004136 }
4137 }
4138 }
4139
4140cleanup:
Michal Vasko0691d522020-05-21 13:21:47 +02004141 lyxp_set_free_content(&xp_set);
Michal Vaskob7be7a82020-08-20 09:09:04 +02004142 lyxp_expr_free((struct ly_ctx *)LYD_CTX(ctx_node), exp);
Radek Krejci8f45abc2020-11-26 12:15:13 +01004143 if (ret) {
4144 ly_set_free(*set, NULL);
4145 *set = NULL;
4146 }
Michal Vaskoccc02342020-05-21 10:09:21 +02004147 return ret;
4148}
Radek Krejcica989142020-11-05 11:32:22 +01004149
Michal Vasko3e1f6552021-01-14 09:27:55 +01004150API LY_ERR
4151lyd_find_path(const struct lyd_node *ctx_node, const char *path, ly_bool output, struct lyd_node **match)
4152{
4153 LY_ERR ret = LY_SUCCESS;
4154 struct lyxp_expr *expr = NULL;
4155 struct ly_path *lypath = NULL;
4156
4157 LY_CHECK_ARG_RET(NULL, ctx_node, ctx_node->schema, path, LY_EINVAL);
4158
4159 /* parse the path */
4160 ret = ly_path_parse(LYD_CTX(ctx_node), ctx_node->schema, path, strlen(path), LY_PATH_BEGIN_EITHER, LY_PATH_LREF_FALSE,
4161 LY_PATH_PREFIX_OPTIONAL, LY_PATH_PRED_SIMPLE, &expr);
4162 LY_CHECK_GOTO(ret, cleanup);
4163
4164 /* compile the path */
Radek Krejcid5d37432021-03-12 13:46:40 +01004165 ret = ly_path_compile(LYD_CTX(ctx_node), NULL, ctx_node->schema, NULL, expr, LY_PATH_LREF_FALSE,
Radek Krejci8df109d2021-04-23 12:19:08 +02004166 output ? LY_PATH_OPER_OUTPUT : LY_PATH_OPER_INPUT, LY_PATH_TARGET_SINGLE, LY_VALUE_JSON,
Michal Vasko3e1f6552021-01-14 09:27:55 +01004167 (void *)LYD_CTX(ctx_node), NULL, &lypath);
4168 LY_CHECK_GOTO(ret, cleanup);
4169
4170 /* evaluate the path */
4171 ret = ly_path_eval_partial(lypath, ctx_node, NULL, match);
4172
4173cleanup:
4174 lyxp_expr_free(LYD_CTX(ctx_node), expr);
4175 ly_path_free(LYD_CTX(ctx_node), lypath);
4176 return ret;
4177}
4178
Radek Krejcica989142020-11-05 11:32:22 +01004179API uint32_t
4180lyd_list_pos(const struct lyd_node *instance)
4181{
4182 const struct lyd_node *iter = NULL;
4183 uint32_t pos = 0;
4184
4185 if (!instance || !(instance->schema->nodetype & (LYS_LIST | LYS_LEAFLIST))) {
4186 return 0;
4187 }
4188
4189 /* data instances are ordered, so we can stop when we found instance of other schema node */
4190 for (iter = instance; iter->schema == instance->schema; iter = iter->prev) {
Radek Krejcibb27df32020-11-13 15:39:16 +01004191 if (pos && (iter->next == NULL)) {
Radek Krejcica989142020-11-05 11:32:22 +01004192 /* overrun to the end of the siblings list */
4193 break;
4194 }
4195 ++pos;
4196 }
4197
4198 return pos;
4199}
Radek Krejci4233f9b2020-11-05 12:38:35 +01004200
4201API struct lyd_node *
4202lyd_first_sibling(const struct lyd_node *node)
4203{
4204 struct lyd_node *start;
4205
4206 if (!node) {
4207 return NULL;
4208 }
4209
4210 /* get the first sibling */
4211 if (node->parent) {
4212 start = node->parent->child;
4213 } else {
Radek Krejcibb27df32020-11-13 15:39:16 +01004214 for (start = (struct lyd_node *)node; start->prev->next; start = start->prev) {}
Radek Krejci4233f9b2020-11-05 12:38:35 +01004215 }
4216
4217 return start;
4218}