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