blob: 46279a8cb7cb064f1180422e4c3b051ab1e33265 [file] [log] [blame]
Radek Krejcie7b95092019-05-15 11:03:07 +02001/**
Michal Vaskob1b5c262020-03-05 14:29:47 +01002 * @file tree_data.c
Radek Krejcie7b95092019-05-15 11:03:07 +02003 * @author Radek Krejci <rkrejci@cesnet.cz>
Michal Vasko6cd9b6b2020-06-22 10:05:22 +02004 * @brief Data tree functions
Radek Krejcie7b95092019-05-15 11:03:07 +02005 *
Michal Vasko6cd9b6b2020-06-22 10:05:22 +02006 * Copyright (c) 2015 - 2020 CESNET, z.s.p.o.
Radek Krejcie7b95092019-05-15 11:03:07 +02007 *
8 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * https://opensource.org/licenses/BSD-3-Clause
13 */
14
Radek Krejci535ea9f2020-05-29 16:01:05 +020015#define _GNU_SOURCE
16
17#include "tree_data.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020018
Radek Krejci084289f2019-07-09 17:35:30 +020019#include <assert.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020020#include <ctype.h>
Radek Krejci47fab892020-11-05 17:02:41 +010021#include <inttypes.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020022#include <stdarg.h>
23#include <stdint.h>
Radek Krejci535ea9f2020-05-29 16:01:05 +020024#include <stdio.h>
25#include <stdlib.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020026#include <string.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020027
Radek Krejci535ea9f2020-05-29 16:01:05 +020028#include "common.h"
Michal Vasko5aa44c02020-06-29 11:47:02 +020029#include "compat.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020030#include "context.h"
31#include "dict.h"
Michal Vaskoa6669ba2020-08-06 16:14:26 +020032#include "diff.h"
Michal Vasko90932a92020-02-12 14:33:03 +010033#include "hash_table.h"
Radek Krejci47fab892020-11-05 17:02:41 +010034#include "in.h"
Michal Vaskoafac7822020-10-20 14:22:26 +020035#include "in_internal.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020036#include "log.h"
Radek Krejci7931b192020-06-25 17:05:03 +020037#include "parser_data.h"
38#include "parser_internal.h"
Michal Vasko004d3152020-06-11 19:59:22 +020039#include "path.h"
Radek Krejci0aa1f702021-04-01 16:16:19 +020040#include "plugins.h"
Radek Krejcif1ca0ac2021-04-12 16:00:06 +020041#include "plugins_exts/metadata.h"
Radek Krejci3e6632f2021-03-22 22:08:21 +010042#include "plugins_internal.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020043#include "plugins_types.h"
44#include "set.h"
45#include "tree.h"
46#include "tree_data_internal.h"
Radek Krejci859a15a2021-03-05 20:56:59 +010047#include "tree_edit.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020048#include "tree_schema.h"
49#include "tree_schema_internal.h"
Michal Vaskoa6669ba2020-08-06 16:14:26 +020050#include "validation.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020051#include "xml.h"
52#include "xpath.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020053
Michal Vaskob104f112020-07-17 09:54:54 +020054static LY_ERR lyd_find_sibling_schema(const struct lyd_node *siblings, const struct lysc_node *schema,
Radek Krejci0f969882020-08-21 16:56:47 +020055 struct lyd_node **match);
Michal Vaskob104f112020-07-17 09:54:54 +020056
Radek Krejci084289f2019-07-09 17:35:30 +020057LY_ERR
Radek Krejcif9943642021-04-26 10:18:21 +020058lyd_value_store(const struct ly_ctx *ctx, struct lyd_value *val, const struct lysc_type *type, const void *value,
Radek Krejci8df109d2021-04-23 12:19:08 +020059 size_t value_len, ly_bool *dynamic, LY_VALUE_FORMAT format, void *prefix_data, uint32_t hints,
Radek Krejci2efc45b2020-12-22 16:25:44 +010060 const struct lysc_node *ctx_node, ly_bool *incomplete)
Radek Krejci084289f2019-07-09 17:35:30 +020061{
Michal Vaskofeca4fb2020-10-05 08:58:40 +020062 LY_ERR ret;
Radek Krejci084289f2019-07-09 17:35:30 +020063 struct ly_err_item *err = NULL;
Radek Krejci0b013302021-03-29 15:22:32 +020064 uint32_t options = (dynamic && *dynamic ? LYPLG_TYPE_STORE_DYNAMIC : 0);
Radek Krejci084289f2019-07-09 17:35:30 +020065
Michal Vasko62ed8652021-04-28 09:38:37 +020066 if (!value) {
67 value = "";
68 }
Michal Vaskofeca4fb2020-10-05 08:58:40 +020069 if (incomplete) {
70 *incomplete = 0;
Radek Krejci084289f2019-07-09 17:35:30 +020071 }
72
Michal Vasko405cc9e2020-12-01 12:01:27 +010073 ret = type->plugin->store(ctx, type, value, value_len, options, format, prefix_data, hints, ctx_node, val, NULL, &err);
Radek Iša96b2de12021-02-16 10:32:57 +010074 if (dynamic) {
75 *dynamic = 0;
76 }
77
Michal Vasko90932a92020-02-12 14:33:03 +010078 if (ret == LY_EINCOMPLETE) {
Michal Vaskofeca4fb2020-10-05 08:58:40 +020079 if (incomplete) {
80 *incomplete = 1;
81 }
82 } else if (ret) {
83 if (err) {
aPiecek4204b612021-05-03 09:06:16 +020084 LOGVAL_ERRITEM(ctx, err);
Michal Vaskofeca4fb2020-10-05 08:58:40 +020085 ly_err_free(err);
86 } else {
Michal Vaskoc1d49132022-02-21 08:47:23 +010087 LOGVAL(ctx, LYVE_OTHER, "Storing value failed.");
Michal Vaskofeca4fb2020-10-05 08:58:40 +020088 }
89 return ret;
Michal Vasko90932a92020-02-12 14:33:03 +010090 }
91
Michal Vaskofeca4fb2020-10-05 08:58:40 +020092 return LY_SUCCESS;
Michal Vasko90932a92020-02-12 14:33:03 +010093}
94
Radek Krejci38d85362019-09-05 16:26:38 +020095LY_ERR
Michal Vaskofeca4fb2020-10-05 08:58:40 +020096lyd_value_validate_incomplete(const struct ly_ctx *ctx, const struct lysc_type *type, struct lyd_value *val,
Radek Krejci2efc45b2020-12-22 16:25:44 +010097 const struct lyd_node *ctx_node, const struct lyd_node *tree)
Radek Krejci38d85362019-09-05 16:26:38 +020098{
Michal Vaskofeca4fb2020-10-05 08:58:40 +020099 LY_ERR ret;
Radek Krejci38d85362019-09-05 16:26:38 +0200100 struct ly_err_item *err = NULL;
Radek Krejci38d85362019-09-05 16:26:38 +0200101
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200102 assert(type->plugin->validate);
Michal Vasko8d544252020-03-02 10:19:52 +0100103
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200104 ret = type->plugin->validate(ctx, type, ctx_node, tree, val, &err);
105 if (ret) {
Radek Krejci38d85362019-09-05 16:26:38 +0200106 if (err) {
aPiecek4204b612021-05-03 09:06:16 +0200107 LOGVAL_ERRITEM(ctx, err);
Radek Krejci38d85362019-09-05 16:26:38 +0200108 ly_err_free(err);
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200109 } else {
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200110 LOGVAL(ctx, LYVE_OTHER, "Resolving value \"%s\" failed.", type->plugin->print(ctx, val, LY_VALUE_CANON,
111 NULL, NULL, NULL));
Radek Krejci38d85362019-09-05 16:26:38 +0200112 }
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200113 return ret;
Radek Krejci38d85362019-09-05 16:26:38 +0200114 }
115
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200116 return LY_SUCCESS;
Radek Krejci38d85362019-09-05 16:26:38 +0200117}
118
Michal Vaskof937cfe2020-08-03 16:07:12 +0200119LY_ERR
Michal Vaskoaebbce02021-04-06 09:23:37 +0200120lys_value_validate(const struct ly_ctx *ctx, const struct lysc_node *node, const char *value, size_t value_len,
Radek Krejci8df109d2021-04-23 12:19:08 +0200121 LY_VALUE_FORMAT format, void *prefix_data)
Radek Krejci084289f2019-07-09 17:35:30 +0200122{
123 LY_ERR rc = LY_SUCCESS;
124 struct ly_err_item *err = NULL;
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200125 struct lyd_value storage;
Radek Krejci084289f2019-07-09 17:35:30 +0200126 struct lysc_type *type;
Radek Krejci084289f2019-07-09 17:35:30 +0200127
128 LY_CHECK_ARG_RET(ctx, node, value, LY_EINVAL);
129
130 if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
131 LOGARG(ctx, node);
132 return LY_EINVAL;
133 }
134
Michal Vasko22df3f02020-08-24 13:29:22 +0200135 type = ((struct lysc_node_leaf *)node)->type;
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200136 rc = type->plugin->store(ctx ? ctx : node->module->ctx, type, value, value_len, 0, format, prefix_data,
Michal Vasko405cc9e2020-12-01 12:01:27 +0100137 LYD_HINT_SCHEMA, node, &storage, NULL, &err);
Radek Krejci73dead22019-07-11 16:46:16 +0200138 if (rc == LY_EINCOMPLETE) {
139 /* actually success since we do not provide the context tree and call validation with
140 * LY_TYPE_OPTS_INCOMPLETE_DATA */
141 rc = LY_SUCCESS;
142 } else if (rc && err) {
143 if (ctx) {
144 /* log only in case the ctx was provided as input parameter */
Radek Krejciddace2c2021-01-08 11:30:56 +0100145 LOG_LOCSET(NULL, NULL, err->path, NULL);
aPiecek4204b612021-05-03 09:06:16 +0200146 LOGVAL_ERRITEM(ctx, err);
Radek Krejciddace2c2021-01-08 11:30:56 +0100147 LOG_LOCBACK(0, 0, 1, 0);
Radek Krejci084289f2019-07-09 17:35:30 +0200148 }
Radek Krejci73dead22019-07-11 16:46:16 +0200149 ly_err_free(err);
Radek Krejci084289f2019-07-09 17:35:30 +0200150 }
151
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200152 if (!rc) {
153 type->plugin->free(ctx ? ctx : node->module->ctx, &storage);
154 }
Radek Krejci084289f2019-07-09 17:35:30 +0200155 return rc;
156}
157
Jan Kundrá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
Michal Vasko366a4a12020-12-04 16:23:57 +0100810 if (use_value) {
Michal Vasko46f7ac92022-02-24 16:07:27 +0100811 switch (value_type) {
812 case LYD_ANYDATA_DATATREE:
813 any->value.tree = (void *)value;
814 break;
815 case LYD_ANYDATA_STRING:
816 case LYD_ANYDATA_XML:
817 case LYD_ANYDATA_JSON:
Michal Vaskofa505ae2022-02-24 16:24:40 +0100818 LY_CHECK_ERR_RET(lydict_insert_zc(schema->module->ctx, (void *)value, &any->value.str), free(any), LY_EMEM);
Michal Vasko46f7ac92022-02-24 16:07:27 +0100819 break;
820 case LYD_ANYDATA_LYB:
821 any->value.mem = (void *)value;
822 break;
823 }
Michal Vasko366a4a12020-12-04 16:23:57 +0100824 any->value_type = value_type;
825 } else {
826 any_val.str = value;
Michal Vasko9e685082021-01-29 14:49:09 +0100827 ret = lyd_any_copy_value(&any->node, &any_val, value_type);
Michal Vasko366a4a12020-12-04 16:23:57 +0100828 LY_CHECK_ERR_RET(ret, free(any), ret);
829 }
Michal Vasko9e685082021-01-29 14:49:09 +0100830 lyd_hash(&any->node);
Michal Vasko90932a92020-02-12 14:33:03 +0100831
Michal Vasko9e685082021-01-29 14:49:09 +0100832 *node = &any->node;
Michal Vasko90932a92020-02-12 14:33:03 +0100833 return LY_SUCCESS;
834}
835
Michal Vasko52927e22020-03-16 17:26:14 +0100836LY_ERR
Michal Vasko501af032020-11-11 20:27:44 +0100837lyd_create_opaq(const struct ly_ctx *ctx, const char *name, size_t name_len, const char *prefix, size_t pref_len,
838 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 +0200839 LY_VALUE_FORMAT format, void *val_prefix_data, uint32_t hints, struct lyd_node **node)
Michal Vasko52927e22020-03-16 17:26:14 +0100840{
Radek Krejci011e4aa2020-09-04 15:22:31 +0200841 LY_ERR ret = LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +0100842 struct lyd_node_opaq *opaq;
843
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200844 assert(ctx && name && name_len && format);
Michal Vasko52927e22020-03-16 17:26:14 +0100845
Michal Vasko4cd0a602021-05-31 11:11:58 +0200846 if (!value_len && (!dynamic || !*dynamic)) {
Michal Vasko52927e22020-03-16 17:26:14 +0100847 value = "";
848 }
849
850 opaq = calloc(1, sizeof *opaq);
Michal Vasko501af032020-11-11 20:27:44 +0100851 LY_CHECK_ERR_GOTO(!opaq, LOGMEM(ctx); ret = LY_EMEM, finish);
Michal Vasko52927e22020-03-16 17:26:14 +0100852
Michal Vasko9e685082021-01-29 14:49:09 +0100853 opaq->prev = &opaq->node;
Michal Vaskoad92b672020-11-12 13:11:31 +0100854 LY_CHECK_GOTO(ret = lydict_insert(ctx, name, name_len, &opaq->name.name), finish);
Radek Krejci011e4aa2020-09-04 15:22:31 +0200855
Michal Vasko52927e22020-03-16 17:26:14 +0100856 if (pref_len) {
Michal Vaskoad92b672020-11-12 13:11:31 +0100857 LY_CHECK_GOTO(ret = lydict_insert(ctx, prefix, pref_len, &opaq->name.prefix), finish);
Michal Vasko52927e22020-03-16 17:26:14 +0100858 }
Radek Krejci1798aae2020-07-14 13:26:06 +0200859 if (module_key_len) {
Michal Vaskoad92b672020-11-12 13:11:31 +0100860 LY_CHECK_GOTO(ret = lydict_insert(ctx, module_key, module_key_len, &opaq->name.module_ns), finish);
Radek Krejci1798aae2020-07-14 13:26:06 +0200861 }
Michal Vasko52927e22020-03-16 17:26:14 +0100862 if (dynamic && *dynamic) {
Radek Krejci011e4aa2020-09-04 15:22:31 +0200863 LY_CHECK_GOTO(ret = lydict_insert_zc(ctx, (char *)value, &opaq->value), finish);
Michal Vasko52927e22020-03-16 17:26:14 +0100864 *dynamic = 0;
865 } else {
Radek Krejci011e4aa2020-09-04 15:22:31 +0200866 LY_CHECK_GOTO(ret = lydict_insert(ctx, value, value_len, &opaq->value), finish);
Michal Vasko52927e22020-03-16 17:26:14 +0100867 }
Michal Vasko501af032020-11-11 20:27:44 +0100868
869 opaq->format = format;
870 opaq->val_prefix_data = val_prefix_data;
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200871 opaq->hints = hints;
Michal Vasko52927e22020-03-16 17:26:14 +0100872 opaq->ctx = ctx;
873
Radek Krejci011e4aa2020-09-04 15:22:31 +0200874finish:
875 if (ret) {
Michal Vasko9e685082021-01-29 14:49:09 +0100876 lyd_free_tree(&opaq->node);
Michal Vasko501af032020-11-11 20:27:44 +0100877 ly_free_prefix_data(format, val_prefix_data);
Radek Krejci011e4aa2020-09-04 15:22:31 +0200878 } else {
Michal Vasko9e685082021-01-29 14:49:09 +0100879 *node = &opaq->node;
Radek Krejci011e4aa2020-09-04 15:22:31 +0200880 }
881 return ret;
Michal Vasko52927e22020-03-16 17:26:14 +0100882}
883
Jan Kundrátc6e39de2021-12-09 16:01:19 +0100884LIBYANG_API_DEF LY_ERR
Michal Vasko65243892020-12-04 16:26:21 +0100885lyd_new_inner(struct lyd_node *parent, const struct lys_module *module, const char *name, ly_bool output,
886 struct lyd_node **node)
Michal Vasko013a8182020-03-03 10:46:53 +0100887{
888 struct lyd_node *ret = NULL;
889 const struct lysc_node *schema;
Michal Vasko1d80a532021-11-24 10:41:05 +0100890 const struct ly_ctx *ctx = parent ? LYD_CTX(parent) : (module ? module->ctx : NULL);
Michal Vasko013a8182020-03-03 10:46:53 +0100891
Michal Vasko6027eb92020-07-15 16:37:30 +0200892 LY_CHECK_ARG_RET(ctx, parent || module, parent || node, name, LY_EINVAL);
Michal Vasko1d80a532021-11-24 10:41:05 +0100893 LY_CHECK_CTX_EQUAL_RET(parent ? LYD_CTX(parent) : NULL, module ? module->ctx : NULL, LY_EINVAL);
Michal Vasko013a8182020-03-03 10:46:53 +0100894
Michal Vaskof03ed032020-03-04 13:31:44 +0100895 if (!module) {
896 module = parent->schema->module;
897 }
898
Michal Vasko3a41dff2020-07-15 14:30:28 +0200899 schema = lys_find_child(parent ? parent->schema : NULL, module, name, 0,
Radek Krejci41ac9942020-11-02 14:47:56 +0100900 LYS_CONTAINER | LYS_NOTIF | LYS_RPC | LYS_ACTION, output ? LYS_GETNEXT_OUTPUT : 0);
Radek Krejcidd2a7662021-03-12 11:26:34 +0100901 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 +0100902
Michal Vasko3a41dff2020-07-15 14:30:28 +0200903 LY_CHECK_RET(lyd_create_inner(schema, &ret));
904 if (parent) {
Michal Vasko6ee6f432021-07-16 09:49:14 +0200905 lyd_insert_node(parent, NULL, ret, 0);
Michal Vasko013a8182020-03-03 10:46:53 +0100906 }
Michal Vasko3a41dff2020-07-15 14:30:28 +0200907
908 if (node) {
909 *node = ret;
910 }
911 return LY_SUCCESS;
Michal Vasko013a8182020-03-03 10:46:53 +0100912}
913
Jan Kundrátc6e39de2021-12-09 16:01:19 +0100914LIBYANG_API_DEF LY_ERR
Radek Krejcidd2a7662021-03-12 11:26:34 +0100915lyd_new_ext_inner(const struct lysc_ext_instance *ext, const char *name, struct lyd_node **node)
916{
917 struct lyd_node *ret = NULL;
918 const struct lysc_node *schema;
919 struct ly_ctx *ctx = ext ? ext->module->ctx : NULL;
920
921 LY_CHECK_ARG_RET(ctx, ext, node, name, LY_EINVAL);
922
923 schema = lysc_ext_find_node(ext, NULL, name, 0, LYS_CONTAINER | LYS_NOTIF | LYS_RPC | LYS_ACTION, 0);
924 if (!schema) {
925 if (ext->argument) {
926 LOGERR(ctx, LY_EINVAL, "Inner node (not a list) \"%s\" not found in instance \"%s\" of extension %s.",
927 name, ext->argument, ext->def->name);
928 } else {
929 LOGERR(ctx, LY_EINVAL, "Inner node (not a list) \"%s\" not found in instance of extension %s.",
930 name, ext->def->name);
931 }
932 return LY_ENOTFOUND;
933 }
934 LY_CHECK_RET(lyd_create_inner(schema, &ret));
935
936 *node = ret;
937
938 return LY_SUCCESS;
939}
940
Michal Vasko208e6d62021-06-28 11:23:50 +0200941/**
942 * @brief Create a new list node in the data tree.
943 *
944 * @param[in] parent Parent node for the node being created. NULL in case of creating a top level element.
945 * @param[in] module Module of the node being created. If NULL, @p parent module will be used.
946 * @param[in] name Schema node name of the new data node. The node must be #LYS_LIST.
947 * @param[in] format Format of key values.
948 * @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
949 * taken into consideration. Otherwise, the output's data node is going to be created.
950 * @param[out] node Optional created node.
951 * @param[in] ap Ordered key values of the new list instance, all must be set. For ::LY_VALUE_LYB, every value must
952 * be followed by the value length.
953 * @return LY_ERR value.
954 */
955static LY_ERR
956_lyd_new_list(struct lyd_node *parent, const struct lys_module *module, const char *name, LY_VALUE_FORMAT format,
957 ly_bool output, struct lyd_node **node, va_list ap)
Michal Vasko013a8182020-03-03 10:46:53 +0100958{
959 struct lyd_node *ret = NULL, *key;
960 const struct lysc_node *schema, *key_s;
Michal Vasko1d80a532021-11-24 10:41:05 +0100961 const struct ly_ctx *ctx = parent ? LYD_CTX(parent) : (module ? module->ctx : NULL);
Michal Vasko208e6d62021-06-28 11:23:50 +0200962 const void *key_val;
963 uint32_t key_len;
Michal Vasko013a8182020-03-03 10:46:53 +0100964 LY_ERR rc = LY_SUCCESS;
965
Michal Vasko6027eb92020-07-15 16:37:30 +0200966 LY_CHECK_ARG_RET(ctx, parent || module, parent || node, name, LY_EINVAL);
Michal Vasko1d80a532021-11-24 10:41:05 +0100967 LY_CHECK_CTX_EQUAL_RET(parent ? LYD_CTX(parent) : NULL, module ? module->ctx : NULL, LY_EINVAL);
Michal Vasko013a8182020-03-03 10:46:53 +0100968
Michal Vaskof03ed032020-03-04 13:31:44 +0100969 if (!module) {
970 module = parent->schema->module;
971 }
972
Radek Krejci41ac9942020-11-02 14:47:56 +0100973 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 +0200974 LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "List node \"%s\" not found.", name), LY_ENOTFOUND);
Michal Vasko013a8182020-03-03 10:46:53 +0100975
976 /* create list inner node */
Michal Vasko3a41dff2020-07-15 14:30:28 +0200977 LY_CHECK_RET(lyd_create_inner(schema, &ret));
Michal Vasko013a8182020-03-03 10:46:53 +0100978
Michal Vasko013a8182020-03-03 10:46:53 +0100979 /* create and insert all the keys */
Michal Vasko544e58a2021-01-28 14:33:41 +0100980 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 +0200981 if (format == LY_VALUE_LYB) {
982 key_val = va_arg(ap, const void *);
983 key_len = va_arg(ap, uint32_t);
984 } else {
985 key_val = va_arg(ap, const char *);
986 key_len = key_val ? strlen((char *)key_val) : 0;
987 }
Michal Vasko013a8182020-03-03 10:46:53 +0100988
Michal Vasko208e6d62021-06-28 11:23:50 +0200989 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 +0200990 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko6ee6f432021-07-16 09:49:14 +0200991 lyd_insert_node(ret, NULL, key, 1);
Michal Vasko013a8182020-03-03 10:46:53 +0100992 }
993
Michal Vasko013a8182020-03-03 10:46:53 +0100994 if (parent) {
Michal Vasko6ee6f432021-07-16 09:49:14 +0200995 lyd_insert_node(parent, NULL, ret, 0);
Michal Vasko013a8182020-03-03 10:46:53 +0100996 }
997
998cleanup:
999 if (rc) {
1000 lyd_free_tree(ret);
1001 ret = NULL;
Michal Vasko3a41dff2020-07-15 14:30:28 +02001002 } else if (node) {
1003 *node = ret;
Michal Vasko013a8182020-03-03 10:46:53 +01001004 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02001005 return rc;
Michal Vasko013a8182020-03-03 10:46:53 +01001006}
1007
Jan Kundrátc6e39de2021-12-09 16:01:19 +01001008LIBYANG_API_DEF LY_ERR
Michal Vasko208e6d62021-06-28 11:23:50 +02001009lyd_new_list(struct lyd_node *parent, const struct lys_module *module, const char *name, ly_bool output,
1010 struct lyd_node **node, ...)
1011{
1012 LY_ERR rc;
1013 va_list ap;
1014
1015 va_start(ap, node);
1016
1017 rc = _lyd_new_list(parent, module, name, LY_VALUE_JSON, output, node, ap);
1018
1019 va_end(ap);
1020 return rc;
1021}
1022
Jan Kundrátc6e39de2021-12-09 16:01:19 +01001023LIBYANG_API_DEF LY_ERR
Michal Vasko208e6d62021-06-28 11:23:50 +02001024lyd_new_list_bin(struct lyd_node *parent, const struct lys_module *module, const char *name, ly_bool output,
1025 struct lyd_node **node, ...)
1026{
1027 LY_ERR rc;
1028 va_list ap;
1029
1030 va_start(ap, node);
1031
1032 rc = _lyd_new_list(parent, module, name, LY_VALUE_LYB, output, node, ap);
1033
1034 va_end(ap);
1035 return rc;
1036}
1037
Jan Kundrátc6e39de2021-12-09 16:01:19 +01001038LIBYANG_API_DEF LY_ERR
Michal Vasko208e6d62021-06-28 11:23:50 +02001039lyd_new_list_canon(struct lyd_node *parent, const struct lys_module *module, const char *name, ly_bool output,
1040 struct lyd_node **node, ...)
1041{
1042 LY_ERR rc;
1043 va_list ap;
1044
1045 va_start(ap, node);
1046
1047 rc = _lyd_new_list(parent, module, name, LY_VALUE_CANON, output, node, ap);
1048
1049 va_end(ap);
1050 return rc;
1051}
1052
Jan Kundrátc6e39de2021-12-09 16:01:19 +01001053LIBYANG_API_DEF LY_ERR
Radek Krejci8247bae2021-03-12 11:47:02 +01001054lyd_new_ext_list(const struct lysc_ext_instance *ext, const char *name, struct lyd_node **node, ...)
1055{
1056 struct lyd_node *ret = NULL, *key;
1057 const struct lysc_node *schema, *key_s;
1058 struct ly_ctx *ctx = ext ? ext->module->ctx : NULL;
1059 va_list ap;
1060 const char *key_val;
1061 LY_ERR rc = LY_SUCCESS;
1062
1063 LY_CHECK_ARG_RET(ctx, ext, node, name, LY_EINVAL);
1064
1065 schema = lysc_ext_find_node(ext, NULL, name, 0, LYS_LIST, 0);
1066 if (!schema) {
1067 if (ext->argument) {
1068 LOGERR(ctx, LY_EINVAL, "List node \"%s\" not found in instance \"%s\" of extension %s.",
1069 name, ext->argument, ext->def->name);
1070 } else {
1071 LOGERR(ctx, LY_EINVAL, "List node \"%s\" not found in instance of extension %s.", name, ext->def->name);
1072 }
1073 return LY_ENOTFOUND;
1074 }
1075 /* create list inner node */
1076 LY_CHECK_RET(lyd_create_inner(schema, &ret));
1077
1078 va_start(ap, node);
1079
1080 /* create and insert all the keys */
1081 for (key_s = lysc_node_child(schema); key_s && (key_s->flags & LYS_KEY); key_s = key_s->next) {
1082 key_val = va_arg(ap, const char *);
1083
Radek Krejci8df109d2021-04-23 12:19:08 +02001084 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 +01001085 NULL, &key);
1086 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko6ee6f432021-07-16 09:49:14 +02001087 lyd_insert_node(ret, NULL, key, 1);
Radek Krejci8247bae2021-03-12 11:47:02 +01001088 }
1089
1090cleanup:
1091 va_end(ap);
1092 if (rc) {
1093 lyd_free_tree(ret);
1094 ret = NULL;
Radek Krejci8247bae2021-03-12 11:47:02 +01001095 }
Radek Krejci8919fbd2021-03-15 09:35:25 +01001096 *node = ret;
Radek Krejci8247bae2021-03-12 11:47:02 +01001097 return rc;
1098}
1099
Jan Kundrátc6e39de2021-12-09 16:01:19 +01001100LIBYANG_API_DEF LY_ERR
Michal Vasko3a41dff2020-07-15 14:30:28 +02001101lyd_new_list2(struct lyd_node *parent, const struct lys_module *module, const char *name, const char *keys,
Radek Krejci41ac9942020-11-02 14:47:56 +01001102 ly_bool output, struct lyd_node **node)
Michal Vasko013a8182020-03-03 10:46:53 +01001103{
1104 struct lyd_node *ret = NULL;
1105 const struct lysc_node *schema;
Michal Vasko1d80a532021-11-24 10:41:05 +01001106 const struct ly_ctx *ctx = parent ? LYD_CTX(parent) : (module ? module->ctx : NULL);
Michal Vasko013a8182020-03-03 10:46:53 +01001107
Michal Vasko6027eb92020-07-15 16:37:30 +02001108 LY_CHECK_ARG_RET(ctx, parent || module, parent || node, name, LY_EINVAL);
Michal Vasko1d80a532021-11-24 10:41:05 +01001109 LY_CHECK_CTX_EQUAL_RET(parent ? LYD_CTX(parent) : NULL, module ? module->ctx : NULL, LY_EINVAL);
Michal Vasko013a8182020-03-03 10:46:53 +01001110
Michal Vaskof03ed032020-03-04 13:31:44 +01001111 if (!module) {
1112 module = parent->schema->module;
1113 }
Michal Vasko004d3152020-06-11 19:59:22 +02001114 if (!keys) {
1115 keys = "";
1116 }
Michal Vaskof03ed032020-03-04 13:31:44 +01001117
Michal Vasko004d3152020-06-11 19:59:22 +02001118 /* find schema node */
Radek Krejci41ac9942020-11-02 14:47:56 +01001119 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 +02001120 LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "List node \"%s\" not found.", name), LY_ENOTFOUND);
Michal Vasko013a8182020-03-03 10:46:53 +01001121
Michal Vasko004d3152020-06-11 19:59:22 +02001122 if ((schema->flags & LYS_KEYLESS) && !keys[0]) {
1123 /* key-less list */
Michal Vasko3a41dff2020-07-15 14:30:28 +02001124 LY_CHECK_RET(lyd_create_inner(schema, &ret));
Michal Vasko004d3152020-06-11 19:59:22 +02001125 } else {
1126 /* create the list node */
Michal Vasko3a41dff2020-07-15 14:30:28 +02001127 LY_CHECK_RET(lyd_create_list2(schema, keys, strlen(keys), &ret));
Michal Vasko004d3152020-06-11 19:59:22 +02001128 }
Michal Vasko004d3152020-06-11 19:59:22 +02001129 if (parent) {
Michal Vasko6ee6f432021-07-16 09:49:14 +02001130 lyd_insert_node(parent, NULL, ret, 0);
Michal Vasko013a8182020-03-03 10:46:53 +01001131 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02001132
1133 if (node) {
1134 *node = ret;
1135 }
1136 return LY_SUCCESS;
Michal Vasko013a8182020-03-03 10:46:53 +01001137}
1138
Radek Krejci09c77442021-04-26 11:10:34 +02001139/**
1140 * @brief Create a new term node in the data tree.
1141 *
1142 * @param[in] parent Parent node for the node being created. NULL in case of creating a top level element.
1143 * @param[in] module Module of the node being created. If NULL, @p parent module will be used.
1144 * @param[in] name Schema node name of the new data node. The node can be ::LYS_LEAF or ::LYS_LEAFLIST.
1145 * @param[in] value Value of the node being created.
1146 * @param[in] value_len Length of @p value.
1147 * @param[in] format Format of @p value.
1148 * @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
1149 * taken into consideration. Otherwise, the output's data node is going to be created.
1150 * @param[out] node Optional created node.
1151 * @return LY_ERR value.
1152 */
1153static LY_ERR
1154_lyd_new_term(struct lyd_node *parent, const struct lys_module *module, const char *name, const void *value,
1155 size_t value_len, LY_VALUE_FORMAT format, ly_bool output, struct lyd_node **node)
Michal Vasko013a8182020-03-03 10:46:53 +01001156{
Michal Vaskocbff3e92020-05-27 12:56:41 +02001157 LY_ERR rc;
Michal Vasko013a8182020-03-03 10:46:53 +01001158 struct lyd_node *ret = NULL;
1159 const struct lysc_node *schema;
Radek Krejci09c77442021-04-26 11:10:34 +02001160 const struct ly_ctx *ctx = parent ? LYD_CTX(parent) : (module ? module->ctx : NULL);
Michal Vasko013a8182020-03-03 10:46:53 +01001161
Michal Vasko6027eb92020-07-15 16:37:30 +02001162 LY_CHECK_ARG_RET(ctx, parent || module, parent || node, name, LY_EINVAL);
Michal Vasko1d80a532021-11-24 10:41:05 +01001163 LY_CHECK_CTX_EQUAL_RET(parent ? LYD_CTX(parent) : NULL, module ? module->ctx : NULL, LY_EINVAL);
Michal Vasko013a8182020-03-03 10:46:53 +01001164
Michal Vaskof03ed032020-03-04 13:31:44 +01001165 if (!module) {
1166 module = parent->schema->module;
1167 }
1168
Radek Krejci41ac9942020-11-02 14:47:56 +01001169 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 +02001170 LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "Term node \"%s\" not found.", name), LY_ENOTFOUND);
Michal Vasko013a8182020-03-03 10:46:53 +01001171
Radek Krejci09c77442021-04-26 11:10:34 +02001172 rc = lyd_create_term(schema, value, value_len, NULL, format, NULL, LYD_HINT_DATA, NULL, &ret);
Michal Vaskofeca4fb2020-10-05 08:58:40 +02001173 LY_CHECK_RET(rc);
Michal Vaskocbff3e92020-05-27 12:56:41 +02001174 if (parent) {
Michal Vasko6ee6f432021-07-16 09:49:14 +02001175 lyd_insert_node(parent, NULL, ret, 0);
Michal Vasko013a8182020-03-03 10:46:53 +01001176 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02001177
1178 if (node) {
1179 *node = ret;
1180 }
1181 return LY_SUCCESS;
Michal Vasko013a8182020-03-03 10:46:53 +01001182}
1183
Jan Kundrátc6e39de2021-12-09 16:01:19 +01001184LIBYANG_API_DEF LY_ERR
Radek Krejci09c77442021-04-26 11:10:34 +02001185lyd_new_term(struct lyd_node *parent, const struct lys_module *module, const char *name, const char *val_str,
1186 ly_bool output, struct lyd_node **node)
1187{
1188 return _lyd_new_term(parent, module, name, val_str, val_str ? strlen(val_str) : 0, LY_VALUE_JSON, output, node);
1189}
1190
Jan Kundrátc6e39de2021-12-09 16:01:19 +01001191LIBYANG_API_DEF LY_ERR
Radek Krejci09c77442021-04-26 11:10:34 +02001192lyd_new_term_bin(struct lyd_node *parent, const struct lys_module *module, const char *name, const void *value,
1193 size_t value_len, ly_bool output, struct lyd_node **node)
1194{
1195 return _lyd_new_term(parent, module, name, value, value_len, LY_VALUE_LYB, output, node);
1196}
1197
Jan Kundrátc6e39de2021-12-09 16:01:19 +01001198LIBYANG_API_DEF LY_ERR
Radek Krejci09c77442021-04-26 11:10:34 +02001199lyd_new_term_canon(struct lyd_node *parent, const struct lys_module *module, const char *name, const char *val_str,
1200 ly_bool output, struct lyd_node **node)
1201{
1202 return _lyd_new_term(parent, module, name, val_str, val_str ? strlen(val_str) : 0, LY_VALUE_CANON, output, node);
1203}
1204
Jan Kundrátc6e39de2021-12-09 16:01:19 +01001205LIBYANG_API_DEF LY_ERR
Radek Krejci8a5afc22021-03-12 11:10:47 +01001206lyd_new_ext_term(const struct lysc_ext_instance *ext, const char *name, const char *val_str, struct lyd_node **node)
1207{
1208 LY_ERR rc;
1209 struct lyd_node *ret = NULL;
1210 const struct lysc_node *schema;
1211 struct ly_ctx *ctx = ext ? ext->module->ctx : NULL;
1212
1213 LY_CHECK_ARG_RET(ctx, ext, node, name, LY_EINVAL);
1214
1215 schema = lysc_ext_find_node(ext, NULL, name, 0, LYD_NODE_TERM, 0);
1216 if (!schema) {
1217 if (ext->argument) {
1218 LOGERR(ctx, LY_EINVAL, "Term node \"%s\" not found in instance \"%s\" of extension %s.",
1219 name, ext->argument, ext->def->name);
1220 } else {
1221 LOGERR(ctx, LY_EINVAL, "Term node \"%s\" not found in instance of extension %s.", name, ext->def->name);
1222 }
1223 return LY_ENOTFOUND;
1224 }
Radek Krejci8df109d2021-04-23 12:19:08 +02001225 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 +01001226 LY_CHECK_RET(rc);
1227
1228 *node = ret;
1229
1230 return LY_SUCCESS;
1231}
1232
Jan Kundrátc6e39de2021-12-09 16:01:19 +01001233LIBYANG_API_DEF LY_ERR
Michal Vasko2a4ab2b2021-03-04 15:52:40 +01001234lyd_new_any(struct lyd_node *parent, const struct lys_module *module, const char *name, const void *value,
1235 ly_bool use_value, LYD_ANYDATA_VALUETYPE value_type, ly_bool output, struct lyd_node **node)
Michal Vasko013a8182020-03-03 10:46:53 +01001236{
1237 struct lyd_node *ret = NULL;
1238 const struct lysc_node *schema;
Michal Vasko1d80a532021-11-24 10:41:05 +01001239 const struct ly_ctx *ctx = parent ? LYD_CTX(parent) : (module ? module->ctx : NULL);
Michal Vasko013a8182020-03-03 10:46:53 +01001240
Michal Vasko6027eb92020-07-15 16:37:30 +02001241 LY_CHECK_ARG_RET(ctx, parent || module, parent || node, name, LY_EINVAL);
Michal Vasko1d80a532021-11-24 10:41:05 +01001242 LY_CHECK_CTX_EQUAL_RET(parent ? LYD_CTX(parent) : NULL, module ? module->ctx : NULL, LY_EINVAL);
Michal Vasko013a8182020-03-03 10:46:53 +01001243
Michal Vaskof03ed032020-03-04 13:31:44 +01001244 if (!module) {
1245 module = parent->schema->module;
1246 }
1247
Radek Krejci41ac9942020-11-02 14:47:56 +01001248 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 +02001249 LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "Any node \"%s\" not found.", name), LY_ENOTFOUND);
Michal Vasko013a8182020-03-03 10:46:53 +01001250
Michal Vasko2a4ab2b2021-03-04 15:52:40 +01001251 LY_CHECK_RET(lyd_create_any(schema, value, value_type, use_value, &ret));
Michal Vasko3a41dff2020-07-15 14:30:28 +02001252 if (parent) {
Michal Vasko6ee6f432021-07-16 09:49:14 +02001253 lyd_insert_node(parent, NULL, ret, 0);
Michal Vasko013a8182020-03-03 10:46:53 +01001254 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02001255
1256 if (node) {
1257 *node = ret;
1258 }
1259 return LY_SUCCESS;
Michal Vasko013a8182020-03-03 10:46:53 +01001260}
1261
Jan Kundrátc6e39de2021-12-09 16:01:19 +01001262LIBYANG_API_DEF LY_ERR
Radek Krejci0b963da2021-03-12 13:23:44 +01001263lyd_new_ext_any(const struct lysc_ext_instance *ext, const char *name, const void *value, ly_bool use_value,
1264 LYD_ANYDATA_VALUETYPE value_type, struct lyd_node **node)
1265{
1266 struct lyd_node *ret = NULL;
1267 const struct lysc_node *schema;
1268 struct ly_ctx *ctx = ext ? ext->module->ctx : NULL;
1269
1270 LY_CHECK_ARG_RET(ctx, ext, node, name, LY_EINVAL);
1271
1272 schema = lysc_ext_find_node(ext, NULL, name, 0, LYD_NODE_ANY, 0);
1273 if (!schema) {
1274 if (ext->argument) {
1275 LOGERR(ctx, LY_EINVAL, "Any node \"%s\" not found in instance \"%s\" of extension %s.",
1276 name, ext->argument, ext->def->name);
1277 } else {
1278 LOGERR(ctx, LY_EINVAL, "Any node \"%s\" not found in instance of extension %s.", name, ext->def->name);
1279 }
1280 return LY_ENOTFOUND;
1281 }
1282 LY_CHECK_RET(lyd_create_any(schema, value, value_type, use_value, &ret));
1283
1284 *node = ret;
1285
1286 return LY_SUCCESS;
1287}
1288
Jan Kundrátc6e39de2021-12-09 16:01:19 +01001289LIBYANG_API_DEF LY_ERR
Michal Vasko871a0252020-11-11 18:35:24 +01001290lyd_new_meta(const struct ly_ctx *ctx, struct lyd_node *parent, const struct lys_module *module, const char *name,
1291 const char *val_str, ly_bool clear_dflt, struct lyd_meta **meta)
Michal Vaskod86997b2020-05-26 15:19:54 +02001292{
Michal Vaskod86997b2020-05-26 15:19:54 +02001293 const char *prefix, *tmp;
Michal Vaskod86997b2020-05-26 15:19:54 +02001294 size_t pref_len, name_len;
1295
Michal Vasko2b5344c2021-02-26 10:12:05 +01001296 LY_CHECK_ARG_RET(ctx, ctx || parent, name, module || strchr(name, ':'), parent || meta, LY_EINVAL);
Michal Vasko1d80a532021-11-24 10:41:05 +01001297 LY_CHECK_CTX_EQUAL_RET(ctx, parent ? LYD_CTX(parent) : NULL, module ? module->ctx : NULL, LY_EINVAL);
Michal Vasko2b5344c2021-02-26 10:12:05 +01001298 if (!ctx) {
1299 ctx = LYD_CTX(parent);
1300 }
Michal Vasko00cbf532020-06-15 13:58:47 +02001301
Michal Vasko871a0252020-11-11 18:35:24 +01001302 if (parent && !parent->schema) {
Michal Vasko33b4fab2021-03-04 15:56:12 +01001303 LOGERR(ctx, LY_EINVAL, "Cannot add metadata \"%s\" to an opaque node \"%s\".", name, LYD_NAME(parent));
Michal Vasko871a0252020-11-11 18:35:24 +01001304 return LY_EINVAL;
1305 }
Michal Vasko610553d2020-11-18 18:15:05 +01001306 if (meta) {
1307 *meta = NULL;
1308 }
Michal Vaskod86997b2020-05-26 15:19:54 +02001309
1310 /* parse the name */
1311 tmp = name;
1312 if (ly_parse_nodeid(&tmp, &prefix, &pref_len, &name, &name_len) || tmp[0]) {
1313 LOGERR(ctx, LY_EINVAL, "Metadata name \"%s\" is not valid.", name);
Michal Vasko871a0252020-11-11 18:35:24 +01001314 return LY_EINVAL;
Michal Vaskod86997b2020-05-26 15:19:54 +02001315 }
1316
1317 /* find the module */
1318 if (prefix) {
Radek Krejci0ad51f12020-07-16 12:08:12 +02001319 module = ly_ctx_get_module_implemented2(ctx, prefix, pref_len);
Radek Krejci422afb12021-03-04 16:38:16 +01001320 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 +02001321 }
1322
1323 /* set value if none */
1324 if (!val_str) {
1325 val_str = "";
1326 }
1327
Radek Krejci8df109d2021-04-23 12:19:08 +02001328 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 +01001329 NULL, LYD_HINT_DATA, parent ? parent->schema : NULL, clear_dflt, NULL);
Michal Vasko871a0252020-11-11 18:35:24 +01001330}
Michal Vasko3a41dff2020-07-15 14:30:28 +02001331
Jan Kundrátc6e39de2021-12-09 16:01:19 +01001332LIBYANG_API_DEF LY_ERR
Michal Vaskoba696702020-11-11 19:12:45 +01001333lyd_new_meta2(const struct ly_ctx *ctx, struct lyd_node *parent, ly_bool clear_dflt, const struct lyd_attr *attr,
1334 struct lyd_meta **meta)
1335{
1336 const struct lys_module *mod;
1337
1338 LY_CHECK_ARG_RET(NULL, ctx, attr, parent || meta, LY_EINVAL);
Michal Vasko1d80a532021-11-24 10:41:05 +01001339 LY_CHECK_CTX_EQUAL_RET(ctx, parent ? LYD_CTX(parent) : NULL, LY_EINVAL);
Michal Vaskoba696702020-11-11 19:12:45 +01001340
1341 if (parent && !parent->schema) {
1342 LOGERR(ctx, LY_EINVAL, "Cannot add metadata to an opaque node \"%s\".", ((struct lyd_node_opaq *)parent)->name);
1343 return LY_EINVAL;
Michal Vasko3a41dff2020-07-15 14:30:28 +02001344 }
Michal Vasko610553d2020-11-18 18:15:05 +01001345 if (meta) {
1346 *meta = NULL;
1347 }
Michal Vaskoba696702020-11-11 19:12:45 +01001348
1349 switch (attr->format) {
Radek Krejci8df109d2021-04-23 12:19:08 +02001350 case LY_VALUE_XML:
Michal Vaskoad92b672020-11-12 13:11:31 +01001351 mod = ly_ctx_get_module_implemented_ns(ctx, attr->name.module_ns);
Michal Vaskoba696702020-11-11 19:12:45 +01001352 if (!mod) {
Michal Vaskoad92b672020-11-12 13:11:31 +01001353 LOGERR(ctx, LY_EINVAL, "Module with namespace \"%s\" not found.", attr->name.module_ns);
Michal Vaskoba696702020-11-11 19:12:45 +01001354 return LY_ENOTFOUND;
1355 }
1356 break;
Radek Krejci8df109d2021-04-23 12:19:08 +02001357 case LY_VALUE_JSON:
Michal Vaskoad92b672020-11-12 13:11:31 +01001358 mod = ly_ctx_get_module_implemented(ctx, attr->name.module_name);
Michal Vaskoba696702020-11-11 19:12:45 +01001359 if (!mod) {
Michal Vaskoad92b672020-11-12 13:11:31 +01001360 LOGERR(ctx, LY_EINVAL, "Module \"%s\" not found.", attr->name.module_name);
Michal Vaskoba696702020-11-11 19:12:45 +01001361 return LY_ENOTFOUND;
1362 }
1363 break;
1364 default:
1365 LOGINT_RET(ctx);
1366 }
1367
Michal Vaskoad92b672020-11-12 13:11:31 +01001368 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 +01001369 NULL, attr->format, attr->val_prefix_data, attr->hints, parent ? parent->schema : NULL, clear_dflt, NULL);
Michal Vaskod86997b2020-05-26 15:19:54 +02001370}
1371
Jan Kundrátc6e39de2021-12-09 16:01:19 +01001372LIBYANG_API_DEF LY_ERR
Michal Vasko00cbf532020-06-15 13:58:47 +02001373lyd_new_opaq(struct lyd_node *parent, const struct ly_ctx *ctx, const char *name, const char *value,
Michal Vasko0ab974d2021-02-24 13:18:26 +01001374 const char *prefix, const char *module_name, struct lyd_node **node)
Michal Vasko00cbf532020-06-15 13:58:47 +02001375{
1376 struct lyd_node *ret = NULL;
Radek Krejci07a55962021-03-02 20:16:43 +01001377
Michal Vasko0ab974d2021-02-24 13:18:26 +01001378 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 +01001379 LY_CHECK_CTX_EQUAL_RET(ctx, parent ? LYD_CTX(parent) : NULL, LY_EINVAL);
Michal Vasko00cbf532020-06-15 13:58:47 +02001380
1381 if (!ctx) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02001382 ctx = LYD_CTX(parent);
Michal Vasko00cbf532020-06-15 13:58:47 +02001383 }
1384 if (!value) {
1385 value = "";
1386 }
1387
Michal Vasko0ab974d2021-02-24 13:18:26 +01001388 LY_CHECK_RET(lyd_create_opaq(ctx, name, strlen(name), prefix, prefix ? strlen(prefix) : 0, module_name,
Radek Krejci8df109d2021-04-23 12:19:08 +02001389 strlen(module_name), value, strlen(value), NULL, LY_VALUE_JSON, NULL, 0, &ret));
Michal Vasko3a41dff2020-07-15 14:30:28 +02001390 if (parent) {
Michal Vasko6ee6f432021-07-16 09:49:14 +02001391 lyd_insert_node(parent, NULL, ret, 1);
Michal Vasko00cbf532020-06-15 13:58:47 +02001392 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02001393
1394 if (node) {
1395 *node = ret;
1396 }
1397 return LY_SUCCESS;
Michal Vasko00cbf532020-06-15 13:58:47 +02001398}
1399
Jan Kundrátc6e39de2021-12-09 16:01:19 +01001400LIBYANG_API_DEF LY_ERR
Michal Vasko8d65f852021-02-17 11:28:13 +01001401lyd_new_opaq2(struct lyd_node *parent, const struct ly_ctx *ctx, const char *name, const char *value,
Michal Vasko0ab974d2021-02-24 13:18:26 +01001402 const char *prefix, const char *module_ns, struct lyd_node **node)
Michal Vasko8d65f852021-02-17 11:28:13 +01001403{
1404 struct lyd_node *ret = NULL;
1405
1406 LY_CHECK_ARG_RET(ctx, parent || ctx, parent || node, name, module_ns, LY_EINVAL);
Michal Vasko1d80a532021-11-24 10:41:05 +01001407 LY_CHECK_CTX_EQUAL_RET(ctx, parent ? LYD_CTX(parent) : NULL, LY_EINVAL);
Michal Vasko8d65f852021-02-17 11:28:13 +01001408
1409 if (!ctx) {
1410 ctx = LYD_CTX(parent);
1411 }
1412 if (!value) {
1413 value = "";
1414 }
1415
Michal Vasko0ab974d2021-02-24 13:18:26 +01001416 LY_CHECK_RET(lyd_create_opaq(ctx, name, strlen(name), prefix, prefix ? strlen(prefix) : 0, module_ns,
Radek Krejci8df109d2021-04-23 12:19:08 +02001417 strlen(module_ns), value, strlen(value), NULL, LY_VALUE_XML, NULL, 0, &ret));
Michal Vasko8d65f852021-02-17 11:28:13 +01001418 if (parent) {
Michal Vasko6ee6f432021-07-16 09:49:14 +02001419 lyd_insert_node(parent, NULL, ret, 1);
Michal Vasko8d65f852021-02-17 11:28:13 +01001420 }
1421
1422 if (node) {
1423 *node = ret;
1424 }
1425 return LY_SUCCESS;
1426}
1427
Jan Kundrátc6e39de2021-12-09 16:01:19 +01001428LIBYANG_API_DEF LY_ERR
Michal Vasko8d65f852021-02-17 11:28:13 +01001429lyd_new_attr(struct lyd_node *parent, const char *module_name, const char *name, const char *value,
Radek Krejci0f969882020-08-21 16:56:47 +02001430 struct lyd_attr **attr)
Michal Vasko00cbf532020-06-15 13:58:47 +02001431{
Radek Krejci1798aae2020-07-14 13:26:06 +02001432 struct lyd_attr *ret = NULL;
Michal Vasko00cbf532020-06-15 13:58:47 +02001433 const struct ly_ctx *ctx;
1434 const char *prefix, *tmp;
Michal Vasko51d21b82020-11-13 18:03:54 +01001435 size_t pref_len, name_len, mod_len;
Michal Vasko00cbf532020-06-15 13:58:47 +02001436
Michal Vasko3a41dff2020-07-15 14:30:28 +02001437 LY_CHECK_ARG_RET(NULL, parent, !parent->schema, name, LY_EINVAL);
Michal Vasko00cbf532020-06-15 13:58:47 +02001438
Michal Vaskob7be7a82020-08-20 09:09:04 +02001439 ctx = LYD_CTX(parent);
Michal Vasko00cbf532020-06-15 13:58:47 +02001440
1441 /* parse the name */
1442 tmp = name;
1443 if (ly_parse_nodeid(&tmp, &prefix, &pref_len, &name, &name_len) || tmp[0]) {
Michal Vaskob69b68c2021-02-17 11:18:16 +01001444 LOGERR(ctx, LY_EINVAL, "Attribute name \"%s\" is not valid.", name);
Michal Vasko3a41dff2020-07-15 14:30:28 +02001445 return LY_EVALID;
Michal Vasko00cbf532020-06-15 13:58:47 +02001446 }
1447
Michal Vaskoe137fc42021-07-22 11:53:13 +02001448 if ((pref_len == 3) && !strncmp(prefix, "xml", 3)) {
1449 /* not a prefix but special name */
1450 name = prefix;
1451 name_len += 1 + pref_len;
1452 prefix = NULL;
1453 pref_len = 0;
1454 }
1455
Michal Vasko51d21b82020-11-13 18:03:54 +01001456 /* get the module */
1457 if (module_name) {
1458 mod_len = strlen(module_name);
1459 } else {
1460 module_name = prefix;
1461 mod_len = pref_len;
1462 }
1463
Michal Vasko00cbf532020-06-15 13:58:47 +02001464 /* set value if none */
Michal Vasko8d65f852021-02-17 11:28:13 +01001465 if (!value) {
1466 value = "";
Michal Vasko00cbf532020-06-15 13:58:47 +02001467 }
1468
Michal Vasko8d65f852021-02-17 11:28:13 +01001469 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 +02001470 strlen(value), NULL, LY_VALUE_JSON, NULL, LYD_HINT_DATA));
Michal Vasko8d65f852021-02-17 11:28:13 +01001471
1472 if (attr) {
1473 *attr = ret;
1474 }
1475 return LY_SUCCESS;
1476}
1477
Jan Kundrátc6e39de2021-12-09 16:01:19 +01001478LIBYANG_API_DEF LY_ERR
Michal Vasko8d65f852021-02-17 11:28:13 +01001479lyd_new_attr2(struct lyd_node *parent, const char *module_ns, const char *name, const char *value,
1480 struct lyd_attr **attr)
1481{
1482 struct lyd_attr *ret = NULL;
1483 const struct ly_ctx *ctx;
1484 const char *prefix, *tmp;
1485 size_t pref_len, name_len;
1486
1487 LY_CHECK_ARG_RET(NULL, parent, !parent->schema, name, LY_EINVAL);
1488
1489 ctx = LYD_CTX(parent);
1490
1491 /* parse the name */
1492 tmp = name;
1493 if (ly_parse_nodeid(&tmp, &prefix, &pref_len, &name, &name_len) || tmp[0]) {
1494 LOGERR(ctx, LY_EINVAL, "Attribute name \"%s\" is not valid.", name);
1495 return LY_EVALID;
1496 }
1497
Michal Vaskoe137fc42021-07-22 11:53:13 +02001498 if ((pref_len == 3) && !strncmp(prefix, "xml", 3)) {
1499 /* not a prefix but special name */
1500 name = prefix;
1501 name_len += 1 + pref_len;
1502 prefix = NULL;
1503 pref_len = 0;
1504 }
1505
Michal Vasko8d65f852021-02-17 11:28:13 +01001506 /* set value if none */
1507 if (!value) {
1508 value = "";
1509 }
1510
1511 LY_CHECK_RET(lyd_create_attr(parent, &ret, ctx, name, name_len, prefix, pref_len, module_ns,
Radek Krejci8df109d2021-04-23 12:19:08 +02001512 module_ns ? strlen(module_ns) : 0, value, strlen(value), NULL, LY_VALUE_XML, NULL, LYD_HINT_DATA));
Michal Vasko3a41dff2020-07-15 14:30:28 +02001513
1514 if (attr) {
1515 *attr = ret;
1516 }
1517 return LY_SUCCESS;
Michal Vasko00cbf532020-06-15 13:58:47 +02001518}
1519
Radek Krejci09c77442021-04-26 11:10:34 +02001520/**
1521 * @brief Change the value of a term (leaf or leaf-list) node.
1522 *
1523 * Node changed this way is always considered explicitly set, meaning its default flag
1524 * is always cleared.
1525 *
1526 * @param[in] term Term node to change.
1527 * @param[in] value New value to set.
1528 * @param[in] value_len Length of @p value.
1529 * @param[in] format Format of @p value.
1530 * @return LY_SUCCESS if value was changed,
1531 * @return LY_EEXIST if value was the same and only the default flag was cleared,
1532 * @return LY_ENOT if the values were equal and no change occured,
1533 * @return LY_ERR value on other errors.
1534 */
1535static LY_ERR
1536_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 +02001537{
1538 LY_ERR ret = LY_SUCCESS;
1539 struct lysc_type *type;
1540 struct lyd_node_term *t;
1541 struct lyd_node *parent;
1542 struct lyd_value val = {0};
Radek Krejci857189e2020-09-01 13:26:36 +02001543 ly_bool dflt_change, val_change;
Michal Vasko00cbf532020-06-15 13:58:47 +02001544
Radek Krejci09c77442021-04-26 11:10:34 +02001545 assert(term && term->schema && (term->schema->nodetype & LYD_NODE_TERM));
Michal Vasko00cbf532020-06-15 13:58:47 +02001546
Michal Vasko00cbf532020-06-15 13:58:47 +02001547 t = (struct lyd_node_term *)term;
1548 type = ((struct lysc_node_leaf *)term->schema)->type;
1549
1550 /* parse the new value */
Radek Krejciddace2c2021-01-08 11:30:56 +01001551 LOG_LOCSET(term->schema, term, NULL, NULL);
Radek Krejci09c77442021-04-26 11:10:34 +02001552 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 +01001553 LOG_LOCBACK(term->schema ? 1 : 0, 1, 0, 0);
Michal Vaskofeca4fb2020-10-05 08:58:40 +02001554 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko00cbf532020-06-15 13:58:47 +02001555
1556 /* compare original and new value */
1557 if (type->plugin->compare(&t->value, &val)) {
1558 /* values differ, switch them */
Michal Vaskob7be7a82020-08-20 09:09:04 +02001559 type->plugin->free(LYD_CTX(term), &t->value);
Michal Vasko00cbf532020-06-15 13:58:47 +02001560 t->value = val;
1561 memset(&val, 0, sizeof val);
1562 val_change = 1;
1563 } else {
1564 val_change = 0;
1565 }
1566
1567 /* always clear the default flag */
1568 if (term->flags & LYD_DEFAULT) {
Michal Vasko9e685082021-01-29 14:49:09 +01001569 for (parent = term; parent; parent = lyd_parent(parent)) {
Michal Vasko00cbf532020-06-15 13:58:47 +02001570 parent->flags &= ~LYD_DEFAULT;
1571 }
1572 dflt_change = 1;
1573 } else {
1574 dflt_change = 0;
1575 }
1576
1577 if (val_change || dflt_change) {
1578 /* make the node non-validated */
1579 term->flags &= LYD_NEW;
1580 }
1581
1582 if (val_change) {
1583 if (term->schema->nodetype == LYS_LEAFLIST) {
1584 /* leaf-list needs to be hashed again and re-inserted into parent */
1585 lyd_unlink_hash(term);
1586 lyd_hash(term);
1587 LY_CHECK_GOTO(ret = lyd_insert_hash(term), cleanup);
1588 } else if ((term->schema->flags & LYS_KEY) && term->parent) {
1589 /* list needs to be updated if its key was changed */
1590 assert(term->parent->schema->nodetype == LYS_LIST);
Michal Vasko9e685082021-01-29 14:49:09 +01001591 lyd_unlink_hash(lyd_parent(term));
1592 lyd_hash(lyd_parent(term));
1593 LY_CHECK_GOTO(ret = lyd_insert_hash(lyd_parent(term)), cleanup);
Michal Vasko00cbf532020-06-15 13:58:47 +02001594 } /* else leaf that is not a key, its value is not used for its hash so it does not change */
1595 }
1596
1597 /* retrun value */
1598 if (!val_change) {
1599 if (dflt_change) {
1600 /* only default flag change */
1601 ret = LY_EEXIST;
1602 } else {
1603 /* no change */
1604 ret = LY_ENOT;
1605 }
1606 } /* else value changed, LY_SUCCESS */
1607
1608cleanup:
Michal Vasko59512fc2020-12-09 18:13:29 +01001609 if (val.realtype) {
1610 type->plugin->free(LYD_CTX(term), &val);
1611 }
Michal Vasko00cbf532020-06-15 13:58:47 +02001612 return ret;
1613}
1614
Jan Kundrátc6e39de2021-12-09 16:01:19 +01001615LIBYANG_API_DEF LY_ERR
Radek Krejci09c77442021-04-26 11:10:34 +02001616lyd_change_term(struct lyd_node *term, const char *val_str)
1617{
1618 LY_CHECK_ARG_RET(NULL, term, term->schema, term->schema->nodetype & LYD_NODE_TERM, LY_EINVAL);
1619
1620 return _lyd_change_term(term, val_str, val_str ? strlen(val_str) : 0, LY_VALUE_JSON);
1621}
1622
Jan Kundrátc6e39de2021-12-09 16:01:19 +01001623LIBYANG_API_DEF LY_ERR
Radek Krejci09c77442021-04-26 11:10:34 +02001624lyd_change_term_bin(struct lyd_node *term, const void *value, size_t value_len)
1625{
1626 LY_CHECK_ARG_RET(NULL, term, term->schema, term->schema->nodetype & LYD_NODE_TERM, LY_EINVAL);
1627
1628 return _lyd_change_term(term, value, value_len, LY_VALUE_LYB);
1629}
1630
Jan Kundrátc6e39de2021-12-09 16:01:19 +01001631LIBYANG_API_DEF LY_ERR
Radek Krejci09c77442021-04-26 11:10:34 +02001632lyd_change_term_canon(struct lyd_node *term, const char *val_str)
1633{
1634 LY_CHECK_ARG_RET(NULL, term, term->schema, term->schema->nodetype & LYD_NODE_TERM, LY_EINVAL);
1635
1636 return _lyd_change_term(term, val_str, val_str ? strlen(val_str) : 0, LY_VALUE_CANON);
1637}
1638
Jan Kundrátc6e39de2021-12-09 16:01:19 +01001639LIBYANG_API_DEF LY_ERR
Michal Vasko41586352020-07-13 13:54:25 +02001640lyd_change_meta(struct lyd_meta *meta, const char *val_str)
1641{
1642 LY_ERR ret = LY_SUCCESS;
Radek Krejci2b18bf12020-11-06 11:20:20 +01001643 struct lyd_meta *m2 = NULL;
Michal Vasko41586352020-07-13 13:54:25 +02001644 struct lyd_value val;
Radek Krejci857189e2020-09-01 13:26:36 +02001645 ly_bool val_change;
Michal Vasko41586352020-07-13 13:54:25 +02001646
1647 LY_CHECK_ARG_RET(NULL, meta, LY_EINVAL);
1648
1649 if (!val_str) {
1650 val_str = "";
1651 }
1652
1653 /* parse the new value into a new meta structure */
Michal Vaskoaac267d2022-01-17 13:34:48 +01001654 ret = lyd_create_meta(NULL, &m2, meta->annotation->module, meta->name, strlen(meta->name), val_str, strlen(val_str),
1655 NULL, LY_VALUE_JSON, NULL, LYD_HINT_DATA, meta->parent ? meta->parent->schema : NULL, 0, NULL);
1656 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko41586352020-07-13 13:54:25 +02001657
1658 /* compare original and new value */
1659 if (lyd_compare_meta(meta, m2)) {
1660 /* values differ, switch them */
1661 val = meta->value;
1662 meta->value = m2->value;
1663 m2->value = val;
1664 val_change = 1;
1665 } else {
1666 val_change = 0;
1667 }
1668
1669 /* retrun value */
1670 if (!val_change) {
1671 /* no change */
1672 ret = LY_ENOT;
1673 } /* else value changed, LY_SUCCESS */
1674
1675cleanup:
Michal Vasko1a66a5d2020-11-18 18:15:37 +01001676 lyd_free_meta_single(m2);
Michal Vasko41586352020-07-13 13:54:25 +02001677 return ret;
1678}
1679
Radek Krejci09c77442021-04-26 11:10:34 +02001680/**
1681 * @brief Update node value.
1682 *
1683 * @param[in] node Node to update.
1684 * @param[in] value New value to set.
1685 * @param[in] value_len Length of @p value.
1686 * @param[in] value_type Type of @p value for anydata/anyxml node.
1687 * @param[in] format Format of @p value.
1688 * @param[out] new_parent Set to @p node if the value was updated, otherwise set to NULL.
1689 * @param[out] new_node Set to @p node if the value was updated, otherwise set to NULL.
1690 * @return LY_ERR value.
1691 */
Michal Vasko6741dc62021-02-04 09:27:45 +01001692static LY_ERR
Radek Krejci09c77442021-04-26 11:10:34 +02001693lyd_new_path_update(struct lyd_node *node, const void *value, size_t value_len, LYD_ANYDATA_VALUETYPE value_type,
1694 LY_VALUE_FORMAT format, struct lyd_node **new_parent, struct lyd_node **new_node)
1695{
1696 LY_ERR ret = LY_SUCCESS;
1697 struct lyd_node *new_any;
1698
1699 switch (node->schema->nodetype) {
1700 case LYS_CONTAINER:
1701 case LYS_NOTIF:
1702 case LYS_RPC:
1703 case LYS_ACTION:
1704 case LYS_LIST:
1705 /* if it exists, there is nothing to update */
1706 *new_parent = NULL;
1707 *new_node = NULL;
1708 break;
1709 case LYS_LEAFLIST:
1710 if (!lysc_is_dup_inst_list(node->schema)) {
1711 /* if it exists, there is nothing to update */
1712 *new_parent = NULL;
1713 *new_node = NULL;
1714 break;
1715 }
1716 /* fallthrough */
1717 case LYS_LEAF:
1718 ret = _lyd_change_term(node, value, value_len, format);
1719 if ((ret == LY_SUCCESS) || (ret == LY_EEXIST)) {
1720 /* there was an actual change (at least of the default flag) */
1721 *new_parent = node;
1722 *new_node = node;
1723 ret = LY_SUCCESS;
1724 } else if (ret == LY_ENOT) {
1725 /* no change */
1726 *new_parent = NULL;
1727 *new_node = NULL;
1728 ret = LY_SUCCESS;
1729 } /* else error */
1730 break;
1731 case LYS_ANYDATA:
1732 case LYS_ANYXML:
1733 /* create a new any node */
1734 LY_CHECK_RET(lyd_create_any(node->schema, value, value_type, 0, &new_any));
1735
1736 /* compare with the existing one */
1737 if (lyd_compare_single(node, new_any, 0)) {
1738 /* not equal, switch values (so that we can use generic node free) */
1739 ((struct lyd_node_any *)new_any)->value = ((struct lyd_node_any *)node)->value;
1740 ((struct lyd_node_any *)new_any)->value_type = ((struct lyd_node_any *)node)->value_type;
1741 ((struct lyd_node_any *)node)->value.str = value;
1742 ((struct lyd_node_any *)node)->value_type = value_type;
1743
1744 *new_parent = node;
1745 *new_node = node;
1746 } else {
1747 /* they are equal */
1748 *new_parent = NULL;
1749 *new_node = NULL;
1750 }
1751 lyd_free_tree(new_any);
1752 break;
1753 default:
1754 LOGINT(LYD_CTX(node));
1755 ret = LY_EINT;
1756 break;
1757 }
1758
1759 return ret;
1760}
1761
1762static LY_ERR
1763lyd_new_path_check_find_lypath(struct ly_path *path, const char *str_path, const char *value, size_t value_len,
1764 LY_VALUE_FORMAT format, uint32_t options)
Michal Vasko6741dc62021-02-04 09:27:45 +01001765{
Michal Vaskoe78faec2021-04-08 17:24:43 +02001766 LY_ERR r;
Michal Vasko6741dc62021-02-04 09:27:45 +01001767 struct ly_path_predicate *pred;
Michal Vasko04726242022-01-10 09:30:08 +01001768 struct lyd_value val;
Michal Vaskoe78faec2021-04-08 17:24:43 +02001769 const struct lysc_node *schema = NULL;
1770 LY_ARRAY_COUNT_TYPE u, new_count;
1771 int create = 0;
Michal Vasko6741dc62021-02-04 09:27:45 +01001772
1773 assert(path);
1774
1775 /* go through all the compiled nodes */
1776 LY_ARRAY_FOR(path, u) {
1777 schema = path[u].node;
Michal Vaskoe78faec2021-04-08 17:24:43 +02001778
1779 if (lysc_is_dup_inst_list(schema)) {
1780 if (path[u].pred_type == LY_PATH_PREDTYPE_NONE) {
1781 /* creating a new key-less list or state leaf-list instance */
1782 create = 1;
1783 new_count = u;
1784 } else if (path[u].pred_type != LY_PATH_PREDTYPE_POSITION) {
1785 LOG_LOCSET(schema, NULL, NULL, NULL);
1786 LOGVAL(schema->module->ctx, LYVE_XPATH, "Invalid predicate for %s \"%s\" in path \"%s\".",
1787 lys_nodetype2str(schema->nodetype), schema->name, str_path);
1788 LOG_LOCBACK(1, 0, 0, 0);
1789 return LY_EINVAL;
1790 }
1791 } else if ((schema->nodetype == LYS_LIST) && (path[u].pred_type != LY_PATH_PREDTYPE_LIST)) {
1792 if ((u < LY_ARRAY_COUNT(path) - 1) || !(options & LYD_NEW_PATH_OPAQ)) {
Michal Vasko6741dc62021-02-04 09:27:45 +01001793 LOG_LOCSET(schema, NULL, NULL, NULL);
1794 LOGVAL(schema->module->ctx, LYVE_XPATH, "Predicate missing for %s \"%s\" in path \"%s\".",
1795 lys_nodetype2str(schema->nodetype), schema->name, str_path);
1796 LOG_LOCBACK(1, 0, 0, 0);
1797 return LY_EINVAL;
Michal Vaskoe78faec2021-04-08 17:24:43 +02001798 } /* else creating an opaque list */
1799 } else if ((schema->nodetype == LYS_LEAFLIST) && (path[u].pred_type != LY_PATH_PREDTYPE_LEAFLIST)) {
Michal Vaskoe78faec2021-04-08 17:24:43 +02001800 r = LY_SUCCESS;
1801 if (options & LYD_NEW_PATH_OPAQ) {
Radek Krejci09c77442021-04-26 11:10:34 +02001802 r = lyd_value_validate(NULL, schema, value, value_len, NULL, NULL, NULL);
Michal Vaskoe78faec2021-04-08 17:24:43 +02001803 }
1804 if (!r) {
Michal Vasko04726242022-01-10 09:30:08 +01001805 /* try to store the value */
1806 LY_CHECK_RET(lyd_value_store(schema->module->ctx, &val, ((struct lysc_node_leaflist *)schema)->type,
1807 value, value_len, NULL, format, NULL, LYD_HINT_DATA, schema, NULL));
1808 ++((struct lysc_type *)val.realtype)->refcount;
1809
Michal Vaskoe78faec2021-04-08 17:24:43 +02001810 /* store the new predicate so that it is used when searching for this instance */
1811 path[u].pred_type = LY_PATH_PREDTYPE_LEAFLIST;
1812 LY_ARRAY_NEW_RET(schema->module->ctx, path[u].predicates, pred, LY_EMEM);
Michal Vasko04726242022-01-10 09:30:08 +01001813 pred->value = val;
Michal Vaskoe78faec2021-04-08 17:24:43 +02001814 } /* else we have opaq flag and the value is not valid, leave no predicate and then create an opaque node */
1815 }
Michal Vasko6741dc62021-02-04 09:27:45 +01001816 }
1817
Michal Vaskoe78faec2021-04-08 17:24:43 +02001818 if (create) {
1819 /* hide the nodes that should always be created so they are not found */
1820 while (new_count < LY_ARRAY_COUNT(path)) {
1821 LY_ARRAY_DECREMENT(path);
1822 }
Michal Vasko6741dc62021-02-04 09:27:45 +01001823 }
1824
Michal Vaskoe78faec2021-04-08 17:24:43 +02001825 return LY_SUCCESS;
Michal Vasko6741dc62021-02-04 09:27:45 +01001826}
1827
Radek Krejci95ccd1b2021-03-12 14:57:22 +01001828/**
1829 * @brief Create a new node in the data tree based on a path. All node types can be created.
1830 *
Michal Vaskoa918a632021-07-02 11:53:36 +02001831 * If @p path points to a list key, the key value from the predicate is used and @p value is ignored.
1832 * Also, if a leaf-list is being created and both a predicate is defined in @p path
Radek Krejci95ccd1b2021-03-12 14:57:22 +01001833 * and @p value is set, the predicate is preferred.
1834 *
1835 * For key-less lists and state leaf-lists, positional predicates can be used. If no preciate is used for these
1836 * nodes, they are always created.
1837 *
1838 * @param[in] parent Data parent to add to/modify, can be NULL. Note that in case a first top-level sibling is used,
1839 * it may no longer be first if @p path is absolute and starts with a non-existing top-level node inserted
1840 * before @p parent. Use ::lyd_first_sibling() to adjust @p parent in these cases.
1841 * @param[in] ctx libyang context, must be set if @p parent is NULL.
1842 * @param[in] ext Extension instance where the node being created is defined. This argument takes effect only for absolute
1843 * path or when the relative paths touches document root (top-level). In such cases the present extension instance replaces
1844 * searching for the appropriate module.
1845 * @param[in] path [Path](@ref howtoXPath) to create.
Radek Krejci09c77442021-04-26 11:10:34 +02001846 * @param[in] value Value of the new leaf/leaf-list (const char *) in ::LY_VALUE_JSON format. If creating an
1847 * anyxml/anydata node, the expected type depends on @p value_type. For other node types, it should be NULL.
1848 * @param[in] value_len Length of @p value in bytes. May be 0 if @p value is a zero-terminated string. Ignored when
1849 * creating anyxml/anydata nodes.
Radek Krejci95ccd1b2021-03-12 14:57:22 +01001850 * @param[in] value_type Anyxml/anydata node @p value type.
1851 * @param[in] options Bitmask of options, see @ref pathoptions.
1852 * @param[out] new_parent Optional first parent node created. If only one node was created, equals to @p new_node.
1853 * @param[out] new_node Optional last node created.
1854 * @return LY_ERR value.
1855 */
1856static LY_ERR
1857lyd_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 +02001858 const void *value, size_t value_len, LYD_ANYDATA_VALUETYPE value_type, uint32_t options,
Radek Krejci95ccd1b2021-03-12 14:57:22 +01001859 struct lyd_node **new_parent, struct lyd_node **new_node)
Michal Vasko00cbf532020-06-15 13:58:47 +02001860{
1861 LY_ERR ret = LY_SUCCESS, r;
1862 struct lyxp_expr *exp = NULL;
1863 struct ly_path *p = NULL;
1864 struct lyd_node *nparent = NULL, *nnode = NULL, *node = NULL, *cur_parent;
1865 const struct lysc_node *schema;
Michal Vaskoe78faec2021-04-08 17:24:43 +02001866 const struct lyd_value *val = NULL;
Radek Krejci910d0132021-03-05 14:19:31 +01001867 LY_ARRAY_COUNT_TYPE path_idx = 0, orig_count = 0;
Radek Krejci09c77442021-04-26 11:10:34 +02001868 LY_VALUE_FORMAT format;
Michal Vasko00cbf532020-06-15 13:58:47 +02001869
Radek Krejci95ccd1b2021-03-12 14:57:22 +01001870 assert(parent || ctx);
Radek Krejci09c77442021-04-26 11:10:34 +02001871 assert(path && ((path[0] == '/') || parent));
1872 assert(!(options & LYD_NEW_PATH_BIN_VALUE) || !(options & LYD_NEW_PATH_CANON_VALUE));
Michal Vasko00cbf532020-06-15 13:58:47 +02001873
1874 if (!ctx) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02001875 ctx = LYD_CTX(parent);
Michal Vasko00cbf532020-06-15 13:58:47 +02001876 }
Radek Krejci09c77442021-04-26 11:10:34 +02001877 if (value && !value_len) {
1878 value_len = strlen(value);
1879 }
1880 if (options & LYD_NEW_PATH_BIN_VALUE) {
1881 format = LY_VALUE_LYB;
1882 } else if (options & LYD_NEW_PATH_CANON_VALUE) {
1883 format = LY_VALUE_CANON;
1884 } else {
1885 format = LY_VALUE_JSON;
1886 }
Michal Vasko00cbf532020-06-15 13:58:47 +02001887
1888 /* parse path */
Michal Vaskoed725d72021-06-23 12:03:45 +02001889 LY_CHECK_GOTO(ret = ly_path_parse(ctx, NULL, path, strlen(path), 0, LY_PATH_BEGIN_EITHER, LY_PATH_PREFIX_OPTIONAL,
1890 LY_PATH_PRED_SIMPLE, &exp), cleanup);
Michal Vasko00cbf532020-06-15 13:58:47 +02001891
1892 /* compile path */
Michal Vasko106f0862021-11-02 11:49:27 +01001893 LY_CHECK_GOTO(ret = ly_path_compile(ctx, NULL, lyd_node_schema(parent), ext, exp, options & LYD_NEW_PATH_OUTPUT ?
1894 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 +02001895
Michal Vasko6741dc62021-02-04 09:27:45 +01001896 /* check the compiled path before searching existing nodes, it may be shortened */
1897 orig_count = LY_ARRAY_COUNT(p);
Radek Krejci09c77442021-04-26 11:10:34 +02001898 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 +02001899
1900 /* try to find any existing nodes in the path */
1901 if (parent) {
1902 ret = ly_path_eval_partial(p, parent, &path_idx, &node);
1903 if (ret == LY_SUCCESS) {
Michal Vasko6741dc62021-02-04 09:27:45 +01001904 if (orig_count == LY_ARRAY_COUNT(p)) {
1905 /* the node exists, are we supposed to update it or is it just a default? */
1906 if (!(options & LYD_NEW_PATH_UPDATE) && !(node->flags & LYD_DEFAULT)) {
1907 LOG_LOCSET(NULL, node, NULL, NULL);
1908 LOGVAL(ctx, LYVE_REFERENCE, "Path \"%s\" already exists", path);
1909 LOG_LOCBACK(0, 1, 0, 0);
1910 ret = LY_EEXIST;
1911 goto cleanup;
1912 }
Michal Vasko00cbf532020-06-15 13:58:47 +02001913
Michal Vasko6741dc62021-02-04 09:27:45 +01001914 /* update the existing node */
Radek Krejci09c77442021-04-26 11:10:34 +02001915 ret = lyd_new_path_update(node, value, value_len, value_type, format, &nparent, &nnode);
Michal Vasko6741dc62021-02-04 09:27:45 +01001916 goto cleanup;
1917 } /* else we were not searching for the whole path */
Michal Vasko00cbf532020-06-15 13:58:47 +02001918 } else if (ret == LY_EINCOMPLETE) {
1919 /* some nodes were found, adjust the iterator to the next segment */
1920 ++path_idx;
1921 } else if (ret == LY_ENOTFOUND) {
1922 /* 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 +01001923 if (lysc_data_parent(p[0].node)) {
Michal Vasko00cbf532020-06-15 13:58:47 +02001924 node = parent;
1925 }
1926 } else {
1927 /* error */
1928 goto cleanup;
1929 }
1930 }
1931
Michal Vasko6741dc62021-02-04 09:27:45 +01001932 /* restore the full path for creating new nodes */
1933 while (orig_count > LY_ARRAY_COUNT(p)) {
1934 LY_ARRAY_INCREMENT(p);
1935 }
1936
Michal Vasko00cbf532020-06-15 13:58:47 +02001937 /* create all the non-existing nodes in a loop */
Michal Vaskod989ba02020-08-24 10:59:24 +02001938 for ( ; path_idx < LY_ARRAY_COUNT(p); ++path_idx) {
Michal Vasko00cbf532020-06-15 13:58:47 +02001939 cur_parent = node;
1940 schema = p[path_idx].node;
1941
1942 switch (schema->nodetype) {
1943 case LYS_LIST:
Michal Vaskoe78faec2021-04-08 17:24:43 +02001944 if (lysc_is_dup_inst_list(schema)) {
1945 /* create key-less list instance */
1946 LY_CHECK_GOTO(ret = lyd_create_inner(schema, &node), cleanup);
1947 } else if ((options & LYD_NEW_PATH_OPAQ) && (p[path_idx].pred_type == LY_PATH_PREDTYPE_NONE)) {
1948 /* creating opaque list without keys */
1949 LY_CHECK_GOTO(ret = lyd_create_opaq(ctx, schema->name, strlen(schema->name), NULL, 0,
Radek Krejci8df109d2021-04-23 12:19:08 +02001950 schema->module->name, strlen(schema->module->name), NULL, 0, NULL, LY_VALUE_JSON, NULL,
Michal Vaskoe78faec2021-04-08 17:24:43 +02001951 LYD_NODEHINT_LIST, &node), cleanup);
1952 } else {
1953 /* create standard list instance */
1954 assert(p[path_idx].pred_type == LY_PATH_PREDTYPE_LIST);
1955 LY_CHECK_GOTO(ret = lyd_create_list(schema, p[path_idx].predicates, &node), cleanup);
Michal Vasko00cbf532020-06-15 13:58:47 +02001956 }
Michal Vaskoe78faec2021-04-08 17:24:43 +02001957 break;
Michal Vasko00cbf532020-06-15 13:58:47 +02001958 case LYS_CONTAINER:
1959 case LYS_NOTIF:
1960 case LYS_RPC:
1961 case LYS_ACTION:
1962 LY_CHECK_GOTO(ret = lyd_create_inner(schema, &node), cleanup);
1963 break;
1964 case LYS_LEAFLIST:
Michal Vaskoe78faec2021-04-08 17:24:43 +02001965 if ((options & LYD_NEW_PATH_OPAQ) && (p[path_idx].pred_type != LY_PATH_PREDTYPE_LEAFLIST)) {
1966 /* we have not checked this only for dup-inst lists, otherwise it must be opaque */
1967 r = LY_EVALID;
1968 if (lysc_is_dup_inst_list(schema)) {
1969 /* validate value */
Radek Krejci09c77442021-04-26 11:10:34 +02001970 r = lyd_value_validate(NULL, schema, value ? value : "", value_len, NULL, NULL, NULL);
Michal Vaskoe78faec2021-04-08 17:24:43 +02001971 }
1972 if (r && (r != LY_EINCOMPLETE)) {
1973 /* creating opaque leaf-list */
Radek Krejci09c77442021-04-26 11:10:34 +02001974 LY_CHECK_GOTO(ret = lyd_create_opaq(ctx, schema->name, strlen(schema->name), value, value_len,
1975 schema->module->name, strlen(schema->module->name), NULL, 0, NULL, format, NULL,
1976 LYD_NODEHINT_LEAFLIST, &node), cleanup);
Michal Vaskoe78faec2021-04-08 17:24:43 +02001977 break;
1978 }
1979 }
1980
1981 /* get value to set */
1982 if (p[path_idx].pred_type == LY_PATH_PREDTYPE_LEAFLIST) {
1983 val = &p[path_idx].predicates[0].value;
Michal Vaskoe78faec2021-04-08 17:24:43 +02001984 }
1985
1986 /* create a leaf-list instance */
1987 if (val) {
Michal Vasko00cbf532020-06-15 13:58:47 +02001988 LY_CHECK_GOTO(ret = lyd_create_term2(schema, &p[path_idx].predicates[0].value, &node), cleanup);
Michal Vaskoe78faec2021-04-08 17:24:43 +02001989 } else {
Radek Krejci09c77442021-04-26 11:10:34 +02001990 LY_CHECK_GOTO(ret = lyd_create_term(schema, value, value_len, NULL, format, NULL, LYD_HINT_DATA,
1991 NULL, &node), cleanup);
Michal Vasko00cbf532020-06-15 13:58:47 +02001992 }
1993 break;
1994 case LYS_LEAF:
Michal Vasko106f0862021-11-02 11:49:27 +01001995 if (lysc_is_key(schema) && cur_parent->schema) {
Michal Vasko35880332020-12-08 13:08:12 +01001996 /* it must have been already created or some error will occur later */
Michal Vaskoa918a632021-07-02 11:53:36 +02001997 lyd_find_sibling_schema(lyd_child(cur_parent), schema, &node);
1998 assert(node);
Michal Vasko35880332020-12-08 13:08:12 +01001999 goto next_iter;
2000 }
2001
Michal Vaskoe78faec2021-04-08 17:24:43 +02002002 if (options & LYD_NEW_PATH_OPAQ) {
Michal Vasko106f0862021-11-02 11:49:27 +01002003 if (cur_parent && !cur_parent->schema) {
2004 /* always create opaque nodes for opaque parents */
2005 r = LY_ENOT;
2006 } else {
2007 /* validate value */
2008 r = lyd_value_validate(NULL, schema, value ? value : "", value_len, NULL, NULL, NULL);
2009 }
Michal Vaskoe78faec2021-04-08 17:24:43 +02002010 if (r && (r != LY_EINCOMPLETE)) {
2011 /* creating opaque leaf */
Radek Krejci09c77442021-04-26 11:10:34 +02002012 LY_CHECK_GOTO(ret = lyd_create_opaq(ctx, schema->name, strlen(schema->name), value, value_len,
2013 schema->module->name, strlen(schema->module->name), NULL, 0, NULL, format, NULL, 0, &node),
2014 cleanup);
Michal Vaskoe78faec2021-04-08 17:24:43 +02002015 break;
2016 }
2017 }
2018
Michal Vaskoe78faec2021-04-08 17:24:43 +02002019 /* create a leaf instance */
Radek Krejci09c77442021-04-26 11:10:34 +02002020 LY_CHECK_GOTO(ret = lyd_create_term(schema, value, value_len, NULL, format, NULL, LYD_HINT_DATA, NULL,
2021 &node), cleanup);
Michal Vasko00cbf532020-06-15 13:58:47 +02002022 break;
2023 case LYS_ANYDATA:
2024 case LYS_ANYXML:
Michal Vasko366a4a12020-12-04 16:23:57 +01002025 LY_CHECK_GOTO(ret = lyd_create_any(schema, value, value_type, 0, &node), cleanup);
Michal Vasko00cbf532020-06-15 13:58:47 +02002026 break;
2027 default:
2028 LOGINT(ctx);
2029 ret = LY_EINT;
2030 goto cleanup;
2031 }
2032
2033 if (cur_parent) {
2034 /* connect to the parent */
Michal Vasko6ee6f432021-07-16 09:49:14 +02002035 lyd_insert_node(cur_parent, NULL, node, 0);
Michal Vasko00cbf532020-06-15 13:58:47 +02002036 } else if (parent) {
2037 /* connect to top-level siblings */
Michal Vasko6ee6f432021-07-16 09:49:14 +02002038 lyd_insert_node(NULL, &parent, node, 0);
Michal Vasko00cbf532020-06-15 13:58:47 +02002039 }
2040
Michal Vasko35880332020-12-08 13:08:12 +01002041next_iter:
Michal Vasko00cbf532020-06-15 13:58:47 +02002042 /* update remembered nodes */
2043 if (!nparent) {
2044 nparent = node;
2045 }
2046 nnode = node;
2047 }
2048
2049cleanup:
2050 lyxp_expr_free(ctx, exp);
Michal Vasko6741dc62021-02-04 09:27:45 +01002051 if (p) {
2052 while (orig_count > LY_ARRAY_COUNT(p)) {
2053 LY_ARRAY_INCREMENT(p);
2054 }
2055 }
Michal Vasko00cbf532020-06-15 13:58:47 +02002056 ly_path_free(ctx, p);
2057 if (!ret) {
2058 /* set out params only on success */
2059 if (new_parent) {
2060 *new_parent = nparent;
2061 }
2062 if (new_node) {
2063 *new_node = nnode;
2064 }
Michal Vaskof4b95ba2020-12-11 08:42:33 +01002065 } else {
2066 lyd_free_tree(nparent);
Michal Vasko00cbf532020-06-15 13:58:47 +02002067 }
2068 return ret;
2069}
2070
Jan Kundrátc6e39de2021-12-09 16:01:19 +01002071LIBYANG_API_DEF LY_ERR
Radek Krejci95ccd1b2021-03-12 14:57:22 +01002072lyd_new_path(struct lyd_node *parent, const struct ly_ctx *ctx, const char *path, const char *value, uint32_t options,
2073 struct lyd_node **node)
2074{
Radek Krejci09c77442021-04-26 11:10:34 +02002075 LY_CHECK_ARG_RET(ctx, parent || ctx, path, (path[0] == '/') || parent,
2076 !(options & LYD_NEW_PATH_BIN_VALUE) || !(options & LYD_NEW_PATH_CANON_VALUE), LY_EINVAL);
Michal Vasko1d80a532021-11-24 10:41:05 +01002077 LY_CHECK_CTX_EQUAL_RET(parent ? LYD_CTX(parent) : NULL, ctx, LY_EINVAL);
Radek Krejci09c77442021-04-26 11:10:34 +02002078
2079 return lyd_new_path_(parent, ctx, NULL, path, value, 0, LYD_ANYDATA_STRING, options, node, NULL);
Radek Krejci95ccd1b2021-03-12 14:57:22 +01002080}
2081
Jan Kundrátc6e39de2021-12-09 16:01:19 +01002082LIBYANG_API_DEF LY_ERR
Radek Krejci95ccd1b2021-03-12 14:57:22 +01002083lyd_new_path2(struct lyd_node *parent, const struct ly_ctx *ctx, const char *path, const void *value,
Radek Krejci09c77442021-04-26 11:10:34 +02002084 size_t value_len, LYD_ANYDATA_VALUETYPE value_type, uint32_t options, struct lyd_node **new_parent,
2085 struct lyd_node **new_node)
Radek Krejci95ccd1b2021-03-12 14:57:22 +01002086{
Radek Krejci09c77442021-04-26 11:10:34 +02002087 LY_CHECK_ARG_RET(ctx, parent || ctx, path, (path[0] == '/') || parent,
2088 !(options & LYD_NEW_PATH_BIN_VALUE) || !(options & LYD_NEW_PATH_CANON_VALUE), LY_EINVAL);
Michal Vasko1d80a532021-11-24 10:41:05 +01002089 LY_CHECK_CTX_EQUAL_RET(parent ? LYD_CTX(parent) : NULL, ctx, LY_EINVAL);
2090
Radek Krejci09c77442021-04-26 11:10:34 +02002091 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 +01002092}
2093
Jan Kundrátc6e39de2021-12-09 16:01:19 +01002094LIBYANG_API_DEF LY_ERR
Radek Krejci95ccd1b2021-03-12 14:57:22 +01002095lyd_new_ext_path(struct lyd_node *parent, const struct lysc_ext_instance *ext, const char *path, const void *value,
2096 uint32_t options, struct lyd_node **node)
2097{
2098 const struct ly_ctx *ctx = ext ? ext->module->ctx : NULL;
2099
Radek Krejci09c77442021-04-26 11:10:34 +02002100 LY_CHECK_ARG_RET(ctx, ext, path, (path[0] == '/') || parent,
2101 !(options & LYD_NEW_PATH_BIN_VALUE) || !(options & LYD_NEW_PATH_CANON_VALUE), LY_EINVAL);
Michal Vasko1d80a532021-11-24 10:41:05 +01002102 LY_CHECK_CTX_EQUAL_RET(parent ? LYD_CTX(parent) : NULL, ctx, LY_EINVAL);
2103
Radek Krejci09c77442021-04-26 11:10:34 +02002104 return lyd_new_path_(parent, ctx, ext, path, value, 0, LYD_ANYDATA_STRING, options, node, NULL);
Radek Krejci95ccd1b2021-03-12 14:57:22 +01002105}
2106
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002107LY_ERR
2108lyd_new_implicit_r(struct lyd_node *parent, struct lyd_node **first, const struct lysc_node *sparent,
Michal Vaskoaac267d2022-01-17 13:34:48 +01002109 const struct lys_module *mod, struct ly_set *node_when, struct ly_set *node_types, uint32_t impl_opts,
2110 struct lyd_node **diff)
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002111{
2112 LY_ERR ret;
Michal Vaskod1e53b92021-01-28 13:11:06 +01002113 const struct lysc_node *iter = NULL;
Radek Krejci2b18bf12020-11-06 11:20:20 +01002114 struct lyd_node *node = NULL;
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002115 struct lyd_value **dflts;
2116 LY_ARRAY_COUNT_TYPE u;
Michal Vasko630d9892020-12-08 17:11:08 +01002117 uint32_t getnext_opts;
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002118
2119 assert(first && (parent || sparent || mod));
2120
2121 if (!sparent && parent) {
2122 sparent = parent->schema;
2123 }
2124
Michal Vasko630d9892020-12-08 17:11:08 +01002125 getnext_opts = LYS_GETNEXT_WITHCHOICE;
2126 if (impl_opts & LYD_IMPLICIT_OUTPUT) {
2127 getnext_opts |= LYS_GETNEXT_OUTPUT;
2128 }
2129
2130 while ((iter = lys_getnext(iter, sparent, mod ? mod->compiled : NULL, getnext_opts))) {
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002131 if ((impl_opts & LYD_IMPLICIT_NO_STATE) && (iter->flags & LYS_CONFIG_R)) {
2132 continue;
Michal Vasko44b19a12020-08-07 09:21:30 +02002133 } else if ((impl_opts & LYD_IMPLICIT_NO_CONFIG) && (iter->flags & LYS_CONFIG_W)) {
2134 continue;
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002135 }
2136
2137 switch (iter->nodetype) {
2138 case LYS_CHOICE:
Michal Vaskobcf4d7d2020-11-18 18:16:49 +01002139 node = lys_getnext_data(NULL, *first, NULL, iter, NULL);
2140 if (!node && ((struct lysc_node_choice *)iter)->dflt) {
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002141 /* create default case data */
Michal Vasko14ed9cd2021-01-28 14:16:25 +01002142 LY_CHECK_RET(lyd_new_implicit_r(parent, first, &((struct lysc_node_choice *)iter)->dflt->node,
Michal Vaskoaac267d2022-01-17 13:34:48 +01002143 NULL, node_when, node_types, impl_opts, diff));
Michal Vaskobcf4d7d2020-11-18 18:16:49 +01002144 } else if (node) {
2145 /* create any default data in the existing case */
2146 assert(node->schema->parent->nodetype == LYS_CASE);
Michal Vaskoaac267d2022-01-17 13:34:48 +01002147 LY_CHECK_RET(lyd_new_implicit_r(parent, first, node->schema->parent, NULL, node_when, node_types,
Michal Vaskobcf4d7d2020-11-18 18:16:49 +01002148 impl_opts, diff));
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002149 }
2150 break;
2151 case LYS_CONTAINER:
2152 if (!(iter->flags & LYS_PRESENCE) && lyd_find_sibling_val(*first, iter, NULL, 0, NULL)) {
2153 /* create default NP container */
2154 LY_CHECK_RET(lyd_create_inner(iter, &node));
Michal Vaskof4d67ea2021-03-31 13:53:21 +02002155 node->flags = LYD_DEFAULT | (lysc_has_when(iter) ? LYD_WHEN_TRUE : 0);
Michal Vasko6ee6f432021-07-16 09:49:14 +02002156 lyd_insert_node(parent, first, node, 0);
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002157
Michal Vaskof4d67ea2021-03-31 13:53:21 +02002158 if (lysc_has_when(iter) && node_when) {
Michal Vaskoe16c7b72021-02-26 10:39:06 +01002159 /* remember to resolve when */
2160 LY_CHECK_RET(ly_set_add(node_when, node, 1, NULL));
2161 }
Michal Vaskoe49b6322020-11-05 17:38:36 +01002162 if (diff) {
2163 /* add into diff */
2164 LY_CHECK_RET(lyd_val_diff_add(node, LYD_DIFF_OP_CREATE, diff));
2165 }
2166
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002167 /* create any default children */
Michal Vaskoaac267d2022-01-17 13:34:48 +01002168 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 +02002169 impl_opts, diff));
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002170 }
2171 break;
2172 case LYS_LEAF:
Michal Vasko69730152020-10-09 16:30:07 +02002173 if (!(impl_opts & LYD_IMPLICIT_NO_DEFAULTS) && ((struct lysc_node_leaf *)iter)->dflt &&
2174 lyd_find_sibling_val(*first, iter, NULL, 0, NULL)) {
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002175 /* create default leaf */
2176 ret = lyd_create_term2(iter, ((struct lysc_node_leaf *)iter)->dflt, &node);
2177 if (ret == LY_EINCOMPLETE) {
2178 if (node_types) {
2179 /* remember to resolve type */
Radek Krejci3d92e442020-10-12 12:48:13 +02002180 LY_CHECK_RET(ly_set_add(node_types, node, 1, NULL));
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002181 }
2182 } else if (ret) {
2183 return ret;
2184 }
Michal Vaskof4d67ea2021-03-31 13:53:21 +02002185 node->flags = LYD_DEFAULT | (lysc_has_when(iter) ? LYD_WHEN_TRUE : 0);
Michal Vasko6ee6f432021-07-16 09:49:14 +02002186 lyd_insert_node(parent, first, node, 0);
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002187
Michal Vaskof4d67ea2021-03-31 13:53:21 +02002188 if (lysc_has_when(iter) && node_when) {
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002189 /* remember to resolve when */
Radek Krejci3d92e442020-10-12 12:48:13 +02002190 LY_CHECK_RET(ly_set_add(node_when, node, 1, NULL));
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002191 }
2192 if (diff) {
2193 /* add into diff */
2194 LY_CHECK_RET(lyd_val_diff_add(node, LYD_DIFF_OP_CREATE, diff));
2195 }
2196 }
2197 break;
2198 case LYS_LEAFLIST:
Michal Vasko69730152020-10-09 16:30:07 +02002199 if (!(impl_opts & LYD_IMPLICIT_NO_DEFAULTS) && ((struct lysc_node_leaflist *)iter)->dflts &&
2200 lyd_find_sibling_val(*first, iter, NULL, 0, NULL)) {
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002201 /* create all default leaf-lists */
2202 dflts = ((struct lysc_node_leaflist *)iter)->dflts;
2203 LY_ARRAY_FOR(dflts, u) {
2204 ret = lyd_create_term2(iter, dflts[u], &node);
2205 if (ret == LY_EINCOMPLETE) {
2206 if (node_types) {
2207 /* remember to resolve type */
Radek Krejci3d92e442020-10-12 12:48:13 +02002208 LY_CHECK_RET(ly_set_add(node_types, node, 1, NULL));
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002209 }
2210 } else if (ret) {
2211 return ret;
2212 }
Michal Vaskof4d67ea2021-03-31 13:53:21 +02002213 node->flags = LYD_DEFAULT | (lysc_has_when(iter) ? LYD_WHEN_TRUE : 0);
Michal Vasko6ee6f432021-07-16 09:49:14 +02002214 lyd_insert_node(parent, first, node, 0);
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002215
Michal Vaskof4d67ea2021-03-31 13:53:21 +02002216 if (lysc_has_when(iter) && node_when) {
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002217 /* remember to resolve when */
Radek Krejci3d92e442020-10-12 12:48:13 +02002218 LY_CHECK_RET(ly_set_add(node_when, node, 1, NULL));
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002219 }
2220 if (diff) {
2221 /* add into diff */
2222 LY_CHECK_RET(lyd_val_diff_add(node, LYD_DIFF_OP_CREATE, diff));
2223 }
2224 }
2225 }
2226 break;
2227 default:
2228 /* without defaults */
2229 break;
2230 }
2231 }
2232
2233 return LY_SUCCESS;
2234}
2235
Jan Kundrátc6e39de2021-12-09 16:01:19 +01002236LIBYANG_API_DEF LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02002237lyd_new_implicit_tree(struct lyd_node *tree, uint32_t implicit_options, struct lyd_node **diff)
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002238{
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002239 LY_ERR ret = LY_SUCCESS;
Michal Vasko3488ada2020-12-03 14:18:19 +01002240 struct lyd_node *node;
Michal Vaskoaac267d2022-01-17 13:34:48 +01002241 struct ly_set node_when = {0};
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002242
2243 LY_CHECK_ARG_RET(NULL, tree, LY_EINVAL);
2244 if (diff) {
2245 *diff = NULL;
2246 }
2247
Michal Vasko56daf732020-08-10 10:57:18 +02002248 LYD_TREE_DFS_BEGIN(tree, node) {
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002249 /* skip added default nodes */
Michal Vasko69730152020-10-09 16:30:07 +02002250 if (((node->flags & (LYD_DEFAULT | LYD_NEW)) != (LYD_DEFAULT | LYD_NEW)) &&
2251 (node->schema->nodetype & LYD_NODE_INNER)) {
Michal Vaskoaac267d2022-01-17 13:34:48 +01002252 LY_CHECK_GOTO(ret = lyd_new_implicit_r(node, lyd_node_child_p(node), NULL, NULL, &node_when, NULL,
2253 implicit_options, diff), cleanup);
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002254 }
2255
Michal Vasko56daf732020-08-10 10:57:18 +02002256 LYD_TREE_DFS_END(tree, node);
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002257 }
2258
Michal Vasko3488ada2020-12-03 14:18:19 +01002259 /* resolve when and remove any invalid defaults */
Michal Vaskoaac267d2022-01-17 13:34:48 +01002260 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 +01002261
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002262cleanup:
Michal Vasko3488ada2020-12-03 14:18:19 +01002263 ly_set_erase(&node_when, NULL);
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002264 if (ret && diff) {
2265 lyd_free_all(*diff);
2266 *diff = NULL;
2267 }
2268 return ret;
2269}
2270
Jan Kundrátc6e39de2021-12-09 16:01:19 +01002271LIBYANG_API_DEF LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02002272lyd_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 +02002273{
2274 const struct lys_module *mod;
2275 struct lyd_node *d = NULL;
2276 uint32_t i = 0;
2277 LY_ERR ret = LY_SUCCESS;
2278
2279 LY_CHECK_ARG_RET(ctx, tree, *tree || ctx, LY_EINVAL);
Michal Vasko1d80a532021-11-24 10:41:05 +01002280 LY_CHECK_CTX_EQUAL_RET(*tree ? LYD_CTX(*tree) : NULL, ctx, LY_EINVAL);
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002281 if (diff) {
2282 *diff = NULL;
2283 }
2284 if (!ctx) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02002285 ctx = LYD_CTX(*tree);
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002286 }
2287
2288 /* add nodes for each module one-by-one */
2289 while ((mod = ly_ctx_get_module_iter(ctx, &i))) {
2290 if (!mod->implemented) {
2291 continue;
2292 }
2293
2294 LY_CHECK_GOTO(ret = lyd_new_implicit_module(tree, mod, implicit_options, diff ? &d : NULL), cleanup);
2295 if (d) {
2296 /* merge into one diff */
2297 lyd_insert_sibling(*diff, d, diff);
2298
2299 d = NULL;
2300 }
2301 }
2302
2303cleanup:
2304 if (ret && diff) {
2305 lyd_free_all(*diff);
2306 *diff = NULL;
2307 }
2308 return ret;
2309}
2310
Jan Kundrátc6e39de2021-12-09 16:01:19 +01002311LIBYANG_API_DEF LY_ERR
Michal Vasko1d80a532021-11-24 10:41:05 +01002312lyd_new_implicit_module(struct lyd_node **tree, const struct lys_module *module, uint32_t implicit_options,
2313 struct lyd_node **diff)
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002314{
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002315 LY_ERR ret = LY_SUCCESS;
Michal Vasko3488ada2020-12-03 14:18:19 +01002316 struct lyd_node *root, *d = NULL;
Michal Vaskoaac267d2022-01-17 13:34:48 +01002317 struct ly_set node_when = {0};
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002318
2319 LY_CHECK_ARG_RET(NULL, tree, module, LY_EINVAL);
Michal Vasko1d80a532021-11-24 10:41:05 +01002320 LY_CHECK_CTX_EQUAL_RET(*tree ? LYD_CTX(*tree) : NULL, module ? module->ctx : NULL, LY_EINVAL);
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002321 if (diff) {
2322 *diff = NULL;
2323 }
2324
2325 /* add all top-level defaults for this module */
Michal Vaskoaac267d2022-01-17 13:34:48 +01002326 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 +01002327
2328 /* resolve when and remove any invalid defaults */
Michal Vaskoaac267d2022-01-17 13:34:48 +01002329 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 +01002330 cleanup);
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002331
2332 /* process nested nodes */
2333 LY_LIST_FOR(*tree, root) {
2334 /* skip added default nodes */
2335 if ((root->flags & (LYD_DEFAULT | LYD_NEW)) != (LYD_DEFAULT | LYD_NEW)) {
2336 LY_CHECK_GOTO(ret = lyd_new_implicit_tree(root, implicit_options, diff ? &d : NULL), cleanup);
2337
2338 if (d) {
2339 /* merge into one diff */
2340 lyd_insert_sibling(*diff, d, diff);
2341
2342 d = NULL;
2343 }
2344 }
2345 }
2346
2347cleanup:
Michal Vasko3488ada2020-12-03 14:18:19 +01002348 ly_set_erase(&node_when, NULL);
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002349 if (ret && diff) {
2350 lyd_free_all(*diff);
2351 *diff = NULL;
2352 }
2353 return ret;
2354}
2355
Michal Vasko90932a92020-02-12 14:33:03 +01002356struct lyd_node *
Michal Vaskob104f112020-07-17 09:54:54 +02002357lyd_insert_get_next_anchor(const struct lyd_node *first_sibling, const struct lyd_node *new_node)
Michal Vasko90932a92020-02-12 14:33:03 +01002358{
Michal Vaskob104f112020-07-17 09:54:54 +02002359 const struct lysc_node *schema, *sparent;
Michal Vasko90932a92020-02-12 14:33:03 +01002360 struct lyd_node *match = NULL;
Radek Krejci857189e2020-09-01 13:26:36 +02002361 ly_bool found;
Michal Vasko93b16062020-12-09 18:14:18 +01002362 uint32_t getnext_opts;
Michal Vasko90932a92020-02-12 14:33:03 +01002363
Michal Vaskob104f112020-07-17 09:54:54 +02002364 assert(new_node);
2365
2366 if (!first_sibling || !new_node->schema) {
2367 /* insert at the end, no next anchor */
Michal Vasko90932a92020-02-12 14:33:03 +01002368 return NULL;
2369 }
2370
Michal Vaskoc08b9d22022-02-10 15:48:39 +01002371 assert(!first_sibling || (LYD_CTX(first_sibling) == LYD_CTX(new_node)));
2372
Michal Vasko93b16062020-12-09 18:14:18 +01002373 getnext_opts = 0;
Michal Vaskod1e53b92021-01-28 13:11:06 +01002374 if (new_node->schema->flags & LYS_IS_OUTPUT) {
Michal Vasko93b16062020-12-09 18:14:18 +01002375 getnext_opts = LYS_GETNEXT_OUTPUT;
2376 }
2377
Michal Vaskoa2756f02021-07-09 13:20:28 +02002378 if (first_sibling->parent && first_sibling->parent->schema && first_sibling->parent->children_ht) {
Michal Vaskob104f112020-07-17 09:54:54 +02002379 /* find the anchor using hashes */
2380 sparent = first_sibling->parent->schema;
Michal Vasko93b16062020-12-09 18:14:18 +01002381 schema = lys_getnext(new_node->schema, sparent, NULL, getnext_opts);
Michal Vaskob104f112020-07-17 09:54:54 +02002382 while (schema) {
2383 /* keep trying to find the first existing instance of the closest following schema sibling,
2384 * otherwise return NULL - inserting at the end */
2385 if (!lyd_find_sibling_schema(first_sibling, schema, &match)) {
2386 break;
2387 }
2388
Michal Vasko93b16062020-12-09 18:14:18 +01002389 schema = lys_getnext(schema, sparent, NULL, getnext_opts);
Michal Vaskob104f112020-07-17 09:54:54 +02002390 }
2391 } else {
2392 /* find the anchor without hashes */
2393 match = (struct lyd_node *)first_sibling;
Michal Vasko3a708622021-07-15 14:20:10 +02002394 sparent = lysc_data_parent(new_node->schema);
2395 if (!sparent) {
Michal Vaskob104f112020-07-17 09:54:54 +02002396 /* we are in top-level, skip all the data from preceding modules */
2397 LY_LIST_FOR(match, match) {
2398 if (!match->schema || (strcmp(lyd_owner_module(match)->name, lyd_owner_module(new_node)->name) >= 0)) {
2399 break;
2400 }
2401 }
2402 }
2403
2404 /* get the first schema sibling */
Michal Vasko93b16062020-12-09 18:14:18 +01002405 schema = lys_getnext(NULL, sparent, new_node->schema->module->compiled, getnext_opts);
Michal Vaskob104f112020-07-17 09:54:54 +02002406
2407 found = 0;
2408 LY_LIST_FOR(match, match) {
2409 if (!match->schema || (lyd_owner_module(match) != lyd_owner_module(new_node))) {
2410 /* we have found an opaque node, which must be at the end, so use it OR
2411 * modules do not match, so we must have traversed all the data from new_node module (if any),
2412 * we have found the first node of the next module, that is what we want */
2413 break;
2414 }
2415
2416 /* skip schema nodes until we find the instantiated one */
2417 while (!found) {
2418 if (new_node->schema == schema) {
2419 /* we have found the schema of the new node, continue search to find the first
2420 * data node with a different schema (after our schema) */
2421 found = 1;
2422 break;
2423 }
2424 if (match->schema == schema) {
2425 /* current node (match) is a data node still before the new node, continue search in data */
2426 break;
2427 }
Michal Vasko93b16062020-12-09 18:14:18 +01002428 schema = lys_getnext(schema, sparent, new_node->schema->module->compiled, getnext_opts);
Michal Vaskob104f112020-07-17 09:54:54 +02002429 assert(schema);
2430 }
2431
2432 if (found && (match->schema != new_node->schema)) {
2433 /* find the next node after we have found our node schema data instance */
2434 break;
2435 }
2436 }
Michal Vasko90932a92020-02-12 14:33:03 +01002437 }
2438
2439 return match;
2440}
2441
2442/**
2443 * @brief Insert node after a sibling.
2444 *
Michal Vasko9f96a052020-03-10 09:41:45 +01002445 * Handles inserting into NP containers and key-less lists.
2446 *
Michal Vasko90932a92020-02-12 14:33:03 +01002447 * @param[in] sibling Sibling to insert after.
2448 * @param[in] node Node to insert.
2449 */
2450static void
Michal Vaskof03ed032020-03-04 13:31:44 +01002451lyd_insert_after_node(struct lyd_node *sibling, struct lyd_node *node)
Michal Vasko90932a92020-02-12 14:33:03 +01002452{
Michal Vasko0249f7c2020-03-05 16:36:40 +01002453 struct lyd_node_inner *par;
2454
Michal Vasko90932a92020-02-12 14:33:03 +01002455 assert(!node->next && (node->prev == node));
2456
2457 node->next = sibling->next;
2458 node->prev = sibling;
2459 sibling->next = node;
2460 if (node->next) {
2461 /* sibling had a succeeding node */
2462 node->next->prev = node;
2463 } else {
2464 /* sibling was last, find first sibling and change its prev */
2465 if (sibling->parent) {
2466 sibling = sibling->parent->child;
2467 } else {
Michal Vaskod989ba02020-08-24 10:59:24 +02002468 for ( ; sibling->prev->next != node; sibling = sibling->prev) {}
Michal Vasko90932a92020-02-12 14:33:03 +01002469 }
2470 sibling->prev = node;
2471 }
2472 node->parent = sibling->parent;
Michal Vasko0249f7c2020-03-05 16:36:40 +01002473
Michal Vasko9f96a052020-03-10 09:41:45 +01002474 for (par = node->parent; par; par = par->parent) {
2475 if ((par->flags & LYD_DEFAULT) && !(node->flags & LYD_DEFAULT)) {
2476 /* remove default flags from NP containers */
Michal Vasko0249f7c2020-03-05 16:36:40 +01002477 par->flags &= ~LYD_DEFAULT;
2478 }
2479 }
Michal Vasko90932a92020-02-12 14:33:03 +01002480}
2481
2482/**
2483 * @brief Insert node before a sibling.
2484 *
Michal Vasko9f96a052020-03-10 09:41:45 +01002485 * Handles inserting into NP containers and key-less lists.
2486 *
Michal Vasko90932a92020-02-12 14:33:03 +01002487 * @param[in] sibling Sibling to insert before.
2488 * @param[in] node Node to insert.
2489 */
2490static void
Michal Vaskof03ed032020-03-04 13:31:44 +01002491lyd_insert_before_node(struct lyd_node *sibling, struct lyd_node *node)
Michal Vasko90932a92020-02-12 14:33:03 +01002492{
Michal Vasko0249f7c2020-03-05 16:36:40 +01002493 struct lyd_node_inner *par;
2494
Michal Vasko90932a92020-02-12 14:33:03 +01002495 assert(!node->next && (node->prev == node));
2496
2497 node->next = sibling;
2498 /* covers situation of sibling being first */
2499 node->prev = sibling->prev;
2500 sibling->prev = node;
2501 if (node->prev->next) {
2502 /* sibling had a preceding node */
2503 node->prev->next = node;
2504 } else if (sibling->parent) {
2505 /* sibling was first and we must also change parent child pointer */
2506 sibling->parent->child = node;
2507 }
2508 node->parent = sibling->parent;
Michal Vasko0249f7c2020-03-05 16:36:40 +01002509
Michal Vasko9f96a052020-03-10 09:41:45 +01002510 for (par = node->parent; par; par = par->parent) {
2511 if ((par->flags & LYD_DEFAULT) && !(node->flags & LYD_DEFAULT)) {
2512 /* remove default flags from NP containers */
Michal Vasko0249f7c2020-03-05 16:36:40 +01002513 par->flags &= ~LYD_DEFAULT;
2514 }
2515 }
Michal Vasko90932a92020-02-12 14:33:03 +01002516}
2517
2518/**
Michal Vaskob104f112020-07-17 09:54:54 +02002519 * @brief Insert node as the first and only child of a parent.
Michal Vasko90932a92020-02-12 14:33:03 +01002520 *
Michal Vasko9f96a052020-03-10 09:41:45 +01002521 * Handles inserting into NP containers and key-less lists.
2522 *
Michal Vasko90932a92020-02-12 14:33:03 +01002523 * @param[in] parent Parent to insert into.
2524 * @param[in] node Node to insert.
2525 */
2526static void
Michal Vaskob104f112020-07-17 09:54:54 +02002527lyd_insert_only_child(struct lyd_node *parent, struct lyd_node *node)
Michal Vasko90932a92020-02-12 14:33:03 +01002528{
2529 struct lyd_node_inner *par;
2530
Radek Krejcia1c1e542020-09-29 16:06:52 +02002531 assert(parent && !lyd_child(parent) && !node->next && (node->prev == node));
Michal Vasko52927e22020-03-16 17:26:14 +01002532 assert(!parent->schema || (parent->schema->nodetype & LYD_NODE_INNER));
Michal Vasko90932a92020-02-12 14:33:03 +01002533
2534 par = (struct lyd_node_inner *)parent;
2535
Michal Vaskob104f112020-07-17 09:54:54 +02002536 par->child = node;
Michal Vasko90932a92020-02-12 14:33:03 +01002537 node->parent = par;
Michal Vasko0249f7c2020-03-05 16:36:40 +01002538
Michal Vaskod989ba02020-08-24 10:59:24 +02002539 for ( ; par; par = par->parent) {
Michal Vasko9f96a052020-03-10 09:41:45 +01002540 if ((par->flags & LYD_DEFAULT) && !(node->flags & LYD_DEFAULT)) {
2541 /* remove default flags from NP containers */
Michal Vasko0249f7c2020-03-05 16:36:40 +01002542 par->flags &= ~LYD_DEFAULT;
2543 }
2544 }
Michal Vasko751cb4d2020-07-14 12:25:28 +02002545}
Michal Vasko0249f7c2020-03-05 16:36:40 +01002546
Michal Vasko751cb4d2020-07-14 12:25:28 +02002547/**
2548 * @brief Learn whether a list instance has all the keys.
2549 *
2550 * @param[in] list List instance to check.
2551 * @return non-zero if all the keys were found,
2552 * @return 0 otherwise.
2553 */
2554static int
2555lyd_insert_has_keys(const struct lyd_node *list)
2556{
2557 const struct lyd_node *key;
2558 const struct lysc_node *skey = NULL;
2559
2560 assert(list->schema->nodetype == LYS_LIST);
Radek Krejcia1c1e542020-09-29 16:06:52 +02002561 key = lyd_child(list);
Michal Vasko751cb4d2020-07-14 12:25:28 +02002562 while ((skey = lys_getnext(skey, list->schema, NULL, 0)) && (skey->flags & LYS_KEY)) {
2563 if (!key || (key->schema != skey)) {
2564 /* key missing */
2565 return 0;
2566 }
2567
2568 key = key->next;
2569 }
2570
2571 /* all keys found */
2572 return 1;
Michal Vasko90932a92020-02-12 14:33:03 +01002573}
2574
2575void
Michal Vasko6ee6f432021-07-16 09:49:14 +02002576lyd_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 +01002577{
Michal Vaskob104f112020-07-17 09:54:54 +02002578 struct lyd_node *anchor, *first_sibling;
Michal Vasko90932a92020-02-12 14:33:03 +01002579
Michal Vaskob104f112020-07-17 09:54:54 +02002580 /* inserting list without its keys is not supported */
2581 assert((parent || first_sibling_p) && node && (node->hash || !node->schema));
Michal Vaskof756c942020-11-06 17:21:37 +01002582 assert(!parent || !parent->schema ||
2583 (parent->schema->nodetype & (LYS_CONTAINER | LYS_LIST | LYS_RPC | LYS_ACTION | LYS_NOTIF)));
Michal Vasko9b368d32020-02-14 13:53:31 +01002584
Michal Vaskob104f112020-07-17 09:54:54 +02002585 if (!parent && first_sibling_p && (*first_sibling_p) && (*first_sibling_p)->parent) {
Michal Vasko9e685082021-01-29 14:49:09 +01002586 parent = lyd_parent(*first_sibling_p);
Michal Vasko9b368d32020-02-14 13:53:31 +01002587 }
Michal Vasko90932a92020-02-12 14:33:03 +01002588
Michal Vaskob104f112020-07-17 09:54:54 +02002589 /* get first sibling */
Michal Vasko9e685082021-01-29 14:49:09 +01002590 first_sibling = parent ? lyd_child(parent) : *first_sibling_p;
Michal Vasko9f96a052020-03-10 09:41:45 +01002591
Michal Vasko6ee6f432021-07-16 09:49:14 +02002592 if (last) {
2593 /* no next anchor */
2594 anchor = NULL;
2595 } else {
2596 /* find the anchor, our next node, so we can insert before it */
2597 anchor = lyd_insert_get_next_anchor(first_sibling, node);
2598 }
2599
Michal Vaskob104f112020-07-17 09:54:54 +02002600 if (anchor) {
Michal Vasko6ee6f432021-07-16 09:49:14 +02002601 /* insert before the anchor */
Michal Vaskob104f112020-07-17 09:54:54 +02002602 lyd_insert_before_node(anchor, node);
Michal Vasko26123192020-11-09 21:02:34 +01002603 if (!parent && (*first_sibling_p == anchor)) {
2604 /* move first sibling */
2605 *first_sibling_p = node;
2606 }
Michal Vaskob104f112020-07-17 09:54:54 +02002607 } else if (first_sibling) {
Michal Vasko6ee6f432021-07-16 09:49:14 +02002608 /* insert as the last node */
Michal Vaskob104f112020-07-17 09:54:54 +02002609 lyd_insert_after_node(first_sibling->prev, node);
2610 } else if (parent) {
Michal Vasko6ee6f432021-07-16 09:49:14 +02002611 /* insert as the only child */
Michal Vaskob104f112020-07-17 09:54:54 +02002612 lyd_insert_only_child(parent, node);
Michal Vasko90932a92020-02-12 14:33:03 +01002613 } else {
Michal Vasko6ee6f432021-07-16 09:49:14 +02002614 /* insert as the only sibling */
Michal Vaskob104f112020-07-17 09:54:54 +02002615 *first_sibling_p = node;
2616 }
2617
2618 /* insert into parent HT */
2619 lyd_insert_hash(node);
2620
2621 /* finish hashes for our parent, if needed and possible */
Michal Vasko9e8de2d2020-09-01 08:17:10 +02002622 if (node->schema && (node->schema->flags & LYS_KEY) && parent && lyd_insert_has_keys(parent)) {
Michal Vaskob104f112020-07-17 09:54:54 +02002623 lyd_hash(parent);
2624
2625 /* now we can insert even the list into its parent HT */
2626 lyd_insert_hash(parent);
Michal Vasko90932a92020-02-12 14:33:03 +01002627 }
Michal Vasko90932a92020-02-12 14:33:03 +01002628}
2629
Michal Vasko717a4c32020-12-07 09:40:10 +01002630/**
2631 * @brief Check schema place of a node to be inserted.
2632 *
2633 * @param[in] parent Schema node of the parent data node.
2634 * @param[in] sibling Schema node of a sibling data node.
2635 * @param[in] schema Schema node if the data node to be inserted.
2636 * @return LY_SUCCESS on success.
2637 * @return LY_EINVAL if the place is invalid.
2638 */
Michal Vaskof03ed032020-03-04 13:31:44 +01002639static LY_ERR
Michal Vasko717a4c32020-12-07 09:40:10 +01002640lyd_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 +01002641{
2642 const struct lysc_node *par2;
2643
Michal Vasko62ed12d2020-05-21 10:08:25 +02002644 assert(!parent || !(parent->nodetype & (LYS_CASE | LYS_CHOICE)));
Michal Vasko717a4c32020-12-07 09:40:10 +01002645 assert(!sibling || !(sibling->nodetype & (LYS_CASE | LYS_CHOICE)));
2646 assert(!schema || !(schema->nodetype & (LYS_CASE | LYS_CHOICE)));
Michal Vaskof03ed032020-03-04 13:31:44 +01002647
Michal Vasko717a4c32020-12-07 09:40:10 +01002648 if (!schema || (!parent && !sibling)) {
Michal Vasko71e795e2020-12-04 16:27:37 +01002649 /* opaque nodes can be inserted wherever */
2650 return LY_SUCCESS;
2651 }
2652
Michal Vasko717a4c32020-12-07 09:40:10 +01002653 if (!parent) {
2654 parent = lysc_data_parent(sibling);
2655 }
2656
Michal Vaskof03ed032020-03-04 13:31:44 +01002657 /* find schema parent */
Michal Vasko62ed12d2020-05-21 10:08:25 +02002658 par2 = lysc_data_parent(schema);
Michal Vaskof03ed032020-03-04 13:31:44 +01002659
2660 if (parent) {
2661 /* inner node */
2662 if (par2 != parent) {
Michal Vaskob104f112020-07-17 09:54:54 +02002663 LOGERR(schema->module->ctx, LY_EINVAL, "Cannot insert, parent of \"%s\" is not \"%s\".", schema->name,
Michal Vasko69730152020-10-09 16:30:07 +02002664 parent->name);
Michal Vaskof03ed032020-03-04 13:31:44 +01002665 return LY_EINVAL;
2666 }
2667 } else {
2668 /* top-level node */
2669 if (par2) {
Radek Krejcif6d14cb2020-07-02 16:11:45 +02002670 LOGERR(schema->module->ctx, LY_EINVAL, "Cannot insert, node \"%s\" is not top-level.", schema->name);
Michal Vaskof03ed032020-03-04 13:31:44 +01002671 return LY_EINVAL;
2672 }
2673 }
2674
2675 return LY_SUCCESS;
2676}
2677
Jan Kundrátc6e39de2021-12-09 16:01:19 +01002678LIBYANG_API_DEF LY_ERR
Michal Vaskob104f112020-07-17 09:54:54 +02002679lyd_insert_child(struct lyd_node *parent, struct lyd_node *node)
Michal Vaskof03ed032020-03-04 13:31:44 +01002680{
2681 struct lyd_node *iter;
2682
Michal Vasko0ab974d2021-02-24 13:18:26 +01002683 LY_CHECK_ARG_RET(NULL, parent, node, !parent->schema || (parent->schema->nodetype & LYD_NODE_INNER), LY_EINVAL);
Michal Vasko1d80a532021-11-24 10:41:05 +01002684 LY_CHECK_CTX_EQUAL_RET(LYD_CTX(parent), LYD_CTX(node), LY_EINVAL);
Michal Vaskof03ed032020-03-04 13:31:44 +01002685
Michal Vasko717a4c32020-12-07 09:40:10 +01002686 LY_CHECK_RET(lyd_insert_check_schema(parent->schema, NULL, node->schema));
Michal Vaskof03ed032020-03-04 13:31:44 +01002687
Michal Vasko0ab974d2021-02-24 13:18:26 +01002688 if (node->schema && (node->schema->flags & LYS_KEY)) {
Michal Vaskoaac267d2022-01-17 13:34:48 +01002689 LOGERR(LYD_CTX(parent), LY_EINVAL, "Cannot insert key \"%s\".", node->schema->name);
Michal Vaskof03ed032020-03-04 13:31:44 +01002690 return LY_EINVAL;
2691 }
2692
2693 if (node->parent || node->prev->next) {
2694 lyd_unlink_tree(node);
2695 }
2696
2697 while (node) {
2698 iter = node->next;
2699 lyd_unlink_tree(node);
Michal Vasko6ee6f432021-07-16 09:49:14 +02002700 lyd_insert_node(parent, NULL, node, 0);
Michal Vaskof03ed032020-03-04 13:31:44 +01002701 node = iter;
2702 }
2703 return LY_SUCCESS;
2704}
2705
Jan Kundrátc6e39de2021-12-09 16:01:19 +01002706LIBYANG_API_DEF LY_ERR
Michal Vaskoaac267d2022-01-17 13:34:48 +01002707lyd_insert_ext(struct lyd_node *parent, struct lyd_node *first)
2708{
2709 struct lyd_node *iter;
2710
2711 LY_CHECK_ARG_RET(NULL, parent, first, !first->parent, !first->prev->next,
2712 !parent->schema || (parent->schema->nodetype & LYD_NODE_INNER), LY_EINVAL);
2713
2714 if (first->schema && (first->schema->flags & LYS_KEY)) {
2715 LOGERR(LYD_CTX(parent), LY_EINVAL, "Cannot insert key \"%s\".", first->schema->name);
2716 return LY_EINVAL;
2717 }
2718
2719 while (first) {
2720 iter = first->next;
2721 lyd_unlink_tree(first);
Michal Vaskoc08b9d22022-02-10 15:48:39 +01002722 lyd_insert_node(parent, NULL, first, 1);
Michal Vaskoaac267d2022-01-17 13:34:48 +01002723 first = iter;
2724 }
2725 return LY_SUCCESS;
2726}
2727
2728LIBYANG_API_DEF LY_ERR
Michal Vaskob104f112020-07-17 09:54:54 +02002729lyd_insert_sibling(struct lyd_node *sibling, struct lyd_node *node, struct lyd_node **first)
Michal Vaskob1b5c262020-03-05 14:29:47 +01002730{
2731 struct lyd_node *iter;
2732
Michal Vaskob104f112020-07-17 09:54:54 +02002733 LY_CHECK_ARG_RET(NULL, node, LY_EINVAL);
Michal Vasko1d80a532021-11-24 10:41:05 +01002734 LY_CHECK_CTX_EQUAL_RET(sibling ? LYD_CTX(sibling) : NULL, LYD_CTX(node), LY_EINVAL);
Michal Vaskob1b5c262020-03-05 14:29:47 +01002735
Michal Vaskob104f112020-07-17 09:54:54 +02002736 if (sibling) {
Michal Vasko717a4c32020-12-07 09:40:10 +01002737 LY_CHECK_RET(lyd_insert_check_schema(NULL, sibling->schema, node->schema));
Michal Vaskob1b5c262020-03-05 14:29:47 +01002738 }
2739
Michal Vasko553d0b52020-12-04 16:27:52 +01002740 if (sibling == node) {
2741 /* we need to keep the connection to siblings so we can insert into them */
2742 sibling = sibling->prev;
2743 }
2744
Michal Vaskob1b5c262020-03-05 14:29:47 +01002745 if (node->parent || node->prev->next) {
2746 lyd_unlink_tree(node);
2747 }
2748
2749 while (node) {
Michal Vasko71e795e2020-12-04 16:27:37 +01002750 if (lysc_is_key(node->schema)) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02002751 LOGERR(LYD_CTX(node), LY_EINVAL, "Cannot insert key \"%s\".", node->schema->name);
Michal Vaskob104f112020-07-17 09:54:54 +02002752 return LY_EINVAL;
2753 }
2754
Michal Vaskob1b5c262020-03-05 14:29:47 +01002755 iter = node->next;
2756 lyd_unlink_tree(node);
Michal Vasko6ee6f432021-07-16 09:49:14 +02002757 lyd_insert_node(NULL, &sibling, node, 0);
Michal Vaskob1b5c262020-03-05 14:29:47 +01002758 node = iter;
2759 }
Michal Vaskob1b5c262020-03-05 14:29:47 +01002760
Michal Vaskob104f112020-07-17 09:54:54 +02002761 if (first) {
2762 /* find the first sibling */
2763 *first = sibling;
2764 while ((*first)->prev->next) {
2765 *first = (*first)->prev;
Michal Vasko0249f7c2020-03-05 16:36:40 +01002766 }
2767 }
2768
2769 return LY_SUCCESS;
2770}
2771
Jan Kundrátc6e39de2021-12-09 16:01:19 +01002772LIBYANG_API_DEF LY_ERR
Michal Vaskof03ed032020-03-04 13:31:44 +01002773lyd_insert_before(struct lyd_node *sibling, struct lyd_node *node)
2774{
Michal Vaskof03ed032020-03-04 13:31:44 +01002775 LY_CHECK_ARG_RET(NULL, sibling, node, LY_EINVAL);
Michal Vasko1d80a532021-11-24 10:41:05 +01002776 LY_CHECK_CTX_EQUAL_RET(LYD_CTX(sibling), LYD_CTX(node), LY_EINVAL);
Michal Vaskof03ed032020-03-04 13:31:44 +01002777
Michal Vasko717a4c32020-12-07 09:40:10 +01002778 LY_CHECK_RET(lyd_insert_check_schema(NULL, sibling->schema, node->schema));
Michal Vaskof03ed032020-03-04 13:31:44 +01002779
Michal Vaskob104f112020-07-17 09:54:54 +02002780 if (!(node->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) || !(node->schema->flags & LYS_ORDBY_USER)) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02002781 LOGERR(LYD_CTX(sibling), LY_EINVAL, "Can be used only for user-ordered nodes.");
Michal Vaskof03ed032020-03-04 13:31:44 +01002782 return LY_EINVAL;
2783 }
Michal Vasko131561d2022-02-22 14:13:10 +01002784 if (lysc_is_key(sibling->schema)) {
2785 LOGERR(LYD_CTX(sibling), LY_EINVAL, "Cannot insert before keys.");
2786 return LY_EINVAL;
2787 }
Michal Vaskof03ed032020-03-04 13:31:44 +01002788
Michal Vasko4bc2ce32020-12-08 10:06:16 +01002789 lyd_unlink_tree(node);
2790 lyd_insert_before_node(sibling, node);
2791 lyd_insert_hash(node);
Michal Vaskof03ed032020-03-04 13:31:44 +01002792
Michal Vaskof03ed032020-03-04 13:31:44 +01002793 return LY_SUCCESS;
2794}
2795
Jan Kundrátc6e39de2021-12-09 16:01:19 +01002796LIBYANG_API_DEF LY_ERR
Michal Vaskof03ed032020-03-04 13:31:44 +01002797lyd_insert_after(struct lyd_node *sibling, struct lyd_node *node)
2798{
Michal Vaskof03ed032020-03-04 13:31:44 +01002799 LY_CHECK_ARG_RET(NULL, sibling, node, LY_EINVAL);
Michal Vasko1d80a532021-11-24 10:41:05 +01002800 LY_CHECK_CTX_EQUAL_RET(LYD_CTX(sibling), LYD_CTX(node), LY_EINVAL);
Michal Vaskof03ed032020-03-04 13:31:44 +01002801
Michal Vasko717a4c32020-12-07 09:40:10 +01002802 LY_CHECK_RET(lyd_insert_check_schema(NULL, sibling->schema, node->schema));
Michal Vaskof03ed032020-03-04 13:31:44 +01002803
Michal Vaskob104f112020-07-17 09:54:54 +02002804 if (!(node->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) || !(node->schema->flags & LYS_ORDBY_USER)) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02002805 LOGERR(LYD_CTX(sibling), LY_EINVAL, "Can be used only for user-ordered nodes.");
Michal Vaskof03ed032020-03-04 13:31:44 +01002806 return LY_EINVAL;
2807 }
Michal Vasko131561d2022-02-22 14:13:10 +01002808 if (sibling->next && lysc_is_key(sibling->next->schema)) {
2809 LOGERR(LYD_CTX(sibling), LY_EINVAL, "Cannot insert before keys.");
2810 return LY_EINVAL;
2811 }
Michal Vaskof03ed032020-03-04 13:31:44 +01002812
Michal Vasko4bc2ce32020-12-08 10:06:16 +01002813 lyd_unlink_tree(node);
2814 lyd_insert_after_node(sibling, node);
2815 lyd_insert_hash(node);
Michal Vaskof03ed032020-03-04 13:31:44 +01002816
Michal Vaskof03ed032020-03-04 13:31:44 +01002817 return LY_SUCCESS;
2818}
2819
Jan Kundrátc6e39de2021-12-09 16:01:19 +01002820LIBYANG_API_DEF void
Michal Vasko66d508c2021-07-21 16:07:09 +02002821lyd_unlink_siblings(struct lyd_node *node)
2822{
2823 struct lyd_node *next, *elem, *first = NULL;
2824
2825 LY_LIST_FOR_SAFE(node, next, elem) {
2826 lyd_unlink_tree(elem);
2827 lyd_insert_node(NULL, &first, elem, 1);
2828 }
2829}
2830
Jan Kundrátc6e39de2021-12-09 16:01:19 +01002831LIBYANG_API_DEF void
Michal Vaskof03ed032020-03-04 13:31:44 +01002832lyd_unlink_tree(struct lyd_node *node)
2833{
2834 struct lyd_node *iter;
2835
2836 if (!node) {
2837 return;
2838 }
2839
Michal Vaskob104f112020-07-17 09:54:54 +02002840 /* update hashes while still linked into the tree */
2841 lyd_unlink_hash(node);
2842
Michal Vaskof03ed032020-03-04 13:31:44 +01002843 /* unlink from siblings */
2844 if (node->prev->next) {
2845 node->prev->next = node->next;
2846 }
2847 if (node->next) {
2848 node->next->prev = node->prev;
2849 } else {
2850 /* unlinking the last node */
2851 if (node->parent) {
2852 iter = node->parent->child;
2853 } else {
2854 iter = node->prev;
2855 while (iter->prev != node) {
2856 iter = iter->prev;
2857 }
2858 }
2859 /* update the "last" pointer from the first node */
2860 iter->prev = node->prev;
2861 }
2862
2863 /* unlink from parent */
2864 if (node->parent) {
2865 if (node->parent->child == node) {
2866 /* the node is the first child */
2867 node->parent->child = node->next;
2868 }
2869
Michal Vaskoab49dbe2020-07-17 12:32:47 +02002870 /* check for NP container whether its last non-default node is not being unlinked */
Michal Vasko69730152020-10-09 16:30:07 +02002871 if (node->parent->schema && (node->parent->schema->nodetype == LYS_CONTAINER) &&
2872 !(node->parent->flags & LYD_DEFAULT) && !(node->parent->schema->flags & LYS_PRESENCE)) {
Michal Vaskoab49dbe2020-07-17 12:32:47 +02002873 LY_LIST_FOR(node->parent->child, iter) {
2874 if ((iter != node) && !(iter->flags & LYD_DEFAULT)) {
2875 break;
2876 }
2877 }
2878 if (!iter) {
2879 node->parent->flags |= LYD_DEFAULT;
2880 }
2881 }
2882
Michal Vaskof03ed032020-03-04 13:31:44 +01002883 node->parent = NULL;
2884 }
2885
2886 node->next = NULL;
2887 node->prev = node;
2888}
2889
Michal Vaskoa5da3292020-08-12 13:10:50 +02002890void
Michal Vasko871a0252020-11-11 18:35:24 +01002891lyd_insert_meta(struct lyd_node *parent, struct lyd_meta *meta, ly_bool clear_dflt)
Radek Krejci1798aae2020-07-14 13:26:06 +02002892{
2893 struct lyd_meta *last, *iter;
2894
2895 assert(parent);
Michal Vaskoa5da3292020-08-12 13:10:50 +02002896
2897 if (!meta) {
2898 return;
2899 }
Radek Krejci1798aae2020-07-14 13:26:06 +02002900
2901 for (iter = meta; iter; iter = iter->next) {
2902 iter->parent = parent;
2903 }
2904
2905 /* insert as the last attribute */
2906 if (parent->meta) {
Radek Krejci1e008d22020-08-17 11:37:37 +02002907 for (last = parent->meta; last->next; last = last->next) {}
Radek Krejci1798aae2020-07-14 13:26:06 +02002908 last->next = meta;
2909 } else {
2910 parent->meta = meta;
2911 }
2912
2913 /* remove default flags from NP containers */
Michal Vasko871a0252020-11-11 18:35:24 +01002914 while (clear_dflt && parent && (parent->schema->nodetype == LYS_CONTAINER) && (parent->flags & LYD_DEFAULT)) {
Radek Krejci1798aae2020-07-14 13:26:06 +02002915 parent->flags &= ~LYD_DEFAULT;
Michal Vasko9e685082021-01-29 14:49:09 +01002916 parent = lyd_parent(parent);
Radek Krejci1798aae2020-07-14 13:26:06 +02002917 }
Radek Krejci1798aae2020-07-14 13:26:06 +02002918}
2919
2920LY_ERR
Michal Vasko9f96a052020-03-10 09:41:45 +01002921lyd_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 +02002922 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 +01002923 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 +01002924{
Radek Krejci2efc45b2020-12-22 16:25:44 +01002925 LY_ERR ret = LY_SUCCESS;
Michal Vasko90932a92020-02-12 14:33:03 +01002926 struct lysc_ext_instance *ant = NULL;
Michal Vaskob0534f92021-07-01 13:24:03 +02002927 const struct lysc_type **ant_type;
Michal Vasko9f96a052020-03-10 09:41:45 +01002928 struct lyd_meta *mt, *last;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002929 LY_ARRAY_COUNT_TYPE u;
Michal Vasko90932a92020-02-12 14:33:03 +01002930
Michal Vasko9f96a052020-03-10 09:41:45 +01002931 assert((parent || meta) && mod);
Michal Vasko6f4cbb62020-02-28 11:15:47 +01002932
Michal Vaskoaac267d2022-01-17 13:34:48 +01002933 LOG_LOCSET(ctx_node, parent, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +01002934
Radek Krejci7eb54ba2020-05-18 16:30:04 +02002935 LY_ARRAY_FOR(mod->compiled->exts, u) {
Radek Krejci3e6632f2021-03-22 22:08:21 +01002936 if ((mod->compiled->exts[u].def->plugin == lyplg_find(LYPLG_EXTENSION, LYEXT_PLUGIN_INTERNAL_ANNOTATION)) &&
Radek Krejci7eb54ba2020-05-18 16:30:04 +02002937 !ly_strncmp(mod->compiled->exts[u].argument, name, name_len)) {
Michal Vasko90932a92020-02-12 14:33:03 +01002938 /* we have the annotation definition */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02002939 ant = &mod->compiled->exts[u];
Michal Vasko90932a92020-02-12 14:33:03 +01002940 break;
2941 }
2942 }
2943 if (!ant) {
2944 /* attribute is not defined as a metadata annotation (RFC 7952) */
Radek Krejci2efc45b2020-12-22 16:25:44 +01002945 LOGVAL(mod->ctx, LYVE_REFERENCE, "Annotation definition for attribute \"%s:%.*s\" not found.",
Radek Krejci422afb12021-03-04 16:38:16 +01002946 mod->name, (int)name_len, name);
Radek Krejci2efc45b2020-12-22 16:25:44 +01002947 ret = LY_EINVAL;
2948 goto cleanup;
Michal Vasko90932a92020-02-12 14:33:03 +01002949 }
2950
Michal Vasko9f96a052020-03-10 09:41:45 +01002951 mt = calloc(1, sizeof *mt);
Radek Krejci2efc45b2020-12-22 16:25:44 +01002952 LY_CHECK_ERR_GOTO(!mt, LOGMEM(mod->ctx); ret = LY_EMEM, cleanup);
Michal Vasko9f96a052020-03-10 09:41:45 +01002953 mt->parent = parent;
2954 mt->annotation = ant;
Michal Vaskob0534f92021-07-01 13:24:03 +02002955 ant_type = ant->substmts[ANNOTATION_SUBSTMT_TYPE].storage;
2956 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 +01002957 ctx_node, incomplete);
Radek Krejci2efc45b2020-12-22 16:25:44 +01002958 LY_CHECK_ERR_GOTO(ret, free(mt), cleanup);
2959 ret = lydict_insert(mod->ctx, name, name_len, &mt->name);
2960 LY_CHECK_ERR_GOTO(ret, free(mt), cleanup);
Michal Vasko90932a92020-02-12 14:33:03 +01002961
Michal Vasko6f4cbb62020-02-28 11:15:47 +01002962 /* insert as the last attribute */
2963 if (parent) {
Michal Vasko871a0252020-11-11 18:35:24 +01002964 lyd_insert_meta(parent, mt, clear_dflt);
Michal Vasko9f96a052020-03-10 09:41:45 +01002965 } else if (*meta) {
Radek Krejci1e008d22020-08-17 11:37:37 +02002966 for (last = *meta; last->next; last = last->next) {}
Michal Vasko9f96a052020-03-10 09:41:45 +01002967 last->next = mt;
Michal Vasko90932a92020-02-12 14:33:03 +01002968 }
2969
Michal Vasko9f96a052020-03-10 09:41:45 +01002970 if (meta) {
2971 *meta = mt;
Michal Vasko90932a92020-02-12 14:33:03 +01002972 }
Radek Krejci2efc45b2020-12-22 16:25:44 +01002973
2974cleanup:
Michal Vaskoaac267d2022-01-17 13:34:48 +01002975 LOG_LOCBACK(ctx_node ? 1 : 0, parent ? 1 : 0, 0, 0);
Radek Krejci2efc45b2020-12-22 16:25:44 +01002976 return ret;
Michal Vasko90932a92020-02-12 14:33:03 +01002977}
2978
Michal Vaskoa5da3292020-08-12 13:10:50 +02002979void
2980lyd_insert_attr(struct lyd_node *parent, struct lyd_attr *attr)
2981{
2982 struct lyd_attr *last, *iter;
2983 struct lyd_node_opaq *opaq;
2984
2985 assert(parent && !parent->schema);
2986
2987 if (!attr) {
2988 return;
2989 }
2990
2991 opaq = (struct lyd_node_opaq *)parent;
2992 for (iter = attr; iter; iter = iter->next) {
2993 iter->parent = opaq;
2994 }
2995
2996 /* insert as the last attribute */
2997 if (opaq->attr) {
Radek Krejci1e008d22020-08-17 11:37:37 +02002998 for (last = opaq->attr; last->next; last = last->next) {}
Michal Vaskoa5da3292020-08-12 13:10:50 +02002999 last->next = attr;
3000 } else {
3001 opaq->attr = attr;
3002 }
3003}
3004
Michal Vasko52927e22020-03-16 17:26:14 +01003005LY_ERR
Michal Vaskofeca4fb2020-10-05 08:58:40 +02003006lyd_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 +01003007 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 +02003008 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 +01003009{
Radek Krejci011e4aa2020-09-04 15:22:31 +02003010 LY_ERR ret = LY_SUCCESS;
Radek Krejci1798aae2020-07-14 13:26:06 +02003011 struct lyd_attr *at, *last;
Michal Vasko52927e22020-03-16 17:26:14 +01003012
3013 assert(ctx && (parent || attr) && (!parent || !parent->schema));
Michal Vaskofeca4fb2020-10-05 08:58:40 +02003014 assert(name && name_len && format);
Michal Vasko52927e22020-03-16 17:26:14 +01003015
Michal Vasko2a3722d2021-06-16 11:52:39 +02003016 if (!value_len && (!dynamic || !*dynamic)) {
Michal Vasko52927e22020-03-16 17:26:14 +01003017 value = "";
3018 }
3019
3020 at = calloc(1, sizeof *at);
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01003021 LY_CHECK_ERR_RET(!at, LOGMEM(ctx); ly_free_prefix_data(format, val_prefix_data), LY_EMEM);
Radek Krejcid46e46a2020-09-15 14:22:42 +02003022
Michal Vaskoad92b672020-11-12 13:11:31 +01003023 LY_CHECK_GOTO(ret = lydict_insert(ctx, name, name_len, &at->name.name), finish);
Michal Vasko501af032020-11-11 20:27:44 +01003024 if (prefix_len) {
Michal Vaskoad92b672020-11-12 13:11:31 +01003025 LY_CHECK_GOTO(ret = lydict_insert(ctx, prefix, prefix_len, &at->name.prefix), finish);
Michal Vasko501af032020-11-11 20:27:44 +01003026 }
3027 if (module_key_len) {
Michal Vaskoad92b672020-11-12 13:11:31 +01003028 LY_CHECK_GOTO(ret = lydict_insert(ctx, module_key, module_key_len, &at->name.module_ns), finish);
Michal Vasko501af032020-11-11 20:27:44 +01003029 }
3030
Michal Vasko52927e22020-03-16 17:26:14 +01003031 if (dynamic && *dynamic) {
Radek Krejci011e4aa2020-09-04 15:22:31 +02003032 ret = lydict_insert_zc(ctx, (char *)value, &at->value);
3033 LY_CHECK_GOTO(ret, finish);
Michal Vasko52927e22020-03-16 17:26:14 +01003034 *dynamic = 0;
3035 } else {
Radek Krejci011e4aa2020-09-04 15:22:31 +02003036 LY_CHECK_GOTO(ret = lydict_insert(ctx, value, value_len, &at->value), finish);
Michal Vasko52927e22020-03-16 17:26:14 +01003037 }
Michal Vasko501af032020-11-11 20:27:44 +01003038 at->format = format;
3039 at->val_prefix_data = val_prefix_data;
3040 at->hints = hints;
Michal Vasko52927e22020-03-16 17:26:14 +01003041
3042 /* insert as the last attribute */
3043 if (parent) {
Michal Vaskoa5da3292020-08-12 13:10:50 +02003044 lyd_insert_attr(parent, at);
Michal Vasko52927e22020-03-16 17:26:14 +01003045 } else if (*attr) {
Radek Krejci1e008d22020-08-17 11:37:37 +02003046 for (last = *attr; last->next; last = last->next) {}
Michal Vasko52927e22020-03-16 17:26:14 +01003047 last->next = at;
3048 }
3049
Radek Krejci011e4aa2020-09-04 15:22:31 +02003050finish:
3051 if (ret) {
3052 lyd_free_attr_single(ctx, at);
3053 } else if (attr) {
Michal Vasko52927e22020-03-16 17:26:14 +01003054 *attr = at;
3055 }
3056 return LY_SUCCESS;
3057}
3058
Jan Kundrátc6e39de2021-12-09 16:01:19 +01003059LIBYANG_API_DEF const struct lyd_node_term *
Michal Vasko004d3152020-06-11 19:59:22 +02003060lyd_target(const struct ly_path *path, const struct lyd_node *tree)
Radek Krejci084289f2019-07-09 17:35:30 +02003061{
Michal Vasko004d3152020-06-11 19:59:22 +02003062 struct lyd_node *target;
Radek Krejci084289f2019-07-09 17:35:30 +02003063
Michal Vasko004d3152020-06-11 19:59:22 +02003064 if (ly_path_eval(path, tree, &target)) {
3065 return NULL;
Radek Krejci084289f2019-07-09 17:35:30 +02003066 }
3067
Michal Vasko004d3152020-06-11 19:59:22 +02003068 return (struct lyd_node_term *)target;
Radek Krejci084289f2019-07-09 17:35:30 +02003069}
3070
aPiecek2f63f952021-03-30 12:22:18 +02003071/**
3072 * @brief Check the equality of the two schemas from different contexts.
3073 *
3074 * @param schema1 of first node.
3075 * @param schema2 of second node.
3076 * @return 1 if the schemas are equal otherwise 0.
3077 */
3078static ly_bool
3079lyd_compare_schema_equal(const struct lysc_node *schema1, const struct lysc_node *schema2)
3080{
3081 if (!schema1 && !schema2) {
3082 return 1;
3083 } else if (!schema1 || !schema2) {
3084 return 0;
3085 }
3086
3087 assert(schema1->module->ctx != schema2->module->ctx);
3088
3089 if (schema1->nodetype != schema2->nodetype) {
3090 return 0;
3091 }
3092
3093 if (strcmp(schema1->name, schema2->name)) {
3094 return 0;
3095 }
3096
3097 if (strcmp(schema1->module->name, schema2->module->name)) {
3098 return 0;
3099 }
3100
3101 if (schema1->module->revision || schema2->module->revision) {
3102 if (!schema1->module->revision || !schema2->module->revision) {
3103 return 0;
3104 }
3105 if (strcmp(schema1->module->revision, schema2->module->revision)) {
3106 return 0;
3107 }
3108 }
3109
3110 return 1;
3111}
3112
3113/**
3114 * @brief Check the equality of the schemas for all parent nodes.
3115 *
3116 * Both nodes must be from different contexts.
3117 *
3118 * @param node1 Data of first node.
3119 * @param node2 Data of second node.
3120 * @return 1 if the all related parental schemas are equal otherwise 0.
3121 */
3122static ly_bool
3123lyd_compare_schema_parents_equal(const struct lyd_node *node1, const struct lyd_node *node2)
3124{
3125 const struct lysc_node *parent1, *parent2;
3126
3127 assert(node1 && node2);
3128
3129 for (parent1 = node1->schema->parent, parent2 = node2->schema->parent;
3130 parent1 && parent2;
3131 parent1 = parent1->parent, parent2 = parent2->parent) {
3132 if (!lyd_compare_schema_equal(parent1, parent2)) {
3133 return 0;
3134 }
3135 }
3136
3137 if (parent1 || parent2) {
3138 return 0;
3139 }
3140
3141 return 1;
3142}
3143
3144/**
3145 * @brief Internal implementation of @ref lyd_compare_single.
3146 * @copydoc lyd_compare_single
3147 * @param[in] parental_schemas_checked Flag used for optimization.
3148 * When this function is called for the first time, the flag must be set to 0.
3149 * The @ref lyd_compare_schema_parents_equal should be called only once during
3150 * recursive calls, and this is accomplished by setting to 1 in the lyd_compare_single_ body.
3151 */
3152static LY_ERR
3153lyd_compare_single_(const struct lyd_node *node1, const struct lyd_node *node2,
3154 uint32_t options, ly_bool parental_schemas_checked)
Radek Krejci1f05b6a2019-07-18 16:15:06 +02003155{
3156 const struct lyd_node *iter1, *iter2;
3157 struct lyd_node_term *term1, *term2;
3158 struct lyd_node_any *any1, *any2;
Michal Vasko52927e22020-03-16 17:26:14 +01003159 struct lyd_node_opaq *opaq1, *opaq2;
Radek Krejci1f05b6a2019-07-18 16:15:06 +02003160 size_t len1, len2;
Radek Krejci084289f2019-07-09 17:35:30 +02003161
Radek Krejci1f05b6a2019-07-18 16:15:06 +02003162 if (!node1 || !node2) {
3163 if (node1 == node2) {
3164 return LY_SUCCESS;
3165 } else {
3166 return LY_ENOT;
3167 }
3168 }
3169
aPiecek2f63f952021-03-30 12:22:18 +02003170 if (LYD_CTX(node1) == LYD_CTX(node2)) {
3171 /* same contexts */
3172 if (node1->schema != node2->schema) {
3173 return LY_ENOT;
3174 }
3175 } else {
3176 /* different contexts */
3177 if (!lyd_compare_schema_equal(node1->schema, node2->schema)) {
3178 return LY_ENOT;
3179 }
3180 if (!parental_schemas_checked) {
3181 if (!lyd_compare_schema_parents_equal(node1, node2)) {
3182 return LY_ENOT;
3183 }
3184 parental_schemas_checked = 1;
3185 }
Radek Krejci1f05b6a2019-07-18 16:15:06 +02003186 }
3187
3188 if (node1->hash != node2->hash) {
3189 return LY_ENOT;
3190 }
aPiecek2f63f952021-03-30 12:22:18 +02003191 /* 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 +02003192
Michal Vasko52927e22020-03-16 17:26:14 +01003193 if (!node1->schema) {
3194 opaq1 = (struct lyd_node_opaq *)node1;
3195 opaq2 = (struct lyd_node_opaq *)node2;
aPiecek2f63f952021-03-30 12:22:18 +02003196 if ((strcmp(opaq1->name.name, opaq2->name.name)) || (opaq1->format != opaq2->format) ||
3197 (strcmp(opaq1->name.module_ns, opaq2->name.module_ns))) {
Radek Krejci1f05b6a2019-07-18 16:15:06 +02003198 return LY_ENOT;
3199 }
Michal Vasko52927e22020-03-16 17:26:14 +01003200 switch (opaq1->format) {
Radek Krejci8df109d2021-04-23 12:19:08 +02003201 case LY_VALUE_XML:
Radek Krejci09c77442021-04-26 11:10:34 +02003202 if (lyxml_value_compare(LYD_CTX(node1), opaq1->value, opaq1->val_prefix_data, LYD_CTX(node2), opaq2->value,
3203 opaq2->val_prefix_data)) {
Michal Vasko52927e22020-03-16 17:26:14 +01003204 return LY_ENOT;
3205 }
3206 break;
Radek Krejci8df109d2021-04-23 12:19:08 +02003207 case LY_VALUE_JSON:
Radek Krejci1798aae2020-07-14 13:26:06 +02003208 /* prefixes in JSON are unique, so it is not necessary to canonize the values */
3209 if (strcmp(opaq1->value, opaq2->value)) {
3210 return LY_ENOT;
3211 }
3212 break;
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01003213 default:
Michal Vasko52927e22020-03-16 17:26:14 +01003214 /* not allowed */
Michal Vaskob7be7a82020-08-20 09:09:04 +02003215 LOGINT(LYD_CTX(node1));
Michal Vasko52927e22020-03-16 17:26:14 +01003216 return LY_EINT;
3217 }
3218 if (options & LYD_COMPARE_FULL_RECURSION) {
3219 iter1 = opaq1->child;
3220 iter2 = opaq2->child;
Radek Krejci1f05b6a2019-07-18 16:15:06 +02003221 goto all_children_compare;
Michal Vasko52927e22020-03-16 17:26:14 +01003222 }
3223 return LY_SUCCESS;
3224 } else {
3225 switch (node1->schema->nodetype) {
3226 case LYS_LEAF:
3227 case LYS_LEAFLIST:
3228 if (options & LYD_COMPARE_DEFAULTS) {
3229 if ((node1->flags & LYD_DEFAULT) != (node2->flags & LYD_DEFAULT)) {
3230 return LY_ENOT;
3231 }
3232 }
3233
Michal Vasko1a2bf2e2020-06-16 13:07:48 +02003234 term1 = (struct lyd_node_term *)node1;
3235 term2 = (struct lyd_node_term *)node2;
aPiecek2f63f952021-03-30 12:22:18 +02003236
3237 /* same contexts */
3238 if (LYD_CTX(node1) == LYD_CTX(node2)) {
3239 return term1->value.realtype->plugin->compare(&term1->value, &term2->value);
3240 }
3241
3242 /* different contexts */
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02003243 if (strcmp(lyd_get_value(node1), lyd_get_value(node2))) {
aPiecek2f63f952021-03-30 12:22:18 +02003244 return LY_ENOT;
3245 }
3246 return LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +01003247 case LYS_CONTAINER:
3248 if (options & LYD_COMPARE_DEFAULTS) {
3249 if ((node1->flags & LYD_DEFAULT) != (node2->flags & LYD_DEFAULT)) {
3250 return LY_ENOT;
3251 }
3252 }
3253 if (options & LYD_COMPARE_FULL_RECURSION) {
Michal Vasko9e685082021-01-29 14:49:09 +01003254 iter1 = lyd_child(node1);
3255 iter2 = lyd_child(node2);
Michal Vasko52927e22020-03-16 17:26:14 +01003256 goto all_children_compare;
Radek Krejci1f05b6a2019-07-18 16:15:06 +02003257 }
3258 return LY_SUCCESS;
Michal Vasko1bf09392020-03-27 12:38:10 +01003259 case LYS_RPC:
Michal Vasko52927e22020-03-16 17:26:14 +01003260 case LYS_ACTION:
3261 if (options & LYD_COMPARE_FULL_RECURSION) {
3262 /* TODO action/RPC
3263 goto all_children_compare;
3264 */
Radek Krejci1f05b6a2019-07-18 16:15:06 +02003265 }
3266 return LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +01003267 case LYS_NOTIF:
3268 if (options & LYD_COMPARE_FULL_RECURSION) {
3269 /* TODO Notification
3270 goto all_children_compare;
3271 */
3272 }
3273 return LY_SUCCESS;
3274 case LYS_LIST:
Michal Vasko9e685082021-01-29 14:49:09 +01003275 iter1 = lyd_child(node1);
3276 iter2 = lyd_child(node2);
Michal Vasko52927e22020-03-16 17:26:14 +01003277
3278 if (!(node1->schema->flags & LYS_KEYLESS) && !(options & LYD_COMPARE_FULL_RECURSION)) {
3279 /* lists with keys, their equivalence is based on their keys */
Michal Vasko544e58a2021-01-28 14:33:41 +01003280 for (const struct lysc_node *key = lysc_node_child(node1->schema);
Michal Vaskoc57d9a92020-06-23 13:28:27 +02003281 key && (key->flags & LYS_KEY);
Michal Vasko52927e22020-03-16 17:26:14 +01003282 key = key->next) {
aPiecek2f63f952021-03-30 12:22:18 +02003283 if (lyd_compare_single_(iter1, iter2, options, parental_schemas_checked)) {
Michal Vasko52927e22020-03-16 17:26:14 +01003284 return LY_ENOT;
3285 }
3286 iter1 = iter1->next;
3287 iter2 = iter2->next;
3288 }
3289 } else {
3290 /* lists without keys, their equivalence is based on equivalence of all the children (both direct and indirect) */
3291
Radek Krejci0f969882020-08-21 16:56:47 +02003292all_children_compare:
Michal Vasko52927e22020-03-16 17:26:14 +01003293 if (!iter1 && !iter2) {
3294 /* no children, nothing to compare */
3295 return LY_SUCCESS;
3296 }
3297
Michal Vaskod989ba02020-08-24 10:59:24 +02003298 for ( ; iter1 && iter2; iter1 = iter1->next, iter2 = iter2->next) {
aPiecek2f63f952021-03-30 12:22:18 +02003299 if (lyd_compare_single_(iter1, iter2, options | LYD_COMPARE_FULL_RECURSION, parental_schemas_checked)) {
Michal Vasko52927e22020-03-16 17:26:14 +01003300 return LY_ENOT;
3301 }
3302 }
3303 if (iter1 || iter2) {
3304 return LY_ENOT;
3305 }
3306 }
3307 return LY_SUCCESS;
3308 case LYS_ANYXML:
3309 case LYS_ANYDATA:
Michal Vasko22df3f02020-08-24 13:29:22 +02003310 any1 = (struct lyd_node_any *)node1;
3311 any2 = (struct lyd_node_any *)node2;
Michal Vasko52927e22020-03-16 17:26:14 +01003312
3313 if (any1->value_type != any2->value_type) {
3314 return LY_ENOT;
3315 }
3316 switch (any1->value_type) {
3317 case LYD_ANYDATA_DATATREE:
3318 iter1 = any1->value.tree;
3319 iter2 = any2->value.tree;
3320 goto all_children_compare;
3321 case LYD_ANYDATA_STRING:
3322 case LYD_ANYDATA_XML:
3323 case LYD_ANYDATA_JSON:
3324 len1 = strlen(any1->value.str);
3325 len2 = strlen(any2->value.str);
Michal Vasko69730152020-10-09 16:30:07 +02003326 if ((len1 != len2) || strcmp(any1->value.str, any2->value.str)) {
Michal Vasko52927e22020-03-16 17:26:14 +01003327 return LY_ENOT;
3328 }
3329 return LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +01003330 case LYD_ANYDATA_LYB:
Michal Vasko60ea6352020-06-29 13:39:39 +02003331 len1 = lyd_lyb_data_length(any1->value.mem);
3332 len2 = lyd_lyb_data_length(any2->value.mem);
Michal Vasko69730152020-10-09 16:30:07 +02003333 if ((len1 != len2) || memcmp(any1->value.mem, any2->value.mem, len1)) {
Michal Vasko52927e22020-03-16 17:26:14 +01003334 return LY_ENOT;
3335 }
3336 return LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +01003337 }
Radek Krejci1f05b6a2019-07-18 16:15:06 +02003338 }
3339 }
3340
Michal Vaskob7be7a82020-08-20 09:09:04 +02003341 LOGINT(LYD_CTX(node1));
Radek Krejci1f05b6a2019-07-18 16:15:06 +02003342 return LY_EINT;
3343}
Radek Krejci22ebdba2019-07-25 13:59:43 +02003344
Jan Kundrátc6e39de2021-12-09 16:01:19 +01003345LIBYANG_API_DEF LY_ERR
aPiecek2f63f952021-03-30 12:22:18 +02003346lyd_compare_single(const struct lyd_node *node1, const struct lyd_node *node2, uint32_t options)
3347{
3348 return lyd_compare_single_(node1, node2, options, 0);
3349}
3350
Jan Kundrátc6e39de2021-12-09 16:01:19 +01003351LIBYANG_API_DEF LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003352lyd_compare_siblings(const struct lyd_node *node1, const struct lyd_node *node2, uint32_t options)
Michal Vasko8f359bf2020-07-28 10:41:15 +02003353{
Michal Vaskod989ba02020-08-24 10:59:24 +02003354 for ( ; node1 && node2; node1 = node1->next, node2 = node2->next) {
Michal Vasko8f359bf2020-07-28 10:41:15 +02003355 LY_CHECK_RET(lyd_compare_single(node1, node2, options));
3356 }
3357
Michal Vasko11a81432020-07-28 16:31:29 +02003358 if (node1 == node2) {
3359 return LY_SUCCESS;
Michal Vasko8f359bf2020-07-28 10:41:15 +02003360 }
Michal Vasko11a81432020-07-28 16:31:29 +02003361 return LY_ENOT;
Michal Vasko8f359bf2020-07-28 10:41:15 +02003362}
3363
Jan Kundrátc6e39de2021-12-09 16:01:19 +01003364LIBYANG_API_DEF LY_ERR
Michal Vasko21725742020-06-29 11:49:25 +02003365lyd_compare_meta(const struct lyd_meta *meta1, const struct lyd_meta *meta2)
3366{
3367 if (!meta1 || !meta2) {
3368 if (meta1 == meta2) {
3369 return LY_SUCCESS;
3370 } else {
3371 return LY_ENOT;
3372 }
3373 }
3374
Michal Vaskoa8083062020-11-06 17:22:10 +01003375 if ((meta1->annotation->module->ctx != meta2->annotation->module->ctx) || (meta1->annotation != meta2->annotation)) {
Michal Vasko21725742020-06-29 11:49:25 +02003376 return LY_ENOT;
3377 }
3378
Michal Vasko21725742020-06-29 11:49:25 +02003379 return meta1->value.realtype->plugin->compare(&meta1->value, &meta2->value);
3380}
3381
Radek Krejci22ebdba2019-07-25 13:59:43 +02003382/**
Michal Vasko9cf62422021-07-01 13:29:32 +02003383 * @brief Create a copy of the attribute.
3384 *
3385 * @param[in] attr Attribute to copy.
3386 * @param[in] node Opaque where to append the new attribute.
3387 * @param[out] dup Optional created attribute copy.
3388 * @return LY_ERR value.
3389 */
3390static LY_ERR
3391lyd_dup_attr_single(const struct lyd_attr *attr, struct lyd_node *node, struct lyd_attr **dup)
3392{
3393 LY_ERR ret = LY_SUCCESS;
3394 struct lyd_attr *a, *last;
3395 struct lyd_node_opaq *opaq = (struct lyd_node_opaq *)node;
3396
3397 LY_CHECK_ARG_RET(NULL, attr, node, !node->schema, LY_EINVAL);
3398
3399 /* create a copy */
3400 a = calloc(1, sizeof *attr);
3401 LY_CHECK_ERR_RET(!a, LOGMEM(LYD_CTX(node)), LY_EMEM);
3402
3403 LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), attr->name.name, 0, &a->name.name), finish);
3404 LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), attr->name.prefix, 0, &a->name.prefix), finish);
3405 LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), attr->name.module_ns, 0, &a->name.module_ns), finish);
3406 LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), attr->value, 0, &a->value), finish);
3407 a->hints = attr->hints;
3408 a->format = attr->format;
3409 if (attr->val_prefix_data) {
3410 ret = ly_dup_prefix_data(LYD_CTX(node), attr->format, attr->val_prefix_data, &a->val_prefix_data);
3411 LY_CHECK_GOTO(ret, finish);
3412 }
3413
3414 /* insert as the last attribute */
3415 a->parent = opaq;
3416 if (opaq->attr) {
3417 for (last = opaq->attr; last->next; last = last->next) {}
3418 last->next = a;
3419 } else {
3420 opaq->attr = a;
3421 }
3422
3423finish:
3424 if (ret) {
3425 lyd_free_attr_single(LYD_CTX(node), a);
3426 } else if (dup) {
3427 *dup = a;
3428 }
3429 return LY_SUCCESS;
3430}
3431
3432/**
Michal Vaskoaac267d2022-01-17 13:34:48 +01003433 * @brief Find @p schema equivalent in @p trg_ctx.
3434 *
3435 * @param[in] schema Schema node to find.
3436 * @param[in] trg_ctx Target context to search in.
3437 * @param[in] parent Data parent of @p schema, if any.
3438 * @param[out] trg_schema Found schema from @p trg_ctx to use.
3439 * @return LY_RRR value.
3440 */
3441static LY_ERR
3442lyd_dup_find_schema(const struct lysc_node *schema, const struct ly_ctx *trg_ctx, struct lyd_node *parent,
3443 const struct lysc_node **trg_schema)
3444{
3445 const struct lysc_node *node = NULL, *sparent = NULL;
3446 const struct lys_module *mod = NULL;
3447 char *path;
3448
3449 if (!schema) {
3450 /* opaque node */
3451 *trg_schema = NULL;
3452 return LY_SUCCESS;
3453 }
3454
3455 if (parent && parent->schema) {
3456 /* start from schema parent */
3457 sparent = parent->schema;
3458 } else {
3459 /* start from module */
3460 mod = ly_ctx_get_module_implemented(trg_ctx, schema->module->name);
3461 if (!mod) {
3462 LOGERR(trg_ctx, LY_ENOTFOUND, "Module \"%s\" not present/implemented in the target context.",
3463 schema->module->name);
3464 return LY_ENOTFOUND;
3465 }
3466 }
3467
3468 /* find the schema node */
3469 while ((node = lys_getnext(node, sparent, mod ? mod->compiled : NULL, 0))) {
3470 if (!strcmp(node->module->name, schema->module->name) && !strcmp(node->name, schema->name)) {
3471 *trg_schema = node;
3472 return LY_SUCCESS;
3473 }
3474 }
3475
3476 /* schema node not found */
3477 path = lysc_path(schema, LYSC_PATH_LOG, NULL, 0);
3478 LOGERR(trg_ctx, LY_ENOTFOUND, "Schema node \"%s\" not found in the target context.", path);
3479 free(path);
3480 return LY_ENOTFOUND;
3481}
3482
3483/**
Michal Vasko52927e22020-03-16 17:26:14 +01003484 * @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 +02003485 *
Michal Vaskoaac267d2022-01-17 13:34:48 +01003486 * 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 +02003487 *
Michal Vaskoaac267d2022-01-17 13:34:48 +01003488 * @param[in] node Node to duplicate.
3489 * @param[in] trg_ctx Target context for duplicated nodes.
Radek Krejcif8b95172020-05-15 14:51:06 +02003490 * @param[in] parent Parent to insert into, NULL for top-level sibling.
Michal Vasko67177e52021-08-25 11:15:15 +02003491 * @param[in] insert_last Whether the duplicated node can be inserted as the last child of @p parent. Set for
3492 * recursive duplication as an optimization.
Radek Krejcif8b95172020-05-15 14:51:06 +02003493 * @param[in,out] first First sibling, NULL if no top-level sibling exist yet. Can be also NULL if @p parent is set.
3494 * @param[in] options Bitmask of options flags, see @ref dupoptions.
Michal Vaskoaac267d2022-01-17 13:34:48 +01003495 * @param[out] dup_p Pointer where the created duplicated node is placed (besides connecting it to @p parent / @p first).
3496 * @return LY_ERR value.
Radek Krejci22ebdba2019-07-25 13:59:43 +02003497 */
Michal Vasko52927e22020-03-16 17:26:14 +01003498static LY_ERR
Michal Vaskoaac267d2022-01-17 13:34:48 +01003499lyd_dup_r(const struct lyd_node *node, const struct ly_ctx *trg_ctx, struct lyd_node *parent, ly_bool insert_last,
3500 struct lyd_node **first, uint32_t options, struct lyd_node **dup_p)
Radek Krejci22ebdba2019-07-25 13:59:43 +02003501{
Michal Vasko52927e22020-03-16 17:26:14 +01003502 LY_ERR ret;
Radek Krejci22ebdba2019-07-25 13:59:43 +02003503 struct lyd_node *dup = NULL;
Michal Vasko61551fa2020-07-09 15:45:45 +02003504 struct lyd_meta *meta;
Michal Vasko9cf62422021-07-01 13:29:32 +02003505 struct lyd_attr *attr;
Michal Vasko61551fa2020-07-09 15:45:45 +02003506 struct lyd_node_any *any;
Michal Vaskoaac267d2022-01-17 13:34:48 +01003507 const struct lysc_type *type;
3508 const char *val_can;
Radek Krejci22ebdba2019-07-25 13:59:43 +02003509
Michal Vasko52927e22020-03-16 17:26:14 +01003510 LY_CHECK_ARG_RET(NULL, node, LY_EINVAL);
Radek Krejci22ebdba2019-07-25 13:59:43 +02003511
Michal Vasko52927e22020-03-16 17:26:14 +01003512 if (!node->schema) {
3513 dup = calloc(1, sizeof(struct lyd_node_opaq));
Michal Vaskoaac267d2022-01-17 13:34:48 +01003514 ((struct lyd_node_opaq *)dup)->ctx = trg_ctx;
Michal Vasko52927e22020-03-16 17:26:14 +01003515 } else {
3516 switch (node->schema->nodetype) {
Michal Vasko1bf09392020-03-27 12:38:10 +01003517 case LYS_RPC:
Michal Vasko52927e22020-03-16 17:26:14 +01003518 case LYS_ACTION:
3519 case LYS_NOTIF:
3520 case LYS_CONTAINER:
3521 case LYS_LIST:
3522 dup = calloc(1, sizeof(struct lyd_node_inner));
3523 break;
3524 case LYS_LEAF:
3525 case LYS_LEAFLIST:
3526 dup = calloc(1, sizeof(struct lyd_node_term));
3527 break;
3528 case LYS_ANYDATA:
3529 case LYS_ANYXML:
3530 dup = calloc(1, sizeof(struct lyd_node_any));
3531 break;
3532 default:
Michal Vaskoaac267d2022-01-17 13:34:48 +01003533 LOGINT(trg_ctx);
Michal Vasko52927e22020-03-16 17:26:14 +01003534 ret = LY_EINT;
3535 goto error;
3536 }
Radek Krejci22ebdba2019-07-25 13:59:43 +02003537 }
Michal Vaskoaac267d2022-01-17 13:34:48 +01003538 LY_CHECK_ERR_GOTO(!dup, LOGMEM(trg_ctx); ret = LY_EMEM, error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02003539
Michal Vaskof6df0a02020-06-16 13:08:34 +02003540 if (options & LYD_DUP_WITH_FLAGS) {
3541 dup->flags = node->flags;
3542 } else {
3543 dup->flags = (node->flags & LYD_DEFAULT) | LYD_NEW;
3544 }
Michal Vaskoaac267d2022-01-17 13:34:48 +01003545 if (trg_ctx == LYD_CTX(node)) {
3546 dup->schema = node->schema;
3547 } else {
3548 LY_CHECK_GOTO(ret = lyd_dup_find_schema(node->schema, trg_ctx, parent, &dup->schema), error);
3549 }
Michal Vasko52927e22020-03-16 17:26:14 +01003550 dup->prev = dup;
Radek Krejci22ebdba2019-07-25 13:59:43 +02003551
Michal Vasko9cf62422021-07-01 13:29:32 +02003552 /* duplicate metadata/attributes */
Michal Vasko25a32822020-07-09 15:48:22 +02003553 if (!(options & LYD_DUP_NO_META)) {
Michal Vasko9cf62422021-07-01 13:29:32 +02003554 if (!node->schema) {
3555 LY_LIST_FOR(((struct lyd_node_opaq *)node)->attr, attr) {
3556 LY_CHECK_GOTO(ret = lyd_dup_attr_single(attr, dup, NULL), error);
3557 }
3558 } else {
3559 LY_LIST_FOR(node->meta, meta) {
3560 LY_CHECK_GOTO(ret = lyd_dup_meta_single(meta, dup, NULL), error);
3561 }
Michal Vasko25a32822020-07-09 15:48:22 +02003562 }
3563 }
Radek Krejci22ebdba2019-07-25 13:59:43 +02003564
3565 /* nodetype-specific work */
Michal Vasko52927e22020-03-16 17:26:14 +01003566 if (!dup->schema) {
3567 struct lyd_node_opaq *opaq = (struct lyd_node_opaq *)dup;
3568 struct lyd_node_opaq *orig = (struct lyd_node_opaq *)node;
3569 struct lyd_node *child;
Radek Krejci22ebdba2019-07-25 13:59:43 +02003570
3571 if (options & LYD_DUP_RECURSIVE) {
3572 /* duplicate all the children */
3573 LY_LIST_FOR(orig->child, child) {
Michal Vaskoaac267d2022-01-17 13:34:48 +01003574 LY_CHECK_GOTO(ret = lyd_dup_r(child, trg_ctx, dup, 1, NULL, options, NULL), error);
Michal Vasko52927e22020-03-16 17:26:14 +01003575 }
3576 }
Michal Vaskoaac267d2022-01-17 13:34:48 +01003577 LY_CHECK_GOTO(ret = lydict_insert(trg_ctx, orig->name.name, 0, &opaq->name.name), error);
3578 LY_CHECK_GOTO(ret = lydict_insert(trg_ctx, orig->name.prefix, 0, &opaq->name.prefix), error);
3579 LY_CHECK_GOTO(ret = lydict_insert(trg_ctx, orig->name.module_ns, 0, &opaq->name.module_ns), error);
3580 LY_CHECK_GOTO(ret = lydict_insert(trg_ctx, orig->value, 0, &opaq->value), error);
Michal Vasko9cf62422021-07-01 13:29:32 +02003581 opaq->hints = orig->hints;
3582 opaq->format = orig->format;
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01003583 if (orig->val_prefix_data) {
Michal Vaskoaac267d2022-01-17 13:34:48 +01003584 ret = ly_dup_prefix_data(trg_ctx, opaq->format, orig->val_prefix_data, &opaq->val_prefix_data);
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01003585 LY_CHECK_GOTO(ret, error);
Michal Vasko52927e22020-03-16 17:26:14 +01003586 }
Michal Vasko52927e22020-03-16 17:26:14 +01003587 } else if (dup->schema->nodetype & LYD_NODE_TERM) {
3588 struct lyd_node_term *term = (struct lyd_node_term *)dup;
3589 struct lyd_node_term *orig = (struct lyd_node_term *)node;
3590
3591 term->hash = orig->hash;
Michal Vaskoaac267d2022-01-17 13:34:48 +01003592 if (trg_ctx == LYD_CTX(node)) {
3593 ret = orig->value.realtype->plugin->duplicate(trg_ctx, &orig->value, &term->value);
3594 LY_CHECK_ERR_GOTO(ret, LOGERR(trg_ctx, ret, "Value duplication failed."), error);
3595 } else {
3596 /* store canonical value in the target context */
3597 val_can = lyd_get_value(node);
3598 type = ((struct lysc_node_leaf *)term->schema)->type;
3599 ret = lyd_value_store(trg_ctx, &term->value, type, val_can, strlen(val_can), NULL, LY_VALUE_CANON, NULL,
3600 LYD_HINT_DATA, term->schema, NULL);
3601 LY_CHECK_GOTO(ret, error);
3602 }
Michal Vasko52927e22020-03-16 17:26:14 +01003603 } else if (dup->schema->nodetype & LYD_NODE_INNER) {
3604 struct lyd_node_inner *orig = (struct lyd_node_inner *)node;
3605 struct lyd_node *child;
3606
3607 if (options & LYD_DUP_RECURSIVE) {
3608 /* duplicate all the children */
3609 LY_LIST_FOR(orig->child, child) {
Michal Vaskoaac267d2022-01-17 13:34:48 +01003610 LY_CHECK_GOTO(ret = lyd_dup_r(child, trg_ctx, dup, 1, NULL, options, NULL), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02003611 }
Michal Vasko69730152020-10-09 16:30:07 +02003612 } else if ((dup->schema->nodetype == LYS_LIST) && !(dup->schema->flags & LYS_KEYLESS)) {
Radek Krejci22ebdba2019-07-25 13:59:43 +02003613 /* always duplicate keys of a list */
Radek Krejci22ebdba2019-07-25 13:59:43 +02003614 child = orig->child;
Michal Vasko544e58a2021-01-28 14:33:41 +01003615 for (const struct lysc_node *key = lysc_node_child(dup->schema);
Michal Vaskoc57d9a92020-06-23 13:28:27 +02003616 key && (key->flags & LYS_KEY);
Radek Krejci0fe9b512019-07-26 17:51:05 +02003617 key = key->next) {
Radek Krejci22ebdba2019-07-25 13:59:43 +02003618 if (!child) {
3619 /* possibly not keys are present in filtered tree */
3620 break;
Radek Krejci0fe9b512019-07-26 17:51:05 +02003621 } else if (child->schema != key) {
3622 /* possibly not all keys are present in filtered tree,
3623 * but there can be also some non-key nodes */
3624 continue;
Radek Krejci22ebdba2019-07-25 13:59:43 +02003625 }
Michal Vaskoaac267d2022-01-17 13:34:48 +01003626 LY_CHECK_GOTO(ret = lyd_dup_r(child, trg_ctx, dup, 1, NULL, options, NULL), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02003627 child = child->next;
3628 }
3629 }
3630 lyd_hash(dup);
3631 } else if (dup->schema->nodetype & LYD_NODE_ANY) {
Michal Vasko61551fa2020-07-09 15:45:45 +02003632 dup->hash = node->hash;
3633 any = (struct lyd_node_any *)node;
3634 LY_CHECK_GOTO(ret = lyd_any_copy_value(dup, &any->value, any->value_type), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02003635 }
3636
Michal Vasko52927e22020-03-16 17:26:14 +01003637 /* insert */
Michal Vasko67177e52021-08-25 11:15:15 +02003638 lyd_insert_node(parent, first, dup, insert_last);
Michal Vasko52927e22020-03-16 17:26:14 +01003639
3640 if (dup_p) {
3641 *dup_p = dup;
3642 }
3643 return LY_SUCCESS;
Radek Krejci22ebdba2019-07-25 13:59:43 +02003644
3645error:
Michal Vasko52927e22020-03-16 17:26:14 +01003646 lyd_free_tree(dup);
3647 return ret;
Radek Krejci22ebdba2019-07-25 13:59:43 +02003648}
3649
Michal Vasko29d674b2021-08-25 11:18:35 +02003650/**
3651 * @brief Get a parent node to connect duplicated subtree to.
3652 *
3653 * @param[in] node Node (subtree) to duplicate.
Michal Vaskoaac267d2022-01-17 13:34:48 +01003654 * @param[in] trg_ctx Target context for duplicated nodes.
Michal Vasko29d674b2021-08-25 11:18:35 +02003655 * @param[in] parent Initial parent to connect to.
3656 * @param[in] options Bitmask of options flags, see @ref dupoptions.
3657 * @param[out] dup_parent First duplicated parent node, if any.
3658 * @param[out] local_parent Correct parent to directly connect duplicated @p node to.
3659 * @return LY_ERR value.
3660 */
Michal Vasko3a41dff2020-07-15 14:30:28 +02003661static LY_ERR
Michal Vaskoaac267d2022-01-17 13:34:48 +01003662lyd_dup_get_local_parent(const struct lyd_node *node, const struct ly_ctx *trg_ctx, const struct lyd_node_inner *parent,
3663 uint32_t options, struct lyd_node **dup_parent, struct lyd_node_inner **local_parent)
Radek Krejci22ebdba2019-07-25 13:59:43 +02003664{
Michal Vasko3a41dff2020-07-15 14:30:28 +02003665 const struct lyd_node_inner *orig_parent, *iter;
Radek Krejci857189e2020-09-01 13:26:36 +02003666 ly_bool repeat = 1;
Michal Vasko3a41dff2020-07-15 14:30:28 +02003667
3668 *dup_parent = NULL;
3669 *local_parent = NULL;
3670
3671 for (orig_parent = node->parent; repeat && orig_parent; orig_parent = orig_parent->parent) {
3672 if (parent && (parent->schema == orig_parent->schema)) {
3673 /* stop creating parents, connect what we have into the provided parent */
3674 iter = parent;
3675 repeat = 0;
3676 } else {
3677 iter = NULL;
Michal Vaskoaac267d2022-01-17 13:34:48 +01003678 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 +02003679 (struct lyd_node **)&iter));
Michal Vasko3a41dff2020-07-15 14:30:28 +02003680 }
3681 if (!*local_parent) {
3682 *local_parent = (struct lyd_node_inner *)iter;
3683 }
3684 if (iter->child) {
3685 /* 1) list - add after keys
3686 * 2) provided parent with some children */
3687 iter->child->prev->next = *dup_parent;
3688 if (*dup_parent) {
3689 (*dup_parent)->prev = iter->child->prev;
3690 iter->child->prev = *dup_parent;
3691 }
3692 } else {
3693 ((struct lyd_node_inner *)iter)->child = *dup_parent;
3694 }
3695 if (*dup_parent) {
3696 (*dup_parent)->parent = (struct lyd_node_inner *)iter;
3697 }
3698 *dup_parent = (struct lyd_node *)iter;
3699 }
3700
3701 if (repeat && parent) {
3702 /* given parent and created parents chain actually do not interconnect */
Michal Vaskoaac267d2022-01-17 13:34:48 +01003703 LOGERR(trg_ctx, LY_EINVAL,
Michal Vasko69730152020-10-09 16:30:07 +02003704 "Invalid argument parent (%s()) - does not interconnect with the created node's parents chain.", __func__);
Michal Vasko3a41dff2020-07-15 14:30:28 +02003705 return LY_EINVAL;
3706 }
3707
3708 return LY_SUCCESS;
3709}
3710
3711static LY_ERR
Michal Vaskoaac267d2022-01-17 13:34:48 +01003712lyd_dup(const struct lyd_node *node, const struct ly_ctx *trg_ctx, struct lyd_node_inner *parent, uint32_t options,
3713 ly_bool nosiblings, struct lyd_node **dup)
Michal Vasko3a41dff2020-07-15 14:30:28 +02003714{
3715 LY_ERR rc;
Radek Krejci22ebdba2019-07-25 13:59:43 +02003716 const struct lyd_node *orig; /* original node to be duplicated */
3717 struct lyd_node *first = NULL; /* the first duplicated node, this is returned */
Radek Krejci22ebdba2019-07-25 13:59:43 +02003718 struct lyd_node *top = NULL; /* the most higher created node */
3719 struct lyd_node_inner *local_parent = NULL; /* the direct parent node for the duplicated node(s) */
Radek Krejci22ebdba2019-07-25 13:59:43 +02003720
Michal Vaskoaac267d2022-01-17 13:34:48 +01003721 assert(node && trg_ctx && (!parent || (LYD_CTX(parent) == trg_ctx)));
Radek Krejci22ebdba2019-07-25 13:59:43 +02003722
3723 if (options & LYD_DUP_WITH_PARENTS) {
Michal Vaskoaac267d2022-01-17 13:34:48 +01003724 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 +02003725 &top, &local_parent), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02003726 } else {
3727 local_parent = parent;
3728 }
3729
Radek Krejci22ebdba2019-07-25 13:59:43 +02003730 LY_LIST_FOR(node, orig) {
Michal Vasko35f4d772021-01-12 12:08:57 +01003731 if (lysc_is_key(orig->schema)) {
3732 if (local_parent) {
3733 /* the key must already exist in the parent */
3734 rc = lyd_find_sibling_schema(local_parent->child, orig->schema, first ? NULL : &first);
Michal Vaskoaac267d2022-01-17 13:34:48 +01003735 LY_CHECK_ERR_GOTO(rc, LOGINT(trg_ctx), error);
Michal Vasko35f4d772021-01-12 12:08:57 +01003736 } else {
3737 assert(!(options & LYD_DUP_WITH_PARENTS));
3738 /* duplicating a single key, okay, I suppose... */
Michal Vaskoaac267d2022-01-17 13:34:48 +01003739 rc = lyd_dup_r(orig, trg_ctx, NULL, 0, &first, options, first ? NULL : &first);
Michal Vasko35f4d772021-01-12 12:08:57 +01003740 LY_CHECK_GOTO(rc, error);
3741 }
3742 } else {
3743 /* if there is no local parent, it will be inserted into first */
Michal Vaskoaac267d2022-01-17 13:34:48 +01003744 rc = lyd_dup_r(orig, trg_ctx, local_parent ? &local_parent->node : NULL, 0, &first, options,
3745 first ? NULL : &first);
Michal Vasko35f4d772021-01-12 12:08:57 +01003746 LY_CHECK_GOTO(rc, error);
3747 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02003748 if (nosiblings) {
Radek Krejci22ebdba2019-07-25 13:59:43 +02003749 break;
3750 }
3751 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02003752
Michal Vasko3a41dff2020-07-15 14:30:28 +02003753 if (dup) {
3754 *dup = first;
3755 }
3756 return LY_SUCCESS;
Radek Krejci22ebdba2019-07-25 13:59:43 +02003757
3758error:
3759 if (top) {
3760 lyd_free_tree(top);
3761 } else {
Michal Vaskof03ed032020-03-04 13:31:44 +01003762 lyd_free_siblings(first);
Radek Krejci22ebdba2019-07-25 13:59:43 +02003763 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02003764 return rc;
Radek Krejci22ebdba2019-07-25 13:59:43 +02003765}
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003766
Jan Kundrátc6e39de2021-12-09 16:01:19 +01003767LIBYANG_API_DEF LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003768lyd_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 +02003769{
Michal Vaskoaac267d2022-01-17 13:34:48 +01003770 LY_CHECK_ARG_RET(NULL, node, LY_EINVAL);
3771 if (node && parent && (LYD_CTX(node) != LYD_CTX(parent))) {
3772 LOGERR(NULL, LY_EINVAL, "Different contexts used in node duplication.");
3773 return LY_EINVAL;
3774 }
3775
3776 return lyd_dup(node, LYD_CTX(node), parent, options, 1, dup);
3777}
3778
3779LIBYANG_API_DEF LY_ERR
3780lyd_dup_single_to_ctx(const struct lyd_node *node, const struct ly_ctx *trg_ctx, struct lyd_node_inner *parent,
3781 uint32_t options, struct lyd_node **dup)
3782{
3783 LY_CHECK_ARG_RET(trg_ctx, node, trg_ctx, LY_EINVAL);
3784
3785 return lyd_dup(node, trg_ctx, parent, options, 1, dup);
Michal Vasko3a41dff2020-07-15 14:30:28 +02003786}
3787
Jan Kundrátc6e39de2021-12-09 16:01:19 +01003788LIBYANG_API_DEF LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003789lyd_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 +02003790{
Michal Vaskoaac267d2022-01-17 13:34:48 +01003791 LY_CHECK_ARG_RET(NULL, node, LY_EINVAL);
3792 if (node && parent && (LYD_CTX(node) != LYD_CTX(parent))) {
3793 LOGERR(NULL, LY_EINVAL, "Different contexts used in node duplication.");
3794 return LY_EINVAL;
3795 }
3796
3797 return lyd_dup(node, LYD_CTX(node), parent, options, 0, dup);
3798}
3799
3800LIBYANG_API_DEF LY_ERR
3801lyd_dup_siblings_to_ctx(const struct lyd_node *node, const struct ly_ctx *trg_ctx, struct lyd_node_inner *parent,
3802 uint32_t options, struct lyd_node **dup)
3803{
3804 LY_CHECK_ARG_RET(trg_ctx, node, trg_ctx, LY_EINVAL);
3805
3806 return lyd_dup(node, trg_ctx, parent, options, 0, dup);
Michal Vasko3a41dff2020-07-15 14:30:28 +02003807}
3808
Jan Kundrátc6e39de2021-12-09 16:01:19 +01003809LIBYANG_API_DEF LY_ERR
Michal Vasko3a41dff2020-07-15 14:30:28 +02003810lyd_dup_meta_single(const struct lyd_meta *meta, struct lyd_node *node, struct lyd_meta **dup)
Michal Vasko25a32822020-07-09 15:48:22 +02003811{
Radek Krejci011e4aa2020-09-04 15:22:31 +02003812 LY_ERR ret = LY_SUCCESS;
Michal Vasko25a32822020-07-09 15:48:22 +02003813 struct lyd_meta *mt, *last;
3814
Michal Vasko3a41dff2020-07-15 14:30:28 +02003815 LY_CHECK_ARG_RET(NULL, meta, node, LY_EINVAL);
Michal Vasko25a32822020-07-09 15:48:22 +02003816
3817 /* create a copy */
3818 mt = calloc(1, sizeof *mt);
Michal Vaskob7be7a82020-08-20 09:09:04 +02003819 LY_CHECK_ERR_RET(!mt, LOGMEM(LYD_CTX(node)), LY_EMEM);
Michal Vasko25a32822020-07-09 15:48:22 +02003820 mt->annotation = meta->annotation;
Michal Vaskob7be7a82020-08-20 09:09:04 +02003821 ret = meta->value.realtype->plugin->duplicate(LYD_CTX(node), &meta->value, &mt->value);
Radek Krejci011e4aa2020-09-04 15:22:31 +02003822 LY_CHECK_ERR_GOTO(ret, LOGERR(LYD_CTX(node), LY_EINT, "Value duplication failed."), finish);
3823 LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), meta->name, 0, &mt->name), finish);
Michal Vasko25a32822020-07-09 15:48:22 +02003824
3825 /* insert as the last attribute */
Radek Krejci011e4aa2020-09-04 15:22:31 +02003826 mt->parent = node;
Michal Vasko25a32822020-07-09 15:48:22 +02003827 if (node->meta) {
Radek Krejci1e008d22020-08-17 11:37:37 +02003828 for (last = node->meta; last->next; last = last->next) {}
Michal Vasko25a32822020-07-09 15:48:22 +02003829 last->next = mt;
3830 } else {
3831 node->meta = mt;
3832 }
3833
Radek Krejci011e4aa2020-09-04 15:22:31 +02003834finish:
3835 if (ret) {
3836 lyd_free_meta_single(mt);
3837 } else if (dup) {
Michal Vasko3a41dff2020-07-15 14:30:28 +02003838 *dup = mt;
3839 }
3840 return LY_SUCCESS;
Michal Vasko25a32822020-07-09 15:48:22 +02003841}
3842
Michal Vasko4490d312020-06-16 13:08:55 +02003843/**
3844 * @brief Merge a source sibling into target siblings.
3845 *
3846 * @param[in,out] first_trg First target sibling, is updated if top-level.
3847 * @param[in] parent_trg Target parent.
3848 * @param[in,out] sibling_src Source sibling to merge, set to NULL if spent.
Michal Vaskocd3f6172021-05-18 16:14:50 +02003849 * @param[in] merge_cb Optional merge callback.
3850 * @param[in] cb_data Arbitrary callback data.
Michal Vasko4490d312020-06-16 13:08:55 +02003851 * @param[in] options Merge options.
Michal Vaskocd3f6172021-05-18 16:14:50 +02003852 * @param[in,out] dup_inst Duplicate instance cache for all @p first_trg siblings.
Michal Vasko4490d312020-06-16 13:08:55 +02003853 * @return LY_ERR value.
3854 */
3855static LY_ERR
3856lyd_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 +02003857 lyd_merge_cb merge_cb, void *cb_data, uint16_t options, struct lyd_dup_inst **dup_inst)
Michal Vasko4490d312020-06-16 13:08:55 +02003858{
Michal Vasko4490d312020-06-16 13:08:55 +02003859 const struct lyd_node *child_src, *tmp, *sibling_src;
Michal Vasko56daf732020-08-10 10:57:18 +02003860 struct lyd_node *match_trg, *dup_src, *elem;
Michal Vaskofee29a92022-01-27 16:18:12 +01003861 struct lyd_node_opaq *opaq_trg, *opaq_src;
Michal Vasko4490d312020-06-16 13:08:55 +02003862 struct lysc_type *type;
Michal Vaskocd3f6172021-05-18 16:14:50 +02003863 struct lyd_dup_inst *child_dup_inst = NULL;
3864 LY_ERR ret;
3865 ly_bool first_inst = 0;
Michal Vasko4490d312020-06-16 13:08:55 +02003866
3867 sibling_src = *sibling_src_p;
Michal Vaskocd3f6172021-05-18 16:14:50 +02003868 if (!sibling_src->schema) {
3869 /* try to find the same opaque node */
3870 lyd_find_sibling_opaq_next(*first_trg, LYD_NAME(sibling_src), &match_trg);
3871 } else if (sibling_src->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) {
Michal Vasko4490d312020-06-16 13:08:55 +02003872 /* try to find the exact instance */
Michal Vaskocd3f6172021-05-18 16:14:50 +02003873 lyd_find_sibling_first(*first_trg, sibling_src, &match_trg);
Michal Vasko4490d312020-06-16 13:08:55 +02003874 } else {
3875 /* try to simply find the node, there cannot be more instances */
Michal Vaskocd3f6172021-05-18 16:14:50 +02003876 lyd_find_sibling_val(*first_trg, sibling_src->schema, NULL, 0, &match_trg);
Michal Vasko4490d312020-06-16 13:08:55 +02003877 }
3878
Michal Vaskocd3f6172021-05-18 16:14:50 +02003879 if (match_trg) {
3880 /* update match as needed */
3881 LY_CHECK_RET(lyd_dup_inst_next(&match_trg, *first_trg, dup_inst));
3882 } else {
3883 /* first instance of this node */
3884 first_inst = 1;
3885 }
3886
3887 if (match_trg) {
3888 /* call callback */
3889 if (merge_cb) {
3890 LY_CHECK_RET(merge_cb(match_trg, sibling_src, cb_data));
3891 }
3892
Michal Vasko4490d312020-06-16 13:08:55 +02003893 /* node found, make sure even value matches for all node types */
Michal Vaskofee29a92022-01-27 16:18:12 +01003894 if (!match_trg->schema) {
3895 if (lyd_compare_single(sibling_src, match_trg, 0)) {
3896 /* update value */
3897 opaq_trg = (struct lyd_node_opaq *)match_trg;
3898 opaq_src = (struct lyd_node_opaq *)sibling_src;
3899
3900 lydict_remove(LYD_CTX(opaq_trg), opaq_trg->value);
3901 lydict_insert(LYD_CTX(opaq_trg), opaq_src->value, 0, &opaq_trg->value);
3902 opaq_trg->hints = opaq_src->hints;
3903
3904 ly_free_prefix_data(opaq_trg->format, opaq_trg->val_prefix_data);
3905 opaq_trg->format = opaq_src->format;
3906 ly_dup_prefix_data(LYD_CTX(opaq_trg), opaq_src->format, opaq_src->val_prefix_data,
3907 &opaq_trg->val_prefix_data);
3908 }
3909 } else if ((match_trg->schema->nodetype == LYS_LEAF) &&
3910 lyd_compare_single(sibling_src, match_trg, LYD_COMPARE_DEFAULTS)) {
Michal Vasko4490d312020-06-16 13:08:55 +02003911 /* since they are different, they cannot both be default */
3912 assert(!(sibling_src->flags & LYD_DEFAULT) || !(match_trg->flags & LYD_DEFAULT));
3913
Michal Vasko3a41dff2020-07-15 14:30:28 +02003914 /* update value (or only LYD_DEFAULT flag) only if flag set or the source node is not default */
3915 if ((options & LYD_MERGE_DEFAULTS) || !(sibling_src->flags & LYD_DEFAULT)) {
Michal Vasko4490d312020-06-16 13:08:55 +02003916 type = ((struct lysc_node_leaf *)match_trg->schema)->type;
Michal Vaskob7be7a82020-08-20 09:09:04 +02003917 type->plugin->free(LYD_CTX(match_trg), &((struct lyd_node_term *)match_trg)->value);
3918 LY_CHECK_RET(type->plugin->duplicate(LYD_CTX(match_trg), &((struct lyd_node_term *)sibling_src)->value,
Michal Vasko69730152020-10-09 16:30:07 +02003919 &((struct lyd_node_term *)match_trg)->value));
Michal Vasko4490d312020-06-16 13:08:55 +02003920
3921 /* copy flags and add LYD_NEW */
Michal Vasko7e8315b2021-08-03 17:01:06 +02003922 match_trg->flags = sibling_src->flags | ((options & LYD_MERGE_WITH_FLAGS) ? 0 : LYD_NEW);
Michal Vasko4490d312020-06-16 13:08:55 +02003923 }
Michal Vasko8f359bf2020-07-28 10:41:15 +02003924 } else if ((match_trg->schema->nodetype & LYS_ANYDATA) && lyd_compare_single(sibling_src, match_trg, 0)) {
Michal Vasko4c583e82020-07-17 12:16:14 +02003925 /* update value */
3926 LY_CHECK_RET(lyd_any_copy_value(match_trg, &((struct lyd_node_any *)sibling_src)->value,
Radek Krejci0f969882020-08-21 16:56:47 +02003927 ((struct lyd_node_any *)sibling_src)->value_type));
Michal Vasko4490d312020-06-16 13:08:55 +02003928
3929 /* copy flags and add LYD_NEW */
Michal Vasko7e8315b2021-08-03 17:01:06 +02003930 match_trg->flags = sibling_src->flags | ((options & LYD_MERGE_WITH_FLAGS) ? 0 : LYD_NEW);
Michal Vaskocd3f6172021-05-18 16:14:50 +02003931 }
3932
3933 /* check descendants, recursively */
3934 ret = LY_SUCCESS;
3935 LY_LIST_FOR_SAFE(lyd_child_no_keys(sibling_src), tmp, child_src) {
3936 ret = lyd_merge_sibling_r(lyd_node_child_p(match_trg), match_trg, &child_src, merge_cb, cb_data, options,
3937 &child_dup_inst);
3938 if (ret) {
3939 break;
Michal Vasko4490d312020-06-16 13:08:55 +02003940 }
3941 }
Michal Vaskocd3f6172021-05-18 16:14:50 +02003942 lyd_dup_inst_free(child_dup_inst);
3943 LY_CHECK_RET(ret);
Michal Vasko4490d312020-06-16 13:08:55 +02003944 } else {
3945 /* node not found, merge it */
3946 if (options & LYD_MERGE_DESTRUCT) {
3947 dup_src = (struct lyd_node *)sibling_src;
3948 lyd_unlink_tree(dup_src);
3949 /* spend it */
3950 *sibling_src_p = NULL;
3951 } else {
Michal Vasko3a41dff2020-07-15 14:30:28 +02003952 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 +02003953 }
3954
Michal Vasko7e8315b2021-08-03 17:01:06 +02003955 if (!(options & LYD_MERGE_WITH_FLAGS)) {
3956 /* set LYD_NEW for all the new nodes, required for validation */
3957 LYD_TREE_DFS_BEGIN(dup_src, elem) {
3958 elem->flags |= LYD_NEW;
3959 LYD_TREE_DFS_END(dup_src, elem);
3960 }
Michal Vasko4490d312020-06-16 13:08:55 +02003961 }
3962
Michal Vaskocd3f6172021-05-18 16:14:50 +02003963 /* insert */
Michal Vasko6ee6f432021-07-16 09:49:14 +02003964 lyd_insert_node(parent_trg, first_trg, dup_src, 0);
Michal Vaskocd3f6172021-05-18 16:14:50 +02003965
3966 if (first_inst) {
3967 /* remember not to find this instance next time */
3968 LY_CHECK_RET(lyd_dup_inst_next(&dup_src, *first_trg, dup_inst));
3969 }
3970
3971 /* call callback, no source node */
3972 if (merge_cb) {
3973 LY_CHECK_RET(merge_cb(dup_src, NULL, cb_data));
3974 }
Michal Vasko4490d312020-06-16 13:08:55 +02003975 }
3976
3977 return LY_SUCCESS;
3978}
3979
Michal Vasko3a41dff2020-07-15 14:30:28 +02003980static LY_ERR
Michal Vaskocd3f6172021-05-18 16:14:50 +02003981lyd_merge(struct lyd_node **target, const struct lyd_node *source, const struct lys_module *mod,
3982 lyd_merge_cb merge_cb, void *cb_data, uint16_t options, ly_bool nosiblings)
Michal Vasko4490d312020-06-16 13:08:55 +02003983{
3984 const struct lyd_node *sibling_src, *tmp;
Michal Vaskocd3f6172021-05-18 16:14:50 +02003985 struct lyd_dup_inst *dup_inst = NULL;
Radek Krejci857189e2020-09-01 13:26:36 +02003986 ly_bool first;
Michal Vaskocd3f6172021-05-18 16:14:50 +02003987 LY_ERR ret = LY_SUCCESS;
Michal Vasko4490d312020-06-16 13:08:55 +02003988
3989 LY_CHECK_ARG_RET(NULL, target, LY_EINVAL);
Michal Vasko1d80a532021-11-24 10:41:05 +01003990 LY_CHECK_CTX_EQUAL_RET(*target ? LYD_CTX(*target) : NULL, source ? LYD_CTX(source) : NULL, mod ? mod->ctx : NULL,
3991 LY_EINVAL);
Michal Vasko4490d312020-06-16 13:08:55 +02003992
3993 if (!source) {
3994 /* nothing to merge */
3995 return LY_SUCCESS;
3996 }
3997
Michal Vasko831422b2020-11-24 11:20:51 +01003998 if ((*target && lysc_data_parent((*target)->schema)) || lysc_data_parent(source->schema)) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02003999 LOGERR(LYD_CTX(source), LY_EINVAL, "Invalid arguments - can merge only 2 top-level subtrees (%s()).", __func__);
Michal Vasko4490d312020-06-16 13:08:55 +02004000 return LY_EINVAL;
4001 }
4002
4003 LY_LIST_FOR_SAFE(source, tmp, sibling_src) {
Michal Vaskocd3f6172021-05-18 16:14:50 +02004004 if (mod && (lyd_owner_module(sibling_src) != mod)) {
4005 /* skip data nodes from different modules */
4006 continue;
4007 }
4008
Radek Krejci857189e2020-09-01 13:26:36 +02004009 first = (sibling_src == source) ? 1 : 0;
Michal Vaskocd3f6172021-05-18 16:14:50 +02004010 ret = lyd_merge_sibling_r(target, NULL, &sibling_src, merge_cb, cb_data, options, &dup_inst);
4011 if (ret) {
4012 break;
4013 }
Michal Vasko4490d312020-06-16 13:08:55 +02004014 if (first && !sibling_src) {
4015 /* source was spent (unlinked), move to the next node */
4016 source = tmp;
4017 }
4018
Michal Vasko3a41dff2020-07-15 14:30:28 +02004019 if (nosiblings) {
Michal Vasko4490d312020-06-16 13:08:55 +02004020 break;
4021 }
4022 }
4023
4024 if (options & LYD_MERGE_DESTRUCT) {
4025 /* free any leftover source data that were not merged */
4026 lyd_free_siblings((struct lyd_node *)source);
4027 }
4028
Michal Vaskocd3f6172021-05-18 16:14:50 +02004029 lyd_dup_inst_free(dup_inst);
4030 return ret;
Michal Vasko4490d312020-06-16 13:08:55 +02004031}
4032
Jan Kundrátc6e39de2021-12-09 16:01:19 +01004033LIBYANG_API_DEF LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004034lyd_merge_tree(struct lyd_node **target, const struct lyd_node *source, uint16_t options)
Michal Vasko3a41dff2020-07-15 14:30:28 +02004035{
Michal Vaskocd3f6172021-05-18 16:14:50 +02004036 return lyd_merge(target, source, NULL, NULL, NULL, options, 1);
Michal Vasko3a41dff2020-07-15 14:30:28 +02004037}
4038
Jan Kundrátc6e39de2021-12-09 16:01:19 +01004039LIBYANG_API_DEF LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004040lyd_merge_siblings(struct lyd_node **target, const struct lyd_node *source, uint16_t options)
Michal Vasko3a41dff2020-07-15 14:30:28 +02004041{
Michal Vaskocd3f6172021-05-18 16:14:50 +02004042 return lyd_merge(target, source, NULL, NULL, NULL, options, 0);
4043}
4044
Jan Kundrátc6e39de2021-12-09 16:01:19 +01004045LIBYANG_API_DEF LY_ERR
Michal Vaskocd3f6172021-05-18 16:14:50 +02004046lyd_merge_module(struct lyd_node **target, const struct lyd_node *source, const struct lys_module *mod,
4047 lyd_merge_cb merge_cb, void *cb_data, uint16_t options)
4048{
4049 return lyd_merge(target, source, mod, merge_cb, cb_data, options, 0);
Michal Vasko3a41dff2020-07-15 14:30:28 +02004050}
4051
Michal Vasko5ec7cda2019-09-11 13:43:08 +02004052static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02004053lyd_path_str_enlarge(char **buffer, size_t *buflen, size_t reqlen, ly_bool is_static)
Michal Vasko5ec7cda2019-09-11 13:43:08 +02004054{
Michal Vasko14654712020-02-06 08:35:21 +01004055 /* ending \0 */
4056 ++reqlen;
4057
Michal Vasko5ec7cda2019-09-11 13:43:08 +02004058 if (reqlen > *buflen) {
4059 if (is_static) {
4060 return LY_EINCOMPLETE;
4061 }
4062
4063 *buffer = ly_realloc(*buffer, reqlen * sizeof **buffer);
4064 if (!*buffer) {
4065 return LY_EMEM;
4066 }
4067
4068 *buflen = reqlen;
4069 }
4070
4071 return LY_SUCCESS;
4072}
4073
Michal Vaskod59035b2020-07-08 12:00:06 +02004074LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02004075lyd_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 +02004076{
4077 const struct lyd_node *key;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02004078 size_t len;
4079 const char *val;
4080 char quot;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02004081
Michal Vasko824d0832021-11-04 15:36:51 +01004082 for (key = lyd_child(node); key && key->schema && (key->schema->flags & LYS_KEY); key = key->next) {
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02004083 val = lyd_get_value(key);
Michal Vasko5ec7cda2019-09-11 13:43:08 +02004084 len = 1 + strlen(key->schema->name) + 2 + strlen(val) + 2;
Michal Vaskoba99a3e2020-08-18 15:50:05 +02004085 LY_CHECK_RET(lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static));
Michal Vasko5ec7cda2019-09-11 13:43:08 +02004086
4087 quot = '\'';
4088 if (strchr(val, '\'')) {
4089 quot = '"';
4090 }
4091 *bufused += sprintf(*buffer + *bufused, "[%s=%c%s%c]", key->schema->name, quot, val, quot);
Michal Vasko5ec7cda2019-09-11 13:43:08 +02004092 }
4093
4094 return LY_SUCCESS;
4095}
4096
4097/**
4098 * @brief Append leaf-list value predicate to path.
4099 *
4100 * @param[in] node Node to print.
4101 * @param[in,out] buffer Buffer to print to.
4102 * @param[in,out] buflen Current buffer length.
4103 * @param[in,out] bufused Current number of characters used in @p buffer.
4104 * @param[in] is_static Whether buffer is static or can be reallocated.
4105 * @return LY_ERR
4106 */
4107static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02004108lyd_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 +02004109{
Michal Vasko5ec7cda2019-09-11 13:43:08 +02004110 size_t len;
4111 const char *val;
4112 char quot;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02004113
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02004114 val = lyd_get_value(node);
Radek Krejcif13b87b2020-12-01 22:02:17 +01004115 len = 4 + strlen(val) + 2; /* "[.='" + val + "']" */
Michal Vaskoba99a3e2020-08-18 15:50:05 +02004116 LY_CHECK_RET(lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static));
Michal Vasko5ec7cda2019-09-11 13:43:08 +02004117
4118 quot = '\'';
4119 if (strchr(val, '\'')) {
4120 quot = '"';
4121 }
4122 *bufused += sprintf(*buffer + *bufused, "[.=%c%s%c]", quot, val, quot);
4123
Michal Vaskoba99a3e2020-08-18 15:50:05 +02004124 return LY_SUCCESS;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02004125}
4126
4127/**
4128 * @brief Append node position (relative to its other instances) predicate to path.
4129 *
4130 * @param[in] node Node to print.
4131 * @param[in,out] buffer Buffer to print to.
4132 * @param[in,out] buflen Current buffer length.
4133 * @param[in,out] bufused Current number of characters used in @p buffer.
4134 * @param[in] is_static Whether buffer is static or can be reallocated.
4135 * @return LY_ERR
4136 */
4137static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02004138lyd_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 +02004139{
Michal Vasko5ec7cda2019-09-11 13:43:08 +02004140 size_t len;
Michal Vasko50cc0562021-05-18 16:15:43 +02004141 uint32_t pos;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02004142 char *val = NULL;
4143 LY_ERR rc;
4144
Michal Vasko50cc0562021-05-18 16:15:43 +02004145 pos = lyd_list_pos(node);
4146 if (asprintf(&val, "%" PRIu32, pos) == -1) {
Michal Vasko5ec7cda2019-09-11 13:43:08 +02004147 return LY_EMEM;
4148 }
4149
4150 len = 1 + strlen(val) + 1;
4151 rc = lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static);
4152 if (rc != LY_SUCCESS) {
4153 goto cleanup;
4154 }
4155
4156 *bufused += sprintf(*buffer + *bufused, "[%s]", val);
4157
4158cleanup:
4159 free(val);
4160 return rc;
4161}
4162
Jan Kundrátc6e39de2021-12-09 16:01:19 +01004163LIBYANG_API_DEF char *
Michal Vasko5ec7cda2019-09-11 13:43:08 +02004164lyd_path(const struct lyd_node *node, LYD_PATH_TYPE pathtype, char *buffer, size_t buflen)
4165{
Radek Krejci857189e2020-09-01 13:26:36 +02004166 ly_bool is_static = 0;
Radek Krejci1deb5be2020-08-26 16:43:36 +02004167 uint32_t i, depth;
Michal Vasko14654712020-02-06 08:35:21 +01004168 size_t bufused = 0, len;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02004169 const struct lyd_node *iter;
4170 const struct lys_module *mod;
Michal Vasko790b2bc2020-08-03 13:35:06 +02004171 LY_ERR rc = LY_SUCCESS;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02004172
4173 LY_CHECK_ARG_RET(NULL, node, NULL);
4174 if (buffer) {
Michal Vasko16385f42021-05-18 16:16:09 +02004175 LY_CHECK_ARG_RET(LYD_CTX(node), buflen > 1, NULL);
Michal Vasko5ec7cda2019-09-11 13:43:08 +02004176 is_static = 1;
Michal Vasko14654712020-02-06 08:35:21 +01004177 } else {
4178 buflen = 0;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02004179 }
4180
4181 switch (pathtype) {
Radek Krejci635d2b82021-01-04 11:26:51 +01004182 case LYD_PATH_STD:
4183 case LYD_PATH_STD_NO_LAST_PRED:
Michal Vasko14654712020-02-06 08:35:21 +01004184 depth = 1;
Michal Vasko9e685082021-01-29 14:49:09 +01004185 for (iter = node; iter->parent; iter = lyd_parent(iter)) {
Michal Vasko5ec7cda2019-09-11 13:43:08 +02004186 ++depth;
4187 }
4188
Michal Vasko5ec7cda2019-09-11 13:43:08 +02004189 goto iter_print;
Michal Vasko14654712020-02-06 08:35:21 +01004190 while (depth) {
Michal Vasko5ec7cda2019-09-11 13:43:08 +02004191 /* find the right node */
Michal Vasko9e685082021-01-29 14:49:09 +01004192 for (iter = node, i = 1; i < depth; iter = lyd_parent(iter), ++i) {}
Michal Vasko5ec7cda2019-09-11 13:43:08 +02004193iter_print:
4194 /* print prefix and name */
4195 mod = NULL;
Michal Vasko9f1b4472022-01-06 13:35:08 +01004196 if (iter->schema && (!iter->parent || !iter->parent->schema ||
4197 (iter->schema->module != iter->parent->schema->module))) {
Michal Vasko5ec7cda2019-09-11 13:43:08 +02004198 mod = iter->schema->module;
4199 }
4200
4201 /* realloc string */
Michal Vaskoad92b672020-11-12 13:11:31 +01004202 len = 1 + (mod ? strlen(mod->name) + 1 : 0) + (iter->schema ? strlen(iter->schema->name) :
4203 strlen(((struct lyd_node_opaq *)iter)->name.name));
Michal Vasko5ec7cda2019-09-11 13:43:08 +02004204 rc = lyd_path_str_enlarge(&buffer, &buflen, bufused + len, is_static);
4205 if (rc != LY_SUCCESS) {
4206 break;
4207 }
4208
4209 /* print next node */
Radek Krejci1798aae2020-07-14 13:26:06 +02004210 bufused += sprintf(buffer + bufused, "/%s%s%s", mod ? mod->name : "", mod ? ":" : "",
Michal Vaskoad92b672020-11-12 13:11:31 +01004211 iter->schema ? iter->schema->name : ((struct lyd_node_opaq *)iter)->name.name);
Michal Vasko5ec7cda2019-09-11 13:43:08 +02004212
Michal Vasko790b2bc2020-08-03 13:35:06 +02004213 /* do not always print the last (first) predicate */
Radek Krejci635d2b82021-01-04 11:26:51 +01004214 if (iter->schema && ((depth > 1) || (pathtype == LYD_PATH_STD))) {
Michal Vasko790b2bc2020-08-03 13:35:06 +02004215 switch (iter->schema->nodetype) {
4216 case LYS_LIST:
4217 if (iter->schema->flags & LYS_KEYLESS) {
4218 /* print its position */
4219 rc = lyd_path_position_predicate(iter, &buffer, &buflen, &bufused, is_static);
4220 } else {
4221 /* print all list keys in predicates */
4222 rc = lyd_path_list_predicate(iter, &buffer, &buflen, &bufused, is_static);
4223 }
4224 break;
4225 case LYS_LEAFLIST:
4226 if (iter->schema->flags & LYS_CONFIG_W) {
4227 /* print leaf-list value */
4228 rc = lyd_path_leaflist_predicate(iter, &buffer, &buflen, &bufused, is_static);
4229 } else {
4230 /* print its position */
4231 rc = lyd_path_position_predicate(iter, &buffer, &buflen, &bufused, is_static);
4232 }
4233 break;
4234 default:
4235 /* nothing to print more */
4236 break;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02004237 }
Michal Vasko5ec7cda2019-09-11 13:43:08 +02004238 }
4239 if (rc != LY_SUCCESS) {
4240 break;
4241 }
4242
Michal Vasko14654712020-02-06 08:35:21 +01004243 --depth;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02004244 }
4245 break;
4246 }
4247
4248 return buffer;
4249}
Michal Vaskoe444f752020-02-10 12:20:06 +01004250
Jan Kundrátc6e39de2021-12-09 16:01:19 +01004251LIBYANG_API_DEF struct lyd_meta *
Michal Vasko25a32822020-07-09 15:48:22 +02004252lyd_find_meta(const struct lyd_meta *first, const struct lys_module *module, const char *name)
4253{
4254 struct lyd_meta *ret = NULL;
4255 const struct ly_ctx *ctx;
4256 const char *prefix, *tmp;
4257 char *str;
4258 size_t pref_len, name_len;
4259
4260 LY_CHECK_ARG_RET(NULL, module || strchr(name, ':'), name, NULL);
Michal Vasko1d80a532021-11-24 10:41:05 +01004261 LY_CHECK_CTX_EQUAL_RET(first ? first->annotation->module->ctx : NULL, module ? module->ctx : NULL, NULL);
Michal Vasko25a32822020-07-09 15:48:22 +02004262
4263 if (!first) {
4264 return NULL;
4265 }
4266
4267 ctx = first->annotation->module->ctx;
4268
4269 /* parse the name */
4270 tmp = name;
4271 if (ly_parse_nodeid(&tmp, &prefix, &pref_len, &name, &name_len) || tmp[0]) {
4272 LOGERR(ctx, LY_EINVAL, "Metadata name \"%s\" is not valid.", name);
4273 return NULL;
4274 }
4275
4276 /* find the module */
4277 if (prefix) {
4278 str = strndup(prefix, pref_len);
4279 module = ly_ctx_get_module_latest(ctx, str);
4280 free(str);
Radek Krejci422afb12021-03-04 16:38:16 +01004281 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 +02004282 }
4283
4284 /* find the metadata */
4285 LY_LIST_FOR(first, first) {
4286 if ((first->annotation->module == module) && !strcmp(first->name, name)) {
4287 ret = (struct lyd_meta *)first;
4288 break;
4289 }
4290 }
4291
4292 return ret;
4293}
4294
Jan Kundrátc6e39de2021-12-09 16:01:19 +01004295LIBYANG_API_DEF LY_ERR
Michal Vaskoe444f752020-02-10 12:20:06 +01004296lyd_find_sibling_first(const struct lyd_node *siblings, const struct lyd_node *target, struct lyd_node **match)
4297{
Michal Vaskoe78faec2021-04-08 17:24:43 +02004298 struct lyd_node **match_p, *iter;
Michal Vaskoe444f752020-02-10 12:20:06 +01004299 struct lyd_node_inner *parent;
Michal Vaskoe78faec2021-04-08 17:24:43 +02004300 ly_bool found;
Michal Vaskoe444f752020-02-10 12:20:06 +01004301
Michal Vaskof03ed032020-03-04 13:31:44 +01004302 LY_CHECK_ARG_RET(NULL, target, LY_EINVAL);
Michal Vasko1d80a532021-11-24 10:41:05 +01004303 LY_CHECK_CTX_EQUAL_RET(siblings ? LYD_CTX(siblings) : NULL, LYD_CTX(target), LY_EINVAL);
Michal Vaskoe444f752020-02-10 12:20:06 +01004304
Michal Vasko6da1e952020-12-09 18:14:38 +01004305 if (!siblings || (siblings->schema && target->schema &&
4306 (lysc_data_parent(siblings->schema) != lysc_data_parent(target->schema)))) {
Michal Vasko62ed12d2020-05-21 10:08:25 +02004307 /* no data or schema mismatch */
Michal Vasko9b368d32020-02-14 13:53:31 +01004308 if (match) {
4309 *match = NULL;
4310 }
Michal Vaskoe444f752020-02-10 12:20:06 +01004311 return LY_ENOTFOUND;
4312 }
4313
Michal Vaskoe78faec2021-04-08 17:24:43 +02004314 /* get first sibling */
4315 siblings = lyd_first_sibling(siblings);
Michal Vaskoe444f752020-02-10 12:20:06 +01004316
Michal Vasko9e685082021-01-29 14:49:09 +01004317 parent = siblings->parent;
Michal Vasko6da1e952020-12-09 18:14:38 +01004318 if (parent && parent->schema && parent->children_ht) {
Michal Vaskoe444f752020-02-10 12:20:06 +01004319 assert(target->hash);
4320
Michal Vaskoe78faec2021-04-08 17:24:43 +02004321 if (lysc_is_dup_inst_list(target->schema)) {
4322 /* we must search the instances from beginning to find the first matching one */
4323 found = 0;
4324 LYD_LIST_FOR_INST(siblings, target->schema, iter) {
4325 if (!lyd_compare_single(target, iter, 0)) {
4326 found = 1;
4327 break;
4328 }
4329 }
4330 if (found) {
4331 siblings = iter;
Michal Vaskoda859032020-07-14 12:20:14 +02004332 } else {
4333 siblings = NULL;
4334 }
Michal Vaskoe444f752020-02-10 12:20:06 +01004335 } else {
Michal Vaskoe78faec2021-04-08 17:24:43 +02004336 /* find by hash */
4337 if (!lyht_find(parent->children_ht, &target, target->hash, (void **)&match_p)) {
4338 siblings = *match_p;
4339 } else {
4340 /* not found */
4341 siblings = NULL;
4342 }
Michal Vaskoe444f752020-02-10 12:20:06 +01004343 }
4344 } else {
4345 /* no children hash table */
Michal Vaskod989ba02020-08-24 10:59:24 +02004346 for ( ; siblings; siblings = siblings->next) {
Michal Vasko8f359bf2020-07-28 10:41:15 +02004347 if (!lyd_compare_single(siblings, target, 0)) {
Michal Vaskoe444f752020-02-10 12:20:06 +01004348 break;
4349 }
4350 }
4351 }
4352
4353 if (!siblings) {
Michal Vasko9b368d32020-02-14 13:53:31 +01004354 if (match) {
4355 *match = NULL;
4356 }
Michal Vaskoe444f752020-02-10 12:20:06 +01004357 return LY_ENOTFOUND;
4358 }
4359
Michal Vasko9b368d32020-02-14 13:53:31 +01004360 if (match) {
4361 *match = (struct lyd_node *)siblings;
4362 }
Michal Vaskoe444f752020-02-10 12:20:06 +01004363 return LY_SUCCESS;
4364}
4365
Radek Krejci857189e2020-09-01 13:26:36 +02004366/**
4367 * @brief Comparison callback to match schema node with a schema of a data node.
4368 *
4369 * @param[in] val1_p Pointer to the schema node
4370 * @param[in] val2_p Pointer to the data node
Michal Vasko62524a92021-02-26 10:08:50 +01004371 * Implementation of ::lyht_value_equal_cb.
Radek Krejci857189e2020-09-01 13:26:36 +02004372 */
4373static ly_bool
4374lyd_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 +01004375{
4376 struct lysc_node *val1;
4377 struct lyd_node *val2;
4378
4379 val1 = *((struct lysc_node **)val1_p);
4380 val2 = *((struct lyd_node **)val2_p);
4381
Michal Vasko90932a92020-02-12 14:33:03 +01004382 if (val1 == val2->schema) {
4383 /* schema match is enough */
4384 return 1;
4385 } else {
4386 return 0;
4387 }
4388}
4389
Michal Vasko92239c72020-11-18 18:17:25 +01004390/**
4391 * @brief Search in the given siblings (NOT recursively) for the first schema node data instance.
4392 * Uses hashes - should be used whenever possible for best performance.
4393 *
4394 * @param[in] siblings Siblings to search in including preceding and succeeding nodes.
4395 * @param[in] schema Target data node schema to find.
4396 * @param[out] match Can be NULL, otherwise the found data node.
4397 * @return LY_SUCCESS on success, @p match set.
4398 * @return LY_ENOTFOUND if not found, @p match set to NULL.
4399 * @return LY_ERR value if another error occurred.
4400 */
Michal Vasko90932a92020-02-12 14:33:03 +01004401static LY_ERR
4402lyd_find_sibling_schema(const struct lyd_node *siblings, const struct lysc_node *schema, struct lyd_node **match)
4403{
4404 struct lyd_node **match_p;
4405 struct lyd_node_inner *parent;
4406 uint32_t hash;
Michal Vasko62524a92021-02-26 10:08:50 +01004407 lyht_value_equal_cb ht_cb;
Michal Vasko90932a92020-02-12 14:33:03 +01004408
Michal Vaskob104f112020-07-17 09:54:54 +02004409 assert(siblings && schema);
Michal Vasko90932a92020-02-12 14:33:03 +01004410
Michal Vasko9e685082021-01-29 14:49:09 +01004411 parent = siblings->parent;
Michal Vasko08fd6132020-11-18 18:17:45 +01004412 if (parent && parent->schema && parent->children_ht) {
Michal Vasko90932a92020-02-12 14:33:03 +01004413 /* calculate our hash */
4414 hash = dict_hash_multi(0, schema->module->name, strlen(schema->module->name));
4415 hash = dict_hash_multi(hash, schema->name, strlen(schema->name));
4416 hash = dict_hash_multi(hash, NULL, 0);
4417
4418 /* use special hash table function */
4419 ht_cb = lyht_set_cb(parent->children_ht, lyd_hash_table_schema_val_equal);
4420
4421 /* find by hash */
4422 if (!lyht_find(parent->children_ht, &schema, hash, (void **)&match_p)) {
4423 siblings = *match_p;
4424 } else {
4425 /* not found */
4426 siblings = NULL;
4427 }
4428
4429 /* set the original hash table compare function back */
4430 lyht_set_cb(parent->children_ht, ht_cb);
4431 } else {
Michal Vaskob104f112020-07-17 09:54:54 +02004432 /* find first sibling */
4433 if (siblings->parent) {
4434 siblings = siblings->parent->child;
4435 } else {
4436 while (siblings->prev->next) {
4437 siblings = siblings->prev;
4438 }
4439 }
4440
4441 /* search manually without hashes */
Michal Vaskod989ba02020-08-24 10:59:24 +02004442 for ( ; siblings; siblings = siblings->next) {
Michal Vasko90932a92020-02-12 14:33:03 +01004443 if (siblings->schema == schema) {
4444 /* schema match is enough */
4445 break;
4446 }
4447 }
4448 }
4449
4450 if (!siblings) {
Michal Vasko9b368d32020-02-14 13:53:31 +01004451 if (match) {
4452 *match = NULL;
4453 }
Michal Vasko90932a92020-02-12 14:33:03 +01004454 return LY_ENOTFOUND;
4455 }
4456
Michal Vasko9b368d32020-02-14 13:53:31 +01004457 if (match) {
4458 *match = (struct lyd_node *)siblings;
4459 }
Michal Vasko90932a92020-02-12 14:33:03 +01004460 return LY_SUCCESS;
4461}
4462
Jan Kundrátc6e39de2021-12-09 16:01:19 +01004463LIBYANG_API_DEF LY_ERR
Michal Vaskoe444f752020-02-10 12:20:06 +01004464lyd_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 +02004465 size_t val_len, struct lyd_node **match)
Michal Vaskoe444f752020-02-10 12:20:06 +01004466{
4467 LY_ERR rc;
4468 struct lyd_node *target = NULL;
4469
Michal Vasko4c583e82020-07-17 12:16:14 +02004470 LY_CHECK_ARG_RET(NULL, schema, !(schema->nodetype & (LYS_CHOICE | LYS_CASE)), LY_EINVAL);
Michal Vasko1d80a532021-11-24 10:41:05 +01004471 LY_CHECK_CTX_EQUAL_RET(siblings ? LYD_CTX(siblings) : NULL, schema->module->ctx, LY_EINVAL);
Michal Vaskoe444f752020-02-10 12:20:06 +01004472
Michal Vasko08fd6132020-11-18 18:17:45 +01004473 if (!siblings || (siblings->schema && (lysc_data_parent(siblings->schema) != lysc_data_parent(schema)))) {
Michal Vasko62ed12d2020-05-21 10:08:25 +02004474 /* no data or schema mismatch */
Michal Vasko9b368d32020-02-14 13:53:31 +01004475 if (match) {
4476 *match = NULL;
4477 }
Michal Vaskoe444f752020-02-10 12:20:06 +01004478 return LY_ENOTFOUND;
4479 }
4480
Michal Vaskof03ed032020-03-04 13:31:44 +01004481 if (key_or_value && !val_len) {
4482 val_len = strlen(key_or_value);
4483 }
4484
Michal Vaskob104f112020-07-17 09:54:54 +02004485 if ((schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) && key_or_value) {
4486 /* create a data node and find the instance */
4487 if (schema->nodetype == LYS_LEAFLIST) {
4488 /* target used attributes: schema, hash, value */
Radek Krejci8df109d2021-04-23 12:19:08 +02004489 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 +02004490 LY_CHECK_RET(rc);
Michal Vaskob104f112020-07-17 09:54:54 +02004491 } else {
Michal Vasko90932a92020-02-12 14:33:03 +01004492 /* target used attributes: schema, hash, child (all keys) */
Michal Vasko004d3152020-06-11 19:59:22 +02004493 LY_CHECK_RET(lyd_create_list2(schema, key_or_value, val_len, &target));
Michal Vasko90932a92020-02-12 14:33:03 +01004494 }
4495
4496 /* find it */
4497 rc = lyd_find_sibling_first(siblings, target, match);
Michal Vaskob104f112020-07-17 09:54:54 +02004498 } else {
4499 /* find the first schema node instance */
4500 rc = lyd_find_sibling_schema(siblings, schema, match);
Michal Vaskoe444f752020-02-10 12:20:06 +01004501 }
4502
Michal Vaskoe444f752020-02-10 12:20:06 +01004503 lyd_free_tree(target);
4504 return rc;
4505}
Michal Vaskoccc02342020-05-21 10:09:21 +02004506
Jan Kundrátc6e39de2021-12-09 16:01:19 +01004507LIBYANG_API_DEF LY_ERR
Michal Vaskoe78faec2021-04-08 17:24:43 +02004508lyd_find_sibling_dup_inst_set(const struct lyd_node *siblings, const struct lyd_node *target, struct ly_set **set)
4509{
4510 struct lyd_node **match_p, *first, *iter;
4511 struct lyd_node_inner *parent;
4512
4513 LY_CHECK_ARG_RET(NULL, target, lysc_is_dup_inst_list(target->schema), set, LY_EINVAL);
Michal Vasko1d80a532021-11-24 10:41:05 +01004514 LY_CHECK_CTX_EQUAL_RET(siblings ? LYD_CTX(siblings) : NULL, LYD_CTX(target), LY_EINVAL);
Michal Vaskoe78faec2021-04-08 17:24:43 +02004515
4516 LY_CHECK_RET(ly_set_new(set));
4517
4518 if (!siblings || (siblings->schema && (lysc_data_parent(siblings->schema) != lysc_data_parent(target->schema)))) {
4519 /* no data or schema mismatch */
4520 return LY_ENOTFOUND;
4521 }
4522
4523 /* get first sibling */
4524 siblings = lyd_first_sibling(siblings);
4525
4526 parent = siblings->parent;
4527 if (parent && parent->schema && parent->children_ht) {
4528 assert(target->hash);
4529
4530 /* find the first instance */
4531 lyd_find_sibling_first(siblings, target, &first);
4532 if (first) {
4533 /* add it so that it is the first in the set */
4534 if (ly_set_add(*set, first, 1, NULL)) {
4535 goto error;
4536 }
4537
4538 /* find by hash */
4539 if (!lyht_find(parent->children_ht, &target, target->hash, (void **)&match_p)) {
4540 iter = *match_p;
4541 } else {
4542 /* not found */
4543 iter = NULL;
4544 }
4545 while (iter) {
4546 /* add all found nodes into the set */
4547 if ((iter != first) && !lyd_compare_single(iter, target, 0) && ly_set_add(*set, iter, 1, NULL)) {
4548 goto error;
4549 }
4550
4551 /* find next instance */
4552 if (lyht_find_next(parent->children_ht, &iter, iter->hash, (void **)&match_p)) {
4553 iter = NULL;
4554 } else {
4555 iter = *match_p;
4556 }
4557 }
4558 }
4559 } else {
4560 /* no children hash table */
4561 LY_LIST_FOR(siblings, siblings) {
4562 if (!lyd_compare_single(target, siblings, 0)) {
4563 ly_set_add(*set, (void *)siblings, 1, NULL);
4564 }
4565 }
4566 }
4567
4568 if (!(*set)->count) {
4569 return LY_ENOTFOUND;
4570 }
4571 return LY_SUCCESS;
4572
4573error:
4574 ly_set_free(*set, NULL);
4575 *set = NULL;
4576 return LY_EMEM;
4577}
4578
Jan Kundrátc6e39de2021-12-09 16:01:19 +01004579LIBYANG_API_DEF LY_ERR
Michal Vasko1d4af6c2021-02-22 13:31:26 +01004580lyd_find_sibling_opaq_next(const struct lyd_node *first, const char *name, struct lyd_node **match)
4581{
4582 LY_CHECK_ARG_RET(NULL, name, LY_EINVAL);
4583
4584 for ( ; first; first = first->next) {
4585 if (!first->schema && !strcmp(LYD_NAME(first), name)) {
4586 break;
4587 }
4588 }
4589
4590 if (match) {
4591 *match = (struct lyd_node *)first;
4592 }
4593 return first ? LY_SUCCESS : LY_ENOTFOUND;
4594}
4595
Jan Kundrátc6e39de2021-12-09 16:01:19 +01004596LIBYANG_API_DEF LY_ERR
Michal Vaskoaac267d2022-01-17 13:34:48 +01004597lyd_find_xpath4(const struct lyd_node *ctx_node, const struct lyd_node *tree, const char *xpath, LY_VALUE_FORMAT format,
4598 void *prefix_data, const struct lyxp_var *vars, struct ly_set **set)
Michal Vaskoccc02342020-05-21 10:09:21 +02004599{
4600 LY_ERR ret = LY_SUCCESS;
Michal Vasko8bd9cc72021-07-16 14:06:10 +02004601 struct lyxp_set xp_set = {0};
Radek Krejcif03a9e22020-09-18 20:09:31 +02004602 struct lyxp_expr *exp = NULL;
Michal Vaskoccc02342020-05-21 10:09:21 +02004603 uint32_t i;
4604
Michal Vaskoaac267d2022-01-17 13:34:48 +01004605 LY_CHECK_ARG_RET(NULL, tree, xpath, format, set, LY_EINVAL);
Michal Vaskoccc02342020-05-21 10:09:21 +02004606
Michal Vasko37c69432021-04-12 14:48:24 +02004607 *set = NULL;
Michal Vaskoccc02342020-05-21 10:09:21 +02004608
Michal Vaskoaac267d2022-01-17 13:34:48 +01004609 /* parse expression */
Michal Vasko279539d2021-12-13 16:58:25 +01004610 ret = lyxp_expr_parse((struct ly_ctx *)LYD_CTX(tree), xpath, 0, 1, &exp);
Radek Krejcif03a9e22020-09-18 20:09:31 +02004611 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskoccc02342020-05-21 10:09:21 +02004612
4613 /* evaluate expression */
Michal Vaskoaac267d2022-01-17 13:34:48 +01004614 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 +02004615 LY_CHECK_GOTO(ret, cleanup);
4616
4617 /* allocate return set */
Radek Krejciba03a5a2020-08-27 14:40:41 +02004618 ret = ly_set_new(set);
4619 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskoccc02342020-05-21 10:09:21 +02004620
4621 /* transform into ly_set */
4622 if (xp_set.type == LYXP_SET_NODE_SET) {
4623 /* allocate memory for all the elements once (even though not all items must be elements but most likely will be) */
4624 (*set)->objs = malloc(xp_set.used * sizeof *(*set)->objs);
Michal Vasko279539d2021-12-13 16:58:25 +01004625 LY_CHECK_ERR_GOTO(!(*set)->objs, LOGMEM(LYD_CTX(tree)); ret = LY_EMEM, cleanup);
Michal Vaskoccc02342020-05-21 10:09:21 +02004626 (*set)->size = xp_set.used;
4627
4628 for (i = 0; i < xp_set.used; ++i) {
4629 if (xp_set.val.nodes[i].type == LYXP_NODE_ELEM) {
Radek Krejci3d92e442020-10-12 12:48:13 +02004630 ret = ly_set_add(*set, xp_set.val.nodes[i].node, 1, NULL);
Radek Krejciba03a5a2020-08-27 14:40:41 +02004631 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskoccc02342020-05-21 10:09:21 +02004632 }
4633 }
4634 }
4635
4636cleanup:
Michal Vasko0691d522020-05-21 13:21:47 +02004637 lyxp_set_free_content(&xp_set);
Michal Vasko279539d2021-12-13 16:58:25 +01004638 lyxp_expr_free((struct ly_ctx *)LYD_CTX(tree), exp);
Radek Krejci8f45abc2020-11-26 12:15:13 +01004639 if (ret) {
4640 ly_set_free(*set, NULL);
4641 *set = NULL;
4642 }
Michal Vaskoccc02342020-05-21 10:09:21 +02004643 return ret;
4644}
Radek Krejcica989142020-11-05 11:32:22 +01004645
Jan Kundrátc6e39de2021-12-09 16:01:19 +01004646LIBYANG_API_DEF LY_ERR
Michal Vaskoaac267d2022-01-17 13:34:48 +01004647lyd_find_xpath3(const struct lyd_node *ctx_node, const struct lyd_node *tree, const char *xpath,
4648 const struct lyxp_var *vars, struct ly_set **set)
4649{
4650 LY_CHECK_ARG_RET(NULL, tree, xpath, set, LY_EINVAL);
4651
4652 return lyd_find_xpath4(ctx_node, tree, xpath, LY_VALUE_JSON, NULL, vars, set);
4653}
4654
4655LIBYANG_API_DEF LY_ERR
Michal Vasko279539d2021-12-13 16:58:25 +01004656lyd_find_xpath2(const struct lyd_node *ctx_node, const char *xpath, const struct lyxp_var *vars, struct ly_set **set)
4657{
4658 LY_CHECK_ARG_RET(NULL, ctx_node, xpath, set, LY_EINVAL);
4659
Michal Vaskoaac267d2022-01-17 13:34:48 +01004660 return lyd_find_xpath4(ctx_node, ctx_node, xpath, LY_VALUE_JSON, NULL, vars, set);
Michal Vasko279539d2021-12-13 16:58:25 +01004661}
4662
Jan Kundrátc6e39de2021-12-09 16:01:19 +01004663LIBYANG_API_DEF LY_ERR
aPiecekfba75362021-10-07 12:39:48 +02004664lyd_find_xpath(const struct lyd_node *ctx_node, const char *xpath, struct ly_set **set)
4665{
Michal Vasko279539d2021-12-13 16:58:25 +01004666 LY_CHECK_ARG_RET(NULL, ctx_node, xpath, set, LY_EINVAL);
4667
Michal Vaskoaac267d2022-01-17 13:34:48 +01004668 return lyd_find_xpath4(ctx_node, ctx_node, xpath, LY_VALUE_JSON, NULL, NULL, set);
aPiecekfba75362021-10-07 12:39:48 +02004669}
4670
Jan Kundrátc6e39de2021-12-09 16:01:19 +01004671LIBYANG_API_DEF LY_ERR
aPiecekfba75362021-10-07 12:39:48 +02004672lyd_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 +02004673{
4674 LY_ERR ret = LY_SUCCESS;
Michal Vasko8bd9cc72021-07-16 14:06:10 +02004675 struct lyxp_set xp_set = {0};
Michal Vaskoc9eb3ca2021-07-16 10:20:37 +02004676 struct lyxp_expr *exp = NULL;
4677
4678 LY_CHECK_ARG_RET(NULL, ctx_node, xpath, result, LY_EINVAL);
4679
4680 /* compile expression */
4681 ret = lyxp_expr_parse((struct ly_ctx *)LYD_CTX(ctx_node), xpath, 0, 1, &exp);
4682 LY_CHECK_GOTO(ret, cleanup);
4683
4684 /* evaluate expression */
aPiecekfba75362021-10-07 12:39:48 +02004685 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 +02004686 LY_CHECK_GOTO(ret, cleanup);
4687
4688 /* transform into boolean */
4689 ret = lyxp_set_cast(&xp_set, LYXP_SET_BOOLEAN);
4690 LY_CHECK_GOTO(ret, cleanup);
4691
4692 /* set result */
4693 *result = xp_set.val.bln;
4694
4695cleanup:
4696 lyxp_set_free_content(&xp_set);
4697 lyxp_expr_free((struct ly_ctx *)LYD_CTX(ctx_node), exp);
4698 return ret;
4699}
4700
Jan Kundrátc6e39de2021-12-09 16:01:19 +01004701LIBYANG_API_DEF LY_ERR
aPiecekfba75362021-10-07 12:39:48 +02004702lyd_eval_xpath(const struct lyd_node *ctx_node, const char *xpath, ly_bool *result)
4703{
4704 return lyd_eval_xpath2(ctx_node, xpath, NULL, result);
4705}
4706
Jan Kundrátc6e39de2021-12-09 16:01:19 +01004707LIBYANG_API_DEF LY_ERR
Michal Vasko3e1f6552021-01-14 09:27:55 +01004708lyd_find_path(const struct lyd_node *ctx_node, const char *path, ly_bool output, struct lyd_node **match)
4709{
4710 LY_ERR ret = LY_SUCCESS;
4711 struct lyxp_expr *expr = NULL;
4712 struct ly_path *lypath = NULL;
4713
4714 LY_CHECK_ARG_RET(NULL, ctx_node, ctx_node->schema, path, LY_EINVAL);
4715
4716 /* parse the path */
Michal Vaskoed725d72021-06-23 12:03:45 +02004717 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 +01004718 LY_PATH_PREFIX_OPTIONAL, LY_PATH_PRED_SIMPLE, &expr);
4719 LY_CHECK_GOTO(ret, cleanup);
4720
4721 /* compile the path */
Michal Vaskoed725d72021-06-23 12:03:45 +02004722 ret = ly_path_compile(LYD_CTX(ctx_node), NULL, ctx_node->schema, NULL, expr,
Michal Vasko0884d212021-10-14 09:21:46 +02004723 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 +01004724 LY_CHECK_GOTO(ret, cleanup);
4725
4726 /* evaluate the path */
4727 ret = ly_path_eval_partial(lypath, ctx_node, NULL, match);
4728
4729cleanup:
4730 lyxp_expr_free(LYD_CTX(ctx_node), expr);
4731 ly_path_free(LYD_CTX(ctx_node), lypath);
4732 return ret;
4733}
4734
Jan Kundrátc6e39de2021-12-09 16:01:19 +01004735LIBYANG_API_DEF LY_ERR
Michal Vaskobb22b182021-06-14 08:14:21 +02004736lyd_find_target(const struct ly_path *path, const struct lyd_node *tree, struct lyd_node **match)
4737{
4738 LY_ERR ret;
4739 struct lyd_node *m;
4740
4741 LY_CHECK_ARG_RET(NULL, path, LY_EINVAL);
4742
4743 ret = ly_path_eval(path, tree, &m);
4744 if (ret) {
4745 if (match) {
4746 *match = NULL;
4747 }
4748 return LY_ENOTFOUND;
4749 }
4750
4751 if (match) {
4752 *match = m;
4753 }
4754 return LY_SUCCESS;
4755}
4756
Jan Kundrátc6e39de2021-12-09 16:01:19 +01004757LIBYANG_API_DEF uint32_t
Radek Krejcica989142020-11-05 11:32:22 +01004758lyd_list_pos(const struct lyd_node *instance)
4759{
4760 const struct lyd_node *iter = NULL;
4761 uint32_t pos = 0;
4762
4763 if (!instance || !(instance->schema->nodetype & (LYS_LIST | LYS_LEAFLIST))) {
4764 return 0;
4765 }
4766
4767 /* data instances are ordered, so we can stop when we found instance of other schema node */
4768 for (iter = instance; iter->schema == instance->schema; iter = iter->prev) {
Radek Krejcibb27df32020-11-13 15:39:16 +01004769 if (pos && (iter->next == NULL)) {
Radek Krejcica989142020-11-05 11:32:22 +01004770 /* overrun to the end of the siblings list */
4771 break;
4772 }
4773 ++pos;
4774 }
4775
4776 return pos;
4777}
Radek Krejci4233f9b2020-11-05 12:38:35 +01004778
Jan Kundrátc6e39de2021-12-09 16:01:19 +01004779LIBYANG_API_DEF struct lyd_node *
Radek Krejci4233f9b2020-11-05 12:38:35 +01004780lyd_first_sibling(const struct lyd_node *node)
4781{
4782 struct lyd_node *start;
4783
4784 if (!node) {
4785 return NULL;
4786 }
4787
4788 /* get the first sibling */
4789 if (node->parent) {
4790 start = node->parent->child;
4791 } else {
Radek Krejcibb27df32020-11-13 15:39:16 +01004792 for (start = (struct lyd_node *)node; start->prev->next; start = start->prev) {}
Radek Krejci4233f9b2020-11-05 12:38:35 +01004793 }
4794
4795 return start;
4796}