libyang REFACTOR minor file reorganization
diff --git a/src/in.c b/src/in.c
index b893dd2..033b642 100644
--- a/src/in.c
+++ b/src/in.c
@@ -15,16 +15,6 @@
#define _GNU_SOURCE
#define _POSIX_C_SOURCE 200809L /* strdup, strndup */
-#ifdef __APPLE__
-#define _DARWIN_C_SOURCE /* F_GETPATH */
-#endif
-
-#if defined (__NetBSD__) || defined (__OpenBSD__)
-/* realpath */
-#define _XOPEN_SOURCE 1
-#define _XOPEN_SOURCE_EXTENDED 1
-#endif
-
#include "in.h"
#include "in_internal.h"
@@ -261,63 +251,6 @@
return NULL;
}
-void
-lys_parser_fill_filepath(struct ly_ctx *ctx, struct ly_in *in, const char **filepath)
-{
- char path[PATH_MAX];
-
-#ifndef __APPLE__
- char proc_path[32];
- int len;
-#endif
-
- LY_CHECK_ARG_RET(NULL, ctx, in, filepath, );
- if (*filepath) {
- /* filepath already set */
- return;
- }
-
- switch (in->type) {
- case LY_IN_FILEPATH:
- if (realpath(in->method.fpath.filepath, path) != NULL) {
- lydict_insert(ctx, path, 0, filepath);
- } else {
- lydict_insert(ctx, in->method.fpath.filepath, 0, filepath);
- }
-
- break;
- case LY_IN_FD:
-#ifdef __APPLE__
- if (fcntl(in->method.fd, F_GETPATH, path) != -1) {
- lydict_insert(ctx, path, 0, filepath);
- }
-#elif defined _WIN32
- HANDLE h = _get_osfhandle(in->method.fd);
- FILE_NAME_INFO info;
- if (GetFileInformationByHandleEx(h, FileNameInfo, &info, sizeof info)) {
- char *buf = calloc(info.FileNameLength + 1 /* trailing NULL */, MB_CUR_MAX);
- len = wcstombs(buf, info.FileName, info.FileNameLength * MB_CUR_MAX);
- lydict_insert(ctx, buf, len, filepath);
- }
-#else
- /* get URI if there is /proc */
- sprintf(proc_path, "/proc/self/fd/%d", in->method.fd);
- if ((len = readlink(proc_path, path, PATH_MAX - 1)) > 0) {
- lydict_insert(ctx, path, len, filepath);
- }
-#endif
- break;
- case LY_IN_MEMORY:
- case LY_IN_FILE:
- /* nothing to do */
- break;
- default:
- LOGINT(ctx);
- break;
- }
-
-}
-
LIBYANG_API_DEF size_t
ly_in_parsed(const struct ly_in *in)
{
@@ -388,164 +321,3 @@
in->current += count;
return LY_SUCCESS;
}
-
-void
-lyd_ctx_free(struct lyd_ctx *lydctx)
-{
- ly_set_erase(&lydctx->node_types, NULL);
- ly_set_erase(&lydctx->meta_types, NULL);
- ly_set_erase(&lydctx->node_when, NULL);
- ly_set_erase(&lydctx->ext_val, free);
-}
-
-LY_ERR
-lyd_parser_find_operation(const struct lyd_node *parent, uint32_t int_opts, struct lyd_node **op)
-{
- const struct lyd_node *iter;
-
- *op = NULL;
-
- if (!parent) {
- /* no parent, nothing to look for */
- return LY_SUCCESS;
- }
-
- /* we need to find the operation node if it already exists */
- for (iter = parent; iter; iter = lyd_parent(iter)) {
- if (iter->schema && (iter->schema->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF))) {
- break;
- }
- }
-
- if (!iter) {
- /* no operation found */
- return LY_SUCCESS;
- }
-
- if (!(int_opts & (LYD_INTOPT_RPC | LYD_INTOPT_ACTION | LYD_INTOPT_NOTIF | LYD_INTOPT_REPLY))) {
- LOGERR(LYD_CTX(parent), LY_EINVAL, "Invalid parent %s \"%s\" node when not parsing any operation.",
- lys_nodetype2str(iter->schema->nodetype), iter->schema->name);
- return LY_EINVAL;
- } else if ((iter->schema->nodetype == LYS_RPC) && !(int_opts & (LYD_INTOPT_RPC | LYD_INTOPT_REPLY))) {
- LOGERR(LYD_CTX(parent), LY_EINVAL, "Invalid parent RPC \"%s\" node when not parsing RPC nor rpc-reply.",
- iter->schema->name);
- return LY_EINVAL;
- } else if ((iter->schema->nodetype == LYS_ACTION) && !(int_opts & (LYD_INTOPT_ACTION | LYD_INTOPT_REPLY))) {
- LOGERR(LYD_CTX(parent), LY_EINVAL, "Invalid parent action \"%s\" node when not parsing action nor rpc-reply.",
- iter->schema->name);
- return LY_EINVAL;
- } else if ((iter->schema->nodetype == LYS_NOTIF) && !(int_opts & LYD_INTOPT_NOTIF)) {
- LOGERR(LYD_CTX(parent), LY_EINVAL, "Invalid parent notification \"%s\" node when not parsing a notification.",
- iter->schema->name);
- return LY_EINVAL;
- }
-
- *op = (struct lyd_node *)iter;
- return LY_SUCCESS;
-}
-
-LY_ERR
-lyd_parser_check_schema(struct lyd_ctx *lydctx, const struct lysc_node *snode)
-{
- LY_ERR rc = LY_SUCCESS;
-
- LOG_LOCSET(snode, NULL, NULL, NULL);
-
- if (lydctx->int_opts & LYD_INTOPT_ANY) {
- /* nothing to check, everything is allowed */
- goto cleanup;
- }
-
- if ((lydctx->parse_opts & LYD_PARSE_NO_STATE) && (snode->flags & LYS_CONFIG_R)) {
- LOGVAL(lydctx->data_ctx->ctx, LY_VCODE_UNEXPNODE, "state", snode->name);
- rc = LY_EVALID;
- goto cleanup;
- }
-
- if (snode->nodetype == LYS_RPC) {
- if (lydctx->int_opts & (LYD_INTOPT_RPC | LYD_INTOPT_REPLY)) {
- if (lydctx->op_node) {
- goto error_node_dup;
- }
- } else {
- goto error_node_inval;
- }
- } else if (snode->nodetype == LYS_ACTION) {
- if (lydctx->int_opts & (LYD_INTOPT_ACTION | LYD_INTOPT_REPLY)) {
- if (lydctx->op_node) {
- goto error_node_dup;
- }
- } else {
- goto error_node_inval;
- }
- } else if (snode->nodetype == LYS_NOTIF) {
- if (lydctx->int_opts & LYD_INTOPT_NOTIF) {
- if (lydctx->op_node) {
- goto error_node_dup;
- }
- } else {
- goto error_node_inval;
- }
- }
-
- /* success */
- goto cleanup;
-
-error_node_dup:
- LOGVAL(lydctx->data_ctx->ctx, LYVE_DATA, "Unexpected %s element \"%s\", %s \"%s\" already parsed.",
- lys_nodetype2str(snode->nodetype), snode->name, lys_nodetype2str(lydctx->op_node->schema->nodetype),
- lydctx->op_node->schema->name);
- rc = LY_EVALID;
- goto cleanup;
-
-error_node_inval:
- LOGVAL(lydctx->data_ctx->ctx, LYVE_DATA, "Unexpected %s element \"%s\".", lys_nodetype2str(snode->nodetype),
- snode->name);
- rc = LY_EVALID;
-
-cleanup:
- LOG_LOCBACK(1, 0, 0, 0);
- return rc;
-}
-
-LY_ERR
-lyd_parser_create_term(struct lyd_ctx *lydctx, const struct lysc_node *schema, const void *value, size_t value_len,
- ly_bool *dynamic, LY_VALUE_FORMAT format, void *prefix_data, uint32_t hints, struct lyd_node **node)
-{
- ly_bool incomplete;
-
- LY_CHECK_RET(lyd_create_term(schema, value, value_len, dynamic, format, prefix_data, hints, &incomplete, node));
-
- if (incomplete && !(lydctx->parse_opts & LYD_PARSE_ONLY)) {
- LY_CHECK_RET(ly_set_add(&lydctx->node_types, *node, 1, NULL));
- }
- return LY_SUCCESS;
-}
-
-LY_ERR
-lyd_parser_create_meta(struct lyd_ctx *lydctx, struct lyd_node *parent, struct lyd_meta **meta, const struct lys_module *mod,
- const char *name, size_t name_len, const void *value, size_t value_len, ly_bool *dynamic, LY_VALUE_FORMAT format,
- void *prefix_data, uint32_t hints, const struct lysc_node *ctx_node)
-{
- ly_bool incomplete;
- struct lyd_meta *first = NULL;
-
- if (meta && *meta) {
- /* remember the first metadata */
- first = *meta;
- }
-
- LY_CHECK_RET(lyd_create_meta(parent, meta, mod, name, name_len, value, value_len, dynamic, format, prefix_data,
- hints, ctx_node, 0, &incomplete));
-
- if (incomplete && !(lydctx->parse_opts & LYD_PARSE_ONLY)) {
- LY_CHECK_RET(ly_set_add(&lydctx->meta_types, *meta, 1, NULL));
- }
-
- if (first) {
- /* always return the first metadata */
- *meta = first;
- }
-
- return LY_SUCCESS;
-}
diff --git a/src/parser_stmt.c b/src/parser_common.c
similarity index 90%
rename from src/parser_stmt.c
rename to src/parser_common.c
index 81ccbfc..5c345bb 100644
--- a/src/parser_stmt.c
+++ b/src/parser_common.c
@@ -1,9 +1,9 @@
/**
- * @file parser_stmt.c
- * @author Radek KrejĨí <rkrejci@cesnet.cz>
- * @brief Parser of the extension substatements.
+ * @file parser_common.c
+ * @author Michal Vasko <mvasko@cesnet.cz>
+ * @brief libyang common parser functions.
*
- * Copyright (c) 2019 CESNET, z.s.p.o.
+ * Copyright (c) 2015 - 2022 CESNET, z.s.p.o.
*
* This source code is licensed under BSD 3-Clause License (the "License").
* You may not use this file except in compliance with the License.
@@ -12,25 +12,207 @@
* https://opensource.org/licenses/BSD-3-Clause
*/
+#define _GNU_SOURCE
+#define _POSIX_C_SOURCE 200809L /* strdup, strndup */
+
+#ifdef __APPLE__
+#define _DARWIN_C_SOURCE /* F_GETPATH */
+#endif
+
+#if defined (__NetBSD__) || defined (__OpenBSD__)
+/* realpath */
+#define _XOPEN_SOURCE 1
+#define _XOPEN_SOURCE_EXTENDED 1
+#endif
+
+#include "parser_internal.h"
+
#include <assert.h>
#include <ctype.h>
#include <errno.h>
+#include <fcntl.h>
+#include <limits.h>
#include <stdint.h>
+#include <stdio.h>
#include <stdlib.h>
#include <string.h>
+#include <unistd.h>
#include "common.h"
+#include "compat.h"
#include "dict.h"
+#include "in_internal.h"
#include "log.h"
-#include "parser_schema.h"
+#include "parser_data.h"
#include "path.h"
-#include "schema_compile.h"
#include "set.h"
#include "tree.h"
-#include "tree_edit.h"
+#include "tree_data.h"
+#include "tree_data_internal.h"
#include "tree_schema.h"
#include "tree_schema_internal.h"
+void
+lyd_ctx_free(struct lyd_ctx *lydctx)
+{
+ ly_set_erase(&lydctx->node_types, NULL);
+ ly_set_erase(&lydctx->meta_types, NULL);
+ ly_set_erase(&lydctx->node_when, NULL);
+ ly_set_erase(&lydctx->ext_val, free);
+}
+
+LY_ERR
+lyd_parser_find_operation(const struct lyd_node *parent, uint32_t int_opts, struct lyd_node **op)
+{
+ const struct lyd_node *iter;
+
+ *op = NULL;
+
+ if (!parent) {
+ /* no parent, nothing to look for */
+ return LY_SUCCESS;
+ }
+
+ /* we need to find the operation node if it already exists */
+ for (iter = parent; iter; iter = lyd_parent(iter)) {
+ if (iter->schema && (iter->schema->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF))) {
+ break;
+ }
+ }
+
+ if (!iter) {
+ /* no operation found */
+ return LY_SUCCESS;
+ }
+
+ if (!(int_opts & (LYD_INTOPT_RPC | LYD_INTOPT_ACTION | LYD_INTOPT_NOTIF | LYD_INTOPT_REPLY))) {
+ LOGERR(LYD_CTX(parent), LY_EINVAL, "Invalid parent %s \"%s\" node when not parsing any operation.",
+ lys_nodetype2str(iter->schema->nodetype), iter->schema->name);
+ return LY_EINVAL;
+ } else if ((iter->schema->nodetype == LYS_RPC) && !(int_opts & (LYD_INTOPT_RPC | LYD_INTOPT_REPLY))) {
+ LOGERR(LYD_CTX(parent), LY_EINVAL, "Invalid parent RPC \"%s\" node when not parsing RPC nor rpc-reply.",
+ iter->schema->name);
+ return LY_EINVAL;
+ } else if ((iter->schema->nodetype == LYS_ACTION) && !(int_opts & (LYD_INTOPT_ACTION | LYD_INTOPT_REPLY))) {
+ LOGERR(LYD_CTX(parent), LY_EINVAL, "Invalid parent action \"%s\" node when not parsing action nor rpc-reply.",
+ iter->schema->name);
+ return LY_EINVAL;
+ } else if ((iter->schema->nodetype == LYS_NOTIF) && !(int_opts & LYD_INTOPT_NOTIF)) {
+ LOGERR(LYD_CTX(parent), LY_EINVAL, "Invalid parent notification \"%s\" node when not parsing a notification.",
+ iter->schema->name);
+ return LY_EINVAL;
+ }
+
+ *op = (struct lyd_node *)iter;
+ return LY_SUCCESS;
+}
+
+LY_ERR
+lyd_parser_check_schema(struct lyd_ctx *lydctx, const struct lysc_node *snode)
+{
+ LY_ERR rc = LY_SUCCESS;
+
+ LOG_LOCSET(snode, NULL, NULL, NULL);
+
+ if (lydctx->int_opts & LYD_INTOPT_ANY) {
+ /* nothing to check, everything is allowed */
+ goto cleanup;
+ }
+
+ if ((lydctx->parse_opts & LYD_PARSE_NO_STATE) && (snode->flags & LYS_CONFIG_R)) {
+ LOGVAL(lydctx->data_ctx->ctx, LY_VCODE_UNEXPNODE, "state", snode->name);
+ rc = LY_EVALID;
+ goto cleanup;
+ }
+
+ if (snode->nodetype == LYS_RPC) {
+ if (lydctx->int_opts & (LYD_INTOPT_RPC | LYD_INTOPT_REPLY)) {
+ if (lydctx->op_node) {
+ goto error_node_dup;
+ }
+ } else {
+ goto error_node_inval;
+ }
+ } else if (snode->nodetype == LYS_ACTION) {
+ if (lydctx->int_opts & (LYD_INTOPT_ACTION | LYD_INTOPT_REPLY)) {
+ if (lydctx->op_node) {
+ goto error_node_dup;
+ }
+ } else {
+ goto error_node_inval;
+ }
+ } else if (snode->nodetype == LYS_NOTIF) {
+ if (lydctx->int_opts & LYD_INTOPT_NOTIF) {
+ if (lydctx->op_node) {
+ goto error_node_dup;
+ }
+ } else {
+ goto error_node_inval;
+ }
+ }
+
+ /* success */
+ goto cleanup;
+
+error_node_dup:
+ LOGVAL(lydctx->data_ctx->ctx, LYVE_DATA, "Unexpected %s element \"%s\", %s \"%s\" already parsed.",
+ lys_nodetype2str(snode->nodetype), snode->name, lys_nodetype2str(lydctx->op_node->schema->nodetype),
+ lydctx->op_node->schema->name);
+ rc = LY_EVALID;
+ goto cleanup;
+
+error_node_inval:
+ LOGVAL(lydctx->data_ctx->ctx, LYVE_DATA, "Unexpected %s element \"%s\".", lys_nodetype2str(snode->nodetype),
+ snode->name);
+ rc = LY_EVALID;
+
+cleanup:
+ LOG_LOCBACK(1, 0, 0, 0);
+ return rc;
+}
+
+LY_ERR
+lyd_parser_create_term(struct lyd_ctx *lydctx, const struct lysc_node *schema, const void *value, size_t value_len,
+ ly_bool *dynamic, LY_VALUE_FORMAT format, void *prefix_data, uint32_t hints, struct lyd_node **node)
+{
+ ly_bool incomplete;
+
+ LY_CHECK_RET(lyd_create_term(schema, value, value_len, dynamic, format, prefix_data, hints, &incomplete, node));
+
+ if (incomplete && !(lydctx->parse_opts & LYD_PARSE_ONLY)) {
+ LY_CHECK_RET(ly_set_add(&lydctx->node_types, *node, 1, NULL));
+ }
+ return LY_SUCCESS;
+}
+
+LY_ERR
+lyd_parser_create_meta(struct lyd_ctx *lydctx, struct lyd_node *parent, struct lyd_meta **meta, const struct lys_module *mod,
+ const char *name, size_t name_len, const void *value, size_t value_len, ly_bool *dynamic, LY_VALUE_FORMAT format,
+ void *prefix_data, uint32_t hints, const struct lysc_node *ctx_node)
+{
+ ly_bool incomplete;
+ struct lyd_meta *first = NULL;
+
+ if (meta && *meta) {
+ /* remember the first metadata */
+ first = *meta;
+ }
+
+ LY_CHECK_RET(lyd_create_meta(parent, meta, mod, name, name_len, value, value_len, dynamic, format, prefix_data,
+ hints, ctx_node, 0, &incomplete));
+
+ if (incomplete && !(lydctx->parse_opts & LYD_PARSE_ONLY)) {
+ LY_CHECK_RET(ly_set_add(&lydctx->meta_types, *meta, 1, NULL));
+ }
+
+ if (first) {
+ /* always return the first metadata */
+ *meta = first;
+ }
+
+ return LY_SUCCESS;
+}
+
static LY_ERR lysp_stmt_container(struct lys_parser_ctx *ctx, const struct lysp_stmt *stmt, struct lysp_node *parent,
struct lysp_node **siblings);
static LY_ERR lysp_stmt_choice(struct lys_parser_ctx *ctx, const struct lysp_stmt *stmt, struct lysp_node *parent,
@@ -368,7 +550,8 @@
* @return LY_ERR values.
*/
static LY_ERR
-lysp_stmt_mandatory(struct lys_parser_ctx *ctx, const struct lysp_stmt *stmt, uint16_t *flags, struct lysp_ext_instance **exts)
+lysp_stmt_mandatory(struct lys_parser_ctx *ctx, const struct lysp_stmt *stmt, uint16_t *flags,
+ struct lysp_ext_instance **exts)
{
size_t arg_len;
@@ -677,7 +860,8 @@
* @return LY_ERR values.
*/
static LY_ERR
-lysp_stmt_type_fracdigits(struct lys_parser_ctx *ctx, const struct lysp_stmt *stmt, uint8_t *fracdig, struct lysp_ext_instance **exts)
+lysp_stmt_type_fracdigits(struct lys_parser_ctx *ctx, const struct lysp_stmt *stmt, uint8_t *fracdig,
+ struct lysp_ext_instance **exts)
{
char *ptr;
size_t arg_len;
@@ -777,7 +961,8 @@
* @return LY_ERR values.
*/
static LY_ERR
-lysp_stmt_type_pattern_modifier(struct lys_parser_ctx *ctx, const struct lysp_stmt *stmt, const char **pat, struct lysp_ext_instance **exts)
+lysp_stmt_type_pattern_modifier(struct lys_parser_ctx *ctx, const struct lysp_stmt *stmt, const char **pat,
+ struct lysp_ext_instance **exts)
{
size_t arg_len;
char *buf;
@@ -1237,7 +1422,8 @@
* @return LY_ERR values.
*/
static LY_ERR
-lysp_stmt_leaflist(struct lys_parser_ctx *ctx, const struct lysp_stmt *stmt, struct lysp_node *parent, struct lysp_node **siblings)
+lysp_stmt_leaflist(struct lys_parser_ctx *ctx, const struct lysp_stmt *stmt, struct lysp_node *parent,
+ struct lysp_node **siblings)
{
struct lysp_node_leaflist *llist;
@@ -1388,7 +1574,8 @@
* @return LY_ERR values.
*/
static LY_ERR
-lysp_stmt_typedef(struct lys_parser_ctx *ctx, const struct lysp_stmt *stmt, struct lysp_node *parent, struct lysp_tpdf **typedefs)
+lysp_stmt_typedef(struct lys_parser_ctx *ctx, const struct lysp_stmt *stmt, struct lysp_node *parent,
+ struct lysp_tpdf **typedefs)
{
struct lysp_tpdf *tpdf;
@@ -1532,7 +1719,8 @@
* @return LY_ERR values.
*/
static LY_ERR
-lysp_stmt_action(struct lys_parser_ctx *ctx, const struct lysp_stmt *stmt, struct lysp_node *parent, struct lysp_node_action **actions)
+lysp_stmt_action(struct lys_parser_ctx *ctx, const struct lysp_stmt *stmt, struct lysp_node *parent,
+ struct lysp_node_action **actions)
{
struct lysp_node_action *act;
@@ -1607,7 +1795,8 @@
* @return LY_ERR values.
*/
static LY_ERR
-lysp_stmt_notif(struct lys_parser_ctx *ctx, const struct lysp_stmt *stmt, struct lysp_node *parent, struct lysp_node_notif **notifs)
+lysp_stmt_notif(struct lys_parser_ctx *ctx, const struct lysp_stmt *stmt, struct lysp_node *parent,
+ struct lysp_node_notif **notifs)
{
struct lysp_node_notif *notif;
@@ -1692,7 +1881,8 @@
* @return LY_ERR values.
*/
static LY_ERR
-lysp_stmt_grouping(struct lys_parser_ctx *ctx, const struct lysp_stmt *stmt, struct lysp_node *parent, struct lysp_node_grp **groupings)
+lysp_stmt_grouping(struct lys_parser_ctx *ctx, const struct lysp_stmt *stmt, struct lysp_node *parent,
+ struct lysp_node_grp **groupings)
{
struct lysp_node_grp *grp;
@@ -1778,7 +1968,8 @@
* @return LY_ERR values.
*/
static LY_ERR
-lysp_stmt_augment(struct lys_parser_ctx *ctx, const struct lysp_stmt *stmt, struct lysp_node *parent, struct lysp_node_augment **augments)
+lysp_stmt_augment(struct lys_parser_ctx *ctx, const struct lysp_stmt *stmt, struct lysp_node *parent,
+ struct lysp_node_augment **augments)
{
struct lysp_node_augment *aug;
@@ -1867,7 +2058,8 @@
* @return LY_ERR values.
*/
static LY_ERR
-lysp_stmt_uses(struct lys_parser_ctx *ctx, const struct lysp_stmt *stmt, struct lysp_node *parent, struct lysp_node **siblings)
+lysp_stmt_uses(struct lys_parser_ctx *ctx, const struct lysp_stmt *stmt, struct lysp_node *parent,
+ struct lysp_node **siblings)
{
struct lysp_node_uses *uses;
@@ -1928,7 +2120,8 @@
* @return LY_ERR values.
*/
static LY_ERR
-lysp_stmt_case(struct lys_parser_ctx *ctx, const struct lysp_stmt *stmt, struct lysp_node *parent, struct lysp_node **siblings)
+lysp_stmt_case(struct lys_parser_ctx *ctx, const struct lysp_stmt *stmt, struct lysp_node *parent,
+ struct lysp_node **siblings)
{
struct lysp_node_case *cas;
@@ -2006,7 +2199,8 @@
* @return LY_ERR values.
*/
static LY_ERR
-lysp_stmt_choice(struct lys_parser_ctx *ctx, const struct lysp_stmt *stmt, struct lysp_node *parent, struct lysp_node **siblings)
+lysp_stmt_choice(struct lys_parser_ctx *ctx, const struct lysp_stmt *stmt, struct lysp_node *parent,
+ struct lysp_node **siblings)
{
struct lysp_node_choice *choice;
@@ -2094,7 +2288,8 @@
* @return LY_ERR values.
*/
static LY_ERR
-lysp_stmt_container(struct lys_parser_ctx *ctx, const struct lysp_stmt *stmt, struct lysp_node *parent, struct lysp_node **siblings)
+lysp_stmt_container(struct lys_parser_ctx *ctx, const struct lysp_stmt *stmt, struct lysp_node *parent,
+ struct lysp_node **siblings)
{
struct lysp_node_container *cont;
@@ -2196,7 +2391,8 @@
* @return LY_ERR values.
*/
static LY_ERR
-lysp_stmt_list(struct lys_parser_ctx *ctx, const struct lysp_stmt *stmt, struct lysp_node *parent, struct lysp_node **siblings)
+lysp_stmt_list(struct lys_parser_ctx *ctx, const struct lysp_stmt *stmt, struct lysp_node *parent,
+ struct lysp_node **siblings)
{
struct lysp_node_list *list;
@@ -2474,3 +2670,59 @@
LOG_LOCBACK(0, 0, 1, 0);
return ret;
}
+
+void
+lys_parser_fill_filepath(struct ly_ctx *ctx, struct ly_in *in, const char **filepath)
+{
+ char path[PATH_MAX];
+
+#ifndef __APPLE__
+ char proc_path[32];
+ int len;
+#endif
+
+ LY_CHECK_ARG_RET(NULL, ctx, in, filepath, );
+ if (*filepath) {
+ /* filepath already set */
+ return;
+ }
+
+ switch (in->type) {
+ case LY_IN_FILEPATH:
+ if (realpath(in->method.fpath.filepath, path) != NULL) {
+ lydict_insert(ctx, path, 0, filepath);
+ } else {
+ lydict_insert(ctx, in->method.fpath.filepath, 0, filepath);
+ }
+
+ break;
+ case LY_IN_FD:
+#ifdef __APPLE__
+ if (fcntl(in->method.fd, F_GETPATH, path) != -1) {
+ lydict_insert(ctx, path, 0, filepath);
+ }
+#elif defined _WIN32
+ HANDLE h = _get_osfhandle(in->method.fd);
+ FILE_NAME_INFO info;
+ if (GetFileInformationByHandleEx(h, FileNameInfo, &info, sizeof info)) {
+ char *buf = calloc(info.FileNameLength + 1 /* trailing NULL */, MB_CUR_MAX);
+ len = wcstombs(buf, info.FileName, info.FileNameLength * MB_CUR_MAX);
+ lydict_insert(ctx, buf, len, filepath);
+ }
+#else
+ /* get URI if there is /proc */
+ sprintf(proc_path, "/proc/self/fd/%d", in->method.fd);
+ if ((len = readlink(proc_path, path, PATH_MAX - 1)) > 0) {
+ lydict_insert(ctx, path, len, filepath);
+ }
+#endif
+ break;
+ case LY_IN_MEMORY:
+ case LY_IN_FILE:
+ /* nothing to do */
+ break;
+ default:
+ LOGINT(ctx);
+ break;
+ }
+}
diff --git a/src/tree_data.c b/src/tree_data.c
index 8cbf42d..ea3ad9c 100644
--- a/src/tree_data.c
+++ b/src/tree_data.c
@@ -51,252 +51,6 @@
#include "xml.h"
#include "xpath.h"
-static LY_ERR lyd_find_sibling_schema(const struct lyd_node *siblings, const struct lysc_node *schema,
- struct lyd_node **match);
-
-LY_ERR
-lyd_value_store(const struct ly_ctx *ctx, struct lyd_value *val, const struct lysc_type *type, const void *value,
- size_t value_len, ly_bool *dynamic, LY_VALUE_FORMAT format, void *prefix_data, uint32_t hints,
- const struct lysc_node *ctx_node, ly_bool *incomplete)
-{
- LY_ERR ret;
- struct ly_err_item *err = NULL;
- uint32_t options = (dynamic && *dynamic ? LYPLG_TYPE_STORE_DYNAMIC : 0);
-
- if (!value) {
- value = "";
- }
- if (incomplete) {
- *incomplete = 0;
- }
-
- ret = type->plugin->store(ctx, type, value, value_len, options, format, prefix_data, hints, ctx_node, val, NULL, &err);
- if (dynamic) {
- *dynamic = 0;
- }
-
- if (ret == LY_EINCOMPLETE) {
- if (incomplete) {
- *incomplete = 1;
- }
- } else if (ret) {
- if (err) {
- LOGVAL_ERRITEM(ctx, err);
- ly_err_free(err);
- } else {
- LOGVAL(ctx, LYVE_OTHER, "Storing value failed.");
- }
- return ret;
- }
-
- return LY_SUCCESS;
-}
-
-LY_ERR
-lyd_value_validate_incomplete(const struct ly_ctx *ctx, const struct lysc_type *type, struct lyd_value *val,
- const struct lyd_node *ctx_node, const struct lyd_node *tree)
-{
- LY_ERR ret;
- struct ly_err_item *err = NULL;
-
- assert(type->plugin->validate);
-
- ret = type->plugin->validate(ctx, type, ctx_node, tree, val, &err);
- if (ret) {
- if (err) {
- LOGVAL_ERRITEM(ctx, err);
- ly_err_free(err);
- } else {
- LOGVAL(ctx, LYVE_OTHER, "Resolving value \"%s\" failed.", type->plugin->print(ctx, val, LY_VALUE_CANON,
- NULL, NULL, NULL));
- }
- return ret;
- }
-
- return LY_SUCCESS;
-}
-
-LY_ERR
-lys_value_validate(const struct ly_ctx *ctx, const struct lysc_node *node, const char *value, size_t value_len,
- LY_VALUE_FORMAT format, void *prefix_data)
-{
- LY_ERR rc = LY_SUCCESS;
- struct ly_err_item *err = NULL;
- struct lyd_value storage;
- struct lysc_type *type;
-
- LY_CHECK_ARG_RET(ctx, node, value, LY_EINVAL);
-
- if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
- LOGARG(ctx, node);
- return LY_EINVAL;
- }
-
- type = ((struct lysc_node_leaf *)node)->type;
- rc = type->plugin->store(ctx ? ctx : node->module->ctx, type, value, value_len, 0, format, prefix_data,
- LYD_HINT_SCHEMA, node, &storage, NULL, &err);
- if (rc == LY_EINCOMPLETE) {
- /* actually success since we do not provide the context tree and call validation with
- * LY_TYPE_OPTS_INCOMPLETE_DATA */
- rc = LY_SUCCESS;
- } else if (rc && err) {
- if (ctx) {
- /* log only in case the ctx was provided as input parameter */
- LOG_LOCSET(NULL, NULL, err->path, NULL);
- LOGVAL_ERRITEM(ctx, err);
- LOG_LOCBACK(0, 0, 1, 0);
- }
- ly_err_free(err);
- }
-
- if (!rc) {
- type->plugin->free(ctx ? ctx : node->module->ctx, &storage);
- }
- return rc;
-}
-
-LIBYANG_API_DEF LY_ERR
-lyd_value_validate(const struct ly_ctx *ctx, const struct lysc_node *schema, const char *value, size_t value_len,
- const struct lyd_node *ctx_node, const struct lysc_type **realtype, const char **canonical)
-{
- LY_ERR rc;
- struct ly_err_item *err = NULL;
- struct lysc_type *type;
- struct lyd_value val = {0};
- ly_bool stored = 0, log = 1;
-
- LY_CHECK_ARG_RET(ctx, schema, value, LY_EINVAL);
-
- if (!ctx) {
- ctx = schema->module->ctx;
- log = 0;
- }
- type = ((struct lysc_node_leaf *)schema)->type;
-
- /* store */
- rc = type->plugin->store(ctx, type, value, value_len, 0, LY_VALUE_JSON, NULL,
- LYD_HINT_DATA, schema, &val, NULL, &err);
- if (!rc || (rc == LY_EINCOMPLETE)) {
- stored = 1;
- }
-
- if (ctx_node && (rc == LY_EINCOMPLETE)) {
- /* resolve */
- rc = type->plugin->validate(ctx, type, ctx_node, ctx_node, &val, &err);
- }
-
- if (rc && (rc != LY_EINCOMPLETE) && err) {
- if (log) {
- /* log error */
- if (err->path) {
- LOG_LOCSET(NULL, NULL, err->path, NULL);
- } else if (ctx_node) {
- LOG_LOCSET(NULL, ctx_node, NULL, NULL);
- } else {
- LOG_LOCSET(schema, NULL, NULL, NULL);
- }
- LOGVAL_ERRITEM(ctx, err);
- if (err->path) {
- LOG_LOCBACK(0, 0, 1, 0);
- } else if (ctx_node) {
- LOG_LOCBACK(0, 1, 0, 0);
- } else {
- LOG_LOCBACK(1, 0, 0, 0);
- }
- }
- ly_err_free(err);
- }
-
- if (!rc || (rc == LY_EINCOMPLETE)) {
- if (realtype) {
- /* return realtype */
- if (val.realtype->basetype == LY_TYPE_UNION) {
- *realtype = val.subvalue->value.realtype;
- } else {
- *realtype = val.realtype;
- }
- }
-
- if (canonical) {
- /* return canonical value */
- lydict_insert(ctx, val.realtype->plugin->print(ctx, &val, LY_VALUE_CANON, NULL, NULL, NULL), 0, canonical);
- }
- }
-
- if (stored) {
- /* free value */
- type->plugin->free(ctx ? ctx : schema->module->ctx, &val);
- }
- return rc;
-}
-
-LIBYANG_API_DEF LY_ERR
-lyd_value_compare(const struct lyd_node_term *node, const char *value, size_t value_len)
-{
- LY_ERR ret = LY_SUCCESS;
- struct ly_ctx *ctx;
- struct lysc_type *type;
- struct lyd_value val = {0};
-
- LY_CHECK_ARG_RET(node ? node->schema->module->ctx : NULL, node, value, LY_EINVAL);
-
- ctx = node->schema->module->ctx;
- type = ((struct lysc_node_leaf *)node->schema)->type;
-
- /* store the value */
- LOG_LOCSET(node->schema, &node->node, NULL, NULL);
- ret = lyd_value_store(ctx, &val, type, value, value_len, NULL, LY_VALUE_JSON, NULL, LYD_HINT_DATA, node->schema, NULL);
- LOG_LOCBACK(1, 1, 0, 0);
- LY_CHECK_RET(ret);
-
- /* compare values */
- ret = type->plugin->compare(&node->value, &val);
-
- type->plugin->free(ctx, &val);
- return ret;
-}
-
-LIBYANG_API_DEF ly_bool
-lyd_is_default(const struct lyd_node *node)
-{
- const struct lysc_node_leaf *leaf;
- const struct lysc_node_leaflist *llist;
- const struct lyd_node_term *term;
- LY_ARRAY_COUNT_TYPE u;
-
- if (!(node->schema->nodetype & LYD_NODE_TERM)) {
- return 0;
- }
-
- term = (const struct lyd_node_term *)node;
-
- if (node->schema->nodetype == LYS_LEAF) {
- leaf = (const struct lysc_node_leaf *)node->schema;
- if (!leaf->dflt) {
- return 0;
- }
-
- /* compare with the default value */
- if (!leaf->type->plugin->compare(&term->value, leaf->dflt)) {
- return 1;
- }
- } else {
- llist = (const struct lysc_node_leaflist *)node->schema;
- if (!llist->dflts) {
- return 0;
- }
-
- LY_ARRAY_FOR(llist->dflts, u) {
- /* compare with each possible default value */
- if (!llist->type->plugin->compare(&term->value, llist->dflts[u])) {
- return 1;
- }
- }
- }
-
- return 0;
-}
-
static LYD_FORMAT
lyd_parse_get_format(const struct ly_in *in, LYD_FORMAT format)
{
@@ -645,1763 +399,6 @@
return lyd_parse_op_(ctx, ext, parent, in, format, data_type, tree, op);
}
-LY_ERR
-lyd_create_term(const struct lysc_node *schema, const char *value, size_t value_len, ly_bool *dynamic,
- LY_VALUE_FORMAT format, void *prefix_data, uint32_t hints, ly_bool *incomplete, struct lyd_node **node)
-{
- LY_ERR ret;
- struct lyd_node_term *term;
-
- assert(schema->nodetype & LYD_NODE_TERM);
-
- term = calloc(1, sizeof *term);
- LY_CHECK_ERR_RET(!term, LOGMEM(schema->module->ctx), LY_EMEM);
-
- term->schema = schema;
- term->prev = &term->node;
- term->flags = LYD_NEW;
-
- LOG_LOCSET(schema, NULL, NULL, NULL);
- ret = lyd_value_store(schema->module->ctx, &term->value, ((struct lysc_node_leaf *)term->schema)->type, value,
- value_len, dynamic, format, prefix_data, hints, schema, incomplete);
- LOG_LOCBACK(1, 0, 0, 0);
- LY_CHECK_ERR_RET(ret, free(term), ret);
- lyd_hash(&term->node);
-
- *node = &term->node;
- return ret;
-}
-
-LY_ERR
-lyd_create_term2(const struct lysc_node *schema, const struct lyd_value *val, struct lyd_node **node)
-{
- LY_ERR ret;
- struct lyd_node_term *term;
- struct lysc_type *type;
-
- assert(schema->nodetype & LYD_NODE_TERM);
- assert(val && val->realtype);
-
- term = calloc(1, sizeof *term);
- LY_CHECK_ERR_RET(!term, LOGMEM(schema->module->ctx), LY_EMEM);
-
- term->schema = schema;
- term->prev = &term->node;
- term->flags = LYD_NEW;
-
- type = ((struct lysc_node_leaf *)schema)->type;
- ret = type->plugin->duplicate(schema->module->ctx, val, &term->value);
- if (ret) {
- LOGERR(schema->module->ctx, ret, "Value duplication failed.");
- free(term);
- return ret;
- }
- lyd_hash(&term->node);
-
- *node = &term->node;
- return ret;
-}
-
-LY_ERR
-lyd_create_inner(const struct lysc_node *schema, struct lyd_node **node)
-{
- struct lyd_node_inner *in;
-
- assert(schema->nodetype & LYD_NODE_INNER);
-
- in = calloc(1, sizeof *in);
- LY_CHECK_ERR_RET(!in, LOGMEM(schema->module->ctx), LY_EMEM);
-
- in->schema = schema;
- in->prev = &in->node;
- in->flags = LYD_NEW;
- if ((schema->nodetype == LYS_CONTAINER) && !(schema->flags & LYS_PRESENCE)) {
- in->flags |= LYD_DEFAULT;
- }
-
- /* do not hash list with keys, we need them for the hash */
- if ((schema->nodetype != LYS_LIST) || (schema->flags & LYS_KEYLESS)) {
- lyd_hash(&in->node);
- }
-
- *node = &in->node;
- return LY_SUCCESS;
-}
-
-LY_ERR
-lyd_create_list(const struct lysc_node *schema, const struct ly_path_predicate *predicates, struct lyd_node **node)
-{
- LY_ERR ret = LY_SUCCESS;
- struct lyd_node *list = NULL, *key;
- LY_ARRAY_COUNT_TYPE u;
-
- assert((schema->nodetype == LYS_LIST) && !(schema->flags & LYS_KEYLESS));
-
- /* create list */
- LY_CHECK_GOTO(ret = lyd_create_inner(schema, &list), cleanup);
-
- LOG_LOCSET(NULL, list, NULL, NULL);
-
- /* create and insert all the keys */
- LY_ARRAY_FOR(predicates, u) {
- LY_CHECK_GOTO(ret = lyd_create_term2(predicates[u].key, &predicates[u].value, &key), cleanup);
- lyd_insert_node(list, NULL, key, 0);
- }
-
- /* hash having all the keys */
- lyd_hash(list);
-
- /* success */
- *node = list;
- list = NULL;
-
-cleanup:
- LOG_LOCBACK(0, 1, 0, 0);
- lyd_free_tree(list);
- return ret;
-}
-
-static LY_ERR
-lyd_create_list2(const struct lysc_node *schema, const char *keys, size_t keys_len, struct lyd_node **node)
-{
- LY_ERR ret = LY_SUCCESS;
- struct lyxp_expr *expr = NULL;
- uint16_t exp_idx = 0;
- enum ly_path_pred_type pred_type = 0;
- struct ly_path_predicate *predicates = NULL;
-
- LOG_LOCSET(schema, NULL, NULL, NULL);
-
- /* parse keys */
- LY_CHECK_GOTO(ret = ly_path_parse_predicate(schema->module->ctx, NULL, keys, keys_len, LY_PATH_PREFIX_OPTIONAL,
- LY_PATH_PRED_KEYS, &expr), cleanup);
-
- /* compile them */
- LY_CHECK_GOTO(ret = ly_path_compile_predicate(schema->module->ctx, NULL, NULL, schema, expr, &exp_idx, LY_VALUE_JSON,
- NULL, &predicates, &pred_type), cleanup);
-
- /* create the list node */
- LY_CHECK_GOTO(ret = lyd_create_list(schema, predicates, node), cleanup);
-
-cleanup:
- LOG_LOCBACK(1, 0, 0, 0);
- lyxp_expr_free(schema->module->ctx, expr);
- ly_path_predicates_free(schema->module->ctx, pred_type, predicates);
- return ret;
-}
-
-LY_ERR
-lyd_create_any(const struct lysc_node *schema, const void *value, LYD_ANYDATA_VALUETYPE value_type, ly_bool use_value,
- struct lyd_node **node)
-{
- LY_ERR ret;
- struct lyd_node_any *any;
- union lyd_any_value any_val;
-
- assert(schema->nodetype & LYD_NODE_ANY);
-
- any = calloc(1, sizeof *any);
- LY_CHECK_ERR_RET(!any, LOGMEM(schema->module->ctx), LY_EMEM);
-
- any->schema = schema;
- any->prev = &any->node;
- any->flags = LYD_NEW;
-
- if (use_value) {
- switch (value_type) {
- case LYD_ANYDATA_DATATREE:
- any->value.tree = (void *)value;
- break;
- case LYD_ANYDATA_STRING:
- case LYD_ANYDATA_XML:
- case LYD_ANYDATA_JSON:
- LY_CHECK_ERR_RET(lydict_insert_zc(schema->module->ctx, (void *)value, &any->value.str), free(any), LY_EMEM);
- break;
- case LYD_ANYDATA_LYB:
- any->value.mem = (void *)value;
- break;
- }
- any->value_type = value_type;
- } else {
- any_val.str = value;
- ret = lyd_any_copy_value(&any->node, &any_val, value_type);
- LY_CHECK_ERR_RET(ret, free(any), ret);
- }
- lyd_hash(&any->node);
-
- *node = &any->node;
- return LY_SUCCESS;
-}
-
-LY_ERR
-lyd_create_opaq(const struct ly_ctx *ctx, const char *name, size_t name_len, const char *prefix, size_t pref_len,
- const char *module_key, size_t module_key_len, const char *value, size_t value_len, ly_bool *dynamic,
- LY_VALUE_FORMAT format, void *val_prefix_data, uint32_t hints, struct lyd_node **node)
-{
- LY_ERR ret = LY_SUCCESS;
- struct lyd_node_opaq *opaq;
-
- assert(ctx && name && name_len && format);
-
- if (!value_len && (!dynamic || !*dynamic)) {
- value = "";
- }
-
- opaq = calloc(1, sizeof *opaq);
- LY_CHECK_ERR_GOTO(!opaq, LOGMEM(ctx); ret = LY_EMEM, finish);
-
- opaq->prev = &opaq->node;
- LY_CHECK_GOTO(ret = lydict_insert(ctx, name, name_len, &opaq->name.name), finish);
-
- if (pref_len) {
- LY_CHECK_GOTO(ret = lydict_insert(ctx, prefix, pref_len, &opaq->name.prefix), finish);
- }
- if (module_key_len) {
- LY_CHECK_GOTO(ret = lydict_insert(ctx, module_key, module_key_len, &opaq->name.module_ns), finish);
- }
- if (dynamic && *dynamic) {
- LY_CHECK_GOTO(ret = lydict_insert_zc(ctx, (char *)value, &opaq->value), finish);
- *dynamic = 0;
- } else {
- LY_CHECK_GOTO(ret = lydict_insert(ctx, value, value_len, &opaq->value), finish);
- }
-
- opaq->format = format;
- opaq->val_prefix_data = val_prefix_data;
- opaq->hints = hints;
- opaq->ctx = ctx;
-
-finish:
- if (ret) {
- lyd_free_tree(&opaq->node);
- ly_free_prefix_data(format, val_prefix_data);
- } else {
- *node = &opaq->node;
- }
- return ret;
-}
-
-LIBYANG_API_DEF LY_ERR
-lyd_new_inner(struct lyd_node *parent, const struct lys_module *module, const char *name, ly_bool output,
- struct lyd_node **node)
-{
- LY_ERR r;
- struct lyd_node *ret = NULL;
- const struct lysc_node *schema;
- struct lysc_ext_instance *ext = NULL;
- const struct ly_ctx *ctx = parent ? LYD_CTX(parent) : (module ? module->ctx : NULL);
-
- LY_CHECK_ARG_RET(ctx, parent || module, parent || node, name, LY_EINVAL);
- LY_CHECK_CTX_EQUAL_RET(parent ? LYD_CTX(parent) : NULL, module ? module->ctx : NULL, LY_EINVAL);
-
- if (!module) {
- module = parent->schema->module;
- }
-
- schema = lys_find_child(parent ? parent->schema : NULL, module, name, 0,
- LYS_CONTAINER | LYS_NOTIF | LYS_RPC | LYS_ACTION, output ? LYS_GETNEXT_OUTPUT : 0);
- if (!schema && parent) {
- r = ly_nested_ext_schema(parent, NULL, module->name, strlen(module->name), LY_VALUE_JSON, NULL, name,
- strlen(name), &schema, &ext);
- LY_CHECK_RET(r && (r != LY_ENOT), r);
- }
- LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "Inner node (container, notif, RPC, or action) \"%s\" not found.",
- name), LY_ENOTFOUND);
-
- LY_CHECK_RET(lyd_create_inner(schema, &ret));
- if (ext) {
- ret->flags |= LYD_EXT;
- }
- if (parent) {
- lyd_insert_node(parent, NULL, ret, 0);
- }
-
- if (node) {
- *node = ret;
- }
- return LY_SUCCESS;
-}
-
-LIBYANG_API_DEF LY_ERR
-lyd_new_ext_inner(const struct lysc_ext_instance *ext, const char *name, struct lyd_node **node)
-{
- struct lyd_node *ret = NULL;
- const struct lysc_node *schema;
- struct ly_ctx *ctx = ext ? ext->module->ctx : NULL;
-
- LY_CHECK_ARG_RET(ctx, ext, node, name, LY_EINVAL);
-
- schema = lysc_ext_find_node(ext, NULL, name, 0, LYS_CONTAINER | LYS_NOTIF | LYS_RPC | LYS_ACTION, 0);
- if (!schema) {
- if (ext->argument) {
- LOGERR(ctx, LY_EINVAL, "Inner node (not a list) \"%s\" not found in instance \"%s\" of extension %s.",
- name, ext->argument, ext->def->name);
- } else {
- LOGERR(ctx, LY_EINVAL, "Inner node (not a list) \"%s\" not found in instance of extension %s.",
- name, ext->def->name);
- }
- return LY_ENOTFOUND;
- }
- LY_CHECK_RET(lyd_create_inner(schema, &ret));
-
- *node = ret;
-
- return LY_SUCCESS;
-}
-
-/**
- * @brief Create a new list node in the data tree.
- *
- * @param[in] parent Parent node for the node being created. NULL in case of creating a top level element.
- * @param[in] module Module of the node being created. If NULL, @p parent module will be used.
- * @param[in] name Schema node name of the new data node. The node must be #LYS_LIST.
- * @param[in] format Format of key values.
- * @param[in] output Flag in case the @p parent is RPC/Action. If value is 0, the input's data nodes of the RPC/Action are
- * taken into consideration. Otherwise, the output's data node is going to be created.
- * @param[out] node Optional created node.
- * @param[in] ap Ordered key values of the new list instance, all must be set. For ::LY_VALUE_LYB, every value must
- * be followed by the value length.
- * @return LY_ERR value.
- */
-static LY_ERR
-_lyd_new_list(struct lyd_node *parent, const struct lys_module *module, const char *name, LY_VALUE_FORMAT format,
- ly_bool output, struct lyd_node **node, va_list ap)
-{
- struct lyd_node *ret = NULL, *key;
- const struct lysc_node *schema, *key_s;
- struct lysc_ext_instance *ext = NULL;
- const struct ly_ctx *ctx = parent ? LYD_CTX(parent) : (module ? module->ctx : NULL);
- const void *key_val;
- uint32_t key_len;
- LY_ERR r, rc = LY_SUCCESS;
-
- LY_CHECK_ARG_RET(ctx, parent || module, parent || node, name, LY_EINVAL);
- LY_CHECK_CTX_EQUAL_RET(parent ? LYD_CTX(parent) : NULL, module ? module->ctx : NULL, LY_EINVAL);
-
- if (!module) {
- module = parent->schema->module;
- }
-
- schema = lys_find_child(parent ? parent->schema : NULL, module, name, 0, LYS_LIST, output ? LYS_GETNEXT_OUTPUT : 0);
- if (!schema && parent) {
- r = ly_nested_ext_schema(parent, NULL, module->name, strlen(module->name), LY_VALUE_JSON, NULL, name,
- strlen(name), &schema, &ext);
- LY_CHECK_RET(r && (r != LY_ENOT), r);
- }
- LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "List node \"%s\" not found.", name), LY_ENOTFOUND);
-
- /* create list inner node */
- LY_CHECK_RET(lyd_create_inner(schema, &ret));
-
- /* create and insert all the keys */
- for (key_s = lysc_node_child(schema); key_s && (key_s->flags & LYS_KEY); key_s = key_s->next) {
- if (format == LY_VALUE_LYB) {
- key_val = va_arg(ap, const void *);
- key_len = va_arg(ap, uint32_t);
- } else {
- key_val = va_arg(ap, const char *);
- key_len = key_val ? strlen((char *)key_val) : 0;
- }
-
- rc = lyd_create_term(key_s, key_val, key_len, NULL, format, NULL, LYD_HINT_DATA, NULL, &key);
- LY_CHECK_GOTO(rc, cleanup);
- lyd_insert_node(ret, NULL, key, 1);
- }
-
- if (ext) {
- ret->flags |= LYD_EXT;
- }
- if (parent) {
- lyd_insert_node(parent, NULL, ret, 0);
- }
-
-cleanup:
- if (rc) {
- lyd_free_tree(ret);
- ret = NULL;
- } else if (node) {
- *node = ret;
- }
- return rc;
-}
-
-LIBYANG_API_DEF LY_ERR
-lyd_new_list(struct lyd_node *parent, const struct lys_module *module, const char *name, ly_bool output,
- struct lyd_node **node, ...)
-{
- LY_ERR rc;
- va_list ap;
-
- va_start(ap, node);
-
- rc = _lyd_new_list(parent, module, name, LY_VALUE_JSON, output, node, ap);
-
- va_end(ap);
- return rc;
-}
-
-LIBYANG_API_DEF LY_ERR
-lyd_new_list_bin(struct lyd_node *parent, const struct lys_module *module, const char *name, ly_bool output,
- struct lyd_node **node, ...)
-{
- LY_ERR rc;
- va_list ap;
-
- va_start(ap, node);
-
- rc = _lyd_new_list(parent, module, name, LY_VALUE_LYB, output, node, ap);
-
- va_end(ap);
- return rc;
-}
-
-LIBYANG_API_DEF LY_ERR
-lyd_new_list_canon(struct lyd_node *parent, const struct lys_module *module, const char *name, ly_bool output,
- struct lyd_node **node, ...)
-{
- LY_ERR rc;
- va_list ap;
-
- va_start(ap, node);
-
- rc = _lyd_new_list(parent, module, name, LY_VALUE_CANON, output, node, ap);
-
- va_end(ap);
- return rc;
-}
-
-LIBYANG_API_DEF LY_ERR
-lyd_new_ext_list(const struct lysc_ext_instance *ext, const char *name, struct lyd_node **node, ...)
-{
- struct lyd_node *ret = NULL, *key;
- const struct lysc_node *schema, *key_s;
- struct ly_ctx *ctx = ext ? ext->module->ctx : NULL;
- va_list ap;
- const char *key_val;
- LY_ERR rc = LY_SUCCESS;
-
- LY_CHECK_ARG_RET(ctx, ext, node, name, LY_EINVAL);
-
- schema = lysc_ext_find_node(ext, NULL, name, 0, LYS_LIST, 0);
- if (!schema) {
- if (ext->argument) {
- LOGERR(ctx, LY_EINVAL, "List node \"%s\" not found in instance \"%s\" of extension %s.",
- name, ext->argument, ext->def->name);
- } else {
- LOGERR(ctx, LY_EINVAL, "List node \"%s\" not found in instance of extension %s.", name, ext->def->name);
- }
- return LY_ENOTFOUND;
- }
- /* create list inner node */
- LY_CHECK_RET(lyd_create_inner(schema, &ret));
-
- va_start(ap, node);
-
- /* create and insert all the keys */
- for (key_s = lysc_node_child(schema); key_s && (key_s->flags & LYS_KEY); key_s = key_s->next) {
- key_val = va_arg(ap, const char *);
-
- rc = lyd_create_term(key_s, key_val, key_val ? strlen(key_val) : 0, NULL, LY_VALUE_JSON, NULL, LYD_HINT_DATA,
- NULL, &key);
- LY_CHECK_GOTO(rc, cleanup);
- lyd_insert_node(ret, NULL, key, 1);
- }
-
-cleanup:
- va_end(ap);
- if (rc) {
- lyd_free_tree(ret);
- ret = NULL;
- }
- *node = ret;
- return rc;
-}
-
-LIBYANG_API_DEF LY_ERR
-lyd_new_list2(struct lyd_node *parent, const struct lys_module *module, const char *name, const char *keys,
- ly_bool output, struct lyd_node **node)
-{
- LY_ERR r;
- struct lyd_node *ret = NULL;
- const struct lysc_node *schema;
- struct lysc_ext_instance *ext = NULL;
- const struct ly_ctx *ctx = parent ? LYD_CTX(parent) : (module ? module->ctx : NULL);
-
- LY_CHECK_ARG_RET(ctx, parent || module, parent || node, name, LY_EINVAL);
- LY_CHECK_CTX_EQUAL_RET(parent ? LYD_CTX(parent) : NULL, module ? module->ctx : NULL, LY_EINVAL);
-
- if (!module) {
- module = parent->schema->module;
- }
- if (!keys) {
- keys = "";
- }
-
- /* find schema node */
- schema = lys_find_child(parent ? parent->schema : NULL, module, name, 0, LYS_LIST, output ? LYS_GETNEXT_OUTPUT : 0);
- if (!schema && parent) {
- r = ly_nested_ext_schema(parent, NULL, module->name, strlen(module->name), LY_VALUE_JSON, NULL, name, strlen(name),
- &schema, &ext);
- LY_CHECK_RET(r && (r != LY_ENOT), r);
- }
- LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "List node \"%s\" not found.", name), LY_ENOTFOUND);
-
- if ((schema->flags & LYS_KEYLESS) && !keys[0]) {
- /* key-less list */
- LY_CHECK_RET(lyd_create_inner(schema, &ret));
- } else {
- /* create the list node */
- LY_CHECK_RET(lyd_create_list2(schema, keys, strlen(keys), &ret));
- }
- if (ext) {
- ret->flags |= LYD_EXT;
- }
- if (parent) {
- lyd_insert_node(parent, NULL, ret, 0);
- }
-
- if (node) {
- *node = ret;
- }
- return LY_SUCCESS;
-}
-
-/**
- * @brief Create a new term node in the data tree.
- *
- * @param[in] parent Parent node for the node being created. NULL in case of creating a top level element.
- * @param[in] module Module of the node being created. If NULL, @p parent module will be used.
- * @param[in] name Schema node name of the new data node. The node can be ::LYS_LEAF or ::LYS_LEAFLIST.
- * @param[in] value Value of the node being created.
- * @param[in] value_len Length of @p value.
- * @param[in] format Format of @p value.
- * @param[in] output Flag in case the @p parent is RPC/Action. If value is 0, the input's data nodes of the RPC/Action are
- * taken into consideration. Otherwise, the output's data node is going to be created.
- * @param[out] node Optional created node.
- * @return LY_ERR value.
- */
-static LY_ERR
-_lyd_new_term(struct lyd_node *parent, const struct lys_module *module, const char *name, const void *value,
- size_t value_len, LY_VALUE_FORMAT format, ly_bool output, struct lyd_node **node)
-{
- LY_ERR r;
- struct lyd_node *ret = NULL;
- const struct lysc_node *schema;
- struct lysc_ext_instance *ext = NULL;
- const struct ly_ctx *ctx = parent ? LYD_CTX(parent) : (module ? module->ctx : NULL);
-
- LY_CHECK_ARG_RET(ctx, parent || module, parent || node, name, LY_EINVAL);
- LY_CHECK_CTX_EQUAL_RET(parent ? LYD_CTX(parent) : NULL, module ? module->ctx : NULL, LY_EINVAL);
-
- if (!module) {
- module = parent->schema->module;
- }
-
- schema = lys_find_child(parent ? parent->schema : NULL, module, name, 0, LYD_NODE_TERM, output ? LYS_GETNEXT_OUTPUT : 0);
- if (!schema && parent) {
- r = ly_nested_ext_schema(parent, NULL, module->name, strlen(module->name), LY_VALUE_JSON, NULL, name,
- strlen(name), &schema, &ext);
- LY_CHECK_RET(r && (r != LY_ENOT), r);
- }
- LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "Term node \"%s\" not found.", name), LY_ENOTFOUND);
-
- LY_CHECK_RET(lyd_create_term(schema, value, value_len, NULL, format, NULL, LYD_HINT_DATA, NULL, &ret));
- if (ext) {
- ret->flags |= LYD_EXT;
- }
- if (parent) {
- lyd_insert_node(parent, NULL, ret, 0);
- }
-
- if (node) {
- *node = ret;
- }
- return LY_SUCCESS;
-}
-
-LIBYANG_API_DEF LY_ERR
-lyd_new_term(struct lyd_node *parent, const struct lys_module *module, const char *name, const char *val_str,
- ly_bool output, struct lyd_node **node)
-{
- return _lyd_new_term(parent, module, name, val_str, val_str ? strlen(val_str) : 0, LY_VALUE_JSON, output, node);
-}
-
-LIBYANG_API_DEF LY_ERR
-lyd_new_term_bin(struct lyd_node *parent, const struct lys_module *module, const char *name, const void *value,
- size_t value_len, ly_bool output, struct lyd_node **node)
-{
- return _lyd_new_term(parent, module, name, value, value_len, LY_VALUE_LYB, output, node);
-}
-
-LIBYANG_API_DEF LY_ERR
-lyd_new_term_canon(struct lyd_node *parent, const struct lys_module *module, const char *name, const char *val_str,
- ly_bool output, struct lyd_node **node)
-{
- return _lyd_new_term(parent, module, name, val_str, val_str ? strlen(val_str) : 0, LY_VALUE_CANON, output, node);
-}
-
-LIBYANG_API_DEF LY_ERR
-lyd_new_ext_term(const struct lysc_ext_instance *ext, const char *name, const char *val_str, struct lyd_node **node)
-{
- LY_ERR rc;
- struct lyd_node *ret = NULL;
- const struct lysc_node *schema;
- struct ly_ctx *ctx = ext ? ext->module->ctx : NULL;
-
- LY_CHECK_ARG_RET(ctx, ext, node, name, LY_EINVAL);
-
- schema = lysc_ext_find_node(ext, NULL, name, 0, LYD_NODE_TERM, 0);
- if (!schema) {
- if (ext->argument) {
- LOGERR(ctx, LY_EINVAL, "Term node \"%s\" not found in instance \"%s\" of extension %s.",
- name, ext->argument, ext->def->name);
- } else {
- LOGERR(ctx, LY_EINVAL, "Term node \"%s\" not found in instance of extension %s.", name, ext->def->name);
- }
- return LY_ENOTFOUND;
- }
- rc = lyd_create_term(schema, val_str, val_str ? strlen(val_str) : 0, NULL, LY_VALUE_JSON, NULL, LYD_HINT_DATA, NULL, &ret);
- LY_CHECK_RET(rc);
-
- *node = ret;
-
- return LY_SUCCESS;
-}
-
-LIBYANG_API_DEF LY_ERR
-lyd_new_any(struct lyd_node *parent, const struct lys_module *module, const char *name, const void *value,
- ly_bool use_value, LYD_ANYDATA_VALUETYPE value_type, ly_bool output, struct lyd_node **node)
-{
- LY_ERR r;
- struct lyd_node *ret = NULL;
- const struct lysc_node *schema;
- struct lysc_ext_instance *ext = NULL;
- const struct ly_ctx *ctx = parent ? LYD_CTX(parent) : (module ? module->ctx : NULL);
-
- LY_CHECK_ARG_RET(ctx, parent || module, parent || node, name, LY_EINVAL);
- LY_CHECK_CTX_EQUAL_RET(parent ? LYD_CTX(parent) : NULL, module ? module->ctx : NULL, LY_EINVAL);
-
- if (!module) {
- module = parent->schema->module;
- }
-
- schema = lys_find_child(parent ? parent->schema : NULL, module, name, 0, LYD_NODE_ANY, output ? LYS_GETNEXT_OUTPUT : 0);
- if (!schema && parent) {
- r = ly_nested_ext_schema(parent, NULL, module->name, strlen(module->name), LY_VALUE_JSON, NULL, name,
- strlen(name), &schema, &ext);
- LY_CHECK_RET(r && (r != LY_ENOT), r);
- }
- LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "Any node \"%s\" not found.", name), LY_ENOTFOUND);
-
- LY_CHECK_RET(lyd_create_any(schema, value, value_type, use_value, &ret));
- if (ext) {
- ret->flags |= LYD_EXT;
- }
- if (parent) {
- lyd_insert_node(parent, NULL, ret, 0);
- }
-
- if (node) {
- *node = ret;
- }
- return LY_SUCCESS;
-}
-
-LIBYANG_API_DEF LY_ERR
-lyd_new_ext_any(const struct lysc_ext_instance *ext, const char *name, const void *value, ly_bool use_value,
- LYD_ANYDATA_VALUETYPE value_type, struct lyd_node **node)
-{
- struct lyd_node *ret = NULL;
- const struct lysc_node *schema;
- struct ly_ctx *ctx = ext ? ext->module->ctx : NULL;
-
- LY_CHECK_ARG_RET(ctx, ext, node, name, LY_EINVAL);
-
- schema = lysc_ext_find_node(ext, NULL, name, 0, LYD_NODE_ANY, 0);
- if (!schema) {
- if (ext->argument) {
- LOGERR(ctx, LY_EINVAL, "Any node \"%s\" not found in instance \"%s\" of extension %s.",
- name, ext->argument, ext->def->name);
- } else {
- LOGERR(ctx, LY_EINVAL, "Any node \"%s\" not found in instance of extension %s.", name, ext->def->name);
- }
- return LY_ENOTFOUND;
- }
- LY_CHECK_RET(lyd_create_any(schema, value, value_type, use_value, &ret));
-
- *node = ret;
-
- return LY_SUCCESS;
-}
-
-LIBYANG_API_DEF LY_ERR
-lyd_new_meta(const struct ly_ctx *ctx, struct lyd_node *parent, const struct lys_module *module, const char *name,
- const char *val_str, ly_bool clear_dflt, struct lyd_meta **meta)
-{
- const char *prefix, *tmp;
- size_t pref_len, name_len;
-
- LY_CHECK_ARG_RET(ctx, ctx || parent, name, module || strchr(name, ':'), parent || meta, LY_EINVAL);
- LY_CHECK_CTX_EQUAL_RET(ctx, parent ? LYD_CTX(parent) : NULL, module ? module->ctx : NULL, LY_EINVAL);
- if (!ctx) {
- ctx = LYD_CTX(parent);
- }
-
- if (parent && !parent->schema) {
- LOGERR(ctx, LY_EINVAL, "Cannot add metadata \"%s\" to an opaque node \"%s\".", name, LYD_NAME(parent));
- return LY_EINVAL;
- }
- if (meta) {
- *meta = NULL;
- }
-
- /* parse the name */
- tmp = name;
- if (ly_parse_nodeid(&tmp, &prefix, &pref_len, &name, &name_len) || tmp[0]) {
- LOGERR(ctx, LY_EINVAL, "Metadata name \"%s\" is not valid.", name);
- return LY_EINVAL;
- }
-
- /* find the module */
- if (prefix) {
- module = ly_ctx_get_module_implemented2(ctx, prefix, pref_len);
- LY_CHECK_ERR_RET(!module, LOGERR(ctx, LY_EINVAL, "Module \"%.*s\" not found.", (int)pref_len, prefix), LY_ENOTFOUND);
- }
-
- /* set value if none */
- if (!val_str) {
- val_str = "";
- }
-
- return lyd_create_meta(parent, meta, module, name, name_len, val_str, strlen(val_str), NULL, LY_VALUE_JSON,
- NULL, LYD_HINT_DATA, parent ? parent->schema : NULL, clear_dflt, NULL);
-}
-
-LIBYANG_API_DEF LY_ERR
-lyd_new_meta2(const struct ly_ctx *ctx, struct lyd_node *parent, ly_bool clear_dflt, const struct lyd_attr *attr,
- struct lyd_meta **meta)
-{
- const struct lys_module *mod;
-
- LY_CHECK_ARG_RET(NULL, ctx, attr, parent || meta, LY_EINVAL);
- LY_CHECK_CTX_EQUAL_RET(ctx, parent ? LYD_CTX(parent) : NULL, LY_EINVAL);
-
- if (parent && !parent->schema) {
- LOGERR(ctx, LY_EINVAL, "Cannot add metadata to an opaque node \"%s\".", ((struct lyd_node_opaq *)parent)->name);
- return LY_EINVAL;
- }
- if (meta) {
- *meta = NULL;
- }
-
- switch (attr->format) {
- case LY_VALUE_XML:
- mod = ly_ctx_get_module_implemented_ns(ctx, attr->name.module_ns);
- if (!mod) {
- LOGERR(ctx, LY_EINVAL, "Module with namespace \"%s\" not found.", attr->name.module_ns);
- return LY_ENOTFOUND;
- }
- break;
- case LY_VALUE_JSON:
- mod = ly_ctx_get_module_implemented(ctx, attr->name.module_name);
- if (!mod) {
- LOGERR(ctx, LY_EINVAL, "Module \"%s\" not found.", attr->name.module_name);
- return LY_ENOTFOUND;
- }
- break;
- default:
- LOGINT_RET(ctx);
- }
-
- return lyd_create_meta(parent, meta, mod, attr->name.name, strlen(attr->name.name), attr->value, strlen(attr->value),
- NULL, attr->format, attr->val_prefix_data, attr->hints, parent ? parent->schema : NULL, clear_dflt, NULL);
-}
-
-LIBYANG_API_DEF LY_ERR
-lyd_new_opaq(struct lyd_node *parent, const struct ly_ctx *ctx, const char *name, const char *value,
- const char *prefix, const char *module_name, struct lyd_node **node)
-{
- struct lyd_node *ret = NULL;
-
- LY_CHECK_ARG_RET(ctx, parent || ctx, parent || node, name, module_name, !prefix || !strcmp(prefix, module_name), LY_EINVAL);
- LY_CHECK_CTX_EQUAL_RET(ctx, parent ? LYD_CTX(parent) : NULL, LY_EINVAL);
-
- if (!ctx) {
- ctx = LYD_CTX(parent);
- }
- if (!value) {
- value = "";
- }
-
- LY_CHECK_RET(lyd_create_opaq(ctx, name, strlen(name), prefix, prefix ? strlen(prefix) : 0, module_name,
- strlen(module_name), value, strlen(value), NULL, LY_VALUE_JSON, NULL, 0, &ret));
- if (parent) {
- lyd_insert_node(parent, NULL, ret, 1);
- }
-
- if (node) {
- *node = ret;
- }
- return LY_SUCCESS;
-}
-
-LIBYANG_API_DEF LY_ERR
-lyd_new_opaq2(struct lyd_node *parent, const struct ly_ctx *ctx, const char *name, const char *value,
- const char *prefix, const char *module_ns, struct lyd_node **node)
-{
- struct lyd_node *ret = NULL;
-
- LY_CHECK_ARG_RET(ctx, parent || ctx, parent || node, name, module_ns, LY_EINVAL);
- LY_CHECK_CTX_EQUAL_RET(ctx, parent ? LYD_CTX(parent) : NULL, LY_EINVAL);
-
- if (!ctx) {
- ctx = LYD_CTX(parent);
- }
- if (!value) {
- value = "";
- }
-
- LY_CHECK_RET(lyd_create_opaq(ctx, name, strlen(name), prefix, prefix ? strlen(prefix) : 0, module_ns,
- strlen(module_ns), value, strlen(value), NULL, LY_VALUE_XML, NULL, 0, &ret));
- if (parent) {
- lyd_insert_node(parent, NULL, ret, 1);
- }
-
- if (node) {
- *node = ret;
- }
- return LY_SUCCESS;
-}
-
-LIBYANG_API_DEF LY_ERR
-lyd_new_attr(struct lyd_node *parent, const char *module_name, const char *name, const char *value,
- struct lyd_attr **attr)
-{
- struct lyd_attr *ret = NULL;
- const struct ly_ctx *ctx;
- const char *prefix, *tmp;
- size_t pref_len, name_len, mod_len;
-
- LY_CHECK_ARG_RET(NULL, parent, !parent->schema, name, LY_EINVAL);
-
- ctx = LYD_CTX(parent);
-
- /* parse the name */
- tmp = name;
- if (ly_parse_nodeid(&tmp, &prefix, &pref_len, &name, &name_len) || tmp[0]) {
- LOGERR(ctx, LY_EINVAL, "Attribute name \"%s\" is not valid.", name);
- return LY_EVALID;
- }
-
- if ((pref_len == 3) && !strncmp(prefix, "xml", 3)) {
- /* not a prefix but special name */
- name = prefix;
- name_len += 1 + pref_len;
- prefix = NULL;
- pref_len = 0;
- }
-
- /* get the module */
- if (module_name) {
- mod_len = strlen(module_name);
- } else {
- module_name = prefix;
- mod_len = pref_len;
- }
-
- /* set value if none */
- if (!value) {
- value = "";
- }
-
- LY_CHECK_RET(lyd_create_attr(parent, &ret, ctx, name, name_len, prefix, pref_len, module_name, mod_len, value,
- strlen(value), NULL, LY_VALUE_JSON, NULL, LYD_HINT_DATA));
-
- if (attr) {
- *attr = ret;
- }
- return LY_SUCCESS;
-}
-
-LIBYANG_API_DEF LY_ERR
-lyd_new_attr2(struct lyd_node *parent, const char *module_ns, const char *name, const char *value,
- struct lyd_attr **attr)
-{
- struct lyd_attr *ret = NULL;
- const struct ly_ctx *ctx;
- const char *prefix, *tmp;
- size_t pref_len, name_len;
-
- LY_CHECK_ARG_RET(NULL, parent, !parent->schema, name, LY_EINVAL);
-
- ctx = LYD_CTX(parent);
-
- /* parse the name */
- tmp = name;
- if (ly_parse_nodeid(&tmp, &prefix, &pref_len, &name, &name_len) || tmp[0]) {
- LOGERR(ctx, LY_EINVAL, "Attribute name \"%s\" is not valid.", name);
- return LY_EVALID;
- }
-
- if ((pref_len == 3) && !strncmp(prefix, "xml", 3)) {
- /* not a prefix but special name */
- name = prefix;
- name_len += 1 + pref_len;
- prefix = NULL;
- pref_len = 0;
- }
-
- /* set value if none */
- if (!value) {
- value = "";
- }
-
- LY_CHECK_RET(lyd_create_attr(parent, &ret, ctx, name, name_len, prefix, pref_len, module_ns,
- module_ns ? strlen(module_ns) : 0, value, strlen(value), NULL, LY_VALUE_XML, NULL, LYD_HINT_DATA));
-
- if (attr) {
- *attr = ret;
- }
- return LY_SUCCESS;
-}
-
-/**
- * @brief Change the value of a term (leaf or leaf-list) node.
- *
- * Node changed this way is always considered explicitly set, meaning its default flag
- * is always cleared.
- *
- * @param[in] term Term node to change.
- * @param[in] value New value to set.
- * @param[in] value_len Length of @p value.
- * @param[in] format Format of @p value.
- * @return LY_SUCCESS if value was changed,
- * @return LY_EEXIST if value was the same and only the default flag was cleared,
- * @return LY_ENOT if the values were equal and no change occured,
- * @return LY_ERR value on other errors.
- */
-static LY_ERR
-_lyd_change_term(struct lyd_node *term, const void *value, size_t value_len, LY_VALUE_FORMAT format)
-{
- LY_ERR ret = LY_SUCCESS;
- struct lysc_type *type;
- struct lyd_node_term *t;
- struct lyd_node *parent;
- struct lyd_value val;
- ly_bool dflt_change, val_change;
-
- assert(term && term->schema && (term->schema->nodetype & LYD_NODE_TERM));
-
- t = (struct lyd_node_term *)term;
- type = ((struct lysc_node_leaf *)term->schema)->type;
-
- /* parse the new value */
- LOG_LOCSET(term->schema, term, NULL, NULL);
- ret = lyd_value_store(LYD_CTX(term), &val, type, value, value_len, NULL, format, NULL, LYD_HINT_DATA, term->schema, NULL);
- LOG_LOCBACK(term->schema ? 1 : 0, 1, 0, 0);
- LY_CHECK_GOTO(ret, cleanup);
-
- /* compare original and new value */
- if (type->plugin->compare(&t->value, &val)) {
- /* values differ, switch them */
- type->plugin->free(LYD_CTX(term), &t->value);
- t->value = val;
- val_change = 1;
- } else {
- /* same values, free the new stored one */
- type->plugin->free(LYD_CTX(term), &val);
- val_change = 0;
- }
-
- /* always clear the default flag */
- if (term->flags & LYD_DEFAULT) {
- for (parent = term; parent; parent = lyd_parent(parent)) {
- parent->flags &= ~LYD_DEFAULT;
- }
- dflt_change = 1;
- } else {
- dflt_change = 0;
- }
-
- if (val_change || dflt_change) {
- /* make the node non-validated */
- term->flags &= LYD_NEW;
- }
-
- if (val_change) {
- if (term->schema->nodetype == LYS_LEAFLIST) {
- /* leaf-list needs to be hashed again and re-inserted into parent */
- lyd_unlink_hash(term);
- lyd_hash(term);
- LY_CHECK_GOTO(ret = lyd_insert_hash(term), cleanup);
- } else if ((term->schema->flags & LYS_KEY) && term->parent) {
- /* list needs to be updated if its key was changed */
- assert(term->parent->schema->nodetype == LYS_LIST);
- lyd_unlink_hash(lyd_parent(term));
- lyd_hash(lyd_parent(term));
- LY_CHECK_GOTO(ret = lyd_insert_hash(lyd_parent(term)), cleanup);
- } /* else leaf that is not a key, its value is not used for its hash so it does not change */
- }
-
- /* retrun value */
- if (!val_change) {
- if (dflt_change) {
- /* only default flag change */
- ret = LY_EEXIST;
- } else {
- /* no change */
- ret = LY_ENOT;
- }
- } /* else value changed, LY_SUCCESS */
-
-cleanup:
- return ret;
-}
-
-LIBYANG_API_DEF LY_ERR
-lyd_change_term(struct lyd_node *term, const char *val_str)
-{
- LY_CHECK_ARG_RET(NULL, term, term->schema, term->schema->nodetype & LYD_NODE_TERM, LY_EINVAL);
-
- return _lyd_change_term(term, val_str, val_str ? strlen(val_str) : 0, LY_VALUE_JSON);
-}
-
-LIBYANG_API_DEF LY_ERR
-lyd_change_term_bin(struct lyd_node *term, const void *value, size_t value_len)
-{
- LY_CHECK_ARG_RET(NULL, term, term->schema, term->schema->nodetype & LYD_NODE_TERM, LY_EINVAL);
-
- return _lyd_change_term(term, value, value_len, LY_VALUE_LYB);
-}
-
-LIBYANG_API_DEF LY_ERR
-lyd_change_term_canon(struct lyd_node *term, const char *val_str)
-{
- LY_CHECK_ARG_RET(NULL, term, term->schema, term->schema->nodetype & LYD_NODE_TERM, LY_EINVAL);
-
- return _lyd_change_term(term, val_str, val_str ? strlen(val_str) : 0, LY_VALUE_CANON);
-}
-
-LIBYANG_API_DEF LY_ERR
-lyd_change_meta(struct lyd_meta *meta, const char *val_str)
-{
- LY_ERR ret = LY_SUCCESS;
- struct lyd_meta *m2 = NULL;
- struct lyd_value val;
- ly_bool val_change;
-
- LY_CHECK_ARG_RET(NULL, meta, LY_EINVAL);
-
- if (!val_str) {
- val_str = "";
- }
-
- /* parse the new value into a new meta structure */
- ret = lyd_create_meta(NULL, &m2, meta->annotation->module, meta->name, strlen(meta->name), val_str, strlen(val_str),
- NULL, LY_VALUE_JSON, NULL, LYD_HINT_DATA, meta->parent ? meta->parent->schema : NULL, 0, NULL);
- LY_CHECK_GOTO(ret, cleanup);
-
- /* compare original and new value */
- if (lyd_compare_meta(meta, m2)) {
- /* values differ, switch them */
- val = meta->value;
- meta->value = m2->value;
- m2->value = val;
- val_change = 1;
- } else {
- val_change = 0;
- }
-
- /* retrun value */
- if (!val_change) {
- /* no change */
- ret = LY_ENOT;
- } /* else value changed, LY_SUCCESS */
-
-cleanup:
- lyd_free_meta_single(m2);
- return ret;
-}
-
-/**
- * @brief Update node value.
- *
- * @param[in] node Node to update.
- * @param[in] value New value to set.
- * @param[in] value_len Length of @p value.
- * @param[in] value_type Type of @p value for anydata/anyxml node.
- * @param[in] format Format of @p value.
- * @param[out] new_parent Set to @p node if the value was updated, otherwise set to NULL.
- * @param[out] new_node Set to @p node if the value was updated, otherwise set to NULL.
- * @return LY_ERR value.
- */
-static LY_ERR
-lyd_new_path_update(struct lyd_node *node, const void *value, size_t value_len, LYD_ANYDATA_VALUETYPE value_type,
- LY_VALUE_FORMAT format, struct lyd_node **new_parent, struct lyd_node **new_node)
-{
- LY_ERR ret = LY_SUCCESS;
- struct lyd_node *new_any;
-
- switch (node->schema->nodetype) {
- case LYS_CONTAINER:
- case LYS_NOTIF:
- case LYS_RPC:
- case LYS_ACTION:
- case LYS_LIST:
- /* if it exists, there is nothing to update */
- *new_parent = NULL;
- *new_node = NULL;
- break;
- case LYS_LEAFLIST:
- if (!lysc_is_dup_inst_list(node->schema)) {
- /* if it exists, there is nothing to update */
- *new_parent = NULL;
- *new_node = NULL;
- break;
- }
- /* fallthrough */
- case LYS_LEAF:
- ret = _lyd_change_term(node, value, value_len, format);
- if ((ret == LY_SUCCESS) || (ret == LY_EEXIST)) {
- /* there was an actual change (at least of the default flag) */
- *new_parent = node;
- *new_node = node;
- ret = LY_SUCCESS;
- } else if (ret == LY_ENOT) {
- /* no change */
- *new_parent = NULL;
- *new_node = NULL;
- ret = LY_SUCCESS;
- } /* else error */
- break;
- case LYS_ANYDATA:
- case LYS_ANYXML:
- /* create a new any node */
- LY_CHECK_RET(lyd_create_any(node->schema, value, value_type, 0, &new_any));
-
- /* compare with the existing one */
- if (lyd_compare_single(node, new_any, 0)) {
- /* not equal, switch values (so that we can use generic node free) */
- ((struct lyd_node_any *)new_any)->value = ((struct lyd_node_any *)node)->value;
- ((struct lyd_node_any *)new_any)->value_type = ((struct lyd_node_any *)node)->value_type;
- ((struct lyd_node_any *)node)->value.str = value;
- ((struct lyd_node_any *)node)->value_type = value_type;
-
- *new_parent = node;
- *new_node = node;
- } else {
- /* they are equal */
- *new_parent = NULL;
- *new_node = NULL;
- }
- lyd_free_tree(new_any);
- break;
- default:
- LOGINT(LYD_CTX(node));
- ret = LY_EINT;
- break;
- }
-
- return ret;
-}
-
-static LY_ERR
-lyd_new_path_check_find_lypath(struct ly_path *path, const char *str_path, const char *value, size_t value_len,
- LY_VALUE_FORMAT format, uint32_t options)
-{
- LY_ERR r;
- struct ly_path_predicate *pred;
- struct lyd_value val;
- const struct lysc_node *schema = NULL;
- LY_ARRAY_COUNT_TYPE u, new_count;
- int create = 0;
-
- assert(path);
-
- /* go through all the compiled nodes */
- LY_ARRAY_FOR(path, u) {
- schema = path[u].node;
-
- if (lysc_is_dup_inst_list(schema)) {
- if (path[u].pred_type == LY_PATH_PREDTYPE_NONE) {
- /* creating a new key-less list or state leaf-list instance */
- create = 1;
- new_count = u;
- } else if (path[u].pred_type != LY_PATH_PREDTYPE_POSITION) {
- LOG_LOCSET(schema, NULL, NULL, NULL);
- LOGVAL(schema->module->ctx, LYVE_XPATH, "Invalid predicate for %s \"%s\" in path \"%s\".",
- lys_nodetype2str(schema->nodetype), schema->name, str_path);
- LOG_LOCBACK(1, 0, 0, 0);
- return LY_EINVAL;
- }
- } else if ((schema->nodetype == LYS_LIST) && (path[u].pred_type != LY_PATH_PREDTYPE_LIST)) {
- if ((u < LY_ARRAY_COUNT(path) - 1) || !(options & LYD_NEW_PATH_OPAQ)) {
- LOG_LOCSET(schema, NULL, NULL, NULL);
- LOGVAL(schema->module->ctx, LYVE_XPATH, "Predicate missing for %s \"%s\" in path \"%s\".",
- lys_nodetype2str(schema->nodetype), schema->name, str_path);
- LOG_LOCBACK(1, 0, 0, 0);
- return LY_EINVAL;
- } /* else creating an opaque list */
- } else if ((schema->nodetype == LYS_LEAFLIST) && (path[u].pred_type != LY_PATH_PREDTYPE_LEAFLIST)) {
- r = LY_SUCCESS;
- if (options & LYD_NEW_PATH_OPAQ) {
- r = lyd_value_validate(NULL, schema, value, value_len, NULL, NULL, NULL);
- }
- if (!r) {
- /* try to store the value */
- LY_CHECK_RET(lyd_value_store(schema->module->ctx, &val, ((struct lysc_node_leaflist *)schema)->type,
- value, value_len, NULL, format, NULL, LYD_HINT_DATA, schema, NULL));
- ++((struct lysc_type *)val.realtype)->refcount;
-
- /* store the new predicate so that it is used when searching for this instance */
- path[u].pred_type = LY_PATH_PREDTYPE_LEAFLIST;
- LY_ARRAY_NEW_RET(schema->module->ctx, path[u].predicates, pred, LY_EMEM);
- pred->value = val;
- } /* else we have opaq flag and the value is not valid, leave no predicate and then create an opaque node */
- }
- }
-
- if (create) {
- /* hide the nodes that should always be created so they are not found */
- while (new_count < LY_ARRAY_COUNT(path)) {
- LY_ARRAY_DECREMENT(path);
- }
- }
-
- return LY_SUCCESS;
-}
-
-/**
- * @brief Create a new node in the data tree based on a path. All node types can be created.
- *
- * If @p path points to a list key, the key value from the predicate is used and @p value is ignored.
- * Also, if a leaf-list is being created and both a predicate is defined in @p path
- * and @p value is set, the predicate is preferred.
- *
- * For key-less lists and state leaf-lists, positional predicates can be used. If no preciate is used for these
- * nodes, they are always created.
- *
- * @param[in] parent Data parent to add to/modify, can be NULL. Note that in case a first top-level sibling is used,
- * it may no longer be first if @p path is absolute and starts with a non-existing top-level node inserted
- * before @p parent. Use ::lyd_first_sibling() to adjust @p parent in these cases.
- * @param[in] ctx libyang context, must be set if @p parent is NULL.
- * @param[in] ext Extension instance where the node being created is defined. This argument takes effect only for absolute
- * path or when the relative paths touches document root (top-level). In such cases the present extension instance replaces
- * searching for the appropriate module.
- * @param[in] path [Path](@ref howtoXPath) to create.
- * @param[in] value Value of the new leaf/leaf-list (const char *) in ::LY_VALUE_JSON format. If creating an
- * anyxml/anydata node, the expected type depends on @p value_type. For other node types, it should be NULL.
- * @param[in] value_len Length of @p value in bytes. May be 0 if @p value is a zero-terminated string. Ignored when
- * creating anyxml/anydata nodes.
- * @param[in] value_type Anyxml/anydata node @p value type.
- * @param[in] options Bitmask of options, see @ref pathoptions.
- * @param[out] new_parent Optional first parent node created. If only one node was created, equals to @p new_node.
- * @param[out] new_node Optional last node created.
- * @return LY_ERR value.
- */
-static LY_ERR
-lyd_new_path_(struct lyd_node *parent, const struct ly_ctx *ctx, const struct lysc_ext_instance *ext, const char *path,
- const void *value, size_t value_len, LYD_ANYDATA_VALUETYPE value_type, uint32_t options,
- struct lyd_node **new_parent, struct lyd_node **new_node)
-{
- LY_ERR ret = LY_SUCCESS, r;
- struct lyxp_expr *exp = NULL;
- struct ly_path *p = NULL;
- struct lyd_node *nparent = NULL, *nnode = NULL, *node = NULL, *cur_parent;
- const struct lysc_node *schema;
- const struct lyd_value *val = NULL;
- LY_ARRAY_COUNT_TYPE path_idx = 0, orig_count = 0;
- LY_VALUE_FORMAT format;
-
- assert(parent || ctx);
- assert(path && ((path[0] == '/') || parent));
- assert(!(options & LYD_NEW_PATH_BIN_VALUE) || !(options & LYD_NEW_PATH_CANON_VALUE));
-
- if (!ctx) {
- ctx = LYD_CTX(parent);
- }
- if (value && !value_len) {
- value_len = strlen(value);
- }
- if (options & LYD_NEW_PATH_BIN_VALUE) {
- format = LY_VALUE_LYB;
- } else if (options & LYD_NEW_PATH_CANON_VALUE) {
- format = LY_VALUE_CANON;
- } else {
- format = LY_VALUE_JSON;
- }
-
- /* parse path */
- LY_CHECK_GOTO(ret = ly_path_parse(ctx, NULL, path, strlen(path), 0, LY_PATH_BEGIN_EITHER, LY_PATH_PREFIX_OPTIONAL,
- LY_PATH_PRED_SIMPLE, &exp), cleanup);
-
- /* compile path */
- LY_CHECK_GOTO(ret = ly_path_compile(ctx, NULL, lyd_node_schema(parent), ext, exp, options & LYD_NEW_PATH_OUTPUT ?
- LY_PATH_OPER_OUTPUT : LY_PATH_OPER_INPUT, LY_PATH_TARGET_MANY, 0, LY_VALUE_JSON, NULL, &p), cleanup);
-
- /* check the compiled path before searching existing nodes, it may be shortened */
- orig_count = LY_ARRAY_COUNT(p);
- LY_CHECK_GOTO(ret = lyd_new_path_check_find_lypath(p, path, value, value_len, format, options), cleanup);
-
- /* try to find any existing nodes in the path */
- if (parent) {
- ret = ly_path_eval_partial(p, parent, &path_idx, &node);
- if (ret == LY_SUCCESS) {
- if (orig_count == LY_ARRAY_COUNT(p)) {
- /* the node exists, are we supposed to update it or is it just a default? */
- if (!(options & LYD_NEW_PATH_UPDATE) && !(node->flags & LYD_DEFAULT)) {
- LOG_LOCSET(NULL, node, NULL, NULL);
- LOGVAL(ctx, LYVE_REFERENCE, "Path \"%s\" already exists", path);
- LOG_LOCBACK(0, 1, 0, 0);
- ret = LY_EEXIST;
- goto cleanup;
- }
-
- /* update the existing node */
- ret = lyd_new_path_update(node, value, value_len, value_type, format, &nparent, &nnode);
- goto cleanup;
- } /* else we were not searching for the whole path */
- } else if (ret == LY_EINCOMPLETE) {
- /* some nodes were found, adjust the iterator to the next segment */
- ++path_idx;
- } else if (ret == LY_ENOTFOUND) {
- /* we will create the nodes from top-level, default behavior (absolute path), or from the parent (relative path) */
- if (lysc_data_parent(p[0].node)) {
- node = parent;
- }
- } else {
- /* error */
- goto cleanup;
- }
- }
-
- /* restore the full path for creating new nodes */
- while (orig_count > LY_ARRAY_COUNT(p)) {
- LY_ARRAY_INCREMENT(p);
- }
-
- /* create all the non-existing nodes in a loop */
- for ( ; path_idx < LY_ARRAY_COUNT(p); ++path_idx) {
- cur_parent = node;
- schema = p[path_idx].node;
-
- switch (schema->nodetype) {
- case LYS_LIST:
- if (lysc_is_dup_inst_list(schema)) {
- /* create key-less list instance */
- LY_CHECK_GOTO(ret = lyd_create_inner(schema, &node), cleanup);
- } else if ((options & LYD_NEW_PATH_OPAQ) && (p[path_idx].pred_type == LY_PATH_PREDTYPE_NONE)) {
- /* creating opaque list without keys */
- LY_CHECK_GOTO(ret = lyd_create_opaq(ctx, schema->name, strlen(schema->name), NULL, 0,
- schema->module->name, strlen(schema->module->name), NULL, 0, NULL, LY_VALUE_JSON, NULL,
- LYD_NODEHINT_LIST, &node), cleanup);
- } else {
- /* create standard list instance */
- assert(p[path_idx].pred_type == LY_PATH_PREDTYPE_LIST);
- LY_CHECK_GOTO(ret = lyd_create_list(schema, p[path_idx].predicates, &node), cleanup);
- }
- break;
- case LYS_CONTAINER:
- case LYS_NOTIF:
- case LYS_RPC:
- case LYS_ACTION:
- LY_CHECK_GOTO(ret = lyd_create_inner(schema, &node), cleanup);
- break;
- case LYS_LEAFLIST:
- if ((options & LYD_NEW_PATH_OPAQ) && (p[path_idx].pred_type != LY_PATH_PREDTYPE_LEAFLIST)) {
- /* we have not checked this only for dup-inst lists, otherwise it must be opaque */
- r = LY_EVALID;
- if (lysc_is_dup_inst_list(schema)) {
- /* validate value */
- r = lyd_value_validate(NULL, schema, value ? value : "", value_len, NULL, NULL, NULL);
- }
- if (r && (r != LY_EINCOMPLETE)) {
- /* creating opaque leaf-list */
- LY_CHECK_GOTO(ret = lyd_create_opaq(ctx, schema->name, strlen(schema->name), value, value_len,
- schema->module->name, strlen(schema->module->name), NULL, 0, NULL, format, NULL,
- LYD_NODEHINT_LEAFLIST, &node), cleanup);
- break;
- }
- }
-
- /* get value to set */
- if (p[path_idx].pred_type == LY_PATH_PREDTYPE_LEAFLIST) {
- val = &p[path_idx].predicates[0].value;
- }
-
- /* create a leaf-list instance */
- if (val) {
- LY_CHECK_GOTO(ret = lyd_create_term2(schema, &p[path_idx].predicates[0].value, &node), cleanup);
- } else {
- LY_CHECK_GOTO(ret = lyd_create_term(schema, value, value_len, NULL, format, NULL, LYD_HINT_DATA,
- NULL, &node), cleanup);
- }
- break;
- case LYS_LEAF:
- if (lysc_is_key(schema) && cur_parent->schema) {
- /* it must have been already created or some error will occur later */
- lyd_find_sibling_schema(lyd_child(cur_parent), schema, &node);
- assert(node);
- goto next_iter;
- }
-
- if (options & LYD_NEW_PATH_OPAQ) {
- if (cur_parent && !cur_parent->schema) {
- /* always create opaque nodes for opaque parents */
- r = LY_ENOT;
- } else {
- /* validate value */
- r = lyd_value_validate(NULL, schema, value ? value : "", value_len, NULL, NULL, NULL);
- }
- if (r && (r != LY_EINCOMPLETE)) {
- /* creating opaque leaf */
- LY_CHECK_GOTO(ret = lyd_create_opaq(ctx, schema->name, strlen(schema->name), value, value_len,
- schema->module->name, strlen(schema->module->name), NULL, 0, NULL, format, NULL, 0, &node),
- cleanup);
- break;
- }
- }
-
- /* create a leaf instance */
- LY_CHECK_GOTO(ret = lyd_create_term(schema, value, value_len, NULL, format, NULL, LYD_HINT_DATA, NULL,
- &node), cleanup);
- break;
- case LYS_ANYDATA:
- case LYS_ANYXML:
- LY_CHECK_GOTO(ret = lyd_create_any(schema, value, value_type, 0, &node), cleanup);
- break;
- default:
- LOGINT(ctx);
- ret = LY_EINT;
- goto cleanup;
- }
-
- if (p[path_idx].ext) {
- node->flags |= LYD_EXT;
- }
- if (cur_parent) {
- /* connect to the parent */
- lyd_insert_node(cur_parent, NULL, node, 0);
- } else if (parent) {
- /* connect to top-level siblings */
- lyd_insert_node(NULL, &parent, node, 0);
- }
-
-next_iter:
- /* update remembered nodes */
- if (!nparent) {
- nparent = node;
- }
- nnode = node;
- }
-
-cleanup:
- lyxp_expr_free(ctx, exp);
- if (p) {
- while (orig_count > LY_ARRAY_COUNT(p)) {
- LY_ARRAY_INCREMENT(p);
- }
- }
- ly_path_free(ctx, p);
- if (!ret) {
- /* set out params only on success */
- if (new_parent) {
- *new_parent = nparent;
- }
- if (new_node) {
- *new_node = nnode;
- }
- } else {
- lyd_free_tree(nparent);
- }
- return ret;
-}
-
-LIBYANG_API_DEF LY_ERR
-lyd_new_path(struct lyd_node *parent, const struct ly_ctx *ctx, const char *path, const char *value, uint32_t options,
- struct lyd_node **node)
-{
- LY_CHECK_ARG_RET(ctx, parent || ctx, path, (path[0] == '/') || parent,
- !(options & LYD_NEW_PATH_BIN_VALUE) || !(options & LYD_NEW_PATH_CANON_VALUE), LY_EINVAL);
- LY_CHECK_CTX_EQUAL_RET(parent ? LYD_CTX(parent) : NULL, ctx, LY_EINVAL);
-
- return lyd_new_path_(parent, ctx, NULL, path, value, 0, LYD_ANYDATA_STRING, options, node, NULL);
-}
-
-LIBYANG_API_DEF LY_ERR
-lyd_new_path2(struct lyd_node *parent, const struct ly_ctx *ctx, const char *path, const void *value,
- size_t value_len, LYD_ANYDATA_VALUETYPE value_type, uint32_t options, struct lyd_node **new_parent,
- struct lyd_node **new_node)
-{
- LY_CHECK_ARG_RET(ctx, parent || ctx, path, (path[0] == '/') || parent,
- !(options & LYD_NEW_PATH_BIN_VALUE) || !(options & LYD_NEW_PATH_CANON_VALUE), LY_EINVAL);
- LY_CHECK_CTX_EQUAL_RET(parent ? LYD_CTX(parent) : NULL, ctx, LY_EINVAL);
-
- return lyd_new_path_(parent, ctx, NULL, path, value, value_len, value_type, options, new_parent, new_node);
-}
-
-LIBYANG_API_DEF LY_ERR
-lyd_new_ext_path(struct lyd_node *parent, const struct lysc_ext_instance *ext, const char *path, const void *value,
- uint32_t options, struct lyd_node **node)
-{
- const struct ly_ctx *ctx = ext ? ext->module->ctx : NULL;
-
- LY_CHECK_ARG_RET(ctx, ext, path, (path[0] == '/') || parent,
- !(options & LYD_NEW_PATH_BIN_VALUE) || !(options & LYD_NEW_PATH_CANON_VALUE), LY_EINVAL);
- LY_CHECK_CTX_EQUAL_RET(parent ? LYD_CTX(parent) : NULL, ctx, LY_EINVAL);
-
- return lyd_new_path_(parent, ctx, ext, path, value, 0, LYD_ANYDATA_STRING, options, node, NULL);
-}
-
-LY_ERR
-lyd_new_implicit_r(struct lyd_node *parent, struct lyd_node **first, const struct lysc_node *sparent,
- const struct lys_module *mod, struct ly_set *node_when, struct ly_set *node_types, uint32_t impl_opts,
- struct lyd_node **diff)
-{
- LY_ERR ret;
- const struct lysc_node *iter = NULL;
- struct lyd_node *node = NULL;
- struct lyd_value **dflts;
- LY_ARRAY_COUNT_TYPE u;
- uint32_t getnext_opts;
-
- assert(first && (parent || sparent || mod));
-
- if (!sparent && parent) {
- sparent = parent->schema;
- }
-
- getnext_opts = LYS_GETNEXT_WITHCHOICE;
- if (impl_opts & LYD_IMPLICIT_OUTPUT) {
- getnext_opts |= LYS_GETNEXT_OUTPUT;
- }
-
- while ((iter = lys_getnext(iter, sparent, mod ? mod->compiled : NULL, getnext_opts))) {
- if ((impl_opts & LYD_IMPLICIT_NO_STATE) && (iter->flags & LYS_CONFIG_R)) {
- continue;
- } else if ((impl_opts & LYD_IMPLICIT_NO_CONFIG) && (iter->flags & LYS_CONFIG_W)) {
- continue;
- }
-
- switch (iter->nodetype) {
- case LYS_CHOICE:
- node = lys_getnext_data(NULL, *first, NULL, iter, NULL);
- if (!node && ((struct lysc_node_choice *)iter)->dflt) {
- /* create default case data */
- LY_CHECK_RET(lyd_new_implicit_r(parent, first, &((struct lysc_node_choice *)iter)->dflt->node,
- NULL, node_when, node_types, impl_opts, diff));
- } else if (node) {
- /* create any default data in the existing case */
- assert(node->schema->parent->nodetype == LYS_CASE);
- LY_CHECK_RET(lyd_new_implicit_r(parent, first, node->schema->parent, NULL, node_when, node_types,
- impl_opts, diff));
- }
- break;
- case LYS_CONTAINER:
- if (!(iter->flags & LYS_PRESENCE) && lyd_find_sibling_val(*first, iter, NULL, 0, NULL)) {
- /* create default NP container */
- LY_CHECK_RET(lyd_create_inner(iter, &node));
- node->flags = LYD_DEFAULT | (lysc_has_when(iter) ? LYD_WHEN_TRUE : 0);
- lyd_insert_node(parent, first, node, 0);
-
- if (lysc_has_when(iter) && node_when) {
- /* remember to resolve when */
- LY_CHECK_RET(ly_set_add(node_when, node, 1, NULL));
- }
- if (diff) {
- /* add into diff */
- LY_CHECK_RET(lyd_val_diff_add(node, LYD_DIFF_OP_CREATE, diff));
- }
-
- /* create any default children */
- LY_CHECK_RET(lyd_new_implicit_r(node, lyd_node_child_p(node), NULL, NULL, node_when, node_types,
- impl_opts, diff));
- }
- break;
- case LYS_LEAF:
- if (!(impl_opts & LYD_IMPLICIT_NO_DEFAULTS) && ((struct lysc_node_leaf *)iter)->dflt &&
- lyd_find_sibling_val(*first, iter, NULL, 0, NULL)) {
- /* create default leaf */
- ret = lyd_create_term2(iter, ((struct lysc_node_leaf *)iter)->dflt, &node);
- if (ret == LY_EINCOMPLETE) {
- if (node_types) {
- /* remember to resolve type */
- LY_CHECK_RET(ly_set_add(node_types, node, 1, NULL));
- }
- } else if (ret) {
- return ret;
- }
- node->flags = LYD_DEFAULT | (lysc_has_when(iter) ? LYD_WHEN_TRUE : 0);
- lyd_insert_node(parent, first, node, 0);
-
- if (lysc_has_when(iter) && node_when) {
- /* remember to resolve when */
- LY_CHECK_RET(ly_set_add(node_when, node, 1, NULL));
- }
- if (diff) {
- /* add into diff */
- LY_CHECK_RET(lyd_val_diff_add(node, LYD_DIFF_OP_CREATE, diff));
- }
- }
- break;
- case LYS_LEAFLIST:
- if (!(impl_opts & LYD_IMPLICIT_NO_DEFAULTS) && ((struct lysc_node_leaflist *)iter)->dflts &&
- lyd_find_sibling_val(*first, iter, NULL, 0, NULL)) {
- /* create all default leaf-lists */
- dflts = ((struct lysc_node_leaflist *)iter)->dflts;
- LY_ARRAY_FOR(dflts, u) {
- ret = lyd_create_term2(iter, dflts[u], &node);
- if (ret == LY_EINCOMPLETE) {
- if (node_types) {
- /* remember to resolve type */
- LY_CHECK_RET(ly_set_add(node_types, node, 1, NULL));
- }
- } else if (ret) {
- return ret;
- }
- node->flags = LYD_DEFAULT | (lysc_has_when(iter) ? LYD_WHEN_TRUE : 0);
- lyd_insert_node(parent, first, node, 0);
-
- if (lysc_has_when(iter) && node_when) {
- /* remember to resolve when */
- LY_CHECK_RET(ly_set_add(node_when, node, 1, NULL));
- }
- if (diff) {
- /* add into diff */
- LY_CHECK_RET(lyd_val_diff_add(node, LYD_DIFF_OP_CREATE, diff));
- }
- }
- }
- break;
- default:
- /* without defaults */
- break;
- }
- }
-
- return LY_SUCCESS;
-}
-
-LIBYANG_API_DEF LY_ERR
-lyd_new_implicit_tree(struct lyd_node *tree, uint32_t implicit_options, struct lyd_node **diff)
-{
- LY_ERR ret = LY_SUCCESS;
- struct lyd_node *node;
- struct ly_set node_when = {0};
-
- LY_CHECK_ARG_RET(NULL, tree, LY_EINVAL);
- if (diff) {
- *diff = NULL;
- }
-
- LYD_TREE_DFS_BEGIN(tree, node) {
- /* skip added default nodes */
- if (((node->flags & (LYD_DEFAULT | LYD_NEW)) != (LYD_DEFAULT | LYD_NEW)) &&
- (node->schema->nodetype & LYD_NODE_INNER)) {
- LY_CHECK_GOTO(ret = lyd_new_implicit_r(node, lyd_node_child_p(node), NULL, NULL, &node_when, NULL,
- implicit_options, diff), cleanup);
- }
-
- LYD_TREE_DFS_END(tree, node);
- }
-
- /* resolve when and remove any invalid defaults */
- LY_CHECK_GOTO(ret = lyd_validate_unres(&tree, NULL, &node_when, LYXP_IGNORE_WHEN, NULL, NULL, NULL, 0, diff), cleanup);
-
-cleanup:
- ly_set_erase(&node_when, NULL);
- if (ret && diff) {
- lyd_free_all(*diff);
- *diff = NULL;
- }
- return ret;
-}
-
-LIBYANG_API_DEF LY_ERR
-lyd_new_implicit_all(struct lyd_node **tree, const struct ly_ctx *ctx, uint32_t implicit_options, struct lyd_node **diff)
-{
- const struct lys_module *mod;
- struct lyd_node *d = NULL;
- uint32_t i = 0;
- LY_ERR ret = LY_SUCCESS;
-
- LY_CHECK_ARG_RET(ctx, tree, *tree || ctx, LY_EINVAL);
- LY_CHECK_CTX_EQUAL_RET(*tree ? LYD_CTX(*tree) : NULL, ctx, LY_EINVAL);
- if (diff) {
- *diff = NULL;
- }
- if (!ctx) {
- ctx = LYD_CTX(*tree);
- }
-
- /* add nodes for each module one-by-one */
- while ((mod = ly_ctx_get_module_iter(ctx, &i))) {
- if (!mod->implemented) {
- continue;
- }
-
- LY_CHECK_GOTO(ret = lyd_new_implicit_module(tree, mod, implicit_options, diff ? &d : NULL), cleanup);
- if (d) {
- /* merge into one diff */
- lyd_insert_sibling(*diff, d, diff);
-
- d = NULL;
- }
- }
-
-cleanup:
- if (ret && diff) {
- lyd_free_all(*diff);
- *diff = NULL;
- }
- return ret;
-}
-
-LIBYANG_API_DEF LY_ERR
-lyd_new_implicit_module(struct lyd_node **tree, const struct lys_module *module, uint32_t implicit_options,
- struct lyd_node **diff)
-{
- LY_ERR ret = LY_SUCCESS;
- struct lyd_node *root, *d = NULL;
- struct ly_set node_when = {0};
-
- LY_CHECK_ARG_RET(NULL, tree, module, LY_EINVAL);
- LY_CHECK_CTX_EQUAL_RET(*tree ? LYD_CTX(*tree) : NULL, module ? module->ctx : NULL, LY_EINVAL);
- if (diff) {
- *diff = NULL;
- }
-
- /* add all top-level defaults for this module */
- LY_CHECK_GOTO(ret = lyd_new_implicit_r(NULL, tree, NULL, module, &node_when, NULL, implicit_options, diff), cleanup);
-
- /* resolve when and remove any invalid defaults */
- LY_CHECK_GOTO(ret = lyd_validate_unres(tree, module, &node_when, LYXP_IGNORE_WHEN, NULL, NULL, NULL, 0, diff),
- cleanup);
-
- /* process nested nodes */
- LY_LIST_FOR(*tree, root) {
- /* skip added default nodes */
- if ((root->flags & (LYD_DEFAULT | LYD_NEW)) != (LYD_DEFAULT | LYD_NEW)) {
- LY_CHECK_GOTO(ret = lyd_new_implicit_tree(root, implicit_options, diff ? &d : NULL), cleanup);
-
- if (d) {
- /* merge into one diff */
- lyd_insert_sibling(*diff, d, diff);
-
- d = NULL;
- }
- }
- }
-
-cleanup:
- ly_set_erase(&node_when, NULL);
- if (ret && diff) {
- lyd_free_all(*diff);
- *diff = NULL;
- }
- return ret;
-}
-
struct lyd_node *
lyd_insert_get_next_anchor(const struct lyd_node *first_sibling, const struct lyd_node *new_node)
{
@@ -4443,103 +2440,6 @@
return LY_SUCCESS;
}
-/**
- * @brief Comparison callback to match schema node with a schema of a data node.
- *
- * @param[in] val1_p Pointer to the schema node
- * @param[in] val2_p Pointer to the data node
- * Implementation of ::lyht_value_equal_cb.
- */
-static ly_bool
-lyd_hash_table_schema_val_equal(void *val1_p, void *val2_p, ly_bool UNUSED(mod), void *UNUSED(cb_data))
-{
- struct lysc_node *val1;
- struct lyd_node *val2;
-
- val1 = *((struct lysc_node **)val1_p);
- val2 = *((struct lyd_node **)val2_p);
-
- if (val1 == val2->schema) {
- /* schema match is enough */
- return 1;
- } else {
- return 0;
- }
-}
-
-/**
- * @brief Search in the given siblings (NOT recursively) for the first schema node data instance.
- * Uses hashes - should be used whenever possible for best performance.
- *
- * @param[in] siblings Siblings to search in including preceding and succeeding nodes.
- * @param[in] schema Target data node schema to find.
- * @param[out] match Can be NULL, otherwise the found data node.
- * @return LY_SUCCESS on success, @p match set.
- * @return LY_ENOTFOUND if not found, @p match set to NULL.
- * @return LY_ERR value if another error occurred.
- */
-static LY_ERR
-lyd_find_sibling_schema(const struct lyd_node *siblings, const struct lysc_node *schema, struct lyd_node **match)
-{
- struct lyd_node **match_p;
- struct lyd_node_inner *parent;
- uint32_t hash;
- lyht_value_equal_cb ht_cb;
-
- assert(siblings && schema);
-
- parent = siblings->parent;
- if (parent && parent->schema && parent->children_ht) {
- /* calculate our hash */
- hash = dict_hash_multi(0, schema->module->name, strlen(schema->module->name));
- hash = dict_hash_multi(hash, schema->name, strlen(schema->name));
- hash = dict_hash_multi(hash, NULL, 0);
-
- /* use special hash table function */
- ht_cb = lyht_set_cb(parent->children_ht, lyd_hash_table_schema_val_equal);
-
- /* find by hash */
- if (!lyht_find(parent->children_ht, &schema, hash, (void **)&match_p)) {
- siblings = *match_p;
- } else {
- /* not found */
- siblings = NULL;
- }
-
- /* set the original hash table compare function back */
- lyht_set_cb(parent->children_ht, ht_cb);
- } else {
- /* find first sibling */
- if (siblings->parent) {
- siblings = siblings->parent->child;
- } else {
- while (siblings->prev->next) {
- siblings = siblings->prev;
- }
- }
-
- /* search manually without hashes */
- for ( ; siblings; siblings = siblings->next) {
- if (siblings->schema == schema) {
- /* schema match is enough */
- break;
- }
- }
- }
-
- if (!siblings) {
- if (match) {
- *match = NULL;
- }
- return LY_ENOTFOUND;
- }
-
- if (match) {
- *match = (struct lyd_node *)siblings;
- }
- return LY_SUCCESS;
-}
-
LIBYANG_API_DEF LY_ERR
lyd_find_sibling_val(const struct lyd_node *siblings, const struct lysc_node *schema, const char *key_or_value,
size_t val_len, struct lyd_node **match)
@@ -4846,44 +2746,3 @@
}
return LY_SUCCESS;
}
-
-LIBYANG_API_DEF uint32_t
-lyd_list_pos(const struct lyd_node *instance)
-{
- const struct lyd_node *iter = NULL;
- uint32_t pos = 0;
-
- if (!instance || !(instance->schema->nodetype & (LYS_LIST | LYS_LEAFLIST))) {
- return 0;
- }
-
- /* data instances are ordered, so we can stop when we found instance of other schema node */
- for (iter = instance; iter->schema == instance->schema; iter = iter->prev) {
- if (pos && (iter->next == NULL)) {
- /* overrun to the end of the siblings list */
- break;
- }
- ++pos;
- }
-
- return pos;
-}
-
-LIBYANG_API_DEF struct lyd_node *
-lyd_first_sibling(const struct lyd_node *node)
-{
- struct lyd_node *start;
-
- if (!node) {
- return NULL;
- }
-
- /* get the first sibling */
- if (node->parent) {
- start = node->parent->child;
- } else {
- for (start = (struct lyd_node *)node; start->prev->next; start = start->prev) {}
- }
-
- return start;
-}
diff --git a/src/tree_data_helpers.c b/src/tree_data_common.c
similarity index 77%
rename from src/tree_data_helpers.c
rename to src/tree_data_common.c
index a364562..762831c 100644
--- a/src/tree_data_helpers.c
+++ b/src/tree_data_common.c
@@ -1,7 +1,8 @@
/**
- * @file tree_data_helpers.c
+ * @file tree_data_common.c
* @author Radek Krejci <rkrejci@cesnet.cz>
- * @brief Parsing and validation helper functions for data trees
+ * @author Michal Vasko <mvasko@cesnet.cz>
+ * @brief Parsing and validation common functions for data trees
*
* Copyright (c) 2015 - 2022 CESNET, z.s.p.o.
*
@@ -453,6 +454,290 @@
return LY_SUCCESS;
}
+LY_ERR
+lyd_value_store(const struct ly_ctx *ctx, struct lyd_value *val, const struct lysc_type *type, const void *value,
+ size_t value_len, ly_bool *dynamic, LY_VALUE_FORMAT format, void *prefix_data, uint32_t hints,
+ const struct lysc_node *ctx_node, ly_bool *incomplete)
+{
+ LY_ERR ret;
+ struct ly_err_item *err = NULL;
+ uint32_t options = (dynamic && *dynamic ? LYPLG_TYPE_STORE_DYNAMIC : 0);
+
+ if (!value) {
+ value = "";
+ }
+ if (incomplete) {
+ *incomplete = 0;
+ }
+
+ ret = type->plugin->store(ctx, type, value, value_len, options, format, prefix_data, hints, ctx_node, val, NULL, &err);
+ if (dynamic) {
+ *dynamic = 0;
+ }
+
+ if (ret == LY_EINCOMPLETE) {
+ if (incomplete) {
+ *incomplete = 1;
+ }
+ } else if (ret) {
+ if (err) {
+ LOGVAL_ERRITEM(ctx, err);
+ ly_err_free(err);
+ } else {
+ LOGVAL(ctx, LYVE_OTHER, "Storing value failed.");
+ }
+ return ret;
+ }
+
+ return LY_SUCCESS;
+}
+
+LY_ERR
+lyd_value_validate_incomplete(const struct ly_ctx *ctx, const struct lysc_type *type, struct lyd_value *val,
+ const struct lyd_node *ctx_node, const struct lyd_node *tree)
+{
+ LY_ERR ret;
+ struct ly_err_item *err = NULL;
+
+ assert(type->plugin->validate);
+
+ ret = type->plugin->validate(ctx, type, ctx_node, tree, val, &err);
+ if (ret) {
+ if (err) {
+ LOGVAL_ERRITEM(ctx, err);
+ ly_err_free(err);
+ } else {
+ LOGVAL(ctx, LYVE_OTHER, "Resolving value \"%s\" failed.", type->plugin->print(ctx, val, LY_VALUE_CANON,
+ NULL, NULL, NULL));
+ }
+ return ret;
+ }
+
+ return LY_SUCCESS;
+}
+
+LY_ERR
+lys_value_validate(const struct ly_ctx *ctx, const struct lysc_node *node, const char *value, size_t value_len,
+ LY_VALUE_FORMAT format, void *prefix_data)
+{
+ LY_ERR rc = LY_SUCCESS;
+ struct ly_err_item *err = NULL;
+ struct lyd_value storage;
+ struct lysc_type *type;
+
+ LY_CHECK_ARG_RET(ctx, node, value, LY_EINVAL);
+
+ if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
+ LOGARG(ctx, node);
+ return LY_EINVAL;
+ }
+
+ type = ((struct lysc_node_leaf *)node)->type;
+ rc = type->plugin->store(ctx ? ctx : node->module->ctx, type, value, value_len, 0, format, prefix_data,
+ LYD_HINT_SCHEMA, node, &storage, NULL, &err);
+ if (rc == LY_EINCOMPLETE) {
+ /* actually success since we do not provide the context tree and call validation with
+ * LY_TYPE_OPTS_INCOMPLETE_DATA */
+ rc = LY_SUCCESS;
+ } else if (rc && err) {
+ if (ctx) {
+ /* log only in case the ctx was provided as input parameter */
+ LOG_LOCSET(NULL, NULL, err->path, NULL);
+ LOGVAL_ERRITEM(ctx, err);
+ LOG_LOCBACK(0, 0, 1, 0);
+ }
+ ly_err_free(err);
+ }
+
+ if (!rc) {
+ type->plugin->free(ctx ? ctx : node->module->ctx, &storage);
+ }
+ return rc;
+}
+
+LIBYANG_API_DEF LY_ERR
+lyd_value_validate(const struct ly_ctx *ctx, const struct lysc_node *schema, const char *value, size_t value_len,
+ const struct lyd_node *ctx_node, const struct lysc_type **realtype, const char **canonical)
+{
+ LY_ERR rc;
+ struct ly_err_item *err = NULL;
+ struct lysc_type *type;
+ struct lyd_value val = {0};
+ ly_bool stored = 0, log = 1;
+
+ LY_CHECK_ARG_RET(ctx, schema, value, LY_EINVAL);
+
+ if (!ctx) {
+ ctx = schema->module->ctx;
+ log = 0;
+ }
+ type = ((struct lysc_node_leaf *)schema)->type;
+
+ /* store */
+ rc = type->plugin->store(ctx, type, value, value_len, 0, LY_VALUE_JSON, NULL,
+ LYD_HINT_DATA, schema, &val, NULL, &err);
+ if (!rc || (rc == LY_EINCOMPLETE)) {
+ stored = 1;
+ }
+
+ if (ctx_node && (rc == LY_EINCOMPLETE)) {
+ /* resolve */
+ rc = type->plugin->validate(ctx, type, ctx_node, ctx_node, &val, &err);
+ }
+
+ if (rc && (rc != LY_EINCOMPLETE) && err) {
+ if (log) {
+ /* log error */
+ if (err->path) {
+ LOG_LOCSET(NULL, NULL, err->path, NULL);
+ } else if (ctx_node) {
+ LOG_LOCSET(NULL, ctx_node, NULL, NULL);
+ } else {
+ LOG_LOCSET(schema, NULL, NULL, NULL);
+ }
+ LOGVAL_ERRITEM(ctx, err);
+ if (err->path) {
+ LOG_LOCBACK(0, 0, 1, 0);
+ } else if (ctx_node) {
+ LOG_LOCBACK(0, 1, 0, 0);
+ } else {
+ LOG_LOCBACK(1, 0, 0, 0);
+ }
+ }
+ ly_err_free(err);
+ }
+
+ if (!rc || (rc == LY_EINCOMPLETE)) {
+ if (realtype) {
+ /* return realtype */
+ if (val.realtype->basetype == LY_TYPE_UNION) {
+ *realtype = val.subvalue->value.realtype;
+ } else {
+ *realtype = val.realtype;
+ }
+ }
+
+ if (canonical) {
+ /* return canonical value */
+ lydict_insert(ctx, val.realtype->plugin->print(ctx, &val, LY_VALUE_CANON, NULL, NULL, NULL), 0, canonical);
+ }
+ }
+
+ if (stored) {
+ /* free value */
+ type->plugin->free(ctx ? ctx : schema->module->ctx, &val);
+ }
+ return rc;
+}
+
+LIBYANG_API_DEF LY_ERR
+lyd_value_compare(const struct lyd_node_term *node, const char *value, size_t value_len)
+{
+ LY_ERR ret = LY_SUCCESS;
+ struct ly_ctx *ctx;
+ struct lysc_type *type;
+ struct lyd_value val = {0};
+
+ LY_CHECK_ARG_RET(node ? node->schema->module->ctx : NULL, node, value, LY_EINVAL);
+
+ ctx = node->schema->module->ctx;
+ type = ((struct lysc_node_leaf *)node->schema)->type;
+
+ /* store the value */
+ LOG_LOCSET(node->schema, &node->node, NULL, NULL);
+ ret = lyd_value_store(ctx, &val, type, value, value_len, NULL, LY_VALUE_JSON, NULL, LYD_HINT_DATA, node->schema, NULL);
+ LOG_LOCBACK(1, 1, 0, 0);
+ LY_CHECK_RET(ret);
+
+ /* compare values */
+ ret = type->plugin->compare(&node->value, &val);
+
+ type->plugin->free(ctx, &val);
+ return ret;
+}
+
+LIBYANG_API_DEF ly_bool
+lyd_is_default(const struct lyd_node *node)
+{
+ const struct lysc_node_leaf *leaf;
+ const struct lysc_node_leaflist *llist;
+ const struct lyd_node_term *term;
+ LY_ARRAY_COUNT_TYPE u;
+
+ if (!(node->schema->nodetype & LYD_NODE_TERM)) {
+ return 0;
+ }
+
+ term = (const struct lyd_node_term *)node;
+
+ if (node->schema->nodetype == LYS_LEAF) {
+ leaf = (const struct lysc_node_leaf *)node->schema;
+ if (!leaf->dflt) {
+ return 0;
+ }
+
+ /* compare with the default value */
+ if (!leaf->type->plugin->compare(&term->value, leaf->dflt)) {
+ return 1;
+ }
+ } else {
+ llist = (const struct lysc_node_leaflist *)node->schema;
+ if (!llist->dflts) {
+ return 0;
+ }
+
+ LY_ARRAY_FOR(llist->dflts, u) {
+ /* compare with each possible default value */
+ if (!llist->type->plugin->compare(&term->value, llist->dflts[u])) {
+ return 1;
+ }
+ }
+ }
+
+ return 0;
+}
+
+LIBYANG_API_DEF uint32_t
+lyd_list_pos(const struct lyd_node *instance)
+{
+ const struct lyd_node *iter = NULL;
+ uint32_t pos = 0;
+
+ if (!instance || !(instance->schema->nodetype & (LYS_LIST | LYS_LEAFLIST))) {
+ return 0;
+ }
+
+ /* data instances are ordered, so we can stop when we found instance of other schema node */
+ for (iter = instance; iter->schema == instance->schema; iter = iter->prev) {
+ if (pos && (iter->next == NULL)) {
+ /* overrun to the end of the siblings list */
+ break;
+ }
+ ++pos;
+ }
+
+ return pos;
+}
+
+LIBYANG_API_DEF struct lyd_node *
+lyd_first_sibling(const struct lyd_node *node)
+{
+ struct lyd_node *start;
+
+ if (!node) {
+ return NULL;
+ }
+
+ /* get the first sibling */
+ if (node->parent) {
+ start = node->parent->child;
+ } else {
+ for (start = (struct lyd_node *)node; start->prev->next; start = start->prev) {}
+ }
+
+ return start;
+}
+
/**
* @brief Check list node parsed into an opaque node for the reason.
*
@@ -778,6 +1063,93 @@
return schema;
}
+
+/**
+ * @brief Comparison callback to match schema node with a schema of a data node.
+ *
+ * @param[in] val1_p Pointer to the schema node
+ * @param[in] val2_p Pointer to the data node
+ * Implementation of ::lyht_value_equal_cb.
+ */
+static ly_bool
+lyd_hash_table_schema_val_equal(void *val1_p, void *val2_p, ly_bool UNUSED(mod), void *UNUSED(cb_data))
+{
+ struct lysc_node *val1;
+ struct lyd_node *val2;
+
+ val1 = *((struct lysc_node **)val1_p);
+ val2 = *((struct lyd_node **)val2_p);
+
+ if (val1 == val2->schema) {
+ /* schema match is enough */
+ return 1;
+ } else {
+ return 0;
+ }
+}
+
+LY_ERR
+lyd_find_sibling_schema(const struct lyd_node *siblings, const struct lysc_node *schema, struct lyd_node **match)
+{
+ struct lyd_node **match_p;
+ struct lyd_node_inner *parent;
+ uint32_t hash;
+ lyht_value_equal_cb ht_cb;
+
+ assert(siblings && schema);
+
+ parent = siblings->parent;
+ if (parent && parent->schema && parent->children_ht) {
+ /* calculate our hash */
+ hash = dict_hash_multi(0, schema->module->name, strlen(schema->module->name));
+ hash = dict_hash_multi(hash, schema->name, strlen(schema->name));
+ hash = dict_hash_multi(hash, NULL, 0);
+
+ /* use special hash table function */
+ ht_cb = lyht_set_cb(parent->children_ht, lyd_hash_table_schema_val_equal);
+
+ /* find by hash */
+ if (!lyht_find(parent->children_ht, &schema, hash, (void **)&match_p)) {
+ siblings = *match_p;
+ } else {
+ /* not found */
+ siblings = NULL;
+ }
+
+ /* set the original hash table compare function back */
+ lyht_set_cb(parent->children_ht, ht_cb);
+ } else {
+ /* find first sibling */
+ if (siblings->parent) {
+ siblings = siblings->parent->child;
+ } else {
+ while (siblings->prev->next) {
+ siblings = siblings->prev;
+ }
+ }
+
+ /* search manually without hashes */
+ for ( ; siblings; siblings = siblings->next) {
+ if (siblings->schema == schema) {
+ /* schema match is enough */
+ break;
+ }
+ }
+ }
+
+ if (!siblings) {
+ if (match) {
+ *match = NULL;
+ }
+ return LY_ENOTFOUND;
+ }
+
+ if (match) {
+ *match = (struct lyd_node *)siblings;
+ }
+ return LY_SUCCESS;
+}
+
void
lyd_del_move_root(struct lyd_node **root, const struct lyd_node *to_del, const struct lys_module *mod)
{
diff --git a/src/tree_data_internal.h b/src/tree_data_internal.h
index f95c2d8..0e45bcb 100644
--- a/src/tree_data_internal.h
+++ b/src/tree_data_internal.h
@@ -146,6 +146,19 @@
const struct lysc_node *lyd_node_schema(const struct lyd_node *node);
/**
+ * @brief Search in the given siblings (NOT recursively) for the first schema node data instance.
+ * Uses hashes - should be used whenever possible for best performance.
+ *
+ * @param[in] siblings Siblings to search in including preceding and succeeding nodes.
+ * @param[in] schema Target data node schema to find.
+ * @param[out] match Can be NULL, otherwise the found data node.
+ * @return LY_SUCCESS on success, @p match set.
+ * @return LY_ENOTFOUND if not found, @p match set to NULL.
+ * @return LY_ERR value if another error occurred.
+ */
+LY_ERR lyd_find_sibling_schema(const struct lyd_node *siblings, const struct lysc_node *schema, struct lyd_node **match);
+
+/**
* @brief Check whether a node to be deleted is the root node, move it if it is.
*
* @param[in] root Root sibling.
@@ -287,6 +300,20 @@
LY_ERR lyd_create_list(const struct lysc_node *schema, const struct ly_path_predicate *predicates, struct lyd_node **node);
/**
+ * @brief Create a list with all its keys (cannot be used for key-less list).
+ *
+ * Hash is calculated and new node flag is set.
+ *
+ * @param[in] schema Schema node of the new data node.
+ * @param[in] keys Key list predicates.
+ * @param[in] keys_len Length of @p keys.
+ * @param[out] node Created node.
+ * @return LY_SUCCESS on success.
+ * @return LY_ERR value if an error occurred.
+ */
+LY_ERR lyd_create_list2(const struct lysc_node *schema, const char *keys, size_t keys_len, struct lyd_node **node);
+
+/**
* @brief Create an anyxml/anydata node.
*
* Hash is calculated and flags are properly set based on @p is_valid.
diff --git a/src/tree_data_new.c b/src/tree_data_new.c
new file mode 100644
index 0000000..74f353d
--- /dev/null
+++ b/src/tree_data_new.c
@@ -0,0 +1,1808 @@
+/**
+ * @file tree_data_new.c
+ * @author Radek Krejci <rkrejci@cesnet.cz>
+ * @author Michal Vasko <mvasko@cesnet.cz>
+ * @brief Data tree new functions
+ *
+ * Copyright (c) 2015 - 2022 CESNET, z.s.p.o.
+ *
+ * This source code is licensed under BSD 3-Clause License (the "License").
+ * You may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * https://opensource.org/licenses/BSD-3-Clause
+ */
+
+#define _GNU_SOURCE
+
+#include <assert.h>
+#include <ctype.h>
+#include <inttypes.h>
+#include <stdarg.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "common.h"
+#include "compat.h"
+#include "context.h"
+#include "dict.h"
+#include "diff.h"
+#include "hash_table.h"
+#include "in.h"
+#include "in_internal.h"
+#include "log.h"
+#include "parser_data.h"
+#include "parser_internal.h"
+#include "path.h"
+#include "plugins.h"
+#include "plugins_exts/metadata.h"
+#include "plugins_internal.h"
+#include "plugins_types.h"
+#include "set.h"
+#include "tree.h"
+#include "tree_data_internal.h"
+#include "tree_edit.h"
+#include "tree_schema.h"
+#include "tree_schema_internal.h"
+#include "validation.h"
+#include "xml.h"
+#include "xpath.h"
+
+LY_ERR
+lyd_create_term(const struct lysc_node *schema, const char *value, size_t value_len, ly_bool *dynamic,
+ LY_VALUE_FORMAT format, void *prefix_data, uint32_t hints, ly_bool *incomplete, struct lyd_node **node)
+{
+ LY_ERR ret;
+ struct lyd_node_term *term;
+
+ assert(schema->nodetype & LYD_NODE_TERM);
+
+ term = calloc(1, sizeof *term);
+ LY_CHECK_ERR_RET(!term, LOGMEM(schema->module->ctx), LY_EMEM);
+
+ term->schema = schema;
+ term->prev = &term->node;
+ term->flags = LYD_NEW;
+
+ LOG_LOCSET(schema, NULL, NULL, NULL);
+ ret = lyd_value_store(schema->module->ctx, &term->value, ((struct lysc_node_leaf *)term->schema)->type, value,
+ value_len, dynamic, format, prefix_data, hints, schema, incomplete);
+ LOG_LOCBACK(1, 0, 0, 0);
+ LY_CHECK_ERR_RET(ret, free(term), ret);
+ lyd_hash(&term->node);
+
+ *node = &term->node;
+ return ret;
+}
+
+LY_ERR
+lyd_create_term2(const struct lysc_node *schema, const struct lyd_value *val, struct lyd_node **node)
+{
+ LY_ERR ret;
+ struct lyd_node_term *term;
+ struct lysc_type *type;
+
+ assert(schema->nodetype & LYD_NODE_TERM);
+ assert(val && val->realtype);
+
+ term = calloc(1, sizeof *term);
+ LY_CHECK_ERR_RET(!term, LOGMEM(schema->module->ctx), LY_EMEM);
+
+ term->schema = schema;
+ term->prev = &term->node;
+ term->flags = LYD_NEW;
+
+ type = ((struct lysc_node_leaf *)schema)->type;
+ ret = type->plugin->duplicate(schema->module->ctx, val, &term->value);
+ if (ret) {
+ LOGERR(schema->module->ctx, ret, "Value duplication failed.");
+ free(term);
+ return ret;
+ }
+ lyd_hash(&term->node);
+
+ *node = &term->node;
+ return ret;
+}
+
+LY_ERR
+lyd_create_inner(const struct lysc_node *schema, struct lyd_node **node)
+{
+ struct lyd_node_inner *in;
+
+ assert(schema->nodetype & LYD_NODE_INNER);
+
+ in = calloc(1, sizeof *in);
+ LY_CHECK_ERR_RET(!in, LOGMEM(schema->module->ctx), LY_EMEM);
+
+ in->schema = schema;
+ in->prev = &in->node;
+ in->flags = LYD_NEW;
+ if ((schema->nodetype == LYS_CONTAINER) && !(schema->flags & LYS_PRESENCE)) {
+ in->flags |= LYD_DEFAULT;
+ }
+
+ /* do not hash list with keys, we need them for the hash */
+ if ((schema->nodetype != LYS_LIST) || (schema->flags & LYS_KEYLESS)) {
+ lyd_hash(&in->node);
+ }
+
+ *node = &in->node;
+ return LY_SUCCESS;
+}
+
+LY_ERR
+lyd_create_list(const struct lysc_node *schema, const struct ly_path_predicate *predicates, struct lyd_node **node)
+{
+ LY_ERR ret = LY_SUCCESS;
+ struct lyd_node *list = NULL, *key;
+ LY_ARRAY_COUNT_TYPE u;
+
+ assert((schema->nodetype == LYS_LIST) && !(schema->flags & LYS_KEYLESS));
+
+ /* create list */
+ LY_CHECK_GOTO(ret = lyd_create_inner(schema, &list), cleanup);
+
+ LOG_LOCSET(NULL, list, NULL, NULL);
+
+ /* create and insert all the keys */
+ LY_ARRAY_FOR(predicates, u) {
+ LY_CHECK_GOTO(ret = lyd_create_term2(predicates[u].key, &predicates[u].value, &key), cleanup);
+ lyd_insert_node(list, NULL, key, 0);
+ }
+
+ /* hash having all the keys */
+ lyd_hash(list);
+
+ /* success */
+ *node = list;
+ list = NULL;
+
+cleanup:
+ LOG_LOCBACK(0, 1, 0, 0);
+ lyd_free_tree(list);
+ return ret;
+}
+
+LY_ERR
+lyd_create_list2(const struct lysc_node *schema, const char *keys, size_t keys_len, struct lyd_node **node)
+{
+ LY_ERR ret = LY_SUCCESS;
+ struct lyxp_expr *expr = NULL;
+ uint16_t exp_idx = 0;
+ enum ly_path_pred_type pred_type = 0;
+ struct ly_path_predicate *predicates = NULL;
+
+ LOG_LOCSET(schema, NULL, NULL, NULL);
+
+ /* parse keys */
+ LY_CHECK_GOTO(ret = ly_path_parse_predicate(schema->module->ctx, NULL, keys, keys_len, LY_PATH_PREFIX_OPTIONAL,
+ LY_PATH_PRED_KEYS, &expr), cleanup);
+
+ /* compile them */
+ LY_CHECK_GOTO(ret = ly_path_compile_predicate(schema->module->ctx, NULL, NULL, schema, expr, &exp_idx, LY_VALUE_JSON,
+ NULL, &predicates, &pred_type), cleanup);
+
+ /* create the list node */
+ LY_CHECK_GOTO(ret = lyd_create_list(schema, predicates, node), cleanup);
+
+cleanup:
+ LOG_LOCBACK(1, 0, 0, 0);
+ lyxp_expr_free(schema->module->ctx, expr);
+ ly_path_predicates_free(schema->module->ctx, pred_type, predicates);
+ return ret;
+}
+
+LY_ERR
+lyd_create_any(const struct lysc_node *schema, const void *value, LYD_ANYDATA_VALUETYPE value_type, ly_bool use_value,
+ struct lyd_node **node)
+{
+ LY_ERR ret;
+ struct lyd_node_any *any;
+ union lyd_any_value any_val;
+
+ assert(schema->nodetype & LYD_NODE_ANY);
+
+ any = calloc(1, sizeof *any);
+ LY_CHECK_ERR_RET(!any, LOGMEM(schema->module->ctx), LY_EMEM);
+
+ any->schema = schema;
+ any->prev = &any->node;
+ any->flags = LYD_NEW;
+
+ if (use_value) {
+ switch (value_type) {
+ case LYD_ANYDATA_DATATREE:
+ any->value.tree = (void *)value;
+ break;
+ case LYD_ANYDATA_STRING:
+ case LYD_ANYDATA_XML:
+ case LYD_ANYDATA_JSON:
+ LY_CHECK_ERR_RET(lydict_insert_zc(schema->module->ctx, (void *)value, &any->value.str), free(any), LY_EMEM);
+ break;
+ case LYD_ANYDATA_LYB:
+ any->value.mem = (void *)value;
+ break;
+ }
+ any->value_type = value_type;
+ } else {
+ any_val.str = value;
+ ret = lyd_any_copy_value(&any->node, &any_val, value_type);
+ LY_CHECK_ERR_RET(ret, free(any), ret);
+ }
+ lyd_hash(&any->node);
+
+ *node = &any->node;
+ return LY_SUCCESS;
+}
+
+LY_ERR
+lyd_create_opaq(const struct ly_ctx *ctx, const char *name, size_t name_len, const char *prefix, size_t pref_len,
+ const char *module_key, size_t module_key_len, const char *value, size_t value_len, ly_bool *dynamic,
+ LY_VALUE_FORMAT format, void *val_prefix_data, uint32_t hints, struct lyd_node **node)
+{
+ LY_ERR ret = LY_SUCCESS;
+ struct lyd_node_opaq *opaq;
+
+ assert(ctx && name && name_len && format);
+
+ if (!value_len && (!dynamic || !*dynamic)) {
+ value = "";
+ }
+
+ opaq = calloc(1, sizeof *opaq);
+ LY_CHECK_ERR_GOTO(!opaq, LOGMEM(ctx); ret = LY_EMEM, finish);
+
+ opaq->prev = &opaq->node;
+ LY_CHECK_GOTO(ret = lydict_insert(ctx, name, name_len, &opaq->name.name), finish);
+
+ if (pref_len) {
+ LY_CHECK_GOTO(ret = lydict_insert(ctx, prefix, pref_len, &opaq->name.prefix), finish);
+ }
+ if (module_key_len) {
+ LY_CHECK_GOTO(ret = lydict_insert(ctx, module_key, module_key_len, &opaq->name.module_ns), finish);
+ }
+ if (dynamic && *dynamic) {
+ LY_CHECK_GOTO(ret = lydict_insert_zc(ctx, (char *)value, &opaq->value), finish);
+ *dynamic = 0;
+ } else {
+ LY_CHECK_GOTO(ret = lydict_insert(ctx, value, value_len, &opaq->value), finish);
+ }
+
+ opaq->format = format;
+ opaq->val_prefix_data = val_prefix_data;
+ opaq->hints = hints;
+ opaq->ctx = ctx;
+
+finish:
+ if (ret) {
+ lyd_free_tree(&opaq->node);
+ ly_free_prefix_data(format, val_prefix_data);
+ } else {
+ *node = &opaq->node;
+ }
+ return ret;
+}
+
+LIBYANG_API_DEF LY_ERR
+lyd_new_inner(struct lyd_node *parent, const struct lys_module *module, const char *name, ly_bool output,
+ struct lyd_node **node)
+{
+ LY_ERR r;
+ struct lyd_node *ret = NULL;
+ const struct lysc_node *schema;
+ struct lysc_ext_instance *ext = NULL;
+ const struct ly_ctx *ctx = parent ? LYD_CTX(parent) : (module ? module->ctx : NULL);
+
+ LY_CHECK_ARG_RET(ctx, parent || module, parent || node, name, LY_EINVAL);
+ LY_CHECK_CTX_EQUAL_RET(parent ? LYD_CTX(parent) : NULL, module ? module->ctx : NULL, LY_EINVAL);
+
+ if (!module) {
+ module = parent->schema->module;
+ }
+
+ schema = lys_find_child(parent ? parent->schema : NULL, module, name, 0,
+ LYS_CONTAINER | LYS_NOTIF | LYS_RPC | LYS_ACTION, output ? LYS_GETNEXT_OUTPUT : 0);
+ if (!schema && parent) {
+ r = ly_nested_ext_schema(parent, NULL, module->name, strlen(module->name), LY_VALUE_JSON, NULL, name,
+ strlen(name), &schema, &ext);
+ LY_CHECK_RET(r && (r != LY_ENOT), r);
+ }
+ LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "Inner node (container, notif, RPC, or action) \"%s\" not found.",
+ name), LY_ENOTFOUND);
+
+ LY_CHECK_RET(lyd_create_inner(schema, &ret));
+ if (ext) {
+ ret->flags |= LYD_EXT;
+ }
+ if (parent) {
+ lyd_insert_node(parent, NULL, ret, 0);
+ }
+
+ if (node) {
+ *node = ret;
+ }
+ return LY_SUCCESS;
+}
+
+LIBYANG_API_DEF LY_ERR
+lyd_new_ext_inner(const struct lysc_ext_instance *ext, const char *name, struct lyd_node **node)
+{
+ struct lyd_node *ret = NULL;
+ const struct lysc_node *schema;
+ struct ly_ctx *ctx = ext ? ext->module->ctx : NULL;
+
+ LY_CHECK_ARG_RET(ctx, ext, node, name, LY_EINVAL);
+
+ schema = lysc_ext_find_node(ext, NULL, name, 0, LYS_CONTAINER | LYS_NOTIF | LYS_RPC | LYS_ACTION, 0);
+ if (!schema) {
+ if (ext->argument) {
+ LOGERR(ctx, LY_EINVAL, "Inner node (not a list) \"%s\" not found in instance \"%s\" of extension %s.",
+ name, ext->argument, ext->def->name);
+ } else {
+ LOGERR(ctx, LY_EINVAL, "Inner node (not a list) \"%s\" not found in instance of extension %s.",
+ name, ext->def->name);
+ }
+ return LY_ENOTFOUND;
+ }
+ LY_CHECK_RET(lyd_create_inner(schema, &ret));
+
+ *node = ret;
+
+ return LY_SUCCESS;
+}
+
+/**
+ * @brief Create a new list node in the data tree.
+ *
+ * @param[in] parent Parent node for the node being created. NULL in case of creating a top level element.
+ * @param[in] module Module of the node being created. If NULL, @p parent module will be used.
+ * @param[in] name Schema node name of the new data node. The node must be #LYS_LIST.
+ * @param[in] format Format of key values.
+ * @param[in] output Flag in case the @p parent is RPC/Action. If value is 0, the input's data nodes of the RPC/Action are
+ * taken into consideration. Otherwise, the output's data node is going to be created.
+ * @param[out] node Optional created node.
+ * @param[in] ap Ordered key values of the new list instance, all must be set. For ::LY_VALUE_LYB, every value must
+ * be followed by the value length.
+ * @return LY_ERR value.
+ */
+static LY_ERR
+_lyd_new_list(struct lyd_node *parent, const struct lys_module *module, const char *name, LY_VALUE_FORMAT format,
+ ly_bool output, struct lyd_node **node, va_list ap)
+{
+ struct lyd_node *ret = NULL, *key;
+ const struct lysc_node *schema, *key_s;
+ struct lysc_ext_instance *ext = NULL;
+ const struct ly_ctx *ctx = parent ? LYD_CTX(parent) : (module ? module->ctx : NULL);
+ const void *key_val;
+ uint32_t key_len;
+ LY_ERR r, rc = LY_SUCCESS;
+
+ LY_CHECK_ARG_RET(ctx, parent || module, parent || node, name, LY_EINVAL);
+ LY_CHECK_CTX_EQUAL_RET(parent ? LYD_CTX(parent) : NULL, module ? module->ctx : NULL, LY_EINVAL);
+
+ if (!module) {
+ module = parent->schema->module;
+ }
+
+ schema = lys_find_child(parent ? parent->schema : NULL, module, name, 0, LYS_LIST, output ? LYS_GETNEXT_OUTPUT : 0);
+ if (!schema && parent) {
+ r = ly_nested_ext_schema(parent, NULL, module->name, strlen(module->name), LY_VALUE_JSON, NULL, name,
+ strlen(name), &schema, &ext);
+ LY_CHECK_RET(r && (r != LY_ENOT), r);
+ }
+ LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "List node \"%s\" not found.", name), LY_ENOTFOUND);
+
+ /* create list inner node */
+ LY_CHECK_RET(lyd_create_inner(schema, &ret));
+
+ /* create and insert all the keys */
+ for (key_s = lysc_node_child(schema); key_s && (key_s->flags & LYS_KEY); key_s = key_s->next) {
+ if (format == LY_VALUE_LYB) {
+ key_val = va_arg(ap, const void *);
+ key_len = va_arg(ap, uint32_t);
+ } else {
+ key_val = va_arg(ap, const char *);
+ key_len = key_val ? strlen((char *)key_val) : 0;
+ }
+
+ rc = lyd_create_term(key_s, key_val, key_len, NULL, format, NULL, LYD_HINT_DATA, NULL, &key);
+ LY_CHECK_GOTO(rc, cleanup);
+ lyd_insert_node(ret, NULL, key, 1);
+ }
+
+ if (ext) {
+ ret->flags |= LYD_EXT;
+ }
+ if (parent) {
+ lyd_insert_node(parent, NULL, ret, 0);
+ }
+
+cleanup:
+ if (rc) {
+ lyd_free_tree(ret);
+ ret = NULL;
+ } else if (node) {
+ *node = ret;
+ }
+ return rc;
+}
+
+LIBYANG_API_DEF LY_ERR
+lyd_new_list(struct lyd_node *parent, const struct lys_module *module, const char *name, ly_bool output,
+ struct lyd_node **node, ...)
+{
+ LY_ERR rc;
+ va_list ap;
+
+ va_start(ap, node);
+
+ rc = _lyd_new_list(parent, module, name, LY_VALUE_JSON, output, node, ap);
+
+ va_end(ap);
+ return rc;
+}
+
+LIBYANG_API_DEF LY_ERR
+lyd_new_list_bin(struct lyd_node *parent, const struct lys_module *module, const char *name, ly_bool output,
+ struct lyd_node **node, ...)
+{
+ LY_ERR rc;
+ va_list ap;
+
+ va_start(ap, node);
+
+ rc = _lyd_new_list(parent, module, name, LY_VALUE_LYB, output, node, ap);
+
+ va_end(ap);
+ return rc;
+}
+
+LIBYANG_API_DEF LY_ERR
+lyd_new_list_canon(struct lyd_node *parent, const struct lys_module *module, const char *name, ly_bool output,
+ struct lyd_node **node, ...)
+{
+ LY_ERR rc;
+ va_list ap;
+
+ va_start(ap, node);
+
+ rc = _lyd_new_list(parent, module, name, LY_VALUE_CANON, output, node, ap);
+
+ va_end(ap);
+ return rc;
+}
+
+LIBYANG_API_DEF LY_ERR
+lyd_new_ext_list(const struct lysc_ext_instance *ext, const char *name, struct lyd_node **node, ...)
+{
+ struct lyd_node *ret = NULL, *key;
+ const struct lysc_node *schema, *key_s;
+ struct ly_ctx *ctx = ext ? ext->module->ctx : NULL;
+ va_list ap;
+ const char *key_val;
+ LY_ERR rc = LY_SUCCESS;
+
+ LY_CHECK_ARG_RET(ctx, ext, node, name, LY_EINVAL);
+
+ schema = lysc_ext_find_node(ext, NULL, name, 0, LYS_LIST, 0);
+ if (!schema) {
+ if (ext->argument) {
+ LOGERR(ctx, LY_EINVAL, "List node \"%s\" not found in instance \"%s\" of extension %s.",
+ name, ext->argument, ext->def->name);
+ } else {
+ LOGERR(ctx, LY_EINVAL, "List node \"%s\" not found in instance of extension %s.", name, ext->def->name);
+ }
+ return LY_ENOTFOUND;
+ }
+ /* create list inner node */
+ LY_CHECK_RET(lyd_create_inner(schema, &ret));
+
+ va_start(ap, node);
+
+ /* create and insert all the keys */
+ for (key_s = lysc_node_child(schema); key_s && (key_s->flags & LYS_KEY); key_s = key_s->next) {
+ key_val = va_arg(ap, const char *);
+
+ rc = lyd_create_term(key_s, key_val, key_val ? strlen(key_val) : 0, NULL, LY_VALUE_JSON, NULL, LYD_HINT_DATA,
+ NULL, &key);
+ LY_CHECK_GOTO(rc, cleanup);
+ lyd_insert_node(ret, NULL, key, 1);
+ }
+
+cleanup:
+ va_end(ap);
+ if (rc) {
+ lyd_free_tree(ret);
+ ret = NULL;
+ }
+ *node = ret;
+ return rc;
+}
+
+LIBYANG_API_DEF LY_ERR
+lyd_new_list2(struct lyd_node *parent, const struct lys_module *module, const char *name, const char *keys,
+ ly_bool output, struct lyd_node **node)
+{
+ LY_ERR r;
+ struct lyd_node *ret = NULL;
+ const struct lysc_node *schema;
+ struct lysc_ext_instance *ext = NULL;
+ const struct ly_ctx *ctx = parent ? LYD_CTX(parent) : (module ? module->ctx : NULL);
+
+ LY_CHECK_ARG_RET(ctx, parent || module, parent || node, name, LY_EINVAL);
+ LY_CHECK_CTX_EQUAL_RET(parent ? LYD_CTX(parent) : NULL, module ? module->ctx : NULL, LY_EINVAL);
+
+ if (!module) {
+ module = parent->schema->module;
+ }
+ if (!keys) {
+ keys = "";
+ }
+
+ /* find schema node */
+ schema = lys_find_child(parent ? parent->schema : NULL, module, name, 0, LYS_LIST, output ? LYS_GETNEXT_OUTPUT : 0);
+ if (!schema && parent) {
+ r = ly_nested_ext_schema(parent, NULL, module->name, strlen(module->name), LY_VALUE_JSON, NULL, name, strlen(name),
+ &schema, &ext);
+ LY_CHECK_RET(r && (r != LY_ENOT), r);
+ }
+ LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "List node \"%s\" not found.", name), LY_ENOTFOUND);
+
+ if ((schema->flags & LYS_KEYLESS) && !keys[0]) {
+ /* key-less list */
+ LY_CHECK_RET(lyd_create_inner(schema, &ret));
+ } else {
+ /* create the list node */
+ LY_CHECK_RET(lyd_create_list2(schema, keys, strlen(keys), &ret));
+ }
+ if (ext) {
+ ret->flags |= LYD_EXT;
+ }
+ if (parent) {
+ lyd_insert_node(parent, NULL, ret, 0);
+ }
+
+ if (node) {
+ *node = ret;
+ }
+ return LY_SUCCESS;
+}
+
+/**
+ * @brief Create a new term node in the data tree.
+ *
+ * @param[in] parent Parent node for the node being created. NULL in case of creating a top level element.
+ * @param[in] module Module of the node being created. If NULL, @p parent module will be used.
+ * @param[in] name Schema node name of the new data node. The node can be ::LYS_LEAF or ::LYS_LEAFLIST.
+ * @param[in] value Value of the node being created.
+ * @param[in] value_len Length of @p value.
+ * @param[in] format Format of @p value.
+ * @param[in] output Flag in case the @p parent is RPC/Action. If value is 0, the input's data nodes of the RPC/Action are
+ * taken into consideration. Otherwise, the output's data node is going to be created.
+ * @param[out] node Optional created node.
+ * @return LY_ERR value.
+ */
+static LY_ERR
+_lyd_new_term(struct lyd_node *parent, const struct lys_module *module, const char *name, const void *value,
+ size_t value_len, LY_VALUE_FORMAT format, ly_bool output, struct lyd_node **node)
+{
+ LY_ERR r;
+ struct lyd_node *ret = NULL;
+ const struct lysc_node *schema;
+ struct lysc_ext_instance *ext = NULL;
+ const struct ly_ctx *ctx = parent ? LYD_CTX(parent) : (module ? module->ctx : NULL);
+
+ LY_CHECK_ARG_RET(ctx, parent || module, parent || node, name, LY_EINVAL);
+ LY_CHECK_CTX_EQUAL_RET(parent ? LYD_CTX(parent) : NULL, module ? module->ctx : NULL, LY_EINVAL);
+
+ if (!module) {
+ module = parent->schema->module;
+ }
+
+ schema = lys_find_child(parent ? parent->schema : NULL, module, name, 0, LYD_NODE_TERM, output ? LYS_GETNEXT_OUTPUT : 0);
+ if (!schema && parent) {
+ r = ly_nested_ext_schema(parent, NULL, module->name, strlen(module->name), LY_VALUE_JSON, NULL, name,
+ strlen(name), &schema, &ext);
+ LY_CHECK_RET(r && (r != LY_ENOT), r);
+ }
+ LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "Term node \"%s\" not found.", name), LY_ENOTFOUND);
+
+ LY_CHECK_RET(lyd_create_term(schema, value, value_len, NULL, format, NULL, LYD_HINT_DATA, NULL, &ret));
+ if (ext) {
+ ret->flags |= LYD_EXT;
+ }
+ if (parent) {
+ lyd_insert_node(parent, NULL, ret, 0);
+ }
+
+ if (node) {
+ *node = ret;
+ }
+ return LY_SUCCESS;
+}
+
+LIBYANG_API_DEF LY_ERR
+lyd_new_term(struct lyd_node *parent, const struct lys_module *module, const char *name, const char *val_str,
+ ly_bool output, struct lyd_node **node)
+{
+ return _lyd_new_term(parent, module, name, val_str, val_str ? strlen(val_str) : 0, LY_VALUE_JSON, output, node);
+}
+
+LIBYANG_API_DEF LY_ERR
+lyd_new_term_bin(struct lyd_node *parent, const struct lys_module *module, const char *name, const void *value,
+ size_t value_len, ly_bool output, struct lyd_node **node)
+{
+ return _lyd_new_term(parent, module, name, value, value_len, LY_VALUE_LYB, output, node);
+}
+
+LIBYANG_API_DEF LY_ERR
+lyd_new_term_canon(struct lyd_node *parent, const struct lys_module *module, const char *name, const char *val_str,
+ ly_bool output, struct lyd_node **node)
+{
+ return _lyd_new_term(parent, module, name, val_str, val_str ? strlen(val_str) : 0, LY_VALUE_CANON, output, node);
+}
+
+LIBYANG_API_DEF LY_ERR
+lyd_new_ext_term(const struct lysc_ext_instance *ext, const char *name, const char *val_str, struct lyd_node **node)
+{
+ LY_ERR rc;
+ struct lyd_node *ret = NULL;
+ const struct lysc_node *schema;
+ struct ly_ctx *ctx = ext ? ext->module->ctx : NULL;
+
+ LY_CHECK_ARG_RET(ctx, ext, node, name, LY_EINVAL);
+
+ schema = lysc_ext_find_node(ext, NULL, name, 0, LYD_NODE_TERM, 0);
+ if (!schema) {
+ if (ext->argument) {
+ LOGERR(ctx, LY_EINVAL, "Term node \"%s\" not found in instance \"%s\" of extension %s.",
+ name, ext->argument, ext->def->name);
+ } else {
+ LOGERR(ctx, LY_EINVAL, "Term node \"%s\" not found in instance of extension %s.", name, ext->def->name);
+ }
+ return LY_ENOTFOUND;
+ }
+ rc = lyd_create_term(schema, val_str, val_str ? strlen(val_str) : 0, NULL, LY_VALUE_JSON, NULL, LYD_HINT_DATA, NULL, &ret);
+ LY_CHECK_RET(rc);
+
+ *node = ret;
+
+ return LY_SUCCESS;
+}
+
+LIBYANG_API_DEF LY_ERR
+lyd_new_any(struct lyd_node *parent, const struct lys_module *module, const char *name, const void *value,
+ ly_bool use_value, LYD_ANYDATA_VALUETYPE value_type, ly_bool output, struct lyd_node **node)
+{
+ LY_ERR r;
+ struct lyd_node *ret = NULL;
+ const struct lysc_node *schema;
+ struct lysc_ext_instance *ext = NULL;
+ const struct ly_ctx *ctx = parent ? LYD_CTX(parent) : (module ? module->ctx : NULL);
+
+ LY_CHECK_ARG_RET(ctx, parent || module, parent || node, name, LY_EINVAL);
+ LY_CHECK_CTX_EQUAL_RET(parent ? LYD_CTX(parent) : NULL, module ? module->ctx : NULL, LY_EINVAL);
+
+ if (!module) {
+ module = parent->schema->module;
+ }
+
+ schema = lys_find_child(parent ? parent->schema : NULL, module, name, 0, LYD_NODE_ANY, output ? LYS_GETNEXT_OUTPUT : 0);
+ if (!schema && parent) {
+ r = ly_nested_ext_schema(parent, NULL, module->name, strlen(module->name), LY_VALUE_JSON, NULL, name,
+ strlen(name), &schema, &ext);
+ LY_CHECK_RET(r && (r != LY_ENOT), r);
+ }
+ LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "Any node \"%s\" not found.", name), LY_ENOTFOUND);
+
+ LY_CHECK_RET(lyd_create_any(schema, value, value_type, use_value, &ret));
+ if (ext) {
+ ret->flags |= LYD_EXT;
+ }
+ if (parent) {
+ lyd_insert_node(parent, NULL, ret, 0);
+ }
+
+ if (node) {
+ *node = ret;
+ }
+ return LY_SUCCESS;
+}
+
+LIBYANG_API_DEF LY_ERR
+lyd_new_ext_any(const struct lysc_ext_instance *ext, const char *name, const void *value, ly_bool use_value,
+ LYD_ANYDATA_VALUETYPE value_type, struct lyd_node **node)
+{
+ struct lyd_node *ret = NULL;
+ const struct lysc_node *schema;
+ struct ly_ctx *ctx = ext ? ext->module->ctx : NULL;
+
+ LY_CHECK_ARG_RET(ctx, ext, node, name, LY_EINVAL);
+
+ schema = lysc_ext_find_node(ext, NULL, name, 0, LYD_NODE_ANY, 0);
+ if (!schema) {
+ if (ext->argument) {
+ LOGERR(ctx, LY_EINVAL, "Any node \"%s\" not found in instance \"%s\" of extension %s.",
+ name, ext->argument, ext->def->name);
+ } else {
+ LOGERR(ctx, LY_EINVAL, "Any node \"%s\" not found in instance of extension %s.", name, ext->def->name);
+ }
+ return LY_ENOTFOUND;
+ }
+ LY_CHECK_RET(lyd_create_any(schema, value, value_type, use_value, &ret));
+
+ *node = ret;
+
+ return LY_SUCCESS;
+}
+
+LIBYANG_API_DEF LY_ERR
+lyd_new_meta(const struct ly_ctx *ctx, struct lyd_node *parent, const struct lys_module *module, const char *name,
+ const char *val_str, ly_bool clear_dflt, struct lyd_meta **meta)
+{
+ const char *prefix, *tmp;
+ size_t pref_len, name_len;
+
+ LY_CHECK_ARG_RET(ctx, ctx || parent, name, module || strchr(name, ':'), parent || meta, LY_EINVAL);
+ LY_CHECK_CTX_EQUAL_RET(ctx, parent ? LYD_CTX(parent) : NULL, module ? module->ctx : NULL, LY_EINVAL);
+ if (!ctx) {
+ ctx = LYD_CTX(parent);
+ }
+
+ if (parent && !parent->schema) {
+ LOGERR(ctx, LY_EINVAL, "Cannot add metadata \"%s\" to an opaque node \"%s\".", name, LYD_NAME(parent));
+ return LY_EINVAL;
+ }
+ if (meta) {
+ *meta = NULL;
+ }
+
+ /* parse the name */
+ tmp = name;
+ if (ly_parse_nodeid(&tmp, &prefix, &pref_len, &name, &name_len) || tmp[0]) {
+ LOGERR(ctx, LY_EINVAL, "Metadata name \"%s\" is not valid.", name);
+ return LY_EINVAL;
+ }
+
+ /* find the module */
+ if (prefix) {
+ module = ly_ctx_get_module_implemented2(ctx, prefix, pref_len);
+ LY_CHECK_ERR_RET(!module, LOGERR(ctx, LY_EINVAL, "Module \"%.*s\" not found.", (int)pref_len, prefix), LY_ENOTFOUND);
+ }
+
+ /* set value if none */
+ if (!val_str) {
+ val_str = "";
+ }
+
+ return lyd_create_meta(parent, meta, module, name, name_len, val_str, strlen(val_str), NULL, LY_VALUE_JSON,
+ NULL, LYD_HINT_DATA, parent ? parent->schema : NULL, clear_dflt, NULL);
+}
+
+LIBYANG_API_DEF LY_ERR
+lyd_new_meta2(const struct ly_ctx *ctx, struct lyd_node *parent, ly_bool clear_dflt, const struct lyd_attr *attr,
+ struct lyd_meta **meta)
+{
+ const struct lys_module *mod;
+
+ LY_CHECK_ARG_RET(NULL, ctx, attr, parent || meta, LY_EINVAL);
+ LY_CHECK_CTX_EQUAL_RET(ctx, parent ? LYD_CTX(parent) : NULL, LY_EINVAL);
+
+ if (parent && !parent->schema) {
+ LOGERR(ctx, LY_EINVAL, "Cannot add metadata to an opaque node \"%s\".", ((struct lyd_node_opaq *)parent)->name);
+ return LY_EINVAL;
+ }
+ if (meta) {
+ *meta = NULL;
+ }
+
+ switch (attr->format) {
+ case LY_VALUE_XML:
+ mod = ly_ctx_get_module_implemented_ns(ctx, attr->name.module_ns);
+ if (!mod) {
+ LOGERR(ctx, LY_EINVAL, "Module with namespace \"%s\" not found.", attr->name.module_ns);
+ return LY_ENOTFOUND;
+ }
+ break;
+ case LY_VALUE_JSON:
+ mod = ly_ctx_get_module_implemented(ctx, attr->name.module_name);
+ if (!mod) {
+ LOGERR(ctx, LY_EINVAL, "Module \"%s\" not found.", attr->name.module_name);
+ return LY_ENOTFOUND;
+ }
+ break;
+ default:
+ LOGINT_RET(ctx);
+ }
+
+ return lyd_create_meta(parent, meta, mod, attr->name.name, strlen(attr->name.name), attr->value, strlen(attr->value),
+ NULL, attr->format, attr->val_prefix_data, attr->hints, parent ? parent->schema : NULL, clear_dflt, NULL);
+}
+
+LIBYANG_API_DEF LY_ERR
+lyd_new_opaq(struct lyd_node *parent, const struct ly_ctx *ctx, const char *name, const char *value,
+ const char *prefix, const char *module_name, struct lyd_node **node)
+{
+ struct lyd_node *ret = NULL;
+
+ LY_CHECK_ARG_RET(ctx, parent || ctx, parent || node, name, module_name, !prefix || !strcmp(prefix, module_name), LY_EINVAL);
+ LY_CHECK_CTX_EQUAL_RET(ctx, parent ? LYD_CTX(parent) : NULL, LY_EINVAL);
+
+ if (!ctx) {
+ ctx = LYD_CTX(parent);
+ }
+ if (!value) {
+ value = "";
+ }
+
+ LY_CHECK_RET(lyd_create_opaq(ctx, name, strlen(name), prefix, prefix ? strlen(prefix) : 0, module_name,
+ strlen(module_name), value, strlen(value), NULL, LY_VALUE_JSON, NULL, 0, &ret));
+ if (parent) {
+ lyd_insert_node(parent, NULL, ret, 1);
+ }
+
+ if (node) {
+ *node = ret;
+ }
+ return LY_SUCCESS;
+}
+
+LIBYANG_API_DEF LY_ERR
+lyd_new_opaq2(struct lyd_node *parent, const struct ly_ctx *ctx, const char *name, const char *value,
+ const char *prefix, const char *module_ns, struct lyd_node **node)
+{
+ struct lyd_node *ret = NULL;
+
+ LY_CHECK_ARG_RET(ctx, parent || ctx, parent || node, name, module_ns, LY_EINVAL);
+ LY_CHECK_CTX_EQUAL_RET(ctx, parent ? LYD_CTX(parent) : NULL, LY_EINVAL);
+
+ if (!ctx) {
+ ctx = LYD_CTX(parent);
+ }
+ if (!value) {
+ value = "";
+ }
+
+ LY_CHECK_RET(lyd_create_opaq(ctx, name, strlen(name), prefix, prefix ? strlen(prefix) : 0, module_ns,
+ strlen(module_ns), value, strlen(value), NULL, LY_VALUE_XML, NULL, 0, &ret));
+ if (parent) {
+ lyd_insert_node(parent, NULL, ret, 1);
+ }
+
+ if (node) {
+ *node = ret;
+ }
+ return LY_SUCCESS;
+}
+
+LIBYANG_API_DEF LY_ERR
+lyd_new_attr(struct lyd_node *parent, const char *module_name, const char *name, const char *value,
+ struct lyd_attr **attr)
+{
+ struct lyd_attr *ret = NULL;
+ const struct ly_ctx *ctx;
+ const char *prefix, *tmp;
+ size_t pref_len, name_len, mod_len;
+
+ LY_CHECK_ARG_RET(NULL, parent, !parent->schema, name, LY_EINVAL);
+
+ ctx = LYD_CTX(parent);
+
+ /* parse the name */
+ tmp = name;
+ if (ly_parse_nodeid(&tmp, &prefix, &pref_len, &name, &name_len) || tmp[0]) {
+ LOGERR(ctx, LY_EINVAL, "Attribute name \"%s\" is not valid.", name);
+ return LY_EVALID;
+ }
+
+ if ((pref_len == 3) && !strncmp(prefix, "xml", 3)) {
+ /* not a prefix but special name */
+ name = prefix;
+ name_len += 1 + pref_len;
+ prefix = NULL;
+ pref_len = 0;
+ }
+
+ /* get the module */
+ if (module_name) {
+ mod_len = strlen(module_name);
+ } else {
+ module_name = prefix;
+ mod_len = pref_len;
+ }
+
+ /* set value if none */
+ if (!value) {
+ value = "";
+ }
+
+ LY_CHECK_RET(lyd_create_attr(parent, &ret, ctx, name, name_len, prefix, pref_len, module_name, mod_len, value,
+ strlen(value), NULL, LY_VALUE_JSON, NULL, LYD_HINT_DATA));
+
+ if (attr) {
+ *attr = ret;
+ }
+ return LY_SUCCESS;
+}
+
+LIBYANG_API_DEF LY_ERR
+lyd_new_attr2(struct lyd_node *parent, const char *module_ns, const char *name, const char *value,
+ struct lyd_attr **attr)
+{
+ struct lyd_attr *ret = NULL;
+ const struct ly_ctx *ctx;
+ const char *prefix, *tmp;
+ size_t pref_len, name_len;
+
+ LY_CHECK_ARG_RET(NULL, parent, !parent->schema, name, LY_EINVAL);
+
+ ctx = LYD_CTX(parent);
+
+ /* parse the name */
+ tmp = name;
+ if (ly_parse_nodeid(&tmp, &prefix, &pref_len, &name, &name_len) || tmp[0]) {
+ LOGERR(ctx, LY_EINVAL, "Attribute name \"%s\" is not valid.", name);
+ return LY_EVALID;
+ }
+
+ if ((pref_len == 3) && !strncmp(prefix, "xml", 3)) {
+ /* not a prefix but special name */
+ name = prefix;
+ name_len += 1 + pref_len;
+ prefix = NULL;
+ pref_len = 0;
+ }
+
+ /* set value if none */
+ if (!value) {
+ value = "";
+ }
+
+ LY_CHECK_RET(lyd_create_attr(parent, &ret, ctx, name, name_len, prefix, pref_len, module_ns,
+ module_ns ? strlen(module_ns) : 0, value, strlen(value), NULL, LY_VALUE_XML, NULL, LYD_HINT_DATA));
+
+ if (attr) {
+ *attr = ret;
+ }
+ return LY_SUCCESS;
+}
+
+/**
+ * @brief Change the value of a term (leaf or leaf-list) node.
+ *
+ * Node changed this way is always considered explicitly set, meaning its default flag
+ * is always cleared.
+ *
+ * @param[in] term Term node to change.
+ * @param[in] value New value to set.
+ * @param[in] value_len Length of @p value.
+ * @param[in] format Format of @p value.
+ * @return LY_SUCCESS if value was changed,
+ * @return LY_EEXIST if value was the same and only the default flag was cleared,
+ * @return LY_ENOT if the values were equal and no change occured,
+ * @return LY_ERR value on other errors.
+ */
+static LY_ERR
+_lyd_change_term(struct lyd_node *term, const void *value, size_t value_len, LY_VALUE_FORMAT format)
+{
+ LY_ERR ret = LY_SUCCESS;
+ struct lysc_type *type;
+ struct lyd_node_term *t;
+ struct lyd_node *parent;
+ struct lyd_value val;
+ ly_bool dflt_change, val_change;
+
+ assert(term && term->schema && (term->schema->nodetype & LYD_NODE_TERM));
+
+ t = (struct lyd_node_term *)term;
+ type = ((struct lysc_node_leaf *)term->schema)->type;
+
+ /* parse the new value */
+ LOG_LOCSET(term->schema, term, NULL, NULL);
+ ret = lyd_value_store(LYD_CTX(term), &val, type, value, value_len, NULL, format, NULL, LYD_HINT_DATA, term->schema, NULL);
+ LOG_LOCBACK(term->schema ? 1 : 0, 1, 0, 0);
+ LY_CHECK_GOTO(ret, cleanup);
+
+ /* compare original and new value */
+ if (type->plugin->compare(&t->value, &val)) {
+ /* values differ, switch them */
+ type->plugin->free(LYD_CTX(term), &t->value);
+ t->value = val;
+ val_change = 1;
+ } else {
+ /* same values, free the new stored one */
+ type->plugin->free(LYD_CTX(term), &val);
+ val_change = 0;
+ }
+
+ /* always clear the default flag */
+ if (term->flags & LYD_DEFAULT) {
+ for (parent = term; parent; parent = lyd_parent(parent)) {
+ parent->flags &= ~LYD_DEFAULT;
+ }
+ dflt_change = 1;
+ } else {
+ dflt_change = 0;
+ }
+
+ if (val_change || dflt_change) {
+ /* make the node non-validated */
+ term->flags &= LYD_NEW;
+ }
+
+ if (val_change) {
+ if (term->schema->nodetype == LYS_LEAFLIST) {
+ /* leaf-list needs to be hashed again and re-inserted into parent */
+ lyd_unlink_hash(term);
+ lyd_hash(term);
+ LY_CHECK_GOTO(ret = lyd_insert_hash(term), cleanup);
+ } else if ((term->schema->flags & LYS_KEY) && term->parent) {
+ /* list needs to be updated if its key was changed */
+ assert(term->parent->schema->nodetype == LYS_LIST);
+ lyd_unlink_hash(lyd_parent(term));
+ lyd_hash(lyd_parent(term));
+ LY_CHECK_GOTO(ret = lyd_insert_hash(lyd_parent(term)), cleanup);
+ } /* else leaf that is not a key, its value is not used for its hash so it does not change */
+ }
+
+ /* retrun value */
+ if (!val_change) {
+ if (dflt_change) {
+ /* only default flag change */
+ ret = LY_EEXIST;
+ } else {
+ /* no change */
+ ret = LY_ENOT;
+ }
+ } /* else value changed, LY_SUCCESS */
+
+cleanup:
+ return ret;
+}
+
+LIBYANG_API_DEF LY_ERR
+lyd_change_term(struct lyd_node *term, const char *val_str)
+{
+ LY_CHECK_ARG_RET(NULL, term, term->schema, term->schema->nodetype & LYD_NODE_TERM, LY_EINVAL);
+
+ return _lyd_change_term(term, val_str, val_str ? strlen(val_str) : 0, LY_VALUE_JSON);
+}
+
+LIBYANG_API_DEF LY_ERR
+lyd_change_term_bin(struct lyd_node *term, const void *value, size_t value_len)
+{
+ LY_CHECK_ARG_RET(NULL, term, term->schema, term->schema->nodetype & LYD_NODE_TERM, LY_EINVAL);
+
+ return _lyd_change_term(term, value, value_len, LY_VALUE_LYB);
+}
+
+LIBYANG_API_DEF LY_ERR
+lyd_change_term_canon(struct lyd_node *term, const char *val_str)
+{
+ LY_CHECK_ARG_RET(NULL, term, term->schema, term->schema->nodetype & LYD_NODE_TERM, LY_EINVAL);
+
+ return _lyd_change_term(term, val_str, val_str ? strlen(val_str) : 0, LY_VALUE_CANON);
+}
+
+LIBYANG_API_DEF LY_ERR
+lyd_change_meta(struct lyd_meta *meta, const char *val_str)
+{
+ LY_ERR ret = LY_SUCCESS;
+ struct lyd_meta *m2 = NULL;
+ struct lyd_value val;
+ ly_bool val_change;
+
+ LY_CHECK_ARG_RET(NULL, meta, LY_EINVAL);
+
+ if (!val_str) {
+ val_str = "";
+ }
+
+ /* parse the new value into a new meta structure */
+ ret = lyd_create_meta(NULL, &m2, meta->annotation->module, meta->name, strlen(meta->name), val_str, strlen(val_str),
+ NULL, LY_VALUE_JSON, NULL, LYD_HINT_DATA, meta->parent ? meta->parent->schema : NULL, 0, NULL);
+ LY_CHECK_GOTO(ret, cleanup);
+
+ /* compare original and new value */
+ if (lyd_compare_meta(meta, m2)) {
+ /* values differ, switch them */
+ val = meta->value;
+ meta->value = m2->value;
+ m2->value = val;
+ val_change = 1;
+ } else {
+ val_change = 0;
+ }
+
+ /* retrun value */
+ if (!val_change) {
+ /* no change */
+ ret = LY_ENOT;
+ } /* else value changed, LY_SUCCESS */
+
+cleanup:
+ lyd_free_meta_single(m2);
+ return ret;
+}
+
+/**
+ * @brief Update node value.
+ *
+ * @param[in] node Node to update.
+ * @param[in] value New value to set.
+ * @param[in] value_len Length of @p value.
+ * @param[in] value_type Type of @p value for anydata/anyxml node.
+ * @param[in] format Format of @p value.
+ * @param[out] new_parent Set to @p node if the value was updated, otherwise set to NULL.
+ * @param[out] new_node Set to @p node if the value was updated, otherwise set to NULL.
+ * @return LY_ERR value.
+ */
+static LY_ERR
+lyd_new_path_update(struct lyd_node *node, const void *value, size_t value_len, LYD_ANYDATA_VALUETYPE value_type,
+ LY_VALUE_FORMAT format, struct lyd_node **new_parent, struct lyd_node **new_node)
+{
+ LY_ERR ret = LY_SUCCESS;
+ struct lyd_node *new_any;
+
+ switch (node->schema->nodetype) {
+ case LYS_CONTAINER:
+ case LYS_NOTIF:
+ case LYS_RPC:
+ case LYS_ACTION:
+ case LYS_LIST:
+ /* if it exists, there is nothing to update */
+ *new_parent = NULL;
+ *new_node = NULL;
+ break;
+ case LYS_LEAFLIST:
+ if (!lysc_is_dup_inst_list(node->schema)) {
+ /* if it exists, there is nothing to update */
+ *new_parent = NULL;
+ *new_node = NULL;
+ break;
+ }
+ /* fallthrough */
+ case LYS_LEAF:
+ ret = _lyd_change_term(node, value, value_len, format);
+ if ((ret == LY_SUCCESS) || (ret == LY_EEXIST)) {
+ /* there was an actual change (at least of the default flag) */
+ *new_parent = node;
+ *new_node = node;
+ ret = LY_SUCCESS;
+ } else if (ret == LY_ENOT) {
+ /* no change */
+ *new_parent = NULL;
+ *new_node = NULL;
+ ret = LY_SUCCESS;
+ } /* else error */
+ break;
+ case LYS_ANYDATA:
+ case LYS_ANYXML:
+ /* create a new any node */
+ LY_CHECK_RET(lyd_create_any(node->schema, value, value_type, 0, &new_any));
+
+ /* compare with the existing one */
+ if (lyd_compare_single(node, new_any, 0)) {
+ /* not equal, switch values (so that we can use generic node free) */
+ ((struct lyd_node_any *)new_any)->value = ((struct lyd_node_any *)node)->value;
+ ((struct lyd_node_any *)new_any)->value_type = ((struct lyd_node_any *)node)->value_type;
+ ((struct lyd_node_any *)node)->value.str = value;
+ ((struct lyd_node_any *)node)->value_type = value_type;
+
+ *new_parent = node;
+ *new_node = node;
+ } else {
+ /* they are equal */
+ *new_parent = NULL;
+ *new_node = NULL;
+ }
+ lyd_free_tree(new_any);
+ break;
+ default:
+ LOGINT(LYD_CTX(node));
+ ret = LY_EINT;
+ break;
+ }
+
+ return ret;
+}
+
+static LY_ERR
+lyd_new_path_check_find_lypath(struct ly_path *path, const char *str_path, const char *value, size_t value_len,
+ LY_VALUE_FORMAT format, uint32_t options)
+{
+ LY_ERR r;
+ struct ly_path_predicate *pred;
+ struct lyd_value val;
+ const struct lysc_node *schema = NULL;
+ LY_ARRAY_COUNT_TYPE u, new_count;
+ int create = 0;
+
+ assert(path);
+
+ /* go through all the compiled nodes */
+ LY_ARRAY_FOR(path, u) {
+ schema = path[u].node;
+
+ if (lysc_is_dup_inst_list(schema)) {
+ if (path[u].pred_type == LY_PATH_PREDTYPE_NONE) {
+ /* creating a new key-less list or state leaf-list instance */
+ create = 1;
+ new_count = u;
+ } else if (path[u].pred_type != LY_PATH_PREDTYPE_POSITION) {
+ LOG_LOCSET(schema, NULL, NULL, NULL);
+ LOGVAL(schema->module->ctx, LYVE_XPATH, "Invalid predicate for %s \"%s\" in path \"%s\".",
+ lys_nodetype2str(schema->nodetype), schema->name, str_path);
+ LOG_LOCBACK(1, 0, 0, 0);
+ return LY_EINVAL;
+ }
+ } else if ((schema->nodetype == LYS_LIST) && (path[u].pred_type != LY_PATH_PREDTYPE_LIST)) {
+ if ((u < LY_ARRAY_COUNT(path) - 1) || !(options & LYD_NEW_PATH_OPAQ)) {
+ LOG_LOCSET(schema, NULL, NULL, NULL);
+ LOGVAL(schema->module->ctx, LYVE_XPATH, "Predicate missing for %s \"%s\" in path \"%s\".",
+ lys_nodetype2str(schema->nodetype), schema->name, str_path);
+ LOG_LOCBACK(1, 0, 0, 0);
+ return LY_EINVAL;
+ } /* else creating an opaque list */
+ } else if ((schema->nodetype == LYS_LEAFLIST) && (path[u].pred_type != LY_PATH_PREDTYPE_LEAFLIST)) {
+ r = LY_SUCCESS;
+ if (options & LYD_NEW_PATH_OPAQ) {
+ r = lyd_value_validate(NULL, schema, value, value_len, NULL, NULL, NULL);
+ }
+ if (!r) {
+ /* try to store the value */
+ LY_CHECK_RET(lyd_value_store(schema->module->ctx, &val, ((struct lysc_node_leaflist *)schema)->type,
+ value, value_len, NULL, format, NULL, LYD_HINT_DATA, schema, NULL));
+ ++((struct lysc_type *)val.realtype)->refcount;
+
+ /* store the new predicate so that it is used when searching for this instance */
+ path[u].pred_type = LY_PATH_PREDTYPE_LEAFLIST;
+ LY_ARRAY_NEW_RET(schema->module->ctx, path[u].predicates, pred, LY_EMEM);
+ pred->value = val;
+ } /* else we have opaq flag and the value is not valid, leave no predicate and then create an opaque node */
+ }
+ }
+
+ if (create) {
+ /* hide the nodes that should always be created so they are not found */
+ while (new_count < LY_ARRAY_COUNT(path)) {
+ LY_ARRAY_DECREMENT(path);
+ }
+ }
+
+ return LY_SUCCESS;
+}
+
+/**
+ * @brief Create a new node in the data tree based on a path. All node types can be created.
+ *
+ * If @p path points to a list key, the key value from the predicate is used and @p value is ignored.
+ * Also, if a leaf-list is being created and both a predicate is defined in @p path
+ * and @p value is set, the predicate is preferred.
+ *
+ * For key-less lists and state leaf-lists, positional predicates can be used. If no preciate is used for these
+ * nodes, they are always created.
+ *
+ * @param[in] parent Data parent to add to/modify, can be NULL. Note that in case a first top-level sibling is used,
+ * it may no longer be first if @p path is absolute and starts with a non-existing top-level node inserted
+ * before @p parent. Use ::lyd_first_sibling() to adjust @p parent in these cases.
+ * @param[in] ctx libyang context, must be set if @p parent is NULL.
+ * @param[in] ext Extension instance where the node being created is defined. This argument takes effect only for absolute
+ * path or when the relative paths touches document root (top-level). In such cases the present extension instance replaces
+ * searching for the appropriate module.
+ * @param[in] path [Path](@ref howtoXPath) to create.
+ * @param[in] value Value of the new leaf/leaf-list (const char *) in ::LY_VALUE_JSON format. If creating an
+ * anyxml/anydata node, the expected type depends on @p value_type. For other node types, it should be NULL.
+ * @param[in] value_len Length of @p value in bytes. May be 0 if @p value is a zero-terminated string. Ignored when
+ * creating anyxml/anydata nodes.
+ * @param[in] value_type Anyxml/anydata node @p value type.
+ * @param[in] options Bitmask of options, see @ref pathoptions.
+ * @param[out] new_parent Optional first parent node created. If only one node was created, equals to @p new_node.
+ * @param[out] new_node Optional last node created.
+ * @return LY_ERR value.
+ */
+static LY_ERR
+lyd_new_path_(struct lyd_node *parent, const struct ly_ctx *ctx, const struct lysc_ext_instance *ext, const char *path,
+ const void *value, size_t value_len, LYD_ANYDATA_VALUETYPE value_type, uint32_t options,
+ struct lyd_node **new_parent, struct lyd_node **new_node)
+{
+ LY_ERR ret = LY_SUCCESS, r;
+ struct lyxp_expr *exp = NULL;
+ struct ly_path *p = NULL;
+ struct lyd_node *nparent = NULL, *nnode = NULL, *node = NULL, *cur_parent;
+ const struct lysc_node *schema;
+ const struct lyd_value *val = NULL;
+ LY_ARRAY_COUNT_TYPE path_idx = 0, orig_count = 0;
+ LY_VALUE_FORMAT format;
+
+ assert(parent || ctx);
+ assert(path && ((path[0] == '/') || parent));
+ assert(!(options & LYD_NEW_PATH_BIN_VALUE) || !(options & LYD_NEW_PATH_CANON_VALUE));
+
+ if (!ctx) {
+ ctx = LYD_CTX(parent);
+ }
+ if (value && !value_len) {
+ value_len = strlen(value);
+ }
+ if (options & LYD_NEW_PATH_BIN_VALUE) {
+ format = LY_VALUE_LYB;
+ } else if (options & LYD_NEW_PATH_CANON_VALUE) {
+ format = LY_VALUE_CANON;
+ } else {
+ format = LY_VALUE_JSON;
+ }
+
+ /* parse path */
+ LY_CHECK_GOTO(ret = ly_path_parse(ctx, NULL, path, strlen(path), 0, LY_PATH_BEGIN_EITHER, LY_PATH_PREFIX_OPTIONAL,
+ LY_PATH_PRED_SIMPLE, &exp), cleanup);
+
+ /* compile path */
+ LY_CHECK_GOTO(ret = ly_path_compile(ctx, NULL, lyd_node_schema(parent), ext, exp, options & LYD_NEW_PATH_OUTPUT ?
+ LY_PATH_OPER_OUTPUT : LY_PATH_OPER_INPUT, LY_PATH_TARGET_MANY, 0, LY_VALUE_JSON, NULL, &p), cleanup);
+
+ /* check the compiled path before searching existing nodes, it may be shortened */
+ orig_count = LY_ARRAY_COUNT(p);
+ LY_CHECK_GOTO(ret = lyd_new_path_check_find_lypath(p, path, value, value_len, format, options), cleanup);
+
+ /* try to find any existing nodes in the path */
+ if (parent) {
+ ret = ly_path_eval_partial(p, parent, &path_idx, &node);
+ if (ret == LY_SUCCESS) {
+ if (orig_count == LY_ARRAY_COUNT(p)) {
+ /* the node exists, are we supposed to update it or is it just a default? */
+ if (!(options & LYD_NEW_PATH_UPDATE) && !(node->flags & LYD_DEFAULT)) {
+ LOG_LOCSET(NULL, node, NULL, NULL);
+ LOGVAL(ctx, LYVE_REFERENCE, "Path \"%s\" already exists", path);
+ LOG_LOCBACK(0, 1, 0, 0);
+ ret = LY_EEXIST;
+ goto cleanup;
+ }
+
+ /* update the existing node */
+ ret = lyd_new_path_update(node, value, value_len, value_type, format, &nparent, &nnode);
+ goto cleanup;
+ } /* else we were not searching for the whole path */
+ } else if (ret == LY_EINCOMPLETE) {
+ /* some nodes were found, adjust the iterator to the next segment */
+ ++path_idx;
+ } else if (ret == LY_ENOTFOUND) {
+ /* we will create the nodes from top-level, default behavior (absolute path), or from the parent (relative path) */
+ if (lysc_data_parent(p[0].node)) {
+ node = parent;
+ }
+ } else {
+ /* error */
+ goto cleanup;
+ }
+ }
+
+ /* restore the full path for creating new nodes */
+ while (orig_count > LY_ARRAY_COUNT(p)) {
+ LY_ARRAY_INCREMENT(p);
+ }
+
+ /* create all the non-existing nodes in a loop */
+ for ( ; path_idx < LY_ARRAY_COUNT(p); ++path_idx) {
+ cur_parent = node;
+ schema = p[path_idx].node;
+
+ switch (schema->nodetype) {
+ case LYS_LIST:
+ if (lysc_is_dup_inst_list(schema)) {
+ /* create key-less list instance */
+ LY_CHECK_GOTO(ret = lyd_create_inner(schema, &node), cleanup);
+ } else if ((options & LYD_NEW_PATH_OPAQ) && (p[path_idx].pred_type == LY_PATH_PREDTYPE_NONE)) {
+ /* creating opaque list without keys */
+ LY_CHECK_GOTO(ret = lyd_create_opaq(ctx, schema->name, strlen(schema->name), NULL, 0,
+ schema->module->name, strlen(schema->module->name), NULL, 0, NULL, LY_VALUE_JSON, NULL,
+ LYD_NODEHINT_LIST, &node), cleanup);
+ } else {
+ /* create standard list instance */
+ assert(p[path_idx].pred_type == LY_PATH_PREDTYPE_LIST);
+ LY_CHECK_GOTO(ret = lyd_create_list(schema, p[path_idx].predicates, &node), cleanup);
+ }
+ break;
+ case LYS_CONTAINER:
+ case LYS_NOTIF:
+ case LYS_RPC:
+ case LYS_ACTION:
+ LY_CHECK_GOTO(ret = lyd_create_inner(schema, &node), cleanup);
+ break;
+ case LYS_LEAFLIST:
+ if ((options & LYD_NEW_PATH_OPAQ) && (p[path_idx].pred_type != LY_PATH_PREDTYPE_LEAFLIST)) {
+ /* we have not checked this only for dup-inst lists, otherwise it must be opaque */
+ r = LY_EVALID;
+ if (lysc_is_dup_inst_list(schema)) {
+ /* validate value */
+ r = lyd_value_validate(NULL, schema, value ? value : "", value_len, NULL, NULL, NULL);
+ }
+ if (r && (r != LY_EINCOMPLETE)) {
+ /* creating opaque leaf-list */
+ LY_CHECK_GOTO(ret = lyd_create_opaq(ctx, schema->name, strlen(schema->name), value, value_len,
+ schema->module->name, strlen(schema->module->name), NULL, 0, NULL, format, NULL,
+ LYD_NODEHINT_LEAFLIST, &node), cleanup);
+ break;
+ }
+ }
+
+ /* get value to set */
+ if (p[path_idx].pred_type == LY_PATH_PREDTYPE_LEAFLIST) {
+ val = &p[path_idx].predicates[0].value;
+ }
+
+ /* create a leaf-list instance */
+ if (val) {
+ LY_CHECK_GOTO(ret = lyd_create_term2(schema, &p[path_idx].predicates[0].value, &node), cleanup);
+ } else {
+ LY_CHECK_GOTO(ret = lyd_create_term(schema, value, value_len, NULL, format, NULL, LYD_HINT_DATA,
+ NULL, &node), cleanup);
+ }
+ break;
+ case LYS_LEAF:
+ if (lysc_is_key(schema) && cur_parent->schema) {
+ /* it must have been already created or some error will occur later */
+ lyd_find_sibling_schema(lyd_child(cur_parent), schema, &node);
+ assert(node);
+ goto next_iter;
+ }
+
+ if (options & LYD_NEW_PATH_OPAQ) {
+ if (cur_parent && !cur_parent->schema) {
+ /* always create opaque nodes for opaque parents */
+ r = LY_ENOT;
+ } else {
+ /* validate value */
+ r = lyd_value_validate(NULL, schema, value ? value : "", value_len, NULL, NULL, NULL);
+ }
+ if (r && (r != LY_EINCOMPLETE)) {
+ /* creating opaque leaf */
+ LY_CHECK_GOTO(ret = lyd_create_opaq(ctx, schema->name, strlen(schema->name), value, value_len,
+ schema->module->name, strlen(schema->module->name), NULL, 0, NULL, format, NULL, 0, &node),
+ cleanup);
+ break;
+ }
+ }
+
+ /* create a leaf instance */
+ LY_CHECK_GOTO(ret = lyd_create_term(schema, value, value_len, NULL, format, NULL, LYD_HINT_DATA, NULL,
+ &node), cleanup);
+ break;
+ case LYS_ANYDATA:
+ case LYS_ANYXML:
+ LY_CHECK_GOTO(ret = lyd_create_any(schema, value, value_type, 0, &node), cleanup);
+ break;
+ default:
+ LOGINT(ctx);
+ ret = LY_EINT;
+ goto cleanup;
+ }
+
+ if (p[path_idx].ext) {
+ node->flags |= LYD_EXT;
+ }
+ if (cur_parent) {
+ /* connect to the parent */
+ lyd_insert_node(cur_parent, NULL, node, 0);
+ } else if (parent) {
+ /* connect to top-level siblings */
+ lyd_insert_node(NULL, &parent, node, 0);
+ }
+
+next_iter:
+ /* update remembered nodes */
+ if (!nparent) {
+ nparent = node;
+ }
+ nnode = node;
+ }
+
+cleanup:
+ lyxp_expr_free(ctx, exp);
+ if (p) {
+ while (orig_count > LY_ARRAY_COUNT(p)) {
+ LY_ARRAY_INCREMENT(p);
+ }
+ }
+ ly_path_free(ctx, p);
+ if (!ret) {
+ /* set out params only on success */
+ if (new_parent) {
+ *new_parent = nparent;
+ }
+ if (new_node) {
+ *new_node = nnode;
+ }
+ } else {
+ lyd_free_tree(nparent);
+ }
+ return ret;
+}
+
+LIBYANG_API_DEF LY_ERR
+lyd_new_path(struct lyd_node *parent, const struct ly_ctx *ctx, const char *path, const char *value, uint32_t options,
+ struct lyd_node **node)
+{
+ LY_CHECK_ARG_RET(ctx, parent || ctx, path, (path[0] == '/') || parent,
+ !(options & LYD_NEW_PATH_BIN_VALUE) || !(options & LYD_NEW_PATH_CANON_VALUE), LY_EINVAL);
+ LY_CHECK_CTX_EQUAL_RET(parent ? LYD_CTX(parent) : NULL, ctx, LY_EINVAL);
+
+ return lyd_new_path_(parent, ctx, NULL, path, value, 0, LYD_ANYDATA_STRING, options, node, NULL);
+}
+
+LIBYANG_API_DEF LY_ERR
+lyd_new_path2(struct lyd_node *parent, const struct ly_ctx *ctx, const char *path, const void *value,
+ size_t value_len, LYD_ANYDATA_VALUETYPE value_type, uint32_t options, struct lyd_node **new_parent,
+ struct lyd_node **new_node)
+{
+ LY_CHECK_ARG_RET(ctx, parent || ctx, path, (path[0] == '/') || parent,
+ !(options & LYD_NEW_PATH_BIN_VALUE) || !(options & LYD_NEW_PATH_CANON_VALUE), LY_EINVAL);
+ LY_CHECK_CTX_EQUAL_RET(parent ? LYD_CTX(parent) : NULL, ctx, LY_EINVAL);
+
+ return lyd_new_path_(parent, ctx, NULL, path, value, value_len, value_type, options, new_parent, new_node);
+}
+
+LIBYANG_API_DEF LY_ERR
+lyd_new_ext_path(struct lyd_node *parent, const struct lysc_ext_instance *ext, const char *path, const void *value,
+ uint32_t options, struct lyd_node **node)
+{
+ const struct ly_ctx *ctx = ext ? ext->module->ctx : NULL;
+
+ LY_CHECK_ARG_RET(ctx, ext, path, (path[0] == '/') || parent,
+ !(options & LYD_NEW_PATH_BIN_VALUE) || !(options & LYD_NEW_PATH_CANON_VALUE), LY_EINVAL);
+ LY_CHECK_CTX_EQUAL_RET(parent ? LYD_CTX(parent) : NULL, ctx, LY_EINVAL);
+
+ return lyd_new_path_(parent, ctx, ext, path, value, 0, LYD_ANYDATA_STRING, options, node, NULL);
+}
+
+LY_ERR
+lyd_new_implicit_r(struct lyd_node *parent, struct lyd_node **first, const struct lysc_node *sparent,
+ const struct lys_module *mod, struct ly_set *node_when, struct ly_set *node_types, uint32_t impl_opts,
+ struct lyd_node **diff)
+{
+ LY_ERR ret;
+ const struct lysc_node *iter = NULL;
+ struct lyd_node *node = NULL;
+ struct lyd_value **dflts;
+ LY_ARRAY_COUNT_TYPE u;
+ uint32_t getnext_opts;
+
+ assert(first && (parent || sparent || mod));
+
+ if (!sparent && parent) {
+ sparent = parent->schema;
+ }
+
+ getnext_opts = LYS_GETNEXT_WITHCHOICE;
+ if (impl_opts & LYD_IMPLICIT_OUTPUT) {
+ getnext_opts |= LYS_GETNEXT_OUTPUT;
+ }
+
+ while ((iter = lys_getnext(iter, sparent, mod ? mod->compiled : NULL, getnext_opts))) {
+ if ((impl_opts & LYD_IMPLICIT_NO_STATE) && (iter->flags & LYS_CONFIG_R)) {
+ continue;
+ } else if ((impl_opts & LYD_IMPLICIT_NO_CONFIG) && (iter->flags & LYS_CONFIG_W)) {
+ continue;
+ }
+
+ switch (iter->nodetype) {
+ case LYS_CHOICE:
+ node = lys_getnext_data(NULL, *first, NULL, iter, NULL);
+ if (!node && ((struct lysc_node_choice *)iter)->dflt) {
+ /* create default case data */
+ LY_CHECK_RET(lyd_new_implicit_r(parent, first, &((struct lysc_node_choice *)iter)->dflt->node,
+ NULL, node_when, node_types, impl_opts, diff));
+ } else if (node) {
+ /* create any default data in the existing case */
+ assert(node->schema->parent->nodetype == LYS_CASE);
+ LY_CHECK_RET(lyd_new_implicit_r(parent, first, node->schema->parent, NULL, node_when, node_types,
+ impl_opts, diff));
+ }
+ break;
+ case LYS_CONTAINER:
+ if (!(iter->flags & LYS_PRESENCE) && lyd_find_sibling_val(*first, iter, NULL, 0, NULL)) {
+ /* create default NP container */
+ LY_CHECK_RET(lyd_create_inner(iter, &node));
+ node->flags = LYD_DEFAULT | (lysc_has_when(iter) ? LYD_WHEN_TRUE : 0);
+ lyd_insert_node(parent, first, node, 0);
+
+ if (lysc_has_when(iter) && node_when) {
+ /* remember to resolve when */
+ LY_CHECK_RET(ly_set_add(node_when, node, 1, NULL));
+ }
+ if (diff) {
+ /* add into diff */
+ LY_CHECK_RET(lyd_val_diff_add(node, LYD_DIFF_OP_CREATE, diff));
+ }
+
+ /* create any default children */
+ LY_CHECK_RET(lyd_new_implicit_r(node, lyd_node_child_p(node), NULL, NULL, node_when, node_types,
+ impl_opts, diff));
+ }
+ break;
+ case LYS_LEAF:
+ if (!(impl_opts & LYD_IMPLICIT_NO_DEFAULTS) && ((struct lysc_node_leaf *)iter)->dflt &&
+ lyd_find_sibling_val(*first, iter, NULL, 0, NULL)) {
+ /* create default leaf */
+ ret = lyd_create_term2(iter, ((struct lysc_node_leaf *)iter)->dflt, &node);
+ if (ret == LY_EINCOMPLETE) {
+ if (node_types) {
+ /* remember to resolve type */
+ LY_CHECK_RET(ly_set_add(node_types, node, 1, NULL));
+ }
+ } else if (ret) {
+ return ret;
+ }
+ node->flags = LYD_DEFAULT | (lysc_has_when(iter) ? LYD_WHEN_TRUE : 0);
+ lyd_insert_node(parent, first, node, 0);
+
+ if (lysc_has_when(iter) && node_when) {
+ /* remember to resolve when */
+ LY_CHECK_RET(ly_set_add(node_when, node, 1, NULL));
+ }
+ if (diff) {
+ /* add into diff */
+ LY_CHECK_RET(lyd_val_diff_add(node, LYD_DIFF_OP_CREATE, diff));
+ }
+ }
+ break;
+ case LYS_LEAFLIST:
+ if (!(impl_opts & LYD_IMPLICIT_NO_DEFAULTS) && ((struct lysc_node_leaflist *)iter)->dflts &&
+ lyd_find_sibling_val(*first, iter, NULL, 0, NULL)) {
+ /* create all default leaf-lists */
+ dflts = ((struct lysc_node_leaflist *)iter)->dflts;
+ LY_ARRAY_FOR(dflts, u) {
+ ret = lyd_create_term2(iter, dflts[u], &node);
+ if (ret == LY_EINCOMPLETE) {
+ if (node_types) {
+ /* remember to resolve type */
+ LY_CHECK_RET(ly_set_add(node_types, node, 1, NULL));
+ }
+ } else if (ret) {
+ return ret;
+ }
+ node->flags = LYD_DEFAULT | (lysc_has_when(iter) ? LYD_WHEN_TRUE : 0);
+ lyd_insert_node(parent, first, node, 0);
+
+ if (lysc_has_when(iter) && node_when) {
+ /* remember to resolve when */
+ LY_CHECK_RET(ly_set_add(node_when, node, 1, NULL));
+ }
+ if (diff) {
+ /* add into diff */
+ LY_CHECK_RET(lyd_val_diff_add(node, LYD_DIFF_OP_CREATE, diff));
+ }
+ }
+ }
+ break;
+ default:
+ /* without defaults */
+ break;
+ }
+ }
+
+ return LY_SUCCESS;
+}
+
+LIBYANG_API_DEF LY_ERR
+lyd_new_implicit_tree(struct lyd_node *tree, uint32_t implicit_options, struct lyd_node **diff)
+{
+ LY_ERR ret = LY_SUCCESS;
+ struct lyd_node *node;
+ struct ly_set node_when = {0};
+
+ LY_CHECK_ARG_RET(NULL, tree, LY_EINVAL);
+ if (diff) {
+ *diff = NULL;
+ }
+
+ LYD_TREE_DFS_BEGIN(tree, node) {
+ /* skip added default nodes */
+ if (((node->flags & (LYD_DEFAULT | LYD_NEW)) != (LYD_DEFAULT | LYD_NEW)) &&
+ (node->schema->nodetype & LYD_NODE_INNER)) {
+ LY_CHECK_GOTO(ret = lyd_new_implicit_r(node, lyd_node_child_p(node), NULL, NULL, &node_when, NULL,
+ implicit_options, diff), cleanup);
+ }
+
+ LYD_TREE_DFS_END(tree, node);
+ }
+
+ /* resolve when and remove any invalid defaults */
+ LY_CHECK_GOTO(ret = lyd_validate_unres(&tree, NULL, &node_when, LYXP_IGNORE_WHEN, NULL, NULL, NULL, 0, diff), cleanup);
+
+cleanup:
+ ly_set_erase(&node_when, NULL);
+ if (ret && diff) {
+ lyd_free_all(*diff);
+ *diff = NULL;
+ }
+ return ret;
+}
+
+LIBYANG_API_DEF LY_ERR
+lyd_new_implicit_all(struct lyd_node **tree, const struct ly_ctx *ctx, uint32_t implicit_options, struct lyd_node **diff)
+{
+ const struct lys_module *mod;
+ struct lyd_node *d = NULL;
+ uint32_t i = 0;
+ LY_ERR ret = LY_SUCCESS;
+
+ LY_CHECK_ARG_RET(ctx, tree, *tree || ctx, LY_EINVAL);
+ LY_CHECK_CTX_EQUAL_RET(*tree ? LYD_CTX(*tree) : NULL, ctx, LY_EINVAL);
+ if (diff) {
+ *diff = NULL;
+ }
+ if (!ctx) {
+ ctx = LYD_CTX(*tree);
+ }
+
+ /* add nodes for each module one-by-one */
+ while ((mod = ly_ctx_get_module_iter(ctx, &i))) {
+ if (!mod->implemented) {
+ continue;
+ }
+
+ LY_CHECK_GOTO(ret = lyd_new_implicit_module(tree, mod, implicit_options, diff ? &d : NULL), cleanup);
+ if (d) {
+ /* merge into one diff */
+ lyd_insert_sibling(*diff, d, diff);
+
+ d = NULL;
+ }
+ }
+
+cleanup:
+ if (ret && diff) {
+ lyd_free_all(*diff);
+ *diff = NULL;
+ }
+ return ret;
+}
+
+LIBYANG_API_DEF LY_ERR
+lyd_new_implicit_module(struct lyd_node **tree, const struct lys_module *module, uint32_t implicit_options,
+ struct lyd_node **diff)
+{
+ LY_ERR ret = LY_SUCCESS;
+ struct lyd_node *root, *d = NULL;
+ struct ly_set node_when = {0};
+
+ LY_CHECK_ARG_RET(NULL, tree, module, LY_EINVAL);
+ LY_CHECK_CTX_EQUAL_RET(*tree ? LYD_CTX(*tree) : NULL, module ? module->ctx : NULL, LY_EINVAL);
+ if (diff) {
+ *diff = NULL;
+ }
+
+ /* add all top-level defaults for this module */
+ LY_CHECK_GOTO(ret = lyd_new_implicit_r(NULL, tree, NULL, module, &node_when, NULL, implicit_options, diff), cleanup);
+
+ /* resolve when and remove any invalid defaults */
+ LY_CHECK_GOTO(ret = lyd_validate_unres(tree, module, &node_when, LYXP_IGNORE_WHEN, NULL, NULL, NULL, 0, diff),
+ cleanup);
+
+ /* process nested nodes */
+ LY_LIST_FOR(*tree, root) {
+ /* skip added default nodes */
+ if ((root->flags & (LYD_DEFAULT | LYD_NEW)) != (LYD_DEFAULT | LYD_NEW)) {
+ LY_CHECK_GOTO(ret = lyd_new_implicit_tree(root, implicit_options, diff ? &d : NULL), cleanup);
+
+ if (d) {
+ /* merge into one diff */
+ lyd_insert_sibling(*diff, d, diff);
+
+ d = NULL;
+ }
+ }
+ }
+
+cleanup:
+ ly_set_erase(&node_when, NULL);
+ if (ret && diff) {
+ lyd_free_all(*diff);
+ *diff = NULL;
+ }
+ return ret;
+}
diff --git a/src/tree_schema_helpers.c b/src/tree_schema_common.c
similarity index 99%
rename from src/tree_schema_helpers.c
rename to src/tree_schema_common.c
index 42aa112..7ba1b23 100644
--- a/src/tree_schema_helpers.c
+++ b/src/tree_schema_common.c
@@ -1,9 +1,10 @@
/**
- * @file tree_schema_helpers.c
+ * @file tree_schema_common.c
* @author Radek Krejci <rkrejci@cesnet.cz>
- * @brief Parsing and validation helper functions for schema trees
+ * @author Michal Vasko <mvasko@cesnet.cz>
+ * @brief Parsing and validation common functions for schema trees
*
- * Copyright (c) 2015 - 2018 CESNET, z.s.p.o.
+ * Copyright (c) 2015 - 2022 CESNET, z.s.p.o.
*
* This source code is licensed under BSD 3-Clause License (the "License").
* You may not use this file except in compliance with the License.