blob: a5572d7d1ea9002114613d94b1c87e5512c034d0 [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 Vasko90932a92020-02-12 14:33:03 +01002104LY_ERR
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);
2110 assert(meta);
2111
2112 for (iter = meta; iter; iter = iter->next) {
2113 iter->parent = parent;
2114 }
2115
2116 /* insert as the last attribute */
2117 if (parent->meta) {
2118 for (last = parent->meta; last->next; last = last->next);
2119 last->next = meta;
2120 } else {
2121 parent->meta = meta;
2122 }
2123
2124 /* remove default flags from NP containers */
2125 while (parent && (parent->schema->nodetype == LYS_CONTAINER) && (parent->flags & LYD_DEFAULT)) {
2126 parent->flags &= ~LYD_DEFAULT;
2127 parent = (struct lyd_node *)parent->parent;
2128 }
2129
2130 return LY_SUCCESS;
2131}
2132
2133LY_ERR
Michal Vasko9f96a052020-03-10 09:41:45 +01002134lyd_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 +02002135 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 +01002136 void *prefix_data, LYD_FORMAT format, const struct lysc_node *ctx_snode)
Michal Vasko90932a92020-02-12 14:33:03 +01002137{
2138 LY_ERR ret;
2139 struct lysc_ext_instance *ant = NULL;
Michal Vasko9f96a052020-03-10 09:41:45 +01002140 struct lyd_meta *mt, *last;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002141 LY_ARRAY_COUNT_TYPE u;
Michal Vasko90932a92020-02-12 14:33:03 +01002142
Michal Vasko9f96a052020-03-10 09:41:45 +01002143 assert((parent || meta) && mod);
Michal Vasko6f4cbb62020-02-28 11:15:47 +01002144
Radek Krejci7eb54ba2020-05-18 16:30:04 +02002145 LY_ARRAY_FOR(mod->compiled->exts, u) {
2146 if (mod->compiled->exts[u].def->plugin == lyext_plugins_internal[LYEXT_PLUGIN_INTERNAL_ANNOTATION].plugin &&
2147 !ly_strncmp(mod->compiled->exts[u].argument, name, name_len)) {
Michal Vasko90932a92020-02-12 14:33:03 +01002148 /* we have the annotation definition */
Radek Krejci7eb54ba2020-05-18 16:30:04 +02002149 ant = &mod->compiled->exts[u];
Michal Vasko90932a92020-02-12 14:33:03 +01002150 break;
2151 }
2152 }
2153 if (!ant) {
2154 /* attribute is not defined as a metadata annotation (RFC 7952) */
2155 LOGERR(mod->ctx, LY_EINVAL, "Annotation definition for attribute \"%s:%.*s\" not found.",
2156 mod->name, name_len, name);
2157 return LY_EINVAL;
2158 }
2159
Michal Vasko9f96a052020-03-10 09:41:45 +01002160 mt = calloc(1, sizeof *mt);
2161 LY_CHECK_ERR_RET(!mt, LOGMEM(mod->ctx), LY_EMEM);
2162 mt->parent = parent;
2163 mt->annotation = ant;
Radek Krejci1798aae2020-07-14 13:26:06 +02002164 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 +01002165 if ((ret != LY_SUCCESS) && (ret != LY_EINCOMPLETE)) {
Michal Vasko9f96a052020-03-10 09:41:45 +01002166 free(mt);
Michal Vasko90932a92020-02-12 14:33:03 +01002167 return ret;
2168 }
Michal Vasko9f96a052020-03-10 09:41:45 +01002169 mt->name = lydict_insert(mod->ctx, name, name_len);
Michal Vasko90932a92020-02-12 14:33:03 +01002170
Michal Vasko6f4cbb62020-02-28 11:15:47 +01002171 /* insert as the last attribute */
2172 if (parent) {
Radek Krejci1798aae2020-07-14 13:26:06 +02002173 LY_CHECK_RET(lyd_insert_meta(parent, mt))
Michal Vasko9f96a052020-03-10 09:41:45 +01002174 } else if (*meta) {
2175 for (last = *meta; last->next; last = last->next);
2176 last->next = mt;
Michal Vasko90932a92020-02-12 14:33:03 +01002177 }
2178
Michal Vasko9f96a052020-03-10 09:41:45 +01002179 if (meta) {
2180 *meta = mt;
Michal Vasko90932a92020-02-12 14:33:03 +01002181 }
2182 return ret;
2183}
2184
Michal Vasko52927e22020-03-16 17:26:14 +01002185LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +02002186lyd_create_attr(struct lyd_node *parent, struct lyd_attr **attr, const struct ly_ctx *ctx, const char *name,
2187 size_t name_len, const char *value, size_t value_len, int *dynamic, int value_hint, LYD_FORMAT format,
2188 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 +01002189{
Radek Krejci1798aae2020-07-14 13:26:06 +02002190 struct lyd_attr *at, *last;
Michal Vasko52927e22020-03-16 17:26:14 +01002191 struct lyd_node_opaq *opaq;
2192
2193 assert(ctx && (parent || attr) && (!parent || !parent->schema));
2194 assert(name && name_len);
Michal Vasko52927e22020-03-16 17:26:14 +01002195
2196 if (!value_len) {
2197 value = "";
2198 }
2199
2200 at = calloc(1, sizeof *at);
2201 LY_CHECK_ERR_RET(!at, LOGMEM(ctx), LY_EMEM);
2202 at->parent = (struct lyd_node_opaq *)parent;
2203 at->name = lydict_insert(ctx, name, name_len);
2204 if (dynamic && *dynamic) {
2205 at->value = lydict_insert_zc(ctx, (char *)value);
2206 *dynamic = 0;
2207 } else {
2208 at->value = lydict_insert(ctx, value, value_len);
2209 }
2210
Radek Krejci1798aae2020-07-14 13:26:06 +02002211 at->hint = value_hint;
Michal Vasko52927e22020-03-16 17:26:14 +01002212 at->format = format;
2213 at->val_prefs = val_prefs;
Radek Krejci1798aae2020-07-14 13:26:06 +02002214 if (prefix_len) {
2215 at->prefix.id = lydict_insert(ctx, prefix, prefix_len);
2216 }
2217 if (module_key_len) {
2218 at->prefix.module_ns = lydict_insert(ctx, module_key, module_key_len);
Michal Vasko52927e22020-03-16 17:26:14 +01002219 }
2220
2221 /* insert as the last attribute */
2222 if (parent) {
2223 opaq = (struct lyd_node_opaq *)parent;
2224 if (opaq->attr) {
2225 for (last = opaq->attr; last->next; last = last->next);
2226 last->next = at;
2227 } else {
2228 opaq->attr = at;
2229 }
2230 } else if (*attr) {
2231 for (last = *attr; last->next; last = last->next);
2232 last->next = at;
2233 }
2234
2235 if (attr) {
2236 *attr = at;
2237 }
2238 return LY_SUCCESS;
2239}
2240
Radek Krejci084289f2019-07-09 17:35:30 +02002241API const struct lyd_node_term *
Michal Vasko004d3152020-06-11 19:59:22 +02002242lyd_target(const struct ly_path *path, const struct lyd_node *tree)
Radek Krejci084289f2019-07-09 17:35:30 +02002243{
Michal Vasko004d3152020-06-11 19:59:22 +02002244 struct lyd_node *target;
Radek Krejci084289f2019-07-09 17:35:30 +02002245
Michal Vasko004d3152020-06-11 19:59:22 +02002246 if (ly_path_eval(path, tree, &target)) {
2247 return NULL;
Radek Krejci084289f2019-07-09 17:35:30 +02002248 }
2249
Michal Vasko004d3152020-06-11 19:59:22 +02002250 return (struct lyd_node_term *)target;
Radek Krejci084289f2019-07-09 17:35:30 +02002251}
2252
Radek Krejci1f05b6a2019-07-18 16:15:06 +02002253API LY_ERR
Michal Vasko8f359bf2020-07-28 10:41:15 +02002254lyd_compare_single(const struct lyd_node *node1, const struct lyd_node *node2, int options)
Radek Krejci1f05b6a2019-07-18 16:15:06 +02002255{
2256 const struct lyd_node *iter1, *iter2;
2257 struct lyd_node_term *term1, *term2;
2258 struct lyd_node_any *any1, *any2;
Michal Vasko52927e22020-03-16 17:26:14 +01002259 struct lyd_node_opaq *opaq1, *opaq2;
Radek Krejci1f05b6a2019-07-18 16:15:06 +02002260 size_t len1, len2;
Radek Krejci084289f2019-07-09 17:35:30 +02002261
Radek Krejci1f05b6a2019-07-18 16:15:06 +02002262 if (!node1 || !node2) {
2263 if (node1 == node2) {
2264 return LY_SUCCESS;
2265 } else {
2266 return LY_ENOT;
2267 }
2268 }
2269
Michal Vasko52927e22020-03-16 17:26:14 +01002270 if ((LYD_NODE_CTX(node1) != LYD_NODE_CTX(node2)) || (node1->schema != node2->schema)) {
Radek Krejci1f05b6a2019-07-18 16:15:06 +02002271 return LY_ENOT;
2272 }
2273
2274 if (node1->hash != node2->hash) {
2275 return LY_ENOT;
2276 }
Michal Vasko52927e22020-03-16 17:26:14 +01002277 /* 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 +02002278
Michal Vasko52927e22020-03-16 17:26:14 +01002279 if (!node1->schema) {
2280 opaq1 = (struct lyd_node_opaq *)node1;
2281 opaq2 = (struct lyd_node_opaq *)node2;
Radek Krejci1798aae2020-07-14 13:26:06 +02002282 if ((opaq1->name != opaq2->name) || (opaq1->format != opaq2->format) || (opaq1->prefix.module_ns != opaq2->prefix.module_ns)) {
Radek Krejci1f05b6a2019-07-18 16:15:06 +02002283 return LY_ENOT;
2284 }
Michal Vasko52927e22020-03-16 17:26:14 +01002285 switch (opaq1->format) {
2286 case LYD_XML:
2287 if (lyxml_value_compare(opaq1->value, opaq1->val_prefs, opaq2->value, opaq2->val_prefs)) {
2288 return LY_ENOT;
2289 }
2290 break;
Radek Krejci1798aae2020-07-14 13:26:06 +02002291 case LYD_JSON:
2292 /* prefixes in JSON are unique, so it is not necessary to canonize the values */
2293 if (strcmp(opaq1->value, opaq2->value)) {
2294 return LY_ENOT;
2295 }
2296 break;
Michal Vasko52927e22020-03-16 17:26:14 +01002297 case LYD_SCHEMA:
Michal Vasko60ea6352020-06-29 13:39:39 +02002298 case LYD_LYB:
Michal Vasko52927e22020-03-16 17:26:14 +01002299 /* not allowed */
2300 LOGINT(LYD_NODE_CTX(node1));
2301 return LY_EINT;
2302 }
2303 if (options & LYD_COMPARE_FULL_RECURSION) {
2304 iter1 = opaq1->child;
2305 iter2 = opaq2->child;
Radek Krejci1f05b6a2019-07-18 16:15:06 +02002306 goto all_children_compare;
Michal Vasko52927e22020-03-16 17:26:14 +01002307 }
2308 return LY_SUCCESS;
2309 } else {
2310 switch (node1->schema->nodetype) {
2311 case LYS_LEAF:
2312 case LYS_LEAFLIST:
2313 if (options & LYD_COMPARE_DEFAULTS) {
2314 if ((node1->flags & LYD_DEFAULT) != (node2->flags & LYD_DEFAULT)) {
2315 return LY_ENOT;
2316 }
2317 }
2318
Michal Vasko1a2bf2e2020-06-16 13:07:48 +02002319 term1 = (struct lyd_node_term *)node1;
2320 term2 = (struct lyd_node_term *)node2;
2321 if (term1->value.realtype != term2->value.realtype) {
2322 return LY_ENOT;
2323 }
Michal Vasko52927e22020-03-16 17:26:14 +01002324
Michal Vasko1a2bf2e2020-06-16 13:07:48 +02002325 return term1->value.realtype->plugin->compare(&term1->value, &term2->value);
Michal Vasko52927e22020-03-16 17:26:14 +01002326 case LYS_CONTAINER:
2327 if (options & LYD_COMPARE_DEFAULTS) {
2328 if ((node1->flags & LYD_DEFAULT) != (node2->flags & LYD_DEFAULT)) {
2329 return LY_ENOT;
2330 }
2331 }
2332 if (options & LYD_COMPARE_FULL_RECURSION) {
2333 iter1 = ((struct lyd_node_inner*)node1)->child;
2334 iter2 = ((struct lyd_node_inner*)node2)->child;
2335 goto all_children_compare;
Radek Krejci1f05b6a2019-07-18 16:15:06 +02002336 }
2337 return LY_SUCCESS;
Michal Vasko1bf09392020-03-27 12:38:10 +01002338 case LYS_RPC:
Michal Vasko52927e22020-03-16 17:26:14 +01002339 case LYS_ACTION:
2340 if (options & LYD_COMPARE_FULL_RECURSION) {
2341 /* TODO action/RPC
2342 goto all_children_compare;
2343 */
Radek Krejci1f05b6a2019-07-18 16:15:06 +02002344 }
2345 return LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +01002346 case LYS_NOTIF:
2347 if (options & LYD_COMPARE_FULL_RECURSION) {
2348 /* TODO Notification
2349 goto all_children_compare;
2350 */
2351 }
2352 return LY_SUCCESS;
2353 case LYS_LIST:
2354 iter1 = ((struct lyd_node_inner*)node1)->child;
2355 iter2 = ((struct lyd_node_inner*)node2)->child;
2356
2357 if (!(node1->schema->flags & LYS_KEYLESS) && !(options & LYD_COMPARE_FULL_RECURSION)) {
2358 /* lists with keys, their equivalence is based on their keys */
2359 for (struct lysc_node *key = ((struct lysc_node_list*)node1->schema)->child;
Michal Vaskoc57d9a92020-06-23 13:28:27 +02002360 key && (key->flags & LYS_KEY);
Michal Vasko52927e22020-03-16 17:26:14 +01002361 key = key->next) {
Michal Vasko8f359bf2020-07-28 10:41:15 +02002362 if (lyd_compare_single(iter1, iter2, options)) {
Michal Vasko52927e22020-03-16 17:26:14 +01002363 return LY_ENOT;
2364 }
2365 iter1 = iter1->next;
2366 iter2 = iter2->next;
2367 }
2368 } else {
2369 /* lists without keys, their equivalence is based on equivalence of all the children (both direct and indirect) */
2370
2371 all_children_compare:
2372 if (!iter1 && !iter2) {
2373 /* no children, nothing to compare */
2374 return LY_SUCCESS;
2375 }
2376
2377 for (; iter1 && iter2; iter1 = iter1->next, iter2 = iter2->next) {
Michal Vasko8f359bf2020-07-28 10:41:15 +02002378 if (lyd_compare_single(iter1, iter2, options | LYD_COMPARE_FULL_RECURSION)) {
Michal Vasko52927e22020-03-16 17:26:14 +01002379 return LY_ENOT;
2380 }
2381 }
2382 if (iter1 || iter2) {
2383 return LY_ENOT;
2384 }
2385 }
2386 return LY_SUCCESS;
2387 case LYS_ANYXML:
2388 case LYS_ANYDATA:
2389 any1 = (struct lyd_node_any*)node1;
2390 any2 = (struct lyd_node_any*)node2;
2391
2392 if (any1->value_type != any2->value_type) {
2393 return LY_ENOT;
2394 }
2395 switch (any1->value_type) {
2396 case LYD_ANYDATA_DATATREE:
2397 iter1 = any1->value.tree;
2398 iter2 = any2->value.tree;
2399 goto all_children_compare;
2400 case LYD_ANYDATA_STRING:
2401 case LYD_ANYDATA_XML:
2402 case LYD_ANYDATA_JSON:
2403 len1 = strlen(any1->value.str);
2404 len2 = strlen(any2->value.str);
2405 if (len1 != len2 || strcmp(any1->value.str, any2->value.str)) {
2406 return LY_ENOT;
2407 }
2408 return LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +01002409 case LYD_ANYDATA_LYB:
Michal Vasko60ea6352020-06-29 13:39:39 +02002410 len1 = lyd_lyb_data_length(any1->value.mem);
2411 len2 = lyd_lyb_data_length(any2->value.mem);
Michal Vasko52927e22020-03-16 17:26:14 +01002412 if (len1 != len2 || memcmp(any1->value.mem, any2->value.mem, len1)) {
2413 return LY_ENOT;
2414 }
2415 return LY_SUCCESS;
Michal Vasko52927e22020-03-16 17:26:14 +01002416 }
Radek Krejci1f05b6a2019-07-18 16:15:06 +02002417 }
2418 }
2419
Michal Vasko52927e22020-03-16 17:26:14 +01002420 LOGINT(LYD_NODE_CTX(node1));
Radek Krejci1f05b6a2019-07-18 16:15:06 +02002421 return LY_EINT;
2422}
Radek Krejci22ebdba2019-07-25 13:59:43 +02002423
Michal Vasko21725742020-06-29 11:49:25 +02002424API LY_ERR
Michal Vasko8f359bf2020-07-28 10:41:15 +02002425lyd_compare_siblings(const struct lyd_node *node1, const struct lyd_node *node2, int options)
2426{
2427 for (; node1 && node2; node1 = node1->next, node2 = node2->next) {
2428 LY_CHECK_RET(lyd_compare_single(node1, node2, options));
2429 }
2430
Michal Vasko11a81432020-07-28 16:31:29 +02002431 if (node1 == node2) {
2432 return LY_SUCCESS;
Michal Vasko8f359bf2020-07-28 10:41:15 +02002433 }
Michal Vasko11a81432020-07-28 16:31:29 +02002434 return LY_ENOT;
Michal Vasko8f359bf2020-07-28 10:41:15 +02002435}
2436
2437API LY_ERR
Michal Vasko21725742020-06-29 11:49:25 +02002438lyd_compare_meta(const struct lyd_meta *meta1, const struct lyd_meta *meta2)
2439{
2440 if (!meta1 || !meta2) {
2441 if (meta1 == meta2) {
2442 return LY_SUCCESS;
2443 } else {
2444 return LY_ENOT;
2445 }
2446 }
2447
2448 if ((LYD_NODE_CTX(meta1->parent) != LYD_NODE_CTX(meta2->parent)) || (meta1->annotation != meta2->annotation)) {
2449 return LY_ENOT;
2450 }
2451
2452 if (meta1->value.realtype != meta2->value.realtype) {
2453 return LY_ENOT;
2454 }
2455
2456 return meta1->value.realtype->plugin->compare(&meta1->value, &meta2->value);
2457}
2458
Radek Krejci22ebdba2019-07-25 13:59:43 +02002459/**
Michal Vasko52927e22020-03-16 17:26:14 +01002460 * @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 +02002461 *
2462 * 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 +02002463 *
2464 * @param[in] node Original node to duplicate
2465 * @param[in] parent Parent to insert into, NULL for top-level sibling.
2466 * @param[in,out] first First sibling, NULL if no top-level sibling exist yet. Can be also NULL if @p parent is set.
2467 * @param[in] options Bitmask of options flags, see @ref dupoptions.
2468 * @param[out] dup_p Pointer where the created duplicated node is placed (besides connecting it int @p parent / @p first sibling).
2469 * @return LY_ERR value
Radek Krejci22ebdba2019-07-25 13:59:43 +02002470 */
Michal Vasko52927e22020-03-16 17:26:14 +01002471static LY_ERR
Michal Vasko3a41dff2020-07-15 14:30:28 +02002472lyd_dup_r(const struct lyd_node *node, struct lyd_node *parent, struct lyd_node **first, int options,
2473 struct lyd_node **dup_p)
Radek Krejci22ebdba2019-07-25 13:59:43 +02002474{
Michal Vasko52927e22020-03-16 17:26:14 +01002475 LY_ERR ret;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002476 struct lyd_node *dup = NULL;
Michal Vasko61551fa2020-07-09 15:45:45 +02002477 struct lyd_meta *meta;
2478 struct lyd_node_any *any;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002479 LY_ARRAY_COUNT_TYPE u;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002480
Michal Vasko52927e22020-03-16 17:26:14 +01002481 LY_CHECK_ARG_RET(NULL, node, LY_EINVAL);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002482
Michal Vasko52927e22020-03-16 17:26:14 +01002483 if (!node->schema) {
2484 dup = calloc(1, sizeof(struct lyd_node_opaq));
2485 } else {
2486 switch (node->schema->nodetype) {
Michal Vasko1bf09392020-03-27 12:38:10 +01002487 case LYS_RPC:
Michal Vasko52927e22020-03-16 17:26:14 +01002488 case LYS_ACTION:
2489 case LYS_NOTIF:
2490 case LYS_CONTAINER:
2491 case LYS_LIST:
2492 dup = calloc(1, sizeof(struct lyd_node_inner));
2493 break;
2494 case LYS_LEAF:
2495 case LYS_LEAFLIST:
2496 dup = calloc(1, sizeof(struct lyd_node_term));
2497 break;
2498 case LYS_ANYDATA:
2499 case LYS_ANYXML:
2500 dup = calloc(1, sizeof(struct lyd_node_any));
2501 break;
2502 default:
2503 LOGINT(LYD_NODE_CTX(node));
2504 ret = LY_EINT;
2505 goto error;
2506 }
Radek Krejci22ebdba2019-07-25 13:59:43 +02002507 }
Michal Vasko52927e22020-03-16 17:26:14 +01002508 LY_CHECK_ERR_GOTO(!dup, LOGMEM(LYD_NODE_CTX(node)); ret = LY_EMEM, error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002509
Michal Vaskof6df0a02020-06-16 13:08:34 +02002510 if (options & LYD_DUP_WITH_FLAGS) {
2511 dup->flags = node->flags;
2512 } else {
2513 dup->flags = (node->flags & LYD_DEFAULT) | LYD_NEW;
2514 }
Radek Krejci22ebdba2019-07-25 13:59:43 +02002515 dup->schema = node->schema;
Michal Vasko52927e22020-03-16 17:26:14 +01002516 dup->prev = dup;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002517
Michal Vasko25a32822020-07-09 15:48:22 +02002518 /* duplicate metadata */
2519 if (!(options & LYD_DUP_NO_META)) {
2520 LY_LIST_FOR(node->meta, meta) {
Michal Vasko3a41dff2020-07-15 14:30:28 +02002521 LY_CHECK_GOTO(ret = lyd_dup_meta_single(meta, dup, NULL), error);
Michal Vasko25a32822020-07-09 15:48:22 +02002522 }
2523 }
Radek Krejci22ebdba2019-07-25 13:59:43 +02002524
2525 /* nodetype-specific work */
Michal Vasko52927e22020-03-16 17:26:14 +01002526 if (!dup->schema) {
2527 struct lyd_node_opaq *opaq = (struct lyd_node_opaq *)dup;
2528 struct lyd_node_opaq *orig = (struct lyd_node_opaq *)node;
2529 struct lyd_node *child;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002530
2531 if (options & LYD_DUP_RECURSIVE) {
2532 /* duplicate all the children */
2533 LY_LIST_FOR(orig->child, child) {
Michal Vasko3a41dff2020-07-15 14:30:28 +02002534 LY_CHECK_GOTO(ret = lyd_dup_r(child, dup, NULL, options, NULL), error);
Michal Vasko52927e22020-03-16 17:26:14 +01002535 }
2536 }
2537 opaq->name = lydict_insert(LYD_NODE_CTX(node), orig->name, 0);
2538 opaq->format = orig->format;
Radek Krejci1798aae2020-07-14 13:26:06 +02002539 if (orig->prefix.id) {
2540 opaq->prefix.id = lydict_insert(LYD_NODE_CTX(node), orig->prefix.id, 0);
Michal Vasko52927e22020-03-16 17:26:14 +01002541 }
Radek Krejci1798aae2020-07-14 13:26:06 +02002542 opaq->prefix.module_ns = lydict_insert(LYD_NODE_CTX(node), orig->prefix.module_ns, 0);
Michal Vasko52927e22020-03-16 17:26:14 +01002543 if (orig->val_prefs) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02002544 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 +01002545 LY_ARRAY_FOR(orig->val_prefs, u) {
Radek Krejci1798aae2020-07-14 13:26:06 +02002546 opaq->val_prefs[u].id = lydict_insert(LYD_NODE_CTX(node), orig->val_prefs[u].id, 0);
2547 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 +01002548 LY_ARRAY_INCREMENT(opaq->val_prefs);
2549 }
2550 }
2551 opaq->value = lydict_insert(LYD_NODE_CTX(node), orig->value, 0);
2552 opaq->ctx = orig->ctx;
2553 } else if (dup->schema->nodetype & LYD_NODE_TERM) {
2554 struct lyd_node_term *term = (struct lyd_node_term *)dup;
2555 struct lyd_node_term *orig = (struct lyd_node_term *)node;
2556
2557 term->hash = orig->hash;
2558 term->value.realtype = orig->value.realtype;
2559 LY_CHECK_ERR_GOTO(term->value.realtype->plugin->duplicate(LYD_NODE_CTX(node), &orig->value, &term->value),
2560 LOGERR(LYD_NODE_CTX(node), LY_EINT, "Value duplication failed."); ret = LY_EINT, error);
2561 } else if (dup->schema->nodetype & LYD_NODE_INNER) {
2562 struct lyd_node_inner *orig = (struct lyd_node_inner *)node;
2563 struct lyd_node *child;
2564
2565 if (options & LYD_DUP_RECURSIVE) {
2566 /* duplicate all the children */
2567 LY_LIST_FOR(orig->child, child) {
Michal Vasko3a41dff2020-07-15 14:30:28 +02002568 LY_CHECK_GOTO(ret = lyd_dup_r(child, dup, NULL, options, NULL), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002569 }
Radek Krejci0fe9b512019-07-26 17:51:05 +02002570 } else if (dup->schema->nodetype == LYS_LIST && !(dup->schema->flags & LYS_KEYLESS)) {
Radek Krejci22ebdba2019-07-25 13:59:43 +02002571 /* always duplicate keys of a list */
Radek Krejci22ebdba2019-07-25 13:59:43 +02002572 child = orig->child;
Michal Vasko52927e22020-03-16 17:26:14 +01002573 for (struct lysc_node *key = ((struct lysc_node_list *)dup->schema)->child;
Michal Vaskoc57d9a92020-06-23 13:28:27 +02002574 key && (key->flags & LYS_KEY);
Radek Krejci0fe9b512019-07-26 17:51:05 +02002575 key = key->next) {
Radek Krejci22ebdba2019-07-25 13:59:43 +02002576 if (!child) {
2577 /* possibly not keys are present in filtered tree */
2578 break;
Radek Krejci0fe9b512019-07-26 17:51:05 +02002579 } else if (child->schema != key) {
2580 /* possibly not all keys are present in filtered tree,
2581 * but there can be also some non-key nodes */
2582 continue;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002583 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02002584 LY_CHECK_GOTO(ret = lyd_dup_r(child, dup, NULL, options, NULL), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002585 child = child->next;
2586 }
2587 }
2588 lyd_hash(dup);
2589 } else if (dup->schema->nodetype & LYD_NODE_ANY) {
Michal Vasko61551fa2020-07-09 15:45:45 +02002590 dup->hash = node->hash;
2591 any = (struct lyd_node_any *)node;
2592 LY_CHECK_GOTO(ret = lyd_any_copy_value(dup, &any->value, any->value_type), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002593 }
2594
Michal Vasko52927e22020-03-16 17:26:14 +01002595 /* insert */
2596 lyd_insert_node(parent, first, dup);
Michal Vasko52927e22020-03-16 17:26:14 +01002597
2598 if (dup_p) {
2599 *dup_p = dup;
2600 }
2601 return LY_SUCCESS;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002602
2603error:
Michal Vasko52927e22020-03-16 17:26:14 +01002604 lyd_free_tree(dup);
2605 return ret;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002606}
2607
Michal Vasko3a41dff2020-07-15 14:30:28 +02002608static LY_ERR
2609lyd_dup_get_local_parent(const struct lyd_node *node, const struct lyd_node_inner *parent, struct lyd_node **dup_parent,
2610 struct lyd_node_inner **local_parent)
Radek Krejci22ebdba2019-07-25 13:59:43 +02002611{
Michal Vasko3a41dff2020-07-15 14:30:28 +02002612 const struct lyd_node_inner *orig_parent, *iter;
2613 int repeat = 1;
2614
2615 *dup_parent = NULL;
2616 *local_parent = NULL;
2617
2618 for (orig_parent = node->parent; repeat && orig_parent; orig_parent = orig_parent->parent) {
2619 if (parent && (parent->schema == orig_parent->schema)) {
2620 /* stop creating parents, connect what we have into the provided parent */
2621 iter = parent;
2622 repeat = 0;
2623 } else {
2624 iter = NULL;
2625 LY_CHECK_RET(lyd_dup_r((struct lyd_node *)orig_parent, NULL, (struct lyd_node **)&iter, 0,
2626 (struct lyd_node **)&iter));
2627 }
2628 if (!*local_parent) {
2629 *local_parent = (struct lyd_node_inner *)iter;
2630 }
2631 if (iter->child) {
2632 /* 1) list - add after keys
2633 * 2) provided parent with some children */
2634 iter->child->prev->next = *dup_parent;
2635 if (*dup_parent) {
2636 (*dup_parent)->prev = iter->child->prev;
2637 iter->child->prev = *dup_parent;
2638 }
2639 } else {
2640 ((struct lyd_node_inner *)iter)->child = *dup_parent;
2641 }
2642 if (*dup_parent) {
2643 (*dup_parent)->parent = (struct lyd_node_inner *)iter;
2644 }
2645 *dup_parent = (struct lyd_node *)iter;
2646 }
2647
2648 if (repeat && parent) {
2649 /* given parent and created parents chain actually do not interconnect */
2650 LOGERR(LYD_NODE_CTX(node), LY_EINVAL,
2651 "Invalid argument parent (%s()) - does not interconnect with the created node's parents chain.", __func__);
2652 return LY_EINVAL;
2653 }
2654
2655 return LY_SUCCESS;
2656}
2657
2658static LY_ERR
2659lyd_dup(const struct lyd_node *node, struct lyd_node_inner *parent, int options, int nosiblings, struct lyd_node **dup)
2660{
2661 LY_ERR rc;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002662 const struct lyd_node *orig; /* original node to be duplicated */
2663 struct lyd_node *first = NULL; /* the first duplicated node, this is returned */
Radek Krejci22ebdba2019-07-25 13:59:43 +02002664 struct lyd_node *top = NULL; /* the most higher created node */
2665 struct lyd_node_inner *local_parent = NULL; /* the direct parent node for the duplicated node(s) */
Radek Krejci22ebdba2019-07-25 13:59:43 +02002666
Michal Vasko3a41dff2020-07-15 14:30:28 +02002667 LY_CHECK_ARG_RET(NULL, node, LY_EINVAL);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002668
2669 if (options & LYD_DUP_WITH_PARENTS) {
Michal Vasko3a41dff2020-07-15 14:30:28 +02002670 LY_CHECK_GOTO(rc = lyd_dup_get_local_parent(node, parent, &top, &local_parent), error);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002671 } else {
2672 local_parent = parent;
2673 }
2674
Radek Krejci22ebdba2019-07-25 13:59:43 +02002675 LY_LIST_FOR(node, orig) {
Michal Vasko52927e22020-03-16 17:26:14 +01002676 /* if there is no local parent, it will be inserted into first */
Michal Vasko3a41dff2020-07-15 14:30:28 +02002677 LY_CHECK_GOTO(rc = lyd_dup_r(orig, (struct lyd_node *)local_parent, &first, options, first ? NULL : &first), error);
2678 if (nosiblings) {
Radek Krejci22ebdba2019-07-25 13:59:43 +02002679 break;
2680 }
2681 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02002682
2683 /* rehash if needed */
2684 for (; local_parent; local_parent = local_parent->parent) {
2685 if (local_parent->schema->nodetype == LYS_LIST && (local_parent->schema->flags & LYS_KEYLESS)) {
2686 lyd_hash((struct lyd_node *)local_parent);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002687 }
2688 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02002689
2690 if (dup) {
2691 *dup = first;
2692 }
2693 return LY_SUCCESS;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002694
2695error:
2696 if (top) {
2697 lyd_free_tree(top);
2698 } else {
Michal Vaskof03ed032020-03-04 13:31:44 +01002699 lyd_free_siblings(first);
Radek Krejci22ebdba2019-07-25 13:59:43 +02002700 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02002701 return rc;
Radek Krejci22ebdba2019-07-25 13:59:43 +02002702}
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002703
Michal Vasko3a41dff2020-07-15 14:30:28 +02002704API LY_ERR
2705lyd_dup_single(const struct lyd_node *node, struct lyd_node_inner *parent, int options, struct lyd_node **dup)
2706{
2707 return lyd_dup(node, parent, options, 1, dup);
2708}
2709
2710API LY_ERR
2711lyd_dup_siblings(const struct lyd_node *node, struct lyd_node_inner *parent, int options, struct lyd_node **dup)
2712{
2713 return lyd_dup(node, parent, options, 0, dup);
2714}
2715
2716API LY_ERR
2717lyd_dup_meta_single(const struct lyd_meta *meta, struct lyd_node *node, struct lyd_meta **dup)
Michal Vasko25a32822020-07-09 15:48:22 +02002718{
2719 LY_ERR ret;
2720 struct lyd_meta *mt, *last;
2721
Michal Vasko3a41dff2020-07-15 14:30:28 +02002722 LY_CHECK_ARG_RET(NULL, meta, node, LY_EINVAL);
Michal Vasko25a32822020-07-09 15:48:22 +02002723
2724 /* create a copy */
2725 mt = calloc(1, sizeof *mt);
Michal Vasko3a41dff2020-07-15 14:30:28 +02002726 LY_CHECK_ERR_RET(!mt, LOGMEM(LYD_NODE_CTX(node)), LY_EMEM);
Michal Vasko25a32822020-07-09 15:48:22 +02002727 mt->parent = node;
2728 mt->annotation = meta->annotation;
2729 mt->value.realtype = meta->value.realtype;
2730 ret = mt->value.realtype->plugin->duplicate(LYD_NODE_CTX(node), &meta->value, &mt->value);
Michal Vasko3a41dff2020-07-15 14:30:28 +02002731 LY_CHECK_ERR_RET(ret, LOGERR(LYD_NODE_CTX(node), LY_EINT, "Value duplication failed."), ret);
Michal Vasko25a32822020-07-09 15:48:22 +02002732 mt->name = lydict_insert(LYD_NODE_CTX(node), meta->name, 0);
2733
2734 /* insert as the last attribute */
2735 if (node->meta) {
2736 for (last = node->meta; last->next; last = last->next);
2737 last->next = mt;
2738 } else {
2739 node->meta = mt;
2740 }
2741
Michal Vasko3a41dff2020-07-15 14:30:28 +02002742 if (dup) {
2743 *dup = mt;
2744 }
2745 return LY_SUCCESS;
Michal Vasko25a32822020-07-09 15:48:22 +02002746}
2747
Michal Vasko4490d312020-06-16 13:08:55 +02002748/**
2749 * @brief Merge a source sibling into target siblings.
2750 *
2751 * @param[in,out] first_trg First target sibling, is updated if top-level.
2752 * @param[in] parent_trg Target parent.
2753 * @param[in,out] sibling_src Source sibling to merge, set to NULL if spent.
2754 * @param[in] options Merge options.
2755 * @return LY_ERR value.
2756 */
2757static LY_ERR
2758lyd_merge_sibling_r(struct lyd_node **first_trg, struct lyd_node *parent_trg, const struct lyd_node **sibling_src_p,
2759 int options)
2760{
2761 LY_ERR ret;
2762 const struct lyd_node *child_src, *tmp, *sibling_src;
Michal Vasko56daf732020-08-10 10:57:18 +02002763 struct lyd_node *match_trg, *dup_src, *elem;
Michal Vasko4490d312020-06-16 13:08:55 +02002764 struct lysc_type *type;
Michal Vasko4490d312020-06-16 13:08:55 +02002765
2766 sibling_src = *sibling_src_p;
2767 if (sibling_src->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) {
2768 /* try to find the exact instance */
2769 ret = lyd_find_sibling_first(*first_trg, sibling_src, &match_trg);
2770 } else {
2771 /* try to simply find the node, there cannot be more instances */
2772 ret = lyd_find_sibling_val(*first_trg, sibling_src->schema, NULL, 0, &match_trg);
2773 }
2774
2775 if (!ret) {
2776 /* node found, make sure even value matches for all node types */
Michal Vasko8f359bf2020-07-28 10:41:15 +02002777 if ((match_trg->schema->nodetype == LYS_LEAF) && lyd_compare_single(sibling_src, match_trg, LYD_COMPARE_DEFAULTS)) {
Michal Vasko4490d312020-06-16 13:08:55 +02002778 /* since they are different, they cannot both be default */
2779 assert(!(sibling_src->flags & LYD_DEFAULT) || !(match_trg->flags & LYD_DEFAULT));
2780
Michal Vasko3a41dff2020-07-15 14:30:28 +02002781 /* update value (or only LYD_DEFAULT flag) only if flag set or the source node is not default */
2782 if ((options & LYD_MERGE_DEFAULTS) || !(sibling_src->flags & LYD_DEFAULT)) {
Michal Vasko4490d312020-06-16 13:08:55 +02002783 type = ((struct lysc_node_leaf *)match_trg->schema)->type;
2784 type->plugin->free(LYD_NODE_CTX(match_trg), &((struct lyd_node_term *)match_trg)->value);
2785 LY_CHECK_RET(type->plugin->duplicate(LYD_NODE_CTX(match_trg), &((struct lyd_node_term *)sibling_src)->value,
2786 &((struct lyd_node_term *)match_trg)->value));
2787
2788 /* copy flags and add LYD_NEW */
2789 match_trg->flags = sibling_src->flags | LYD_NEW;
2790 }
Michal Vasko8f359bf2020-07-28 10:41:15 +02002791 } else if ((match_trg->schema->nodetype & LYS_ANYDATA) && lyd_compare_single(sibling_src, match_trg, 0)) {
Michal Vasko4c583e82020-07-17 12:16:14 +02002792 /* update value */
2793 LY_CHECK_RET(lyd_any_copy_value(match_trg, &((struct lyd_node_any *)sibling_src)->value,
2794 ((struct lyd_node_any *)sibling_src)->value_type));
Michal Vasko4490d312020-06-16 13:08:55 +02002795
2796 /* copy flags and add LYD_NEW */
2797 match_trg->flags = sibling_src->flags | LYD_NEW;
Michal Vasko4490d312020-06-16 13:08:55 +02002798 } else {
2799 /* check descendants, recursively */
Michal Vasko8ee464a2020-08-11 14:12:50 +02002800 LY_LIST_FOR_SAFE(LYD_CHILD_NO_KEYS(sibling_src), tmp, child_src) {
Michal Vasko4490d312020-06-16 13:08:55 +02002801 LY_CHECK_RET(lyd_merge_sibling_r(lyd_node_children_p(match_trg), match_trg, &child_src, options));
2802 }
2803 }
2804 } else {
2805 /* node not found, merge it */
2806 if (options & LYD_MERGE_DESTRUCT) {
2807 dup_src = (struct lyd_node *)sibling_src;
2808 lyd_unlink_tree(dup_src);
2809 /* spend it */
2810 *sibling_src_p = NULL;
2811 } else {
Michal Vasko3a41dff2020-07-15 14:30:28 +02002812 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 +02002813 }
2814
2815 /* set LYD_NEW for all the new nodes, required for validation */
Michal Vasko56daf732020-08-10 10:57:18 +02002816 LYD_TREE_DFS_BEGIN(dup_src, elem) {
Michal Vasko4490d312020-06-16 13:08:55 +02002817 elem->flags |= LYD_NEW;
Michal Vasko56daf732020-08-10 10:57:18 +02002818 LYD_TREE_DFS_END(dup_src, elem);
Michal Vasko4490d312020-06-16 13:08:55 +02002819 }
2820
2821 lyd_insert_node(parent_trg, first_trg, dup_src);
2822 }
2823
2824 return LY_SUCCESS;
2825}
2826
Michal Vasko3a41dff2020-07-15 14:30:28 +02002827static LY_ERR
2828lyd_merge(struct lyd_node **target, const struct lyd_node *source, int options, int nosiblings)
Michal Vasko4490d312020-06-16 13:08:55 +02002829{
2830 const struct lyd_node *sibling_src, *tmp;
2831 int first;
2832
2833 LY_CHECK_ARG_RET(NULL, target, LY_EINVAL);
2834
2835 if (!source) {
2836 /* nothing to merge */
2837 return LY_SUCCESS;
2838 }
2839
2840 if (lysc_data_parent((*target)->schema) || lysc_data_parent(source->schema)) {
2841 LOGERR(LYD_NODE_CTX(source), LY_EINVAL, "Invalid arguments - can merge only 2 top-level subtrees (%s()).", __func__);
2842 return LY_EINVAL;
2843 }
2844
2845 LY_LIST_FOR_SAFE(source, tmp, sibling_src) {
2846 first = sibling_src == source ? 1 : 0;
2847 LY_CHECK_RET(lyd_merge_sibling_r(target, NULL, &sibling_src, options));
2848 if (first && !sibling_src) {
2849 /* source was spent (unlinked), move to the next node */
2850 source = tmp;
2851 }
2852
Michal Vasko3a41dff2020-07-15 14:30:28 +02002853 if (nosiblings) {
Michal Vasko4490d312020-06-16 13:08:55 +02002854 break;
2855 }
2856 }
2857
2858 if (options & LYD_MERGE_DESTRUCT) {
2859 /* free any leftover source data that were not merged */
2860 lyd_free_siblings((struct lyd_node *)source);
2861 }
2862
2863 return LY_SUCCESS;
2864}
2865
Michal Vasko3a41dff2020-07-15 14:30:28 +02002866API LY_ERR
2867lyd_merge_tree(struct lyd_node **target, const struct lyd_node *source, int options)
2868{
2869 return lyd_merge(target, source, options, 1);
2870}
2871
2872API LY_ERR
2873lyd_merge_siblings(struct lyd_node **target, const struct lyd_node *source, int options)
2874{
2875 return lyd_merge(target, source, options, 0);
2876}
2877
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002878static LY_ERR
2879lyd_path_str_enlarge(char **buffer, size_t *buflen, size_t reqlen, int is_static)
2880{
Michal Vasko14654712020-02-06 08:35:21 +01002881 /* ending \0 */
2882 ++reqlen;
2883
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002884 if (reqlen > *buflen) {
2885 if (is_static) {
2886 return LY_EINCOMPLETE;
2887 }
2888
2889 *buffer = ly_realloc(*buffer, reqlen * sizeof **buffer);
2890 if (!*buffer) {
2891 return LY_EMEM;
2892 }
2893
2894 *buflen = reqlen;
2895 }
2896
2897 return LY_SUCCESS;
2898}
2899
Michal Vaskod59035b2020-07-08 12:00:06 +02002900LY_ERR
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002901lyd_path_list_predicate(const struct lyd_node *node, char **buffer, size_t *buflen, size_t *bufused, int is_static)
2902{
2903 const struct lyd_node *key;
2904 int dynamic = 0;
2905 size_t len;
2906 const char *val;
2907 char quot;
2908 LY_ERR rc;
2909
Michal Vasko5bfd4be2020-06-23 13:26:19 +02002910 for (key = lyd_node_children(node, 0); key && (key->schema->flags & LYS_KEY); key = key->next) {
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002911 val = lyd_value2str((struct lyd_node_term *)key, &dynamic);
2912 len = 1 + strlen(key->schema->name) + 2 + strlen(val) + 2;
2913 rc = lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static);
2914 if (rc != LY_SUCCESS) {
2915 if (dynamic) {
2916 free((char *)val);
2917 }
2918 return rc;
2919 }
2920
2921 quot = '\'';
2922 if (strchr(val, '\'')) {
2923 quot = '"';
2924 }
2925 *bufused += sprintf(*buffer + *bufused, "[%s=%c%s%c]", key->schema->name, quot, val, quot);
2926
2927 if (dynamic) {
2928 free((char *)val);
2929 }
2930 }
2931
2932 return LY_SUCCESS;
2933}
2934
2935/**
2936 * @brief Append leaf-list value predicate to path.
2937 *
2938 * @param[in] node Node to print.
2939 * @param[in,out] buffer Buffer to print to.
2940 * @param[in,out] buflen Current buffer length.
2941 * @param[in,out] bufused Current number of characters used in @p buffer.
2942 * @param[in] is_static Whether buffer is static or can be reallocated.
2943 * @return LY_ERR
2944 */
2945static LY_ERR
2946lyd_path_leaflist_predicate(const struct lyd_node *node, char **buffer, size_t *buflen, size_t *bufused, int is_static)
2947{
2948 int dynamic = 0;
2949 size_t len;
2950 const char *val;
2951 char quot;
2952 LY_ERR rc;
2953
2954 val = lyd_value2str((struct lyd_node_term *)node, &dynamic);
2955 len = 4 + strlen(val) + 2;
2956 rc = lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static);
2957 if (rc != LY_SUCCESS) {
2958 goto cleanup;
2959 }
2960
2961 quot = '\'';
2962 if (strchr(val, '\'')) {
2963 quot = '"';
2964 }
2965 *bufused += sprintf(*buffer + *bufused, "[.=%c%s%c]", quot, val, quot);
2966
2967cleanup:
2968 if (dynamic) {
2969 free((char *)val);
2970 }
2971 return rc;
2972}
2973
2974/**
2975 * @brief Append node position (relative to its other instances) predicate to path.
2976 *
2977 * @param[in] node Node to print.
2978 * @param[in,out] buffer Buffer to print to.
2979 * @param[in,out] buflen Current buffer length.
2980 * @param[in,out] bufused Current number of characters used in @p buffer.
2981 * @param[in] is_static Whether buffer is static or can be reallocated.
2982 * @return LY_ERR
2983 */
2984static LY_ERR
2985lyd_path_position_predicate(const struct lyd_node *node, char **buffer, size_t *buflen, size_t *bufused, int is_static)
2986{
2987 const struct lyd_node *first, *iter;
2988 size_t len;
2989 int pos;
2990 char *val = NULL;
2991 LY_ERR rc;
2992
2993 if (node->parent) {
2994 first = node->parent->child;
2995 } else {
2996 for (first = node; node->prev->next; node = node->prev);
2997 }
2998 pos = 1;
2999 for (iter = first; iter != node; iter = iter->next) {
3000 if (iter->schema == node->schema) {
3001 ++pos;
3002 }
3003 }
3004 if (asprintf(&val, "%d", pos) == -1) {
3005 return LY_EMEM;
3006 }
3007
3008 len = 1 + strlen(val) + 1;
3009 rc = lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static);
3010 if (rc != LY_SUCCESS) {
3011 goto cleanup;
3012 }
3013
3014 *bufused += sprintf(*buffer + *bufused, "[%s]", val);
3015
3016cleanup:
3017 free(val);
3018 return rc;
3019}
3020
3021API char *
3022lyd_path(const struct lyd_node *node, LYD_PATH_TYPE pathtype, char *buffer, size_t buflen)
3023{
Michal Vasko14654712020-02-06 08:35:21 +01003024 int is_static = 0, i, depth;
3025 size_t bufused = 0, len;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003026 const struct lyd_node *iter;
3027 const struct lys_module *mod;
Michal Vasko790b2bc2020-08-03 13:35:06 +02003028 LY_ERR rc = LY_SUCCESS;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003029
3030 LY_CHECK_ARG_RET(NULL, node, NULL);
3031 if (buffer) {
3032 LY_CHECK_ARG_RET(node->schema->module->ctx, buflen > 1, NULL);
3033 is_static = 1;
Michal Vasko14654712020-02-06 08:35:21 +01003034 } else {
3035 buflen = 0;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003036 }
3037
3038 switch (pathtype) {
Michal Vasko14654712020-02-06 08:35:21 +01003039 case LYD_PATH_LOG:
Michal Vasko790b2bc2020-08-03 13:35:06 +02003040 case LYD_PATH_LOG_NO_LAST_PRED:
Michal Vasko14654712020-02-06 08:35:21 +01003041 depth = 1;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003042 for (iter = node; iter->parent; iter = (const struct lyd_node *)iter->parent) {
3043 ++depth;
3044 }
3045
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003046 goto iter_print;
Michal Vasko14654712020-02-06 08:35:21 +01003047 while (depth) {
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003048 /* find the right node */
Michal Vasko14654712020-02-06 08:35:21 +01003049 for (iter = node, i = 1; i < depth; iter = (const struct lyd_node *)iter->parent, ++i);
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003050iter_print:
3051 /* print prefix and name */
3052 mod = NULL;
Radek Krejci1798aae2020-07-14 13:26:06 +02003053 if (iter->schema && (!iter->parent || iter->schema->module != iter->parent->schema->module)) {
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003054 mod = iter->schema->module;
3055 }
3056
3057 /* realloc string */
Radek Krejci1798aae2020-07-14 13:26:06 +02003058 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 +02003059 rc = lyd_path_str_enlarge(&buffer, &buflen, bufused + len, is_static);
3060 if (rc != LY_SUCCESS) {
3061 break;
3062 }
3063
3064 /* print next node */
Radek Krejci1798aae2020-07-14 13:26:06 +02003065 bufused += sprintf(buffer + bufused, "/%s%s%s", mod ? mod->name : "", mod ? ":" : "",
3066 iter->schema ? iter->schema->name : ((struct lyd_node_opaq*)iter)->name);
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003067
Michal Vasko790b2bc2020-08-03 13:35:06 +02003068 /* do not always print the last (first) predicate */
Radek Krejci1798aae2020-07-14 13:26:06 +02003069 if (iter->schema && (bufused || (pathtype == LYD_PATH_LOG))) {
Michal Vasko790b2bc2020-08-03 13:35:06 +02003070 switch (iter->schema->nodetype) {
3071 case LYS_LIST:
3072 if (iter->schema->flags & LYS_KEYLESS) {
3073 /* print its position */
3074 rc = lyd_path_position_predicate(iter, &buffer, &buflen, &bufused, is_static);
3075 } else {
3076 /* print all list keys in predicates */
3077 rc = lyd_path_list_predicate(iter, &buffer, &buflen, &bufused, is_static);
3078 }
3079 break;
3080 case LYS_LEAFLIST:
3081 if (iter->schema->flags & LYS_CONFIG_W) {
3082 /* print leaf-list value */
3083 rc = lyd_path_leaflist_predicate(iter, &buffer, &buflen, &bufused, is_static);
3084 } else {
3085 /* print its position */
3086 rc = lyd_path_position_predicate(iter, &buffer, &buflen, &bufused, is_static);
3087 }
3088 break;
3089 default:
3090 /* nothing to print more */
3091 break;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003092 }
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003093 }
3094 if (rc != LY_SUCCESS) {
3095 break;
3096 }
3097
Michal Vasko14654712020-02-06 08:35:21 +01003098 --depth;
Michal Vasko5ec7cda2019-09-11 13:43:08 +02003099 }
3100 break;
3101 }
3102
3103 return buffer;
3104}
Michal Vaskoe444f752020-02-10 12:20:06 +01003105
Michal Vasko25a32822020-07-09 15:48:22 +02003106API struct lyd_meta *
3107lyd_find_meta(const struct lyd_meta *first, const struct lys_module *module, const char *name)
3108{
3109 struct lyd_meta *ret = NULL;
3110 const struct ly_ctx *ctx;
3111 const char *prefix, *tmp;
3112 char *str;
3113 size_t pref_len, name_len;
3114
3115 LY_CHECK_ARG_RET(NULL, module || strchr(name, ':'), name, NULL);
3116
3117 if (!first) {
3118 return NULL;
3119 }
3120
3121 ctx = first->annotation->module->ctx;
3122
3123 /* parse the name */
3124 tmp = name;
3125 if (ly_parse_nodeid(&tmp, &prefix, &pref_len, &name, &name_len) || tmp[0]) {
3126 LOGERR(ctx, LY_EINVAL, "Metadata name \"%s\" is not valid.", name);
3127 return NULL;
3128 }
3129
3130 /* find the module */
3131 if (prefix) {
3132 str = strndup(prefix, pref_len);
3133 module = ly_ctx_get_module_latest(ctx, str);
3134 free(str);
3135 LY_CHECK_ERR_RET(!module, LOGERR(ctx, LY_EINVAL, "Module \"%.*s\" not found.", pref_len, prefix), NULL);
3136 }
3137
3138 /* find the metadata */
3139 LY_LIST_FOR(first, first) {
3140 if ((first->annotation->module == module) && !strcmp(first->name, name)) {
3141 ret = (struct lyd_meta *)first;
3142 break;
3143 }
3144 }
3145
3146 return ret;
3147}
3148
Michal Vasko9b368d32020-02-14 13:53:31 +01003149API LY_ERR
Michal Vaskoe444f752020-02-10 12:20:06 +01003150lyd_find_sibling_first(const struct lyd_node *siblings, const struct lyd_node *target, struct lyd_node **match)
3151{
3152 struct lyd_node **match_p;
3153 struct lyd_node_inner *parent;
3154
Michal Vaskof03ed032020-03-04 13:31:44 +01003155 LY_CHECK_ARG_RET(NULL, target, LY_EINVAL);
Michal Vaskoe444f752020-02-10 12:20:06 +01003156
Michal Vasko62ed12d2020-05-21 10:08:25 +02003157 if (!siblings || (lysc_data_parent(siblings->schema) != lysc_data_parent(target->schema))) {
3158 /* no data or schema mismatch */
Michal Vasko9b368d32020-02-14 13:53:31 +01003159 if (match) {
3160 *match = NULL;
3161 }
Michal Vaskoe444f752020-02-10 12:20:06 +01003162 return LY_ENOTFOUND;
3163 }
3164
3165 /* find first sibling */
3166 if (siblings->parent) {
3167 siblings = siblings->parent->child;
3168 } else {
3169 while (siblings->prev->next) {
3170 siblings = siblings->prev;
3171 }
3172 }
3173
3174 parent = (struct lyd_node_inner *)siblings->parent;
3175 if (parent && parent->children_ht) {
3176 assert(target->hash);
3177
3178 /* find by hash */
3179 if (!lyht_find(parent->children_ht, &target, target->hash, (void **)&match_p)) {
Michal Vaskoda859032020-07-14 12:20:14 +02003180 /* check even value when needed */
Michal Vasko8f359bf2020-07-28 10:41:15 +02003181 if (!(target->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) || !lyd_compare_single(target, *match_p, 0)) {
Michal Vaskoda859032020-07-14 12:20:14 +02003182 siblings = *match_p;
3183 } else {
3184 siblings = NULL;
3185 }
Michal Vaskoe444f752020-02-10 12:20:06 +01003186 } else {
3187 /* not found */
3188 siblings = NULL;
3189 }
3190 } else {
3191 /* no children hash table */
3192 for (; siblings; siblings = siblings->next) {
Michal Vasko8f359bf2020-07-28 10:41:15 +02003193 if (!lyd_compare_single(siblings, target, 0)) {
Michal Vaskoe444f752020-02-10 12:20:06 +01003194 break;
3195 }
3196 }
3197 }
3198
3199 if (!siblings) {
Michal Vasko9b368d32020-02-14 13:53:31 +01003200 if (match) {
3201 *match = NULL;
3202 }
Michal Vaskoe444f752020-02-10 12:20:06 +01003203 return LY_ENOTFOUND;
3204 }
3205
Michal Vasko9b368d32020-02-14 13:53:31 +01003206 if (match) {
3207 *match = (struct lyd_node *)siblings;
3208 }
Michal Vaskoe444f752020-02-10 12:20:06 +01003209 return LY_SUCCESS;
3210}
3211
Michal Vasko90932a92020-02-12 14:33:03 +01003212static int
3213lyd_hash_table_schema_val_equal(void *val1_p, void *val2_p, int UNUSED(mod), void *UNUSED(cb_data))
3214{
3215 struct lysc_node *val1;
3216 struct lyd_node *val2;
3217
3218 val1 = *((struct lysc_node **)val1_p);
3219 val2 = *((struct lyd_node **)val2_p);
3220
Michal Vasko90932a92020-02-12 14:33:03 +01003221 if (val1 == val2->schema) {
3222 /* schema match is enough */
3223 return 1;
3224 } else {
3225 return 0;
3226 }
3227}
3228
3229static LY_ERR
3230lyd_find_sibling_schema(const struct lyd_node *siblings, const struct lysc_node *schema, struct lyd_node **match)
3231{
3232 struct lyd_node **match_p;
3233 struct lyd_node_inner *parent;
3234 uint32_t hash;
3235 values_equal_cb ht_cb;
3236
Michal Vaskob104f112020-07-17 09:54:54 +02003237 assert(siblings && schema);
Michal Vasko90932a92020-02-12 14:33:03 +01003238
3239 parent = (struct lyd_node_inner *)siblings->parent;
3240 if (parent && parent->children_ht) {
3241 /* calculate our hash */
3242 hash = dict_hash_multi(0, schema->module->name, strlen(schema->module->name));
3243 hash = dict_hash_multi(hash, schema->name, strlen(schema->name));
3244 hash = dict_hash_multi(hash, NULL, 0);
3245
3246 /* use special hash table function */
3247 ht_cb = lyht_set_cb(parent->children_ht, lyd_hash_table_schema_val_equal);
3248
3249 /* find by hash */
3250 if (!lyht_find(parent->children_ht, &schema, hash, (void **)&match_p)) {
3251 siblings = *match_p;
3252 } else {
3253 /* not found */
3254 siblings = NULL;
3255 }
3256
3257 /* set the original hash table compare function back */
3258 lyht_set_cb(parent->children_ht, ht_cb);
3259 } else {
Michal Vaskob104f112020-07-17 09:54:54 +02003260 /* find first sibling */
3261 if (siblings->parent) {
3262 siblings = siblings->parent->child;
3263 } else {
3264 while (siblings->prev->next) {
3265 siblings = siblings->prev;
3266 }
3267 }
3268
3269 /* search manually without hashes */
Michal Vasko90932a92020-02-12 14:33:03 +01003270 for (; siblings; siblings = siblings->next) {
3271 if (siblings->schema == schema) {
3272 /* schema match is enough */
3273 break;
3274 }
3275 }
3276 }
3277
3278 if (!siblings) {
Michal Vasko9b368d32020-02-14 13:53:31 +01003279 if (match) {
3280 *match = NULL;
3281 }
Michal Vasko90932a92020-02-12 14:33:03 +01003282 return LY_ENOTFOUND;
3283 }
3284
Michal Vasko9b368d32020-02-14 13:53:31 +01003285 if (match) {
3286 *match = (struct lyd_node *)siblings;
3287 }
Michal Vasko90932a92020-02-12 14:33:03 +01003288 return LY_SUCCESS;
3289}
3290
Michal Vaskoe444f752020-02-10 12:20:06 +01003291API LY_ERR
3292lyd_find_sibling_val(const struct lyd_node *siblings, const struct lysc_node *schema, const char *key_or_value,
3293 size_t val_len, struct lyd_node **match)
3294{
3295 LY_ERR rc;
3296 struct lyd_node *target = NULL;
3297
Michal Vasko4c583e82020-07-17 12:16:14 +02003298 LY_CHECK_ARG_RET(NULL, schema, !(schema->nodetype & (LYS_CHOICE | LYS_CASE)), LY_EINVAL);
Michal Vaskoe444f752020-02-10 12:20:06 +01003299
Michal Vasko62ed12d2020-05-21 10:08:25 +02003300 if (!siblings || (lysc_data_parent(siblings->schema) != lysc_data_parent(schema))) {
3301 /* no data or schema mismatch */
Michal Vasko9b368d32020-02-14 13:53:31 +01003302 if (match) {
3303 *match = NULL;
3304 }
Michal Vaskoe444f752020-02-10 12:20:06 +01003305 return LY_ENOTFOUND;
3306 }
3307
Michal Vaskof03ed032020-03-04 13:31:44 +01003308 if (key_or_value && !val_len) {
3309 val_len = strlen(key_or_value);
3310 }
3311
Michal Vaskob104f112020-07-17 09:54:54 +02003312 if ((schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) && key_or_value) {
3313 /* create a data node and find the instance */
3314 if (schema->nodetype == LYS_LEAFLIST) {
3315 /* target used attributes: schema, hash, value */
Radek Krejci1798aae2020-07-14 13:26:06 +02003316 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 +02003317 LY_CHECK_RET(rc && (rc != LY_EINCOMPLETE), rc);
3318 } else {
Michal Vasko90932a92020-02-12 14:33:03 +01003319 /* target used attributes: schema, hash, child (all keys) */
Michal Vasko004d3152020-06-11 19:59:22 +02003320 LY_CHECK_RET(lyd_create_list2(schema, key_or_value, val_len, &target));
Michal Vasko90932a92020-02-12 14:33:03 +01003321 }
3322
3323 /* find it */
3324 rc = lyd_find_sibling_first(siblings, target, match);
Michal Vaskob104f112020-07-17 09:54:54 +02003325 } else {
3326 /* find the first schema node instance */
3327 rc = lyd_find_sibling_schema(siblings, schema, match);
Michal Vaskoe444f752020-02-10 12:20:06 +01003328 }
3329
Michal Vaskoe444f752020-02-10 12:20:06 +01003330 lyd_free_tree(target);
3331 return rc;
3332}
Michal Vaskoccc02342020-05-21 10:09:21 +02003333
3334API LY_ERR
3335lyd_find_xpath(const struct lyd_node *ctx_node, const char *xpath, struct ly_set **set)
3336{
3337 LY_ERR ret = LY_SUCCESS;
3338 struct lyxp_set xp_set;
3339 struct lyxp_expr *exp;
3340 uint32_t i;
3341
3342 LY_CHECK_ARG_RET(NULL, ctx_node, xpath, set, LY_EINVAL);
3343
3344 memset(&xp_set, 0, sizeof xp_set);
3345
3346 /* compile expression */
Michal Vasko004d3152020-06-11 19:59:22 +02003347 exp = lyxp_expr_parse((struct ly_ctx *)LYD_NODE_CTX(ctx_node), xpath, 0, 1);
Michal Vaskoccc02342020-05-21 10:09:21 +02003348 LY_CHECK_ERR_GOTO(!exp, ret = LY_EINVAL, cleanup);
3349
3350 /* evaluate expression */
3351 ret = lyxp_eval(exp, LYD_JSON, ctx_node->schema->module, ctx_node, LYXP_NODE_ELEM, ctx_node, &xp_set, 0);
3352 LY_CHECK_GOTO(ret, cleanup);
3353
3354 /* allocate return set */
3355 *set = ly_set_new();
3356 LY_CHECK_ERR_GOTO(!*set, LOGMEM(LYD_NODE_CTX(ctx_node)); ret = LY_EMEM, cleanup);
3357
3358 /* transform into ly_set */
3359 if (xp_set.type == LYXP_SET_NODE_SET) {
3360 /* allocate memory for all the elements once (even though not all items must be elements but most likely will be) */
3361 (*set)->objs = malloc(xp_set.used * sizeof *(*set)->objs);
3362 LY_CHECK_ERR_GOTO(!(*set)->objs, LOGMEM(LYD_NODE_CTX(ctx_node)); ret = LY_EMEM, cleanup);
3363 (*set)->size = xp_set.used;
3364
3365 for (i = 0; i < xp_set.used; ++i) {
3366 if (xp_set.val.nodes[i].type == LYXP_NODE_ELEM) {
3367 ly_set_add(*set, xp_set.val.nodes[i].node, LY_SET_OPT_USEASLIST);
3368 }
3369 }
3370 }
3371
3372cleanup:
Michal Vasko0691d522020-05-21 13:21:47 +02003373 lyxp_set_free_content(&xp_set);
Michal Vaskoccc02342020-05-21 10:09:21 +02003374 lyxp_expr_free((struct ly_ctx *)LYD_NODE_CTX(ctx_node), exp);
3375 return ret;
3376}