blob: 328af886d0d12df94a58d40d9f91498174e458df [file] [log] [blame]
Radek Krejcie7b95092019-05-15 11:03:07 +02001/**
Michal Vaskob1b5c262020-03-05 14:29:47 +01002 * @file tree_data.c
Radek Krejcie7b95092019-05-15 11:03:07 +02003 * @author Radek Krejci <rkrejci@cesnet.cz>
Michal Vasko6cd9b6b2020-06-22 10:05:22 +02004 * @brief Data tree functions
Radek Krejcie7b95092019-05-15 11:03:07 +02005 *
Michal Vasko6cd9b6b2020-06-22 10:05:22 +02006 * Copyright (c) 2015 - 2020 CESNET, z.s.p.o.
Radek Krejcie7b95092019-05-15 11:03:07 +02007 *
8 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * https://opensource.org/licenses/BSD-3-Clause
13 */
14
Radek Krejci535ea9f2020-05-29 16:01:05 +020015#define _GNU_SOURCE
16
17#include "tree_data.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020018
Radek Krejci084289f2019-07-09 17:35:30 +020019#include <assert.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020020#include <ctype.h>
Radek Krejci47fab892020-11-05 17:02:41 +010021#include <inttypes.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020022#include <stdarg.h>
23#include <stdint.h>
Radek Krejci535ea9f2020-05-29 16:01:05 +020024#include <stdio.h>
25#include <stdlib.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020026#include <string.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020027
Radek Krejci535ea9f2020-05-29 16:01:05 +020028#include "common.h"
Michal Vasko5aa44c02020-06-29 11:47:02 +020029#include "compat.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020030#include "context.h"
31#include "dict.h"
Michal Vaskoa6669ba2020-08-06 16:14:26 +020032#include "diff.h"
Michal Vasko90932a92020-02-12 14:33:03 +010033#include "hash_table.h"
Radek Krejci47fab892020-11-05 17:02:41 +010034#include "in.h"
Michal Vaskoafac7822020-10-20 14:22:26 +020035#include "in_internal.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020036#include "log.h"
Radek Krejci7931b192020-06-25 17:05:03 +020037#include "parser_data.h"
38#include "parser_internal.h"
Michal Vasko004d3152020-06-11 19:59:22 +020039#include "path.h"
Radek Krejci0aa1f702021-04-01 16:16:19 +020040#include "plugins.h"
Radek Krejcif1ca0ac2021-04-12 16:00:06 +020041#include "plugins_exts/metadata.h"
Radek Krejci3e6632f2021-03-22 22:08:21 +010042#include "plugins_internal.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020043#include "plugins_types.h"
44#include "set.h"
45#include "tree.h"
46#include "tree_data_internal.h"
Radek Krejci859a15a2021-03-05 20:56:59 +010047#include "tree_edit.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020048#include "tree_schema.h"
49#include "tree_schema_internal.h"
Michal Vaskoa6669ba2020-08-06 16:14:26 +020050#include "validation.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020051#include "xml.h"
52#include "xpath.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020053
Michal Vaskob104f112020-07-17 09:54:54 +020054static LY_ERR lyd_find_sibling_schema(const struct lyd_node *siblings, const struct lysc_node *schema,
Radek Krejci0f969882020-08-21 16:56:47 +020055 struct lyd_node **match);
Michal Vaskob104f112020-07-17 09:54:54 +020056
Radek Krejci084289f2019-07-09 17:35:30 +020057LY_ERR
Radek Krejcif9943642021-04-26 10:18:21 +020058lyd_value_store(const struct ly_ctx *ctx, struct lyd_value *val, const struct lysc_type *type, const void *value,
Radek Krejci8df109d2021-04-23 12:19:08 +020059 size_t value_len, ly_bool *dynamic, LY_VALUE_FORMAT format, void *prefix_data, uint32_t hints,
Radek Krejci2efc45b2020-12-22 16:25:44 +010060 const struct lysc_node *ctx_node, ly_bool *incomplete)
Radek Krejci084289f2019-07-09 17:35:30 +020061{
Michal Vaskofeca4fb2020-10-05 08:58:40 +020062 LY_ERR ret;
Radek Krejci084289f2019-07-09 17:35:30 +020063 struct ly_err_item *err = NULL;
Radek Krejci0b013302021-03-29 15:22:32 +020064 uint32_t options = (dynamic && *dynamic ? LYPLG_TYPE_STORE_DYNAMIC : 0);
Radek Krejci084289f2019-07-09 17:35:30 +020065
Michal Vasko62ed8652021-04-28 09:38:37 +020066 if (!value) {
67 value = "";
68 }
Michal Vaskofeca4fb2020-10-05 08:58:40 +020069 if (incomplete) {
70 *incomplete = 0;
Radek Krejci084289f2019-07-09 17:35:30 +020071 }
72
Michal Vasko405cc9e2020-12-01 12:01:27 +010073 ret = type->plugin->store(ctx, type, value, value_len, options, format, prefix_data, hints, ctx_node, val, NULL, &err);
Radek Iša96b2de12021-02-16 10:32:57 +010074 if (dynamic) {
75 *dynamic = 0;
76 }
77
Michal Vasko90932a92020-02-12 14:33:03 +010078 if (ret == LY_EINCOMPLETE) {
Michal Vaskofeca4fb2020-10-05 08:58:40 +020079 if (incomplete) {
80 *incomplete = 1;
81 }
82 } else if (ret) {
83 if (err) {
aPiecek4204b612021-05-03 09:06:16 +020084 LOGVAL_ERRITEM(ctx, err);
Michal Vaskofeca4fb2020-10-05 08:58:40 +020085 ly_err_free(err);
86 } else {
Michal Vasko509d7212022-02-21 08:47:23 +010087 LOGVAL(ctx, LYVE_OTHER, "Storing value failed.");
Michal Vaskofeca4fb2020-10-05 08:58:40 +020088 }
89 return ret;
Michal Vasko90932a92020-02-12 14:33:03 +010090 }
91
Michal Vaskofeca4fb2020-10-05 08:58:40 +020092 return LY_SUCCESS;
Michal Vasko90932a92020-02-12 14:33:03 +010093}
94
Radek Krejci38d85362019-09-05 16:26:38 +020095LY_ERR
Michal Vaskofeca4fb2020-10-05 08:58:40 +020096lyd_value_validate_incomplete(const struct ly_ctx *ctx, const struct lysc_type *type, struct lyd_value *val,
Radek Krejci2efc45b2020-12-22 16:25:44 +010097 const struct lyd_node *ctx_node, const struct lyd_node *tree)
Radek Krejci38d85362019-09-05 16:26:38 +020098{
Michal Vaskofeca4fb2020-10-05 08:58:40 +020099 LY_ERR ret;
Radek Krejci38d85362019-09-05 16:26:38 +0200100 struct ly_err_item *err = NULL;
Radek Krejci38d85362019-09-05 16:26:38 +0200101
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200102 assert(type->plugin->validate);
Michal Vasko8d544252020-03-02 10:19:52 +0100103
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200104 ret = type->plugin->validate(ctx, type, ctx_node, tree, val, &err);
105 if (ret) {
Radek Krejci38d85362019-09-05 16:26:38 +0200106 if (err) {
aPiecek4204b612021-05-03 09:06:16 +0200107 LOGVAL_ERRITEM(ctx, err);
Radek Krejci38d85362019-09-05 16:26:38 +0200108 ly_err_free(err);
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200109 } else {
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200110 LOGVAL(ctx, LYVE_OTHER, "Resolving value \"%s\" failed.", type->plugin->print(ctx, val, LY_VALUE_CANON,
111 NULL, NULL, NULL));
Radek Krejci38d85362019-09-05 16:26:38 +0200112 }
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200113 return ret;
Radek Krejci38d85362019-09-05 16:26:38 +0200114 }
115
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200116 return LY_SUCCESS;
Radek Krejci38d85362019-09-05 16:26:38 +0200117}
118
Michal Vaskof937cfe2020-08-03 16:07:12 +0200119LY_ERR
Michal Vaskoaebbce02021-04-06 09:23:37 +0200120lys_value_validate(const struct ly_ctx *ctx, const struct lysc_node *node, const char *value, size_t value_len,
Radek Krejci8df109d2021-04-23 12:19:08 +0200121 LY_VALUE_FORMAT format, void *prefix_data)
Radek Krejci084289f2019-07-09 17:35:30 +0200122{
123 LY_ERR rc = LY_SUCCESS;
124 struct ly_err_item *err = NULL;
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200125 struct lyd_value storage;
Radek Krejci084289f2019-07-09 17:35:30 +0200126 struct lysc_type *type;
Radek Krejci084289f2019-07-09 17:35:30 +0200127
128 LY_CHECK_ARG_RET(ctx, node, value, LY_EINVAL);
129
130 if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
131 LOGARG(ctx, node);
132 return LY_EINVAL;
133 }
134
Michal Vasko22df3f02020-08-24 13:29:22 +0200135 type = ((struct lysc_node_leaf *)node)->type;
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200136 rc = type->plugin->store(ctx ? ctx : node->module->ctx, type, value, value_len, 0, format, prefix_data,
Michal Vasko405cc9e2020-12-01 12:01:27 +0100137 LYD_HINT_SCHEMA, node, &storage, NULL, &err);
Radek Krejci73dead22019-07-11 16:46:16 +0200138 if (rc == LY_EINCOMPLETE) {
139 /* actually success since we do not provide the context tree and call validation with
140 * LY_TYPE_OPTS_INCOMPLETE_DATA */
141 rc = LY_SUCCESS;
142 } else if (rc && err) {
143 if (ctx) {
144 /* log only in case the ctx was provided as input parameter */
Radek Krejciddace2c2021-01-08 11:30:56 +0100145 LOG_LOCSET(NULL, NULL, err->path, NULL);
aPiecek4204b612021-05-03 09:06:16 +0200146 LOGVAL_ERRITEM(ctx, err);
Radek Krejciddace2c2021-01-08 11:30:56 +0100147 LOG_LOCBACK(0, 0, 1, 0);
Radek Krejci084289f2019-07-09 17:35:30 +0200148 }
Radek Krejci73dead22019-07-11 16:46:16 +0200149 ly_err_free(err);
Radek Krejci084289f2019-07-09 17:35:30 +0200150 }
151
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200152 if (!rc) {
153 type->plugin->free(ctx ? ctx : node->module->ctx, &storage);
154 }
Radek Krejci084289f2019-07-09 17:35:30 +0200155 return rc;
156}
157
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100158LIBYANG_API_DEF LY_ERR
Michal Vaskoaebbce02021-04-06 09:23:37 +0200159lyd_value_validate(const struct ly_ctx *ctx, const struct lysc_node *schema, const char *value, size_t value_len,
160 const struct lyd_node *ctx_node, const struct lysc_type **realtype, const char **canonical)
Radek Krejci084289f2019-07-09 17:35:30 +0200161{
162 LY_ERR rc;
163 struct ly_err_item *err = NULL;
164 struct lysc_type *type;
Michal Vasko3701af52020-08-03 14:29:38 +0200165 struct lyd_value val = {0};
Radek Krejci09c77442021-04-26 11:10:34 +0200166 ly_bool stored = 0, log = 1;
Radek Krejci084289f2019-07-09 17:35:30 +0200167
Michal Vaskoaebbce02021-04-06 09:23:37 +0200168 LY_CHECK_ARG_RET(ctx, schema, value, LY_EINVAL);
Radek Krejci084289f2019-07-09 17:35:30 +0200169
Radek Krejci09c77442021-04-26 11:10:34 +0200170 if (!ctx) {
171 ctx = schema->module->ctx;
172 log = 0;
173 }
Michal Vaskoaebbce02021-04-06 09:23:37 +0200174 type = ((struct lysc_node_leaf *)schema)->type;
175
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200176 /* store */
Radek Krejci09c77442021-04-26 11:10:34 +0200177 rc = type->plugin->store(ctx, type, value, value_len, 0, LY_VALUE_JSON, NULL,
Michal Vaskoaebbce02021-04-06 09:23:37 +0200178 LYD_HINT_DATA, schema, &val, NULL, &err);
179 if (!rc || (rc == LY_EINCOMPLETE)) {
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200180 stored = 1;
Michal Vaskoaebbce02021-04-06 09:23:37 +0200181 }
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200182
Michal Vaskoaebbce02021-04-06 09:23:37 +0200183 if (ctx_node && (rc == LY_EINCOMPLETE)) {
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200184 /* resolve */
Radek Krejci09c77442021-04-26 11:10:34 +0200185 rc = type->plugin->validate(ctx, type, ctx_node, ctx_node, &val, &err);
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200186 }
187
Michal Vaskoaebbce02021-04-06 09:23:37 +0200188 if (rc && (rc != LY_EINCOMPLETE) && err) {
Radek Krejci09c77442021-04-26 11:10:34 +0200189 if (log) {
Michal Vaskoaebbce02021-04-06 09:23:37 +0200190 /* log error */
191 if (err->path) {
192 LOG_LOCSET(NULL, NULL, err->path, NULL);
193 } else if (ctx_node) {
194 LOG_LOCSET(NULL, ctx_node, NULL, NULL);
195 } else {
196 LOG_LOCSET(schema, NULL, NULL, NULL);
Radek Krejci084289f2019-07-09 17:35:30 +0200197 }
aPiecek4204b612021-05-03 09:06:16 +0200198 LOGVAL_ERRITEM(ctx, err);
Michal Vaskoaebbce02021-04-06 09:23:37 +0200199 if (err->path) {
200 LOG_LOCBACK(0, 0, 1, 0);
201 } else if (ctx_node) {
202 LOG_LOCBACK(0, 1, 0, 0);
203 } else {
204 LOG_LOCBACK(1, 0, 0, 0);
205 }
Radek Krejci084289f2019-07-09 17:35:30 +0200206 }
Michal Vaskoaebbce02021-04-06 09:23:37 +0200207 ly_err_free(err);
Radek Krejci084289f2019-07-09 17:35:30 +0200208 }
209
Michal Vaskoaebbce02021-04-06 09:23:37 +0200210 if (!rc || (rc == LY_EINCOMPLETE)) {
211 if (realtype) {
212 /* return realtype */
213 if (val.realtype->basetype == LY_TYPE_UNION) {
214 *realtype = val.subvalue->value.realtype;
215 } else {
216 *realtype = val.realtype;
217 }
218 }
219
220 if (canonical) {
221 /* return canonical value */
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200222 lydict_insert(ctx, val.realtype->plugin->print(ctx, &val, LY_VALUE_CANON, NULL, NULL, NULL), 0, canonical);
Michal Vasko29134f82020-11-13 18:03:20 +0100223 }
Michal Vasko3701af52020-08-03 14:29:38 +0200224 }
225
Michal Vaskoaebbce02021-04-06 09:23:37 +0200226 if (stored) {
227 /* free value */
228 type->plugin->free(ctx ? ctx : schema->module->ctx, &val);
229 }
230 return rc;
Radek Krejci084289f2019-07-09 17:35:30 +0200231}
232
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100233LIBYANG_API_DEF LY_ERR
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200234lyd_value_compare(const struct lyd_node_term *node, const char *value, size_t value_len)
Radek Krejci084289f2019-07-09 17:35:30 +0200235{
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200236 LY_ERR ret = LY_SUCCESS;
Radek Krejci084289f2019-07-09 17:35:30 +0200237 struct ly_ctx *ctx;
238 struct lysc_type *type;
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200239 struct lyd_value val = {0};
Radek Krejci084289f2019-07-09 17:35:30 +0200240
241 LY_CHECK_ARG_RET(node ? node->schema->module->ctx : NULL, node, value, LY_EINVAL);
242
243 ctx = node->schema->module->ctx;
Michal Vasko22df3f02020-08-24 13:29:22 +0200244 type = ((struct lysc_node_leaf *)node->schema)->type;
Radek Krejci084289f2019-07-09 17:35:30 +0200245
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200246 /* store the value */
Michal Vasko9e685082021-01-29 14:49:09 +0100247 LOG_LOCSET(node->schema, &node->node, NULL, NULL);
Radek Krejci8df109d2021-04-23 12:19:08 +0200248 ret = lyd_value_store(ctx, &val, type, value, value_len, NULL, LY_VALUE_JSON, NULL, LYD_HINT_DATA, node->schema, NULL);
Radek Krejciddace2c2021-01-08 11:30:56 +0100249 LOG_LOCBACK(1, 1, 0, 0);
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200250 LY_CHECK_RET(ret);
Radek Krejci084289f2019-07-09 17:35:30 +0200251
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200252 /* compare values */
253 ret = type->plugin->compare(&node->value, &val);
Radek Krejci084289f2019-07-09 17:35:30 +0200254
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200255 type->plugin->free(ctx, &val);
Radek Krejci084289f2019-07-09 17:35:30 +0200256 return ret;
257}
258
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100259LIBYANG_API_DEF ly_bool
Radek Krejci19611252020-10-04 13:54:53 +0200260lyd_is_default(const struct lyd_node *node)
261{
262 const struct lysc_node_leaf *leaf;
263 const struct lysc_node_leaflist *llist;
264 const struct lyd_node_term *term;
265 LY_ARRAY_COUNT_TYPE u;
266
Michal Vasko39eb6882021-09-16 08:53:13 +0200267 if (!(node->schema->nodetype & LYD_NODE_TERM)) {
268 return 0;
269 }
270
Radek Krejci19611252020-10-04 13:54:53 +0200271 term = (const struct lyd_node_term *)node;
272
273 if (node->schema->nodetype == LYS_LEAF) {
274 leaf = (const struct lysc_node_leaf *)node->schema;
275 if (!leaf->dflt) {
276 return 0;
277 }
278
279 /* compare with the default value */
Michal Vasko39eb6882021-09-16 08:53:13 +0200280 if (!leaf->type->plugin->compare(&term->value, leaf->dflt)) {
281 return 1;
Radek Krejci19611252020-10-04 13:54:53 +0200282 }
283 } else {
284 llist = (const struct lysc_node_leaflist *)node->schema;
285 if (!llist->dflts) {
286 return 0;
287 }
288
289 LY_ARRAY_FOR(llist->dflts, u) {
290 /* compare with each possible default value */
Michal Vasko39eb6882021-09-16 08:53:13 +0200291 if (!llist->type->plugin->compare(&term->value, llist->dflts[u])) {
292 return 1;
Radek Krejci19611252020-10-04 13:54:53 +0200293 }
294 }
295 }
296
Michal Vasko39eb6882021-09-16 08:53:13 +0200297 return 0;
Radek Krejci19611252020-10-04 13:54:53 +0200298}
299
Radek Krejci7931b192020-06-25 17:05:03 +0200300static LYD_FORMAT
Michal Vasko63f3d842020-07-08 10:10:14 +0200301lyd_parse_get_format(const struct ly_in *in, LYD_FORMAT format)
Radek Krejcie7b95092019-05-15 11:03:07 +0200302{
Michal Vasko69730152020-10-09 16:30:07 +0200303 if (!format && (in->type == LY_IN_FILEPATH)) {
Radek Krejcie7b95092019-05-15 11:03:07 +0200304 /* unknown format - try to detect it from filename's suffix */
Radek Krejci7931b192020-06-25 17:05:03 +0200305 const char *path = in->method.fpath.filepath;
306 size_t len = strlen(path);
Radek Krejcie7b95092019-05-15 11:03:07 +0200307
308 /* ignore trailing whitespaces */
Michal Vaskod989ba02020-08-24 10:59:24 +0200309 for ( ; len > 0 && isspace(path[len - 1]); len--) {}
Radek Krejcie7b95092019-05-15 11:03:07 +0200310
Radek Krejcif13b87b2020-12-01 22:02:17 +0100311 if ((len >= LY_XML_SUFFIX_LEN + 1) &&
312 !strncmp(&path[len - LY_XML_SUFFIX_LEN], LY_XML_SUFFIX, LY_XML_SUFFIX_LEN)) {
Radek Krejcie7b95092019-05-15 11:03:07 +0200313 format = LYD_XML;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100314 } else if ((len >= LY_JSON_SUFFIX_LEN + 1) &&
315 !strncmp(&path[len - LY_JSON_SUFFIX_LEN], LY_JSON_SUFFIX, LY_JSON_SUFFIX_LEN)) {
Radek Krejcie7b95092019-05-15 11:03:07 +0200316 format = LYD_JSON;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100317 } else if ((len >= LY_LYB_SUFFIX_LEN + 1) &&
318 !strncmp(&path[len - LY_LYB_SUFFIX_LEN], LY_LYB_SUFFIX, LY_LYB_SUFFIX_LEN)) {
Radek Krejcie7b95092019-05-15 11:03:07 +0200319 format = LYD_LYB;
Radek Krejci7931b192020-06-25 17:05:03 +0200320 } /* else still unknown */
Radek Krejcie7b95092019-05-15 11:03:07 +0200321 }
322
Radek Krejci7931b192020-06-25 17:05:03 +0200323 return format;
324}
Radek Krejcie7b95092019-05-15 11:03:07 +0200325
Michal Vaskoe0665742021-02-11 11:08:44 +0100326/**
327 * @brief Parse YANG data into a data tree.
328 *
329 * @param[in] ctx libyang context.
Radek Krejcif16e2542021-02-17 15:39:23 +0100330 * @param[in] ext Optional extenion instance to parse data following the schema tree specified in the extension instance
Michal Vaskoe0665742021-02-11 11:08:44 +0100331 * @param[in] parent Parent to connect the parsed nodes to, if any.
332 * @param[in,out] first_p Pointer to the first top-level parsed node, used only if @p parent is NULL.
333 * @param[in] in Input handle to read the input from.
334 * @param[in] format Expected format of the data in @p in.
335 * @param[in] parse_opts Options for parser.
336 * @param[in] val_opts Options for validation.
337 * @param[out] op Optional pointer to the parsed operation, if any.
338 * @return LY_ERR value.
339 */
340static LY_ERR
Radek Krejcif16e2542021-02-17 15:39:23 +0100341lyd_parse(const struct ly_ctx *ctx, const struct lysc_ext_instance *ext, struct lyd_node *parent, struct lyd_node **first_p,
342 struct ly_in *in, LYD_FORMAT format, uint32_t parse_opts, uint32_t val_opts, struct lyd_node **op)
Radek Krejci7931b192020-06-25 17:05:03 +0200343{
Michal Vaskoe0665742021-02-11 11:08:44 +0100344 LY_ERR rc = LY_SUCCESS;
Radek Krejci1798aae2020-07-14 13:26:06 +0200345 struct lyd_ctx *lydctx = NULL;
Michal Vaskoe0665742021-02-11 11:08:44 +0100346 struct ly_set parsed = {0};
347 struct lyd_node *first;
348 uint32_t i;
Michal Vaskoddd76592022-01-17 13:34:48 +0100349 ly_bool subtree_sibling = 0;
Radek Krejci1798aae2020-07-14 13:26:06 +0200350
Michal Vaskoe0665742021-02-11 11:08:44 +0100351 assert(ctx && (parent || first_p));
Radek Krejci7931b192020-06-25 17:05:03 +0200352
353 format = lyd_parse_get_format(in, format);
Michal Vaskoe0665742021-02-11 11:08:44 +0100354 if (first_p) {
355 *first_p = NULL;
356 }
Radek Krejci1798aae2020-07-14 13:26:06 +0200357
Michal Vasko63f3d842020-07-08 10:10:14 +0200358 /* remember input position */
359 in->func_start = in->current;
Radek Krejci7931b192020-06-25 17:05:03 +0200360
Michal Vaskoe0665742021-02-11 11:08:44 +0100361 /* parse the data */
Radek Krejci7931b192020-06-25 17:05:03 +0200362 switch (format) {
363 case LYD_XML:
Michal Vaskoddd76592022-01-17 13:34:48 +0100364 rc = lyd_parse_xml(ctx, ext, parent, first_p, in, parse_opts, val_opts, LYD_TYPE_DATA_YANG, NULL, &parsed,
365 &subtree_sibling, &lydctx);
Radek Krejci1798aae2020-07-14 13:26:06 +0200366 break;
Radek Krejci7931b192020-06-25 17:05:03 +0200367 case LYD_JSON:
Michal Vaskoddd76592022-01-17 13:34:48 +0100368 rc = lyd_parse_json(ctx, ext, parent, first_p, in, parse_opts, val_opts, LYD_TYPE_DATA_YANG, &parsed,
369 &subtree_sibling, &lydctx);
Radek Krejci1798aae2020-07-14 13:26:06 +0200370 break;
Radek Krejci7931b192020-06-25 17:05:03 +0200371 case LYD_LYB:
Michal Vaskoddd76592022-01-17 13:34:48 +0100372 rc = lyd_parse_lyb(ctx, ext, parent, first_p, in, parse_opts, val_opts, LYD_TYPE_DATA_YANG, &parsed,
373 &subtree_sibling, &lydctx);
Radek Krejci1798aae2020-07-14 13:26:06 +0200374 break;
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200375 case LYD_UNKNOWN:
Michal Vaskod74978f2021-02-12 11:59:36 +0100376 LOGARG(ctx, format);
377 rc = LY_EINVAL;
Michal Vaskoe0665742021-02-11 11:08:44 +0100378 break;
379 }
380 LY_CHECK_GOTO(rc, cleanup);
381
382 if (parent) {
383 /* get first top-level sibling */
384 for (first = parent; first->parent; first = lyd_parent(first)) {}
385 first = lyd_first_sibling(first);
386 first_p = &first;
Radek Krejci7931b192020-06-25 17:05:03 +0200387 }
388
Michal Vaskoe0665742021-02-11 11:08:44 +0100389 if (!(parse_opts & LYD_PARSE_ONLY)) {
390 /* validate data */
Michal Vaskoddd76592022-01-17 13:34:48 +0100391 rc = lyd_validate(first_p, NULL, ctx, val_opts, 0, &lydctx->node_when, &lydctx->node_types, &lydctx->meta_types,
392 &lydctx->ext_val, NULL);
Michal Vaskoe0665742021-02-11 11:08:44 +0100393 LY_CHECK_GOTO(rc, cleanup);
394 }
Radek Krejci7931b192020-06-25 17:05:03 +0200395
Michal Vaskoe0665742021-02-11 11:08:44 +0100396 /* set the operation node */
397 if (op) {
398 *op = lydctx->op_node;
Radek Krejci1798aae2020-07-14 13:26:06 +0200399 }
400
401cleanup:
Michal Vaskoe0665742021-02-11 11:08:44 +0100402 if (lydctx) {
403 lydctx->free(lydctx);
Radek Krejci1798aae2020-07-14 13:26:06 +0200404 }
Michal Vaskoe0665742021-02-11 11:08:44 +0100405 if (rc) {
406 if (parent) {
407 /* free all the parsed subtrees */
408 for (i = 0; i < parsed.count; ++i) {
409 lyd_free_tree(parsed.dnodes[i]);
410 }
411 } else {
412 /* free everything */
413 lyd_free_all(*first_p);
414 *first_p = NULL;
415 }
Michal Vaskoddd76592022-01-17 13:34:48 +0100416 } else if (subtree_sibling) {
417 rc = LY_ENOT;
Michal Vaskoe0665742021-02-11 11:08:44 +0100418 }
419 ly_set_erase(&parsed, NULL);
420 return rc;
Radek Krejci7931b192020-06-25 17:05:03 +0200421}
422
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100423LIBYANG_API_DEF LY_ERR
Radek Krejcif16e2542021-02-17 15:39:23 +0100424lyd_parse_ext_data(const struct lysc_ext_instance *ext, struct lyd_node *parent, struct ly_in *in, LYD_FORMAT format,
425 uint32_t parse_options, uint32_t validate_options, struct lyd_node **tree)
426{
427 const struct ly_ctx *ctx = ext ? ext->module->ctx : NULL;
428
429 LY_CHECK_ARG_RET(ctx, ext, in, parent || tree, LY_EINVAL);
430 LY_CHECK_ARG_RET(ctx, !(parse_options & ~LYD_PARSE_OPTS_MASK), LY_EINVAL);
431 LY_CHECK_ARG_RET(ctx, !(validate_options & ~LYD_VALIDATE_OPTS_MASK), LY_EINVAL);
432
433 return lyd_parse(ctx, ext, parent, tree, in, format, parse_options, validate_options, NULL);
434}
435
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100436LIBYANG_API_DEF LY_ERR
Michal Vaskoe0665742021-02-11 11:08:44 +0100437lyd_parse_data(const struct ly_ctx *ctx, struct lyd_node *parent, struct ly_in *in, LYD_FORMAT format,
438 uint32_t parse_options, uint32_t validate_options, struct lyd_node **tree)
439{
440 LY_CHECK_ARG_RET(ctx, ctx, in, parent || tree, LY_EINVAL);
441 LY_CHECK_ARG_RET(ctx, !(parse_options & ~LYD_PARSE_OPTS_MASK), LY_EINVAL);
442 LY_CHECK_ARG_RET(ctx, !(validate_options & ~LYD_VALIDATE_OPTS_MASK), LY_EINVAL);
443
Radek Krejcif16e2542021-02-17 15:39:23 +0100444 return lyd_parse(ctx, NULL, parent, tree, in, format, parse_options, validate_options, NULL);
Michal Vaskoe0665742021-02-11 11:08:44 +0100445}
446
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100447LIBYANG_API_DEF LY_ERR
Michal Vaskoe0665742021-02-11 11:08:44 +0100448lyd_parse_data_mem(const struct ly_ctx *ctx, const char *data, LYD_FORMAT format, uint32_t parse_options,
449 uint32_t validate_options, struct lyd_node **tree)
Radek Krejci7931b192020-06-25 17:05:03 +0200450{
451 LY_ERR ret;
452 struct ly_in *in;
453
454 LY_CHECK_RET(ly_in_new_memory(data, &in));
Michal Vaskoe0665742021-02-11 11:08:44 +0100455 ret = lyd_parse_data(ctx, NULL, in, format, parse_options, validate_options, tree);
Radek Krejci7931b192020-06-25 17:05:03 +0200456
457 ly_in_free(in, 0);
458 return ret;
459}
460
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100461LIBYANG_API_DEF LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200462lyd_parse_data_fd(const struct ly_ctx *ctx, int fd, LYD_FORMAT format, uint32_t parse_options, uint32_t validate_options,
Radek Krejci0f969882020-08-21 16:56:47 +0200463 struct lyd_node **tree)
Radek Krejci7931b192020-06-25 17:05:03 +0200464{
465 LY_ERR ret;
466 struct ly_in *in;
467
468 LY_CHECK_RET(ly_in_new_fd(fd, &in));
Michal Vaskoe0665742021-02-11 11:08:44 +0100469 ret = lyd_parse_data(ctx, NULL, in, format, parse_options, validate_options, tree);
Radek Krejci7931b192020-06-25 17:05:03 +0200470
471 ly_in_free(in, 0);
472 return ret;
473}
474
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100475LIBYANG_API_DEF LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200476lyd_parse_data_path(const struct ly_ctx *ctx, const char *path, LYD_FORMAT format, uint32_t parse_options,
477 uint32_t validate_options, struct lyd_node **tree)
Radek Krejci7931b192020-06-25 17:05:03 +0200478{
479 LY_ERR ret;
480 struct ly_in *in;
481
482 LY_CHECK_RET(ly_in_new_filepath(path, 0, &in));
Michal Vaskoe0665742021-02-11 11:08:44 +0100483 ret = lyd_parse_data(ctx, NULL, in, format, parse_options, validate_options, tree);
Radek Krejci7931b192020-06-25 17:05:03 +0200484
485 ly_in_free(in, 0);
486 return ret;
487}
488
Radek Krejcif16e2542021-02-17 15:39:23 +0100489/**
490 * @brief Parse YANG data into an operation data tree, in case the extension instance is specified, keep the searching
491 * for schema nodes locked inside the extension instance.
492 *
493 * At least one of @p parent, @p tree, or @p op must always be set.
494 *
495 * Specific @p data_type values have different parameter meaning as follows:
496 * - ::LYD_TYPE_RPC_NETCONF:
497 * - @p parent - must be NULL, the whole RPC is expected;
498 * - @p format - must be ::LYD_XML, NETCONF supports only this format;
499 * - @p tree - must be provided, all the NETCONF-specific XML envelopes will be returned here as
500 * a separate opaque data tree, even if the function fails, this may be returned;
501 * - @p op - must be provided, the RPC/action data tree itself will be returned here, pointing to the operation;
502 *
503 * - ::LYD_TYPE_NOTIF_NETCONF:
504 * - @p parent - must be NULL, the whole notification is expected;
505 * - @p format - must be ::LYD_XML, NETCONF supports only this format;
506 * - @p tree - must be provided, all the NETCONF-specific XML envelopes will be returned here as
507 * a separate opaque data tree, even if the function fails, this may be returned;
508 * - @p op - must be provided, the notification data tree itself will be returned here, pointing to the operation;
509 *
510 * - ::LYD_TYPE_REPLY_NETCONF:
511 * - @p parent - must be set, pointing to the invoked RPC operation (RPC or action) node;
512 * - @p format - must be ::LYD_XML, NETCONF supports only this format;
513 * - @p tree - must be provided, all the NETCONF-specific XML envelopes will be returned here as
514 * a separate opaque data tree, even if the function fails, this may be returned;
515 * - @p op - must be NULL, the reply is appended to the RPC;
516 * Note that there are 3 kinds of NETCONF replies - ok, error, and data. Only data reply appends any nodes to the RPC.
517 *
518 * @param[in] ctx libyang context.
519 * @param[in] ext Extension instance providing the specific schema tree to match with the data being parsed.
520 * @param[in] parent Optional parent to connect the parsed nodes to.
521 * @param[in] in Input handle to read the input from.
522 * @param[in] format Expected format of the data in @p in.
523 * @param[in] data_type Expected operation to parse (@ref datatype).
524 * @param[out] tree Optional full parsed data tree. If @p parent is set, set to NULL.
525 * @param[out] op Optional parsed operation node.
526 * @return LY_ERR value.
527 * @return LY_ENOT if @p data_type is a NETCONF message and the root XML element is not the expected one.
528 */
529static LY_ERR
530lyd_parse_op_(const struct ly_ctx *ctx, const struct lysc_ext_instance *ext, struct lyd_node *parent,
531 struct ly_in *in, LYD_FORMAT format, enum lyd_type data_type, struct lyd_node **tree, struct lyd_node **op)
Radek Krejci7931b192020-06-25 17:05:03 +0200532{
Michal Vaskoe0665742021-02-11 11:08:44 +0100533 LY_ERR rc = LY_SUCCESS;
534 struct lyd_ctx *lydctx = NULL;
535 struct ly_set parsed = {0};
536 struct lyd_node *first = NULL, *envp = NULL;
537 uint32_t i, parse_opts, val_opts;
Radek Krejci7931b192020-06-25 17:05:03 +0200538
Michal Vasko27fb0262021-02-23 09:42:01 +0100539 if (!ctx) {
540 ctx = LYD_CTX(parent);
541 }
Radek Krejci1798aae2020-07-14 13:26:06 +0200542 if (tree) {
543 *tree = NULL;
544 }
545 if (op) {
546 *op = NULL;
547 }
548
Radek Krejci7931b192020-06-25 17:05:03 +0200549 format = lyd_parse_get_format(in, format);
Radek Krejci7931b192020-06-25 17:05:03 +0200550
Michal Vasko63f3d842020-07-08 10:10:14 +0200551 /* remember input position */
552 in->func_start = in->current;
553
Michal Vaskoe0665742021-02-11 11:08:44 +0100554 /* check params based on the data type */
Michal Vasko1e4c68e2021-02-18 15:03:01 +0100555 if ((data_type == LYD_TYPE_RPC_NETCONF) || (data_type == LYD_TYPE_NOTIF_NETCONF)) {
Michal Vaskoe0665742021-02-11 11:08:44 +0100556 LY_CHECK_ARG_RET(ctx, format == LYD_XML, !parent, tree, op, LY_EINVAL);
Michal Vasko1e4c68e2021-02-18 15:03:01 +0100557 } else if (data_type == LYD_TYPE_REPLY_NETCONF) {
558 LY_CHECK_ARG_RET(ctx, format == LYD_XML, parent, parent->schema->nodetype & (LYS_RPC | LYS_ACTION), tree, !op,
559 LY_EINVAL);
Michal Vaskoe0665742021-02-11 11:08:44 +0100560 }
Michal Vasko9da78cf2021-07-21 13:45:10 +0200561 parse_opts = LYD_PARSE_ONLY | LYD_PARSE_OPAQ;
Michal Vaskoe0665742021-02-11 11:08:44 +0100562 val_opts = 0;
563
564 /* parse the data */
Radek Krejci7931b192020-06-25 17:05:03 +0200565 switch (format) {
566 case LYD_XML:
Michal Vaskoddd76592022-01-17 13:34:48 +0100567 rc = lyd_parse_xml(ctx, ext, parent, &first, in, parse_opts, val_opts, data_type, &envp, &parsed, NULL, &lydctx);
Michal Vasko299d5d12021-02-16 16:36:37 +0100568 if (rc && envp) {
569 /* special situation when the envelopes were parsed successfully */
570 if (tree) {
571 *tree = envp;
572 }
573 ly_set_erase(&parsed, NULL);
574 return rc;
575 }
Michal Vaskoe0665742021-02-11 11:08:44 +0100576 break;
Radek Krejci7931b192020-06-25 17:05:03 +0200577 case LYD_JSON:
Michal Vaskoddd76592022-01-17 13:34:48 +0100578 rc = lyd_parse_json(ctx, ext, parent, &first, in, parse_opts, val_opts, data_type, &parsed, NULL, &lydctx);
Michal Vaskoe0665742021-02-11 11:08:44 +0100579 break;
Radek Krejci7931b192020-06-25 17:05:03 +0200580 case LYD_LYB:
Michal Vaskoddd76592022-01-17 13:34:48 +0100581 rc = lyd_parse_lyb(ctx, ext, parent, &first, in, parse_opts, val_opts, data_type, &parsed, NULL, &lydctx);
Michal Vaskoe0665742021-02-11 11:08:44 +0100582 break;
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200583 case LYD_UNKNOWN:
Michal Vaskod74978f2021-02-12 11:59:36 +0100584 LOGARG(ctx, format);
585 rc = LY_EINVAL;
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200586 break;
Radek Krejci7931b192020-06-25 17:05:03 +0200587 }
Michal Vaskoe0665742021-02-11 11:08:44 +0100588 LY_CHECK_GOTO(rc, cleanup);
Radek Krejci7931b192020-06-25 17:05:03 +0200589
Michal Vaskoe0665742021-02-11 11:08:44 +0100590 /* set out params correctly */
591 if (tree) {
592 if (envp) {
593 /* special out param meaning */
594 *tree = envp;
595 } else {
596 *tree = parent ? NULL : first;
597 }
598 }
599 if (op) {
600 *op = lydctx->op_node;
601 }
602
603cleanup:
604 if (lydctx) {
605 lydctx->free(lydctx);
606 }
607 if (rc) {
608 if (parent) {
609 /* free all the parsed subtrees */
610 for (i = 0; i < parsed.count; ++i) {
611 lyd_free_tree(parsed.dnodes[i]);
612 }
613 } else {
614 /* free everything (cannot occur in the current code, a safety) */
615 lyd_free_all(first);
616 if (tree) {
617 *tree = NULL;
618 }
619 if (op) {
620 *op = NULL;
621 }
622 }
623 }
624 ly_set_erase(&parsed, NULL);
625 return rc;
Radek Krejcie7b95092019-05-15 11:03:07 +0200626}
Radek Krejci084289f2019-07-09 17:35:30 +0200627
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100628LIBYANG_API_DEF LY_ERR
Radek Krejcif16e2542021-02-17 15:39:23 +0100629lyd_parse_op(const struct ly_ctx *ctx, struct lyd_node *parent, struct ly_in *in, LYD_FORMAT format,
630 enum lyd_type data_type, struct lyd_node **tree, struct lyd_node **op)
631{
632 LY_CHECK_ARG_RET(ctx, ctx || parent, in, data_type, parent || tree || op, LY_EINVAL);
633
634 return lyd_parse_op_(ctx, NULL, parent, in, format, data_type, tree, op);
635}
636
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100637LIBYANG_API_DEF LY_ERR
Radek Krejcif16e2542021-02-17 15:39:23 +0100638lyd_parse_ext_op(const struct lysc_ext_instance *ext, struct lyd_node *parent, struct ly_in *in, LYD_FORMAT format,
639 enum lyd_type data_type, struct lyd_node **tree, struct lyd_node **op)
640{
641 const struct ly_ctx *ctx = ext ? ext->module->ctx : NULL;
642
643 LY_CHECK_ARG_RET(ctx, ext, in, data_type, parent || tree || op, LY_EINVAL);
644
645 return lyd_parse_op_(ctx, ext, parent, in, format, data_type, tree, op);
646}
647
Michal Vasko90932a92020-02-12 14:33:03 +0100648LY_ERR
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200649lyd_create_term(const struct lysc_node *schema, const char *value, size_t value_len, ly_bool *dynamic,
Radek Krejci8df109d2021-04-23 12:19:08 +0200650 LY_VALUE_FORMAT format, void *prefix_data, uint32_t hints, ly_bool *incomplete, struct lyd_node **node)
Michal Vasko90932a92020-02-12 14:33:03 +0100651{
652 LY_ERR ret;
653 struct lyd_node_term *term;
654
Michal Vasko9b368d32020-02-14 13:53:31 +0100655 assert(schema->nodetype & LYD_NODE_TERM);
656
Michal Vasko90932a92020-02-12 14:33:03 +0100657 term = calloc(1, sizeof *term);
658 LY_CHECK_ERR_RET(!term, LOGMEM(schema->module->ctx), LY_EMEM);
659
660 term->schema = schema;
Michal Vasko9e685082021-01-29 14:49:09 +0100661 term->prev = &term->node;
Michal Vasko9b368d32020-02-14 13:53:31 +0100662 term->flags = LYD_NEW;
Michal Vasko90932a92020-02-12 14:33:03 +0100663
Radek Krejciddace2c2021-01-08 11:30:56 +0100664 LOG_LOCSET(schema, NULL, NULL, NULL);
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200665 ret = lyd_value_store(schema->module->ctx, &term->value, ((struct lysc_node_leaf *)term->schema)->type, value,
Radek Krejci2efc45b2020-12-22 16:25:44 +0100666 value_len, dynamic, format, prefix_data, hints, schema, incomplete);
Radek Krejciddace2c2021-01-08 11:30:56 +0100667 LOG_LOCBACK(1, 0, 0, 0);
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200668 LY_CHECK_ERR_RET(ret, free(term), ret);
Michal Vasko9e685082021-01-29 14:49:09 +0100669 lyd_hash(&term->node);
Michal Vasko9b368d32020-02-14 13:53:31 +0100670
Michal Vasko9e685082021-01-29 14:49:09 +0100671 *node = &term->node;
Michal Vasko9b368d32020-02-14 13:53:31 +0100672 return ret;
673}
674
675LY_ERR
676lyd_create_term2(const struct lysc_node *schema, const struct lyd_value *val, struct lyd_node **node)
677{
678 LY_ERR ret;
679 struct lyd_node_term *term;
680 struct lysc_type *type;
681
682 assert(schema->nodetype & LYD_NODE_TERM);
Radek Krejci224d4b42021-04-23 13:54:59 +0200683 assert(val && val->realtype);
Michal Vasko9b368d32020-02-14 13:53:31 +0100684
685 term = calloc(1, sizeof *term);
686 LY_CHECK_ERR_RET(!term, LOGMEM(schema->module->ctx), LY_EMEM);
687
688 term->schema = schema;
Michal Vasko9e685082021-01-29 14:49:09 +0100689 term->prev = &term->node;
Michal Vasko9b368d32020-02-14 13:53:31 +0100690 term->flags = LYD_NEW;
691
692 type = ((struct lysc_node_leaf *)schema)->type;
693 ret = type->plugin->duplicate(schema->module->ctx, val, &term->value);
694 if (ret) {
695 LOGERR(schema->module->ctx, ret, "Value duplication failed.");
696 free(term);
697 return ret;
698 }
Michal Vasko9e685082021-01-29 14:49:09 +0100699 lyd_hash(&term->node);
Michal Vasko90932a92020-02-12 14:33:03 +0100700
Michal Vasko9e685082021-01-29 14:49:09 +0100701 *node = &term->node;
Michal Vasko90932a92020-02-12 14:33:03 +0100702 return ret;
703}
704
705LY_ERR
706lyd_create_inner(const struct lysc_node *schema, struct lyd_node **node)
707{
708 struct lyd_node_inner *in;
709
Michal Vasko9b368d32020-02-14 13:53:31 +0100710 assert(schema->nodetype & LYD_NODE_INNER);
711
Michal Vasko90932a92020-02-12 14:33:03 +0100712 in = calloc(1, sizeof *in);
713 LY_CHECK_ERR_RET(!in, LOGMEM(schema->module->ctx), LY_EMEM);
714
715 in->schema = schema;
Michal Vasko9e685082021-01-29 14:49:09 +0100716 in->prev = &in->node;
Michal Vasko9b368d32020-02-14 13:53:31 +0100717 in->flags = LYD_NEW;
Michal Vasko3383be72020-11-03 17:15:31 +0100718 if ((schema->nodetype == LYS_CONTAINER) && !(schema->flags & LYS_PRESENCE)) {
719 in->flags |= LYD_DEFAULT;
720 }
Michal Vasko90932a92020-02-12 14:33:03 +0100721
Michal Vasko9b368d32020-02-14 13:53:31 +0100722 /* do not hash list with keys, we need them for the hash */
723 if ((schema->nodetype != LYS_LIST) || (schema->flags & LYS_KEYLESS)) {
Michal Vasko9e685082021-01-29 14:49:09 +0100724 lyd_hash(&in->node);
Michal Vasko9b368d32020-02-14 13:53:31 +0100725 }
Michal Vasko90932a92020-02-12 14:33:03 +0100726
Michal Vasko9e685082021-01-29 14:49:09 +0100727 *node = &in->node;
Michal Vasko90932a92020-02-12 14:33:03 +0100728 return LY_SUCCESS;
729}
730
Michal Vasko90932a92020-02-12 14:33:03 +0100731LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +0200732lyd_create_list(const struct lysc_node *schema, const struct ly_path_predicate *predicates, struct lyd_node **node)
Michal Vasko90932a92020-02-12 14:33:03 +0100733{
734 LY_ERR ret = LY_SUCCESS;
Michal Vasko90932a92020-02-12 14:33:03 +0100735 struct lyd_node *list = NULL, *key;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200736 LY_ARRAY_COUNT_TYPE u;
Michal Vasko90932a92020-02-12 14:33:03 +0100737
Michal Vasko004d3152020-06-11 19:59:22 +0200738 assert((schema->nodetype == LYS_LIST) && !(schema->flags & LYS_KEYLESS));
Michal Vasko90932a92020-02-12 14:33:03 +0100739
740 /* create list */
741 LY_CHECK_GOTO(ret = lyd_create_inner(schema, &list), cleanup);
742
Radek Krejciddace2c2021-01-08 11:30:56 +0100743 LOG_LOCSET(NULL, list, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100744
Michal Vasko90932a92020-02-12 14:33:03 +0100745 /* create and insert all the keys */
Michal Vasko004d3152020-06-11 19:59:22 +0200746 LY_ARRAY_FOR(predicates, u) {
747 LY_CHECK_GOTO(ret = lyd_create_term2(predicates[u].key, &predicates[u].value, &key), cleanup);
Michal Vasko6ee6f432021-07-16 09:49:14 +0200748 lyd_insert_node(list, NULL, key, 0);
Michal Vasko90932a92020-02-12 14:33:03 +0100749 }
750
Michal Vasko9b368d32020-02-14 13:53:31 +0100751 /* hash having all the keys */
752 lyd_hash(list);
753
Michal Vasko90932a92020-02-12 14:33:03 +0100754 /* success */
755 *node = list;
756 list = NULL;
757
758cleanup:
Radek Krejciddace2c2021-01-08 11:30:56 +0100759 LOG_LOCBACK(0, 1, 0, 0);
Michal Vasko90932a92020-02-12 14:33:03 +0100760 lyd_free_tree(list);
Michal Vasko004d3152020-06-11 19:59:22 +0200761 return ret;
762}
763
764static LY_ERR
765lyd_create_list2(const struct lysc_node *schema, const char *keys, size_t keys_len, struct lyd_node **node)
766{
767 LY_ERR ret = LY_SUCCESS;
768 struct lyxp_expr *expr = NULL;
769 uint16_t exp_idx = 0;
770 enum ly_path_pred_type pred_type = 0;
771 struct ly_path_predicate *predicates = NULL;
772
Radek Krejciddace2c2021-01-08 11:30:56 +0100773 LOG_LOCSET(schema, NULL, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100774
Michal Vasko004d3152020-06-11 19:59:22 +0200775 /* parse keys */
Michal Vasko6b26e742020-07-17 15:02:10 +0200776 LY_CHECK_GOTO(ret = ly_path_parse_predicate(schema->module->ctx, NULL, keys, keys_len, LY_PATH_PREFIX_OPTIONAL,
Michal Vasko69730152020-10-09 16:30:07 +0200777 LY_PATH_PRED_KEYS, &expr), cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +0200778
779 /* compile them */
Radek Krejci8df109d2021-04-23 12:19:08 +0200780 LY_CHECK_GOTO(ret = ly_path_compile_predicate(schema->module->ctx, NULL, NULL, schema, expr, &exp_idx, LY_VALUE_JSON,
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200781 NULL, &predicates, &pred_type), cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +0200782
783 /* create the list node */
784 LY_CHECK_GOTO(ret = lyd_create_list(schema, predicates, node), cleanup);
785
786cleanup:
Radek Krejciddace2c2021-01-08 11:30:56 +0100787 LOG_LOCBACK(1, 0, 0, 0);
Michal Vasko004d3152020-06-11 19:59:22 +0200788 lyxp_expr_free(schema->module->ctx, expr);
Michal Vaskof7e16e22020-10-21 09:24:39 +0200789 ly_path_predicates_free(schema->module->ctx, pred_type, predicates);
Michal Vasko90932a92020-02-12 14:33:03 +0100790 return ret;
791}
792
793LY_ERR
Michal Vasko366a4a12020-12-04 16:23:57 +0100794lyd_create_any(const struct lysc_node *schema, const void *value, LYD_ANYDATA_VALUETYPE value_type, ly_bool use_value,
795 struct lyd_node **node)
Michal Vasko90932a92020-02-12 14:33:03 +0100796{
Michal Vasko366a4a12020-12-04 16:23:57 +0100797 LY_ERR ret;
Michal Vasko90932a92020-02-12 14:33:03 +0100798 struct lyd_node_any *any;
Michal Vasko366a4a12020-12-04 16:23:57 +0100799 union lyd_any_value any_val;
Michal Vasko90932a92020-02-12 14:33:03 +0100800
Michal Vasko9b368d32020-02-14 13:53:31 +0100801 assert(schema->nodetype & LYD_NODE_ANY);
802
Michal Vasko90932a92020-02-12 14:33:03 +0100803 any = calloc(1, sizeof *any);
804 LY_CHECK_ERR_RET(!any, LOGMEM(schema->module->ctx), LY_EMEM);
805
806 any->schema = schema;
Michal Vasko9e685082021-01-29 14:49:09 +0100807 any->prev = &any->node;
Michal Vasko9b368d32020-02-14 13:53:31 +0100808 any->flags = LYD_NEW;
Michal Vasko90932a92020-02-12 14:33:03 +0100809
Michal Vasko366a4a12020-12-04 16:23:57 +0100810 if (use_value) {
Michal Vasko742a5b12022-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 Vasko0d00e462022-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 Vasko742a5b12022-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átc53a7ec2021-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{
Michal Vasko8cc3f662022-03-29 11:25:51 +0200888 LY_ERR r;
Michal Vasko013a8182020-03-03 10:46:53 +0100889 struct lyd_node *ret = NULL;
890 const struct lysc_node *schema;
Michal Vasko8cc3f662022-03-29 11:25:51 +0200891 struct lysc_ext_instance *ext = NULL;
Michal Vasko892f5bf2021-11-24 10:41:05 +0100892 const struct ly_ctx *ctx = parent ? LYD_CTX(parent) : (module ? module->ctx : NULL);
Michal Vasko013a8182020-03-03 10:46:53 +0100893
Michal Vasko6027eb92020-07-15 16:37:30 +0200894 LY_CHECK_ARG_RET(ctx, parent || module, parent || node, name, LY_EINVAL);
Michal Vasko892f5bf2021-11-24 10:41:05 +0100895 LY_CHECK_CTX_EQUAL_RET(parent ? LYD_CTX(parent) : NULL, module ? module->ctx : NULL, LY_EINVAL);
Michal Vasko013a8182020-03-03 10:46:53 +0100896
Michal Vaskof03ed032020-03-04 13:31:44 +0100897 if (!module) {
898 module = parent->schema->module;
899 }
900
Michal Vasko3a41dff2020-07-15 14:30:28 +0200901 schema = lys_find_child(parent ? parent->schema : NULL, module, name, 0,
Radek Krejci41ac9942020-11-02 14:47:56 +0100902 LYS_CONTAINER | LYS_NOTIF | LYS_RPC | LYS_ACTION, output ? LYS_GETNEXT_OUTPUT : 0);
Michal Vasko8cc3f662022-03-29 11:25:51 +0200903 if (!schema && parent) {
904 r = ly_nested_ext_schema(parent, NULL, module->name, strlen(module->name), LY_VALUE_JSON, NULL, name,
905 strlen(name), &schema, &ext);
906 LY_CHECK_RET(r && (r != LY_ENOT), r);
907 }
908 LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "Inner node (container, notif, RPC, or action) \"%s\" not found.",
909 name), LY_ENOTFOUND);
Michal Vasko013a8182020-03-03 10:46:53 +0100910
Michal Vasko3a41dff2020-07-15 14:30:28 +0200911 LY_CHECK_RET(lyd_create_inner(schema, &ret));
Michal Vasko8cc3f662022-03-29 11:25:51 +0200912 if (ext) {
913 ret->flags |= LYD_EXT;
914 }
Michal Vasko3a41dff2020-07-15 14:30:28 +0200915 if (parent) {
Michal Vasko6ee6f432021-07-16 09:49:14 +0200916 lyd_insert_node(parent, NULL, ret, 0);
Michal Vasko013a8182020-03-03 10:46:53 +0100917 }
Michal Vasko3a41dff2020-07-15 14:30:28 +0200918
919 if (node) {
920 *node = ret;
921 }
922 return LY_SUCCESS;
Michal Vasko013a8182020-03-03 10:46:53 +0100923}
924
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100925LIBYANG_API_DEF LY_ERR
Radek Krejcidd2a7662021-03-12 11:26:34 +0100926lyd_new_ext_inner(const struct lysc_ext_instance *ext, const char *name, struct lyd_node **node)
927{
928 struct lyd_node *ret = NULL;
929 const struct lysc_node *schema;
930 struct ly_ctx *ctx = ext ? ext->module->ctx : NULL;
931
932 LY_CHECK_ARG_RET(ctx, ext, node, name, LY_EINVAL);
933
934 schema = lysc_ext_find_node(ext, NULL, name, 0, LYS_CONTAINER | LYS_NOTIF | LYS_RPC | LYS_ACTION, 0);
935 if (!schema) {
936 if (ext->argument) {
937 LOGERR(ctx, LY_EINVAL, "Inner node (not a list) \"%s\" not found in instance \"%s\" of extension %s.",
938 name, ext->argument, ext->def->name);
939 } else {
940 LOGERR(ctx, LY_EINVAL, "Inner node (not a list) \"%s\" not found in instance of extension %s.",
941 name, ext->def->name);
942 }
943 return LY_ENOTFOUND;
944 }
945 LY_CHECK_RET(lyd_create_inner(schema, &ret));
946
947 *node = ret;
948
949 return LY_SUCCESS;
950}
951
Michal Vasko208e6d62021-06-28 11:23:50 +0200952/**
953 * @brief Create a new list node in the data tree.
954 *
955 * @param[in] parent Parent node for the node being created. NULL in case of creating a top level element.
956 * @param[in] module Module of the node being created. If NULL, @p parent module will be used.
957 * @param[in] name Schema node name of the new data node. The node must be #LYS_LIST.
958 * @param[in] format Format of key values.
959 * @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
960 * taken into consideration. Otherwise, the output's data node is going to be created.
961 * @param[out] node Optional created node.
962 * @param[in] ap Ordered key values of the new list instance, all must be set. For ::LY_VALUE_LYB, every value must
963 * be followed by the value length.
964 * @return LY_ERR value.
965 */
966static LY_ERR
967_lyd_new_list(struct lyd_node *parent, const struct lys_module *module, const char *name, LY_VALUE_FORMAT format,
968 ly_bool output, struct lyd_node **node, va_list ap)
Michal Vasko013a8182020-03-03 10:46:53 +0100969{
970 struct lyd_node *ret = NULL, *key;
971 const struct lysc_node *schema, *key_s;
Michal Vasko8cc3f662022-03-29 11:25:51 +0200972 struct lysc_ext_instance *ext = NULL;
Michal Vasko892f5bf2021-11-24 10:41:05 +0100973 const struct ly_ctx *ctx = parent ? LYD_CTX(parent) : (module ? module->ctx : NULL);
Michal Vasko208e6d62021-06-28 11:23:50 +0200974 const void *key_val;
975 uint32_t key_len;
Michal Vasko8cc3f662022-03-29 11:25:51 +0200976 LY_ERR r, rc = LY_SUCCESS;
Michal Vasko013a8182020-03-03 10:46:53 +0100977
Michal Vasko6027eb92020-07-15 16:37:30 +0200978 LY_CHECK_ARG_RET(ctx, parent || module, parent || node, name, LY_EINVAL);
Michal Vasko892f5bf2021-11-24 10:41:05 +0100979 LY_CHECK_CTX_EQUAL_RET(parent ? LYD_CTX(parent) : NULL, module ? module->ctx : NULL, LY_EINVAL);
Michal Vasko013a8182020-03-03 10:46:53 +0100980
Michal Vaskof03ed032020-03-04 13:31:44 +0100981 if (!module) {
982 module = parent->schema->module;
983 }
984
Radek Krejci41ac9942020-11-02 14:47:56 +0100985 schema = lys_find_child(parent ? parent->schema : NULL, module, name, 0, LYS_LIST, output ? LYS_GETNEXT_OUTPUT : 0);
Michal Vasko8cc3f662022-03-29 11:25:51 +0200986 if (!schema && parent) {
987 r = ly_nested_ext_schema(parent, NULL, module->name, strlen(module->name), LY_VALUE_JSON, NULL, name,
988 strlen(name), &schema, &ext);
989 LY_CHECK_RET(r && (r != LY_ENOT), r);
990 }
Michal Vasko3a41dff2020-07-15 14:30:28 +0200991 LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "List node \"%s\" not found.", name), LY_ENOTFOUND);
Michal Vasko013a8182020-03-03 10:46:53 +0100992
993 /* create list inner node */
Michal Vasko3a41dff2020-07-15 14:30:28 +0200994 LY_CHECK_RET(lyd_create_inner(schema, &ret));
Michal Vasko013a8182020-03-03 10:46:53 +0100995
Michal Vasko013a8182020-03-03 10:46:53 +0100996 /* create and insert all the keys */
Michal Vasko544e58a2021-01-28 14:33:41 +0100997 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 +0200998 if (format == LY_VALUE_LYB) {
999 key_val = va_arg(ap, const void *);
1000 key_len = va_arg(ap, uint32_t);
1001 } else {
1002 key_val = va_arg(ap, const char *);
1003 key_len = key_val ? strlen((char *)key_val) : 0;
1004 }
Michal Vasko013a8182020-03-03 10:46:53 +01001005
Michal Vasko208e6d62021-06-28 11:23:50 +02001006 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 +02001007 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko6ee6f432021-07-16 09:49:14 +02001008 lyd_insert_node(ret, NULL, key, 1);
Michal Vasko013a8182020-03-03 10:46:53 +01001009 }
1010
Michal Vasko8cc3f662022-03-29 11:25:51 +02001011 if (ext) {
1012 ret->flags |= LYD_EXT;
1013 }
Michal Vasko013a8182020-03-03 10:46:53 +01001014 if (parent) {
Michal Vasko6ee6f432021-07-16 09:49:14 +02001015 lyd_insert_node(parent, NULL, ret, 0);
Michal Vasko013a8182020-03-03 10:46:53 +01001016 }
1017
1018cleanup:
1019 if (rc) {
1020 lyd_free_tree(ret);
1021 ret = NULL;
Michal Vasko3a41dff2020-07-15 14:30:28 +02001022 } else if (node) {
1023 *node = ret;
Michal Vasko013a8182020-03-03 10:46:53 +01001024 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02001025 return rc;
Michal Vasko013a8182020-03-03 10:46:53 +01001026}
1027
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001028LIBYANG_API_DEF LY_ERR
Michal Vasko208e6d62021-06-28 11:23:50 +02001029lyd_new_list(struct lyd_node *parent, const struct lys_module *module, const char *name, ly_bool output,
1030 struct lyd_node **node, ...)
1031{
1032 LY_ERR rc;
1033 va_list ap;
1034
1035 va_start(ap, node);
1036
1037 rc = _lyd_new_list(parent, module, name, LY_VALUE_JSON, output, node, ap);
1038
1039 va_end(ap);
1040 return rc;
1041}
1042
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001043LIBYANG_API_DEF LY_ERR
Michal Vasko208e6d62021-06-28 11:23:50 +02001044lyd_new_list_bin(struct lyd_node *parent, const struct lys_module *module, const char *name, ly_bool output,
1045 struct lyd_node **node, ...)
1046{
1047 LY_ERR rc;
1048 va_list ap;
1049
1050 va_start(ap, node);
1051
1052 rc = _lyd_new_list(parent, module, name, LY_VALUE_LYB, output, node, ap);
1053
1054 va_end(ap);
1055 return rc;
1056}
1057
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001058LIBYANG_API_DEF LY_ERR
Michal Vasko208e6d62021-06-28 11:23:50 +02001059lyd_new_list_canon(struct lyd_node *parent, const struct lys_module *module, const char *name, ly_bool output,
1060 struct lyd_node **node, ...)
1061{
1062 LY_ERR rc;
1063 va_list ap;
1064
1065 va_start(ap, node);
1066
1067 rc = _lyd_new_list(parent, module, name, LY_VALUE_CANON, output, node, ap);
1068
1069 va_end(ap);
1070 return rc;
1071}
1072
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001073LIBYANG_API_DEF LY_ERR
Radek Krejci8247bae2021-03-12 11:47:02 +01001074lyd_new_ext_list(const struct lysc_ext_instance *ext, const char *name, struct lyd_node **node, ...)
1075{
1076 struct lyd_node *ret = NULL, *key;
1077 const struct lysc_node *schema, *key_s;
1078 struct ly_ctx *ctx = ext ? ext->module->ctx : NULL;
1079 va_list ap;
1080 const char *key_val;
1081 LY_ERR rc = LY_SUCCESS;
1082
1083 LY_CHECK_ARG_RET(ctx, ext, node, name, LY_EINVAL);
1084
1085 schema = lysc_ext_find_node(ext, NULL, name, 0, LYS_LIST, 0);
1086 if (!schema) {
1087 if (ext->argument) {
1088 LOGERR(ctx, LY_EINVAL, "List node \"%s\" not found in instance \"%s\" of extension %s.",
1089 name, ext->argument, ext->def->name);
1090 } else {
1091 LOGERR(ctx, LY_EINVAL, "List node \"%s\" not found in instance of extension %s.", name, ext->def->name);
1092 }
1093 return LY_ENOTFOUND;
1094 }
1095 /* create list inner node */
1096 LY_CHECK_RET(lyd_create_inner(schema, &ret));
1097
1098 va_start(ap, node);
1099
1100 /* create and insert all the keys */
1101 for (key_s = lysc_node_child(schema); key_s && (key_s->flags & LYS_KEY); key_s = key_s->next) {
1102 key_val = va_arg(ap, const char *);
1103
Radek Krejci8df109d2021-04-23 12:19:08 +02001104 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 +01001105 NULL, &key);
1106 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko6ee6f432021-07-16 09:49:14 +02001107 lyd_insert_node(ret, NULL, key, 1);
Radek Krejci8247bae2021-03-12 11:47:02 +01001108 }
1109
1110cleanup:
1111 va_end(ap);
1112 if (rc) {
1113 lyd_free_tree(ret);
1114 ret = NULL;
Radek Krejci8247bae2021-03-12 11:47:02 +01001115 }
Radek Krejci8919fbd2021-03-15 09:35:25 +01001116 *node = ret;
Radek Krejci8247bae2021-03-12 11:47:02 +01001117 return rc;
1118}
1119
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001120LIBYANG_API_DEF LY_ERR
Michal Vasko3a41dff2020-07-15 14:30:28 +02001121lyd_new_list2(struct lyd_node *parent, const struct lys_module *module, const char *name, const char *keys,
Radek Krejci41ac9942020-11-02 14:47:56 +01001122 ly_bool output, struct lyd_node **node)
Michal Vasko013a8182020-03-03 10:46:53 +01001123{
Michal Vasko8cc3f662022-03-29 11:25:51 +02001124 LY_ERR r;
Michal Vasko013a8182020-03-03 10:46:53 +01001125 struct lyd_node *ret = NULL;
1126 const struct lysc_node *schema;
Michal Vasko8cc3f662022-03-29 11:25:51 +02001127 struct lysc_ext_instance *ext = NULL;
Michal Vasko892f5bf2021-11-24 10:41:05 +01001128 const struct ly_ctx *ctx = parent ? LYD_CTX(parent) : (module ? module->ctx : NULL);
Michal Vasko013a8182020-03-03 10:46:53 +01001129
Michal Vasko6027eb92020-07-15 16:37:30 +02001130 LY_CHECK_ARG_RET(ctx, parent || module, parent || node, name, LY_EINVAL);
Michal Vasko892f5bf2021-11-24 10:41:05 +01001131 LY_CHECK_CTX_EQUAL_RET(parent ? LYD_CTX(parent) : NULL, module ? module->ctx : NULL, LY_EINVAL);
Michal Vasko013a8182020-03-03 10:46:53 +01001132
Michal Vaskof03ed032020-03-04 13:31:44 +01001133 if (!module) {
1134 module = parent->schema->module;
1135 }
Michal Vasko004d3152020-06-11 19:59:22 +02001136 if (!keys) {
1137 keys = "";
1138 }
Michal Vaskof03ed032020-03-04 13:31:44 +01001139
Michal Vasko004d3152020-06-11 19:59:22 +02001140 /* find schema node */
Radek Krejci41ac9942020-11-02 14:47:56 +01001141 schema = lys_find_child(parent ? parent->schema : NULL, module, name, 0, LYS_LIST, output ? LYS_GETNEXT_OUTPUT : 0);
Michal Vasko8cc3f662022-03-29 11:25:51 +02001142 if (!schema && parent) {
1143 r = ly_nested_ext_schema(parent, NULL, module->name, strlen(module->name), LY_VALUE_JSON, NULL, name, strlen(name),
1144 &schema, &ext);
1145 LY_CHECK_RET(r && (r != LY_ENOT), r);
1146 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02001147 LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "List node \"%s\" not found.", name), LY_ENOTFOUND);
Michal Vasko013a8182020-03-03 10:46:53 +01001148
Michal Vasko004d3152020-06-11 19:59:22 +02001149 if ((schema->flags & LYS_KEYLESS) && !keys[0]) {
1150 /* key-less list */
Michal Vasko3a41dff2020-07-15 14:30:28 +02001151 LY_CHECK_RET(lyd_create_inner(schema, &ret));
Michal Vasko004d3152020-06-11 19:59:22 +02001152 } else {
1153 /* create the list node */
Michal Vasko3a41dff2020-07-15 14:30:28 +02001154 LY_CHECK_RET(lyd_create_list2(schema, keys, strlen(keys), &ret));
Michal Vasko004d3152020-06-11 19:59:22 +02001155 }
Michal Vasko8cc3f662022-03-29 11:25:51 +02001156 if (ext) {
1157 ret->flags |= LYD_EXT;
1158 }
Michal Vasko004d3152020-06-11 19:59:22 +02001159 if (parent) {
Michal Vasko6ee6f432021-07-16 09:49:14 +02001160 lyd_insert_node(parent, NULL, ret, 0);
Michal Vasko013a8182020-03-03 10:46:53 +01001161 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02001162
1163 if (node) {
1164 *node = ret;
1165 }
1166 return LY_SUCCESS;
Michal Vasko013a8182020-03-03 10:46:53 +01001167}
1168
Radek Krejci09c77442021-04-26 11:10:34 +02001169/**
1170 * @brief Create a new term node in the data tree.
1171 *
1172 * @param[in] parent Parent node for the node being created. NULL in case of creating a top level element.
1173 * @param[in] module Module of the node being created. If NULL, @p parent module will be used.
1174 * @param[in] name Schema node name of the new data node. The node can be ::LYS_LEAF or ::LYS_LEAFLIST.
1175 * @param[in] value Value of the node being created.
1176 * @param[in] value_len Length of @p value.
1177 * @param[in] format Format of @p value.
1178 * @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
1179 * taken into consideration. Otherwise, the output's data node is going to be created.
1180 * @param[out] node Optional created node.
1181 * @return LY_ERR value.
1182 */
1183static LY_ERR
1184_lyd_new_term(struct lyd_node *parent, const struct lys_module *module, const char *name, const void *value,
1185 size_t value_len, LY_VALUE_FORMAT format, ly_bool output, struct lyd_node **node)
Michal Vasko013a8182020-03-03 10:46:53 +01001186{
Michal Vasko8cc3f662022-03-29 11:25:51 +02001187 LY_ERR r;
Michal Vasko013a8182020-03-03 10:46:53 +01001188 struct lyd_node *ret = NULL;
1189 const struct lysc_node *schema;
Michal Vasko8cc3f662022-03-29 11:25:51 +02001190 struct lysc_ext_instance *ext = NULL;
Radek Krejci09c77442021-04-26 11:10:34 +02001191 const struct ly_ctx *ctx = parent ? LYD_CTX(parent) : (module ? module->ctx : NULL);
Michal Vasko013a8182020-03-03 10:46:53 +01001192
Michal Vasko6027eb92020-07-15 16:37:30 +02001193 LY_CHECK_ARG_RET(ctx, parent || module, parent || node, name, LY_EINVAL);
Michal Vasko892f5bf2021-11-24 10:41:05 +01001194 LY_CHECK_CTX_EQUAL_RET(parent ? LYD_CTX(parent) : NULL, module ? module->ctx : NULL, LY_EINVAL);
Michal Vasko013a8182020-03-03 10:46:53 +01001195
Michal Vaskof03ed032020-03-04 13:31:44 +01001196 if (!module) {
1197 module = parent->schema->module;
1198 }
1199
Radek Krejci41ac9942020-11-02 14:47:56 +01001200 schema = lys_find_child(parent ? parent->schema : NULL, module, name, 0, LYD_NODE_TERM, output ? LYS_GETNEXT_OUTPUT : 0);
Michal Vasko8cc3f662022-03-29 11:25:51 +02001201 if (!schema && parent) {
1202 r = ly_nested_ext_schema(parent, NULL, module->name, strlen(module->name), LY_VALUE_JSON, NULL, name,
1203 strlen(name), &schema, &ext);
1204 LY_CHECK_RET(r && (r != LY_ENOT), r);
1205 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02001206 LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "Term node \"%s\" not found.", name), LY_ENOTFOUND);
Michal Vasko013a8182020-03-03 10:46:53 +01001207
Michal Vasko8cc3f662022-03-29 11:25:51 +02001208 LY_CHECK_RET(lyd_create_term(schema, value, value_len, NULL, format, NULL, LYD_HINT_DATA, NULL, &ret));
1209 if (ext) {
1210 ret->flags |= LYD_EXT;
1211 }
Michal Vaskocbff3e92020-05-27 12:56:41 +02001212 if (parent) {
Michal Vasko6ee6f432021-07-16 09:49:14 +02001213 lyd_insert_node(parent, NULL, ret, 0);
Michal Vasko013a8182020-03-03 10:46:53 +01001214 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02001215
1216 if (node) {
1217 *node = ret;
1218 }
1219 return LY_SUCCESS;
Michal Vasko013a8182020-03-03 10:46:53 +01001220}
1221
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001222LIBYANG_API_DEF LY_ERR
Radek Krejci09c77442021-04-26 11:10:34 +02001223lyd_new_term(struct lyd_node *parent, const struct lys_module *module, const char *name, const char *val_str,
1224 ly_bool output, struct lyd_node **node)
1225{
1226 return _lyd_new_term(parent, module, name, val_str, val_str ? strlen(val_str) : 0, LY_VALUE_JSON, output, node);
1227}
1228
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001229LIBYANG_API_DEF LY_ERR
Radek Krejci09c77442021-04-26 11:10:34 +02001230lyd_new_term_bin(struct lyd_node *parent, const struct lys_module *module, const char *name, const void *value,
1231 size_t value_len, ly_bool output, struct lyd_node **node)
1232{
1233 return _lyd_new_term(parent, module, name, value, value_len, LY_VALUE_LYB, output, node);
1234}
1235
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001236LIBYANG_API_DEF LY_ERR
Radek Krejci09c77442021-04-26 11:10:34 +02001237lyd_new_term_canon(struct lyd_node *parent, const struct lys_module *module, const char *name, const char *val_str,
1238 ly_bool output, struct lyd_node **node)
1239{
1240 return _lyd_new_term(parent, module, name, val_str, val_str ? strlen(val_str) : 0, LY_VALUE_CANON, output, node);
1241}
1242
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001243LIBYANG_API_DEF LY_ERR
Radek Krejci8a5afc22021-03-12 11:10:47 +01001244lyd_new_ext_term(const struct lysc_ext_instance *ext, const char *name, const char *val_str, struct lyd_node **node)
1245{
1246 LY_ERR rc;
1247 struct lyd_node *ret = NULL;
1248 const struct lysc_node *schema;
1249 struct ly_ctx *ctx = ext ? ext->module->ctx : NULL;
1250
1251 LY_CHECK_ARG_RET(ctx, ext, node, name, LY_EINVAL);
1252
1253 schema = lysc_ext_find_node(ext, NULL, name, 0, LYD_NODE_TERM, 0);
1254 if (!schema) {
1255 if (ext->argument) {
1256 LOGERR(ctx, LY_EINVAL, "Term node \"%s\" not found in instance \"%s\" of extension %s.",
1257 name, ext->argument, ext->def->name);
1258 } else {
1259 LOGERR(ctx, LY_EINVAL, "Term node \"%s\" not found in instance of extension %s.", name, ext->def->name);
1260 }
1261 return LY_ENOTFOUND;
1262 }
Radek Krejci8df109d2021-04-23 12:19:08 +02001263 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 +01001264 LY_CHECK_RET(rc);
1265
1266 *node = ret;
1267
1268 return LY_SUCCESS;
1269}
1270
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001271LIBYANG_API_DEF LY_ERR
Michal Vasko2a4ab2b2021-03-04 15:52:40 +01001272lyd_new_any(struct lyd_node *parent, const struct lys_module *module, const char *name, const void *value,
1273 ly_bool use_value, LYD_ANYDATA_VALUETYPE value_type, ly_bool output, struct lyd_node **node)
Michal Vasko013a8182020-03-03 10:46:53 +01001274{
Michal Vasko8cc3f662022-03-29 11:25:51 +02001275 LY_ERR r;
Michal Vasko013a8182020-03-03 10:46:53 +01001276 struct lyd_node *ret = NULL;
1277 const struct lysc_node *schema;
Michal Vasko8cc3f662022-03-29 11:25:51 +02001278 struct lysc_ext_instance *ext = NULL;
Michal Vasko892f5bf2021-11-24 10:41:05 +01001279 const struct ly_ctx *ctx = parent ? LYD_CTX(parent) : (module ? module->ctx : NULL);
Michal Vasko013a8182020-03-03 10:46:53 +01001280
Michal Vasko6027eb92020-07-15 16:37:30 +02001281 LY_CHECK_ARG_RET(ctx, parent || module, parent || node, name, LY_EINVAL);
Michal Vasko892f5bf2021-11-24 10:41:05 +01001282 LY_CHECK_CTX_EQUAL_RET(parent ? LYD_CTX(parent) : NULL, module ? module->ctx : NULL, LY_EINVAL);
Michal Vasko013a8182020-03-03 10:46:53 +01001283
Michal Vaskof03ed032020-03-04 13:31:44 +01001284 if (!module) {
1285 module = parent->schema->module;
1286 }
1287
Radek Krejci41ac9942020-11-02 14:47:56 +01001288 schema = lys_find_child(parent ? parent->schema : NULL, module, name, 0, LYD_NODE_ANY, output ? LYS_GETNEXT_OUTPUT : 0);
Michal Vasko8cc3f662022-03-29 11:25:51 +02001289 if (!schema && parent) {
1290 r = ly_nested_ext_schema(parent, NULL, module->name, strlen(module->name), LY_VALUE_JSON, NULL, name,
1291 strlen(name), &schema, &ext);
1292 LY_CHECK_RET(r && (r != LY_ENOT), r);
1293 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02001294 LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "Any node \"%s\" not found.", name), LY_ENOTFOUND);
Michal Vasko013a8182020-03-03 10:46:53 +01001295
Michal Vasko2a4ab2b2021-03-04 15:52:40 +01001296 LY_CHECK_RET(lyd_create_any(schema, value, value_type, use_value, &ret));
Michal Vasko8cc3f662022-03-29 11:25:51 +02001297 if (ext) {
1298 ret->flags |= LYD_EXT;
1299 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02001300 if (parent) {
Michal Vasko6ee6f432021-07-16 09:49:14 +02001301 lyd_insert_node(parent, NULL, ret, 0);
Michal Vasko013a8182020-03-03 10:46:53 +01001302 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02001303
1304 if (node) {
1305 *node = ret;
1306 }
1307 return LY_SUCCESS;
Michal Vasko013a8182020-03-03 10:46:53 +01001308}
1309
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001310LIBYANG_API_DEF LY_ERR
Radek Krejci0b963da2021-03-12 13:23:44 +01001311lyd_new_ext_any(const struct lysc_ext_instance *ext, const char *name, const void *value, ly_bool use_value,
1312 LYD_ANYDATA_VALUETYPE value_type, struct lyd_node **node)
1313{
1314 struct lyd_node *ret = NULL;
1315 const struct lysc_node *schema;
1316 struct ly_ctx *ctx = ext ? ext->module->ctx : NULL;
1317
1318 LY_CHECK_ARG_RET(ctx, ext, node, name, LY_EINVAL);
1319
1320 schema = lysc_ext_find_node(ext, NULL, name, 0, LYD_NODE_ANY, 0);
1321 if (!schema) {
1322 if (ext->argument) {
1323 LOGERR(ctx, LY_EINVAL, "Any node \"%s\" not found in instance \"%s\" of extension %s.",
1324 name, ext->argument, ext->def->name);
1325 } else {
1326 LOGERR(ctx, LY_EINVAL, "Any node \"%s\" not found in instance of extension %s.", name, ext->def->name);
1327 }
1328 return LY_ENOTFOUND;
1329 }
1330 LY_CHECK_RET(lyd_create_any(schema, value, value_type, use_value, &ret));
1331
1332 *node = ret;
1333
1334 return LY_SUCCESS;
1335}
1336
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001337LIBYANG_API_DEF LY_ERR
Michal Vasko871a0252020-11-11 18:35:24 +01001338lyd_new_meta(const struct ly_ctx *ctx, struct lyd_node *parent, const struct lys_module *module, const char *name,
1339 const char *val_str, ly_bool clear_dflt, struct lyd_meta **meta)
Michal Vaskod86997b2020-05-26 15:19:54 +02001340{
Michal Vaskod86997b2020-05-26 15:19:54 +02001341 const char *prefix, *tmp;
Michal Vaskod86997b2020-05-26 15:19:54 +02001342 size_t pref_len, name_len;
1343
Michal Vasko2b5344c2021-02-26 10:12:05 +01001344 LY_CHECK_ARG_RET(ctx, ctx || parent, name, module || strchr(name, ':'), parent || meta, LY_EINVAL);
Michal Vasko892f5bf2021-11-24 10:41:05 +01001345 LY_CHECK_CTX_EQUAL_RET(ctx, parent ? LYD_CTX(parent) : NULL, module ? module->ctx : NULL, LY_EINVAL);
Michal Vasko2b5344c2021-02-26 10:12:05 +01001346 if (!ctx) {
1347 ctx = LYD_CTX(parent);
1348 }
Michal Vasko00cbf532020-06-15 13:58:47 +02001349
Michal Vasko871a0252020-11-11 18:35:24 +01001350 if (parent && !parent->schema) {
Michal Vasko33b4fab2021-03-04 15:56:12 +01001351 LOGERR(ctx, LY_EINVAL, "Cannot add metadata \"%s\" to an opaque node \"%s\".", name, LYD_NAME(parent));
Michal Vasko871a0252020-11-11 18:35:24 +01001352 return LY_EINVAL;
1353 }
Michal Vasko610553d2020-11-18 18:15:05 +01001354 if (meta) {
1355 *meta = NULL;
1356 }
Michal Vaskod86997b2020-05-26 15:19:54 +02001357
1358 /* parse the name */
1359 tmp = name;
1360 if (ly_parse_nodeid(&tmp, &prefix, &pref_len, &name, &name_len) || tmp[0]) {
1361 LOGERR(ctx, LY_EINVAL, "Metadata name \"%s\" is not valid.", name);
Michal Vasko871a0252020-11-11 18:35:24 +01001362 return LY_EINVAL;
Michal Vaskod86997b2020-05-26 15:19:54 +02001363 }
1364
1365 /* find the module */
1366 if (prefix) {
Radek Krejci0ad51f12020-07-16 12:08:12 +02001367 module = ly_ctx_get_module_implemented2(ctx, prefix, pref_len);
Radek Krejci422afb12021-03-04 16:38:16 +01001368 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 +02001369 }
1370
1371 /* set value if none */
1372 if (!val_str) {
1373 val_str = "";
1374 }
1375
Radek Krejci8df109d2021-04-23 12:19:08 +02001376 return lyd_create_meta(parent, meta, module, name, name_len, val_str, strlen(val_str), NULL, LY_VALUE_JSON,
Michal Vaskoddd76592022-01-17 13:34:48 +01001377 NULL, LYD_HINT_DATA, parent ? parent->schema : NULL, clear_dflt, NULL);
Michal Vasko871a0252020-11-11 18:35:24 +01001378}
Michal Vasko3a41dff2020-07-15 14:30:28 +02001379
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001380LIBYANG_API_DEF LY_ERR
Michal Vaskoba696702020-11-11 19:12:45 +01001381lyd_new_meta2(const struct ly_ctx *ctx, struct lyd_node *parent, ly_bool clear_dflt, const struct lyd_attr *attr,
1382 struct lyd_meta **meta)
1383{
1384 const struct lys_module *mod;
1385
1386 LY_CHECK_ARG_RET(NULL, ctx, attr, parent || meta, LY_EINVAL);
Michal Vasko892f5bf2021-11-24 10:41:05 +01001387 LY_CHECK_CTX_EQUAL_RET(ctx, parent ? LYD_CTX(parent) : NULL, LY_EINVAL);
Michal Vaskoba696702020-11-11 19:12:45 +01001388
1389 if (parent && !parent->schema) {
1390 LOGERR(ctx, LY_EINVAL, "Cannot add metadata to an opaque node \"%s\".", ((struct lyd_node_opaq *)parent)->name);
1391 return LY_EINVAL;
Michal Vasko3a41dff2020-07-15 14:30:28 +02001392 }
Michal Vasko610553d2020-11-18 18:15:05 +01001393 if (meta) {
1394 *meta = NULL;
1395 }
Michal Vaskoba696702020-11-11 19:12:45 +01001396
1397 switch (attr->format) {
Radek Krejci8df109d2021-04-23 12:19:08 +02001398 case LY_VALUE_XML:
Michal Vaskoad92b672020-11-12 13:11:31 +01001399 mod = ly_ctx_get_module_implemented_ns(ctx, attr->name.module_ns);
Michal Vaskoba696702020-11-11 19:12:45 +01001400 if (!mod) {
Michal Vaskoad92b672020-11-12 13:11:31 +01001401 LOGERR(ctx, LY_EINVAL, "Module with namespace \"%s\" not found.", attr->name.module_ns);
Michal Vaskoba696702020-11-11 19:12:45 +01001402 return LY_ENOTFOUND;
1403 }
1404 break;
Radek Krejci8df109d2021-04-23 12:19:08 +02001405 case LY_VALUE_JSON:
Michal Vaskoad92b672020-11-12 13:11:31 +01001406 mod = ly_ctx_get_module_implemented(ctx, attr->name.module_name);
Michal Vaskoba696702020-11-11 19:12:45 +01001407 if (!mod) {
Michal Vaskoad92b672020-11-12 13:11:31 +01001408 LOGERR(ctx, LY_EINVAL, "Module \"%s\" not found.", attr->name.module_name);
Michal Vaskoba696702020-11-11 19:12:45 +01001409 return LY_ENOTFOUND;
1410 }
1411 break;
1412 default:
1413 LOGINT_RET(ctx);
1414 }
1415
Michal Vaskoad92b672020-11-12 13:11:31 +01001416 return lyd_create_meta(parent, meta, mod, attr->name.name, strlen(attr->name.name), attr->value, strlen(attr->value),
Michal Vaskoddd76592022-01-17 13:34:48 +01001417 NULL, attr->format, attr->val_prefix_data, attr->hints, parent ? parent->schema : NULL, clear_dflt, NULL);
Michal Vaskod86997b2020-05-26 15:19:54 +02001418}
1419
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001420LIBYANG_API_DEF LY_ERR
Michal Vasko00cbf532020-06-15 13:58:47 +02001421lyd_new_opaq(struct lyd_node *parent, const struct ly_ctx *ctx, const char *name, const char *value,
Michal Vasko0ab974d2021-02-24 13:18:26 +01001422 const char *prefix, const char *module_name, struct lyd_node **node)
Michal Vasko00cbf532020-06-15 13:58:47 +02001423{
1424 struct lyd_node *ret = NULL;
Radek Krejci07a55962021-03-02 20:16:43 +01001425
Michal Vasko0ab974d2021-02-24 13:18:26 +01001426 LY_CHECK_ARG_RET(ctx, parent || ctx, parent || node, name, module_name, !prefix || !strcmp(prefix, module_name), LY_EINVAL);
Michal Vasko892f5bf2021-11-24 10:41:05 +01001427 LY_CHECK_CTX_EQUAL_RET(ctx, parent ? LYD_CTX(parent) : NULL, LY_EINVAL);
Michal Vasko00cbf532020-06-15 13:58:47 +02001428
1429 if (!ctx) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02001430 ctx = LYD_CTX(parent);
Michal Vasko00cbf532020-06-15 13:58:47 +02001431 }
1432 if (!value) {
1433 value = "";
1434 }
1435
Michal Vasko0ab974d2021-02-24 13:18:26 +01001436 LY_CHECK_RET(lyd_create_opaq(ctx, name, strlen(name), prefix, prefix ? strlen(prefix) : 0, module_name,
Radek Krejci8df109d2021-04-23 12:19:08 +02001437 strlen(module_name), value, strlen(value), NULL, LY_VALUE_JSON, NULL, 0, &ret));
Michal Vasko3a41dff2020-07-15 14:30:28 +02001438 if (parent) {
Michal Vasko6ee6f432021-07-16 09:49:14 +02001439 lyd_insert_node(parent, NULL, ret, 1);
Michal Vasko00cbf532020-06-15 13:58:47 +02001440 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02001441
1442 if (node) {
1443 *node = ret;
1444 }
1445 return LY_SUCCESS;
Michal Vasko00cbf532020-06-15 13:58:47 +02001446}
1447
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001448LIBYANG_API_DEF LY_ERR
Michal Vasko8d65f852021-02-17 11:28:13 +01001449lyd_new_opaq2(struct lyd_node *parent, const struct ly_ctx *ctx, const char *name, const char *value,
Michal Vasko0ab974d2021-02-24 13:18:26 +01001450 const char *prefix, const char *module_ns, struct lyd_node **node)
Michal Vasko8d65f852021-02-17 11:28:13 +01001451{
1452 struct lyd_node *ret = NULL;
1453
1454 LY_CHECK_ARG_RET(ctx, parent || ctx, parent || node, name, module_ns, LY_EINVAL);
Michal Vasko892f5bf2021-11-24 10:41:05 +01001455 LY_CHECK_CTX_EQUAL_RET(ctx, parent ? LYD_CTX(parent) : NULL, LY_EINVAL);
Michal Vasko8d65f852021-02-17 11:28:13 +01001456
1457 if (!ctx) {
1458 ctx = LYD_CTX(parent);
1459 }
1460 if (!value) {
1461 value = "";
1462 }
1463
Michal Vasko0ab974d2021-02-24 13:18:26 +01001464 LY_CHECK_RET(lyd_create_opaq(ctx, name, strlen(name), prefix, prefix ? strlen(prefix) : 0, module_ns,
Radek Krejci8df109d2021-04-23 12:19:08 +02001465 strlen(module_ns), value, strlen(value), NULL, LY_VALUE_XML, NULL, 0, &ret));
Michal Vasko8d65f852021-02-17 11:28:13 +01001466 if (parent) {
Michal Vasko6ee6f432021-07-16 09:49:14 +02001467 lyd_insert_node(parent, NULL, ret, 1);
Michal Vasko8d65f852021-02-17 11:28:13 +01001468 }
1469
1470 if (node) {
1471 *node = ret;
1472 }
1473 return LY_SUCCESS;
1474}
1475
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001476LIBYANG_API_DEF LY_ERR
Michal Vasko8d65f852021-02-17 11:28:13 +01001477lyd_new_attr(struct lyd_node *parent, const char *module_name, const char *name, const char *value,
Radek Krejci0f969882020-08-21 16:56:47 +02001478 struct lyd_attr **attr)
Michal Vasko00cbf532020-06-15 13:58:47 +02001479{
Radek Krejci1798aae2020-07-14 13:26:06 +02001480 struct lyd_attr *ret = NULL;
Michal Vasko00cbf532020-06-15 13:58:47 +02001481 const struct ly_ctx *ctx;
1482 const char *prefix, *tmp;
Michal Vasko51d21b82020-11-13 18:03:54 +01001483 size_t pref_len, name_len, mod_len;
Michal Vasko00cbf532020-06-15 13:58:47 +02001484
Michal Vasko3a41dff2020-07-15 14:30:28 +02001485 LY_CHECK_ARG_RET(NULL, parent, !parent->schema, name, LY_EINVAL);
Michal Vasko00cbf532020-06-15 13:58:47 +02001486
Michal Vaskob7be7a82020-08-20 09:09:04 +02001487 ctx = LYD_CTX(parent);
Michal Vasko00cbf532020-06-15 13:58:47 +02001488
1489 /* parse the name */
1490 tmp = name;
1491 if (ly_parse_nodeid(&tmp, &prefix, &pref_len, &name, &name_len) || tmp[0]) {
Michal Vaskob69b68c2021-02-17 11:18:16 +01001492 LOGERR(ctx, LY_EINVAL, "Attribute name \"%s\" is not valid.", name);
Michal Vasko3a41dff2020-07-15 14:30:28 +02001493 return LY_EVALID;
Michal Vasko00cbf532020-06-15 13:58:47 +02001494 }
1495
Michal Vaskoe137fc42021-07-22 11:53:13 +02001496 if ((pref_len == 3) && !strncmp(prefix, "xml", 3)) {
1497 /* not a prefix but special name */
1498 name = prefix;
1499 name_len += 1 + pref_len;
1500 prefix = NULL;
1501 pref_len = 0;
1502 }
1503
Michal Vasko51d21b82020-11-13 18:03:54 +01001504 /* get the module */
1505 if (module_name) {
1506 mod_len = strlen(module_name);
1507 } else {
1508 module_name = prefix;
1509 mod_len = pref_len;
1510 }
1511
Michal Vasko00cbf532020-06-15 13:58:47 +02001512 /* set value if none */
Michal Vasko8d65f852021-02-17 11:28:13 +01001513 if (!value) {
1514 value = "";
Michal Vasko00cbf532020-06-15 13:58:47 +02001515 }
1516
Michal Vasko8d65f852021-02-17 11:28:13 +01001517 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 +02001518 strlen(value), NULL, LY_VALUE_JSON, NULL, LYD_HINT_DATA));
Michal Vasko8d65f852021-02-17 11:28:13 +01001519
1520 if (attr) {
1521 *attr = ret;
1522 }
1523 return LY_SUCCESS;
1524}
1525
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001526LIBYANG_API_DEF LY_ERR
Michal Vasko8d65f852021-02-17 11:28:13 +01001527lyd_new_attr2(struct lyd_node *parent, const char *module_ns, const char *name, const char *value,
1528 struct lyd_attr **attr)
1529{
1530 struct lyd_attr *ret = NULL;
1531 const struct ly_ctx *ctx;
1532 const char *prefix, *tmp;
1533 size_t pref_len, name_len;
1534
1535 LY_CHECK_ARG_RET(NULL, parent, !parent->schema, name, LY_EINVAL);
1536
1537 ctx = LYD_CTX(parent);
1538
1539 /* parse the name */
1540 tmp = name;
1541 if (ly_parse_nodeid(&tmp, &prefix, &pref_len, &name, &name_len) || tmp[0]) {
1542 LOGERR(ctx, LY_EINVAL, "Attribute name \"%s\" is not valid.", name);
1543 return LY_EVALID;
1544 }
1545
Michal Vaskoe137fc42021-07-22 11:53:13 +02001546 if ((pref_len == 3) && !strncmp(prefix, "xml", 3)) {
1547 /* not a prefix but special name */
1548 name = prefix;
1549 name_len += 1 + pref_len;
1550 prefix = NULL;
1551 pref_len = 0;
1552 }
1553
Michal Vasko8d65f852021-02-17 11:28:13 +01001554 /* set value if none */
1555 if (!value) {
1556 value = "";
1557 }
1558
1559 LY_CHECK_RET(lyd_create_attr(parent, &ret, ctx, name, name_len, prefix, pref_len, module_ns,
Radek Krejci8df109d2021-04-23 12:19:08 +02001560 module_ns ? strlen(module_ns) : 0, value, strlen(value), NULL, LY_VALUE_XML, NULL, LYD_HINT_DATA));
Michal Vasko3a41dff2020-07-15 14:30:28 +02001561
1562 if (attr) {
1563 *attr = ret;
1564 }
1565 return LY_SUCCESS;
Michal Vasko00cbf532020-06-15 13:58:47 +02001566}
1567
Radek Krejci09c77442021-04-26 11:10:34 +02001568/**
1569 * @brief Change the value of a term (leaf or leaf-list) node.
1570 *
1571 * Node changed this way is always considered explicitly set, meaning its default flag
1572 * is always cleared.
1573 *
1574 * @param[in] term Term node to change.
1575 * @param[in] value New value to set.
1576 * @param[in] value_len Length of @p value.
1577 * @param[in] format Format of @p value.
1578 * @return LY_SUCCESS if value was changed,
1579 * @return LY_EEXIST if value was the same and only the default flag was cleared,
1580 * @return LY_ENOT if the values were equal and no change occured,
1581 * @return LY_ERR value on other errors.
1582 */
1583static LY_ERR
1584_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 +02001585{
1586 LY_ERR ret = LY_SUCCESS;
1587 struct lysc_type *type;
1588 struct lyd_node_term *t;
1589 struct lyd_node *parent;
1590 struct lyd_value val = {0};
Radek Krejci857189e2020-09-01 13:26:36 +02001591 ly_bool dflt_change, val_change;
Michal Vasko00cbf532020-06-15 13:58:47 +02001592
Radek Krejci09c77442021-04-26 11:10:34 +02001593 assert(term && term->schema && (term->schema->nodetype & LYD_NODE_TERM));
Michal Vasko00cbf532020-06-15 13:58:47 +02001594
Michal Vasko00cbf532020-06-15 13:58:47 +02001595 t = (struct lyd_node_term *)term;
1596 type = ((struct lysc_node_leaf *)term->schema)->type;
1597
1598 /* parse the new value */
Radek Krejciddace2c2021-01-08 11:30:56 +01001599 LOG_LOCSET(term->schema, term, NULL, NULL);
Radek Krejci09c77442021-04-26 11:10:34 +02001600 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 +01001601 LOG_LOCBACK(term->schema ? 1 : 0, 1, 0, 0);
Michal Vaskofeca4fb2020-10-05 08:58:40 +02001602 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko00cbf532020-06-15 13:58:47 +02001603
1604 /* compare original and new value */
1605 if (type->plugin->compare(&t->value, &val)) {
1606 /* values differ, switch them */
Michal Vaskob7be7a82020-08-20 09:09:04 +02001607 type->plugin->free(LYD_CTX(term), &t->value);
Michal Vasko00cbf532020-06-15 13:58:47 +02001608 t->value = val;
1609 memset(&val, 0, sizeof val);
1610 val_change = 1;
1611 } else {
1612 val_change = 0;
1613 }
1614
1615 /* always clear the default flag */
1616 if (term->flags & LYD_DEFAULT) {
Michal Vasko9e685082021-01-29 14:49:09 +01001617 for (parent = term; parent; parent = lyd_parent(parent)) {
Michal Vasko00cbf532020-06-15 13:58:47 +02001618 parent->flags &= ~LYD_DEFAULT;
1619 }
1620 dflt_change = 1;
1621 } else {
1622 dflt_change = 0;
1623 }
1624
1625 if (val_change || dflt_change) {
1626 /* make the node non-validated */
1627 term->flags &= LYD_NEW;
1628 }
1629
1630 if (val_change) {
1631 if (term->schema->nodetype == LYS_LEAFLIST) {
1632 /* leaf-list needs to be hashed again and re-inserted into parent */
1633 lyd_unlink_hash(term);
1634 lyd_hash(term);
1635 LY_CHECK_GOTO(ret = lyd_insert_hash(term), cleanup);
1636 } else if ((term->schema->flags & LYS_KEY) && term->parent) {
1637 /* list needs to be updated if its key was changed */
1638 assert(term->parent->schema->nodetype == LYS_LIST);
Michal Vasko9e685082021-01-29 14:49:09 +01001639 lyd_unlink_hash(lyd_parent(term));
1640 lyd_hash(lyd_parent(term));
1641 LY_CHECK_GOTO(ret = lyd_insert_hash(lyd_parent(term)), cleanup);
Michal Vasko00cbf532020-06-15 13:58:47 +02001642 } /* else leaf that is not a key, its value is not used for its hash so it does not change */
1643 }
1644
1645 /* retrun value */
1646 if (!val_change) {
1647 if (dflt_change) {
1648 /* only default flag change */
1649 ret = LY_EEXIST;
1650 } else {
1651 /* no change */
1652 ret = LY_ENOT;
1653 }
1654 } /* else value changed, LY_SUCCESS */
1655
1656cleanup:
Michal Vasko59512fc2020-12-09 18:13:29 +01001657 if (val.realtype) {
1658 type->plugin->free(LYD_CTX(term), &val);
1659 }
Michal Vasko00cbf532020-06-15 13:58:47 +02001660 return ret;
1661}
1662
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001663LIBYANG_API_DEF LY_ERR
Radek Krejci09c77442021-04-26 11:10:34 +02001664lyd_change_term(struct lyd_node *term, const char *val_str)
1665{
1666 LY_CHECK_ARG_RET(NULL, term, term->schema, term->schema->nodetype & LYD_NODE_TERM, LY_EINVAL);
1667
1668 return _lyd_change_term(term, val_str, val_str ? strlen(val_str) : 0, LY_VALUE_JSON);
1669}
1670
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001671LIBYANG_API_DEF LY_ERR
Radek Krejci09c77442021-04-26 11:10:34 +02001672lyd_change_term_bin(struct lyd_node *term, const void *value, size_t value_len)
1673{
1674 LY_CHECK_ARG_RET(NULL, term, term->schema, term->schema->nodetype & LYD_NODE_TERM, LY_EINVAL);
1675
1676 return _lyd_change_term(term, value, value_len, LY_VALUE_LYB);
1677}
1678
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001679LIBYANG_API_DEF LY_ERR
Radek Krejci09c77442021-04-26 11:10:34 +02001680lyd_change_term_canon(struct lyd_node *term, const char *val_str)
1681{
1682 LY_CHECK_ARG_RET(NULL, term, term->schema, term->schema->nodetype & LYD_NODE_TERM, LY_EINVAL);
1683
1684 return _lyd_change_term(term, val_str, val_str ? strlen(val_str) : 0, LY_VALUE_CANON);
1685}
1686
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001687LIBYANG_API_DEF LY_ERR
Michal Vasko41586352020-07-13 13:54:25 +02001688lyd_change_meta(struct lyd_meta *meta, const char *val_str)
1689{
1690 LY_ERR ret = LY_SUCCESS;
Radek Krejci2b18bf12020-11-06 11:20:20 +01001691 struct lyd_meta *m2 = NULL;
Michal Vasko41586352020-07-13 13:54:25 +02001692 struct lyd_value val;
Radek Krejci857189e2020-09-01 13:26:36 +02001693 ly_bool val_change;
Michal Vasko41586352020-07-13 13:54:25 +02001694
1695 LY_CHECK_ARG_RET(NULL, meta, LY_EINVAL);
1696
1697 if (!val_str) {
1698 val_str = "";
1699 }
1700
1701 /* parse the new value into a new meta structure */
Michal Vaskoddd76592022-01-17 13:34:48 +01001702 ret = lyd_create_meta(NULL, &m2, meta->annotation->module, meta->name, strlen(meta->name), val_str, strlen(val_str),
1703 NULL, LY_VALUE_JSON, NULL, LYD_HINT_DATA, meta->parent ? meta->parent->schema : NULL, 0, NULL);
1704 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko41586352020-07-13 13:54:25 +02001705
1706 /* compare original and new value */
1707 if (lyd_compare_meta(meta, m2)) {
1708 /* values differ, switch them */
1709 val = meta->value;
1710 meta->value = m2->value;
1711 m2->value = val;
1712 val_change = 1;
1713 } else {
1714 val_change = 0;
1715 }
1716
1717 /* retrun value */
1718 if (!val_change) {
1719 /* no change */
1720 ret = LY_ENOT;
1721 } /* else value changed, LY_SUCCESS */
1722
1723cleanup:
Michal Vasko1a66a5d2020-11-18 18:15:37 +01001724 lyd_free_meta_single(m2);
Michal Vasko41586352020-07-13 13:54:25 +02001725 return ret;
1726}
1727
Radek Krejci09c77442021-04-26 11:10:34 +02001728/**
1729 * @brief Update node value.
1730 *
1731 * @param[in] node Node to update.
1732 * @param[in] value New value to set.
1733 * @param[in] value_len Length of @p value.
1734 * @param[in] value_type Type of @p value for anydata/anyxml node.
1735 * @param[in] format Format of @p value.
1736 * @param[out] new_parent Set to @p node if the value was updated, otherwise set to NULL.
1737 * @param[out] new_node Set to @p node if the value was updated, otherwise set to NULL.
1738 * @return LY_ERR value.
1739 */
Michal Vasko6741dc62021-02-04 09:27:45 +01001740static LY_ERR
Radek Krejci09c77442021-04-26 11:10:34 +02001741lyd_new_path_update(struct lyd_node *node, const void *value, size_t value_len, LYD_ANYDATA_VALUETYPE value_type,
1742 LY_VALUE_FORMAT format, struct lyd_node **new_parent, struct lyd_node **new_node)
1743{
1744 LY_ERR ret = LY_SUCCESS;
1745 struct lyd_node *new_any;
1746
1747 switch (node->schema->nodetype) {
1748 case LYS_CONTAINER:
1749 case LYS_NOTIF:
1750 case LYS_RPC:
1751 case LYS_ACTION:
1752 case LYS_LIST:
1753 /* if it exists, there is nothing to update */
1754 *new_parent = NULL;
1755 *new_node = NULL;
1756 break;
1757 case LYS_LEAFLIST:
1758 if (!lysc_is_dup_inst_list(node->schema)) {
1759 /* if it exists, there is nothing to update */
1760 *new_parent = NULL;
1761 *new_node = NULL;
1762 break;
1763 }
1764 /* fallthrough */
1765 case LYS_LEAF:
1766 ret = _lyd_change_term(node, value, value_len, format);
1767 if ((ret == LY_SUCCESS) || (ret == LY_EEXIST)) {
1768 /* there was an actual change (at least of the default flag) */
1769 *new_parent = node;
1770 *new_node = node;
1771 ret = LY_SUCCESS;
1772 } else if (ret == LY_ENOT) {
1773 /* no change */
1774 *new_parent = NULL;
1775 *new_node = NULL;
1776 ret = LY_SUCCESS;
1777 } /* else error */
1778 break;
1779 case LYS_ANYDATA:
1780 case LYS_ANYXML:
1781 /* create a new any node */
1782 LY_CHECK_RET(lyd_create_any(node->schema, value, value_type, 0, &new_any));
1783
1784 /* compare with the existing one */
1785 if (lyd_compare_single(node, new_any, 0)) {
1786 /* not equal, switch values (so that we can use generic node free) */
1787 ((struct lyd_node_any *)new_any)->value = ((struct lyd_node_any *)node)->value;
1788 ((struct lyd_node_any *)new_any)->value_type = ((struct lyd_node_any *)node)->value_type;
1789 ((struct lyd_node_any *)node)->value.str = value;
1790 ((struct lyd_node_any *)node)->value_type = value_type;
1791
1792 *new_parent = node;
1793 *new_node = node;
1794 } else {
1795 /* they are equal */
1796 *new_parent = NULL;
1797 *new_node = NULL;
1798 }
1799 lyd_free_tree(new_any);
1800 break;
1801 default:
1802 LOGINT(LYD_CTX(node));
1803 ret = LY_EINT;
1804 break;
1805 }
1806
1807 return ret;
1808}
1809
1810static LY_ERR
1811lyd_new_path_check_find_lypath(struct ly_path *path, const char *str_path, const char *value, size_t value_len,
1812 LY_VALUE_FORMAT format, uint32_t options)
Michal Vasko6741dc62021-02-04 09:27:45 +01001813{
Michal Vaskoe78faec2021-04-08 17:24:43 +02001814 LY_ERR r;
Michal Vasko6741dc62021-02-04 09:27:45 +01001815 struct ly_path_predicate *pred;
Michal Vaskoe1886a92022-01-10 09:30:08 +01001816 struct lyd_value val;
Michal Vaskoe78faec2021-04-08 17:24:43 +02001817 const struct lysc_node *schema = NULL;
1818 LY_ARRAY_COUNT_TYPE u, new_count;
1819 int create = 0;
Michal Vasko6741dc62021-02-04 09:27:45 +01001820
1821 assert(path);
1822
1823 /* go through all the compiled nodes */
1824 LY_ARRAY_FOR(path, u) {
1825 schema = path[u].node;
Michal Vaskoe78faec2021-04-08 17:24:43 +02001826
1827 if (lysc_is_dup_inst_list(schema)) {
1828 if (path[u].pred_type == LY_PATH_PREDTYPE_NONE) {
1829 /* creating a new key-less list or state leaf-list instance */
1830 create = 1;
1831 new_count = u;
1832 } else if (path[u].pred_type != LY_PATH_PREDTYPE_POSITION) {
1833 LOG_LOCSET(schema, NULL, NULL, NULL);
1834 LOGVAL(schema->module->ctx, LYVE_XPATH, "Invalid predicate for %s \"%s\" in path \"%s\".",
1835 lys_nodetype2str(schema->nodetype), schema->name, str_path);
1836 LOG_LOCBACK(1, 0, 0, 0);
1837 return LY_EINVAL;
1838 }
1839 } else if ((schema->nodetype == LYS_LIST) && (path[u].pred_type != LY_PATH_PREDTYPE_LIST)) {
1840 if ((u < LY_ARRAY_COUNT(path) - 1) || !(options & LYD_NEW_PATH_OPAQ)) {
Michal Vasko6741dc62021-02-04 09:27:45 +01001841 LOG_LOCSET(schema, NULL, NULL, NULL);
1842 LOGVAL(schema->module->ctx, LYVE_XPATH, "Predicate missing for %s \"%s\" in path \"%s\".",
1843 lys_nodetype2str(schema->nodetype), schema->name, str_path);
1844 LOG_LOCBACK(1, 0, 0, 0);
1845 return LY_EINVAL;
Michal Vaskoe78faec2021-04-08 17:24:43 +02001846 } /* else creating an opaque list */
1847 } else if ((schema->nodetype == LYS_LEAFLIST) && (path[u].pred_type != LY_PATH_PREDTYPE_LEAFLIST)) {
Michal Vaskoe78faec2021-04-08 17:24:43 +02001848 r = LY_SUCCESS;
1849 if (options & LYD_NEW_PATH_OPAQ) {
Radek Krejci09c77442021-04-26 11:10:34 +02001850 r = lyd_value_validate(NULL, schema, value, value_len, NULL, NULL, NULL);
Michal Vaskoe78faec2021-04-08 17:24:43 +02001851 }
1852 if (!r) {
Michal Vaskoe1886a92022-01-10 09:30:08 +01001853 /* try to store the value */
1854 LY_CHECK_RET(lyd_value_store(schema->module->ctx, &val, ((struct lysc_node_leaflist *)schema)->type,
1855 value, value_len, NULL, format, NULL, LYD_HINT_DATA, schema, NULL));
1856 ++((struct lysc_type *)val.realtype)->refcount;
1857
Michal Vaskoe78faec2021-04-08 17:24:43 +02001858 /* store the new predicate so that it is used when searching for this instance */
1859 path[u].pred_type = LY_PATH_PREDTYPE_LEAFLIST;
1860 LY_ARRAY_NEW_RET(schema->module->ctx, path[u].predicates, pred, LY_EMEM);
Michal Vaskoe1886a92022-01-10 09:30:08 +01001861 pred->value = val;
Michal Vaskoe78faec2021-04-08 17:24:43 +02001862 } /* else we have opaq flag and the value is not valid, leave no predicate and then create an opaque node */
1863 }
Michal Vasko6741dc62021-02-04 09:27:45 +01001864 }
1865
Michal Vaskoe78faec2021-04-08 17:24:43 +02001866 if (create) {
1867 /* hide the nodes that should always be created so they are not found */
1868 while (new_count < LY_ARRAY_COUNT(path)) {
1869 LY_ARRAY_DECREMENT(path);
1870 }
Michal Vasko6741dc62021-02-04 09:27:45 +01001871 }
1872
Michal Vaskoe78faec2021-04-08 17:24:43 +02001873 return LY_SUCCESS;
Michal Vasko6741dc62021-02-04 09:27:45 +01001874}
1875
Radek Krejci95ccd1b2021-03-12 14:57:22 +01001876/**
1877 * @brief Create a new node in the data tree based on a path. All node types can be created.
1878 *
Michal Vaskoa918a632021-07-02 11:53:36 +02001879 * If @p path points to a list key, the key value from the predicate is used and @p value is ignored.
1880 * Also, if a leaf-list is being created and both a predicate is defined in @p path
Radek Krejci95ccd1b2021-03-12 14:57:22 +01001881 * and @p value is set, the predicate is preferred.
1882 *
1883 * For key-less lists and state leaf-lists, positional predicates can be used. If no preciate is used for these
1884 * nodes, they are always created.
1885 *
1886 * @param[in] parent Data parent to add to/modify, can be NULL. Note that in case a first top-level sibling is used,
1887 * it may no longer be first if @p path is absolute and starts with a non-existing top-level node inserted
1888 * before @p parent. Use ::lyd_first_sibling() to adjust @p parent in these cases.
1889 * @param[in] ctx libyang context, must be set if @p parent is NULL.
1890 * @param[in] ext Extension instance where the node being created is defined. This argument takes effect only for absolute
1891 * path or when the relative paths touches document root (top-level). In such cases the present extension instance replaces
1892 * searching for the appropriate module.
1893 * @param[in] path [Path](@ref howtoXPath) to create.
Radek Krejci09c77442021-04-26 11:10:34 +02001894 * @param[in] value Value of the new leaf/leaf-list (const char *) in ::LY_VALUE_JSON format. If creating an
1895 * anyxml/anydata node, the expected type depends on @p value_type. For other node types, it should be NULL.
1896 * @param[in] value_len Length of @p value in bytes. May be 0 if @p value is a zero-terminated string. Ignored when
1897 * creating anyxml/anydata nodes.
Radek Krejci95ccd1b2021-03-12 14:57:22 +01001898 * @param[in] value_type Anyxml/anydata node @p value type.
1899 * @param[in] options Bitmask of options, see @ref pathoptions.
1900 * @param[out] new_parent Optional first parent node created. If only one node was created, equals to @p new_node.
1901 * @param[out] new_node Optional last node created.
1902 * @return LY_ERR value.
1903 */
1904static LY_ERR
1905lyd_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 +02001906 const void *value, size_t value_len, LYD_ANYDATA_VALUETYPE value_type, uint32_t options,
Radek Krejci95ccd1b2021-03-12 14:57:22 +01001907 struct lyd_node **new_parent, struct lyd_node **new_node)
Michal Vasko00cbf532020-06-15 13:58:47 +02001908{
1909 LY_ERR ret = LY_SUCCESS, r;
1910 struct lyxp_expr *exp = NULL;
1911 struct ly_path *p = NULL;
1912 struct lyd_node *nparent = NULL, *nnode = NULL, *node = NULL, *cur_parent;
1913 const struct lysc_node *schema;
Michal Vaskoe78faec2021-04-08 17:24:43 +02001914 const struct lyd_value *val = NULL;
Radek Krejci910d0132021-03-05 14:19:31 +01001915 LY_ARRAY_COUNT_TYPE path_idx = 0, orig_count = 0;
Radek Krejci09c77442021-04-26 11:10:34 +02001916 LY_VALUE_FORMAT format;
Michal Vasko00cbf532020-06-15 13:58:47 +02001917
Radek Krejci95ccd1b2021-03-12 14:57:22 +01001918 assert(parent || ctx);
Radek Krejci09c77442021-04-26 11:10:34 +02001919 assert(path && ((path[0] == '/') || parent));
1920 assert(!(options & LYD_NEW_PATH_BIN_VALUE) || !(options & LYD_NEW_PATH_CANON_VALUE));
Michal Vasko00cbf532020-06-15 13:58:47 +02001921
1922 if (!ctx) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02001923 ctx = LYD_CTX(parent);
Michal Vasko00cbf532020-06-15 13:58:47 +02001924 }
Radek Krejci09c77442021-04-26 11:10:34 +02001925 if (value && !value_len) {
1926 value_len = strlen(value);
1927 }
1928 if (options & LYD_NEW_PATH_BIN_VALUE) {
1929 format = LY_VALUE_LYB;
1930 } else if (options & LYD_NEW_PATH_CANON_VALUE) {
1931 format = LY_VALUE_CANON;
1932 } else {
1933 format = LY_VALUE_JSON;
1934 }
Michal Vasko00cbf532020-06-15 13:58:47 +02001935
1936 /* parse path */
Michal Vaskoed725d72021-06-23 12:03:45 +02001937 LY_CHECK_GOTO(ret = ly_path_parse(ctx, NULL, path, strlen(path), 0, LY_PATH_BEGIN_EITHER, LY_PATH_PREFIX_OPTIONAL,
1938 LY_PATH_PRED_SIMPLE, &exp), cleanup);
Michal Vasko00cbf532020-06-15 13:58:47 +02001939
1940 /* compile path */
Michal Vasko106f0862021-11-02 11:49:27 +01001941 LY_CHECK_GOTO(ret = ly_path_compile(ctx, NULL, lyd_node_schema(parent), ext, exp, options & LYD_NEW_PATH_OUTPUT ?
1942 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 +02001943
Michal Vasko6741dc62021-02-04 09:27:45 +01001944 /* check the compiled path before searching existing nodes, it may be shortened */
1945 orig_count = LY_ARRAY_COUNT(p);
Radek Krejci09c77442021-04-26 11:10:34 +02001946 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 +02001947
1948 /* try to find any existing nodes in the path */
1949 if (parent) {
1950 ret = ly_path_eval_partial(p, parent, &path_idx, &node);
1951 if (ret == LY_SUCCESS) {
Michal Vasko6741dc62021-02-04 09:27:45 +01001952 if (orig_count == LY_ARRAY_COUNT(p)) {
1953 /* the node exists, are we supposed to update it or is it just a default? */
1954 if (!(options & LYD_NEW_PATH_UPDATE) && !(node->flags & LYD_DEFAULT)) {
1955 LOG_LOCSET(NULL, node, NULL, NULL);
1956 LOGVAL(ctx, LYVE_REFERENCE, "Path \"%s\" already exists", path);
1957 LOG_LOCBACK(0, 1, 0, 0);
1958 ret = LY_EEXIST;
1959 goto cleanup;
1960 }
Michal Vasko00cbf532020-06-15 13:58:47 +02001961
Michal Vasko6741dc62021-02-04 09:27:45 +01001962 /* update the existing node */
Radek Krejci09c77442021-04-26 11:10:34 +02001963 ret = lyd_new_path_update(node, value, value_len, value_type, format, &nparent, &nnode);
Michal Vasko6741dc62021-02-04 09:27:45 +01001964 goto cleanup;
1965 } /* else we were not searching for the whole path */
Michal Vasko00cbf532020-06-15 13:58:47 +02001966 } else if (ret == LY_EINCOMPLETE) {
1967 /* some nodes were found, adjust the iterator to the next segment */
1968 ++path_idx;
1969 } else if (ret == LY_ENOTFOUND) {
1970 /* 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 +01001971 if (lysc_data_parent(p[0].node)) {
Michal Vasko00cbf532020-06-15 13:58:47 +02001972 node = parent;
1973 }
1974 } else {
1975 /* error */
1976 goto cleanup;
1977 }
1978 }
1979
Michal Vasko6741dc62021-02-04 09:27:45 +01001980 /* restore the full path for creating new nodes */
1981 while (orig_count > LY_ARRAY_COUNT(p)) {
1982 LY_ARRAY_INCREMENT(p);
1983 }
1984
Michal Vasko00cbf532020-06-15 13:58:47 +02001985 /* create all the non-existing nodes in a loop */
Michal Vaskod989ba02020-08-24 10:59:24 +02001986 for ( ; path_idx < LY_ARRAY_COUNT(p); ++path_idx) {
Michal Vasko00cbf532020-06-15 13:58:47 +02001987 cur_parent = node;
1988 schema = p[path_idx].node;
1989
1990 switch (schema->nodetype) {
1991 case LYS_LIST:
Michal Vaskoe78faec2021-04-08 17:24:43 +02001992 if (lysc_is_dup_inst_list(schema)) {
1993 /* create key-less list instance */
1994 LY_CHECK_GOTO(ret = lyd_create_inner(schema, &node), cleanup);
1995 } else if ((options & LYD_NEW_PATH_OPAQ) && (p[path_idx].pred_type == LY_PATH_PREDTYPE_NONE)) {
1996 /* creating opaque list without keys */
1997 LY_CHECK_GOTO(ret = lyd_create_opaq(ctx, schema->name, strlen(schema->name), NULL, 0,
Radek Krejci8df109d2021-04-23 12:19:08 +02001998 schema->module->name, strlen(schema->module->name), NULL, 0, NULL, LY_VALUE_JSON, NULL,
Michal Vaskoe78faec2021-04-08 17:24:43 +02001999 LYD_NODEHINT_LIST, &node), cleanup);
2000 } else {
2001 /* create standard list instance */
2002 assert(p[path_idx].pred_type == LY_PATH_PREDTYPE_LIST);
2003 LY_CHECK_GOTO(ret = lyd_create_list(schema, p[path_idx].predicates, &node), cleanup);
Michal Vasko00cbf532020-06-15 13:58:47 +02002004 }
Michal Vaskoe78faec2021-04-08 17:24:43 +02002005 break;
Michal Vasko00cbf532020-06-15 13:58:47 +02002006 case LYS_CONTAINER:
2007 case LYS_NOTIF:
2008 case LYS_RPC:
2009 case LYS_ACTION:
2010 LY_CHECK_GOTO(ret = lyd_create_inner(schema, &node), cleanup);
2011 break;
2012 case LYS_LEAFLIST:
Michal Vaskoe78faec2021-04-08 17:24:43 +02002013 if ((options & LYD_NEW_PATH_OPAQ) && (p[path_idx].pred_type != LY_PATH_PREDTYPE_LEAFLIST)) {
2014 /* we have not checked this only for dup-inst lists, otherwise it must be opaque */
2015 r = LY_EVALID;
2016 if (lysc_is_dup_inst_list(schema)) {
2017 /* validate value */
Radek Krejci09c77442021-04-26 11:10:34 +02002018 r = lyd_value_validate(NULL, schema, value ? value : "", value_len, NULL, NULL, NULL);
Michal Vaskoe78faec2021-04-08 17:24:43 +02002019 }
2020 if (r && (r != LY_EINCOMPLETE)) {
2021 /* creating opaque leaf-list */
Radek Krejci09c77442021-04-26 11:10:34 +02002022 LY_CHECK_GOTO(ret = lyd_create_opaq(ctx, schema->name, strlen(schema->name), value, value_len,
2023 schema->module->name, strlen(schema->module->name), NULL, 0, NULL, format, NULL,
2024 LYD_NODEHINT_LEAFLIST, &node), cleanup);
Michal Vaskoe78faec2021-04-08 17:24:43 +02002025 break;
2026 }
2027 }
2028
2029 /* get value to set */
2030 if (p[path_idx].pred_type == LY_PATH_PREDTYPE_LEAFLIST) {
2031 val = &p[path_idx].predicates[0].value;
Michal Vaskoe78faec2021-04-08 17:24:43 +02002032 }
2033
2034 /* create a leaf-list instance */
2035 if (val) {
Michal Vasko00cbf532020-06-15 13:58:47 +02002036 LY_CHECK_GOTO(ret = lyd_create_term2(schema, &p[path_idx].predicates[0].value, &node), cleanup);
Michal Vaskoe78faec2021-04-08 17:24:43 +02002037 } else {
Radek Krejci09c77442021-04-26 11:10:34 +02002038 LY_CHECK_GOTO(ret = lyd_create_term(schema, value, value_len, NULL, format, NULL, LYD_HINT_DATA,
2039 NULL, &node), cleanup);
Michal Vasko00cbf532020-06-15 13:58:47 +02002040 }
2041 break;
2042 case LYS_LEAF:
Michal Vasko106f0862021-11-02 11:49:27 +01002043 if (lysc_is_key(schema) && cur_parent->schema) {
Michal Vasko35880332020-12-08 13:08:12 +01002044 /* it must have been already created or some error will occur later */
Michal Vaskoa918a632021-07-02 11:53:36 +02002045 lyd_find_sibling_schema(lyd_child(cur_parent), schema, &node);
2046 assert(node);
Michal Vasko35880332020-12-08 13:08:12 +01002047 goto next_iter;
2048 }
2049
Michal Vaskoe78faec2021-04-08 17:24:43 +02002050 if (options & LYD_NEW_PATH_OPAQ) {
Michal Vasko106f0862021-11-02 11:49:27 +01002051 if (cur_parent && !cur_parent->schema) {
2052 /* always create opaque nodes for opaque parents */
2053 r = LY_ENOT;
2054 } else {
2055 /* validate value */
2056 r = lyd_value_validate(NULL, schema, value ? value : "", value_len, NULL, NULL, NULL);
2057 }
Michal Vaskoe78faec2021-04-08 17:24:43 +02002058 if (r && (r != LY_EINCOMPLETE)) {
2059 /* creating opaque leaf */
Radek Krejci09c77442021-04-26 11:10:34 +02002060 LY_CHECK_GOTO(ret = lyd_create_opaq(ctx, schema->name, strlen(schema->name), value, value_len,
2061 schema->module->name, strlen(schema->module->name), NULL, 0, NULL, format, NULL, 0, &node),
2062 cleanup);
Michal Vaskoe78faec2021-04-08 17:24:43 +02002063 break;
2064 }
2065 }
2066
Michal Vaskoe78faec2021-04-08 17:24:43 +02002067 /* create a leaf instance */
Radek Krejci09c77442021-04-26 11:10:34 +02002068 LY_CHECK_GOTO(ret = lyd_create_term(schema, value, value_len, NULL, format, NULL, LYD_HINT_DATA, NULL,
2069 &node), cleanup);
Michal Vasko00cbf532020-06-15 13:58:47 +02002070 break;
2071 case LYS_ANYDATA:
2072 case LYS_ANYXML:
Michal Vasko366a4a12020-12-04 16:23:57 +01002073 LY_CHECK_GOTO(ret = lyd_create_any(schema, value, value_type, 0, &node), cleanup);
Michal Vasko00cbf532020-06-15 13:58:47 +02002074 break;
2075 default:
2076 LOGINT(ctx);
2077 ret = LY_EINT;
2078 goto cleanup;
2079 }
2080
Michal Vasko8cc3f662022-03-29 11:25:51 +02002081 if (p[path_idx].ext) {
2082 node->flags |= LYD_EXT;
2083 }
Michal Vasko00cbf532020-06-15 13:58:47 +02002084 if (cur_parent) {
2085 /* connect to the parent */
Michal Vasko6ee6f432021-07-16 09:49:14 +02002086 lyd_insert_node(cur_parent, NULL, node, 0);
Michal Vasko00cbf532020-06-15 13:58:47 +02002087 } else if (parent) {
2088 /* connect to top-level siblings */
Michal Vasko6ee6f432021-07-16 09:49:14 +02002089 lyd_insert_node(NULL, &parent, node, 0);
Michal Vasko00cbf532020-06-15 13:58:47 +02002090 }
2091
Michal Vasko35880332020-12-08 13:08:12 +01002092next_iter:
Michal Vasko00cbf532020-06-15 13:58:47 +02002093 /* update remembered nodes */
2094 if (!nparent) {
2095 nparent = node;
2096 }
2097 nnode = node;
2098 }
2099
2100cleanup:
2101 lyxp_expr_free(ctx, exp);
Michal Vasko6741dc62021-02-04 09:27:45 +01002102 if (p) {
2103 while (orig_count > LY_ARRAY_COUNT(p)) {
2104 LY_ARRAY_INCREMENT(p);
2105 }
2106 }
Michal Vasko00cbf532020-06-15 13:58:47 +02002107 ly_path_free(ctx, p);
2108 if (!ret) {
2109 /* set out params only on success */
2110 if (new_parent) {
2111 *new_parent = nparent;
2112 }
2113 if (new_node) {
2114 *new_node = nnode;
2115 }
Michal Vaskof4b95ba2020-12-11 08:42:33 +01002116 } else {
2117 lyd_free_tree(nparent);
Michal Vasko00cbf532020-06-15 13:58:47 +02002118 }
2119 return ret;
2120}
2121
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002122LIBYANG_API_DEF LY_ERR
Radek Krejci95ccd1b2021-03-12 14:57:22 +01002123lyd_new_path(struct lyd_node *parent, const struct ly_ctx *ctx, const char *path, const char *value, uint32_t options,
2124 struct lyd_node **node)
2125{
Radek Krejci09c77442021-04-26 11:10:34 +02002126 LY_CHECK_ARG_RET(ctx, parent || ctx, path, (path[0] == '/') || parent,
2127 !(options & LYD_NEW_PATH_BIN_VALUE) || !(options & LYD_NEW_PATH_CANON_VALUE), LY_EINVAL);
Michal Vasko892f5bf2021-11-24 10:41:05 +01002128 LY_CHECK_CTX_EQUAL_RET(parent ? LYD_CTX(parent) : NULL, ctx, LY_EINVAL);
Radek Krejci09c77442021-04-26 11:10:34 +02002129
2130 return lyd_new_path_(parent, ctx, NULL, path, value, 0, LYD_ANYDATA_STRING, options, node, NULL);
Radek Krejci95ccd1b2021-03-12 14:57:22 +01002131}
2132
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002133LIBYANG_API_DEF LY_ERR
Radek Krejci95ccd1b2021-03-12 14:57:22 +01002134lyd_new_path2(struct lyd_node *parent, const struct ly_ctx *ctx, const char *path, const void *value,
Radek Krejci09c77442021-04-26 11:10:34 +02002135 size_t value_len, LYD_ANYDATA_VALUETYPE value_type, uint32_t options, struct lyd_node **new_parent,
2136 struct lyd_node **new_node)
Radek Krejci95ccd1b2021-03-12 14:57:22 +01002137{
Radek Krejci09c77442021-04-26 11:10:34 +02002138 LY_CHECK_ARG_RET(ctx, parent || ctx, path, (path[0] == '/') || parent,
2139 !(options & LYD_NEW_PATH_BIN_VALUE) || !(options & LYD_NEW_PATH_CANON_VALUE), LY_EINVAL);
Michal Vasko892f5bf2021-11-24 10:41:05 +01002140 LY_CHECK_CTX_EQUAL_RET(parent ? LYD_CTX(parent) : NULL, ctx, LY_EINVAL);
2141
Radek Krejci09c77442021-04-26 11:10:34 +02002142 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 +01002143}
2144
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002145LIBYANG_API_DEF LY_ERR
Radek Krejci95ccd1b2021-03-12 14:57:22 +01002146lyd_new_ext_path(struct lyd_node *parent, const struct lysc_ext_instance *ext, const char *path, const void *value,
2147 uint32_t options, struct lyd_node **node)
2148{
2149 const struct ly_ctx *ctx = ext ? ext->module->ctx : NULL;
2150
Radek Krejci09c77442021-04-26 11:10:34 +02002151 LY_CHECK_ARG_RET(ctx, ext, path, (path[0] == '/') || parent,
2152 !(options & LYD_NEW_PATH_BIN_VALUE) || !(options & LYD_NEW_PATH_CANON_VALUE), LY_EINVAL);
Michal Vasko892f5bf2021-11-24 10:41:05 +01002153 LY_CHECK_CTX_EQUAL_RET(parent ? LYD_CTX(parent) : NULL, ctx, LY_EINVAL);
2154
Radek Krejci09c77442021-04-26 11:10:34 +02002155 return lyd_new_path_(parent, ctx, ext, path, value, 0, LYD_ANYDATA_STRING, options, node, NULL);
Radek Krejci95ccd1b2021-03-12 14:57:22 +01002156}
2157
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002158LY_ERR
2159lyd_new_implicit_r(struct lyd_node *parent, struct lyd_node **first, const struct lysc_node *sparent,
Michal Vaskoddd76592022-01-17 13:34:48 +01002160 const struct lys_module *mod, struct ly_set *node_when, struct ly_set *node_types, uint32_t impl_opts,
2161 struct lyd_node **diff)
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002162{
2163 LY_ERR ret;
Michal Vaskod1e53b92021-01-28 13:11:06 +01002164 const struct lysc_node *iter = NULL;
Radek Krejci2b18bf12020-11-06 11:20:20 +01002165 struct lyd_node *node = NULL;
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002166 struct lyd_value **dflts;
2167 LY_ARRAY_COUNT_TYPE u;
Michal Vasko630d9892020-12-08 17:11:08 +01002168 uint32_t getnext_opts;
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002169
2170 assert(first && (parent || sparent || mod));
2171
2172 if (!sparent && parent) {
2173 sparent = parent->schema;
2174 }
2175
Michal Vasko630d9892020-12-08 17:11:08 +01002176 getnext_opts = LYS_GETNEXT_WITHCHOICE;
2177 if (impl_opts & LYD_IMPLICIT_OUTPUT) {
2178 getnext_opts |= LYS_GETNEXT_OUTPUT;
2179 }
2180
2181 while ((iter = lys_getnext(iter, sparent, mod ? mod->compiled : NULL, getnext_opts))) {
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002182 if ((impl_opts & LYD_IMPLICIT_NO_STATE) && (iter->flags & LYS_CONFIG_R)) {
2183 continue;
Michal Vasko44b19a12020-08-07 09:21:30 +02002184 } else if ((impl_opts & LYD_IMPLICIT_NO_CONFIG) && (iter->flags & LYS_CONFIG_W)) {
2185 continue;
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002186 }
2187
2188 switch (iter->nodetype) {
2189 case LYS_CHOICE:
Michal Vaskobcf4d7d2020-11-18 18:16:49 +01002190 node = lys_getnext_data(NULL, *first, NULL, iter, NULL);
2191 if (!node && ((struct lysc_node_choice *)iter)->dflt) {
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002192 /* create default case data */
Michal Vasko14ed9cd2021-01-28 14:16:25 +01002193 LY_CHECK_RET(lyd_new_implicit_r(parent, first, &((struct lysc_node_choice *)iter)->dflt->node,
Michal Vaskoddd76592022-01-17 13:34:48 +01002194 NULL, node_when, node_types, impl_opts, diff));
Michal Vaskobcf4d7d2020-11-18 18:16:49 +01002195 } else if (node) {
2196 /* create any default data in the existing case */
2197 assert(node->schema->parent->nodetype == LYS_CASE);
Michal Vaskoddd76592022-01-17 13:34:48 +01002198 LY_CHECK_RET(lyd_new_implicit_r(parent, first, node->schema->parent, NULL, node_when, node_types,
Michal Vaskobcf4d7d2020-11-18 18:16:49 +01002199 impl_opts, diff));
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002200 }
2201 break;
2202 case LYS_CONTAINER:
2203 if (!(iter->flags & LYS_PRESENCE) && lyd_find_sibling_val(*first, iter, NULL, 0, NULL)) {
2204 /* create default NP container */
2205 LY_CHECK_RET(lyd_create_inner(iter, &node));
Michal Vaskof4d67ea2021-03-31 13:53:21 +02002206 node->flags = LYD_DEFAULT | (lysc_has_when(iter) ? LYD_WHEN_TRUE : 0);
Michal Vasko6ee6f432021-07-16 09:49:14 +02002207 lyd_insert_node(parent, first, node, 0);
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002208
Michal Vaskof4d67ea2021-03-31 13:53:21 +02002209 if (lysc_has_when(iter) && node_when) {
Michal Vaskoe16c7b72021-02-26 10:39:06 +01002210 /* remember to resolve when */
2211 LY_CHECK_RET(ly_set_add(node_when, node, 1, NULL));
2212 }
Michal Vaskoe49b6322020-11-05 17:38:36 +01002213 if (diff) {
2214 /* add into diff */
2215 LY_CHECK_RET(lyd_val_diff_add(node, LYD_DIFF_OP_CREATE, diff));
2216 }
2217
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002218 /* create any default children */
Michal Vaskoddd76592022-01-17 13:34:48 +01002219 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 +02002220 impl_opts, diff));
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002221 }
2222 break;
2223 case LYS_LEAF:
Michal Vasko69730152020-10-09 16:30:07 +02002224 if (!(impl_opts & LYD_IMPLICIT_NO_DEFAULTS) && ((struct lysc_node_leaf *)iter)->dflt &&
2225 lyd_find_sibling_val(*first, iter, NULL, 0, NULL)) {
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002226 /* create default leaf */
2227 ret = lyd_create_term2(iter, ((struct lysc_node_leaf *)iter)->dflt, &node);
2228 if (ret == LY_EINCOMPLETE) {
2229 if (node_types) {
2230 /* remember to resolve type */
Radek Krejci3d92e442020-10-12 12:48:13 +02002231 LY_CHECK_RET(ly_set_add(node_types, node, 1, NULL));
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002232 }
2233 } else if (ret) {
2234 return ret;
2235 }
Michal Vaskof4d67ea2021-03-31 13:53:21 +02002236 node->flags = LYD_DEFAULT | (lysc_has_when(iter) ? LYD_WHEN_TRUE : 0);
Michal Vasko6ee6f432021-07-16 09:49:14 +02002237 lyd_insert_node(parent, first, node, 0);
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002238
Michal Vaskof4d67ea2021-03-31 13:53:21 +02002239 if (lysc_has_when(iter) && node_when) {
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002240 /* remember to resolve when */
Radek Krejci3d92e442020-10-12 12:48:13 +02002241 LY_CHECK_RET(ly_set_add(node_when, node, 1, NULL));
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002242 }
2243 if (diff) {
2244 /* add into diff */
2245 LY_CHECK_RET(lyd_val_diff_add(node, LYD_DIFF_OP_CREATE, diff));
2246 }
2247 }
2248 break;
2249 case LYS_LEAFLIST:
Michal Vasko69730152020-10-09 16:30:07 +02002250 if (!(impl_opts & LYD_IMPLICIT_NO_DEFAULTS) && ((struct lysc_node_leaflist *)iter)->dflts &&
2251 lyd_find_sibling_val(*first, iter, NULL, 0, NULL)) {
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002252 /* create all default leaf-lists */
2253 dflts = ((struct lysc_node_leaflist *)iter)->dflts;
2254 LY_ARRAY_FOR(dflts, u) {
2255 ret = lyd_create_term2(iter, dflts[u], &node);
2256 if (ret == LY_EINCOMPLETE) {
2257 if (node_types) {
2258 /* remember to resolve type */
Radek Krejci3d92e442020-10-12 12:48:13 +02002259 LY_CHECK_RET(ly_set_add(node_types, node, 1, NULL));
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002260 }
2261 } else if (ret) {
2262 return ret;
2263 }
Michal Vaskof4d67ea2021-03-31 13:53:21 +02002264 node->flags = LYD_DEFAULT | (lysc_has_when(iter) ? LYD_WHEN_TRUE : 0);
Michal Vasko6ee6f432021-07-16 09:49:14 +02002265 lyd_insert_node(parent, first, node, 0);
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002266
Michal Vaskof4d67ea2021-03-31 13:53:21 +02002267 if (lysc_has_when(iter) && node_when) {
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002268 /* remember to resolve when */
Radek Krejci3d92e442020-10-12 12:48:13 +02002269 LY_CHECK_RET(ly_set_add(node_when, node, 1, NULL));
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002270 }
2271 if (diff) {
2272 /* add into diff */
2273 LY_CHECK_RET(lyd_val_diff_add(node, LYD_DIFF_OP_CREATE, diff));
2274 }
2275 }
2276 }
2277 break;
2278 default:
2279 /* without defaults */
2280 break;
2281 }
2282 }
2283
2284 return LY_SUCCESS;
2285}
2286
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002287LIBYANG_API_DEF LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02002288lyd_new_implicit_tree(struct lyd_node *tree, uint32_t implicit_options, struct lyd_node **diff)
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002289{
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002290 LY_ERR ret = LY_SUCCESS;
Michal Vasko3488ada2020-12-03 14:18:19 +01002291 struct lyd_node *node;
Michal Vaskoddd76592022-01-17 13:34:48 +01002292 struct ly_set node_when = {0};
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002293
2294 LY_CHECK_ARG_RET(NULL, tree, LY_EINVAL);
2295 if (diff) {
2296 *diff = NULL;
2297 }
2298
Michal Vasko56daf732020-08-10 10:57:18 +02002299 LYD_TREE_DFS_BEGIN(tree, node) {
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002300 /* skip added default nodes */
Michal Vasko69730152020-10-09 16:30:07 +02002301 if (((node->flags & (LYD_DEFAULT | LYD_NEW)) != (LYD_DEFAULT | LYD_NEW)) &&
2302 (node->schema->nodetype & LYD_NODE_INNER)) {
Michal Vaskoddd76592022-01-17 13:34:48 +01002303 LY_CHECK_GOTO(ret = lyd_new_implicit_r(node, lyd_node_child_p(node), NULL, NULL, &node_when, NULL,
2304 implicit_options, diff), cleanup);
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002305 }
2306
Michal Vasko56daf732020-08-10 10:57:18 +02002307 LYD_TREE_DFS_END(tree, node);
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002308 }
2309
Michal Vasko3488ada2020-12-03 14:18:19 +01002310 /* resolve when and remove any invalid defaults */
Michal Vaskoddd76592022-01-17 13:34:48 +01002311 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 +01002312
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002313cleanup:
Michal Vasko3488ada2020-12-03 14:18:19 +01002314 ly_set_erase(&node_when, NULL);
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002315 if (ret && diff) {
2316 lyd_free_all(*diff);
2317 *diff = NULL;
2318 }
2319 return ret;
2320}
2321
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002322LIBYANG_API_DEF LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02002323lyd_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 +02002324{
2325 const struct lys_module *mod;
2326 struct lyd_node *d = NULL;
2327 uint32_t i = 0;
2328 LY_ERR ret = LY_SUCCESS;
2329
2330 LY_CHECK_ARG_RET(ctx, tree, *tree || ctx, LY_EINVAL);
Michal Vasko892f5bf2021-11-24 10:41:05 +01002331 LY_CHECK_CTX_EQUAL_RET(*tree ? LYD_CTX(*tree) : NULL, ctx, LY_EINVAL);
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002332 if (diff) {
2333 *diff = NULL;
2334 }
2335 if (!ctx) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02002336 ctx = LYD_CTX(*tree);
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002337 }
2338
2339 /* add nodes for each module one-by-one */
2340 while ((mod = ly_ctx_get_module_iter(ctx, &i))) {
2341 if (!mod->implemented) {
2342 continue;
2343 }
2344
2345 LY_CHECK_GOTO(ret = lyd_new_implicit_module(tree, mod, implicit_options, diff ? &d : NULL), cleanup);
2346 if (d) {
2347 /* merge into one diff */
2348 lyd_insert_sibling(*diff, d, diff);
2349
2350 d = NULL;
2351 }
2352 }
2353
2354cleanup:
2355 if (ret && diff) {
2356 lyd_free_all(*diff);
2357 *diff = NULL;
2358 }
2359 return ret;
2360}
2361
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002362LIBYANG_API_DEF LY_ERR
Michal Vasko892f5bf2021-11-24 10:41:05 +01002363lyd_new_implicit_module(struct lyd_node **tree, const struct lys_module *module, uint32_t implicit_options,
2364 struct lyd_node **diff)
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002365{
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002366 LY_ERR ret = LY_SUCCESS;
Michal Vasko3488ada2020-12-03 14:18:19 +01002367 struct lyd_node *root, *d = NULL;
Michal Vaskoddd76592022-01-17 13:34:48 +01002368 struct ly_set node_when = {0};
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002369
2370 LY_CHECK_ARG_RET(NULL, tree, module, LY_EINVAL);
Michal Vasko892f5bf2021-11-24 10:41:05 +01002371 LY_CHECK_CTX_EQUAL_RET(*tree ? LYD_CTX(*tree) : NULL, module ? module->ctx : NULL, LY_EINVAL);
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002372 if (diff) {
2373 *diff = NULL;
2374 }
2375
2376 /* add all top-level defaults for this module */
Michal Vaskoddd76592022-01-17 13:34:48 +01002377 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 +01002378
2379 /* resolve when and remove any invalid defaults */
Michal Vaskoddd76592022-01-17 13:34:48 +01002380 LY_CHECK_GOTO(ret = lyd_validate_unres(tree, module, &node_when, LYXP_IGNORE_WHEN, NULL, NULL, NULL, 0, diff),
Michal Vasko976ec432021-12-06 15:42:22 +01002381 cleanup);
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002382
2383 /* process nested nodes */
2384 LY_LIST_FOR(*tree, root) {
2385 /* skip added default nodes */
2386 if ((root->flags & (LYD_DEFAULT | LYD_NEW)) != (LYD_DEFAULT | LYD_NEW)) {
2387 LY_CHECK_GOTO(ret = lyd_new_implicit_tree(root, implicit_options, diff ? &d : NULL), cleanup);
2388
2389 if (d) {
2390 /* merge into one diff */
2391 lyd_insert_sibling(*diff, d, diff);
2392
2393 d = NULL;
2394 }
2395 }
2396 }
2397
2398cleanup:
Michal Vasko3488ada2020-12-03 14:18:19 +01002399 ly_set_erase(&node_when, NULL);
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002400 if (ret && diff) {
2401 lyd_free_all(*diff);
2402 *diff = NULL;
2403 }
2404 return ret;
2405}
2406
Michal Vasko90932a92020-02-12 14:33:03 +01002407struct lyd_node *
Michal Vaskob104f112020-07-17 09:54:54 +02002408lyd_insert_get_next_anchor(const struct lyd_node *first_sibling, const struct lyd_node *new_node)
Michal Vasko90932a92020-02-12 14:33:03 +01002409{
Michal Vaskob104f112020-07-17 09:54:54 +02002410 const struct lysc_node *schema, *sparent;
Michal Vasko90932a92020-02-12 14:33:03 +01002411 struct lyd_node *match = NULL;
Radek Krejci857189e2020-09-01 13:26:36 +02002412 ly_bool found;
Michal Vasko93b16062020-12-09 18:14:18 +01002413 uint32_t getnext_opts;
Michal Vasko90932a92020-02-12 14:33:03 +01002414
Michal Vaskob104f112020-07-17 09:54:54 +02002415 assert(new_node);
2416
Michal Vasko9beceb82022-04-05 12:14:15 +02002417 if (!first_sibling || !new_node->schema || (LYD_CTX(first_sibling) != LYD_CTX(new_node))) {
Michal Vaskob104f112020-07-17 09:54:54 +02002418 /* insert at the end, no next anchor */
Michal Vasko90932a92020-02-12 14:33:03 +01002419 return NULL;
2420 }
2421
Michal Vasko93b16062020-12-09 18:14:18 +01002422 getnext_opts = 0;
Michal Vaskod1e53b92021-01-28 13:11:06 +01002423 if (new_node->schema->flags & LYS_IS_OUTPUT) {
Michal Vasko93b16062020-12-09 18:14:18 +01002424 getnext_opts = LYS_GETNEXT_OUTPUT;
2425 }
2426
Michal Vaskoa2756f02021-07-09 13:20:28 +02002427 if (first_sibling->parent && first_sibling->parent->schema && first_sibling->parent->children_ht) {
Michal Vaskob104f112020-07-17 09:54:54 +02002428 /* find the anchor using hashes */
2429 sparent = first_sibling->parent->schema;
Michal Vasko93b16062020-12-09 18:14:18 +01002430 schema = lys_getnext(new_node->schema, sparent, NULL, getnext_opts);
Michal Vaskob104f112020-07-17 09:54:54 +02002431 while (schema) {
2432 /* keep trying to find the first existing instance of the closest following schema sibling,
2433 * otherwise return NULL - inserting at the end */
2434 if (!lyd_find_sibling_schema(first_sibling, schema, &match)) {
2435 break;
2436 }
2437
Michal Vasko93b16062020-12-09 18:14:18 +01002438 schema = lys_getnext(schema, sparent, NULL, getnext_opts);
Michal Vaskob104f112020-07-17 09:54:54 +02002439 }
2440 } else {
2441 /* find the anchor without hashes */
2442 match = (struct lyd_node *)first_sibling;
Michal Vasko3a708622021-07-15 14:20:10 +02002443 sparent = lysc_data_parent(new_node->schema);
2444 if (!sparent) {
Michal Vaskob104f112020-07-17 09:54:54 +02002445 /* we are in top-level, skip all the data from preceding modules */
2446 LY_LIST_FOR(match, match) {
2447 if (!match->schema || (strcmp(lyd_owner_module(match)->name, lyd_owner_module(new_node)->name) >= 0)) {
2448 break;
2449 }
2450 }
2451 }
2452
2453 /* get the first schema sibling */
Michal Vasko93b16062020-12-09 18:14:18 +01002454 schema = lys_getnext(NULL, sparent, new_node->schema->module->compiled, getnext_opts);
Michal Vaskob104f112020-07-17 09:54:54 +02002455
2456 found = 0;
2457 LY_LIST_FOR(match, match) {
2458 if (!match->schema || (lyd_owner_module(match) != lyd_owner_module(new_node))) {
2459 /* we have found an opaque node, which must be at the end, so use it OR
2460 * modules do not match, so we must have traversed all the data from new_node module (if any),
2461 * we have found the first node of the next module, that is what we want */
2462 break;
2463 }
2464
2465 /* skip schema nodes until we find the instantiated one */
2466 while (!found) {
2467 if (new_node->schema == schema) {
2468 /* we have found the schema of the new node, continue search to find the first
2469 * data node with a different schema (after our schema) */
2470 found = 1;
2471 break;
2472 }
2473 if (match->schema == schema) {
2474 /* current node (match) is a data node still before the new node, continue search in data */
2475 break;
2476 }
Michal Vasko93b16062020-12-09 18:14:18 +01002477 schema = lys_getnext(schema, sparent, new_node->schema->module->compiled, getnext_opts);
Michal Vaskob104f112020-07-17 09:54:54 +02002478 assert(schema);
2479 }
2480
2481 if (found && (match->schema != new_node->schema)) {
2482 /* find the next node after we have found our node schema data instance */
2483 break;
2484 }
2485 }
Michal Vasko90932a92020-02-12 14:33:03 +01002486 }
2487
2488 return match;
2489}
2490
2491/**
2492 * @brief Insert node after a sibling.
2493 *
Michal Vasko9f96a052020-03-10 09:41:45 +01002494 * Handles inserting into NP containers and key-less lists.
2495 *
Michal Vasko90932a92020-02-12 14:33:03 +01002496 * @param[in] sibling Sibling to insert after.
2497 * @param[in] node Node to insert.
2498 */
2499static void
Michal Vaskof03ed032020-03-04 13:31:44 +01002500lyd_insert_after_node(struct lyd_node *sibling, struct lyd_node *node)
Michal Vasko90932a92020-02-12 14:33:03 +01002501{
Michal Vasko0249f7c2020-03-05 16:36:40 +01002502 struct lyd_node_inner *par;
2503
Michal Vasko90932a92020-02-12 14:33:03 +01002504 assert(!node->next && (node->prev == node));
2505
2506 node->next = sibling->next;
2507 node->prev = sibling;
2508 sibling->next = node;
2509 if (node->next) {
2510 /* sibling had a succeeding node */
2511 node->next->prev = node;
2512 } else {
2513 /* sibling was last, find first sibling and change its prev */
2514 if (sibling->parent) {
2515 sibling = sibling->parent->child;
2516 } else {
Michal Vaskod989ba02020-08-24 10:59:24 +02002517 for ( ; sibling->prev->next != node; sibling = sibling->prev) {}
Michal Vasko90932a92020-02-12 14:33:03 +01002518 }
2519 sibling->prev = node;
2520 }
2521 node->parent = sibling->parent;
Michal Vasko0249f7c2020-03-05 16:36:40 +01002522
Michal Vasko9f96a052020-03-10 09:41:45 +01002523 for (par = node->parent; par; par = par->parent) {
2524 if ((par->flags & LYD_DEFAULT) && !(node->flags & LYD_DEFAULT)) {
2525 /* remove default flags from NP containers */
Michal Vasko0249f7c2020-03-05 16:36:40 +01002526 par->flags &= ~LYD_DEFAULT;
2527 }
2528 }
Michal Vasko90932a92020-02-12 14:33:03 +01002529}
2530
2531/**
2532 * @brief Insert node before a sibling.
2533 *
Michal Vasko9f96a052020-03-10 09:41:45 +01002534 * Handles inserting into NP containers and key-less lists.
2535 *
Michal Vasko90932a92020-02-12 14:33:03 +01002536 * @param[in] sibling Sibling to insert before.
2537 * @param[in] node Node to insert.
2538 */
2539static void
Michal Vaskof03ed032020-03-04 13:31:44 +01002540lyd_insert_before_node(struct lyd_node *sibling, struct lyd_node *node)
Michal Vasko90932a92020-02-12 14:33:03 +01002541{
Michal Vasko0249f7c2020-03-05 16:36:40 +01002542 struct lyd_node_inner *par;
2543
Michal Vasko90932a92020-02-12 14:33:03 +01002544 assert(!node->next && (node->prev == node));
2545
2546 node->next = sibling;
2547 /* covers situation of sibling being first */
2548 node->prev = sibling->prev;
2549 sibling->prev = node;
2550 if (node->prev->next) {
2551 /* sibling had a preceding node */
2552 node->prev->next = node;
2553 } else if (sibling->parent) {
2554 /* sibling was first and we must also change parent child pointer */
2555 sibling->parent->child = node;
2556 }
2557 node->parent = sibling->parent;
Michal Vasko0249f7c2020-03-05 16:36:40 +01002558
Michal Vasko9f96a052020-03-10 09:41:45 +01002559 for (par = node->parent; par; par = par->parent) {
2560 if ((par->flags & LYD_DEFAULT) && !(node->flags & LYD_DEFAULT)) {
2561 /* remove default flags from NP containers */
Michal Vasko0249f7c2020-03-05 16:36:40 +01002562 par->flags &= ~LYD_DEFAULT;
2563 }
2564 }
Michal Vasko90932a92020-02-12 14:33:03 +01002565}
2566
2567/**
Michal Vaskob104f112020-07-17 09:54:54 +02002568 * @brief Insert node as the first and only child of a parent.
Michal Vasko90932a92020-02-12 14:33:03 +01002569 *
Michal Vasko9f96a052020-03-10 09:41:45 +01002570 * Handles inserting into NP containers and key-less lists.
2571 *
Michal Vasko90932a92020-02-12 14:33:03 +01002572 * @param[in] parent Parent to insert into.
2573 * @param[in] node Node to insert.
2574 */
2575static void
Michal Vaskob104f112020-07-17 09:54:54 +02002576lyd_insert_only_child(struct lyd_node *parent, struct lyd_node *node)
Michal Vasko90932a92020-02-12 14:33:03 +01002577{
2578 struct lyd_node_inner *par;
2579
Radek Krejcia1c1e542020-09-29 16:06:52 +02002580 assert(parent && !lyd_child(parent) && !node->next && (node->prev == node));
Michal Vasko52927e22020-03-16 17:26:14 +01002581 assert(!parent->schema || (parent->schema->nodetype & LYD_NODE_INNER));
Michal Vasko90932a92020-02-12 14:33:03 +01002582
2583 par = (struct lyd_node_inner *)parent;
2584
Michal Vaskob104f112020-07-17 09:54:54 +02002585 par->child = node;
Michal Vasko90932a92020-02-12 14:33:03 +01002586 node->parent = par;
Michal Vasko0249f7c2020-03-05 16:36:40 +01002587
Michal Vaskod989ba02020-08-24 10:59:24 +02002588 for ( ; par; par = par->parent) {
Michal Vasko9f96a052020-03-10 09:41:45 +01002589 if ((par->flags & LYD_DEFAULT) && !(node->flags & LYD_DEFAULT)) {
2590 /* remove default flags from NP containers */
Michal Vasko0249f7c2020-03-05 16:36:40 +01002591 par->flags &= ~LYD_DEFAULT;
2592 }
2593 }
Michal Vasko751cb4d2020-07-14 12:25:28 +02002594}
Michal Vasko0249f7c2020-03-05 16:36:40 +01002595
Michal Vasko751cb4d2020-07-14 12:25:28 +02002596/**
2597 * @brief Learn whether a list instance has all the keys.
2598 *
2599 * @param[in] list List instance to check.
2600 * @return non-zero if all the keys were found,
2601 * @return 0 otherwise.
2602 */
2603static int
2604lyd_insert_has_keys(const struct lyd_node *list)
2605{
2606 const struct lyd_node *key;
2607 const struct lysc_node *skey = NULL;
2608
2609 assert(list->schema->nodetype == LYS_LIST);
Radek Krejcia1c1e542020-09-29 16:06:52 +02002610 key = lyd_child(list);
Michal Vasko751cb4d2020-07-14 12:25:28 +02002611 while ((skey = lys_getnext(skey, list->schema, NULL, 0)) && (skey->flags & LYS_KEY)) {
2612 if (!key || (key->schema != skey)) {
2613 /* key missing */
2614 return 0;
2615 }
2616
2617 key = key->next;
2618 }
2619
2620 /* all keys found */
2621 return 1;
Michal Vasko90932a92020-02-12 14:33:03 +01002622}
2623
2624void
Michal Vasko6ee6f432021-07-16 09:49:14 +02002625lyd_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 +01002626{
Michal Vaskob104f112020-07-17 09:54:54 +02002627 struct lyd_node *anchor, *first_sibling;
Michal Vasko90932a92020-02-12 14:33:03 +01002628
Michal Vaskob104f112020-07-17 09:54:54 +02002629 /* inserting list without its keys is not supported */
2630 assert((parent || first_sibling_p) && node && (node->hash || !node->schema));
Michal Vaskof756c942020-11-06 17:21:37 +01002631 assert(!parent || !parent->schema ||
2632 (parent->schema->nodetype & (LYS_CONTAINER | LYS_LIST | LYS_RPC | LYS_ACTION | LYS_NOTIF)));
Michal Vasko9b368d32020-02-14 13:53:31 +01002633
Michal Vaskob104f112020-07-17 09:54:54 +02002634 if (!parent && first_sibling_p && (*first_sibling_p) && (*first_sibling_p)->parent) {
Michal Vasko9e685082021-01-29 14:49:09 +01002635 parent = lyd_parent(*first_sibling_p);
Michal Vasko9b368d32020-02-14 13:53:31 +01002636 }
Michal Vasko90932a92020-02-12 14:33:03 +01002637
Michal Vaskob104f112020-07-17 09:54:54 +02002638 /* get first sibling */
Michal Vasko9e685082021-01-29 14:49:09 +01002639 first_sibling = parent ? lyd_child(parent) : *first_sibling_p;
Michal Vasko9f96a052020-03-10 09:41:45 +01002640
Michal Vasko9beceb82022-04-05 12:14:15 +02002641 if (last || (first_sibling && (first_sibling->flags & LYD_EXT))) {
Michal Vasko6ee6f432021-07-16 09:49:14 +02002642 /* no next anchor */
2643 anchor = NULL;
2644 } else {
2645 /* find the anchor, our next node, so we can insert before it */
2646 anchor = lyd_insert_get_next_anchor(first_sibling, node);
2647 }
2648
Michal Vaskob104f112020-07-17 09:54:54 +02002649 if (anchor) {
Michal Vasko6ee6f432021-07-16 09:49:14 +02002650 /* insert before the anchor */
Michal Vaskob104f112020-07-17 09:54:54 +02002651 lyd_insert_before_node(anchor, node);
Michal Vasko26123192020-11-09 21:02:34 +01002652 if (!parent && (*first_sibling_p == anchor)) {
2653 /* move first sibling */
2654 *first_sibling_p = node;
2655 }
Michal Vaskob104f112020-07-17 09:54:54 +02002656 } else if (first_sibling) {
Michal Vasko6ee6f432021-07-16 09:49:14 +02002657 /* insert as the last node */
Michal Vaskob104f112020-07-17 09:54:54 +02002658 lyd_insert_after_node(first_sibling->prev, node);
2659 } else if (parent) {
Michal Vasko6ee6f432021-07-16 09:49:14 +02002660 /* insert as the only child */
Michal Vaskob104f112020-07-17 09:54:54 +02002661 lyd_insert_only_child(parent, node);
Michal Vasko90932a92020-02-12 14:33:03 +01002662 } else {
Michal Vasko6ee6f432021-07-16 09:49:14 +02002663 /* insert as the only sibling */
Michal Vaskob104f112020-07-17 09:54:54 +02002664 *first_sibling_p = node;
2665 }
2666
2667 /* insert into parent HT */
2668 lyd_insert_hash(node);
2669
2670 /* finish hashes for our parent, if needed and possible */
Michal Vasko9e8de2d2020-09-01 08:17:10 +02002671 if (node->schema && (node->schema->flags & LYS_KEY) && parent && lyd_insert_has_keys(parent)) {
Michal Vaskob104f112020-07-17 09:54:54 +02002672 lyd_hash(parent);
2673
2674 /* now we can insert even the list into its parent HT */
2675 lyd_insert_hash(parent);
Michal Vasko90932a92020-02-12 14:33:03 +01002676 }
Michal Vasko90932a92020-02-12 14:33:03 +01002677}
2678
Michal Vasko717a4c32020-12-07 09:40:10 +01002679/**
2680 * @brief Check schema place of a node to be inserted.
2681 *
2682 * @param[in] parent Schema node of the parent data node.
2683 * @param[in] sibling Schema node of a sibling data node.
2684 * @param[in] schema Schema node if the data node to be inserted.
2685 * @return LY_SUCCESS on success.
2686 * @return LY_EINVAL if the place is invalid.
2687 */
Michal Vaskof03ed032020-03-04 13:31:44 +01002688static LY_ERR
Michal Vasko717a4c32020-12-07 09:40:10 +01002689lyd_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 +01002690{
2691 const struct lysc_node *par2;
2692
Michal Vasko62ed12d2020-05-21 10:08:25 +02002693 assert(!parent || !(parent->nodetype & (LYS_CASE | LYS_CHOICE)));
Michal Vasko717a4c32020-12-07 09:40:10 +01002694 assert(!sibling || !(sibling->nodetype & (LYS_CASE | LYS_CHOICE)));
2695 assert(!schema || !(schema->nodetype & (LYS_CASE | LYS_CHOICE)));
Michal Vaskof03ed032020-03-04 13:31:44 +01002696
Michal Vasko717a4c32020-12-07 09:40:10 +01002697 if (!schema || (!parent && !sibling)) {
Michal Vasko71e795e2020-12-04 16:27:37 +01002698 /* opaque nodes can be inserted wherever */
2699 return LY_SUCCESS;
2700 }
2701
Michal Vasko717a4c32020-12-07 09:40:10 +01002702 if (!parent) {
2703 parent = lysc_data_parent(sibling);
2704 }
2705
Michal Vaskof03ed032020-03-04 13:31:44 +01002706 /* find schema parent */
Michal Vasko62ed12d2020-05-21 10:08:25 +02002707 par2 = lysc_data_parent(schema);
Michal Vaskof03ed032020-03-04 13:31:44 +01002708
2709 if (parent) {
2710 /* inner node */
2711 if (par2 != parent) {
Michal Vaskob104f112020-07-17 09:54:54 +02002712 LOGERR(schema->module->ctx, LY_EINVAL, "Cannot insert, parent of \"%s\" is not \"%s\".", schema->name,
Michal Vasko69730152020-10-09 16:30:07 +02002713 parent->name);
Michal Vaskof03ed032020-03-04 13:31:44 +01002714 return LY_EINVAL;
2715 }
2716 } else {
2717 /* top-level node */
2718 if (par2) {
Radek Krejcif6d14cb2020-07-02 16:11:45 +02002719 LOGERR(schema->module->ctx, LY_EINVAL, "Cannot insert, node \"%s\" is not top-level.", schema->name);
Michal Vaskof03ed032020-03-04 13:31:44 +01002720 return LY_EINVAL;
2721 }
2722 }
2723
2724 return LY_SUCCESS;
2725}
2726
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002727LIBYANG_API_DEF LY_ERR
Michal Vaskob104f112020-07-17 09:54:54 +02002728lyd_insert_child(struct lyd_node *parent, struct lyd_node *node)
Michal Vaskof03ed032020-03-04 13:31:44 +01002729{
2730 struct lyd_node *iter;
2731
Michal Vasko0ab974d2021-02-24 13:18:26 +01002732 LY_CHECK_ARG_RET(NULL, parent, node, !parent->schema || (parent->schema->nodetype & LYD_NODE_INNER), LY_EINVAL);
Michal Vasko892f5bf2021-11-24 10:41:05 +01002733 LY_CHECK_CTX_EQUAL_RET(LYD_CTX(parent), LYD_CTX(node), LY_EINVAL);
Michal Vaskof03ed032020-03-04 13:31:44 +01002734
Michal Vasko717a4c32020-12-07 09:40:10 +01002735 LY_CHECK_RET(lyd_insert_check_schema(parent->schema, NULL, node->schema));
Michal Vaskof03ed032020-03-04 13:31:44 +01002736
Michal Vasko0ab974d2021-02-24 13:18:26 +01002737 if (node->schema && (node->schema->flags & LYS_KEY)) {
Michal Vaskoddd76592022-01-17 13:34:48 +01002738 LOGERR(LYD_CTX(parent), LY_EINVAL, "Cannot insert key \"%s\".", node->schema->name);
Michal Vaskof03ed032020-03-04 13:31:44 +01002739 return LY_EINVAL;
2740 }
2741
2742 if (node->parent || node->prev->next) {
2743 lyd_unlink_tree(node);
2744 }
2745
2746 while (node) {
2747 iter = node->next;
2748 lyd_unlink_tree(node);
Michal Vasko6ee6f432021-07-16 09:49:14 +02002749 lyd_insert_node(parent, NULL, node, 0);
Michal Vaskof03ed032020-03-04 13:31:44 +01002750 node = iter;
2751 }
2752 return LY_SUCCESS;
2753}
2754
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002755LIBYANG_API_DEF LY_ERR
Michal Vaskoddd76592022-01-17 13:34:48 +01002756lyd_insert_ext(struct lyd_node *parent, struct lyd_node *first)
2757{
2758 struct lyd_node *iter;
2759
2760 LY_CHECK_ARG_RET(NULL, parent, first, !first->parent, !first->prev->next,
2761 !parent->schema || (parent->schema->nodetype & LYD_NODE_INNER), LY_EINVAL);
2762
2763 if (first->schema && (first->schema->flags & LYS_KEY)) {
2764 LOGERR(LYD_CTX(parent), LY_EINVAL, "Cannot insert key \"%s\".", first->schema->name);
2765 return LY_EINVAL;
2766 }
2767
2768 while (first) {
2769 iter = first->next;
2770 lyd_unlink_tree(first);
Michal Vasko61ad1ff2022-02-10 15:48:39 +01002771 lyd_insert_node(parent, NULL, first, 1);
Michal Vaskoddd76592022-01-17 13:34:48 +01002772 first = iter;
2773 }
2774 return LY_SUCCESS;
2775}
2776
2777LIBYANG_API_DEF LY_ERR
Michal Vaskob104f112020-07-17 09:54:54 +02002778lyd_insert_sibling(struct lyd_node *sibling, struct lyd_node *node, struct lyd_node **first)
Michal Vaskob1b5c262020-03-05 14:29:47 +01002779{
2780 struct lyd_node *iter;
2781
Michal Vaskob104f112020-07-17 09:54:54 +02002782 LY_CHECK_ARG_RET(NULL, node, LY_EINVAL);
Michal Vasko892f5bf2021-11-24 10:41:05 +01002783 LY_CHECK_CTX_EQUAL_RET(sibling ? LYD_CTX(sibling) : NULL, LYD_CTX(node), LY_EINVAL);
Michal Vaskob1b5c262020-03-05 14:29:47 +01002784
Michal Vaskob104f112020-07-17 09:54:54 +02002785 if (sibling) {
Michal Vasko717a4c32020-12-07 09:40:10 +01002786 LY_CHECK_RET(lyd_insert_check_schema(NULL, sibling->schema, node->schema));
Michal Vaskob1b5c262020-03-05 14:29:47 +01002787 }
2788
Michal Vasko553d0b52020-12-04 16:27:52 +01002789 if (sibling == node) {
2790 /* we need to keep the connection to siblings so we can insert into them */
2791 sibling = sibling->prev;
2792 }
2793
Michal Vaskob1b5c262020-03-05 14:29:47 +01002794 if (node->parent || node->prev->next) {
2795 lyd_unlink_tree(node);
2796 }
2797
2798 while (node) {
Michal Vasko71e795e2020-12-04 16:27:37 +01002799 if (lysc_is_key(node->schema)) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02002800 LOGERR(LYD_CTX(node), LY_EINVAL, "Cannot insert key \"%s\".", node->schema->name);
Michal Vaskob104f112020-07-17 09:54:54 +02002801 return LY_EINVAL;
2802 }
2803
Michal Vaskob1b5c262020-03-05 14:29:47 +01002804 iter = node->next;
2805 lyd_unlink_tree(node);
Michal Vasko6ee6f432021-07-16 09:49:14 +02002806 lyd_insert_node(NULL, &sibling, node, 0);
Michal Vaskob1b5c262020-03-05 14:29:47 +01002807 node = iter;
2808 }
Michal Vaskob1b5c262020-03-05 14:29:47 +01002809
Michal Vaskob104f112020-07-17 09:54:54 +02002810 if (first) {
2811 /* find the first sibling */
2812 *first = sibling;
2813 while ((*first)->prev->next) {
2814 *first = (*first)->prev;
Michal Vasko0249f7c2020-03-05 16:36:40 +01002815 }
2816 }
2817
2818 return LY_SUCCESS;
2819}
2820
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002821LIBYANG_API_DEF LY_ERR
Michal Vaskof03ed032020-03-04 13:31:44 +01002822lyd_insert_before(struct lyd_node *sibling, struct lyd_node *node)
2823{
Michal Vaskof03ed032020-03-04 13:31:44 +01002824 LY_CHECK_ARG_RET(NULL, sibling, node, LY_EINVAL);
Michal Vasko892f5bf2021-11-24 10:41:05 +01002825 LY_CHECK_CTX_EQUAL_RET(LYD_CTX(sibling), LYD_CTX(node), LY_EINVAL);
Michal Vaskof03ed032020-03-04 13:31:44 +01002826
Michal Vasko717a4c32020-12-07 09:40:10 +01002827 LY_CHECK_RET(lyd_insert_check_schema(NULL, sibling->schema, node->schema));
Michal Vaskof03ed032020-03-04 13:31:44 +01002828
Michal Vaskoab7a2bf2022-04-01 14:37:54 +02002829 if (node->schema && (!(node->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) || !(node->schema->flags & LYS_ORDBY_USER))) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02002830 LOGERR(LYD_CTX(sibling), LY_EINVAL, "Can be used only for user-ordered nodes.");
Michal Vaskof03ed032020-03-04 13:31:44 +01002831 return LY_EINVAL;
2832 }
Michal Vasko7508ef22022-02-22 14:13:10 +01002833 if (lysc_is_key(sibling->schema)) {
2834 LOGERR(LYD_CTX(sibling), LY_EINVAL, "Cannot insert before keys.");
2835 return LY_EINVAL;
2836 }
Michal Vaskof03ed032020-03-04 13:31:44 +01002837
Michal Vasko4bc2ce32020-12-08 10:06:16 +01002838 lyd_unlink_tree(node);
2839 lyd_insert_before_node(sibling, node);
2840 lyd_insert_hash(node);
Michal Vaskof03ed032020-03-04 13:31:44 +01002841
Michal Vaskof03ed032020-03-04 13:31:44 +01002842 return LY_SUCCESS;
2843}
2844
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002845LIBYANG_API_DEF LY_ERR
Michal Vaskof03ed032020-03-04 13:31:44 +01002846lyd_insert_after(struct lyd_node *sibling, struct lyd_node *node)
2847{
Michal Vaskof03ed032020-03-04 13:31:44 +01002848 LY_CHECK_ARG_RET(NULL, sibling, node, LY_EINVAL);
Michal Vasko892f5bf2021-11-24 10:41:05 +01002849 LY_CHECK_CTX_EQUAL_RET(LYD_CTX(sibling), LYD_CTX(node), LY_EINVAL);
Michal Vaskof03ed032020-03-04 13:31:44 +01002850
Michal Vasko717a4c32020-12-07 09:40:10 +01002851 LY_CHECK_RET(lyd_insert_check_schema(NULL, sibling->schema, node->schema));
Michal Vaskof03ed032020-03-04 13:31:44 +01002852
Michal Vaskoab7a2bf2022-04-01 14:37:54 +02002853 if (node->schema && (!(node->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) || !(node->schema->flags & LYS_ORDBY_USER))) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02002854 LOGERR(LYD_CTX(sibling), LY_EINVAL, "Can be used only for user-ordered nodes.");
Michal Vaskof03ed032020-03-04 13:31:44 +01002855 return LY_EINVAL;
2856 }
Michal Vasko7508ef22022-02-22 14:13:10 +01002857 if (sibling->next && lysc_is_key(sibling->next->schema)) {
2858 LOGERR(LYD_CTX(sibling), LY_EINVAL, "Cannot insert before keys.");
2859 return LY_EINVAL;
2860 }
Michal Vaskof03ed032020-03-04 13:31:44 +01002861
Michal Vasko4bc2ce32020-12-08 10:06:16 +01002862 lyd_unlink_tree(node);
2863 lyd_insert_after_node(sibling, node);
2864 lyd_insert_hash(node);
Michal Vaskof03ed032020-03-04 13:31:44 +01002865
Michal Vaskof03ed032020-03-04 13:31:44 +01002866 return LY_SUCCESS;
2867}
2868
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002869LIBYANG_API_DEF void
Michal Vasko66d508c2021-07-21 16:07:09 +02002870lyd_unlink_siblings(struct lyd_node *node)
2871{
2872 struct lyd_node *next, *elem, *first = NULL;
2873
2874 LY_LIST_FOR_SAFE(node, next, elem) {
2875 lyd_unlink_tree(elem);
2876 lyd_insert_node(NULL, &first, elem, 1);
2877 }
2878}
2879
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002880LIBYANG_API_DEF void
Michal Vaskof03ed032020-03-04 13:31:44 +01002881lyd_unlink_tree(struct lyd_node *node)
2882{
2883 struct lyd_node *iter;
2884
2885 if (!node) {
2886 return;
2887 }
2888
Michal Vaskob104f112020-07-17 09:54:54 +02002889 /* update hashes while still linked into the tree */
2890 lyd_unlink_hash(node);
2891
Michal Vaskof03ed032020-03-04 13:31:44 +01002892 /* unlink from siblings */
2893 if (node->prev->next) {
2894 node->prev->next = node->next;
2895 }
2896 if (node->next) {
2897 node->next->prev = node->prev;
2898 } else {
2899 /* unlinking the last node */
2900 if (node->parent) {
2901 iter = node->parent->child;
2902 } else {
2903 iter = node->prev;
2904 while (iter->prev != node) {
2905 iter = iter->prev;
2906 }
2907 }
2908 /* update the "last" pointer from the first node */
2909 iter->prev = node->prev;
2910 }
2911
2912 /* unlink from parent */
2913 if (node->parent) {
2914 if (node->parent->child == node) {
2915 /* the node is the first child */
2916 node->parent->child = node->next;
2917 }
2918
Michal Vaskoab49dbe2020-07-17 12:32:47 +02002919 /* check for NP container whether its last non-default node is not being unlinked */
Michal Vasko69730152020-10-09 16:30:07 +02002920 if (node->parent->schema && (node->parent->schema->nodetype == LYS_CONTAINER) &&
2921 !(node->parent->flags & LYD_DEFAULT) && !(node->parent->schema->flags & LYS_PRESENCE)) {
Michal Vaskoab49dbe2020-07-17 12:32:47 +02002922 LY_LIST_FOR(node->parent->child, iter) {
2923 if ((iter != node) && !(iter->flags & LYD_DEFAULT)) {
2924 break;
2925 }
2926 }
2927 if (!iter) {
2928 node->parent->flags |= LYD_DEFAULT;
2929 }
2930 }
2931
Michal Vaskof03ed032020-03-04 13:31:44 +01002932 node->parent = NULL;
2933 }
2934
2935 node->next = NULL;
2936 node->prev = node;
2937}
2938
Michal Vaskoa5da3292020-08-12 13:10:50 +02002939void
Michal Vasko871a0252020-11-11 18:35:24 +01002940lyd_insert_meta(struct lyd_node *parent, struct lyd_meta *meta, ly_bool clear_dflt)
Radek Krejci1798aae2020-07-14 13:26:06 +02002941{
2942 struct lyd_meta *last, *iter;
2943
2944 assert(parent);
Michal Vaskoa5da3292020-08-12 13:10:50 +02002945
2946 if (!meta) {
2947 return;
2948 }
Radek Krejci1798aae2020-07-14 13:26:06 +02002949
2950 for (iter = meta; iter; iter = iter->next) {
2951 iter->parent = parent;
2952 }
2953
2954 /* insert as the last attribute */
2955 if (parent->meta) {
Radek Krejci1e008d22020-08-17 11:37:37 +02002956 for (last = parent->meta; last->next; last = last->next) {}
Radek Krejci1798aae2020-07-14 13:26:06 +02002957 last->next = meta;
2958 } else {
2959 parent->meta = meta;
2960 }
2961
2962 /* remove default flags from NP containers */
Michal Vasko871a0252020-11-11 18:35:24 +01002963 while (clear_dflt && parent && (parent->schema->nodetype == LYS_CONTAINER) && (parent->flags & LYD_DEFAULT)) {
Radek Krejci1798aae2020-07-14 13:26:06 +02002964 parent->flags &= ~LYD_DEFAULT;
Michal Vasko9e685082021-01-29 14:49:09 +01002965 parent = lyd_parent(parent);
Radek Krejci1798aae2020-07-14 13:26:06 +02002966 }
Radek Krejci1798aae2020-07-14 13:26:06 +02002967}
2968
2969LY_ERR
Michal Vasko9f96a052020-03-10 09:41:45 +01002970lyd_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 +02002971 size_t name_len, const char *value, size_t value_len, ly_bool *dynamic, LY_VALUE_FORMAT format,
Michal Vaskoddd76592022-01-17 13:34:48 +01002972 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 +01002973{
Radek Krejci2efc45b2020-12-22 16:25:44 +01002974 LY_ERR ret = LY_SUCCESS;
Michal Vasko90932a92020-02-12 14:33:03 +01002975 struct lysc_ext_instance *ant = NULL;
Michal Vaskob0534f92021-07-01 13:24:03 +02002976 const struct lysc_type **ant_type;
Michal Vasko9f96a052020-03-10 09:41:45 +01002977 struct lyd_meta *mt, *last;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002978 LY_ARRAY_COUNT_TYPE u;
Michal Vasko90932a92020-02-12 14:33:03 +01002979
Michal Vasko9f96a052020-03-10 09:41:45 +01002980 assert((parent || meta) && mod);
Michal Vasko6f4cbb62020-02-28 11:15:47 +01002981
Michal Vaskoddd76592022-01-17 13:34:48 +01002982 LOG_LOCSET(ctx_node, parent, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +01002983
Radek Krejci7eb54ba2020-05-18 16:30:04 +02002984 LY_ARRAY_FOR(mod->compiled->exts, u) {
Radek Krejci3e6632f2021-03-22 22:08:21 +01002985 if ((mod->compiled->exts[u].def->plugin == lyplg_find(LYPLG_EXTENSION, LYEXT_PLUGIN_INTERNAL_ANNOTATION)) &&
Radek Krejci7eb54ba2020-05-18 16:30:04 +02002986 !ly_strncmp(mod->compiled->exts[u].argument, name, name_len)) {
Michal Vasko90932a92020-02-12 14:33:03 +01002987 /* we have the annotation definition */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02002988 ant = &mod->compiled->exts[u];
Michal Vasko90932a92020-02-12 14:33:03 +01002989 break;
2990 }
2991 }
2992 if (!ant) {
2993 /* attribute is not defined as a metadata annotation (RFC 7952) */
Radek Krejci2efc45b2020-12-22 16:25:44 +01002994 LOGVAL(mod->ctx, LYVE_REFERENCE, "Annotation definition for attribute \"%s:%.*s\" not found.",
Radek Krejci422afb12021-03-04 16:38:16 +01002995 mod->name, (int)name_len, name);
Radek Krejci2efc45b2020-12-22 16:25:44 +01002996 ret = LY_EINVAL;
2997 goto cleanup;
Michal Vasko90932a92020-02-12 14:33:03 +01002998 }
2999
Michal Vasko9f96a052020-03-10 09:41:45 +01003000 mt = calloc(1, sizeof *mt);
Radek Krejci2efc45b2020-12-22 16:25:44 +01003001 LY_CHECK_ERR_GOTO(!mt, LOGMEM(mod->ctx); ret = LY_EMEM, cleanup);
Michal Vasko9f96a052020-03-10 09:41:45 +01003002 mt->parent = parent;
3003 mt->annotation = ant;
Michal Vaskob0534f92021-07-01 13:24:03 +02003004 ant_type = ant->substmts[ANNOTATION_SUBSTMT_TYPE].storage;
3005 ret = lyd_value_store(mod->ctx, &mt->value, *ant_type, value, value_len, dynamic, format, prefix_data, hints,
Michal Vaskoddd76592022-01-17 13:34:48 +01003006 ctx_node, incomplete);
Radek Krejci2efc45b2020-12-22 16:25:44 +01003007 LY_CHECK_ERR_GOTO(ret, free(mt), cleanup);
3008 ret = lydict_insert(mod->ctx, name, name_len, &mt->name);
3009 LY_CHECK_ERR_GOTO(ret, free(mt), cleanup);
Michal Vasko90932a92020-02-12 14:33:03 +01003010
Michal Vasko6f4cbb62020-02-28 11:15:47 +01003011 /* insert as the last attribute */
3012 if (parent) {
Michal Vasko871a0252020-11-11 18:35:24 +01003013 lyd_insert_meta(parent, mt, clear_dflt);
Michal Vasko9f96a052020-03-10 09:41:45 +01003014 } else if (*meta) {
Radek Krejci1e008d22020-08-17 11:37:37 +02003015 for (last = *meta; last->next; last = last->next) {}
Michal Vasko9f96a052020-03-10 09:41:45 +01003016 last->next = mt;
Michal Vasko90932a92020-02-12 14:33:03 +01003017 }
3018
Michal Vasko9f96a052020-03-10 09:41:45 +01003019 if (meta) {
3020 *meta = mt;
Michal Vasko90932a92020-02-12 14:33:03 +01003021 }
Radek Krejci2efc45b2020-12-22 16:25:44 +01003022
3023cleanup:
Michal Vaskoddd76592022-01-17 13:34:48 +01003024 LOG_LOCBACK(ctx_node ? 1 : 0, parent ? 1 : 0, 0, 0);
Radek Krejci2efc45b2020-12-22 16:25:44 +01003025 return ret;
Michal Vasko90932a92020-02-12 14:33:03 +01003026}
3027
Michal Vaskoa5da3292020-08-12 13:10:50 +02003028void
3029lyd_insert_attr(struct lyd_node *parent, struct lyd_attr *attr)
3030{
3031 struct lyd_attr *last, *iter;
3032 struct lyd_node_opaq *opaq;
3033
3034 assert(parent && !parent->schema);
3035
3036 if (!attr) {
3037 return;
3038 }
3039
3040 opaq = (struct lyd_node_opaq *)parent;
3041 for (iter = attr; iter; iter = iter->next) {
3042 iter->parent = opaq;
3043 }
3044
3045 /* insert as the last attribute */
3046 if (opaq->attr) {
Radek Krejci1e008d22020-08-17 11:37:37 +02003047 for (last = opaq->attr; last->next; last = last->next) {}
Michal Vaskoa5da3292020-08-12 13:10:50 +02003048 last->next = attr;
3049 } else {
3050 opaq->attr = attr;
3051 }
3052}
3053
Michal Vasko52927e22020-03-16 17:26:14 +01003054LY_ERR
Michal Vaskofeca4fb2020-10-05 08:58:40 +02003055lyd_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 +01003056 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 +02003057 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 +01003058{
Radek Krejci011e4aa2020-09-04 15:22:31 +02003059 LY_ERR ret = LY_SUCCESS;
Radek Krejci1798aae2020-07-14 13:26:06 +02003060 struct lyd_attr *at, *last;
Michal Vasko52927e22020-03-16 17:26:14 +01003061
3062 assert(ctx && (parent || attr) && (!parent || !parent->schema));
Michal Vaskofeca4fb2020-10-05 08:58:40 +02003063 assert(name && name_len && format);
Michal Vasko52927e22020-03-16 17:26:14 +01003064
Michal Vasko2a3722d2021-06-16 11:52:39 +02003065 if (!value_len && (!dynamic || !*dynamic)) {
Michal Vasko52927e22020-03-16 17:26:14 +01003066 value = "";
3067 }
3068
3069 at = calloc(1, sizeof *at);
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01003070 LY_CHECK_ERR_RET(!at, LOGMEM(ctx); ly_free_prefix_data(format, val_prefix_data), LY_EMEM);
Radek Krejcid46e46a2020-09-15 14:22:42 +02003071
Michal Vaskoad92b672020-11-12 13:11:31 +01003072 LY_CHECK_GOTO(ret = lydict_insert(ctx, name, name_len, &at->name.name), finish);
Michal Vasko501af032020-11-11 20:27:44 +01003073 if (prefix_len) {
Michal Vaskoad92b672020-11-12 13:11:31 +01003074 LY_CHECK_GOTO(ret = lydict_insert(ctx, prefix, prefix_len, &at->name.prefix), finish);
Michal Vasko501af032020-11-11 20:27:44 +01003075 }
3076 if (module_key_len) {
Michal Vaskoad92b672020-11-12 13:11:31 +01003077 LY_CHECK_GOTO(ret = lydict_insert(ctx, module_key, module_key_len, &at->name.module_ns), finish);
Michal Vasko501af032020-11-11 20:27:44 +01003078 }
3079
Michal Vasko52927e22020-03-16 17:26:14 +01003080 if (dynamic && *dynamic) {
Radek Krejci011e4aa2020-09-04 15:22:31 +02003081 ret = lydict_insert_zc(ctx, (char *)value, &at->value);
3082 LY_CHECK_GOTO(ret, finish);
Michal Vasko52927e22020-03-16 17:26:14 +01003083 *dynamic = 0;
3084 } else {
Radek Krejci011e4aa2020-09-04 15:22:31 +02003085 LY_CHECK_GOTO(ret = lydict_insert(ctx, value, value_len, &at->value), finish);
Michal Vasko52927e22020-03-16 17:26:14 +01003086 }
Michal Vasko501af032020-11-11 20:27:44 +01003087 at->format = format;
3088 at->val_prefix_data = val_prefix_data;
3089 at->hints = hints;
Michal Vasko52927e22020-03-16 17:26:14 +01003090
3091 /* insert as the last attribute */
3092 if (parent) {
Michal Vaskoa5da3292020-08-12 13:10:50 +02003093 lyd_insert_attr(parent, at);
Michal Vasko52927e22020-03-16 17:26:14 +01003094 } else if (*attr) {
Radek Krejci1e008d22020-08-17 11:37:37 +02003095 for (last = *attr; last->next; last = last->next) {}
Michal Vasko52927e22020-03-16 17:26:14 +01003096 last->next = at;
3097 }
3098
Radek Krejci011e4aa2020-09-04 15:22:31 +02003099finish:
3100 if (ret) {
3101 lyd_free_attr_single(ctx, at);
3102 } else if (attr) {
Michal Vasko52927e22020-03-16 17:26:14 +01003103 *attr = at;
3104 }
3105 return LY_SUCCESS;
3106}
3107
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01003108LIBYANG_API_DEF const struct lyd_node_term *
Michal Vasko004d3152020-06-11 19:59:22 +02003109lyd_target(const struct ly_path *path, const struct lyd_node *tree)
Radek Krejci084289f2019-07-09 17:35:30 +02003110{
Michal Vasko004d3152020-06-11 19:59:22 +02003111 struct lyd_node *target;
Radek Krejci084289f2019-07-09 17:35:30 +02003112
Michal Vasko004d3152020-06-11 19:59:22 +02003113 if (ly_path_eval(path, tree, &target)) {
3114 return NULL;
Radek Krejci084289f2019-07-09 17:35:30 +02003115 }
3116
Michal Vasko004d3152020-06-11 19:59:22 +02003117 return (struct lyd_node_term *)target;
Radek Krejci084289f2019-07-09 17:35:30 +02003118}
3119
aPiecek2f63f952021-03-30 12:22:18 +02003120/**
3121 * @brief Check the equality of the two schemas from different contexts.
3122 *
3123 * @param schema1 of first node.
3124 * @param schema2 of second node.
3125 * @return 1 if the schemas are equal otherwise 0.
3126 */
3127static ly_bool
3128lyd_compare_schema_equal(const struct lysc_node *schema1, const struct lysc_node *schema2)
3129{
3130 if (!schema1 && !schema2) {
3131 return 1;
3132 } else if (!schema1 || !schema2) {
3133 return 0;
3134 }
3135
3136 assert(schema1->module->ctx != schema2->module->ctx);
3137
3138 if (schema1->nodetype != schema2->nodetype) {
3139 return 0;
3140 }
3141
3142 if (strcmp(schema1->name, schema2->name)) {
3143 return 0;
3144 }
3145
3146 if (strcmp(schema1->module->name, schema2->module->name)) {
3147 return 0;
3148 }
3149
3150 if (schema1->module->revision || schema2->module->revision) {
3151 if (!schema1->module->revision || !schema2->module->revision) {
3152 return 0;
3153 }
3154 if (strcmp(schema1->module->revision, schema2->module->revision)) {
3155 return 0;
3156 }
3157 }
3158
3159 return 1;
3160}
3161
3162/**
3163 * @brief Check the equality of the schemas for all parent nodes.
3164 *
3165 * Both nodes must be from different contexts.
3166 *
3167 * @param node1 Data of first node.
3168 * @param node2 Data of second node.
3169 * @return 1 if the all related parental schemas are equal otherwise 0.
3170 */
3171static ly_bool
3172lyd_compare_schema_parents_equal(const struct lyd_node *node1, const struct lyd_node *node2)
3173{
3174 const struct lysc_node *parent1, *parent2;
3175
3176 assert(node1 && node2);
3177
3178 for (parent1 = node1->schema->parent, parent2 = node2->schema->parent;
3179 parent1 && parent2;
3180 parent1 = parent1->parent, parent2 = parent2->parent) {
3181 if (!lyd_compare_schema_equal(parent1, parent2)) {
3182 return 0;
3183 }
3184 }
3185
3186 if (parent1 || parent2) {
3187 return 0;
3188 }
3189
3190 return 1;
3191}
3192
3193/**
3194 * @brief Internal implementation of @ref lyd_compare_single.
3195 * @copydoc lyd_compare_single
3196 * @param[in] parental_schemas_checked Flag used for optimization.
3197 * When this function is called for the first time, the flag must be set to 0.
3198 * The @ref lyd_compare_schema_parents_equal should be called only once during
3199 * recursive calls, and this is accomplished by setting to 1 in the lyd_compare_single_ body.
3200 */
3201static LY_ERR
3202lyd_compare_single_(const struct lyd_node *node1, const struct lyd_node *node2,
3203 uint32_t options, ly_bool parental_schemas_checked)
Radek Krejci1f05b6a2019-07-18 16:15:06 +02003204{
3205 const struct lyd_node *iter1, *iter2;
3206 struct lyd_node_term *term1, *term2;
3207 struct lyd_node_any *any1, *any2;
Michal Vasko52927e22020-03-16 17:26:14 +01003208 struct lyd_node_opaq *opaq1, *opaq2;
Radek Krejci1f05b6a2019-07-18 16:15:06 +02003209 size_t len1, len2;
Radek Krejci084289f2019-07-09 17:35:30 +02003210
Radek Krejci1f05b6a2019-07-18 16:15:06 +02003211 if (!node1 || !node2) {
3212 if (node1 == node2) {
3213 return LY_SUCCESS;
3214 } else {
3215 return LY_ENOT;
3216 }
3217 }
3218
aPiecek2f63f952021-03-30 12:22:18 +02003219 if (LYD_CTX(node1) == LYD_CTX(node2)) {
3220 /* same contexts */
3221 if (node1->schema != node2->schema) {
3222 return LY_ENOT;
3223 }
3224 } else {
3225 /* different contexts */
3226 if (!lyd_compare_schema_equal(node1->schema, node2->schema)) {
3227 return LY_ENOT;
3228 }
3229 if (!parental_schemas_checked) {
3230 if (!lyd_compare_schema_parents_equal(node1, node2)) {
3231 return LY_ENOT;
3232 }
3233 parental_schemas_checked = 1;
3234 }
Radek Krejci1f05b6a2019-07-18 16:15:06 +02003235 }
3236
3237 if (node1->hash != node2->hash) {
3238 return LY_ENOT;
3239 }
aPiecek2f63f952021-03-30 12:22:18 +02003240 /* 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 +02003241
Michal Vasko52927e22020-03-16 17:26:14 +01003242 if (!node1->schema) {
3243 opaq1 = (struct lyd_node_opaq *)node1;
3244 opaq2 = (struct lyd_node_opaq *)node2;
aPiecek2f63f952021-03-30 12:22:18 +02003245 if ((strcmp(opaq1->name.name, opaq2->name.name)) || (opaq1->format != opaq2->format) ||
3246 (strcmp(opaq1->name.module_ns, opaq2->name.module_ns))) {
Radek Krejci1f05b6a2019-07-18 16:15:06 +02003247 return LY_ENOT;
3248 }
Michal Vasko52927e22020-03-16 17:26:14 +01003249 switch (opaq1->format) {
Radek Krejci8df109d2021-04-23 12:19:08 +02003250 case LY_VALUE_XML:
Radek Krejci09c77442021-04-26 11:10:34 +02003251 if (lyxml_value_compare(LYD_CTX(node1), opaq1->value, opaq1->val_prefix_data, LYD_CTX(node2), opaq2->value,
3252 opaq2->val_prefix_data)) {
Michal Vasko52927e22020-03-16 17:26:14 +01003253 return LY_ENOT;
3254 }
3255 break;
Radek Krejci8df109d2021-04-23 12:19:08 +02003256 case LY_VALUE_JSON:
Radek Krejci1798aae2020-07-14 13:26:06 +02003257 /* prefixes in JSON are unique, so it is not necessary to canonize the values */
3258 if (strcmp(opaq1->value, opaq2->value)) {
3259 return LY_ENOT;
3260 }
3261 break;
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01003262 default:
Michal Vasko52927e22020-03-16 17:26:14 +01003263 /* not allowed */
Michal Vaskob7be7a82020-08-20 09:09:04 +02003264 LOGINT(LYD_CTX(node1));
Michal Vasko52927e22020-03-16 17:26:14 +01003265 return LY_EINT;
3266 }
3267 if (options & LYD_COMPARE_FULL_RECURSION) {
3268 iter1 = opaq1->child;
3269 iter2 = opaq2->child;
Radek Krejci1f05b6a2019-07-18 16:15:06 +02003270 goto all_children_compare;
Michal Vasko52927e22020-03-16 17:26:14 +01003271 }
3272 return LY_SUCCESS;
3273 } else {
3274 switch (node1->schema->nodetype) {
3275 case LYS_LEAF:
3276 case LYS_LEAFLIST:
3277 if (options & LYD_COMPARE_DEFAULTS) {
3278 if ((node1->flags & LYD_DEFAULT) != (node2->flags & LYD_DEFAULT)) {
3279 return LY_ENOT;
3280 }
3281 }
3282
Michal Vasko1a2bf2e2020-06-16 13:07:48 +02003283 term1 = (struct lyd_node_term *)node1;
3284 term2 = (struct lyd_node_term *)node2;
aPiecek2f63f952021-03-30 12:22:18 +02003285
3286 /* same contexts */
3287 if (LYD_CTX(node1) == LYD_CTX(node2)) {
3288 return term1->value.realtype->plugin->compare(&term1->value, &term2->value);
3289 }
3290
3291 /* different contexts */
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02003292 if (strcmp(lyd_get_value(node1), lyd_get_value(node2))) {
aPiecek2f63f952021-03-30 12:22:18 +02003293 return LY_ENOT;
3294 }
3295 return LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +01003296 case LYS_CONTAINER:
3297 if (options & LYD_COMPARE_DEFAULTS) {
3298 if ((node1->flags & LYD_DEFAULT) != (node2->flags & LYD_DEFAULT)) {
3299 return LY_ENOT;
3300 }
3301 }
3302 if (options & LYD_COMPARE_FULL_RECURSION) {
Michal Vasko9e685082021-01-29 14:49:09 +01003303 iter1 = lyd_child(node1);
3304 iter2 = lyd_child(node2);
Michal Vasko52927e22020-03-16 17:26:14 +01003305 goto all_children_compare;
Radek Krejci1f05b6a2019-07-18 16:15:06 +02003306 }
3307 return LY_SUCCESS;
Michal Vasko1bf09392020-03-27 12:38:10 +01003308 case LYS_RPC:
Michal Vasko52927e22020-03-16 17:26:14 +01003309 case LYS_ACTION:
3310 if (options & LYD_COMPARE_FULL_RECURSION) {
3311 /* TODO action/RPC
3312 goto all_children_compare;
3313 */
Radek Krejci1f05b6a2019-07-18 16:15:06 +02003314 }
3315 return LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +01003316 case LYS_NOTIF:
3317 if (options & LYD_COMPARE_FULL_RECURSION) {
3318 /* TODO Notification
3319 goto all_children_compare;
3320 */
3321 }
3322 return LY_SUCCESS;
3323 case LYS_LIST:
Michal Vasko9e685082021-01-29 14:49:09 +01003324 iter1 = lyd_child(node1);
3325 iter2 = lyd_child(node2);
Michal Vasko52927e22020-03-16 17:26:14 +01003326
3327 if (!(node1->schema->flags & LYS_KEYLESS) && !(options & LYD_COMPARE_FULL_RECURSION)) {
3328 /* lists with keys, their equivalence is based on their keys */
Michal Vasko544e58a2021-01-28 14:33:41 +01003329 for (const struct lysc_node *key = lysc_node_child(node1->schema);
Michal Vaskoc57d9a92020-06-23 13:28:27 +02003330 key && (key->flags & LYS_KEY);
Michal Vasko52927e22020-03-16 17:26:14 +01003331 key = key->next) {
aPiecek2f63f952021-03-30 12:22:18 +02003332 if (lyd_compare_single_(iter1, iter2, options, parental_schemas_checked)) {
Michal Vasko52927e22020-03-16 17:26:14 +01003333 return LY_ENOT;
3334 }
3335 iter1 = iter1->next;
3336 iter2 = iter2->next;
3337 }
3338 } else {
3339 /* lists without keys, their equivalence is based on equivalence of all the children (both direct and indirect) */
3340
Radek Krejci0f969882020-08-21 16:56:47 +02003341all_children_compare:
Michal Vasko52927e22020-03-16 17:26:14 +01003342 if (!iter1 && !iter2) {
3343 /* no children, nothing to compare */
3344 return LY_SUCCESS;
3345 }
3346
Michal Vaskod989ba02020-08-24 10:59:24 +02003347 for ( ; iter1 && iter2; iter1 = iter1->next, iter2 = iter2->next) {
aPiecek2f63f952021-03-30 12:22:18 +02003348 if (lyd_compare_single_(iter1, iter2, options | LYD_COMPARE_FULL_RECURSION, parental_schemas_checked)) {
Michal Vasko52927e22020-03-16 17:26:14 +01003349 return LY_ENOT;
3350 }
3351 }
3352 if (iter1 || iter2) {
3353 return LY_ENOT;
3354 }
3355 }
3356 return LY_SUCCESS;
3357 case LYS_ANYXML:
3358 case LYS_ANYDATA:
Michal Vasko22df3f02020-08-24 13:29:22 +02003359 any1 = (struct lyd_node_any *)node1;
3360 any2 = (struct lyd_node_any *)node2;
Michal Vasko52927e22020-03-16 17:26:14 +01003361
3362 if (any1->value_type != any2->value_type) {
3363 return LY_ENOT;
3364 }
3365 switch (any1->value_type) {
3366 case LYD_ANYDATA_DATATREE:
3367 iter1 = any1->value.tree;
3368 iter2 = any2->value.tree;
3369 goto all_children_compare;
3370 case LYD_ANYDATA_STRING:
3371 case LYD_ANYDATA_XML:
3372 case LYD_ANYDATA_JSON:
3373 len1 = strlen(any1->value.str);
3374 len2 = strlen(any2->value.str);
Michal Vasko69730152020-10-09 16:30:07 +02003375 if ((len1 != len2) || strcmp(any1->value.str, any2->value.str)) {
Michal Vasko52927e22020-03-16 17:26:14 +01003376 return LY_ENOT;
3377 }
3378 return LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +01003379 case LYD_ANYDATA_LYB:
Michal Vasko60ea6352020-06-29 13:39:39 +02003380 len1 = lyd_lyb_data_length(any1->value.mem);
3381 len2 = lyd_lyb_data_length(any2->value.mem);
Michal Vasko69730152020-10-09 16:30:07 +02003382 if ((len1 != len2) || memcmp(any1->value.mem, any2->value.mem, len1)) {
Michal Vasko52927e22020-03-16 17:26:14 +01003383 return LY_ENOT;
3384 }
3385 return LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +01003386 }
Radek Krejci1f05b6a2019-07-18 16:15:06 +02003387 }
3388 }
3389
Michal Vaskob7be7a82020-08-20 09:09:04 +02003390 LOGINT(LYD_CTX(node1));
Radek Krejci1f05b6a2019-07-18 16:15:06 +02003391 return LY_EINT;
3392}
Radek Krejci22ebdba2019-07-25 13:59:43 +02003393
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01003394LIBYANG_API_DEF LY_ERR
aPiecek2f63f952021-03-30 12:22:18 +02003395lyd_compare_single(const struct lyd_node *node1, const struct lyd_node *node2, uint32_t options)
3396{
3397 return lyd_compare_single_(node1, node2, options, 0);
3398}
3399
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01003400LIBYANG_API_DEF LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003401lyd_compare_siblings(const struct lyd_node *node1, const struct lyd_node *node2, uint32_t options)
Michal Vasko8f359bf2020-07-28 10:41:15 +02003402{
Michal Vaskod989ba02020-08-24 10:59:24 +02003403 for ( ; node1 && node2; node1 = node1->next, node2 = node2->next) {
Michal Vasko8f359bf2020-07-28 10:41:15 +02003404 LY_CHECK_RET(lyd_compare_single(node1, node2, options));
3405 }
3406
Michal Vasko11a81432020-07-28 16:31:29 +02003407 if (node1 == node2) {
3408 return LY_SUCCESS;
Michal Vasko8f359bf2020-07-28 10:41:15 +02003409 }
Michal Vasko11a81432020-07-28 16:31:29 +02003410 return LY_ENOT;
Michal Vasko8f359bf2020-07-28 10:41:15 +02003411}
3412
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01003413LIBYANG_API_DEF LY_ERR
Michal Vasko21725742020-06-29 11:49:25 +02003414lyd_compare_meta(const struct lyd_meta *meta1, const struct lyd_meta *meta2)
3415{
3416 if (!meta1 || !meta2) {
3417 if (meta1 == meta2) {
3418 return LY_SUCCESS;
3419 } else {
3420 return LY_ENOT;
3421 }
3422 }
3423
Michal Vaskoa8083062020-11-06 17:22:10 +01003424 if ((meta1->annotation->module->ctx != meta2->annotation->module->ctx) || (meta1->annotation != meta2->annotation)) {
Michal Vasko21725742020-06-29 11:49:25 +02003425 return LY_ENOT;
3426 }
3427
Michal Vasko21725742020-06-29 11:49:25 +02003428 return meta1->value.realtype->plugin->compare(&meta1->value, &meta2->value);
3429}
3430
Radek Krejci22ebdba2019-07-25 13:59:43 +02003431/**
Michal Vasko9cf62422021-07-01 13:29:32 +02003432 * @brief Create a copy of the attribute.
3433 *
3434 * @param[in] attr Attribute to copy.
3435 * @param[in] node Opaque where to append the new attribute.
3436 * @param[out] dup Optional created attribute copy.
3437 * @return LY_ERR value.
3438 */
3439static LY_ERR
3440lyd_dup_attr_single(const struct lyd_attr *attr, struct lyd_node *node, struct lyd_attr **dup)
3441{
3442 LY_ERR ret = LY_SUCCESS;
3443 struct lyd_attr *a, *last;
3444 struct lyd_node_opaq *opaq = (struct lyd_node_opaq *)node;
3445
3446 LY_CHECK_ARG_RET(NULL, attr, node, !node->schema, LY_EINVAL);
3447
3448 /* create a copy */
3449 a = calloc(1, sizeof *attr);
3450 LY_CHECK_ERR_RET(!a, LOGMEM(LYD_CTX(node)), LY_EMEM);
3451
3452 LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), attr->name.name, 0, &a->name.name), finish);
3453 LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), attr->name.prefix, 0, &a->name.prefix), finish);
3454 LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), attr->name.module_ns, 0, &a->name.module_ns), finish);
3455 LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), attr->value, 0, &a->value), finish);
3456 a->hints = attr->hints;
3457 a->format = attr->format;
3458 if (attr->val_prefix_data) {
3459 ret = ly_dup_prefix_data(LYD_CTX(node), attr->format, attr->val_prefix_data, &a->val_prefix_data);
3460 LY_CHECK_GOTO(ret, finish);
3461 }
3462
3463 /* insert as the last attribute */
3464 a->parent = opaq;
3465 if (opaq->attr) {
3466 for (last = opaq->attr; last->next; last = last->next) {}
3467 last->next = a;
3468 } else {
3469 opaq->attr = a;
3470 }
3471
3472finish:
3473 if (ret) {
3474 lyd_free_attr_single(LYD_CTX(node), a);
3475 } else if (dup) {
3476 *dup = a;
3477 }
3478 return LY_SUCCESS;
3479}
3480
3481/**
Michal Vaskoddd76592022-01-17 13:34:48 +01003482 * @brief Find @p schema equivalent in @p trg_ctx.
3483 *
3484 * @param[in] schema Schema node to find.
3485 * @param[in] trg_ctx Target context to search in.
3486 * @param[in] parent Data parent of @p schema, if any.
3487 * @param[out] trg_schema Found schema from @p trg_ctx to use.
3488 * @return LY_RRR value.
3489 */
3490static LY_ERR
Michal Vasko9beceb82022-04-05 12:14:15 +02003491lyd_dup_find_schema(const struct lysc_node *schema, const struct ly_ctx *trg_ctx, const struct lyd_node *parent,
Michal Vaskoddd76592022-01-17 13:34:48 +01003492 const struct lysc_node **trg_schema)
3493{
Michal Vasko9beceb82022-04-05 12:14:15 +02003494 const struct lysc_node *src_parent = NULL, *trg_parent = NULL, *sp, *tp;
3495 const struct lys_module *trg_mod = NULL;
Michal Vaskoddd76592022-01-17 13:34:48 +01003496 char *path;
3497
3498 if (!schema) {
3499 /* opaque node */
3500 *trg_schema = NULL;
3501 return LY_SUCCESS;
3502 }
3503
Michal Vasko9beceb82022-04-05 12:14:15 +02003504 if (lysc_data_parent(schema) && parent && parent->schema) {
Michal Vaskoddd76592022-01-17 13:34:48 +01003505 /* start from schema parent */
Michal Vasko9beceb82022-04-05 12:14:15 +02003506 trg_parent = parent->schema;
3507 src_parent = lysc_data_parent(schema);
3508 }
3509
3510 do {
3511 /* find the next parent */
3512 sp = schema;
3513 while (sp->parent != src_parent) {
3514 sp = sp->parent;
3515 }
3516 src_parent = sp;
3517
3518 if (!src_parent->parent) {
3519 /* find the module first */
3520 trg_mod = ly_ctx_get_module_implemented(trg_ctx, src_parent->module->name);
3521 if (!trg_mod) {
3522 LOGERR(trg_ctx, LY_ENOTFOUND, "Module \"%s\" not present/implemented in the target context.",
3523 src_parent->module->name);
3524 return LY_ENOTFOUND;
3525 }
3526 }
3527
3528 /* find the next parent */
3529 assert(trg_parent || trg_mod);
3530 tp = NULL;
3531 while ((tp = lys_getnext(tp, trg_parent, trg_mod ? trg_mod->compiled : NULL, 0))) {
3532 if (!strcmp(tp->name, src_parent->name) && !strcmp(tp->module->name, src_parent->module->name)) {
3533 break;
3534 }
3535 }
3536 if (!tp) {
3537 /* schema node not found */
3538 path = lysc_path(src_parent, LYSC_PATH_LOG, NULL, 0);
3539 LOGERR(trg_ctx, LY_ENOTFOUND, "Schema node \"%s\" not found in the target context.", path);
3540 free(path);
Michal Vaskoddd76592022-01-17 13:34:48 +01003541 return LY_ENOTFOUND;
3542 }
Michal Vaskoddd76592022-01-17 13:34:48 +01003543
Michal Vasko9beceb82022-04-05 12:14:15 +02003544 trg_parent = tp;
3545 } while (schema != src_parent);
Michal Vaskoddd76592022-01-17 13:34:48 +01003546
Michal Vasko9beceb82022-04-05 12:14:15 +02003547 /* success */
3548 *trg_schema = trg_parent;
3549 return LY_SUCCESS;
Michal Vaskoddd76592022-01-17 13:34:48 +01003550}
3551
3552/**
Michal Vasko52927e22020-03-16 17:26:14 +01003553 * @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 +02003554 *
Michal Vaskoddd76592022-01-17 13:34:48 +01003555 * 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 +02003556 *
Michal Vaskoddd76592022-01-17 13:34:48 +01003557 * @param[in] node Node to duplicate.
3558 * @param[in] trg_ctx Target context for duplicated nodes.
Radek Krejcif8b95172020-05-15 14:51:06 +02003559 * @param[in] parent Parent to insert into, NULL for top-level sibling.
Michal Vasko67177e52021-08-25 11:15:15 +02003560 * @param[in] insert_last Whether the duplicated node can be inserted as the last child of @p parent. Set for
3561 * recursive duplication as an optimization.
Radek Krejcif8b95172020-05-15 14:51:06 +02003562 * @param[in,out] first First sibling, NULL if no top-level sibling exist yet. Can be also NULL if @p parent is set.
3563 * @param[in] options Bitmask of options flags, see @ref dupoptions.
Michal Vaskoddd76592022-01-17 13:34:48 +01003564 * @param[out] dup_p Pointer where the created duplicated node is placed (besides connecting it to @p parent / @p first).
3565 * @return LY_ERR value.
Radek Krejci22ebdba2019-07-25 13:59:43 +02003566 */
Michal Vasko52927e22020-03-16 17:26:14 +01003567static LY_ERR
Michal Vaskoddd76592022-01-17 13:34:48 +01003568lyd_dup_r(const struct lyd_node *node, const struct ly_ctx *trg_ctx, struct lyd_node *parent, ly_bool insert_last,
3569 struct lyd_node **first, uint32_t options, struct lyd_node **dup_p)
Radek Krejci22ebdba2019-07-25 13:59:43 +02003570{
Michal Vasko52927e22020-03-16 17:26:14 +01003571 LY_ERR ret;
Radek Krejci22ebdba2019-07-25 13:59:43 +02003572 struct lyd_node *dup = NULL;
Michal Vasko61551fa2020-07-09 15:45:45 +02003573 struct lyd_meta *meta;
Michal Vasko9cf62422021-07-01 13:29:32 +02003574 struct lyd_attr *attr;
Michal Vasko61551fa2020-07-09 15:45:45 +02003575 struct lyd_node_any *any;
Michal Vaskoddd76592022-01-17 13:34:48 +01003576 const struct lysc_type *type;
3577 const char *val_can;
Radek Krejci22ebdba2019-07-25 13:59:43 +02003578
Michal Vasko52927e22020-03-16 17:26:14 +01003579 LY_CHECK_ARG_RET(NULL, node, LY_EINVAL);
Radek Krejci22ebdba2019-07-25 13:59:43 +02003580
Michal Vasko19175b62022-04-01 09:17:07 +02003581 if (node->flags & LYD_EXT) {
3582 /* we need to use the same context */
3583 trg_ctx = LYD_CTX(node);
3584 }
3585
Michal Vasko52927e22020-03-16 17:26:14 +01003586 if (!node->schema) {
3587 dup = calloc(1, sizeof(struct lyd_node_opaq));
Michal Vaskoddd76592022-01-17 13:34:48 +01003588 ((struct lyd_node_opaq *)dup)->ctx = trg_ctx;
Michal Vasko52927e22020-03-16 17:26:14 +01003589 } else {
3590 switch (node->schema->nodetype) {
Michal Vasko1bf09392020-03-27 12:38:10 +01003591 case LYS_RPC:
Michal Vasko52927e22020-03-16 17:26:14 +01003592 case LYS_ACTION:
3593 case LYS_NOTIF:
3594 case LYS_CONTAINER:
3595 case LYS_LIST:
3596 dup = calloc(1, sizeof(struct lyd_node_inner));
3597 break;
3598 case LYS_LEAF:
3599 case LYS_LEAFLIST:
3600 dup = calloc(1, sizeof(struct lyd_node_term));
3601 break;
3602 case LYS_ANYDATA:
3603 case LYS_ANYXML:
3604 dup = calloc(1, sizeof(struct lyd_node_any));
3605 break;
3606 default:
Michal Vaskoddd76592022-01-17 13:34:48 +01003607 LOGINT(trg_ctx);
Michal Vasko52927e22020-03-16 17:26:14 +01003608 ret = LY_EINT;
3609 goto error;
3610 }
Radek Krejci22ebdba2019-07-25 13:59:43 +02003611 }
Michal Vaskoddd76592022-01-17 13:34:48 +01003612 LY_CHECK_ERR_GOTO(!dup, LOGMEM(trg_ctx); ret = LY_EMEM, error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02003613
Michal Vaskof6df0a02020-06-16 13:08:34 +02003614 if (options & LYD_DUP_WITH_FLAGS) {
3615 dup->flags = node->flags;
3616 } else {
Michal Vasko19175b62022-04-01 09:17:07 +02003617 dup->flags = (node->flags & (LYD_DEFAULT | LYD_EXT)) | LYD_NEW;
Michal Vaskof6df0a02020-06-16 13:08:34 +02003618 }
Michal Vaskoddd76592022-01-17 13:34:48 +01003619 if (trg_ctx == LYD_CTX(node)) {
3620 dup->schema = node->schema;
3621 } else {
Michal Vasko27f536f2022-04-01 09:17:30 +02003622 ret = lyd_dup_find_schema(node->schema, trg_ctx, parent, &dup->schema);
3623 if (ret) {
3624 /* has no schema but is not an opaque node */
3625 free(dup);
3626 dup = NULL;
3627 goto error;
3628 }
Michal Vaskoddd76592022-01-17 13:34:48 +01003629 }
Michal Vasko52927e22020-03-16 17:26:14 +01003630 dup->prev = dup;
Radek Krejci22ebdba2019-07-25 13:59:43 +02003631
Michal Vasko9cf62422021-07-01 13:29:32 +02003632 /* duplicate metadata/attributes */
Michal Vasko25a32822020-07-09 15:48:22 +02003633 if (!(options & LYD_DUP_NO_META)) {
Michal Vasko9cf62422021-07-01 13:29:32 +02003634 if (!node->schema) {
3635 LY_LIST_FOR(((struct lyd_node_opaq *)node)->attr, attr) {
3636 LY_CHECK_GOTO(ret = lyd_dup_attr_single(attr, dup, NULL), error);
3637 }
3638 } else {
3639 LY_LIST_FOR(node->meta, meta) {
3640 LY_CHECK_GOTO(ret = lyd_dup_meta_single(meta, dup, NULL), error);
3641 }
Michal Vasko25a32822020-07-09 15:48:22 +02003642 }
3643 }
Radek Krejci22ebdba2019-07-25 13:59:43 +02003644
3645 /* nodetype-specific work */
Michal Vasko52927e22020-03-16 17:26:14 +01003646 if (!dup->schema) {
3647 struct lyd_node_opaq *opaq = (struct lyd_node_opaq *)dup;
3648 struct lyd_node_opaq *orig = (struct lyd_node_opaq *)node;
3649 struct lyd_node *child;
Radek Krejci22ebdba2019-07-25 13:59:43 +02003650
3651 if (options & LYD_DUP_RECURSIVE) {
3652 /* duplicate all the children */
3653 LY_LIST_FOR(orig->child, child) {
Michal Vaskoddd76592022-01-17 13:34:48 +01003654 LY_CHECK_GOTO(ret = lyd_dup_r(child, trg_ctx, dup, 1, NULL, options, NULL), error);
Michal Vasko52927e22020-03-16 17:26:14 +01003655 }
3656 }
Michal Vaskoddd76592022-01-17 13:34:48 +01003657 LY_CHECK_GOTO(ret = lydict_insert(trg_ctx, orig->name.name, 0, &opaq->name.name), error);
3658 LY_CHECK_GOTO(ret = lydict_insert(trg_ctx, orig->name.prefix, 0, &opaq->name.prefix), error);
3659 LY_CHECK_GOTO(ret = lydict_insert(trg_ctx, orig->name.module_ns, 0, &opaq->name.module_ns), error);
3660 LY_CHECK_GOTO(ret = lydict_insert(trg_ctx, orig->value, 0, &opaq->value), error);
Michal Vasko9cf62422021-07-01 13:29:32 +02003661 opaq->hints = orig->hints;
3662 opaq->format = orig->format;
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01003663 if (orig->val_prefix_data) {
Michal Vaskoddd76592022-01-17 13:34:48 +01003664 ret = ly_dup_prefix_data(trg_ctx, opaq->format, orig->val_prefix_data, &opaq->val_prefix_data);
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01003665 LY_CHECK_GOTO(ret, error);
Michal Vasko52927e22020-03-16 17:26:14 +01003666 }
Michal Vasko52927e22020-03-16 17:26:14 +01003667 } else if (dup->schema->nodetype & LYD_NODE_TERM) {
3668 struct lyd_node_term *term = (struct lyd_node_term *)dup;
3669 struct lyd_node_term *orig = (struct lyd_node_term *)node;
3670
3671 term->hash = orig->hash;
Michal Vaskoddd76592022-01-17 13:34:48 +01003672 if (trg_ctx == LYD_CTX(node)) {
3673 ret = orig->value.realtype->plugin->duplicate(trg_ctx, &orig->value, &term->value);
3674 LY_CHECK_ERR_GOTO(ret, LOGERR(trg_ctx, ret, "Value duplication failed."), error);
3675 } else {
3676 /* store canonical value in the target context */
3677 val_can = lyd_get_value(node);
3678 type = ((struct lysc_node_leaf *)term->schema)->type;
3679 ret = lyd_value_store(trg_ctx, &term->value, type, val_can, strlen(val_can), NULL, LY_VALUE_CANON, NULL,
3680 LYD_HINT_DATA, term->schema, NULL);
3681 LY_CHECK_GOTO(ret, error);
3682 }
Michal Vasko52927e22020-03-16 17:26:14 +01003683 } else if (dup->schema->nodetype & LYD_NODE_INNER) {
3684 struct lyd_node_inner *orig = (struct lyd_node_inner *)node;
3685 struct lyd_node *child;
3686
3687 if (options & LYD_DUP_RECURSIVE) {
3688 /* duplicate all the children */
3689 LY_LIST_FOR(orig->child, child) {
Michal Vaskoddd76592022-01-17 13:34:48 +01003690 LY_CHECK_GOTO(ret = lyd_dup_r(child, trg_ctx, dup, 1, NULL, options, NULL), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02003691 }
Michal Vasko69730152020-10-09 16:30:07 +02003692 } else if ((dup->schema->nodetype == LYS_LIST) && !(dup->schema->flags & LYS_KEYLESS)) {
Radek Krejci22ebdba2019-07-25 13:59:43 +02003693 /* always duplicate keys of a list */
Michal Vasko9beceb82022-04-05 12:14:15 +02003694 for (child = orig->child; child && lysc_is_key(child->schema); child = child->next) {
Michal Vaskoddd76592022-01-17 13:34:48 +01003695 LY_CHECK_GOTO(ret = lyd_dup_r(child, trg_ctx, dup, 1, NULL, options, NULL), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02003696 }
3697 }
3698 lyd_hash(dup);
3699 } else if (dup->schema->nodetype & LYD_NODE_ANY) {
Michal Vasko61551fa2020-07-09 15:45:45 +02003700 dup->hash = node->hash;
3701 any = (struct lyd_node_any *)node;
3702 LY_CHECK_GOTO(ret = lyd_any_copy_value(dup, &any->value, any->value_type), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02003703 }
3704
Michal Vasko52927e22020-03-16 17:26:14 +01003705 /* insert */
Michal Vasko67177e52021-08-25 11:15:15 +02003706 lyd_insert_node(parent, first, dup, insert_last);
Michal Vasko52927e22020-03-16 17:26:14 +01003707
3708 if (dup_p) {
3709 *dup_p = dup;
3710 }
3711 return LY_SUCCESS;
Radek Krejci22ebdba2019-07-25 13:59:43 +02003712
3713error:
Michal Vasko52927e22020-03-16 17:26:14 +01003714 lyd_free_tree(dup);
3715 return ret;
Radek Krejci22ebdba2019-07-25 13:59:43 +02003716}
3717
Michal Vasko29d674b2021-08-25 11:18:35 +02003718/**
3719 * @brief Get a parent node to connect duplicated subtree to.
3720 *
3721 * @param[in] node Node (subtree) to duplicate.
Michal Vaskoddd76592022-01-17 13:34:48 +01003722 * @param[in] trg_ctx Target context for duplicated nodes.
Michal Vasko29d674b2021-08-25 11:18:35 +02003723 * @param[in] parent Initial parent to connect to.
3724 * @param[in] options Bitmask of options flags, see @ref dupoptions.
3725 * @param[out] dup_parent First duplicated parent node, if any.
3726 * @param[out] local_parent Correct parent to directly connect duplicated @p node to.
3727 * @return LY_ERR value.
3728 */
Michal Vasko3a41dff2020-07-15 14:30:28 +02003729static LY_ERR
Michal Vaskoddd76592022-01-17 13:34:48 +01003730lyd_dup_get_local_parent(const struct lyd_node *node, const struct ly_ctx *trg_ctx, const struct lyd_node_inner *parent,
3731 uint32_t options, struct lyd_node **dup_parent, struct lyd_node_inner **local_parent)
Radek Krejci22ebdba2019-07-25 13:59:43 +02003732{
Michal Vasko3a41dff2020-07-15 14:30:28 +02003733 const struct lyd_node_inner *orig_parent, *iter;
Radek Krejci857189e2020-09-01 13:26:36 +02003734 ly_bool repeat = 1;
Michal Vasko3a41dff2020-07-15 14:30:28 +02003735
3736 *dup_parent = NULL;
3737 *local_parent = NULL;
3738
3739 for (orig_parent = node->parent; repeat && orig_parent; orig_parent = orig_parent->parent) {
3740 if (parent && (parent->schema == orig_parent->schema)) {
3741 /* stop creating parents, connect what we have into the provided parent */
3742 iter = parent;
3743 repeat = 0;
3744 } else {
3745 iter = NULL;
Michal Vaskoddd76592022-01-17 13:34:48 +01003746 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 +02003747 (struct lyd_node **)&iter));
Michal Vasko3a41dff2020-07-15 14:30:28 +02003748 }
3749 if (!*local_parent) {
3750 *local_parent = (struct lyd_node_inner *)iter;
3751 }
3752 if (iter->child) {
3753 /* 1) list - add after keys
3754 * 2) provided parent with some children */
3755 iter->child->prev->next = *dup_parent;
3756 if (*dup_parent) {
3757 (*dup_parent)->prev = iter->child->prev;
3758 iter->child->prev = *dup_parent;
3759 }
3760 } else {
3761 ((struct lyd_node_inner *)iter)->child = *dup_parent;
3762 }
3763 if (*dup_parent) {
3764 (*dup_parent)->parent = (struct lyd_node_inner *)iter;
3765 }
3766 *dup_parent = (struct lyd_node *)iter;
3767 }
3768
3769 if (repeat && parent) {
3770 /* given parent and created parents chain actually do not interconnect */
Michal Vaskoddd76592022-01-17 13:34:48 +01003771 LOGERR(trg_ctx, LY_EINVAL,
Michal Vasko69730152020-10-09 16:30:07 +02003772 "Invalid argument parent (%s()) - does not interconnect with the created node's parents chain.", __func__);
Michal Vasko3a41dff2020-07-15 14:30:28 +02003773 return LY_EINVAL;
3774 }
3775
3776 return LY_SUCCESS;
3777}
3778
3779static LY_ERR
Michal Vaskoddd76592022-01-17 13:34:48 +01003780lyd_dup(const struct lyd_node *node, const struct ly_ctx *trg_ctx, struct lyd_node_inner *parent, uint32_t options,
3781 ly_bool nosiblings, struct lyd_node **dup)
Michal Vasko3a41dff2020-07-15 14:30:28 +02003782{
3783 LY_ERR rc;
Radek Krejci22ebdba2019-07-25 13:59:43 +02003784 const struct lyd_node *orig; /* original node to be duplicated */
3785 struct lyd_node *first = NULL; /* the first duplicated node, this is returned */
Radek Krejci22ebdba2019-07-25 13:59:43 +02003786 struct lyd_node *top = NULL; /* the most higher created node */
3787 struct lyd_node_inner *local_parent = NULL; /* the direct parent node for the duplicated node(s) */
Radek Krejci22ebdba2019-07-25 13:59:43 +02003788
Michal Vaskoddd76592022-01-17 13:34:48 +01003789 assert(node && trg_ctx && (!parent || (LYD_CTX(parent) == trg_ctx)));
Radek Krejci22ebdba2019-07-25 13:59:43 +02003790
3791 if (options & LYD_DUP_WITH_PARENTS) {
Michal Vaskoddd76592022-01-17 13:34:48 +01003792 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 +02003793 &top, &local_parent), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02003794 } else {
3795 local_parent = parent;
3796 }
3797
Radek Krejci22ebdba2019-07-25 13:59:43 +02003798 LY_LIST_FOR(node, orig) {
Michal Vasko35f4d772021-01-12 12:08:57 +01003799 if (lysc_is_key(orig->schema)) {
3800 if (local_parent) {
3801 /* the key must already exist in the parent */
3802 rc = lyd_find_sibling_schema(local_parent->child, orig->schema, first ? NULL : &first);
Michal Vaskoddd76592022-01-17 13:34:48 +01003803 LY_CHECK_ERR_GOTO(rc, LOGINT(trg_ctx), error);
Michal Vasko35f4d772021-01-12 12:08:57 +01003804 } else {
3805 assert(!(options & LYD_DUP_WITH_PARENTS));
3806 /* duplicating a single key, okay, I suppose... */
Michal Vaskoddd76592022-01-17 13:34:48 +01003807 rc = lyd_dup_r(orig, trg_ctx, NULL, 0, &first, options, first ? NULL : &first);
Michal Vasko35f4d772021-01-12 12:08:57 +01003808 LY_CHECK_GOTO(rc, error);
3809 }
3810 } else {
3811 /* if there is no local parent, it will be inserted into first */
Michal Vaskoddd76592022-01-17 13:34:48 +01003812 rc = lyd_dup_r(orig, trg_ctx, local_parent ? &local_parent->node : NULL, 0, &first, options,
3813 first ? NULL : &first);
Michal Vasko35f4d772021-01-12 12:08:57 +01003814 LY_CHECK_GOTO(rc, error);
3815 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02003816 if (nosiblings) {
Radek Krejci22ebdba2019-07-25 13:59:43 +02003817 break;
3818 }
3819 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02003820
Michal Vasko3a41dff2020-07-15 14:30:28 +02003821 if (dup) {
3822 *dup = first;
3823 }
3824 return LY_SUCCESS;
Radek Krejci22ebdba2019-07-25 13:59:43 +02003825
3826error:
3827 if (top) {
3828 lyd_free_tree(top);
3829 } else {
Michal Vaskof03ed032020-03-04 13:31:44 +01003830 lyd_free_siblings(first);
Radek Krejci22ebdba2019-07-25 13:59:43 +02003831 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02003832 return rc;
Radek Krejci22ebdba2019-07-25 13:59:43 +02003833}
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003834
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01003835LIBYANG_API_DEF LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003836lyd_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 +02003837{
Michal Vaskoddd76592022-01-17 13:34:48 +01003838 LY_CHECK_ARG_RET(NULL, node, LY_EINVAL);
3839 if (node && parent && (LYD_CTX(node) != LYD_CTX(parent))) {
3840 LOGERR(NULL, LY_EINVAL, "Different contexts used in node duplication.");
3841 return LY_EINVAL;
3842 }
3843
3844 return lyd_dup(node, LYD_CTX(node), parent, options, 1, dup);
3845}
3846
3847LIBYANG_API_DEF LY_ERR
3848lyd_dup_single_to_ctx(const struct lyd_node *node, const struct ly_ctx *trg_ctx, struct lyd_node_inner *parent,
3849 uint32_t options, struct lyd_node **dup)
3850{
3851 LY_CHECK_ARG_RET(trg_ctx, node, trg_ctx, LY_EINVAL);
3852
3853 return lyd_dup(node, trg_ctx, parent, options, 1, dup);
Michal Vasko3a41dff2020-07-15 14:30:28 +02003854}
3855
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01003856LIBYANG_API_DEF LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003857lyd_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 +02003858{
Michal Vaskoddd76592022-01-17 13:34:48 +01003859 LY_CHECK_ARG_RET(NULL, node, LY_EINVAL);
3860 if (node && parent && (LYD_CTX(node) != LYD_CTX(parent))) {
3861 LOGERR(NULL, LY_EINVAL, "Different contexts used in node duplication.");
3862 return LY_EINVAL;
3863 }
3864
3865 return lyd_dup(node, LYD_CTX(node), parent, options, 0, dup);
3866}
3867
3868LIBYANG_API_DEF LY_ERR
3869lyd_dup_siblings_to_ctx(const struct lyd_node *node, const struct ly_ctx *trg_ctx, struct lyd_node_inner *parent,
3870 uint32_t options, struct lyd_node **dup)
3871{
3872 LY_CHECK_ARG_RET(trg_ctx, node, trg_ctx, LY_EINVAL);
3873
3874 return lyd_dup(node, trg_ctx, parent, options, 0, dup);
Michal Vasko3a41dff2020-07-15 14:30:28 +02003875}
3876
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01003877LIBYANG_API_DEF LY_ERR
Michal Vasko3a41dff2020-07-15 14:30:28 +02003878lyd_dup_meta_single(const struct lyd_meta *meta, struct lyd_node *node, struct lyd_meta **dup)
Michal Vasko25a32822020-07-09 15:48:22 +02003879{
Radek Krejci011e4aa2020-09-04 15:22:31 +02003880 LY_ERR ret = LY_SUCCESS;
Michal Vasko25a32822020-07-09 15:48:22 +02003881 struct lyd_meta *mt, *last;
3882
Michal Vasko3a41dff2020-07-15 14:30:28 +02003883 LY_CHECK_ARG_RET(NULL, meta, node, LY_EINVAL);
Michal Vasko25a32822020-07-09 15:48:22 +02003884
3885 /* create a copy */
3886 mt = calloc(1, sizeof *mt);
Michal Vaskob7be7a82020-08-20 09:09:04 +02003887 LY_CHECK_ERR_RET(!mt, LOGMEM(LYD_CTX(node)), LY_EMEM);
Michal Vasko25a32822020-07-09 15:48:22 +02003888 mt->annotation = meta->annotation;
Michal Vaskob7be7a82020-08-20 09:09:04 +02003889 ret = meta->value.realtype->plugin->duplicate(LYD_CTX(node), &meta->value, &mt->value);
Radek Krejci011e4aa2020-09-04 15:22:31 +02003890 LY_CHECK_ERR_GOTO(ret, LOGERR(LYD_CTX(node), LY_EINT, "Value duplication failed."), finish);
3891 LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), meta->name, 0, &mt->name), finish);
Michal Vasko25a32822020-07-09 15:48:22 +02003892
3893 /* insert as the last attribute */
Radek Krejci011e4aa2020-09-04 15:22:31 +02003894 mt->parent = node;
Michal Vasko25a32822020-07-09 15:48:22 +02003895 if (node->meta) {
Radek Krejci1e008d22020-08-17 11:37:37 +02003896 for (last = node->meta; last->next; last = last->next) {}
Michal Vasko25a32822020-07-09 15:48:22 +02003897 last->next = mt;
3898 } else {
3899 node->meta = mt;
3900 }
3901
Radek Krejci011e4aa2020-09-04 15:22:31 +02003902finish:
3903 if (ret) {
3904 lyd_free_meta_single(mt);
3905 } else if (dup) {
Michal Vasko3a41dff2020-07-15 14:30:28 +02003906 *dup = mt;
3907 }
3908 return LY_SUCCESS;
Michal Vasko25a32822020-07-09 15:48:22 +02003909}
3910
Michal Vasko4490d312020-06-16 13:08:55 +02003911/**
3912 * @brief Merge a source sibling into target siblings.
3913 *
3914 * @param[in,out] first_trg First target sibling, is updated if top-level.
3915 * @param[in] parent_trg Target parent.
3916 * @param[in,out] sibling_src Source sibling to merge, set to NULL if spent.
Michal Vaskocd3f6172021-05-18 16:14:50 +02003917 * @param[in] merge_cb Optional merge callback.
3918 * @param[in] cb_data Arbitrary callback data.
Michal Vasko4490d312020-06-16 13:08:55 +02003919 * @param[in] options Merge options.
Michal Vaskocd3f6172021-05-18 16:14:50 +02003920 * @param[in,out] dup_inst Duplicate instance cache for all @p first_trg siblings.
Michal Vasko4490d312020-06-16 13:08:55 +02003921 * @return LY_ERR value.
3922 */
3923static LY_ERR
3924lyd_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 +02003925 lyd_merge_cb merge_cb, void *cb_data, uint16_t options, struct lyd_dup_inst **dup_inst)
Michal Vasko4490d312020-06-16 13:08:55 +02003926{
Michal Vasko4490d312020-06-16 13:08:55 +02003927 const struct lyd_node *child_src, *tmp, *sibling_src;
Michal Vasko56daf732020-08-10 10:57:18 +02003928 struct lyd_node *match_trg, *dup_src, *elem;
Michal Vaskod1afa502022-01-27 16:18:12 +01003929 struct lyd_node_opaq *opaq_trg, *opaq_src;
Michal Vasko4490d312020-06-16 13:08:55 +02003930 struct lysc_type *type;
Michal Vaskocd3f6172021-05-18 16:14:50 +02003931 struct lyd_dup_inst *child_dup_inst = NULL;
3932 LY_ERR ret;
3933 ly_bool first_inst = 0;
Michal Vasko4490d312020-06-16 13:08:55 +02003934
3935 sibling_src = *sibling_src_p;
Michal Vaskocd3f6172021-05-18 16:14:50 +02003936 if (!sibling_src->schema) {
3937 /* try to find the same opaque node */
3938 lyd_find_sibling_opaq_next(*first_trg, LYD_NAME(sibling_src), &match_trg);
3939 } else if (sibling_src->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) {
Michal Vasko4490d312020-06-16 13:08:55 +02003940 /* try to find the exact instance */
Michal Vaskocd3f6172021-05-18 16:14:50 +02003941 lyd_find_sibling_first(*first_trg, sibling_src, &match_trg);
Michal Vasko4490d312020-06-16 13:08:55 +02003942 } else {
3943 /* try to simply find the node, there cannot be more instances */
Michal Vaskocd3f6172021-05-18 16:14:50 +02003944 lyd_find_sibling_val(*first_trg, sibling_src->schema, NULL, 0, &match_trg);
Michal Vasko4490d312020-06-16 13:08:55 +02003945 }
3946
Michal Vaskocd3f6172021-05-18 16:14:50 +02003947 if (match_trg) {
3948 /* update match as needed */
3949 LY_CHECK_RET(lyd_dup_inst_next(&match_trg, *first_trg, dup_inst));
3950 } else {
3951 /* first instance of this node */
3952 first_inst = 1;
3953 }
3954
3955 if (match_trg) {
3956 /* call callback */
3957 if (merge_cb) {
3958 LY_CHECK_RET(merge_cb(match_trg, sibling_src, cb_data));
3959 }
3960
Michal Vasko4490d312020-06-16 13:08:55 +02003961 /* node found, make sure even value matches for all node types */
Michal Vaskod1afa502022-01-27 16:18:12 +01003962 if (!match_trg->schema) {
3963 if (lyd_compare_single(sibling_src, match_trg, 0)) {
3964 /* update value */
3965 opaq_trg = (struct lyd_node_opaq *)match_trg;
3966 opaq_src = (struct lyd_node_opaq *)sibling_src;
3967
3968 lydict_remove(LYD_CTX(opaq_trg), opaq_trg->value);
3969 lydict_insert(LYD_CTX(opaq_trg), opaq_src->value, 0, &opaq_trg->value);
3970 opaq_trg->hints = opaq_src->hints;
3971
3972 ly_free_prefix_data(opaq_trg->format, opaq_trg->val_prefix_data);
3973 opaq_trg->format = opaq_src->format;
3974 ly_dup_prefix_data(LYD_CTX(opaq_trg), opaq_src->format, opaq_src->val_prefix_data,
3975 &opaq_trg->val_prefix_data);
3976 }
3977 } else if ((match_trg->schema->nodetype == LYS_LEAF) &&
3978 lyd_compare_single(sibling_src, match_trg, LYD_COMPARE_DEFAULTS)) {
Michal Vasko4490d312020-06-16 13:08:55 +02003979 /* since they are different, they cannot both be default */
3980 assert(!(sibling_src->flags & LYD_DEFAULT) || !(match_trg->flags & LYD_DEFAULT));
3981
Michal Vasko3a41dff2020-07-15 14:30:28 +02003982 /* update value (or only LYD_DEFAULT flag) only if flag set or the source node is not default */
3983 if ((options & LYD_MERGE_DEFAULTS) || !(sibling_src->flags & LYD_DEFAULT)) {
Michal Vasko4490d312020-06-16 13:08:55 +02003984 type = ((struct lysc_node_leaf *)match_trg->schema)->type;
Michal Vaskob7be7a82020-08-20 09:09:04 +02003985 type->plugin->free(LYD_CTX(match_trg), &((struct lyd_node_term *)match_trg)->value);
3986 LY_CHECK_RET(type->plugin->duplicate(LYD_CTX(match_trg), &((struct lyd_node_term *)sibling_src)->value,
Michal Vasko69730152020-10-09 16:30:07 +02003987 &((struct lyd_node_term *)match_trg)->value));
Michal Vasko4490d312020-06-16 13:08:55 +02003988
3989 /* copy flags and add LYD_NEW */
Michal Vasko7e8315b2021-08-03 17:01:06 +02003990 match_trg->flags = sibling_src->flags | ((options & LYD_MERGE_WITH_FLAGS) ? 0 : LYD_NEW);
Michal Vasko4490d312020-06-16 13:08:55 +02003991 }
Michal Vasko8f359bf2020-07-28 10:41:15 +02003992 } else if ((match_trg->schema->nodetype & LYS_ANYDATA) && lyd_compare_single(sibling_src, match_trg, 0)) {
Michal Vasko4c583e82020-07-17 12:16:14 +02003993 /* update value */
3994 LY_CHECK_RET(lyd_any_copy_value(match_trg, &((struct lyd_node_any *)sibling_src)->value,
Radek Krejci0f969882020-08-21 16:56:47 +02003995 ((struct lyd_node_any *)sibling_src)->value_type));
Michal Vasko4490d312020-06-16 13:08:55 +02003996
3997 /* copy flags and add LYD_NEW */
Michal Vasko7e8315b2021-08-03 17:01:06 +02003998 match_trg->flags = sibling_src->flags | ((options & LYD_MERGE_WITH_FLAGS) ? 0 : LYD_NEW);
Michal Vaskocd3f6172021-05-18 16:14:50 +02003999 }
4000
4001 /* check descendants, recursively */
4002 ret = LY_SUCCESS;
4003 LY_LIST_FOR_SAFE(lyd_child_no_keys(sibling_src), tmp, child_src) {
4004 ret = lyd_merge_sibling_r(lyd_node_child_p(match_trg), match_trg, &child_src, merge_cb, cb_data, options,
4005 &child_dup_inst);
4006 if (ret) {
4007 break;
Michal Vasko4490d312020-06-16 13:08:55 +02004008 }
4009 }
Michal Vaskocd3f6172021-05-18 16:14:50 +02004010 lyd_dup_inst_free(child_dup_inst);
4011 LY_CHECK_RET(ret);
Michal Vasko4490d312020-06-16 13:08:55 +02004012 } else {
4013 /* node not found, merge it */
4014 if (options & LYD_MERGE_DESTRUCT) {
4015 dup_src = (struct lyd_node *)sibling_src;
4016 lyd_unlink_tree(dup_src);
4017 /* spend it */
4018 *sibling_src_p = NULL;
4019 } else {
Michal Vasko3a41dff2020-07-15 14:30:28 +02004020 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 +02004021 }
4022
Michal Vasko7e8315b2021-08-03 17:01:06 +02004023 if (!(options & LYD_MERGE_WITH_FLAGS)) {
4024 /* set LYD_NEW for all the new nodes, required for validation */
4025 LYD_TREE_DFS_BEGIN(dup_src, elem) {
4026 elem->flags |= LYD_NEW;
4027 LYD_TREE_DFS_END(dup_src, elem);
4028 }
Michal Vasko4490d312020-06-16 13:08:55 +02004029 }
4030
Michal Vaskocd3f6172021-05-18 16:14:50 +02004031 /* insert */
Michal Vasko6ee6f432021-07-16 09:49:14 +02004032 lyd_insert_node(parent_trg, first_trg, dup_src, 0);
Michal Vaskocd3f6172021-05-18 16:14:50 +02004033
4034 if (first_inst) {
4035 /* remember not to find this instance next time */
4036 LY_CHECK_RET(lyd_dup_inst_next(&dup_src, *first_trg, dup_inst));
4037 }
4038
4039 /* call callback, no source node */
4040 if (merge_cb) {
4041 LY_CHECK_RET(merge_cb(dup_src, NULL, cb_data));
4042 }
Michal Vasko4490d312020-06-16 13:08:55 +02004043 }
4044
4045 return LY_SUCCESS;
4046}
4047
Michal Vasko3a41dff2020-07-15 14:30:28 +02004048static LY_ERR
Michal Vaskocd3f6172021-05-18 16:14:50 +02004049lyd_merge(struct lyd_node **target, const struct lyd_node *source, const struct lys_module *mod,
4050 lyd_merge_cb merge_cb, void *cb_data, uint16_t options, ly_bool nosiblings)
Michal Vasko4490d312020-06-16 13:08:55 +02004051{
4052 const struct lyd_node *sibling_src, *tmp;
Michal Vaskocd3f6172021-05-18 16:14:50 +02004053 struct lyd_dup_inst *dup_inst = NULL;
Radek Krejci857189e2020-09-01 13:26:36 +02004054 ly_bool first;
Michal Vaskocd3f6172021-05-18 16:14:50 +02004055 LY_ERR ret = LY_SUCCESS;
Michal Vasko4490d312020-06-16 13:08:55 +02004056
4057 LY_CHECK_ARG_RET(NULL, target, LY_EINVAL);
Michal Vasko892f5bf2021-11-24 10:41:05 +01004058 LY_CHECK_CTX_EQUAL_RET(*target ? LYD_CTX(*target) : NULL, source ? LYD_CTX(source) : NULL, mod ? mod->ctx : NULL,
4059 LY_EINVAL);
Michal Vasko4490d312020-06-16 13:08:55 +02004060
4061 if (!source) {
4062 /* nothing to merge */
4063 return LY_SUCCESS;
4064 }
4065
Michal Vasko831422b2020-11-24 11:20:51 +01004066 if ((*target && lysc_data_parent((*target)->schema)) || lysc_data_parent(source->schema)) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02004067 LOGERR(LYD_CTX(source), LY_EINVAL, "Invalid arguments - can merge only 2 top-level subtrees (%s()).", __func__);
Michal Vasko4490d312020-06-16 13:08:55 +02004068 return LY_EINVAL;
4069 }
4070
4071 LY_LIST_FOR_SAFE(source, tmp, sibling_src) {
Michal Vaskocd3f6172021-05-18 16:14:50 +02004072 if (mod && (lyd_owner_module(sibling_src) != mod)) {
4073 /* skip data nodes from different modules */
4074 continue;
4075 }
4076
Radek Krejci857189e2020-09-01 13:26:36 +02004077 first = (sibling_src == source) ? 1 : 0;
Michal Vaskocd3f6172021-05-18 16:14:50 +02004078 ret = lyd_merge_sibling_r(target, NULL, &sibling_src, merge_cb, cb_data, options, &dup_inst);
4079 if (ret) {
4080 break;
4081 }
Michal Vasko4490d312020-06-16 13:08:55 +02004082 if (first && !sibling_src) {
4083 /* source was spent (unlinked), move to the next node */
4084 source = tmp;
4085 }
4086
Michal Vasko3a41dff2020-07-15 14:30:28 +02004087 if (nosiblings) {
Michal Vasko4490d312020-06-16 13:08:55 +02004088 break;
4089 }
4090 }
4091
4092 if (options & LYD_MERGE_DESTRUCT) {
4093 /* free any leftover source data that were not merged */
4094 lyd_free_siblings((struct lyd_node *)source);
4095 }
4096
Michal Vaskocd3f6172021-05-18 16:14:50 +02004097 lyd_dup_inst_free(dup_inst);
4098 return ret;
Michal Vasko4490d312020-06-16 13:08:55 +02004099}
4100
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01004101LIBYANG_API_DEF LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004102lyd_merge_tree(struct lyd_node **target, const struct lyd_node *source, uint16_t options)
Michal Vasko3a41dff2020-07-15 14:30:28 +02004103{
Michal Vaskocd3f6172021-05-18 16:14:50 +02004104 return lyd_merge(target, source, NULL, NULL, NULL, options, 1);
Michal Vasko3a41dff2020-07-15 14:30:28 +02004105}
4106
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01004107LIBYANG_API_DEF LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004108lyd_merge_siblings(struct lyd_node **target, const struct lyd_node *source, uint16_t options)
Michal Vasko3a41dff2020-07-15 14:30:28 +02004109{
Michal Vaskocd3f6172021-05-18 16:14:50 +02004110 return lyd_merge(target, source, NULL, NULL, NULL, options, 0);
4111}
4112
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01004113LIBYANG_API_DEF LY_ERR
Michal Vaskocd3f6172021-05-18 16:14:50 +02004114lyd_merge_module(struct lyd_node **target, const struct lyd_node *source, const struct lys_module *mod,
4115 lyd_merge_cb merge_cb, void *cb_data, uint16_t options)
4116{
4117 return lyd_merge(target, source, mod, merge_cb, cb_data, options, 0);
Michal Vasko3a41dff2020-07-15 14:30:28 +02004118}
4119
Michal Vasko5ec7cda2019-09-11 13:43:08 +02004120static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02004121lyd_path_str_enlarge(char **buffer, size_t *buflen, size_t reqlen, ly_bool is_static)
Michal Vasko5ec7cda2019-09-11 13:43:08 +02004122{
Michal Vasko14654712020-02-06 08:35:21 +01004123 /* ending \0 */
4124 ++reqlen;
4125
Michal Vasko5ec7cda2019-09-11 13:43:08 +02004126 if (reqlen > *buflen) {
4127 if (is_static) {
4128 return LY_EINCOMPLETE;
4129 }
4130
4131 *buffer = ly_realloc(*buffer, reqlen * sizeof **buffer);
4132 if (!*buffer) {
4133 return LY_EMEM;
4134 }
4135
4136 *buflen = reqlen;
4137 }
4138
4139 return LY_SUCCESS;
4140}
4141
Michal Vaskod59035b2020-07-08 12:00:06 +02004142LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02004143lyd_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 +02004144{
4145 const struct lyd_node *key;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02004146 size_t len;
4147 const char *val;
4148 char quot;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02004149
Michal Vasko824d0832021-11-04 15:36:51 +01004150 for (key = lyd_child(node); key && key->schema && (key->schema->flags & LYS_KEY); key = key->next) {
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02004151 val = lyd_get_value(key);
Michal Vasko5ec7cda2019-09-11 13:43:08 +02004152 len = 1 + strlen(key->schema->name) + 2 + strlen(val) + 2;
Michal Vaskoba99a3e2020-08-18 15:50:05 +02004153 LY_CHECK_RET(lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static));
Michal Vasko5ec7cda2019-09-11 13:43:08 +02004154
4155 quot = '\'';
4156 if (strchr(val, '\'')) {
4157 quot = '"';
4158 }
4159 *bufused += sprintf(*buffer + *bufused, "[%s=%c%s%c]", key->schema->name, quot, val, quot);
Michal Vasko5ec7cda2019-09-11 13:43:08 +02004160 }
4161
4162 return LY_SUCCESS;
4163}
4164
4165/**
4166 * @brief Append leaf-list value predicate to path.
4167 *
4168 * @param[in] node Node to print.
4169 * @param[in,out] buffer Buffer to print to.
4170 * @param[in,out] buflen Current buffer length.
4171 * @param[in,out] bufused Current number of characters used in @p buffer.
4172 * @param[in] is_static Whether buffer is static or can be reallocated.
4173 * @return LY_ERR
4174 */
4175static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02004176lyd_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 +02004177{
Michal Vasko5ec7cda2019-09-11 13:43:08 +02004178 size_t len;
4179 const char *val;
4180 char quot;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02004181
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02004182 val = lyd_get_value(node);
Radek Krejcif13b87b2020-12-01 22:02:17 +01004183 len = 4 + strlen(val) + 2; /* "[.='" + val + "']" */
Michal Vaskoba99a3e2020-08-18 15:50:05 +02004184 LY_CHECK_RET(lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static));
Michal Vasko5ec7cda2019-09-11 13:43:08 +02004185
4186 quot = '\'';
4187 if (strchr(val, '\'')) {
4188 quot = '"';
4189 }
4190 *bufused += sprintf(*buffer + *bufused, "[.=%c%s%c]", quot, val, quot);
4191
Michal Vaskoba99a3e2020-08-18 15:50:05 +02004192 return LY_SUCCESS;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02004193}
4194
4195/**
4196 * @brief Append node position (relative to its other instances) predicate to path.
4197 *
4198 * @param[in] node Node to print.
4199 * @param[in,out] buffer Buffer to print to.
4200 * @param[in,out] buflen Current buffer length.
4201 * @param[in,out] bufused Current number of characters used in @p buffer.
4202 * @param[in] is_static Whether buffer is static or can be reallocated.
4203 * @return LY_ERR
4204 */
4205static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02004206lyd_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 +02004207{
Michal Vasko5ec7cda2019-09-11 13:43:08 +02004208 size_t len;
Michal Vasko50cc0562021-05-18 16:15:43 +02004209 uint32_t pos;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02004210 char *val = NULL;
4211 LY_ERR rc;
4212
Michal Vasko50cc0562021-05-18 16:15:43 +02004213 pos = lyd_list_pos(node);
4214 if (asprintf(&val, "%" PRIu32, pos) == -1) {
Michal Vasko5ec7cda2019-09-11 13:43:08 +02004215 return LY_EMEM;
4216 }
4217
4218 len = 1 + strlen(val) + 1;
4219 rc = lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static);
4220 if (rc != LY_SUCCESS) {
4221 goto cleanup;
4222 }
4223
4224 *bufused += sprintf(*buffer + *bufused, "[%s]", val);
4225
4226cleanup:
4227 free(val);
4228 return rc;
4229}
4230
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01004231LIBYANG_API_DEF char *
Michal Vasko5ec7cda2019-09-11 13:43:08 +02004232lyd_path(const struct lyd_node *node, LYD_PATH_TYPE pathtype, char *buffer, size_t buflen)
4233{
Radek Krejci857189e2020-09-01 13:26:36 +02004234 ly_bool is_static = 0;
Radek Krejci1deb5be2020-08-26 16:43:36 +02004235 uint32_t i, depth;
Michal Vasko14654712020-02-06 08:35:21 +01004236 size_t bufused = 0, len;
Michal Vasko12eb6222022-03-18 13:35:49 +01004237 const struct lyd_node *iter, *parent;
4238 const struct lys_module *mod, *prev_mod;
Michal Vasko790b2bc2020-08-03 13:35:06 +02004239 LY_ERR rc = LY_SUCCESS;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02004240
4241 LY_CHECK_ARG_RET(NULL, node, NULL);
4242 if (buffer) {
Michal Vasko16385f42021-05-18 16:16:09 +02004243 LY_CHECK_ARG_RET(LYD_CTX(node), buflen > 1, NULL);
Michal Vasko5ec7cda2019-09-11 13:43:08 +02004244 is_static = 1;
Michal Vasko14654712020-02-06 08:35:21 +01004245 } else {
4246 buflen = 0;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02004247 }
4248
4249 switch (pathtype) {
Radek Krejci635d2b82021-01-04 11:26:51 +01004250 case LYD_PATH_STD:
4251 case LYD_PATH_STD_NO_LAST_PRED:
Michal Vasko14654712020-02-06 08:35:21 +01004252 depth = 1;
Michal Vasko9e685082021-01-29 14:49:09 +01004253 for (iter = node; iter->parent; iter = lyd_parent(iter)) {
Michal Vasko5ec7cda2019-09-11 13:43:08 +02004254 ++depth;
4255 }
4256
Michal Vasko5ec7cda2019-09-11 13:43:08 +02004257 goto iter_print;
Michal Vasko14654712020-02-06 08:35:21 +01004258 while (depth) {
Michal Vasko5ec7cda2019-09-11 13:43:08 +02004259 /* find the right node */
Michal Vasko9e685082021-01-29 14:49:09 +01004260 for (iter = node, i = 1; i < depth; iter = lyd_parent(iter), ++i) {}
Michal Vasko5ec7cda2019-09-11 13:43:08 +02004261iter_print:
Michal Vasko12eb6222022-03-18 13:35:49 +01004262 /* get the module */
4263 mod = iter->schema ? iter->schema->module : lyd_owner_module(iter);
4264 parent = lyd_parent(iter);
4265 prev_mod = (parent && parent->schema) ? parent->schema->module : lyd_owner_module(parent);
4266 if (prev_mod == mod) {
4267 mod = NULL;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02004268 }
4269
4270 /* realloc string */
Michal Vaskoad92b672020-11-12 13:11:31 +01004271 len = 1 + (mod ? strlen(mod->name) + 1 : 0) + (iter->schema ? strlen(iter->schema->name) :
4272 strlen(((struct lyd_node_opaq *)iter)->name.name));
Michal Vasko5ec7cda2019-09-11 13:43:08 +02004273 rc = lyd_path_str_enlarge(&buffer, &buflen, bufused + len, is_static);
4274 if (rc != LY_SUCCESS) {
4275 break;
4276 }
4277
4278 /* print next node */
Radek Krejci1798aae2020-07-14 13:26:06 +02004279 bufused += sprintf(buffer + bufused, "/%s%s%s", mod ? mod->name : "", mod ? ":" : "",
Michal Vaskoad92b672020-11-12 13:11:31 +01004280 iter->schema ? iter->schema->name : ((struct lyd_node_opaq *)iter)->name.name);
Michal Vasko5ec7cda2019-09-11 13:43:08 +02004281
Michal Vasko790b2bc2020-08-03 13:35:06 +02004282 /* do not always print the last (first) predicate */
Radek Krejci635d2b82021-01-04 11:26:51 +01004283 if (iter->schema && ((depth > 1) || (pathtype == LYD_PATH_STD))) {
Michal Vasko790b2bc2020-08-03 13:35:06 +02004284 switch (iter->schema->nodetype) {
4285 case LYS_LIST:
4286 if (iter->schema->flags & LYS_KEYLESS) {
4287 /* print its position */
4288 rc = lyd_path_position_predicate(iter, &buffer, &buflen, &bufused, is_static);
4289 } else {
4290 /* print all list keys in predicates */
4291 rc = lyd_path_list_predicate(iter, &buffer, &buflen, &bufused, is_static);
4292 }
4293 break;
4294 case LYS_LEAFLIST:
4295 if (iter->schema->flags & LYS_CONFIG_W) {
4296 /* print leaf-list value */
4297 rc = lyd_path_leaflist_predicate(iter, &buffer, &buflen, &bufused, is_static);
4298 } else {
4299 /* print its position */
4300 rc = lyd_path_position_predicate(iter, &buffer, &buflen, &bufused, is_static);
4301 }
4302 break;
4303 default:
4304 /* nothing to print more */
4305 break;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02004306 }
Michal Vasko5ec7cda2019-09-11 13:43:08 +02004307 }
4308 if (rc != LY_SUCCESS) {
4309 break;
4310 }
4311
Michal Vasko14654712020-02-06 08:35:21 +01004312 --depth;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02004313 }
4314 break;
4315 }
4316
4317 return buffer;
4318}
Michal Vaskoe444f752020-02-10 12:20:06 +01004319
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01004320LIBYANG_API_DEF struct lyd_meta *
Michal Vasko25a32822020-07-09 15:48:22 +02004321lyd_find_meta(const struct lyd_meta *first, const struct lys_module *module, const char *name)
4322{
4323 struct lyd_meta *ret = NULL;
4324 const struct ly_ctx *ctx;
4325 const char *prefix, *tmp;
4326 char *str;
4327 size_t pref_len, name_len;
4328
4329 LY_CHECK_ARG_RET(NULL, module || strchr(name, ':'), name, NULL);
Michal Vasko892f5bf2021-11-24 10:41:05 +01004330 LY_CHECK_CTX_EQUAL_RET(first ? first->annotation->module->ctx : NULL, module ? module->ctx : NULL, NULL);
Michal Vasko25a32822020-07-09 15:48:22 +02004331
4332 if (!first) {
4333 return NULL;
4334 }
4335
4336 ctx = first->annotation->module->ctx;
4337
4338 /* parse the name */
4339 tmp = name;
4340 if (ly_parse_nodeid(&tmp, &prefix, &pref_len, &name, &name_len) || tmp[0]) {
4341 LOGERR(ctx, LY_EINVAL, "Metadata name \"%s\" is not valid.", name);
4342 return NULL;
4343 }
4344
4345 /* find the module */
4346 if (prefix) {
4347 str = strndup(prefix, pref_len);
4348 module = ly_ctx_get_module_latest(ctx, str);
4349 free(str);
Radek Krejci422afb12021-03-04 16:38:16 +01004350 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 +02004351 }
4352
4353 /* find the metadata */
4354 LY_LIST_FOR(first, first) {
4355 if ((first->annotation->module == module) && !strcmp(first->name, name)) {
4356 ret = (struct lyd_meta *)first;
4357 break;
4358 }
4359 }
4360
4361 return ret;
4362}
4363
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01004364LIBYANG_API_DEF LY_ERR
Michal Vaskoe444f752020-02-10 12:20:06 +01004365lyd_find_sibling_first(const struct lyd_node *siblings, const struct lyd_node *target, struct lyd_node **match)
4366{
Michal Vasko9beceb82022-04-05 12:14:15 +02004367 struct lyd_node **match_p, *iter, *dup = NULL;
Michal Vaskoe444f752020-02-10 12:20:06 +01004368 struct lyd_node_inner *parent;
Michal Vaskoe78faec2021-04-08 17:24:43 +02004369 ly_bool found;
Michal Vaskoe444f752020-02-10 12:20:06 +01004370
Michal Vaskof03ed032020-03-04 13:31:44 +01004371 LY_CHECK_ARG_RET(NULL, target, LY_EINVAL);
Michal Vasko9beceb82022-04-05 12:14:15 +02004372 if (!siblings) {
4373 /* no data */
4374 if (match) {
4375 *match = NULL;
4376 }
4377 return LY_ENOTFOUND;
4378 }
Michal Vaskoe444f752020-02-10 12:20:06 +01004379
Michal Vasko9beceb82022-04-05 12:14:15 +02004380 if (LYD_CTX(siblings) != LYD_CTX(target)) {
4381 /* create a duplicate in this context */
4382 LY_CHECK_RET(lyd_dup_single_to_ctx(target, LYD_CTX(siblings), NULL, 0, &dup));
4383 target = dup;
4384 }
4385
4386 if ((siblings->schema && target->schema && (lysc_data_parent(siblings->schema) != lysc_data_parent(target->schema)))) {
4387 /* schema mismatch */
4388 lyd_free_tree(dup);
Michal Vasko9b368d32020-02-14 13:53:31 +01004389 if (match) {
4390 *match = NULL;
4391 }
Michal Vaskoe444f752020-02-10 12:20:06 +01004392 return LY_ENOTFOUND;
4393 }
4394
Michal Vaskoe78faec2021-04-08 17:24:43 +02004395 /* get first sibling */
4396 siblings = lyd_first_sibling(siblings);
Michal Vaskoe444f752020-02-10 12:20:06 +01004397
Michal Vasko9e685082021-01-29 14:49:09 +01004398 parent = siblings->parent;
Michal Vasko6da1e952020-12-09 18:14:38 +01004399 if (parent && parent->schema && parent->children_ht) {
Michal Vaskoe444f752020-02-10 12:20:06 +01004400 assert(target->hash);
4401
Michal Vaskoe78faec2021-04-08 17:24:43 +02004402 if (lysc_is_dup_inst_list(target->schema)) {
4403 /* we must search the instances from beginning to find the first matching one */
4404 found = 0;
4405 LYD_LIST_FOR_INST(siblings, target->schema, iter) {
4406 if (!lyd_compare_single(target, iter, 0)) {
4407 found = 1;
4408 break;
4409 }
4410 }
4411 if (found) {
4412 siblings = iter;
Michal Vaskoda859032020-07-14 12:20:14 +02004413 } else {
4414 siblings = NULL;
4415 }
Michal Vaskoe444f752020-02-10 12:20:06 +01004416 } else {
Michal Vaskoe78faec2021-04-08 17:24:43 +02004417 /* find by hash */
4418 if (!lyht_find(parent->children_ht, &target, target->hash, (void **)&match_p)) {
4419 siblings = *match_p;
4420 } else {
4421 /* not found */
4422 siblings = NULL;
4423 }
Michal Vaskoe444f752020-02-10 12:20:06 +01004424 }
4425 } else {
4426 /* no children hash table */
Michal Vaskod989ba02020-08-24 10:59:24 +02004427 for ( ; siblings; siblings = siblings->next) {
Michal Vasko8f359bf2020-07-28 10:41:15 +02004428 if (!lyd_compare_single(siblings, target, 0)) {
Michal Vaskoe444f752020-02-10 12:20:06 +01004429 break;
4430 }
4431 }
4432 }
4433
Michal Vasko9beceb82022-04-05 12:14:15 +02004434 lyd_free_tree(dup);
Michal Vaskoe444f752020-02-10 12:20:06 +01004435 if (!siblings) {
Michal Vasko9b368d32020-02-14 13:53:31 +01004436 if (match) {
4437 *match = NULL;
4438 }
Michal Vaskoe444f752020-02-10 12:20:06 +01004439 return LY_ENOTFOUND;
4440 }
4441
Michal Vasko9b368d32020-02-14 13:53:31 +01004442 if (match) {
4443 *match = (struct lyd_node *)siblings;
4444 }
Michal Vaskoe444f752020-02-10 12:20:06 +01004445 return LY_SUCCESS;
4446}
4447
Radek Krejci857189e2020-09-01 13:26:36 +02004448/**
4449 * @brief Comparison callback to match schema node with a schema of a data node.
4450 *
4451 * @param[in] val1_p Pointer to the schema node
4452 * @param[in] val2_p Pointer to the data node
Michal Vasko62524a92021-02-26 10:08:50 +01004453 * Implementation of ::lyht_value_equal_cb.
Radek Krejci857189e2020-09-01 13:26:36 +02004454 */
4455static ly_bool
4456lyd_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 +01004457{
4458 struct lysc_node *val1;
4459 struct lyd_node *val2;
4460
4461 val1 = *((struct lysc_node **)val1_p);
4462 val2 = *((struct lyd_node **)val2_p);
4463
Michal Vasko90932a92020-02-12 14:33:03 +01004464 if (val1 == val2->schema) {
4465 /* schema match is enough */
4466 return 1;
4467 } else {
4468 return 0;
4469 }
4470}
4471
Michal Vasko92239c72020-11-18 18:17:25 +01004472/**
4473 * @brief Search in the given siblings (NOT recursively) for the first schema node data instance.
4474 * Uses hashes - should be used whenever possible for best performance.
4475 *
4476 * @param[in] siblings Siblings to search in including preceding and succeeding nodes.
4477 * @param[in] schema Target data node schema to find.
4478 * @param[out] match Can be NULL, otherwise the found data node.
4479 * @return LY_SUCCESS on success, @p match set.
4480 * @return LY_ENOTFOUND if not found, @p match set to NULL.
4481 * @return LY_ERR value if another error occurred.
4482 */
Michal Vasko90932a92020-02-12 14:33:03 +01004483static LY_ERR
4484lyd_find_sibling_schema(const struct lyd_node *siblings, const struct lysc_node *schema, struct lyd_node **match)
4485{
4486 struct lyd_node **match_p;
4487 struct lyd_node_inner *parent;
4488 uint32_t hash;
Michal Vasko62524a92021-02-26 10:08:50 +01004489 lyht_value_equal_cb ht_cb;
Michal Vasko90932a92020-02-12 14:33:03 +01004490
Michal Vaskob104f112020-07-17 09:54:54 +02004491 assert(siblings && schema);
Michal Vasko90932a92020-02-12 14:33:03 +01004492
Michal Vasko9e685082021-01-29 14:49:09 +01004493 parent = siblings->parent;
Michal Vasko08fd6132020-11-18 18:17:45 +01004494 if (parent && parent->schema && parent->children_ht) {
Michal Vasko90932a92020-02-12 14:33:03 +01004495 /* calculate our hash */
4496 hash = dict_hash_multi(0, schema->module->name, strlen(schema->module->name));
4497 hash = dict_hash_multi(hash, schema->name, strlen(schema->name));
4498 hash = dict_hash_multi(hash, NULL, 0);
4499
4500 /* use special hash table function */
4501 ht_cb = lyht_set_cb(parent->children_ht, lyd_hash_table_schema_val_equal);
4502
4503 /* find by hash */
4504 if (!lyht_find(parent->children_ht, &schema, hash, (void **)&match_p)) {
4505 siblings = *match_p;
4506 } else {
4507 /* not found */
4508 siblings = NULL;
4509 }
4510
4511 /* set the original hash table compare function back */
4512 lyht_set_cb(parent->children_ht, ht_cb);
4513 } else {
Michal Vaskob104f112020-07-17 09:54:54 +02004514 /* find first sibling */
4515 if (siblings->parent) {
4516 siblings = siblings->parent->child;
4517 } else {
4518 while (siblings->prev->next) {
4519 siblings = siblings->prev;
4520 }
4521 }
4522
4523 /* search manually without hashes */
Michal Vaskod989ba02020-08-24 10:59:24 +02004524 for ( ; siblings; siblings = siblings->next) {
Michal Vasko90932a92020-02-12 14:33:03 +01004525 if (siblings->schema == schema) {
4526 /* schema match is enough */
4527 break;
4528 }
4529 }
4530 }
4531
4532 if (!siblings) {
Michal Vasko9b368d32020-02-14 13:53:31 +01004533 if (match) {
4534 *match = NULL;
4535 }
Michal Vasko90932a92020-02-12 14:33:03 +01004536 return LY_ENOTFOUND;
4537 }
4538
Michal Vasko9b368d32020-02-14 13:53:31 +01004539 if (match) {
4540 *match = (struct lyd_node *)siblings;
4541 }
Michal Vasko90932a92020-02-12 14:33:03 +01004542 return LY_SUCCESS;
4543}
4544
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01004545LIBYANG_API_DEF LY_ERR
Michal Vaskoe444f752020-02-10 12:20:06 +01004546lyd_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 +02004547 size_t val_len, struct lyd_node **match)
Michal Vaskoe444f752020-02-10 12:20:06 +01004548{
4549 LY_ERR rc;
4550 struct lyd_node *target = NULL;
Michal Vasko9beceb82022-04-05 12:14:15 +02004551 const struct lyd_node *parent;
Michal Vaskoe444f752020-02-10 12:20:06 +01004552
Michal Vasko4c583e82020-07-17 12:16:14 +02004553 LY_CHECK_ARG_RET(NULL, schema, !(schema->nodetype & (LYS_CHOICE | LYS_CASE)), LY_EINVAL);
Michal Vasko9beceb82022-04-05 12:14:15 +02004554 if (!siblings) {
4555 /* no data */
4556 if (match) {
4557 *match = NULL;
4558 }
4559 return LY_ENOTFOUND;
4560 }
Michal Vaskoe444f752020-02-10 12:20:06 +01004561
Michal Vasko9beceb82022-04-05 12:14:15 +02004562 if ((LYD_CTX(siblings) != schema->module->ctx)) {
4563 /* parent of ext nodes is useless */
4564 parent = (siblings->flags & LYD_EXT) ? NULL : lyd_parent(siblings);
4565 LY_CHECK_RET(lyd_dup_find_schema(schema, LYD_CTX(siblings), parent, &schema));
4566 }
4567
4568 if (siblings->schema && (lysc_data_parent(siblings->schema) != lysc_data_parent(schema))) {
4569 /* schema mismatch */
Michal Vasko9b368d32020-02-14 13:53:31 +01004570 if (match) {
4571 *match = NULL;
4572 }
Michal Vaskoe444f752020-02-10 12:20:06 +01004573 return LY_ENOTFOUND;
4574 }
4575
Michal Vaskof03ed032020-03-04 13:31:44 +01004576 if (key_or_value && !val_len) {
4577 val_len = strlen(key_or_value);
4578 }
4579
Michal Vaskob104f112020-07-17 09:54:54 +02004580 if ((schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) && key_or_value) {
4581 /* create a data node and find the instance */
4582 if (schema->nodetype == LYS_LEAFLIST) {
4583 /* target used attributes: schema, hash, value */
Radek Krejci8df109d2021-04-23 12:19:08 +02004584 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 +02004585 LY_CHECK_RET(rc);
Michal Vaskob104f112020-07-17 09:54:54 +02004586 } else {
Michal Vasko90932a92020-02-12 14:33:03 +01004587 /* target used attributes: schema, hash, child (all keys) */
Michal Vasko004d3152020-06-11 19:59:22 +02004588 LY_CHECK_RET(lyd_create_list2(schema, key_or_value, val_len, &target));
Michal Vasko90932a92020-02-12 14:33:03 +01004589 }
4590
4591 /* find it */
4592 rc = lyd_find_sibling_first(siblings, target, match);
Michal Vaskob104f112020-07-17 09:54:54 +02004593 } else {
4594 /* find the first schema node instance */
4595 rc = lyd_find_sibling_schema(siblings, schema, match);
Michal Vaskoe444f752020-02-10 12:20:06 +01004596 }
4597
Michal Vaskoe444f752020-02-10 12:20:06 +01004598 lyd_free_tree(target);
4599 return rc;
4600}
Michal Vaskoccc02342020-05-21 10:09:21 +02004601
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01004602LIBYANG_API_DEF LY_ERR
Michal Vaskoe78faec2021-04-08 17:24:43 +02004603lyd_find_sibling_dup_inst_set(const struct lyd_node *siblings, const struct lyd_node *target, struct ly_set **set)
4604{
4605 struct lyd_node **match_p, *first, *iter;
4606 struct lyd_node_inner *parent;
4607
4608 LY_CHECK_ARG_RET(NULL, target, lysc_is_dup_inst_list(target->schema), set, LY_EINVAL);
Michal Vasko892f5bf2021-11-24 10:41:05 +01004609 LY_CHECK_CTX_EQUAL_RET(siblings ? LYD_CTX(siblings) : NULL, LYD_CTX(target), LY_EINVAL);
Michal Vaskoe78faec2021-04-08 17:24:43 +02004610
4611 LY_CHECK_RET(ly_set_new(set));
4612
4613 if (!siblings || (siblings->schema && (lysc_data_parent(siblings->schema) != lysc_data_parent(target->schema)))) {
4614 /* no data or schema mismatch */
4615 return LY_ENOTFOUND;
4616 }
4617
4618 /* get first sibling */
4619 siblings = lyd_first_sibling(siblings);
4620
4621 parent = siblings->parent;
4622 if (parent && parent->schema && parent->children_ht) {
4623 assert(target->hash);
4624
4625 /* find the first instance */
4626 lyd_find_sibling_first(siblings, target, &first);
4627 if (first) {
4628 /* add it so that it is the first in the set */
4629 if (ly_set_add(*set, first, 1, NULL)) {
4630 goto error;
4631 }
4632
4633 /* find by hash */
4634 if (!lyht_find(parent->children_ht, &target, target->hash, (void **)&match_p)) {
4635 iter = *match_p;
4636 } else {
4637 /* not found */
4638 iter = NULL;
4639 }
4640 while (iter) {
4641 /* add all found nodes into the set */
4642 if ((iter != first) && !lyd_compare_single(iter, target, 0) && ly_set_add(*set, iter, 1, NULL)) {
4643 goto error;
4644 }
4645
4646 /* find next instance */
4647 if (lyht_find_next(parent->children_ht, &iter, iter->hash, (void **)&match_p)) {
4648 iter = NULL;
4649 } else {
4650 iter = *match_p;
4651 }
4652 }
4653 }
4654 } else {
4655 /* no children hash table */
4656 LY_LIST_FOR(siblings, siblings) {
4657 if (!lyd_compare_single(target, siblings, 0)) {
4658 ly_set_add(*set, (void *)siblings, 1, NULL);
4659 }
4660 }
4661 }
4662
4663 if (!(*set)->count) {
4664 return LY_ENOTFOUND;
4665 }
4666 return LY_SUCCESS;
4667
4668error:
4669 ly_set_free(*set, NULL);
4670 *set = NULL;
4671 return LY_EMEM;
4672}
4673
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01004674LIBYANG_API_DEF LY_ERR
Michal Vasko1d4af6c2021-02-22 13:31:26 +01004675lyd_find_sibling_opaq_next(const struct lyd_node *first, const char *name, struct lyd_node **match)
4676{
4677 LY_CHECK_ARG_RET(NULL, name, LY_EINVAL);
4678
4679 for ( ; first; first = first->next) {
4680 if (!first->schema && !strcmp(LYD_NAME(first), name)) {
4681 break;
4682 }
4683 }
4684
4685 if (match) {
4686 *match = (struct lyd_node *)first;
4687 }
4688 return first ? LY_SUCCESS : LY_ENOTFOUND;
4689}
4690
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01004691LIBYANG_API_DEF LY_ERR
Michal Vaskoddd76592022-01-17 13:34:48 +01004692lyd_find_xpath4(const struct lyd_node *ctx_node, const struct lyd_node *tree, const char *xpath, LY_VALUE_FORMAT format,
4693 void *prefix_data, const struct lyxp_var *vars, struct ly_set **set)
Michal Vaskoccc02342020-05-21 10:09:21 +02004694{
4695 LY_ERR ret = LY_SUCCESS;
Michal Vasko8bd9cc72021-07-16 14:06:10 +02004696 struct lyxp_set xp_set = {0};
Radek Krejcif03a9e22020-09-18 20:09:31 +02004697 struct lyxp_expr *exp = NULL;
Michal Vaskoccc02342020-05-21 10:09:21 +02004698 uint32_t i;
4699
Michal Vaskoddd76592022-01-17 13:34:48 +01004700 LY_CHECK_ARG_RET(NULL, tree, xpath, format, set, LY_EINVAL);
Michal Vaskoccc02342020-05-21 10:09:21 +02004701
Michal Vasko37c69432021-04-12 14:48:24 +02004702 *set = NULL;
Michal Vaskoccc02342020-05-21 10:09:21 +02004703
Michal Vaskoddd76592022-01-17 13:34:48 +01004704 /* parse expression */
Michal Vaskoe3716b32021-12-13 16:58:25 +01004705 ret = lyxp_expr_parse((struct ly_ctx *)LYD_CTX(tree), xpath, 0, 1, &exp);
Radek Krejcif03a9e22020-09-18 20:09:31 +02004706 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskoccc02342020-05-21 10:09:21 +02004707
4708 /* evaluate expression */
Michal Vaskoddd76592022-01-17 13:34:48 +01004709 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 +02004710 LY_CHECK_GOTO(ret, cleanup);
4711
4712 /* allocate return set */
Radek Krejciba03a5a2020-08-27 14:40:41 +02004713 ret = ly_set_new(set);
4714 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskoccc02342020-05-21 10:09:21 +02004715
4716 /* transform into ly_set */
4717 if (xp_set.type == LYXP_SET_NODE_SET) {
4718 /* allocate memory for all the elements once (even though not all items must be elements but most likely will be) */
4719 (*set)->objs = malloc(xp_set.used * sizeof *(*set)->objs);
Michal Vaskoe3716b32021-12-13 16:58:25 +01004720 LY_CHECK_ERR_GOTO(!(*set)->objs, LOGMEM(LYD_CTX(tree)); ret = LY_EMEM, cleanup);
Michal Vaskoccc02342020-05-21 10:09:21 +02004721 (*set)->size = xp_set.used;
4722
4723 for (i = 0; i < xp_set.used; ++i) {
4724 if (xp_set.val.nodes[i].type == LYXP_NODE_ELEM) {
Radek Krejci3d92e442020-10-12 12:48:13 +02004725 ret = ly_set_add(*set, xp_set.val.nodes[i].node, 1, NULL);
Radek Krejciba03a5a2020-08-27 14:40:41 +02004726 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskoccc02342020-05-21 10:09:21 +02004727 }
4728 }
4729 }
4730
4731cleanup:
Michal Vasko0691d522020-05-21 13:21:47 +02004732 lyxp_set_free_content(&xp_set);
Michal Vaskoe3716b32021-12-13 16:58:25 +01004733 lyxp_expr_free((struct ly_ctx *)LYD_CTX(tree), exp);
Radek Krejci8f45abc2020-11-26 12:15:13 +01004734 if (ret) {
4735 ly_set_free(*set, NULL);
4736 *set = NULL;
4737 }
Michal Vaskoccc02342020-05-21 10:09:21 +02004738 return ret;
4739}
Radek Krejcica989142020-11-05 11:32:22 +01004740
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01004741LIBYANG_API_DEF LY_ERR
Michal Vaskoddd76592022-01-17 13:34:48 +01004742lyd_find_xpath3(const struct lyd_node *ctx_node, const struct lyd_node *tree, const char *xpath,
4743 const struct lyxp_var *vars, struct ly_set **set)
4744{
4745 LY_CHECK_ARG_RET(NULL, tree, xpath, set, LY_EINVAL);
4746
4747 return lyd_find_xpath4(ctx_node, tree, xpath, LY_VALUE_JSON, NULL, vars, set);
4748}
4749
4750LIBYANG_API_DEF LY_ERR
Michal Vaskoe3716b32021-12-13 16:58:25 +01004751lyd_find_xpath2(const struct lyd_node *ctx_node, const char *xpath, const struct lyxp_var *vars, struct ly_set **set)
4752{
4753 LY_CHECK_ARG_RET(NULL, ctx_node, xpath, set, LY_EINVAL);
4754
Michal Vaskoddd76592022-01-17 13:34:48 +01004755 return lyd_find_xpath4(ctx_node, ctx_node, xpath, LY_VALUE_JSON, NULL, vars, set);
Michal Vaskoe3716b32021-12-13 16:58:25 +01004756}
4757
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01004758LIBYANG_API_DEF LY_ERR
aPiecekfba75362021-10-07 12:39:48 +02004759lyd_find_xpath(const struct lyd_node *ctx_node, const char *xpath, struct ly_set **set)
4760{
Michal Vaskoe3716b32021-12-13 16:58:25 +01004761 LY_CHECK_ARG_RET(NULL, ctx_node, xpath, set, LY_EINVAL);
4762
Michal Vaskoddd76592022-01-17 13:34:48 +01004763 return lyd_find_xpath4(ctx_node, ctx_node, xpath, LY_VALUE_JSON, NULL, NULL, set);
aPiecekfba75362021-10-07 12:39:48 +02004764}
4765
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01004766LIBYANG_API_DEF LY_ERR
aPiecekfba75362021-10-07 12:39:48 +02004767lyd_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 +02004768{
4769 LY_ERR ret = LY_SUCCESS;
Michal Vasko8bd9cc72021-07-16 14:06:10 +02004770 struct lyxp_set xp_set = {0};
Michal Vaskoc9eb3ca2021-07-16 10:20:37 +02004771 struct lyxp_expr *exp = NULL;
4772
4773 LY_CHECK_ARG_RET(NULL, ctx_node, xpath, result, LY_EINVAL);
4774
4775 /* compile expression */
4776 ret = lyxp_expr_parse((struct ly_ctx *)LYD_CTX(ctx_node), xpath, 0, 1, &exp);
4777 LY_CHECK_GOTO(ret, cleanup);
4778
4779 /* evaluate expression */
aPiecekfba75362021-10-07 12:39:48 +02004780 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 +02004781 LY_CHECK_GOTO(ret, cleanup);
4782
4783 /* transform into boolean */
4784 ret = lyxp_set_cast(&xp_set, LYXP_SET_BOOLEAN);
4785 LY_CHECK_GOTO(ret, cleanup);
4786
4787 /* set result */
4788 *result = xp_set.val.bln;
4789
4790cleanup:
4791 lyxp_set_free_content(&xp_set);
4792 lyxp_expr_free((struct ly_ctx *)LYD_CTX(ctx_node), exp);
4793 return ret;
4794}
4795
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01004796LIBYANG_API_DEF LY_ERR
aPiecekfba75362021-10-07 12:39:48 +02004797lyd_eval_xpath(const struct lyd_node *ctx_node, const char *xpath, ly_bool *result)
4798{
4799 return lyd_eval_xpath2(ctx_node, xpath, NULL, result);
4800}
4801
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01004802LIBYANG_API_DEF LY_ERR
Michal Vasko3e1f6552021-01-14 09:27:55 +01004803lyd_find_path(const struct lyd_node *ctx_node, const char *path, ly_bool output, struct lyd_node **match)
4804{
4805 LY_ERR ret = LY_SUCCESS;
4806 struct lyxp_expr *expr = NULL;
4807 struct ly_path *lypath = NULL;
4808
4809 LY_CHECK_ARG_RET(NULL, ctx_node, ctx_node->schema, path, LY_EINVAL);
4810
4811 /* parse the path */
Michal Vaskoed725d72021-06-23 12:03:45 +02004812 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 +01004813 LY_PATH_PREFIX_OPTIONAL, LY_PATH_PRED_SIMPLE, &expr);
4814 LY_CHECK_GOTO(ret, cleanup);
4815
4816 /* compile the path */
Michal Vaskoed725d72021-06-23 12:03:45 +02004817 ret = ly_path_compile(LYD_CTX(ctx_node), NULL, ctx_node->schema, NULL, expr,
Michal Vasko0884d212021-10-14 09:21:46 +02004818 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 +01004819 LY_CHECK_GOTO(ret, cleanup);
4820
4821 /* evaluate the path */
4822 ret = ly_path_eval_partial(lypath, ctx_node, NULL, match);
4823
4824cleanup:
4825 lyxp_expr_free(LYD_CTX(ctx_node), expr);
4826 ly_path_free(LYD_CTX(ctx_node), lypath);
4827 return ret;
4828}
4829
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01004830LIBYANG_API_DEF LY_ERR
Michal Vaskobb22b182021-06-14 08:14:21 +02004831lyd_find_target(const struct ly_path *path, const struct lyd_node *tree, struct lyd_node **match)
4832{
4833 LY_ERR ret;
4834 struct lyd_node *m;
4835
4836 LY_CHECK_ARG_RET(NULL, path, LY_EINVAL);
4837
4838 ret = ly_path_eval(path, tree, &m);
4839 if (ret) {
4840 if (match) {
4841 *match = NULL;
4842 }
4843 return LY_ENOTFOUND;
4844 }
4845
4846 if (match) {
4847 *match = m;
4848 }
4849 return LY_SUCCESS;
4850}
4851
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01004852LIBYANG_API_DEF uint32_t
Radek Krejcica989142020-11-05 11:32:22 +01004853lyd_list_pos(const struct lyd_node *instance)
4854{
4855 const struct lyd_node *iter = NULL;
4856 uint32_t pos = 0;
4857
4858 if (!instance || !(instance->schema->nodetype & (LYS_LIST | LYS_LEAFLIST))) {
4859 return 0;
4860 }
4861
4862 /* data instances are ordered, so we can stop when we found instance of other schema node */
4863 for (iter = instance; iter->schema == instance->schema; iter = iter->prev) {
Radek Krejcibb27df32020-11-13 15:39:16 +01004864 if (pos && (iter->next == NULL)) {
Radek Krejcica989142020-11-05 11:32:22 +01004865 /* overrun to the end of the siblings list */
4866 break;
4867 }
4868 ++pos;
4869 }
4870
4871 return pos;
4872}
Radek Krejci4233f9b2020-11-05 12:38:35 +01004873
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01004874LIBYANG_API_DEF struct lyd_node *
Radek Krejci4233f9b2020-11-05 12:38:35 +01004875lyd_first_sibling(const struct lyd_node *node)
4876{
4877 struct lyd_node *start;
4878
4879 if (!node) {
4880 return NULL;
4881 }
4882
4883 /* get the first sibling */
4884 if (node->parent) {
4885 start = node->parent->child;
4886 } else {
Radek Krejcibb27df32020-11-13 15:39:16 +01004887 for (start = (struct lyd_node *)node; start->prev->next; start = start->prev) {}
Radek Krejci4233f9b2020-11-05 12:38:35 +01004888 }
4889
4890 return start;
4891}