blob: 7e21f8718940aed60d655fa889e321ce78f81147 [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>
21#include <errno.h>
22#include <fcntl.h>
23#include <stdarg.h>
24#include <stdint.h>
Radek Krejci535ea9f2020-05-29 16:01:05 +020025#include <stdio.h>
26#include <stdlib.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020027#include <string.h>
28#include <unistd.h>
29
Radek Krejci535ea9f2020-05-29 16:01:05 +020030#include "common.h"
Michal Vasko5aa44c02020-06-29 11:47:02 +020031#include "compat.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020032#include "config.h"
33#include "context.h"
34#include "dict.h"
Michal Vaskoa6669ba2020-08-06 16:14:26 +020035#include "diff.h"
Michal Vasko90932a92020-02-12 14:33:03 +010036#include "hash_table.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020037#include "log.h"
Radek Krejci7931b192020-06-25 17:05:03 +020038#include "parser_data.h"
39#include "parser_internal.h"
Michal Vasko004d3152020-06-11 19:59:22 +020040#include "path.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020041#include "plugins_exts.h"
Radek Krejci38d85362019-09-05 16:26:38 +020042#include "plugins_exts_metadata.h"
Michal Vasko90932a92020-02-12 14:33:03 +010043#include "plugins_exts_internal.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020044#include "plugins_types.h"
45#include "set.h"
46#include "tree.h"
47#include "tree_data_internal.h"
48#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,
55 struct lyd_node **match);
56
Radek Krejci084289f2019-07-09 17:35:30 +020057LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +020058lyd_value_parse(struct lyd_node_term *node, const char *value, size_t value_len, int *dynamic, int second, int value_hint,
Michal Vaskoc8a230d2020-08-14 12:17:10 +020059 LY_PREFIX_FORMAT format, void *prefix_data, const struct lyd_node *tree)
Radek Krejci084289f2019-07-09 17:35:30 +020060{
Michal Vasko90932a92020-02-12 14:33:03 +010061 LY_ERR ret = LY_SUCCESS;
Radek Krejci084289f2019-07-09 17:35:30 +020062 struct ly_err_item *err = NULL;
63 struct ly_ctx *ctx;
64 struct lysc_type *type;
Radek Krejci1798aae2020-07-14 13:26:06 +020065 int options = value_hint | LY_TYPE_OPTS_STORE | (second ? LY_TYPE_OPTS_SECOND_CALL : 0) |
Michal Vaskof03ed032020-03-04 13:31:44 +010066 (dynamic && *dynamic ? LY_TYPE_OPTS_DYNAMIC : 0) | (tree ? 0 : LY_TYPE_OPTS_INCOMPLETE_DATA);
Radek Krejci084289f2019-07-09 17:35:30 +020067 assert(node);
68
69 ctx = node->schema->module->ctx;
Radek Krejci084289f2019-07-09 17:35:30 +020070
Radek Krejci73dead22019-07-11 16:46:16 +020071 type = ((struct lysc_node_leaf*)node->schema)->type;
Radek Krejci62903c32019-07-15 14:42:05 +020072 if (!second) {
73 node->value.realtype = type;
74 }
Michal Vaskoc8a230d2020-08-14 12:17:10 +020075 ret = type->plugin->store(ctx, type, value, value_len, options, format, prefix_data,
Michal Vasko004d3152020-06-11 19:59:22 +020076 tree ? (void *)node : (void *)node->schema, tree, &node->value, NULL, &err);
Michal Vasko90932a92020-02-12 14:33:03 +010077 if (ret && (ret != LY_EINCOMPLETE)) {
Radek Krejci73dead22019-07-11 16:46:16 +020078 if (err) {
Michal Vasko3544d1e2020-05-27 11:17:51 +020079 /* node may not be connected yet so use the schema node */
Michal Vaskof872e202020-05-27 11:49:06 +020080 if (!node->parent && lysc_data_parent(node->schema)) {
81 LOGVAL(ctx, LY_VLOG_LYSC, node->schema, err->vecode, err->msg);
82 } else {
83 LOGVAL(ctx, LY_VLOG_LYD, node, err->vecode, err->msg);
84 }
Radek Krejci73dead22019-07-11 16:46:16 +020085 ly_err_free(err);
Radek Krejci084289f2019-07-09 17:35:30 +020086 }
Radek Krejci73dead22019-07-11 16:46:16 +020087 goto error;
Michal Vasko90932a92020-02-12 14:33:03 +010088 } else if (dynamic) {
89 *dynamic = 0;
Radek Krejci084289f2019-07-09 17:35:30 +020090 }
91
92error:
93 return ret;
94}
95
Michal Vasko00cbf532020-06-15 13:58:47 +020096/* similar to lyd_value_parse except can be used just to store the value, hence also does not support a second call */
Michal Vasko004d3152020-06-11 19:59:22 +020097LY_ERR
Michal Vasko90932a92020-02-12 14:33:03 +010098lyd_value_store(struct lyd_value *val, const struct lysc_node *schema, const char *value, size_t value_len, int *dynamic,
Michal Vaskoc8a230d2020-08-14 12:17:10 +020099 LY_PREFIX_FORMAT format, void *prefix_data)
Michal Vasko90932a92020-02-12 14:33:03 +0100100{
101 LY_ERR ret = LY_SUCCESS;
102 struct ly_err_item *err = NULL;
103 struct ly_ctx *ctx;
104 struct lysc_type *type;
105 int options = LY_TYPE_OPTS_STORE | LY_TYPE_OPTS_INCOMPLETE_DATA | (dynamic && *dynamic ? LY_TYPE_OPTS_DYNAMIC : 0);
106
107 assert(val && schema && (schema->nodetype & LYD_NODE_TERM));
108
109 ctx = schema->module->ctx;
110 type = ((struct lysc_node_leaf *)schema)->type;
111 val->realtype = type;
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200112 ret = type->plugin->store(ctx, type, value, value_len, options, format, prefix_data, (void *)schema, NULL,
Michal Vasko90932a92020-02-12 14:33:03 +0100113 val, NULL, &err);
114 if (ret == LY_EINCOMPLETE) {
115 /* this is fine, we do not need it resolved */
116 ret = LY_SUCCESS;
117 } else if (ret && err) {
118 ly_err_print(err);
119 LOGVAL(ctx, LY_VLOG_STR, err->path, err->vecode, err->msg);
120 ly_err_free(err);
121 }
122 if (!ret && dynamic) {
123 *dynamic = 0;
124 }
125
126 return ret;
127}
128
Radek Krejci38d85362019-09-05 16:26:38 +0200129LY_ERR
Michal Vasko41586352020-07-13 13:54:25 +0200130lyd_value_parse_meta(const struct ly_ctx *ctx, struct lyd_meta *meta, const char *value, size_t value_len, int *dynamic,
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200131 int second, int value_hint, LY_PREFIX_FORMAT format, void *prefix_data, const struct lysc_node *ctx_snode,
132 const struct lyd_node *tree)
Radek Krejci38d85362019-09-05 16:26:38 +0200133{
Michal Vasko90932a92020-02-12 14:33:03 +0100134 LY_ERR ret = LY_SUCCESS;
Radek Krejci38d85362019-09-05 16:26:38 +0200135 struct ly_err_item *err = NULL;
Radek Krejci38d85362019-09-05 16:26:38 +0200136 struct lyext_metadata *ant;
Radek Krejci1798aae2020-07-14 13:26:06 +0200137 int options = value_hint | LY_TYPE_OPTS_STORE | (second ? LY_TYPE_OPTS_SECOND_CALL : 0) |
Michal Vaskof03ed032020-03-04 13:31:44 +0100138 (dynamic && *dynamic ? LY_TYPE_OPTS_DYNAMIC : 0) | (tree ? 0 : LY_TYPE_OPTS_INCOMPLETE_DATA);
Radek Krejci38d85362019-09-05 16:26:38 +0200139
Michal Vasko9f96a052020-03-10 09:41:45 +0100140 assert(ctx && meta && ((tree && meta->parent) || ctx_snode));
Michal Vasko8d544252020-03-02 10:19:52 +0100141
Michal Vasko9f96a052020-03-10 09:41:45 +0100142 ant = meta->annotation->data;
Radek Krejci38d85362019-09-05 16:26:38 +0200143
144 if (!second) {
Michal Vasko9f96a052020-03-10 09:41:45 +0100145 meta->value.realtype = ant->type;
Radek Krejci38d85362019-09-05 16:26:38 +0200146 }
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200147 ret = ant->type->plugin->store(ctx, ant->type, value, value_len, options, format, prefix_data,
Michal Vasko9f96a052020-03-10 09:41:45 +0100148 tree ? (void *)meta->parent : (void *)ctx_snode, tree, &meta->value, NULL, &err);
Michal Vasko90932a92020-02-12 14:33:03 +0100149 if (ret && (ret != LY_EINCOMPLETE)) {
Radek Krejci38d85362019-09-05 16:26:38 +0200150 if (err) {
151 ly_err_print(err);
152 LOGVAL(ctx, LY_VLOG_STR, err->path, err->vecode, err->msg);
153 ly_err_free(err);
154 }
155 goto error;
Michal Vasko90932a92020-02-12 14:33:03 +0100156 } else if (dynamic) {
157 *dynamic = 0;
Radek Krejci38d85362019-09-05 16:26:38 +0200158 }
159
160error:
161 return ret;
162}
163
Michal Vaskof937cfe2020-08-03 16:07:12 +0200164LY_ERR
165_lys_value_validate(const struct ly_ctx *ctx, const struct lysc_node *node, const char *value, size_t value_len,
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200166 LY_PREFIX_FORMAT format, void *prefix_data)
Radek Krejci084289f2019-07-09 17:35:30 +0200167{
168 LY_ERR rc = LY_SUCCESS;
169 struct ly_err_item *err = NULL;
170 struct lysc_type *type;
Radek Krejci084289f2019-07-09 17:35:30 +0200171
172 LY_CHECK_ARG_RET(ctx, node, value, LY_EINVAL);
173
174 if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
175 LOGARG(ctx, node);
176 return LY_EINVAL;
177 }
178
179 type = ((struct lysc_node_leaf*)node)->type;
Radek Krejci73dead22019-07-11 16:46:16 +0200180 /* just validate, no storing of enything */
181 rc = type->plugin->store(ctx ? ctx : node->module->ctx, type, value, value_len, LY_TYPE_OPTS_INCOMPLETE_DATA,
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200182 format, prefix_data, node, NULL, NULL, NULL, &err);
Radek Krejci73dead22019-07-11 16:46:16 +0200183 if (rc == LY_EINCOMPLETE) {
184 /* actually success since we do not provide the context tree and call validation with
185 * LY_TYPE_OPTS_INCOMPLETE_DATA */
186 rc = LY_SUCCESS;
187 } else if (rc && err) {
188 if (ctx) {
189 /* log only in case the ctx was provided as input parameter */
190 ly_err_print(err);
191 LOGVAL(ctx, LY_VLOG_STR, err->path, err->vecode, err->msg);
Radek Krejci084289f2019-07-09 17:35:30 +0200192 }
Radek Krejci73dead22019-07-11 16:46:16 +0200193 ly_err_free(err);
Radek Krejci084289f2019-07-09 17:35:30 +0200194 }
195
196 return rc;
197}
198
199API LY_ERR
Michal Vaskof937cfe2020-08-03 16:07:12 +0200200lys_value_validate(const struct ly_ctx *ctx, const struct lysc_node *node, const char *value, size_t value_len)
201{
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200202 return _lys_value_validate(ctx, node, value, value_len, LY_PREF_JSON, NULL);
Michal Vaskof937cfe2020-08-03 16:07:12 +0200203}
204
205API LY_ERR
Michal Vasko44685da2020-03-17 15:38:06 +0100206lyd_value_validate(const struct ly_ctx *ctx, const struct lyd_node_term *node, const char *value, size_t value_len,
Michal Vasko3701af52020-08-03 14:29:38 +0200207 const struct lyd_node *tree, struct lysc_type **realtype)
Radek Krejci084289f2019-07-09 17:35:30 +0200208{
209 LY_ERR rc;
210 struct ly_err_item *err = NULL;
211 struct lysc_type *type;
Michal Vasko3701af52020-08-03 14:29:38 +0200212 struct lyd_value val = {0};
213 int options = (tree ? 0 : LY_TYPE_OPTS_INCOMPLETE_DATA) | (realtype ? LY_TYPE_OPTS_STORE : 0);
Radek Krejci084289f2019-07-09 17:35:30 +0200214
215 LY_CHECK_ARG_RET(ctx, node, value, LY_EINVAL);
216
217 type = ((struct lysc_node_leaf*)node->schema)->type;
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200218 rc = type->plugin->store(ctx ? ctx : node->schema->module->ctx, type, value, value_len, options, LY_PREF_JSON, NULL,
219 tree ? (void*)node : (void*)node->schema, tree, &val, NULL, &err);
Radek Krejci73dead22019-07-11 16:46:16 +0200220 if (rc == LY_EINCOMPLETE) {
221 return rc;
222 } else if (rc) {
223 if (err) {
224 if (ctx) {
225 ly_err_print(err);
226 LOGVAL(ctx, LY_VLOG_STR, err->path, err->vecode, err->msg);
Radek Krejci084289f2019-07-09 17:35:30 +0200227 }
Radek Krejci73dead22019-07-11 16:46:16 +0200228 ly_err_free(err);
Radek Krejci084289f2019-07-09 17:35:30 +0200229 }
Radek Krejci73dead22019-07-11 16:46:16 +0200230 return rc;
Radek Krejci084289f2019-07-09 17:35:30 +0200231 }
232
Michal Vasko3701af52020-08-03 14:29:38 +0200233 if (realtype) {
234 *realtype = val.realtype;
235 }
236
237 type->plugin->free(ctx ? ctx : node->schema->module->ctx, &val);
Radek Krejci084289f2019-07-09 17:35:30 +0200238 return LY_SUCCESS;
239}
240
241API LY_ERR
Michal Vaskof937cfe2020-08-03 16:07:12 +0200242lyd_value_compare(const struct lyd_node_term *node, const char *value, size_t value_len, const struct lyd_node *tree)
Radek Krejci084289f2019-07-09 17:35:30 +0200243{
244 LY_ERR ret = LY_SUCCESS, rc;
245 struct ly_err_item *err = NULL;
246 struct ly_ctx *ctx;
247 struct lysc_type *type;
Radek Krejci084289f2019-07-09 17:35:30 +0200248 struct lyd_value data = {0};
Michal Vaskof03ed032020-03-04 13:31:44 +0100249 int options = LY_TYPE_OPTS_STORE | (tree ? 0 : LY_TYPE_OPTS_INCOMPLETE_DATA);
Radek Krejci084289f2019-07-09 17:35:30 +0200250
251 LY_CHECK_ARG_RET(node ? node->schema->module->ctx : NULL, node, value, LY_EINVAL);
252
253 ctx = node->schema->module->ctx;
254 type = ((struct lysc_node_leaf*)node->schema)->type;
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200255 rc = type->plugin->store(ctx, type, value, value_len, options, LY_PREF_JSON, NULL, (struct lyd_node *)node, tree, &data,
256 NULL, &err);
Radek Krejci73dead22019-07-11 16:46:16 +0200257 if (rc == LY_EINCOMPLETE) {
258 ret = rc;
259 /* continue with comparing, just remember what to return if storing is ok */
260 } else if (rc) {
261 /* value to compare is invalid */
262 ret = LY_EINVAL;
263 if (err) {
264 ly_err_free(err);
Radek Krejci084289f2019-07-09 17:35:30 +0200265 }
Radek Krejci73dead22019-07-11 16:46:16 +0200266 goto cleanup;
Radek Krejci084289f2019-07-09 17:35:30 +0200267 }
268
269 /* compare data */
Radek Krejci5af04842019-07-12 11:32:07 +0200270 if (type->plugin->compare(&node->value, &data)) {
271 /* do not assign it directly from the compare callback to keep possible LY_EINCOMPLETE from validation */
Michal Vaskob3ddccb2020-07-09 15:43:05 +0200272 ret = LY_ENOT;
Radek Krejci5af04842019-07-12 11:32:07 +0200273 }
Radek Krejci084289f2019-07-09 17:35:30 +0200274
275cleanup:
Radek Krejci62903c32019-07-15 14:42:05 +0200276 type->plugin->free(ctx, &data);
Radek Krejci084289f2019-07-09 17:35:30 +0200277
278 return ret;
279}
280
Michal Vasko5ec7cda2019-09-11 13:43:08 +0200281API const char *
282lyd_value2str(const struct lyd_node_term *node, int *dynamic)
283{
284 LY_CHECK_ARG_RET(node ? node->schema->module->ctx : NULL, node, dynamic, NULL);
285
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200286 return node->value.realtype->plugin->print(&node->value, LY_PREF_JSON, NULL, dynamic);
Michal Vasko5ec7cda2019-09-11 13:43:08 +0200287}
288
289API const char *
Michal Vasko9f96a052020-03-10 09:41:45 +0100290lyd_meta2str(const struct lyd_meta *meta, int *dynamic)
Michal Vasko5ec7cda2019-09-11 13:43:08 +0200291{
Michal Vasko9f96a052020-03-10 09:41:45 +0100292 LY_CHECK_ARG_RET(meta ? meta->parent->schema->module->ctx : NULL, meta, dynamic, NULL);
Michal Vasko5ec7cda2019-09-11 13:43:08 +0200293
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200294 return meta->value.realtype->plugin->print(&meta->value, LY_PREF_JSON, NULL, dynamic);
Michal Vasko5ec7cda2019-09-11 13:43:08 +0200295}
296
Radek Krejci7931b192020-06-25 17:05:03 +0200297static LYD_FORMAT
Michal Vasko63f3d842020-07-08 10:10:14 +0200298lyd_parse_get_format(const struct ly_in *in, LYD_FORMAT format)
Radek Krejcie7b95092019-05-15 11:03:07 +0200299{
Radek Krejcie7b95092019-05-15 11:03:07 +0200300
Radek Krejci7931b192020-06-25 17:05:03 +0200301 if (!format && in->type == LY_IN_FILEPATH) {
Radek Krejcie7b95092019-05-15 11:03:07 +0200302 /* unknown format - try to detect it from filename's suffix */
Radek Krejci7931b192020-06-25 17:05:03 +0200303 const char *path = in->method.fpath.filepath;
304 size_t len = strlen(path);
Radek Krejcie7b95092019-05-15 11:03:07 +0200305
306 /* ignore trailing whitespaces */
307 for (; len > 0 && isspace(path[len - 1]); len--);
308
309 if (len >= 5 && !strncmp(&path[len - 4], ".xml", 4)) {
310 format = LYD_XML;
Radek Krejcie7b95092019-05-15 11:03:07 +0200311 } else if (len >= 6 && !strncmp(&path[len - 5], ".json", 5)) {
312 format = LYD_JSON;
313 } else if (len >= 5 && !strncmp(&path[len - 4], ".lyb", 4)) {
314 format = LYD_LYB;
Radek Krejci7931b192020-06-25 17:05:03 +0200315 } /* else still unknown */
Radek Krejcie7b95092019-05-15 11:03:07 +0200316 }
317
Radek Krejci7931b192020-06-25 17:05:03 +0200318 return format;
319}
Radek Krejcie7b95092019-05-15 11:03:07 +0200320
Radek Krejci7931b192020-06-25 17:05:03 +0200321API LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +0200322lyd_parse_data(const struct ly_ctx *ctx, struct ly_in *in, LYD_FORMAT format, int parse_options, int validate_options,
323 struct lyd_node **tree)
Radek Krejci7931b192020-06-25 17:05:03 +0200324{
Radek Krejci1798aae2020-07-14 13:26:06 +0200325 LY_ERR ret = LY_SUCCESS;
326 struct lyd_ctx *lydctx = NULL;
327
Radek Krejci7931b192020-06-25 17:05:03 +0200328 LY_CHECK_ARG_RET(ctx, ctx, in, tree, LY_EINVAL);
329 LY_CHECK_ARG_RET(ctx, !(parse_options & ~LYD_PARSE_OPTS_MASK), LY_EINVAL);
330 LY_CHECK_ARG_RET(ctx, !(validate_options & ~LYD_VALIDATE_OPTS_MASK), LY_EINVAL);
331
332 format = lyd_parse_get_format(in, format);
333 LY_CHECK_ARG_RET(ctx, format, LY_EINVAL);
334
Radek Krejci1798aae2020-07-14 13:26:06 +0200335 /* init */
336 *tree = NULL;
337
Michal Vasko63f3d842020-07-08 10:10:14 +0200338 /* remember input position */
339 in->func_start = in->current;
Radek Krejci7931b192020-06-25 17:05:03 +0200340
341 switch (format) {
342 case LYD_XML:
Radek Krejci1798aae2020-07-14 13:26:06 +0200343 LY_CHECK_RET(lyd_parse_xml_data(ctx, in, parse_options, validate_options, tree, &lydctx));
344 break;
Radek Krejci7931b192020-06-25 17:05:03 +0200345 case LYD_JSON:
Radek Krejci1798aae2020-07-14 13:26:06 +0200346 LY_CHECK_RET(lyd_parse_json_data(ctx, in, parse_options, validate_options, tree, &lydctx));
347 break;
Radek Krejci7931b192020-06-25 17:05:03 +0200348 case LYD_LYB:
Radek Krejci1798aae2020-07-14 13:26:06 +0200349 LY_CHECK_RET(lyd_parse_lyb_data(ctx, in, parse_options, validate_options, tree, &lydctx));
350 break;
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200351 case LYD_UNKNOWN:
Radek Krejci7931b192020-06-25 17:05:03 +0200352 LOGINT_RET(ctx);
353 }
354
Radek Krejci1798aae2020-07-14 13:26:06 +0200355 if (!(parse_options & LYD_PARSE_ONLY)) {
356 uint32_t i = 0;
357 const struct lys_module *mod;
358 struct lyd_node *first, *next, **first2;
Radek Krejci7931b192020-06-25 17:05:03 +0200359
Radek Krejci1798aae2020-07-14 13:26:06 +0200360 next = *tree;
361 while (1) {
362 if (validate_options & LYD_VALIDATE_PRESENT) {
363 mod = lyd_data_next_module(&next, &first);
364 } else {
365 mod = lyd_mod_next_module(next, NULL, ctx, &i, &first);
366 }
367 if (!mod) {
368 break;
369 }
370 if (first == *tree) {
371 /* make sure first2 changes are carried to tree */
372 first2 = tree;
373 } else {
374 first2 = &first;
375 }
376
377 /* validate new top-level nodes, autodelete CANNOT occur, all nodes are new */
378 LY_CHECK_GOTO(ret = lyd_validate_new(first2, NULL, mod, NULL), cleanup);
379
380 /* add all top-level defaults for this module */
381 ret = lyd_new_implicit_r(NULL, first2, NULL, mod, &lydctx->unres_node_type, &lydctx->when_check,
382 (validate_options & LYD_VALIDATE_NO_STATE) ? LYD_IMPLICIT_NO_STATE : 0, NULL);
383 LY_CHECK_GOTO(ret, cleanup);
384
385 /* finish incompletely validated terminal values/attributes and when conditions */
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200386 switch (format) {
387 case LYD_XML:
388 ret = lyd_validate_unres(tree, &lydctx->when_check, &lydctx->unres_node_type, &lydctx->unres_meta_type,
389 LY_PREF_XML, &((struct lyxml_ctx *)lydctx->data_ctx)->ns, NULL);
390 break;
391 case LYD_JSON:
392 case LYD_LYB:
393 ret = lyd_validate_unres(tree, &lydctx->when_check, &lydctx->unres_node_type, &lydctx->unres_meta_type,
394 LY_PREF_JSON, NULL, NULL);
395 break;
396 case LYD_UNKNOWN:
397 LOGINT_RET(ctx);
398 }
Radek Krejci1798aae2020-07-14 13:26:06 +0200399 LY_CHECK_GOTO(ret, cleanup);
400
401 /* perform final validation that assumes the data tree is final */
402 LY_CHECK_GOTO(ret = lyd_validate_final_r(*first2, NULL, mod, validate_options, 0), cleanup);
403 }
404 }
405
406cleanup:
407 lydctx->free((struct lyd_ctx *)lydctx);
408 if (ret) {
409 lyd_free_all(*tree);
410 *tree = NULL;
411 }
412 return ret;
Radek Krejci7931b192020-06-25 17:05:03 +0200413}
414
415API LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +0200416lyd_parse_data_mem(const struct ly_ctx *ctx, const char *data, LYD_FORMAT format, int parse_options, int validate_options,
417 struct lyd_node **tree)
Radek Krejci7931b192020-06-25 17:05:03 +0200418{
419 LY_ERR ret;
420 struct ly_in *in;
421
422 LY_CHECK_RET(ly_in_new_memory(data, &in));
423 ret = lyd_parse_data(ctx, in, format, parse_options, validate_options, tree);
424
425 ly_in_free(in, 0);
426 return ret;
427}
428
429API LY_ERR
Michal Vaskob104f112020-07-17 09:54:54 +0200430lyd_parse_data_fd(const struct ly_ctx *ctx, int fd, LYD_FORMAT format, int parse_options, int validate_options,
431 struct lyd_node **tree)
Radek Krejci7931b192020-06-25 17:05:03 +0200432{
433 LY_ERR ret;
434 struct ly_in *in;
435
436 LY_CHECK_RET(ly_in_new_fd(fd, &in));
437 ret = lyd_parse_data(ctx, in, format, parse_options, validate_options, tree);
438
439 ly_in_free(in, 0);
440 return ret;
441}
442
443API LY_ERR
Michal Vaskob104f112020-07-17 09:54:54 +0200444lyd_parse_data_path(const struct ly_ctx *ctx, const char *path, LYD_FORMAT format, int parse_options,
445 int validate_options, struct lyd_node **tree)
Radek Krejci7931b192020-06-25 17:05:03 +0200446{
447 LY_ERR ret;
448 struct ly_in *in;
449
450 LY_CHECK_RET(ly_in_new_filepath(path, 0, &in));
451 ret = lyd_parse_data(ctx, in, format, parse_options, validate_options, tree);
452
453 ly_in_free(in, 0);
454 return ret;
455}
456
Radek Krejci7931b192020-06-25 17:05:03 +0200457API LY_ERR
458lyd_parse_rpc(const struct ly_ctx *ctx, struct ly_in *in, LYD_FORMAT format, struct lyd_node **tree, struct lyd_node **op)
459{
460 LY_CHECK_ARG_RET(ctx, ctx, in, tree, LY_EINVAL);
461
462 format = lyd_parse_get_format(in, format);
463 LY_CHECK_ARG_RET(ctx, format, LY_EINVAL);
464
Radek Krejci1798aae2020-07-14 13:26:06 +0200465 /* init */
466 *tree = NULL;
467 if (op) {
468 *op = NULL;
469 }
470
Michal Vasko63f3d842020-07-08 10:10:14 +0200471 /* remember input position */
472 in->func_start = in->current;
473
Radek Krejci7931b192020-06-25 17:05:03 +0200474 switch (format) {
475 case LYD_XML:
Michal Vasko63f3d842020-07-08 10:10:14 +0200476 return lyd_parse_xml_rpc(ctx, in, tree, op);
Radek Krejci7931b192020-06-25 17:05:03 +0200477 case LYD_JSON:
Michal Vasko63f3d842020-07-08 10:10:14 +0200478 return lyd_parse_json_rpc(ctx, in, tree, op);
Radek Krejci7931b192020-06-25 17:05:03 +0200479 case LYD_LYB:
Michal Vasko63f3d842020-07-08 10:10:14 +0200480 return lyd_parse_lyb_rpc(ctx, in, tree, op);
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200481 case LYD_UNKNOWN:
482 break;
Radek Krejci7931b192020-06-25 17:05:03 +0200483 }
484
485 LOGINT_RET(ctx);
486}
487
488API LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +0200489lyd_parse_reply(const struct lyd_node *request, struct ly_in *in, LYD_FORMAT format, struct lyd_node **tree,
490 struct lyd_node **op)
Radek Krejci7931b192020-06-25 17:05:03 +0200491{
492 LY_CHECK_ARG_RET(NULL, request, LY_EINVAL);
Radek Krejci1798aae2020-07-14 13:26:06 +0200493 LY_CHECK_ARG_RET(LYD_NODE_CTX(request), in, tree || op, LY_EINVAL);
Radek Krejci7931b192020-06-25 17:05:03 +0200494
495 format = lyd_parse_get_format(in, format);
496 LY_CHECK_ARG_RET(LYD_NODE_CTX(request), format, LY_EINVAL);
497
Radek Krejci1798aae2020-07-14 13:26:06 +0200498 /* init */
499 if (tree) {
500 *tree = NULL;
501 }
502 if (op) {
503 *op = NULL;
504 }
505
Michal Vasko63f3d842020-07-08 10:10:14 +0200506 /* remember input position */
507 in->func_start = in->current;
508
Radek Krejci7931b192020-06-25 17:05:03 +0200509 switch (format) {
510 case LYD_XML:
Michal Vasko63f3d842020-07-08 10:10:14 +0200511 return lyd_parse_xml_reply(request, in, tree, op);
Radek Krejci7931b192020-06-25 17:05:03 +0200512 case LYD_JSON:
Michal Vasko63f3d842020-07-08 10:10:14 +0200513 return lyd_parse_json_reply(request, in, tree, op);
Radek Krejci7931b192020-06-25 17:05:03 +0200514 case LYD_LYB:
Michal Vasko63f3d842020-07-08 10:10:14 +0200515 return lyd_parse_lyb_reply(request, in, tree, op);
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200516 case LYD_UNKNOWN:
517 break;
Radek Krejci7931b192020-06-25 17:05:03 +0200518 }
519
520 LOGINT_RET(LYD_NODE_CTX(request));
521}
522
523API LY_ERR
524lyd_parse_notif(const struct ly_ctx *ctx, struct ly_in *in, LYD_FORMAT format, struct lyd_node **tree, struct lyd_node **ntf)
525{
Radek Krejci1798aae2020-07-14 13:26:06 +0200526 LY_CHECK_ARG_RET(ctx, ctx, in, tree || ntf, LY_EINVAL);
Radek Krejci7931b192020-06-25 17:05:03 +0200527
528 format = lyd_parse_get_format(in, format);
529 LY_CHECK_ARG_RET(ctx, format, LY_EINVAL);
530
Radek Krejci1798aae2020-07-14 13:26:06 +0200531 /* init */
532 if (tree) {
533 *tree = NULL;
534 }
535 if (ntf) {
536 *ntf = NULL;
537 }
538
Michal Vasko63f3d842020-07-08 10:10:14 +0200539 /* remember input position */
540 in->func_start = in->current;
541
Radek Krejci7931b192020-06-25 17:05:03 +0200542 switch (format) {
543 case LYD_XML:
Michal Vasko63f3d842020-07-08 10:10:14 +0200544 return lyd_parse_xml_notif(ctx, in, tree, ntf);
Radek Krejci7931b192020-06-25 17:05:03 +0200545 case LYD_JSON:
Michal Vasko63f3d842020-07-08 10:10:14 +0200546 return lyd_parse_json_notif(ctx, in, tree, ntf);
Radek Krejci7931b192020-06-25 17:05:03 +0200547 case LYD_LYB:
Michal Vasko63f3d842020-07-08 10:10:14 +0200548 return lyd_parse_lyb_notif(ctx, in, tree, ntf);
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200549 case LYD_UNKNOWN:
550 break;
Radek Krejci7931b192020-06-25 17:05:03 +0200551 }
552
553 LOGINT_RET(ctx);
Radek Krejcie7b95092019-05-15 11:03:07 +0200554}
Radek Krejci084289f2019-07-09 17:35:30 +0200555
Michal Vasko90932a92020-02-12 14:33:03 +0100556LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +0200557lyd_create_term(const struct lysc_node *schema, const char *value, size_t value_len, int *dynamic, int value_hint,
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200558 LY_PREFIX_FORMAT format, void *prefix_data, struct lyd_node **node)
Michal Vasko90932a92020-02-12 14:33:03 +0100559{
560 LY_ERR ret;
561 struct lyd_node_term *term;
562
Michal Vasko9b368d32020-02-14 13:53:31 +0100563 assert(schema->nodetype & LYD_NODE_TERM);
564
Michal Vasko90932a92020-02-12 14:33:03 +0100565 term = calloc(1, sizeof *term);
566 LY_CHECK_ERR_RET(!term, LOGMEM(schema->module->ctx), LY_EMEM);
567
568 term->schema = schema;
569 term->prev = (struct lyd_node *)term;
Michal Vasko9b368d32020-02-14 13:53:31 +0100570 term->flags = LYD_NEW;
Michal Vasko90932a92020-02-12 14:33:03 +0100571
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200572 ret = lyd_value_parse(term, value, value_len, dynamic, 0, value_hint, format, prefix_data, NULL);
Michal Vasko90932a92020-02-12 14:33:03 +0100573 if (ret && (ret != LY_EINCOMPLETE)) {
574 free(term);
575 return ret;
576 }
Michal Vasko9b368d32020-02-14 13:53:31 +0100577 lyd_hash((struct lyd_node *)term);
578
579 *node = (struct lyd_node *)term;
580 return ret;
581}
582
583LY_ERR
584lyd_create_term2(const struct lysc_node *schema, const struct lyd_value *val, struct lyd_node **node)
585{
586 LY_ERR ret;
587 struct lyd_node_term *term;
588 struct lysc_type *type;
589
590 assert(schema->nodetype & LYD_NODE_TERM);
Michal Vasko00cbf532020-06-15 13:58:47 +0200591 assert(val && val->realtype);
Michal Vasko9b368d32020-02-14 13:53:31 +0100592
593 term = calloc(1, sizeof *term);
594 LY_CHECK_ERR_RET(!term, LOGMEM(schema->module->ctx), LY_EMEM);
595
596 term->schema = schema;
597 term->prev = (struct lyd_node *)term;
598 term->flags = LYD_NEW;
599
600 type = ((struct lysc_node_leaf *)schema)->type;
601 ret = type->plugin->duplicate(schema->module->ctx, val, &term->value);
602 if (ret) {
603 LOGERR(schema->module->ctx, ret, "Value duplication failed.");
604 free(term);
605 return ret;
606 }
Michal Vasko00cbf532020-06-15 13:58:47 +0200607 term->value.realtype = val->realtype;
Michal Vasko9b368d32020-02-14 13:53:31 +0100608 lyd_hash((struct lyd_node *)term);
Michal Vasko90932a92020-02-12 14:33:03 +0100609
610 *node = (struct lyd_node *)term;
611 return ret;
612}
613
614LY_ERR
615lyd_create_inner(const struct lysc_node *schema, struct lyd_node **node)
616{
617 struct lyd_node_inner *in;
618
Michal Vasko9b368d32020-02-14 13:53:31 +0100619 assert(schema->nodetype & LYD_NODE_INNER);
620
Michal Vasko90932a92020-02-12 14:33:03 +0100621 in = calloc(1, sizeof *in);
622 LY_CHECK_ERR_RET(!in, LOGMEM(schema->module->ctx), LY_EMEM);
623
624 in->schema = schema;
625 in->prev = (struct lyd_node *)in;
Michal Vasko9b368d32020-02-14 13:53:31 +0100626 in->flags = LYD_NEW;
Michal Vasko90932a92020-02-12 14:33:03 +0100627
Michal Vasko9b368d32020-02-14 13:53:31 +0100628 /* do not hash list with keys, we need them for the hash */
629 if ((schema->nodetype != LYS_LIST) || (schema->flags & LYS_KEYLESS)) {
630 lyd_hash((struct lyd_node *)in);
631 }
Michal Vasko90932a92020-02-12 14:33:03 +0100632
633 *node = (struct lyd_node *)in;
634 return LY_SUCCESS;
635}
636
Michal Vasko90932a92020-02-12 14:33:03 +0100637LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +0200638lyd_create_list(const struct lysc_node *schema, const struct ly_path_predicate *predicates, struct lyd_node **node)
Michal Vasko90932a92020-02-12 14:33:03 +0100639{
640 LY_ERR ret = LY_SUCCESS;
Michal Vasko90932a92020-02-12 14:33:03 +0100641 struct lyd_node *list = NULL, *key;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200642 LY_ARRAY_COUNT_TYPE u;
Michal Vasko90932a92020-02-12 14:33:03 +0100643
Michal Vasko004d3152020-06-11 19:59:22 +0200644 assert((schema->nodetype == LYS_LIST) && !(schema->flags & LYS_KEYLESS));
Michal Vasko90932a92020-02-12 14:33:03 +0100645
646 /* create list */
647 LY_CHECK_GOTO(ret = lyd_create_inner(schema, &list), cleanup);
648
Michal Vasko90932a92020-02-12 14:33:03 +0100649 /* create and insert all the keys */
Michal Vasko004d3152020-06-11 19:59:22 +0200650 LY_ARRAY_FOR(predicates, u) {
651 LY_CHECK_GOTO(ret = lyd_create_term2(predicates[u].key, &predicates[u].value, &key), cleanup);
Michal Vasko90932a92020-02-12 14:33:03 +0100652 lyd_insert_node(list, NULL, key);
653 }
654
Michal Vasko9b368d32020-02-14 13:53:31 +0100655 /* hash having all the keys */
656 lyd_hash(list);
657
Michal Vasko90932a92020-02-12 14:33:03 +0100658 /* success */
659 *node = list;
660 list = NULL;
661
662cleanup:
663 lyd_free_tree(list);
Michal Vasko004d3152020-06-11 19:59:22 +0200664 return ret;
665}
666
667static LY_ERR
668lyd_create_list2(const struct lysc_node *schema, const char *keys, size_t keys_len, struct lyd_node **node)
669{
670 LY_ERR ret = LY_SUCCESS;
671 struct lyxp_expr *expr = NULL;
672 uint16_t exp_idx = 0;
673 enum ly_path_pred_type pred_type = 0;
674 struct ly_path_predicate *predicates = NULL;
675
676 /* parse keys */
Michal Vasko6b26e742020-07-17 15:02:10 +0200677 LY_CHECK_GOTO(ret = ly_path_parse_predicate(schema->module->ctx, NULL, keys, keys_len, LY_PATH_PREFIX_OPTIONAL,
Michal Vasko004d3152020-06-11 19:59:22 +0200678 LY_PATH_PRED_KEYS, &expr), cleanup);
679
680 /* compile them */
Michal Vasko6b26e742020-07-17 15:02:10 +0200681 LY_CHECK_GOTO(ret = ly_path_compile_predicate(schema->module->ctx, NULL, NULL, schema, expr, &exp_idx,
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200682 LY_PREF_JSON, NULL, &predicates, &pred_type), cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +0200683
684 /* create the list node */
685 LY_CHECK_GOTO(ret = lyd_create_list(schema, predicates, node), cleanup);
686
687cleanup:
688 lyxp_expr_free(schema->module->ctx, expr);
689 ly_path_predicates_free(schema->module->ctx, pred_type, NULL, predicates);
Michal Vasko90932a92020-02-12 14:33:03 +0100690 return ret;
691}
692
693LY_ERR
694lyd_create_any(const struct lysc_node *schema, const void *value, LYD_ANYDATA_VALUETYPE value_type, struct lyd_node **node)
695{
696 struct lyd_node_any *any;
697
Michal Vasko9b368d32020-02-14 13:53:31 +0100698 assert(schema->nodetype & LYD_NODE_ANY);
699
Michal Vasko90932a92020-02-12 14:33:03 +0100700 any = calloc(1, sizeof *any);
701 LY_CHECK_ERR_RET(!any, LOGMEM(schema->module->ctx), LY_EMEM);
702
703 any->schema = schema;
704 any->prev = (struct lyd_node *)any;
Michal Vasko9b368d32020-02-14 13:53:31 +0100705 any->flags = LYD_NEW;
Michal Vasko90932a92020-02-12 14:33:03 +0100706
Radek Krejci1798aae2020-07-14 13:26:06 +0200707 /* TODO: convert XML/JSON strings into a opaq data tree */
708 any->value.str = value;
Michal Vasko90932a92020-02-12 14:33:03 +0100709 any->value_type = value_type;
Michal Vasko9b368d32020-02-14 13:53:31 +0100710 lyd_hash((struct lyd_node *)any);
Michal Vasko90932a92020-02-12 14:33:03 +0100711
712 *node = (struct lyd_node *)any;
713 return LY_SUCCESS;
714}
715
Michal Vasko52927e22020-03-16 17:26:14 +0100716LY_ERR
717lyd_create_opaq(const struct ly_ctx *ctx, const char *name, size_t name_len, const char *value, size_t value_len,
Radek Krejci1798aae2020-07-14 13:26:06 +0200718 int *dynamic, int value_hint, LYD_FORMAT format, struct ly_prefix *val_prefs, const char *prefix, size_t pref_len,
719 const char *module_key, size_t module_key_len, struct lyd_node **node)
Michal Vasko52927e22020-03-16 17:26:14 +0100720{
721 struct lyd_node_opaq *opaq;
722
Radek Krejci1798aae2020-07-14 13:26:06 +0200723 assert(ctx && name && name_len);
Michal Vasko52927e22020-03-16 17:26:14 +0100724
725 if (!value_len) {
726 value = "";
727 }
728
729 opaq = calloc(1, sizeof *opaq);
730 LY_CHECK_ERR_RET(!opaq, LOGMEM(ctx), LY_EMEM);
731
732 opaq->prev = (struct lyd_node *)opaq;
733
734 opaq->name = lydict_insert(ctx, name, name_len);
735 opaq->format = format;
736 if (pref_len) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200737 opaq->prefix.id = lydict_insert(ctx, prefix, pref_len);
Michal Vasko52927e22020-03-16 17:26:14 +0100738 }
Radek Krejci1798aae2020-07-14 13:26:06 +0200739 if (module_key_len) {
740 opaq->prefix.module_ns = lydict_insert(ctx, module_key, module_key_len);
741 }
742
Michal Vasko52927e22020-03-16 17:26:14 +0100743 opaq->val_prefs = val_prefs;
744 if (dynamic && *dynamic) {
745 opaq->value = lydict_insert_zc(ctx, (char *)value);
746 *dynamic = 0;
747 } else {
748 opaq->value = lydict_insert(ctx, value, value_len);
749 }
Radek Krejci1798aae2020-07-14 13:26:06 +0200750 opaq->hint = value_hint;
Michal Vasko52927e22020-03-16 17:26:14 +0100751 opaq->ctx = ctx;
752
753 *node = (struct lyd_node *)opaq;
754 return LY_SUCCESS;
755}
756
Michal Vasko3a41dff2020-07-15 14:30:28 +0200757API LY_ERR
758lyd_new_inner(struct lyd_node *parent, const struct lys_module *module, const char *name, struct lyd_node **node)
Michal Vasko013a8182020-03-03 10:46:53 +0100759{
760 struct lyd_node *ret = NULL;
761 const struct lysc_node *schema;
762 struct ly_ctx *ctx = parent ? parent->schema->module->ctx : (module ? module->ctx : NULL);
763
Michal Vasko6027eb92020-07-15 16:37:30 +0200764 LY_CHECK_ARG_RET(ctx, parent || module, parent || node, name, LY_EINVAL);
Michal Vasko013a8182020-03-03 10:46:53 +0100765
Michal Vaskof03ed032020-03-04 13:31:44 +0100766 if (!module) {
767 module = parent->schema->module;
768 }
769
Michal Vasko3a41dff2020-07-15 14:30:28 +0200770 schema = lys_find_child(parent ? parent->schema : NULL, module, name, 0,
771 LYS_CONTAINER | LYS_NOTIF | LYS_RPC | LYS_ACTION, 0);
772 LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "Inner node (and not a list) \"%s\" not found.", name), LY_ENOTFOUND);
Michal Vasko013a8182020-03-03 10:46:53 +0100773
Michal Vasko3a41dff2020-07-15 14:30:28 +0200774 LY_CHECK_RET(lyd_create_inner(schema, &ret));
775 if (parent) {
Michal Vasko013a8182020-03-03 10:46:53 +0100776 lyd_insert_node(parent, NULL, ret);
777 }
Michal Vasko3a41dff2020-07-15 14:30:28 +0200778
779 if (node) {
780 *node = ret;
781 }
782 return LY_SUCCESS;
Michal Vasko013a8182020-03-03 10:46:53 +0100783}
784
Michal Vasko3a41dff2020-07-15 14:30:28 +0200785API LY_ERR
786lyd_new_list(struct lyd_node *parent, const struct lys_module *module, const char *name, struct lyd_node **node, ...)
Michal Vasko013a8182020-03-03 10:46:53 +0100787{
788 struct lyd_node *ret = NULL, *key;
789 const struct lysc_node *schema, *key_s;
790 struct ly_ctx *ctx = parent ? parent->schema->module->ctx : (module ? module->ctx : NULL);
791 va_list ap;
792 const char *key_val;
793 LY_ERR rc = LY_SUCCESS;
794
Michal Vasko6027eb92020-07-15 16:37:30 +0200795 LY_CHECK_ARG_RET(ctx, parent || module, parent || node, name, LY_EINVAL);
Michal Vasko013a8182020-03-03 10:46:53 +0100796
Michal Vaskof03ed032020-03-04 13:31:44 +0100797 if (!module) {
798 module = parent->schema->module;
799 }
800
Michal Vasko013a8182020-03-03 10:46:53 +0100801 schema = lys_find_child(parent ? parent->schema : NULL, module, name, 0, LYS_LIST, 0);
Michal Vasko3a41dff2020-07-15 14:30:28 +0200802 LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "List node \"%s\" not found.", name), LY_ENOTFOUND);
Michal Vasko013a8182020-03-03 10:46:53 +0100803
804 /* create list inner node */
Michal Vasko3a41dff2020-07-15 14:30:28 +0200805 LY_CHECK_RET(lyd_create_inner(schema, &ret));
Michal Vasko013a8182020-03-03 10:46:53 +0100806
Michal Vasko3a41dff2020-07-15 14:30:28 +0200807 va_start(ap, node);
Michal Vasko013a8182020-03-03 10:46:53 +0100808
809 /* create and insert all the keys */
810 for (key_s = lysc_node_children(schema, 0); key_s && (key_s->flags & LYS_KEY); key_s = key_s->next) {
811 key_val = va_arg(ap, const char *);
812
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200813 rc = lyd_create_term(key_s, key_val, key_val ? strlen(key_val) : 0, NULL, 0, LY_PREF_JSON, NULL, &key);
Michal Vaskocbff3e92020-05-27 12:56:41 +0200814 LY_CHECK_GOTO(rc && (rc != LY_EINCOMPLETE), cleanup);
815 rc = LY_SUCCESS;
Michal Vasko013a8182020-03-03 10:46:53 +0100816 lyd_insert_node(ret, NULL, key);
817 }
818
Michal Vasko013a8182020-03-03 10:46:53 +0100819 if (parent) {
820 lyd_insert_node(parent, NULL, ret);
821 }
822
823cleanup:
Michal Vasko3a41dff2020-07-15 14:30:28 +0200824 va_end(ap);
Michal Vasko013a8182020-03-03 10:46:53 +0100825 if (rc) {
826 lyd_free_tree(ret);
827 ret = NULL;
Michal Vasko3a41dff2020-07-15 14:30:28 +0200828 } else if (node) {
829 *node = ret;
Michal Vasko013a8182020-03-03 10:46:53 +0100830 }
Michal Vasko3a41dff2020-07-15 14:30:28 +0200831 return rc;
Michal Vasko013a8182020-03-03 10:46:53 +0100832}
833
Michal Vasko3a41dff2020-07-15 14:30:28 +0200834API LY_ERR
835lyd_new_list2(struct lyd_node *parent, const struct lys_module *module, const char *name, const char *keys,
836 struct lyd_node **node)
Michal Vasko013a8182020-03-03 10:46:53 +0100837{
838 struct lyd_node *ret = NULL;
839 const struct lysc_node *schema;
840 struct ly_ctx *ctx = parent ? parent->schema->module->ctx : (module ? module->ctx : NULL);
841
Michal Vasko6027eb92020-07-15 16:37:30 +0200842 LY_CHECK_ARG_RET(ctx, parent || module, parent || node, name, LY_EINVAL);
Michal Vasko013a8182020-03-03 10:46:53 +0100843
Michal Vaskof03ed032020-03-04 13:31:44 +0100844 if (!module) {
845 module = parent->schema->module;
846 }
Michal Vasko004d3152020-06-11 19:59:22 +0200847 if (!keys) {
848 keys = "";
849 }
Michal Vaskof03ed032020-03-04 13:31:44 +0100850
Michal Vasko004d3152020-06-11 19:59:22 +0200851 /* find schema node */
Michal Vasko013a8182020-03-03 10:46:53 +0100852 schema = lys_find_child(parent ? parent->schema : NULL, module, name, 0, LYS_LIST, 0);
Michal Vasko3a41dff2020-07-15 14:30:28 +0200853 LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "List node \"%s\" not found.", name), LY_ENOTFOUND);
Michal Vasko013a8182020-03-03 10:46:53 +0100854
Michal Vasko004d3152020-06-11 19:59:22 +0200855 if ((schema->flags & LYS_KEYLESS) && !keys[0]) {
856 /* key-less list */
Michal Vasko3a41dff2020-07-15 14:30:28 +0200857 LY_CHECK_RET(lyd_create_inner(schema, &ret));
Michal Vasko004d3152020-06-11 19:59:22 +0200858 } else {
859 /* create the list node */
Michal Vasko3a41dff2020-07-15 14:30:28 +0200860 LY_CHECK_RET(lyd_create_list2(schema, keys, strlen(keys), &ret));
Michal Vasko004d3152020-06-11 19:59:22 +0200861 }
Michal Vasko004d3152020-06-11 19:59:22 +0200862 if (parent) {
Michal Vasko013a8182020-03-03 10:46:53 +0100863 lyd_insert_node(parent, NULL, ret);
864 }
Michal Vasko3a41dff2020-07-15 14:30:28 +0200865
866 if (node) {
867 *node = ret;
868 }
869 return LY_SUCCESS;
Michal Vasko013a8182020-03-03 10:46:53 +0100870}
871
Michal Vasko3a41dff2020-07-15 14:30:28 +0200872API LY_ERR
873lyd_new_term(struct lyd_node *parent, const struct lys_module *module, const char *name, const char *val_str,
874 struct lyd_node **node)
Michal Vasko013a8182020-03-03 10:46:53 +0100875{
Michal Vaskocbff3e92020-05-27 12:56:41 +0200876 LY_ERR rc;
Michal Vasko013a8182020-03-03 10:46:53 +0100877 struct lyd_node *ret = NULL;
878 const struct lysc_node *schema;
879 struct ly_ctx *ctx = parent ? parent->schema->module->ctx : (module ? module->ctx : NULL);
880
Michal Vasko6027eb92020-07-15 16:37:30 +0200881 LY_CHECK_ARG_RET(ctx, parent || module, parent || node, name, LY_EINVAL);
Michal Vasko013a8182020-03-03 10:46:53 +0100882
Michal Vaskof03ed032020-03-04 13:31:44 +0100883 if (!module) {
884 module = parent->schema->module;
885 }
886
Michal Vasko013a8182020-03-03 10:46:53 +0100887 schema = lys_find_child(parent ? parent->schema : NULL, module, name, 0, LYD_NODE_TERM, 0);
Michal Vasko3a41dff2020-07-15 14:30:28 +0200888 LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "Term node \"%s\" not found.", name), LY_ENOTFOUND);
Michal Vasko013a8182020-03-03 10:46:53 +0100889
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200890 rc = lyd_create_term(schema, val_str, val_str ? strlen(val_str) : 0, NULL, 0, LY_PREF_JSON, NULL, &ret);
Michal Vasko3a41dff2020-07-15 14:30:28 +0200891 LY_CHECK_RET(rc && (rc != LY_EINCOMPLETE), rc);
Michal Vaskocbff3e92020-05-27 12:56:41 +0200892 if (parent) {
Michal Vasko013a8182020-03-03 10:46:53 +0100893 lyd_insert_node(parent, NULL, ret);
894 }
Michal Vasko3a41dff2020-07-15 14:30:28 +0200895
896 if (node) {
897 *node = ret;
898 }
899 return LY_SUCCESS;
Michal Vasko013a8182020-03-03 10:46:53 +0100900}
901
Michal Vasko3a41dff2020-07-15 14:30:28 +0200902API LY_ERR
Michal Vasko013a8182020-03-03 10:46:53 +0100903lyd_new_any(struct lyd_node *parent, const struct lys_module *module, const char *name, const void *value,
Michal Vasko3a41dff2020-07-15 14:30:28 +0200904 LYD_ANYDATA_VALUETYPE value_type, struct lyd_node **node)
Michal Vasko013a8182020-03-03 10:46:53 +0100905{
906 struct lyd_node *ret = NULL;
907 const struct lysc_node *schema;
908 struct ly_ctx *ctx = parent ? parent->schema->module->ctx : (module ? module->ctx : NULL);
909
Michal Vasko6027eb92020-07-15 16:37:30 +0200910 LY_CHECK_ARG_RET(ctx, parent || module, parent || node, name, LY_EINVAL);
Michal Vasko013a8182020-03-03 10:46:53 +0100911
Michal Vaskof03ed032020-03-04 13:31:44 +0100912 if (!module) {
913 module = parent->schema->module;
914 }
915
Michal Vasko013a8182020-03-03 10:46:53 +0100916 schema = lys_find_child(parent ? parent->schema : NULL, module, name, 0, LYD_NODE_ANY, 0);
Michal Vasko3a41dff2020-07-15 14:30:28 +0200917 LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "Any node \"%s\" not found.", name), LY_ENOTFOUND);
Michal Vasko013a8182020-03-03 10:46:53 +0100918
Michal Vasko3a41dff2020-07-15 14:30:28 +0200919 LY_CHECK_RET(lyd_create_any(schema, value, value_type, &ret));
920 if (parent) {
Michal Vasko013a8182020-03-03 10:46:53 +0100921 lyd_insert_node(parent, NULL, ret);
922 }
Michal Vasko3a41dff2020-07-15 14:30:28 +0200923
924 if (node) {
925 *node = ret;
926 }
927 return LY_SUCCESS;
Michal Vasko013a8182020-03-03 10:46:53 +0100928}
929
Michal Vasko4490d312020-06-16 13:08:55 +0200930/**
931 * @brief Update node value.
932 *
933 * @param[in] node Node to update.
934 * @param[in] value New value to set.
935 * @param[in] value_type Type of @p value for any node.
936 * @param[out] new_parent Set to @p node if the value was updated, otherwise set to NULL.
937 * @param[out] new_node Set to @p node if the value was updated, otherwise set to NULL.
938 * @return LY_ERR value.
939 */
Michal Vasko00cbf532020-06-15 13:58:47 +0200940static LY_ERR
941lyd_new_path_update(struct lyd_node *node, const void *value, LYD_ANYDATA_VALUETYPE value_type,
942 struct lyd_node **new_parent, struct lyd_node **new_node)
943{
944 LY_ERR ret = LY_SUCCESS;
945 struct lyd_node *new_any;
946
947 switch (node->schema->nodetype) {
948 case LYS_CONTAINER:
949 case LYS_NOTIF:
950 case LYS_RPC:
951 case LYS_ACTION:
952 case LYS_LIST:
953 case LYS_LEAFLIST:
954 /* if it exists, there is nothing to update */
955 *new_parent = NULL;
956 *new_node = NULL;
957 break;
958 case LYS_LEAF:
959 ret = lyd_change_term(node, value);
960 if ((ret == LY_SUCCESS) || (ret == LY_EEXIST)) {
961 /* there was an actual change (at least of the default flag) */
962 *new_parent = node;
963 *new_node = node;
964 ret = LY_SUCCESS;
965 } else if (ret == LY_ENOT) {
966 /* no change */
967 *new_parent = NULL;
968 *new_node = NULL;
969 ret = LY_SUCCESS;
970 } /* else error */
971 break;
972 case LYS_ANYDATA:
973 case LYS_ANYXML:
974 /* create a new any node */
975 LY_CHECK_RET(lyd_create_any(node->schema, value, value_type, &new_any));
976
977 /* compare with the existing one */
Michal Vasko8f359bf2020-07-28 10:41:15 +0200978 if (lyd_compare_single(node, new_any, 0)) {
Michal Vasko00cbf532020-06-15 13:58:47 +0200979 /* not equal, switch values (so that we can use generic node free) */
980 ((struct lyd_node_any *)new_any)->value = ((struct lyd_node_any *)node)->value;
981 ((struct lyd_node_any *)new_any)->value_type = ((struct lyd_node_any *)node)->value_type;
982 ((struct lyd_node_any *)node)->value.str = value;
983 ((struct lyd_node_any *)node)->value_type = value_type;
984
985 *new_parent = node;
986 *new_node = node;
987 } else {
988 /* they are equal */
989 *new_parent = NULL;
990 *new_node = NULL;
991 }
992 lyd_free_tree(new_any);
993 break;
994 default:
995 LOGINT(LYD_NODE_CTX(node));
996 ret = LY_EINT;
997 break;
998 }
999
1000 return ret;
1001}
1002
Michal Vasko3a41dff2020-07-15 14:30:28 +02001003API LY_ERR
1004lyd_new_meta(struct lyd_node *parent, const struct lys_module *module, const char *name, const char *val_str,
1005 struct lyd_meta **meta)
Michal Vaskod86997b2020-05-26 15:19:54 +02001006{
1007 struct lyd_meta *ret = NULL;
Michal Vasko00cbf532020-06-15 13:58:47 +02001008 const struct ly_ctx *ctx;
Michal Vaskod86997b2020-05-26 15:19:54 +02001009 const char *prefix, *tmp;
Michal Vaskod86997b2020-05-26 15:19:54 +02001010 size_t pref_len, name_len;
1011
Michal Vasko3a41dff2020-07-15 14:30:28 +02001012 LY_CHECK_ARG_RET(NULL, parent, name, module || strchr(name, ':'), LY_EINVAL);
Michal Vasko00cbf532020-06-15 13:58:47 +02001013
1014 ctx = LYD_NODE_CTX(parent);
Michal Vaskod86997b2020-05-26 15:19:54 +02001015
1016 /* parse the name */
1017 tmp = name;
1018 if (ly_parse_nodeid(&tmp, &prefix, &pref_len, &name, &name_len) || tmp[0]) {
1019 LOGERR(ctx, LY_EINVAL, "Metadata name \"%s\" is not valid.", name);
Michal Vasko3a41dff2020-07-15 14:30:28 +02001020 return LY_EVALID;
Michal Vaskod86997b2020-05-26 15:19:54 +02001021 }
1022
1023 /* find the module */
1024 if (prefix) {
Radek Krejci0ad51f12020-07-16 12:08:12 +02001025 module = ly_ctx_get_module_implemented2(ctx, prefix, pref_len);
Michal Vasko3a41dff2020-07-15 14:30:28 +02001026 LY_CHECK_ERR_RET(!module, LOGERR(ctx, LY_EINVAL, "Module \"%.*s\" not found.", pref_len, prefix), LY_ENOTFOUND);
Michal Vaskod86997b2020-05-26 15:19:54 +02001027 }
1028
1029 /* set value if none */
1030 if (!val_str) {
1031 val_str = "";
1032 }
1033
Radek Krejci1798aae2020-07-14 13:26:06 +02001034 LY_CHECK_RET(lyd_create_meta(parent, &ret, module, name, name_len, val_str, strlen(val_str), NULL, 0,
Michal Vaskoc8a230d2020-08-14 12:17:10 +02001035 LY_PREF_JSON, NULL, parent->schema));
Michal Vasko3a41dff2020-07-15 14:30:28 +02001036
1037 if (meta) {
1038 *meta = ret;
1039 }
1040 return LY_SUCCESS;
Michal Vaskod86997b2020-05-26 15:19:54 +02001041}
1042
Michal Vasko3a41dff2020-07-15 14:30:28 +02001043API LY_ERR
Michal Vasko00cbf532020-06-15 13:58:47 +02001044lyd_new_opaq(struct lyd_node *parent, const struct ly_ctx *ctx, const char *name, const char *value,
Michal Vasko3a41dff2020-07-15 14:30:28 +02001045 const char *module_name, struct lyd_node **node)
Michal Vasko00cbf532020-06-15 13:58:47 +02001046{
1047 struct lyd_node *ret = NULL;
1048
Michal Vasko6027eb92020-07-15 16:37:30 +02001049 LY_CHECK_ARG_RET(ctx, parent || ctx, parent || node, name, module_name, LY_EINVAL);
Michal Vasko00cbf532020-06-15 13:58:47 +02001050
1051 if (!ctx) {
1052 ctx = LYD_NODE_CTX(parent);
1053 }
1054 if (!value) {
1055 value = "";
1056 }
1057
Radek Krejci1798aae2020-07-14 13:26:06 +02001058 LY_CHECK_RET(lyd_create_opaq(ctx, name, strlen(name), value, strlen(value), NULL, 0,
1059 LYD_JSON, NULL, NULL, 0, module_name, strlen(module_name), &ret));
Michal Vasko3a41dff2020-07-15 14:30:28 +02001060 if (parent) {
Michal Vasko00cbf532020-06-15 13:58:47 +02001061 lyd_insert_node(parent, NULL, ret);
1062 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02001063
1064 if (node) {
1065 *node = ret;
1066 }
1067 return LY_SUCCESS;
Michal Vasko00cbf532020-06-15 13:58:47 +02001068}
1069
Michal Vasko3a41dff2020-07-15 14:30:28 +02001070API LY_ERR
1071lyd_new_attr(struct lyd_node *parent, const char *module_name, const char *name, const char *val_str,
Radek Krejci1798aae2020-07-14 13:26:06 +02001072 struct lyd_attr **attr)
Michal Vasko00cbf532020-06-15 13:58:47 +02001073{
Radek Krejci1798aae2020-07-14 13:26:06 +02001074 struct lyd_attr *ret = NULL;
Michal Vasko00cbf532020-06-15 13:58:47 +02001075 const struct ly_ctx *ctx;
1076 const char *prefix, *tmp;
1077 size_t pref_len, name_len;
1078
Michal Vasko3a41dff2020-07-15 14:30:28 +02001079 LY_CHECK_ARG_RET(NULL, parent, !parent->schema, name, LY_EINVAL);
Michal Vasko00cbf532020-06-15 13:58:47 +02001080
1081 ctx = LYD_NODE_CTX(parent);
1082
1083 /* parse the name */
1084 tmp = name;
1085 if (ly_parse_nodeid(&tmp, &prefix, &pref_len, &name, &name_len) || tmp[0]) {
1086 LOGERR(ctx, LY_EINVAL, "Metadata name \"%s\" is not valid.", name);
Michal Vasko3a41dff2020-07-15 14:30:28 +02001087 return LY_EVALID;
Michal Vasko00cbf532020-06-15 13:58:47 +02001088 }
1089
1090 /* set value if none */
1091 if (!val_str) {
1092 val_str = "";
1093 }
1094
Radek Krejci1798aae2020-07-14 13:26:06 +02001095 LY_CHECK_RET(lyd_create_attr(parent, &ret, ctx, name, name_len, val_str, strlen(val_str), NULL, 0, LYD_JSON, NULL,
1096 prefix, pref_len, module_name, module_name ? strlen(module_name) : 0));
Michal Vasko3a41dff2020-07-15 14:30:28 +02001097
1098 if (attr) {
1099 *attr = ret;
1100 }
1101 return LY_SUCCESS;
Michal Vasko00cbf532020-06-15 13:58:47 +02001102}
1103
1104API LY_ERR
1105lyd_change_term(struct lyd_node *term, const char *val_str)
1106{
1107 LY_ERR ret = LY_SUCCESS;
1108 struct lysc_type *type;
1109 struct lyd_node_term *t;
1110 struct lyd_node *parent;
1111 struct lyd_value val = {0};
1112 int dflt_change, val_change;
1113
1114 LY_CHECK_ARG_RET(NULL, term, term->schema, term->schema->nodetype & LYD_NODE_TERM, LY_EINVAL);
1115
1116 if (!val_str) {
1117 val_str = "";
1118 }
1119 t = (struct lyd_node_term *)term;
1120 type = ((struct lysc_node_leaf *)term->schema)->type;
1121
1122 /* parse the new value */
Michal Vaskoc8a230d2020-08-14 12:17:10 +02001123 LY_CHECK_GOTO(ret = lyd_value_store(&val, term->schema, val_str, strlen(val_str), NULL, LY_PREF_JSON, NULL), cleanup);
Michal Vasko00cbf532020-06-15 13:58:47 +02001124
1125 /* compare original and new value */
1126 if (type->plugin->compare(&t->value, &val)) {
1127 /* values differ, switch them */
1128 type->plugin->free(LYD_NODE_CTX(term), &t->value);
1129 t->value = val;
1130 memset(&val, 0, sizeof val);
1131 val_change = 1;
1132 } else {
1133 val_change = 0;
1134 }
1135
1136 /* always clear the default flag */
1137 if (term->flags & LYD_DEFAULT) {
1138 for (parent = term; parent; parent = (struct lyd_node *)parent->parent) {
1139 parent->flags &= ~LYD_DEFAULT;
1140 }
1141 dflt_change = 1;
1142 } else {
1143 dflt_change = 0;
1144 }
1145
1146 if (val_change || dflt_change) {
1147 /* make the node non-validated */
1148 term->flags &= LYD_NEW;
1149 }
1150
1151 if (val_change) {
1152 if (term->schema->nodetype == LYS_LEAFLIST) {
1153 /* leaf-list needs to be hashed again and re-inserted into parent */
1154 lyd_unlink_hash(term);
1155 lyd_hash(term);
1156 LY_CHECK_GOTO(ret = lyd_insert_hash(term), cleanup);
1157 } else if ((term->schema->flags & LYS_KEY) && term->parent) {
1158 /* list needs to be updated if its key was changed */
1159 assert(term->parent->schema->nodetype == LYS_LIST);
1160 lyd_unlink_hash((struct lyd_node *)term->parent);
1161 lyd_hash((struct lyd_node *)term->parent);
1162 LY_CHECK_GOTO(ret = lyd_insert_hash((struct lyd_node *)term->parent), cleanup);
1163 } /* else leaf that is not a key, its value is not used for its hash so it does not change */
1164 }
1165
1166 /* retrun value */
1167 if (!val_change) {
1168 if (dflt_change) {
1169 /* only default flag change */
1170 ret = LY_EEXIST;
1171 } else {
1172 /* no change */
1173 ret = LY_ENOT;
1174 }
1175 } /* else value changed, LY_SUCCESS */
1176
1177cleanup:
1178 type->plugin->free(LYD_NODE_CTX(term), &val);
1179 return ret;
1180}
1181
Michal Vasko41586352020-07-13 13:54:25 +02001182API LY_ERR
1183lyd_change_meta(struct lyd_meta *meta, const char *val_str)
1184{
1185 LY_ERR ret = LY_SUCCESS;
1186 struct lyd_meta *m2;
1187 struct lyd_value val;
1188 int val_change;
1189
1190 LY_CHECK_ARG_RET(NULL, meta, LY_EINVAL);
1191
1192 if (!val_str) {
1193 val_str = "";
1194 }
1195
1196 /* parse the new value into a new meta structure */
1197 LY_CHECK_GOTO(ret = lyd_create_meta(NULL, &m2, meta->annotation->module, meta->name, strlen(meta->name), val_str,
Michal Vaskoc8a230d2020-08-14 12:17:10 +02001198 strlen(val_str), NULL, 0, LY_PREF_JSON, NULL, NULL), cleanup);
Michal Vasko41586352020-07-13 13:54:25 +02001199
1200 /* compare original and new value */
1201 if (lyd_compare_meta(meta, m2)) {
1202 /* values differ, switch them */
1203 val = meta->value;
1204 meta->value = m2->value;
1205 m2->value = val;
1206 val_change = 1;
1207 } else {
1208 val_change = 0;
1209 }
1210
1211 /* retrun value */
1212 if (!val_change) {
1213 /* no change */
1214 ret = LY_ENOT;
1215 } /* else value changed, LY_SUCCESS */
1216
1217cleanup:
1218 return ret;
1219}
1220
Michal Vasko3a41dff2020-07-15 14:30:28 +02001221API LY_ERR
1222lyd_new_path(struct lyd_node *parent, const struct ly_ctx *ctx, const char *path, const char *value, int options,
1223 struct lyd_node **node)
Michal Vasko00cbf532020-06-15 13:58:47 +02001224{
Michal Vasko3a41dff2020-07-15 14:30:28 +02001225 return lyd_new_path2(parent, ctx, path, value, 0, options, node, NULL);
Michal Vasko00cbf532020-06-15 13:58:47 +02001226}
1227
1228API LY_ERR
1229lyd_new_path2(struct lyd_node *parent, const struct ly_ctx *ctx, const char *path, const void *value,
1230 LYD_ANYDATA_VALUETYPE value_type, int options, struct lyd_node **new_parent, struct lyd_node **new_node)
1231{
1232 LY_ERR ret = LY_SUCCESS, r;
1233 struct lyxp_expr *exp = NULL;
1234 struct ly_path *p = NULL;
1235 struct lyd_node *nparent = NULL, *nnode = NULL, *node = NULL, *cur_parent;
1236 const struct lysc_node *schema;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001237 LY_ARRAY_COUNT_TYPE path_idx = 0;
Michal Vasko00cbf532020-06-15 13:58:47 +02001238 struct ly_path_predicate *pred;
1239
1240 LY_CHECK_ARG_RET(ctx, parent || ctx, path, (path[0] == '/') || parent, LY_EINVAL);
1241
1242 if (!ctx) {
1243 ctx = LYD_NODE_CTX(parent);
1244 }
1245
1246 /* parse path */
Michal Vasko6b26e742020-07-17 15:02:10 +02001247 LY_CHECK_GOTO(ret = ly_path_parse(ctx, NULL, path, strlen(path), LY_PATH_BEGIN_EITHER, LY_PATH_LREF_FALSE,
Michal Vasko00cbf532020-06-15 13:58:47 +02001248 LY_PATH_PREFIX_OPTIONAL, LY_PATH_PRED_SIMPLE, &exp), cleanup);
1249
1250 /* compile path */
1251 LY_CHECK_GOTO(ret = ly_path_compile(ctx, NULL, parent ? parent->schema : NULL, exp, LY_PATH_LREF_FALSE,
1252 options & LYD_NEWOPT_OUTPUT ? LY_PATH_OPER_OUTPUT : LY_PATH_OPER_INPUT,
Michal Vaskoc8a230d2020-08-14 12:17:10 +02001253 LY_PATH_TARGET_MANY, LY_PREF_JSON, NULL, &p), cleanup);
Michal Vasko00cbf532020-06-15 13:58:47 +02001254
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001255 schema = p[LY_ARRAY_COUNT(p) - 1].node;
1256 if ((schema->nodetype == LYS_LIST) && (p[LY_ARRAY_COUNT(p) - 1].pred_type == LY_PATH_PREDTYPE_NONE)
Michal Vasko00cbf532020-06-15 13:58:47 +02001257 && !(options & LYD_NEWOPT_OPAQ)) {
1258 LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_XPATH, "Predicate missing for %s \"%s\" in path.",
1259 lys_nodetype2str(schema->nodetype), schema->name);
1260 ret = LY_EINVAL;
1261 goto cleanup;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001262 } else if ((schema->nodetype == LYS_LEAFLIST) && (p[LY_ARRAY_COUNT(p) - 1].pred_type == LY_PATH_PREDTYPE_NONE)) {
Michal Vasko00cbf532020-06-15 13:58:47 +02001263 /* parse leafref value into a predicate, if not defined in the path */
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001264 p[LY_ARRAY_COUNT(p) - 1].pred_type = LY_PATH_PREDTYPE_LEAFLIST;
1265 LY_ARRAY_NEW_GOTO(ctx, p[LY_ARRAY_COUNT(p) - 1].predicates, pred, ret, cleanup);
Michal Vasko00cbf532020-06-15 13:58:47 +02001266
1267 if (!value) {
1268 value = "";
1269 }
1270
1271 r = LY_SUCCESS;
1272 if (options & LYD_NEWOPT_OPAQ) {
Michal Vaskof937cfe2020-08-03 16:07:12 +02001273 r = lys_value_validate(NULL, schema, value, strlen(value));
Michal Vasko00cbf532020-06-15 13:58:47 +02001274 }
1275 if (!r) {
Michal Vaskoc8a230d2020-08-14 12:17:10 +02001276 LY_CHECK_GOTO(ret = lyd_value_store(&pred->value, schema, value, strlen(value), NULL, LY_PREF_JSON, NULL), cleanup);
Michal Vasko00cbf532020-06-15 13:58:47 +02001277 } /* else we have opaq flag and the value is not valid, leavne no predicate and then create an opaque node */
1278 }
1279
1280 /* try to find any existing nodes in the path */
1281 if (parent) {
1282 ret = ly_path_eval_partial(p, parent, &path_idx, &node);
1283 if (ret == LY_SUCCESS) {
1284 /* the node exists, are we supposed to update it or is it just a default? */
1285 if (!(options & LYD_NEWOPT_UPDATE) && !(node->flags & LYD_DEFAULT)) {
1286 LOGERR(ctx, LY_EEXIST, "Path \"%s\" already exists", path);
1287 ret = LY_EEXIST;
1288 goto cleanup;
1289 }
1290
1291 /* update the existing node */
1292 ret = lyd_new_path_update(node, value, value_type, &nparent, &nnode);
1293 goto cleanup;
1294 } else if (ret == LY_EINCOMPLETE) {
1295 /* some nodes were found, adjust the iterator to the next segment */
1296 ++path_idx;
1297 } else if (ret == LY_ENOTFOUND) {
1298 /* we will create the nodes from top-level, default behavior (absolute path), or from the parent (relative path) */
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001299 if (lysc_data_parent(p[LY_ARRAY_COUNT(p) - 1].node)) {
Michal Vasko00cbf532020-06-15 13:58:47 +02001300 node = parent;
1301 }
1302 } else {
1303 /* error */
1304 goto cleanup;
1305 }
1306 }
1307
1308 /* create all the non-existing nodes in a loop */
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001309 for (; path_idx < LY_ARRAY_COUNT(p); ++path_idx) {
Michal Vasko00cbf532020-06-15 13:58:47 +02001310 cur_parent = node;
1311 schema = p[path_idx].node;
1312
1313 switch (schema->nodetype) {
1314 case LYS_LIST:
1315 if (!(schema->flags & LYS_KEYLESS)) {
1316 if ((options & LYD_NEWOPT_OPAQ) && (p[path_idx].pred_type == LY_PATH_PREDTYPE_NONE)) {
1317 /* creating opaque list without keys */
Radek Krejci1798aae2020-07-14 13:26:06 +02001318 LY_CHECK_GOTO(ret = lyd_create_opaq(ctx, schema->name, strlen(schema->name), NULL, 0, NULL, 0,
1319 LYD_JSON, NULL, NULL, 0, schema->module->name, strlen(schema->module->name), &node),
1320 cleanup);
Michal Vasko00cbf532020-06-15 13:58:47 +02001321 } else {
1322 assert(p[path_idx].pred_type == LY_PATH_PREDTYPE_LIST);
1323 LY_CHECK_GOTO(ret = lyd_create_list(schema, p[path_idx].predicates, &node), cleanup);
1324 }
1325 break;
1326 }
1327 /* fallthrough */
1328 case LYS_CONTAINER:
1329 case LYS_NOTIF:
1330 case LYS_RPC:
1331 case LYS_ACTION:
1332 LY_CHECK_GOTO(ret = lyd_create_inner(schema, &node), cleanup);
1333 break;
1334 case LYS_LEAFLIST:
1335 if ((options & LYD_NEWOPT_OPAQ) && (p[path_idx].pred_type == LY_PATH_PREDTYPE_NONE)) {
1336 /* creating opaque leaf-list without value */
Radek Krejci1798aae2020-07-14 13:26:06 +02001337 LY_CHECK_GOTO(ret = lyd_create_opaq(ctx, schema->name, strlen(schema->name), NULL, 0, NULL, 0,
1338 LYD_JSON, NULL, NULL, 0, schema->module->name, strlen(schema->module->name), &node),
1339 cleanup);
Michal Vasko00cbf532020-06-15 13:58:47 +02001340 } else {
1341 assert(p[path_idx].pred_type == LY_PATH_PREDTYPE_LEAFLIST);
1342 LY_CHECK_GOTO(ret = lyd_create_term2(schema, &p[path_idx].predicates[0].value, &node), cleanup);
1343 }
1344 break;
1345 case LYS_LEAF:
1346 /* make there is some value */
1347 if (!value) {
1348 value = "";
1349 }
1350
1351 r = LY_SUCCESS;
1352 if (options & LYD_NEWOPT_OPAQ) {
Michal Vaskof937cfe2020-08-03 16:07:12 +02001353 r = lys_value_validate(NULL, schema, value, strlen(value));
Michal Vasko00cbf532020-06-15 13:58:47 +02001354 }
1355 if (!r) {
Michal Vaskoc8a230d2020-08-14 12:17:10 +02001356 LY_CHECK_GOTO(ret = lyd_create_term(schema, value, strlen(value), NULL, 0, LY_PREF_JSON, NULL, &node), cleanup);
Michal Vasko00cbf532020-06-15 13:58:47 +02001357 } else {
1358 /* creating opaque leaf without value */
Radek Krejci1798aae2020-07-14 13:26:06 +02001359 LY_CHECK_GOTO(ret = lyd_create_opaq(ctx, schema->name, strlen(schema->name), NULL, 0, NULL, 0,
Michal Vaskoc8a230d2020-08-14 12:17:10 +02001360 LYD_JSON, NULL, NULL, 0, schema->module->name,
1361 strlen(schema->module->name), &node), cleanup);
Michal Vasko00cbf532020-06-15 13:58:47 +02001362 }
1363 break;
1364 case LYS_ANYDATA:
1365 case LYS_ANYXML:
1366 LY_CHECK_GOTO(ret = lyd_create_any(schema, value, value_type, &node), cleanup);
1367 break;
1368 default:
1369 LOGINT(ctx);
1370 ret = LY_EINT;
1371 goto cleanup;
1372 }
1373
1374 if (cur_parent) {
1375 /* connect to the parent */
1376 lyd_insert_node(cur_parent, NULL, node);
1377 } else if (parent) {
1378 /* connect to top-level siblings */
1379 lyd_insert_node(NULL, &parent, node);
1380 }
1381
1382 /* update remembered nodes */
1383 if (!nparent) {
1384 nparent = node;
1385 }
1386 nnode = node;
1387 }
1388
1389cleanup:
1390 lyxp_expr_free(ctx, exp);
1391 ly_path_free(ctx, p);
1392 if (!ret) {
1393 /* set out params only on success */
1394 if (new_parent) {
1395 *new_parent = nparent;
1396 }
1397 if (new_node) {
1398 *new_node = nnode;
1399 }
1400 }
1401 return ret;
1402}
1403
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001404LY_ERR
1405lyd_new_implicit_r(struct lyd_node *parent, struct lyd_node **first, const struct lysc_node *sparent,
1406 const struct lys_module *mod, struct ly_set *node_types, struct ly_set *node_when, int impl_opts,
1407 struct lyd_node **diff)
1408{
1409 LY_ERR ret;
1410 const struct lysc_node *iter = NULL;
1411 struct lyd_node *node;
1412 struct lyd_value **dflts;
1413 LY_ARRAY_COUNT_TYPE u;
1414
1415 assert(first && (parent || sparent || mod));
1416
1417 if (!sparent && parent) {
1418 sparent = parent->schema;
1419 }
1420
1421 while ((iter = lys_getnext(iter, sparent, mod ? mod->compiled : NULL, LYS_GETNEXT_WITHCHOICE))) {
1422 if ((impl_opts & LYD_IMPLICIT_NO_STATE) && (iter->flags & LYS_CONFIG_R)) {
1423 continue;
Michal Vasko44b19a12020-08-07 09:21:30 +02001424 } else if ((impl_opts & LYD_IMPLICIT_NO_CONFIG) && (iter->flags & LYS_CONFIG_W)) {
1425 continue;
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001426 }
1427
1428 switch (iter->nodetype) {
1429 case LYS_CHOICE:
1430 if (((struct lysc_node_choice *)iter)->dflt && !lys_getnext_data(NULL, *first, NULL, iter, NULL)) {
1431 /* create default case data */
1432 LY_CHECK_RET(lyd_new_implicit_r(parent, first, (struct lysc_node *)((struct lysc_node_choice *)iter)->dflt,
1433 NULL, node_types, node_when, impl_opts, diff));
1434 }
1435 break;
1436 case LYS_CONTAINER:
1437 if (!(iter->flags & LYS_PRESENCE) && lyd_find_sibling_val(*first, iter, NULL, 0, NULL)) {
1438 /* create default NP container */
1439 LY_CHECK_RET(lyd_create_inner(iter, &node));
1440 node->flags = LYD_DEFAULT;
1441 lyd_insert_node(parent, first, node);
1442
1443 /* cannot be a NP container with when */
1444 assert(!iter->when);
1445
1446 /* create any default children */
1447 LY_CHECK_RET(lyd_new_implicit_r(node, lyd_node_children_p(node), NULL, NULL, node_types, node_when,
1448 impl_opts, diff));
1449 }
1450 break;
1451 case LYS_LEAF:
1452 if (!(impl_opts & LYD_IMPLICIT_NO_DEFAULTS) && ((struct lysc_node_leaf *)iter)->dflt
1453 && lyd_find_sibling_val(*first, iter, NULL, 0, NULL)) {
1454 /* create default leaf */
1455 ret = lyd_create_term2(iter, ((struct lysc_node_leaf *)iter)->dflt, &node);
1456 if (ret == LY_EINCOMPLETE) {
1457 if (node_types) {
1458 /* remember to resolve type */
1459 ly_set_add(node_types, node, LY_SET_OPT_USEASLIST);
1460 }
1461 } else if (ret) {
1462 return ret;
1463 }
1464 node->flags = LYD_DEFAULT;
1465 lyd_insert_node(parent, first, node);
1466
1467 if (iter->when && node_when) {
1468 /* remember to resolve when */
1469 ly_set_add(node_when, node, LY_SET_OPT_USEASLIST);
1470 }
1471 if (diff) {
1472 /* add into diff */
1473 LY_CHECK_RET(lyd_val_diff_add(node, LYD_DIFF_OP_CREATE, diff));
1474 }
1475 }
1476 break;
1477 case LYS_LEAFLIST:
1478 if (!(impl_opts & LYD_IMPLICIT_NO_DEFAULTS) && ((struct lysc_node_leaflist *)iter)->dflts
1479 && lyd_find_sibling_val(*first, iter, NULL, 0, NULL)) {
1480 /* create all default leaf-lists */
1481 dflts = ((struct lysc_node_leaflist *)iter)->dflts;
1482 LY_ARRAY_FOR(dflts, u) {
1483 ret = lyd_create_term2(iter, dflts[u], &node);
1484 if (ret == LY_EINCOMPLETE) {
1485 if (node_types) {
1486 /* remember to resolve type */
1487 ly_set_add(node_types, node, LY_SET_OPT_USEASLIST);
1488 }
1489 } else if (ret) {
1490 return ret;
1491 }
1492 node->flags = LYD_DEFAULT;
1493 lyd_insert_node(parent, first, node);
1494
1495 if (iter->when && node_when) {
1496 /* remember to resolve when */
1497 ly_set_add(node_when, node, LY_SET_OPT_USEASLIST);
1498 }
1499 if (diff) {
1500 /* add into diff */
1501 LY_CHECK_RET(lyd_val_diff_add(node, LYD_DIFF_OP_CREATE, diff));
1502 }
1503 }
1504 }
1505 break;
1506 default:
1507 /* without defaults */
1508 break;
1509 }
1510 }
1511
1512 return LY_SUCCESS;
1513}
1514
1515API LY_ERR
1516lyd_new_implicit_tree(struct lyd_node *tree, int implicit_options, struct lyd_node **diff)
1517{
Michal Vasko56daf732020-08-10 10:57:18 +02001518 struct lyd_node *node;
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001519 LY_ERR ret = LY_SUCCESS;
1520
1521 LY_CHECK_ARG_RET(NULL, tree, LY_EINVAL);
1522 if (diff) {
1523 *diff = NULL;
1524 }
1525
Michal Vasko56daf732020-08-10 10:57:18 +02001526 LYD_TREE_DFS_BEGIN(tree, node) {
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001527 /* skip added default nodes */
1528 if (((node->flags & (LYD_DEFAULT | LYD_NEW)) != (LYD_DEFAULT | LYD_NEW))
1529 && (node->schema->nodetype & LYD_NODE_INNER)) {
1530 LY_CHECK_GOTO(ret = lyd_new_implicit_r(node, lyd_node_children_p((struct lyd_node *)node), NULL, NULL, NULL,
1531 NULL, implicit_options, diff), cleanup);
1532 }
1533
Michal Vasko56daf732020-08-10 10:57:18 +02001534 LYD_TREE_DFS_END(tree, node);
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001535 }
1536
1537cleanup:
1538 if (ret && diff) {
1539 lyd_free_all(*diff);
1540 *diff = NULL;
1541 }
1542 return ret;
1543}
1544
1545API LY_ERR
1546lyd_new_implicit_all(struct lyd_node **tree, const struct ly_ctx *ctx, int implicit_options, struct lyd_node **diff)
1547{
1548 const struct lys_module *mod;
1549 struct lyd_node *d = NULL;
1550 uint32_t i = 0;
1551 LY_ERR ret = LY_SUCCESS;
1552
1553 LY_CHECK_ARG_RET(ctx, tree, *tree || ctx, LY_EINVAL);
1554 if (diff) {
1555 *diff = NULL;
1556 }
1557 if (!ctx) {
1558 ctx = LYD_NODE_CTX(*tree);
1559 }
1560
1561 /* add nodes for each module one-by-one */
1562 while ((mod = ly_ctx_get_module_iter(ctx, &i))) {
1563 if (!mod->implemented) {
1564 continue;
1565 }
1566
1567 LY_CHECK_GOTO(ret = lyd_new_implicit_module(tree, mod, implicit_options, diff ? &d : NULL), cleanup);
1568 if (d) {
1569 /* merge into one diff */
1570 lyd_insert_sibling(*diff, d, diff);
1571
1572 d = NULL;
1573 }
1574 }
1575
1576cleanup:
1577 if (ret && diff) {
1578 lyd_free_all(*diff);
1579 *diff = NULL;
1580 }
1581 return ret;
1582}
1583
1584API LY_ERR
1585lyd_new_implicit_module(struct lyd_node **tree, const struct lys_module *module, int implicit_options, struct lyd_node **diff)
1586{
1587 struct lyd_node *root, *d = NULL;
1588 LY_ERR ret = LY_SUCCESS;
1589
1590 LY_CHECK_ARG_RET(NULL, tree, module, LY_EINVAL);
1591 if (diff) {
1592 *diff = NULL;
1593 }
1594
1595 /* add all top-level defaults for this module */
1596 LY_CHECK_GOTO(ret = lyd_new_implicit_r(NULL, tree, NULL, module, NULL, NULL, implicit_options, diff), cleanup);
1597
1598 /* process nested nodes */
1599 LY_LIST_FOR(*tree, root) {
1600 /* skip added default nodes */
1601 if ((root->flags & (LYD_DEFAULT | LYD_NEW)) != (LYD_DEFAULT | LYD_NEW)) {
1602 LY_CHECK_GOTO(ret = lyd_new_implicit_tree(root, implicit_options, diff ? &d : NULL), cleanup);
1603
1604 if (d) {
1605 /* merge into one diff */
1606 lyd_insert_sibling(*diff, d, diff);
1607
1608 d = NULL;
1609 }
1610 }
1611 }
1612
1613cleanup:
1614 if (ret && diff) {
1615 lyd_free_all(*diff);
1616 *diff = NULL;
1617 }
1618 return ret;
1619}
1620
Michal Vasko90932a92020-02-12 14:33:03 +01001621struct lyd_node *
Michal Vaskob104f112020-07-17 09:54:54 +02001622lyd_insert_get_next_anchor(const struct lyd_node *first_sibling, const struct lyd_node *new_node)
Michal Vasko90932a92020-02-12 14:33:03 +01001623{
Michal Vaskob104f112020-07-17 09:54:54 +02001624 const struct lysc_node *schema, *sparent;
Michal Vasko90932a92020-02-12 14:33:03 +01001625 struct lyd_node *match = NULL;
Michal Vaskob104f112020-07-17 09:54:54 +02001626 int found;
Michal Vasko90932a92020-02-12 14:33:03 +01001627
Michal Vaskob104f112020-07-17 09:54:54 +02001628 assert(new_node);
1629
1630 if (!first_sibling || !new_node->schema) {
1631 /* insert at the end, no next anchor */
Michal Vasko90932a92020-02-12 14:33:03 +01001632 return NULL;
1633 }
1634
Michal Vaskob104f112020-07-17 09:54:54 +02001635 if (first_sibling->parent && first_sibling->parent->children_ht) {
1636 /* find the anchor using hashes */
1637 sparent = first_sibling->parent->schema;
1638 schema = lys_getnext(new_node->schema, sparent, NULL, 0);
1639 while (schema) {
1640 /* keep trying to find the first existing instance of the closest following schema sibling,
1641 * otherwise return NULL - inserting at the end */
1642 if (!lyd_find_sibling_schema(first_sibling, schema, &match)) {
1643 break;
1644 }
1645
1646 schema = lys_getnext(schema, sparent, NULL, 0);
1647 }
1648 } else {
1649 /* find the anchor without hashes */
1650 match = (struct lyd_node *)first_sibling;
1651 if (!lysc_data_parent(new_node->schema)) {
1652 /* we are in top-level, skip all the data from preceding modules */
1653 LY_LIST_FOR(match, match) {
1654 if (!match->schema || (strcmp(lyd_owner_module(match)->name, lyd_owner_module(new_node)->name) >= 0)) {
1655 break;
1656 }
1657 }
1658 }
1659
1660 /* get the first schema sibling */
1661 sparent = lysc_data_parent(new_node->schema);
1662 schema = lys_getnext(NULL, sparent, new_node->schema->module->compiled, 0);
1663
1664 found = 0;
1665 LY_LIST_FOR(match, match) {
1666 if (!match->schema || (lyd_owner_module(match) != lyd_owner_module(new_node))) {
1667 /* we have found an opaque node, which must be at the end, so use it OR
1668 * modules do not match, so we must have traversed all the data from new_node module (if any),
1669 * we have found the first node of the next module, that is what we want */
1670 break;
1671 }
1672
1673 /* skip schema nodes until we find the instantiated one */
1674 while (!found) {
1675 if (new_node->schema == schema) {
1676 /* we have found the schema of the new node, continue search to find the first
1677 * data node with a different schema (after our schema) */
1678 found = 1;
1679 break;
1680 }
1681 if (match->schema == schema) {
1682 /* current node (match) is a data node still before the new node, continue search in data */
1683 break;
1684 }
1685 schema = lys_getnext(schema, sparent, new_node->schema->module->compiled, 0);
1686 assert(schema);
1687 }
1688
1689 if (found && (match->schema != new_node->schema)) {
1690 /* find the next node after we have found our node schema data instance */
1691 break;
1692 }
1693 }
Michal Vasko90932a92020-02-12 14:33:03 +01001694 }
1695
1696 return match;
1697}
1698
1699/**
1700 * @brief Insert node after a sibling.
1701 *
Michal Vasko9f96a052020-03-10 09:41:45 +01001702 * Handles inserting into NP containers and key-less lists.
1703 *
Michal Vasko90932a92020-02-12 14:33:03 +01001704 * @param[in] sibling Sibling to insert after.
1705 * @param[in] node Node to insert.
1706 */
1707static void
Michal Vaskof03ed032020-03-04 13:31:44 +01001708lyd_insert_after_node(struct lyd_node *sibling, struct lyd_node *node)
Michal Vasko90932a92020-02-12 14:33:03 +01001709{
Michal Vasko0249f7c2020-03-05 16:36:40 +01001710 struct lyd_node_inner *par;
1711
Michal Vasko90932a92020-02-12 14:33:03 +01001712 assert(!node->next && (node->prev == node));
1713
1714 node->next = sibling->next;
1715 node->prev = sibling;
1716 sibling->next = node;
1717 if (node->next) {
1718 /* sibling had a succeeding node */
1719 node->next->prev = node;
1720 } else {
1721 /* sibling was last, find first sibling and change its prev */
1722 if (sibling->parent) {
1723 sibling = sibling->parent->child;
1724 } else {
1725 for (; sibling->prev->next != node; sibling = sibling->prev);
1726 }
1727 sibling->prev = node;
1728 }
1729 node->parent = sibling->parent;
Michal Vasko0249f7c2020-03-05 16:36:40 +01001730
Michal Vasko9f96a052020-03-10 09:41:45 +01001731 for (par = node->parent; par; par = par->parent) {
1732 if ((par->flags & LYD_DEFAULT) && !(node->flags & LYD_DEFAULT)) {
1733 /* remove default flags from NP containers */
Michal Vasko0249f7c2020-03-05 16:36:40 +01001734 par->flags &= ~LYD_DEFAULT;
1735 }
Michal Vaskob104f112020-07-17 09:54:54 +02001736 if (par->schema && (par->schema->nodetype == LYS_LIST) && (par->schema->flags & LYS_KEYLESS)) {
Michal Vasko9f96a052020-03-10 09:41:45 +01001737 /* rehash key-less list */
1738 lyd_hash((struct lyd_node *)par);
1739 }
Michal Vasko0249f7c2020-03-05 16:36:40 +01001740 }
Michal Vasko90932a92020-02-12 14:33:03 +01001741}
1742
1743/**
1744 * @brief Insert node before a sibling.
1745 *
Michal Vasko9f96a052020-03-10 09:41:45 +01001746 * Handles inserting into NP containers and key-less lists.
1747 *
Michal Vasko90932a92020-02-12 14:33:03 +01001748 * @param[in] sibling Sibling to insert before.
1749 * @param[in] node Node to insert.
1750 */
1751static void
Michal Vaskof03ed032020-03-04 13:31:44 +01001752lyd_insert_before_node(struct lyd_node *sibling, struct lyd_node *node)
Michal Vasko90932a92020-02-12 14:33:03 +01001753{
Michal Vasko0249f7c2020-03-05 16:36:40 +01001754 struct lyd_node_inner *par;
1755
Michal Vasko90932a92020-02-12 14:33:03 +01001756 assert(!node->next && (node->prev == node));
1757
1758 node->next = sibling;
1759 /* covers situation of sibling being first */
1760 node->prev = sibling->prev;
1761 sibling->prev = node;
1762 if (node->prev->next) {
1763 /* sibling had a preceding node */
1764 node->prev->next = node;
1765 } else if (sibling->parent) {
1766 /* sibling was first and we must also change parent child pointer */
1767 sibling->parent->child = node;
1768 }
1769 node->parent = sibling->parent;
Michal Vasko0249f7c2020-03-05 16:36:40 +01001770
Michal Vasko9f96a052020-03-10 09:41:45 +01001771 for (par = node->parent; par; par = par->parent) {
1772 if ((par->flags & LYD_DEFAULT) && !(node->flags & LYD_DEFAULT)) {
1773 /* remove default flags from NP containers */
Michal Vasko0249f7c2020-03-05 16:36:40 +01001774 par->flags &= ~LYD_DEFAULT;
1775 }
Michal Vaskob104f112020-07-17 09:54:54 +02001776 if (par->schema && (par->schema->nodetype == LYS_LIST) && (par->schema->flags & LYS_KEYLESS)) {
Michal Vasko9f96a052020-03-10 09:41:45 +01001777 /* rehash key-less list */
1778 lyd_hash((struct lyd_node *)par);
1779 }
Michal Vasko0249f7c2020-03-05 16:36:40 +01001780 }
Michal Vasko90932a92020-02-12 14:33:03 +01001781}
1782
1783/**
Michal Vaskob104f112020-07-17 09:54:54 +02001784 * @brief Insert node as the first and only child of a parent.
Michal Vasko90932a92020-02-12 14:33:03 +01001785 *
Michal Vasko9f96a052020-03-10 09:41:45 +01001786 * Handles inserting into NP containers and key-less lists.
1787 *
Michal Vasko90932a92020-02-12 14:33:03 +01001788 * @param[in] parent Parent to insert into.
1789 * @param[in] node Node to insert.
1790 */
1791static void
Michal Vaskob104f112020-07-17 09:54:54 +02001792lyd_insert_only_child(struct lyd_node *parent, struct lyd_node *node)
Michal Vasko90932a92020-02-12 14:33:03 +01001793{
1794 struct lyd_node_inner *par;
1795
Michal Vaskob104f112020-07-17 09:54:54 +02001796 assert(parent && !lyd_node_children(parent, 0) && !node->next && (node->prev == node));
Michal Vasko52927e22020-03-16 17:26:14 +01001797 assert(!parent->schema || (parent->schema->nodetype & LYD_NODE_INNER));
Michal Vasko90932a92020-02-12 14:33:03 +01001798
1799 par = (struct lyd_node_inner *)parent;
1800
Michal Vaskob104f112020-07-17 09:54:54 +02001801 par->child = node;
Michal Vasko90932a92020-02-12 14:33:03 +01001802 node->parent = par;
Michal Vasko0249f7c2020-03-05 16:36:40 +01001803
Michal Vasko9f96a052020-03-10 09:41:45 +01001804 for (; par; par = par->parent) {
1805 if ((par->flags & LYD_DEFAULT) && !(node->flags & LYD_DEFAULT)) {
1806 /* remove default flags from NP containers */
Michal Vasko0249f7c2020-03-05 16:36:40 +01001807 par->flags &= ~LYD_DEFAULT;
1808 }
Michal Vasko52927e22020-03-16 17:26:14 +01001809 if (par->schema && (par->schema->nodetype == LYS_LIST) && (par->schema->flags & LYS_KEYLESS)) {
Michal Vasko9f96a052020-03-10 09:41:45 +01001810 /* rehash key-less list */
1811 lyd_hash((struct lyd_node *)par);
1812 }
Michal Vasko0249f7c2020-03-05 16:36:40 +01001813 }
Michal Vasko751cb4d2020-07-14 12:25:28 +02001814}
Michal Vasko0249f7c2020-03-05 16:36:40 +01001815
Michal Vasko751cb4d2020-07-14 12:25:28 +02001816/**
1817 * @brief Learn whether a list instance has all the keys.
1818 *
1819 * @param[in] list List instance to check.
1820 * @return non-zero if all the keys were found,
1821 * @return 0 otherwise.
1822 */
1823static int
1824lyd_insert_has_keys(const struct lyd_node *list)
1825{
1826 const struct lyd_node *key;
1827 const struct lysc_node *skey = NULL;
1828
1829 assert(list->schema->nodetype == LYS_LIST);
1830 key = lyd_node_children(list, 0);
1831 while ((skey = lys_getnext(skey, list->schema, NULL, 0)) && (skey->flags & LYS_KEY)) {
1832 if (!key || (key->schema != skey)) {
1833 /* key missing */
1834 return 0;
1835 }
1836
1837 key = key->next;
1838 }
1839
1840 /* all keys found */
1841 return 1;
Michal Vasko90932a92020-02-12 14:33:03 +01001842}
1843
1844void
Michal Vaskob104f112020-07-17 09:54:54 +02001845lyd_insert_node(struct lyd_node *parent, struct lyd_node **first_sibling_p, struct lyd_node *node)
Michal Vasko90932a92020-02-12 14:33:03 +01001846{
Michal Vaskob104f112020-07-17 09:54:54 +02001847 struct lyd_node *anchor, *first_sibling;
Michal Vasko90932a92020-02-12 14:33:03 +01001848
Michal Vaskob104f112020-07-17 09:54:54 +02001849 /* inserting list without its keys is not supported */
1850 assert((parent || first_sibling_p) && node && (node->hash || !node->schema));
Michal Vasko9b368d32020-02-14 13:53:31 +01001851
Michal Vaskob104f112020-07-17 09:54:54 +02001852 if (!parent && first_sibling_p && (*first_sibling_p) && (*first_sibling_p)->parent) {
1853 parent = (struct lyd_node *)(*first_sibling_p)->parent;
Michal Vasko9b368d32020-02-14 13:53:31 +01001854 }
Michal Vasko90932a92020-02-12 14:33:03 +01001855
Michal Vaskob104f112020-07-17 09:54:54 +02001856 /* get first sibling */
1857 first_sibling = parent ? ((struct lyd_node_inner *)parent)->child : *first_sibling_p;
Michal Vasko9f96a052020-03-10 09:41:45 +01001858
Michal Vaskob104f112020-07-17 09:54:54 +02001859 /* find the anchor, our next node, so we can insert before it */
1860 anchor = lyd_insert_get_next_anchor(first_sibling, node);
1861 if (anchor) {
1862 lyd_insert_before_node(anchor, node);
1863 } else if (first_sibling) {
1864 lyd_insert_after_node(first_sibling->prev, node);
1865 } else if (parent) {
1866 lyd_insert_only_child(parent, node);
Michal Vasko90932a92020-02-12 14:33:03 +01001867 } else {
Michal Vaskob104f112020-07-17 09:54:54 +02001868 *first_sibling_p = node;
1869 }
1870
1871 /* insert into parent HT */
1872 lyd_insert_hash(node);
1873
1874 /* finish hashes for our parent, if needed and possible */
1875 if (node->schema && (node->schema->flags & LYS_KEY) && lyd_insert_has_keys(parent)) {
1876 lyd_hash(parent);
1877
1878 /* now we can insert even the list into its parent HT */
1879 lyd_insert_hash(parent);
Michal Vasko90932a92020-02-12 14:33:03 +01001880 }
Michal Vasko90932a92020-02-12 14:33:03 +01001881}
1882
Michal Vaskof03ed032020-03-04 13:31:44 +01001883static LY_ERR
1884lyd_insert_check_schema(const struct lysc_node *parent, const struct lysc_node *schema)
1885{
1886 const struct lysc_node *par2;
1887
1888 assert(schema);
Michal Vasko62ed12d2020-05-21 10:08:25 +02001889 assert(!parent || !(parent->nodetype & (LYS_CASE | LYS_CHOICE)));
Michal Vaskof03ed032020-03-04 13:31:44 +01001890
1891 /* find schema parent */
Michal Vasko62ed12d2020-05-21 10:08:25 +02001892 par2 = lysc_data_parent(schema);
Michal Vaskof03ed032020-03-04 13:31:44 +01001893
1894 if (parent) {
1895 /* inner node */
1896 if (par2 != parent) {
Michal Vaskob104f112020-07-17 09:54:54 +02001897 LOGERR(schema->module->ctx, LY_EINVAL, "Cannot insert, parent of \"%s\" is not \"%s\".", schema->name,
1898 parent->name);
Michal Vaskof03ed032020-03-04 13:31:44 +01001899 return LY_EINVAL;
1900 }
1901 } else {
1902 /* top-level node */
1903 if (par2) {
Radek Krejcif6d14cb2020-07-02 16:11:45 +02001904 LOGERR(schema->module->ctx, LY_EINVAL, "Cannot insert, node \"%s\" is not top-level.", schema->name);
Michal Vaskof03ed032020-03-04 13:31:44 +01001905 return LY_EINVAL;
1906 }
1907 }
1908
1909 return LY_SUCCESS;
1910}
1911
1912API LY_ERR
Michal Vaskob104f112020-07-17 09:54:54 +02001913lyd_insert_child(struct lyd_node *parent, struct lyd_node *node)
Michal Vaskof03ed032020-03-04 13:31:44 +01001914{
1915 struct lyd_node *iter;
1916
Michal Vasko654bc852020-06-23 13:28:06 +02001917 LY_CHECK_ARG_RET(NULL, parent, node, parent->schema->nodetype & LYD_NODE_INNER, LY_EINVAL);
Michal Vaskof03ed032020-03-04 13:31:44 +01001918
1919 LY_CHECK_RET(lyd_insert_check_schema(parent->schema, node->schema));
1920
1921 if (node->schema->flags & LYS_KEY) {
1922 LOGERR(parent->schema->module->ctx, LY_EINVAL, "Cannot insert key \"%s\".", node->schema->name);
1923 return LY_EINVAL;
1924 }
1925
1926 if (node->parent || node->prev->next) {
1927 lyd_unlink_tree(node);
1928 }
1929
1930 while (node) {
1931 iter = node->next;
1932 lyd_unlink_tree(node);
1933 lyd_insert_node(parent, NULL, node);
1934 node = iter;
1935 }
1936 return LY_SUCCESS;
1937}
1938
1939API LY_ERR
Michal Vaskob104f112020-07-17 09:54:54 +02001940lyd_insert_sibling(struct lyd_node *sibling, struct lyd_node *node, struct lyd_node **first)
Michal Vaskob1b5c262020-03-05 14:29:47 +01001941{
1942 struct lyd_node *iter;
1943
Michal Vaskob104f112020-07-17 09:54:54 +02001944 LY_CHECK_ARG_RET(NULL, node, LY_EINVAL);
Michal Vaskob1b5c262020-03-05 14:29:47 +01001945
Michal Vaskob104f112020-07-17 09:54:54 +02001946 if (sibling) {
1947 LY_CHECK_RET(lyd_insert_check_schema(lysc_data_parent(sibling->schema), node->schema));
Michal Vaskob1b5c262020-03-05 14:29:47 +01001948 }
1949
1950 if (node->parent || node->prev->next) {
1951 lyd_unlink_tree(node);
1952 }
1953
1954 while (node) {
Michal Vaskob104f112020-07-17 09:54:54 +02001955 if (node->schema->flags & LYS_KEY) {
1956 LOGERR(LYD_NODE_CTX(node), LY_EINVAL, "Cannot insert key \"%s\".", node->schema->name);
1957 return LY_EINVAL;
1958 }
1959
Michal Vaskob1b5c262020-03-05 14:29:47 +01001960 iter = node->next;
1961 lyd_unlink_tree(node);
1962 lyd_insert_node(NULL, &sibling, node);
1963 node = iter;
1964 }
Michal Vaskob1b5c262020-03-05 14:29:47 +01001965
Michal Vaskob104f112020-07-17 09:54:54 +02001966 if (first) {
1967 /* find the first sibling */
1968 *first = sibling;
1969 while ((*first)->prev->next) {
1970 *first = (*first)->prev;
Michal Vasko0249f7c2020-03-05 16:36:40 +01001971 }
1972 }
1973
1974 return LY_SUCCESS;
1975}
1976
Michal Vaskob1b5c262020-03-05 14:29:47 +01001977API LY_ERR
Michal Vaskof03ed032020-03-04 13:31:44 +01001978lyd_insert_before(struct lyd_node *sibling, struct lyd_node *node)
1979{
1980 struct lyd_node *iter;
1981
1982 LY_CHECK_ARG_RET(NULL, sibling, node, LY_EINVAL);
1983
Michal Vasko62ed12d2020-05-21 10:08:25 +02001984 LY_CHECK_RET(lyd_insert_check_schema(lysc_data_parent(sibling->schema), node->schema));
Michal Vaskof03ed032020-03-04 13:31:44 +01001985
Michal Vaskob104f112020-07-17 09:54:54 +02001986 if (!(node->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) || !(node->schema->flags & LYS_ORDBY_USER)) {
1987 LOGERR(LYD_NODE_CTX(sibling), LY_EINVAL, "Can be used only for user-ordered nodes.");
Michal Vaskof03ed032020-03-04 13:31:44 +01001988 return LY_EINVAL;
1989 }
1990
1991 if (node->parent || node->prev->next) {
1992 lyd_unlink_tree(node);
1993 }
1994
1995 /* insert in reverse order to get the original order */
1996 node = node->prev;
1997 while (node) {
1998 iter = node->prev;
1999 lyd_unlink_tree(node);
2000
2001 lyd_insert_before_node(sibling, node);
Michal Vasko751cb4d2020-07-14 12:25:28 +02002002 lyd_insert_hash(node);
2003
Michal Vaskof03ed032020-03-04 13:31:44 +01002004 /* move the anchor accordingly */
2005 sibling = node;
2006
2007 node = (iter == node) ? NULL : iter;
2008 }
2009 return LY_SUCCESS;
2010}
2011
2012API LY_ERR
2013lyd_insert_after(struct lyd_node *sibling, struct lyd_node *node)
2014{
2015 struct lyd_node *iter;
2016
2017 LY_CHECK_ARG_RET(NULL, sibling, node, LY_EINVAL);
2018
Michal Vasko62ed12d2020-05-21 10:08:25 +02002019 LY_CHECK_RET(lyd_insert_check_schema(lysc_data_parent(sibling->schema), node->schema));
Michal Vaskof03ed032020-03-04 13:31:44 +01002020
Michal Vaskob104f112020-07-17 09:54:54 +02002021 if (!(node->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) || !(node->schema->flags & LYS_ORDBY_USER)) {
2022 LOGERR(LYD_NODE_CTX(sibling), LY_EINVAL, "Can be used only for user-ordered nodes.");
Michal Vaskof03ed032020-03-04 13:31:44 +01002023 return LY_EINVAL;
2024 }
2025
2026 if (node->parent || node->prev->next) {
2027 lyd_unlink_tree(node);
2028 }
2029
2030 while (node) {
2031 iter = node->next;
2032 lyd_unlink_tree(node);
2033
2034 lyd_insert_after_node(sibling, node);
Michal Vasko751cb4d2020-07-14 12:25:28 +02002035 lyd_insert_hash(node);
2036
Michal Vaskof03ed032020-03-04 13:31:44 +01002037 /* move the anchor accordingly */
2038 sibling = node;
2039
2040 node = iter;
2041 }
2042 return LY_SUCCESS;
2043}
2044
2045API void
2046lyd_unlink_tree(struct lyd_node *node)
2047{
2048 struct lyd_node *iter;
2049
2050 if (!node) {
2051 return;
2052 }
2053
Michal Vaskob104f112020-07-17 09:54:54 +02002054 /* update hashes while still linked into the tree */
2055 lyd_unlink_hash(node);
2056
Michal Vaskof03ed032020-03-04 13:31:44 +01002057 /* unlink from siblings */
2058 if (node->prev->next) {
2059 node->prev->next = node->next;
2060 }
2061 if (node->next) {
2062 node->next->prev = node->prev;
2063 } else {
2064 /* unlinking the last node */
2065 if (node->parent) {
2066 iter = node->parent->child;
2067 } else {
2068 iter = node->prev;
2069 while (iter->prev != node) {
2070 iter = iter->prev;
2071 }
2072 }
2073 /* update the "last" pointer from the first node */
2074 iter->prev = node->prev;
2075 }
2076
2077 /* unlink from parent */
2078 if (node->parent) {
2079 if (node->parent->child == node) {
2080 /* the node is the first child */
2081 node->parent->child = node->next;
2082 }
2083
Michal Vaskoab49dbe2020-07-17 12:32:47 +02002084 /* check for NP container whether its last non-default node is not being unlinked */
2085 if (node->parent->schema && (node->parent->schema->nodetype == LYS_CONTAINER)
2086 && !(node->parent->flags & LYD_DEFAULT) && !(node->parent->schema->flags & LYS_PRESENCE)) {
2087 LY_LIST_FOR(node->parent->child, iter) {
2088 if ((iter != node) && !(iter->flags & LYD_DEFAULT)) {
2089 break;
2090 }
2091 }
2092 if (!iter) {
2093 node->parent->flags |= LYD_DEFAULT;
2094 }
2095 }
2096
Michal Vaskof03ed032020-03-04 13:31:44 +01002097 /* check for keyless list and update its hash */
2098 for (iter = (struct lyd_node *)node->parent; iter; iter = (struct lyd_node *)iter->parent) {
Michal Vasko413c7f22020-05-05 12:34:06 +02002099 if (iter->schema && (iter->schema->flags & LYS_KEYLESS)) {
Michal Vaskof03ed032020-03-04 13:31:44 +01002100 lyd_hash(iter);
2101 }
2102 }
2103
2104 node->parent = NULL;
2105 }
2106
2107 node->next = NULL;
2108 node->prev = node;
2109}
2110
Michal Vaskoa5da3292020-08-12 13:10:50 +02002111void
Radek Krejci1798aae2020-07-14 13:26:06 +02002112lyd_insert_meta(struct lyd_node *parent, struct lyd_meta *meta)
2113{
2114 struct lyd_meta *last, *iter;
2115
2116 assert(parent);
Michal Vaskoa5da3292020-08-12 13:10:50 +02002117
2118 if (!meta) {
2119 return;
2120 }
Radek Krejci1798aae2020-07-14 13:26:06 +02002121
2122 for (iter = meta; iter; iter = iter->next) {
2123 iter->parent = parent;
2124 }
2125
2126 /* insert as the last attribute */
2127 if (parent->meta) {
2128 for (last = parent->meta; last->next; last = last->next);
2129 last->next = meta;
2130 } else {
2131 parent->meta = meta;
2132 }
2133
2134 /* remove default flags from NP containers */
2135 while (parent && (parent->schema->nodetype == LYS_CONTAINER) && (parent->flags & LYD_DEFAULT)) {
2136 parent->flags &= ~LYD_DEFAULT;
2137 parent = (struct lyd_node *)parent->parent;
2138 }
Radek Krejci1798aae2020-07-14 13:26:06 +02002139}
2140
2141LY_ERR
Michal Vasko9f96a052020-03-10 09:41:45 +01002142lyd_create_meta(struct lyd_node *parent, struct lyd_meta **meta, const struct lys_module *mod, const char *name,
Michal Vaskoc8a230d2020-08-14 12:17:10 +02002143 size_t name_len, const char *value, size_t value_len, int *dynamic, int value_hint, LY_PREFIX_FORMAT format,
2144 void *prefix_data, const struct lysc_node *ctx_snode)
Michal Vasko90932a92020-02-12 14:33:03 +01002145{
2146 LY_ERR ret;
2147 struct lysc_ext_instance *ant = NULL;
Michal Vasko9f96a052020-03-10 09:41:45 +01002148 struct lyd_meta *mt, *last;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002149 LY_ARRAY_COUNT_TYPE u;
Michal Vasko90932a92020-02-12 14:33:03 +01002150
Michal Vasko9f96a052020-03-10 09:41:45 +01002151 assert((parent || meta) && mod);
Michal Vasko6f4cbb62020-02-28 11:15:47 +01002152
Radek Krejci7eb54ba2020-05-18 16:30:04 +02002153 LY_ARRAY_FOR(mod->compiled->exts, u) {
2154 if (mod->compiled->exts[u].def->plugin == lyext_plugins_internal[LYEXT_PLUGIN_INTERNAL_ANNOTATION].plugin &&
2155 !ly_strncmp(mod->compiled->exts[u].argument, name, name_len)) {
Michal Vasko90932a92020-02-12 14:33:03 +01002156 /* we have the annotation definition */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02002157 ant = &mod->compiled->exts[u];
Michal Vasko90932a92020-02-12 14:33:03 +01002158 break;
2159 }
2160 }
2161 if (!ant) {
2162 /* attribute is not defined as a metadata annotation (RFC 7952) */
2163 LOGERR(mod->ctx, LY_EINVAL, "Annotation definition for attribute \"%s:%.*s\" not found.",
2164 mod->name, name_len, name);
2165 return LY_EINVAL;
2166 }
2167
Michal Vasko9f96a052020-03-10 09:41:45 +01002168 mt = calloc(1, sizeof *mt);
2169 LY_CHECK_ERR_RET(!mt, LOGMEM(mod->ctx), LY_EMEM);
2170 mt->parent = parent;
2171 mt->annotation = ant;
Michal Vaskoc8a230d2020-08-14 12:17:10 +02002172 ret = lyd_value_parse_meta(mod->ctx, mt, value, value_len, dynamic, 0, value_hint, format, prefix_data, ctx_snode, NULL);
Michal Vasko90932a92020-02-12 14:33:03 +01002173 if ((ret != LY_SUCCESS) && (ret != LY_EINCOMPLETE)) {
Michal Vasko9f96a052020-03-10 09:41:45 +01002174 free(mt);
Michal Vasko90932a92020-02-12 14:33:03 +01002175 return ret;
2176 }
Michal Vasko9f96a052020-03-10 09:41:45 +01002177 mt->name = lydict_insert(mod->ctx, name, name_len);
Michal Vasko90932a92020-02-12 14:33:03 +01002178
Michal Vasko6f4cbb62020-02-28 11:15:47 +01002179 /* insert as the last attribute */
2180 if (parent) {
Michal Vaskoa5da3292020-08-12 13:10:50 +02002181 lyd_insert_meta(parent, mt);
Michal Vasko9f96a052020-03-10 09:41:45 +01002182 } else if (*meta) {
2183 for (last = *meta; last->next; last = last->next);
2184 last->next = mt;
Michal Vasko90932a92020-02-12 14:33:03 +01002185 }
2186
Michal Vasko9f96a052020-03-10 09:41:45 +01002187 if (meta) {
2188 *meta = mt;
Michal Vasko90932a92020-02-12 14:33:03 +01002189 }
2190 return ret;
2191}
2192
Michal Vaskoa5da3292020-08-12 13:10:50 +02002193void
2194lyd_insert_attr(struct lyd_node *parent, struct lyd_attr *attr)
2195{
2196 struct lyd_attr *last, *iter;
2197 struct lyd_node_opaq *opaq;
2198
2199 assert(parent && !parent->schema);
2200
2201 if (!attr) {
2202 return;
2203 }
2204
2205 opaq = (struct lyd_node_opaq *)parent;
2206 for (iter = attr; iter; iter = iter->next) {
2207 iter->parent = opaq;
2208 }
2209
2210 /* insert as the last attribute */
2211 if (opaq->attr) {
2212 for (last = opaq->attr; last->next; last = last->next);
2213 last->next = attr;
2214 } else {
2215 opaq->attr = attr;
2216 }
2217}
2218
Michal Vasko52927e22020-03-16 17:26:14 +01002219LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +02002220lyd_create_attr(struct lyd_node *parent, struct lyd_attr **attr, const struct ly_ctx *ctx, const char *name,
2221 size_t name_len, const char *value, size_t value_len, int *dynamic, int value_hint, LYD_FORMAT format,
2222 struct ly_prefix *val_prefs, const char *prefix, size_t prefix_len, const char *module_key, size_t module_key_len)
Michal Vasko52927e22020-03-16 17:26:14 +01002223{
Radek Krejci1798aae2020-07-14 13:26:06 +02002224 struct lyd_attr *at, *last;
Michal Vasko52927e22020-03-16 17:26:14 +01002225
2226 assert(ctx && (parent || attr) && (!parent || !parent->schema));
2227 assert(name && name_len);
Michal Vasko52927e22020-03-16 17:26:14 +01002228
2229 if (!value_len) {
2230 value = "";
2231 }
2232
2233 at = calloc(1, sizeof *at);
2234 LY_CHECK_ERR_RET(!at, LOGMEM(ctx), LY_EMEM);
2235 at->parent = (struct lyd_node_opaq *)parent;
2236 at->name = lydict_insert(ctx, name, name_len);
2237 if (dynamic && *dynamic) {
2238 at->value = lydict_insert_zc(ctx, (char *)value);
2239 *dynamic = 0;
2240 } else {
2241 at->value = lydict_insert(ctx, value, value_len);
2242 }
2243
Radek Krejci1798aae2020-07-14 13:26:06 +02002244 at->hint = value_hint;
Michal Vasko52927e22020-03-16 17:26:14 +01002245 at->format = format;
2246 at->val_prefs = val_prefs;
Radek Krejci1798aae2020-07-14 13:26:06 +02002247 if (prefix_len) {
2248 at->prefix.id = lydict_insert(ctx, prefix, prefix_len);
2249 }
2250 if (module_key_len) {
2251 at->prefix.module_ns = lydict_insert(ctx, module_key, module_key_len);
Michal Vasko52927e22020-03-16 17:26:14 +01002252 }
2253
2254 /* insert as the last attribute */
2255 if (parent) {
Michal Vaskoa5da3292020-08-12 13:10:50 +02002256 lyd_insert_attr(parent, at);
Michal Vasko52927e22020-03-16 17:26:14 +01002257 } else if (*attr) {
2258 for (last = *attr; last->next; last = last->next);
2259 last->next = at;
2260 }
2261
2262 if (attr) {
2263 *attr = at;
2264 }
2265 return LY_SUCCESS;
2266}
2267
Radek Krejci084289f2019-07-09 17:35:30 +02002268API const struct lyd_node_term *
Michal Vasko004d3152020-06-11 19:59:22 +02002269lyd_target(const struct ly_path *path, const struct lyd_node *tree)
Radek Krejci084289f2019-07-09 17:35:30 +02002270{
Michal Vasko004d3152020-06-11 19:59:22 +02002271 struct lyd_node *target;
Radek Krejci084289f2019-07-09 17:35:30 +02002272
Michal Vasko004d3152020-06-11 19:59:22 +02002273 if (ly_path_eval(path, tree, &target)) {
2274 return NULL;
Radek Krejci084289f2019-07-09 17:35:30 +02002275 }
2276
Michal Vasko004d3152020-06-11 19:59:22 +02002277 return (struct lyd_node_term *)target;
Radek Krejci084289f2019-07-09 17:35:30 +02002278}
2279
Radek Krejci1f05b6a2019-07-18 16:15:06 +02002280API LY_ERR
Michal Vasko8f359bf2020-07-28 10:41:15 +02002281lyd_compare_single(const struct lyd_node *node1, const struct lyd_node *node2, int options)
Radek Krejci1f05b6a2019-07-18 16:15:06 +02002282{
2283 const struct lyd_node *iter1, *iter2;
2284 struct lyd_node_term *term1, *term2;
2285 struct lyd_node_any *any1, *any2;
Michal Vasko52927e22020-03-16 17:26:14 +01002286 struct lyd_node_opaq *opaq1, *opaq2;
Radek Krejci1f05b6a2019-07-18 16:15:06 +02002287 size_t len1, len2;
Radek Krejci084289f2019-07-09 17:35:30 +02002288
Radek Krejci1f05b6a2019-07-18 16:15:06 +02002289 if (!node1 || !node2) {
2290 if (node1 == node2) {
2291 return LY_SUCCESS;
2292 } else {
2293 return LY_ENOT;
2294 }
2295 }
2296
Michal Vasko52927e22020-03-16 17:26:14 +01002297 if ((LYD_NODE_CTX(node1) != LYD_NODE_CTX(node2)) || (node1->schema != node2->schema)) {
Radek Krejci1f05b6a2019-07-18 16:15:06 +02002298 return LY_ENOT;
2299 }
2300
2301 if (node1->hash != node2->hash) {
2302 return LY_ENOT;
2303 }
Michal Vasko52927e22020-03-16 17:26:14 +01002304 /* equal hashes do not mean equal nodes, they can be just in collision (or both be 0) so the nodes must be checked explicitly */
Radek Krejci1f05b6a2019-07-18 16:15:06 +02002305
Michal Vasko52927e22020-03-16 17:26:14 +01002306 if (!node1->schema) {
2307 opaq1 = (struct lyd_node_opaq *)node1;
2308 opaq2 = (struct lyd_node_opaq *)node2;
Radek Krejci1798aae2020-07-14 13:26:06 +02002309 if ((opaq1->name != opaq2->name) || (opaq1->format != opaq2->format) || (opaq1->prefix.module_ns != opaq2->prefix.module_ns)) {
Radek Krejci1f05b6a2019-07-18 16:15:06 +02002310 return LY_ENOT;
2311 }
Michal Vasko52927e22020-03-16 17:26:14 +01002312 switch (opaq1->format) {
2313 case LYD_XML:
2314 if (lyxml_value_compare(opaq1->value, opaq1->val_prefs, opaq2->value, opaq2->val_prefs)) {
2315 return LY_ENOT;
2316 }
2317 break;
Radek Krejci1798aae2020-07-14 13:26:06 +02002318 case LYD_JSON:
2319 /* prefixes in JSON are unique, so it is not necessary to canonize the values */
2320 if (strcmp(opaq1->value, opaq2->value)) {
2321 return LY_ENOT;
2322 }
2323 break;
Michal Vasko60ea6352020-06-29 13:39:39 +02002324 case LYD_LYB:
Michal Vaskoc8a230d2020-08-14 12:17:10 +02002325 case LYD_UNKNOWN:
Michal Vasko52927e22020-03-16 17:26:14 +01002326 /* not allowed */
2327 LOGINT(LYD_NODE_CTX(node1));
2328 return LY_EINT;
2329 }
2330 if (options & LYD_COMPARE_FULL_RECURSION) {
2331 iter1 = opaq1->child;
2332 iter2 = opaq2->child;
Radek Krejci1f05b6a2019-07-18 16:15:06 +02002333 goto all_children_compare;
Michal Vasko52927e22020-03-16 17:26:14 +01002334 }
2335 return LY_SUCCESS;
2336 } else {
2337 switch (node1->schema->nodetype) {
2338 case LYS_LEAF:
2339 case LYS_LEAFLIST:
2340 if (options & LYD_COMPARE_DEFAULTS) {
2341 if ((node1->flags & LYD_DEFAULT) != (node2->flags & LYD_DEFAULT)) {
2342 return LY_ENOT;
2343 }
2344 }
2345
Michal Vasko1a2bf2e2020-06-16 13:07:48 +02002346 term1 = (struct lyd_node_term *)node1;
2347 term2 = (struct lyd_node_term *)node2;
2348 if (term1->value.realtype != term2->value.realtype) {
2349 return LY_ENOT;
2350 }
Michal Vasko52927e22020-03-16 17:26:14 +01002351
Michal Vasko1a2bf2e2020-06-16 13:07:48 +02002352 return term1->value.realtype->plugin->compare(&term1->value, &term2->value);
Michal Vasko52927e22020-03-16 17:26:14 +01002353 case LYS_CONTAINER:
2354 if (options & LYD_COMPARE_DEFAULTS) {
2355 if ((node1->flags & LYD_DEFAULT) != (node2->flags & LYD_DEFAULT)) {
2356 return LY_ENOT;
2357 }
2358 }
2359 if (options & LYD_COMPARE_FULL_RECURSION) {
2360 iter1 = ((struct lyd_node_inner*)node1)->child;
2361 iter2 = ((struct lyd_node_inner*)node2)->child;
2362 goto all_children_compare;
Radek Krejci1f05b6a2019-07-18 16:15:06 +02002363 }
2364 return LY_SUCCESS;
Michal Vasko1bf09392020-03-27 12:38:10 +01002365 case LYS_RPC:
Michal Vasko52927e22020-03-16 17:26:14 +01002366 case LYS_ACTION:
2367 if (options & LYD_COMPARE_FULL_RECURSION) {
2368 /* TODO action/RPC
2369 goto all_children_compare;
2370 */
Radek Krejci1f05b6a2019-07-18 16:15:06 +02002371 }
2372 return LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +01002373 case LYS_NOTIF:
2374 if (options & LYD_COMPARE_FULL_RECURSION) {
2375 /* TODO Notification
2376 goto all_children_compare;
2377 */
2378 }
2379 return LY_SUCCESS;
2380 case LYS_LIST:
2381 iter1 = ((struct lyd_node_inner*)node1)->child;
2382 iter2 = ((struct lyd_node_inner*)node2)->child;
2383
2384 if (!(node1->schema->flags & LYS_KEYLESS) && !(options & LYD_COMPARE_FULL_RECURSION)) {
2385 /* lists with keys, their equivalence is based on their keys */
2386 for (struct lysc_node *key = ((struct lysc_node_list*)node1->schema)->child;
Michal Vaskoc57d9a92020-06-23 13:28:27 +02002387 key && (key->flags & LYS_KEY);
Michal Vasko52927e22020-03-16 17:26:14 +01002388 key = key->next) {
Michal Vasko8f359bf2020-07-28 10:41:15 +02002389 if (lyd_compare_single(iter1, iter2, options)) {
Michal Vasko52927e22020-03-16 17:26:14 +01002390 return LY_ENOT;
2391 }
2392 iter1 = iter1->next;
2393 iter2 = iter2->next;
2394 }
2395 } else {
2396 /* lists without keys, their equivalence is based on equivalence of all the children (both direct and indirect) */
2397
2398 all_children_compare:
2399 if (!iter1 && !iter2) {
2400 /* no children, nothing to compare */
2401 return LY_SUCCESS;
2402 }
2403
2404 for (; iter1 && iter2; iter1 = iter1->next, iter2 = iter2->next) {
Michal Vasko8f359bf2020-07-28 10:41:15 +02002405 if (lyd_compare_single(iter1, iter2, options | LYD_COMPARE_FULL_RECURSION)) {
Michal Vasko52927e22020-03-16 17:26:14 +01002406 return LY_ENOT;
2407 }
2408 }
2409 if (iter1 || iter2) {
2410 return LY_ENOT;
2411 }
2412 }
2413 return LY_SUCCESS;
2414 case LYS_ANYXML:
2415 case LYS_ANYDATA:
2416 any1 = (struct lyd_node_any*)node1;
2417 any2 = (struct lyd_node_any*)node2;
2418
2419 if (any1->value_type != any2->value_type) {
2420 return LY_ENOT;
2421 }
2422 switch (any1->value_type) {
2423 case LYD_ANYDATA_DATATREE:
2424 iter1 = any1->value.tree;
2425 iter2 = any2->value.tree;
2426 goto all_children_compare;
2427 case LYD_ANYDATA_STRING:
2428 case LYD_ANYDATA_XML:
2429 case LYD_ANYDATA_JSON:
2430 len1 = strlen(any1->value.str);
2431 len2 = strlen(any2->value.str);
2432 if (len1 != len2 || strcmp(any1->value.str, any2->value.str)) {
2433 return LY_ENOT;
2434 }
2435 return LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +01002436 case LYD_ANYDATA_LYB:
Michal Vasko60ea6352020-06-29 13:39:39 +02002437 len1 = lyd_lyb_data_length(any1->value.mem);
2438 len2 = lyd_lyb_data_length(any2->value.mem);
Michal Vasko52927e22020-03-16 17:26:14 +01002439 if (len1 != len2 || memcmp(any1->value.mem, any2->value.mem, len1)) {
2440 return LY_ENOT;
2441 }
2442 return LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +01002443 }
Radek Krejci1f05b6a2019-07-18 16:15:06 +02002444 }
2445 }
2446
Michal Vasko52927e22020-03-16 17:26:14 +01002447 LOGINT(LYD_NODE_CTX(node1));
Radek Krejci1f05b6a2019-07-18 16:15:06 +02002448 return LY_EINT;
2449}
Radek Krejci22ebdba2019-07-25 13:59:43 +02002450
Michal Vasko21725742020-06-29 11:49:25 +02002451API LY_ERR
Michal Vasko8f359bf2020-07-28 10:41:15 +02002452lyd_compare_siblings(const struct lyd_node *node1, const struct lyd_node *node2, int options)
2453{
2454 for (; node1 && node2; node1 = node1->next, node2 = node2->next) {
2455 LY_CHECK_RET(lyd_compare_single(node1, node2, options));
2456 }
2457
Michal Vasko11a81432020-07-28 16:31:29 +02002458 if (node1 == node2) {
2459 return LY_SUCCESS;
Michal Vasko8f359bf2020-07-28 10:41:15 +02002460 }
Michal Vasko11a81432020-07-28 16:31:29 +02002461 return LY_ENOT;
Michal Vasko8f359bf2020-07-28 10:41:15 +02002462}
2463
2464API LY_ERR
Michal Vasko21725742020-06-29 11:49:25 +02002465lyd_compare_meta(const struct lyd_meta *meta1, const struct lyd_meta *meta2)
2466{
2467 if (!meta1 || !meta2) {
2468 if (meta1 == meta2) {
2469 return LY_SUCCESS;
2470 } else {
2471 return LY_ENOT;
2472 }
2473 }
2474
2475 if ((LYD_NODE_CTX(meta1->parent) != LYD_NODE_CTX(meta2->parent)) || (meta1->annotation != meta2->annotation)) {
2476 return LY_ENOT;
2477 }
2478
2479 if (meta1->value.realtype != meta2->value.realtype) {
2480 return LY_ENOT;
2481 }
2482
2483 return meta1->value.realtype->plugin->compare(&meta1->value, &meta2->value);
2484}
2485
Radek Krejci22ebdba2019-07-25 13:59:43 +02002486/**
Michal Vasko52927e22020-03-16 17:26:14 +01002487 * @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 +02002488 *
2489 * 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 +02002490 *
2491 * @param[in] node Original node to duplicate
2492 * @param[in] parent Parent to insert into, NULL for top-level sibling.
2493 * @param[in,out] first First sibling, NULL if no top-level sibling exist yet. Can be also NULL if @p parent is set.
2494 * @param[in] options Bitmask of options flags, see @ref dupoptions.
2495 * @param[out] dup_p Pointer where the created duplicated node is placed (besides connecting it int @p parent / @p first sibling).
2496 * @return LY_ERR value
Radek Krejci22ebdba2019-07-25 13:59:43 +02002497 */
Michal Vasko52927e22020-03-16 17:26:14 +01002498static LY_ERR
Michal Vasko3a41dff2020-07-15 14:30:28 +02002499lyd_dup_r(const struct lyd_node *node, struct lyd_node *parent, struct lyd_node **first, int options,
2500 struct lyd_node **dup_p)
Radek Krejci22ebdba2019-07-25 13:59:43 +02002501{
Michal Vasko52927e22020-03-16 17:26:14 +01002502 LY_ERR ret;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002503 struct lyd_node *dup = NULL;
Michal Vasko61551fa2020-07-09 15:45:45 +02002504 struct lyd_meta *meta;
2505 struct lyd_node_any *any;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002506 LY_ARRAY_COUNT_TYPE u;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002507
Michal Vasko52927e22020-03-16 17:26:14 +01002508 LY_CHECK_ARG_RET(NULL, node, LY_EINVAL);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002509
Michal Vasko52927e22020-03-16 17:26:14 +01002510 if (!node->schema) {
2511 dup = calloc(1, sizeof(struct lyd_node_opaq));
2512 } else {
2513 switch (node->schema->nodetype) {
Michal Vasko1bf09392020-03-27 12:38:10 +01002514 case LYS_RPC:
Michal Vasko52927e22020-03-16 17:26:14 +01002515 case LYS_ACTION:
2516 case LYS_NOTIF:
2517 case LYS_CONTAINER:
2518 case LYS_LIST:
2519 dup = calloc(1, sizeof(struct lyd_node_inner));
2520 break;
2521 case LYS_LEAF:
2522 case LYS_LEAFLIST:
2523 dup = calloc(1, sizeof(struct lyd_node_term));
2524 break;
2525 case LYS_ANYDATA:
2526 case LYS_ANYXML:
2527 dup = calloc(1, sizeof(struct lyd_node_any));
2528 break;
2529 default:
2530 LOGINT(LYD_NODE_CTX(node));
2531 ret = LY_EINT;
2532 goto error;
2533 }
Radek Krejci22ebdba2019-07-25 13:59:43 +02002534 }
Michal Vasko52927e22020-03-16 17:26:14 +01002535 LY_CHECK_ERR_GOTO(!dup, LOGMEM(LYD_NODE_CTX(node)); ret = LY_EMEM, error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002536
Michal Vaskof6df0a02020-06-16 13:08:34 +02002537 if (options & LYD_DUP_WITH_FLAGS) {
2538 dup->flags = node->flags;
2539 } else {
2540 dup->flags = (node->flags & LYD_DEFAULT) | LYD_NEW;
2541 }
Radek Krejci22ebdba2019-07-25 13:59:43 +02002542 dup->schema = node->schema;
Michal Vasko52927e22020-03-16 17:26:14 +01002543 dup->prev = dup;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002544
Michal Vasko25a32822020-07-09 15:48:22 +02002545 /* duplicate metadata */
2546 if (!(options & LYD_DUP_NO_META)) {
2547 LY_LIST_FOR(node->meta, meta) {
Michal Vasko3a41dff2020-07-15 14:30:28 +02002548 LY_CHECK_GOTO(ret = lyd_dup_meta_single(meta, dup, NULL), error);
Michal Vasko25a32822020-07-09 15:48:22 +02002549 }
2550 }
Radek Krejci22ebdba2019-07-25 13:59:43 +02002551
2552 /* nodetype-specific work */
Michal Vasko52927e22020-03-16 17:26:14 +01002553 if (!dup->schema) {
2554 struct lyd_node_opaq *opaq = (struct lyd_node_opaq *)dup;
2555 struct lyd_node_opaq *orig = (struct lyd_node_opaq *)node;
2556 struct lyd_node *child;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002557
2558 if (options & LYD_DUP_RECURSIVE) {
2559 /* duplicate all the children */
2560 LY_LIST_FOR(orig->child, child) {
Michal Vasko3a41dff2020-07-15 14:30:28 +02002561 LY_CHECK_GOTO(ret = lyd_dup_r(child, dup, NULL, options, NULL), error);
Michal Vasko52927e22020-03-16 17:26:14 +01002562 }
2563 }
2564 opaq->name = lydict_insert(LYD_NODE_CTX(node), orig->name, 0);
2565 opaq->format = orig->format;
Radek Krejci1798aae2020-07-14 13:26:06 +02002566 if (orig->prefix.id) {
2567 opaq->prefix.id = lydict_insert(LYD_NODE_CTX(node), orig->prefix.id, 0);
Michal Vasko52927e22020-03-16 17:26:14 +01002568 }
Radek Krejci1798aae2020-07-14 13:26:06 +02002569 opaq->prefix.module_ns = lydict_insert(LYD_NODE_CTX(node), orig->prefix.module_ns, 0);
Michal Vasko52927e22020-03-16 17:26:14 +01002570 if (orig->val_prefs) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002571 LY_ARRAY_CREATE_GOTO(LYD_NODE_CTX(node), opaq->val_prefs, LY_ARRAY_COUNT(orig->val_prefs), ret, error);
Michal Vasko52927e22020-03-16 17:26:14 +01002572 LY_ARRAY_FOR(orig->val_prefs, u) {
Radek Krejci1798aae2020-07-14 13:26:06 +02002573 opaq->val_prefs[u].id = lydict_insert(LYD_NODE_CTX(node), orig->val_prefs[u].id, 0);
2574 opaq->val_prefs[u].module_ns = lydict_insert(LYD_NODE_CTX(node), orig->val_prefs[u].module_ns, 0);
Michal Vasko52927e22020-03-16 17:26:14 +01002575 LY_ARRAY_INCREMENT(opaq->val_prefs);
2576 }
2577 }
2578 opaq->value = lydict_insert(LYD_NODE_CTX(node), orig->value, 0);
2579 opaq->ctx = orig->ctx;
2580 } else if (dup->schema->nodetype & LYD_NODE_TERM) {
2581 struct lyd_node_term *term = (struct lyd_node_term *)dup;
2582 struct lyd_node_term *orig = (struct lyd_node_term *)node;
2583
2584 term->hash = orig->hash;
2585 term->value.realtype = orig->value.realtype;
2586 LY_CHECK_ERR_GOTO(term->value.realtype->plugin->duplicate(LYD_NODE_CTX(node), &orig->value, &term->value),
2587 LOGERR(LYD_NODE_CTX(node), LY_EINT, "Value duplication failed."); ret = LY_EINT, error);
2588 } else if (dup->schema->nodetype & LYD_NODE_INNER) {
2589 struct lyd_node_inner *orig = (struct lyd_node_inner *)node;
2590 struct lyd_node *child;
2591
2592 if (options & LYD_DUP_RECURSIVE) {
2593 /* duplicate all the children */
2594 LY_LIST_FOR(orig->child, child) {
Michal Vasko3a41dff2020-07-15 14:30:28 +02002595 LY_CHECK_GOTO(ret = lyd_dup_r(child, dup, NULL, options, NULL), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002596 }
Radek Krejci0fe9b512019-07-26 17:51:05 +02002597 } else if (dup->schema->nodetype == LYS_LIST && !(dup->schema->flags & LYS_KEYLESS)) {
Radek Krejci22ebdba2019-07-25 13:59:43 +02002598 /* always duplicate keys of a list */
Radek Krejci22ebdba2019-07-25 13:59:43 +02002599 child = orig->child;
Michal Vasko52927e22020-03-16 17:26:14 +01002600 for (struct lysc_node *key = ((struct lysc_node_list *)dup->schema)->child;
Michal Vaskoc57d9a92020-06-23 13:28:27 +02002601 key && (key->flags & LYS_KEY);
Radek Krejci0fe9b512019-07-26 17:51:05 +02002602 key = key->next) {
Radek Krejci22ebdba2019-07-25 13:59:43 +02002603 if (!child) {
2604 /* possibly not keys are present in filtered tree */
2605 break;
Radek Krejci0fe9b512019-07-26 17:51:05 +02002606 } else if (child->schema != key) {
2607 /* possibly not all keys are present in filtered tree,
2608 * but there can be also some non-key nodes */
2609 continue;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002610 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02002611 LY_CHECK_GOTO(ret = lyd_dup_r(child, dup, NULL, options, NULL), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002612 child = child->next;
2613 }
2614 }
2615 lyd_hash(dup);
2616 } else if (dup->schema->nodetype & LYD_NODE_ANY) {
Michal Vasko61551fa2020-07-09 15:45:45 +02002617 dup->hash = node->hash;
2618 any = (struct lyd_node_any *)node;
2619 LY_CHECK_GOTO(ret = lyd_any_copy_value(dup, &any->value, any->value_type), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002620 }
2621
Michal Vasko52927e22020-03-16 17:26:14 +01002622 /* insert */
2623 lyd_insert_node(parent, first, dup);
Michal Vasko52927e22020-03-16 17:26:14 +01002624
2625 if (dup_p) {
2626 *dup_p = dup;
2627 }
2628 return LY_SUCCESS;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002629
2630error:
Michal Vasko52927e22020-03-16 17:26:14 +01002631 lyd_free_tree(dup);
2632 return ret;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002633}
2634
Michal Vasko3a41dff2020-07-15 14:30:28 +02002635static LY_ERR
2636lyd_dup_get_local_parent(const struct lyd_node *node, const struct lyd_node_inner *parent, struct lyd_node **dup_parent,
2637 struct lyd_node_inner **local_parent)
Radek Krejci22ebdba2019-07-25 13:59:43 +02002638{
Michal Vasko3a41dff2020-07-15 14:30:28 +02002639 const struct lyd_node_inner *orig_parent, *iter;
2640 int repeat = 1;
2641
2642 *dup_parent = NULL;
2643 *local_parent = NULL;
2644
2645 for (orig_parent = node->parent; repeat && orig_parent; orig_parent = orig_parent->parent) {
2646 if (parent && (parent->schema == orig_parent->schema)) {
2647 /* stop creating parents, connect what we have into the provided parent */
2648 iter = parent;
2649 repeat = 0;
2650 } else {
2651 iter = NULL;
2652 LY_CHECK_RET(lyd_dup_r((struct lyd_node *)orig_parent, NULL, (struct lyd_node **)&iter, 0,
2653 (struct lyd_node **)&iter));
2654 }
2655 if (!*local_parent) {
2656 *local_parent = (struct lyd_node_inner *)iter;
2657 }
2658 if (iter->child) {
2659 /* 1) list - add after keys
2660 * 2) provided parent with some children */
2661 iter->child->prev->next = *dup_parent;
2662 if (*dup_parent) {
2663 (*dup_parent)->prev = iter->child->prev;
2664 iter->child->prev = *dup_parent;
2665 }
2666 } else {
2667 ((struct lyd_node_inner *)iter)->child = *dup_parent;
2668 }
2669 if (*dup_parent) {
2670 (*dup_parent)->parent = (struct lyd_node_inner *)iter;
2671 }
2672 *dup_parent = (struct lyd_node *)iter;
2673 }
2674
2675 if (repeat && parent) {
2676 /* given parent and created parents chain actually do not interconnect */
2677 LOGERR(LYD_NODE_CTX(node), LY_EINVAL,
2678 "Invalid argument parent (%s()) - does not interconnect with the created node's parents chain.", __func__);
2679 return LY_EINVAL;
2680 }
2681
2682 return LY_SUCCESS;
2683}
2684
2685static LY_ERR
2686lyd_dup(const struct lyd_node *node, struct lyd_node_inner *parent, int options, int nosiblings, struct lyd_node **dup)
2687{
2688 LY_ERR rc;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002689 const struct lyd_node *orig; /* original node to be duplicated */
2690 struct lyd_node *first = NULL; /* the first duplicated node, this is returned */
Radek Krejci22ebdba2019-07-25 13:59:43 +02002691 struct lyd_node *top = NULL; /* the most higher created node */
2692 struct lyd_node_inner *local_parent = NULL; /* the direct parent node for the duplicated node(s) */
Radek Krejci22ebdba2019-07-25 13:59:43 +02002693
Michal Vasko3a41dff2020-07-15 14:30:28 +02002694 LY_CHECK_ARG_RET(NULL, node, LY_EINVAL);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002695
2696 if (options & LYD_DUP_WITH_PARENTS) {
Michal Vasko3a41dff2020-07-15 14:30:28 +02002697 LY_CHECK_GOTO(rc = lyd_dup_get_local_parent(node, parent, &top, &local_parent), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002698 } else {
2699 local_parent = parent;
2700 }
2701
Radek Krejci22ebdba2019-07-25 13:59:43 +02002702 LY_LIST_FOR(node, orig) {
Michal Vasko52927e22020-03-16 17:26:14 +01002703 /* if there is no local parent, it will be inserted into first */
Michal Vasko3a41dff2020-07-15 14:30:28 +02002704 LY_CHECK_GOTO(rc = lyd_dup_r(orig, (struct lyd_node *)local_parent, &first, options, first ? NULL : &first), error);
2705 if (nosiblings) {
Radek Krejci22ebdba2019-07-25 13:59:43 +02002706 break;
2707 }
2708 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02002709
2710 /* rehash if needed */
2711 for (; local_parent; local_parent = local_parent->parent) {
2712 if (local_parent->schema->nodetype == LYS_LIST && (local_parent->schema->flags & LYS_KEYLESS)) {
2713 lyd_hash((struct lyd_node *)local_parent);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002714 }
2715 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02002716
2717 if (dup) {
2718 *dup = first;
2719 }
2720 return LY_SUCCESS;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002721
2722error:
2723 if (top) {
2724 lyd_free_tree(top);
2725 } else {
Michal Vaskof03ed032020-03-04 13:31:44 +01002726 lyd_free_siblings(first);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002727 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02002728 return rc;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002729}
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002730
Michal Vasko3a41dff2020-07-15 14:30:28 +02002731API LY_ERR
2732lyd_dup_single(const struct lyd_node *node, struct lyd_node_inner *parent, int options, struct lyd_node **dup)
2733{
2734 return lyd_dup(node, parent, options, 1, dup);
2735}
2736
2737API LY_ERR
2738lyd_dup_siblings(const struct lyd_node *node, struct lyd_node_inner *parent, int options, struct lyd_node **dup)
2739{
2740 return lyd_dup(node, parent, options, 0, dup);
2741}
2742
2743API LY_ERR
2744lyd_dup_meta_single(const struct lyd_meta *meta, struct lyd_node *node, struct lyd_meta **dup)
Michal Vasko25a32822020-07-09 15:48:22 +02002745{
2746 LY_ERR ret;
2747 struct lyd_meta *mt, *last;
2748
Michal Vasko3a41dff2020-07-15 14:30:28 +02002749 LY_CHECK_ARG_RET(NULL, meta, node, LY_EINVAL);
Michal Vasko25a32822020-07-09 15:48:22 +02002750
2751 /* create a copy */
2752 mt = calloc(1, sizeof *mt);
Michal Vasko3a41dff2020-07-15 14:30:28 +02002753 LY_CHECK_ERR_RET(!mt, LOGMEM(LYD_NODE_CTX(node)), LY_EMEM);
Michal Vasko25a32822020-07-09 15:48:22 +02002754 mt->parent = node;
2755 mt->annotation = meta->annotation;
2756 mt->value.realtype = meta->value.realtype;
2757 ret = mt->value.realtype->plugin->duplicate(LYD_NODE_CTX(node), &meta->value, &mt->value);
Michal Vasko3a41dff2020-07-15 14:30:28 +02002758 LY_CHECK_ERR_RET(ret, LOGERR(LYD_NODE_CTX(node), LY_EINT, "Value duplication failed."), ret);
Michal Vasko25a32822020-07-09 15:48:22 +02002759 mt->name = lydict_insert(LYD_NODE_CTX(node), meta->name, 0);
2760
2761 /* insert as the last attribute */
2762 if (node->meta) {
2763 for (last = node->meta; last->next; last = last->next);
2764 last->next = mt;
2765 } else {
2766 node->meta = mt;
2767 }
2768
Michal Vasko3a41dff2020-07-15 14:30:28 +02002769 if (dup) {
2770 *dup = mt;
2771 }
2772 return LY_SUCCESS;
Michal Vasko25a32822020-07-09 15:48:22 +02002773}
2774
Michal Vasko4490d312020-06-16 13:08:55 +02002775/**
2776 * @brief Merge a source sibling into target siblings.
2777 *
2778 * @param[in,out] first_trg First target sibling, is updated if top-level.
2779 * @param[in] parent_trg Target parent.
2780 * @param[in,out] sibling_src Source sibling to merge, set to NULL if spent.
2781 * @param[in] options Merge options.
2782 * @return LY_ERR value.
2783 */
2784static LY_ERR
2785lyd_merge_sibling_r(struct lyd_node **first_trg, struct lyd_node *parent_trg, const struct lyd_node **sibling_src_p,
2786 int options)
2787{
2788 LY_ERR ret;
2789 const struct lyd_node *child_src, *tmp, *sibling_src;
Michal Vasko56daf732020-08-10 10:57:18 +02002790 struct lyd_node *match_trg, *dup_src, *elem;
Michal Vasko4490d312020-06-16 13:08:55 +02002791 struct lysc_type *type;
Michal Vasko4490d312020-06-16 13:08:55 +02002792
2793 sibling_src = *sibling_src_p;
2794 if (sibling_src->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) {
2795 /* try to find the exact instance */
2796 ret = lyd_find_sibling_first(*first_trg, sibling_src, &match_trg);
2797 } else {
2798 /* try to simply find the node, there cannot be more instances */
2799 ret = lyd_find_sibling_val(*first_trg, sibling_src->schema, NULL, 0, &match_trg);
2800 }
2801
2802 if (!ret) {
2803 /* node found, make sure even value matches for all node types */
Michal Vasko8f359bf2020-07-28 10:41:15 +02002804 if ((match_trg->schema->nodetype == LYS_LEAF) && lyd_compare_single(sibling_src, match_trg, LYD_COMPARE_DEFAULTS)) {
Michal Vasko4490d312020-06-16 13:08:55 +02002805 /* since they are different, they cannot both be default */
2806 assert(!(sibling_src->flags & LYD_DEFAULT) || !(match_trg->flags & LYD_DEFAULT));
2807
Michal Vasko3a41dff2020-07-15 14:30:28 +02002808 /* update value (or only LYD_DEFAULT flag) only if flag set or the source node is not default */
2809 if ((options & LYD_MERGE_DEFAULTS) || !(sibling_src->flags & LYD_DEFAULT)) {
Michal Vasko4490d312020-06-16 13:08:55 +02002810 type = ((struct lysc_node_leaf *)match_trg->schema)->type;
2811 type->plugin->free(LYD_NODE_CTX(match_trg), &((struct lyd_node_term *)match_trg)->value);
2812 LY_CHECK_RET(type->plugin->duplicate(LYD_NODE_CTX(match_trg), &((struct lyd_node_term *)sibling_src)->value,
2813 &((struct lyd_node_term *)match_trg)->value));
2814
2815 /* copy flags and add LYD_NEW */
2816 match_trg->flags = sibling_src->flags | LYD_NEW;
2817 }
Michal Vasko8f359bf2020-07-28 10:41:15 +02002818 } else if ((match_trg->schema->nodetype & LYS_ANYDATA) && lyd_compare_single(sibling_src, match_trg, 0)) {
Michal Vasko4c583e82020-07-17 12:16:14 +02002819 /* update value */
2820 LY_CHECK_RET(lyd_any_copy_value(match_trg, &((struct lyd_node_any *)sibling_src)->value,
2821 ((struct lyd_node_any *)sibling_src)->value_type));
Michal Vasko4490d312020-06-16 13:08:55 +02002822
2823 /* copy flags and add LYD_NEW */
2824 match_trg->flags = sibling_src->flags | LYD_NEW;
Michal Vasko4490d312020-06-16 13:08:55 +02002825 } else {
2826 /* check descendants, recursively */
Michal Vasko8ee464a2020-08-11 14:12:50 +02002827 LY_LIST_FOR_SAFE(LYD_CHILD_NO_KEYS(sibling_src), tmp, child_src) {
Michal Vasko4490d312020-06-16 13:08:55 +02002828 LY_CHECK_RET(lyd_merge_sibling_r(lyd_node_children_p(match_trg), match_trg, &child_src, options));
2829 }
2830 }
2831 } else {
2832 /* node not found, merge it */
2833 if (options & LYD_MERGE_DESTRUCT) {
2834 dup_src = (struct lyd_node *)sibling_src;
2835 lyd_unlink_tree(dup_src);
2836 /* spend it */
2837 *sibling_src_p = NULL;
2838 } else {
Michal Vasko3a41dff2020-07-15 14:30:28 +02002839 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 +02002840 }
2841
2842 /* set LYD_NEW for all the new nodes, required for validation */
Michal Vasko56daf732020-08-10 10:57:18 +02002843 LYD_TREE_DFS_BEGIN(dup_src, elem) {
Michal Vasko4490d312020-06-16 13:08:55 +02002844 elem->flags |= LYD_NEW;
Michal Vasko56daf732020-08-10 10:57:18 +02002845 LYD_TREE_DFS_END(dup_src, elem);
Michal Vasko4490d312020-06-16 13:08:55 +02002846 }
2847
2848 lyd_insert_node(parent_trg, first_trg, dup_src);
2849 }
2850
2851 return LY_SUCCESS;
2852}
2853
Michal Vasko3a41dff2020-07-15 14:30:28 +02002854static LY_ERR
2855lyd_merge(struct lyd_node **target, const struct lyd_node *source, int options, int nosiblings)
Michal Vasko4490d312020-06-16 13:08:55 +02002856{
2857 const struct lyd_node *sibling_src, *tmp;
2858 int first;
2859
2860 LY_CHECK_ARG_RET(NULL, target, LY_EINVAL);
2861
2862 if (!source) {
2863 /* nothing to merge */
2864 return LY_SUCCESS;
2865 }
2866
2867 if (lysc_data_parent((*target)->schema) || lysc_data_parent(source->schema)) {
2868 LOGERR(LYD_NODE_CTX(source), LY_EINVAL, "Invalid arguments - can merge only 2 top-level subtrees (%s()).", __func__);
2869 return LY_EINVAL;
2870 }
2871
2872 LY_LIST_FOR_SAFE(source, tmp, sibling_src) {
2873 first = sibling_src == source ? 1 : 0;
2874 LY_CHECK_RET(lyd_merge_sibling_r(target, NULL, &sibling_src, options));
2875 if (first && !sibling_src) {
2876 /* source was spent (unlinked), move to the next node */
2877 source = tmp;
2878 }
2879
Michal Vasko3a41dff2020-07-15 14:30:28 +02002880 if (nosiblings) {
Michal Vasko4490d312020-06-16 13:08:55 +02002881 break;
2882 }
2883 }
2884
2885 if (options & LYD_MERGE_DESTRUCT) {
2886 /* free any leftover source data that were not merged */
2887 lyd_free_siblings((struct lyd_node *)source);
2888 }
2889
2890 return LY_SUCCESS;
2891}
2892
Michal Vasko3a41dff2020-07-15 14:30:28 +02002893API LY_ERR
2894lyd_merge_tree(struct lyd_node **target, const struct lyd_node *source, int options)
2895{
2896 return lyd_merge(target, source, options, 1);
2897}
2898
2899API LY_ERR
2900lyd_merge_siblings(struct lyd_node **target, const struct lyd_node *source, int options)
2901{
2902 return lyd_merge(target, source, options, 0);
2903}
2904
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002905static LY_ERR
2906lyd_path_str_enlarge(char **buffer, size_t *buflen, size_t reqlen, int is_static)
2907{
Michal Vasko14654712020-02-06 08:35:21 +01002908 /* ending \0 */
2909 ++reqlen;
2910
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002911 if (reqlen > *buflen) {
2912 if (is_static) {
2913 return LY_EINCOMPLETE;
2914 }
2915
2916 *buffer = ly_realloc(*buffer, reqlen * sizeof **buffer);
2917 if (!*buffer) {
2918 return LY_EMEM;
2919 }
2920
2921 *buflen = reqlen;
2922 }
2923
2924 return LY_SUCCESS;
2925}
2926
Michal Vaskod59035b2020-07-08 12:00:06 +02002927LY_ERR
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002928lyd_path_list_predicate(const struct lyd_node *node, char **buffer, size_t *buflen, size_t *bufused, int is_static)
2929{
2930 const struct lyd_node *key;
2931 int dynamic = 0;
2932 size_t len;
2933 const char *val;
2934 char quot;
2935 LY_ERR rc;
2936
Michal Vasko5bfd4be2020-06-23 13:26:19 +02002937 for (key = lyd_node_children(node, 0); key && (key->schema->flags & LYS_KEY); key = key->next) {
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002938 val = lyd_value2str((struct lyd_node_term *)key, &dynamic);
2939 len = 1 + strlen(key->schema->name) + 2 + strlen(val) + 2;
2940 rc = lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static);
2941 if (rc != LY_SUCCESS) {
2942 if (dynamic) {
2943 free((char *)val);
2944 }
2945 return rc;
2946 }
2947
2948 quot = '\'';
2949 if (strchr(val, '\'')) {
2950 quot = '"';
2951 }
2952 *bufused += sprintf(*buffer + *bufused, "[%s=%c%s%c]", key->schema->name, quot, val, quot);
2953
2954 if (dynamic) {
2955 free((char *)val);
2956 }
2957 }
2958
2959 return LY_SUCCESS;
2960}
2961
2962/**
2963 * @brief Append leaf-list value predicate to path.
2964 *
2965 * @param[in] node Node to print.
2966 * @param[in,out] buffer Buffer to print to.
2967 * @param[in,out] buflen Current buffer length.
2968 * @param[in,out] bufused Current number of characters used in @p buffer.
2969 * @param[in] is_static Whether buffer is static or can be reallocated.
2970 * @return LY_ERR
2971 */
2972static LY_ERR
2973lyd_path_leaflist_predicate(const struct lyd_node *node, char **buffer, size_t *buflen, size_t *bufused, int is_static)
2974{
2975 int dynamic = 0;
2976 size_t len;
2977 const char *val;
2978 char quot;
2979 LY_ERR rc;
2980
2981 val = lyd_value2str((struct lyd_node_term *)node, &dynamic);
2982 len = 4 + strlen(val) + 2;
2983 rc = lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static);
2984 if (rc != LY_SUCCESS) {
2985 goto cleanup;
2986 }
2987
2988 quot = '\'';
2989 if (strchr(val, '\'')) {
2990 quot = '"';
2991 }
2992 *bufused += sprintf(*buffer + *bufused, "[.=%c%s%c]", quot, val, quot);
2993
2994cleanup:
2995 if (dynamic) {
2996 free((char *)val);
2997 }
2998 return rc;
2999}
3000
3001/**
3002 * @brief Append node position (relative to its other instances) predicate to path.
3003 *
3004 * @param[in] node Node to print.
3005 * @param[in,out] buffer Buffer to print to.
3006 * @param[in,out] buflen Current buffer length.
3007 * @param[in,out] bufused Current number of characters used in @p buffer.
3008 * @param[in] is_static Whether buffer is static or can be reallocated.
3009 * @return LY_ERR
3010 */
3011static LY_ERR
3012lyd_path_position_predicate(const struct lyd_node *node, char **buffer, size_t *buflen, size_t *bufused, int is_static)
3013{
3014 const struct lyd_node *first, *iter;
3015 size_t len;
3016 int pos;
3017 char *val = NULL;
3018 LY_ERR rc;
3019
3020 if (node->parent) {
3021 first = node->parent->child;
3022 } else {
3023 for (first = node; node->prev->next; node = node->prev);
3024 }
3025 pos = 1;
3026 for (iter = first; iter != node; iter = iter->next) {
3027 if (iter->schema == node->schema) {
3028 ++pos;
3029 }
3030 }
3031 if (asprintf(&val, "%d", pos) == -1) {
3032 return LY_EMEM;
3033 }
3034
3035 len = 1 + strlen(val) + 1;
3036 rc = lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static);
3037 if (rc != LY_SUCCESS) {
3038 goto cleanup;
3039 }
3040
3041 *bufused += sprintf(*buffer + *bufused, "[%s]", val);
3042
3043cleanup:
3044 free(val);
3045 return rc;
3046}
3047
3048API char *
3049lyd_path(const struct lyd_node *node, LYD_PATH_TYPE pathtype, char *buffer, size_t buflen)
3050{
Michal Vasko14654712020-02-06 08:35:21 +01003051 int is_static = 0, i, depth;
3052 size_t bufused = 0, len;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003053 const struct lyd_node *iter;
3054 const struct lys_module *mod;
Michal Vasko790b2bc2020-08-03 13:35:06 +02003055 LY_ERR rc = LY_SUCCESS;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003056
3057 LY_CHECK_ARG_RET(NULL, node, NULL);
3058 if (buffer) {
3059 LY_CHECK_ARG_RET(node->schema->module->ctx, buflen > 1, NULL);
3060 is_static = 1;
Michal Vasko14654712020-02-06 08:35:21 +01003061 } else {
3062 buflen = 0;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003063 }
3064
3065 switch (pathtype) {
Michal Vasko14654712020-02-06 08:35:21 +01003066 case LYD_PATH_LOG:
Michal Vasko790b2bc2020-08-03 13:35:06 +02003067 case LYD_PATH_LOG_NO_LAST_PRED:
Michal Vasko14654712020-02-06 08:35:21 +01003068 depth = 1;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003069 for (iter = node; iter->parent; iter = (const struct lyd_node *)iter->parent) {
3070 ++depth;
3071 }
3072
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003073 goto iter_print;
Michal Vasko14654712020-02-06 08:35:21 +01003074 while (depth) {
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003075 /* find the right node */
Michal Vasko14654712020-02-06 08:35:21 +01003076 for (iter = node, i = 1; i < depth; iter = (const struct lyd_node *)iter->parent, ++i);
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003077iter_print:
3078 /* print prefix and name */
3079 mod = NULL;
Radek Krejci1798aae2020-07-14 13:26:06 +02003080 if (iter->schema && (!iter->parent || iter->schema->module != iter->parent->schema->module)) {
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003081 mod = iter->schema->module;
3082 }
3083
3084 /* realloc string */
Radek Krejci1798aae2020-07-14 13:26:06 +02003085 len = 1 + (mod ? strlen(mod->name) + 1 : 0) + (iter->schema ? strlen(iter->schema->name) : strlen(((struct lyd_node_opaq*)iter)->name));
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003086 rc = lyd_path_str_enlarge(&buffer, &buflen, bufused + len, is_static);
3087 if (rc != LY_SUCCESS) {
3088 break;
3089 }
3090
3091 /* print next node */
Radek Krejci1798aae2020-07-14 13:26:06 +02003092 bufused += sprintf(buffer + bufused, "/%s%s%s", mod ? mod->name : "", mod ? ":" : "",
3093 iter->schema ? iter->schema->name : ((struct lyd_node_opaq*)iter)->name);
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003094
Michal Vasko790b2bc2020-08-03 13:35:06 +02003095 /* do not always print the last (first) predicate */
Radek Krejci1798aae2020-07-14 13:26:06 +02003096 if (iter->schema && (bufused || (pathtype == LYD_PATH_LOG))) {
Michal Vasko790b2bc2020-08-03 13:35:06 +02003097 switch (iter->schema->nodetype) {
3098 case LYS_LIST:
3099 if (iter->schema->flags & LYS_KEYLESS) {
3100 /* print its position */
3101 rc = lyd_path_position_predicate(iter, &buffer, &buflen, &bufused, is_static);
3102 } else {
3103 /* print all list keys in predicates */
3104 rc = lyd_path_list_predicate(iter, &buffer, &buflen, &bufused, is_static);
3105 }
3106 break;
3107 case LYS_LEAFLIST:
3108 if (iter->schema->flags & LYS_CONFIG_W) {
3109 /* print leaf-list value */
3110 rc = lyd_path_leaflist_predicate(iter, &buffer, &buflen, &bufused, is_static);
3111 } else {
3112 /* print its position */
3113 rc = lyd_path_position_predicate(iter, &buffer, &buflen, &bufused, is_static);
3114 }
3115 break;
3116 default:
3117 /* nothing to print more */
3118 break;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003119 }
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003120 }
3121 if (rc != LY_SUCCESS) {
3122 break;
3123 }
3124
Michal Vasko14654712020-02-06 08:35:21 +01003125 --depth;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003126 }
3127 break;
3128 }
3129
3130 return buffer;
3131}
Michal Vaskoe444f752020-02-10 12:20:06 +01003132
Michal Vasko25a32822020-07-09 15:48:22 +02003133API struct lyd_meta *
3134lyd_find_meta(const struct lyd_meta *first, const struct lys_module *module, const char *name)
3135{
3136 struct lyd_meta *ret = NULL;
3137 const struct ly_ctx *ctx;
3138 const char *prefix, *tmp;
3139 char *str;
3140 size_t pref_len, name_len;
3141
3142 LY_CHECK_ARG_RET(NULL, module || strchr(name, ':'), name, NULL);
3143
3144 if (!first) {
3145 return NULL;
3146 }
3147
3148 ctx = first->annotation->module->ctx;
3149
3150 /* parse the name */
3151 tmp = name;
3152 if (ly_parse_nodeid(&tmp, &prefix, &pref_len, &name, &name_len) || tmp[0]) {
3153 LOGERR(ctx, LY_EINVAL, "Metadata name \"%s\" is not valid.", name);
3154 return NULL;
3155 }
3156
3157 /* find the module */
3158 if (prefix) {
3159 str = strndup(prefix, pref_len);
3160 module = ly_ctx_get_module_latest(ctx, str);
3161 free(str);
3162 LY_CHECK_ERR_RET(!module, LOGERR(ctx, LY_EINVAL, "Module \"%.*s\" not found.", pref_len, prefix), NULL);
3163 }
3164
3165 /* find the metadata */
3166 LY_LIST_FOR(first, first) {
3167 if ((first->annotation->module == module) && !strcmp(first->name, name)) {
3168 ret = (struct lyd_meta *)first;
3169 break;
3170 }
3171 }
3172
3173 return ret;
3174}
3175
Michal Vasko9b368d32020-02-14 13:53:31 +01003176API LY_ERR
Michal Vaskoe444f752020-02-10 12:20:06 +01003177lyd_find_sibling_first(const struct lyd_node *siblings, const struct lyd_node *target, struct lyd_node **match)
3178{
3179 struct lyd_node **match_p;
3180 struct lyd_node_inner *parent;
3181
Michal Vaskof03ed032020-03-04 13:31:44 +01003182 LY_CHECK_ARG_RET(NULL, target, LY_EINVAL);
Michal Vaskoe444f752020-02-10 12:20:06 +01003183
Michal Vasko62ed12d2020-05-21 10:08:25 +02003184 if (!siblings || (lysc_data_parent(siblings->schema) != lysc_data_parent(target->schema))) {
3185 /* no data or schema mismatch */
Michal Vasko9b368d32020-02-14 13:53:31 +01003186 if (match) {
3187 *match = NULL;
3188 }
Michal Vaskoe444f752020-02-10 12:20:06 +01003189 return LY_ENOTFOUND;
3190 }
3191
3192 /* find first sibling */
3193 if (siblings->parent) {
3194 siblings = siblings->parent->child;
3195 } else {
3196 while (siblings->prev->next) {
3197 siblings = siblings->prev;
3198 }
3199 }
3200
3201 parent = (struct lyd_node_inner *)siblings->parent;
3202 if (parent && parent->children_ht) {
3203 assert(target->hash);
3204
3205 /* find by hash */
3206 if (!lyht_find(parent->children_ht, &target, target->hash, (void **)&match_p)) {
Michal Vaskoda859032020-07-14 12:20:14 +02003207 /* check even value when needed */
Michal Vasko8f359bf2020-07-28 10:41:15 +02003208 if (!(target->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) || !lyd_compare_single(target, *match_p, 0)) {
Michal Vaskoda859032020-07-14 12:20:14 +02003209 siblings = *match_p;
3210 } else {
3211 siblings = NULL;
3212 }
Michal Vaskoe444f752020-02-10 12:20:06 +01003213 } else {
3214 /* not found */
3215 siblings = NULL;
3216 }
3217 } else {
3218 /* no children hash table */
3219 for (; siblings; siblings = siblings->next) {
Michal Vasko8f359bf2020-07-28 10:41:15 +02003220 if (!lyd_compare_single(siblings, target, 0)) {
Michal Vaskoe444f752020-02-10 12:20:06 +01003221 break;
3222 }
3223 }
3224 }
3225
3226 if (!siblings) {
Michal Vasko9b368d32020-02-14 13:53:31 +01003227 if (match) {
3228 *match = NULL;
3229 }
Michal Vaskoe444f752020-02-10 12:20:06 +01003230 return LY_ENOTFOUND;
3231 }
3232
Michal Vasko9b368d32020-02-14 13:53:31 +01003233 if (match) {
3234 *match = (struct lyd_node *)siblings;
3235 }
Michal Vaskoe444f752020-02-10 12:20:06 +01003236 return LY_SUCCESS;
3237}
3238
Michal Vasko90932a92020-02-12 14:33:03 +01003239static int
3240lyd_hash_table_schema_val_equal(void *val1_p, void *val2_p, int UNUSED(mod), void *UNUSED(cb_data))
3241{
3242 struct lysc_node *val1;
3243 struct lyd_node *val2;
3244
3245 val1 = *((struct lysc_node **)val1_p);
3246 val2 = *((struct lyd_node **)val2_p);
3247
Michal Vasko90932a92020-02-12 14:33:03 +01003248 if (val1 == val2->schema) {
3249 /* schema match is enough */
3250 return 1;
3251 } else {
3252 return 0;
3253 }
3254}
3255
3256static LY_ERR
3257lyd_find_sibling_schema(const struct lyd_node *siblings, const struct lysc_node *schema, struct lyd_node **match)
3258{
3259 struct lyd_node **match_p;
3260 struct lyd_node_inner *parent;
3261 uint32_t hash;
3262 values_equal_cb ht_cb;
3263
Michal Vaskob104f112020-07-17 09:54:54 +02003264 assert(siblings && schema);
Michal Vasko90932a92020-02-12 14:33:03 +01003265
3266 parent = (struct lyd_node_inner *)siblings->parent;
3267 if (parent && parent->children_ht) {
3268 /* calculate our hash */
3269 hash = dict_hash_multi(0, schema->module->name, strlen(schema->module->name));
3270 hash = dict_hash_multi(hash, schema->name, strlen(schema->name));
3271 hash = dict_hash_multi(hash, NULL, 0);
3272
3273 /* use special hash table function */
3274 ht_cb = lyht_set_cb(parent->children_ht, lyd_hash_table_schema_val_equal);
3275
3276 /* find by hash */
3277 if (!lyht_find(parent->children_ht, &schema, hash, (void **)&match_p)) {
3278 siblings = *match_p;
3279 } else {
3280 /* not found */
3281 siblings = NULL;
3282 }
3283
3284 /* set the original hash table compare function back */
3285 lyht_set_cb(parent->children_ht, ht_cb);
3286 } else {
Michal Vaskob104f112020-07-17 09:54:54 +02003287 /* find first sibling */
3288 if (siblings->parent) {
3289 siblings = siblings->parent->child;
3290 } else {
3291 while (siblings->prev->next) {
3292 siblings = siblings->prev;
3293 }
3294 }
3295
3296 /* search manually without hashes */
Michal Vasko90932a92020-02-12 14:33:03 +01003297 for (; siblings; siblings = siblings->next) {
3298 if (siblings->schema == schema) {
3299 /* schema match is enough */
3300 break;
3301 }
3302 }
3303 }
3304
3305 if (!siblings) {
Michal Vasko9b368d32020-02-14 13:53:31 +01003306 if (match) {
3307 *match = NULL;
3308 }
Michal Vasko90932a92020-02-12 14:33:03 +01003309 return LY_ENOTFOUND;
3310 }
3311
Michal Vasko9b368d32020-02-14 13:53:31 +01003312 if (match) {
3313 *match = (struct lyd_node *)siblings;
3314 }
Michal Vasko90932a92020-02-12 14:33:03 +01003315 return LY_SUCCESS;
3316}
3317
Michal Vaskoe444f752020-02-10 12:20:06 +01003318API LY_ERR
3319lyd_find_sibling_val(const struct lyd_node *siblings, const struct lysc_node *schema, const char *key_or_value,
3320 size_t val_len, struct lyd_node **match)
3321{
3322 LY_ERR rc;
3323 struct lyd_node *target = NULL;
3324
Michal Vasko4c583e82020-07-17 12:16:14 +02003325 LY_CHECK_ARG_RET(NULL, schema, !(schema->nodetype & (LYS_CHOICE | LYS_CASE)), LY_EINVAL);
Michal Vaskoe444f752020-02-10 12:20:06 +01003326
Michal Vasko62ed12d2020-05-21 10:08:25 +02003327 if (!siblings || (lysc_data_parent(siblings->schema) != lysc_data_parent(schema))) {
3328 /* no data or schema mismatch */
Michal Vasko9b368d32020-02-14 13:53:31 +01003329 if (match) {
3330 *match = NULL;
3331 }
Michal Vaskoe444f752020-02-10 12:20:06 +01003332 return LY_ENOTFOUND;
3333 }
3334
Michal Vaskof03ed032020-03-04 13:31:44 +01003335 if (key_or_value && !val_len) {
3336 val_len = strlen(key_or_value);
3337 }
3338
Michal Vaskob104f112020-07-17 09:54:54 +02003339 if ((schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) && key_or_value) {
3340 /* create a data node and find the instance */
3341 if (schema->nodetype == LYS_LEAFLIST) {
3342 /* target used attributes: schema, hash, value */
Michal Vaskoc8a230d2020-08-14 12:17:10 +02003343 rc = lyd_create_term(schema, key_or_value, val_len, NULL, 0, LY_PREF_JSON, NULL, &target);
Michal Vaskob104f112020-07-17 09:54:54 +02003344 LY_CHECK_RET(rc && (rc != LY_EINCOMPLETE), rc);
3345 } else {
Michal Vasko90932a92020-02-12 14:33:03 +01003346 /* target used attributes: schema, hash, child (all keys) */
Michal Vasko004d3152020-06-11 19:59:22 +02003347 LY_CHECK_RET(lyd_create_list2(schema, key_or_value, val_len, &target));
Michal Vasko90932a92020-02-12 14:33:03 +01003348 }
3349
3350 /* find it */
3351 rc = lyd_find_sibling_first(siblings, target, match);
Michal Vaskob104f112020-07-17 09:54:54 +02003352 } else {
3353 /* find the first schema node instance */
3354 rc = lyd_find_sibling_schema(siblings, schema, match);
Michal Vaskoe444f752020-02-10 12:20:06 +01003355 }
3356
Michal Vaskoe444f752020-02-10 12:20:06 +01003357 lyd_free_tree(target);
3358 return rc;
3359}
Michal Vaskoccc02342020-05-21 10:09:21 +02003360
3361API LY_ERR
3362lyd_find_xpath(const struct lyd_node *ctx_node, const char *xpath, struct ly_set **set)
3363{
3364 LY_ERR ret = LY_SUCCESS;
3365 struct lyxp_set xp_set;
3366 struct lyxp_expr *exp;
3367 uint32_t i;
3368
3369 LY_CHECK_ARG_RET(NULL, ctx_node, xpath, set, LY_EINVAL);
3370
3371 memset(&xp_set, 0, sizeof xp_set);
3372
3373 /* compile expression */
Michal Vasko004d3152020-06-11 19:59:22 +02003374 exp = lyxp_expr_parse((struct ly_ctx *)LYD_NODE_CTX(ctx_node), xpath, 0, 1);
Michal Vaskoccc02342020-05-21 10:09:21 +02003375 LY_CHECK_ERR_GOTO(!exp, ret = LY_EINVAL, cleanup);
3376
3377 /* evaluate expression */
Michal Vaskoc8a230d2020-08-14 12:17:10 +02003378 ret = lyxp_eval(exp, LY_PREF_JSON, ctx_node->schema->module, ctx_node, LYXP_NODE_ELEM, ctx_node, &xp_set, 0);
Michal Vaskoccc02342020-05-21 10:09:21 +02003379 LY_CHECK_GOTO(ret, cleanup);
3380
3381 /* allocate return set */
3382 *set = ly_set_new();
3383 LY_CHECK_ERR_GOTO(!*set, LOGMEM(LYD_NODE_CTX(ctx_node)); ret = LY_EMEM, cleanup);
3384
3385 /* transform into ly_set */
3386 if (xp_set.type == LYXP_SET_NODE_SET) {
3387 /* allocate memory for all the elements once (even though not all items must be elements but most likely will be) */
3388 (*set)->objs = malloc(xp_set.used * sizeof *(*set)->objs);
3389 LY_CHECK_ERR_GOTO(!(*set)->objs, LOGMEM(LYD_NODE_CTX(ctx_node)); ret = LY_EMEM, cleanup);
3390 (*set)->size = xp_set.used;
3391
3392 for (i = 0; i < xp_set.used; ++i) {
3393 if (xp_set.val.nodes[i].type == LYXP_NODE_ELEM) {
3394 ly_set_add(*set, xp_set.val.nodes[i].node, LY_SET_OPT_USEASLIST);
3395 }
3396 }
3397 }
3398
3399cleanup:
Michal Vasko0691d522020-05-21 13:21:47 +02003400 lyxp_set_free_content(&xp_set);
Michal Vaskoccc02342020-05-21 10:09:21 +02003401 lyxp_expr_free((struct ly_ctx *)LYD_NODE_CTX(ctx_node), exp);
3402 return ret;
3403}