Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 1 | /** |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 2 | * @file tree_data.c |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 3 | * @author Radek Krejci <rkrejci@cesnet.cz> |
Michal Vasko | 6cd9b6b | 2020-06-22 10:05:22 +0200 | [diff] [blame] | 4 | * @brief Data tree functions |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 5 | * |
Michal Vasko | 6cd9b6b | 2020-06-22 10:05:22 +0200 | [diff] [blame] | 6 | * Copyright (c) 2015 - 2020 CESNET, z.s.p.o. |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 7 | * |
| 8 | * This source code is licensed under BSD 3-Clause License (the "License"). |
| 9 | * You may not use this file except in compliance with the License. |
| 10 | * You may obtain a copy of the License at |
| 11 | * |
| 12 | * https://opensource.org/licenses/BSD-3-Clause |
| 13 | */ |
| 14 | |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 15 | #define _GNU_SOURCE |
| 16 | |
| 17 | #include "tree_data.h" |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 18 | |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 19 | #include <assert.h> |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 20 | #include <ctype.h> |
| 21 | #include <errno.h> |
| 22 | #include <fcntl.h> |
| 23 | #include <stdarg.h> |
| 24 | #include <stdint.h> |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 25 | #include <stdio.h> |
| 26 | #include <stdlib.h> |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 27 | #include <string.h> |
| 28 | #include <unistd.h> |
| 29 | |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 30 | #include "common.h" |
Michal Vasko | 5aa44c0 | 2020-06-29 11:47:02 +0200 | [diff] [blame] | 31 | #include "compat.h" |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 32 | #include "context.h" |
| 33 | #include "dict.h" |
Michal Vasko | a6669ba | 2020-08-06 16:14:26 +0200 | [diff] [blame] | 34 | #include "diff.h" |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 35 | #include "hash_table.h" |
Michal Vasko | afac782 | 2020-10-20 14:22:26 +0200 | [diff] [blame] | 36 | #include "in_internal.h" |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 37 | #include "log.h" |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 38 | #include "parser_data.h" |
| 39 | #include "parser_internal.h" |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 40 | #include "path.h" |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 41 | #include "plugins_exts.h" |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 42 | #include "plugins_exts_internal.h" |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 43 | #include "plugins_exts_metadata.h" |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 44 | #include "plugins_types.h" |
| 45 | #include "set.h" |
| 46 | #include "tree.h" |
| 47 | #include "tree_data_internal.h" |
| 48 | #include "tree_schema.h" |
| 49 | #include "tree_schema_internal.h" |
Michal Vasko | a6669ba | 2020-08-06 16:14:26 +0200 | [diff] [blame] | 50 | #include "validation.h" |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 51 | #include "xml.h" |
| 52 | #include "xpath.h" |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 53 | |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 54 | static LY_ERR lyd_find_sibling_schema(const struct lyd_node *siblings, const struct lysc_node *schema, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 55 | struct lyd_node **match); |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 56 | |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 57 | LY_ERR |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 58 | lyd_value_store(const struct ly_ctx *ctx, struct lyd_value *val, const struct lysc_type *type, const char *value, |
| 59 | size_t value_len, ly_bool *dynamic, LY_PREFIX_FORMAT format, void *prefix_data, uint32_t hints, |
| 60 | const struct lysc_node *ctx_node, ly_bool *incomplete, enum LY_VLOG_ELEM log_elem_type, const void *log_elem) |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 61 | { |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 62 | LY_ERR ret; |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 63 | struct ly_err_item *err = NULL; |
Michal Vasko | 25d6ad0 | 2020-10-22 12:20:22 +0200 | [diff] [blame] | 64 | uint32_t options = (dynamic && *dynamic ? LY_TYPE_STORE_DYNAMIC : 0); |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 65 | |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 66 | if (incomplete) { |
| 67 | *incomplete = 0; |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 68 | } |
| 69 | |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 70 | ret = type->plugin->store(ctx, type, value, value_len, options, format, prefix_data, hints, ctx_node, val, &err); |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 71 | if (ret == LY_EINCOMPLETE) { |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 72 | if (incomplete) { |
| 73 | *incomplete = 1; |
| 74 | } |
| 75 | } else if (ret) { |
| 76 | if (err) { |
| 77 | LOGVAL(ctx, log_elem_type, log_elem, err->vecode, err->msg); |
| 78 | ly_err_free(err); |
| 79 | } else { |
| 80 | LOGVAL(ctx, log_elem_type, log_elem, LYVE_OTHER, "Storing value \"%.*s\" failed.", (int)value_len, value); |
| 81 | } |
| 82 | return ret; |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 83 | } |
| 84 | |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 85 | if (dynamic) { |
| 86 | *dynamic = 0; |
| 87 | } |
| 88 | return LY_SUCCESS; |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 89 | } |
| 90 | |
Radek Krejci | 38d8536 | 2019-09-05 16:26:38 +0200 | [diff] [blame] | 91 | LY_ERR |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 92 | lyd_value_validate_incomplete(const struct ly_ctx *ctx, const struct lysc_type *type, struct lyd_value *val, |
| 93 | const struct lyd_node *ctx_node, const struct lyd_node *tree, enum LY_VLOG_ELEM log_elem_type, const void *log_elem) |
Radek Krejci | 38d8536 | 2019-09-05 16:26:38 +0200 | [diff] [blame] | 94 | { |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 95 | LY_ERR ret; |
Radek Krejci | 38d8536 | 2019-09-05 16:26:38 +0200 | [diff] [blame] | 96 | struct ly_err_item *err = NULL; |
Radek Krejci | 38d8536 | 2019-09-05 16:26:38 +0200 | [diff] [blame] | 97 | |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 98 | assert(type->plugin->validate); |
Michal Vasko | 8d54425 | 2020-03-02 10:19:52 +0100 | [diff] [blame] | 99 | |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 100 | ret = type->plugin->validate(ctx, type, ctx_node, tree, val, &err); |
| 101 | if (ret) { |
Radek Krejci | 38d8536 | 2019-09-05 16:26:38 +0200 | [diff] [blame] | 102 | if (err) { |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 103 | LOGVAL(ctx, log_elem_type, log_elem, err->vecode, err->msg); |
Radek Krejci | 38d8536 | 2019-09-05 16:26:38 +0200 | [diff] [blame] | 104 | ly_err_free(err); |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 105 | } else { |
| 106 | LOGVAL(ctx, log_elem_type, log_elem, LYVE_OTHER, "Resolving value \"%s\" failed.", val->canonical); |
Radek Krejci | 38d8536 | 2019-09-05 16:26:38 +0200 | [diff] [blame] | 107 | } |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 108 | return ret; |
Radek Krejci | 38d8536 | 2019-09-05 16:26:38 +0200 | [diff] [blame] | 109 | } |
| 110 | |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 111 | return LY_SUCCESS; |
Radek Krejci | 38d8536 | 2019-09-05 16:26:38 +0200 | [diff] [blame] | 112 | } |
| 113 | |
Michal Vasko | f937cfe | 2020-08-03 16:07:12 +0200 | [diff] [blame] | 114 | LY_ERR |
| 115 | _lys_value_validate(const struct ly_ctx *ctx, const struct lysc_node *node, const char *value, size_t value_len, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 116 | LY_PREFIX_FORMAT format, void *prefix_data) |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 117 | { |
| 118 | LY_ERR rc = LY_SUCCESS; |
| 119 | struct ly_err_item *err = NULL; |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 120 | struct lyd_value storage; |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 121 | struct lysc_type *type; |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 122 | |
| 123 | LY_CHECK_ARG_RET(ctx, node, value, LY_EINVAL); |
| 124 | |
| 125 | if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 126 | LOGARG(ctx, node); |
| 127 | return LY_EINVAL; |
| 128 | } |
| 129 | |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 130 | type = ((struct lysc_node_leaf *)node)->type; |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 131 | rc = type->plugin->store(ctx ? ctx : node->module->ctx, type, value, value_len, 0, format, prefix_data, |
| 132 | LYD_HINT_SCHEMA, node, &storage, &err); |
Radek Krejci | 73dead2 | 2019-07-11 16:46:16 +0200 | [diff] [blame] | 133 | if (rc == LY_EINCOMPLETE) { |
| 134 | /* actually success since we do not provide the context tree and call validation with |
| 135 | * LY_TYPE_OPTS_INCOMPLETE_DATA */ |
| 136 | rc = LY_SUCCESS; |
| 137 | } else if (rc && err) { |
| 138 | if (ctx) { |
| 139 | /* log only in case the ctx was provided as input parameter */ |
Radek Krejci | 73dead2 | 2019-07-11 16:46:16 +0200 | [diff] [blame] | 140 | LOGVAL(ctx, LY_VLOG_STR, err->path, err->vecode, err->msg); |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 141 | } |
Radek Krejci | 73dead2 | 2019-07-11 16:46:16 +0200 | [diff] [blame] | 142 | ly_err_free(err); |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 143 | } |
| 144 | |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 145 | if (!rc) { |
| 146 | type->plugin->free(ctx ? ctx : node->module->ctx, &storage); |
| 147 | } |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 148 | return rc; |
| 149 | } |
| 150 | |
| 151 | API LY_ERR |
Michal Vasko | f937cfe | 2020-08-03 16:07:12 +0200 | [diff] [blame] | 152 | lys_value_validate(const struct ly_ctx *ctx, const struct lysc_node *node, const char *value, size_t value_len) |
| 153 | { |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 154 | return _lys_value_validate(ctx, node, value, value_len, LY_PREF_JSON, NULL); |
Michal Vasko | f937cfe | 2020-08-03 16:07:12 +0200 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | API LY_ERR |
Michal Vasko | 44685da | 2020-03-17 15:38:06 +0100 | [diff] [blame] | 158 | lyd_value_validate(const struct ly_ctx *ctx, const struct lyd_node_term *node, const char *value, size_t value_len, |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 159 | const struct lyd_node *tree, const struct lysc_type **realtype) |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 160 | { |
| 161 | LY_ERR rc; |
| 162 | struct ly_err_item *err = NULL; |
| 163 | struct lysc_type *type; |
Michal Vasko | 3701af5 | 2020-08-03 14:29:38 +0200 | [diff] [blame] | 164 | struct lyd_value val = {0}; |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 165 | ly_bool stored = 0; |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 166 | |
| 167 | LY_CHECK_ARG_RET(ctx, node, value, LY_EINVAL); |
| 168 | |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 169 | type = ((struct lysc_node_leaf *)node->schema)->type; |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 170 | /* store */ |
| 171 | rc = type->plugin->store(ctx ? ctx : LYD_CTX(node), type, value, value_len, 0, LY_PREF_JSON, NULL, |
| 172 | LYD_HINT_DATA, node->schema, &val, &err); |
Radek Krejci | 73dead2 | 2019-07-11 16:46:16 +0200 | [diff] [blame] | 173 | if (rc == LY_EINCOMPLETE) { |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 174 | stored = 1; |
| 175 | |
| 176 | /* resolve */ |
| 177 | rc = type->plugin->validate(ctx ? ctx : LYD_CTX(node), type, (struct lyd_node *)node, tree, &val, &err); |
| 178 | } |
| 179 | |
| 180 | if (rc) { |
Radek Krejci | 73dead2 | 2019-07-11 16:46:16 +0200 | [diff] [blame] | 181 | if (err) { |
| 182 | if (ctx) { |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 183 | LOGVAL(ctx, LY_VLOG_LYD, node, err->vecode, err->msg); |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 184 | } |
Radek Krejci | 73dead2 | 2019-07-11 16:46:16 +0200 | [diff] [blame] | 185 | ly_err_free(err); |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 186 | } |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 187 | if (stored) { |
| 188 | type->plugin->free(ctx ? ctx : LYD_CTX(node), &val); |
| 189 | } |
Radek Krejci | 73dead2 | 2019-07-11 16:46:16 +0200 | [diff] [blame] | 190 | return rc; |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 191 | } |
| 192 | |
Michal Vasko | 3701af5 | 2020-08-03 14:29:38 +0200 | [diff] [blame] | 193 | if (realtype) { |
| 194 | *realtype = val.realtype; |
| 195 | } |
| 196 | |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 197 | type->plugin->free(ctx ? ctx : LYD_CTX(node), &val); |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 198 | return LY_SUCCESS; |
| 199 | } |
| 200 | |
| 201 | API LY_ERR |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 202 | lyd_value_compare(const struct lyd_node_term *node, const char *value, size_t value_len) |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 203 | { |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 204 | LY_ERR ret = LY_SUCCESS; |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 205 | struct ly_ctx *ctx; |
| 206 | struct lysc_type *type; |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 207 | struct lyd_value val = {0}; |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 208 | |
| 209 | LY_CHECK_ARG_RET(node ? node->schema->module->ctx : NULL, node, value, LY_EINVAL); |
| 210 | |
| 211 | ctx = node->schema->module->ctx; |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 212 | type = ((struct lysc_node_leaf *)node->schema)->type; |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 213 | |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 214 | /* store the value */ |
| 215 | ret = lyd_value_store(ctx, &val, type, value, value_len, NULL, LY_PREF_JSON, NULL, LYD_HINT_DATA, node->schema, |
| 216 | NULL, LY_VLOG_LYSC, node->schema); |
| 217 | LY_CHECK_RET(ret); |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 218 | |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 219 | /* compare values */ |
| 220 | ret = type->plugin->compare(&node->value, &val); |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 221 | |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 222 | type->plugin->free(ctx, &val); |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 223 | return ret; |
| 224 | } |
| 225 | |
Radek Krejci | 1961125 | 2020-10-04 13:54:53 +0200 | [diff] [blame] | 226 | API ly_bool |
| 227 | lyd_is_default(const struct lyd_node *node) |
| 228 | { |
| 229 | const struct lysc_node_leaf *leaf; |
| 230 | const struct lysc_node_leaflist *llist; |
| 231 | const struct lyd_node_term *term; |
| 232 | LY_ARRAY_COUNT_TYPE u; |
| 233 | |
| 234 | assert(node->schema->nodetype & LYD_NODE_TERM); |
| 235 | term = (const struct lyd_node_term *)node; |
| 236 | |
| 237 | if (node->schema->nodetype == LYS_LEAF) { |
| 238 | leaf = (const struct lysc_node_leaf *)node->schema; |
| 239 | if (!leaf->dflt) { |
| 240 | return 0; |
| 241 | } |
| 242 | |
| 243 | /* compare with the default value */ |
| 244 | if (leaf->type->plugin->compare(&term->value, leaf->dflt)) { |
| 245 | return 0; |
| 246 | } |
| 247 | } else { |
| 248 | llist = (const struct lysc_node_leaflist *)node->schema; |
| 249 | if (!llist->dflts) { |
| 250 | return 0; |
| 251 | } |
| 252 | |
| 253 | LY_ARRAY_FOR(llist->dflts, u) { |
| 254 | /* compare with each possible default value */ |
| 255 | if (llist->type->plugin->compare(&term->value, llist->dflts[u])) { |
| 256 | return 0; |
| 257 | } |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | return 1; |
| 262 | } |
| 263 | |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 264 | static LYD_FORMAT |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 265 | lyd_parse_get_format(const struct ly_in *in, LYD_FORMAT format) |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 266 | { |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 267 | if (!format && (in->type == LY_IN_FILEPATH)) { |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 268 | /* unknown format - try to detect it from filename's suffix */ |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 269 | const char *path = in->method.fpath.filepath; |
| 270 | size_t len = strlen(path); |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 271 | |
| 272 | /* ignore trailing whitespaces */ |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 273 | for ( ; len > 0 && isspace(path[len - 1]); len--) {} |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 274 | |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 275 | if ((len >= 5) && !strncmp(&path[len - 4], ".xml", 4)) { |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 276 | format = LYD_XML; |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 277 | } else if ((len >= 6) && !strncmp(&path[len - 5], ".json", 5)) { |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 278 | format = LYD_JSON; |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 279 | } else if ((len >= 5) && !strncmp(&path[len - 4], ".lyb", 4)) { |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 280 | format = LYD_LYB; |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 281 | } /* else still unknown */ |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 282 | } |
| 283 | |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 284 | return format; |
| 285 | } |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 286 | |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 287 | API LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 288 | lyd_parse_data(const struct ly_ctx *ctx, struct ly_in *in, LYD_FORMAT format, uint32_t parse_options, uint32_t validate_options, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 289 | struct lyd_node **tree) |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 290 | { |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 291 | LY_ERR ret = LY_SUCCESS; |
| 292 | struct lyd_ctx *lydctx = NULL; |
| 293 | |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 294 | LY_CHECK_ARG_RET(ctx, ctx, in, tree, LY_EINVAL); |
| 295 | LY_CHECK_ARG_RET(ctx, !(parse_options & ~LYD_PARSE_OPTS_MASK), LY_EINVAL); |
| 296 | LY_CHECK_ARG_RET(ctx, !(validate_options & ~LYD_VALIDATE_OPTS_MASK), LY_EINVAL); |
| 297 | |
| 298 | format = lyd_parse_get_format(in, format); |
| 299 | LY_CHECK_ARG_RET(ctx, format, LY_EINVAL); |
| 300 | |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 301 | /* init */ |
| 302 | *tree = NULL; |
| 303 | |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 304 | /* remember input position */ |
| 305 | in->func_start = in->current; |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 306 | |
| 307 | switch (format) { |
| 308 | case LYD_XML: |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 309 | LY_CHECK_RET(lyd_parse_xml_data(ctx, in, parse_options, validate_options, tree, &lydctx)); |
| 310 | break; |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 311 | case LYD_JSON: |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 312 | LY_CHECK_RET(lyd_parse_json_data(ctx, in, parse_options, validate_options, tree, &lydctx)); |
| 313 | break; |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 314 | case LYD_LYB: |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 315 | LY_CHECK_RET(lyd_parse_lyb_data(ctx, in, parse_options, validate_options, tree, &lydctx)); |
| 316 | break; |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 317 | case LYD_UNKNOWN: |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 318 | LOGINT_RET(ctx); |
| 319 | } |
| 320 | |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 321 | if (!(parse_options & LYD_PARSE_ONLY)) { |
| 322 | uint32_t i = 0; |
| 323 | const struct lys_module *mod; |
| 324 | struct lyd_node *first, *next, **first2; |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 325 | |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 326 | next = *tree; |
| 327 | while (1) { |
| 328 | if (validate_options & LYD_VALIDATE_PRESENT) { |
| 329 | mod = lyd_data_next_module(&next, &first); |
| 330 | } else { |
| 331 | mod = lyd_mod_next_module(next, NULL, ctx, &i, &first); |
| 332 | } |
| 333 | if (!mod) { |
| 334 | break; |
| 335 | } |
| 336 | if (first == *tree) { |
| 337 | /* make sure first2 changes are carried to tree */ |
| 338 | first2 = tree; |
| 339 | } else { |
| 340 | first2 = &first; |
| 341 | } |
| 342 | |
| 343 | /* validate new top-level nodes, autodelete CANNOT occur, all nodes are new */ |
| 344 | LY_CHECK_GOTO(ret = lyd_validate_new(first2, NULL, mod, NULL), cleanup); |
| 345 | |
| 346 | /* add all top-level defaults for this module */ |
| 347 | ret = lyd_new_implicit_r(NULL, first2, NULL, mod, &lydctx->unres_node_type, &lydctx->when_check, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 348 | (validate_options & LYD_VALIDATE_NO_STATE) ? LYD_IMPLICIT_NO_STATE : 0, NULL); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 349 | LY_CHECK_GOTO(ret, cleanup); |
| 350 | |
| 351 | /* finish incompletely validated terminal values/attributes and when conditions */ |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 352 | ret = lyd_validate_unres(tree, &lydctx->when_check, &lydctx->unres_node_type, &lydctx->unres_meta_type, NULL); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 353 | LY_CHECK_GOTO(ret, cleanup); |
| 354 | |
| 355 | /* perform final validation that assumes the data tree is final */ |
| 356 | LY_CHECK_GOTO(ret = lyd_validate_final_r(*first2, NULL, mod, validate_options, 0), cleanup); |
| 357 | } |
| 358 | } |
| 359 | |
| 360 | cleanup: |
Michal Vasko | afac782 | 2020-10-20 14:22:26 +0200 | [diff] [blame] | 361 | lydctx->free(lydctx); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 362 | if (ret) { |
| 363 | lyd_free_all(*tree); |
| 364 | *tree = NULL; |
| 365 | } |
| 366 | return ret; |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 367 | } |
| 368 | |
| 369 | API LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 370 | lyd_parse_data_mem(const struct ly_ctx *ctx, const char *data, LYD_FORMAT format, uint32_t parse_options, uint32_t validate_options, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 371 | struct lyd_node **tree) |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 372 | { |
| 373 | LY_ERR ret; |
| 374 | struct ly_in *in; |
| 375 | |
| 376 | LY_CHECK_RET(ly_in_new_memory(data, &in)); |
| 377 | ret = lyd_parse_data(ctx, in, format, parse_options, validate_options, tree); |
| 378 | |
| 379 | ly_in_free(in, 0); |
| 380 | return ret; |
| 381 | } |
| 382 | |
| 383 | API LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 384 | lyd_parse_data_fd(const struct ly_ctx *ctx, int fd, LYD_FORMAT format, uint32_t parse_options, uint32_t validate_options, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 385 | struct lyd_node **tree) |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 386 | { |
| 387 | LY_ERR ret; |
| 388 | struct ly_in *in; |
| 389 | |
| 390 | LY_CHECK_RET(ly_in_new_fd(fd, &in)); |
| 391 | ret = lyd_parse_data(ctx, in, format, parse_options, validate_options, tree); |
| 392 | |
| 393 | ly_in_free(in, 0); |
| 394 | return ret; |
| 395 | } |
| 396 | |
| 397 | API LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 398 | lyd_parse_data_path(const struct ly_ctx *ctx, const char *path, LYD_FORMAT format, uint32_t parse_options, |
| 399 | uint32_t validate_options, struct lyd_node **tree) |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 400 | { |
| 401 | LY_ERR ret; |
| 402 | struct ly_in *in; |
| 403 | |
| 404 | LY_CHECK_RET(ly_in_new_filepath(path, 0, &in)); |
| 405 | ret = lyd_parse_data(ctx, in, format, parse_options, validate_options, tree); |
| 406 | |
| 407 | ly_in_free(in, 0); |
| 408 | return ret; |
| 409 | } |
| 410 | |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 411 | API LY_ERR |
| 412 | lyd_parse_rpc(const struct ly_ctx *ctx, struct ly_in *in, LYD_FORMAT format, struct lyd_node **tree, struct lyd_node **op) |
| 413 | { |
| 414 | LY_CHECK_ARG_RET(ctx, ctx, in, tree, LY_EINVAL); |
| 415 | |
| 416 | format = lyd_parse_get_format(in, format); |
| 417 | LY_CHECK_ARG_RET(ctx, format, LY_EINVAL); |
| 418 | |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 419 | /* init */ |
| 420 | *tree = NULL; |
| 421 | if (op) { |
| 422 | *op = NULL; |
| 423 | } |
| 424 | |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 425 | /* remember input position */ |
| 426 | in->func_start = in->current; |
| 427 | |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 428 | switch (format) { |
| 429 | case LYD_XML: |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 430 | return lyd_parse_xml_rpc(ctx, in, tree, op); |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 431 | case LYD_JSON: |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 432 | return lyd_parse_json_rpc(ctx, in, tree, op); |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 433 | case LYD_LYB: |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 434 | return lyd_parse_lyb_rpc(ctx, in, tree, op); |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 435 | case LYD_UNKNOWN: |
| 436 | break; |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 437 | } |
| 438 | |
| 439 | LOGINT_RET(ctx); |
| 440 | } |
| 441 | |
| 442 | API LY_ERR |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 443 | lyd_parse_reply(const struct lyd_node *request, struct ly_in *in, LYD_FORMAT format, struct lyd_node **tree, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 444 | struct lyd_node **op) |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 445 | { |
| 446 | LY_CHECK_ARG_RET(NULL, request, LY_EINVAL); |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 447 | LY_CHECK_ARG_RET(LYD_CTX(request), in, tree || op, LY_EINVAL); |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 448 | |
| 449 | format = lyd_parse_get_format(in, format); |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 450 | LY_CHECK_ARG_RET(LYD_CTX(request), format, LY_EINVAL); |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 451 | |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 452 | /* init */ |
| 453 | if (tree) { |
| 454 | *tree = NULL; |
| 455 | } |
| 456 | if (op) { |
| 457 | *op = NULL; |
| 458 | } |
| 459 | |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 460 | /* remember input position */ |
| 461 | in->func_start = in->current; |
| 462 | |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 463 | switch (format) { |
| 464 | case LYD_XML: |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 465 | return lyd_parse_xml_reply(request, in, tree, op); |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 466 | case LYD_JSON: |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 467 | return lyd_parse_json_reply(request, in, tree, op); |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 468 | case LYD_LYB: |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 469 | return lyd_parse_lyb_reply(request, in, tree, op); |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 470 | case LYD_UNKNOWN: |
| 471 | break; |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 472 | } |
| 473 | |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 474 | LOGINT_RET(LYD_CTX(request)); |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 475 | } |
| 476 | |
| 477 | API LY_ERR |
| 478 | lyd_parse_notif(const struct ly_ctx *ctx, struct ly_in *in, LYD_FORMAT format, struct lyd_node **tree, struct lyd_node **ntf) |
| 479 | { |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 480 | LY_CHECK_ARG_RET(ctx, ctx, in, tree || ntf, LY_EINVAL); |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 481 | |
| 482 | format = lyd_parse_get_format(in, format); |
| 483 | LY_CHECK_ARG_RET(ctx, format, LY_EINVAL); |
| 484 | |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 485 | /* init */ |
| 486 | if (tree) { |
| 487 | *tree = NULL; |
| 488 | } |
| 489 | if (ntf) { |
| 490 | *ntf = NULL; |
| 491 | } |
| 492 | |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 493 | /* remember input position */ |
| 494 | in->func_start = in->current; |
| 495 | |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 496 | switch (format) { |
| 497 | case LYD_XML: |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 498 | return lyd_parse_xml_notif(ctx, in, tree, ntf); |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 499 | case LYD_JSON: |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 500 | return lyd_parse_json_notif(ctx, in, tree, ntf); |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 501 | case LYD_LYB: |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 502 | return lyd_parse_lyb_notif(ctx, in, tree, ntf); |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 503 | case LYD_UNKNOWN: |
| 504 | break; |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 505 | } |
| 506 | |
| 507 | LOGINT_RET(ctx); |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 508 | } |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 509 | |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 510 | LY_ERR |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 511 | lyd_create_term(const struct lysc_node *schema, const char *value, size_t value_len, ly_bool *dynamic, |
| 512 | LY_PREFIX_FORMAT format, void *prefix_data, uint32_t hints, ly_bool *incomplete, struct lyd_node **node) |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 513 | { |
| 514 | LY_ERR ret; |
| 515 | struct lyd_node_term *term; |
| 516 | |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 517 | assert(schema->nodetype & LYD_NODE_TERM); |
| 518 | |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 519 | term = calloc(1, sizeof *term); |
| 520 | LY_CHECK_ERR_RET(!term, LOGMEM(schema->module->ctx), LY_EMEM); |
| 521 | |
| 522 | term->schema = schema; |
| 523 | term->prev = (struct lyd_node *)term; |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 524 | term->flags = LYD_NEW; |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 525 | |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 526 | ret = lyd_value_store(schema->module->ctx, &term->value, ((struct lysc_node_leaf *)term->schema)->type, value, |
| 527 | value_len, dynamic, format, prefix_data, hints, schema, incomplete, LY_VLOG_LYSC, schema); |
| 528 | LY_CHECK_ERR_RET(ret, free(term), ret); |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 529 | lyd_hash((struct lyd_node *)term); |
| 530 | |
| 531 | *node = (struct lyd_node *)term; |
| 532 | return ret; |
| 533 | } |
| 534 | |
| 535 | LY_ERR |
| 536 | lyd_create_term2(const struct lysc_node *schema, const struct lyd_value *val, struct lyd_node **node) |
| 537 | { |
| 538 | LY_ERR ret; |
| 539 | struct lyd_node_term *term; |
| 540 | struct lysc_type *type; |
| 541 | |
| 542 | assert(schema->nodetype & LYD_NODE_TERM); |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 543 | assert(val && val->canonical && val->realtype); |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 544 | |
| 545 | term = calloc(1, sizeof *term); |
| 546 | LY_CHECK_ERR_RET(!term, LOGMEM(schema->module->ctx), LY_EMEM); |
| 547 | |
| 548 | term->schema = schema; |
| 549 | term->prev = (struct lyd_node *)term; |
| 550 | term->flags = LYD_NEW; |
| 551 | |
| 552 | type = ((struct lysc_node_leaf *)schema)->type; |
| 553 | ret = type->plugin->duplicate(schema->module->ctx, val, &term->value); |
| 554 | if (ret) { |
| 555 | LOGERR(schema->module->ctx, ret, "Value duplication failed."); |
| 556 | free(term); |
| 557 | return ret; |
| 558 | } |
| 559 | lyd_hash((struct lyd_node *)term); |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 560 | |
| 561 | *node = (struct lyd_node *)term; |
| 562 | return ret; |
| 563 | } |
| 564 | |
| 565 | LY_ERR |
| 566 | lyd_create_inner(const struct lysc_node *schema, struct lyd_node **node) |
| 567 | { |
| 568 | struct lyd_node_inner *in; |
| 569 | |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 570 | assert(schema->nodetype & LYD_NODE_INNER); |
| 571 | |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 572 | in = calloc(1, sizeof *in); |
| 573 | LY_CHECK_ERR_RET(!in, LOGMEM(schema->module->ctx), LY_EMEM); |
| 574 | |
| 575 | in->schema = schema; |
| 576 | in->prev = (struct lyd_node *)in; |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 577 | in->flags = LYD_NEW; |
Michal Vasko | 3383be7 | 2020-11-03 17:15:31 +0100 | [diff] [blame] | 578 | if ((schema->nodetype == LYS_CONTAINER) && !(schema->flags & LYS_PRESENCE)) { |
| 579 | in->flags |= LYD_DEFAULT; |
| 580 | } |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 581 | |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 582 | /* do not hash list with keys, we need them for the hash */ |
| 583 | if ((schema->nodetype != LYS_LIST) || (schema->flags & LYS_KEYLESS)) { |
| 584 | lyd_hash((struct lyd_node *)in); |
| 585 | } |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 586 | |
| 587 | *node = (struct lyd_node *)in; |
| 588 | return LY_SUCCESS; |
| 589 | } |
| 590 | |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 591 | LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 592 | lyd_create_list(const struct lysc_node *schema, const struct ly_path_predicate *predicates, struct lyd_node **node) |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 593 | { |
| 594 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 595 | struct lyd_node *list = NULL, *key; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 596 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 597 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 598 | assert((schema->nodetype == LYS_LIST) && !(schema->flags & LYS_KEYLESS)); |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 599 | |
| 600 | /* create list */ |
| 601 | LY_CHECK_GOTO(ret = lyd_create_inner(schema, &list), cleanup); |
| 602 | |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 603 | /* create and insert all the keys */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 604 | LY_ARRAY_FOR(predicates, u) { |
| 605 | LY_CHECK_GOTO(ret = lyd_create_term2(predicates[u].key, &predicates[u].value, &key), cleanup); |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 606 | lyd_insert_node(list, NULL, key); |
| 607 | } |
| 608 | |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 609 | /* hash having all the keys */ |
| 610 | lyd_hash(list); |
| 611 | |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 612 | /* success */ |
| 613 | *node = list; |
| 614 | list = NULL; |
| 615 | |
| 616 | cleanup: |
| 617 | lyd_free_tree(list); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 618 | return ret; |
| 619 | } |
| 620 | |
| 621 | static LY_ERR |
| 622 | lyd_create_list2(const struct lysc_node *schema, const char *keys, size_t keys_len, struct lyd_node **node) |
| 623 | { |
| 624 | LY_ERR ret = LY_SUCCESS; |
| 625 | struct lyxp_expr *expr = NULL; |
| 626 | uint16_t exp_idx = 0; |
| 627 | enum ly_path_pred_type pred_type = 0; |
| 628 | struct ly_path_predicate *predicates = NULL; |
| 629 | |
| 630 | /* parse keys */ |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 631 | LY_CHECK_GOTO(ret = ly_path_parse_predicate(schema->module->ctx, NULL, keys, keys_len, LY_PATH_PREFIX_OPTIONAL, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 632 | LY_PATH_PRED_KEYS, &expr), cleanup); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 633 | |
| 634 | /* compile them */ |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 635 | LY_CHECK_GOTO(ret = ly_path_compile_predicate(schema->module->ctx, NULL, NULL, schema, expr, &exp_idx, LY_PREF_JSON, |
| 636 | NULL, &predicates, &pred_type), cleanup); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 637 | |
| 638 | /* create the list node */ |
| 639 | LY_CHECK_GOTO(ret = lyd_create_list(schema, predicates, node), cleanup); |
| 640 | |
| 641 | cleanup: |
| 642 | lyxp_expr_free(schema->module->ctx, expr); |
Michal Vasko | f7e16e2 | 2020-10-21 09:24:39 +0200 | [diff] [blame] | 643 | ly_path_predicates_free(schema->module->ctx, pred_type, predicates); |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 644 | return ret; |
| 645 | } |
| 646 | |
| 647 | LY_ERR |
| 648 | lyd_create_any(const struct lysc_node *schema, const void *value, LYD_ANYDATA_VALUETYPE value_type, struct lyd_node **node) |
| 649 | { |
| 650 | struct lyd_node_any *any; |
| 651 | |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 652 | assert(schema->nodetype & LYD_NODE_ANY); |
| 653 | |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 654 | any = calloc(1, sizeof *any); |
| 655 | LY_CHECK_ERR_RET(!any, LOGMEM(schema->module->ctx), LY_EMEM); |
| 656 | |
| 657 | any->schema = schema; |
| 658 | any->prev = (struct lyd_node *)any; |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 659 | any->flags = LYD_NEW; |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 660 | |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 661 | /* TODO: convert XML/JSON strings into a opaq data tree */ |
| 662 | any->value.str = value; |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 663 | any->value_type = value_type; |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 664 | lyd_hash((struct lyd_node *)any); |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 665 | |
| 666 | *node = (struct lyd_node *)any; |
| 667 | return LY_SUCCESS; |
| 668 | } |
| 669 | |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 670 | LY_ERR |
| 671 | lyd_create_opaq(const struct ly_ctx *ctx, const char *name, size_t name_len, const char *value, size_t value_len, |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 672 | ly_bool *dynamic, LYD_FORMAT format, uint32_t hints, struct ly_prefix *val_prefs, const char *prefix, size_t pref_len, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 673 | const char *module_key, size_t module_key_len, struct lyd_node **node) |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 674 | { |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 675 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 676 | struct lyd_node_opaq *opaq; |
| 677 | |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 678 | assert(ctx && name && name_len && format); |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 679 | |
| 680 | if (!value_len) { |
| 681 | value = ""; |
| 682 | } |
| 683 | |
| 684 | opaq = calloc(1, sizeof *opaq); |
Radek Krejci | d46e46a | 2020-09-15 14:22:42 +0200 | [diff] [blame] | 685 | LY_CHECK_ERR_RET(!opaq, LOGMEM(ctx); ly_free_val_prefs(ctx, val_prefs), LY_EMEM); |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 686 | |
| 687 | opaq->prev = (struct lyd_node *)opaq; |
Radek Krejci | d46e46a | 2020-09-15 14:22:42 +0200 | [diff] [blame] | 688 | opaq->val_prefs = val_prefs; |
| 689 | opaq->format = format; |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 690 | LY_CHECK_GOTO(ret = lydict_insert(ctx, name, name_len, &opaq->name), finish); |
| 691 | |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 692 | if (pref_len) { |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 693 | LY_CHECK_GOTO(ret = lydict_insert(ctx, prefix, pref_len, &opaq->prefix.id), finish); |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 694 | } |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 695 | if (module_key_len) { |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 696 | LY_CHECK_GOTO(ret = lydict_insert(ctx, module_key, module_key_len, &opaq->prefix.module_ns), finish); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 697 | } |
| 698 | |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 699 | if (dynamic && *dynamic) { |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 700 | LY_CHECK_GOTO(ret = lydict_insert_zc(ctx, (char *)value, &opaq->value), finish); |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 701 | *dynamic = 0; |
| 702 | } else { |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 703 | LY_CHECK_GOTO(ret = lydict_insert(ctx, value, value_len, &opaq->value), finish); |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 704 | } |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 705 | opaq->hints = hints; |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 706 | opaq->ctx = ctx; |
| 707 | |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 708 | finish: |
| 709 | if (ret) { |
| 710 | lyd_free_tree((struct lyd_node *)opaq); |
| 711 | } else { |
| 712 | *node = (struct lyd_node *)opaq; |
| 713 | } |
| 714 | return ret; |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 715 | } |
| 716 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 717 | API LY_ERR |
Radek Krejci | 41ac994 | 2020-11-02 14:47:56 +0100 | [diff] [blame] | 718 | lyd_new_inner(struct lyd_node *parent, const struct lys_module *module, const char *name, ly_bool output, struct lyd_node **node) |
Michal Vasko | 013a818 | 2020-03-03 10:46:53 +0100 | [diff] [blame] | 719 | { |
| 720 | struct lyd_node *ret = NULL; |
| 721 | const struct lysc_node *schema; |
| 722 | struct ly_ctx *ctx = parent ? parent->schema->module->ctx : (module ? module->ctx : NULL); |
| 723 | |
Michal Vasko | 6027eb9 | 2020-07-15 16:37:30 +0200 | [diff] [blame] | 724 | LY_CHECK_ARG_RET(ctx, parent || module, parent || node, name, LY_EINVAL); |
Michal Vasko | 013a818 | 2020-03-03 10:46:53 +0100 | [diff] [blame] | 725 | |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 726 | if (!module) { |
| 727 | module = parent->schema->module; |
| 728 | } |
| 729 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 730 | schema = lys_find_child(parent ? parent->schema : NULL, module, name, 0, |
Radek Krejci | 41ac994 | 2020-11-02 14:47:56 +0100 | [diff] [blame] | 731 | LYS_CONTAINER | LYS_NOTIF | LYS_RPC | LYS_ACTION, output ? LYS_GETNEXT_OUTPUT : 0); |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 732 | LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "Inner node (and not a list) \"%s\" not found.", name), LY_ENOTFOUND); |
Michal Vasko | 013a818 | 2020-03-03 10:46:53 +0100 | [diff] [blame] | 733 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 734 | LY_CHECK_RET(lyd_create_inner(schema, &ret)); |
| 735 | if (parent) { |
Michal Vasko | 013a818 | 2020-03-03 10:46:53 +0100 | [diff] [blame] | 736 | lyd_insert_node(parent, NULL, ret); |
| 737 | } |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 738 | |
| 739 | if (node) { |
| 740 | *node = ret; |
| 741 | } |
| 742 | return LY_SUCCESS; |
Michal Vasko | 013a818 | 2020-03-03 10:46:53 +0100 | [diff] [blame] | 743 | } |
| 744 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 745 | API LY_ERR |
Radek Krejci | 41ac994 | 2020-11-02 14:47:56 +0100 | [diff] [blame] | 746 | lyd_new_list(struct lyd_node *parent, const struct lys_module *module, const char *name, ly_bool output, struct lyd_node **node, ...) |
Michal Vasko | 013a818 | 2020-03-03 10:46:53 +0100 | [diff] [blame] | 747 | { |
| 748 | struct lyd_node *ret = NULL, *key; |
| 749 | const struct lysc_node *schema, *key_s; |
| 750 | struct ly_ctx *ctx = parent ? parent->schema->module->ctx : (module ? module->ctx : NULL); |
| 751 | va_list ap; |
| 752 | const char *key_val; |
| 753 | LY_ERR rc = LY_SUCCESS; |
| 754 | |
Michal Vasko | 6027eb9 | 2020-07-15 16:37:30 +0200 | [diff] [blame] | 755 | LY_CHECK_ARG_RET(ctx, parent || module, parent || node, name, LY_EINVAL); |
Michal Vasko | 013a818 | 2020-03-03 10:46:53 +0100 | [diff] [blame] | 756 | |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 757 | if (!module) { |
| 758 | module = parent->schema->module; |
| 759 | } |
| 760 | |
Radek Krejci | 41ac994 | 2020-11-02 14:47:56 +0100 | [diff] [blame] | 761 | schema = lys_find_child(parent ? parent->schema : NULL, module, name, 0, LYS_LIST, output ? LYS_GETNEXT_OUTPUT : 0); |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 762 | LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "List node \"%s\" not found.", name), LY_ENOTFOUND); |
Michal Vasko | 013a818 | 2020-03-03 10:46:53 +0100 | [diff] [blame] | 763 | |
| 764 | /* create list inner node */ |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 765 | LY_CHECK_RET(lyd_create_inner(schema, &ret)); |
Michal Vasko | 013a818 | 2020-03-03 10:46:53 +0100 | [diff] [blame] | 766 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 767 | va_start(ap, node); |
Michal Vasko | 013a818 | 2020-03-03 10:46:53 +0100 | [diff] [blame] | 768 | |
| 769 | /* create and insert all the keys */ |
| 770 | for (key_s = lysc_node_children(schema, 0); key_s && (key_s->flags & LYS_KEY); key_s = key_s->next) { |
| 771 | key_val = va_arg(ap, const char *); |
| 772 | |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 773 | rc = lyd_create_term(key_s, key_val, key_val ? strlen(key_val) : 0, NULL, LY_PREF_JSON, NULL, LYD_HINT_DATA, |
| 774 | NULL, &key); |
| 775 | LY_CHECK_GOTO(rc, cleanup); |
Michal Vasko | 013a818 | 2020-03-03 10:46:53 +0100 | [diff] [blame] | 776 | lyd_insert_node(ret, NULL, key); |
| 777 | } |
| 778 | |
Michal Vasko | 013a818 | 2020-03-03 10:46:53 +0100 | [diff] [blame] | 779 | if (parent) { |
| 780 | lyd_insert_node(parent, NULL, ret); |
| 781 | } |
| 782 | |
| 783 | cleanup: |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 784 | va_end(ap); |
Michal Vasko | 013a818 | 2020-03-03 10:46:53 +0100 | [diff] [blame] | 785 | if (rc) { |
| 786 | lyd_free_tree(ret); |
| 787 | ret = NULL; |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 788 | } else if (node) { |
| 789 | *node = ret; |
Michal Vasko | 013a818 | 2020-03-03 10:46:53 +0100 | [diff] [blame] | 790 | } |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 791 | return rc; |
Michal Vasko | 013a818 | 2020-03-03 10:46:53 +0100 | [diff] [blame] | 792 | } |
| 793 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 794 | API LY_ERR |
| 795 | lyd_new_list2(struct lyd_node *parent, const struct lys_module *module, const char *name, const char *keys, |
Radek Krejci | 41ac994 | 2020-11-02 14:47:56 +0100 | [diff] [blame] | 796 | ly_bool output, struct lyd_node **node) |
Michal Vasko | 013a818 | 2020-03-03 10:46:53 +0100 | [diff] [blame] | 797 | { |
| 798 | struct lyd_node *ret = NULL; |
| 799 | const struct lysc_node *schema; |
| 800 | struct ly_ctx *ctx = parent ? parent->schema->module->ctx : (module ? module->ctx : NULL); |
| 801 | |
Michal Vasko | 6027eb9 | 2020-07-15 16:37:30 +0200 | [diff] [blame] | 802 | LY_CHECK_ARG_RET(ctx, parent || module, parent || node, name, LY_EINVAL); |
Michal Vasko | 013a818 | 2020-03-03 10:46:53 +0100 | [diff] [blame] | 803 | |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 804 | if (!module) { |
| 805 | module = parent->schema->module; |
| 806 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 807 | if (!keys) { |
| 808 | keys = ""; |
| 809 | } |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 810 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 811 | /* find schema node */ |
Radek Krejci | 41ac994 | 2020-11-02 14:47:56 +0100 | [diff] [blame] | 812 | schema = lys_find_child(parent ? parent->schema : NULL, module, name, 0, LYS_LIST, output ? LYS_GETNEXT_OUTPUT : 0); |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 813 | LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "List node \"%s\" not found.", name), LY_ENOTFOUND); |
Michal Vasko | 013a818 | 2020-03-03 10:46:53 +0100 | [diff] [blame] | 814 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 815 | if ((schema->flags & LYS_KEYLESS) && !keys[0]) { |
| 816 | /* key-less list */ |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 817 | LY_CHECK_RET(lyd_create_inner(schema, &ret)); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 818 | } else { |
| 819 | /* create the list node */ |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 820 | LY_CHECK_RET(lyd_create_list2(schema, keys, strlen(keys), &ret)); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 821 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 822 | if (parent) { |
Michal Vasko | 013a818 | 2020-03-03 10:46:53 +0100 | [diff] [blame] | 823 | lyd_insert_node(parent, NULL, ret); |
| 824 | } |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 825 | |
| 826 | if (node) { |
| 827 | *node = ret; |
| 828 | } |
| 829 | return LY_SUCCESS; |
Michal Vasko | 013a818 | 2020-03-03 10:46:53 +0100 | [diff] [blame] | 830 | } |
| 831 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 832 | API LY_ERR |
| 833 | lyd_new_term(struct lyd_node *parent, const struct lys_module *module, const char *name, const char *val_str, |
Radek Krejci | 41ac994 | 2020-11-02 14:47:56 +0100 | [diff] [blame] | 834 | ly_bool output, struct lyd_node **node) |
Michal Vasko | 013a818 | 2020-03-03 10:46:53 +0100 | [diff] [blame] | 835 | { |
Michal Vasko | cbff3e9 | 2020-05-27 12:56:41 +0200 | [diff] [blame] | 836 | LY_ERR rc; |
Michal Vasko | 013a818 | 2020-03-03 10:46:53 +0100 | [diff] [blame] | 837 | struct lyd_node *ret = NULL; |
| 838 | const struct lysc_node *schema; |
| 839 | struct ly_ctx *ctx = parent ? parent->schema->module->ctx : (module ? module->ctx : NULL); |
| 840 | |
Michal Vasko | 6027eb9 | 2020-07-15 16:37:30 +0200 | [diff] [blame] | 841 | LY_CHECK_ARG_RET(ctx, parent || module, parent || node, name, LY_EINVAL); |
Michal Vasko | 013a818 | 2020-03-03 10:46:53 +0100 | [diff] [blame] | 842 | |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 843 | if (!module) { |
| 844 | module = parent->schema->module; |
| 845 | } |
| 846 | |
Radek Krejci | 41ac994 | 2020-11-02 14:47:56 +0100 | [diff] [blame] | 847 | schema = lys_find_child(parent ? parent->schema : NULL, module, name, 0, LYD_NODE_TERM, output ? LYS_GETNEXT_OUTPUT : 0); |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 848 | LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "Term node \"%s\" not found.", name), LY_ENOTFOUND); |
Michal Vasko | 013a818 | 2020-03-03 10:46:53 +0100 | [diff] [blame] | 849 | |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 850 | rc = lyd_create_term(schema, val_str, val_str ? strlen(val_str) : 0, NULL, LY_PREF_JSON, NULL, LYD_HINT_DATA, NULL, |
| 851 | &ret); |
| 852 | LY_CHECK_RET(rc); |
Michal Vasko | cbff3e9 | 2020-05-27 12:56:41 +0200 | [diff] [blame] | 853 | if (parent) { |
Michal Vasko | 013a818 | 2020-03-03 10:46:53 +0100 | [diff] [blame] | 854 | lyd_insert_node(parent, NULL, ret); |
| 855 | } |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 856 | |
| 857 | if (node) { |
| 858 | *node = ret; |
| 859 | } |
| 860 | return LY_SUCCESS; |
Michal Vasko | 013a818 | 2020-03-03 10:46:53 +0100 | [diff] [blame] | 861 | } |
| 862 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 863 | API LY_ERR |
Michal Vasko | 013a818 | 2020-03-03 10:46:53 +0100 | [diff] [blame] | 864 | lyd_new_any(struct lyd_node *parent, const struct lys_module *module, const char *name, const void *value, |
Radek Krejci | 41ac994 | 2020-11-02 14:47:56 +0100 | [diff] [blame] | 865 | LYD_ANYDATA_VALUETYPE value_type, ly_bool output, struct lyd_node **node) |
Michal Vasko | 013a818 | 2020-03-03 10:46:53 +0100 | [diff] [blame] | 866 | { |
| 867 | struct lyd_node *ret = NULL; |
| 868 | const struct lysc_node *schema; |
| 869 | struct ly_ctx *ctx = parent ? parent->schema->module->ctx : (module ? module->ctx : NULL); |
| 870 | |
Michal Vasko | 6027eb9 | 2020-07-15 16:37:30 +0200 | [diff] [blame] | 871 | LY_CHECK_ARG_RET(ctx, parent || module, parent || node, name, LY_EINVAL); |
Michal Vasko | 013a818 | 2020-03-03 10:46:53 +0100 | [diff] [blame] | 872 | |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 873 | if (!module) { |
| 874 | module = parent->schema->module; |
| 875 | } |
| 876 | |
Radek Krejci | 41ac994 | 2020-11-02 14:47:56 +0100 | [diff] [blame] | 877 | schema = lys_find_child(parent ? parent->schema : NULL, module, name, 0, LYD_NODE_ANY, output ? LYS_GETNEXT_OUTPUT : 0); |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 878 | LY_CHECK_ERR_RET(!schema, LOGERR(ctx, LY_EINVAL, "Any node \"%s\" not found.", name), LY_ENOTFOUND); |
Michal Vasko | 013a818 | 2020-03-03 10:46:53 +0100 | [diff] [blame] | 879 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 880 | LY_CHECK_RET(lyd_create_any(schema, value, value_type, &ret)); |
| 881 | if (parent) { |
Michal Vasko | 013a818 | 2020-03-03 10:46:53 +0100 | [diff] [blame] | 882 | lyd_insert_node(parent, NULL, ret); |
| 883 | } |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 884 | |
| 885 | if (node) { |
| 886 | *node = ret; |
| 887 | } |
| 888 | return LY_SUCCESS; |
Michal Vasko | 013a818 | 2020-03-03 10:46:53 +0100 | [diff] [blame] | 889 | } |
| 890 | |
Michal Vasko | 4490d31 | 2020-06-16 13:08:55 +0200 | [diff] [blame] | 891 | /** |
| 892 | * @brief Update node value. |
| 893 | * |
| 894 | * @param[in] node Node to update. |
| 895 | * @param[in] value New value to set. |
| 896 | * @param[in] value_type Type of @p value for any node. |
| 897 | * @param[out] new_parent Set to @p node if the value was updated, otherwise set to NULL. |
| 898 | * @param[out] new_node Set to @p node if the value was updated, otherwise set to NULL. |
| 899 | * @return LY_ERR value. |
| 900 | */ |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 901 | static LY_ERR |
| 902 | lyd_new_path_update(struct lyd_node *node, const void *value, LYD_ANYDATA_VALUETYPE value_type, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 903 | struct lyd_node **new_parent, struct lyd_node **new_node) |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 904 | { |
| 905 | LY_ERR ret = LY_SUCCESS; |
| 906 | struct lyd_node *new_any; |
| 907 | |
| 908 | switch (node->schema->nodetype) { |
| 909 | case LYS_CONTAINER: |
| 910 | case LYS_NOTIF: |
| 911 | case LYS_RPC: |
| 912 | case LYS_ACTION: |
| 913 | case LYS_LIST: |
| 914 | case LYS_LEAFLIST: |
| 915 | /* if it exists, there is nothing to update */ |
| 916 | *new_parent = NULL; |
| 917 | *new_node = NULL; |
| 918 | break; |
| 919 | case LYS_LEAF: |
| 920 | ret = lyd_change_term(node, value); |
| 921 | if ((ret == LY_SUCCESS) || (ret == LY_EEXIST)) { |
| 922 | /* there was an actual change (at least of the default flag) */ |
| 923 | *new_parent = node; |
| 924 | *new_node = node; |
| 925 | ret = LY_SUCCESS; |
| 926 | } else if (ret == LY_ENOT) { |
| 927 | /* no change */ |
| 928 | *new_parent = NULL; |
| 929 | *new_node = NULL; |
| 930 | ret = LY_SUCCESS; |
| 931 | } /* else error */ |
| 932 | break; |
| 933 | case LYS_ANYDATA: |
| 934 | case LYS_ANYXML: |
| 935 | /* create a new any node */ |
| 936 | LY_CHECK_RET(lyd_create_any(node->schema, value, value_type, &new_any)); |
| 937 | |
| 938 | /* compare with the existing one */ |
Michal Vasko | 8f359bf | 2020-07-28 10:41:15 +0200 | [diff] [blame] | 939 | if (lyd_compare_single(node, new_any, 0)) { |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 940 | /* not equal, switch values (so that we can use generic node free) */ |
| 941 | ((struct lyd_node_any *)new_any)->value = ((struct lyd_node_any *)node)->value; |
| 942 | ((struct lyd_node_any *)new_any)->value_type = ((struct lyd_node_any *)node)->value_type; |
| 943 | ((struct lyd_node_any *)node)->value.str = value; |
| 944 | ((struct lyd_node_any *)node)->value_type = value_type; |
| 945 | |
| 946 | *new_parent = node; |
| 947 | *new_node = node; |
| 948 | } else { |
| 949 | /* they are equal */ |
| 950 | *new_parent = NULL; |
| 951 | *new_node = NULL; |
| 952 | } |
| 953 | lyd_free_tree(new_any); |
| 954 | break; |
| 955 | default: |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 956 | LOGINT(LYD_CTX(node)); |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 957 | ret = LY_EINT; |
| 958 | break; |
| 959 | } |
| 960 | |
| 961 | return ret; |
| 962 | } |
| 963 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 964 | API LY_ERR |
| 965 | lyd_new_meta(struct lyd_node *parent, const struct lys_module *module, const char *name, const char *val_str, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 966 | struct lyd_meta **meta) |
Michal Vasko | d86997b | 2020-05-26 15:19:54 +0200 | [diff] [blame] | 967 | { |
| 968 | struct lyd_meta *ret = NULL; |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 969 | const struct ly_ctx *ctx; |
Michal Vasko | d86997b | 2020-05-26 15:19:54 +0200 | [diff] [blame] | 970 | const char *prefix, *tmp; |
Michal Vasko | d86997b | 2020-05-26 15:19:54 +0200 | [diff] [blame] | 971 | size_t pref_len, name_len; |
| 972 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 973 | LY_CHECK_ARG_RET(NULL, parent, name, module || strchr(name, ':'), LY_EINVAL); |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 974 | |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 975 | ctx = LYD_CTX(parent); |
Michal Vasko | d86997b | 2020-05-26 15:19:54 +0200 | [diff] [blame] | 976 | |
| 977 | /* parse the name */ |
| 978 | tmp = name; |
| 979 | if (ly_parse_nodeid(&tmp, &prefix, &pref_len, &name, &name_len) || tmp[0]) { |
| 980 | LOGERR(ctx, LY_EINVAL, "Metadata name \"%s\" is not valid.", name); |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 981 | return LY_EVALID; |
Michal Vasko | d86997b | 2020-05-26 15:19:54 +0200 | [diff] [blame] | 982 | } |
| 983 | |
| 984 | /* find the module */ |
| 985 | if (prefix) { |
Radek Krejci | 0ad51f1 | 2020-07-16 12:08:12 +0200 | [diff] [blame] | 986 | module = ly_ctx_get_module_implemented2(ctx, prefix, pref_len); |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 987 | LY_CHECK_ERR_RET(!module, LOGERR(ctx, LY_EINVAL, "Module \"%.*s\" not found.", pref_len, prefix), LY_ENOTFOUND); |
Michal Vasko | d86997b | 2020-05-26 15:19:54 +0200 | [diff] [blame] | 988 | } |
| 989 | |
| 990 | /* set value if none */ |
| 991 | if (!val_str) { |
| 992 | val_str = ""; |
| 993 | } |
| 994 | |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 995 | LY_CHECK_RET(lyd_create_meta(parent, &ret, module, name, name_len, val_str, strlen(val_str), NULL, LY_PREF_JSON, |
| 996 | NULL, LYD_HINT_DATA, NULL)); |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 997 | |
| 998 | if (meta) { |
| 999 | *meta = ret; |
| 1000 | } |
| 1001 | return LY_SUCCESS; |
Michal Vasko | d86997b | 2020-05-26 15:19:54 +0200 | [diff] [blame] | 1002 | } |
| 1003 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1004 | API LY_ERR |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 1005 | lyd_new_opaq(struct lyd_node *parent, const struct ly_ctx *ctx, const char *name, const char *value, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 1006 | const char *module_name, struct lyd_node **node) |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 1007 | { |
| 1008 | struct lyd_node *ret = NULL; |
| 1009 | |
Michal Vasko | 6027eb9 | 2020-07-15 16:37:30 +0200 | [diff] [blame] | 1010 | LY_CHECK_ARG_RET(ctx, parent || ctx, parent || node, name, module_name, LY_EINVAL); |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 1011 | |
| 1012 | if (!ctx) { |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 1013 | ctx = LYD_CTX(parent); |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 1014 | } |
| 1015 | if (!value) { |
| 1016 | value = ""; |
| 1017 | } |
| 1018 | |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 1019 | LY_CHECK_RET(lyd_create_opaq(ctx, name, strlen(name), value, strlen(value), NULL, LYD_JSON, 0, NULL, NULL, 0, |
| 1020 | module_name, strlen(module_name), &ret)); |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1021 | if (parent) { |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 1022 | lyd_insert_node(parent, NULL, ret); |
| 1023 | } |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1024 | |
| 1025 | if (node) { |
| 1026 | *node = ret; |
| 1027 | } |
| 1028 | return LY_SUCCESS; |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 1029 | } |
| 1030 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1031 | API LY_ERR |
| 1032 | lyd_new_attr(struct lyd_node *parent, const char *module_name, const char *name, const char *val_str, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 1033 | struct lyd_attr **attr) |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 1034 | { |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1035 | struct lyd_attr *ret = NULL; |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 1036 | const struct ly_ctx *ctx; |
| 1037 | const char *prefix, *tmp; |
| 1038 | size_t pref_len, name_len; |
| 1039 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1040 | LY_CHECK_ARG_RET(NULL, parent, !parent->schema, name, LY_EINVAL); |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 1041 | |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 1042 | ctx = LYD_CTX(parent); |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 1043 | |
| 1044 | /* parse the name */ |
| 1045 | tmp = name; |
| 1046 | if (ly_parse_nodeid(&tmp, &prefix, &pref_len, &name, &name_len) || tmp[0]) { |
| 1047 | LOGERR(ctx, LY_EINVAL, "Metadata name \"%s\" is not valid.", name); |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1048 | return LY_EVALID; |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 1049 | } |
| 1050 | |
| 1051 | /* set value if none */ |
| 1052 | if (!val_str) { |
| 1053 | val_str = ""; |
| 1054 | } |
| 1055 | |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 1056 | LY_CHECK_RET(lyd_create_attr(parent, &ret, ctx, name, name_len, val_str, strlen(val_str), NULL, LYD_JSON, 0, NULL, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 1057 | prefix, pref_len, module_name, module_name ? strlen(module_name) : 0)); |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1058 | |
| 1059 | if (attr) { |
| 1060 | *attr = ret; |
| 1061 | } |
| 1062 | return LY_SUCCESS; |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 1063 | } |
| 1064 | |
| 1065 | API LY_ERR |
| 1066 | lyd_change_term(struct lyd_node *term, const char *val_str) |
| 1067 | { |
| 1068 | LY_ERR ret = LY_SUCCESS; |
| 1069 | struct lysc_type *type; |
| 1070 | struct lyd_node_term *t; |
| 1071 | struct lyd_node *parent; |
| 1072 | struct lyd_value val = {0}; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 1073 | ly_bool dflt_change, val_change; |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 1074 | |
| 1075 | LY_CHECK_ARG_RET(NULL, term, term->schema, term->schema->nodetype & LYD_NODE_TERM, LY_EINVAL); |
| 1076 | |
| 1077 | if (!val_str) { |
| 1078 | val_str = ""; |
| 1079 | } |
| 1080 | t = (struct lyd_node_term *)term; |
| 1081 | type = ((struct lysc_node_leaf *)term->schema)->type; |
| 1082 | |
| 1083 | /* parse the new value */ |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 1084 | ret = lyd_value_store(LYD_CTX(term), &val, type, val_str, strlen(val_str), NULL, LY_PREF_JSON, NULL, LYD_HINT_DATA, |
| 1085 | term->schema, NULL, LY_VLOG_LYD, term); |
| 1086 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 1087 | |
| 1088 | /* compare original and new value */ |
| 1089 | if (type->plugin->compare(&t->value, &val)) { |
| 1090 | /* values differ, switch them */ |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 1091 | type->plugin->free(LYD_CTX(term), &t->value); |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 1092 | t->value = val; |
| 1093 | memset(&val, 0, sizeof val); |
| 1094 | val_change = 1; |
| 1095 | } else { |
| 1096 | val_change = 0; |
| 1097 | } |
| 1098 | |
| 1099 | /* always clear the default flag */ |
| 1100 | if (term->flags & LYD_DEFAULT) { |
| 1101 | for (parent = term; parent; parent = (struct lyd_node *)parent->parent) { |
| 1102 | parent->flags &= ~LYD_DEFAULT; |
| 1103 | } |
| 1104 | dflt_change = 1; |
| 1105 | } else { |
| 1106 | dflt_change = 0; |
| 1107 | } |
| 1108 | |
| 1109 | if (val_change || dflt_change) { |
| 1110 | /* make the node non-validated */ |
| 1111 | term->flags &= LYD_NEW; |
| 1112 | } |
| 1113 | |
| 1114 | if (val_change) { |
| 1115 | if (term->schema->nodetype == LYS_LEAFLIST) { |
| 1116 | /* leaf-list needs to be hashed again and re-inserted into parent */ |
| 1117 | lyd_unlink_hash(term); |
| 1118 | lyd_hash(term); |
| 1119 | LY_CHECK_GOTO(ret = lyd_insert_hash(term), cleanup); |
| 1120 | } else if ((term->schema->flags & LYS_KEY) && term->parent) { |
| 1121 | /* list needs to be updated if its key was changed */ |
| 1122 | assert(term->parent->schema->nodetype == LYS_LIST); |
| 1123 | lyd_unlink_hash((struct lyd_node *)term->parent); |
| 1124 | lyd_hash((struct lyd_node *)term->parent); |
| 1125 | LY_CHECK_GOTO(ret = lyd_insert_hash((struct lyd_node *)term->parent), cleanup); |
| 1126 | } /* else leaf that is not a key, its value is not used for its hash so it does not change */ |
| 1127 | } |
| 1128 | |
| 1129 | /* retrun value */ |
| 1130 | if (!val_change) { |
| 1131 | if (dflt_change) { |
| 1132 | /* only default flag change */ |
| 1133 | ret = LY_EEXIST; |
| 1134 | } else { |
| 1135 | /* no change */ |
| 1136 | ret = LY_ENOT; |
| 1137 | } |
| 1138 | } /* else value changed, LY_SUCCESS */ |
| 1139 | |
| 1140 | cleanup: |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 1141 | type->plugin->free(LYD_CTX(term), &val); |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 1142 | return ret; |
| 1143 | } |
| 1144 | |
Michal Vasko | 4158635 | 2020-07-13 13:54:25 +0200 | [diff] [blame] | 1145 | API LY_ERR |
| 1146 | lyd_change_meta(struct lyd_meta *meta, const char *val_str) |
| 1147 | { |
| 1148 | LY_ERR ret = LY_SUCCESS; |
| 1149 | struct lyd_meta *m2; |
| 1150 | struct lyd_value val; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 1151 | ly_bool val_change; |
Michal Vasko | 4158635 | 2020-07-13 13:54:25 +0200 | [diff] [blame] | 1152 | |
| 1153 | LY_CHECK_ARG_RET(NULL, meta, LY_EINVAL); |
| 1154 | |
| 1155 | if (!val_str) { |
| 1156 | val_str = ""; |
| 1157 | } |
| 1158 | |
| 1159 | /* parse the new value into a new meta structure */ |
| 1160 | LY_CHECK_GOTO(ret = lyd_create_meta(NULL, &m2, meta->annotation->module, meta->name, strlen(meta->name), val_str, |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 1161 | strlen(val_str), NULL, LY_PREF_JSON, NULL, LYD_HINT_DATA, NULL), cleanup); |
Michal Vasko | 4158635 | 2020-07-13 13:54:25 +0200 | [diff] [blame] | 1162 | |
| 1163 | /* compare original and new value */ |
| 1164 | if (lyd_compare_meta(meta, m2)) { |
| 1165 | /* values differ, switch them */ |
| 1166 | val = meta->value; |
| 1167 | meta->value = m2->value; |
| 1168 | m2->value = val; |
| 1169 | val_change = 1; |
| 1170 | } else { |
| 1171 | val_change = 0; |
| 1172 | } |
| 1173 | |
| 1174 | /* retrun value */ |
| 1175 | if (!val_change) { |
| 1176 | /* no change */ |
| 1177 | ret = LY_ENOT; |
| 1178 | } /* else value changed, LY_SUCCESS */ |
| 1179 | |
| 1180 | cleanup: |
| 1181 | return ret; |
| 1182 | } |
| 1183 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1184 | API LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 1185 | lyd_new_path(struct lyd_node *parent, const struct ly_ctx *ctx, const char *path, const char *value, uint32_t options, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 1186 | struct lyd_node **node) |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 1187 | { |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1188 | return lyd_new_path2(parent, ctx, path, value, 0, options, node, NULL); |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 1189 | } |
| 1190 | |
| 1191 | API LY_ERR |
| 1192 | lyd_new_path2(struct lyd_node *parent, const struct ly_ctx *ctx, const char *path, const void *value, |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 1193 | LYD_ANYDATA_VALUETYPE value_type, uint32_t options, struct lyd_node **new_parent, struct lyd_node **new_node) |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 1194 | { |
| 1195 | LY_ERR ret = LY_SUCCESS, r; |
| 1196 | struct lyxp_expr *exp = NULL; |
| 1197 | struct ly_path *p = NULL; |
| 1198 | struct lyd_node *nparent = NULL, *nnode = NULL, *node = NULL, *cur_parent; |
| 1199 | const struct lysc_node *schema; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1200 | LY_ARRAY_COUNT_TYPE path_idx = 0; |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 1201 | struct ly_path_predicate *pred; |
| 1202 | |
| 1203 | LY_CHECK_ARG_RET(ctx, parent || ctx, path, (path[0] == '/') || parent, LY_EINVAL); |
| 1204 | |
| 1205 | if (!ctx) { |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 1206 | ctx = LYD_CTX(parent); |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 1207 | } |
| 1208 | |
| 1209 | /* parse path */ |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 1210 | LY_CHECK_GOTO(ret = ly_path_parse(ctx, NULL, path, strlen(path), LY_PATH_BEGIN_EITHER, LY_PATH_LREF_FALSE, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 1211 | LY_PATH_PREFIX_OPTIONAL, LY_PATH_PRED_SIMPLE, &exp), cleanup); |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 1212 | |
| 1213 | /* compile path */ |
| 1214 | LY_CHECK_GOTO(ret = ly_path_compile(ctx, NULL, parent ? parent->schema : NULL, exp, LY_PATH_LREF_FALSE, |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 1215 | options & LYD_NEW_PATH_OUTPUT ? LY_PATH_OPER_OUTPUT : LY_PATH_OPER_INPUT, LY_PATH_TARGET_MANY, LY_PREF_JSON, |
| 1216 | NULL, &p), cleanup); |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 1217 | |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1218 | schema = p[LY_ARRAY_COUNT(p) - 1].node; |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 1219 | if ((schema->nodetype == LYS_LIST) && (p[LY_ARRAY_COUNT(p) - 1].pred_type == LY_PATH_PREDTYPE_NONE) && |
Radek Krejci | 092e33c | 2020-10-12 15:33:10 +0200 | [diff] [blame] | 1220 | !(options & LYD_NEW_PATH_OPAQ)) { |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 1221 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_XPATH, "Predicate missing for %s \"%s\" in path.", |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 1222 | lys_nodetype2str(schema->nodetype), schema->name); |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 1223 | ret = LY_EINVAL; |
| 1224 | goto cleanup; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1225 | } else if ((schema->nodetype == LYS_LEAFLIST) && (p[LY_ARRAY_COUNT(p) - 1].pred_type == LY_PATH_PREDTYPE_NONE)) { |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 1226 | /* parse leafref value into a predicate, if not defined in the path */ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1227 | p[LY_ARRAY_COUNT(p) - 1].pred_type = LY_PATH_PREDTYPE_LEAFLIST; |
| 1228 | LY_ARRAY_NEW_GOTO(ctx, p[LY_ARRAY_COUNT(p) - 1].predicates, pred, ret, cleanup); |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 1229 | |
| 1230 | if (!value) { |
| 1231 | value = ""; |
| 1232 | } |
| 1233 | |
| 1234 | r = LY_SUCCESS; |
Radek Krejci | 092e33c | 2020-10-12 15:33:10 +0200 | [diff] [blame] | 1235 | if (options & LYD_NEW_PATH_OPAQ) { |
Michal Vasko | f937cfe | 2020-08-03 16:07:12 +0200 | [diff] [blame] | 1236 | r = lys_value_validate(NULL, schema, value, strlen(value)); |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 1237 | } |
| 1238 | if (!r) { |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 1239 | ret = lyd_value_store(ctx, &pred->value, ((struct lysc_node_leaflist *)schema)->type, value, strlen(value), |
| 1240 | NULL, LY_PREF_JSON, NULL, LYD_HINT_DATA, schema, NULL, LY_VLOG_LYSC, schema); |
| 1241 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | ae87566 | 2020-10-21 10:33:17 +0200 | [diff] [blame] | 1242 | ++((struct lysc_type *)pred->value.realtype)->refcount; |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 1243 | } /* else we have opaq flag and the value is not valid, leavne no predicate and then create an opaque node */ |
| 1244 | } |
| 1245 | |
| 1246 | /* try to find any existing nodes in the path */ |
| 1247 | if (parent) { |
| 1248 | ret = ly_path_eval_partial(p, parent, &path_idx, &node); |
| 1249 | if (ret == LY_SUCCESS) { |
| 1250 | /* the node exists, are we supposed to update it or is it just a default? */ |
Radek Krejci | 092e33c | 2020-10-12 15:33:10 +0200 | [diff] [blame] | 1251 | if (!(options & LYD_NEW_PATH_UPDATE) && !(node->flags & LYD_DEFAULT)) { |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 1252 | LOGERR(ctx, LY_EEXIST, "Path \"%s\" already exists", path); |
| 1253 | ret = LY_EEXIST; |
| 1254 | goto cleanup; |
| 1255 | } |
| 1256 | |
| 1257 | /* update the existing node */ |
| 1258 | ret = lyd_new_path_update(node, value, value_type, &nparent, &nnode); |
| 1259 | goto cleanup; |
| 1260 | } else if (ret == LY_EINCOMPLETE) { |
| 1261 | /* some nodes were found, adjust the iterator to the next segment */ |
| 1262 | ++path_idx; |
| 1263 | } else if (ret == LY_ENOTFOUND) { |
| 1264 | /* we will create the nodes from top-level, default behavior (absolute path), or from the parent (relative path) */ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1265 | if (lysc_data_parent(p[LY_ARRAY_COUNT(p) - 1].node)) { |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 1266 | node = parent; |
| 1267 | } |
| 1268 | } else { |
| 1269 | /* error */ |
| 1270 | goto cleanup; |
| 1271 | } |
| 1272 | } |
| 1273 | |
| 1274 | /* create all the non-existing nodes in a loop */ |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 1275 | for ( ; path_idx < LY_ARRAY_COUNT(p); ++path_idx) { |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 1276 | cur_parent = node; |
| 1277 | schema = p[path_idx].node; |
| 1278 | |
| 1279 | switch (schema->nodetype) { |
| 1280 | case LYS_LIST: |
| 1281 | if (!(schema->flags & LYS_KEYLESS)) { |
Radek Krejci | 092e33c | 2020-10-12 15:33:10 +0200 | [diff] [blame] | 1282 | if ((options & LYD_NEW_PATH_OPAQ) && (p[path_idx].pred_type == LY_PATH_PREDTYPE_NONE)) { |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 1283 | /* creating opaque list without keys */ |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 1284 | LY_CHECK_GOTO(ret = lyd_create_opaq(ctx, schema->name, strlen(schema->name), NULL, 0, NULL, LYD_JSON, |
| 1285 | LYD_NODEHINT_LIST, NULL, NULL, 0, schema->module->name, strlen(schema->module->name), &node), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 1286 | cleanup); |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 1287 | } else { |
| 1288 | assert(p[path_idx].pred_type == LY_PATH_PREDTYPE_LIST); |
| 1289 | LY_CHECK_GOTO(ret = lyd_create_list(schema, p[path_idx].predicates, &node), cleanup); |
| 1290 | } |
| 1291 | break; |
| 1292 | } |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 1293 | /* fallthrough */ |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 1294 | case LYS_CONTAINER: |
| 1295 | case LYS_NOTIF: |
| 1296 | case LYS_RPC: |
| 1297 | case LYS_ACTION: |
| 1298 | LY_CHECK_GOTO(ret = lyd_create_inner(schema, &node), cleanup); |
| 1299 | break; |
| 1300 | case LYS_LEAFLIST: |
Radek Krejci | 092e33c | 2020-10-12 15:33:10 +0200 | [diff] [blame] | 1301 | if ((options & LYD_NEW_PATH_OPAQ) && (p[path_idx].pred_type == LY_PATH_PREDTYPE_NONE)) { |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 1302 | /* creating opaque leaf-list without value */ |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 1303 | LY_CHECK_GOTO(ret = lyd_create_opaq(ctx, schema->name, strlen(schema->name), NULL, 0, NULL, LYD_JSON, |
| 1304 | LYD_NODEHINT_LEAFLIST, NULL, NULL, 0, schema->module->name, strlen(schema->module->name), &node), |
| 1305 | cleanup); |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 1306 | } else { |
| 1307 | assert(p[path_idx].pred_type == LY_PATH_PREDTYPE_LEAFLIST); |
| 1308 | LY_CHECK_GOTO(ret = lyd_create_term2(schema, &p[path_idx].predicates[0].value, &node), cleanup); |
| 1309 | } |
| 1310 | break; |
| 1311 | case LYS_LEAF: |
| 1312 | /* make there is some value */ |
| 1313 | if (!value) { |
| 1314 | value = ""; |
| 1315 | } |
| 1316 | |
| 1317 | r = LY_SUCCESS; |
Radek Krejci | 092e33c | 2020-10-12 15:33:10 +0200 | [diff] [blame] | 1318 | if (options & LYD_NEW_PATH_OPAQ) { |
Michal Vasko | f937cfe | 2020-08-03 16:07:12 +0200 | [diff] [blame] | 1319 | r = lys_value_validate(NULL, schema, value, strlen(value)); |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 1320 | } |
| 1321 | if (!r) { |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 1322 | ret = lyd_create_term(schema, value, strlen(value), NULL, LY_PREF_JSON, NULL, LYD_HINT_DATA, NULL, &node); |
| 1323 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 1324 | } else { |
| 1325 | /* creating opaque leaf without value */ |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 1326 | LY_CHECK_GOTO(ret = lyd_create_opaq(ctx, schema->name, strlen(schema->name), NULL, 0, NULL, LYD_JSON, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 1327 | 0, NULL, NULL, 0, schema->module->name, |
| 1328 | strlen(schema->module->name), &node), cleanup); |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 1329 | } |
| 1330 | break; |
| 1331 | case LYS_ANYDATA: |
| 1332 | case LYS_ANYXML: |
| 1333 | LY_CHECK_GOTO(ret = lyd_create_any(schema, value, value_type, &node), cleanup); |
| 1334 | break; |
| 1335 | default: |
| 1336 | LOGINT(ctx); |
| 1337 | ret = LY_EINT; |
| 1338 | goto cleanup; |
| 1339 | } |
| 1340 | |
| 1341 | if (cur_parent) { |
| 1342 | /* connect to the parent */ |
| 1343 | lyd_insert_node(cur_parent, NULL, node); |
| 1344 | } else if (parent) { |
| 1345 | /* connect to top-level siblings */ |
| 1346 | lyd_insert_node(NULL, &parent, node); |
| 1347 | } |
| 1348 | |
| 1349 | /* update remembered nodes */ |
| 1350 | if (!nparent) { |
| 1351 | nparent = node; |
| 1352 | } |
| 1353 | nnode = node; |
| 1354 | } |
| 1355 | |
| 1356 | cleanup: |
| 1357 | lyxp_expr_free(ctx, exp); |
| 1358 | ly_path_free(ctx, p); |
| 1359 | if (!ret) { |
| 1360 | /* set out params only on success */ |
| 1361 | if (new_parent) { |
| 1362 | *new_parent = nparent; |
| 1363 | } |
| 1364 | if (new_node) { |
| 1365 | *new_node = nnode; |
| 1366 | } |
| 1367 | } |
| 1368 | return ret; |
| 1369 | } |
| 1370 | |
Michal Vasko | a6669ba | 2020-08-06 16:14:26 +0200 | [diff] [blame] | 1371 | LY_ERR |
| 1372 | lyd_new_implicit_r(struct lyd_node *parent, struct lyd_node **first, const struct lysc_node *sparent, |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 1373 | const struct lys_module *mod, struct ly_set *node_types, struct ly_set *node_when, uint32_t impl_opts, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 1374 | struct lyd_node **diff) |
Michal Vasko | a6669ba | 2020-08-06 16:14:26 +0200 | [diff] [blame] | 1375 | { |
| 1376 | LY_ERR ret; |
| 1377 | const struct lysc_node *iter = NULL; |
| 1378 | struct lyd_node *node; |
| 1379 | struct lyd_value **dflts; |
| 1380 | LY_ARRAY_COUNT_TYPE u; |
| 1381 | |
| 1382 | assert(first && (parent || sparent || mod)); |
| 1383 | |
| 1384 | if (!sparent && parent) { |
| 1385 | sparent = parent->schema; |
| 1386 | } |
| 1387 | |
| 1388 | while ((iter = lys_getnext(iter, sparent, mod ? mod->compiled : NULL, LYS_GETNEXT_WITHCHOICE))) { |
| 1389 | if ((impl_opts & LYD_IMPLICIT_NO_STATE) && (iter->flags & LYS_CONFIG_R)) { |
| 1390 | continue; |
Michal Vasko | 44b19a1 | 2020-08-07 09:21:30 +0200 | [diff] [blame] | 1391 | } else if ((impl_opts & LYD_IMPLICIT_NO_CONFIG) && (iter->flags & LYS_CONFIG_W)) { |
| 1392 | continue; |
Michal Vasko | a6669ba | 2020-08-06 16:14:26 +0200 | [diff] [blame] | 1393 | } |
| 1394 | |
| 1395 | switch (iter->nodetype) { |
| 1396 | case LYS_CHOICE: |
| 1397 | if (((struct lysc_node_choice *)iter)->dflt && !lys_getnext_data(NULL, *first, NULL, iter, NULL)) { |
| 1398 | /* create default case data */ |
| 1399 | LY_CHECK_RET(lyd_new_implicit_r(parent, first, (struct lysc_node *)((struct lysc_node_choice *)iter)->dflt, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 1400 | NULL, node_types, node_when, impl_opts, diff)); |
Michal Vasko | a6669ba | 2020-08-06 16:14:26 +0200 | [diff] [blame] | 1401 | } |
| 1402 | break; |
| 1403 | case LYS_CONTAINER: |
| 1404 | if (!(iter->flags & LYS_PRESENCE) && lyd_find_sibling_val(*first, iter, NULL, 0, NULL)) { |
| 1405 | /* create default NP container */ |
| 1406 | LY_CHECK_RET(lyd_create_inner(iter, &node)); |
Michal Vasko | 0cadfcb | 2020-11-04 17:17:05 +0100 | [diff] [blame] | 1407 | node->flags = LYD_DEFAULT | (node->schema->when ? LYD_WHEN_TRUE : 0); |
Michal Vasko | a6669ba | 2020-08-06 16:14:26 +0200 | [diff] [blame] | 1408 | lyd_insert_node(parent, first, node); |
| 1409 | |
| 1410 | /* cannot be a NP container with when */ |
| 1411 | assert(!iter->when); |
| 1412 | |
Michal Vasko | e49b632 | 2020-11-05 17:38:36 +0100 | [diff] [blame] | 1413 | if (diff) { |
| 1414 | /* add into diff */ |
| 1415 | LY_CHECK_RET(lyd_val_diff_add(node, LYD_DIFF_OP_CREATE, diff)); |
| 1416 | } |
| 1417 | |
Michal Vasko | a6669ba | 2020-08-06 16:14:26 +0200 | [diff] [blame] | 1418 | /* create any default children */ |
| 1419 | LY_CHECK_RET(lyd_new_implicit_r(node, lyd_node_children_p(node), NULL, NULL, node_types, node_when, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 1420 | impl_opts, diff)); |
Michal Vasko | a6669ba | 2020-08-06 16:14:26 +0200 | [diff] [blame] | 1421 | } |
| 1422 | break; |
| 1423 | case LYS_LEAF: |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 1424 | if (!(impl_opts & LYD_IMPLICIT_NO_DEFAULTS) && ((struct lysc_node_leaf *)iter)->dflt && |
| 1425 | lyd_find_sibling_val(*first, iter, NULL, 0, NULL)) { |
Michal Vasko | a6669ba | 2020-08-06 16:14:26 +0200 | [diff] [blame] | 1426 | /* create default leaf */ |
| 1427 | ret = lyd_create_term2(iter, ((struct lysc_node_leaf *)iter)->dflt, &node); |
| 1428 | if (ret == LY_EINCOMPLETE) { |
| 1429 | if (node_types) { |
| 1430 | /* remember to resolve type */ |
Radek Krejci | 3d92e44 | 2020-10-12 12:48:13 +0200 | [diff] [blame] | 1431 | LY_CHECK_RET(ly_set_add(node_types, node, 1, NULL)); |
Michal Vasko | a6669ba | 2020-08-06 16:14:26 +0200 | [diff] [blame] | 1432 | } |
| 1433 | } else if (ret) { |
| 1434 | return ret; |
| 1435 | } |
Michal Vasko | 0cadfcb | 2020-11-04 17:17:05 +0100 | [diff] [blame] | 1436 | node->flags = LYD_DEFAULT | (node->schema->when ? LYD_WHEN_TRUE : 0); |
Michal Vasko | a6669ba | 2020-08-06 16:14:26 +0200 | [diff] [blame] | 1437 | lyd_insert_node(parent, first, node); |
| 1438 | |
| 1439 | if (iter->when && node_when) { |
| 1440 | /* remember to resolve when */ |
Radek Krejci | 3d92e44 | 2020-10-12 12:48:13 +0200 | [diff] [blame] | 1441 | LY_CHECK_RET(ly_set_add(node_when, node, 1, NULL)); |
Michal Vasko | a6669ba | 2020-08-06 16:14:26 +0200 | [diff] [blame] | 1442 | } |
| 1443 | if (diff) { |
| 1444 | /* add into diff */ |
| 1445 | LY_CHECK_RET(lyd_val_diff_add(node, LYD_DIFF_OP_CREATE, diff)); |
| 1446 | } |
| 1447 | } |
| 1448 | break; |
| 1449 | case LYS_LEAFLIST: |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 1450 | if (!(impl_opts & LYD_IMPLICIT_NO_DEFAULTS) && ((struct lysc_node_leaflist *)iter)->dflts && |
| 1451 | lyd_find_sibling_val(*first, iter, NULL, 0, NULL)) { |
Michal Vasko | a6669ba | 2020-08-06 16:14:26 +0200 | [diff] [blame] | 1452 | /* create all default leaf-lists */ |
| 1453 | dflts = ((struct lysc_node_leaflist *)iter)->dflts; |
| 1454 | LY_ARRAY_FOR(dflts, u) { |
| 1455 | ret = lyd_create_term2(iter, dflts[u], &node); |
| 1456 | if (ret == LY_EINCOMPLETE) { |
| 1457 | if (node_types) { |
| 1458 | /* remember to resolve type */ |
Radek Krejci | 3d92e44 | 2020-10-12 12:48:13 +0200 | [diff] [blame] | 1459 | LY_CHECK_RET(ly_set_add(node_types, node, 1, NULL)); |
Michal Vasko | a6669ba | 2020-08-06 16:14:26 +0200 | [diff] [blame] | 1460 | } |
| 1461 | } else if (ret) { |
| 1462 | return ret; |
| 1463 | } |
Radek Krejci | c5b54a0 | 2020-11-05 17:13:18 +0100 | [diff] [blame] | 1464 | node->flags = LYD_DEFAULT | (node->schema->when ? LYD_WHEN_TRUE : 0); |
Michal Vasko | a6669ba | 2020-08-06 16:14:26 +0200 | [diff] [blame] | 1465 | lyd_insert_node(parent, first, node); |
| 1466 | |
| 1467 | if (iter->when && node_when) { |
| 1468 | /* remember to resolve when */ |
Radek Krejci | 3d92e44 | 2020-10-12 12:48:13 +0200 | [diff] [blame] | 1469 | LY_CHECK_RET(ly_set_add(node_when, node, 1, NULL)); |
Michal Vasko | a6669ba | 2020-08-06 16:14:26 +0200 | [diff] [blame] | 1470 | } |
| 1471 | if (diff) { |
| 1472 | /* add into diff */ |
| 1473 | LY_CHECK_RET(lyd_val_diff_add(node, LYD_DIFF_OP_CREATE, diff)); |
| 1474 | } |
| 1475 | } |
| 1476 | } |
| 1477 | break; |
| 1478 | default: |
| 1479 | /* without defaults */ |
| 1480 | break; |
| 1481 | } |
| 1482 | } |
| 1483 | |
| 1484 | return LY_SUCCESS; |
| 1485 | } |
| 1486 | |
| 1487 | API LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 1488 | lyd_new_implicit_tree(struct lyd_node *tree, uint32_t implicit_options, struct lyd_node **diff) |
Michal Vasko | a6669ba | 2020-08-06 16:14:26 +0200 | [diff] [blame] | 1489 | { |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1490 | struct lyd_node *node; |
Michal Vasko | a6669ba | 2020-08-06 16:14:26 +0200 | [diff] [blame] | 1491 | LY_ERR ret = LY_SUCCESS; |
| 1492 | |
| 1493 | LY_CHECK_ARG_RET(NULL, tree, LY_EINVAL); |
| 1494 | if (diff) { |
| 1495 | *diff = NULL; |
| 1496 | } |
| 1497 | |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1498 | LYD_TREE_DFS_BEGIN(tree, node) { |
Michal Vasko | a6669ba | 2020-08-06 16:14:26 +0200 | [diff] [blame] | 1499 | /* skip added default nodes */ |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 1500 | if (((node->flags & (LYD_DEFAULT | LYD_NEW)) != (LYD_DEFAULT | LYD_NEW)) && |
| 1501 | (node->schema->nodetype & LYD_NODE_INNER)) { |
Michal Vasko | a6669ba | 2020-08-06 16:14:26 +0200 | [diff] [blame] | 1502 | LY_CHECK_GOTO(ret = lyd_new_implicit_r(node, lyd_node_children_p((struct lyd_node *)node), NULL, NULL, NULL, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 1503 | NULL, implicit_options, diff), cleanup); |
Michal Vasko | a6669ba | 2020-08-06 16:14:26 +0200 | [diff] [blame] | 1504 | } |
| 1505 | |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1506 | LYD_TREE_DFS_END(tree, node); |
Michal Vasko | a6669ba | 2020-08-06 16:14:26 +0200 | [diff] [blame] | 1507 | } |
| 1508 | |
| 1509 | cleanup: |
| 1510 | if (ret && diff) { |
| 1511 | lyd_free_all(*diff); |
| 1512 | *diff = NULL; |
| 1513 | } |
| 1514 | return ret; |
| 1515 | } |
| 1516 | |
| 1517 | API LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 1518 | lyd_new_implicit_all(struct lyd_node **tree, const struct ly_ctx *ctx, uint32_t implicit_options, struct lyd_node **diff) |
Michal Vasko | a6669ba | 2020-08-06 16:14:26 +0200 | [diff] [blame] | 1519 | { |
| 1520 | const struct lys_module *mod; |
| 1521 | struct lyd_node *d = NULL; |
| 1522 | uint32_t i = 0; |
| 1523 | LY_ERR ret = LY_SUCCESS; |
| 1524 | |
| 1525 | LY_CHECK_ARG_RET(ctx, tree, *tree || ctx, LY_EINVAL); |
| 1526 | if (diff) { |
| 1527 | *diff = NULL; |
| 1528 | } |
| 1529 | if (!ctx) { |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 1530 | ctx = LYD_CTX(*tree); |
Michal Vasko | a6669ba | 2020-08-06 16:14:26 +0200 | [diff] [blame] | 1531 | } |
| 1532 | |
| 1533 | /* add nodes for each module one-by-one */ |
| 1534 | while ((mod = ly_ctx_get_module_iter(ctx, &i))) { |
| 1535 | if (!mod->implemented) { |
| 1536 | continue; |
| 1537 | } |
| 1538 | |
| 1539 | LY_CHECK_GOTO(ret = lyd_new_implicit_module(tree, mod, implicit_options, diff ? &d : NULL), cleanup); |
| 1540 | if (d) { |
| 1541 | /* merge into one diff */ |
| 1542 | lyd_insert_sibling(*diff, d, diff); |
| 1543 | |
| 1544 | d = NULL; |
| 1545 | } |
| 1546 | } |
| 1547 | |
| 1548 | cleanup: |
| 1549 | if (ret && diff) { |
| 1550 | lyd_free_all(*diff); |
| 1551 | *diff = NULL; |
| 1552 | } |
| 1553 | return ret; |
| 1554 | } |
| 1555 | |
| 1556 | API LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 1557 | lyd_new_implicit_module(struct lyd_node **tree, const struct lys_module *module, uint32_t implicit_options, struct lyd_node **diff) |
Michal Vasko | a6669ba | 2020-08-06 16:14:26 +0200 | [diff] [blame] | 1558 | { |
| 1559 | struct lyd_node *root, *d = NULL; |
| 1560 | LY_ERR ret = LY_SUCCESS; |
| 1561 | |
| 1562 | LY_CHECK_ARG_RET(NULL, tree, module, LY_EINVAL); |
| 1563 | if (diff) { |
| 1564 | *diff = NULL; |
| 1565 | } |
| 1566 | |
| 1567 | /* add all top-level defaults for this module */ |
| 1568 | LY_CHECK_GOTO(ret = lyd_new_implicit_r(NULL, tree, NULL, module, NULL, NULL, implicit_options, diff), cleanup); |
| 1569 | |
| 1570 | /* process nested nodes */ |
| 1571 | LY_LIST_FOR(*tree, root) { |
| 1572 | /* skip added default nodes */ |
| 1573 | if ((root->flags & (LYD_DEFAULT | LYD_NEW)) != (LYD_DEFAULT | LYD_NEW)) { |
| 1574 | LY_CHECK_GOTO(ret = lyd_new_implicit_tree(root, implicit_options, diff ? &d : NULL), cleanup); |
| 1575 | |
| 1576 | if (d) { |
| 1577 | /* merge into one diff */ |
| 1578 | lyd_insert_sibling(*diff, d, diff); |
| 1579 | |
| 1580 | d = NULL; |
| 1581 | } |
| 1582 | } |
| 1583 | } |
| 1584 | |
| 1585 | cleanup: |
| 1586 | if (ret && diff) { |
| 1587 | lyd_free_all(*diff); |
| 1588 | *diff = NULL; |
| 1589 | } |
| 1590 | return ret; |
| 1591 | } |
| 1592 | |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 1593 | struct lyd_node * |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 1594 | lyd_insert_get_next_anchor(const struct lyd_node *first_sibling, const struct lyd_node *new_node) |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 1595 | { |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 1596 | const struct lysc_node *schema, *sparent; |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 1597 | struct lyd_node *match = NULL; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 1598 | ly_bool found; |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 1599 | |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 1600 | assert(new_node); |
| 1601 | |
| 1602 | if (!first_sibling || !new_node->schema) { |
| 1603 | /* insert at the end, no next anchor */ |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 1604 | return NULL; |
| 1605 | } |
| 1606 | |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 1607 | if (first_sibling->parent && first_sibling->parent->children_ht) { |
| 1608 | /* find the anchor using hashes */ |
| 1609 | sparent = first_sibling->parent->schema; |
| 1610 | schema = lys_getnext(new_node->schema, sparent, NULL, 0); |
| 1611 | while (schema) { |
| 1612 | /* keep trying to find the first existing instance of the closest following schema sibling, |
| 1613 | * otherwise return NULL - inserting at the end */ |
| 1614 | if (!lyd_find_sibling_schema(first_sibling, schema, &match)) { |
| 1615 | break; |
| 1616 | } |
| 1617 | |
| 1618 | schema = lys_getnext(schema, sparent, NULL, 0); |
| 1619 | } |
| 1620 | } else { |
| 1621 | /* find the anchor without hashes */ |
| 1622 | match = (struct lyd_node *)first_sibling; |
| 1623 | if (!lysc_data_parent(new_node->schema)) { |
| 1624 | /* we are in top-level, skip all the data from preceding modules */ |
| 1625 | LY_LIST_FOR(match, match) { |
| 1626 | if (!match->schema || (strcmp(lyd_owner_module(match)->name, lyd_owner_module(new_node)->name) >= 0)) { |
| 1627 | break; |
| 1628 | } |
| 1629 | } |
| 1630 | } |
| 1631 | |
| 1632 | /* get the first schema sibling */ |
| 1633 | sparent = lysc_data_parent(new_node->schema); |
| 1634 | schema = lys_getnext(NULL, sparent, new_node->schema->module->compiled, 0); |
| 1635 | |
| 1636 | found = 0; |
| 1637 | LY_LIST_FOR(match, match) { |
| 1638 | if (!match->schema || (lyd_owner_module(match) != lyd_owner_module(new_node))) { |
| 1639 | /* we have found an opaque node, which must be at the end, so use it OR |
| 1640 | * modules do not match, so we must have traversed all the data from new_node module (if any), |
| 1641 | * we have found the first node of the next module, that is what we want */ |
| 1642 | break; |
| 1643 | } |
| 1644 | |
| 1645 | /* skip schema nodes until we find the instantiated one */ |
| 1646 | while (!found) { |
| 1647 | if (new_node->schema == schema) { |
| 1648 | /* we have found the schema of the new node, continue search to find the first |
| 1649 | * data node with a different schema (after our schema) */ |
| 1650 | found = 1; |
| 1651 | break; |
| 1652 | } |
| 1653 | if (match->schema == schema) { |
| 1654 | /* current node (match) is a data node still before the new node, continue search in data */ |
| 1655 | break; |
| 1656 | } |
| 1657 | schema = lys_getnext(schema, sparent, new_node->schema->module->compiled, 0); |
| 1658 | assert(schema); |
| 1659 | } |
| 1660 | |
| 1661 | if (found && (match->schema != new_node->schema)) { |
| 1662 | /* find the next node after we have found our node schema data instance */ |
| 1663 | break; |
| 1664 | } |
| 1665 | } |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 1666 | } |
| 1667 | |
| 1668 | return match; |
| 1669 | } |
| 1670 | |
| 1671 | /** |
| 1672 | * @brief Insert node after a sibling. |
| 1673 | * |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1674 | * Handles inserting into NP containers and key-less lists. |
| 1675 | * |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 1676 | * @param[in] sibling Sibling to insert after. |
| 1677 | * @param[in] node Node to insert. |
| 1678 | */ |
| 1679 | static void |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 1680 | lyd_insert_after_node(struct lyd_node *sibling, struct lyd_node *node) |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 1681 | { |
Michal Vasko | 0249f7c | 2020-03-05 16:36:40 +0100 | [diff] [blame] | 1682 | struct lyd_node_inner *par; |
| 1683 | |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 1684 | assert(!node->next && (node->prev == node)); |
| 1685 | |
| 1686 | node->next = sibling->next; |
| 1687 | node->prev = sibling; |
| 1688 | sibling->next = node; |
| 1689 | if (node->next) { |
| 1690 | /* sibling had a succeeding node */ |
| 1691 | node->next->prev = node; |
| 1692 | } else { |
| 1693 | /* sibling was last, find first sibling and change its prev */ |
| 1694 | if (sibling->parent) { |
| 1695 | sibling = sibling->parent->child; |
| 1696 | } else { |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 1697 | for ( ; sibling->prev->next != node; sibling = sibling->prev) {} |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 1698 | } |
| 1699 | sibling->prev = node; |
| 1700 | } |
| 1701 | node->parent = sibling->parent; |
Michal Vasko | 0249f7c | 2020-03-05 16:36:40 +0100 | [diff] [blame] | 1702 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1703 | for (par = node->parent; par; par = par->parent) { |
| 1704 | if ((par->flags & LYD_DEFAULT) && !(node->flags & LYD_DEFAULT)) { |
| 1705 | /* remove default flags from NP containers */ |
Michal Vasko | 0249f7c | 2020-03-05 16:36:40 +0100 | [diff] [blame] | 1706 | par->flags &= ~LYD_DEFAULT; |
| 1707 | } |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 1708 | if (par->schema && (par->schema->nodetype == LYS_LIST) && (par->schema->flags & LYS_KEYLESS)) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1709 | /* rehash key-less list */ |
| 1710 | lyd_hash((struct lyd_node *)par); |
| 1711 | } |
Michal Vasko | 0249f7c | 2020-03-05 16:36:40 +0100 | [diff] [blame] | 1712 | } |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 1713 | } |
| 1714 | |
| 1715 | /** |
| 1716 | * @brief Insert node before a sibling. |
| 1717 | * |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1718 | * Handles inserting into NP containers and key-less lists. |
| 1719 | * |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 1720 | * @param[in] sibling Sibling to insert before. |
| 1721 | * @param[in] node Node to insert. |
| 1722 | */ |
| 1723 | static void |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 1724 | lyd_insert_before_node(struct lyd_node *sibling, struct lyd_node *node) |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 1725 | { |
Michal Vasko | 0249f7c | 2020-03-05 16:36:40 +0100 | [diff] [blame] | 1726 | struct lyd_node_inner *par; |
| 1727 | |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 1728 | assert(!node->next && (node->prev == node)); |
| 1729 | |
| 1730 | node->next = sibling; |
| 1731 | /* covers situation of sibling being first */ |
| 1732 | node->prev = sibling->prev; |
| 1733 | sibling->prev = node; |
| 1734 | if (node->prev->next) { |
| 1735 | /* sibling had a preceding node */ |
| 1736 | node->prev->next = node; |
| 1737 | } else if (sibling->parent) { |
| 1738 | /* sibling was first and we must also change parent child pointer */ |
| 1739 | sibling->parent->child = node; |
| 1740 | } |
| 1741 | node->parent = sibling->parent; |
Michal Vasko | 0249f7c | 2020-03-05 16:36:40 +0100 | [diff] [blame] | 1742 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1743 | for (par = node->parent; par; par = par->parent) { |
| 1744 | if ((par->flags & LYD_DEFAULT) && !(node->flags & LYD_DEFAULT)) { |
| 1745 | /* remove default flags from NP containers */ |
Michal Vasko | 0249f7c | 2020-03-05 16:36:40 +0100 | [diff] [blame] | 1746 | par->flags &= ~LYD_DEFAULT; |
| 1747 | } |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 1748 | if (par->schema && (par->schema->nodetype == LYS_LIST) && (par->schema->flags & LYS_KEYLESS)) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1749 | /* rehash key-less list */ |
| 1750 | lyd_hash((struct lyd_node *)par); |
| 1751 | } |
Michal Vasko | 0249f7c | 2020-03-05 16:36:40 +0100 | [diff] [blame] | 1752 | } |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 1753 | } |
| 1754 | |
| 1755 | /** |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 1756 | * @brief Insert node as the first and only child of a parent. |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 1757 | * |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1758 | * Handles inserting into NP containers and key-less lists. |
| 1759 | * |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 1760 | * @param[in] parent Parent to insert into. |
| 1761 | * @param[in] node Node to insert. |
| 1762 | */ |
| 1763 | static void |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 1764 | lyd_insert_only_child(struct lyd_node *parent, struct lyd_node *node) |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 1765 | { |
| 1766 | struct lyd_node_inner *par; |
| 1767 | |
Radek Krejci | a1c1e54 | 2020-09-29 16:06:52 +0200 | [diff] [blame] | 1768 | assert(parent && !lyd_child(parent) && !node->next && (node->prev == node)); |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 1769 | assert(!parent->schema || (parent->schema->nodetype & LYD_NODE_INNER)); |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 1770 | |
| 1771 | par = (struct lyd_node_inner *)parent; |
| 1772 | |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 1773 | par->child = node; |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 1774 | node->parent = par; |
Michal Vasko | 0249f7c | 2020-03-05 16:36:40 +0100 | [diff] [blame] | 1775 | |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 1776 | for ( ; par; par = par->parent) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1777 | if ((par->flags & LYD_DEFAULT) && !(node->flags & LYD_DEFAULT)) { |
| 1778 | /* remove default flags from NP containers */ |
Michal Vasko | 0249f7c | 2020-03-05 16:36:40 +0100 | [diff] [blame] | 1779 | par->flags &= ~LYD_DEFAULT; |
| 1780 | } |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 1781 | if (par->schema && (par->schema->nodetype == LYS_LIST) && (par->schema->flags & LYS_KEYLESS)) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1782 | /* rehash key-less list */ |
| 1783 | lyd_hash((struct lyd_node *)par); |
| 1784 | } |
Michal Vasko | 0249f7c | 2020-03-05 16:36:40 +0100 | [diff] [blame] | 1785 | } |
Michal Vasko | 751cb4d | 2020-07-14 12:25:28 +0200 | [diff] [blame] | 1786 | } |
Michal Vasko | 0249f7c | 2020-03-05 16:36:40 +0100 | [diff] [blame] | 1787 | |
Michal Vasko | 751cb4d | 2020-07-14 12:25:28 +0200 | [diff] [blame] | 1788 | /** |
| 1789 | * @brief Learn whether a list instance has all the keys. |
| 1790 | * |
| 1791 | * @param[in] list List instance to check. |
| 1792 | * @return non-zero if all the keys were found, |
| 1793 | * @return 0 otherwise. |
| 1794 | */ |
| 1795 | static int |
| 1796 | lyd_insert_has_keys(const struct lyd_node *list) |
| 1797 | { |
| 1798 | const struct lyd_node *key; |
| 1799 | const struct lysc_node *skey = NULL; |
| 1800 | |
| 1801 | assert(list->schema->nodetype == LYS_LIST); |
Radek Krejci | a1c1e54 | 2020-09-29 16:06:52 +0200 | [diff] [blame] | 1802 | key = lyd_child(list); |
Michal Vasko | 751cb4d | 2020-07-14 12:25:28 +0200 | [diff] [blame] | 1803 | while ((skey = lys_getnext(skey, list->schema, NULL, 0)) && (skey->flags & LYS_KEY)) { |
| 1804 | if (!key || (key->schema != skey)) { |
| 1805 | /* key missing */ |
| 1806 | return 0; |
| 1807 | } |
| 1808 | |
| 1809 | key = key->next; |
| 1810 | } |
| 1811 | |
| 1812 | /* all keys found */ |
| 1813 | return 1; |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 1814 | } |
| 1815 | |
| 1816 | void |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 1817 | lyd_insert_node(struct lyd_node *parent, struct lyd_node **first_sibling_p, struct lyd_node *node) |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 1818 | { |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 1819 | struct lyd_node *anchor, *first_sibling; |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 1820 | |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 1821 | /* inserting list without its keys is not supported */ |
| 1822 | assert((parent || first_sibling_p) && node && (node->hash || !node->schema)); |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 1823 | |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 1824 | if (!parent && first_sibling_p && (*first_sibling_p) && (*first_sibling_p)->parent) { |
| 1825 | parent = (struct lyd_node *)(*first_sibling_p)->parent; |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 1826 | } |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 1827 | |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 1828 | /* get first sibling */ |
| 1829 | first_sibling = parent ? ((struct lyd_node_inner *)parent)->child : *first_sibling_p; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1830 | |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 1831 | /* find the anchor, our next node, so we can insert before it */ |
| 1832 | anchor = lyd_insert_get_next_anchor(first_sibling, node); |
| 1833 | if (anchor) { |
| 1834 | lyd_insert_before_node(anchor, node); |
| 1835 | } else if (first_sibling) { |
| 1836 | lyd_insert_after_node(first_sibling->prev, node); |
| 1837 | } else if (parent) { |
| 1838 | lyd_insert_only_child(parent, node); |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 1839 | } else { |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 1840 | *first_sibling_p = node; |
| 1841 | } |
| 1842 | |
| 1843 | /* insert into parent HT */ |
| 1844 | lyd_insert_hash(node); |
| 1845 | |
| 1846 | /* finish hashes for our parent, if needed and possible */ |
Michal Vasko | 9e8de2d | 2020-09-01 08:17:10 +0200 | [diff] [blame] | 1847 | if (node->schema && (node->schema->flags & LYS_KEY) && parent && lyd_insert_has_keys(parent)) { |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 1848 | lyd_hash(parent); |
| 1849 | |
| 1850 | /* now we can insert even the list into its parent HT */ |
| 1851 | lyd_insert_hash(parent); |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 1852 | } |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 1853 | } |
| 1854 | |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 1855 | static LY_ERR |
| 1856 | lyd_insert_check_schema(const struct lysc_node *parent, const struct lysc_node *schema) |
| 1857 | { |
| 1858 | const struct lysc_node *par2; |
| 1859 | |
| 1860 | assert(schema); |
Michal Vasko | 62ed12d | 2020-05-21 10:08:25 +0200 | [diff] [blame] | 1861 | assert(!parent || !(parent->nodetype & (LYS_CASE | LYS_CHOICE))); |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 1862 | |
| 1863 | /* find schema parent */ |
Michal Vasko | 62ed12d | 2020-05-21 10:08:25 +0200 | [diff] [blame] | 1864 | par2 = lysc_data_parent(schema); |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 1865 | |
| 1866 | if (parent) { |
| 1867 | /* inner node */ |
| 1868 | if (par2 != parent) { |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 1869 | LOGERR(schema->module->ctx, LY_EINVAL, "Cannot insert, parent of \"%s\" is not \"%s\".", schema->name, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 1870 | parent->name); |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 1871 | return LY_EINVAL; |
| 1872 | } |
| 1873 | } else { |
| 1874 | /* top-level node */ |
| 1875 | if (par2) { |
Radek Krejci | f6d14cb | 2020-07-02 16:11:45 +0200 | [diff] [blame] | 1876 | LOGERR(schema->module->ctx, LY_EINVAL, "Cannot insert, node \"%s\" is not top-level.", schema->name); |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 1877 | return LY_EINVAL; |
| 1878 | } |
| 1879 | } |
| 1880 | |
| 1881 | return LY_SUCCESS; |
| 1882 | } |
| 1883 | |
| 1884 | API LY_ERR |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 1885 | lyd_insert_child(struct lyd_node *parent, struct lyd_node *node) |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 1886 | { |
| 1887 | struct lyd_node *iter; |
| 1888 | |
Michal Vasko | 654bc85 | 2020-06-23 13:28:06 +0200 | [diff] [blame] | 1889 | LY_CHECK_ARG_RET(NULL, parent, node, parent->schema->nodetype & LYD_NODE_INNER, LY_EINVAL); |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 1890 | |
| 1891 | LY_CHECK_RET(lyd_insert_check_schema(parent->schema, node->schema)); |
| 1892 | |
| 1893 | if (node->schema->flags & LYS_KEY) { |
| 1894 | LOGERR(parent->schema->module->ctx, LY_EINVAL, "Cannot insert key \"%s\".", node->schema->name); |
| 1895 | return LY_EINVAL; |
| 1896 | } |
| 1897 | |
| 1898 | if (node->parent || node->prev->next) { |
| 1899 | lyd_unlink_tree(node); |
| 1900 | } |
| 1901 | |
| 1902 | while (node) { |
| 1903 | iter = node->next; |
| 1904 | lyd_unlink_tree(node); |
| 1905 | lyd_insert_node(parent, NULL, node); |
| 1906 | node = iter; |
| 1907 | } |
| 1908 | return LY_SUCCESS; |
| 1909 | } |
| 1910 | |
| 1911 | API LY_ERR |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 1912 | lyd_insert_sibling(struct lyd_node *sibling, struct lyd_node *node, struct lyd_node **first) |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 1913 | { |
| 1914 | struct lyd_node *iter; |
| 1915 | |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 1916 | LY_CHECK_ARG_RET(NULL, node, LY_EINVAL); |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 1917 | |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 1918 | if (sibling) { |
| 1919 | LY_CHECK_RET(lyd_insert_check_schema(lysc_data_parent(sibling->schema), node->schema)); |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 1920 | } |
| 1921 | |
| 1922 | if (node->parent || node->prev->next) { |
| 1923 | lyd_unlink_tree(node); |
| 1924 | } |
| 1925 | |
| 1926 | while (node) { |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 1927 | if (node->schema->flags & LYS_KEY) { |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 1928 | LOGERR(LYD_CTX(node), LY_EINVAL, "Cannot insert key \"%s\".", node->schema->name); |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 1929 | return LY_EINVAL; |
| 1930 | } |
| 1931 | |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 1932 | iter = node->next; |
| 1933 | lyd_unlink_tree(node); |
| 1934 | lyd_insert_node(NULL, &sibling, node); |
| 1935 | node = iter; |
| 1936 | } |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 1937 | |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 1938 | if (first) { |
| 1939 | /* find the first sibling */ |
| 1940 | *first = sibling; |
| 1941 | while ((*first)->prev->next) { |
| 1942 | *first = (*first)->prev; |
Michal Vasko | 0249f7c | 2020-03-05 16:36:40 +0100 | [diff] [blame] | 1943 | } |
| 1944 | } |
| 1945 | |
| 1946 | return LY_SUCCESS; |
| 1947 | } |
| 1948 | |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 1949 | API LY_ERR |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 1950 | lyd_insert_before(struct lyd_node *sibling, struct lyd_node *node) |
| 1951 | { |
| 1952 | struct lyd_node *iter; |
| 1953 | |
| 1954 | LY_CHECK_ARG_RET(NULL, sibling, node, LY_EINVAL); |
| 1955 | |
Michal Vasko | 62ed12d | 2020-05-21 10:08:25 +0200 | [diff] [blame] | 1956 | LY_CHECK_RET(lyd_insert_check_schema(lysc_data_parent(sibling->schema), node->schema)); |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 1957 | |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 1958 | if (!(node->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) || !(node->schema->flags & LYS_ORDBY_USER)) { |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 1959 | LOGERR(LYD_CTX(sibling), LY_EINVAL, "Can be used only for user-ordered nodes."); |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 1960 | return LY_EINVAL; |
| 1961 | } |
| 1962 | |
| 1963 | if (node->parent || node->prev->next) { |
| 1964 | lyd_unlink_tree(node); |
| 1965 | } |
| 1966 | |
| 1967 | /* insert in reverse order to get the original order */ |
| 1968 | node = node->prev; |
| 1969 | while (node) { |
| 1970 | iter = node->prev; |
| 1971 | lyd_unlink_tree(node); |
| 1972 | |
| 1973 | lyd_insert_before_node(sibling, node); |
Michal Vasko | 751cb4d | 2020-07-14 12:25:28 +0200 | [diff] [blame] | 1974 | lyd_insert_hash(node); |
| 1975 | |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 1976 | /* move the anchor accordingly */ |
| 1977 | sibling = node; |
| 1978 | |
| 1979 | node = (iter == node) ? NULL : iter; |
| 1980 | } |
| 1981 | return LY_SUCCESS; |
| 1982 | } |
| 1983 | |
| 1984 | API LY_ERR |
| 1985 | lyd_insert_after(struct lyd_node *sibling, struct lyd_node *node) |
| 1986 | { |
| 1987 | struct lyd_node *iter; |
| 1988 | |
| 1989 | LY_CHECK_ARG_RET(NULL, sibling, node, LY_EINVAL); |
| 1990 | |
Michal Vasko | 62ed12d | 2020-05-21 10:08:25 +0200 | [diff] [blame] | 1991 | LY_CHECK_RET(lyd_insert_check_schema(lysc_data_parent(sibling->schema), node->schema)); |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 1992 | |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 1993 | if (!(node->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) || !(node->schema->flags & LYS_ORDBY_USER)) { |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 1994 | LOGERR(LYD_CTX(sibling), LY_EINVAL, "Can be used only for user-ordered nodes."); |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 1995 | return LY_EINVAL; |
| 1996 | } |
| 1997 | |
| 1998 | if (node->parent || node->prev->next) { |
| 1999 | lyd_unlink_tree(node); |
| 2000 | } |
| 2001 | |
| 2002 | while (node) { |
| 2003 | iter = node->next; |
| 2004 | lyd_unlink_tree(node); |
| 2005 | |
| 2006 | lyd_insert_after_node(sibling, node); |
Michal Vasko | 751cb4d | 2020-07-14 12:25:28 +0200 | [diff] [blame] | 2007 | lyd_insert_hash(node); |
| 2008 | |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 2009 | /* move the anchor accordingly */ |
| 2010 | sibling = node; |
| 2011 | |
| 2012 | node = iter; |
| 2013 | } |
| 2014 | return LY_SUCCESS; |
| 2015 | } |
| 2016 | |
| 2017 | API void |
| 2018 | lyd_unlink_tree(struct lyd_node *node) |
| 2019 | { |
| 2020 | struct lyd_node *iter; |
| 2021 | |
| 2022 | if (!node) { |
| 2023 | return; |
| 2024 | } |
| 2025 | |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 2026 | /* update hashes while still linked into the tree */ |
| 2027 | lyd_unlink_hash(node); |
| 2028 | |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 2029 | /* unlink from siblings */ |
| 2030 | if (node->prev->next) { |
| 2031 | node->prev->next = node->next; |
| 2032 | } |
| 2033 | if (node->next) { |
| 2034 | node->next->prev = node->prev; |
| 2035 | } else { |
| 2036 | /* unlinking the last node */ |
| 2037 | if (node->parent) { |
| 2038 | iter = node->parent->child; |
| 2039 | } else { |
| 2040 | iter = node->prev; |
| 2041 | while (iter->prev != node) { |
| 2042 | iter = iter->prev; |
| 2043 | } |
| 2044 | } |
| 2045 | /* update the "last" pointer from the first node */ |
| 2046 | iter->prev = node->prev; |
| 2047 | } |
| 2048 | |
| 2049 | /* unlink from parent */ |
| 2050 | if (node->parent) { |
| 2051 | if (node->parent->child == node) { |
| 2052 | /* the node is the first child */ |
| 2053 | node->parent->child = node->next; |
| 2054 | } |
| 2055 | |
Michal Vasko | ab49dbe | 2020-07-17 12:32:47 +0200 | [diff] [blame] | 2056 | /* check for NP container whether its last non-default node is not being unlinked */ |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 2057 | if (node->parent->schema && (node->parent->schema->nodetype == LYS_CONTAINER) && |
| 2058 | !(node->parent->flags & LYD_DEFAULT) && !(node->parent->schema->flags & LYS_PRESENCE)) { |
Michal Vasko | ab49dbe | 2020-07-17 12:32:47 +0200 | [diff] [blame] | 2059 | LY_LIST_FOR(node->parent->child, iter) { |
| 2060 | if ((iter != node) && !(iter->flags & LYD_DEFAULT)) { |
| 2061 | break; |
| 2062 | } |
| 2063 | } |
| 2064 | if (!iter) { |
| 2065 | node->parent->flags |= LYD_DEFAULT; |
| 2066 | } |
| 2067 | } |
| 2068 | |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 2069 | /* check for keyless list and update its hash */ |
| 2070 | for (iter = (struct lyd_node *)node->parent; iter; iter = (struct lyd_node *)iter->parent) { |
Michal Vasko | 413c7f2 | 2020-05-05 12:34:06 +0200 | [diff] [blame] | 2071 | if (iter->schema && (iter->schema->flags & LYS_KEYLESS)) { |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 2072 | lyd_hash(iter); |
| 2073 | } |
| 2074 | } |
| 2075 | |
| 2076 | node->parent = NULL; |
| 2077 | } |
| 2078 | |
| 2079 | node->next = NULL; |
| 2080 | node->prev = node; |
| 2081 | } |
| 2082 | |
Michal Vasko | a5da329 | 2020-08-12 13:10:50 +0200 | [diff] [blame] | 2083 | void |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 2084 | lyd_insert_meta(struct lyd_node *parent, struct lyd_meta *meta) |
| 2085 | { |
| 2086 | struct lyd_meta *last, *iter; |
| 2087 | |
| 2088 | assert(parent); |
Michal Vasko | a5da329 | 2020-08-12 13:10:50 +0200 | [diff] [blame] | 2089 | |
| 2090 | if (!meta) { |
| 2091 | return; |
| 2092 | } |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 2093 | |
| 2094 | for (iter = meta; iter; iter = iter->next) { |
| 2095 | iter->parent = parent; |
| 2096 | } |
| 2097 | |
| 2098 | /* insert as the last attribute */ |
| 2099 | if (parent->meta) { |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 2100 | for (last = parent->meta; last->next; last = last->next) {} |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 2101 | last->next = meta; |
| 2102 | } else { |
| 2103 | parent->meta = meta; |
| 2104 | } |
| 2105 | |
| 2106 | /* remove default flags from NP containers */ |
| 2107 | while (parent && (parent->schema->nodetype == LYS_CONTAINER) && (parent->flags & LYD_DEFAULT)) { |
| 2108 | parent->flags &= ~LYD_DEFAULT; |
| 2109 | parent = (struct lyd_node *)parent->parent; |
| 2110 | } |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 2111 | } |
| 2112 | |
| 2113 | LY_ERR |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 2114 | lyd_create_meta(struct lyd_node *parent, struct lyd_meta **meta, const struct lys_module *mod, const char *name, |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 2115 | size_t name_len, const char *value, size_t value_len, ly_bool *dynamic, LY_PREFIX_FORMAT format, |
| 2116 | void *prefix_data, uint32_t hints, ly_bool *incomplete) |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 2117 | { |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 2118 | LY_ERR rc; |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 2119 | struct lysc_ext_instance *ant = NULL; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 2120 | struct lyd_meta *mt, *last; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 2121 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 2122 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 2123 | assert((parent || meta) && mod); |
Michal Vasko | 6f4cbb6 | 2020-02-28 11:15:47 +0100 | [diff] [blame] | 2124 | |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 2125 | LY_ARRAY_FOR(mod->compiled->exts, u) { |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 2126 | if ((mod->compiled->exts[u].def->plugin == lyext_plugins_internal[LYEXT_PLUGIN_INTERNAL_ANNOTATION].plugin) && |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 2127 | !ly_strncmp(mod->compiled->exts[u].argument, name, name_len)) { |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 2128 | /* we have the annotation definition */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 2129 | ant = &mod->compiled->exts[u]; |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 2130 | break; |
| 2131 | } |
| 2132 | } |
| 2133 | if (!ant) { |
| 2134 | /* attribute is not defined as a metadata annotation (RFC 7952) */ |
| 2135 | LOGERR(mod->ctx, LY_EINVAL, "Annotation definition for attribute \"%s:%.*s\" not found.", |
| 2136 | mod->name, name_len, name); |
| 2137 | return LY_EINVAL; |
| 2138 | } |
| 2139 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 2140 | mt = calloc(1, sizeof *mt); |
| 2141 | LY_CHECK_ERR_RET(!mt, LOGMEM(mod->ctx), LY_EMEM); |
| 2142 | mt->parent = parent; |
| 2143 | mt->annotation = ant; |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 2144 | rc = lyd_value_store(mod->ctx, &mt->value, ((struct lyext_metadata *)ant->data)->type, value, value_len, dynamic, |
| 2145 | format, prefix_data, hints, parent ? parent->schema : NULL, incomplete, LY_VLOG_NONE, NULL); |
| 2146 | LY_CHECK_ERR_RET(rc, free(mt), rc); |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 2147 | rc = lydict_insert(mod->ctx, name, name_len, &mt->name); |
| 2148 | LY_CHECK_ERR_RET(rc, free(mt), rc); |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 2149 | |
Michal Vasko | 6f4cbb6 | 2020-02-28 11:15:47 +0100 | [diff] [blame] | 2150 | /* insert as the last attribute */ |
| 2151 | if (parent) { |
Michal Vasko | a5da329 | 2020-08-12 13:10:50 +0200 | [diff] [blame] | 2152 | lyd_insert_meta(parent, mt); |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 2153 | } else if (*meta) { |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 2154 | for (last = *meta; last->next; last = last->next) {} |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 2155 | last->next = mt; |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 2156 | } |
| 2157 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 2158 | if (meta) { |
| 2159 | *meta = mt; |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 2160 | } |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 2161 | return LY_SUCCESS; |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 2162 | } |
| 2163 | |
Michal Vasko | a5da329 | 2020-08-12 13:10:50 +0200 | [diff] [blame] | 2164 | void |
| 2165 | lyd_insert_attr(struct lyd_node *parent, struct lyd_attr *attr) |
| 2166 | { |
| 2167 | struct lyd_attr *last, *iter; |
| 2168 | struct lyd_node_opaq *opaq; |
| 2169 | |
| 2170 | assert(parent && !parent->schema); |
| 2171 | |
| 2172 | if (!attr) { |
| 2173 | return; |
| 2174 | } |
| 2175 | |
| 2176 | opaq = (struct lyd_node_opaq *)parent; |
| 2177 | for (iter = attr; iter; iter = iter->next) { |
| 2178 | iter->parent = opaq; |
| 2179 | } |
| 2180 | |
| 2181 | /* insert as the last attribute */ |
| 2182 | if (opaq->attr) { |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 2183 | for (last = opaq->attr; last->next; last = last->next) {} |
Michal Vasko | a5da329 | 2020-08-12 13:10:50 +0200 | [diff] [blame] | 2184 | last->next = attr; |
| 2185 | } else { |
| 2186 | opaq->attr = attr; |
| 2187 | } |
| 2188 | } |
| 2189 | |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 2190 | LY_ERR |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 2191 | lyd_create_attr(struct lyd_node *parent, struct lyd_attr **attr, const struct ly_ctx *ctx, const char *name, size_t name_len, |
| 2192 | const char *value, size_t value_len, ly_bool *dynamic, LYD_FORMAT format, uint32_t hints, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 2193 | struct ly_prefix *val_prefs, const char *prefix, size_t prefix_len, const char *module_key, size_t module_key_len) |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 2194 | { |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 2195 | LY_ERR ret = LY_SUCCESS; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 2196 | struct lyd_attr *at, *last; |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 2197 | |
| 2198 | assert(ctx && (parent || attr) && (!parent || !parent->schema)); |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 2199 | assert(name && name_len && format); |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 2200 | |
| 2201 | if (!value_len) { |
| 2202 | value = ""; |
| 2203 | } |
| 2204 | |
| 2205 | at = calloc(1, sizeof *at); |
Radek Krejci | d46e46a | 2020-09-15 14:22:42 +0200 | [diff] [blame] | 2206 | LY_CHECK_ERR_RET(!at, LOGMEM(ctx); ly_free_val_prefs(ctx, val_prefs), LY_EMEM); |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 2207 | at->hints = hints; |
Radek Krejci | d46e46a | 2020-09-15 14:22:42 +0200 | [diff] [blame] | 2208 | at->format = format; |
| 2209 | at->val_prefs = val_prefs; |
| 2210 | |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 2211 | LY_CHECK_GOTO(ret = lydict_insert(ctx, name, name_len, &at->name), finish); |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 2212 | if (dynamic && *dynamic) { |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 2213 | ret = lydict_insert_zc(ctx, (char *)value, &at->value); |
| 2214 | LY_CHECK_GOTO(ret, finish); |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 2215 | *dynamic = 0; |
| 2216 | } else { |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 2217 | LY_CHECK_GOTO(ret = lydict_insert(ctx, value, value_len, &at->value), finish); |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 2218 | } |
| 2219 | |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 2220 | if (prefix_len) { |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 2221 | LY_CHECK_GOTO(ret = lydict_insert(ctx, prefix, prefix_len, &at->prefix.id), finish); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 2222 | } |
| 2223 | if (module_key_len) { |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 2224 | LY_CHECK_GOTO(ret = lydict_insert(ctx, module_key, module_key_len, &at->prefix.module_ns), finish); |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 2225 | } |
| 2226 | |
| 2227 | /* insert as the last attribute */ |
| 2228 | if (parent) { |
Michal Vasko | a5da329 | 2020-08-12 13:10:50 +0200 | [diff] [blame] | 2229 | lyd_insert_attr(parent, at); |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 2230 | } else if (*attr) { |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 2231 | for (last = *attr; last->next; last = last->next) {} |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 2232 | last->next = at; |
| 2233 | } |
| 2234 | |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 2235 | finish: |
| 2236 | if (ret) { |
| 2237 | lyd_free_attr_single(ctx, at); |
| 2238 | } else if (attr) { |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 2239 | *attr = at; |
| 2240 | } |
| 2241 | return LY_SUCCESS; |
| 2242 | } |
| 2243 | |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 2244 | API const struct lyd_node_term * |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2245 | lyd_target(const struct ly_path *path, const struct lyd_node *tree) |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 2246 | { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2247 | struct lyd_node *target; |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 2248 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2249 | if (ly_path_eval(path, tree, &target)) { |
| 2250 | return NULL; |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 2251 | } |
| 2252 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2253 | return (struct lyd_node_term *)target; |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 2254 | } |
| 2255 | |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 2256 | API LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 2257 | lyd_compare_single(const struct lyd_node *node1, const struct lyd_node *node2, uint32_t options) |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 2258 | { |
| 2259 | const struct lyd_node *iter1, *iter2; |
| 2260 | struct lyd_node_term *term1, *term2; |
| 2261 | struct lyd_node_any *any1, *any2; |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 2262 | struct lyd_node_opaq *opaq1, *opaq2; |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 2263 | size_t len1, len2; |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 2264 | |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 2265 | if (!node1 || !node2) { |
| 2266 | if (node1 == node2) { |
| 2267 | return LY_SUCCESS; |
| 2268 | } else { |
| 2269 | return LY_ENOT; |
| 2270 | } |
| 2271 | } |
| 2272 | |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 2273 | if ((LYD_CTX(node1) != LYD_CTX(node2)) || (node1->schema != node2->schema)) { |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 2274 | return LY_ENOT; |
| 2275 | } |
| 2276 | |
| 2277 | if (node1->hash != node2->hash) { |
| 2278 | return LY_ENOT; |
| 2279 | } |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 2280 | /* equal hashes do not mean equal nodes, they can be just in collision (or both be 0) so the nodes must be checked explicitly */ |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 2281 | |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 2282 | if (!node1->schema) { |
| 2283 | opaq1 = (struct lyd_node_opaq *)node1; |
| 2284 | opaq2 = (struct lyd_node_opaq *)node2; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 2285 | if ((opaq1->name != opaq2->name) || (opaq1->format != opaq2->format) || (opaq1->prefix.module_ns != opaq2->prefix.module_ns)) { |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 2286 | return LY_ENOT; |
| 2287 | } |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 2288 | switch (opaq1->format) { |
| 2289 | case LYD_XML: |
| 2290 | if (lyxml_value_compare(opaq1->value, opaq1->val_prefs, opaq2->value, opaq2->val_prefs)) { |
| 2291 | return LY_ENOT; |
| 2292 | } |
| 2293 | break; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 2294 | case LYD_JSON: |
| 2295 | /* prefixes in JSON are unique, so it is not necessary to canonize the values */ |
| 2296 | if (strcmp(opaq1->value, opaq2->value)) { |
| 2297 | return LY_ENOT; |
| 2298 | } |
| 2299 | break; |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 2300 | case LYD_LYB: |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 2301 | case LYD_UNKNOWN: |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 2302 | /* not allowed */ |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 2303 | LOGINT(LYD_CTX(node1)); |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 2304 | return LY_EINT; |
| 2305 | } |
| 2306 | if (options & LYD_COMPARE_FULL_RECURSION) { |
| 2307 | iter1 = opaq1->child; |
| 2308 | iter2 = opaq2->child; |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 2309 | goto all_children_compare; |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 2310 | } |
| 2311 | return LY_SUCCESS; |
| 2312 | } else { |
| 2313 | switch (node1->schema->nodetype) { |
| 2314 | case LYS_LEAF: |
| 2315 | case LYS_LEAFLIST: |
| 2316 | if (options & LYD_COMPARE_DEFAULTS) { |
| 2317 | if ((node1->flags & LYD_DEFAULT) != (node2->flags & LYD_DEFAULT)) { |
| 2318 | return LY_ENOT; |
| 2319 | } |
| 2320 | } |
| 2321 | |
Michal Vasko | 1a2bf2e | 2020-06-16 13:07:48 +0200 | [diff] [blame] | 2322 | term1 = (struct lyd_node_term *)node1; |
| 2323 | term2 = (struct lyd_node_term *)node2; |
| 2324 | if (term1->value.realtype != term2->value.realtype) { |
| 2325 | return LY_ENOT; |
| 2326 | } |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 2327 | |
Michal Vasko | 1a2bf2e | 2020-06-16 13:07:48 +0200 | [diff] [blame] | 2328 | return term1->value.realtype->plugin->compare(&term1->value, &term2->value); |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 2329 | case LYS_CONTAINER: |
| 2330 | if (options & LYD_COMPARE_DEFAULTS) { |
| 2331 | if ((node1->flags & LYD_DEFAULT) != (node2->flags & LYD_DEFAULT)) { |
| 2332 | return LY_ENOT; |
| 2333 | } |
| 2334 | } |
| 2335 | if (options & LYD_COMPARE_FULL_RECURSION) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 2336 | iter1 = ((struct lyd_node_inner *)node1)->child; |
| 2337 | iter2 = ((struct lyd_node_inner *)node2)->child; |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 2338 | goto all_children_compare; |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 2339 | } |
| 2340 | return LY_SUCCESS; |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 2341 | case LYS_RPC: |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 2342 | case LYS_ACTION: |
| 2343 | if (options & LYD_COMPARE_FULL_RECURSION) { |
| 2344 | /* TODO action/RPC |
| 2345 | goto all_children_compare; |
| 2346 | */ |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 2347 | } |
| 2348 | return LY_SUCCESS; |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 2349 | case LYS_NOTIF: |
| 2350 | if (options & LYD_COMPARE_FULL_RECURSION) { |
| 2351 | /* TODO Notification |
| 2352 | goto all_children_compare; |
| 2353 | */ |
| 2354 | } |
| 2355 | return LY_SUCCESS; |
| 2356 | case LYS_LIST: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 2357 | iter1 = ((struct lyd_node_inner *)node1)->child; |
| 2358 | iter2 = ((struct lyd_node_inner *)node2)->child; |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 2359 | |
| 2360 | if (!(node1->schema->flags & LYS_KEYLESS) && !(options & LYD_COMPARE_FULL_RECURSION)) { |
| 2361 | /* lists with keys, their equivalence is based on their keys */ |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 2362 | for (struct lysc_node *key = ((struct lysc_node_list *)node1->schema)->child; |
Michal Vasko | c57d9a9 | 2020-06-23 13:28:27 +0200 | [diff] [blame] | 2363 | key && (key->flags & LYS_KEY); |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 2364 | key = key->next) { |
Michal Vasko | 8f359bf | 2020-07-28 10:41:15 +0200 | [diff] [blame] | 2365 | if (lyd_compare_single(iter1, iter2, options)) { |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 2366 | return LY_ENOT; |
| 2367 | } |
| 2368 | iter1 = iter1->next; |
| 2369 | iter2 = iter2->next; |
| 2370 | } |
| 2371 | } else { |
| 2372 | /* lists without keys, their equivalence is based on equivalence of all the children (both direct and indirect) */ |
| 2373 | |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 2374 | all_children_compare: |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 2375 | if (!iter1 && !iter2) { |
| 2376 | /* no children, nothing to compare */ |
| 2377 | return LY_SUCCESS; |
| 2378 | } |
| 2379 | |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 2380 | for ( ; iter1 && iter2; iter1 = iter1->next, iter2 = iter2->next) { |
Michal Vasko | 8f359bf | 2020-07-28 10:41:15 +0200 | [diff] [blame] | 2381 | if (lyd_compare_single(iter1, iter2, options | LYD_COMPARE_FULL_RECURSION)) { |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 2382 | return LY_ENOT; |
| 2383 | } |
| 2384 | } |
| 2385 | if (iter1 || iter2) { |
| 2386 | return LY_ENOT; |
| 2387 | } |
| 2388 | } |
| 2389 | return LY_SUCCESS; |
| 2390 | case LYS_ANYXML: |
| 2391 | case LYS_ANYDATA: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 2392 | any1 = (struct lyd_node_any *)node1; |
| 2393 | any2 = (struct lyd_node_any *)node2; |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 2394 | |
| 2395 | if (any1->value_type != any2->value_type) { |
| 2396 | return LY_ENOT; |
| 2397 | } |
| 2398 | switch (any1->value_type) { |
| 2399 | case LYD_ANYDATA_DATATREE: |
| 2400 | iter1 = any1->value.tree; |
| 2401 | iter2 = any2->value.tree; |
| 2402 | goto all_children_compare; |
| 2403 | case LYD_ANYDATA_STRING: |
| 2404 | case LYD_ANYDATA_XML: |
| 2405 | case LYD_ANYDATA_JSON: |
| 2406 | len1 = strlen(any1->value.str); |
| 2407 | len2 = strlen(any2->value.str); |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 2408 | if ((len1 != len2) || strcmp(any1->value.str, any2->value.str)) { |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 2409 | return LY_ENOT; |
| 2410 | } |
| 2411 | return LY_SUCCESS; |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 2412 | case LYD_ANYDATA_LYB: |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 2413 | len1 = lyd_lyb_data_length(any1->value.mem); |
| 2414 | len2 = lyd_lyb_data_length(any2->value.mem); |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 2415 | if ((len1 != len2) || memcmp(any1->value.mem, any2->value.mem, len1)) { |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 2416 | return LY_ENOT; |
| 2417 | } |
| 2418 | return LY_SUCCESS; |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 2419 | } |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 2420 | } |
| 2421 | } |
| 2422 | |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 2423 | LOGINT(LYD_CTX(node1)); |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 2424 | return LY_EINT; |
| 2425 | } |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 2426 | |
Michal Vasko | 2172574 | 2020-06-29 11:49:25 +0200 | [diff] [blame] | 2427 | API LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 2428 | lyd_compare_siblings(const struct lyd_node *node1, const struct lyd_node *node2, uint32_t options) |
Michal Vasko | 8f359bf | 2020-07-28 10:41:15 +0200 | [diff] [blame] | 2429 | { |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 2430 | for ( ; node1 && node2; node1 = node1->next, node2 = node2->next) { |
Michal Vasko | 8f359bf | 2020-07-28 10:41:15 +0200 | [diff] [blame] | 2431 | LY_CHECK_RET(lyd_compare_single(node1, node2, options)); |
| 2432 | } |
| 2433 | |
Michal Vasko | 11a8143 | 2020-07-28 16:31:29 +0200 | [diff] [blame] | 2434 | if (node1 == node2) { |
| 2435 | return LY_SUCCESS; |
Michal Vasko | 8f359bf | 2020-07-28 10:41:15 +0200 | [diff] [blame] | 2436 | } |
Michal Vasko | 11a8143 | 2020-07-28 16:31:29 +0200 | [diff] [blame] | 2437 | return LY_ENOT; |
Michal Vasko | 8f359bf | 2020-07-28 10:41:15 +0200 | [diff] [blame] | 2438 | } |
| 2439 | |
| 2440 | API LY_ERR |
Michal Vasko | 2172574 | 2020-06-29 11:49:25 +0200 | [diff] [blame] | 2441 | lyd_compare_meta(const struct lyd_meta *meta1, const struct lyd_meta *meta2) |
| 2442 | { |
| 2443 | if (!meta1 || !meta2) { |
| 2444 | if (meta1 == meta2) { |
| 2445 | return LY_SUCCESS; |
| 2446 | } else { |
| 2447 | return LY_ENOT; |
| 2448 | } |
| 2449 | } |
| 2450 | |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 2451 | if ((LYD_CTX(meta1->parent) != LYD_CTX(meta2->parent)) || (meta1->annotation != meta2->annotation)) { |
Michal Vasko | 2172574 | 2020-06-29 11:49:25 +0200 | [diff] [blame] | 2452 | return LY_ENOT; |
| 2453 | } |
| 2454 | |
| 2455 | if (meta1->value.realtype != meta2->value.realtype) { |
| 2456 | return LY_ENOT; |
| 2457 | } |
| 2458 | |
| 2459 | return meta1->value.realtype->plugin->compare(&meta1->value, &meta2->value); |
| 2460 | } |
| 2461 | |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 2462 | /** |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 2463 | * @brief Duplicate a single node and connect it into @p parent (if present) or last of @p first siblings. |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 2464 | * |
| 2465 | * Ignores LYD_DUP_WITH_PARENTS and LYD_DUP_WITH_SIBLINGS which are supposed to be handled by lyd_dup(). |
Radek Krejci | f8b9517 | 2020-05-15 14:51:06 +0200 | [diff] [blame] | 2466 | * |
| 2467 | * @param[in] node Original node to duplicate |
| 2468 | * @param[in] parent Parent to insert into, NULL for top-level sibling. |
| 2469 | * @param[in,out] first First sibling, NULL if no top-level sibling exist yet. Can be also NULL if @p parent is set. |
| 2470 | * @param[in] options Bitmask of options flags, see @ref dupoptions. |
| 2471 | * @param[out] dup_p Pointer where the created duplicated node is placed (besides connecting it int @p parent / @p first sibling). |
| 2472 | * @return LY_ERR value |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 2473 | */ |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 2474 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 2475 | lyd_dup_r(const struct lyd_node *node, struct lyd_node *parent, struct lyd_node **first, uint32_t options, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 2476 | struct lyd_node **dup_p) |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 2477 | { |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 2478 | LY_ERR ret; |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 2479 | struct lyd_node *dup = NULL; |
Michal Vasko | 61551fa | 2020-07-09 15:45:45 +0200 | [diff] [blame] | 2480 | struct lyd_meta *meta; |
| 2481 | struct lyd_node_any *any; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 2482 | LY_ARRAY_COUNT_TYPE u; |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 2483 | |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 2484 | LY_CHECK_ARG_RET(NULL, node, LY_EINVAL); |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 2485 | |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 2486 | if (!node->schema) { |
| 2487 | dup = calloc(1, sizeof(struct lyd_node_opaq)); |
| 2488 | } else { |
| 2489 | switch (node->schema->nodetype) { |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 2490 | case LYS_RPC: |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 2491 | case LYS_ACTION: |
| 2492 | case LYS_NOTIF: |
| 2493 | case LYS_CONTAINER: |
| 2494 | case LYS_LIST: |
| 2495 | dup = calloc(1, sizeof(struct lyd_node_inner)); |
| 2496 | break; |
| 2497 | case LYS_LEAF: |
| 2498 | case LYS_LEAFLIST: |
| 2499 | dup = calloc(1, sizeof(struct lyd_node_term)); |
| 2500 | break; |
| 2501 | case LYS_ANYDATA: |
| 2502 | case LYS_ANYXML: |
| 2503 | dup = calloc(1, sizeof(struct lyd_node_any)); |
| 2504 | break; |
| 2505 | default: |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 2506 | LOGINT(LYD_CTX(node)); |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 2507 | ret = LY_EINT; |
| 2508 | goto error; |
| 2509 | } |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 2510 | } |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 2511 | LY_CHECK_ERR_GOTO(!dup, LOGMEM(LYD_CTX(node)); ret = LY_EMEM, error); |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 2512 | |
Michal Vasko | f6df0a0 | 2020-06-16 13:08:34 +0200 | [diff] [blame] | 2513 | if (options & LYD_DUP_WITH_FLAGS) { |
| 2514 | dup->flags = node->flags; |
| 2515 | } else { |
| 2516 | dup->flags = (node->flags & LYD_DEFAULT) | LYD_NEW; |
| 2517 | } |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 2518 | dup->schema = node->schema; |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 2519 | dup->prev = dup; |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 2520 | |
Michal Vasko | 25a3282 | 2020-07-09 15:48:22 +0200 | [diff] [blame] | 2521 | /* duplicate metadata */ |
| 2522 | if (!(options & LYD_DUP_NO_META)) { |
| 2523 | LY_LIST_FOR(node->meta, meta) { |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 2524 | LY_CHECK_GOTO(ret = lyd_dup_meta_single(meta, dup, NULL), error); |
Michal Vasko | 25a3282 | 2020-07-09 15:48:22 +0200 | [diff] [blame] | 2525 | } |
| 2526 | } |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 2527 | |
| 2528 | /* nodetype-specific work */ |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 2529 | if (!dup->schema) { |
| 2530 | struct lyd_node_opaq *opaq = (struct lyd_node_opaq *)dup; |
| 2531 | struct lyd_node_opaq *orig = (struct lyd_node_opaq *)node; |
| 2532 | struct lyd_node *child; |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 2533 | |
| 2534 | if (options & LYD_DUP_RECURSIVE) { |
| 2535 | /* duplicate all the children */ |
| 2536 | LY_LIST_FOR(orig->child, child) { |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 2537 | LY_CHECK_GOTO(ret = lyd_dup_r(child, dup, NULL, options, NULL), error); |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 2538 | } |
| 2539 | } |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 2540 | LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), orig->name, 0, &opaq->name), error); |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 2541 | opaq->format = orig->format; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 2542 | if (orig->prefix.id) { |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 2543 | LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), orig->prefix.id, 0, &opaq->prefix.id), error); |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 2544 | } |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 2545 | LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), orig->prefix.module_ns, 0, &opaq->prefix.module_ns), error); |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 2546 | if (orig->val_prefs) { |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 2547 | LY_ARRAY_CREATE_GOTO(LYD_CTX(node), opaq->val_prefs, LY_ARRAY_COUNT(orig->val_prefs), ret, error); |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 2548 | LY_ARRAY_FOR(orig->val_prefs, u) { |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 2549 | LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), orig->val_prefs[u].id, 0, &opaq->val_prefs[u].id), error); |
| 2550 | LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), orig->val_prefs[u].module_ns, 0, &opaq->val_prefs[u].module_ns), error); |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 2551 | LY_ARRAY_INCREMENT(opaq->val_prefs); |
| 2552 | } |
| 2553 | } |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 2554 | LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), orig->value, 0, &opaq->value), error); |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 2555 | opaq->ctx = orig->ctx; |
| 2556 | } else if (dup->schema->nodetype & LYD_NODE_TERM) { |
| 2557 | struct lyd_node_term *term = (struct lyd_node_term *)dup; |
| 2558 | struct lyd_node_term *orig = (struct lyd_node_term *)node; |
| 2559 | |
| 2560 | term->hash = orig->hash; |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 2561 | LY_CHECK_ERR_GOTO(orig->value.realtype->plugin->duplicate(LYD_CTX(node), &orig->value, &term->value), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 2562 | LOGERR(LYD_CTX(node), LY_EINT, "Value duplication failed."); ret = LY_EINT, error); |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 2563 | } else if (dup->schema->nodetype & LYD_NODE_INNER) { |
| 2564 | struct lyd_node_inner *orig = (struct lyd_node_inner *)node; |
| 2565 | struct lyd_node *child; |
| 2566 | |
| 2567 | if (options & LYD_DUP_RECURSIVE) { |
| 2568 | /* duplicate all the children */ |
| 2569 | LY_LIST_FOR(orig->child, child) { |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 2570 | LY_CHECK_GOTO(ret = lyd_dup_r(child, dup, NULL, options, NULL), error); |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 2571 | } |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 2572 | } else if ((dup->schema->nodetype == LYS_LIST) && !(dup->schema->flags & LYS_KEYLESS)) { |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 2573 | /* always duplicate keys of a list */ |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 2574 | child = orig->child; |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 2575 | for (struct lysc_node *key = ((struct lysc_node_list *)dup->schema)->child; |
Michal Vasko | c57d9a9 | 2020-06-23 13:28:27 +0200 | [diff] [blame] | 2576 | key && (key->flags & LYS_KEY); |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 2577 | key = key->next) { |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 2578 | if (!child) { |
| 2579 | /* possibly not keys are present in filtered tree */ |
| 2580 | break; |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 2581 | } else if (child->schema != key) { |
| 2582 | /* possibly not all keys are present in filtered tree, |
| 2583 | * but there can be also some non-key nodes */ |
| 2584 | continue; |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 2585 | } |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 2586 | LY_CHECK_GOTO(ret = lyd_dup_r(child, dup, NULL, options, NULL), error); |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 2587 | child = child->next; |
| 2588 | } |
| 2589 | } |
| 2590 | lyd_hash(dup); |
| 2591 | } else if (dup->schema->nodetype & LYD_NODE_ANY) { |
Michal Vasko | 61551fa | 2020-07-09 15:45:45 +0200 | [diff] [blame] | 2592 | dup->hash = node->hash; |
| 2593 | any = (struct lyd_node_any *)node; |
| 2594 | LY_CHECK_GOTO(ret = lyd_any_copy_value(dup, &any->value, any->value_type), error); |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 2595 | } |
| 2596 | |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 2597 | /* insert */ |
| 2598 | lyd_insert_node(parent, first, dup); |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 2599 | |
| 2600 | if (dup_p) { |
| 2601 | *dup_p = dup; |
| 2602 | } |
| 2603 | return LY_SUCCESS; |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 2604 | |
| 2605 | error: |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 2606 | lyd_free_tree(dup); |
| 2607 | return ret; |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 2608 | } |
| 2609 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 2610 | static LY_ERR |
| 2611 | lyd_dup_get_local_parent(const struct lyd_node *node, const struct lyd_node_inner *parent, struct lyd_node **dup_parent, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 2612 | struct lyd_node_inner **local_parent) |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 2613 | { |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 2614 | const struct lyd_node_inner *orig_parent, *iter; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 2615 | ly_bool repeat = 1; |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 2616 | |
| 2617 | *dup_parent = NULL; |
| 2618 | *local_parent = NULL; |
| 2619 | |
| 2620 | for (orig_parent = node->parent; repeat && orig_parent; orig_parent = orig_parent->parent) { |
| 2621 | if (parent && (parent->schema == orig_parent->schema)) { |
| 2622 | /* stop creating parents, connect what we have into the provided parent */ |
| 2623 | iter = parent; |
| 2624 | repeat = 0; |
| 2625 | } else { |
| 2626 | iter = NULL; |
| 2627 | LY_CHECK_RET(lyd_dup_r((struct lyd_node *)orig_parent, NULL, (struct lyd_node **)&iter, 0, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 2628 | (struct lyd_node **)&iter)); |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 2629 | } |
| 2630 | if (!*local_parent) { |
| 2631 | *local_parent = (struct lyd_node_inner *)iter; |
| 2632 | } |
| 2633 | if (iter->child) { |
| 2634 | /* 1) list - add after keys |
| 2635 | * 2) provided parent with some children */ |
| 2636 | iter->child->prev->next = *dup_parent; |
| 2637 | if (*dup_parent) { |
| 2638 | (*dup_parent)->prev = iter->child->prev; |
| 2639 | iter->child->prev = *dup_parent; |
| 2640 | } |
| 2641 | } else { |
| 2642 | ((struct lyd_node_inner *)iter)->child = *dup_parent; |
| 2643 | } |
| 2644 | if (*dup_parent) { |
| 2645 | (*dup_parent)->parent = (struct lyd_node_inner *)iter; |
| 2646 | } |
| 2647 | *dup_parent = (struct lyd_node *)iter; |
| 2648 | } |
| 2649 | |
| 2650 | if (repeat && parent) { |
| 2651 | /* given parent and created parents chain actually do not interconnect */ |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 2652 | LOGERR(LYD_CTX(node), LY_EINVAL, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 2653 | "Invalid argument parent (%s()) - does not interconnect with the created node's parents chain.", __func__); |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 2654 | return LY_EINVAL; |
| 2655 | } |
| 2656 | |
| 2657 | return LY_SUCCESS; |
| 2658 | } |
| 2659 | |
| 2660 | static LY_ERR |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 2661 | lyd_dup(const struct lyd_node *node, struct lyd_node_inner *parent, uint32_t options, ly_bool nosiblings, struct lyd_node **dup) |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 2662 | { |
| 2663 | LY_ERR rc; |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 2664 | const struct lyd_node *orig; /* original node to be duplicated */ |
| 2665 | struct lyd_node *first = NULL; /* the first duplicated node, this is returned */ |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 2666 | struct lyd_node *top = NULL; /* the most higher created node */ |
| 2667 | struct lyd_node_inner *local_parent = NULL; /* the direct parent node for the duplicated node(s) */ |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 2668 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 2669 | LY_CHECK_ARG_RET(NULL, node, LY_EINVAL); |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 2670 | |
| 2671 | if (options & LYD_DUP_WITH_PARENTS) { |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 2672 | LY_CHECK_GOTO(rc = lyd_dup_get_local_parent(node, parent, &top, &local_parent), error); |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 2673 | } else { |
| 2674 | local_parent = parent; |
| 2675 | } |
| 2676 | |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 2677 | LY_LIST_FOR(node, orig) { |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 2678 | /* if there is no local parent, it will be inserted into first */ |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 2679 | LY_CHECK_GOTO(rc = lyd_dup_r(orig, (struct lyd_node *)local_parent, &first, options, first ? NULL : &first), error); |
| 2680 | if (nosiblings) { |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 2681 | break; |
| 2682 | } |
| 2683 | } |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 2684 | |
| 2685 | /* rehash if needed */ |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 2686 | for ( ; local_parent; local_parent = local_parent->parent) { |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 2687 | if ((local_parent->schema->nodetype == LYS_LIST) && (local_parent->schema->flags & LYS_KEYLESS)) { |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 2688 | lyd_hash((struct lyd_node *)local_parent); |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 2689 | } |
| 2690 | } |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 2691 | |
| 2692 | if (dup) { |
| 2693 | *dup = first; |
| 2694 | } |
| 2695 | return LY_SUCCESS; |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 2696 | |
| 2697 | error: |
| 2698 | if (top) { |
| 2699 | lyd_free_tree(top); |
| 2700 | } else { |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 2701 | lyd_free_siblings(first); |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 2702 | } |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 2703 | return rc; |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 2704 | } |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 2705 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 2706 | API LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 2707 | lyd_dup_single(const struct lyd_node *node, struct lyd_node_inner *parent, uint32_t options, struct lyd_node **dup) |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 2708 | { |
| 2709 | return lyd_dup(node, parent, options, 1, dup); |
| 2710 | } |
| 2711 | |
| 2712 | API LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 2713 | lyd_dup_siblings(const struct lyd_node *node, struct lyd_node_inner *parent, uint32_t options, struct lyd_node **dup) |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 2714 | { |
| 2715 | return lyd_dup(node, parent, options, 0, dup); |
| 2716 | } |
| 2717 | |
| 2718 | API LY_ERR |
| 2719 | lyd_dup_meta_single(const struct lyd_meta *meta, struct lyd_node *node, struct lyd_meta **dup) |
Michal Vasko | 25a3282 | 2020-07-09 15:48:22 +0200 | [diff] [blame] | 2720 | { |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 2721 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 25a3282 | 2020-07-09 15:48:22 +0200 | [diff] [blame] | 2722 | struct lyd_meta *mt, *last; |
| 2723 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 2724 | LY_CHECK_ARG_RET(NULL, meta, node, LY_EINVAL); |
Michal Vasko | 25a3282 | 2020-07-09 15:48:22 +0200 | [diff] [blame] | 2725 | |
| 2726 | /* create a copy */ |
| 2727 | mt = calloc(1, sizeof *mt); |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 2728 | LY_CHECK_ERR_RET(!mt, LOGMEM(LYD_CTX(node)), LY_EMEM); |
Michal Vasko | 25a3282 | 2020-07-09 15:48:22 +0200 | [diff] [blame] | 2729 | mt->annotation = meta->annotation; |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 2730 | ret = meta->value.realtype->plugin->duplicate(LYD_CTX(node), &meta->value, &mt->value); |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 2731 | LY_CHECK_ERR_GOTO(ret, LOGERR(LYD_CTX(node), LY_EINT, "Value duplication failed."), finish); |
| 2732 | LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), meta->name, 0, &mt->name), finish); |
Michal Vasko | 25a3282 | 2020-07-09 15:48:22 +0200 | [diff] [blame] | 2733 | |
| 2734 | /* insert as the last attribute */ |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 2735 | mt->parent = node; |
Michal Vasko | 25a3282 | 2020-07-09 15:48:22 +0200 | [diff] [blame] | 2736 | if (node->meta) { |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 2737 | for (last = node->meta; last->next; last = last->next) {} |
Michal Vasko | 25a3282 | 2020-07-09 15:48:22 +0200 | [diff] [blame] | 2738 | last->next = mt; |
| 2739 | } else { |
| 2740 | node->meta = mt; |
| 2741 | } |
| 2742 | |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 2743 | finish: |
| 2744 | if (ret) { |
| 2745 | lyd_free_meta_single(mt); |
| 2746 | } else if (dup) { |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 2747 | *dup = mt; |
| 2748 | } |
| 2749 | return LY_SUCCESS; |
Michal Vasko | 25a3282 | 2020-07-09 15:48:22 +0200 | [diff] [blame] | 2750 | } |
| 2751 | |
Michal Vasko | 4490d31 | 2020-06-16 13:08:55 +0200 | [diff] [blame] | 2752 | /** |
| 2753 | * @brief Merge a source sibling into target siblings. |
| 2754 | * |
| 2755 | * @param[in,out] first_trg First target sibling, is updated if top-level. |
| 2756 | * @param[in] parent_trg Target parent. |
| 2757 | * @param[in,out] sibling_src Source sibling to merge, set to NULL if spent. |
| 2758 | * @param[in] options Merge options. |
| 2759 | * @return LY_ERR value. |
| 2760 | */ |
| 2761 | static LY_ERR |
| 2762 | lyd_merge_sibling_r(struct lyd_node **first_trg, struct lyd_node *parent_trg, const struct lyd_node **sibling_src_p, |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 2763 | uint16_t options) |
Michal Vasko | 4490d31 | 2020-06-16 13:08:55 +0200 | [diff] [blame] | 2764 | { |
| 2765 | LY_ERR ret; |
| 2766 | const struct lyd_node *child_src, *tmp, *sibling_src; |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 2767 | struct lyd_node *match_trg, *dup_src, *elem; |
Michal Vasko | 4490d31 | 2020-06-16 13:08:55 +0200 | [diff] [blame] | 2768 | struct lysc_type *type; |
Michal Vasko | 4490d31 | 2020-06-16 13:08:55 +0200 | [diff] [blame] | 2769 | |
| 2770 | sibling_src = *sibling_src_p; |
| 2771 | if (sibling_src->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) { |
| 2772 | /* try to find the exact instance */ |
| 2773 | ret = lyd_find_sibling_first(*first_trg, sibling_src, &match_trg); |
| 2774 | } else { |
| 2775 | /* try to simply find the node, there cannot be more instances */ |
| 2776 | ret = lyd_find_sibling_val(*first_trg, sibling_src->schema, NULL, 0, &match_trg); |
| 2777 | } |
| 2778 | |
| 2779 | if (!ret) { |
| 2780 | /* node found, make sure even value matches for all node types */ |
Michal Vasko | 8f359bf | 2020-07-28 10:41:15 +0200 | [diff] [blame] | 2781 | if ((match_trg->schema->nodetype == LYS_LEAF) && lyd_compare_single(sibling_src, match_trg, LYD_COMPARE_DEFAULTS)) { |
Michal Vasko | 4490d31 | 2020-06-16 13:08:55 +0200 | [diff] [blame] | 2782 | /* since they are different, they cannot both be default */ |
| 2783 | assert(!(sibling_src->flags & LYD_DEFAULT) || !(match_trg->flags & LYD_DEFAULT)); |
| 2784 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 2785 | /* update value (or only LYD_DEFAULT flag) only if flag set or the source node is not default */ |
| 2786 | if ((options & LYD_MERGE_DEFAULTS) || !(sibling_src->flags & LYD_DEFAULT)) { |
Michal Vasko | 4490d31 | 2020-06-16 13:08:55 +0200 | [diff] [blame] | 2787 | type = ((struct lysc_node_leaf *)match_trg->schema)->type; |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 2788 | type->plugin->free(LYD_CTX(match_trg), &((struct lyd_node_term *)match_trg)->value); |
| 2789 | LY_CHECK_RET(type->plugin->duplicate(LYD_CTX(match_trg), &((struct lyd_node_term *)sibling_src)->value, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 2790 | &((struct lyd_node_term *)match_trg)->value)); |
Michal Vasko | 4490d31 | 2020-06-16 13:08:55 +0200 | [diff] [blame] | 2791 | |
| 2792 | /* copy flags and add LYD_NEW */ |
| 2793 | match_trg->flags = sibling_src->flags | LYD_NEW; |
| 2794 | } |
Michal Vasko | 8f359bf | 2020-07-28 10:41:15 +0200 | [diff] [blame] | 2795 | } else if ((match_trg->schema->nodetype & LYS_ANYDATA) && lyd_compare_single(sibling_src, match_trg, 0)) { |
Michal Vasko | 4c583e8 | 2020-07-17 12:16:14 +0200 | [diff] [blame] | 2796 | /* update value */ |
| 2797 | LY_CHECK_RET(lyd_any_copy_value(match_trg, &((struct lyd_node_any *)sibling_src)->value, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 2798 | ((struct lyd_node_any *)sibling_src)->value_type)); |
Michal Vasko | 4490d31 | 2020-06-16 13:08:55 +0200 | [diff] [blame] | 2799 | |
| 2800 | /* copy flags and add LYD_NEW */ |
| 2801 | match_trg->flags = sibling_src->flags | LYD_NEW; |
Michal Vasko | 4490d31 | 2020-06-16 13:08:55 +0200 | [diff] [blame] | 2802 | } else { |
| 2803 | /* check descendants, recursively */ |
Radek Krejci | a1c1e54 | 2020-09-29 16:06:52 +0200 | [diff] [blame] | 2804 | LY_LIST_FOR_SAFE(lyd_child_no_keys(sibling_src), tmp, child_src) { |
Michal Vasko | 4490d31 | 2020-06-16 13:08:55 +0200 | [diff] [blame] | 2805 | LY_CHECK_RET(lyd_merge_sibling_r(lyd_node_children_p(match_trg), match_trg, &child_src, options)); |
| 2806 | } |
| 2807 | } |
| 2808 | } else { |
| 2809 | /* node not found, merge it */ |
| 2810 | if (options & LYD_MERGE_DESTRUCT) { |
| 2811 | dup_src = (struct lyd_node *)sibling_src; |
| 2812 | lyd_unlink_tree(dup_src); |
| 2813 | /* spend it */ |
| 2814 | *sibling_src_p = NULL; |
| 2815 | } else { |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 2816 | LY_CHECK_RET(lyd_dup_single(sibling_src, NULL, LYD_DUP_RECURSIVE | LYD_DUP_WITH_FLAGS, &dup_src)); |
Michal Vasko | 4490d31 | 2020-06-16 13:08:55 +0200 | [diff] [blame] | 2817 | } |
| 2818 | |
| 2819 | /* set LYD_NEW for all the new nodes, required for validation */ |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 2820 | LYD_TREE_DFS_BEGIN(dup_src, elem) { |
Michal Vasko | 4490d31 | 2020-06-16 13:08:55 +0200 | [diff] [blame] | 2821 | elem->flags |= LYD_NEW; |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 2822 | LYD_TREE_DFS_END(dup_src, elem); |
Michal Vasko | 4490d31 | 2020-06-16 13:08:55 +0200 | [diff] [blame] | 2823 | } |
| 2824 | |
| 2825 | lyd_insert_node(parent_trg, first_trg, dup_src); |
| 2826 | } |
| 2827 | |
| 2828 | return LY_SUCCESS; |
| 2829 | } |
| 2830 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 2831 | static LY_ERR |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 2832 | lyd_merge(struct lyd_node **target, const struct lyd_node *source, uint16_t options, ly_bool nosiblings) |
Michal Vasko | 4490d31 | 2020-06-16 13:08:55 +0200 | [diff] [blame] | 2833 | { |
| 2834 | const struct lyd_node *sibling_src, *tmp; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 2835 | ly_bool first; |
Michal Vasko | 4490d31 | 2020-06-16 13:08:55 +0200 | [diff] [blame] | 2836 | |
| 2837 | LY_CHECK_ARG_RET(NULL, target, LY_EINVAL); |
| 2838 | |
| 2839 | if (!source) { |
| 2840 | /* nothing to merge */ |
| 2841 | return LY_SUCCESS; |
| 2842 | } |
| 2843 | |
| 2844 | if (lysc_data_parent((*target)->schema) || lysc_data_parent(source->schema)) { |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 2845 | LOGERR(LYD_CTX(source), LY_EINVAL, "Invalid arguments - can merge only 2 top-level subtrees (%s()).", __func__); |
Michal Vasko | 4490d31 | 2020-06-16 13:08:55 +0200 | [diff] [blame] | 2846 | return LY_EINVAL; |
| 2847 | } |
| 2848 | |
| 2849 | LY_LIST_FOR_SAFE(source, tmp, sibling_src) { |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 2850 | first = (sibling_src == source) ? 1 : 0; |
Michal Vasko | 4490d31 | 2020-06-16 13:08:55 +0200 | [diff] [blame] | 2851 | LY_CHECK_RET(lyd_merge_sibling_r(target, NULL, &sibling_src, options)); |
| 2852 | if (first && !sibling_src) { |
| 2853 | /* source was spent (unlinked), move to the next node */ |
| 2854 | source = tmp; |
| 2855 | } |
| 2856 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 2857 | if (nosiblings) { |
Michal Vasko | 4490d31 | 2020-06-16 13:08:55 +0200 | [diff] [blame] | 2858 | break; |
| 2859 | } |
| 2860 | } |
| 2861 | |
| 2862 | if (options & LYD_MERGE_DESTRUCT) { |
| 2863 | /* free any leftover source data that were not merged */ |
| 2864 | lyd_free_siblings((struct lyd_node *)source); |
| 2865 | } |
| 2866 | |
| 2867 | return LY_SUCCESS; |
| 2868 | } |
| 2869 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 2870 | API LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 2871 | lyd_merge_tree(struct lyd_node **target, const struct lyd_node *source, uint16_t options) |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 2872 | { |
| 2873 | return lyd_merge(target, source, options, 1); |
| 2874 | } |
| 2875 | |
| 2876 | API LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 2877 | lyd_merge_siblings(struct lyd_node **target, const struct lyd_node *source, uint16_t options) |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 2878 | { |
| 2879 | return lyd_merge(target, source, options, 0); |
| 2880 | } |
| 2881 | |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 2882 | static LY_ERR |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 2883 | lyd_path_str_enlarge(char **buffer, size_t *buflen, size_t reqlen, ly_bool is_static) |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 2884 | { |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 2885 | /* ending \0 */ |
| 2886 | ++reqlen; |
| 2887 | |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 2888 | if (reqlen > *buflen) { |
| 2889 | if (is_static) { |
| 2890 | return LY_EINCOMPLETE; |
| 2891 | } |
| 2892 | |
| 2893 | *buffer = ly_realloc(*buffer, reqlen * sizeof **buffer); |
| 2894 | if (!*buffer) { |
| 2895 | return LY_EMEM; |
| 2896 | } |
| 2897 | |
| 2898 | *buflen = reqlen; |
| 2899 | } |
| 2900 | |
| 2901 | return LY_SUCCESS; |
| 2902 | } |
| 2903 | |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 2904 | LY_ERR |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 2905 | lyd_path_list_predicate(const struct lyd_node *node, char **buffer, size_t *buflen, size_t *bufused, ly_bool is_static) |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 2906 | { |
| 2907 | const struct lyd_node *key; |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 2908 | size_t len; |
| 2909 | const char *val; |
| 2910 | char quot; |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 2911 | |
Radek Krejci | a1c1e54 | 2020-09-29 16:06:52 +0200 | [diff] [blame] | 2912 | for (key = lyd_child(node); key && (key->schema->flags & LYS_KEY); key = key->next) { |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 2913 | val = LYD_CANON_VALUE(key); |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 2914 | len = 1 + strlen(key->schema->name) + 2 + strlen(val) + 2; |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 2915 | LY_CHECK_RET(lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static)); |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 2916 | |
| 2917 | quot = '\''; |
| 2918 | if (strchr(val, '\'')) { |
| 2919 | quot = '"'; |
| 2920 | } |
| 2921 | *bufused += sprintf(*buffer + *bufused, "[%s=%c%s%c]", key->schema->name, quot, val, quot); |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 2922 | } |
| 2923 | |
| 2924 | return LY_SUCCESS; |
| 2925 | } |
| 2926 | |
| 2927 | /** |
| 2928 | * @brief Append leaf-list value predicate to path. |
| 2929 | * |
| 2930 | * @param[in] node Node to print. |
| 2931 | * @param[in,out] buffer Buffer to print to. |
| 2932 | * @param[in,out] buflen Current buffer length. |
| 2933 | * @param[in,out] bufused Current number of characters used in @p buffer. |
| 2934 | * @param[in] is_static Whether buffer is static or can be reallocated. |
| 2935 | * @return LY_ERR |
| 2936 | */ |
| 2937 | static LY_ERR |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 2938 | lyd_path_leaflist_predicate(const struct lyd_node *node, char **buffer, size_t *buflen, size_t *bufused, ly_bool is_static) |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 2939 | { |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 2940 | size_t len; |
| 2941 | const char *val; |
| 2942 | char quot; |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 2943 | |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 2944 | val = LYD_CANON_VALUE(node); |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 2945 | len = 4 + strlen(val) + 2; |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 2946 | LY_CHECK_RET(lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static)); |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 2947 | |
| 2948 | quot = '\''; |
| 2949 | if (strchr(val, '\'')) { |
| 2950 | quot = '"'; |
| 2951 | } |
| 2952 | *bufused += sprintf(*buffer + *bufused, "[.=%c%s%c]", quot, val, quot); |
| 2953 | |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 2954 | return LY_SUCCESS; |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 2955 | } |
| 2956 | |
| 2957 | /** |
| 2958 | * @brief Append node position (relative to its other instances) predicate to path. |
| 2959 | * |
| 2960 | * @param[in] node Node to print. |
| 2961 | * @param[in,out] buffer Buffer to print to. |
| 2962 | * @param[in,out] buflen Current buffer length. |
| 2963 | * @param[in,out] bufused Current number of characters used in @p buffer. |
| 2964 | * @param[in] is_static Whether buffer is static or can be reallocated. |
| 2965 | * @return LY_ERR |
| 2966 | */ |
| 2967 | static LY_ERR |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 2968 | lyd_path_position_predicate(const struct lyd_node *node, char **buffer, size_t *buflen, size_t *bufused, ly_bool is_static) |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 2969 | { |
| 2970 | const struct lyd_node *first, *iter; |
| 2971 | size_t len; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 2972 | uint64_t pos; |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 2973 | char *val = NULL; |
| 2974 | LY_ERR rc; |
| 2975 | |
| 2976 | if (node->parent) { |
| 2977 | first = node->parent->child; |
| 2978 | } else { |
Michal Vasko | e070bfe | 2020-08-31 12:27:25 +0200 | [diff] [blame] | 2979 | for (first = node; first->prev->next; first = first->prev) {} |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 2980 | } |
| 2981 | pos = 1; |
| 2982 | for (iter = first; iter != node; iter = iter->next) { |
| 2983 | if (iter->schema == node->schema) { |
| 2984 | ++pos; |
| 2985 | } |
| 2986 | } |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 2987 | if (asprintf(&val, "%" PRIu64, pos) == -1) { |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 2988 | return LY_EMEM; |
| 2989 | } |
| 2990 | |
| 2991 | len = 1 + strlen(val) + 1; |
| 2992 | rc = lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static); |
| 2993 | if (rc != LY_SUCCESS) { |
| 2994 | goto cleanup; |
| 2995 | } |
| 2996 | |
| 2997 | *bufused += sprintf(*buffer + *bufused, "[%s]", val); |
| 2998 | |
| 2999 | cleanup: |
| 3000 | free(val); |
| 3001 | return rc; |
| 3002 | } |
| 3003 | |
| 3004 | API char * |
| 3005 | lyd_path(const struct lyd_node *node, LYD_PATH_TYPE pathtype, char *buffer, size_t buflen) |
| 3006 | { |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3007 | ly_bool is_static = 0; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3008 | uint32_t i, depth; |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 3009 | size_t bufused = 0, len; |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 3010 | const struct lyd_node *iter; |
| 3011 | const struct lys_module *mod; |
Michal Vasko | 790b2bc | 2020-08-03 13:35:06 +0200 | [diff] [blame] | 3012 | LY_ERR rc = LY_SUCCESS; |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 3013 | |
| 3014 | LY_CHECK_ARG_RET(NULL, node, NULL); |
| 3015 | if (buffer) { |
| 3016 | LY_CHECK_ARG_RET(node->schema->module->ctx, buflen > 1, NULL); |
| 3017 | is_static = 1; |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 3018 | } else { |
| 3019 | buflen = 0; |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 3020 | } |
| 3021 | |
| 3022 | switch (pathtype) { |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 3023 | case LYD_PATH_LOG: |
Michal Vasko | 790b2bc | 2020-08-03 13:35:06 +0200 | [diff] [blame] | 3024 | case LYD_PATH_LOG_NO_LAST_PRED: |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 3025 | depth = 1; |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 3026 | for (iter = node; iter->parent; iter = (const struct lyd_node *)iter->parent) { |
| 3027 | ++depth; |
| 3028 | } |
| 3029 | |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 3030 | goto iter_print; |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 3031 | while (depth) { |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 3032 | /* find the right node */ |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 3033 | for (iter = node, i = 1; i < depth; iter = (const struct lyd_node *)iter->parent, ++i) {} |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 3034 | iter_print: |
| 3035 | /* print prefix and name */ |
| 3036 | mod = NULL; |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 3037 | if (iter->schema && (!iter->parent || (iter->schema->module != iter->parent->schema->module))) { |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 3038 | mod = iter->schema->module; |
| 3039 | } |
| 3040 | |
| 3041 | /* realloc string */ |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 3042 | len = 1 + (mod ? strlen(mod->name) + 1 : 0) + (iter->schema ? strlen(iter->schema->name) : strlen(((struct lyd_node_opaq *)iter)->name)); |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 3043 | rc = lyd_path_str_enlarge(&buffer, &buflen, bufused + len, is_static); |
| 3044 | if (rc != LY_SUCCESS) { |
| 3045 | break; |
| 3046 | } |
| 3047 | |
| 3048 | /* print next node */ |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 3049 | bufused += sprintf(buffer + bufused, "/%s%s%s", mod ? mod->name : "", mod ? ":" : "", |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 3050 | iter->schema ? iter->schema->name : ((struct lyd_node_opaq *)iter)->name); |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 3051 | |
Michal Vasko | 790b2bc | 2020-08-03 13:35:06 +0200 | [diff] [blame] | 3052 | /* do not always print the last (first) predicate */ |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 3053 | if (iter->schema && (bufused || (pathtype == LYD_PATH_LOG))) { |
Michal Vasko | 790b2bc | 2020-08-03 13:35:06 +0200 | [diff] [blame] | 3054 | switch (iter->schema->nodetype) { |
| 3055 | case LYS_LIST: |
| 3056 | if (iter->schema->flags & LYS_KEYLESS) { |
| 3057 | /* print its position */ |
| 3058 | rc = lyd_path_position_predicate(iter, &buffer, &buflen, &bufused, is_static); |
| 3059 | } else { |
| 3060 | /* print all list keys in predicates */ |
| 3061 | rc = lyd_path_list_predicate(iter, &buffer, &buflen, &bufused, is_static); |
| 3062 | } |
| 3063 | break; |
| 3064 | case LYS_LEAFLIST: |
| 3065 | if (iter->schema->flags & LYS_CONFIG_W) { |
| 3066 | /* print leaf-list value */ |
| 3067 | rc = lyd_path_leaflist_predicate(iter, &buffer, &buflen, &bufused, is_static); |
| 3068 | } else { |
| 3069 | /* print its position */ |
| 3070 | rc = lyd_path_position_predicate(iter, &buffer, &buflen, &bufused, is_static); |
| 3071 | } |
| 3072 | break; |
| 3073 | default: |
| 3074 | /* nothing to print more */ |
| 3075 | break; |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 3076 | } |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 3077 | } |
| 3078 | if (rc != LY_SUCCESS) { |
| 3079 | break; |
| 3080 | } |
| 3081 | |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 3082 | --depth; |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 3083 | } |
| 3084 | break; |
| 3085 | } |
| 3086 | |
| 3087 | return buffer; |
| 3088 | } |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 3089 | |
Michal Vasko | 25a3282 | 2020-07-09 15:48:22 +0200 | [diff] [blame] | 3090 | API struct lyd_meta * |
| 3091 | lyd_find_meta(const struct lyd_meta *first, const struct lys_module *module, const char *name) |
| 3092 | { |
| 3093 | struct lyd_meta *ret = NULL; |
| 3094 | const struct ly_ctx *ctx; |
| 3095 | const char *prefix, *tmp; |
| 3096 | char *str; |
| 3097 | size_t pref_len, name_len; |
| 3098 | |
| 3099 | LY_CHECK_ARG_RET(NULL, module || strchr(name, ':'), name, NULL); |
| 3100 | |
| 3101 | if (!first) { |
| 3102 | return NULL; |
| 3103 | } |
| 3104 | |
| 3105 | ctx = first->annotation->module->ctx; |
| 3106 | |
| 3107 | /* parse the name */ |
| 3108 | tmp = name; |
| 3109 | if (ly_parse_nodeid(&tmp, &prefix, &pref_len, &name, &name_len) || tmp[0]) { |
| 3110 | LOGERR(ctx, LY_EINVAL, "Metadata name \"%s\" is not valid.", name); |
| 3111 | return NULL; |
| 3112 | } |
| 3113 | |
| 3114 | /* find the module */ |
| 3115 | if (prefix) { |
| 3116 | str = strndup(prefix, pref_len); |
| 3117 | module = ly_ctx_get_module_latest(ctx, str); |
| 3118 | free(str); |
| 3119 | LY_CHECK_ERR_RET(!module, LOGERR(ctx, LY_EINVAL, "Module \"%.*s\" not found.", pref_len, prefix), NULL); |
| 3120 | } |
| 3121 | |
| 3122 | /* find the metadata */ |
| 3123 | LY_LIST_FOR(first, first) { |
| 3124 | if ((first->annotation->module == module) && !strcmp(first->name, name)) { |
| 3125 | ret = (struct lyd_meta *)first; |
| 3126 | break; |
| 3127 | } |
| 3128 | } |
| 3129 | |
| 3130 | return ret; |
| 3131 | } |
| 3132 | |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 3133 | API LY_ERR |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 3134 | lyd_find_sibling_first(const struct lyd_node *siblings, const struct lyd_node *target, struct lyd_node **match) |
| 3135 | { |
| 3136 | struct lyd_node **match_p; |
| 3137 | struct lyd_node_inner *parent; |
| 3138 | |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 3139 | LY_CHECK_ARG_RET(NULL, target, LY_EINVAL); |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 3140 | |
Michal Vasko | 62ed12d | 2020-05-21 10:08:25 +0200 | [diff] [blame] | 3141 | if (!siblings || (lysc_data_parent(siblings->schema) != lysc_data_parent(target->schema))) { |
| 3142 | /* no data or schema mismatch */ |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 3143 | if (match) { |
| 3144 | *match = NULL; |
| 3145 | } |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 3146 | return LY_ENOTFOUND; |
| 3147 | } |
| 3148 | |
| 3149 | /* find first sibling */ |
| 3150 | if (siblings->parent) { |
| 3151 | siblings = siblings->parent->child; |
| 3152 | } else { |
| 3153 | while (siblings->prev->next) { |
| 3154 | siblings = siblings->prev; |
| 3155 | } |
| 3156 | } |
| 3157 | |
| 3158 | parent = (struct lyd_node_inner *)siblings->parent; |
| 3159 | if (parent && parent->children_ht) { |
| 3160 | assert(target->hash); |
| 3161 | |
| 3162 | /* find by hash */ |
| 3163 | if (!lyht_find(parent->children_ht, &target, target->hash, (void **)&match_p)) { |
Michal Vasko | da85903 | 2020-07-14 12:20:14 +0200 | [diff] [blame] | 3164 | /* check even value when needed */ |
Michal Vasko | 8f359bf | 2020-07-28 10:41:15 +0200 | [diff] [blame] | 3165 | if (!(target->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) || !lyd_compare_single(target, *match_p, 0)) { |
Michal Vasko | da85903 | 2020-07-14 12:20:14 +0200 | [diff] [blame] | 3166 | siblings = *match_p; |
| 3167 | } else { |
| 3168 | siblings = NULL; |
| 3169 | } |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 3170 | } else { |
| 3171 | /* not found */ |
| 3172 | siblings = NULL; |
| 3173 | } |
| 3174 | } else { |
| 3175 | /* no children hash table */ |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 3176 | for ( ; siblings; siblings = siblings->next) { |
Michal Vasko | 8f359bf | 2020-07-28 10:41:15 +0200 | [diff] [blame] | 3177 | if (!lyd_compare_single(siblings, target, 0)) { |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 3178 | break; |
| 3179 | } |
| 3180 | } |
| 3181 | } |
| 3182 | |
| 3183 | if (!siblings) { |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 3184 | if (match) { |
| 3185 | *match = NULL; |
| 3186 | } |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 3187 | return LY_ENOTFOUND; |
| 3188 | } |
| 3189 | |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 3190 | if (match) { |
| 3191 | *match = (struct lyd_node *)siblings; |
| 3192 | } |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 3193 | return LY_SUCCESS; |
| 3194 | } |
| 3195 | |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3196 | /** |
| 3197 | * @brief Comparison callback to match schema node with a schema of a data node. |
| 3198 | * |
| 3199 | * @param[in] val1_p Pointer to the schema node |
| 3200 | * @param[in] val2_p Pointer to the data node |
| 3201 | * Implementation of ::values_equal_cb. |
| 3202 | */ |
| 3203 | static ly_bool |
| 3204 | lyd_hash_table_schema_val_equal(void *val1_p, void *val2_p, ly_bool UNUSED(mod), void *UNUSED(cb_data)) |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 3205 | { |
| 3206 | struct lysc_node *val1; |
| 3207 | struct lyd_node *val2; |
| 3208 | |
| 3209 | val1 = *((struct lysc_node **)val1_p); |
| 3210 | val2 = *((struct lyd_node **)val2_p); |
| 3211 | |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 3212 | if (val1 == val2->schema) { |
| 3213 | /* schema match is enough */ |
| 3214 | return 1; |
| 3215 | } else { |
| 3216 | return 0; |
| 3217 | } |
| 3218 | } |
| 3219 | |
| 3220 | static LY_ERR |
| 3221 | lyd_find_sibling_schema(const struct lyd_node *siblings, const struct lysc_node *schema, struct lyd_node **match) |
| 3222 | { |
| 3223 | struct lyd_node **match_p; |
| 3224 | struct lyd_node_inner *parent; |
| 3225 | uint32_t hash; |
| 3226 | values_equal_cb ht_cb; |
| 3227 | |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 3228 | assert(siblings && schema); |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 3229 | |
| 3230 | parent = (struct lyd_node_inner *)siblings->parent; |
| 3231 | if (parent && parent->children_ht) { |
| 3232 | /* calculate our hash */ |
| 3233 | hash = dict_hash_multi(0, schema->module->name, strlen(schema->module->name)); |
| 3234 | hash = dict_hash_multi(hash, schema->name, strlen(schema->name)); |
| 3235 | hash = dict_hash_multi(hash, NULL, 0); |
| 3236 | |
| 3237 | /* use special hash table function */ |
| 3238 | ht_cb = lyht_set_cb(parent->children_ht, lyd_hash_table_schema_val_equal); |
| 3239 | |
| 3240 | /* find by hash */ |
| 3241 | if (!lyht_find(parent->children_ht, &schema, hash, (void **)&match_p)) { |
| 3242 | siblings = *match_p; |
| 3243 | } else { |
| 3244 | /* not found */ |
| 3245 | siblings = NULL; |
| 3246 | } |
| 3247 | |
| 3248 | /* set the original hash table compare function back */ |
| 3249 | lyht_set_cb(parent->children_ht, ht_cb); |
| 3250 | } else { |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 3251 | /* find first sibling */ |
| 3252 | if (siblings->parent) { |
| 3253 | siblings = siblings->parent->child; |
| 3254 | } else { |
| 3255 | while (siblings->prev->next) { |
| 3256 | siblings = siblings->prev; |
| 3257 | } |
| 3258 | } |
| 3259 | |
| 3260 | /* search manually without hashes */ |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 3261 | for ( ; siblings; siblings = siblings->next) { |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 3262 | if (siblings->schema == schema) { |
| 3263 | /* schema match is enough */ |
| 3264 | break; |
| 3265 | } |
| 3266 | } |
| 3267 | } |
| 3268 | |
| 3269 | if (!siblings) { |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 3270 | if (match) { |
| 3271 | *match = NULL; |
| 3272 | } |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 3273 | return LY_ENOTFOUND; |
| 3274 | } |
| 3275 | |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 3276 | if (match) { |
| 3277 | *match = (struct lyd_node *)siblings; |
| 3278 | } |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 3279 | return LY_SUCCESS; |
| 3280 | } |
| 3281 | |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 3282 | API LY_ERR |
| 3283 | lyd_find_sibling_val(const struct lyd_node *siblings, const struct lysc_node *schema, const char *key_or_value, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 3284 | size_t val_len, struct lyd_node **match) |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 3285 | { |
| 3286 | LY_ERR rc; |
| 3287 | struct lyd_node *target = NULL; |
| 3288 | |
Michal Vasko | 4c583e8 | 2020-07-17 12:16:14 +0200 | [diff] [blame] | 3289 | LY_CHECK_ARG_RET(NULL, schema, !(schema->nodetype & (LYS_CHOICE | LYS_CASE)), LY_EINVAL); |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 3290 | |
Michal Vasko | 62ed12d | 2020-05-21 10:08:25 +0200 | [diff] [blame] | 3291 | if (!siblings || (lysc_data_parent(siblings->schema) != lysc_data_parent(schema))) { |
| 3292 | /* no data or schema mismatch */ |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 3293 | if (match) { |
| 3294 | *match = NULL; |
| 3295 | } |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 3296 | return LY_ENOTFOUND; |
| 3297 | } |
| 3298 | |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 3299 | if (key_or_value && !val_len) { |
| 3300 | val_len = strlen(key_or_value); |
| 3301 | } |
| 3302 | |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 3303 | if ((schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) && key_or_value) { |
| 3304 | /* create a data node and find the instance */ |
| 3305 | if (schema->nodetype == LYS_LEAFLIST) { |
| 3306 | /* target used attributes: schema, hash, value */ |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 3307 | rc = lyd_create_term(schema, key_or_value, val_len, NULL, LY_PREF_JSON, NULL, LYD_HINT_DATA, NULL, &target); |
| 3308 | LY_CHECK_RET(rc); |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 3309 | } else { |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 3310 | /* target used attributes: schema, hash, child (all keys) */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3311 | LY_CHECK_RET(lyd_create_list2(schema, key_or_value, val_len, &target)); |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 3312 | } |
| 3313 | |
| 3314 | /* find it */ |
| 3315 | rc = lyd_find_sibling_first(siblings, target, match); |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 3316 | } else { |
| 3317 | /* find the first schema node instance */ |
| 3318 | rc = lyd_find_sibling_schema(siblings, schema, match); |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 3319 | } |
| 3320 | |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 3321 | lyd_free_tree(target); |
| 3322 | return rc; |
| 3323 | } |
Michal Vasko | ccc0234 | 2020-05-21 10:09:21 +0200 | [diff] [blame] | 3324 | |
| 3325 | API LY_ERR |
| 3326 | lyd_find_xpath(const struct lyd_node *ctx_node, const char *xpath, struct ly_set **set) |
| 3327 | { |
| 3328 | LY_ERR ret = LY_SUCCESS; |
| 3329 | struct lyxp_set xp_set; |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3330 | struct lyxp_expr *exp = NULL; |
Michal Vasko | ccc0234 | 2020-05-21 10:09:21 +0200 | [diff] [blame] | 3331 | uint32_t i; |
| 3332 | |
| 3333 | LY_CHECK_ARG_RET(NULL, ctx_node, xpath, set, LY_EINVAL); |
| 3334 | |
| 3335 | memset(&xp_set, 0, sizeof xp_set); |
| 3336 | |
| 3337 | /* compile expression */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3338 | ret = lyxp_expr_parse((struct ly_ctx *)LYD_CTX(ctx_node), xpath, 0, 1, &exp); |
| 3339 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | ccc0234 | 2020-05-21 10:09:21 +0200 | [diff] [blame] | 3340 | |
| 3341 | /* evaluate expression */ |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 3342 | ret = lyxp_eval(exp, NULL, LY_PREF_JSON, NULL, ctx_node, ctx_node, &xp_set, 0); |
Michal Vasko | ccc0234 | 2020-05-21 10:09:21 +0200 | [diff] [blame] | 3343 | LY_CHECK_GOTO(ret, cleanup); |
| 3344 | |
| 3345 | /* allocate return set */ |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 3346 | ret = ly_set_new(set); |
| 3347 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | ccc0234 | 2020-05-21 10:09:21 +0200 | [diff] [blame] | 3348 | |
| 3349 | /* transform into ly_set */ |
| 3350 | if (xp_set.type == LYXP_SET_NODE_SET) { |
| 3351 | /* allocate memory for all the elements once (even though not all items must be elements but most likely will be) */ |
| 3352 | (*set)->objs = malloc(xp_set.used * sizeof *(*set)->objs); |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 3353 | LY_CHECK_ERR_GOTO(!(*set)->objs, LOGMEM(LYD_CTX(ctx_node)); ret = LY_EMEM, cleanup); |
Michal Vasko | ccc0234 | 2020-05-21 10:09:21 +0200 | [diff] [blame] | 3354 | (*set)->size = xp_set.used; |
| 3355 | |
| 3356 | for (i = 0; i < xp_set.used; ++i) { |
| 3357 | if (xp_set.val.nodes[i].type == LYXP_NODE_ELEM) { |
Radek Krejci | 3d92e44 | 2020-10-12 12:48:13 +0200 | [diff] [blame] | 3358 | ret = ly_set_add(*set, xp_set.val.nodes[i].node, 1, NULL); |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 3359 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | ccc0234 | 2020-05-21 10:09:21 +0200 | [diff] [blame] | 3360 | } |
| 3361 | } |
| 3362 | } |
| 3363 | |
| 3364 | cleanup: |
Michal Vasko | 0691d52 | 2020-05-21 13:21:47 +0200 | [diff] [blame] | 3365 | lyxp_set_free_content(&xp_set); |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 3366 | lyxp_expr_free((struct ly_ctx *)LYD_CTX(ctx_node), exp); |
Michal Vasko | ccc0234 | 2020-05-21 10:09:21 +0200 | [diff] [blame] | 3367 | return ret; |
| 3368 | } |
Radek Krejci | ca98914 | 2020-11-05 11:32:22 +0100 | [diff] [blame] | 3369 | |
| 3370 | API uint32_t |
| 3371 | lyd_list_pos(const struct lyd_node *instance) |
| 3372 | { |
| 3373 | const struct lyd_node *iter = NULL; |
| 3374 | uint32_t pos = 0; |
| 3375 | |
| 3376 | if (!instance || !(instance->schema->nodetype & (LYS_LIST | LYS_LEAFLIST))) { |
| 3377 | return 0; |
| 3378 | } |
| 3379 | |
| 3380 | /* data instances are ordered, so we can stop when we found instance of other schema node */ |
| 3381 | for (iter = instance; iter->schema == instance->schema; iter = iter->prev) { |
| 3382 | if (pos && iter->next == NULL) { |
| 3383 | /* overrun to the end of the siblings list */ |
| 3384 | break; |
| 3385 | } |
| 3386 | ++pos; |
| 3387 | } |
| 3388 | |
| 3389 | return pos; |
| 3390 | } |
Radek Krejci | 4233f9b | 2020-11-05 12:38:35 +0100 | [diff] [blame] | 3391 | |
| 3392 | API struct lyd_node * |
| 3393 | lyd_first_sibling(const struct lyd_node *node) |
| 3394 | { |
| 3395 | struct lyd_node *start; |
| 3396 | |
| 3397 | if (!node) { |
| 3398 | return NULL; |
| 3399 | } |
| 3400 | |
| 3401 | /* get the first sibling */ |
| 3402 | if (node->parent) { |
| 3403 | start = node->parent->child; |
| 3404 | } else { |
| 3405 | for (start = (struct lyd_node *)node; start->prev->next; start = start->prev); |
| 3406 | } |
| 3407 | |
| 3408 | return start; |
| 3409 | } |