blob: a42fa001bfa438419c3848485d2fd9a9261bb832 [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;
874 struct ly_ctx *ctx = parent ? parent->schema->module->ctx : (module ? module->ctx : NULL);
875
Michal Vasko6027eb92020-07-15 16:37:30 +0200876 LY_CHECK_ARG_RET(ctx, parent || module, parent || node, name, LY_EINVAL);
Michal Vasko013a8182020-03-03 10:46:53 +0100877
Michal Vaskof03ed032020-03-04 13:31:44 +0100878 if (!module) {
879 module = parent->schema->module;
880 }
881
Michal Vasko3a41dff2020-07-15 14:30:28 +0200882 schema = lys_find_child(parent ? parent->schema : NULL, module, name, 0,
Radek Krejci41ac9942020-11-02 14:47:56 +0100883 LYS_CONTAINER | LYS_NOTIF | LYS_RPC | LYS_ACTION, output ? LYS_GETNEXT_OUTPUT : 0);
Radek Krejcidd2a7662021-03-12 11:26:34 +0100884 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 +0100885
Michal Vasko3a41dff2020-07-15 14:30:28 +0200886 LY_CHECK_RET(lyd_create_inner(schema, &ret));
887 if (parent) {
Michal Vasko6ee6f432021-07-16 09:49:14 +0200888 lyd_insert_node(parent, NULL, ret, 0);
Michal Vasko013a8182020-03-03 10:46:53 +0100889 }
Michal Vasko3a41dff2020-07-15 14:30:28 +0200890
891 if (node) {
892 *node = ret;
893 }
894 return LY_SUCCESS;
Michal Vasko013a8182020-03-03 10:46:53 +0100895}
896
Michal Vasko3a41dff2020-07-15 14:30:28 +0200897API LY_ERR
Radek Krejcidd2a7662021-03-12 11:26:34 +0100898lyd_new_ext_inner(const struct lysc_ext_instance *ext, const char *name, struct lyd_node **node)
899{
900 struct lyd_node *ret = NULL;
901 const struct lysc_node *schema;
902 struct ly_ctx *ctx = ext ? ext->module->ctx : NULL;
903
904 LY_CHECK_ARG_RET(ctx, ext, node, name, LY_EINVAL);
905
906 schema = lysc_ext_find_node(ext, NULL, name, 0, LYS_CONTAINER | LYS_NOTIF | LYS_RPC | LYS_ACTION, 0);
907 if (!schema) {
908 if (ext->argument) {
909 LOGERR(ctx, LY_EINVAL, "Inner node (not a list) \"%s\" not found in instance \"%s\" of extension %s.",
910 name, ext->argument, ext->def->name);
911 } else {
912 LOGERR(ctx, LY_EINVAL, "Inner node (not a list) \"%s\" not found in instance of extension %s.",
913 name, ext->def->name);
914 }
915 return LY_ENOTFOUND;
916 }
917 LY_CHECK_RET(lyd_create_inner(schema, &ret));
918
919 *node = ret;
920
921 return LY_SUCCESS;
922}
923
Michal Vasko208e6d62021-06-28 11:23:50 +0200924/**
925 * @brief Create a new list node in the data tree.
926 *
927 * @param[in] parent Parent node for the node being created. NULL in case of creating a top level element.
928 * @param[in] module Module of the node being created. If NULL, @p parent module will be used.
929 * @param[in] name Schema node name of the new data node. The node must be #LYS_LIST.
930 * @param[in] format Format of key values.
931 * @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
932 * taken into consideration. Otherwise, the output's data node is going to be created.
933 * @param[out] node Optional created node.
934 * @param[in] ap Ordered key values of the new list instance, all must be set. For ::LY_VALUE_LYB, every value must
935 * be followed by the value length.
936 * @return LY_ERR value.
937 */
938static LY_ERR
939_lyd_new_list(struct lyd_node *parent, const struct lys_module *module, const char *name, LY_VALUE_FORMAT format,
940 ly_bool output, struct lyd_node **node, va_list ap)
Michal Vasko013a8182020-03-03 10:46:53 +0100941{
942 struct lyd_node *ret = NULL, *key;
943 const struct lysc_node *schema, *key_s;
944 struct ly_ctx *ctx = parent ? parent->schema->module->ctx : (module ? module->ctx : NULL);
Michal Vasko208e6d62021-06-28 11:23:50 +0200945 const void *key_val;
946 uint32_t key_len;
Michal Vasko013a8182020-03-03 10:46:53 +0100947 LY_ERR rc = LY_SUCCESS;
948
Michal Vasko6027eb92020-07-15 16:37:30 +0200949 LY_CHECK_ARG_RET(ctx, parent || module, parent || node, name, LY_EINVAL);
Michal Vasko013a8182020-03-03 10:46:53 +0100950
Michal Vaskof03ed032020-03-04 13:31:44 +0100951 if (!module) {
952 module = parent->schema->module;
953 }
954
Radek Krejci41ac9942020-11-02 14:47:56 +0100955 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 +0200956 LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "List node \"%s\" not found.", name), LY_ENOTFOUND);
Michal Vasko013a8182020-03-03 10:46:53 +0100957
958 /* create list inner node */
Michal Vasko3a41dff2020-07-15 14:30:28 +0200959 LY_CHECK_RET(lyd_create_inner(schema, &ret));
Michal Vasko013a8182020-03-03 10:46:53 +0100960
Michal Vasko013a8182020-03-03 10:46:53 +0100961 /* create and insert all the keys */
Michal Vasko544e58a2021-01-28 14:33:41 +0100962 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 +0200963 if (format == LY_VALUE_LYB) {
964 key_val = va_arg(ap, const void *);
965 key_len = va_arg(ap, uint32_t);
966 } else {
967 key_val = va_arg(ap, const char *);
968 key_len = key_val ? strlen((char *)key_val) : 0;
969 }
Michal Vasko013a8182020-03-03 10:46:53 +0100970
Michal Vasko208e6d62021-06-28 11:23:50 +0200971 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 +0200972 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko6ee6f432021-07-16 09:49:14 +0200973 lyd_insert_node(ret, NULL, key, 1);
Michal Vasko013a8182020-03-03 10:46:53 +0100974 }
975
Michal Vasko013a8182020-03-03 10:46:53 +0100976 if (parent) {
Michal Vasko6ee6f432021-07-16 09:49:14 +0200977 lyd_insert_node(parent, NULL, ret, 0);
Michal Vasko013a8182020-03-03 10:46:53 +0100978 }
979
980cleanup:
981 if (rc) {
982 lyd_free_tree(ret);
983 ret = NULL;
Michal Vasko3a41dff2020-07-15 14:30:28 +0200984 } else if (node) {
985 *node = ret;
Michal Vasko013a8182020-03-03 10:46:53 +0100986 }
Michal Vasko3a41dff2020-07-15 14:30:28 +0200987 return rc;
Michal Vasko013a8182020-03-03 10:46:53 +0100988}
989
Michal Vasko3a41dff2020-07-15 14:30:28 +0200990API LY_ERR
Michal Vasko208e6d62021-06-28 11:23:50 +0200991lyd_new_list(struct lyd_node *parent, const struct lys_module *module, const char *name, ly_bool output,
992 struct lyd_node **node, ...)
993{
994 LY_ERR rc;
995 va_list ap;
996
997 va_start(ap, node);
998
999 rc = _lyd_new_list(parent, module, name, LY_VALUE_JSON, output, node, ap);
1000
1001 va_end(ap);
1002 return rc;
1003}
1004
1005API LY_ERR
1006lyd_new_list_bin(struct lyd_node *parent, const struct lys_module *module, const char *name, ly_bool output,
1007 struct lyd_node **node, ...)
1008{
1009 LY_ERR rc;
1010 va_list ap;
1011
1012 va_start(ap, node);
1013
1014 rc = _lyd_new_list(parent, module, name, LY_VALUE_LYB, output, node, ap);
1015
1016 va_end(ap);
1017 return rc;
1018}
1019
1020API LY_ERR
1021lyd_new_list_canon(struct lyd_node *parent, const struct lys_module *module, const char *name, ly_bool output,
1022 struct lyd_node **node, ...)
1023{
1024 LY_ERR rc;
1025 va_list ap;
1026
1027 va_start(ap, node);
1028
1029 rc = _lyd_new_list(parent, module, name, LY_VALUE_CANON, output, node, ap);
1030
1031 va_end(ap);
1032 return rc;
1033}
1034
1035API LY_ERR
Radek Krejci8247bae2021-03-12 11:47:02 +01001036lyd_new_ext_list(const struct lysc_ext_instance *ext, const char *name, struct lyd_node **node, ...)
1037{
1038 struct lyd_node *ret = NULL, *key;
1039 const struct lysc_node *schema, *key_s;
1040 struct ly_ctx *ctx = ext ? ext->module->ctx : NULL;
1041 va_list ap;
1042 const char *key_val;
1043 LY_ERR rc = LY_SUCCESS;
1044
1045 LY_CHECK_ARG_RET(ctx, ext, node, name, LY_EINVAL);
1046
1047 schema = lysc_ext_find_node(ext, NULL, name, 0, LYS_LIST, 0);
1048 if (!schema) {
1049 if (ext->argument) {
1050 LOGERR(ctx, LY_EINVAL, "List node \"%s\" not found in instance \"%s\" of extension %s.",
1051 name, ext->argument, ext->def->name);
1052 } else {
1053 LOGERR(ctx, LY_EINVAL, "List node \"%s\" not found in instance of extension %s.", name, ext->def->name);
1054 }
1055 return LY_ENOTFOUND;
1056 }
1057 /* create list inner node */
1058 LY_CHECK_RET(lyd_create_inner(schema, &ret));
1059
1060 va_start(ap, node);
1061
1062 /* create and insert all the keys */
1063 for (key_s = lysc_node_child(schema); key_s && (key_s->flags & LYS_KEY); key_s = key_s->next) {
1064 key_val = va_arg(ap, const char *);
1065
Radek Krejci8df109d2021-04-23 12:19:08 +02001066 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 +01001067 NULL, &key);
1068 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko6ee6f432021-07-16 09:49:14 +02001069 lyd_insert_node(ret, NULL, key, 1);
Radek Krejci8247bae2021-03-12 11:47:02 +01001070 }
1071
1072cleanup:
1073 va_end(ap);
1074 if (rc) {
1075 lyd_free_tree(ret);
1076 ret = NULL;
Radek Krejci8247bae2021-03-12 11:47:02 +01001077 }
Radek Krejci8919fbd2021-03-15 09:35:25 +01001078 *node = ret;
Radek Krejci8247bae2021-03-12 11:47:02 +01001079 return rc;
1080}
1081
1082API LY_ERR
Michal Vasko3a41dff2020-07-15 14:30:28 +02001083lyd_new_list2(struct lyd_node *parent, const struct lys_module *module, const char *name, const char *keys,
Radek Krejci41ac9942020-11-02 14:47:56 +01001084 ly_bool output, struct lyd_node **node)
Michal Vasko013a8182020-03-03 10:46:53 +01001085{
1086 struct lyd_node *ret = NULL;
1087 const struct lysc_node *schema;
1088 struct ly_ctx *ctx = parent ? parent->schema->module->ctx : (module ? module->ctx : NULL);
1089
Michal Vasko6027eb92020-07-15 16:37:30 +02001090 LY_CHECK_ARG_RET(ctx, parent || module, parent || node, name, LY_EINVAL);
Michal Vasko013a8182020-03-03 10:46:53 +01001091
Michal Vaskof03ed032020-03-04 13:31:44 +01001092 if (!module) {
1093 module = parent->schema->module;
1094 }
Michal Vasko004d3152020-06-11 19:59:22 +02001095 if (!keys) {
1096 keys = "";
1097 }
Michal Vaskof03ed032020-03-04 13:31:44 +01001098
Michal Vasko004d3152020-06-11 19:59:22 +02001099 /* find schema node */
Radek Krejci41ac9942020-11-02 14:47:56 +01001100 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 +02001101 LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "List node \"%s\" not found.", name), LY_ENOTFOUND);
Michal Vasko013a8182020-03-03 10:46:53 +01001102
Michal Vasko004d3152020-06-11 19:59:22 +02001103 if ((schema->flags & LYS_KEYLESS) && !keys[0]) {
1104 /* key-less list */
Michal Vasko3a41dff2020-07-15 14:30:28 +02001105 LY_CHECK_RET(lyd_create_inner(schema, &ret));
Michal Vasko004d3152020-06-11 19:59:22 +02001106 } else {
1107 /* create the list node */
Michal Vasko3a41dff2020-07-15 14:30:28 +02001108 LY_CHECK_RET(lyd_create_list2(schema, keys, strlen(keys), &ret));
Michal Vasko004d3152020-06-11 19:59:22 +02001109 }
Michal Vasko004d3152020-06-11 19:59:22 +02001110 if (parent) {
Michal Vasko6ee6f432021-07-16 09:49:14 +02001111 lyd_insert_node(parent, NULL, ret, 0);
Michal Vasko013a8182020-03-03 10:46:53 +01001112 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02001113
1114 if (node) {
1115 *node = ret;
1116 }
1117 return LY_SUCCESS;
Michal Vasko013a8182020-03-03 10:46:53 +01001118}
1119
Radek Krejci09c77442021-04-26 11:10:34 +02001120/**
1121 * @brief Create a new term node in the data tree.
1122 *
1123 * @param[in] parent Parent node for the node being created. NULL in case of creating a top level element.
1124 * @param[in] module Module of the node being created. If NULL, @p parent module will be used.
1125 * @param[in] name Schema node name of the new data node. The node can be ::LYS_LEAF or ::LYS_LEAFLIST.
1126 * @param[in] value Value of the node being created.
1127 * @param[in] value_len Length of @p value.
1128 * @param[in] format Format of @p value.
1129 * @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
1130 * taken into consideration. Otherwise, the output's data node is going to be created.
1131 * @param[out] node Optional created node.
1132 * @return LY_ERR value.
1133 */
1134static LY_ERR
1135_lyd_new_term(struct lyd_node *parent, const struct lys_module *module, const char *name, const void *value,
1136 size_t value_len, LY_VALUE_FORMAT format, ly_bool output, struct lyd_node **node)
Michal Vasko013a8182020-03-03 10:46:53 +01001137{
Michal Vaskocbff3e92020-05-27 12:56:41 +02001138 LY_ERR rc;
Michal Vasko013a8182020-03-03 10:46:53 +01001139 struct lyd_node *ret = NULL;
1140 const struct lysc_node *schema;
Radek Krejci09c77442021-04-26 11:10:34 +02001141 const struct ly_ctx *ctx = parent ? LYD_CTX(parent) : (module ? module->ctx : NULL);
Michal Vasko013a8182020-03-03 10:46:53 +01001142
Michal Vasko6027eb92020-07-15 16:37:30 +02001143 LY_CHECK_ARG_RET(ctx, parent || module, parent || node, name, LY_EINVAL);
Michal Vasko013a8182020-03-03 10:46:53 +01001144
Michal Vaskof03ed032020-03-04 13:31:44 +01001145 if (!module) {
1146 module = parent->schema->module;
1147 }
1148
Radek Krejci41ac9942020-11-02 14:47:56 +01001149 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 +02001150 LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "Term node \"%s\" not found.", name), LY_ENOTFOUND);
Michal Vasko013a8182020-03-03 10:46:53 +01001151
Radek Krejci09c77442021-04-26 11:10:34 +02001152 rc = lyd_create_term(schema, value, value_len, NULL, format, NULL, LYD_HINT_DATA, NULL, &ret);
Michal Vaskofeca4fb2020-10-05 08:58:40 +02001153 LY_CHECK_RET(rc);
Michal Vaskocbff3e92020-05-27 12:56:41 +02001154 if (parent) {
Michal Vasko6ee6f432021-07-16 09:49:14 +02001155 lyd_insert_node(parent, NULL, ret, 0);
Michal Vasko013a8182020-03-03 10:46:53 +01001156 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02001157
1158 if (node) {
1159 *node = ret;
1160 }
1161 return LY_SUCCESS;
Michal Vasko013a8182020-03-03 10:46:53 +01001162}
1163
Michal Vasko3a41dff2020-07-15 14:30:28 +02001164API LY_ERR
Radek Krejci09c77442021-04-26 11:10:34 +02001165lyd_new_term(struct lyd_node *parent, const struct lys_module *module, const char *name, const char *val_str,
1166 ly_bool output, struct lyd_node **node)
1167{
1168 return _lyd_new_term(parent, module, name, val_str, val_str ? strlen(val_str) : 0, LY_VALUE_JSON, output, node);
1169}
1170
1171API LY_ERR
1172lyd_new_term_bin(struct lyd_node *parent, const struct lys_module *module, const char *name, const void *value,
1173 size_t value_len, ly_bool output, struct lyd_node **node)
1174{
1175 return _lyd_new_term(parent, module, name, value, value_len, LY_VALUE_LYB, output, node);
1176}
1177
1178API LY_ERR
1179lyd_new_term_canon(struct lyd_node *parent, const struct lys_module *module, const char *name, const char *val_str,
1180 ly_bool output, struct lyd_node **node)
1181{
1182 return _lyd_new_term(parent, module, name, val_str, val_str ? strlen(val_str) : 0, LY_VALUE_CANON, output, node);
1183}
1184
1185API LY_ERR
Radek Krejci8a5afc22021-03-12 11:10:47 +01001186lyd_new_ext_term(const struct lysc_ext_instance *ext, const char *name, const char *val_str, struct lyd_node **node)
1187{
1188 LY_ERR rc;
1189 struct lyd_node *ret = NULL;
1190 const struct lysc_node *schema;
1191 struct ly_ctx *ctx = ext ? ext->module->ctx : NULL;
1192
1193 LY_CHECK_ARG_RET(ctx, ext, node, name, LY_EINVAL);
1194
1195 schema = lysc_ext_find_node(ext, NULL, name, 0, LYD_NODE_TERM, 0);
1196 if (!schema) {
1197 if (ext->argument) {
1198 LOGERR(ctx, LY_EINVAL, "Term node \"%s\" not found in instance \"%s\" of extension %s.",
1199 name, ext->argument, ext->def->name);
1200 } else {
1201 LOGERR(ctx, LY_EINVAL, "Term node \"%s\" not found in instance of extension %s.", name, ext->def->name);
1202 }
1203 return LY_ENOTFOUND;
1204 }
Radek Krejci8df109d2021-04-23 12:19:08 +02001205 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 +01001206 LY_CHECK_RET(rc);
1207
1208 *node = ret;
1209
1210 return LY_SUCCESS;
1211}
1212
1213API LY_ERR
Michal Vasko2a4ab2b2021-03-04 15:52:40 +01001214lyd_new_any(struct lyd_node *parent, const struct lys_module *module, const char *name, const void *value,
1215 ly_bool use_value, LYD_ANYDATA_VALUETYPE value_type, ly_bool output, struct lyd_node **node)
Michal Vasko013a8182020-03-03 10:46:53 +01001216{
1217 struct lyd_node *ret = NULL;
1218 const struct lysc_node *schema;
1219 struct ly_ctx *ctx = parent ? parent->schema->module->ctx : (module ? module->ctx : NULL);
1220
Michal Vasko6027eb92020-07-15 16:37:30 +02001221 LY_CHECK_ARG_RET(ctx, parent || module, parent || node, name, LY_EINVAL);
Michal Vasko013a8182020-03-03 10:46:53 +01001222
Michal Vaskof03ed032020-03-04 13:31:44 +01001223 if (!module) {
1224 module = parent->schema->module;
1225 }
1226
Radek Krejci41ac9942020-11-02 14:47:56 +01001227 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 +02001228 LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "Any node \"%s\" not found.", name), LY_ENOTFOUND);
Michal Vasko013a8182020-03-03 10:46:53 +01001229
Michal Vasko2a4ab2b2021-03-04 15:52:40 +01001230 LY_CHECK_RET(lyd_create_any(schema, value, value_type, use_value, &ret));
Michal Vasko3a41dff2020-07-15 14:30:28 +02001231 if (parent) {
Michal Vasko6ee6f432021-07-16 09:49:14 +02001232 lyd_insert_node(parent, NULL, ret, 0);
Michal Vasko013a8182020-03-03 10:46:53 +01001233 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02001234
1235 if (node) {
1236 *node = ret;
1237 }
1238 return LY_SUCCESS;
Michal Vasko013a8182020-03-03 10:46:53 +01001239}
1240
Radek Krejci0b963da2021-03-12 13:23:44 +01001241API LY_ERR
1242lyd_new_ext_any(const struct lysc_ext_instance *ext, const char *name, const void *value, ly_bool use_value,
1243 LYD_ANYDATA_VALUETYPE value_type, struct lyd_node **node)
1244{
1245 struct lyd_node *ret = NULL;
1246 const struct lysc_node *schema;
1247 struct ly_ctx *ctx = ext ? ext->module->ctx : NULL;
1248
1249 LY_CHECK_ARG_RET(ctx, ext, node, name, LY_EINVAL);
1250
1251 schema = lysc_ext_find_node(ext, NULL, name, 0, LYD_NODE_ANY, 0);
1252 if (!schema) {
1253 if (ext->argument) {
1254 LOGERR(ctx, LY_EINVAL, "Any node \"%s\" not found in instance \"%s\" of extension %s.",
1255 name, ext->argument, ext->def->name);
1256 } else {
1257 LOGERR(ctx, LY_EINVAL, "Any node \"%s\" not found in instance of extension %s.", name, ext->def->name);
1258 }
1259 return LY_ENOTFOUND;
1260 }
1261 LY_CHECK_RET(lyd_create_any(schema, value, value_type, use_value, &ret));
1262
1263 *node = ret;
1264
1265 return LY_SUCCESS;
1266}
1267
Michal Vasko3a41dff2020-07-15 14:30:28 +02001268API LY_ERR
Michal Vasko871a0252020-11-11 18:35:24 +01001269lyd_new_meta(const struct ly_ctx *ctx, struct lyd_node *parent, const struct lys_module *module, const char *name,
1270 const char *val_str, ly_bool clear_dflt, struct lyd_meta **meta)
Michal Vaskod86997b2020-05-26 15:19:54 +02001271{
Michal Vaskod86997b2020-05-26 15:19:54 +02001272 const char *prefix, *tmp;
Michal Vaskod86997b2020-05-26 15:19:54 +02001273 size_t pref_len, name_len;
1274
Michal Vasko2b5344c2021-02-26 10:12:05 +01001275 LY_CHECK_ARG_RET(ctx, ctx || parent, name, module || strchr(name, ':'), parent || meta, LY_EINVAL);
1276 if (!ctx) {
1277 ctx = LYD_CTX(parent);
1278 }
Michal Vasko00cbf532020-06-15 13:58:47 +02001279
Michal Vasko871a0252020-11-11 18:35:24 +01001280 if (parent && !parent->schema) {
Michal Vasko33b4fab2021-03-04 15:56:12 +01001281 LOGERR(ctx, LY_EINVAL, "Cannot add metadata \"%s\" to an opaque node \"%s\".", name, LYD_NAME(parent));
Michal Vasko871a0252020-11-11 18:35:24 +01001282 return LY_EINVAL;
1283 }
Michal Vasko610553d2020-11-18 18:15:05 +01001284 if (meta) {
1285 *meta = NULL;
1286 }
Michal Vaskod86997b2020-05-26 15:19:54 +02001287
1288 /* parse the name */
1289 tmp = name;
1290 if (ly_parse_nodeid(&tmp, &prefix, &pref_len, &name, &name_len) || tmp[0]) {
1291 LOGERR(ctx, LY_EINVAL, "Metadata name \"%s\" is not valid.", name);
Michal Vasko871a0252020-11-11 18:35:24 +01001292 return LY_EINVAL;
Michal Vaskod86997b2020-05-26 15:19:54 +02001293 }
1294
1295 /* find the module */
1296 if (prefix) {
Radek Krejci0ad51f12020-07-16 12:08:12 +02001297 module = ly_ctx_get_module_implemented2(ctx, prefix, pref_len);
Radek Krejci422afb12021-03-04 16:38:16 +01001298 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 +02001299 }
1300
1301 /* set value if none */
1302 if (!val_str) {
1303 val_str = "";
1304 }
1305
Radek Krejci8df109d2021-04-23 12:19:08 +02001306 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 +01001307 NULL, LYD_HINT_DATA, clear_dflt, NULL);
1308}
Michal Vasko3a41dff2020-07-15 14:30:28 +02001309
Michal Vaskoba696702020-11-11 19:12:45 +01001310API LY_ERR
1311lyd_new_meta2(const struct ly_ctx *ctx, struct lyd_node *parent, ly_bool clear_dflt, const struct lyd_attr *attr,
1312 struct lyd_meta **meta)
1313{
1314 const struct lys_module *mod;
1315
1316 LY_CHECK_ARG_RET(NULL, ctx, attr, parent || meta, LY_EINVAL);
1317
1318 if (parent && !parent->schema) {
1319 LOGERR(ctx, LY_EINVAL, "Cannot add metadata to an opaque node \"%s\".", ((struct lyd_node_opaq *)parent)->name);
1320 return LY_EINVAL;
Michal Vasko3a41dff2020-07-15 14:30:28 +02001321 }
Michal Vasko610553d2020-11-18 18:15:05 +01001322 if (meta) {
1323 *meta = NULL;
1324 }
Michal Vaskoba696702020-11-11 19:12:45 +01001325
1326 switch (attr->format) {
Radek Krejci8df109d2021-04-23 12:19:08 +02001327 case LY_VALUE_XML:
Michal Vaskoad92b672020-11-12 13:11:31 +01001328 mod = ly_ctx_get_module_implemented_ns(ctx, attr->name.module_ns);
Michal Vaskoba696702020-11-11 19:12:45 +01001329 if (!mod) {
Michal Vaskoad92b672020-11-12 13:11:31 +01001330 LOGERR(ctx, LY_EINVAL, "Module with namespace \"%s\" not found.", attr->name.module_ns);
Michal Vaskoba696702020-11-11 19:12:45 +01001331 return LY_ENOTFOUND;
1332 }
1333 break;
Radek Krejci8df109d2021-04-23 12:19:08 +02001334 case LY_VALUE_JSON:
Michal Vaskoad92b672020-11-12 13:11:31 +01001335 mod = ly_ctx_get_module_implemented(ctx, attr->name.module_name);
Michal Vaskoba696702020-11-11 19:12:45 +01001336 if (!mod) {
Michal Vaskoad92b672020-11-12 13:11:31 +01001337 LOGERR(ctx, LY_EINVAL, "Module \"%s\" not found.", attr->name.module_name);
Michal Vaskoba696702020-11-11 19:12:45 +01001338 return LY_ENOTFOUND;
1339 }
1340 break;
1341 default:
1342 LOGINT_RET(ctx);
1343 }
1344
Michal Vaskoad92b672020-11-12 13:11:31 +01001345 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 +01001346 NULL, attr->format, attr->val_prefix_data, attr->hints, clear_dflt, NULL);
Michal Vaskod86997b2020-05-26 15:19:54 +02001347}
1348
Michal Vasko3a41dff2020-07-15 14:30:28 +02001349API LY_ERR
Michal Vasko00cbf532020-06-15 13:58:47 +02001350lyd_new_opaq(struct lyd_node *parent, const struct ly_ctx *ctx, const char *name, const char *value,
Michal Vasko0ab974d2021-02-24 13:18:26 +01001351 const char *prefix, const char *module_name, struct lyd_node **node)
Michal Vasko00cbf532020-06-15 13:58:47 +02001352{
1353 struct lyd_node *ret = NULL;
Radek Krejci07a55962021-03-02 20:16:43 +01001354
Michal Vasko0ab974d2021-02-24 13:18:26 +01001355 LY_CHECK_ARG_RET(ctx, parent || ctx, parent || node, name, module_name, !prefix || !strcmp(prefix, module_name), LY_EINVAL);
Michal Vasko00cbf532020-06-15 13:58:47 +02001356
1357 if (!ctx) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02001358 ctx = LYD_CTX(parent);
Michal Vasko00cbf532020-06-15 13:58:47 +02001359 }
1360 if (!value) {
1361 value = "";
1362 }
1363
Michal Vasko0ab974d2021-02-24 13:18:26 +01001364 LY_CHECK_RET(lyd_create_opaq(ctx, name, strlen(name), prefix, prefix ? strlen(prefix) : 0, module_name,
Radek Krejci8df109d2021-04-23 12:19:08 +02001365 strlen(module_name), value, strlen(value), NULL, LY_VALUE_JSON, NULL, 0, &ret));
Michal Vasko3a41dff2020-07-15 14:30:28 +02001366 if (parent) {
Michal Vasko6ee6f432021-07-16 09:49:14 +02001367 lyd_insert_node(parent, NULL, ret, 1);
Michal Vasko00cbf532020-06-15 13:58:47 +02001368 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02001369
1370 if (node) {
1371 *node = ret;
1372 }
1373 return LY_SUCCESS;
Michal Vasko00cbf532020-06-15 13:58:47 +02001374}
1375
Michal Vasko3a41dff2020-07-15 14:30:28 +02001376API LY_ERR
Michal Vasko8d65f852021-02-17 11:28:13 +01001377lyd_new_opaq2(struct lyd_node *parent, const struct ly_ctx *ctx, const char *name, const char *value,
Michal Vasko0ab974d2021-02-24 13:18:26 +01001378 const char *prefix, const char *module_ns, struct lyd_node **node)
Michal Vasko8d65f852021-02-17 11:28:13 +01001379{
1380 struct lyd_node *ret = NULL;
1381
1382 LY_CHECK_ARG_RET(ctx, parent || ctx, parent || node, name, module_ns, LY_EINVAL);
1383
1384 if (!ctx) {
1385 ctx = LYD_CTX(parent);
1386 }
1387 if (!value) {
1388 value = "";
1389 }
1390
Michal Vasko0ab974d2021-02-24 13:18:26 +01001391 LY_CHECK_RET(lyd_create_opaq(ctx, name, strlen(name), prefix, prefix ? strlen(prefix) : 0, module_ns,
Radek Krejci8df109d2021-04-23 12:19:08 +02001392 strlen(module_ns), value, strlen(value), NULL, LY_VALUE_XML, NULL, 0, &ret));
Michal Vasko8d65f852021-02-17 11:28:13 +01001393 if (parent) {
Michal Vasko6ee6f432021-07-16 09:49:14 +02001394 lyd_insert_node(parent, NULL, ret, 1);
Michal Vasko8d65f852021-02-17 11:28:13 +01001395 }
1396
1397 if (node) {
1398 *node = ret;
1399 }
1400 return LY_SUCCESS;
1401}
1402
1403API LY_ERR
1404lyd_new_attr(struct lyd_node *parent, const char *module_name, const char *name, const char *value,
Radek Krejci0f969882020-08-21 16:56:47 +02001405 struct lyd_attr **attr)
Michal Vasko00cbf532020-06-15 13:58:47 +02001406{
Radek Krejci1798aae2020-07-14 13:26:06 +02001407 struct lyd_attr *ret = NULL;
Michal Vasko00cbf532020-06-15 13:58:47 +02001408 const struct ly_ctx *ctx;
1409 const char *prefix, *tmp;
Michal Vasko51d21b82020-11-13 18:03:54 +01001410 size_t pref_len, name_len, mod_len;
Michal Vasko00cbf532020-06-15 13:58:47 +02001411
Michal Vasko3a41dff2020-07-15 14:30:28 +02001412 LY_CHECK_ARG_RET(NULL, parent, !parent->schema, name, LY_EINVAL);
Michal Vasko00cbf532020-06-15 13:58:47 +02001413
Michal Vaskob7be7a82020-08-20 09:09:04 +02001414 ctx = LYD_CTX(parent);
Michal Vasko00cbf532020-06-15 13:58:47 +02001415
1416 /* parse the name */
1417 tmp = name;
1418 if (ly_parse_nodeid(&tmp, &prefix, &pref_len, &name, &name_len) || tmp[0]) {
Michal Vaskob69b68c2021-02-17 11:18:16 +01001419 LOGERR(ctx, LY_EINVAL, "Attribute name \"%s\" is not valid.", name);
Michal Vasko3a41dff2020-07-15 14:30:28 +02001420 return LY_EVALID;
Michal Vasko00cbf532020-06-15 13:58:47 +02001421 }
1422
Michal Vaskoe137fc42021-07-22 11:53:13 +02001423 if ((pref_len == 3) && !strncmp(prefix, "xml", 3)) {
1424 /* not a prefix but special name */
1425 name = prefix;
1426 name_len += 1 + pref_len;
1427 prefix = NULL;
1428 pref_len = 0;
1429 }
1430
Michal Vasko51d21b82020-11-13 18:03:54 +01001431 /* get the module */
1432 if (module_name) {
1433 mod_len = strlen(module_name);
1434 } else {
1435 module_name = prefix;
1436 mod_len = pref_len;
1437 }
1438
Michal Vasko00cbf532020-06-15 13:58:47 +02001439 /* set value if none */
Michal Vasko8d65f852021-02-17 11:28:13 +01001440 if (!value) {
1441 value = "";
Michal Vasko00cbf532020-06-15 13:58:47 +02001442 }
1443
Michal Vasko8d65f852021-02-17 11:28:13 +01001444 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 +02001445 strlen(value), NULL, LY_VALUE_JSON, NULL, LYD_HINT_DATA));
Michal Vasko8d65f852021-02-17 11:28:13 +01001446
1447 if (attr) {
1448 *attr = ret;
1449 }
1450 return LY_SUCCESS;
1451}
1452
1453API LY_ERR
1454lyd_new_attr2(struct lyd_node *parent, const char *module_ns, const char *name, const char *value,
1455 struct lyd_attr **attr)
1456{
1457 struct lyd_attr *ret = NULL;
1458 const struct ly_ctx *ctx;
1459 const char *prefix, *tmp;
1460 size_t pref_len, name_len;
1461
1462 LY_CHECK_ARG_RET(NULL, parent, !parent->schema, name, LY_EINVAL);
1463
1464 ctx = LYD_CTX(parent);
1465
1466 /* parse the name */
1467 tmp = name;
1468 if (ly_parse_nodeid(&tmp, &prefix, &pref_len, &name, &name_len) || tmp[0]) {
1469 LOGERR(ctx, LY_EINVAL, "Attribute name \"%s\" is not valid.", name);
1470 return LY_EVALID;
1471 }
1472
Michal Vaskoe137fc42021-07-22 11:53:13 +02001473 if ((pref_len == 3) && !strncmp(prefix, "xml", 3)) {
1474 /* not a prefix but special name */
1475 name = prefix;
1476 name_len += 1 + pref_len;
1477 prefix = NULL;
1478 pref_len = 0;
1479 }
1480
Michal Vasko8d65f852021-02-17 11:28:13 +01001481 /* set value if none */
1482 if (!value) {
1483 value = "";
1484 }
1485
1486 LY_CHECK_RET(lyd_create_attr(parent, &ret, ctx, name, name_len, prefix, pref_len, module_ns,
Radek Krejci8df109d2021-04-23 12:19:08 +02001487 module_ns ? strlen(module_ns) : 0, value, strlen(value), NULL, LY_VALUE_XML, NULL, LYD_HINT_DATA));
Michal Vasko3a41dff2020-07-15 14:30:28 +02001488
1489 if (attr) {
1490 *attr = ret;
1491 }
1492 return LY_SUCCESS;
Michal Vasko00cbf532020-06-15 13:58:47 +02001493}
1494
Radek Krejci09c77442021-04-26 11:10:34 +02001495/**
1496 * @brief Change the value of a term (leaf or leaf-list) node.
1497 *
1498 * Node changed this way is always considered explicitly set, meaning its default flag
1499 * is always cleared.
1500 *
1501 * @param[in] term Term node to change.
1502 * @param[in] value New value to set.
1503 * @param[in] value_len Length of @p value.
1504 * @param[in] format Format of @p value.
1505 * @return LY_SUCCESS if value was changed,
1506 * @return LY_EEXIST if value was the same and only the default flag was cleared,
1507 * @return LY_ENOT if the values were equal and no change occured,
1508 * @return LY_ERR value on other errors.
1509 */
1510static LY_ERR
1511_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 +02001512{
1513 LY_ERR ret = LY_SUCCESS;
1514 struct lysc_type *type;
1515 struct lyd_node_term *t;
1516 struct lyd_node *parent;
1517 struct lyd_value val = {0};
Radek Krejci857189e2020-09-01 13:26:36 +02001518 ly_bool dflt_change, val_change;
Michal Vasko00cbf532020-06-15 13:58:47 +02001519
Radek Krejci09c77442021-04-26 11:10:34 +02001520 assert(term && term->schema && (term->schema->nodetype & LYD_NODE_TERM));
Michal Vasko00cbf532020-06-15 13:58:47 +02001521
Michal Vasko00cbf532020-06-15 13:58:47 +02001522 t = (struct lyd_node_term *)term;
1523 type = ((struct lysc_node_leaf *)term->schema)->type;
1524
1525 /* parse the new value */
Radek Krejciddace2c2021-01-08 11:30:56 +01001526 LOG_LOCSET(term->schema, term, NULL, NULL);
Radek Krejci09c77442021-04-26 11:10:34 +02001527 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 +01001528 LOG_LOCBACK(term->schema ? 1 : 0, 1, 0, 0);
Michal Vaskofeca4fb2020-10-05 08:58:40 +02001529 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko00cbf532020-06-15 13:58:47 +02001530
1531 /* compare original and new value */
1532 if (type->plugin->compare(&t->value, &val)) {
1533 /* values differ, switch them */
Michal Vaskob7be7a82020-08-20 09:09:04 +02001534 type->plugin->free(LYD_CTX(term), &t->value);
Michal Vasko00cbf532020-06-15 13:58:47 +02001535 t->value = val;
1536 memset(&val, 0, sizeof val);
1537 val_change = 1;
1538 } else {
1539 val_change = 0;
1540 }
1541
1542 /* always clear the default flag */
1543 if (term->flags & LYD_DEFAULT) {
Michal Vasko9e685082021-01-29 14:49:09 +01001544 for (parent = term; parent; parent = lyd_parent(parent)) {
Michal Vasko00cbf532020-06-15 13:58:47 +02001545 parent->flags &= ~LYD_DEFAULT;
1546 }
1547 dflt_change = 1;
1548 } else {
1549 dflt_change = 0;
1550 }
1551
1552 if (val_change || dflt_change) {
1553 /* make the node non-validated */
1554 term->flags &= LYD_NEW;
1555 }
1556
1557 if (val_change) {
1558 if (term->schema->nodetype == LYS_LEAFLIST) {
1559 /* leaf-list needs to be hashed again and re-inserted into parent */
1560 lyd_unlink_hash(term);
1561 lyd_hash(term);
1562 LY_CHECK_GOTO(ret = lyd_insert_hash(term), cleanup);
1563 } else if ((term->schema->flags & LYS_KEY) && term->parent) {
1564 /* list needs to be updated if its key was changed */
1565 assert(term->parent->schema->nodetype == LYS_LIST);
Michal Vasko9e685082021-01-29 14:49:09 +01001566 lyd_unlink_hash(lyd_parent(term));
1567 lyd_hash(lyd_parent(term));
1568 LY_CHECK_GOTO(ret = lyd_insert_hash(lyd_parent(term)), cleanup);
Michal Vasko00cbf532020-06-15 13:58:47 +02001569 } /* else leaf that is not a key, its value is not used for its hash so it does not change */
1570 }
1571
1572 /* retrun value */
1573 if (!val_change) {
1574 if (dflt_change) {
1575 /* only default flag change */
1576 ret = LY_EEXIST;
1577 } else {
1578 /* no change */
1579 ret = LY_ENOT;
1580 }
1581 } /* else value changed, LY_SUCCESS */
1582
1583cleanup:
Michal Vasko59512fc2020-12-09 18:13:29 +01001584 if (val.realtype) {
1585 type->plugin->free(LYD_CTX(term), &val);
1586 }
Michal Vasko00cbf532020-06-15 13:58:47 +02001587 return ret;
1588}
1589
Michal Vasko41586352020-07-13 13:54:25 +02001590API LY_ERR
Radek Krejci09c77442021-04-26 11:10:34 +02001591lyd_change_term(struct lyd_node *term, const char *val_str)
1592{
1593 LY_CHECK_ARG_RET(NULL, term, term->schema, term->schema->nodetype & LYD_NODE_TERM, LY_EINVAL);
1594
1595 return _lyd_change_term(term, val_str, val_str ? strlen(val_str) : 0, LY_VALUE_JSON);
1596}
1597
1598API LY_ERR
1599lyd_change_term_bin(struct lyd_node *term, const void *value, size_t value_len)
1600{
1601 LY_CHECK_ARG_RET(NULL, term, term->schema, term->schema->nodetype & LYD_NODE_TERM, LY_EINVAL);
1602
1603 return _lyd_change_term(term, value, value_len, LY_VALUE_LYB);
1604}
1605
1606API LY_ERR
1607lyd_change_term_canon(struct lyd_node *term, const char *val_str)
1608{
1609 LY_CHECK_ARG_RET(NULL, term, term->schema, term->schema->nodetype & LYD_NODE_TERM, LY_EINVAL);
1610
1611 return _lyd_change_term(term, val_str, val_str ? strlen(val_str) : 0, LY_VALUE_CANON);
1612}
1613
1614API LY_ERR
Michal Vasko41586352020-07-13 13:54:25 +02001615lyd_change_meta(struct lyd_meta *meta, const char *val_str)
1616{
1617 LY_ERR ret = LY_SUCCESS;
Radek Krejci2b18bf12020-11-06 11:20:20 +01001618 struct lyd_meta *m2 = NULL;
Michal Vasko41586352020-07-13 13:54:25 +02001619 struct lyd_value val;
Radek Krejci857189e2020-09-01 13:26:36 +02001620 ly_bool val_change;
Michal Vasko41586352020-07-13 13:54:25 +02001621
1622 LY_CHECK_ARG_RET(NULL, meta, LY_EINVAL);
1623
1624 if (!val_str) {
1625 val_str = "";
1626 }
1627
1628 /* parse the new value into a new meta structure */
1629 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 +02001630 strlen(val_str), NULL, LY_VALUE_JSON, NULL, LYD_HINT_DATA, 0, NULL), cleanup);
Michal Vasko41586352020-07-13 13:54:25 +02001631
1632 /* compare original and new value */
1633 if (lyd_compare_meta(meta, m2)) {
1634 /* values differ, switch them */
1635 val = meta->value;
1636 meta->value = m2->value;
1637 m2->value = val;
1638 val_change = 1;
1639 } else {
1640 val_change = 0;
1641 }
1642
1643 /* retrun value */
1644 if (!val_change) {
1645 /* no change */
1646 ret = LY_ENOT;
1647 } /* else value changed, LY_SUCCESS */
1648
1649cleanup:
Michal Vasko1a66a5d2020-11-18 18:15:37 +01001650 lyd_free_meta_single(m2);
Michal Vasko41586352020-07-13 13:54:25 +02001651 return ret;
1652}
1653
Radek Krejci09c77442021-04-26 11:10:34 +02001654/**
1655 * @brief Update node value.
1656 *
1657 * @param[in] node Node to update.
1658 * @param[in] value New value to set.
1659 * @param[in] value_len Length of @p value.
1660 * @param[in] value_type Type of @p value for anydata/anyxml node.
1661 * @param[in] format Format of @p value.
1662 * @param[out] new_parent Set to @p node if the value was updated, otherwise set to NULL.
1663 * @param[out] new_node Set to @p node if the value was updated, otherwise set to NULL.
1664 * @return LY_ERR value.
1665 */
Michal Vasko6741dc62021-02-04 09:27:45 +01001666static LY_ERR
Radek Krejci09c77442021-04-26 11:10:34 +02001667lyd_new_path_update(struct lyd_node *node, const void *value, size_t value_len, LYD_ANYDATA_VALUETYPE value_type,
1668 LY_VALUE_FORMAT format, struct lyd_node **new_parent, struct lyd_node **new_node)
1669{
1670 LY_ERR ret = LY_SUCCESS;
1671 struct lyd_node *new_any;
1672
1673 switch (node->schema->nodetype) {
1674 case LYS_CONTAINER:
1675 case LYS_NOTIF:
1676 case LYS_RPC:
1677 case LYS_ACTION:
1678 case LYS_LIST:
1679 /* if it exists, there is nothing to update */
1680 *new_parent = NULL;
1681 *new_node = NULL;
1682 break;
1683 case LYS_LEAFLIST:
1684 if (!lysc_is_dup_inst_list(node->schema)) {
1685 /* if it exists, there is nothing to update */
1686 *new_parent = NULL;
1687 *new_node = NULL;
1688 break;
1689 }
1690 /* fallthrough */
1691 case LYS_LEAF:
1692 ret = _lyd_change_term(node, value, value_len, format);
1693 if ((ret == LY_SUCCESS) || (ret == LY_EEXIST)) {
1694 /* there was an actual change (at least of the default flag) */
1695 *new_parent = node;
1696 *new_node = node;
1697 ret = LY_SUCCESS;
1698 } else if (ret == LY_ENOT) {
1699 /* no change */
1700 *new_parent = NULL;
1701 *new_node = NULL;
1702 ret = LY_SUCCESS;
1703 } /* else error */
1704 break;
1705 case LYS_ANYDATA:
1706 case LYS_ANYXML:
1707 /* create a new any node */
1708 LY_CHECK_RET(lyd_create_any(node->schema, value, value_type, 0, &new_any));
1709
1710 /* compare with the existing one */
1711 if (lyd_compare_single(node, new_any, 0)) {
1712 /* not equal, switch values (so that we can use generic node free) */
1713 ((struct lyd_node_any *)new_any)->value = ((struct lyd_node_any *)node)->value;
1714 ((struct lyd_node_any *)new_any)->value_type = ((struct lyd_node_any *)node)->value_type;
1715 ((struct lyd_node_any *)node)->value.str = value;
1716 ((struct lyd_node_any *)node)->value_type = value_type;
1717
1718 *new_parent = node;
1719 *new_node = node;
1720 } else {
1721 /* they are equal */
1722 *new_parent = NULL;
1723 *new_node = NULL;
1724 }
1725 lyd_free_tree(new_any);
1726 break;
1727 default:
1728 LOGINT(LYD_CTX(node));
1729 ret = LY_EINT;
1730 break;
1731 }
1732
1733 return ret;
1734}
1735
1736static LY_ERR
1737lyd_new_path_check_find_lypath(struct ly_path *path, const char *str_path, const char *value, size_t value_len,
1738 LY_VALUE_FORMAT format, uint32_t options)
Michal Vasko6741dc62021-02-04 09:27:45 +01001739{
Michal Vaskoe78faec2021-04-08 17:24:43 +02001740 LY_ERR r;
Michal Vasko6741dc62021-02-04 09:27:45 +01001741 struct ly_path_predicate *pred;
Michal Vaskoe78faec2021-04-08 17:24:43 +02001742 const struct lysc_node *schema = NULL;
1743 LY_ARRAY_COUNT_TYPE u, new_count;
1744 int create = 0;
Michal Vasko6741dc62021-02-04 09:27:45 +01001745
1746 assert(path);
1747
1748 /* go through all the compiled nodes */
1749 LY_ARRAY_FOR(path, u) {
1750 schema = path[u].node;
Michal Vaskoe78faec2021-04-08 17:24:43 +02001751
1752 if (lysc_is_dup_inst_list(schema)) {
1753 if (path[u].pred_type == LY_PATH_PREDTYPE_NONE) {
1754 /* creating a new key-less list or state leaf-list instance */
1755 create = 1;
1756 new_count = u;
1757 } else if (path[u].pred_type != LY_PATH_PREDTYPE_POSITION) {
1758 LOG_LOCSET(schema, NULL, NULL, NULL);
1759 LOGVAL(schema->module->ctx, LYVE_XPATH, "Invalid predicate for %s \"%s\" in path \"%s\".",
1760 lys_nodetype2str(schema->nodetype), schema->name, str_path);
1761 LOG_LOCBACK(1, 0, 0, 0);
1762 return LY_EINVAL;
1763 }
1764 } else if ((schema->nodetype == LYS_LIST) && (path[u].pred_type != LY_PATH_PREDTYPE_LIST)) {
1765 if ((u < LY_ARRAY_COUNT(path) - 1) || !(options & LYD_NEW_PATH_OPAQ)) {
Michal Vasko6741dc62021-02-04 09:27:45 +01001766 LOG_LOCSET(schema, NULL, NULL, NULL);
1767 LOGVAL(schema->module->ctx, LYVE_XPATH, "Predicate missing for %s \"%s\" in path \"%s\".",
1768 lys_nodetype2str(schema->nodetype), schema->name, str_path);
1769 LOG_LOCBACK(1, 0, 0, 0);
1770 return LY_EINVAL;
Michal Vaskoe78faec2021-04-08 17:24:43 +02001771 } /* else creating an opaque list */
1772 } else if ((schema->nodetype == LYS_LEAFLIST) && (path[u].pred_type != LY_PATH_PREDTYPE_LEAFLIST)) {
Michal Vaskoe78faec2021-04-08 17:24:43 +02001773 r = LY_SUCCESS;
1774 if (options & LYD_NEW_PATH_OPAQ) {
Radek Krejci09c77442021-04-26 11:10:34 +02001775 r = lyd_value_validate(NULL, schema, value, value_len, NULL, NULL, NULL);
Michal Vaskoe78faec2021-04-08 17:24:43 +02001776 }
1777 if (!r) {
1778 /* store the new predicate so that it is used when searching for this instance */
1779 path[u].pred_type = LY_PATH_PREDTYPE_LEAFLIST;
1780 LY_ARRAY_NEW_RET(schema->module->ctx, path[u].predicates, pred, LY_EMEM);
1781
1782 LY_CHECK_RET(lyd_value_store(schema->module->ctx, &pred->value,
Radek Krejci09c77442021-04-26 11:10:34 +02001783 ((struct lysc_node_leaflist *)schema)->type, value, value_len, NULL, format, NULL,
Michal Vaskoe78faec2021-04-08 17:24:43 +02001784 LYD_HINT_DATA, schema, NULL));
1785 ++((struct lysc_type *)pred->value.realtype)->refcount;
1786 } /* else we have opaq flag and the value is not valid, leave no predicate and then create an opaque node */
1787 }
Michal Vasko6741dc62021-02-04 09:27:45 +01001788 }
1789
Michal Vaskoe78faec2021-04-08 17:24:43 +02001790 if (create) {
1791 /* hide the nodes that should always be created so they are not found */
1792 while (new_count < LY_ARRAY_COUNT(path)) {
1793 LY_ARRAY_DECREMENT(path);
1794 }
Michal Vasko6741dc62021-02-04 09:27:45 +01001795 }
1796
Michal Vaskoe78faec2021-04-08 17:24:43 +02001797 return LY_SUCCESS;
Michal Vasko6741dc62021-02-04 09:27:45 +01001798}
1799
Radek Krejci95ccd1b2021-03-12 14:57:22 +01001800/**
1801 * @brief Create a new node in the data tree based on a path. All node types can be created.
1802 *
Michal Vaskoa918a632021-07-02 11:53:36 +02001803 * If @p path points to a list key, the key value from the predicate is used and @p value is ignored.
1804 * Also, if a leaf-list is being created and both a predicate is defined in @p path
Radek Krejci95ccd1b2021-03-12 14:57:22 +01001805 * and @p value is set, the predicate is preferred.
1806 *
1807 * For key-less lists and state leaf-lists, positional predicates can be used. If no preciate is used for these
1808 * nodes, they are always created.
1809 *
1810 * @param[in] parent Data parent to add to/modify, can be NULL. Note that in case a first top-level sibling is used,
1811 * it may no longer be first if @p path is absolute and starts with a non-existing top-level node inserted
1812 * before @p parent. Use ::lyd_first_sibling() to adjust @p parent in these cases.
1813 * @param[in] ctx libyang context, must be set if @p parent is NULL.
1814 * @param[in] ext Extension instance where the node being created is defined. This argument takes effect only for absolute
1815 * path or when the relative paths touches document root (top-level). In such cases the present extension instance replaces
1816 * searching for the appropriate module.
1817 * @param[in] path [Path](@ref howtoXPath) to create.
Radek Krejci09c77442021-04-26 11:10:34 +02001818 * @param[in] value Value of the new leaf/leaf-list (const char *) in ::LY_VALUE_JSON format. If creating an
1819 * anyxml/anydata node, the expected type depends on @p value_type. For other node types, it should be NULL.
1820 * @param[in] value_len Length of @p value in bytes. May be 0 if @p value is a zero-terminated string. Ignored when
1821 * creating anyxml/anydata nodes.
Radek Krejci95ccd1b2021-03-12 14:57:22 +01001822 * @param[in] value_type Anyxml/anydata node @p value type.
1823 * @param[in] options Bitmask of options, see @ref pathoptions.
1824 * @param[out] new_parent Optional first parent node created. If only one node was created, equals to @p new_node.
1825 * @param[out] new_node Optional last node created.
1826 * @return LY_ERR value.
1827 */
1828static LY_ERR
1829lyd_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 +02001830 const void *value, size_t value_len, LYD_ANYDATA_VALUETYPE value_type, uint32_t options,
Radek Krejci95ccd1b2021-03-12 14:57:22 +01001831 struct lyd_node **new_parent, struct lyd_node **new_node)
Michal Vasko00cbf532020-06-15 13:58:47 +02001832{
1833 LY_ERR ret = LY_SUCCESS, r;
1834 struct lyxp_expr *exp = NULL;
1835 struct ly_path *p = NULL;
1836 struct lyd_node *nparent = NULL, *nnode = NULL, *node = NULL, *cur_parent;
1837 const struct lysc_node *schema;
Michal Vaskoe78faec2021-04-08 17:24:43 +02001838 const struct lyd_value *val = NULL;
Radek Krejci910d0132021-03-05 14:19:31 +01001839 LY_ARRAY_COUNT_TYPE path_idx = 0, orig_count = 0;
Radek Krejci09c77442021-04-26 11:10:34 +02001840 LY_VALUE_FORMAT format;
Michal Vasko00cbf532020-06-15 13:58:47 +02001841
Radek Krejci95ccd1b2021-03-12 14:57:22 +01001842 assert(parent || ctx);
Radek Krejci09c77442021-04-26 11:10:34 +02001843 assert(path && ((path[0] == '/') || parent));
1844 assert(!(options & LYD_NEW_PATH_BIN_VALUE) || !(options & LYD_NEW_PATH_CANON_VALUE));
Michal Vasko00cbf532020-06-15 13:58:47 +02001845
1846 if (!ctx) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02001847 ctx = LYD_CTX(parent);
Michal Vasko00cbf532020-06-15 13:58:47 +02001848 }
Radek Krejci09c77442021-04-26 11:10:34 +02001849 if (value && !value_len) {
1850 value_len = strlen(value);
1851 }
1852 if (options & LYD_NEW_PATH_BIN_VALUE) {
1853 format = LY_VALUE_LYB;
1854 } else if (options & LYD_NEW_PATH_CANON_VALUE) {
1855 format = LY_VALUE_CANON;
1856 } else {
1857 format = LY_VALUE_JSON;
1858 }
Michal Vasko00cbf532020-06-15 13:58:47 +02001859
1860 /* parse path */
Michal Vaskoed725d72021-06-23 12:03:45 +02001861 LY_CHECK_GOTO(ret = ly_path_parse(ctx, NULL, path, strlen(path), 0, LY_PATH_BEGIN_EITHER, LY_PATH_PREFIX_OPTIONAL,
1862 LY_PATH_PRED_SIMPLE, &exp), cleanup);
Michal Vasko00cbf532020-06-15 13:58:47 +02001863
1864 /* compile path */
Michal Vasko106f0862021-11-02 11:49:27 +01001865 LY_CHECK_GOTO(ret = ly_path_compile(ctx, NULL, lyd_node_schema(parent), ext, exp, options & LYD_NEW_PATH_OUTPUT ?
1866 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 +02001867
Michal Vasko6741dc62021-02-04 09:27:45 +01001868 /* check the compiled path before searching existing nodes, it may be shortened */
1869 orig_count = LY_ARRAY_COUNT(p);
Radek Krejci09c77442021-04-26 11:10:34 +02001870 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 +02001871
1872 /* try to find any existing nodes in the path */
1873 if (parent) {
1874 ret = ly_path_eval_partial(p, parent, &path_idx, &node);
1875 if (ret == LY_SUCCESS) {
Michal Vasko6741dc62021-02-04 09:27:45 +01001876 if (orig_count == LY_ARRAY_COUNT(p)) {
1877 /* the node exists, are we supposed to update it or is it just a default? */
1878 if (!(options & LYD_NEW_PATH_UPDATE) && !(node->flags & LYD_DEFAULT)) {
1879 LOG_LOCSET(NULL, node, NULL, NULL);
1880 LOGVAL(ctx, LYVE_REFERENCE, "Path \"%s\" already exists", path);
1881 LOG_LOCBACK(0, 1, 0, 0);
1882 ret = LY_EEXIST;
1883 goto cleanup;
1884 }
Michal Vasko00cbf532020-06-15 13:58:47 +02001885
Michal Vasko6741dc62021-02-04 09:27:45 +01001886 /* update the existing node */
Radek Krejci09c77442021-04-26 11:10:34 +02001887 ret = lyd_new_path_update(node, value, value_len, value_type, format, &nparent, &nnode);
Michal Vasko6741dc62021-02-04 09:27:45 +01001888 goto cleanup;
1889 } /* else we were not searching for the whole path */
Michal Vasko00cbf532020-06-15 13:58:47 +02001890 } else if (ret == LY_EINCOMPLETE) {
1891 /* some nodes were found, adjust the iterator to the next segment */
1892 ++path_idx;
1893 } else if (ret == LY_ENOTFOUND) {
1894 /* 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 +01001895 if (lysc_data_parent(p[0].node)) {
Michal Vasko00cbf532020-06-15 13:58:47 +02001896 node = parent;
1897 }
1898 } else {
1899 /* error */
1900 goto cleanup;
1901 }
1902 }
1903
Michal Vasko6741dc62021-02-04 09:27:45 +01001904 /* restore the full path for creating new nodes */
1905 while (orig_count > LY_ARRAY_COUNT(p)) {
1906 LY_ARRAY_INCREMENT(p);
1907 }
1908
Michal Vasko00cbf532020-06-15 13:58:47 +02001909 /* create all the non-existing nodes in a loop */
Michal Vaskod989ba02020-08-24 10:59:24 +02001910 for ( ; path_idx < LY_ARRAY_COUNT(p); ++path_idx) {
Michal Vasko00cbf532020-06-15 13:58:47 +02001911 cur_parent = node;
1912 schema = p[path_idx].node;
1913
1914 switch (schema->nodetype) {
1915 case LYS_LIST:
Michal Vaskoe78faec2021-04-08 17:24:43 +02001916 if (lysc_is_dup_inst_list(schema)) {
1917 /* create key-less list instance */
1918 LY_CHECK_GOTO(ret = lyd_create_inner(schema, &node), cleanup);
1919 } else if ((options & LYD_NEW_PATH_OPAQ) && (p[path_idx].pred_type == LY_PATH_PREDTYPE_NONE)) {
1920 /* creating opaque list without keys */
1921 LY_CHECK_GOTO(ret = lyd_create_opaq(ctx, schema->name, strlen(schema->name), NULL, 0,
Radek Krejci8df109d2021-04-23 12:19:08 +02001922 schema->module->name, strlen(schema->module->name), NULL, 0, NULL, LY_VALUE_JSON, NULL,
Michal Vaskoe78faec2021-04-08 17:24:43 +02001923 LYD_NODEHINT_LIST, &node), cleanup);
1924 } else {
1925 /* create standard list instance */
1926 assert(p[path_idx].pred_type == LY_PATH_PREDTYPE_LIST);
1927 LY_CHECK_GOTO(ret = lyd_create_list(schema, p[path_idx].predicates, &node), cleanup);
Michal Vasko00cbf532020-06-15 13:58:47 +02001928 }
Michal Vaskoe78faec2021-04-08 17:24:43 +02001929 break;
Michal Vasko00cbf532020-06-15 13:58:47 +02001930 case LYS_CONTAINER:
1931 case LYS_NOTIF:
1932 case LYS_RPC:
1933 case LYS_ACTION:
1934 LY_CHECK_GOTO(ret = lyd_create_inner(schema, &node), cleanup);
1935 break;
1936 case LYS_LEAFLIST:
Michal Vaskoe78faec2021-04-08 17:24:43 +02001937 if ((options & LYD_NEW_PATH_OPAQ) && (p[path_idx].pred_type != LY_PATH_PREDTYPE_LEAFLIST)) {
1938 /* we have not checked this only for dup-inst lists, otherwise it must be opaque */
1939 r = LY_EVALID;
1940 if (lysc_is_dup_inst_list(schema)) {
1941 /* validate value */
Radek Krejci09c77442021-04-26 11:10:34 +02001942 r = lyd_value_validate(NULL, schema, value ? value : "", value_len, NULL, NULL, NULL);
Michal Vaskoe78faec2021-04-08 17:24:43 +02001943 }
1944 if (r && (r != LY_EINCOMPLETE)) {
1945 /* creating opaque leaf-list */
Radek Krejci09c77442021-04-26 11:10:34 +02001946 LY_CHECK_GOTO(ret = lyd_create_opaq(ctx, schema->name, strlen(schema->name), value, value_len,
1947 schema->module->name, strlen(schema->module->name), NULL, 0, NULL, format, NULL,
1948 LYD_NODEHINT_LEAFLIST, &node), cleanup);
Michal Vaskoe78faec2021-04-08 17:24:43 +02001949 break;
1950 }
1951 }
1952
1953 /* get value to set */
1954 if (p[path_idx].pred_type == LY_PATH_PREDTYPE_LEAFLIST) {
1955 val = &p[path_idx].predicates[0].value;
Michal Vaskoe78faec2021-04-08 17:24:43 +02001956 }
1957
1958 /* create a leaf-list instance */
1959 if (val) {
Michal Vasko00cbf532020-06-15 13:58:47 +02001960 LY_CHECK_GOTO(ret = lyd_create_term2(schema, &p[path_idx].predicates[0].value, &node), cleanup);
Michal Vaskoe78faec2021-04-08 17:24:43 +02001961 } else {
Radek Krejci09c77442021-04-26 11:10:34 +02001962 LY_CHECK_GOTO(ret = lyd_create_term(schema, value, value_len, NULL, format, NULL, LYD_HINT_DATA,
1963 NULL, &node), cleanup);
Michal Vasko00cbf532020-06-15 13:58:47 +02001964 }
1965 break;
1966 case LYS_LEAF:
Michal Vasko106f0862021-11-02 11:49:27 +01001967 if (lysc_is_key(schema) && cur_parent->schema) {
Michal Vasko35880332020-12-08 13:08:12 +01001968 /* it must have been already created or some error will occur later */
Michal Vaskoa918a632021-07-02 11:53:36 +02001969 lyd_find_sibling_schema(lyd_child(cur_parent), schema, &node);
1970 assert(node);
Michal Vasko35880332020-12-08 13:08:12 +01001971 goto next_iter;
1972 }
1973
Michal Vaskoe78faec2021-04-08 17:24:43 +02001974 if (options & LYD_NEW_PATH_OPAQ) {
Michal Vasko106f0862021-11-02 11:49:27 +01001975 if (cur_parent && !cur_parent->schema) {
1976 /* always create opaque nodes for opaque parents */
1977 r = LY_ENOT;
1978 } else {
1979 /* validate value */
1980 r = lyd_value_validate(NULL, schema, value ? value : "", value_len, NULL, NULL, NULL);
1981 }
Michal Vaskoe78faec2021-04-08 17:24:43 +02001982 if (r && (r != LY_EINCOMPLETE)) {
1983 /* creating opaque leaf */
Radek Krejci09c77442021-04-26 11:10:34 +02001984 LY_CHECK_GOTO(ret = lyd_create_opaq(ctx, schema->name, strlen(schema->name), value, value_len,
1985 schema->module->name, strlen(schema->module->name), NULL, 0, NULL, format, NULL, 0, &node),
1986 cleanup);
Michal Vaskoe78faec2021-04-08 17:24:43 +02001987 break;
1988 }
1989 }
1990
Michal Vaskoe78faec2021-04-08 17:24:43 +02001991 /* create a leaf instance */
Radek Krejci09c77442021-04-26 11:10:34 +02001992 LY_CHECK_GOTO(ret = lyd_create_term(schema, value, value_len, NULL, format, NULL, LYD_HINT_DATA, NULL,
1993 &node), cleanup);
Michal Vasko00cbf532020-06-15 13:58:47 +02001994 break;
1995 case LYS_ANYDATA:
1996 case LYS_ANYXML:
Michal Vasko366a4a12020-12-04 16:23:57 +01001997 LY_CHECK_GOTO(ret = lyd_create_any(schema, value, value_type, 0, &node), cleanup);
Michal Vasko00cbf532020-06-15 13:58:47 +02001998 break;
1999 default:
2000 LOGINT(ctx);
2001 ret = LY_EINT;
2002 goto cleanup;
2003 }
2004
2005 if (cur_parent) {
2006 /* connect to the parent */
Michal Vasko6ee6f432021-07-16 09:49:14 +02002007 lyd_insert_node(cur_parent, NULL, node, 0);
Michal Vasko00cbf532020-06-15 13:58:47 +02002008 } else if (parent) {
2009 /* connect to top-level siblings */
Michal Vasko6ee6f432021-07-16 09:49:14 +02002010 lyd_insert_node(NULL, &parent, node, 0);
Michal Vasko00cbf532020-06-15 13:58:47 +02002011 }
2012
Michal Vasko35880332020-12-08 13:08:12 +01002013next_iter:
Michal Vasko00cbf532020-06-15 13:58:47 +02002014 /* update remembered nodes */
2015 if (!nparent) {
2016 nparent = node;
2017 }
2018 nnode = node;
2019 }
2020
2021cleanup:
2022 lyxp_expr_free(ctx, exp);
Michal Vasko6741dc62021-02-04 09:27:45 +01002023 if (p) {
2024 while (orig_count > LY_ARRAY_COUNT(p)) {
2025 LY_ARRAY_INCREMENT(p);
2026 }
2027 }
Michal Vasko00cbf532020-06-15 13:58:47 +02002028 ly_path_free(ctx, p);
2029 if (!ret) {
2030 /* set out params only on success */
2031 if (new_parent) {
2032 *new_parent = nparent;
2033 }
2034 if (new_node) {
2035 *new_node = nnode;
2036 }
Michal Vaskof4b95ba2020-12-11 08:42:33 +01002037 } else {
2038 lyd_free_tree(nparent);
Michal Vasko00cbf532020-06-15 13:58:47 +02002039 }
2040 return ret;
2041}
2042
Radek Krejci95ccd1b2021-03-12 14:57:22 +01002043API LY_ERR
2044lyd_new_path(struct lyd_node *parent, const struct ly_ctx *ctx, const char *path, const char *value, uint32_t options,
2045 struct lyd_node **node)
2046{
Radek Krejci09c77442021-04-26 11:10:34 +02002047 LY_CHECK_ARG_RET(ctx, parent || ctx, path, (path[0] == '/') || parent,
2048 !(options & LYD_NEW_PATH_BIN_VALUE) || !(options & LYD_NEW_PATH_CANON_VALUE), LY_EINVAL);
2049
2050 return lyd_new_path_(parent, ctx, NULL, path, value, 0, LYD_ANYDATA_STRING, options, node, NULL);
Radek Krejci95ccd1b2021-03-12 14:57:22 +01002051}
2052
2053API LY_ERR
2054lyd_new_path2(struct lyd_node *parent, const struct ly_ctx *ctx, const char *path, const void *value,
Radek Krejci09c77442021-04-26 11:10:34 +02002055 size_t value_len, LYD_ANYDATA_VALUETYPE value_type, uint32_t options, struct lyd_node **new_parent,
2056 struct lyd_node **new_node)
Radek Krejci95ccd1b2021-03-12 14:57:22 +01002057{
Radek Krejci09c77442021-04-26 11:10:34 +02002058 LY_CHECK_ARG_RET(ctx, parent || ctx, path, (path[0] == '/') || parent,
2059 !(options & LYD_NEW_PATH_BIN_VALUE) || !(options & LYD_NEW_PATH_CANON_VALUE), LY_EINVAL);
2060 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 +01002061}
2062
2063API LY_ERR
2064lyd_new_ext_path(struct lyd_node *parent, const struct lysc_ext_instance *ext, const char *path, const void *value,
2065 uint32_t options, struct lyd_node **node)
2066{
2067 const struct ly_ctx *ctx = ext ? ext->module->ctx : NULL;
2068
Radek Krejci09c77442021-04-26 11:10:34 +02002069 LY_CHECK_ARG_RET(ctx, ext, path, (path[0] == '/') || parent,
2070 !(options & LYD_NEW_PATH_BIN_VALUE) || !(options & LYD_NEW_PATH_CANON_VALUE), LY_EINVAL);
2071 return lyd_new_path_(parent, ctx, ext, path, value, 0, LYD_ANYDATA_STRING, options, node, NULL);
Radek Krejci95ccd1b2021-03-12 14:57:22 +01002072}
2073
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002074LY_ERR
2075lyd_new_implicit_r(struct lyd_node *parent, struct lyd_node **first, const struct lysc_node *sparent,
Radek Krejci4f2e3e52021-03-30 14:20:28 +02002076 const struct lys_module *mod, struct ly_set *node_when, struct ly_set *node_exts, struct ly_set *node_types,
2077 uint32_t impl_opts, struct lyd_node **diff)
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002078{
2079 LY_ERR ret;
Michal Vaskod1e53b92021-01-28 13:11:06 +01002080 const struct lysc_node *iter = NULL;
Radek Krejci2b18bf12020-11-06 11:20:20 +01002081 struct lyd_node *node = NULL;
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002082 struct lyd_value **dflts;
2083 LY_ARRAY_COUNT_TYPE u;
Michal Vasko630d9892020-12-08 17:11:08 +01002084 uint32_t getnext_opts;
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002085
2086 assert(first && (parent || sparent || mod));
2087
2088 if (!sparent && parent) {
2089 sparent = parent->schema;
2090 }
2091
Michal Vasko630d9892020-12-08 17:11:08 +01002092 getnext_opts = LYS_GETNEXT_WITHCHOICE;
2093 if (impl_opts & LYD_IMPLICIT_OUTPUT) {
2094 getnext_opts |= LYS_GETNEXT_OUTPUT;
2095 }
2096
2097 while ((iter = lys_getnext(iter, sparent, mod ? mod->compiled : NULL, getnext_opts))) {
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002098 if ((impl_opts & LYD_IMPLICIT_NO_STATE) && (iter->flags & LYS_CONFIG_R)) {
2099 continue;
Michal Vasko44b19a12020-08-07 09:21:30 +02002100 } else if ((impl_opts & LYD_IMPLICIT_NO_CONFIG) && (iter->flags & LYS_CONFIG_W)) {
2101 continue;
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002102 }
2103
2104 switch (iter->nodetype) {
2105 case LYS_CHOICE:
Michal Vaskobcf4d7d2020-11-18 18:16:49 +01002106 node = lys_getnext_data(NULL, *first, NULL, iter, NULL);
2107 if (!node && ((struct lysc_node_choice *)iter)->dflt) {
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002108 /* create default case data */
Michal Vasko14ed9cd2021-01-28 14:16:25 +01002109 LY_CHECK_RET(lyd_new_implicit_r(parent, first, &((struct lysc_node_choice *)iter)->dflt->node,
Radek Krejci4f2e3e52021-03-30 14:20:28 +02002110 NULL, node_when, node_exts, node_types, impl_opts, diff));
Michal Vaskobcf4d7d2020-11-18 18:16:49 +01002111 } else if (node) {
2112 /* create any default data in the existing case */
2113 assert(node->schema->parent->nodetype == LYS_CASE);
Radek Krejci4f2e3e52021-03-30 14:20:28 +02002114 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 +01002115 impl_opts, diff));
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002116 }
2117 break;
2118 case LYS_CONTAINER:
2119 if (!(iter->flags & LYS_PRESENCE) && lyd_find_sibling_val(*first, iter, NULL, 0, NULL)) {
2120 /* create default NP container */
2121 LY_CHECK_RET(lyd_create_inner(iter, &node));
Michal Vaskof4d67ea2021-03-31 13:53:21 +02002122 node->flags = LYD_DEFAULT | (lysc_has_when(iter) ? LYD_WHEN_TRUE : 0);
Michal Vasko6ee6f432021-07-16 09:49:14 +02002123 lyd_insert_node(parent, first, node, 0);
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002124
Michal Vaskof4d67ea2021-03-31 13:53:21 +02002125 if (lysc_has_when(iter) && node_when) {
Michal Vaskoe16c7b72021-02-26 10:39:06 +01002126 /* remember to resolve when */
2127 LY_CHECK_RET(ly_set_add(node_when, node, 1, NULL));
2128 }
Radek Krejci4f2e3e52021-03-30 14:20:28 +02002129 if (node_exts) {
2130 /* remember to call all the extension's validation callbacks */
2131 LY_CHECK_RET(lysc_node_ext_tovalidate(node_exts, node));
2132 }
Michal Vaskoe49b6322020-11-05 17:38:36 +01002133 if (diff) {
2134 /* add into diff */
2135 LY_CHECK_RET(lyd_val_diff_add(node, LYD_DIFF_OP_CREATE, diff));
2136 }
2137
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002138 /* create any default children */
Radek Krejci4f2e3e52021-03-30 14:20:28 +02002139 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 +02002140 impl_opts, diff));
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002141 }
2142 break;
2143 case LYS_LEAF:
Michal Vasko69730152020-10-09 16:30:07 +02002144 if (!(impl_opts & LYD_IMPLICIT_NO_DEFAULTS) && ((struct lysc_node_leaf *)iter)->dflt &&
2145 lyd_find_sibling_val(*first, iter, NULL, 0, NULL)) {
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002146 /* create default leaf */
2147 ret = lyd_create_term2(iter, ((struct lysc_node_leaf *)iter)->dflt, &node);
2148 if (ret == LY_EINCOMPLETE) {
2149 if (node_types) {
2150 /* remember to resolve type */
Radek Krejci3d92e442020-10-12 12:48:13 +02002151 LY_CHECK_RET(ly_set_add(node_types, node, 1, NULL));
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002152 }
2153 } else if (ret) {
2154 return ret;
2155 }
Michal Vaskof4d67ea2021-03-31 13:53:21 +02002156 node->flags = LYD_DEFAULT | (lysc_has_when(iter) ? LYD_WHEN_TRUE : 0);
Michal Vasko6ee6f432021-07-16 09:49:14 +02002157 lyd_insert_node(parent, first, node, 0);
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002158
Michal Vaskof4d67ea2021-03-31 13:53:21 +02002159 if (lysc_has_when(iter) && node_when) {
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002160 /* remember to resolve when */
Radek Krejci3d92e442020-10-12 12:48:13 +02002161 LY_CHECK_RET(ly_set_add(node_when, node, 1, NULL));
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002162 }
Radek Krejci4f2e3e52021-03-30 14:20:28 +02002163 if (node_exts) {
2164 /* remember to call all the extension's validation callbacks */
2165 LY_CHECK_RET(lysc_node_ext_tovalidate(node_exts, node));
2166 }
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002167 if (diff) {
2168 /* add into diff */
2169 LY_CHECK_RET(lyd_val_diff_add(node, LYD_DIFF_OP_CREATE, diff));
2170 }
2171 }
2172 break;
2173 case LYS_LEAFLIST:
Michal Vasko69730152020-10-09 16:30:07 +02002174 if (!(impl_opts & LYD_IMPLICIT_NO_DEFAULTS) && ((struct lysc_node_leaflist *)iter)->dflts &&
2175 lyd_find_sibling_val(*first, iter, NULL, 0, NULL)) {
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002176 /* create all default leaf-lists */
2177 dflts = ((struct lysc_node_leaflist *)iter)->dflts;
2178 LY_ARRAY_FOR(dflts, u) {
2179 ret = lyd_create_term2(iter, dflts[u], &node);
2180 if (ret == LY_EINCOMPLETE) {
2181 if (node_types) {
2182 /* remember to resolve type */
Radek Krejci3d92e442020-10-12 12:48:13 +02002183 LY_CHECK_RET(ly_set_add(node_types, node, 1, NULL));
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002184 }
2185 } else if (ret) {
2186 return ret;
2187 }
Michal Vaskof4d67ea2021-03-31 13:53:21 +02002188 node->flags = LYD_DEFAULT | (lysc_has_when(iter) ? LYD_WHEN_TRUE : 0);
Michal Vasko6ee6f432021-07-16 09:49:14 +02002189 lyd_insert_node(parent, first, node, 0);
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002190
Michal Vaskof4d67ea2021-03-31 13:53:21 +02002191 if (lysc_has_when(iter) && node_when) {
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002192 /* remember to resolve when */
Radek Krejci3d92e442020-10-12 12:48:13 +02002193 LY_CHECK_RET(ly_set_add(node_when, node, 1, NULL));
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002194 }
Radek Krejci4f2e3e52021-03-30 14:20:28 +02002195 if (node_exts) {
2196 /* remember to call all the extension's validation callbacks */
2197 LY_CHECK_RET(lysc_node_ext_tovalidate(node_exts, node));
2198 }
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002199 if (diff) {
2200 /* add into diff */
2201 LY_CHECK_RET(lyd_val_diff_add(node, LYD_DIFF_OP_CREATE, diff));
2202 }
2203 }
2204 }
2205 break;
2206 default:
2207 /* without defaults */
2208 break;
2209 }
2210 }
2211
2212 return LY_SUCCESS;
2213}
2214
2215API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02002216lyd_new_implicit_tree(struct lyd_node *tree, uint32_t implicit_options, struct lyd_node **diff)
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002217{
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002218 LY_ERR ret = LY_SUCCESS;
Michal Vasko3488ada2020-12-03 14:18:19 +01002219 struct lyd_node *node;
Radek Krejci4f2e3e52021-03-30 14:20:28 +02002220 struct ly_set node_when = {0}, node_exts = {0};
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002221
2222 LY_CHECK_ARG_RET(NULL, tree, LY_EINVAL);
2223 if (diff) {
2224 *diff = NULL;
2225 }
2226
Michal Vasko56daf732020-08-10 10:57:18 +02002227 LYD_TREE_DFS_BEGIN(tree, node) {
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002228 /* skip added default nodes */
Michal Vasko69730152020-10-09 16:30:07 +02002229 if (((node->flags & (LYD_DEFAULT | LYD_NEW)) != (LYD_DEFAULT | LYD_NEW)) &&
2230 (node->schema->nodetype & LYD_NODE_INNER)) {
Radek Krejci4f2e3e52021-03-30 14:20:28 +02002231 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 +01002232 NULL, implicit_options, diff), cleanup);
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002233 }
2234
Michal Vasko56daf732020-08-10 10:57:18 +02002235 LYD_TREE_DFS_END(tree, node);
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002236 }
2237
Michal Vasko3488ada2020-12-03 14:18:19 +01002238 /* resolve when and remove any invalid defaults */
Radek Krejci4f2e3e52021-03-30 14:20:28 +02002239 LY_CHECK_GOTO(ret = lyd_validate_unres(&tree, NULL, &node_when, &node_exts, NULL, NULL, diff), cleanup);
Michal Vasko3488ada2020-12-03 14:18:19 +01002240
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002241cleanup:
Michal Vasko3488ada2020-12-03 14:18:19 +01002242 ly_set_erase(&node_when, NULL);
Radek Krejci4f2e3e52021-03-30 14:20:28 +02002243 ly_set_erase(&node_exts, NULL);
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002244 if (ret && diff) {
2245 lyd_free_all(*diff);
2246 *diff = NULL;
2247 }
2248 return ret;
2249}
2250
2251API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02002252lyd_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 +02002253{
2254 const struct lys_module *mod;
2255 struct lyd_node *d = NULL;
2256 uint32_t i = 0;
2257 LY_ERR ret = LY_SUCCESS;
2258
2259 LY_CHECK_ARG_RET(ctx, tree, *tree || ctx, LY_EINVAL);
2260 if (diff) {
2261 *diff = NULL;
2262 }
2263 if (!ctx) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02002264 ctx = LYD_CTX(*tree);
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002265 }
2266
2267 /* add nodes for each module one-by-one */
2268 while ((mod = ly_ctx_get_module_iter(ctx, &i))) {
2269 if (!mod->implemented) {
2270 continue;
2271 }
2272
2273 LY_CHECK_GOTO(ret = lyd_new_implicit_module(tree, mod, implicit_options, diff ? &d : NULL), cleanup);
2274 if (d) {
2275 /* merge into one diff */
2276 lyd_insert_sibling(*diff, d, diff);
2277
2278 d = NULL;
2279 }
2280 }
2281
2282cleanup:
2283 if (ret && diff) {
2284 lyd_free_all(*diff);
2285 *diff = NULL;
2286 }
2287 return ret;
2288}
2289
2290API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02002291lyd_new_implicit_module(struct lyd_node **tree, const struct lys_module *module, uint32_t implicit_options, struct lyd_node **diff)
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002292{
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002293 LY_ERR ret = LY_SUCCESS;
Michal Vasko3488ada2020-12-03 14:18:19 +01002294 struct lyd_node *root, *d = NULL;
Radek Krejci4f2e3e52021-03-30 14:20:28 +02002295 struct ly_set node_when = {0}, node_exts = {0};
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002296
2297 LY_CHECK_ARG_RET(NULL, tree, module, LY_EINVAL);
2298 if (diff) {
2299 *diff = NULL;
2300 }
2301
2302 /* add all top-level defaults for this module */
Radek Krejci4f2e3e52021-03-30 14:20:28 +02002303 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 +01002304
2305 /* resolve when and remove any invalid defaults */
Radek Krejci4f2e3e52021-03-30 14:20:28 +02002306 LY_CHECK_GOTO(ret = lyd_validate_unres(tree, module, &node_when, &node_exts, NULL, NULL, diff), cleanup);
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002307
2308 /* process nested nodes */
2309 LY_LIST_FOR(*tree, root) {
2310 /* skip added default nodes */
2311 if ((root->flags & (LYD_DEFAULT | LYD_NEW)) != (LYD_DEFAULT | LYD_NEW)) {
2312 LY_CHECK_GOTO(ret = lyd_new_implicit_tree(root, implicit_options, diff ? &d : NULL), cleanup);
2313
2314 if (d) {
2315 /* merge into one diff */
2316 lyd_insert_sibling(*diff, d, diff);
2317
2318 d = NULL;
2319 }
2320 }
2321 }
2322
2323cleanup:
Michal Vasko3488ada2020-12-03 14:18:19 +01002324 ly_set_erase(&node_when, NULL);
Radek Krejci4f2e3e52021-03-30 14:20:28 +02002325 ly_set_erase(&node_exts, NULL);
Michal Vaskoa6669ba2020-08-06 16:14:26 +02002326 if (ret && diff) {
2327 lyd_free_all(*diff);
2328 *diff = NULL;
2329 }
2330 return ret;
2331}
2332
Michal Vasko90932a92020-02-12 14:33:03 +01002333struct lyd_node *
Michal Vaskob104f112020-07-17 09:54:54 +02002334lyd_insert_get_next_anchor(const struct lyd_node *first_sibling, const struct lyd_node *new_node)
Michal Vasko90932a92020-02-12 14:33:03 +01002335{
Michal Vaskob104f112020-07-17 09:54:54 +02002336 const struct lysc_node *schema, *sparent;
Michal Vasko90932a92020-02-12 14:33:03 +01002337 struct lyd_node *match = NULL;
Radek Krejci857189e2020-09-01 13:26:36 +02002338 ly_bool found;
Michal Vasko93b16062020-12-09 18:14:18 +01002339 uint32_t getnext_opts;
Michal Vasko90932a92020-02-12 14:33:03 +01002340
Michal Vaskob104f112020-07-17 09:54:54 +02002341 assert(new_node);
2342
2343 if (!first_sibling || !new_node->schema) {
2344 /* insert at the end, no next anchor */
Michal Vasko90932a92020-02-12 14:33:03 +01002345 return NULL;
2346 }
2347
Michal Vasko93b16062020-12-09 18:14:18 +01002348 getnext_opts = 0;
Michal Vaskod1e53b92021-01-28 13:11:06 +01002349 if (new_node->schema->flags & LYS_IS_OUTPUT) {
Michal Vasko93b16062020-12-09 18:14:18 +01002350 getnext_opts = LYS_GETNEXT_OUTPUT;
2351 }
2352
Michal Vaskoa2756f02021-07-09 13:20:28 +02002353 if (first_sibling->parent && first_sibling->parent->schema && first_sibling->parent->children_ht) {
Michal Vaskob104f112020-07-17 09:54:54 +02002354 /* find the anchor using hashes */
2355 sparent = first_sibling->parent->schema;
Michal Vasko93b16062020-12-09 18:14:18 +01002356 schema = lys_getnext(new_node->schema, sparent, NULL, getnext_opts);
Michal Vaskob104f112020-07-17 09:54:54 +02002357 while (schema) {
2358 /* keep trying to find the first existing instance of the closest following schema sibling,
2359 * otherwise return NULL - inserting at the end */
2360 if (!lyd_find_sibling_schema(first_sibling, schema, &match)) {
2361 break;
2362 }
2363
Michal Vasko93b16062020-12-09 18:14:18 +01002364 schema = lys_getnext(schema, sparent, NULL, getnext_opts);
Michal Vaskob104f112020-07-17 09:54:54 +02002365 }
2366 } else {
2367 /* find the anchor without hashes */
2368 match = (struct lyd_node *)first_sibling;
Michal Vasko3a708622021-07-15 14:20:10 +02002369 sparent = lysc_data_parent(new_node->schema);
2370 if (!sparent) {
Michal Vaskob104f112020-07-17 09:54:54 +02002371 /* we are in top-level, skip all the data from preceding modules */
2372 LY_LIST_FOR(match, match) {
2373 if (!match->schema || (strcmp(lyd_owner_module(match)->name, lyd_owner_module(new_node)->name) >= 0)) {
2374 break;
2375 }
2376 }
2377 }
2378
2379 /* get the first schema sibling */
Michal Vasko93b16062020-12-09 18:14:18 +01002380 schema = lys_getnext(NULL, sparent, new_node->schema->module->compiled, getnext_opts);
Michal Vaskob104f112020-07-17 09:54:54 +02002381
2382 found = 0;
2383 LY_LIST_FOR(match, match) {
2384 if (!match->schema || (lyd_owner_module(match) != lyd_owner_module(new_node))) {
2385 /* we have found an opaque node, which must be at the end, so use it OR
2386 * modules do not match, so we must have traversed all the data from new_node module (if any),
2387 * we have found the first node of the next module, that is what we want */
2388 break;
2389 }
2390
2391 /* skip schema nodes until we find the instantiated one */
2392 while (!found) {
2393 if (new_node->schema == schema) {
2394 /* we have found the schema of the new node, continue search to find the first
2395 * data node with a different schema (after our schema) */
2396 found = 1;
2397 break;
2398 }
2399 if (match->schema == schema) {
2400 /* current node (match) is a data node still before the new node, continue search in data */
2401 break;
2402 }
Michal Vasko93b16062020-12-09 18:14:18 +01002403 schema = lys_getnext(schema, sparent, new_node->schema->module->compiled, getnext_opts);
Michal Vaskob104f112020-07-17 09:54:54 +02002404 assert(schema);
2405 }
2406
2407 if (found && (match->schema != new_node->schema)) {
2408 /* find the next node after we have found our node schema data instance */
2409 break;
2410 }
2411 }
Michal Vasko90932a92020-02-12 14:33:03 +01002412 }
2413
2414 return match;
2415}
2416
2417/**
2418 * @brief Insert node after a sibling.
2419 *
Michal Vasko9f96a052020-03-10 09:41:45 +01002420 * Handles inserting into NP containers and key-less lists.
2421 *
Michal Vasko90932a92020-02-12 14:33:03 +01002422 * @param[in] sibling Sibling to insert after.
2423 * @param[in] node Node to insert.
2424 */
2425static void
Michal Vaskof03ed032020-03-04 13:31:44 +01002426lyd_insert_after_node(struct lyd_node *sibling, struct lyd_node *node)
Michal Vasko90932a92020-02-12 14:33:03 +01002427{
Michal Vasko0249f7c2020-03-05 16:36:40 +01002428 struct lyd_node_inner *par;
2429
Michal Vasko90932a92020-02-12 14:33:03 +01002430 assert(!node->next && (node->prev == node));
2431
2432 node->next = sibling->next;
2433 node->prev = sibling;
2434 sibling->next = node;
2435 if (node->next) {
2436 /* sibling had a succeeding node */
2437 node->next->prev = node;
2438 } else {
2439 /* sibling was last, find first sibling and change its prev */
2440 if (sibling->parent) {
2441 sibling = sibling->parent->child;
2442 } else {
Michal Vaskod989ba02020-08-24 10:59:24 +02002443 for ( ; sibling->prev->next != node; sibling = sibling->prev) {}
Michal Vasko90932a92020-02-12 14:33:03 +01002444 }
2445 sibling->prev = node;
2446 }
2447 node->parent = sibling->parent;
Michal Vasko0249f7c2020-03-05 16:36:40 +01002448
Michal Vasko9f96a052020-03-10 09:41:45 +01002449 for (par = node->parent; par; par = par->parent) {
2450 if ((par->flags & LYD_DEFAULT) && !(node->flags & LYD_DEFAULT)) {
2451 /* remove default flags from NP containers */
Michal Vasko0249f7c2020-03-05 16:36:40 +01002452 par->flags &= ~LYD_DEFAULT;
2453 }
2454 }
Michal Vasko90932a92020-02-12 14:33:03 +01002455}
2456
2457/**
2458 * @brief Insert node before a sibling.
2459 *
Michal Vasko9f96a052020-03-10 09:41:45 +01002460 * Handles inserting into NP containers and key-less lists.
2461 *
Michal Vasko90932a92020-02-12 14:33:03 +01002462 * @param[in] sibling Sibling to insert before.
2463 * @param[in] node Node to insert.
2464 */
2465static void
Michal Vaskof03ed032020-03-04 13:31:44 +01002466lyd_insert_before_node(struct lyd_node *sibling, struct lyd_node *node)
Michal Vasko90932a92020-02-12 14:33:03 +01002467{
Michal Vasko0249f7c2020-03-05 16:36:40 +01002468 struct lyd_node_inner *par;
2469
Michal Vasko90932a92020-02-12 14:33:03 +01002470 assert(!node->next && (node->prev == node));
2471
2472 node->next = sibling;
2473 /* covers situation of sibling being first */
2474 node->prev = sibling->prev;
2475 sibling->prev = node;
2476 if (node->prev->next) {
2477 /* sibling had a preceding node */
2478 node->prev->next = node;
2479 } else if (sibling->parent) {
2480 /* sibling was first and we must also change parent child pointer */
2481 sibling->parent->child = node;
2482 }
2483 node->parent = sibling->parent;
Michal Vasko0249f7c2020-03-05 16:36:40 +01002484
Michal Vasko9f96a052020-03-10 09:41:45 +01002485 for (par = node->parent; par; par = par->parent) {
2486 if ((par->flags & LYD_DEFAULT) && !(node->flags & LYD_DEFAULT)) {
2487 /* remove default flags from NP containers */
Michal Vasko0249f7c2020-03-05 16:36:40 +01002488 par->flags &= ~LYD_DEFAULT;
2489 }
2490 }
Michal Vasko90932a92020-02-12 14:33:03 +01002491}
2492
2493/**
Michal Vaskob104f112020-07-17 09:54:54 +02002494 * @brief Insert node as the first and only child of a parent.
Michal Vasko90932a92020-02-12 14:33:03 +01002495 *
Michal Vasko9f96a052020-03-10 09:41:45 +01002496 * Handles inserting into NP containers and key-less lists.
2497 *
Michal Vasko90932a92020-02-12 14:33:03 +01002498 * @param[in] parent Parent to insert into.
2499 * @param[in] node Node to insert.
2500 */
2501static void
Michal Vaskob104f112020-07-17 09:54:54 +02002502lyd_insert_only_child(struct lyd_node *parent, struct lyd_node *node)
Michal Vasko90932a92020-02-12 14:33:03 +01002503{
2504 struct lyd_node_inner *par;
2505
Radek Krejcia1c1e542020-09-29 16:06:52 +02002506 assert(parent && !lyd_child(parent) && !node->next && (node->prev == node));
Michal Vasko52927e22020-03-16 17:26:14 +01002507 assert(!parent->schema || (parent->schema->nodetype & LYD_NODE_INNER));
Michal Vasko90932a92020-02-12 14:33:03 +01002508
2509 par = (struct lyd_node_inner *)parent;
2510
Michal Vaskob104f112020-07-17 09:54:54 +02002511 par->child = node;
Michal Vasko90932a92020-02-12 14:33:03 +01002512 node->parent = par;
Michal Vasko0249f7c2020-03-05 16:36:40 +01002513
Michal Vaskod989ba02020-08-24 10:59:24 +02002514 for ( ; par; par = par->parent) {
Michal Vasko9f96a052020-03-10 09:41:45 +01002515 if ((par->flags & LYD_DEFAULT) && !(node->flags & LYD_DEFAULT)) {
2516 /* remove default flags from NP containers */
Michal Vasko0249f7c2020-03-05 16:36:40 +01002517 par->flags &= ~LYD_DEFAULT;
2518 }
2519 }
Michal Vasko751cb4d2020-07-14 12:25:28 +02002520}
Michal Vasko0249f7c2020-03-05 16:36:40 +01002521
Michal Vasko751cb4d2020-07-14 12:25:28 +02002522/**
2523 * @brief Learn whether a list instance has all the keys.
2524 *
2525 * @param[in] list List instance to check.
2526 * @return non-zero if all the keys were found,
2527 * @return 0 otherwise.
2528 */
2529static int
2530lyd_insert_has_keys(const struct lyd_node *list)
2531{
2532 const struct lyd_node *key;
2533 const struct lysc_node *skey = NULL;
2534
2535 assert(list->schema->nodetype == LYS_LIST);
Radek Krejcia1c1e542020-09-29 16:06:52 +02002536 key = lyd_child(list);
Michal Vasko751cb4d2020-07-14 12:25:28 +02002537 while ((skey = lys_getnext(skey, list->schema, NULL, 0)) && (skey->flags & LYS_KEY)) {
2538 if (!key || (key->schema != skey)) {
2539 /* key missing */
2540 return 0;
2541 }
2542
2543 key = key->next;
2544 }
2545
2546 /* all keys found */
2547 return 1;
Michal Vasko90932a92020-02-12 14:33:03 +01002548}
2549
2550void
Michal Vasko6ee6f432021-07-16 09:49:14 +02002551lyd_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 +01002552{
Michal Vaskob104f112020-07-17 09:54:54 +02002553 struct lyd_node *anchor, *first_sibling;
Michal Vasko90932a92020-02-12 14:33:03 +01002554
Michal Vaskob104f112020-07-17 09:54:54 +02002555 /* inserting list without its keys is not supported */
2556 assert((parent || first_sibling_p) && node && (node->hash || !node->schema));
Michal Vaskof756c942020-11-06 17:21:37 +01002557 assert(!parent || !parent->schema ||
2558 (parent->schema->nodetype & (LYS_CONTAINER | LYS_LIST | LYS_RPC | LYS_ACTION | LYS_NOTIF)));
Michal Vasko9b368d32020-02-14 13:53:31 +01002559
Michal Vaskob104f112020-07-17 09:54:54 +02002560 if (!parent && first_sibling_p && (*first_sibling_p) && (*first_sibling_p)->parent) {
Michal Vasko9e685082021-01-29 14:49:09 +01002561 parent = lyd_parent(*first_sibling_p);
Michal Vasko9b368d32020-02-14 13:53:31 +01002562 }
Michal Vasko90932a92020-02-12 14:33:03 +01002563
Michal Vaskob104f112020-07-17 09:54:54 +02002564 /* get first sibling */
Michal Vasko9e685082021-01-29 14:49:09 +01002565 first_sibling = parent ? lyd_child(parent) : *first_sibling_p;
Michal Vasko9f96a052020-03-10 09:41:45 +01002566
Michal Vasko6ee6f432021-07-16 09:49:14 +02002567 if (last) {
2568 /* no next anchor */
2569 anchor = NULL;
2570 } else {
2571 /* find the anchor, our next node, so we can insert before it */
2572 anchor = lyd_insert_get_next_anchor(first_sibling, node);
2573 }
2574
Michal Vaskob104f112020-07-17 09:54:54 +02002575 if (anchor) {
Michal Vasko6ee6f432021-07-16 09:49:14 +02002576 /* insert before the anchor */
Michal Vaskob104f112020-07-17 09:54:54 +02002577 lyd_insert_before_node(anchor, node);
Michal Vasko26123192020-11-09 21:02:34 +01002578 if (!parent && (*first_sibling_p == anchor)) {
2579 /* move first sibling */
2580 *first_sibling_p = node;
2581 }
Michal Vaskob104f112020-07-17 09:54:54 +02002582 } else if (first_sibling) {
Michal Vasko6ee6f432021-07-16 09:49:14 +02002583 /* insert as the last node */
Michal Vaskob104f112020-07-17 09:54:54 +02002584 lyd_insert_after_node(first_sibling->prev, node);
2585 } else if (parent) {
Michal Vasko6ee6f432021-07-16 09:49:14 +02002586 /* insert as the only child */
Michal Vaskob104f112020-07-17 09:54:54 +02002587 lyd_insert_only_child(parent, node);
Michal Vasko90932a92020-02-12 14:33:03 +01002588 } else {
Michal Vasko6ee6f432021-07-16 09:49:14 +02002589 /* insert as the only sibling */
Michal Vaskob104f112020-07-17 09:54:54 +02002590 *first_sibling_p = node;
2591 }
2592
2593 /* insert into parent HT */
2594 lyd_insert_hash(node);
2595
2596 /* finish hashes for our parent, if needed and possible */
Michal Vasko9e8de2d2020-09-01 08:17:10 +02002597 if (node->schema && (node->schema->flags & LYS_KEY) && parent && lyd_insert_has_keys(parent)) {
Michal Vaskob104f112020-07-17 09:54:54 +02002598 lyd_hash(parent);
2599
2600 /* now we can insert even the list into its parent HT */
2601 lyd_insert_hash(parent);
Michal Vasko90932a92020-02-12 14:33:03 +01002602 }
Michal Vasko90932a92020-02-12 14:33:03 +01002603}
2604
Michal Vasko717a4c32020-12-07 09:40:10 +01002605/**
2606 * @brief Check schema place of a node to be inserted.
2607 *
2608 * @param[in] parent Schema node of the parent data node.
2609 * @param[in] sibling Schema node of a sibling data node.
2610 * @param[in] schema Schema node if the data node to be inserted.
2611 * @return LY_SUCCESS on success.
2612 * @return LY_EINVAL if the place is invalid.
2613 */
Michal Vaskof03ed032020-03-04 13:31:44 +01002614static LY_ERR
Michal Vasko717a4c32020-12-07 09:40:10 +01002615lyd_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 +01002616{
2617 const struct lysc_node *par2;
2618
Michal Vasko62ed12d2020-05-21 10:08:25 +02002619 assert(!parent || !(parent->nodetype & (LYS_CASE | LYS_CHOICE)));
Michal Vasko717a4c32020-12-07 09:40:10 +01002620 assert(!sibling || !(sibling->nodetype & (LYS_CASE | LYS_CHOICE)));
2621 assert(!schema || !(schema->nodetype & (LYS_CASE | LYS_CHOICE)));
Michal Vaskof03ed032020-03-04 13:31:44 +01002622
Michal Vasko717a4c32020-12-07 09:40:10 +01002623 if (!schema || (!parent && !sibling)) {
Michal Vasko71e795e2020-12-04 16:27:37 +01002624 /* opaque nodes can be inserted wherever */
2625 return LY_SUCCESS;
2626 }
2627
Michal Vasko717a4c32020-12-07 09:40:10 +01002628 if (!parent) {
2629 parent = lysc_data_parent(sibling);
2630 }
2631
Michal Vaskof03ed032020-03-04 13:31:44 +01002632 /* find schema parent */
Michal Vasko62ed12d2020-05-21 10:08:25 +02002633 par2 = lysc_data_parent(schema);
Michal Vaskof03ed032020-03-04 13:31:44 +01002634
2635 if (parent) {
2636 /* inner node */
2637 if (par2 != parent) {
Michal Vaskob104f112020-07-17 09:54:54 +02002638 LOGERR(schema->module->ctx, LY_EINVAL, "Cannot insert, parent of \"%s\" is not \"%s\".", schema->name,
Michal Vasko69730152020-10-09 16:30:07 +02002639 parent->name);
Michal Vaskof03ed032020-03-04 13:31:44 +01002640 return LY_EINVAL;
2641 }
2642 } else {
2643 /* top-level node */
2644 if (par2) {
Radek Krejcif6d14cb2020-07-02 16:11:45 +02002645 LOGERR(schema->module->ctx, LY_EINVAL, "Cannot insert, node \"%s\" is not top-level.", schema->name);
Michal Vaskof03ed032020-03-04 13:31:44 +01002646 return LY_EINVAL;
2647 }
2648 }
2649
2650 return LY_SUCCESS;
2651}
2652
2653API LY_ERR
Michal Vaskob104f112020-07-17 09:54:54 +02002654lyd_insert_child(struct lyd_node *parent, struct lyd_node *node)
Michal Vaskof03ed032020-03-04 13:31:44 +01002655{
2656 struct lyd_node *iter;
2657
Michal Vasko0ab974d2021-02-24 13:18:26 +01002658 LY_CHECK_ARG_RET(NULL, parent, node, !parent->schema || (parent->schema->nodetype & LYD_NODE_INNER), LY_EINVAL);
Michal Vaskof03ed032020-03-04 13:31:44 +01002659
Michal Vasko717a4c32020-12-07 09:40:10 +01002660 LY_CHECK_RET(lyd_insert_check_schema(parent->schema, NULL, node->schema));
Michal Vaskof03ed032020-03-04 13:31:44 +01002661
Michal Vasko0ab974d2021-02-24 13:18:26 +01002662 if (node->schema && (node->schema->flags & LYS_KEY)) {
Michal Vaskof03ed032020-03-04 13:31:44 +01002663 LOGERR(parent->schema->module->ctx, LY_EINVAL, "Cannot insert key \"%s\".", node->schema->name);
2664 return LY_EINVAL;
2665 }
2666
2667 if (node->parent || node->prev->next) {
2668 lyd_unlink_tree(node);
2669 }
2670
2671 while (node) {
2672 iter = node->next;
2673 lyd_unlink_tree(node);
Michal Vasko6ee6f432021-07-16 09:49:14 +02002674 lyd_insert_node(parent, NULL, node, 0);
Michal Vaskof03ed032020-03-04 13:31:44 +01002675 node = iter;
2676 }
2677 return LY_SUCCESS;
2678}
2679
2680API LY_ERR
Michal Vaskob104f112020-07-17 09:54:54 +02002681lyd_insert_sibling(struct lyd_node *sibling, struct lyd_node *node, struct lyd_node **first)
Michal Vaskob1b5c262020-03-05 14:29:47 +01002682{
2683 struct lyd_node *iter;
2684
Michal Vaskob104f112020-07-17 09:54:54 +02002685 LY_CHECK_ARG_RET(NULL, node, LY_EINVAL);
Michal Vaskob1b5c262020-03-05 14:29:47 +01002686
Michal Vaskob104f112020-07-17 09:54:54 +02002687 if (sibling) {
Michal Vasko717a4c32020-12-07 09:40:10 +01002688 LY_CHECK_RET(lyd_insert_check_schema(NULL, sibling->schema, node->schema));
Michal Vaskob1b5c262020-03-05 14:29:47 +01002689 }
2690
Michal Vasko553d0b52020-12-04 16:27:52 +01002691 if (sibling == node) {
2692 /* we need to keep the connection to siblings so we can insert into them */
2693 sibling = sibling->prev;
2694 }
2695
Michal Vaskob1b5c262020-03-05 14:29:47 +01002696 if (node->parent || node->prev->next) {
2697 lyd_unlink_tree(node);
2698 }
2699
2700 while (node) {
Michal Vasko71e795e2020-12-04 16:27:37 +01002701 if (lysc_is_key(node->schema)) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02002702 LOGERR(LYD_CTX(node), LY_EINVAL, "Cannot insert key \"%s\".", node->schema->name);
Michal Vaskob104f112020-07-17 09:54:54 +02002703 return LY_EINVAL;
2704 }
2705
Michal Vaskob1b5c262020-03-05 14:29:47 +01002706 iter = node->next;
2707 lyd_unlink_tree(node);
Michal Vasko6ee6f432021-07-16 09:49:14 +02002708 lyd_insert_node(NULL, &sibling, node, 0);
Michal Vaskob1b5c262020-03-05 14:29:47 +01002709 node = iter;
2710 }
Michal Vaskob1b5c262020-03-05 14:29:47 +01002711
Michal Vaskob104f112020-07-17 09:54:54 +02002712 if (first) {
2713 /* find the first sibling */
2714 *first = sibling;
2715 while ((*first)->prev->next) {
2716 *first = (*first)->prev;
Michal Vasko0249f7c2020-03-05 16:36:40 +01002717 }
2718 }
2719
2720 return LY_SUCCESS;
2721}
2722
Michal Vaskob1b5c262020-03-05 14:29:47 +01002723API LY_ERR
Michal Vaskof03ed032020-03-04 13:31:44 +01002724lyd_insert_before(struct lyd_node *sibling, struct lyd_node *node)
2725{
Michal Vaskof03ed032020-03-04 13:31:44 +01002726 LY_CHECK_ARG_RET(NULL, sibling, node, LY_EINVAL);
2727
Michal Vasko717a4c32020-12-07 09:40:10 +01002728 LY_CHECK_RET(lyd_insert_check_schema(NULL, sibling->schema, node->schema));
Michal Vaskof03ed032020-03-04 13:31:44 +01002729
Michal Vaskob104f112020-07-17 09:54:54 +02002730 if (!(node->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) || !(node->schema->flags & LYS_ORDBY_USER)) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02002731 LOGERR(LYD_CTX(sibling), LY_EINVAL, "Can be used only for user-ordered nodes.");
Michal Vaskof03ed032020-03-04 13:31:44 +01002732 return LY_EINVAL;
2733 }
2734
Michal Vasko4bc2ce32020-12-08 10:06:16 +01002735 lyd_unlink_tree(node);
2736 lyd_insert_before_node(sibling, node);
2737 lyd_insert_hash(node);
Michal Vaskof03ed032020-03-04 13:31:44 +01002738
Michal Vaskof03ed032020-03-04 13:31:44 +01002739 return LY_SUCCESS;
2740}
2741
2742API LY_ERR
2743lyd_insert_after(struct lyd_node *sibling, struct lyd_node *node)
2744{
Michal Vaskof03ed032020-03-04 13:31:44 +01002745 LY_CHECK_ARG_RET(NULL, sibling, node, LY_EINVAL);
2746
Michal Vasko717a4c32020-12-07 09:40:10 +01002747 LY_CHECK_RET(lyd_insert_check_schema(NULL, sibling->schema, node->schema));
Michal Vaskof03ed032020-03-04 13:31:44 +01002748
Michal Vaskob104f112020-07-17 09:54:54 +02002749 if (!(node->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) || !(node->schema->flags & LYS_ORDBY_USER)) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02002750 LOGERR(LYD_CTX(sibling), LY_EINVAL, "Can be used only for user-ordered nodes.");
Michal Vaskof03ed032020-03-04 13:31:44 +01002751 return LY_EINVAL;
2752 }
2753
Michal Vasko4bc2ce32020-12-08 10:06:16 +01002754 lyd_unlink_tree(node);
2755 lyd_insert_after_node(sibling, node);
2756 lyd_insert_hash(node);
Michal Vaskof03ed032020-03-04 13:31:44 +01002757
Michal Vaskof03ed032020-03-04 13:31:44 +01002758 return LY_SUCCESS;
2759}
2760
2761API void
Michal Vasko66d508c2021-07-21 16:07:09 +02002762lyd_unlink_siblings(struct lyd_node *node)
2763{
2764 struct lyd_node *next, *elem, *first = NULL;
2765
2766 LY_LIST_FOR_SAFE(node, next, elem) {
2767 lyd_unlink_tree(elem);
2768 lyd_insert_node(NULL, &first, elem, 1);
2769 }
2770}
2771
2772API void
Michal Vaskof03ed032020-03-04 13:31:44 +01002773lyd_unlink_tree(struct lyd_node *node)
2774{
2775 struct lyd_node *iter;
2776
2777 if (!node) {
2778 return;
2779 }
2780
Michal Vaskob104f112020-07-17 09:54:54 +02002781 /* update hashes while still linked into the tree */
2782 lyd_unlink_hash(node);
2783
Michal Vaskof03ed032020-03-04 13:31:44 +01002784 /* unlink from siblings */
2785 if (node->prev->next) {
2786 node->prev->next = node->next;
2787 }
2788 if (node->next) {
2789 node->next->prev = node->prev;
2790 } else {
2791 /* unlinking the last node */
2792 if (node->parent) {
2793 iter = node->parent->child;
2794 } else {
2795 iter = node->prev;
2796 while (iter->prev != node) {
2797 iter = iter->prev;
2798 }
2799 }
2800 /* update the "last" pointer from the first node */
2801 iter->prev = node->prev;
2802 }
2803
2804 /* unlink from parent */
2805 if (node->parent) {
2806 if (node->parent->child == node) {
2807 /* the node is the first child */
2808 node->parent->child = node->next;
2809 }
2810
Michal Vaskoab49dbe2020-07-17 12:32:47 +02002811 /* check for NP container whether its last non-default node is not being unlinked */
Michal Vasko69730152020-10-09 16:30:07 +02002812 if (node->parent->schema && (node->parent->schema->nodetype == LYS_CONTAINER) &&
2813 !(node->parent->flags & LYD_DEFAULT) && !(node->parent->schema->flags & LYS_PRESENCE)) {
Michal Vaskoab49dbe2020-07-17 12:32:47 +02002814 LY_LIST_FOR(node->parent->child, iter) {
2815 if ((iter != node) && !(iter->flags & LYD_DEFAULT)) {
2816 break;
2817 }
2818 }
2819 if (!iter) {
2820 node->parent->flags |= LYD_DEFAULT;
2821 }
2822 }
2823
Michal Vaskof03ed032020-03-04 13:31:44 +01002824 node->parent = NULL;
2825 }
2826
2827 node->next = NULL;
2828 node->prev = node;
2829}
2830
Michal Vaskoa5da3292020-08-12 13:10:50 +02002831void
Michal Vasko871a0252020-11-11 18:35:24 +01002832lyd_insert_meta(struct lyd_node *parent, struct lyd_meta *meta, ly_bool clear_dflt)
Radek Krejci1798aae2020-07-14 13:26:06 +02002833{
2834 struct lyd_meta *last, *iter;
2835
2836 assert(parent);
Michal Vaskoa5da3292020-08-12 13:10:50 +02002837
2838 if (!meta) {
2839 return;
2840 }
Radek Krejci1798aae2020-07-14 13:26:06 +02002841
2842 for (iter = meta; iter; iter = iter->next) {
2843 iter->parent = parent;
2844 }
2845
2846 /* insert as the last attribute */
2847 if (parent->meta) {
Radek Krejci1e008d22020-08-17 11:37:37 +02002848 for (last = parent->meta; last->next; last = last->next) {}
Radek Krejci1798aae2020-07-14 13:26:06 +02002849 last->next = meta;
2850 } else {
2851 parent->meta = meta;
2852 }
2853
2854 /* remove default flags from NP containers */
Michal Vasko871a0252020-11-11 18:35:24 +01002855 while (clear_dflt && parent && (parent->schema->nodetype == LYS_CONTAINER) && (parent->flags & LYD_DEFAULT)) {
Radek Krejci1798aae2020-07-14 13:26:06 +02002856 parent->flags &= ~LYD_DEFAULT;
Michal Vasko9e685082021-01-29 14:49:09 +01002857 parent = lyd_parent(parent);
Radek Krejci1798aae2020-07-14 13:26:06 +02002858 }
Radek Krejci1798aae2020-07-14 13:26:06 +02002859}
2860
2861LY_ERR
Michal Vasko9f96a052020-03-10 09:41:45 +01002862lyd_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 +02002863 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 +01002864 void *prefix_data, uint32_t hints, ly_bool clear_dflt, ly_bool *incomplete)
Michal Vasko90932a92020-02-12 14:33:03 +01002865{
Radek Krejci2efc45b2020-12-22 16:25:44 +01002866 LY_ERR ret = LY_SUCCESS;
Michal Vasko90932a92020-02-12 14:33:03 +01002867 struct lysc_ext_instance *ant = NULL;
Michal Vaskob0534f92021-07-01 13:24:03 +02002868 const struct lysc_type **ant_type;
Michal Vasko9f96a052020-03-10 09:41:45 +01002869 struct lyd_meta *mt, *last;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002870 LY_ARRAY_COUNT_TYPE u;
Michal Vasko90932a92020-02-12 14:33:03 +01002871
Michal Vasko9f96a052020-03-10 09:41:45 +01002872 assert((parent || meta) && mod);
Michal Vasko6f4cbb62020-02-28 11:15:47 +01002873
Radek Krejciddace2c2021-01-08 11:30:56 +01002874 LOG_LOCSET(parent ? parent->schema : NULL, parent, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +01002875
Radek Krejci7eb54ba2020-05-18 16:30:04 +02002876 LY_ARRAY_FOR(mod->compiled->exts, u) {
Radek Krejci3e6632f2021-03-22 22:08:21 +01002877 if ((mod->compiled->exts[u].def->plugin == lyplg_find(LYPLG_EXTENSION, LYEXT_PLUGIN_INTERNAL_ANNOTATION)) &&
Radek Krejci7eb54ba2020-05-18 16:30:04 +02002878 !ly_strncmp(mod->compiled->exts[u].argument, name, name_len)) {
Michal Vasko90932a92020-02-12 14:33:03 +01002879 /* we have the annotation definition */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02002880 ant = &mod->compiled->exts[u];
Michal Vasko90932a92020-02-12 14:33:03 +01002881 break;
2882 }
2883 }
2884 if (!ant) {
2885 /* attribute is not defined as a metadata annotation (RFC 7952) */
Radek Krejci2efc45b2020-12-22 16:25:44 +01002886 LOGVAL(mod->ctx, LYVE_REFERENCE, "Annotation definition for attribute \"%s:%.*s\" not found.",
Radek Krejci422afb12021-03-04 16:38:16 +01002887 mod->name, (int)name_len, name);
Radek Krejci2efc45b2020-12-22 16:25:44 +01002888 ret = LY_EINVAL;
2889 goto cleanup;
Michal Vasko90932a92020-02-12 14:33:03 +01002890 }
2891
Michal Vasko9f96a052020-03-10 09:41:45 +01002892 mt = calloc(1, sizeof *mt);
Radek Krejci2efc45b2020-12-22 16:25:44 +01002893 LY_CHECK_ERR_GOTO(!mt, LOGMEM(mod->ctx); ret = LY_EMEM, cleanup);
Michal Vasko9f96a052020-03-10 09:41:45 +01002894 mt->parent = parent;
2895 mt->annotation = ant;
Michal Vaskob0534f92021-07-01 13:24:03 +02002896 ant_type = ant->substmts[ANNOTATION_SUBSTMT_TYPE].storage;
2897 ret = lyd_value_store(mod->ctx, &mt->value, *ant_type, value, value_len, dynamic, format, prefix_data, hints,
2898 parent ? parent->schema : NULL, incomplete);
Radek Krejci2efc45b2020-12-22 16:25:44 +01002899 LY_CHECK_ERR_GOTO(ret, free(mt), cleanup);
2900 ret = lydict_insert(mod->ctx, name, name_len, &mt->name);
2901 LY_CHECK_ERR_GOTO(ret, free(mt), cleanup);
Michal Vasko90932a92020-02-12 14:33:03 +01002902
Michal Vasko6f4cbb62020-02-28 11:15:47 +01002903 /* insert as the last attribute */
2904 if (parent) {
Michal Vasko871a0252020-11-11 18:35:24 +01002905 lyd_insert_meta(parent, mt, clear_dflt);
Michal Vasko9f96a052020-03-10 09:41:45 +01002906 } else if (*meta) {
Radek Krejci1e008d22020-08-17 11:37:37 +02002907 for (last = *meta; last->next; last = last->next) {}
Michal Vasko9f96a052020-03-10 09:41:45 +01002908 last->next = mt;
Michal Vasko90932a92020-02-12 14:33:03 +01002909 }
2910
Michal Vasko9f96a052020-03-10 09:41:45 +01002911 if (meta) {
2912 *meta = mt;
Michal Vasko90932a92020-02-12 14:33:03 +01002913 }
Radek Krejci2efc45b2020-12-22 16:25:44 +01002914
2915cleanup:
Radek Krejciddace2c2021-01-08 11:30:56 +01002916 LOG_LOCBACK((parent && parent->schema) ? 1 : 0, parent ? 1 : 0, 0, 0);
Radek Krejci2efc45b2020-12-22 16:25:44 +01002917 return ret;
Michal Vasko90932a92020-02-12 14:33:03 +01002918}
2919
Michal Vaskoa5da3292020-08-12 13:10:50 +02002920void
2921lyd_insert_attr(struct lyd_node *parent, struct lyd_attr *attr)
2922{
2923 struct lyd_attr *last, *iter;
2924 struct lyd_node_opaq *opaq;
2925
2926 assert(parent && !parent->schema);
2927
2928 if (!attr) {
2929 return;
2930 }
2931
2932 opaq = (struct lyd_node_opaq *)parent;
2933 for (iter = attr; iter; iter = iter->next) {
2934 iter->parent = opaq;
2935 }
2936
2937 /* insert as the last attribute */
2938 if (opaq->attr) {
Radek Krejci1e008d22020-08-17 11:37:37 +02002939 for (last = opaq->attr; last->next; last = last->next) {}
Michal Vaskoa5da3292020-08-12 13:10:50 +02002940 last->next = attr;
2941 } else {
2942 opaq->attr = attr;
2943 }
2944}
2945
Michal Vasko52927e22020-03-16 17:26:14 +01002946LY_ERR
Michal Vaskofeca4fb2020-10-05 08:58:40 +02002947lyd_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 +01002948 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 +02002949 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 +01002950{
Radek Krejci011e4aa2020-09-04 15:22:31 +02002951 LY_ERR ret = LY_SUCCESS;
Radek Krejci1798aae2020-07-14 13:26:06 +02002952 struct lyd_attr *at, *last;
Michal Vasko52927e22020-03-16 17:26:14 +01002953
2954 assert(ctx && (parent || attr) && (!parent || !parent->schema));
Michal Vaskofeca4fb2020-10-05 08:58:40 +02002955 assert(name && name_len && format);
Michal Vasko52927e22020-03-16 17:26:14 +01002956
Michal Vasko2a3722d2021-06-16 11:52:39 +02002957 if (!value_len && (!dynamic || !*dynamic)) {
Michal Vasko52927e22020-03-16 17:26:14 +01002958 value = "";
2959 }
2960
2961 at = calloc(1, sizeof *at);
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01002962 LY_CHECK_ERR_RET(!at, LOGMEM(ctx); ly_free_prefix_data(format, val_prefix_data), LY_EMEM);
Radek Krejcid46e46a2020-09-15 14:22:42 +02002963
Michal Vaskoad92b672020-11-12 13:11:31 +01002964 LY_CHECK_GOTO(ret = lydict_insert(ctx, name, name_len, &at->name.name), finish);
Michal Vasko501af032020-11-11 20:27:44 +01002965 if (prefix_len) {
Michal Vaskoad92b672020-11-12 13:11:31 +01002966 LY_CHECK_GOTO(ret = lydict_insert(ctx, prefix, prefix_len, &at->name.prefix), finish);
Michal Vasko501af032020-11-11 20:27:44 +01002967 }
2968 if (module_key_len) {
Michal Vaskoad92b672020-11-12 13:11:31 +01002969 LY_CHECK_GOTO(ret = lydict_insert(ctx, module_key, module_key_len, &at->name.module_ns), finish);
Michal Vasko501af032020-11-11 20:27:44 +01002970 }
2971
Michal Vasko52927e22020-03-16 17:26:14 +01002972 if (dynamic && *dynamic) {
Radek Krejci011e4aa2020-09-04 15:22:31 +02002973 ret = lydict_insert_zc(ctx, (char *)value, &at->value);
2974 LY_CHECK_GOTO(ret, finish);
Michal Vasko52927e22020-03-16 17:26:14 +01002975 *dynamic = 0;
2976 } else {
Radek Krejci011e4aa2020-09-04 15:22:31 +02002977 LY_CHECK_GOTO(ret = lydict_insert(ctx, value, value_len, &at->value), finish);
Michal Vasko52927e22020-03-16 17:26:14 +01002978 }
Michal Vasko501af032020-11-11 20:27:44 +01002979 at->format = format;
2980 at->val_prefix_data = val_prefix_data;
2981 at->hints = hints;
Michal Vasko52927e22020-03-16 17:26:14 +01002982
2983 /* insert as the last attribute */
2984 if (parent) {
Michal Vaskoa5da3292020-08-12 13:10:50 +02002985 lyd_insert_attr(parent, at);
Michal Vasko52927e22020-03-16 17:26:14 +01002986 } else if (*attr) {
Radek Krejci1e008d22020-08-17 11:37:37 +02002987 for (last = *attr; last->next; last = last->next) {}
Michal Vasko52927e22020-03-16 17:26:14 +01002988 last->next = at;
2989 }
2990
Radek Krejci011e4aa2020-09-04 15:22:31 +02002991finish:
2992 if (ret) {
2993 lyd_free_attr_single(ctx, at);
2994 } else if (attr) {
Michal Vasko52927e22020-03-16 17:26:14 +01002995 *attr = at;
2996 }
2997 return LY_SUCCESS;
2998}
2999
Radek Krejci084289f2019-07-09 17:35:30 +02003000API const struct lyd_node_term *
Michal Vasko004d3152020-06-11 19:59:22 +02003001lyd_target(const struct ly_path *path, const struct lyd_node *tree)
Radek Krejci084289f2019-07-09 17:35:30 +02003002{
Michal Vasko004d3152020-06-11 19:59:22 +02003003 struct lyd_node *target;
Radek Krejci084289f2019-07-09 17:35:30 +02003004
Michal Vasko004d3152020-06-11 19:59:22 +02003005 if (ly_path_eval(path, tree, &target)) {
3006 return NULL;
Radek Krejci084289f2019-07-09 17:35:30 +02003007 }
3008
Michal Vasko004d3152020-06-11 19:59:22 +02003009 return (struct lyd_node_term *)target;
Radek Krejci084289f2019-07-09 17:35:30 +02003010}
3011
aPiecek2f63f952021-03-30 12:22:18 +02003012/**
3013 * @brief Check the equality of the two schemas from different contexts.
3014 *
3015 * @param schema1 of first node.
3016 * @param schema2 of second node.
3017 * @return 1 if the schemas are equal otherwise 0.
3018 */
3019static ly_bool
3020lyd_compare_schema_equal(const struct lysc_node *schema1, const struct lysc_node *schema2)
3021{
3022 if (!schema1 && !schema2) {
3023 return 1;
3024 } else if (!schema1 || !schema2) {
3025 return 0;
3026 }
3027
3028 assert(schema1->module->ctx != schema2->module->ctx);
3029
3030 if (schema1->nodetype != schema2->nodetype) {
3031 return 0;
3032 }
3033
3034 if (strcmp(schema1->name, schema2->name)) {
3035 return 0;
3036 }
3037
3038 if (strcmp(schema1->module->name, schema2->module->name)) {
3039 return 0;
3040 }
3041
3042 if (schema1->module->revision || schema2->module->revision) {
3043 if (!schema1->module->revision || !schema2->module->revision) {
3044 return 0;
3045 }
3046 if (strcmp(schema1->module->revision, schema2->module->revision)) {
3047 return 0;
3048 }
3049 }
3050
3051 return 1;
3052}
3053
3054/**
3055 * @brief Check the equality of the schemas for all parent nodes.
3056 *
3057 * Both nodes must be from different contexts.
3058 *
3059 * @param node1 Data of first node.
3060 * @param node2 Data of second node.
3061 * @return 1 if the all related parental schemas are equal otherwise 0.
3062 */
3063static ly_bool
3064lyd_compare_schema_parents_equal(const struct lyd_node *node1, const struct lyd_node *node2)
3065{
3066 const struct lysc_node *parent1, *parent2;
3067
3068 assert(node1 && node2);
3069
3070 for (parent1 = node1->schema->parent, parent2 = node2->schema->parent;
3071 parent1 && parent2;
3072 parent1 = parent1->parent, parent2 = parent2->parent) {
3073 if (!lyd_compare_schema_equal(parent1, parent2)) {
3074 return 0;
3075 }
3076 }
3077
3078 if (parent1 || parent2) {
3079 return 0;
3080 }
3081
3082 return 1;
3083}
3084
3085/**
3086 * @brief Internal implementation of @ref lyd_compare_single.
3087 * @copydoc lyd_compare_single
3088 * @param[in] parental_schemas_checked Flag used for optimization.
3089 * When this function is called for the first time, the flag must be set to 0.
3090 * The @ref lyd_compare_schema_parents_equal should be called only once during
3091 * recursive calls, and this is accomplished by setting to 1 in the lyd_compare_single_ body.
3092 */
3093static LY_ERR
3094lyd_compare_single_(const struct lyd_node *node1, const struct lyd_node *node2,
3095 uint32_t options, ly_bool parental_schemas_checked)
Radek Krejci1f05b6a2019-07-18 16:15:06 +02003096{
3097 const struct lyd_node *iter1, *iter2;
3098 struct lyd_node_term *term1, *term2;
3099 struct lyd_node_any *any1, *any2;
Michal Vasko52927e22020-03-16 17:26:14 +01003100 struct lyd_node_opaq *opaq1, *opaq2;
Radek Krejci1f05b6a2019-07-18 16:15:06 +02003101 size_t len1, len2;
Radek Krejci084289f2019-07-09 17:35:30 +02003102
Radek Krejci1f05b6a2019-07-18 16:15:06 +02003103 if (!node1 || !node2) {
3104 if (node1 == node2) {
3105 return LY_SUCCESS;
3106 } else {
3107 return LY_ENOT;
3108 }
3109 }
3110
aPiecek2f63f952021-03-30 12:22:18 +02003111 if (LYD_CTX(node1) == LYD_CTX(node2)) {
3112 /* same contexts */
3113 if (node1->schema != node2->schema) {
3114 return LY_ENOT;
3115 }
3116 } else {
3117 /* different contexts */
3118 if (!lyd_compare_schema_equal(node1->schema, node2->schema)) {
3119 return LY_ENOT;
3120 }
3121 if (!parental_schemas_checked) {
3122 if (!lyd_compare_schema_parents_equal(node1, node2)) {
3123 return LY_ENOT;
3124 }
3125 parental_schemas_checked = 1;
3126 }
Radek Krejci1f05b6a2019-07-18 16:15:06 +02003127 }
3128
3129 if (node1->hash != node2->hash) {
3130 return LY_ENOT;
3131 }
aPiecek2f63f952021-03-30 12:22:18 +02003132 /* 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 +02003133
Michal Vasko52927e22020-03-16 17:26:14 +01003134 if (!node1->schema) {
3135 opaq1 = (struct lyd_node_opaq *)node1;
3136 opaq2 = (struct lyd_node_opaq *)node2;
aPiecek2f63f952021-03-30 12:22:18 +02003137 if ((strcmp(opaq1->name.name, opaq2->name.name)) || (opaq1->format != opaq2->format) ||
3138 (strcmp(opaq1->name.module_ns, opaq2->name.module_ns))) {
Radek Krejci1f05b6a2019-07-18 16:15:06 +02003139 return LY_ENOT;
3140 }
Michal Vasko52927e22020-03-16 17:26:14 +01003141 switch (opaq1->format) {
Radek Krejci8df109d2021-04-23 12:19:08 +02003142 case LY_VALUE_XML:
Radek Krejci09c77442021-04-26 11:10:34 +02003143 if (lyxml_value_compare(LYD_CTX(node1), opaq1->value, opaq1->val_prefix_data, LYD_CTX(node2), opaq2->value,
3144 opaq2->val_prefix_data)) {
Michal Vasko52927e22020-03-16 17:26:14 +01003145 return LY_ENOT;
3146 }
3147 break;
Radek Krejci8df109d2021-04-23 12:19:08 +02003148 case LY_VALUE_JSON:
Radek Krejci1798aae2020-07-14 13:26:06 +02003149 /* prefixes in JSON are unique, so it is not necessary to canonize the values */
3150 if (strcmp(opaq1->value, opaq2->value)) {
3151 return LY_ENOT;
3152 }
3153 break;
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01003154 default:
Michal Vasko52927e22020-03-16 17:26:14 +01003155 /* not allowed */
Michal Vaskob7be7a82020-08-20 09:09:04 +02003156 LOGINT(LYD_CTX(node1));
Michal Vasko52927e22020-03-16 17:26:14 +01003157 return LY_EINT;
3158 }
3159 if (options & LYD_COMPARE_FULL_RECURSION) {
3160 iter1 = opaq1->child;
3161 iter2 = opaq2->child;
Radek Krejci1f05b6a2019-07-18 16:15:06 +02003162 goto all_children_compare;
Michal Vasko52927e22020-03-16 17:26:14 +01003163 }
3164 return LY_SUCCESS;
3165 } else {
3166 switch (node1->schema->nodetype) {
3167 case LYS_LEAF:
3168 case LYS_LEAFLIST:
3169 if (options & LYD_COMPARE_DEFAULTS) {
3170 if ((node1->flags & LYD_DEFAULT) != (node2->flags & LYD_DEFAULT)) {
3171 return LY_ENOT;
3172 }
3173 }
3174
Michal Vasko1a2bf2e2020-06-16 13:07:48 +02003175 term1 = (struct lyd_node_term *)node1;
3176 term2 = (struct lyd_node_term *)node2;
aPiecek2f63f952021-03-30 12:22:18 +02003177
3178 /* same contexts */
3179 if (LYD_CTX(node1) == LYD_CTX(node2)) {
3180 return term1->value.realtype->plugin->compare(&term1->value, &term2->value);
3181 }
3182
3183 /* different contexts */
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02003184 if (strcmp(lyd_get_value(node1), lyd_get_value(node2))) {
aPiecek2f63f952021-03-30 12:22:18 +02003185 return LY_ENOT;
3186 }
3187 return LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +01003188 case LYS_CONTAINER:
3189 if (options & LYD_COMPARE_DEFAULTS) {
3190 if ((node1->flags & LYD_DEFAULT) != (node2->flags & LYD_DEFAULT)) {
3191 return LY_ENOT;
3192 }
3193 }
3194 if (options & LYD_COMPARE_FULL_RECURSION) {
Michal Vasko9e685082021-01-29 14:49:09 +01003195 iter1 = lyd_child(node1);
3196 iter2 = lyd_child(node2);
Michal Vasko52927e22020-03-16 17:26:14 +01003197 goto all_children_compare;
Radek Krejci1f05b6a2019-07-18 16:15:06 +02003198 }
3199 return LY_SUCCESS;
Michal Vasko1bf09392020-03-27 12:38:10 +01003200 case LYS_RPC:
Michal Vasko52927e22020-03-16 17:26:14 +01003201 case LYS_ACTION:
3202 if (options & LYD_COMPARE_FULL_RECURSION) {
3203 /* TODO action/RPC
3204 goto all_children_compare;
3205 */
Radek Krejci1f05b6a2019-07-18 16:15:06 +02003206 }
3207 return LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +01003208 case LYS_NOTIF:
3209 if (options & LYD_COMPARE_FULL_RECURSION) {
3210 /* TODO Notification
3211 goto all_children_compare;
3212 */
3213 }
3214 return LY_SUCCESS;
3215 case LYS_LIST:
Michal Vasko9e685082021-01-29 14:49:09 +01003216 iter1 = lyd_child(node1);
3217 iter2 = lyd_child(node2);
Michal Vasko52927e22020-03-16 17:26:14 +01003218
3219 if (!(node1->schema->flags & LYS_KEYLESS) && !(options & LYD_COMPARE_FULL_RECURSION)) {
3220 /* lists with keys, their equivalence is based on their keys */
Michal Vasko544e58a2021-01-28 14:33:41 +01003221 for (const struct lysc_node *key = lysc_node_child(node1->schema);
Michal Vaskoc57d9a92020-06-23 13:28:27 +02003222 key && (key->flags & LYS_KEY);
Michal Vasko52927e22020-03-16 17:26:14 +01003223 key = key->next) {
aPiecek2f63f952021-03-30 12:22:18 +02003224 if (lyd_compare_single_(iter1, iter2, options, parental_schemas_checked)) {
Michal Vasko52927e22020-03-16 17:26:14 +01003225 return LY_ENOT;
3226 }
3227 iter1 = iter1->next;
3228 iter2 = iter2->next;
3229 }
3230 } else {
3231 /* lists without keys, their equivalence is based on equivalence of all the children (both direct and indirect) */
3232
Radek Krejci0f969882020-08-21 16:56:47 +02003233all_children_compare:
Michal Vasko52927e22020-03-16 17:26:14 +01003234 if (!iter1 && !iter2) {
3235 /* no children, nothing to compare */
3236 return LY_SUCCESS;
3237 }
3238
Michal Vaskod989ba02020-08-24 10:59:24 +02003239 for ( ; iter1 && iter2; iter1 = iter1->next, iter2 = iter2->next) {
aPiecek2f63f952021-03-30 12:22:18 +02003240 if (lyd_compare_single_(iter1, iter2, options | LYD_COMPARE_FULL_RECURSION, parental_schemas_checked)) {
Michal Vasko52927e22020-03-16 17:26:14 +01003241 return LY_ENOT;
3242 }
3243 }
3244 if (iter1 || iter2) {
3245 return LY_ENOT;
3246 }
3247 }
3248 return LY_SUCCESS;
3249 case LYS_ANYXML:
3250 case LYS_ANYDATA:
Michal Vasko22df3f02020-08-24 13:29:22 +02003251 any1 = (struct lyd_node_any *)node1;
3252 any2 = (struct lyd_node_any *)node2;
Michal Vasko52927e22020-03-16 17:26:14 +01003253
3254 if (any1->value_type != any2->value_type) {
3255 return LY_ENOT;
3256 }
3257 switch (any1->value_type) {
3258 case LYD_ANYDATA_DATATREE:
3259 iter1 = any1->value.tree;
3260 iter2 = any2->value.tree;
3261 goto all_children_compare;
3262 case LYD_ANYDATA_STRING:
3263 case LYD_ANYDATA_XML:
3264 case LYD_ANYDATA_JSON:
3265 len1 = strlen(any1->value.str);
3266 len2 = strlen(any2->value.str);
Michal Vasko69730152020-10-09 16:30:07 +02003267 if ((len1 != len2) || strcmp(any1->value.str, any2->value.str)) {
Michal Vasko52927e22020-03-16 17:26:14 +01003268 return LY_ENOT;
3269 }
3270 return LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +01003271 case LYD_ANYDATA_LYB:
Michal Vasko60ea6352020-06-29 13:39:39 +02003272 len1 = lyd_lyb_data_length(any1->value.mem);
3273 len2 = lyd_lyb_data_length(any2->value.mem);
Michal Vasko69730152020-10-09 16:30:07 +02003274 if ((len1 != len2) || memcmp(any1->value.mem, any2->value.mem, len1)) {
Michal Vasko52927e22020-03-16 17:26:14 +01003275 return LY_ENOT;
3276 }
3277 return LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +01003278 }
Radek Krejci1f05b6a2019-07-18 16:15:06 +02003279 }
3280 }
3281
Michal Vaskob7be7a82020-08-20 09:09:04 +02003282 LOGINT(LYD_CTX(node1));
Radek Krejci1f05b6a2019-07-18 16:15:06 +02003283 return LY_EINT;
3284}
Radek Krejci22ebdba2019-07-25 13:59:43 +02003285
Michal Vasko21725742020-06-29 11:49:25 +02003286API LY_ERR
aPiecek2f63f952021-03-30 12:22:18 +02003287lyd_compare_single(const struct lyd_node *node1, const struct lyd_node *node2, uint32_t options)
3288{
3289 return lyd_compare_single_(node1, node2, options, 0);
3290}
3291
3292API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003293lyd_compare_siblings(const struct lyd_node *node1, const struct lyd_node *node2, uint32_t options)
Michal Vasko8f359bf2020-07-28 10:41:15 +02003294{
Michal Vaskod989ba02020-08-24 10:59:24 +02003295 for ( ; node1 && node2; node1 = node1->next, node2 = node2->next) {
Michal Vasko8f359bf2020-07-28 10:41:15 +02003296 LY_CHECK_RET(lyd_compare_single(node1, node2, options));
3297 }
3298
Michal Vasko11a81432020-07-28 16:31:29 +02003299 if (node1 == node2) {
3300 return LY_SUCCESS;
Michal Vasko8f359bf2020-07-28 10:41:15 +02003301 }
Michal Vasko11a81432020-07-28 16:31:29 +02003302 return LY_ENOT;
Michal Vasko8f359bf2020-07-28 10:41:15 +02003303}
3304
3305API LY_ERR
Michal Vasko21725742020-06-29 11:49:25 +02003306lyd_compare_meta(const struct lyd_meta *meta1, const struct lyd_meta *meta2)
3307{
3308 if (!meta1 || !meta2) {
3309 if (meta1 == meta2) {
3310 return LY_SUCCESS;
3311 } else {
3312 return LY_ENOT;
3313 }
3314 }
3315
Michal Vaskoa8083062020-11-06 17:22:10 +01003316 if ((meta1->annotation->module->ctx != meta2->annotation->module->ctx) || (meta1->annotation != meta2->annotation)) {
Michal Vasko21725742020-06-29 11:49:25 +02003317 return LY_ENOT;
3318 }
3319
Michal Vasko21725742020-06-29 11:49:25 +02003320 return meta1->value.realtype->plugin->compare(&meta1->value, &meta2->value);
3321}
3322
Radek Krejci22ebdba2019-07-25 13:59:43 +02003323/**
Michal Vasko9cf62422021-07-01 13:29:32 +02003324 * @brief Create a copy of the attribute.
3325 *
3326 * @param[in] attr Attribute to copy.
3327 * @param[in] node Opaque where to append the new attribute.
3328 * @param[out] dup Optional created attribute copy.
3329 * @return LY_ERR value.
3330 */
3331static LY_ERR
3332lyd_dup_attr_single(const struct lyd_attr *attr, struct lyd_node *node, struct lyd_attr **dup)
3333{
3334 LY_ERR ret = LY_SUCCESS;
3335 struct lyd_attr *a, *last;
3336 struct lyd_node_opaq *opaq = (struct lyd_node_opaq *)node;
3337
3338 LY_CHECK_ARG_RET(NULL, attr, node, !node->schema, LY_EINVAL);
3339
3340 /* create a copy */
3341 a = calloc(1, sizeof *attr);
3342 LY_CHECK_ERR_RET(!a, LOGMEM(LYD_CTX(node)), LY_EMEM);
3343
3344 LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), attr->name.name, 0, &a->name.name), finish);
3345 LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), attr->name.prefix, 0, &a->name.prefix), finish);
3346 LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), attr->name.module_ns, 0, &a->name.module_ns), finish);
3347 LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), attr->value, 0, &a->value), finish);
3348 a->hints = attr->hints;
3349 a->format = attr->format;
3350 if (attr->val_prefix_data) {
3351 ret = ly_dup_prefix_data(LYD_CTX(node), attr->format, attr->val_prefix_data, &a->val_prefix_data);
3352 LY_CHECK_GOTO(ret, finish);
3353 }
3354
3355 /* insert as the last attribute */
3356 a->parent = opaq;
3357 if (opaq->attr) {
3358 for (last = opaq->attr; last->next; last = last->next) {}
3359 last->next = a;
3360 } else {
3361 opaq->attr = a;
3362 }
3363
3364finish:
3365 if (ret) {
3366 lyd_free_attr_single(LYD_CTX(node), a);
3367 } else if (dup) {
3368 *dup = a;
3369 }
3370 return LY_SUCCESS;
3371}
3372
3373/**
Michal Vasko52927e22020-03-16 17:26:14 +01003374 * @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 +02003375 *
3376 * 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 +02003377 *
3378 * @param[in] node Original node to duplicate
3379 * @param[in] parent Parent to insert into, NULL for top-level sibling.
Michal Vasko67177e52021-08-25 11:15:15 +02003380 * @param[in] insert_last Whether the duplicated node can be inserted as the last child of @p parent. Set for
3381 * recursive duplication as an optimization.
Radek Krejcif8b95172020-05-15 14:51:06 +02003382 * @param[in,out] first First sibling, NULL if no top-level sibling exist yet. Can be also NULL if @p parent is set.
3383 * @param[in] options Bitmask of options flags, see @ref dupoptions.
3384 * @param[out] dup_p Pointer where the created duplicated node is placed (besides connecting it int @p parent / @p first sibling).
3385 * @return LY_ERR value
Radek Krejci22ebdba2019-07-25 13:59:43 +02003386 */
Michal Vasko52927e22020-03-16 17:26:14 +01003387static LY_ERR
Michal Vasko67177e52021-08-25 11:15:15 +02003388lyd_dup_r(const struct lyd_node *node, struct lyd_node *parent, ly_bool insert_last, struct lyd_node **first,
3389 uint32_t options, struct lyd_node **dup_p)
Radek Krejci22ebdba2019-07-25 13:59:43 +02003390{
Michal Vasko52927e22020-03-16 17:26:14 +01003391 LY_ERR ret;
Radek Krejci22ebdba2019-07-25 13:59:43 +02003392 struct lyd_node *dup = NULL;
Michal Vasko61551fa2020-07-09 15:45:45 +02003393 struct lyd_meta *meta;
Michal Vasko9cf62422021-07-01 13:29:32 +02003394 struct lyd_attr *attr;
Michal Vasko61551fa2020-07-09 15:45:45 +02003395 struct lyd_node_any *any;
Radek Krejci22ebdba2019-07-25 13:59:43 +02003396
Michal Vasko52927e22020-03-16 17:26:14 +01003397 LY_CHECK_ARG_RET(NULL, node, LY_EINVAL);
Radek Krejci22ebdba2019-07-25 13:59:43 +02003398
Michal Vasko52927e22020-03-16 17:26:14 +01003399 if (!node->schema) {
3400 dup = calloc(1, sizeof(struct lyd_node_opaq));
Michal Vasko9cf62422021-07-01 13:29:32 +02003401 ((struct lyd_node_opaq *)dup)->ctx = LYD_CTX(node);
Michal Vasko52927e22020-03-16 17:26:14 +01003402 } else {
3403 switch (node->schema->nodetype) {
Michal Vasko1bf09392020-03-27 12:38:10 +01003404 case LYS_RPC:
Michal Vasko52927e22020-03-16 17:26:14 +01003405 case LYS_ACTION:
3406 case LYS_NOTIF:
3407 case LYS_CONTAINER:
3408 case LYS_LIST:
3409 dup = calloc(1, sizeof(struct lyd_node_inner));
3410 break;
3411 case LYS_LEAF:
3412 case LYS_LEAFLIST:
3413 dup = calloc(1, sizeof(struct lyd_node_term));
3414 break;
3415 case LYS_ANYDATA:
3416 case LYS_ANYXML:
3417 dup = calloc(1, sizeof(struct lyd_node_any));
3418 break;
3419 default:
Michal Vaskob7be7a82020-08-20 09:09:04 +02003420 LOGINT(LYD_CTX(node));
Michal Vasko52927e22020-03-16 17:26:14 +01003421 ret = LY_EINT;
3422 goto error;
3423 }
Radek Krejci22ebdba2019-07-25 13:59:43 +02003424 }
Michal Vaskob7be7a82020-08-20 09:09:04 +02003425 LY_CHECK_ERR_GOTO(!dup, LOGMEM(LYD_CTX(node)); ret = LY_EMEM, error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02003426
Michal Vaskof6df0a02020-06-16 13:08:34 +02003427 if (options & LYD_DUP_WITH_FLAGS) {
3428 dup->flags = node->flags;
3429 } else {
3430 dup->flags = (node->flags & LYD_DEFAULT) | LYD_NEW;
3431 }
Radek Krejci22ebdba2019-07-25 13:59:43 +02003432 dup->schema = node->schema;
Michal Vasko52927e22020-03-16 17:26:14 +01003433 dup->prev = dup;
Radek Krejci22ebdba2019-07-25 13:59:43 +02003434
Michal Vasko9cf62422021-07-01 13:29:32 +02003435 /* duplicate metadata/attributes */
Michal Vasko25a32822020-07-09 15:48:22 +02003436 if (!(options & LYD_DUP_NO_META)) {
Michal Vasko9cf62422021-07-01 13:29:32 +02003437 if (!node->schema) {
3438 LY_LIST_FOR(((struct lyd_node_opaq *)node)->attr, attr) {
3439 LY_CHECK_GOTO(ret = lyd_dup_attr_single(attr, dup, NULL), error);
3440 }
3441 } else {
3442 LY_LIST_FOR(node->meta, meta) {
3443 LY_CHECK_GOTO(ret = lyd_dup_meta_single(meta, dup, NULL), error);
3444 }
Michal Vasko25a32822020-07-09 15:48:22 +02003445 }
3446 }
Radek Krejci22ebdba2019-07-25 13:59:43 +02003447
3448 /* nodetype-specific work */
Michal Vasko52927e22020-03-16 17:26:14 +01003449 if (!dup->schema) {
3450 struct lyd_node_opaq *opaq = (struct lyd_node_opaq *)dup;
3451 struct lyd_node_opaq *orig = (struct lyd_node_opaq *)node;
3452 struct lyd_node *child;
Radek Krejci22ebdba2019-07-25 13:59:43 +02003453
3454 if (options & LYD_DUP_RECURSIVE) {
3455 /* duplicate all the children */
3456 LY_LIST_FOR(orig->child, child) {
Michal Vasko67177e52021-08-25 11:15:15 +02003457 LY_CHECK_GOTO(ret = lyd_dup_r(child, dup, 1, NULL, options, NULL), error);
Michal Vasko52927e22020-03-16 17:26:14 +01003458 }
3459 }
Michal Vaskoad92b672020-11-12 13:11:31 +01003460 LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), orig->name.name, 0, &opaq->name.name), error);
Michal Vasko9cf62422021-07-01 13:29:32 +02003461 LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), orig->name.prefix, 0, &opaq->name.prefix), error);
Michal Vaskoad92b672020-11-12 13:11:31 +01003462 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 +02003463 LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), orig->value, 0, &opaq->value), error);
3464 opaq->hints = orig->hints;
3465 opaq->format = orig->format;
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01003466 if (orig->val_prefix_data) {
3467 ret = ly_dup_prefix_data(LYD_CTX(node), opaq->format, orig->val_prefix_data, &opaq->val_prefix_data);
3468 LY_CHECK_GOTO(ret, error);
Michal Vasko52927e22020-03-16 17:26:14 +01003469 }
Michal Vasko52927e22020-03-16 17:26:14 +01003470 } else if (dup->schema->nodetype & LYD_NODE_TERM) {
3471 struct lyd_node_term *term = (struct lyd_node_term *)dup;
3472 struct lyd_node_term *orig = (struct lyd_node_term *)node;
3473
3474 term->hash = orig->hash;
Michal Vaskob7be7a82020-08-20 09:09:04 +02003475 LY_CHECK_ERR_GOTO(orig->value.realtype->plugin->duplicate(LYD_CTX(node), &orig->value, &term->value),
Michal Vasko69730152020-10-09 16:30:07 +02003476 LOGERR(LYD_CTX(node), LY_EINT, "Value duplication failed."); ret = LY_EINT, error);
Michal Vasko52927e22020-03-16 17:26:14 +01003477 } else if (dup->schema->nodetype & LYD_NODE_INNER) {
3478 struct lyd_node_inner *orig = (struct lyd_node_inner *)node;
3479 struct lyd_node *child;
3480
3481 if (options & LYD_DUP_RECURSIVE) {
3482 /* duplicate all the children */
3483 LY_LIST_FOR(orig->child, child) {
Michal Vasko67177e52021-08-25 11:15:15 +02003484 LY_CHECK_GOTO(ret = lyd_dup_r(child, dup, 1, NULL, options, NULL), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02003485 }
Michal Vasko69730152020-10-09 16:30:07 +02003486 } else if ((dup->schema->nodetype == LYS_LIST) && !(dup->schema->flags & LYS_KEYLESS)) {
Radek Krejci22ebdba2019-07-25 13:59:43 +02003487 /* always duplicate keys of a list */
Radek Krejci22ebdba2019-07-25 13:59:43 +02003488 child = orig->child;
Michal Vasko544e58a2021-01-28 14:33:41 +01003489 for (const struct lysc_node *key = lysc_node_child(dup->schema);
Michal Vaskoc57d9a92020-06-23 13:28:27 +02003490 key && (key->flags & LYS_KEY);
Radek Krejci0fe9b512019-07-26 17:51:05 +02003491 key = key->next) {
Radek Krejci22ebdba2019-07-25 13:59:43 +02003492 if (!child) {
3493 /* possibly not keys are present in filtered tree */
3494 break;
Radek Krejci0fe9b512019-07-26 17:51:05 +02003495 } else if (child->schema != key) {
3496 /* possibly not all keys are present in filtered tree,
3497 * but there can be also some non-key nodes */
3498 continue;
Radek Krejci22ebdba2019-07-25 13:59:43 +02003499 }
Michal Vasko67177e52021-08-25 11:15:15 +02003500 LY_CHECK_GOTO(ret = lyd_dup_r(child, dup, 1, NULL, options, NULL), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02003501 child = child->next;
3502 }
3503 }
3504 lyd_hash(dup);
3505 } else if (dup->schema->nodetype & LYD_NODE_ANY) {
Michal Vasko61551fa2020-07-09 15:45:45 +02003506 dup->hash = node->hash;
3507 any = (struct lyd_node_any *)node;
3508 LY_CHECK_GOTO(ret = lyd_any_copy_value(dup, &any->value, any->value_type), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02003509 }
3510
Michal Vasko52927e22020-03-16 17:26:14 +01003511 /* insert */
Michal Vasko67177e52021-08-25 11:15:15 +02003512 lyd_insert_node(parent, first, dup, insert_last);
Michal Vasko52927e22020-03-16 17:26:14 +01003513
3514 if (dup_p) {
3515 *dup_p = dup;
3516 }
3517 return LY_SUCCESS;
Radek Krejci22ebdba2019-07-25 13:59:43 +02003518
3519error:
Michal Vasko52927e22020-03-16 17:26:14 +01003520 lyd_free_tree(dup);
3521 return ret;
Radek Krejci22ebdba2019-07-25 13:59:43 +02003522}
3523
Michal Vasko29d674b2021-08-25 11:18:35 +02003524/**
3525 * @brief Get a parent node to connect duplicated subtree to.
3526 *
3527 * @param[in] node Node (subtree) to duplicate.
3528 * @param[in] parent Initial parent to connect to.
3529 * @param[in] options Bitmask of options flags, see @ref dupoptions.
3530 * @param[out] dup_parent First duplicated parent node, if any.
3531 * @param[out] local_parent Correct parent to directly connect duplicated @p node to.
3532 * @return LY_ERR value.
3533 */
Michal Vasko3a41dff2020-07-15 14:30:28 +02003534static LY_ERR
Michal Vasko20909752021-05-18 16:13:38 +02003535lyd_dup_get_local_parent(const struct lyd_node *node, const struct lyd_node_inner *parent, uint32_t options,
3536 struct lyd_node **dup_parent, struct lyd_node_inner **local_parent)
Radek Krejci22ebdba2019-07-25 13:59:43 +02003537{
Michal Vasko3a41dff2020-07-15 14:30:28 +02003538 const struct lyd_node_inner *orig_parent, *iter;
Radek Krejci857189e2020-09-01 13:26:36 +02003539 ly_bool repeat = 1;
Michal Vasko3a41dff2020-07-15 14:30:28 +02003540
3541 *dup_parent = NULL;
3542 *local_parent = NULL;
3543
3544 for (orig_parent = node->parent; repeat && orig_parent; orig_parent = orig_parent->parent) {
3545 if (parent && (parent->schema == orig_parent->schema)) {
3546 /* stop creating parents, connect what we have into the provided parent */
3547 iter = parent;
3548 repeat = 0;
3549 } else {
3550 iter = NULL;
Michal Vasko67177e52021-08-25 11:15:15 +02003551 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 +02003552 (struct lyd_node **)&iter));
Michal Vasko3a41dff2020-07-15 14:30:28 +02003553 }
3554 if (!*local_parent) {
3555 *local_parent = (struct lyd_node_inner *)iter;
3556 }
3557 if (iter->child) {
3558 /* 1) list - add after keys
3559 * 2) provided parent with some children */
3560 iter->child->prev->next = *dup_parent;
3561 if (*dup_parent) {
3562 (*dup_parent)->prev = iter->child->prev;
3563 iter->child->prev = *dup_parent;
3564 }
3565 } else {
3566 ((struct lyd_node_inner *)iter)->child = *dup_parent;
3567 }
3568 if (*dup_parent) {
3569 (*dup_parent)->parent = (struct lyd_node_inner *)iter;
3570 }
3571 *dup_parent = (struct lyd_node *)iter;
3572 }
3573
3574 if (repeat && parent) {
3575 /* given parent and created parents chain actually do not interconnect */
Michal Vaskob7be7a82020-08-20 09:09:04 +02003576 LOGERR(LYD_CTX(node), LY_EINVAL,
Michal Vasko69730152020-10-09 16:30:07 +02003577 "Invalid argument parent (%s()) - does not interconnect with the created node's parents chain.", __func__);
Michal Vasko3a41dff2020-07-15 14:30:28 +02003578 return LY_EINVAL;
3579 }
3580
3581 return LY_SUCCESS;
3582}
3583
3584static LY_ERR
Michal Vasko67177e52021-08-25 11:15:15 +02003585lyd_dup(const struct lyd_node *node, struct lyd_node_inner *parent, uint32_t options, ly_bool nosiblings,
3586 struct lyd_node **dup)
Michal Vasko3a41dff2020-07-15 14:30:28 +02003587{
3588 LY_ERR rc;
Radek Krejci22ebdba2019-07-25 13:59:43 +02003589 const struct lyd_node *orig; /* original node to be duplicated */
3590 struct lyd_node *first = NULL; /* the first duplicated node, this is returned */
Radek Krejci22ebdba2019-07-25 13:59:43 +02003591 struct lyd_node *top = NULL; /* the most higher created node */
3592 struct lyd_node_inner *local_parent = NULL; /* the direct parent node for the duplicated node(s) */
Radek Krejci22ebdba2019-07-25 13:59:43 +02003593
Michal Vasko3a41dff2020-07-15 14:30:28 +02003594 LY_CHECK_ARG_RET(NULL, node, LY_EINVAL);
Radek Krejci22ebdba2019-07-25 13:59:43 +02003595
3596 if (options & LYD_DUP_WITH_PARENTS) {
Michal Vasko20909752021-05-18 16:13:38 +02003597 LY_CHECK_GOTO(rc = lyd_dup_get_local_parent(node, parent, options & (LYD_DUP_WITH_FLAGS | LYD_DUP_NO_META),
3598 &top, &local_parent), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02003599 } else {
3600 local_parent = parent;
3601 }
3602
Radek Krejci22ebdba2019-07-25 13:59:43 +02003603 LY_LIST_FOR(node, orig) {
Michal Vasko35f4d772021-01-12 12:08:57 +01003604 if (lysc_is_key(orig->schema)) {
3605 if (local_parent) {
3606 /* the key must already exist in the parent */
3607 rc = lyd_find_sibling_schema(local_parent->child, orig->schema, first ? NULL : &first);
3608 LY_CHECK_ERR_GOTO(rc, LOGINT(LYD_CTX(node)), error);
3609 } else {
3610 assert(!(options & LYD_DUP_WITH_PARENTS));
3611 /* duplicating a single key, okay, I suppose... */
Michal Vasko67177e52021-08-25 11:15:15 +02003612 rc = lyd_dup_r(orig, NULL, 0, &first, options, first ? NULL : &first);
Michal Vasko35f4d772021-01-12 12:08:57 +01003613 LY_CHECK_GOTO(rc, error);
3614 }
3615 } else {
3616 /* if there is no local parent, it will be inserted into first */
Michal Vasko67177e52021-08-25 11:15:15 +02003617 rc = lyd_dup_r(orig, local_parent ? &local_parent->node : NULL, 0, &first, options, first ? NULL : &first);
Michal Vasko35f4d772021-01-12 12:08:57 +01003618 LY_CHECK_GOTO(rc, error);
3619 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02003620 if (nosiblings) {
Radek Krejci22ebdba2019-07-25 13:59:43 +02003621 break;
3622 }
3623 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02003624
Michal Vasko3a41dff2020-07-15 14:30:28 +02003625 if (dup) {
3626 *dup = first;
3627 }
3628 return LY_SUCCESS;
Radek Krejci22ebdba2019-07-25 13:59:43 +02003629
3630error:
3631 if (top) {
3632 lyd_free_tree(top);
3633 } else {
Michal Vaskof03ed032020-03-04 13:31:44 +01003634 lyd_free_siblings(first);
Radek Krejci22ebdba2019-07-25 13:59:43 +02003635 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02003636 return rc;
Radek Krejci22ebdba2019-07-25 13:59:43 +02003637}
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003638
Michal Vasko3a41dff2020-07-15 14:30:28 +02003639API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003640lyd_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 +02003641{
3642 return lyd_dup(node, parent, options, 1, dup);
3643}
3644
3645API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003646lyd_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 +02003647{
3648 return lyd_dup(node, parent, options, 0, dup);
3649}
3650
3651API LY_ERR
3652lyd_dup_meta_single(const struct lyd_meta *meta, struct lyd_node *node, struct lyd_meta **dup)
Michal Vasko25a32822020-07-09 15:48:22 +02003653{
Radek Krejci011e4aa2020-09-04 15:22:31 +02003654 LY_ERR ret = LY_SUCCESS;
Michal Vasko25a32822020-07-09 15:48:22 +02003655 struct lyd_meta *mt, *last;
3656
Michal Vasko3a41dff2020-07-15 14:30:28 +02003657 LY_CHECK_ARG_RET(NULL, meta, node, LY_EINVAL);
Michal Vasko25a32822020-07-09 15:48:22 +02003658
3659 /* create a copy */
3660 mt = calloc(1, sizeof *mt);
Michal Vaskob7be7a82020-08-20 09:09:04 +02003661 LY_CHECK_ERR_RET(!mt, LOGMEM(LYD_CTX(node)), LY_EMEM);
Michal Vasko25a32822020-07-09 15:48:22 +02003662 mt->annotation = meta->annotation;
Michal Vaskob7be7a82020-08-20 09:09:04 +02003663 ret = meta->value.realtype->plugin->duplicate(LYD_CTX(node), &meta->value, &mt->value);
Radek Krejci011e4aa2020-09-04 15:22:31 +02003664 LY_CHECK_ERR_GOTO(ret, LOGERR(LYD_CTX(node), LY_EINT, "Value duplication failed."), finish);
3665 LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), meta->name, 0, &mt->name), finish);
Michal Vasko25a32822020-07-09 15:48:22 +02003666
3667 /* insert as the last attribute */
Radek Krejci011e4aa2020-09-04 15:22:31 +02003668 mt->parent = node;
Michal Vasko25a32822020-07-09 15:48:22 +02003669 if (node->meta) {
Radek Krejci1e008d22020-08-17 11:37:37 +02003670 for (last = node->meta; last->next; last = last->next) {}
Michal Vasko25a32822020-07-09 15:48:22 +02003671 last->next = mt;
3672 } else {
3673 node->meta = mt;
3674 }
3675
Radek Krejci011e4aa2020-09-04 15:22:31 +02003676finish:
3677 if (ret) {
3678 lyd_free_meta_single(mt);
3679 } else if (dup) {
Michal Vasko3a41dff2020-07-15 14:30:28 +02003680 *dup = mt;
3681 }
3682 return LY_SUCCESS;
Michal Vasko25a32822020-07-09 15:48:22 +02003683}
3684
Michal Vasko4490d312020-06-16 13:08:55 +02003685/**
3686 * @brief Merge a source sibling into target siblings.
3687 *
3688 * @param[in,out] first_trg First target sibling, is updated if top-level.
3689 * @param[in] parent_trg Target parent.
3690 * @param[in,out] sibling_src Source sibling to merge, set to NULL if spent.
Michal Vaskocd3f6172021-05-18 16:14:50 +02003691 * @param[in] merge_cb Optional merge callback.
3692 * @param[in] cb_data Arbitrary callback data.
Michal Vasko4490d312020-06-16 13:08:55 +02003693 * @param[in] options Merge options.
Michal Vaskocd3f6172021-05-18 16:14:50 +02003694 * @param[in,out] dup_inst Duplicate instance cache for all @p first_trg siblings.
Michal Vasko4490d312020-06-16 13:08:55 +02003695 * @return LY_ERR value.
3696 */
3697static LY_ERR
3698lyd_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 +02003699 lyd_merge_cb merge_cb, void *cb_data, uint16_t options, struct lyd_dup_inst **dup_inst)
Michal Vasko4490d312020-06-16 13:08:55 +02003700{
Michal Vasko4490d312020-06-16 13:08:55 +02003701 const struct lyd_node *child_src, *tmp, *sibling_src;
Michal Vasko56daf732020-08-10 10:57:18 +02003702 struct lyd_node *match_trg, *dup_src, *elem;
Michal Vasko4490d312020-06-16 13:08:55 +02003703 struct lysc_type *type;
Michal Vaskocd3f6172021-05-18 16:14:50 +02003704 struct lyd_dup_inst *child_dup_inst = NULL;
3705 LY_ERR ret;
3706 ly_bool first_inst = 0;
Michal Vasko4490d312020-06-16 13:08:55 +02003707
3708 sibling_src = *sibling_src_p;
Michal Vaskocd3f6172021-05-18 16:14:50 +02003709 if (!sibling_src->schema) {
3710 /* try to find the same opaque node */
3711 lyd_find_sibling_opaq_next(*first_trg, LYD_NAME(sibling_src), &match_trg);
3712 } else if (sibling_src->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) {
Michal Vasko4490d312020-06-16 13:08:55 +02003713 /* try to find the exact instance */
Michal Vaskocd3f6172021-05-18 16:14:50 +02003714 lyd_find_sibling_first(*first_trg, sibling_src, &match_trg);
Michal Vasko4490d312020-06-16 13:08:55 +02003715 } else {
3716 /* try to simply find the node, there cannot be more instances */
Michal Vaskocd3f6172021-05-18 16:14:50 +02003717 lyd_find_sibling_val(*first_trg, sibling_src->schema, NULL, 0, &match_trg);
Michal Vasko4490d312020-06-16 13:08:55 +02003718 }
3719
Michal Vaskocd3f6172021-05-18 16:14:50 +02003720 if (match_trg) {
3721 /* update match as needed */
3722 LY_CHECK_RET(lyd_dup_inst_next(&match_trg, *first_trg, dup_inst));
3723 } else {
3724 /* first instance of this node */
3725 first_inst = 1;
3726 }
3727
3728 if (match_trg) {
3729 /* call callback */
3730 if (merge_cb) {
3731 LY_CHECK_RET(merge_cb(match_trg, sibling_src, cb_data));
3732 }
3733
Michal Vasko4490d312020-06-16 13:08:55 +02003734 /* node found, make sure even value matches for all node types */
Michal Vasko8f359bf2020-07-28 10:41:15 +02003735 if ((match_trg->schema->nodetype == LYS_LEAF) && lyd_compare_single(sibling_src, match_trg, LYD_COMPARE_DEFAULTS)) {
Michal Vasko4490d312020-06-16 13:08:55 +02003736 /* since they are different, they cannot both be default */
3737 assert(!(sibling_src->flags & LYD_DEFAULT) || !(match_trg->flags & LYD_DEFAULT));
3738
Michal Vasko3a41dff2020-07-15 14:30:28 +02003739 /* update value (or only LYD_DEFAULT flag) only if flag set or the source node is not default */
3740 if ((options & LYD_MERGE_DEFAULTS) || !(sibling_src->flags & LYD_DEFAULT)) {
Michal Vasko4490d312020-06-16 13:08:55 +02003741 type = ((struct lysc_node_leaf *)match_trg->schema)->type;
Michal Vaskob7be7a82020-08-20 09:09:04 +02003742 type->plugin->free(LYD_CTX(match_trg), &((struct lyd_node_term *)match_trg)->value);
3743 LY_CHECK_RET(type->plugin->duplicate(LYD_CTX(match_trg), &((struct lyd_node_term *)sibling_src)->value,
Michal Vasko69730152020-10-09 16:30:07 +02003744 &((struct lyd_node_term *)match_trg)->value));
Michal Vasko4490d312020-06-16 13:08:55 +02003745
3746 /* copy flags and add LYD_NEW */
Michal Vasko7e8315b2021-08-03 17:01:06 +02003747 match_trg->flags = sibling_src->flags | ((options & LYD_MERGE_WITH_FLAGS) ? 0 : LYD_NEW);
Michal Vasko4490d312020-06-16 13:08:55 +02003748 }
Michal Vasko8f359bf2020-07-28 10:41:15 +02003749 } else if ((match_trg->schema->nodetype & LYS_ANYDATA) && lyd_compare_single(sibling_src, match_trg, 0)) {
Michal Vasko4c583e82020-07-17 12:16:14 +02003750 /* update value */
3751 LY_CHECK_RET(lyd_any_copy_value(match_trg, &((struct lyd_node_any *)sibling_src)->value,
Radek Krejci0f969882020-08-21 16:56:47 +02003752 ((struct lyd_node_any *)sibling_src)->value_type));
Michal Vasko4490d312020-06-16 13:08:55 +02003753
3754 /* copy flags and add LYD_NEW */
Michal Vasko7e8315b2021-08-03 17:01:06 +02003755 match_trg->flags = sibling_src->flags | ((options & LYD_MERGE_WITH_FLAGS) ? 0 : LYD_NEW);
Michal Vaskocd3f6172021-05-18 16:14:50 +02003756 }
3757
3758 /* check descendants, recursively */
3759 ret = LY_SUCCESS;
3760 LY_LIST_FOR_SAFE(lyd_child_no_keys(sibling_src), tmp, child_src) {
3761 ret = lyd_merge_sibling_r(lyd_node_child_p(match_trg), match_trg, &child_src, merge_cb, cb_data, options,
3762 &child_dup_inst);
3763 if (ret) {
3764 break;
Michal Vasko4490d312020-06-16 13:08:55 +02003765 }
3766 }
Michal Vaskocd3f6172021-05-18 16:14:50 +02003767 lyd_dup_inst_free(child_dup_inst);
3768 LY_CHECK_RET(ret);
Michal Vasko4490d312020-06-16 13:08:55 +02003769 } else {
3770 /* node not found, merge it */
3771 if (options & LYD_MERGE_DESTRUCT) {
3772 dup_src = (struct lyd_node *)sibling_src;
3773 lyd_unlink_tree(dup_src);
3774 /* spend it */
3775 *sibling_src_p = NULL;
3776 } else {
Michal Vasko3a41dff2020-07-15 14:30:28 +02003777 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 +02003778 }
3779
Michal Vasko7e8315b2021-08-03 17:01:06 +02003780 if (!(options & LYD_MERGE_WITH_FLAGS)) {
3781 /* set LYD_NEW for all the new nodes, required for validation */
3782 LYD_TREE_DFS_BEGIN(dup_src, elem) {
3783 elem->flags |= LYD_NEW;
3784 LYD_TREE_DFS_END(dup_src, elem);
3785 }
Michal Vasko4490d312020-06-16 13:08:55 +02003786 }
3787
Michal Vaskocd3f6172021-05-18 16:14:50 +02003788 /* insert */
Michal Vasko6ee6f432021-07-16 09:49:14 +02003789 lyd_insert_node(parent_trg, first_trg, dup_src, 0);
Michal Vaskocd3f6172021-05-18 16:14:50 +02003790
3791 if (first_inst) {
3792 /* remember not to find this instance next time */
3793 LY_CHECK_RET(lyd_dup_inst_next(&dup_src, *first_trg, dup_inst));
3794 }
3795
3796 /* call callback, no source node */
3797 if (merge_cb) {
3798 LY_CHECK_RET(merge_cb(dup_src, NULL, cb_data));
3799 }
Michal Vasko4490d312020-06-16 13:08:55 +02003800 }
3801
3802 return LY_SUCCESS;
3803}
3804
Michal Vasko3a41dff2020-07-15 14:30:28 +02003805static LY_ERR
Michal Vaskocd3f6172021-05-18 16:14:50 +02003806lyd_merge(struct lyd_node **target, const struct lyd_node *source, const struct lys_module *mod,
3807 lyd_merge_cb merge_cb, void *cb_data, uint16_t options, ly_bool nosiblings)
Michal Vasko4490d312020-06-16 13:08:55 +02003808{
3809 const struct lyd_node *sibling_src, *tmp;
Michal Vaskocd3f6172021-05-18 16:14:50 +02003810 struct lyd_dup_inst *dup_inst = NULL;
Radek Krejci857189e2020-09-01 13:26:36 +02003811 ly_bool first;
Michal Vaskocd3f6172021-05-18 16:14:50 +02003812 LY_ERR ret = LY_SUCCESS;
Michal Vasko4490d312020-06-16 13:08:55 +02003813
3814 LY_CHECK_ARG_RET(NULL, target, LY_EINVAL);
3815
3816 if (!source) {
3817 /* nothing to merge */
3818 return LY_SUCCESS;
3819 }
3820
Michal Vasko831422b2020-11-24 11:20:51 +01003821 if ((*target && lysc_data_parent((*target)->schema)) || lysc_data_parent(source->schema)) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02003822 LOGERR(LYD_CTX(source), LY_EINVAL, "Invalid arguments - can merge only 2 top-level subtrees (%s()).", __func__);
Michal Vasko4490d312020-06-16 13:08:55 +02003823 return LY_EINVAL;
3824 }
3825
3826 LY_LIST_FOR_SAFE(source, tmp, sibling_src) {
Michal Vaskocd3f6172021-05-18 16:14:50 +02003827 if (mod && (lyd_owner_module(sibling_src) != mod)) {
3828 /* skip data nodes from different modules */
3829 continue;
3830 }
3831
Radek Krejci857189e2020-09-01 13:26:36 +02003832 first = (sibling_src == source) ? 1 : 0;
Michal Vaskocd3f6172021-05-18 16:14:50 +02003833 ret = lyd_merge_sibling_r(target, NULL, &sibling_src, merge_cb, cb_data, options, &dup_inst);
3834 if (ret) {
3835 break;
3836 }
Michal Vasko4490d312020-06-16 13:08:55 +02003837 if (first && !sibling_src) {
3838 /* source was spent (unlinked), move to the next node */
3839 source = tmp;
3840 }
3841
Michal Vasko3a41dff2020-07-15 14:30:28 +02003842 if (nosiblings) {
Michal Vasko4490d312020-06-16 13:08:55 +02003843 break;
3844 }
3845 }
3846
3847 if (options & LYD_MERGE_DESTRUCT) {
3848 /* free any leftover source data that were not merged */
3849 lyd_free_siblings((struct lyd_node *)source);
3850 }
3851
Michal Vaskocd3f6172021-05-18 16:14:50 +02003852 lyd_dup_inst_free(dup_inst);
3853 return ret;
Michal Vasko4490d312020-06-16 13:08:55 +02003854}
3855
Michal Vasko3a41dff2020-07-15 14:30:28 +02003856API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003857lyd_merge_tree(struct lyd_node **target, const struct lyd_node *source, uint16_t options)
Michal Vasko3a41dff2020-07-15 14:30:28 +02003858{
Michal Vaskocd3f6172021-05-18 16:14:50 +02003859 return lyd_merge(target, source, NULL, NULL, NULL, options, 1);
Michal Vasko3a41dff2020-07-15 14:30:28 +02003860}
3861
3862API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003863lyd_merge_siblings(struct lyd_node **target, const struct lyd_node *source, uint16_t options)
Michal Vasko3a41dff2020-07-15 14:30:28 +02003864{
Michal Vaskocd3f6172021-05-18 16:14:50 +02003865 return lyd_merge(target, source, NULL, NULL, NULL, options, 0);
3866}
3867
3868API LY_ERR
3869lyd_merge_module(struct lyd_node **target, const struct lyd_node *source, const struct lys_module *mod,
3870 lyd_merge_cb merge_cb, void *cb_data, uint16_t options)
3871{
3872 return lyd_merge(target, source, mod, merge_cb, cb_data, options, 0);
Michal Vasko3a41dff2020-07-15 14:30:28 +02003873}
3874
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003875static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02003876lyd_path_str_enlarge(char **buffer, size_t *buflen, size_t reqlen, ly_bool is_static)
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003877{
Michal Vasko14654712020-02-06 08:35:21 +01003878 /* ending \0 */
3879 ++reqlen;
3880
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003881 if (reqlen > *buflen) {
3882 if (is_static) {
3883 return LY_EINCOMPLETE;
3884 }
3885
3886 *buffer = ly_realloc(*buffer, reqlen * sizeof **buffer);
3887 if (!*buffer) {
3888 return LY_EMEM;
3889 }
3890
3891 *buflen = reqlen;
3892 }
3893
3894 return LY_SUCCESS;
3895}
3896
Michal Vaskod59035b2020-07-08 12:00:06 +02003897LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02003898lyd_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 +02003899{
3900 const struct lyd_node *key;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003901 size_t len;
3902 const char *val;
3903 char quot;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003904
Radek Krejcia1c1e542020-09-29 16:06:52 +02003905 for (key = lyd_child(node); key && (key->schema->flags & LYS_KEY); key = key->next) {
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02003906 val = lyd_get_value(key);
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003907 len = 1 + strlen(key->schema->name) + 2 + strlen(val) + 2;
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003908 LY_CHECK_RET(lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static));
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003909
3910 quot = '\'';
3911 if (strchr(val, '\'')) {
3912 quot = '"';
3913 }
3914 *bufused += sprintf(*buffer + *bufused, "[%s=%c%s%c]", key->schema->name, quot, val, quot);
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003915 }
3916
3917 return LY_SUCCESS;
3918}
3919
3920/**
3921 * @brief Append leaf-list value predicate to path.
3922 *
3923 * @param[in] node Node to print.
3924 * @param[in,out] buffer Buffer to print to.
3925 * @param[in,out] buflen Current buffer length.
3926 * @param[in,out] bufused Current number of characters used in @p buffer.
3927 * @param[in] is_static Whether buffer is static or can be reallocated.
3928 * @return LY_ERR
3929 */
3930static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02003931lyd_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 +02003932{
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003933 size_t len;
3934 const char *val;
3935 char quot;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003936
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02003937 val = lyd_get_value(node);
Radek Krejcif13b87b2020-12-01 22:02:17 +01003938 len = 4 + strlen(val) + 2; /* "[.='" + val + "']" */
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003939 LY_CHECK_RET(lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static));
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003940
3941 quot = '\'';
3942 if (strchr(val, '\'')) {
3943 quot = '"';
3944 }
3945 *bufused += sprintf(*buffer + *bufused, "[.=%c%s%c]", quot, val, quot);
3946
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003947 return LY_SUCCESS;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003948}
3949
3950/**
3951 * @brief Append node position (relative to its other instances) predicate to path.
3952 *
3953 * @param[in] node Node to print.
3954 * @param[in,out] buffer Buffer to print to.
3955 * @param[in,out] buflen Current buffer length.
3956 * @param[in,out] bufused Current number of characters used in @p buffer.
3957 * @param[in] is_static Whether buffer is static or can be reallocated.
3958 * @return LY_ERR
3959 */
3960static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02003961lyd_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 +02003962{
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003963 size_t len;
Michal Vasko50cc0562021-05-18 16:15:43 +02003964 uint32_t pos;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003965 char *val = NULL;
3966 LY_ERR rc;
3967
Michal Vasko50cc0562021-05-18 16:15:43 +02003968 pos = lyd_list_pos(node);
3969 if (asprintf(&val, "%" PRIu32, pos) == -1) {
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003970 return LY_EMEM;
3971 }
3972
3973 len = 1 + strlen(val) + 1;
3974 rc = lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static);
3975 if (rc != LY_SUCCESS) {
3976 goto cleanup;
3977 }
3978
3979 *bufused += sprintf(*buffer + *bufused, "[%s]", val);
3980
3981cleanup:
3982 free(val);
3983 return rc;
3984}
3985
3986API char *
3987lyd_path(const struct lyd_node *node, LYD_PATH_TYPE pathtype, char *buffer, size_t buflen)
3988{
Radek Krejci857189e2020-09-01 13:26:36 +02003989 ly_bool is_static = 0;
Radek Krejci1deb5be2020-08-26 16:43:36 +02003990 uint32_t i, depth;
Michal Vasko14654712020-02-06 08:35:21 +01003991 size_t bufused = 0, len;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003992 const struct lyd_node *iter;
3993 const struct lys_module *mod;
Michal Vasko790b2bc2020-08-03 13:35:06 +02003994 LY_ERR rc = LY_SUCCESS;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003995
3996 LY_CHECK_ARG_RET(NULL, node, NULL);
3997 if (buffer) {
Michal Vasko16385f42021-05-18 16:16:09 +02003998 LY_CHECK_ARG_RET(LYD_CTX(node), buflen > 1, NULL);
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003999 is_static = 1;
Michal Vasko14654712020-02-06 08:35:21 +01004000 } else {
4001 buflen = 0;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02004002 }
4003
4004 switch (pathtype) {
Radek Krejci635d2b82021-01-04 11:26:51 +01004005 case LYD_PATH_STD:
4006 case LYD_PATH_STD_NO_LAST_PRED:
Michal Vasko14654712020-02-06 08:35:21 +01004007 depth = 1;
Michal Vasko9e685082021-01-29 14:49:09 +01004008 for (iter = node; iter->parent; iter = lyd_parent(iter)) {
Michal Vasko5ec7cda2019-09-11 13:43:08 +02004009 ++depth;
4010 }
4011
Michal Vasko5ec7cda2019-09-11 13:43:08 +02004012 goto iter_print;
Michal Vasko14654712020-02-06 08:35:21 +01004013 while (depth) {
Michal Vasko5ec7cda2019-09-11 13:43:08 +02004014 /* find the right node */
Michal Vasko9e685082021-01-29 14:49:09 +01004015 for (iter = node, i = 1; i < depth; iter = lyd_parent(iter), ++i) {}
Michal Vasko5ec7cda2019-09-11 13:43:08 +02004016iter_print:
4017 /* print prefix and name */
4018 mod = NULL;
Michal Vasko69730152020-10-09 16:30:07 +02004019 if (iter->schema && (!iter->parent || (iter->schema->module != iter->parent->schema->module))) {
Michal Vasko5ec7cda2019-09-11 13:43:08 +02004020 mod = iter->schema->module;
4021 }
4022
4023 /* realloc string */
Michal Vaskoad92b672020-11-12 13:11:31 +01004024 len = 1 + (mod ? strlen(mod->name) + 1 : 0) + (iter->schema ? strlen(iter->schema->name) :
4025 strlen(((struct lyd_node_opaq *)iter)->name.name));
Michal Vasko5ec7cda2019-09-11 13:43:08 +02004026 rc = lyd_path_str_enlarge(&buffer, &buflen, bufused + len, is_static);
4027 if (rc != LY_SUCCESS) {
4028 break;
4029 }
4030
4031 /* print next node */
Radek Krejci1798aae2020-07-14 13:26:06 +02004032 bufused += sprintf(buffer + bufused, "/%s%s%s", mod ? mod->name : "", mod ? ":" : "",
Michal Vaskoad92b672020-11-12 13:11:31 +01004033 iter->schema ? iter->schema->name : ((struct lyd_node_opaq *)iter)->name.name);
Michal Vasko5ec7cda2019-09-11 13:43:08 +02004034
Michal Vasko790b2bc2020-08-03 13:35:06 +02004035 /* do not always print the last (first) predicate */
Radek Krejci635d2b82021-01-04 11:26:51 +01004036 if (iter->schema && ((depth > 1) || (pathtype == LYD_PATH_STD))) {
Michal Vasko790b2bc2020-08-03 13:35:06 +02004037 switch (iter->schema->nodetype) {
4038 case LYS_LIST:
4039 if (iter->schema->flags & LYS_KEYLESS) {
4040 /* print its position */
4041 rc = lyd_path_position_predicate(iter, &buffer, &buflen, &bufused, is_static);
4042 } else {
4043 /* print all list keys in predicates */
4044 rc = lyd_path_list_predicate(iter, &buffer, &buflen, &bufused, is_static);
4045 }
4046 break;
4047 case LYS_LEAFLIST:
4048 if (iter->schema->flags & LYS_CONFIG_W) {
4049 /* print leaf-list value */
4050 rc = lyd_path_leaflist_predicate(iter, &buffer, &buflen, &bufused, is_static);
4051 } else {
4052 /* print its position */
4053 rc = lyd_path_position_predicate(iter, &buffer, &buflen, &bufused, is_static);
4054 }
4055 break;
4056 default:
4057 /* nothing to print more */
4058 break;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02004059 }
Michal Vasko5ec7cda2019-09-11 13:43:08 +02004060 }
4061 if (rc != LY_SUCCESS) {
4062 break;
4063 }
4064
Michal Vasko14654712020-02-06 08:35:21 +01004065 --depth;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02004066 }
4067 break;
4068 }
4069
4070 return buffer;
4071}
Michal Vaskoe444f752020-02-10 12:20:06 +01004072
Michal Vasko25a32822020-07-09 15:48:22 +02004073API struct lyd_meta *
4074lyd_find_meta(const struct lyd_meta *first, const struct lys_module *module, const char *name)
4075{
4076 struct lyd_meta *ret = NULL;
4077 const struct ly_ctx *ctx;
4078 const char *prefix, *tmp;
4079 char *str;
4080 size_t pref_len, name_len;
4081
4082 LY_CHECK_ARG_RET(NULL, module || strchr(name, ':'), name, NULL);
4083
4084 if (!first) {
4085 return NULL;
4086 }
4087
4088 ctx = first->annotation->module->ctx;
4089
4090 /* parse the name */
4091 tmp = name;
4092 if (ly_parse_nodeid(&tmp, &prefix, &pref_len, &name, &name_len) || tmp[0]) {
4093 LOGERR(ctx, LY_EINVAL, "Metadata name \"%s\" is not valid.", name);
4094 return NULL;
4095 }
4096
4097 /* find the module */
4098 if (prefix) {
4099 str = strndup(prefix, pref_len);
4100 module = ly_ctx_get_module_latest(ctx, str);
4101 free(str);
Radek Krejci422afb12021-03-04 16:38:16 +01004102 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 +02004103 }
4104
4105 /* find the metadata */
4106 LY_LIST_FOR(first, first) {
4107 if ((first->annotation->module == module) && !strcmp(first->name, name)) {
4108 ret = (struct lyd_meta *)first;
4109 break;
4110 }
4111 }
4112
4113 return ret;
4114}
4115
Michal Vasko9b368d32020-02-14 13:53:31 +01004116API LY_ERR
Michal Vaskoe444f752020-02-10 12:20:06 +01004117lyd_find_sibling_first(const struct lyd_node *siblings, const struct lyd_node *target, struct lyd_node **match)
4118{
Michal Vaskoe78faec2021-04-08 17:24:43 +02004119 struct lyd_node **match_p, *iter;
Michal Vaskoe444f752020-02-10 12:20:06 +01004120 struct lyd_node_inner *parent;
Michal Vaskoe78faec2021-04-08 17:24:43 +02004121 ly_bool found;
Michal Vaskoe444f752020-02-10 12:20:06 +01004122
Michal Vaskof03ed032020-03-04 13:31:44 +01004123 LY_CHECK_ARG_RET(NULL, target, LY_EINVAL);
Michal Vaskoe444f752020-02-10 12:20:06 +01004124
Michal Vasko6da1e952020-12-09 18:14:38 +01004125 if (!siblings || (siblings->schema && target->schema &&
4126 (lysc_data_parent(siblings->schema) != lysc_data_parent(target->schema)))) {
Michal Vasko62ed12d2020-05-21 10:08:25 +02004127 /* no data or schema mismatch */
Michal Vasko9b368d32020-02-14 13:53:31 +01004128 if (match) {
4129 *match = NULL;
4130 }
Michal Vaskoe444f752020-02-10 12:20:06 +01004131 return LY_ENOTFOUND;
4132 }
4133
Michal Vaskoe78faec2021-04-08 17:24:43 +02004134 /* get first sibling */
4135 siblings = lyd_first_sibling(siblings);
Michal Vaskoe444f752020-02-10 12:20:06 +01004136
Michal Vasko9e685082021-01-29 14:49:09 +01004137 parent = siblings->parent;
Michal Vasko6da1e952020-12-09 18:14:38 +01004138 if (parent && parent->schema && parent->children_ht) {
Michal Vaskoe444f752020-02-10 12:20:06 +01004139 assert(target->hash);
4140
Michal Vaskoe78faec2021-04-08 17:24:43 +02004141 if (lysc_is_dup_inst_list(target->schema)) {
4142 /* we must search the instances from beginning to find the first matching one */
4143 found = 0;
4144 LYD_LIST_FOR_INST(siblings, target->schema, iter) {
4145 if (!lyd_compare_single(target, iter, 0)) {
4146 found = 1;
4147 break;
4148 }
4149 }
4150 if (found) {
4151 siblings = iter;
Michal Vaskoda859032020-07-14 12:20:14 +02004152 } else {
4153 siblings = NULL;
4154 }
Michal Vaskoe444f752020-02-10 12:20:06 +01004155 } else {
Michal Vaskoe78faec2021-04-08 17:24:43 +02004156 /* find by hash */
4157 if (!lyht_find(parent->children_ht, &target, target->hash, (void **)&match_p)) {
4158 siblings = *match_p;
4159 } else {
4160 /* not found */
4161 siblings = NULL;
4162 }
Michal Vaskoe444f752020-02-10 12:20:06 +01004163 }
4164 } else {
4165 /* no children hash table */
Michal Vaskod989ba02020-08-24 10:59:24 +02004166 for ( ; siblings; siblings = siblings->next) {
Michal Vasko8f359bf2020-07-28 10:41:15 +02004167 if (!lyd_compare_single(siblings, target, 0)) {
Michal Vaskoe444f752020-02-10 12:20:06 +01004168 break;
4169 }
4170 }
4171 }
4172
4173 if (!siblings) {
Michal Vasko9b368d32020-02-14 13:53:31 +01004174 if (match) {
4175 *match = NULL;
4176 }
Michal Vaskoe444f752020-02-10 12:20:06 +01004177 return LY_ENOTFOUND;
4178 }
4179
Michal Vasko9b368d32020-02-14 13:53:31 +01004180 if (match) {
4181 *match = (struct lyd_node *)siblings;
4182 }
Michal Vaskoe444f752020-02-10 12:20:06 +01004183 return LY_SUCCESS;
4184}
4185
Radek Krejci857189e2020-09-01 13:26:36 +02004186/**
4187 * @brief Comparison callback to match schema node with a schema of a data node.
4188 *
4189 * @param[in] val1_p Pointer to the schema node
4190 * @param[in] val2_p Pointer to the data node
Michal Vasko62524a92021-02-26 10:08:50 +01004191 * Implementation of ::lyht_value_equal_cb.
Radek Krejci857189e2020-09-01 13:26:36 +02004192 */
4193static ly_bool
4194lyd_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 +01004195{
4196 struct lysc_node *val1;
4197 struct lyd_node *val2;
4198
4199 val1 = *((struct lysc_node **)val1_p);
4200 val2 = *((struct lyd_node **)val2_p);
4201
Michal Vasko90932a92020-02-12 14:33:03 +01004202 if (val1 == val2->schema) {
4203 /* schema match is enough */
4204 return 1;
4205 } else {
4206 return 0;
4207 }
4208}
4209
Michal Vasko92239c72020-11-18 18:17:25 +01004210/**
4211 * @brief Search in the given siblings (NOT recursively) for the first schema node data instance.
4212 * Uses hashes - should be used whenever possible for best performance.
4213 *
4214 * @param[in] siblings Siblings to search in including preceding and succeeding nodes.
4215 * @param[in] schema Target data node schema to find.
4216 * @param[out] match Can be NULL, otherwise the found data node.
4217 * @return LY_SUCCESS on success, @p match set.
4218 * @return LY_ENOTFOUND if not found, @p match set to NULL.
4219 * @return LY_ERR value if another error occurred.
4220 */
Michal Vasko90932a92020-02-12 14:33:03 +01004221static LY_ERR
4222lyd_find_sibling_schema(const struct lyd_node *siblings, const struct lysc_node *schema, struct lyd_node **match)
4223{
4224 struct lyd_node **match_p;
4225 struct lyd_node_inner *parent;
4226 uint32_t hash;
Michal Vasko62524a92021-02-26 10:08:50 +01004227 lyht_value_equal_cb ht_cb;
Michal Vasko90932a92020-02-12 14:33:03 +01004228
Michal Vaskob104f112020-07-17 09:54:54 +02004229 assert(siblings && schema);
Michal Vasko90932a92020-02-12 14:33:03 +01004230
Michal Vasko9e685082021-01-29 14:49:09 +01004231 parent = siblings->parent;
Michal Vasko08fd6132020-11-18 18:17:45 +01004232 if (parent && parent->schema && parent->children_ht) {
Michal Vasko90932a92020-02-12 14:33:03 +01004233 /* calculate our hash */
4234 hash = dict_hash_multi(0, schema->module->name, strlen(schema->module->name));
4235 hash = dict_hash_multi(hash, schema->name, strlen(schema->name));
4236 hash = dict_hash_multi(hash, NULL, 0);
4237
4238 /* use special hash table function */
4239 ht_cb = lyht_set_cb(parent->children_ht, lyd_hash_table_schema_val_equal);
4240
4241 /* find by hash */
4242 if (!lyht_find(parent->children_ht, &schema, hash, (void **)&match_p)) {
4243 siblings = *match_p;
4244 } else {
4245 /* not found */
4246 siblings = NULL;
4247 }
4248
4249 /* set the original hash table compare function back */
4250 lyht_set_cb(parent->children_ht, ht_cb);
4251 } else {
Michal Vaskob104f112020-07-17 09:54:54 +02004252 /* find first sibling */
4253 if (siblings->parent) {
4254 siblings = siblings->parent->child;
4255 } else {
4256 while (siblings->prev->next) {
4257 siblings = siblings->prev;
4258 }
4259 }
4260
4261 /* search manually without hashes */
Michal Vaskod989ba02020-08-24 10:59:24 +02004262 for ( ; siblings; siblings = siblings->next) {
Michal Vasko90932a92020-02-12 14:33:03 +01004263 if (siblings->schema == schema) {
4264 /* schema match is enough */
4265 break;
4266 }
4267 }
4268 }
4269
4270 if (!siblings) {
Michal Vasko9b368d32020-02-14 13:53:31 +01004271 if (match) {
4272 *match = NULL;
4273 }
Michal Vasko90932a92020-02-12 14:33:03 +01004274 return LY_ENOTFOUND;
4275 }
4276
Michal Vasko9b368d32020-02-14 13:53:31 +01004277 if (match) {
4278 *match = (struct lyd_node *)siblings;
4279 }
Michal Vasko90932a92020-02-12 14:33:03 +01004280 return LY_SUCCESS;
4281}
4282
Michal Vaskoe444f752020-02-10 12:20:06 +01004283API LY_ERR
4284lyd_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 +02004285 size_t val_len, struct lyd_node **match)
Michal Vaskoe444f752020-02-10 12:20:06 +01004286{
4287 LY_ERR rc;
4288 struct lyd_node *target = NULL;
4289
Michal Vasko4c583e82020-07-17 12:16:14 +02004290 LY_CHECK_ARG_RET(NULL, schema, !(schema->nodetype & (LYS_CHOICE | LYS_CASE)), LY_EINVAL);
Michal Vaskoe444f752020-02-10 12:20:06 +01004291
Michal Vasko08fd6132020-11-18 18:17:45 +01004292 if (!siblings || (siblings->schema && (lysc_data_parent(siblings->schema) != lysc_data_parent(schema)))) {
Michal Vasko62ed12d2020-05-21 10:08:25 +02004293 /* no data or schema mismatch */
Michal Vasko9b368d32020-02-14 13:53:31 +01004294 if (match) {
4295 *match = NULL;
4296 }
Michal Vaskoe444f752020-02-10 12:20:06 +01004297 return LY_ENOTFOUND;
4298 }
4299
Michal Vaskof03ed032020-03-04 13:31:44 +01004300 if (key_or_value && !val_len) {
4301 val_len = strlen(key_or_value);
4302 }
4303
Michal Vaskob104f112020-07-17 09:54:54 +02004304 if ((schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) && key_or_value) {
4305 /* create a data node and find the instance */
4306 if (schema->nodetype == LYS_LEAFLIST) {
4307 /* target used attributes: schema, hash, value */
Radek Krejci8df109d2021-04-23 12:19:08 +02004308 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 +02004309 LY_CHECK_RET(rc);
Michal Vaskob104f112020-07-17 09:54:54 +02004310 } else {
Michal Vasko90932a92020-02-12 14:33:03 +01004311 /* target used attributes: schema, hash, child (all keys) */
Michal Vasko004d3152020-06-11 19:59:22 +02004312 LY_CHECK_RET(lyd_create_list2(schema, key_or_value, val_len, &target));
Michal Vasko90932a92020-02-12 14:33:03 +01004313 }
4314
4315 /* find it */
4316 rc = lyd_find_sibling_first(siblings, target, match);
Michal Vaskob104f112020-07-17 09:54:54 +02004317 } else {
4318 /* find the first schema node instance */
4319 rc = lyd_find_sibling_schema(siblings, schema, match);
Michal Vaskoe444f752020-02-10 12:20:06 +01004320 }
4321
Michal Vaskoe444f752020-02-10 12:20:06 +01004322 lyd_free_tree(target);
4323 return rc;
4324}
Michal Vaskoccc02342020-05-21 10:09:21 +02004325
4326API LY_ERR
Michal Vaskoe78faec2021-04-08 17:24:43 +02004327lyd_find_sibling_dup_inst_set(const struct lyd_node *siblings, const struct lyd_node *target, struct ly_set **set)
4328{
4329 struct lyd_node **match_p, *first, *iter;
4330 struct lyd_node_inner *parent;
4331
4332 LY_CHECK_ARG_RET(NULL, target, lysc_is_dup_inst_list(target->schema), set, LY_EINVAL);
4333
4334 LY_CHECK_RET(ly_set_new(set));
4335
4336 if (!siblings || (siblings->schema && (lysc_data_parent(siblings->schema) != lysc_data_parent(target->schema)))) {
4337 /* no data or schema mismatch */
4338 return LY_ENOTFOUND;
4339 }
4340
4341 /* get first sibling */
4342 siblings = lyd_first_sibling(siblings);
4343
4344 parent = siblings->parent;
4345 if (parent && parent->schema && parent->children_ht) {
4346 assert(target->hash);
4347
4348 /* find the first instance */
4349 lyd_find_sibling_first(siblings, target, &first);
4350 if (first) {
4351 /* add it so that it is the first in the set */
4352 if (ly_set_add(*set, first, 1, NULL)) {
4353 goto error;
4354 }
4355
4356 /* find by hash */
4357 if (!lyht_find(parent->children_ht, &target, target->hash, (void **)&match_p)) {
4358 iter = *match_p;
4359 } else {
4360 /* not found */
4361 iter = NULL;
4362 }
4363 while (iter) {
4364 /* add all found nodes into the set */
4365 if ((iter != first) && !lyd_compare_single(iter, target, 0) && ly_set_add(*set, iter, 1, NULL)) {
4366 goto error;
4367 }
4368
4369 /* find next instance */
4370 if (lyht_find_next(parent->children_ht, &iter, iter->hash, (void **)&match_p)) {
4371 iter = NULL;
4372 } else {
4373 iter = *match_p;
4374 }
4375 }
4376 }
4377 } else {
4378 /* no children hash table */
4379 LY_LIST_FOR(siblings, siblings) {
4380 if (!lyd_compare_single(target, siblings, 0)) {
4381 ly_set_add(*set, (void *)siblings, 1, NULL);
4382 }
4383 }
4384 }
4385
4386 if (!(*set)->count) {
4387 return LY_ENOTFOUND;
4388 }
4389 return LY_SUCCESS;
4390
4391error:
4392 ly_set_free(*set, NULL);
4393 *set = NULL;
4394 return LY_EMEM;
4395}
4396
4397API LY_ERR
Michal Vasko1d4af6c2021-02-22 13:31:26 +01004398lyd_find_sibling_opaq_next(const struct lyd_node *first, const char *name, struct lyd_node **match)
4399{
4400 LY_CHECK_ARG_RET(NULL, name, LY_EINVAL);
4401
4402 for ( ; first; first = first->next) {
4403 if (!first->schema && !strcmp(LYD_NAME(first), name)) {
4404 break;
4405 }
4406 }
4407
4408 if (match) {
4409 *match = (struct lyd_node *)first;
4410 }
4411 return first ? LY_SUCCESS : LY_ENOTFOUND;
4412}
4413
4414API LY_ERR
aPiecekfba75362021-10-07 12:39:48 +02004415lyd_find_xpath2(const struct lyd_node *ctx_node, const char *xpath, const struct lyxp_var *vars, struct ly_set **set)
Michal Vaskoccc02342020-05-21 10:09:21 +02004416{
4417 LY_ERR ret = LY_SUCCESS;
Michal Vasko8bd9cc72021-07-16 14:06:10 +02004418 struct lyxp_set xp_set = {0};
Radek Krejcif03a9e22020-09-18 20:09:31 +02004419 struct lyxp_expr *exp = NULL;
Michal Vaskoccc02342020-05-21 10:09:21 +02004420 uint32_t i;
4421
4422 LY_CHECK_ARG_RET(NULL, ctx_node, xpath, set, LY_EINVAL);
4423
Michal Vasko37c69432021-04-12 14:48:24 +02004424 *set = NULL;
Michal Vaskoccc02342020-05-21 10:09:21 +02004425
4426 /* compile expression */
Radek Krejcif03a9e22020-09-18 20:09:31 +02004427 ret = lyxp_expr_parse((struct ly_ctx *)LYD_CTX(ctx_node), xpath, 0, 1, &exp);
4428 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskoccc02342020-05-21 10:09:21 +02004429
4430 /* evaluate expression */
aPiecekfba75362021-10-07 12:39:48 +02004431 ret = lyxp_eval(LYD_CTX(ctx_node), exp, NULL, LY_VALUE_JSON, NULL, ctx_node, ctx_node, vars, &xp_set, LYXP_IGNORE_WHEN);
Michal Vaskoccc02342020-05-21 10:09:21 +02004432 LY_CHECK_GOTO(ret, cleanup);
4433
4434 /* allocate return set */
Radek Krejciba03a5a2020-08-27 14:40:41 +02004435 ret = ly_set_new(set);
4436 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskoccc02342020-05-21 10:09:21 +02004437
4438 /* transform into ly_set */
4439 if (xp_set.type == LYXP_SET_NODE_SET) {
4440 /* allocate memory for all the elements once (even though not all items must be elements but most likely will be) */
4441 (*set)->objs = malloc(xp_set.used * sizeof *(*set)->objs);
Michal Vaskob7be7a82020-08-20 09:09:04 +02004442 LY_CHECK_ERR_GOTO(!(*set)->objs, LOGMEM(LYD_CTX(ctx_node)); ret = LY_EMEM, cleanup);
Michal Vaskoccc02342020-05-21 10:09:21 +02004443 (*set)->size = xp_set.used;
4444
4445 for (i = 0; i < xp_set.used; ++i) {
4446 if (xp_set.val.nodes[i].type == LYXP_NODE_ELEM) {
Radek Krejci3d92e442020-10-12 12:48:13 +02004447 ret = ly_set_add(*set, xp_set.val.nodes[i].node, 1, NULL);
Radek Krejciba03a5a2020-08-27 14:40:41 +02004448 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskoccc02342020-05-21 10:09:21 +02004449 }
4450 }
4451 }
4452
4453cleanup:
Michal Vasko0691d522020-05-21 13:21:47 +02004454 lyxp_set_free_content(&xp_set);
Michal Vaskob7be7a82020-08-20 09:09:04 +02004455 lyxp_expr_free((struct ly_ctx *)LYD_CTX(ctx_node), exp);
Radek Krejci8f45abc2020-11-26 12:15:13 +01004456 if (ret) {
4457 ly_set_free(*set, NULL);
4458 *set = NULL;
4459 }
Michal Vaskoccc02342020-05-21 10:09:21 +02004460 return ret;
4461}
Radek Krejcica989142020-11-05 11:32:22 +01004462
Michal Vasko3e1f6552021-01-14 09:27:55 +01004463API LY_ERR
aPiecekfba75362021-10-07 12:39:48 +02004464lyd_find_xpath(const struct lyd_node *ctx_node, const char *xpath, struct ly_set **set)
4465{
4466 return lyd_find_xpath2(ctx_node, xpath, NULL, set);
4467}
4468
4469API LY_ERR
4470lyd_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 +02004471{
4472 LY_ERR ret = LY_SUCCESS;
Michal Vasko8bd9cc72021-07-16 14:06:10 +02004473 struct lyxp_set xp_set = {0};
Michal Vaskoc9eb3ca2021-07-16 10:20:37 +02004474 struct lyxp_expr *exp = NULL;
4475
4476 LY_CHECK_ARG_RET(NULL, ctx_node, xpath, result, LY_EINVAL);
4477
4478 /* compile expression */
4479 ret = lyxp_expr_parse((struct ly_ctx *)LYD_CTX(ctx_node), xpath, 0, 1, &exp);
4480 LY_CHECK_GOTO(ret, cleanup);
4481
4482 /* evaluate expression */
aPiecekfba75362021-10-07 12:39:48 +02004483 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 +02004484 LY_CHECK_GOTO(ret, cleanup);
4485
4486 /* transform into boolean */
4487 ret = lyxp_set_cast(&xp_set, LYXP_SET_BOOLEAN);
4488 LY_CHECK_GOTO(ret, cleanup);
4489
4490 /* set result */
4491 *result = xp_set.val.bln;
4492
4493cleanup:
4494 lyxp_set_free_content(&xp_set);
4495 lyxp_expr_free((struct ly_ctx *)LYD_CTX(ctx_node), exp);
4496 return ret;
4497}
4498
4499API LY_ERR
aPiecekfba75362021-10-07 12:39:48 +02004500lyd_eval_xpath(const struct lyd_node *ctx_node, const char *xpath, ly_bool *result)
4501{
4502 return lyd_eval_xpath2(ctx_node, xpath, NULL, result);
4503}
4504
4505API LY_ERR
Michal Vasko3e1f6552021-01-14 09:27:55 +01004506lyd_find_path(const struct lyd_node *ctx_node, const char *path, ly_bool output, struct lyd_node **match)
4507{
4508 LY_ERR ret = LY_SUCCESS;
4509 struct lyxp_expr *expr = NULL;
4510 struct ly_path *lypath = NULL;
4511
4512 LY_CHECK_ARG_RET(NULL, ctx_node, ctx_node->schema, path, LY_EINVAL);
4513
4514 /* parse the path */
Michal Vaskoed725d72021-06-23 12:03:45 +02004515 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 +01004516 LY_PATH_PREFIX_OPTIONAL, LY_PATH_PRED_SIMPLE, &expr);
4517 LY_CHECK_GOTO(ret, cleanup);
4518
4519 /* compile the path */
Michal Vaskoed725d72021-06-23 12:03:45 +02004520 ret = ly_path_compile(LYD_CTX(ctx_node), NULL, ctx_node->schema, NULL, expr,
Michal Vasko0884d212021-10-14 09:21:46 +02004521 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 +01004522 LY_CHECK_GOTO(ret, cleanup);
4523
4524 /* evaluate the path */
4525 ret = ly_path_eval_partial(lypath, ctx_node, NULL, match);
4526
4527cleanup:
4528 lyxp_expr_free(LYD_CTX(ctx_node), expr);
4529 ly_path_free(LYD_CTX(ctx_node), lypath);
4530 return ret;
4531}
4532
Michal Vaskobb22b182021-06-14 08:14:21 +02004533API LY_ERR
4534lyd_find_target(const struct ly_path *path, const struct lyd_node *tree, struct lyd_node **match)
4535{
4536 LY_ERR ret;
4537 struct lyd_node *m;
4538
4539 LY_CHECK_ARG_RET(NULL, path, LY_EINVAL);
4540
4541 ret = ly_path_eval(path, tree, &m);
4542 if (ret) {
4543 if (match) {
4544 *match = NULL;
4545 }
4546 return LY_ENOTFOUND;
4547 }
4548
4549 if (match) {
4550 *match = m;
4551 }
4552 return LY_SUCCESS;
4553}
4554
Radek Krejcica989142020-11-05 11:32:22 +01004555API uint32_t
4556lyd_list_pos(const struct lyd_node *instance)
4557{
4558 const struct lyd_node *iter = NULL;
4559 uint32_t pos = 0;
4560
4561 if (!instance || !(instance->schema->nodetype & (LYS_LIST | LYS_LEAFLIST))) {
4562 return 0;
4563 }
4564
4565 /* data instances are ordered, so we can stop when we found instance of other schema node */
4566 for (iter = instance; iter->schema == instance->schema; iter = iter->prev) {
Radek Krejcibb27df32020-11-13 15:39:16 +01004567 if (pos && (iter->next == NULL)) {
Radek Krejcica989142020-11-05 11:32:22 +01004568 /* overrun to the end of the siblings list */
4569 break;
4570 }
4571 ++pos;
4572 }
4573
4574 return pos;
4575}
Radek Krejci4233f9b2020-11-05 12:38:35 +01004576
4577API struct lyd_node *
4578lyd_first_sibling(const struct lyd_node *node)
4579{
4580 struct lyd_node *start;
4581
4582 if (!node) {
4583 return NULL;
4584 }
4585
4586 /* get the first sibling */
4587 if (node->parent) {
4588 start = node->parent->child;
4589 } else {
Radek Krejcibb27df32020-11-13 15:39:16 +01004590 for (start = (struct lyd_node *)node; start->prev->next; start = start->prev) {}
Radek Krejci4233f9b2020-11-05 12:38:35 +01004591 }
4592
4593 return start;
4594}