blob: 922d96642fd4042b071e0e38684942f69085b499 [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,
59 ly_resolve_prefix_clb get_prefix, void *parser, LYD_FORMAT format, 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 Vasko90932a92020-02-12 14:33:03 +010075 ret = type->plugin->store(ctx, type, value, value_len, options, get_prefix, parser, format,
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,
Radek Krejci1798aae2020-07-14 13:26:06 +020099 ly_resolve_prefix_clb get_prefix, void *parser, LYD_FORMAT format)
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;
112 ret = type->plugin->store(ctx, type, value, value_len, options, get_prefix, parser, format, (void *)schema, NULL,
113 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,
Radek Krejci1798aae2020-07-14 13:26:06 +0200131 int second, int value_hint, ly_resolve_prefix_clb get_prefix, void *parser, LYD_FORMAT format,
Michal Vaskof03ed032020-03-04 13:31:44 +0100132 const struct lysc_node *ctx_snode, 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 Vasko90932a92020-02-12 14:33:03 +0100147 ret = ant->type->plugin->store(ctx, ant->type, value, value_len, options, get_prefix, parser, format,
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,
Radek Krejci1798aae2020-07-14 13:26:06 +0200166 ly_resolve_prefix_clb resolve_prefix, void *prefix_data, LYD_FORMAT format)
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 Vaskof937cfe2020-08-03 16:07:12 +0200182 resolve_prefix, prefix_data, format, 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{
202 return _lys_value_validate(ctx, node, value, value_len, lydjson_resolve_prefix, NULL, LYD_JSON);
203}
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;
Radek Krejci73dead22019-07-11 16:46:16 +0200218 rc = type->plugin->store(ctx ? ctx : node->schema->module->ctx, type, value, value_len, options,
Michal Vaskof937cfe2020-08-03 16:07:12 +0200219 lydjson_resolve_prefix, NULL, LYD_JSON, tree ? (void*)node : (void*)node->schema, tree,
Michal Vasko3701af52020-08-03 14:29:38 +0200220 &val, NULL, &err);
Radek Krejci73dead22019-07-11 16:46:16 +0200221 if (rc == LY_EINCOMPLETE) {
222 return rc;
223 } else if (rc) {
224 if (err) {
225 if (ctx) {
226 ly_err_print(err);
227 LOGVAL(ctx, LY_VLOG_STR, err->path, err->vecode, err->msg);
Radek Krejci084289f2019-07-09 17:35:30 +0200228 }
Radek Krejci73dead22019-07-11 16:46:16 +0200229 ly_err_free(err);
Radek Krejci084289f2019-07-09 17:35:30 +0200230 }
Radek Krejci73dead22019-07-11 16:46:16 +0200231 return rc;
Radek Krejci084289f2019-07-09 17:35:30 +0200232 }
233
Michal Vasko3701af52020-08-03 14:29:38 +0200234 if (realtype) {
235 *realtype = val.realtype;
236 }
237
238 type->plugin->free(ctx ? ctx : node->schema->module->ctx, &val);
Radek Krejci084289f2019-07-09 17:35:30 +0200239 return LY_SUCCESS;
240}
241
242API LY_ERR
Michal Vaskof937cfe2020-08-03 16:07:12 +0200243lyd_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 +0200244{
245 LY_ERR ret = LY_SUCCESS, rc;
246 struct ly_err_item *err = NULL;
247 struct ly_ctx *ctx;
248 struct lysc_type *type;
Radek Krejci084289f2019-07-09 17:35:30 +0200249 struct lyd_value data = {0};
Michal Vaskof03ed032020-03-04 13:31:44 +0100250 int options = LY_TYPE_OPTS_STORE | (tree ? 0 : LY_TYPE_OPTS_INCOMPLETE_DATA);
Radek Krejci084289f2019-07-09 17:35:30 +0200251
252 LY_CHECK_ARG_RET(node ? node->schema->module->ctx : NULL, node, value, LY_EINVAL);
253
254 ctx = node->schema->module->ctx;
255 type = ((struct lysc_node_leaf*)node->schema)->type;
Michal Vaskof937cfe2020-08-03 16:07:12 +0200256 rc = type->plugin->store(ctx, type, value, value_len, options, lydjson_resolve_prefix, NULL, LYD_JSON,
257 (struct lyd_node *)node, tree, &data, NULL, &err);
Radek Krejci73dead22019-07-11 16:46:16 +0200258 if (rc == LY_EINCOMPLETE) {
259 ret = rc;
260 /* continue with comparing, just remember what to return if storing is ok */
261 } else if (rc) {
262 /* value to compare is invalid */
263 ret = LY_EINVAL;
264 if (err) {
265 ly_err_free(err);
Radek Krejci084289f2019-07-09 17:35:30 +0200266 }
Radek Krejci73dead22019-07-11 16:46:16 +0200267 goto cleanup;
Radek Krejci084289f2019-07-09 17:35:30 +0200268 }
269
270 /* compare data */
Radek Krejci5af04842019-07-12 11:32:07 +0200271 if (type->plugin->compare(&node->value, &data)) {
272 /* do not assign it directly from the compare callback to keep possible LY_EINCOMPLETE from validation */
Michal Vaskob3ddccb2020-07-09 15:43:05 +0200273 ret = LY_ENOT;
Radek Krejci5af04842019-07-12 11:32:07 +0200274 }
Radek Krejci084289f2019-07-09 17:35:30 +0200275
276cleanup:
Radek Krejci62903c32019-07-15 14:42:05 +0200277 type->plugin->free(ctx, &data);
Radek Krejci084289f2019-07-09 17:35:30 +0200278
279 return ret;
280}
281
Michal Vasko5ec7cda2019-09-11 13:43:08 +0200282API const char *
283lyd_value2str(const struct lyd_node_term *node, int *dynamic)
284{
285 LY_CHECK_ARG_RET(node ? node->schema->module->ctx : NULL, node, dynamic, NULL);
286
287 return node->value.realtype->plugin->print(&node->value, LYD_JSON, json_print_get_prefix, NULL, dynamic);
288}
289
290API const char *
Michal Vasko9f96a052020-03-10 09:41:45 +0100291lyd_meta2str(const struct lyd_meta *meta, int *dynamic)
Michal Vasko5ec7cda2019-09-11 13:43:08 +0200292{
Michal Vasko9f96a052020-03-10 09:41:45 +0100293 LY_CHECK_ARG_RET(meta ? meta->parent->schema->module->ctx : NULL, meta, dynamic, NULL);
Michal Vasko5ec7cda2019-09-11 13:43:08 +0200294
Michal Vasko9f96a052020-03-10 09:41:45 +0100295 return meta->value.realtype->plugin->print(&meta->value, LYD_JSON, json_print_get_prefix, NULL, dynamic);
Michal Vasko5ec7cda2019-09-11 13:43:08 +0200296}
297
Radek Krejci7931b192020-06-25 17:05:03 +0200298static LYD_FORMAT
Michal Vasko63f3d842020-07-08 10:10:14 +0200299lyd_parse_get_format(const struct ly_in *in, LYD_FORMAT format)
Radek Krejcie7b95092019-05-15 11:03:07 +0200300{
Radek Krejcie7b95092019-05-15 11:03:07 +0200301
Radek Krejci7931b192020-06-25 17:05:03 +0200302 if (!format && in->type == LY_IN_FILEPATH) {
Radek Krejcie7b95092019-05-15 11:03:07 +0200303 /* unknown format - try to detect it from filename's suffix */
Radek Krejci7931b192020-06-25 17:05:03 +0200304 const char *path = in->method.fpath.filepath;
305 size_t len = strlen(path);
Radek Krejcie7b95092019-05-15 11:03:07 +0200306
307 /* ignore trailing whitespaces */
308 for (; len > 0 && isspace(path[len - 1]); len--);
309
310 if (len >= 5 && !strncmp(&path[len - 4], ".xml", 4)) {
311 format = LYD_XML;
Radek Krejcie7b95092019-05-15 11:03:07 +0200312 } else if (len >= 6 && !strncmp(&path[len - 5], ".json", 5)) {
313 format = LYD_JSON;
314 } else if (len >= 5 && !strncmp(&path[len - 4], ".lyb", 4)) {
315 format = LYD_LYB;
Radek Krejci7931b192020-06-25 17:05:03 +0200316 } /* else still unknown */
Radek Krejcie7b95092019-05-15 11:03:07 +0200317 }
318
Radek Krejci7931b192020-06-25 17:05:03 +0200319 return format;
320}
Radek Krejcie7b95092019-05-15 11:03:07 +0200321
Radek Krejci7931b192020-06-25 17:05:03 +0200322API LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +0200323lyd_parse_data(const struct ly_ctx *ctx, struct ly_in *in, LYD_FORMAT format, int parse_options, int validate_options,
324 struct lyd_node **tree)
Radek Krejci7931b192020-06-25 17:05:03 +0200325{
Radek Krejci1798aae2020-07-14 13:26:06 +0200326 LY_ERR ret = LY_SUCCESS;
327 struct lyd_ctx *lydctx = NULL;
328
Radek Krejci7931b192020-06-25 17:05:03 +0200329 LY_CHECK_ARG_RET(ctx, ctx, in, tree, LY_EINVAL);
330 LY_CHECK_ARG_RET(ctx, !(parse_options & ~LYD_PARSE_OPTS_MASK), LY_EINVAL);
331 LY_CHECK_ARG_RET(ctx, !(validate_options & ~LYD_VALIDATE_OPTS_MASK), LY_EINVAL);
332
333 format = lyd_parse_get_format(in, format);
334 LY_CHECK_ARG_RET(ctx, format, LY_EINVAL);
335
Radek Krejci1798aae2020-07-14 13:26:06 +0200336 /* init */
337 *tree = NULL;
338
Michal Vasko63f3d842020-07-08 10:10:14 +0200339 /* remember input position */
340 in->func_start = in->current;
Radek Krejci7931b192020-06-25 17:05:03 +0200341
342 switch (format) {
343 case LYD_XML:
Radek Krejci1798aae2020-07-14 13:26:06 +0200344 LY_CHECK_RET(lyd_parse_xml_data(ctx, in, parse_options, validate_options, tree, &lydctx));
345 break;
Radek Krejci7931b192020-06-25 17:05:03 +0200346 case LYD_JSON:
Radek Krejci1798aae2020-07-14 13:26:06 +0200347 LY_CHECK_RET(lyd_parse_json_data(ctx, in, parse_options, validate_options, tree, &lydctx));
348 break;
Radek Krejci7931b192020-06-25 17:05:03 +0200349 case LYD_LYB:
Radek Krejci1798aae2020-07-14 13:26:06 +0200350 LY_CHECK_RET(lyd_parse_lyb_data(ctx, in, parse_options, validate_options, tree, &lydctx));
351 break;
Radek Krejci7931b192020-06-25 17:05:03 +0200352 case LYD_SCHEMA:
353 LOGINT_RET(ctx);
354 }
355
Radek Krejci1798aae2020-07-14 13:26:06 +0200356 if (!(parse_options & LYD_PARSE_ONLY)) {
357 uint32_t i = 0;
358 const struct lys_module *mod;
359 struct lyd_node *first, *next, **first2;
Radek Krejci7931b192020-06-25 17:05:03 +0200360
Radek Krejci1798aae2020-07-14 13:26:06 +0200361 next = *tree;
362 while (1) {
363 if (validate_options & LYD_VALIDATE_PRESENT) {
364 mod = lyd_data_next_module(&next, &first);
365 } else {
366 mod = lyd_mod_next_module(next, NULL, ctx, &i, &first);
367 }
368 if (!mod) {
369 break;
370 }
371 if (first == *tree) {
372 /* make sure first2 changes are carried to tree */
373 first2 = tree;
374 } else {
375 first2 = &first;
376 }
377
378 /* validate new top-level nodes, autodelete CANNOT occur, all nodes are new */
379 LY_CHECK_GOTO(ret = lyd_validate_new(first2, NULL, mod, NULL), cleanup);
380
381 /* add all top-level defaults for this module */
382 ret = lyd_new_implicit_r(NULL, first2, NULL, mod, &lydctx->unres_node_type, &lydctx->when_check,
383 (validate_options & LYD_VALIDATE_NO_STATE) ? LYD_IMPLICIT_NO_STATE : 0, NULL);
384 LY_CHECK_GOTO(ret, cleanup);
385
386 /* finish incompletely validated terminal values/attributes and when conditions */
387 ret = lyd_validate_unres(tree, &lydctx->when_check, &lydctx->unres_node_type, &lydctx->unres_meta_type, LYD_XML,
388 lydctx->resolve_prefix, lydctx->data_ctx, NULL);
389 LY_CHECK_GOTO(ret, cleanup);
390
391 /* perform final validation that assumes the data tree is final */
392 LY_CHECK_GOTO(ret = lyd_validate_final_r(*first2, NULL, mod, validate_options, 0), cleanup);
393 }
394 }
395
396cleanup:
397 lydctx->free((struct lyd_ctx *)lydctx);
398 if (ret) {
399 lyd_free_all(*tree);
400 *tree = NULL;
401 }
402 return ret;
Radek Krejci7931b192020-06-25 17:05:03 +0200403}
404
405API LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +0200406lyd_parse_data_mem(const struct ly_ctx *ctx, const char *data, LYD_FORMAT format, int parse_options, int validate_options,
407 struct lyd_node **tree)
Radek Krejci7931b192020-06-25 17:05:03 +0200408{
409 LY_ERR ret;
410 struct ly_in *in;
411
412 LY_CHECK_RET(ly_in_new_memory(data, &in));
413 ret = lyd_parse_data(ctx, in, format, parse_options, validate_options, tree);
414
415 ly_in_free(in, 0);
416 return ret;
417}
418
419API LY_ERR
Michal Vaskob104f112020-07-17 09:54:54 +0200420lyd_parse_data_fd(const struct ly_ctx *ctx, int fd, LYD_FORMAT format, int parse_options, int validate_options,
421 struct lyd_node **tree)
Radek Krejci7931b192020-06-25 17:05:03 +0200422{
423 LY_ERR ret;
424 struct ly_in *in;
425
426 LY_CHECK_RET(ly_in_new_fd(fd, &in));
427 ret = lyd_parse_data(ctx, in, format, parse_options, validate_options, tree);
428
429 ly_in_free(in, 0);
430 return ret;
431}
432
433API LY_ERR
Michal Vaskob104f112020-07-17 09:54:54 +0200434lyd_parse_data_path(const struct ly_ctx *ctx, const char *path, LYD_FORMAT format, int parse_options,
435 int validate_options, struct lyd_node **tree)
Radek Krejci7931b192020-06-25 17:05:03 +0200436{
437 LY_ERR ret;
438 struct ly_in *in;
439
440 LY_CHECK_RET(ly_in_new_filepath(path, 0, &in));
441 ret = lyd_parse_data(ctx, in, format, parse_options, validate_options, tree);
442
443 ly_in_free(in, 0);
444 return ret;
445}
446
Radek Krejci7931b192020-06-25 17:05:03 +0200447API LY_ERR
448lyd_parse_rpc(const struct ly_ctx *ctx, struct ly_in *in, LYD_FORMAT format, struct lyd_node **tree, struct lyd_node **op)
449{
450 LY_CHECK_ARG_RET(ctx, ctx, in, tree, LY_EINVAL);
451
452 format = lyd_parse_get_format(in, format);
453 LY_CHECK_ARG_RET(ctx, format, LY_EINVAL);
454
Radek Krejci1798aae2020-07-14 13:26:06 +0200455 /* init */
456 *tree = NULL;
457 if (op) {
458 *op = NULL;
459 }
460
Michal Vasko63f3d842020-07-08 10:10:14 +0200461 /* remember input position */
462 in->func_start = in->current;
463
Radek Krejci7931b192020-06-25 17:05:03 +0200464 switch (format) {
465 case LYD_XML:
Michal Vasko63f3d842020-07-08 10:10:14 +0200466 return lyd_parse_xml_rpc(ctx, in, tree, op);
Radek Krejci7931b192020-06-25 17:05:03 +0200467 case LYD_JSON:
Michal Vasko63f3d842020-07-08 10:10:14 +0200468 return lyd_parse_json_rpc(ctx, in, tree, op);
Radek Krejci7931b192020-06-25 17:05:03 +0200469 case LYD_LYB:
Michal Vasko63f3d842020-07-08 10:10:14 +0200470 return lyd_parse_lyb_rpc(ctx, in, tree, op);
Radek Krejci7931b192020-06-25 17:05:03 +0200471 case LYD_SCHEMA:
472 LOGINT_RET(ctx);
473 }
474
475 LOGINT_RET(ctx);
476}
477
478API LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +0200479lyd_parse_reply(const struct lyd_node *request, struct ly_in *in, LYD_FORMAT format, struct lyd_node **tree,
480 struct lyd_node **op)
Radek Krejci7931b192020-06-25 17:05:03 +0200481{
482 LY_CHECK_ARG_RET(NULL, request, LY_EINVAL);
Radek Krejci1798aae2020-07-14 13:26:06 +0200483 LY_CHECK_ARG_RET(LYD_NODE_CTX(request), in, tree || op, LY_EINVAL);
Radek Krejci7931b192020-06-25 17:05:03 +0200484
485 format = lyd_parse_get_format(in, format);
486 LY_CHECK_ARG_RET(LYD_NODE_CTX(request), format, LY_EINVAL);
487
Radek Krejci1798aae2020-07-14 13:26:06 +0200488 /* init */
489 if (tree) {
490 *tree = NULL;
491 }
492 if (op) {
493 *op = NULL;
494 }
495
Michal Vasko63f3d842020-07-08 10:10:14 +0200496 /* remember input position */
497 in->func_start = in->current;
498
Radek Krejci7931b192020-06-25 17:05:03 +0200499 switch (format) {
500 case LYD_XML:
Michal Vasko63f3d842020-07-08 10:10:14 +0200501 return lyd_parse_xml_reply(request, in, tree, op);
Radek Krejci7931b192020-06-25 17:05:03 +0200502 case LYD_JSON:
Michal Vasko63f3d842020-07-08 10:10:14 +0200503 return lyd_parse_json_reply(request, in, tree, op);
Radek Krejci7931b192020-06-25 17:05:03 +0200504 case LYD_LYB:
Michal Vasko63f3d842020-07-08 10:10:14 +0200505 return lyd_parse_lyb_reply(request, in, tree, op);
Radek Krejci7931b192020-06-25 17:05:03 +0200506 case LYD_SCHEMA:
507 LOGINT_RET(LYD_NODE_CTX(request));
508 }
509
510 LOGINT_RET(LYD_NODE_CTX(request));
511}
512
513API LY_ERR
514lyd_parse_notif(const struct ly_ctx *ctx, struct ly_in *in, LYD_FORMAT format, struct lyd_node **tree, struct lyd_node **ntf)
515{
Radek Krejci1798aae2020-07-14 13:26:06 +0200516 LY_CHECK_ARG_RET(ctx, ctx, in, tree || ntf, LY_EINVAL);
Radek Krejci7931b192020-06-25 17:05:03 +0200517
518 format = lyd_parse_get_format(in, format);
519 LY_CHECK_ARG_RET(ctx, format, LY_EINVAL);
520
Radek Krejci1798aae2020-07-14 13:26:06 +0200521 /* init */
522 if (tree) {
523 *tree = NULL;
524 }
525 if (ntf) {
526 *ntf = NULL;
527 }
528
Michal Vasko63f3d842020-07-08 10:10:14 +0200529 /* remember input position */
530 in->func_start = in->current;
531
Radek Krejci7931b192020-06-25 17:05:03 +0200532 switch (format) {
533 case LYD_XML:
Michal Vasko63f3d842020-07-08 10:10:14 +0200534 return lyd_parse_xml_notif(ctx, in, tree, ntf);
Radek Krejci7931b192020-06-25 17:05:03 +0200535 case LYD_JSON:
Michal Vasko63f3d842020-07-08 10:10:14 +0200536 return lyd_parse_json_notif(ctx, in, tree, ntf);
Radek Krejci7931b192020-06-25 17:05:03 +0200537 case LYD_LYB:
Michal Vasko63f3d842020-07-08 10:10:14 +0200538 return lyd_parse_lyb_notif(ctx, in, tree, ntf);
Radek Krejci7931b192020-06-25 17:05:03 +0200539 case LYD_SCHEMA:
540 LOGINT_RET(ctx);
541 }
542
543 LOGINT_RET(ctx);
Radek Krejcie7b95092019-05-15 11:03:07 +0200544}
Radek Krejci084289f2019-07-09 17:35:30 +0200545
Michal Vasko90932a92020-02-12 14:33:03 +0100546LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +0200547lyd_create_term(const struct lysc_node *schema, const char *value, size_t value_len, int *dynamic, int value_hint,
548 ly_resolve_prefix_clb get_prefix, void *prefix_data, LYD_FORMAT format, struct lyd_node **node)
Michal Vasko90932a92020-02-12 14:33:03 +0100549{
550 LY_ERR ret;
551 struct lyd_node_term *term;
552
Michal Vasko9b368d32020-02-14 13:53:31 +0100553 assert(schema->nodetype & LYD_NODE_TERM);
554
Michal Vasko90932a92020-02-12 14:33:03 +0100555 term = calloc(1, sizeof *term);
556 LY_CHECK_ERR_RET(!term, LOGMEM(schema->module->ctx), LY_EMEM);
557
558 term->schema = schema;
559 term->prev = (struct lyd_node *)term;
Michal Vasko9b368d32020-02-14 13:53:31 +0100560 term->flags = LYD_NEW;
Michal Vasko90932a92020-02-12 14:33:03 +0100561
Radek Krejci1798aae2020-07-14 13:26:06 +0200562 ret = lyd_value_parse(term, value, value_len, dynamic, 0, value_hint, get_prefix, prefix_data, format, NULL);
Michal Vasko90932a92020-02-12 14:33:03 +0100563 if (ret && (ret != LY_EINCOMPLETE)) {
564 free(term);
565 return ret;
566 }
Michal Vasko9b368d32020-02-14 13:53:31 +0100567 lyd_hash((struct lyd_node *)term);
568
569 *node = (struct lyd_node *)term;
570 return ret;
571}
572
573LY_ERR
574lyd_create_term2(const struct lysc_node *schema, const struct lyd_value *val, struct lyd_node **node)
575{
576 LY_ERR ret;
577 struct lyd_node_term *term;
578 struct lysc_type *type;
579
580 assert(schema->nodetype & LYD_NODE_TERM);
Michal Vasko00cbf532020-06-15 13:58:47 +0200581 assert(val && val->realtype);
Michal Vasko9b368d32020-02-14 13:53:31 +0100582
583 term = calloc(1, sizeof *term);
584 LY_CHECK_ERR_RET(!term, LOGMEM(schema->module->ctx), LY_EMEM);
585
586 term->schema = schema;
587 term->prev = (struct lyd_node *)term;
588 term->flags = LYD_NEW;
589
590 type = ((struct lysc_node_leaf *)schema)->type;
591 ret = type->plugin->duplicate(schema->module->ctx, val, &term->value);
592 if (ret) {
593 LOGERR(schema->module->ctx, ret, "Value duplication failed.");
594 free(term);
595 return ret;
596 }
Michal Vasko00cbf532020-06-15 13:58:47 +0200597 term->value.realtype = val->realtype;
Michal Vasko9b368d32020-02-14 13:53:31 +0100598 lyd_hash((struct lyd_node *)term);
Michal Vasko90932a92020-02-12 14:33:03 +0100599
600 *node = (struct lyd_node *)term;
601 return ret;
602}
603
604LY_ERR
605lyd_create_inner(const struct lysc_node *schema, struct lyd_node **node)
606{
607 struct lyd_node_inner *in;
608
Michal Vasko9b368d32020-02-14 13:53:31 +0100609 assert(schema->nodetype & LYD_NODE_INNER);
610
Michal Vasko90932a92020-02-12 14:33:03 +0100611 in = calloc(1, sizeof *in);
612 LY_CHECK_ERR_RET(!in, LOGMEM(schema->module->ctx), LY_EMEM);
613
614 in->schema = schema;
615 in->prev = (struct lyd_node *)in;
Michal Vasko9b368d32020-02-14 13:53:31 +0100616 in->flags = LYD_NEW;
Michal Vasko90932a92020-02-12 14:33:03 +0100617
Michal Vasko9b368d32020-02-14 13:53:31 +0100618 /* do not hash list with keys, we need them for the hash */
619 if ((schema->nodetype != LYS_LIST) || (schema->flags & LYS_KEYLESS)) {
620 lyd_hash((struct lyd_node *)in);
621 }
Michal Vasko90932a92020-02-12 14:33:03 +0100622
623 *node = (struct lyd_node *)in;
624 return LY_SUCCESS;
625}
626
Michal Vasko90932a92020-02-12 14:33:03 +0100627LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +0200628lyd_create_list(const struct lysc_node *schema, const struct ly_path_predicate *predicates, struct lyd_node **node)
Michal Vasko90932a92020-02-12 14:33:03 +0100629{
630 LY_ERR ret = LY_SUCCESS;
Michal Vasko90932a92020-02-12 14:33:03 +0100631 struct lyd_node *list = NULL, *key;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200632 LY_ARRAY_COUNT_TYPE u;
Michal Vasko90932a92020-02-12 14:33:03 +0100633
Michal Vasko004d3152020-06-11 19:59:22 +0200634 assert((schema->nodetype == LYS_LIST) && !(schema->flags & LYS_KEYLESS));
Michal Vasko90932a92020-02-12 14:33:03 +0100635
636 /* create list */
637 LY_CHECK_GOTO(ret = lyd_create_inner(schema, &list), cleanup);
638
Michal Vasko90932a92020-02-12 14:33:03 +0100639 /* create and insert all the keys */
Michal Vasko004d3152020-06-11 19:59:22 +0200640 LY_ARRAY_FOR(predicates, u) {
641 LY_CHECK_GOTO(ret = lyd_create_term2(predicates[u].key, &predicates[u].value, &key), cleanup);
Michal Vasko90932a92020-02-12 14:33:03 +0100642 lyd_insert_node(list, NULL, key);
643 }
644
Michal Vasko9b368d32020-02-14 13:53:31 +0100645 /* hash having all the keys */
646 lyd_hash(list);
647
Michal Vasko90932a92020-02-12 14:33:03 +0100648 /* success */
649 *node = list;
650 list = NULL;
651
652cleanup:
653 lyd_free_tree(list);
Michal Vasko004d3152020-06-11 19:59:22 +0200654 return ret;
655}
656
657static LY_ERR
658lyd_create_list2(const struct lysc_node *schema, const char *keys, size_t keys_len, struct lyd_node **node)
659{
660 LY_ERR ret = LY_SUCCESS;
661 struct lyxp_expr *expr = NULL;
662 uint16_t exp_idx = 0;
663 enum ly_path_pred_type pred_type = 0;
664 struct ly_path_predicate *predicates = NULL;
665
666 /* parse keys */
Michal Vasko6b26e742020-07-17 15:02:10 +0200667 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 +0200668 LY_PATH_PRED_KEYS, &expr), cleanup);
669
670 /* compile them */
Michal Vasko6b26e742020-07-17 15:02:10 +0200671 LY_CHECK_GOTO(ret = ly_path_compile_predicate(schema->module->ctx, NULL, NULL, schema, expr, &exp_idx,
672 lydjson_resolve_prefix, NULL, LYD_JSON, &predicates, &pred_type), cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +0200673
674 /* create the list node */
675 LY_CHECK_GOTO(ret = lyd_create_list(schema, predicates, node), cleanup);
676
677cleanup:
678 lyxp_expr_free(schema->module->ctx, expr);
679 ly_path_predicates_free(schema->module->ctx, pred_type, NULL, predicates);
Michal Vasko90932a92020-02-12 14:33:03 +0100680 return ret;
681}
682
683LY_ERR
684lyd_create_any(const struct lysc_node *schema, const void *value, LYD_ANYDATA_VALUETYPE value_type, struct lyd_node **node)
685{
686 struct lyd_node_any *any;
687
Michal Vasko9b368d32020-02-14 13:53:31 +0100688 assert(schema->nodetype & LYD_NODE_ANY);
689
Michal Vasko90932a92020-02-12 14:33:03 +0100690 any = calloc(1, sizeof *any);
691 LY_CHECK_ERR_RET(!any, LOGMEM(schema->module->ctx), LY_EMEM);
692
693 any->schema = schema;
694 any->prev = (struct lyd_node *)any;
Michal Vasko9b368d32020-02-14 13:53:31 +0100695 any->flags = LYD_NEW;
Michal Vasko90932a92020-02-12 14:33:03 +0100696
Radek Krejci1798aae2020-07-14 13:26:06 +0200697 /* TODO: convert XML/JSON strings into a opaq data tree */
698 any->value.str = value;
Michal Vasko90932a92020-02-12 14:33:03 +0100699 any->value_type = value_type;
Michal Vasko9b368d32020-02-14 13:53:31 +0100700 lyd_hash((struct lyd_node *)any);
Michal Vasko90932a92020-02-12 14:33:03 +0100701
702 *node = (struct lyd_node *)any;
703 return LY_SUCCESS;
704}
705
Michal Vasko52927e22020-03-16 17:26:14 +0100706LY_ERR
707lyd_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 +0200708 int *dynamic, int value_hint, LYD_FORMAT format, struct ly_prefix *val_prefs, const char *prefix, size_t pref_len,
709 const char *module_key, size_t module_key_len, struct lyd_node **node)
Michal Vasko52927e22020-03-16 17:26:14 +0100710{
711 struct lyd_node_opaq *opaq;
712
Radek Krejci1798aae2020-07-14 13:26:06 +0200713 assert(ctx && name && name_len);
Michal Vasko52927e22020-03-16 17:26:14 +0100714
715 if (!value_len) {
716 value = "";
717 }
718
719 opaq = calloc(1, sizeof *opaq);
720 LY_CHECK_ERR_RET(!opaq, LOGMEM(ctx), LY_EMEM);
721
722 opaq->prev = (struct lyd_node *)opaq;
723
724 opaq->name = lydict_insert(ctx, name, name_len);
725 opaq->format = format;
726 if (pref_len) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200727 opaq->prefix.id = lydict_insert(ctx, prefix, pref_len);
Michal Vasko52927e22020-03-16 17:26:14 +0100728 }
Radek Krejci1798aae2020-07-14 13:26:06 +0200729 if (module_key_len) {
730 opaq->prefix.module_ns = lydict_insert(ctx, module_key, module_key_len);
731 }
732
Michal Vasko52927e22020-03-16 17:26:14 +0100733 opaq->val_prefs = val_prefs;
734 if (dynamic && *dynamic) {
735 opaq->value = lydict_insert_zc(ctx, (char *)value);
736 *dynamic = 0;
737 } else {
738 opaq->value = lydict_insert(ctx, value, value_len);
739 }
Radek Krejci1798aae2020-07-14 13:26:06 +0200740 opaq->hint = value_hint;
Michal Vasko52927e22020-03-16 17:26:14 +0100741 opaq->ctx = ctx;
742
743 *node = (struct lyd_node *)opaq;
744 return LY_SUCCESS;
745}
746
Michal Vasko3a41dff2020-07-15 14:30:28 +0200747API LY_ERR
748lyd_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 +0100749{
750 struct lyd_node *ret = NULL;
751 const struct lysc_node *schema;
752 struct ly_ctx *ctx = parent ? parent->schema->module->ctx : (module ? module->ctx : NULL);
753
Michal Vasko6027eb92020-07-15 16:37:30 +0200754 LY_CHECK_ARG_RET(ctx, parent || module, parent || node, name, LY_EINVAL);
Michal Vasko013a8182020-03-03 10:46:53 +0100755
Michal Vaskof03ed032020-03-04 13:31:44 +0100756 if (!module) {
757 module = parent->schema->module;
758 }
759
Michal Vasko3a41dff2020-07-15 14:30:28 +0200760 schema = lys_find_child(parent ? parent->schema : NULL, module, name, 0,
761 LYS_CONTAINER | LYS_NOTIF | LYS_RPC | LYS_ACTION, 0);
762 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 +0100763
Michal Vasko3a41dff2020-07-15 14:30:28 +0200764 LY_CHECK_RET(lyd_create_inner(schema, &ret));
765 if (parent) {
Michal Vasko013a8182020-03-03 10:46:53 +0100766 lyd_insert_node(parent, NULL, ret);
767 }
Michal Vasko3a41dff2020-07-15 14:30:28 +0200768
769 if (node) {
770 *node = ret;
771 }
772 return LY_SUCCESS;
Michal Vasko013a8182020-03-03 10:46:53 +0100773}
774
Michal Vasko3a41dff2020-07-15 14:30:28 +0200775API LY_ERR
776lyd_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 +0100777{
778 struct lyd_node *ret = NULL, *key;
779 const struct lysc_node *schema, *key_s;
780 struct ly_ctx *ctx = parent ? parent->schema->module->ctx : (module ? module->ctx : NULL);
781 va_list ap;
782 const char *key_val;
783 LY_ERR rc = LY_SUCCESS;
784
Michal Vasko6027eb92020-07-15 16:37:30 +0200785 LY_CHECK_ARG_RET(ctx, parent || module, parent || node, name, LY_EINVAL);
Michal Vasko013a8182020-03-03 10:46:53 +0100786
Michal Vaskof03ed032020-03-04 13:31:44 +0100787 if (!module) {
788 module = parent->schema->module;
789 }
790
Michal Vasko013a8182020-03-03 10:46:53 +0100791 schema = lys_find_child(parent ? parent->schema : NULL, module, name, 0, LYS_LIST, 0);
Michal Vasko3a41dff2020-07-15 14:30:28 +0200792 LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "List node \"%s\" not found.", name), LY_ENOTFOUND);
Michal Vasko013a8182020-03-03 10:46:53 +0100793
794 /* create list inner node */
Michal Vasko3a41dff2020-07-15 14:30:28 +0200795 LY_CHECK_RET(lyd_create_inner(schema, &ret));
Michal Vasko013a8182020-03-03 10:46:53 +0100796
Michal Vasko3a41dff2020-07-15 14:30:28 +0200797 va_start(ap, node);
Michal Vasko013a8182020-03-03 10:46:53 +0100798
799 /* create and insert all the keys */
800 for (key_s = lysc_node_children(schema, 0); key_s && (key_s->flags & LYS_KEY); key_s = key_s->next) {
801 key_val = va_arg(ap, const char *);
802
Radek Krejci1798aae2020-07-14 13:26:06 +0200803 rc = lyd_create_term(key_s, key_val, key_val ? strlen(key_val) : 0, NULL, 0, lydjson_resolve_prefix, NULL, LYD_JSON, &key);
Michal Vaskocbff3e92020-05-27 12:56:41 +0200804 LY_CHECK_GOTO(rc && (rc != LY_EINCOMPLETE), cleanup);
805 rc = LY_SUCCESS;
Michal Vasko013a8182020-03-03 10:46:53 +0100806 lyd_insert_node(ret, NULL, key);
807 }
808
Michal Vasko013a8182020-03-03 10:46:53 +0100809 if (parent) {
810 lyd_insert_node(parent, NULL, ret);
811 }
812
813cleanup:
Michal Vasko3a41dff2020-07-15 14:30:28 +0200814 va_end(ap);
Michal Vasko013a8182020-03-03 10:46:53 +0100815 if (rc) {
816 lyd_free_tree(ret);
817 ret = NULL;
Michal Vasko3a41dff2020-07-15 14:30:28 +0200818 } else if (node) {
819 *node = ret;
Michal Vasko013a8182020-03-03 10:46:53 +0100820 }
Michal Vasko3a41dff2020-07-15 14:30:28 +0200821 return rc;
Michal Vasko013a8182020-03-03 10:46:53 +0100822}
823
Michal Vasko3a41dff2020-07-15 14:30:28 +0200824API LY_ERR
825lyd_new_list2(struct lyd_node *parent, const struct lys_module *module, const char *name, const char *keys,
826 struct lyd_node **node)
Michal Vasko013a8182020-03-03 10:46:53 +0100827{
828 struct lyd_node *ret = NULL;
829 const struct lysc_node *schema;
830 struct ly_ctx *ctx = parent ? parent->schema->module->ctx : (module ? module->ctx : NULL);
831
Michal Vasko6027eb92020-07-15 16:37:30 +0200832 LY_CHECK_ARG_RET(ctx, parent || module, parent || node, name, LY_EINVAL);
Michal Vasko013a8182020-03-03 10:46:53 +0100833
Michal Vaskof03ed032020-03-04 13:31:44 +0100834 if (!module) {
835 module = parent->schema->module;
836 }
Michal Vasko004d3152020-06-11 19:59:22 +0200837 if (!keys) {
838 keys = "";
839 }
Michal Vaskof03ed032020-03-04 13:31:44 +0100840
Michal Vasko004d3152020-06-11 19:59:22 +0200841 /* find schema node */
Michal Vasko013a8182020-03-03 10:46:53 +0100842 schema = lys_find_child(parent ? parent->schema : NULL, module, name, 0, LYS_LIST, 0);
Michal Vasko3a41dff2020-07-15 14:30:28 +0200843 LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "List node \"%s\" not found.", name), LY_ENOTFOUND);
Michal Vasko013a8182020-03-03 10:46:53 +0100844
Michal Vasko004d3152020-06-11 19:59:22 +0200845 if ((schema->flags & LYS_KEYLESS) && !keys[0]) {
846 /* key-less list */
Michal Vasko3a41dff2020-07-15 14:30:28 +0200847 LY_CHECK_RET(lyd_create_inner(schema, &ret));
Michal Vasko004d3152020-06-11 19:59:22 +0200848 } else {
849 /* create the list node */
Michal Vasko3a41dff2020-07-15 14:30:28 +0200850 LY_CHECK_RET(lyd_create_list2(schema, keys, strlen(keys), &ret));
Michal Vasko004d3152020-06-11 19:59:22 +0200851 }
Michal Vasko004d3152020-06-11 19:59:22 +0200852 if (parent) {
Michal Vasko013a8182020-03-03 10:46:53 +0100853 lyd_insert_node(parent, NULL, ret);
854 }
Michal Vasko3a41dff2020-07-15 14:30:28 +0200855
856 if (node) {
857 *node = ret;
858 }
859 return LY_SUCCESS;
Michal Vasko013a8182020-03-03 10:46:53 +0100860}
861
Michal Vasko3a41dff2020-07-15 14:30:28 +0200862API LY_ERR
863lyd_new_term(struct lyd_node *parent, const struct lys_module *module, const char *name, const char *val_str,
864 struct lyd_node **node)
Michal Vasko013a8182020-03-03 10:46:53 +0100865{
Michal Vaskocbff3e92020-05-27 12:56:41 +0200866 LY_ERR rc;
Michal Vasko013a8182020-03-03 10:46:53 +0100867 struct lyd_node *ret = NULL;
868 const struct lysc_node *schema;
869 struct ly_ctx *ctx = parent ? parent->schema->module->ctx : (module ? module->ctx : NULL);
870
Michal Vasko6027eb92020-07-15 16:37:30 +0200871 LY_CHECK_ARG_RET(ctx, parent || module, parent || node, name, LY_EINVAL);
Michal Vasko013a8182020-03-03 10:46:53 +0100872
Michal Vaskof03ed032020-03-04 13:31:44 +0100873 if (!module) {
874 module = parent->schema->module;
875 }
876
Michal Vasko013a8182020-03-03 10:46:53 +0100877 schema = lys_find_child(parent ? parent->schema : NULL, module, name, 0, LYD_NODE_TERM, 0);
Michal Vasko3a41dff2020-07-15 14:30:28 +0200878 LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "Term node \"%s\" not found.", name), LY_ENOTFOUND);
Michal Vasko013a8182020-03-03 10:46:53 +0100879
Radek Krejci1798aae2020-07-14 13:26:06 +0200880 rc = lyd_create_term(schema, val_str, val_str ? strlen(val_str) : 0, NULL, 0, lydjson_resolve_prefix, NULL, LYD_JSON, &ret);
Michal Vasko3a41dff2020-07-15 14:30:28 +0200881 LY_CHECK_RET(rc && (rc != LY_EINCOMPLETE), rc);
Michal Vaskocbff3e92020-05-27 12:56:41 +0200882 if (parent) {
Michal Vasko013a8182020-03-03 10:46:53 +0100883 lyd_insert_node(parent, NULL, ret);
884 }
Michal Vasko3a41dff2020-07-15 14:30:28 +0200885
886 if (node) {
887 *node = ret;
888 }
889 return LY_SUCCESS;
Michal Vasko013a8182020-03-03 10:46:53 +0100890}
891
Michal Vasko3a41dff2020-07-15 14:30:28 +0200892API LY_ERR
Michal Vasko013a8182020-03-03 10:46:53 +0100893lyd_new_any(struct lyd_node *parent, const struct lys_module *module, const char *name, const void *value,
Michal Vasko3a41dff2020-07-15 14:30:28 +0200894 LYD_ANYDATA_VALUETYPE value_type, struct lyd_node **node)
Michal Vasko013a8182020-03-03 10:46:53 +0100895{
896 struct lyd_node *ret = NULL;
897 const struct lysc_node *schema;
898 struct ly_ctx *ctx = parent ? parent->schema->module->ctx : (module ? module->ctx : NULL);
899
Michal Vasko6027eb92020-07-15 16:37:30 +0200900 LY_CHECK_ARG_RET(ctx, parent || module, parent || node, name, LY_EINVAL);
Michal Vasko013a8182020-03-03 10:46:53 +0100901
Michal Vaskof03ed032020-03-04 13:31:44 +0100902 if (!module) {
903 module = parent->schema->module;
904 }
905
Michal Vasko013a8182020-03-03 10:46:53 +0100906 schema = lys_find_child(parent ? parent->schema : NULL, module, name, 0, LYD_NODE_ANY, 0);
Michal Vasko3a41dff2020-07-15 14:30:28 +0200907 LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "Any node \"%s\" not found.", name), LY_ENOTFOUND);
Michal Vasko013a8182020-03-03 10:46:53 +0100908
Michal Vasko3a41dff2020-07-15 14:30:28 +0200909 LY_CHECK_RET(lyd_create_any(schema, value, value_type, &ret));
910 if (parent) {
Michal Vasko013a8182020-03-03 10:46:53 +0100911 lyd_insert_node(parent, NULL, ret);
912 }
Michal Vasko3a41dff2020-07-15 14:30:28 +0200913
914 if (node) {
915 *node = ret;
916 }
917 return LY_SUCCESS;
Michal Vasko013a8182020-03-03 10:46:53 +0100918}
919
Michal Vasko4490d312020-06-16 13:08:55 +0200920/**
921 * @brief Update node value.
922 *
923 * @param[in] node Node to update.
924 * @param[in] value New value to set.
925 * @param[in] value_type Type of @p value for any node.
926 * @param[out] new_parent Set to @p node if the value was updated, otherwise set to NULL.
927 * @param[out] new_node Set to @p node if the value was updated, otherwise set to NULL.
928 * @return LY_ERR value.
929 */
Michal Vasko00cbf532020-06-15 13:58:47 +0200930static LY_ERR
931lyd_new_path_update(struct lyd_node *node, const void *value, LYD_ANYDATA_VALUETYPE value_type,
932 struct lyd_node **new_parent, struct lyd_node **new_node)
933{
934 LY_ERR ret = LY_SUCCESS;
935 struct lyd_node *new_any;
936
937 switch (node->schema->nodetype) {
938 case LYS_CONTAINER:
939 case LYS_NOTIF:
940 case LYS_RPC:
941 case LYS_ACTION:
942 case LYS_LIST:
943 case LYS_LEAFLIST:
944 /* if it exists, there is nothing to update */
945 *new_parent = NULL;
946 *new_node = NULL;
947 break;
948 case LYS_LEAF:
949 ret = lyd_change_term(node, value);
950 if ((ret == LY_SUCCESS) || (ret == LY_EEXIST)) {
951 /* there was an actual change (at least of the default flag) */
952 *new_parent = node;
953 *new_node = node;
954 ret = LY_SUCCESS;
955 } else if (ret == LY_ENOT) {
956 /* no change */
957 *new_parent = NULL;
958 *new_node = NULL;
959 ret = LY_SUCCESS;
960 } /* else error */
961 break;
962 case LYS_ANYDATA:
963 case LYS_ANYXML:
964 /* create a new any node */
965 LY_CHECK_RET(lyd_create_any(node->schema, value, value_type, &new_any));
966
967 /* compare with the existing one */
Michal Vasko8f359bf2020-07-28 10:41:15 +0200968 if (lyd_compare_single(node, new_any, 0)) {
Michal Vasko00cbf532020-06-15 13:58:47 +0200969 /* not equal, switch values (so that we can use generic node free) */
970 ((struct lyd_node_any *)new_any)->value = ((struct lyd_node_any *)node)->value;
971 ((struct lyd_node_any *)new_any)->value_type = ((struct lyd_node_any *)node)->value_type;
972 ((struct lyd_node_any *)node)->value.str = value;
973 ((struct lyd_node_any *)node)->value_type = value_type;
974
975 *new_parent = node;
976 *new_node = node;
977 } else {
978 /* they are equal */
979 *new_parent = NULL;
980 *new_node = NULL;
981 }
982 lyd_free_tree(new_any);
983 break;
984 default:
985 LOGINT(LYD_NODE_CTX(node));
986 ret = LY_EINT;
987 break;
988 }
989
990 return ret;
991}
992
Michal Vasko3a41dff2020-07-15 14:30:28 +0200993API LY_ERR
994lyd_new_meta(struct lyd_node *parent, const struct lys_module *module, const char *name, const char *val_str,
995 struct lyd_meta **meta)
Michal Vaskod86997b2020-05-26 15:19:54 +0200996{
997 struct lyd_meta *ret = NULL;
Michal Vasko00cbf532020-06-15 13:58:47 +0200998 const struct ly_ctx *ctx;
Michal Vaskod86997b2020-05-26 15:19:54 +0200999 const char *prefix, *tmp;
Michal Vaskod86997b2020-05-26 15:19:54 +02001000 size_t pref_len, name_len;
1001
Michal Vasko3a41dff2020-07-15 14:30:28 +02001002 LY_CHECK_ARG_RET(NULL, parent, name, module || strchr(name, ':'), LY_EINVAL);
Michal Vasko00cbf532020-06-15 13:58:47 +02001003
1004 ctx = LYD_NODE_CTX(parent);
Michal Vaskod86997b2020-05-26 15:19:54 +02001005
1006 /* parse the name */
1007 tmp = name;
1008 if (ly_parse_nodeid(&tmp, &prefix, &pref_len, &name, &name_len) || tmp[0]) {
1009 LOGERR(ctx, LY_EINVAL, "Metadata name \"%s\" is not valid.", name);
Michal Vasko3a41dff2020-07-15 14:30:28 +02001010 return LY_EVALID;
Michal Vaskod86997b2020-05-26 15:19:54 +02001011 }
1012
1013 /* find the module */
1014 if (prefix) {
Radek Krejci0ad51f12020-07-16 12:08:12 +02001015 module = ly_ctx_get_module_implemented2(ctx, prefix, pref_len);
Michal Vasko3a41dff2020-07-15 14:30:28 +02001016 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 +02001017 }
1018
1019 /* set value if none */
1020 if (!val_str) {
1021 val_str = "";
1022 }
1023
Radek Krejci1798aae2020-07-14 13:26:06 +02001024 LY_CHECK_RET(lyd_create_meta(parent, &ret, module, name, name_len, val_str, strlen(val_str), NULL, 0,
Michal Vasko3a41dff2020-07-15 14:30:28 +02001025 lydjson_resolve_prefix, NULL, LYD_JSON, parent->schema));
1026
1027 if (meta) {
1028 *meta = ret;
1029 }
1030 return LY_SUCCESS;
Michal Vaskod86997b2020-05-26 15:19:54 +02001031}
1032
Michal Vasko3a41dff2020-07-15 14:30:28 +02001033API LY_ERR
Michal Vasko00cbf532020-06-15 13:58:47 +02001034lyd_new_opaq(struct lyd_node *parent, const struct ly_ctx *ctx, const char *name, const char *value,
Michal Vasko3a41dff2020-07-15 14:30:28 +02001035 const char *module_name, struct lyd_node **node)
Michal Vasko00cbf532020-06-15 13:58:47 +02001036{
1037 struct lyd_node *ret = NULL;
1038
Michal Vasko6027eb92020-07-15 16:37:30 +02001039 LY_CHECK_ARG_RET(ctx, parent || ctx, parent || node, name, module_name, LY_EINVAL);
Michal Vasko00cbf532020-06-15 13:58:47 +02001040
1041 if (!ctx) {
1042 ctx = LYD_NODE_CTX(parent);
1043 }
1044 if (!value) {
1045 value = "";
1046 }
1047
Radek Krejci1798aae2020-07-14 13:26:06 +02001048 LY_CHECK_RET(lyd_create_opaq(ctx, name, strlen(name), value, strlen(value), NULL, 0,
1049 LYD_JSON, NULL, NULL, 0, module_name, strlen(module_name), &ret));
Michal Vasko3a41dff2020-07-15 14:30:28 +02001050 if (parent) {
Michal Vasko00cbf532020-06-15 13:58:47 +02001051 lyd_insert_node(parent, NULL, ret);
1052 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02001053
1054 if (node) {
1055 *node = ret;
1056 }
1057 return LY_SUCCESS;
Michal Vasko00cbf532020-06-15 13:58:47 +02001058}
1059
Michal Vasko3a41dff2020-07-15 14:30:28 +02001060API LY_ERR
1061lyd_new_attr(struct lyd_node *parent, const char *module_name, const char *name, const char *val_str,
Radek Krejci1798aae2020-07-14 13:26:06 +02001062 struct lyd_attr **attr)
Michal Vasko00cbf532020-06-15 13:58:47 +02001063{
Radek Krejci1798aae2020-07-14 13:26:06 +02001064 struct lyd_attr *ret = NULL;
Michal Vasko00cbf532020-06-15 13:58:47 +02001065 const struct ly_ctx *ctx;
1066 const char *prefix, *tmp;
1067 size_t pref_len, name_len;
1068
Michal Vasko3a41dff2020-07-15 14:30:28 +02001069 LY_CHECK_ARG_RET(NULL, parent, !parent->schema, name, LY_EINVAL);
Michal Vasko00cbf532020-06-15 13:58:47 +02001070
1071 ctx = LYD_NODE_CTX(parent);
1072
1073 /* parse the name */
1074 tmp = name;
1075 if (ly_parse_nodeid(&tmp, &prefix, &pref_len, &name, &name_len) || tmp[0]) {
1076 LOGERR(ctx, LY_EINVAL, "Metadata name \"%s\" is not valid.", name);
Michal Vasko3a41dff2020-07-15 14:30:28 +02001077 return LY_EVALID;
Michal Vasko00cbf532020-06-15 13:58:47 +02001078 }
1079
1080 /* set value if none */
1081 if (!val_str) {
1082 val_str = "";
1083 }
1084
Radek Krejci1798aae2020-07-14 13:26:06 +02001085 LY_CHECK_RET(lyd_create_attr(parent, &ret, ctx, name, name_len, val_str, strlen(val_str), NULL, 0, LYD_JSON, NULL,
1086 prefix, pref_len, module_name, module_name ? strlen(module_name) : 0));
Michal Vasko3a41dff2020-07-15 14:30:28 +02001087
1088 if (attr) {
1089 *attr = ret;
1090 }
1091 return LY_SUCCESS;
Michal Vasko00cbf532020-06-15 13:58:47 +02001092}
1093
1094API LY_ERR
1095lyd_change_term(struct lyd_node *term, const char *val_str)
1096{
1097 LY_ERR ret = LY_SUCCESS;
1098 struct lysc_type *type;
1099 struct lyd_node_term *t;
1100 struct lyd_node *parent;
1101 struct lyd_value val = {0};
1102 int dflt_change, val_change;
1103
1104 LY_CHECK_ARG_RET(NULL, term, term->schema, term->schema->nodetype & LYD_NODE_TERM, LY_EINVAL);
1105
1106 if (!val_str) {
1107 val_str = "";
1108 }
1109 t = (struct lyd_node_term *)term;
1110 type = ((struct lysc_node_leaf *)term->schema)->type;
1111
1112 /* parse the new value */
1113 LY_CHECK_GOTO(ret = lyd_value_store(&val, term->schema, val_str, strlen(val_str), NULL, lydjson_resolve_prefix, NULL,
1114 LYD_JSON), cleanup);
1115
1116 /* compare original and new value */
1117 if (type->plugin->compare(&t->value, &val)) {
1118 /* values differ, switch them */
1119 type->plugin->free(LYD_NODE_CTX(term), &t->value);
1120 t->value = val;
1121 memset(&val, 0, sizeof val);
1122 val_change = 1;
1123 } else {
1124 val_change = 0;
1125 }
1126
1127 /* always clear the default flag */
1128 if (term->flags & LYD_DEFAULT) {
1129 for (parent = term; parent; parent = (struct lyd_node *)parent->parent) {
1130 parent->flags &= ~LYD_DEFAULT;
1131 }
1132 dflt_change = 1;
1133 } else {
1134 dflt_change = 0;
1135 }
1136
1137 if (val_change || dflt_change) {
1138 /* make the node non-validated */
1139 term->flags &= LYD_NEW;
1140 }
1141
1142 if (val_change) {
1143 if (term->schema->nodetype == LYS_LEAFLIST) {
1144 /* leaf-list needs to be hashed again and re-inserted into parent */
1145 lyd_unlink_hash(term);
1146 lyd_hash(term);
1147 LY_CHECK_GOTO(ret = lyd_insert_hash(term), cleanup);
1148 } else if ((term->schema->flags & LYS_KEY) && term->parent) {
1149 /* list needs to be updated if its key was changed */
1150 assert(term->parent->schema->nodetype == LYS_LIST);
1151 lyd_unlink_hash((struct lyd_node *)term->parent);
1152 lyd_hash((struct lyd_node *)term->parent);
1153 LY_CHECK_GOTO(ret = lyd_insert_hash((struct lyd_node *)term->parent), cleanup);
1154 } /* else leaf that is not a key, its value is not used for its hash so it does not change */
1155 }
1156
1157 /* retrun value */
1158 if (!val_change) {
1159 if (dflt_change) {
1160 /* only default flag change */
1161 ret = LY_EEXIST;
1162 } else {
1163 /* no change */
1164 ret = LY_ENOT;
1165 }
1166 } /* else value changed, LY_SUCCESS */
1167
1168cleanup:
1169 type->plugin->free(LYD_NODE_CTX(term), &val);
1170 return ret;
1171}
1172
Michal Vasko41586352020-07-13 13:54:25 +02001173API LY_ERR
1174lyd_change_meta(struct lyd_meta *meta, const char *val_str)
1175{
1176 LY_ERR ret = LY_SUCCESS;
1177 struct lyd_meta *m2;
1178 struct lyd_value val;
1179 int val_change;
1180
1181 LY_CHECK_ARG_RET(NULL, meta, LY_EINVAL);
1182
1183 if (!val_str) {
1184 val_str = "";
1185 }
1186
1187 /* parse the new value into a new meta structure */
1188 LY_CHECK_GOTO(ret = lyd_create_meta(NULL, &m2, meta->annotation->module, meta->name, strlen(meta->name), val_str,
Radek Krejci1798aae2020-07-14 13:26:06 +02001189 strlen(val_str), NULL, 0, lydjson_resolve_prefix, NULL, LYD_JSON, NULL), cleanup);
Michal Vasko41586352020-07-13 13:54:25 +02001190
1191 /* compare original and new value */
1192 if (lyd_compare_meta(meta, m2)) {
1193 /* values differ, switch them */
1194 val = meta->value;
1195 meta->value = m2->value;
1196 m2->value = val;
1197 val_change = 1;
1198 } else {
1199 val_change = 0;
1200 }
1201
1202 /* retrun value */
1203 if (!val_change) {
1204 /* no change */
1205 ret = LY_ENOT;
1206 } /* else value changed, LY_SUCCESS */
1207
1208cleanup:
1209 return ret;
1210}
1211
Michal Vasko3a41dff2020-07-15 14:30:28 +02001212API LY_ERR
1213lyd_new_path(struct lyd_node *parent, const struct ly_ctx *ctx, const char *path, const char *value, int options,
1214 struct lyd_node **node)
Michal Vasko00cbf532020-06-15 13:58:47 +02001215{
Michal Vasko3a41dff2020-07-15 14:30:28 +02001216 return lyd_new_path2(parent, ctx, path, value, 0, options, node, NULL);
Michal Vasko00cbf532020-06-15 13:58:47 +02001217}
1218
1219API LY_ERR
1220lyd_new_path2(struct lyd_node *parent, const struct ly_ctx *ctx, const char *path, const void *value,
1221 LYD_ANYDATA_VALUETYPE value_type, int options, struct lyd_node **new_parent, struct lyd_node **new_node)
1222{
1223 LY_ERR ret = LY_SUCCESS, r;
1224 struct lyxp_expr *exp = NULL;
1225 struct ly_path *p = NULL;
1226 struct lyd_node *nparent = NULL, *nnode = NULL, *node = NULL, *cur_parent;
1227 const struct lysc_node *schema;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001228 LY_ARRAY_COUNT_TYPE path_idx = 0;
Michal Vasko00cbf532020-06-15 13:58:47 +02001229 struct ly_path_predicate *pred;
1230
1231 LY_CHECK_ARG_RET(ctx, parent || ctx, path, (path[0] == '/') || parent, LY_EINVAL);
1232
1233 if (!ctx) {
1234 ctx = LYD_NODE_CTX(parent);
1235 }
1236
1237 /* parse path */
Michal Vasko6b26e742020-07-17 15:02:10 +02001238 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 +02001239 LY_PATH_PREFIX_OPTIONAL, LY_PATH_PRED_SIMPLE, &exp), cleanup);
1240
1241 /* compile path */
1242 LY_CHECK_GOTO(ret = ly_path_compile(ctx, NULL, parent ? parent->schema : NULL, exp, LY_PATH_LREF_FALSE,
1243 options & LYD_NEWOPT_OUTPUT ? LY_PATH_OPER_OUTPUT : LY_PATH_OPER_INPUT,
1244 LY_PATH_TARGET_MANY, lydjson_resolve_prefix, NULL, LYD_JSON, &p), cleanup);
1245
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001246 schema = p[LY_ARRAY_COUNT(p) - 1].node;
1247 if ((schema->nodetype == LYS_LIST) && (p[LY_ARRAY_COUNT(p) - 1].pred_type == LY_PATH_PREDTYPE_NONE)
Michal Vasko00cbf532020-06-15 13:58:47 +02001248 && !(options & LYD_NEWOPT_OPAQ)) {
1249 LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_XPATH, "Predicate missing for %s \"%s\" in path.",
1250 lys_nodetype2str(schema->nodetype), schema->name);
1251 ret = LY_EINVAL;
1252 goto cleanup;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001253 } 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 +02001254 /* parse leafref value into a predicate, if not defined in the path */
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001255 p[LY_ARRAY_COUNT(p) - 1].pred_type = LY_PATH_PREDTYPE_LEAFLIST;
1256 LY_ARRAY_NEW_GOTO(ctx, p[LY_ARRAY_COUNT(p) - 1].predicates, pred, ret, cleanup);
Michal Vasko00cbf532020-06-15 13:58:47 +02001257
1258 if (!value) {
1259 value = "";
1260 }
1261
1262 r = LY_SUCCESS;
1263 if (options & LYD_NEWOPT_OPAQ) {
Michal Vaskof937cfe2020-08-03 16:07:12 +02001264 r = lys_value_validate(NULL, schema, value, strlen(value));
Michal Vasko00cbf532020-06-15 13:58:47 +02001265 }
1266 if (!r) {
1267 LY_CHECK_GOTO(ret = lyd_value_store(&pred->value, schema, value, strlen(value), NULL, lydjson_resolve_prefix,
1268 NULL, LYD_JSON), cleanup);
1269 } /* else we have opaq flag and the value is not valid, leavne no predicate and then create an opaque node */
1270 }
1271
1272 /* try to find any existing nodes in the path */
1273 if (parent) {
1274 ret = ly_path_eval_partial(p, parent, &path_idx, &node);
1275 if (ret == LY_SUCCESS) {
1276 /* the node exists, are we supposed to update it or is it just a default? */
1277 if (!(options & LYD_NEWOPT_UPDATE) && !(node->flags & LYD_DEFAULT)) {
1278 LOGERR(ctx, LY_EEXIST, "Path \"%s\" already exists", path);
1279 ret = LY_EEXIST;
1280 goto cleanup;
1281 }
1282
1283 /* update the existing node */
1284 ret = lyd_new_path_update(node, value, value_type, &nparent, &nnode);
1285 goto cleanup;
1286 } else if (ret == LY_EINCOMPLETE) {
1287 /* some nodes were found, adjust the iterator to the next segment */
1288 ++path_idx;
1289 } else if (ret == LY_ENOTFOUND) {
1290 /* 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 +02001291 if (lysc_data_parent(p[LY_ARRAY_COUNT(p) - 1].node)) {
Michal Vasko00cbf532020-06-15 13:58:47 +02001292 node = parent;
1293 }
1294 } else {
1295 /* error */
1296 goto cleanup;
1297 }
1298 }
1299
1300 /* create all the non-existing nodes in a loop */
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001301 for (; path_idx < LY_ARRAY_COUNT(p); ++path_idx) {
Michal Vasko00cbf532020-06-15 13:58:47 +02001302 cur_parent = node;
1303 schema = p[path_idx].node;
1304
1305 switch (schema->nodetype) {
1306 case LYS_LIST:
1307 if (!(schema->flags & LYS_KEYLESS)) {
1308 if ((options & LYD_NEWOPT_OPAQ) && (p[path_idx].pred_type == LY_PATH_PREDTYPE_NONE)) {
1309 /* creating opaque list without keys */
Radek Krejci1798aae2020-07-14 13:26:06 +02001310 LY_CHECK_GOTO(ret = lyd_create_opaq(ctx, schema->name, strlen(schema->name), NULL, 0, NULL, 0,
1311 LYD_JSON, NULL, NULL, 0, schema->module->name, strlen(schema->module->name), &node),
1312 cleanup);
Michal Vasko00cbf532020-06-15 13:58:47 +02001313 } else {
1314 assert(p[path_idx].pred_type == LY_PATH_PREDTYPE_LIST);
1315 LY_CHECK_GOTO(ret = lyd_create_list(schema, p[path_idx].predicates, &node), cleanup);
1316 }
1317 break;
1318 }
1319 /* fallthrough */
1320 case LYS_CONTAINER:
1321 case LYS_NOTIF:
1322 case LYS_RPC:
1323 case LYS_ACTION:
1324 LY_CHECK_GOTO(ret = lyd_create_inner(schema, &node), cleanup);
1325 break;
1326 case LYS_LEAFLIST:
1327 if ((options & LYD_NEWOPT_OPAQ) && (p[path_idx].pred_type == LY_PATH_PREDTYPE_NONE)) {
1328 /* creating opaque leaf-list without value */
Radek Krejci1798aae2020-07-14 13:26:06 +02001329 LY_CHECK_GOTO(ret = lyd_create_opaq(ctx, schema->name, strlen(schema->name), NULL, 0, NULL, 0,
1330 LYD_JSON, NULL, NULL, 0, schema->module->name, strlen(schema->module->name), &node),
1331 cleanup);
Michal Vasko00cbf532020-06-15 13:58:47 +02001332 } else {
1333 assert(p[path_idx].pred_type == LY_PATH_PREDTYPE_LEAFLIST);
1334 LY_CHECK_GOTO(ret = lyd_create_term2(schema, &p[path_idx].predicates[0].value, &node), cleanup);
1335 }
1336 break;
1337 case LYS_LEAF:
1338 /* make there is some value */
1339 if (!value) {
1340 value = "";
1341 }
1342
1343 r = LY_SUCCESS;
1344 if (options & LYD_NEWOPT_OPAQ) {
Michal Vaskof937cfe2020-08-03 16:07:12 +02001345 r = lys_value_validate(NULL, schema, value, strlen(value));
Michal Vasko00cbf532020-06-15 13:58:47 +02001346 }
1347 if (!r) {
Radek Krejci1798aae2020-07-14 13:26:06 +02001348 LY_CHECK_GOTO(ret = lyd_create_term(schema, value, strlen(value), NULL, 0, lydjson_resolve_prefix, NULL,
Michal Vasko00cbf532020-06-15 13:58:47 +02001349 LYD_JSON, &node), cleanup);
1350 } else {
1351 /* creating opaque leaf without value */
Radek Krejci1798aae2020-07-14 13:26:06 +02001352 LY_CHECK_GOTO(ret = lyd_create_opaq(ctx, schema->name, strlen(schema->name), NULL, 0, NULL, 0,
1353 LYD_JSON, NULL, NULL, 0, schema->module->name, strlen(schema->module->name), &node),
1354 cleanup);
Michal Vasko00cbf532020-06-15 13:58:47 +02001355 }
1356 break;
1357 case LYS_ANYDATA:
1358 case LYS_ANYXML:
1359 LY_CHECK_GOTO(ret = lyd_create_any(schema, value, value_type, &node), cleanup);
1360 break;
1361 default:
1362 LOGINT(ctx);
1363 ret = LY_EINT;
1364 goto cleanup;
1365 }
1366
1367 if (cur_parent) {
1368 /* connect to the parent */
1369 lyd_insert_node(cur_parent, NULL, node);
1370 } else if (parent) {
1371 /* connect to top-level siblings */
1372 lyd_insert_node(NULL, &parent, node);
1373 }
1374
1375 /* update remembered nodes */
1376 if (!nparent) {
1377 nparent = node;
1378 }
1379 nnode = node;
1380 }
1381
1382cleanup:
1383 lyxp_expr_free(ctx, exp);
1384 ly_path_free(ctx, p);
1385 if (!ret) {
1386 /* set out params only on success */
1387 if (new_parent) {
1388 *new_parent = nparent;
1389 }
1390 if (new_node) {
1391 *new_node = nnode;
1392 }
1393 }
1394 return ret;
1395}
1396
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001397LY_ERR
1398lyd_new_implicit_r(struct lyd_node *parent, struct lyd_node **first, const struct lysc_node *sparent,
1399 const struct lys_module *mod, struct ly_set *node_types, struct ly_set *node_when, int impl_opts,
1400 struct lyd_node **diff)
1401{
1402 LY_ERR ret;
1403 const struct lysc_node *iter = NULL;
1404 struct lyd_node *node;
1405 struct lyd_value **dflts;
1406 LY_ARRAY_COUNT_TYPE u;
1407
1408 assert(first && (parent || sparent || mod));
1409
1410 if (!sparent && parent) {
1411 sparent = parent->schema;
1412 }
1413
1414 while ((iter = lys_getnext(iter, sparent, mod ? mod->compiled : NULL, LYS_GETNEXT_WITHCHOICE))) {
1415 if ((impl_opts & LYD_IMPLICIT_NO_STATE) && (iter->flags & LYS_CONFIG_R)) {
1416 continue;
Michal Vasko44b19a12020-08-07 09:21:30 +02001417 } else if ((impl_opts & LYD_IMPLICIT_NO_CONFIG) && (iter->flags & LYS_CONFIG_W)) {
1418 continue;
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001419 }
1420
1421 switch (iter->nodetype) {
1422 case LYS_CHOICE:
1423 if (((struct lysc_node_choice *)iter)->dflt && !lys_getnext_data(NULL, *first, NULL, iter, NULL)) {
1424 /* create default case data */
1425 LY_CHECK_RET(lyd_new_implicit_r(parent, first, (struct lysc_node *)((struct lysc_node_choice *)iter)->dflt,
1426 NULL, node_types, node_when, impl_opts, diff));
1427 }
1428 break;
1429 case LYS_CONTAINER:
1430 if (!(iter->flags & LYS_PRESENCE) && lyd_find_sibling_val(*first, iter, NULL, 0, NULL)) {
1431 /* create default NP container */
1432 LY_CHECK_RET(lyd_create_inner(iter, &node));
1433 node->flags = LYD_DEFAULT;
1434 lyd_insert_node(parent, first, node);
1435
1436 /* cannot be a NP container with when */
1437 assert(!iter->when);
1438
1439 /* create any default children */
1440 LY_CHECK_RET(lyd_new_implicit_r(node, lyd_node_children_p(node), NULL, NULL, node_types, node_when,
1441 impl_opts, diff));
1442 }
1443 break;
1444 case LYS_LEAF:
1445 if (!(impl_opts & LYD_IMPLICIT_NO_DEFAULTS) && ((struct lysc_node_leaf *)iter)->dflt
1446 && lyd_find_sibling_val(*first, iter, NULL, 0, NULL)) {
1447 /* create default leaf */
1448 ret = lyd_create_term2(iter, ((struct lysc_node_leaf *)iter)->dflt, &node);
1449 if (ret == LY_EINCOMPLETE) {
1450 if (node_types) {
1451 /* remember to resolve type */
1452 ly_set_add(node_types, node, LY_SET_OPT_USEASLIST);
1453 }
1454 } else if (ret) {
1455 return ret;
1456 }
1457 node->flags = LYD_DEFAULT;
1458 lyd_insert_node(parent, first, node);
1459
1460 if (iter->when && node_when) {
1461 /* remember to resolve when */
1462 ly_set_add(node_when, node, LY_SET_OPT_USEASLIST);
1463 }
1464 if (diff) {
1465 /* add into diff */
1466 LY_CHECK_RET(lyd_val_diff_add(node, LYD_DIFF_OP_CREATE, diff));
1467 }
1468 }
1469 break;
1470 case LYS_LEAFLIST:
1471 if (!(impl_opts & LYD_IMPLICIT_NO_DEFAULTS) && ((struct lysc_node_leaflist *)iter)->dflts
1472 && lyd_find_sibling_val(*first, iter, NULL, 0, NULL)) {
1473 /* create all default leaf-lists */
1474 dflts = ((struct lysc_node_leaflist *)iter)->dflts;
1475 LY_ARRAY_FOR(dflts, u) {
1476 ret = lyd_create_term2(iter, dflts[u], &node);
1477 if (ret == LY_EINCOMPLETE) {
1478 if (node_types) {
1479 /* remember to resolve type */
1480 ly_set_add(node_types, node, LY_SET_OPT_USEASLIST);
1481 }
1482 } else if (ret) {
1483 return ret;
1484 }
1485 node->flags = LYD_DEFAULT;
1486 lyd_insert_node(parent, first, node);
1487
1488 if (iter->when && node_when) {
1489 /* remember to resolve when */
1490 ly_set_add(node_when, node, LY_SET_OPT_USEASLIST);
1491 }
1492 if (diff) {
1493 /* add into diff */
1494 LY_CHECK_RET(lyd_val_diff_add(node, LYD_DIFF_OP_CREATE, diff));
1495 }
1496 }
1497 }
1498 break;
1499 default:
1500 /* without defaults */
1501 break;
1502 }
1503 }
1504
1505 return LY_SUCCESS;
1506}
1507
1508API LY_ERR
1509lyd_new_implicit_tree(struct lyd_node *tree, int implicit_options, struct lyd_node **diff)
1510{
Michal Vasko56daf732020-08-10 10:57:18 +02001511 struct lyd_node *node;
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001512 LY_ERR ret = LY_SUCCESS;
1513
1514 LY_CHECK_ARG_RET(NULL, tree, LY_EINVAL);
1515 if (diff) {
1516 *diff = NULL;
1517 }
1518
Michal Vasko56daf732020-08-10 10:57:18 +02001519 LYD_TREE_DFS_BEGIN(tree, node) {
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001520 /* skip added default nodes */
1521 if (((node->flags & (LYD_DEFAULT | LYD_NEW)) != (LYD_DEFAULT | LYD_NEW))
1522 && (node->schema->nodetype & LYD_NODE_INNER)) {
1523 LY_CHECK_GOTO(ret = lyd_new_implicit_r(node, lyd_node_children_p((struct lyd_node *)node), NULL, NULL, NULL,
1524 NULL, implicit_options, diff), cleanup);
1525 }
1526
Michal Vasko56daf732020-08-10 10:57:18 +02001527 LYD_TREE_DFS_END(tree, node);
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001528 }
1529
1530cleanup:
1531 if (ret && diff) {
1532 lyd_free_all(*diff);
1533 *diff = NULL;
1534 }
1535 return ret;
1536}
1537
1538API LY_ERR
1539lyd_new_implicit_all(struct lyd_node **tree, const struct ly_ctx *ctx, int implicit_options, struct lyd_node **diff)
1540{
1541 const struct lys_module *mod;
1542 struct lyd_node *d = NULL;
1543 uint32_t i = 0;
1544 LY_ERR ret = LY_SUCCESS;
1545
1546 LY_CHECK_ARG_RET(ctx, tree, *tree || ctx, LY_EINVAL);
1547 if (diff) {
1548 *diff = NULL;
1549 }
1550 if (!ctx) {
1551 ctx = LYD_NODE_CTX(*tree);
1552 }
1553
1554 /* add nodes for each module one-by-one */
1555 while ((mod = ly_ctx_get_module_iter(ctx, &i))) {
1556 if (!mod->implemented) {
1557 continue;
1558 }
1559
1560 LY_CHECK_GOTO(ret = lyd_new_implicit_module(tree, mod, implicit_options, diff ? &d : NULL), cleanup);
1561 if (d) {
1562 /* merge into one diff */
1563 lyd_insert_sibling(*diff, d, diff);
1564
1565 d = NULL;
1566 }
1567 }
1568
1569cleanup:
1570 if (ret && diff) {
1571 lyd_free_all(*diff);
1572 *diff = NULL;
1573 }
1574 return ret;
1575}
1576
1577API LY_ERR
1578lyd_new_implicit_module(struct lyd_node **tree, const struct lys_module *module, int implicit_options, struct lyd_node **diff)
1579{
1580 struct lyd_node *root, *d = NULL;
1581 LY_ERR ret = LY_SUCCESS;
1582
1583 LY_CHECK_ARG_RET(NULL, tree, module, LY_EINVAL);
1584 if (diff) {
1585 *diff = NULL;
1586 }
1587
1588 /* add all top-level defaults for this module */
1589 LY_CHECK_GOTO(ret = lyd_new_implicit_r(NULL, tree, NULL, module, NULL, NULL, implicit_options, diff), cleanup);
1590
1591 /* process nested nodes */
1592 LY_LIST_FOR(*tree, root) {
1593 /* skip added default nodes */
1594 if ((root->flags & (LYD_DEFAULT | LYD_NEW)) != (LYD_DEFAULT | LYD_NEW)) {
1595 LY_CHECK_GOTO(ret = lyd_new_implicit_tree(root, implicit_options, diff ? &d : NULL), cleanup);
1596
1597 if (d) {
1598 /* merge into one diff */
1599 lyd_insert_sibling(*diff, d, diff);
1600
1601 d = NULL;
1602 }
1603 }
1604 }
1605
1606cleanup:
1607 if (ret && diff) {
1608 lyd_free_all(*diff);
1609 *diff = NULL;
1610 }
1611 return ret;
1612}
1613
Michal Vasko90932a92020-02-12 14:33:03 +01001614struct lyd_node *
Michal Vaskob104f112020-07-17 09:54:54 +02001615lyd_insert_get_next_anchor(const struct lyd_node *first_sibling, const struct lyd_node *new_node)
Michal Vasko90932a92020-02-12 14:33:03 +01001616{
Michal Vaskob104f112020-07-17 09:54:54 +02001617 const struct lysc_node *schema, *sparent;
Michal Vasko90932a92020-02-12 14:33:03 +01001618 struct lyd_node *match = NULL;
Michal Vaskob104f112020-07-17 09:54:54 +02001619 int found;
Michal Vasko90932a92020-02-12 14:33:03 +01001620
Michal Vaskob104f112020-07-17 09:54:54 +02001621 assert(new_node);
1622
1623 if (!first_sibling || !new_node->schema) {
1624 /* insert at the end, no next anchor */
Michal Vasko90932a92020-02-12 14:33:03 +01001625 return NULL;
1626 }
1627
Michal Vaskob104f112020-07-17 09:54:54 +02001628 if (first_sibling->parent && first_sibling->parent->children_ht) {
1629 /* find the anchor using hashes */
1630 sparent = first_sibling->parent->schema;
1631 schema = lys_getnext(new_node->schema, sparent, NULL, 0);
1632 while (schema) {
1633 /* keep trying to find the first existing instance of the closest following schema sibling,
1634 * otherwise return NULL - inserting at the end */
1635 if (!lyd_find_sibling_schema(first_sibling, schema, &match)) {
1636 break;
1637 }
1638
1639 schema = lys_getnext(schema, sparent, NULL, 0);
1640 }
1641 } else {
1642 /* find the anchor without hashes */
1643 match = (struct lyd_node *)first_sibling;
1644 if (!lysc_data_parent(new_node->schema)) {
1645 /* we are in top-level, skip all the data from preceding modules */
1646 LY_LIST_FOR(match, match) {
1647 if (!match->schema || (strcmp(lyd_owner_module(match)->name, lyd_owner_module(new_node)->name) >= 0)) {
1648 break;
1649 }
1650 }
1651 }
1652
1653 /* get the first schema sibling */
1654 sparent = lysc_data_parent(new_node->schema);
1655 schema = lys_getnext(NULL, sparent, new_node->schema->module->compiled, 0);
1656
1657 found = 0;
1658 LY_LIST_FOR(match, match) {
1659 if (!match->schema || (lyd_owner_module(match) != lyd_owner_module(new_node))) {
1660 /* we have found an opaque node, which must be at the end, so use it OR
1661 * modules do not match, so we must have traversed all the data from new_node module (if any),
1662 * we have found the first node of the next module, that is what we want */
1663 break;
1664 }
1665
1666 /* skip schema nodes until we find the instantiated one */
1667 while (!found) {
1668 if (new_node->schema == schema) {
1669 /* we have found the schema of the new node, continue search to find the first
1670 * data node with a different schema (after our schema) */
1671 found = 1;
1672 break;
1673 }
1674 if (match->schema == schema) {
1675 /* current node (match) is a data node still before the new node, continue search in data */
1676 break;
1677 }
1678 schema = lys_getnext(schema, sparent, new_node->schema->module->compiled, 0);
1679 assert(schema);
1680 }
1681
1682 if (found && (match->schema != new_node->schema)) {
1683 /* find the next node after we have found our node schema data instance */
1684 break;
1685 }
1686 }
Michal Vasko90932a92020-02-12 14:33:03 +01001687 }
1688
1689 return match;
1690}
1691
1692/**
1693 * @brief Insert node after a sibling.
1694 *
Michal Vasko9f96a052020-03-10 09:41:45 +01001695 * Handles inserting into NP containers and key-less lists.
1696 *
Michal Vasko90932a92020-02-12 14:33:03 +01001697 * @param[in] sibling Sibling to insert after.
1698 * @param[in] node Node to insert.
1699 */
1700static void
Michal Vaskof03ed032020-03-04 13:31:44 +01001701lyd_insert_after_node(struct lyd_node *sibling, struct lyd_node *node)
Michal Vasko90932a92020-02-12 14:33:03 +01001702{
Michal Vasko0249f7c2020-03-05 16:36:40 +01001703 struct lyd_node_inner *par;
1704
Michal Vasko90932a92020-02-12 14:33:03 +01001705 assert(!node->next && (node->prev == node));
1706
1707 node->next = sibling->next;
1708 node->prev = sibling;
1709 sibling->next = node;
1710 if (node->next) {
1711 /* sibling had a succeeding node */
1712 node->next->prev = node;
1713 } else {
1714 /* sibling was last, find first sibling and change its prev */
1715 if (sibling->parent) {
1716 sibling = sibling->parent->child;
1717 } else {
1718 for (; sibling->prev->next != node; sibling = sibling->prev);
1719 }
1720 sibling->prev = node;
1721 }
1722 node->parent = sibling->parent;
Michal Vasko0249f7c2020-03-05 16:36:40 +01001723
Michal Vasko9f96a052020-03-10 09:41:45 +01001724 for (par = node->parent; par; par = par->parent) {
1725 if ((par->flags & LYD_DEFAULT) && !(node->flags & LYD_DEFAULT)) {
1726 /* remove default flags from NP containers */
Michal Vasko0249f7c2020-03-05 16:36:40 +01001727 par->flags &= ~LYD_DEFAULT;
1728 }
Michal Vaskob104f112020-07-17 09:54:54 +02001729 if (par->schema && (par->schema->nodetype == LYS_LIST) && (par->schema->flags & LYS_KEYLESS)) {
Michal Vasko9f96a052020-03-10 09:41:45 +01001730 /* rehash key-less list */
1731 lyd_hash((struct lyd_node *)par);
1732 }
Michal Vasko0249f7c2020-03-05 16:36:40 +01001733 }
Michal Vasko90932a92020-02-12 14:33:03 +01001734}
1735
1736/**
1737 * @brief Insert node before a sibling.
1738 *
Michal Vasko9f96a052020-03-10 09:41:45 +01001739 * Handles inserting into NP containers and key-less lists.
1740 *
Michal Vasko90932a92020-02-12 14:33:03 +01001741 * @param[in] sibling Sibling to insert before.
1742 * @param[in] node Node to insert.
1743 */
1744static void
Michal Vaskof03ed032020-03-04 13:31:44 +01001745lyd_insert_before_node(struct lyd_node *sibling, struct lyd_node *node)
Michal Vasko90932a92020-02-12 14:33:03 +01001746{
Michal Vasko0249f7c2020-03-05 16:36:40 +01001747 struct lyd_node_inner *par;
1748
Michal Vasko90932a92020-02-12 14:33:03 +01001749 assert(!node->next && (node->prev == node));
1750
1751 node->next = sibling;
1752 /* covers situation of sibling being first */
1753 node->prev = sibling->prev;
1754 sibling->prev = node;
1755 if (node->prev->next) {
1756 /* sibling had a preceding node */
1757 node->prev->next = node;
1758 } else if (sibling->parent) {
1759 /* sibling was first and we must also change parent child pointer */
1760 sibling->parent->child = node;
1761 }
1762 node->parent = sibling->parent;
Michal Vasko0249f7c2020-03-05 16:36:40 +01001763
Michal Vasko9f96a052020-03-10 09:41:45 +01001764 for (par = node->parent; par; par = par->parent) {
1765 if ((par->flags & LYD_DEFAULT) && !(node->flags & LYD_DEFAULT)) {
1766 /* remove default flags from NP containers */
Michal Vasko0249f7c2020-03-05 16:36:40 +01001767 par->flags &= ~LYD_DEFAULT;
1768 }
Michal Vaskob104f112020-07-17 09:54:54 +02001769 if (par->schema && (par->schema->nodetype == LYS_LIST) && (par->schema->flags & LYS_KEYLESS)) {
Michal Vasko9f96a052020-03-10 09:41:45 +01001770 /* rehash key-less list */
1771 lyd_hash((struct lyd_node *)par);
1772 }
Michal Vasko0249f7c2020-03-05 16:36:40 +01001773 }
Michal Vasko90932a92020-02-12 14:33:03 +01001774}
1775
1776/**
Michal Vaskob104f112020-07-17 09:54:54 +02001777 * @brief Insert node as the first and only child of a parent.
Michal Vasko90932a92020-02-12 14:33:03 +01001778 *
Michal Vasko9f96a052020-03-10 09:41:45 +01001779 * Handles inserting into NP containers and key-less lists.
1780 *
Michal Vasko90932a92020-02-12 14:33:03 +01001781 * @param[in] parent Parent to insert into.
1782 * @param[in] node Node to insert.
1783 */
1784static void
Michal Vaskob104f112020-07-17 09:54:54 +02001785lyd_insert_only_child(struct lyd_node *parent, struct lyd_node *node)
Michal Vasko90932a92020-02-12 14:33:03 +01001786{
1787 struct lyd_node_inner *par;
1788
Michal Vaskob104f112020-07-17 09:54:54 +02001789 assert(parent && !lyd_node_children(parent, 0) && !node->next && (node->prev == node));
Michal Vasko52927e22020-03-16 17:26:14 +01001790 assert(!parent->schema || (parent->schema->nodetype & LYD_NODE_INNER));
Michal Vasko90932a92020-02-12 14:33:03 +01001791
1792 par = (struct lyd_node_inner *)parent;
1793
Michal Vaskob104f112020-07-17 09:54:54 +02001794 par->child = node;
Michal Vasko90932a92020-02-12 14:33:03 +01001795 node->parent = par;
Michal Vasko0249f7c2020-03-05 16:36:40 +01001796
Michal Vasko9f96a052020-03-10 09:41:45 +01001797 for (; par; par = par->parent) {
1798 if ((par->flags & LYD_DEFAULT) && !(node->flags & LYD_DEFAULT)) {
1799 /* remove default flags from NP containers */
Michal Vasko0249f7c2020-03-05 16:36:40 +01001800 par->flags &= ~LYD_DEFAULT;
1801 }
Michal Vasko52927e22020-03-16 17:26:14 +01001802 if (par->schema && (par->schema->nodetype == LYS_LIST) && (par->schema->flags & LYS_KEYLESS)) {
Michal Vasko9f96a052020-03-10 09:41:45 +01001803 /* rehash key-less list */
1804 lyd_hash((struct lyd_node *)par);
1805 }
Michal Vasko0249f7c2020-03-05 16:36:40 +01001806 }
Michal Vasko751cb4d2020-07-14 12:25:28 +02001807}
Michal Vasko0249f7c2020-03-05 16:36:40 +01001808
Michal Vasko751cb4d2020-07-14 12:25:28 +02001809/**
1810 * @brief Learn whether a list instance has all the keys.
1811 *
1812 * @param[in] list List instance to check.
1813 * @return non-zero if all the keys were found,
1814 * @return 0 otherwise.
1815 */
1816static int
1817lyd_insert_has_keys(const struct lyd_node *list)
1818{
1819 const struct lyd_node *key;
1820 const struct lysc_node *skey = NULL;
1821
1822 assert(list->schema->nodetype == LYS_LIST);
1823 key = lyd_node_children(list, 0);
1824 while ((skey = lys_getnext(skey, list->schema, NULL, 0)) && (skey->flags & LYS_KEY)) {
1825 if (!key || (key->schema != skey)) {
1826 /* key missing */
1827 return 0;
1828 }
1829
1830 key = key->next;
1831 }
1832
1833 /* all keys found */
1834 return 1;
Michal Vasko90932a92020-02-12 14:33:03 +01001835}
1836
1837void
Michal Vaskob104f112020-07-17 09:54:54 +02001838lyd_insert_node(struct lyd_node *parent, struct lyd_node **first_sibling_p, struct lyd_node *node)
Michal Vasko90932a92020-02-12 14:33:03 +01001839{
Michal Vaskob104f112020-07-17 09:54:54 +02001840 struct lyd_node *anchor, *first_sibling;
Michal Vasko90932a92020-02-12 14:33:03 +01001841
Michal Vaskob104f112020-07-17 09:54:54 +02001842 /* inserting list without its keys is not supported */
1843 assert((parent || first_sibling_p) && node && (node->hash || !node->schema));
Michal Vasko9b368d32020-02-14 13:53:31 +01001844
Michal Vaskob104f112020-07-17 09:54:54 +02001845 if (!parent && first_sibling_p && (*first_sibling_p) && (*first_sibling_p)->parent) {
1846 parent = (struct lyd_node *)(*first_sibling_p)->parent;
Michal Vasko9b368d32020-02-14 13:53:31 +01001847 }
Michal Vasko90932a92020-02-12 14:33:03 +01001848
Michal Vaskob104f112020-07-17 09:54:54 +02001849 /* get first sibling */
1850 first_sibling = parent ? ((struct lyd_node_inner *)parent)->child : *first_sibling_p;
Michal Vasko9f96a052020-03-10 09:41:45 +01001851
Michal Vaskob104f112020-07-17 09:54:54 +02001852 /* find the anchor, our next node, so we can insert before it */
1853 anchor = lyd_insert_get_next_anchor(first_sibling, node);
1854 if (anchor) {
1855 lyd_insert_before_node(anchor, node);
1856 } else if (first_sibling) {
1857 lyd_insert_after_node(first_sibling->prev, node);
1858 } else if (parent) {
1859 lyd_insert_only_child(parent, node);
Michal Vasko90932a92020-02-12 14:33:03 +01001860 } else {
Michal Vaskob104f112020-07-17 09:54:54 +02001861 *first_sibling_p = node;
1862 }
1863
1864 /* insert into parent HT */
1865 lyd_insert_hash(node);
1866
1867 /* finish hashes for our parent, if needed and possible */
1868 if (node->schema && (node->schema->flags & LYS_KEY) && lyd_insert_has_keys(parent)) {
1869 lyd_hash(parent);
1870
1871 /* now we can insert even the list into its parent HT */
1872 lyd_insert_hash(parent);
Michal Vasko90932a92020-02-12 14:33:03 +01001873 }
Michal Vasko90932a92020-02-12 14:33:03 +01001874}
1875
Michal Vaskof03ed032020-03-04 13:31:44 +01001876static LY_ERR
1877lyd_insert_check_schema(const struct lysc_node *parent, const struct lysc_node *schema)
1878{
1879 const struct lysc_node *par2;
1880
1881 assert(schema);
Michal Vasko62ed12d2020-05-21 10:08:25 +02001882 assert(!parent || !(parent->nodetype & (LYS_CASE | LYS_CHOICE)));
Michal Vaskof03ed032020-03-04 13:31:44 +01001883
1884 /* find schema parent */
Michal Vasko62ed12d2020-05-21 10:08:25 +02001885 par2 = lysc_data_parent(schema);
Michal Vaskof03ed032020-03-04 13:31:44 +01001886
1887 if (parent) {
1888 /* inner node */
1889 if (par2 != parent) {
Michal Vaskob104f112020-07-17 09:54:54 +02001890 LOGERR(schema->module->ctx, LY_EINVAL, "Cannot insert, parent of \"%s\" is not \"%s\".", schema->name,
1891 parent->name);
Michal Vaskof03ed032020-03-04 13:31:44 +01001892 return LY_EINVAL;
1893 }
1894 } else {
1895 /* top-level node */
1896 if (par2) {
Radek Krejcif6d14cb2020-07-02 16:11:45 +02001897 LOGERR(schema->module->ctx, LY_EINVAL, "Cannot insert, node \"%s\" is not top-level.", schema->name);
Michal Vaskof03ed032020-03-04 13:31:44 +01001898 return LY_EINVAL;
1899 }
1900 }
1901
1902 return LY_SUCCESS;
1903}
1904
1905API LY_ERR
Michal Vaskob104f112020-07-17 09:54:54 +02001906lyd_insert_child(struct lyd_node *parent, struct lyd_node *node)
Michal Vaskof03ed032020-03-04 13:31:44 +01001907{
1908 struct lyd_node *iter;
1909
Michal Vasko654bc852020-06-23 13:28:06 +02001910 LY_CHECK_ARG_RET(NULL, parent, node, parent->schema->nodetype & LYD_NODE_INNER, LY_EINVAL);
Michal Vaskof03ed032020-03-04 13:31:44 +01001911
1912 LY_CHECK_RET(lyd_insert_check_schema(parent->schema, node->schema));
1913
1914 if (node->schema->flags & LYS_KEY) {
1915 LOGERR(parent->schema->module->ctx, LY_EINVAL, "Cannot insert key \"%s\".", node->schema->name);
1916 return LY_EINVAL;
1917 }
1918
1919 if (node->parent || node->prev->next) {
1920 lyd_unlink_tree(node);
1921 }
1922
1923 while (node) {
1924 iter = node->next;
1925 lyd_unlink_tree(node);
1926 lyd_insert_node(parent, NULL, node);
1927 node = iter;
1928 }
1929 return LY_SUCCESS;
1930}
1931
1932API LY_ERR
Michal Vaskob104f112020-07-17 09:54:54 +02001933lyd_insert_sibling(struct lyd_node *sibling, struct lyd_node *node, struct lyd_node **first)
Michal Vaskob1b5c262020-03-05 14:29:47 +01001934{
1935 struct lyd_node *iter;
1936
Michal Vaskob104f112020-07-17 09:54:54 +02001937 LY_CHECK_ARG_RET(NULL, node, LY_EINVAL);
Michal Vaskob1b5c262020-03-05 14:29:47 +01001938
Michal Vaskob104f112020-07-17 09:54:54 +02001939 if (sibling) {
1940 LY_CHECK_RET(lyd_insert_check_schema(lysc_data_parent(sibling->schema), node->schema));
Michal Vaskob1b5c262020-03-05 14:29:47 +01001941 }
1942
1943 if (node->parent || node->prev->next) {
1944 lyd_unlink_tree(node);
1945 }
1946
1947 while (node) {
Michal Vaskob104f112020-07-17 09:54:54 +02001948 if (node->schema->flags & LYS_KEY) {
1949 LOGERR(LYD_NODE_CTX(node), LY_EINVAL, "Cannot insert key \"%s\".", node->schema->name);
1950 return LY_EINVAL;
1951 }
1952
Michal Vaskob1b5c262020-03-05 14:29:47 +01001953 iter = node->next;
1954 lyd_unlink_tree(node);
1955 lyd_insert_node(NULL, &sibling, node);
1956 node = iter;
1957 }
Michal Vaskob1b5c262020-03-05 14:29:47 +01001958
Michal Vaskob104f112020-07-17 09:54:54 +02001959 if (first) {
1960 /* find the first sibling */
1961 *first = sibling;
1962 while ((*first)->prev->next) {
1963 *first = (*first)->prev;
Michal Vasko0249f7c2020-03-05 16:36:40 +01001964 }
1965 }
1966
1967 return LY_SUCCESS;
1968}
1969
Michal Vaskob1b5c262020-03-05 14:29:47 +01001970API LY_ERR
Michal Vaskof03ed032020-03-04 13:31:44 +01001971lyd_insert_before(struct lyd_node *sibling, struct lyd_node *node)
1972{
1973 struct lyd_node *iter;
1974
1975 LY_CHECK_ARG_RET(NULL, sibling, node, LY_EINVAL);
1976
Michal Vasko62ed12d2020-05-21 10:08:25 +02001977 LY_CHECK_RET(lyd_insert_check_schema(lysc_data_parent(sibling->schema), node->schema));
Michal Vaskof03ed032020-03-04 13:31:44 +01001978
Michal Vaskob104f112020-07-17 09:54:54 +02001979 if (!(node->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) || !(node->schema->flags & LYS_ORDBY_USER)) {
1980 LOGERR(LYD_NODE_CTX(sibling), LY_EINVAL, "Can be used only for user-ordered nodes.");
Michal Vaskof03ed032020-03-04 13:31:44 +01001981 return LY_EINVAL;
1982 }
1983
1984 if (node->parent || node->prev->next) {
1985 lyd_unlink_tree(node);
1986 }
1987
1988 /* insert in reverse order to get the original order */
1989 node = node->prev;
1990 while (node) {
1991 iter = node->prev;
1992 lyd_unlink_tree(node);
1993
1994 lyd_insert_before_node(sibling, node);
Michal Vasko751cb4d2020-07-14 12:25:28 +02001995 lyd_insert_hash(node);
1996
Michal Vaskof03ed032020-03-04 13:31:44 +01001997 /* move the anchor accordingly */
1998 sibling = node;
1999
2000 node = (iter == node) ? NULL : iter;
2001 }
2002 return LY_SUCCESS;
2003}
2004
2005API LY_ERR
2006lyd_insert_after(struct lyd_node *sibling, struct lyd_node *node)
2007{
2008 struct lyd_node *iter;
2009
2010 LY_CHECK_ARG_RET(NULL, sibling, node, LY_EINVAL);
2011
Michal Vasko62ed12d2020-05-21 10:08:25 +02002012 LY_CHECK_RET(lyd_insert_check_schema(lysc_data_parent(sibling->schema), node->schema));
Michal Vaskof03ed032020-03-04 13:31:44 +01002013
Michal Vaskob104f112020-07-17 09:54:54 +02002014 if (!(node->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) || !(node->schema->flags & LYS_ORDBY_USER)) {
2015 LOGERR(LYD_NODE_CTX(sibling), LY_EINVAL, "Can be used only for user-ordered nodes.");
Michal Vaskof03ed032020-03-04 13:31:44 +01002016 return LY_EINVAL;
2017 }
2018
2019 if (node->parent || node->prev->next) {
2020 lyd_unlink_tree(node);
2021 }
2022
2023 while (node) {
2024 iter = node->next;
2025 lyd_unlink_tree(node);
2026
2027 lyd_insert_after_node(sibling, node);
Michal Vasko751cb4d2020-07-14 12:25:28 +02002028 lyd_insert_hash(node);
2029
Michal Vaskof03ed032020-03-04 13:31:44 +01002030 /* move the anchor accordingly */
2031 sibling = node;
2032
2033 node = iter;
2034 }
2035 return LY_SUCCESS;
2036}
2037
2038API void
2039lyd_unlink_tree(struct lyd_node *node)
2040{
2041 struct lyd_node *iter;
2042
2043 if (!node) {
2044 return;
2045 }
2046
Michal Vaskob104f112020-07-17 09:54:54 +02002047 /* update hashes while still linked into the tree */
2048 lyd_unlink_hash(node);
2049
Michal Vaskof03ed032020-03-04 13:31:44 +01002050 /* unlink from siblings */
2051 if (node->prev->next) {
2052 node->prev->next = node->next;
2053 }
2054 if (node->next) {
2055 node->next->prev = node->prev;
2056 } else {
2057 /* unlinking the last node */
2058 if (node->parent) {
2059 iter = node->parent->child;
2060 } else {
2061 iter = node->prev;
2062 while (iter->prev != node) {
2063 iter = iter->prev;
2064 }
2065 }
2066 /* update the "last" pointer from the first node */
2067 iter->prev = node->prev;
2068 }
2069
2070 /* unlink from parent */
2071 if (node->parent) {
2072 if (node->parent->child == node) {
2073 /* the node is the first child */
2074 node->parent->child = node->next;
2075 }
2076
Michal Vaskoab49dbe2020-07-17 12:32:47 +02002077 /* check for NP container whether its last non-default node is not being unlinked */
2078 if (node->parent->schema && (node->parent->schema->nodetype == LYS_CONTAINER)
2079 && !(node->parent->flags & LYD_DEFAULT) && !(node->parent->schema->flags & LYS_PRESENCE)) {
2080 LY_LIST_FOR(node->parent->child, iter) {
2081 if ((iter != node) && !(iter->flags & LYD_DEFAULT)) {
2082 break;
2083 }
2084 }
2085 if (!iter) {
2086 node->parent->flags |= LYD_DEFAULT;
2087 }
2088 }
2089
Michal Vaskof03ed032020-03-04 13:31:44 +01002090 /* check for keyless list and update its hash */
2091 for (iter = (struct lyd_node *)node->parent; iter; iter = (struct lyd_node *)iter->parent) {
Michal Vasko413c7f22020-05-05 12:34:06 +02002092 if (iter->schema && (iter->schema->flags & LYS_KEYLESS)) {
Michal Vaskof03ed032020-03-04 13:31:44 +01002093 lyd_hash(iter);
2094 }
2095 }
2096
2097 node->parent = NULL;
2098 }
2099
2100 node->next = NULL;
2101 node->prev = node;
2102}
2103
Michal Vaskoa5da3292020-08-12 13:10:50 +02002104void
Radek Krejci1798aae2020-07-14 13:26:06 +02002105lyd_insert_meta(struct lyd_node *parent, struct lyd_meta *meta)
2106{
2107 struct lyd_meta *last, *iter;
2108
2109 assert(parent);
Michal Vaskoa5da3292020-08-12 13:10:50 +02002110
2111 if (!meta) {
2112 return;
2113 }
Radek Krejci1798aae2020-07-14 13:26:06 +02002114
2115 for (iter = meta; iter; iter = iter->next) {
2116 iter->parent = parent;
2117 }
2118
2119 /* insert as the last attribute */
2120 if (parent->meta) {
2121 for (last = parent->meta; last->next; last = last->next);
2122 last->next = meta;
2123 } else {
2124 parent->meta = meta;
2125 }
2126
2127 /* remove default flags from NP containers */
2128 while (parent && (parent->schema->nodetype == LYS_CONTAINER) && (parent->flags & LYD_DEFAULT)) {
2129 parent->flags &= ~LYD_DEFAULT;
2130 parent = (struct lyd_node *)parent->parent;
2131 }
Radek Krejci1798aae2020-07-14 13:26:06 +02002132}
2133
2134LY_ERR
Michal Vasko9f96a052020-03-10 09:41:45 +01002135lyd_create_meta(struct lyd_node *parent, struct lyd_meta **meta, const struct lys_module *mod, const char *name,
Radek Krejci1798aae2020-07-14 13:26:06 +02002136 size_t name_len, const char *value, size_t value_len, int *dynamic, int value_hint, ly_resolve_prefix_clb resolve_prefix,
Michal Vasko8d544252020-03-02 10:19:52 +01002137 void *prefix_data, LYD_FORMAT format, const struct lysc_node *ctx_snode)
Michal Vasko90932a92020-02-12 14:33:03 +01002138{
2139 LY_ERR ret;
2140 struct lysc_ext_instance *ant = NULL;
Michal Vasko9f96a052020-03-10 09:41:45 +01002141 struct lyd_meta *mt, *last;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002142 LY_ARRAY_COUNT_TYPE u;
Michal Vasko90932a92020-02-12 14:33:03 +01002143
Michal Vasko9f96a052020-03-10 09:41:45 +01002144 assert((parent || meta) && mod);
Michal Vasko6f4cbb62020-02-28 11:15:47 +01002145
Radek Krejci7eb54ba2020-05-18 16:30:04 +02002146 LY_ARRAY_FOR(mod->compiled->exts, u) {
2147 if (mod->compiled->exts[u].def->plugin == lyext_plugins_internal[LYEXT_PLUGIN_INTERNAL_ANNOTATION].plugin &&
2148 !ly_strncmp(mod->compiled->exts[u].argument, name, name_len)) {
Michal Vasko90932a92020-02-12 14:33:03 +01002149 /* we have the annotation definition */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02002150 ant = &mod->compiled->exts[u];
Michal Vasko90932a92020-02-12 14:33:03 +01002151 break;
2152 }
2153 }
2154 if (!ant) {
2155 /* attribute is not defined as a metadata annotation (RFC 7952) */
2156 LOGERR(mod->ctx, LY_EINVAL, "Annotation definition for attribute \"%s:%.*s\" not found.",
2157 mod->name, name_len, name);
2158 return LY_EINVAL;
2159 }
2160
Michal Vasko9f96a052020-03-10 09:41:45 +01002161 mt = calloc(1, sizeof *mt);
2162 LY_CHECK_ERR_RET(!mt, LOGMEM(mod->ctx), LY_EMEM);
2163 mt->parent = parent;
2164 mt->annotation = ant;
Radek Krejci1798aae2020-07-14 13:26:06 +02002165 ret = lyd_value_parse_meta(mod->ctx, mt, value, value_len, dynamic, 0, value_hint, resolve_prefix, prefix_data, format, ctx_snode, NULL);
Michal Vasko90932a92020-02-12 14:33:03 +01002166 if ((ret != LY_SUCCESS) && (ret != LY_EINCOMPLETE)) {
Michal Vasko9f96a052020-03-10 09:41:45 +01002167 free(mt);
Michal Vasko90932a92020-02-12 14:33:03 +01002168 return ret;
2169 }
Michal Vasko9f96a052020-03-10 09:41:45 +01002170 mt->name = lydict_insert(mod->ctx, name, name_len);
Michal Vasko90932a92020-02-12 14:33:03 +01002171
Michal Vasko6f4cbb62020-02-28 11:15:47 +01002172 /* insert as the last attribute */
2173 if (parent) {
Michal Vaskoa5da3292020-08-12 13:10:50 +02002174 lyd_insert_meta(parent, mt);
Michal Vasko9f96a052020-03-10 09:41:45 +01002175 } else if (*meta) {
2176 for (last = *meta; last->next; last = last->next);
2177 last->next = mt;
Michal Vasko90932a92020-02-12 14:33:03 +01002178 }
2179
Michal Vasko9f96a052020-03-10 09:41:45 +01002180 if (meta) {
2181 *meta = mt;
Michal Vasko90932a92020-02-12 14:33:03 +01002182 }
2183 return ret;
2184}
2185
Michal Vaskoa5da3292020-08-12 13:10:50 +02002186void
2187lyd_insert_attr(struct lyd_node *parent, struct lyd_attr *attr)
2188{
2189 struct lyd_attr *last, *iter;
2190 struct lyd_node_opaq *opaq;
2191
2192 assert(parent && !parent->schema);
2193
2194 if (!attr) {
2195 return;
2196 }
2197
2198 opaq = (struct lyd_node_opaq *)parent;
2199 for (iter = attr; iter; iter = iter->next) {
2200 iter->parent = opaq;
2201 }
2202
2203 /* insert as the last attribute */
2204 if (opaq->attr) {
2205 for (last = opaq->attr; last->next; last = last->next);
2206 last->next = attr;
2207 } else {
2208 opaq->attr = attr;
2209 }
2210}
2211
Michal Vasko52927e22020-03-16 17:26:14 +01002212LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +02002213lyd_create_attr(struct lyd_node *parent, struct lyd_attr **attr, const struct ly_ctx *ctx, const char *name,
2214 size_t name_len, const char *value, size_t value_len, int *dynamic, int value_hint, LYD_FORMAT format,
2215 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 +01002216{
Radek Krejci1798aae2020-07-14 13:26:06 +02002217 struct lyd_attr *at, *last;
Michal Vasko52927e22020-03-16 17:26:14 +01002218
2219 assert(ctx && (parent || attr) && (!parent || !parent->schema));
2220 assert(name && name_len);
Michal Vasko52927e22020-03-16 17:26:14 +01002221
2222 if (!value_len) {
2223 value = "";
2224 }
2225
2226 at = calloc(1, sizeof *at);
2227 LY_CHECK_ERR_RET(!at, LOGMEM(ctx), LY_EMEM);
2228 at->parent = (struct lyd_node_opaq *)parent;
2229 at->name = lydict_insert(ctx, name, name_len);
2230 if (dynamic && *dynamic) {
2231 at->value = lydict_insert_zc(ctx, (char *)value);
2232 *dynamic = 0;
2233 } else {
2234 at->value = lydict_insert(ctx, value, value_len);
2235 }
2236
Radek Krejci1798aae2020-07-14 13:26:06 +02002237 at->hint = value_hint;
Michal Vasko52927e22020-03-16 17:26:14 +01002238 at->format = format;
2239 at->val_prefs = val_prefs;
Radek Krejci1798aae2020-07-14 13:26:06 +02002240 if (prefix_len) {
2241 at->prefix.id = lydict_insert(ctx, prefix, prefix_len);
2242 }
2243 if (module_key_len) {
2244 at->prefix.module_ns = lydict_insert(ctx, module_key, module_key_len);
Michal Vasko52927e22020-03-16 17:26:14 +01002245 }
2246
2247 /* insert as the last attribute */
2248 if (parent) {
Michal Vaskoa5da3292020-08-12 13:10:50 +02002249 lyd_insert_attr(parent, at);
Michal Vasko52927e22020-03-16 17:26:14 +01002250 } else if (*attr) {
2251 for (last = *attr; last->next; last = last->next);
2252 last->next = at;
2253 }
2254
2255 if (attr) {
2256 *attr = at;
2257 }
2258 return LY_SUCCESS;
2259}
2260
Radek Krejci084289f2019-07-09 17:35:30 +02002261API const struct lyd_node_term *
Michal Vasko004d3152020-06-11 19:59:22 +02002262lyd_target(const struct ly_path *path, const struct lyd_node *tree)
Radek Krejci084289f2019-07-09 17:35:30 +02002263{
Michal Vasko004d3152020-06-11 19:59:22 +02002264 struct lyd_node *target;
Radek Krejci084289f2019-07-09 17:35:30 +02002265
Michal Vasko004d3152020-06-11 19:59:22 +02002266 if (ly_path_eval(path, tree, &target)) {
2267 return NULL;
Radek Krejci084289f2019-07-09 17:35:30 +02002268 }
2269
Michal Vasko004d3152020-06-11 19:59:22 +02002270 return (struct lyd_node_term *)target;
Radek Krejci084289f2019-07-09 17:35:30 +02002271}
2272
Radek Krejci1f05b6a2019-07-18 16:15:06 +02002273API LY_ERR
Michal Vasko8f359bf2020-07-28 10:41:15 +02002274lyd_compare_single(const struct lyd_node *node1, const struct lyd_node *node2, int options)
Radek Krejci1f05b6a2019-07-18 16:15:06 +02002275{
2276 const struct lyd_node *iter1, *iter2;
2277 struct lyd_node_term *term1, *term2;
2278 struct lyd_node_any *any1, *any2;
Michal Vasko52927e22020-03-16 17:26:14 +01002279 struct lyd_node_opaq *opaq1, *opaq2;
Radek Krejci1f05b6a2019-07-18 16:15:06 +02002280 size_t len1, len2;
Radek Krejci084289f2019-07-09 17:35:30 +02002281
Radek Krejci1f05b6a2019-07-18 16:15:06 +02002282 if (!node1 || !node2) {
2283 if (node1 == node2) {
2284 return LY_SUCCESS;
2285 } else {
2286 return LY_ENOT;
2287 }
2288 }
2289
Michal Vasko52927e22020-03-16 17:26:14 +01002290 if ((LYD_NODE_CTX(node1) != LYD_NODE_CTX(node2)) || (node1->schema != node2->schema)) {
Radek Krejci1f05b6a2019-07-18 16:15:06 +02002291 return LY_ENOT;
2292 }
2293
2294 if (node1->hash != node2->hash) {
2295 return LY_ENOT;
2296 }
Michal Vasko52927e22020-03-16 17:26:14 +01002297 /* 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 +02002298
Michal Vasko52927e22020-03-16 17:26:14 +01002299 if (!node1->schema) {
2300 opaq1 = (struct lyd_node_opaq *)node1;
2301 opaq2 = (struct lyd_node_opaq *)node2;
Radek Krejci1798aae2020-07-14 13:26:06 +02002302 if ((opaq1->name != opaq2->name) || (opaq1->format != opaq2->format) || (opaq1->prefix.module_ns != opaq2->prefix.module_ns)) {
Radek Krejci1f05b6a2019-07-18 16:15:06 +02002303 return LY_ENOT;
2304 }
Michal Vasko52927e22020-03-16 17:26:14 +01002305 switch (opaq1->format) {
2306 case LYD_XML:
2307 if (lyxml_value_compare(opaq1->value, opaq1->val_prefs, opaq2->value, opaq2->val_prefs)) {
2308 return LY_ENOT;
2309 }
2310 break;
Radek Krejci1798aae2020-07-14 13:26:06 +02002311 case LYD_JSON:
2312 /* prefixes in JSON are unique, so it is not necessary to canonize the values */
2313 if (strcmp(opaq1->value, opaq2->value)) {
2314 return LY_ENOT;
2315 }
2316 break;
Michal Vasko52927e22020-03-16 17:26:14 +01002317 case LYD_SCHEMA:
Michal Vasko60ea6352020-06-29 13:39:39 +02002318 case LYD_LYB:
Michal Vasko52927e22020-03-16 17:26:14 +01002319 /* not allowed */
2320 LOGINT(LYD_NODE_CTX(node1));
2321 return LY_EINT;
2322 }
2323 if (options & LYD_COMPARE_FULL_RECURSION) {
2324 iter1 = opaq1->child;
2325 iter2 = opaq2->child;
Radek Krejci1f05b6a2019-07-18 16:15:06 +02002326 goto all_children_compare;
Michal Vasko52927e22020-03-16 17:26:14 +01002327 }
2328 return LY_SUCCESS;
2329 } else {
2330 switch (node1->schema->nodetype) {
2331 case LYS_LEAF:
2332 case LYS_LEAFLIST:
2333 if (options & LYD_COMPARE_DEFAULTS) {
2334 if ((node1->flags & LYD_DEFAULT) != (node2->flags & LYD_DEFAULT)) {
2335 return LY_ENOT;
2336 }
2337 }
2338
Michal Vasko1a2bf2e2020-06-16 13:07:48 +02002339 term1 = (struct lyd_node_term *)node1;
2340 term2 = (struct lyd_node_term *)node2;
2341 if (term1->value.realtype != term2->value.realtype) {
2342 return LY_ENOT;
2343 }
Michal Vasko52927e22020-03-16 17:26:14 +01002344
Michal Vasko1a2bf2e2020-06-16 13:07:48 +02002345 return term1->value.realtype->plugin->compare(&term1->value, &term2->value);
Michal Vasko52927e22020-03-16 17:26:14 +01002346 case LYS_CONTAINER:
2347 if (options & LYD_COMPARE_DEFAULTS) {
2348 if ((node1->flags & LYD_DEFAULT) != (node2->flags & LYD_DEFAULT)) {
2349 return LY_ENOT;
2350 }
2351 }
2352 if (options & LYD_COMPARE_FULL_RECURSION) {
2353 iter1 = ((struct lyd_node_inner*)node1)->child;
2354 iter2 = ((struct lyd_node_inner*)node2)->child;
2355 goto all_children_compare;
Radek Krejci1f05b6a2019-07-18 16:15:06 +02002356 }
2357 return LY_SUCCESS;
Michal Vasko1bf09392020-03-27 12:38:10 +01002358 case LYS_RPC:
Michal Vasko52927e22020-03-16 17:26:14 +01002359 case LYS_ACTION:
2360 if (options & LYD_COMPARE_FULL_RECURSION) {
2361 /* TODO action/RPC
2362 goto all_children_compare;
2363 */
Radek Krejci1f05b6a2019-07-18 16:15:06 +02002364 }
2365 return LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +01002366 case LYS_NOTIF:
2367 if (options & LYD_COMPARE_FULL_RECURSION) {
2368 /* TODO Notification
2369 goto all_children_compare;
2370 */
2371 }
2372 return LY_SUCCESS;
2373 case LYS_LIST:
2374 iter1 = ((struct lyd_node_inner*)node1)->child;
2375 iter2 = ((struct lyd_node_inner*)node2)->child;
2376
2377 if (!(node1->schema->flags & LYS_KEYLESS) && !(options & LYD_COMPARE_FULL_RECURSION)) {
2378 /* lists with keys, their equivalence is based on their keys */
2379 for (struct lysc_node *key = ((struct lysc_node_list*)node1->schema)->child;
Michal Vaskoc57d9a92020-06-23 13:28:27 +02002380 key && (key->flags & LYS_KEY);
Michal Vasko52927e22020-03-16 17:26:14 +01002381 key = key->next) {
Michal Vasko8f359bf2020-07-28 10:41:15 +02002382 if (lyd_compare_single(iter1, iter2, options)) {
Michal Vasko52927e22020-03-16 17:26:14 +01002383 return LY_ENOT;
2384 }
2385 iter1 = iter1->next;
2386 iter2 = iter2->next;
2387 }
2388 } else {
2389 /* lists without keys, their equivalence is based on equivalence of all the children (both direct and indirect) */
2390
2391 all_children_compare:
2392 if (!iter1 && !iter2) {
2393 /* no children, nothing to compare */
2394 return LY_SUCCESS;
2395 }
2396
2397 for (; iter1 && iter2; iter1 = iter1->next, iter2 = iter2->next) {
Michal Vasko8f359bf2020-07-28 10:41:15 +02002398 if (lyd_compare_single(iter1, iter2, options | LYD_COMPARE_FULL_RECURSION)) {
Michal Vasko52927e22020-03-16 17:26:14 +01002399 return LY_ENOT;
2400 }
2401 }
2402 if (iter1 || iter2) {
2403 return LY_ENOT;
2404 }
2405 }
2406 return LY_SUCCESS;
2407 case LYS_ANYXML:
2408 case LYS_ANYDATA:
2409 any1 = (struct lyd_node_any*)node1;
2410 any2 = (struct lyd_node_any*)node2;
2411
2412 if (any1->value_type != any2->value_type) {
2413 return LY_ENOT;
2414 }
2415 switch (any1->value_type) {
2416 case LYD_ANYDATA_DATATREE:
2417 iter1 = any1->value.tree;
2418 iter2 = any2->value.tree;
2419 goto all_children_compare;
2420 case LYD_ANYDATA_STRING:
2421 case LYD_ANYDATA_XML:
2422 case LYD_ANYDATA_JSON:
2423 len1 = strlen(any1->value.str);
2424 len2 = strlen(any2->value.str);
2425 if (len1 != len2 || strcmp(any1->value.str, any2->value.str)) {
2426 return LY_ENOT;
2427 }
2428 return LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +01002429 case LYD_ANYDATA_LYB:
Michal Vasko60ea6352020-06-29 13:39:39 +02002430 len1 = lyd_lyb_data_length(any1->value.mem);
2431 len2 = lyd_lyb_data_length(any2->value.mem);
Michal Vasko52927e22020-03-16 17:26:14 +01002432 if (len1 != len2 || memcmp(any1->value.mem, any2->value.mem, len1)) {
2433 return LY_ENOT;
2434 }
2435 return LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +01002436 }
Radek Krejci1f05b6a2019-07-18 16:15:06 +02002437 }
2438 }
2439
Michal Vasko52927e22020-03-16 17:26:14 +01002440 LOGINT(LYD_NODE_CTX(node1));
Radek Krejci1f05b6a2019-07-18 16:15:06 +02002441 return LY_EINT;
2442}
Radek Krejci22ebdba2019-07-25 13:59:43 +02002443
Michal Vasko21725742020-06-29 11:49:25 +02002444API LY_ERR
Michal Vasko8f359bf2020-07-28 10:41:15 +02002445lyd_compare_siblings(const struct lyd_node *node1, const struct lyd_node *node2, int options)
2446{
2447 for (; node1 && node2; node1 = node1->next, node2 = node2->next) {
2448 LY_CHECK_RET(lyd_compare_single(node1, node2, options));
2449 }
2450
Michal Vasko11a81432020-07-28 16:31:29 +02002451 if (node1 == node2) {
2452 return LY_SUCCESS;
Michal Vasko8f359bf2020-07-28 10:41:15 +02002453 }
Michal Vasko11a81432020-07-28 16:31:29 +02002454 return LY_ENOT;
Michal Vasko8f359bf2020-07-28 10:41:15 +02002455}
2456
2457API LY_ERR
Michal Vasko21725742020-06-29 11:49:25 +02002458lyd_compare_meta(const struct lyd_meta *meta1, const struct lyd_meta *meta2)
2459{
2460 if (!meta1 || !meta2) {
2461 if (meta1 == meta2) {
2462 return LY_SUCCESS;
2463 } else {
2464 return LY_ENOT;
2465 }
2466 }
2467
2468 if ((LYD_NODE_CTX(meta1->parent) != LYD_NODE_CTX(meta2->parent)) || (meta1->annotation != meta2->annotation)) {
2469 return LY_ENOT;
2470 }
2471
2472 if (meta1->value.realtype != meta2->value.realtype) {
2473 return LY_ENOT;
2474 }
2475
2476 return meta1->value.realtype->plugin->compare(&meta1->value, &meta2->value);
2477}
2478
Radek Krejci22ebdba2019-07-25 13:59:43 +02002479/**
Michal Vasko52927e22020-03-16 17:26:14 +01002480 * @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 +02002481 *
2482 * 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 +02002483 *
2484 * @param[in] node Original node to duplicate
2485 * @param[in] parent Parent to insert into, NULL for top-level sibling.
2486 * @param[in,out] first First sibling, NULL if no top-level sibling exist yet. Can be also NULL if @p parent is set.
2487 * @param[in] options Bitmask of options flags, see @ref dupoptions.
2488 * @param[out] dup_p Pointer where the created duplicated node is placed (besides connecting it int @p parent / @p first sibling).
2489 * @return LY_ERR value
Radek Krejci22ebdba2019-07-25 13:59:43 +02002490 */
Michal Vasko52927e22020-03-16 17:26:14 +01002491static LY_ERR
Michal Vasko3a41dff2020-07-15 14:30:28 +02002492lyd_dup_r(const struct lyd_node *node, struct lyd_node *parent, struct lyd_node **first, int options,
2493 struct lyd_node **dup_p)
Radek Krejci22ebdba2019-07-25 13:59:43 +02002494{
Michal Vasko52927e22020-03-16 17:26:14 +01002495 LY_ERR ret;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002496 struct lyd_node *dup = NULL;
Michal Vasko61551fa2020-07-09 15:45:45 +02002497 struct lyd_meta *meta;
2498 struct lyd_node_any *any;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002499 LY_ARRAY_COUNT_TYPE u;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002500
Michal Vasko52927e22020-03-16 17:26:14 +01002501 LY_CHECK_ARG_RET(NULL, node, LY_EINVAL);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002502
Michal Vasko52927e22020-03-16 17:26:14 +01002503 if (!node->schema) {
2504 dup = calloc(1, sizeof(struct lyd_node_opaq));
2505 } else {
2506 switch (node->schema->nodetype) {
Michal Vasko1bf09392020-03-27 12:38:10 +01002507 case LYS_RPC:
Michal Vasko52927e22020-03-16 17:26:14 +01002508 case LYS_ACTION:
2509 case LYS_NOTIF:
2510 case LYS_CONTAINER:
2511 case LYS_LIST:
2512 dup = calloc(1, sizeof(struct lyd_node_inner));
2513 break;
2514 case LYS_LEAF:
2515 case LYS_LEAFLIST:
2516 dup = calloc(1, sizeof(struct lyd_node_term));
2517 break;
2518 case LYS_ANYDATA:
2519 case LYS_ANYXML:
2520 dup = calloc(1, sizeof(struct lyd_node_any));
2521 break;
2522 default:
2523 LOGINT(LYD_NODE_CTX(node));
2524 ret = LY_EINT;
2525 goto error;
2526 }
Radek Krejci22ebdba2019-07-25 13:59:43 +02002527 }
Michal Vasko52927e22020-03-16 17:26:14 +01002528 LY_CHECK_ERR_GOTO(!dup, LOGMEM(LYD_NODE_CTX(node)); ret = LY_EMEM, error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002529
Michal Vaskof6df0a02020-06-16 13:08:34 +02002530 if (options & LYD_DUP_WITH_FLAGS) {
2531 dup->flags = node->flags;
2532 } else {
2533 dup->flags = (node->flags & LYD_DEFAULT) | LYD_NEW;
2534 }
Radek Krejci22ebdba2019-07-25 13:59:43 +02002535 dup->schema = node->schema;
Michal Vasko52927e22020-03-16 17:26:14 +01002536 dup->prev = dup;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002537
Michal Vasko25a32822020-07-09 15:48:22 +02002538 /* duplicate metadata */
2539 if (!(options & LYD_DUP_NO_META)) {
2540 LY_LIST_FOR(node->meta, meta) {
Michal Vasko3a41dff2020-07-15 14:30:28 +02002541 LY_CHECK_GOTO(ret = lyd_dup_meta_single(meta, dup, NULL), error);
Michal Vasko25a32822020-07-09 15:48:22 +02002542 }
2543 }
Radek Krejci22ebdba2019-07-25 13:59:43 +02002544
2545 /* nodetype-specific work */
Michal Vasko52927e22020-03-16 17:26:14 +01002546 if (!dup->schema) {
2547 struct lyd_node_opaq *opaq = (struct lyd_node_opaq *)dup;
2548 struct lyd_node_opaq *orig = (struct lyd_node_opaq *)node;
2549 struct lyd_node *child;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002550
2551 if (options & LYD_DUP_RECURSIVE) {
2552 /* duplicate all the children */
2553 LY_LIST_FOR(orig->child, child) {
Michal Vasko3a41dff2020-07-15 14:30:28 +02002554 LY_CHECK_GOTO(ret = lyd_dup_r(child, dup, NULL, options, NULL), error);
Michal Vasko52927e22020-03-16 17:26:14 +01002555 }
2556 }
2557 opaq->name = lydict_insert(LYD_NODE_CTX(node), orig->name, 0);
2558 opaq->format = orig->format;
Radek Krejci1798aae2020-07-14 13:26:06 +02002559 if (orig->prefix.id) {
2560 opaq->prefix.id = lydict_insert(LYD_NODE_CTX(node), orig->prefix.id, 0);
Michal Vasko52927e22020-03-16 17:26:14 +01002561 }
Radek Krejci1798aae2020-07-14 13:26:06 +02002562 opaq->prefix.module_ns = lydict_insert(LYD_NODE_CTX(node), orig->prefix.module_ns, 0);
Michal Vasko52927e22020-03-16 17:26:14 +01002563 if (orig->val_prefs) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002564 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 +01002565 LY_ARRAY_FOR(orig->val_prefs, u) {
Radek Krejci1798aae2020-07-14 13:26:06 +02002566 opaq->val_prefs[u].id = lydict_insert(LYD_NODE_CTX(node), orig->val_prefs[u].id, 0);
2567 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 +01002568 LY_ARRAY_INCREMENT(opaq->val_prefs);
2569 }
2570 }
2571 opaq->value = lydict_insert(LYD_NODE_CTX(node), orig->value, 0);
2572 opaq->ctx = orig->ctx;
2573 } else if (dup->schema->nodetype & LYD_NODE_TERM) {
2574 struct lyd_node_term *term = (struct lyd_node_term *)dup;
2575 struct lyd_node_term *orig = (struct lyd_node_term *)node;
2576
2577 term->hash = orig->hash;
2578 term->value.realtype = orig->value.realtype;
2579 LY_CHECK_ERR_GOTO(term->value.realtype->plugin->duplicate(LYD_NODE_CTX(node), &orig->value, &term->value),
2580 LOGERR(LYD_NODE_CTX(node), LY_EINT, "Value duplication failed."); ret = LY_EINT, error);
2581 } else if (dup->schema->nodetype & LYD_NODE_INNER) {
2582 struct lyd_node_inner *orig = (struct lyd_node_inner *)node;
2583 struct lyd_node *child;
2584
2585 if (options & LYD_DUP_RECURSIVE) {
2586 /* duplicate all the children */
2587 LY_LIST_FOR(orig->child, child) {
Michal Vasko3a41dff2020-07-15 14:30:28 +02002588 LY_CHECK_GOTO(ret = lyd_dup_r(child, dup, NULL, options, NULL), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002589 }
Radek Krejci0fe9b512019-07-26 17:51:05 +02002590 } else if (dup->schema->nodetype == LYS_LIST && !(dup->schema->flags & LYS_KEYLESS)) {
Radek Krejci22ebdba2019-07-25 13:59:43 +02002591 /* always duplicate keys of a list */
Radek Krejci22ebdba2019-07-25 13:59:43 +02002592 child = orig->child;
Michal Vasko52927e22020-03-16 17:26:14 +01002593 for (struct lysc_node *key = ((struct lysc_node_list *)dup->schema)->child;
Michal Vaskoc57d9a92020-06-23 13:28:27 +02002594 key && (key->flags & LYS_KEY);
Radek Krejci0fe9b512019-07-26 17:51:05 +02002595 key = key->next) {
Radek Krejci22ebdba2019-07-25 13:59:43 +02002596 if (!child) {
2597 /* possibly not keys are present in filtered tree */
2598 break;
Radek Krejci0fe9b512019-07-26 17:51:05 +02002599 } else if (child->schema != key) {
2600 /* possibly not all keys are present in filtered tree,
2601 * but there can be also some non-key nodes */
2602 continue;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002603 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02002604 LY_CHECK_GOTO(ret = lyd_dup_r(child, dup, NULL, options, NULL), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002605 child = child->next;
2606 }
2607 }
2608 lyd_hash(dup);
2609 } else if (dup->schema->nodetype & LYD_NODE_ANY) {
Michal Vasko61551fa2020-07-09 15:45:45 +02002610 dup->hash = node->hash;
2611 any = (struct lyd_node_any *)node;
2612 LY_CHECK_GOTO(ret = lyd_any_copy_value(dup, &any->value, any->value_type), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002613 }
2614
Michal Vasko52927e22020-03-16 17:26:14 +01002615 /* insert */
2616 lyd_insert_node(parent, first, dup);
Michal Vasko52927e22020-03-16 17:26:14 +01002617
2618 if (dup_p) {
2619 *dup_p = dup;
2620 }
2621 return LY_SUCCESS;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002622
2623error:
Michal Vasko52927e22020-03-16 17:26:14 +01002624 lyd_free_tree(dup);
2625 return ret;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002626}
2627
Michal Vasko3a41dff2020-07-15 14:30:28 +02002628static LY_ERR
2629lyd_dup_get_local_parent(const struct lyd_node *node, const struct lyd_node_inner *parent, struct lyd_node **dup_parent,
2630 struct lyd_node_inner **local_parent)
Radek Krejci22ebdba2019-07-25 13:59:43 +02002631{
Michal Vasko3a41dff2020-07-15 14:30:28 +02002632 const struct lyd_node_inner *orig_parent, *iter;
2633 int repeat = 1;
2634
2635 *dup_parent = NULL;
2636 *local_parent = NULL;
2637
2638 for (orig_parent = node->parent; repeat && orig_parent; orig_parent = orig_parent->parent) {
2639 if (parent && (parent->schema == orig_parent->schema)) {
2640 /* stop creating parents, connect what we have into the provided parent */
2641 iter = parent;
2642 repeat = 0;
2643 } else {
2644 iter = NULL;
2645 LY_CHECK_RET(lyd_dup_r((struct lyd_node *)orig_parent, NULL, (struct lyd_node **)&iter, 0,
2646 (struct lyd_node **)&iter));
2647 }
2648 if (!*local_parent) {
2649 *local_parent = (struct lyd_node_inner *)iter;
2650 }
2651 if (iter->child) {
2652 /* 1) list - add after keys
2653 * 2) provided parent with some children */
2654 iter->child->prev->next = *dup_parent;
2655 if (*dup_parent) {
2656 (*dup_parent)->prev = iter->child->prev;
2657 iter->child->prev = *dup_parent;
2658 }
2659 } else {
2660 ((struct lyd_node_inner *)iter)->child = *dup_parent;
2661 }
2662 if (*dup_parent) {
2663 (*dup_parent)->parent = (struct lyd_node_inner *)iter;
2664 }
2665 *dup_parent = (struct lyd_node *)iter;
2666 }
2667
2668 if (repeat && parent) {
2669 /* given parent and created parents chain actually do not interconnect */
2670 LOGERR(LYD_NODE_CTX(node), LY_EINVAL,
2671 "Invalid argument parent (%s()) - does not interconnect with the created node's parents chain.", __func__);
2672 return LY_EINVAL;
2673 }
2674
2675 return LY_SUCCESS;
2676}
2677
2678static LY_ERR
2679lyd_dup(const struct lyd_node *node, struct lyd_node_inner *parent, int options, int nosiblings, struct lyd_node **dup)
2680{
2681 LY_ERR rc;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002682 const struct lyd_node *orig; /* original node to be duplicated */
2683 struct lyd_node *first = NULL; /* the first duplicated node, this is returned */
Radek Krejci22ebdba2019-07-25 13:59:43 +02002684 struct lyd_node *top = NULL; /* the most higher created node */
2685 struct lyd_node_inner *local_parent = NULL; /* the direct parent node for the duplicated node(s) */
Radek Krejci22ebdba2019-07-25 13:59:43 +02002686
Michal Vasko3a41dff2020-07-15 14:30:28 +02002687 LY_CHECK_ARG_RET(NULL, node, LY_EINVAL);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002688
2689 if (options & LYD_DUP_WITH_PARENTS) {
Michal Vasko3a41dff2020-07-15 14:30:28 +02002690 LY_CHECK_GOTO(rc = lyd_dup_get_local_parent(node, parent, &top, &local_parent), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002691 } else {
2692 local_parent = parent;
2693 }
2694
Radek Krejci22ebdba2019-07-25 13:59:43 +02002695 LY_LIST_FOR(node, orig) {
Michal Vasko52927e22020-03-16 17:26:14 +01002696 /* if there is no local parent, it will be inserted into first */
Michal Vasko3a41dff2020-07-15 14:30:28 +02002697 LY_CHECK_GOTO(rc = lyd_dup_r(orig, (struct lyd_node *)local_parent, &first, options, first ? NULL : &first), error);
2698 if (nosiblings) {
Radek Krejci22ebdba2019-07-25 13:59:43 +02002699 break;
2700 }
2701 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02002702
2703 /* rehash if needed */
2704 for (; local_parent; local_parent = local_parent->parent) {
2705 if (local_parent->schema->nodetype == LYS_LIST && (local_parent->schema->flags & LYS_KEYLESS)) {
2706 lyd_hash((struct lyd_node *)local_parent);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002707 }
2708 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02002709
2710 if (dup) {
2711 *dup = first;
2712 }
2713 return LY_SUCCESS;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002714
2715error:
2716 if (top) {
2717 lyd_free_tree(top);
2718 } else {
Michal Vaskof03ed032020-03-04 13:31:44 +01002719 lyd_free_siblings(first);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002720 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02002721 return rc;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002722}
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002723
Michal Vasko3a41dff2020-07-15 14:30:28 +02002724API LY_ERR
2725lyd_dup_single(const struct lyd_node *node, struct lyd_node_inner *parent, int options, struct lyd_node **dup)
2726{
2727 return lyd_dup(node, parent, options, 1, dup);
2728}
2729
2730API LY_ERR
2731lyd_dup_siblings(const struct lyd_node *node, struct lyd_node_inner *parent, int options, struct lyd_node **dup)
2732{
2733 return lyd_dup(node, parent, options, 0, dup);
2734}
2735
2736API LY_ERR
2737lyd_dup_meta_single(const struct lyd_meta *meta, struct lyd_node *node, struct lyd_meta **dup)
Michal Vasko25a32822020-07-09 15:48:22 +02002738{
2739 LY_ERR ret;
2740 struct lyd_meta *mt, *last;
2741
Michal Vasko3a41dff2020-07-15 14:30:28 +02002742 LY_CHECK_ARG_RET(NULL, meta, node, LY_EINVAL);
Michal Vasko25a32822020-07-09 15:48:22 +02002743
2744 /* create a copy */
2745 mt = calloc(1, sizeof *mt);
Michal Vasko3a41dff2020-07-15 14:30:28 +02002746 LY_CHECK_ERR_RET(!mt, LOGMEM(LYD_NODE_CTX(node)), LY_EMEM);
Michal Vasko25a32822020-07-09 15:48:22 +02002747 mt->parent = node;
2748 mt->annotation = meta->annotation;
2749 mt->value.realtype = meta->value.realtype;
2750 ret = mt->value.realtype->plugin->duplicate(LYD_NODE_CTX(node), &meta->value, &mt->value);
Michal Vasko3a41dff2020-07-15 14:30:28 +02002751 LY_CHECK_ERR_RET(ret, LOGERR(LYD_NODE_CTX(node), LY_EINT, "Value duplication failed."), ret);
Michal Vasko25a32822020-07-09 15:48:22 +02002752 mt->name = lydict_insert(LYD_NODE_CTX(node), meta->name, 0);
2753
2754 /* insert as the last attribute */
2755 if (node->meta) {
2756 for (last = node->meta; last->next; last = last->next);
2757 last->next = mt;
2758 } else {
2759 node->meta = mt;
2760 }
2761
Michal Vasko3a41dff2020-07-15 14:30:28 +02002762 if (dup) {
2763 *dup = mt;
2764 }
2765 return LY_SUCCESS;
Michal Vasko25a32822020-07-09 15:48:22 +02002766}
2767
Michal Vasko4490d312020-06-16 13:08:55 +02002768/**
2769 * @brief Merge a source sibling into target siblings.
2770 *
2771 * @param[in,out] first_trg First target sibling, is updated if top-level.
2772 * @param[in] parent_trg Target parent.
2773 * @param[in,out] sibling_src Source sibling to merge, set to NULL if spent.
2774 * @param[in] options Merge options.
2775 * @return LY_ERR value.
2776 */
2777static LY_ERR
2778lyd_merge_sibling_r(struct lyd_node **first_trg, struct lyd_node *parent_trg, const struct lyd_node **sibling_src_p,
2779 int options)
2780{
2781 LY_ERR ret;
2782 const struct lyd_node *child_src, *tmp, *sibling_src;
Michal Vasko56daf732020-08-10 10:57:18 +02002783 struct lyd_node *match_trg, *dup_src, *elem;
Michal Vasko4490d312020-06-16 13:08:55 +02002784 struct lysc_type *type;
Michal Vasko4490d312020-06-16 13:08:55 +02002785
2786 sibling_src = *sibling_src_p;
2787 if (sibling_src->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) {
2788 /* try to find the exact instance */
2789 ret = lyd_find_sibling_first(*first_trg, sibling_src, &match_trg);
2790 } else {
2791 /* try to simply find the node, there cannot be more instances */
2792 ret = lyd_find_sibling_val(*first_trg, sibling_src->schema, NULL, 0, &match_trg);
2793 }
2794
2795 if (!ret) {
2796 /* node found, make sure even value matches for all node types */
Michal Vasko8f359bf2020-07-28 10:41:15 +02002797 if ((match_trg->schema->nodetype == LYS_LEAF) && lyd_compare_single(sibling_src, match_trg, LYD_COMPARE_DEFAULTS)) {
Michal Vasko4490d312020-06-16 13:08:55 +02002798 /* since they are different, they cannot both be default */
2799 assert(!(sibling_src->flags & LYD_DEFAULT) || !(match_trg->flags & LYD_DEFAULT));
2800
Michal Vasko3a41dff2020-07-15 14:30:28 +02002801 /* update value (or only LYD_DEFAULT flag) only if flag set or the source node is not default */
2802 if ((options & LYD_MERGE_DEFAULTS) || !(sibling_src->flags & LYD_DEFAULT)) {
Michal Vasko4490d312020-06-16 13:08:55 +02002803 type = ((struct lysc_node_leaf *)match_trg->schema)->type;
2804 type->plugin->free(LYD_NODE_CTX(match_trg), &((struct lyd_node_term *)match_trg)->value);
2805 LY_CHECK_RET(type->plugin->duplicate(LYD_NODE_CTX(match_trg), &((struct lyd_node_term *)sibling_src)->value,
2806 &((struct lyd_node_term *)match_trg)->value));
2807
2808 /* copy flags and add LYD_NEW */
2809 match_trg->flags = sibling_src->flags | LYD_NEW;
2810 }
Michal Vasko8f359bf2020-07-28 10:41:15 +02002811 } else if ((match_trg->schema->nodetype & LYS_ANYDATA) && lyd_compare_single(sibling_src, match_trg, 0)) {
Michal Vasko4c583e82020-07-17 12:16:14 +02002812 /* update value */
2813 LY_CHECK_RET(lyd_any_copy_value(match_trg, &((struct lyd_node_any *)sibling_src)->value,
2814 ((struct lyd_node_any *)sibling_src)->value_type));
Michal Vasko4490d312020-06-16 13:08:55 +02002815
2816 /* copy flags and add LYD_NEW */
2817 match_trg->flags = sibling_src->flags | LYD_NEW;
Michal Vasko4490d312020-06-16 13:08:55 +02002818 } else {
2819 /* check descendants, recursively */
Michal Vasko8ee464a2020-08-11 14:12:50 +02002820 LY_LIST_FOR_SAFE(LYD_CHILD_NO_KEYS(sibling_src), tmp, child_src) {
Michal Vasko4490d312020-06-16 13:08:55 +02002821 LY_CHECK_RET(lyd_merge_sibling_r(lyd_node_children_p(match_trg), match_trg, &child_src, options));
2822 }
2823 }
2824 } else {
2825 /* node not found, merge it */
2826 if (options & LYD_MERGE_DESTRUCT) {
2827 dup_src = (struct lyd_node *)sibling_src;
2828 lyd_unlink_tree(dup_src);
2829 /* spend it */
2830 *sibling_src_p = NULL;
2831 } else {
Michal Vasko3a41dff2020-07-15 14:30:28 +02002832 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 +02002833 }
2834
2835 /* set LYD_NEW for all the new nodes, required for validation */
Michal Vasko56daf732020-08-10 10:57:18 +02002836 LYD_TREE_DFS_BEGIN(dup_src, elem) {
Michal Vasko4490d312020-06-16 13:08:55 +02002837 elem->flags |= LYD_NEW;
Michal Vasko56daf732020-08-10 10:57:18 +02002838 LYD_TREE_DFS_END(dup_src, elem);
Michal Vasko4490d312020-06-16 13:08:55 +02002839 }
2840
2841 lyd_insert_node(parent_trg, first_trg, dup_src);
2842 }
2843
2844 return LY_SUCCESS;
2845}
2846
Michal Vasko3a41dff2020-07-15 14:30:28 +02002847static LY_ERR
2848lyd_merge(struct lyd_node **target, const struct lyd_node *source, int options, int nosiblings)
Michal Vasko4490d312020-06-16 13:08:55 +02002849{
2850 const struct lyd_node *sibling_src, *tmp;
2851 int first;
2852
2853 LY_CHECK_ARG_RET(NULL, target, LY_EINVAL);
2854
2855 if (!source) {
2856 /* nothing to merge */
2857 return LY_SUCCESS;
2858 }
2859
2860 if (lysc_data_parent((*target)->schema) || lysc_data_parent(source->schema)) {
2861 LOGERR(LYD_NODE_CTX(source), LY_EINVAL, "Invalid arguments - can merge only 2 top-level subtrees (%s()).", __func__);
2862 return LY_EINVAL;
2863 }
2864
2865 LY_LIST_FOR_SAFE(source, tmp, sibling_src) {
2866 first = sibling_src == source ? 1 : 0;
2867 LY_CHECK_RET(lyd_merge_sibling_r(target, NULL, &sibling_src, options));
2868 if (first && !sibling_src) {
2869 /* source was spent (unlinked), move to the next node */
2870 source = tmp;
2871 }
2872
Michal Vasko3a41dff2020-07-15 14:30:28 +02002873 if (nosiblings) {
Michal Vasko4490d312020-06-16 13:08:55 +02002874 break;
2875 }
2876 }
2877
2878 if (options & LYD_MERGE_DESTRUCT) {
2879 /* free any leftover source data that were not merged */
2880 lyd_free_siblings((struct lyd_node *)source);
2881 }
2882
2883 return LY_SUCCESS;
2884}
2885
Michal Vasko3a41dff2020-07-15 14:30:28 +02002886API LY_ERR
2887lyd_merge_tree(struct lyd_node **target, const struct lyd_node *source, int options)
2888{
2889 return lyd_merge(target, source, options, 1);
2890}
2891
2892API LY_ERR
2893lyd_merge_siblings(struct lyd_node **target, const struct lyd_node *source, int options)
2894{
2895 return lyd_merge(target, source, options, 0);
2896}
2897
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002898static LY_ERR
2899lyd_path_str_enlarge(char **buffer, size_t *buflen, size_t reqlen, int is_static)
2900{
Michal Vasko14654712020-02-06 08:35:21 +01002901 /* ending \0 */
2902 ++reqlen;
2903
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002904 if (reqlen > *buflen) {
2905 if (is_static) {
2906 return LY_EINCOMPLETE;
2907 }
2908
2909 *buffer = ly_realloc(*buffer, reqlen * sizeof **buffer);
2910 if (!*buffer) {
2911 return LY_EMEM;
2912 }
2913
2914 *buflen = reqlen;
2915 }
2916
2917 return LY_SUCCESS;
2918}
2919
Michal Vaskod59035b2020-07-08 12:00:06 +02002920LY_ERR
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002921lyd_path_list_predicate(const struct lyd_node *node, char **buffer, size_t *buflen, size_t *bufused, int is_static)
2922{
2923 const struct lyd_node *key;
2924 int dynamic = 0;
2925 size_t len;
2926 const char *val;
2927 char quot;
2928 LY_ERR rc;
2929
Michal Vasko5bfd4be2020-06-23 13:26:19 +02002930 for (key = lyd_node_children(node, 0); key && (key->schema->flags & LYS_KEY); key = key->next) {
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002931 val = lyd_value2str((struct lyd_node_term *)key, &dynamic);
2932 len = 1 + strlen(key->schema->name) + 2 + strlen(val) + 2;
2933 rc = lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static);
2934 if (rc != LY_SUCCESS) {
2935 if (dynamic) {
2936 free((char *)val);
2937 }
2938 return rc;
2939 }
2940
2941 quot = '\'';
2942 if (strchr(val, '\'')) {
2943 quot = '"';
2944 }
2945 *bufused += sprintf(*buffer + *bufused, "[%s=%c%s%c]", key->schema->name, quot, val, quot);
2946
2947 if (dynamic) {
2948 free((char *)val);
2949 }
2950 }
2951
2952 return LY_SUCCESS;
2953}
2954
2955/**
2956 * @brief Append leaf-list value predicate to path.
2957 *
2958 * @param[in] node Node to print.
2959 * @param[in,out] buffer Buffer to print to.
2960 * @param[in,out] buflen Current buffer length.
2961 * @param[in,out] bufused Current number of characters used in @p buffer.
2962 * @param[in] is_static Whether buffer is static or can be reallocated.
2963 * @return LY_ERR
2964 */
2965static LY_ERR
2966lyd_path_leaflist_predicate(const struct lyd_node *node, char **buffer, size_t *buflen, size_t *bufused, int is_static)
2967{
2968 int dynamic = 0;
2969 size_t len;
2970 const char *val;
2971 char quot;
2972 LY_ERR rc;
2973
2974 val = lyd_value2str((struct lyd_node_term *)node, &dynamic);
2975 len = 4 + strlen(val) + 2;
2976 rc = lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static);
2977 if (rc != LY_SUCCESS) {
2978 goto cleanup;
2979 }
2980
2981 quot = '\'';
2982 if (strchr(val, '\'')) {
2983 quot = '"';
2984 }
2985 *bufused += sprintf(*buffer + *bufused, "[.=%c%s%c]", quot, val, quot);
2986
2987cleanup:
2988 if (dynamic) {
2989 free((char *)val);
2990 }
2991 return rc;
2992}
2993
2994/**
2995 * @brief Append node position (relative to its other instances) predicate to path.
2996 *
2997 * @param[in] node Node to print.
2998 * @param[in,out] buffer Buffer to print to.
2999 * @param[in,out] buflen Current buffer length.
3000 * @param[in,out] bufused Current number of characters used in @p buffer.
3001 * @param[in] is_static Whether buffer is static or can be reallocated.
3002 * @return LY_ERR
3003 */
3004static LY_ERR
3005lyd_path_position_predicate(const struct lyd_node *node, char **buffer, size_t *buflen, size_t *bufused, int is_static)
3006{
3007 const struct lyd_node *first, *iter;
3008 size_t len;
3009 int pos;
3010 char *val = NULL;
3011 LY_ERR rc;
3012
3013 if (node->parent) {
3014 first = node->parent->child;
3015 } else {
3016 for (first = node; node->prev->next; node = node->prev);
3017 }
3018 pos = 1;
3019 for (iter = first; iter != node; iter = iter->next) {
3020 if (iter->schema == node->schema) {
3021 ++pos;
3022 }
3023 }
3024 if (asprintf(&val, "%d", pos) == -1) {
3025 return LY_EMEM;
3026 }
3027
3028 len = 1 + strlen(val) + 1;
3029 rc = lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static);
3030 if (rc != LY_SUCCESS) {
3031 goto cleanup;
3032 }
3033
3034 *bufused += sprintf(*buffer + *bufused, "[%s]", val);
3035
3036cleanup:
3037 free(val);
3038 return rc;
3039}
3040
3041API char *
3042lyd_path(const struct lyd_node *node, LYD_PATH_TYPE pathtype, char *buffer, size_t buflen)
3043{
Michal Vasko14654712020-02-06 08:35:21 +01003044 int is_static = 0, i, depth;
3045 size_t bufused = 0, len;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003046 const struct lyd_node *iter;
3047 const struct lys_module *mod;
Michal Vasko790b2bc2020-08-03 13:35:06 +02003048 LY_ERR rc = LY_SUCCESS;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003049
3050 LY_CHECK_ARG_RET(NULL, node, NULL);
3051 if (buffer) {
3052 LY_CHECK_ARG_RET(node->schema->module->ctx, buflen > 1, NULL);
3053 is_static = 1;
Michal Vasko14654712020-02-06 08:35:21 +01003054 } else {
3055 buflen = 0;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003056 }
3057
3058 switch (pathtype) {
Michal Vasko14654712020-02-06 08:35:21 +01003059 case LYD_PATH_LOG:
Michal Vasko790b2bc2020-08-03 13:35:06 +02003060 case LYD_PATH_LOG_NO_LAST_PRED:
Michal Vasko14654712020-02-06 08:35:21 +01003061 depth = 1;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003062 for (iter = node; iter->parent; iter = (const struct lyd_node *)iter->parent) {
3063 ++depth;
3064 }
3065
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003066 goto iter_print;
Michal Vasko14654712020-02-06 08:35:21 +01003067 while (depth) {
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003068 /* find the right node */
Michal Vasko14654712020-02-06 08:35:21 +01003069 for (iter = node, i = 1; i < depth; iter = (const struct lyd_node *)iter->parent, ++i);
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003070iter_print:
3071 /* print prefix and name */
3072 mod = NULL;
Radek Krejci1798aae2020-07-14 13:26:06 +02003073 if (iter->schema && (!iter->parent || iter->schema->module != iter->parent->schema->module)) {
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003074 mod = iter->schema->module;
3075 }
3076
3077 /* realloc string */
Radek Krejci1798aae2020-07-14 13:26:06 +02003078 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 +02003079 rc = lyd_path_str_enlarge(&buffer, &buflen, bufused + len, is_static);
3080 if (rc != LY_SUCCESS) {
3081 break;
3082 }
3083
3084 /* print next node */
Radek Krejci1798aae2020-07-14 13:26:06 +02003085 bufused += sprintf(buffer + bufused, "/%s%s%s", mod ? mod->name : "", mod ? ":" : "",
3086 iter->schema ? iter->schema->name : ((struct lyd_node_opaq*)iter)->name);
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003087
Michal Vasko790b2bc2020-08-03 13:35:06 +02003088 /* do not always print the last (first) predicate */
Radek Krejci1798aae2020-07-14 13:26:06 +02003089 if (iter->schema && (bufused || (pathtype == LYD_PATH_LOG))) {
Michal Vasko790b2bc2020-08-03 13:35:06 +02003090 switch (iter->schema->nodetype) {
3091 case LYS_LIST:
3092 if (iter->schema->flags & LYS_KEYLESS) {
3093 /* print its position */
3094 rc = lyd_path_position_predicate(iter, &buffer, &buflen, &bufused, is_static);
3095 } else {
3096 /* print all list keys in predicates */
3097 rc = lyd_path_list_predicate(iter, &buffer, &buflen, &bufused, is_static);
3098 }
3099 break;
3100 case LYS_LEAFLIST:
3101 if (iter->schema->flags & LYS_CONFIG_W) {
3102 /* print leaf-list value */
3103 rc = lyd_path_leaflist_predicate(iter, &buffer, &buflen, &bufused, is_static);
3104 } else {
3105 /* print its position */
3106 rc = lyd_path_position_predicate(iter, &buffer, &buflen, &bufused, is_static);
3107 }
3108 break;
3109 default:
3110 /* nothing to print more */
3111 break;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003112 }
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003113 }
3114 if (rc != LY_SUCCESS) {
3115 break;
3116 }
3117
Michal Vasko14654712020-02-06 08:35:21 +01003118 --depth;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003119 }
3120 break;
3121 }
3122
3123 return buffer;
3124}
Michal Vaskoe444f752020-02-10 12:20:06 +01003125
Michal Vasko25a32822020-07-09 15:48:22 +02003126API struct lyd_meta *
3127lyd_find_meta(const struct lyd_meta *first, const struct lys_module *module, const char *name)
3128{
3129 struct lyd_meta *ret = NULL;
3130 const struct ly_ctx *ctx;
3131 const char *prefix, *tmp;
3132 char *str;
3133 size_t pref_len, name_len;
3134
3135 LY_CHECK_ARG_RET(NULL, module || strchr(name, ':'), name, NULL);
3136
3137 if (!first) {
3138 return NULL;
3139 }
3140
3141 ctx = first->annotation->module->ctx;
3142
3143 /* parse the name */
3144 tmp = name;
3145 if (ly_parse_nodeid(&tmp, &prefix, &pref_len, &name, &name_len) || tmp[0]) {
3146 LOGERR(ctx, LY_EINVAL, "Metadata name \"%s\" is not valid.", name);
3147 return NULL;
3148 }
3149
3150 /* find the module */
3151 if (prefix) {
3152 str = strndup(prefix, pref_len);
3153 module = ly_ctx_get_module_latest(ctx, str);
3154 free(str);
3155 LY_CHECK_ERR_RET(!module, LOGERR(ctx, LY_EINVAL, "Module \"%.*s\" not found.", pref_len, prefix), NULL);
3156 }
3157
3158 /* find the metadata */
3159 LY_LIST_FOR(first, first) {
3160 if ((first->annotation->module == module) && !strcmp(first->name, name)) {
3161 ret = (struct lyd_meta *)first;
3162 break;
3163 }
3164 }
3165
3166 return ret;
3167}
3168
Michal Vasko9b368d32020-02-14 13:53:31 +01003169API LY_ERR
Michal Vaskoe444f752020-02-10 12:20:06 +01003170lyd_find_sibling_first(const struct lyd_node *siblings, const struct lyd_node *target, struct lyd_node **match)
3171{
3172 struct lyd_node **match_p;
3173 struct lyd_node_inner *parent;
3174
Michal Vaskof03ed032020-03-04 13:31:44 +01003175 LY_CHECK_ARG_RET(NULL, target, LY_EINVAL);
Michal Vaskoe444f752020-02-10 12:20:06 +01003176
Michal Vasko62ed12d2020-05-21 10:08:25 +02003177 if (!siblings || (lysc_data_parent(siblings->schema) != lysc_data_parent(target->schema))) {
3178 /* no data or schema mismatch */
Michal Vasko9b368d32020-02-14 13:53:31 +01003179 if (match) {
3180 *match = NULL;
3181 }
Michal Vaskoe444f752020-02-10 12:20:06 +01003182 return LY_ENOTFOUND;
3183 }
3184
3185 /* find first sibling */
3186 if (siblings->parent) {
3187 siblings = siblings->parent->child;
3188 } else {
3189 while (siblings->prev->next) {
3190 siblings = siblings->prev;
3191 }
3192 }
3193
3194 parent = (struct lyd_node_inner *)siblings->parent;
3195 if (parent && parent->children_ht) {
3196 assert(target->hash);
3197
3198 /* find by hash */
3199 if (!lyht_find(parent->children_ht, &target, target->hash, (void **)&match_p)) {
Michal Vaskoda859032020-07-14 12:20:14 +02003200 /* check even value when needed */
Michal Vasko8f359bf2020-07-28 10:41:15 +02003201 if (!(target->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) || !lyd_compare_single(target, *match_p, 0)) {
Michal Vaskoda859032020-07-14 12:20:14 +02003202 siblings = *match_p;
3203 } else {
3204 siblings = NULL;
3205 }
Michal Vaskoe444f752020-02-10 12:20:06 +01003206 } else {
3207 /* not found */
3208 siblings = NULL;
3209 }
3210 } else {
3211 /* no children hash table */
3212 for (; siblings; siblings = siblings->next) {
Michal Vasko8f359bf2020-07-28 10:41:15 +02003213 if (!lyd_compare_single(siblings, target, 0)) {
Michal Vaskoe444f752020-02-10 12:20:06 +01003214 break;
3215 }
3216 }
3217 }
3218
3219 if (!siblings) {
Michal Vasko9b368d32020-02-14 13:53:31 +01003220 if (match) {
3221 *match = NULL;
3222 }
Michal Vaskoe444f752020-02-10 12:20:06 +01003223 return LY_ENOTFOUND;
3224 }
3225
Michal Vasko9b368d32020-02-14 13:53:31 +01003226 if (match) {
3227 *match = (struct lyd_node *)siblings;
3228 }
Michal Vaskoe444f752020-02-10 12:20:06 +01003229 return LY_SUCCESS;
3230}
3231
Michal Vasko90932a92020-02-12 14:33:03 +01003232static int
3233lyd_hash_table_schema_val_equal(void *val1_p, void *val2_p, int UNUSED(mod), void *UNUSED(cb_data))
3234{
3235 struct lysc_node *val1;
3236 struct lyd_node *val2;
3237
3238 val1 = *((struct lysc_node **)val1_p);
3239 val2 = *((struct lyd_node **)val2_p);
3240
Michal Vasko90932a92020-02-12 14:33:03 +01003241 if (val1 == val2->schema) {
3242 /* schema match is enough */
3243 return 1;
3244 } else {
3245 return 0;
3246 }
3247}
3248
3249static LY_ERR
3250lyd_find_sibling_schema(const struct lyd_node *siblings, const struct lysc_node *schema, struct lyd_node **match)
3251{
3252 struct lyd_node **match_p;
3253 struct lyd_node_inner *parent;
3254 uint32_t hash;
3255 values_equal_cb ht_cb;
3256
Michal Vaskob104f112020-07-17 09:54:54 +02003257 assert(siblings && schema);
Michal Vasko90932a92020-02-12 14:33:03 +01003258
3259 parent = (struct lyd_node_inner *)siblings->parent;
3260 if (parent && parent->children_ht) {
3261 /* calculate our hash */
3262 hash = dict_hash_multi(0, schema->module->name, strlen(schema->module->name));
3263 hash = dict_hash_multi(hash, schema->name, strlen(schema->name));
3264 hash = dict_hash_multi(hash, NULL, 0);
3265
3266 /* use special hash table function */
3267 ht_cb = lyht_set_cb(parent->children_ht, lyd_hash_table_schema_val_equal);
3268
3269 /* find by hash */
3270 if (!lyht_find(parent->children_ht, &schema, hash, (void **)&match_p)) {
3271 siblings = *match_p;
3272 } else {
3273 /* not found */
3274 siblings = NULL;
3275 }
3276
3277 /* set the original hash table compare function back */
3278 lyht_set_cb(parent->children_ht, ht_cb);
3279 } else {
Michal Vaskob104f112020-07-17 09:54:54 +02003280 /* find first sibling */
3281 if (siblings->parent) {
3282 siblings = siblings->parent->child;
3283 } else {
3284 while (siblings->prev->next) {
3285 siblings = siblings->prev;
3286 }
3287 }
3288
3289 /* search manually without hashes */
Michal Vasko90932a92020-02-12 14:33:03 +01003290 for (; siblings; siblings = siblings->next) {
3291 if (siblings->schema == schema) {
3292 /* schema match is enough */
3293 break;
3294 }
3295 }
3296 }
3297
3298 if (!siblings) {
Michal Vasko9b368d32020-02-14 13:53:31 +01003299 if (match) {
3300 *match = NULL;
3301 }
Michal Vasko90932a92020-02-12 14:33:03 +01003302 return LY_ENOTFOUND;
3303 }
3304
Michal Vasko9b368d32020-02-14 13:53:31 +01003305 if (match) {
3306 *match = (struct lyd_node *)siblings;
3307 }
Michal Vasko90932a92020-02-12 14:33:03 +01003308 return LY_SUCCESS;
3309}
3310
Michal Vaskoe444f752020-02-10 12:20:06 +01003311API LY_ERR
3312lyd_find_sibling_val(const struct lyd_node *siblings, const struct lysc_node *schema, const char *key_or_value,
3313 size_t val_len, struct lyd_node **match)
3314{
3315 LY_ERR rc;
3316 struct lyd_node *target = NULL;
3317
Michal Vasko4c583e82020-07-17 12:16:14 +02003318 LY_CHECK_ARG_RET(NULL, schema, !(schema->nodetype & (LYS_CHOICE | LYS_CASE)), LY_EINVAL);
Michal Vaskoe444f752020-02-10 12:20:06 +01003319
Michal Vasko62ed12d2020-05-21 10:08:25 +02003320 if (!siblings || (lysc_data_parent(siblings->schema) != lysc_data_parent(schema))) {
3321 /* no data or schema mismatch */
Michal Vasko9b368d32020-02-14 13:53:31 +01003322 if (match) {
3323 *match = NULL;
3324 }
Michal Vaskoe444f752020-02-10 12:20:06 +01003325 return LY_ENOTFOUND;
3326 }
3327
Michal Vaskof03ed032020-03-04 13:31:44 +01003328 if (key_or_value && !val_len) {
3329 val_len = strlen(key_or_value);
3330 }
3331
Michal Vaskob104f112020-07-17 09:54:54 +02003332 if ((schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) && key_or_value) {
3333 /* create a data node and find the instance */
3334 if (schema->nodetype == LYS_LEAFLIST) {
3335 /* target used attributes: schema, hash, value */
Radek Krejci1798aae2020-07-14 13:26:06 +02003336 rc = lyd_create_term(schema, key_or_value, val_len, NULL, 0, lydjson_resolve_prefix, NULL, LYD_JSON, &target);
Michal Vaskob104f112020-07-17 09:54:54 +02003337 LY_CHECK_RET(rc && (rc != LY_EINCOMPLETE), rc);
3338 } else {
Michal Vasko90932a92020-02-12 14:33:03 +01003339 /* target used attributes: schema, hash, child (all keys) */
Michal Vasko004d3152020-06-11 19:59:22 +02003340 LY_CHECK_RET(lyd_create_list2(schema, key_or_value, val_len, &target));
Michal Vasko90932a92020-02-12 14:33:03 +01003341 }
3342
3343 /* find it */
3344 rc = lyd_find_sibling_first(siblings, target, match);
Michal Vaskob104f112020-07-17 09:54:54 +02003345 } else {
3346 /* find the first schema node instance */
3347 rc = lyd_find_sibling_schema(siblings, schema, match);
Michal Vaskoe444f752020-02-10 12:20:06 +01003348 }
3349
Michal Vaskoe444f752020-02-10 12:20:06 +01003350 lyd_free_tree(target);
3351 return rc;
3352}
Michal Vaskoccc02342020-05-21 10:09:21 +02003353
3354API LY_ERR
3355lyd_find_xpath(const struct lyd_node *ctx_node, const char *xpath, struct ly_set **set)
3356{
3357 LY_ERR ret = LY_SUCCESS;
3358 struct lyxp_set xp_set;
3359 struct lyxp_expr *exp;
3360 uint32_t i;
3361
3362 LY_CHECK_ARG_RET(NULL, ctx_node, xpath, set, LY_EINVAL);
3363
3364 memset(&xp_set, 0, sizeof xp_set);
3365
3366 /* compile expression */
Michal Vasko004d3152020-06-11 19:59:22 +02003367 exp = lyxp_expr_parse((struct ly_ctx *)LYD_NODE_CTX(ctx_node), xpath, 0, 1);
Michal Vaskoccc02342020-05-21 10:09:21 +02003368 LY_CHECK_ERR_GOTO(!exp, ret = LY_EINVAL, cleanup);
3369
3370 /* evaluate expression */
3371 ret = lyxp_eval(exp, LYD_JSON, ctx_node->schema->module, ctx_node, LYXP_NODE_ELEM, ctx_node, &xp_set, 0);
3372 LY_CHECK_GOTO(ret, cleanup);
3373
3374 /* allocate return set */
3375 *set = ly_set_new();
3376 LY_CHECK_ERR_GOTO(!*set, LOGMEM(LYD_NODE_CTX(ctx_node)); ret = LY_EMEM, cleanup);
3377
3378 /* transform into ly_set */
3379 if (xp_set.type == LYXP_SET_NODE_SET) {
3380 /* allocate memory for all the elements once (even though not all items must be elements but most likely will be) */
3381 (*set)->objs = malloc(xp_set.used * sizeof *(*set)->objs);
3382 LY_CHECK_ERR_GOTO(!(*set)->objs, LOGMEM(LYD_NODE_CTX(ctx_node)); ret = LY_EMEM, cleanup);
3383 (*set)->size = xp_set.used;
3384
3385 for (i = 0; i < xp_set.used; ++i) {
3386 if (xp_set.val.nodes[i].type == LYXP_NODE_ELEM) {
3387 ly_set_add(*set, xp_set.val.nodes[i].node, LY_SET_OPT_USEASLIST);
3388 }
3389 }
3390 }
3391
3392cleanup:
Michal Vasko0691d522020-05-21 13:21:47 +02003393 lyxp_set_free_content(&xp_set);
Michal Vaskoccc02342020-05-21 10:09:21 +02003394 lyxp_expr_free((struct ly_ctx *)LYD_NODE_CTX(ctx_node), exp);
3395 return ret;
3396}