blob: 893bdd08209706589acea2ba96124bb0263b22df [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 {
Radek Krejci2efc45b2020-12-22 16:25:44 +010087 LOGVAL(ctx, LYVE_OTHER, "Storing value \"%.*s\" failed.", (int)value_len, value);
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átc6e39de2021-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átc6e39de2021-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átc6e39de2021-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 Vaskoaac267d2022-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 Vaskoaac267d2022-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 Vaskoaac267d2022-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 Vaskoaac267d2022-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 Vaskoaac267d2022-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 Vaskoaac267d2022-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átc6e39de2021-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átc6e39de2021-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átc6e39de2021-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átc6e39de2021-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átc6e39de2021-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 Vaskoaac267d2022-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 Vaskoaac267d2022-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 Vaskoaac267d2022-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átc6e39de2021-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átc6e39de2021-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) {
813 any->value.str = value;
814 any->value_type = value_type;
815 } else {
816 any_val.str = value;
Michal Vasko9e685082021-01-29 14:49:09 +0100817 ret = lyd_any_copy_value(&any->node, &any_val, value_type);
Michal Vasko366a4a12020-12-04 16:23:57 +0100818 LY_CHECK_ERR_RET(ret, free(any), ret);
819 }
Michal Vasko9e685082021-01-29 14:49:09 +0100820 lyd_hash(&any->node);
Michal Vasko90932a92020-02-12 14:33:03 +0100821
Michal Vasko9e685082021-01-29 14:49:09 +0100822 *node = &any->node;
Michal Vasko90932a92020-02-12 14:33:03 +0100823 return LY_SUCCESS;
824}
825
Michal Vasko52927e22020-03-16 17:26:14 +0100826LY_ERR
Michal Vasko501af032020-11-11 20:27:44 +0100827lyd_create_opaq(const struct ly_ctx *ctx, const char *name, size_t name_len, const char *prefix, size_t pref_len,
828 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 +0200829 LY_VALUE_FORMAT format, void *val_prefix_data, uint32_t hints, struct lyd_node **node)
Michal Vasko52927e22020-03-16 17:26:14 +0100830{
Radek Krejci011e4aa2020-09-04 15:22:31 +0200831 LY_ERR ret = LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +0100832 struct lyd_node_opaq *opaq;
833
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200834 assert(ctx && name && name_len && format);
Michal Vasko52927e22020-03-16 17:26:14 +0100835
Michal Vasko4cd0a602021-05-31 11:11:58 +0200836 if (!value_len && (!dynamic || !*dynamic)) {
Michal Vasko52927e22020-03-16 17:26:14 +0100837 value = "";
838 }
839
840 opaq = calloc(1, sizeof *opaq);
Michal Vasko501af032020-11-11 20:27:44 +0100841 LY_CHECK_ERR_GOTO(!opaq, LOGMEM(ctx); ret = LY_EMEM, finish);
Michal Vasko52927e22020-03-16 17:26:14 +0100842
Michal Vasko9e685082021-01-29 14:49:09 +0100843 opaq->prev = &opaq->node;
Michal Vaskoad92b672020-11-12 13:11:31 +0100844 LY_CHECK_GOTO(ret = lydict_insert(ctx, name, name_len, &opaq->name.name), finish);
Radek Krejci011e4aa2020-09-04 15:22:31 +0200845
Michal Vasko52927e22020-03-16 17:26:14 +0100846 if (pref_len) {
Michal Vaskoad92b672020-11-12 13:11:31 +0100847 LY_CHECK_GOTO(ret = lydict_insert(ctx, prefix, pref_len, &opaq->name.prefix), finish);
Michal Vasko52927e22020-03-16 17:26:14 +0100848 }
Radek Krejci1798aae2020-07-14 13:26:06 +0200849 if (module_key_len) {
Michal Vaskoad92b672020-11-12 13:11:31 +0100850 LY_CHECK_GOTO(ret = lydict_insert(ctx, module_key, module_key_len, &opaq->name.module_ns), finish);
Radek Krejci1798aae2020-07-14 13:26:06 +0200851 }
Michal Vasko52927e22020-03-16 17:26:14 +0100852 if (dynamic && *dynamic) {
Radek Krejci011e4aa2020-09-04 15:22:31 +0200853 LY_CHECK_GOTO(ret = lydict_insert_zc(ctx, (char *)value, &opaq->value), finish);
Michal Vasko52927e22020-03-16 17:26:14 +0100854 *dynamic = 0;
855 } else {
Radek Krejci011e4aa2020-09-04 15:22:31 +0200856 LY_CHECK_GOTO(ret = lydict_insert(ctx, value, value_len, &opaq->value), finish);
Michal Vasko52927e22020-03-16 17:26:14 +0100857 }
Michal Vasko501af032020-11-11 20:27:44 +0100858
859 opaq->format = format;
860 opaq->val_prefix_data = val_prefix_data;
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200861 opaq->hints = hints;
Michal Vasko52927e22020-03-16 17:26:14 +0100862 opaq->ctx = ctx;
863
Radek Krejci011e4aa2020-09-04 15:22:31 +0200864finish:
865 if (ret) {
Michal Vasko9e685082021-01-29 14:49:09 +0100866 lyd_free_tree(&opaq->node);
Michal Vasko501af032020-11-11 20:27:44 +0100867 ly_free_prefix_data(format, val_prefix_data);
Radek Krejci011e4aa2020-09-04 15:22:31 +0200868 } else {
Michal Vasko9e685082021-01-29 14:49:09 +0100869 *node = &opaq->node;
Radek Krejci011e4aa2020-09-04 15:22:31 +0200870 }
871 return ret;
Michal Vasko52927e22020-03-16 17:26:14 +0100872}
873
Jan Kundrátc6e39de2021-12-09 16:01:19 +0100874LIBYANG_API_DEF LY_ERR
Michal Vasko65243892020-12-04 16:26:21 +0100875lyd_new_inner(struct lyd_node *parent, const struct lys_module *module, const char *name, ly_bool output,
876 struct lyd_node **node)
Michal Vasko013a8182020-03-03 10:46:53 +0100877{
878 struct lyd_node *ret = NULL;
879 const struct lysc_node *schema;
Michal Vasko1d80a532021-11-24 10:41:05 +0100880 const struct ly_ctx *ctx = parent ? LYD_CTX(parent) : (module ? module->ctx : NULL);
Michal Vasko013a8182020-03-03 10:46:53 +0100881
Michal Vasko6027eb92020-07-15 16:37:30 +0200882 LY_CHECK_ARG_RET(ctx, parent || module, parent || node, name, LY_EINVAL);
Michal Vasko1d80a532021-11-24 10:41:05 +0100883 LY_CHECK_CTX_EQUAL_RET(parent ? LYD_CTX(parent) : NULL, module ? module->ctx : NULL, LY_EINVAL);
Michal Vasko013a8182020-03-03 10:46:53 +0100884
Michal Vaskof03ed032020-03-04 13:31:44 +0100885 if (!module) {
886 module = parent->schema->module;
887 }
888
Michal Vasko3a41dff2020-07-15 14:30:28 +0200889 schema = lys_find_child(parent ? parent->schema : NULL, module, name, 0,
Radek Krejci41ac9942020-11-02 14:47:56 +0100890 LYS_CONTAINER | LYS_NOTIF | LYS_RPC | LYS_ACTION, output ? LYS_GETNEXT_OUTPUT : 0);
Radek Krejcidd2a7662021-03-12 11:26:34 +0100891 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 +0100892
Michal Vasko3a41dff2020-07-15 14:30:28 +0200893 LY_CHECK_RET(lyd_create_inner(schema, &ret));
894 if (parent) {
Michal Vasko6ee6f432021-07-16 09:49:14 +0200895 lyd_insert_node(parent, NULL, ret, 0);
Michal Vasko013a8182020-03-03 10:46:53 +0100896 }
Michal Vasko3a41dff2020-07-15 14:30:28 +0200897
898 if (node) {
899 *node = ret;
900 }
901 return LY_SUCCESS;
Michal Vasko013a8182020-03-03 10:46:53 +0100902}
903
Jan Kundrátc6e39de2021-12-09 16:01:19 +0100904LIBYANG_API_DEF LY_ERR
Radek Krejcidd2a7662021-03-12 11:26:34 +0100905lyd_new_ext_inner(const struct lysc_ext_instance *ext, const char *name, struct lyd_node **node)
906{
907 struct lyd_node *ret = NULL;
908 const struct lysc_node *schema;
909 struct ly_ctx *ctx = ext ? ext->module->ctx : NULL;
910
911 LY_CHECK_ARG_RET(ctx, ext, node, name, LY_EINVAL);
912
913 schema = lysc_ext_find_node(ext, NULL, name, 0, LYS_CONTAINER | LYS_NOTIF | LYS_RPC | LYS_ACTION, 0);
914 if (!schema) {
915 if (ext->argument) {
916 LOGERR(ctx, LY_EINVAL, "Inner node (not a list) \"%s\" not found in instance \"%s\" of extension %s.",
917 name, ext->argument, ext->def->name);
918 } else {
919 LOGERR(ctx, LY_EINVAL, "Inner node (not a list) \"%s\" not found in instance of extension %s.",
920 name, ext->def->name);
921 }
922 return LY_ENOTFOUND;
923 }
924 LY_CHECK_RET(lyd_create_inner(schema, &ret));
925
926 *node = ret;
927
928 return LY_SUCCESS;
929}
930
Michal Vasko208e6d62021-06-28 11:23:50 +0200931/**
932 * @brief Create a new list node in the data tree.
933 *
934 * @param[in] parent Parent node for the node being created. NULL in case of creating a top level element.
935 * @param[in] module Module of the node being created. If NULL, @p parent module will be used.
936 * @param[in] name Schema node name of the new data node. The node must be #LYS_LIST.
937 * @param[in] format Format of key values.
938 * @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
939 * taken into consideration. Otherwise, the output's data node is going to be created.
940 * @param[out] node Optional created node.
941 * @param[in] ap Ordered key values of the new list instance, all must be set. For ::LY_VALUE_LYB, every value must
942 * be followed by the value length.
943 * @return LY_ERR value.
944 */
945static LY_ERR
946_lyd_new_list(struct lyd_node *parent, const struct lys_module *module, const char *name, LY_VALUE_FORMAT format,
947 ly_bool output, struct lyd_node **node, va_list ap)
Michal Vasko013a8182020-03-03 10:46:53 +0100948{
949 struct lyd_node *ret = NULL, *key;
950 const struct lysc_node *schema, *key_s;
Michal Vasko1d80a532021-11-24 10:41:05 +0100951 const struct ly_ctx *ctx = parent ? LYD_CTX(parent) : (module ? module->ctx : NULL);
Michal Vasko208e6d62021-06-28 11:23:50 +0200952 const void *key_val;
953 uint32_t key_len;
Michal Vasko013a8182020-03-03 10:46:53 +0100954 LY_ERR rc = LY_SUCCESS;
955
Michal Vasko6027eb92020-07-15 16:37:30 +0200956 LY_CHECK_ARG_RET(ctx, parent || module, parent || node, name, LY_EINVAL);
Michal Vasko1d80a532021-11-24 10:41:05 +0100957 LY_CHECK_CTX_EQUAL_RET(parent ? LYD_CTX(parent) : NULL, module ? module->ctx : NULL, LY_EINVAL);
Michal Vasko013a8182020-03-03 10:46:53 +0100958
Michal Vaskof03ed032020-03-04 13:31:44 +0100959 if (!module) {
960 module = parent->schema->module;
961 }
962
Radek Krejci41ac9942020-11-02 14:47:56 +0100963 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 +0200964 LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "List node \"%s\" not found.", name), LY_ENOTFOUND);
Michal Vasko013a8182020-03-03 10:46:53 +0100965
966 /* create list inner node */
Michal Vasko3a41dff2020-07-15 14:30:28 +0200967 LY_CHECK_RET(lyd_create_inner(schema, &ret));
Michal Vasko013a8182020-03-03 10:46:53 +0100968
Michal Vasko013a8182020-03-03 10:46:53 +0100969 /* create and insert all the keys */
Michal Vasko544e58a2021-01-28 14:33:41 +0100970 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 +0200971 if (format == LY_VALUE_LYB) {
972 key_val = va_arg(ap, const void *);
973 key_len = va_arg(ap, uint32_t);
974 } else {
975 key_val = va_arg(ap, const char *);
976 key_len = key_val ? strlen((char *)key_val) : 0;
977 }
Michal Vasko013a8182020-03-03 10:46:53 +0100978
Michal Vasko208e6d62021-06-28 11:23:50 +0200979 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 +0200980 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko6ee6f432021-07-16 09:49:14 +0200981 lyd_insert_node(ret, NULL, key, 1);
Michal Vasko013a8182020-03-03 10:46:53 +0100982 }
983
Michal Vasko013a8182020-03-03 10:46:53 +0100984 if (parent) {
Michal Vasko6ee6f432021-07-16 09:49:14 +0200985 lyd_insert_node(parent, NULL, ret, 0);
Michal Vasko013a8182020-03-03 10:46:53 +0100986 }
987
988cleanup:
989 if (rc) {
990 lyd_free_tree(ret);
991 ret = NULL;
Michal Vasko3a41dff2020-07-15 14:30:28 +0200992 } else if (node) {
993 *node = ret;
Michal Vasko013a8182020-03-03 10:46:53 +0100994 }
Michal Vasko3a41dff2020-07-15 14:30:28 +0200995 return rc;
Michal Vasko013a8182020-03-03 10:46:53 +0100996}
997
Jan Kundrátc6e39de2021-12-09 16:01:19 +0100998LIBYANG_API_DEF LY_ERR
Michal Vasko208e6d62021-06-28 11:23:50 +0200999lyd_new_list(struct lyd_node *parent, const struct lys_module *module, const char *name, ly_bool output,
1000 struct lyd_node **node, ...)
1001{
1002 LY_ERR rc;
1003 va_list ap;
1004
1005 va_start(ap, node);
1006
1007 rc = _lyd_new_list(parent, module, name, LY_VALUE_JSON, output, node, ap);
1008
1009 va_end(ap);
1010 return rc;
1011}
1012
Jan Kundrátc6e39de2021-12-09 16:01:19 +01001013LIBYANG_API_DEF LY_ERR
Michal Vasko208e6d62021-06-28 11:23:50 +02001014lyd_new_list_bin(struct lyd_node *parent, const struct lys_module *module, const char *name, ly_bool output,
1015 struct lyd_node **node, ...)
1016{
1017 LY_ERR rc;
1018 va_list ap;
1019
1020 va_start(ap, node);
1021
1022 rc = _lyd_new_list(parent, module, name, LY_VALUE_LYB, output, node, ap);
1023
1024 va_end(ap);
1025 return rc;
1026}
1027
Jan Kundrátc6e39de2021-12-09 16:01:19 +01001028LIBYANG_API_DEF LY_ERR
Michal Vasko208e6d62021-06-28 11:23:50 +02001029lyd_new_list_canon(struct lyd_node *parent, const struct lys_module *module, const char *name, ly_bool output,
1030 struct lyd_node **node, ...)
1031{
1032 LY_ERR rc;
1033 va_list ap;
1034
1035 va_start(ap, node);
1036
1037 rc = _lyd_new_list(parent, module, name, LY_VALUE_CANON, output, node, ap);
1038
1039 va_end(ap);
1040 return rc;
1041}
1042
Jan Kundrátc6e39de2021-12-09 16:01:19 +01001043LIBYANG_API_DEF LY_ERR
Radek Krejci8247bae2021-03-12 11:47:02 +01001044lyd_new_ext_list(const struct lysc_ext_instance *ext, const char *name, struct lyd_node **node, ...)
1045{
1046 struct lyd_node *ret = NULL, *key;
1047 const struct lysc_node *schema, *key_s;
1048 struct ly_ctx *ctx = ext ? ext->module->ctx : NULL;
1049 va_list ap;
1050 const char *key_val;
1051 LY_ERR rc = LY_SUCCESS;
1052
1053 LY_CHECK_ARG_RET(ctx, ext, node, name, LY_EINVAL);
1054
1055 schema = lysc_ext_find_node(ext, NULL, name, 0, LYS_LIST, 0);
1056 if (!schema) {
1057 if (ext->argument) {
1058 LOGERR(ctx, LY_EINVAL, "List node \"%s\" not found in instance \"%s\" of extension %s.",
1059 name, ext->argument, ext->def->name);
1060 } else {
1061 LOGERR(ctx, LY_EINVAL, "List node \"%s\" not found in instance of extension %s.", name, ext->def->name);
1062 }
1063 return LY_ENOTFOUND;
1064 }
1065 /* create list inner node */
1066 LY_CHECK_RET(lyd_create_inner(schema, &ret));
1067
1068 va_start(ap, node);
1069
1070 /* create and insert all the keys */
1071 for (key_s = lysc_node_child(schema); key_s && (key_s->flags & LYS_KEY); key_s = key_s->next) {
1072 key_val = va_arg(ap, const char *);
1073
Radek Krejci8df109d2021-04-23 12:19:08 +02001074 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 +01001075 NULL, &key);
1076 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko6ee6f432021-07-16 09:49:14 +02001077 lyd_insert_node(ret, NULL, key, 1);
Radek Krejci8247bae2021-03-12 11:47:02 +01001078 }
1079
1080cleanup:
1081 va_end(ap);
1082 if (rc) {
1083 lyd_free_tree(ret);
1084 ret = NULL;
Radek Krejci8247bae2021-03-12 11:47:02 +01001085 }
Radek Krejci8919fbd2021-03-15 09:35:25 +01001086 *node = ret;
Radek Krejci8247bae2021-03-12 11:47:02 +01001087 return rc;
1088}
1089
Jan Kundrátc6e39de2021-12-09 16:01:19 +01001090LIBYANG_API_DEF LY_ERR
Michal Vasko3a41dff2020-07-15 14:30:28 +02001091lyd_new_list2(struct lyd_node *parent, const struct lys_module *module, const char *name, const char *keys,
Radek Krejci41ac9942020-11-02 14:47:56 +01001092 ly_bool output, struct lyd_node **node)
Michal Vasko013a8182020-03-03 10:46:53 +01001093{
1094 struct lyd_node *ret = NULL;
1095 const struct lysc_node *schema;
Michal Vasko1d80a532021-11-24 10:41:05 +01001096 const struct ly_ctx *ctx = parent ? LYD_CTX(parent) : (module ? module->ctx : NULL);
Michal Vasko013a8182020-03-03 10:46:53 +01001097
Michal Vasko6027eb92020-07-15 16:37:30 +02001098 LY_CHECK_ARG_RET(ctx, parent || module, parent || node, name, LY_EINVAL);
Michal Vasko1d80a532021-11-24 10:41:05 +01001099 LY_CHECK_CTX_EQUAL_RET(parent ? LYD_CTX(parent) : NULL, module ? module->ctx : NULL, LY_EINVAL);
Michal Vasko013a8182020-03-03 10:46:53 +01001100
Michal Vaskof03ed032020-03-04 13:31:44 +01001101 if (!module) {
1102 module = parent->schema->module;
1103 }
Michal Vasko004d3152020-06-11 19:59:22 +02001104 if (!keys) {
1105 keys = "";
1106 }
Michal Vaskof03ed032020-03-04 13:31:44 +01001107
Michal Vasko004d3152020-06-11 19:59:22 +02001108 /* find schema node */
Radek Krejci41ac9942020-11-02 14:47:56 +01001109 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 +02001110 LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "List node \"%s\" not found.", name), LY_ENOTFOUND);
Michal Vasko013a8182020-03-03 10:46:53 +01001111
Michal Vasko004d3152020-06-11 19:59:22 +02001112 if ((schema->flags & LYS_KEYLESS) && !keys[0]) {
1113 /* key-less list */
Michal Vasko3a41dff2020-07-15 14:30:28 +02001114 LY_CHECK_RET(lyd_create_inner(schema, &ret));
Michal Vasko004d3152020-06-11 19:59:22 +02001115 } else {
1116 /* create the list node */
Michal Vasko3a41dff2020-07-15 14:30:28 +02001117 LY_CHECK_RET(lyd_create_list2(schema, keys, strlen(keys), &ret));
Michal Vasko004d3152020-06-11 19:59:22 +02001118 }
Michal Vasko004d3152020-06-11 19:59:22 +02001119 if (parent) {
Michal Vasko6ee6f432021-07-16 09:49:14 +02001120 lyd_insert_node(parent, NULL, ret, 0);
Michal Vasko013a8182020-03-03 10:46:53 +01001121 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02001122
1123 if (node) {
1124 *node = ret;
1125 }
1126 return LY_SUCCESS;
Michal Vasko013a8182020-03-03 10:46:53 +01001127}
1128
Radek Krejci09c77442021-04-26 11:10:34 +02001129/**
1130 * @brief Create a new term node in the data tree.
1131 *
1132 * @param[in] parent Parent node for the node being created. NULL in case of creating a top level element.
1133 * @param[in] module Module of the node being created. If NULL, @p parent module will be used.
1134 * @param[in] name Schema node name of the new data node. The node can be ::LYS_LEAF or ::LYS_LEAFLIST.
1135 * @param[in] value Value of the node being created.
1136 * @param[in] value_len Length of @p value.
1137 * @param[in] format Format of @p value.
1138 * @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
1139 * taken into consideration. Otherwise, the output's data node is going to be created.
1140 * @param[out] node Optional created node.
1141 * @return LY_ERR value.
1142 */
1143static LY_ERR
1144_lyd_new_term(struct lyd_node *parent, const struct lys_module *module, const char *name, const void *value,
1145 size_t value_len, LY_VALUE_FORMAT format, ly_bool output, struct lyd_node **node)
Michal Vasko013a8182020-03-03 10:46:53 +01001146{
Michal Vaskocbff3e92020-05-27 12:56:41 +02001147 LY_ERR rc;
Michal Vasko013a8182020-03-03 10:46:53 +01001148 struct lyd_node *ret = NULL;
1149 const struct lysc_node *schema;
Radek Krejci09c77442021-04-26 11:10:34 +02001150 const struct ly_ctx *ctx = parent ? LYD_CTX(parent) : (module ? module->ctx : NULL);
Michal Vasko013a8182020-03-03 10:46:53 +01001151
Michal Vasko6027eb92020-07-15 16:37:30 +02001152 LY_CHECK_ARG_RET(ctx, parent || module, parent || node, name, LY_EINVAL);
Michal Vasko1d80a532021-11-24 10:41:05 +01001153 LY_CHECK_CTX_EQUAL_RET(parent ? LYD_CTX(parent) : NULL, module ? module->ctx : NULL, LY_EINVAL);
Michal Vasko013a8182020-03-03 10:46:53 +01001154
Michal Vaskof03ed032020-03-04 13:31:44 +01001155 if (!module) {
1156 module = parent->schema->module;
1157 }
1158
Radek Krejci41ac9942020-11-02 14:47:56 +01001159 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 +02001160 LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "Term node \"%s\" not found.", name), LY_ENOTFOUND);
Michal Vasko013a8182020-03-03 10:46:53 +01001161
Radek Krejci09c77442021-04-26 11:10:34 +02001162 rc = lyd_create_term(schema, value, value_len, NULL, format, NULL, LYD_HINT_DATA, NULL, &ret);
Michal Vaskofeca4fb2020-10-05 08:58:40 +02001163 LY_CHECK_RET(rc);
Michal Vaskocbff3e92020-05-27 12:56:41 +02001164 if (parent) {
Michal Vasko6ee6f432021-07-16 09:49:14 +02001165 lyd_insert_node(parent, NULL, ret, 0);
Michal Vasko013a8182020-03-03 10:46:53 +01001166 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02001167
1168 if (node) {
1169 *node = ret;
1170 }
1171 return LY_SUCCESS;
Michal Vasko013a8182020-03-03 10:46:53 +01001172}
1173
Jan Kundrátc6e39de2021-12-09 16:01:19 +01001174LIBYANG_API_DEF LY_ERR
Radek Krejci09c77442021-04-26 11:10:34 +02001175lyd_new_term(struct lyd_node *parent, const struct lys_module *module, const char *name, const char *val_str,
1176 ly_bool output, struct lyd_node **node)
1177{
1178 return _lyd_new_term(parent, module, name, val_str, val_str ? strlen(val_str) : 0, LY_VALUE_JSON, output, node);
1179}
1180
Jan Kundrátc6e39de2021-12-09 16:01:19 +01001181LIBYANG_API_DEF LY_ERR
Radek Krejci09c77442021-04-26 11:10:34 +02001182lyd_new_term_bin(struct lyd_node *parent, const struct lys_module *module, const char *name, const void *value,
1183 size_t value_len, ly_bool output, struct lyd_node **node)
1184{
1185 return _lyd_new_term(parent, module, name, value, value_len, LY_VALUE_LYB, output, node);
1186}
1187
Jan Kundrátc6e39de2021-12-09 16:01:19 +01001188LIBYANG_API_DEF LY_ERR
Radek Krejci09c77442021-04-26 11:10:34 +02001189lyd_new_term_canon(struct lyd_node *parent, const struct lys_module *module, const char *name, const char *val_str,
1190 ly_bool output, struct lyd_node **node)
1191{
1192 return _lyd_new_term(parent, module, name, val_str, val_str ? strlen(val_str) : 0, LY_VALUE_CANON, output, node);
1193}
1194
Jan Kundrátc6e39de2021-12-09 16:01:19 +01001195LIBYANG_API_DEF LY_ERR
Radek Krejci8a5afc22021-03-12 11:10:47 +01001196lyd_new_ext_term(const struct lysc_ext_instance *ext, const char *name, const char *val_str, struct lyd_node **node)
1197{
1198 LY_ERR rc;
1199 struct lyd_node *ret = NULL;
1200 const struct lysc_node *schema;
1201 struct ly_ctx *ctx = ext ? ext->module->ctx : NULL;
1202
1203 LY_CHECK_ARG_RET(ctx, ext, node, name, LY_EINVAL);
1204
1205 schema = lysc_ext_find_node(ext, NULL, name, 0, LYD_NODE_TERM, 0);
1206 if (!schema) {
1207 if (ext->argument) {
1208 LOGERR(ctx, LY_EINVAL, "Term node \"%s\" not found in instance \"%s\" of extension %s.",
1209 name, ext->argument, ext->def->name);
1210 } else {
1211 LOGERR(ctx, LY_EINVAL, "Term node \"%s\" not found in instance of extension %s.", name, ext->def->name);
1212 }
1213 return LY_ENOTFOUND;
1214 }
Radek Krejci8df109d2021-04-23 12:19:08 +02001215 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 +01001216 LY_CHECK_RET(rc);
1217
1218 *node = ret;
1219
1220 return LY_SUCCESS;
1221}
1222
Jan Kundrátc6e39de2021-12-09 16:01:19 +01001223LIBYANG_API_DEF LY_ERR
Michal Vasko2a4ab2b2021-03-04 15:52:40 +01001224lyd_new_any(struct lyd_node *parent, const struct lys_module *module, const char *name, const void *value,
1225 ly_bool use_value, LYD_ANYDATA_VALUETYPE value_type, ly_bool output, struct lyd_node **node)
Michal Vasko013a8182020-03-03 10:46:53 +01001226{
1227 struct lyd_node *ret = NULL;
1228 const struct lysc_node *schema;
Michal Vasko1d80a532021-11-24 10:41:05 +01001229 const struct ly_ctx *ctx = parent ? LYD_CTX(parent) : (module ? module->ctx : NULL);
Michal Vasko013a8182020-03-03 10:46:53 +01001230
Michal Vasko6027eb92020-07-15 16:37:30 +02001231 LY_CHECK_ARG_RET(ctx, parent || module, parent || node, name, LY_EINVAL);
Michal Vasko1d80a532021-11-24 10:41:05 +01001232 LY_CHECK_CTX_EQUAL_RET(parent ? LYD_CTX(parent) : NULL, module ? module->ctx : NULL, LY_EINVAL);
Michal Vasko013a8182020-03-03 10:46:53 +01001233
Michal Vaskof03ed032020-03-04 13:31:44 +01001234 if (!module) {
1235 module = parent->schema->module;
1236 }
1237
Radek Krejci41ac9942020-11-02 14:47:56 +01001238 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 +02001239 LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "Any node \"%s\" not found.", name), LY_ENOTFOUND);
Michal Vasko013a8182020-03-03 10:46:53 +01001240
Michal Vasko2a4ab2b2021-03-04 15:52:40 +01001241 LY_CHECK_RET(lyd_create_any(schema, value, value_type, use_value, &ret));
Michal Vasko3a41dff2020-07-15 14:30:28 +02001242 if (parent) {
Michal Vasko6ee6f432021-07-16 09:49:14 +02001243 lyd_insert_node(parent, NULL, ret, 0);
Michal Vasko013a8182020-03-03 10:46:53 +01001244 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02001245
1246 if (node) {
1247 *node = ret;
1248 }
1249 return LY_SUCCESS;
Michal Vasko013a8182020-03-03 10:46:53 +01001250}
1251
Jan Kundrátc6e39de2021-12-09 16:01:19 +01001252LIBYANG_API_DEF LY_ERR
Radek Krejci0b963da2021-03-12 13:23:44 +01001253lyd_new_ext_any(const struct lysc_ext_instance *ext, const char *name, const void *value, ly_bool use_value,
1254 LYD_ANYDATA_VALUETYPE value_type, struct lyd_node **node)
1255{
1256 struct lyd_node *ret = NULL;
1257 const struct lysc_node *schema;
1258 struct ly_ctx *ctx = ext ? ext->module->ctx : NULL;
1259
1260 LY_CHECK_ARG_RET(ctx, ext, node, name, LY_EINVAL);
1261
1262 schema = lysc_ext_find_node(ext, NULL, name, 0, LYD_NODE_ANY, 0);
1263 if (!schema) {
1264 if (ext->argument) {
1265 LOGERR(ctx, LY_EINVAL, "Any node \"%s\" not found in instance \"%s\" of extension %s.",
1266 name, ext->argument, ext->def->name);
1267 } else {
1268 LOGERR(ctx, LY_EINVAL, "Any node \"%s\" not found in instance of extension %s.", name, ext->def->name);
1269 }
1270 return LY_ENOTFOUND;
1271 }
1272 LY_CHECK_RET(lyd_create_any(schema, value, value_type, use_value, &ret));
1273
1274 *node = ret;
1275
1276 return LY_SUCCESS;
1277}
1278
Jan Kundrátc6e39de2021-12-09 16:01:19 +01001279LIBYANG_API_DEF LY_ERR
Michal Vasko871a0252020-11-11 18:35:24 +01001280lyd_new_meta(const struct ly_ctx *ctx, struct lyd_node *parent, const struct lys_module *module, const char *name,
1281 const char *val_str, ly_bool clear_dflt, struct lyd_meta **meta)
Michal Vaskod86997b2020-05-26 15:19:54 +02001282{
Michal Vaskod86997b2020-05-26 15:19:54 +02001283 const char *prefix, *tmp;
Michal Vaskod86997b2020-05-26 15:19:54 +02001284 size_t pref_len, name_len;
1285
Michal Vasko2b5344c2021-02-26 10:12:05 +01001286 LY_CHECK_ARG_RET(ctx, ctx || parent, name, module || strchr(name, ':'), parent || meta, LY_EINVAL);
Michal Vasko1d80a532021-11-24 10:41:05 +01001287 LY_CHECK_CTX_EQUAL_RET(ctx, parent ? LYD_CTX(parent) : NULL, module ? module->ctx : NULL, LY_EINVAL);
Michal Vasko2b5344c2021-02-26 10:12:05 +01001288 if (!ctx) {
1289 ctx = LYD_CTX(parent);
1290 }
Michal Vasko00cbf532020-06-15 13:58:47 +02001291
Michal Vasko871a0252020-11-11 18:35:24 +01001292 if (parent && !parent->schema) {
Michal Vasko33b4fab2021-03-04 15:56:12 +01001293 LOGERR(ctx, LY_EINVAL, "Cannot add metadata \"%s\" to an opaque node \"%s\".", name, LYD_NAME(parent));
Michal Vasko871a0252020-11-11 18:35:24 +01001294 return LY_EINVAL;
1295 }
Michal Vasko610553d2020-11-18 18:15:05 +01001296 if (meta) {
1297 *meta = NULL;
1298 }
Michal Vaskod86997b2020-05-26 15:19:54 +02001299
1300 /* parse the name */
1301 tmp = name;
1302 if (ly_parse_nodeid(&tmp, &prefix, &pref_len, &name, &name_len) || tmp[0]) {
1303 LOGERR(ctx, LY_EINVAL, "Metadata name \"%s\" is not valid.", name);
Michal Vasko871a0252020-11-11 18:35:24 +01001304 return LY_EINVAL;
Michal Vaskod86997b2020-05-26 15:19:54 +02001305 }
1306
1307 /* find the module */
1308 if (prefix) {
Radek Krejci0ad51f12020-07-16 12:08:12 +02001309 module = ly_ctx_get_module_implemented2(ctx, prefix, pref_len);
Radek Krejci422afb12021-03-04 16:38:16 +01001310 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 +02001311 }
1312
1313 /* set value if none */
1314 if (!val_str) {
1315 val_str = "";
1316 }
1317
Radek Krejci8df109d2021-04-23 12:19:08 +02001318 return lyd_create_meta(parent, meta, module, name, name_len, val_str, strlen(val_str), NULL, LY_VALUE_JSON,
Michal Vaskoaac267d2022-01-17 13:34:48 +01001319 NULL, LYD_HINT_DATA, parent ? parent->schema : NULL, clear_dflt, NULL);
Michal Vasko871a0252020-11-11 18:35:24 +01001320}
Michal Vasko3a41dff2020-07-15 14:30:28 +02001321
Jan Kundrátc6e39de2021-12-09 16:01:19 +01001322LIBYANG_API_DEF LY_ERR
Michal Vaskoba696702020-11-11 19:12:45 +01001323lyd_new_meta2(const struct ly_ctx *ctx, struct lyd_node *parent, ly_bool clear_dflt, const struct lyd_attr *attr,
1324 struct lyd_meta **meta)
1325{
1326 const struct lys_module *mod;
1327
1328 LY_CHECK_ARG_RET(NULL, ctx, attr, parent || meta, LY_EINVAL);
Michal Vasko1d80a532021-11-24 10:41:05 +01001329 LY_CHECK_CTX_EQUAL_RET(ctx, parent ? LYD_CTX(parent) : NULL, LY_EINVAL);
Michal Vaskoba696702020-11-11 19:12:45 +01001330
1331 if (parent && !parent->schema) {
1332 LOGERR(ctx, LY_EINVAL, "Cannot add metadata to an opaque node \"%s\".", ((struct lyd_node_opaq *)parent)->name);
1333 return LY_EINVAL;
Michal Vasko3a41dff2020-07-15 14:30:28 +02001334 }
Michal Vasko610553d2020-11-18 18:15:05 +01001335 if (meta) {
1336 *meta = NULL;
1337 }
Michal Vaskoba696702020-11-11 19:12:45 +01001338
1339 switch (attr->format) {
Radek Krejci8df109d2021-04-23 12:19:08 +02001340 case LY_VALUE_XML:
Michal Vaskoad92b672020-11-12 13:11:31 +01001341 mod = ly_ctx_get_module_implemented_ns(ctx, attr->name.module_ns);
Michal Vaskoba696702020-11-11 19:12:45 +01001342 if (!mod) {
Michal Vaskoad92b672020-11-12 13:11:31 +01001343 LOGERR(ctx, LY_EINVAL, "Module with namespace \"%s\" not found.", attr->name.module_ns);
Michal Vaskoba696702020-11-11 19:12:45 +01001344 return LY_ENOTFOUND;
1345 }
1346 break;
Radek Krejci8df109d2021-04-23 12:19:08 +02001347 case LY_VALUE_JSON:
Michal Vaskoad92b672020-11-12 13:11:31 +01001348 mod = ly_ctx_get_module_implemented(ctx, attr->name.module_name);
Michal Vaskoba696702020-11-11 19:12:45 +01001349 if (!mod) {
Michal Vaskoad92b672020-11-12 13:11:31 +01001350 LOGERR(ctx, LY_EINVAL, "Module \"%s\" not found.", attr->name.module_name);
Michal Vaskoba696702020-11-11 19:12:45 +01001351 return LY_ENOTFOUND;
1352 }
1353 break;
1354 default:
1355 LOGINT_RET(ctx);
1356 }
1357
Michal Vaskoad92b672020-11-12 13:11:31 +01001358 return lyd_create_meta(parent, meta, mod, attr->name.name, strlen(attr->name.name), attr->value, strlen(attr->value),
Michal Vaskoaac267d2022-01-17 13:34:48 +01001359 NULL, attr->format, attr->val_prefix_data, attr->hints, parent ? parent->schema : NULL, clear_dflt, NULL);
Michal Vaskod86997b2020-05-26 15:19:54 +02001360}
1361
Jan Kundrátc6e39de2021-12-09 16:01:19 +01001362LIBYANG_API_DEF LY_ERR
Michal Vasko00cbf532020-06-15 13:58:47 +02001363lyd_new_opaq(struct lyd_node *parent, const struct ly_ctx *ctx, const char *name, const char *value,
Michal Vasko0ab974d2021-02-24 13:18:26 +01001364 const char *prefix, const char *module_name, struct lyd_node **node)
Michal Vasko00cbf532020-06-15 13:58:47 +02001365{
1366 struct lyd_node *ret = NULL;
Radek Krejci07a55962021-03-02 20:16:43 +01001367
Michal Vasko0ab974d2021-02-24 13:18:26 +01001368 LY_CHECK_ARG_RET(ctx, parent || ctx, parent || node, name, module_name, !prefix || !strcmp(prefix, module_name), LY_EINVAL);
Michal Vasko1d80a532021-11-24 10:41:05 +01001369 LY_CHECK_CTX_EQUAL_RET(ctx, parent ? LYD_CTX(parent) : NULL, LY_EINVAL);
Michal Vasko00cbf532020-06-15 13:58:47 +02001370
1371 if (!ctx) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02001372 ctx = LYD_CTX(parent);
Michal Vasko00cbf532020-06-15 13:58:47 +02001373 }
1374 if (!value) {
1375 value = "";
1376 }
1377
Michal Vasko0ab974d2021-02-24 13:18:26 +01001378 LY_CHECK_RET(lyd_create_opaq(ctx, name, strlen(name), prefix, prefix ? strlen(prefix) : 0, module_name,
Radek Krejci8df109d2021-04-23 12:19:08 +02001379 strlen(module_name), value, strlen(value), NULL, LY_VALUE_JSON, NULL, 0, &ret));
Michal Vasko3a41dff2020-07-15 14:30:28 +02001380 if (parent) {
Michal Vasko6ee6f432021-07-16 09:49:14 +02001381 lyd_insert_node(parent, NULL, ret, 1);
Michal Vasko00cbf532020-06-15 13:58:47 +02001382 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02001383
1384 if (node) {
1385 *node = ret;
1386 }
1387 return LY_SUCCESS;
Michal Vasko00cbf532020-06-15 13:58:47 +02001388}
1389
Jan Kundrátc6e39de2021-12-09 16:01:19 +01001390LIBYANG_API_DEF LY_ERR
Michal Vasko8d65f852021-02-17 11:28:13 +01001391lyd_new_opaq2(struct lyd_node *parent, const struct ly_ctx *ctx, const char *name, const char *value,
Michal Vasko0ab974d2021-02-24 13:18:26 +01001392 const char *prefix, const char *module_ns, struct lyd_node **node)
Michal Vasko8d65f852021-02-17 11:28:13 +01001393{
1394 struct lyd_node *ret = NULL;
1395
1396 LY_CHECK_ARG_RET(ctx, parent || ctx, parent || node, name, module_ns, LY_EINVAL);
Michal Vasko1d80a532021-11-24 10:41:05 +01001397 LY_CHECK_CTX_EQUAL_RET(ctx, parent ? LYD_CTX(parent) : NULL, LY_EINVAL);
Michal Vasko8d65f852021-02-17 11:28:13 +01001398
1399 if (!ctx) {
1400 ctx = LYD_CTX(parent);
1401 }
1402 if (!value) {
1403 value = "";
1404 }
1405
Michal Vasko0ab974d2021-02-24 13:18:26 +01001406 LY_CHECK_RET(lyd_create_opaq(ctx, name, strlen(name), prefix, prefix ? strlen(prefix) : 0, module_ns,
Radek Krejci8df109d2021-04-23 12:19:08 +02001407 strlen(module_ns), value, strlen(value), NULL, LY_VALUE_XML, NULL, 0, &ret));
Michal Vasko8d65f852021-02-17 11:28:13 +01001408 if (parent) {
Michal Vasko6ee6f432021-07-16 09:49:14 +02001409 lyd_insert_node(parent, NULL, ret, 1);
Michal Vasko8d65f852021-02-17 11:28:13 +01001410 }
1411
1412 if (node) {
1413 *node = ret;
1414 }
1415 return LY_SUCCESS;
1416}
1417
Jan Kundrátc6e39de2021-12-09 16:01:19 +01001418LIBYANG_API_DEF LY_ERR
Michal Vasko8d65f852021-02-17 11:28:13 +01001419lyd_new_attr(struct lyd_node *parent, const char *module_name, const char *name, const char *value,
Radek Krejci0f969882020-08-21 16:56:47 +02001420 struct lyd_attr **attr)
Michal Vasko00cbf532020-06-15 13:58:47 +02001421{
Radek Krejci1798aae2020-07-14 13:26:06 +02001422 struct lyd_attr *ret = NULL;
Michal Vasko00cbf532020-06-15 13:58:47 +02001423 const struct ly_ctx *ctx;
1424 const char *prefix, *tmp;
Michal Vasko51d21b82020-11-13 18:03:54 +01001425 size_t pref_len, name_len, mod_len;
Michal Vasko00cbf532020-06-15 13:58:47 +02001426
Michal Vasko3a41dff2020-07-15 14:30:28 +02001427 LY_CHECK_ARG_RET(NULL, parent, !parent->schema, name, LY_EINVAL);
Michal Vasko00cbf532020-06-15 13:58:47 +02001428
Michal Vaskob7be7a82020-08-20 09:09:04 +02001429 ctx = LYD_CTX(parent);
Michal Vasko00cbf532020-06-15 13:58:47 +02001430
1431 /* parse the name */
1432 tmp = name;
1433 if (ly_parse_nodeid(&tmp, &prefix, &pref_len, &name, &name_len) || tmp[0]) {
Michal Vaskob69b68c2021-02-17 11:18:16 +01001434 LOGERR(ctx, LY_EINVAL, "Attribute name \"%s\" is not valid.", name);
Michal Vasko3a41dff2020-07-15 14:30:28 +02001435 return LY_EVALID;
Michal Vasko00cbf532020-06-15 13:58:47 +02001436 }
1437
Michal Vaskoe137fc42021-07-22 11:53:13 +02001438 if ((pref_len == 3) && !strncmp(prefix, "xml", 3)) {
1439 /* not a prefix but special name */
1440 name = prefix;
1441 name_len += 1 + pref_len;
1442 prefix = NULL;
1443 pref_len = 0;
1444 }
1445
Michal Vasko51d21b82020-11-13 18:03:54 +01001446 /* get the module */
1447 if (module_name) {
1448 mod_len = strlen(module_name);
1449 } else {
1450 module_name = prefix;
1451 mod_len = pref_len;
1452 }
1453
Michal Vasko00cbf532020-06-15 13:58:47 +02001454 /* set value if none */
Michal Vasko8d65f852021-02-17 11:28:13 +01001455 if (!value) {
1456 value = "";
Michal Vasko00cbf532020-06-15 13:58:47 +02001457 }
1458
Michal Vasko8d65f852021-02-17 11:28:13 +01001459 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 +02001460 strlen(value), NULL, LY_VALUE_JSON, NULL, LYD_HINT_DATA));
Michal Vasko8d65f852021-02-17 11:28:13 +01001461
1462 if (attr) {
1463 *attr = ret;
1464 }
1465 return LY_SUCCESS;
1466}
1467
Jan Kundrátc6e39de2021-12-09 16:01:19 +01001468LIBYANG_API_DEF LY_ERR
Michal Vasko8d65f852021-02-17 11:28:13 +01001469lyd_new_attr2(struct lyd_node *parent, const char *module_ns, const char *name, const char *value,
1470 struct lyd_attr **attr)
1471{
1472 struct lyd_attr *ret = NULL;
1473 const struct ly_ctx *ctx;
1474 const char *prefix, *tmp;
1475 size_t pref_len, name_len;
1476
1477 LY_CHECK_ARG_RET(NULL, parent, !parent->schema, name, LY_EINVAL);
1478
1479 ctx = LYD_CTX(parent);
1480
1481 /* parse the name */
1482 tmp = name;
1483 if (ly_parse_nodeid(&tmp, &prefix, &pref_len, &name, &name_len) || tmp[0]) {
1484 LOGERR(ctx, LY_EINVAL, "Attribute name \"%s\" is not valid.", name);
1485 return LY_EVALID;
1486 }
1487
Michal Vaskoe137fc42021-07-22 11:53:13 +02001488 if ((pref_len == 3) && !strncmp(prefix, "xml", 3)) {
1489 /* not a prefix but special name */
1490 name = prefix;
1491 name_len += 1 + pref_len;
1492 prefix = NULL;
1493 pref_len = 0;
1494 }
1495
Michal Vasko8d65f852021-02-17 11:28:13 +01001496 /* set value if none */
1497 if (!value) {
1498 value = "";
1499 }
1500
1501 LY_CHECK_RET(lyd_create_attr(parent, &ret, ctx, name, name_len, prefix, pref_len, module_ns,
Radek Krejci8df109d2021-04-23 12:19:08 +02001502 module_ns ? strlen(module_ns) : 0, value, strlen(value), NULL, LY_VALUE_XML, NULL, LYD_HINT_DATA));
Michal Vasko3a41dff2020-07-15 14:30:28 +02001503
1504 if (attr) {
1505 *attr = ret;
1506 }
1507 return LY_SUCCESS;
Michal Vasko00cbf532020-06-15 13:58:47 +02001508}
1509
Radek Krejci09c77442021-04-26 11:10:34 +02001510/**
1511 * @brief Change the value of a term (leaf or leaf-list) node.
1512 *
1513 * Node changed this way is always considered explicitly set, meaning its default flag
1514 * is always cleared.
1515 *
1516 * @param[in] term Term node to change.
1517 * @param[in] value New value to set.
1518 * @param[in] value_len Length of @p value.
1519 * @param[in] format Format of @p value.
1520 * @return LY_SUCCESS if value was changed,
1521 * @return LY_EEXIST if value was the same and only the default flag was cleared,
1522 * @return LY_ENOT if the values were equal and no change occured,
1523 * @return LY_ERR value on other errors.
1524 */
1525static LY_ERR
1526_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 +02001527{
1528 LY_ERR ret = LY_SUCCESS;
1529 struct lysc_type *type;
1530 struct lyd_node_term *t;
1531 struct lyd_node *parent;
1532 struct lyd_value val = {0};
Radek Krejci857189e2020-09-01 13:26:36 +02001533 ly_bool dflt_change, val_change;
Michal Vasko00cbf532020-06-15 13:58:47 +02001534
Radek Krejci09c77442021-04-26 11:10:34 +02001535 assert(term && term->schema && (term->schema->nodetype & LYD_NODE_TERM));
Michal Vasko00cbf532020-06-15 13:58:47 +02001536
Michal Vasko00cbf532020-06-15 13:58:47 +02001537 t = (struct lyd_node_term *)term;
1538 type = ((struct lysc_node_leaf *)term->schema)->type;
1539
1540 /* parse the new value */
Radek Krejciddace2c2021-01-08 11:30:56 +01001541 LOG_LOCSET(term->schema, term, NULL, NULL);
Radek Krejci09c77442021-04-26 11:10:34 +02001542 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 +01001543 LOG_LOCBACK(term->schema ? 1 : 0, 1, 0, 0);
Michal Vaskofeca4fb2020-10-05 08:58:40 +02001544 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko00cbf532020-06-15 13:58:47 +02001545
1546 /* compare original and new value */
1547 if (type->plugin->compare(&t->value, &val)) {
1548 /* values differ, switch them */
Michal Vaskob7be7a82020-08-20 09:09:04 +02001549 type->plugin->free(LYD_CTX(term), &t->value);
Michal Vasko00cbf532020-06-15 13:58:47 +02001550 t->value = val;
1551 memset(&val, 0, sizeof val);
1552 val_change = 1;
1553 } else {
1554 val_change = 0;
1555 }
1556
1557 /* always clear the default flag */
1558 if (term->flags & LYD_DEFAULT) {
Michal Vasko9e685082021-01-29 14:49:09 +01001559 for (parent = term; parent; parent = lyd_parent(parent)) {
Michal Vasko00cbf532020-06-15 13:58:47 +02001560 parent->flags &= ~LYD_DEFAULT;
1561 }
1562 dflt_change = 1;
1563 } else {
1564 dflt_change = 0;
1565 }
1566
1567 if (val_change || dflt_change) {
1568 /* make the node non-validated */
1569 term->flags &= LYD_NEW;
1570 }
1571
1572 if (val_change) {
1573 if (term->schema->nodetype == LYS_LEAFLIST) {
1574 /* leaf-list needs to be hashed again and re-inserted into parent */
1575 lyd_unlink_hash(term);
1576 lyd_hash(term);
1577 LY_CHECK_GOTO(ret = lyd_insert_hash(term), cleanup);
1578 } else if ((term->schema->flags & LYS_KEY) && term->parent) {
1579 /* list needs to be updated if its key was changed */
1580 assert(term->parent->schema->nodetype == LYS_LIST);
Michal Vasko9e685082021-01-29 14:49:09 +01001581 lyd_unlink_hash(lyd_parent(term));
1582 lyd_hash(lyd_parent(term));
1583 LY_CHECK_GOTO(ret = lyd_insert_hash(lyd_parent(term)), cleanup);
Michal Vasko00cbf532020-06-15 13:58:47 +02001584 } /* else leaf that is not a key, its value is not used for its hash so it does not change */
1585 }
1586
1587 /* retrun value */
1588 if (!val_change) {
1589 if (dflt_change) {
1590 /* only default flag change */
1591 ret = LY_EEXIST;
1592 } else {
1593 /* no change */
1594 ret = LY_ENOT;
1595 }
1596 } /* else value changed, LY_SUCCESS */
1597
1598cleanup:
Michal Vasko59512fc2020-12-09 18:13:29 +01001599 if (val.realtype) {
1600 type->plugin->free(LYD_CTX(term), &val);
1601 }
Michal Vasko00cbf532020-06-15 13:58:47 +02001602 return ret;
1603}
1604
Jan Kundrátc6e39de2021-12-09 16:01:19 +01001605LIBYANG_API_DEF LY_ERR
Radek Krejci09c77442021-04-26 11:10:34 +02001606lyd_change_term(struct lyd_node *term, const char *val_str)
1607{
1608 LY_CHECK_ARG_RET(NULL, term, term->schema, term->schema->nodetype & LYD_NODE_TERM, LY_EINVAL);
1609
1610 return _lyd_change_term(term, val_str, val_str ? strlen(val_str) : 0, LY_VALUE_JSON);
1611}
1612
Jan Kundrátc6e39de2021-12-09 16:01:19 +01001613LIBYANG_API_DEF LY_ERR
Radek Krejci09c77442021-04-26 11:10:34 +02001614lyd_change_term_bin(struct lyd_node *term, const void *value, size_t value_len)
1615{
1616 LY_CHECK_ARG_RET(NULL, term, term->schema, term->schema->nodetype & LYD_NODE_TERM, LY_EINVAL);
1617
1618 return _lyd_change_term(term, value, value_len, LY_VALUE_LYB);
1619}
1620
Jan Kundrátc6e39de2021-12-09 16:01:19 +01001621LIBYANG_API_DEF LY_ERR
Radek Krejci09c77442021-04-26 11:10:34 +02001622lyd_change_term_canon(struct lyd_node *term, const char *val_str)
1623{
1624 LY_CHECK_ARG_RET(NULL, term, term->schema, term->schema->nodetype & LYD_NODE_TERM, LY_EINVAL);
1625
1626 return _lyd_change_term(term, val_str, val_str ? strlen(val_str) : 0, LY_VALUE_CANON);
1627}
1628
Jan Kundrátc6e39de2021-12-09 16:01:19 +01001629LIBYANG_API_DEF LY_ERR
Michal Vasko41586352020-07-13 13:54:25 +02001630lyd_change_meta(struct lyd_meta *meta, const char *val_str)
1631{
1632 LY_ERR ret = LY_SUCCESS;
Radek Krejci2b18bf12020-11-06 11:20:20 +01001633 struct lyd_meta *m2 = NULL;
Michal Vasko41586352020-07-13 13:54:25 +02001634 struct lyd_value val;
Radek Krejci857189e2020-09-01 13:26:36 +02001635 ly_bool val_change;
Michal Vasko41586352020-07-13 13:54:25 +02001636
1637 LY_CHECK_ARG_RET(NULL, meta, LY_EINVAL);
1638
1639 if (!val_str) {
1640 val_str = "";
1641 }
1642
1643 /* parse the new value into a new meta structure */
Michal Vaskoaac267d2022-01-17 13:34:48 +01001644 ret = lyd_create_meta(NULL, &m2, meta->annotation->module, meta->name, strlen(meta->name), val_str, strlen(val_str),
1645 NULL, LY_VALUE_JSON, NULL, LYD_HINT_DATA, meta->parent ? meta->parent->schema : NULL, 0, NULL);
1646 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko41586352020-07-13 13:54:25 +02001647
1648 /* compare original and new value */
1649 if (lyd_compare_meta(meta, m2)) {
1650 /* values differ, switch them */
1651 val = meta->value;
1652 meta->value = m2->value;
1653 m2->value = val;
1654 val_change = 1;
1655 } else {
1656 val_change = 0;
1657 }
1658
1659 /* retrun value */
1660 if (!val_change) {
1661 /* no change */
1662 ret = LY_ENOT;
1663 } /* else value changed, LY_SUCCESS */
1664
1665cleanup:
Michal Vasko1a66a5d2020-11-18 18:15:37 +01001666 lyd_free_meta_single(m2);
Michal Vasko41586352020-07-13 13:54:25 +02001667 return ret;
1668}
1669
Radek Krejci09c77442021-04-26 11:10:34 +02001670/**
1671 * @brief Update node value.
1672 *
1673 * @param[in] node Node to update.
1674 * @param[in] value New value to set.
1675 * @param[in] value_len Length of @p value.
1676 * @param[in] value_type Type of @p value for anydata/anyxml node.
1677 * @param[in] format Format of @p value.
1678 * @param[out] new_parent Set to @p node if the value was updated, otherwise set to NULL.
1679 * @param[out] new_node Set to @p node if the value was updated, otherwise set to NULL.
1680 * @return LY_ERR value.
1681 */
Michal Vasko6741dc62021-02-04 09:27:45 +01001682static LY_ERR
Radek Krejci09c77442021-04-26 11:10:34 +02001683lyd_new_path_update(struct lyd_node *node, const void *value, size_t value_len, LYD_ANYDATA_VALUETYPE value_type,
1684 LY_VALUE_FORMAT format, struct lyd_node **new_parent, struct lyd_node **new_node)
1685{
1686 LY_ERR ret = LY_SUCCESS;
1687 struct lyd_node *new_any;
1688
1689 switch (node->schema->nodetype) {
1690 case LYS_CONTAINER:
1691 case LYS_NOTIF:
1692 case LYS_RPC:
1693 case LYS_ACTION:
1694 case LYS_LIST:
1695 /* if it exists, there is nothing to update */
1696 *new_parent = NULL;
1697 *new_node = NULL;
1698 break;
1699 case LYS_LEAFLIST:
1700 if (!lysc_is_dup_inst_list(node->schema)) {
1701 /* if it exists, there is nothing to update */
1702 *new_parent = NULL;
1703 *new_node = NULL;
1704 break;
1705 }
1706 /* fallthrough */
1707 case LYS_LEAF:
1708 ret = _lyd_change_term(node, value, value_len, format);
1709 if ((ret == LY_SUCCESS) || (ret == LY_EEXIST)) {
1710 /* there was an actual change (at least of the default flag) */
1711 *new_parent = node;
1712 *new_node = node;
1713 ret = LY_SUCCESS;
1714 } else if (ret == LY_ENOT) {
1715 /* no change */
1716 *new_parent = NULL;
1717 *new_node = NULL;
1718 ret = LY_SUCCESS;
1719 } /* else error */
1720 break;
1721 case LYS_ANYDATA:
1722 case LYS_ANYXML:
1723 /* create a new any node */
1724 LY_CHECK_RET(lyd_create_any(node->schema, value, value_type, 0, &new_any));
1725
1726 /* compare with the existing one */
1727 if (lyd_compare_single(node, new_any, 0)) {
1728 /* not equal, switch values (so that we can use generic node free) */
1729 ((struct lyd_node_any *)new_any)->value = ((struct lyd_node_any *)node)->value;
1730 ((struct lyd_node_any *)new_any)->value_type = ((struct lyd_node_any *)node)->value_type;
1731 ((struct lyd_node_any *)node)->value.str = value;
1732 ((struct lyd_node_any *)node)->value_type = value_type;
1733
1734 *new_parent = node;
1735 *new_node = node;
1736 } else {
1737 /* they are equal */
1738 *new_parent = NULL;
1739 *new_node = NULL;
1740 }
1741 lyd_free_tree(new_any);
1742 break;
1743 default:
1744 LOGINT(LYD_CTX(node));
1745 ret = LY_EINT;
1746 break;
1747 }
1748
1749 return ret;
1750}
1751
1752static LY_ERR
1753lyd_new_path_check_find_lypath(struct ly_path *path, const char *str_path, const char *value, size_t value_len,
1754 LY_VALUE_FORMAT format, uint32_t options)
Michal Vasko6741dc62021-02-04 09:27:45 +01001755{
Michal Vaskoe78faec2021-04-08 17:24:43 +02001756 LY_ERR r;
Michal Vasko6741dc62021-02-04 09:27:45 +01001757 struct ly_path_predicate *pred;
Michal Vasko04726242022-01-10 09:30:08 +01001758 struct lyd_value val;
Michal Vaskoe78faec2021-04-08 17:24:43 +02001759 const struct lysc_node *schema = NULL;
1760 LY_ARRAY_COUNT_TYPE u, new_count;
1761 int create = 0;
Michal Vasko6741dc62021-02-04 09:27:45 +01001762
1763 assert(path);
1764
1765 /* go through all the compiled nodes */
1766 LY_ARRAY_FOR(path, u) {
1767 schema = path[u].node;
Michal Vaskoe78faec2021-04-08 17:24:43 +02001768
1769 if (lysc_is_dup_inst_list(schema)) {
1770 if (path[u].pred_type == LY_PATH_PREDTYPE_NONE) {
1771 /* creating a new key-less list or state leaf-list instance */
1772 create = 1;
1773 new_count = u;
1774 } else if (path[u].pred_type != LY_PATH_PREDTYPE_POSITION) {
1775 LOG_LOCSET(schema, NULL, NULL, NULL);
1776 LOGVAL(schema->module->ctx, LYVE_XPATH, "Invalid predicate for %s \"%s\" in path \"%s\".",
1777 lys_nodetype2str(schema->nodetype), schema->name, str_path);
1778 LOG_LOCBACK(1, 0, 0, 0);
1779 return LY_EINVAL;
1780 }
1781 } else if ((schema->nodetype == LYS_LIST) && (path[u].pred_type != LY_PATH_PREDTYPE_LIST)) {
1782 if ((u < LY_ARRAY_COUNT(path) - 1) || !(options & LYD_NEW_PATH_OPAQ)) {
Michal Vasko6741dc62021-02-04 09:27:45 +01001783 LOG_LOCSET(schema, NULL, NULL, NULL);
1784 LOGVAL(schema->module->ctx, LYVE_XPATH, "Predicate missing for %s \"%s\" in path \"%s\".",
1785 lys_nodetype2str(schema->nodetype), schema->name, str_path);
1786 LOG_LOCBACK(1, 0, 0, 0);
1787 return LY_EINVAL;
Michal Vaskoe78faec2021-04-08 17:24:43 +02001788 } /* else creating an opaque list */
1789 } else if ((schema->nodetype == LYS_LEAFLIST) && (path[u].pred_type != LY_PATH_PREDTYPE_LEAFLIST)) {
Michal Vaskoe78faec2021-04-08 17:24:43 +02001790 r = LY_SUCCESS;
1791 if (options & LYD_NEW_PATH_OPAQ) {
Radek Krejci09c77442021-04-26 11:10:34 +02001792 r = lyd_value_validate(NULL, schema, value, value_len, NULL, NULL, NULL);
Michal Vaskoe78faec2021-04-08 17:24:43 +02001793 }
1794 if (!r) {
Michal Vasko04726242022-01-10 09:30:08 +01001795 /* try to store the value */
1796 LY_CHECK_RET(lyd_value_store(schema->module->ctx, &val, ((struct lysc_node_leaflist *)schema)->type,
1797 value, value_len, NULL, format, NULL, LYD_HINT_DATA, schema, NULL));
1798 ++((struct lysc_type *)val.realtype)->refcount;
1799
Michal Vaskoe78faec2021-04-08 17:24:43 +02001800 /* store the new predicate so that it is used when searching for this instance */
1801 path[u].pred_type = LY_PATH_PREDTYPE_LEAFLIST;
1802 LY_ARRAY_NEW_RET(schema->module->ctx, path[u].predicates, pred, LY_EMEM);
Michal Vasko04726242022-01-10 09:30:08 +01001803 pred->value = val;
Michal Vaskoe78faec2021-04-08 17:24:43 +02001804 } /* else we have opaq flag and the value is not valid, leave no predicate and then create an opaque node */
1805 }
Michal Vasko6741dc62021-02-04 09:27:45 +01001806 }
1807
Michal Vaskoe78faec2021-04-08 17:24:43 +02001808 if (create) {
1809 /* hide the nodes that should always be created so they are not found */
1810 while (new_count < LY_ARRAY_COUNT(path)) {
1811 LY_ARRAY_DECREMENT(path);
1812 }
Michal Vasko6741dc62021-02-04 09:27:45 +01001813 }
1814
Michal Vaskoe78faec2021-04-08 17:24:43 +02001815 return LY_SUCCESS;
Michal Vasko6741dc62021-02-04 09:27:45 +01001816}
1817
Radek Krejci95ccd1b2021-03-12 14:57:22 +01001818/**
1819 * @brief Create a new node in the data tree based on a path. All node types can be created.
1820 *
Michal Vaskoa918a632021-07-02 11:53:36 +02001821 * If @p path points to a list key, the key value from the predicate is used and @p value is ignored.
1822 * Also, if a leaf-list is being created and both a predicate is defined in @p path
Radek Krejci95ccd1b2021-03-12 14:57:22 +01001823 * and @p value is set, the predicate is preferred.
1824 *
1825 * For key-less lists and state leaf-lists, positional predicates can be used. If no preciate is used for these
1826 * nodes, they are always created.
1827 *
1828 * @param[in] parent Data parent to add to/modify, can be NULL. Note that in case a first top-level sibling is used,
1829 * it may no longer be first if @p path is absolute and starts with a non-existing top-level node inserted
1830 * before @p parent. Use ::lyd_first_sibling() to adjust @p parent in these cases.
1831 * @param[in] ctx libyang context, must be set if @p parent is NULL.
1832 * @param[in] ext Extension instance where the node being created is defined. This argument takes effect only for absolute
1833 * path or when the relative paths touches document root (top-level). In such cases the present extension instance replaces
1834 * searching for the appropriate module.
1835 * @param[in] path [Path](@ref howtoXPath) to create.
Radek Krejci09c77442021-04-26 11:10:34 +02001836 * @param[in] value Value of the new leaf/leaf-list (const char *) in ::LY_VALUE_JSON format. If creating an
1837 * anyxml/anydata node, the expected type depends on @p value_type. For other node types, it should be NULL.
1838 * @param[in] value_len Length of @p value in bytes. May be 0 if @p value is a zero-terminated string. Ignored when
1839 * creating anyxml/anydata nodes.
Radek Krejci95ccd1b2021-03-12 14:57:22 +01001840 * @param[in] value_type Anyxml/anydata node @p value type.
1841 * @param[in] options Bitmask of options, see @ref pathoptions.
1842 * @param[out] new_parent Optional first parent node created. If only one node was created, equals to @p new_node.
1843 * @param[out] new_node Optional last node created.
1844 * @return LY_ERR value.
1845 */
1846static LY_ERR
1847lyd_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 +02001848 const void *value, size_t value_len, LYD_ANYDATA_VALUETYPE value_type, uint32_t options,
Radek Krejci95ccd1b2021-03-12 14:57:22 +01001849 struct lyd_node **new_parent, struct lyd_node **new_node)
Michal Vasko00cbf532020-06-15 13:58:47 +02001850{
1851 LY_ERR ret = LY_SUCCESS, r;
1852 struct lyxp_expr *exp = NULL;
1853 struct ly_path *p = NULL;
1854 struct lyd_node *nparent = NULL, *nnode = NULL, *node = NULL, *cur_parent;
1855 const struct lysc_node *schema;
Michal Vaskoe78faec2021-04-08 17:24:43 +02001856 const struct lyd_value *val = NULL;
Radek Krejci910d0132021-03-05 14:19:31 +01001857 LY_ARRAY_COUNT_TYPE path_idx = 0, orig_count = 0;
Radek Krejci09c77442021-04-26 11:10:34 +02001858 LY_VALUE_FORMAT format;
Michal Vasko00cbf532020-06-15 13:58:47 +02001859
Radek Krejci95ccd1b2021-03-12 14:57:22 +01001860 assert(parent || ctx);
Radek Krejci09c77442021-04-26 11:10:34 +02001861 assert(path && ((path[0] == '/') || parent));
1862 assert(!(options & LYD_NEW_PATH_BIN_VALUE) || !(options & LYD_NEW_PATH_CANON_VALUE));
Michal Vasko00cbf532020-06-15 13:58:47 +02001863
1864 if (!ctx) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02001865 ctx = LYD_CTX(parent);
Michal Vasko00cbf532020-06-15 13:58:47 +02001866 }
Radek Krejci09c77442021-04-26 11:10:34 +02001867 if (value && !value_len) {
1868 value_len = strlen(value);
1869 }
1870 if (options & LYD_NEW_PATH_BIN_VALUE) {
1871 format = LY_VALUE_LYB;
1872 } else if (options & LYD_NEW_PATH_CANON_VALUE) {
1873 format = LY_VALUE_CANON;
1874 } else {
1875 format = LY_VALUE_JSON;
1876 }
Michal Vasko00cbf532020-06-15 13:58:47 +02001877
1878 /* parse path */
Michal Vaskoed725d72021-06-23 12:03:45 +02001879 LY_CHECK_GOTO(ret = ly_path_parse(ctx, NULL, path, strlen(path), 0, LY_PATH_BEGIN_EITHER, LY_PATH_PREFIX_OPTIONAL,
1880 LY_PATH_PRED_SIMPLE, &exp), cleanup);
Michal Vasko00cbf532020-06-15 13:58:47 +02001881
1882 /* compile path */
Michal Vasko106f0862021-11-02 11:49:27 +01001883 LY_CHECK_GOTO(ret = ly_path_compile(ctx, NULL, lyd_node_schema(parent), ext, exp, options & LYD_NEW_PATH_OUTPUT ?
1884 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 +02001885
Michal Vasko6741dc62021-02-04 09:27:45 +01001886 /* check the compiled path before searching existing nodes, it may be shortened */
1887 orig_count = LY_ARRAY_COUNT(p);
Radek Krejci09c77442021-04-26 11:10:34 +02001888 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 +02001889
1890 /* try to find any existing nodes in the path */
1891 if (parent) {
1892 ret = ly_path_eval_partial(p, parent, &path_idx, &node);
1893 if (ret == LY_SUCCESS) {
Michal Vasko6741dc62021-02-04 09:27:45 +01001894 if (orig_count == LY_ARRAY_COUNT(p)) {
1895 /* the node exists, are we supposed to update it or is it just a default? */
1896 if (!(options & LYD_NEW_PATH_UPDATE) && !(node->flags & LYD_DEFAULT)) {
1897 LOG_LOCSET(NULL, node, NULL, NULL);
1898 LOGVAL(ctx, LYVE_REFERENCE, "Path \"%s\" already exists", path);
1899 LOG_LOCBACK(0, 1, 0, 0);
1900 ret = LY_EEXIST;
1901 goto cleanup;
1902 }
Michal Vasko00cbf532020-06-15 13:58:47 +02001903
Michal Vasko6741dc62021-02-04 09:27:45 +01001904 /* update the existing node */
Radek Krejci09c77442021-04-26 11:10:34 +02001905 ret = lyd_new_path_update(node, value, value_len, value_type, format, &nparent, &nnode);
Michal Vasko6741dc62021-02-04 09:27:45 +01001906 goto cleanup;
1907 } /* else we were not searching for the whole path */
Michal Vasko00cbf532020-06-15 13:58:47 +02001908 } else if (ret == LY_EINCOMPLETE) {
1909 /* some nodes were found, adjust the iterator to the next segment */
1910 ++path_idx;
1911 } else if (ret == LY_ENOTFOUND) {
1912 /* 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 +01001913 if (lysc_data_parent(p[0].node)) {
Michal Vasko00cbf532020-06-15 13:58:47 +02001914 node = parent;
1915 }
1916 } else {
1917 /* error */
1918 goto cleanup;
1919 }
1920 }
1921
Michal Vasko6741dc62021-02-04 09:27:45 +01001922 /* restore the full path for creating new nodes */
1923 while (orig_count > LY_ARRAY_COUNT(p)) {
1924 LY_ARRAY_INCREMENT(p);
1925 }
1926
Michal Vasko00cbf532020-06-15 13:58:47 +02001927 /* create all the non-existing nodes in a loop */
Michal Vaskod989ba02020-08-24 10:59:24 +02001928 for ( ; path_idx < LY_ARRAY_COUNT(p); ++path_idx) {
Michal Vasko00cbf532020-06-15 13:58:47 +02001929 cur_parent = node;
1930 schema = p[path_idx].node;
1931
1932 switch (schema->nodetype) {
1933 case LYS_LIST:
Michal Vaskoe78faec2021-04-08 17:24:43 +02001934 if (lysc_is_dup_inst_list(schema)) {
1935 /* create key-less list instance */
1936 LY_CHECK_GOTO(ret = lyd_create_inner(schema, &node), cleanup);
1937 } else if ((options & LYD_NEW_PATH_OPAQ) && (p[path_idx].pred_type == LY_PATH_PREDTYPE_NONE)) {
1938 /* creating opaque list without keys */
1939 LY_CHECK_GOTO(ret = lyd_create_opaq(ctx, schema->name, strlen(schema->name), NULL, 0,
Radek Krejci8df109d2021-04-23 12:19:08 +02001940 schema->module->name, strlen(schema->module->name), NULL, 0, NULL, LY_VALUE_JSON, NULL,
Michal Vaskoe78faec2021-04-08 17:24:43 +02001941 LYD_NODEHINT_LIST, &node), cleanup);
1942 } else {
1943 /* create standard list instance */
1944 assert(p[path_idx].pred_type == LY_PATH_PREDTYPE_LIST);
1945 LY_CHECK_GOTO(ret = lyd_create_list(schema, p[path_idx].predicates, &node), cleanup);
Michal Vasko00cbf532020-06-15 13:58:47 +02001946 }
Michal Vaskoe78faec2021-04-08 17:24:43 +02001947 break;
Michal Vasko00cbf532020-06-15 13:58:47 +02001948 case LYS_CONTAINER:
1949 case LYS_NOTIF:
1950 case LYS_RPC:
1951 case LYS_ACTION:
1952 LY_CHECK_GOTO(ret = lyd_create_inner(schema, &node), cleanup);
1953 break;
1954 case LYS_LEAFLIST:
Michal Vaskoe78faec2021-04-08 17:24:43 +02001955 if ((options & LYD_NEW_PATH_OPAQ) && (p[path_idx].pred_type != LY_PATH_PREDTYPE_LEAFLIST)) {
1956 /* we have not checked this only for dup-inst lists, otherwise it must be opaque */
1957 r = LY_EVALID;
1958 if (lysc_is_dup_inst_list(schema)) {
1959 /* validate value */
Radek Krejci09c77442021-04-26 11:10:34 +02001960 r = lyd_value_validate(NULL, schema, value ? value : "", value_len, NULL, NULL, NULL);
Michal Vaskoe78faec2021-04-08 17:24:43 +02001961 }
1962 if (r && (r != LY_EINCOMPLETE)) {
1963 /* creating opaque leaf-list */
Radek Krejci09c77442021-04-26 11:10:34 +02001964 LY_CHECK_GOTO(ret = lyd_create_opaq(ctx, schema->name, strlen(schema->name), value, value_len,
1965 schema->module->name, strlen(schema->module->name), NULL, 0, NULL, format, NULL,
1966 LYD_NODEHINT_LEAFLIST, &node), cleanup);
Michal Vaskoe78faec2021-04-08 17:24:43 +02001967 break;
1968 }
1969 }
1970
1971 /* get value to set */
1972 if (p[path_idx].pred_type == LY_PATH_PREDTYPE_LEAFLIST) {
1973 val = &p[path_idx].predicates[0].value;
Michal Vaskoe78faec2021-04-08 17:24:43 +02001974 }
1975
1976 /* create a leaf-list instance */
1977 if (val) {
Michal Vasko00cbf532020-06-15 13:58:47 +02001978 LY_CHECK_GOTO(ret = lyd_create_term2(schema, &p[path_idx].predicates[0].value, &node), cleanup);
Michal Vaskoe78faec2021-04-08 17:24:43 +02001979 } else {
Radek Krejci09c77442021-04-26 11:10:34 +02001980 LY_CHECK_GOTO(ret = lyd_create_term(schema, value, value_len, NULL, format, NULL, LYD_HINT_DATA,
1981 NULL, &node), cleanup);
Michal Vasko00cbf532020-06-15 13:58:47 +02001982 }
1983 break;
1984 case LYS_LEAF:
Michal Vasko106f0862021-11-02 11:49:27 +01001985 if (lysc_is_key(schema) && cur_parent->schema) {
Michal Vasko35880332020-12-08 13:08:12 +01001986 /* it must have been already created or some error will occur later */
Michal Vaskoa918a632021-07-02 11:53:36 +02001987 lyd_find_sibling_schema(lyd_child(cur_parent), schema, &node);
1988 assert(node);
Michal Vasko35880332020-12-08 13:08:12 +01001989 goto next_iter;
1990 }
1991
Michal Vaskoe78faec2021-04-08 17:24:43 +02001992 if (options & LYD_NEW_PATH_OPAQ) {
Michal Vasko106f0862021-11-02 11:49:27 +01001993 if (cur_parent && !cur_parent->schema) {
1994 /* always create opaque nodes for opaque parents */
1995 r = LY_ENOT;
1996 } else {
1997 /* validate value */
1998 r = lyd_value_validate(NULL, schema, value ? value : "", value_len, NULL, NULL, NULL);
1999 }
Michal Vaskoe78faec2021-04-08 17:24:43 +02002000 if (r && (r != LY_EINCOMPLETE)) {
2001 /* creating opaque leaf */
Radek Krejci09c77442021-04-26 11:10:34 +02002002 LY_CHECK_GOTO(ret = lyd_create_opaq(ctx, schema->name, strlen(schema->name), value, value_len,
2003 schema->module->name, strlen(schema->module->name), NULL, 0, NULL, format, NULL, 0, &node),
2004 cleanup);
Michal Vaskoe78faec2021-04-08 17:24:43 +02002005 break;
2006 }
2007 }
2008
Michal Vaskoe78faec2021-04-08 17:24:43 +02002009 /* create a leaf instance */
Radek Krejci09c77442021-04-26 11:10:34 +02002010 LY_CHECK_GOTO(ret = lyd_create_term(schema, value, value_len, NULL, format, NULL, LYD_HINT_DATA, NULL,
2011 &node), cleanup);
Michal Vasko00cbf532020-06-15 13:58:47 +02002012 break;
2013 case LYS_ANYDATA:
2014 case LYS_ANYXML:
Michal Vasko366a4a12020-12-04 16:23:57 +01002015 LY_CHECK_GOTO(ret = lyd_create_any(schema, value, value_type, 0, &node), cleanup);
Michal Vasko00cbf532020-06-15 13:58:47 +02002016 break;
2017 default:
2018 LOGINT(ctx);
2019 ret = LY_EINT;
2020 goto cleanup;
2021 }
2022
2023 if (cur_parent) {
2024 /* connect to the parent */
Michal Vasko6ee6f432021-07-16 09:49:14 +02002025 lyd_insert_node(cur_parent, NULL, node, 0);
Michal Vasko00cbf532020-06-15 13:58:47 +02002026 } else if (parent) {
2027 /* connect to top-level siblings */
Michal Vasko6ee6f432021-07-16 09:49:14 +02002028 lyd_insert_node(NULL, &parent, node, 0);
Michal Vasko00cbf532020-06-15 13:58:47 +02002029 }
2030
Michal Vasko35880332020-12-08 13:08:12 +01002031next_iter:
Michal Vasko00cbf532020-06-15 13:58:47 +02002032 /* update remembered nodes */
2033 if (!nparent) {
2034 nparent = node;
2035 }
2036 nnode = node;
2037 }
2038
2039cleanup:
2040 lyxp_expr_free(ctx, exp);
Michal Vasko6741dc62021-02-04 09:27:45 +01002041 if (p) {
2042 while (orig_count > LY_ARRAY_COUNT(p)) {
2043 LY_ARRAY_INCREMENT(p);
2044 }
2045 }
Michal Vasko00cbf532020-06-15 13:58:47 +02002046 ly_path_free(ctx, p);
2047 if (!ret) {
2048 /* set out params only on success */
2049 if (new_parent) {
2050 *new_parent = nparent;
2051 }
2052 if (new_node) {
2053 *new_node = nnode;
2054 }
Michal Vaskof4b95ba2020-12-11 08:42:33 +01002055 } else {
2056 lyd_free_tree(nparent);
Michal Vasko00cbf532020-06-15 13:58:47 +02002057 }
2058 return ret;
2059}
2060
Jan Kundrátc6e39de2021-12-09 16:01:19 +01002061LIBYANG_API_DEF LY_ERR
Radek Krejci95ccd1b2021-03-12 14:57:22 +01002062lyd_new_path(struct lyd_node *parent, const struct ly_ctx *ctx, const char *path, const char *value, uint32_t options,
2063 struct lyd_node **node)
2064{
Radek Krejci09c77442021-04-26 11:10:34 +02002065 LY_CHECK_ARG_RET(ctx, parent || ctx, path, (path[0] == '/') || parent,
2066 !(options & LYD_NEW_PATH_BIN_VALUE) || !(options & LYD_NEW_PATH_CANON_VALUE), LY_EINVAL);
Michal Vasko1d80a532021-11-24 10:41:05 +01002067 LY_CHECK_CTX_EQUAL_RET(parent ? LYD_CTX(parent) : NULL, ctx, LY_EINVAL);
Radek Krejci09c77442021-04-26 11:10:34 +02002068
2069 return lyd_new_path_(parent, ctx, NULL, path, value, 0, LYD_ANYDATA_STRING, options, node, NULL);
Radek Krejci95ccd1b2021-03-12 14:57:22 +01002070}
2071
Jan Kundrátc6e39de2021-12-09 16:01:19 +01002072LIBYANG_API_DEF LY_ERR
Radek Krejci95ccd1b2021-03-12 14:57:22 +01002073lyd_new_path2(struct lyd_node *parent, const struct ly_ctx *ctx, const char *path, const void *value,
Radek Krejci09c77442021-04-26 11:10:34 +02002074 size_t value_len, LYD_ANYDATA_VALUETYPE value_type, uint32_t options, struct lyd_node **new_parent,
2075 struct lyd_node **new_node)
Radek Krejci95ccd1b2021-03-12 14:57:22 +01002076{
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 Vasko1d80a532021-11-24 10:41:05 +01002079 LY_CHECK_CTX_EQUAL_RET(parent ? LYD_CTX(parent) : NULL, ctx, LY_EINVAL);
2080
Radek Krejci09c77442021-04-26 11:10:34 +02002081 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 +01002082}
2083
Jan Kundrátc6e39de2021-12-09 16:01:19 +01002084LIBYANG_API_DEF LY_ERR
Radek Krejci95ccd1b2021-03-12 14:57:22 +01002085lyd_new_ext_path(struct lyd_node *parent, const struct lysc_ext_instance *ext, const char *path, const void *value,
2086 uint32_t options, struct lyd_node **node)
2087{
2088 const struct ly_ctx *ctx = ext ? ext->module->ctx : NULL;
2089
Radek Krejci09c77442021-04-26 11:10:34 +02002090 LY_CHECK_ARG_RET(ctx, ext, path, (path[0] == '/') || parent,
2091 !(options & LYD_NEW_PATH_BIN_VALUE) || !(options & LYD_NEW_PATH_CANON_VALUE), LY_EINVAL);
Michal Vasko1d80a532021-11-24 10:41:05 +01002092 LY_CHECK_CTX_EQUAL_RET(parent ? LYD_CTX(parent) : NULL, ctx, LY_EINVAL);
2093
Radek Krejci09c77442021-04-26 11:10:34 +02002094 return lyd_new_path_(parent, ctx, ext, path, value, 0, LYD_ANYDATA_STRING, options, node, NULL);
Radek Krejci95ccd1b2021-03-12 14:57:22 +01002095}
2096
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002097LY_ERR
2098lyd_new_implicit_r(struct lyd_node *parent, struct lyd_node **first, const struct lysc_node *sparent,
Michal Vaskoaac267d2022-01-17 13:34:48 +01002099 const struct lys_module *mod, struct ly_set *node_when, struct ly_set *node_types, uint32_t impl_opts,
2100 struct lyd_node **diff)
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002101{
2102 LY_ERR ret;
Michal Vaskod1e53b92021-01-28 13:11:06 +01002103 const struct lysc_node *iter = NULL;
Radek Krejci2b18bf12020-11-06 11:20:20 +01002104 struct lyd_node *node = NULL;
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002105 struct lyd_value **dflts;
2106 LY_ARRAY_COUNT_TYPE u;
Michal Vasko630d9892020-12-08 17:11:08 +01002107 uint32_t getnext_opts;
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002108
2109 assert(first && (parent || sparent || mod));
2110
2111 if (!sparent && parent) {
2112 sparent = parent->schema;
2113 }
2114
Michal Vasko630d9892020-12-08 17:11:08 +01002115 getnext_opts = LYS_GETNEXT_WITHCHOICE;
2116 if (impl_opts & LYD_IMPLICIT_OUTPUT) {
2117 getnext_opts |= LYS_GETNEXT_OUTPUT;
2118 }
2119
2120 while ((iter = lys_getnext(iter, sparent, mod ? mod->compiled : NULL, getnext_opts))) {
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002121 if ((impl_opts & LYD_IMPLICIT_NO_STATE) && (iter->flags & LYS_CONFIG_R)) {
2122 continue;
Michal Vasko44b19a12020-08-07 09:21:30 +02002123 } else if ((impl_opts & LYD_IMPLICIT_NO_CONFIG) && (iter->flags & LYS_CONFIG_W)) {
2124 continue;
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002125 }
2126
2127 switch (iter->nodetype) {
2128 case LYS_CHOICE:
Michal Vaskobcf4d7d2020-11-18 18:16:49 +01002129 node = lys_getnext_data(NULL, *first, NULL, iter, NULL);
2130 if (!node && ((struct lysc_node_choice *)iter)->dflt) {
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002131 /* create default case data */
Michal Vasko14ed9cd2021-01-28 14:16:25 +01002132 LY_CHECK_RET(lyd_new_implicit_r(parent, first, &((struct lysc_node_choice *)iter)->dflt->node,
Michal Vaskoaac267d2022-01-17 13:34:48 +01002133 NULL, node_when, node_types, impl_opts, diff));
Michal Vaskobcf4d7d2020-11-18 18:16:49 +01002134 } else if (node) {
2135 /* create any default data in the existing case */
2136 assert(node->schema->parent->nodetype == LYS_CASE);
Michal Vaskoaac267d2022-01-17 13:34:48 +01002137 LY_CHECK_RET(lyd_new_implicit_r(parent, first, node->schema->parent, NULL, node_when, node_types,
Michal Vaskobcf4d7d2020-11-18 18:16:49 +01002138 impl_opts, diff));
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002139 }
2140 break;
2141 case LYS_CONTAINER:
2142 if (!(iter->flags & LYS_PRESENCE) && lyd_find_sibling_val(*first, iter, NULL, 0, NULL)) {
2143 /* create default NP container */
2144 LY_CHECK_RET(lyd_create_inner(iter, &node));
Michal Vaskof4d67ea2021-03-31 13:53:21 +02002145 node->flags = LYD_DEFAULT | (lysc_has_when(iter) ? LYD_WHEN_TRUE : 0);
Michal Vasko6ee6f432021-07-16 09:49:14 +02002146 lyd_insert_node(parent, first, node, 0);
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002147
Michal Vaskof4d67ea2021-03-31 13:53:21 +02002148 if (lysc_has_when(iter) && node_when) {
Michal Vaskoe16c7b72021-02-26 10:39:06 +01002149 /* remember to resolve when */
2150 LY_CHECK_RET(ly_set_add(node_when, node, 1, NULL));
2151 }
Michal Vaskoe49b6322020-11-05 17:38:36 +01002152 if (diff) {
2153 /* add into diff */
2154 LY_CHECK_RET(lyd_val_diff_add(node, LYD_DIFF_OP_CREATE, diff));
2155 }
2156
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002157 /* create any default children */
Michal Vaskoaac267d2022-01-17 13:34:48 +01002158 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 +02002159 impl_opts, diff));
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002160 }
2161 break;
2162 case LYS_LEAF:
Michal Vasko69730152020-10-09 16:30:07 +02002163 if (!(impl_opts & LYD_IMPLICIT_NO_DEFAULTS) && ((struct lysc_node_leaf *)iter)->dflt &&
2164 lyd_find_sibling_val(*first, iter, NULL, 0, NULL)) {
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002165 /* create default leaf */
2166 ret = lyd_create_term2(iter, ((struct lysc_node_leaf *)iter)->dflt, &node);
2167 if (ret == LY_EINCOMPLETE) {
2168 if (node_types) {
2169 /* remember to resolve type */
Radek Krejci3d92e442020-10-12 12:48:13 +02002170 LY_CHECK_RET(ly_set_add(node_types, node, 1, NULL));
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002171 }
2172 } else if (ret) {
2173 return ret;
2174 }
Michal Vaskof4d67ea2021-03-31 13:53:21 +02002175 node->flags = LYD_DEFAULT | (lysc_has_when(iter) ? LYD_WHEN_TRUE : 0);
Michal Vasko6ee6f432021-07-16 09:49:14 +02002176 lyd_insert_node(parent, first, node, 0);
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002177
Michal Vaskof4d67ea2021-03-31 13:53:21 +02002178 if (lysc_has_when(iter) && node_when) {
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002179 /* remember to resolve when */
Radek Krejci3d92e442020-10-12 12:48:13 +02002180 LY_CHECK_RET(ly_set_add(node_when, node, 1, NULL));
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002181 }
2182 if (diff) {
2183 /* add into diff */
2184 LY_CHECK_RET(lyd_val_diff_add(node, LYD_DIFF_OP_CREATE, diff));
2185 }
2186 }
2187 break;
2188 case LYS_LEAFLIST:
Michal Vasko69730152020-10-09 16:30:07 +02002189 if (!(impl_opts & LYD_IMPLICIT_NO_DEFAULTS) && ((struct lysc_node_leaflist *)iter)->dflts &&
2190 lyd_find_sibling_val(*first, iter, NULL, 0, NULL)) {
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002191 /* create all default leaf-lists */
2192 dflts = ((struct lysc_node_leaflist *)iter)->dflts;
2193 LY_ARRAY_FOR(dflts, u) {
2194 ret = lyd_create_term2(iter, dflts[u], &node);
2195 if (ret == LY_EINCOMPLETE) {
2196 if (node_types) {
2197 /* remember to resolve type */
Radek Krejci3d92e442020-10-12 12:48:13 +02002198 LY_CHECK_RET(ly_set_add(node_types, node, 1, NULL));
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002199 }
2200 } else if (ret) {
2201 return ret;
2202 }
Michal Vaskof4d67ea2021-03-31 13:53:21 +02002203 node->flags = LYD_DEFAULT | (lysc_has_when(iter) ? LYD_WHEN_TRUE : 0);
Michal Vasko6ee6f432021-07-16 09:49:14 +02002204 lyd_insert_node(parent, first, node, 0);
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002205
Michal Vaskof4d67ea2021-03-31 13:53:21 +02002206 if (lysc_has_when(iter) && node_when) {
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002207 /* remember to resolve when */
Radek Krejci3d92e442020-10-12 12:48:13 +02002208 LY_CHECK_RET(ly_set_add(node_when, node, 1, NULL));
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002209 }
2210 if (diff) {
2211 /* add into diff */
2212 LY_CHECK_RET(lyd_val_diff_add(node, LYD_DIFF_OP_CREATE, diff));
2213 }
2214 }
2215 }
2216 break;
2217 default:
2218 /* without defaults */
2219 break;
2220 }
2221 }
2222
2223 return LY_SUCCESS;
2224}
2225
Jan Kundrátc6e39de2021-12-09 16:01:19 +01002226LIBYANG_API_DEF LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02002227lyd_new_implicit_tree(struct lyd_node *tree, uint32_t implicit_options, struct lyd_node **diff)
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002228{
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002229 LY_ERR ret = LY_SUCCESS;
Michal Vasko3488ada2020-12-03 14:18:19 +01002230 struct lyd_node *node;
Michal Vaskoaac267d2022-01-17 13:34:48 +01002231 struct ly_set node_when = {0};
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002232
2233 LY_CHECK_ARG_RET(NULL, tree, LY_EINVAL);
2234 if (diff) {
2235 *diff = NULL;
2236 }
2237
Michal Vasko56daf732020-08-10 10:57:18 +02002238 LYD_TREE_DFS_BEGIN(tree, node) {
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002239 /* skip added default nodes */
Michal Vasko69730152020-10-09 16:30:07 +02002240 if (((node->flags & (LYD_DEFAULT | LYD_NEW)) != (LYD_DEFAULT | LYD_NEW)) &&
2241 (node->schema->nodetype & LYD_NODE_INNER)) {
Michal Vaskoaac267d2022-01-17 13:34:48 +01002242 LY_CHECK_GOTO(ret = lyd_new_implicit_r(node, lyd_node_child_p(node), NULL, NULL, &node_when, NULL,
2243 implicit_options, diff), cleanup);
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002244 }
2245
Michal Vasko56daf732020-08-10 10:57:18 +02002246 LYD_TREE_DFS_END(tree, node);
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002247 }
2248
Michal Vasko3488ada2020-12-03 14:18:19 +01002249 /* resolve when and remove any invalid defaults */
Michal Vaskoaac267d2022-01-17 13:34:48 +01002250 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 +01002251
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002252cleanup:
Michal Vasko3488ada2020-12-03 14:18:19 +01002253 ly_set_erase(&node_when, NULL);
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002254 if (ret && diff) {
2255 lyd_free_all(*diff);
2256 *diff = NULL;
2257 }
2258 return ret;
2259}
2260
Jan Kundrátc6e39de2021-12-09 16:01:19 +01002261LIBYANG_API_DEF LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02002262lyd_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 +02002263{
2264 const struct lys_module *mod;
2265 struct lyd_node *d = NULL;
2266 uint32_t i = 0;
2267 LY_ERR ret = LY_SUCCESS;
2268
2269 LY_CHECK_ARG_RET(ctx, tree, *tree || ctx, LY_EINVAL);
Michal Vasko1d80a532021-11-24 10:41:05 +01002270 LY_CHECK_CTX_EQUAL_RET(*tree ? LYD_CTX(*tree) : NULL, ctx, LY_EINVAL);
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002271 if (diff) {
2272 *diff = NULL;
2273 }
2274 if (!ctx) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02002275 ctx = LYD_CTX(*tree);
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002276 }
2277
2278 /* add nodes for each module one-by-one */
2279 while ((mod = ly_ctx_get_module_iter(ctx, &i))) {
2280 if (!mod->implemented) {
2281 continue;
2282 }
2283
2284 LY_CHECK_GOTO(ret = lyd_new_implicit_module(tree, mod, implicit_options, diff ? &d : NULL), cleanup);
2285 if (d) {
2286 /* merge into one diff */
2287 lyd_insert_sibling(*diff, d, diff);
2288
2289 d = NULL;
2290 }
2291 }
2292
2293cleanup:
2294 if (ret && diff) {
2295 lyd_free_all(*diff);
2296 *diff = NULL;
2297 }
2298 return ret;
2299}
2300
Jan Kundrátc6e39de2021-12-09 16:01:19 +01002301LIBYANG_API_DEF LY_ERR
Michal Vasko1d80a532021-11-24 10:41:05 +01002302lyd_new_implicit_module(struct lyd_node **tree, const struct lys_module *module, uint32_t implicit_options,
2303 struct lyd_node **diff)
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002304{
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002305 LY_ERR ret = LY_SUCCESS;
Michal Vasko3488ada2020-12-03 14:18:19 +01002306 struct lyd_node *root, *d = NULL;
Michal Vaskoaac267d2022-01-17 13:34:48 +01002307 struct ly_set node_when = {0};
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002308
2309 LY_CHECK_ARG_RET(NULL, tree, module, LY_EINVAL);
Michal Vasko1d80a532021-11-24 10:41:05 +01002310 LY_CHECK_CTX_EQUAL_RET(*tree ? LYD_CTX(*tree) : NULL, module ? module->ctx : NULL, LY_EINVAL);
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002311 if (diff) {
2312 *diff = NULL;
2313 }
2314
2315 /* add all top-level defaults for this module */
Michal Vaskoaac267d2022-01-17 13:34:48 +01002316 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 +01002317
2318 /* resolve when and remove any invalid defaults */
Michal Vaskoaac267d2022-01-17 13:34:48 +01002319 LY_CHECK_GOTO(ret = lyd_validate_unres(tree, module, &node_when, LYXP_IGNORE_WHEN, NULL, NULL, NULL, 0, diff),
Michal Vasko0110cc92021-12-06 15:42:22 +01002320 cleanup);
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002321
2322 /* process nested nodes */
2323 LY_LIST_FOR(*tree, root) {
2324 /* skip added default nodes */
2325 if ((root->flags & (LYD_DEFAULT | LYD_NEW)) != (LYD_DEFAULT | LYD_NEW)) {
2326 LY_CHECK_GOTO(ret = lyd_new_implicit_tree(root, implicit_options, diff ? &d : NULL), cleanup);
2327
2328 if (d) {
2329 /* merge into one diff */
2330 lyd_insert_sibling(*diff, d, diff);
2331
2332 d = NULL;
2333 }
2334 }
2335 }
2336
2337cleanup:
Michal Vasko3488ada2020-12-03 14:18:19 +01002338 ly_set_erase(&node_when, NULL);
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002339 if (ret && diff) {
2340 lyd_free_all(*diff);
2341 *diff = NULL;
2342 }
2343 return ret;
2344}
2345
Michal Vasko90932a92020-02-12 14:33:03 +01002346struct lyd_node *
Michal Vaskob104f112020-07-17 09:54:54 +02002347lyd_insert_get_next_anchor(const struct lyd_node *first_sibling, const struct lyd_node *new_node)
Michal Vasko90932a92020-02-12 14:33:03 +01002348{
Michal Vaskob104f112020-07-17 09:54:54 +02002349 const struct lysc_node *schema, *sparent;
Michal Vasko90932a92020-02-12 14:33:03 +01002350 struct lyd_node *match = NULL;
Radek Krejci857189e2020-09-01 13:26:36 +02002351 ly_bool found;
Michal Vasko93b16062020-12-09 18:14:18 +01002352 uint32_t getnext_opts;
Michal Vasko90932a92020-02-12 14:33:03 +01002353
Michal Vaskob104f112020-07-17 09:54:54 +02002354 assert(new_node);
2355
2356 if (!first_sibling || !new_node->schema) {
2357 /* insert at the end, no next anchor */
Michal Vasko90932a92020-02-12 14:33:03 +01002358 return NULL;
2359 }
2360
Michal Vasko93b16062020-12-09 18:14:18 +01002361 getnext_opts = 0;
Michal Vaskod1e53b92021-01-28 13:11:06 +01002362 if (new_node->schema->flags & LYS_IS_OUTPUT) {
Michal Vasko93b16062020-12-09 18:14:18 +01002363 getnext_opts = LYS_GETNEXT_OUTPUT;
2364 }
2365
Michal Vaskoa2756f02021-07-09 13:20:28 +02002366 if (first_sibling->parent && first_sibling->parent->schema && first_sibling->parent->children_ht) {
Michal Vaskob104f112020-07-17 09:54:54 +02002367 /* find the anchor using hashes */
2368 sparent = first_sibling->parent->schema;
Michal Vasko93b16062020-12-09 18:14:18 +01002369 schema = lys_getnext(new_node->schema, sparent, NULL, getnext_opts);
Michal Vaskob104f112020-07-17 09:54:54 +02002370 while (schema) {
2371 /* keep trying to find the first existing instance of the closest following schema sibling,
2372 * otherwise return NULL - inserting at the end */
2373 if (!lyd_find_sibling_schema(first_sibling, schema, &match)) {
2374 break;
2375 }
2376
Michal Vasko93b16062020-12-09 18:14:18 +01002377 schema = lys_getnext(schema, sparent, NULL, getnext_opts);
Michal Vaskob104f112020-07-17 09:54:54 +02002378 }
2379 } else {
2380 /* find the anchor without hashes */
2381 match = (struct lyd_node *)first_sibling;
Michal Vasko3a708622021-07-15 14:20:10 +02002382 sparent = lysc_data_parent(new_node->schema);
2383 if (!sparent) {
Michal Vaskob104f112020-07-17 09:54:54 +02002384 /* we are in top-level, skip all the data from preceding modules */
2385 LY_LIST_FOR(match, match) {
2386 if (!match->schema || (strcmp(lyd_owner_module(match)->name, lyd_owner_module(new_node)->name) >= 0)) {
2387 break;
2388 }
2389 }
2390 }
2391
2392 /* get the first schema sibling */
Michal Vasko93b16062020-12-09 18:14:18 +01002393 schema = lys_getnext(NULL, sparent, new_node->schema->module->compiled, getnext_opts);
Michal Vaskob104f112020-07-17 09:54:54 +02002394
2395 found = 0;
2396 LY_LIST_FOR(match, match) {
2397 if (!match->schema || (lyd_owner_module(match) != lyd_owner_module(new_node))) {
2398 /* we have found an opaque node, which must be at the end, so use it OR
2399 * modules do not match, so we must have traversed all the data from new_node module (if any),
2400 * we have found the first node of the next module, that is what we want */
2401 break;
2402 }
2403
2404 /* skip schema nodes until we find the instantiated one */
2405 while (!found) {
2406 if (new_node->schema == schema) {
2407 /* we have found the schema of the new node, continue search to find the first
2408 * data node with a different schema (after our schema) */
2409 found = 1;
2410 break;
2411 }
2412 if (match->schema == schema) {
2413 /* current node (match) is a data node still before the new node, continue search in data */
2414 break;
2415 }
Michal Vasko93b16062020-12-09 18:14:18 +01002416 schema = lys_getnext(schema, sparent, new_node->schema->module->compiled, getnext_opts);
Michal Vaskob104f112020-07-17 09:54:54 +02002417 assert(schema);
2418 }
2419
2420 if (found && (match->schema != new_node->schema)) {
2421 /* find the next node after we have found our node schema data instance */
2422 break;
2423 }
2424 }
Michal Vasko90932a92020-02-12 14:33:03 +01002425 }
2426
2427 return match;
2428}
2429
2430/**
2431 * @brief Insert node after a sibling.
2432 *
Michal Vasko9f96a052020-03-10 09:41:45 +01002433 * Handles inserting into NP containers and key-less lists.
2434 *
Michal Vasko90932a92020-02-12 14:33:03 +01002435 * @param[in] sibling Sibling to insert after.
2436 * @param[in] node Node to insert.
2437 */
2438static void
Michal Vaskof03ed032020-03-04 13:31:44 +01002439lyd_insert_after_node(struct lyd_node *sibling, struct lyd_node *node)
Michal Vasko90932a92020-02-12 14:33:03 +01002440{
Michal Vasko0249f7c2020-03-05 16:36:40 +01002441 struct lyd_node_inner *par;
2442
Michal Vasko90932a92020-02-12 14:33:03 +01002443 assert(!node->next && (node->prev == node));
2444
2445 node->next = sibling->next;
2446 node->prev = sibling;
2447 sibling->next = node;
2448 if (node->next) {
2449 /* sibling had a succeeding node */
2450 node->next->prev = node;
2451 } else {
2452 /* sibling was last, find first sibling and change its prev */
2453 if (sibling->parent) {
2454 sibling = sibling->parent->child;
2455 } else {
Michal Vaskod989ba02020-08-24 10:59:24 +02002456 for ( ; sibling->prev->next != node; sibling = sibling->prev) {}
Michal Vasko90932a92020-02-12 14:33:03 +01002457 }
2458 sibling->prev = node;
2459 }
2460 node->parent = sibling->parent;
Michal Vasko0249f7c2020-03-05 16:36:40 +01002461
Michal Vasko9f96a052020-03-10 09:41:45 +01002462 for (par = node->parent; par; par = par->parent) {
2463 if ((par->flags & LYD_DEFAULT) && !(node->flags & LYD_DEFAULT)) {
2464 /* remove default flags from NP containers */
Michal Vasko0249f7c2020-03-05 16:36:40 +01002465 par->flags &= ~LYD_DEFAULT;
2466 }
2467 }
Michal Vasko90932a92020-02-12 14:33:03 +01002468}
2469
2470/**
2471 * @brief Insert node before a sibling.
2472 *
Michal Vasko9f96a052020-03-10 09:41:45 +01002473 * Handles inserting into NP containers and key-less lists.
2474 *
Michal Vasko90932a92020-02-12 14:33:03 +01002475 * @param[in] sibling Sibling to insert before.
2476 * @param[in] node Node to insert.
2477 */
2478static void
Michal Vaskof03ed032020-03-04 13:31:44 +01002479lyd_insert_before_node(struct lyd_node *sibling, struct lyd_node *node)
Michal Vasko90932a92020-02-12 14:33:03 +01002480{
Michal Vasko0249f7c2020-03-05 16:36:40 +01002481 struct lyd_node_inner *par;
2482
Michal Vasko90932a92020-02-12 14:33:03 +01002483 assert(!node->next && (node->prev == node));
2484
2485 node->next = sibling;
2486 /* covers situation of sibling being first */
2487 node->prev = sibling->prev;
2488 sibling->prev = node;
2489 if (node->prev->next) {
2490 /* sibling had a preceding node */
2491 node->prev->next = node;
2492 } else if (sibling->parent) {
2493 /* sibling was first and we must also change parent child pointer */
2494 sibling->parent->child = node;
2495 }
2496 node->parent = sibling->parent;
Michal Vasko0249f7c2020-03-05 16:36:40 +01002497
Michal Vasko9f96a052020-03-10 09:41:45 +01002498 for (par = node->parent; par; par = par->parent) {
2499 if ((par->flags & LYD_DEFAULT) && !(node->flags & LYD_DEFAULT)) {
2500 /* remove default flags from NP containers */
Michal Vasko0249f7c2020-03-05 16:36:40 +01002501 par->flags &= ~LYD_DEFAULT;
2502 }
2503 }
Michal Vasko90932a92020-02-12 14:33:03 +01002504}
2505
2506/**
Michal Vaskob104f112020-07-17 09:54:54 +02002507 * @brief Insert node as the first and only child of a parent.
Michal Vasko90932a92020-02-12 14:33:03 +01002508 *
Michal Vasko9f96a052020-03-10 09:41:45 +01002509 * Handles inserting into NP containers and key-less lists.
2510 *
Michal Vasko90932a92020-02-12 14:33:03 +01002511 * @param[in] parent Parent to insert into.
2512 * @param[in] node Node to insert.
2513 */
2514static void
Michal Vaskob104f112020-07-17 09:54:54 +02002515lyd_insert_only_child(struct lyd_node *parent, struct lyd_node *node)
Michal Vasko90932a92020-02-12 14:33:03 +01002516{
2517 struct lyd_node_inner *par;
2518
Radek Krejcia1c1e542020-09-29 16:06:52 +02002519 assert(parent && !lyd_child(parent) && !node->next && (node->prev == node));
Michal Vasko52927e22020-03-16 17:26:14 +01002520 assert(!parent->schema || (parent->schema->nodetype & LYD_NODE_INNER));
Michal Vasko90932a92020-02-12 14:33:03 +01002521
2522 par = (struct lyd_node_inner *)parent;
2523
Michal Vaskob104f112020-07-17 09:54:54 +02002524 par->child = node;
Michal Vasko90932a92020-02-12 14:33:03 +01002525 node->parent = par;
Michal Vasko0249f7c2020-03-05 16:36:40 +01002526
Michal Vaskod989ba02020-08-24 10:59:24 +02002527 for ( ; par; par = par->parent) {
Michal Vasko9f96a052020-03-10 09:41:45 +01002528 if ((par->flags & LYD_DEFAULT) && !(node->flags & LYD_DEFAULT)) {
2529 /* remove default flags from NP containers */
Michal Vasko0249f7c2020-03-05 16:36:40 +01002530 par->flags &= ~LYD_DEFAULT;
2531 }
2532 }
Michal Vasko751cb4d2020-07-14 12:25:28 +02002533}
Michal Vasko0249f7c2020-03-05 16:36:40 +01002534
Michal Vasko751cb4d2020-07-14 12:25:28 +02002535/**
2536 * @brief Learn whether a list instance has all the keys.
2537 *
2538 * @param[in] list List instance to check.
2539 * @return non-zero if all the keys were found,
2540 * @return 0 otherwise.
2541 */
2542static int
2543lyd_insert_has_keys(const struct lyd_node *list)
2544{
2545 const struct lyd_node *key;
2546 const struct lysc_node *skey = NULL;
2547
2548 assert(list->schema->nodetype == LYS_LIST);
Radek Krejcia1c1e542020-09-29 16:06:52 +02002549 key = lyd_child(list);
Michal Vasko751cb4d2020-07-14 12:25:28 +02002550 while ((skey = lys_getnext(skey, list->schema, NULL, 0)) && (skey->flags & LYS_KEY)) {
2551 if (!key || (key->schema != skey)) {
2552 /* key missing */
2553 return 0;
2554 }
2555
2556 key = key->next;
2557 }
2558
2559 /* all keys found */
2560 return 1;
Michal Vasko90932a92020-02-12 14:33:03 +01002561}
2562
2563void
Michal Vasko6ee6f432021-07-16 09:49:14 +02002564lyd_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 +01002565{
Michal Vaskob104f112020-07-17 09:54:54 +02002566 struct lyd_node *anchor, *first_sibling;
Michal Vasko90932a92020-02-12 14:33:03 +01002567
Michal Vaskob104f112020-07-17 09:54:54 +02002568 /* inserting list without its keys is not supported */
2569 assert((parent || first_sibling_p) && node && (node->hash || !node->schema));
Michal Vaskof756c942020-11-06 17:21:37 +01002570 assert(!parent || !parent->schema ||
2571 (parent->schema->nodetype & (LYS_CONTAINER | LYS_LIST | LYS_RPC | LYS_ACTION | LYS_NOTIF)));
Michal Vasko9b368d32020-02-14 13:53:31 +01002572
Michal Vaskob104f112020-07-17 09:54:54 +02002573 if (!parent && first_sibling_p && (*first_sibling_p) && (*first_sibling_p)->parent) {
Michal Vasko9e685082021-01-29 14:49:09 +01002574 parent = lyd_parent(*first_sibling_p);
Michal Vasko9b368d32020-02-14 13:53:31 +01002575 }
Michal Vasko90932a92020-02-12 14:33:03 +01002576
Michal Vaskob104f112020-07-17 09:54:54 +02002577 /* get first sibling */
Michal Vasko9e685082021-01-29 14:49:09 +01002578 first_sibling = parent ? lyd_child(parent) : *first_sibling_p;
Michal Vasko9f96a052020-03-10 09:41:45 +01002579
Michal Vasko6ee6f432021-07-16 09:49:14 +02002580 if (last) {
2581 /* no next anchor */
2582 anchor = NULL;
2583 } else {
2584 /* find the anchor, our next node, so we can insert before it */
2585 anchor = lyd_insert_get_next_anchor(first_sibling, node);
2586 }
2587
Michal Vaskob104f112020-07-17 09:54:54 +02002588 if (anchor) {
Michal Vasko6ee6f432021-07-16 09:49:14 +02002589 /* insert before the anchor */
Michal Vaskob104f112020-07-17 09:54:54 +02002590 lyd_insert_before_node(anchor, node);
Michal Vasko26123192020-11-09 21:02:34 +01002591 if (!parent && (*first_sibling_p == anchor)) {
2592 /* move first sibling */
2593 *first_sibling_p = node;
2594 }
Michal Vaskob104f112020-07-17 09:54:54 +02002595 } else if (first_sibling) {
Michal Vasko6ee6f432021-07-16 09:49:14 +02002596 /* insert as the last node */
Michal Vaskob104f112020-07-17 09:54:54 +02002597 lyd_insert_after_node(first_sibling->prev, node);
2598 } else if (parent) {
Michal Vasko6ee6f432021-07-16 09:49:14 +02002599 /* insert as the only child */
Michal Vaskob104f112020-07-17 09:54:54 +02002600 lyd_insert_only_child(parent, node);
Michal Vasko90932a92020-02-12 14:33:03 +01002601 } else {
Michal Vasko6ee6f432021-07-16 09:49:14 +02002602 /* insert as the only sibling */
Michal Vaskob104f112020-07-17 09:54:54 +02002603 *first_sibling_p = node;
2604 }
2605
2606 /* insert into parent HT */
2607 lyd_insert_hash(node);
2608
2609 /* finish hashes for our parent, if needed and possible */
Michal Vasko9e8de2d2020-09-01 08:17:10 +02002610 if (node->schema && (node->schema->flags & LYS_KEY) && parent && lyd_insert_has_keys(parent)) {
Michal Vaskob104f112020-07-17 09:54:54 +02002611 lyd_hash(parent);
2612
2613 /* now we can insert even the list into its parent HT */
2614 lyd_insert_hash(parent);
Michal Vasko90932a92020-02-12 14:33:03 +01002615 }
Michal Vasko90932a92020-02-12 14:33:03 +01002616}
2617
Michal Vasko717a4c32020-12-07 09:40:10 +01002618/**
2619 * @brief Check schema place of a node to be inserted.
2620 *
2621 * @param[in] parent Schema node of the parent data node.
2622 * @param[in] sibling Schema node of a sibling data node.
2623 * @param[in] schema Schema node if the data node to be inserted.
2624 * @return LY_SUCCESS on success.
2625 * @return LY_EINVAL if the place is invalid.
2626 */
Michal Vaskof03ed032020-03-04 13:31:44 +01002627static LY_ERR
Michal Vasko717a4c32020-12-07 09:40:10 +01002628lyd_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 +01002629{
2630 const struct lysc_node *par2;
2631
Michal Vasko62ed12d2020-05-21 10:08:25 +02002632 assert(!parent || !(parent->nodetype & (LYS_CASE | LYS_CHOICE)));
Michal Vasko717a4c32020-12-07 09:40:10 +01002633 assert(!sibling || !(sibling->nodetype & (LYS_CASE | LYS_CHOICE)));
2634 assert(!schema || !(schema->nodetype & (LYS_CASE | LYS_CHOICE)));
Michal Vaskof03ed032020-03-04 13:31:44 +01002635
Michal Vasko717a4c32020-12-07 09:40:10 +01002636 if (!schema || (!parent && !sibling)) {
Michal Vasko71e795e2020-12-04 16:27:37 +01002637 /* opaque nodes can be inserted wherever */
2638 return LY_SUCCESS;
2639 }
2640
Michal Vasko717a4c32020-12-07 09:40:10 +01002641 if (!parent) {
2642 parent = lysc_data_parent(sibling);
2643 }
2644
Michal Vaskof03ed032020-03-04 13:31:44 +01002645 /* find schema parent */
Michal Vasko62ed12d2020-05-21 10:08:25 +02002646 par2 = lysc_data_parent(schema);
Michal Vaskof03ed032020-03-04 13:31:44 +01002647
2648 if (parent) {
2649 /* inner node */
2650 if (par2 != parent) {
Michal Vaskob104f112020-07-17 09:54:54 +02002651 LOGERR(schema->module->ctx, LY_EINVAL, "Cannot insert, parent of \"%s\" is not \"%s\".", schema->name,
Michal Vasko69730152020-10-09 16:30:07 +02002652 parent->name);
Michal Vaskof03ed032020-03-04 13:31:44 +01002653 return LY_EINVAL;
2654 }
2655 } else {
2656 /* top-level node */
2657 if (par2) {
Radek Krejcif6d14cb2020-07-02 16:11:45 +02002658 LOGERR(schema->module->ctx, LY_EINVAL, "Cannot insert, node \"%s\" is not top-level.", schema->name);
Michal Vaskof03ed032020-03-04 13:31:44 +01002659 return LY_EINVAL;
2660 }
2661 }
2662
2663 return LY_SUCCESS;
2664}
2665
Jan Kundrátc6e39de2021-12-09 16:01:19 +01002666LIBYANG_API_DEF LY_ERR
Michal Vaskob104f112020-07-17 09:54:54 +02002667lyd_insert_child(struct lyd_node *parent, struct lyd_node *node)
Michal Vaskof03ed032020-03-04 13:31:44 +01002668{
2669 struct lyd_node *iter;
2670
Michal Vasko0ab974d2021-02-24 13:18:26 +01002671 LY_CHECK_ARG_RET(NULL, parent, node, !parent->schema || (parent->schema->nodetype & LYD_NODE_INNER), LY_EINVAL);
Michal Vasko1d80a532021-11-24 10:41:05 +01002672 LY_CHECK_CTX_EQUAL_RET(LYD_CTX(parent), LYD_CTX(node), LY_EINVAL);
Michal Vaskof03ed032020-03-04 13:31:44 +01002673
Michal Vasko717a4c32020-12-07 09:40:10 +01002674 LY_CHECK_RET(lyd_insert_check_schema(parent->schema, NULL, node->schema));
Michal Vaskof03ed032020-03-04 13:31:44 +01002675
Michal Vasko0ab974d2021-02-24 13:18:26 +01002676 if (node->schema && (node->schema->flags & LYS_KEY)) {
Michal Vaskoaac267d2022-01-17 13:34:48 +01002677 LOGERR(LYD_CTX(parent), LY_EINVAL, "Cannot insert key \"%s\".", node->schema->name);
Michal Vaskof03ed032020-03-04 13:31:44 +01002678 return LY_EINVAL;
2679 }
2680
2681 if (node->parent || node->prev->next) {
2682 lyd_unlink_tree(node);
2683 }
2684
2685 while (node) {
2686 iter = node->next;
2687 lyd_unlink_tree(node);
Michal Vasko6ee6f432021-07-16 09:49:14 +02002688 lyd_insert_node(parent, NULL, node, 0);
Michal Vaskof03ed032020-03-04 13:31:44 +01002689 node = iter;
2690 }
2691 return LY_SUCCESS;
2692}
2693
Jan Kundrátc6e39de2021-12-09 16:01:19 +01002694LIBYANG_API_DEF LY_ERR
Michal Vaskoaac267d2022-01-17 13:34:48 +01002695lyd_insert_ext(struct lyd_node *parent, struct lyd_node *first)
2696{
2697 struct lyd_node *iter;
2698
2699 LY_CHECK_ARG_RET(NULL, parent, first, !first->parent, !first->prev->next,
2700 !parent->schema || (parent->schema->nodetype & LYD_NODE_INNER), LY_EINVAL);
2701
2702 if (first->schema && (first->schema->flags & LYS_KEY)) {
2703 LOGERR(LYD_CTX(parent), LY_EINVAL, "Cannot insert key \"%s\".", first->schema->name);
2704 return LY_EINVAL;
2705 }
2706
2707 while (first) {
2708 iter = first->next;
2709 lyd_unlink_tree(first);
2710 lyd_insert_node(parent, NULL, first, 0);
2711 first = iter;
2712 }
2713 return LY_SUCCESS;
2714}
2715
2716LIBYANG_API_DEF LY_ERR
Michal Vaskob104f112020-07-17 09:54:54 +02002717lyd_insert_sibling(struct lyd_node *sibling, struct lyd_node *node, struct lyd_node **first)
Michal Vaskob1b5c262020-03-05 14:29:47 +01002718{
2719 struct lyd_node *iter;
2720
Michal Vaskob104f112020-07-17 09:54:54 +02002721 LY_CHECK_ARG_RET(NULL, node, LY_EINVAL);
Michal Vasko1d80a532021-11-24 10:41:05 +01002722 LY_CHECK_CTX_EQUAL_RET(sibling ? LYD_CTX(sibling) : NULL, LYD_CTX(node), LY_EINVAL);
Michal Vaskob1b5c262020-03-05 14:29:47 +01002723
Michal Vaskob104f112020-07-17 09:54:54 +02002724 if (sibling) {
Michal Vasko717a4c32020-12-07 09:40:10 +01002725 LY_CHECK_RET(lyd_insert_check_schema(NULL, sibling->schema, node->schema));
Michal Vaskob1b5c262020-03-05 14:29:47 +01002726 }
2727
Michal Vasko553d0b52020-12-04 16:27:52 +01002728 if (sibling == node) {
2729 /* we need to keep the connection to siblings so we can insert into them */
2730 sibling = sibling->prev;
2731 }
2732
Michal Vaskob1b5c262020-03-05 14:29:47 +01002733 if (node->parent || node->prev->next) {
2734 lyd_unlink_tree(node);
2735 }
2736
2737 while (node) {
Michal Vasko71e795e2020-12-04 16:27:37 +01002738 if (lysc_is_key(node->schema)) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02002739 LOGERR(LYD_CTX(node), LY_EINVAL, "Cannot insert key \"%s\".", node->schema->name);
Michal Vaskob104f112020-07-17 09:54:54 +02002740 return LY_EINVAL;
2741 }
2742
Michal Vaskob1b5c262020-03-05 14:29:47 +01002743 iter = node->next;
2744 lyd_unlink_tree(node);
Michal Vasko6ee6f432021-07-16 09:49:14 +02002745 lyd_insert_node(NULL, &sibling, node, 0);
Michal Vaskob1b5c262020-03-05 14:29:47 +01002746 node = iter;
2747 }
Michal Vaskob1b5c262020-03-05 14:29:47 +01002748
Michal Vaskob104f112020-07-17 09:54:54 +02002749 if (first) {
2750 /* find the first sibling */
2751 *first = sibling;
2752 while ((*first)->prev->next) {
2753 *first = (*first)->prev;
Michal Vasko0249f7c2020-03-05 16:36:40 +01002754 }
2755 }
2756
2757 return LY_SUCCESS;
2758}
2759
Jan Kundrátc6e39de2021-12-09 16:01:19 +01002760LIBYANG_API_DEF LY_ERR
Michal Vaskof03ed032020-03-04 13:31:44 +01002761lyd_insert_before(struct lyd_node *sibling, struct lyd_node *node)
2762{
Michal Vaskof03ed032020-03-04 13:31:44 +01002763 LY_CHECK_ARG_RET(NULL, sibling, node, LY_EINVAL);
Michal Vasko1d80a532021-11-24 10:41:05 +01002764 LY_CHECK_CTX_EQUAL_RET(LYD_CTX(sibling), LYD_CTX(node), LY_EINVAL);
Michal Vaskof03ed032020-03-04 13:31:44 +01002765
Michal Vasko717a4c32020-12-07 09:40:10 +01002766 LY_CHECK_RET(lyd_insert_check_schema(NULL, sibling->schema, node->schema));
Michal Vaskof03ed032020-03-04 13:31:44 +01002767
Michal Vaskob104f112020-07-17 09:54:54 +02002768 if (!(node->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) || !(node->schema->flags & LYS_ORDBY_USER)) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02002769 LOGERR(LYD_CTX(sibling), LY_EINVAL, "Can be used only for user-ordered nodes.");
Michal Vaskof03ed032020-03-04 13:31:44 +01002770 return LY_EINVAL;
2771 }
2772
Michal Vasko4bc2ce32020-12-08 10:06:16 +01002773 lyd_unlink_tree(node);
2774 lyd_insert_before_node(sibling, node);
2775 lyd_insert_hash(node);
Michal Vaskof03ed032020-03-04 13:31:44 +01002776
Michal Vaskof03ed032020-03-04 13:31:44 +01002777 return LY_SUCCESS;
2778}
2779
Jan Kundrátc6e39de2021-12-09 16:01:19 +01002780LIBYANG_API_DEF LY_ERR
Michal Vaskof03ed032020-03-04 13:31:44 +01002781lyd_insert_after(struct lyd_node *sibling, struct lyd_node *node)
2782{
Michal Vaskof03ed032020-03-04 13:31:44 +01002783 LY_CHECK_ARG_RET(NULL, sibling, node, LY_EINVAL);
Michal Vasko1d80a532021-11-24 10:41:05 +01002784 LY_CHECK_CTX_EQUAL_RET(LYD_CTX(sibling), LYD_CTX(node), LY_EINVAL);
Michal Vaskof03ed032020-03-04 13:31:44 +01002785
Michal Vasko717a4c32020-12-07 09:40:10 +01002786 LY_CHECK_RET(lyd_insert_check_schema(NULL, sibling->schema, node->schema));
Michal Vaskof03ed032020-03-04 13:31:44 +01002787
Michal Vaskob104f112020-07-17 09:54:54 +02002788 if (!(node->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) || !(node->schema->flags & LYS_ORDBY_USER)) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02002789 LOGERR(LYD_CTX(sibling), LY_EINVAL, "Can be used only for user-ordered nodes.");
Michal Vaskof03ed032020-03-04 13:31:44 +01002790 return LY_EINVAL;
2791 }
2792
Michal Vasko4bc2ce32020-12-08 10:06:16 +01002793 lyd_unlink_tree(node);
2794 lyd_insert_after_node(sibling, node);
2795 lyd_insert_hash(node);
Michal Vaskof03ed032020-03-04 13:31:44 +01002796
Michal Vaskof03ed032020-03-04 13:31:44 +01002797 return LY_SUCCESS;
2798}
2799
Jan Kundrátc6e39de2021-12-09 16:01:19 +01002800LIBYANG_API_DEF void
Michal Vasko66d508c2021-07-21 16:07:09 +02002801lyd_unlink_siblings(struct lyd_node *node)
2802{
2803 struct lyd_node *next, *elem, *first = NULL;
2804
2805 LY_LIST_FOR_SAFE(node, next, elem) {
2806 lyd_unlink_tree(elem);
2807 lyd_insert_node(NULL, &first, elem, 1);
2808 }
2809}
2810
Jan Kundrátc6e39de2021-12-09 16:01:19 +01002811LIBYANG_API_DEF void
Michal Vaskof03ed032020-03-04 13:31:44 +01002812lyd_unlink_tree(struct lyd_node *node)
2813{
2814 struct lyd_node *iter;
2815
2816 if (!node) {
2817 return;
2818 }
2819
Michal Vaskob104f112020-07-17 09:54:54 +02002820 /* update hashes while still linked into the tree */
2821 lyd_unlink_hash(node);
2822
Michal Vaskof03ed032020-03-04 13:31:44 +01002823 /* unlink from siblings */
2824 if (node->prev->next) {
2825 node->prev->next = node->next;
2826 }
2827 if (node->next) {
2828 node->next->prev = node->prev;
2829 } else {
2830 /* unlinking the last node */
2831 if (node->parent) {
2832 iter = node->parent->child;
2833 } else {
2834 iter = node->prev;
2835 while (iter->prev != node) {
2836 iter = iter->prev;
2837 }
2838 }
2839 /* update the "last" pointer from the first node */
2840 iter->prev = node->prev;
2841 }
2842
2843 /* unlink from parent */
2844 if (node->parent) {
2845 if (node->parent->child == node) {
2846 /* the node is the first child */
2847 node->parent->child = node->next;
2848 }
2849
Michal Vaskoab49dbe2020-07-17 12:32:47 +02002850 /* check for NP container whether its last non-default node is not being unlinked */
Michal Vasko69730152020-10-09 16:30:07 +02002851 if (node->parent->schema && (node->parent->schema->nodetype == LYS_CONTAINER) &&
2852 !(node->parent->flags & LYD_DEFAULT) && !(node->parent->schema->flags & LYS_PRESENCE)) {
Michal Vaskoab49dbe2020-07-17 12:32:47 +02002853 LY_LIST_FOR(node->parent->child, iter) {
2854 if ((iter != node) && !(iter->flags & LYD_DEFAULT)) {
2855 break;
2856 }
2857 }
2858 if (!iter) {
2859 node->parent->flags |= LYD_DEFAULT;
2860 }
2861 }
2862
Michal Vaskof03ed032020-03-04 13:31:44 +01002863 node->parent = NULL;
2864 }
2865
2866 node->next = NULL;
2867 node->prev = node;
2868}
2869
Michal Vaskoa5da3292020-08-12 13:10:50 +02002870void
Michal Vasko871a0252020-11-11 18:35:24 +01002871lyd_insert_meta(struct lyd_node *parent, struct lyd_meta *meta, ly_bool clear_dflt)
Radek Krejci1798aae2020-07-14 13:26:06 +02002872{
2873 struct lyd_meta *last, *iter;
2874
2875 assert(parent);
Michal Vaskoa5da3292020-08-12 13:10:50 +02002876
2877 if (!meta) {
2878 return;
2879 }
Radek Krejci1798aae2020-07-14 13:26:06 +02002880
2881 for (iter = meta; iter; iter = iter->next) {
2882 iter->parent = parent;
2883 }
2884
2885 /* insert as the last attribute */
2886 if (parent->meta) {
Radek Krejci1e008d22020-08-17 11:37:37 +02002887 for (last = parent->meta; last->next; last = last->next) {}
Radek Krejci1798aae2020-07-14 13:26:06 +02002888 last->next = meta;
2889 } else {
2890 parent->meta = meta;
2891 }
2892
2893 /* remove default flags from NP containers */
Michal Vasko871a0252020-11-11 18:35:24 +01002894 while (clear_dflt && parent && (parent->schema->nodetype == LYS_CONTAINER) && (parent->flags & LYD_DEFAULT)) {
Radek Krejci1798aae2020-07-14 13:26:06 +02002895 parent->flags &= ~LYD_DEFAULT;
Michal Vasko9e685082021-01-29 14:49:09 +01002896 parent = lyd_parent(parent);
Radek Krejci1798aae2020-07-14 13:26:06 +02002897 }
Radek Krejci1798aae2020-07-14 13:26:06 +02002898}
2899
2900LY_ERR
Michal Vasko9f96a052020-03-10 09:41:45 +01002901lyd_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 +02002902 size_t name_len, const char *value, size_t value_len, ly_bool *dynamic, LY_VALUE_FORMAT format,
Michal Vaskoaac267d2022-01-17 13:34:48 +01002903 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 +01002904{
Radek Krejci2efc45b2020-12-22 16:25:44 +01002905 LY_ERR ret = LY_SUCCESS;
Michal Vasko90932a92020-02-12 14:33:03 +01002906 struct lysc_ext_instance *ant = NULL;
Michal Vaskob0534f92021-07-01 13:24:03 +02002907 const struct lysc_type **ant_type;
Michal Vasko9f96a052020-03-10 09:41:45 +01002908 struct lyd_meta *mt, *last;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002909 LY_ARRAY_COUNT_TYPE u;
Michal Vasko90932a92020-02-12 14:33:03 +01002910
Michal Vasko9f96a052020-03-10 09:41:45 +01002911 assert((parent || meta) && mod);
Michal Vasko6f4cbb62020-02-28 11:15:47 +01002912
Michal Vaskoaac267d2022-01-17 13:34:48 +01002913 LOG_LOCSET(ctx_node, parent, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +01002914
Radek Krejci7eb54ba2020-05-18 16:30:04 +02002915 LY_ARRAY_FOR(mod->compiled->exts, u) {
Radek Krejci3e6632f2021-03-22 22:08:21 +01002916 if ((mod->compiled->exts[u].def->plugin == lyplg_find(LYPLG_EXTENSION, LYEXT_PLUGIN_INTERNAL_ANNOTATION)) &&
Radek Krejci7eb54ba2020-05-18 16:30:04 +02002917 !ly_strncmp(mod->compiled->exts[u].argument, name, name_len)) {
Michal Vasko90932a92020-02-12 14:33:03 +01002918 /* we have the annotation definition */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02002919 ant = &mod->compiled->exts[u];
Michal Vasko90932a92020-02-12 14:33:03 +01002920 break;
2921 }
2922 }
2923 if (!ant) {
2924 /* attribute is not defined as a metadata annotation (RFC 7952) */
Radek Krejci2efc45b2020-12-22 16:25:44 +01002925 LOGVAL(mod->ctx, LYVE_REFERENCE, "Annotation definition for attribute \"%s:%.*s\" not found.",
Radek Krejci422afb12021-03-04 16:38:16 +01002926 mod->name, (int)name_len, name);
Radek Krejci2efc45b2020-12-22 16:25:44 +01002927 ret = LY_EINVAL;
2928 goto cleanup;
Michal Vasko90932a92020-02-12 14:33:03 +01002929 }
2930
Michal Vasko9f96a052020-03-10 09:41:45 +01002931 mt = calloc(1, sizeof *mt);
Radek Krejci2efc45b2020-12-22 16:25:44 +01002932 LY_CHECK_ERR_GOTO(!mt, LOGMEM(mod->ctx); ret = LY_EMEM, cleanup);
Michal Vasko9f96a052020-03-10 09:41:45 +01002933 mt->parent = parent;
2934 mt->annotation = ant;
Michal Vaskob0534f92021-07-01 13:24:03 +02002935 ant_type = ant->substmts[ANNOTATION_SUBSTMT_TYPE].storage;
2936 ret = lyd_value_store(mod->ctx, &mt->value, *ant_type, value, value_len, dynamic, format, prefix_data, hints,
Michal Vaskoaac267d2022-01-17 13:34:48 +01002937 ctx_node, incomplete);
Radek Krejci2efc45b2020-12-22 16:25:44 +01002938 LY_CHECK_ERR_GOTO(ret, free(mt), cleanup);
2939 ret = lydict_insert(mod->ctx, name, name_len, &mt->name);
2940 LY_CHECK_ERR_GOTO(ret, free(mt), cleanup);
Michal Vasko90932a92020-02-12 14:33:03 +01002941
Michal Vasko6f4cbb62020-02-28 11:15:47 +01002942 /* insert as the last attribute */
2943 if (parent) {
Michal Vasko871a0252020-11-11 18:35:24 +01002944 lyd_insert_meta(parent, mt, clear_dflt);
Michal Vasko9f96a052020-03-10 09:41:45 +01002945 } else if (*meta) {
Radek Krejci1e008d22020-08-17 11:37:37 +02002946 for (last = *meta; last->next; last = last->next) {}
Michal Vasko9f96a052020-03-10 09:41:45 +01002947 last->next = mt;
Michal Vasko90932a92020-02-12 14:33:03 +01002948 }
2949
Michal Vasko9f96a052020-03-10 09:41:45 +01002950 if (meta) {
2951 *meta = mt;
Michal Vasko90932a92020-02-12 14:33:03 +01002952 }
Radek Krejci2efc45b2020-12-22 16:25:44 +01002953
2954cleanup:
Michal Vaskoaac267d2022-01-17 13:34:48 +01002955 LOG_LOCBACK(ctx_node ? 1 : 0, parent ? 1 : 0, 0, 0);
Radek Krejci2efc45b2020-12-22 16:25:44 +01002956 return ret;
Michal Vasko90932a92020-02-12 14:33:03 +01002957}
2958
Michal Vaskoa5da3292020-08-12 13:10:50 +02002959void
2960lyd_insert_attr(struct lyd_node *parent, struct lyd_attr *attr)
2961{
2962 struct lyd_attr *last, *iter;
2963 struct lyd_node_opaq *opaq;
2964
2965 assert(parent && !parent->schema);
2966
2967 if (!attr) {
2968 return;
2969 }
2970
2971 opaq = (struct lyd_node_opaq *)parent;
2972 for (iter = attr; iter; iter = iter->next) {
2973 iter->parent = opaq;
2974 }
2975
2976 /* insert as the last attribute */
2977 if (opaq->attr) {
Radek Krejci1e008d22020-08-17 11:37:37 +02002978 for (last = opaq->attr; last->next; last = last->next) {}
Michal Vaskoa5da3292020-08-12 13:10:50 +02002979 last->next = attr;
2980 } else {
2981 opaq->attr = attr;
2982 }
2983}
2984
Michal Vasko52927e22020-03-16 17:26:14 +01002985LY_ERR
Michal Vaskofeca4fb2020-10-05 08:58:40 +02002986lyd_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 +01002987 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 +02002988 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 +01002989{
Radek Krejci011e4aa2020-09-04 15:22:31 +02002990 LY_ERR ret = LY_SUCCESS;
Radek Krejci1798aae2020-07-14 13:26:06 +02002991 struct lyd_attr *at, *last;
Michal Vasko52927e22020-03-16 17:26:14 +01002992
2993 assert(ctx && (parent || attr) && (!parent || !parent->schema));
Michal Vaskofeca4fb2020-10-05 08:58:40 +02002994 assert(name && name_len && format);
Michal Vasko52927e22020-03-16 17:26:14 +01002995
Michal Vasko2a3722d2021-06-16 11:52:39 +02002996 if (!value_len && (!dynamic || !*dynamic)) {
Michal Vasko52927e22020-03-16 17:26:14 +01002997 value = "";
2998 }
2999
3000 at = calloc(1, sizeof *at);
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01003001 LY_CHECK_ERR_RET(!at, LOGMEM(ctx); ly_free_prefix_data(format, val_prefix_data), LY_EMEM);
Radek Krejcid46e46a2020-09-15 14:22:42 +02003002
Michal Vaskoad92b672020-11-12 13:11:31 +01003003 LY_CHECK_GOTO(ret = lydict_insert(ctx, name, name_len, &at->name.name), finish);
Michal Vasko501af032020-11-11 20:27:44 +01003004 if (prefix_len) {
Michal Vaskoad92b672020-11-12 13:11:31 +01003005 LY_CHECK_GOTO(ret = lydict_insert(ctx, prefix, prefix_len, &at->name.prefix), finish);
Michal Vasko501af032020-11-11 20:27:44 +01003006 }
3007 if (module_key_len) {
Michal Vaskoad92b672020-11-12 13:11:31 +01003008 LY_CHECK_GOTO(ret = lydict_insert(ctx, module_key, module_key_len, &at->name.module_ns), finish);
Michal Vasko501af032020-11-11 20:27:44 +01003009 }
3010
Michal Vasko52927e22020-03-16 17:26:14 +01003011 if (dynamic && *dynamic) {
Radek Krejci011e4aa2020-09-04 15:22:31 +02003012 ret = lydict_insert_zc(ctx, (char *)value, &at->value);
3013 LY_CHECK_GOTO(ret, finish);
Michal Vasko52927e22020-03-16 17:26:14 +01003014 *dynamic = 0;
3015 } else {
Radek Krejci011e4aa2020-09-04 15:22:31 +02003016 LY_CHECK_GOTO(ret = lydict_insert(ctx, value, value_len, &at->value), finish);
Michal Vasko52927e22020-03-16 17:26:14 +01003017 }
Michal Vasko501af032020-11-11 20:27:44 +01003018 at->format = format;
3019 at->val_prefix_data = val_prefix_data;
3020 at->hints = hints;
Michal Vasko52927e22020-03-16 17:26:14 +01003021
3022 /* insert as the last attribute */
3023 if (parent) {
Michal Vaskoa5da3292020-08-12 13:10:50 +02003024 lyd_insert_attr(parent, at);
Michal Vasko52927e22020-03-16 17:26:14 +01003025 } else if (*attr) {
Radek Krejci1e008d22020-08-17 11:37:37 +02003026 for (last = *attr; last->next; last = last->next) {}
Michal Vasko52927e22020-03-16 17:26:14 +01003027 last->next = at;
3028 }
3029
Radek Krejci011e4aa2020-09-04 15:22:31 +02003030finish:
3031 if (ret) {
3032 lyd_free_attr_single(ctx, at);
3033 } else if (attr) {
Michal Vasko52927e22020-03-16 17:26:14 +01003034 *attr = at;
3035 }
3036 return LY_SUCCESS;
3037}
3038
Jan Kundrátc6e39de2021-12-09 16:01:19 +01003039LIBYANG_API_DEF const struct lyd_node_term *
Michal Vasko004d3152020-06-11 19:59:22 +02003040lyd_target(const struct ly_path *path, const struct lyd_node *tree)
Radek Krejci084289f2019-07-09 17:35:30 +02003041{
Michal Vasko004d3152020-06-11 19:59:22 +02003042 struct lyd_node *target;
Radek Krejci084289f2019-07-09 17:35:30 +02003043
Michal Vasko004d3152020-06-11 19:59:22 +02003044 if (ly_path_eval(path, tree, &target)) {
3045 return NULL;
Radek Krejci084289f2019-07-09 17:35:30 +02003046 }
3047
Michal Vasko004d3152020-06-11 19:59:22 +02003048 return (struct lyd_node_term *)target;
Radek Krejci084289f2019-07-09 17:35:30 +02003049}
3050
aPiecek2f63f952021-03-30 12:22:18 +02003051/**
3052 * @brief Check the equality of the two schemas from different contexts.
3053 *
3054 * @param schema1 of first node.
3055 * @param schema2 of second node.
3056 * @return 1 if the schemas are equal otherwise 0.
3057 */
3058static ly_bool
3059lyd_compare_schema_equal(const struct lysc_node *schema1, const struct lysc_node *schema2)
3060{
3061 if (!schema1 && !schema2) {
3062 return 1;
3063 } else if (!schema1 || !schema2) {
3064 return 0;
3065 }
3066
3067 assert(schema1->module->ctx != schema2->module->ctx);
3068
3069 if (schema1->nodetype != schema2->nodetype) {
3070 return 0;
3071 }
3072
3073 if (strcmp(schema1->name, schema2->name)) {
3074 return 0;
3075 }
3076
3077 if (strcmp(schema1->module->name, schema2->module->name)) {
3078 return 0;
3079 }
3080
3081 if (schema1->module->revision || schema2->module->revision) {
3082 if (!schema1->module->revision || !schema2->module->revision) {
3083 return 0;
3084 }
3085 if (strcmp(schema1->module->revision, schema2->module->revision)) {
3086 return 0;
3087 }
3088 }
3089
3090 return 1;
3091}
3092
3093/**
3094 * @brief Check the equality of the schemas for all parent nodes.
3095 *
3096 * Both nodes must be from different contexts.
3097 *
3098 * @param node1 Data of first node.
3099 * @param node2 Data of second node.
3100 * @return 1 if the all related parental schemas are equal otherwise 0.
3101 */
3102static ly_bool
3103lyd_compare_schema_parents_equal(const struct lyd_node *node1, const struct lyd_node *node2)
3104{
3105 const struct lysc_node *parent1, *parent2;
3106
3107 assert(node1 && node2);
3108
3109 for (parent1 = node1->schema->parent, parent2 = node2->schema->parent;
3110 parent1 && parent2;
3111 parent1 = parent1->parent, parent2 = parent2->parent) {
3112 if (!lyd_compare_schema_equal(parent1, parent2)) {
3113 return 0;
3114 }
3115 }
3116
3117 if (parent1 || parent2) {
3118 return 0;
3119 }
3120
3121 return 1;
3122}
3123
3124/**
3125 * @brief Internal implementation of @ref lyd_compare_single.
3126 * @copydoc lyd_compare_single
3127 * @param[in] parental_schemas_checked Flag used for optimization.
3128 * When this function is called for the first time, the flag must be set to 0.
3129 * The @ref lyd_compare_schema_parents_equal should be called only once during
3130 * recursive calls, and this is accomplished by setting to 1 in the lyd_compare_single_ body.
3131 */
3132static LY_ERR
3133lyd_compare_single_(const struct lyd_node *node1, const struct lyd_node *node2,
3134 uint32_t options, ly_bool parental_schemas_checked)
Radek Krejci1f05b6a2019-07-18 16:15:06 +02003135{
3136 const struct lyd_node *iter1, *iter2;
3137 struct lyd_node_term *term1, *term2;
3138 struct lyd_node_any *any1, *any2;
Michal Vasko52927e22020-03-16 17:26:14 +01003139 struct lyd_node_opaq *opaq1, *opaq2;
Radek Krejci1f05b6a2019-07-18 16:15:06 +02003140 size_t len1, len2;
Radek Krejci084289f2019-07-09 17:35:30 +02003141
Radek Krejci1f05b6a2019-07-18 16:15:06 +02003142 if (!node1 || !node2) {
3143 if (node1 == node2) {
3144 return LY_SUCCESS;
3145 } else {
3146 return LY_ENOT;
3147 }
3148 }
3149
aPiecek2f63f952021-03-30 12:22:18 +02003150 if (LYD_CTX(node1) == LYD_CTX(node2)) {
3151 /* same contexts */
3152 if (node1->schema != node2->schema) {
3153 return LY_ENOT;
3154 }
3155 } else {
3156 /* different contexts */
3157 if (!lyd_compare_schema_equal(node1->schema, node2->schema)) {
3158 return LY_ENOT;
3159 }
3160 if (!parental_schemas_checked) {
3161 if (!lyd_compare_schema_parents_equal(node1, node2)) {
3162 return LY_ENOT;
3163 }
3164 parental_schemas_checked = 1;
3165 }
Radek Krejci1f05b6a2019-07-18 16:15:06 +02003166 }
3167
3168 if (node1->hash != node2->hash) {
3169 return LY_ENOT;
3170 }
aPiecek2f63f952021-03-30 12:22:18 +02003171 /* 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 +02003172
Michal Vasko52927e22020-03-16 17:26:14 +01003173 if (!node1->schema) {
3174 opaq1 = (struct lyd_node_opaq *)node1;
3175 opaq2 = (struct lyd_node_opaq *)node2;
aPiecek2f63f952021-03-30 12:22:18 +02003176 if ((strcmp(opaq1->name.name, opaq2->name.name)) || (opaq1->format != opaq2->format) ||
3177 (strcmp(opaq1->name.module_ns, opaq2->name.module_ns))) {
Radek Krejci1f05b6a2019-07-18 16:15:06 +02003178 return LY_ENOT;
3179 }
Michal Vasko52927e22020-03-16 17:26:14 +01003180 switch (opaq1->format) {
Radek Krejci8df109d2021-04-23 12:19:08 +02003181 case LY_VALUE_XML:
Radek Krejci09c77442021-04-26 11:10:34 +02003182 if (lyxml_value_compare(LYD_CTX(node1), opaq1->value, opaq1->val_prefix_data, LYD_CTX(node2), opaq2->value,
3183 opaq2->val_prefix_data)) {
Michal Vasko52927e22020-03-16 17:26:14 +01003184 return LY_ENOT;
3185 }
3186 break;
Radek Krejci8df109d2021-04-23 12:19:08 +02003187 case LY_VALUE_JSON:
Radek Krejci1798aae2020-07-14 13:26:06 +02003188 /* prefixes in JSON are unique, so it is not necessary to canonize the values */
3189 if (strcmp(opaq1->value, opaq2->value)) {
3190 return LY_ENOT;
3191 }
3192 break;
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01003193 default:
Michal Vasko52927e22020-03-16 17:26:14 +01003194 /* not allowed */
Michal Vaskob7be7a82020-08-20 09:09:04 +02003195 LOGINT(LYD_CTX(node1));
Michal Vasko52927e22020-03-16 17:26:14 +01003196 return LY_EINT;
3197 }
3198 if (options & LYD_COMPARE_FULL_RECURSION) {
3199 iter1 = opaq1->child;
3200 iter2 = opaq2->child;
Radek Krejci1f05b6a2019-07-18 16:15:06 +02003201 goto all_children_compare;
Michal Vasko52927e22020-03-16 17:26:14 +01003202 }
3203 return LY_SUCCESS;
3204 } else {
3205 switch (node1->schema->nodetype) {
3206 case LYS_LEAF:
3207 case LYS_LEAFLIST:
3208 if (options & LYD_COMPARE_DEFAULTS) {
3209 if ((node1->flags & LYD_DEFAULT) != (node2->flags & LYD_DEFAULT)) {
3210 return LY_ENOT;
3211 }
3212 }
3213
Michal Vasko1a2bf2e2020-06-16 13:07:48 +02003214 term1 = (struct lyd_node_term *)node1;
3215 term2 = (struct lyd_node_term *)node2;
aPiecek2f63f952021-03-30 12:22:18 +02003216
3217 /* same contexts */
3218 if (LYD_CTX(node1) == LYD_CTX(node2)) {
3219 return term1->value.realtype->plugin->compare(&term1->value, &term2->value);
3220 }
3221
3222 /* different contexts */
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02003223 if (strcmp(lyd_get_value(node1), lyd_get_value(node2))) {
aPiecek2f63f952021-03-30 12:22:18 +02003224 return LY_ENOT;
3225 }
3226 return LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +01003227 case LYS_CONTAINER:
3228 if (options & LYD_COMPARE_DEFAULTS) {
3229 if ((node1->flags & LYD_DEFAULT) != (node2->flags & LYD_DEFAULT)) {
3230 return LY_ENOT;
3231 }
3232 }
3233 if (options & LYD_COMPARE_FULL_RECURSION) {
Michal Vasko9e685082021-01-29 14:49:09 +01003234 iter1 = lyd_child(node1);
3235 iter2 = lyd_child(node2);
Michal Vasko52927e22020-03-16 17:26:14 +01003236 goto all_children_compare;
Radek Krejci1f05b6a2019-07-18 16:15:06 +02003237 }
3238 return LY_SUCCESS;
Michal Vasko1bf09392020-03-27 12:38:10 +01003239 case LYS_RPC:
Michal Vasko52927e22020-03-16 17:26:14 +01003240 case LYS_ACTION:
3241 if (options & LYD_COMPARE_FULL_RECURSION) {
3242 /* TODO action/RPC
3243 goto all_children_compare;
3244 */
Radek Krejci1f05b6a2019-07-18 16:15:06 +02003245 }
3246 return LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +01003247 case LYS_NOTIF:
3248 if (options & LYD_COMPARE_FULL_RECURSION) {
3249 /* TODO Notification
3250 goto all_children_compare;
3251 */
3252 }
3253 return LY_SUCCESS;
3254 case LYS_LIST:
Michal Vasko9e685082021-01-29 14:49:09 +01003255 iter1 = lyd_child(node1);
3256 iter2 = lyd_child(node2);
Michal Vasko52927e22020-03-16 17:26:14 +01003257
3258 if (!(node1->schema->flags & LYS_KEYLESS) && !(options & LYD_COMPARE_FULL_RECURSION)) {
3259 /* lists with keys, their equivalence is based on their keys */
Michal Vasko544e58a2021-01-28 14:33:41 +01003260 for (const struct lysc_node *key = lysc_node_child(node1->schema);
Michal Vaskoc57d9a92020-06-23 13:28:27 +02003261 key && (key->flags & LYS_KEY);
Michal Vasko52927e22020-03-16 17:26:14 +01003262 key = key->next) {
aPiecek2f63f952021-03-30 12:22:18 +02003263 if (lyd_compare_single_(iter1, iter2, options, parental_schemas_checked)) {
Michal Vasko52927e22020-03-16 17:26:14 +01003264 return LY_ENOT;
3265 }
3266 iter1 = iter1->next;
3267 iter2 = iter2->next;
3268 }
3269 } else {
3270 /* lists without keys, their equivalence is based on equivalence of all the children (both direct and indirect) */
3271
Radek Krejci0f969882020-08-21 16:56:47 +02003272all_children_compare:
Michal Vasko52927e22020-03-16 17:26:14 +01003273 if (!iter1 && !iter2) {
3274 /* no children, nothing to compare */
3275 return LY_SUCCESS;
3276 }
3277
Michal Vaskod989ba02020-08-24 10:59:24 +02003278 for ( ; iter1 && iter2; iter1 = iter1->next, iter2 = iter2->next) {
aPiecek2f63f952021-03-30 12:22:18 +02003279 if (lyd_compare_single_(iter1, iter2, options | LYD_COMPARE_FULL_RECURSION, parental_schemas_checked)) {
Michal Vasko52927e22020-03-16 17:26:14 +01003280 return LY_ENOT;
3281 }
3282 }
3283 if (iter1 || iter2) {
3284 return LY_ENOT;
3285 }
3286 }
3287 return LY_SUCCESS;
3288 case LYS_ANYXML:
3289 case LYS_ANYDATA:
Michal Vasko22df3f02020-08-24 13:29:22 +02003290 any1 = (struct lyd_node_any *)node1;
3291 any2 = (struct lyd_node_any *)node2;
Michal Vasko52927e22020-03-16 17:26:14 +01003292
3293 if (any1->value_type != any2->value_type) {
3294 return LY_ENOT;
3295 }
3296 switch (any1->value_type) {
3297 case LYD_ANYDATA_DATATREE:
3298 iter1 = any1->value.tree;
3299 iter2 = any2->value.tree;
3300 goto all_children_compare;
3301 case LYD_ANYDATA_STRING:
3302 case LYD_ANYDATA_XML:
3303 case LYD_ANYDATA_JSON:
3304 len1 = strlen(any1->value.str);
3305 len2 = strlen(any2->value.str);
Michal Vasko69730152020-10-09 16:30:07 +02003306 if ((len1 != len2) || strcmp(any1->value.str, any2->value.str)) {
Michal Vasko52927e22020-03-16 17:26:14 +01003307 return LY_ENOT;
3308 }
3309 return LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +01003310 case LYD_ANYDATA_LYB:
Michal Vasko60ea6352020-06-29 13:39:39 +02003311 len1 = lyd_lyb_data_length(any1->value.mem);
3312 len2 = lyd_lyb_data_length(any2->value.mem);
Michal Vasko69730152020-10-09 16:30:07 +02003313 if ((len1 != len2) || memcmp(any1->value.mem, any2->value.mem, len1)) {
Michal Vasko52927e22020-03-16 17:26:14 +01003314 return LY_ENOT;
3315 }
3316 return LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +01003317 }
Radek Krejci1f05b6a2019-07-18 16:15:06 +02003318 }
3319 }
3320
Michal Vaskob7be7a82020-08-20 09:09:04 +02003321 LOGINT(LYD_CTX(node1));
Radek Krejci1f05b6a2019-07-18 16:15:06 +02003322 return LY_EINT;
3323}
Radek Krejci22ebdba2019-07-25 13:59:43 +02003324
Jan Kundrátc6e39de2021-12-09 16:01:19 +01003325LIBYANG_API_DEF LY_ERR
aPiecek2f63f952021-03-30 12:22:18 +02003326lyd_compare_single(const struct lyd_node *node1, const struct lyd_node *node2, uint32_t options)
3327{
3328 return lyd_compare_single_(node1, node2, options, 0);
3329}
3330
Jan Kundrátc6e39de2021-12-09 16:01:19 +01003331LIBYANG_API_DEF LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003332lyd_compare_siblings(const struct lyd_node *node1, const struct lyd_node *node2, uint32_t options)
Michal Vasko8f359bf2020-07-28 10:41:15 +02003333{
Michal Vaskod989ba02020-08-24 10:59:24 +02003334 for ( ; node1 && node2; node1 = node1->next, node2 = node2->next) {
Michal Vasko8f359bf2020-07-28 10:41:15 +02003335 LY_CHECK_RET(lyd_compare_single(node1, node2, options));
3336 }
3337
Michal Vasko11a81432020-07-28 16:31:29 +02003338 if (node1 == node2) {
3339 return LY_SUCCESS;
Michal Vasko8f359bf2020-07-28 10:41:15 +02003340 }
Michal Vasko11a81432020-07-28 16:31:29 +02003341 return LY_ENOT;
Michal Vasko8f359bf2020-07-28 10:41:15 +02003342}
3343
Jan Kundrátc6e39de2021-12-09 16:01:19 +01003344LIBYANG_API_DEF LY_ERR
Michal Vasko21725742020-06-29 11:49:25 +02003345lyd_compare_meta(const struct lyd_meta *meta1, const struct lyd_meta *meta2)
3346{
3347 if (!meta1 || !meta2) {
3348 if (meta1 == meta2) {
3349 return LY_SUCCESS;
3350 } else {
3351 return LY_ENOT;
3352 }
3353 }
3354
Michal Vaskoa8083062020-11-06 17:22:10 +01003355 if ((meta1->annotation->module->ctx != meta2->annotation->module->ctx) || (meta1->annotation != meta2->annotation)) {
Michal Vasko21725742020-06-29 11:49:25 +02003356 return LY_ENOT;
3357 }
3358
Michal Vasko21725742020-06-29 11:49:25 +02003359 return meta1->value.realtype->plugin->compare(&meta1->value, &meta2->value);
3360}
3361
Radek Krejci22ebdba2019-07-25 13:59:43 +02003362/**
Michal Vasko9cf62422021-07-01 13:29:32 +02003363 * @brief Create a copy of the attribute.
3364 *
3365 * @param[in] attr Attribute to copy.
3366 * @param[in] node Opaque where to append the new attribute.
3367 * @param[out] dup Optional created attribute copy.
3368 * @return LY_ERR value.
3369 */
3370static LY_ERR
3371lyd_dup_attr_single(const struct lyd_attr *attr, struct lyd_node *node, struct lyd_attr **dup)
3372{
3373 LY_ERR ret = LY_SUCCESS;
3374 struct lyd_attr *a, *last;
3375 struct lyd_node_opaq *opaq = (struct lyd_node_opaq *)node;
3376
3377 LY_CHECK_ARG_RET(NULL, attr, node, !node->schema, LY_EINVAL);
3378
3379 /* create a copy */
3380 a = calloc(1, sizeof *attr);
3381 LY_CHECK_ERR_RET(!a, LOGMEM(LYD_CTX(node)), LY_EMEM);
3382
3383 LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), attr->name.name, 0, &a->name.name), finish);
3384 LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), attr->name.prefix, 0, &a->name.prefix), finish);
3385 LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), attr->name.module_ns, 0, &a->name.module_ns), finish);
3386 LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), attr->value, 0, &a->value), finish);
3387 a->hints = attr->hints;
3388 a->format = attr->format;
3389 if (attr->val_prefix_data) {
3390 ret = ly_dup_prefix_data(LYD_CTX(node), attr->format, attr->val_prefix_data, &a->val_prefix_data);
3391 LY_CHECK_GOTO(ret, finish);
3392 }
3393
3394 /* insert as the last attribute */
3395 a->parent = opaq;
3396 if (opaq->attr) {
3397 for (last = opaq->attr; last->next; last = last->next) {}
3398 last->next = a;
3399 } else {
3400 opaq->attr = a;
3401 }
3402
3403finish:
3404 if (ret) {
3405 lyd_free_attr_single(LYD_CTX(node), a);
3406 } else if (dup) {
3407 *dup = a;
3408 }
3409 return LY_SUCCESS;
3410}
3411
3412/**
Michal Vaskoaac267d2022-01-17 13:34:48 +01003413 * @brief Find @p schema equivalent in @p trg_ctx.
3414 *
3415 * @param[in] schema Schema node to find.
3416 * @param[in] trg_ctx Target context to search in.
3417 * @param[in] parent Data parent of @p schema, if any.
3418 * @param[out] trg_schema Found schema from @p trg_ctx to use.
3419 * @return LY_RRR value.
3420 */
3421static LY_ERR
3422lyd_dup_find_schema(const struct lysc_node *schema, const struct ly_ctx *trg_ctx, struct lyd_node *parent,
3423 const struct lysc_node **trg_schema)
3424{
3425 const struct lysc_node *node = NULL, *sparent = NULL;
3426 const struct lys_module *mod = NULL;
3427 char *path;
3428
3429 if (!schema) {
3430 /* opaque node */
3431 *trg_schema = NULL;
3432 return LY_SUCCESS;
3433 }
3434
3435 if (parent && parent->schema) {
3436 /* start from schema parent */
3437 sparent = parent->schema;
3438 } else {
3439 /* start from module */
3440 mod = ly_ctx_get_module_implemented(trg_ctx, schema->module->name);
3441 if (!mod) {
3442 LOGERR(trg_ctx, LY_ENOTFOUND, "Module \"%s\" not present/implemented in the target context.",
3443 schema->module->name);
3444 return LY_ENOTFOUND;
3445 }
3446 }
3447
3448 /* find the schema node */
3449 while ((node = lys_getnext(node, sparent, mod ? mod->compiled : NULL, 0))) {
3450 if (!strcmp(node->module->name, schema->module->name) && !strcmp(node->name, schema->name)) {
3451 *trg_schema = node;
3452 return LY_SUCCESS;
3453 }
3454 }
3455
3456 /* schema node not found */
3457 path = lysc_path(schema, LYSC_PATH_LOG, NULL, 0);
3458 LOGERR(trg_ctx, LY_ENOTFOUND, "Schema node \"%s\" not found in the target context.", path);
3459 free(path);
3460 return LY_ENOTFOUND;
3461}
3462
3463/**
Michal Vasko52927e22020-03-16 17:26:14 +01003464 * @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 +02003465 *
Michal Vaskoaac267d2022-01-17 13:34:48 +01003466 * 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 +02003467 *
Michal Vaskoaac267d2022-01-17 13:34:48 +01003468 * @param[in] node Node to duplicate.
3469 * @param[in] trg_ctx Target context for duplicated nodes.
Radek Krejcif8b95172020-05-15 14:51:06 +02003470 * @param[in] parent Parent to insert into, NULL for top-level sibling.
Michal Vasko67177e52021-08-25 11:15:15 +02003471 * @param[in] insert_last Whether the duplicated node can be inserted as the last child of @p parent. Set for
3472 * recursive duplication as an optimization.
Radek Krejcif8b95172020-05-15 14:51:06 +02003473 * @param[in,out] first First sibling, NULL if no top-level sibling exist yet. Can be also NULL if @p parent is set.
3474 * @param[in] options Bitmask of options flags, see @ref dupoptions.
Michal Vaskoaac267d2022-01-17 13:34:48 +01003475 * @param[out] dup_p Pointer where the created duplicated node is placed (besides connecting it to @p parent / @p first).
3476 * @return LY_ERR value.
Radek Krejci22ebdba2019-07-25 13:59:43 +02003477 */
Michal Vasko52927e22020-03-16 17:26:14 +01003478static LY_ERR
Michal Vaskoaac267d2022-01-17 13:34:48 +01003479lyd_dup_r(const struct lyd_node *node, const struct ly_ctx *trg_ctx, struct lyd_node *parent, ly_bool insert_last,
3480 struct lyd_node **first, uint32_t options, struct lyd_node **dup_p)
Radek Krejci22ebdba2019-07-25 13:59:43 +02003481{
Michal Vasko52927e22020-03-16 17:26:14 +01003482 LY_ERR ret;
Radek Krejci22ebdba2019-07-25 13:59:43 +02003483 struct lyd_node *dup = NULL;
Michal Vasko61551fa2020-07-09 15:45:45 +02003484 struct lyd_meta *meta;
Michal Vasko9cf62422021-07-01 13:29:32 +02003485 struct lyd_attr *attr;
Michal Vasko61551fa2020-07-09 15:45:45 +02003486 struct lyd_node_any *any;
Michal Vaskoaac267d2022-01-17 13:34:48 +01003487 const struct lysc_type *type;
3488 const char *val_can;
Radek Krejci22ebdba2019-07-25 13:59:43 +02003489
Michal Vasko52927e22020-03-16 17:26:14 +01003490 LY_CHECK_ARG_RET(NULL, node, LY_EINVAL);
Radek Krejci22ebdba2019-07-25 13:59:43 +02003491
Michal Vasko52927e22020-03-16 17:26:14 +01003492 if (!node->schema) {
3493 dup = calloc(1, sizeof(struct lyd_node_opaq));
Michal Vaskoaac267d2022-01-17 13:34:48 +01003494 ((struct lyd_node_opaq *)dup)->ctx = trg_ctx;
Michal Vasko52927e22020-03-16 17:26:14 +01003495 } else {
3496 switch (node->schema->nodetype) {
Michal Vasko1bf09392020-03-27 12:38:10 +01003497 case LYS_RPC:
Michal Vasko52927e22020-03-16 17:26:14 +01003498 case LYS_ACTION:
3499 case LYS_NOTIF:
3500 case LYS_CONTAINER:
3501 case LYS_LIST:
3502 dup = calloc(1, sizeof(struct lyd_node_inner));
3503 break;
3504 case LYS_LEAF:
3505 case LYS_LEAFLIST:
3506 dup = calloc(1, sizeof(struct lyd_node_term));
3507 break;
3508 case LYS_ANYDATA:
3509 case LYS_ANYXML:
3510 dup = calloc(1, sizeof(struct lyd_node_any));
3511 break;
3512 default:
Michal Vaskoaac267d2022-01-17 13:34:48 +01003513 LOGINT(trg_ctx);
Michal Vasko52927e22020-03-16 17:26:14 +01003514 ret = LY_EINT;
3515 goto error;
3516 }
Radek Krejci22ebdba2019-07-25 13:59:43 +02003517 }
Michal Vaskoaac267d2022-01-17 13:34:48 +01003518 LY_CHECK_ERR_GOTO(!dup, LOGMEM(trg_ctx); ret = LY_EMEM, error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02003519
Michal Vaskof6df0a02020-06-16 13:08:34 +02003520 if (options & LYD_DUP_WITH_FLAGS) {
3521 dup->flags = node->flags;
3522 } else {
3523 dup->flags = (node->flags & LYD_DEFAULT) | LYD_NEW;
3524 }
Michal Vaskoaac267d2022-01-17 13:34:48 +01003525 if (trg_ctx == LYD_CTX(node)) {
3526 dup->schema = node->schema;
3527 } else {
3528 LY_CHECK_GOTO(ret = lyd_dup_find_schema(node->schema, trg_ctx, parent, &dup->schema), error);
3529 }
Michal Vasko52927e22020-03-16 17:26:14 +01003530 dup->prev = dup;
Radek Krejci22ebdba2019-07-25 13:59:43 +02003531
Michal Vasko9cf62422021-07-01 13:29:32 +02003532 /* duplicate metadata/attributes */
Michal Vasko25a32822020-07-09 15:48:22 +02003533 if (!(options & LYD_DUP_NO_META)) {
Michal Vasko9cf62422021-07-01 13:29:32 +02003534 if (!node->schema) {
3535 LY_LIST_FOR(((struct lyd_node_opaq *)node)->attr, attr) {
3536 LY_CHECK_GOTO(ret = lyd_dup_attr_single(attr, dup, NULL), error);
3537 }
3538 } else {
3539 LY_LIST_FOR(node->meta, meta) {
3540 LY_CHECK_GOTO(ret = lyd_dup_meta_single(meta, dup, NULL), error);
3541 }
Michal Vasko25a32822020-07-09 15:48:22 +02003542 }
3543 }
Radek Krejci22ebdba2019-07-25 13:59:43 +02003544
3545 /* nodetype-specific work */
Michal Vasko52927e22020-03-16 17:26:14 +01003546 if (!dup->schema) {
3547 struct lyd_node_opaq *opaq = (struct lyd_node_opaq *)dup;
3548 struct lyd_node_opaq *orig = (struct lyd_node_opaq *)node;
3549 struct lyd_node *child;
Radek Krejci22ebdba2019-07-25 13:59:43 +02003550
3551 if (options & LYD_DUP_RECURSIVE) {
3552 /* duplicate all the children */
3553 LY_LIST_FOR(orig->child, child) {
Michal Vaskoaac267d2022-01-17 13:34:48 +01003554 LY_CHECK_GOTO(ret = lyd_dup_r(child, trg_ctx, dup, 1, NULL, options, NULL), error);
Michal Vasko52927e22020-03-16 17:26:14 +01003555 }
3556 }
Michal Vaskoaac267d2022-01-17 13:34:48 +01003557 LY_CHECK_GOTO(ret = lydict_insert(trg_ctx, orig->name.name, 0, &opaq->name.name), error);
3558 LY_CHECK_GOTO(ret = lydict_insert(trg_ctx, orig->name.prefix, 0, &opaq->name.prefix), error);
3559 LY_CHECK_GOTO(ret = lydict_insert(trg_ctx, orig->name.module_ns, 0, &opaq->name.module_ns), error);
3560 LY_CHECK_GOTO(ret = lydict_insert(trg_ctx, orig->value, 0, &opaq->value), error);
Michal Vasko9cf62422021-07-01 13:29:32 +02003561 opaq->hints = orig->hints;
3562 opaq->format = orig->format;
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01003563 if (orig->val_prefix_data) {
Michal Vaskoaac267d2022-01-17 13:34:48 +01003564 ret = ly_dup_prefix_data(trg_ctx, opaq->format, orig->val_prefix_data, &opaq->val_prefix_data);
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01003565 LY_CHECK_GOTO(ret, error);
Michal Vasko52927e22020-03-16 17:26:14 +01003566 }
Michal Vasko52927e22020-03-16 17:26:14 +01003567 } else if (dup->schema->nodetype & LYD_NODE_TERM) {
3568 struct lyd_node_term *term = (struct lyd_node_term *)dup;
3569 struct lyd_node_term *orig = (struct lyd_node_term *)node;
3570
3571 term->hash = orig->hash;
Michal Vaskoaac267d2022-01-17 13:34:48 +01003572 if (trg_ctx == LYD_CTX(node)) {
3573 ret = orig->value.realtype->plugin->duplicate(trg_ctx, &orig->value, &term->value);
3574 LY_CHECK_ERR_GOTO(ret, LOGERR(trg_ctx, ret, "Value duplication failed."), error);
3575 } else {
3576 /* store canonical value in the target context */
3577 val_can = lyd_get_value(node);
3578 type = ((struct lysc_node_leaf *)term->schema)->type;
3579 ret = lyd_value_store(trg_ctx, &term->value, type, val_can, strlen(val_can), NULL, LY_VALUE_CANON, NULL,
3580 LYD_HINT_DATA, term->schema, NULL);
3581 LY_CHECK_GOTO(ret, error);
3582 }
Michal Vasko52927e22020-03-16 17:26:14 +01003583 } else if (dup->schema->nodetype & LYD_NODE_INNER) {
3584 struct lyd_node_inner *orig = (struct lyd_node_inner *)node;
3585 struct lyd_node *child;
3586
3587 if (options & LYD_DUP_RECURSIVE) {
3588 /* duplicate all the children */
3589 LY_LIST_FOR(orig->child, child) {
Michal Vaskoaac267d2022-01-17 13:34:48 +01003590 LY_CHECK_GOTO(ret = lyd_dup_r(child, trg_ctx, dup, 1, NULL, options, NULL), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02003591 }
Michal Vasko69730152020-10-09 16:30:07 +02003592 } else if ((dup->schema->nodetype == LYS_LIST) && !(dup->schema->flags & LYS_KEYLESS)) {
Radek Krejci22ebdba2019-07-25 13:59:43 +02003593 /* always duplicate keys of a list */
Radek Krejci22ebdba2019-07-25 13:59:43 +02003594 child = orig->child;
Michal Vasko544e58a2021-01-28 14:33:41 +01003595 for (const struct lysc_node *key = lysc_node_child(dup->schema);
Michal Vaskoc57d9a92020-06-23 13:28:27 +02003596 key && (key->flags & LYS_KEY);
Radek Krejci0fe9b512019-07-26 17:51:05 +02003597 key = key->next) {
Radek Krejci22ebdba2019-07-25 13:59:43 +02003598 if (!child) {
3599 /* possibly not keys are present in filtered tree */
3600 break;
Radek Krejci0fe9b512019-07-26 17:51:05 +02003601 } else if (child->schema != key) {
3602 /* possibly not all keys are present in filtered tree,
3603 * but there can be also some non-key nodes */
3604 continue;
Radek Krejci22ebdba2019-07-25 13:59:43 +02003605 }
Michal Vaskoaac267d2022-01-17 13:34:48 +01003606 LY_CHECK_GOTO(ret = lyd_dup_r(child, trg_ctx, dup, 1, NULL, options, NULL), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02003607 child = child->next;
3608 }
3609 }
3610 lyd_hash(dup);
3611 } else if (dup->schema->nodetype & LYD_NODE_ANY) {
Michal Vasko61551fa2020-07-09 15:45:45 +02003612 dup->hash = node->hash;
3613 any = (struct lyd_node_any *)node;
3614 LY_CHECK_GOTO(ret = lyd_any_copy_value(dup, &any->value, any->value_type), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02003615 }
3616
Michal Vasko52927e22020-03-16 17:26:14 +01003617 /* insert */
Michal Vasko67177e52021-08-25 11:15:15 +02003618 lyd_insert_node(parent, first, dup, insert_last);
Michal Vasko52927e22020-03-16 17:26:14 +01003619
3620 if (dup_p) {
3621 *dup_p = dup;
3622 }
3623 return LY_SUCCESS;
Radek Krejci22ebdba2019-07-25 13:59:43 +02003624
3625error:
Michal Vasko52927e22020-03-16 17:26:14 +01003626 lyd_free_tree(dup);
3627 return ret;
Radek Krejci22ebdba2019-07-25 13:59:43 +02003628}
3629
Michal Vasko29d674b2021-08-25 11:18:35 +02003630/**
3631 * @brief Get a parent node to connect duplicated subtree to.
3632 *
3633 * @param[in] node Node (subtree) to duplicate.
Michal Vaskoaac267d2022-01-17 13:34:48 +01003634 * @param[in] trg_ctx Target context for duplicated nodes.
Michal Vasko29d674b2021-08-25 11:18:35 +02003635 * @param[in] parent Initial parent to connect to.
3636 * @param[in] options Bitmask of options flags, see @ref dupoptions.
3637 * @param[out] dup_parent First duplicated parent node, if any.
3638 * @param[out] local_parent Correct parent to directly connect duplicated @p node to.
3639 * @return LY_ERR value.
3640 */
Michal Vasko3a41dff2020-07-15 14:30:28 +02003641static LY_ERR
Michal Vaskoaac267d2022-01-17 13:34:48 +01003642lyd_dup_get_local_parent(const struct lyd_node *node, const struct ly_ctx *trg_ctx, const struct lyd_node_inner *parent,
3643 uint32_t options, struct lyd_node **dup_parent, struct lyd_node_inner **local_parent)
Radek Krejci22ebdba2019-07-25 13:59:43 +02003644{
Michal Vasko3a41dff2020-07-15 14:30:28 +02003645 const struct lyd_node_inner *orig_parent, *iter;
Radek Krejci857189e2020-09-01 13:26:36 +02003646 ly_bool repeat = 1;
Michal Vasko3a41dff2020-07-15 14:30:28 +02003647
3648 *dup_parent = NULL;
3649 *local_parent = NULL;
3650
3651 for (orig_parent = node->parent; repeat && orig_parent; orig_parent = orig_parent->parent) {
3652 if (parent && (parent->schema == orig_parent->schema)) {
3653 /* stop creating parents, connect what we have into the provided parent */
3654 iter = parent;
3655 repeat = 0;
3656 } else {
3657 iter = NULL;
Michal Vaskoaac267d2022-01-17 13:34:48 +01003658 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 +02003659 (struct lyd_node **)&iter));
Michal Vasko3a41dff2020-07-15 14:30:28 +02003660 }
3661 if (!*local_parent) {
3662 *local_parent = (struct lyd_node_inner *)iter;
3663 }
3664 if (iter->child) {
3665 /* 1) list - add after keys
3666 * 2) provided parent with some children */
3667 iter->child->prev->next = *dup_parent;
3668 if (*dup_parent) {
3669 (*dup_parent)->prev = iter->child->prev;
3670 iter->child->prev = *dup_parent;
3671 }
3672 } else {
3673 ((struct lyd_node_inner *)iter)->child = *dup_parent;
3674 }
3675 if (*dup_parent) {
3676 (*dup_parent)->parent = (struct lyd_node_inner *)iter;
3677 }
3678 *dup_parent = (struct lyd_node *)iter;
3679 }
3680
3681 if (repeat && parent) {
3682 /* given parent and created parents chain actually do not interconnect */
Michal Vaskoaac267d2022-01-17 13:34:48 +01003683 LOGERR(trg_ctx, LY_EINVAL,
Michal Vasko69730152020-10-09 16:30:07 +02003684 "Invalid argument parent (%s()) - does not interconnect with the created node's parents chain.", __func__);
Michal Vasko3a41dff2020-07-15 14:30:28 +02003685 return LY_EINVAL;
3686 }
3687
3688 return LY_SUCCESS;
3689}
3690
3691static LY_ERR
Michal Vaskoaac267d2022-01-17 13:34:48 +01003692lyd_dup(const struct lyd_node *node, const struct ly_ctx *trg_ctx, struct lyd_node_inner *parent, uint32_t options,
3693 ly_bool nosiblings, struct lyd_node **dup)
Michal Vasko3a41dff2020-07-15 14:30:28 +02003694{
3695 LY_ERR rc;
Radek Krejci22ebdba2019-07-25 13:59:43 +02003696 const struct lyd_node *orig; /* original node to be duplicated */
3697 struct lyd_node *first = NULL; /* the first duplicated node, this is returned */
Radek Krejci22ebdba2019-07-25 13:59:43 +02003698 struct lyd_node *top = NULL; /* the most higher created node */
3699 struct lyd_node_inner *local_parent = NULL; /* the direct parent node for the duplicated node(s) */
Radek Krejci22ebdba2019-07-25 13:59:43 +02003700
Michal Vaskoaac267d2022-01-17 13:34:48 +01003701 assert(node && trg_ctx && (!parent || (LYD_CTX(parent) == trg_ctx)));
Radek Krejci22ebdba2019-07-25 13:59:43 +02003702
3703 if (options & LYD_DUP_WITH_PARENTS) {
Michal Vaskoaac267d2022-01-17 13:34:48 +01003704 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 +02003705 &top, &local_parent), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02003706 } else {
3707 local_parent = parent;
3708 }
3709
Radek Krejci22ebdba2019-07-25 13:59:43 +02003710 LY_LIST_FOR(node, orig) {
Michal Vasko35f4d772021-01-12 12:08:57 +01003711 if (lysc_is_key(orig->schema)) {
3712 if (local_parent) {
3713 /* the key must already exist in the parent */
3714 rc = lyd_find_sibling_schema(local_parent->child, orig->schema, first ? NULL : &first);
Michal Vaskoaac267d2022-01-17 13:34:48 +01003715 LY_CHECK_ERR_GOTO(rc, LOGINT(trg_ctx), error);
Michal Vasko35f4d772021-01-12 12:08:57 +01003716 } else {
3717 assert(!(options & LYD_DUP_WITH_PARENTS));
3718 /* duplicating a single key, okay, I suppose... */
Michal Vaskoaac267d2022-01-17 13:34:48 +01003719 rc = lyd_dup_r(orig, trg_ctx, NULL, 0, &first, options, first ? NULL : &first);
Michal Vasko35f4d772021-01-12 12:08:57 +01003720 LY_CHECK_GOTO(rc, error);
3721 }
3722 } else {
3723 /* if there is no local parent, it will be inserted into first */
Michal Vaskoaac267d2022-01-17 13:34:48 +01003724 rc = lyd_dup_r(orig, trg_ctx, local_parent ? &local_parent->node : NULL, 0, &first, options,
3725 first ? NULL : &first);
Michal Vasko35f4d772021-01-12 12:08:57 +01003726 LY_CHECK_GOTO(rc, error);
3727 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02003728 if (nosiblings) {
Radek Krejci22ebdba2019-07-25 13:59:43 +02003729 break;
3730 }
3731 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02003732
Michal Vasko3a41dff2020-07-15 14:30:28 +02003733 if (dup) {
3734 *dup = first;
3735 }
3736 return LY_SUCCESS;
Radek Krejci22ebdba2019-07-25 13:59:43 +02003737
3738error:
3739 if (top) {
3740 lyd_free_tree(top);
3741 } else {
Michal Vaskof03ed032020-03-04 13:31:44 +01003742 lyd_free_siblings(first);
Radek Krejci22ebdba2019-07-25 13:59:43 +02003743 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02003744 return rc;
Radek Krejci22ebdba2019-07-25 13:59:43 +02003745}
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003746
Jan Kundrátc6e39de2021-12-09 16:01:19 +01003747LIBYANG_API_DEF LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003748lyd_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 +02003749{
Michal Vaskoaac267d2022-01-17 13:34:48 +01003750 LY_CHECK_ARG_RET(NULL, node, LY_EINVAL);
3751 if (node && parent && (LYD_CTX(node) != LYD_CTX(parent))) {
3752 LOGERR(NULL, LY_EINVAL, "Different contexts used in node duplication.");
3753 return LY_EINVAL;
3754 }
3755
3756 return lyd_dup(node, LYD_CTX(node), parent, options, 1, dup);
3757}
3758
3759LIBYANG_API_DEF LY_ERR
3760lyd_dup_single_to_ctx(const struct lyd_node *node, const struct ly_ctx *trg_ctx, struct lyd_node_inner *parent,
3761 uint32_t options, struct lyd_node **dup)
3762{
3763 LY_CHECK_ARG_RET(trg_ctx, node, trg_ctx, LY_EINVAL);
3764
3765 return lyd_dup(node, trg_ctx, parent, options, 1, dup);
Michal Vasko3a41dff2020-07-15 14:30:28 +02003766}
3767
Jan Kundrátc6e39de2021-12-09 16:01:19 +01003768LIBYANG_API_DEF LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003769lyd_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 +02003770{
Michal Vaskoaac267d2022-01-17 13:34:48 +01003771 LY_CHECK_ARG_RET(NULL, node, LY_EINVAL);
3772 if (node && parent && (LYD_CTX(node) != LYD_CTX(parent))) {
3773 LOGERR(NULL, LY_EINVAL, "Different contexts used in node duplication.");
3774 return LY_EINVAL;
3775 }
3776
3777 return lyd_dup(node, LYD_CTX(node), parent, options, 0, dup);
3778}
3779
3780LIBYANG_API_DEF LY_ERR
3781lyd_dup_siblings_to_ctx(const struct lyd_node *node, const struct ly_ctx *trg_ctx, struct lyd_node_inner *parent,
3782 uint32_t options, struct lyd_node **dup)
3783{
3784 LY_CHECK_ARG_RET(trg_ctx, node, trg_ctx, LY_EINVAL);
3785
3786 return lyd_dup(node, trg_ctx, parent, options, 0, dup);
Michal Vasko3a41dff2020-07-15 14:30:28 +02003787}
3788
Jan Kundrátc6e39de2021-12-09 16:01:19 +01003789LIBYANG_API_DEF LY_ERR
Michal Vasko3a41dff2020-07-15 14:30:28 +02003790lyd_dup_meta_single(const struct lyd_meta *meta, struct lyd_node *node, struct lyd_meta **dup)
Michal Vasko25a32822020-07-09 15:48:22 +02003791{
Radek Krejci011e4aa2020-09-04 15:22:31 +02003792 LY_ERR ret = LY_SUCCESS;
Michal Vasko25a32822020-07-09 15:48:22 +02003793 struct lyd_meta *mt, *last;
3794
Michal Vasko3a41dff2020-07-15 14:30:28 +02003795 LY_CHECK_ARG_RET(NULL, meta, node, LY_EINVAL);
Michal Vasko25a32822020-07-09 15:48:22 +02003796
3797 /* create a copy */
3798 mt = calloc(1, sizeof *mt);
Michal Vaskob7be7a82020-08-20 09:09:04 +02003799 LY_CHECK_ERR_RET(!mt, LOGMEM(LYD_CTX(node)), LY_EMEM);
Michal Vasko25a32822020-07-09 15:48:22 +02003800 mt->annotation = meta->annotation;
Michal Vaskob7be7a82020-08-20 09:09:04 +02003801 ret = meta->value.realtype->plugin->duplicate(LYD_CTX(node), &meta->value, &mt->value);
Radek Krejci011e4aa2020-09-04 15:22:31 +02003802 LY_CHECK_ERR_GOTO(ret, LOGERR(LYD_CTX(node), LY_EINT, "Value duplication failed."), finish);
3803 LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), meta->name, 0, &mt->name), finish);
Michal Vasko25a32822020-07-09 15:48:22 +02003804
3805 /* insert as the last attribute */
Radek Krejci011e4aa2020-09-04 15:22:31 +02003806 mt->parent = node;
Michal Vasko25a32822020-07-09 15:48:22 +02003807 if (node->meta) {
Radek Krejci1e008d22020-08-17 11:37:37 +02003808 for (last = node->meta; last->next; last = last->next) {}
Michal Vasko25a32822020-07-09 15:48:22 +02003809 last->next = mt;
3810 } else {
3811 node->meta = mt;
3812 }
3813
Radek Krejci011e4aa2020-09-04 15:22:31 +02003814finish:
3815 if (ret) {
3816 lyd_free_meta_single(mt);
3817 } else if (dup) {
Michal Vasko3a41dff2020-07-15 14:30:28 +02003818 *dup = mt;
3819 }
3820 return LY_SUCCESS;
Michal Vasko25a32822020-07-09 15:48:22 +02003821}
3822
Michal Vasko4490d312020-06-16 13:08:55 +02003823/**
3824 * @brief Merge a source sibling into target siblings.
3825 *
3826 * @param[in,out] first_trg First target sibling, is updated if top-level.
3827 * @param[in] parent_trg Target parent.
3828 * @param[in,out] sibling_src Source sibling to merge, set to NULL if spent.
Michal Vaskocd3f6172021-05-18 16:14:50 +02003829 * @param[in] merge_cb Optional merge callback.
3830 * @param[in] cb_data Arbitrary callback data.
Michal Vasko4490d312020-06-16 13:08:55 +02003831 * @param[in] options Merge options.
Michal Vaskocd3f6172021-05-18 16:14:50 +02003832 * @param[in,out] dup_inst Duplicate instance cache for all @p first_trg siblings.
Michal Vasko4490d312020-06-16 13:08:55 +02003833 * @return LY_ERR value.
3834 */
3835static LY_ERR
3836lyd_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 +02003837 lyd_merge_cb merge_cb, void *cb_data, uint16_t options, struct lyd_dup_inst **dup_inst)
Michal Vasko4490d312020-06-16 13:08:55 +02003838{
Michal Vasko4490d312020-06-16 13:08:55 +02003839 const struct lyd_node *child_src, *tmp, *sibling_src;
Michal Vasko56daf732020-08-10 10:57:18 +02003840 struct lyd_node *match_trg, *dup_src, *elem;
Michal Vaskofee29a92022-01-27 16:18:12 +01003841 struct lyd_node_opaq *opaq_trg, *opaq_src;
Michal Vasko4490d312020-06-16 13:08:55 +02003842 struct lysc_type *type;
Michal Vaskocd3f6172021-05-18 16:14:50 +02003843 struct lyd_dup_inst *child_dup_inst = NULL;
3844 LY_ERR ret;
3845 ly_bool first_inst = 0;
Michal Vasko4490d312020-06-16 13:08:55 +02003846
3847 sibling_src = *sibling_src_p;
Michal Vaskocd3f6172021-05-18 16:14:50 +02003848 if (!sibling_src->schema) {
3849 /* try to find the same opaque node */
3850 lyd_find_sibling_opaq_next(*first_trg, LYD_NAME(sibling_src), &match_trg);
3851 } else if (sibling_src->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) {
Michal Vasko4490d312020-06-16 13:08:55 +02003852 /* try to find the exact instance */
Michal Vaskocd3f6172021-05-18 16:14:50 +02003853 lyd_find_sibling_first(*first_trg, sibling_src, &match_trg);
Michal Vasko4490d312020-06-16 13:08:55 +02003854 } else {
3855 /* try to simply find the node, there cannot be more instances */
Michal Vaskocd3f6172021-05-18 16:14:50 +02003856 lyd_find_sibling_val(*first_trg, sibling_src->schema, NULL, 0, &match_trg);
Michal Vasko4490d312020-06-16 13:08:55 +02003857 }
3858
Michal Vaskocd3f6172021-05-18 16:14:50 +02003859 if (match_trg) {
3860 /* update match as needed */
3861 LY_CHECK_RET(lyd_dup_inst_next(&match_trg, *first_trg, dup_inst));
3862 } else {
3863 /* first instance of this node */
3864 first_inst = 1;
3865 }
3866
3867 if (match_trg) {
3868 /* call callback */
3869 if (merge_cb) {
3870 LY_CHECK_RET(merge_cb(match_trg, sibling_src, cb_data));
3871 }
3872
Michal Vasko4490d312020-06-16 13:08:55 +02003873 /* node found, make sure even value matches for all node types */
Michal Vaskofee29a92022-01-27 16:18:12 +01003874 if (!match_trg->schema) {
3875 if (lyd_compare_single(sibling_src, match_trg, 0)) {
3876 /* update value */
3877 opaq_trg = (struct lyd_node_opaq *)match_trg;
3878 opaq_src = (struct lyd_node_opaq *)sibling_src;
3879
3880 lydict_remove(LYD_CTX(opaq_trg), opaq_trg->value);
3881 lydict_insert(LYD_CTX(opaq_trg), opaq_src->value, 0, &opaq_trg->value);
3882 opaq_trg->hints = opaq_src->hints;
3883
3884 ly_free_prefix_data(opaq_trg->format, opaq_trg->val_prefix_data);
3885 opaq_trg->format = opaq_src->format;
3886 ly_dup_prefix_data(LYD_CTX(opaq_trg), opaq_src->format, opaq_src->val_prefix_data,
3887 &opaq_trg->val_prefix_data);
3888 }
3889 } else if ((match_trg->schema->nodetype == LYS_LEAF) &&
3890 lyd_compare_single(sibling_src, match_trg, LYD_COMPARE_DEFAULTS)) {
Michal Vasko4490d312020-06-16 13:08:55 +02003891 /* since they are different, they cannot both be default */
3892 assert(!(sibling_src->flags & LYD_DEFAULT) || !(match_trg->flags & LYD_DEFAULT));
3893
Michal Vasko3a41dff2020-07-15 14:30:28 +02003894 /* update value (or only LYD_DEFAULT flag) only if flag set or the source node is not default */
3895 if ((options & LYD_MERGE_DEFAULTS) || !(sibling_src->flags & LYD_DEFAULT)) {
Michal Vasko4490d312020-06-16 13:08:55 +02003896 type = ((struct lysc_node_leaf *)match_trg->schema)->type;
Michal Vaskob7be7a82020-08-20 09:09:04 +02003897 type->plugin->free(LYD_CTX(match_trg), &((struct lyd_node_term *)match_trg)->value);
3898 LY_CHECK_RET(type->plugin->duplicate(LYD_CTX(match_trg), &((struct lyd_node_term *)sibling_src)->value,
Michal Vasko69730152020-10-09 16:30:07 +02003899 &((struct lyd_node_term *)match_trg)->value));
Michal Vasko4490d312020-06-16 13:08:55 +02003900
3901 /* copy flags and add LYD_NEW */
Michal Vasko7e8315b2021-08-03 17:01:06 +02003902 match_trg->flags = sibling_src->flags | ((options & LYD_MERGE_WITH_FLAGS) ? 0 : LYD_NEW);
Michal Vasko4490d312020-06-16 13:08:55 +02003903 }
Michal Vasko8f359bf2020-07-28 10:41:15 +02003904 } else if ((match_trg->schema->nodetype & LYS_ANYDATA) && lyd_compare_single(sibling_src, match_trg, 0)) {
Michal Vasko4c583e82020-07-17 12:16:14 +02003905 /* update value */
3906 LY_CHECK_RET(lyd_any_copy_value(match_trg, &((struct lyd_node_any *)sibling_src)->value,
Radek Krejci0f969882020-08-21 16:56:47 +02003907 ((struct lyd_node_any *)sibling_src)->value_type));
Michal Vasko4490d312020-06-16 13:08:55 +02003908
3909 /* copy flags and add LYD_NEW */
Michal Vasko7e8315b2021-08-03 17:01:06 +02003910 match_trg->flags = sibling_src->flags | ((options & LYD_MERGE_WITH_FLAGS) ? 0 : LYD_NEW);
Michal Vaskocd3f6172021-05-18 16:14:50 +02003911 }
3912
3913 /* check descendants, recursively */
3914 ret = LY_SUCCESS;
3915 LY_LIST_FOR_SAFE(lyd_child_no_keys(sibling_src), tmp, child_src) {
3916 ret = lyd_merge_sibling_r(lyd_node_child_p(match_trg), match_trg, &child_src, merge_cb, cb_data, options,
3917 &child_dup_inst);
3918 if (ret) {
3919 break;
Michal Vasko4490d312020-06-16 13:08:55 +02003920 }
3921 }
Michal Vaskocd3f6172021-05-18 16:14:50 +02003922 lyd_dup_inst_free(child_dup_inst);
3923 LY_CHECK_RET(ret);
Michal Vasko4490d312020-06-16 13:08:55 +02003924 } else {
3925 /* node not found, merge it */
3926 if (options & LYD_MERGE_DESTRUCT) {
3927 dup_src = (struct lyd_node *)sibling_src;
3928 lyd_unlink_tree(dup_src);
3929 /* spend it */
3930 *sibling_src_p = NULL;
3931 } else {
Michal Vasko3a41dff2020-07-15 14:30:28 +02003932 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 +02003933 }
3934
Michal Vasko7e8315b2021-08-03 17:01:06 +02003935 if (!(options & LYD_MERGE_WITH_FLAGS)) {
3936 /* set LYD_NEW for all the new nodes, required for validation */
3937 LYD_TREE_DFS_BEGIN(dup_src, elem) {
3938 elem->flags |= LYD_NEW;
3939 LYD_TREE_DFS_END(dup_src, elem);
3940 }
Michal Vasko4490d312020-06-16 13:08:55 +02003941 }
3942
Michal Vaskocd3f6172021-05-18 16:14:50 +02003943 /* insert */
Michal Vasko6ee6f432021-07-16 09:49:14 +02003944 lyd_insert_node(parent_trg, first_trg, dup_src, 0);
Michal Vaskocd3f6172021-05-18 16:14:50 +02003945
3946 if (first_inst) {
3947 /* remember not to find this instance next time */
3948 LY_CHECK_RET(lyd_dup_inst_next(&dup_src, *first_trg, dup_inst));
3949 }
3950
3951 /* call callback, no source node */
3952 if (merge_cb) {
3953 LY_CHECK_RET(merge_cb(dup_src, NULL, cb_data));
3954 }
Michal Vasko4490d312020-06-16 13:08:55 +02003955 }
3956
3957 return LY_SUCCESS;
3958}
3959
Michal Vasko3a41dff2020-07-15 14:30:28 +02003960static LY_ERR
Michal Vaskocd3f6172021-05-18 16:14:50 +02003961lyd_merge(struct lyd_node **target, const struct lyd_node *source, const struct lys_module *mod,
3962 lyd_merge_cb merge_cb, void *cb_data, uint16_t options, ly_bool nosiblings)
Michal Vasko4490d312020-06-16 13:08:55 +02003963{
3964 const struct lyd_node *sibling_src, *tmp;
Michal Vaskocd3f6172021-05-18 16:14:50 +02003965 struct lyd_dup_inst *dup_inst = NULL;
Radek Krejci857189e2020-09-01 13:26:36 +02003966 ly_bool first;
Michal Vaskocd3f6172021-05-18 16:14:50 +02003967 LY_ERR ret = LY_SUCCESS;
Michal Vasko4490d312020-06-16 13:08:55 +02003968
3969 LY_CHECK_ARG_RET(NULL, target, LY_EINVAL);
Michal Vasko1d80a532021-11-24 10:41:05 +01003970 LY_CHECK_CTX_EQUAL_RET(*target ? LYD_CTX(*target) : NULL, source ? LYD_CTX(source) : NULL, mod ? mod->ctx : NULL,
3971 LY_EINVAL);
Michal Vasko4490d312020-06-16 13:08:55 +02003972
3973 if (!source) {
3974 /* nothing to merge */
3975 return LY_SUCCESS;
3976 }
3977
Michal Vasko831422b2020-11-24 11:20:51 +01003978 if ((*target && lysc_data_parent((*target)->schema)) || lysc_data_parent(source->schema)) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02003979 LOGERR(LYD_CTX(source), LY_EINVAL, "Invalid arguments - can merge only 2 top-level subtrees (%s()).", __func__);
Michal Vasko4490d312020-06-16 13:08:55 +02003980 return LY_EINVAL;
3981 }
3982
3983 LY_LIST_FOR_SAFE(source, tmp, sibling_src) {
Michal Vaskocd3f6172021-05-18 16:14:50 +02003984 if (mod && (lyd_owner_module(sibling_src) != mod)) {
3985 /* skip data nodes from different modules */
3986 continue;
3987 }
3988
Radek Krejci857189e2020-09-01 13:26:36 +02003989 first = (sibling_src == source) ? 1 : 0;
Michal Vaskocd3f6172021-05-18 16:14:50 +02003990 ret = lyd_merge_sibling_r(target, NULL, &sibling_src, merge_cb, cb_data, options, &dup_inst);
3991 if (ret) {
3992 break;
3993 }
Michal Vasko4490d312020-06-16 13:08:55 +02003994 if (first && !sibling_src) {
3995 /* source was spent (unlinked), move to the next node */
3996 source = tmp;
3997 }
3998
Michal Vasko3a41dff2020-07-15 14:30:28 +02003999 if (nosiblings) {
Michal Vasko4490d312020-06-16 13:08:55 +02004000 break;
4001 }
4002 }
4003
4004 if (options & LYD_MERGE_DESTRUCT) {
4005 /* free any leftover source data that were not merged */
4006 lyd_free_siblings((struct lyd_node *)source);
4007 }
4008
Michal Vaskocd3f6172021-05-18 16:14:50 +02004009 lyd_dup_inst_free(dup_inst);
4010 return ret;
Michal Vasko4490d312020-06-16 13:08:55 +02004011}
4012
Jan Kundrátc6e39de2021-12-09 16:01:19 +01004013LIBYANG_API_DEF LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004014lyd_merge_tree(struct lyd_node **target, const struct lyd_node *source, uint16_t options)
Michal Vasko3a41dff2020-07-15 14:30:28 +02004015{
Michal Vaskocd3f6172021-05-18 16:14:50 +02004016 return lyd_merge(target, source, NULL, NULL, NULL, options, 1);
Michal Vasko3a41dff2020-07-15 14:30:28 +02004017}
4018
Jan Kundrátc6e39de2021-12-09 16:01:19 +01004019LIBYANG_API_DEF LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004020lyd_merge_siblings(struct lyd_node **target, const struct lyd_node *source, uint16_t options)
Michal Vasko3a41dff2020-07-15 14:30:28 +02004021{
Michal Vaskocd3f6172021-05-18 16:14:50 +02004022 return lyd_merge(target, source, NULL, NULL, NULL, options, 0);
4023}
4024
Jan Kundrátc6e39de2021-12-09 16:01:19 +01004025LIBYANG_API_DEF LY_ERR
Michal Vaskocd3f6172021-05-18 16:14:50 +02004026lyd_merge_module(struct lyd_node **target, const struct lyd_node *source, const struct lys_module *mod,
4027 lyd_merge_cb merge_cb, void *cb_data, uint16_t options)
4028{
4029 return lyd_merge(target, source, mod, merge_cb, cb_data, options, 0);
Michal Vasko3a41dff2020-07-15 14:30:28 +02004030}
4031
Michal Vasko5ec7cda2019-09-11 13:43:08 +02004032static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02004033lyd_path_str_enlarge(char **buffer, size_t *buflen, size_t reqlen, ly_bool is_static)
Michal Vasko5ec7cda2019-09-11 13:43:08 +02004034{
Michal Vasko14654712020-02-06 08:35:21 +01004035 /* ending \0 */
4036 ++reqlen;
4037
Michal Vasko5ec7cda2019-09-11 13:43:08 +02004038 if (reqlen > *buflen) {
4039 if (is_static) {
4040 return LY_EINCOMPLETE;
4041 }
4042
4043 *buffer = ly_realloc(*buffer, reqlen * sizeof **buffer);
4044 if (!*buffer) {
4045 return LY_EMEM;
4046 }
4047
4048 *buflen = reqlen;
4049 }
4050
4051 return LY_SUCCESS;
4052}
4053
Michal Vaskod59035b2020-07-08 12:00:06 +02004054LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02004055lyd_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 +02004056{
4057 const struct lyd_node *key;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02004058 size_t len;
4059 const char *val;
4060 char quot;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02004061
Michal Vasko824d0832021-11-04 15:36:51 +01004062 for (key = lyd_child(node); key && key->schema && (key->schema->flags & LYS_KEY); key = key->next) {
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02004063 val = lyd_get_value(key);
Michal Vasko5ec7cda2019-09-11 13:43:08 +02004064 len = 1 + strlen(key->schema->name) + 2 + strlen(val) + 2;
Michal Vaskoba99a3e2020-08-18 15:50:05 +02004065 LY_CHECK_RET(lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static));
Michal Vasko5ec7cda2019-09-11 13:43:08 +02004066
4067 quot = '\'';
4068 if (strchr(val, '\'')) {
4069 quot = '"';
4070 }
4071 *bufused += sprintf(*buffer + *bufused, "[%s=%c%s%c]", key->schema->name, quot, val, quot);
Michal Vasko5ec7cda2019-09-11 13:43:08 +02004072 }
4073
4074 return LY_SUCCESS;
4075}
4076
4077/**
4078 * @brief Append leaf-list value predicate to path.
4079 *
4080 * @param[in] node Node to print.
4081 * @param[in,out] buffer Buffer to print to.
4082 * @param[in,out] buflen Current buffer length.
4083 * @param[in,out] bufused Current number of characters used in @p buffer.
4084 * @param[in] is_static Whether buffer is static or can be reallocated.
4085 * @return LY_ERR
4086 */
4087static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02004088lyd_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 +02004089{
Michal Vasko5ec7cda2019-09-11 13:43:08 +02004090 size_t len;
4091 const char *val;
4092 char quot;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02004093
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02004094 val = lyd_get_value(node);
Radek Krejcif13b87b2020-12-01 22:02:17 +01004095 len = 4 + strlen(val) + 2; /* "[.='" + val + "']" */
Michal Vaskoba99a3e2020-08-18 15:50:05 +02004096 LY_CHECK_RET(lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static));
Michal Vasko5ec7cda2019-09-11 13:43:08 +02004097
4098 quot = '\'';
4099 if (strchr(val, '\'')) {
4100 quot = '"';
4101 }
4102 *bufused += sprintf(*buffer + *bufused, "[.=%c%s%c]", quot, val, quot);
4103
Michal Vaskoba99a3e2020-08-18 15:50:05 +02004104 return LY_SUCCESS;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02004105}
4106
4107/**
4108 * @brief Append node position (relative to its other instances) predicate to path.
4109 *
4110 * @param[in] node Node to print.
4111 * @param[in,out] buffer Buffer to print to.
4112 * @param[in,out] buflen Current buffer length.
4113 * @param[in,out] bufused Current number of characters used in @p buffer.
4114 * @param[in] is_static Whether buffer is static or can be reallocated.
4115 * @return LY_ERR
4116 */
4117static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02004118lyd_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 +02004119{
Michal Vasko5ec7cda2019-09-11 13:43:08 +02004120 size_t len;
Michal Vasko50cc0562021-05-18 16:15:43 +02004121 uint32_t pos;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02004122 char *val = NULL;
4123 LY_ERR rc;
4124
Michal Vasko50cc0562021-05-18 16:15:43 +02004125 pos = lyd_list_pos(node);
4126 if (asprintf(&val, "%" PRIu32, pos) == -1) {
Michal Vasko5ec7cda2019-09-11 13:43:08 +02004127 return LY_EMEM;
4128 }
4129
4130 len = 1 + strlen(val) + 1;
4131 rc = lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static);
4132 if (rc != LY_SUCCESS) {
4133 goto cleanup;
4134 }
4135
4136 *bufused += sprintf(*buffer + *bufused, "[%s]", val);
4137
4138cleanup:
4139 free(val);
4140 return rc;
4141}
4142
Jan Kundrátc6e39de2021-12-09 16:01:19 +01004143LIBYANG_API_DEF char *
Michal Vasko5ec7cda2019-09-11 13:43:08 +02004144lyd_path(const struct lyd_node *node, LYD_PATH_TYPE pathtype, char *buffer, size_t buflen)
4145{
Radek Krejci857189e2020-09-01 13:26:36 +02004146 ly_bool is_static = 0;
Radek Krejci1deb5be2020-08-26 16:43:36 +02004147 uint32_t i, depth;
Michal Vasko14654712020-02-06 08:35:21 +01004148 size_t bufused = 0, len;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02004149 const struct lyd_node *iter;
4150 const struct lys_module *mod;
Michal Vasko790b2bc2020-08-03 13:35:06 +02004151 LY_ERR rc = LY_SUCCESS;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02004152
4153 LY_CHECK_ARG_RET(NULL, node, NULL);
4154 if (buffer) {
Michal Vasko16385f42021-05-18 16:16:09 +02004155 LY_CHECK_ARG_RET(LYD_CTX(node), buflen > 1, NULL);
Michal Vasko5ec7cda2019-09-11 13:43:08 +02004156 is_static = 1;
Michal Vasko14654712020-02-06 08:35:21 +01004157 } else {
4158 buflen = 0;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02004159 }
4160
4161 switch (pathtype) {
Radek Krejci635d2b82021-01-04 11:26:51 +01004162 case LYD_PATH_STD:
4163 case LYD_PATH_STD_NO_LAST_PRED:
Michal Vasko14654712020-02-06 08:35:21 +01004164 depth = 1;
Michal Vasko9e685082021-01-29 14:49:09 +01004165 for (iter = node; iter->parent; iter = lyd_parent(iter)) {
Michal Vasko5ec7cda2019-09-11 13:43:08 +02004166 ++depth;
4167 }
4168
Michal Vasko5ec7cda2019-09-11 13:43:08 +02004169 goto iter_print;
Michal Vasko14654712020-02-06 08:35:21 +01004170 while (depth) {
Michal Vasko5ec7cda2019-09-11 13:43:08 +02004171 /* find the right node */
Michal Vasko9e685082021-01-29 14:49:09 +01004172 for (iter = node, i = 1; i < depth; iter = lyd_parent(iter), ++i) {}
Michal Vasko5ec7cda2019-09-11 13:43:08 +02004173iter_print:
4174 /* print prefix and name */
4175 mod = NULL;
Michal Vasko9f1b4472022-01-06 13:35:08 +01004176 if (iter->schema && (!iter->parent || !iter->parent->schema ||
4177 (iter->schema->module != iter->parent->schema->module))) {
Michal Vasko5ec7cda2019-09-11 13:43:08 +02004178 mod = iter->schema->module;
4179 }
4180
4181 /* realloc string */
Michal Vaskoad92b672020-11-12 13:11:31 +01004182 len = 1 + (mod ? strlen(mod->name) + 1 : 0) + (iter->schema ? strlen(iter->schema->name) :
4183 strlen(((struct lyd_node_opaq *)iter)->name.name));
Michal Vasko5ec7cda2019-09-11 13:43:08 +02004184 rc = lyd_path_str_enlarge(&buffer, &buflen, bufused + len, is_static);
4185 if (rc != LY_SUCCESS) {
4186 break;
4187 }
4188
4189 /* print next node */
Radek Krejci1798aae2020-07-14 13:26:06 +02004190 bufused += sprintf(buffer + bufused, "/%s%s%s", mod ? mod->name : "", mod ? ":" : "",
Michal Vaskoad92b672020-11-12 13:11:31 +01004191 iter->schema ? iter->schema->name : ((struct lyd_node_opaq *)iter)->name.name);
Michal Vasko5ec7cda2019-09-11 13:43:08 +02004192
Michal Vasko790b2bc2020-08-03 13:35:06 +02004193 /* do not always print the last (first) predicate */
Radek Krejci635d2b82021-01-04 11:26:51 +01004194 if (iter->schema && ((depth > 1) || (pathtype == LYD_PATH_STD))) {
Michal Vasko790b2bc2020-08-03 13:35:06 +02004195 switch (iter->schema->nodetype) {
4196 case LYS_LIST:
4197 if (iter->schema->flags & LYS_KEYLESS) {
4198 /* print its position */
4199 rc = lyd_path_position_predicate(iter, &buffer, &buflen, &bufused, is_static);
4200 } else {
4201 /* print all list keys in predicates */
4202 rc = lyd_path_list_predicate(iter, &buffer, &buflen, &bufused, is_static);
4203 }
4204 break;
4205 case LYS_LEAFLIST:
4206 if (iter->schema->flags & LYS_CONFIG_W) {
4207 /* print leaf-list value */
4208 rc = lyd_path_leaflist_predicate(iter, &buffer, &buflen, &bufused, is_static);
4209 } else {
4210 /* print its position */
4211 rc = lyd_path_position_predicate(iter, &buffer, &buflen, &bufused, is_static);
4212 }
4213 break;
4214 default:
4215 /* nothing to print more */
4216 break;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02004217 }
Michal Vasko5ec7cda2019-09-11 13:43:08 +02004218 }
4219 if (rc != LY_SUCCESS) {
4220 break;
4221 }
4222
Michal Vasko14654712020-02-06 08:35:21 +01004223 --depth;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02004224 }
4225 break;
4226 }
4227
4228 return buffer;
4229}
Michal Vaskoe444f752020-02-10 12:20:06 +01004230
Jan Kundrátc6e39de2021-12-09 16:01:19 +01004231LIBYANG_API_DEF struct lyd_meta *
Michal Vasko25a32822020-07-09 15:48:22 +02004232lyd_find_meta(const struct lyd_meta *first, const struct lys_module *module, const char *name)
4233{
4234 struct lyd_meta *ret = NULL;
4235 const struct ly_ctx *ctx;
4236 const char *prefix, *tmp;
4237 char *str;
4238 size_t pref_len, name_len;
4239
4240 LY_CHECK_ARG_RET(NULL, module || strchr(name, ':'), name, NULL);
Michal Vasko1d80a532021-11-24 10:41:05 +01004241 LY_CHECK_CTX_EQUAL_RET(first ? first->annotation->module->ctx : NULL, module ? module->ctx : NULL, NULL);
Michal Vasko25a32822020-07-09 15:48:22 +02004242
4243 if (!first) {
4244 return NULL;
4245 }
4246
4247 ctx = first->annotation->module->ctx;
4248
4249 /* parse the name */
4250 tmp = name;
4251 if (ly_parse_nodeid(&tmp, &prefix, &pref_len, &name, &name_len) || tmp[0]) {
4252 LOGERR(ctx, LY_EINVAL, "Metadata name \"%s\" is not valid.", name);
4253 return NULL;
4254 }
4255
4256 /* find the module */
4257 if (prefix) {
4258 str = strndup(prefix, pref_len);
4259 module = ly_ctx_get_module_latest(ctx, str);
4260 free(str);
Radek Krejci422afb12021-03-04 16:38:16 +01004261 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 +02004262 }
4263
4264 /* find the metadata */
4265 LY_LIST_FOR(first, first) {
4266 if ((first->annotation->module == module) && !strcmp(first->name, name)) {
4267 ret = (struct lyd_meta *)first;
4268 break;
4269 }
4270 }
4271
4272 return ret;
4273}
4274
Jan Kundrátc6e39de2021-12-09 16:01:19 +01004275LIBYANG_API_DEF LY_ERR
Michal Vaskoe444f752020-02-10 12:20:06 +01004276lyd_find_sibling_first(const struct lyd_node *siblings, const struct lyd_node *target, struct lyd_node **match)
4277{
Michal Vaskoe78faec2021-04-08 17:24:43 +02004278 struct lyd_node **match_p, *iter;
Michal Vaskoe444f752020-02-10 12:20:06 +01004279 struct lyd_node_inner *parent;
Michal Vaskoe78faec2021-04-08 17:24:43 +02004280 ly_bool found;
Michal Vaskoe444f752020-02-10 12:20:06 +01004281
Michal Vaskof03ed032020-03-04 13:31:44 +01004282 LY_CHECK_ARG_RET(NULL, target, LY_EINVAL);
Michal Vasko1d80a532021-11-24 10:41:05 +01004283 LY_CHECK_CTX_EQUAL_RET(siblings ? LYD_CTX(siblings) : NULL, LYD_CTX(target), LY_EINVAL);
Michal Vaskoe444f752020-02-10 12:20:06 +01004284
Michal Vasko6da1e952020-12-09 18:14:38 +01004285 if (!siblings || (siblings->schema && target->schema &&
4286 (lysc_data_parent(siblings->schema) != lysc_data_parent(target->schema)))) {
Michal Vasko62ed12d2020-05-21 10:08:25 +02004287 /* no data or schema mismatch */
Michal Vasko9b368d32020-02-14 13:53:31 +01004288 if (match) {
4289 *match = NULL;
4290 }
Michal Vaskoe444f752020-02-10 12:20:06 +01004291 return LY_ENOTFOUND;
4292 }
4293
Michal Vaskoe78faec2021-04-08 17:24:43 +02004294 /* get first sibling */
4295 siblings = lyd_first_sibling(siblings);
Michal Vaskoe444f752020-02-10 12:20:06 +01004296
Michal Vasko9e685082021-01-29 14:49:09 +01004297 parent = siblings->parent;
Michal Vasko6da1e952020-12-09 18:14:38 +01004298 if (parent && parent->schema && parent->children_ht) {
Michal Vaskoe444f752020-02-10 12:20:06 +01004299 assert(target->hash);
4300
Michal Vaskoe78faec2021-04-08 17:24:43 +02004301 if (lysc_is_dup_inst_list(target->schema)) {
4302 /* we must search the instances from beginning to find the first matching one */
4303 found = 0;
4304 LYD_LIST_FOR_INST(siblings, target->schema, iter) {
4305 if (!lyd_compare_single(target, iter, 0)) {
4306 found = 1;
4307 break;
4308 }
4309 }
4310 if (found) {
4311 siblings = iter;
Michal Vaskoda859032020-07-14 12:20:14 +02004312 } else {
4313 siblings = NULL;
4314 }
Michal Vaskoe444f752020-02-10 12:20:06 +01004315 } else {
Michal Vaskoe78faec2021-04-08 17:24:43 +02004316 /* find by hash */
4317 if (!lyht_find(parent->children_ht, &target, target->hash, (void **)&match_p)) {
4318 siblings = *match_p;
4319 } else {
4320 /* not found */
4321 siblings = NULL;
4322 }
Michal Vaskoe444f752020-02-10 12:20:06 +01004323 }
4324 } else {
4325 /* no children hash table */
Michal Vaskod989ba02020-08-24 10:59:24 +02004326 for ( ; siblings; siblings = siblings->next) {
Michal Vasko8f359bf2020-07-28 10:41:15 +02004327 if (!lyd_compare_single(siblings, target, 0)) {
Michal Vaskoe444f752020-02-10 12:20:06 +01004328 break;
4329 }
4330 }
4331 }
4332
4333 if (!siblings) {
Michal Vasko9b368d32020-02-14 13:53:31 +01004334 if (match) {
4335 *match = NULL;
4336 }
Michal Vaskoe444f752020-02-10 12:20:06 +01004337 return LY_ENOTFOUND;
4338 }
4339
Michal Vasko9b368d32020-02-14 13:53:31 +01004340 if (match) {
4341 *match = (struct lyd_node *)siblings;
4342 }
Michal Vaskoe444f752020-02-10 12:20:06 +01004343 return LY_SUCCESS;
4344}
4345
Radek Krejci857189e2020-09-01 13:26:36 +02004346/**
4347 * @brief Comparison callback to match schema node with a schema of a data node.
4348 *
4349 * @param[in] val1_p Pointer to the schema node
4350 * @param[in] val2_p Pointer to the data node
Michal Vasko62524a92021-02-26 10:08:50 +01004351 * Implementation of ::lyht_value_equal_cb.
Radek Krejci857189e2020-09-01 13:26:36 +02004352 */
4353static ly_bool
4354lyd_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 +01004355{
4356 struct lysc_node *val1;
4357 struct lyd_node *val2;
4358
4359 val1 = *((struct lysc_node **)val1_p);
4360 val2 = *((struct lyd_node **)val2_p);
4361
Michal Vasko90932a92020-02-12 14:33:03 +01004362 if (val1 == val2->schema) {
4363 /* schema match is enough */
4364 return 1;
4365 } else {
4366 return 0;
4367 }
4368}
4369
Michal Vasko92239c72020-11-18 18:17:25 +01004370/**
4371 * @brief Search in the given siblings (NOT recursively) for the first schema node data instance.
4372 * Uses hashes - should be used whenever possible for best performance.
4373 *
4374 * @param[in] siblings Siblings to search in including preceding and succeeding nodes.
4375 * @param[in] schema Target data node schema to find.
4376 * @param[out] match Can be NULL, otherwise the found data node.
4377 * @return LY_SUCCESS on success, @p match set.
4378 * @return LY_ENOTFOUND if not found, @p match set to NULL.
4379 * @return LY_ERR value if another error occurred.
4380 */
Michal Vasko90932a92020-02-12 14:33:03 +01004381static LY_ERR
4382lyd_find_sibling_schema(const struct lyd_node *siblings, const struct lysc_node *schema, struct lyd_node **match)
4383{
4384 struct lyd_node **match_p;
4385 struct lyd_node_inner *parent;
4386 uint32_t hash;
Michal Vasko62524a92021-02-26 10:08:50 +01004387 lyht_value_equal_cb ht_cb;
Michal Vasko90932a92020-02-12 14:33:03 +01004388
Michal Vaskob104f112020-07-17 09:54:54 +02004389 assert(siblings && schema);
Michal Vasko90932a92020-02-12 14:33:03 +01004390
Michal Vasko9e685082021-01-29 14:49:09 +01004391 parent = siblings->parent;
Michal Vasko08fd6132020-11-18 18:17:45 +01004392 if (parent && parent->schema && parent->children_ht) {
Michal Vasko90932a92020-02-12 14:33:03 +01004393 /* calculate our hash */
4394 hash = dict_hash_multi(0, schema->module->name, strlen(schema->module->name));
4395 hash = dict_hash_multi(hash, schema->name, strlen(schema->name));
4396 hash = dict_hash_multi(hash, NULL, 0);
4397
4398 /* use special hash table function */
4399 ht_cb = lyht_set_cb(parent->children_ht, lyd_hash_table_schema_val_equal);
4400
4401 /* find by hash */
4402 if (!lyht_find(parent->children_ht, &schema, hash, (void **)&match_p)) {
4403 siblings = *match_p;
4404 } else {
4405 /* not found */
4406 siblings = NULL;
4407 }
4408
4409 /* set the original hash table compare function back */
4410 lyht_set_cb(parent->children_ht, ht_cb);
4411 } else {
Michal Vaskob104f112020-07-17 09:54:54 +02004412 /* find first sibling */
4413 if (siblings->parent) {
4414 siblings = siblings->parent->child;
4415 } else {
4416 while (siblings->prev->next) {
4417 siblings = siblings->prev;
4418 }
4419 }
4420
4421 /* search manually without hashes */
Michal Vaskod989ba02020-08-24 10:59:24 +02004422 for ( ; siblings; siblings = siblings->next) {
Michal Vasko90932a92020-02-12 14:33:03 +01004423 if (siblings->schema == schema) {
4424 /* schema match is enough */
4425 break;
4426 }
4427 }
4428 }
4429
4430 if (!siblings) {
Michal Vasko9b368d32020-02-14 13:53:31 +01004431 if (match) {
4432 *match = NULL;
4433 }
Michal Vasko90932a92020-02-12 14:33:03 +01004434 return LY_ENOTFOUND;
4435 }
4436
Michal Vasko9b368d32020-02-14 13:53:31 +01004437 if (match) {
4438 *match = (struct lyd_node *)siblings;
4439 }
Michal Vasko90932a92020-02-12 14:33:03 +01004440 return LY_SUCCESS;
4441}
4442
Jan Kundrátc6e39de2021-12-09 16:01:19 +01004443LIBYANG_API_DEF LY_ERR
Michal Vaskoe444f752020-02-10 12:20:06 +01004444lyd_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 +02004445 size_t val_len, struct lyd_node **match)
Michal Vaskoe444f752020-02-10 12:20:06 +01004446{
4447 LY_ERR rc;
4448 struct lyd_node *target = NULL;
4449
Michal Vasko4c583e82020-07-17 12:16:14 +02004450 LY_CHECK_ARG_RET(NULL, schema, !(schema->nodetype & (LYS_CHOICE | LYS_CASE)), LY_EINVAL);
Michal Vasko1d80a532021-11-24 10:41:05 +01004451 LY_CHECK_CTX_EQUAL_RET(siblings ? LYD_CTX(siblings) : NULL, schema->module->ctx, LY_EINVAL);
Michal Vaskoe444f752020-02-10 12:20:06 +01004452
Michal Vasko08fd6132020-11-18 18:17:45 +01004453 if (!siblings || (siblings->schema && (lysc_data_parent(siblings->schema) != lysc_data_parent(schema)))) {
Michal Vasko62ed12d2020-05-21 10:08:25 +02004454 /* no data or schema mismatch */
Michal Vasko9b368d32020-02-14 13:53:31 +01004455 if (match) {
4456 *match = NULL;
4457 }
Michal Vaskoe444f752020-02-10 12:20:06 +01004458 return LY_ENOTFOUND;
4459 }
4460
Michal Vaskof03ed032020-03-04 13:31:44 +01004461 if (key_or_value && !val_len) {
4462 val_len = strlen(key_or_value);
4463 }
4464
Michal Vaskob104f112020-07-17 09:54:54 +02004465 if ((schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) && key_or_value) {
4466 /* create a data node and find the instance */
4467 if (schema->nodetype == LYS_LEAFLIST) {
4468 /* target used attributes: schema, hash, value */
Radek Krejci8df109d2021-04-23 12:19:08 +02004469 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 +02004470 LY_CHECK_RET(rc);
Michal Vaskob104f112020-07-17 09:54:54 +02004471 } else {
Michal Vasko90932a92020-02-12 14:33:03 +01004472 /* target used attributes: schema, hash, child (all keys) */
Michal Vasko004d3152020-06-11 19:59:22 +02004473 LY_CHECK_RET(lyd_create_list2(schema, key_or_value, val_len, &target));
Michal Vasko90932a92020-02-12 14:33:03 +01004474 }
4475
4476 /* find it */
4477 rc = lyd_find_sibling_first(siblings, target, match);
Michal Vaskob104f112020-07-17 09:54:54 +02004478 } else {
4479 /* find the first schema node instance */
4480 rc = lyd_find_sibling_schema(siblings, schema, match);
Michal Vaskoe444f752020-02-10 12:20:06 +01004481 }
4482
Michal Vaskoe444f752020-02-10 12:20:06 +01004483 lyd_free_tree(target);
4484 return rc;
4485}
Michal Vaskoccc02342020-05-21 10:09:21 +02004486
Jan Kundrátc6e39de2021-12-09 16:01:19 +01004487LIBYANG_API_DEF LY_ERR
Michal Vaskoe78faec2021-04-08 17:24:43 +02004488lyd_find_sibling_dup_inst_set(const struct lyd_node *siblings, const struct lyd_node *target, struct ly_set **set)
4489{
4490 struct lyd_node **match_p, *first, *iter;
4491 struct lyd_node_inner *parent;
4492
4493 LY_CHECK_ARG_RET(NULL, target, lysc_is_dup_inst_list(target->schema), set, LY_EINVAL);
Michal Vasko1d80a532021-11-24 10:41:05 +01004494 LY_CHECK_CTX_EQUAL_RET(siblings ? LYD_CTX(siblings) : NULL, LYD_CTX(target), LY_EINVAL);
Michal Vaskoe78faec2021-04-08 17:24:43 +02004495
4496 LY_CHECK_RET(ly_set_new(set));
4497
4498 if (!siblings || (siblings->schema && (lysc_data_parent(siblings->schema) != lysc_data_parent(target->schema)))) {
4499 /* no data or schema mismatch */
4500 return LY_ENOTFOUND;
4501 }
4502
4503 /* get first sibling */
4504 siblings = lyd_first_sibling(siblings);
4505
4506 parent = siblings->parent;
4507 if (parent && parent->schema && parent->children_ht) {
4508 assert(target->hash);
4509
4510 /* find the first instance */
4511 lyd_find_sibling_first(siblings, target, &first);
4512 if (first) {
4513 /* add it so that it is the first in the set */
4514 if (ly_set_add(*set, first, 1, NULL)) {
4515 goto error;
4516 }
4517
4518 /* find by hash */
4519 if (!lyht_find(parent->children_ht, &target, target->hash, (void **)&match_p)) {
4520 iter = *match_p;
4521 } else {
4522 /* not found */
4523 iter = NULL;
4524 }
4525 while (iter) {
4526 /* add all found nodes into the set */
4527 if ((iter != first) && !lyd_compare_single(iter, target, 0) && ly_set_add(*set, iter, 1, NULL)) {
4528 goto error;
4529 }
4530
4531 /* find next instance */
4532 if (lyht_find_next(parent->children_ht, &iter, iter->hash, (void **)&match_p)) {
4533 iter = NULL;
4534 } else {
4535 iter = *match_p;
4536 }
4537 }
4538 }
4539 } else {
4540 /* no children hash table */
4541 LY_LIST_FOR(siblings, siblings) {
4542 if (!lyd_compare_single(target, siblings, 0)) {
4543 ly_set_add(*set, (void *)siblings, 1, NULL);
4544 }
4545 }
4546 }
4547
4548 if (!(*set)->count) {
4549 return LY_ENOTFOUND;
4550 }
4551 return LY_SUCCESS;
4552
4553error:
4554 ly_set_free(*set, NULL);
4555 *set = NULL;
4556 return LY_EMEM;
4557}
4558
Jan Kundrátc6e39de2021-12-09 16:01:19 +01004559LIBYANG_API_DEF LY_ERR
Michal Vasko1d4af6c2021-02-22 13:31:26 +01004560lyd_find_sibling_opaq_next(const struct lyd_node *first, const char *name, struct lyd_node **match)
4561{
4562 LY_CHECK_ARG_RET(NULL, name, LY_EINVAL);
4563
4564 for ( ; first; first = first->next) {
4565 if (!first->schema && !strcmp(LYD_NAME(first), name)) {
4566 break;
4567 }
4568 }
4569
4570 if (match) {
4571 *match = (struct lyd_node *)first;
4572 }
4573 return first ? LY_SUCCESS : LY_ENOTFOUND;
4574}
4575
Jan Kundrátc6e39de2021-12-09 16:01:19 +01004576LIBYANG_API_DEF LY_ERR
Michal Vaskoaac267d2022-01-17 13:34:48 +01004577lyd_find_xpath4(const struct lyd_node *ctx_node, const struct lyd_node *tree, const char *xpath, LY_VALUE_FORMAT format,
4578 void *prefix_data, const struct lyxp_var *vars, struct ly_set **set)
Michal Vaskoccc02342020-05-21 10:09:21 +02004579{
4580 LY_ERR ret = LY_SUCCESS;
Michal Vasko8bd9cc72021-07-16 14:06:10 +02004581 struct lyxp_set xp_set = {0};
Radek Krejcif03a9e22020-09-18 20:09:31 +02004582 struct lyxp_expr *exp = NULL;
Michal Vaskoccc02342020-05-21 10:09:21 +02004583 uint32_t i;
4584
Michal Vaskoaac267d2022-01-17 13:34:48 +01004585 LY_CHECK_ARG_RET(NULL, tree, xpath, format, set, LY_EINVAL);
Michal Vaskoccc02342020-05-21 10:09:21 +02004586
Michal Vasko37c69432021-04-12 14:48:24 +02004587 *set = NULL;
Michal Vaskoccc02342020-05-21 10:09:21 +02004588
Michal Vaskoaac267d2022-01-17 13:34:48 +01004589 /* parse expression */
Michal Vasko279539d2021-12-13 16:58:25 +01004590 ret = lyxp_expr_parse((struct ly_ctx *)LYD_CTX(tree), xpath, 0, 1, &exp);
Radek Krejcif03a9e22020-09-18 20:09:31 +02004591 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskoccc02342020-05-21 10:09:21 +02004592
4593 /* evaluate expression */
Michal Vaskoaac267d2022-01-17 13:34:48 +01004594 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 +02004595 LY_CHECK_GOTO(ret, cleanup);
4596
4597 /* allocate return set */
Radek Krejciba03a5a2020-08-27 14:40:41 +02004598 ret = ly_set_new(set);
4599 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskoccc02342020-05-21 10:09:21 +02004600
4601 /* transform into ly_set */
4602 if (xp_set.type == LYXP_SET_NODE_SET) {
4603 /* allocate memory for all the elements once (even though not all items must be elements but most likely will be) */
4604 (*set)->objs = malloc(xp_set.used * sizeof *(*set)->objs);
Michal Vasko279539d2021-12-13 16:58:25 +01004605 LY_CHECK_ERR_GOTO(!(*set)->objs, LOGMEM(LYD_CTX(tree)); ret = LY_EMEM, cleanup);
Michal Vaskoccc02342020-05-21 10:09:21 +02004606 (*set)->size = xp_set.used;
4607
4608 for (i = 0; i < xp_set.used; ++i) {
4609 if (xp_set.val.nodes[i].type == LYXP_NODE_ELEM) {
Radek Krejci3d92e442020-10-12 12:48:13 +02004610 ret = ly_set_add(*set, xp_set.val.nodes[i].node, 1, NULL);
Radek Krejciba03a5a2020-08-27 14:40:41 +02004611 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskoccc02342020-05-21 10:09:21 +02004612 }
4613 }
4614 }
4615
4616cleanup:
Michal Vasko0691d522020-05-21 13:21:47 +02004617 lyxp_set_free_content(&xp_set);
Michal Vasko279539d2021-12-13 16:58:25 +01004618 lyxp_expr_free((struct ly_ctx *)LYD_CTX(tree), exp);
Radek Krejci8f45abc2020-11-26 12:15:13 +01004619 if (ret) {
4620 ly_set_free(*set, NULL);
4621 *set = NULL;
4622 }
Michal Vaskoccc02342020-05-21 10:09:21 +02004623 return ret;
4624}
Radek Krejcica989142020-11-05 11:32:22 +01004625
Jan Kundrátc6e39de2021-12-09 16:01:19 +01004626LIBYANG_API_DEF LY_ERR
Michal Vaskoaac267d2022-01-17 13:34:48 +01004627lyd_find_xpath3(const struct lyd_node *ctx_node, const struct lyd_node *tree, const char *xpath,
4628 const struct lyxp_var *vars, struct ly_set **set)
4629{
4630 LY_CHECK_ARG_RET(NULL, tree, xpath, set, LY_EINVAL);
4631
4632 return lyd_find_xpath4(ctx_node, tree, xpath, LY_VALUE_JSON, NULL, vars, set);
4633}
4634
4635LIBYANG_API_DEF LY_ERR
Michal Vasko279539d2021-12-13 16:58:25 +01004636lyd_find_xpath2(const struct lyd_node *ctx_node, const char *xpath, const struct lyxp_var *vars, struct ly_set **set)
4637{
4638 LY_CHECK_ARG_RET(NULL, ctx_node, xpath, set, LY_EINVAL);
4639
Michal Vaskoaac267d2022-01-17 13:34:48 +01004640 return lyd_find_xpath4(ctx_node, ctx_node, xpath, LY_VALUE_JSON, NULL, vars, set);
Michal Vasko279539d2021-12-13 16:58:25 +01004641}
4642
Jan Kundrátc6e39de2021-12-09 16:01:19 +01004643LIBYANG_API_DEF LY_ERR
aPiecekfba75362021-10-07 12:39:48 +02004644lyd_find_xpath(const struct lyd_node *ctx_node, const char *xpath, struct ly_set **set)
4645{
Michal Vasko279539d2021-12-13 16:58:25 +01004646 LY_CHECK_ARG_RET(NULL, ctx_node, xpath, set, LY_EINVAL);
4647
Michal Vaskoaac267d2022-01-17 13:34:48 +01004648 return lyd_find_xpath4(ctx_node, ctx_node, xpath, LY_VALUE_JSON, NULL, NULL, set);
aPiecekfba75362021-10-07 12:39:48 +02004649}
4650
Jan Kundrátc6e39de2021-12-09 16:01:19 +01004651LIBYANG_API_DEF LY_ERR
aPiecekfba75362021-10-07 12:39:48 +02004652lyd_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 +02004653{
4654 LY_ERR ret = LY_SUCCESS;
Michal Vasko8bd9cc72021-07-16 14:06:10 +02004655 struct lyxp_set xp_set = {0};
Michal Vaskoc9eb3ca2021-07-16 10:20:37 +02004656 struct lyxp_expr *exp = NULL;
4657
4658 LY_CHECK_ARG_RET(NULL, ctx_node, xpath, result, LY_EINVAL);
4659
4660 /* compile expression */
4661 ret = lyxp_expr_parse((struct ly_ctx *)LYD_CTX(ctx_node), xpath, 0, 1, &exp);
4662 LY_CHECK_GOTO(ret, cleanup);
4663
4664 /* evaluate expression */
aPiecekfba75362021-10-07 12:39:48 +02004665 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 +02004666 LY_CHECK_GOTO(ret, cleanup);
4667
4668 /* transform into boolean */
4669 ret = lyxp_set_cast(&xp_set, LYXP_SET_BOOLEAN);
4670 LY_CHECK_GOTO(ret, cleanup);
4671
4672 /* set result */
4673 *result = xp_set.val.bln;
4674
4675cleanup:
4676 lyxp_set_free_content(&xp_set);
4677 lyxp_expr_free((struct ly_ctx *)LYD_CTX(ctx_node), exp);
4678 return ret;
4679}
4680
Jan Kundrátc6e39de2021-12-09 16:01:19 +01004681LIBYANG_API_DEF LY_ERR
aPiecekfba75362021-10-07 12:39:48 +02004682lyd_eval_xpath(const struct lyd_node *ctx_node, const char *xpath, ly_bool *result)
4683{
4684 return lyd_eval_xpath2(ctx_node, xpath, NULL, result);
4685}
4686
Jan Kundrátc6e39de2021-12-09 16:01:19 +01004687LIBYANG_API_DEF LY_ERR
Michal Vasko3e1f6552021-01-14 09:27:55 +01004688lyd_find_path(const struct lyd_node *ctx_node, const char *path, ly_bool output, struct lyd_node **match)
4689{
4690 LY_ERR ret = LY_SUCCESS;
4691 struct lyxp_expr *expr = NULL;
4692 struct ly_path *lypath = NULL;
4693
4694 LY_CHECK_ARG_RET(NULL, ctx_node, ctx_node->schema, path, LY_EINVAL);
4695
4696 /* parse the path */
Michal Vaskoed725d72021-06-23 12:03:45 +02004697 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 +01004698 LY_PATH_PREFIX_OPTIONAL, LY_PATH_PRED_SIMPLE, &expr);
4699 LY_CHECK_GOTO(ret, cleanup);
4700
4701 /* compile the path */
Michal Vaskoed725d72021-06-23 12:03:45 +02004702 ret = ly_path_compile(LYD_CTX(ctx_node), NULL, ctx_node->schema, NULL, expr,
Michal Vasko0884d212021-10-14 09:21:46 +02004703 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 +01004704 LY_CHECK_GOTO(ret, cleanup);
4705
4706 /* evaluate the path */
4707 ret = ly_path_eval_partial(lypath, ctx_node, NULL, match);
4708
4709cleanup:
4710 lyxp_expr_free(LYD_CTX(ctx_node), expr);
4711 ly_path_free(LYD_CTX(ctx_node), lypath);
4712 return ret;
4713}
4714
Jan Kundrátc6e39de2021-12-09 16:01:19 +01004715LIBYANG_API_DEF LY_ERR
Michal Vaskobb22b182021-06-14 08:14:21 +02004716lyd_find_target(const struct ly_path *path, const struct lyd_node *tree, struct lyd_node **match)
4717{
4718 LY_ERR ret;
4719 struct lyd_node *m;
4720
4721 LY_CHECK_ARG_RET(NULL, path, LY_EINVAL);
4722
4723 ret = ly_path_eval(path, tree, &m);
4724 if (ret) {
4725 if (match) {
4726 *match = NULL;
4727 }
4728 return LY_ENOTFOUND;
4729 }
4730
4731 if (match) {
4732 *match = m;
4733 }
4734 return LY_SUCCESS;
4735}
4736
Jan Kundrátc6e39de2021-12-09 16:01:19 +01004737LIBYANG_API_DEF uint32_t
Radek Krejcica989142020-11-05 11:32:22 +01004738lyd_list_pos(const struct lyd_node *instance)
4739{
4740 const struct lyd_node *iter = NULL;
4741 uint32_t pos = 0;
4742
4743 if (!instance || !(instance->schema->nodetype & (LYS_LIST | LYS_LEAFLIST))) {
4744 return 0;
4745 }
4746
4747 /* data instances are ordered, so we can stop when we found instance of other schema node */
4748 for (iter = instance; iter->schema == instance->schema; iter = iter->prev) {
Radek Krejcibb27df32020-11-13 15:39:16 +01004749 if (pos && (iter->next == NULL)) {
Radek Krejcica989142020-11-05 11:32:22 +01004750 /* overrun to the end of the siblings list */
4751 break;
4752 }
4753 ++pos;
4754 }
4755
4756 return pos;
4757}
Radek Krejci4233f9b2020-11-05 12:38:35 +01004758
Jan Kundrátc6e39de2021-12-09 16:01:19 +01004759LIBYANG_API_DEF struct lyd_node *
Radek Krejci4233f9b2020-11-05 12:38:35 +01004760lyd_first_sibling(const struct lyd_node *node)
4761{
4762 struct lyd_node *start;
4763
4764 if (!node) {
4765 return NULL;
4766 }
4767
4768 /* get the first sibling */
4769 if (node->parent) {
4770 start = node->parent->child;
4771 } else {
Radek Krejcibb27df32020-11-13 15:39:16 +01004772 for (start = (struct lyd_node *)node; start->prev->next; start = start->prev) {}
Radek Krejci4233f9b2020-11-05 12:38:35 +01004773 }
4774
4775 return start;
4776}