blob: bbe79ea53da077ee064d4c2a58ac9fb996299491 [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
Radek Krejcif9943642021-04-26 10:18:21 +020058lyd_value_store(const struct ly_ctx *ctx, struct lyd_value *val, const struct lysc_type *type, const void *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 Krejci6d5ba0c2021-04-26 07:49:59 +0200107 LOGVAL(ctx, LYVE_OTHER, "Resolving value \"%s\" failed.", type->plugin->print(ctx, val, LY_VALUE_CANON,
108 NULL, NULL, NULL));
Radek Krejci38d85362019-09-05 16:26:38 +0200109 }
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200110 return ret;
Radek Krejci38d85362019-09-05 16:26:38 +0200111 }
112
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200113 return LY_SUCCESS;
Radek Krejci38d85362019-09-05 16:26:38 +0200114}
115
Michal Vaskof937cfe2020-08-03 16:07:12 +0200116LY_ERR
Michal Vaskoaebbce02021-04-06 09:23:37 +0200117lys_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 +0200118 LY_VALUE_FORMAT format, void *prefix_data)
Radek Krejci084289f2019-07-09 17:35:30 +0200119{
120 LY_ERR rc = LY_SUCCESS;
121 struct ly_err_item *err = NULL;
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200122 struct lyd_value storage;
Radek Krejci084289f2019-07-09 17:35:30 +0200123 struct lysc_type *type;
Radek Krejci084289f2019-07-09 17:35:30 +0200124
125 LY_CHECK_ARG_RET(ctx, node, value, LY_EINVAL);
126
127 if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
128 LOGARG(ctx, node);
129 return LY_EINVAL;
130 }
131
Michal Vasko22df3f02020-08-24 13:29:22 +0200132 type = ((struct lysc_node_leaf *)node)->type;
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200133 rc = type->plugin->store(ctx ? ctx : node->module->ctx, type, value, value_len, 0, format, prefix_data,
Michal Vasko405cc9e2020-12-01 12:01:27 +0100134 LYD_HINT_SCHEMA, node, &storage, NULL, &err);
Radek Krejci73dead22019-07-11 16:46:16 +0200135 if (rc == LY_EINCOMPLETE) {
136 /* actually success since we do not provide the context tree and call validation with
137 * LY_TYPE_OPTS_INCOMPLETE_DATA */
138 rc = LY_SUCCESS;
139 } else if (rc && err) {
140 if (ctx) {
141 /* log only in case the ctx was provided as input parameter */
Radek Krejciddace2c2021-01-08 11:30:56 +0100142 LOG_LOCSET(NULL, NULL, err->path, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100143 LOGVAL(ctx, err->vecode, err->msg);
Radek Krejciddace2c2021-01-08 11:30:56 +0100144 LOG_LOCBACK(0, 0, 1, 0);
Radek Krejci084289f2019-07-09 17:35:30 +0200145 }
Radek Krejci73dead22019-07-11 16:46:16 +0200146 ly_err_free(err);
Radek Krejci084289f2019-07-09 17:35:30 +0200147 }
148
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200149 if (!rc) {
150 type->plugin->free(ctx ? ctx : node->module->ctx, &storage);
151 }
Radek Krejci084289f2019-07-09 17:35:30 +0200152 return rc;
153}
154
155API LY_ERR
Michal Vaskoaebbce02021-04-06 09:23:37 +0200156lyd_value_validate(const struct ly_ctx *ctx, const struct lysc_node *schema, const char *value, size_t value_len,
157 const struct lyd_node *ctx_node, const struct lysc_type **realtype, const char **canonical)
Radek Krejci084289f2019-07-09 17:35:30 +0200158{
159 LY_ERR rc;
160 struct ly_err_item *err = NULL;
161 struct lysc_type *type;
Michal Vasko3701af52020-08-03 14:29:38 +0200162 struct lyd_value val = {0};
Radek Krejci09c77442021-04-26 11:10:34 +0200163 ly_bool stored = 0, log = 1;
Radek Krejci084289f2019-07-09 17:35:30 +0200164
Michal Vaskoaebbce02021-04-06 09:23:37 +0200165 LY_CHECK_ARG_RET(ctx, schema, value, LY_EINVAL);
Radek Krejci084289f2019-07-09 17:35:30 +0200166
Radek Krejci09c77442021-04-26 11:10:34 +0200167 if (!ctx) {
168 ctx = schema->module->ctx;
169 log = 0;
170 }
Michal Vaskoaebbce02021-04-06 09:23:37 +0200171 type = ((struct lysc_node_leaf *)schema)->type;
172
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200173 /* store */
Radek Krejci09c77442021-04-26 11:10:34 +0200174 rc = type->plugin->store(ctx, type, value, value_len, 0, LY_VALUE_JSON, NULL,
Michal Vaskoaebbce02021-04-06 09:23:37 +0200175 LYD_HINT_DATA, schema, &val, NULL, &err);
176 if (!rc || (rc == LY_EINCOMPLETE)) {
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200177 stored = 1;
Michal Vaskoaebbce02021-04-06 09:23:37 +0200178 }
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200179
Michal Vaskoaebbce02021-04-06 09:23:37 +0200180 if (ctx_node && (rc == LY_EINCOMPLETE)) {
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200181 /* resolve */
Radek Krejci09c77442021-04-26 11:10:34 +0200182 rc = type->plugin->validate(ctx, type, ctx_node, ctx_node, &val, &err);
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200183 }
184
Michal Vaskoaebbce02021-04-06 09:23:37 +0200185 if (rc && (rc != LY_EINCOMPLETE) && err) {
Radek Krejci09c77442021-04-26 11:10:34 +0200186 if (log) {
Michal Vaskoaebbce02021-04-06 09:23:37 +0200187 /* log error */
188 if (err->path) {
189 LOG_LOCSET(NULL, NULL, err->path, NULL);
190 } else if (ctx_node) {
191 LOG_LOCSET(NULL, ctx_node, NULL, NULL);
192 } else {
193 LOG_LOCSET(schema, NULL, NULL, NULL);
Radek Krejci084289f2019-07-09 17:35:30 +0200194 }
Michal Vaskoaebbce02021-04-06 09:23:37 +0200195 LOGVAL(ctx, err->vecode, err->msg);
196 if (err->path) {
197 LOG_LOCBACK(0, 0, 1, 0);
198 } else if (ctx_node) {
199 LOG_LOCBACK(0, 1, 0, 0);
200 } else {
201 LOG_LOCBACK(1, 0, 0, 0);
202 }
Radek Krejci084289f2019-07-09 17:35:30 +0200203 }
Michal Vaskoaebbce02021-04-06 09:23:37 +0200204 ly_err_free(err);
Radek Krejci084289f2019-07-09 17:35:30 +0200205 }
206
Michal Vaskoaebbce02021-04-06 09:23:37 +0200207 if (!rc || (rc == LY_EINCOMPLETE)) {
208 if (realtype) {
209 /* return realtype */
210 if (val.realtype->basetype == LY_TYPE_UNION) {
211 *realtype = val.subvalue->value.realtype;
212 } else {
213 *realtype = val.realtype;
214 }
215 }
216
217 if (canonical) {
218 /* return canonical value */
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200219 lydict_insert(ctx, val.realtype->plugin->print(ctx, &val, LY_VALUE_CANON, NULL, NULL, NULL), 0, canonical);
Michal Vasko29134f82020-11-13 18:03:20 +0100220 }
Michal Vasko3701af52020-08-03 14:29:38 +0200221 }
222
Michal Vaskoaebbce02021-04-06 09:23:37 +0200223 if (stored) {
224 /* free value */
225 type->plugin->free(ctx ? ctx : schema->module->ctx, &val);
226 }
227 return rc;
Radek Krejci084289f2019-07-09 17:35:30 +0200228}
229
230API LY_ERR
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200231lyd_value_compare(const struct lyd_node_term *node, const char *value, size_t value_len)
Radek Krejci084289f2019-07-09 17:35:30 +0200232{
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200233 LY_ERR ret = LY_SUCCESS;
Radek Krejci084289f2019-07-09 17:35:30 +0200234 struct ly_ctx *ctx;
235 struct lysc_type *type;
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200236 struct lyd_value val = {0};
Radek Krejci084289f2019-07-09 17:35:30 +0200237
238 LY_CHECK_ARG_RET(node ? node->schema->module->ctx : NULL, node, value, LY_EINVAL);
239
240 ctx = node->schema->module->ctx;
Michal Vasko22df3f02020-08-24 13:29:22 +0200241 type = ((struct lysc_node_leaf *)node->schema)->type;
Radek Krejci084289f2019-07-09 17:35:30 +0200242
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200243 /* store the value */
Michal Vasko9e685082021-01-29 14:49:09 +0100244 LOG_LOCSET(node->schema, &node->node, NULL, NULL);
Radek Krejci8df109d2021-04-23 12:19:08 +0200245 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 +0100246 LOG_LOCBACK(1, 1, 0, 0);
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200247 LY_CHECK_RET(ret);
Radek Krejci084289f2019-07-09 17:35:30 +0200248
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200249 /* compare values */
250 ret = type->plugin->compare(&node->value, &val);
Radek Krejci084289f2019-07-09 17:35:30 +0200251
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200252 type->plugin->free(ctx, &val);
Radek Krejci084289f2019-07-09 17:35:30 +0200253 return ret;
254}
255
Radek Krejci19611252020-10-04 13:54:53 +0200256API ly_bool
257lyd_is_default(const struct lyd_node *node)
258{
259 const struct lysc_node_leaf *leaf;
260 const struct lysc_node_leaflist *llist;
261 const struct lyd_node_term *term;
262 LY_ARRAY_COUNT_TYPE u;
263
264 assert(node->schema->nodetype & LYD_NODE_TERM);
265 term = (const struct lyd_node_term *)node;
266
267 if (node->schema->nodetype == LYS_LEAF) {
268 leaf = (const struct lysc_node_leaf *)node->schema;
269 if (!leaf->dflt) {
270 return 0;
271 }
272
273 /* compare with the default value */
274 if (leaf->type->plugin->compare(&term->value, leaf->dflt)) {
275 return 0;
276 }
277 } else {
278 llist = (const struct lysc_node_leaflist *)node->schema;
279 if (!llist->dflts) {
280 return 0;
281 }
282
283 LY_ARRAY_FOR(llist->dflts, u) {
284 /* compare with each possible default value */
285 if (llist->type->plugin->compare(&term->value, llist->dflts[u])) {
286 return 0;
287 }
288 }
289 }
290
291 return 1;
292}
293
Radek Krejci7931b192020-06-25 17:05:03 +0200294static LYD_FORMAT
Michal Vasko63f3d842020-07-08 10:10:14 +0200295lyd_parse_get_format(const struct ly_in *in, LYD_FORMAT format)
Radek Krejcie7b95092019-05-15 11:03:07 +0200296{
Michal Vasko69730152020-10-09 16:30:07 +0200297 if (!format && (in->type == LY_IN_FILEPATH)) {
Radek Krejcie7b95092019-05-15 11:03:07 +0200298 /* unknown format - try to detect it from filename's suffix */
Radek Krejci7931b192020-06-25 17:05:03 +0200299 const char *path = in->method.fpath.filepath;
300 size_t len = strlen(path);
Radek Krejcie7b95092019-05-15 11:03:07 +0200301
302 /* ignore trailing whitespaces */
Michal Vaskod989ba02020-08-24 10:59:24 +0200303 for ( ; len > 0 && isspace(path[len - 1]); len--) {}
Radek Krejcie7b95092019-05-15 11:03:07 +0200304
Radek Krejcif13b87b2020-12-01 22:02:17 +0100305 if ((len >= LY_XML_SUFFIX_LEN + 1) &&
306 !strncmp(&path[len - LY_XML_SUFFIX_LEN], LY_XML_SUFFIX, LY_XML_SUFFIX_LEN)) {
Radek Krejcie7b95092019-05-15 11:03:07 +0200307 format = LYD_XML;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100308 } else if ((len >= LY_JSON_SUFFIX_LEN + 1) &&
309 !strncmp(&path[len - LY_JSON_SUFFIX_LEN], LY_JSON_SUFFIX, LY_JSON_SUFFIX_LEN)) {
Radek Krejcie7b95092019-05-15 11:03:07 +0200310 format = LYD_JSON;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100311 } else if ((len >= LY_LYB_SUFFIX_LEN + 1) &&
312 !strncmp(&path[len - LY_LYB_SUFFIX_LEN], LY_LYB_SUFFIX, LY_LYB_SUFFIX_LEN)) {
Radek Krejcie7b95092019-05-15 11:03:07 +0200313 format = LYD_LYB;
Radek Krejci7931b192020-06-25 17:05:03 +0200314 } /* else still unknown */
Radek Krejcie7b95092019-05-15 11:03:07 +0200315 }
316
Radek Krejci7931b192020-06-25 17:05:03 +0200317 return format;
318}
Radek Krejcie7b95092019-05-15 11:03:07 +0200319
Michal Vaskoe0665742021-02-11 11:08:44 +0100320/**
321 * @brief Parse YANG data into a data tree.
322 *
323 * @param[in] ctx libyang context.
Radek Krejcif16e2542021-02-17 15:39:23 +0100324 * @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 +0100325 * @param[in] parent Parent to connect the parsed nodes to, if any.
326 * @param[in,out] first_p Pointer to the first top-level parsed node, used only if @p parent is NULL.
327 * @param[in] in Input handle to read the input from.
328 * @param[in] format Expected format of the data in @p in.
329 * @param[in] parse_opts Options for parser.
330 * @param[in] val_opts Options for validation.
331 * @param[out] op Optional pointer to the parsed operation, if any.
332 * @return LY_ERR value.
333 */
334static LY_ERR
Radek Krejcif16e2542021-02-17 15:39:23 +0100335lyd_parse(const struct ly_ctx *ctx, const struct lysc_ext_instance *ext, struct lyd_node *parent, struct lyd_node **first_p,
336 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 +0200337{
Michal Vaskoe0665742021-02-11 11:08:44 +0100338 LY_ERR rc = LY_SUCCESS;
Radek Krejci1798aae2020-07-14 13:26:06 +0200339 struct lyd_ctx *lydctx = NULL;
Michal Vaskoe0665742021-02-11 11:08:44 +0100340 struct ly_set parsed = {0};
341 struct lyd_node *first;
342 uint32_t i;
Radek Krejci1798aae2020-07-14 13:26:06 +0200343
Michal Vaskoe0665742021-02-11 11:08:44 +0100344 assert(ctx && (parent || first_p));
Radek Krejci7931b192020-06-25 17:05:03 +0200345
346 format = lyd_parse_get_format(in, format);
Michal Vaskoe0665742021-02-11 11:08:44 +0100347 if (first_p) {
348 *first_p = NULL;
349 }
Radek Krejci1798aae2020-07-14 13:26:06 +0200350
Michal Vasko63f3d842020-07-08 10:10:14 +0200351 /* remember input position */
352 in->func_start = in->current;
Radek Krejci7931b192020-06-25 17:05:03 +0200353
Michal Vaskoe0665742021-02-11 11:08:44 +0100354 /* parse the data */
Radek Krejci7931b192020-06-25 17:05:03 +0200355 switch (format) {
356 case LYD_XML:
Radek Krejcif16e2542021-02-17 15:39:23 +0100357 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 +0200358 break;
Radek Krejci7931b192020-06-25 17:05:03 +0200359 case LYD_JSON:
Radek Krejcif16e2542021-02-17 15:39:23 +0100360 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 +0200361 break;
Radek Krejci7931b192020-06-25 17:05:03 +0200362 case LYD_LYB:
Radek Krejcif16e2542021-02-17 15:39:23 +0100363 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 +0200364 break;
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200365 case LYD_UNKNOWN:
Michal Vaskod74978f2021-02-12 11:59:36 +0100366 LOGARG(ctx, format);
367 rc = LY_EINVAL;
Michal Vaskoe0665742021-02-11 11:08:44 +0100368 break;
369 }
370 LY_CHECK_GOTO(rc, cleanup);
371
372 if (parent) {
373 /* get first top-level sibling */
374 for (first = parent; first->parent; first = lyd_parent(first)) {}
375 first = lyd_first_sibling(first);
376 first_p = &first;
Radek Krejci7931b192020-06-25 17:05:03 +0200377 }
378
Michal Vaskoe0665742021-02-11 11:08:44 +0100379 if (!(parse_opts & LYD_PARSE_ONLY)) {
380 /* validate data */
Radek Krejci4f2e3e52021-03-30 14:20:28 +0200381 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 +0100382 &lydctx->meta_types, NULL);
383 LY_CHECK_GOTO(rc, cleanup);
384 }
Radek Krejci7931b192020-06-25 17:05:03 +0200385
Michal Vaskoe0665742021-02-11 11:08:44 +0100386 /* set the operation node */
387 if (op) {
388 *op = lydctx->op_node;
Radek Krejci1798aae2020-07-14 13:26:06 +0200389 }
390
391cleanup:
Michal Vaskoe0665742021-02-11 11:08:44 +0100392 if (lydctx) {
393 lydctx->free(lydctx);
Radek Krejci1798aae2020-07-14 13:26:06 +0200394 }
Michal Vaskoe0665742021-02-11 11:08:44 +0100395 if (rc) {
396 if (parent) {
397 /* free all the parsed subtrees */
398 for (i = 0; i < parsed.count; ++i) {
399 lyd_free_tree(parsed.dnodes[i]);
400 }
401 } else {
402 /* free everything */
403 lyd_free_all(*first_p);
404 *first_p = NULL;
405 }
406 }
407 ly_set_erase(&parsed, NULL);
408 return rc;
Radek Krejci7931b192020-06-25 17:05:03 +0200409}
410
411API LY_ERR
Radek Krejcif16e2542021-02-17 15:39:23 +0100412lyd_parse_ext_data(const struct lysc_ext_instance *ext, struct lyd_node *parent, struct ly_in *in, LYD_FORMAT format,
413 uint32_t parse_options, uint32_t validate_options, struct lyd_node **tree)
414{
415 const struct ly_ctx *ctx = ext ? ext->module->ctx : NULL;
416
417 LY_CHECK_ARG_RET(ctx, ext, in, parent || tree, LY_EINVAL);
418 LY_CHECK_ARG_RET(ctx, !(parse_options & ~LYD_PARSE_OPTS_MASK), LY_EINVAL);
419 LY_CHECK_ARG_RET(ctx, !(validate_options & ~LYD_VALIDATE_OPTS_MASK), LY_EINVAL);
420
421 return lyd_parse(ctx, ext, parent, tree, in, format, parse_options, validate_options, NULL);
422}
423
424API LY_ERR
Michal Vaskoe0665742021-02-11 11:08:44 +0100425lyd_parse_data(const struct ly_ctx *ctx, struct lyd_node *parent, struct ly_in *in, LYD_FORMAT format,
426 uint32_t parse_options, uint32_t validate_options, struct lyd_node **tree)
427{
428 LY_CHECK_ARG_RET(ctx, ctx, in, parent || tree, LY_EINVAL);
429 LY_CHECK_ARG_RET(ctx, !(parse_options & ~LYD_PARSE_OPTS_MASK), LY_EINVAL);
430 LY_CHECK_ARG_RET(ctx, !(validate_options & ~LYD_VALIDATE_OPTS_MASK), LY_EINVAL);
431
Radek Krejcif16e2542021-02-17 15:39:23 +0100432 return lyd_parse(ctx, NULL, parent, tree, in, format, parse_options, validate_options, NULL);
Michal Vaskoe0665742021-02-11 11:08:44 +0100433}
434
435API LY_ERR
436lyd_parse_data_mem(const struct ly_ctx *ctx, const char *data, LYD_FORMAT format, uint32_t parse_options,
437 uint32_t validate_options, struct lyd_node **tree)
Radek Krejci7931b192020-06-25 17:05:03 +0200438{
439 LY_ERR ret;
440 struct ly_in *in;
441
442 LY_CHECK_RET(ly_in_new_memory(data, &in));
Michal Vaskoe0665742021-02-11 11:08:44 +0100443 ret = lyd_parse_data(ctx, NULL, in, format, parse_options, validate_options, tree);
Radek Krejci7931b192020-06-25 17:05:03 +0200444
445 ly_in_free(in, 0);
446 return ret;
447}
448
449API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200450lyd_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 +0200451 struct lyd_node **tree)
Radek Krejci7931b192020-06-25 17:05:03 +0200452{
453 LY_ERR ret;
454 struct ly_in *in;
455
456 LY_CHECK_RET(ly_in_new_fd(fd, &in));
Michal Vaskoe0665742021-02-11 11:08:44 +0100457 ret = lyd_parse_data(ctx, NULL, in, format, parse_options, validate_options, tree);
Radek Krejci7931b192020-06-25 17:05:03 +0200458
459 ly_in_free(in, 0);
460 return ret;
461}
462
463API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200464lyd_parse_data_path(const struct ly_ctx *ctx, const char *path, LYD_FORMAT format, uint32_t parse_options,
465 uint32_t validate_options, struct lyd_node **tree)
Radek Krejci7931b192020-06-25 17:05:03 +0200466{
467 LY_ERR ret;
468 struct ly_in *in;
469
470 LY_CHECK_RET(ly_in_new_filepath(path, 0, &in));
Michal Vaskoe0665742021-02-11 11:08:44 +0100471 ret = lyd_parse_data(ctx, NULL, in, format, parse_options, validate_options, tree);
Radek Krejci7931b192020-06-25 17:05:03 +0200472
473 ly_in_free(in, 0);
474 return ret;
475}
476
Radek Krejcif16e2542021-02-17 15:39:23 +0100477/**
478 * @brief Parse YANG data into an operation data tree, in case the extension instance is specified, keep the searching
479 * for schema nodes locked inside the extension instance.
480 *
481 * At least one of @p parent, @p tree, or @p op must always be set.
482 *
483 * Specific @p data_type values have different parameter meaning as follows:
484 * - ::LYD_TYPE_RPC_NETCONF:
485 * - @p parent - must be NULL, the whole RPC is expected;
486 * - @p format - must be ::LYD_XML, NETCONF supports only this format;
487 * - @p tree - must be provided, all the NETCONF-specific XML envelopes will be returned here as
488 * a separate opaque data tree, even if the function fails, this may be returned;
489 * - @p op - must be provided, the RPC/action data tree itself will be returned here, pointing to the operation;
490 *
491 * - ::LYD_TYPE_NOTIF_NETCONF:
492 * - @p parent - must be NULL, the whole notification is expected;
493 * - @p format - must be ::LYD_XML, NETCONF supports only this format;
494 * - @p tree - must be provided, all the NETCONF-specific XML envelopes will be returned here as
495 * a separate opaque data tree, even if the function fails, this may be returned;
496 * - @p op - must be provided, the notification data tree itself will be returned here, pointing to the operation;
497 *
498 * - ::LYD_TYPE_REPLY_NETCONF:
499 * - @p parent - must be set, pointing to the invoked RPC operation (RPC or action) node;
500 * - @p format - must be ::LYD_XML, NETCONF supports only this format;
501 * - @p tree - must be provided, all the NETCONF-specific XML envelopes will be returned here as
502 * a separate opaque data tree, even if the function fails, this may be returned;
503 * - @p op - must be NULL, the reply is appended to the RPC;
504 * Note that there are 3 kinds of NETCONF replies - ok, error, and data. Only data reply appends any nodes to the RPC.
505 *
506 * @param[in] ctx libyang context.
507 * @param[in] ext Extension instance providing the specific schema tree to match with the data being parsed.
508 * @param[in] parent Optional parent to connect the parsed nodes to.
509 * @param[in] in Input handle to read the input from.
510 * @param[in] format Expected format of the data in @p in.
511 * @param[in] data_type Expected operation to parse (@ref datatype).
512 * @param[out] tree Optional full parsed data tree. If @p parent is set, set to NULL.
513 * @param[out] op Optional parsed operation node.
514 * @return LY_ERR value.
515 * @return LY_ENOT if @p data_type is a NETCONF message and the root XML element is not the expected one.
516 */
517static LY_ERR
518lyd_parse_op_(const struct ly_ctx *ctx, const struct lysc_ext_instance *ext, struct lyd_node *parent,
519 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 +0200520{
Michal Vaskoe0665742021-02-11 11:08:44 +0100521 LY_ERR rc = LY_SUCCESS;
522 struct lyd_ctx *lydctx = NULL;
523 struct ly_set parsed = {0};
524 struct lyd_node *first = NULL, *envp = NULL;
525 uint32_t i, parse_opts, val_opts;
Radek Krejci7931b192020-06-25 17:05:03 +0200526
Michal Vasko27fb0262021-02-23 09:42:01 +0100527 if (!ctx) {
528 ctx = LYD_CTX(parent);
529 }
Radek Krejci1798aae2020-07-14 13:26:06 +0200530 if (tree) {
531 *tree = NULL;
532 }
533 if (op) {
534 *op = NULL;
535 }
536
Radek Krejci7931b192020-06-25 17:05:03 +0200537 format = lyd_parse_get_format(in, format);
Radek Krejci7931b192020-06-25 17:05:03 +0200538
Michal Vasko63f3d842020-07-08 10:10:14 +0200539 /* remember input position */
540 in->func_start = in->current;
541
Michal Vaskoe0665742021-02-11 11:08:44 +0100542 /* check params based on the data type */
Michal Vasko1e4c68e2021-02-18 15:03:01 +0100543 if ((data_type == LYD_TYPE_RPC_NETCONF) || (data_type == LYD_TYPE_NOTIF_NETCONF)) {
Michal Vaskoe0665742021-02-11 11:08:44 +0100544 LY_CHECK_ARG_RET(ctx, format == LYD_XML, !parent, tree, op, LY_EINVAL);
Michal Vasko1e4c68e2021-02-18 15:03:01 +0100545 } else if (data_type == LYD_TYPE_REPLY_NETCONF) {
546 LY_CHECK_ARG_RET(ctx, format == LYD_XML, parent, parent->schema->nodetype & (LYS_RPC | LYS_ACTION), tree, !op,
547 LY_EINVAL);
Michal Vaskoe0665742021-02-11 11:08:44 +0100548 }
549 parse_opts = LYD_PARSE_ONLY | LYD_PARSE_STRICT;
550 val_opts = 0;
551
552 /* parse the data */
Radek Krejci7931b192020-06-25 17:05:03 +0200553 switch (format) {
554 case LYD_XML:
Radek Krejcif16e2542021-02-17 15:39:23 +0100555 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 +0100556 if (rc && envp) {
557 /* special situation when the envelopes were parsed successfully */
558 if (tree) {
559 *tree = envp;
560 }
561 ly_set_erase(&parsed, NULL);
562 return rc;
563 }
Michal Vaskoe0665742021-02-11 11:08:44 +0100564 break;
Radek Krejci7931b192020-06-25 17:05:03 +0200565 case LYD_JSON:
Radek Krejcif16e2542021-02-17 15:39:23 +0100566 rc = lyd_parse_json(ctx, ext, parent, &first, in, parse_opts, val_opts, data_type, &parsed, &lydctx);
Michal Vaskoe0665742021-02-11 11:08:44 +0100567 break;
Radek Krejci7931b192020-06-25 17:05:03 +0200568 case LYD_LYB:
Radek Krejcif16e2542021-02-17 15:39:23 +0100569 rc = lyd_parse_lyb(ctx, ext, parent, &first, in, parse_opts, val_opts, data_type, &parsed, &lydctx);
Michal Vaskoe0665742021-02-11 11:08:44 +0100570 break;
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200571 case LYD_UNKNOWN:
Michal Vaskod74978f2021-02-12 11:59:36 +0100572 LOGARG(ctx, format);
573 rc = LY_EINVAL;
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200574 break;
Radek Krejci7931b192020-06-25 17:05:03 +0200575 }
Michal Vaskoe0665742021-02-11 11:08:44 +0100576 LY_CHECK_GOTO(rc, cleanup);
Radek Krejci7931b192020-06-25 17:05:03 +0200577
Michal Vaskoe0665742021-02-11 11:08:44 +0100578 /* set out params correctly */
579 if (tree) {
580 if (envp) {
581 /* special out param meaning */
582 *tree = envp;
583 } else {
584 *tree = parent ? NULL : first;
585 }
586 }
587 if (op) {
588 *op = lydctx->op_node;
589 }
590
591cleanup:
592 if (lydctx) {
593 lydctx->free(lydctx);
594 }
595 if (rc) {
596 if (parent) {
597 /* free all the parsed subtrees */
598 for (i = 0; i < parsed.count; ++i) {
599 lyd_free_tree(parsed.dnodes[i]);
600 }
601 } else {
602 /* free everything (cannot occur in the current code, a safety) */
603 lyd_free_all(first);
604 if (tree) {
605 *tree = NULL;
606 }
607 if (op) {
608 *op = NULL;
609 }
610 }
611 }
612 ly_set_erase(&parsed, NULL);
613 return rc;
Radek Krejcie7b95092019-05-15 11:03:07 +0200614}
Radek Krejci084289f2019-07-09 17:35:30 +0200615
Radek Krejcif16e2542021-02-17 15:39:23 +0100616API LY_ERR
617lyd_parse_op(const struct ly_ctx *ctx, struct lyd_node *parent, struct ly_in *in, LYD_FORMAT format,
618 enum lyd_type data_type, struct lyd_node **tree, struct lyd_node **op)
619{
620 LY_CHECK_ARG_RET(ctx, ctx || parent, in, data_type, parent || tree || op, LY_EINVAL);
621
622 return lyd_parse_op_(ctx, NULL, parent, in, format, data_type, tree, op);
623}
624
625API LY_ERR
626lyd_parse_ext_op(const struct lysc_ext_instance *ext, struct lyd_node *parent, struct ly_in *in, LYD_FORMAT format,
627 enum lyd_type data_type, struct lyd_node **tree, struct lyd_node **op)
628{
629 const struct ly_ctx *ctx = ext ? ext->module->ctx : NULL;
630
631 LY_CHECK_ARG_RET(ctx, ext, in, data_type, parent || tree || op, LY_EINVAL);
632
633 return lyd_parse_op_(ctx, ext, parent, in, format, data_type, tree, op);
634}
635
Michal Vasko90932a92020-02-12 14:33:03 +0100636LY_ERR
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200637lyd_create_term(const struct lysc_node *schema, const char *value, size_t value_len, ly_bool *dynamic,
Radek Krejci8df109d2021-04-23 12:19:08 +0200638 LY_VALUE_FORMAT format, void *prefix_data, uint32_t hints, ly_bool *incomplete, struct lyd_node **node)
Michal Vasko90932a92020-02-12 14:33:03 +0100639{
640 LY_ERR ret;
641 struct lyd_node_term *term;
642
Michal Vasko9b368d32020-02-14 13:53:31 +0100643 assert(schema->nodetype & LYD_NODE_TERM);
644
Michal Vasko90932a92020-02-12 14:33:03 +0100645 term = calloc(1, sizeof *term);
646 LY_CHECK_ERR_RET(!term, LOGMEM(schema->module->ctx), LY_EMEM);
647
648 term->schema = schema;
Michal Vasko9e685082021-01-29 14:49:09 +0100649 term->prev = &term->node;
Michal Vasko9b368d32020-02-14 13:53:31 +0100650 term->flags = LYD_NEW;
Michal Vasko90932a92020-02-12 14:33:03 +0100651
Radek Krejciddace2c2021-01-08 11:30:56 +0100652 LOG_LOCSET(schema, NULL, NULL, NULL);
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200653 ret = lyd_value_store(schema->module->ctx, &term->value, ((struct lysc_node_leaf *)term->schema)->type, value,
Radek Krejci2efc45b2020-12-22 16:25:44 +0100654 value_len, dynamic, format, prefix_data, hints, schema, incomplete);
Radek Krejciddace2c2021-01-08 11:30:56 +0100655 LOG_LOCBACK(1, 0, 0, 0);
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200656 LY_CHECK_ERR_RET(ret, free(term), ret);
Michal Vasko9e685082021-01-29 14:49:09 +0100657 lyd_hash(&term->node);
Michal Vasko9b368d32020-02-14 13:53:31 +0100658
Michal Vasko9e685082021-01-29 14:49:09 +0100659 *node = &term->node;
Michal Vasko9b368d32020-02-14 13:53:31 +0100660 return ret;
661}
662
663LY_ERR
664lyd_create_term2(const struct lysc_node *schema, const struct lyd_value *val, struct lyd_node **node)
665{
666 LY_ERR ret;
667 struct lyd_node_term *term;
668 struct lysc_type *type;
669
670 assert(schema->nodetype & LYD_NODE_TERM);
Radek Krejci224d4b42021-04-23 13:54:59 +0200671 assert(val && val->realtype);
Michal Vasko9b368d32020-02-14 13:53:31 +0100672
673 term = calloc(1, sizeof *term);
674 LY_CHECK_ERR_RET(!term, LOGMEM(schema->module->ctx), LY_EMEM);
675
676 term->schema = schema;
Michal Vasko9e685082021-01-29 14:49:09 +0100677 term->prev = &term->node;
Michal Vasko9b368d32020-02-14 13:53:31 +0100678 term->flags = LYD_NEW;
679
680 type = ((struct lysc_node_leaf *)schema)->type;
681 ret = type->plugin->duplicate(schema->module->ctx, val, &term->value);
682 if (ret) {
683 LOGERR(schema->module->ctx, ret, "Value duplication failed.");
684 free(term);
685 return ret;
686 }
Michal Vasko9e685082021-01-29 14:49:09 +0100687 lyd_hash(&term->node);
Michal Vasko90932a92020-02-12 14:33:03 +0100688
Michal Vasko9e685082021-01-29 14:49:09 +0100689 *node = &term->node;
Michal Vasko90932a92020-02-12 14:33:03 +0100690 return ret;
691}
692
693LY_ERR
694lyd_create_inner(const struct lysc_node *schema, struct lyd_node **node)
695{
696 struct lyd_node_inner *in;
697
Michal Vasko9b368d32020-02-14 13:53:31 +0100698 assert(schema->nodetype & LYD_NODE_INNER);
699
Michal Vasko90932a92020-02-12 14:33:03 +0100700 in = calloc(1, sizeof *in);
701 LY_CHECK_ERR_RET(!in, LOGMEM(schema->module->ctx), LY_EMEM);
702
703 in->schema = schema;
Michal Vasko9e685082021-01-29 14:49:09 +0100704 in->prev = &in->node;
Michal Vasko9b368d32020-02-14 13:53:31 +0100705 in->flags = LYD_NEW;
Michal Vasko3383be72020-11-03 17:15:31 +0100706 if ((schema->nodetype == LYS_CONTAINER) && !(schema->flags & LYS_PRESENCE)) {
707 in->flags |= LYD_DEFAULT;
708 }
Michal Vasko90932a92020-02-12 14:33:03 +0100709
Michal Vasko9b368d32020-02-14 13:53:31 +0100710 /* do not hash list with keys, we need them for the hash */
711 if ((schema->nodetype != LYS_LIST) || (schema->flags & LYS_KEYLESS)) {
Michal Vasko9e685082021-01-29 14:49:09 +0100712 lyd_hash(&in->node);
Michal Vasko9b368d32020-02-14 13:53:31 +0100713 }
Michal Vasko90932a92020-02-12 14:33:03 +0100714
Michal Vasko9e685082021-01-29 14:49:09 +0100715 *node = &in->node;
Michal Vasko90932a92020-02-12 14:33:03 +0100716 return LY_SUCCESS;
717}
718
Michal Vasko90932a92020-02-12 14:33:03 +0100719LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +0200720lyd_create_list(const struct lysc_node *schema, const struct ly_path_predicate *predicates, struct lyd_node **node)
Michal Vasko90932a92020-02-12 14:33:03 +0100721{
722 LY_ERR ret = LY_SUCCESS;
Michal Vasko90932a92020-02-12 14:33:03 +0100723 struct lyd_node *list = NULL, *key;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200724 LY_ARRAY_COUNT_TYPE u;
Michal Vasko90932a92020-02-12 14:33:03 +0100725
Michal Vasko004d3152020-06-11 19:59:22 +0200726 assert((schema->nodetype == LYS_LIST) && !(schema->flags & LYS_KEYLESS));
Michal Vasko90932a92020-02-12 14:33:03 +0100727
728 /* create list */
729 LY_CHECK_GOTO(ret = lyd_create_inner(schema, &list), cleanup);
730
Radek Krejciddace2c2021-01-08 11:30:56 +0100731 LOG_LOCSET(NULL, list, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100732
Michal Vasko90932a92020-02-12 14:33:03 +0100733 /* create and insert all the keys */
Michal Vasko004d3152020-06-11 19:59:22 +0200734 LY_ARRAY_FOR(predicates, u) {
735 LY_CHECK_GOTO(ret = lyd_create_term2(predicates[u].key, &predicates[u].value, &key), cleanup);
Michal Vasko90932a92020-02-12 14:33:03 +0100736 lyd_insert_node(list, NULL, key);
737 }
738
Michal Vasko9b368d32020-02-14 13:53:31 +0100739 /* hash having all the keys */
740 lyd_hash(list);
741
Michal Vasko90932a92020-02-12 14:33:03 +0100742 /* success */
743 *node = list;
744 list = NULL;
745
746cleanup:
Radek Krejciddace2c2021-01-08 11:30:56 +0100747 LOG_LOCBACK(0, 1, 0, 0);
Michal Vasko90932a92020-02-12 14:33:03 +0100748 lyd_free_tree(list);
Michal Vasko004d3152020-06-11 19:59:22 +0200749 return ret;
750}
751
752static LY_ERR
753lyd_create_list2(const struct lysc_node *schema, const char *keys, size_t keys_len, struct lyd_node **node)
754{
755 LY_ERR ret = LY_SUCCESS;
756 struct lyxp_expr *expr = NULL;
757 uint16_t exp_idx = 0;
758 enum ly_path_pred_type pred_type = 0;
759 struct ly_path_predicate *predicates = NULL;
760
Radek Krejciddace2c2021-01-08 11:30:56 +0100761 LOG_LOCSET(schema, NULL, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100762
Michal Vasko004d3152020-06-11 19:59:22 +0200763 /* parse keys */
Michal Vasko6b26e742020-07-17 15:02:10 +0200764 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 +0200765 LY_PATH_PRED_KEYS, &expr), cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +0200766
767 /* compile them */
Radek Krejci8df109d2021-04-23 12:19:08 +0200768 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 +0200769 NULL, &predicates, &pred_type), cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +0200770
771 /* create the list node */
772 LY_CHECK_GOTO(ret = lyd_create_list(schema, predicates, node), cleanup);
773
774cleanup:
Radek Krejciddace2c2021-01-08 11:30:56 +0100775 LOG_LOCBACK(1, 0, 0, 0);
Michal Vasko004d3152020-06-11 19:59:22 +0200776 lyxp_expr_free(schema->module->ctx, expr);
Michal Vaskof7e16e22020-10-21 09:24:39 +0200777 ly_path_predicates_free(schema->module->ctx, pred_type, predicates);
Michal Vasko90932a92020-02-12 14:33:03 +0100778 return ret;
779}
780
781LY_ERR
Michal Vasko366a4a12020-12-04 16:23:57 +0100782lyd_create_any(const struct lysc_node *schema, const void *value, LYD_ANYDATA_VALUETYPE value_type, ly_bool use_value,
783 struct lyd_node **node)
Michal Vasko90932a92020-02-12 14:33:03 +0100784{
Michal Vasko366a4a12020-12-04 16:23:57 +0100785 LY_ERR ret;
Michal Vasko90932a92020-02-12 14:33:03 +0100786 struct lyd_node_any *any;
Michal Vasko366a4a12020-12-04 16:23:57 +0100787 union lyd_any_value any_val;
Michal Vasko90932a92020-02-12 14:33:03 +0100788
Michal Vasko9b368d32020-02-14 13:53:31 +0100789 assert(schema->nodetype & LYD_NODE_ANY);
790
Michal Vasko90932a92020-02-12 14:33:03 +0100791 any = calloc(1, sizeof *any);
792 LY_CHECK_ERR_RET(!any, LOGMEM(schema->module->ctx), LY_EMEM);
793
794 any->schema = schema;
Michal Vasko9e685082021-01-29 14:49:09 +0100795 any->prev = &any->node;
Michal Vasko9b368d32020-02-14 13:53:31 +0100796 any->flags = LYD_NEW;
Michal Vasko90932a92020-02-12 14:33:03 +0100797
Radek Krejci1798aae2020-07-14 13:26:06 +0200798 /* TODO: convert XML/JSON strings into a opaq data tree */
Michal Vasko366a4a12020-12-04 16:23:57 +0100799
800 if (use_value) {
801 any->value.str = value;
802 any->value_type = value_type;
803 } else {
804 any_val.str = value;
Michal Vasko9e685082021-01-29 14:49:09 +0100805 ret = lyd_any_copy_value(&any->node, &any_val, value_type);
Michal Vasko366a4a12020-12-04 16:23:57 +0100806 LY_CHECK_ERR_RET(ret, free(any), ret);
807 }
Michal Vasko9e685082021-01-29 14:49:09 +0100808 lyd_hash(&any->node);
Michal Vasko90932a92020-02-12 14:33:03 +0100809
Michal Vasko9e685082021-01-29 14:49:09 +0100810 *node = &any->node;
Michal Vasko90932a92020-02-12 14:33:03 +0100811 return LY_SUCCESS;
812}
813
Michal Vasko52927e22020-03-16 17:26:14 +0100814LY_ERR
Michal Vasko501af032020-11-11 20:27:44 +0100815lyd_create_opaq(const struct ly_ctx *ctx, const char *name, size_t name_len, const char *prefix, size_t pref_len,
816 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 +0200817 LY_VALUE_FORMAT format, void *val_prefix_data, uint32_t hints, struct lyd_node **node)
Michal Vasko52927e22020-03-16 17:26:14 +0100818{
Radek Krejci011e4aa2020-09-04 15:22:31 +0200819 LY_ERR ret = LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +0100820 struct lyd_node_opaq *opaq;
821
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200822 assert(ctx && name && name_len && format);
Michal Vasko52927e22020-03-16 17:26:14 +0100823
824 if (!value_len) {
825 value = "";
826 }
827
828 opaq = calloc(1, sizeof *opaq);
Michal Vasko501af032020-11-11 20:27:44 +0100829 LY_CHECK_ERR_GOTO(!opaq, LOGMEM(ctx); ret = LY_EMEM, finish);
Michal Vasko52927e22020-03-16 17:26:14 +0100830
Michal Vasko9e685082021-01-29 14:49:09 +0100831 opaq->prev = &opaq->node;
Michal Vaskoad92b672020-11-12 13:11:31 +0100832 LY_CHECK_GOTO(ret = lydict_insert(ctx, name, name_len, &opaq->name.name), finish);
Radek Krejci011e4aa2020-09-04 15:22:31 +0200833
Michal Vasko52927e22020-03-16 17:26:14 +0100834 if (pref_len) {
Michal Vaskoad92b672020-11-12 13:11:31 +0100835 LY_CHECK_GOTO(ret = lydict_insert(ctx, prefix, pref_len, &opaq->name.prefix), finish);
Michal Vasko52927e22020-03-16 17:26:14 +0100836 }
Radek Krejci1798aae2020-07-14 13:26:06 +0200837 if (module_key_len) {
Michal Vaskoad92b672020-11-12 13:11:31 +0100838 LY_CHECK_GOTO(ret = lydict_insert(ctx, module_key, module_key_len, &opaq->name.module_ns), finish);
Radek Krejci1798aae2020-07-14 13:26:06 +0200839 }
Michal Vasko52927e22020-03-16 17:26:14 +0100840 if (dynamic && *dynamic) {
Radek Krejci011e4aa2020-09-04 15:22:31 +0200841 LY_CHECK_GOTO(ret = lydict_insert_zc(ctx, (char *)value, &opaq->value), finish);
Michal Vasko52927e22020-03-16 17:26:14 +0100842 *dynamic = 0;
843 } else {
Radek Krejci011e4aa2020-09-04 15:22:31 +0200844 LY_CHECK_GOTO(ret = lydict_insert(ctx, value, value_len, &opaq->value), finish);
Michal Vasko52927e22020-03-16 17:26:14 +0100845 }
Michal Vasko501af032020-11-11 20:27:44 +0100846
847 opaq->format = format;
848 opaq->val_prefix_data = val_prefix_data;
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200849 opaq->hints = hints;
Michal Vasko52927e22020-03-16 17:26:14 +0100850 opaq->ctx = ctx;
851
Radek Krejci011e4aa2020-09-04 15:22:31 +0200852finish:
853 if (ret) {
Michal Vasko9e685082021-01-29 14:49:09 +0100854 lyd_free_tree(&opaq->node);
Michal Vasko501af032020-11-11 20:27:44 +0100855 ly_free_prefix_data(format, val_prefix_data);
Radek Krejci011e4aa2020-09-04 15:22:31 +0200856 } else {
Michal Vasko9e685082021-01-29 14:49:09 +0100857 *node = &opaq->node;
Radek Krejci011e4aa2020-09-04 15:22:31 +0200858 }
859 return ret;
Michal Vasko52927e22020-03-16 17:26:14 +0100860}
861
Michal Vasko3a41dff2020-07-15 14:30:28 +0200862API LY_ERR
Michal Vasko65243892020-12-04 16:26:21 +0100863lyd_new_inner(struct lyd_node *parent, const struct lys_module *module, const char *name, ly_bool output,
864 struct lyd_node **node)
Michal Vasko013a8182020-03-03 10:46:53 +0100865{
866 struct lyd_node *ret = NULL;
867 const struct lysc_node *schema;
868 struct ly_ctx *ctx = parent ? parent->schema->module->ctx : (module ? module->ctx : NULL);
869
Michal Vasko6027eb92020-07-15 16:37:30 +0200870 LY_CHECK_ARG_RET(ctx, parent || module, parent || node, name, LY_EINVAL);
Michal Vasko013a8182020-03-03 10:46:53 +0100871
Michal Vaskof03ed032020-03-04 13:31:44 +0100872 if (!module) {
873 module = parent->schema->module;
874 }
875
Michal Vasko3a41dff2020-07-15 14:30:28 +0200876 schema = lys_find_child(parent ? parent->schema : NULL, module, name, 0,
Radek Krejci41ac9942020-11-02 14:47:56 +0100877 LYS_CONTAINER | LYS_NOTIF | LYS_RPC | LYS_ACTION, output ? LYS_GETNEXT_OUTPUT : 0);
Radek Krejcidd2a7662021-03-12 11:26:34 +0100878 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 +0100879
Michal Vasko3a41dff2020-07-15 14:30:28 +0200880 LY_CHECK_RET(lyd_create_inner(schema, &ret));
881 if (parent) {
Michal Vasko013a8182020-03-03 10:46:53 +0100882 lyd_insert_node(parent, NULL, ret);
883 }
Michal Vasko3a41dff2020-07-15 14:30:28 +0200884
885 if (node) {
886 *node = ret;
887 }
888 return LY_SUCCESS;
Michal Vasko013a8182020-03-03 10:46:53 +0100889}
890
Michal Vasko3a41dff2020-07-15 14:30:28 +0200891API LY_ERR
Radek Krejcidd2a7662021-03-12 11:26:34 +0100892lyd_new_ext_inner(const struct lysc_ext_instance *ext, const char *name, struct lyd_node **node)
893{
894 struct lyd_node *ret = NULL;
895 const struct lysc_node *schema;
896 struct ly_ctx *ctx = ext ? ext->module->ctx : NULL;
897
898 LY_CHECK_ARG_RET(ctx, ext, node, name, LY_EINVAL);
899
900 schema = lysc_ext_find_node(ext, NULL, name, 0, LYS_CONTAINER | LYS_NOTIF | LYS_RPC | LYS_ACTION, 0);
901 if (!schema) {
902 if (ext->argument) {
903 LOGERR(ctx, LY_EINVAL, "Inner node (not a list) \"%s\" not found in instance \"%s\" of extension %s.",
904 name, ext->argument, ext->def->name);
905 } else {
906 LOGERR(ctx, LY_EINVAL, "Inner node (not a list) \"%s\" not found in instance of extension %s.",
907 name, ext->def->name);
908 }
909 return LY_ENOTFOUND;
910 }
911 LY_CHECK_RET(lyd_create_inner(schema, &ret));
912
913 *node = ret;
914
915 return LY_SUCCESS;
916}
917
918API LY_ERR
Michal Vasko65243892020-12-04 16:26:21 +0100919lyd_new_list(struct lyd_node *parent, const struct lys_module *module, const char *name, ly_bool output,
920 struct lyd_node **node, ...)
Michal Vasko013a8182020-03-03 10:46:53 +0100921{
922 struct lyd_node *ret = NULL, *key;
923 const struct lysc_node *schema, *key_s;
924 struct ly_ctx *ctx = parent ? parent->schema->module->ctx : (module ? module->ctx : NULL);
925 va_list ap;
926 const char *key_val;
927 LY_ERR rc = LY_SUCCESS;
928
Michal Vasko6027eb92020-07-15 16:37:30 +0200929 LY_CHECK_ARG_RET(ctx, parent || module, parent || node, name, LY_EINVAL);
Michal Vasko013a8182020-03-03 10:46:53 +0100930
Michal Vaskof03ed032020-03-04 13:31:44 +0100931 if (!module) {
932 module = parent->schema->module;
933 }
934
Radek Krejci41ac9942020-11-02 14:47:56 +0100935 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 +0200936 LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "List node \"%s\" not found.", name), LY_ENOTFOUND);
Michal Vasko013a8182020-03-03 10:46:53 +0100937
938 /* create list inner node */
Michal Vasko3a41dff2020-07-15 14:30:28 +0200939 LY_CHECK_RET(lyd_create_inner(schema, &ret));
Michal Vasko013a8182020-03-03 10:46:53 +0100940
Michal Vasko3a41dff2020-07-15 14:30:28 +0200941 va_start(ap, node);
Michal Vasko013a8182020-03-03 10:46:53 +0100942
943 /* create and insert all the keys */
Michal Vasko544e58a2021-01-28 14:33:41 +0100944 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 +0100945 key_val = va_arg(ap, const char *);
946
Radek Krejci8df109d2021-04-23 12:19:08 +0200947 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 +0200948 NULL, &key);
949 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko013a8182020-03-03 10:46:53 +0100950 lyd_insert_node(ret, NULL, key);
951 }
952
Michal Vasko013a8182020-03-03 10:46:53 +0100953 if (parent) {
954 lyd_insert_node(parent, NULL, ret);
955 }
956
957cleanup:
Michal Vasko3a41dff2020-07-15 14:30:28 +0200958 va_end(ap);
Michal Vasko013a8182020-03-03 10:46:53 +0100959 if (rc) {
960 lyd_free_tree(ret);
961 ret = NULL;
Michal Vasko3a41dff2020-07-15 14:30:28 +0200962 } else if (node) {
963 *node = ret;
Michal Vasko013a8182020-03-03 10:46:53 +0100964 }
Michal Vasko3a41dff2020-07-15 14:30:28 +0200965 return rc;
Michal Vasko013a8182020-03-03 10:46:53 +0100966}
967
Michal Vasko3a41dff2020-07-15 14:30:28 +0200968API LY_ERR
Radek Krejci8247bae2021-03-12 11:47:02 +0100969lyd_new_ext_list(const struct lysc_ext_instance *ext, const char *name, struct lyd_node **node, ...)
970{
971 struct lyd_node *ret = NULL, *key;
972 const struct lysc_node *schema, *key_s;
973 struct ly_ctx *ctx = ext ? ext->module->ctx : NULL;
974 va_list ap;
975 const char *key_val;
976 LY_ERR rc = LY_SUCCESS;
977
978 LY_CHECK_ARG_RET(ctx, ext, node, name, LY_EINVAL);
979
980 schema = lysc_ext_find_node(ext, NULL, name, 0, LYS_LIST, 0);
981 if (!schema) {
982 if (ext->argument) {
983 LOGERR(ctx, LY_EINVAL, "List node \"%s\" not found in instance \"%s\" of extension %s.",
984 name, ext->argument, ext->def->name);
985 } else {
986 LOGERR(ctx, LY_EINVAL, "List node \"%s\" not found in instance of extension %s.", name, ext->def->name);
987 }
988 return LY_ENOTFOUND;
989 }
990 /* create list inner node */
991 LY_CHECK_RET(lyd_create_inner(schema, &ret));
992
993 va_start(ap, node);
994
995 /* create and insert all the keys */
996 for (key_s = lysc_node_child(schema); key_s && (key_s->flags & LYS_KEY); key_s = key_s->next) {
997 key_val = va_arg(ap, const char *);
998
Radek Krejci8df109d2021-04-23 12:19:08 +0200999 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 +01001000 NULL, &key);
1001 LY_CHECK_GOTO(rc, cleanup);
1002 lyd_insert_node(ret, NULL, key);
1003 }
1004
1005cleanup:
1006 va_end(ap);
1007 if (rc) {
1008 lyd_free_tree(ret);
1009 ret = NULL;
Radek Krejci8247bae2021-03-12 11:47:02 +01001010 }
Radek Krejci8919fbd2021-03-15 09:35:25 +01001011 *node = ret;
Radek Krejci8247bae2021-03-12 11:47:02 +01001012 return rc;
1013}
1014
1015API LY_ERR
Michal Vasko3a41dff2020-07-15 14:30:28 +02001016lyd_new_list2(struct lyd_node *parent, const struct lys_module *module, const char *name, const char *keys,
Radek Krejci41ac9942020-11-02 14:47:56 +01001017 ly_bool output, struct lyd_node **node)
Michal Vasko013a8182020-03-03 10:46:53 +01001018{
1019 struct lyd_node *ret = NULL;
1020 const struct lysc_node *schema;
1021 struct ly_ctx *ctx = parent ? parent->schema->module->ctx : (module ? module->ctx : NULL);
1022
Michal Vasko6027eb92020-07-15 16:37:30 +02001023 LY_CHECK_ARG_RET(ctx, parent || module, parent || node, name, LY_EINVAL);
Michal Vasko013a8182020-03-03 10:46:53 +01001024
Michal Vaskof03ed032020-03-04 13:31:44 +01001025 if (!module) {
1026 module = parent->schema->module;
1027 }
Michal Vasko004d3152020-06-11 19:59:22 +02001028 if (!keys) {
1029 keys = "";
1030 }
Michal Vaskof03ed032020-03-04 13:31:44 +01001031
Michal Vasko004d3152020-06-11 19:59:22 +02001032 /* find schema node */
Radek Krejci41ac9942020-11-02 14:47:56 +01001033 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 +02001034 LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "List node \"%s\" not found.", name), LY_ENOTFOUND);
Michal Vasko013a8182020-03-03 10:46:53 +01001035
Michal Vasko004d3152020-06-11 19:59:22 +02001036 if ((schema->flags & LYS_KEYLESS) && !keys[0]) {
1037 /* key-less list */
Michal Vasko3a41dff2020-07-15 14:30:28 +02001038 LY_CHECK_RET(lyd_create_inner(schema, &ret));
Michal Vasko004d3152020-06-11 19:59:22 +02001039 } else {
1040 /* create the list node */
Michal Vasko3a41dff2020-07-15 14:30:28 +02001041 LY_CHECK_RET(lyd_create_list2(schema, keys, strlen(keys), &ret));
Michal Vasko004d3152020-06-11 19:59:22 +02001042 }
Michal Vasko004d3152020-06-11 19:59:22 +02001043 if (parent) {
Michal Vasko013a8182020-03-03 10:46:53 +01001044 lyd_insert_node(parent, NULL, ret);
1045 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02001046
1047 if (node) {
1048 *node = ret;
1049 }
1050 return LY_SUCCESS;
Michal Vasko013a8182020-03-03 10:46:53 +01001051}
1052
Radek Krejci09c77442021-04-26 11:10:34 +02001053/**
1054 * @brief Create a new term node in the data tree.
1055 *
1056 * @param[in] parent Parent node for the node being created. NULL in case of creating a top level element.
1057 * @param[in] module Module of the node being created. If NULL, @p parent module will be used.
1058 * @param[in] name Schema node name of the new data node. The node can be ::LYS_LEAF or ::LYS_LEAFLIST.
1059 * @param[in] value Value of the node being created.
1060 * @param[in] value_len Length of @p value.
1061 * @param[in] format Format of @p value.
1062 * @param[in] output Flag in case the @p parent is RPC/Action. If value is 0, the input's data nodes of the RPC/Action are
1063 * taken into consideration. Otherwise, the output's data node is going to be created.
1064 * @param[out] node Optional created node.
1065 * @return LY_ERR value.
1066 */
1067static LY_ERR
1068_lyd_new_term(struct lyd_node *parent, const struct lys_module *module, const char *name, const void *value,
1069 size_t value_len, LY_VALUE_FORMAT format, ly_bool output, struct lyd_node **node)
Michal Vasko013a8182020-03-03 10:46:53 +01001070{
Michal Vaskocbff3e92020-05-27 12:56:41 +02001071 LY_ERR rc;
Michal Vasko013a8182020-03-03 10:46:53 +01001072 struct lyd_node *ret = NULL;
1073 const struct lysc_node *schema;
Radek Krejci09c77442021-04-26 11:10:34 +02001074 const struct ly_ctx *ctx = parent ? LYD_CTX(parent) : (module ? module->ctx : NULL);
Michal Vasko013a8182020-03-03 10:46:53 +01001075
Michal Vasko6027eb92020-07-15 16:37:30 +02001076 LY_CHECK_ARG_RET(ctx, parent || module, parent || node, name, LY_EINVAL);
Michal Vasko013a8182020-03-03 10:46:53 +01001077
Michal Vaskof03ed032020-03-04 13:31:44 +01001078 if (!module) {
1079 module = parent->schema->module;
1080 }
1081
Radek Krejci41ac9942020-11-02 14:47:56 +01001082 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 +02001083 LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "Term node \"%s\" not found.", name), LY_ENOTFOUND);
Michal Vasko013a8182020-03-03 10:46:53 +01001084
Radek Krejci09c77442021-04-26 11:10:34 +02001085 rc = lyd_create_term(schema, value, value_len, NULL, format, NULL, LYD_HINT_DATA, NULL, &ret);
Michal Vaskofeca4fb2020-10-05 08:58:40 +02001086 LY_CHECK_RET(rc);
Michal Vaskocbff3e92020-05-27 12:56:41 +02001087 if (parent) {
Michal Vasko013a8182020-03-03 10:46:53 +01001088 lyd_insert_node(parent, NULL, ret);
1089 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02001090
1091 if (node) {
1092 *node = ret;
1093 }
1094 return LY_SUCCESS;
Michal Vasko013a8182020-03-03 10:46:53 +01001095}
1096
Michal Vasko3a41dff2020-07-15 14:30:28 +02001097API LY_ERR
Radek Krejci09c77442021-04-26 11:10:34 +02001098lyd_new_term(struct lyd_node *parent, const struct lys_module *module, const char *name, const char *val_str,
1099 ly_bool output, struct lyd_node **node)
1100{
1101 return _lyd_new_term(parent, module, name, val_str, val_str ? strlen(val_str) : 0, LY_VALUE_JSON, output, node);
1102}
1103
1104API LY_ERR
1105lyd_new_term_bin(struct lyd_node *parent, const struct lys_module *module, const char *name, const void *value,
1106 size_t value_len, ly_bool output, struct lyd_node **node)
1107{
1108 return _lyd_new_term(parent, module, name, value, value_len, LY_VALUE_LYB, output, node);
1109}
1110
1111API LY_ERR
1112lyd_new_term_canon(struct lyd_node *parent, const struct lys_module *module, const char *name, const char *val_str,
1113 ly_bool output, struct lyd_node **node)
1114{
1115 return _lyd_new_term(parent, module, name, val_str, val_str ? strlen(val_str) : 0, LY_VALUE_CANON, output, node);
1116}
1117
1118API LY_ERR
Radek Krejci8a5afc22021-03-12 11:10:47 +01001119lyd_new_ext_term(const struct lysc_ext_instance *ext, const char *name, const char *val_str, struct lyd_node **node)
1120{
1121 LY_ERR rc;
1122 struct lyd_node *ret = NULL;
1123 const struct lysc_node *schema;
1124 struct ly_ctx *ctx = ext ? ext->module->ctx : NULL;
1125
1126 LY_CHECK_ARG_RET(ctx, ext, node, name, LY_EINVAL);
1127
1128 schema = lysc_ext_find_node(ext, NULL, name, 0, LYD_NODE_TERM, 0);
1129 if (!schema) {
1130 if (ext->argument) {
1131 LOGERR(ctx, LY_EINVAL, "Term node \"%s\" not found in instance \"%s\" of extension %s.",
1132 name, ext->argument, ext->def->name);
1133 } else {
1134 LOGERR(ctx, LY_EINVAL, "Term node \"%s\" not found in instance of extension %s.", name, ext->def->name);
1135 }
1136 return LY_ENOTFOUND;
1137 }
Radek Krejci8df109d2021-04-23 12:19:08 +02001138 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 +01001139 LY_CHECK_RET(rc);
1140
1141 *node = ret;
1142
1143 return LY_SUCCESS;
1144}
1145
1146API LY_ERR
Michal Vasko2a4ab2b2021-03-04 15:52:40 +01001147lyd_new_any(struct lyd_node *parent, const struct lys_module *module, const char *name, const void *value,
1148 ly_bool use_value, LYD_ANYDATA_VALUETYPE value_type, ly_bool output, struct lyd_node **node)
Michal Vasko013a8182020-03-03 10:46:53 +01001149{
1150 struct lyd_node *ret = NULL;
1151 const struct lysc_node *schema;
1152 struct ly_ctx *ctx = parent ? parent->schema->module->ctx : (module ? module->ctx : NULL);
1153
Michal Vasko6027eb92020-07-15 16:37:30 +02001154 LY_CHECK_ARG_RET(ctx, parent || module, parent || node, name, LY_EINVAL);
Michal Vasko013a8182020-03-03 10:46:53 +01001155
Michal Vaskof03ed032020-03-04 13:31:44 +01001156 if (!module) {
1157 module = parent->schema->module;
1158 }
1159
Radek Krejci41ac9942020-11-02 14:47:56 +01001160 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 +02001161 LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "Any node \"%s\" not found.", name), LY_ENOTFOUND);
Michal Vasko013a8182020-03-03 10:46:53 +01001162
Michal Vasko2a4ab2b2021-03-04 15:52:40 +01001163 LY_CHECK_RET(lyd_create_any(schema, value, value_type, use_value, &ret));
Michal Vasko3a41dff2020-07-15 14:30:28 +02001164 if (parent) {
Michal Vasko013a8182020-03-03 10:46:53 +01001165 lyd_insert_node(parent, NULL, ret);
1166 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02001167
1168 if (node) {
1169 *node = ret;
1170 }
1171 return LY_SUCCESS;
Michal Vasko013a8182020-03-03 10:46:53 +01001172}
1173
Radek Krejci0b963da2021-03-12 13:23:44 +01001174API LY_ERR
1175lyd_new_ext_any(const struct lysc_ext_instance *ext, const char *name, const void *value, ly_bool use_value,
1176 LYD_ANYDATA_VALUETYPE value_type, struct lyd_node **node)
1177{
1178 struct lyd_node *ret = NULL;
1179 const struct lysc_node *schema;
1180 struct ly_ctx *ctx = ext ? ext->module->ctx : NULL;
1181
1182 LY_CHECK_ARG_RET(ctx, ext, node, name, LY_EINVAL);
1183
1184 schema = lysc_ext_find_node(ext, NULL, name, 0, LYD_NODE_ANY, 0);
1185 if (!schema) {
1186 if (ext->argument) {
1187 LOGERR(ctx, LY_EINVAL, "Any node \"%s\" not found in instance \"%s\" of extension %s.",
1188 name, ext->argument, ext->def->name);
1189 } else {
1190 LOGERR(ctx, LY_EINVAL, "Any node \"%s\" not found in instance of extension %s.", name, ext->def->name);
1191 }
1192 return LY_ENOTFOUND;
1193 }
1194 LY_CHECK_RET(lyd_create_any(schema, value, value_type, use_value, &ret));
1195
1196 *node = ret;
1197
1198 return LY_SUCCESS;
1199}
1200
Michal Vasko3a41dff2020-07-15 14:30:28 +02001201API LY_ERR
Michal Vasko871a0252020-11-11 18:35:24 +01001202lyd_new_meta(const struct ly_ctx *ctx, struct lyd_node *parent, const struct lys_module *module, const char *name,
1203 const char *val_str, ly_bool clear_dflt, struct lyd_meta **meta)
Michal Vaskod86997b2020-05-26 15:19:54 +02001204{
Michal Vaskod86997b2020-05-26 15:19:54 +02001205 const char *prefix, *tmp;
Michal Vaskod86997b2020-05-26 15:19:54 +02001206 size_t pref_len, name_len;
1207
Michal Vasko2b5344c2021-02-26 10:12:05 +01001208 LY_CHECK_ARG_RET(ctx, ctx || parent, name, module || strchr(name, ':'), parent || meta, LY_EINVAL);
1209 if (!ctx) {
1210 ctx = LYD_CTX(parent);
1211 }
Michal Vasko00cbf532020-06-15 13:58:47 +02001212
Michal Vasko871a0252020-11-11 18:35:24 +01001213 if (parent && !parent->schema) {
Michal Vasko33b4fab2021-03-04 15:56:12 +01001214 LOGERR(ctx, LY_EINVAL, "Cannot add metadata \"%s\" to an opaque node \"%s\".", name, LYD_NAME(parent));
Michal Vasko871a0252020-11-11 18:35:24 +01001215 return LY_EINVAL;
1216 }
Michal Vasko610553d2020-11-18 18:15:05 +01001217 if (meta) {
1218 *meta = NULL;
1219 }
Michal Vaskod86997b2020-05-26 15:19:54 +02001220
1221 /* parse the name */
1222 tmp = name;
1223 if (ly_parse_nodeid(&tmp, &prefix, &pref_len, &name, &name_len) || tmp[0]) {
1224 LOGERR(ctx, LY_EINVAL, "Metadata name \"%s\" is not valid.", name);
Michal Vasko871a0252020-11-11 18:35:24 +01001225 return LY_EINVAL;
Michal Vaskod86997b2020-05-26 15:19:54 +02001226 }
1227
1228 /* find the module */
1229 if (prefix) {
Radek Krejci0ad51f12020-07-16 12:08:12 +02001230 module = ly_ctx_get_module_implemented2(ctx, prefix, pref_len);
Radek Krejci422afb12021-03-04 16:38:16 +01001231 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 +02001232 }
1233
1234 /* set value if none */
1235 if (!val_str) {
1236 val_str = "";
1237 }
1238
Radek Krejci8df109d2021-04-23 12:19:08 +02001239 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 +01001240 NULL, LYD_HINT_DATA, clear_dflt, NULL);
1241}
Michal Vasko3a41dff2020-07-15 14:30:28 +02001242
Michal Vaskoba696702020-11-11 19:12:45 +01001243API LY_ERR
1244lyd_new_meta2(const struct ly_ctx *ctx, struct lyd_node *parent, ly_bool clear_dflt, const struct lyd_attr *attr,
1245 struct lyd_meta **meta)
1246{
1247 const struct lys_module *mod;
1248
1249 LY_CHECK_ARG_RET(NULL, ctx, attr, parent || meta, LY_EINVAL);
1250
1251 if (parent && !parent->schema) {
1252 LOGERR(ctx, LY_EINVAL, "Cannot add metadata to an opaque node \"%s\".", ((struct lyd_node_opaq *)parent)->name);
1253 return LY_EINVAL;
Michal Vasko3a41dff2020-07-15 14:30:28 +02001254 }
Michal Vasko610553d2020-11-18 18:15:05 +01001255 if (meta) {
1256 *meta = NULL;
1257 }
Michal Vaskoba696702020-11-11 19:12:45 +01001258
1259 switch (attr->format) {
Radek Krejci8df109d2021-04-23 12:19:08 +02001260 case LY_VALUE_XML:
Michal Vaskoad92b672020-11-12 13:11:31 +01001261 mod = ly_ctx_get_module_implemented_ns(ctx, attr->name.module_ns);
Michal Vaskoba696702020-11-11 19:12:45 +01001262 if (!mod) {
Michal Vaskoad92b672020-11-12 13:11:31 +01001263 LOGERR(ctx, LY_EINVAL, "Module with namespace \"%s\" not found.", attr->name.module_ns);
Michal Vaskoba696702020-11-11 19:12:45 +01001264 return LY_ENOTFOUND;
1265 }
1266 break;
Radek Krejci8df109d2021-04-23 12:19:08 +02001267 case LY_VALUE_JSON:
Michal Vaskoad92b672020-11-12 13:11:31 +01001268 mod = ly_ctx_get_module_implemented(ctx, attr->name.module_name);
Michal Vaskoba696702020-11-11 19:12:45 +01001269 if (!mod) {
Michal Vaskoad92b672020-11-12 13:11:31 +01001270 LOGERR(ctx, LY_EINVAL, "Module \"%s\" not found.", attr->name.module_name);
Michal Vaskoba696702020-11-11 19:12:45 +01001271 return LY_ENOTFOUND;
1272 }
1273 break;
1274 default:
1275 LOGINT_RET(ctx);
1276 }
1277
Michal Vaskoad92b672020-11-12 13:11:31 +01001278 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 +01001279 NULL, attr->format, attr->val_prefix_data, attr->hints, clear_dflt, NULL);
Michal Vaskod86997b2020-05-26 15:19:54 +02001280}
1281
Michal Vasko3a41dff2020-07-15 14:30:28 +02001282API LY_ERR
Michal Vasko00cbf532020-06-15 13:58:47 +02001283lyd_new_opaq(struct lyd_node *parent, const struct ly_ctx *ctx, const char *name, const char *value,
Michal Vasko0ab974d2021-02-24 13:18:26 +01001284 const char *prefix, const char *module_name, struct lyd_node **node)
Michal Vasko00cbf532020-06-15 13:58:47 +02001285{
1286 struct lyd_node *ret = NULL;
Radek Krejci07a55962021-03-02 20:16:43 +01001287
Michal Vasko0ab974d2021-02-24 13:18:26 +01001288 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 +02001289
1290 if (!ctx) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02001291 ctx = LYD_CTX(parent);
Michal Vasko00cbf532020-06-15 13:58:47 +02001292 }
1293 if (!value) {
1294 value = "";
1295 }
1296
Michal Vasko0ab974d2021-02-24 13:18:26 +01001297 LY_CHECK_RET(lyd_create_opaq(ctx, name, strlen(name), prefix, prefix ? strlen(prefix) : 0, module_name,
Radek Krejci8df109d2021-04-23 12:19:08 +02001298 strlen(module_name), value, strlen(value), NULL, LY_VALUE_JSON, NULL, 0, &ret));
Michal Vasko3a41dff2020-07-15 14:30:28 +02001299 if (parent) {
Michal Vasko00cbf532020-06-15 13:58:47 +02001300 lyd_insert_node(parent, NULL, ret);
1301 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02001302
1303 if (node) {
1304 *node = ret;
1305 }
1306 return LY_SUCCESS;
Michal Vasko00cbf532020-06-15 13:58:47 +02001307}
1308
Michal Vasko3a41dff2020-07-15 14:30:28 +02001309API LY_ERR
Michal Vasko8d65f852021-02-17 11:28:13 +01001310lyd_new_opaq2(struct lyd_node *parent, const struct ly_ctx *ctx, const char *name, const char *value,
Michal Vasko0ab974d2021-02-24 13:18:26 +01001311 const char *prefix, const char *module_ns, struct lyd_node **node)
Michal Vasko8d65f852021-02-17 11:28:13 +01001312{
1313 struct lyd_node *ret = NULL;
1314
1315 LY_CHECK_ARG_RET(ctx, parent || ctx, parent || node, name, module_ns, LY_EINVAL);
1316
1317 if (!ctx) {
1318 ctx = LYD_CTX(parent);
1319 }
1320 if (!value) {
1321 value = "";
1322 }
1323
Michal Vasko0ab974d2021-02-24 13:18:26 +01001324 LY_CHECK_RET(lyd_create_opaq(ctx, name, strlen(name), prefix, prefix ? strlen(prefix) : 0, module_ns,
Radek Krejci8df109d2021-04-23 12:19:08 +02001325 strlen(module_ns), value, strlen(value), NULL, LY_VALUE_XML, NULL, 0, &ret));
Michal Vasko8d65f852021-02-17 11:28:13 +01001326 if (parent) {
1327 lyd_insert_node(parent, NULL, ret);
1328 }
1329
1330 if (node) {
1331 *node = ret;
1332 }
1333 return LY_SUCCESS;
1334}
1335
1336API LY_ERR
1337lyd_new_attr(struct lyd_node *parent, const char *module_name, const char *name, const char *value,
Radek Krejci0f969882020-08-21 16:56:47 +02001338 struct lyd_attr **attr)
Michal Vasko00cbf532020-06-15 13:58:47 +02001339{
Radek Krejci1798aae2020-07-14 13:26:06 +02001340 struct lyd_attr *ret = NULL;
Michal Vasko00cbf532020-06-15 13:58:47 +02001341 const struct ly_ctx *ctx;
1342 const char *prefix, *tmp;
Michal Vasko51d21b82020-11-13 18:03:54 +01001343 size_t pref_len, name_len, mod_len;
Michal Vasko00cbf532020-06-15 13:58:47 +02001344
Michal Vasko3a41dff2020-07-15 14:30:28 +02001345 LY_CHECK_ARG_RET(NULL, parent, !parent->schema, name, LY_EINVAL);
Michal Vasko00cbf532020-06-15 13:58:47 +02001346
Michal Vaskob7be7a82020-08-20 09:09:04 +02001347 ctx = LYD_CTX(parent);
Michal Vasko00cbf532020-06-15 13:58:47 +02001348
1349 /* parse the name */
1350 tmp = name;
1351 if (ly_parse_nodeid(&tmp, &prefix, &pref_len, &name, &name_len) || tmp[0]) {
Michal Vaskob69b68c2021-02-17 11:18:16 +01001352 LOGERR(ctx, LY_EINVAL, "Attribute name \"%s\" is not valid.", name);
Michal Vasko3a41dff2020-07-15 14:30:28 +02001353 return LY_EVALID;
Michal Vasko00cbf532020-06-15 13:58:47 +02001354 }
1355
Michal Vasko51d21b82020-11-13 18:03:54 +01001356 /* get the module */
1357 if (module_name) {
1358 mod_len = strlen(module_name);
1359 } else {
1360 module_name = prefix;
1361 mod_len = pref_len;
1362 }
1363
Michal Vasko00cbf532020-06-15 13:58:47 +02001364 /* set value if none */
Michal Vasko8d65f852021-02-17 11:28:13 +01001365 if (!value) {
1366 value = "";
Michal Vasko00cbf532020-06-15 13:58:47 +02001367 }
1368
Michal Vasko8d65f852021-02-17 11:28:13 +01001369 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 +02001370 strlen(value), NULL, LY_VALUE_JSON, NULL, LYD_HINT_DATA));
Michal Vasko8d65f852021-02-17 11:28:13 +01001371
1372 if (attr) {
1373 *attr = ret;
1374 }
1375 return LY_SUCCESS;
1376}
1377
1378API LY_ERR
1379lyd_new_attr2(struct lyd_node *parent, const char *module_ns, const char *name, const char *value,
1380 struct lyd_attr **attr)
1381{
1382 struct lyd_attr *ret = NULL;
1383 const struct ly_ctx *ctx;
1384 const char *prefix, *tmp;
1385 size_t pref_len, name_len;
1386
1387 LY_CHECK_ARG_RET(NULL, parent, !parent->schema, name, LY_EINVAL);
1388
1389 ctx = LYD_CTX(parent);
1390
1391 /* parse the name */
1392 tmp = name;
1393 if (ly_parse_nodeid(&tmp, &prefix, &pref_len, &name, &name_len) || tmp[0]) {
1394 LOGERR(ctx, LY_EINVAL, "Attribute name \"%s\" is not valid.", name);
1395 return LY_EVALID;
1396 }
1397
1398 /* set value if none */
1399 if (!value) {
1400 value = "";
1401 }
1402
1403 LY_CHECK_RET(lyd_create_attr(parent, &ret, ctx, name, name_len, prefix, pref_len, module_ns,
Radek Krejci8df109d2021-04-23 12:19:08 +02001404 module_ns ? strlen(module_ns) : 0, value, strlen(value), NULL, LY_VALUE_XML, NULL, LYD_HINT_DATA));
Michal Vasko3a41dff2020-07-15 14:30:28 +02001405
1406 if (attr) {
1407 *attr = ret;
1408 }
1409 return LY_SUCCESS;
Michal Vasko00cbf532020-06-15 13:58:47 +02001410}
1411
Radek Krejci09c77442021-04-26 11:10:34 +02001412/**
1413 * @brief Change the value of a term (leaf or leaf-list) node.
1414 *
1415 * Node changed this way is always considered explicitly set, meaning its default flag
1416 * is always cleared.
1417 *
1418 * @param[in] term Term node to change.
1419 * @param[in] value New value to set.
1420 * @param[in] value_len Length of @p value.
1421 * @param[in] format Format of @p value.
1422 * @return LY_SUCCESS if value was changed,
1423 * @return LY_EEXIST if value was the same and only the default flag was cleared,
1424 * @return LY_ENOT if the values were equal and no change occured,
1425 * @return LY_ERR value on other errors.
1426 */
1427static LY_ERR
1428_lyd_change_term(struct lyd_node *term, const void *value, size_t value_len, LY_VALUE_FORMAT format)
Michal Vasko00cbf532020-06-15 13:58:47 +02001429{
1430 LY_ERR ret = LY_SUCCESS;
1431 struct lysc_type *type;
1432 struct lyd_node_term *t;
1433 struct lyd_node *parent;
1434 struct lyd_value val = {0};
Radek Krejci857189e2020-09-01 13:26:36 +02001435 ly_bool dflt_change, val_change;
Michal Vasko00cbf532020-06-15 13:58:47 +02001436
Radek Krejci09c77442021-04-26 11:10:34 +02001437 assert(term && term->schema && (term->schema->nodetype & LYD_NODE_TERM));
Michal Vasko00cbf532020-06-15 13:58:47 +02001438
Michal Vasko00cbf532020-06-15 13:58:47 +02001439 t = (struct lyd_node_term *)term;
1440 type = ((struct lysc_node_leaf *)term->schema)->type;
1441
1442 /* parse the new value */
Radek Krejciddace2c2021-01-08 11:30:56 +01001443 LOG_LOCSET(term->schema, term, NULL, NULL);
Radek Krejci09c77442021-04-26 11:10:34 +02001444 ret = lyd_value_store(LYD_CTX(term), &val, type, value, value_len, NULL, format, NULL, LYD_HINT_DATA, term->schema, NULL);
Radek Krejciddace2c2021-01-08 11:30:56 +01001445 LOG_LOCBACK(term->schema ? 1 : 0, 1, 0, 0);
Michal Vaskofeca4fb2020-10-05 08:58:40 +02001446 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko00cbf532020-06-15 13:58:47 +02001447
1448 /* compare original and new value */
1449 if (type->plugin->compare(&t->value, &val)) {
1450 /* values differ, switch them */
Michal Vaskob7be7a82020-08-20 09:09:04 +02001451 type->plugin->free(LYD_CTX(term), &t->value);
Michal Vasko00cbf532020-06-15 13:58:47 +02001452 t->value = val;
1453 memset(&val, 0, sizeof val);
1454 val_change = 1;
1455 } else {
1456 val_change = 0;
1457 }
1458
1459 /* always clear the default flag */
1460 if (term->flags & LYD_DEFAULT) {
Michal Vasko9e685082021-01-29 14:49:09 +01001461 for (parent = term; parent; parent = lyd_parent(parent)) {
Michal Vasko00cbf532020-06-15 13:58:47 +02001462 parent->flags &= ~LYD_DEFAULT;
1463 }
1464 dflt_change = 1;
1465 } else {
1466 dflt_change = 0;
1467 }
1468
1469 if (val_change || dflt_change) {
1470 /* make the node non-validated */
1471 term->flags &= LYD_NEW;
1472 }
1473
1474 if (val_change) {
1475 if (term->schema->nodetype == LYS_LEAFLIST) {
1476 /* leaf-list needs to be hashed again and re-inserted into parent */
1477 lyd_unlink_hash(term);
1478 lyd_hash(term);
1479 LY_CHECK_GOTO(ret = lyd_insert_hash(term), cleanup);
1480 } else if ((term->schema->flags & LYS_KEY) && term->parent) {
1481 /* list needs to be updated if its key was changed */
1482 assert(term->parent->schema->nodetype == LYS_LIST);
Michal Vasko9e685082021-01-29 14:49:09 +01001483 lyd_unlink_hash(lyd_parent(term));
1484 lyd_hash(lyd_parent(term));
1485 LY_CHECK_GOTO(ret = lyd_insert_hash(lyd_parent(term)), cleanup);
Michal Vasko00cbf532020-06-15 13:58:47 +02001486 } /* else leaf that is not a key, its value is not used for its hash so it does not change */
1487 }
1488
1489 /* retrun value */
1490 if (!val_change) {
1491 if (dflt_change) {
1492 /* only default flag change */
1493 ret = LY_EEXIST;
1494 } else {
1495 /* no change */
1496 ret = LY_ENOT;
1497 }
1498 } /* else value changed, LY_SUCCESS */
1499
1500cleanup:
Michal Vasko59512fc2020-12-09 18:13:29 +01001501 if (val.realtype) {
1502 type->plugin->free(LYD_CTX(term), &val);
1503 }
Michal Vasko00cbf532020-06-15 13:58:47 +02001504 return ret;
1505}
1506
Michal Vasko41586352020-07-13 13:54:25 +02001507API LY_ERR
Radek Krejci09c77442021-04-26 11:10:34 +02001508lyd_change_term(struct lyd_node *term, const char *val_str)
1509{
1510 LY_CHECK_ARG_RET(NULL, term, term->schema, term->schema->nodetype & LYD_NODE_TERM, LY_EINVAL);
1511
1512 return _lyd_change_term(term, val_str, val_str ? strlen(val_str) : 0, LY_VALUE_JSON);
1513}
1514
1515API LY_ERR
1516lyd_change_term_bin(struct lyd_node *term, const void *value, size_t value_len)
1517{
1518 LY_CHECK_ARG_RET(NULL, term, term->schema, term->schema->nodetype & LYD_NODE_TERM, LY_EINVAL);
1519
1520 return _lyd_change_term(term, value, value_len, LY_VALUE_LYB);
1521}
1522
1523API LY_ERR
1524lyd_change_term_canon(struct lyd_node *term, const char *val_str)
1525{
1526 LY_CHECK_ARG_RET(NULL, term, term->schema, term->schema->nodetype & LYD_NODE_TERM, LY_EINVAL);
1527
1528 return _lyd_change_term(term, val_str, val_str ? strlen(val_str) : 0, LY_VALUE_CANON);
1529}
1530
1531API LY_ERR
Michal Vasko41586352020-07-13 13:54:25 +02001532lyd_change_meta(struct lyd_meta *meta, const char *val_str)
1533{
1534 LY_ERR ret = LY_SUCCESS;
Radek Krejci2b18bf12020-11-06 11:20:20 +01001535 struct lyd_meta *m2 = NULL;
Michal Vasko41586352020-07-13 13:54:25 +02001536 struct lyd_value val;
Radek Krejci857189e2020-09-01 13:26:36 +02001537 ly_bool val_change;
Michal Vasko41586352020-07-13 13:54:25 +02001538
1539 LY_CHECK_ARG_RET(NULL, meta, LY_EINVAL);
1540
1541 if (!val_str) {
1542 val_str = "";
1543 }
1544
1545 /* parse the new value into a new meta structure */
1546 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 +02001547 strlen(val_str), NULL, LY_VALUE_JSON, NULL, LYD_HINT_DATA, 0, NULL), cleanup);
Michal Vasko41586352020-07-13 13:54:25 +02001548
1549 /* compare original and new value */
1550 if (lyd_compare_meta(meta, m2)) {
1551 /* values differ, switch them */
1552 val = meta->value;
1553 meta->value = m2->value;
1554 m2->value = val;
1555 val_change = 1;
1556 } else {
1557 val_change = 0;
1558 }
1559
1560 /* retrun value */
1561 if (!val_change) {
1562 /* no change */
1563 ret = LY_ENOT;
1564 } /* else value changed, LY_SUCCESS */
1565
1566cleanup:
Michal Vasko1a66a5d2020-11-18 18:15:37 +01001567 lyd_free_meta_single(m2);
Michal Vasko41586352020-07-13 13:54:25 +02001568 return ret;
1569}
1570
Radek Krejci09c77442021-04-26 11:10:34 +02001571/**
1572 * @brief Update node value.
1573 *
1574 * @param[in] node Node to update.
1575 * @param[in] value New value to set.
1576 * @param[in] value_len Length of @p value.
1577 * @param[in] value_type Type of @p value for anydata/anyxml node.
1578 * @param[in] format Format of @p value.
1579 * @param[out] new_parent Set to @p node if the value was updated, otherwise set to NULL.
1580 * @param[out] new_node Set to @p node if the value was updated, otherwise set to NULL.
1581 * @return LY_ERR value.
1582 */
Michal Vasko6741dc62021-02-04 09:27:45 +01001583static LY_ERR
Radek Krejci09c77442021-04-26 11:10:34 +02001584lyd_new_path_update(struct lyd_node *node, const void *value, size_t value_len, LYD_ANYDATA_VALUETYPE value_type,
1585 LY_VALUE_FORMAT format, struct lyd_node **new_parent, struct lyd_node **new_node)
1586{
1587 LY_ERR ret = LY_SUCCESS;
1588 struct lyd_node *new_any;
1589
1590 switch (node->schema->nodetype) {
1591 case LYS_CONTAINER:
1592 case LYS_NOTIF:
1593 case LYS_RPC:
1594 case LYS_ACTION:
1595 case LYS_LIST:
1596 /* if it exists, there is nothing to update */
1597 *new_parent = NULL;
1598 *new_node = NULL;
1599 break;
1600 case LYS_LEAFLIST:
1601 if (!lysc_is_dup_inst_list(node->schema)) {
1602 /* if it exists, there is nothing to update */
1603 *new_parent = NULL;
1604 *new_node = NULL;
1605 break;
1606 }
1607 /* fallthrough */
1608 case LYS_LEAF:
1609 ret = _lyd_change_term(node, value, value_len, format);
1610 if ((ret == LY_SUCCESS) || (ret == LY_EEXIST)) {
1611 /* there was an actual change (at least of the default flag) */
1612 *new_parent = node;
1613 *new_node = node;
1614 ret = LY_SUCCESS;
1615 } else if (ret == LY_ENOT) {
1616 /* no change */
1617 *new_parent = NULL;
1618 *new_node = NULL;
1619 ret = LY_SUCCESS;
1620 } /* else error */
1621 break;
1622 case LYS_ANYDATA:
1623 case LYS_ANYXML:
1624 /* create a new any node */
1625 LY_CHECK_RET(lyd_create_any(node->schema, value, value_type, 0, &new_any));
1626
1627 /* compare with the existing one */
1628 if (lyd_compare_single(node, new_any, 0)) {
1629 /* not equal, switch values (so that we can use generic node free) */
1630 ((struct lyd_node_any *)new_any)->value = ((struct lyd_node_any *)node)->value;
1631 ((struct lyd_node_any *)new_any)->value_type = ((struct lyd_node_any *)node)->value_type;
1632 ((struct lyd_node_any *)node)->value.str = value;
1633 ((struct lyd_node_any *)node)->value_type = value_type;
1634
1635 *new_parent = node;
1636 *new_node = node;
1637 } else {
1638 /* they are equal */
1639 *new_parent = NULL;
1640 *new_node = NULL;
1641 }
1642 lyd_free_tree(new_any);
1643 break;
1644 default:
1645 LOGINT(LYD_CTX(node));
1646 ret = LY_EINT;
1647 break;
1648 }
1649
1650 return ret;
1651}
1652
1653static LY_ERR
1654lyd_new_path_check_find_lypath(struct ly_path *path, const char *str_path, const char *value, size_t value_len,
1655 LY_VALUE_FORMAT format, uint32_t options)
Michal Vasko6741dc62021-02-04 09:27:45 +01001656{
Michal Vaskoe78faec2021-04-08 17:24:43 +02001657 LY_ERR r;
Michal Vasko6741dc62021-02-04 09:27:45 +01001658 struct ly_path_predicate *pred;
Michal Vaskoe78faec2021-04-08 17:24:43 +02001659 const struct lysc_node *schema = NULL;
1660 LY_ARRAY_COUNT_TYPE u, new_count;
1661 int create = 0;
Michal Vasko6741dc62021-02-04 09:27:45 +01001662
1663 assert(path);
1664
1665 /* go through all the compiled nodes */
1666 LY_ARRAY_FOR(path, u) {
1667 schema = path[u].node;
Michal Vaskoe78faec2021-04-08 17:24:43 +02001668
1669 if (lysc_is_dup_inst_list(schema)) {
1670 if (path[u].pred_type == LY_PATH_PREDTYPE_NONE) {
1671 /* creating a new key-less list or state leaf-list instance */
1672 create = 1;
1673 new_count = u;
1674 } else if (path[u].pred_type != LY_PATH_PREDTYPE_POSITION) {
1675 LOG_LOCSET(schema, NULL, NULL, NULL);
1676 LOGVAL(schema->module->ctx, LYVE_XPATH, "Invalid predicate for %s \"%s\" in path \"%s\".",
1677 lys_nodetype2str(schema->nodetype), schema->name, str_path);
1678 LOG_LOCBACK(1, 0, 0, 0);
1679 return LY_EINVAL;
1680 }
1681 } else if ((schema->nodetype == LYS_LIST) && (path[u].pred_type != LY_PATH_PREDTYPE_LIST)) {
1682 if ((u < LY_ARRAY_COUNT(path) - 1) || !(options & LYD_NEW_PATH_OPAQ)) {
Michal Vasko6741dc62021-02-04 09:27:45 +01001683 LOG_LOCSET(schema, NULL, NULL, NULL);
1684 LOGVAL(schema->module->ctx, LYVE_XPATH, "Predicate missing for %s \"%s\" in path \"%s\".",
1685 lys_nodetype2str(schema->nodetype), schema->name, str_path);
1686 LOG_LOCBACK(1, 0, 0, 0);
1687 return LY_EINVAL;
Michal Vaskoe78faec2021-04-08 17:24:43 +02001688 } /* else creating an opaque list */
1689 } else if ((schema->nodetype == LYS_LEAFLIST) && (path[u].pred_type != LY_PATH_PREDTYPE_LEAFLIST)) {
Michal Vaskoe78faec2021-04-08 17:24:43 +02001690 r = LY_SUCCESS;
1691 if (options & LYD_NEW_PATH_OPAQ) {
Radek Krejci09c77442021-04-26 11:10:34 +02001692 r = lyd_value_validate(NULL, schema, value, value_len, NULL, NULL, NULL);
Michal Vaskoe78faec2021-04-08 17:24:43 +02001693 }
1694 if (!r) {
1695 /* store the new predicate so that it is used when searching for this instance */
1696 path[u].pred_type = LY_PATH_PREDTYPE_LEAFLIST;
1697 LY_ARRAY_NEW_RET(schema->module->ctx, path[u].predicates, pred, LY_EMEM);
1698
1699 LY_CHECK_RET(lyd_value_store(schema->module->ctx, &pred->value,
Radek Krejci09c77442021-04-26 11:10:34 +02001700 ((struct lysc_node_leaflist *)schema)->type, value, value_len, NULL, format, NULL,
Michal Vaskoe78faec2021-04-08 17:24:43 +02001701 LYD_HINT_DATA, schema, NULL));
1702 ++((struct lysc_type *)pred->value.realtype)->refcount;
1703 } /* else we have opaq flag and the value is not valid, leave no predicate and then create an opaque node */
1704 }
Michal Vasko6741dc62021-02-04 09:27:45 +01001705 }
1706
Michal Vaskoe78faec2021-04-08 17:24:43 +02001707 if (create) {
1708 /* hide the nodes that should always be created so they are not found */
1709 while (new_count < LY_ARRAY_COUNT(path)) {
1710 LY_ARRAY_DECREMENT(path);
1711 }
Michal Vasko6741dc62021-02-04 09:27:45 +01001712 }
1713
Michal Vaskoe78faec2021-04-08 17:24:43 +02001714 return LY_SUCCESS;
Michal Vasko6741dc62021-02-04 09:27:45 +01001715}
1716
Radek Krejci95ccd1b2021-03-12 14:57:22 +01001717/**
1718 * @brief Create a new node in the data tree based on a path. All node types can be created.
1719 *
1720 * If @p path points to a list key and the list instance does not exist, the key value from the predicate is used
1721 * and @p value is ignored. Also, if a leaf-list is being created and both a predicate is defined in @p path
1722 * and @p value is set, the predicate is preferred.
1723 *
1724 * For key-less lists and state leaf-lists, positional predicates can be used. If no preciate is used for these
1725 * nodes, they are always created.
1726 *
1727 * @param[in] parent Data parent to add to/modify, can be NULL. Note that in case a first top-level sibling is used,
1728 * it may no longer be first if @p path is absolute and starts with a non-existing top-level node inserted
1729 * before @p parent. Use ::lyd_first_sibling() to adjust @p parent in these cases.
1730 * @param[in] ctx libyang context, must be set if @p parent is NULL.
1731 * @param[in] ext Extension instance where the node being created is defined. This argument takes effect only for absolute
1732 * path or when the relative paths touches document root (top-level). In such cases the present extension instance replaces
1733 * searching for the appropriate module.
1734 * @param[in] path [Path](@ref howtoXPath) to create.
Radek Krejci09c77442021-04-26 11:10:34 +02001735 * @param[in] value Value of the new leaf/leaf-list (const char *) in ::LY_VALUE_JSON format. If creating an
1736 * anyxml/anydata node, the expected type depends on @p value_type. For other node types, it should be NULL.
1737 * @param[in] value_len Length of @p value in bytes. May be 0 if @p value is a zero-terminated string. Ignored when
1738 * creating anyxml/anydata nodes.
Radek Krejci95ccd1b2021-03-12 14:57:22 +01001739 * @param[in] value_type Anyxml/anydata node @p value type.
1740 * @param[in] options Bitmask of options, see @ref pathoptions.
1741 * @param[out] new_parent Optional first parent node created. If only one node was created, equals to @p new_node.
1742 * @param[out] new_node Optional last node created.
1743 * @return LY_ERR value.
1744 */
1745static LY_ERR
1746lyd_new_path_(struct lyd_node *parent, const struct ly_ctx *ctx, const struct lysc_ext_instance *ext, const char *path,
Radek Krejci09c77442021-04-26 11:10:34 +02001747 const void *value, size_t value_len, LYD_ANYDATA_VALUETYPE value_type, uint32_t options,
Radek Krejci95ccd1b2021-03-12 14:57:22 +01001748 struct lyd_node **new_parent, struct lyd_node **new_node)
Michal Vasko00cbf532020-06-15 13:58:47 +02001749{
1750 LY_ERR ret = LY_SUCCESS, r;
1751 struct lyxp_expr *exp = NULL;
1752 struct ly_path *p = NULL;
1753 struct lyd_node *nparent = NULL, *nnode = NULL, *node = NULL, *cur_parent;
1754 const struct lysc_node *schema;
Michal Vaskoe78faec2021-04-08 17:24:43 +02001755 const struct lyd_value *val = NULL;
Radek Krejci910d0132021-03-05 14:19:31 +01001756 LY_ARRAY_COUNT_TYPE path_idx = 0, orig_count = 0;
Radek Krejci09c77442021-04-26 11:10:34 +02001757 LY_VALUE_FORMAT format;
Michal Vasko00cbf532020-06-15 13:58:47 +02001758
Radek Krejci95ccd1b2021-03-12 14:57:22 +01001759 assert(parent || ctx);
Radek Krejci09c77442021-04-26 11:10:34 +02001760 assert(path && ((path[0] == '/') || parent));
1761 assert(!(options & LYD_NEW_PATH_BIN_VALUE) || !(options & LYD_NEW_PATH_CANON_VALUE));
Michal Vasko00cbf532020-06-15 13:58:47 +02001762
1763 if (!ctx) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02001764 ctx = LYD_CTX(parent);
Michal Vasko00cbf532020-06-15 13:58:47 +02001765 }
Radek Krejci09c77442021-04-26 11:10:34 +02001766 if (value && !value_len) {
1767 value_len = strlen(value);
1768 }
1769 if (options & LYD_NEW_PATH_BIN_VALUE) {
1770 format = LY_VALUE_LYB;
1771 } else if (options & LYD_NEW_PATH_CANON_VALUE) {
1772 format = LY_VALUE_CANON;
1773 } else {
1774 format = LY_VALUE_JSON;
1775 }
Michal Vasko00cbf532020-06-15 13:58:47 +02001776
1777 /* parse path */
Michal Vasko6b26e742020-07-17 15:02:10 +02001778 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 +02001779 LY_PATH_PREFIX_OPTIONAL, LY_PATH_PRED_SIMPLE, &exp), cleanup);
Michal Vasko00cbf532020-06-15 13:58:47 +02001780
1781 /* compile path */
Radek Krejci95ccd1b2021-03-12 14:57:22 +01001782 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 +02001783 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 +01001784 NULL, NULL, &p), cleanup);
Michal Vasko00cbf532020-06-15 13:58:47 +02001785
Michal Vasko6741dc62021-02-04 09:27:45 +01001786 /* check the compiled path before searching existing nodes, it may be shortened */
1787 orig_count = LY_ARRAY_COUNT(p);
Radek Krejci09c77442021-04-26 11:10:34 +02001788 LY_CHECK_GOTO(ret = lyd_new_path_check_find_lypath(p, path, value, value_len, format, options), cleanup);
Michal Vasko00cbf532020-06-15 13:58:47 +02001789
1790 /* try to find any existing nodes in the path */
1791 if (parent) {
1792 ret = ly_path_eval_partial(p, parent, &path_idx, &node);
1793 if (ret == LY_SUCCESS) {
Michal Vasko6741dc62021-02-04 09:27:45 +01001794 if (orig_count == LY_ARRAY_COUNT(p)) {
1795 /* the node exists, are we supposed to update it or is it just a default? */
1796 if (!(options & LYD_NEW_PATH_UPDATE) && !(node->flags & LYD_DEFAULT)) {
1797 LOG_LOCSET(NULL, node, NULL, NULL);
1798 LOGVAL(ctx, LYVE_REFERENCE, "Path \"%s\" already exists", path);
1799 LOG_LOCBACK(0, 1, 0, 0);
1800 ret = LY_EEXIST;
1801 goto cleanup;
1802 }
Michal Vasko00cbf532020-06-15 13:58:47 +02001803
Michal Vasko6741dc62021-02-04 09:27:45 +01001804 /* update the existing node */
Radek Krejci09c77442021-04-26 11:10:34 +02001805 ret = lyd_new_path_update(node, value, value_len, value_type, format, &nparent, &nnode);
Michal Vasko6741dc62021-02-04 09:27:45 +01001806 goto cleanup;
1807 } /* else we were not searching for the whole path */
Michal Vasko00cbf532020-06-15 13:58:47 +02001808 } else if (ret == LY_EINCOMPLETE) {
1809 /* some nodes were found, adjust the iterator to the next segment */
1810 ++path_idx;
1811 } else if (ret == LY_ENOTFOUND) {
1812 /* 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 +01001813 if (lysc_data_parent(p[0].node)) {
Michal Vasko00cbf532020-06-15 13:58:47 +02001814 node = parent;
1815 }
1816 } else {
1817 /* error */
1818 goto cleanup;
1819 }
1820 }
1821
Michal Vasko6741dc62021-02-04 09:27:45 +01001822 /* restore the full path for creating new nodes */
1823 while (orig_count > LY_ARRAY_COUNT(p)) {
1824 LY_ARRAY_INCREMENT(p);
1825 }
1826
Michal Vasko00cbf532020-06-15 13:58:47 +02001827 /* create all the non-existing nodes in a loop */
Michal Vaskod989ba02020-08-24 10:59:24 +02001828 for ( ; path_idx < LY_ARRAY_COUNT(p); ++path_idx) {
Michal Vasko00cbf532020-06-15 13:58:47 +02001829 cur_parent = node;
1830 schema = p[path_idx].node;
1831
1832 switch (schema->nodetype) {
1833 case LYS_LIST:
Michal Vaskoe78faec2021-04-08 17:24:43 +02001834 if (lysc_is_dup_inst_list(schema)) {
1835 /* create key-less list instance */
1836 LY_CHECK_GOTO(ret = lyd_create_inner(schema, &node), cleanup);
1837 } else if ((options & LYD_NEW_PATH_OPAQ) && (p[path_idx].pred_type == LY_PATH_PREDTYPE_NONE)) {
1838 /* creating opaque list without keys */
1839 LY_CHECK_GOTO(ret = lyd_create_opaq(ctx, schema->name, strlen(schema->name), NULL, 0,
Radek Krejci8df109d2021-04-23 12:19:08 +02001840 schema->module->name, strlen(schema->module->name), NULL, 0, NULL, LY_VALUE_JSON, NULL,
Michal Vaskoe78faec2021-04-08 17:24:43 +02001841 LYD_NODEHINT_LIST, &node), cleanup);
1842 } else {
1843 /* create standard list instance */
1844 assert(p[path_idx].pred_type == LY_PATH_PREDTYPE_LIST);
1845 LY_CHECK_GOTO(ret = lyd_create_list(schema, p[path_idx].predicates, &node), cleanup);
Michal Vasko00cbf532020-06-15 13:58:47 +02001846 }
Michal Vaskoe78faec2021-04-08 17:24:43 +02001847 break;
Michal Vasko00cbf532020-06-15 13:58:47 +02001848 case LYS_CONTAINER:
1849 case LYS_NOTIF:
1850 case LYS_RPC:
1851 case LYS_ACTION:
1852 LY_CHECK_GOTO(ret = lyd_create_inner(schema, &node), cleanup);
1853 break;
1854 case LYS_LEAFLIST:
Michal Vaskoe78faec2021-04-08 17:24:43 +02001855 if ((options & LYD_NEW_PATH_OPAQ) && (p[path_idx].pred_type != LY_PATH_PREDTYPE_LEAFLIST)) {
1856 /* we have not checked this only for dup-inst lists, otherwise it must be opaque */
1857 r = LY_EVALID;
1858 if (lysc_is_dup_inst_list(schema)) {
1859 /* validate value */
Radek Krejci09c77442021-04-26 11:10:34 +02001860 r = lyd_value_validate(NULL, schema, value ? value : "", value_len, NULL, NULL, NULL);
Michal Vaskoe78faec2021-04-08 17:24:43 +02001861 }
1862 if (r && (r != LY_EINCOMPLETE)) {
1863 /* creating opaque leaf-list */
Radek Krejci09c77442021-04-26 11:10:34 +02001864 LY_CHECK_GOTO(ret = lyd_create_opaq(ctx, schema->name, strlen(schema->name), value, value_len,
1865 schema->module->name, strlen(schema->module->name), NULL, 0, NULL, format, NULL,
1866 LYD_NODEHINT_LEAFLIST, &node), cleanup);
Michal Vaskoe78faec2021-04-08 17:24:43 +02001867 break;
1868 }
1869 }
1870
1871 /* get value to set */
1872 if (p[path_idx].pred_type == LY_PATH_PREDTYPE_LEAFLIST) {
1873 val = &p[path_idx].predicates[0].value;
Michal Vaskoe78faec2021-04-08 17:24:43 +02001874 }
1875
1876 /* create a leaf-list instance */
1877 if (val) {
Michal Vasko00cbf532020-06-15 13:58:47 +02001878 LY_CHECK_GOTO(ret = lyd_create_term2(schema, &p[path_idx].predicates[0].value, &node), cleanup);
Michal Vaskoe78faec2021-04-08 17:24:43 +02001879 } else {
Radek Krejci09c77442021-04-26 11:10:34 +02001880 LY_CHECK_GOTO(ret = lyd_create_term(schema, value, value_len, NULL, format, NULL, LYD_HINT_DATA,
1881 NULL, &node), cleanup);
Michal Vasko00cbf532020-06-15 13:58:47 +02001882 }
1883 break;
1884 case LYS_LEAF:
Michal Vasko35880332020-12-08 13:08:12 +01001885 if (lysc_is_key(schema)) {
1886 /* it must have been already created or some error will occur later */
1887 assert(cur_parent);
1888 node = lyd_child(cur_parent);
1889 assert(node && (node->schema == schema));
1890 goto next_iter;
1891 }
1892
Michal Vaskoe78faec2021-04-08 17:24:43 +02001893 if (options & LYD_NEW_PATH_OPAQ) {
1894 /* validate value */
Radek Krejci09c77442021-04-26 11:10:34 +02001895 r = lyd_value_validate(NULL, schema, value ? value : "", value_len, NULL, NULL, NULL);
Michal Vaskoe78faec2021-04-08 17:24:43 +02001896 if (r && (r != LY_EINCOMPLETE)) {
1897 /* creating opaque leaf */
Radek Krejci09c77442021-04-26 11:10:34 +02001898 LY_CHECK_GOTO(ret = lyd_create_opaq(ctx, schema->name, strlen(schema->name), value, value_len,
1899 schema->module->name, strlen(schema->module->name), NULL, 0, NULL, format, NULL, 0, &node),
1900 cleanup);
Michal Vaskoe78faec2021-04-08 17:24:43 +02001901 break;
1902 }
1903 }
1904
Michal Vaskoe78faec2021-04-08 17:24:43 +02001905 /* create a leaf instance */
Radek Krejci09c77442021-04-26 11:10:34 +02001906 LY_CHECK_GOTO(ret = lyd_create_term(schema, value, value_len, NULL, format, NULL, LYD_HINT_DATA, NULL,
1907 &node), cleanup);
Michal Vasko00cbf532020-06-15 13:58:47 +02001908 break;
1909 case LYS_ANYDATA:
1910 case LYS_ANYXML:
Michal Vasko366a4a12020-12-04 16:23:57 +01001911 LY_CHECK_GOTO(ret = lyd_create_any(schema, value, value_type, 0, &node), cleanup);
Michal Vasko00cbf532020-06-15 13:58:47 +02001912 break;
1913 default:
1914 LOGINT(ctx);
1915 ret = LY_EINT;
1916 goto cleanup;
1917 }
1918
1919 if (cur_parent) {
1920 /* connect to the parent */
1921 lyd_insert_node(cur_parent, NULL, node);
1922 } else if (parent) {
1923 /* connect to top-level siblings */
1924 lyd_insert_node(NULL, &parent, node);
1925 }
1926
Michal Vasko35880332020-12-08 13:08:12 +01001927next_iter:
Michal Vasko00cbf532020-06-15 13:58:47 +02001928 /* update remembered nodes */
1929 if (!nparent) {
1930 nparent = node;
1931 }
1932 nnode = node;
1933 }
1934
1935cleanup:
1936 lyxp_expr_free(ctx, exp);
Michal Vasko6741dc62021-02-04 09:27:45 +01001937 if (p) {
1938 while (orig_count > LY_ARRAY_COUNT(p)) {
1939 LY_ARRAY_INCREMENT(p);
1940 }
1941 }
Michal Vasko00cbf532020-06-15 13:58:47 +02001942 ly_path_free(ctx, p);
1943 if (!ret) {
1944 /* set out params only on success */
1945 if (new_parent) {
1946 *new_parent = nparent;
1947 }
1948 if (new_node) {
1949 *new_node = nnode;
1950 }
Michal Vaskof4b95ba2020-12-11 08:42:33 +01001951 } else {
1952 lyd_free_tree(nparent);
Michal Vasko00cbf532020-06-15 13:58:47 +02001953 }
1954 return ret;
1955}
1956
Radek Krejci95ccd1b2021-03-12 14:57:22 +01001957API LY_ERR
1958lyd_new_path(struct lyd_node *parent, const struct ly_ctx *ctx, const char *path, const char *value, uint32_t options,
1959 struct lyd_node **node)
1960{
Radek Krejci09c77442021-04-26 11:10:34 +02001961 LY_CHECK_ARG_RET(ctx, parent || ctx, path, (path[0] == '/') || parent,
1962 !(options & LYD_NEW_PATH_BIN_VALUE) || !(options & LYD_NEW_PATH_CANON_VALUE), LY_EINVAL);
1963
1964 return lyd_new_path_(parent, ctx, NULL, path, value, 0, LYD_ANYDATA_STRING, options, node, NULL);
Radek Krejci95ccd1b2021-03-12 14:57:22 +01001965}
1966
1967API LY_ERR
1968lyd_new_path2(struct lyd_node *parent, const struct ly_ctx *ctx, const char *path, const void *value,
Radek Krejci09c77442021-04-26 11:10:34 +02001969 size_t value_len, LYD_ANYDATA_VALUETYPE value_type, uint32_t options, struct lyd_node **new_parent,
1970 struct lyd_node **new_node)
Radek Krejci95ccd1b2021-03-12 14:57:22 +01001971{
Radek Krejci09c77442021-04-26 11:10:34 +02001972 LY_CHECK_ARG_RET(ctx, parent || ctx, path, (path[0] == '/') || parent,
1973 !(options & LYD_NEW_PATH_BIN_VALUE) || !(options & LYD_NEW_PATH_CANON_VALUE), LY_EINVAL);
1974 return lyd_new_path_(parent, ctx, NULL, path, value, value_len, value_type, options, new_parent, new_node);
Radek Krejci95ccd1b2021-03-12 14:57:22 +01001975}
1976
1977API LY_ERR
1978lyd_new_ext_path(struct lyd_node *parent, const struct lysc_ext_instance *ext, const char *path, const void *value,
1979 uint32_t options, struct lyd_node **node)
1980{
1981 const struct ly_ctx *ctx = ext ? ext->module->ctx : NULL;
1982
Radek Krejci09c77442021-04-26 11:10:34 +02001983 LY_CHECK_ARG_RET(ctx, ext, path, (path[0] == '/') || parent,
1984 !(options & LYD_NEW_PATH_BIN_VALUE) || !(options & LYD_NEW_PATH_CANON_VALUE), LY_EINVAL);
1985 return lyd_new_path_(parent, ctx, ext, path, value, 0, LYD_ANYDATA_STRING, options, node, NULL);
Radek Krejci95ccd1b2021-03-12 14:57:22 +01001986}
1987
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001988LY_ERR
1989lyd_new_implicit_r(struct lyd_node *parent, struct lyd_node **first, const struct lysc_node *sparent,
Radek Krejci4f2e3e52021-03-30 14:20:28 +02001990 const struct lys_module *mod, struct ly_set *node_when, struct ly_set *node_exts, struct ly_set *node_types,
1991 uint32_t impl_opts, struct lyd_node **diff)
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001992{
1993 LY_ERR ret;
Michal Vaskod1e53b92021-01-28 13:11:06 +01001994 const struct lysc_node *iter = NULL;
Radek Krejci2b18bf12020-11-06 11:20:20 +01001995 struct lyd_node *node = NULL;
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001996 struct lyd_value **dflts;
1997 LY_ARRAY_COUNT_TYPE u;
Michal Vasko630d9892020-12-08 17:11:08 +01001998 uint32_t getnext_opts;
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001999
2000 assert(first && (parent || sparent || mod));
2001
2002 if (!sparent && parent) {
2003 sparent = parent->schema;
2004 }
2005
Michal Vasko630d9892020-12-08 17:11:08 +01002006 getnext_opts = LYS_GETNEXT_WITHCHOICE;
2007 if (impl_opts & LYD_IMPLICIT_OUTPUT) {
2008 getnext_opts |= LYS_GETNEXT_OUTPUT;
2009 }
2010
2011 while ((iter = lys_getnext(iter, sparent, mod ? mod->compiled : NULL, getnext_opts))) {
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002012 if ((impl_opts & LYD_IMPLICIT_NO_STATE) && (iter->flags & LYS_CONFIG_R)) {
2013 continue;
Michal Vasko44b19a12020-08-07 09:21:30 +02002014 } else if ((impl_opts & LYD_IMPLICIT_NO_CONFIG) && (iter->flags & LYS_CONFIG_W)) {
2015 continue;
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002016 }
2017
2018 switch (iter->nodetype) {
2019 case LYS_CHOICE:
Michal Vaskobcf4d7d2020-11-18 18:16:49 +01002020 node = lys_getnext_data(NULL, *first, NULL, iter, NULL);
2021 if (!node && ((struct lysc_node_choice *)iter)->dflt) {
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002022 /* create default case data */
Michal Vasko14ed9cd2021-01-28 14:16:25 +01002023 LY_CHECK_RET(lyd_new_implicit_r(parent, first, &((struct lysc_node_choice *)iter)->dflt->node,
Radek Krejci4f2e3e52021-03-30 14:20:28 +02002024 NULL, node_when, node_exts, node_types, impl_opts, diff));
Michal Vaskobcf4d7d2020-11-18 18:16:49 +01002025 } else if (node) {
2026 /* create any default data in the existing case */
2027 assert(node->schema->parent->nodetype == LYS_CASE);
Radek Krejci4f2e3e52021-03-30 14:20:28 +02002028 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 +01002029 impl_opts, diff));
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002030 }
2031 break;
2032 case LYS_CONTAINER:
2033 if (!(iter->flags & LYS_PRESENCE) && lyd_find_sibling_val(*first, iter, NULL, 0, NULL)) {
2034 /* create default NP container */
2035 LY_CHECK_RET(lyd_create_inner(iter, &node));
Michal Vaskof4d67ea2021-03-31 13:53:21 +02002036 node->flags = LYD_DEFAULT | (lysc_has_when(iter) ? LYD_WHEN_TRUE : 0);
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002037 lyd_insert_node(parent, first, node);
2038
Michal Vaskof4d67ea2021-03-31 13:53:21 +02002039 if (lysc_has_when(iter) && node_when) {
Michal Vaskoe16c7b72021-02-26 10:39:06 +01002040 /* remember to resolve when */
2041 LY_CHECK_RET(ly_set_add(node_when, node, 1, NULL));
2042 }
Radek Krejci4f2e3e52021-03-30 14:20:28 +02002043 if (node_exts) {
2044 /* remember to call all the extension's validation callbacks */
2045 LY_CHECK_RET(lysc_node_ext_tovalidate(node_exts, node));
2046 }
Michal Vaskoe49b6322020-11-05 17:38:36 +01002047 if (diff) {
2048 /* add into diff */
2049 LY_CHECK_RET(lyd_val_diff_add(node, LYD_DIFF_OP_CREATE, diff));
2050 }
2051
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002052 /* create any default children */
Radek Krejci4f2e3e52021-03-30 14:20:28 +02002053 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 +02002054 impl_opts, diff));
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002055 }
2056 break;
2057 case LYS_LEAF:
Michal Vasko69730152020-10-09 16:30:07 +02002058 if (!(impl_opts & LYD_IMPLICIT_NO_DEFAULTS) && ((struct lysc_node_leaf *)iter)->dflt &&
2059 lyd_find_sibling_val(*first, iter, NULL, 0, NULL)) {
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002060 /* create default leaf */
2061 ret = lyd_create_term2(iter, ((struct lysc_node_leaf *)iter)->dflt, &node);
2062 if (ret == LY_EINCOMPLETE) {
2063 if (node_types) {
2064 /* remember to resolve type */
Radek Krejci3d92e442020-10-12 12:48:13 +02002065 LY_CHECK_RET(ly_set_add(node_types, node, 1, NULL));
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002066 }
2067 } else if (ret) {
2068 return ret;
2069 }
Michal Vaskof4d67ea2021-03-31 13:53:21 +02002070 node->flags = LYD_DEFAULT | (lysc_has_when(iter) ? LYD_WHEN_TRUE : 0);
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002071 lyd_insert_node(parent, first, node);
2072
Michal Vaskof4d67ea2021-03-31 13:53:21 +02002073 if (lysc_has_when(iter) && node_when) {
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002074 /* remember to resolve when */
Radek Krejci3d92e442020-10-12 12:48:13 +02002075 LY_CHECK_RET(ly_set_add(node_when, node, 1, NULL));
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002076 }
Radek Krejci4f2e3e52021-03-30 14:20:28 +02002077 if (node_exts) {
2078 /* remember to call all the extension's validation callbacks */
2079 LY_CHECK_RET(lysc_node_ext_tovalidate(node_exts, node));
2080 }
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002081 if (diff) {
2082 /* add into diff */
2083 LY_CHECK_RET(lyd_val_diff_add(node, LYD_DIFF_OP_CREATE, diff));
2084 }
2085 }
2086 break;
2087 case LYS_LEAFLIST:
Michal Vasko69730152020-10-09 16:30:07 +02002088 if (!(impl_opts & LYD_IMPLICIT_NO_DEFAULTS) && ((struct lysc_node_leaflist *)iter)->dflts &&
2089 lyd_find_sibling_val(*first, iter, NULL, 0, NULL)) {
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002090 /* create all default leaf-lists */
2091 dflts = ((struct lysc_node_leaflist *)iter)->dflts;
2092 LY_ARRAY_FOR(dflts, u) {
2093 ret = lyd_create_term2(iter, dflts[u], &node);
2094 if (ret == LY_EINCOMPLETE) {
2095 if (node_types) {
2096 /* remember to resolve type */
Radek Krejci3d92e442020-10-12 12:48:13 +02002097 LY_CHECK_RET(ly_set_add(node_types, node, 1, NULL));
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002098 }
2099 } else if (ret) {
2100 return ret;
2101 }
Michal Vaskof4d67ea2021-03-31 13:53:21 +02002102 node->flags = LYD_DEFAULT | (lysc_has_when(iter) ? LYD_WHEN_TRUE : 0);
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002103 lyd_insert_node(parent, first, node);
2104
Michal Vaskof4d67ea2021-03-31 13:53:21 +02002105 if (lysc_has_when(iter) && node_when) {
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002106 /* remember to resolve when */
Radek Krejci3d92e442020-10-12 12:48:13 +02002107 LY_CHECK_RET(ly_set_add(node_when, node, 1, NULL));
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002108 }
Radek Krejci4f2e3e52021-03-30 14:20:28 +02002109 if (node_exts) {
2110 /* remember to call all the extension's validation callbacks */
2111 LY_CHECK_RET(lysc_node_ext_tovalidate(node_exts, node));
2112 }
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002113 if (diff) {
2114 /* add into diff */
2115 LY_CHECK_RET(lyd_val_diff_add(node, LYD_DIFF_OP_CREATE, diff));
2116 }
2117 }
2118 }
2119 break;
2120 default:
2121 /* without defaults */
2122 break;
2123 }
2124 }
2125
2126 return LY_SUCCESS;
2127}
2128
2129API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02002130lyd_new_implicit_tree(struct lyd_node *tree, uint32_t implicit_options, struct lyd_node **diff)
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002131{
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002132 LY_ERR ret = LY_SUCCESS;
Michal Vasko3488ada2020-12-03 14:18:19 +01002133 struct lyd_node *node;
Radek Krejci4f2e3e52021-03-30 14:20:28 +02002134 struct ly_set node_when = {0}, node_exts = {0};
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002135
2136 LY_CHECK_ARG_RET(NULL, tree, LY_EINVAL);
2137 if (diff) {
2138 *diff = NULL;
2139 }
2140
Michal Vasko56daf732020-08-10 10:57:18 +02002141 LYD_TREE_DFS_BEGIN(tree, node) {
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002142 /* skip added default nodes */
Michal Vasko69730152020-10-09 16:30:07 +02002143 if (((node->flags & (LYD_DEFAULT | LYD_NEW)) != (LYD_DEFAULT | LYD_NEW)) &&
2144 (node->schema->nodetype & LYD_NODE_INNER)) {
Radek Krejci4f2e3e52021-03-30 14:20:28 +02002145 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 +01002146 NULL, implicit_options, diff), cleanup);
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002147 }
2148
Michal Vasko56daf732020-08-10 10:57:18 +02002149 LYD_TREE_DFS_END(tree, node);
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002150 }
2151
Michal Vasko3488ada2020-12-03 14:18:19 +01002152 /* resolve when and remove any invalid defaults */
Radek Krejci4f2e3e52021-03-30 14:20:28 +02002153 LY_CHECK_GOTO(ret = lyd_validate_unres(&tree, NULL, &node_when, &node_exts, NULL, NULL, diff), cleanup);
Michal Vasko3488ada2020-12-03 14:18:19 +01002154
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002155cleanup:
Michal Vasko3488ada2020-12-03 14:18:19 +01002156 ly_set_erase(&node_when, NULL);
Radek Krejci4f2e3e52021-03-30 14:20:28 +02002157 ly_set_erase(&node_exts, NULL);
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002158 if (ret && diff) {
2159 lyd_free_all(*diff);
2160 *diff = NULL;
2161 }
2162 return ret;
2163}
2164
2165API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02002166lyd_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 +02002167{
2168 const struct lys_module *mod;
2169 struct lyd_node *d = NULL;
2170 uint32_t i = 0;
2171 LY_ERR ret = LY_SUCCESS;
2172
2173 LY_CHECK_ARG_RET(ctx, tree, *tree || ctx, LY_EINVAL);
2174 if (diff) {
2175 *diff = NULL;
2176 }
2177 if (!ctx) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02002178 ctx = LYD_CTX(*tree);
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002179 }
2180
2181 /* add nodes for each module one-by-one */
2182 while ((mod = ly_ctx_get_module_iter(ctx, &i))) {
2183 if (!mod->implemented) {
2184 continue;
2185 }
2186
2187 LY_CHECK_GOTO(ret = lyd_new_implicit_module(tree, mod, implicit_options, diff ? &d : NULL), cleanup);
2188 if (d) {
2189 /* merge into one diff */
2190 lyd_insert_sibling(*diff, d, diff);
2191
2192 d = NULL;
2193 }
2194 }
2195
2196cleanup:
2197 if (ret && diff) {
2198 lyd_free_all(*diff);
2199 *diff = NULL;
2200 }
2201 return ret;
2202}
2203
2204API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02002205lyd_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 +02002206{
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002207 LY_ERR ret = LY_SUCCESS;
Michal Vasko3488ada2020-12-03 14:18:19 +01002208 struct lyd_node *root, *d = NULL;
Radek Krejci4f2e3e52021-03-30 14:20:28 +02002209 struct ly_set node_when = {0}, node_exts = {0};
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002210
2211 LY_CHECK_ARG_RET(NULL, tree, module, LY_EINVAL);
2212 if (diff) {
2213 *diff = NULL;
2214 }
2215
2216 /* add all top-level defaults for this module */
Radek Krejci4f2e3e52021-03-30 14:20:28 +02002217 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 +01002218
2219 /* resolve when and remove any invalid defaults */
Radek Krejci4f2e3e52021-03-30 14:20:28 +02002220 LY_CHECK_GOTO(ret = lyd_validate_unres(tree, module, &node_when, &node_exts, NULL, NULL, diff), cleanup);
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002221
2222 /* process nested nodes */
2223 LY_LIST_FOR(*tree, root) {
2224 /* skip added default nodes */
2225 if ((root->flags & (LYD_DEFAULT | LYD_NEW)) != (LYD_DEFAULT | LYD_NEW)) {
2226 LY_CHECK_GOTO(ret = lyd_new_implicit_tree(root, implicit_options, diff ? &d : NULL), cleanup);
2227
2228 if (d) {
2229 /* merge into one diff */
2230 lyd_insert_sibling(*diff, d, diff);
2231
2232 d = NULL;
2233 }
2234 }
2235 }
2236
2237cleanup:
Michal Vasko3488ada2020-12-03 14:18:19 +01002238 ly_set_erase(&node_when, NULL);
Radek Krejci4f2e3e52021-03-30 14:20:28 +02002239 ly_set_erase(&node_exts, NULL);
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002240 if (ret && diff) {
2241 lyd_free_all(*diff);
2242 *diff = NULL;
2243 }
2244 return ret;
2245}
2246
Michal Vasko90932a92020-02-12 14:33:03 +01002247struct lyd_node *
Michal Vaskob104f112020-07-17 09:54:54 +02002248lyd_insert_get_next_anchor(const struct lyd_node *first_sibling, const struct lyd_node *new_node)
Michal Vasko90932a92020-02-12 14:33:03 +01002249{
Michal Vaskob104f112020-07-17 09:54:54 +02002250 const struct lysc_node *schema, *sparent;
Michal Vasko90932a92020-02-12 14:33:03 +01002251 struct lyd_node *match = NULL;
Radek Krejci857189e2020-09-01 13:26:36 +02002252 ly_bool found;
Michal Vasko93b16062020-12-09 18:14:18 +01002253 uint32_t getnext_opts;
Michal Vasko90932a92020-02-12 14:33:03 +01002254
Michal Vaskob104f112020-07-17 09:54:54 +02002255 assert(new_node);
2256
2257 if (!first_sibling || !new_node->schema) {
2258 /* insert at the end, no next anchor */
Michal Vasko90932a92020-02-12 14:33:03 +01002259 return NULL;
2260 }
2261
Michal Vasko93b16062020-12-09 18:14:18 +01002262 getnext_opts = 0;
Michal Vaskod1e53b92021-01-28 13:11:06 +01002263 if (new_node->schema->flags & LYS_IS_OUTPUT) {
Michal Vasko93b16062020-12-09 18:14:18 +01002264 getnext_opts = LYS_GETNEXT_OUTPUT;
2265 }
2266
Michal Vaskob104f112020-07-17 09:54:54 +02002267 if (first_sibling->parent && first_sibling->parent->children_ht) {
2268 /* find the anchor using hashes */
2269 sparent = first_sibling->parent->schema;
Michal Vasko93b16062020-12-09 18:14:18 +01002270 schema = lys_getnext(new_node->schema, sparent, NULL, getnext_opts);
Michal Vaskob104f112020-07-17 09:54:54 +02002271 while (schema) {
2272 /* keep trying to find the first existing instance of the closest following schema sibling,
2273 * otherwise return NULL - inserting at the end */
2274 if (!lyd_find_sibling_schema(first_sibling, schema, &match)) {
2275 break;
2276 }
2277
Michal Vasko93b16062020-12-09 18:14:18 +01002278 schema = lys_getnext(schema, sparent, NULL, getnext_opts);
Michal Vaskob104f112020-07-17 09:54:54 +02002279 }
2280 } else {
2281 /* find the anchor without hashes */
2282 match = (struct lyd_node *)first_sibling;
2283 if (!lysc_data_parent(new_node->schema)) {
2284 /* we are in top-level, skip all the data from preceding modules */
2285 LY_LIST_FOR(match, match) {
2286 if (!match->schema || (strcmp(lyd_owner_module(match)->name, lyd_owner_module(new_node)->name) >= 0)) {
2287 break;
2288 }
2289 }
2290 }
2291
2292 /* get the first schema sibling */
2293 sparent = lysc_data_parent(new_node->schema);
Michal Vasko93b16062020-12-09 18:14:18 +01002294 schema = lys_getnext(NULL, sparent, new_node->schema->module->compiled, getnext_opts);
Michal Vaskob104f112020-07-17 09:54:54 +02002295
2296 found = 0;
2297 LY_LIST_FOR(match, match) {
2298 if (!match->schema || (lyd_owner_module(match) != lyd_owner_module(new_node))) {
2299 /* we have found an opaque node, which must be at the end, so use it OR
2300 * modules do not match, so we must have traversed all the data from new_node module (if any),
2301 * we have found the first node of the next module, that is what we want */
2302 break;
2303 }
2304
2305 /* skip schema nodes until we find the instantiated one */
2306 while (!found) {
2307 if (new_node->schema == schema) {
2308 /* we have found the schema of the new node, continue search to find the first
2309 * data node with a different schema (after our schema) */
2310 found = 1;
2311 break;
2312 }
2313 if (match->schema == schema) {
2314 /* current node (match) is a data node still before the new node, continue search in data */
2315 break;
2316 }
Michal Vasko93b16062020-12-09 18:14:18 +01002317 schema = lys_getnext(schema, sparent, new_node->schema->module->compiled, getnext_opts);
Michal Vaskob104f112020-07-17 09:54:54 +02002318 assert(schema);
2319 }
2320
2321 if (found && (match->schema != new_node->schema)) {
2322 /* find the next node after we have found our node schema data instance */
2323 break;
2324 }
2325 }
Michal Vasko90932a92020-02-12 14:33:03 +01002326 }
2327
2328 return match;
2329}
2330
2331/**
2332 * @brief Insert node after a sibling.
2333 *
Michal Vasko9f96a052020-03-10 09:41:45 +01002334 * Handles inserting into NP containers and key-less lists.
2335 *
Michal Vasko90932a92020-02-12 14:33:03 +01002336 * @param[in] sibling Sibling to insert after.
2337 * @param[in] node Node to insert.
2338 */
2339static void
Michal Vaskof03ed032020-03-04 13:31:44 +01002340lyd_insert_after_node(struct lyd_node *sibling, struct lyd_node *node)
Michal Vasko90932a92020-02-12 14:33:03 +01002341{
Michal Vasko0249f7c2020-03-05 16:36:40 +01002342 struct lyd_node_inner *par;
2343
Michal Vasko90932a92020-02-12 14:33:03 +01002344 assert(!node->next && (node->prev == node));
2345
2346 node->next = sibling->next;
2347 node->prev = sibling;
2348 sibling->next = node;
2349 if (node->next) {
2350 /* sibling had a succeeding node */
2351 node->next->prev = node;
2352 } else {
2353 /* sibling was last, find first sibling and change its prev */
2354 if (sibling->parent) {
2355 sibling = sibling->parent->child;
2356 } else {
Michal Vaskod989ba02020-08-24 10:59:24 +02002357 for ( ; sibling->prev->next != node; sibling = sibling->prev) {}
Michal Vasko90932a92020-02-12 14:33:03 +01002358 }
2359 sibling->prev = node;
2360 }
2361 node->parent = sibling->parent;
Michal Vasko0249f7c2020-03-05 16:36:40 +01002362
Michal Vasko9f96a052020-03-10 09:41:45 +01002363 for (par = node->parent; par; par = par->parent) {
2364 if ((par->flags & LYD_DEFAULT) && !(node->flags & LYD_DEFAULT)) {
2365 /* remove default flags from NP containers */
Michal Vasko0249f7c2020-03-05 16:36:40 +01002366 par->flags &= ~LYD_DEFAULT;
2367 }
2368 }
Michal Vasko90932a92020-02-12 14:33:03 +01002369}
2370
2371/**
2372 * @brief Insert node before a sibling.
2373 *
Michal Vasko9f96a052020-03-10 09:41:45 +01002374 * Handles inserting into NP containers and key-less lists.
2375 *
Michal Vasko90932a92020-02-12 14:33:03 +01002376 * @param[in] sibling Sibling to insert before.
2377 * @param[in] node Node to insert.
2378 */
2379static void
Michal Vaskof03ed032020-03-04 13:31:44 +01002380lyd_insert_before_node(struct lyd_node *sibling, struct lyd_node *node)
Michal Vasko90932a92020-02-12 14:33:03 +01002381{
Michal Vasko0249f7c2020-03-05 16:36:40 +01002382 struct lyd_node_inner *par;
2383
Michal Vasko90932a92020-02-12 14:33:03 +01002384 assert(!node->next && (node->prev == node));
2385
2386 node->next = sibling;
2387 /* covers situation of sibling being first */
2388 node->prev = sibling->prev;
2389 sibling->prev = node;
2390 if (node->prev->next) {
2391 /* sibling had a preceding node */
2392 node->prev->next = node;
2393 } else if (sibling->parent) {
2394 /* sibling was first and we must also change parent child pointer */
2395 sibling->parent->child = node;
2396 }
2397 node->parent = sibling->parent;
Michal Vasko0249f7c2020-03-05 16:36:40 +01002398
Michal Vasko9f96a052020-03-10 09:41:45 +01002399 for (par = node->parent; par; par = par->parent) {
2400 if ((par->flags & LYD_DEFAULT) && !(node->flags & LYD_DEFAULT)) {
2401 /* remove default flags from NP containers */
Michal Vasko0249f7c2020-03-05 16:36:40 +01002402 par->flags &= ~LYD_DEFAULT;
2403 }
2404 }
Michal Vasko90932a92020-02-12 14:33:03 +01002405}
2406
2407/**
Michal Vaskob104f112020-07-17 09:54:54 +02002408 * @brief Insert node as the first and only child of a parent.
Michal Vasko90932a92020-02-12 14:33:03 +01002409 *
Michal Vasko9f96a052020-03-10 09:41:45 +01002410 * Handles inserting into NP containers and key-less lists.
2411 *
Michal Vasko90932a92020-02-12 14:33:03 +01002412 * @param[in] parent Parent to insert into.
2413 * @param[in] node Node to insert.
2414 */
2415static void
Michal Vaskob104f112020-07-17 09:54:54 +02002416lyd_insert_only_child(struct lyd_node *parent, struct lyd_node *node)
Michal Vasko90932a92020-02-12 14:33:03 +01002417{
2418 struct lyd_node_inner *par;
2419
Radek Krejcia1c1e542020-09-29 16:06:52 +02002420 assert(parent && !lyd_child(parent) && !node->next && (node->prev == node));
Michal Vasko52927e22020-03-16 17:26:14 +01002421 assert(!parent->schema || (parent->schema->nodetype & LYD_NODE_INNER));
Michal Vasko90932a92020-02-12 14:33:03 +01002422
2423 par = (struct lyd_node_inner *)parent;
2424
Michal Vaskob104f112020-07-17 09:54:54 +02002425 par->child = node;
Michal Vasko90932a92020-02-12 14:33:03 +01002426 node->parent = par;
Michal Vasko0249f7c2020-03-05 16:36:40 +01002427
Michal Vaskod989ba02020-08-24 10:59:24 +02002428 for ( ; par; par = par->parent) {
Michal Vasko9f96a052020-03-10 09:41:45 +01002429 if ((par->flags & LYD_DEFAULT) && !(node->flags & LYD_DEFAULT)) {
2430 /* remove default flags from NP containers */
Michal Vasko0249f7c2020-03-05 16:36:40 +01002431 par->flags &= ~LYD_DEFAULT;
2432 }
2433 }
Michal Vasko751cb4d2020-07-14 12:25:28 +02002434}
Michal Vasko0249f7c2020-03-05 16:36:40 +01002435
Michal Vasko751cb4d2020-07-14 12:25:28 +02002436/**
2437 * @brief Learn whether a list instance has all the keys.
2438 *
2439 * @param[in] list List instance to check.
2440 * @return non-zero if all the keys were found,
2441 * @return 0 otherwise.
2442 */
2443static int
2444lyd_insert_has_keys(const struct lyd_node *list)
2445{
2446 const struct lyd_node *key;
2447 const struct lysc_node *skey = NULL;
2448
2449 assert(list->schema->nodetype == LYS_LIST);
Radek Krejcia1c1e542020-09-29 16:06:52 +02002450 key = lyd_child(list);
Michal Vasko751cb4d2020-07-14 12:25:28 +02002451 while ((skey = lys_getnext(skey, list->schema, NULL, 0)) && (skey->flags & LYS_KEY)) {
2452 if (!key || (key->schema != skey)) {
2453 /* key missing */
2454 return 0;
2455 }
2456
2457 key = key->next;
2458 }
2459
2460 /* all keys found */
2461 return 1;
Michal Vasko90932a92020-02-12 14:33:03 +01002462}
2463
2464void
Michal Vaskob104f112020-07-17 09:54:54 +02002465lyd_insert_node(struct lyd_node *parent, struct lyd_node **first_sibling_p, struct lyd_node *node)
Michal Vasko90932a92020-02-12 14:33:03 +01002466{
Michal Vaskob104f112020-07-17 09:54:54 +02002467 struct lyd_node *anchor, *first_sibling;
Michal Vasko90932a92020-02-12 14:33:03 +01002468
Michal Vaskob104f112020-07-17 09:54:54 +02002469 /* inserting list without its keys is not supported */
2470 assert((parent || first_sibling_p) && node && (node->hash || !node->schema));
Michal Vaskof756c942020-11-06 17:21:37 +01002471 assert(!parent || !parent->schema ||
2472 (parent->schema->nodetype & (LYS_CONTAINER | LYS_LIST | LYS_RPC | LYS_ACTION | LYS_NOTIF)));
Michal Vasko9b368d32020-02-14 13:53:31 +01002473
Michal Vaskob104f112020-07-17 09:54:54 +02002474 if (!parent && first_sibling_p && (*first_sibling_p) && (*first_sibling_p)->parent) {
Michal Vasko9e685082021-01-29 14:49:09 +01002475 parent = lyd_parent(*first_sibling_p);
Michal Vasko9b368d32020-02-14 13:53:31 +01002476 }
Michal Vasko90932a92020-02-12 14:33:03 +01002477
Michal Vaskob104f112020-07-17 09:54:54 +02002478 /* get first sibling */
Michal Vasko9e685082021-01-29 14:49:09 +01002479 first_sibling = parent ? lyd_child(parent) : *first_sibling_p;
Michal Vasko9f96a052020-03-10 09:41:45 +01002480
Michal Vaskob104f112020-07-17 09:54:54 +02002481 /* find the anchor, our next node, so we can insert before it */
2482 anchor = lyd_insert_get_next_anchor(first_sibling, node);
2483 if (anchor) {
2484 lyd_insert_before_node(anchor, node);
Michal Vasko26123192020-11-09 21:02:34 +01002485 if (!parent && (*first_sibling_p == anchor)) {
2486 /* move first sibling */
2487 *first_sibling_p = node;
2488 }
Michal Vaskob104f112020-07-17 09:54:54 +02002489 } else if (first_sibling) {
2490 lyd_insert_after_node(first_sibling->prev, node);
2491 } else if (parent) {
2492 lyd_insert_only_child(parent, node);
Michal Vasko90932a92020-02-12 14:33:03 +01002493 } else {
Michal Vaskob104f112020-07-17 09:54:54 +02002494 *first_sibling_p = node;
2495 }
2496
2497 /* insert into parent HT */
2498 lyd_insert_hash(node);
2499
2500 /* finish hashes for our parent, if needed and possible */
Michal Vasko9e8de2d2020-09-01 08:17:10 +02002501 if (node->schema && (node->schema->flags & LYS_KEY) && parent && lyd_insert_has_keys(parent)) {
Michal Vaskob104f112020-07-17 09:54:54 +02002502 lyd_hash(parent);
2503
2504 /* now we can insert even the list into its parent HT */
2505 lyd_insert_hash(parent);
Michal Vasko90932a92020-02-12 14:33:03 +01002506 }
Michal Vasko90932a92020-02-12 14:33:03 +01002507}
2508
Michal Vasko717a4c32020-12-07 09:40:10 +01002509/**
2510 * @brief Check schema place of a node to be inserted.
2511 *
2512 * @param[in] parent Schema node of the parent data node.
2513 * @param[in] sibling Schema node of a sibling data node.
2514 * @param[in] schema Schema node if the data node to be inserted.
2515 * @return LY_SUCCESS on success.
2516 * @return LY_EINVAL if the place is invalid.
2517 */
Michal Vaskof03ed032020-03-04 13:31:44 +01002518static LY_ERR
Michal Vasko717a4c32020-12-07 09:40:10 +01002519lyd_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 +01002520{
2521 const struct lysc_node *par2;
2522
Michal Vasko62ed12d2020-05-21 10:08:25 +02002523 assert(!parent || !(parent->nodetype & (LYS_CASE | LYS_CHOICE)));
Michal Vasko717a4c32020-12-07 09:40:10 +01002524 assert(!sibling || !(sibling->nodetype & (LYS_CASE | LYS_CHOICE)));
2525 assert(!schema || !(schema->nodetype & (LYS_CASE | LYS_CHOICE)));
Michal Vaskof03ed032020-03-04 13:31:44 +01002526
Michal Vasko717a4c32020-12-07 09:40:10 +01002527 if (!schema || (!parent && !sibling)) {
Michal Vasko71e795e2020-12-04 16:27:37 +01002528 /* opaque nodes can be inserted wherever */
2529 return LY_SUCCESS;
2530 }
2531
Michal Vasko717a4c32020-12-07 09:40:10 +01002532 if (!parent) {
2533 parent = lysc_data_parent(sibling);
2534 }
2535
Michal Vaskof03ed032020-03-04 13:31:44 +01002536 /* find schema parent */
Michal Vasko62ed12d2020-05-21 10:08:25 +02002537 par2 = lysc_data_parent(schema);
Michal Vaskof03ed032020-03-04 13:31:44 +01002538
2539 if (parent) {
2540 /* inner node */
2541 if (par2 != parent) {
Michal Vaskob104f112020-07-17 09:54:54 +02002542 LOGERR(schema->module->ctx, LY_EINVAL, "Cannot insert, parent of \"%s\" is not \"%s\".", schema->name,
Michal Vasko69730152020-10-09 16:30:07 +02002543 parent->name);
Michal Vaskof03ed032020-03-04 13:31:44 +01002544 return LY_EINVAL;
2545 }
2546 } else {
2547 /* top-level node */
2548 if (par2) {
Radek Krejcif6d14cb2020-07-02 16:11:45 +02002549 LOGERR(schema->module->ctx, LY_EINVAL, "Cannot insert, node \"%s\" is not top-level.", schema->name);
Michal Vaskof03ed032020-03-04 13:31:44 +01002550 return LY_EINVAL;
2551 }
2552 }
2553
2554 return LY_SUCCESS;
2555}
2556
2557API LY_ERR
Michal Vaskob104f112020-07-17 09:54:54 +02002558lyd_insert_child(struct lyd_node *parent, struct lyd_node *node)
Michal Vaskof03ed032020-03-04 13:31:44 +01002559{
2560 struct lyd_node *iter;
2561
Michal Vasko0ab974d2021-02-24 13:18:26 +01002562 LY_CHECK_ARG_RET(NULL, parent, node, !parent->schema || (parent->schema->nodetype & LYD_NODE_INNER), LY_EINVAL);
Michal Vaskof03ed032020-03-04 13:31:44 +01002563
Michal Vasko717a4c32020-12-07 09:40:10 +01002564 LY_CHECK_RET(lyd_insert_check_schema(parent->schema, NULL, node->schema));
Michal Vaskof03ed032020-03-04 13:31:44 +01002565
Michal Vasko0ab974d2021-02-24 13:18:26 +01002566 if (node->schema && (node->schema->flags & LYS_KEY)) {
Michal Vaskof03ed032020-03-04 13:31:44 +01002567 LOGERR(parent->schema->module->ctx, LY_EINVAL, "Cannot insert key \"%s\".", node->schema->name);
2568 return LY_EINVAL;
2569 }
2570
2571 if (node->parent || node->prev->next) {
2572 lyd_unlink_tree(node);
2573 }
2574
2575 while (node) {
2576 iter = node->next;
2577 lyd_unlink_tree(node);
2578 lyd_insert_node(parent, NULL, node);
2579 node = iter;
2580 }
2581 return LY_SUCCESS;
2582}
2583
2584API LY_ERR
Michal Vaskob104f112020-07-17 09:54:54 +02002585lyd_insert_sibling(struct lyd_node *sibling, struct lyd_node *node, struct lyd_node **first)
Michal Vaskob1b5c262020-03-05 14:29:47 +01002586{
2587 struct lyd_node *iter;
2588
Michal Vaskob104f112020-07-17 09:54:54 +02002589 LY_CHECK_ARG_RET(NULL, node, LY_EINVAL);
Michal Vaskob1b5c262020-03-05 14:29:47 +01002590
Michal Vaskob104f112020-07-17 09:54:54 +02002591 if (sibling) {
Michal Vasko717a4c32020-12-07 09:40:10 +01002592 LY_CHECK_RET(lyd_insert_check_schema(NULL, sibling->schema, node->schema));
Michal Vaskob1b5c262020-03-05 14:29:47 +01002593 }
2594
Michal Vasko553d0b52020-12-04 16:27:52 +01002595 if (sibling == node) {
2596 /* we need to keep the connection to siblings so we can insert into them */
2597 sibling = sibling->prev;
2598 }
2599
Michal Vaskob1b5c262020-03-05 14:29:47 +01002600 if (node->parent || node->prev->next) {
2601 lyd_unlink_tree(node);
2602 }
2603
2604 while (node) {
Michal Vasko71e795e2020-12-04 16:27:37 +01002605 if (lysc_is_key(node->schema)) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02002606 LOGERR(LYD_CTX(node), LY_EINVAL, "Cannot insert key \"%s\".", node->schema->name);
Michal Vaskob104f112020-07-17 09:54:54 +02002607 return LY_EINVAL;
2608 }
2609
Michal Vaskob1b5c262020-03-05 14:29:47 +01002610 iter = node->next;
2611 lyd_unlink_tree(node);
2612 lyd_insert_node(NULL, &sibling, node);
2613 node = iter;
2614 }
Michal Vaskob1b5c262020-03-05 14:29:47 +01002615
Michal Vaskob104f112020-07-17 09:54:54 +02002616 if (first) {
2617 /* find the first sibling */
2618 *first = sibling;
2619 while ((*first)->prev->next) {
2620 *first = (*first)->prev;
Michal Vasko0249f7c2020-03-05 16:36:40 +01002621 }
2622 }
2623
2624 return LY_SUCCESS;
2625}
2626
Michal Vaskob1b5c262020-03-05 14:29:47 +01002627API LY_ERR
Michal Vaskof03ed032020-03-04 13:31:44 +01002628lyd_insert_before(struct lyd_node *sibling, struct lyd_node *node)
2629{
Michal Vaskof03ed032020-03-04 13:31:44 +01002630 LY_CHECK_ARG_RET(NULL, sibling, node, LY_EINVAL);
2631
Michal Vasko717a4c32020-12-07 09:40:10 +01002632 LY_CHECK_RET(lyd_insert_check_schema(NULL, sibling->schema, node->schema));
Michal Vaskof03ed032020-03-04 13:31:44 +01002633
Michal Vaskob104f112020-07-17 09:54:54 +02002634 if (!(node->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) || !(node->schema->flags & LYS_ORDBY_USER)) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02002635 LOGERR(LYD_CTX(sibling), LY_EINVAL, "Can be used only for user-ordered nodes.");
Michal Vaskof03ed032020-03-04 13:31:44 +01002636 return LY_EINVAL;
2637 }
2638
Michal Vasko4bc2ce32020-12-08 10:06:16 +01002639 lyd_unlink_tree(node);
2640 lyd_insert_before_node(sibling, node);
2641 lyd_insert_hash(node);
Michal Vaskof03ed032020-03-04 13:31:44 +01002642
Michal Vaskof03ed032020-03-04 13:31:44 +01002643 return LY_SUCCESS;
2644}
2645
2646API LY_ERR
2647lyd_insert_after(struct lyd_node *sibling, struct lyd_node *node)
2648{
Michal Vaskof03ed032020-03-04 13:31:44 +01002649 LY_CHECK_ARG_RET(NULL, sibling, node, LY_EINVAL);
2650
Michal Vasko717a4c32020-12-07 09:40:10 +01002651 LY_CHECK_RET(lyd_insert_check_schema(NULL, sibling->schema, node->schema));
Michal Vaskof03ed032020-03-04 13:31:44 +01002652
Michal Vaskob104f112020-07-17 09:54:54 +02002653 if (!(node->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) || !(node->schema->flags & LYS_ORDBY_USER)) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02002654 LOGERR(LYD_CTX(sibling), LY_EINVAL, "Can be used only for user-ordered nodes.");
Michal Vaskof03ed032020-03-04 13:31:44 +01002655 return LY_EINVAL;
2656 }
2657
Michal Vasko4bc2ce32020-12-08 10:06:16 +01002658 lyd_unlink_tree(node);
2659 lyd_insert_after_node(sibling, node);
2660 lyd_insert_hash(node);
Michal Vaskof03ed032020-03-04 13:31:44 +01002661
Michal Vaskof03ed032020-03-04 13:31:44 +01002662 return LY_SUCCESS;
2663}
2664
2665API void
2666lyd_unlink_tree(struct lyd_node *node)
2667{
2668 struct lyd_node *iter;
2669
2670 if (!node) {
2671 return;
2672 }
2673
Michal Vaskob104f112020-07-17 09:54:54 +02002674 /* update hashes while still linked into the tree */
2675 lyd_unlink_hash(node);
2676
Michal Vaskof03ed032020-03-04 13:31:44 +01002677 /* unlink from siblings */
2678 if (node->prev->next) {
2679 node->prev->next = node->next;
2680 }
2681 if (node->next) {
2682 node->next->prev = node->prev;
2683 } else {
2684 /* unlinking the last node */
2685 if (node->parent) {
2686 iter = node->parent->child;
2687 } else {
2688 iter = node->prev;
2689 while (iter->prev != node) {
2690 iter = iter->prev;
2691 }
2692 }
2693 /* update the "last" pointer from the first node */
2694 iter->prev = node->prev;
2695 }
2696
2697 /* unlink from parent */
2698 if (node->parent) {
2699 if (node->parent->child == node) {
2700 /* the node is the first child */
2701 node->parent->child = node->next;
2702 }
2703
Michal Vaskoab49dbe2020-07-17 12:32:47 +02002704 /* check for NP container whether its last non-default node is not being unlinked */
Michal Vasko69730152020-10-09 16:30:07 +02002705 if (node->parent->schema && (node->parent->schema->nodetype == LYS_CONTAINER) &&
2706 !(node->parent->flags & LYD_DEFAULT) && !(node->parent->schema->flags & LYS_PRESENCE)) {
Michal Vaskoab49dbe2020-07-17 12:32:47 +02002707 LY_LIST_FOR(node->parent->child, iter) {
2708 if ((iter != node) && !(iter->flags & LYD_DEFAULT)) {
2709 break;
2710 }
2711 }
2712 if (!iter) {
2713 node->parent->flags |= LYD_DEFAULT;
2714 }
2715 }
2716
Michal Vaskof03ed032020-03-04 13:31:44 +01002717 node->parent = NULL;
2718 }
2719
2720 node->next = NULL;
2721 node->prev = node;
2722}
2723
Michal Vaskoa5da3292020-08-12 13:10:50 +02002724void
Michal Vasko871a0252020-11-11 18:35:24 +01002725lyd_insert_meta(struct lyd_node *parent, struct lyd_meta *meta, ly_bool clear_dflt)
Radek Krejci1798aae2020-07-14 13:26:06 +02002726{
2727 struct lyd_meta *last, *iter;
2728
2729 assert(parent);
Michal Vaskoa5da3292020-08-12 13:10:50 +02002730
2731 if (!meta) {
2732 return;
2733 }
Radek Krejci1798aae2020-07-14 13:26:06 +02002734
2735 for (iter = meta; iter; iter = iter->next) {
2736 iter->parent = parent;
2737 }
2738
2739 /* insert as the last attribute */
2740 if (parent->meta) {
Radek Krejci1e008d22020-08-17 11:37:37 +02002741 for (last = parent->meta; last->next; last = last->next) {}
Radek Krejci1798aae2020-07-14 13:26:06 +02002742 last->next = meta;
2743 } else {
2744 parent->meta = meta;
2745 }
2746
2747 /* remove default flags from NP containers */
Michal Vasko871a0252020-11-11 18:35:24 +01002748 while (clear_dflt && parent && (parent->schema->nodetype == LYS_CONTAINER) && (parent->flags & LYD_DEFAULT)) {
Radek Krejci1798aae2020-07-14 13:26:06 +02002749 parent->flags &= ~LYD_DEFAULT;
Michal Vasko9e685082021-01-29 14:49:09 +01002750 parent = lyd_parent(parent);
Radek Krejci1798aae2020-07-14 13:26:06 +02002751 }
Radek Krejci1798aae2020-07-14 13:26:06 +02002752}
2753
2754LY_ERR
Michal Vasko9f96a052020-03-10 09:41:45 +01002755lyd_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 +02002756 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 +01002757 void *prefix_data, uint32_t hints, ly_bool clear_dflt, ly_bool *incomplete)
Michal Vasko90932a92020-02-12 14:33:03 +01002758{
Radek Krejci2efc45b2020-12-22 16:25:44 +01002759 LY_ERR ret = LY_SUCCESS;
Michal Vasko90932a92020-02-12 14:33:03 +01002760 struct lysc_ext_instance *ant = NULL;
Michal Vasko9f96a052020-03-10 09:41:45 +01002761 struct lyd_meta *mt, *last;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002762 LY_ARRAY_COUNT_TYPE u;
Michal Vasko90932a92020-02-12 14:33:03 +01002763
Michal Vasko9f96a052020-03-10 09:41:45 +01002764 assert((parent || meta) && mod);
Michal Vasko6f4cbb62020-02-28 11:15:47 +01002765
Radek Krejciddace2c2021-01-08 11:30:56 +01002766 LOG_LOCSET(parent ? parent->schema : NULL, parent, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +01002767
Radek Krejci7eb54ba2020-05-18 16:30:04 +02002768 LY_ARRAY_FOR(mod->compiled->exts, u) {
Radek Krejci3e6632f2021-03-22 22:08:21 +01002769 if ((mod->compiled->exts[u].def->plugin == lyplg_find(LYPLG_EXTENSION, LYEXT_PLUGIN_INTERNAL_ANNOTATION)) &&
Radek Krejci7eb54ba2020-05-18 16:30:04 +02002770 !ly_strncmp(mod->compiled->exts[u].argument, name, name_len)) {
Michal Vasko90932a92020-02-12 14:33:03 +01002771 /* we have the annotation definition */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02002772 ant = &mod->compiled->exts[u];
Michal Vasko90932a92020-02-12 14:33:03 +01002773 break;
2774 }
2775 }
2776 if (!ant) {
2777 /* attribute is not defined as a metadata annotation (RFC 7952) */
Radek Krejci2efc45b2020-12-22 16:25:44 +01002778 LOGVAL(mod->ctx, LYVE_REFERENCE, "Annotation definition for attribute \"%s:%.*s\" not found.",
Radek Krejci422afb12021-03-04 16:38:16 +01002779 mod->name, (int)name_len, name);
Radek Krejci2efc45b2020-12-22 16:25:44 +01002780 ret = LY_EINVAL;
2781 goto cleanup;
Michal Vasko90932a92020-02-12 14:33:03 +01002782 }
2783
Michal Vasko9f96a052020-03-10 09:41:45 +01002784 mt = calloc(1, sizeof *mt);
Radek Krejci2efc45b2020-12-22 16:25:44 +01002785 LY_CHECK_ERR_GOTO(!mt, LOGMEM(mod->ctx); ret = LY_EMEM, cleanup);
Michal Vasko9f96a052020-03-10 09:41:45 +01002786 mt->parent = parent;
2787 mt->annotation = ant;
Radek Krejci1b2eef82021-02-17 11:17:27 +01002788 ret = lyd_value_store(mod->ctx, &mt->value, *(const struct lysc_type **)ant->substmts[ANNOTATION_SUBSTMT_TYPE].storage,
2789 value, value_len, dynamic, format, prefix_data, hints, parent ? parent->schema : NULL, incomplete);
Radek Krejci2efc45b2020-12-22 16:25:44 +01002790 LY_CHECK_ERR_GOTO(ret, free(mt), cleanup);
2791 ret = lydict_insert(mod->ctx, name, name_len, &mt->name);
2792 LY_CHECK_ERR_GOTO(ret, free(mt), cleanup);
Michal Vasko90932a92020-02-12 14:33:03 +01002793
Michal Vasko6f4cbb62020-02-28 11:15:47 +01002794 /* insert as the last attribute */
2795 if (parent) {
Michal Vasko871a0252020-11-11 18:35:24 +01002796 lyd_insert_meta(parent, mt, clear_dflt);
Michal Vasko9f96a052020-03-10 09:41:45 +01002797 } else if (*meta) {
Radek Krejci1e008d22020-08-17 11:37:37 +02002798 for (last = *meta; last->next; last = last->next) {}
Michal Vasko9f96a052020-03-10 09:41:45 +01002799 last->next = mt;
Michal Vasko90932a92020-02-12 14:33:03 +01002800 }
2801
Michal Vasko9f96a052020-03-10 09:41:45 +01002802 if (meta) {
2803 *meta = mt;
Michal Vasko90932a92020-02-12 14:33:03 +01002804 }
Radek Krejci2efc45b2020-12-22 16:25:44 +01002805
2806cleanup:
Radek Krejciddace2c2021-01-08 11:30:56 +01002807 LOG_LOCBACK((parent && parent->schema) ? 1 : 0, parent ? 1 : 0, 0, 0);
Radek Krejci2efc45b2020-12-22 16:25:44 +01002808 return ret;
Michal Vasko90932a92020-02-12 14:33:03 +01002809}
2810
Michal Vaskoa5da3292020-08-12 13:10:50 +02002811void
2812lyd_insert_attr(struct lyd_node *parent, struct lyd_attr *attr)
2813{
2814 struct lyd_attr *last, *iter;
2815 struct lyd_node_opaq *opaq;
2816
2817 assert(parent && !parent->schema);
2818
2819 if (!attr) {
2820 return;
2821 }
2822
2823 opaq = (struct lyd_node_opaq *)parent;
2824 for (iter = attr; iter; iter = iter->next) {
2825 iter->parent = opaq;
2826 }
2827
2828 /* insert as the last attribute */
2829 if (opaq->attr) {
Radek Krejci1e008d22020-08-17 11:37:37 +02002830 for (last = opaq->attr; last->next; last = last->next) {}
Michal Vaskoa5da3292020-08-12 13:10:50 +02002831 last->next = attr;
2832 } else {
2833 opaq->attr = attr;
2834 }
2835}
2836
Michal Vasko52927e22020-03-16 17:26:14 +01002837LY_ERR
Michal Vaskofeca4fb2020-10-05 08:58:40 +02002838lyd_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 +01002839 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 +02002840 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 +01002841{
Radek Krejci011e4aa2020-09-04 15:22:31 +02002842 LY_ERR ret = LY_SUCCESS;
Radek Krejci1798aae2020-07-14 13:26:06 +02002843 struct lyd_attr *at, *last;
Michal Vasko52927e22020-03-16 17:26:14 +01002844
2845 assert(ctx && (parent || attr) && (!parent || !parent->schema));
Michal Vaskofeca4fb2020-10-05 08:58:40 +02002846 assert(name && name_len && format);
Michal Vasko52927e22020-03-16 17:26:14 +01002847
2848 if (!value_len) {
2849 value = "";
2850 }
2851
2852 at = calloc(1, sizeof *at);
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01002853 LY_CHECK_ERR_RET(!at, LOGMEM(ctx); ly_free_prefix_data(format, val_prefix_data), LY_EMEM);
Radek Krejcid46e46a2020-09-15 14:22:42 +02002854
Michal Vaskoad92b672020-11-12 13:11:31 +01002855 LY_CHECK_GOTO(ret = lydict_insert(ctx, name, name_len, &at->name.name), finish);
Michal Vasko501af032020-11-11 20:27:44 +01002856 if (prefix_len) {
Michal Vaskoad92b672020-11-12 13:11:31 +01002857 LY_CHECK_GOTO(ret = lydict_insert(ctx, prefix, prefix_len, &at->name.prefix), finish);
Michal Vasko501af032020-11-11 20:27:44 +01002858 }
2859 if (module_key_len) {
Michal Vaskoad92b672020-11-12 13:11:31 +01002860 LY_CHECK_GOTO(ret = lydict_insert(ctx, module_key, module_key_len, &at->name.module_ns), finish);
Michal Vasko501af032020-11-11 20:27:44 +01002861 }
2862
Michal Vasko52927e22020-03-16 17:26:14 +01002863 if (dynamic && *dynamic) {
Radek Krejci011e4aa2020-09-04 15:22:31 +02002864 ret = lydict_insert_zc(ctx, (char *)value, &at->value);
2865 LY_CHECK_GOTO(ret, finish);
Michal Vasko52927e22020-03-16 17:26:14 +01002866 *dynamic = 0;
2867 } else {
Radek Krejci011e4aa2020-09-04 15:22:31 +02002868 LY_CHECK_GOTO(ret = lydict_insert(ctx, value, value_len, &at->value), finish);
Michal Vasko52927e22020-03-16 17:26:14 +01002869 }
Michal Vasko501af032020-11-11 20:27:44 +01002870 at->format = format;
2871 at->val_prefix_data = val_prefix_data;
2872 at->hints = hints;
Michal Vasko52927e22020-03-16 17:26:14 +01002873
2874 /* insert as the last attribute */
2875 if (parent) {
Michal Vaskoa5da3292020-08-12 13:10:50 +02002876 lyd_insert_attr(parent, at);
Michal Vasko52927e22020-03-16 17:26:14 +01002877 } else if (*attr) {
Radek Krejci1e008d22020-08-17 11:37:37 +02002878 for (last = *attr; last->next; last = last->next) {}
Michal Vasko52927e22020-03-16 17:26:14 +01002879 last->next = at;
2880 }
2881
Radek Krejci011e4aa2020-09-04 15:22:31 +02002882finish:
2883 if (ret) {
2884 lyd_free_attr_single(ctx, at);
2885 } else if (attr) {
Michal Vasko52927e22020-03-16 17:26:14 +01002886 *attr = at;
2887 }
2888 return LY_SUCCESS;
2889}
2890
Radek Krejci084289f2019-07-09 17:35:30 +02002891API const struct lyd_node_term *
Michal Vasko004d3152020-06-11 19:59:22 +02002892lyd_target(const struct ly_path *path, const struct lyd_node *tree)
Radek Krejci084289f2019-07-09 17:35:30 +02002893{
Michal Vasko004d3152020-06-11 19:59:22 +02002894 struct lyd_node *target;
Radek Krejci084289f2019-07-09 17:35:30 +02002895
Michal Vasko004d3152020-06-11 19:59:22 +02002896 if (ly_path_eval(path, tree, &target)) {
2897 return NULL;
Radek Krejci084289f2019-07-09 17:35:30 +02002898 }
2899
Michal Vasko004d3152020-06-11 19:59:22 +02002900 return (struct lyd_node_term *)target;
Radek Krejci084289f2019-07-09 17:35:30 +02002901}
2902
aPiecek2f63f952021-03-30 12:22:18 +02002903/**
2904 * @brief Check the equality of the two schemas from different contexts.
2905 *
2906 * @param schema1 of first node.
2907 * @param schema2 of second node.
2908 * @return 1 if the schemas are equal otherwise 0.
2909 */
2910static ly_bool
2911lyd_compare_schema_equal(const struct lysc_node *schema1, const struct lysc_node *schema2)
2912{
2913 if (!schema1 && !schema2) {
2914 return 1;
2915 } else if (!schema1 || !schema2) {
2916 return 0;
2917 }
2918
2919 assert(schema1->module->ctx != schema2->module->ctx);
2920
2921 if (schema1->nodetype != schema2->nodetype) {
2922 return 0;
2923 }
2924
2925 if (strcmp(schema1->name, schema2->name)) {
2926 return 0;
2927 }
2928
2929 if (strcmp(schema1->module->name, schema2->module->name)) {
2930 return 0;
2931 }
2932
2933 if (schema1->module->revision || schema2->module->revision) {
2934 if (!schema1->module->revision || !schema2->module->revision) {
2935 return 0;
2936 }
2937 if (strcmp(schema1->module->revision, schema2->module->revision)) {
2938 return 0;
2939 }
2940 }
2941
2942 return 1;
2943}
2944
2945/**
2946 * @brief Check the equality of the schemas for all parent nodes.
2947 *
2948 * Both nodes must be from different contexts.
2949 *
2950 * @param node1 Data of first node.
2951 * @param node2 Data of second node.
2952 * @return 1 if the all related parental schemas are equal otherwise 0.
2953 */
2954static ly_bool
2955lyd_compare_schema_parents_equal(const struct lyd_node *node1, const struct lyd_node *node2)
2956{
2957 const struct lysc_node *parent1, *parent2;
2958
2959 assert(node1 && node2);
2960
2961 for (parent1 = node1->schema->parent, parent2 = node2->schema->parent;
2962 parent1 && parent2;
2963 parent1 = parent1->parent, parent2 = parent2->parent) {
2964 if (!lyd_compare_schema_equal(parent1, parent2)) {
2965 return 0;
2966 }
2967 }
2968
2969 if (parent1 || parent2) {
2970 return 0;
2971 }
2972
2973 return 1;
2974}
2975
2976/**
2977 * @brief Internal implementation of @ref lyd_compare_single.
2978 * @copydoc lyd_compare_single
2979 * @param[in] parental_schemas_checked Flag used for optimization.
2980 * When this function is called for the first time, the flag must be set to 0.
2981 * The @ref lyd_compare_schema_parents_equal should be called only once during
2982 * recursive calls, and this is accomplished by setting to 1 in the lyd_compare_single_ body.
2983 */
2984static LY_ERR
2985lyd_compare_single_(const struct lyd_node *node1, const struct lyd_node *node2,
2986 uint32_t options, ly_bool parental_schemas_checked)
Radek Krejci1f05b6a2019-07-18 16:15:06 +02002987{
2988 const struct lyd_node *iter1, *iter2;
2989 struct lyd_node_term *term1, *term2;
2990 struct lyd_node_any *any1, *any2;
Michal Vasko52927e22020-03-16 17:26:14 +01002991 struct lyd_node_opaq *opaq1, *opaq2;
Radek Krejci1f05b6a2019-07-18 16:15:06 +02002992 size_t len1, len2;
Radek Krejci084289f2019-07-09 17:35:30 +02002993
Radek Krejci1f05b6a2019-07-18 16:15:06 +02002994 if (!node1 || !node2) {
2995 if (node1 == node2) {
2996 return LY_SUCCESS;
2997 } else {
2998 return LY_ENOT;
2999 }
3000 }
3001
aPiecek2f63f952021-03-30 12:22:18 +02003002 if (LYD_CTX(node1) == LYD_CTX(node2)) {
3003 /* same contexts */
3004 if (node1->schema != node2->schema) {
3005 return LY_ENOT;
3006 }
3007 } else {
3008 /* different contexts */
3009 if (!lyd_compare_schema_equal(node1->schema, node2->schema)) {
3010 return LY_ENOT;
3011 }
3012 if (!parental_schemas_checked) {
3013 if (!lyd_compare_schema_parents_equal(node1, node2)) {
3014 return LY_ENOT;
3015 }
3016 parental_schemas_checked = 1;
3017 }
Radek Krejci1f05b6a2019-07-18 16:15:06 +02003018 }
3019
3020 if (node1->hash != node2->hash) {
3021 return LY_ENOT;
3022 }
aPiecek2f63f952021-03-30 12:22:18 +02003023 /* 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 +02003024
Michal Vasko52927e22020-03-16 17:26:14 +01003025 if (!node1->schema) {
3026 opaq1 = (struct lyd_node_opaq *)node1;
3027 opaq2 = (struct lyd_node_opaq *)node2;
aPiecek2f63f952021-03-30 12:22:18 +02003028 if ((strcmp(opaq1->name.name, opaq2->name.name)) || (opaq1->format != opaq2->format) ||
3029 (strcmp(opaq1->name.module_ns, opaq2->name.module_ns))) {
Radek Krejci1f05b6a2019-07-18 16:15:06 +02003030 return LY_ENOT;
3031 }
Michal Vasko52927e22020-03-16 17:26:14 +01003032 switch (opaq1->format) {
Radek Krejci8df109d2021-04-23 12:19:08 +02003033 case LY_VALUE_XML:
Radek Krejci09c77442021-04-26 11:10:34 +02003034 if (lyxml_value_compare(LYD_CTX(node1), opaq1->value, opaq1->val_prefix_data, LYD_CTX(node2), opaq2->value,
3035 opaq2->val_prefix_data)) {
Michal Vasko52927e22020-03-16 17:26:14 +01003036 return LY_ENOT;
3037 }
3038 break;
Radek Krejci8df109d2021-04-23 12:19:08 +02003039 case LY_VALUE_JSON:
Radek Krejci1798aae2020-07-14 13:26:06 +02003040 /* prefixes in JSON are unique, so it is not necessary to canonize the values */
3041 if (strcmp(opaq1->value, opaq2->value)) {
3042 return LY_ENOT;
3043 }
3044 break;
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01003045 default:
Michal Vasko52927e22020-03-16 17:26:14 +01003046 /* not allowed */
Michal Vaskob7be7a82020-08-20 09:09:04 +02003047 LOGINT(LYD_CTX(node1));
Michal Vasko52927e22020-03-16 17:26:14 +01003048 return LY_EINT;
3049 }
3050 if (options & LYD_COMPARE_FULL_RECURSION) {
3051 iter1 = opaq1->child;
3052 iter2 = opaq2->child;
Radek Krejci1f05b6a2019-07-18 16:15:06 +02003053 goto all_children_compare;
Michal Vasko52927e22020-03-16 17:26:14 +01003054 }
3055 return LY_SUCCESS;
3056 } else {
3057 switch (node1->schema->nodetype) {
3058 case LYS_LEAF:
3059 case LYS_LEAFLIST:
3060 if (options & LYD_COMPARE_DEFAULTS) {
3061 if ((node1->flags & LYD_DEFAULT) != (node2->flags & LYD_DEFAULT)) {
3062 return LY_ENOT;
3063 }
3064 }
3065
Michal Vasko1a2bf2e2020-06-16 13:07:48 +02003066 term1 = (struct lyd_node_term *)node1;
3067 term2 = (struct lyd_node_term *)node2;
aPiecek2f63f952021-03-30 12:22:18 +02003068
3069 /* same contexts */
3070 if (LYD_CTX(node1) == LYD_CTX(node2)) {
3071 return term1->value.realtype->plugin->compare(&term1->value, &term2->value);
3072 }
3073
3074 /* different contexts */
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02003075 if (strcmp(lyd_get_value(node1), lyd_get_value(node2))) {
aPiecek2f63f952021-03-30 12:22:18 +02003076 return LY_ENOT;
3077 }
3078 return LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +01003079 case LYS_CONTAINER:
3080 if (options & LYD_COMPARE_DEFAULTS) {
3081 if ((node1->flags & LYD_DEFAULT) != (node2->flags & LYD_DEFAULT)) {
3082 return LY_ENOT;
3083 }
3084 }
3085 if (options & LYD_COMPARE_FULL_RECURSION) {
Michal Vasko9e685082021-01-29 14:49:09 +01003086 iter1 = lyd_child(node1);
3087 iter2 = lyd_child(node2);
Michal Vasko52927e22020-03-16 17:26:14 +01003088 goto all_children_compare;
Radek Krejci1f05b6a2019-07-18 16:15:06 +02003089 }
3090 return LY_SUCCESS;
Michal Vasko1bf09392020-03-27 12:38:10 +01003091 case LYS_RPC:
Michal Vasko52927e22020-03-16 17:26:14 +01003092 case LYS_ACTION:
3093 if (options & LYD_COMPARE_FULL_RECURSION) {
3094 /* TODO action/RPC
3095 goto all_children_compare;
3096 */
Radek Krejci1f05b6a2019-07-18 16:15:06 +02003097 }
3098 return LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +01003099 case LYS_NOTIF:
3100 if (options & LYD_COMPARE_FULL_RECURSION) {
3101 /* TODO Notification
3102 goto all_children_compare;
3103 */
3104 }
3105 return LY_SUCCESS;
3106 case LYS_LIST:
Michal Vasko9e685082021-01-29 14:49:09 +01003107 iter1 = lyd_child(node1);
3108 iter2 = lyd_child(node2);
Michal Vasko52927e22020-03-16 17:26:14 +01003109
3110 if (!(node1->schema->flags & LYS_KEYLESS) && !(options & LYD_COMPARE_FULL_RECURSION)) {
3111 /* lists with keys, their equivalence is based on their keys */
Michal Vasko544e58a2021-01-28 14:33:41 +01003112 for (const struct lysc_node *key = lysc_node_child(node1->schema);
Michal Vaskoc57d9a92020-06-23 13:28:27 +02003113 key && (key->flags & LYS_KEY);
Michal Vasko52927e22020-03-16 17:26:14 +01003114 key = key->next) {
aPiecek2f63f952021-03-30 12:22:18 +02003115 if (lyd_compare_single_(iter1, iter2, options, parental_schemas_checked)) {
Michal Vasko52927e22020-03-16 17:26:14 +01003116 return LY_ENOT;
3117 }
3118 iter1 = iter1->next;
3119 iter2 = iter2->next;
3120 }
3121 } else {
3122 /* lists without keys, their equivalence is based on equivalence of all the children (both direct and indirect) */
3123
Radek Krejci0f969882020-08-21 16:56:47 +02003124all_children_compare:
Michal Vasko52927e22020-03-16 17:26:14 +01003125 if (!iter1 && !iter2) {
3126 /* no children, nothing to compare */
3127 return LY_SUCCESS;
3128 }
3129
Michal Vaskod989ba02020-08-24 10:59:24 +02003130 for ( ; iter1 && iter2; iter1 = iter1->next, iter2 = iter2->next) {
aPiecek2f63f952021-03-30 12:22:18 +02003131 if (lyd_compare_single_(iter1, iter2, options | LYD_COMPARE_FULL_RECURSION, parental_schemas_checked)) {
Michal Vasko52927e22020-03-16 17:26:14 +01003132 return LY_ENOT;
3133 }
3134 }
3135 if (iter1 || iter2) {
3136 return LY_ENOT;
3137 }
3138 }
3139 return LY_SUCCESS;
3140 case LYS_ANYXML:
3141 case LYS_ANYDATA:
Michal Vasko22df3f02020-08-24 13:29:22 +02003142 any1 = (struct lyd_node_any *)node1;
3143 any2 = (struct lyd_node_any *)node2;
Michal Vasko52927e22020-03-16 17:26:14 +01003144
3145 if (any1->value_type != any2->value_type) {
3146 return LY_ENOT;
3147 }
3148 switch (any1->value_type) {
3149 case LYD_ANYDATA_DATATREE:
3150 iter1 = any1->value.tree;
3151 iter2 = any2->value.tree;
3152 goto all_children_compare;
3153 case LYD_ANYDATA_STRING:
3154 case LYD_ANYDATA_XML:
3155 case LYD_ANYDATA_JSON:
3156 len1 = strlen(any1->value.str);
3157 len2 = strlen(any2->value.str);
Michal Vasko69730152020-10-09 16:30:07 +02003158 if ((len1 != len2) || strcmp(any1->value.str, any2->value.str)) {
Michal Vasko52927e22020-03-16 17:26:14 +01003159 return LY_ENOT;
3160 }
3161 return LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +01003162 case LYD_ANYDATA_LYB:
Michal Vasko60ea6352020-06-29 13:39:39 +02003163 len1 = lyd_lyb_data_length(any1->value.mem);
3164 len2 = lyd_lyb_data_length(any2->value.mem);
Michal Vasko69730152020-10-09 16:30:07 +02003165 if ((len1 != len2) || memcmp(any1->value.mem, any2->value.mem, len1)) {
Michal Vasko52927e22020-03-16 17:26:14 +01003166 return LY_ENOT;
3167 }
3168 return LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +01003169 }
Radek Krejci1f05b6a2019-07-18 16:15:06 +02003170 }
3171 }
3172
Michal Vaskob7be7a82020-08-20 09:09:04 +02003173 LOGINT(LYD_CTX(node1));
Radek Krejci1f05b6a2019-07-18 16:15:06 +02003174 return LY_EINT;
3175}
Radek Krejci22ebdba2019-07-25 13:59:43 +02003176
Michal Vasko21725742020-06-29 11:49:25 +02003177API LY_ERR
aPiecek2f63f952021-03-30 12:22:18 +02003178lyd_compare_single(const struct lyd_node *node1, const struct lyd_node *node2, uint32_t options)
3179{
3180 return lyd_compare_single_(node1, node2, options, 0);
3181}
3182
3183API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003184lyd_compare_siblings(const struct lyd_node *node1, const struct lyd_node *node2, uint32_t options)
Michal Vasko8f359bf2020-07-28 10:41:15 +02003185{
Michal Vaskod989ba02020-08-24 10:59:24 +02003186 for ( ; node1 && node2; node1 = node1->next, node2 = node2->next) {
Michal Vasko8f359bf2020-07-28 10:41:15 +02003187 LY_CHECK_RET(lyd_compare_single(node1, node2, options));
3188 }
3189
Michal Vasko11a81432020-07-28 16:31:29 +02003190 if (node1 == node2) {
3191 return LY_SUCCESS;
Michal Vasko8f359bf2020-07-28 10:41:15 +02003192 }
Michal Vasko11a81432020-07-28 16:31:29 +02003193 return LY_ENOT;
Michal Vasko8f359bf2020-07-28 10:41:15 +02003194}
3195
3196API LY_ERR
Michal Vasko21725742020-06-29 11:49:25 +02003197lyd_compare_meta(const struct lyd_meta *meta1, const struct lyd_meta *meta2)
3198{
3199 if (!meta1 || !meta2) {
3200 if (meta1 == meta2) {
3201 return LY_SUCCESS;
3202 } else {
3203 return LY_ENOT;
3204 }
3205 }
3206
Michal Vaskoa8083062020-11-06 17:22:10 +01003207 if ((meta1->annotation->module->ctx != meta2->annotation->module->ctx) || (meta1->annotation != meta2->annotation)) {
Michal Vasko21725742020-06-29 11:49:25 +02003208 return LY_ENOT;
3209 }
3210
Michal Vasko21725742020-06-29 11:49:25 +02003211 return meta1->value.realtype->plugin->compare(&meta1->value, &meta2->value);
3212}
3213
Radek Krejci22ebdba2019-07-25 13:59:43 +02003214/**
Michal Vasko52927e22020-03-16 17:26:14 +01003215 * @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 +02003216 *
3217 * 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 +02003218 *
3219 * @param[in] node Original node to duplicate
3220 * @param[in] parent Parent to insert into, NULL for top-level sibling.
3221 * @param[in,out] first First sibling, NULL if no top-level sibling exist yet. Can be also NULL if @p parent is set.
3222 * @param[in] options Bitmask of options flags, see @ref dupoptions.
3223 * @param[out] dup_p Pointer where the created duplicated node is placed (besides connecting it int @p parent / @p first sibling).
3224 * @return LY_ERR value
Radek Krejci22ebdba2019-07-25 13:59:43 +02003225 */
Michal Vasko52927e22020-03-16 17:26:14 +01003226static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003227lyd_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 +02003228 struct lyd_node **dup_p)
Radek Krejci22ebdba2019-07-25 13:59:43 +02003229{
Michal Vasko52927e22020-03-16 17:26:14 +01003230 LY_ERR ret;
Radek Krejci22ebdba2019-07-25 13:59:43 +02003231 struct lyd_node *dup = NULL;
Michal Vasko61551fa2020-07-09 15:45:45 +02003232 struct lyd_meta *meta;
3233 struct lyd_node_any *any;
Radek Krejci22ebdba2019-07-25 13:59:43 +02003234
Michal Vasko52927e22020-03-16 17:26:14 +01003235 LY_CHECK_ARG_RET(NULL, node, LY_EINVAL);
Radek Krejci22ebdba2019-07-25 13:59:43 +02003236
Michal Vasko52927e22020-03-16 17:26:14 +01003237 if (!node->schema) {
3238 dup = calloc(1, sizeof(struct lyd_node_opaq));
3239 } else {
3240 switch (node->schema->nodetype) {
Michal Vasko1bf09392020-03-27 12:38:10 +01003241 case LYS_RPC:
Michal Vasko52927e22020-03-16 17:26:14 +01003242 case LYS_ACTION:
3243 case LYS_NOTIF:
3244 case LYS_CONTAINER:
3245 case LYS_LIST:
3246 dup = calloc(1, sizeof(struct lyd_node_inner));
3247 break;
3248 case LYS_LEAF:
3249 case LYS_LEAFLIST:
3250 dup = calloc(1, sizeof(struct lyd_node_term));
3251 break;
3252 case LYS_ANYDATA:
3253 case LYS_ANYXML:
3254 dup = calloc(1, sizeof(struct lyd_node_any));
3255 break;
3256 default:
Michal Vaskob7be7a82020-08-20 09:09:04 +02003257 LOGINT(LYD_CTX(node));
Michal Vasko52927e22020-03-16 17:26:14 +01003258 ret = LY_EINT;
3259 goto error;
3260 }
Radek Krejci22ebdba2019-07-25 13:59:43 +02003261 }
Michal Vaskob7be7a82020-08-20 09:09:04 +02003262 LY_CHECK_ERR_GOTO(!dup, LOGMEM(LYD_CTX(node)); ret = LY_EMEM, error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02003263
Michal Vaskof6df0a02020-06-16 13:08:34 +02003264 if (options & LYD_DUP_WITH_FLAGS) {
3265 dup->flags = node->flags;
3266 } else {
3267 dup->flags = (node->flags & LYD_DEFAULT) | LYD_NEW;
3268 }
Radek Krejci22ebdba2019-07-25 13:59:43 +02003269 dup->schema = node->schema;
Michal Vasko52927e22020-03-16 17:26:14 +01003270 dup->prev = dup;
Radek Krejci22ebdba2019-07-25 13:59:43 +02003271
Michal Vasko25a32822020-07-09 15:48:22 +02003272 /* duplicate metadata */
3273 if (!(options & LYD_DUP_NO_META)) {
3274 LY_LIST_FOR(node->meta, meta) {
Michal Vasko3a41dff2020-07-15 14:30:28 +02003275 LY_CHECK_GOTO(ret = lyd_dup_meta_single(meta, dup, NULL), error);
Michal Vasko25a32822020-07-09 15:48:22 +02003276 }
3277 }
Radek Krejci22ebdba2019-07-25 13:59:43 +02003278
3279 /* nodetype-specific work */
Michal Vasko52927e22020-03-16 17:26:14 +01003280 if (!dup->schema) {
3281 struct lyd_node_opaq *opaq = (struct lyd_node_opaq *)dup;
3282 struct lyd_node_opaq *orig = (struct lyd_node_opaq *)node;
3283 struct lyd_node *child;
Radek Krejci22ebdba2019-07-25 13:59:43 +02003284
3285 if (options & LYD_DUP_RECURSIVE) {
3286 /* duplicate all the children */
3287 LY_LIST_FOR(orig->child, child) {
Michal Vasko3a41dff2020-07-15 14:30:28 +02003288 LY_CHECK_GOTO(ret = lyd_dup_r(child, dup, NULL, options, NULL), error);
Michal Vasko52927e22020-03-16 17:26:14 +01003289 }
3290 }
Michal Vaskoad92b672020-11-12 13:11:31 +01003291 LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), orig->name.name, 0, &opaq->name.name), error);
Michal Vasko52927e22020-03-16 17:26:14 +01003292 opaq->format = orig->format;
Michal Vaskoad92b672020-11-12 13:11:31 +01003293 if (orig->name.prefix) {
3294 LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), orig->name.prefix, 0, &opaq->name.prefix), error);
Michal Vasko52927e22020-03-16 17:26:14 +01003295 }
Michal Vaskoad92b672020-11-12 13:11:31 +01003296 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 +01003297 if (orig->val_prefix_data) {
3298 ret = ly_dup_prefix_data(LYD_CTX(node), opaq->format, orig->val_prefix_data, &opaq->val_prefix_data);
3299 LY_CHECK_GOTO(ret, error);
Michal Vasko52927e22020-03-16 17:26:14 +01003300 }
Radek Krejci011e4aa2020-09-04 15:22:31 +02003301 LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), orig->value, 0, &opaq->value), error);
Michal Vasko52927e22020-03-16 17:26:14 +01003302 opaq->ctx = orig->ctx;
3303 } else if (dup->schema->nodetype & LYD_NODE_TERM) {
3304 struct lyd_node_term *term = (struct lyd_node_term *)dup;
3305 struct lyd_node_term *orig = (struct lyd_node_term *)node;
3306
3307 term->hash = orig->hash;
Michal Vaskob7be7a82020-08-20 09:09:04 +02003308 LY_CHECK_ERR_GOTO(orig->value.realtype->plugin->duplicate(LYD_CTX(node), &orig->value, &term->value),
Michal Vasko69730152020-10-09 16:30:07 +02003309 LOGERR(LYD_CTX(node), LY_EINT, "Value duplication failed."); ret = LY_EINT, error);
Michal Vasko52927e22020-03-16 17:26:14 +01003310 } else if (dup->schema->nodetype & LYD_NODE_INNER) {
3311 struct lyd_node_inner *orig = (struct lyd_node_inner *)node;
3312 struct lyd_node *child;
3313
3314 if (options & LYD_DUP_RECURSIVE) {
3315 /* duplicate all the children */
3316 LY_LIST_FOR(orig->child, child) {
Michal Vasko3a41dff2020-07-15 14:30:28 +02003317 LY_CHECK_GOTO(ret = lyd_dup_r(child, dup, NULL, options, NULL), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02003318 }
Michal Vasko69730152020-10-09 16:30:07 +02003319 } else if ((dup->schema->nodetype == LYS_LIST) && !(dup->schema->flags & LYS_KEYLESS)) {
Radek Krejci22ebdba2019-07-25 13:59:43 +02003320 /* always duplicate keys of a list */
Radek Krejci22ebdba2019-07-25 13:59:43 +02003321 child = orig->child;
Michal Vasko544e58a2021-01-28 14:33:41 +01003322 for (const struct lysc_node *key = lysc_node_child(dup->schema);
Michal Vaskoc57d9a92020-06-23 13:28:27 +02003323 key && (key->flags & LYS_KEY);
Radek Krejci0fe9b512019-07-26 17:51:05 +02003324 key = key->next) {
Radek Krejci22ebdba2019-07-25 13:59:43 +02003325 if (!child) {
3326 /* possibly not keys are present in filtered tree */
3327 break;
Radek Krejci0fe9b512019-07-26 17:51:05 +02003328 } else if (child->schema != key) {
3329 /* possibly not all keys are present in filtered tree,
3330 * but there can be also some non-key nodes */
3331 continue;
Radek Krejci22ebdba2019-07-25 13:59:43 +02003332 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02003333 LY_CHECK_GOTO(ret = lyd_dup_r(child, dup, NULL, options, NULL), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02003334 child = child->next;
3335 }
3336 }
3337 lyd_hash(dup);
3338 } else if (dup->schema->nodetype & LYD_NODE_ANY) {
Michal Vasko61551fa2020-07-09 15:45:45 +02003339 dup->hash = node->hash;
3340 any = (struct lyd_node_any *)node;
3341 LY_CHECK_GOTO(ret = lyd_any_copy_value(dup, &any->value, any->value_type), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02003342 }
3343
Michal Vasko52927e22020-03-16 17:26:14 +01003344 /* insert */
3345 lyd_insert_node(parent, first, dup);
Michal Vasko52927e22020-03-16 17:26:14 +01003346
3347 if (dup_p) {
3348 *dup_p = dup;
3349 }
3350 return LY_SUCCESS;
Radek Krejci22ebdba2019-07-25 13:59:43 +02003351
3352error:
Michal Vasko52927e22020-03-16 17:26:14 +01003353 lyd_free_tree(dup);
3354 return ret;
Radek Krejci22ebdba2019-07-25 13:59:43 +02003355}
3356
Michal Vasko3a41dff2020-07-15 14:30:28 +02003357static LY_ERR
3358lyd_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 +02003359 struct lyd_node_inner **local_parent)
Radek Krejci22ebdba2019-07-25 13:59:43 +02003360{
Michal Vasko3a41dff2020-07-15 14:30:28 +02003361 const struct lyd_node_inner *orig_parent, *iter;
Radek Krejci857189e2020-09-01 13:26:36 +02003362 ly_bool repeat = 1;
Michal Vasko3a41dff2020-07-15 14:30:28 +02003363
3364 *dup_parent = NULL;
3365 *local_parent = NULL;
3366
3367 for (orig_parent = node->parent; repeat && orig_parent; orig_parent = orig_parent->parent) {
3368 if (parent && (parent->schema == orig_parent->schema)) {
3369 /* stop creating parents, connect what we have into the provided parent */
3370 iter = parent;
3371 repeat = 0;
3372 } else {
3373 iter = NULL;
3374 LY_CHECK_RET(lyd_dup_r((struct lyd_node *)orig_parent, NULL, (struct lyd_node **)&iter, 0,
Radek Krejci0f969882020-08-21 16:56:47 +02003375 (struct lyd_node **)&iter));
Michal Vasko3a41dff2020-07-15 14:30:28 +02003376 }
3377 if (!*local_parent) {
3378 *local_parent = (struct lyd_node_inner *)iter;
3379 }
3380 if (iter->child) {
3381 /* 1) list - add after keys
3382 * 2) provided parent with some children */
3383 iter->child->prev->next = *dup_parent;
3384 if (*dup_parent) {
3385 (*dup_parent)->prev = iter->child->prev;
3386 iter->child->prev = *dup_parent;
3387 }
3388 } else {
3389 ((struct lyd_node_inner *)iter)->child = *dup_parent;
3390 }
3391 if (*dup_parent) {
3392 (*dup_parent)->parent = (struct lyd_node_inner *)iter;
3393 }
3394 *dup_parent = (struct lyd_node *)iter;
3395 }
3396
3397 if (repeat && parent) {
3398 /* given parent and created parents chain actually do not interconnect */
Michal Vaskob7be7a82020-08-20 09:09:04 +02003399 LOGERR(LYD_CTX(node), LY_EINVAL,
Michal Vasko69730152020-10-09 16:30:07 +02003400 "Invalid argument parent (%s()) - does not interconnect with the created node's parents chain.", __func__);
Michal Vasko3a41dff2020-07-15 14:30:28 +02003401 return LY_EINVAL;
3402 }
3403
3404 return LY_SUCCESS;
3405}
3406
3407static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02003408lyd_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 +02003409{
3410 LY_ERR rc;
Radek Krejci22ebdba2019-07-25 13:59:43 +02003411 const struct lyd_node *orig; /* original node to be duplicated */
3412 struct lyd_node *first = NULL; /* the first duplicated node, this is returned */
Radek Krejci22ebdba2019-07-25 13:59:43 +02003413 struct lyd_node *top = NULL; /* the most higher created node */
3414 struct lyd_node_inner *local_parent = NULL; /* the direct parent node for the duplicated node(s) */
Radek Krejci22ebdba2019-07-25 13:59:43 +02003415
Michal Vasko3a41dff2020-07-15 14:30:28 +02003416 LY_CHECK_ARG_RET(NULL, node, LY_EINVAL);
Radek Krejci22ebdba2019-07-25 13:59:43 +02003417
3418 if (options & LYD_DUP_WITH_PARENTS) {
Michal Vasko3a41dff2020-07-15 14:30:28 +02003419 LY_CHECK_GOTO(rc = lyd_dup_get_local_parent(node, parent, &top, &local_parent), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02003420 } else {
3421 local_parent = parent;
3422 }
3423
Radek Krejci22ebdba2019-07-25 13:59:43 +02003424 LY_LIST_FOR(node, orig) {
Michal Vasko35f4d772021-01-12 12:08:57 +01003425 if (lysc_is_key(orig->schema)) {
3426 if (local_parent) {
3427 /* the key must already exist in the parent */
3428 rc = lyd_find_sibling_schema(local_parent->child, orig->schema, first ? NULL : &first);
3429 LY_CHECK_ERR_GOTO(rc, LOGINT(LYD_CTX(node)), error);
3430 } else {
3431 assert(!(options & LYD_DUP_WITH_PARENTS));
3432 /* duplicating a single key, okay, I suppose... */
3433 rc = lyd_dup_r(orig, NULL, &first, options, first ? NULL : &first);
3434 LY_CHECK_GOTO(rc, error);
3435 }
3436 } else {
3437 /* if there is no local parent, it will be inserted into first */
Michal Vasko9e685082021-01-29 14:49:09 +01003438 rc = lyd_dup_r(orig, &local_parent->node, &first, options, first ? NULL : &first);
Michal Vasko35f4d772021-01-12 12:08:57 +01003439 LY_CHECK_GOTO(rc, error);
3440 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02003441 if (nosiblings) {
Radek Krejci22ebdba2019-07-25 13:59:43 +02003442 break;
3443 }
3444 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02003445
Michal Vasko3a41dff2020-07-15 14:30:28 +02003446 if (dup) {
3447 *dup = first;
3448 }
3449 return LY_SUCCESS;
Radek Krejci22ebdba2019-07-25 13:59:43 +02003450
3451error:
3452 if (top) {
3453 lyd_free_tree(top);
3454 } else {
Michal Vaskof03ed032020-03-04 13:31:44 +01003455 lyd_free_siblings(first);
Radek Krejci22ebdba2019-07-25 13:59:43 +02003456 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02003457 return rc;
Radek Krejci22ebdba2019-07-25 13:59:43 +02003458}
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003459
Michal Vasko3a41dff2020-07-15 14:30:28 +02003460API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003461lyd_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 +02003462{
3463 return lyd_dup(node, parent, options, 1, dup);
3464}
3465
3466API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003467lyd_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 +02003468{
3469 return lyd_dup(node, parent, options, 0, dup);
3470}
3471
3472API LY_ERR
3473lyd_dup_meta_single(const struct lyd_meta *meta, struct lyd_node *node, struct lyd_meta **dup)
Michal Vasko25a32822020-07-09 15:48:22 +02003474{
Radek Krejci011e4aa2020-09-04 15:22:31 +02003475 LY_ERR ret = LY_SUCCESS;
Michal Vasko25a32822020-07-09 15:48:22 +02003476 struct lyd_meta *mt, *last;
3477
Michal Vasko3a41dff2020-07-15 14:30:28 +02003478 LY_CHECK_ARG_RET(NULL, meta, node, LY_EINVAL);
Michal Vasko25a32822020-07-09 15:48:22 +02003479
3480 /* create a copy */
3481 mt = calloc(1, sizeof *mt);
Michal Vaskob7be7a82020-08-20 09:09:04 +02003482 LY_CHECK_ERR_RET(!mt, LOGMEM(LYD_CTX(node)), LY_EMEM);
Michal Vasko25a32822020-07-09 15:48:22 +02003483 mt->annotation = meta->annotation;
Michal Vaskob7be7a82020-08-20 09:09:04 +02003484 ret = meta->value.realtype->plugin->duplicate(LYD_CTX(node), &meta->value, &mt->value);
Radek Krejci011e4aa2020-09-04 15:22:31 +02003485 LY_CHECK_ERR_GOTO(ret, LOGERR(LYD_CTX(node), LY_EINT, "Value duplication failed."), finish);
3486 LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), meta->name, 0, &mt->name), finish);
Michal Vasko25a32822020-07-09 15:48:22 +02003487
3488 /* insert as the last attribute */
Radek Krejci011e4aa2020-09-04 15:22:31 +02003489 mt->parent = node;
Michal Vasko25a32822020-07-09 15:48:22 +02003490 if (node->meta) {
Radek Krejci1e008d22020-08-17 11:37:37 +02003491 for (last = node->meta; last->next; last = last->next) {}
Michal Vasko25a32822020-07-09 15:48:22 +02003492 last->next = mt;
3493 } else {
3494 node->meta = mt;
3495 }
3496
Radek Krejci011e4aa2020-09-04 15:22:31 +02003497finish:
3498 if (ret) {
3499 lyd_free_meta_single(mt);
3500 } else if (dup) {
Michal Vasko3a41dff2020-07-15 14:30:28 +02003501 *dup = mt;
3502 }
3503 return LY_SUCCESS;
Michal Vasko25a32822020-07-09 15:48:22 +02003504}
3505
Michal Vasko4490d312020-06-16 13:08:55 +02003506/**
3507 * @brief Merge a source sibling into target siblings.
3508 *
3509 * @param[in,out] first_trg First target sibling, is updated if top-level.
3510 * @param[in] parent_trg Target parent.
3511 * @param[in,out] sibling_src Source sibling to merge, set to NULL if spent.
3512 * @param[in] options Merge options.
3513 * @return LY_ERR value.
3514 */
3515static LY_ERR
3516lyd_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 +02003517 uint16_t options)
Michal Vasko4490d312020-06-16 13:08:55 +02003518{
3519 LY_ERR ret;
3520 const struct lyd_node *child_src, *tmp, *sibling_src;
Michal Vasko56daf732020-08-10 10:57:18 +02003521 struct lyd_node *match_trg, *dup_src, *elem;
Michal Vasko4490d312020-06-16 13:08:55 +02003522 struct lysc_type *type;
Michal Vasko4490d312020-06-16 13:08:55 +02003523
3524 sibling_src = *sibling_src_p;
3525 if (sibling_src->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) {
3526 /* try to find the exact instance */
3527 ret = lyd_find_sibling_first(*first_trg, sibling_src, &match_trg);
3528 } else {
3529 /* try to simply find the node, there cannot be more instances */
3530 ret = lyd_find_sibling_val(*first_trg, sibling_src->schema, NULL, 0, &match_trg);
3531 }
3532
3533 if (!ret) {
3534 /* node found, make sure even value matches for all node types */
Michal Vasko8f359bf2020-07-28 10:41:15 +02003535 if ((match_trg->schema->nodetype == LYS_LEAF) && lyd_compare_single(sibling_src, match_trg, LYD_COMPARE_DEFAULTS)) {
Michal Vasko4490d312020-06-16 13:08:55 +02003536 /* since they are different, they cannot both be default */
3537 assert(!(sibling_src->flags & LYD_DEFAULT) || !(match_trg->flags & LYD_DEFAULT));
3538
Michal Vasko3a41dff2020-07-15 14:30:28 +02003539 /* update value (or only LYD_DEFAULT flag) only if flag set or the source node is not default */
3540 if ((options & LYD_MERGE_DEFAULTS) || !(sibling_src->flags & LYD_DEFAULT)) {
Michal Vasko4490d312020-06-16 13:08:55 +02003541 type = ((struct lysc_node_leaf *)match_trg->schema)->type;
Michal Vaskob7be7a82020-08-20 09:09:04 +02003542 type->plugin->free(LYD_CTX(match_trg), &((struct lyd_node_term *)match_trg)->value);
3543 LY_CHECK_RET(type->plugin->duplicate(LYD_CTX(match_trg), &((struct lyd_node_term *)sibling_src)->value,
Michal Vasko69730152020-10-09 16:30:07 +02003544 &((struct lyd_node_term *)match_trg)->value));
Michal Vasko4490d312020-06-16 13:08:55 +02003545
3546 /* copy flags and add LYD_NEW */
3547 match_trg->flags = sibling_src->flags | LYD_NEW;
3548 }
Michal Vasko8f359bf2020-07-28 10:41:15 +02003549 } else if ((match_trg->schema->nodetype & LYS_ANYDATA) && lyd_compare_single(sibling_src, match_trg, 0)) {
Michal Vasko4c583e82020-07-17 12:16:14 +02003550 /* update value */
3551 LY_CHECK_RET(lyd_any_copy_value(match_trg, &((struct lyd_node_any *)sibling_src)->value,
Radek Krejci0f969882020-08-21 16:56:47 +02003552 ((struct lyd_node_any *)sibling_src)->value_type));
Michal Vasko4490d312020-06-16 13:08:55 +02003553
3554 /* copy flags and add LYD_NEW */
3555 match_trg->flags = sibling_src->flags | LYD_NEW;
Michal Vasko4490d312020-06-16 13:08:55 +02003556 } else {
3557 /* check descendants, recursively */
Radek Krejcia1c1e542020-09-29 16:06:52 +02003558 LY_LIST_FOR_SAFE(lyd_child_no_keys(sibling_src), tmp, child_src) {
Michal Vaskoe0665742021-02-11 11:08:44 +01003559 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 +02003560 }
3561 }
3562 } else {
3563 /* node not found, merge it */
3564 if (options & LYD_MERGE_DESTRUCT) {
3565 dup_src = (struct lyd_node *)sibling_src;
3566 lyd_unlink_tree(dup_src);
3567 /* spend it */
3568 *sibling_src_p = NULL;
3569 } else {
Michal Vasko3a41dff2020-07-15 14:30:28 +02003570 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 +02003571 }
3572
3573 /* set LYD_NEW for all the new nodes, required for validation */
Michal Vasko56daf732020-08-10 10:57:18 +02003574 LYD_TREE_DFS_BEGIN(dup_src, elem) {
Michal Vasko4490d312020-06-16 13:08:55 +02003575 elem->flags |= LYD_NEW;
Michal Vasko56daf732020-08-10 10:57:18 +02003576 LYD_TREE_DFS_END(dup_src, elem);
Michal Vasko4490d312020-06-16 13:08:55 +02003577 }
3578
3579 lyd_insert_node(parent_trg, first_trg, dup_src);
3580 }
3581
3582 return LY_SUCCESS;
3583}
3584
Michal Vasko3a41dff2020-07-15 14:30:28 +02003585static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02003586lyd_merge(struct lyd_node **target, const struct lyd_node *source, uint16_t options, ly_bool nosiblings)
Michal Vasko4490d312020-06-16 13:08:55 +02003587{
3588 const struct lyd_node *sibling_src, *tmp;
Radek Krejci857189e2020-09-01 13:26:36 +02003589 ly_bool first;
Michal Vasko4490d312020-06-16 13:08:55 +02003590
3591 LY_CHECK_ARG_RET(NULL, target, LY_EINVAL);
3592
3593 if (!source) {
3594 /* nothing to merge */
3595 return LY_SUCCESS;
3596 }
3597
Michal Vasko831422b2020-11-24 11:20:51 +01003598 if ((*target && lysc_data_parent((*target)->schema)) || lysc_data_parent(source->schema)) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02003599 LOGERR(LYD_CTX(source), LY_EINVAL, "Invalid arguments - can merge only 2 top-level subtrees (%s()).", __func__);
Michal Vasko4490d312020-06-16 13:08:55 +02003600 return LY_EINVAL;
3601 }
3602
3603 LY_LIST_FOR_SAFE(source, tmp, sibling_src) {
Radek Krejci857189e2020-09-01 13:26:36 +02003604 first = (sibling_src == source) ? 1 : 0;
Michal Vasko4490d312020-06-16 13:08:55 +02003605 LY_CHECK_RET(lyd_merge_sibling_r(target, NULL, &sibling_src, options));
3606 if (first && !sibling_src) {
3607 /* source was spent (unlinked), move to the next node */
3608 source = tmp;
3609 }
3610
Michal Vasko3a41dff2020-07-15 14:30:28 +02003611 if (nosiblings) {
Michal Vasko4490d312020-06-16 13:08:55 +02003612 break;
3613 }
3614 }
3615
3616 if (options & LYD_MERGE_DESTRUCT) {
3617 /* free any leftover source data that were not merged */
3618 lyd_free_siblings((struct lyd_node *)source);
3619 }
3620
3621 return LY_SUCCESS;
3622}
3623
Michal Vasko3a41dff2020-07-15 14:30:28 +02003624API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003625lyd_merge_tree(struct lyd_node **target, const struct lyd_node *source, uint16_t options)
Michal Vasko3a41dff2020-07-15 14:30:28 +02003626{
3627 return lyd_merge(target, source, options, 1);
3628}
3629
3630API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003631lyd_merge_siblings(struct lyd_node **target, const struct lyd_node *source, uint16_t options)
Michal Vasko3a41dff2020-07-15 14:30:28 +02003632{
3633 return lyd_merge(target, source, options, 0);
3634}
3635
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003636static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02003637lyd_path_str_enlarge(char **buffer, size_t *buflen, size_t reqlen, ly_bool is_static)
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003638{
Michal Vasko14654712020-02-06 08:35:21 +01003639 /* ending \0 */
3640 ++reqlen;
3641
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003642 if (reqlen > *buflen) {
3643 if (is_static) {
3644 return LY_EINCOMPLETE;
3645 }
3646
3647 *buffer = ly_realloc(*buffer, reqlen * sizeof **buffer);
3648 if (!*buffer) {
3649 return LY_EMEM;
3650 }
3651
3652 *buflen = reqlen;
3653 }
3654
3655 return LY_SUCCESS;
3656}
3657
Michal Vaskod59035b2020-07-08 12:00:06 +02003658LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02003659lyd_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 +02003660{
3661 const struct lyd_node *key;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003662 size_t len;
3663 const char *val;
3664 char quot;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003665
Radek Krejcia1c1e542020-09-29 16:06:52 +02003666 for (key = lyd_child(node); key && (key->schema->flags & LYS_KEY); key = key->next) {
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02003667 val = lyd_get_value(key);
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003668 len = 1 + strlen(key->schema->name) + 2 + strlen(val) + 2;
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003669 LY_CHECK_RET(lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static));
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003670
3671 quot = '\'';
3672 if (strchr(val, '\'')) {
3673 quot = '"';
3674 }
3675 *bufused += sprintf(*buffer + *bufused, "[%s=%c%s%c]", key->schema->name, quot, val, quot);
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003676 }
3677
3678 return LY_SUCCESS;
3679}
3680
3681/**
3682 * @brief Append leaf-list value predicate to path.
3683 *
3684 * @param[in] node Node to print.
3685 * @param[in,out] buffer Buffer to print to.
3686 * @param[in,out] buflen Current buffer length.
3687 * @param[in,out] bufused Current number of characters used in @p buffer.
3688 * @param[in] is_static Whether buffer is static or can be reallocated.
3689 * @return LY_ERR
3690 */
3691static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02003692lyd_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 +02003693{
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003694 size_t len;
3695 const char *val;
3696 char quot;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003697
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02003698 val = lyd_get_value(node);
Radek Krejcif13b87b2020-12-01 22:02:17 +01003699 len = 4 + strlen(val) + 2; /* "[.='" + val + "']" */
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003700 LY_CHECK_RET(lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static));
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003701
3702 quot = '\'';
3703 if (strchr(val, '\'')) {
3704 quot = '"';
3705 }
3706 *bufused += sprintf(*buffer + *bufused, "[.=%c%s%c]", quot, val, quot);
3707
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003708 return LY_SUCCESS;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003709}
3710
3711/**
3712 * @brief Append node position (relative to its other instances) predicate to path.
3713 *
3714 * @param[in] node Node to print.
3715 * @param[in,out] buffer Buffer to print to.
3716 * @param[in,out] buflen Current buffer length.
3717 * @param[in,out] bufused Current number of characters used in @p buffer.
3718 * @param[in] is_static Whether buffer is static or can be reallocated.
3719 * @return LY_ERR
3720 */
3721static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02003722lyd_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 +02003723{
3724 const struct lyd_node *first, *iter;
3725 size_t len;
Radek Krejci1deb5be2020-08-26 16:43:36 +02003726 uint64_t pos;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003727 char *val = NULL;
3728 LY_ERR rc;
3729
3730 if (node->parent) {
3731 first = node->parent->child;
3732 } else {
Michal Vaskoe070bfe2020-08-31 12:27:25 +02003733 for (first = node; first->prev->next; first = first->prev) {}
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003734 }
3735 pos = 1;
3736 for (iter = first; iter != node; iter = iter->next) {
3737 if (iter->schema == node->schema) {
3738 ++pos;
3739 }
3740 }
Radek Krejci1deb5be2020-08-26 16:43:36 +02003741 if (asprintf(&val, "%" PRIu64, pos) == -1) {
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003742 return LY_EMEM;
3743 }
3744
3745 len = 1 + strlen(val) + 1;
3746 rc = lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static);
3747 if (rc != LY_SUCCESS) {
3748 goto cleanup;
3749 }
3750
3751 *bufused += sprintf(*buffer + *bufused, "[%s]", val);
3752
3753cleanup:
3754 free(val);
3755 return rc;
3756}
3757
3758API char *
3759lyd_path(const struct lyd_node *node, LYD_PATH_TYPE pathtype, char *buffer, size_t buflen)
3760{
Radek Krejci857189e2020-09-01 13:26:36 +02003761 ly_bool is_static = 0;
Radek Krejci1deb5be2020-08-26 16:43:36 +02003762 uint32_t i, depth;
Michal Vasko14654712020-02-06 08:35:21 +01003763 size_t bufused = 0, len;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003764 const struct lyd_node *iter;
3765 const struct lys_module *mod;
Michal Vasko790b2bc2020-08-03 13:35:06 +02003766 LY_ERR rc = LY_SUCCESS;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003767
3768 LY_CHECK_ARG_RET(NULL, node, NULL);
3769 if (buffer) {
3770 LY_CHECK_ARG_RET(node->schema->module->ctx, buflen > 1, NULL);
3771 is_static = 1;
Michal Vasko14654712020-02-06 08:35:21 +01003772 } else {
3773 buflen = 0;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003774 }
3775
3776 switch (pathtype) {
Radek Krejci635d2b82021-01-04 11:26:51 +01003777 case LYD_PATH_STD:
3778 case LYD_PATH_STD_NO_LAST_PRED:
Michal Vasko14654712020-02-06 08:35:21 +01003779 depth = 1;
Michal Vasko9e685082021-01-29 14:49:09 +01003780 for (iter = node; iter->parent; iter = lyd_parent(iter)) {
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003781 ++depth;
3782 }
3783
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003784 goto iter_print;
Michal Vasko14654712020-02-06 08:35:21 +01003785 while (depth) {
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003786 /* find the right node */
Michal Vasko9e685082021-01-29 14:49:09 +01003787 for (iter = node, i = 1; i < depth; iter = lyd_parent(iter), ++i) {}
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003788iter_print:
3789 /* print prefix and name */
3790 mod = NULL;
Michal Vasko69730152020-10-09 16:30:07 +02003791 if (iter->schema && (!iter->parent || (iter->schema->module != iter->parent->schema->module))) {
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003792 mod = iter->schema->module;
3793 }
3794
3795 /* realloc string */
Michal Vaskoad92b672020-11-12 13:11:31 +01003796 len = 1 + (mod ? strlen(mod->name) + 1 : 0) + (iter->schema ? strlen(iter->schema->name) :
3797 strlen(((struct lyd_node_opaq *)iter)->name.name));
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003798 rc = lyd_path_str_enlarge(&buffer, &buflen, bufused + len, is_static);
3799 if (rc != LY_SUCCESS) {
3800 break;
3801 }
3802
3803 /* print next node */
Radek Krejci1798aae2020-07-14 13:26:06 +02003804 bufused += sprintf(buffer + bufused, "/%s%s%s", mod ? mod->name : "", mod ? ":" : "",
Michal Vaskoad92b672020-11-12 13:11:31 +01003805 iter->schema ? iter->schema->name : ((struct lyd_node_opaq *)iter)->name.name);
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003806
Michal Vasko790b2bc2020-08-03 13:35:06 +02003807 /* do not always print the last (first) predicate */
Radek Krejci635d2b82021-01-04 11:26:51 +01003808 if (iter->schema && ((depth > 1) || (pathtype == LYD_PATH_STD))) {
Michal Vasko790b2bc2020-08-03 13:35:06 +02003809 switch (iter->schema->nodetype) {
3810 case LYS_LIST:
3811 if (iter->schema->flags & LYS_KEYLESS) {
3812 /* print its position */
3813 rc = lyd_path_position_predicate(iter, &buffer, &buflen, &bufused, is_static);
3814 } else {
3815 /* print all list keys in predicates */
3816 rc = lyd_path_list_predicate(iter, &buffer, &buflen, &bufused, is_static);
3817 }
3818 break;
3819 case LYS_LEAFLIST:
3820 if (iter->schema->flags & LYS_CONFIG_W) {
3821 /* print leaf-list value */
3822 rc = lyd_path_leaflist_predicate(iter, &buffer, &buflen, &bufused, is_static);
3823 } else {
3824 /* print its position */
3825 rc = lyd_path_position_predicate(iter, &buffer, &buflen, &bufused, is_static);
3826 }
3827 break;
3828 default:
3829 /* nothing to print more */
3830 break;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003831 }
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003832 }
3833 if (rc != LY_SUCCESS) {
3834 break;
3835 }
3836
Michal Vasko14654712020-02-06 08:35:21 +01003837 --depth;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003838 }
3839 break;
3840 }
3841
3842 return buffer;
3843}
Michal Vaskoe444f752020-02-10 12:20:06 +01003844
Michal Vasko25a32822020-07-09 15:48:22 +02003845API struct lyd_meta *
3846lyd_find_meta(const struct lyd_meta *first, const struct lys_module *module, const char *name)
3847{
3848 struct lyd_meta *ret = NULL;
3849 const struct ly_ctx *ctx;
3850 const char *prefix, *tmp;
3851 char *str;
3852 size_t pref_len, name_len;
3853
3854 LY_CHECK_ARG_RET(NULL, module || strchr(name, ':'), name, NULL);
3855
3856 if (!first) {
3857 return NULL;
3858 }
3859
3860 ctx = first->annotation->module->ctx;
3861
3862 /* parse the name */
3863 tmp = name;
3864 if (ly_parse_nodeid(&tmp, &prefix, &pref_len, &name, &name_len) || tmp[0]) {
3865 LOGERR(ctx, LY_EINVAL, "Metadata name \"%s\" is not valid.", name);
3866 return NULL;
3867 }
3868
3869 /* find the module */
3870 if (prefix) {
3871 str = strndup(prefix, pref_len);
3872 module = ly_ctx_get_module_latest(ctx, str);
3873 free(str);
Radek Krejci422afb12021-03-04 16:38:16 +01003874 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 +02003875 }
3876
3877 /* find the metadata */
3878 LY_LIST_FOR(first, first) {
3879 if ((first->annotation->module == module) && !strcmp(first->name, name)) {
3880 ret = (struct lyd_meta *)first;
3881 break;
3882 }
3883 }
3884
3885 return ret;
3886}
3887
Michal Vasko9b368d32020-02-14 13:53:31 +01003888API LY_ERR
Michal Vaskoe444f752020-02-10 12:20:06 +01003889lyd_find_sibling_first(const struct lyd_node *siblings, const struct lyd_node *target, struct lyd_node **match)
3890{
Michal Vaskoe78faec2021-04-08 17:24:43 +02003891 struct lyd_node **match_p, *iter;
Michal Vaskoe444f752020-02-10 12:20:06 +01003892 struct lyd_node_inner *parent;
Michal Vaskoe78faec2021-04-08 17:24:43 +02003893 ly_bool found;
Michal Vaskoe444f752020-02-10 12:20:06 +01003894
Michal Vaskof03ed032020-03-04 13:31:44 +01003895 LY_CHECK_ARG_RET(NULL, target, LY_EINVAL);
Michal Vaskoe444f752020-02-10 12:20:06 +01003896
Michal Vasko6da1e952020-12-09 18:14:38 +01003897 if (!siblings || (siblings->schema && target->schema &&
3898 (lysc_data_parent(siblings->schema) != lysc_data_parent(target->schema)))) {
Michal Vasko62ed12d2020-05-21 10:08:25 +02003899 /* no data or schema mismatch */
Michal Vasko9b368d32020-02-14 13:53:31 +01003900 if (match) {
3901 *match = NULL;
3902 }
Michal Vaskoe444f752020-02-10 12:20:06 +01003903 return LY_ENOTFOUND;
3904 }
3905
Michal Vaskoe78faec2021-04-08 17:24:43 +02003906 /* get first sibling */
3907 siblings = lyd_first_sibling(siblings);
Michal Vaskoe444f752020-02-10 12:20:06 +01003908
Michal Vasko9e685082021-01-29 14:49:09 +01003909 parent = siblings->parent;
Michal Vasko6da1e952020-12-09 18:14:38 +01003910 if (parent && parent->schema && parent->children_ht) {
Michal Vaskoe444f752020-02-10 12:20:06 +01003911 assert(target->hash);
3912
Michal Vaskoe78faec2021-04-08 17:24:43 +02003913 if (lysc_is_dup_inst_list(target->schema)) {
3914 /* we must search the instances from beginning to find the first matching one */
3915 found = 0;
3916 LYD_LIST_FOR_INST(siblings, target->schema, iter) {
3917 if (!lyd_compare_single(target, iter, 0)) {
3918 found = 1;
3919 break;
3920 }
3921 }
3922 if (found) {
3923 siblings = iter;
Michal Vaskoda859032020-07-14 12:20:14 +02003924 } else {
3925 siblings = NULL;
3926 }
Michal Vaskoe444f752020-02-10 12:20:06 +01003927 } else {
Michal Vaskoe78faec2021-04-08 17:24:43 +02003928 /* find by hash */
3929 if (!lyht_find(parent->children_ht, &target, target->hash, (void **)&match_p)) {
3930 siblings = *match_p;
3931 } else {
3932 /* not found */
3933 siblings = NULL;
3934 }
Michal Vaskoe444f752020-02-10 12:20:06 +01003935 }
3936 } else {
3937 /* no children hash table */
Michal Vaskod989ba02020-08-24 10:59:24 +02003938 for ( ; siblings; siblings = siblings->next) {
Michal Vasko8f359bf2020-07-28 10:41:15 +02003939 if (!lyd_compare_single(siblings, target, 0)) {
Michal Vaskoe444f752020-02-10 12:20:06 +01003940 break;
3941 }
3942 }
3943 }
3944
3945 if (!siblings) {
Michal Vasko9b368d32020-02-14 13:53:31 +01003946 if (match) {
3947 *match = NULL;
3948 }
Michal Vaskoe444f752020-02-10 12:20:06 +01003949 return LY_ENOTFOUND;
3950 }
3951
Michal Vasko9b368d32020-02-14 13:53:31 +01003952 if (match) {
3953 *match = (struct lyd_node *)siblings;
3954 }
Michal Vaskoe444f752020-02-10 12:20:06 +01003955 return LY_SUCCESS;
3956}
3957
Radek Krejci857189e2020-09-01 13:26:36 +02003958/**
3959 * @brief Comparison callback to match schema node with a schema of a data node.
3960 *
3961 * @param[in] val1_p Pointer to the schema node
3962 * @param[in] val2_p Pointer to the data node
Michal Vasko62524a92021-02-26 10:08:50 +01003963 * Implementation of ::lyht_value_equal_cb.
Radek Krejci857189e2020-09-01 13:26:36 +02003964 */
3965static ly_bool
3966lyd_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 +01003967{
3968 struct lysc_node *val1;
3969 struct lyd_node *val2;
3970
3971 val1 = *((struct lysc_node **)val1_p);
3972 val2 = *((struct lyd_node **)val2_p);
3973
Michal Vasko90932a92020-02-12 14:33:03 +01003974 if (val1 == val2->schema) {
3975 /* schema match is enough */
3976 return 1;
3977 } else {
3978 return 0;
3979 }
3980}
3981
Michal Vasko92239c72020-11-18 18:17:25 +01003982/**
3983 * @brief Search in the given siblings (NOT recursively) for the first schema node data instance.
3984 * Uses hashes - should be used whenever possible for best performance.
3985 *
3986 * @param[in] siblings Siblings to search in including preceding and succeeding nodes.
3987 * @param[in] schema Target data node schema to find.
3988 * @param[out] match Can be NULL, otherwise the found data node.
3989 * @return LY_SUCCESS on success, @p match set.
3990 * @return LY_ENOTFOUND if not found, @p match set to NULL.
3991 * @return LY_ERR value if another error occurred.
3992 */
Michal Vasko90932a92020-02-12 14:33:03 +01003993static LY_ERR
3994lyd_find_sibling_schema(const struct lyd_node *siblings, const struct lysc_node *schema, struct lyd_node **match)
3995{
3996 struct lyd_node **match_p;
3997 struct lyd_node_inner *parent;
3998 uint32_t hash;
Michal Vasko62524a92021-02-26 10:08:50 +01003999 lyht_value_equal_cb ht_cb;
Michal Vasko90932a92020-02-12 14:33:03 +01004000
Michal Vaskob104f112020-07-17 09:54:54 +02004001 assert(siblings && schema);
Michal Vasko90932a92020-02-12 14:33:03 +01004002
Michal Vasko9e685082021-01-29 14:49:09 +01004003 parent = siblings->parent;
Michal Vasko08fd6132020-11-18 18:17:45 +01004004 if (parent && parent->schema && parent->children_ht) {
Michal Vasko90932a92020-02-12 14:33:03 +01004005 /* calculate our hash */
4006 hash = dict_hash_multi(0, schema->module->name, strlen(schema->module->name));
4007 hash = dict_hash_multi(hash, schema->name, strlen(schema->name));
4008 hash = dict_hash_multi(hash, NULL, 0);
4009
4010 /* use special hash table function */
4011 ht_cb = lyht_set_cb(parent->children_ht, lyd_hash_table_schema_val_equal);
4012
4013 /* find by hash */
4014 if (!lyht_find(parent->children_ht, &schema, hash, (void **)&match_p)) {
4015 siblings = *match_p;
4016 } else {
4017 /* not found */
4018 siblings = NULL;
4019 }
4020
4021 /* set the original hash table compare function back */
4022 lyht_set_cb(parent->children_ht, ht_cb);
4023 } else {
Michal Vaskob104f112020-07-17 09:54:54 +02004024 /* find first sibling */
4025 if (siblings->parent) {
4026 siblings = siblings->parent->child;
4027 } else {
4028 while (siblings->prev->next) {
4029 siblings = siblings->prev;
4030 }
4031 }
4032
4033 /* search manually without hashes */
Michal Vaskod989ba02020-08-24 10:59:24 +02004034 for ( ; siblings; siblings = siblings->next) {
Michal Vasko90932a92020-02-12 14:33:03 +01004035 if (siblings->schema == schema) {
4036 /* schema match is enough */
4037 break;
4038 }
4039 }
4040 }
4041
4042 if (!siblings) {
Michal Vasko9b368d32020-02-14 13:53:31 +01004043 if (match) {
4044 *match = NULL;
4045 }
Michal Vasko90932a92020-02-12 14:33:03 +01004046 return LY_ENOTFOUND;
4047 }
4048
Michal Vasko9b368d32020-02-14 13:53:31 +01004049 if (match) {
4050 *match = (struct lyd_node *)siblings;
4051 }
Michal Vasko90932a92020-02-12 14:33:03 +01004052 return LY_SUCCESS;
4053}
4054
Michal Vaskoe444f752020-02-10 12:20:06 +01004055API LY_ERR
4056lyd_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 +02004057 size_t val_len, struct lyd_node **match)
Michal Vaskoe444f752020-02-10 12:20:06 +01004058{
4059 LY_ERR rc;
4060 struct lyd_node *target = NULL;
4061
Michal Vasko4c583e82020-07-17 12:16:14 +02004062 LY_CHECK_ARG_RET(NULL, schema, !(schema->nodetype & (LYS_CHOICE | LYS_CASE)), LY_EINVAL);
Michal Vaskoe444f752020-02-10 12:20:06 +01004063
Michal Vasko08fd6132020-11-18 18:17:45 +01004064 if (!siblings || (siblings->schema && (lysc_data_parent(siblings->schema) != lysc_data_parent(schema)))) {
Michal Vasko62ed12d2020-05-21 10:08:25 +02004065 /* no data or schema mismatch */
Michal Vasko9b368d32020-02-14 13:53:31 +01004066 if (match) {
4067 *match = NULL;
4068 }
Michal Vaskoe444f752020-02-10 12:20:06 +01004069 return LY_ENOTFOUND;
4070 }
4071
Michal Vaskof03ed032020-03-04 13:31:44 +01004072 if (key_or_value && !val_len) {
4073 val_len = strlen(key_or_value);
4074 }
4075
Michal Vaskob104f112020-07-17 09:54:54 +02004076 if ((schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) && key_or_value) {
4077 /* create a data node and find the instance */
4078 if (schema->nodetype == LYS_LEAFLIST) {
4079 /* target used attributes: schema, hash, value */
Radek Krejci8df109d2021-04-23 12:19:08 +02004080 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 +02004081 LY_CHECK_RET(rc);
Michal Vaskob104f112020-07-17 09:54:54 +02004082 } else {
Michal Vasko90932a92020-02-12 14:33:03 +01004083 /* target used attributes: schema, hash, child (all keys) */
Michal Vasko004d3152020-06-11 19:59:22 +02004084 LY_CHECK_RET(lyd_create_list2(schema, key_or_value, val_len, &target));
Michal Vasko90932a92020-02-12 14:33:03 +01004085 }
4086
4087 /* find it */
4088 rc = lyd_find_sibling_first(siblings, target, match);
Michal Vaskob104f112020-07-17 09:54:54 +02004089 } else {
4090 /* find the first schema node instance */
4091 rc = lyd_find_sibling_schema(siblings, schema, match);
Michal Vaskoe444f752020-02-10 12:20:06 +01004092 }
4093
Michal Vaskoe444f752020-02-10 12:20:06 +01004094 lyd_free_tree(target);
4095 return rc;
4096}
Michal Vaskoccc02342020-05-21 10:09:21 +02004097
4098API LY_ERR
Michal Vaskoe78faec2021-04-08 17:24:43 +02004099lyd_find_sibling_dup_inst_set(const struct lyd_node *siblings, const struct lyd_node *target, struct ly_set **set)
4100{
4101 struct lyd_node **match_p, *first, *iter;
4102 struct lyd_node_inner *parent;
4103
4104 LY_CHECK_ARG_RET(NULL, target, lysc_is_dup_inst_list(target->schema), set, LY_EINVAL);
4105
4106 LY_CHECK_RET(ly_set_new(set));
4107
4108 if (!siblings || (siblings->schema && (lysc_data_parent(siblings->schema) != lysc_data_parent(target->schema)))) {
4109 /* no data or schema mismatch */
4110 return LY_ENOTFOUND;
4111 }
4112
4113 /* get first sibling */
4114 siblings = lyd_first_sibling(siblings);
4115
4116 parent = siblings->parent;
4117 if (parent && parent->schema && parent->children_ht) {
4118 assert(target->hash);
4119
4120 /* find the first instance */
4121 lyd_find_sibling_first(siblings, target, &first);
4122 if (first) {
4123 /* add it so that it is the first in the set */
4124 if (ly_set_add(*set, first, 1, NULL)) {
4125 goto error;
4126 }
4127
4128 /* find by hash */
4129 if (!lyht_find(parent->children_ht, &target, target->hash, (void **)&match_p)) {
4130 iter = *match_p;
4131 } else {
4132 /* not found */
4133 iter = NULL;
4134 }
4135 while (iter) {
4136 /* add all found nodes into the set */
4137 if ((iter != first) && !lyd_compare_single(iter, target, 0) && ly_set_add(*set, iter, 1, NULL)) {
4138 goto error;
4139 }
4140
4141 /* find next instance */
4142 if (lyht_find_next(parent->children_ht, &iter, iter->hash, (void **)&match_p)) {
4143 iter = NULL;
4144 } else {
4145 iter = *match_p;
4146 }
4147 }
4148 }
4149 } else {
4150 /* no children hash table */
4151 LY_LIST_FOR(siblings, siblings) {
4152 if (!lyd_compare_single(target, siblings, 0)) {
4153 ly_set_add(*set, (void *)siblings, 1, NULL);
4154 }
4155 }
4156 }
4157
4158 if (!(*set)->count) {
4159 return LY_ENOTFOUND;
4160 }
4161 return LY_SUCCESS;
4162
4163error:
4164 ly_set_free(*set, NULL);
4165 *set = NULL;
4166 return LY_EMEM;
4167}
4168
4169API LY_ERR
Michal Vasko1d4af6c2021-02-22 13:31:26 +01004170lyd_find_sibling_opaq_next(const struct lyd_node *first, const char *name, struct lyd_node **match)
4171{
4172 LY_CHECK_ARG_RET(NULL, name, LY_EINVAL);
4173
4174 for ( ; first; first = first->next) {
4175 if (!first->schema && !strcmp(LYD_NAME(first), name)) {
4176 break;
4177 }
4178 }
4179
4180 if (match) {
4181 *match = (struct lyd_node *)first;
4182 }
4183 return first ? LY_SUCCESS : LY_ENOTFOUND;
4184}
4185
4186API LY_ERR
Michal Vaskoccc02342020-05-21 10:09:21 +02004187lyd_find_xpath(const struct lyd_node *ctx_node, const char *xpath, struct ly_set **set)
4188{
4189 LY_ERR ret = LY_SUCCESS;
4190 struct lyxp_set xp_set;
Radek Krejcif03a9e22020-09-18 20:09:31 +02004191 struct lyxp_expr *exp = NULL;
Michal Vaskoccc02342020-05-21 10:09:21 +02004192 uint32_t i;
4193
4194 LY_CHECK_ARG_RET(NULL, ctx_node, xpath, set, LY_EINVAL);
4195
Michal Vasko37c69432021-04-12 14:48:24 +02004196 *set = NULL;
Michal Vaskoccc02342020-05-21 10:09:21 +02004197 memset(&xp_set, 0, sizeof xp_set);
4198
4199 /* compile expression */
Radek Krejcif03a9e22020-09-18 20:09:31 +02004200 ret = lyxp_expr_parse((struct ly_ctx *)LYD_CTX(ctx_node), xpath, 0, 1, &exp);
4201 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskoccc02342020-05-21 10:09:21 +02004202
4203 /* evaluate expression */
Radek Krejci8df109d2021-04-23 12:19:08 +02004204 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 +02004205 LY_CHECK_GOTO(ret, cleanup);
4206
4207 /* allocate return set */
Radek Krejciba03a5a2020-08-27 14:40:41 +02004208 ret = ly_set_new(set);
4209 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskoccc02342020-05-21 10:09:21 +02004210
4211 /* transform into ly_set */
4212 if (xp_set.type == LYXP_SET_NODE_SET) {
4213 /* allocate memory for all the elements once (even though not all items must be elements but most likely will be) */
4214 (*set)->objs = malloc(xp_set.used * sizeof *(*set)->objs);
Michal Vaskob7be7a82020-08-20 09:09:04 +02004215 LY_CHECK_ERR_GOTO(!(*set)->objs, LOGMEM(LYD_CTX(ctx_node)); ret = LY_EMEM, cleanup);
Michal Vaskoccc02342020-05-21 10:09:21 +02004216 (*set)->size = xp_set.used;
4217
4218 for (i = 0; i < xp_set.used; ++i) {
4219 if (xp_set.val.nodes[i].type == LYXP_NODE_ELEM) {
Radek Krejci3d92e442020-10-12 12:48:13 +02004220 ret = ly_set_add(*set, xp_set.val.nodes[i].node, 1, NULL);
Radek Krejciba03a5a2020-08-27 14:40:41 +02004221 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskoccc02342020-05-21 10:09:21 +02004222 }
4223 }
4224 }
4225
4226cleanup:
Michal Vasko0691d522020-05-21 13:21:47 +02004227 lyxp_set_free_content(&xp_set);
Michal Vaskob7be7a82020-08-20 09:09:04 +02004228 lyxp_expr_free((struct ly_ctx *)LYD_CTX(ctx_node), exp);
Radek Krejci8f45abc2020-11-26 12:15:13 +01004229 if (ret) {
4230 ly_set_free(*set, NULL);
4231 *set = NULL;
4232 }
Michal Vaskoccc02342020-05-21 10:09:21 +02004233 return ret;
4234}
Radek Krejcica989142020-11-05 11:32:22 +01004235
Michal Vasko3e1f6552021-01-14 09:27:55 +01004236API LY_ERR
4237lyd_find_path(const struct lyd_node *ctx_node, const char *path, ly_bool output, struct lyd_node **match)
4238{
4239 LY_ERR ret = LY_SUCCESS;
4240 struct lyxp_expr *expr = NULL;
4241 struct ly_path *lypath = NULL;
4242
4243 LY_CHECK_ARG_RET(NULL, ctx_node, ctx_node->schema, path, LY_EINVAL);
4244
4245 /* parse the path */
4246 ret = ly_path_parse(LYD_CTX(ctx_node), ctx_node->schema, path, strlen(path), LY_PATH_BEGIN_EITHER, LY_PATH_LREF_FALSE,
4247 LY_PATH_PREFIX_OPTIONAL, LY_PATH_PRED_SIMPLE, &expr);
4248 LY_CHECK_GOTO(ret, cleanup);
4249
4250 /* compile the path */
Radek Krejcid5d37432021-03-12 13:46:40 +01004251 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 +02004252 output ? LY_PATH_OPER_OUTPUT : LY_PATH_OPER_INPUT, LY_PATH_TARGET_SINGLE, LY_VALUE_JSON,
Michal Vasko3e1f6552021-01-14 09:27:55 +01004253 (void *)LYD_CTX(ctx_node), NULL, &lypath);
4254 LY_CHECK_GOTO(ret, cleanup);
4255
4256 /* evaluate the path */
4257 ret = ly_path_eval_partial(lypath, ctx_node, NULL, match);
4258
4259cleanup:
4260 lyxp_expr_free(LYD_CTX(ctx_node), expr);
4261 ly_path_free(LYD_CTX(ctx_node), lypath);
4262 return ret;
4263}
4264
Radek Krejcica989142020-11-05 11:32:22 +01004265API uint32_t
4266lyd_list_pos(const struct lyd_node *instance)
4267{
4268 const struct lyd_node *iter = NULL;
4269 uint32_t pos = 0;
4270
4271 if (!instance || !(instance->schema->nodetype & (LYS_LIST | LYS_LEAFLIST))) {
4272 return 0;
4273 }
4274
4275 /* data instances are ordered, so we can stop when we found instance of other schema node */
4276 for (iter = instance; iter->schema == instance->schema; iter = iter->prev) {
Radek Krejcibb27df32020-11-13 15:39:16 +01004277 if (pos && (iter->next == NULL)) {
Radek Krejcica989142020-11-05 11:32:22 +01004278 /* overrun to the end of the siblings list */
4279 break;
4280 }
4281 ++pos;
4282 }
4283
4284 return pos;
4285}
Radek Krejci4233f9b2020-11-05 12:38:35 +01004286
4287API struct lyd_node *
4288lyd_first_sibling(const struct lyd_node *node)
4289{
4290 struct lyd_node *start;
4291
4292 if (!node) {
4293 return NULL;
4294 }
4295
4296 /* get the first sibling */
4297 if (node->parent) {
4298 start = node->parent->child;
4299 } else {
Radek Krejcibb27df32020-11-13 15:39:16 +01004300 for (start = (struct lyd_node *)node; start->prev->next; start = start->prev) {}
Radek Krejci4233f9b2020-11-05 12:38:35 +01004301 }
4302
4303 return start;
4304}