blob: 2ab998e2996eb8bca98005169dc3afd8c9cb5bd2 [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 Vasko62ed8652021-04-28 09:38:37 +020066 if (!value) {
67 value = "";
68 }
Michal Vaskofeca4fb2020-10-05 08:58:40 +020069 if (incomplete) {
70 *incomplete = 0;
Radek Krejci084289f2019-07-09 17:35:30 +020071 }
72
Michal Vasko405cc9e2020-12-01 12:01:27 +010073 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 +010074 if (dynamic) {
75 *dynamic = 0;
76 }
77
Michal Vasko90932a92020-02-12 14:33:03 +010078 if (ret == LY_EINCOMPLETE) {
Michal Vaskofeca4fb2020-10-05 08:58:40 +020079 if (incomplete) {
80 *incomplete = 1;
81 }
82 } else if (ret) {
83 if (err) {
aPiecek4204b612021-05-03 09:06:16 +020084 LOGVAL_ERRITEM(ctx, err);
Michal Vaskofeca4fb2020-10-05 08:58:40 +020085 ly_err_free(err);
86 } else {
Michal Vasko509d7212022-02-21 08:47:23 +010087 LOGVAL(ctx, LYVE_OTHER, "Storing value failed.");
Michal Vaskofeca4fb2020-10-05 08:58:40 +020088 }
89 return ret;
Michal Vasko90932a92020-02-12 14:33:03 +010090 }
91
Michal Vaskofeca4fb2020-10-05 08:58:40 +020092 return LY_SUCCESS;
Michal Vasko90932a92020-02-12 14:33:03 +010093}
94
Radek Krejci38d85362019-09-05 16:26:38 +020095LY_ERR
Michal Vaskofeca4fb2020-10-05 08:58:40 +020096lyd_value_validate_incomplete(const struct ly_ctx *ctx, const struct lysc_type *type, struct lyd_value *val,
Radek Krejci2efc45b2020-12-22 16:25:44 +010097 const struct lyd_node *ctx_node, const struct lyd_node *tree)
Radek Krejci38d85362019-09-05 16:26:38 +020098{
Michal Vaskofeca4fb2020-10-05 08:58:40 +020099 LY_ERR ret;
Radek Krejci38d85362019-09-05 16:26:38 +0200100 struct ly_err_item *err = NULL;
Radek Krejci38d85362019-09-05 16:26:38 +0200101
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200102 assert(type->plugin->validate);
Michal Vasko8d544252020-03-02 10:19:52 +0100103
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200104 ret = type->plugin->validate(ctx, type, ctx_node, tree, val, &err);
105 if (ret) {
Radek Krejci38d85362019-09-05 16:26:38 +0200106 if (err) {
aPiecek4204b612021-05-03 09:06:16 +0200107 LOGVAL_ERRITEM(ctx, err);
Radek Krejci38d85362019-09-05 16:26:38 +0200108 ly_err_free(err);
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200109 } else {
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200110 LOGVAL(ctx, LYVE_OTHER, "Resolving value \"%s\" failed.", type->plugin->print(ctx, val, LY_VALUE_CANON,
111 NULL, NULL, NULL));
Radek Krejci38d85362019-09-05 16:26:38 +0200112 }
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200113 return ret;
Radek Krejci38d85362019-09-05 16:26:38 +0200114 }
115
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200116 return LY_SUCCESS;
Radek Krejci38d85362019-09-05 16:26:38 +0200117}
118
Michal Vaskof937cfe2020-08-03 16:07:12 +0200119LY_ERR
Michal Vaskoaebbce02021-04-06 09:23:37 +0200120lys_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 +0200121 LY_VALUE_FORMAT format, void *prefix_data)
Radek Krejci084289f2019-07-09 17:35:30 +0200122{
123 LY_ERR rc = LY_SUCCESS;
124 struct ly_err_item *err = NULL;
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200125 struct lyd_value storage;
Radek Krejci084289f2019-07-09 17:35:30 +0200126 struct lysc_type *type;
Radek Krejci084289f2019-07-09 17:35:30 +0200127
128 LY_CHECK_ARG_RET(ctx, node, value, LY_EINVAL);
129
130 if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
131 LOGARG(ctx, node);
132 return LY_EINVAL;
133 }
134
Michal Vasko22df3f02020-08-24 13:29:22 +0200135 type = ((struct lysc_node_leaf *)node)->type;
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200136 rc = type->plugin->store(ctx ? ctx : node->module->ctx, type, value, value_len, 0, format, prefix_data,
Michal Vasko405cc9e2020-12-01 12:01:27 +0100137 LYD_HINT_SCHEMA, node, &storage, NULL, &err);
Radek Krejci73dead22019-07-11 16:46:16 +0200138 if (rc == LY_EINCOMPLETE) {
139 /* actually success since we do not provide the context tree and call validation with
140 * LY_TYPE_OPTS_INCOMPLETE_DATA */
141 rc = LY_SUCCESS;
142 } else if (rc && err) {
143 if (ctx) {
144 /* log only in case the ctx was provided as input parameter */
Radek Krejciddace2c2021-01-08 11:30:56 +0100145 LOG_LOCSET(NULL, NULL, err->path, NULL);
aPiecek4204b612021-05-03 09:06:16 +0200146 LOGVAL_ERRITEM(ctx, err);
Radek Krejciddace2c2021-01-08 11:30:56 +0100147 LOG_LOCBACK(0, 0, 1, 0);
Radek Krejci084289f2019-07-09 17:35:30 +0200148 }
Radek Krejci73dead22019-07-11 16:46:16 +0200149 ly_err_free(err);
Radek Krejci084289f2019-07-09 17:35:30 +0200150 }
151
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200152 if (!rc) {
153 type->plugin->free(ctx ? ctx : node->module->ctx, &storage);
154 }
Radek Krejci084289f2019-07-09 17:35:30 +0200155 return rc;
156}
157
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100158LIBYANG_API_DEF LY_ERR
Michal Vaskoaebbce02021-04-06 09:23:37 +0200159lyd_value_validate(const struct ly_ctx *ctx, const struct lysc_node *schema, const char *value, size_t value_len,
160 const struct lyd_node *ctx_node, const struct lysc_type **realtype, const char **canonical)
Radek Krejci084289f2019-07-09 17:35:30 +0200161{
162 LY_ERR rc;
163 struct ly_err_item *err = NULL;
164 struct lysc_type *type;
Michal Vasko3701af52020-08-03 14:29:38 +0200165 struct lyd_value val = {0};
Radek Krejci09c77442021-04-26 11:10:34 +0200166 ly_bool stored = 0, log = 1;
Radek Krejci084289f2019-07-09 17:35:30 +0200167
Michal Vaskoaebbce02021-04-06 09:23:37 +0200168 LY_CHECK_ARG_RET(ctx, schema, value, LY_EINVAL);
Radek Krejci084289f2019-07-09 17:35:30 +0200169
Radek Krejci09c77442021-04-26 11:10:34 +0200170 if (!ctx) {
171 ctx = schema->module->ctx;
172 log = 0;
173 }
Michal Vaskoaebbce02021-04-06 09:23:37 +0200174 type = ((struct lysc_node_leaf *)schema)->type;
175
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200176 /* store */
Radek Krejci09c77442021-04-26 11:10:34 +0200177 rc = type->plugin->store(ctx, type, value, value_len, 0, LY_VALUE_JSON, NULL,
Michal Vaskoaebbce02021-04-06 09:23:37 +0200178 LYD_HINT_DATA, schema, &val, NULL, &err);
179 if (!rc || (rc == LY_EINCOMPLETE)) {
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200180 stored = 1;
Michal Vaskoaebbce02021-04-06 09:23:37 +0200181 }
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200182
Michal Vaskoaebbce02021-04-06 09:23:37 +0200183 if (ctx_node && (rc == LY_EINCOMPLETE)) {
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200184 /* resolve */
Radek Krejci09c77442021-04-26 11:10:34 +0200185 rc = type->plugin->validate(ctx, type, ctx_node, ctx_node, &val, &err);
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200186 }
187
Michal Vaskoaebbce02021-04-06 09:23:37 +0200188 if (rc && (rc != LY_EINCOMPLETE) && err) {
Radek Krejci09c77442021-04-26 11:10:34 +0200189 if (log) {
Michal Vaskoaebbce02021-04-06 09:23:37 +0200190 /* log error */
191 if (err->path) {
192 LOG_LOCSET(NULL, NULL, err->path, NULL);
193 } else if (ctx_node) {
194 LOG_LOCSET(NULL, ctx_node, NULL, NULL);
195 } else {
196 LOG_LOCSET(schema, NULL, NULL, NULL);
Radek Krejci084289f2019-07-09 17:35:30 +0200197 }
aPiecek4204b612021-05-03 09:06:16 +0200198 LOGVAL_ERRITEM(ctx, err);
Michal Vaskoaebbce02021-04-06 09:23:37 +0200199 if (err->path) {
200 LOG_LOCBACK(0, 0, 1, 0);
201 } else if (ctx_node) {
202 LOG_LOCBACK(0, 1, 0, 0);
203 } else {
204 LOG_LOCBACK(1, 0, 0, 0);
205 }
Radek Krejci084289f2019-07-09 17:35:30 +0200206 }
Michal Vaskoaebbce02021-04-06 09:23:37 +0200207 ly_err_free(err);
Radek Krejci084289f2019-07-09 17:35:30 +0200208 }
209
Michal Vaskoaebbce02021-04-06 09:23:37 +0200210 if (!rc || (rc == LY_EINCOMPLETE)) {
211 if (realtype) {
212 /* return realtype */
213 if (val.realtype->basetype == LY_TYPE_UNION) {
214 *realtype = val.subvalue->value.realtype;
215 } else {
216 *realtype = val.realtype;
217 }
218 }
219
220 if (canonical) {
221 /* return canonical value */
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200222 lydict_insert(ctx, val.realtype->plugin->print(ctx, &val, LY_VALUE_CANON, NULL, NULL, NULL), 0, canonical);
Michal Vasko29134f82020-11-13 18:03:20 +0100223 }
Michal Vasko3701af52020-08-03 14:29:38 +0200224 }
225
Michal Vaskoaebbce02021-04-06 09:23:37 +0200226 if (stored) {
227 /* free value */
228 type->plugin->free(ctx ? ctx : schema->module->ctx, &val);
229 }
230 return rc;
Radek Krejci084289f2019-07-09 17:35:30 +0200231}
232
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100233LIBYANG_API_DEF LY_ERR
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200234lyd_value_compare(const struct lyd_node_term *node, const char *value, size_t value_len)
Radek Krejci084289f2019-07-09 17:35:30 +0200235{
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200236 LY_ERR ret = LY_SUCCESS;
Radek Krejci084289f2019-07-09 17:35:30 +0200237 struct ly_ctx *ctx;
238 struct lysc_type *type;
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200239 struct lyd_value val = {0};
Radek Krejci084289f2019-07-09 17:35:30 +0200240
241 LY_CHECK_ARG_RET(node ? node->schema->module->ctx : NULL, node, value, LY_EINVAL);
242
243 ctx = node->schema->module->ctx;
Michal Vasko22df3f02020-08-24 13:29:22 +0200244 type = ((struct lysc_node_leaf *)node->schema)->type;
Radek Krejci084289f2019-07-09 17:35:30 +0200245
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200246 /* store the value */
Michal Vasko9e685082021-01-29 14:49:09 +0100247 LOG_LOCSET(node->schema, &node->node, NULL, NULL);
Radek Krejci8df109d2021-04-23 12:19:08 +0200248 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 +0100249 LOG_LOCBACK(1, 1, 0, 0);
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200250 LY_CHECK_RET(ret);
Radek Krejci084289f2019-07-09 17:35:30 +0200251
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200252 /* compare values */
253 ret = type->plugin->compare(&node->value, &val);
Radek Krejci084289f2019-07-09 17:35:30 +0200254
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200255 type->plugin->free(ctx, &val);
Radek Krejci084289f2019-07-09 17:35:30 +0200256 return ret;
257}
258
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100259LIBYANG_API_DEF ly_bool
Radek Krejci19611252020-10-04 13:54:53 +0200260lyd_is_default(const struct lyd_node *node)
261{
262 const struct lysc_node_leaf *leaf;
263 const struct lysc_node_leaflist *llist;
264 const struct lyd_node_term *term;
265 LY_ARRAY_COUNT_TYPE u;
266
Michal Vasko39eb6882021-09-16 08:53:13 +0200267 if (!(node->schema->nodetype & LYD_NODE_TERM)) {
268 return 0;
269 }
270
Radek Krejci19611252020-10-04 13:54:53 +0200271 term = (const struct lyd_node_term *)node;
272
273 if (node->schema->nodetype == LYS_LEAF) {
274 leaf = (const struct lysc_node_leaf *)node->schema;
275 if (!leaf->dflt) {
276 return 0;
277 }
278
279 /* compare with the default value */
Michal Vasko39eb6882021-09-16 08:53:13 +0200280 if (!leaf->type->plugin->compare(&term->value, leaf->dflt)) {
281 return 1;
Radek Krejci19611252020-10-04 13:54:53 +0200282 }
283 } else {
284 llist = (const struct lysc_node_leaflist *)node->schema;
285 if (!llist->dflts) {
286 return 0;
287 }
288
289 LY_ARRAY_FOR(llist->dflts, u) {
290 /* compare with each possible default value */
Michal Vasko39eb6882021-09-16 08:53:13 +0200291 if (!llist->type->plugin->compare(&term->value, llist->dflts[u])) {
292 return 1;
Radek Krejci19611252020-10-04 13:54:53 +0200293 }
294 }
295 }
296
Michal Vasko39eb6882021-09-16 08:53:13 +0200297 return 0;
Radek Krejci19611252020-10-04 13:54:53 +0200298}
299
Radek Krejci7931b192020-06-25 17:05:03 +0200300static LYD_FORMAT
Michal Vasko63f3d842020-07-08 10:10:14 +0200301lyd_parse_get_format(const struct ly_in *in, LYD_FORMAT format)
Radek Krejcie7b95092019-05-15 11:03:07 +0200302{
Michal Vasko69730152020-10-09 16:30:07 +0200303 if (!format && (in->type == LY_IN_FILEPATH)) {
Radek Krejcie7b95092019-05-15 11:03:07 +0200304 /* unknown format - try to detect it from filename's suffix */
Radek Krejci7931b192020-06-25 17:05:03 +0200305 const char *path = in->method.fpath.filepath;
306 size_t len = strlen(path);
Radek Krejcie7b95092019-05-15 11:03:07 +0200307
308 /* ignore trailing whitespaces */
Michal Vaskod989ba02020-08-24 10:59:24 +0200309 for ( ; len > 0 && isspace(path[len - 1]); len--) {}
Radek Krejcie7b95092019-05-15 11:03:07 +0200310
Radek Krejcif13b87b2020-12-01 22:02:17 +0100311 if ((len >= LY_XML_SUFFIX_LEN + 1) &&
312 !strncmp(&path[len - LY_XML_SUFFIX_LEN], LY_XML_SUFFIX, LY_XML_SUFFIX_LEN)) {
Radek Krejcie7b95092019-05-15 11:03:07 +0200313 format = LYD_XML;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100314 } else if ((len >= LY_JSON_SUFFIX_LEN + 1) &&
315 !strncmp(&path[len - LY_JSON_SUFFIX_LEN], LY_JSON_SUFFIX, LY_JSON_SUFFIX_LEN)) {
Radek Krejcie7b95092019-05-15 11:03:07 +0200316 format = LYD_JSON;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100317 } else if ((len >= LY_LYB_SUFFIX_LEN + 1) &&
318 !strncmp(&path[len - LY_LYB_SUFFIX_LEN], LY_LYB_SUFFIX, LY_LYB_SUFFIX_LEN)) {
Radek Krejcie7b95092019-05-15 11:03:07 +0200319 format = LYD_LYB;
Radek Krejci7931b192020-06-25 17:05:03 +0200320 } /* else still unknown */
Radek Krejcie7b95092019-05-15 11:03:07 +0200321 }
322
Radek Krejci7931b192020-06-25 17:05:03 +0200323 return format;
324}
Radek Krejcie7b95092019-05-15 11:03:07 +0200325
Michal Vaskoe0665742021-02-11 11:08:44 +0100326/**
327 * @brief Parse YANG data into a data tree.
328 *
329 * @param[in] ctx libyang context.
Radek Krejcif16e2542021-02-17 15:39:23 +0100330 * @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 +0100331 * @param[in] parent Parent to connect the parsed nodes to, if any.
332 * @param[in,out] first_p Pointer to the first top-level parsed node, used only if @p parent is NULL.
333 * @param[in] in Input handle to read the input from.
334 * @param[in] format Expected format of the data in @p in.
335 * @param[in] parse_opts Options for parser.
336 * @param[in] val_opts Options for validation.
337 * @param[out] op Optional pointer to the parsed operation, if any.
338 * @return LY_ERR value.
339 */
340static LY_ERR
Radek Krejcif16e2542021-02-17 15:39:23 +0100341lyd_parse(const struct ly_ctx *ctx, const struct lysc_ext_instance *ext, struct lyd_node *parent, struct lyd_node **first_p,
342 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 +0200343{
Michal Vaskoe0665742021-02-11 11:08:44 +0100344 LY_ERR rc = LY_SUCCESS;
Radek Krejci1798aae2020-07-14 13:26:06 +0200345 struct lyd_ctx *lydctx = NULL;
Michal Vaskoe0665742021-02-11 11:08:44 +0100346 struct ly_set parsed = {0};
347 struct lyd_node *first;
348 uint32_t i;
Michal Vaskoddd76592022-01-17 13:34:48 +0100349 ly_bool subtree_sibling = 0;
Radek Krejci1798aae2020-07-14 13:26:06 +0200350
Michal Vaskoe0665742021-02-11 11:08:44 +0100351 assert(ctx && (parent || first_p));
Radek Krejci7931b192020-06-25 17:05:03 +0200352
353 format = lyd_parse_get_format(in, format);
Michal Vaskoe0665742021-02-11 11:08:44 +0100354 if (first_p) {
355 *first_p = NULL;
356 }
Radek Krejci1798aae2020-07-14 13:26:06 +0200357
Michal Vasko63f3d842020-07-08 10:10:14 +0200358 /* remember input position */
359 in->func_start = in->current;
Radek Krejci7931b192020-06-25 17:05:03 +0200360
Michal Vaskoe0665742021-02-11 11:08:44 +0100361 /* parse the data */
Radek Krejci7931b192020-06-25 17:05:03 +0200362 switch (format) {
363 case LYD_XML:
Michal Vaskoddd76592022-01-17 13:34:48 +0100364 rc = lyd_parse_xml(ctx, ext, parent, first_p, in, parse_opts, val_opts, LYD_TYPE_DATA_YANG, NULL, &parsed,
365 &subtree_sibling, &lydctx);
Radek Krejci1798aae2020-07-14 13:26:06 +0200366 break;
Radek Krejci7931b192020-06-25 17:05:03 +0200367 case LYD_JSON:
Michal Vaskoddd76592022-01-17 13:34:48 +0100368 rc = lyd_parse_json(ctx, ext, parent, first_p, in, parse_opts, val_opts, LYD_TYPE_DATA_YANG, &parsed,
369 &subtree_sibling, &lydctx);
Radek Krejci1798aae2020-07-14 13:26:06 +0200370 break;
Radek Krejci7931b192020-06-25 17:05:03 +0200371 case LYD_LYB:
Michal Vaskoddd76592022-01-17 13:34:48 +0100372 rc = lyd_parse_lyb(ctx, ext, parent, first_p, in, parse_opts, val_opts, LYD_TYPE_DATA_YANG, &parsed,
373 &subtree_sibling, &lydctx);
Radek Krejci1798aae2020-07-14 13:26:06 +0200374 break;
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200375 case LYD_UNKNOWN:
Michal Vaskod74978f2021-02-12 11:59:36 +0100376 LOGARG(ctx, format);
377 rc = LY_EINVAL;
Michal Vaskoe0665742021-02-11 11:08:44 +0100378 break;
379 }
380 LY_CHECK_GOTO(rc, cleanup);
381
382 if (parent) {
383 /* get first top-level sibling */
384 for (first = parent; first->parent; first = lyd_parent(first)) {}
385 first = lyd_first_sibling(first);
386 first_p = &first;
Radek Krejci7931b192020-06-25 17:05:03 +0200387 }
388
Michal Vaskoe0665742021-02-11 11:08:44 +0100389 if (!(parse_opts & LYD_PARSE_ONLY)) {
390 /* validate data */
Michal Vaskoddd76592022-01-17 13:34:48 +0100391 rc = lyd_validate(first_p, NULL, ctx, val_opts, 0, &lydctx->node_when, &lydctx->node_types, &lydctx->meta_types,
392 &lydctx->ext_val, NULL);
Michal Vaskoe0665742021-02-11 11:08:44 +0100393 LY_CHECK_GOTO(rc, cleanup);
394 }
Radek Krejci7931b192020-06-25 17:05:03 +0200395
Michal Vaskoe0665742021-02-11 11:08:44 +0100396 /* set the operation node */
397 if (op) {
398 *op = lydctx->op_node;
Radek Krejci1798aae2020-07-14 13:26:06 +0200399 }
400
401cleanup:
Michal Vaskoe0665742021-02-11 11:08:44 +0100402 if (lydctx) {
403 lydctx->free(lydctx);
Radek Krejci1798aae2020-07-14 13:26:06 +0200404 }
Michal Vaskoe0665742021-02-11 11:08:44 +0100405 if (rc) {
406 if (parent) {
407 /* free all the parsed subtrees */
408 for (i = 0; i < parsed.count; ++i) {
409 lyd_free_tree(parsed.dnodes[i]);
410 }
411 } else {
412 /* free everything */
413 lyd_free_all(*first_p);
414 *first_p = NULL;
415 }
Michal Vaskoddd76592022-01-17 13:34:48 +0100416 } else if (subtree_sibling) {
417 rc = LY_ENOT;
Michal Vaskoe0665742021-02-11 11:08:44 +0100418 }
419 ly_set_erase(&parsed, NULL);
420 return rc;
Radek Krejci7931b192020-06-25 17:05:03 +0200421}
422
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100423LIBYANG_API_DEF LY_ERR
Radek Krejcif16e2542021-02-17 15:39:23 +0100424lyd_parse_ext_data(const struct lysc_ext_instance *ext, struct lyd_node *parent, struct ly_in *in, LYD_FORMAT format,
425 uint32_t parse_options, uint32_t validate_options, struct lyd_node **tree)
426{
427 const struct ly_ctx *ctx = ext ? ext->module->ctx : NULL;
428
429 LY_CHECK_ARG_RET(ctx, ext, in, parent || tree, LY_EINVAL);
430 LY_CHECK_ARG_RET(ctx, !(parse_options & ~LYD_PARSE_OPTS_MASK), LY_EINVAL);
431 LY_CHECK_ARG_RET(ctx, !(validate_options & ~LYD_VALIDATE_OPTS_MASK), LY_EINVAL);
432
433 return lyd_parse(ctx, ext, parent, tree, in, format, parse_options, validate_options, NULL);
434}
435
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100436LIBYANG_API_DEF LY_ERR
Michal Vaskoe0665742021-02-11 11:08:44 +0100437lyd_parse_data(const struct ly_ctx *ctx, struct lyd_node *parent, struct ly_in *in, LYD_FORMAT format,
438 uint32_t parse_options, uint32_t validate_options, struct lyd_node **tree)
439{
440 LY_CHECK_ARG_RET(ctx, ctx, in, parent || tree, LY_EINVAL);
441 LY_CHECK_ARG_RET(ctx, !(parse_options & ~LYD_PARSE_OPTS_MASK), LY_EINVAL);
442 LY_CHECK_ARG_RET(ctx, !(validate_options & ~LYD_VALIDATE_OPTS_MASK), LY_EINVAL);
443
Radek Krejcif16e2542021-02-17 15:39:23 +0100444 return lyd_parse(ctx, NULL, parent, tree, in, format, parse_options, validate_options, NULL);
Michal Vaskoe0665742021-02-11 11:08:44 +0100445}
446
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100447LIBYANG_API_DEF LY_ERR
Michal Vaskoe0665742021-02-11 11:08:44 +0100448lyd_parse_data_mem(const struct ly_ctx *ctx, const char *data, LYD_FORMAT format, uint32_t parse_options,
449 uint32_t validate_options, struct lyd_node **tree)
Radek Krejci7931b192020-06-25 17:05:03 +0200450{
451 LY_ERR ret;
452 struct ly_in *in;
453
454 LY_CHECK_RET(ly_in_new_memory(data, &in));
Michal Vaskoe0665742021-02-11 11:08:44 +0100455 ret = lyd_parse_data(ctx, NULL, in, format, parse_options, validate_options, tree);
Radek Krejci7931b192020-06-25 17:05:03 +0200456
457 ly_in_free(in, 0);
458 return ret;
459}
460
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100461LIBYANG_API_DEF LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200462lyd_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 +0200463 struct lyd_node **tree)
Radek Krejci7931b192020-06-25 17:05:03 +0200464{
465 LY_ERR ret;
466 struct ly_in *in;
467
468 LY_CHECK_RET(ly_in_new_fd(fd, &in));
Michal Vaskoe0665742021-02-11 11:08:44 +0100469 ret = lyd_parse_data(ctx, NULL, in, format, parse_options, validate_options, tree);
Radek Krejci7931b192020-06-25 17:05:03 +0200470
471 ly_in_free(in, 0);
472 return ret;
473}
474
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100475LIBYANG_API_DEF LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200476lyd_parse_data_path(const struct ly_ctx *ctx, const char *path, LYD_FORMAT format, uint32_t parse_options,
477 uint32_t validate_options, struct lyd_node **tree)
Radek Krejci7931b192020-06-25 17:05:03 +0200478{
479 LY_ERR ret;
480 struct ly_in *in;
481
482 LY_CHECK_RET(ly_in_new_filepath(path, 0, &in));
Michal Vaskoe0665742021-02-11 11:08:44 +0100483 ret = lyd_parse_data(ctx, NULL, in, format, parse_options, validate_options, tree);
Radek Krejci7931b192020-06-25 17:05:03 +0200484
485 ly_in_free(in, 0);
486 return ret;
487}
488
Radek Krejcif16e2542021-02-17 15:39:23 +0100489/**
490 * @brief Parse YANG data into an operation data tree, in case the extension instance is specified, keep the searching
491 * for schema nodes locked inside the extension instance.
492 *
493 * At least one of @p parent, @p tree, or @p op must always be set.
494 *
495 * Specific @p data_type values have different parameter meaning as follows:
496 * - ::LYD_TYPE_RPC_NETCONF:
497 * - @p parent - must be NULL, the whole RPC is expected;
498 * - @p format - must be ::LYD_XML, NETCONF supports only this format;
499 * - @p tree - must be provided, all the NETCONF-specific XML envelopes will be returned here as
500 * a separate opaque data tree, even if the function fails, this may be returned;
501 * - @p op - must be provided, the RPC/action data tree itself will be returned here, pointing to the operation;
502 *
503 * - ::LYD_TYPE_NOTIF_NETCONF:
504 * - @p parent - must be NULL, the whole notification is expected;
505 * - @p format - must be ::LYD_XML, NETCONF supports only this format;
506 * - @p tree - must be provided, all the NETCONF-specific XML envelopes will be returned here as
507 * a separate opaque data tree, even if the function fails, this may be returned;
508 * - @p op - must be provided, the notification data tree itself will be returned here, pointing to the operation;
509 *
510 * - ::LYD_TYPE_REPLY_NETCONF:
511 * - @p parent - must be set, pointing to the invoked RPC operation (RPC or action) node;
512 * - @p format - must be ::LYD_XML, NETCONF supports only this format;
513 * - @p tree - must be provided, all the NETCONF-specific XML envelopes will be returned here as
514 * a separate opaque data tree, even if the function fails, this may be returned;
515 * - @p op - must be NULL, the reply is appended to the RPC;
516 * Note that there are 3 kinds of NETCONF replies - ok, error, and data. Only data reply appends any nodes to the RPC.
517 *
518 * @param[in] ctx libyang context.
519 * @param[in] ext Extension instance providing the specific schema tree to match with the data being parsed.
520 * @param[in] parent Optional parent to connect the parsed nodes to.
521 * @param[in] in Input handle to read the input from.
522 * @param[in] format Expected format of the data in @p in.
523 * @param[in] data_type Expected operation to parse (@ref datatype).
524 * @param[out] tree Optional full parsed data tree. If @p parent is set, set to NULL.
525 * @param[out] op Optional parsed operation node.
526 * @return LY_ERR value.
527 * @return LY_ENOT if @p data_type is a NETCONF message and the root XML element is not the expected one.
528 */
529static LY_ERR
530lyd_parse_op_(const struct ly_ctx *ctx, const struct lysc_ext_instance *ext, struct lyd_node *parent,
531 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 +0200532{
Michal Vaskoe0665742021-02-11 11:08:44 +0100533 LY_ERR rc = LY_SUCCESS;
534 struct lyd_ctx *lydctx = NULL;
535 struct ly_set parsed = {0};
536 struct lyd_node *first = NULL, *envp = NULL;
537 uint32_t i, parse_opts, val_opts;
Radek Krejci7931b192020-06-25 17:05:03 +0200538
Michal Vasko27fb0262021-02-23 09:42:01 +0100539 if (!ctx) {
540 ctx = LYD_CTX(parent);
541 }
Radek Krejci1798aae2020-07-14 13:26:06 +0200542 if (tree) {
543 *tree = NULL;
544 }
545 if (op) {
546 *op = NULL;
547 }
548
Radek Krejci7931b192020-06-25 17:05:03 +0200549 format = lyd_parse_get_format(in, format);
Radek Krejci7931b192020-06-25 17:05:03 +0200550
Michal Vasko63f3d842020-07-08 10:10:14 +0200551 /* remember input position */
552 in->func_start = in->current;
553
Michal Vaskoe0665742021-02-11 11:08:44 +0100554 /* check params based on the data type */
Michal Vasko1e4c68e2021-02-18 15:03:01 +0100555 if ((data_type == LYD_TYPE_RPC_NETCONF) || (data_type == LYD_TYPE_NOTIF_NETCONF)) {
Michal Vaskoe0665742021-02-11 11:08:44 +0100556 LY_CHECK_ARG_RET(ctx, format == LYD_XML, !parent, tree, op, LY_EINVAL);
Michal Vasko1e4c68e2021-02-18 15:03:01 +0100557 } else if (data_type == LYD_TYPE_REPLY_NETCONF) {
558 LY_CHECK_ARG_RET(ctx, format == LYD_XML, parent, parent->schema->nodetype & (LYS_RPC | LYS_ACTION), tree, !op,
559 LY_EINVAL);
Michal Vaskoe0665742021-02-11 11:08:44 +0100560 }
Michal Vasko9da78cf2021-07-21 13:45:10 +0200561 parse_opts = LYD_PARSE_ONLY | LYD_PARSE_OPAQ;
Michal Vaskoe0665742021-02-11 11:08:44 +0100562 val_opts = 0;
563
564 /* parse the data */
Radek Krejci7931b192020-06-25 17:05:03 +0200565 switch (format) {
566 case LYD_XML:
Michal Vaskoddd76592022-01-17 13:34:48 +0100567 rc = lyd_parse_xml(ctx, ext, parent, &first, in, parse_opts, val_opts, data_type, &envp, &parsed, NULL, &lydctx);
Michal Vasko299d5d12021-02-16 16:36:37 +0100568 if (rc && envp) {
569 /* special situation when the envelopes were parsed successfully */
570 if (tree) {
571 *tree = envp;
572 }
573 ly_set_erase(&parsed, NULL);
574 return rc;
575 }
Michal Vaskoe0665742021-02-11 11:08:44 +0100576 break;
Radek Krejci7931b192020-06-25 17:05:03 +0200577 case LYD_JSON:
Michal Vaskoddd76592022-01-17 13:34:48 +0100578 rc = lyd_parse_json(ctx, ext, parent, &first, in, parse_opts, val_opts, data_type, &parsed, NULL, &lydctx);
Michal Vaskoe0665742021-02-11 11:08:44 +0100579 break;
Radek Krejci7931b192020-06-25 17:05:03 +0200580 case LYD_LYB:
Michal Vaskoddd76592022-01-17 13:34:48 +0100581 rc = lyd_parse_lyb(ctx, ext, parent, &first, in, parse_opts, val_opts, data_type, &parsed, NULL, &lydctx);
Michal Vaskoe0665742021-02-11 11:08:44 +0100582 break;
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200583 case LYD_UNKNOWN:
Michal Vaskod74978f2021-02-12 11:59:36 +0100584 LOGARG(ctx, format);
585 rc = LY_EINVAL;
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200586 break;
Radek Krejci7931b192020-06-25 17:05:03 +0200587 }
Michal Vaskoe0665742021-02-11 11:08:44 +0100588 LY_CHECK_GOTO(rc, cleanup);
Radek Krejci7931b192020-06-25 17:05:03 +0200589
Michal Vaskoe0665742021-02-11 11:08:44 +0100590 /* set out params correctly */
591 if (tree) {
592 if (envp) {
593 /* special out param meaning */
594 *tree = envp;
595 } else {
596 *tree = parent ? NULL : first;
597 }
598 }
599 if (op) {
600 *op = lydctx->op_node;
601 }
602
603cleanup:
604 if (lydctx) {
605 lydctx->free(lydctx);
606 }
607 if (rc) {
608 if (parent) {
609 /* free all the parsed subtrees */
610 for (i = 0; i < parsed.count; ++i) {
611 lyd_free_tree(parsed.dnodes[i]);
612 }
613 } else {
614 /* free everything (cannot occur in the current code, a safety) */
615 lyd_free_all(first);
616 if (tree) {
617 *tree = NULL;
618 }
619 if (op) {
620 *op = NULL;
621 }
622 }
623 }
624 ly_set_erase(&parsed, NULL);
625 return rc;
Radek Krejcie7b95092019-05-15 11:03:07 +0200626}
Radek Krejci084289f2019-07-09 17:35:30 +0200627
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100628LIBYANG_API_DEF LY_ERR
Radek Krejcif16e2542021-02-17 15:39:23 +0100629lyd_parse_op(const struct ly_ctx *ctx, struct lyd_node *parent, struct ly_in *in, LYD_FORMAT format,
630 enum lyd_type data_type, struct lyd_node **tree, struct lyd_node **op)
631{
632 LY_CHECK_ARG_RET(ctx, ctx || parent, in, data_type, parent || tree || op, LY_EINVAL);
633
634 return lyd_parse_op_(ctx, NULL, parent, in, format, data_type, tree, op);
635}
636
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100637LIBYANG_API_DEF LY_ERR
Radek Krejcif16e2542021-02-17 15:39:23 +0100638lyd_parse_ext_op(const struct lysc_ext_instance *ext, struct lyd_node *parent, struct ly_in *in, LYD_FORMAT format,
639 enum lyd_type data_type, struct lyd_node **tree, struct lyd_node **op)
640{
641 const struct ly_ctx *ctx = ext ? ext->module->ctx : NULL;
642
643 LY_CHECK_ARG_RET(ctx, ext, in, data_type, parent || tree || op, LY_EINVAL);
644
645 return lyd_parse_op_(ctx, ext, parent, in, format, data_type, tree, op);
646}
647
Michal Vasko90932a92020-02-12 14:33:03 +0100648LY_ERR
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200649lyd_create_term(const struct lysc_node *schema, const char *value, size_t value_len, ly_bool *dynamic,
Radek Krejci8df109d2021-04-23 12:19:08 +0200650 LY_VALUE_FORMAT format, void *prefix_data, uint32_t hints, ly_bool *incomplete, struct lyd_node **node)
Michal Vasko90932a92020-02-12 14:33:03 +0100651{
652 LY_ERR ret;
653 struct lyd_node_term *term;
654
Michal Vasko9b368d32020-02-14 13:53:31 +0100655 assert(schema->nodetype & LYD_NODE_TERM);
656
Michal Vasko90932a92020-02-12 14:33:03 +0100657 term = calloc(1, sizeof *term);
658 LY_CHECK_ERR_RET(!term, LOGMEM(schema->module->ctx), LY_EMEM);
659
660 term->schema = schema;
Michal Vasko9e685082021-01-29 14:49:09 +0100661 term->prev = &term->node;
Michal Vasko9b368d32020-02-14 13:53:31 +0100662 term->flags = LYD_NEW;
Michal Vasko90932a92020-02-12 14:33:03 +0100663
Radek Krejciddace2c2021-01-08 11:30:56 +0100664 LOG_LOCSET(schema, NULL, NULL, NULL);
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200665 ret = lyd_value_store(schema->module->ctx, &term->value, ((struct lysc_node_leaf *)term->schema)->type, value,
Radek Krejci2efc45b2020-12-22 16:25:44 +0100666 value_len, dynamic, format, prefix_data, hints, schema, incomplete);
Radek Krejciddace2c2021-01-08 11:30:56 +0100667 LOG_LOCBACK(1, 0, 0, 0);
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200668 LY_CHECK_ERR_RET(ret, free(term), ret);
Michal Vasko9e685082021-01-29 14:49:09 +0100669 lyd_hash(&term->node);
Michal Vasko9b368d32020-02-14 13:53:31 +0100670
Michal Vasko9e685082021-01-29 14:49:09 +0100671 *node = &term->node;
Michal Vasko9b368d32020-02-14 13:53:31 +0100672 return ret;
673}
674
675LY_ERR
676lyd_create_term2(const struct lysc_node *schema, const struct lyd_value *val, struct lyd_node **node)
677{
678 LY_ERR ret;
679 struct lyd_node_term *term;
680 struct lysc_type *type;
681
682 assert(schema->nodetype & LYD_NODE_TERM);
Radek Krejci224d4b42021-04-23 13:54:59 +0200683 assert(val && val->realtype);
Michal Vasko9b368d32020-02-14 13:53:31 +0100684
685 term = calloc(1, sizeof *term);
686 LY_CHECK_ERR_RET(!term, LOGMEM(schema->module->ctx), LY_EMEM);
687
688 term->schema = schema;
Michal Vasko9e685082021-01-29 14:49:09 +0100689 term->prev = &term->node;
Michal Vasko9b368d32020-02-14 13:53:31 +0100690 term->flags = LYD_NEW;
691
692 type = ((struct lysc_node_leaf *)schema)->type;
693 ret = type->plugin->duplicate(schema->module->ctx, val, &term->value);
694 if (ret) {
695 LOGERR(schema->module->ctx, ret, "Value duplication failed.");
696 free(term);
697 return ret;
698 }
Michal Vasko9e685082021-01-29 14:49:09 +0100699 lyd_hash(&term->node);
Michal Vasko90932a92020-02-12 14:33:03 +0100700
Michal Vasko9e685082021-01-29 14:49:09 +0100701 *node = &term->node;
Michal Vasko90932a92020-02-12 14:33:03 +0100702 return ret;
703}
704
705LY_ERR
706lyd_create_inner(const struct lysc_node *schema, struct lyd_node **node)
707{
708 struct lyd_node_inner *in;
709
Michal Vasko9b368d32020-02-14 13:53:31 +0100710 assert(schema->nodetype & LYD_NODE_INNER);
711
Michal Vasko90932a92020-02-12 14:33:03 +0100712 in = calloc(1, sizeof *in);
713 LY_CHECK_ERR_RET(!in, LOGMEM(schema->module->ctx), LY_EMEM);
714
715 in->schema = schema;
Michal Vasko9e685082021-01-29 14:49:09 +0100716 in->prev = &in->node;
Michal Vasko9b368d32020-02-14 13:53:31 +0100717 in->flags = LYD_NEW;
Michal Vasko3383be72020-11-03 17:15:31 +0100718 if ((schema->nodetype == LYS_CONTAINER) && !(schema->flags & LYS_PRESENCE)) {
719 in->flags |= LYD_DEFAULT;
720 }
Michal Vasko90932a92020-02-12 14:33:03 +0100721
Michal Vasko9b368d32020-02-14 13:53:31 +0100722 /* do not hash list with keys, we need them for the hash */
723 if ((schema->nodetype != LYS_LIST) || (schema->flags & LYS_KEYLESS)) {
Michal Vasko9e685082021-01-29 14:49:09 +0100724 lyd_hash(&in->node);
Michal Vasko9b368d32020-02-14 13:53:31 +0100725 }
Michal Vasko90932a92020-02-12 14:33:03 +0100726
Michal Vasko9e685082021-01-29 14:49:09 +0100727 *node = &in->node;
Michal Vasko90932a92020-02-12 14:33:03 +0100728 return LY_SUCCESS;
729}
730
Michal Vasko90932a92020-02-12 14:33:03 +0100731LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +0200732lyd_create_list(const struct lysc_node *schema, const struct ly_path_predicate *predicates, struct lyd_node **node)
Michal Vasko90932a92020-02-12 14:33:03 +0100733{
734 LY_ERR ret = LY_SUCCESS;
Michal Vasko90932a92020-02-12 14:33:03 +0100735 struct lyd_node *list = NULL, *key;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200736 LY_ARRAY_COUNT_TYPE u;
Michal Vasko90932a92020-02-12 14:33:03 +0100737
Michal Vasko004d3152020-06-11 19:59:22 +0200738 assert((schema->nodetype == LYS_LIST) && !(schema->flags & LYS_KEYLESS));
Michal Vasko90932a92020-02-12 14:33:03 +0100739
740 /* create list */
741 LY_CHECK_GOTO(ret = lyd_create_inner(schema, &list), cleanup);
742
Radek Krejciddace2c2021-01-08 11:30:56 +0100743 LOG_LOCSET(NULL, list, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100744
Michal Vasko90932a92020-02-12 14:33:03 +0100745 /* create and insert all the keys */
Michal Vasko004d3152020-06-11 19:59:22 +0200746 LY_ARRAY_FOR(predicates, u) {
747 LY_CHECK_GOTO(ret = lyd_create_term2(predicates[u].key, &predicates[u].value, &key), cleanup);
Michal Vasko6ee6f432021-07-16 09:49:14 +0200748 lyd_insert_node(list, NULL, key, 0);
Michal Vasko90932a92020-02-12 14:33:03 +0100749 }
750
Michal Vasko9b368d32020-02-14 13:53:31 +0100751 /* hash having all the keys */
752 lyd_hash(list);
753
Michal Vasko90932a92020-02-12 14:33:03 +0100754 /* success */
755 *node = list;
756 list = NULL;
757
758cleanup:
Radek Krejciddace2c2021-01-08 11:30:56 +0100759 LOG_LOCBACK(0, 1, 0, 0);
Michal Vasko90932a92020-02-12 14:33:03 +0100760 lyd_free_tree(list);
Michal Vasko004d3152020-06-11 19:59:22 +0200761 return ret;
762}
763
764static LY_ERR
765lyd_create_list2(const struct lysc_node *schema, const char *keys, size_t keys_len, struct lyd_node **node)
766{
767 LY_ERR ret = LY_SUCCESS;
768 struct lyxp_expr *expr = NULL;
769 uint16_t exp_idx = 0;
770 enum ly_path_pred_type pred_type = 0;
771 struct ly_path_predicate *predicates = NULL;
772
Radek Krejciddace2c2021-01-08 11:30:56 +0100773 LOG_LOCSET(schema, NULL, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100774
Michal Vasko004d3152020-06-11 19:59:22 +0200775 /* parse keys */
Michal Vasko6b26e742020-07-17 15:02:10 +0200776 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 +0200777 LY_PATH_PRED_KEYS, &expr), cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +0200778
779 /* compile them */
Radek Krejci8df109d2021-04-23 12:19:08 +0200780 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 +0200781 NULL, &predicates, &pred_type), cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +0200782
783 /* create the list node */
784 LY_CHECK_GOTO(ret = lyd_create_list(schema, predicates, node), cleanup);
785
786cleanup:
Radek Krejciddace2c2021-01-08 11:30:56 +0100787 LOG_LOCBACK(1, 0, 0, 0);
Michal Vasko004d3152020-06-11 19:59:22 +0200788 lyxp_expr_free(schema->module->ctx, expr);
Michal Vaskof7e16e22020-10-21 09:24:39 +0200789 ly_path_predicates_free(schema->module->ctx, pred_type, predicates);
Michal Vasko90932a92020-02-12 14:33:03 +0100790 return ret;
791}
792
793LY_ERR
Michal Vasko366a4a12020-12-04 16:23:57 +0100794lyd_create_any(const struct lysc_node *schema, const void *value, LYD_ANYDATA_VALUETYPE value_type, ly_bool use_value,
795 struct lyd_node **node)
Michal Vasko90932a92020-02-12 14:33:03 +0100796{
Michal Vasko366a4a12020-12-04 16:23:57 +0100797 LY_ERR ret;
Michal Vasko90932a92020-02-12 14:33:03 +0100798 struct lyd_node_any *any;
Michal Vasko366a4a12020-12-04 16:23:57 +0100799 union lyd_any_value any_val;
Michal Vasko90932a92020-02-12 14:33:03 +0100800
Michal Vasko9b368d32020-02-14 13:53:31 +0100801 assert(schema->nodetype & LYD_NODE_ANY);
802
Michal Vasko90932a92020-02-12 14:33:03 +0100803 any = calloc(1, sizeof *any);
804 LY_CHECK_ERR_RET(!any, LOGMEM(schema->module->ctx), LY_EMEM);
805
806 any->schema = schema;
Michal Vasko9e685082021-01-29 14:49:09 +0100807 any->prev = &any->node;
Michal Vasko9b368d32020-02-14 13:53:31 +0100808 any->flags = LYD_NEW;
Michal Vasko90932a92020-02-12 14:33:03 +0100809
Radek Krejci1798aae2020-07-14 13:26:06 +0200810 /* TODO: convert XML/JSON strings into a opaq data tree */
Michal Vasko366a4a12020-12-04 16:23:57 +0100811
812 if (use_value) {
Michal Vasko742a5b12022-02-24 16:07:27 +0100813 switch (value_type) {
814 case LYD_ANYDATA_DATATREE:
815 any->value.tree = (void *)value;
816 break;
817 case LYD_ANYDATA_STRING:
818 case LYD_ANYDATA_XML:
819 case LYD_ANYDATA_JSON:
820 lydict_insert_zc(schema->module->ctx, (void *)value, &any->value.str);
821 break;
822 case LYD_ANYDATA_LYB:
823 any->value.mem = (void *)value;
824 break;
825 }
Michal Vasko366a4a12020-12-04 16:23:57 +0100826 any->value_type = value_type;
827 } else {
828 any_val.str = value;
Michal Vasko9e685082021-01-29 14:49:09 +0100829 ret = lyd_any_copy_value(&any->node, &any_val, value_type);
Michal Vasko366a4a12020-12-04 16:23:57 +0100830 LY_CHECK_ERR_RET(ret, free(any), ret);
831 }
Michal Vasko9e685082021-01-29 14:49:09 +0100832 lyd_hash(&any->node);
Michal Vasko90932a92020-02-12 14:33:03 +0100833
Michal Vasko9e685082021-01-29 14:49:09 +0100834 *node = &any->node;
Michal Vasko90932a92020-02-12 14:33:03 +0100835 return LY_SUCCESS;
836}
837
Michal Vasko52927e22020-03-16 17:26:14 +0100838LY_ERR
Michal Vasko501af032020-11-11 20:27:44 +0100839lyd_create_opaq(const struct ly_ctx *ctx, const char *name, size_t name_len, const char *prefix, size_t pref_len,
840 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 +0200841 LY_VALUE_FORMAT format, void *val_prefix_data, uint32_t hints, struct lyd_node **node)
Michal Vasko52927e22020-03-16 17:26:14 +0100842{
Radek Krejci011e4aa2020-09-04 15:22:31 +0200843 LY_ERR ret = LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +0100844 struct lyd_node_opaq *opaq;
845
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200846 assert(ctx && name && name_len && format);
Michal Vasko52927e22020-03-16 17:26:14 +0100847
Michal Vasko4cd0a602021-05-31 11:11:58 +0200848 if (!value_len && (!dynamic || !*dynamic)) {
Michal Vasko52927e22020-03-16 17:26:14 +0100849 value = "";
850 }
851
852 opaq = calloc(1, sizeof *opaq);
Michal Vasko501af032020-11-11 20:27:44 +0100853 LY_CHECK_ERR_GOTO(!opaq, LOGMEM(ctx); ret = LY_EMEM, finish);
Michal Vasko52927e22020-03-16 17:26:14 +0100854
Michal Vasko9e685082021-01-29 14:49:09 +0100855 opaq->prev = &opaq->node;
Michal Vaskoad92b672020-11-12 13:11:31 +0100856 LY_CHECK_GOTO(ret = lydict_insert(ctx, name, name_len, &opaq->name.name), finish);
Radek Krejci011e4aa2020-09-04 15:22:31 +0200857
Michal Vasko52927e22020-03-16 17:26:14 +0100858 if (pref_len) {
Michal Vaskoad92b672020-11-12 13:11:31 +0100859 LY_CHECK_GOTO(ret = lydict_insert(ctx, prefix, pref_len, &opaq->name.prefix), finish);
Michal Vasko52927e22020-03-16 17:26:14 +0100860 }
Radek Krejci1798aae2020-07-14 13:26:06 +0200861 if (module_key_len) {
Michal Vaskoad92b672020-11-12 13:11:31 +0100862 LY_CHECK_GOTO(ret = lydict_insert(ctx, module_key, module_key_len, &opaq->name.module_ns), finish);
Radek Krejci1798aae2020-07-14 13:26:06 +0200863 }
Michal Vasko52927e22020-03-16 17:26:14 +0100864 if (dynamic && *dynamic) {
Radek Krejci011e4aa2020-09-04 15:22:31 +0200865 LY_CHECK_GOTO(ret = lydict_insert_zc(ctx, (char *)value, &opaq->value), finish);
Michal Vasko52927e22020-03-16 17:26:14 +0100866 *dynamic = 0;
867 } else {
Radek Krejci011e4aa2020-09-04 15:22:31 +0200868 LY_CHECK_GOTO(ret = lydict_insert(ctx, value, value_len, &opaq->value), finish);
Michal Vasko52927e22020-03-16 17:26:14 +0100869 }
Michal Vasko501af032020-11-11 20:27:44 +0100870
871 opaq->format = format;
872 opaq->val_prefix_data = val_prefix_data;
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200873 opaq->hints = hints;
Michal Vasko52927e22020-03-16 17:26:14 +0100874 opaq->ctx = ctx;
875
Radek Krejci011e4aa2020-09-04 15:22:31 +0200876finish:
877 if (ret) {
Michal Vasko9e685082021-01-29 14:49:09 +0100878 lyd_free_tree(&opaq->node);
Michal Vasko501af032020-11-11 20:27:44 +0100879 ly_free_prefix_data(format, val_prefix_data);
Radek Krejci011e4aa2020-09-04 15:22:31 +0200880 } else {
Michal Vasko9e685082021-01-29 14:49:09 +0100881 *node = &opaq->node;
Radek Krejci011e4aa2020-09-04 15:22:31 +0200882 }
883 return ret;
Michal Vasko52927e22020-03-16 17:26:14 +0100884}
885
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100886LIBYANG_API_DEF LY_ERR
Michal Vasko65243892020-12-04 16:26:21 +0100887lyd_new_inner(struct lyd_node *parent, const struct lys_module *module, const char *name, ly_bool output,
888 struct lyd_node **node)
Michal Vasko013a8182020-03-03 10:46:53 +0100889{
890 struct lyd_node *ret = NULL;
891 const struct lysc_node *schema;
Michal Vasko892f5bf2021-11-24 10:41:05 +0100892 const struct ly_ctx *ctx = parent ? LYD_CTX(parent) : (module ? module->ctx : NULL);
Michal Vasko013a8182020-03-03 10:46:53 +0100893
Michal Vasko6027eb92020-07-15 16:37:30 +0200894 LY_CHECK_ARG_RET(ctx, parent || module, parent || node, name, LY_EINVAL);
Michal Vasko892f5bf2021-11-24 10:41:05 +0100895 LY_CHECK_CTX_EQUAL_RET(parent ? LYD_CTX(parent) : NULL, module ? module->ctx : NULL, LY_EINVAL);
Michal Vasko013a8182020-03-03 10:46:53 +0100896
Michal Vaskof03ed032020-03-04 13:31:44 +0100897 if (!module) {
898 module = parent->schema->module;
899 }
900
Michal Vasko3a41dff2020-07-15 14:30:28 +0200901 schema = lys_find_child(parent ? parent->schema : NULL, module, name, 0,
Radek Krejci41ac9942020-11-02 14:47:56 +0100902 LYS_CONTAINER | LYS_NOTIF | LYS_RPC | LYS_ACTION, output ? LYS_GETNEXT_OUTPUT : 0);
Radek Krejcidd2a7662021-03-12 11:26:34 +0100903 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 +0100904
Michal Vasko3a41dff2020-07-15 14:30:28 +0200905 LY_CHECK_RET(lyd_create_inner(schema, &ret));
906 if (parent) {
Michal Vasko6ee6f432021-07-16 09:49:14 +0200907 lyd_insert_node(parent, NULL, ret, 0);
Michal Vasko013a8182020-03-03 10:46:53 +0100908 }
Michal Vasko3a41dff2020-07-15 14:30:28 +0200909
910 if (node) {
911 *node = ret;
912 }
913 return LY_SUCCESS;
Michal Vasko013a8182020-03-03 10:46:53 +0100914}
915
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100916LIBYANG_API_DEF LY_ERR
Radek Krejcidd2a7662021-03-12 11:26:34 +0100917lyd_new_ext_inner(const struct lysc_ext_instance *ext, const char *name, struct lyd_node **node)
918{
919 struct lyd_node *ret = NULL;
920 const struct lysc_node *schema;
921 struct ly_ctx *ctx = ext ? ext->module->ctx : NULL;
922
923 LY_CHECK_ARG_RET(ctx, ext, node, name, LY_EINVAL);
924
925 schema = lysc_ext_find_node(ext, NULL, name, 0, LYS_CONTAINER | LYS_NOTIF | LYS_RPC | LYS_ACTION, 0);
926 if (!schema) {
927 if (ext->argument) {
928 LOGERR(ctx, LY_EINVAL, "Inner node (not a list) \"%s\" not found in instance \"%s\" of extension %s.",
929 name, ext->argument, ext->def->name);
930 } else {
931 LOGERR(ctx, LY_EINVAL, "Inner node (not a list) \"%s\" not found in instance of extension %s.",
932 name, ext->def->name);
933 }
934 return LY_ENOTFOUND;
935 }
936 LY_CHECK_RET(lyd_create_inner(schema, &ret));
937
938 *node = ret;
939
940 return LY_SUCCESS;
941}
942
Michal Vasko208e6d62021-06-28 11:23:50 +0200943/**
944 * @brief Create a new list node in the data tree.
945 *
946 * @param[in] parent Parent node for the node being created. NULL in case of creating a top level element.
947 * @param[in] module Module of the node being created. If NULL, @p parent module will be used.
948 * @param[in] name Schema node name of the new data node. The node must be #LYS_LIST.
949 * @param[in] format Format of key values.
950 * @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
951 * taken into consideration. Otherwise, the output's data node is going to be created.
952 * @param[out] node Optional created node.
953 * @param[in] ap Ordered key values of the new list instance, all must be set. For ::LY_VALUE_LYB, every value must
954 * be followed by the value length.
955 * @return LY_ERR value.
956 */
957static LY_ERR
958_lyd_new_list(struct lyd_node *parent, const struct lys_module *module, const char *name, LY_VALUE_FORMAT format,
959 ly_bool output, struct lyd_node **node, va_list ap)
Michal Vasko013a8182020-03-03 10:46:53 +0100960{
961 struct lyd_node *ret = NULL, *key;
962 const struct lysc_node *schema, *key_s;
Michal Vasko892f5bf2021-11-24 10:41:05 +0100963 const struct ly_ctx *ctx = parent ? LYD_CTX(parent) : (module ? module->ctx : NULL);
Michal Vasko208e6d62021-06-28 11:23:50 +0200964 const void *key_val;
965 uint32_t key_len;
Michal Vasko013a8182020-03-03 10:46:53 +0100966 LY_ERR rc = LY_SUCCESS;
967
Michal Vasko6027eb92020-07-15 16:37:30 +0200968 LY_CHECK_ARG_RET(ctx, parent || module, parent || node, name, LY_EINVAL);
Michal Vasko892f5bf2021-11-24 10:41:05 +0100969 LY_CHECK_CTX_EQUAL_RET(parent ? LYD_CTX(parent) : NULL, module ? module->ctx : NULL, LY_EINVAL);
Michal Vasko013a8182020-03-03 10:46:53 +0100970
Michal Vaskof03ed032020-03-04 13:31:44 +0100971 if (!module) {
972 module = parent->schema->module;
973 }
974
Radek Krejci41ac9942020-11-02 14:47:56 +0100975 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 +0200976 LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "List node \"%s\" not found.", name), LY_ENOTFOUND);
Michal Vasko013a8182020-03-03 10:46:53 +0100977
978 /* create list inner node */
Michal Vasko3a41dff2020-07-15 14:30:28 +0200979 LY_CHECK_RET(lyd_create_inner(schema, &ret));
Michal Vasko013a8182020-03-03 10:46:53 +0100980
Michal Vasko013a8182020-03-03 10:46:53 +0100981 /* create and insert all the keys */
Michal Vasko544e58a2021-01-28 14:33:41 +0100982 for (key_s = lysc_node_child(schema); key_s && (key_s->flags & LYS_KEY); key_s = key_s->next) {
Michal Vasko208e6d62021-06-28 11:23:50 +0200983 if (format == LY_VALUE_LYB) {
984 key_val = va_arg(ap, const void *);
985 key_len = va_arg(ap, uint32_t);
986 } else {
987 key_val = va_arg(ap, const char *);
988 key_len = key_val ? strlen((char *)key_val) : 0;
989 }
Michal Vasko013a8182020-03-03 10:46:53 +0100990
Michal Vasko208e6d62021-06-28 11:23:50 +0200991 rc = lyd_create_term(key_s, key_val, key_len, NULL, format, NULL, LYD_HINT_DATA, NULL, &key);
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200992 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko6ee6f432021-07-16 09:49:14 +0200993 lyd_insert_node(ret, NULL, key, 1);
Michal Vasko013a8182020-03-03 10:46:53 +0100994 }
995
Michal Vasko013a8182020-03-03 10:46:53 +0100996 if (parent) {
Michal Vasko6ee6f432021-07-16 09:49:14 +0200997 lyd_insert_node(parent, NULL, ret, 0);
Michal Vasko013a8182020-03-03 10:46:53 +0100998 }
999
1000cleanup:
1001 if (rc) {
1002 lyd_free_tree(ret);
1003 ret = NULL;
Michal Vasko3a41dff2020-07-15 14:30:28 +02001004 } else if (node) {
1005 *node = ret;
Michal Vasko013a8182020-03-03 10:46:53 +01001006 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02001007 return rc;
Michal Vasko013a8182020-03-03 10:46:53 +01001008}
1009
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001010LIBYANG_API_DEF LY_ERR
Michal Vasko208e6d62021-06-28 11:23:50 +02001011lyd_new_list(struct lyd_node *parent, const struct lys_module *module, const char *name, ly_bool output,
1012 struct lyd_node **node, ...)
1013{
1014 LY_ERR rc;
1015 va_list ap;
1016
1017 va_start(ap, node);
1018
1019 rc = _lyd_new_list(parent, module, name, LY_VALUE_JSON, output, node, ap);
1020
1021 va_end(ap);
1022 return rc;
1023}
1024
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001025LIBYANG_API_DEF LY_ERR
Michal Vasko208e6d62021-06-28 11:23:50 +02001026lyd_new_list_bin(struct lyd_node *parent, const struct lys_module *module, const char *name, ly_bool output,
1027 struct lyd_node **node, ...)
1028{
1029 LY_ERR rc;
1030 va_list ap;
1031
1032 va_start(ap, node);
1033
1034 rc = _lyd_new_list(parent, module, name, LY_VALUE_LYB, output, node, ap);
1035
1036 va_end(ap);
1037 return rc;
1038}
1039
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001040LIBYANG_API_DEF LY_ERR
Michal Vasko208e6d62021-06-28 11:23:50 +02001041lyd_new_list_canon(struct lyd_node *parent, const struct lys_module *module, const char *name, ly_bool output,
1042 struct lyd_node **node, ...)
1043{
1044 LY_ERR rc;
1045 va_list ap;
1046
1047 va_start(ap, node);
1048
1049 rc = _lyd_new_list(parent, module, name, LY_VALUE_CANON, output, node, ap);
1050
1051 va_end(ap);
1052 return rc;
1053}
1054
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001055LIBYANG_API_DEF LY_ERR
Radek Krejci8247bae2021-03-12 11:47:02 +01001056lyd_new_ext_list(const struct lysc_ext_instance *ext, const char *name, struct lyd_node **node, ...)
1057{
1058 struct lyd_node *ret = NULL, *key;
1059 const struct lysc_node *schema, *key_s;
1060 struct ly_ctx *ctx = ext ? ext->module->ctx : NULL;
1061 va_list ap;
1062 const char *key_val;
1063 LY_ERR rc = LY_SUCCESS;
1064
1065 LY_CHECK_ARG_RET(ctx, ext, node, name, LY_EINVAL);
1066
1067 schema = lysc_ext_find_node(ext, NULL, name, 0, LYS_LIST, 0);
1068 if (!schema) {
1069 if (ext->argument) {
1070 LOGERR(ctx, LY_EINVAL, "List node \"%s\" not found in instance \"%s\" of extension %s.",
1071 name, ext->argument, ext->def->name);
1072 } else {
1073 LOGERR(ctx, LY_EINVAL, "List node \"%s\" not found in instance of extension %s.", name, ext->def->name);
1074 }
1075 return LY_ENOTFOUND;
1076 }
1077 /* create list inner node */
1078 LY_CHECK_RET(lyd_create_inner(schema, &ret));
1079
1080 va_start(ap, node);
1081
1082 /* create and insert all the keys */
1083 for (key_s = lysc_node_child(schema); key_s && (key_s->flags & LYS_KEY); key_s = key_s->next) {
1084 key_val = va_arg(ap, const char *);
1085
Radek Krejci8df109d2021-04-23 12:19:08 +02001086 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 +01001087 NULL, &key);
1088 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko6ee6f432021-07-16 09:49:14 +02001089 lyd_insert_node(ret, NULL, key, 1);
Radek Krejci8247bae2021-03-12 11:47:02 +01001090 }
1091
1092cleanup:
1093 va_end(ap);
1094 if (rc) {
1095 lyd_free_tree(ret);
1096 ret = NULL;
Radek Krejci8247bae2021-03-12 11:47:02 +01001097 }
Radek Krejci8919fbd2021-03-15 09:35:25 +01001098 *node = ret;
Radek Krejci8247bae2021-03-12 11:47:02 +01001099 return rc;
1100}
1101
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001102LIBYANG_API_DEF LY_ERR
Michal Vasko3a41dff2020-07-15 14:30:28 +02001103lyd_new_list2(struct lyd_node *parent, const struct lys_module *module, const char *name, const char *keys,
Radek Krejci41ac9942020-11-02 14:47:56 +01001104 ly_bool output, struct lyd_node **node)
Michal Vasko013a8182020-03-03 10:46:53 +01001105{
1106 struct lyd_node *ret = NULL;
1107 const struct lysc_node *schema;
Michal Vasko892f5bf2021-11-24 10:41:05 +01001108 const struct ly_ctx *ctx = parent ? LYD_CTX(parent) : (module ? module->ctx : NULL);
Michal Vasko013a8182020-03-03 10:46:53 +01001109
Michal Vasko6027eb92020-07-15 16:37:30 +02001110 LY_CHECK_ARG_RET(ctx, parent || module, parent || node, name, LY_EINVAL);
Michal Vasko892f5bf2021-11-24 10:41:05 +01001111 LY_CHECK_CTX_EQUAL_RET(parent ? LYD_CTX(parent) : NULL, module ? module->ctx : NULL, LY_EINVAL);
Michal Vasko013a8182020-03-03 10:46:53 +01001112
Michal Vaskof03ed032020-03-04 13:31:44 +01001113 if (!module) {
1114 module = parent->schema->module;
1115 }
Michal Vasko004d3152020-06-11 19:59:22 +02001116 if (!keys) {
1117 keys = "";
1118 }
Michal Vaskof03ed032020-03-04 13:31:44 +01001119
Michal Vasko004d3152020-06-11 19:59:22 +02001120 /* find schema node */
Radek Krejci41ac9942020-11-02 14:47:56 +01001121 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 +02001122 LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "List node \"%s\" not found.", name), LY_ENOTFOUND);
Michal Vasko013a8182020-03-03 10:46:53 +01001123
Michal Vasko004d3152020-06-11 19:59:22 +02001124 if ((schema->flags & LYS_KEYLESS) && !keys[0]) {
1125 /* key-less list */
Michal Vasko3a41dff2020-07-15 14:30:28 +02001126 LY_CHECK_RET(lyd_create_inner(schema, &ret));
Michal Vasko004d3152020-06-11 19:59:22 +02001127 } else {
1128 /* create the list node */
Michal Vasko3a41dff2020-07-15 14:30:28 +02001129 LY_CHECK_RET(lyd_create_list2(schema, keys, strlen(keys), &ret));
Michal Vasko004d3152020-06-11 19:59:22 +02001130 }
Michal Vasko004d3152020-06-11 19:59:22 +02001131 if (parent) {
Michal Vasko6ee6f432021-07-16 09:49:14 +02001132 lyd_insert_node(parent, NULL, ret, 0);
Michal Vasko013a8182020-03-03 10:46:53 +01001133 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02001134
1135 if (node) {
1136 *node = ret;
1137 }
1138 return LY_SUCCESS;
Michal Vasko013a8182020-03-03 10:46:53 +01001139}
1140
Radek Krejci09c77442021-04-26 11:10:34 +02001141/**
1142 * @brief Create a new term node in the data tree.
1143 *
1144 * @param[in] parent Parent node for the node being created. NULL in case of creating a top level element.
1145 * @param[in] module Module of the node being created. If NULL, @p parent module will be used.
1146 * @param[in] name Schema node name of the new data node. The node can be ::LYS_LEAF or ::LYS_LEAFLIST.
1147 * @param[in] value Value of the node being created.
1148 * @param[in] value_len Length of @p value.
1149 * @param[in] format Format of @p value.
1150 * @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
1151 * taken into consideration. Otherwise, the output's data node is going to be created.
1152 * @param[out] node Optional created node.
1153 * @return LY_ERR value.
1154 */
1155static LY_ERR
1156_lyd_new_term(struct lyd_node *parent, const struct lys_module *module, const char *name, const void *value,
1157 size_t value_len, LY_VALUE_FORMAT format, ly_bool output, struct lyd_node **node)
Michal Vasko013a8182020-03-03 10:46:53 +01001158{
Michal Vaskocbff3e92020-05-27 12:56:41 +02001159 LY_ERR rc;
Michal Vasko013a8182020-03-03 10:46:53 +01001160 struct lyd_node *ret = NULL;
1161 const struct lysc_node *schema;
Radek Krejci09c77442021-04-26 11:10:34 +02001162 const struct ly_ctx *ctx = parent ? LYD_CTX(parent) : (module ? module->ctx : NULL);
Michal Vasko013a8182020-03-03 10:46:53 +01001163
Michal Vasko6027eb92020-07-15 16:37:30 +02001164 LY_CHECK_ARG_RET(ctx, parent || module, parent || node, name, LY_EINVAL);
Michal Vasko892f5bf2021-11-24 10:41:05 +01001165 LY_CHECK_CTX_EQUAL_RET(parent ? LYD_CTX(parent) : NULL, module ? module->ctx : NULL, LY_EINVAL);
Michal Vasko013a8182020-03-03 10:46:53 +01001166
Michal Vaskof03ed032020-03-04 13:31:44 +01001167 if (!module) {
1168 module = parent->schema->module;
1169 }
1170
Radek Krejci41ac9942020-11-02 14:47:56 +01001171 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 +02001172 LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "Term node \"%s\" not found.", name), LY_ENOTFOUND);
Michal Vasko013a8182020-03-03 10:46:53 +01001173
Radek Krejci09c77442021-04-26 11:10:34 +02001174 rc = lyd_create_term(schema, value, value_len, NULL, format, NULL, LYD_HINT_DATA, NULL, &ret);
Michal Vaskofeca4fb2020-10-05 08:58:40 +02001175 LY_CHECK_RET(rc);
Michal Vaskocbff3e92020-05-27 12:56:41 +02001176 if (parent) {
Michal Vasko6ee6f432021-07-16 09:49:14 +02001177 lyd_insert_node(parent, NULL, ret, 0);
Michal Vasko013a8182020-03-03 10:46:53 +01001178 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02001179
1180 if (node) {
1181 *node = ret;
1182 }
1183 return LY_SUCCESS;
Michal Vasko013a8182020-03-03 10:46:53 +01001184}
1185
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001186LIBYANG_API_DEF LY_ERR
Radek Krejci09c77442021-04-26 11:10:34 +02001187lyd_new_term(struct lyd_node *parent, const struct lys_module *module, const char *name, const char *val_str,
1188 ly_bool output, struct lyd_node **node)
1189{
1190 return _lyd_new_term(parent, module, name, val_str, val_str ? strlen(val_str) : 0, LY_VALUE_JSON, output, node);
1191}
1192
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001193LIBYANG_API_DEF LY_ERR
Radek Krejci09c77442021-04-26 11:10:34 +02001194lyd_new_term_bin(struct lyd_node *parent, const struct lys_module *module, const char *name, const void *value,
1195 size_t value_len, ly_bool output, struct lyd_node **node)
1196{
1197 return _lyd_new_term(parent, module, name, value, value_len, LY_VALUE_LYB, output, node);
1198}
1199
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001200LIBYANG_API_DEF LY_ERR
Radek Krejci09c77442021-04-26 11:10:34 +02001201lyd_new_term_canon(struct lyd_node *parent, const struct lys_module *module, const char *name, const char *val_str,
1202 ly_bool output, struct lyd_node **node)
1203{
1204 return _lyd_new_term(parent, module, name, val_str, val_str ? strlen(val_str) : 0, LY_VALUE_CANON, output, node);
1205}
1206
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001207LIBYANG_API_DEF LY_ERR
Radek Krejci8a5afc22021-03-12 11:10:47 +01001208lyd_new_ext_term(const struct lysc_ext_instance *ext, const char *name, const char *val_str, struct lyd_node **node)
1209{
1210 LY_ERR rc;
1211 struct lyd_node *ret = NULL;
1212 const struct lysc_node *schema;
1213 struct ly_ctx *ctx = ext ? ext->module->ctx : NULL;
1214
1215 LY_CHECK_ARG_RET(ctx, ext, node, name, LY_EINVAL);
1216
1217 schema = lysc_ext_find_node(ext, NULL, name, 0, LYD_NODE_TERM, 0);
1218 if (!schema) {
1219 if (ext->argument) {
1220 LOGERR(ctx, LY_EINVAL, "Term node \"%s\" not found in instance \"%s\" of extension %s.",
1221 name, ext->argument, ext->def->name);
1222 } else {
1223 LOGERR(ctx, LY_EINVAL, "Term node \"%s\" not found in instance of extension %s.", name, ext->def->name);
1224 }
1225 return LY_ENOTFOUND;
1226 }
Radek Krejci8df109d2021-04-23 12:19:08 +02001227 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 +01001228 LY_CHECK_RET(rc);
1229
1230 *node = ret;
1231
1232 return LY_SUCCESS;
1233}
1234
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001235LIBYANG_API_DEF LY_ERR
Michal Vasko2a4ab2b2021-03-04 15:52:40 +01001236lyd_new_any(struct lyd_node *parent, const struct lys_module *module, const char *name, const void *value,
1237 ly_bool use_value, LYD_ANYDATA_VALUETYPE value_type, ly_bool output, struct lyd_node **node)
Michal Vasko013a8182020-03-03 10:46:53 +01001238{
1239 struct lyd_node *ret = NULL;
1240 const struct lysc_node *schema;
Michal Vasko892f5bf2021-11-24 10:41:05 +01001241 const struct ly_ctx *ctx = parent ? LYD_CTX(parent) : (module ? module->ctx : NULL);
Michal Vasko013a8182020-03-03 10:46:53 +01001242
Michal Vasko6027eb92020-07-15 16:37:30 +02001243 LY_CHECK_ARG_RET(ctx, parent || module, parent || node, name, LY_EINVAL);
Michal Vasko892f5bf2021-11-24 10:41:05 +01001244 LY_CHECK_CTX_EQUAL_RET(parent ? LYD_CTX(parent) : NULL, module ? module->ctx : NULL, LY_EINVAL);
Michal Vasko013a8182020-03-03 10:46:53 +01001245
Michal Vaskof03ed032020-03-04 13:31:44 +01001246 if (!module) {
1247 module = parent->schema->module;
1248 }
1249
Radek Krejci41ac9942020-11-02 14:47:56 +01001250 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 +02001251 LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "Any node \"%s\" not found.", name), LY_ENOTFOUND);
Michal Vasko013a8182020-03-03 10:46:53 +01001252
Michal Vasko2a4ab2b2021-03-04 15:52:40 +01001253 LY_CHECK_RET(lyd_create_any(schema, value, value_type, use_value, &ret));
Michal Vasko3a41dff2020-07-15 14:30:28 +02001254 if (parent) {
Michal Vasko6ee6f432021-07-16 09:49:14 +02001255 lyd_insert_node(parent, NULL, ret, 0);
Michal Vasko013a8182020-03-03 10:46:53 +01001256 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02001257
1258 if (node) {
1259 *node = ret;
1260 }
1261 return LY_SUCCESS;
Michal Vasko013a8182020-03-03 10:46:53 +01001262}
1263
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001264LIBYANG_API_DEF LY_ERR
Radek Krejci0b963da2021-03-12 13:23:44 +01001265lyd_new_ext_any(const struct lysc_ext_instance *ext, const char *name, const void *value, ly_bool use_value,
1266 LYD_ANYDATA_VALUETYPE value_type, struct lyd_node **node)
1267{
1268 struct lyd_node *ret = NULL;
1269 const struct lysc_node *schema;
1270 struct ly_ctx *ctx = ext ? ext->module->ctx : NULL;
1271
1272 LY_CHECK_ARG_RET(ctx, ext, node, name, LY_EINVAL);
1273
1274 schema = lysc_ext_find_node(ext, NULL, name, 0, LYD_NODE_ANY, 0);
1275 if (!schema) {
1276 if (ext->argument) {
1277 LOGERR(ctx, LY_EINVAL, "Any node \"%s\" not found in instance \"%s\" of extension %s.",
1278 name, ext->argument, ext->def->name);
1279 } else {
1280 LOGERR(ctx, LY_EINVAL, "Any node \"%s\" not found in instance of extension %s.", name, ext->def->name);
1281 }
1282 return LY_ENOTFOUND;
1283 }
1284 LY_CHECK_RET(lyd_create_any(schema, value, value_type, use_value, &ret));
1285
1286 *node = ret;
1287
1288 return LY_SUCCESS;
1289}
1290
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001291LIBYANG_API_DEF LY_ERR
Michal Vasko871a0252020-11-11 18:35:24 +01001292lyd_new_meta(const struct ly_ctx *ctx, struct lyd_node *parent, const struct lys_module *module, const char *name,
1293 const char *val_str, ly_bool clear_dflt, struct lyd_meta **meta)
Michal Vaskod86997b2020-05-26 15:19:54 +02001294{
Michal Vaskod86997b2020-05-26 15:19:54 +02001295 const char *prefix, *tmp;
Michal Vaskod86997b2020-05-26 15:19:54 +02001296 size_t pref_len, name_len;
1297
Michal Vasko2b5344c2021-02-26 10:12:05 +01001298 LY_CHECK_ARG_RET(ctx, ctx || parent, name, module || strchr(name, ':'), parent || meta, LY_EINVAL);
Michal Vasko892f5bf2021-11-24 10:41:05 +01001299 LY_CHECK_CTX_EQUAL_RET(ctx, parent ? LYD_CTX(parent) : NULL, module ? module->ctx : NULL, LY_EINVAL);
Michal Vasko2b5344c2021-02-26 10:12:05 +01001300 if (!ctx) {
1301 ctx = LYD_CTX(parent);
1302 }
Michal Vasko00cbf532020-06-15 13:58:47 +02001303
Michal Vasko871a0252020-11-11 18:35:24 +01001304 if (parent && !parent->schema) {
Michal Vasko33b4fab2021-03-04 15:56:12 +01001305 LOGERR(ctx, LY_EINVAL, "Cannot add metadata \"%s\" to an opaque node \"%s\".", name, LYD_NAME(parent));
Michal Vasko871a0252020-11-11 18:35:24 +01001306 return LY_EINVAL;
1307 }
Michal Vasko610553d2020-11-18 18:15:05 +01001308 if (meta) {
1309 *meta = NULL;
1310 }
Michal Vaskod86997b2020-05-26 15:19:54 +02001311
1312 /* parse the name */
1313 tmp = name;
1314 if (ly_parse_nodeid(&tmp, &prefix, &pref_len, &name, &name_len) || tmp[0]) {
1315 LOGERR(ctx, LY_EINVAL, "Metadata name \"%s\" is not valid.", name);
Michal Vasko871a0252020-11-11 18:35:24 +01001316 return LY_EINVAL;
Michal Vaskod86997b2020-05-26 15:19:54 +02001317 }
1318
1319 /* find the module */
1320 if (prefix) {
Radek Krejci0ad51f12020-07-16 12:08:12 +02001321 module = ly_ctx_get_module_implemented2(ctx, prefix, pref_len);
Radek Krejci422afb12021-03-04 16:38:16 +01001322 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 +02001323 }
1324
1325 /* set value if none */
1326 if (!val_str) {
1327 val_str = "";
1328 }
1329
Radek Krejci8df109d2021-04-23 12:19:08 +02001330 return lyd_create_meta(parent, meta, module, name, name_len, val_str, strlen(val_str), NULL, LY_VALUE_JSON,
Michal Vaskoddd76592022-01-17 13:34:48 +01001331 NULL, LYD_HINT_DATA, parent ? parent->schema : NULL, clear_dflt, NULL);
Michal Vasko871a0252020-11-11 18:35:24 +01001332}
Michal Vasko3a41dff2020-07-15 14:30:28 +02001333
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001334LIBYANG_API_DEF LY_ERR
Michal Vaskoba696702020-11-11 19:12:45 +01001335lyd_new_meta2(const struct ly_ctx *ctx, struct lyd_node *parent, ly_bool clear_dflt, const struct lyd_attr *attr,
1336 struct lyd_meta **meta)
1337{
1338 const struct lys_module *mod;
1339
1340 LY_CHECK_ARG_RET(NULL, ctx, attr, parent || meta, LY_EINVAL);
Michal Vasko892f5bf2021-11-24 10:41:05 +01001341 LY_CHECK_CTX_EQUAL_RET(ctx, parent ? LYD_CTX(parent) : NULL, LY_EINVAL);
Michal Vaskoba696702020-11-11 19:12:45 +01001342
1343 if (parent && !parent->schema) {
1344 LOGERR(ctx, LY_EINVAL, "Cannot add metadata to an opaque node \"%s\".", ((struct lyd_node_opaq *)parent)->name);
1345 return LY_EINVAL;
Michal Vasko3a41dff2020-07-15 14:30:28 +02001346 }
Michal Vasko610553d2020-11-18 18:15:05 +01001347 if (meta) {
1348 *meta = NULL;
1349 }
Michal Vaskoba696702020-11-11 19:12:45 +01001350
1351 switch (attr->format) {
Radek Krejci8df109d2021-04-23 12:19:08 +02001352 case LY_VALUE_XML:
Michal Vaskoad92b672020-11-12 13:11:31 +01001353 mod = ly_ctx_get_module_implemented_ns(ctx, attr->name.module_ns);
Michal Vaskoba696702020-11-11 19:12:45 +01001354 if (!mod) {
Michal Vaskoad92b672020-11-12 13:11:31 +01001355 LOGERR(ctx, LY_EINVAL, "Module with namespace \"%s\" not found.", attr->name.module_ns);
Michal Vaskoba696702020-11-11 19:12:45 +01001356 return LY_ENOTFOUND;
1357 }
1358 break;
Radek Krejci8df109d2021-04-23 12:19:08 +02001359 case LY_VALUE_JSON:
Michal Vaskoad92b672020-11-12 13:11:31 +01001360 mod = ly_ctx_get_module_implemented(ctx, attr->name.module_name);
Michal Vaskoba696702020-11-11 19:12:45 +01001361 if (!mod) {
Michal Vaskoad92b672020-11-12 13:11:31 +01001362 LOGERR(ctx, LY_EINVAL, "Module \"%s\" not found.", attr->name.module_name);
Michal Vaskoba696702020-11-11 19:12:45 +01001363 return LY_ENOTFOUND;
1364 }
1365 break;
1366 default:
1367 LOGINT_RET(ctx);
1368 }
1369
Michal Vaskoad92b672020-11-12 13:11:31 +01001370 return lyd_create_meta(parent, meta, mod, attr->name.name, strlen(attr->name.name), attr->value, strlen(attr->value),
Michal Vaskoddd76592022-01-17 13:34:48 +01001371 NULL, attr->format, attr->val_prefix_data, attr->hints, parent ? parent->schema : NULL, clear_dflt, NULL);
Michal Vaskod86997b2020-05-26 15:19:54 +02001372}
1373
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001374LIBYANG_API_DEF LY_ERR
Michal Vasko00cbf532020-06-15 13:58:47 +02001375lyd_new_opaq(struct lyd_node *parent, const struct ly_ctx *ctx, const char *name, const char *value,
Michal Vasko0ab974d2021-02-24 13:18:26 +01001376 const char *prefix, const char *module_name, struct lyd_node **node)
Michal Vasko00cbf532020-06-15 13:58:47 +02001377{
1378 struct lyd_node *ret = NULL;
Radek Krejci07a55962021-03-02 20:16:43 +01001379
Michal Vasko0ab974d2021-02-24 13:18:26 +01001380 LY_CHECK_ARG_RET(ctx, parent || ctx, parent || node, name, module_name, !prefix || !strcmp(prefix, module_name), LY_EINVAL);
Michal Vasko892f5bf2021-11-24 10:41:05 +01001381 LY_CHECK_CTX_EQUAL_RET(ctx, parent ? LYD_CTX(parent) : NULL, LY_EINVAL);
Michal Vasko00cbf532020-06-15 13:58:47 +02001382
1383 if (!ctx) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02001384 ctx = LYD_CTX(parent);
Michal Vasko00cbf532020-06-15 13:58:47 +02001385 }
1386 if (!value) {
1387 value = "";
1388 }
1389
Michal Vasko0ab974d2021-02-24 13:18:26 +01001390 LY_CHECK_RET(lyd_create_opaq(ctx, name, strlen(name), prefix, prefix ? strlen(prefix) : 0, module_name,
Radek Krejci8df109d2021-04-23 12:19:08 +02001391 strlen(module_name), value, strlen(value), NULL, LY_VALUE_JSON, NULL, 0, &ret));
Michal Vasko3a41dff2020-07-15 14:30:28 +02001392 if (parent) {
Michal Vasko6ee6f432021-07-16 09:49:14 +02001393 lyd_insert_node(parent, NULL, ret, 1);
Michal Vasko00cbf532020-06-15 13:58:47 +02001394 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02001395
1396 if (node) {
1397 *node = ret;
1398 }
1399 return LY_SUCCESS;
Michal Vasko00cbf532020-06-15 13:58:47 +02001400}
1401
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001402LIBYANG_API_DEF LY_ERR
Michal Vasko8d65f852021-02-17 11:28:13 +01001403lyd_new_opaq2(struct lyd_node *parent, const struct ly_ctx *ctx, const char *name, const char *value,
Michal Vasko0ab974d2021-02-24 13:18:26 +01001404 const char *prefix, const char *module_ns, struct lyd_node **node)
Michal Vasko8d65f852021-02-17 11:28:13 +01001405{
1406 struct lyd_node *ret = NULL;
1407
1408 LY_CHECK_ARG_RET(ctx, parent || ctx, parent || node, name, module_ns, LY_EINVAL);
Michal Vasko892f5bf2021-11-24 10:41:05 +01001409 LY_CHECK_CTX_EQUAL_RET(ctx, parent ? LYD_CTX(parent) : NULL, LY_EINVAL);
Michal Vasko8d65f852021-02-17 11:28:13 +01001410
1411 if (!ctx) {
1412 ctx = LYD_CTX(parent);
1413 }
1414 if (!value) {
1415 value = "";
1416 }
1417
Michal Vasko0ab974d2021-02-24 13:18:26 +01001418 LY_CHECK_RET(lyd_create_opaq(ctx, name, strlen(name), prefix, prefix ? strlen(prefix) : 0, module_ns,
Radek Krejci8df109d2021-04-23 12:19:08 +02001419 strlen(module_ns), value, strlen(value), NULL, LY_VALUE_XML, NULL, 0, &ret));
Michal Vasko8d65f852021-02-17 11:28:13 +01001420 if (parent) {
Michal Vasko6ee6f432021-07-16 09:49:14 +02001421 lyd_insert_node(parent, NULL, ret, 1);
Michal Vasko8d65f852021-02-17 11:28:13 +01001422 }
1423
1424 if (node) {
1425 *node = ret;
1426 }
1427 return LY_SUCCESS;
1428}
1429
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001430LIBYANG_API_DEF LY_ERR
Michal Vasko8d65f852021-02-17 11:28:13 +01001431lyd_new_attr(struct lyd_node *parent, const char *module_name, const char *name, const char *value,
Radek Krejci0f969882020-08-21 16:56:47 +02001432 struct lyd_attr **attr)
Michal Vasko00cbf532020-06-15 13:58:47 +02001433{
Radek Krejci1798aae2020-07-14 13:26:06 +02001434 struct lyd_attr *ret = NULL;
Michal Vasko00cbf532020-06-15 13:58:47 +02001435 const struct ly_ctx *ctx;
1436 const char *prefix, *tmp;
Michal Vasko51d21b82020-11-13 18:03:54 +01001437 size_t pref_len, name_len, mod_len;
Michal Vasko00cbf532020-06-15 13:58:47 +02001438
Michal Vasko3a41dff2020-07-15 14:30:28 +02001439 LY_CHECK_ARG_RET(NULL, parent, !parent->schema, name, LY_EINVAL);
Michal Vasko00cbf532020-06-15 13:58:47 +02001440
Michal Vaskob7be7a82020-08-20 09:09:04 +02001441 ctx = LYD_CTX(parent);
Michal Vasko00cbf532020-06-15 13:58:47 +02001442
1443 /* parse the name */
1444 tmp = name;
1445 if (ly_parse_nodeid(&tmp, &prefix, &pref_len, &name, &name_len) || tmp[0]) {
Michal Vaskob69b68c2021-02-17 11:18:16 +01001446 LOGERR(ctx, LY_EINVAL, "Attribute name \"%s\" is not valid.", name);
Michal Vasko3a41dff2020-07-15 14:30:28 +02001447 return LY_EVALID;
Michal Vasko00cbf532020-06-15 13:58:47 +02001448 }
1449
Michal Vaskoe137fc42021-07-22 11:53:13 +02001450 if ((pref_len == 3) && !strncmp(prefix, "xml", 3)) {
1451 /* not a prefix but special name */
1452 name = prefix;
1453 name_len += 1 + pref_len;
1454 prefix = NULL;
1455 pref_len = 0;
1456 }
1457
Michal Vasko51d21b82020-11-13 18:03:54 +01001458 /* get the module */
1459 if (module_name) {
1460 mod_len = strlen(module_name);
1461 } else {
1462 module_name = prefix;
1463 mod_len = pref_len;
1464 }
1465
Michal Vasko00cbf532020-06-15 13:58:47 +02001466 /* set value if none */
Michal Vasko8d65f852021-02-17 11:28:13 +01001467 if (!value) {
1468 value = "";
Michal Vasko00cbf532020-06-15 13:58:47 +02001469 }
1470
Michal Vasko8d65f852021-02-17 11:28:13 +01001471 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 +02001472 strlen(value), NULL, LY_VALUE_JSON, NULL, LYD_HINT_DATA));
Michal Vasko8d65f852021-02-17 11:28:13 +01001473
1474 if (attr) {
1475 *attr = ret;
1476 }
1477 return LY_SUCCESS;
1478}
1479
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001480LIBYANG_API_DEF LY_ERR
Michal Vasko8d65f852021-02-17 11:28:13 +01001481lyd_new_attr2(struct lyd_node *parent, const char *module_ns, const char *name, const char *value,
1482 struct lyd_attr **attr)
1483{
1484 struct lyd_attr *ret = NULL;
1485 const struct ly_ctx *ctx;
1486 const char *prefix, *tmp;
1487 size_t pref_len, name_len;
1488
1489 LY_CHECK_ARG_RET(NULL, parent, !parent->schema, name, LY_EINVAL);
1490
1491 ctx = LYD_CTX(parent);
1492
1493 /* parse the name */
1494 tmp = name;
1495 if (ly_parse_nodeid(&tmp, &prefix, &pref_len, &name, &name_len) || tmp[0]) {
1496 LOGERR(ctx, LY_EINVAL, "Attribute name \"%s\" is not valid.", name);
1497 return LY_EVALID;
1498 }
1499
Michal Vaskoe137fc42021-07-22 11:53:13 +02001500 if ((pref_len == 3) && !strncmp(prefix, "xml", 3)) {
1501 /* not a prefix but special name */
1502 name = prefix;
1503 name_len += 1 + pref_len;
1504 prefix = NULL;
1505 pref_len = 0;
1506 }
1507
Michal Vasko8d65f852021-02-17 11:28:13 +01001508 /* set value if none */
1509 if (!value) {
1510 value = "";
1511 }
1512
1513 LY_CHECK_RET(lyd_create_attr(parent, &ret, ctx, name, name_len, prefix, pref_len, module_ns,
Radek Krejci8df109d2021-04-23 12:19:08 +02001514 module_ns ? strlen(module_ns) : 0, value, strlen(value), NULL, LY_VALUE_XML, NULL, LYD_HINT_DATA));
Michal Vasko3a41dff2020-07-15 14:30:28 +02001515
1516 if (attr) {
1517 *attr = ret;
1518 }
1519 return LY_SUCCESS;
Michal Vasko00cbf532020-06-15 13:58:47 +02001520}
1521
Radek Krejci09c77442021-04-26 11:10:34 +02001522/**
1523 * @brief Change the value of a term (leaf or leaf-list) node.
1524 *
1525 * Node changed this way is always considered explicitly set, meaning its default flag
1526 * is always cleared.
1527 *
1528 * @param[in] term Term node to change.
1529 * @param[in] value New value to set.
1530 * @param[in] value_len Length of @p value.
1531 * @param[in] format Format of @p value.
1532 * @return LY_SUCCESS if value was changed,
1533 * @return LY_EEXIST if value was the same and only the default flag was cleared,
1534 * @return LY_ENOT if the values were equal and no change occured,
1535 * @return LY_ERR value on other errors.
1536 */
1537static LY_ERR
1538_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 +02001539{
1540 LY_ERR ret = LY_SUCCESS;
1541 struct lysc_type *type;
1542 struct lyd_node_term *t;
1543 struct lyd_node *parent;
1544 struct lyd_value val = {0};
Radek Krejci857189e2020-09-01 13:26:36 +02001545 ly_bool dflt_change, val_change;
Michal Vasko00cbf532020-06-15 13:58:47 +02001546
Radek Krejci09c77442021-04-26 11:10:34 +02001547 assert(term && term->schema && (term->schema->nodetype & LYD_NODE_TERM));
Michal Vasko00cbf532020-06-15 13:58:47 +02001548
Michal Vasko00cbf532020-06-15 13:58:47 +02001549 t = (struct lyd_node_term *)term;
1550 type = ((struct lysc_node_leaf *)term->schema)->type;
1551
1552 /* parse the new value */
Radek Krejciddace2c2021-01-08 11:30:56 +01001553 LOG_LOCSET(term->schema, term, NULL, NULL);
Radek Krejci09c77442021-04-26 11:10:34 +02001554 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 +01001555 LOG_LOCBACK(term->schema ? 1 : 0, 1, 0, 0);
Michal Vaskofeca4fb2020-10-05 08:58:40 +02001556 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko00cbf532020-06-15 13:58:47 +02001557
1558 /* compare original and new value */
1559 if (type->plugin->compare(&t->value, &val)) {
1560 /* values differ, switch them */
Michal Vaskob7be7a82020-08-20 09:09:04 +02001561 type->plugin->free(LYD_CTX(term), &t->value);
Michal Vasko00cbf532020-06-15 13:58:47 +02001562 t->value = val;
1563 memset(&val, 0, sizeof val);
1564 val_change = 1;
1565 } else {
1566 val_change = 0;
1567 }
1568
1569 /* always clear the default flag */
1570 if (term->flags & LYD_DEFAULT) {
Michal Vasko9e685082021-01-29 14:49:09 +01001571 for (parent = term; parent; parent = lyd_parent(parent)) {
Michal Vasko00cbf532020-06-15 13:58:47 +02001572 parent->flags &= ~LYD_DEFAULT;
1573 }
1574 dflt_change = 1;
1575 } else {
1576 dflt_change = 0;
1577 }
1578
1579 if (val_change || dflt_change) {
1580 /* make the node non-validated */
1581 term->flags &= LYD_NEW;
1582 }
1583
1584 if (val_change) {
1585 if (term->schema->nodetype == LYS_LEAFLIST) {
1586 /* leaf-list needs to be hashed again and re-inserted into parent */
1587 lyd_unlink_hash(term);
1588 lyd_hash(term);
1589 LY_CHECK_GOTO(ret = lyd_insert_hash(term), cleanup);
1590 } else if ((term->schema->flags & LYS_KEY) && term->parent) {
1591 /* list needs to be updated if its key was changed */
1592 assert(term->parent->schema->nodetype == LYS_LIST);
Michal Vasko9e685082021-01-29 14:49:09 +01001593 lyd_unlink_hash(lyd_parent(term));
1594 lyd_hash(lyd_parent(term));
1595 LY_CHECK_GOTO(ret = lyd_insert_hash(lyd_parent(term)), cleanup);
Michal Vasko00cbf532020-06-15 13:58:47 +02001596 } /* else leaf that is not a key, its value is not used for its hash so it does not change */
1597 }
1598
1599 /* retrun value */
1600 if (!val_change) {
1601 if (dflt_change) {
1602 /* only default flag change */
1603 ret = LY_EEXIST;
1604 } else {
1605 /* no change */
1606 ret = LY_ENOT;
1607 }
1608 } /* else value changed, LY_SUCCESS */
1609
1610cleanup:
Michal Vasko59512fc2020-12-09 18:13:29 +01001611 if (val.realtype) {
1612 type->plugin->free(LYD_CTX(term), &val);
1613 }
Michal Vasko00cbf532020-06-15 13:58:47 +02001614 return ret;
1615}
1616
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001617LIBYANG_API_DEF LY_ERR
Radek Krejci09c77442021-04-26 11:10:34 +02001618lyd_change_term(struct lyd_node *term, const char *val_str)
1619{
1620 LY_CHECK_ARG_RET(NULL, term, term->schema, term->schema->nodetype & LYD_NODE_TERM, LY_EINVAL);
1621
1622 return _lyd_change_term(term, val_str, val_str ? strlen(val_str) : 0, LY_VALUE_JSON);
1623}
1624
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001625LIBYANG_API_DEF LY_ERR
Radek Krejci09c77442021-04-26 11:10:34 +02001626lyd_change_term_bin(struct lyd_node *term, const void *value, size_t value_len)
1627{
1628 LY_CHECK_ARG_RET(NULL, term, term->schema, term->schema->nodetype & LYD_NODE_TERM, LY_EINVAL);
1629
1630 return _lyd_change_term(term, value, value_len, LY_VALUE_LYB);
1631}
1632
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001633LIBYANG_API_DEF LY_ERR
Radek Krejci09c77442021-04-26 11:10:34 +02001634lyd_change_term_canon(struct lyd_node *term, const char *val_str)
1635{
1636 LY_CHECK_ARG_RET(NULL, term, term->schema, term->schema->nodetype & LYD_NODE_TERM, LY_EINVAL);
1637
1638 return _lyd_change_term(term, val_str, val_str ? strlen(val_str) : 0, LY_VALUE_CANON);
1639}
1640
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001641LIBYANG_API_DEF LY_ERR
Michal Vasko41586352020-07-13 13:54:25 +02001642lyd_change_meta(struct lyd_meta *meta, const char *val_str)
1643{
1644 LY_ERR ret = LY_SUCCESS;
Radek Krejci2b18bf12020-11-06 11:20:20 +01001645 struct lyd_meta *m2 = NULL;
Michal Vasko41586352020-07-13 13:54:25 +02001646 struct lyd_value val;
Radek Krejci857189e2020-09-01 13:26:36 +02001647 ly_bool val_change;
Michal Vasko41586352020-07-13 13:54:25 +02001648
1649 LY_CHECK_ARG_RET(NULL, meta, LY_EINVAL);
1650
1651 if (!val_str) {
1652 val_str = "";
1653 }
1654
1655 /* parse the new value into a new meta structure */
Michal Vaskoddd76592022-01-17 13:34:48 +01001656 ret = lyd_create_meta(NULL, &m2, meta->annotation->module, meta->name, strlen(meta->name), val_str, strlen(val_str),
1657 NULL, LY_VALUE_JSON, NULL, LYD_HINT_DATA, meta->parent ? meta->parent->schema : NULL, 0, NULL);
1658 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko41586352020-07-13 13:54:25 +02001659
1660 /* compare original and new value */
1661 if (lyd_compare_meta(meta, m2)) {
1662 /* values differ, switch them */
1663 val = meta->value;
1664 meta->value = m2->value;
1665 m2->value = val;
1666 val_change = 1;
1667 } else {
1668 val_change = 0;
1669 }
1670
1671 /* retrun value */
1672 if (!val_change) {
1673 /* no change */
1674 ret = LY_ENOT;
1675 } /* else value changed, LY_SUCCESS */
1676
1677cleanup:
Michal Vasko1a66a5d2020-11-18 18:15:37 +01001678 lyd_free_meta_single(m2);
Michal Vasko41586352020-07-13 13:54:25 +02001679 return ret;
1680}
1681
Radek Krejci09c77442021-04-26 11:10:34 +02001682/**
1683 * @brief Update node value.
1684 *
1685 * @param[in] node Node to update.
1686 * @param[in] value New value to set.
1687 * @param[in] value_len Length of @p value.
1688 * @param[in] value_type Type of @p value for anydata/anyxml node.
1689 * @param[in] format Format of @p value.
1690 * @param[out] new_parent Set to @p node if the value was updated, otherwise set to NULL.
1691 * @param[out] new_node Set to @p node if the value was updated, otherwise set to NULL.
1692 * @return LY_ERR value.
1693 */
Michal Vasko6741dc62021-02-04 09:27:45 +01001694static LY_ERR
Radek Krejci09c77442021-04-26 11:10:34 +02001695lyd_new_path_update(struct lyd_node *node, const void *value, size_t value_len, LYD_ANYDATA_VALUETYPE value_type,
1696 LY_VALUE_FORMAT format, struct lyd_node **new_parent, struct lyd_node **new_node)
1697{
1698 LY_ERR ret = LY_SUCCESS;
1699 struct lyd_node *new_any;
1700
1701 switch (node->schema->nodetype) {
1702 case LYS_CONTAINER:
1703 case LYS_NOTIF:
1704 case LYS_RPC:
1705 case LYS_ACTION:
1706 case LYS_LIST:
1707 /* if it exists, there is nothing to update */
1708 *new_parent = NULL;
1709 *new_node = NULL;
1710 break;
1711 case LYS_LEAFLIST:
1712 if (!lysc_is_dup_inst_list(node->schema)) {
1713 /* if it exists, there is nothing to update */
1714 *new_parent = NULL;
1715 *new_node = NULL;
1716 break;
1717 }
1718 /* fallthrough */
1719 case LYS_LEAF:
1720 ret = _lyd_change_term(node, value, value_len, format);
1721 if ((ret == LY_SUCCESS) || (ret == LY_EEXIST)) {
1722 /* there was an actual change (at least of the default flag) */
1723 *new_parent = node;
1724 *new_node = node;
1725 ret = LY_SUCCESS;
1726 } else if (ret == LY_ENOT) {
1727 /* no change */
1728 *new_parent = NULL;
1729 *new_node = NULL;
1730 ret = LY_SUCCESS;
1731 } /* else error */
1732 break;
1733 case LYS_ANYDATA:
1734 case LYS_ANYXML:
1735 /* create a new any node */
1736 LY_CHECK_RET(lyd_create_any(node->schema, value, value_type, 0, &new_any));
1737
1738 /* compare with the existing one */
1739 if (lyd_compare_single(node, new_any, 0)) {
1740 /* not equal, switch values (so that we can use generic node free) */
1741 ((struct lyd_node_any *)new_any)->value = ((struct lyd_node_any *)node)->value;
1742 ((struct lyd_node_any *)new_any)->value_type = ((struct lyd_node_any *)node)->value_type;
1743 ((struct lyd_node_any *)node)->value.str = value;
1744 ((struct lyd_node_any *)node)->value_type = value_type;
1745
1746 *new_parent = node;
1747 *new_node = node;
1748 } else {
1749 /* they are equal */
1750 *new_parent = NULL;
1751 *new_node = NULL;
1752 }
1753 lyd_free_tree(new_any);
1754 break;
1755 default:
1756 LOGINT(LYD_CTX(node));
1757 ret = LY_EINT;
1758 break;
1759 }
1760
1761 return ret;
1762}
1763
1764static LY_ERR
1765lyd_new_path_check_find_lypath(struct ly_path *path, const char *str_path, const char *value, size_t value_len,
1766 LY_VALUE_FORMAT format, uint32_t options)
Michal Vasko6741dc62021-02-04 09:27:45 +01001767{
Michal Vaskoe78faec2021-04-08 17:24:43 +02001768 LY_ERR r;
Michal Vasko6741dc62021-02-04 09:27:45 +01001769 struct ly_path_predicate *pred;
Michal Vaskoe1886a92022-01-10 09:30:08 +01001770 struct lyd_value val;
Michal Vaskoe78faec2021-04-08 17:24:43 +02001771 const struct lysc_node *schema = NULL;
1772 LY_ARRAY_COUNT_TYPE u, new_count;
1773 int create = 0;
Michal Vasko6741dc62021-02-04 09:27:45 +01001774
1775 assert(path);
1776
1777 /* go through all the compiled nodes */
1778 LY_ARRAY_FOR(path, u) {
1779 schema = path[u].node;
Michal Vaskoe78faec2021-04-08 17:24:43 +02001780
1781 if (lysc_is_dup_inst_list(schema)) {
1782 if (path[u].pred_type == LY_PATH_PREDTYPE_NONE) {
1783 /* creating a new key-less list or state leaf-list instance */
1784 create = 1;
1785 new_count = u;
1786 } else if (path[u].pred_type != LY_PATH_PREDTYPE_POSITION) {
1787 LOG_LOCSET(schema, NULL, NULL, NULL);
1788 LOGVAL(schema->module->ctx, LYVE_XPATH, "Invalid predicate for %s \"%s\" in path \"%s\".",
1789 lys_nodetype2str(schema->nodetype), schema->name, str_path);
1790 LOG_LOCBACK(1, 0, 0, 0);
1791 return LY_EINVAL;
1792 }
1793 } else if ((schema->nodetype == LYS_LIST) && (path[u].pred_type != LY_PATH_PREDTYPE_LIST)) {
1794 if ((u < LY_ARRAY_COUNT(path) - 1) || !(options & LYD_NEW_PATH_OPAQ)) {
Michal Vasko6741dc62021-02-04 09:27:45 +01001795 LOG_LOCSET(schema, NULL, NULL, NULL);
1796 LOGVAL(schema->module->ctx, LYVE_XPATH, "Predicate missing for %s \"%s\" in path \"%s\".",
1797 lys_nodetype2str(schema->nodetype), schema->name, str_path);
1798 LOG_LOCBACK(1, 0, 0, 0);
1799 return LY_EINVAL;
Michal Vaskoe78faec2021-04-08 17:24:43 +02001800 } /* else creating an opaque list */
1801 } else if ((schema->nodetype == LYS_LEAFLIST) && (path[u].pred_type != LY_PATH_PREDTYPE_LEAFLIST)) {
Michal Vaskoe78faec2021-04-08 17:24:43 +02001802 r = LY_SUCCESS;
1803 if (options & LYD_NEW_PATH_OPAQ) {
Radek Krejci09c77442021-04-26 11:10:34 +02001804 r = lyd_value_validate(NULL, schema, value, value_len, NULL, NULL, NULL);
Michal Vaskoe78faec2021-04-08 17:24:43 +02001805 }
1806 if (!r) {
Michal Vaskoe1886a92022-01-10 09:30:08 +01001807 /* try to store the value */
1808 LY_CHECK_RET(lyd_value_store(schema->module->ctx, &val, ((struct lysc_node_leaflist *)schema)->type,
1809 value, value_len, NULL, format, NULL, LYD_HINT_DATA, schema, NULL));
1810 ++((struct lysc_type *)val.realtype)->refcount;
1811
Michal Vaskoe78faec2021-04-08 17:24:43 +02001812 /* store the new predicate so that it is used when searching for this instance */
1813 path[u].pred_type = LY_PATH_PREDTYPE_LEAFLIST;
1814 LY_ARRAY_NEW_RET(schema->module->ctx, path[u].predicates, pred, LY_EMEM);
Michal Vaskoe1886a92022-01-10 09:30:08 +01001815 pred->value = val;
Michal Vaskoe78faec2021-04-08 17:24:43 +02001816 } /* else we have opaq flag and the value is not valid, leave no predicate and then create an opaque node */
1817 }
Michal Vasko6741dc62021-02-04 09:27:45 +01001818 }
1819
Michal Vaskoe78faec2021-04-08 17:24:43 +02001820 if (create) {
1821 /* hide the nodes that should always be created so they are not found */
1822 while (new_count < LY_ARRAY_COUNT(path)) {
1823 LY_ARRAY_DECREMENT(path);
1824 }
Michal Vasko6741dc62021-02-04 09:27:45 +01001825 }
1826
Michal Vaskoe78faec2021-04-08 17:24:43 +02001827 return LY_SUCCESS;
Michal Vasko6741dc62021-02-04 09:27:45 +01001828}
1829
Radek Krejci95ccd1b2021-03-12 14:57:22 +01001830/**
1831 * @brief Create a new node in the data tree based on a path. All node types can be created.
1832 *
Michal Vaskoa918a632021-07-02 11:53:36 +02001833 * If @p path points to a list key, the key value from the predicate is used and @p value is ignored.
1834 * Also, if a leaf-list is being created and both a predicate is defined in @p path
Radek Krejci95ccd1b2021-03-12 14:57:22 +01001835 * and @p value is set, the predicate is preferred.
1836 *
1837 * For key-less lists and state leaf-lists, positional predicates can be used. If no preciate is used for these
1838 * nodes, they are always created.
1839 *
1840 * @param[in] parent Data parent to add to/modify, can be NULL. Note that in case a first top-level sibling is used,
1841 * it may no longer be first if @p path is absolute and starts with a non-existing top-level node inserted
1842 * before @p parent. Use ::lyd_first_sibling() to adjust @p parent in these cases.
1843 * @param[in] ctx libyang context, must be set if @p parent is NULL.
1844 * @param[in] ext Extension instance where the node being created is defined. This argument takes effect only for absolute
1845 * path or when the relative paths touches document root (top-level). In such cases the present extension instance replaces
1846 * searching for the appropriate module.
1847 * @param[in] path [Path](@ref howtoXPath) to create.
Radek Krejci09c77442021-04-26 11:10:34 +02001848 * @param[in] value Value of the new leaf/leaf-list (const char *) in ::LY_VALUE_JSON format. If creating an
1849 * anyxml/anydata node, the expected type depends on @p value_type. For other node types, it should be NULL.
1850 * @param[in] value_len Length of @p value in bytes. May be 0 if @p value is a zero-terminated string. Ignored when
1851 * creating anyxml/anydata nodes.
Radek Krejci95ccd1b2021-03-12 14:57:22 +01001852 * @param[in] value_type Anyxml/anydata node @p value type.
1853 * @param[in] options Bitmask of options, see @ref pathoptions.
1854 * @param[out] new_parent Optional first parent node created. If only one node was created, equals to @p new_node.
1855 * @param[out] new_node Optional last node created.
1856 * @return LY_ERR value.
1857 */
1858static LY_ERR
1859lyd_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 +02001860 const void *value, size_t value_len, LYD_ANYDATA_VALUETYPE value_type, uint32_t options,
Radek Krejci95ccd1b2021-03-12 14:57:22 +01001861 struct lyd_node **new_parent, struct lyd_node **new_node)
Michal Vasko00cbf532020-06-15 13:58:47 +02001862{
1863 LY_ERR ret = LY_SUCCESS, r;
1864 struct lyxp_expr *exp = NULL;
1865 struct ly_path *p = NULL;
1866 struct lyd_node *nparent = NULL, *nnode = NULL, *node = NULL, *cur_parent;
1867 const struct lysc_node *schema;
Michal Vaskoe78faec2021-04-08 17:24:43 +02001868 const struct lyd_value *val = NULL;
Radek Krejci910d0132021-03-05 14:19:31 +01001869 LY_ARRAY_COUNT_TYPE path_idx = 0, orig_count = 0;
Radek Krejci09c77442021-04-26 11:10:34 +02001870 LY_VALUE_FORMAT format;
Michal Vasko00cbf532020-06-15 13:58:47 +02001871
Radek Krejci95ccd1b2021-03-12 14:57:22 +01001872 assert(parent || ctx);
Radek Krejci09c77442021-04-26 11:10:34 +02001873 assert(path && ((path[0] == '/') || parent));
1874 assert(!(options & LYD_NEW_PATH_BIN_VALUE) || !(options & LYD_NEW_PATH_CANON_VALUE));
Michal Vasko00cbf532020-06-15 13:58:47 +02001875
1876 if (!ctx) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02001877 ctx = LYD_CTX(parent);
Michal Vasko00cbf532020-06-15 13:58:47 +02001878 }
Radek Krejci09c77442021-04-26 11:10:34 +02001879 if (value && !value_len) {
1880 value_len = strlen(value);
1881 }
1882 if (options & LYD_NEW_PATH_BIN_VALUE) {
1883 format = LY_VALUE_LYB;
1884 } else if (options & LYD_NEW_PATH_CANON_VALUE) {
1885 format = LY_VALUE_CANON;
1886 } else {
1887 format = LY_VALUE_JSON;
1888 }
Michal Vasko00cbf532020-06-15 13:58:47 +02001889
1890 /* parse path */
Michal Vaskoed725d72021-06-23 12:03:45 +02001891 LY_CHECK_GOTO(ret = ly_path_parse(ctx, NULL, path, strlen(path), 0, LY_PATH_BEGIN_EITHER, LY_PATH_PREFIX_OPTIONAL,
1892 LY_PATH_PRED_SIMPLE, &exp), cleanup);
Michal Vasko00cbf532020-06-15 13:58:47 +02001893
1894 /* compile path */
Michal Vasko106f0862021-11-02 11:49:27 +01001895 LY_CHECK_GOTO(ret = ly_path_compile(ctx, NULL, lyd_node_schema(parent), ext, exp, options & LYD_NEW_PATH_OUTPUT ?
1896 LY_PATH_OPER_OUTPUT : LY_PATH_OPER_INPUT, LY_PATH_TARGET_MANY, 0, LY_VALUE_JSON, NULL, &p), cleanup);
Michal Vasko00cbf532020-06-15 13:58:47 +02001897
Michal Vasko6741dc62021-02-04 09:27:45 +01001898 /* check the compiled path before searching existing nodes, it may be shortened */
1899 orig_count = LY_ARRAY_COUNT(p);
Radek Krejci09c77442021-04-26 11:10:34 +02001900 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 +02001901
1902 /* try to find any existing nodes in the path */
1903 if (parent) {
1904 ret = ly_path_eval_partial(p, parent, &path_idx, &node);
1905 if (ret == LY_SUCCESS) {
Michal Vasko6741dc62021-02-04 09:27:45 +01001906 if (orig_count == LY_ARRAY_COUNT(p)) {
1907 /* the node exists, are we supposed to update it or is it just a default? */
1908 if (!(options & LYD_NEW_PATH_UPDATE) && !(node->flags & LYD_DEFAULT)) {
1909 LOG_LOCSET(NULL, node, NULL, NULL);
1910 LOGVAL(ctx, LYVE_REFERENCE, "Path \"%s\" already exists", path);
1911 LOG_LOCBACK(0, 1, 0, 0);
1912 ret = LY_EEXIST;
1913 goto cleanup;
1914 }
Michal Vasko00cbf532020-06-15 13:58:47 +02001915
Michal Vasko6741dc62021-02-04 09:27:45 +01001916 /* update the existing node */
Radek Krejci09c77442021-04-26 11:10:34 +02001917 ret = lyd_new_path_update(node, value, value_len, value_type, format, &nparent, &nnode);
Michal Vasko6741dc62021-02-04 09:27:45 +01001918 goto cleanup;
1919 } /* else we were not searching for the whole path */
Michal Vasko00cbf532020-06-15 13:58:47 +02001920 } else if (ret == LY_EINCOMPLETE) {
1921 /* some nodes were found, adjust the iterator to the next segment */
1922 ++path_idx;
1923 } else if (ret == LY_ENOTFOUND) {
1924 /* 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 +01001925 if (lysc_data_parent(p[0].node)) {
Michal Vasko00cbf532020-06-15 13:58:47 +02001926 node = parent;
1927 }
1928 } else {
1929 /* error */
1930 goto cleanup;
1931 }
1932 }
1933
Michal Vasko6741dc62021-02-04 09:27:45 +01001934 /* restore the full path for creating new nodes */
1935 while (orig_count > LY_ARRAY_COUNT(p)) {
1936 LY_ARRAY_INCREMENT(p);
1937 }
1938
Michal Vasko00cbf532020-06-15 13:58:47 +02001939 /* create all the non-existing nodes in a loop */
Michal Vaskod989ba02020-08-24 10:59:24 +02001940 for ( ; path_idx < LY_ARRAY_COUNT(p); ++path_idx) {
Michal Vasko00cbf532020-06-15 13:58:47 +02001941 cur_parent = node;
1942 schema = p[path_idx].node;
1943
1944 switch (schema->nodetype) {
1945 case LYS_LIST:
Michal Vaskoe78faec2021-04-08 17:24:43 +02001946 if (lysc_is_dup_inst_list(schema)) {
1947 /* create key-less list instance */
1948 LY_CHECK_GOTO(ret = lyd_create_inner(schema, &node), cleanup);
1949 } else if ((options & LYD_NEW_PATH_OPAQ) && (p[path_idx].pred_type == LY_PATH_PREDTYPE_NONE)) {
1950 /* creating opaque list without keys */
1951 LY_CHECK_GOTO(ret = lyd_create_opaq(ctx, schema->name, strlen(schema->name), NULL, 0,
Radek Krejci8df109d2021-04-23 12:19:08 +02001952 schema->module->name, strlen(schema->module->name), NULL, 0, NULL, LY_VALUE_JSON, NULL,
Michal Vaskoe78faec2021-04-08 17:24:43 +02001953 LYD_NODEHINT_LIST, &node), cleanup);
1954 } else {
1955 /* create standard list instance */
1956 assert(p[path_idx].pred_type == LY_PATH_PREDTYPE_LIST);
1957 LY_CHECK_GOTO(ret = lyd_create_list(schema, p[path_idx].predicates, &node), cleanup);
Michal Vasko00cbf532020-06-15 13:58:47 +02001958 }
Michal Vaskoe78faec2021-04-08 17:24:43 +02001959 break;
Michal Vasko00cbf532020-06-15 13:58:47 +02001960 case LYS_CONTAINER:
1961 case LYS_NOTIF:
1962 case LYS_RPC:
1963 case LYS_ACTION:
1964 LY_CHECK_GOTO(ret = lyd_create_inner(schema, &node), cleanup);
1965 break;
1966 case LYS_LEAFLIST:
Michal Vaskoe78faec2021-04-08 17:24:43 +02001967 if ((options & LYD_NEW_PATH_OPAQ) && (p[path_idx].pred_type != LY_PATH_PREDTYPE_LEAFLIST)) {
1968 /* we have not checked this only for dup-inst lists, otherwise it must be opaque */
1969 r = LY_EVALID;
1970 if (lysc_is_dup_inst_list(schema)) {
1971 /* validate value */
Radek Krejci09c77442021-04-26 11:10:34 +02001972 r = lyd_value_validate(NULL, schema, value ? value : "", value_len, NULL, NULL, NULL);
Michal Vaskoe78faec2021-04-08 17:24:43 +02001973 }
1974 if (r && (r != LY_EINCOMPLETE)) {
1975 /* creating opaque leaf-list */
Radek Krejci09c77442021-04-26 11:10:34 +02001976 LY_CHECK_GOTO(ret = lyd_create_opaq(ctx, schema->name, strlen(schema->name), value, value_len,
1977 schema->module->name, strlen(schema->module->name), NULL, 0, NULL, format, NULL,
1978 LYD_NODEHINT_LEAFLIST, &node), cleanup);
Michal Vaskoe78faec2021-04-08 17:24:43 +02001979 break;
1980 }
1981 }
1982
1983 /* get value to set */
1984 if (p[path_idx].pred_type == LY_PATH_PREDTYPE_LEAFLIST) {
1985 val = &p[path_idx].predicates[0].value;
Michal Vaskoe78faec2021-04-08 17:24:43 +02001986 }
1987
1988 /* create a leaf-list instance */
1989 if (val) {
Michal Vasko00cbf532020-06-15 13:58:47 +02001990 LY_CHECK_GOTO(ret = lyd_create_term2(schema, &p[path_idx].predicates[0].value, &node), cleanup);
Michal Vaskoe78faec2021-04-08 17:24:43 +02001991 } else {
Radek Krejci09c77442021-04-26 11:10:34 +02001992 LY_CHECK_GOTO(ret = lyd_create_term(schema, value, value_len, NULL, format, NULL, LYD_HINT_DATA,
1993 NULL, &node), cleanup);
Michal Vasko00cbf532020-06-15 13:58:47 +02001994 }
1995 break;
1996 case LYS_LEAF:
Michal Vasko106f0862021-11-02 11:49:27 +01001997 if (lysc_is_key(schema) && cur_parent->schema) {
Michal Vasko35880332020-12-08 13:08:12 +01001998 /* it must have been already created or some error will occur later */
Michal Vaskoa918a632021-07-02 11:53:36 +02001999 lyd_find_sibling_schema(lyd_child(cur_parent), schema, &node);
2000 assert(node);
Michal Vasko35880332020-12-08 13:08:12 +01002001 goto next_iter;
2002 }
2003
Michal Vaskoe78faec2021-04-08 17:24:43 +02002004 if (options & LYD_NEW_PATH_OPAQ) {
Michal Vasko106f0862021-11-02 11:49:27 +01002005 if (cur_parent && !cur_parent->schema) {
2006 /* always create opaque nodes for opaque parents */
2007 r = LY_ENOT;
2008 } else {
2009 /* validate value */
2010 r = lyd_value_validate(NULL, schema, value ? value : "", value_len, NULL, NULL, NULL);
2011 }
Michal Vaskoe78faec2021-04-08 17:24:43 +02002012 if (r && (r != LY_EINCOMPLETE)) {
2013 /* creating opaque leaf */
Radek Krejci09c77442021-04-26 11:10:34 +02002014 LY_CHECK_GOTO(ret = lyd_create_opaq(ctx, schema->name, strlen(schema->name), value, value_len,
2015 schema->module->name, strlen(schema->module->name), NULL, 0, NULL, format, NULL, 0, &node),
2016 cleanup);
Michal Vaskoe78faec2021-04-08 17:24:43 +02002017 break;
2018 }
2019 }
2020
Michal Vaskoe78faec2021-04-08 17:24:43 +02002021 /* create a leaf instance */
Radek Krejci09c77442021-04-26 11:10:34 +02002022 LY_CHECK_GOTO(ret = lyd_create_term(schema, value, value_len, NULL, format, NULL, LYD_HINT_DATA, NULL,
2023 &node), cleanup);
Michal Vasko00cbf532020-06-15 13:58:47 +02002024 break;
2025 case LYS_ANYDATA:
2026 case LYS_ANYXML:
Michal Vasko366a4a12020-12-04 16:23:57 +01002027 LY_CHECK_GOTO(ret = lyd_create_any(schema, value, value_type, 0, &node), cleanup);
Michal Vasko00cbf532020-06-15 13:58:47 +02002028 break;
2029 default:
2030 LOGINT(ctx);
2031 ret = LY_EINT;
2032 goto cleanup;
2033 }
2034
2035 if (cur_parent) {
2036 /* connect to the parent */
Michal Vasko6ee6f432021-07-16 09:49:14 +02002037 lyd_insert_node(cur_parent, NULL, node, 0);
Michal Vasko00cbf532020-06-15 13:58:47 +02002038 } else if (parent) {
2039 /* connect to top-level siblings */
Michal Vasko6ee6f432021-07-16 09:49:14 +02002040 lyd_insert_node(NULL, &parent, node, 0);
Michal Vasko00cbf532020-06-15 13:58:47 +02002041 }
2042
Michal Vasko35880332020-12-08 13:08:12 +01002043next_iter:
Michal Vasko00cbf532020-06-15 13:58:47 +02002044 /* update remembered nodes */
2045 if (!nparent) {
2046 nparent = node;
2047 }
2048 nnode = node;
2049 }
2050
2051cleanup:
2052 lyxp_expr_free(ctx, exp);
Michal Vasko6741dc62021-02-04 09:27:45 +01002053 if (p) {
2054 while (orig_count > LY_ARRAY_COUNT(p)) {
2055 LY_ARRAY_INCREMENT(p);
2056 }
2057 }
Michal Vasko00cbf532020-06-15 13:58:47 +02002058 ly_path_free(ctx, p);
2059 if (!ret) {
2060 /* set out params only on success */
2061 if (new_parent) {
2062 *new_parent = nparent;
2063 }
2064 if (new_node) {
2065 *new_node = nnode;
2066 }
Michal Vaskof4b95ba2020-12-11 08:42:33 +01002067 } else {
2068 lyd_free_tree(nparent);
Michal Vasko00cbf532020-06-15 13:58:47 +02002069 }
2070 return ret;
2071}
2072
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002073LIBYANG_API_DEF LY_ERR
Radek Krejci95ccd1b2021-03-12 14:57:22 +01002074lyd_new_path(struct lyd_node *parent, const struct ly_ctx *ctx, const char *path, const char *value, uint32_t options,
2075 struct lyd_node **node)
2076{
Radek Krejci09c77442021-04-26 11:10:34 +02002077 LY_CHECK_ARG_RET(ctx, parent || ctx, path, (path[0] == '/') || parent,
2078 !(options & LYD_NEW_PATH_BIN_VALUE) || !(options & LYD_NEW_PATH_CANON_VALUE), LY_EINVAL);
Michal Vasko892f5bf2021-11-24 10:41:05 +01002079 LY_CHECK_CTX_EQUAL_RET(parent ? LYD_CTX(parent) : NULL, ctx, LY_EINVAL);
Radek Krejci09c77442021-04-26 11:10:34 +02002080
2081 return lyd_new_path_(parent, ctx, NULL, path, value, 0, LYD_ANYDATA_STRING, options, node, NULL);
Radek Krejci95ccd1b2021-03-12 14:57:22 +01002082}
2083
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002084LIBYANG_API_DEF LY_ERR
Radek Krejci95ccd1b2021-03-12 14:57:22 +01002085lyd_new_path2(struct lyd_node *parent, const struct ly_ctx *ctx, const char *path, const void *value,
Radek Krejci09c77442021-04-26 11:10:34 +02002086 size_t value_len, LYD_ANYDATA_VALUETYPE value_type, uint32_t options, struct lyd_node **new_parent,
2087 struct lyd_node **new_node)
Radek Krejci95ccd1b2021-03-12 14:57:22 +01002088{
Radek Krejci09c77442021-04-26 11:10:34 +02002089 LY_CHECK_ARG_RET(ctx, parent || ctx, path, (path[0] == '/') || parent,
2090 !(options & LYD_NEW_PATH_BIN_VALUE) || !(options & LYD_NEW_PATH_CANON_VALUE), LY_EINVAL);
Michal Vasko892f5bf2021-11-24 10:41:05 +01002091 LY_CHECK_CTX_EQUAL_RET(parent ? LYD_CTX(parent) : NULL, ctx, LY_EINVAL);
2092
Radek Krejci09c77442021-04-26 11:10:34 +02002093 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 +01002094}
2095
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002096LIBYANG_API_DEF LY_ERR
Radek Krejci95ccd1b2021-03-12 14:57:22 +01002097lyd_new_ext_path(struct lyd_node *parent, const struct lysc_ext_instance *ext, const char *path, const void *value,
2098 uint32_t options, struct lyd_node **node)
2099{
2100 const struct ly_ctx *ctx = ext ? ext->module->ctx : NULL;
2101
Radek Krejci09c77442021-04-26 11:10:34 +02002102 LY_CHECK_ARG_RET(ctx, ext, path, (path[0] == '/') || parent,
2103 !(options & LYD_NEW_PATH_BIN_VALUE) || !(options & LYD_NEW_PATH_CANON_VALUE), LY_EINVAL);
Michal Vasko892f5bf2021-11-24 10:41:05 +01002104 LY_CHECK_CTX_EQUAL_RET(parent ? LYD_CTX(parent) : NULL, ctx, LY_EINVAL);
2105
Radek Krejci09c77442021-04-26 11:10:34 +02002106 return lyd_new_path_(parent, ctx, ext, path, value, 0, LYD_ANYDATA_STRING, options, node, NULL);
Radek Krejci95ccd1b2021-03-12 14:57:22 +01002107}
2108
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002109LY_ERR
2110lyd_new_implicit_r(struct lyd_node *parent, struct lyd_node **first, const struct lysc_node *sparent,
Michal Vaskoddd76592022-01-17 13:34:48 +01002111 const struct lys_module *mod, struct ly_set *node_when, struct ly_set *node_types, uint32_t impl_opts,
2112 struct lyd_node **diff)
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002113{
2114 LY_ERR ret;
Michal Vaskod1e53b92021-01-28 13:11:06 +01002115 const struct lysc_node *iter = NULL;
Radek Krejci2b18bf12020-11-06 11:20:20 +01002116 struct lyd_node *node = NULL;
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002117 struct lyd_value **dflts;
2118 LY_ARRAY_COUNT_TYPE u;
Michal Vasko630d9892020-12-08 17:11:08 +01002119 uint32_t getnext_opts;
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002120
2121 assert(first && (parent || sparent || mod));
2122
2123 if (!sparent && parent) {
2124 sparent = parent->schema;
2125 }
2126
Michal Vasko630d9892020-12-08 17:11:08 +01002127 getnext_opts = LYS_GETNEXT_WITHCHOICE;
2128 if (impl_opts & LYD_IMPLICIT_OUTPUT) {
2129 getnext_opts |= LYS_GETNEXT_OUTPUT;
2130 }
2131
2132 while ((iter = lys_getnext(iter, sparent, mod ? mod->compiled : NULL, getnext_opts))) {
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002133 if ((impl_opts & LYD_IMPLICIT_NO_STATE) && (iter->flags & LYS_CONFIG_R)) {
2134 continue;
Michal Vasko44b19a12020-08-07 09:21:30 +02002135 } else if ((impl_opts & LYD_IMPLICIT_NO_CONFIG) && (iter->flags & LYS_CONFIG_W)) {
2136 continue;
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002137 }
2138
2139 switch (iter->nodetype) {
2140 case LYS_CHOICE:
Michal Vaskobcf4d7d2020-11-18 18:16:49 +01002141 node = lys_getnext_data(NULL, *first, NULL, iter, NULL);
2142 if (!node && ((struct lysc_node_choice *)iter)->dflt) {
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002143 /* create default case data */
Michal Vasko14ed9cd2021-01-28 14:16:25 +01002144 LY_CHECK_RET(lyd_new_implicit_r(parent, first, &((struct lysc_node_choice *)iter)->dflt->node,
Michal Vaskoddd76592022-01-17 13:34:48 +01002145 NULL, node_when, node_types, impl_opts, diff));
Michal Vaskobcf4d7d2020-11-18 18:16:49 +01002146 } else if (node) {
2147 /* create any default data in the existing case */
2148 assert(node->schema->parent->nodetype == LYS_CASE);
Michal Vaskoddd76592022-01-17 13:34:48 +01002149 LY_CHECK_RET(lyd_new_implicit_r(parent, first, node->schema->parent, NULL, node_when, node_types,
Michal Vaskobcf4d7d2020-11-18 18:16:49 +01002150 impl_opts, diff));
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002151 }
2152 break;
2153 case LYS_CONTAINER:
2154 if (!(iter->flags & LYS_PRESENCE) && lyd_find_sibling_val(*first, iter, NULL, 0, NULL)) {
2155 /* create default NP container */
2156 LY_CHECK_RET(lyd_create_inner(iter, &node));
Michal Vaskof4d67ea2021-03-31 13:53:21 +02002157 node->flags = LYD_DEFAULT | (lysc_has_when(iter) ? LYD_WHEN_TRUE : 0);
Michal Vasko6ee6f432021-07-16 09:49:14 +02002158 lyd_insert_node(parent, first, node, 0);
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002159
Michal Vaskof4d67ea2021-03-31 13:53:21 +02002160 if (lysc_has_when(iter) && node_when) {
Michal Vaskoe16c7b72021-02-26 10:39:06 +01002161 /* remember to resolve when */
2162 LY_CHECK_RET(ly_set_add(node_when, node, 1, NULL));
2163 }
Michal Vaskoe49b6322020-11-05 17:38:36 +01002164 if (diff) {
2165 /* add into diff */
2166 LY_CHECK_RET(lyd_val_diff_add(node, LYD_DIFF_OP_CREATE, diff));
2167 }
2168
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002169 /* create any default children */
Michal Vaskoddd76592022-01-17 13:34:48 +01002170 LY_CHECK_RET(lyd_new_implicit_r(node, lyd_node_child_p(node), NULL, NULL, node_when, node_types,
Michal Vasko69730152020-10-09 16:30:07 +02002171 impl_opts, diff));
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002172 }
2173 break;
2174 case LYS_LEAF:
Michal Vasko69730152020-10-09 16:30:07 +02002175 if (!(impl_opts & LYD_IMPLICIT_NO_DEFAULTS) && ((struct lysc_node_leaf *)iter)->dflt &&
2176 lyd_find_sibling_val(*first, iter, NULL, 0, NULL)) {
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002177 /* create default leaf */
2178 ret = lyd_create_term2(iter, ((struct lysc_node_leaf *)iter)->dflt, &node);
2179 if (ret == LY_EINCOMPLETE) {
2180 if (node_types) {
2181 /* remember to resolve type */
Radek Krejci3d92e442020-10-12 12:48:13 +02002182 LY_CHECK_RET(ly_set_add(node_types, node, 1, NULL));
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002183 }
2184 } else if (ret) {
2185 return ret;
2186 }
Michal Vaskof4d67ea2021-03-31 13:53:21 +02002187 node->flags = LYD_DEFAULT | (lysc_has_when(iter) ? LYD_WHEN_TRUE : 0);
Michal Vasko6ee6f432021-07-16 09:49:14 +02002188 lyd_insert_node(parent, first, node, 0);
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002189
Michal Vaskof4d67ea2021-03-31 13:53:21 +02002190 if (lysc_has_when(iter) && node_when) {
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002191 /* remember to resolve when */
Radek Krejci3d92e442020-10-12 12:48:13 +02002192 LY_CHECK_RET(ly_set_add(node_when, node, 1, NULL));
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002193 }
2194 if (diff) {
2195 /* add into diff */
2196 LY_CHECK_RET(lyd_val_diff_add(node, LYD_DIFF_OP_CREATE, diff));
2197 }
2198 }
2199 break;
2200 case LYS_LEAFLIST:
Michal Vasko69730152020-10-09 16:30:07 +02002201 if (!(impl_opts & LYD_IMPLICIT_NO_DEFAULTS) && ((struct lysc_node_leaflist *)iter)->dflts &&
2202 lyd_find_sibling_val(*first, iter, NULL, 0, NULL)) {
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002203 /* create all default leaf-lists */
2204 dflts = ((struct lysc_node_leaflist *)iter)->dflts;
2205 LY_ARRAY_FOR(dflts, u) {
2206 ret = lyd_create_term2(iter, dflts[u], &node);
2207 if (ret == LY_EINCOMPLETE) {
2208 if (node_types) {
2209 /* remember to resolve type */
Radek Krejci3d92e442020-10-12 12:48:13 +02002210 LY_CHECK_RET(ly_set_add(node_types, node, 1, NULL));
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002211 }
2212 } else if (ret) {
2213 return ret;
2214 }
Michal Vaskof4d67ea2021-03-31 13:53:21 +02002215 node->flags = LYD_DEFAULT | (lysc_has_when(iter) ? LYD_WHEN_TRUE : 0);
Michal Vasko6ee6f432021-07-16 09:49:14 +02002216 lyd_insert_node(parent, first, node, 0);
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002217
Michal Vaskof4d67ea2021-03-31 13:53:21 +02002218 if (lysc_has_when(iter) && node_when) {
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002219 /* remember to resolve when */
Radek Krejci3d92e442020-10-12 12:48:13 +02002220 LY_CHECK_RET(ly_set_add(node_when, node, 1, NULL));
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002221 }
2222 if (diff) {
2223 /* add into diff */
2224 LY_CHECK_RET(lyd_val_diff_add(node, LYD_DIFF_OP_CREATE, diff));
2225 }
2226 }
2227 }
2228 break;
2229 default:
2230 /* without defaults */
2231 break;
2232 }
2233 }
2234
2235 return LY_SUCCESS;
2236}
2237
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002238LIBYANG_API_DEF LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02002239lyd_new_implicit_tree(struct lyd_node *tree, uint32_t implicit_options, struct lyd_node **diff)
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002240{
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002241 LY_ERR ret = LY_SUCCESS;
Michal Vasko3488ada2020-12-03 14:18:19 +01002242 struct lyd_node *node;
Michal Vaskoddd76592022-01-17 13:34:48 +01002243 struct ly_set node_when = {0};
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002244
2245 LY_CHECK_ARG_RET(NULL, tree, LY_EINVAL);
2246 if (diff) {
2247 *diff = NULL;
2248 }
2249
Michal Vasko56daf732020-08-10 10:57:18 +02002250 LYD_TREE_DFS_BEGIN(tree, node) {
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002251 /* skip added default nodes */
Michal Vasko69730152020-10-09 16:30:07 +02002252 if (((node->flags & (LYD_DEFAULT | LYD_NEW)) != (LYD_DEFAULT | LYD_NEW)) &&
2253 (node->schema->nodetype & LYD_NODE_INNER)) {
Michal Vaskoddd76592022-01-17 13:34:48 +01002254 LY_CHECK_GOTO(ret = lyd_new_implicit_r(node, lyd_node_child_p(node), NULL, NULL, &node_when, NULL,
2255 implicit_options, diff), cleanup);
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002256 }
2257
Michal Vasko56daf732020-08-10 10:57:18 +02002258 LYD_TREE_DFS_END(tree, node);
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002259 }
2260
Michal Vasko3488ada2020-12-03 14:18:19 +01002261 /* resolve when and remove any invalid defaults */
Michal Vaskoddd76592022-01-17 13:34:48 +01002262 LY_CHECK_GOTO(ret = lyd_validate_unres(&tree, NULL, &node_when, LYXP_IGNORE_WHEN, NULL, NULL, NULL, 0, diff), cleanup);
Michal Vasko3488ada2020-12-03 14:18:19 +01002263
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002264cleanup:
Michal Vasko3488ada2020-12-03 14:18:19 +01002265 ly_set_erase(&node_when, NULL);
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002266 if (ret && diff) {
2267 lyd_free_all(*diff);
2268 *diff = NULL;
2269 }
2270 return ret;
2271}
2272
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002273LIBYANG_API_DEF LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02002274lyd_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 +02002275{
2276 const struct lys_module *mod;
2277 struct lyd_node *d = NULL;
2278 uint32_t i = 0;
2279 LY_ERR ret = LY_SUCCESS;
2280
2281 LY_CHECK_ARG_RET(ctx, tree, *tree || ctx, LY_EINVAL);
Michal Vasko892f5bf2021-11-24 10:41:05 +01002282 LY_CHECK_CTX_EQUAL_RET(*tree ? LYD_CTX(*tree) : NULL, ctx, LY_EINVAL);
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002283 if (diff) {
2284 *diff = NULL;
2285 }
2286 if (!ctx) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02002287 ctx = LYD_CTX(*tree);
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002288 }
2289
2290 /* add nodes for each module one-by-one */
2291 while ((mod = ly_ctx_get_module_iter(ctx, &i))) {
2292 if (!mod->implemented) {
2293 continue;
2294 }
2295
2296 LY_CHECK_GOTO(ret = lyd_new_implicit_module(tree, mod, implicit_options, diff ? &d : NULL), cleanup);
2297 if (d) {
2298 /* merge into one diff */
2299 lyd_insert_sibling(*diff, d, diff);
2300
2301 d = NULL;
2302 }
2303 }
2304
2305cleanup:
2306 if (ret && diff) {
2307 lyd_free_all(*diff);
2308 *diff = NULL;
2309 }
2310 return ret;
2311}
2312
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002313LIBYANG_API_DEF LY_ERR
Michal Vasko892f5bf2021-11-24 10:41:05 +01002314lyd_new_implicit_module(struct lyd_node **tree, const struct lys_module *module, uint32_t implicit_options,
2315 struct lyd_node **diff)
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002316{
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002317 LY_ERR ret = LY_SUCCESS;
Michal Vasko3488ada2020-12-03 14:18:19 +01002318 struct lyd_node *root, *d = NULL;
Michal Vaskoddd76592022-01-17 13:34:48 +01002319 struct ly_set node_when = {0};
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002320
2321 LY_CHECK_ARG_RET(NULL, tree, module, LY_EINVAL);
Michal Vasko892f5bf2021-11-24 10:41:05 +01002322 LY_CHECK_CTX_EQUAL_RET(*tree ? LYD_CTX(*tree) : NULL, module ? module->ctx : NULL, LY_EINVAL);
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002323 if (diff) {
2324 *diff = NULL;
2325 }
2326
2327 /* add all top-level defaults for this module */
Michal Vaskoddd76592022-01-17 13:34:48 +01002328 LY_CHECK_GOTO(ret = lyd_new_implicit_r(NULL, tree, NULL, module, &node_when, NULL, implicit_options, diff), cleanup);
Michal Vasko3488ada2020-12-03 14:18:19 +01002329
2330 /* resolve when and remove any invalid defaults */
Michal Vaskoddd76592022-01-17 13:34:48 +01002331 LY_CHECK_GOTO(ret = lyd_validate_unres(tree, module, &node_when, LYXP_IGNORE_WHEN, NULL, NULL, NULL, 0, diff),
Michal Vasko976ec432021-12-06 15:42:22 +01002332 cleanup);
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002333
2334 /* process nested nodes */
2335 LY_LIST_FOR(*tree, root) {
2336 /* skip added default nodes */
2337 if ((root->flags & (LYD_DEFAULT | LYD_NEW)) != (LYD_DEFAULT | LYD_NEW)) {
2338 LY_CHECK_GOTO(ret = lyd_new_implicit_tree(root, implicit_options, diff ? &d : NULL), cleanup);
2339
2340 if (d) {
2341 /* merge into one diff */
2342 lyd_insert_sibling(*diff, d, diff);
2343
2344 d = NULL;
2345 }
2346 }
2347 }
2348
2349cleanup:
Michal Vasko3488ada2020-12-03 14:18:19 +01002350 ly_set_erase(&node_when, NULL);
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002351 if (ret && diff) {
2352 lyd_free_all(*diff);
2353 *diff = NULL;
2354 }
2355 return ret;
2356}
2357
Michal Vasko90932a92020-02-12 14:33:03 +01002358struct lyd_node *
Michal Vaskob104f112020-07-17 09:54:54 +02002359lyd_insert_get_next_anchor(const struct lyd_node *first_sibling, const struct lyd_node *new_node)
Michal Vasko90932a92020-02-12 14:33:03 +01002360{
Michal Vaskob104f112020-07-17 09:54:54 +02002361 const struct lysc_node *schema, *sparent;
Michal Vasko90932a92020-02-12 14:33:03 +01002362 struct lyd_node *match = NULL;
Radek Krejci857189e2020-09-01 13:26:36 +02002363 ly_bool found;
Michal Vasko93b16062020-12-09 18:14:18 +01002364 uint32_t getnext_opts;
Michal Vasko90932a92020-02-12 14:33:03 +01002365
Michal Vaskob104f112020-07-17 09:54:54 +02002366 assert(new_node);
2367
2368 if (!first_sibling || !new_node->schema) {
2369 /* insert at the end, no next anchor */
Michal Vasko90932a92020-02-12 14:33:03 +01002370 return NULL;
2371 }
2372
Michal Vasko61ad1ff2022-02-10 15:48:39 +01002373 assert(!first_sibling || (LYD_CTX(first_sibling) == LYD_CTX(new_node)));
2374
Michal Vasko93b16062020-12-09 18:14:18 +01002375 getnext_opts = 0;
Michal Vaskod1e53b92021-01-28 13:11:06 +01002376 if (new_node->schema->flags & LYS_IS_OUTPUT) {
Michal Vasko93b16062020-12-09 18:14:18 +01002377 getnext_opts = LYS_GETNEXT_OUTPUT;
2378 }
2379
Michal Vaskoa2756f02021-07-09 13:20:28 +02002380 if (first_sibling->parent && first_sibling->parent->schema && first_sibling->parent->children_ht) {
Michal Vaskob104f112020-07-17 09:54:54 +02002381 /* find the anchor using hashes */
2382 sparent = first_sibling->parent->schema;
Michal Vasko93b16062020-12-09 18:14:18 +01002383 schema = lys_getnext(new_node->schema, sparent, NULL, getnext_opts);
Michal Vaskob104f112020-07-17 09:54:54 +02002384 while (schema) {
2385 /* keep trying to find the first existing instance of the closest following schema sibling,
2386 * otherwise return NULL - inserting at the end */
2387 if (!lyd_find_sibling_schema(first_sibling, schema, &match)) {
2388 break;
2389 }
2390
Michal Vasko93b16062020-12-09 18:14:18 +01002391 schema = lys_getnext(schema, sparent, NULL, getnext_opts);
Michal Vaskob104f112020-07-17 09:54:54 +02002392 }
2393 } else {
2394 /* find the anchor without hashes */
2395 match = (struct lyd_node *)first_sibling;
Michal Vasko3a708622021-07-15 14:20:10 +02002396 sparent = lysc_data_parent(new_node->schema);
2397 if (!sparent) {
Michal Vaskob104f112020-07-17 09:54:54 +02002398 /* we are in top-level, skip all the data from preceding modules */
2399 LY_LIST_FOR(match, match) {
2400 if (!match->schema || (strcmp(lyd_owner_module(match)->name, lyd_owner_module(new_node)->name) >= 0)) {
2401 break;
2402 }
2403 }
2404 }
2405
2406 /* get the first schema sibling */
Michal Vasko93b16062020-12-09 18:14:18 +01002407 schema = lys_getnext(NULL, sparent, new_node->schema->module->compiled, getnext_opts);
Michal Vaskob104f112020-07-17 09:54:54 +02002408
2409 found = 0;
2410 LY_LIST_FOR(match, match) {
2411 if (!match->schema || (lyd_owner_module(match) != lyd_owner_module(new_node))) {
2412 /* we have found an opaque node, which must be at the end, so use it OR
2413 * modules do not match, so we must have traversed all the data from new_node module (if any),
2414 * we have found the first node of the next module, that is what we want */
2415 break;
2416 }
2417
2418 /* skip schema nodes until we find the instantiated one */
2419 while (!found) {
2420 if (new_node->schema == schema) {
2421 /* we have found the schema of the new node, continue search to find the first
2422 * data node with a different schema (after our schema) */
2423 found = 1;
2424 break;
2425 }
2426 if (match->schema == schema) {
2427 /* current node (match) is a data node still before the new node, continue search in data */
2428 break;
2429 }
Michal Vasko93b16062020-12-09 18:14:18 +01002430 schema = lys_getnext(schema, sparent, new_node->schema->module->compiled, getnext_opts);
Michal Vaskob104f112020-07-17 09:54:54 +02002431 assert(schema);
2432 }
2433
2434 if (found && (match->schema != new_node->schema)) {
2435 /* find the next node after we have found our node schema data instance */
2436 break;
2437 }
2438 }
Michal Vasko90932a92020-02-12 14:33:03 +01002439 }
2440
2441 return match;
2442}
2443
2444/**
2445 * @brief Insert node after a sibling.
2446 *
Michal Vasko9f96a052020-03-10 09:41:45 +01002447 * Handles inserting into NP containers and key-less lists.
2448 *
Michal Vasko90932a92020-02-12 14:33:03 +01002449 * @param[in] sibling Sibling to insert after.
2450 * @param[in] node Node to insert.
2451 */
2452static void
Michal Vaskof03ed032020-03-04 13:31:44 +01002453lyd_insert_after_node(struct lyd_node *sibling, struct lyd_node *node)
Michal Vasko90932a92020-02-12 14:33:03 +01002454{
Michal Vasko0249f7c2020-03-05 16:36:40 +01002455 struct lyd_node_inner *par;
2456
Michal Vasko90932a92020-02-12 14:33:03 +01002457 assert(!node->next && (node->prev == node));
2458
2459 node->next = sibling->next;
2460 node->prev = sibling;
2461 sibling->next = node;
2462 if (node->next) {
2463 /* sibling had a succeeding node */
2464 node->next->prev = node;
2465 } else {
2466 /* sibling was last, find first sibling and change its prev */
2467 if (sibling->parent) {
2468 sibling = sibling->parent->child;
2469 } else {
Michal Vaskod989ba02020-08-24 10:59:24 +02002470 for ( ; sibling->prev->next != node; sibling = sibling->prev) {}
Michal Vasko90932a92020-02-12 14:33:03 +01002471 }
2472 sibling->prev = node;
2473 }
2474 node->parent = sibling->parent;
Michal Vasko0249f7c2020-03-05 16:36:40 +01002475
Michal Vasko9f96a052020-03-10 09:41:45 +01002476 for (par = node->parent; par; par = par->parent) {
2477 if ((par->flags & LYD_DEFAULT) && !(node->flags & LYD_DEFAULT)) {
2478 /* remove default flags from NP containers */
Michal Vasko0249f7c2020-03-05 16:36:40 +01002479 par->flags &= ~LYD_DEFAULT;
2480 }
2481 }
Michal Vasko90932a92020-02-12 14:33:03 +01002482}
2483
2484/**
2485 * @brief Insert node before a sibling.
2486 *
Michal Vasko9f96a052020-03-10 09:41:45 +01002487 * Handles inserting into NP containers and key-less lists.
2488 *
Michal Vasko90932a92020-02-12 14:33:03 +01002489 * @param[in] sibling Sibling to insert before.
2490 * @param[in] node Node to insert.
2491 */
2492static void
Michal Vaskof03ed032020-03-04 13:31:44 +01002493lyd_insert_before_node(struct lyd_node *sibling, struct lyd_node *node)
Michal Vasko90932a92020-02-12 14:33:03 +01002494{
Michal Vasko0249f7c2020-03-05 16:36:40 +01002495 struct lyd_node_inner *par;
2496
Michal Vasko90932a92020-02-12 14:33:03 +01002497 assert(!node->next && (node->prev == node));
2498
2499 node->next = sibling;
2500 /* covers situation of sibling being first */
2501 node->prev = sibling->prev;
2502 sibling->prev = node;
2503 if (node->prev->next) {
2504 /* sibling had a preceding node */
2505 node->prev->next = node;
2506 } else if (sibling->parent) {
2507 /* sibling was first and we must also change parent child pointer */
2508 sibling->parent->child = node;
2509 }
2510 node->parent = sibling->parent;
Michal Vasko0249f7c2020-03-05 16:36:40 +01002511
Michal Vasko9f96a052020-03-10 09:41:45 +01002512 for (par = node->parent; par; par = par->parent) {
2513 if ((par->flags & LYD_DEFAULT) && !(node->flags & LYD_DEFAULT)) {
2514 /* remove default flags from NP containers */
Michal Vasko0249f7c2020-03-05 16:36:40 +01002515 par->flags &= ~LYD_DEFAULT;
2516 }
2517 }
Michal Vasko90932a92020-02-12 14:33:03 +01002518}
2519
2520/**
Michal Vaskob104f112020-07-17 09:54:54 +02002521 * @brief Insert node as the first and only child of a parent.
Michal Vasko90932a92020-02-12 14:33:03 +01002522 *
Michal Vasko9f96a052020-03-10 09:41:45 +01002523 * Handles inserting into NP containers and key-less lists.
2524 *
Michal Vasko90932a92020-02-12 14:33:03 +01002525 * @param[in] parent Parent to insert into.
2526 * @param[in] node Node to insert.
2527 */
2528static void
Michal Vaskob104f112020-07-17 09:54:54 +02002529lyd_insert_only_child(struct lyd_node *parent, struct lyd_node *node)
Michal Vasko90932a92020-02-12 14:33:03 +01002530{
2531 struct lyd_node_inner *par;
2532
Radek Krejcia1c1e542020-09-29 16:06:52 +02002533 assert(parent && !lyd_child(parent) && !node->next && (node->prev == node));
Michal Vasko52927e22020-03-16 17:26:14 +01002534 assert(!parent->schema || (parent->schema->nodetype & LYD_NODE_INNER));
Michal Vasko90932a92020-02-12 14:33:03 +01002535
2536 par = (struct lyd_node_inner *)parent;
2537
Michal Vaskob104f112020-07-17 09:54:54 +02002538 par->child = node;
Michal Vasko90932a92020-02-12 14:33:03 +01002539 node->parent = par;
Michal Vasko0249f7c2020-03-05 16:36:40 +01002540
Michal Vaskod989ba02020-08-24 10:59:24 +02002541 for ( ; par; par = par->parent) {
Michal Vasko9f96a052020-03-10 09:41:45 +01002542 if ((par->flags & LYD_DEFAULT) && !(node->flags & LYD_DEFAULT)) {
2543 /* remove default flags from NP containers */
Michal Vasko0249f7c2020-03-05 16:36:40 +01002544 par->flags &= ~LYD_DEFAULT;
2545 }
2546 }
Michal Vasko751cb4d2020-07-14 12:25:28 +02002547}
Michal Vasko0249f7c2020-03-05 16:36:40 +01002548
Michal Vasko751cb4d2020-07-14 12:25:28 +02002549/**
2550 * @brief Learn whether a list instance has all the keys.
2551 *
2552 * @param[in] list List instance to check.
2553 * @return non-zero if all the keys were found,
2554 * @return 0 otherwise.
2555 */
2556static int
2557lyd_insert_has_keys(const struct lyd_node *list)
2558{
2559 const struct lyd_node *key;
2560 const struct lysc_node *skey = NULL;
2561
2562 assert(list->schema->nodetype == LYS_LIST);
Radek Krejcia1c1e542020-09-29 16:06:52 +02002563 key = lyd_child(list);
Michal Vasko751cb4d2020-07-14 12:25:28 +02002564 while ((skey = lys_getnext(skey, list->schema, NULL, 0)) && (skey->flags & LYS_KEY)) {
2565 if (!key || (key->schema != skey)) {
2566 /* key missing */
2567 return 0;
2568 }
2569
2570 key = key->next;
2571 }
2572
2573 /* all keys found */
2574 return 1;
Michal Vasko90932a92020-02-12 14:33:03 +01002575}
2576
2577void
Michal Vasko6ee6f432021-07-16 09:49:14 +02002578lyd_insert_node(struct lyd_node *parent, struct lyd_node **first_sibling_p, struct lyd_node *node, ly_bool last)
Michal Vasko90932a92020-02-12 14:33:03 +01002579{
Michal Vaskob104f112020-07-17 09:54:54 +02002580 struct lyd_node *anchor, *first_sibling;
Michal Vasko90932a92020-02-12 14:33:03 +01002581
Michal Vaskob104f112020-07-17 09:54:54 +02002582 /* inserting list without its keys is not supported */
2583 assert((parent || first_sibling_p) && node && (node->hash || !node->schema));
Michal Vaskof756c942020-11-06 17:21:37 +01002584 assert(!parent || !parent->schema ||
2585 (parent->schema->nodetype & (LYS_CONTAINER | LYS_LIST | LYS_RPC | LYS_ACTION | LYS_NOTIF)));
Michal Vasko9b368d32020-02-14 13:53:31 +01002586
Michal Vaskob104f112020-07-17 09:54:54 +02002587 if (!parent && first_sibling_p && (*first_sibling_p) && (*first_sibling_p)->parent) {
Michal Vasko9e685082021-01-29 14:49:09 +01002588 parent = lyd_parent(*first_sibling_p);
Michal Vasko9b368d32020-02-14 13:53:31 +01002589 }
Michal Vasko90932a92020-02-12 14:33:03 +01002590
Michal Vaskob104f112020-07-17 09:54:54 +02002591 /* get first sibling */
Michal Vasko9e685082021-01-29 14:49:09 +01002592 first_sibling = parent ? lyd_child(parent) : *first_sibling_p;
Michal Vasko9f96a052020-03-10 09:41:45 +01002593
Michal Vasko6ee6f432021-07-16 09:49:14 +02002594 if (last) {
2595 /* no next anchor */
2596 anchor = NULL;
2597 } else {
2598 /* find the anchor, our next node, so we can insert before it */
2599 anchor = lyd_insert_get_next_anchor(first_sibling, node);
2600 }
2601
Michal Vaskob104f112020-07-17 09:54:54 +02002602 if (anchor) {
Michal Vasko6ee6f432021-07-16 09:49:14 +02002603 /* insert before the anchor */
Michal Vaskob104f112020-07-17 09:54:54 +02002604 lyd_insert_before_node(anchor, node);
Michal Vasko26123192020-11-09 21:02:34 +01002605 if (!parent && (*first_sibling_p == anchor)) {
2606 /* move first sibling */
2607 *first_sibling_p = node;
2608 }
Michal Vaskob104f112020-07-17 09:54:54 +02002609 } else if (first_sibling) {
Michal Vasko6ee6f432021-07-16 09:49:14 +02002610 /* insert as the last node */
Michal Vaskob104f112020-07-17 09:54:54 +02002611 lyd_insert_after_node(first_sibling->prev, node);
2612 } else if (parent) {
Michal Vasko6ee6f432021-07-16 09:49:14 +02002613 /* insert as the only child */
Michal Vaskob104f112020-07-17 09:54:54 +02002614 lyd_insert_only_child(parent, node);
Michal Vasko90932a92020-02-12 14:33:03 +01002615 } else {
Michal Vasko6ee6f432021-07-16 09:49:14 +02002616 /* insert as the only sibling */
Michal Vaskob104f112020-07-17 09:54:54 +02002617 *first_sibling_p = node;
2618 }
2619
2620 /* insert into parent HT */
2621 lyd_insert_hash(node);
2622
2623 /* finish hashes for our parent, if needed and possible */
Michal Vasko9e8de2d2020-09-01 08:17:10 +02002624 if (node->schema && (node->schema->flags & LYS_KEY) && parent && lyd_insert_has_keys(parent)) {
Michal Vaskob104f112020-07-17 09:54:54 +02002625 lyd_hash(parent);
2626
2627 /* now we can insert even the list into its parent HT */
2628 lyd_insert_hash(parent);
Michal Vasko90932a92020-02-12 14:33:03 +01002629 }
Michal Vasko90932a92020-02-12 14:33:03 +01002630}
2631
Michal Vasko717a4c32020-12-07 09:40:10 +01002632/**
2633 * @brief Check schema place of a node to be inserted.
2634 *
2635 * @param[in] parent Schema node of the parent data node.
2636 * @param[in] sibling Schema node of a sibling data node.
2637 * @param[in] schema Schema node if the data node to be inserted.
2638 * @return LY_SUCCESS on success.
2639 * @return LY_EINVAL if the place is invalid.
2640 */
Michal Vaskof03ed032020-03-04 13:31:44 +01002641static LY_ERR
Michal Vasko717a4c32020-12-07 09:40:10 +01002642lyd_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 +01002643{
2644 const struct lysc_node *par2;
2645
Michal Vasko62ed12d2020-05-21 10:08:25 +02002646 assert(!parent || !(parent->nodetype & (LYS_CASE | LYS_CHOICE)));
Michal Vasko717a4c32020-12-07 09:40:10 +01002647 assert(!sibling || !(sibling->nodetype & (LYS_CASE | LYS_CHOICE)));
2648 assert(!schema || !(schema->nodetype & (LYS_CASE | LYS_CHOICE)));
Michal Vaskof03ed032020-03-04 13:31:44 +01002649
Michal Vasko717a4c32020-12-07 09:40:10 +01002650 if (!schema || (!parent && !sibling)) {
Michal Vasko71e795e2020-12-04 16:27:37 +01002651 /* opaque nodes can be inserted wherever */
2652 return LY_SUCCESS;
2653 }
2654
Michal Vasko717a4c32020-12-07 09:40:10 +01002655 if (!parent) {
2656 parent = lysc_data_parent(sibling);
2657 }
2658
Michal Vaskof03ed032020-03-04 13:31:44 +01002659 /* find schema parent */
Michal Vasko62ed12d2020-05-21 10:08:25 +02002660 par2 = lysc_data_parent(schema);
Michal Vaskof03ed032020-03-04 13:31:44 +01002661
2662 if (parent) {
2663 /* inner node */
2664 if (par2 != parent) {
Michal Vaskob104f112020-07-17 09:54:54 +02002665 LOGERR(schema->module->ctx, LY_EINVAL, "Cannot insert, parent of \"%s\" is not \"%s\".", schema->name,
Michal Vasko69730152020-10-09 16:30:07 +02002666 parent->name);
Michal Vaskof03ed032020-03-04 13:31:44 +01002667 return LY_EINVAL;
2668 }
2669 } else {
2670 /* top-level node */
2671 if (par2) {
Radek Krejcif6d14cb2020-07-02 16:11:45 +02002672 LOGERR(schema->module->ctx, LY_EINVAL, "Cannot insert, node \"%s\" is not top-level.", schema->name);
Michal Vaskof03ed032020-03-04 13:31:44 +01002673 return LY_EINVAL;
2674 }
2675 }
2676
2677 return LY_SUCCESS;
2678}
2679
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002680LIBYANG_API_DEF LY_ERR
Michal Vaskob104f112020-07-17 09:54:54 +02002681lyd_insert_child(struct lyd_node *parent, struct lyd_node *node)
Michal Vaskof03ed032020-03-04 13:31:44 +01002682{
2683 struct lyd_node *iter;
2684
Michal Vasko0ab974d2021-02-24 13:18:26 +01002685 LY_CHECK_ARG_RET(NULL, parent, node, !parent->schema || (parent->schema->nodetype & LYD_NODE_INNER), LY_EINVAL);
Michal Vasko892f5bf2021-11-24 10:41:05 +01002686 LY_CHECK_CTX_EQUAL_RET(LYD_CTX(parent), LYD_CTX(node), LY_EINVAL);
Michal Vaskof03ed032020-03-04 13:31:44 +01002687
Michal Vasko717a4c32020-12-07 09:40:10 +01002688 LY_CHECK_RET(lyd_insert_check_schema(parent->schema, NULL, node->schema));
Michal Vaskof03ed032020-03-04 13:31:44 +01002689
Michal Vasko0ab974d2021-02-24 13:18:26 +01002690 if (node->schema && (node->schema->flags & LYS_KEY)) {
Michal Vaskoddd76592022-01-17 13:34:48 +01002691 LOGERR(LYD_CTX(parent), LY_EINVAL, "Cannot insert key \"%s\".", node->schema->name);
Michal Vaskof03ed032020-03-04 13:31:44 +01002692 return LY_EINVAL;
2693 }
2694
2695 if (node->parent || node->prev->next) {
2696 lyd_unlink_tree(node);
2697 }
2698
2699 while (node) {
2700 iter = node->next;
2701 lyd_unlink_tree(node);
Michal Vasko6ee6f432021-07-16 09:49:14 +02002702 lyd_insert_node(parent, NULL, node, 0);
Michal Vaskof03ed032020-03-04 13:31:44 +01002703 node = iter;
2704 }
2705 return LY_SUCCESS;
2706}
2707
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002708LIBYANG_API_DEF LY_ERR
Michal Vaskoddd76592022-01-17 13:34:48 +01002709lyd_insert_ext(struct lyd_node *parent, struct lyd_node *first)
2710{
2711 struct lyd_node *iter;
2712
2713 LY_CHECK_ARG_RET(NULL, parent, first, !first->parent, !first->prev->next,
2714 !parent->schema || (parent->schema->nodetype & LYD_NODE_INNER), LY_EINVAL);
2715
2716 if (first->schema && (first->schema->flags & LYS_KEY)) {
2717 LOGERR(LYD_CTX(parent), LY_EINVAL, "Cannot insert key \"%s\".", first->schema->name);
2718 return LY_EINVAL;
2719 }
2720
2721 while (first) {
2722 iter = first->next;
2723 lyd_unlink_tree(first);
Michal Vasko61ad1ff2022-02-10 15:48:39 +01002724 lyd_insert_node(parent, NULL, first, 1);
Michal Vaskoddd76592022-01-17 13:34:48 +01002725 first = iter;
2726 }
2727 return LY_SUCCESS;
2728}
2729
2730LIBYANG_API_DEF LY_ERR
Michal Vaskob104f112020-07-17 09:54:54 +02002731lyd_insert_sibling(struct lyd_node *sibling, struct lyd_node *node, struct lyd_node **first)
Michal Vaskob1b5c262020-03-05 14:29:47 +01002732{
2733 struct lyd_node *iter;
2734
Michal Vaskob104f112020-07-17 09:54:54 +02002735 LY_CHECK_ARG_RET(NULL, node, LY_EINVAL);
Michal Vasko892f5bf2021-11-24 10:41:05 +01002736 LY_CHECK_CTX_EQUAL_RET(sibling ? LYD_CTX(sibling) : NULL, LYD_CTX(node), LY_EINVAL);
Michal Vaskob1b5c262020-03-05 14:29:47 +01002737
Michal Vaskob104f112020-07-17 09:54:54 +02002738 if (sibling) {
Michal Vasko717a4c32020-12-07 09:40:10 +01002739 LY_CHECK_RET(lyd_insert_check_schema(NULL, sibling->schema, node->schema));
Michal Vaskob1b5c262020-03-05 14:29:47 +01002740 }
2741
Michal Vasko553d0b52020-12-04 16:27:52 +01002742 if (sibling == node) {
2743 /* we need to keep the connection to siblings so we can insert into them */
2744 sibling = sibling->prev;
2745 }
2746
Michal Vaskob1b5c262020-03-05 14:29:47 +01002747 if (node->parent || node->prev->next) {
2748 lyd_unlink_tree(node);
2749 }
2750
2751 while (node) {
Michal Vasko71e795e2020-12-04 16:27:37 +01002752 if (lysc_is_key(node->schema)) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02002753 LOGERR(LYD_CTX(node), LY_EINVAL, "Cannot insert key \"%s\".", node->schema->name);
Michal Vaskob104f112020-07-17 09:54:54 +02002754 return LY_EINVAL;
2755 }
2756
Michal Vaskob1b5c262020-03-05 14:29:47 +01002757 iter = node->next;
2758 lyd_unlink_tree(node);
Michal Vasko6ee6f432021-07-16 09:49:14 +02002759 lyd_insert_node(NULL, &sibling, node, 0);
Michal Vaskob1b5c262020-03-05 14:29:47 +01002760 node = iter;
2761 }
Michal Vaskob1b5c262020-03-05 14:29:47 +01002762
Michal Vaskob104f112020-07-17 09:54:54 +02002763 if (first) {
2764 /* find the first sibling */
2765 *first = sibling;
2766 while ((*first)->prev->next) {
2767 *first = (*first)->prev;
Michal Vasko0249f7c2020-03-05 16:36:40 +01002768 }
2769 }
2770
2771 return LY_SUCCESS;
2772}
2773
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002774LIBYANG_API_DEF LY_ERR
Michal Vaskof03ed032020-03-04 13:31:44 +01002775lyd_insert_before(struct lyd_node *sibling, struct lyd_node *node)
2776{
Michal Vaskof03ed032020-03-04 13:31:44 +01002777 LY_CHECK_ARG_RET(NULL, sibling, node, LY_EINVAL);
Michal Vasko892f5bf2021-11-24 10:41:05 +01002778 LY_CHECK_CTX_EQUAL_RET(LYD_CTX(sibling), LYD_CTX(node), LY_EINVAL);
Michal Vaskof03ed032020-03-04 13:31:44 +01002779
Michal Vasko717a4c32020-12-07 09:40:10 +01002780 LY_CHECK_RET(lyd_insert_check_schema(NULL, sibling->schema, node->schema));
Michal Vaskof03ed032020-03-04 13:31:44 +01002781
Michal Vaskob104f112020-07-17 09:54:54 +02002782 if (!(node->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) || !(node->schema->flags & LYS_ORDBY_USER)) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02002783 LOGERR(LYD_CTX(sibling), LY_EINVAL, "Can be used only for user-ordered nodes.");
Michal Vaskof03ed032020-03-04 13:31:44 +01002784 return LY_EINVAL;
2785 }
Michal Vasko7508ef22022-02-22 14:13:10 +01002786 if (lysc_is_key(sibling->schema)) {
2787 LOGERR(LYD_CTX(sibling), LY_EINVAL, "Cannot insert before keys.");
2788 return LY_EINVAL;
2789 }
Michal Vaskof03ed032020-03-04 13:31:44 +01002790
Michal Vasko4bc2ce32020-12-08 10:06:16 +01002791 lyd_unlink_tree(node);
2792 lyd_insert_before_node(sibling, node);
2793 lyd_insert_hash(node);
Michal Vaskof03ed032020-03-04 13:31:44 +01002794
Michal Vaskof03ed032020-03-04 13:31:44 +01002795 return LY_SUCCESS;
2796}
2797
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002798LIBYANG_API_DEF LY_ERR
Michal Vaskof03ed032020-03-04 13:31:44 +01002799lyd_insert_after(struct lyd_node *sibling, struct lyd_node *node)
2800{
Michal Vaskof03ed032020-03-04 13:31:44 +01002801 LY_CHECK_ARG_RET(NULL, sibling, node, LY_EINVAL);
Michal Vasko892f5bf2021-11-24 10:41:05 +01002802 LY_CHECK_CTX_EQUAL_RET(LYD_CTX(sibling), LYD_CTX(node), LY_EINVAL);
Michal Vaskof03ed032020-03-04 13:31:44 +01002803
Michal Vasko717a4c32020-12-07 09:40:10 +01002804 LY_CHECK_RET(lyd_insert_check_schema(NULL, sibling->schema, node->schema));
Michal Vaskof03ed032020-03-04 13:31:44 +01002805
Michal Vaskob104f112020-07-17 09:54:54 +02002806 if (!(node->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) || !(node->schema->flags & LYS_ORDBY_USER)) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02002807 LOGERR(LYD_CTX(sibling), LY_EINVAL, "Can be used only for user-ordered nodes.");
Michal Vaskof03ed032020-03-04 13:31:44 +01002808 return LY_EINVAL;
2809 }
Michal Vasko7508ef22022-02-22 14:13:10 +01002810 if (sibling->next && lysc_is_key(sibling->next->schema)) {
2811 LOGERR(LYD_CTX(sibling), LY_EINVAL, "Cannot insert before keys.");
2812 return LY_EINVAL;
2813 }
Michal Vaskof03ed032020-03-04 13:31:44 +01002814
Michal Vasko4bc2ce32020-12-08 10:06:16 +01002815 lyd_unlink_tree(node);
2816 lyd_insert_after_node(sibling, node);
2817 lyd_insert_hash(node);
Michal Vaskof03ed032020-03-04 13:31:44 +01002818
Michal Vaskof03ed032020-03-04 13:31:44 +01002819 return LY_SUCCESS;
2820}
2821
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002822LIBYANG_API_DEF void
Michal Vasko66d508c2021-07-21 16:07:09 +02002823lyd_unlink_siblings(struct lyd_node *node)
2824{
2825 struct lyd_node *next, *elem, *first = NULL;
2826
2827 LY_LIST_FOR_SAFE(node, next, elem) {
2828 lyd_unlink_tree(elem);
2829 lyd_insert_node(NULL, &first, elem, 1);
2830 }
2831}
2832
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002833LIBYANG_API_DEF void
Michal Vaskof03ed032020-03-04 13:31:44 +01002834lyd_unlink_tree(struct lyd_node *node)
2835{
2836 struct lyd_node *iter;
2837
2838 if (!node) {
2839 return;
2840 }
2841
Michal Vaskob104f112020-07-17 09:54:54 +02002842 /* update hashes while still linked into the tree */
2843 lyd_unlink_hash(node);
2844
Michal Vaskof03ed032020-03-04 13:31:44 +01002845 /* unlink from siblings */
2846 if (node->prev->next) {
2847 node->prev->next = node->next;
2848 }
2849 if (node->next) {
2850 node->next->prev = node->prev;
2851 } else {
2852 /* unlinking the last node */
2853 if (node->parent) {
2854 iter = node->parent->child;
2855 } else {
2856 iter = node->prev;
2857 while (iter->prev != node) {
2858 iter = iter->prev;
2859 }
2860 }
2861 /* update the "last" pointer from the first node */
2862 iter->prev = node->prev;
2863 }
2864
2865 /* unlink from parent */
2866 if (node->parent) {
2867 if (node->parent->child == node) {
2868 /* the node is the first child */
2869 node->parent->child = node->next;
2870 }
2871
Michal Vaskoab49dbe2020-07-17 12:32:47 +02002872 /* check for NP container whether its last non-default node is not being unlinked */
Michal Vasko69730152020-10-09 16:30:07 +02002873 if (node->parent->schema && (node->parent->schema->nodetype == LYS_CONTAINER) &&
2874 !(node->parent->flags & LYD_DEFAULT) && !(node->parent->schema->flags & LYS_PRESENCE)) {
Michal Vaskoab49dbe2020-07-17 12:32:47 +02002875 LY_LIST_FOR(node->parent->child, iter) {
2876 if ((iter != node) && !(iter->flags & LYD_DEFAULT)) {
2877 break;
2878 }
2879 }
2880 if (!iter) {
2881 node->parent->flags |= LYD_DEFAULT;
2882 }
2883 }
2884
Michal Vaskof03ed032020-03-04 13:31:44 +01002885 node->parent = NULL;
2886 }
2887
2888 node->next = NULL;
2889 node->prev = node;
2890}
2891
Michal Vaskoa5da3292020-08-12 13:10:50 +02002892void
Michal Vasko871a0252020-11-11 18:35:24 +01002893lyd_insert_meta(struct lyd_node *parent, struct lyd_meta *meta, ly_bool clear_dflt)
Radek Krejci1798aae2020-07-14 13:26:06 +02002894{
2895 struct lyd_meta *last, *iter;
2896
2897 assert(parent);
Michal Vaskoa5da3292020-08-12 13:10:50 +02002898
2899 if (!meta) {
2900 return;
2901 }
Radek Krejci1798aae2020-07-14 13:26:06 +02002902
2903 for (iter = meta; iter; iter = iter->next) {
2904 iter->parent = parent;
2905 }
2906
2907 /* insert as the last attribute */
2908 if (parent->meta) {
Radek Krejci1e008d22020-08-17 11:37:37 +02002909 for (last = parent->meta; last->next; last = last->next) {}
Radek Krejci1798aae2020-07-14 13:26:06 +02002910 last->next = meta;
2911 } else {
2912 parent->meta = meta;
2913 }
2914
2915 /* remove default flags from NP containers */
Michal Vasko871a0252020-11-11 18:35:24 +01002916 while (clear_dflt && parent && (parent->schema->nodetype == LYS_CONTAINER) && (parent->flags & LYD_DEFAULT)) {
Radek Krejci1798aae2020-07-14 13:26:06 +02002917 parent->flags &= ~LYD_DEFAULT;
Michal Vasko9e685082021-01-29 14:49:09 +01002918 parent = lyd_parent(parent);
Radek Krejci1798aae2020-07-14 13:26:06 +02002919 }
Radek Krejci1798aae2020-07-14 13:26:06 +02002920}
2921
2922LY_ERR
Michal Vasko9f96a052020-03-10 09:41:45 +01002923lyd_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 +02002924 size_t name_len, const char *value, size_t value_len, ly_bool *dynamic, LY_VALUE_FORMAT format,
Michal Vaskoddd76592022-01-17 13:34:48 +01002925 void *prefix_data, uint32_t hints, const struct lysc_node *ctx_node, ly_bool clear_dflt, ly_bool *incomplete)
Michal Vasko90932a92020-02-12 14:33:03 +01002926{
Radek Krejci2efc45b2020-12-22 16:25:44 +01002927 LY_ERR ret = LY_SUCCESS;
Michal Vasko90932a92020-02-12 14:33:03 +01002928 struct lysc_ext_instance *ant = NULL;
Michal Vaskob0534f92021-07-01 13:24:03 +02002929 const struct lysc_type **ant_type;
Michal Vasko9f96a052020-03-10 09:41:45 +01002930 struct lyd_meta *mt, *last;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002931 LY_ARRAY_COUNT_TYPE u;
Michal Vasko90932a92020-02-12 14:33:03 +01002932
Michal Vasko9f96a052020-03-10 09:41:45 +01002933 assert((parent || meta) && mod);
Michal Vasko6f4cbb62020-02-28 11:15:47 +01002934
Michal Vaskoddd76592022-01-17 13:34:48 +01002935 LOG_LOCSET(ctx_node, parent, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +01002936
Radek Krejci7eb54ba2020-05-18 16:30:04 +02002937 LY_ARRAY_FOR(mod->compiled->exts, u) {
Radek Krejci3e6632f2021-03-22 22:08:21 +01002938 if ((mod->compiled->exts[u].def->plugin == lyplg_find(LYPLG_EXTENSION, LYEXT_PLUGIN_INTERNAL_ANNOTATION)) &&
Radek Krejci7eb54ba2020-05-18 16:30:04 +02002939 !ly_strncmp(mod->compiled->exts[u].argument, name, name_len)) {
Michal Vasko90932a92020-02-12 14:33:03 +01002940 /* we have the annotation definition */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02002941 ant = &mod->compiled->exts[u];
Michal Vasko90932a92020-02-12 14:33:03 +01002942 break;
2943 }
2944 }
2945 if (!ant) {
2946 /* attribute is not defined as a metadata annotation (RFC 7952) */
Radek Krejci2efc45b2020-12-22 16:25:44 +01002947 LOGVAL(mod->ctx, LYVE_REFERENCE, "Annotation definition for attribute \"%s:%.*s\" not found.",
Radek Krejci422afb12021-03-04 16:38:16 +01002948 mod->name, (int)name_len, name);
Radek Krejci2efc45b2020-12-22 16:25:44 +01002949 ret = LY_EINVAL;
2950 goto cleanup;
Michal Vasko90932a92020-02-12 14:33:03 +01002951 }
2952
Michal Vasko9f96a052020-03-10 09:41:45 +01002953 mt = calloc(1, sizeof *mt);
Radek Krejci2efc45b2020-12-22 16:25:44 +01002954 LY_CHECK_ERR_GOTO(!mt, LOGMEM(mod->ctx); ret = LY_EMEM, cleanup);
Michal Vasko9f96a052020-03-10 09:41:45 +01002955 mt->parent = parent;
2956 mt->annotation = ant;
Michal Vaskob0534f92021-07-01 13:24:03 +02002957 ant_type = ant->substmts[ANNOTATION_SUBSTMT_TYPE].storage;
2958 ret = lyd_value_store(mod->ctx, &mt->value, *ant_type, value, value_len, dynamic, format, prefix_data, hints,
Michal Vaskoddd76592022-01-17 13:34:48 +01002959 ctx_node, incomplete);
Radek Krejci2efc45b2020-12-22 16:25:44 +01002960 LY_CHECK_ERR_GOTO(ret, free(mt), cleanup);
2961 ret = lydict_insert(mod->ctx, name, name_len, &mt->name);
2962 LY_CHECK_ERR_GOTO(ret, free(mt), cleanup);
Michal Vasko90932a92020-02-12 14:33:03 +01002963
Michal Vasko6f4cbb62020-02-28 11:15:47 +01002964 /* insert as the last attribute */
2965 if (parent) {
Michal Vasko871a0252020-11-11 18:35:24 +01002966 lyd_insert_meta(parent, mt, clear_dflt);
Michal Vasko9f96a052020-03-10 09:41:45 +01002967 } else if (*meta) {
Radek Krejci1e008d22020-08-17 11:37:37 +02002968 for (last = *meta; last->next; last = last->next) {}
Michal Vasko9f96a052020-03-10 09:41:45 +01002969 last->next = mt;
Michal Vasko90932a92020-02-12 14:33:03 +01002970 }
2971
Michal Vasko9f96a052020-03-10 09:41:45 +01002972 if (meta) {
2973 *meta = mt;
Michal Vasko90932a92020-02-12 14:33:03 +01002974 }
Radek Krejci2efc45b2020-12-22 16:25:44 +01002975
2976cleanup:
Michal Vaskoddd76592022-01-17 13:34:48 +01002977 LOG_LOCBACK(ctx_node ? 1 : 0, parent ? 1 : 0, 0, 0);
Radek Krejci2efc45b2020-12-22 16:25:44 +01002978 return ret;
Michal Vasko90932a92020-02-12 14:33:03 +01002979}
2980
Michal Vaskoa5da3292020-08-12 13:10:50 +02002981void
2982lyd_insert_attr(struct lyd_node *parent, struct lyd_attr *attr)
2983{
2984 struct lyd_attr *last, *iter;
2985 struct lyd_node_opaq *opaq;
2986
2987 assert(parent && !parent->schema);
2988
2989 if (!attr) {
2990 return;
2991 }
2992
2993 opaq = (struct lyd_node_opaq *)parent;
2994 for (iter = attr; iter; iter = iter->next) {
2995 iter->parent = opaq;
2996 }
2997
2998 /* insert as the last attribute */
2999 if (opaq->attr) {
Radek Krejci1e008d22020-08-17 11:37:37 +02003000 for (last = opaq->attr; last->next; last = last->next) {}
Michal Vaskoa5da3292020-08-12 13:10:50 +02003001 last->next = attr;
3002 } else {
3003 opaq->attr = attr;
3004 }
3005}
3006
Michal Vasko52927e22020-03-16 17:26:14 +01003007LY_ERR
Michal Vaskofeca4fb2020-10-05 08:58:40 +02003008lyd_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 +01003009 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 +02003010 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 +01003011{
Radek Krejci011e4aa2020-09-04 15:22:31 +02003012 LY_ERR ret = LY_SUCCESS;
Radek Krejci1798aae2020-07-14 13:26:06 +02003013 struct lyd_attr *at, *last;
Michal Vasko52927e22020-03-16 17:26:14 +01003014
3015 assert(ctx && (parent || attr) && (!parent || !parent->schema));
Michal Vaskofeca4fb2020-10-05 08:58:40 +02003016 assert(name && name_len && format);
Michal Vasko52927e22020-03-16 17:26:14 +01003017
Michal Vasko2a3722d2021-06-16 11:52:39 +02003018 if (!value_len && (!dynamic || !*dynamic)) {
Michal Vasko52927e22020-03-16 17:26:14 +01003019 value = "";
3020 }
3021
3022 at = calloc(1, sizeof *at);
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01003023 LY_CHECK_ERR_RET(!at, LOGMEM(ctx); ly_free_prefix_data(format, val_prefix_data), LY_EMEM);
Radek Krejcid46e46a2020-09-15 14:22:42 +02003024
Michal Vaskoad92b672020-11-12 13:11:31 +01003025 LY_CHECK_GOTO(ret = lydict_insert(ctx, name, name_len, &at->name.name), finish);
Michal Vasko501af032020-11-11 20:27:44 +01003026 if (prefix_len) {
Michal Vaskoad92b672020-11-12 13:11:31 +01003027 LY_CHECK_GOTO(ret = lydict_insert(ctx, prefix, prefix_len, &at->name.prefix), finish);
Michal Vasko501af032020-11-11 20:27:44 +01003028 }
3029 if (module_key_len) {
Michal Vaskoad92b672020-11-12 13:11:31 +01003030 LY_CHECK_GOTO(ret = lydict_insert(ctx, module_key, module_key_len, &at->name.module_ns), finish);
Michal Vasko501af032020-11-11 20:27:44 +01003031 }
3032
Michal Vasko52927e22020-03-16 17:26:14 +01003033 if (dynamic && *dynamic) {
Radek Krejci011e4aa2020-09-04 15:22:31 +02003034 ret = lydict_insert_zc(ctx, (char *)value, &at->value);
3035 LY_CHECK_GOTO(ret, finish);
Michal Vasko52927e22020-03-16 17:26:14 +01003036 *dynamic = 0;
3037 } else {
Radek Krejci011e4aa2020-09-04 15:22:31 +02003038 LY_CHECK_GOTO(ret = lydict_insert(ctx, value, value_len, &at->value), finish);
Michal Vasko52927e22020-03-16 17:26:14 +01003039 }
Michal Vasko501af032020-11-11 20:27:44 +01003040 at->format = format;
3041 at->val_prefix_data = val_prefix_data;
3042 at->hints = hints;
Michal Vasko52927e22020-03-16 17:26:14 +01003043
3044 /* insert as the last attribute */
3045 if (parent) {
Michal Vaskoa5da3292020-08-12 13:10:50 +02003046 lyd_insert_attr(parent, at);
Michal Vasko52927e22020-03-16 17:26:14 +01003047 } else if (*attr) {
Radek Krejci1e008d22020-08-17 11:37:37 +02003048 for (last = *attr; last->next; last = last->next) {}
Michal Vasko52927e22020-03-16 17:26:14 +01003049 last->next = at;
3050 }
3051
Radek Krejci011e4aa2020-09-04 15:22:31 +02003052finish:
3053 if (ret) {
3054 lyd_free_attr_single(ctx, at);
3055 } else if (attr) {
Michal Vasko52927e22020-03-16 17:26:14 +01003056 *attr = at;
3057 }
3058 return LY_SUCCESS;
3059}
3060
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01003061LIBYANG_API_DEF const struct lyd_node_term *
Michal Vasko004d3152020-06-11 19:59:22 +02003062lyd_target(const struct ly_path *path, const struct lyd_node *tree)
Radek Krejci084289f2019-07-09 17:35:30 +02003063{
Michal Vasko004d3152020-06-11 19:59:22 +02003064 struct lyd_node *target;
Radek Krejci084289f2019-07-09 17:35:30 +02003065
Michal Vasko004d3152020-06-11 19:59:22 +02003066 if (ly_path_eval(path, tree, &target)) {
3067 return NULL;
Radek Krejci084289f2019-07-09 17:35:30 +02003068 }
3069
Michal Vasko004d3152020-06-11 19:59:22 +02003070 return (struct lyd_node_term *)target;
Radek Krejci084289f2019-07-09 17:35:30 +02003071}
3072
aPiecek2f63f952021-03-30 12:22:18 +02003073/**
3074 * @brief Check the equality of the two schemas from different contexts.
3075 *
3076 * @param schema1 of first node.
3077 * @param schema2 of second node.
3078 * @return 1 if the schemas are equal otherwise 0.
3079 */
3080static ly_bool
3081lyd_compare_schema_equal(const struct lysc_node *schema1, const struct lysc_node *schema2)
3082{
3083 if (!schema1 && !schema2) {
3084 return 1;
3085 } else if (!schema1 || !schema2) {
3086 return 0;
3087 }
3088
3089 assert(schema1->module->ctx != schema2->module->ctx);
3090
3091 if (schema1->nodetype != schema2->nodetype) {
3092 return 0;
3093 }
3094
3095 if (strcmp(schema1->name, schema2->name)) {
3096 return 0;
3097 }
3098
3099 if (strcmp(schema1->module->name, schema2->module->name)) {
3100 return 0;
3101 }
3102
3103 if (schema1->module->revision || schema2->module->revision) {
3104 if (!schema1->module->revision || !schema2->module->revision) {
3105 return 0;
3106 }
3107 if (strcmp(schema1->module->revision, schema2->module->revision)) {
3108 return 0;
3109 }
3110 }
3111
3112 return 1;
3113}
3114
3115/**
3116 * @brief Check the equality of the schemas for all parent nodes.
3117 *
3118 * Both nodes must be from different contexts.
3119 *
3120 * @param node1 Data of first node.
3121 * @param node2 Data of second node.
3122 * @return 1 if the all related parental schemas are equal otherwise 0.
3123 */
3124static ly_bool
3125lyd_compare_schema_parents_equal(const struct lyd_node *node1, const struct lyd_node *node2)
3126{
3127 const struct lysc_node *parent1, *parent2;
3128
3129 assert(node1 && node2);
3130
3131 for (parent1 = node1->schema->parent, parent2 = node2->schema->parent;
3132 parent1 && parent2;
3133 parent1 = parent1->parent, parent2 = parent2->parent) {
3134 if (!lyd_compare_schema_equal(parent1, parent2)) {
3135 return 0;
3136 }
3137 }
3138
3139 if (parent1 || parent2) {
3140 return 0;
3141 }
3142
3143 return 1;
3144}
3145
3146/**
3147 * @brief Internal implementation of @ref lyd_compare_single.
3148 * @copydoc lyd_compare_single
3149 * @param[in] parental_schemas_checked Flag used for optimization.
3150 * When this function is called for the first time, the flag must be set to 0.
3151 * The @ref lyd_compare_schema_parents_equal should be called only once during
3152 * recursive calls, and this is accomplished by setting to 1 in the lyd_compare_single_ body.
3153 */
3154static LY_ERR
3155lyd_compare_single_(const struct lyd_node *node1, const struct lyd_node *node2,
3156 uint32_t options, ly_bool parental_schemas_checked)
Radek Krejci1f05b6a2019-07-18 16:15:06 +02003157{
3158 const struct lyd_node *iter1, *iter2;
3159 struct lyd_node_term *term1, *term2;
3160 struct lyd_node_any *any1, *any2;
Michal Vasko52927e22020-03-16 17:26:14 +01003161 struct lyd_node_opaq *opaq1, *opaq2;
Radek Krejci1f05b6a2019-07-18 16:15:06 +02003162 size_t len1, len2;
Radek Krejci084289f2019-07-09 17:35:30 +02003163
Radek Krejci1f05b6a2019-07-18 16:15:06 +02003164 if (!node1 || !node2) {
3165 if (node1 == node2) {
3166 return LY_SUCCESS;
3167 } else {
3168 return LY_ENOT;
3169 }
3170 }
3171
aPiecek2f63f952021-03-30 12:22:18 +02003172 if (LYD_CTX(node1) == LYD_CTX(node2)) {
3173 /* same contexts */
3174 if (node1->schema != node2->schema) {
3175 return LY_ENOT;
3176 }
3177 } else {
3178 /* different contexts */
3179 if (!lyd_compare_schema_equal(node1->schema, node2->schema)) {
3180 return LY_ENOT;
3181 }
3182 if (!parental_schemas_checked) {
3183 if (!lyd_compare_schema_parents_equal(node1, node2)) {
3184 return LY_ENOT;
3185 }
3186 parental_schemas_checked = 1;
3187 }
Radek Krejci1f05b6a2019-07-18 16:15:06 +02003188 }
3189
3190 if (node1->hash != node2->hash) {
3191 return LY_ENOT;
3192 }
aPiecek2f63f952021-03-30 12:22:18 +02003193 /* 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 +02003194
Michal Vasko52927e22020-03-16 17:26:14 +01003195 if (!node1->schema) {
3196 opaq1 = (struct lyd_node_opaq *)node1;
3197 opaq2 = (struct lyd_node_opaq *)node2;
aPiecek2f63f952021-03-30 12:22:18 +02003198 if ((strcmp(opaq1->name.name, opaq2->name.name)) || (opaq1->format != opaq2->format) ||
3199 (strcmp(opaq1->name.module_ns, opaq2->name.module_ns))) {
Radek Krejci1f05b6a2019-07-18 16:15:06 +02003200 return LY_ENOT;
3201 }
Michal Vasko52927e22020-03-16 17:26:14 +01003202 switch (opaq1->format) {
Radek Krejci8df109d2021-04-23 12:19:08 +02003203 case LY_VALUE_XML:
Radek Krejci09c77442021-04-26 11:10:34 +02003204 if (lyxml_value_compare(LYD_CTX(node1), opaq1->value, opaq1->val_prefix_data, LYD_CTX(node2), opaq2->value,
3205 opaq2->val_prefix_data)) {
Michal Vasko52927e22020-03-16 17:26:14 +01003206 return LY_ENOT;
3207 }
3208 break;
Radek Krejci8df109d2021-04-23 12:19:08 +02003209 case LY_VALUE_JSON:
Radek Krejci1798aae2020-07-14 13:26:06 +02003210 /* prefixes in JSON are unique, so it is not necessary to canonize the values */
3211 if (strcmp(opaq1->value, opaq2->value)) {
3212 return LY_ENOT;
3213 }
3214 break;
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01003215 default:
Michal Vasko52927e22020-03-16 17:26:14 +01003216 /* not allowed */
Michal Vaskob7be7a82020-08-20 09:09:04 +02003217 LOGINT(LYD_CTX(node1));
Michal Vasko52927e22020-03-16 17:26:14 +01003218 return LY_EINT;
3219 }
3220 if (options & LYD_COMPARE_FULL_RECURSION) {
3221 iter1 = opaq1->child;
3222 iter2 = opaq2->child;
Radek Krejci1f05b6a2019-07-18 16:15:06 +02003223 goto all_children_compare;
Michal Vasko52927e22020-03-16 17:26:14 +01003224 }
3225 return LY_SUCCESS;
3226 } else {
3227 switch (node1->schema->nodetype) {
3228 case LYS_LEAF:
3229 case LYS_LEAFLIST:
3230 if (options & LYD_COMPARE_DEFAULTS) {
3231 if ((node1->flags & LYD_DEFAULT) != (node2->flags & LYD_DEFAULT)) {
3232 return LY_ENOT;
3233 }
3234 }
3235
Michal Vasko1a2bf2e2020-06-16 13:07:48 +02003236 term1 = (struct lyd_node_term *)node1;
3237 term2 = (struct lyd_node_term *)node2;
aPiecek2f63f952021-03-30 12:22:18 +02003238
3239 /* same contexts */
3240 if (LYD_CTX(node1) == LYD_CTX(node2)) {
3241 return term1->value.realtype->plugin->compare(&term1->value, &term2->value);
3242 }
3243
3244 /* different contexts */
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02003245 if (strcmp(lyd_get_value(node1), lyd_get_value(node2))) {
aPiecek2f63f952021-03-30 12:22:18 +02003246 return LY_ENOT;
3247 }
3248 return LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +01003249 case LYS_CONTAINER:
3250 if (options & LYD_COMPARE_DEFAULTS) {
3251 if ((node1->flags & LYD_DEFAULT) != (node2->flags & LYD_DEFAULT)) {
3252 return LY_ENOT;
3253 }
3254 }
3255 if (options & LYD_COMPARE_FULL_RECURSION) {
Michal Vasko9e685082021-01-29 14:49:09 +01003256 iter1 = lyd_child(node1);
3257 iter2 = lyd_child(node2);
Michal Vasko52927e22020-03-16 17:26:14 +01003258 goto all_children_compare;
Radek Krejci1f05b6a2019-07-18 16:15:06 +02003259 }
3260 return LY_SUCCESS;
Michal Vasko1bf09392020-03-27 12:38:10 +01003261 case LYS_RPC:
Michal Vasko52927e22020-03-16 17:26:14 +01003262 case LYS_ACTION:
3263 if (options & LYD_COMPARE_FULL_RECURSION) {
3264 /* TODO action/RPC
3265 goto all_children_compare;
3266 */
Radek Krejci1f05b6a2019-07-18 16:15:06 +02003267 }
3268 return LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +01003269 case LYS_NOTIF:
3270 if (options & LYD_COMPARE_FULL_RECURSION) {
3271 /* TODO Notification
3272 goto all_children_compare;
3273 */
3274 }
3275 return LY_SUCCESS;
3276 case LYS_LIST:
Michal Vasko9e685082021-01-29 14:49:09 +01003277 iter1 = lyd_child(node1);
3278 iter2 = lyd_child(node2);
Michal Vasko52927e22020-03-16 17:26:14 +01003279
3280 if (!(node1->schema->flags & LYS_KEYLESS) && !(options & LYD_COMPARE_FULL_RECURSION)) {
3281 /* lists with keys, their equivalence is based on their keys */
Michal Vasko544e58a2021-01-28 14:33:41 +01003282 for (const struct lysc_node *key = lysc_node_child(node1->schema);
Michal Vaskoc57d9a92020-06-23 13:28:27 +02003283 key && (key->flags & LYS_KEY);
Michal Vasko52927e22020-03-16 17:26:14 +01003284 key = key->next) {
aPiecek2f63f952021-03-30 12:22:18 +02003285 if (lyd_compare_single_(iter1, iter2, options, parental_schemas_checked)) {
Michal Vasko52927e22020-03-16 17:26:14 +01003286 return LY_ENOT;
3287 }
3288 iter1 = iter1->next;
3289 iter2 = iter2->next;
3290 }
3291 } else {
3292 /* lists without keys, their equivalence is based on equivalence of all the children (both direct and indirect) */
3293
Radek Krejci0f969882020-08-21 16:56:47 +02003294all_children_compare:
Michal Vasko52927e22020-03-16 17:26:14 +01003295 if (!iter1 && !iter2) {
3296 /* no children, nothing to compare */
3297 return LY_SUCCESS;
3298 }
3299
Michal Vaskod989ba02020-08-24 10:59:24 +02003300 for ( ; iter1 && iter2; iter1 = iter1->next, iter2 = iter2->next) {
aPiecek2f63f952021-03-30 12:22:18 +02003301 if (lyd_compare_single_(iter1, iter2, options | LYD_COMPARE_FULL_RECURSION, parental_schemas_checked)) {
Michal Vasko52927e22020-03-16 17:26:14 +01003302 return LY_ENOT;
3303 }
3304 }
3305 if (iter1 || iter2) {
3306 return LY_ENOT;
3307 }
3308 }
3309 return LY_SUCCESS;
3310 case LYS_ANYXML:
3311 case LYS_ANYDATA:
Michal Vasko22df3f02020-08-24 13:29:22 +02003312 any1 = (struct lyd_node_any *)node1;
3313 any2 = (struct lyd_node_any *)node2;
Michal Vasko52927e22020-03-16 17:26:14 +01003314
3315 if (any1->value_type != any2->value_type) {
3316 return LY_ENOT;
3317 }
3318 switch (any1->value_type) {
3319 case LYD_ANYDATA_DATATREE:
3320 iter1 = any1->value.tree;
3321 iter2 = any2->value.tree;
3322 goto all_children_compare;
3323 case LYD_ANYDATA_STRING:
3324 case LYD_ANYDATA_XML:
3325 case LYD_ANYDATA_JSON:
3326 len1 = strlen(any1->value.str);
3327 len2 = strlen(any2->value.str);
Michal Vasko69730152020-10-09 16:30:07 +02003328 if ((len1 != len2) || strcmp(any1->value.str, any2->value.str)) {
Michal Vasko52927e22020-03-16 17:26:14 +01003329 return LY_ENOT;
3330 }
3331 return LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +01003332 case LYD_ANYDATA_LYB:
Michal Vasko60ea6352020-06-29 13:39:39 +02003333 len1 = lyd_lyb_data_length(any1->value.mem);
3334 len2 = lyd_lyb_data_length(any2->value.mem);
Michal Vasko69730152020-10-09 16:30:07 +02003335 if ((len1 != len2) || memcmp(any1->value.mem, any2->value.mem, len1)) {
Michal Vasko52927e22020-03-16 17:26:14 +01003336 return LY_ENOT;
3337 }
3338 return LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +01003339 }
Radek Krejci1f05b6a2019-07-18 16:15:06 +02003340 }
3341 }
3342
Michal Vaskob7be7a82020-08-20 09:09:04 +02003343 LOGINT(LYD_CTX(node1));
Radek Krejci1f05b6a2019-07-18 16:15:06 +02003344 return LY_EINT;
3345}
Radek Krejci22ebdba2019-07-25 13:59:43 +02003346
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01003347LIBYANG_API_DEF LY_ERR
aPiecek2f63f952021-03-30 12:22:18 +02003348lyd_compare_single(const struct lyd_node *node1, const struct lyd_node *node2, uint32_t options)
3349{
3350 return lyd_compare_single_(node1, node2, options, 0);
3351}
3352
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01003353LIBYANG_API_DEF LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003354lyd_compare_siblings(const struct lyd_node *node1, const struct lyd_node *node2, uint32_t options)
Michal Vasko8f359bf2020-07-28 10:41:15 +02003355{
Michal Vaskod989ba02020-08-24 10:59:24 +02003356 for ( ; node1 && node2; node1 = node1->next, node2 = node2->next) {
Michal Vasko8f359bf2020-07-28 10:41:15 +02003357 LY_CHECK_RET(lyd_compare_single(node1, node2, options));
3358 }
3359
Michal Vasko11a81432020-07-28 16:31:29 +02003360 if (node1 == node2) {
3361 return LY_SUCCESS;
Michal Vasko8f359bf2020-07-28 10:41:15 +02003362 }
Michal Vasko11a81432020-07-28 16:31:29 +02003363 return LY_ENOT;
Michal Vasko8f359bf2020-07-28 10:41:15 +02003364}
3365
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01003366LIBYANG_API_DEF LY_ERR
Michal Vasko21725742020-06-29 11:49:25 +02003367lyd_compare_meta(const struct lyd_meta *meta1, const struct lyd_meta *meta2)
3368{
3369 if (!meta1 || !meta2) {
3370 if (meta1 == meta2) {
3371 return LY_SUCCESS;
3372 } else {
3373 return LY_ENOT;
3374 }
3375 }
3376
Michal Vaskoa8083062020-11-06 17:22:10 +01003377 if ((meta1->annotation->module->ctx != meta2->annotation->module->ctx) || (meta1->annotation != meta2->annotation)) {
Michal Vasko21725742020-06-29 11:49:25 +02003378 return LY_ENOT;
3379 }
3380
Michal Vasko21725742020-06-29 11:49:25 +02003381 return meta1->value.realtype->plugin->compare(&meta1->value, &meta2->value);
3382}
3383
Radek Krejci22ebdba2019-07-25 13:59:43 +02003384/**
Michal Vasko9cf62422021-07-01 13:29:32 +02003385 * @brief Create a copy of the attribute.
3386 *
3387 * @param[in] attr Attribute to copy.
3388 * @param[in] node Opaque where to append the new attribute.
3389 * @param[out] dup Optional created attribute copy.
3390 * @return LY_ERR value.
3391 */
3392static LY_ERR
3393lyd_dup_attr_single(const struct lyd_attr *attr, struct lyd_node *node, struct lyd_attr **dup)
3394{
3395 LY_ERR ret = LY_SUCCESS;
3396 struct lyd_attr *a, *last;
3397 struct lyd_node_opaq *opaq = (struct lyd_node_opaq *)node;
3398
3399 LY_CHECK_ARG_RET(NULL, attr, node, !node->schema, LY_EINVAL);
3400
3401 /* create a copy */
3402 a = calloc(1, sizeof *attr);
3403 LY_CHECK_ERR_RET(!a, LOGMEM(LYD_CTX(node)), LY_EMEM);
3404
3405 LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), attr->name.name, 0, &a->name.name), finish);
3406 LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), attr->name.prefix, 0, &a->name.prefix), finish);
3407 LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), attr->name.module_ns, 0, &a->name.module_ns), finish);
3408 LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), attr->value, 0, &a->value), finish);
3409 a->hints = attr->hints;
3410 a->format = attr->format;
3411 if (attr->val_prefix_data) {
3412 ret = ly_dup_prefix_data(LYD_CTX(node), attr->format, attr->val_prefix_data, &a->val_prefix_data);
3413 LY_CHECK_GOTO(ret, finish);
3414 }
3415
3416 /* insert as the last attribute */
3417 a->parent = opaq;
3418 if (opaq->attr) {
3419 for (last = opaq->attr; last->next; last = last->next) {}
3420 last->next = a;
3421 } else {
3422 opaq->attr = a;
3423 }
3424
3425finish:
3426 if (ret) {
3427 lyd_free_attr_single(LYD_CTX(node), a);
3428 } else if (dup) {
3429 *dup = a;
3430 }
3431 return LY_SUCCESS;
3432}
3433
3434/**
Michal Vaskoddd76592022-01-17 13:34:48 +01003435 * @brief Find @p schema equivalent in @p trg_ctx.
3436 *
3437 * @param[in] schema Schema node to find.
3438 * @param[in] trg_ctx Target context to search in.
3439 * @param[in] parent Data parent of @p schema, if any.
3440 * @param[out] trg_schema Found schema from @p trg_ctx to use.
3441 * @return LY_RRR value.
3442 */
3443static LY_ERR
3444lyd_dup_find_schema(const struct lysc_node *schema, const struct ly_ctx *trg_ctx, struct lyd_node *parent,
3445 const struct lysc_node **trg_schema)
3446{
3447 const struct lysc_node *node = NULL, *sparent = NULL;
3448 const struct lys_module *mod = NULL;
3449 char *path;
3450
3451 if (!schema) {
3452 /* opaque node */
3453 *trg_schema = NULL;
3454 return LY_SUCCESS;
3455 }
3456
3457 if (parent && parent->schema) {
3458 /* start from schema parent */
3459 sparent = parent->schema;
3460 } else {
3461 /* start from module */
3462 mod = ly_ctx_get_module_implemented(trg_ctx, schema->module->name);
3463 if (!mod) {
3464 LOGERR(trg_ctx, LY_ENOTFOUND, "Module \"%s\" not present/implemented in the target context.",
3465 schema->module->name);
3466 return LY_ENOTFOUND;
3467 }
3468 }
3469
3470 /* find the schema node */
3471 while ((node = lys_getnext(node, sparent, mod ? mod->compiled : NULL, 0))) {
3472 if (!strcmp(node->module->name, schema->module->name) && !strcmp(node->name, schema->name)) {
3473 *trg_schema = node;
3474 return LY_SUCCESS;
3475 }
3476 }
3477
3478 /* schema node not found */
3479 path = lysc_path(schema, LYSC_PATH_LOG, NULL, 0);
3480 LOGERR(trg_ctx, LY_ENOTFOUND, "Schema node \"%s\" not found in the target context.", path);
3481 free(path);
3482 return LY_ENOTFOUND;
3483}
3484
3485/**
Michal Vasko52927e22020-03-16 17:26:14 +01003486 * @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 +02003487 *
Michal Vaskoddd76592022-01-17 13:34:48 +01003488 * 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 +02003489 *
Michal Vaskoddd76592022-01-17 13:34:48 +01003490 * @param[in] node Node to duplicate.
3491 * @param[in] trg_ctx Target context for duplicated nodes.
Radek Krejcif8b95172020-05-15 14:51:06 +02003492 * @param[in] parent Parent to insert into, NULL for top-level sibling.
Michal Vasko67177e52021-08-25 11:15:15 +02003493 * @param[in] insert_last Whether the duplicated node can be inserted as the last child of @p parent. Set for
3494 * recursive duplication as an optimization.
Radek Krejcif8b95172020-05-15 14:51:06 +02003495 * @param[in,out] first First sibling, NULL if no top-level sibling exist yet. Can be also NULL if @p parent is set.
3496 * @param[in] options Bitmask of options flags, see @ref dupoptions.
Michal Vaskoddd76592022-01-17 13:34:48 +01003497 * @param[out] dup_p Pointer where the created duplicated node is placed (besides connecting it to @p parent / @p first).
3498 * @return LY_ERR value.
Radek Krejci22ebdba2019-07-25 13:59:43 +02003499 */
Michal Vasko52927e22020-03-16 17:26:14 +01003500static LY_ERR
Michal Vaskoddd76592022-01-17 13:34:48 +01003501lyd_dup_r(const struct lyd_node *node, const struct ly_ctx *trg_ctx, struct lyd_node *parent, ly_bool insert_last,
3502 struct lyd_node **first, uint32_t options, struct lyd_node **dup_p)
Radek Krejci22ebdba2019-07-25 13:59:43 +02003503{
Michal Vasko52927e22020-03-16 17:26:14 +01003504 LY_ERR ret;
Radek Krejci22ebdba2019-07-25 13:59:43 +02003505 struct lyd_node *dup = NULL;
Michal Vasko61551fa2020-07-09 15:45:45 +02003506 struct lyd_meta *meta;
Michal Vasko9cf62422021-07-01 13:29:32 +02003507 struct lyd_attr *attr;
Michal Vasko61551fa2020-07-09 15:45:45 +02003508 struct lyd_node_any *any;
Michal Vaskoddd76592022-01-17 13:34:48 +01003509 const struct lysc_type *type;
3510 const char *val_can;
Radek Krejci22ebdba2019-07-25 13:59:43 +02003511
Michal Vasko52927e22020-03-16 17:26:14 +01003512 LY_CHECK_ARG_RET(NULL, node, LY_EINVAL);
Radek Krejci22ebdba2019-07-25 13:59:43 +02003513
Michal Vasko52927e22020-03-16 17:26:14 +01003514 if (!node->schema) {
3515 dup = calloc(1, sizeof(struct lyd_node_opaq));
Michal Vaskoddd76592022-01-17 13:34:48 +01003516 ((struct lyd_node_opaq *)dup)->ctx = trg_ctx;
Michal Vasko52927e22020-03-16 17:26:14 +01003517 } else {
3518 switch (node->schema->nodetype) {
Michal Vasko1bf09392020-03-27 12:38:10 +01003519 case LYS_RPC:
Michal Vasko52927e22020-03-16 17:26:14 +01003520 case LYS_ACTION:
3521 case LYS_NOTIF:
3522 case LYS_CONTAINER:
3523 case LYS_LIST:
3524 dup = calloc(1, sizeof(struct lyd_node_inner));
3525 break;
3526 case LYS_LEAF:
3527 case LYS_LEAFLIST:
3528 dup = calloc(1, sizeof(struct lyd_node_term));
3529 break;
3530 case LYS_ANYDATA:
3531 case LYS_ANYXML:
3532 dup = calloc(1, sizeof(struct lyd_node_any));
3533 break;
3534 default:
Michal Vaskoddd76592022-01-17 13:34:48 +01003535 LOGINT(trg_ctx);
Michal Vasko52927e22020-03-16 17:26:14 +01003536 ret = LY_EINT;
3537 goto error;
3538 }
Radek Krejci22ebdba2019-07-25 13:59:43 +02003539 }
Michal Vaskoddd76592022-01-17 13:34:48 +01003540 LY_CHECK_ERR_GOTO(!dup, LOGMEM(trg_ctx); ret = LY_EMEM, error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02003541
Michal Vaskof6df0a02020-06-16 13:08:34 +02003542 if (options & LYD_DUP_WITH_FLAGS) {
3543 dup->flags = node->flags;
3544 } else {
3545 dup->flags = (node->flags & LYD_DEFAULT) | LYD_NEW;
3546 }
Michal Vaskoddd76592022-01-17 13:34:48 +01003547 if (trg_ctx == LYD_CTX(node)) {
3548 dup->schema = node->schema;
3549 } else {
3550 LY_CHECK_GOTO(ret = lyd_dup_find_schema(node->schema, trg_ctx, parent, &dup->schema), error);
3551 }
Michal Vasko52927e22020-03-16 17:26:14 +01003552 dup->prev = dup;
Radek Krejci22ebdba2019-07-25 13:59:43 +02003553
Michal Vasko9cf62422021-07-01 13:29:32 +02003554 /* duplicate metadata/attributes */
Michal Vasko25a32822020-07-09 15:48:22 +02003555 if (!(options & LYD_DUP_NO_META)) {
Michal Vasko9cf62422021-07-01 13:29:32 +02003556 if (!node->schema) {
3557 LY_LIST_FOR(((struct lyd_node_opaq *)node)->attr, attr) {
3558 LY_CHECK_GOTO(ret = lyd_dup_attr_single(attr, dup, NULL), error);
3559 }
3560 } else {
3561 LY_LIST_FOR(node->meta, meta) {
3562 LY_CHECK_GOTO(ret = lyd_dup_meta_single(meta, dup, NULL), error);
3563 }
Michal Vasko25a32822020-07-09 15:48:22 +02003564 }
3565 }
Radek Krejci22ebdba2019-07-25 13:59:43 +02003566
3567 /* nodetype-specific work */
Michal Vasko52927e22020-03-16 17:26:14 +01003568 if (!dup->schema) {
3569 struct lyd_node_opaq *opaq = (struct lyd_node_opaq *)dup;
3570 struct lyd_node_opaq *orig = (struct lyd_node_opaq *)node;
3571 struct lyd_node *child;
Radek Krejci22ebdba2019-07-25 13:59:43 +02003572
3573 if (options & LYD_DUP_RECURSIVE) {
3574 /* duplicate all the children */
3575 LY_LIST_FOR(orig->child, child) {
Michal Vaskoddd76592022-01-17 13:34:48 +01003576 LY_CHECK_GOTO(ret = lyd_dup_r(child, trg_ctx, dup, 1, NULL, options, NULL), error);
Michal Vasko52927e22020-03-16 17:26:14 +01003577 }
3578 }
Michal Vaskoddd76592022-01-17 13:34:48 +01003579 LY_CHECK_GOTO(ret = lydict_insert(trg_ctx, orig->name.name, 0, &opaq->name.name), error);
3580 LY_CHECK_GOTO(ret = lydict_insert(trg_ctx, orig->name.prefix, 0, &opaq->name.prefix), error);
3581 LY_CHECK_GOTO(ret = lydict_insert(trg_ctx, orig->name.module_ns, 0, &opaq->name.module_ns), error);
3582 LY_CHECK_GOTO(ret = lydict_insert(trg_ctx, orig->value, 0, &opaq->value), error);
Michal Vasko9cf62422021-07-01 13:29:32 +02003583 opaq->hints = orig->hints;
3584 opaq->format = orig->format;
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01003585 if (orig->val_prefix_data) {
Michal Vaskoddd76592022-01-17 13:34:48 +01003586 ret = ly_dup_prefix_data(trg_ctx, opaq->format, orig->val_prefix_data, &opaq->val_prefix_data);
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01003587 LY_CHECK_GOTO(ret, error);
Michal Vasko52927e22020-03-16 17:26:14 +01003588 }
Michal Vasko52927e22020-03-16 17:26:14 +01003589 } else if (dup->schema->nodetype & LYD_NODE_TERM) {
3590 struct lyd_node_term *term = (struct lyd_node_term *)dup;
3591 struct lyd_node_term *orig = (struct lyd_node_term *)node;
3592
3593 term->hash = orig->hash;
Michal Vaskoddd76592022-01-17 13:34:48 +01003594 if (trg_ctx == LYD_CTX(node)) {
3595 ret = orig->value.realtype->plugin->duplicate(trg_ctx, &orig->value, &term->value);
3596 LY_CHECK_ERR_GOTO(ret, LOGERR(trg_ctx, ret, "Value duplication failed."), error);
3597 } else {
3598 /* store canonical value in the target context */
3599 val_can = lyd_get_value(node);
3600 type = ((struct lysc_node_leaf *)term->schema)->type;
3601 ret = lyd_value_store(trg_ctx, &term->value, type, val_can, strlen(val_can), NULL, LY_VALUE_CANON, NULL,
3602 LYD_HINT_DATA, term->schema, NULL);
3603 LY_CHECK_GOTO(ret, error);
3604 }
Michal Vasko52927e22020-03-16 17:26:14 +01003605 } else if (dup->schema->nodetype & LYD_NODE_INNER) {
3606 struct lyd_node_inner *orig = (struct lyd_node_inner *)node;
3607 struct lyd_node *child;
3608
3609 if (options & LYD_DUP_RECURSIVE) {
3610 /* duplicate all the children */
3611 LY_LIST_FOR(orig->child, child) {
Michal Vaskoddd76592022-01-17 13:34:48 +01003612 LY_CHECK_GOTO(ret = lyd_dup_r(child, trg_ctx, dup, 1, NULL, options, NULL), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02003613 }
Michal Vasko69730152020-10-09 16:30:07 +02003614 } else if ((dup->schema->nodetype == LYS_LIST) && !(dup->schema->flags & LYS_KEYLESS)) {
Radek Krejci22ebdba2019-07-25 13:59:43 +02003615 /* always duplicate keys of a list */
Radek Krejci22ebdba2019-07-25 13:59:43 +02003616 child = orig->child;
Michal Vasko544e58a2021-01-28 14:33:41 +01003617 for (const struct lysc_node *key = lysc_node_child(dup->schema);
Michal Vaskoc57d9a92020-06-23 13:28:27 +02003618 key && (key->flags & LYS_KEY);
Radek Krejci0fe9b512019-07-26 17:51:05 +02003619 key = key->next) {
Radek Krejci22ebdba2019-07-25 13:59:43 +02003620 if (!child) {
3621 /* possibly not keys are present in filtered tree */
3622 break;
Radek Krejci0fe9b512019-07-26 17:51:05 +02003623 } else if (child->schema != key) {
3624 /* possibly not all keys are present in filtered tree,
3625 * but there can be also some non-key nodes */
3626 continue;
Radek Krejci22ebdba2019-07-25 13:59:43 +02003627 }
Michal Vaskoddd76592022-01-17 13:34:48 +01003628 LY_CHECK_GOTO(ret = lyd_dup_r(child, trg_ctx, dup, 1, NULL, options, NULL), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02003629 child = child->next;
3630 }
3631 }
3632 lyd_hash(dup);
3633 } else if (dup->schema->nodetype & LYD_NODE_ANY) {
Michal Vasko61551fa2020-07-09 15:45:45 +02003634 dup->hash = node->hash;
3635 any = (struct lyd_node_any *)node;
3636 LY_CHECK_GOTO(ret = lyd_any_copy_value(dup, &any->value, any->value_type), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02003637 }
3638
Michal Vasko52927e22020-03-16 17:26:14 +01003639 /* insert */
Michal Vasko67177e52021-08-25 11:15:15 +02003640 lyd_insert_node(parent, first, dup, insert_last);
Michal Vasko52927e22020-03-16 17:26:14 +01003641
3642 if (dup_p) {
3643 *dup_p = dup;
3644 }
3645 return LY_SUCCESS;
Radek Krejci22ebdba2019-07-25 13:59:43 +02003646
3647error:
Michal Vasko52927e22020-03-16 17:26:14 +01003648 lyd_free_tree(dup);
3649 return ret;
Radek Krejci22ebdba2019-07-25 13:59:43 +02003650}
3651
Michal Vasko29d674b2021-08-25 11:18:35 +02003652/**
3653 * @brief Get a parent node to connect duplicated subtree to.
3654 *
3655 * @param[in] node Node (subtree) to duplicate.
Michal Vaskoddd76592022-01-17 13:34:48 +01003656 * @param[in] trg_ctx Target context for duplicated nodes.
Michal Vasko29d674b2021-08-25 11:18:35 +02003657 * @param[in] parent Initial parent to connect to.
3658 * @param[in] options Bitmask of options flags, see @ref dupoptions.
3659 * @param[out] dup_parent First duplicated parent node, if any.
3660 * @param[out] local_parent Correct parent to directly connect duplicated @p node to.
3661 * @return LY_ERR value.
3662 */
Michal Vasko3a41dff2020-07-15 14:30:28 +02003663static LY_ERR
Michal Vaskoddd76592022-01-17 13:34:48 +01003664lyd_dup_get_local_parent(const struct lyd_node *node, const struct ly_ctx *trg_ctx, const struct lyd_node_inner *parent,
3665 uint32_t options, struct lyd_node **dup_parent, struct lyd_node_inner **local_parent)
Radek Krejci22ebdba2019-07-25 13:59:43 +02003666{
Michal Vasko3a41dff2020-07-15 14:30:28 +02003667 const struct lyd_node_inner *orig_parent, *iter;
Radek Krejci857189e2020-09-01 13:26:36 +02003668 ly_bool repeat = 1;
Michal Vasko3a41dff2020-07-15 14:30:28 +02003669
3670 *dup_parent = NULL;
3671 *local_parent = NULL;
3672
3673 for (orig_parent = node->parent; repeat && orig_parent; orig_parent = orig_parent->parent) {
3674 if (parent && (parent->schema == orig_parent->schema)) {
3675 /* stop creating parents, connect what we have into the provided parent */
3676 iter = parent;
3677 repeat = 0;
3678 } else {
3679 iter = NULL;
Michal Vaskoddd76592022-01-17 13:34:48 +01003680 LY_CHECK_RET(lyd_dup_r((struct lyd_node *)orig_parent, trg_ctx, NULL, 0, (struct lyd_node **)&iter, options,
Radek Krejci0f969882020-08-21 16:56:47 +02003681 (struct lyd_node **)&iter));
Michal Vasko3a41dff2020-07-15 14:30:28 +02003682 }
3683 if (!*local_parent) {
3684 *local_parent = (struct lyd_node_inner *)iter;
3685 }
3686 if (iter->child) {
3687 /* 1) list - add after keys
3688 * 2) provided parent with some children */
3689 iter->child->prev->next = *dup_parent;
3690 if (*dup_parent) {
3691 (*dup_parent)->prev = iter->child->prev;
3692 iter->child->prev = *dup_parent;
3693 }
3694 } else {
3695 ((struct lyd_node_inner *)iter)->child = *dup_parent;
3696 }
3697 if (*dup_parent) {
3698 (*dup_parent)->parent = (struct lyd_node_inner *)iter;
3699 }
3700 *dup_parent = (struct lyd_node *)iter;
3701 }
3702
3703 if (repeat && parent) {
3704 /* given parent and created parents chain actually do not interconnect */
Michal Vaskoddd76592022-01-17 13:34:48 +01003705 LOGERR(trg_ctx, LY_EINVAL,
Michal Vasko69730152020-10-09 16:30:07 +02003706 "Invalid argument parent (%s()) - does not interconnect with the created node's parents chain.", __func__);
Michal Vasko3a41dff2020-07-15 14:30:28 +02003707 return LY_EINVAL;
3708 }
3709
3710 return LY_SUCCESS;
3711}
3712
3713static LY_ERR
Michal Vaskoddd76592022-01-17 13:34:48 +01003714lyd_dup(const struct lyd_node *node, const struct ly_ctx *trg_ctx, struct lyd_node_inner *parent, uint32_t options,
3715 ly_bool nosiblings, struct lyd_node **dup)
Michal Vasko3a41dff2020-07-15 14:30:28 +02003716{
3717 LY_ERR rc;
Radek Krejci22ebdba2019-07-25 13:59:43 +02003718 const struct lyd_node *orig; /* original node to be duplicated */
3719 struct lyd_node *first = NULL; /* the first duplicated node, this is returned */
Radek Krejci22ebdba2019-07-25 13:59:43 +02003720 struct lyd_node *top = NULL; /* the most higher created node */
3721 struct lyd_node_inner *local_parent = NULL; /* the direct parent node for the duplicated node(s) */
Radek Krejci22ebdba2019-07-25 13:59:43 +02003722
Michal Vaskoddd76592022-01-17 13:34:48 +01003723 assert(node && trg_ctx && (!parent || (LYD_CTX(parent) == trg_ctx)));
Radek Krejci22ebdba2019-07-25 13:59:43 +02003724
3725 if (options & LYD_DUP_WITH_PARENTS) {
Michal Vaskoddd76592022-01-17 13:34:48 +01003726 LY_CHECK_GOTO(rc = lyd_dup_get_local_parent(node, trg_ctx, parent, options & (LYD_DUP_WITH_FLAGS | LYD_DUP_NO_META),
Michal Vasko20909752021-05-18 16:13:38 +02003727 &top, &local_parent), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02003728 } else {
3729 local_parent = parent;
3730 }
3731
Radek Krejci22ebdba2019-07-25 13:59:43 +02003732 LY_LIST_FOR(node, orig) {
Michal Vasko35f4d772021-01-12 12:08:57 +01003733 if (lysc_is_key(orig->schema)) {
3734 if (local_parent) {
3735 /* the key must already exist in the parent */
3736 rc = lyd_find_sibling_schema(local_parent->child, orig->schema, first ? NULL : &first);
Michal Vaskoddd76592022-01-17 13:34:48 +01003737 LY_CHECK_ERR_GOTO(rc, LOGINT(trg_ctx), error);
Michal Vasko35f4d772021-01-12 12:08:57 +01003738 } else {
3739 assert(!(options & LYD_DUP_WITH_PARENTS));
3740 /* duplicating a single key, okay, I suppose... */
Michal Vaskoddd76592022-01-17 13:34:48 +01003741 rc = lyd_dup_r(orig, trg_ctx, NULL, 0, &first, options, first ? NULL : &first);
Michal Vasko35f4d772021-01-12 12:08:57 +01003742 LY_CHECK_GOTO(rc, error);
3743 }
3744 } else {
3745 /* if there is no local parent, it will be inserted into first */
Michal Vaskoddd76592022-01-17 13:34:48 +01003746 rc = lyd_dup_r(orig, trg_ctx, local_parent ? &local_parent->node : NULL, 0, &first, options,
3747 first ? NULL : &first);
Michal Vasko35f4d772021-01-12 12:08:57 +01003748 LY_CHECK_GOTO(rc, error);
3749 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02003750 if (nosiblings) {
Radek Krejci22ebdba2019-07-25 13:59:43 +02003751 break;
3752 }
3753 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02003754
Michal Vasko3a41dff2020-07-15 14:30:28 +02003755 if (dup) {
3756 *dup = first;
3757 }
3758 return LY_SUCCESS;
Radek Krejci22ebdba2019-07-25 13:59:43 +02003759
3760error:
3761 if (top) {
3762 lyd_free_tree(top);
3763 } else {
Michal Vaskof03ed032020-03-04 13:31:44 +01003764 lyd_free_siblings(first);
Radek Krejci22ebdba2019-07-25 13:59:43 +02003765 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02003766 return rc;
Radek Krejci22ebdba2019-07-25 13:59:43 +02003767}
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003768
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01003769LIBYANG_API_DEF LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003770lyd_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 +02003771{
Michal Vaskoddd76592022-01-17 13:34:48 +01003772 LY_CHECK_ARG_RET(NULL, node, LY_EINVAL);
3773 if (node && parent && (LYD_CTX(node) != LYD_CTX(parent))) {
3774 LOGERR(NULL, LY_EINVAL, "Different contexts used in node duplication.");
3775 return LY_EINVAL;
3776 }
3777
3778 return lyd_dup(node, LYD_CTX(node), parent, options, 1, dup);
3779}
3780
3781LIBYANG_API_DEF LY_ERR
3782lyd_dup_single_to_ctx(const struct lyd_node *node, const struct ly_ctx *trg_ctx, struct lyd_node_inner *parent,
3783 uint32_t options, struct lyd_node **dup)
3784{
3785 LY_CHECK_ARG_RET(trg_ctx, node, trg_ctx, LY_EINVAL);
3786
3787 return lyd_dup(node, trg_ctx, parent, options, 1, dup);
Michal Vasko3a41dff2020-07-15 14:30:28 +02003788}
3789
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01003790LIBYANG_API_DEF LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003791lyd_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 +02003792{
Michal Vaskoddd76592022-01-17 13:34:48 +01003793 LY_CHECK_ARG_RET(NULL, node, LY_EINVAL);
3794 if (node && parent && (LYD_CTX(node) != LYD_CTX(parent))) {
3795 LOGERR(NULL, LY_EINVAL, "Different contexts used in node duplication.");
3796 return LY_EINVAL;
3797 }
3798
3799 return lyd_dup(node, LYD_CTX(node), parent, options, 0, dup);
3800}
3801
3802LIBYANG_API_DEF LY_ERR
3803lyd_dup_siblings_to_ctx(const struct lyd_node *node, const struct ly_ctx *trg_ctx, struct lyd_node_inner *parent,
3804 uint32_t options, struct lyd_node **dup)
3805{
3806 LY_CHECK_ARG_RET(trg_ctx, node, trg_ctx, LY_EINVAL);
3807
3808 return lyd_dup(node, trg_ctx, parent, options, 0, dup);
Michal Vasko3a41dff2020-07-15 14:30:28 +02003809}
3810
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01003811LIBYANG_API_DEF LY_ERR
Michal Vasko3a41dff2020-07-15 14:30:28 +02003812lyd_dup_meta_single(const struct lyd_meta *meta, struct lyd_node *node, struct lyd_meta **dup)
Michal Vasko25a32822020-07-09 15:48:22 +02003813{
Radek Krejci011e4aa2020-09-04 15:22:31 +02003814 LY_ERR ret = LY_SUCCESS;
Michal Vasko25a32822020-07-09 15:48:22 +02003815 struct lyd_meta *mt, *last;
3816
Michal Vasko3a41dff2020-07-15 14:30:28 +02003817 LY_CHECK_ARG_RET(NULL, meta, node, LY_EINVAL);
Michal Vasko25a32822020-07-09 15:48:22 +02003818
3819 /* create a copy */
3820 mt = calloc(1, sizeof *mt);
Michal Vaskob7be7a82020-08-20 09:09:04 +02003821 LY_CHECK_ERR_RET(!mt, LOGMEM(LYD_CTX(node)), LY_EMEM);
Michal Vasko25a32822020-07-09 15:48:22 +02003822 mt->annotation = meta->annotation;
Michal Vaskob7be7a82020-08-20 09:09:04 +02003823 ret = meta->value.realtype->plugin->duplicate(LYD_CTX(node), &meta->value, &mt->value);
Radek Krejci011e4aa2020-09-04 15:22:31 +02003824 LY_CHECK_ERR_GOTO(ret, LOGERR(LYD_CTX(node), LY_EINT, "Value duplication failed."), finish);
3825 LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), meta->name, 0, &mt->name), finish);
Michal Vasko25a32822020-07-09 15:48:22 +02003826
3827 /* insert as the last attribute */
Radek Krejci011e4aa2020-09-04 15:22:31 +02003828 mt->parent = node;
Michal Vasko25a32822020-07-09 15:48:22 +02003829 if (node->meta) {
Radek Krejci1e008d22020-08-17 11:37:37 +02003830 for (last = node->meta; last->next; last = last->next) {}
Michal Vasko25a32822020-07-09 15:48:22 +02003831 last->next = mt;
3832 } else {
3833 node->meta = mt;
3834 }
3835
Radek Krejci011e4aa2020-09-04 15:22:31 +02003836finish:
3837 if (ret) {
3838 lyd_free_meta_single(mt);
3839 } else if (dup) {
Michal Vasko3a41dff2020-07-15 14:30:28 +02003840 *dup = mt;
3841 }
3842 return LY_SUCCESS;
Michal Vasko25a32822020-07-09 15:48:22 +02003843}
3844
Michal Vasko4490d312020-06-16 13:08:55 +02003845/**
3846 * @brief Merge a source sibling into target siblings.
3847 *
3848 * @param[in,out] first_trg First target sibling, is updated if top-level.
3849 * @param[in] parent_trg Target parent.
3850 * @param[in,out] sibling_src Source sibling to merge, set to NULL if spent.
Michal Vaskocd3f6172021-05-18 16:14:50 +02003851 * @param[in] merge_cb Optional merge callback.
3852 * @param[in] cb_data Arbitrary callback data.
Michal Vasko4490d312020-06-16 13:08:55 +02003853 * @param[in] options Merge options.
Michal Vaskocd3f6172021-05-18 16:14:50 +02003854 * @param[in,out] dup_inst Duplicate instance cache for all @p first_trg siblings.
Michal Vasko4490d312020-06-16 13:08:55 +02003855 * @return LY_ERR value.
3856 */
3857static LY_ERR
3858lyd_merge_sibling_r(struct lyd_node **first_trg, struct lyd_node *parent_trg, const struct lyd_node **sibling_src_p,
Michal Vaskocd3f6172021-05-18 16:14:50 +02003859 lyd_merge_cb merge_cb, void *cb_data, uint16_t options, struct lyd_dup_inst **dup_inst)
Michal Vasko4490d312020-06-16 13:08:55 +02003860{
Michal Vasko4490d312020-06-16 13:08:55 +02003861 const struct lyd_node *child_src, *tmp, *sibling_src;
Michal Vasko56daf732020-08-10 10:57:18 +02003862 struct lyd_node *match_trg, *dup_src, *elem;
Michal Vaskod1afa502022-01-27 16:18:12 +01003863 struct lyd_node_opaq *opaq_trg, *opaq_src;
Michal Vasko4490d312020-06-16 13:08:55 +02003864 struct lysc_type *type;
Michal Vaskocd3f6172021-05-18 16:14:50 +02003865 struct lyd_dup_inst *child_dup_inst = NULL;
3866 LY_ERR ret;
3867 ly_bool first_inst = 0;
Michal Vasko4490d312020-06-16 13:08:55 +02003868
3869 sibling_src = *sibling_src_p;
Michal Vaskocd3f6172021-05-18 16:14:50 +02003870 if (!sibling_src->schema) {
3871 /* try to find the same opaque node */
3872 lyd_find_sibling_opaq_next(*first_trg, LYD_NAME(sibling_src), &match_trg);
3873 } else if (sibling_src->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) {
Michal Vasko4490d312020-06-16 13:08:55 +02003874 /* try to find the exact instance */
Michal Vaskocd3f6172021-05-18 16:14:50 +02003875 lyd_find_sibling_first(*first_trg, sibling_src, &match_trg);
Michal Vasko4490d312020-06-16 13:08:55 +02003876 } else {
3877 /* try to simply find the node, there cannot be more instances */
Michal Vaskocd3f6172021-05-18 16:14:50 +02003878 lyd_find_sibling_val(*first_trg, sibling_src->schema, NULL, 0, &match_trg);
Michal Vasko4490d312020-06-16 13:08:55 +02003879 }
3880
Michal Vaskocd3f6172021-05-18 16:14:50 +02003881 if (match_trg) {
3882 /* update match as needed */
3883 LY_CHECK_RET(lyd_dup_inst_next(&match_trg, *first_trg, dup_inst));
3884 } else {
3885 /* first instance of this node */
3886 first_inst = 1;
3887 }
3888
3889 if (match_trg) {
3890 /* call callback */
3891 if (merge_cb) {
3892 LY_CHECK_RET(merge_cb(match_trg, sibling_src, cb_data));
3893 }
3894
Michal Vasko4490d312020-06-16 13:08:55 +02003895 /* node found, make sure even value matches for all node types */
Michal Vaskod1afa502022-01-27 16:18:12 +01003896 if (!match_trg->schema) {
3897 if (lyd_compare_single(sibling_src, match_trg, 0)) {
3898 /* update value */
3899 opaq_trg = (struct lyd_node_opaq *)match_trg;
3900 opaq_src = (struct lyd_node_opaq *)sibling_src;
3901
3902 lydict_remove(LYD_CTX(opaq_trg), opaq_trg->value);
3903 lydict_insert(LYD_CTX(opaq_trg), opaq_src->value, 0, &opaq_trg->value);
3904 opaq_trg->hints = opaq_src->hints;
3905
3906 ly_free_prefix_data(opaq_trg->format, opaq_trg->val_prefix_data);
3907 opaq_trg->format = opaq_src->format;
3908 ly_dup_prefix_data(LYD_CTX(opaq_trg), opaq_src->format, opaq_src->val_prefix_data,
3909 &opaq_trg->val_prefix_data);
3910 }
3911 } else if ((match_trg->schema->nodetype == LYS_LEAF) &&
3912 lyd_compare_single(sibling_src, match_trg, LYD_COMPARE_DEFAULTS)) {
Michal Vasko4490d312020-06-16 13:08:55 +02003913 /* since they are different, they cannot both be default */
3914 assert(!(sibling_src->flags & LYD_DEFAULT) || !(match_trg->flags & LYD_DEFAULT));
3915
Michal Vasko3a41dff2020-07-15 14:30:28 +02003916 /* update value (or only LYD_DEFAULT flag) only if flag set or the source node is not default */
3917 if ((options & LYD_MERGE_DEFAULTS) || !(sibling_src->flags & LYD_DEFAULT)) {
Michal Vasko4490d312020-06-16 13:08:55 +02003918 type = ((struct lysc_node_leaf *)match_trg->schema)->type;
Michal Vaskob7be7a82020-08-20 09:09:04 +02003919 type->plugin->free(LYD_CTX(match_trg), &((struct lyd_node_term *)match_trg)->value);
3920 LY_CHECK_RET(type->plugin->duplicate(LYD_CTX(match_trg), &((struct lyd_node_term *)sibling_src)->value,
Michal Vasko69730152020-10-09 16:30:07 +02003921 &((struct lyd_node_term *)match_trg)->value));
Michal Vasko4490d312020-06-16 13:08:55 +02003922
3923 /* copy flags and add LYD_NEW */
Michal Vasko7e8315b2021-08-03 17:01:06 +02003924 match_trg->flags = sibling_src->flags | ((options & LYD_MERGE_WITH_FLAGS) ? 0 : LYD_NEW);
Michal Vasko4490d312020-06-16 13:08:55 +02003925 }
Michal Vasko8f359bf2020-07-28 10:41:15 +02003926 } else if ((match_trg->schema->nodetype & LYS_ANYDATA) && lyd_compare_single(sibling_src, match_trg, 0)) {
Michal Vasko4c583e82020-07-17 12:16:14 +02003927 /* update value */
3928 LY_CHECK_RET(lyd_any_copy_value(match_trg, &((struct lyd_node_any *)sibling_src)->value,
Radek Krejci0f969882020-08-21 16:56:47 +02003929 ((struct lyd_node_any *)sibling_src)->value_type));
Michal Vasko4490d312020-06-16 13:08:55 +02003930
3931 /* copy flags and add LYD_NEW */
Michal Vasko7e8315b2021-08-03 17:01:06 +02003932 match_trg->flags = sibling_src->flags | ((options & LYD_MERGE_WITH_FLAGS) ? 0 : LYD_NEW);
Michal Vaskocd3f6172021-05-18 16:14:50 +02003933 }
3934
3935 /* check descendants, recursively */
3936 ret = LY_SUCCESS;
3937 LY_LIST_FOR_SAFE(lyd_child_no_keys(sibling_src), tmp, child_src) {
3938 ret = lyd_merge_sibling_r(lyd_node_child_p(match_trg), match_trg, &child_src, merge_cb, cb_data, options,
3939 &child_dup_inst);
3940 if (ret) {
3941 break;
Michal Vasko4490d312020-06-16 13:08:55 +02003942 }
3943 }
Michal Vaskocd3f6172021-05-18 16:14:50 +02003944 lyd_dup_inst_free(child_dup_inst);
3945 LY_CHECK_RET(ret);
Michal Vasko4490d312020-06-16 13:08:55 +02003946 } else {
3947 /* node not found, merge it */
3948 if (options & LYD_MERGE_DESTRUCT) {
3949 dup_src = (struct lyd_node *)sibling_src;
3950 lyd_unlink_tree(dup_src);
3951 /* spend it */
3952 *sibling_src_p = NULL;
3953 } else {
Michal Vasko3a41dff2020-07-15 14:30:28 +02003954 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 +02003955 }
3956
Michal Vasko7e8315b2021-08-03 17:01:06 +02003957 if (!(options & LYD_MERGE_WITH_FLAGS)) {
3958 /* set LYD_NEW for all the new nodes, required for validation */
3959 LYD_TREE_DFS_BEGIN(dup_src, elem) {
3960 elem->flags |= LYD_NEW;
3961 LYD_TREE_DFS_END(dup_src, elem);
3962 }
Michal Vasko4490d312020-06-16 13:08:55 +02003963 }
3964
Michal Vaskocd3f6172021-05-18 16:14:50 +02003965 /* insert */
Michal Vasko6ee6f432021-07-16 09:49:14 +02003966 lyd_insert_node(parent_trg, first_trg, dup_src, 0);
Michal Vaskocd3f6172021-05-18 16:14:50 +02003967
3968 if (first_inst) {
3969 /* remember not to find this instance next time */
3970 LY_CHECK_RET(lyd_dup_inst_next(&dup_src, *first_trg, dup_inst));
3971 }
3972
3973 /* call callback, no source node */
3974 if (merge_cb) {
3975 LY_CHECK_RET(merge_cb(dup_src, NULL, cb_data));
3976 }
Michal Vasko4490d312020-06-16 13:08:55 +02003977 }
3978
3979 return LY_SUCCESS;
3980}
3981
Michal Vasko3a41dff2020-07-15 14:30:28 +02003982static LY_ERR
Michal Vaskocd3f6172021-05-18 16:14:50 +02003983lyd_merge(struct lyd_node **target, const struct lyd_node *source, const struct lys_module *mod,
3984 lyd_merge_cb merge_cb, void *cb_data, uint16_t options, ly_bool nosiblings)
Michal Vasko4490d312020-06-16 13:08:55 +02003985{
3986 const struct lyd_node *sibling_src, *tmp;
Michal Vaskocd3f6172021-05-18 16:14:50 +02003987 struct lyd_dup_inst *dup_inst = NULL;
Radek Krejci857189e2020-09-01 13:26:36 +02003988 ly_bool first;
Michal Vaskocd3f6172021-05-18 16:14:50 +02003989 LY_ERR ret = LY_SUCCESS;
Michal Vasko4490d312020-06-16 13:08:55 +02003990
3991 LY_CHECK_ARG_RET(NULL, target, LY_EINVAL);
Michal Vasko892f5bf2021-11-24 10:41:05 +01003992 LY_CHECK_CTX_EQUAL_RET(*target ? LYD_CTX(*target) : NULL, source ? LYD_CTX(source) : NULL, mod ? mod->ctx : NULL,
3993 LY_EINVAL);
Michal Vasko4490d312020-06-16 13:08:55 +02003994
3995 if (!source) {
3996 /* nothing to merge */
3997 return LY_SUCCESS;
3998 }
3999
Michal Vasko831422b2020-11-24 11:20:51 +01004000 if ((*target && lysc_data_parent((*target)->schema)) || lysc_data_parent(source->schema)) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02004001 LOGERR(LYD_CTX(source), LY_EINVAL, "Invalid arguments - can merge only 2 top-level subtrees (%s()).", __func__);
Michal Vasko4490d312020-06-16 13:08:55 +02004002 return LY_EINVAL;
4003 }
4004
4005 LY_LIST_FOR_SAFE(source, tmp, sibling_src) {
Michal Vaskocd3f6172021-05-18 16:14:50 +02004006 if (mod && (lyd_owner_module(sibling_src) != mod)) {
4007 /* skip data nodes from different modules */
4008 continue;
4009 }
4010
Radek Krejci857189e2020-09-01 13:26:36 +02004011 first = (sibling_src == source) ? 1 : 0;
Michal Vaskocd3f6172021-05-18 16:14:50 +02004012 ret = lyd_merge_sibling_r(target, NULL, &sibling_src, merge_cb, cb_data, options, &dup_inst);
4013 if (ret) {
4014 break;
4015 }
Michal Vasko4490d312020-06-16 13:08:55 +02004016 if (first && !sibling_src) {
4017 /* source was spent (unlinked), move to the next node */
4018 source = tmp;
4019 }
4020
Michal Vasko3a41dff2020-07-15 14:30:28 +02004021 if (nosiblings) {
Michal Vasko4490d312020-06-16 13:08:55 +02004022 break;
4023 }
4024 }
4025
4026 if (options & LYD_MERGE_DESTRUCT) {
4027 /* free any leftover source data that were not merged */
4028 lyd_free_siblings((struct lyd_node *)source);
4029 }
4030
Michal Vaskocd3f6172021-05-18 16:14:50 +02004031 lyd_dup_inst_free(dup_inst);
4032 return ret;
Michal Vasko4490d312020-06-16 13:08:55 +02004033}
4034
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01004035LIBYANG_API_DEF LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004036lyd_merge_tree(struct lyd_node **target, const struct lyd_node *source, uint16_t options)
Michal Vasko3a41dff2020-07-15 14:30:28 +02004037{
Michal Vaskocd3f6172021-05-18 16:14:50 +02004038 return lyd_merge(target, source, NULL, NULL, NULL, options, 1);
Michal Vasko3a41dff2020-07-15 14:30:28 +02004039}
4040
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01004041LIBYANG_API_DEF LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004042lyd_merge_siblings(struct lyd_node **target, const struct lyd_node *source, uint16_t options)
Michal Vasko3a41dff2020-07-15 14:30:28 +02004043{
Michal Vaskocd3f6172021-05-18 16:14:50 +02004044 return lyd_merge(target, source, NULL, NULL, NULL, options, 0);
4045}
4046
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01004047LIBYANG_API_DEF LY_ERR
Michal Vaskocd3f6172021-05-18 16:14:50 +02004048lyd_merge_module(struct lyd_node **target, const struct lyd_node *source, const struct lys_module *mod,
4049 lyd_merge_cb merge_cb, void *cb_data, uint16_t options)
4050{
4051 return lyd_merge(target, source, mod, merge_cb, cb_data, options, 0);
Michal Vasko3a41dff2020-07-15 14:30:28 +02004052}
4053
Michal Vasko5ec7cda2019-09-11 13:43:08 +02004054static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02004055lyd_path_str_enlarge(char **buffer, size_t *buflen, size_t reqlen, ly_bool is_static)
Michal Vasko5ec7cda2019-09-11 13:43:08 +02004056{
Michal Vasko14654712020-02-06 08:35:21 +01004057 /* ending \0 */
4058 ++reqlen;
4059
Michal Vasko5ec7cda2019-09-11 13:43:08 +02004060 if (reqlen > *buflen) {
4061 if (is_static) {
4062 return LY_EINCOMPLETE;
4063 }
4064
4065 *buffer = ly_realloc(*buffer, reqlen * sizeof **buffer);
4066 if (!*buffer) {
4067 return LY_EMEM;
4068 }
4069
4070 *buflen = reqlen;
4071 }
4072
4073 return LY_SUCCESS;
4074}
4075
Michal Vaskod59035b2020-07-08 12:00:06 +02004076LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02004077lyd_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 +02004078{
4079 const struct lyd_node *key;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02004080 size_t len;
4081 const char *val;
4082 char quot;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02004083
Michal Vasko824d0832021-11-04 15:36:51 +01004084 for (key = lyd_child(node); key && key->schema && (key->schema->flags & LYS_KEY); key = key->next) {
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02004085 val = lyd_get_value(key);
Michal Vasko5ec7cda2019-09-11 13:43:08 +02004086 len = 1 + strlen(key->schema->name) + 2 + strlen(val) + 2;
Michal Vaskoba99a3e2020-08-18 15:50:05 +02004087 LY_CHECK_RET(lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static));
Michal Vasko5ec7cda2019-09-11 13:43:08 +02004088
4089 quot = '\'';
4090 if (strchr(val, '\'')) {
4091 quot = '"';
4092 }
4093 *bufused += sprintf(*buffer + *bufused, "[%s=%c%s%c]", key->schema->name, quot, val, quot);
Michal Vasko5ec7cda2019-09-11 13:43:08 +02004094 }
4095
4096 return LY_SUCCESS;
4097}
4098
4099/**
4100 * @brief Append leaf-list value predicate to path.
4101 *
4102 * @param[in] node Node to print.
4103 * @param[in,out] buffer Buffer to print to.
4104 * @param[in,out] buflen Current buffer length.
4105 * @param[in,out] bufused Current number of characters used in @p buffer.
4106 * @param[in] is_static Whether buffer is static or can be reallocated.
4107 * @return LY_ERR
4108 */
4109static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02004110lyd_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 +02004111{
Michal Vasko5ec7cda2019-09-11 13:43:08 +02004112 size_t len;
4113 const char *val;
4114 char quot;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02004115
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02004116 val = lyd_get_value(node);
Radek Krejcif13b87b2020-12-01 22:02:17 +01004117 len = 4 + strlen(val) + 2; /* "[.='" + val + "']" */
Michal Vaskoba99a3e2020-08-18 15:50:05 +02004118 LY_CHECK_RET(lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static));
Michal Vasko5ec7cda2019-09-11 13:43:08 +02004119
4120 quot = '\'';
4121 if (strchr(val, '\'')) {
4122 quot = '"';
4123 }
4124 *bufused += sprintf(*buffer + *bufused, "[.=%c%s%c]", quot, val, quot);
4125
Michal Vaskoba99a3e2020-08-18 15:50:05 +02004126 return LY_SUCCESS;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02004127}
4128
4129/**
4130 * @brief Append node position (relative to its other instances) predicate to path.
4131 *
4132 * @param[in] node Node to print.
4133 * @param[in,out] buffer Buffer to print to.
4134 * @param[in,out] buflen Current buffer length.
4135 * @param[in,out] bufused Current number of characters used in @p buffer.
4136 * @param[in] is_static Whether buffer is static or can be reallocated.
4137 * @return LY_ERR
4138 */
4139static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02004140lyd_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 +02004141{
Michal Vasko5ec7cda2019-09-11 13:43:08 +02004142 size_t len;
Michal Vasko50cc0562021-05-18 16:15:43 +02004143 uint32_t pos;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02004144 char *val = NULL;
4145 LY_ERR rc;
4146
Michal Vasko50cc0562021-05-18 16:15:43 +02004147 pos = lyd_list_pos(node);
4148 if (asprintf(&val, "%" PRIu32, pos) == -1) {
Michal Vasko5ec7cda2019-09-11 13:43:08 +02004149 return LY_EMEM;
4150 }
4151
4152 len = 1 + strlen(val) + 1;
4153 rc = lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static);
4154 if (rc != LY_SUCCESS) {
4155 goto cleanup;
4156 }
4157
4158 *bufused += sprintf(*buffer + *bufused, "[%s]", val);
4159
4160cleanup:
4161 free(val);
4162 return rc;
4163}
4164
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01004165LIBYANG_API_DEF char *
Michal Vasko5ec7cda2019-09-11 13:43:08 +02004166lyd_path(const struct lyd_node *node, LYD_PATH_TYPE pathtype, char *buffer, size_t buflen)
4167{
Radek Krejci857189e2020-09-01 13:26:36 +02004168 ly_bool is_static = 0;
Radek Krejci1deb5be2020-08-26 16:43:36 +02004169 uint32_t i, depth;
Michal Vasko14654712020-02-06 08:35:21 +01004170 size_t bufused = 0, len;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02004171 const struct lyd_node *iter;
4172 const struct lys_module *mod;
Michal Vasko790b2bc2020-08-03 13:35:06 +02004173 LY_ERR rc = LY_SUCCESS;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02004174
4175 LY_CHECK_ARG_RET(NULL, node, NULL);
4176 if (buffer) {
Michal Vasko16385f42021-05-18 16:16:09 +02004177 LY_CHECK_ARG_RET(LYD_CTX(node), buflen > 1, NULL);
Michal Vasko5ec7cda2019-09-11 13:43:08 +02004178 is_static = 1;
Michal Vasko14654712020-02-06 08:35:21 +01004179 } else {
4180 buflen = 0;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02004181 }
4182
4183 switch (pathtype) {
Radek Krejci635d2b82021-01-04 11:26:51 +01004184 case LYD_PATH_STD:
4185 case LYD_PATH_STD_NO_LAST_PRED:
Michal Vasko14654712020-02-06 08:35:21 +01004186 depth = 1;
Michal Vasko9e685082021-01-29 14:49:09 +01004187 for (iter = node; iter->parent; iter = lyd_parent(iter)) {
Michal Vasko5ec7cda2019-09-11 13:43:08 +02004188 ++depth;
4189 }
4190
Michal Vasko5ec7cda2019-09-11 13:43:08 +02004191 goto iter_print;
Michal Vasko14654712020-02-06 08:35:21 +01004192 while (depth) {
Michal Vasko5ec7cda2019-09-11 13:43:08 +02004193 /* find the right node */
Michal Vasko9e685082021-01-29 14:49:09 +01004194 for (iter = node, i = 1; i < depth; iter = lyd_parent(iter), ++i) {}
Michal Vasko5ec7cda2019-09-11 13:43:08 +02004195iter_print:
4196 /* print prefix and name */
4197 mod = NULL;
Michal Vasko195607b2022-01-06 13:35:08 +01004198 if (iter->schema && (!iter->parent || !iter->parent->schema ||
4199 (iter->schema->module != iter->parent->schema->module))) {
Michal Vasko5ec7cda2019-09-11 13:43:08 +02004200 mod = iter->schema->module;
4201 }
4202
4203 /* realloc string */
Michal Vaskoad92b672020-11-12 13:11:31 +01004204 len = 1 + (mod ? strlen(mod->name) + 1 : 0) + (iter->schema ? strlen(iter->schema->name) :
4205 strlen(((struct lyd_node_opaq *)iter)->name.name));
Michal Vasko5ec7cda2019-09-11 13:43:08 +02004206 rc = lyd_path_str_enlarge(&buffer, &buflen, bufused + len, is_static);
4207 if (rc != LY_SUCCESS) {
4208 break;
4209 }
4210
4211 /* print next node */
Radek Krejci1798aae2020-07-14 13:26:06 +02004212 bufused += sprintf(buffer + bufused, "/%s%s%s", mod ? mod->name : "", mod ? ":" : "",
Michal Vaskoad92b672020-11-12 13:11:31 +01004213 iter->schema ? iter->schema->name : ((struct lyd_node_opaq *)iter)->name.name);
Michal Vasko5ec7cda2019-09-11 13:43:08 +02004214
Michal Vasko790b2bc2020-08-03 13:35:06 +02004215 /* do not always print the last (first) predicate */
Radek Krejci635d2b82021-01-04 11:26:51 +01004216 if (iter->schema && ((depth > 1) || (pathtype == LYD_PATH_STD))) {
Michal Vasko790b2bc2020-08-03 13:35:06 +02004217 switch (iter->schema->nodetype) {
4218 case LYS_LIST:
4219 if (iter->schema->flags & LYS_KEYLESS) {
4220 /* print its position */
4221 rc = lyd_path_position_predicate(iter, &buffer, &buflen, &bufused, is_static);
4222 } else {
4223 /* print all list keys in predicates */
4224 rc = lyd_path_list_predicate(iter, &buffer, &buflen, &bufused, is_static);
4225 }
4226 break;
4227 case LYS_LEAFLIST:
4228 if (iter->schema->flags & LYS_CONFIG_W) {
4229 /* print leaf-list value */
4230 rc = lyd_path_leaflist_predicate(iter, &buffer, &buflen, &bufused, is_static);
4231 } else {
4232 /* print its position */
4233 rc = lyd_path_position_predicate(iter, &buffer, &buflen, &bufused, is_static);
4234 }
4235 break;
4236 default:
4237 /* nothing to print more */
4238 break;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02004239 }
Michal Vasko5ec7cda2019-09-11 13:43:08 +02004240 }
4241 if (rc != LY_SUCCESS) {
4242 break;
4243 }
4244
Michal Vasko14654712020-02-06 08:35:21 +01004245 --depth;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02004246 }
4247 break;
4248 }
4249
4250 return buffer;
4251}
Michal Vaskoe444f752020-02-10 12:20:06 +01004252
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01004253LIBYANG_API_DEF struct lyd_meta *
Michal Vasko25a32822020-07-09 15:48:22 +02004254lyd_find_meta(const struct lyd_meta *first, const struct lys_module *module, const char *name)
4255{
4256 struct lyd_meta *ret = NULL;
4257 const struct ly_ctx *ctx;
4258 const char *prefix, *tmp;
4259 char *str;
4260 size_t pref_len, name_len;
4261
4262 LY_CHECK_ARG_RET(NULL, module || strchr(name, ':'), name, NULL);
Michal Vasko892f5bf2021-11-24 10:41:05 +01004263 LY_CHECK_CTX_EQUAL_RET(first ? first->annotation->module->ctx : NULL, module ? module->ctx : NULL, NULL);
Michal Vasko25a32822020-07-09 15:48:22 +02004264
4265 if (!first) {
4266 return NULL;
4267 }
4268
4269 ctx = first->annotation->module->ctx;
4270
4271 /* parse the name */
4272 tmp = name;
4273 if (ly_parse_nodeid(&tmp, &prefix, &pref_len, &name, &name_len) || tmp[0]) {
4274 LOGERR(ctx, LY_EINVAL, "Metadata name \"%s\" is not valid.", name);
4275 return NULL;
4276 }
4277
4278 /* find the module */
4279 if (prefix) {
4280 str = strndup(prefix, pref_len);
4281 module = ly_ctx_get_module_latest(ctx, str);
4282 free(str);
Radek Krejci422afb12021-03-04 16:38:16 +01004283 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 +02004284 }
4285
4286 /* find the metadata */
4287 LY_LIST_FOR(first, first) {
4288 if ((first->annotation->module == module) && !strcmp(first->name, name)) {
4289 ret = (struct lyd_meta *)first;
4290 break;
4291 }
4292 }
4293
4294 return ret;
4295}
4296
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01004297LIBYANG_API_DEF LY_ERR
Michal Vaskoe444f752020-02-10 12:20:06 +01004298lyd_find_sibling_first(const struct lyd_node *siblings, const struct lyd_node *target, struct lyd_node **match)
4299{
Michal Vaskoe78faec2021-04-08 17:24:43 +02004300 struct lyd_node **match_p, *iter;
Michal Vaskoe444f752020-02-10 12:20:06 +01004301 struct lyd_node_inner *parent;
Michal Vaskoe78faec2021-04-08 17:24:43 +02004302 ly_bool found;
Michal Vaskoe444f752020-02-10 12:20:06 +01004303
Michal Vaskof03ed032020-03-04 13:31:44 +01004304 LY_CHECK_ARG_RET(NULL, target, LY_EINVAL);
Michal Vasko892f5bf2021-11-24 10:41:05 +01004305 LY_CHECK_CTX_EQUAL_RET(siblings ? LYD_CTX(siblings) : NULL, LYD_CTX(target), LY_EINVAL);
Michal Vaskoe444f752020-02-10 12:20:06 +01004306
Michal Vasko6da1e952020-12-09 18:14:38 +01004307 if (!siblings || (siblings->schema && target->schema &&
4308 (lysc_data_parent(siblings->schema) != lysc_data_parent(target->schema)))) {
Michal Vasko62ed12d2020-05-21 10:08:25 +02004309 /* no data or schema mismatch */
Michal Vasko9b368d32020-02-14 13:53:31 +01004310 if (match) {
4311 *match = NULL;
4312 }
Michal Vaskoe444f752020-02-10 12:20:06 +01004313 return LY_ENOTFOUND;
4314 }
4315
Michal Vaskoe78faec2021-04-08 17:24:43 +02004316 /* get first sibling */
4317 siblings = lyd_first_sibling(siblings);
Michal Vaskoe444f752020-02-10 12:20:06 +01004318
Michal Vasko9e685082021-01-29 14:49:09 +01004319 parent = siblings->parent;
Michal Vasko6da1e952020-12-09 18:14:38 +01004320 if (parent && parent->schema && parent->children_ht) {
Michal Vaskoe444f752020-02-10 12:20:06 +01004321 assert(target->hash);
4322
Michal Vaskoe78faec2021-04-08 17:24:43 +02004323 if (lysc_is_dup_inst_list(target->schema)) {
4324 /* we must search the instances from beginning to find the first matching one */
4325 found = 0;
4326 LYD_LIST_FOR_INST(siblings, target->schema, iter) {
4327 if (!lyd_compare_single(target, iter, 0)) {
4328 found = 1;
4329 break;
4330 }
4331 }
4332 if (found) {
4333 siblings = iter;
Michal Vaskoda859032020-07-14 12:20:14 +02004334 } else {
4335 siblings = NULL;
4336 }
Michal Vaskoe444f752020-02-10 12:20:06 +01004337 } else {
Michal Vaskoe78faec2021-04-08 17:24:43 +02004338 /* find by hash */
4339 if (!lyht_find(parent->children_ht, &target, target->hash, (void **)&match_p)) {
4340 siblings = *match_p;
4341 } else {
4342 /* not found */
4343 siblings = NULL;
4344 }
Michal Vaskoe444f752020-02-10 12:20:06 +01004345 }
4346 } else {
4347 /* no children hash table */
Michal Vaskod989ba02020-08-24 10:59:24 +02004348 for ( ; siblings; siblings = siblings->next) {
Michal Vasko8f359bf2020-07-28 10:41:15 +02004349 if (!lyd_compare_single(siblings, target, 0)) {
Michal Vaskoe444f752020-02-10 12:20:06 +01004350 break;
4351 }
4352 }
4353 }
4354
4355 if (!siblings) {
Michal Vasko9b368d32020-02-14 13:53:31 +01004356 if (match) {
4357 *match = NULL;
4358 }
Michal Vaskoe444f752020-02-10 12:20:06 +01004359 return LY_ENOTFOUND;
4360 }
4361
Michal Vasko9b368d32020-02-14 13:53:31 +01004362 if (match) {
4363 *match = (struct lyd_node *)siblings;
4364 }
Michal Vaskoe444f752020-02-10 12:20:06 +01004365 return LY_SUCCESS;
4366}
4367
Radek Krejci857189e2020-09-01 13:26:36 +02004368/**
4369 * @brief Comparison callback to match schema node with a schema of a data node.
4370 *
4371 * @param[in] val1_p Pointer to the schema node
4372 * @param[in] val2_p Pointer to the data node
Michal Vasko62524a92021-02-26 10:08:50 +01004373 * Implementation of ::lyht_value_equal_cb.
Radek Krejci857189e2020-09-01 13:26:36 +02004374 */
4375static ly_bool
4376lyd_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 +01004377{
4378 struct lysc_node *val1;
4379 struct lyd_node *val2;
4380
4381 val1 = *((struct lysc_node **)val1_p);
4382 val2 = *((struct lyd_node **)val2_p);
4383
Michal Vasko90932a92020-02-12 14:33:03 +01004384 if (val1 == val2->schema) {
4385 /* schema match is enough */
4386 return 1;
4387 } else {
4388 return 0;
4389 }
4390}
4391
Michal Vasko92239c72020-11-18 18:17:25 +01004392/**
4393 * @brief Search in the given siblings (NOT recursively) for the first schema node data instance.
4394 * Uses hashes - should be used whenever possible for best performance.
4395 *
4396 * @param[in] siblings Siblings to search in including preceding and succeeding nodes.
4397 * @param[in] schema Target data node schema to find.
4398 * @param[out] match Can be NULL, otherwise the found data node.
4399 * @return LY_SUCCESS on success, @p match set.
4400 * @return LY_ENOTFOUND if not found, @p match set to NULL.
4401 * @return LY_ERR value if another error occurred.
4402 */
Michal Vasko90932a92020-02-12 14:33:03 +01004403static LY_ERR
4404lyd_find_sibling_schema(const struct lyd_node *siblings, const struct lysc_node *schema, struct lyd_node **match)
4405{
4406 struct lyd_node **match_p;
4407 struct lyd_node_inner *parent;
4408 uint32_t hash;
Michal Vasko62524a92021-02-26 10:08:50 +01004409 lyht_value_equal_cb ht_cb;
Michal Vasko90932a92020-02-12 14:33:03 +01004410
Michal Vaskob104f112020-07-17 09:54:54 +02004411 assert(siblings && schema);
Michal Vasko90932a92020-02-12 14:33:03 +01004412
Michal Vasko9e685082021-01-29 14:49:09 +01004413 parent = siblings->parent;
Michal Vasko08fd6132020-11-18 18:17:45 +01004414 if (parent && parent->schema && parent->children_ht) {
Michal Vasko90932a92020-02-12 14:33:03 +01004415 /* calculate our hash */
4416 hash = dict_hash_multi(0, schema->module->name, strlen(schema->module->name));
4417 hash = dict_hash_multi(hash, schema->name, strlen(schema->name));
4418 hash = dict_hash_multi(hash, NULL, 0);
4419
4420 /* use special hash table function */
4421 ht_cb = lyht_set_cb(parent->children_ht, lyd_hash_table_schema_val_equal);
4422
4423 /* find by hash */
4424 if (!lyht_find(parent->children_ht, &schema, hash, (void **)&match_p)) {
4425 siblings = *match_p;
4426 } else {
4427 /* not found */
4428 siblings = NULL;
4429 }
4430
4431 /* set the original hash table compare function back */
4432 lyht_set_cb(parent->children_ht, ht_cb);
4433 } else {
Michal Vaskob104f112020-07-17 09:54:54 +02004434 /* find first sibling */
4435 if (siblings->parent) {
4436 siblings = siblings->parent->child;
4437 } else {
4438 while (siblings->prev->next) {
4439 siblings = siblings->prev;
4440 }
4441 }
4442
4443 /* search manually without hashes */
Michal Vaskod989ba02020-08-24 10:59:24 +02004444 for ( ; siblings; siblings = siblings->next) {
Michal Vasko90932a92020-02-12 14:33:03 +01004445 if (siblings->schema == schema) {
4446 /* schema match is enough */
4447 break;
4448 }
4449 }
4450 }
4451
4452 if (!siblings) {
Michal Vasko9b368d32020-02-14 13:53:31 +01004453 if (match) {
4454 *match = NULL;
4455 }
Michal Vasko90932a92020-02-12 14:33:03 +01004456 return LY_ENOTFOUND;
4457 }
4458
Michal Vasko9b368d32020-02-14 13:53:31 +01004459 if (match) {
4460 *match = (struct lyd_node *)siblings;
4461 }
Michal Vasko90932a92020-02-12 14:33:03 +01004462 return LY_SUCCESS;
4463}
4464
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01004465LIBYANG_API_DEF LY_ERR
Michal Vaskoe444f752020-02-10 12:20:06 +01004466lyd_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 +02004467 size_t val_len, struct lyd_node **match)
Michal Vaskoe444f752020-02-10 12:20:06 +01004468{
4469 LY_ERR rc;
4470 struct lyd_node *target = NULL;
4471
Michal Vasko4c583e82020-07-17 12:16:14 +02004472 LY_CHECK_ARG_RET(NULL, schema, !(schema->nodetype & (LYS_CHOICE | LYS_CASE)), LY_EINVAL);
Michal Vasko892f5bf2021-11-24 10:41:05 +01004473 LY_CHECK_CTX_EQUAL_RET(siblings ? LYD_CTX(siblings) : NULL, schema->module->ctx, LY_EINVAL);
Michal Vaskoe444f752020-02-10 12:20:06 +01004474
Michal Vasko08fd6132020-11-18 18:17:45 +01004475 if (!siblings || (siblings->schema && (lysc_data_parent(siblings->schema) != lysc_data_parent(schema)))) {
Michal Vasko62ed12d2020-05-21 10:08:25 +02004476 /* no data or schema mismatch */
Michal Vasko9b368d32020-02-14 13:53:31 +01004477 if (match) {
4478 *match = NULL;
4479 }
Michal Vaskoe444f752020-02-10 12:20:06 +01004480 return LY_ENOTFOUND;
4481 }
4482
Michal Vaskof03ed032020-03-04 13:31:44 +01004483 if (key_or_value && !val_len) {
4484 val_len = strlen(key_or_value);
4485 }
4486
Michal Vaskob104f112020-07-17 09:54:54 +02004487 if ((schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) && key_or_value) {
4488 /* create a data node and find the instance */
4489 if (schema->nodetype == LYS_LEAFLIST) {
4490 /* target used attributes: schema, hash, value */
Radek Krejci8df109d2021-04-23 12:19:08 +02004491 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 +02004492 LY_CHECK_RET(rc);
Michal Vaskob104f112020-07-17 09:54:54 +02004493 } else {
Michal Vasko90932a92020-02-12 14:33:03 +01004494 /* target used attributes: schema, hash, child (all keys) */
Michal Vasko004d3152020-06-11 19:59:22 +02004495 LY_CHECK_RET(lyd_create_list2(schema, key_or_value, val_len, &target));
Michal Vasko90932a92020-02-12 14:33:03 +01004496 }
4497
4498 /* find it */
4499 rc = lyd_find_sibling_first(siblings, target, match);
Michal Vaskob104f112020-07-17 09:54:54 +02004500 } else {
4501 /* find the first schema node instance */
4502 rc = lyd_find_sibling_schema(siblings, schema, match);
Michal Vaskoe444f752020-02-10 12:20:06 +01004503 }
4504
Michal Vaskoe444f752020-02-10 12:20:06 +01004505 lyd_free_tree(target);
4506 return rc;
4507}
Michal Vaskoccc02342020-05-21 10:09:21 +02004508
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01004509LIBYANG_API_DEF LY_ERR
Michal Vaskoe78faec2021-04-08 17:24:43 +02004510lyd_find_sibling_dup_inst_set(const struct lyd_node *siblings, const struct lyd_node *target, struct ly_set **set)
4511{
4512 struct lyd_node **match_p, *first, *iter;
4513 struct lyd_node_inner *parent;
4514
4515 LY_CHECK_ARG_RET(NULL, target, lysc_is_dup_inst_list(target->schema), set, LY_EINVAL);
Michal Vasko892f5bf2021-11-24 10:41:05 +01004516 LY_CHECK_CTX_EQUAL_RET(siblings ? LYD_CTX(siblings) : NULL, LYD_CTX(target), LY_EINVAL);
Michal Vaskoe78faec2021-04-08 17:24:43 +02004517
4518 LY_CHECK_RET(ly_set_new(set));
4519
4520 if (!siblings || (siblings->schema && (lysc_data_parent(siblings->schema) != lysc_data_parent(target->schema)))) {
4521 /* no data or schema mismatch */
4522 return LY_ENOTFOUND;
4523 }
4524
4525 /* get first sibling */
4526 siblings = lyd_first_sibling(siblings);
4527
4528 parent = siblings->parent;
4529 if (parent && parent->schema && parent->children_ht) {
4530 assert(target->hash);
4531
4532 /* find the first instance */
4533 lyd_find_sibling_first(siblings, target, &first);
4534 if (first) {
4535 /* add it so that it is the first in the set */
4536 if (ly_set_add(*set, first, 1, NULL)) {
4537 goto error;
4538 }
4539
4540 /* find by hash */
4541 if (!lyht_find(parent->children_ht, &target, target->hash, (void **)&match_p)) {
4542 iter = *match_p;
4543 } else {
4544 /* not found */
4545 iter = NULL;
4546 }
4547 while (iter) {
4548 /* add all found nodes into the set */
4549 if ((iter != first) && !lyd_compare_single(iter, target, 0) && ly_set_add(*set, iter, 1, NULL)) {
4550 goto error;
4551 }
4552
4553 /* find next instance */
4554 if (lyht_find_next(parent->children_ht, &iter, iter->hash, (void **)&match_p)) {
4555 iter = NULL;
4556 } else {
4557 iter = *match_p;
4558 }
4559 }
4560 }
4561 } else {
4562 /* no children hash table */
4563 LY_LIST_FOR(siblings, siblings) {
4564 if (!lyd_compare_single(target, siblings, 0)) {
4565 ly_set_add(*set, (void *)siblings, 1, NULL);
4566 }
4567 }
4568 }
4569
4570 if (!(*set)->count) {
4571 return LY_ENOTFOUND;
4572 }
4573 return LY_SUCCESS;
4574
4575error:
4576 ly_set_free(*set, NULL);
4577 *set = NULL;
4578 return LY_EMEM;
4579}
4580
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01004581LIBYANG_API_DEF LY_ERR
Michal Vasko1d4af6c2021-02-22 13:31:26 +01004582lyd_find_sibling_opaq_next(const struct lyd_node *first, const char *name, struct lyd_node **match)
4583{
4584 LY_CHECK_ARG_RET(NULL, name, LY_EINVAL);
4585
4586 for ( ; first; first = first->next) {
4587 if (!first->schema && !strcmp(LYD_NAME(first), name)) {
4588 break;
4589 }
4590 }
4591
4592 if (match) {
4593 *match = (struct lyd_node *)first;
4594 }
4595 return first ? LY_SUCCESS : LY_ENOTFOUND;
4596}
4597
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01004598LIBYANG_API_DEF LY_ERR
Michal Vaskoddd76592022-01-17 13:34:48 +01004599lyd_find_xpath4(const struct lyd_node *ctx_node, const struct lyd_node *tree, const char *xpath, LY_VALUE_FORMAT format,
4600 void *prefix_data, const struct lyxp_var *vars, struct ly_set **set)
Michal Vaskoccc02342020-05-21 10:09:21 +02004601{
4602 LY_ERR ret = LY_SUCCESS;
Michal Vasko8bd9cc72021-07-16 14:06:10 +02004603 struct lyxp_set xp_set = {0};
Radek Krejcif03a9e22020-09-18 20:09:31 +02004604 struct lyxp_expr *exp = NULL;
Michal Vaskoccc02342020-05-21 10:09:21 +02004605 uint32_t i;
4606
Michal Vaskoddd76592022-01-17 13:34:48 +01004607 LY_CHECK_ARG_RET(NULL, tree, xpath, format, set, LY_EINVAL);
Michal Vaskoccc02342020-05-21 10:09:21 +02004608
Michal Vasko37c69432021-04-12 14:48:24 +02004609 *set = NULL;
Michal Vaskoccc02342020-05-21 10:09:21 +02004610
Michal Vaskoddd76592022-01-17 13:34:48 +01004611 /* parse expression */
Michal Vaskoe3716b32021-12-13 16:58:25 +01004612 ret = lyxp_expr_parse((struct ly_ctx *)LYD_CTX(tree), xpath, 0, 1, &exp);
Radek Krejcif03a9e22020-09-18 20:09:31 +02004613 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskoccc02342020-05-21 10:09:21 +02004614
4615 /* evaluate expression */
Michal Vaskoddd76592022-01-17 13:34:48 +01004616 ret = lyxp_eval(LYD_CTX(tree), exp, NULL, format, prefix_data, ctx_node, tree, vars, &xp_set, LYXP_IGNORE_WHEN);
Michal Vaskoccc02342020-05-21 10:09:21 +02004617 LY_CHECK_GOTO(ret, cleanup);
4618
4619 /* allocate return set */
Radek Krejciba03a5a2020-08-27 14:40:41 +02004620 ret = ly_set_new(set);
4621 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskoccc02342020-05-21 10:09:21 +02004622
4623 /* transform into ly_set */
4624 if (xp_set.type == LYXP_SET_NODE_SET) {
4625 /* allocate memory for all the elements once (even though not all items must be elements but most likely will be) */
4626 (*set)->objs = malloc(xp_set.used * sizeof *(*set)->objs);
Michal Vaskoe3716b32021-12-13 16:58:25 +01004627 LY_CHECK_ERR_GOTO(!(*set)->objs, LOGMEM(LYD_CTX(tree)); ret = LY_EMEM, cleanup);
Michal Vaskoccc02342020-05-21 10:09:21 +02004628 (*set)->size = xp_set.used;
4629
4630 for (i = 0; i < xp_set.used; ++i) {
4631 if (xp_set.val.nodes[i].type == LYXP_NODE_ELEM) {
Radek Krejci3d92e442020-10-12 12:48:13 +02004632 ret = ly_set_add(*set, xp_set.val.nodes[i].node, 1, NULL);
Radek Krejciba03a5a2020-08-27 14:40:41 +02004633 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskoccc02342020-05-21 10:09:21 +02004634 }
4635 }
4636 }
4637
4638cleanup:
Michal Vasko0691d522020-05-21 13:21:47 +02004639 lyxp_set_free_content(&xp_set);
Michal Vaskoe3716b32021-12-13 16:58:25 +01004640 lyxp_expr_free((struct ly_ctx *)LYD_CTX(tree), exp);
Radek Krejci8f45abc2020-11-26 12:15:13 +01004641 if (ret) {
4642 ly_set_free(*set, NULL);
4643 *set = NULL;
4644 }
Michal Vaskoccc02342020-05-21 10:09:21 +02004645 return ret;
4646}
Radek Krejcica989142020-11-05 11:32:22 +01004647
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01004648LIBYANG_API_DEF LY_ERR
Michal Vaskoddd76592022-01-17 13:34:48 +01004649lyd_find_xpath3(const struct lyd_node *ctx_node, const struct lyd_node *tree, const char *xpath,
4650 const struct lyxp_var *vars, struct ly_set **set)
4651{
4652 LY_CHECK_ARG_RET(NULL, tree, xpath, set, LY_EINVAL);
4653
4654 return lyd_find_xpath4(ctx_node, tree, xpath, LY_VALUE_JSON, NULL, vars, set);
4655}
4656
4657LIBYANG_API_DEF LY_ERR
Michal Vaskoe3716b32021-12-13 16:58:25 +01004658lyd_find_xpath2(const struct lyd_node *ctx_node, const char *xpath, const struct lyxp_var *vars, struct ly_set **set)
4659{
4660 LY_CHECK_ARG_RET(NULL, ctx_node, xpath, set, LY_EINVAL);
4661
Michal Vaskoddd76592022-01-17 13:34:48 +01004662 return lyd_find_xpath4(ctx_node, ctx_node, xpath, LY_VALUE_JSON, NULL, vars, set);
Michal Vaskoe3716b32021-12-13 16:58:25 +01004663}
4664
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01004665LIBYANG_API_DEF LY_ERR
aPiecekfba75362021-10-07 12:39:48 +02004666lyd_find_xpath(const struct lyd_node *ctx_node, const char *xpath, struct ly_set **set)
4667{
Michal Vaskoe3716b32021-12-13 16:58:25 +01004668 LY_CHECK_ARG_RET(NULL, ctx_node, xpath, set, LY_EINVAL);
4669
Michal Vaskoddd76592022-01-17 13:34:48 +01004670 return lyd_find_xpath4(ctx_node, ctx_node, xpath, LY_VALUE_JSON, NULL, NULL, set);
aPiecekfba75362021-10-07 12:39:48 +02004671}
4672
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01004673LIBYANG_API_DEF LY_ERR
aPiecekfba75362021-10-07 12:39:48 +02004674lyd_eval_xpath2(const struct lyd_node *ctx_node, const char *xpath, const struct lyxp_var *vars, ly_bool *result)
Michal Vaskoc9eb3ca2021-07-16 10:20:37 +02004675{
4676 LY_ERR ret = LY_SUCCESS;
Michal Vasko8bd9cc72021-07-16 14:06:10 +02004677 struct lyxp_set xp_set = {0};
Michal Vaskoc9eb3ca2021-07-16 10:20:37 +02004678 struct lyxp_expr *exp = NULL;
4679
4680 LY_CHECK_ARG_RET(NULL, ctx_node, xpath, result, LY_EINVAL);
4681
4682 /* compile expression */
4683 ret = lyxp_expr_parse((struct ly_ctx *)LYD_CTX(ctx_node), xpath, 0, 1, &exp);
4684 LY_CHECK_GOTO(ret, cleanup);
4685
4686 /* evaluate expression */
aPiecekfba75362021-10-07 12:39:48 +02004687 ret = lyxp_eval(LYD_CTX(ctx_node), exp, NULL, LY_VALUE_JSON, NULL, ctx_node, ctx_node, vars, &xp_set, LYXP_IGNORE_WHEN);
Michal Vaskoc9eb3ca2021-07-16 10:20:37 +02004688 LY_CHECK_GOTO(ret, cleanup);
4689
4690 /* transform into boolean */
4691 ret = lyxp_set_cast(&xp_set, LYXP_SET_BOOLEAN);
4692 LY_CHECK_GOTO(ret, cleanup);
4693
4694 /* set result */
4695 *result = xp_set.val.bln;
4696
4697cleanup:
4698 lyxp_set_free_content(&xp_set);
4699 lyxp_expr_free((struct ly_ctx *)LYD_CTX(ctx_node), exp);
4700 return ret;
4701}
4702
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01004703LIBYANG_API_DEF LY_ERR
aPiecekfba75362021-10-07 12:39:48 +02004704lyd_eval_xpath(const struct lyd_node *ctx_node, const char *xpath, ly_bool *result)
4705{
4706 return lyd_eval_xpath2(ctx_node, xpath, NULL, result);
4707}
4708
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01004709LIBYANG_API_DEF LY_ERR
Michal Vasko3e1f6552021-01-14 09:27:55 +01004710lyd_find_path(const struct lyd_node *ctx_node, const char *path, ly_bool output, struct lyd_node **match)
4711{
4712 LY_ERR ret = LY_SUCCESS;
4713 struct lyxp_expr *expr = NULL;
4714 struct ly_path *lypath = NULL;
4715
4716 LY_CHECK_ARG_RET(NULL, ctx_node, ctx_node->schema, path, LY_EINVAL);
4717
4718 /* parse the path */
Michal Vaskoed725d72021-06-23 12:03:45 +02004719 ret = ly_path_parse(LYD_CTX(ctx_node), ctx_node->schema, path, strlen(path), 0, LY_PATH_BEGIN_EITHER,
Michal Vasko3e1f6552021-01-14 09:27:55 +01004720 LY_PATH_PREFIX_OPTIONAL, LY_PATH_PRED_SIMPLE, &expr);
4721 LY_CHECK_GOTO(ret, cleanup);
4722
4723 /* compile the path */
Michal Vaskoed725d72021-06-23 12:03:45 +02004724 ret = ly_path_compile(LYD_CTX(ctx_node), NULL, ctx_node->schema, NULL, expr,
Michal Vasko0884d212021-10-14 09:21:46 +02004725 output ? LY_PATH_OPER_OUTPUT : LY_PATH_OPER_INPUT, LY_PATH_TARGET_SINGLE, 0, LY_VALUE_JSON, NULL, &lypath);
Michal Vasko3e1f6552021-01-14 09:27:55 +01004726 LY_CHECK_GOTO(ret, cleanup);
4727
4728 /* evaluate the path */
4729 ret = ly_path_eval_partial(lypath, ctx_node, NULL, match);
4730
4731cleanup:
4732 lyxp_expr_free(LYD_CTX(ctx_node), expr);
4733 ly_path_free(LYD_CTX(ctx_node), lypath);
4734 return ret;
4735}
4736
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01004737LIBYANG_API_DEF LY_ERR
Michal Vaskobb22b182021-06-14 08:14:21 +02004738lyd_find_target(const struct ly_path *path, const struct lyd_node *tree, struct lyd_node **match)
4739{
4740 LY_ERR ret;
4741 struct lyd_node *m;
4742
4743 LY_CHECK_ARG_RET(NULL, path, LY_EINVAL);
4744
4745 ret = ly_path_eval(path, tree, &m);
4746 if (ret) {
4747 if (match) {
4748 *match = NULL;
4749 }
4750 return LY_ENOTFOUND;
4751 }
4752
4753 if (match) {
4754 *match = m;
4755 }
4756 return LY_SUCCESS;
4757}
4758
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01004759LIBYANG_API_DEF uint32_t
Radek Krejcica989142020-11-05 11:32:22 +01004760lyd_list_pos(const struct lyd_node *instance)
4761{
4762 const struct lyd_node *iter = NULL;
4763 uint32_t pos = 0;
4764
4765 if (!instance || !(instance->schema->nodetype & (LYS_LIST | LYS_LEAFLIST))) {
4766 return 0;
4767 }
4768
4769 /* data instances are ordered, so we can stop when we found instance of other schema node */
4770 for (iter = instance; iter->schema == instance->schema; iter = iter->prev) {
Radek Krejcibb27df32020-11-13 15:39:16 +01004771 if (pos && (iter->next == NULL)) {
Radek Krejcica989142020-11-05 11:32:22 +01004772 /* overrun to the end of the siblings list */
4773 break;
4774 }
4775 ++pos;
4776 }
4777
4778 return pos;
4779}
Radek Krejci4233f9b2020-11-05 12:38:35 +01004780
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01004781LIBYANG_API_DEF struct lyd_node *
Radek Krejci4233f9b2020-11-05 12:38:35 +01004782lyd_first_sibling(const struct lyd_node *node)
4783{
4784 struct lyd_node *start;
4785
4786 if (!node) {
4787 return NULL;
4788 }
4789
4790 /* get the first sibling */
4791 if (node->parent) {
4792 start = node->parent->child;
4793 } else {
Radek Krejcibb27df32020-11-13 15:39:16 +01004794 for (start = (struct lyd_node *)node; start->prev->next; start = start->prev) {}
Radek Krejci4233f9b2020-11-05 12:38:35 +01004795 }
4796
4797 return start;
4798}