blob: c996d39df59990110592a1c76c2c19f748786542 [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
Michal Vasko39eb6882021-09-16 08:53:13 +0200267 if (!(node->schema->nodetype & LYD_NODE_TERM)) {
268 return 0;
269 }
270
Radek Krejci19611252020-10-04 13:54:53 +0200271 term = (const struct lyd_node_term *)node;
272
273 if (node->schema->nodetype == LYS_LEAF) {
274 leaf = (const struct lysc_node_leaf *)node->schema;
275 if (!leaf->dflt) {
276 return 0;
277 }
278
279 /* compare with the default value */
Michal Vasko39eb6882021-09-16 08:53:13 +0200280 if (!leaf->type->plugin->compare(&term->value, leaf->dflt)) {
281 return 1;
Radek Krejci19611252020-10-04 13:54:53 +0200282 }
283 } else {
284 llist = (const struct lysc_node_leaflist *)node->schema;
285 if (!llist->dflts) {
286 return 0;
287 }
288
289 LY_ARRAY_FOR(llist->dflts, u) {
290 /* compare with each possible default value */
Michal Vasko39eb6882021-09-16 08:53:13 +0200291 if (!llist->type->plugin->compare(&term->value, llist->dflts[u])) {
292 return 1;
Radek Krejci19611252020-10-04 13:54:53 +0200293 }
294 }
295 }
296
Michal Vasko39eb6882021-09-16 08:53:13 +0200297 return 0;
Radek Krejci19611252020-10-04 13:54:53 +0200298}
299
Radek Krejci7931b192020-06-25 17:05:03 +0200300static LYD_FORMAT
Michal Vasko63f3d842020-07-08 10:10:14 +0200301lyd_parse_get_format(const struct ly_in *in, LYD_FORMAT format)
Radek Krejcie7b95092019-05-15 11:03:07 +0200302{
Michal Vasko69730152020-10-09 16:30:07 +0200303 if (!format && (in->type == LY_IN_FILEPATH)) {
Radek Krejcie7b95092019-05-15 11:03:07 +0200304 /* unknown format - try to detect it from filename's suffix */
Radek Krejci7931b192020-06-25 17:05:03 +0200305 const char *path = in->method.fpath.filepath;
306 size_t len = strlen(path);
Radek Krejcie7b95092019-05-15 11:03:07 +0200307
308 /* ignore trailing whitespaces */
Michal Vaskod989ba02020-08-24 10:59:24 +0200309 for ( ; len > 0 && isspace(path[len - 1]); len--) {}
Radek Krejcie7b95092019-05-15 11:03:07 +0200310
Radek Krejcif13b87b2020-12-01 22:02:17 +0100311 if ((len >= LY_XML_SUFFIX_LEN + 1) &&
312 !strncmp(&path[len - LY_XML_SUFFIX_LEN], LY_XML_SUFFIX, LY_XML_SUFFIX_LEN)) {
Radek Krejcie7b95092019-05-15 11:03:07 +0200313 format = LYD_XML;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100314 } else if ((len >= LY_JSON_SUFFIX_LEN + 1) &&
315 !strncmp(&path[len - LY_JSON_SUFFIX_LEN], LY_JSON_SUFFIX, LY_JSON_SUFFIX_LEN)) {
Radek Krejcie7b95092019-05-15 11:03:07 +0200316 format = LYD_JSON;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100317 } else if ((len >= LY_LYB_SUFFIX_LEN + 1) &&
318 !strncmp(&path[len - LY_LYB_SUFFIX_LEN], LY_LYB_SUFFIX, LY_LYB_SUFFIX_LEN)) {
Radek Krejcie7b95092019-05-15 11:03:07 +0200319 format = LYD_LYB;
Radek Krejci7931b192020-06-25 17:05:03 +0200320 } /* else still unknown */
Radek Krejcie7b95092019-05-15 11:03:07 +0200321 }
322
Radek Krejci7931b192020-06-25 17:05:03 +0200323 return format;
324}
Radek Krejcie7b95092019-05-15 11:03:07 +0200325
Michal Vaskoe0665742021-02-11 11:08:44 +0100326/**
327 * @brief Parse YANG data into a data tree.
328 *
329 * @param[in] ctx libyang context.
Radek Krejcif16e2542021-02-17 15:39:23 +0100330 * @param[in] ext Optional extenion instance to parse data following the schema tree specified in the extension instance
Michal Vaskoe0665742021-02-11 11:08:44 +0100331 * @param[in] parent Parent to connect the parsed nodes to, if any.
332 * @param[in,out] first_p Pointer to the first top-level parsed node, used only if @p parent is NULL.
333 * @param[in] in Input handle to read the input from.
334 * @param[in] format Expected format of the data in @p in.
335 * @param[in] parse_opts Options for parser.
336 * @param[in] val_opts Options for validation.
337 * @param[out] op Optional pointer to the parsed operation, if any.
338 * @return LY_ERR value.
339 */
340static LY_ERR
Radek Krejcif16e2542021-02-17 15:39:23 +0100341lyd_parse(const struct ly_ctx *ctx, const struct lysc_ext_instance *ext, struct lyd_node *parent, struct lyd_node **first_p,
342 struct ly_in *in, LYD_FORMAT format, uint32_t parse_opts, uint32_t val_opts, struct lyd_node **op)
Radek Krejci7931b192020-06-25 17:05:03 +0200343{
Michal Vaskoe0665742021-02-11 11:08:44 +0100344 LY_ERR rc = LY_SUCCESS;
Radek Krejci1798aae2020-07-14 13:26:06 +0200345 struct lyd_ctx *lydctx = NULL;
Michal Vaskoe0665742021-02-11 11:08:44 +0100346 struct ly_set parsed = {0};
347 struct lyd_node *first;
348 uint32_t i;
Radek Krejci1798aae2020-07-14 13:26:06 +0200349
Michal Vaskoe0665742021-02-11 11:08:44 +0100350 assert(ctx && (parent || first_p));
Radek Krejci7931b192020-06-25 17:05:03 +0200351
352 format = lyd_parse_get_format(in, format);
Michal Vaskoe0665742021-02-11 11:08:44 +0100353 if (first_p) {
354 *first_p = NULL;
355 }
Radek Krejci1798aae2020-07-14 13:26:06 +0200356
Michal Vasko63f3d842020-07-08 10:10:14 +0200357 /* remember input position */
358 in->func_start = in->current;
Radek Krejci7931b192020-06-25 17:05:03 +0200359
Michal Vaskoe0665742021-02-11 11:08:44 +0100360 /* parse the data */
Radek Krejci7931b192020-06-25 17:05:03 +0200361 switch (format) {
362 case LYD_XML:
Radek Krejcif16e2542021-02-17 15:39:23 +0100363 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 +0200364 break;
Radek Krejci7931b192020-06-25 17:05:03 +0200365 case LYD_JSON:
Radek Krejcif16e2542021-02-17 15:39:23 +0100366 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 +0200367 break;
Radek Krejci7931b192020-06-25 17:05:03 +0200368 case LYD_LYB:
Radek Krejcif16e2542021-02-17 15:39:23 +0100369 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 +0200370 break;
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200371 case LYD_UNKNOWN:
Michal Vaskod74978f2021-02-12 11:59:36 +0100372 LOGARG(ctx, format);
373 rc = LY_EINVAL;
Michal Vaskoe0665742021-02-11 11:08:44 +0100374 break;
375 }
376 LY_CHECK_GOTO(rc, cleanup);
377
378 if (parent) {
379 /* get first top-level sibling */
380 for (first = parent; first->parent; first = lyd_parent(first)) {}
381 first = lyd_first_sibling(first);
382 first_p = &first;
Radek Krejci7931b192020-06-25 17:05:03 +0200383 }
384
Michal Vaskoe0665742021-02-11 11:08:44 +0100385 if (!(parse_opts & LYD_PARSE_ONLY)) {
386 /* validate data */
Radek Krejci4f2e3e52021-03-30 14:20:28 +0200387 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 +0100388 &lydctx->meta_types, NULL);
389 LY_CHECK_GOTO(rc, cleanup);
390 }
Radek Krejci7931b192020-06-25 17:05:03 +0200391
Michal Vaskoe0665742021-02-11 11:08:44 +0100392 /* set the operation node */
393 if (op) {
394 *op = lydctx->op_node;
Radek Krejci1798aae2020-07-14 13:26:06 +0200395 }
396
397cleanup:
Michal Vaskoe0665742021-02-11 11:08:44 +0100398 if (lydctx) {
399 lydctx->free(lydctx);
Radek Krejci1798aae2020-07-14 13:26:06 +0200400 }
Michal Vaskoe0665742021-02-11 11:08:44 +0100401 if (rc) {
402 if (parent) {
403 /* free all the parsed subtrees */
404 for (i = 0; i < parsed.count; ++i) {
405 lyd_free_tree(parsed.dnodes[i]);
406 }
407 } else {
408 /* free everything */
409 lyd_free_all(*first_p);
410 *first_p = NULL;
411 }
412 }
413 ly_set_erase(&parsed, NULL);
414 return rc;
Radek Krejci7931b192020-06-25 17:05:03 +0200415}
416
417API LY_ERR
Radek Krejcif16e2542021-02-17 15:39:23 +0100418lyd_parse_ext_data(const struct lysc_ext_instance *ext, struct lyd_node *parent, struct ly_in *in, LYD_FORMAT format,
419 uint32_t parse_options, uint32_t validate_options, struct lyd_node **tree)
420{
421 const struct ly_ctx *ctx = ext ? ext->module->ctx : NULL;
422
423 LY_CHECK_ARG_RET(ctx, ext, in, parent || tree, LY_EINVAL);
424 LY_CHECK_ARG_RET(ctx, !(parse_options & ~LYD_PARSE_OPTS_MASK), LY_EINVAL);
425 LY_CHECK_ARG_RET(ctx, !(validate_options & ~LYD_VALIDATE_OPTS_MASK), LY_EINVAL);
426
427 return lyd_parse(ctx, ext, parent, tree, in, format, parse_options, validate_options, NULL);
428}
429
430API LY_ERR
Michal Vaskoe0665742021-02-11 11:08:44 +0100431lyd_parse_data(const struct ly_ctx *ctx, struct lyd_node *parent, struct ly_in *in, LYD_FORMAT format,
432 uint32_t parse_options, uint32_t validate_options, struct lyd_node **tree)
433{
434 LY_CHECK_ARG_RET(ctx, ctx, in, parent || tree, LY_EINVAL);
435 LY_CHECK_ARG_RET(ctx, !(parse_options & ~LYD_PARSE_OPTS_MASK), LY_EINVAL);
436 LY_CHECK_ARG_RET(ctx, !(validate_options & ~LYD_VALIDATE_OPTS_MASK), LY_EINVAL);
437
Radek Krejcif16e2542021-02-17 15:39:23 +0100438 return lyd_parse(ctx, NULL, parent, tree, in, format, parse_options, validate_options, NULL);
Michal Vaskoe0665742021-02-11 11:08:44 +0100439}
440
441API LY_ERR
442lyd_parse_data_mem(const struct ly_ctx *ctx, const char *data, LYD_FORMAT format, uint32_t parse_options,
443 uint32_t validate_options, struct lyd_node **tree)
Radek Krejci7931b192020-06-25 17:05:03 +0200444{
445 LY_ERR ret;
446 struct ly_in *in;
447
448 LY_CHECK_RET(ly_in_new_memory(data, &in));
Michal Vaskoe0665742021-02-11 11:08:44 +0100449 ret = lyd_parse_data(ctx, NULL, in, format, parse_options, validate_options, tree);
Radek Krejci7931b192020-06-25 17:05:03 +0200450
451 ly_in_free(in, 0);
452 return ret;
453}
454
455API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200456lyd_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 +0200457 struct lyd_node **tree)
Radek Krejci7931b192020-06-25 17:05:03 +0200458{
459 LY_ERR ret;
460 struct ly_in *in;
461
462 LY_CHECK_RET(ly_in_new_fd(fd, &in));
Michal Vaskoe0665742021-02-11 11:08:44 +0100463 ret = lyd_parse_data(ctx, NULL, in, format, parse_options, validate_options, tree);
Radek Krejci7931b192020-06-25 17:05:03 +0200464
465 ly_in_free(in, 0);
466 return ret;
467}
468
469API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200470lyd_parse_data_path(const struct ly_ctx *ctx, const char *path, LYD_FORMAT format, uint32_t parse_options,
471 uint32_t validate_options, struct lyd_node **tree)
Radek Krejci7931b192020-06-25 17:05:03 +0200472{
473 LY_ERR ret;
474 struct ly_in *in;
475
476 LY_CHECK_RET(ly_in_new_filepath(path, 0, &in));
Michal Vaskoe0665742021-02-11 11:08:44 +0100477 ret = lyd_parse_data(ctx, NULL, in, format, parse_options, validate_options, tree);
Radek Krejci7931b192020-06-25 17:05:03 +0200478
479 ly_in_free(in, 0);
480 return ret;
481}
482
Radek Krejcif16e2542021-02-17 15:39:23 +0100483/**
484 * @brief Parse YANG data into an operation data tree, in case the extension instance is specified, keep the searching
485 * for schema nodes locked inside the extension instance.
486 *
487 * At least one of @p parent, @p tree, or @p op must always be set.
488 *
489 * Specific @p data_type values have different parameter meaning as follows:
490 * - ::LYD_TYPE_RPC_NETCONF:
491 * - @p parent - must be NULL, the whole RPC is expected;
492 * - @p format - must be ::LYD_XML, NETCONF supports only this format;
493 * - @p tree - must be provided, all the NETCONF-specific XML envelopes will be returned here as
494 * a separate opaque data tree, even if the function fails, this may be returned;
495 * - @p op - must be provided, the RPC/action data tree itself will be returned here, pointing to the operation;
496 *
497 * - ::LYD_TYPE_NOTIF_NETCONF:
498 * - @p parent - must be NULL, the whole notification is expected;
499 * - @p format - must be ::LYD_XML, NETCONF supports only this format;
500 * - @p tree - must be provided, all the NETCONF-specific XML envelopes will be returned here as
501 * a separate opaque data tree, even if the function fails, this may be returned;
502 * - @p op - must be provided, the notification data tree itself will be returned here, pointing to the operation;
503 *
504 * - ::LYD_TYPE_REPLY_NETCONF:
505 * - @p parent - must be set, pointing to the invoked RPC operation (RPC or action) node;
506 * - @p format - must be ::LYD_XML, NETCONF supports only this format;
507 * - @p tree - must be provided, all the NETCONF-specific XML envelopes will be returned here as
508 * a separate opaque data tree, even if the function fails, this may be returned;
509 * - @p op - must be NULL, the reply is appended to the RPC;
510 * Note that there are 3 kinds of NETCONF replies - ok, error, and data. Only data reply appends any nodes to the RPC.
511 *
512 * @param[in] ctx libyang context.
513 * @param[in] ext Extension instance providing the specific schema tree to match with the data being parsed.
514 * @param[in] parent Optional parent to connect the parsed nodes to.
515 * @param[in] in Input handle to read the input from.
516 * @param[in] format Expected format of the data in @p in.
517 * @param[in] data_type Expected operation to parse (@ref datatype).
518 * @param[out] tree Optional full parsed data tree. If @p parent is set, set to NULL.
519 * @param[out] op Optional parsed operation node.
520 * @return LY_ERR value.
521 * @return LY_ENOT if @p data_type is a NETCONF message and the root XML element is not the expected one.
522 */
523static LY_ERR
524lyd_parse_op_(const struct ly_ctx *ctx, const struct lysc_ext_instance *ext, struct lyd_node *parent,
525 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 +0200526{
Michal Vaskoe0665742021-02-11 11:08:44 +0100527 LY_ERR rc = LY_SUCCESS;
528 struct lyd_ctx *lydctx = NULL;
529 struct ly_set parsed = {0};
530 struct lyd_node *first = NULL, *envp = NULL;
531 uint32_t i, parse_opts, val_opts;
Radek Krejci7931b192020-06-25 17:05:03 +0200532
Michal Vasko27fb0262021-02-23 09:42:01 +0100533 if (!ctx) {
534 ctx = LYD_CTX(parent);
535 }
Radek Krejci1798aae2020-07-14 13:26:06 +0200536 if (tree) {
537 *tree = NULL;
538 }
539 if (op) {
540 *op = NULL;
541 }
542
Radek Krejci7931b192020-06-25 17:05:03 +0200543 format = lyd_parse_get_format(in, format);
Radek Krejci7931b192020-06-25 17:05:03 +0200544
Michal Vasko63f3d842020-07-08 10:10:14 +0200545 /* remember input position */
546 in->func_start = in->current;
547
Michal Vaskoe0665742021-02-11 11:08:44 +0100548 /* check params based on the data type */
Michal Vasko1e4c68e2021-02-18 15:03:01 +0100549 if ((data_type == LYD_TYPE_RPC_NETCONF) || (data_type == LYD_TYPE_NOTIF_NETCONF)) {
Michal Vaskoe0665742021-02-11 11:08:44 +0100550 LY_CHECK_ARG_RET(ctx, format == LYD_XML, !parent, tree, op, LY_EINVAL);
Michal Vasko1e4c68e2021-02-18 15:03:01 +0100551 } else if (data_type == LYD_TYPE_REPLY_NETCONF) {
552 LY_CHECK_ARG_RET(ctx, format == LYD_XML, parent, parent->schema->nodetype & (LYS_RPC | LYS_ACTION), tree, !op,
553 LY_EINVAL);
Michal Vaskoe0665742021-02-11 11:08:44 +0100554 }
Michal Vasko9da78cf2021-07-21 13:45:10 +0200555 parse_opts = LYD_PARSE_ONLY | LYD_PARSE_OPAQ;
Michal Vaskoe0665742021-02-11 11:08:44 +0100556 val_opts = 0;
557
558 /* parse the data */
Radek Krejci7931b192020-06-25 17:05:03 +0200559 switch (format) {
560 case LYD_XML:
Radek Krejcif16e2542021-02-17 15:39:23 +0100561 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 +0100562 if (rc && envp) {
563 /* special situation when the envelopes were parsed successfully */
564 if (tree) {
565 *tree = envp;
566 }
567 ly_set_erase(&parsed, NULL);
568 return rc;
569 }
Michal Vaskoe0665742021-02-11 11:08:44 +0100570 break;
Radek Krejci7931b192020-06-25 17:05:03 +0200571 case LYD_JSON:
Radek Krejcif16e2542021-02-17 15:39:23 +0100572 rc = lyd_parse_json(ctx, ext, parent, &first, in, parse_opts, val_opts, data_type, &parsed, &lydctx);
Michal Vaskoe0665742021-02-11 11:08:44 +0100573 break;
Radek Krejci7931b192020-06-25 17:05:03 +0200574 case LYD_LYB:
Radek Krejcif16e2542021-02-17 15:39:23 +0100575 rc = lyd_parse_lyb(ctx, ext, parent, &first, in, parse_opts, val_opts, data_type, &parsed, &lydctx);
Michal Vaskoe0665742021-02-11 11:08:44 +0100576 break;
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200577 case LYD_UNKNOWN:
Michal Vaskod74978f2021-02-12 11:59:36 +0100578 LOGARG(ctx, format);
579 rc = LY_EINVAL;
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200580 break;
Radek Krejci7931b192020-06-25 17:05:03 +0200581 }
Michal Vaskoe0665742021-02-11 11:08:44 +0100582 LY_CHECK_GOTO(rc, cleanup);
Radek Krejci7931b192020-06-25 17:05:03 +0200583
Michal Vaskoe0665742021-02-11 11:08:44 +0100584 /* set out params correctly */
585 if (tree) {
586 if (envp) {
587 /* special out param meaning */
588 *tree = envp;
589 } else {
590 *tree = parent ? NULL : first;
591 }
592 }
593 if (op) {
594 *op = lydctx->op_node;
595 }
596
597cleanup:
598 if (lydctx) {
599 lydctx->free(lydctx);
600 }
601 if (rc) {
602 if (parent) {
603 /* free all the parsed subtrees */
604 for (i = 0; i < parsed.count; ++i) {
605 lyd_free_tree(parsed.dnodes[i]);
606 }
607 } else {
608 /* free everything (cannot occur in the current code, a safety) */
609 lyd_free_all(first);
610 if (tree) {
611 *tree = NULL;
612 }
613 if (op) {
614 *op = NULL;
615 }
616 }
617 }
618 ly_set_erase(&parsed, NULL);
619 return rc;
Radek Krejcie7b95092019-05-15 11:03:07 +0200620}
Radek Krejci084289f2019-07-09 17:35:30 +0200621
Radek Krejcif16e2542021-02-17 15:39:23 +0100622API LY_ERR
623lyd_parse_op(const struct ly_ctx *ctx, struct lyd_node *parent, struct ly_in *in, LYD_FORMAT format,
624 enum lyd_type data_type, struct lyd_node **tree, struct lyd_node **op)
625{
626 LY_CHECK_ARG_RET(ctx, ctx || parent, in, data_type, parent || tree || op, LY_EINVAL);
627
628 return lyd_parse_op_(ctx, NULL, parent, in, format, data_type, tree, op);
629}
630
631API LY_ERR
632lyd_parse_ext_op(const struct lysc_ext_instance *ext, struct lyd_node *parent, struct ly_in *in, LYD_FORMAT format,
633 enum lyd_type data_type, struct lyd_node **tree, struct lyd_node **op)
634{
635 const struct ly_ctx *ctx = ext ? ext->module->ctx : NULL;
636
637 LY_CHECK_ARG_RET(ctx, ext, in, data_type, parent || tree || op, LY_EINVAL);
638
639 return lyd_parse_op_(ctx, ext, parent, in, format, data_type, tree, op);
640}
641
Michal Vasko90932a92020-02-12 14:33:03 +0100642LY_ERR
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200643lyd_create_term(const struct lysc_node *schema, const char *value, size_t value_len, ly_bool *dynamic,
Radek Krejci8df109d2021-04-23 12:19:08 +0200644 LY_VALUE_FORMAT format, void *prefix_data, uint32_t hints, ly_bool *incomplete, struct lyd_node **node)
Michal Vasko90932a92020-02-12 14:33:03 +0100645{
646 LY_ERR ret;
647 struct lyd_node_term *term;
648
Michal Vasko9b368d32020-02-14 13:53:31 +0100649 assert(schema->nodetype & LYD_NODE_TERM);
650
Michal Vasko90932a92020-02-12 14:33:03 +0100651 term = calloc(1, sizeof *term);
652 LY_CHECK_ERR_RET(!term, LOGMEM(schema->module->ctx), LY_EMEM);
653
654 term->schema = schema;
Michal Vasko9e685082021-01-29 14:49:09 +0100655 term->prev = &term->node;
Michal Vasko9b368d32020-02-14 13:53:31 +0100656 term->flags = LYD_NEW;
Michal Vasko90932a92020-02-12 14:33:03 +0100657
Radek Krejciddace2c2021-01-08 11:30:56 +0100658 LOG_LOCSET(schema, NULL, NULL, NULL);
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200659 ret = lyd_value_store(schema->module->ctx, &term->value, ((struct lysc_node_leaf *)term->schema)->type, value,
Radek Krejci2efc45b2020-12-22 16:25:44 +0100660 value_len, dynamic, format, prefix_data, hints, schema, incomplete);
Radek Krejciddace2c2021-01-08 11:30:56 +0100661 LOG_LOCBACK(1, 0, 0, 0);
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200662 LY_CHECK_ERR_RET(ret, free(term), ret);
Michal Vasko9e685082021-01-29 14:49:09 +0100663 lyd_hash(&term->node);
Michal Vasko9b368d32020-02-14 13:53:31 +0100664
Michal Vasko9e685082021-01-29 14:49:09 +0100665 *node = &term->node;
Michal Vasko9b368d32020-02-14 13:53:31 +0100666 return ret;
667}
668
669LY_ERR
670lyd_create_term2(const struct lysc_node *schema, const struct lyd_value *val, struct lyd_node **node)
671{
672 LY_ERR ret;
673 struct lyd_node_term *term;
674 struct lysc_type *type;
675
676 assert(schema->nodetype & LYD_NODE_TERM);
Radek Krejci224d4b42021-04-23 13:54:59 +0200677 assert(val && val->realtype);
Michal Vasko9b368d32020-02-14 13:53:31 +0100678
679 term = calloc(1, sizeof *term);
680 LY_CHECK_ERR_RET(!term, LOGMEM(schema->module->ctx), LY_EMEM);
681
682 term->schema = schema;
Michal Vasko9e685082021-01-29 14:49:09 +0100683 term->prev = &term->node;
Michal Vasko9b368d32020-02-14 13:53:31 +0100684 term->flags = LYD_NEW;
685
686 type = ((struct lysc_node_leaf *)schema)->type;
687 ret = type->plugin->duplicate(schema->module->ctx, val, &term->value);
688 if (ret) {
689 LOGERR(schema->module->ctx, ret, "Value duplication failed.");
690 free(term);
691 return ret;
692 }
Michal Vasko9e685082021-01-29 14:49:09 +0100693 lyd_hash(&term->node);
Michal Vasko90932a92020-02-12 14:33:03 +0100694
Michal Vasko9e685082021-01-29 14:49:09 +0100695 *node = &term->node;
Michal Vasko90932a92020-02-12 14:33:03 +0100696 return ret;
697}
698
699LY_ERR
700lyd_create_inner(const struct lysc_node *schema, struct lyd_node **node)
701{
702 struct lyd_node_inner *in;
703
Michal Vasko9b368d32020-02-14 13:53:31 +0100704 assert(schema->nodetype & LYD_NODE_INNER);
705
Michal Vasko90932a92020-02-12 14:33:03 +0100706 in = calloc(1, sizeof *in);
707 LY_CHECK_ERR_RET(!in, LOGMEM(schema->module->ctx), LY_EMEM);
708
709 in->schema = schema;
Michal Vasko9e685082021-01-29 14:49:09 +0100710 in->prev = &in->node;
Michal Vasko9b368d32020-02-14 13:53:31 +0100711 in->flags = LYD_NEW;
Michal Vasko3383be72020-11-03 17:15:31 +0100712 if ((schema->nodetype == LYS_CONTAINER) && !(schema->flags & LYS_PRESENCE)) {
713 in->flags |= LYD_DEFAULT;
714 }
Michal Vasko90932a92020-02-12 14:33:03 +0100715
Michal Vasko9b368d32020-02-14 13:53:31 +0100716 /* do not hash list with keys, we need them for the hash */
717 if ((schema->nodetype != LYS_LIST) || (schema->flags & LYS_KEYLESS)) {
Michal Vasko9e685082021-01-29 14:49:09 +0100718 lyd_hash(&in->node);
Michal Vasko9b368d32020-02-14 13:53:31 +0100719 }
Michal Vasko90932a92020-02-12 14:33:03 +0100720
Michal Vasko9e685082021-01-29 14:49:09 +0100721 *node = &in->node;
Michal Vasko90932a92020-02-12 14:33:03 +0100722 return LY_SUCCESS;
723}
724
Michal Vasko90932a92020-02-12 14:33:03 +0100725LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +0200726lyd_create_list(const struct lysc_node *schema, const struct ly_path_predicate *predicates, struct lyd_node **node)
Michal Vasko90932a92020-02-12 14:33:03 +0100727{
728 LY_ERR ret = LY_SUCCESS;
Michal Vasko90932a92020-02-12 14:33:03 +0100729 struct lyd_node *list = NULL, *key;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200730 LY_ARRAY_COUNT_TYPE u;
Michal Vasko90932a92020-02-12 14:33:03 +0100731
Michal Vasko004d3152020-06-11 19:59:22 +0200732 assert((schema->nodetype == LYS_LIST) && !(schema->flags & LYS_KEYLESS));
Michal Vasko90932a92020-02-12 14:33:03 +0100733
734 /* create list */
735 LY_CHECK_GOTO(ret = lyd_create_inner(schema, &list), cleanup);
736
Radek Krejciddace2c2021-01-08 11:30:56 +0100737 LOG_LOCSET(NULL, list, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100738
Michal Vasko90932a92020-02-12 14:33:03 +0100739 /* create and insert all the keys */
Michal Vasko004d3152020-06-11 19:59:22 +0200740 LY_ARRAY_FOR(predicates, u) {
741 LY_CHECK_GOTO(ret = lyd_create_term2(predicates[u].key, &predicates[u].value, &key), cleanup);
Michal Vasko6ee6f432021-07-16 09:49:14 +0200742 lyd_insert_node(list, NULL, key, 0);
Michal Vasko90932a92020-02-12 14:33:03 +0100743 }
744
Michal Vasko9b368d32020-02-14 13:53:31 +0100745 /* hash having all the keys */
746 lyd_hash(list);
747
Michal Vasko90932a92020-02-12 14:33:03 +0100748 /* success */
749 *node = list;
750 list = NULL;
751
752cleanup:
Radek Krejciddace2c2021-01-08 11:30:56 +0100753 LOG_LOCBACK(0, 1, 0, 0);
Michal Vasko90932a92020-02-12 14:33:03 +0100754 lyd_free_tree(list);
Michal Vasko004d3152020-06-11 19:59:22 +0200755 return ret;
756}
757
758static LY_ERR
759lyd_create_list2(const struct lysc_node *schema, const char *keys, size_t keys_len, struct lyd_node **node)
760{
761 LY_ERR ret = LY_SUCCESS;
762 struct lyxp_expr *expr = NULL;
763 uint16_t exp_idx = 0;
764 enum ly_path_pred_type pred_type = 0;
765 struct ly_path_predicate *predicates = NULL;
766
Radek Krejciddace2c2021-01-08 11:30:56 +0100767 LOG_LOCSET(schema, NULL, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100768
Michal Vasko004d3152020-06-11 19:59:22 +0200769 /* parse keys */
Michal Vasko6b26e742020-07-17 15:02:10 +0200770 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 +0200771 LY_PATH_PRED_KEYS, &expr), cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +0200772
773 /* compile them */
Radek Krejci8df109d2021-04-23 12:19:08 +0200774 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 +0200775 NULL, &predicates, &pred_type), cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +0200776
777 /* create the list node */
778 LY_CHECK_GOTO(ret = lyd_create_list(schema, predicates, node), cleanup);
779
780cleanup:
Radek Krejciddace2c2021-01-08 11:30:56 +0100781 LOG_LOCBACK(1, 0, 0, 0);
Michal Vasko004d3152020-06-11 19:59:22 +0200782 lyxp_expr_free(schema->module->ctx, expr);
Michal Vaskof7e16e22020-10-21 09:24:39 +0200783 ly_path_predicates_free(schema->module->ctx, pred_type, predicates);
Michal Vasko90932a92020-02-12 14:33:03 +0100784 return ret;
785}
786
787LY_ERR
Michal Vasko366a4a12020-12-04 16:23:57 +0100788lyd_create_any(const struct lysc_node *schema, const void *value, LYD_ANYDATA_VALUETYPE value_type, ly_bool use_value,
789 struct lyd_node **node)
Michal Vasko90932a92020-02-12 14:33:03 +0100790{
Michal Vasko366a4a12020-12-04 16:23:57 +0100791 LY_ERR ret;
Michal Vasko90932a92020-02-12 14:33:03 +0100792 struct lyd_node_any *any;
Michal Vasko366a4a12020-12-04 16:23:57 +0100793 union lyd_any_value any_val;
Michal Vasko90932a92020-02-12 14:33:03 +0100794
Michal Vasko9b368d32020-02-14 13:53:31 +0100795 assert(schema->nodetype & LYD_NODE_ANY);
796
Michal Vasko90932a92020-02-12 14:33:03 +0100797 any = calloc(1, sizeof *any);
798 LY_CHECK_ERR_RET(!any, LOGMEM(schema->module->ctx), LY_EMEM);
799
800 any->schema = schema;
Michal Vasko9e685082021-01-29 14:49:09 +0100801 any->prev = &any->node;
Michal Vasko9b368d32020-02-14 13:53:31 +0100802 any->flags = LYD_NEW;
Michal Vasko90932a92020-02-12 14:33:03 +0100803
Radek Krejci1798aae2020-07-14 13:26:06 +0200804 /* TODO: convert XML/JSON strings into a opaq data tree */
Michal Vasko366a4a12020-12-04 16:23:57 +0100805
806 if (use_value) {
807 any->value.str = value;
808 any->value_type = value_type;
809 } else {
810 any_val.str = value;
Michal Vasko9e685082021-01-29 14:49:09 +0100811 ret = lyd_any_copy_value(&any->node, &any_val, value_type);
Michal Vasko366a4a12020-12-04 16:23:57 +0100812 LY_CHECK_ERR_RET(ret, free(any), ret);
813 }
Michal Vasko9e685082021-01-29 14:49:09 +0100814 lyd_hash(&any->node);
Michal Vasko90932a92020-02-12 14:33:03 +0100815
Michal Vasko9e685082021-01-29 14:49:09 +0100816 *node = &any->node;
Michal Vasko90932a92020-02-12 14:33:03 +0100817 return LY_SUCCESS;
818}
819
Michal Vasko52927e22020-03-16 17:26:14 +0100820LY_ERR
Michal Vasko501af032020-11-11 20:27:44 +0100821lyd_create_opaq(const struct ly_ctx *ctx, const char *name, size_t name_len, const char *prefix, size_t pref_len,
822 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 +0200823 LY_VALUE_FORMAT format, void *val_prefix_data, uint32_t hints, struct lyd_node **node)
Michal Vasko52927e22020-03-16 17:26:14 +0100824{
Radek Krejci011e4aa2020-09-04 15:22:31 +0200825 LY_ERR ret = LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +0100826 struct lyd_node_opaq *opaq;
827
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200828 assert(ctx && name && name_len && format);
Michal Vasko52927e22020-03-16 17:26:14 +0100829
Michal Vasko4cd0a602021-05-31 11:11:58 +0200830 if (!value_len && (!dynamic || !*dynamic)) {
Michal Vasko52927e22020-03-16 17:26:14 +0100831 value = "";
832 }
833
834 opaq = calloc(1, sizeof *opaq);
Michal Vasko501af032020-11-11 20:27:44 +0100835 LY_CHECK_ERR_GOTO(!opaq, LOGMEM(ctx); ret = LY_EMEM, finish);
Michal Vasko52927e22020-03-16 17:26:14 +0100836
Michal Vasko9e685082021-01-29 14:49:09 +0100837 opaq->prev = &opaq->node;
Michal Vaskoad92b672020-11-12 13:11:31 +0100838 LY_CHECK_GOTO(ret = lydict_insert(ctx, name, name_len, &opaq->name.name), finish);
Radek Krejci011e4aa2020-09-04 15:22:31 +0200839
Michal Vasko52927e22020-03-16 17:26:14 +0100840 if (pref_len) {
Michal Vaskoad92b672020-11-12 13:11:31 +0100841 LY_CHECK_GOTO(ret = lydict_insert(ctx, prefix, pref_len, &opaq->name.prefix), finish);
Michal Vasko52927e22020-03-16 17:26:14 +0100842 }
Radek Krejci1798aae2020-07-14 13:26:06 +0200843 if (module_key_len) {
Michal Vaskoad92b672020-11-12 13:11:31 +0100844 LY_CHECK_GOTO(ret = lydict_insert(ctx, module_key, module_key_len, &opaq->name.module_ns), finish);
Radek Krejci1798aae2020-07-14 13:26:06 +0200845 }
Michal Vasko52927e22020-03-16 17:26:14 +0100846 if (dynamic && *dynamic) {
Radek Krejci011e4aa2020-09-04 15:22:31 +0200847 LY_CHECK_GOTO(ret = lydict_insert_zc(ctx, (char *)value, &opaq->value), finish);
Michal Vasko52927e22020-03-16 17:26:14 +0100848 *dynamic = 0;
849 } else {
Radek Krejci011e4aa2020-09-04 15:22:31 +0200850 LY_CHECK_GOTO(ret = lydict_insert(ctx, value, value_len, &opaq->value), finish);
Michal Vasko52927e22020-03-16 17:26:14 +0100851 }
Michal Vasko501af032020-11-11 20:27:44 +0100852
853 opaq->format = format;
854 opaq->val_prefix_data = val_prefix_data;
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200855 opaq->hints = hints;
Michal Vasko52927e22020-03-16 17:26:14 +0100856 opaq->ctx = ctx;
857
Radek Krejci011e4aa2020-09-04 15:22:31 +0200858finish:
859 if (ret) {
Michal Vasko9e685082021-01-29 14:49:09 +0100860 lyd_free_tree(&opaq->node);
Michal Vasko501af032020-11-11 20:27:44 +0100861 ly_free_prefix_data(format, val_prefix_data);
Radek Krejci011e4aa2020-09-04 15:22:31 +0200862 } else {
Michal Vasko9e685082021-01-29 14:49:09 +0100863 *node = &opaq->node;
Radek Krejci011e4aa2020-09-04 15:22:31 +0200864 }
865 return ret;
Michal Vasko52927e22020-03-16 17:26:14 +0100866}
867
Michal Vasko3a41dff2020-07-15 14:30:28 +0200868API LY_ERR
Michal Vasko65243892020-12-04 16:26:21 +0100869lyd_new_inner(struct lyd_node *parent, const struct lys_module *module, const char *name, ly_bool output,
870 struct lyd_node **node)
Michal Vasko013a8182020-03-03 10:46:53 +0100871{
872 struct lyd_node *ret = NULL;
873 const struct lysc_node *schema;
Michal Vasko892f5bf2021-11-24 10:41:05 +0100874 const struct ly_ctx *ctx = parent ? LYD_CTX(parent) : (module ? module->ctx : NULL);
Michal Vasko013a8182020-03-03 10:46:53 +0100875
Michal Vasko6027eb92020-07-15 16:37:30 +0200876 LY_CHECK_ARG_RET(ctx, parent || module, parent || node, name, LY_EINVAL);
Michal Vasko892f5bf2021-11-24 10:41:05 +0100877 LY_CHECK_CTX_EQUAL_RET(parent ? LYD_CTX(parent) : NULL, module ? module->ctx : NULL, LY_EINVAL);
Michal Vasko013a8182020-03-03 10:46:53 +0100878
Michal Vaskof03ed032020-03-04 13:31:44 +0100879 if (!module) {
880 module = parent->schema->module;
881 }
882
Michal Vasko3a41dff2020-07-15 14:30:28 +0200883 schema = lys_find_child(parent ? parent->schema : NULL, module, name, 0,
Radek Krejci41ac9942020-11-02 14:47:56 +0100884 LYS_CONTAINER | LYS_NOTIF | LYS_RPC | LYS_ACTION, output ? LYS_GETNEXT_OUTPUT : 0);
Radek Krejcidd2a7662021-03-12 11:26:34 +0100885 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 +0100886
Michal Vasko3a41dff2020-07-15 14:30:28 +0200887 LY_CHECK_RET(lyd_create_inner(schema, &ret));
888 if (parent) {
Michal Vasko6ee6f432021-07-16 09:49:14 +0200889 lyd_insert_node(parent, NULL, ret, 0);
Michal Vasko013a8182020-03-03 10:46:53 +0100890 }
Michal Vasko3a41dff2020-07-15 14:30:28 +0200891
892 if (node) {
893 *node = ret;
894 }
895 return LY_SUCCESS;
Michal Vasko013a8182020-03-03 10:46:53 +0100896}
897
Michal Vasko3a41dff2020-07-15 14:30:28 +0200898API LY_ERR
Radek Krejcidd2a7662021-03-12 11:26:34 +0100899lyd_new_ext_inner(const struct lysc_ext_instance *ext, const char *name, struct lyd_node **node)
900{
901 struct lyd_node *ret = NULL;
902 const struct lysc_node *schema;
903 struct ly_ctx *ctx = ext ? ext->module->ctx : NULL;
904
905 LY_CHECK_ARG_RET(ctx, ext, node, name, LY_EINVAL);
906
907 schema = lysc_ext_find_node(ext, NULL, name, 0, LYS_CONTAINER | LYS_NOTIF | LYS_RPC | LYS_ACTION, 0);
908 if (!schema) {
909 if (ext->argument) {
910 LOGERR(ctx, LY_EINVAL, "Inner node (not a list) \"%s\" not found in instance \"%s\" of extension %s.",
911 name, ext->argument, ext->def->name);
912 } else {
913 LOGERR(ctx, LY_EINVAL, "Inner node (not a list) \"%s\" not found in instance of extension %s.",
914 name, ext->def->name);
915 }
916 return LY_ENOTFOUND;
917 }
918 LY_CHECK_RET(lyd_create_inner(schema, &ret));
919
920 *node = ret;
921
922 return LY_SUCCESS;
923}
924
Michal Vasko208e6d62021-06-28 11:23:50 +0200925/**
926 * @brief Create a new list node in the data tree.
927 *
928 * @param[in] parent Parent node for the node being created. NULL in case of creating a top level element.
929 * @param[in] module Module of the node being created. If NULL, @p parent module will be used.
930 * @param[in] name Schema node name of the new data node. The node must be #LYS_LIST.
931 * @param[in] format Format of key values.
932 * @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
933 * taken into consideration. Otherwise, the output's data node is going to be created.
934 * @param[out] node Optional created node.
935 * @param[in] ap Ordered key values of the new list instance, all must be set. For ::LY_VALUE_LYB, every value must
936 * be followed by the value length.
937 * @return LY_ERR value.
938 */
939static LY_ERR
940_lyd_new_list(struct lyd_node *parent, const struct lys_module *module, const char *name, LY_VALUE_FORMAT format,
941 ly_bool output, struct lyd_node **node, va_list ap)
Michal Vasko013a8182020-03-03 10:46:53 +0100942{
943 struct lyd_node *ret = NULL, *key;
944 const struct lysc_node *schema, *key_s;
Michal Vasko892f5bf2021-11-24 10:41:05 +0100945 const struct ly_ctx *ctx = parent ? LYD_CTX(parent) : (module ? module->ctx : NULL);
Michal Vasko208e6d62021-06-28 11:23:50 +0200946 const void *key_val;
947 uint32_t key_len;
Michal Vasko013a8182020-03-03 10:46:53 +0100948 LY_ERR rc = LY_SUCCESS;
949
Michal Vasko6027eb92020-07-15 16:37:30 +0200950 LY_CHECK_ARG_RET(ctx, parent || module, parent || node, name, LY_EINVAL);
Michal Vasko892f5bf2021-11-24 10:41:05 +0100951 LY_CHECK_CTX_EQUAL_RET(parent ? LYD_CTX(parent) : NULL, module ? module->ctx : NULL, LY_EINVAL);
Michal Vasko013a8182020-03-03 10:46:53 +0100952
Michal Vaskof03ed032020-03-04 13:31:44 +0100953 if (!module) {
954 module = parent->schema->module;
955 }
956
Radek Krejci41ac9942020-11-02 14:47:56 +0100957 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 +0200958 LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "List node \"%s\" not found.", name), LY_ENOTFOUND);
Michal Vasko013a8182020-03-03 10:46:53 +0100959
960 /* create list inner node */
Michal Vasko3a41dff2020-07-15 14:30:28 +0200961 LY_CHECK_RET(lyd_create_inner(schema, &ret));
Michal Vasko013a8182020-03-03 10:46:53 +0100962
Michal Vasko013a8182020-03-03 10:46:53 +0100963 /* create and insert all the keys */
Michal Vasko544e58a2021-01-28 14:33:41 +0100964 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 +0200965 if (format == LY_VALUE_LYB) {
966 key_val = va_arg(ap, const void *);
967 key_len = va_arg(ap, uint32_t);
968 } else {
969 key_val = va_arg(ap, const char *);
970 key_len = key_val ? strlen((char *)key_val) : 0;
971 }
Michal Vasko013a8182020-03-03 10:46:53 +0100972
Michal Vasko208e6d62021-06-28 11:23:50 +0200973 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 +0200974 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko6ee6f432021-07-16 09:49:14 +0200975 lyd_insert_node(ret, NULL, key, 1);
Michal Vasko013a8182020-03-03 10:46:53 +0100976 }
977
Michal Vasko013a8182020-03-03 10:46:53 +0100978 if (parent) {
Michal Vasko6ee6f432021-07-16 09:49:14 +0200979 lyd_insert_node(parent, NULL, ret, 0);
Michal Vasko013a8182020-03-03 10:46:53 +0100980 }
981
982cleanup:
983 if (rc) {
984 lyd_free_tree(ret);
985 ret = NULL;
Michal Vasko3a41dff2020-07-15 14:30:28 +0200986 } else if (node) {
987 *node = ret;
Michal Vasko013a8182020-03-03 10:46:53 +0100988 }
Michal Vasko3a41dff2020-07-15 14:30:28 +0200989 return rc;
Michal Vasko013a8182020-03-03 10:46:53 +0100990}
991
Michal Vasko3a41dff2020-07-15 14:30:28 +0200992API LY_ERR
Michal Vasko208e6d62021-06-28 11:23:50 +0200993lyd_new_list(struct lyd_node *parent, const struct lys_module *module, const char *name, ly_bool output,
994 struct lyd_node **node, ...)
995{
996 LY_ERR rc;
997 va_list ap;
998
999 va_start(ap, node);
1000
1001 rc = _lyd_new_list(parent, module, name, LY_VALUE_JSON, output, node, ap);
1002
1003 va_end(ap);
1004 return rc;
1005}
1006
1007API LY_ERR
1008lyd_new_list_bin(struct lyd_node *parent, const struct lys_module *module, const char *name, ly_bool output,
1009 struct lyd_node **node, ...)
1010{
1011 LY_ERR rc;
1012 va_list ap;
1013
1014 va_start(ap, node);
1015
1016 rc = _lyd_new_list(parent, module, name, LY_VALUE_LYB, output, node, ap);
1017
1018 va_end(ap);
1019 return rc;
1020}
1021
1022API LY_ERR
1023lyd_new_list_canon(struct lyd_node *parent, const struct lys_module *module, const char *name, ly_bool output,
1024 struct lyd_node **node, ...)
1025{
1026 LY_ERR rc;
1027 va_list ap;
1028
1029 va_start(ap, node);
1030
1031 rc = _lyd_new_list(parent, module, name, LY_VALUE_CANON, output, node, ap);
1032
1033 va_end(ap);
1034 return rc;
1035}
1036
1037API LY_ERR
Radek Krejci8247bae2021-03-12 11:47:02 +01001038lyd_new_ext_list(const struct lysc_ext_instance *ext, const char *name, struct lyd_node **node, ...)
1039{
1040 struct lyd_node *ret = NULL, *key;
1041 const struct lysc_node *schema, *key_s;
1042 struct ly_ctx *ctx = ext ? ext->module->ctx : NULL;
1043 va_list ap;
1044 const char *key_val;
1045 LY_ERR rc = LY_SUCCESS;
1046
1047 LY_CHECK_ARG_RET(ctx, ext, node, name, LY_EINVAL);
1048
1049 schema = lysc_ext_find_node(ext, NULL, name, 0, LYS_LIST, 0);
1050 if (!schema) {
1051 if (ext->argument) {
1052 LOGERR(ctx, LY_EINVAL, "List node \"%s\" not found in instance \"%s\" of extension %s.",
1053 name, ext->argument, ext->def->name);
1054 } else {
1055 LOGERR(ctx, LY_EINVAL, "List node \"%s\" not found in instance of extension %s.", name, ext->def->name);
1056 }
1057 return LY_ENOTFOUND;
1058 }
1059 /* create list inner node */
1060 LY_CHECK_RET(lyd_create_inner(schema, &ret));
1061
1062 va_start(ap, node);
1063
1064 /* create and insert all the keys */
1065 for (key_s = lysc_node_child(schema); key_s && (key_s->flags & LYS_KEY); key_s = key_s->next) {
1066 key_val = va_arg(ap, const char *);
1067
Radek Krejci8df109d2021-04-23 12:19:08 +02001068 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 +01001069 NULL, &key);
1070 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko6ee6f432021-07-16 09:49:14 +02001071 lyd_insert_node(ret, NULL, key, 1);
Radek Krejci8247bae2021-03-12 11:47:02 +01001072 }
1073
1074cleanup:
1075 va_end(ap);
1076 if (rc) {
1077 lyd_free_tree(ret);
1078 ret = NULL;
Radek Krejci8247bae2021-03-12 11:47:02 +01001079 }
Radek Krejci8919fbd2021-03-15 09:35:25 +01001080 *node = ret;
Radek Krejci8247bae2021-03-12 11:47:02 +01001081 return rc;
1082}
1083
1084API LY_ERR
Michal Vasko3a41dff2020-07-15 14:30:28 +02001085lyd_new_list2(struct lyd_node *parent, const struct lys_module *module, const char *name, const char *keys,
Radek Krejci41ac9942020-11-02 14:47:56 +01001086 ly_bool output, struct lyd_node **node)
Michal Vasko013a8182020-03-03 10:46:53 +01001087{
1088 struct lyd_node *ret = NULL;
1089 const struct lysc_node *schema;
Michal Vasko892f5bf2021-11-24 10:41:05 +01001090 const struct ly_ctx *ctx = parent ? LYD_CTX(parent) : (module ? module->ctx : NULL);
Michal Vasko013a8182020-03-03 10:46:53 +01001091
Michal Vasko6027eb92020-07-15 16:37:30 +02001092 LY_CHECK_ARG_RET(ctx, parent || module, parent || node, name, LY_EINVAL);
Michal Vasko892f5bf2021-11-24 10:41:05 +01001093 LY_CHECK_CTX_EQUAL_RET(parent ? LYD_CTX(parent) : NULL, module ? module->ctx : NULL, LY_EINVAL);
Michal Vasko013a8182020-03-03 10:46:53 +01001094
Michal Vaskof03ed032020-03-04 13:31:44 +01001095 if (!module) {
1096 module = parent->schema->module;
1097 }
Michal Vasko004d3152020-06-11 19:59:22 +02001098 if (!keys) {
1099 keys = "";
1100 }
Michal Vaskof03ed032020-03-04 13:31:44 +01001101
Michal Vasko004d3152020-06-11 19:59:22 +02001102 /* find schema node */
Radek Krejci41ac9942020-11-02 14:47:56 +01001103 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 +02001104 LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "List node \"%s\" not found.", name), LY_ENOTFOUND);
Michal Vasko013a8182020-03-03 10:46:53 +01001105
Michal Vasko004d3152020-06-11 19:59:22 +02001106 if ((schema->flags & LYS_KEYLESS) && !keys[0]) {
1107 /* key-less list */
Michal Vasko3a41dff2020-07-15 14:30:28 +02001108 LY_CHECK_RET(lyd_create_inner(schema, &ret));
Michal Vasko004d3152020-06-11 19:59:22 +02001109 } else {
1110 /* create the list node */
Michal Vasko3a41dff2020-07-15 14:30:28 +02001111 LY_CHECK_RET(lyd_create_list2(schema, keys, strlen(keys), &ret));
Michal Vasko004d3152020-06-11 19:59:22 +02001112 }
Michal Vasko004d3152020-06-11 19:59:22 +02001113 if (parent) {
Michal Vasko6ee6f432021-07-16 09:49:14 +02001114 lyd_insert_node(parent, NULL, ret, 0);
Michal Vasko013a8182020-03-03 10:46:53 +01001115 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02001116
1117 if (node) {
1118 *node = ret;
1119 }
1120 return LY_SUCCESS;
Michal Vasko013a8182020-03-03 10:46:53 +01001121}
1122
Radek Krejci09c77442021-04-26 11:10:34 +02001123/**
1124 * @brief Create a new term node in the data tree.
1125 *
1126 * @param[in] parent Parent node for the node being created. NULL in case of creating a top level element.
1127 * @param[in] module Module of the node being created. If NULL, @p parent module will be used.
1128 * @param[in] name Schema node name of the new data node. The node can be ::LYS_LEAF or ::LYS_LEAFLIST.
1129 * @param[in] value Value of the node being created.
1130 * @param[in] value_len Length of @p value.
1131 * @param[in] format Format of @p value.
1132 * @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
1133 * taken into consideration. Otherwise, the output's data node is going to be created.
1134 * @param[out] node Optional created node.
1135 * @return LY_ERR value.
1136 */
1137static LY_ERR
1138_lyd_new_term(struct lyd_node *parent, const struct lys_module *module, const char *name, const void *value,
1139 size_t value_len, LY_VALUE_FORMAT format, ly_bool output, struct lyd_node **node)
Michal Vasko013a8182020-03-03 10:46:53 +01001140{
Michal Vaskocbff3e92020-05-27 12:56:41 +02001141 LY_ERR rc;
Michal Vasko013a8182020-03-03 10:46:53 +01001142 struct lyd_node *ret = NULL;
1143 const struct lysc_node *schema;
Radek Krejci09c77442021-04-26 11:10:34 +02001144 const struct ly_ctx *ctx = parent ? LYD_CTX(parent) : (module ? module->ctx : NULL);
Michal Vasko013a8182020-03-03 10:46:53 +01001145
Michal Vasko6027eb92020-07-15 16:37:30 +02001146 LY_CHECK_ARG_RET(ctx, parent || module, parent || node, name, LY_EINVAL);
Michal Vasko892f5bf2021-11-24 10:41:05 +01001147 LY_CHECK_CTX_EQUAL_RET(parent ? LYD_CTX(parent) : NULL, module ? module->ctx : NULL, LY_EINVAL);
Michal Vasko013a8182020-03-03 10:46:53 +01001148
Michal Vaskof03ed032020-03-04 13:31:44 +01001149 if (!module) {
1150 module = parent->schema->module;
1151 }
1152
Radek Krejci41ac9942020-11-02 14:47:56 +01001153 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 +02001154 LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "Term node \"%s\" not found.", name), LY_ENOTFOUND);
Michal Vasko013a8182020-03-03 10:46:53 +01001155
Radek Krejci09c77442021-04-26 11:10:34 +02001156 rc = lyd_create_term(schema, value, value_len, NULL, format, NULL, LYD_HINT_DATA, NULL, &ret);
Michal Vaskofeca4fb2020-10-05 08:58:40 +02001157 LY_CHECK_RET(rc);
Michal Vaskocbff3e92020-05-27 12:56:41 +02001158 if (parent) {
Michal Vasko6ee6f432021-07-16 09:49:14 +02001159 lyd_insert_node(parent, NULL, ret, 0);
Michal Vasko013a8182020-03-03 10:46:53 +01001160 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02001161
1162 if (node) {
1163 *node = ret;
1164 }
1165 return LY_SUCCESS;
Michal Vasko013a8182020-03-03 10:46:53 +01001166}
1167
Michal Vasko3a41dff2020-07-15 14:30:28 +02001168API LY_ERR
Radek Krejci09c77442021-04-26 11:10:34 +02001169lyd_new_term(struct lyd_node *parent, const struct lys_module *module, const char *name, const char *val_str,
1170 ly_bool output, struct lyd_node **node)
1171{
1172 return _lyd_new_term(parent, module, name, val_str, val_str ? strlen(val_str) : 0, LY_VALUE_JSON, output, node);
1173}
1174
1175API LY_ERR
1176lyd_new_term_bin(struct lyd_node *parent, const struct lys_module *module, const char *name, const void *value,
1177 size_t value_len, ly_bool output, struct lyd_node **node)
1178{
1179 return _lyd_new_term(parent, module, name, value, value_len, LY_VALUE_LYB, output, node);
1180}
1181
1182API LY_ERR
1183lyd_new_term_canon(struct lyd_node *parent, const struct lys_module *module, const char *name, const char *val_str,
1184 ly_bool output, struct lyd_node **node)
1185{
1186 return _lyd_new_term(parent, module, name, val_str, val_str ? strlen(val_str) : 0, LY_VALUE_CANON, output, node);
1187}
1188
1189API LY_ERR
Radek Krejci8a5afc22021-03-12 11:10:47 +01001190lyd_new_ext_term(const struct lysc_ext_instance *ext, const char *name, const char *val_str, struct lyd_node **node)
1191{
1192 LY_ERR rc;
1193 struct lyd_node *ret = NULL;
1194 const struct lysc_node *schema;
1195 struct ly_ctx *ctx = ext ? ext->module->ctx : NULL;
1196
1197 LY_CHECK_ARG_RET(ctx, ext, node, name, LY_EINVAL);
1198
1199 schema = lysc_ext_find_node(ext, NULL, name, 0, LYD_NODE_TERM, 0);
1200 if (!schema) {
1201 if (ext->argument) {
1202 LOGERR(ctx, LY_EINVAL, "Term node \"%s\" not found in instance \"%s\" of extension %s.",
1203 name, ext->argument, ext->def->name);
1204 } else {
1205 LOGERR(ctx, LY_EINVAL, "Term node \"%s\" not found in instance of extension %s.", name, ext->def->name);
1206 }
1207 return LY_ENOTFOUND;
1208 }
Radek Krejci8df109d2021-04-23 12:19:08 +02001209 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 +01001210 LY_CHECK_RET(rc);
1211
1212 *node = ret;
1213
1214 return LY_SUCCESS;
1215}
1216
1217API LY_ERR
Michal Vasko2a4ab2b2021-03-04 15:52:40 +01001218lyd_new_any(struct lyd_node *parent, const struct lys_module *module, const char *name, const void *value,
1219 ly_bool use_value, LYD_ANYDATA_VALUETYPE value_type, ly_bool output, struct lyd_node **node)
Michal Vasko013a8182020-03-03 10:46:53 +01001220{
1221 struct lyd_node *ret = NULL;
1222 const struct lysc_node *schema;
Michal Vasko892f5bf2021-11-24 10:41:05 +01001223 const struct ly_ctx *ctx = parent ? LYD_CTX(parent) : (module ? module->ctx : NULL);
Michal Vasko013a8182020-03-03 10:46:53 +01001224
Michal Vasko6027eb92020-07-15 16:37:30 +02001225 LY_CHECK_ARG_RET(ctx, parent || module, parent || node, name, LY_EINVAL);
Michal Vasko892f5bf2021-11-24 10:41:05 +01001226 LY_CHECK_CTX_EQUAL_RET(parent ? LYD_CTX(parent) : NULL, module ? module->ctx : NULL, LY_EINVAL);
Michal Vasko013a8182020-03-03 10:46:53 +01001227
Michal Vaskof03ed032020-03-04 13:31:44 +01001228 if (!module) {
1229 module = parent->schema->module;
1230 }
1231
Radek Krejci41ac9942020-11-02 14:47:56 +01001232 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 +02001233 LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "Any node \"%s\" not found.", name), LY_ENOTFOUND);
Michal Vasko013a8182020-03-03 10:46:53 +01001234
Michal Vasko2a4ab2b2021-03-04 15:52:40 +01001235 LY_CHECK_RET(lyd_create_any(schema, value, value_type, use_value, &ret));
Michal Vasko3a41dff2020-07-15 14:30:28 +02001236 if (parent) {
Michal Vasko6ee6f432021-07-16 09:49:14 +02001237 lyd_insert_node(parent, NULL, ret, 0);
Michal Vasko013a8182020-03-03 10:46:53 +01001238 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02001239
1240 if (node) {
1241 *node = ret;
1242 }
1243 return LY_SUCCESS;
Michal Vasko013a8182020-03-03 10:46:53 +01001244}
1245
Radek Krejci0b963da2021-03-12 13:23:44 +01001246API LY_ERR
1247lyd_new_ext_any(const struct lysc_ext_instance *ext, const char *name, const void *value, ly_bool use_value,
1248 LYD_ANYDATA_VALUETYPE value_type, struct lyd_node **node)
1249{
1250 struct lyd_node *ret = NULL;
1251 const struct lysc_node *schema;
1252 struct ly_ctx *ctx = ext ? ext->module->ctx : NULL;
1253
1254 LY_CHECK_ARG_RET(ctx, ext, node, name, LY_EINVAL);
1255
1256 schema = lysc_ext_find_node(ext, NULL, name, 0, LYD_NODE_ANY, 0);
1257 if (!schema) {
1258 if (ext->argument) {
1259 LOGERR(ctx, LY_EINVAL, "Any node \"%s\" not found in instance \"%s\" of extension %s.",
1260 name, ext->argument, ext->def->name);
1261 } else {
1262 LOGERR(ctx, LY_EINVAL, "Any node \"%s\" not found in instance of extension %s.", name, ext->def->name);
1263 }
1264 return LY_ENOTFOUND;
1265 }
1266 LY_CHECK_RET(lyd_create_any(schema, value, value_type, use_value, &ret));
1267
1268 *node = ret;
1269
1270 return LY_SUCCESS;
1271}
1272
Michal Vasko3a41dff2020-07-15 14:30:28 +02001273API LY_ERR
Michal Vasko871a0252020-11-11 18:35:24 +01001274lyd_new_meta(const struct ly_ctx *ctx, struct lyd_node *parent, const struct lys_module *module, const char *name,
1275 const char *val_str, ly_bool clear_dflt, struct lyd_meta **meta)
Michal Vaskod86997b2020-05-26 15:19:54 +02001276{
Michal Vaskod86997b2020-05-26 15:19:54 +02001277 const char *prefix, *tmp;
Michal Vaskod86997b2020-05-26 15:19:54 +02001278 size_t pref_len, name_len;
1279
Michal Vasko2b5344c2021-02-26 10:12:05 +01001280 LY_CHECK_ARG_RET(ctx, ctx || parent, name, module || strchr(name, ':'), parent || meta, LY_EINVAL);
Michal Vasko892f5bf2021-11-24 10:41:05 +01001281 LY_CHECK_CTX_EQUAL_RET(ctx, parent ? LYD_CTX(parent) : NULL, module ? module->ctx : NULL, LY_EINVAL);
Michal Vasko2b5344c2021-02-26 10:12:05 +01001282 if (!ctx) {
1283 ctx = LYD_CTX(parent);
1284 }
Michal Vasko00cbf532020-06-15 13:58:47 +02001285
Michal Vasko871a0252020-11-11 18:35:24 +01001286 if (parent && !parent->schema) {
Michal Vasko33b4fab2021-03-04 15:56:12 +01001287 LOGERR(ctx, LY_EINVAL, "Cannot add metadata \"%s\" to an opaque node \"%s\".", name, LYD_NAME(parent));
Michal Vasko871a0252020-11-11 18:35:24 +01001288 return LY_EINVAL;
1289 }
Michal Vasko610553d2020-11-18 18:15:05 +01001290 if (meta) {
1291 *meta = NULL;
1292 }
Michal Vaskod86997b2020-05-26 15:19:54 +02001293
1294 /* parse the name */
1295 tmp = name;
1296 if (ly_parse_nodeid(&tmp, &prefix, &pref_len, &name, &name_len) || tmp[0]) {
1297 LOGERR(ctx, LY_EINVAL, "Metadata name \"%s\" is not valid.", name);
Michal Vasko871a0252020-11-11 18:35:24 +01001298 return LY_EINVAL;
Michal Vaskod86997b2020-05-26 15:19:54 +02001299 }
1300
1301 /* find the module */
1302 if (prefix) {
Radek Krejci0ad51f12020-07-16 12:08:12 +02001303 module = ly_ctx_get_module_implemented2(ctx, prefix, pref_len);
Radek Krejci422afb12021-03-04 16:38:16 +01001304 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 +02001305 }
1306
1307 /* set value if none */
1308 if (!val_str) {
1309 val_str = "";
1310 }
1311
Radek Krejci8df109d2021-04-23 12:19:08 +02001312 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 +01001313 NULL, LYD_HINT_DATA, clear_dflt, NULL);
1314}
Michal Vasko3a41dff2020-07-15 14:30:28 +02001315
Michal Vaskoba696702020-11-11 19:12:45 +01001316API LY_ERR
1317lyd_new_meta2(const struct ly_ctx *ctx, struct lyd_node *parent, ly_bool clear_dflt, const struct lyd_attr *attr,
1318 struct lyd_meta **meta)
1319{
1320 const struct lys_module *mod;
1321
1322 LY_CHECK_ARG_RET(NULL, ctx, attr, parent || meta, LY_EINVAL);
Michal Vasko892f5bf2021-11-24 10:41:05 +01001323 LY_CHECK_CTX_EQUAL_RET(ctx, parent ? LYD_CTX(parent) : NULL, LY_EINVAL);
Michal Vaskoba696702020-11-11 19:12:45 +01001324
1325 if (parent && !parent->schema) {
1326 LOGERR(ctx, LY_EINVAL, "Cannot add metadata to an opaque node \"%s\".", ((struct lyd_node_opaq *)parent)->name);
1327 return LY_EINVAL;
Michal Vasko3a41dff2020-07-15 14:30:28 +02001328 }
Michal Vasko610553d2020-11-18 18:15:05 +01001329 if (meta) {
1330 *meta = NULL;
1331 }
Michal Vaskoba696702020-11-11 19:12:45 +01001332
1333 switch (attr->format) {
Radek Krejci8df109d2021-04-23 12:19:08 +02001334 case LY_VALUE_XML:
Michal Vaskoad92b672020-11-12 13:11:31 +01001335 mod = ly_ctx_get_module_implemented_ns(ctx, attr->name.module_ns);
Michal Vaskoba696702020-11-11 19:12:45 +01001336 if (!mod) {
Michal Vaskoad92b672020-11-12 13:11:31 +01001337 LOGERR(ctx, LY_EINVAL, "Module with namespace \"%s\" not found.", attr->name.module_ns);
Michal Vaskoba696702020-11-11 19:12:45 +01001338 return LY_ENOTFOUND;
1339 }
1340 break;
Radek Krejci8df109d2021-04-23 12:19:08 +02001341 case LY_VALUE_JSON:
Michal Vaskoad92b672020-11-12 13:11:31 +01001342 mod = ly_ctx_get_module_implemented(ctx, attr->name.module_name);
Michal Vaskoba696702020-11-11 19:12:45 +01001343 if (!mod) {
Michal Vaskoad92b672020-11-12 13:11:31 +01001344 LOGERR(ctx, LY_EINVAL, "Module \"%s\" not found.", attr->name.module_name);
Michal Vaskoba696702020-11-11 19:12:45 +01001345 return LY_ENOTFOUND;
1346 }
1347 break;
1348 default:
1349 LOGINT_RET(ctx);
1350 }
1351
Michal Vaskoad92b672020-11-12 13:11:31 +01001352 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 +01001353 NULL, attr->format, attr->val_prefix_data, attr->hints, clear_dflt, NULL);
Michal Vaskod86997b2020-05-26 15:19:54 +02001354}
1355
Michal Vasko3a41dff2020-07-15 14:30:28 +02001356API LY_ERR
Michal Vasko00cbf532020-06-15 13:58:47 +02001357lyd_new_opaq(struct lyd_node *parent, const struct ly_ctx *ctx, const char *name, const char *value,
Michal Vasko0ab974d2021-02-24 13:18:26 +01001358 const char *prefix, const char *module_name, struct lyd_node **node)
Michal Vasko00cbf532020-06-15 13:58:47 +02001359{
1360 struct lyd_node *ret = NULL;
Radek Krejci07a55962021-03-02 20:16:43 +01001361
Michal Vasko0ab974d2021-02-24 13:18:26 +01001362 LY_CHECK_ARG_RET(ctx, parent || ctx, parent || node, name, module_name, !prefix || !strcmp(prefix, module_name), LY_EINVAL);
Michal Vasko892f5bf2021-11-24 10:41:05 +01001363 LY_CHECK_CTX_EQUAL_RET(ctx, parent ? LYD_CTX(parent) : NULL, LY_EINVAL);
Michal Vasko00cbf532020-06-15 13:58:47 +02001364
1365 if (!ctx) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02001366 ctx = LYD_CTX(parent);
Michal Vasko00cbf532020-06-15 13:58:47 +02001367 }
1368 if (!value) {
1369 value = "";
1370 }
1371
Michal Vasko0ab974d2021-02-24 13:18:26 +01001372 LY_CHECK_RET(lyd_create_opaq(ctx, name, strlen(name), prefix, prefix ? strlen(prefix) : 0, module_name,
Radek Krejci8df109d2021-04-23 12:19:08 +02001373 strlen(module_name), value, strlen(value), NULL, LY_VALUE_JSON, NULL, 0, &ret));
Michal Vasko3a41dff2020-07-15 14:30:28 +02001374 if (parent) {
Michal Vasko6ee6f432021-07-16 09:49:14 +02001375 lyd_insert_node(parent, NULL, ret, 1);
Michal Vasko00cbf532020-06-15 13:58:47 +02001376 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02001377
1378 if (node) {
1379 *node = ret;
1380 }
1381 return LY_SUCCESS;
Michal Vasko00cbf532020-06-15 13:58:47 +02001382}
1383
Michal Vasko3a41dff2020-07-15 14:30:28 +02001384API LY_ERR
Michal Vasko8d65f852021-02-17 11:28:13 +01001385lyd_new_opaq2(struct lyd_node *parent, const struct ly_ctx *ctx, const char *name, const char *value,
Michal Vasko0ab974d2021-02-24 13:18:26 +01001386 const char *prefix, const char *module_ns, struct lyd_node **node)
Michal Vasko8d65f852021-02-17 11:28:13 +01001387{
1388 struct lyd_node *ret = NULL;
1389
1390 LY_CHECK_ARG_RET(ctx, parent || ctx, parent || node, name, module_ns, LY_EINVAL);
Michal Vasko892f5bf2021-11-24 10:41:05 +01001391 LY_CHECK_CTX_EQUAL_RET(ctx, parent ? LYD_CTX(parent) : NULL, LY_EINVAL);
Michal Vasko8d65f852021-02-17 11:28:13 +01001392
1393 if (!ctx) {
1394 ctx = LYD_CTX(parent);
1395 }
1396 if (!value) {
1397 value = "";
1398 }
1399
Michal Vasko0ab974d2021-02-24 13:18:26 +01001400 LY_CHECK_RET(lyd_create_opaq(ctx, name, strlen(name), prefix, prefix ? strlen(prefix) : 0, module_ns,
Radek Krejci8df109d2021-04-23 12:19:08 +02001401 strlen(module_ns), value, strlen(value), NULL, LY_VALUE_XML, NULL, 0, &ret));
Michal Vasko8d65f852021-02-17 11:28:13 +01001402 if (parent) {
Michal Vasko6ee6f432021-07-16 09:49:14 +02001403 lyd_insert_node(parent, NULL, ret, 1);
Michal Vasko8d65f852021-02-17 11:28:13 +01001404 }
1405
1406 if (node) {
1407 *node = ret;
1408 }
1409 return LY_SUCCESS;
1410}
1411
1412API LY_ERR
1413lyd_new_attr(struct lyd_node *parent, const char *module_name, const char *name, const char *value,
Radek Krejci0f969882020-08-21 16:56:47 +02001414 struct lyd_attr **attr)
Michal Vasko00cbf532020-06-15 13:58:47 +02001415{
Radek Krejci1798aae2020-07-14 13:26:06 +02001416 struct lyd_attr *ret = NULL;
Michal Vasko00cbf532020-06-15 13:58:47 +02001417 const struct ly_ctx *ctx;
1418 const char *prefix, *tmp;
Michal Vasko51d21b82020-11-13 18:03:54 +01001419 size_t pref_len, name_len, mod_len;
Michal Vasko00cbf532020-06-15 13:58:47 +02001420
Michal Vasko3a41dff2020-07-15 14:30:28 +02001421 LY_CHECK_ARG_RET(NULL, parent, !parent->schema, name, LY_EINVAL);
Michal Vasko00cbf532020-06-15 13:58:47 +02001422
Michal Vaskob7be7a82020-08-20 09:09:04 +02001423 ctx = LYD_CTX(parent);
Michal Vasko00cbf532020-06-15 13:58:47 +02001424
1425 /* parse the name */
1426 tmp = name;
1427 if (ly_parse_nodeid(&tmp, &prefix, &pref_len, &name, &name_len) || tmp[0]) {
Michal Vaskob69b68c2021-02-17 11:18:16 +01001428 LOGERR(ctx, LY_EINVAL, "Attribute name \"%s\" is not valid.", name);
Michal Vasko3a41dff2020-07-15 14:30:28 +02001429 return LY_EVALID;
Michal Vasko00cbf532020-06-15 13:58:47 +02001430 }
1431
Michal Vaskoe137fc42021-07-22 11:53:13 +02001432 if ((pref_len == 3) && !strncmp(prefix, "xml", 3)) {
1433 /* not a prefix but special name */
1434 name = prefix;
1435 name_len += 1 + pref_len;
1436 prefix = NULL;
1437 pref_len = 0;
1438 }
1439
Michal Vasko51d21b82020-11-13 18:03:54 +01001440 /* get the module */
1441 if (module_name) {
1442 mod_len = strlen(module_name);
1443 } else {
1444 module_name = prefix;
1445 mod_len = pref_len;
1446 }
1447
Michal Vasko00cbf532020-06-15 13:58:47 +02001448 /* set value if none */
Michal Vasko8d65f852021-02-17 11:28:13 +01001449 if (!value) {
1450 value = "";
Michal Vasko00cbf532020-06-15 13:58:47 +02001451 }
1452
Michal Vasko8d65f852021-02-17 11:28:13 +01001453 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 +02001454 strlen(value), NULL, LY_VALUE_JSON, NULL, LYD_HINT_DATA));
Michal Vasko8d65f852021-02-17 11:28:13 +01001455
1456 if (attr) {
1457 *attr = ret;
1458 }
1459 return LY_SUCCESS;
1460}
1461
1462API LY_ERR
1463lyd_new_attr2(struct lyd_node *parent, const char *module_ns, const char *name, const char *value,
1464 struct lyd_attr **attr)
1465{
1466 struct lyd_attr *ret = NULL;
1467 const struct ly_ctx *ctx;
1468 const char *prefix, *tmp;
1469 size_t pref_len, name_len;
1470
1471 LY_CHECK_ARG_RET(NULL, parent, !parent->schema, name, LY_EINVAL);
1472
1473 ctx = LYD_CTX(parent);
1474
1475 /* parse the name */
1476 tmp = name;
1477 if (ly_parse_nodeid(&tmp, &prefix, &pref_len, &name, &name_len) || tmp[0]) {
1478 LOGERR(ctx, LY_EINVAL, "Attribute name \"%s\" is not valid.", name);
1479 return LY_EVALID;
1480 }
1481
Michal Vaskoe137fc42021-07-22 11:53:13 +02001482 if ((pref_len == 3) && !strncmp(prefix, "xml", 3)) {
1483 /* not a prefix but special name */
1484 name = prefix;
1485 name_len += 1 + pref_len;
1486 prefix = NULL;
1487 pref_len = 0;
1488 }
1489
Michal Vasko8d65f852021-02-17 11:28:13 +01001490 /* set value if none */
1491 if (!value) {
1492 value = "";
1493 }
1494
1495 LY_CHECK_RET(lyd_create_attr(parent, &ret, ctx, name, name_len, prefix, pref_len, module_ns,
Radek Krejci8df109d2021-04-23 12:19:08 +02001496 module_ns ? strlen(module_ns) : 0, value, strlen(value), NULL, LY_VALUE_XML, NULL, LYD_HINT_DATA));
Michal Vasko3a41dff2020-07-15 14:30:28 +02001497
1498 if (attr) {
1499 *attr = ret;
1500 }
1501 return LY_SUCCESS;
Michal Vasko00cbf532020-06-15 13:58:47 +02001502}
1503
Radek Krejci09c77442021-04-26 11:10:34 +02001504/**
1505 * @brief Change the value of a term (leaf or leaf-list) node.
1506 *
1507 * Node changed this way is always considered explicitly set, meaning its default flag
1508 * is always cleared.
1509 *
1510 * @param[in] term Term node to change.
1511 * @param[in] value New value to set.
1512 * @param[in] value_len Length of @p value.
1513 * @param[in] format Format of @p value.
1514 * @return LY_SUCCESS if value was changed,
1515 * @return LY_EEXIST if value was the same and only the default flag was cleared,
1516 * @return LY_ENOT if the values were equal and no change occured,
1517 * @return LY_ERR value on other errors.
1518 */
1519static LY_ERR
1520_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 +02001521{
1522 LY_ERR ret = LY_SUCCESS;
1523 struct lysc_type *type;
1524 struct lyd_node_term *t;
1525 struct lyd_node *parent;
1526 struct lyd_value val = {0};
Radek Krejci857189e2020-09-01 13:26:36 +02001527 ly_bool dflt_change, val_change;
Michal Vasko00cbf532020-06-15 13:58:47 +02001528
Radek Krejci09c77442021-04-26 11:10:34 +02001529 assert(term && term->schema && (term->schema->nodetype & LYD_NODE_TERM));
Michal Vasko00cbf532020-06-15 13:58:47 +02001530
Michal Vasko00cbf532020-06-15 13:58:47 +02001531 t = (struct lyd_node_term *)term;
1532 type = ((struct lysc_node_leaf *)term->schema)->type;
1533
1534 /* parse the new value */
Radek Krejciddace2c2021-01-08 11:30:56 +01001535 LOG_LOCSET(term->schema, term, NULL, NULL);
Radek Krejci09c77442021-04-26 11:10:34 +02001536 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 +01001537 LOG_LOCBACK(term->schema ? 1 : 0, 1, 0, 0);
Michal Vaskofeca4fb2020-10-05 08:58:40 +02001538 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko00cbf532020-06-15 13:58:47 +02001539
1540 /* compare original and new value */
1541 if (type->plugin->compare(&t->value, &val)) {
1542 /* values differ, switch them */
Michal Vaskob7be7a82020-08-20 09:09:04 +02001543 type->plugin->free(LYD_CTX(term), &t->value);
Michal Vasko00cbf532020-06-15 13:58:47 +02001544 t->value = val;
1545 memset(&val, 0, sizeof val);
1546 val_change = 1;
1547 } else {
1548 val_change = 0;
1549 }
1550
1551 /* always clear the default flag */
1552 if (term->flags & LYD_DEFAULT) {
Michal Vasko9e685082021-01-29 14:49:09 +01001553 for (parent = term; parent; parent = lyd_parent(parent)) {
Michal Vasko00cbf532020-06-15 13:58:47 +02001554 parent->flags &= ~LYD_DEFAULT;
1555 }
1556 dflt_change = 1;
1557 } else {
1558 dflt_change = 0;
1559 }
1560
1561 if (val_change || dflt_change) {
1562 /* make the node non-validated */
1563 term->flags &= LYD_NEW;
1564 }
1565
1566 if (val_change) {
1567 if (term->schema->nodetype == LYS_LEAFLIST) {
1568 /* leaf-list needs to be hashed again and re-inserted into parent */
1569 lyd_unlink_hash(term);
1570 lyd_hash(term);
1571 LY_CHECK_GOTO(ret = lyd_insert_hash(term), cleanup);
1572 } else if ((term->schema->flags & LYS_KEY) && term->parent) {
1573 /* list needs to be updated if its key was changed */
1574 assert(term->parent->schema->nodetype == LYS_LIST);
Michal Vasko9e685082021-01-29 14:49:09 +01001575 lyd_unlink_hash(lyd_parent(term));
1576 lyd_hash(lyd_parent(term));
1577 LY_CHECK_GOTO(ret = lyd_insert_hash(lyd_parent(term)), cleanup);
Michal Vasko00cbf532020-06-15 13:58:47 +02001578 } /* else leaf that is not a key, its value is not used for its hash so it does not change */
1579 }
1580
1581 /* retrun value */
1582 if (!val_change) {
1583 if (dflt_change) {
1584 /* only default flag change */
1585 ret = LY_EEXIST;
1586 } else {
1587 /* no change */
1588 ret = LY_ENOT;
1589 }
1590 } /* else value changed, LY_SUCCESS */
1591
1592cleanup:
Michal Vasko59512fc2020-12-09 18:13:29 +01001593 if (val.realtype) {
1594 type->plugin->free(LYD_CTX(term), &val);
1595 }
Michal Vasko00cbf532020-06-15 13:58:47 +02001596 return ret;
1597}
1598
Michal Vasko41586352020-07-13 13:54:25 +02001599API LY_ERR
Radek Krejci09c77442021-04-26 11:10:34 +02001600lyd_change_term(struct lyd_node *term, const char *val_str)
1601{
1602 LY_CHECK_ARG_RET(NULL, term, term->schema, term->schema->nodetype & LYD_NODE_TERM, LY_EINVAL);
1603
1604 return _lyd_change_term(term, val_str, val_str ? strlen(val_str) : 0, LY_VALUE_JSON);
1605}
1606
1607API LY_ERR
1608lyd_change_term_bin(struct lyd_node *term, const void *value, size_t value_len)
1609{
1610 LY_CHECK_ARG_RET(NULL, term, term->schema, term->schema->nodetype & LYD_NODE_TERM, LY_EINVAL);
1611
1612 return _lyd_change_term(term, value, value_len, LY_VALUE_LYB);
1613}
1614
1615API LY_ERR
1616lyd_change_term_canon(struct lyd_node *term, const char *val_str)
1617{
1618 LY_CHECK_ARG_RET(NULL, term, term->schema, term->schema->nodetype & LYD_NODE_TERM, LY_EINVAL);
1619
1620 return _lyd_change_term(term, val_str, val_str ? strlen(val_str) : 0, LY_VALUE_CANON);
1621}
1622
1623API LY_ERR
Michal Vasko41586352020-07-13 13:54:25 +02001624lyd_change_meta(struct lyd_meta *meta, const char *val_str)
1625{
1626 LY_ERR ret = LY_SUCCESS;
Radek Krejci2b18bf12020-11-06 11:20:20 +01001627 struct lyd_meta *m2 = NULL;
Michal Vasko41586352020-07-13 13:54:25 +02001628 struct lyd_value val;
Radek Krejci857189e2020-09-01 13:26:36 +02001629 ly_bool val_change;
Michal Vasko41586352020-07-13 13:54:25 +02001630
1631 LY_CHECK_ARG_RET(NULL, meta, LY_EINVAL);
1632
1633 if (!val_str) {
1634 val_str = "";
1635 }
1636
1637 /* parse the new value into a new meta structure */
1638 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 +02001639 strlen(val_str), NULL, LY_VALUE_JSON, NULL, LYD_HINT_DATA, 0, NULL), cleanup);
Michal Vasko41586352020-07-13 13:54:25 +02001640
1641 /* compare original and new value */
1642 if (lyd_compare_meta(meta, m2)) {
1643 /* values differ, switch them */
1644 val = meta->value;
1645 meta->value = m2->value;
1646 m2->value = val;
1647 val_change = 1;
1648 } else {
1649 val_change = 0;
1650 }
1651
1652 /* retrun value */
1653 if (!val_change) {
1654 /* no change */
1655 ret = LY_ENOT;
1656 } /* else value changed, LY_SUCCESS */
1657
1658cleanup:
Michal Vasko1a66a5d2020-11-18 18:15:37 +01001659 lyd_free_meta_single(m2);
Michal Vasko41586352020-07-13 13:54:25 +02001660 return ret;
1661}
1662
Radek Krejci09c77442021-04-26 11:10:34 +02001663/**
1664 * @brief Update node value.
1665 *
1666 * @param[in] node Node to update.
1667 * @param[in] value New value to set.
1668 * @param[in] value_len Length of @p value.
1669 * @param[in] value_type Type of @p value for anydata/anyxml node.
1670 * @param[in] format Format of @p value.
1671 * @param[out] new_parent Set to @p node if the value was updated, otherwise set to NULL.
1672 * @param[out] new_node Set to @p node if the value was updated, otherwise set to NULL.
1673 * @return LY_ERR value.
1674 */
Michal Vasko6741dc62021-02-04 09:27:45 +01001675static LY_ERR
Radek Krejci09c77442021-04-26 11:10:34 +02001676lyd_new_path_update(struct lyd_node *node, const void *value, size_t value_len, LYD_ANYDATA_VALUETYPE value_type,
1677 LY_VALUE_FORMAT format, struct lyd_node **new_parent, struct lyd_node **new_node)
1678{
1679 LY_ERR ret = LY_SUCCESS;
1680 struct lyd_node *new_any;
1681
1682 switch (node->schema->nodetype) {
1683 case LYS_CONTAINER:
1684 case LYS_NOTIF:
1685 case LYS_RPC:
1686 case LYS_ACTION:
1687 case LYS_LIST:
1688 /* if it exists, there is nothing to update */
1689 *new_parent = NULL;
1690 *new_node = NULL;
1691 break;
1692 case LYS_LEAFLIST:
1693 if (!lysc_is_dup_inst_list(node->schema)) {
1694 /* if it exists, there is nothing to update */
1695 *new_parent = NULL;
1696 *new_node = NULL;
1697 break;
1698 }
1699 /* fallthrough */
1700 case LYS_LEAF:
1701 ret = _lyd_change_term(node, value, value_len, format);
1702 if ((ret == LY_SUCCESS) || (ret == LY_EEXIST)) {
1703 /* there was an actual change (at least of the default flag) */
1704 *new_parent = node;
1705 *new_node = node;
1706 ret = LY_SUCCESS;
1707 } else if (ret == LY_ENOT) {
1708 /* no change */
1709 *new_parent = NULL;
1710 *new_node = NULL;
1711 ret = LY_SUCCESS;
1712 } /* else error */
1713 break;
1714 case LYS_ANYDATA:
1715 case LYS_ANYXML:
1716 /* create a new any node */
1717 LY_CHECK_RET(lyd_create_any(node->schema, value, value_type, 0, &new_any));
1718
1719 /* compare with the existing one */
1720 if (lyd_compare_single(node, new_any, 0)) {
1721 /* not equal, switch values (so that we can use generic node free) */
1722 ((struct lyd_node_any *)new_any)->value = ((struct lyd_node_any *)node)->value;
1723 ((struct lyd_node_any *)new_any)->value_type = ((struct lyd_node_any *)node)->value_type;
1724 ((struct lyd_node_any *)node)->value.str = value;
1725 ((struct lyd_node_any *)node)->value_type = value_type;
1726
1727 *new_parent = node;
1728 *new_node = node;
1729 } else {
1730 /* they are equal */
1731 *new_parent = NULL;
1732 *new_node = NULL;
1733 }
1734 lyd_free_tree(new_any);
1735 break;
1736 default:
1737 LOGINT(LYD_CTX(node));
1738 ret = LY_EINT;
1739 break;
1740 }
1741
1742 return ret;
1743}
1744
1745static LY_ERR
1746lyd_new_path_check_find_lypath(struct ly_path *path, const char *str_path, const char *value, size_t value_len,
1747 LY_VALUE_FORMAT format, uint32_t options)
Michal Vasko6741dc62021-02-04 09:27:45 +01001748{
Michal Vaskoe78faec2021-04-08 17:24:43 +02001749 LY_ERR r;
Michal Vasko6741dc62021-02-04 09:27:45 +01001750 struct ly_path_predicate *pred;
Michal Vaskoe78faec2021-04-08 17:24:43 +02001751 const struct lysc_node *schema = NULL;
1752 LY_ARRAY_COUNT_TYPE u, new_count;
1753 int create = 0;
Michal Vasko6741dc62021-02-04 09:27:45 +01001754
1755 assert(path);
1756
1757 /* go through all the compiled nodes */
1758 LY_ARRAY_FOR(path, u) {
1759 schema = path[u].node;
Michal Vaskoe78faec2021-04-08 17:24:43 +02001760
1761 if (lysc_is_dup_inst_list(schema)) {
1762 if (path[u].pred_type == LY_PATH_PREDTYPE_NONE) {
1763 /* creating a new key-less list or state leaf-list instance */
1764 create = 1;
1765 new_count = u;
1766 } else if (path[u].pred_type != LY_PATH_PREDTYPE_POSITION) {
1767 LOG_LOCSET(schema, NULL, NULL, NULL);
1768 LOGVAL(schema->module->ctx, LYVE_XPATH, "Invalid predicate for %s \"%s\" in path \"%s\".",
1769 lys_nodetype2str(schema->nodetype), schema->name, str_path);
1770 LOG_LOCBACK(1, 0, 0, 0);
1771 return LY_EINVAL;
1772 }
1773 } else if ((schema->nodetype == LYS_LIST) && (path[u].pred_type != LY_PATH_PREDTYPE_LIST)) {
1774 if ((u < LY_ARRAY_COUNT(path) - 1) || !(options & LYD_NEW_PATH_OPAQ)) {
Michal Vasko6741dc62021-02-04 09:27:45 +01001775 LOG_LOCSET(schema, NULL, NULL, NULL);
1776 LOGVAL(schema->module->ctx, LYVE_XPATH, "Predicate missing for %s \"%s\" in path \"%s\".",
1777 lys_nodetype2str(schema->nodetype), schema->name, str_path);
1778 LOG_LOCBACK(1, 0, 0, 0);
1779 return LY_EINVAL;
Michal Vaskoe78faec2021-04-08 17:24:43 +02001780 } /* else creating an opaque list */
1781 } else if ((schema->nodetype == LYS_LEAFLIST) && (path[u].pred_type != LY_PATH_PREDTYPE_LEAFLIST)) {
Michal Vaskoe78faec2021-04-08 17:24:43 +02001782 r = LY_SUCCESS;
1783 if (options & LYD_NEW_PATH_OPAQ) {
Radek Krejci09c77442021-04-26 11:10:34 +02001784 r = lyd_value_validate(NULL, schema, value, value_len, NULL, NULL, NULL);
Michal Vaskoe78faec2021-04-08 17:24:43 +02001785 }
1786 if (!r) {
1787 /* store the new predicate so that it is used when searching for this instance */
1788 path[u].pred_type = LY_PATH_PREDTYPE_LEAFLIST;
1789 LY_ARRAY_NEW_RET(schema->module->ctx, path[u].predicates, pred, LY_EMEM);
1790
1791 LY_CHECK_RET(lyd_value_store(schema->module->ctx, &pred->value,
Radek Krejci09c77442021-04-26 11:10:34 +02001792 ((struct lysc_node_leaflist *)schema)->type, value, value_len, NULL, format, NULL,
Michal Vaskoe78faec2021-04-08 17:24:43 +02001793 LYD_HINT_DATA, schema, NULL));
1794 ++((struct lysc_type *)pred->value.realtype)->refcount;
1795 } /* else we have opaq flag and the value is not valid, leave no predicate and then create an opaque node */
1796 }
Michal Vasko6741dc62021-02-04 09:27:45 +01001797 }
1798
Michal Vaskoe78faec2021-04-08 17:24:43 +02001799 if (create) {
1800 /* hide the nodes that should always be created so they are not found */
1801 while (new_count < LY_ARRAY_COUNT(path)) {
1802 LY_ARRAY_DECREMENT(path);
1803 }
Michal Vasko6741dc62021-02-04 09:27:45 +01001804 }
1805
Michal Vaskoe78faec2021-04-08 17:24:43 +02001806 return LY_SUCCESS;
Michal Vasko6741dc62021-02-04 09:27:45 +01001807}
1808
Radek Krejci95ccd1b2021-03-12 14:57:22 +01001809/**
1810 * @brief Create a new node in the data tree based on a path. All node types can be created.
1811 *
Michal Vaskoa918a632021-07-02 11:53:36 +02001812 * If @p path points to a list key, the key value from the predicate is used and @p value is ignored.
1813 * Also, if a leaf-list is being created and both a predicate is defined in @p path
Radek Krejci95ccd1b2021-03-12 14:57:22 +01001814 * and @p value is set, the predicate is preferred.
1815 *
1816 * For key-less lists and state leaf-lists, positional predicates can be used. If no preciate is used for these
1817 * nodes, they are always created.
1818 *
1819 * @param[in] parent Data parent to add to/modify, can be NULL. Note that in case a first top-level sibling is used,
1820 * it may no longer be first if @p path is absolute and starts with a non-existing top-level node inserted
1821 * before @p parent. Use ::lyd_first_sibling() to adjust @p parent in these cases.
1822 * @param[in] ctx libyang context, must be set if @p parent is NULL.
1823 * @param[in] ext Extension instance where the node being created is defined. This argument takes effect only for absolute
1824 * path or when the relative paths touches document root (top-level). In such cases the present extension instance replaces
1825 * searching for the appropriate module.
1826 * @param[in] path [Path](@ref howtoXPath) to create.
Radek Krejci09c77442021-04-26 11:10:34 +02001827 * @param[in] value Value of the new leaf/leaf-list (const char *) in ::LY_VALUE_JSON format. If creating an
1828 * anyxml/anydata node, the expected type depends on @p value_type. For other node types, it should be NULL.
1829 * @param[in] value_len Length of @p value in bytes. May be 0 if @p value is a zero-terminated string. Ignored when
1830 * creating anyxml/anydata nodes.
Radek Krejci95ccd1b2021-03-12 14:57:22 +01001831 * @param[in] value_type Anyxml/anydata node @p value type.
1832 * @param[in] options Bitmask of options, see @ref pathoptions.
1833 * @param[out] new_parent Optional first parent node created. If only one node was created, equals to @p new_node.
1834 * @param[out] new_node Optional last node created.
1835 * @return LY_ERR value.
1836 */
1837static LY_ERR
1838lyd_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 +02001839 const void *value, size_t value_len, LYD_ANYDATA_VALUETYPE value_type, uint32_t options,
Radek Krejci95ccd1b2021-03-12 14:57:22 +01001840 struct lyd_node **new_parent, struct lyd_node **new_node)
Michal Vasko00cbf532020-06-15 13:58:47 +02001841{
1842 LY_ERR ret = LY_SUCCESS, r;
1843 struct lyxp_expr *exp = NULL;
1844 struct ly_path *p = NULL;
1845 struct lyd_node *nparent = NULL, *nnode = NULL, *node = NULL, *cur_parent;
1846 const struct lysc_node *schema;
Michal Vaskoe78faec2021-04-08 17:24:43 +02001847 const struct lyd_value *val = NULL;
Radek Krejci910d0132021-03-05 14:19:31 +01001848 LY_ARRAY_COUNT_TYPE path_idx = 0, orig_count = 0;
Radek Krejci09c77442021-04-26 11:10:34 +02001849 LY_VALUE_FORMAT format;
Michal Vasko00cbf532020-06-15 13:58:47 +02001850
Radek Krejci95ccd1b2021-03-12 14:57:22 +01001851 assert(parent || ctx);
Radek Krejci09c77442021-04-26 11:10:34 +02001852 assert(path && ((path[0] == '/') || parent));
1853 assert(!(options & LYD_NEW_PATH_BIN_VALUE) || !(options & LYD_NEW_PATH_CANON_VALUE));
Michal Vasko00cbf532020-06-15 13:58:47 +02001854
1855 if (!ctx) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02001856 ctx = LYD_CTX(parent);
Michal Vasko00cbf532020-06-15 13:58:47 +02001857 }
Radek Krejci09c77442021-04-26 11:10:34 +02001858 if (value && !value_len) {
1859 value_len = strlen(value);
1860 }
1861 if (options & LYD_NEW_PATH_BIN_VALUE) {
1862 format = LY_VALUE_LYB;
1863 } else if (options & LYD_NEW_PATH_CANON_VALUE) {
1864 format = LY_VALUE_CANON;
1865 } else {
1866 format = LY_VALUE_JSON;
1867 }
Michal Vasko00cbf532020-06-15 13:58:47 +02001868
1869 /* parse path */
Michal Vaskoed725d72021-06-23 12:03:45 +02001870 LY_CHECK_GOTO(ret = ly_path_parse(ctx, NULL, path, strlen(path), 0, LY_PATH_BEGIN_EITHER, LY_PATH_PREFIX_OPTIONAL,
1871 LY_PATH_PRED_SIMPLE, &exp), cleanup);
Michal Vasko00cbf532020-06-15 13:58:47 +02001872
1873 /* compile path */
Michal Vasko106f0862021-11-02 11:49:27 +01001874 LY_CHECK_GOTO(ret = ly_path_compile(ctx, NULL, lyd_node_schema(parent), ext, exp, options & LYD_NEW_PATH_OUTPUT ?
1875 LY_PATH_OPER_OUTPUT : LY_PATH_OPER_INPUT, LY_PATH_TARGET_MANY, 0, LY_VALUE_JSON, NULL, &p), cleanup);
Michal Vasko00cbf532020-06-15 13:58:47 +02001876
Michal Vasko6741dc62021-02-04 09:27:45 +01001877 /* check the compiled path before searching existing nodes, it may be shortened */
1878 orig_count = LY_ARRAY_COUNT(p);
Radek Krejci09c77442021-04-26 11:10:34 +02001879 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 +02001880
1881 /* try to find any existing nodes in the path */
1882 if (parent) {
1883 ret = ly_path_eval_partial(p, parent, &path_idx, &node);
1884 if (ret == LY_SUCCESS) {
Michal Vasko6741dc62021-02-04 09:27:45 +01001885 if (orig_count == LY_ARRAY_COUNT(p)) {
1886 /* the node exists, are we supposed to update it or is it just a default? */
1887 if (!(options & LYD_NEW_PATH_UPDATE) && !(node->flags & LYD_DEFAULT)) {
1888 LOG_LOCSET(NULL, node, NULL, NULL);
1889 LOGVAL(ctx, LYVE_REFERENCE, "Path \"%s\" already exists", path);
1890 LOG_LOCBACK(0, 1, 0, 0);
1891 ret = LY_EEXIST;
1892 goto cleanup;
1893 }
Michal Vasko00cbf532020-06-15 13:58:47 +02001894
Michal Vasko6741dc62021-02-04 09:27:45 +01001895 /* update the existing node */
Radek Krejci09c77442021-04-26 11:10:34 +02001896 ret = lyd_new_path_update(node, value, value_len, value_type, format, &nparent, &nnode);
Michal Vasko6741dc62021-02-04 09:27:45 +01001897 goto cleanup;
1898 } /* else we were not searching for the whole path */
Michal Vasko00cbf532020-06-15 13:58:47 +02001899 } else if (ret == LY_EINCOMPLETE) {
1900 /* some nodes were found, adjust the iterator to the next segment */
1901 ++path_idx;
1902 } else if (ret == LY_ENOTFOUND) {
1903 /* 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 +01001904 if (lysc_data_parent(p[0].node)) {
Michal Vasko00cbf532020-06-15 13:58:47 +02001905 node = parent;
1906 }
1907 } else {
1908 /* error */
1909 goto cleanup;
1910 }
1911 }
1912
Michal Vasko6741dc62021-02-04 09:27:45 +01001913 /* restore the full path for creating new nodes */
1914 while (orig_count > LY_ARRAY_COUNT(p)) {
1915 LY_ARRAY_INCREMENT(p);
1916 }
1917
Michal Vasko00cbf532020-06-15 13:58:47 +02001918 /* create all the non-existing nodes in a loop */
Michal Vaskod989ba02020-08-24 10:59:24 +02001919 for ( ; path_idx < LY_ARRAY_COUNT(p); ++path_idx) {
Michal Vasko00cbf532020-06-15 13:58:47 +02001920 cur_parent = node;
1921 schema = p[path_idx].node;
1922
1923 switch (schema->nodetype) {
1924 case LYS_LIST:
Michal Vaskoe78faec2021-04-08 17:24:43 +02001925 if (lysc_is_dup_inst_list(schema)) {
1926 /* create key-less list instance */
1927 LY_CHECK_GOTO(ret = lyd_create_inner(schema, &node), cleanup);
1928 } else if ((options & LYD_NEW_PATH_OPAQ) && (p[path_idx].pred_type == LY_PATH_PREDTYPE_NONE)) {
1929 /* creating opaque list without keys */
1930 LY_CHECK_GOTO(ret = lyd_create_opaq(ctx, schema->name, strlen(schema->name), NULL, 0,
Radek Krejci8df109d2021-04-23 12:19:08 +02001931 schema->module->name, strlen(schema->module->name), NULL, 0, NULL, LY_VALUE_JSON, NULL,
Michal Vaskoe78faec2021-04-08 17:24:43 +02001932 LYD_NODEHINT_LIST, &node), cleanup);
1933 } else {
1934 /* create standard list instance */
1935 assert(p[path_idx].pred_type == LY_PATH_PREDTYPE_LIST);
1936 LY_CHECK_GOTO(ret = lyd_create_list(schema, p[path_idx].predicates, &node), cleanup);
Michal Vasko00cbf532020-06-15 13:58:47 +02001937 }
Michal Vaskoe78faec2021-04-08 17:24:43 +02001938 break;
Michal Vasko00cbf532020-06-15 13:58:47 +02001939 case LYS_CONTAINER:
1940 case LYS_NOTIF:
1941 case LYS_RPC:
1942 case LYS_ACTION:
1943 LY_CHECK_GOTO(ret = lyd_create_inner(schema, &node), cleanup);
1944 break;
1945 case LYS_LEAFLIST:
Michal Vaskoe78faec2021-04-08 17:24:43 +02001946 if ((options & LYD_NEW_PATH_OPAQ) && (p[path_idx].pred_type != LY_PATH_PREDTYPE_LEAFLIST)) {
1947 /* we have not checked this only for dup-inst lists, otherwise it must be opaque */
1948 r = LY_EVALID;
1949 if (lysc_is_dup_inst_list(schema)) {
1950 /* validate value */
Radek Krejci09c77442021-04-26 11:10:34 +02001951 r = lyd_value_validate(NULL, schema, value ? value : "", value_len, NULL, NULL, NULL);
Michal Vaskoe78faec2021-04-08 17:24:43 +02001952 }
1953 if (r && (r != LY_EINCOMPLETE)) {
1954 /* creating opaque leaf-list */
Radek Krejci09c77442021-04-26 11:10:34 +02001955 LY_CHECK_GOTO(ret = lyd_create_opaq(ctx, schema->name, strlen(schema->name), value, value_len,
1956 schema->module->name, strlen(schema->module->name), NULL, 0, NULL, format, NULL,
1957 LYD_NODEHINT_LEAFLIST, &node), cleanup);
Michal Vaskoe78faec2021-04-08 17:24:43 +02001958 break;
1959 }
1960 }
1961
1962 /* get value to set */
1963 if (p[path_idx].pred_type == LY_PATH_PREDTYPE_LEAFLIST) {
1964 val = &p[path_idx].predicates[0].value;
Michal Vaskoe78faec2021-04-08 17:24:43 +02001965 }
1966
1967 /* create a leaf-list instance */
1968 if (val) {
Michal Vasko00cbf532020-06-15 13:58:47 +02001969 LY_CHECK_GOTO(ret = lyd_create_term2(schema, &p[path_idx].predicates[0].value, &node), cleanup);
Michal Vaskoe78faec2021-04-08 17:24:43 +02001970 } else {
Radek Krejci09c77442021-04-26 11:10:34 +02001971 LY_CHECK_GOTO(ret = lyd_create_term(schema, value, value_len, NULL, format, NULL, LYD_HINT_DATA,
1972 NULL, &node), cleanup);
Michal Vasko00cbf532020-06-15 13:58:47 +02001973 }
1974 break;
1975 case LYS_LEAF:
Michal Vasko106f0862021-11-02 11:49:27 +01001976 if (lysc_is_key(schema) && cur_parent->schema) {
Michal Vasko35880332020-12-08 13:08:12 +01001977 /* it must have been already created or some error will occur later */
Michal Vaskoa918a632021-07-02 11:53:36 +02001978 lyd_find_sibling_schema(lyd_child(cur_parent), schema, &node);
1979 assert(node);
Michal Vasko35880332020-12-08 13:08:12 +01001980 goto next_iter;
1981 }
1982
Michal Vaskoe78faec2021-04-08 17:24:43 +02001983 if (options & LYD_NEW_PATH_OPAQ) {
Michal Vasko106f0862021-11-02 11:49:27 +01001984 if (cur_parent && !cur_parent->schema) {
1985 /* always create opaque nodes for opaque parents */
1986 r = LY_ENOT;
1987 } else {
1988 /* validate value */
1989 r = lyd_value_validate(NULL, schema, value ? value : "", value_len, NULL, NULL, NULL);
1990 }
Michal Vaskoe78faec2021-04-08 17:24:43 +02001991 if (r && (r != LY_EINCOMPLETE)) {
1992 /* creating opaque leaf */
Radek Krejci09c77442021-04-26 11:10:34 +02001993 LY_CHECK_GOTO(ret = lyd_create_opaq(ctx, schema->name, strlen(schema->name), value, value_len,
1994 schema->module->name, strlen(schema->module->name), NULL, 0, NULL, format, NULL, 0, &node),
1995 cleanup);
Michal Vaskoe78faec2021-04-08 17:24:43 +02001996 break;
1997 }
1998 }
1999
Michal Vaskoe78faec2021-04-08 17:24:43 +02002000 /* create a leaf instance */
Radek Krejci09c77442021-04-26 11:10:34 +02002001 LY_CHECK_GOTO(ret = lyd_create_term(schema, value, value_len, NULL, format, NULL, LYD_HINT_DATA, NULL,
2002 &node), cleanup);
Michal Vasko00cbf532020-06-15 13:58:47 +02002003 break;
2004 case LYS_ANYDATA:
2005 case LYS_ANYXML:
Michal Vasko366a4a12020-12-04 16:23:57 +01002006 LY_CHECK_GOTO(ret = lyd_create_any(schema, value, value_type, 0, &node), cleanup);
Michal Vasko00cbf532020-06-15 13:58:47 +02002007 break;
2008 default:
2009 LOGINT(ctx);
2010 ret = LY_EINT;
2011 goto cleanup;
2012 }
2013
2014 if (cur_parent) {
2015 /* connect to the parent */
Michal Vasko6ee6f432021-07-16 09:49:14 +02002016 lyd_insert_node(cur_parent, NULL, node, 0);
Michal Vasko00cbf532020-06-15 13:58:47 +02002017 } else if (parent) {
2018 /* connect to top-level siblings */
Michal Vasko6ee6f432021-07-16 09:49:14 +02002019 lyd_insert_node(NULL, &parent, node, 0);
Michal Vasko00cbf532020-06-15 13:58:47 +02002020 }
2021
Michal Vasko35880332020-12-08 13:08:12 +01002022next_iter:
Michal Vasko00cbf532020-06-15 13:58:47 +02002023 /* update remembered nodes */
2024 if (!nparent) {
2025 nparent = node;
2026 }
2027 nnode = node;
2028 }
2029
2030cleanup:
2031 lyxp_expr_free(ctx, exp);
Michal Vasko6741dc62021-02-04 09:27:45 +01002032 if (p) {
2033 while (orig_count > LY_ARRAY_COUNT(p)) {
2034 LY_ARRAY_INCREMENT(p);
2035 }
2036 }
Michal Vasko00cbf532020-06-15 13:58:47 +02002037 ly_path_free(ctx, p);
2038 if (!ret) {
2039 /* set out params only on success */
2040 if (new_parent) {
2041 *new_parent = nparent;
2042 }
2043 if (new_node) {
2044 *new_node = nnode;
2045 }
Michal Vaskof4b95ba2020-12-11 08:42:33 +01002046 } else {
2047 lyd_free_tree(nparent);
Michal Vasko00cbf532020-06-15 13:58:47 +02002048 }
2049 return ret;
2050}
2051
Radek Krejci95ccd1b2021-03-12 14:57:22 +01002052API LY_ERR
2053lyd_new_path(struct lyd_node *parent, const struct ly_ctx *ctx, const char *path, const char *value, uint32_t options,
2054 struct lyd_node **node)
2055{
Radek Krejci09c77442021-04-26 11:10:34 +02002056 LY_CHECK_ARG_RET(ctx, parent || ctx, path, (path[0] == '/') || parent,
2057 !(options & LYD_NEW_PATH_BIN_VALUE) || !(options & LYD_NEW_PATH_CANON_VALUE), LY_EINVAL);
Michal Vasko892f5bf2021-11-24 10:41:05 +01002058 LY_CHECK_CTX_EQUAL_RET(parent ? LYD_CTX(parent) : NULL, ctx, LY_EINVAL);
Radek Krejci09c77442021-04-26 11:10:34 +02002059
2060 return lyd_new_path_(parent, ctx, NULL, path, value, 0, LYD_ANYDATA_STRING, options, node, NULL);
Radek Krejci95ccd1b2021-03-12 14:57:22 +01002061}
2062
2063API LY_ERR
2064lyd_new_path2(struct lyd_node *parent, const struct ly_ctx *ctx, const char *path, const void *value,
Radek Krejci09c77442021-04-26 11:10:34 +02002065 size_t value_len, LYD_ANYDATA_VALUETYPE value_type, uint32_t options, struct lyd_node **new_parent,
2066 struct lyd_node **new_node)
Radek Krejci95ccd1b2021-03-12 14:57:22 +01002067{
Radek Krejci09c77442021-04-26 11:10:34 +02002068 LY_CHECK_ARG_RET(ctx, parent || ctx, path, (path[0] == '/') || parent,
2069 !(options & LYD_NEW_PATH_BIN_VALUE) || !(options & LYD_NEW_PATH_CANON_VALUE), LY_EINVAL);
Michal Vasko892f5bf2021-11-24 10:41:05 +01002070 LY_CHECK_CTX_EQUAL_RET(parent ? LYD_CTX(parent) : NULL, ctx, LY_EINVAL);
2071
Radek Krejci09c77442021-04-26 11:10:34 +02002072 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 +01002073}
2074
2075API LY_ERR
2076lyd_new_ext_path(struct lyd_node *parent, const struct lysc_ext_instance *ext, const char *path, const void *value,
2077 uint32_t options, struct lyd_node **node)
2078{
2079 const struct ly_ctx *ctx = ext ? ext->module->ctx : NULL;
2080
Radek Krejci09c77442021-04-26 11:10:34 +02002081 LY_CHECK_ARG_RET(ctx, ext, path, (path[0] == '/') || parent,
2082 !(options & LYD_NEW_PATH_BIN_VALUE) || !(options & LYD_NEW_PATH_CANON_VALUE), LY_EINVAL);
Michal Vasko892f5bf2021-11-24 10:41:05 +01002083 LY_CHECK_CTX_EQUAL_RET(parent ? LYD_CTX(parent) : NULL, ctx, LY_EINVAL);
2084
Radek Krejci09c77442021-04-26 11:10:34 +02002085 return lyd_new_path_(parent, ctx, ext, path, value, 0, LYD_ANYDATA_STRING, options, node, NULL);
Radek Krejci95ccd1b2021-03-12 14:57:22 +01002086}
2087
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002088LY_ERR
2089lyd_new_implicit_r(struct lyd_node *parent, struct lyd_node **first, const struct lysc_node *sparent,
Radek Krejci4f2e3e52021-03-30 14:20:28 +02002090 const struct lys_module *mod, struct ly_set *node_when, struct ly_set *node_exts, struct ly_set *node_types,
2091 uint32_t impl_opts, struct lyd_node **diff)
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002092{
2093 LY_ERR ret;
Michal Vaskod1e53b92021-01-28 13:11:06 +01002094 const struct lysc_node *iter = NULL;
Radek Krejci2b18bf12020-11-06 11:20:20 +01002095 struct lyd_node *node = NULL;
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002096 struct lyd_value **dflts;
2097 LY_ARRAY_COUNT_TYPE u;
Michal Vasko630d9892020-12-08 17:11:08 +01002098 uint32_t getnext_opts;
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002099
2100 assert(first && (parent || sparent || mod));
2101
2102 if (!sparent && parent) {
2103 sparent = parent->schema;
2104 }
2105
Michal Vasko630d9892020-12-08 17:11:08 +01002106 getnext_opts = LYS_GETNEXT_WITHCHOICE;
2107 if (impl_opts & LYD_IMPLICIT_OUTPUT) {
2108 getnext_opts |= LYS_GETNEXT_OUTPUT;
2109 }
2110
2111 while ((iter = lys_getnext(iter, sparent, mod ? mod->compiled : NULL, getnext_opts))) {
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002112 if ((impl_opts & LYD_IMPLICIT_NO_STATE) && (iter->flags & LYS_CONFIG_R)) {
2113 continue;
Michal Vasko44b19a12020-08-07 09:21:30 +02002114 } else if ((impl_opts & LYD_IMPLICIT_NO_CONFIG) && (iter->flags & LYS_CONFIG_W)) {
2115 continue;
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002116 }
2117
2118 switch (iter->nodetype) {
2119 case LYS_CHOICE:
Michal Vaskobcf4d7d2020-11-18 18:16:49 +01002120 node = lys_getnext_data(NULL, *first, NULL, iter, NULL);
2121 if (!node && ((struct lysc_node_choice *)iter)->dflt) {
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002122 /* create default case data */
Michal Vasko14ed9cd2021-01-28 14:16:25 +01002123 LY_CHECK_RET(lyd_new_implicit_r(parent, first, &((struct lysc_node_choice *)iter)->dflt->node,
Radek Krejci4f2e3e52021-03-30 14:20:28 +02002124 NULL, node_when, node_exts, node_types, impl_opts, diff));
Michal Vaskobcf4d7d2020-11-18 18:16:49 +01002125 } else if (node) {
2126 /* create any default data in the existing case */
2127 assert(node->schema->parent->nodetype == LYS_CASE);
Radek Krejci4f2e3e52021-03-30 14:20:28 +02002128 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 +01002129 impl_opts, diff));
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002130 }
2131 break;
2132 case LYS_CONTAINER:
2133 if (!(iter->flags & LYS_PRESENCE) && lyd_find_sibling_val(*first, iter, NULL, 0, NULL)) {
2134 /* create default NP container */
2135 LY_CHECK_RET(lyd_create_inner(iter, &node));
Michal Vaskof4d67ea2021-03-31 13:53:21 +02002136 node->flags = LYD_DEFAULT | (lysc_has_when(iter) ? LYD_WHEN_TRUE : 0);
Michal Vasko6ee6f432021-07-16 09:49:14 +02002137 lyd_insert_node(parent, first, node, 0);
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002138
Michal Vaskof4d67ea2021-03-31 13:53:21 +02002139 if (lysc_has_when(iter) && node_when) {
Michal Vaskoe16c7b72021-02-26 10:39:06 +01002140 /* remember to resolve when */
2141 LY_CHECK_RET(ly_set_add(node_when, node, 1, NULL));
2142 }
Radek Krejci4f2e3e52021-03-30 14:20:28 +02002143 if (node_exts) {
2144 /* remember to call all the extension's validation callbacks */
2145 LY_CHECK_RET(lysc_node_ext_tovalidate(node_exts, node));
2146 }
Michal Vaskoe49b6322020-11-05 17:38:36 +01002147 if (diff) {
2148 /* add into diff */
2149 LY_CHECK_RET(lyd_val_diff_add(node, LYD_DIFF_OP_CREATE, diff));
2150 }
2151
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002152 /* create any default children */
Radek Krejci4f2e3e52021-03-30 14:20:28 +02002153 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 +02002154 impl_opts, diff));
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002155 }
2156 break;
2157 case LYS_LEAF:
Michal Vasko69730152020-10-09 16:30:07 +02002158 if (!(impl_opts & LYD_IMPLICIT_NO_DEFAULTS) && ((struct lysc_node_leaf *)iter)->dflt &&
2159 lyd_find_sibling_val(*first, iter, NULL, 0, NULL)) {
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002160 /* create default leaf */
2161 ret = lyd_create_term2(iter, ((struct lysc_node_leaf *)iter)->dflt, &node);
2162 if (ret == LY_EINCOMPLETE) {
2163 if (node_types) {
2164 /* remember to resolve type */
Radek Krejci3d92e442020-10-12 12:48:13 +02002165 LY_CHECK_RET(ly_set_add(node_types, node, 1, NULL));
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002166 }
2167 } else if (ret) {
2168 return ret;
2169 }
Michal Vaskof4d67ea2021-03-31 13:53:21 +02002170 node->flags = LYD_DEFAULT | (lysc_has_when(iter) ? LYD_WHEN_TRUE : 0);
Michal Vasko6ee6f432021-07-16 09:49:14 +02002171 lyd_insert_node(parent, first, node, 0);
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002172
Michal Vaskof4d67ea2021-03-31 13:53:21 +02002173 if (lysc_has_when(iter) && node_when) {
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002174 /* remember to resolve when */
Radek Krejci3d92e442020-10-12 12:48:13 +02002175 LY_CHECK_RET(ly_set_add(node_when, node, 1, NULL));
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002176 }
Radek Krejci4f2e3e52021-03-30 14:20:28 +02002177 if (node_exts) {
2178 /* remember to call all the extension's validation callbacks */
2179 LY_CHECK_RET(lysc_node_ext_tovalidate(node_exts, node));
2180 }
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002181 if (diff) {
2182 /* add into diff */
2183 LY_CHECK_RET(lyd_val_diff_add(node, LYD_DIFF_OP_CREATE, diff));
2184 }
2185 }
2186 break;
2187 case LYS_LEAFLIST:
Michal Vasko69730152020-10-09 16:30:07 +02002188 if (!(impl_opts & LYD_IMPLICIT_NO_DEFAULTS) && ((struct lysc_node_leaflist *)iter)->dflts &&
2189 lyd_find_sibling_val(*first, iter, NULL, 0, NULL)) {
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002190 /* create all default leaf-lists */
2191 dflts = ((struct lysc_node_leaflist *)iter)->dflts;
2192 LY_ARRAY_FOR(dflts, u) {
2193 ret = lyd_create_term2(iter, dflts[u], &node);
2194 if (ret == LY_EINCOMPLETE) {
2195 if (node_types) {
2196 /* remember to resolve type */
Radek Krejci3d92e442020-10-12 12:48:13 +02002197 LY_CHECK_RET(ly_set_add(node_types, node, 1, NULL));
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002198 }
2199 } else if (ret) {
2200 return ret;
2201 }
Michal Vaskof4d67ea2021-03-31 13:53:21 +02002202 node->flags = LYD_DEFAULT | (lysc_has_when(iter) ? LYD_WHEN_TRUE : 0);
Michal Vasko6ee6f432021-07-16 09:49:14 +02002203 lyd_insert_node(parent, first, node, 0);
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002204
Michal Vaskof4d67ea2021-03-31 13:53:21 +02002205 if (lysc_has_when(iter) && node_when) {
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002206 /* remember to resolve when */
Radek Krejci3d92e442020-10-12 12:48:13 +02002207 LY_CHECK_RET(ly_set_add(node_when, node, 1, NULL));
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002208 }
Radek Krejci4f2e3e52021-03-30 14:20:28 +02002209 if (node_exts) {
2210 /* remember to call all the extension's validation callbacks */
2211 LY_CHECK_RET(lysc_node_ext_tovalidate(node_exts, node));
2212 }
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002213 if (diff) {
2214 /* add into diff */
2215 LY_CHECK_RET(lyd_val_diff_add(node, LYD_DIFF_OP_CREATE, diff));
2216 }
2217 }
2218 }
2219 break;
2220 default:
2221 /* without defaults */
2222 break;
2223 }
2224 }
2225
2226 return LY_SUCCESS;
2227}
2228
2229API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02002230lyd_new_implicit_tree(struct lyd_node *tree, uint32_t implicit_options, struct lyd_node **diff)
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002231{
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002232 LY_ERR ret = LY_SUCCESS;
Michal Vasko3488ada2020-12-03 14:18:19 +01002233 struct lyd_node *node;
Radek Krejci4f2e3e52021-03-30 14:20:28 +02002234 struct ly_set node_when = {0}, node_exts = {0};
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002235
2236 LY_CHECK_ARG_RET(NULL, tree, LY_EINVAL);
2237 if (diff) {
2238 *diff = NULL;
2239 }
2240
Michal Vasko56daf732020-08-10 10:57:18 +02002241 LYD_TREE_DFS_BEGIN(tree, node) {
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002242 /* skip added default nodes */
Michal Vasko69730152020-10-09 16:30:07 +02002243 if (((node->flags & (LYD_DEFAULT | LYD_NEW)) != (LYD_DEFAULT | LYD_NEW)) &&
2244 (node->schema->nodetype & LYD_NODE_INNER)) {
Radek Krejci4f2e3e52021-03-30 14:20:28 +02002245 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 +01002246 NULL, implicit_options, diff), cleanup);
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002247 }
2248
Michal Vasko56daf732020-08-10 10:57:18 +02002249 LYD_TREE_DFS_END(tree, node);
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002250 }
2251
Michal Vasko3488ada2020-12-03 14:18:19 +01002252 /* resolve when and remove any invalid defaults */
Michal Vasko976ec432021-12-06 15:42:22 +01002253 LY_CHECK_GOTO(ret = lyd_validate_unres(&tree, NULL, &node_when, LYXP_IGNORE_WHEN, &node_exts, NULL, NULL, diff),
2254 cleanup);
Michal Vasko3488ada2020-12-03 14:18:19 +01002255
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002256cleanup:
Michal Vasko3488ada2020-12-03 14:18:19 +01002257 ly_set_erase(&node_when, NULL);
Radek Krejci4f2e3e52021-03-30 14:20:28 +02002258 ly_set_erase(&node_exts, NULL);
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002259 if (ret && diff) {
2260 lyd_free_all(*diff);
2261 *diff = NULL;
2262 }
2263 return ret;
2264}
2265
2266API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02002267lyd_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 +02002268{
2269 const struct lys_module *mod;
2270 struct lyd_node *d = NULL;
2271 uint32_t i = 0;
2272 LY_ERR ret = LY_SUCCESS;
2273
2274 LY_CHECK_ARG_RET(ctx, tree, *tree || ctx, LY_EINVAL);
Michal Vasko892f5bf2021-11-24 10:41:05 +01002275 LY_CHECK_CTX_EQUAL_RET(*tree ? LYD_CTX(*tree) : NULL, ctx, LY_EINVAL);
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002276 if (diff) {
2277 *diff = NULL;
2278 }
2279 if (!ctx) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02002280 ctx = LYD_CTX(*tree);
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002281 }
2282
2283 /* add nodes for each module one-by-one */
2284 while ((mod = ly_ctx_get_module_iter(ctx, &i))) {
2285 if (!mod->implemented) {
2286 continue;
2287 }
2288
2289 LY_CHECK_GOTO(ret = lyd_new_implicit_module(tree, mod, implicit_options, diff ? &d : NULL), cleanup);
2290 if (d) {
2291 /* merge into one diff */
2292 lyd_insert_sibling(*diff, d, diff);
2293
2294 d = NULL;
2295 }
2296 }
2297
2298cleanup:
2299 if (ret && diff) {
2300 lyd_free_all(*diff);
2301 *diff = NULL;
2302 }
2303 return ret;
2304}
2305
2306API LY_ERR
Michal Vasko892f5bf2021-11-24 10:41:05 +01002307lyd_new_implicit_module(struct lyd_node **tree, const struct lys_module *module, uint32_t implicit_options,
2308 struct lyd_node **diff)
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002309{
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002310 LY_ERR ret = LY_SUCCESS;
Michal Vasko3488ada2020-12-03 14:18:19 +01002311 struct lyd_node *root, *d = NULL;
Radek Krejci4f2e3e52021-03-30 14:20:28 +02002312 struct ly_set node_when = {0}, node_exts = {0};
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002313
2314 LY_CHECK_ARG_RET(NULL, tree, module, LY_EINVAL);
Michal Vasko892f5bf2021-11-24 10:41:05 +01002315 LY_CHECK_CTX_EQUAL_RET(*tree ? LYD_CTX(*tree) : NULL, module ? module->ctx : NULL, LY_EINVAL);
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002316 if (diff) {
2317 *diff = NULL;
2318 }
2319
2320 /* add all top-level defaults for this module */
Radek Krejci4f2e3e52021-03-30 14:20:28 +02002321 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 +01002322
2323 /* resolve when and remove any invalid defaults */
Michal Vasko976ec432021-12-06 15:42:22 +01002324 LY_CHECK_GOTO(ret = lyd_validate_unres(tree, module, &node_when, LYXP_IGNORE_WHEN, &node_exts, NULL, NULL, diff),
2325 cleanup);
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002326
2327 /* process nested nodes */
2328 LY_LIST_FOR(*tree, root) {
2329 /* skip added default nodes */
2330 if ((root->flags & (LYD_DEFAULT | LYD_NEW)) != (LYD_DEFAULT | LYD_NEW)) {
2331 LY_CHECK_GOTO(ret = lyd_new_implicit_tree(root, implicit_options, diff ? &d : NULL), cleanup);
2332
2333 if (d) {
2334 /* merge into one diff */
2335 lyd_insert_sibling(*diff, d, diff);
2336
2337 d = NULL;
2338 }
2339 }
2340 }
2341
2342cleanup:
Michal Vasko3488ada2020-12-03 14:18:19 +01002343 ly_set_erase(&node_when, NULL);
Radek Krejci4f2e3e52021-03-30 14:20:28 +02002344 ly_set_erase(&node_exts, NULL);
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002345 if (ret && diff) {
2346 lyd_free_all(*diff);
2347 *diff = NULL;
2348 }
2349 return ret;
2350}
2351
Michal Vasko90932a92020-02-12 14:33:03 +01002352struct lyd_node *
Michal Vaskob104f112020-07-17 09:54:54 +02002353lyd_insert_get_next_anchor(const struct lyd_node *first_sibling, const struct lyd_node *new_node)
Michal Vasko90932a92020-02-12 14:33:03 +01002354{
Michal Vaskob104f112020-07-17 09:54:54 +02002355 const struct lysc_node *schema, *sparent;
Michal Vasko90932a92020-02-12 14:33:03 +01002356 struct lyd_node *match = NULL;
Radek Krejci857189e2020-09-01 13:26:36 +02002357 ly_bool found;
Michal Vasko93b16062020-12-09 18:14:18 +01002358 uint32_t getnext_opts;
Michal Vasko90932a92020-02-12 14:33:03 +01002359
Michal Vaskob104f112020-07-17 09:54:54 +02002360 assert(new_node);
2361
2362 if (!first_sibling || !new_node->schema) {
2363 /* insert at the end, no next anchor */
Michal Vasko90932a92020-02-12 14:33:03 +01002364 return NULL;
2365 }
2366
Michal Vasko93b16062020-12-09 18:14:18 +01002367 getnext_opts = 0;
Michal Vaskod1e53b92021-01-28 13:11:06 +01002368 if (new_node->schema->flags & LYS_IS_OUTPUT) {
Michal Vasko93b16062020-12-09 18:14:18 +01002369 getnext_opts = LYS_GETNEXT_OUTPUT;
2370 }
2371
Michal Vaskoa2756f02021-07-09 13:20:28 +02002372 if (first_sibling->parent && first_sibling->parent->schema && first_sibling->parent->children_ht) {
Michal Vaskob104f112020-07-17 09:54:54 +02002373 /* find the anchor using hashes */
2374 sparent = first_sibling->parent->schema;
Michal Vasko93b16062020-12-09 18:14:18 +01002375 schema = lys_getnext(new_node->schema, sparent, NULL, getnext_opts);
Michal Vaskob104f112020-07-17 09:54:54 +02002376 while (schema) {
2377 /* keep trying to find the first existing instance of the closest following schema sibling,
2378 * otherwise return NULL - inserting at the end */
2379 if (!lyd_find_sibling_schema(first_sibling, schema, &match)) {
2380 break;
2381 }
2382
Michal Vasko93b16062020-12-09 18:14:18 +01002383 schema = lys_getnext(schema, sparent, NULL, getnext_opts);
Michal Vaskob104f112020-07-17 09:54:54 +02002384 }
2385 } else {
2386 /* find the anchor without hashes */
2387 match = (struct lyd_node *)first_sibling;
Michal Vasko3a708622021-07-15 14:20:10 +02002388 sparent = lysc_data_parent(new_node->schema);
2389 if (!sparent) {
Michal Vaskob104f112020-07-17 09:54:54 +02002390 /* we are in top-level, skip all the data from preceding modules */
2391 LY_LIST_FOR(match, match) {
2392 if (!match->schema || (strcmp(lyd_owner_module(match)->name, lyd_owner_module(new_node)->name) >= 0)) {
2393 break;
2394 }
2395 }
2396 }
2397
2398 /* get the first schema sibling */
Michal Vasko93b16062020-12-09 18:14:18 +01002399 schema = lys_getnext(NULL, sparent, new_node->schema->module->compiled, getnext_opts);
Michal Vaskob104f112020-07-17 09:54:54 +02002400
2401 found = 0;
2402 LY_LIST_FOR(match, match) {
2403 if (!match->schema || (lyd_owner_module(match) != lyd_owner_module(new_node))) {
2404 /* we have found an opaque node, which must be at the end, so use it OR
2405 * modules do not match, so we must have traversed all the data from new_node module (if any),
2406 * we have found the first node of the next module, that is what we want */
2407 break;
2408 }
2409
2410 /* skip schema nodes until we find the instantiated one */
2411 while (!found) {
2412 if (new_node->schema == schema) {
2413 /* we have found the schema of the new node, continue search to find the first
2414 * data node with a different schema (after our schema) */
2415 found = 1;
2416 break;
2417 }
2418 if (match->schema == schema) {
2419 /* current node (match) is a data node still before the new node, continue search in data */
2420 break;
2421 }
Michal Vasko93b16062020-12-09 18:14:18 +01002422 schema = lys_getnext(schema, sparent, new_node->schema->module->compiled, getnext_opts);
Michal Vaskob104f112020-07-17 09:54:54 +02002423 assert(schema);
2424 }
2425
2426 if (found && (match->schema != new_node->schema)) {
2427 /* find the next node after we have found our node schema data instance */
2428 break;
2429 }
2430 }
Michal Vasko90932a92020-02-12 14:33:03 +01002431 }
2432
2433 return match;
2434}
2435
2436/**
2437 * @brief Insert node after a sibling.
2438 *
Michal Vasko9f96a052020-03-10 09:41:45 +01002439 * Handles inserting into NP containers and key-less lists.
2440 *
Michal Vasko90932a92020-02-12 14:33:03 +01002441 * @param[in] sibling Sibling to insert after.
2442 * @param[in] node Node to insert.
2443 */
2444static void
Michal Vaskof03ed032020-03-04 13:31:44 +01002445lyd_insert_after_node(struct lyd_node *sibling, struct lyd_node *node)
Michal Vasko90932a92020-02-12 14:33:03 +01002446{
Michal Vasko0249f7c2020-03-05 16:36:40 +01002447 struct lyd_node_inner *par;
2448
Michal Vasko90932a92020-02-12 14:33:03 +01002449 assert(!node->next && (node->prev == node));
2450
2451 node->next = sibling->next;
2452 node->prev = sibling;
2453 sibling->next = node;
2454 if (node->next) {
2455 /* sibling had a succeeding node */
2456 node->next->prev = node;
2457 } else {
2458 /* sibling was last, find first sibling and change its prev */
2459 if (sibling->parent) {
2460 sibling = sibling->parent->child;
2461 } else {
Michal Vaskod989ba02020-08-24 10:59:24 +02002462 for ( ; sibling->prev->next != node; sibling = sibling->prev) {}
Michal Vasko90932a92020-02-12 14:33:03 +01002463 }
2464 sibling->prev = node;
2465 }
2466 node->parent = sibling->parent;
Michal Vasko0249f7c2020-03-05 16:36:40 +01002467
Michal Vasko9f96a052020-03-10 09:41:45 +01002468 for (par = node->parent; par; par = par->parent) {
2469 if ((par->flags & LYD_DEFAULT) && !(node->flags & LYD_DEFAULT)) {
2470 /* remove default flags from NP containers */
Michal Vasko0249f7c2020-03-05 16:36:40 +01002471 par->flags &= ~LYD_DEFAULT;
2472 }
2473 }
Michal Vasko90932a92020-02-12 14:33:03 +01002474}
2475
2476/**
2477 * @brief Insert node before a sibling.
2478 *
Michal Vasko9f96a052020-03-10 09:41:45 +01002479 * Handles inserting into NP containers and key-less lists.
2480 *
Michal Vasko90932a92020-02-12 14:33:03 +01002481 * @param[in] sibling Sibling to insert before.
2482 * @param[in] node Node to insert.
2483 */
2484static void
Michal Vaskof03ed032020-03-04 13:31:44 +01002485lyd_insert_before_node(struct lyd_node *sibling, struct lyd_node *node)
Michal Vasko90932a92020-02-12 14:33:03 +01002486{
Michal Vasko0249f7c2020-03-05 16:36:40 +01002487 struct lyd_node_inner *par;
2488
Michal Vasko90932a92020-02-12 14:33:03 +01002489 assert(!node->next && (node->prev == node));
2490
2491 node->next = sibling;
2492 /* covers situation of sibling being first */
2493 node->prev = sibling->prev;
2494 sibling->prev = node;
2495 if (node->prev->next) {
2496 /* sibling had a preceding node */
2497 node->prev->next = node;
2498 } else if (sibling->parent) {
2499 /* sibling was first and we must also change parent child pointer */
2500 sibling->parent->child = node;
2501 }
2502 node->parent = sibling->parent;
Michal Vasko0249f7c2020-03-05 16:36:40 +01002503
Michal Vasko9f96a052020-03-10 09:41:45 +01002504 for (par = node->parent; par; par = par->parent) {
2505 if ((par->flags & LYD_DEFAULT) && !(node->flags & LYD_DEFAULT)) {
2506 /* remove default flags from NP containers */
Michal Vasko0249f7c2020-03-05 16:36:40 +01002507 par->flags &= ~LYD_DEFAULT;
2508 }
2509 }
Michal Vasko90932a92020-02-12 14:33:03 +01002510}
2511
2512/**
Michal Vaskob104f112020-07-17 09:54:54 +02002513 * @brief Insert node as the first and only child of a parent.
Michal Vasko90932a92020-02-12 14:33:03 +01002514 *
Michal Vasko9f96a052020-03-10 09:41:45 +01002515 * Handles inserting into NP containers and key-less lists.
2516 *
Michal Vasko90932a92020-02-12 14:33:03 +01002517 * @param[in] parent Parent to insert into.
2518 * @param[in] node Node to insert.
2519 */
2520static void
Michal Vaskob104f112020-07-17 09:54:54 +02002521lyd_insert_only_child(struct lyd_node *parent, struct lyd_node *node)
Michal Vasko90932a92020-02-12 14:33:03 +01002522{
2523 struct lyd_node_inner *par;
2524
Radek Krejcia1c1e542020-09-29 16:06:52 +02002525 assert(parent && !lyd_child(parent) && !node->next && (node->prev == node));
Michal Vasko52927e22020-03-16 17:26:14 +01002526 assert(!parent->schema || (parent->schema->nodetype & LYD_NODE_INNER));
Michal Vasko90932a92020-02-12 14:33:03 +01002527
2528 par = (struct lyd_node_inner *)parent;
2529
Michal Vaskob104f112020-07-17 09:54:54 +02002530 par->child = node;
Michal Vasko90932a92020-02-12 14:33:03 +01002531 node->parent = par;
Michal Vasko0249f7c2020-03-05 16:36:40 +01002532
Michal Vaskod989ba02020-08-24 10:59:24 +02002533 for ( ; par; par = par->parent) {
Michal Vasko9f96a052020-03-10 09:41:45 +01002534 if ((par->flags & LYD_DEFAULT) && !(node->flags & LYD_DEFAULT)) {
2535 /* remove default flags from NP containers */
Michal Vasko0249f7c2020-03-05 16:36:40 +01002536 par->flags &= ~LYD_DEFAULT;
2537 }
2538 }
Michal Vasko751cb4d2020-07-14 12:25:28 +02002539}
Michal Vasko0249f7c2020-03-05 16:36:40 +01002540
Michal Vasko751cb4d2020-07-14 12:25:28 +02002541/**
2542 * @brief Learn whether a list instance has all the keys.
2543 *
2544 * @param[in] list List instance to check.
2545 * @return non-zero if all the keys were found,
2546 * @return 0 otherwise.
2547 */
2548static int
2549lyd_insert_has_keys(const struct lyd_node *list)
2550{
2551 const struct lyd_node *key;
2552 const struct lysc_node *skey = NULL;
2553
2554 assert(list->schema->nodetype == LYS_LIST);
Radek Krejcia1c1e542020-09-29 16:06:52 +02002555 key = lyd_child(list);
Michal Vasko751cb4d2020-07-14 12:25:28 +02002556 while ((skey = lys_getnext(skey, list->schema, NULL, 0)) && (skey->flags & LYS_KEY)) {
2557 if (!key || (key->schema != skey)) {
2558 /* key missing */
2559 return 0;
2560 }
2561
2562 key = key->next;
2563 }
2564
2565 /* all keys found */
2566 return 1;
Michal Vasko90932a92020-02-12 14:33:03 +01002567}
2568
2569void
Michal Vasko6ee6f432021-07-16 09:49:14 +02002570lyd_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 +01002571{
Michal Vaskob104f112020-07-17 09:54:54 +02002572 struct lyd_node *anchor, *first_sibling;
Michal Vasko90932a92020-02-12 14:33:03 +01002573
Michal Vaskob104f112020-07-17 09:54:54 +02002574 /* inserting list without its keys is not supported */
2575 assert((parent || first_sibling_p) && node && (node->hash || !node->schema));
Michal Vaskof756c942020-11-06 17:21:37 +01002576 assert(!parent || !parent->schema ||
2577 (parent->schema->nodetype & (LYS_CONTAINER | LYS_LIST | LYS_RPC | LYS_ACTION | LYS_NOTIF)));
Michal Vasko9b368d32020-02-14 13:53:31 +01002578
Michal Vaskob104f112020-07-17 09:54:54 +02002579 if (!parent && first_sibling_p && (*first_sibling_p) && (*first_sibling_p)->parent) {
Michal Vasko9e685082021-01-29 14:49:09 +01002580 parent = lyd_parent(*first_sibling_p);
Michal Vasko9b368d32020-02-14 13:53:31 +01002581 }
Michal Vasko90932a92020-02-12 14:33:03 +01002582
Michal Vaskob104f112020-07-17 09:54:54 +02002583 /* get first sibling */
Michal Vasko9e685082021-01-29 14:49:09 +01002584 first_sibling = parent ? lyd_child(parent) : *first_sibling_p;
Michal Vasko9f96a052020-03-10 09:41:45 +01002585
Michal Vasko6ee6f432021-07-16 09:49:14 +02002586 if (last) {
2587 /* no next anchor */
2588 anchor = NULL;
2589 } else {
2590 /* find the anchor, our next node, so we can insert before it */
2591 anchor = lyd_insert_get_next_anchor(first_sibling, node);
2592 }
2593
Michal Vaskob104f112020-07-17 09:54:54 +02002594 if (anchor) {
Michal Vasko6ee6f432021-07-16 09:49:14 +02002595 /* insert before the anchor */
Michal Vaskob104f112020-07-17 09:54:54 +02002596 lyd_insert_before_node(anchor, node);
Michal Vasko26123192020-11-09 21:02:34 +01002597 if (!parent && (*first_sibling_p == anchor)) {
2598 /* move first sibling */
2599 *first_sibling_p = node;
2600 }
Michal Vaskob104f112020-07-17 09:54:54 +02002601 } else if (first_sibling) {
Michal Vasko6ee6f432021-07-16 09:49:14 +02002602 /* insert as the last node */
Michal Vaskob104f112020-07-17 09:54:54 +02002603 lyd_insert_after_node(first_sibling->prev, node);
2604 } else if (parent) {
Michal Vasko6ee6f432021-07-16 09:49:14 +02002605 /* insert as the only child */
Michal Vaskob104f112020-07-17 09:54:54 +02002606 lyd_insert_only_child(parent, node);
Michal Vasko90932a92020-02-12 14:33:03 +01002607 } else {
Michal Vasko6ee6f432021-07-16 09:49:14 +02002608 /* insert as the only sibling */
Michal Vaskob104f112020-07-17 09:54:54 +02002609 *first_sibling_p = node;
2610 }
2611
2612 /* insert into parent HT */
2613 lyd_insert_hash(node);
2614
2615 /* finish hashes for our parent, if needed and possible */
Michal Vasko9e8de2d2020-09-01 08:17:10 +02002616 if (node->schema && (node->schema->flags & LYS_KEY) && parent && lyd_insert_has_keys(parent)) {
Michal Vaskob104f112020-07-17 09:54:54 +02002617 lyd_hash(parent);
2618
2619 /* now we can insert even the list into its parent HT */
2620 lyd_insert_hash(parent);
Michal Vasko90932a92020-02-12 14:33:03 +01002621 }
Michal Vasko90932a92020-02-12 14:33:03 +01002622}
2623
Michal Vasko717a4c32020-12-07 09:40:10 +01002624/**
2625 * @brief Check schema place of a node to be inserted.
2626 *
2627 * @param[in] parent Schema node of the parent data node.
2628 * @param[in] sibling Schema node of a sibling data node.
2629 * @param[in] schema Schema node if the data node to be inserted.
2630 * @return LY_SUCCESS on success.
2631 * @return LY_EINVAL if the place is invalid.
2632 */
Michal Vaskof03ed032020-03-04 13:31:44 +01002633static LY_ERR
Michal Vasko717a4c32020-12-07 09:40:10 +01002634lyd_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 +01002635{
2636 const struct lysc_node *par2;
2637
Michal Vasko62ed12d2020-05-21 10:08:25 +02002638 assert(!parent || !(parent->nodetype & (LYS_CASE | LYS_CHOICE)));
Michal Vasko717a4c32020-12-07 09:40:10 +01002639 assert(!sibling || !(sibling->nodetype & (LYS_CASE | LYS_CHOICE)));
2640 assert(!schema || !(schema->nodetype & (LYS_CASE | LYS_CHOICE)));
Michal Vaskof03ed032020-03-04 13:31:44 +01002641
Michal Vasko717a4c32020-12-07 09:40:10 +01002642 if (!schema || (!parent && !sibling)) {
Michal Vasko71e795e2020-12-04 16:27:37 +01002643 /* opaque nodes can be inserted wherever */
2644 return LY_SUCCESS;
2645 }
2646
Michal Vasko717a4c32020-12-07 09:40:10 +01002647 if (!parent) {
2648 parent = lysc_data_parent(sibling);
2649 }
2650
Michal Vaskof03ed032020-03-04 13:31:44 +01002651 /* find schema parent */
Michal Vasko62ed12d2020-05-21 10:08:25 +02002652 par2 = lysc_data_parent(schema);
Michal Vaskof03ed032020-03-04 13:31:44 +01002653
2654 if (parent) {
2655 /* inner node */
2656 if (par2 != parent) {
Michal Vaskob104f112020-07-17 09:54:54 +02002657 LOGERR(schema->module->ctx, LY_EINVAL, "Cannot insert, parent of \"%s\" is not \"%s\".", schema->name,
Michal Vasko69730152020-10-09 16:30:07 +02002658 parent->name);
Michal Vaskof03ed032020-03-04 13:31:44 +01002659 return LY_EINVAL;
2660 }
2661 } else {
2662 /* top-level node */
2663 if (par2) {
Radek Krejcif6d14cb2020-07-02 16:11:45 +02002664 LOGERR(schema->module->ctx, LY_EINVAL, "Cannot insert, node \"%s\" is not top-level.", schema->name);
Michal Vaskof03ed032020-03-04 13:31:44 +01002665 return LY_EINVAL;
2666 }
2667 }
2668
2669 return LY_SUCCESS;
2670}
2671
2672API LY_ERR
Michal Vaskob104f112020-07-17 09:54:54 +02002673lyd_insert_child(struct lyd_node *parent, struct lyd_node *node)
Michal Vaskof03ed032020-03-04 13:31:44 +01002674{
2675 struct lyd_node *iter;
2676
Michal Vasko0ab974d2021-02-24 13:18:26 +01002677 LY_CHECK_ARG_RET(NULL, parent, node, !parent->schema || (parent->schema->nodetype & LYD_NODE_INNER), LY_EINVAL);
Michal Vasko892f5bf2021-11-24 10:41:05 +01002678 LY_CHECK_CTX_EQUAL_RET(LYD_CTX(parent), LYD_CTX(node), LY_EINVAL);
Michal Vaskof03ed032020-03-04 13:31:44 +01002679
Michal Vasko717a4c32020-12-07 09:40:10 +01002680 LY_CHECK_RET(lyd_insert_check_schema(parent->schema, NULL, node->schema));
Michal Vaskof03ed032020-03-04 13:31:44 +01002681
Michal Vasko0ab974d2021-02-24 13:18:26 +01002682 if (node->schema && (node->schema->flags & LYS_KEY)) {
Michal Vaskof03ed032020-03-04 13:31:44 +01002683 LOGERR(parent->schema->module->ctx, LY_EINVAL, "Cannot insert key \"%s\".", node->schema->name);
2684 return LY_EINVAL;
2685 }
2686
2687 if (node->parent || node->prev->next) {
2688 lyd_unlink_tree(node);
2689 }
2690
2691 while (node) {
2692 iter = node->next;
2693 lyd_unlink_tree(node);
Michal Vasko6ee6f432021-07-16 09:49:14 +02002694 lyd_insert_node(parent, NULL, node, 0);
Michal Vaskof03ed032020-03-04 13:31:44 +01002695 node = iter;
2696 }
2697 return LY_SUCCESS;
2698}
2699
2700API LY_ERR
Michal Vaskob104f112020-07-17 09:54:54 +02002701lyd_insert_sibling(struct lyd_node *sibling, struct lyd_node *node, struct lyd_node **first)
Michal Vaskob1b5c262020-03-05 14:29:47 +01002702{
2703 struct lyd_node *iter;
2704
Michal Vaskob104f112020-07-17 09:54:54 +02002705 LY_CHECK_ARG_RET(NULL, node, LY_EINVAL);
Michal Vasko892f5bf2021-11-24 10:41:05 +01002706 LY_CHECK_CTX_EQUAL_RET(sibling ? LYD_CTX(sibling) : NULL, LYD_CTX(node), LY_EINVAL);
Michal Vaskob1b5c262020-03-05 14:29:47 +01002707
Michal Vaskob104f112020-07-17 09:54:54 +02002708 if (sibling) {
Michal Vasko717a4c32020-12-07 09:40:10 +01002709 LY_CHECK_RET(lyd_insert_check_schema(NULL, sibling->schema, node->schema));
Michal Vaskob1b5c262020-03-05 14:29:47 +01002710 }
2711
Michal Vasko553d0b52020-12-04 16:27:52 +01002712 if (sibling == node) {
2713 /* we need to keep the connection to siblings so we can insert into them */
2714 sibling = sibling->prev;
2715 }
2716
Michal Vaskob1b5c262020-03-05 14:29:47 +01002717 if (node->parent || node->prev->next) {
2718 lyd_unlink_tree(node);
2719 }
2720
2721 while (node) {
Michal Vasko71e795e2020-12-04 16:27:37 +01002722 if (lysc_is_key(node->schema)) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02002723 LOGERR(LYD_CTX(node), LY_EINVAL, "Cannot insert key \"%s\".", node->schema->name);
Michal Vaskob104f112020-07-17 09:54:54 +02002724 return LY_EINVAL;
2725 }
2726
Michal Vaskob1b5c262020-03-05 14:29:47 +01002727 iter = node->next;
2728 lyd_unlink_tree(node);
Michal Vasko6ee6f432021-07-16 09:49:14 +02002729 lyd_insert_node(NULL, &sibling, node, 0);
Michal Vaskob1b5c262020-03-05 14:29:47 +01002730 node = iter;
2731 }
Michal Vaskob1b5c262020-03-05 14:29:47 +01002732
Michal Vaskob104f112020-07-17 09:54:54 +02002733 if (first) {
2734 /* find the first sibling */
2735 *first = sibling;
2736 while ((*first)->prev->next) {
2737 *first = (*first)->prev;
Michal Vasko0249f7c2020-03-05 16:36:40 +01002738 }
2739 }
2740
2741 return LY_SUCCESS;
2742}
2743
Michal Vaskob1b5c262020-03-05 14:29:47 +01002744API LY_ERR
Michal Vaskof03ed032020-03-04 13:31:44 +01002745lyd_insert_before(struct lyd_node *sibling, struct lyd_node *node)
2746{
Michal Vaskof03ed032020-03-04 13:31:44 +01002747 LY_CHECK_ARG_RET(NULL, sibling, node, LY_EINVAL);
Michal Vasko892f5bf2021-11-24 10:41:05 +01002748 LY_CHECK_CTX_EQUAL_RET(LYD_CTX(sibling), LYD_CTX(node), LY_EINVAL);
Michal Vaskof03ed032020-03-04 13:31:44 +01002749
Michal Vasko717a4c32020-12-07 09:40:10 +01002750 LY_CHECK_RET(lyd_insert_check_schema(NULL, sibling->schema, node->schema));
Michal Vaskof03ed032020-03-04 13:31:44 +01002751
Michal Vaskob104f112020-07-17 09:54:54 +02002752 if (!(node->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) || !(node->schema->flags & LYS_ORDBY_USER)) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02002753 LOGERR(LYD_CTX(sibling), LY_EINVAL, "Can be used only for user-ordered nodes.");
Michal Vaskof03ed032020-03-04 13:31:44 +01002754 return LY_EINVAL;
2755 }
2756
Michal Vasko4bc2ce32020-12-08 10:06:16 +01002757 lyd_unlink_tree(node);
2758 lyd_insert_before_node(sibling, node);
2759 lyd_insert_hash(node);
Michal Vaskof03ed032020-03-04 13:31:44 +01002760
Michal Vaskof03ed032020-03-04 13:31:44 +01002761 return LY_SUCCESS;
2762}
2763
2764API LY_ERR
2765lyd_insert_after(struct lyd_node *sibling, struct lyd_node *node)
2766{
Michal Vaskof03ed032020-03-04 13:31:44 +01002767 LY_CHECK_ARG_RET(NULL, sibling, node, LY_EINVAL);
Michal Vasko892f5bf2021-11-24 10:41:05 +01002768 LY_CHECK_CTX_EQUAL_RET(LYD_CTX(sibling), LYD_CTX(node), LY_EINVAL);
Michal Vaskof03ed032020-03-04 13:31:44 +01002769
Michal Vasko717a4c32020-12-07 09:40:10 +01002770 LY_CHECK_RET(lyd_insert_check_schema(NULL, sibling->schema, node->schema));
Michal Vaskof03ed032020-03-04 13:31:44 +01002771
Michal Vaskob104f112020-07-17 09:54:54 +02002772 if (!(node->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) || !(node->schema->flags & LYS_ORDBY_USER)) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02002773 LOGERR(LYD_CTX(sibling), LY_EINVAL, "Can be used only for user-ordered nodes.");
Michal Vaskof03ed032020-03-04 13:31:44 +01002774 return LY_EINVAL;
2775 }
2776
Michal Vasko4bc2ce32020-12-08 10:06:16 +01002777 lyd_unlink_tree(node);
2778 lyd_insert_after_node(sibling, node);
2779 lyd_insert_hash(node);
Michal Vaskof03ed032020-03-04 13:31:44 +01002780
Michal Vaskof03ed032020-03-04 13:31:44 +01002781 return LY_SUCCESS;
2782}
2783
2784API void
Michal Vasko66d508c2021-07-21 16:07:09 +02002785lyd_unlink_siblings(struct lyd_node *node)
2786{
2787 struct lyd_node *next, *elem, *first = NULL;
2788
2789 LY_LIST_FOR_SAFE(node, next, elem) {
2790 lyd_unlink_tree(elem);
2791 lyd_insert_node(NULL, &first, elem, 1);
2792 }
2793}
2794
2795API void
Michal Vaskof03ed032020-03-04 13:31:44 +01002796lyd_unlink_tree(struct lyd_node *node)
2797{
2798 struct lyd_node *iter;
2799
2800 if (!node) {
2801 return;
2802 }
2803
Michal Vaskob104f112020-07-17 09:54:54 +02002804 /* update hashes while still linked into the tree */
2805 lyd_unlink_hash(node);
2806
Michal Vaskof03ed032020-03-04 13:31:44 +01002807 /* unlink from siblings */
2808 if (node->prev->next) {
2809 node->prev->next = node->next;
2810 }
2811 if (node->next) {
2812 node->next->prev = node->prev;
2813 } else {
2814 /* unlinking the last node */
2815 if (node->parent) {
2816 iter = node->parent->child;
2817 } else {
2818 iter = node->prev;
2819 while (iter->prev != node) {
2820 iter = iter->prev;
2821 }
2822 }
2823 /* update the "last" pointer from the first node */
2824 iter->prev = node->prev;
2825 }
2826
2827 /* unlink from parent */
2828 if (node->parent) {
2829 if (node->parent->child == node) {
2830 /* the node is the first child */
2831 node->parent->child = node->next;
2832 }
2833
Michal Vaskoab49dbe2020-07-17 12:32:47 +02002834 /* check for NP container whether its last non-default node is not being unlinked */
Michal Vasko69730152020-10-09 16:30:07 +02002835 if (node->parent->schema && (node->parent->schema->nodetype == LYS_CONTAINER) &&
2836 !(node->parent->flags & LYD_DEFAULT) && !(node->parent->schema->flags & LYS_PRESENCE)) {
Michal Vaskoab49dbe2020-07-17 12:32:47 +02002837 LY_LIST_FOR(node->parent->child, iter) {
2838 if ((iter != node) && !(iter->flags & LYD_DEFAULT)) {
2839 break;
2840 }
2841 }
2842 if (!iter) {
2843 node->parent->flags |= LYD_DEFAULT;
2844 }
2845 }
2846
Michal Vaskof03ed032020-03-04 13:31:44 +01002847 node->parent = NULL;
2848 }
2849
2850 node->next = NULL;
2851 node->prev = node;
2852}
2853
Michal Vaskoa5da3292020-08-12 13:10:50 +02002854void
Michal Vasko871a0252020-11-11 18:35:24 +01002855lyd_insert_meta(struct lyd_node *parent, struct lyd_meta *meta, ly_bool clear_dflt)
Radek Krejci1798aae2020-07-14 13:26:06 +02002856{
2857 struct lyd_meta *last, *iter;
2858
2859 assert(parent);
Michal Vaskoa5da3292020-08-12 13:10:50 +02002860
2861 if (!meta) {
2862 return;
2863 }
Radek Krejci1798aae2020-07-14 13:26:06 +02002864
2865 for (iter = meta; iter; iter = iter->next) {
2866 iter->parent = parent;
2867 }
2868
2869 /* insert as the last attribute */
2870 if (parent->meta) {
Radek Krejci1e008d22020-08-17 11:37:37 +02002871 for (last = parent->meta; last->next; last = last->next) {}
Radek Krejci1798aae2020-07-14 13:26:06 +02002872 last->next = meta;
2873 } else {
2874 parent->meta = meta;
2875 }
2876
2877 /* remove default flags from NP containers */
Michal Vasko871a0252020-11-11 18:35:24 +01002878 while (clear_dflt && parent && (parent->schema->nodetype == LYS_CONTAINER) && (parent->flags & LYD_DEFAULT)) {
Radek Krejci1798aae2020-07-14 13:26:06 +02002879 parent->flags &= ~LYD_DEFAULT;
Michal Vasko9e685082021-01-29 14:49:09 +01002880 parent = lyd_parent(parent);
Radek Krejci1798aae2020-07-14 13:26:06 +02002881 }
Radek Krejci1798aae2020-07-14 13:26:06 +02002882}
2883
2884LY_ERR
Michal Vasko9f96a052020-03-10 09:41:45 +01002885lyd_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 +02002886 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 +01002887 void *prefix_data, uint32_t hints, ly_bool clear_dflt, ly_bool *incomplete)
Michal Vasko90932a92020-02-12 14:33:03 +01002888{
Radek Krejci2efc45b2020-12-22 16:25:44 +01002889 LY_ERR ret = LY_SUCCESS;
Michal Vasko90932a92020-02-12 14:33:03 +01002890 struct lysc_ext_instance *ant = NULL;
Michal Vaskob0534f92021-07-01 13:24:03 +02002891 const struct lysc_type **ant_type;
Michal Vasko9f96a052020-03-10 09:41:45 +01002892 struct lyd_meta *mt, *last;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002893 LY_ARRAY_COUNT_TYPE u;
Michal Vasko90932a92020-02-12 14:33:03 +01002894
Michal Vasko9f96a052020-03-10 09:41:45 +01002895 assert((parent || meta) && mod);
Michal Vasko6f4cbb62020-02-28 11:15:47 +01002896
Radek Krejciddace2c2021-01-08 11:30:56 +01002897 LOG_LOCSET(parent ? parent->schema : NULL, parent, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +01002898
Radek Krejci7eb54ba2020-05-18 16:30:04 +02002899 LY_ARRAY_FOR(mod->compiled->exts, u) {
Radek Krejci3e6632f2021-03-22 22:08:21 +01002900 if ((mod->compiled->exts[u].def->plugin == lyplg_find(LYPLG_EXTENSION, LYEXT_PLUGIN_INTERNAL_ANNOTATION)) &&
Radek Krejci7eb54ba2020-05-18 16:30:04 +02002901 !ly_strncmp(mod->compiled->exts[u].argument, name, name_len)) {
Michal Vasko90932a92020-02-12 14:33:03 +01002902 /* we have the annotation definition */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02002903 ant = &mod->compiled->exts[u];
Michal Vasko90932a92020-02-12 14:33:03 +01002904 break;
2905 }
2906 }
2907 if (!ant) {
2908 /* attribute is not defined as a metadata annotation (RFC 7952) */
Radek Krejci2efc45b2020-12-22 16:25:44 +01002909 LOGVAL(mod->ctx, LYVE_REFERENCE, "Annotation definition for attribute \"%s:%.*s\" not found.",
Radek Krejci422afb12021-03-04 16:38:16 +01002910 mod->name, (int)name_len, name);
Radek Krejci2efc45b2020-12-22 16:25:44 +01002911 ret = LY_EINVAL;
2912 goto cleanup;
Michal Vasko90932a92020-02-12 14:33:03 +01002913 }
2914
Michal Vasko9f96a052020-03-10 09:41:45 +01002915 mt = calloc(1, sizeof *mt);
Radek Krejci2efc45b2020-12-22 16:25:44 +01002916 LY_CHECK_ERR_GOTO(!mt, LOGMEM(mod->ctx); ret = LY_EMEM, cleanup);
Michal Vasko9f96a052020-03-10 09:41:45 +01002917 mt->parent = parent;
2918 mt->annotation = ant;
Michal Vaskob0534f92021-07-01 13:24:03 +02002919 ant_type = ant->substmts[ANNOTATION_SUBSTMT_TYPE].storage;
2920 ret = lyd_value_store(mod->ctx, &mt->value, *ant_type, value, value_len, dynamic, format, prefix_data, hints,
2921 parent ? parent->schema : NULL, incomplete);
Radek Krejci2efc45b2020-12-22 16:25:44 +01002922 LY_CHECK_ERR_GOTO(ret, free(mt), cleanup);
2923 ret = lydict_insert(mod->ctx, name, name_len, &mt->name);
2924 LY_CHECK_ERR_GOTO(ret, free(mt), cleanup);
Michal Vasko90932a92020-02-12 14:33:03 +01002925
Michal Vasko6f4cbb62020-02-28 11:15:47 +01002926 /* insert as the last attribute */
2927 if (parent) {
Michal Vasko871a0252020-11-11 18:35:24 +01002928 lyd_insert_meta(parent, mt, clear_dflt);
Michal Vasko9f96a052020-03-10 09:41:45 +01002929 } else if (*meta) {
Radek Krejci1e008d22020-08-17 11:37:37 +02002930 for (last = *meta; last->next; last = last->next) {}
Michal Vasko9f96a052020-03-10 09:41:45 +01002931 last->next = mt;
Michal Vasko90932a92020-02-12 14:33:03 +01002932 }
2933
Michal Vasko9f96a052020-03-10 09:41:45 +01002934 if (meta) {
2935 *meta = mt;
Michal Vasko90932a92020-02-12 14:33:03 +01002936 }
Radek Krejci2efc45b2020-12-22 16:25:44 +01002937
2938cleanup:
Radek Krejciddace2c2021-01-08 11:30:56 +01002939 LOG_LOCBACK((parent && parent->schema) ? 1 : 0, parent ? 1 : 0, 0, 0);
Radek Krejci2efc45b2020-12-22 16:25:44 +01002940 return ret;
Michal Vasko90932a92020-02-12 14:33:03 +01002941}
2942
Michal Vaskoa5da3292020-08-12 13:10:50 +02002943void
2944lyd_insert_attr(struct lyd_node *parent, struct lyd_attr *attr)
2945{
2946 struct lyd_attr *last, *iter;
2947 struct lyd_node_opaq *opaq;
2948
2949 assert(parent && !parent->schema);
2950
2951 if (!attr) {
2952 return;
2953 }
2954
2955 opaq = (struct lyd_node_opaq *)parent;
2956 for (iter = attr; iter; iter = iter->next) {
2957 iter->parent = opaq;
2958 }
2959
2960 /* insert as the last attribute */
2961 if (opaq->attr) {
Radek Krejci1e008d22020-08-17 11:37:37 +02002962 for (last = opaq->attr; last->next; last = last->next) {}
Michal Vaskoa5da3292020-08-12 13:10:50 +02002963 last->next = attr;
2964 } else {
2965 opaq->attr = attr;
2966 }
2967}
2968
Michal Vasko52927e22020-03-16 17:26:14 +01002969LY_ERR
Michal Vaskofeca4fb2020-10-05 08:58:40 +02002970lyd_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 +01002971 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 +02002972 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 +01002973{
Radek Krejci011e4aa2020-09-04 15:22:31 +02002974 LY_ERR ret = LY_SUCCESS;
Radek Krejci1798aae2020-07-14 13:26:06 +02002975 struct lyd_attr *at, *last;
Michal Vasko52927e22020-03-16 17:26:14 +01002976
2977 assert(ctx && (parent || attr) && (!parent || !parent->schema));
Michal Vaskofeca4fb2020-10-05 08:58:40 +02002978 assert(name && name_len && format);
Michal Vasko52927e22020-03-16 17:26:14 +01002979
Michal Vasko2a3722d2021-06-16 11:52:39 +02002980 if (!value_len && (!dynamic || !*dynamic)) {
Michal Vasko52927e22020-03-16 17:26:14 +01002981 value = "";
2982 }
2983
2984 at = calloc(1, sizeof *at);
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01002985 LY_CHECK_ERR_RET(!at, LOGMEM(ctx); ly_free_prefix_data(format, val_prefix_data), LY_EMEM);
Radek Krejcid46e46a2020-09-15 14:22:42 +02002986
Michal Vaskoad92b672020-11-12 13:11:31 +01002987 LY_CHECK_GOTO(ret = lydict_insert(ctx, name, name_len, &at->name.name), finish);
Michal Vasko501af032020-11-11 20:27:44 +01002988 if (prefix_len) {
Michal Vaskoad92b672020-11-12 13:11:31 +01002989 LY_CHECK_GOTO(ret = lydict_insert(ctx, prefix, prefix_len, &at->name.prefix), finish);
Michal Vasko501af032020-11-11 20:27:44 +01002990 }
2991 if (module_key_len) {
Michal Vaskoad92b672020-11-12 13:11:31 +01002992 LY_CHECK_GOTO(ret = lydict_insert(ctx, module_key, module_key_len, &at->name.module_ns), finish);
Michal Vasko501af032020-11-11 20:27:44 +01002993 }
2994
Michal Vasko52927e22020-03-16 17:26:14 +01002995 if (dynamic && *dynamic) {
Radek Krejci011e4aa2020-09-04 15:22:31 +02002996 ret = lydict_insert_zc(ctx, (char *)value, &at->value);
2997 LY_CHECK_GOTO(ret, finish);
Michal Vasko52927e22020-03-16 17:26:14 +01002998 *dynamic = 0;
2999 } else {
Radek Krejci011e4aa2020-09-04 15:22:31 +02003000 LY_CHECK_GOTO(ret = lydict_insert(ctx, value, value_len, &at->value), finish);
Michal Vasko52927e22020-03-16 17:26:14 +01003001 }
Michal Vasko501af032020-11-11 20:27:44 +01003002 at->format = format;
3003 at->val_prefix_data = val_prefix_data;
3004 at->hints = hints;
Michal Vasko52927e22020-03-16 17:26:14 +01003005
3006 /* insert as the last attribute */
3007 if (parent) {
Michal Vaskoa5da3292020-08-12 13:10:50 +02003008 lyd_insert_attr(parent, at);
Michal Vasko52927e22020-03-16 17:26:14 +01003009 } else if (*attr) {
Radek Krejci1e008d22020-08-17 11:37:37 +02003010 for (last = *attr; last->next; last = last->next) {}
Michal Vasko52927e22020-03-16 17:26:14 +01003011 last->next = at;
3012 }
3013
Radek Krejci011e4aa2020-09-04 15:22:31 +02003014finish:
3015 if (ret) {
3016 lyd_free_attr_single(ctx, at);
3017 } else if (attr) {
Michal Vasko52927e22020-03-16 17:26:14 +01003018 *attr = at;
3019 }
3020 return LY_SUCCESS;
3021}
3022
Radek Krejci084289f2019-07-09 17:35:30 +02003023API const struct lyd_node_term *
Michal Vasko004d3152020-06-11 19:59:22 +02003024lyd_target(const struct ly_path *path, const struct lyd_node *tree)
Radek Krejci084289f2019-07-09 17:35:30 +02003025{
Michal Vasko004d3152020-06-11 19:59:22 +02003026 struct lyd_node *target;
Radek Krejci084289f2019-07-09 17:35:30 +02003027
Michal Vasko004d3152020-06-11 19:59:22 +02003028 if (ly_path_eval(path, tree, &target)) {
3029 return NULL;
Radek Krejci084289f2019-07-09 17:35:30 +02003030 }
3031
Michal Vasko004d3152020-06-11 19:59:22 +02003032 return (struct lyd_node_term *)target;
Radek Krejci084289f2019-07-09 17:35:30 +02003033}
3034
aPiecek2f63f952021-03-30 12:22:18 +02003035/**
3036 * @brief Check the equality of the two schemas from different contexts.
3037 *
3038 * @param schema1 of first node.
3039 * @param schema2 of second node.
3040 * @return 1 if the schemas are equal otherwise 0.
3041 */
3042static ly_bool
3043lyd_compare_schema_equal(const struct lysc_node *schema1, const struct lysc_node *schema2)
3044{
3045 if (!schema1 && !schema2) {
3046 return 1;
3047 } else if (!schema1 || !schema2) {
3048 return 0;
3049 }
3050
3051 assert(schema1->module->ctx != schema2->module->ctx);
3052
3053 if (schema1->nodetype != schema2->nodetype) {
3054 return 0;
3055 }
3056
3057 if (strcmp(schema1->name, schema2->name)) {
3058 return 0;
3059 }
3060
3061 if (strcmp(schema1->module->name, schema2->module->name)) {
3062 return 0;
3063 }
3064
3065 if (schema1->module->revision || schema2->module->revision) {
3066 if (!schema1->module->revision || !schema2->module->revision) {
3067 return 0;
3068 }
3069 if (strcmp(schema1->module->revision, schema2->module->revision)) {
3070 return 0;
3071 }
3072 }
3073
3074 return 1;
3075}
3076
3077/**
3078 * @brief Check the equality of the schemas for all parent nodes.
3079 *
3080 * Both nodes must be from different contexts.
3081 *
3082 * @param node1 Data of first node.
3083 * @param node2 Data of second node.
3084 * @return 1 if the all related parental schemas are equal otherwise 0.
3085 */
3086static ly_bool
3087lyd_compare_schema_parents_equal(const struct lyd_node *node1, const struct lyd_node *node2)
3088{
3089 const struct lysc_node *parent1, *parent2;
3090
3091 assert(node1 && node2);
3092
3093 for (parent1 = node1->schema->parent, parent2 = node2->schema->parent;
3094 parent1 && parent2;
3095 parent1 = parent1->parent, parent2 = parent2->parent) {
3096 if (!lyd_compare_schema_equal(parent1, parent2)) {
3097 return 0;
3098 }
3099 }
3100
3101 if (parent1 || parent2) {
3102 return 0;
3103 }
3104
3105 return 1;
3106}
3107
3108/**
3109 * @brief Internal implementation of @ref lyd_compare_single.
3110 * @copydoc lyd_compare_single
3111 * @param[in] parental_schemas_checked Flag used for optimization.
3112 * When this function is called for the first time, the flag must be set to 0.
3113 * The @ref lyd_compare_schema_parents_equal should be called only once during
3114 * recursive calls, and this is accomplished by setting to 1 in the lyd_compare_single_ body.
3115 */
3116static LY_ERR
3117lyd_compare_single_(const struct lyd_node *node1, const struct lyd_node *node2,
3118 uint32_t options, ly_bool parental_schemas_checked)
Radek Krejci1f05b6a2019-07-18 16:15:06 +02003119{
3120 const struct lyd_node *iter1, *iter2;
3121 struct lyd_node_term *term1, *term2;
3122 struct lyd_node_any *any1, *any2;
Michal Vasko52927e22020-03-16 17:26:14 +01003123 struct lyd_node_opaq *opaq1, *opaq2;
Radek Krejci1f05b6a2019-07-18 16:15:06 +02003124 size_t len1, len2;
Radek Krejci084289f2019-07-09 17:35:30 +02003125
Radek Krejci1f05b6a2019-07-18 16:15:06 +02003126 if (!node1 || !node2) {
3127 if (node1 == node2) {
3128 return LY_SUCCESS;
3129 } else {
3130 return LY_ENOT;
3131 }
3132 }
3133
aPiecek2f63f952021-03-30 12:22:18 +02003134 if (LYD_CTX(node1) == LYD_CTX(node2)) {
3135 /* same contexts */
3136 if (node1->schema != node2->schema) {
3137 return LY_ENOT;
3138 }
3139 } else {
3140 /* different contexts */
3141 if (!lyd_compare_schema_equal(node1->schema, node2->schema)) {
3142 return LY_ENOT;
3143 }
3144 if (!parental_schemas_checked) {
3145 if (!lyd_compare_schema_parents_equal(node1, node2)) {
3146 return LY_ENOT;
3147 }
3148 parental_schemas_checked = 1;
3149 }
Radek Krejci1f05b6a2019-07-18 16:15:06 +02003150 }
3151
3152 if (node1->hash != node2->hash) {
3153 return LY_ENOT;
3154 }
aPiecek2f63f952021-03-30 12:22:18 +02003155 /* 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 +02003156
Michal Vasko52927e22020-03-16 17:26:14 +01003157 if (!node1->schema) {
3158 opaq1 = (struct lyd_node_opaq *)node1;
3159 opaq2 = (struct lyd_node_opaq *)node2;
aPiecek2f63f952021-03-30 12:22:18 +02003160 if ((strcmp(opaq1->name.name, opaq2->name.name)) || (opaq1->format != opaq2->format) ||
3161 (strcmp(opaq1->name.module_ns, opaq2->name.module_ns))) {
Radek Krejci1f05b6a2019-07-18 16:15:06 +02003162 return LY_ENOT;
3163 }
Michal Vasko52927e22020-03-16 17:26:14 +01003164 switch (opaq1->format) {
Radek Krejci8df109d2021-04-23 12:19:08 +02003165 case LY_VALUE_XML:
Radek Krejci09c77442021-04-26 11:10:34 +02003166 if (lyxml_value_compare(LYD_CTX(node1), opaq1->value, opaq1->val_prefix_data, LYD_CTX(node2), opaq2->value,
3167 opaq2->val_prefix_data)) {
Michal Vasko52927e22020-03-16 17:26:14 +01003168 return LY_ENOT;
3169 }
3170 break;
Radek Krejci8df109d2021-04-23 12:19:08 +02003171 case LY_VALUE_JSON:
Radek Krejci1798aae2020-07-14 13:26:06 +02003172 /* prefixes in JSON are unique, so it is not necessary to canonize the values */
3173 if (strcmp(opaq1->value, opaq2->value)) {
3174 return LY_ENOT;
3175 }
3176 break;
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01003177 default:
Michal Vasko52927e22020-03-16 17:26:14 +01003178 /* not allowed */
Michal Vaskob7be7a82020-08-20 09:09:04 +02003179 LOGINT(LYD_CTX(node1));
Michal Vasko52927e22020-03-16 17:26:14 +01003180 return LY_EINT;
3181 }
3182 if (options & LYD_COMPARE_FULL_RECURSION) {
3183 iter1 = opaq1->child;
3184 iter2 = opaq2->child;
Radek Krejci1f05b6a2019-07-18 16:15:06 +02003185 goto all_children_compare;
Michal Vasko52927e22020-03-16 17:26:14 +01003186 }
3187 return LY_SUCCESS;
3188 } else {
3189 switch (node1->schema->nodetype) {
3190 case LYS_LEAF:
3191 case LYS_LEAFLIST:
3192 if (options & LYD_COMPARE_DEFAULTS) {
3193 if ((node1->flags & LYD_DEFAULT) != (node2->flags & LYD_DEFAULT)) {
3194 return LY_ENOT;
3195 }
3196 }
3197
Michal Vasko1a2bf2e2020-06-16 13:07:48 +02003198 term1 = (struct lyd_node_term *)node1;
3199 term2 = (struct lyd_node_term *)node2;
aPiecek2f63f952021-03-30 12:22:18 +02003200
3201 /* same contexts */
3202 if (LYD_CTX(node1) == LYD_CTX(node2)) {
3203 return term1->value.realtype->plugin->compare(&term1->value, &term2->value);
3204 }
3205
3206 /* different contexts */
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02003207 if (strcmp(lyd_get_value(node1), lyd_get_value(node2))) {
aPiecek2f63f952021-03-30 12:22:18 +02003208 return LY_ENOT;
3209 }
3210 return LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +01003211 case LYS_CONTAINER:
3212 if (options & LYD_COMPARE_DEFAULTS) {
3213 if ((node1->flags & LYD_DEFAULT) != (node2->flags & LYD_DEFAULT)) {
3214 return LY_ENOT;
3215 }
3216 }
3217 if (options & LYD_COMPARE_FULL_RECURSION) {
Michal Vasko9e685082021-01-29 14:49:09 +01003218 iter1 = lyd_child(node1);
3219 iter2 = lyd_child(node2);
Michal Vasko52927e22020-03-16 17:26:14 +01003220 goto all_children_compare;
Radek Krejci1f05b6a2019-07-18 16:15:06 +02003221 }
3222 return LY_SUCCESS;
Michal Vasko1bf09392020-03-27 12:38:10 +01003223 case LYS_RPC:
Michal Vasko52927e22020-03-16 17:26:14 +01003224 case LYS_ACTION:
3225 if (options & LYD_COMPARE_FULL_RECURSION) {
3226 /* TODO action/RPC
3227 goto all_children_compare;
3228 */
Radek Krejci1f05b6a2019-07-18 16:15:06 +02003229 }
3230 return LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +01003231 case LYS_NOTIF:
3232 if (options & LYD_COMPARE_FULL_RECURSION) {
3233 /* TODO Notification
3234 goto all_children_compare;
3235 */
3236 }
3237 return LY_SUCCESS;
3238 case LYS_LIST:
Michal Vasko9e685082021-01-29 14:49:09 +01003239 iter1 = lyd_child(node1);
3240 iter2 = lyd_child(node2);
Michal Vasko52927e22020-03-16 17:26:14 +01003241
3242 if (!(node1->schema->flags & LYS_KEYLESS) && !(options & LYD_COMPARE_FULL_RECURSION)) {
3243 /* lists with keys, their equivalence is based on their keys */
Michal Vasko544e58a2021-01-28 14:33:41 +01003244 for (const struct lysc_node *key = lysc_node_child(node1->schema);
Michal Vaskoc57d9a92020-06-23 13:28:27 +02003245 key && (key->flags & LYS_KEY);
Michal Vasko52927e22020-03-16 17:26:14 +01003246 key = key->next) {
aPiecek2f63f952021-03-30 12:22:18 +02003247 if (lyd_compare_single_(iter1, iter2, options, parental_schemas_checked)) {
Michal Vasko52927e22020-03-16 17:26:14 +01003248 return LY_ENOT;
3249 }
3250 iter1 = iter1->next;
3251 iter2 = iter2->next;
3252 }
3253 } else {
3254 /* lists without keys, their equivalence is based on equivalence of all the children (both direct and indirect) */
3255
Radek Krejci0f969882020-08-21 16:56:47 +02003256all_children_compare:
Michal Vasko52927e22020-03-16 17:26:14 +01003257 if (!iter1 && !iter2) {
3258 /* no children, nothing to compare */
3259 return LY_SUCCESS;
3260 }
3261
Michal Vaskod989ba02020-08-24 10:59:24 +02003262 for ( ; iter1 && iter2; iter1 = iter1->next, iter2 = iter2->next) {
aPiecek2f63f952021-03-30 12:22:18 +02003263 if (lyd_compare_single_(iter1, iter2, options | LYD_COMPARE_FULL_RECURSION, parental_schemas_checked)) {
Michal Vasko52927e22020-03-16 17:26:14 +01003264 return LY_ENOT;
3265 }
3266 }
3267 if (iter1 || iter2) {
3268 return LY_ENOT;
3269 }
3270 }
3271 return LY_SUCCESS;
3272 case LYS_ANYXML:
3273 case LYS_ANYDATA:
Michal Vasko22df3f02020-08-24 13:29:22 +02003274 any1 = (struct lyd_node_any *)node1;
3275 any2 = (struct lyd_node_any *)node2;
Michal Vasko52927e22020-03-16 17:26:14 +01003276
3277 if (any1->value_type != any2->value_type) {
3278 return LY_ENOT;
3279 }
3280 switch (any1->value_type) {
3281 case LYD_ANYDATA_DATATREE:
3282 iter1 = any1->value.tree;
3283 iter2 = any2->value.tree;
3284 goto all_children_compare;
3285 case LYD_ANYDATA_STRING:
3286 case LYD_ANYDATA_XML:
3287 case LYD_ANYDATA_JSON:
3288 len1 = strlen(any1->value.str);
3289 len2 = strlen(any2->value.str);
Michal Vasko69730152020-10-09 16:30:07 +02003290 if ((len1 != len2) || strcmp(any1->value.str, any2->value.str)) {
Michal Vasko52927e22020-03-16 17:26:14 +01003291 return LY_ENOT;
3292 }
3293 return LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +01003294 case LYD_ANYDATA_LYB:
Michal Vasko60ea6352020-06-29 13:39:39 +02003295 len1 = lyd_lyb_data_length(any1->value.mem);
3296 len2 = lyd_lyb_data_length(any2->value.mem);
Michal Vasko69730152020-10-09 16:30:07 +02003297 if ((len1 != len2) || memcmp(any1->value.mem, any2->value.mem, len1)) {
Michal Vasko52927e22020-03-16 17:26:14 +01003298 return LY_ENOT;
3299 }
3300 return LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +01003301 }
Radek Krejci1f05b6a2019-07-18 16:15:06 +02003302 }
3303 }
3304
Michal Vaskob7be7a82020-08-20 09:09:04 +02003305 LOGINT(LYD_CTX(node1));
Radek Krejci1f05b6a2019-07-18 16:15:06 +02003306 return LY_EINT;
3307}
Radek Krejci22ebdba2019-07-25 13:59:43 +02003308
Michal Vasko21725742020-06-29 11:49:25 +02003309API LY_ERR
aPiecek2f63f952021-03-30 12:22:18 +02003310lyd_compare_single(const struct lyd_node *node1, const struct lyd_node *node2, uint32_t options)
3311{
3312 return lyd_compare_single_(node1, node2, options, 0);
3313}
3314
3315API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003316lyd_compare_siblings(const struct lyd_node *node1, const struct lyd_node *node2, uint32_t options)
Michal Vasko8f359bf2020-07-28 10:41:15 +02003317{
Michal Vaskod989ba02020-08-24 10:59:24 +02003318 for ( ; node1 && node2; node1 = node1->next, node2 = node2->next) {
Michal Vasko8f359bf2020-07-28 10:41:15 +02003319 LY_CHECK_RET(lyd_compare_single(node1, node2, options));
3320 }
3321
Michal Vasko11a81432020-07-28 16:31:29 +02003322 if (node1 == node2) {
3323 return LY_SUCCESS;
Michal Vasko8f359bf2020-07-28 10:41:15 +02003324 }
Michal Vasko11a81432020-07-28 16:31:29 +02003325 return LY_ENOT;
Michal Vasko8f359bf2020-07-28 10:41:15 +02003326}
3327
3328API LY_ERR
Michal Vasko21725742020-06-29 11:49:25 +02003329lyd_compare_meta(const struct lyd_meta *meta1, const struct lyd_meta *meta2)
3330{
3331 if (!meta1 || !meta2) {
3332 if (meta1 == meta2) {
3333 return LY_SUCCESS;
3334 } else {
3335 return LY_ENOT;
3336 }
3337 }
3338
Michal Vaskoa8083062020-11-06 17:22:10 +01003339 if ((meta1->annotation->module->ctx != meta2->annotation->module->ctx) || (meta1->annotation != meta2->annotation)) {
Michal Vasko21725742020-06-29 11:49:25 +02003340 return LY_ENOT;
3341 }
3342
Michal Vasko21725742020-06-29 11:49:25 +02003343 return meta1->value.realtype->plugin->compare(&meta1->value, &meta2->value);
3344}
3345
Radek Krejci22ebdba2019-07-25 13:59:43 +02003346/**
Michal Vasko9cf62422021-07-01 13:29:32 +02003347 * @brief Create a copy of the attribute.
3348 *
3349 * @param[in] attr Attribute to copy.
3350 * @param[in] node Opaque where to append the new attribute.
3351 * @param[out] dup Optional created attribute copy.
3352 * @return LY_ERR value.
3353 */
3354static LY_ERR
3355lyd_dup_attr_single(const struct lyd_attr *attr, struct lyd_node *node, struct lyd_attr **dup)
3356{
3357 LY_ERR ret = LY_SUCCESS;
3358 struct lyd_attr *a, *last;
3359 struct lyd_node_opaq *opaq = (struct lyd_node_opaq *)node;
3360
3361 LY_CHECK_ARG_RET(NULL, attr, node, !node->schema, LY_EINVAL);
3362
3363 /* create a copy */
3364 a = calloc(1, sizeof *attr);
3365 LY_CHECK_ERR_RET(!a, LOGMEM(LYD_CTX(node)), LY_EMEM);
3366
3367 LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), attr->name.name, 0, &a->name.name), finish);
3368 LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), attr->name.prefix, 0, &a->name.prefix), finish);
3369 LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), attr->name.module_ns, 0, &a->name.module_ns), finish);
3370 LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), attr->value, 0, &a->value), finish);
3371 a->hints = attr->hints;
3372 a->format = attr->format;
3373 if (attr->val_prefix_data) {
3374 ret = ly_dup_prefix_data(LYD_CTX(node), attr->format, attr->val_prefix_data, &a->val_prefix_data);
3375 LY_CHECK_GOTO(ret, finish);
3376 }
3377
3378 /* insert as the last attribute */
3379 a->parent = opaq;
3380 if (opaq->attr) {
3381 for (last = opaq->attr; last->next; last = last->next) {}
3382 last->next = a;
3383 } else {
3384 opaq->attr = a;
3385 }
3386
3387finish:
3388 if (ret) {
3389 lyd_free_attr_single(LYD_CTX(node), a);
3390 } else if (dup) {
3391 *dup = a;
3392 }
3393 return LY_SUCCESS;
3394}
3395
3396/**
Michal Vasko52927e22020-03-16 17:26:14 +01003397 * @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 +02003398 *
3399 * 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 +02003400 *
3401 * @param[in] node Original node to duplicate
3402 * @param[in] parent Parent to insert into, NULL for top-level sibling.
Michal Vasko67177e52021-08-25 11:15:15 +02003403 * @param[in] insert_last Whether the duplicated node can be inserted as the last child of @p parent. Set for
3404 * recursive duplication as an optimization.
Radek Krejcif8b95172020-05-15 14:51:06 +02003405 * @param[in,out] first First sibling, NULL if no top-level sibling exist yet. Can be also NULL if @p parent is set.
3406 * @param[in] options Bitmask of options flags, see @ref dupoptions.
3407 * @param[out] dup_p Pointer where the created duplicated node is placed (besides connecting it int @p parent / @p first sibling).
3408 * @return LY_ERR value
Radek Krejci22ebdba2019-07-25 13:59:43 +02003409 */
Michal Vasko52927e22020-03-16 17:26:14 +01003410static LY_ERR
Michal Vasko67177e52021-08-25 11:15:15 +02003411lyd_dup_r(const struct lyd_node *node, struct lyd_node *parent, ly_bool insert_last, struct lyd_node **first,
3412 uint32_t options, struct lyd_node **dup_p)
Radek Krejci22ebdba2019-07-25 13:59:43 +02003413{
Michal Vasko52927e22020-03-16 17:26:14 +01003414 LY_ERR ret;
Radek Krejci22ebdba2019-07-25 13:59:43 +02003415 struct lyd_node *dup = NULL;
Michal Vasko61551fa2020-07-09 15:45:45 +02003416 struct lyd_meta *meta;
Michal Vasko9cf62422021-07-01 13:29:32 +02003417 struct lyd_attr *attr;
Michal Vasko61551fa2020-07-09 15:45:45 +02003418 struct lyd_node_any *any;
Radek Krejci22ebdba2019-07-25 13:59:43 +02003419
Michal Vasko52927e22020-03-16 17:26:14 +01003420 LY_CHECK_ARG_RET(NULL, node, LY_EINVAL);
Radek Krejci22ebdba2019-07-25 13:59:43 +02003421
Michal Vasko52927e22020-03-16 17:26:14 +01003422 if (!node->schema) {
3423 dup = calloc(1, sizeof(struct lyd_node_opaq));
Michal Vasko9cf62422021-07-01 13:29:32 +02003424 ((struct lyd_node_opaq *)dup)->ctx = LYD_CTX(node);
Michal Vasko52927e22020-03-16 17:26:14 +01003425 } else {
3426 switch (node->schema->nodetype) {
Michal Vasko1bf09392020-03-27 12:38:10 +01003427 case LYS_RPC:
Michal Vasko52927e22020-03-16 17:26:14 +01003428 case LYS_ACTION:
3429 case LYS_NOTIF:
3430 case LYS_CONTAINER:
3431 case LYS_LIST:
3432 dup = calloc(1, sizeof(struct lyd_node_inner));
3433 break;
3434 case LYS_LEAF:
3435 case LYS_LEAFLIST:
3436 dup = calloc(1, sizeof(struct lyd_node_term));
3437 break;
3438 case LYS_ANYDATA:
3439 case LYS_ANYXML:
3440 dup = calloc(1, sizeof(struct lyd_node_any));
3441 break;
3442 default:
Michal Vaskob7be7a82020-08-20 09:09:04 +02003443 LOGINT(LYD_CTX(node));
Michal Vasko52927e22020-03-16 17:26:14 +01003444 ret = LY_EINT;
3445 goto error;
3446 }
Radek Krejci22ebdba2019-07-25 13:59:43 +02003447 }
Michal Vaskob7be7a82020-08-20 09:09:04 +02003448 LY_CHECK_ERR_GOTO(!dup, LOGMEM(LYD_CTX(node)); ret = LY_EMEM, error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02003449
Michal Vaskof6df0a02020-06-16 13:08:34 +02003450 if (options & LYD_DUP_WITH_FLAGS) {
3451 dup->flags = node->flags;
3452 } else {
3453 dup->flags = (node->flags & LYD_DEFAULT) | LYD_NEW;
3454 }
Radek Krejci22ebdba2019-07-25 13:59:43 +02003455 dup->schema = node->schema;
Michal Vasko52927e22020-03-16 17:26:14 +01003456 dup->prev = dup;
Radek Krejci22ebdba2019-07-25 13:59:43 +02003457
Michal Vasko9cf62422021-07-01 13:29:32 +02003458 /* duplicate metadata/attributes */
Michal Vasko25a32822020-07-09 15:48:22 +02003459 if (!(options & LYD_DUP_NO_META)) {
Michal Vasko9cf62422021-07-01 13:29:32 +02003460 if (!node->schema) {
3461 LY_LIST_FOR(((struct lyd_node_opaq *)node)->attr, attr) {
3462 LY_CHECK_GOTO(ret = lyd_dup_attr_single(attr, dup, NULL), error);
3463 }
3464 } else {
3465 LY_LIST_FOR(node->meta, meta) {
3466 LY_CHECK_GOTO(ret = lyd_dup_meta_single(meta, dup, NULL), error);
3467 }
Michal Vasko25a32822020-07-09 15:48:22 +02003468 }
3469 }
Radek Krejci22ebdba2019-07-25 13:59:43 +02003470
3471 /* nodetype-specific work */
Michal Vasko52927e22020-03-16 17:26:14 +01003472 if (!dup->schema) {
3473 struct lyd_node_opaq *opaq = (struct lyd_node_opaq *)dup;
3474 struct lyd_node_opaq *orig = (struct lyd_node_opaq *)node;
3475 struct lyd_node *child;
Radek Krejci22ebdba2019-07-25 13:59:43 +02003476
3477 if (options & LYD_DUP_RECURSIVE) {
3478 /* duplicate all the children */
3479 LY_LIST_FOR(orig->child, child) {
Michal Vasko67177e52021-08-25 11:15:15 +02003480 LY_CHECK_GOTO(ret = lyd_dup_r(child, dup, 1, NULL, options, NULL), error);
Michal Vasko52927e22020-03-16 17:26:14 +01003481 }
3482 }
Michal Vaskoad92b672020-11-12 13:11:31 +01003483 LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), orig->name.name, 0, &opaq->name.name), error);
Michal Vasko9cf62422021-07-01 13:29:32 +02003484 LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), orig->name.prefix, 0, &opaq->name.prefix), error);
Michal Vaskoad92b672020-11-12 13:11:31 +01003485 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 +02003486 LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), orig->value, 0, &opaq->value), error);
3487 opaq->hints = orig->hints;
3488 opaq->format = orig->format;
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01003489 if (orig->val_prefix_data) {
3490 ret = ly_dup_prefix_data(LYD_CTX(node), opaq->format, orig->val_prefix_data, &opaq->val_prefix_data);
3491 LY_CHECK_GOTO(ret, error);
Michal Vasko52927e22020-03-16 17:26:14 +01003492 }
Michal Vasko52927e22020-03-16 17:26:14 +01003493 } else if (dup->schema->nodetype & LYD_NODE_TERM) {
3494 struct lyd_node_term *term = (struct lyd_node_term *)dup;
3495 struct lyd_node_term *orig = (struct lyd_node_term *)node;
3496
3497 term->hash = orig->hash;
Michal Vaskob7be7a82020-08-20 09:09:04 +02003498 LY_CHECK_ERR_GOTO(orig->value.realtype->plugin->duplicate(LYD_CTX(node), &orig->value, &term->value),
Michal Vasko69730152020-10-09 16:30:07 +02003499 LOGERR(LYD_CTX(node), LY_EINT, "Value duplication failed."); ret = LY_EINT, error);
Michal Vasko52927e22020-03-16 17:26:14 +01003500 } else if (dup->schema->nodetype & LYD_NODE_INNER) {
3501 struct lyd_node_inner *orig = (struct lyd_node_inner *)node;
3502 struct lyd_node *child;
3503
3504 if (options & LYD_DUP_RECURSIVE) {
3505 /* duplicate all the children */
3506 LY_LIST_FOR(orig->child, child) {
Michal Vasko67177e52021-08-25 11:15:15 +02003507 LY_CHECK_GOTO(ret = lyd_dup_r(child, dup, 1, NULL, options, NULL), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02003508 }
Michal Vasko69730152020-10-09 16:30:07 +02003509 } else if ((dup->schema->nodetype == LYS_LIST) && !(dup->schema->flags & LYS_KEYLESS)) {
Radek Krejci22ebdba2019-07-25 13:59:43 +02003510 /* always duplicate keys of a list */
Radek Krejci22ebdba2019-07-25 13:59:43 +02003511 child = orig->child;
Michal Vasko544e58a2021-01-28 14:33:41 +01003512 for (const struct lysc_node *key = lysc_node_child(dup->schema);
Michal Vaskoc57d9a92020-06-23 13:28:27 +02003513 key && (key->flags & LYS_KEY);
Radek Krejci0fe9b512019-07-26 17:51:05 +02003514 key = key->next) {
Radek Krejci22ebdba2019-07-25 13:59:43 +02003515 if (!child) {
3516 /* possibly not keys are present in filtered tree */
3517 break;
Radek Krejci0fe9b512019-07-26 17:51:05 +02003518 } else if (child->schema != key) {
3519 /* possibly not all keys are present in filtered tree,
3520 * but there can be also some non-key nodes */
3521 continue;
Radek Krejci22ebdba2019-07-25 13:59:43 +02003522 }
Michal Vasko67177e52021-08-25 11:15:15 +02003523 LY_CHECK_GOTO(ret = lyd_dup_r(child, dup, 1, NULL, options, NULL), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02003524 child = child->next;
3525 }
3526 }
3527 lyd_hash(dup);
3528 } else if (dup->schema->nodetype & LYD_NODE_ANY) {
Michal Vasko61551fa2020-07-09 15:45:45 +02003529 dup->hash = node->hash;
3530 any = (struct lyd_node_any *)node;
3531 LY_CHECK_GOTO(ret = lyd_any_copy_value(dup, &any->value, any->value_type), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02003532 }
3533
Michal Vasko52927e22020-03-16 17:26:14 +01003534 /* insert */
Michal Vasko67177e52021-08-25 11:15:15 +02003535 lyd_insert_node(parent, first, dup, insert_last);
Michal Vasko52927e22020-03-16 17:26:14 +01003536
3537 if (dup_p) {
3538 *dup_p = dup;
3539 }
3540 return LY_SUCCESS;
Radek Krejci22ebdba2019-07-25 13:59:43 +02003541
3542error:
Michal Vasko52927e22020-03-16 17:26:14 +01003543 lyd_free_tree(dup);
3544 return ret;
Radek Krejci22ebdba2019-07-25 13:59:43 +02003545}
3546
Michal Vasko29d674b2021-08-25 11:18:35 +02003547/**
3548 * @brief Get a parent node to connect duplicated subtree to.
3549 *
3550 * @param[in] node Node (subtree) to duplicate.
3551 * @param[in] parent Initial parent to connect to.
3552 * @param[in] options Bitmask of options flags, see @ref dupoptions.
3553 * @param[out] dup_parent First duplicated parent node, if any.
3554 * @param[out] local_parent Correct parent to directly connect duplicated @p node to.
3555 * @return LY_ERR value.
3556 */
Michal Vasko3a41dff2020-07-15 14:30:28 +02003557static LY_ERR
Michal Vasko20909752021-05-18 16:13:38 +02003558lyd_dup_get_local_parent(const struct lyd_node *node, const struct lyd_node_inner *parent, uint32_t options,
3559 struct lyd_node **dup_parent, struct lyd_node_inner **local_parent)
Radek Krejci22ebdba2019-07-25 13:59:43 +02003560{
Michal Vasko3a41dff2020-07-15 14:30:28 +02003561 const struct lyd_node_inner *orig_parent, *iter;
Radek Krejci857189e2020-09-01 13:26:36 +02003562 ly_bool repeat = 1;
Michal Vasko3a41dff2020-07-15 14:30:28 +02003563
3564 *dup_parent = NULL;
3565 *local_parent = NULL;
3566
3567 for (orig_parent = node->parent; repeat && orig_parent; orig_parent = orig_parent->parent) {
3568 if (parent && (parent->schema == orig_parent->schema)) {
3569 /* stop creating parents, connect what we have into the provided parent */
3570 iter = parent;
3571 repeat = 0;
3572 } else {
3573 iter = NULL;
Michal Vasko67177e52021-08-25 11:15:15 +02003574 LY_CHECK_RET(lyd_dup_r((struct lyd_node *)orig_parent, NULL, 0, (struct lyd_node **)&iter, options,
Radek Krejci0f969882020-08-21 16:56:47 +02003575 (struct lyd_node **)&iter));
Michal Vasko3a41dff2020-07-15 14:30:28 +02003576 }
3577 if (!*local_parent) {
3578 *local_parent = (struct lyd_node_inner *)iter;
3579 }
3580 if (iter->child) {
3581 /* 1) list - add after keys
3582 * 2) provided parent with some children */
3583 iter->child->prev->next = *dup_parent;
3584 if (*dup_parent) {
3585 (*dup_parent)->prev = iter->child->prev;
3586 iter->child->prev = *dup_parent;
3587 }
3588 } else {
3589 ((struct lyd_node_inner *)iter)->child = *dup_parent;
3590 }
3591 if (*dup_parent) {
3592 (*dup_parent)->parent = (struct lyd_node_inner *)iter;
3593 }
3594 *dup_parent = (struct lyd_node *)iter;
3595 }
3596
3597 if (repeat && parent) {
3598 /* given parent and created parents chain actually do not interconnect */
Michal Vaskob7be7a82020-08-20 09:09:04 +02003599 LOGERR(LYD_CTX(node), LY_EINVAL,
Michal Vasko69730152020-10-09 16:30:07 +02003600 "Invalid argument parent (%s()) - does not interconnect with the created node's parents chain.", __func__);
Michal Vasko3a41dff2020-07-15 14:30:28 +02003601 return LY_EINVAL;
3602 }
3603
3604 return LY_SUCCESS;
3605}
3606
3607static LY_ERR
Michal Vasko67177e52021-08-25 11:15:15 +02003608lyd_dup(const struct lyd_node *node, struct lyd_node_inner *parent, uint32_t options, ly_bool nosiblings,
3609 struct lyd_node **dup)
Michal Vasko3a41dff2020-07-15 14:30:28 +02003610{
3611 LY_ERR rc;
Radek Krejci22ebdba2019-07-25 13:59:43 +02003612 const struct lyd_node *orig; /* original node to be duplicated */
3613 struct lyd_node *first = NULL; /* the first duplicated node, this is returned */
Radek Krejci22ebdba2019-07-25 13:59:43 +02003614 struct lyd_node *top = NULL; /* the most higher created node */
3615 struct lyd_node_inner *local_parent = NULL; /* the direct parent node for the duplicated node(s) */
Radek Krejci22ebdba2019-07-25 13:59:43 +02003616
Michal Vasko3a41dff2020-07-15 14:30:28 +02003617 LY_CHECK_ARG_RET(NULL, node, LY_EINVAL);
Radek Krejci22ebdba2019-07-25 13:59:43 +02003618
3619 if (options & LYD_DUP_WITH_PARENTS) {
Michal Vasko20909752021-05-18 16:13:38 +02003620 LY_CHECK_GOTO(rc = lyd_dup_get_local_parent(node, parent, options & (LYD_DUP_WITH_FLAGS | LYD_DUP_NO_META),
3621 &top, &local_parent), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02003622 } else {
3623 local_parent = parent;
3624 }
3625
Radek Krejci22ebdba2019-07-25 13:59:43 +02003626 LY_LIST_FOR(node, orig) {
Michal Vasko35f4d772021-01-12 12:08:57 +01003627 if (lysc_is_key(orig->schema)) {
3628 if (local_parent) {
3629 /* the key must already exist in the parent */
3630 rc = lyd_find_sibling_schema(local_parent->child, orig->schema, first ? NULL : &first);
3631 LY_CHECK_ERR_GOTO(rc, LOGINT(LYD_CTX(node)), error);
3632 } else {
3633 assert(!(options & LYD_DUP_WITH_PARENTS));
3634 /* duplicating a single key, okay, I suppose... */
Michal Vasko67177e52021-08-25 11:15:15 +02003635 rc = lyd_dup_r(orig, NULL, 0, &first, options, first ? NULL : &first);
Michal Vasko35f4d772021-01-12 12:08:57 +01003636 LY_CHECK_GOTO(rc, error);
3637 }
3638 } else {
3639 /* if there is no local parent, it will be inserted into first */
Michal Vasko67177e52021-08-25 11:15:15 +02003640 rc = lyd_dup_r(orig, local_parent ? &local_parent->node : NULL, 0, &first, options, first ? NULL : &first);
Michal Vasko35f4d772021-01-12 12:08:57 +01003641 LY_CHECK_GOTO(rc, error);
3642 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02003643 if (nosiblings) {
Radek Krejci22ebdba2019-07-25 13:59:43 +02003644 break;
3645 }
3646 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02003647
Michal Vasko3a41dff2020-07-15 14:30:28 +02003648 if (dup) {
3649 *dup = first;
3650 }
3651 return LY_SUCCESS;
Radek Krejci22ebdba2019-07-25 13:59:43 +02003652
3653error:
3654 if (top) {
3655 lyd_free_tree(top);
3656 } else {
Michal Vaskof03ed032020-03-04 13:31:44 +01003657 lyd_free_siblings(first);
Radek Krejci22ebdba2019-07-25 13:59:43 +02003658 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02003659 return rc;
Radek Krejci22ebdba2019-07-25 13:59:43 +02003660}
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003661
Michal Vasko3a41dff2020-07-15 14:30:28 +02003662API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003663lyd_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 +02003664{
3665 return lyd_dup(node, parent, options, 1, dup);
3666}
3667
3668API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003669lyd_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 +02003670{
3671 return lyd_dup(node, parent, options, 0, dup);
3672}
3673
3674API LY_ERR
3675lyd_dup_meta_single(const struct lyd_meta *meta, struct lyd_node *node, struct lyd_meta **dup)
Michal Vasko25a32822020-07-09 15:48:22 +02003676{
Radek Krejci011e4aa2020-09-04 15:22:31 +02003677 LY_ERR ret = LY_SUCCESS;
Michal Vasko25a32822020-07-09 15:48:22 +02003678 struct lyd_meta *mt, *last;
3679
Michal Vasko3a41dff2020-07-15 14:30:28 +02003680 LY_CHECK_ARG_RET(NULL, meta, node, LY_EINVAL);
Michal Vasko25a32822020-07-09 15:48:22 +02003681
3682 /* create a copy */
3683 mt = calloc(1, sizeof *mt);
Michal Vaskob7be7a82020-08-20 09:09:04 +02003684 LY_CHECK_ERR_RET(!mt, LOGMEM(LYD_CTX(node)), LY_EMEM);
Michal Vasko25a32822020-07-09 15:48:22 +02003685 mt->annotation = meta->annotation;
Michal Vaskob7be7a82020-08-20 09:09:04 +02003686 ret = meta->value.realtype->plugin->duplicate(LYD_CTX(node), &meta->value, &mt->value);
Radek Krejci011e4aa2020-09-04 15:22:31 +02003687 LY_CHECK_ERR_GOTO(ret, LOGERR(LYD_CTX(node), LY_EINT, "Value duplication failed."), finish);
3688 LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), meta->name, 0, &mt->name), finish);
Michal Vasko25a32822020-07-09 15:48:22 +02003689
3690 /* insert as the last attribute */
Radek Krejci011e4aa2020-09-04 15:22:31 +02003691 mt->parent = node;
Michal Vasko25a32822020-07-09 15:48:22 +02003692 if (node->meta) {
Radek Krejci1e008d22020-08-17 11:37:37 +02003693 for (last = node->meta; last->next; last = last->next) {}
Michal Vasko25a32822020-07-09 15:48:22 +02003694 last->next = mt;
3695 } else {
3696 node->meta = mt;
3697 }
3698
Radek Krejci011e4aa2020-09-04 15:22:31 +02003699finish:
3700 if (ret) {
3701 lyd_free_meta_single(mt);
3702 } else if (dup) {
Michal Vasko3a41dff2020-07-15 14:30:28 +02003703 *dup = mt;
3704 }
3705 return LY_SUCCESS;
Michal Vasko25a32822020-07-09 15:48:22 +02003706}
3707
Michal Vasko4490d312020-06-16 13:08:55 +02003708/**
3709 * @brief Merge a source sibling into target siblings.
3710 *
3711 * @param[in,out] first_trg First target sibling, is updated if top-level.
3712 * @param[in] parent_trg Target parent.
3713 * @param[in,out] sibling_src Source sibling to merge, set to NULL if spent.
Michal Vaskocd3f6172021-05-18 16:14:50 +02003714 * @param[in] merge_cb Optional merge callback.
3715 * @param[in] cb_data Arbitrary callback data.
Michal Vasko4490d312020-06-16 13:08:55 +02003716 * @param[in] options Merge options.
Michal Vaskocd3f6172021-05-18 16:14:50 +02003717 * @param[in,out] dup_inst Duplicate instance cache for all @p first_trg siblings.
Michal Vasko4490d312020-06-16 13:08:55 +02003718 * @return LY_ERR value.
3719 */
3720static LY_ERR
3721lyd_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 +02003722 lyd_merge_cb merge_cb, void *cb_data, uint16_t options, struct lyd_dup_inst **dup_inst)
Michal Vasko4490d312020-06-16 13:08:55 +02003723{
Michal Vasko4490d312020-06-16 13:08:55 +02003724 const struct lyd_node *child_src, *tmp, *sibling_src;
Michal Vasko56daf732020-08-10 10:57:18 +02003725 struct lyd_node *match_trg, *dup_src, *elem;
Michal Vasko4490d312020-06-16 13:08:55 +02003726 struct lysc_type *type;
Michal Vaskocd3f6172021-05-18 16:14:50 +02003727 struct lyd_dup_inst *child_dup_inst = NULL;
3728 LY_ERR ret;
3729 ly_bool first_inst = 0;
Michal Vasko4490d312020-06-16 13:08:55 +02003730
3731 sibling_src = *sibling_src_p;
Michal Vaskocd3f6172021-05-18 16:14:50 +02003732 if (!sibling_src->schema) {
3733 /* try to find the same opaque node */
3734 lyd_find_sibling_opaq_next(*first_trg, LYD_NAME(sibling_src), &match_trg);
3735 } else if (sibling_src->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) {
Michal Vasko4490d312020-06-16 13:08:55 +02003736 /* try to find the exact instance */
Michal Vaskocd3f6172021-05-18 16:14:50 +02003737 lyd_find_sibling_first(*first_trg, sibling_src, &match_trg);
Michal Vasko4490d312020-06-16 13:08:55 +02003738 } else {
3739 /* try to simply find the node, there cannot be more instances */
Michal Vaskocd3f6172021-05-18 16:14:50 +02003740 lyd_find_sibling_val(*first_trg, sibling_src->schema, NULL, 0, &match_trg);
Michal Vasko4490d312020-06-16 13:08:55 +02003741 }
3742
Michal Vaskocd3f6172021-05-18 16:14:50 +02003743 if (match_trg) {
3744 /* update match as needed */
3745 LY_CHECK_RET(lyd_dup_inst_next(&match_trg, *first_trg, dup_inst));
3746 } else {
3747 /* first instance of this node */
3748 first_inst = 1;
3749 }
3750
3751 if (match_trg) {
3752 /* call callback */
3753 if (merge_cb) {
3754 LY_CHECK_RET(merge_cb(match_trg, sibling_src, cb_data));
3755 }
3756
Michal Vasko4490d312020-06-16 13:08:55 +02003757 /* node found, make sure even value matches for all node types */
Michal Vasko8f359bf2020-07-28 10:41:15 +02003758 if ((match_trg->schema->nodetype == LYS_LEAF) && lyd_compare_single(sibling_src, match_trg, LYD_COMPARE_DEFAULTS)) {
Michal Vasko4490d312020-06-16 13:08:55 +02003759 /* since they are different, they cannot both be default */
3760 assert(!(sibling_src->flags & LYD_DEFAULT) || !(match_trg->flags & LYD_DEFAULT));
3761
Michal Vasko3a41dff2020-07-15 14:30:28 +02003762 /* update value (or only LYD_DEFAULT flag) only if flag set or the source node is not default */
3763 if ((options & LYD_MERGE_DEFAULTS) || !(sibling_src->flags & LYD_DEFAULT)) {
Michal Vasko4490d312020-06-16 13:08:55 +02003764 type = ((struct lysc_node_leaf *)match_trg->schema)->type;
Michal Vaskob7be7a82020-08-20 09:09:04 +02003765 type->plugin->free(LYD_CTX(match_trg), &((struct lyd_node_term *)match_trg)->value);
3766 LY_CHECK_RET(type->plugin->duplicate(LYD_CTX(match_trg), &((struct lyd_node_term *)sibling_src)->value,
Michal Vasko69730152020-10-09 16:30:07 +02003767 &((struct lyd_node_term *)match_trg)->value));
Michal Vasko4490d312020-06-16 13:08:55 +02003768
3769 /* copy flags and add LYD_NEW */
Michal Vasko7e8315b2021-08-03 17:01:06 +02003770 match_trg->flags = sibling_src->flags | ((options & LYD_MERGE_WITH_FLAGS) ? 0 : LYD_NEW);
Michal Vasko4490d312020-06-16 13:08:55 +02003771 }
Michal Vasko8f359bf2020-07-28 10:41:15 +02003772 } else if ((match_trg->schema->nodetype & LYS_ANYDATA) && lyd_compare_single(sibling_src, match_trg, 0)) {
Michal Vasko4c583e82020-07-17 12:16:14 +02003773 /* update value */
3774 LY_CHECK_RET(lyd_any_copy_value(match_trg, &((struct lyd_node_any *)sibling_src)->value,
Radek Krejci0f969882020-08-21 16:56:47 +02003775 ((struct lyd_node_any *)sibling_src)->value_type));
Michal Vasko4490d312020-06-16 13:08:55 +02003776
3777 /* copy flags and add LYD_NEW */
Michal Vasko7e8315b2021-08-03 17:01:06 +02003778 match_trg->flags = sibling_src->flags | ((options & LYD_MERGE_WITH_FLAGS) ? 0 : LYD_NEW);
Michal Vaskocd3f6172021-05-18 16:14:50 +02003779 }
3780
3781 /* check descendants, recursively */
3782 ret = LY_SUCCESS;
3783 LY_LIST_FOR_SAFE(lyd_child_no_keys(sibling_src), tmp, child_src) {
3784 ret = lyd_merge_sibling_r(lyd_node_child_p(match_trg), match_trg, &child_src, merge_cb, cb_data, options,
3785 &child_dup_inst);
3786 if (ret) {
3787 break;
Michal Vasko4490d312020-06-16 13:08:55 +02003788 }
3789 }
Michal Vaskocd3f6172021-05-18 16:14:50 +02003790 lyd_dup_inst_free(child_dup_inst);
3791 LY_CHECK_RET(ret);
Michal Vasko4490d312020-06-16 13:08:55 +02003792 } else {
3793 /* node not found, merge it */
3794 if (options & LYD_MERGE_DESTRUCT) {
3795 dup_src = (struct lyd_node *)sibling_src;
3796 lyd_unlink_tree(dup_src);
3797 /* spend it */
3798 *sibling_src_p = NULL;
3799 } else {
Michal Vasko3a41dff2020-07-15 14:30:28 +02003800 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 +02003801 }
3802
Michal Vasko7e8315b2021-08-03 17:01:06 +02003803 if (!(options & LYD_MERGE_WITH_FLAGS)) {
3804 /* set LYD_NEW for all the new nodes, required for validation */
3805 LYD_TREE_DFS_BEGIN(dup_src, elem) {
3806 elem->flags |= LYD_NEW;
3807 LYD_TREE_DFS_END(dup_src, elem);
3808 }
Michal Vasko4490d312020-06-16 13:08:55 +02003809 }
3810
Michal Vaskocd3f6172021-05-18 16:14:50 +02003811 /* insert */
Michal Vasko6ee6f432021-07-16 09:49:14 +02003812 lyd_insert_node(parent_trg, first_trg, dup_src, 0);
Michal Vaskocd3f6172021-05-18 16:14:50 +02003813
3814 if (first_inst) {
3815 /* remember not to find this instance next time */
3816 LY_CHECK_RET(lyd_dup_inst_next(&dup_src, *first_trg, dup_inst));
3817 }
3818
3819 /* call callback, no source node */
3820 if (merge_cb) {
3821 LY_CHECK_RET(merge_cb(dup_src, NULL, cb_data));
3822 }
Michal Vasko4490d312020-06-16 13:08:55 +02003823 }
3824
3825 return LY_SUCCESS;
3826}
3827
Michal Vasko3a41dff2020-07-15 14:30:28 +02003828static LY_ERR
Michal Vaskocd3f6172021-05-18 16:14:50 +02003829lyd_merge(struct lyd_node **target, const struct lyd_node *source, const struct lys_module *mod,
3830 lyd_merge_cb merge_cb, void *cb_data, uint16_t options, ly_bool nosiblings)
Michal Vasko4490d312020-06-16 13:08:55 +02003831{
3832 const struct lyd_node *sibling_src, *tmp;
Michal Vaskocd3f6172021-05-18 16:14:50 +02003833 struct lyd_dup_inst *dup_inst = NULL;
Radek Krejci857189e2020-09-01 13:26:36 +02003834 ly_bool first;
Michal Vaskocd3f6172021-05-18 16:14:50 +02003835 LY_ERR ret = LY_SUCCESS;
Michal Vasko4490d312020-06-16 13:08:55 +02003836
3837 LY_CHECK_ARG_RET(NULL, target, LY_EINVAL);
Michal Vasko892f5bf2021-11-24 10:41:05 +01003838 LY_CHECK_CTX_EQUAL_RET(*target ? LYD_CTX(*target) : NULL, source ? LYD_CTX(source) : NULL, mod ? mod->ctx : NULL,
3839 LY_EINVAL);
Michal Vasko4490d312020-06-16 13:08:55 +02003840
3841 if (!source) {
3842 /* nothing to merge */
3843 return LY_SUCCESS;
3844 }
3845
Michal Vasko831422b2020-11-24 11:20:51 +01003846 if ((*target && lysc_data_parent((*target)->schema)) || lysc_data_parent(source->schema)) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02003847 LOGERR(LYD_CTX(source), LY_EINVAL, "Invalid arguments - can merge only 2 top-level subtrees (%s()).", __func__);
Michal Vasko4490d312020-06-16 13:08:55 +02003848 return LY_EINVAL;
3849 }
3850
3851 LY_LIST_FOR_SAFE(source, tmp, sibling_src) {
Michal Vaskocd3f6172021-05-18 16:14:50 +02003852 if (mod && (lyd_owner_module(sibling_src) != mod)) {
3853 /* skip data nodes from different modules */
3854 continue;
3855 }
3856
Radek Krejci857189e2020-09-01 13:26:36 +02003857 first = (sibling_src == source) ? 1 : 0;
Michal Vaskocd3f6172021-05-18 16:14:50 +02003858 ret = lyd_merge_sibling_r(target, NULL, &sibling_src, merge_cb, cb_data, options, &dup_inst);
3859 if (ret) {
3860 break;
3861 }
Michal Vasko4490d312020-06-16 13:08:55 +02003862 if (first && !sibling_src) {
3863 /* source was spent (unlinked), move to the next node */
3864 source = tmp;
3865 }
3866
Michal Vasko3a41dff2020-07-15 14:30:28 +02003867 if (nosiblings) {
Michal Vasko4490d312020-06-16 13:08:55 +02003868 break;
3869 }
3870 }
3871
3872 if (options & LYD_MERGE_DESTRUCT) {
3873 /* free any leftover source data that were not merged */
3874 lyd_free_siblings((struct lyd_node *)source);
3875 }
3876
Michal Vaskocd3f6172021-05-18 16:14:50 +02003877 lyd_dup_inst_free(dup_inst);
3878 return ret;
Michal Vasko4490d312020-06-16 13:08:55 +02003879}
3880
Michal Vasko3a41dff2020-07-15 14:30:28 +02003881API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003882lyd_merge_tree(struct lyd_node **target, const struct lyd_node *source, uint16_t options)
Michal Vasko3a41dff2020-07-15 14:30:28 +02003883{
Michal Vaskocd3f6172021-05-18 16:14:50 +02003884 return lyd_merge(target, source, NULL, NULL, NULL, options, 1);
Michal Vasko3a41dff2020-07-15 14:30:28 +02003885}
3886
3887API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003888lyd_merge_siblings(struct lyd_node **target, const struct lyd_node *source, uint16_t options)
Michal Vasko3a41dff2020-07-15 14:30:28 +02003889{
Michal Vaskocd3f6172021-05-18 16:14:50 +02003890 return lyd_merge(target, source, NULL, NULL, NULL, options, 0);
3891}
3892
3893API LY_ERR
3894lyd_merge_module(struct lyd_node **target, const struct lyd_node *source, const struct lys_module *mod,
3895 lyd_merge_cb merge_cb, void *cb_data, uint16_t options)
3896{
3897 return lyd_merge(target, source, mod, merge_cb, cb_data, options, 0);
Michal Vasko3a41dff2020-07-15 14:30:28 +02003898}
3899
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003900static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02003901lyd_path_str_enlarge(char **buffer, size_t *buflen, size_t reqlen, ly_bool is_static)
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003902{
Michal Vasko14654712020-02-06 08:35:21 +01003903 /* ending \0 */
3904 ++reqlen;
3905
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003906 if (reqlen > *buflen) {
3907 if (is_static) {
3908 return LY_EINCOMPLETE;
3909 }
3910
3911 *buffer = ly_realloc(*buffer, reqlen * sizeof **buffer);
3912 if (!*buffer) {
3913 return LY_EMEM;
3914 }
3915
3916 *buflen = reqlen;
3917 }
3918
3919 return LY_SUCCESS;
3920}
3921
Michal Vaskod59035b2020-07-08 12:00:06 +02003922LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02003923lyd_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 +02003924{
3925 const struct lyd_node *key;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003926 size_t len;
3927 const char *val;
3928 char quot;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003929
Michal Vasko824d0832021-11-04 15:36:51 +01003930 for (key = lyd_child(node); key && key->schema && (key->schema->flags & LYS_KEY); key = key->next) {
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02003931 val = lyd_get_value(key);
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003932 len = 1 + strlen(key->schema->name) + 2 + strlen(val) + 2;
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003933 LY_CHECK_RET(lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static));
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003934
3935 quot = '\'';
3936 if (strchr(val, '\'')) {
3937 quot = '"';
3938 }
3939 *bufused += sprintf(*buffer + *bufused, "[%s=%c%s%c]", key->schema->name, quot, val, quot);
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003940 }
3941
3942 return LY_SUCCESS;
3943}
3944
3945/**
3946 * @brief Append leaf-list value predicate to path.
3947 *
3948 * @param[in] node Node to print.
3949 * @param[in,out] buffer Buffer to print to.
3950 * @param[in,out] buflen Current buffer length.
3951 * @param[in,out] bufused Current number of characters used in @p buffer.
3952 * @param[in] is_static Whether buffer is static or can be reallocated.
3953 * @return LY_ERR
3954 */
3955static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02003956lyd_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 +02003957{
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003958 size_t len;
3959 const char *val;
3960 char quot;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003961
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02003962 val = lyd_get_value(node);
Radek Krejcif13b87b2020-12-01 22:02:17 +01003963 len = 4 + strlen(val) + 2; /* "[.='" + val + "']" */
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003964 LY_CHECK_RET(lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static));
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003965
3966 quot = '\'';
3967 if (strchr(val, '\'')) {
3968 quot = '"';
3969 }
3970 *bufused += sprintf(*buffer + *bufused, "[.=%c%s%c]", quot, val, quot);
3971
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003972 return LY_SUCCESS;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003973}
3974
3975/**
3976 * @brief Append node position (relative to its other instances) predicate to path.
3977 *
3978 * @param[in] node Node to print.
3979 * @param[in,out] buffer Buffer to print to.
3980 * @param[in,out] buflen Current buffer length.
3981 * @param[in,out] bufused Current number of characters used in @p buffer.
3982 * @param[in] is_static Whether buffer is static or can be reallocated.
3983 * @return LY_ERR
3984 */
3985static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02003986lyd_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 +02003987{
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003988 size_t len;
Michal Vasko50cc0562021-05-18 16:15:43 +02003989 uint32_t pos;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003990 char *val = NULL;
3991 LY_ERR rc;
3992
Michal Vasko50cc0562021-05-18 16:15:43 +02003993 pos = lyd_list_pos(node);
3994 if (asprintf(&val, "%" PRIu32, pos) == -1) {
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003995 return LY_EMEM;
3996 }
3997
3998 len = 1 + strlen(val) + 1;
3999 rc = lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static);
4000 if (rc != LY_SUCCESS) {
4001 goto cleanup;
4002 }
4003
4004 *bufused += sprintf(*buffer + *bufused, "[%s]", val);
4005
4006cleanup:
4007 free(val);
4008 return rc;
4009}
4010
4011API char *
4012lyd_path(const struct lyd_node *node, LYD_PATH_TYPE pathtype, char *buffer, size_t buflen)
4013{
Radek Krejci857189e2020-09-01 13:26:36 +02004014 ly_bool is_static = 0;
Radek Krejci1deb5be2020-08-26 16:43:36 +02004015 uint32_t i, depth;
Michal Vasko14654712020-02-06 08:35:21 +01004016 size_t bufused = 0, len;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02004017 const struct lyd_node *iter;
4018 const struct lys_module *mod;
Michal Vasko790b2bc2020-08-03 13:35:06 +02004019 LY_ERR rc = LY_SUCCESS;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02004020
4021 LY_CHECK_ARG_RET(NULL, node, NULL);
4022 if (buffer) {
Michal Vasko16385f42021-05-18 16:16:09 +02004023 LY_CHECK_ARG_RET(LYD_CTX(node), buflen > 1, NULL);
Michal Vasko5ec7cda2019-09-11 13:43:08 +02004024 is_static = 1;
Michal Vasko14654712020-02-06 08:35:21 +01004025 } else {
4026 buflen = 0;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02004027 }
4028
4029 switch (pathtype) {
Radek Krejci635d2b82021-01-04 11:26:51 +01004030 case LYD_PATH_STD:
4031 case LYD_PATH_STD_NO_LAST_PRED:
Michal Vasko14654712020-02-06 08:35:21 +01004032 depth = 1;
Michal Vasko9e685082021-01-29 14:49:09 +01004033 for (iter = node; iter->parent; iter = lyd_parent(iter)) {
Michal Vasko5ec7cda2019-09-11 13:43:08 +02004034 ++depth;
4035 }
4036
Michal Vasko5ec7cda2019-09-11 13:43:08 +02004037 goto iter_print;
Michal Vasko14654712020-02-06 08:35:21 +01004038 while (depth) {
Michal Vasko5ec7cda2019-09-11 13:43:08 +02004039 /* find the right node */
Michal Vasko9e685082021-01-29 14:49:09 +01004040 for (iter = node, i = 1; i < depth; iter = lyd_parent(iter), ++i) {}
Michal Vasko5ec7cda2019-09-11 13:43:08 +02004041iter_print:
4042 /* print prefix and name */
4043 mod = NULL;
Michal Vasko69730152020-10-09 16:30:07 +02004044 if (iter->schema && (!iter->parent || (iter->schema->module != iter->parent->schema->module))) {
Michal Vasko5ec7cda2019-09-11 13:43:08 +02004045 mod = iter->schema->module;
4046 }
4047
4048 /* realloc string */
Michal Vaskoad92b672020-11-12 13:11:31 +01004049 len = 1 + (mod ? strlen(mod->name) + 1 : 0) + (iter->schema ? strlen(iter->schema->name) :
4050 strlen(((struct lyd_node_opaq *)iter)->name.name));
Michal Vasko5ec7cda2019-09-11 13:43:08 +02004051 rc = lyd_path_str_enlarge(&buffer, &buflen, bufused + len, is_static);
4052 if (rc != LY_SUCCESS) {
4053 break;
4054 }
4055
4056 /* print next node */
Radek Krejci1798aae2020-07-14 13:26:06 +02004057 bufused += sprintf(buffer + bufused, "/%s%s%s", mod ? mod->name : "", mod ? ":" : "",
Michal Vaskoad92b672020-11-12 13:11:31 +01004058 iter->schema ? iter->schema->name : ((struct lyd_node_opaq *)iter)->name.name);
Michal Vasko5ec7cda2019-09-11 13:43:08 +02004059
Michal Vasko790b2bc2020-08-03 13:35:06 +02004060 /* do not always print the last (first) predicate */
Radek Krejci635d2b82021-01-04 11:26:51 +01004061 if (iter->schema && ((depth > 1) || (pathtype == LYD_PATH_STD))) {
Michal Vasko790b2bc2020-08-03 13:35:06 +02004062 switch (iter->schema->nodetype) {
4063 case LYS_LIST:
4064 if (iter->schema->flags & LYS_KEYLESS) {
4065 /* print its position */
4066 rc = lyd_path_position_predicate(iter, &buffer, &buflen, &bufused, is_static);
4067 } else {
4068 /* print all list keys in predicates */
4069 rc = lyd_path_list_predicate(iter, &buffer, &buflen, &bufused, is_static);
4070 }
4071 break;
4072 case LYS_LEAFLIST:
4073 if (iter->schema->flags & LYS_CONFIG_W) {
4074 /* print leaf-list value */
4075 rc = lyd_path_leaflist_predicate(iter, &buffer, &buflen, &bufused, is_static);
4076 } else {
4077 /* print its position */
4078 rc = lyd_path_position_predicate(iter, &buffer, &buflen, &bufused, is_static);
4079 }
4080 break;
4081 default:
4082 /* nothing to print more */
4083 break;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02004084 }
Michal Vasko5ec7cda2019-09-11 13:43:08 +02004085 }
4086 if (rc != LY_SUCCESS) {
4087 break;
4088 }
4089
Michal Vasko14654712020-02-06 08:35:21 +01004090 --depth;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02004091 }
4092 break;
4093 }
4094
4095 return buffer;
4096}
Michal Vaskoe444f752020-02-10 12:20:06 +01004097
Michal Vasko25a32822020-07-09 15:48:22 +02004098API struct lyd_meta *
4099lyd_find_meta(const struct lyd_meta *first, const struct lys_module *module, const char *name)
4100{
4101 struct lyd_meta *ret = NULL;
4102 const struct ly_ctx *ctx;
4103 const char *prefix, *tmp;
4104 char *str;
4105 size_t pref_len, name_len;
4106
4107 LY_CHECK_ARG_RET(NULL, module || strchr(name, ':'), name, NULL);
Michal Vasko892f5bf2021-11-24 10:41:05 +01004108 LY_CHECK_CTX_EQUAL_RET(first ? first->annotation->module->ctx : NULL, module ? module->ctx : NULL, NULL);
Michal Vasko25a32822020-07-09 15:48:22 +02004109
4110 if (!first) {
4111 return NULL;
4112 }
4113
4114 ctx = first->annotation->module->ctx;
4115
4116 /* parse the name */
4117 tmp = name;
4118 if (ly_parse_nodeid(&tmp, &prefix, &pref_len, &name, &name_len) || tmp[0]) {
4119 LOGERR(ctx, LY_EINVAL, "Metadata name \"%s\" is not valid.", name);
4120 return NULL;
4121 }
4122
4123 /* find the module */
4124 if (prefix) {
4125 str = strndup(prefix, pref_len);
4126 module = ly_ctx_get_module_latest(ctx, str);
4127 free(str);
Radek Krejci422afb12021-03-04 16:38:16 +01004128 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 +02004129 }
4130
4131 /* find the metadata */
4132 LY_LIST_FOR(first, first) {
4133 if ((first->annotation->module == module) && !strcmp(first->name, name)) {
4134 ret = (struct lyd_meta *)first;
4135 break;
4136 }
4137 }
4138
4139 return ret;
4140}
4141
Michal Vasko9b368d32020-02-14 13:53:31 +01004142API LY_ERR
Michal Vaskoe444f752020-02-10 12:20:06 +01004143lyd_find_sibling_first(const struct lyd_node *siblings, const struct lyd_node *target, struct lyd_node **match)
4144{
Michal Vaskoe78faec2021-04-08 17:24:43 +02004145 struct lyd_node **match_p, *iter;
Michal Vaskoe444f752020-02-10 12:20:06 +01004146 struct lyd_node_inner *parent;
Michal Vaskoe78faec2021-04-08 17:24:43 +02004147 ly_bool found;
Michal Vaskoe444f752020-02-10 12:20:06 +01004148
Michal Vaskof03ed032020-03-04 13:31:44 +01004149 LY_CHECK_ARG_RET(NULL, target, LY_EINVAL);
Michal Vasko892f5bf2021-11-24 10:41:05 +01004150 LY_CHECK_CTX_EQUAL_RET(siblings ? LYD_CTX(siblings) : NULL, LYD_CTX(target), LY_EINVAL);
Michal Vaskoe444f752020-02-10 12:20:06 +01004151
Michal Vasko6da1e952020-12-09 18:14:38 +01004152 if (!siblings || (siblings->schema && target->schema &&
4153 (lysc_data_parent(siblings->schema) != lysc_data_parent(target->schema)))) {
Michal Vasko62ed12d2020-05-21 10:08:25 +02004154 /* no data or schema mismatch */
Michal Vasko9b368d32020-02-14 13:53:31 +01004155 if (match) {
4156 *match = NULL;
4157 }
Michal Vaskoe444f752020-02-10 12:20:06 +01004158 return LY_ENOTFOUND;
4159 }
4160
Michal Vaskoe78faec2021-04-08 17:24:43 +02004161 /* get first sibling */
4162 siblings = lyd_first_sibling(siblings);
Michal Vaskoe444f752020-02-10 12:20:06 +01004163
Michal Vasko9e685082021-01-29 14:49:09 +01004164 parent = siblings->parent;
Michal Vasko6da1e952020-12-09 18:14:38 +01004165 if (parent && parent->schema && parent->children_ht) {
Michal Vaskoe444f752020-02-10 12:20:06 +01004166 assert(target->hash);
4167
Michal Vaskoe78faec2021-04-08 17:24:43 +02004168 if (lysc_is_dup_inst_list(target->schema)) {
4169 /* we must search the instances from beginning to find the first matching one */
4170 found = 0;
4171 LYD_LIST_FOR_INST(siblings, target->schema, iter) {
4172 if (!lyd_compare_single(target, iter, 0)) {
4173 found = 1;
4174 break;
4175 }
4176 }
4177 if (found) {
4178 siblings = iter;
Michal Vaskoda859032020-07-14 12:20:14 +02004179 } else {
4180 siblings = NULL;
4181 }
Michal Vaskoe444f752020-02-10 12:20:06 +01004182 } else {
Michal Vaskoe78faec2021-04-08 17:24:43 +02004183 /* find by hash */
4184 if (!lyht_find(parent->children_ht, &target, target->hash, (void **)&match_p)) {
4185 siblings = *match_p;
4186 } else {
4187 /* not found */
4188 siblings = NULL;
4189 }
Michal Vaskoe444f752020-02-10 12:20:06 +01004190 }
4191 } else {
4192 /* no children hash table */
Michal Vaskod989ba02020-08-24 10:59:24 +02004193 for ( ; siblings; siblings = siblings->next) {
Michal Vasko8f359bf2020-07-28 10:41:15 +02004194 if (!lyd_compare_single(siblings, target, 0)) {
Michal Vaskoe444f752020-02-10 12:20:06 +01004195 break;
4196 }
4197 }
4198 }
4199
4200 if (!siblings) {
Michal Vasko9b368d32020-02-14 13:53:31 +01004201 if (match) {
4202 *match = NULL;
4203 }
Michal Vaskoe444f752020-02-10 12:20:06 +01004204 return LY_ENOTFOUND;
4205 }
4206
Michal Vasko9b368d32020-02-14 13:53:31 +01004207 if (match) {
4208 *match = (struct lyd_node *)siblings;
4209 }
Michal Vaskoe444f752020-02-10 12:20:06 +01004210 return LY_SUCCESS;
4211}
4212
Radek Krejci857189e2020-09-01 13:26:36 +02004213/**
4214 * @brief Comparison callback to match schema node with a schema of a data node.
4215 *
4216 * @param[in] val1_p Pointer to the schema node
4217 * @param[in] val2_p Pointer to the data node
Michal Vasko62524a92021-02-26 10:08:50 +01004218 * Implementation of ::lyht_value_equal_cb.
Radek Krejci857189e2020-09-01 13:26:36 +02004219 */
4220static ly_bool
4221lyd_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 +01004222{
4223 struct lysc_node *val1;
4224 struct lyd_node *val2;
4225
4226 val1 = *((struct lysc_node **)val1_p);
4227 val2 = *((struct lyd_node **)val2_p);
4228
Michal Vasko90932a92020-02-12 14:33:03 +01004229 if (val1 == val2->schema) {
4230 /* schema match is enough */
4231 return 1;
4232 } else {
4233 return 0;
4234 }
4235}
4236
Michal Vasko92239c72020-11-18 18:17:25 +01004237/**
4238 * @brief Search in the given siblings (NOT recursively) for the first schema node data instance.
4239 * Uses hashes - should be used whenever possible for best performance.
4240 *
4241 * @param[in] siblings Siblings to search in including preceding and succeeding nodes.
4242 * @param[in] schema Target data node schema to find.
4243 * @param[out] match Can be NULL, otherwise the found data node.
4244 * @return LY_SUCCESS on success, @p match set.
4245 * @return LY_ENOTFOUND if not found, @p match set to NULL.
4246 * @return LY_ERR value if another error occurred.
4247 */
Michal Vasko90932a92020-02-12 14:33:03 +01004248static LY_ERR
4249lyd_find_sibling_schema(const struct lyd_node *siblings, const struct lysc_node *schema, struct lyd_node **match)
4250{
4251 struct lyd_node **match_p;
4252 struct lyd_node_inner *parent;
4253 uint32_t hash;
Michal Vasko62524a92021-02-26 10:08:50 +01004254 lyht_value_equal_cb ht_cb;
Michal Vasko90932a92020-02-12 14:33:03 +01004255
Michal Vaskob104f112020-07-17 09:54:54 +02004256 assert(siblings && schema);
Michal Vasko90932a92020-02-12 14:33:03 +01004257
Michal Vasko9e685082021-01-29 14:49:09 +01004258 parent = siblings->parent;
Michal Vasko08fd6132020-11-18 18:17:45 +01004259 if (parent && parent->schema && parent->children_ht) {
Michal Vasko90932a92020-02-12 14:33:03 +01004260 /* calculate our hash */
4261 hash = dict_hash_multi(0, schema->module->name, strlen(schema->module->name));
4262 hash = dict_hash_multi(hash, schema->name, strlen(schema->name));
4263 hash = dict_hash_multi(hash, NULL, 0);
4264
4265 /* use special hash table function */
4266 ht_cb = lyht_set_cb(parent->children_ht, lyd_hash_table_schema_val_equal);
4267
4268 /* find by hash */
4269 if (!lyht_find(parent->children_ht, &schema, hash, (void **)&match_p)) {
4270 siblings = *match_p;
4271 } else {
4272 /* not found */
4273 siblings = NULL;
4274 }
4275
4276 /* set the original hash table compare function back */
4277 lyht_set_cb(parent->children_ht, ht_cb);
4278 } else {
Michal Vaskob104f112020-07-17 09:54:54 +02004279 /* find first sibling */
4280 if (siblings->parent) {
4281 siblings = siblings->parent->child;
4282 } else {
4283 while (siblings->prev->next) {
4284 siblings = siblings->prev;
4285 }
4286 }
4287
4288 /* search manually without hashes */
Michal Vaskod989ba02020-08-24 10:59:24 +02004289 for ( ; siblings; siblings = siblings->next) {
Michal Vasko90932a92020-02-12 14:33:03 +01004290 if (siblings->schema == schema) {
4291 /* schema match is enough */
4292 break;
4293 }
4294 }
4295 }
4296
4297 if (!siblings) {
Michal Vasko9b368d32020-02-14 13:53:31 +01004298 if (match) {
4299 *match = NULL;
4300 }
Michal Vasko90932a92020-02-12 14:33:03 +01004301 return LY_ENOTFOUND;
4302 }
4303
Michal Vasko9b368d32020-02-14 13:53:31 +01004304 if (match) {
4305 *match = (struct lyd_node *)siblings;
4306 }
Michal Vasko90932a92020-02-12 14:33:03 +01004307 return LY_SUCCESS;
4308}
4309
Michal Vaskoe444f752020-02-10 12:20:06 +01004310API LY_ERR
4311lyd_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 +02004312 size_t val_len, struct lyd_node **match)
Michal Vaskoe444f752020-02-10 12:20:06 +01004313{
4314 LY_ERR rc;
4315 struct lyd_node *target = NULL;
4316
Michal Vasko4c583e82020-07-17 12:16:14 +02004317 LY_CHECK_ARG_RET(NULL, schema, !(schema->nodetype & (LYS_CHOICE | LYS_CASE)), LY_EINVAL);
Michal Vasko892f5bf2021-11-24 10:41:05 +01004318 LY_CHECK_CTX_EQUAL_RET(siblings ? LYD_CTX(siblings) : NULL, schema->module->ctx, LY_EINVAL);
Michal Vaskoe444f752020-02-10 12:20:06 +01004319
Michal Vasko08fd6132020-11-18 18:17:45 +01004320 if (!siblings || (siblings->schema && (lysc_data_parent(siblings->schema) != lysc_data_parent(schema)))) {
Michal Vasko62ed12d2020-05-21 10:08:25 +02004321 /* no data or schema mismatch */
Michal Vasko9b368d32020-02-14 13:53:31 +01004322 if (match) {
4323 *match = NULL;
4324 }
Michal Vaskoe444f752020-02-10 12:20:06 +01004325 return LY_ENOTFOUND;
4326 }
4327
Michal Vaskof03ed032020-03-04 13:31:44 +01004328 if (key_or_value && !val_len) {
4329 val_len = strlen(key_or_value);
4330 }
4331
Michal Vaskob104f112020-07-17 09:54:54 +02004332 if ((schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) && key_or_value) {
4333 /* create a data node and find the instance */
4334 if (schema->nodetype == LYS_LEAFLIST) {
4335 /* target used attributes: schema, hash, value */
Radek Krejci8df109d2021-04-23 12:19:08 +02004336 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 +02004337 LY_CHECK_RET(rc);
Michal Vaskob104f112020-07-17 09:54:54 +02004338 } else {
Michal Vasko90932a92020-02-12 14:33:03 +01004339 /* target used attributes: schema, hash, child (all keys) */
Michal Vasko004d3152020-06-11 19:59:22 +02004340 LY_CHECK_RET(lyd_create_list2(schema, key_or_value, val_len, &target));
Michal Vasko90932a92020-02-12 14:33:03 +01004341 }
4342
4343 /* find it */
4344 rc = lyd_find_sibling_first(siblings, target, match);
Michal Vaskob104f112020-07-17 09:54:54 +02004345 } else {
4346 /* find the first schema node instance */
4347 rc = lyd_find_sibling_schema(siblings, schema, match);
Michal Vaskoe444f752020-02-10 12:20:06 +01004348 }
4349
Michal Vaskoe444f752020-02-10 12:20:06 +01004350 lyd_free_tree(target);
4351 return rc;
4352}
Michal Vaskoccc02342020-05-21 10:09:21 +02004353
4354API LY_ERR
Michal Vaskoe78faec2021-04-08 17:24:43 +02004355lyd_find_sibling_dup_inst_set(const struct lyd_node *siblings, const struct lyd_node *target, struct ly_set **set)
4356{
4357 struct lyd_node **match_p, *first, *iter;
4358 struct lyd_node_inner *parent;
4359
4360 LY_CHECK_ARG_RET(NULL, target, lysc_is_dup_inst_list(target->schema), set, LY_EINVAL);
Michal Vasko892f5bf2021-11-24 10:41:05 +01004361 LY_CHECK_CTX_EQUAL_RET(siblings ? LYD_CTX(siblings) : NULL, LYD_CTX(target), LY_EINVAL);
Michal Vaskoe78faec2021-04-08 17:24:43 +02004362
4363 LY_CHECK_RET(ly_set_new(set));
4364
4365 if (!siblings || (siblings->schema && (lysc_data_parent(siblings->schema) != lysc_data_parent(target->schema)))) {
4366 /* no data or schema mismatch */
4367 return LY_ENOTFOUND;
4368 }
4369
4370 /* get first sibling */
4371 siblings = lyd_first_sibling(siblings);
4372
4373 parent = siblings->parent;
4374 if (parent && parent->schema && parent->children_ht) {
4375 assert(target->hash);
4376
4377 /* find the first instance */
4378 lyd_find_sibling_first(siblings, target, &first);
4379 if (first) {
4380 /* add it so that it is the first in the set */
4381 if (ly_set_add(*set, first, 1, NULL)) {
4382 goto error;
4383 }
4384
4385 /* find by hash */
4386 if (!lyht_find(parent->children_ht, &target, target->hash, (void **)&match_p)) {
4387 iter = *match_p;
4388 } else {
4389 /* not found */
4390 iter = NULL;
4391 }
4392 while (iter) {
4393 /* add all found nodes into the set */
4394 if ((iter != first) && !lyd_compare_single(iter, target, 0) && ly_set_add(*set, iter, 1, NULL)) {
4395 goto error;
4396 }
4397
4398 /* find next instance */
4399 if (lyht_find_next(parent->children_ht, &iter, iter->hash, (void **)&match_p)) {
4400 iter = NULL;
4401 } else {
4402 iter = *match_p;
4403 }
4404 }
4405 }
4406 } else {
4407 /* no children hash table */
4408 LY_LIST_FOR(siblings, siblings) {
4409 if (!lyd_compare_single(target, siblings, 0)) {
4410 ly_set_add(*set, (void *)siblings, 1, NULL);
4411 }
4412 }
4413 }
4414
4415 if (!(*set)->count) {
4416 return LY_ENOTFOUND;
4417 }
4418 return LY_SUCCESS;
4419
4420error:
4421 ly_set_free(*set, NULL);
4422 *set = NULL;
4423 return LY_EMEM;
4424}
4425
4426API LY_ERR
Michal Vasko1d4af6c2021-02-22 13:31:26 +01004427lyd_find_sibling_opaq_next(const struct lyd_node *first, const char *name, struct lyd_node **match)
4428{
4429 LY_CHECK_ARG_RET(NULL, name, LY_EINVAL);
4430
4431 for ( ; first; first = first->next) {
4432 if (!first->schema && !strcmp(LYD_NAME(first), name)) {
4433 break;
4434 }
4435 }
4436
4437 if (match) {
4438 *match = (struct lyd_node *)first;
4439 }
4440 return first ? LY_SUCCESS : LY_ENOTFOUND;
4441}
4442
4443API LY_ERR
Michal Vaskoe3716b32021-12-13 16:58:25 +01004444lyd_find_xpath3(const struct lyd_node *ctx_node, const struct lyd_node *tree, const char *xpath,
4445 const struct lyxp_var *vars, struct ly_set **set)
Michal Vaskoccc02342020-05-21 10:09:21 +02004446{
4447 LY_ERR ret = LY_SUCCESS;
Michal Vasko8bd9cc72021-07-16 14:06:10 +02004448 struct lyxp_set xp_set = {0};
Radek Krejcif03a9e22020-09-18 20:09:31 +02004449 struct lyxp_expr *exp = NULL;
Michal Vaskoccc02342020-05-21 10:09:21 +02004450 uint32_t i;
4451
Michal Vaskoe3716b32021-12-13 16:58:25 +01004452 LY_CHECK_ARG_RET(NULL, tree, xpath, set, LY_EINVAL);
Michal Vaskoccc02342020-05-21 10:09:21 +02004453
Michal Vasko37c69432021-04-12 14:48:24 +02004454 *set = NULL;
Michal Vaskoccc02342020-05-21 10:09:21 +02004455
4456 /* compile expression */
Michal Vaskoe3716b32021-12-13 16:58:25 +01004457 ret = lyxp_expr_parse((struct ly_ctx *)LYD_CTX(tree), xpath, 0, 1, &exp);
Radek Krejcif03a9e22020-09-18 20:09:31 +02004458 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskoccc02342020-05-21 10:09:21 +02004459
4460 /* evaluate expression */
Michal Vaskoe3716b32021-12-13 16:58:25 +01004461 ret = lyxp_eval(LYD_CTX(tree), exp, NULL, LY_VALUE_JSON, NULL, ctx_node, tree, vars, &xp_set, LYXP_IGNORE_WHEN);
Michal Vaskoccc02342020-05-21 10:09:21 +02004462 LY_CHECK_GOTO(ret, cleanup);
4463
4464 /* allocate return set */
Radek Krejciba03a5a2020-08-27 14:40:41 +02004465 ret = ly_set_new(set);
4466 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskoccc02342020-05-21 10:09:21 +02004467
4468 /* transform into ly_set */
4469 if (xp_set.type == LYXP_SET_NODE_SET) {
4470 /* allocate memory for all the elements once (even though not all items must be elements but most likely will be) */
4471 (*set)->objs = malloc(xp_set.used * sizeof *(*set)->objs);
Michal Vaskoe3716b32021-12-13 16:58:25 +01004472 LY_CHECK_ERR_GOTO(!(*set)->objs, LOGMEM(LYD_CTX(tree)); ret = LY_EMEM, cleanup);
Michal Vaskoccc02342020-05-21 10:09:21 +02004473 (*set)->size = xp_set.used;
4474
4475 for (i = 0; i < xp_set.used; ++i) {
4476 if (xp_set.val.nodes[i].type == LYXP_NODE_ELEM) {
Radek Krejci3d92e442020-10-12 12:48:13 +02004477 ret = ly_set_add(*set, xp_set.val.nodes[i].node, 1, NULL);
Radek Krejciba03a5a2020-08-27 14:40:41 +02004478 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskoccc02342020-05-21 10:09:21 +02004479 }
4480 }
4481 }
4482
4483cleanup:
Michal Vasko0691d522020-05-21 13:21:47 +02004484 lyxp_set_free_content(&xp_set);
Michal Vaskoe3716b32021-12-13 16:58:25 +01004485 lyxp_expr_free((struct ly_ctx *)LYD_CTX(tree), exp);
Radek Krejci8f45abc2020-11-26 12:15:13 +01004486 if (ret) {
4487 ly_set_free(*set, NULL);
4488 *set = NULL;
4489 }
Michal Vaskoccc02342020-05-21 10:09:21 +02004490 return ret;
4491}
Radek Krejcica989142020-11-05 11:32:22 +01004492
Michal Vasko3e1f6552021-01-14 09:27:55 +01004493API LY_ERR
Michal Vaskoe3716b32021-12-13 16:58:25 +01004494lyd_find_xpath2(const struct lyd_node *ctx_node, const char *xpath, const struct lyxp_var *vars, struct ly_set **set)
4495{
4496 LY_CHECK_ARG_RET(NULL, ctx_node, xpath, set, LY_EINVAL);
4497
4498 return lyd_find_xpath3(ctx_node, ctx_node, xpath, vars, set);
4499}
4500
4501API LY_ERR
aPiecekfba75362021-10-07 12:39:48 +02004502lyd_find_xpath(const struct lyd_node *ctx_node, const char *xpath, struct ly_set **set)
4503{
Michal Vaskoe3716b32021-12-13 16:58:25 +01004504 LY_CHECK_ARG_RET(NULL, ctx_node, xpath, set, LY_EINVAL);
4505
4506 return lyd_find_xpath3(ctx_node, ctx_node, xpath, NULL, set);
aPiecekfba75362021-10-07 12:39:48 +02004507}
4508
4509API LY_ERR
4510lyd_eval_xpath2(const struct lyd_node *ctx_node, const char *xpath, const struct lyxp_var *vars, ly_bool *result)
Michal Vaskoc9eb3ca2021-07-16 10:20:37 +02004511{
4512 LY_ERR ret = LY_SUCCESS;
Michal Vasko8bd9cc72021-07-16 14:06:10 +02004513 struct lyxp_set xp_set = {0};
Michal Vaskoc9eb3ca2021-07-16 10:20:37 +02004514 struct lyxp_expr *exp = NULL;
4515
4516 LY_CHECK_ARG_RET(NULL, ctx_node, xpath, result, LY_EINVAL);
4517
4518 /* compile expression */
4519 ret = lyxp_expr_parse((struct ly_ctx *)LYD_CTX(ctx_node), xpath, 0, 1, &exp);
4520 LY_CHECK_GOTO(ret, cleanup);
4521
4522 /* evaluate expression */
aPiecekfba75362021-10-07 12:39:48 +02004523 ret = lyxp_eval(LYD_CTX(ctx_node), exp, NULL, LY_VALUE_JSON, NULL, ctx_node, ctx_node, vars, &xp_set, LYXP_IGNORE_WHEN);
Michal Vaskoc9eb3ca2021-07-16 10:20:37 +02004524 LY_CHECK_GOTO(ret, cleanup);
4525
4526 /* transform into boolean */
4527 ret = lyxp_set_cast(&xp_set, LYXP_SET_BOOLEAN);
4528 LY_CHECK_GOTO(ret, cleanup);
4529
4530 /* set result */
4531 *result = xp_set.val.bln;
4532
4533cleanup:
4534 lyxp_set_free_content(&xp_set);
4535 lyxp_expr_free((struct ly_ctx *)LYD_CTX(ctx_node), exp);
4536 return ret;
4537}
4538
4539API LY_ERR
aPiecekfba75362021-10-07 12:39:48 +02004540lyd_eval_xpath(const struct lyd_node *ctx_node, const char *xpath, ly_bool *result)
4541{
4542 return lyd_eval_xpath2(ctx_node, xpath, NULL, result);
4543}
4544
4545API LY_ERR
Michal Vasko3e1f6552021-01-14 09:27:55 +01004546lyd_find_path(const struct lyd_node *ctx_node, const char *path, ly_bool output, struct lyd_node **match)
4547{
4548 LY_ERR ret = LY_SUCCESS;
4549 struct lyxp_expr *expr = NULL;
4550 struct ly_path *lypath = NULL;
4551
4552 LY_CHECK_ARG_RET(NULL, ctx_node, ctx_node->schema, path, LY_EINVAL);
4553
4554 /* parse the path */
Michal Vaskoed725d72021-06-23 12:03:45 +02004555 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 +01004556 LY_PATH_PREFIX_OPTIONAL, LY_PATH_PRED_SIMPLE, &expr);
4557 LY_CHECK_GOTO(ret, cleanup);
4558
4559 /* compile the path */
Michal Vaskoed725d72021-06-23 12:03:45 +02004560 ret = ly_path_compile(LYD_CTX(ctx_node), NULL, ctx_node->schema, NULL, expr,
Michal Vasko0884d212021-10-14 09:21:46 +02004561 output ? LY_PATH_OPER_OUTPUT : LY_PATH_OPER_INPUT, LY_PATH_TARGET_SINGLE, 0, LY_VALUE_JSON, NULL, &lypath);
Michal Vasko3e1f6552021-01-14 09:27:55 +01004562 LY_CHECK_GOTO(ret, cleanup);
4563
4564 /* evaluate the path */
4565 ret = ly_path_eval_partial(lypath, ctx_node, NULL, match);
4566
4567cleanup:
4568 lyxp_expr_free(LYD_CTX(ctx_node), expr);
4569 ly_path_free(LYD_CTX(ctx_node), lypath);
4570 return ret;
4571}
4572
Michal Vaskobb22b182021-06-14 08:14:21 +02004573API LY_ERR
4574lyd_find_target(const struct ly_path *path, const struct lyd_node *tree, struct lyd_node **match)
4575{
4576 LY_ERR ret;
4577 struct lyd_node *m;
4578
4579 LY_CHECK_ARG_RET(NULL, path, LY_EINVAL);
4580
4581 ret = ly_path_eval(path, tree, &m);
4582 if (ret) {
4583 if (match) {
4584 *match = NULL;
4585 }
4586 return LY_ENOTFOUND;
4587 }
4588
4589 if (match) {
4590 *match = m;
4591 }
4592 return LY_SUCCESS;
4593}
4594
Radek Krejcica989142020-11-05 11:32:22 +01004595API uint32_t
4596lyd_list_pos(const struct lyd_node *instance)
4597{
4598 const struct lyd_node *iter = NULL;
4599 uint32_t pos = 0;
4600
4601 if (!instance || !(instance->schema->nodetype & (LYS_LIST | LYS_LEAFLIST))) {
4602 return 0;
4603 }
4604
4605 /* data instances are ordered, so we can stop when we found instance of other schema node */
4606 for (iter = instance; iter->schema == instance->schema; iter = iter->prev) {
Radek Krejcibb27df32020-11-13 15:39:16 +01004607 if (pos && (iter->next == NULL)) {
Radek Krejcica989142020-11-05 11:32:22 +01004608 /* overrun to the end of the siblings list */
4609 break;
4610 }
4611 ++pos;
4612 }
4613
4614 return pos;
4615}
Radek Krejci4233f9b2020-11-05 12:38:35 +01004616
4617API struct lyd_node *
4618lyd_first_sibling(const struct lyd_node *node)
4619{
4620 struct lyd_node *start;
4621
4622 if (!node) {
4623 return NULL;
4624 }
4625
4626 /* get the first sibling */
4627 if (node->parent) {
4628 start = node->parent->child;
4629 } else {
Radek Krejcibb27df32020-11-13 15:39:16 +01004630 for (start = (struct lyd_node *)node; start->prev->next; start = start->prev) {}
Radek Krejci4233f9b2020-11-05 12:38:35 +01004631 }
4632
4633 return start;
4634}