blob: a5af00e59447b5627cdab19ef70794f9c216eaa5 [file] [log] [blame]
Radek Krejcie7b95092019-05-15 11:03:07 +02001/**
Michal Vaskob1b5c262020-03-05 14:29:47 +01002 * @file tree_data.c
Radek Krejcie7b95092019-05-15 11:03:07 +02003 * @author Radek Krejci <rkrejci@cesnet.cz>
Michal Vasko6cd9b6b2020-06-22 10:05:22 +02004 * @brief Data tree functions
Radek Krejcie7b95092019-05-15 11:03:07 +02005 *
Michal Vasko6cd9b6b2020-06-22 10:05:22 +02006 * Copyright (c) 2015 - 2020 CESNET, z.s.p.o.
Radek Krejcie7b95092019-05-15 11:03:07 +02007 *
8 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * https://opensource.org/licenses/BSD-3-Clause
13 */
14
Radek Krejci535ea9f2020-05-29 16:01:05 +020015#define _GNU_SOURCE
16
17#include "tree_data.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020018
Radek Krejci084289f2019-07-09 17:35:30 +020019#include <assert.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020020#include <ctype.h>
Radek Krejci47fab892020-11-05 17:02:41 +010021#include <inttypes.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020022#include <stdarg.h>
23#include <stdint.h>
Radek Krejci535ea9f2020-05-29 16:01:05 +020024#include <stdio.h>
25#include <stdlib.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020026#include <string.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020027
Radek Krejci535ea9f2020-05-29 16:01:05 +020028#include "common.h"
Michal Vasko5aa44c02020-06-29 11:47:02 +020029#include "compat.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020030#include "context.h"
31#include "dict.h"
Michal Vaskoa6669ba2020-08-06 16:14:26 +020032#include "diff.h"
Michal Vasko90932a92020-02-12 14:33:03 +010033#include "hash_table.h"
Radek Krejci47fab892020-11-05 17:02:41 +010034#include "in.h"
Michal Vaskoafac7822020-10-20 14:22:26 +020035#include "in_internal.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020036#include "log.h"
Radek Krejci7931b192020-06-25 17:05:03 +020037#include "parser_data.h"
38#include "parser_internal.h"
Michal Vasko004d3152020-06-11 19:59:22 +020039#include "path.h"
Radek Krejci0aa1f702021-04-01 16:16:19 +020040#include "plugins.h"
Radek Krejcif1ca0ac2021-04-12 16:00:06 +020041#include "plugins_exts/metadata.h"
Radek Krejci3e6632f2021-03-22 22:08:21 +010042#include "plugins_internal.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020043#include "plugins_types.h"
44#include "set.h"
45#include "tree.h"
46#include "tree_data_internal.h"
Radek Krejci859a15a2021-03-05 20:56:59 +010047#include "tree_edit.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020048#include "tree_schema.h"
49#include "tree_schema_internal.h"
Michal Vaskoa6669ba2020-08-06 16:14:26 +020050#include "validation.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020051#include "xml.h"
52#include "xpath.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020053
Michal Vaskob104f112020-07-17 09:54:54 +020054static LY_ERR lyd_find_sibling_schema(const struct lyd_node *siblings, const struct lysc_node *schema,
Radek Krejci0f969882020-08-21 16:56:47 +020055 struct lyd_node **match);
Michal Vaskob104f112020-07-17 09:54:54 +020056
Radek Krejci084289f2019-07-09 17:35:30 +020057LY_ERR
Radek Krejcif9943642021-04-26 10:18:21 +020058lyd_value_store(const struct ly_ctx *ctx, struct lyd_value *val, const struct lysc_type *type, const void *value,
Radek Krejci8df109d2021-04-23 12:19:08 +020059 size_t value_len, ly_bool *dynamic, LY_VALUE_FORMAT format, void *prefix_data, uint32_t hints,
Radek Krejci2efc45b2020-12-22 16:25:44 +010060 const struct lysc_node *ctx_node, ly_bool *incomplete)
Radek Krejci084289f2019-07-09 17:35:30 +020061{
Michal Vaskofeca4fb2020-10-05 08:58:40 +020062 LY_ERR ret;
Radek Krejci084289f2019-07-09 17:35:30 +020063 struct ly_err_item *err = NULL;
Radek Krejci0b013302021-03-29 15:22:32 +020064 uint32_t options = (dynamic && *dynamic ? LYPLG_TYPE_STORE_DYNAMIC : 0);
Radek Krejci084289f2019-07-09 17:35:30 +020065
Michal Vasko62ed8652021-04-28 09:38:37 +020066 if (!value) {
67 value = "";
68 }
Michal Vaskofeca4fb2020-10-05 08:58:40 +020069 if (incomplete) {
70 *incomplete = 0;
Radek Krejci084289f2019-07-09 17:35:30 +020071 }
72
Michal Vasko405cc9e2020-12-01 12:01:27 +010073 ret = type->plugin->store(ctx, type, value, value_len, options, format, prefix_data, hints, ctx_node, val, NULL, &err);
Radek Iša96b2de12021-02-16 10:32:57 +010074 if (dynamic) {
75 *dynamic = 0;
76 }
77
Michal Vasko90932a92020-02-12 14:33:03 +010078 if (ret == LY_EINCOMPLETE) {
Michal Vaskofeca4fb2020-10-05 08:58:40 +020079 if (incomplete) {
80 *incomplete = 1;
81 }
82 } else if (ret) {
83 if (err) {
aPiecek4204b612021-05-03 09:06:16 +020084 LOGVAL_ERRITEM(ctx, err);
Michal Vaskofeca4fb2020-10-05 08:58:40 +020085 ly_err_free(err);
86 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +010087 LOGVAL(ctx, LYVE_OTHER, "Storing value \"%.*s\" failed.", (int)value_len, value);
Michal Vaskofeca4fb2020-10-05 08:58:40 +020088 }
89 return ret;
Michal Vasko90932a92020-02-12 14:33:03 +010090 }
91
Michal Vaskofeca4fb2020-10-05 08:58:40 +020092 return LY_SUCCESS;
Michal Vasko90932a92020-02-12 14:33:03 +010093}
94
Radek Krejci38d85362019-09-05 16:26:38 +020095LY_ERR
Michal Vaskofeca4fb2020-10-05 08:58:40 +020096lyd_value_validate_incomplete(const struct ly_ctx *ctx, const struct lysc_type *type, struct lyd_value *val,
Radek Krejci2efc45b2020-12-22 16:25:44 +010097 const struct lyd_node *ctx_node, const struct lyd_node *tree)
Radek Krejci38d85362019-09-05 16:26:38 +020098{
Michal Vaskofeca4fb2020-10-05 08:58:40 +020099 LY_ERR ret;
Radek Krejci38d85362019-09-05 16:26:38 +0200100 struct ly_err_item *err = NULL;
Radek Krejci38d85362019-09-05 16:26:38 +0200101
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200102 assert(type->plugin->validate);
Michal Vasko8d544252020-03-02 10:19:52 +0100103
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200104 ret = type->plugin->validate(ctx, type, ctx_node, tree, val, &err);
105 if (ret) {
Radek Krejci38d85362019-09-05 16:26:38 +0200106 if (err) {
aPiecek4204b612021-05-03 09:06:16 +0200107 LOGVAL_ERRITEM(ctx, err);
Radek Krejci38d85362019-09-05 16:26:38 +0200108 ly_err_free(err);
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200109 } else {
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200110 LOGVAL(ctx, LYVE_OTHER, "Resolving value \"%s\" failed.", type->plugin->print(ctx, val, LY_VALUE_CANON,
111 NULL, NULL, NULL));
Radek Krejci38d85362019-09-05 16:26:38 +0200112 }
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200113 return ret;
Radek Krejci38d85362019-09-05 16:26:38 +0200114 }
115
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200116 return LY_SUCCESS;
Radek Krejci38d85362019-09-05 16:26:38 +0200117}
118
Michal Vaskof937cfe2020-08-03 16:07:12 +0200119LY_ERR
Michal Vaskoaebbce02021-04-06 09:23:37 +0200120lys_value_validate(const struct ly_ctx *ctx, const struct lysc_node *node, const char *value, size_t value_len,
Radek Krejci8df109d2021-04-23 12:19:08 +0200121 LY_VALUE_FORMAT format, void *prefix_data)
Radek Krejci084289f2019-07-09 17:35:30 +0200122{
123 LY_ERR rc = LY_SUCCESS;
124 struct ly_err_item *err = NULL;
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200125 struct lyd_value storage;
Radek Krejci084289f2019-07-09 17:35:30 +0200126 struct lysc_type *type;
Radek Krejci084289f2019-07-09 17:35:30 +0200127
128 LY_CHECK_ARG_RET(ctx, node, value, LY_EINVAL);
129
130 if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
131 LOGARG(ctx, node);
132 return LY_EINVAL;
133 }
134
Michal Vasko22df3f02020-08-24 13:29:22 +0200135 type = ((struct lysc_node_leaf *)node)->type;
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200136 rc = type->plugin->store(ctx ? ctx : node->module->ctx, type, value, value_len, 0, format, prefix_data,
Michal Vasko405cc9e2020-12-01 12:01:27 +0100137 LYD_HINT_SCHEMA, node, &storage, NULL, &err);
Radek Krejci73dead22019-07-11 16:46:16 +0200138 if (rc == LY_EINCOMPLETE) {
139 /* actually success since we do not provide the context tree and call validation with
140 * LY_TYPE_OPTS_INCOMPLETE_DATA */
141 rc = LY_SUCCESS;
142 } else if (rc && err) {
143 if (ctx) {
144 /* log only in case the ctx was provided as input parameter */
Radek Krejciddace2c2021-01-08 11:30:56 +0100145 LOG_LOCSET(NULL, NULL, err->path, NULL);
aPiecek4204b612021-05-03 09:06:16 +0200146 LOGVAL_ERRITEM(ctx, err);
Radek Krejciddace2c2021-01-08 11:30:56 +0100147 LOG_LOCBACK(0, 0, 1, 0);
Radek Krejci084289f2019-07-09 17:35:30 +0200148 }
Radek Krejci73dead22019-07-11 16:46:16 +0200149 ly_err_free(err);
Radek Krejci084289f2019-07-09 17:35:30 +0200150 }
151
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200152 if (!rc) {
153 type->plugin->free(ctx ? ctx : node->module->ctx, &storage);
154 }
Radek Krejci084289f2019-07-09 17:35:30 +0200155 return rc;
156}
157
158API 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
233API 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
Radek Krejci19611252020-10-04 13:54:53 +0200259API ly_bool
260lyd_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
267 assert(node->schema->nodetype & LYD_NODE_TERM);
268 term = (const struct lyd_node_term *)node;
269
270 if (node->schema->nodetype == LYS_LEAF) {
271 leaf = (const struct lysc_node_leaf *)node->schema;
272 if (!leaf->dflt) {
273 return 0;
274 }
275
276 /* compare with the default value */
277 if (leaf->type->plugin->compare(&term->value, leaf->dflt)) {
278 return 0;
279 }
280 } else {
281 llist = (const struct lysc_node_leaflist *)node->schema;
282 if (!llist->dflts) {
283 return 0;
284 }
285
286 LY_ARRAY_FOR(llist->dflts, u) {
287 /* compare with each possible default value */
288 if (llist->type->plugin->compare(&term->value, llist->dflts[u])) {
289 return 0;
290 }
291 }
292 }
293
294 return 1;
295}
296
Radek Krejci7931b192020-06-25 17:05:03 +0200297static LYD_FORMAT
Michal Vasko63f3d842020-07-08 10:10:14 +0200298lyd_parse_get_format(const struct ly_in *in, LYD_FORMAT format)
Radek Krejcie7b95092019-05-15 11:03:07 +0200299{
Michal Vasko69730152020-10-09 16:30:07 +0200300 if (!format && (in->type == LY_IN_FILEPATH)) {
Radek Krejcie7b95092019-05-15 11:03:07 +0200301 /* unknown format - try to detect it from filename's suffix */
Radek Krejci7931b192020-06-25 17:05:03 +0200302 const char *path = in->method.fpath.filepath;
303 size_t len = strlen(path);
Radek Krejcie7b95092019-05-15 11:03:07 +0200304
305 /* ignore trailing whitespaces */
Michal Vaskod989ba02020-08-24 10:59:24 +0200306 for ( ; len > 0 && isspace(path[len - 1]); len--) {}
Radek Krejcie7b95092019-05-15 11:03:07 +0200307
Radek Krejcif13b87b2020-12-01 22:02:17 +0100308 if ((len >= LY_XML_SUFFIX_LEN + 1) &&
309 !strncmp(&path[len - LY_XML_SUFFIX_LEN], LY_XML_SUFFIX, LY_XML_SUFFIX_LEN)) {
Radek Krejcie7b95092019-05-15 11:03:07 +0200310 format = LYD_XML;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100311 } else if ((len >= LY_JSON_SUFFIX_LEN + 1) &&
312 !strncmp(&path[len - LY_JSON_SUFFIX_LEN], LY_JSON_SUFFIX, LY_JSON_SUFFIX_LEN)) {
Radek Krejcie7b95092019-05-15 11:03:07 +0200313 format = LYD_JSON;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100314 } else if ((len >= LY_LYB_SUFFIX_LEN + 1) &&
315 !strncmp(&path[len - LY_LYB_SUFFIX_LEN], LY_LYB_SUFFIX, LY_LYB_SUFFIX_LEN)) {
Radek Krejcie7b95092019-05-15 11:03:07 +0200316 format = LYD_LYB;
Radek Krejci7931b192020-06-25 17:05:03 +0200317 } /* else still unknown */
Radek Krejcie7b95092019-05-15 11:03:07 +0200318 }
319
Radek Krejci7931b192020-06-25 17:05:03 +0200320 return format;
321}
Radek Krejcie7b95092019-05-15 11:03:07 +0200322
Michal Vaskoe0665742021-02-11 11:08:44 +0100323/**
324 * @brief Parse YANG data into a data tree.
325 *
326 * @param[in] ctx libyang context.
Radek Krejcif16e2542021-02-17 15:39:23 +0100327 * @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 +0100328 * @param[in] parent Parent to connect the parsed nodes to, if any.
329 * @param[in,out] first_p Pointer to the first top-level parsed node, used only if @p parent is NULL.
330 * @param[in] in Input handle to read the input from.
331 * @param[in] format Expected format of the data in @p in.
332 * @param[in] parse_opts Options for parser.
333 * @param[in] val_opts Options for validation.
334 * @param[out] op Optional pointer to the parsed operation, if any.
335 * @return LY_ERR value.
336 */
337static LY_ERR
Radek Krejcif16e2542021-02-17 15:39:23 +0100338lyd_parse(const struct ly_ctx *ctx, const struct lysc_ext_instance *ext, struct lyd_node *parent, struct lyd_node **first_p,
339 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 +0200340{
Michal Vaskoe0665742021-02-11 11:08:44 +0100341 LY_ERR rc = LY_SUCCESS;
Radek Krejci1798aae2020-07-14 13:26:06 +0200342 struct lyd_ctx *lydctx = NULL;
Michal Vaskoe0665742021-02-11 11:08:44 +0100343 struct ly_set parsed = {0};
344 struct lyd_node *first;
345 uint32_t i;
Radek Krejci1798aae2020-07-14 13:26:06 +0200346
Michal Vaskoe0665742021-02-11 11:08:44 +0100347 assert(ctx && (parent || first_p));
Radek Krejci7931b192020-06-25 17:05:03 +0200348
349 format = lyd_parse_get_format(in, format);
Michal Vaskoe0665742021-02-11 11:08:44 +0100350 if (first_p) {
351 *first_p = NULL;
352 }
Radek Krejci1798aae2020-07-14 13:26:06 +0200353
Michal Vasko63f3d842020-07-08 10:10:14 +0200354 /* remember input position */
355 in->func_start = in->current;
Radek Krejci7931b192020-06-25 17:05:03 +0200356
Michal Vaskoe0665742021-02-11 11:08:44 +0100357 /* parse the data */
Radek Krejci7931b192020-06-25 17:05:03 +0200358 switch (format) {
359 case LYD_XML:
Radek Krejcif16e2542021-02-17 15:39:23 +0100360 rc = lyd_parse_xml(ctx, ext, parent, first_p, in, parse_opts, val_opts, LYD_TYPE_DATA_YANG, NULL, &parsed, &lydctx);
Radek Krejci1798aae2020-07-14 13:26:06 +0200361 break;
Radek Krejci7931b192020-06-25 17:05:03 +0200362 case LYD_JSON:
Radek Krejcif16e2542021-02-17 15:39:23 +0100363 rc = lyd_parse_json(ctx, ext, parent, first_p, in, parse_opts, val_opts, LYD_TYPE_DATA_YANG, &parsed, &lydctx);
Radek Krejci1798aae2020-07-14 13:26:06 +0200364 break;
Radek Krejci7931b192020-06-25 17:05:03 +0200365 case LYD_LYB:
Radek Krejcif16e2542021-02-17 15:39:23 +0100366 rc = lyd_parse_lyb(ctx, ext, parent, first_p, in, parse_opts, val_opts, LYD_TYPE_DATA_YANG, &parsed, &lydctx);
Radek Krejci1798aae2020-07-14 13:26:06 +0200367 break;
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200368 case LYD_UNKNOWN:
Michal Vaskod74978f2021-02-12 11:59:36 +0100369 LOGARG(ctx, format);
370 rc = LY_EINVAL;
Michal Vaskoe0665742021-02-11 11:08:44 +0100371 break;
372 }
373 LY_CHECK_GOTO(rc, cleanup);
374
375 if (parent) {
376 /* get first top-level sibling */
377 for (first = parent; first->parent; first = lyd_parent(first)) {}
378 first = lyd_first_sibling(first);
379 first_p = &first;
Radek Krejci7931b192020-06-25 17:05:03 +0200380 }
381
Michal Vaskoe0665742021-02-11 11:08:44 +0100382 if (!(parse_opts & LYD_PARSE_ONLY)) {
383 /* validate data */
Radek Krejci4f2e3e52021-03-30 14:20:28 +0200384 rc = lyd_validate(first_p, NULL, ctx, val_opts, 0, &lydctx->node_when, &lydctx->node_exts, &lydctx->node_types,
Michal Vaskoe0665742021-02-11 11:08:44 +0100385 &lydctx->meta_types, NULL);
386 LY_CHECK_GOTO(rc, cleanup);
387 }
Radek Krejci7931b192020-06-25 17:05:03 +0200388
Michal Vaskoe0665742021-02-11 11:08:44 +0100389 /* set the operation node */
390 if (op) {
391 *op = lydctx->op_node;
Radek Krejci1798aae2020-07-14 13:26:06 +0200392 }
393
394cleanup:
Michal Vaskoe0665742021-02-11 11:08:44 +0100395 if (lydctx) {
396 lydctx->free(lydctx);
Radek Krejci1798aae2020-07-14 13:26:06 +0200397 }
Michal Vaskoe0665742021-02-11 11:08:44 +0100398 if (rc) {
399 if (parent) {
400 /* free all the parsed subtrees */
401 for (i = 0; i < parsed.count; ++i) {
402 lyd_free_tree(parsed.dnodes[i]);
403 }
404 } else {
405 /* free everything */
406 lyd_free_all(*first_p);
407 *first_p = NULL;
408 }
409 }
410 ly_set_erase(&parsed, NULL);
411 return rc;
Radek Krejci7931b192020-06-25 17:05:03 +0200412}
413
414API LY_ERR
Radek Krejcif16e2542021-02-17 15:39:23 +0100415lyd_parse_ext_data(const struct lysc_ext_instance *ext, struct lyd_node *parent, struct ly_in *in, LYD_FORMAT format,
416 uint32_t parse_options, uint32_t validate_options, struct lyd_node **tree)
417{
418 const struct ly_ctx *ctx = ext ? ext->module->ctx : NULL;
419
420 LY_CHECK_ARG_RET(ctx, ext, in, parent || tree, LY_EINVAL);
421 LY_CHECK_ARG_RET(ctx, !(parse_options & ~LYD_PARSE_OPTS_MASK), LY_EINVAL);
422 LY_CHECK_ARG_RET(ctx, !(validate_options & ~LYD_VALIDATE_OPTS_MASK), LY_EINVAL);
423
424 return lyd_parse(ctx, ext, parent, tree, in, format, parse_options, validate_options, NULL);
425}
426
427API LY_ERR
Michal Vaskoe0665742021-02-11 11:08:44 +0100428lyd_parse_data(const struct ly_ctx *ctx, struct lyd_node *parent, struct ly_in *in, LYD_FORMAT format,
429 uint32_t parse_options, uint32_t validate_options, struct lyd_node **tree)
430{
431 LY_CHECK_ARG_RET(ctx, ctx, in, parent || tree, LY_EINVAL);
432 LY_CHECK_ARG_RET(ctx, !(parse_options & ~LYD_PARSE_OPTS_MASK), LY_EINVAL);
433 LY_CHECK_ARG_RET(ctx, !(validate_options & ~LYD_VALIDATE_OPTS_MASK), LY_EINVAL);
434
Radek Krejcif16e2542021-02-17 15:39:23 +0100435 return lyd_parse(ctx, NULL, parent, tree, in, format, parse_options, validate_options, NULL);
Michal Vaskoe0665742021-02-11 11:08:44 +0100436}
437
438API LY_ERR
439lyd_parse_data_mem(const struct ly_ctx *ctx, const char *data, LYD_FORMAT format, uint32_t parse_options,
440 uint32_t validate_options, struct lyd_node **tree)
Radek Krejci7931b192020-06-25 17:05:03 +0200441{
442 LY_ERR ret;
443 struct ly_in *in;
444
445 LY_CHECK_RET(ly_in_new_memory(data, &in));
Michal Vaskoe0665742021-02-11 11:08:44 +0100446 ret = lyd_parse_data(ctx, NULL, in, format, parse_options, validate_options, tree);
Radek Krejci7931b192020-06-25 17:05:03 +0200447
448 ly_in_free(in, 0);
449 return ret;
450}
451
452API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200453lyd_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 +0200454 struct lyd_node **tree)
Radek Krejci7931b192020-06-25 17:05:03 +0200455{
456 LY_ERR ret;
457 struct ly_in *in;
458
459 LY_CHECK_RET(ly_in_new_fd(fd, &in));
Michal Vaskoe0665742021-02-11 11:08:44 +0100460 ret = lyd_parse_data(ctx, NULL, in, format, parse_options, validate_options, tree);
Radek Krejci7931b192020-06-25 17:05:03 +0200461
462 ly_in_free(in, 0);
463 return ret;
464}
465
466API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200467lyd_parse_data_path(const struct ly_ctx *ctx, const char *path, LYD_FORMAT format, uint32_t parse_options,
468 uint32_t validate_options, struct lyd_node **tree)
Radek Krejci7931b192020-06-25 17:05:03 +0200469{
470 LY_ERR ret;
471 struct ly_in *in;
472
473 LY_CHECK_RET(ly_in_new_filepath(path, 0, &in));
Michal Vaskoe0665742021-02-11 11:08:44 +0100474 ret = lyd_parse_data(ctx, NULL, in, format, parse_options, validate_options, tree);
Radek Krejci7931b192020-06-25 17:05:03 +0200475
476 ly_in_free(in, 0);
477 return ret;
478}
479
Radek Krejcif16e2542021-02-17 15:39:23 +0100480/**
481 * @brief Parse YANG data into an operation data tree, in case the extension instance is specified, keep the searching
482 * for schema nodes locked inside the extension instance.
483 *
484 * At least one of @p parent, @p tree, or @p op must always be set.
485 *
486 * Specific @p data_type values have different parameter meaning as follows:
487 * - ::LYD_TYPE_RPC_NETCONF:
488 * - @p parent - must be NULL, the whole RPC is expected;
489 * - @p format - must be ::LYD_XML, NETCONF supports only this format;
490 * - @p tree - must be provided, all the NETCONF-specific XML envelopes will be returned here as
491 * a separate opaque data tree, even if the function fails, this may be returned;
492 * - @p op - must be provided, the RPC/action data tree itself will be returned here, pointing to the operation;
493 *
494 * - ::LYD_TYPE_NOTIF_NETCONF:
495 * - @p parent - must be NULL, the whole notification is expected;
496 * - @p format - must be ::LYD_XML, NETCONF supports only this format;
497 * - @p tree - must be provided, all the NETCONF-specific XML envelopes will be returned here as
498 * a separate opaque data tree, even if the function fails, this may be returned;
499 * - @p op - must be provided, the notification data tree itself will be returned here, pointing to the operation;
500 *
501 * - ::LYD_TYPE_REPLY_NETCONF:
502 * - @p parent - must be set, pointing to the invoked RPC operation (RPC or action) node;
503 * - @p format - must be ::LYD_XML, NETCONF supports only this format;
504 * - @p tree - must be provided, all the NETCONF-specific XML envelopes will be returned here as
505 * a separate opaque data tree, even if the function fails, this may be returned;
506 * - @p op - must be NULL, the reply is appended to the RPC;
507 * Note that there are 3 kinds of NETCONF replies - ok, error, and data. Only data reply appends any nodes to the RPC.
508 *
509 * @param[in] ctx libyang context.
510 * @param[in] ext Extension instance providing the specific schema tree to match with the data being parsed.
511 * @param[in] parent Optional parent to connect the parsed nodes to.
512 * @param[in] in Input handle to read the input from.
513 * @param[in] format Expected format of the data in @p in.
514 * @param[in] data_type Expected operation to parse (@ref datatype).
515 * @param[out] tree Optional full parsed data tree. If @p parent is set, set to NULL.
516 * @param[out] op Optional parsed operation node.
517 * @return LY_ERR value.
518 * @return LY_ENOT if @p data_type is a NETCONF message and the root XML element is not the expected one.
519 */
520static LY_ERR
521lyd_parse_op_(const struct ly_ctx *ctx, const struct lysc_ext_instance *ext, struct lyd_node *parent,
522 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 +0200523{
Michal Vaskoe0665742021-02-11 11:08:44 +0100524 LY_ERR rc = LY_SUCCESS;
525 struct lyd_ctx *lydctx = NULL;
526 struct ly_set parsed = {0};
527 struct lyd_node *first = NULL, *envp = NULL;
528 uint32_t i, parse_opts, val_opts;
Radek Krejci7931b192020-06-25 17:05:03 +0200529
Michal Vasko27fb0262021-02-23 09:42:01 +0100530 if (!ctx) {
531 ctx = LYD_CTX(parent);
532 }
Radek Krejci1798aae2020-07-14 13:26:06 +0200533 if (tree) {
534 *tree = NULL;
535 }
536 if (op) {
537 *op = NULL;
538 }
539
Radek Krejci7931b192020-06-25 17:05:03 +0200540 format = lyd_parse_get_format(in, format);
Radek Krejci7931b192020-06-25 17:05:03 +0200541
Michal Vasko63f3d842020-07-08 10:10:14 +0200542 /* remember input position */
543 in->func_start = in->current;
544
Michal Vaskoe0665742021-02-11 11:08:44 +0100545 /* check params based on the data type */
Michal Vasko1e4c68e2021-02-18 15:03:01 +0100546 if ((data_type == LYD_TYPE_RPC_NETCONF) || (data_type == LYD_TYPE_NOTIF_NETCONF)) {
Michal Vaskoe0665742021-02-11 11:08:44 +0100547 LY_CHECK_ARG_RET(ctx, format == LYD_XML, !parent, tree, op, LY_EINVAL);
Michal Vasko1e4c68e2021-02-18 15:03:01 +0100548 } else if (data_type == LYD_TYPE_REPLY_NETCONF) {
549 LY_CHECK_ARG_RET(ctx, format == LYD_XML, parent, parent->schema->nodetype & (LYS_RPC | LYS_ACTION), tree, !op,
550 LY_EINVAL);
Michal Vaskoe0665742021-02-11 11:08:44 +0100551 }
552 parse_opts = LYD_PARSE_ONLY | LYD_PARSE_STRICT;
553 val_opts = 0;
554
555 /* parse the data */
Radek Krejci7931b192020-06-25 17:05:03 +0200556 switch (format) {
557 case LYD_XML:
Radek Krejcif16e2542021-02-17 15:39:23 +0100558 rc = lyd_parse_xml(ctx, ext, parent, &first, in, parse_opts, val_opts, data_type, &envp, &parsed, &lydctx);
Michal Vasko299d5d12021-02-16 16:36:37 +0100559 if (rc && envp) {
560 /* special situation when the envelopes were parsed successfully */
561 if (tree) {
562 *tree = envp;
563 }
564 ly_set_erase(&parsed, NULL);
565 return rc;
566 }
Michal Vaskoe0665742021-02-11 11:08:44 +0100567 break;
Radek Krejci7931b192020-06-25 17:05:03 +0200568 case LYD_JSON:
Radek Krejcif16e2542021-02-17 15:39:23 +0100569 rc = lyd_parse_json(ctx, ext, parent, &first, in, parse_opts, val_opts, data_type, &parsed, &lydctx);
Michal Vaskoe0665742021-02-11 11:08:44 +0100570 break;
Radek Krejci7931b192020-06-25 17:05:03 +0200571 case LYD_LYB:
Radek Krejcif16e2542021-02-17 15:39:23 +0100572 rc = lyd_parse_lyb(ctx, ext, parent, &first, in, parse_opts, val_opts, data_type, &parsed, &lydctx);
Michal Vaskoe0665742021-02-11 11:08:44 +0100573 break;
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200574 case LYD_UNKNOWN:
Michal Vaskod74978f2021-02-12 11:59:36 +0100575 LOGARG(ctx, format);
576 rc = LY_EINVAL;
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200577 break;
Radek Krejci7931b192020-06-25 17:05:03 +0200578 }
Michal Vaskoe0665742021-02-11 11:08:44 +0100579 LY_CHECK_GOTO(rc, cleanup);
Radek Krejci7931b192020-06-25 17:05:03 +0200580
Michal Vaskoe0665742021-02-11 11:08:44 +0100581 /* set out params correctly */
582 if (tree) {
583 if (envp) {
584 /* special out param meaning */
585 *tree = envp;
586 } else {
587 *tree = parent ? NULL : first;
588 }
589 }
590 if (op) {
591 *op = lydctx->op_node;
592 }
593
594cleanup:
595 if (lydctx) {
596 lydctx->free(lydctx);
597 }
598 if (rc) {
599 if (parent) {
600 /* free all the parsed subtrees */
601 for (i = 0; i < parsed.count; ++i) {
602 lyd_free_tree(parsed.dnodes[i]);
603 }
604 } else {
605 /* free everything (cannot occur in the current code, a safety) */
606 lyd_free_all(first);
607 if (tree) {
608 *tree = NULL;
609 }
610 if (op) {
611 *op = NULL;
612 }
613 }
614 }
615 ly_set_erase(&parsed, NULL);
616 return rc;
Radek Krejcie7b95092019-05-15 11:03:07 +0200617}
Radek Krejci084289f2019-07-09 17:35:30 +0200618
Radek Krejcif16e2542021-02-17 15:39:23 +0100619API LY_ERR
620lyd_parse_op(const struct ly_ctx *ctx, struct lyd_node *parent, struct ly_in *in, LYD_FORMAT format,
621 enum lyd_type data_type, struct lyd_node **tree, struct lyd_node **op)
622{
623 LY_CHECK_ARG_RET(ctx, ctx || parent, in, data_type, parent || tree || op, LY_EINVAL);
624
625 return lyd_parse_op_(ctx, NULL, parent, in, format, data_type, tree, op);
626}
627
628API LY_ERR
629lyd_parse_ext_op(const struct lysc_ext_instance *ext, 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 const struct ly_ctx *ctx = ext ? ext->module->ctx : NULL;
633
634 LY_CHECK_ARG_RET(ctx, ext, in, data_type, parent || tree || op, LY_EINVAL);
635
636 return lyd_parse_op_(ctx, ext, parent, in, format, data_type, tree, op);
637}
638
Michal Vasko90932a92020-02-12 14:33:03 +0100639LY_ERR
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200640lyd_create_term(const struct lysc_node *schema, const char *value, size_t value_len, ly_bool *dynamic,
Radek Krejci8df109d2021-04-23 12:19:08 +0200641 LY_VALUE_FORMAT format, void *prefix_data, uint32_t hints, ly_bool *incomplete, struct lyd_node **node)
Michal Vasko90932a92020-02-12 14:33:03 +0100642{
643 LY_ERR ret;
644 struct lyd_node_term *term;
645
Michal Vasko9b368d32020-02-14 13:53:31 +0100646 assert(schema->nodetype & LYD_NODE_TERM);
647
Michal Vasko90932a92020-02-12 14:33:03 +0100648 term = calloc(1, sizeof *term);
649 LY_CHECK_ERR_RET(!term, LOGMEM(schema->module->ctx), LY_EMEM);
650
651 term->schema = schema;
Michal Vasko9e685082021-01-29 14:49:09 +0100652 term->prev = &term->node;
Michal Vasko9b368d32020-02-14 13:53:31 +0100653 term->flags = LYD_NEW;
Michal Vasko90932a92020-02-12 14:33:03 +0100654
Radek Krejciddace2c2021-01-08 11:30:56 +0100655 LOG_LOCSET(schema, NULL, NULL, NULL);
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200656 ret = lyd_value_store(schema->module->ctx, &term->value, ((struct lysc_node_leaf *)term->schema)->type, value,
Radek Krejci2efc45b2020-12-22 16:25:44 +0100657 value_len, dynamic, format, prefix_data, hints, schema, incomplete);
Radek Krejciddace2c2021-01-08 11:30:56 +0100658 LOG_LOCBACK(1, 0, 0, 0);
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200659 LY_CHECK_ERR_RET(ret, free(term), ret);
Michal Vasko9e685082021-01-29 14:49:09 +0100660 lyd_hash(&term->node);
Michal Vasko9b368d32020-02-14 13:53:31 +0100661
Michal Vasko9e685082021-01-29 14:49:09 +0100662 *node = &term->node;
Michal Vasko9b368d32020-02-14 13:53:31 +0100663 return ret;
664}
665
666LY_ERR
667lyd_create_term2(const struct lysc_node *schema, const struct lyd_value *val, struct lyd_node **node)
668{
669 LY_ERR ret;
670 struct lyd_node_term *term;
671 struct lysc_type *type;
672
673 assert(schema->nodetype & LYD_NODE_TERM);
Radek Krejci224d4b42021-04-23 13:54:59 +0200674 assert(val && val->realtype);
Michal Vasko9b368d32020-02-14 13:53:31 +0100675
676 term = calloc(1, sizeof *term);
677 LY_CHECK_ERR_RET(!term, LOGMEM(schema->module->ctx), LY_EMEM);
678
679 term->schema = schema;
Michal Vasko9e685082021-01-29 14:49:09 +0100680 term->prev = &term->node;
Michal Vasko9b368d32020-02-14 13:53:31 +0100681 term->flags = LYD_NEW;
682
683 type = ((struct lysc_node_leaf *)schema)->type;
684 ret = type->plugin->duplicate(schema->module->ctx, val, &term->value);
685 if (ret) {
686 LOGERR(schema->module->ctx, ret, "Value duplication failed.");
687 free(term);
688 return ret;
689 }
Michal Vasko9e685082021-01-29 14:49:09 +0100690 lyd_hash(&term->node);
Michal Vasko90932a92020-02-12 14:33:03 +0100691
Michal Vasko9e685082021-01-29 14:49:09 +0100692 *node = &term->node;
Michal Vasko90932a92020-02-12 14:33:03 +0100693 return ret;
694}
695
696LY_ERR
697lyd_create_inner(const struct lysc_node *schema, struct lyd_node **node)
698{
699 struct lyd_node_inner *in;
700
Michal Vasko9b368d32020-02-14 13:53:31 +0100701 assert(schema->nodetype & LYD_NODE_INNER);
702
Michal Vasko90932a92020-02-12 14:33:03 +0100703 in = calloc(1, sizeof *in);
704 LY_CHECK_ERR_RET(!in, LOGMEM(schema->module->ctx), LY_EMEM);
705
706 in->schema = schema;
Michal Vasko9e685082021-01-29 14:49:09 +0100707 in->prev = &in->node;
Michal Vasko9b368d32020-02-14 13:53:31 +0100708 in->flags = LYD_NEW;
Michal Vasko3383be72020-11-03 17:15:31 +0100709 if ((schema->nodetype == LYS_CONTAINER) && !(schema->flags & LYS_PRESENCE)) {
710 in->flags |= LYD_DEFAULT;
711 }
Michal Vasko90932a92020-02-12 14:33:03 +0100712
Michal Vasko9b368d32020-02-14 13:53:31 +0100713 /* do not hash list with keys, we need them for the hash */
714 if ((schema->nodetype != LYS_LIST) || (schema->flags & LYS_KEYLESS)) {
Michal Vasko9e685082021-01-29 14:49:09 +0100715 lyd_hash(&in->node);
Michal Vasko9b368d32020-02-14 13:53:31 +0100716 }
Michal Vasko90932a92020-02-12 14:33:03 +0100717
Michal Vasko9e685082021-01-29 14:49:09 +0100718 *node = &in->node;
Michal Vasko90932a92020-02-12 14:33:03 +0100719 return LY_SUCCESS;
720}
721
Michal Vasko90932a92020-02-12 14:33:03 +0100722LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +0200723lyd_create_list(const struct lysc_node *schema, const struct ly_path_predicate *predicates, struct lyd_node **node)
Michal Vasko90932a92020-02-12 14:33:03 +0100724{
725 LY_ERR ret = LY_SUCCESS;
Michal Vasko90932a92020-02-12 14:33:03 +0100726 struct lyd_node *list = NULL, *key;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200727 LY_ARRAY_COUNT_TYPE u;
Michal Vasko90932a92020-02-12 14:33:03 +0100728
Michal Vasko004d3152020-06-11 19:59:22 +0200729 assert((schema->nodetype == LYS_LIST) && !(schema->flags & LYS_KEYLESS));
Michal Vasko90932a92020-02-12 14:33:03 +0100730
731 /* create list */
732 LY_CHECK_GOTO(ret = lyd_create_inner(schema, &list), cleanup);
733
Radek Krejciddace2c2021-01-08 11:30:56 +0100734 LOG_LOCSET(NULL, list, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100735
Michal Vasko90932a92020-02-12 14:33:03 +0100736 /* create and insert all the keys */
Michal Vasko004d3152020-06-11 19:59:22 +0200737 LY_ARRAY_FOR(predicates, u) {
738 LY_CHECK_GOTO(ret = lyd_create_term2(predicates[u].key, &predicates[u].value, &key), cleanup);
Michal Vasko6ee6f432021-07-16 09:49:14 +0200739 lyd_insert_node(list, NULL, key, 0);
Michal Vasko90932a92020-02-12 14:33:03 +0100740 }
741
Michal Vasko9b368d32020-02-14 13:53:31 +0100742 /* hash having all the keys */
743 lyd_hash(list);
744
Michal Vasko90932a92020-02-12 14:33:03 +0100745 /* success */
746 *node = list;
747 list = NULL;
748
749cleanup:
Radek Krejciddace2c2021-01-08 11:30:56 +0100750 LOG_LOCBACK(0, 1, 0, 0);
Michal Vasko90932a92020-02-12 14:33:03 +0100751 lyd_free_tree(list);
Michal Vasko004d3152020-06-11 19:59:22 +0200752 return ret;
753}
754
755static LY_ERR
756lyd_create_list2(const struct lysc_node *schema, const char *keys, size_t keys_len, struct lyd_node **node)
757{
758 LY_ERR ret = LY_SUCCESS;
759 struct lyxp_expr *expr = NULL;
760 uint16_t exp_idx = 0;
761 enum ly_path_pred_type pred_type = 0;
762 struct ly_path_predicate *predicates = NULL;
763
Radek Krejciddace2c2021-01-08 11:30:56 +0100764 LOG_LOCSET(schema, NULL, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100765
Michal Vasko004d3152020-06-11 19:59:22 +0200766 /* parse keys */
Michal Vasko6b26e742020-07-17 15:02:10 +0200767 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 +0200768 LY_PATH_PRED_KEYS, &expr), cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +0200769
770 /* compile them */
Radek Krejci8df109d2021-04-23 12:19:08 +0200771 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 +0200772 NULL, &predicates, &pred_type), cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +0200773
774 /* create the list node */
775 LY_CHECK_GOTO(ret = lyd_create_list(schema, predicates, node), cleanup);
776
777cleanup:
Radek Krejciddace2c2021-01-08 11:30:56 +0100778 LOG_LOCBACK(1, 0, 0, 0);
Michal Vasko004d3152020-06-11 19:59:22 +0200779 lyxp_expr_free(schema->module->ctx, expr);
Michal Vaskof7e16e22020-10-21 09:24:39 +0200780 ly_path_predicates_free(schema->module->ctx, pred_type, predicates);
Michal Vasko90932a92020-02-12 14:33:03 +0100781 return ret;
782}
783
784LY_ERR
Michal Vasko366a4a12020-12-04 16:23:57 +0100785lyd_create_any(const struct lysc_node *schema, const void *value, LYD_ANYDATA_VALUETYPE value_type, ly_bool use_value,
786 struct lyd_node **node)
Michal Vasko90932a92020-02-12 14:33:03 +0100787{
Michal Vasko366a4a12020-12-04 16:23:57 +0100788 LY_ERR ret;
Michal Vasko90932a92020-02-12 14:33:03 +0100789 struct lyd_node_any *any;
Michal Vasko366a4a12020-12-04 16:23:57 +0100790 union lyd_any_value any_val;
Michal Vasko90932a92020-02-12 14:33:03 +0100791
Michal Vasko9b368d32020-02-14 13:53:31 +0100792 assert(schema->nodetype & LYD_NODE_ANY);
793
Michal Vasko90932a92020-02-12 14:33:03 +0100794 any = calloc(1, sizeof *any);
795 LY_CHECK_ERR_RET(!any, LOGMEM(schema->module->ctx), LY_EMEM);
796
797 any->schema = schema;
Michal Vasko9e685082021-01-29 14:49:09 +0100798 any->prev = &any->node;
Michal Vasko9b368d32020-02-14 13:53:31 +0100799 any->flags = LYD_NEW;
Michal Vasko90932a92020-02-12 14:33:03 +0100800
Radek Krejci1798aae2020-07-14 13:26:06 +0200801 /* TODO: convert XML/JSON strings into a opaq data tree */
Michal Vasko366a4a12020-12-04 16:23:57 +0100802
803 if (use_value) {
804 any->value.str = value;
805 any->value_type = value_type;
806 } else {
807 any_val.str = value;
Michal Vasko9e685082021-01-29 14:49:09 +0100808 ret = lyd_any_copy_value(&any->node, &any_val, value_type);
Michal Vasko366a4a12020-12-04 16:23:57 +0100809 LY_CHECK_ERR_RET(ret, free(any), ret);
810 }
Michal Vasko9e685082021-01-29 14:49:09 +0100811 lyd_hash(&any->node);
Michal Vasko90932a92020-02-12 14:33:03 +0100812
Michal Vasko9e685082021-01-29 14:49:09 +0100813 *node = &any->node;
Michal Vasko90932a92020-02-12 14:33:03 +0100814 return LY_SUCCESS;
815}
816
Michal Vasko52927e22020-03-16 17:26:14 +0100817LY_ERR
Michal Vasko501af032020-11-11 20:27:44 +0100818lyd_create_opaq(const struct ly_ctx *ctx, const char *name, size_t name_len, const char *prefix, size_t pref_len,
819 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 +0200820 LY_VALUE_FORMAT format, void *val_prefix_data, uint32_t hints, struct lyd_node **node)
Michal Vasko52927e22020-03-16 17:26:14 +0100821{
Radek Krejci011e4aa2020-09-04 15:22:31 +0200822 LY_ERR ret = LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +0100823 struct lyd_node_opaq *opaq;
824
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200825 assert(ctx && name && name_len && format);
Michal Vasko52927e22020-03-16 17:26:14 +0100826
Michal Vasko4cd0a602021-05-31 11:11:58 +0200827 if (!value_len && (!dynamic || !*dynamic)) {
Michal Vasko52927e22020-03-16 17:26:14 +0100828 value = "";
829 }
830
831 opaq = calloc(1, sizeof *opaq);
Michal Vasko501af032020-11-11 20:27:44 +0100832 LY_CHECK_ERR_GOTO(!opaq, LOGMEM(ctx); ret = LY_EMEM, finish);
Michal Vasko52927e22020-03-16 17:26:14 +0100833
Michal Vasko9e685082021-01-29 14:49:09 +0100834 opaq->prev = &opaq->node;
Michal Vaskoad92b672020-11-12 13:11:31 +0100835 LY_CHECK_GOTO(ret = lydict_insert(ctx, name, name_len, &opaq->name.name), finish);
Radek Krejci011e4aa2020-09-04 15:22:31 +0200836
Michal Vasko52927e22020-03-16 17:26:14 +0100837 if (pref_len) {
Michal Vaskoad92b672020-11-12 13:11:31 +0100838 LY_CHECK_GOTO(ret = lydict_insert(ctx, prefix, pref_len, &opaq->name.prefix), finish);
Michal Vasko52927e22020-03-16 17:26:14 +0100839 }
Radek Krejci1798aae2020-07-14 13:26:06 +0200840 if (module_key_len) {
Michal Vaskoad92b672020-11-12 13:11:31 +0100841 LY_CHECK_GOTO(ret = lydict_insert(ctx, module_key, module_key_len, &opaq->name.module_ns), finish);
Radek Krejci1798aae2020-07-14 13:26:06 +0200842 }
Michal Vasko52927e22020-03-16 17:26:14 +0100843 if (dynamic && *dynamic) {
Radek Krejci011e4aa2020-09-04 15:22:31 +0200844 LY_CHECK_GOTO(ret = lydict_insert_zc(ctx, (char *)value, &opaq->value), finish);
Michal Vasko52927e22020-03-16 17:26:14 +0100845 *dynamic = 0;
846 } else {
Radek Krejci011e4aa2020-09-04 15:22:31 +0200847 LY_CHECK_GOTO(ret = lydict_insert(ctx, value, value_len, &opaq->value), finish);
Michal Vasko52927e22020-03-16 17:26:14 +0100848 }
Michal Vasko501af032020-11-11 20:27:44 +0100849
850 opaq->format = format;
851 opaq->val_prefix_data = val_prefix_data;
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200852 opaq->hints = hints;
Michal Vasko52927e22020-03-16 17:26:14 +0100853 opaq->ctx = ctx;
854
Radek Krejci011e4aa2020-09-04 15:22:31 +0200855finish:
856 if (ret) {
Michal Vasko9e685082021-01-29 14:49:09 +0100857 lyd_free_tree(&opaq->node);
Michal Vasko501af032020-11-11 20:27:44 +0100858 ly_free_prefix_data(format, val_prefix_data);
Radek Krejci011e4aa2020-09-04 15:22:31 +0200859 } else {
Michal Vasko9e685082021-01-29 14:49:09 +0100860 *node = &opaq->node;
Radek Krejci011e4aa2020-09-04 15:22:31 +0200861 }
862 return ret;
Michal Vasko52927e22020-03-16 17:26:14 +0100863}
864
Michal Vasko3a41dff2020-07-15 14:30:28 +0200865API LY_ERR
Michal Vasko65243892020-12-04 16:26:21 +0100866lyd_new_inner(struct lyd_node *parent, const struct lys_module *module, const char *name, ly_bool output,
867 struct lyd_node **node)
Michal Vasko013a8182020-03-03 10:46:53 +0100868{
869 struct lyd_node *ret = NULL;
870 const struct lysc_node *schema;
871 struct ly_ctx *ctx = parent ? parent->schema->module->ctx : (module ? module->ctx : NULL);
872
Michal Vasko6027eb92020-07-15 16:37:30 +0200873 LY_CHECK_ARG_RET(ctx, parent || module, parent || node, name, LY_EINVAL);
Michal Vasko013a8182020-03-03 10:46:53 +0100874
Michal Vaskof03ed032020-03-04 13:31:44 +0100875 if (!module) {
876 module = parent->schema->module;
877 }
878
Michal Vasko3a41dff2020-07-15 14:30:28 +0200879 schema = lys_find_child(parent ? parent->schema : NULL, module, name, 0,
Radek Krejci41ac9942020-11-02 14:47:56 +0100880 LYS_CONTAINER | LYS_NOTIF | LYS_RPC | LYS_ACTION, output ? LYS_GETNEXT_OUTPUT : 0);
Radek Krejcidd2a7662021-03-12 11:26:34 +0100881 LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "Inner node (not a list) \"%s\" not found.", name), LY_ENOTFOUND);
Michal Vasko013a8182020-03-03 10:46:53 +0100882
Michal Vasko3a41dff2020-07-15 14:30:28 +0200883 LY_CHECK_RET(lyd_create_inner(schema, &ret));
884 if (parent) {
Michal Vasko6ee6f432021-07-16 09:49:14 +0200885 lyd_insert_node(parent, NULL, ret, 0);
Michal Vasko013a8182020-03-03 10:46:53 +0100886 }
Michal Vasko3a41dff2020-07-15 14:30:28 +0200887
888 if (node) {
889 *node = ret;
890 }
891 return LY_SUCCESS;
Michal Vasko013a8182020-03-03 10:46:53 +0100892}
893
Michal Vasko3a41dff2020-07-15 14:30:28 +0200894API LY_ERR
Radek Krejcidd2a7662021-03-12 11:26:34 +0100895lyd_new_ext_inner(const struct lysc_ext_instance *ext, const char *name, struct lyd_node **node)
896{
897 struct lyd_node *ret = NULL;
898 const struct lysc_node *schema;
899 struct ly_ctx *ctx = ext ? ext->module->ctx : NULL;
900
901 LY_CHECK_ARG_RET(ctx, ext, node, name, LY_EINVAL);
902
903 schema = lysc_ext_find_node(ext, NULL, name, 0, LYS_CONTAINER | LYS_NOTIF | LYS_RPC | LYS_ACTION, 0);
904 if (!schema) {
905 if (ext->argument) {
906 LOGERR(ctx, LY_EINVAL, "Inner node (not a list) \"%s\" not found in instance \"%s\" of extension %s.",
907 name, ext->argument, ext->def->name);
908 } else {
909 LOGERR(ctx, LY_EINVAL, "Inner node (not a list) \"%s\" not found in instance of extension %s.",
910 name, ext->def->name);
911 }
912 return LY_ENOTFOUND;
913 }
914 LY_CHECK_RET(lyd_create_inner(schema, &ret));
915
916 *node = ret;
917
918 return LY_SUCCESS;
919}
920
Michal Vasko208e6d62021-06-28 11:23:50 +0200921/**
922 * @brief Create a new list node in the data tree.
923 *
924 * @param[in] parent Parent node for the node being created. NULL in case of creating a top level element.
925 * @param[in] module Module of the node being created. If NULL, @p parent module will be used.
926 * @param[in] name Schema node name of the new data node. The node must be #LYS_LIST.
927 * @param[in] format Format of key values.
928 * @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
929 * taken into consideration. Otherwise, the output's data node is going to be created.
930 * @param[out] node Optional created node.
931 * @param[in] ap Ordered key values of the new list instance, all must be set. For ::LY_VALUE_LYB, every value must
932 * be followed by the value length.
933 * @return LY_ERR value.
934 */
935static LY_ERR
936_lyd_new_list(struct lyd_node *parent, const struct lys_module *module, const char *name, LY_VALUE_FORMAT format,
937 ly_bool output, struct lyd_node **node, va_list ap)
Michal Vasko013a8182020-03-03 10:46:53 +0100938{
939 struct lyd_node *ret = NULL, *key;
940 const struct lysc_node *schema, *key_s;
941 struct ly_ctx *ctx = parent ? parent->schema->module->ctx : (module ? module->ctx : NULL);
Michal Vasko208e6d62021-06-28 11:23:50 +0200942 const void *key_val;
943 uint32_t key_len;
Michal Vasko013a8182020-03-03 10:46:53 +0100944 LY_ERR rc = LY_SUCCESS;
945
Michal Vasko6027eb92020-07-15 16:37:30 +0200946 LY_CHECK_ARG_RET(ctx, parent || module, parent || node, name, LY_EINVAL);
Michal Vasko013a8182020-03-03 10:46:53 +0100947
Michal Vaskof03ed032020-03-04 13:31:44 +0100948 if (!module) {
949 module = parent->schema->module;
950 }
951
Radek Krejci41ac9942020-11-02 14:47:56 +0100952 schema = lys_find_child(parent ? parent->schema : NULL, module, name, 0, LYS_LIST, output ? LYS_GETNEXT_OUTPUT : 0);
Michal Vasko3a41dff2020-07-15 14:30:28 +0200953 LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "List node \"%s\" not found.", name), LY_ENOTFOUND);
Michal Vasko013a8182020-03-03 10:46:53 +0100954
955 /* create list inner node */
Michal Vasko3a41dff2020-07-15 14:30:28 +0200956 LY_CHECK_RET(lyd_create_inner(schema, &ret));
Michal Vasko013a8182020-03-03 10:46:53 +0100957
Michal Vasko013a8182020-03-03 10:46:53 +0100958 /* create and insert all the keys */
Michal Vasko544e58a2021-01-28 14:33:41 +0100959 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 +0200960 if (format == LY_VALUE_LYB) {
961 key_val = va_arg(ap, const void *);
962 key_len = va_arg(ap, uint32_t);
963 } else {
964 key_val = va_arg(ap, const char *);
965 key_len = key_val ? strlen((char *)key_val) : 0;
966 }
Michal Vasko013a8182020-03-03 10:46:53 +0100967
Michal Vasko208e6d62021-06-28 11:23:50 +0200968 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 +0200969 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko6ee6f432021-07-16 09:49:14 +0200970 lyd_insert_node(ret, NULL, key, 1);
Michal Vasko013a8182020-03-03 10:46:53 +0100971 }
972
Michal Vasko013a8182020-03-03 10:46:53 +0100973 if (parent) {
Michal Vasko6ee6f432021-07-16 09:49:14 +0200974 lyd_insert_node(parent, NULL, ret, 0);
Michal Vasko013a8182020-03-03 10:46:53 +0100975 }
976
977cleanup:
978 if (rc) {
979 lyd_free_tree(ret);
980 ret = NULL;
Michal Vasko3a41dff2020-07-15 14:30:28 +0200981 } else if (node) {
982 *node = ret;
Michal Vasko013a8182020-03-03 10:46:53 +0100983 }
Michal Vasko3a41dff2020-07-15 14:30:28 +0200984 return rc;
Michal Vasko013a8182020-03-03 10:46:53 +0100985}
986
Michal Vasko3a41dff2020-07-15 14:30:28 +0200987API LY_ERR
Michal Vasko208e6d62021-06-28 11:23:50 +0200988lyd_new_list(struct lyd_node *parent, const struct lys_module *module, const char *name, ly_bool output,
989 struct lyd_node **node, ...)
990{
991 LY_ERR rc;
992 va_list ap;
993
994 va_start(ap, node);
995
996 rc = _lyd_new_list(parent, module, name, LY_VALUE_JSON, output, node, ap);
997
998 va_end(ap);
999 return rc;
1000}
1001
1002API LY_ERR
1003lyd_new_list_bin(struct lyd_node *parent, const struct lys_module *module, const char *name, ly_bool output,
1004 struct lyd_node **node, ...)
1005{
1006 LY_ERR rc;
1007 va_list ap;
1008
1009 va_start(ap, node);
1010
1011 rc = _lyd_new_list(parent, module, name, LY_VALUE_LYB, output, node, ap);
1012
1013 va_end(ap);
1014 return rc;
1015}
1016
1017API LY_ERR
1018lyd_new_list_canon(struct lyd_node *parent, const struct lys_module *module, const char *name, ly_bool output,
1019 struct lyd_node **node, ...)
1020{
1021 LY_ERR rc;
1022 va_list ap;
1023
1024 va_start(ap, node);
1025
1026 rc = _lyd_new_list(parent, module, name, LY_VALUE_CANON, output, node, ap);
1027
1028 va_end(ap);
1029 return rc;
1030}
1031
1032API LY_ERR
Radek Krejci8247bae2021-03-12 11:47:02 +01001033lyd_new_ext_list(const struct lysc_ext_instance *ext, const char *name, struct lyd_node **node, ...)
1034{
1035 struct lyd_node *ret = NULL, *key;
1036 const struct lysc_node *schema, *key_s;
1037 struct ly_ctx *ctx = ext ? ext->module->ctx : NULL;
1038 va_list ap;
1039 const char *key_val;
1040 LY_ERR rc = LY_SUCCESS;
1041
1042 LY_CHECK_ARG_RET(ctx, ext, node, name, LY_EINVAL);
1043
1044 schema = lysc_ext_find_node(ext, NULL, name, 0, LYS_LIST, 0);
1045 if (!schema) {
1046 if (ext->argument) {
1047 LOGERR(ctx, LY_EINVAL, "List node \"%s\" not found in instance \"%s\" of extension %s.",
1048 name, ext->argument, ext->def->name);
1049 } else {
1050 LOGERR(ctx, LY_EINVAL, "List node \"%s\" not found in instance of extension %s.", name, ext->def->name);
1051 }
1052 return LY_ENOTFOUND;
1053 }
1054 /* create list inner node */
1055 LY_CHECK_RET(lyd_create_inner(schema, &ret));
1056
1057 va_start(ap, node);
1058
1059 /* create and insert all the keys */
1060 for (key_s = lysc_node_child(schema); key_s && (key_s->flags & LYS_KEY); key_s = key_s->next) {
1061 key_val = va_arg(ap, const char *);
1062
Radek Krejci8df109d2021-04-23 12:19:08 +02001063 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 +01001064 NULL, &key);
1065 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko6ee6f432021-07-16 09:49:14 +02001066 lyd_insert_node(ret, NULL, key, 1);
Radek Krejci8247bae2021-03-12 11:47:02 +01001067 }
1068
1069cleanup:
1070 va_end(ap);
1071 if (rc) {
1072 lyd_free_tree(ret);
1073 ret = NULL;
Radek Krejci8247bae2021-03-12 11:47:02 +01001074 }
Radek Krejci8919fbd2021-03-15 09:35:25 +01001075 *node = ret;
Radek Krejci8247bae2021-03-12 11:47:02 +01001076 return rc;
1077}
1078
1079API LY_ERR
Michal Vasko3a41dff2020-07-15 14:30:28 +02001080lyd_new_list2(struct lyd_node *parent, const struct lys_module *module, const char *name, const char *keys,
Radek Krejci41ac9942020-11-02 14:47:56 +01001081 ly_bool output, struct lyd_node **node)
Michal Vasko013a8182020-03-03 10:46:53 +01001082{
1083 struct lyd_node *ret = NULL;
1084 const struct lysc_node *schema;
1085 struct ly_ctx *ctx = parent ? parent->schema->module->ctx : (module ? module->ctx : NULL);
1086
Michal Vasko6027eb92020-07-15 16:37:30 +02001087 LY_CHECK_ARG_RET(ctx, parent || module, parent || node, name, LY_EINVAL);
Michal Vasko013a8182020-03-03 10:46:53 +01001088
Michal Vaskof03ed032020-03-04 13:31:44 +01001089 if (!module) {
1090 module = parent->schema->module;
1091 }
Michal Vasko004d3152020-06-11 19:59:22 +02001092 if (!keys) {
1093 keys = "";
1094 }
Michal Vaskof03ed032020-03-04 13:31:44 +01001095
Michal Vasko004d3152020-06-11 19:59:22 +02001096 /* find schema node */
Radek Krejci41ac9942020-11-02 14:47:56 +01001097 schema = lys_find_child(parent ? parent->schema : NULL, module, name, 0, LYS_LIST, output ? LYS_GETNEXT_OUTPUT : 0);
Michal Vasko3a41dff2020-07-15 14:30:28 +02001098 LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "List node \"%s\" not found.", name), LY_ENOTFOUND);
Michal Vasko013a8182020-03-03 10:46:53 +01001099
Michal Vasko004d3152020-06-11 19:59:22 +02001100 if ((schema->flags & LYS_KEYLESS) && !keys[0]) {
1101 /* key-less list */
Michal Vasko3a41dff2020-07-15 14:30:28 +02001102 LY_CHECK_RET(lyd_create_inner(schema, &ret));
Michal Vasko004d3152020-06-11 19:59:22 +02001103 } else {
1104 /* create the list node */
Michal Vasko3a41dff2020-07-15 14:30:28 +02001105 LY_CHECK_RET(lyd_create_list2(schema, keys, strlen(keys), &ret));
Michal Vasko004d3152020-06-11 19:59:22 +02001106 }
Michal Vasko004d3152020-06-11 19:59:22 +02001107 if (parent) {
Michal Vasko6ee6f432021-07-16 09:49:14 +02001108 lyd_insert_node(parent, NULL, ret, 0);
Michal Vasko013a8182020-03-03 10:46:53 +01001109 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02001110
1111 if (node) {
1112 *node = ret;
1113 }
1114 return LY_SUCCESS;
Michal Vasko013a8182020-03-03 10:46:53 +01001115}
1116
Radek Krejci09c77442021-04-26 11:10:34 +02001117/**
1118 * @brief Create a new term node in the data tree.
1119 *
1120 * @param[in] parent Parent node for the node being created. NULL in case of creating a top level element.
1121 * @param[in] module Module of the node being created. If NULL, @p parent module will be used.
1122 * @param[in] name Schema node name of the new data node. The node can be ::LYS_LEAF or ::LYS_LEAFLIST.
1123 * @param[in] value Value of the node being created.
1124 * @param[in] value_len Length of @p value.
1125 * @param[in] format Format of @p value.
1126 * @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
1127 * taken into consideration. Otherwise, the output's data node is going to be created.
1128 * @param[out] node Optional created node.
1129 * @return LY_ERR value.
1130 */
1131static LY_ERR
1132_lyd_new_term(struct lyd_node *parent, const struct lys_module *module, const char *name, const void *value,
1133 size_t value_len, LY_VALUE_FORMAT format, ly_bool output, struct lyd_node **node)
Michal Vasko013a8182020-03-03 10:46:53 +01001134{
Michal Vaskocbff3e92020-05-27 12:56:41 +02001135 LY_ERR rc;
Michal Vasko013a8182020-03-03 10:46:53 +01001136 struct lyd_node *ret = NULL;
1137 const struct lysc_node *schema;
Radek Krejci09c77442021-04-26 11:10:34 +02001138 const struct ly_ctx *ctx = parent ? LYD_CTX(parent) : (module ? module->ctx : NULL);
Michal Vasko013a8182020-03-03 10:46:53 +01001139
Michal Vasko6027eb92020-07-15 16:37:30 +02001140 LY_CHECK_ARG_RET(ctx, parent || module, parent || node, name, LY_EINVAL);
Michal Vasko013a8182020-03-03 10:46:53 +01001141
Michal Vaskof03ed032020-03-04 13:31:44 +01001142 if (!module) {
1143 module = parent->schema->module;
1144 }
1145
Radek Krejci41ac9942020-11-02 14:47:56 +01001146 schema = lys_find_child(parent ? parent->schema : NULL, module, name, 0, LYD_NODE_TERM, output ? LYS_GETNEXT_OUTPUT : 0);
Michal Vasko3a41dff2020-07-15 14:30:28 +02001147 LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "Term node \"%s\" not found.", name), LY_ENOTFOUND);
Michal Vasko013a8182020-03-03 10:46:53 +01001148
Radek Krejci09c77442021-04-26 11:10:34 +02001149 rc = lyd_create_term(schema, value, value_len, NULL, format, NULL, LYD_HINT_DATA, NULL, &ret);
Michal Vaskofeca4fb2020-10-05 08:58:40 +02001150 LY_CHECK_RET(rc);
Michal Vaskocbff3e92020-05-27 12:56:41 +02001151 if (parent) {
Michal Vasko6ee6f432021-07-16 09:49:14 +02001152 lyd_insert_node(parent, NULL, ret, 0);
Michal Vasko013a8182020-03-03 10:46:53 +01001153 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02001154
1155 if (node) {
1156 *node = ret;
1157 }
1158 return LY_SUCCESS;
Michal Vasko013a8182020-03-03 10:46:53 +01001159}
1160
Michal Vasko3a41dff2020-07-15 14:30:28 +02001161API LY_ERR
Radek Krejci09c77442021-04-26 11:10:34 +02001162lyd_new_term(struct lyd_node *parent, const struct lys_module *module, const char *name, const char *val_str,
1163 ly_bool output, struct lyd_node **node)
1164{
1165 return _lyd_new_term(parent, module, name, val_str, val_str ? strlen(val_str) : 0, LY_VALUE_JSON, output, node);
1166}
1167
1168API LY_ERR
1169lyd_new_term_bin(struct lyd_node *parent, const struct lys_module *module, const char *name, const void *value,
1170 size_t value_len, ly_bool output, struct lyd_node **node)
1171{
1172 return _lyd_new_term(parent, module, name, value, value_len, LY_VALUE_LYB, output, node);
1173}
1174
1175API LY_ERR
1176lyd_new_term_canon(struct lyd_node *parent, const struct lys_module *module, const char *name, const char *val_str,
1177 ly_bool output, struct lyd_node **node)
1178{
1179 return _lyd_new_term(parent, module, name, val_str, val_str ? strlen(val_str) : 0, LY_VALUE_CANON, output, node);
1180}
1181
1182API LY_ERR
Radek Krejci8a5afc22021-03-12 11:10:47 +01001183lyd_new_ext_term(const struct lysc_ext_instance *ext, const char *name, const char *val_str, struct lyd_node **node)
1184{
1185 LY_ERR rc;
1186 struct lyd_node *ret = NULL;
1187 const struct lysc_node *schema;
1188 struct ly_ctx *ctx = ext ? ext->module->ctx : NULL;
1189
1190 LY_CHECK_ARG_RET(ctx, ext, node, name, LY_EINVAL);
1191
1192 schema = lysc_ext_find_node(ext, NULL, name, 0, LYD_NODE_TERM, 0);
1193 if (!schema) {
1194 if (ext->argument) {
1195 LOGERR(ctx, LY_EINVAL, "Term node \"%s\" not found in instance \"%s\" of extension %s.",
1196 name, ext->argument, ext->def->name);
1197 } else {
1198 LOGERR(ctx, LY_EINVAL, "Term node \"%s\" not found in instance of extension %s.", name, ext->def->name);
1199 }
1200 return LY_ENOTFOUND;
1201 }
Radek Krejci8df109d2021-04-23 12:19:08 +02001202 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 +01001203 LY_CHECK_RET(rc);
1204
1205 *node = ret;
1206
1207 return LY_SUCCESS;
1208}
1209
1210API LY_ERR
Michal Vasko2a4ab2b2021-03-04 15:52:40 +01001211lyd_new_any(struct lyd_node *parent, const struct lys_module *module, const char *name, const void *value,
1212 ly_bool use_value, LYD_ANYDATA_VALUETYPE value_type, ly_bool output, struct lyd_node **node)
Michal Vasko013a8182020-03-03 10:46:53 +01001213{
1214 struct lyd_node *ret = NULL;
1215 const struct lysc_node *schema;
1216 struct ly_ctx *ctx = parent ? parent->schema->module->ctx : (module ? module->ctx : NULL);
1217
Michal Vasko6027eb92020-07-15 16:37:30 +02001218 LY_CHECK_ARG_RET(ctx, parent || module, parent || node, name, LY_EINVAL);
Michal Vasko013a8182020-03-03 10:46:53 +01001219
Michal Vaskof03ed032020-03-04 13:31:44 +01001220 if (!module) {
1221 module = parent->schema->module;
1222 }
1223
Radek Krejci41ac9942020-11-02 14:47:56 +01001224 schema = lys_find_child(parent ? parent->schema : NULL, module, name, 0, LYD_NODE_ANY, output ? LYS_GETNEXT_OUTPUT : 0);
Michal Vasko3a41dff2020-07-15 14:30:28 +02001225 LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "Any node \"%s\" not found.", name), LY_ENOTFOUND);
Michal Vasko013a8182020-03-03 10:46:53 +01001226
Michal Vasko2a4ab2b2021-03-04 15:52:40 +01001227 LY_CHECK_RET(lyd_create_any(schema, value, value_type, use_value, &ret));
Michal Vasko3a41dff2020-07-15 14:30:28 +02001228 if (parent) {
Michal Vasko6ee6f432021-07-16 09:49:14 +02001229 lyd_insert_node(parent, NULL, ret, 0);
Michal Vasko013a8182020-03-03 10:46:53 +01001230 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02001231
1232 if (node) {
1233 *node = ret;
1234 }
1235 return LY_SUCCESS;
Michal Vasko013a8182020-03-03 10:46:53 +01001236}
1237
Radek Krejci0b963da2021-03-12 13:23:44 +01001238API LY_ERR
1239lyd_new_ext_any(const struct lysc_ext_instance *ext, const char *name, const void *value, ly_bool use_value,
1240 LYD_ANYDATA_VALUETYPE value_type, struct lyd_node **node)
1241{
1242 struct lyd_node *ret = NULL;
1243 const struct lysc_node *schema;
1244 struct ly_ctx *ctx = ext ? ext->module->ctx : NULL;
1245
1246 LY_CHECK_ARG_RET(ctx, ext, node, name, LY_EINVAL);
1247
1248 schema = lysc_ext_find_node(ext, NULL, name, 0, LYD_NODE_ANY, 0);
1249 if (!schema) {
1250 if (ext->argument) {
1251 LOGERR(ctx, LY_EINVAL, "Any node \"%s\" not found in instance \"%s\" of extension %s.",
1252 name, ext->argument, ext->def->name);
1253 } else {
1254 LOGERR(ctx, LY_EINVAL, "Any node \"%s\" not found in instance of extension %s.", name, ext->def->name);
1255 }
1256 return LY_ENOTFOUND;
1257 }
1258 LY_CHECK_RET(lyd_create_any(schema, value, value_type, use_value, &ret));
1259
1260 *node = ret;
1261
1262 return LY_SUCCESS;
1263}
1264
Michal Vasko3a41dff2020-07-15 14:30:28 +02001265API LY_ERR
Michal Vasko871a0252020-11-11 18:35:24 +01001266lyd_new_meta(const struct ly_ctx *ctx, struct lyd_node *parent, const struct lys_module *module, const char *name,
1267 const char *val_str, ly_bool clear_dflt, struct lyd_meta **meta)
Michal Vaskod86997b2020-05-26 15:19:54 +02001268{
Michal Vaskod86997b2020-05-26 15:19:54 +02001269 const char *prefix, *tmp;
Michal Vaskod86997b2020-05-26 15:19:54 +02001270 size_t pref_len, name_len;
1271
Michal Vasko2b5344c2021-02-26 10:12:05 +01001272 LY_CHECK_ARG_RET(ctx, ctx || parent, name, module || strchr(name, ':'), parent || meta, LY_EINVAL);
1273 if (!ctx) {
1274 ctx = LYD_CTX(parent);
1275 }
Michal Vasko00cbf532020-06-15 13:58:47 +02001276
Michal Vasko871a0252020-11-11 18:35:24 +01001277 if (parent && !parent->schema) {
Michal Vasko33b4fab2021-03-04 15:56:12 +01001278 LOGERR(ctx, LY_EINVAL, "Cannot add metadata \"%s\" to an opaque node \"%s\".", name, LYD_NAME(parent));
Michal Vasko871a0252020-11-11 18:35:24 +01001279 return LY_EINVAL;
1280 }
Michal Vasko610553d2020-11-18 18:15:05 +01001281 if (meta) {
1282 *meta = NULL;
1283 }
Michal Vaskod86997b2020-05-26 15:19:54 +02001284
1285 /* parse the name */
1286 tmp = name;
1287 if (ly_parse_nodeid(&tmp, &prefix, &pref_len, &name, &name_len) || tmp[0]) {
1288 LOGERR(ctx, LY_EINVAL, "Metadata name \"%s\" is not valid.", name);
Michal Vasko871a0252020-11-11 18:35:24 +01001289 return LY_EINVAL;
Michal Vaskod86997b2020-05-26 15:19:54 +02001290 }
1291
1292 /* find the module */
1293 if (prefix) {
Radek Krejci0ad51f12020-07-16 12:08:12 +02001294 module = ly_ctx_get_module_implemented2(ctx, prefix, pref_len);
Radek Krejci422afb12021-03-04 16:38:16 +01001295 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 +02001296 }
1297
1298 /* set value if none */
1299 if (!val_str) {
1300 val_str = "";
1301 }
1302
Radek Krejci8df109d2021-04-23 12:19:08 +02001303 return lyd_create_meta(parent, meta, module, name, name_len, val_str, strlen(val_str), NULL, LY_VALUE_JSON,
Michal Vasko871a0252020-11-11 18:35:24 +01001304 NULL, LYD_HINT_DATA, clear_dflt, NULL);
1305}
Michal Vasko3a41dff2020-07-15 14:30:28 +02001306
Michal Vaskoba696702020-11-11 19:12:45 +01001307API LY_ERR
1308lyd_new_meta2(const struct ly_ctx *ctx, struct lyd_node *parent, ly_bool clear_dflt, const struct lyd_attr *attr,
1309 struct lyd_meta **meta)
1310{
1311 const struct lys_module *mod;
1312
1313 LY_CHECK_ARG_RET(NULL, ctx, attr, parent || meta, LY_EINVAL);
1314
1315 if (parent && !parent->schema) {
1316 LOGERR(ctx, LY_EINVAL, "Cannot add metadata to an opaque node \"%s\".", ((struct lyd_node_opaq *)parent)->name);
1317 return LY_EINVAL;
Michal Vasko3a41dff2020-07-15 14:30:28 +02001318 }
Michal Vasko610553d2020-11-18 18:15:05 +01001319 if (meta) {
1320 *meta = NULL;
1321 }
Michal Vaskoba696702020-11-11 19:12:45 +01001322
1323 switch (attr->format) {
Radek Krejci8df109d2021-04-23 12:19:08 +02001324 case LY_VALUE_XML:
Michal Vaskoad92b672020-11-12 13:11:31 +01001325 mod = ly_ctx_get_module_implemented_ns(ctx, attr->name.module_ns);
Michal Vaskoba696702020-11-11 19:12:45 +01001326 if (!mod) {
Michal Vaskoad92b672020-11-12 13:11:31 +01001327 LOGERR(ctx, LY_EINVAL, "Module with namespace \"%s\" not found.", attr->name.module_ns);
Michal Vaskoba696702020-11-11 19:12:45 +01001328 return LY_ENOTFOUND;
1329 }
1330 break;
Radek Krejci8df109d2021-04-23 12:19:08 +02001331 case LY_VALUE_JSON:
Michal Vaskoad92b672020-11-12 13:11:31 +01001332 mod = ly_ctx_get_module_implemented(ctx, attr->name.module_name);
Michal Vaskoba696702020-11-11 19:12:45 +01001333 if (!mod) {
Michal Vaskoad92b672020-11-12 13:11:31 +01001334 LOGERR(ctx, LY_EINVAL, "Module \"%s\" not found.", attr->name.module_name);
Michal Vaskoba696702020-11-11 19:12:45 +01001335 return LY_ENOTFOUND;
1336 }
1337 break;
1338 default:
1339 LOGINT_RET(ctx);
1340 }
1341
Michal Vaskoad92b672020-11-12 13:11:31 +01001342 return lyd_create_meta(parent, meta, mod, attr->name.name, strlen(attr->name.name), attr->value, strlen(attr->value),
Michal Vaskoba696702020-11-11 19:12:45 +01001343 NULL, attr->format, attr->val_prefix_data, attr->hints, clear_dflt, NULL);
Michal Vaskod86997b2020-05-26 15:19:54 +02001344}
1345
Michal Vasko3a41dff2020-07-15 14:30:28 +02001346API LY_ERR
Michal Vasko00cbf532020-06-15 13:58:47 +02001347lyd_new_opaq(struct lyd_node *parent, const struct ly_ctx *ctx, const char *name, const char *value,
Michal Vasko0ab974d2021-02-24 13:18:26 +01001348 const char *prefix, const char *module_name, struct lyd_node **node)
Michal Vasko00cbf532020-06-15 13:58:47 +02001349{
1350 struct lyd_node *ret = NULL;
Radek Krejci07a55962021-03-02 20:16:43 +01001351
Michal Vasko0ab974d2021-02-24 13:18:26 +01001352 LY_CHECK_ARG_RET(ctx, parent || ctx, parent || node, name, module_name, !prefix || !strcmp(prefix, module_name), LY_EINVAL);
Michal Vasko00cbf532020-06-15 13:58:47 +02001353
1354 if (!ctx) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02001355 ctx = LYD_CTX(parent);
Michal Vasko00cbf532020-06-15 13:58:47 +02001356 }
1357 if (!value) {
1358 value = "";
1359 }
1360
Michal Vasko0ab974d2021-02-24 13:18:26 +01001361 LY_CHECK_RET(lyd_create_opaq(ctx, name, strlen(name), prefix, prefix ? strlen(prefix) : 0, module_name,
Radek Krejci8df109d2021-04-23 12:19:08 +02001362 strlen(module_name), value, strlen(value), NULL, LY_VALUE_JSON, NULL, 0, &ret));
Michal Vasko3a41dff2020-07-15 14:30:28 +02001363 if (parent) {
Michal Vasko6ee6f432021-07-16 09:49:14 +02001364 lyd_insert_node(parent, NULL, ret, 1);
Michal Vasko00cbf532020-06-15 13:58:47 +02001365 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02001366
1367 if (node) {
1368 *node = ret;
1369 }
1370 return LY_SUCCESS;
Michal Vasko00cbf532020-06-15 13:58:47 +02001371}
1372
Michal Vasko3a41dff2020-07-15 14:30:28 +02001373API LY_ERR
Michal Vasko8d65f852021-02-17 11:28:13 +01001374lyd_new_opaq2(struct lyd_node *parent, const struct ly_ctx *ctx, const char *name, const char *value,
Michal Vasko0ab974d2021-02-24 13:18:26 +01001375 const char *prefix, const char *module_ns, struct lyd_node **node)
Michal Vasko8d65f852021-02-17 11:28:13 +01001376{
1377 struct lyd_node *ret = NULL;
1378
1379 LY_CHECK_ARG_RET(ctx, parent || ctx, parent || node, name, module_ns, LY_EINVAL);
1380
1381 if (!ctx) {
1382 ctx = LYD_CTX(parent);
1383 }
1384 if (!value) {
1385 value = "";
1386 }
1387
Michal Vasko0ab974d2021-02-24 13:18:26 +01001388 LY_CHECK_RET(lyd_create_opaq(ctx, name, strlen(name), prefix, prefix ? strlen(prefix) : 0, module_ns,
Radek Krejci8df109d2021-04-23 12:19:08 +02001389 strlen(module_ns), value, strlen(value), NULL, LY_VALUE_XML, NULL, 0, &ret));
Michal Vasko8d65f852021-02-17 11:28:13 +01001390 if (parent) {
Michal Vasko6ee6f432021-07-16 09:49:14 +02001391 lyd_insert_node(parent, NULL, ret, 1);
Michal Vasko8d65f852021-02-17 11:28:13 +01001392 }
1393
1394 if (node) {
1395 *node = ret;
1396 }
1397 return LY_SUCCESS;
1398}
1399
1400API LY_ERR
1401lyd_new_attr(struct lyd_node *parent, const char *module_name, const char *name, const char *value,
Radek Krejci0f969882020-08-21 16:56:47 +02001402 struct lyd_attr **attr)
Michal Vasko00cbf532020-06-15 13:58:47 +02001403{
Radek Krejci1798aae2020-07-14 13:26:06 +02001404 struct lyd_attr *ret = NULL;
Michal Vasko00cbf532020-06-15 13:58:47 +02001405 const struct ly_ctx *ctx;
1406 const char *prefix, *tmp;
Michal Vasko51d21b82020-11-13 18:03:54 +01001407 size_t pref_len, name_len, mod_len;
Michal Vasko00cbf532020-06-15 13:58:47 +02001408
Michal Vasko3a41dff2020-07-15 14:30:28 +02001409 LY_CHECK_ARG_RET(NULL, parent, !parent->schema, name, LY_EINVAL);
Michal Vasko00cbf532020-06-15 13:58:47 +02001410
Michal Vaskob7be7a82020-08-20 09:09:04 +02001411 ctx = LYD_CTX(parent);
Michal Vasko00cbf532020-06-15 13:58:47 +02001412
1413 /* parse the name */
1414 tmp = name;
1415 if (ly_parse_nodeid(&tmp, &prefix, &pref_len, &name, &name_len) || tmp[0]) {
Michal Vaskob69b68c2021-02-17 11:18:16 +01001416 LOGERR(ctx, LY_EINVAL, "Attribute name \"%s\" is not valid.", name);
Michal Vasko3a41dff2020-07-15 14:30:28 +02001417 return LY_EVALID;
Michal Vasko00cbf532020-06-15 13:58:47 +02001418 }
1419
Michal Vasko51d21b82020-11-13 18:03:54 +01001420 /* get the module */
1421 if (module_name) {
1422 mod_len = strlen(module_name);
1423 } else {
1424 module_name = prefix;
1425 mod_len = pref_len;
1426 }
1427
Michal Vasko00cbf532020-06-15 13:58:47 +02001428 /* set value if none */
Michal Vasko8d65f852021-02-17 11:28:13 +01001429 if (!value) {
1430 value = "";
Michal Vasko00cbf532020-06-15 13:58:47 +02001431 }
1432
Michal Vasko8d65f852021-02-17 11:28:13 +01001433 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 +02001434 strlen(value), NULL, LY_VALUE_JSON, NULL, LYD_HINT_DATA));
Michal Vasko8d65f852021-02-17 11:28:13 +01001435
1436 if (attr) {
1437 *attr = ret;
1438 }
1439 return LY_SUCCESS;
1440}
1441
1442API LY_ERR
1443lyd_new_attr2(struct lyd_node *parent, const char *module_ns, const char *name, const char *value,
1444 struct lyd_attr **attr)
1445{
1446 struct lyd_attr *ret = NULL;
1447 const struct ly_ctx *ctx;
1448 const char *prefix, *tmp;
1449 size_t pref_len, name_len;
1450
1451 LY_CHECK_ARG_RET(NULL, parent, !parent->schema, name, LY_EINVAL);
1452
1453 ctx = LYD_CTX(parent);
1454
1455 /* parse the name */
1456 tmp = name;
1457 if (ly_parse_nodeid(&tmp, &prefix, &pref_len, &name, &name_len) || tmp[0]) {
1458 LOGERR(ctx, LY_EINVAL, "Attribute name \"%s\" is not valid.", name);
1459 return LY_EVALID;
1460 }
1461
1462 /* set value if none */
1463 if (!value) {
1464 value = "";
1465 }
1466
1467 LY_CHECK_RET(lyd_create_attr(parent, &ret, ctx, name, name_len, prefix, pref_len, module_ns,
Radek Krejci8df109d2021-04-23 12:19:08 +02001468 module_ns ? strlen(module_ns) : 0, value, strlen(value), NULL, LY_VALUE_XML, NULL, LYD_HINT_DATA));
Michal Vasko3a41dff2020-07-15 14:30:28 +02001469
1470 if (attr) {
1471 *attr = ret;
1472 }
1473 return LY_SUCCESS;
Michal Vasko00cbf532020-06-15 13:58:47 +02001474}
1475
Radek Krejci09c77442021-04-26 11:10:34 +02001476/**
1477 * @brief Change the value of a term (leaf or leaf-list) node.
1478 *
1479 * Node changed this way is always considered explicitly set, meaning its default flag
1480 * is always cleared.
1481 *
1482 * @param[in] term Term node to change.
1483 * @param[in] value New value to set.
1484 * @param[in] value_len Length of @p value.
1485 * @param[in] format Format of @p value.
1486 * @return LY_SUCCESS if value was changed,
1487 * @return LY_EEXIST if value was the same and only the default flag was cleared,
1488 * @return LY_ENOT if the values were equal and no change occured,
1489 * @return LY_ERR value on other errors.
1490 */
1491static LY_ERR
1492_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 +02001493{
1494 LY_ERR ret = LY_SUCCESS;
1495 struct lysc_type *type;
1496 struct lyd_node_term *t;
1497 struct lyd_node *parent;
1498 struct lyd_value val = {0};
Radek Krejci857189e2020-09-01 13:26:36 +02001499 ly_bool dflt_change, val_change;
Michal Vasko00cbf532020-06-15 13:58:47 +02001500
Radek Krejci09c77442021-04-26 11:10:34 +02001501 assert(term && term->schema && (term->schema->nodetype & LYD_NODE_TERM));
Michal Vasko00cbf532020-06-15 13:58:47 +02001502
Michal Vasko00cbf532020-06-15 13:58:47 +02001503 t = (struct lyd_node_term *)term;
1504 type = ((struct lysc_node_leaf *)term->schema)->type;
1505
1506 /* parse the new value */
Radek Krejciddace2c2021-01-08 11:30:56 +01001507 LOG_LOCSET(term->schema, term, NULL, NULL);
Radek Krejci09c77442021-04-26 11:10:34 +02001508 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 +01001509 LOG_LOCBACK(term->schema ? 1 : 0, 1, 0, 0);
Michal Vaskofeca4fb2020-10-05 08:58:40 +02001510 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko00cbf532020-06-15 13:58:47 +02001511
1512 /* compare original and new value */
1513 if (type->plugin->compare(&t->value, &val)) {
1514 /* values differ, switch them */
Michal Vaskob7be7a82020-08-20 09:09:04 +02001515 type->plugin->free(LYD_CTX(term), &t->value);
Michal Vasko00cbf532020-06-15 13:58:47 +02001516 t->value = val;
1517 memset(&val, 0, sizeof val);
1518 val_change = 1;
1519 } else {
1520 val_change = 0;
1521 }
1522
1523 /* always clear the default flag */
1524 if (term->flags & LYD_DEFAULT) {
Michal Vasko9e685082021-01-29 14:49:09 +01001525 for (parent = term; parent; parent = lyd_parent(parent)) {
Michal Vasko00cbf532020-06-15 13:58:47 +02001526 parent->flags &= ~LYD_DEFAULT;
1527 }
1528 dflt_change = 1;
1529 } else {
1530 dflt_change = 0;
1531 }
1532
1533 if (val_change || dflt_change) {
1534 /* make the node non-validated */
1535 term->flags &= LYD_NEW;
1536 }
1537
1538 if (val_change) {
1539 if (term->schema->nodetype == LYS_LEAFLIST) {
1540 /* leaf-list needs to be hashed again and re-inserted into parent */
1541 lyd_unlink_hash(term);
1542 lyd_hash(term);
1543 LY_CHECK_GOTO(ret = lyd_insert_hash(term), cleanup);
1544 } else if ((term->schema->flags & LYS_KEY) && term->parent) {
1545 /* list needs to be updated if its key was changed */
1546 assert(term->parent->schema->nodetype == LYS_LIST);
Michal Vasko9e685082021-01-29 14:49:09 +01001547 lyd_unlink_hash(lyd_parent(term));
1548 lyd_hash(lyd_parent(term));
1549 LY_CHECK_GOTO(ret = lyd_insert_hash(lyd_parent(term)), cleanup);
Michal Vasko00cbf532020-06-15 13:58:47 +02001550 } /* else leaf that is not a key, its value is not used for its hash so it does not change */
1551 }
1552
1553 /* retrun value */
1554 if (!val_change) {
1555 if (dflt_change) {
1556 /* only default flag change */
1557 ret = LY_EEXIST;
1558 } else {
1559 /* no change */
1560 ret = LY_ENOT;
1561 }
1562 } /* else value changed, LY_SUCCESS */
1563
1564cleanup:
Michal Vasko59512fc2020-12-09 18:13:29 +01001565 if (val.realtype) {
1566 type->plugin->free(LYD_CTX(term), &val);
1567 }
Michal Vasko00cbf532020-06-15 13:58:47 +02001568 return ret;
1569}
1570
Michal Vasko41586352020-07-13 13:54:25 +02001571API LY_ERR
Radek Krejci09c77442021-04-26 11:10:34 +02001572lyd_change_term(struct lyd_node *term, const char *val_str)
1573{
1574 LY_CHECK_ARG_RET(NULL, term, term->schema, term->schema->nodetype & LYD_NODE_TERM, LY_EINVAL);
1575
1576 return _lyd_change_term(term, val_str, val_str ? strlen(val_str) : 0, LY_VALUE_JSON);
1577}
1578
1579API LY_ERR
1580lyd_change_term_bin(struct lyd_node *term, const void *value, size_t value_len)
1581{
1582 LY_CHECK_ARG_RET(NULL, term, term->schema, term->schema->nodetype & LYD_NODE_TERM, LY_EINVAL);
1583
1584 return _lyd_change_term(term, value, value_len, LY_VALUE_LYB);
1585}
1586
1587API LY_ERR
1588lyd_change_term_canon(struct lyd_node *term, const char *val_str)
1589{
1590 LY_CHECK_ARG_RET(NULL, term, term->schema, term->schema->nodetype & LYD_NODE_TERM, LY_EINVAL);
1591
1592 return _lyd_change_term(term, val_str, val_str ? strlen(val_str) : 0, LY_VALUE_CANON);
1593}
1594
1595API LY_ERR
Michal Vasko41586352020-07-13 13:54:25 +02001596lyd_change_meta(struct lyd_meta *meta, const char *val_str)
1597{
1598 LY_ERR ret = LY_SUCCESS;
Radek Krejci2b18bf12020-11-06 11:20:20 +01001599 struct lyd_meta *m2 = NULL;
Michal Vasko41586352020-07-13 13:54:25 +02001600 struct lyd_value val;
Radek Krejci857189e2020-09-01 13:26:36 +02001601 ly_bool val_change;
Michal Vasko41586352020-07-13 13:54:25 +02001602
1603 LY_CHECK_ARG_RET(NULL, meta, LY_EINVAL);
1604
1605 if (!val_str) {
1606 val_str = "";
1607 }
1608
1609 /* parse the new value into a new meta structure */
1610 LY_CHECK_GOTO(ret = lyd_create_meta(NULL, &m2, meta->annotation->module, meta->name, strlen(meta->name), val_str,
Radek Krejci8df109d2021-04-23 12:19:08 +02001611 strlen(val_str), NULL, LY_VALUE_JSON, NULL, LYD_HINT_DATA, 0, NULL), cleanup);
Michal Vasko41586352020-07-13 13:54:25 +02001612
1613 /* compare original and new value */
1614 if (lyd_compare_meta(meta, m2)) {
1615 /* values differ, switch them */
1616 val = meta->value;
1617 meta->value = m2->value;
1618 m2->value = val;
1619 val_change = 1;
1620 } else {
1621 val_change = 0;
1622 }
1623
1624 /* retrun value */
1625 if (!val_change) {
1626 /* no change */
1627 ret = LY_ENOT;
1628 } /* else value changed, LY_SUCCESS */
1629
1630cleanup:
Michal Vasko1a66a5d2020-11-18 18:15:37 +01001631 lyd_free_meta_single(m2);
Michal Vasko41586352020-07-13 13:54:25 +02001632 return ret;
1633}
1634
Radek Krejci09c77442021-04-26 11:10:34 +02001635/**
1636 * @brief Update node value.
1637 *
1638 * @param[in] node Node to update.
1639 * @param[in] value New value to set.
1640 * @param[in] value_len Length of @p value.
1641 * @param[in] value_type Type of @p value for anydata/anyxml node.
1642 * @param[in] format Format of @p value.
1643 * @param[out] new_parent Set to @p node if the value was updated, otherwise set to NULL.
1644 * @param[out] new_node Set to @p node if the value was updated, otherwise set to NULL.
1645 * @return LY_ERR value.
1646 */
Michal Vasko6741dc62021-02-04 09:27:45 +01001647static LY_ERR
Radek Krejci09c77442021-04-26 11:10:34 +02001648lyd_new_path_update(struct lyd_node *node, const void *value, size_t value_len, LYD_ANYDATA_VALUETYPE value_type,
1649 LY_VALUE_FORMAT format, struct lyd_node **new_parent, struct lyd_node **new_node)
1650{
1651 LY_ERR ret = LY_SUCCESS;
1652 struct lyd_node *new_any;
1653
1654 switch (node->schema->nodetype) {
1655 case LYS_CONTAINER:
1656 case LYS_NOTIF:
1657 case LYS_RPC:
1658 case LYS_ACTION:
1659 case LYS_LIST:
1660 /* if it exists, there is nothing to update */
1661 *new_parent = NULL;
1662 *new_node = NULL;
1663 break;
1664 case LYS_LEAFLIST:
1665 if (!lysc_is_dup_inst_list(node->schema)) {
1666 /* if it exists, there is nothing to update */
1667 *new_parent = NULL;
1668 *new_node = NULL;
1669 break;
1670 }
1671 /* fallthrough */
1672 case LYS_LEAF:
1673 ret = _lyd_change_term(node, value, value_len, format);
1674 if ((ret == LY_SUCCESS) || (ret == LY_EEXIST)) {
1675 /* there was an actual change (at least of the default flag) */
1676 *new_parent = node;
1677 *new_node = node;
1678 ret = LY_SUCCESS;
1679 } else if (ret == LY_ENOT) {
1680 /* no change */
1681 *new_parent = NULL;
1682 *new_node = NULL;
1683 ret = LY_SUCCESS;
1684 } /* else error */
1685 break;
1686 case LYS_ANYDATA:
1687 case LYS_ANYXML:
1688 /* create a new any node */
1689 LY_CHECK_RET(lyd_create_any(node->schema, value, value_type, 0, &new_any));
1690
1691 /* compare with the existing one */
1692 if (lyd_compare_single(node, new_any, 0)) {
1693 /* not equal, switch values (so that we can use generic node free) */
1694 ((struct lyd_node_any *)new_any)->value = ((struct lyd_node_any *)node)->value;
1695 ((struct lyd_node_any *)new_any)->value_type = ((struct lyd_node_any *)node)->value_type;
1696 ((struct lyd_node_any *)node)->value.str = value;
1697 ((struct lyd_node_any *)node)->value_type = value_type;
1698
1699 *new_parent = node;
1700 *new_node = node;
1701 } else {
1702 /* they are equal */
1703 *new_parent = NULL;
1704 *new_node = NULL;
1705 }
1706 lyd_free_tree(new_any);
1707 break;
1708 default:
1709 LOGINT(LYD_CTX(node));
1710 ret = LY_EINT;
1711 break;
1712 }
1713
1714 return ret;
1715}
1716
1717static LY_ERR
1718lyd_new_path_check_find_lypath(struct ly_path *path, const char *str_path, const char *value, size_t value_len,
1719 LY_VALUE_FORMAT format, uint32_t options)
Michal Vasko6741dc62021-02-04 09:27:45 +01001720{
Michal Vaskoe78faec2021-04-08 17:24:43 +02001721 LY_ERR r;
Michal Vasko6741dc62021-02-04 09:27:45 +01001722 struct ly_path_predicate *pred;
Michal Vaskoe78faec2021-04-08 17:24:43 +02001723 const struct lysc_node *schema = NULL;
1724 LY_ARRAY_COUNT_TYPE u, new_count;
1725 int create = 0;
Michal Vasko6741dc62021-02-04 09:27:45 +01001726
1727 assert(path);
1728
1729 /* go through all the compiled nodes */
1730 LY_ARRAY_FOR(path, u) {
1731 schema = path[u].node;
Michal Vaskoe78faec2021-04-08 17:24:43 +02001732
1733 if (lysc_is_dup_inst_list(schema)) {
1734 if (path[u].pred_type == LY_PATH_PREDTYPE_NONE) {
1735 /* creating a new key-less list or state leaf-list instance */
1736 create = 1;
1737 new_count = u;
1738 } else if (path[u].pred_type != LY_PATH_PREDTYPE_POSITION) {
1739 LOG_LOCSET(schema, NULL, NULL, NULL);
1740 LOGVAL(schema->module->ctx, LYVE_XPATH, "Invalid predicate for %s \"%s\" in path \"%s\".",
1741 lys_nodetype2str(schema->nodetype), schema->name, str_path);
1742 LOG_LOCBACK(1, 0, 0, 0);
1743 return LY_EINVAL;
1744 }
1745 } else if ((schema->nodetype == LYS_LIST) && (path[u].pred_type != LY_PATH_PREDTYPE_LIST)) {
1746 if ((u < LY_ARRAY_COUNT(path) - 1) || !(options & LYD_NEW_PATH_OPAQ)) {
Michal Vasko6741dc62021-02-04 09:27:45 +01001747 LOG_LOCSET(schema, NULL, NULL, NULL);
1748 LOGVAL(schema->module->ctx, LYVE_XPATH, "Predicate missing for %s \"%s\" in path \"%s\".",
1749 lys_nodetype2str(schema->nodetype), schema->name, str_path);
1750 LOG_LOCBACK(1, 0, 0, 0);
1751 return LY_EINVAL;
Michal Vaskoe78faec2021-04-08 17:24:43 +02001752 } /* else creating an opaque list */
1753 } else if ((schema->nodetype == LYS_LEAFLIST) && (path[u].pred_type != LY_PATH_PREDTYPE_LEAFLIST)) {
Michal Vaskoe78faec2021-04-08 17:24:43 +02001754 r = LY_SUCCESS;
1755 if (options & LYD_NEW_PATH_OPAQ) {
Radek Krejci09c77442021-04-26 11:10:34 +02001756 r = lyd_value_validate(NULL, schema, value, value_len, NULL, NULL, NULL);
Michal Vaskoe78faec2021-04-08 17:24:43 +02001757 }
1758 if (!r) {
1759 /* store the new predicate so that it is used when searching for this instance */
1760 path[u].pred_type = LY_PATH_PREDTYPE_LEAFLIST;
1761 LY_ARRAY_NEW_RET(schema->module->ctx, path[u].predicates, pred, LY_EMEM);
1762
1763 LY_CHECK_RET(lyd_value_store(schema->module->ctx, &pred->value,
Radek Krejci09c77442021-04-26 11:10:34 +02001764 ((struct lysc_node_leaflist *)schema)->type, value, value_len, NULL, format, NULL,
Michal Vaskoe78faec2021-04-08 17:24:43 +02001765 LYD_HINT_DATA, schema, NULL));
1766 ++((struct lysc_type *)pred->value.realtype)->refcount;
1767 } /* else we have opaq flag and the value is not valid, leave no predicate and then create an opaque node */
1768 }
Michal Vasko6741dc62021-02-04 09:27:45 +01001769 }
1770
Michal Vaskoe78faec2021-04-08 17:24:43 +02001771 if (create) {
1772 /* hide the nodes that should always be created so they are not found */
1773 while (new_count < LY_ARRAY_COUNT(path)) {
1774 LY_ARRAY_DECREMENT(path);
1775 }
Michal Vasko6741dc62021-02-04 09:27:45 +01001776 }
1777
Michal Vaskoe78faec2021-04-08 17:24:43 +02001778 return LY_SUCCESS;
Michal Vasko6741dc62021-02-04 09:27:45 +01001779}
1780
Radek Krejci95ccd1b2021-03-12 14:57:22 +01001781/**
1782 * @brief Create a new node in the data tree based on a path. All node types can be created.
1783 *
Michal Vaskoa918a632021-07-02 11:53:36 +02001784 * If @p path points to a list key, the key value from the predicate is used and @p value is ignored.
1785 * Also, if a leaf-list is being created and both a predicate is defined in @p path
Radek Krejci95ccd1b2021-03-12 14:57:22 +01001786 * and @p value is set, the predicate is preferred.
1787 *
1788 * For key-less lists and state leaf-lists, positional predicates can be used. If no preciate is used for these
1789 * nodes, they are always created.
1790 *
1791 * @param[in] parent Data parent to add to/modify, can be NULL. Note that in case a first top-level sibling is used,
1792 * it may no longer be first if @p path is absolute and starts with a non-existing top-level node inserted
1793 * before @p parent. Use ::lyd_first_sibling() to adjust @p parent in these cases.
1794 * @param[in] ctx libyang context, must be set if @p parent is NULL.
1795 * @param[in] ext Extension instance where the node being created is defined. This argument takes effect only for absolute
1796 * path or when the relative paths touches document root (top-level). In such cases the present extension instance replaces
1797 * searching for the appropriate module.
1798 * @param[in] path [Path](@ref howtoXPath) to create.
Radek Krejci09c77442021-04-26 11:10:34 +02001799 * @param[in] value Value of the new leaf/leaf-list (const char *) in ::LY_VALUE_JSON format. If creating an
1800 * anyxml/anydata node, the expected type depends on @p value_type. For other node types, it should be NULL.
1801 * @param[in] value_len Length of @p value in bytes. May be 0 if @p value is a zero-terminated string. Ignored when
1802 * creating anyxml/anydata nodes.
Radek Krejci95ccd1b2021-03-12 14:57:22 +01001803 * @param[in] value_type Anyxml/anydata node @p value type.
1804 * @param[in] options Bitmask of options, see @ref pathoptions.
1805 * @param[out] new_parent Optional first parent node created. If only one node was created, equals to @p new_node.
1806 * @param[out] new_node Optional last node created.
1807 * @return LY_ERR value.
1808 */
1809static LY_ERR
1810lyd_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 +02001811 const void *value, size_t value_len, LYD_ANYDATA_VALUETYPE value_type, uint32_t options,
Radek Krejci95ccd1b2021-03-12 14:57:22 +01001812 struct lyd_node **new_parent, struct lyd_node **new_node)
Michal Vasko00cbf532020-06-15 13:58:47 +02001813{
1814 LY_ERR ret = LY_SUCCESS, r;
1815 struct lyxp_expr *exp = NULL;
1816 struct ly_path *p = NULL;
1817 struct lyd_node *nparent = NULL, *nnode = NULL, *node = NULL, *cur_parent;
1818 const struct lysc_node *schema;
Michal Vaskoe78faec2021-04-08 17:24:43 +02001819 const struct lyd_value *val = NULL;
Radek Krejci910d0132021-03-05 14:19:31 +01001820 LY_ARRAY_COUNT_TYPE path_idx = 0, orig_count = 0;
Radek Krejci09c77442021-04-26 11:10:34 +02001821 LY_VALUE_FORMAT format;
Michal Vasko00cbf532020-06-15 13:58:47 +02001822
Radek Krejci95ccd1b2021-03-12 14:57:22 +01001823 assert(parent || ctx);
Radek Krejci09c77442021-04-26 11:10:34 +02001824 assert(path && ((path[0] == '/') || parent));
1825 assert(!(options & LYD_NEW_PATH_BIN_VALUE) || !(options & LYD_NEW_PATH_CANON_VALUE));
Michal Vasko00cbf532020-06-15 13:58:47 +02001826
1827 if (!ctx) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02001828 ctx = LYD_CTX(parent);
Michal Vasko00cbf532020-06-15 13:58:47 +02001829 }
Radek Krejci09c77442021-04-26 11:10:34 +02001830 if (value && !value_len) {
1831 value_len = strlen(value);
1832 }
1833 if (options & LYD_NEW_PATH_BIN_VALUE) {
1834 format = LY_VALUE_LYB;
1835 } else if (options & LYD_NEW_PATH_CANON_VALUE) {
1836 format = LY_VALUE_CANON;
1837 } else {
1838 format = LY_VALUE_JSON;
1839 }
Michal Vasko00cbf532020-06-15 13:58:47 +02001840
1841 /* parse path */
Michal Vaskoed725d72021-06-23 12:03:45 +02001842 LY_CHECK_GOTO(ret = ly_path_parse(ctx, NULL, path, strlen(path), 0, LY_PATH_BEGIN_EITHER, LY_PATH_PREFIX_OPTIONAL,
1843 LY_PATH_PRED_SIMPLE, &exp), cleanup);
Michal Vasko00cbf532020-06-15 13:58:47 +02001844
1845 /* compile path */
Michal Vaskoed725d72021-06-23 12:03:45 +02001846 LY_CHECK_GOTO(ret = ly_path_compile(ctx, NULL, parent ? parent->schema : NULL, ext, exp,
Radek Krejci8df109d2021-04-23 12:19:08 +02001847 options & LYD_NEW_PATH_OUTPUT ? LY_PATH_OPER_OUTPUT : LY_PATH_OPER_INPUT, LY_PATH_TARGET_MANY, LY_VALUE_JSON,
Michal Vaskoed725d72021-06-23 12:03:45 +02001848 NULL, &p), cleanup);
Michal Vasko00cbf532020-06-15 13:58:47 +02001849
Michal Vasko6741dc62021-02-04 09:27:45 +01001850 /* check the compiled path before searching existing nodes, it may be shortened */
1851 orig_count = LY_ARRAY_COUNT(p);
Radek Krejci09c77442021-04-26 11:10:34 +02001852 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 +02001853
1854 /* try to find any existing nodes in the path */
1855 if (parent) {
1856 ret = ly_path_eval_partial(p, parent, &path_idx, &node);
1857 if (ret == LY_SUCCESS) {
Michal Vasko6741dc62021-02-04 09:27:45 +01001858 if (orig_count == LY_ARRAY_COUNT(p)) {
1859 /* the node exists, are we supposed to update it or is it just a default? */
1860 if (!(options & LYD_NEW_PATH_UPDATE) && !(node->flags & LYD_DEFAULT)) {
1861 LOG_LOCSET(NULL, node, NULL, NULL);
1862 LOGVAL(ctx, LYVE_REFERENCE, "Path \"%s\" already exists", path);
1863 LOG_LOCBACK(0, 1, 0, 0);
1864 ret = LY_EEXIST;
1865 goto cleanup;
1866 }
Michal Vasko00cbf532020-06-15 13:58:47 +02001867
Michal Vasko6741dc62021-02-04 09:27:45 +01001868 /* update the existing node */
Radek Krejci09c77442021-04-26 11:10:34 +02001869 ret = lyd_new_path_update(node, value, value_len, value_type, format, &nparent, &nnode);
Michal Vasko6741dc62021-02-04 09:27:45 +01001870 goto cleanup;
1871 } /* else we were not searching for the whole path */
Michal Vasko00cbf532020-06-15 13:58:47 +02001872 } else if (ret == LY_EINCOMPLETE) {
1873 /* some nodes were found, adjust the iterator to the next segment */
1874 ++path_idx;
1875 } else if (ret == LY_ENOTFOUND) {
1876 /* 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 +01001877 if (lysc_data_parent(p[0].node)) {
Michal Vasko00cbf532020-06-15 13:58:47 +02001878 node = parent;
1879 }
1880 } else {
1881 /* error */
1882 goto cleanup;
1883 }
1884 }
1885
Michal Vasko6741dc62021-02-04 09:27:45 +01001886 /* restore the full path for creating new nodes */
1887 while (orig_count > LY_ARRAY_COUNT(p)) {
1888 LY_ARRAY_INCREMENT(p);
1889 }
1890
Michal Vasko00cbf532020-06-15 13:58:47 +02001891 /* create all the non-existing nodes in a loop */
Michal Vaskod989ba02020-08-24 10:59:24 +02001892 for ( ; path_idx < LY_ARRAY_COUNT(p); ++path_idx) {
Michal Vasko00cbf532020-06-15 13:58:47 +02001893 cur_parent = node;
1894 schema = p[path_idx].node;
1895
1896 switch (schema->nodetype) {
1897 case LYS_LIST:
Michal Vaskoe78faec2021-04-08 17:24:43 +02001898 if (lysc_is_dup_inst_list(schema)) {
1899 /* create key-less list instance */
1900 LY_CHECK_GOTO(ret = lyd_create_inner(schema, &node), cleanup);
1901 } else if ((options & LYD_NEW_PATH_OPAQ) && (p[path_idx].pred_type == LY_PATH_PREDTYPE_NONE)) {
1902 /* creating opaque list without keys */
1903 LY_CHECK_GOTO(ret = lyd_create_opaq(ctx, schema->name, strlen(schema->name), NULL, 0,
Radek Krejci8df109d2021-04-23 12:19:08 +02001904 schema->module->name, strlen(schema->module->name), NULL, 0, NULL, LY_VALUE_JSON, NULL,
Michal Vaskoe78faec2021-04-08 17:24:43 +02001905 LYD_NODEHINT_LIST, &node), cleanup);
1906 } else {
1907 /* create standard list instance */
1908 assert(p[path_idx].pred_type == LY_PATH_PREDTYPE_LIST);
1909 LY_CHECK_GOTO(ret = lyd_create_list(schema, p[path_idx].predicates, &node), cleanup);
Michal Vasko00cbf532020-06-15 13:58:47 +02001910 }
Michal Vaskoe78faec2021-04-08 17:24:43 +02001911 break;
Michal Vasko00cbf532020-06-15 13:58:47 +02001912 case LYS_CONTAINER:
1913 case LYS_NOTIF:
1914 case LYS_RPC:
1915 case LYS_ACTION:
1916 LY_CHECK_GOTO(ret = lyd_create_inner(schema, &node), cleanup);
1917 break;
1918 case LYS_LEAFLIST:
Michal Vaskoe78faec2021-04-08 17:24:43 +02001919 if ((options & LYD_NEW_PATH_OPAQ) && (p[path_idx].pred_type != LY_PATH_PREDTYPE_LEAFLIST)) {
1920 /* we have not checked this only for dup-inst lists, otherwise it must be opaque */
1921 r = LY_EVALID;
1922 if (lysc_is_dup_inst_list(schema)) {
1923 /* validate value */
Radek Krejci09c77442021-04-26 11:10:34 +02001924 r = lyd_value_validate(NULL, schema, value ? value : "", value_len, NULL, NULL, NULL);
Michal Vaskoe78faec2021-04-08 17:24:43 +02001925 }
1926 if (r && (r != LY_EINCOMPLETE)) {
1927 /* creating opaque leaf-list */
Radek Krejci09c77442021-04-26 11:10:34 +02001928 LY_CHECK_GOTO(ret = lyd_create_opaq(ctx, schema->name, strlen(schema->name), value, value_len,
1929 schema->module->name, strlen(schema->module->name), NULL, 0, NULL, format, NULL,
1930 LYD_NODEHINT_LEAFLIST, &node), cleanup);
Michal Vaskoe78faec2021-04-08 17:24:43 +02001931 break;
1932 }
1933 }
1934
1935 /* get value to set */
1936 if (p[path_idx].pred_type == LY_PATH_PREDTYPE_LEAFLIST) {
1937 val = &p[path_idx].predicates[0].value;
Michal Vaskoe78faec2021-04-08 17:24:43 +02001938 }
1939
1940 /* create a leaf-list instance */
1941 if (val) {
Michal Vasko00cbf532020-06-15 13:58:47 +02001942 LY_CHECK_GOTO(ret = lyd_create_term2(schema, &p[path_idx].predicates[0].value, &node), cleanup);
Michal Vaskoe78faec2021-04-08 17:24:43 +02001943 } else {
Radek Krejci09c77442021-04-26 11:10:34 +02001944 LY_CHECK_GOTO(ret = lyd_create_term(schema, value, value_len, NULL, format, NULL, LYD_HINT_DATA,
1945 NULL, &node), cleanup);
Michal Vasko00cbf532020-06-15 13:58:47 +02001946 }
1947 break;
1948 case LYS_LEAF:
Michal Vasko35880332020-12-08 13:08:12 +01001949 if (lysc_is_key(schema)) {
1950 /* it must have been already created or some error will occur later */
1951 assert(cur_parent);
Michal Vaskoa918a632021-07-02 11:53:36 +02001952 lyd_find_sibling_schema(lyd_child(cur_parent), schema, &node);
1953 assert(node);
Michal Vasko35880332020-12-08 13:08:12 +01001954 goto next_iter;
1955 }
1956
Michal Vaskoe78faec2021-04-08 17:24:43 +02001957 if (options & LYD_NEW_PATH_OPAQ) {
1958 /* validate value */
Radek Krejci09c77442021-04-26 11:10:34 +02001959 r = lyd_value_validate(NULL, schema, value ? value : "", value_len, NULL, NULL, NULL);
Michal Vaskoe78faec2021-04-08 17:24:43 +02001960 if (r && (r != LY_EINCOMPLETE)) {
1961 /* creating opaque leaf */
Radek Krejci09c77442021-04-26 11:10:34 +02001962 LY_CHECK_GOTO(ret = lyd_create_opaq(ctx, schema->name, strlen(schema->name), value, value_len,
1963 schema->module->name, strlen(schema->module->name), NULL, 0, NULL, format, NULL, 0, &node),
1964 cleanup);
Michal Vaskoe78faec2021-04-08 17:24:43 +02001965 break;
1966 }
1967 }
1968
Michal Vaskoe78faec2021-04-08 17:24:43 +02001969 /* create a leaf instance */
Radek Krejci09c77442021-04-26 11:10:34 +02001970 LY_CHECK_GOTO(ret = lyd_create_term(schema, value, value_len, NULL, format, NULL, LYD_HINT_DATA, NULL,
1971 &node), cleanup);
Michal Vasko00cbf532020-06-15 13:58:47 +02001972 break;
1973 case LYS_ANYDATA:
1974 case LYS_ANYXML:
Michal Vasko366a4a12020-12-04 16:23:57 +01001975 LY_CHECK_GOTO(ret = lyd_create_any(schema, value, value_type, 0, &node), cleanup);
Michal Vasko00cbf532020-06-15 13:58:47 +02001976 break;
1977 default:
1978 LOGINT(ctx);
1979 ret = LY_EINT;
1980 goto cleanup;
1981 }
1982
1983 if (cur_parent) {
1984 /* connect to the parent */
Michal Vasko6ee6f432021-07-16 09:49:14 +02001985 lyd_insert_node(cur_parent, NULL, node, 0);
Michal Vasko00cbf532020-06-15 13:58:47 +02001986 } else if (parent) {
1987 /* connect to top-level siblings */
Michal Vasko6ee6f432021-07-16 09:49:14 +02001988 lyd_insert_node(NULL, &parent, node, 0);
Michal Vasko00cbf532020-06-15 13:58:47 +02001989 }
1990
Michal Vasko35880332020-12-08 13:08:12 +01001991next_iter:
Michal Vasko00cbf532020-06-15 13:58:47 +02001992 /* update remembered nodes */
1993 if (!nparent) {
1994 nparent = node;
1995 }
1996 nnode = node;
1997 }
1998
1999cleanup:
2000 lyxp_expr_free(ctx, exp);
Michal Vasko6741dc62021-02-04 09:27:45 +01002001 if (p) {
2002 while (orig_count > LY_ARRAY_COUNT(p)) {
2003 LY_ARRAY_INCREMENT(p);
2004 }
2005 }
Michal Vasko00cbf532020-06-15 13:58:47 +02002006 ly_path_free(ctx, p);
2007 if (!ret) {
2008 /* set out params only on success */
2009 if (new_parent) {
2010 *new_parent = nparent;
2011 }
2012 if (new_node) {
2013 *new_node = nnode;
2014 }
Michal Vaskof4b95ba2020-12-11 08:42:33 +01002015 } else {
2016 lyd_free_tree(nparent);
Michal Vasko00cbf532020-06-15 13:58:47 +02002017 }
2018 return ret;
2019}
2020
Radek Krejci95ccd1b2021-03-12 14:57:22 +01002021API LY_ERR
2022lyd_new_path(struct lyd_node *parent, const struct ly_ctx *ctx, const char *path, const char *value, uint32_t options,
2023 struct lyd_node **node)
2024{
Radek Krejci09c77442021-04-26 11:10:34 +02002025 LY_CHECK_ARG_RET(ctx, parent || ctx, path, (path[0] == '/') || parent,
2026 !(options & LYD_NEW_PATH_BIN_VALUE) || !(options & LYD_NEW_PATH_CANON_VALUE), LY_EINVAL);
2027
2028 return lyd_new_path_(parent, ctx, NULL, path, value, 0, LYD_ANYDATA_STRING, options, node, NULL);
Radek Krejci95ccd1b2021-03-12 14:57:22 +01002029}
2030
2031API LY_ERR
2032lyd_new_path2(struct lyd_node *parent, const struct ly_ctx *ctx, const char *path, const void *value,
Radek Krejci09c77442021-04-26 11:10:34 +02002033 size_t value_len, LYD_ANYDATA_VALUETYPE value_type, uint32_t options, struct lyd_node **new_parent,
2034 struct lyd_node **new_node)
Radek Krejci95ccd1b2021-03-12 14:57:22 +01002035{
Radek Krejci09c77442021-04-26 11:10:34 +02002036 LY_CHECK_ARG_RET(ctx, parent || ctx, path, (path[0] == '/') || parent,
2037 !(options & LYD_NEW_PATH_BIN_VALUE) || !(options & LYD_NEW_PATH_CANON_VALUE), LY_EINVAL);
2038 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 +01002039}
2040
2041API LY_ERR
2042lyd_new_ext_path(struct lyd_node *parent, const struct lysc_ext_instance *ext, const char *path, const void *value,
2043 uint32_t options, struct lyd_node **node)
2044{
2045 const struct ly_ctx *ctx = ext ? ext->module->ctx : NULL;
2046
Radek Krejci09c77442021-04-26 11:10:34 +02002047 LY_CHECK_ARG_RET(ctx, ext, path, (path[0] == '/') || parent,
2048 !(options & LYD_NEW_PATH_BIN_VALUE) || !(options & LYD_NEW_PATH_CANON_VALUE), LY_EINVAL);
2049 return lyd_new_path_(parent, ctx, ext, path, value, 0, LYD_ANYDATA_STRING, options, node, NULL);
Radek Krejci95ccd1b2021-03-12 14:57:22 +01002050}
2051
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002052LY_ERR
2053lyd_new_implicit_r(struct lyd_node *parent, struct lyd_node **first, const struct lysc_node *sparent,
Radek Krejci4f2e3e52021-03-30 14:20:28 +02002054 const struct lys_module *mod, struct ly_set *node_when, struct ly_set *node_exts, struct ly_set *node_types,
2055 uint32_t impl_opts, struct lyd_node **diff)
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002056{
2057 LY_ERR ret;
Michal Vaskod1e53b92021-01-28 13:11:06 +01002058 const struct lysc_node *iter = NULL;
Radek Krejci2b18bf12020-11-06 11:20:20 +01002059 struct lyd_node *node = NULL;
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002060 struct lyd_value **dflts;
2061 LY_ARRAY_COUNT_TYPE u;
Michal Vasko630d9892020-12-08 17:11:08 +01002062 uint32_t getnext_opts;
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002063
2064 assert(first && (parent || sparent || mod));
2065
2066 if (!sparent && parent) {
2067 sparent = parent->schema;
2068 }
2069
Michal Vasko630d9892020-12-08 17:11:08 +01002070 getnext_opts = LYS_GETNEXT_WITHCHOICE;
2071 if (impl_opts & LYD_IMPLICIT_OUTPUT) {
2072 getnext_opts |= LYS_GETNEXT_OUTPUT;
2073 }
2074
2075 while ((iter = lys_getnext(iter, sparent, mod ? mod->compiled : NULL, getnext_opts))) {
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002076 if ((impl_opts & LYD_IMPLICIT_NO_STATE) && (iter->flags & LYS_CONFIG_R)) {
2077 continue;
Michal Vasko44b19a12020-08-07 09:21:30 +02002078 } else if ((impl_opts & LYD_IMPLICIT_NO_CONFIG) && (iter->flags & LYS_CONFIG_W)) {
2079 continue;
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002080 }
2081
2082 switch (iter->nodetype) {
2083 case LYS_CHOICE:
Michal Vaskobcf4d7d2020-11-18 18:16:49 +01002084 node = lys_getnext_data(NULL, *first, NULL, iter, NULL);
2085 if (!node && ((struct lysc_node_choice *)iter)->dflt) {
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002086 /* create default case data */
Michal Vasko14ed9cd2021-01-28 14:16:25 +01002087 LY_CHECK_RET(lyd_new_implicit_r(parent, first, &((struct lysc_node_choice *)iter)->dflt->node,
Radek Krejci4f2e3e52021-03-30 14:20:28 +02002088 NULL, node_when, node_exts, node_types, impl_opts, diff));
Michal Vaskobcf4d7d2020-11-18 18:16:49 +01002089 } else if (node) {
2090 /* create any default data in the existing case */
2091 assert(node->schema->parent->nodetype == LYS_CASE);
Radek Krejci4f2e3e52021-03-30 14:20:28 +02002092 LY_CHECK_RET(lyd_new_implicit_r(parent, first, node->schema->parent, NULL, node_when, node_exts, node_types,
Michal Vaskobcf4d7d2020-11-18 18:16:49 +01002093 impl_opts, diff));
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002094 }
2095 break;
2096 case LYS_CONTAINER:
2097 if (!(iter->flags & LYS_PRESENCE) && lyd_find_sibling_val(*first, iter, NULL, 0, NULL)) {
2098 /* create default NP container */
2099 LY_CHECK_RET(lyd_create_inner(iter, &node));
Michal Vaskof4d67ea2021-03-31 13:53:21 +02002100 node->flags = LYD_DEFAULT | (lysc_has_when(iter) ? LYD_WHEN_TRUE : 0);
Michal Vasko6ee6f432021-07-16 09:49:14 +02002101 lyd_insert_node(parent, first, node, 0);
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002102
Michal Vaskof4d67ea2021-03-31 13:53:21 +02002103 if (lysc_has_when(iter) && node_when) {
Michal Vaskoe16c7b72021-02-26 10:39:06 +01002104 /* remember to resolve when */
2105 LY_CHECK_RET(ly_set_add(node_when, node, 1, NULL));
2106 }
Radek Krejci4f2e3e52021-03-30 14:20:28 +02002107 if (node_exts) {
2108 /* remember to call all the extension's validation callbacks */
2109 LY_CHECK_RET(lysc_node_ext_tovalidate(node_exts, node));
2110 }
Michal Vaskoe49b6322020-11-05 17:38:36 +01002111 if (diff) {
2112 /* add into diff */
2113 LY_CHECK_RET(lyd_val_diff_add(node, LYD_DIFF_OP_CREATE, diff));
2114 }
2115
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002116 /* create any default children */
Radek Krejci4f2e3e52021-03-30 14:20:28 +02002117 LY_CHECK_RET(lyd_new_implicit_r(node, lyd_node_child_p(node), NULL, NULL, node_when, node_exts, node_types,
Michal Vasko69730152020-10-09 16:30:07 +02002118 impl_opts, diff));
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002119 }
2120 break;
2121 case LYS_LEAF:
Michal Vasko69730152020-10-09 16:30:07 +02002122 if (!(impl_opts & LYD_IMPLICIT_NO_DEFAULTS) && ((struct lysc_node_leaf *)iter)->dflt &&
2123 lyd_find_sibling_val(*first, iter, NULL, 0, NULL)) {
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002124 /* create default leaf */
2125 ret = lyd_create_term2(iter, ((struct lysc_node_leaf *)iter)->dflt, &node);
2126 if (ret == LY_EINCOMPLETE) {
2127 if (node_types) {
2128 /* remember to resolve type */
Radek Krejci3d92e442020-10-12 12:48:13 +02002129 LY_CHECK_RET(ly_set_add(node_types, node, 1, NULL));
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002130 }
2131 } else if (ret) {
2132 return ret;
2133 }
Michal Vaskof4d67ea2021-03-31 13:53:21 +02002134 node->flags = LYD_DEFAULT | (lysc_has_when(iter) ? LYD_WHEN_TRUE : 0);
Michal Vasko6ee6f432021-07-16 09:49:14 +02002135 lyd_insert_node(parent, first, node, 0);
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002136
Michal Vaskof4d67ea2021-03-31 13:53:21 +02002137 if (lysc_has_when(iter) && node_when) {
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002138 /* remember to resolve when */
Radek Krejci3d92e442020-10-12 12:48:13 +02002139 LY_CHECK_RET(ly_set_add(node_when, node, 1, NULL));
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002140 }
Radek Krejci4f2e3e52021-03-30 14:20:28 +02002141 if (node_exts) {
2142 /* remember to call all the extension's validation callbacks */
2143 LY_CHECK_RET(lysc_node_ext_tovalidate(node_exts, node));
2144 }
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002145 if (diff) {
2146 /* add into diff */
2147 LY_CHECK_RET(lyd_val_diff_add(node, LYD_DIFF_OP_CREATE, diff));
2148 }
2149 }
2150 break;
2151 case LYS_LEAFLIST:
Michal Vasko69730152020-10-09 16:30:07 +02002152 if (!(impl_opts & LYD_IMPLICIT_NO_DEFAULTS) && ((struct lysc_node_leaflist *)iter)->dflts &&
2153 lyd_find_sibling_val(*first, iter, NULL, 0, NULL)) {
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002154 /* create all default leaf-lists */
2155 dflts = ((struct lysc_node_leaflist *)iter)->dflts;
2156 LY_ARRAY_FOR(dflts, u) {
2157 ret = lyd_create_term2(iter, dflts[u], &node);
2158 if (ret == LY_EINCOMPLETE) {
2159 if (node_types) {
2160 /* remember to resolve type */
Radek Krejci3d92e442020-10-12 12:48:13 +02002161 LY_CHECK_RET(ly_set_add(node_types, node, 1, NULL));
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002162 }
2163 } else if (ret) {
2164 return ret;
2165 }
Michal Vaskof4d67ea2021-03-31 13:53:21 +02002166 node->flags = LYD_DEFAULT | (lysc_has_when(iter) ? LYD_WHEN_TRUE : 0);
Michal Vasko6ee6f432021-07-16 09:49:14 +02002167 lyd_insert_node(parent, first, node, 0);
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002168
Michal Vaskof4d67ea2021-03-31 13:53:21 +02002169 if (lysc_has_when(iter) && node_when) {
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002170 /* remember to resolve when */
Radek Krejci3d92e442020-10-12 12:48:13 +02002171 LY_CHECK_RET(ly_set_add(node_when, node, 1, NULL));
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002172 }
Radek Krejci4f2e3e52021-03-30 14:20:28 +02002173 if (node_exts) {
2174 /* remember to call all the extension's validation callbacks */
2175 LY_CHECK_RET(lysc_node_ext_tovalidate(node_exts, node));
2176 }
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002177 if (diff) {
2178 /* add into diff */
2179 LY_CHECK_RET(lyd_val_diff_add(node, LYD_DIFF_OP_CREATE, diff));
2180 }
2181 }
2182 }
2183 break;
2184 default:
2185 /* without defaults */
2186 break;
2187 }
2188 }
2189
2190 return LY_SUCCESS;
2191}
2192
2193API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02002194lyd_new_implicit_tree(struct lyd_node *tree, uint32_t implicit_options, struct lyd_node **diff)
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002195{
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002196 LY_ERR ret = LY_SUCCESS;
Michal Vasko3488ada2020-12-03 14:18:19 +01002197 struct lyd_node *node;
Radek Krejci4f2e3e52021-03-30 14:20:28 +02002198 struct ly_set node_when = {0}, node_exts = {0};
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002199
2200 LY_CHECK_ARG_RET(NULL, tree, LY_EINVAL);
2201 if (diff) {
2202 *diff = NULL;
2203 }
2204
Michal Vasko56daf732020-08-10 10:57:18 +02002205 LYD_TREE_DFS_BEGIN(tree, node) {
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002206 /* skip added default nodes */
Michal Vasko69730152020-10-09 16:30:07 +02002207 if (((node->flags & (LYD_DEFAULT | LYD_NEW)) != (LYD_DEFAULT | LYD_NEW)) &&
2208 (node->schema->nodetype & LYD_NODE_INNER)) {
Radek Krejci4f2e3e52021-03-30 14:20:28 +02002209 LY_CHECK_GOTO(ret = lyd_new_implicit_r(node, lyd_node_child_p(node), NULL, NULL, &node_when, &node_exts,
Michal Vaskoc43c8ab2021-03-05 13:32:44 +01002210 NULL, implicit_options, diff), cleanup);
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002211 }
2212
Michal Vasko56daf732020-08-10 10:57:18 +02002213 LYD_TREE_DFS_END(tree, node);
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002214 }
2215
Michal Vasko3488ada2020-12-03 14:18:19 +01002216 /* resolve when and remove any invalid defaults */
Radek Krejci4f2e3e52021-03-30 14:20:28 +02002217 LY_CHECK_GOTO(ret = lyd_validate_unres(&tree, NULL, &node_when, &node_exts, NULL, NULL, diff), cleanup);
Michal Vasko3488ada2020-12-03 14:18:19 +01002218
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002219cleanup:
Michal Vasko3488ada2020-12-03 14:18:19 +01002220 ly_set_erase(&node_when, NULL);
Radek Krejci4f2e3e52021-03-30 14:20:28 +02002221 ly_set_erase(&node_exts, NULL);
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002222 if (ret && diff) {
2223 lyd_free_all(*diff);
2224 *diff = NULL;
2225 }
2226 return ret;
2227}
2228
2229API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02002230lyd_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 +02002231{
2232 const struct lys_module *mod;
2233 struct lyd_node *d = NULL;
2234 uint32_t i = 0;
2235 LY_ERR ret = LY_SUCCESS;
2236
2237 LY_CHECK_ARG_RET(ctx, tree, *tree || ctx, LY_EINVAL);
2238 if (diff) {
2239 *diff = NULL;
2240 }
2241 if (!ctx) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02002242 ctx = LYD_CTX(*tree);
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002243 }
2244
2245 /* add nodes for each module one-by-one */
2246 while ((mod = ly_ctx_get_module_iter(ctx, &i))) {
2247 if (!mod->implemented) {
2248 continue;
2249 }
2250
2251 LY_CHECK_GOTO(ret = lyd_new_implicit_module(tree, mod, implicit_options, diff ? &d : NULL), cleanup);
2252 if (d) {
2253 /* merge into one diff */
2254 lyd_insert_sibling(*diff, d, diff);
2255
2256 d = NULL;
2257 }
2258 }
2259
2260cleanup:
2261 if (ret && diff) {
2262 lyd_free_all(*diff);
2263 *diff = NULL;
2264 }
2265 return ret;
2266}
2267
2268API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02002269lyd_new_implicit_module(struct lyd_node **tree, const struct lys_module *module, uint32_t implicit_options, struct lyd_node **diff)
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002270{
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002271 LY_ERR ret = LY_SUCCESS;
Michal Vasko3488ada2020-12-03 14:18:19 +01002272 struct lyd_node *root, *d = NULL;
Radek Krejci4f2e3e52021-03-30 14:20:28 +02002273 struct ly_set node_when = {0}, node_exts = {0};
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002274
2275 LY_CHECK_ARG_RET(NULL, tree, module, LY_EINVAL);
2276 if (diff) {
2277 *diff = NULL;
2278 }
2279
2280 /* add all top-level defaults for this module */
Radek Krejci4f2e3e52021-03-30 14:20:28 +02002281 LY_CHECK_GOTO(ret = lyd_new_implicit_r(NULL, tree, NULL, module, &node_when, &node_exts, NULL, implicit_options, diff), cleanup);
Michal Vasko3488ada2020-12-03 14:18:19 +01002282
2283 /* resolve when and remove any invalid defaults */
Radek Krejci4f2e3e52021-03-30 14:20:28 +02002284 LY_CHECK_GOTO(ret = lyd_validate_unres(tree, module, &node_when, &node_exts, NULL, NULL, diff), cleanup);
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002285
2286 /* process nested nodes */
2287 LY_LIST_FOR(*tree, root) {
2288 /* skip added default nodes */
2289 if ((root->flags & (LYD_DEFAULT | LYD_NEW)) != (LYD_DEFAULT | LYD_NEW)) {
2290 LY_CHECK_GOTO(ret = lyd_new_implicit_tree(root, implicit_options, diff ? &d : NULL), cleanup);
2291
2292 if (d) {
2293 /* merge into one diff */
2294 lyd_insert_sibling(*diff, d, diff);
2295
2296 d = NULL;
2297 }
2298 }
2299 }
2300
2301cleanup:
Michal Vasko3488ada2020-12-03 14:18:19 +01002302 ly_set_erase(&node_when, NULL);
Radek Krejci4f2e3e52021-03-30 14:20:28 +02002303 ly_set_erase(&node_exts, NULL);
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002304 if (ret && diff) {
2305 lyd_free_all(*diff);
2306 *diff = NULL;
2307 }
2308 return ret;
2309}
2310
Michal Vasko90932a92020-02-12 14:33:03 +01002311struct lyd_node *
Michal Vaskob104f112020-07-17 09:54:54 +02002312lyd_insert_get_next_anchor(const struct lyd_node *first_sibling, const struct lyd_node *new_node)
Michal Vasko90932a92020-02-12 14:33:03 +01002313{
Michal Vaskob104f112020-07-17 09:54:54 +02002314 const struct lysc_node *schema, *sparent;
Michal Vasko90932a92020-02-12 14:33:03 +01002315 struct lyd_node *match = NULL;
Radek Krejci857189e2020-09-01 13:26:36 +02002316 ly_bool found;
Michal Vasko93b16062020-12-09 18:14:18 +01002317 uint32_t getnext_opts;
Michal Vasko90932a92020-02-12 14:33:03 +01002318
Michal Vaskob104f112020-07-17 09:54:54 +02002319 assert(new_node);
2320
2321 if (!first_sibling || !new_node->schema) {
2322 /* insert at the end, no next anchor */
Michal Vasko90932a92020-02-12 14:33:03 +01002323 return NULL;
2324 }
2325
Michal Vasko93b16062020-12-09 18:14:18 +01002326 getnext_opts = 0;
Michal Vaskod1e53b92021-01-28 13:11:06 +01002327 if (new_node->schema->flags & LYS_IS_OUTPUT) {
Michal Vasko93b16062020-12-09 18:14:18 +01002328 getnext_opts = LYS_GETNEXT_OUTPUT;
2329 }
2330
Michal Vaskoa2756f02021-07-09 13:20:28 +02002331 if (first_sibling->parent && first_sibling->parent->schema && first_sibling->parent->children_ht) {
Michal Vaskob104f112020-07-17 09:54:54 +02002332 /* find the anchor using hashes */
2333 sparent = first_sibling->parent->schema;
Michal Vasko93b16062020-12-09 18:14:18 +01002334 schema = lys_getnext(new_node->schema, sparent, NULL, getnext_opts);
Michal Vaskob104f112020-07-17 09:54:54 +02002335 while (schema) {
2336 /* keep trying to find the first existing instance of the closest following schema sibling,
2337 * otherwise return NULL - inserting at the end */
2338 if (!lyd_find_sibling_schema(first_sibling, schema, &match)) {
2339 break;
2340 }
2341
Michal Vasko93b16062020-12-09 18:14:18 +01002342 schema = lys_getnext(schema, sparent, NULL, getnext_opts);
Michal Vaskob104f112020-07-17 09:54:54 +02002343 }
2344 } else {
2345 /* find the anchor without hashes */
2346 match = (struct lyd_node *)first_sibling;
Michal Vasko3a708622021-07-15 14:20:10 +02002347 sparent = lysc_data_parent(new_node->schema);
2348 if (!sparent) {
Michal Vaskob104f112020-07-17 09:54:54 +02002349 /* we are in top-level, skip all the data from preceding modules */
2350 LY_LIST_FOR(match, match) {
2351 if (!match->schema || (strcmp(lyd_owner_module(match)->name, lyd_owner_module(new_node)->name) >= 0)) {
2352 break;
2353 }
2354 }
2355 }
2356
2357 /* get the first schema sibling */
Michal Vasko93b16062020-12-09 18:14:18 +01002358 schema = lys_getnext(NULL, sparent, new_node->schema->module->compiled, getnext_opts);
Michal Vaskob104f112020-07-17 09:54:54 +02002359
2360 found = 0;
2361 LY_LIST_FOR(match, match) {
2362 if (!match->schema || (lyd_owner_module(match) != lyd_owner_module(new_node))) {
2363 /* we have found an opaque node, which must be at the end, so use it OR
2364 * modules do not match, so we must have traversed all the data from new_node module (if any),
2365 * we have found the first node of the next module, that is what we want */
2366 break;
2367 }
2368
2369 /* skip schema nodes until we find the instantiated one */
2370 while (!found) {
2371 if (new_node->schema == schema) {
2372 /* we have found the schema of the new node, continue search to find the first
2373 * data node with a different schema (after our schema) */
2374 found = 1;
2375 break;
2376 }
2377 if (match->schema == schema) {
2378 /* current node (match) is a data node still before the new node, continue search in data */
2379 break;
2380 }
Michal Vasko93b16062020-12-09 18:14:18 +01002381 schema = lys_getnext(schema, sparent, new_node->schema->module->compiled, getnext_opts);
Michal Vaskob104f112020-07-17 09:54:54 +02002382 assert(schema);
2383 }
2384
2385 if (found && (match->schema != new_node->schema)) {
2386 /* find the next node after we have found our node schema data instance */
2387 break;
2388 }
2389 }
Michal Vasko90932a92020-02-12 14:33:03 +01002390 }
2391
2392 return match;
2393}
2394
2395/**
2396 * @brief Insert node after a sibling.
2397 *
Michal Vasko9f96a052020-03-10 09:41:45 +01002398 * Handles inserting into NP containers and key-less lists.
2399 *
Michal Vasko90932a92020-02-12 14:33:03 +01002400 * @param[in] sibling Sibling to insert after.
2401 * @param[in] node Node to insert.
2402 */
2403static void
Michal Vaskof03ed032020-03-04 13:31:44 +01002404lyd_insert_after_node(struct lyd_node *sibling, struct lyd_node *node)
Michal Vasko90932a92020-02-12 14:33:03 +01002405{
Michal Vasko0249f7c2020-03-05 16:36:40 +01002406 struct lyd_node_inner *par;
2407
Michal Vasko90932a92020-02-12 14:33:03 +01002408 assert(!node->next && (node->prev == node));
2409
2410 node->next = sibling->next;
2411 node->prev = sibling;
2412 sibling->next = node;
2413 if (node->next) {
2414 /* sibling had a succeeding node */
2415 node->next->prev = node;
2416 } else {
2417 /* sibling was last, find first sibling and change its prev */
2418 if (sibling->parent) {
2419 sibling = sibling->parent->child;
2420 } else {
Michal Vaskod989ba02020-08-24 10:59:24 +02002421 for ( ; sibling->prev->next != node; sibling = sibling->prev) {}
Michal Vasko90932a92020-02-12 14:33:03 +01002422 }
2423 sibling->prev = node;
2424 }
2425 node->parent = sibling->parent;
Michal Vasko0249f7c2020-03-05 16:36:40 +01002426
Michal Vasko9f96a052020-03-10 09:41:45 +01002427 for (par = node->parent; par; par = par->parent) {
2428 if ((par->flags & LYD_DEFAULT) && !(node->flags & LYD_DEFAULT)) {
2429 /* remove default flags from NP containers */
Michal Vasko0249f7c2020-03-05 16:36:40 +01002430 par->flags &= ~LYD_DEFAULT;
2431 }
2432 }
Michal Vasko90932a92020-02-12 14:33:03 +01002433}
2434
2435/**
2436 * @brief Insert node before a sibling.
2437 *
Michal Vasko9f96a052020-03-10 09:41:45 +01002438 * Handles inserting into NP containers and key-less lists.
2439 *
Michal Vasko90932a92020-02-12 14:33:03 +01002440 * @param[in] sibling Sibling to insert before.
2441 * @param[in] node Node to insert.
2442 */
2443static void
Michal Vaskof03ed032020-03-04 13:31:44 +01002444lyd_insert_before_node(struct lyd_node *sibling, struct lyd_node *node)
Michal Vasko90932a92020-02-12 14:33:03 +01002445{
Michal Vasko0249f7c2020-03-05 16:36:40 +01002446 struct lyd_node_inner *par;
2447
Michal Vasko90932a92020-02-12 14:33:03 +01002448 assert(!node->next && (node->prev == node));
2449
2450 node->next = sibling;
2451 /* covers situation of sibling being first */
2452 node->prev = sibling->prev;
2453 sibling->prev = node;
2454 if (node->prev->next) {
2455 /* sibling had a preceding node */
2456 node->prev->next = node;
2457 } else if (sibling->parent) {
2458 /* sibling was first and we must also change parent child pointer */
2459 sibling->parent->child = node;
2460 }
2461 node->parent = sibling->parent;
Michal Vasko0249f7c2020-03-05 16:36:40 +01002462
Michal Vasko9f96a052020-03-10 09:41:45 +01002463 for (par = node->parent; par; par = par->parent) {
2464 if ((par->flags & LYD_DEFAULT) && !(node->flags & LYD_DEFAULT)) {
2465 /* remove default flags from NP containers */
Michal Vasko0249f7c2020-03-05 16:36:40 +01002466 par->flags &= ~LYD_DEFAULT;
2467 }
2468 }
Michal Vasko90932a92020-02-12 14:33:03 +01002469}
2470
2471/**
Michal Vaskob104f112020-07-17 09:54:54 +02002472 * @brief Insert node as the first and only child of a parent.
Michal Vasko90932a92020-02-12 14:33:03 +01002473 *
Michal Vasko9f96a052020-03-10 09:41:45 +01002474 * Handles inserting into NP containers and key-less lists.
2475 *
Michal Vasko90932a92020-02-12 14:33:03 +01002476 * @param[in] parent Parent to insert into.
2477 * @param[in] node Node to insert.
2478 */
2479static void
Michal Vaskob104f112020-07-17 09:54:54 +02002480lyd_insert_only_child(struct lyd_node *parent, struct lyd_node *node)
Michal Vasko90932a92020-02-12 14:33:03 +01002481{
2482 struct lyd_node_inner *par;
2483
Radek Krejcia1c1e542020-09-29 16:06:52 +02002484 assert(parent && !lyd_child(parent) && !node->next && (node->prev == node));
Michal Vasko52927e22020-03-16 17:26:14 +01002485 assert(!parent->schema || (parent->schema->nodetype & LYD_NODE_INNER));
Michal Vasko90932a92020-02-12 14:33:03 +01002486
2487 par = (struct lyd_node_inner *)parent;
2488
Michal Vaskob104f112020-07-17 09:54:54 +02002489 par->child = node;
Michal Vasko90932a92020-02-12 14:33:03 +01002490 node->parent = par;
Michal Vasko0249f7c2020-03-05 16:36:40 +01002491
Michal Vaskod989ba02020-08-24 10:59:24 +02002492 for ( ; par; par = par->parent) {
Michal Vasko9f96a052020-03-10 09:41:45 +01002493 if ((par->flags & LYD_DEFAULT) && !(node->flags & LYD_DEFAULT)) {
2494 /* remove default flags from NP containers */
Michal Vasko0249f7c2020-03-05 16:36:40 +01002495 par->flags &= ~LYD_DEFAULT;
2496 }
2497 }
Michal Vasko751cb4d2020-07-14 12:25:28 +02002498}
Michal Vasko0249f7c2020-03-05 16:36:40 +01002499
Michal Vasko751cb4d2020-07-14 12:25:28 +02002500/**
2501 * @brief Learn whether a list instance has all the keys.
2502 *
2503 * @param[in] list List instance to check.
2504 * @return non-zero if all the keys were found,
2505 * @return 0 otherwise.
2506 */
2507static int
2508lyd_insert_has_keys(const struct lyd_node *list)
2509{
2510 const struct lyd_node *key;
2511 const struct lysc_node *skey = NULL;
2512
2513 assert(list->schema->nodetype == LYS_LIST);
Radek Krejcia1c1e542020-09-29 16:06:52 +02002514 key = lyd_child(list);
Michal Vasko751cb4d2020-07-14 12:25:28 +02002515 while ((skey = lys_getnext(skey, list->schema, NULL, 0)) && (skey->flags & LYS_KEY)) {
2516 if (!key || (key->schema != skey)) {
2517 /* key missing */
2518 return 0;
2519 }
2520
2521 key = key->next;
2522 }
2523
2524 /* all keys found */
2525 return 1;
Michal Vasko90932a92020-02-12 14:33:03 +01002526}
2527
2528void
Michal Vasko6ee6f432021-07-16 09:49:14 +02002529lyd_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 +01002530{
Michal Vaskob104f112020-07-17 09:54:54 +02002531 struct lyd_node *anchor, *first_sibling;
Michal Vasko90932a92020-02-12 14:33:03 +01002532
Michal Vaskob104f112020-07-17 09:54:54 +02002533 /* inserting list without its keys is not supported */
2534 assert((parent || first_sibling_p) && node && (node->hash || !node->schema));
Michal Vaskof756c942020-11-06 17:21:37 +01002535 assert(!parent || !parent->schema ||
2536 (parent->schema->nodetype & (LYS_CONTAINER | LYS_LIST | LYS_RPC | LYS_ACTION | LYS_NOTIF)));
Michal Vasko9b368d32020-02-14 13:53:31 +01002537
Michal Vaskob104f112020-07-17 09:54:54 +02002538 if (!parent && first_sibling_p && (*first_sibling_p) && (*first_sibling_p)->parent) {
Michal Vasko9e685082021-01-29 14:49:09 +01002539 parent = lyd_parent(*first_sibling_p);
Michal Vasko9b368d32020-02-14 13:53:31 +01002540 }
Michal Vasko90932a92020-02-12 14:33:03 +01002541
Michal Vaskob104f112020-07-17 09:54:54 +02002542 /* get first sibling */
Michal Vasko9e685082021-01-29 14:49:09 +01002543 first_sibling = parent ? lyd_child(parent) : *first_sibling_p;
Michal Vasko9f96a052020-03-10 09:41:45 +01002544
Michal Vasko6ee6f432021-07-16 09:49:14 +02002545 if (last) {
2546 /* no next anchor */
2547 anchor = NULL;
2548 } else {
2549 /* find the anchor, our next node, so we can insert before it */
2550 anchor = lyd_insert_get_next_anchor(first_sibling, node);
2551 }
2552
Michal Vaskob104f112020-07-17 09:54:54 +02002553 if (anchor) {
Michal Vasko6ee6f432021-07-16 09:49:14 +02002554 /* insert before the anchor */
Michal Vaskob104f112020-07-17 09:54:54 +02002555 lyd_insert_before_node(anchor, node);
Michal Vasko26123192020-11-09 21:02:34 +01002556 if (!parent && (*first_sibling_p == anchor)) {
2557 /* move first sibling */
2558 *first_sibling_p = node;
2559 }
Michal Vaskob104f112020-07-17 09:54:54 +02002560 } else if (first_sibling) {
Michal Vasko6ee6f432021-07-16 09:49:14 +02002561 /* insert as the last node */
Michal Vaskob104f112020-07-17 09:54:54 +02002562 lyd_insert_after_node(first_sibling->prev, node);
2563 } else if (parent) {
Michal Vasko6ee6f432021-07-16 09:49:14 +02002564 /* insert as the only child */
Michal Vaskob104f112020-07-17 09:54:54 +02002565 lyd_insert_only_child(parent, node);
Michal Vasko90932a92020-02-12 14:33:03 +01002566 } else {
Michal Vasko6ee6f432021-07-16 09:49:14 +02002567 /* insert as the only sibling */
Michal Vaskob104f112020-07-17 09:54:54 +02002568 *first_sibling_p = node;
2569 }
2570
2571 /* insert into parent HT */
2572 lyd_insert_hash(node);
2573
2574 /* finish hashes for our parent, if needed and possible */
Michal Vasko9e8de2d2020-09-01 08:17:10 +02002575 if (node->schema && (node->schema->flags & LYS_KEY) && parent && lyd_insert_has_keys(parent)) {
Michal Vaskob104f112020-07-17 09:54:54 +02002576 lyd_hash(parent);
2577
2578 /* now we can insert even the list into its parent HT */
2579 lyd_insert_hash(parent);
Michal Vasko90932a92020-02-12 14:33:03 +01002580 }
Michal Vasko90932a92020-02-12 14:33:03 +01002581}
2582
Michal Vasko717a4c32020-12-07 09:40:10 +01002583/**
2584 * @brief Check schema place of a node to be inserted.
2585 *
2586 * @param[in] parent Schema node of the parent data node.
2587 * @param[in] sibling Schema node of a sibling data node.
2588 * @param[in] schema Schema node if the data node to be inserted.
2589 * @return LY_SUCCESS on success.
2590 * @return LY_EINVAL if the place is invalid.
2591 */
Michal Vaskof03ed032020-03-04 13:31:44 +01002592static LY_ERR
Michal Vasko717a4c32020-12-07 09:40:10 +01002593lyd_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 +01002594{
2595 const struct lysc_node *par2;
2596
Michal Vasko62ed12d2020-05-21 10:08:25 +02002597 assert(!parent || !(parent->nodetype & (LYS_CASE | LYS_CHOICE)));
Michal Vasko717a4c32020-12-07 09:40:10 +01002598 assert(!sibling || !(sibling->nodetype & (LYS_CASE | LYS_CHOICE)));
2599 assert(!schema || !(schema->nodetype & (LYS_CASE | LYS_CHOICE)));
Michal Vaskof03ed032020-03-04 13:31:44 +01002600
Michal Vasko717a4c32020-12-07 09:40:10 +01002601 if (!schema || (!parent && !sibling)) {
Michal Vasko71e795e2020-12-04 16:27:37 +01002602 /* opaque nodes can be inserted wherever */
2603 return LY_SUCCESS;
2604 }
2605
Michal Vasko717a4c32020-12-07 09:40:10 +01002606 if (!parent) {
2607 parent = lysc_data_parent(sibling);
2608 }
2609
Michal Vaskof03ed032020-03-04 13:31:44 +01002610 /* find schema parent */
Michal Vasko62ed12d2020-05-21 10:08:25 +02002611 par2 = lysc_data_parent(schema);
Michal Vaskof03ed032020-03-04 13:31:44 +01002612
2613 if (parent) {
2614 /* inner node */
2615 if (par2 != parent) {
Michal Vaskob104f112020-07-17 09:54:54 +02002616 LOGERR(schema->module->ctx, LY_EINVAL, "Cannot insert, parent of \"%s\" is not \"%s\".", schema->name,
Michal Vasko69730152020-10-09 16:30:07 +02002617 parent->name);
Michal Vaskof03ed032020-03-04 13:31:44 +01002618 return LY_EINVAL;
2619 }
2620 } else {
2621 /* top-level node */
2622 if (par2) {
Radek Krejcif6d14cb2020-07-02 16:11:45 +02002623 LOGERR(schema->module->ctx, LY_EINVAL, "Cannot insert, node \"%s\" is not top-level.", schema->name);
Michal Vaskof03ed032020-03-04 13:31:44 +01002624 return LY_EINVAL;
2625 }
2626 }
2627
2628 return LY_SUCCESS;
2629}
2630
2631API LY_ERR
Michal Vaskob104f112020-07-17 09:54:54 +02002632lyd_insert_child(struct lyd_node *parent, struct lyd_node *node)
Michal Vaskof03ed032020-03-04 13:31:44 +01002633{
2634 struct lyd_node *iter;
2635
Michal Vasko0ab974d2021-02-24 13:18:26 +01002636 LY_CHECK_ARG_RET(NULL, parent, node, !parent->schema || (parent->schema->nodetype & LYD_NODE_INNER), LY_EINVAL);
Michal Vaskof03ed032020-03-04 13:31:44 +01002637
Michal Vasko717a4c32020-12-07 09:40:10 +01002638 LY_CHECK_RET(lyd_insert_check_schema(parent->schema, NULL, node->schema));
Michal Vaskof03ed032020-03-04 13:31:44 +01002639
Michal Vasko0ab974d2021-02-24 13:18:26 +01002640 if (node->schema && (node->schema->flags & LYS_KEY)) {
Michal Vaskof03ed032020-03-04 13:31:44 +01002641 LOGERR(parent->schema->module->ctx, LY_EINVAL, "Cannot insert key \"%s\".", node->schema->name);
2642 return LY_EINVAL;
2643 }
2644
2645 if (node->parent || node->prev->next) {
2646 lyd_unlink_tree(node);
2647 }
2648
2649 while (node) {
2650 iter = node->next;
2651 lyd_unlink_tree(node);
Michal Vasko6ee6f432021-07-16 09:49:14 +02002652 lyd_insert_node(parent, NULL, node, 0);
Michal Vaskof03ed032020-03-04 13:31:44 +01002653 node = iter;
2654 }
2655 return LY_SUCCESS;
2656}
2657
2658API LY_ERR
Michal Vaskob104f112020-07-17 09:54:54 +02002659lyd_insert_sibling(struct lyd_node *sibling, struct lyd_node *node, struct lyd_node **first)
Michal Vaskob1b5c262020-03-05 14:29:47 +01002660{
2661 struct lyd_node *iter;
2662
Michal Vaskob104f112020-07-17 09:54:54 +02002663 LY_CHECK_ARG_RET(NULL, node, LY_EINVAL);
Michal Vaskob1b5c262020-03-05 14:29:47 +01002664
Michal Vaskob104f112020-07-17 09:54:54 +02002665 if (sibling) {
Michal Vasko717a4c32020-12-07 09:40:10 +01002666 LY_CHECK_RET(lyd_insert_check_schema(NULL, sibling->schema, node->schema));
Michal Vaskob1b5c262020-03-05 14:29:47 +01002667 }
2668
Michal Vasko553d0b52020-12-04 16:27:52 +01002669 if (sibling == node) {
2670 /* we need to keep the connection to siblings so we can insert into them */
2671 sibling = sibling->prev;
2672 }
2673
Michal Vaskob1b5c262020-03-05 14:29:47 +01002674 if (node->parent || node->prev->next) {
2675 lyd_unlink_tree(node);
2676 }
2677
2678 while (node) {
Michal Vasko71e795e2020-12-04 16:27:37 +01002679 if (lysc_is_key(node->schema)) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02002680 LOGERR(LYD_CTX(node), LY_EINVAL, "Cannot insert key \"%s\".", node->schema->name);
Michal Vaskob104f112020-07-17 09:54:54 +02002681 return LY_EINVAL;
2682 }
2683
Michal Vaskob1b5c262020-03-05 14:29:47 +01002684 iter = node->next;
2685 lyd_unlink_tree(node);
Michal Vasko6ee6f432021-07-16 09:49:14 +02002686 lyd_insert_node(NULL, &sibling, node, 0);
Michal Vaskob1b5c262020-03-05 14:29:47 +01002687 node = iter;
2688 }
Michal Vaskob1b5c262020-03-05 14:29:47 +01002689
Michal Vaskob104f112020-07-17 09:54:54 +02002690 if (first) {
2691 /* find the first sibling */
2692 *first = sibling;
2693 while ((*first)->prev->next) {
2694 *first = (*first)->prev;
Michal Vasko0249f7c2020-03-05 16:36:40 +01002695 }
2696 }
2697
2698 return LY_SUCCESS;
2699}
2700
Michal Vaskob1b5c262020-03-05 14:29:47 +01002701API LY_ERR
Michal Vaskof03ed032020-03-04 13:31:44 +01002702lyd_insert_before(struct lyd_node *sibling, struct lyd_node *node)
2703{
Michal Vaskof03ed032020-03-04 13:31:44 +01002704 LY_CHECK_ARG_RET(NULL, sibling, node, LY_EINVAL);
2705
Michal Vasko717a4c32020-12-07 09:40:10 +01002706 LY_CHECK_RET(lyd_insert_check_schema(NULL, sibling->schema, node->schema));
Michal Vaskof03ed032020-03-04 13:31:44 +01002707
Michal Vaskob104f112020-07-17 09:54:54 +02002708 if (!(node->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) || !(node->schema->flags & LYS_ORDBY_USER)) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02002709 LOGERR(LYD_CTX(sibling), LY_EINVAL, "Can be used only for user-ordered nodes.");
Michal Vaskof03ed032020-03-04 13:31:44 +01002710 return LY_EINVAL;
2711 }
2712
Michal Vasko4bc2ce32020-12-08 10:06:16 +01002713 lyd_unlink_tree(node);
2714 lyd_insert_before_node(sibling, node);
2715 lyd_insert_hash(node);
Michal Vaskof03ed032020-03-04 13:31:44 +01002716
Michal Vaskof03ed032020-03-04 13:31:44 +01002717 return LY_SUCCESS;
2718}
2719
2720API LY_ERR
2721lyd_insert_after(struct lyd_node *sibling, struct lyd_node *node)
2722{
Michal Vaskof03ed032020-03-04 13:31:44 +01002723 LY_CHECK_ARG_RET(NULL, sibling, node, LY_EINVAL);
2724
Michal Vasko717a4c32020-12-07 09:40:10 +01002725 LY_CHECK_RET(lyd_insert_check_schema(NULL, sibling->schema, node->schema));
Michal Vaskof03ed032020-03-04 13:31:44 +01002726
Michal Vaskob104f112020-07-17 09:54:54 +02002727 if (!(node->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) || !(node->schema->flags & LYS_ORDBY_USER)) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02002728 LOGERR(LYD_CTX(sibling), LY_EINVAL, "Can be used only for user-ordered nodes.");
Michal Vaskof03ed032020-03-04 13:31:44 +01002729 return LY_EINVAL;
2730 }
2731
Michal Vasko4bc2ce32020-12-08 10:06:16 +01002732 lyd_unlink_tree(node);
2733 lyd_insert_after_node(sibling, node);
2734 lyd_insert_hash(node);
Michal Vaskof03ed032020-03-04 13:31:44 +01002735
Michal Vaskof03ed032020-03-04 13:31:44 +01002736 return LY_SUCCESS;
2737}
2738
2739API void
2740lyd_unlink_tree(struct lyd_node *node)
2741{
2742 struct lyd_node *iter;
2743
2744 if (!node) {
2745 return;
2746 }
2747
Michal Vaskob104f112020-07-17 09:54:54 +02002748 /* update hashes while still linked into the tree */
2749 lyd_unlink_hash(node);
2750
Michal Vaskof03ed032020-03-04 13:31:44 +01002751 /* unlink from siblings */
2752 if (node->prev->next) {
2753 node->prev->next = node->next;
2754 }
2755 if (node->next) {
2756 node->next->prev = node->prev;
2757 } else {
2758 /* unlinking the last node */
2759 if (node->parent) {
2760 iter = node->parent->child;
2761 } else {
2762 iter = node->prev;
2763 while (iter->prev != node) {
2764 iter = iter->prev;
2765 }
2766 }
2767 /* update the "last" pointer from the first node */
2768 iter->prev = node->prev;
2769 }
2770
2771 /* unlink from parent */
2772 if (node->parent) {
2773 if (node->parent->child == node) {
2774 /* the node is the first child */
2775 node->parent->child = node->next;
2776 }
2777
Michal Vaskoab49dbe2020-07-17 12:32:47 +02002778 /* check for NP container whether its last non-default node is not being unlinked */
Michal Vasko69730152020-10-09 16:30:07 +02002779 if (node->parent->schema && (node->parent->schema->nodetype == LYS_CONTAINER) &&
2780 !(node->parent->flags & LYD_DEFAULT) && !(node->parent->schema->flags & LYS_PRESENCE)) {
Michal Vaskoab49dbe2020-07-17 12:32:47 +02002781 LY_LIST_FOR(node->parent->child, iter) {
2782 if ((iter != node) && !(iter->flags & LYD_DEFAULT)) {
2783 break;
2784 }
2785 }
2786 if (!iter) {
2787 node->parent->flags |= LYD_DEFAULT;
2788 }
2789 }
2790
Michal Vaskof03ed032020-03-04 13:31:44 +01002791 node->parent = NULL;
2792 }
2793
2794 node->next = NULL;
2795 node->prev = node;
2796}
2797
Michal Vaskoa5da3292020-08-12 13:10:50 +02002798void
Michal Vasko871a0252020-11-11 18:35:24 +01002799lyd_insert_meta(struct lyd_node *parent, struct lyd_meta *meta, ly_bool clear_dflt)
Radek Krejci1798aae2020-07-14 13:26:06 +02002800{
2801 struct lyd_meta *last, *iter;
2802
2803 assert(parent);
Michal Vaskoa5da3292020-08-12 13:10:50 +02002804
2805 if (!meta) {
2806 return;
2807 }
Radek Krejci1798aae2020-07-14 13:26:06 +02002808
2809 for (iter = meta; iter; iter = iter->next) {
2810 iter->parent = parent;
2811 }
2812
2813 /* insert as the last attribute */
2814 if (parent->meta) {
Radek Krejci1e008d22020-08-17 11:37:37 +02002815 for (last = parent->meta; last->next; last = last->next) {}
Radek Krejci1798aae2020-07-14 13:26:06 +02002816 last->next = meta;
2817 } else {
2818 parent->meta = meta;
2819 }
2820
2821 /* remove default flags from NP containers */
Michal Vasko871a0252020-11-11 18:35:24 +01002822 while (clear_dflt && parent && (parent->schema->nodetype == LYS_CONTAINER) && (parent->flags & LYD_DEFAULT)) {
Radek Krejci1798aae2020-07-14 13:26:06 +02002823 parent->flags &= ~LYD_DEFAULT;
Michal Vasko9e685082021-01-29 14:49:09 +01002824 parent = lyd_parent(parent);
Radek Krejci1798aae2020-07-14 13:26:06 +02002825 }
Radek Krejci1798aae2020-07-14 13:26:06 +02002826}
2827
2828LY_ERR
Michal Vasko9f96a052020-03-10 09:41:45 +01002829lyd_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 +02002830 size_t name_len, const char *value, size_t value_len, ly_bool *dynamic, LY_VALUE_FORMAT format,
Michal Vasko871a0252020-11-11 18:35:24 +01002831 void *prefix_data, uint32_t hints, ly_bool clear_dflt, ly_bool *incomplete)
Michal Vasko90932a92020-02-12 14:33:03 +01002832{
Radek Krejci2efc45b2020-12-22 16:25:44 +01002833 LY_ERR ret = LY_SUCCESS;
Michal Vasko90932a92020-02-12 14:33:03 +01002834 struct lysc_ext_instance *ant = NULL;
Michal Vaskob0534f92021-07-01 13:24:03 +02002835 const struct lysc_type **ant_type;
Michal Vasko9f96a052020-03-10 09:41:45 +01002836 struct lyd_meta *mt, *last;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002837 LY_ARRAY_COUNT_TYPE u;
Michal Vasko90932a92020-02-12 14:33:03 +01002838
Michal Vasko9f96a052020-03-10 09:41:45 +01002839 assert((parent || meta) && mod);
Michal Vasko6f4cbb62020-02-28 11:15:47 +01002840
Radek Krejciddace2c2021-01-08 11:30:56 +01002841 LOG_LOCSET(parent ? parent->schema : NULL, parent, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +01002842
Radek Krejci7eb54ba2020-05-18 16:30:04 +02002843 LY_ARRAY_FOR(mod->compiled->exts, u) {
Radek Krejci3e6632f2021-03-22 22:08:21 +01002844 if ((mod->compiled->exts[u].def->plugin == lyplg_find(LYPLG_EXTENSION, LYEXT_PLUGIN_INTERNAL_ANNOTATION)) &&
Radek Krejci7eb54ba2020-05-18 16:30:04 +02002845 !ly_strncmp(mod->compiled->exts[u].argument, name, name_len)) {
Michal Vasko90932a92020-02-12 14:33:03 +01002846 /* we have the annotation definition */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02002847 ant = &mod->compiled->exts[u];
Michal Vasko90932a92020-02-12 14:33:03 +01002848 break;
2849 }
2850 }
2851 if (!ant) {
2852 /* attribute is not defined as a metadata annotation (RFC 7952) */
Radek Krejci2efc45b2020-12-22 16:25:44 +01002853 LOGVAL(mod->ctx, LYVE_REFERENCE, "Annotation definition for attribute \"%s:%.*s\" not found.",
Radek Krejci422afb12021-03-04 16:38:16 +01002854 mod->name, (int)name_len, name);
Radek Krejci2efc45b2020-12-22 16:25:44 +01002855 ret = LY_EINVAL;
2856 goto cleanup;
Michal Vasko90932a92020-02-12 14:33:03 +01002857 }
2858
Michal Vasko9f96a052020-03-10 09:41:45 +01002859 mt = calloc(1, sizeof *mt);
Radek Krejci2efc45b2020-12-22 16:25:44 +01002860 LY_CHECK_ERR_GOTO(!mt, LOGMEM(mod->ctx); ret = LY_EMEM, cleanup);
Michal Vasko9f96a052020-03-10 09:41:45 +01002861 mt->parent = parent;
2862 mt->annotation = ant;
Michal Vaskob0534f92021-07-01 13:24:03 +02002863 ant_type = ant->substmts[ANNOTATION_SUBSTMT_TYPE].storage;
2864 ret = lyd_value_store(mod->ctx, &mt->value, *ant_type, value, value_len, dynamic, format, prefix_data, hints,
2865 parent ? parent->schema : NULL, incomplete);
Radek Krejci2efc45b2020-12-22 16:25:44 +01002866 LY_CHECK_ERR_GOTO(ret, free(mt), cleanup);
2867 ret = lydict_insert(mod->ctx, name, name_len, &mt->name);
2868 LY_CHECK_ERR_GOTO(ret, free(mt), cleanup);
Michal Vasko90932a92020-02-12 14:33:03 +01002869
Michal Vasko6f4cbb62020-02-28 11:15:47 +01002870 /* insert as the last attribute */
2871 if (parent) {
Michal Vasko871a0252020-11-11 18:35:24 +01002872 lyd_insert_meta(parent, mt, clear_dflt);
Michal Vasko9f96a052020-03-10 09:41:45 +01002873 } else if (*meta) {
Radek Krejci1e008d22020-08-17 11:37:37 +02002874 for (last = *meta; last->next; last = last->next) {}
Michal Vasko9f96a052020-03-10 09:41:45 +01002875 last->next = mt;
Michal Vasko90932a92020-02-12 14:33:03 +01002876 }
2877
Michal Vasko9f96a052020-03-10 09:41:45 +01002878 if (meta) {
2879 *meta = mt;
Michal Vasko90932a92020-02-12 14:33:03 +01002880 }
Radek Krejci2efc45b2020-12-22 16:25:44 +01002881
2882cleanup:
Radek Krejciddace2c2021-01-08 11:30:56 +01002883 LOG_LOCBACK((parent && parent->schema) ? 1 : 0, parent ? 1 : 0, 0, 0);
Radek Krejci2efc45b2020-12-22 16:25:44 +01002884 return ret;
Michal Vasko90932a92020-02-12 14:33:03 +01002885}
2886
Michal Vaskoa5da3292020-08-12 13:10:50 +02002887void
2888lyd_insert_attr(struct lyd_node *parent, struct lyd_attr *attr)
2889{
2890 struct lyd_attr *last, *iter;
2891 struct lyd_node_opaq *opaq;
2892
2893 assert(parent && !parent->schema);
2894
2895 if (!attr) {
2896 return;
2897 }
2898
2899 opaq = (struct lyd_node_opaq *)parent;
2900 for (iter = attr; iter; iter = iter->next) {
2901 iter->parent = opaq;
2902 }
2903
2904 /* insert as the last attribute */
2905 if (opaq->attr) {
Radek Krejci1e008d22020-08-17 11:37:37 +02002906 for (last = opaq->attr; last->next; last = last->next) {}
Michal Vaskoa5da3292020-08-12 13:10:50 +02002907 last->next = attr;
2908 } else {
2909 opaq->attr = attr;
2910 }
2911}
2912
Michal Vasko52927e22020-03-16 17:26:14 +01002913LY_ERR
Michal Vaskofeca4fb2020-10-05 08:58:40 +02002914lyd_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 +01002915 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 +02002916 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 +01002917{
Radek Krejci011e4aa2020-09-04 15:22:31 +02002918 LY_ERR ret = LY_SUCCESS;
Radek Krejci1798aae2020-07-14 13:26:06 +02002919 struct lyd_attr *at, *last;
Michal Vasko52927e22020-03-16 17:26:14 +01002920
2921 assert(ctx && (parent || attr) && (!parent || !parent->schema));
Michal Vaskofeca4fb2020-10-05 08:58:40 +02002922 assert(name && name_len && format);
Michal Vasko52927e22020-03-16 17:26:14 +01002923
Michal Vasko2a3722d2021-06-16 11:52:39 +02002924 if (!value_len && (!dynamic || !*dynamic)) {
Michal Vasko52927e22020-03-16 17:26:14 +01002925 value = "";
2926 }
2927
2928 at = calloc(1, sizeof *at);
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01002929 LY_CHECK_ERR_RET(!at, LOGMEM(ctx); ly_free_prefix_data(format, val_prefix_data), LY_EMEM);
Radek Krejcid46e46a2020-09-15 14:22:42 +02002930
Michal Vaskoad92b672020-11-12 13:11:31 +01002931 LY_CHECK_GOTO(ret = lydict_insert(ctx, name, name_len, &at->name.name), finish);
Michal Vasko501af032020-11-11 20:27:44 +01002932 if (prefix_len) {
Michal Vaskoad92b672020-11-12 13:11:31 +01002933 LY_CHECK_GOTO(ret = lydict_insert(ctx, prefix, prefix_len, &at->name.prefix), finish);
Michal Vasko501af032020-11-11 20:27:44 +01002934 }
2935 if (module_key_len) {
Michal Vaskoad92b672020-11-12 13:11:31 +01002936 LY_CHECK_GOTO(ret = lydict_insert(ctx, module_key, module_key_len, &at->name.module_ns), finish);
Michal Vasko501af032020-11-11 20:27:44 +01002937 }
2938
Michal Vasko52927e22020-03-16 17:26:14 +01002939 if (dynamic && *dynamic) {
Radek Krejci011e4aa2020-09-04 15:22:31 +02002940 ret = lydict_insert_zc(ctx, (char *)value, &at->value);
2941 LY_CHECK_GOTO(ret, finish);
Michal Vasko52927e22020-03-16 17:26:14 +01002942 *dynamic = 0;
2943 } else {
Radek Krejci011e4aa2020-09-04 15:22:31 +02002944 LY_CHECK_GOTO(ret = lydict_insert(ctx, value, value_len, &at->value), finish);
Michal Vasko52927e22020-03-16 17:26:14 +01002945 }
Michal Vasko501af032020-11-11 20:27:44 +01002946 at->format = format;
2947 at->val_prefix_data = val_prefix_data;
2948 at->hints = hints;
Michal Vasko52927e22020-03-16 17:26:14 +01002949
2950 /* insert as the last attribute */
2951 if (parent) {
Michal Vaskoa5da3292020-08-12 13:10:50 +02002952 lyd_insert_attr(parent, at);
Michal Vasko52927e22020-03-16 17:26:14 +01002953 } else if (*attr) {
Radek Krejci1e008d22020-08-17 11:37:37 +02002954 for (last = *attr; last->next; last = last->next) {}
Michal Vasko52927e22020-03-16 17:26:14 +01002955 last->next = at;
2956 }
2957
Radek Krejci011e4aa2020-09-04 15:22:31 +02002958finish:
2959 if (ret) {
2960 lyd_free_attr_single(ctx, at);
2961 } else if (attr) {
Michal Vasko52927e22020-03-16 17:26:14 +01002962 *attr = at;
2963 }
2964 return LY_SUCCESS;
2965}
2966
Radek Krejci084289f2019-07-09 17:35:30 +02002967API const struct lyd_node_term *
Michal Vasko004d3152020-06-11 19:59:22 +02002968lyd_target(const struct ly_path *path, const struct lyd_node *tree)
Radek Krejci084289f2019-07-09 17:35:30 +02002969{
Michal Vasko004d3152020-06-11 19:59:22 +02002970 struct lyd_node *target;
Radek Krejci084289f2019-07-09 17:35:30 +02002971
Michal Vasko004d3152020-06-11 19:59:22 +02002972 if (ly_path_eval(path, tree, &target)) {
2973 return NULL;
Radek Krejci084289f2019-07-09 17:35:30 +02002974 }
2975
Michal Vasko004d3152020-06-11 19:59:22 +02002976 return (struct lyd_node_term *)target;
Radek Krejci084289f2019-07-09 17:35:30 +02002977}
2978
aPiecek2f63f952021-03-30 12:22:18 +02002979/**
2980 * @brief Check the equality of the two schemas from different contexts.
2981 *
2982 * @param schema1 of first node.
2983 * @param schema2 of second node.
2984 * @return 1 if the schemas are equal otherwise 0.
2985 */
2986static ly_bool
2987lyd_compare_schema_equal(const struct lysc_node *schema1, const struct lysc_node *schema2)
2988{
2989 if (!schema1 && !schema2) {
2990 return 1;
2991 } else if (!schema1 || !schema2) {
2992 return 0;
2993 }
2994
2995 assert(schema1->module->ctx != schema2->module->ctx);
2996
2997 if (schema1->nodetype != schema2->nodetype) {
2998 return 0;
2999 }
3000
3001 if (strcmp(schema1->name, schema2->name)) {
3002 return 0;
3003 }
3004
3005 if (strcmp(schema1->module->name, schema2->module->name)) {
3006 return 0;
3007 }
3008
3009 if (schema1->module->revision || schema2->module->revision) {
3010 if (!schema1->module->revision || !schema2->module->revision) {
3011 return 0;
3012 }
3013 if (strcmp(schema1->module->revision, schema2->module->revision)) {
3014 return 0;
3015 }
3016 }
3017
3018 return 1;
3019}
3020
3021/**
3022 * @brief Check the equality of the schemas for all parent nodes.
3023 *
3024 * Both nodes must be from different contexts.
3025 *
3026 * @param node1 Data of first node.
3027 * @param node2 Data of second node.
3028 * @return 1 if the all related parental schemas are equal otherwise 0.
3029 */
3030static ly_bool
3031lyd_compare_schema_parents_equal(const struct lyd_node *node1, const struct lyd_node *node2)
3032{
3033 const struct lysc_node *parent1, *parent2;
3034
3035 assert(node1 && node2);
3036
3037 for (parent1 = node1->schema->parent, parent2 = node2->schema->parent;
3038 parent1 && parent2;
3039 parent1 = parent1->parent, parent2 = parent2->parent) {
3040 if (!lyd_compare_schema_equal(parent1, parent2)) {
3041 return 0;
3042 }
3043 }
3044
3045 if (parent1 || parent2) {
3046 return 0;
3047 }
3048
3049 return 1;
3050}
3051
3052/**
3053 * @brief Internal implementation of @ref lyd_compare_single.
3054 * @copydoc lyd_compare_single
3055 * @param[in] parental_schemas_checked Flag used for optimization.
3056 * When this function is called for the first time, the flag must be set to 0.
3057 * The @ref lyd_compare_schema_parents_equal should be called only once during
3058 * recursive calls, and this is accomplished by setting to 1 in the lyd_compare_single_ body.
3059 */
3060static LY_ERR
3061lyd_compare_single_(const struct lyd_node *node1, const struct lyd_node *node2,
3062 uint32_t options, ly_bool parental_schemas_checked)
Radek Krejci1f05b6a2019-07-18 16:15:06 +02003063{
3064 const struct lyd_node *iter1, *iter2;
3065 struct lyd_node_term *term1, *term2;
3066 struct lyd_node_any *any1, *any2;
Michal Vasko52927e22020-03-16 17:26:14 +01003067 struct lyd_node_opaq *opaq1, *opaq2;
Radek Krejci1f05b6a2019-07-18 16:15:06 +02003068 size_t len1, len2;
Radek Krejci084289f2019-07-09 17:35:30 +02003069
Radek Krejci1f05b6a2019-07-18 16:15:06 +02003070 if (!node1 || !node2) {
3071 if (node1 == node2) {
3072 return LY_SUCCESS;
3073 } else {
3074 return LY_ENOT;
3075 }
3076 }
3077
aPiecek2f63f952021-03-30 12:22:18 +02003078 if (LYD_CTX(node1) == LYD_CTX(node2)) {
3079 /* same contexts */
3080 if (node1->schema != node2->schema) {
3081 return LY_ENOT;
3082 }
3083 } else {
3084 /* different contexts */
3085 if (!lyd_compare_schema_equal(node1->schema, node2->schema)) {
3086 return LY_ENOT;
3087 }
3088 if (!parental_schemas_checked) {
3089 if (!lyd_compare_schema_parents_equal(node1, node2)) {
3090 return LY_ENOT;
3091 }
3092 parental_schemas_checked = 1;
3093 }
Radek Krejci1f05b6a2019-07-18 16:15:06 +02003094 }
3095
3096 if (node1->hash != node2->hash) {
3097 return LY_ENOT;
3098 }
aPiecek2f63f952021-03-30 12:22:18 +02003099 /* 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 +02003100
Michal Vasko52927e22020-03-16 17:26:14 +01003101 if (!node1->schema) {
3102 opaq1 = (struct lyd_node_opaq *)node1;
3103 opaq2 = (struct lyd_node_opaq *)node2;
aPiecek2f63f952021-03-30 12:22:18 +02003104 if ((strcmp(opaq1->name.name, opaq2->name.name)) || (opaq1->format != opaq2->format) ||
3105 (strcmp(opaq1->name.module_ns, opaq2->name.module_ns))) {
Radek Krejci1f05b6a2019-07-18 16:15:06 +02003106 return LY_ENOT;
3107 }
Michal Vasko52927e22020-03-16 17:26:14 +01003108 switch (opaq1->format) {
Radek Krejci8df109d2021-04-23 12:19:08 +02003109 case LY_VALUE_XML:
Radek Krejci09c77442021-04-26 11:10:34 +02003110 if (lyxml_value_compare(LYD_CTX(node1), opaq1->value, opaq1->val_prefix_data, LYD_CTX(node2), opaq2->value,
3111 opaq2->val_prefix_data)) {
Michal Vasko52927e22020-03-16 17:26:14 +01003112 return LY_ENOT;
3113 }
3114 break;
Radek Krejci8df109d2021-04-23 12:19:08 +02003115 case LY_VALUE_JSON:
Radek Krejci1798aae2020-07-14 13:26:06 +02003116 /* prefixes in JSON are unique, so it is not necessary to canonize the values */
3117 if (strcmp(opaq1->value, opaq2->value)) {
3118 return LY_ENOT;
3119 }
3120 break;
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01003121 default:
Michal Vasko52927e22020-03-16 17:26:14 +01003122 /* not allowed */
Michal Vaskob7be7a82020-08-20 09:09:04 +02003123 LOGINT(LYD_CTX(node1));
Michal Vasko52927e22020-03-16 17:26:14 +01003124 return LY_EINT;
3125 }
3126 if (options & LYD_COMPARE_FULL_RECURSION) {
3127 iter1 = opaq1->child;
3128 iter2 = opaq2->child;
Radek Krejci1f05b6a2019-07-18 16:15:06 +02003129 goto all_children_compare;
Michal Vasko52927e22020-03-16 17:26:14 +01003130 }
3131 return LY_SUCCESS;
3132 } else {
3133 switch (node1->schema->nodetype) {
3134 case LYS_LEAF:
3135 case LYS_LEAFLIST:
3136 if (options & LYD_COMPARE_DEFAULTS) {
3137 if ((node1->flags & LYD_DEFAULT) != (node2->flags & LYD_DEFAULT)) {
3138 return LY_ENOT;
3139 }
3140 }
3141
Michal Vasko1a2bf2e2020-06-16 13:07:48 +02003142 term1 = (struct lyd_node_term *)node1;
3143 term2 = (struct lyd_node_term *)node2;
aPiecek2f63f952021-03-30 12:22:18 +02003144
3145 /* same contexts */
3146 if (LYD_CTX(node1) == LYD_CTX(node2)) {
3147 return term1->value.realtype->plugin->compare(&term1->value, &term2->value);
3148 }
3149
3150 /* different contexts */
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02003151 if (strcmp(lyd_get_value(node1), lyd_get_value(node2))) {
aPiecek2f63f952021-03-30 12:22:18 +02003152 return LY_ENOT;
3153 }
3154 return LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +01003155 case LYS_CONTAINER:
3156 if (options & LYD_COMPARE_DEFAULTS) {
3157 if ((node1->flags & LYD_DEFAULT) != (node2->flags & LYD_DEFAULT)) {
3158 return LY_ENOT;
3159 }
3160 }
3161 if (options & LYD_COMPARE_FULL_RECURSION) {
Michal Vasko9e685082021-01-29 14:49:09 +01003162 iter1 = lyd_child(node1);
3163 iter2 = lyd_child(node2);
Michal Vasko52927e22020-03-16 17:26:14 +01003164 goto all_children_compare;
Radek Krejci1f05b6a2019-07-18 16:15:06 +02003165 }
3166 return LY_SUCCESS;
Michal Vasko1bf09392020-03-27 12:38:10 +01003167 case LYS_RPC:
Michal Vasko52927e22020-03-16 17:26:14 +01003168 case LYS_ACTION:
3169 if (options & LYD_COMPARE_FULL_RECURSION) {
3170 /* TODO action/RPC
3171 goto all_children_compare;
3172 */
Radek Krejci1f05b6a2019-07-18 16:15:06 +02003173 }
3174 return LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +01003175 case LYS_NOTIF:
3176 if (options & LYD_COMPARE_FULL_RECURSION) {
3177 /* TODO Notification
3178 goto all_children_compare;
3179 */
3180 }
3181 return LY_SUCCESS;
3182 case LYS_LIST:
Michal Vasko9e685082021-01-29 14:49:09 +01003183 iter1 = lyd_child(node1);
3184 iter2 = lyd_child(node2);
Michal Vasko52927e22020-03-16 17:26:14 +01003185
3186 if (!(node1->schema->flags & LYS_KEYLESS) && !(options & LYD_COMPARE_FULL_RECURSION)) {
3187 /* lists with keys, their equivalence is based on their keys */
Michal Vasko544e58a2021-01-28 14:33:41 +01003188 for (const struct lysc_node *key = lysc_node_child(node1->schema);
Michal Vaskoc57d9a92020-06-23 13:28:27 +02003189 key && (key->flags & LYS_KEY);
Michal Vasko52927e22020-03-16 17:26:14 +01003190 key = key->next) {
aPiecek2f63f952021-03-30 12:22:18 +02003191 if (lyd_compare_single_(iter1, iter2, options, parental_schemas_checked)) {
Michal Vasko52927e22020-03-16 17:26:14 +01003192 return LY_ENOT;
3193 }
3194 iter1 = iter1->next;
3195 iter2 = iter2->next;
3196 }
3197 } else {
3198 /* lists without keys, their equivalence is based on equivalence of all the children (both direct and indirect) */
3199
Radek Krejci0f969882020-08-21 16:56:47 +02003200all_children_compare:
Michal Vasko52927e22020-03-16 17:26:14 +01003201 if (!iter1 && !iter2) {
3202 /* no children, nothing to compare */
3203 return LY_SUCCESS;
3204 }
3205
Michal Vaskod989ba02020-08-24 10:59:24 +02003206 for ( ; iter1 && iter2; iter1 = iter1->next, iter2 = iter2->next) {
aPiecek2f63f952021-03-30 12:22:18 +02003207 if (lyd_compare_single_(iter1, iter2, options | LYD_COMPARE_FULL_RECURSION, parental_schemas_checked)) {
Michal Vasko52927e22020-03-16 17:26:14 +01003208 return LY_ENOT;
3209 }
3210 }
3211 if (iter1 || iter2) {
3212 return LY_ENOT;
3213 }
3214 }
3215 return LY_SUCCESS;
3216 case LYS_ANYXML:
3217 case LYS_ANYDATA:
Michal Vasko22df3f02020-08-24 13:29:22 +02003218 any1 = (struct lyd_node_any *)node1;
3219 any2 = (struct lyd_node_any *)node2;
Michal Vasko52927e22020-03-16 17:26:14 +01003220
3221 if (any1->value_type != any2->value_type) {
3222 return LY_ENOT;
3223 }
3224 switch (any1->value_type) {
3225 case LYD_ANYDATA_DATATREE:
3226 iter1 = any1->value.tree;
3227 iter2 = any2->value.tree;
3228 goto all_children_compare;
3229 case LYD_ANYDATA_STRING:
3230 case LYD_ANYDATA_XML:
3231 case LYD_ANYDATA_JSON:
3232 len1 = strlen(any1->value.str);
3233 len2 = strlen(any2->value.str);
Michal Vasko69730152020-10-09 16:30:07 +02003234 if ((len1 != len2) || strcmp(any1->value.str, any2->value.str)) {
Michal Vasko52927e22020-03-16 17:26:14 +01003235 return LY_ENOT;
3236 }
3237 return LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +01003238 case LYD_ANYDATA_LYB:
Michal Vasko60ea6352020-06-29 13:39:39 +02003239 len1 = lyd_lyb_data_length(any1->value.mem);
3240 len2 = lyd_lyb_data_length(any2->value.mem);
Michal Vasko69730152020-10-09 16:30:07 +02003241 if ((len1 != len2) || memcmp(any1->value.mem, any2->value.mem, len1)) {
Michal Vasko52927e22020-03-16 17:26:14 +01003242 return LY_ENOT;
3243 }
3244 return LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +01003245 }
Radek Krejci1f05b6a2019-07-18 16:15:06 +02003246 }
3247 }
3248
Michal Vaskob7be7a82020-08-20 09:09:04 +02003249 LOGINT(LYD_CTX(node1));
Radek Krejci1f05b6a2019-07-18 16:15:06 +02003250 return LY_EINT;
3251}
Radek Krejci22ebdba2019-07-25 13:59:43 +02003252
Michal Vasko21725742020-06-29 11:49:25 +02003253API LY_ERR
aPiecek2f63f952021-03-30 12:22:18 +02003254lyd_compare_single(const struct lyd_node *node1, const struct lyd_node *node2, uint32_t options)
3255{
3256 return lyd_compare_single_(node1, node2, options, 0);
3257}
3258
3259API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003260lyd_compare_siblings(const struct lyd_node *node1, const struct lyd_node *node2, uint32_t options)
Michal Vasko8f359bf2020-07-28 10:41:15 +02003261{
Michal Vaskod989ba02020-08-24 10:59:24 +02003262 for ( ; node1 && node2; node1 = node1->next, node2 = node2->next) {
Michal Vasko8f359bf2020-07-28 10:41:15 +02003263 LY_CHECK_RET(lyd_compare_single(node1, node2, options));
3264 }
3265
Michal Vasko11a81432020-07-28 16:31:29 +02003266 if (node1 == node2) {
3267 return LY_SUCCESS;
Michal Vasko8f359bf2020-07-28 10:41:15 +02003268 }
Michal Vasko11a81432020-07-28 16:31:29 +02003269 return LY_ENOT;
Michal Vasko8f359bf2020-07-28 10:41:15 +02003270}
3271
3272API LY_ERR
Michal Vasko21725742020-06-29 11:49:25 +02003273lyd_compare_meta(const struct lyd_meta *meta1, const struct lyd_meta *meta2)
3274{
3275 if (!meta1 || !meta2) {
3276 if (meta1 == meta2) {
3277 return LY_SUCCESS;
3278 } else {
3279 return LY_ENOT;
3280 }
3281 }
3282
Michal Vaskoa8083062020-11-06 17:22:10 +01003283 if ((meta1->annotation->module->ctx != meta2->annotation->module->ctx) || (meta1->annotation != meta2->annotation)) {
Michal Vasko21725742020-06-29 11:49:25 +02003284 return LY_ENOT;
3285 }
3286
Michal Vasko21725742020-06-29 11:49:25 +02003287 return meta1->value.realtype->plugin->compare(&meta1->value, &meta2->value);
3288}
3289
Radek Krejci22ebdba2019-07-25 13:59:43 +02003290/**
Michal Vasko9cf62422021-07-01 13:29:32 +02003291 * @brief Create a copy of the attribute.
3292 *
3293 * @param[in] attr Attribute to copy.
3294 * @param[in] node Opaque where to append the new attribute.
3295 * @param[out] dup Optional created attribute copy.
3296 * @return LY_ERR value.
3297 */
3298static LY_ERR
3299lyd_dup_attr_single(const struct lyd_attr *attr, struct lyd_node *node, struct lyd_attr **dup)
3300{
3301 LY_ERR ret = LY_SUCCESS;
3302 struct lyd_attr *a, *last;
3303 struct lyd_node_opaq *opaq = (struct lyd_node_opaq *)node;
3304
3305 LY_CHECK_ARG_RET(NULL, attr, node, !node->schema, LY_EINVAL);
3306
3307 /* create a copy */
3308 a = calloc(1, sizeof *attr);
3309 LY_CHECK_ERR_RET(!a, LOGMEM(LYD_CTX(node)), LY_EMEM);
3310
3311 LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), attr->name.name, 0, &a->name.name), finish);
3312 LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), attr->name.prefix, 0, &a->name.prefix), finish);
3313 LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), attr->name.module_ns, 0, &a->name.module_ns), finish);
3314 LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), attr->value, 0, &a->value), finish);
3315 a->hints = attr->hints;
3316 a->format = attr->format;
3317 if (attr->val_prefix_data) {
3318 ret = ly_dup_prefix_data(LYD_CTX(node), attr->format, attr->val_prefix_data, &a->val_prefix_data);
3319 LY_CHECK_GOTO(ret, finish);
3320 }
3321
3322 /* insert as the last attribute */
3323 a->parent = opaq;
3324 if (opaq->attr) {
3325 for (last = opaq->attr; last->next; last = last->next) {}
3326 last->next = a;
3327 } else {
3328 opaq->attr = a;
3329 }
3330
3331finish:
3332 if (ret) {
3333 lyd_free_attr_single(LYD_CTX(node), a);
3334 } else if (dup) {
3335 *dup = a;
3336 }
3337 return LY_SUCCESS;
3338}
3339
3340/**
Michal Vasko52927e22020-03-16 17:26:14 +01003341 * @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 +02003342 *
3343 * 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 +02003344 *
3345 * @param[in] node Original node to duplicate
3346 * @param[in] parent Parent to insert into, NULL for top-level sibling.
3347 * @param[in,out] first First sibling, NULL if no top-level sibling exist yet. Can be also NULL if @p parent is set.
3348 * @param[in] options Bitmask of options flags, see @ref dupoptions.
3349 * @param[out] dup_p Pointer where the created duplicated node is placed (besides connecting it int @p parent / @p first sibling).
3350 * @return LY_ERR value
Radek Krejci22ebdba2019-07-25 13:59:43 +02003351 */
Michal Vasko52927e22020-03-16 17:26:14 +01003352static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003353lyd_dup_r(const struct lyd_node *node, struct lyd_node *parent, struct lyd_node **first, uint32_t options,
Radek Krejci0f969882020-08-21 16:56:47 +02003354 struct lyd_node **dup_p)
Radek Krejci22ebdba2019-07-25 13:59:43 +02003355{
Michal Vasko52927e22020-03-16 17:26:14 +01003356 LY_ERR ret;
Radek Krejci22ebdba2019-07-25 13:59:43 +02003357 struct lyd_node *dup = NULL;
Michal Vasko61551fa2020-07-09 15:45:45 +02003358 struct lyd_meta *meta;
Michal Vasko9cf62422021-07-01 13:29:32 +02003359 struct lyd_attr *attr;
Michal Vasko61551fa2020-07-09 15:45:45 +02003360 struct lyd_node_any *any;
Radek Krejci22ebdba2019-07-25 13:59:43 +02003361
Michal Vasko52927e22020-03-16 17:26:14 +01003362 LY_CHECK_ARG_RET(NULL, node, LY_EINVAL);
Radek Krejci22ebdba2019-07-25 13:59:43 +02003363
Michal Vasko52927e22020-03-16 17:26:14 +01003364 if (!node->schema) {
3365 dup = calloc(1, sizeof(struct lyd_node_opaq));
Michal Vasko9cf62422021-07-01 13:29:32 +02003366 ((struct lyd_node_opaq *)dup)->ctx = LYD_CTX(node);
Michal Vasko52927e22020-03-16 17:26:14 +01003367 } else {
3368 switch (node->schema->nodetype) {
Michal Vasko1bf09392020-03-27 12:38:10 +01003369 case LYS_RPC:
Michal Vasko52927e22020-03-16 17:26:14 +01003370 case LYS_ACTION:
3371 case LYS_NOTIF:
3372 case LYS_CONTAINER:
3373 case LYS_LIST:
3374 dup = calloc(1, sizeof(struct lyd_node_inner));
3375 break;
3376 case LYS_LEAF:
3377 case LYS_LEAFLIST:
3378 dup = calloc(1, sizeof(struct lyd_node_term));
3379 break;
3380 case LYS_ANYDATA:
3381 case LYS_ANYXML:
3382 dup = calloc(1, sizeof(struct lyd_node_any));
3383 break;
3384 default:
Michal Vaskob7be7a82020-08-20 09:09:04 +02003385 LOGINT(LYD_CTX(node));
Michal Vasko52927e22020-03-16 17:26:14 +01003386 ret = LY_EINT;
3387 goto error;
3388 }
Radek Krejci22ebdba2019-07-25 13:59:43 +02003389 }
Michal Vaskob7be7a82020-08-20 09:09:04 +02003390 LY_CHECK_ERR_GOTO(!dup, LOGMEM(LYD_CTX(node)); ret = LY_EMEM, error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02003391
Michal Vaskof6df0a02020-06-16 13:08:34 +02003392 if (options & LYD_DUP_WITH_FLAGS) {
3393 dup->flags = node->flags;
3394 } else {
3395 dup->flags = (node->flags & LYD_DEFAULT) | LYD_NEW;
3396 }
Radek Krejci22ebdba2019-07-25 13:59:43 +02003397 dup->schema = node->schema;
Michal Vasko52927e22020-03-16 17:26:14 +01003398 dup->prev = dup;
Radek Krejci22ebdba2019-07-25 13:59:43 +02003399
Michal Vasko9cf62422021-07-01 13:29:32 +02003400 /* duplicate metadata/attributes */
Michal Vasko25a32822020-07-09 15:48:22 +02003401 if (!(options & LYD_DUP_NO_META)) {
Michal Vasko9cf62422021-07-01 13:29:32 +02003402 if (!node->schema) {
3403 LY_LIST_FOR(((struct lyd_node_opaq *)node)->attr, attr) {
3404 LY_CHECK_GOTO(ret = lyd_dup_attr_single(attr, dup, NULL), error);
3405 }
3406 } else {
3407 LY_LIST_FOR(node->meta, meta) {
3408 LY_CHECK_GOTO(ret = lyd_dup_meta_single(meta, dup, NULL), error);
3409 }
Michal Vasko25a32822020-07-09 15:48:22 +02003410 }
3411 }
Radek Krejci22ebdba2019-07-25 13:59:43 +02003412
3413 /* nodetype-specific work */
Michal Vasko52927e22020-03-16 17:26:14 +01003414 if (!dup->schema) {
3415 struct lyd_node_opaq *opaq = (struct lyd_node_opaq *)dup;
3416 struct lyd_node_opaq *orig = (struct lyd_node_opaq *)node;
3417 struct lyd_node *child;
Radek Krejci22ebdba2019-07-25 13:59:43 +02003418
3419 if (options & LYD_DUP_RECURSIVE) {
3420 /* duplicate all the children */
3421 LY_LIST_FOR(orig->child, child) {
Michal Vasko3a41dff2020-07-15 14:30:28 +02003422 LY_CHECK_GOTO(ret = lyd_dup_r(child, dup, NULL, options, NULL), error);
Michal Vasko52927e22020-03-16 17:26:14 +01003423 }
3424 }
Michal Vaskoad92b672020-11-12 13:11:31 +01003425 LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), orig->name.name, 0, &opaq->name.name), error);
Michal Vasko9cf62422021-07-01 13:29:32 +02003426 LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), orig->name.prefix, 0, &opaq->name.prefix), error);
Michal Vaskoad92b672020-11-12 13:11:31 +01003427 LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), orig->name.module_ns, 0, &opaq->name.module_ns), error);
Michal Vasko9cf62422021-07-01 13:29:32 +02003428 LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), orig->value, 0, &opaq->value), error);
3429 opaq->hints = orig->hints;
3430 opaq->format = orig->format;
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01003431 if (orig->val_prefix_data) {
3432 ret = ly_dup_prefix_data(LYD_CTX(node), opaq->format, orig->val_prefix_data, &opaq->val_prefix_data);
3433 LY_CHECK_GOTO(ret, error);
Michal Vasko52927e22020-03-16 17:26:14 +01003434 }
Michal Vasko52927e22020-03-16 17:26:14 +01003435 } else if (dup->schema->nodetype & LYD_NODE_TERM) {
3436 struct lyd_node_term *term = (struct lyd_node_term *)dup;
3437 struct lyd_node_term *orig = (struct lyd_node_term *)node;
3438
3439 term->hash = orig->hash;
Michal Vaskob7be7a82020-08-20 09:09:04 +02003440 LY_CHECK_ERR_GOTO(orig->value.realtype->plugin->duplicate(LYD_CTX(node), &orig->value, &term->value),
Michal Vasko69730152020-10-09 16:30:07 +02003441 LOGERR(LYD_CTX(node), LY_EINT, "Value duplication failed."); ret = LY_EINT, error);
Michal Vasko52927e22020-03-16 17:26:14 +01003442 } else if (dup->schema->nodetype & LYD_NODE_INNER) {
3443 struct lyd_node_inner *orig = (struct lyd_node_inner *)node;
3444 struct lyd_node *child;
3445
3446 if (options & LYD_DUP_RECURSIVE) {
3447 /* duplicate all the children */
3448 LY_LIST_FOR(orig->child, child) {
Michal Vasko3a41dff2020-07-15 14:30:28 +02003449 LY_CHECK_GOTO(ret = lyd_dup_r(child, dup, NULL, options, NULL), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02003450 }
Michal Vasko69730152020-10-09 16:30:07 +02003451 } else if ((dup->schema->nodetype == LYS_LIST) && !(dup->schema->flags & LYS_KEYLESS)) {
Radek Krejci22ebdba2019-07-25 13:59:43 +02003452 /* always duplicate keys of a list */
Radek Krejci22ebdba2019-07-25 13:59:43 +02003453 child = orig->child;
Michal Vasko544e58a2021-01-28 14:33:41 +01003454 for (const struct lysc_node *key = lysc_node_child(dup->schema);
Michal Vaskoc57d9a92020-06-23 13:28:27 +02003455 key && (key->flags & LYS_KEY);
Radek Krejci0fe9b512019-07-26 17:51:05 +02003456 key = key->next) {
Radek Krejci22ebdba2019-07-25 13:59:43 +02003457 if (!child) {
3458 /* possibly not keys are present in filtered tree */
3459 break;
Radek Krejci0fe9b512019-07-26 17:51:05 +02003460 } else if (child->schema != key) {
3461 /* possibly not all keys are present in filtered tree,
3462 * but there can be also some non-key nodes */
3463 continue;
Radek Krejci22ebdba2019-07-25 13:59:43 +02003464 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02003465 LY_CHECK_GOTO(ret = lyd_dup_r(child, dup, NULL, options, NULL), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02003466 child = child->next;
3467 }
3468 }
3469 lyd_hash(dup);
3470 } else if (dup->schema->nodetype & LYD_NODE_ANY) {
Michal Vasko61551fa2020-07-09 15:45:45 +02003471 dup->hash = node->hash;
3472 any = (struct lyd_node_any *)node;
3473 LY_CHECK_GOTO(ret = lyd_any_copy_value(dup, &any->value, any->value_type), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02003474 }
3475
Michal Vasko52927e22020-03-16 17:26:14 +01003476 /* insert */
Michal Vasko6ee6f432021-07-16 09:49:14 +02003477 lyd_insert_node(parent, first, dup, 0);
Michal Vasko52927e22020-03-16 17:26:14 +01003478
3479 if (dup_p) {
3480 *dup_p = dup;
3481 }
3482 return LY_SUCCESS;
Radek Krejci22ebdba2019-07-25 13:59:43 +02003483
3484error:
Michal Vasko52927e22020-03-16 17:26:14 +01003485 lyd_free_tree(dup);
3486 return ret;
Radek Krejci22ebdba2019-07-25 13:59:43 +02003487}
3488
Michal Vasko3a41dff2020-07-15 14:30:28 +02003489static LY_ERR
Michal Vasko20909752021-05-18 16:13:38 +02003490lyd_dup_get_local_parent(const struct lyd_node *node, const struct lyd_node_inner *parent, uint32_t options,
3491 struct lyd_node **dup_parent, struct lyd_node_inner **local_parent)
Radek Krejci22ebdba2019-07-25 13:59:43 +02003492{
Michal Vasko3a41dff2020-07-15 14:30:28 +02003493 const struct lyd_node_inner *orig_parent, *iter;
Radek Krejci857189e2020-09-01 13:26:36 +02003494 ly_bool repeat = 1;
Michal Vasko3a41dff2020-07-15 14:30:28 +02003495
3496 *dup_parent = NULL;
3497 *local_parent = NULL;
3498
3499 for (orig_parent = node->parent; repeat && orig_parent; orig_parent = orig_parent->parent) {
3500 if (parent && (parent->schema == orig_parent->schema)) {
3501 /* stop creating parents, connect what we have into the provided parent */
3502 iter = parent;
3503 repeat = 0;
3504 } else {
3505 iter = NULL;
Michal Vasko20909752021-05-18 16:13:38 +02003506 LY_CHECK_RET(lyd_dup_r((struct lyd_node *)orig_parent, NULL, (struct lyd_node **)&iter, options,
Radek Krejci0f969882020-08-21 16:56:47 +02003507 (struct lyd_node **)&iter));
Michal Vasko3a41dff2020-07-15 14:30:28 +02003508 }
3509 if (!*local_parent) {
3510 *local_parent = (struct lyd_node_inner *)iter;
3511 }
3512 if (iter->child) {
3513 /* 1) list - add after keys
3514 * 2) provided parent with some children */
3515 iter->child->prev->next = *dup_parent;
3516 if (*dup_parent) {
3517 (*dup_parent)->prev = iter->child->prev;
3518 iter->child->prev = *dup_parent;
3519 }
3520 } else {
3521 ((struct lyd_node_inner *)iter)->child = *dup_parent;
3522 }
3523 if (*dup_parent) {
3524 (*dup_parent)->parent = (struct lyd_node_inner *)iter;
3525 }
3526 *dup_parent = (struct lyd_node *)iter;
3527 }
3528
3529 if (repeat && parent) {
3530 /* given parent and created parents chain actually do not interconnect */
Michal Vaskob7be7a82020-08-20 09:09:04 +02003531 LOGERR(LYD_CTX(node), LY_EINVAL,
Michal Vasko69730152020-10-09 16:30:07 +02003532 "Invalid argument parent (%s()) - does not interconnect with the created node's parents chain.", __func__);
Michal Vasko3a41dff2020-07-15 14:30:28 +02003533 return LY_EINVAL;
3534 }
3535
3536 return LY_SUCCESS;
3537}
3538
3539static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02003540lyd_dup(const struct lyd_node *node, struct lyd_node_inner *parent, uint32_t options, ly_bool nosiblings, struct lyd_node **dup)
Michal Vasko3a41dff2020-07-15 14:30:28 +02003541{
3542 LY_ERR rc;
Radek Krejci22ebdba2019-07-25 13:59:43 +02003543 const struct lyd_node *orig; /* original node to be duplicated */
3544 struct lyd_node *first = NULL; /* the first duplicated node, this is returned */
Radek Krejci22ebdba2019-07-25 13:59:43 +02003545 struct lyd_node *top = NULL; /* the most higher created node */
3546 struct lyd_node_inner *local_parent = NULL; /* the direct parent node for the duplicated node(s) */
Radek Krejci22ebdba2019-07-25 13:59:43 +02003547
Michal Vasko3a41dff2020-07-15 14:30:28 +02003548 LY_CHECK_ARG_RET(NULL, node, LY_EINVAL);
Radek Krejci22ebdba2019-07-25 13:59:43 +02003549
3550 if (options & LYD_DUP_WITH_PARENTS) {
Michal Vasko20909752021-05-18 16:13:38 +02003551 LY_CHECK_GOTO(rc = lyd_dup_get_local_parent(node, parent, options & (LYD_DUP_WITH_FLAGS | LYD_DUP_NO_META),
3552 &top, &local_parent), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02003553 } else {
3554 local_parent = parent;
3555 }
3556
Radek Krejci22ebdba2019-07-25 13:59:43 +02003557 LY_LIST_FOR(node, orig) {
Michal Vasko35f4d772021-01-12 12:08:57 +01003558 if (lysc_is_key(orig->schema)) {
3559 if (local_parent) {
3560 /* the key must already exist in the parent */
3561 rc = lyd_find_sibling_schema(local_parent->child, orig->schema, first ? NULL : &first);
3562 LY_CHECK_ERR_GOTO(rc, LOGINT(LYD_CTX(node)), error);
3563 } else {
3564 assert(!(options & LYD_DUP_WITH_PARENTS));
3565 /* duplicating a single key, okay, I suppose... */
3566 rc = lyd_dup_r(orig, NULL, &first, options, first ? NULL : &first);
3567 LY_CHECK_GOTO(rc, error);
3568 }
3569 } else {
3570 /* if there is no local parent, it will be inserted into first */
Michal Vasko9e543402021-06-11 13:55:30 +02003571 rc = lyd_dup_r(orig, local_parent ? &local_parent->node : NULL, &first, options, first ? NULL : &first);
Michal Vasko35f4d772021-01-12 12:08:57 +01003572 LY_CHECK_GOTO(rc, error);
3573 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02003574 if (nosiblings) {
Radek Krejci22ebdba2019-07-25 13:59:43 +02003575 break;
3576 }
3577 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02003578
Michal Vasko3a41dff2020-07-15 14:30:28 +02003579 if (dup) {
3580 *dup = first;
3581 }
3582 return LY_SUCCESS;
Radek Krejci22ebdba2019-07-25 13:59:43 +02003583
3584error:
3585 if (top) {
3586 lyd_free_tree(top);
3587 } else {
Michal Vaskof03ed032020-03-04 13:31:44 +01003588 lyd_free_siblings(first);
Radek Krejci22ebdba2019-07-25 13:59:43 +02003589 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02003590 return rc;
Radek Krejci22ebdba2019-07-25 13:59:43 +02003591}
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003592
Michal Vasko3a41dff2020-07-15 14:30:28 +02003593API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003594lyd_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 +02003595{
3596 return lyd_dup(node, parent, options, 1, dup);
3597}
3598
3599API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003600lyd_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 +02003601{
3602 return lyd_dup(node, parent, options, 0, dup);
3603}
3604
3605API LY_ERR
3606lyd_dup_meta_single(const struct lyd_meta *meta, struct lyd_node *node, struct lyd_meta **dup)
Michal Vasko25a32822020-07-09 15:48:22 +02003607{
Radek Krejci011e4aa2020-09-04 15:22:31 +02003608 LY_ERR ret = LY_SUCCESS;
Michal Vasko25a32822020-07-09 15:48:22 +02003609 struct lyd_meta *mt, *last;
3610
Michal Vasko3a41dff2020-07-15 14:30:28 +02003611 LY_CHECK_ARG_RET(NULL, meta, node, LY_EINVAL);
Michal Vasko25a32822020-07-09 15:48:22 +02003612
3613 /* create a copy */
3614 mt = calloc(1, sizeof *mt);
Michal Vaskob7be7a82020-08-20 09:09:04 +02003615 LY_CHECK_ERR_RET(!mt, LOGMEM(LYD_CTX(node)), LY_EMEM);
Michal Vasko25a32822020-07-09 15:48:22 +02003616 mt->annotation = meta->annotation;
Michal Vaskob7be7a82020-08-20 09:09:04 +02003617 ret = meta->value.realtype->plugin->duplicate(LYD_CTX(node), &meta->value, &mt->value);
Radek Krejci011e4aa2020-09-04 15:22:31 +02003618 LY_CHECK_ERR_GOTO(ret, LOGERR(LYD_CTX(node), LY_EINT, "Value duplication failed."), finish);
3619 LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), meta->name, 0, &mt->name), finish);
Michal Vasko25a32822020-07-09 15:48:22 +02003620
3621 /* insert as the last attribute */
Radek Krejci011e4aa2020-09-04 15:22:31 +02003622 mt->parent = node;
Michal Vasko25a32822020-07-09 15:48:22 +02003623 if (node->meta) {
Radek Krejci1e008d22020-08-17 11:37:37 +02003624 for (last = node->meta; last->next; last = last->next) {}
Michal Vasko25a32822020-07-09 15:48:22 +02003625 last->next = mt;
3626 } else {
3627 node->meta = mt;
3628 }
3629
Radek Krejci011e4aa2020-09-04 15:22:31 +02003630finish:
3631 if (ret) {
3632 lyd_free_meta_single(mt);
3633 } else if (dup) {
Michal Vasko3a41dff2020-07-15 14:30:28 +02003634 *dup = mt;
3635 }
3636 return LY_SUCCESS;
Michal Vasko25a32822020-07-09 15:48:22 +02003637}
3638
Michal Vasko4490d312020-06-16 13:08:55 +02003639/**
3640 * @brief Merge a source sibling into target siblings.
3641 *
3642 * @param[in,out] first_trg First target sibling, is updated if top-level.
3643 * @param[in] parent_trg Target parent.
3644 * @param[in,out] sibling_src Source sibling to merge, set to NULL if spent.
Michal Vaskocd3f6172021-05-18 16:14:50 +02003645 * @param[in] merge_cb Optional merge callback.
3646 * @param[in] cb_data Arbitrary callback data.
Michal Vasko4490d312020-06-16 13:08:55 +02003647 * @param[in] options Merge options.
Michal Vaskocd3f6172021-05-18 16:14:50 +02003648 * @param[in,out] dup_inst Duplicate instance cache for all @p first_trg siblings.
Michal Vasko4490d312020-06-16 13:08:55 +02003649 * @return LY_ERR value.
3650 */
3651static LY_ERR
3652lyd_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 +02003653 lyd_merge_cb merge_cb, void *cb_data, uint16_t options, struct lyd_dup_inst **dup_inst)
Michal Vasko4490d312020-06-16 13:08:55 +02003654{
Michal Vasko4490d312020-06-16 13:08:55 +02003655 const struct lyd_node *child_src, *tmp, *sibling_src;
Michal Vasko56daf732020-08-10 10:57:18 +02003656 struct lyd_node *match_trg, *dup_src, *elem;
Michal Vasko4490d312020-06-16 13:08:55 +02003657 struct lysc_type *type;
Michal Vaskocd3f6172021-05-18 16:14:50 +02003658 struct lyd_dup_inst *child_dup_inst = NULL;
3659 LY_ERR ret;
3660 ly_bool first_inst = 0;
Michal Vasko4490d312020-06-16 13:08:55 +02003661
3662 sibling_src = *sibling_src_p;
Michal Vaskocd3f6172021-05-18 16:14:50 +02003663 if (!sibling_src->schema) {
3664 /* try to find the same opaque node */
3665 lyd_find_sibling_opaq_next(*first_trg, LYD_NAME(sibling_src), &match_trg);
3666 } else if (sibling_src->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) {
Michal Vasko4490d312020-06-16 13:08:55 +02003667 /* try to find the exact instance */
Michal Vaskocd3f6172021-05-18 16:14:50 +02003668 lyd_find_sibling_first(*first_trg, sibling_src, &match_trg);
Michal Vasko4490d312020-06-16 13:08:55 +02003669 } else {
3670 /* try to simply find the node, there cannot be more instances */
Michal Vaskocd3f6172021-05-18 16:14:50 +02003671 lyd_find_sibling_val(*first_trg, sibling_src->schema, NULL, 0, &match_trg);
Michal Vasko4490d312020-06-16 13:08:55 +02003672 }
3673
Michal Vaskocd3f6172021-05-18 16:14:50 +02003674 if (match_trg) {
3675 /* update match as needed */
3676 LY_CHECK_RET(lyd_dup_inst_next(&match_trg, *first_trg, dup_inst));
3677 } else {
3678 /* first instance of this node */
3679 first_inst = 1;
3680 }
3681
3682 if (match_trg) {
3683 /* call callback */
3684 if (merge_cb) {
3685 LY_CHECK_RET(merge_cb(match_trg, sibling_src, cb_data));
3686 }
3687
Michal Vasko4490d312020-06-16 13:08:55 +02003688 /* node found, make sure even value matches for all node types */
Michal Vasko8f359bf2020-07-28 10:41:15 +02003689 if ((match_trg->schema->nodetype == LYS_LEAF) && lyd_compare_single(sibling_src, match_trg, LYD_COMPARE_DEFAULTS)) {
Michal Vasko4490d312020-06-16 13:08:55 +02003690 /* since they are different, they cannot both be default */
3691 assert(!(sibling_src->flags & LYD_DEFAULT) || !(match_trg->flags & LYD_DEFAULT));
3692
Michal Vasko3a41dff2020-07-15 14:30:28 +02003693 /* update value (or only LYD_DEFAULT flag) only if flag set or the source node is not default */
3694 if ((options & LYD_MERGE_DEFAULTS) || !(sibling_src->flags & LYD_DEFAULT)) {
Michal Vasko4490d312020-06-16 13:08:55 +02003695 type = ((struct lysc_node_leaf *)match_trg->schema)->type;
Michal Vaskob7be7a82020-08-20 09:09:04 +02003696 type->plugin->free(LYD_CTX(match_trg), &((struct lyd_node_term *)match_trg)->value);
3697 LY_CHECK_RET(type->plugin->duplicate(LYD_CTX(match_trg), &((struct lyd_node_term *)sibling_src)->value,
Michal Vasko69730152020-10-09 16:30:07 +02003698 &((struct lyd_node_term *)match_trg)->value));
Michal Vasko4490d312020-06-16 13:08:55 +02003699
3700 /* copy flags and add LYD_NEW */
3701 match_trg->flags = sibling_src->flags | LYD_NEW;
3702 }
Michal Vasko8f359bf2020-07-28 10:41:15 +02003703 } else if ((match_trg->schema->nodetype & LYS_ANYDATA) && lyd_compare_single(sibling_src, match_trg, 0)) {
Michal Vasko4c583e82020-07-17 12:16:14 +02003704 /* update value */
3705 LY_CHECK_RET(lyd_any_copy_value(match_trg, &((struct lyd_node_any *)sibling_src)->value,
Radek Krejci0f969882020-08-21 16:56:47 +02003706 ((struct lyd_node_any *)sibling_src)->value_type));
Michal Vasko4490d312020-06-16 13:08:55 +02003707
3708 /* copy flags and add LYD_NEW */
3709 match_trg->flags = sibling_src->flags | LYD_NEW;
Michal Vaskocd3f6172021-05-18 16:14:50 +02003710 }
3711
3712 /* check descendants, recursively */
3713 ret = LY_SUCCESS;
3714 LY_LIST_FOR_SAFE(lyd_child_no_keys(sibling_src), tmp, child_src) {
3715 ret = lyd_merge_sibling_r(lyd_node_child_p(match_trg), match_trg, &child_src, merge_cb, cb_data, options,
3716 &child_dup_inst);
3717 if (ret) {
3718 break;
Michal Vasko4490d312020-06-16 13:08:55 +02003719 }
3720 }
Michal Vaskocd3f6172021-05-18 16:14:50 +02003721 lyd_dup_inst_free(child_dup_inst);
3722 LY_CHECK_RET(ret);
Michal Vasko4490d312020-06-16 13:08:55 +02003723 } else {
3724 /* node not found, merge it */
3725 if (options & LYD_MERGE_DESTRUCT) {
3726 dup_src = (struct lyd_node *)sibling_src;
3727 lyd_unlink_tree(dup_src);
3728 /* spend it */
3729 *sibling_src_p = NULL;
3730 } else {
Michal Vasko3a41dff2020-07-15 14:30:28 +02003731 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 +02003732 }
3733
3734 /* set LYD_NEW for all the new nodes, required for validation */
Michal Vasko56daf732020-08-10 10:57:18 +02003735 LYD_TREE_DFS_BEGIN(dup_src, elem) {
Michal Vasko4490d312020-06-16 13:08:55 +02003736 elem->flags |= LYD_NEW;
Michal Vasko56daf732020-08-10 10:57:18 +02003737 LYD_TREE_DFS_END(dup_src, elem);
Michal Vasko4490d312020-06-16 13:08:55 +02003738 }
3739
Michal Vaskocd3f6172021-05-18 16:14:50 +02003740 /* insert */
Michal Vasko6ee6f432021-07-16 09:49:14 +02003741 lyd_insert_node(parent_trg, first_trg, dup_src, 0);
Michal Vaskocd3f6172021-05-18 16:14:50 +02003742
3743 if (first_inst) {
3744 /* remember not to find this instance next time */
3745 LY_CHECK_RET(lyd_dup_inst_next(&dup_src, *first_trg, dup_inst));
3746 }
3747
3748 /* call callback, no source node */
3749 if (merge_cb) {
3750 LY_CHECK_RET(merge_cb(dup_src, NULL, cb_data));
3751 }
Michal Vasko4490d312020-06-16 13:08:55 +02003752 }
3753
3754 return LY_SUCCESS;
3755}
3756
Michal Vasko3a41dff2020-07-15 14:30:28 +02003757static LY_ERR
Michal Vaskocd3f6172021-05-18 16:14:50 +02003758lyd_merge(struct lyd_node **target, const struct lyd_node *source, const struct lys_module *mod,
3759 lyd_merge_cb merge_cb, void *cb_data, uint16_t options, ly_bool nosiblings)
Michal Vasko4490d312020-06-16 13:08:55 +02003760{
3761 const struct lyd_node *sibling_src, *tmp;
Michal Vaskocd3f6172021-05-18 16:14:50 +02003762 struct lyd_dup_inst *dup_inst = NULL;
Radek Krejci857189e2020-09-01 13:26:36 +02003763 ly_bool first;
Michal Vaskocd3f6172021-05-18 16:14:50 +02003764 LY_ERR ret = LY_SUCCESS;
Michal Vasko4490d312020-06-16 13:08:55 +02003765
3766 LY_CHECK_ARG_RET(NULL, target, LY_EINVAL);
3767
3768 if (!source) {
3769 /* nothing to merge */
3770 return LY_SUCCESS;
3771 }
3772
Michal Vasko831422b2020-11-24 11:20:51 +01003773 if ((*target && lysc_data_parent((*target)->schema)) || lysc_data_parent(source->schema)) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02003774 LOGERR(LYD_CTX(source), LY_EINVAL, "Invalid arguments - can merge only 2 top-level subtrees (%s()).", __func__);
Michal Vasko4490d312020-06-16 13:08:55 +02003775 return LY_EINVAL;
3776 }
3777
3778 LY_LIST_FOR_SAFE(source, tmp, sibling_src) {
Michal Vaskocd3f6172021-05-18 16:14:50 +02003779 if (mod && (lyd_owner_module(sibling_src) != mod)) {
3780 /* skip data nodes from different modules */
3781 continue;
3782 }
3783
Radek Krejci857189e2020-09-01 13:26:36 +02003784 first = (sibling_src == source) ? 1 : 0;
Michal Vaskocd3f6172021-05-18 16:14:50 +02003785 ret = lyd_merge_sibling_r(target, NULL, &sibling_src, merge_cb, cb_data, options, &dup_inst);
3786 if (ret) {
3787 break;
3788 }
Michal Vasko4490d312020-06-16 13:08:55 +02003789 if (first && !sibling_src) {
3790 /* source was spent (unlinked), move to the next node */
3791 source = tmp;
3792 }
3793
Michal Vasko3a41dff2020-07-15 14:30:28 +02003794 if (nosiblings) {
Michal Vasko4490d312020-06-16 13:08:55 +02003795 break;
3796 }
3797 }
3798
3799 if (options & LYD_MERGE_DESTRUCT) {
3800 /* free any leftover source data that were not merged */
3801 lyd_free_siblings((struct lyd_node *)source);
3802 }
3803
Michal Vaskocd3f6172021-05-18 16:14:50 +02003804 lyd_dup_inst_free(dup_inst);
3805 return ret;
Michal Vasko4490d312020-06-16 13:08:55 +02003806}
3807
Michal Vasko3a41dff2020-07-15 14:30:28 +02003808API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003809lyd_merge_tree(struct lyd_node **target, const struct lyd_node *source, uint16_t options)
Michal Vasko3a41dff2020-07-15 14:30:28 +02003810{
Michal Vaskocd3f6172021-05-18 16:14:50 +02003811 return lyd_merge(target, source, NULL, NULL, NULL, options, 1);
Michal Vasko3a41dff2020-07-15 14:30:28 +02003812}
3813
3814API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003815lyd_merge_siblings(struct lyd_node **target, const struct lyd_node *source, uint16_t options)
Michal Vasko3a41dff2020-07-15 14:30:28 +02003816{
Michal Vaskocd3f6172021-05-18 16:14:50 +02003817 return lyd_merge(target, source, NULL, NULL, NULL, options, 0);
3818}
3819
3820API LY_ERR
3821lyd_merge_module(struct lyd_node **target, const struct lyd_node *source, const struct lys_module *mod,
3822 lyd_merge_cb merge_cb, void *cb_data, uint16_t options)
3823{
3824 return lyd_merge(target, source, mod, merge_cb, cb_data, options, 0);
Michal Vasko3a41dff2020-07-15 14:30:28 +02003825}
3826
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003827static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02003828lyd_path_str_enlarge(char **buffer, size_t *buflen, size_t reqlen, ly_bool is_static)
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003829{
Michal Vasko14654712020-02-06 08:35:21 +01003830 /* ending \0 */
3831 ++reqlen;
3832
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003833 if (reqlen > *buflen) {
3834 if (is_static) {
3835 return LY_EINCOMPLETE;
3836 }
3837
3838 *buffer = ly_realloc(*buffer, reqlen * sizeof **buffer);
3839 if (!*buffer) {
3840 return LY_EMEM;
3841 }
3842
3843 *buflen = reqlen;
3844 }
3845
3846 return LY_SUCCESS;
3847}
3848
Michal Vaskod59035b2020-07-08 12:00:06 +02003849LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02003850lyd_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 +02003851{
3852 const struct lyd_node *key;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003853 size_t len;
3854 const char *val;
3855 char quot;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003856
Radek Krejcia1c1e542020-09-29 16:06:52 +02003857 for (key = lyd_child(node); key && (key->schema->flags & LYS_KEY); key = key->next) {
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02003858 val = lyd_get_value(key);
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003859 len = 1 + strlen(key->schema->name) + 2 + strlen(val) + 2;
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003860 LY_CHECK_RET(lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static));
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003861
3862 quot = '\'';
3863 if (strchr(val, '\'')) {
3864 quot = '"';
3865 }
3866 *bufused += sprintf(*buffer + *bufused, "[%s=%c%s%c]", key->schema->name, quot, val, quot);
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003867 }
3868
3869 return LY_SUCCESS;
3870}
3871
3872/**
3873 * @brief Append leaf-list value predicate to path.
3874 *
3875 * @param[in] node Node to print.
3876 * @param[in,out] buffer Buffer to print to.
3877 * @param[in,out] buflen Current buffer length.
3878 * @param[in,out] bufused Current number of characters used in @p buffer.
3879 * @param[in] is_static Whether buffer is static or can be reallocated.
3880 * @return LY_ERR
3881 */
3882static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02003883lyd_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 +02003884{
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003885 size_t len;
3886 const char *val;
3887 char quot;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003888
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02003889 val = lyd_get_value(node);
Radek Krejcif13b87b2020-12-01 22:02:17 +01003890 len = 4 + strlen(val) + 2; /* "[.='" + val + "']" */
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003891 LY_CHECK_RET(lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static));
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003892
3893 quot = '\'';
3894 if (strchr(val, '\'')) {
3895 quot = '"';
3896 }
3897 *bufused += sprintf(*buffer + *bufused, "[.=%c%s%c]", quot, val, quot);
3898
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003899 return LY_SUCCESS;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003900}
3901
3902/**
3903 * @brief Append node position (relative to its other instances) predicate to path.
3904 *
3905 * @param[in] node Node to print.
3906 * @param[in,out] buffer Buffer to print to.
3907 * @param[in,out] buflen Current buffer length.
3908 * @param[in,out] bufused Current number of characters used in @p buffer.
3909 * @param[in] is_static Whether buffer is static or can be reallocated.
3910 * @return LY_ERR
3911 */
3912static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02003913lyd_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 +02003914{
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003915 size_t len;
Michal Vasko50cc0562021-05-18 16:15:43 +02003916 uint32_t pos;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003917 char *val = NULL;
3918 LY_ERR rc;
3919
Michal Vasko50cc0562021-05-18 16:15:43 +02003920 pos = lyd_list_pos(node);
3921 if (asprintf(&val, "%" PRIu32, pos) == -1) {
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003922 return LY_EMEM;
3923 }
3924
3925 len = 1 + strlen(val) + 1;
3926 rc = lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static);
3927 if (rc != LY_SUCCESS) {
3928 goto cleanup;
3929 }
3930
3931 *bufused += sprintf(*buffer + *bufused, "[%s]", val);
3932
3933cleanup:
3934 free(val);
3935 return rc;
3936}
3937
3938API char *
3939lyd_path(const struct lyd_node *node, LYD_PATH_TYPE pathtype, char *buffer, size_t buflen)
3940{
Radek Krejci857189e2020-09-01 13:26:36 +02003941 ly_bool is_static = 0;
Radek Krejci1deb5be2020-08-26 16:43:36 +02003942 uint32_t i, depth;
Michal Vasko14654712020-02-06 08:35:21 +01003943 size_t bufused = 0, len;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003944 const struct lyd_node *iter;
3945 const struct lys_module *mod;
Michal Vasko790b2bc2020-08-03 13:35:06 +02003946 LY_ERR rc = LY_SUCCESS;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003947
3948 LY_CHECK_ARG_RET(NULL, node, NULL);
3949 if (buffer) {
Michal Vasko16385f42021-05-18 16:16:09 +02003950 LY_CHECK_ARG_RET(LYD_CTX(node), buflen > 1, NULL);
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003951 is_static = 1;
Michal Vasko14654712020-02-06 08:35:21 +01003952 } else {
3953 buflen = 0;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003954 }
3955
3956 switch (pathtype) {
Radek Krejci635d2b82021-01-04 11:26:51 +01003957 case LYD_PATH_STD:
3958 case LYD_PATH_STD_NO_LAST_PRED:
Michal Vasko14654712020-02-06 08:35:21 +01003959 depth = 1;
Michal Vasko9e685082021-01-29 14:49:09 +01003960 for (iter = node; iter->parent; iter = lyd_parent(iter)) {
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003961 ++depth;
3962 }
3963
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003964 goto iter_print;
Michal Vasko14654712020-02-06 08:35:21 +01003965 while (depth) {
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003966 /* find the right node */
Michal Vasko9e685082021-01-29 14:49:09 +01003967 for (iter = node, i = 1; i < depth; iter = lyd_parent(iter), ++i) {}
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003968iter_print:
3969 /* print prefix and name */
3970 mod = NULL;
Michal Vasko69730152020-10-09 16:30:07 +02003971 if (iter->schema && (!iter->parent || (iter->schema->module != iter->parent->schema->module))) {
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003972 mod = iter->schema->module;
3973 }
3974
3975 /* realloc string */
Michal Vaskoad92b672020-11-12 13:11:31 +01003976 len = 1 + (mod ? strlen(mod->name) + 1 : 0) + (iter->schema ? strlen(iter->schema->name) :
3977 strlen(((struct lyd_node_opaq *)iter)->name.name));
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003978 rc = lyd_path_str_enlarge(&buffer, &buflen, bufused + len, is_static);
3979 if (rc != LY_SUCCESS) {
3980 break;
3981 }
3982
3983 /* print next node */
Radek Krejci1798aae2020-07-14 13:26:06 +02003984 bufused += sprintf(buffer + bufused, "/%s%s%s", mod ? mod->name : "", mod ? ":" : "",
Michal Vaskoad92b672020-11-12 13:11:31 +01003985 iter->schema ? iter->schema->name : ((struct lyd_node_opaq *)iter)->name.name);
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003986
Michal Vasko790b2bc2020-08-03 13:35:06 +02003987 /* do not always print the last (first) predicate */
Radek Krejci635d2b82021-01-04 11:26:51 +01003988 if (iter->schema && ((depth > 1) || (pathtype == LYD_PATH_STD))) {
Michal Vasko790b2bc2020-08-03 13:35:06 +02003989 switch (iter->schema->nodetype) {
3990 case LYS_LIST:
3991 if (iter->schema->flags & LYS_KEYLESS) {
3992 /* print its position */
3993 rc = lyd_path_position_predicate(iter, &buffer, &buflen, &bufused, is_static);
3994 } else {
3995 /* print all list keys in predicates */
3996 rc = lyd_path_list_predicate(iter, &buffer, &buflen, &bufused, is_static);
3997 }
3998 break;
3999 case LYS_LEAFLIST:
4000 if (iter->schema->flags & LYS_CONFIG_W) {
4001 /* print leaf-list value */
4002 rc = lyd_path_leaflist_predicate(iter, &buffer, &buflen, &bufused, is_static);
4003 } else {
4004 /* print its position */
4005 rc = lyd_path_position_predicate(iter, &buffer, &buflen, &bufused, is_static);
4006 }
4007 break;
4008 default:
4009 /* nothing to print more */
4010 break;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02004011 }
Michal Vasko5ec7cda2019-09-11 13:43:08 +02004012 }
4013 if (rc != LY_SUCCESS) {
4014 break;
4015 }
4016
Michal Vasko14654712020-02-06 08:35:21 +01004017 --depth;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02004018 }
4019 break;
4020 }
4021
4022 return buffer;
4023}
Michal Vaskoe444f752020-02-10 12:20:06 +01004024
Michal Vasko25a32822020-07-09 15:48:22 +02004025API struct lyd_meta *
4026lyd_find_meta(const struct lyd_meta *first, const struct lys_module *module, const char *name)
4027{
4028 struct lyd_meta *ret = NULL;
4029 const struct ly_ctx *ctx;
4030 const char *prefix, *tmp;
4031 char *str;
4032 size_t pref_len, name_len;
4033
4034 LY_CHECK_ARG_RET(NULL, module || strchr(name, ':'), name, NULL);
4035
4036 if (!first) {
4037 return NULL;
4038 }
4039
4040 ctx = first->annotation->module->ctx;
4041
4042 /* parse the name */
4043 tmp = name;
4044 if (ly_parse_nodeid(&tmp, &prefix, &pref_len, &name, &name_len) || tmp[0]) {
4045 LOGERR(ctx, LY_EINVAL, "Metadata name \"%s\" is not valid.", name);
4046 return NULL;
4047 }
4048
4049 /* find the module */
4050 if (prefix) {
4051 str = strndup(prefix, pref_len);
4052 module = ly_ctx_get_module_latest(ctx, str);
4053 free(str);
Radek Krejci422afb12021-03-04 16:38:16 +01004054 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 +02004055 }
4056
4057 /* find the metadata */
4058 LY_LIST_FOR(first, first) {
4059 if ((first->annotation->module == module) && !strcmp(first->name, name)) {
4060 ret = (struct lyd_meta *)first;
4061 break;
4062 }
4063 }
4064
4065 return ret;
4066}
4067
Michal Vasko9b368d32020-02-14 13:53:31 +01004068API LY_ERR
Michal Vaskoe444f752020-02-10 12:20:06 +01004069lyd_find_sibling_first(const struct lyd_node *siblings, const struct lyd_node *target, struct lyd_node **match)
4070{
Michal Vaskoe78faec2021-04-08 17:24:43 +02004071 struct lyd_node **match_p, *iter;
Michal Vaskoe444f752020-02-10 12:20:06 +01004072 struct lyd_node_inner *parent;
Michal Vaskoe78faec2021-04-08 17:24:43 +02004073 ly_bool found;
Michal Vaskoe444f752020-02-10 12:20:06 +01004074
Michal Vaskof03ed032020-03-04 13:31:44 +01004075 LY_CHECK_ARG_RET(NULL, target, LY_EINVAL);
Michal Vaskoe444f752020-02-10 12:20:06 +01004076
Michal Vasko6da1e952020-12-09 18:14:38 +01004077 if (!siblings || (siblings->schema && target->schema &&
4078 (lysc_data_parent(siblings->schema) != lysc_data_parent(target->schema)))) {
Michal Vasko62ed12d2020-05-21 10:08:25 +02004079 /* no data or schema mismatch */
Michal Vasko9b368d32020-02-14 13:53:31 +01004080 if (match) {
4081 *match = NULL;
4082 }
Michal Vaskoe444f752020-02-10 12:20:06 +01004083 return LY_ENOTFOUND;
4084 }
4085
Michal Vaskoe78faec2021-04-08 17:24:43 +02004086 /* get first sibling */
4087 siblings = lyd_first_sibling(siblings);
Michal Vaskoe444f752020-02-10 12:20:06 +01004088
Michal Vasko9e685082021-01-29 14:49:09 +01004089 parent = siblings->parent;
Michal Vasko6da1e952020-12-09 18:14:38 +01004090 if (parent && parent->schema && parent->children_ht) {
Michal Vaskoe444f752020-02-10 12:20:06 +01004091 assert(target->hash);
4092
Michal Vaskoe78faec2021-04-08 17:24:43 +02004093 if (lysc_is_dup_inst_list(target->schema)) {
4094 /* we must search the instances from beginning to find the first matching one */
4095 found = 0;
4096 LYD_LIST_FOR_INST(siblings, target->schema, iter) {
4097 if (!lyd_compare_single(target, iter, 0)) {
4098 found = 1;
4099 break;
4100 }
4101 }
4102 if (found) {
4103 siblings = iter;
Michal Vaskoda859032020-07-14 12:20:14 +02004104 } else {
4105 siblings = NULL;
4106 }
Michal Vaskoe444f752020-02-10 12:20:06 +01004107 } else {
Michal Vaskoe78faec2021-04-08 17:24:43 +02004108 /* find by hash */
4109 if (!lyht_find(parent->children_ht, &target, target->hash, (void **)&match_p)) {
4110 siblings = *match_p;
4111 } else {
4112 /* not found */
4113 siblings = NULL;
4114 }
Michal Vaskoe444f752020-02-10 12:20:06 +01004115 }
4116 } else {
4117 /* no children hash table */
Michal Vaskod989ba02020-08-24 10:59:24 +02004118 for ( ; siblings; siblings = siblings->next) {
Michal Vasko8f359bf2020-07-28 10:41:15 +02004119 if (!lyd_compare_single(siblings, target, 0)) {
Michal Vaskoe444f752020-02-10 12:20:06 +01004120 break;
4121 }
4122 }
4123 }
4124
4125 if (!siblings) {
Michal Vasko9b368d32020-02-14 13:53:31 +01004126 if (match) {
4127 *match = NULL;
4128 }
Michal Vaskoe444f752020-02-10 12:20:06 +01004129 return LY_ENOTFOUND;
4130 }
4131
Michal Vasko9b368d32020-02-14 13:53:31 +01004132 if (match) {
4133 *match = (struct lyd_node *)siblings;
4134 }
Michal Vaskoe444f752020-02-10 12:20:06 +01004135 return LY_SUCCESS;
4136}
4137
Radek Krejci857189e2020-09-01 13:26:36 +02004138/**
4139 * @brief Comparison callback to match schema node with a schema of a data node.
4140 *
4141 * @param[in] val1_p Pointer to the schema node
4142 * @param[in] val2_p Pointer to the data node
Michal Vasko62524a92021-02-26 10:08:50 +01004143 * Implementation of ::lyht_value_equal_cb.
Radek Krejci857189e2020-09-01 13:26:36 +02004144 */
4145static ly_bool
4146lyd_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 +01004147{
4148 struct lysc_node *val1;
4149 struct lyd_node *val2;
4150
4151 val1 = *((struct lysc_node **)val1_p);
4152 val2 = *((struct lyd_node **)val2_p);
4153
Michal Vasko90932a92020-02-12 14:33:03 +01004154 if (val1 == val2->schema) {
4155 /* schema match is enough */
4156 return 1;
4157 } else {
4158 return 0;
4159 }
4160}
4161
Michal Vasko92239c72020-11-18 18:17:25 +01004162/**
4163 * @brief Search in the given siblings (NOT recursively) for the first schema node data instance.
4164 * Uses hashes - should be used whenever possible for best performance.
4165 *
4166 * @param[in] siblings Siblings to search in including preceding and succeeding nodes.
4167 * @param[in] schema Target data node schema to find.
4168 * @param[out] match Can be NULL, otherwise the found data node.
4169 * @return LY_SUCCESS on success, @p match set.
4170 * @return LY_ENOTFOUND if not found, @p match set to NULL.
4171 * @return LY_ERR value if another error occurred.
4172 */
Michal Vasko90932a92020-02-12 14:33:03 +01004173static LY_ERR
4174lyd_find_sibling_schema(const struct lyd_node *siblings, const struct lysc_node *schema, struct lyd_node **match)
4175{
4176 struct lyd_node **match_p;
4177 struct lyd_node_inner *parent;
4178 uint32_t hash;
Michal Vasko62524a92021-02-26 10:08:50 +01004179 lyht_value_equal_cb ht_cb;
Michal Vasko90932a92020-02-12 14:33:03 +01004180
Michal Vaskob104f112020-07-17 09:54:54 +02004181 assert(siblings && schema);
Michal Vasko90932a92020-02-12 14:33:03 +01004182
Michal Vasko9e685082021-01-29 14:49:09 +01004183 parent = siblings->parent;
Michal Vasko08fd6132020-11-18 18:17:45 +01004184 if (parent && parent->schema && parent->children_ht) {
Michal Vasko90932a92020-02-12 14:33:03 +01004185 /* calculate our hash */
4186 hash = dict_hash_multi(0, schema->module->name, strlen(schema->module->name));
4187 hash = dict_hash_multi(hash, schema->name, strlen(schema->name));
4188 hash = dict_hash_multi(hash, NULL, 0);
4189
4190 /* use special hash table function */
4191 ht_cb = lyht_set_cb(parent->children_ht, lyd_hash_table_schema_val_equal);
4192
4193 /* find by hash */
4194 if (!lyht_find(parent->children_ht, &schema, hash, (void **)&match_p)) {
4195 siblings = *match_p;
4196 } else {
4197 /* not found */
4198 siblings = NULL;
4199 }
4200
4201 /* set the original hash table compare function back */
4202 lyht_set_cb(parent->children_ht, ht_cb);
4203 } else {
Michal Vaskob104f112020-07-17 09:54:54 +02004204 /* find first sibling */
4205 if (siblings->parent) {
4206 siblings = siblings->parent->child;
4207 } else {
4208 while (siblings->prev->next) {
4209 siblings = siblings->prev;
4210 }
4211 }
4212
4213 /* search manually without hashes */
Michal Vaskod989ba02020-08-24 10:59:24 +02004214 for ( ; siblings; siblings = siblings->next) {
Michal Vasko90932a92020-02-12 14:33:03 +01004215 if (siblings->schema == schema) {
4216 /* schema match is enough */
4217 break;
4218 }
4219 }
4220 }
4221
4222 if (!siblings) {
Michal Vasko9b368d32020-02-14 13:53:31 +01004223 if (match) {
4224 *match = NULL;
4225 }
Michal Vasko90932a92020-02-12 14:33:03 +01004226 return LY_ENOTFOUND;
4227 }
4228
Michal Vasko9b368d32020-02-14 13:53:31 +01004229 if (match) {
4230 *match = (struct lyd_node *)siblings;
4231 }
Michal Vasko90932a92020-02-12 14:33:03 +01004232 return LY_SUCCESS;
4233}
4234
Michal Vaskoe444f752020-02-10 12:20:06 +01004235API LY_ERR
4236lyd_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 +02004237 size_t val_len, struct lyd_node **match)
Michal Vaskoe444f752020-02-10 12:20:06 +01004238{
4239 LY_ERR rc;
4240 struct lyd_node *target = NULL;
4241
Michal Vasko4c583e82020-07-17 12:16:14 +02004242 LY_CHECK_ARG_RET(NULL, schema, !(schema->nodetype & (LYS_CHOICE | LYS_CASE)), LY_EINVAL);
Michal Vaskoe444f752020-02-10 12:20:06 +01004243
Michal Vasko08fd6132020-11-18 18:17:45 +01004244 if (!siblings || (siblings->schema && (lysc_data_parent(siblings->schema) != lysc_data_parent(schema)))) {
Michal Vasko62ed12d2020-05-21 10:08:25 +02004245 /* no data or schema mismatch */
Michal Vasko9b368d32020-02-14 13:53:31 +01004246 if (match) {
4247 *match = NULL;
4248 }
Michal Vaskoe444f752020-02-10 12:20:06 +01004249 return LY_ENOTFOUND;
4250 }
4251
Michal Vaskof03ed032020-03-04 13:31:44 +01004252 if (key_or_value && !val_len) {
4253 val_len = strlen(key_or_value);
4254 }
4255
Michal Vaskob104f112020-07-17 09:54:54 +02004256 if ((schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) && key_or_value) {
4257 /* create a data node and find the instance */
4258 if (schema->nodetype == LYS_LEAFLIST) {
4259 /* target used attributes: schema, hash, value */
Radek Krejci8df109d2021-04-23 12:19:08 +02004260 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 +02004261 LY_CHECK_RET(rc);
Michal Vaskob104f112020-07-17 09:54:54 +02004262 } else {
Michal Vasko90932a92020-02-12 14:33:03 +01004263 /* target used attributes: schema, hash, child (all keys) */
Michal Vasko004d3152020-06-11 19:59:22 +02004264 LY_CHECK_RET(lyd_create_list2(schema, key_or_value, val_len, &target));
Michal Vasko90932a92020-02-12 14:33:03 +01004265 }
4266
4267 /* find it */
4268 rc = lyd_find_sibling_first(siblings, target, match);
Michal Vaskob104f112020-07-17 09:54:54 +02004269 } else {
4270 /* find the first schema node instance */
4271 rc = lyd_find_sibling_schema(siblings, schema, match);
Michal Vaskoe444f752020-02-10 12:20:06 +01004272 }
4273
Michal Vaskoe444f752020-02-10 12:20:06 +01004274 lyd_free_tree(target);
4275 return rc;
4276}
Michal Vaskoccc02342020-05-21 10:09:21 +02004277
4278API LY_ERR
Michal Vaskoe78faec2021-04-08 17:24:43 +02004279lyd_find_sibling_dup_inst_set(const struct lyd_node *siblings, const struct lyd_node *target, struct ly_set **set)
4280{
4281 struct lyd_node **match_p, *first, *iter;
4282 struct lyd_node_inner *parent;
4283
4284 LY_CHECK_ARG_RET(NULL, target, lysc_is_dup_inst_list(target->schema), set, LY_EINVAL);
4285
4286 LY_CHECK_RET(ly_set_new(set));
4287
4288 if (!siblings || (siblings->schema && (lysc_data_parent(siblings->schema) != lysc_data_parent(target->schema)))) {
4289 /* no data or schema mismatch */
4290 return LY_ENOTFOUND;
4291 }
4292
4293 /* get first sibling */
4294 siblings = lyd_first_sibling(siblings);
4295
4296 parent = siblings->parent;
4297 if (parent && parent->schema && parent->children_ht) {
4298 assert(target->hash);
4299
4300 /* find the first instance */
4301 lyd_find_sibling_first(siblings, target, &first);
4302 if (first) {
4303 /* add it so that it is the first in the set */
4304 if (ly_set_add(*set, first, 1, NULL)) {
4305 goto error;
4306 }
4307
4308 /* find by hash */
4309 if (!lyht_find(parent->children_ht, &target, target->hash, (void **)&match_p)) {
4310 iter = *match_p;
4311 } else {
4312 /* not found */
4313 iter = NULL;
4314 }
4315 while (iter) {
4316 /* add all found nodes into the set */
4317 if ((iter != first) && !lyd_compare_single(iter, target, 0) && ly_set_add(*set, iter, 1, NULL)) {
4318 goto error;
4319 }
4320
4321 /* find next instance */
4322 if (lyht_find_next(parent->children_ht, &iter, iter->hash, (void **)&match_p)) {
4323 iter = NULL;
4324 } else {
4325 iter = *match_p;
4326 }
4327 }
4328 }
4329 } else {
4330 /* no children hash table */
4331 LY_LIST_FOR(siblings, siblings) {
4332 if (!lyd_compare_single(target, siblings, 0)) {
4333 ly_set_add(*set, (void *)siblings, 1, NULL);
4334 }
4335 }
4336 }
4337
4338 if (!(*set)->count) {
4339 return LY_ENOTFOUND;
4340 }
4341 return LY_SUCCESS;
4342
4343error:
4344 ly_set_free(*set, NULL);
4345 *set = NULL;
4346 return LY_EMEM;
4347}
4348
4349API LY_ERR
Michal Vasko1d4af6c2021-02-22 13:31:26 +01004350lyd_find_sibling_opaq_next(const struct lyd_node *first, const char *name, struct lyd_node **match)
4351{
4352 LY_CHECK_ARG_RET(NULL, name, LY_EINVAL);
4353
4354 for ( ; first; first = first->next) {
4355 if (!first->schema && !strcmp(LYD_NAME(first), name)) {
4356 break;
4357 }
4358 }
4359
4360 if (match) {
4361 *match = (struct lyd_node *)first;
4362 }
4363 return first ? LY_SUCCESS : LY_ENOTFOUND;
4364}
4365
4366API LY_ERR
Michal Vaskoccc02342020-05-21 10:09:21 +02004367lyd_find_xpath(const struct lyd_node *ctx_node, const char *xpath, struct ly_set **set)
4368{
4369 LY_ERR ret = LY_SUCCESS;
Michal Vasko8bd9cc72021-07-16 14:06:10 +02004370 struct lyxp_set xp_set = {0};
Radek Krejcif03a9e22020-09-18 20:09:31 +02004371 struct lyxp_expr *exp = NULL;
Michal Vaskoccc02342020-05-21 10:09:21 +02004372 uint32_t i;
4373
4374 LY_CHECK_ARG_RET(NULL, ctx_node, xpath, set, LY_EINVAL);
4375
Michal Vasko37c69432021-04-12 14:48:24 +02004376 *set = NULL;
Michal Vaskoccc02342020-05-21 10:09:21 +02004377
4378 /* compile expression */
Radek Krejcif03a9e22020-09-18 20:09:31 +02004379 ret = lyxp_expr_parse((struct ly_ctx *)LYD_CTX(ctx_node), xpath, 0, 1, &exp);
4380 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskoccc02342020-05-21 10:09:21 +02004381
4382 /* evaluate expression */
Radek Krejci8df109d2021-04-23 12:19:08 +02004383 ret = lyxp_eval(LYD_CTX(ctx_node), exp, NULL, LY_VALUE_JSON, NULL, ctx_node, ctx_node, &xp_set, LYXP_IGNORE_WHEN);
Michal Vaskoccc02342020-05-21 10:09:21 +02004384 LY_CHECK_GOTO(ret, cleanup);
4385
4386 /* allocate return set */
Radek Krejciba03a5a2020-08-27 14:40:41 +02004387 ret = ly_set_new(set);
4388 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskoccc02342020-05-21 10:09:21 +02004389
4390 /* transform into ly_set */
4391 if (xp_set.type == LYXP_SET_NODE_SET) {
4392 /* allocate memory for all the elements once (even though not all items must be elements but most likely will be) */
4393 (*set)->objs = malloc(xp_set.used * sizeof *(*set)->objs);
Michal Vaskob7be7a82020-08-20 09:09:04 +02004394 LY_CHECK_ERR_GOTO(!(*set)->objs, LOGMEM(LYD_CTX(ctx_node)); ret = LY_EMEM, cleanup);
Michal Vaskoccc02342020-05-21 10:09:21 +02004395 (*set)->size = xp_set.used;
4396
4397 for (i = 0; i < xp_set.used; ++i) {
4398 if (xp_set.val.nodes[i].type == LYXP_NODE_ELEM) {
Radek Krejci3d92e442020-10-12 12:48:13 +02004399 ret = ly_set_add(*set, xp_set.val.nodes[i].node, 1, NULL);
Radek Krejciba03a5a2020-08-27 14:40:41 +02004400 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskoccc02342020-05-21 10:09:21 +02004401 }
4402 }
4403 }
4404
4405cleanup:
Michal Vasko0691d522020-05-21 13:21:47 +02004406 lyxp_set_free_content(&xp_set);
Michal Vaskob7be7a82020-08-20 09:09:04 +02004407 lyxp_expr_free((struct ly_ctx *)LYD_CTX(ctx_node), exp);
Radek Krejci8f45abc2020-11-26 12:15:13 +01004408 if (ret) {
4409 ly_set_free(*set, NULL);
4410 *set = NULL;
4411 }
Michal Vaskoccc02342020-05-21 10:09:21 +02004412 return ret;
4413}
Radek Krejcica989142020-11-05 11:32:22 +01004414
Michal Vasko3e1f6552021-01-14 09:27:55 +01004415API LY_ERR
Michal Vaskoc9eb3ca2021-07-16 10:20:37 +02004416lyd_eval_xpath(const struct lyd_node *ctx_node, const char *xpath, ly_bool *result)
4417{
4418 LY_ERR ret = LY_SUCCESS;
Michal Vasko8bd9cc72021-07-16 14:06:10 +02004419 struct lyxp_set xp_set = {0};
Michal Vaskoc9eb3ca2021-07-16 10:20:37 +02004420 struct lyxp_expr *exp = NULL;
4421
4422 LY_CHECK_ARG_RET(NULL, ctx_node, xpath, result, LY_EINVAL);
4423
4424 /* compile expression */
4425 ret = lyxp_expr_parse((struct ly_ctx *)LYD_CTX(ctx_node), xpath, 0, 1, &exp);
4426 LY_CHECK_GOTO(ret, cleanup);
4427
4428 /* evaluate expression */
4429 ret = lyxp_eval(LYD_CTX(ctx_node), exp, NULL, LY_VALUE_JSON, NULL, ctx_node, ctx_node, &xp_set, LYXP_IGNORE_WHEN);
4430 LY_CHECK_GOTO(ret, cleanup);
4431
4432 /* transform into boolean */
4433 ret = lyxp_set_cast(&xp_set, LYXP_SET_BOOLEAN);
4434 LY_CHECK_GOTO(ret, cleanup);
4435
4436 /* set result */
4437 *result = xp_set.val.bln;
4438
4439cleanup:
4440 lyxp_set_free_content(&xp_set);
4441 lyxp_expr_free((struct ly_ctx *)LYD_CTX(ctx_node), exp);
4442 return ret;
4443}
4444
4445API LY_ERR
Michal Vasko3e1f6552021-01-14 09:27:55 +01004446lyd_find_path(const struct lyd_node *ctx_node, const char *path, ly_bool output, struct lyd_node **match)
4447{
4448 LY_ERR ret = LY_SUCCESS;
4449 struct lyxp_expr *expr = NULL;
4450 struct ly_path *lypath = NULL;
4451
4452 LY_CHECK_ARG_RET(NULL, ctx_node, ctx_node->schema, path, LY_EINVAL);
4453
4454 /* parse the path */
Michal Vaskoed725d72021-06-23 12:03:45 +02004455 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 +01004456 LY_PATH_PREFIX_OPTIONAL, LY_PATH_PRED_SIMPLE, &expr);
4457 LY_CHECK_GOTO(ret, cleanup);
4458
4459 /* compile the path */
Michal Vaskoed725d72021-06-23 12:03:45 +02004460 ret = ly_path_compile(LYD_CTX(ctx_node), NULL, ctx_node->schema, NULL, expr,
4461 output ? LY_PATH_OPER_OUTPUT : LY_PATH_OPER_INPUT, LY_PATH_TARGET_SINGLE, LY_VALUE_JSON, NULL, &lypath);
Michal Vasko3e1f6552021-01-14 09:27:55 +01004462 LY_CHECK_GOTO(ret, cleanup);
4463
4464 /* evaluate the path */
4465 ret = ly_path_eval_partial(lypath, ctx_node, NULL, match);
4466
4467cleanup:
4468 lyxp_expr_free(LYD_CTX(ctx_node), expr);
4469 ly_path_free(LYD_CTX(ctx_node), lypath);
4470 return ret;
4471}
4472
Michal Vaskobb22b182021-06-14 08:14:21 +02004473API LY_ERR
4474lyd_find_target(const struct ly_path *path, const struct lyd_node *tree, struct lyd_node **match)
4475{
4476 LY_ERR ret;
4477 struct lyd_node *m;
4478
4479 LY_CHECK_ARG_RET(NULL, path, LY_EINVAL);
4480
4481 ret = ly_path_eval(path, tree, &m);
4482 if (ret) {
4483 if (match) {
4484 *match = NULL;
4485 }
4486 return LY_ENOTFOUND;
4487 }
4488
4489 if (match) {
4490 *match = m;
4491 }
4492 return LY_SUCCESS;
4493}
4494
Radek Krejcica989142020-11-05 11:32:22 +01004495API uint32_t
4496lyd_list_pos(const struct lyd_node *instance)
4497{
4498 const struct lyd_node *iter = NULL;
4499 uint32_t pos = 0;
4500
4501 if (!instance || !(instance->schema->nodetype & (LYS_LIST | LYS_LEAFLIST))) {
4502 return 0;
4503 }
4504
4505 /* data instances are ordered, so we can stop when we found instance of other schema node */
4506 for (iter = instance; iter->schema == instance->schema; iter = iter->prev) {
Radek Krejcibb27df32020-11-13 15:39:16 +01004507 if (pos && (iter->next == NULL)) {
Radek Krejcica989142020-11-05 11:32:22 +01004508 /* overrun to the end of the siblings list */
4509 break;
4510 }
4511 ++pos;
4512 }
4513
4514 return pos;
4515}
Radek Krejci4233f9b2020-11-05 12:38:35 +01004516
4517API struct lyd_node *
4518lyd_first_sibling(const struct lyd_node *node)
4519{
4520 struct lyd_node *start;
4521
4522 if (!node) {
4523 return NULL;
4524 }
4525
4526 /* get the first sibling */
4527 if (node->parent) {
4528 start = node->parent->child;
4529 } else {
Radek Krejcibb27df32020-11-13 15:39:16 +01004530 for (start = (struct lyd_node *)node; start->prev->next; start = start->prev) {}
Radek Krejci4233f9b2020-11-05 12:38:35 +01004531 }
4532
4533 return start;
4534}