Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 1 | /** |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 2 | * @file tree_data.c |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 3 | * @author Radek Krejci <rkrejci@cesnet.cz> |
Michal Vasko | 6cd9b6b | 2020-06-22 10:05:22 +0200 | [diff] [blame] | 4 | * @brief Data tree functions |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 5 | * |
Michal Vasko | 6cd9b6b | 2020-06-22 10:05:22 +0200 | [diff] [blame] | 6 | * Copyright (c) 2015 - 2020 CESNET, z.s.p.o. |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 7 | * |
| 8 | * This source code is licensed under BSD 3-Clause License (the "License"). |
| 9 | * You may not use this file except in compliance with the License. |
| 10 | * You may obtain a copy of the License at |
| 11 | * |
| 12 | * https://opensource.org/licenses/BSD-3-Clause |
| 13 | */ |
| 14 | |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 15 | #define _GNU_SOURCE |
| 16 | |
| 17 | #include "tree_data.h" |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 18 | |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 19 | #include <assert.h> |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 20 | #include <ctype.h> |
| 21 | #include <errno.h> |
| 22 | #include <fcntl.h> |
| 23 | #include <stdarg.h> |
| 24 | #include <stdint.h> |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 25 | #include <stdio.h> |
| 26 | #include <stdlib.h> |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 27 | #include <string.h> |
| 28 | #include <unistd.h> |
| 29 | |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 30 | #include "common.h" |
Michal Vasko | 5aa44c0 | 2020-06-29 11:47:02 +0200 | [diff] [blame] | 31 | #include "compat.h" |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 32 | #include "context.h" |
| 33 | #include "dict.h" |
Michal Vasko | a6669ba | 2020-08-06 16:14:26 +0200 | [diff] [blame] | 34 | #include "diff.h" |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 35 | #include "hash_table.h" |
Michal Vasko | afac782 | 2020-10-20 14:22:26 +0200 | [diff] [blame] | 36 | #include "in_internal.h" |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 37 | #include "log.h" |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 38 | #include "parser_data.h" |
| 39 | #include "parser_internal.h" |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 40 | #include "path.h" |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 41 | #include "plugins_exts.h" |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 42 | #include "plugins_exts_internal.h" |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 43 | #include "plugins_exts_metadata.h" |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 44 | #include "plugins_types.h" |
| 45 | #include "set.h" |
| 46 | #include "tree.h" |
| 47 | #include "tree_data_internal.h" |
| 48 | #include "tree_schema.h" |
| 49 | #include "tree_schema_internal.h" |
Michal Vasko | a6669ba | 2020-08-06 16:14:26 +0200 | [diff] [blame] | 50 | #include "validation.h" |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 51 | #include "xml.h" |
| 52 | #include "xpath.h" |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 53 | |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 54 | static LY_ERR lyd_find_sibling_schema(const struct lyd_node *siblings, const struct lysc_node *schema, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 55 | struct lyd_node **match); |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 56 | |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 57 | LY_ERR |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 58 | lyd_value_store(const struct ly_ctx *ctx, struct lyd_value *val, const struct lysc_type *type, const char *value, |
| 59 | size_t value_len, ly_bool *dynamic, LY_PREFIX_FORMAT format, void *prefix_data, uint32_t hints, |
| 60 | const struct lysc_node *ctx_node, ly_bool *incomplete, enum LY_VLOG_ELEM log_elem_type, const void *log_elem) |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 61 | { |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 62 | LY_ERR ret; |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 63 | struct ly_err_item *err = NULL; |
Michal Vasko | 25d6ad0 | 2020-10-22 12:20:22 +0200 | [diff] [blame] | 64 | uint32_t options = (dynamic && *dynamic ? LY_TYPE_STORE_DYNAMIC : 0); |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 65 | |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 66 | if (incomplete) { |
| 67 | *incomplete = 0; |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 68 | } |
| 69 | |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 70 | ret = type->plugin->store(ctx, type, value, value_len, options, format, prefix_data, hints, ctx_node, val, &err); |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 71 | if (ret == LY_EINCOMPLETE) { |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 72 | if (incomplete) { |
| 73 | *incomplete = 1; |
| 74 | } |
| 75 | } else if (ret) { |
| 76 | if (err) { |
| 77 | LOGVAL(ctx, log_elem_type, log_elem, err->vecode, err->msg); |
| 78 | ly_err_free(err); |
| 79 | } else { |
| 80 | LOGVAL(ctx, log_elem_type, log_elem, LYVE_OTHER, "Storing value \"%.*s\" failed.", (int)value_len, value); |
| 81 | } |
| 82 | return ret; |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 83 | } |
| 84 | |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 85 | if (dynamic) { |
| 86 | *dynamic = 0; |
| 87 | } |
| 88 | return LY_SUCCESS; |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 89 | } |
| 90 | |
Radek Krejci | 38d8536 | 2019-09-05 16:26:38 +0200 | [diff] [blame] | 91 | LY_ERR |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 92 | lyd_value_validate_incomplete(const struct ly_ctx *ctx, const struct lysc_type *type, struct lyd_value *val, |
| 93 | const struct lyd_node *ctx_node, const struct lyd_node *tree, enum LY_VLOG_ELEM log_elem_type, const void *log_elem) |
Radek Krejci | 38d8536 | 2019-09-05 16:26:38 +0200 | [diff] [blame] | 94 | { |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 95 | LY_ERR ret; |
Radek Krejci | 38d8536 | 2019-09-05 16:26:38 +0200 | [diff] [blame] | 96 | struct ly_err_item *err = NULL; |
Radek Krejci | 38d8536 | 2019-09-05 16:26:38 +0200 | [diff] [blame] | 97 | |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 98 | assert(type->plugin->validate); |
Michal Vasko | 8d54425 | 2020-03-02 10:19:52 +0100 | [diff] [blame] | 99 | |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 100 | ret = type->plugin->validate(ctx, type, ctx_node, tree, val, &err); |
| 101 | if (ret) { |
Radek Krejci | 38d8536 | 2019-09-05 16:26:38 +0200 | [diff] [blame] | 102 | if (err) { |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 103 | LOGVAL(ctx, log_elem_type, log_elem, err->vecode, err->msg); |
Radek Krejci | 38d8536 | 2019-09-05 16:26:38 +0200 | [diff] [blame] | 104 | ly_err_free(err); |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 105 | } else { |
| 106 | LOGVAL(ctx, log_elem_type, log_elem, LYVE_OTHER, "Resolving value \"%s\" failed.", val->canonical); |
Radek Krejci | 38d8536 | 2019-09-05 16:26:38 +0200 | [diff] [blame] | 107 | } |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 108 | return ret; |
Radek Krejci | 38d8536 | 2019-09-05 16:26:38 +0200 | [diff] [blame] | 109 | } |
| 110 | |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 111 | return LY_SUCCESS; |
Radek Krejci | 38d8536 | 2019-09-05 16:26:38 +0200 | [diff] [blame] | 112 | } |
| 113 | |
Michal Vasko | f937cfe | 2020-08-03 16:07:12 +0200 | [diff] [blame] | 114 | LY_ERR |
| 115 | _lys_value_validate(const struct ly_ctx *ctx, const struct lysc_node *node, const char *value, size_t value_len, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 116 | LY_PREFIX_FORMAT format, void *prefix_data) |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 117 | { |
| 118 | LY_ERR rc = LY_SUCCESS; |
| 119 | struct ly_err_item *err = NULL; |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 120 | struct lyd_value storage; |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 121 | struct lysc_type *type; |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 122 | |
| 123 | LY_CHECK_ARG_RET(ctx, node, value, LY_EINVAL); |
| 124 | |
| 125 | if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 126 | LOGARG(ctx, node); |
| 127 | return LY_EINVAL; |
| 128 | } |
| 129 | |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 130 | type = ((struct lysc_node_leaf *)node)->type; |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 131 | rc = type->plugin->store(ctx ? ctx : node->module->ctx, type, value, value_len, 0, format, prefix_data, |
| 132 | LYD_HINT_SCHEMA, node, &storage, &err); |
Radek Krejci | 73dead2 | 2019-07-11 16:46:16 +0200 | [diff] [blame] | 133 | if (rc == LY_EINCOMPLETE) { |
| 134 | /* actually success since we do not provide the context tree and call validation with |
| 135 | * LY_TYPE_OPTS_INCOMPLETE_DATA */ |
| 136 | rc = LY_SUCCESS; |
| 137 | } else if (rc && err) { |
| 138 | if (ctx) { |
| 139 | /* log only in case the ctx was provided as input parameter */ |
| 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); |
Michal Vasko | f7e16e2 | 2020-10-21 09:24:39 +0200 | [diff] [blame] | 641 | ly_path_predicates_free(schema->module->ctx, pred_type, 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 | ae87566 | 2020-10-21 10:33:17 +0200 | [diff] [blame] | 1240 | ++((struct lysc_type *)pred->value.realtype)->refcount; |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 1241 | } /* else we have opaq flag and the value is not valid, leavne no predicate and then create an opaque node */ |
| 1242 | } |
| 1243 | |
| 1244 | /* try to find any existing nodes in the path */ |
| 1245 | if (parent) { |
| 1246 | ret = ly_path_eval_partial(p, parent, &path_idx, &node); |
| 1247 | if (ret == LY_SUCCESS) { |
| 1248 | /* 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] | 1249 | if (!(options & LYD_NEW_PATH_UPDATE) && !(node->flags & LYD_DEFAULT)) { |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 1250 | LOGERR(ctx, LY_EEXIST, "Path \"%s\" already exists", path); |
| 1251 | ret = LY_EEXIST; |
| 1252 | goto cleanup; |
| 1253 | } |
| 1254 | |
| 1255 | /* update the existing node */ |
| 1256 | ret = lyd_new_path_update(node, value, value_type, &nparent, &nnode); |
| 1257 | goto cleanup; |
| 1258 | } else if (ret == LY_EINCOMPLETE) { |
| 1259 | /* some nodes were found, adjust the iterator to the next segment */ |
| 1260 | ++path_idx; |
| 1261 | } else if (ret == LY_ENOTFOUND) { |
| 1262 | /* 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] | 1263 | if (lysc_data_parent(p[LY_ARRAY_COUNT(p) - 1].node)) { |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 1264 | node = parent; |
| 1265 | } |
| 1266 | } else { |
| 1267 | /* error */ |
| 1268 | goto cleanup; |
| 1269 | } |
| 1270 | } |
| 1271 | |
| 1272 | /* create all the non-existing nodes in a loop */ |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 1273 | for ( ; path_idx < LY_ARRAY_COUNT(p); ++path_idx) { |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 1274 | cur_parent = node; |
| 1275 | schema = p[path_idx].node; |
| 1276 | |
| 1277 | switch (schema->nodetype) { |
| 1278 | case LYS_LIST: |
| 1279 | if (!(schema->flags & LYS_KEYLESS)) { |
Radek Krejci | 092e33c | 2020-10-12 15:33:10 +0200 | [diff] [blame] | 1280 | 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] | 1281 | /* creating opaque list without keys */ |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 1282 | LY_CHECK_GOTO(ret = lyd_create_opaq(ctx, schema->name, strlen(schema->name), NULL, 0, NULL, LYD_JSON, |
| 1283 | 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] | 1284 | cleanup); |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 1285 | } else { |
| 1286 | assert(p[path_idx].pred_type == LY_PATH_PREDTYPE_LIST); |
| 1287 | LY_CHECK_GOTO(ret = lyd_create_list(schema, p[path_idx].predicates, &node), cleanup); |
| 1288 | } |
| 1289 | break; |
| 1290 | } |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 1291 | /* fallthrough */ |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 1292 | case LYS_CONTAINER: |
| 1293 | case LYS_NOTIF: |
| 1294 | case LYS_RPC: |
| 1295 | case LYS_ACTION: |
| 1296 | LY_CHECK_GOTO(ret = lyd_create_inner(schema, &node), cleanup); |
| 1297 | break; |
| 1298 | case LYS_LEAFLIST: |
Radek Krejci | 092e33c | 2020-10-12 15:33:10 +0200 | [diff] [blame] | 1299 | 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] | 1300 | /* creating opaque leaf-list without value */ |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 1301 | LY_CHECK_GOTO(ret = lyd_create_opaq(ctx, schema->name, strlen(schema->name), NULL, 0, NULL, LYD_JSON, |
| 1302 | LYD_NODEHINT_LEAFLIST, NULL, NULL, 0, schema->module->name, strlen(schema->module->name), &node), |
| 1303 | cleanup); |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 1304 | } else { |
| 1305 | assert(p[path_idx].pred_type == LY_PATH_PREDTYPE_LEAFLIST); |
| 1306 | LY_CHECK_GOTO(ret = lyd_create_term2(schema, &p[path_idx].predicates[0].value, &node), cleanup); |
| 1307 | } |
| 1308 | break; |
| 1309 | case LYS_LEAF: |
| 1310 | /* make there is some value */ |
| 1311 | if (!value) { |
| 1312 | value = ""; |
| 1313 | } |
| 1314 | |
| 1315 | r = LY_SUCCESS; |
Radek Krejci | 092e33c | 2020-10-12 15:33:10 +0200 | [diff] [blame] | 1316 | if (options & LYD_NEW_PATH_OPAQ) { |
Michal Vasko | f937cfe | 2020-08-03 16:07:12 +0200 | [diff] [blame] | 1317 | r = lys_value_validate(NULL, schema, value, strlen(value)); |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 1318 | } |
| 1319 | if (!r) { |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 1320 | ret = lyd_create_term(schema, value, strlen(value), NULL, LY_PREF_JSON, NULL, LYD_HINT_DATA, NULL, &node); |
| 1321 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 1322 | } else { |
| 1323 | /* creating opaque leaf without value */ |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 1324 | 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] | 1325 | 0, NULL, NULL, 0, schema->module->name, |
| 1326 | strlen(schema->module->name), &node), cleanup); |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 1327 | } |
| 1328 | break; |
| 1329 | case LYS_ANYDATA: |
| 1330 | case LYS_ANYXML: |
| 1331 | LY_CHECK_GOTO(ret = lyd_create_any(schema, value, value_type, &node), cleanup); |
| 1332 | break; |
| 1333 | default: |
| 1334 | LOGINT(ctx); |
| 1335 | ret = LY_EINT; |
| 1336 | goto cleanup; |
| 1337 | } |
| 1338 | |
| 1339 | if (cur_parent) { |
| 1340 | /* connect to the parent */ |
| 1341 | lyd_insert_node(cur_parent, NULL, node); |
| 1342 | } else if (parent) { |
| 1343 | /* connect to top-level siblings */ |
| 1344 | lyd_insert_node(NULL, &parent, node); |
| 1345 | } |
| 1346 | |
| 1347 | /* update remembered nodes */ |
| 1348 | if (!nparent) { |
| 1349 | nparent = node; |
| 1350 | } |
| 1351 | nnode = node; |
| 1352 | } |
| 1353 | |
| 1354 | cleanup: |
| 1355 | lyxp_expr_free(ctx, exp); |
| 1356 | ly_path_free(ctx, p); |
| 1357 | if (!ret) { |
| 1358 | /* set out params only on success */ |
| 1359 | if (new_parent) { |
| 1360 | *new_parent = nparent; |
| 1361 | } |
| 1362 | if (new_node) { |
| 1363 | *new_node = nnode; |
| 1364 | } |
| 1365 | } |
| 1366 | return ret; |
| 1367 | } |
| 1368 | |
Michal Vasko | a6669ba | 2020-08-06 16:14:26 +0200 | [diff] [blame] | 1369 | LY_ERR |
| 1370 | 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] | 1371 | 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] | 1372 | struct lyd_node **diff) |
Michal Vasko | a6669ba | 2020-08-06 16:14:26 +0200 | [diff] [blame] | 1373 | { |
| 1374 | LY_ERR ret; |
| 1375 | const struct lysc_node *iter = NULL; |
| 1376 | struct lyd_node *node; |
| 1377 | struct lyd_value **dflts; |
| 1378 | LY_ARRAY_COUNT_TYPE u; |
| 1379 | |
| 1380 | assert(first && (parent || sparent || mod)); |
| 1381 | |
| 1382 | if (!sparent && parent) { |
| 1383 | sparent = parent->schema; |
| 1384 | } |
| 1385 | |
| 1386 | while ((iter = lys_getnext(iter, sparent, mod ? mod->compiled : NULL, LYS_GETNEXT_WITHCHOICE))) { |
| 1387 | if ((impl_opts & LYD_IMPLICIT_NO_STATE) && (iter->flags & LYS_CONFIG_R)) { |
| 1388 | continue; |
Michal Vasko | 44b19a1 | 2020-08-07 09:21:30 +0200 | [diff] [blame] | 1389 | } else if ((impl_opts & LYD_IMPLICIT_NO_CONFIG) && (iter->flags & LYS_CONFIG_W)) { |
| 1390 | continue; |
Michal Vasko | a6669ba | 2020-08-06 16:14:26 +0200 | [diff] [blame] | 1391 | } |
| 1392 | |
| 1393 | switch (iter->nodetype) { |
| 1394 | case LYS_CHOICE: |
| 1395 | if (((struct lysc_node_choice *)iter)->dflt && !lys_getnext_data(NULL, *first, NULL, iter, NULL)) { |
| 1396 | /* create default case data */ |
| 1397 | 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] | 1398 | NULL, node_types, node_when, impl_opts, diff)); |
Michal Vasko | a6669ba | 2020-08-06 16:14:26 +0200 | [diff] [blame] | 1399 | } |
| 1400 | break; |
| 1401 | case LYS_CONTAINER: |
| 1402 | if (!(iter->flags & LYS_PRESENCE) && lyd_find_sibling_val(*first, iter, NULL, 0, NULL)) { |
| 1403 | /* create default NP container */ |
| 1404 | LY_CHECK_RET(lyd_create_inner(iter, &node)); |
| 1405 | node->flags = LYD_DEFAULT; |
| 1406 | lyd_insert_node(parent, first, node); |
| 1407 | |
| 1408 | /* cannot be a NP container with when */ |
| 1409 | assert(!iter->when); |
| 1410 | |
| 1411 | /* create any default children */ |
| 1412 | 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] | 1413 | impl_opts, diff)); |
Michal Vasko | a6669ba | 2020-08-06 16:14:26 +0200 | [diff] [blame] | 1414 | } |
| 1415 | break; |
| 1416 | case LYS_LEAF: |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 1417 | if (!(impl_opts & LYD_IMPLICIT_NO_DEFAULTS) && ((struct lysc_node_leaf *)iter)->dflt && |
| 1418 | lyd_find_sibling_val(*first, iter, NULL, 0, NULL)) { |
Michal Vasko | a6669ba | 2020-08-06 16:14:26 +0200 | [diff] [blame] | 1419 | /* create default leaf */ |
| 1420 | ret = lyd_create_term2(iter, ((struct lysc_node_leaf *)iter)->dflt, &node); |
| 1421 | if (ret == LY_EINCOMPLETE) { |
| 1422 | if (node_types) { |
| 1423 | /* remember to resolve type */ |
Radek Krejci | 3d92e44 | 2020-10-12 12:48:13 +0200 | [diff] [blame] | 1424 | LY_CHECK_RET(ly_set_add(node_types, node, 1, NULL)); |
Michal Vasko | a6669ba | 2020-08-06 16:14:26 +0200 | [diff] [blame] | 1425 | } |
| 1426 | } else if (ret) { |
| 1427 | return ret; |
| 1428 | } |
| 1429 | node->flags = LYD_DEFAULT; |
| 1430 | lyd_insert_node(parent, first, node); |
| 1431 | |
| 1432 | if (iter->when && node_when) { |
| 1433 | /* remember to resolve when */ |
Radek Krejci | 3d92e44 | 2020-10-12 12:48:13 +0200 | [diff] [blame] | 1434 | LY_CHECK_RET(ly_set_add(node_when, node, 1, NULL)); |
Michal Vasko | a6669ba | 2020-08-06 16:14:26 +0200 | [diff] [blame] | 1435 | } |
| 1436 | if (diff) { |
| 1437 | /* add into diff */ |
| 1438 | LY_CHECK_RET(lyd_val_diff_add(node, LYD_DIFF_OP_CREATE, diff)); |
| 1439 | } |
| 1440 | } |
| 1441 | break; |
| 1442 | case LYS_LEAFLIST: |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 1443 | if (!(impl_opts & LYD_IMPLICIT_NO_DEFAULTS) && ((struct lysc_node_leaflist *)iter)->dflts && |
| 1444 | lyd_find_sibling_val(*first, iter, NULL, 0, NULL)) { |
Michal Vasko | a6669ba | 2020-08-06 16:14:26 +0200 | [diff] [blame] | 1445 | /* create all default leaf-lists */ |
| 1446 | dflts = ((struct lysc_node_leaflist *)iter)->dflts; |
| 1447 | LY_ARRAY_FOR(dflts, u) { |
| 1448 | ret = lyd_create_term2(iter, dflts[u], &node); |
| 1449 | if (ret == LY_EINCOMPLETE) { |
| 1450 | if (node_types) { |
| 1451 | /* remember to resolve type */ |
Radek Krejci | 3d92e44 | 2020-10-12 12:48:13 +0200 | [diff] [blame] | 1452 | LY_CHECK_RET(ly_set_add(node_types, node, 1, NULL)); |
Michal Vasko | a6669ba | 2020-08-06 16:14:26 +0200 | [diff] [blame] | 1453 | } |
| 1454 | } else if (ret) { |
| 1455 | return ret; |
| 1456 | } |
| 1457 | node->flags = LYD_DEFAULT; |
| 1458 | lyd_insert_node(parent, first, node); |
| 1459 | |
| 1460 | if (iter->when && node_when) { |
| 1461 | /* remember to resolve when */ |
Radek Krejci | 3d92e44 | 2020-10-12 12:48:13 +0200 | [diff] [blame] | 1462 | LY_CHECK_RET(ly_set_add(node_when, node, 1, NULL)); |
Michal Vasko | a6669ba | 2020-08-06 16:14:26 +0200 | [diff] [blame] | 1463 | } |
| 1464 | if (diff) { |
| 1465 | /* add into diff */ |
| 1466 | LY_CHECK_RET(lyd_val_diff_add(node, LYD_DIFF_OP_CREATE, diff)); |
| 1467 | } |
| 1468 | } |
| 1469 | } |
| 1470 | break; |
| 1471 | default: |
| 1472 | /* without defaults */ |
| 1473 | break; |
| 1474 | } |
| 1475 | } |
| 1476 | |
| 1477 | return LY_SUCCESS; |
| 1478 | } |
| 1479 | |
| 1480 | API LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 1481 | 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] | 1482 | { |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1483 | struct lyd_node *node; |
Michal Vasko | a6669ba | 2020-08-06 16:14:26 +0200 | [diff] [blame] | 1484 | LY_ERR ret = LY_SUCCESS; |
| 1485 | |
| 1486 | LY_CHECK_ARG_RET(NULL, tree, LY_EINVAL); |
| 1487 | if (diff) { |
| 1488 | *diff = NULL; |
| 1489 | } |
| 1490 | |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1491 | LYD_TREE_DFS_BEGIN(tree, node) { |
Michal Vasko | a6669ba | 2020-08-06 16:14:26 +0200 | [diff] [blame] | 1492 | /* skip added default nodes */ |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 1493 | if (((node->flags & (LYD_DEFAULT | LYD_NEW)) != (LYD_DEFAULT | LYD_NEW)) && |
| 1494 | (node->schema->nodetype & LYD_NODE_INNER)) { |
Michal Vasko | a6669ba | 2020-08-06 16:14:26 +0200 | [diff] [blame] | 1495 | 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] | 1496 | NULL, implicit_options, diff), cleanup); |
Michal Vasko | a6669ba | 2020-08-06 16:14:26 +0200 | [diff] [blame] | 1497 | } |
| 1498 | |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1499 | LYD_TREE_DFS_END(tree, node); |
Michal Vasko | a6669ba | 2020-08-06 16:14:26 +0200 | [diff] [blame] | 1500 | } |
| 1501 | |
| 1502 | cleanup: |
| 1503 | if (ret && diff) { |
| 1504 | lyd_free_all(*diff); |
| 1505 | *diff = NULL; |
| 1506 | } |
| 1507 | return ret; |
| 1508 | } |
| 1509 | |
| 1510 | API LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 1511 | 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] | 1512 | { |
| 1513 | const struct lys_module *mod; |
| 1514 | struct lyd_node *d = NULL; |
| 1515 | uint32_t i = 0; |
| 1516 | LY_ERR ret = LY_SUCCESS; |
| 1517 | |
| 1518 | LY_CHECK_ARG_RET(ctx, tree, *tree || ctx, LY_EINVAL); |
| 1519 | if (diff) { |
| 1520 | *diff = NULL; |
| 1521 | } |
| 1522 | if (!ctx) { |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 1523 | ctx = LYD_CTX(*tree); |
Michal Vasko | a6669ba | 2020-08-06 16:14:26 +0200 | [diff] [blame] | 1524 | } |
| 1525 | |
| 1526 | /* add nodes for each module one-by-one */ |
| 1527 | while ((mod = ly_ctx_get_module_iter(ctx, &i))) { |
| 1528 | if (!mod->implemented) { |
| 1529 | continue; |
| 1530 | } |
| 1531 | |
| 1532 | LY_CHECK_GOTO(ret = lyd_new_implicit_module(tree, mod, implicit_options, diff ? &d : NULL), cleanup); |
| 1533 | if (d) { |
| 1534 | /* merge into one diff */ |
| 1535 | lyd_insert_sibling(*diff, d, diff); |
| 1536 | |
| 1537 | d = NULL; |
| 1538 | } |
| 1539 | } |
| 1540 | |
| 1541 | cleanup: |
| 1542 | if (ret && diff) { |
| 1543 | lyd_free_all(*diff); |
| 1544 | *diff = NULL; |
| 1545 | } |
| 1546 | return ret; |
| 1547 | } |
| 1548 | |
| 1549 | API LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 1550 | 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] | 1551 | { |
| 1552 | struct lyd_node *root, *d = NULL; |
| 1553 | LY_ERR ret = LY_SUCCESS; |
| 1554 | |
| 1555 | LY_CHECK_ARG_RET(NULL, tree, module, LY_EINVAL); |
| 1556 | if (diff) { |
| 1557 | *diff = NULL; |
| 1558 | } |
| 1559 | |
| 1560 | /* add all top-level defaults for this module */ |
| 1561 | LY_CHECK_GOTO(ret = lyd_new_implicit_r(NULL, tree, NULL, module, NULL, NULL, implicit_options, diff), cleanup); |
| 1562 | |
| 1563 | /* process nested nodes */ |
| 1564 | LY_LIST_FOR(*tree, root) { |
| 1565 | /* skip added default nodes */ |
| 1566 | if ((root->flags & (LYD_DEFAULT | LYD_NEW)) != (LYD_DEFAULT | LYD_NEW)) { |
| 1567 | LY_CHECK_GOTO(ret = lyd_new_implicit_tree(root, implicit_options, diff ? &d : NULL), cleanup); |
| 1568 | |
| 1569 | if (d) { |
| 1570 | /* merge into one diff */ |
| 1571 | lyd_insert_sibling(*diff, d, diff); |
| 1572 | |
| 1573 | d = NULL; |
| 1574 | } |
| 1575 | } |
| 1576 | } |
| 1577 | |
| 1578 | cleanup: |
| 1579 | if (ret && diff) { |
| 1580 | lyd_free_all(*diff); |
| 1581 | *diff = NULL; |
| 1582 | } |
| 1583 | return ret; |
| 1584 | } |
| 1585 | |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 1586 | struct lyd_node * |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 1587 | 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] | 1588 | { |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 1589 | const struct lysc_node *schema, *sparent; |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 1590 | struct lyd_node *match = NULL; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 1591 | ly_bool found; |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 1592 | |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 1593 | assert(new_node); |
| 1594 | |
| 1595 | if (!first_sibling || !new_node->schema) { |
| 1596 | /* insert at the end, no next anchor */ |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 1597 | return NULL; |
| 1598 | } |
| 1599 | |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 1600 | if (first_sibling->parent && first_sibling->parent->children_ht) { |
| 1601 | /* find the anchor using hashes */ |
| 1602 | sparent = first_sibling->parent->schema; |
| 1603 | schema = lys_getnext(new_node->schema, sparent, NULL, 0); |
| 1604 | while (schema) { |
| 1605 | /* keep trying to find the first existing instance of the closest following schema sibling, |
| 1606 | * otherwise return NULL - inserting at the end */ |
| 1607 | if (!lyd_find_sibling_schema(first_sibling, schema, &match)) { |
| 1608 | break; |
| 1609 | } |
| 1610 | |
| 1611 | schema = lys_getnext(schema, sparent, NULL, 0); |
| 1612 | } |
| 1613 | } else { |
| 1614 | /* find the anchor without hashes */ |
| 1615 | match = (struct lyd_node *)first_sibling; |
| 1616 | if (!lysc_data_parent(new_node->schema)) { |
| 1617 | /* we are in top-level, skip all the data from preceding modules */ |
| 1618 | LY_LIST_FOR(match, match) { |
| 1619 | if (!match->schema || (strcmp(lyd_owner_module(match)->name, lyd_owner_module(new_node)->name) >= 0)) { |
| 1620 | break; |
| 1621 | } |
| 1622 | } |
| 1623 | } |
| 1624 | |
| 1625 | /* get the first schema sibling */ |
| 1626 | sparent = lysc_data_parent(new_node->schema); |
| 1627 | schema = lys_getnext(NULL, sparent, new_node->schema->module->compiled, 0); |
| 1628 | |
| 1629 | found = 0; |
| 1630 | LY_LIST_FOR(match, match) { |
| 1631 | if (!match->schema || (lyd_owner_module(match) != lyd_owner_module(new_node))) { |
| 1632 | /* we have found an opaque node, which must be at the end, so use it OR |
| 1633 | * modules do not match, so we must have traversed all the data from new_node module (if any), |
| 1634 | * we have found the first node of the next module, that is what we want */ |
| 1635 | break; |
| 1636 | } |
| 1637 | |
| 1638 | /* skip schema nodes until we find the instantiated one */ |
| 1639 | while (!found) { |
| 1640 | if (new_node->schema == schema) { |
| 1641 | /* we have found the schema of the new node, continue search to find the first |
| 1642 | * data node with a different schema (after our schema) */ |
| 1643 | found = 1; |
| 1644 | break; |
| 1645 | } |
| 1646 | if (match->schema == schema) { |
| 1647 | /* current node (match) is a data node still before the new node, continue search in data */ |
| 1648 | break; |
| 1649 | } |
| 1650 | schema = lys_getnext(schema, sparent, new_node->schema->module->compiled, 0); |
| 1651 | assert(schema); |
| 1652 | } |
| 1653 | |
| 1654 | if (found && (match->schema != new_node->schema)) { |
| 1655 | /* find the next node after we have found our node schema data instance */ |
| 1656 | break; |
| 1657 | } |
| 1658 | } |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 1659 | } |
| 1660 | |
| 1661 | return match; |
| 1662 | } |
| 1663 | |
| 1664 | /** |
| 1665 | * @brief Insert node after a sibling. |
| 1666 | * |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1667 | * Handles inserting into NP containers and key-less lists. |
| 1668 | * |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 1669 | * @param[in] sibling Sibling to insert after. |
| 1670 | * @param[in] node Node to insert. |
| 1671 | */ |
| 1672 | static void |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 1673 | lyd_insert_after_node(struct lyd_node *sibling, struct lyd_node *node) |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 1674 | { |
Michal Vasko | 0249f7c | 2020-03-05 16:36:40 +0100 | [diff] [blame] | 1675 | struct lyd_node_inner *par; |
| 1676 | |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 1677 | assert(!node->next && (node->prev == node)); |
| 1678 | |
| 1679 | node->next = sibling->next; |
| 1680 | node->prev = sibling; |
| 1681 | sibling->next = node; |
| 1682 | if (node->next) { |
| 1683 | /* sibling had a succeeding node */ |
| 1684 | node->next->prev = node; |
| 1685 | } else { |
| 1686 | /* sibling was last, find first sibling and change its prev */ |
| 1687 | if (sibling->parent) { |
| 1688 | sibling = sibling->parent->child; |
| 1689 | } else { |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 1690 | for ( ; sibling->prev->next != node; sibling = sibling->prev) {} |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 1691 | } |
| 1692 | sibling->prev = node; |
| 1693 | } |
| 1694 | node->parent = sibling->parent; |
Michal Vasko | 0249f7c | 2020-03-05 16:36:40 +0100 | [diff] [blame] | 1695 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1696 | for (par = node->parent; par; par = par->parent) { |
| 1697 | if ((par->flags & LYD_DEFAULT) && !(node->flags & LYD_DEFAULT)) { |
| 1698 | /* remove default flags from NP containers */ |
Michal Vasko | 0249f7c | 2020-03-05 16:36:40 +0100 | [diff] [blame] | 1699 | par->flags &= ~LYD_DEFAULT; |
| 1700 | } |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 1701 | 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] | 1702 | /* rehash key-less list */ |
| 1703 | lyd_hash((struct lyd_node *)par); |
| 1704 | } |
Michal Vasko | 0249f7c | 2020-03-05 16:36:40 +0100 | [diff] [blame] | 1705 | } |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 1706 | } |
| 1707 | |
| 1708 | /** |
| 1709 | * @brief Insert node before a sibling. |
| 1710 | * |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1711 | * Handles inserting into NP containers and key-less lists. |
| 1712 | * |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 1713 | * @param[in] sibling Sibling to insert before. |
| 1714 | * @param[in] node Node to insert. |
| 1715 | */ |
| 1716 | static void |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 1717 | lyd_insert_before_node(struct lyd_node *sibling, struct lyd_node *node) |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 1718 | { |
Michal Vasko | 0249f7c | 2020-03-05 16:36:40 +0100 | [diff] [blame] | 1719 | struct lyd_node_inner *par; |
| 1720 | |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 1721 | assert(!node->next && (node->prev == node)); |
| 1722 | |
| 1723 | node->next = sibling; |
| 1724 | /* covers situation of sibling being first */ |
| 1725 | node->prev = sibling->prev; |
| 1726 | sibling->prev = node; |
| 1727 | if (node->prev->next) { |
| 1728 | /* sibling had a preceding node */ |
| 1729 | node->prev->next = node; |
| 1730 | } else if (sibling->parent) { |
| 1731 | /* sibling was first and we must also change parent child pointer */ |
| 1732 | sibling->parent->child = node; |
| 1733 | } |
| 1734 | node->parent = sibling->parent; |
Michal Vasko | 0249f7c | 2020-03-05 16:36:40 +0100 | [diff] [blame] | 1735 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1736 | for (par = node->parent; par; par = par->parent) { |
| 1737 | if ((par->flags & LYD_DEFAULT) && !(node->flags & LYD_DEFAULT)) { |
| 1738 | /* remove default flags from NP containers */ |
Michal Vasko | 0249f7c | 2020-03-05 16:36:40 +0100 | [diff] [blame] | 1739 | par->flags &= ~LYD_DEFAULT; |
| 1740 | } |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 1741 | 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] | 1742 | /* rehash key-less list */ |
| 1743 | lyd_hash((struct lyd_node *)par); |
| 1744 | } |
Michal Vasko | 0249f7c | 2020-03-05 16:36:40 +0100 | [diff] [blame] | 1745 | } |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 1746 | } |
| 1747 | |
| 1748 | /** |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 1749 | * @brief Insert node as the first and only child of a parent. |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 1750 | * |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1751 | * Handles inserting into NP containers and key-less lists. |
| 1752 | * |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 1753 | * @param[in] parent Parent to insert into. |
| 1754 | * @param[in] node Node to insert. |
| 1755 | */ |
| 1756 | static void |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 1757 | lyd_insert_only_child(struct lyd_node *parent, struct lyd_node *node) |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 1758 | { |
| 1759 | struct lyd_node_inner *par; |
| 1760 | |
Radek Krejci | a1c1e54 | 2020-09-29 16:06:52 +0200 | [diff] [blame] | 1761 | assert(parent && !lyd_child(parent) && !node->next && (node->prev == node)); |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 1762 | assert(!parent->schema || (parent->schema->nodetype & LYD_NODE_INNER)); |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 1763 | |
| 1764 | par = (struct lyd_node_inner *)parent; |
| 1765 | |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 1766 | par->child = node; |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 1767 | node->parent = par; |
Michal Vasko | 0249f7c | 2020-03-05 16:36:40 +0100 | [diff] [blame] | 1768 | |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 1769 | for ( ; par; par = par->parent) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1770 | if ((par->flags & LYD_DEFAULT) && !(node->flags & LYD_DEFAULT)) { |
| 1771 | /* remove default flags from NP containers */ |
Michal Vasko | 0249f7c | 2020-03-05 16:36:40 +0100 | [diff] [blame] | 1772 | par->flags &= ~LYD_DEFAULT; |
| 1773 | } |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 1774 | 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] | 1775 | /* rehash key-less list */ |
| 1776 | lyd_hash((struct lyd_node *)par); |
| 1777 | } |
Michal Vasko | 0249f7c | 2020-03-05 16:36:40 +0100 | [diff] [blame] | 1778 | } |
Michal Vasko | 751cb4d | 2020-07-14 12:25:28 +0200 | [diff] [blame] | 1779 | } |
Michal Vasko | 0249f7c | 2020-03-05 16:36:40 +0100 | [diff] [blame] | 1780 | |
Michal Vasko | 751cb4d | 2020-07-14 12:25:28 +0200 | [diff] [blame] | 1781 | /** |
| 1782 | * @brief Learn whether a list instance has all the keys. |
| 1783 | * |
| 1784 | * @param[in] list List instance to check. |
| 1785 | * @return non-zero if all the keys were found, |
| 1786 | * @return 0 otherwise. |
| 1787 | */ |
| 1788 | static int |
| 1789 | lyd_insert_has_keys(const struct lyd_node *list) |
| 1790 | { |
| 1791 | const struct lyd_node *key; |
| 1792 | const struct lysc_node *skey = NULL; |
| 1793 | |
| 1794 | assert(list->schema->nodetype == LYS_LIST); |
Radek Krejci | a1c1e54 | 2020-09-29 16:06:52 +0200 | [diff] [blame] | 1795 | key = lyd_child(list); |
Michal Vasko | 751cb4d | 2020-07-14 12:25:28 +0200 | [diff] [blame] | 1796 | while ((skey = lys_getnext(skey, list->schema, NULL, 0)) && (skey->flags & LYS_KEY)) { |
| 1797 | if (!key || (key->schema != skey)) { |
| 1798 | /* key missing */ |
| 1799 | return 0; |
| 1800 | } |
| 1801 | |
| 1802 | key = key->next; |
| 1803 | } |
| 1804 | |
| 1805 | /* all keys found */ |
| 1806 | return 1; |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 1807 | } |
| 1808 | |
| 1809 | void |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 1810 | 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] | 1811 | { |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 1812 | struct lyd_node *anchor, *first_sibling; |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 1813 | |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 1814 | /* inserting list without its keys is not supported */ |
| 1815 | assert((parent || first_sibling_p) && node && (node->hash || !node->schema)); |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 1816 | |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 1817 | if (!parent && first_sibling_p && (*first_sibling_p) && (*first_sibling_p)->parent) { |
| 1818 | parent = (struct lyd_node *)(*first_sibling_p)->parent; |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 1819 | } |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 1820 | |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 1821 | /* get first sibling */ |
| 1822 | first_sibling = parent ? ((struct lyd_node_inner *)parent)->child : *first_sibling_p; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1823 | |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 1824 | /* find the anchor, our next node, so we can insert before it */ |
| 1825 | anchor = lyd_insert_get_next_anchor(first_sibling, node); |
| 1826 | if (anchor) { |
| 1827 | lyd_insert_before_node(anchor, node); |
| 1828 | } else if (first_sibling) { |
| 1829 | lyd_insert_after_node(first_sibling->prev, node); |
| 1830 | } else if (parent) { |
| 1831 | lyd_insert_only_child(parent, node); |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 1832 | } else { |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 1833 | *first_sibling_p = node; |
| 1834 | } |
| 1835 | |
| 1836 | /* insert into parent HT */ |
| 1837 | lyd_insert_hash(node); |
| 1838 | |
| 1839 | /* finish hashes for our parent, if needed and possible */ |
Michal Vasko | 9e8de2d | 2020-09-01 08:17:10 +0200 | [diff] [blame] | 1840 | 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] | 1841 | lyd_hash(parent); |
| 1842 | |
| 1843 | /* now we can insert even the list into its parent HT */ |
| 1844 | lyd_insert_hash(parent); |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 1845 | } |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 1846 | } |
| 1847 | |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 1848 | static LY_ERR |
| 1849 | lyd_insert_check_schema(const struct lysc_node *parent, const struct lysc_node *schema) |
| 1850 | { |
| 1851 | const struct lysc_node *par2; |
| 1852 | |
| 1853 | assert(schema); |
Michal Vasko | 62ed12d | 2020-05-21 10:08:25 +0200 | [diff] [blame] | 1854 | assert(!parent || !(parent->nodetype & (LYS_CASE | LYS_CHOICE))); |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 1855 | |
| 1856 | /* find schema parent */ |
Michal Vasko | 62ed12d | 2020-05-21 10:08:25 +0200 | [diff] [blame] | 1857 | par2 = lysc_data_parent(schema); |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 1858 | |
| 1859 | if (parent) { |
| 1860 | /* inner node */ |
| 1861 | if (par2 != parent) { |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 1862 | 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] | 1863 | parent->name); |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 1864 | return LY_EINVAL; |
| 1865 | } |
| 1866 | } else { |
| 1867 | /* top-level node */ |
| 1868 | if (par2) { |
Radek Krejci | f6d14cb | 2020-07-02 16:11:45 +0200 | [diff] [blame] | 1869 | 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] | 1870 | return LY_EINVAL; |
| 1871 | } |
| 1872 | } |
| 1873 | |
| 1874 | return LY_SUCCESS; |
| 1875 | } |
| 1876 | |
| 1877 | API LY_ERR |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 1878 | lyd_insert_child(struct lyd_node *parent, struct lyd_node *node) |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 1879 | { |
| 1880 | struct lyd_node *iter; |
| 1881 | |
Michal Vasko | 654bc85 | 2020-06-23 13:28:06 +0200 | [diff] [blame] | 1882 | 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] | 1883 | |
| 1884 | LY_CHECK_RET(lyd_insert_check_schema(parent->schema, node->schema)); |
| 1885 | |
| 1886 | if (node->schema->flags & LYS_KEY) { |
| 1887 | LOGERR(parent->schema->module->ctx, LY_EINVAL, "Cannot insert key \"%s\".", node->schema->name); |
| 1888 | return LY_EINVAL; |
| 1889 | } |
| 1890 | |
| 1891 | if (node->parent || node->prev->next) { |
| 1892 | lyd_unlink_tree(node); |
| 1893 | } |
| 1894 | |
| 1895 | while (node) { |
| 1896 | iter = node->next; |
| 1897 | lyd_unlink_tree(node); |
| 1898 | lyd_insert_node(parent, NULL, node); |
| 1899 | node = iter; |
| 1900 | } |
| 1901 | return LY_SUCCESS; |
| 1902 | } |
| 1903 | |
| 1904 | API LY_ERR |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 1905 | 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] | 1906 | { |
| 1907 | struct lyd_node *iter; |
| 1908 | |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 1909 | LY_CHECK_ARG_RET(NULL, node, LY_EINVAL); |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 1910 | |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 1911 | if (sibling) { |
| 1912 | 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] | 1913 | } |
| 1914 | |
| 1915 | if (node->parent || node->prev->next) { |
| 1916 | lyd_unlink_tree(node); |
| 1917 | } |
| 1918 | |
| 1919 | while (node) { |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 1920 | if (node->schema->flags & LYS_KEY) { |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 1921 | 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] | 1922 | return LY_EINVAL; |
| 1923 | } |
| 1924 | |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 1925 | iter = node->next; |
| 1926 | lyd_unlink_tree(node); |
| 1927 | lyd_insert_node(NULL, &sibling, node); |
| 1928 | node = iter; |
| 1929 | } |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 1930 | |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 1931 | if (first) { |
| 1932 | /* find the first sibling */ |
| 1933 | *first = sibling; |
| 1934 | while ((*first)->prev->next) { |
| 1935 | *first = (*first)->prev; |
Michal Vasko | 0249f7c | 2020-03-05 16:36:40 +0100 | [diff] [blame] | 1936 | } |
| 1937 | } |
| 1938 | |
| 1939 | return LY_SUCCESS; |
| 1940 | } |
| 1941 | |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 1942 | API LY_ERR |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 1943 | lyd_insert_before(struct lyd_node *sibling, struct lyd_node *node) |
| 1944 | { |
| 1945 | struct lyd_node *iter; |
| 1946 | |
| 1947 | LY_CHECK_ARG_RET(NULL, sibling, node, LY_EINVAL); |
| 1948 | |
Michal Vasko | 62ed12d | 2020-05-21 10:08:25 +0200 | [diff] [blame] | 1949 | 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] | 1950 | |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 1951 | 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] | 1952 | 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] | 1953 | return LY_EINVAL; |
| 1954 | } |
| 1955 | |
| 1956 | if (node->parent || node->prev->next) { |
| 1957 | lyd_unlink_tree(node); |
| 1958 | } |
| 1959 | |
| 1960 | /* insert in reverse order to get the original order */ |
| 1961 | node = node->prev; |
| 1962 | while (node) { |
| 1963 | iter = node->prev; |
| 1964 | lyd_unlink_tree(node); |
| 1965 | |
| 1966 | lyd_insert_before_node(sibling, node); |
Michal Vasko | 751cb4d | 2020-07-14 12:25:28 +0200 | [diff] [blame] | 1967 | lyd_insert_hash(node); |
| 1968 | |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 1969 | /* move the anchor accordingly */ |
| 1970 | sibling = node; |
| 1971 | |
| 1972 | node = (iter == node) ? NULL : iter; |
| 1973 | } |
| 1974 | return LY_SUCCESS; |
| 1975 | } |
| 1976 | |
| 1977 | API LY_ERR |
| 1978 | lyd_insert_after(struct lyd_node *sibling, struct lyd_node *node) |
| 1979 | { |
| 1980 | struct lyd_node *iter; |
| 1981 | |
| 1982 | LY_CHECK_ARG_RET(NULL, sibling, node, LY_EINVAL); |
| 1983 | |
Michal Vasko | 62ed12d | 2020-05-21 10:08:25 +0200 | [diff] [blame] | 1984 | 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] | 1985 | |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 1986 | 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] | 1987 | 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] | 1988 | return LY_EINVAL; |
| 1989 | } |
| 1990 | |
| 1991 | if (node->parent || node->prev->next) { |
| 1992 | lyd_unlink_tree(node); |
| 1993 | } |
| 1994 | |
| 1995 | while (node) { |
| 1996 | iter = node->next; |
| 1997 | lyd_unlink_tree(node); |
| 1998 | |
| 1999 | lyd_insert_after_node(sibling, node); |
Michal Vasko | 751cb4d | 2020-07-14 12:25:28 +0200 | [diff] [blame] | 2000 | lyd_insert_hash(node); |
| 2001 | |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 2002 | /* move the anchor accordingly */ |
| 2003 | sibling = node; |
| 2004 | |
| 2005 | node = iter; |
| 2006 | } |
| 2007 | return LY_SUCCESS; |
| 2008 | } |
| 2009 | |
| 2010 | API void |
| 2011 | lyd_unlink_tree(struct lyd_node *node) |
| 2012 | { |
| 2013 | struct lyd_node *iter; |
| 2014 | |
| 2015 | if (!node) { |
| 2016 | return; |
| 2017 | } |
| 2018 | |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 2019 | /* update hashes while still linked into the tree */ |
| 2020 | lyd_unlink_hash(node); |
| 2021 | |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 2022 | /* unlink from siblings */ |
| 2023 | if (node->prev->next) { |
| 2024 | node->prev->next = node->next; |
| 2025 | } |
| 2026 | if (node->next) { |
| 2027 | node->next->prev = node->prev; |
| 2028 | } else { |
| 2029 | /* unlinking the last node */ |
| 2030 | if (node->parent) { |
| 2031 | iter = node->parent->child; |
| 2032 | } else { |
| 2033 | iter = node->prev; |
| 2034 | while (iter->prev != node) { |
| 2035 | iter = iter->prev; |
| 2036 | } |
| 2037 | } |
| 2038 | /* update the "last" pointer from the first node */ |
| 2039 | iter->prev = node->prev; |
| 2040 | } |
| 2041 | |
| 2042 | /* unlink from parent */ |
| 2043 | if (node->parent) { |
| 2044 | if (node->parent->child == node) { |
| 2045 | /* the node is the first child */ |
| 2046 | node->parent->child = node->next; |
| 2047 | } |
| 2048 | |
Michal Vasko | ab49dbe | 2020-07-17 12:32:47 +0200 | [diff] [blame] | 2049 | /* 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] | 2050 | if (node->parent->schema && (node->parent->schema->nodetype == LYS_CONTAINER) && |
| 2051 | !(node->parent->flags & LYD_DEFAULT) && !(node->parent->schema->flags & LYS_PRESENCE)) { |
Michal Vasko | ab49dbe | 2020-07-17 12:32:47 +0200 | [diff] [blame] | 2052 | LY_LIST_FOR(node->parent->child, iter) { |
| 2053 | if ((iter != node) && !(iter->flags & LYD_DEFAULT)) { |
| 2054 | break; |
| 2055 | } |
| 2056 | } |
| 2057 | if (!iter) { |
| 2058 | node->parent->flags |= LYD_DEFAULT; |
| 2059 | } |
| 2060 | } |
| 2061 | |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 2062 | /* check for keyless list and update its hash */ |
| 2063 | 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] | 2064 | if (iter->schema && (iter->schema->flags & LYS_KEYLESS)) { |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 2065 | lyd_hash(iter); |
| 2066 | } |
| 2067 | } |
| 2068 | |
| 2069 | node->parent = NULL; |
| 2070 | } |
| 2071 | |
| 2072 | node->next = NULL; |
| 2073 | node->prev = node; |
| 2074 | } |
| 2075 | |
Michal Vasko | a5da329 | 2020-08-12 13:10:50 +0200 | [diff] [blame] | 2076 | void |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 2077 | lyd_insert_meta(struct lyd_node *parent, struct lyd_meta *meta) |
| 2078 | { |
| 2079 | struct lyd_meta *last, *iter; |
| 2080 | |
| 2081 | assert(parent); |
Michal Vasko | a5da329 | 2020-08-12 13:10:50 +0200 | [diff] [blame] | 2082 | |
| 2083 | if (!meta) { |
| 2084 | return; |
| 2085 | } |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 2086 | |
| 2087 | for (iter = meta; iter; iter = iter->next) { |
| 2088 | iter->parent = parent; |
| 2089 | } |
| 2090 | |
| 2091 | /* insert as the last attribute */ |
| 2092 | if (parent->meta) { |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 2093 | for (last = parent->meta; last->next; last = last->next) {} |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 2094 | last->next = meta; |
| 2095 | } else { |
| 2096 | parent->meta = meta; |
| 2097 | } |
| 2098 | |
| 2099 | /* remove default flags from NP containers */ |
| 2100 | while (parent && (parent->schema->nodetype == LYS_CONTAINER) && (parent->flags & LYD_DEFAULT)) { |
| 2101 | parent->flags &= ~LYD_DEFAULT; |
| 2102 | parent = (struct lyd_node *)parent->parent; |
| 2103 | } |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 2104 | } |
| 2105 | |
| 2106 | LY_ERR |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 2107 | 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] | 2108 | size_t name_len, const char *value, size_t value_len, ly_bool *dynamic, LY_PREFIX_FORMAT format, |
| 2109 | void *prefix_data, uint32_t hints, ly_bool *incomplete) |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 2110 | { |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 2111 | LY_ERR rc; |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 2112 | struct lysc_ext_instance *ant = NULL; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 2113 | struct lyd_meta *mt, *last; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 2114 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 2115 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 2116 | assert((parent || meta) && mod); |
Michal Vasko | 6f4cbb6 | 2020-02-28 11:15:47 +0100 | [diff] [blame] | 2117 | |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 2118 | LY_ARRAY_FOR(mod->compiled->exts, u) { |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 2119 | 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] | 2120 | !ly_strncmp(mod->compiled->exts[u].argument, name, name_len)) { |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 2121 | /* we have the annotation definition */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 2122 | ant = &mod->compiled->exts[u]; |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 2123 | break; |
| 2124 | } |
| 2125 | } |
| 2126 | if (!ant) { |
| 2127 | /* attribute is not defined as a metadata annotation (RFC 7952) */ |
| 2128 | LOGERR(mod->ctx, LY_EINVAL, "Annotation definition for attribute \"%s:%.*s\" not found.", |
| 2129 | mod->name, name_len, name); |
| 2130 | return LY_EINVAL; |
| 2131 | } |
| 2132 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 2133 | mt = calloc(1, sizeof *mt); |
| 2134 | LY_CHECK_ERR_RET(!mt, LOGMEM(mod->ctx), LY_EMEM); |
| 2135 | mt->parent = parent; |
| 2136 | mt->annotation = ant; |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 2137 | rc = lyd_value_store(mod->ctx, &mt->value, ((struct lyext_metadata *)ant->data)->type, value, value_len, dynamic, |
| 2138 | format, prefix_data, hints, parent ? parent->schema : NULL, incomplete, LY_VLOG_NONE, NULL); |
| 2139 | LY_CHECK_ERR_RET(rc, free(mt), rc); |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 2140 | rc = lydict_insert(mod->ctx, name, name_len, &mt->name); |
| 2141 | LY_CHECK_ERR_RET(rc, free(mt), rc); |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 2142 | |
Michal Vasko | 6f4cbb6 | 2020-02-28 11:15:47 +0100 | [diff] [blame] | 2143 | /* insert as the last attribute */ |
| 2144 | if (parent) { |
Michal Vasko | a5da329 | 2020-08-12 13:10:50 +0200 | [diff] [blame] | 2145 | lyd_insert_meta(parent, mt); |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 2146 | } else if (*meta) { |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 2147 | for (last = *meta; last->next; last = last->next) {} |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 2148 | last->next = mt; |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 2149 | } |
| 2150 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 2151 | if (meta) { |
| 2152 | *meta = mt; |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 2153 | } |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 2154 | return LY_SUCCESS; |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 2155 | } |
| 2156 | |
Michal Vasko | a5da329 | 2020-08-12 13:10:50 +0200 | [diff] [blame] | 2157 | void |
| 2158 | lyd_insert_attr(struct lyd_node *parent, struct lyd_attr *attr) |
| 2159 | { |
| 2160 | struct lyd_attr *last, *iter; |
| 2161 | struct lyd_node_opaq *opaq; |
| 2162 | |
| 2163 | assert(parent && !parent->schema); |
| 2164 | |
| 2165 | if (!attr) { |
| 2166 | return; |
| 2167 | } |
| 2168 | |
| 2169 | opaq = (struct lyd_node_opaq *)parent; |
| 2170 | for (iter = attr; iter; iter = iter->next) { |
| 2171 | iter->parent = opaq; |
| 2172 | } |
| 2173 | |
| 2174 | /* insert as the last attribute */ |
| 2175 | if (opaq->attr) { |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 2176 | for (last = opaq->attr; last->next; last = last->next) {} |
Michal Vasko | a5da329 | 2020-08-12 13:10:50 +0200 | [diff] [blame] | 2177 | last->next = attr; |
| 2178 | } else { |
| 2179 | opaq->attr = attr; |
| 2180 | } |
| 2181 | } |
| 2182 | |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 2183 | LY_ERR |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 2184 | lyd_create_attr(struct lyd_node *parent, struct lyd_attr **attr, const struct ly_ctx *ctx, const char *name, size_t name_len, |
| 2185 | 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] | 2186 | 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] | 2187 | { |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 2188 | LY_ERR ret = LY_SUCCESS; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 2189 | struct lyd_attr *at, *last; |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 2190 | |
| 2191 | assert(ctx && (parent || attr) && (!parent || !parent->schema)); |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 2192 | assert(name && name_len && format); |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 2193 | |
| 2194 | if (!value_len) { |
| 2195 | value = ""; |
| 2196 | } |
| 2197 | |
| 2198 | at = calloc(1, sizeof *at); |
Radek Krejci | d46e46a | 2020-09-15 14:22:42 +0200 | [diff] [blame] | 2199 | 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] | 2200 | at->hints = hints; |
Radek Krejci | d46e46a | 2020-09-15 14:22:42 +0200 | [diff] [blame] | 2201 | at->format = format; |
| 2202 | at->val_prefs = val_prefs; |
| 2203 | |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 2204 | 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] | 2205 | if (dynamic && *dynamic) { |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 2206 | ret = lydict_insert_zc(ctx, (char *)value, &at->value); |
| 2207 | LY_CHECK_GOTO(ret, finish); |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 2208 | *dynamic = 0; |
| 2209 | } else { |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 2210 | 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] | 2211 | } |
| 2212 | |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 2213 | if (prefix_len) { |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 2214 | 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] | 2215 | } |
| 2216 | if (module_key_len) { |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 2217 | 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] | 2218 | } |
| 2219 | |
| 2220 | /* insert as the last attribute */ |
| 2221 | if (parent) { |
Michal Vasko | a5da329 | 2020-08-12 13:10:50 +0200 | [diff] [blame] | 2222 | lyd_insert_attr(parent, at); |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 2223 | } else if (*attr) { |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 2224 | for (last = *attr; last->next; last = last->next) {} |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 2225 | last->next = at; |
| 2226 | } |
| 2227 | |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 2228 | finish: |
| 2229 | if (ret) { |
| 2230 | lyd_free_attr_single(ctx, at); |
| 2231 | } else if (attr) { |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 2232 | *attr = at; |
| 2233 | } |
| 2234 | return LY_SUCCESS; |
| 2235 | } |
| 2236 | |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 2237 | API const struct lyd_node_term * |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2238 | lyd_target(const struct ly_path *path, const struct lyd_node *tree) |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 2239 | { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2240 | struct lyd_node *target; |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 2241 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2242 | if (ly_path_eval(path, tree, &target)) { |
| 2243 | return NULL; |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 2244 | } |
| 2245 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2246 | return (struct lyd_node_term *)target; |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 2247 | } |
| 2248 | |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 2249 | API LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 2250 | 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] | 2251 | { |
| 2252 | const struct lyd_node *iter1, *iter2; |
| 2253 | struct lyd_node_term *term1, *term2; |
| 2254 | struct lyd_node_any *any1, *any2; |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 2255 | struct lyd_node_opaq *opaq1, *opaq2; |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 2256 | size_t len1, len2; |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 2257 | |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 2258 | if (!node1 || !node2) { |
| 2259 | if (node1 == node2) { |
| 2260 | return LY_SUCCESS; |
| 2261 | } else { |
| 2262 | return LY_ENOT; |
| 2263 | } |
| 2264 | } |
| 2265 | |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 2266 | if ((LYD_CTX(node1) != LYD_CTX(node2)) || (node1->schema != node2->schema)) { |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 2267 | return LY_ENOT; |
| 2268 | } |
| 2269 | |
| 2270 | if (node1->hash != node2->hash) { |
| 2271 | return LY_ENOT; |
| 2272 | } |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 2273 | /* 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] | 2274 | |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 2275 | if (!node1->schema) { |
| 2276 | opaq1 = (struct lyd_node_opaq *)node1; |
| 2277 | opaq2 = (struct lyd_node_opaq *)node2; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 2278 | 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] | 2279 | return LY_ENOT; |
| 2280 | } |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 2281 | switch (opaq1->format) { |
| 2282 | case LYD_XML: |
| 2283 | if (lyxml_value_compare(opaq1->value, opaq1->val_prefs, opaq2->value, opaq2->val_prefs)) { |
| 2284 | return LY_ENOT; |
| 2285 | } |
| 2286 | break; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 2287 | case LYD_JSON: |
| 2288 | /* prefixes in JSON are unique, so it is not necessary to canonize the values */ |
| 2289 | if (strcmp(opaq1->value, opaq2->value)) { |
| 2290 | return LY_ENOT; |
| 2291 | } |
| 2292 | break; |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 2293 | case LYD_LYB: |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 2294 | case LYD_UNKNOWN: |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 2295 | /* not allowed */ |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 2296 | LOGINT(LYD_CTX(node1)); |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 2297 | return LY_EINT; |
| 2298 | } |
| 2299 | if (options & LYD_COMPARE_FULL_RECURSION) { |
| 2300 | iter1 = opaq1->child; |
| 2301 | iter2 = opaq2->child; |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 2302 | goto all_children_compare; |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 2303 | } |
| 2304 | return LY_SUCCESS; |
| 2305 | } else { |
| 2306 | switch (node1->schema->nodetype) { |
| 2307 | case LYS_LEAF: |
| 2308 | case LYS_LEAFLIST: |
| 2309 | if (options & LYD_COMPARE_DEFAULTS) { |
| 2310 | if ((node1->flags & LYD_DEFAULT) != (node2->flags & LYD_DEFAULT)) { |
| 2311 | return LY_ENOT; |
| 2312 | } |
| 2313 | } |
| 2314 | |
Michal Vasko | 1a2bf2e | 2020-06-16 13:07:48 +0200 | [diff] [blame] | 2315 | term1 = (struct lyd_node_term *)node1; |
| 2316 | term2 = (struct lyd_node_term *)node2; |
| 2317 | if (term1->value.realtype != term2->value.realtype) { |
| 2318 | return LY_ENOT; |
| 2319 | } |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 2320 | |
Michal Vasko | 1a2bf2e | 2020-06-16 13:07:48 +0200 | [diff] [blame] | 2321 | return term1->value.realtype->plugin->compare(&term1->value, &term2->value); |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 2322 | case LYS_CONTAINER: |
| 2323 | if (options & LYD_COMPARE_DEFAULTS) { |
| 2324 | if ((node1->flags & LYD_DEFAULT) != (node2->flags & LYD_DEFAULT)) { |
| 2325 | return LY_ENOT; |
| 2326 | } |
| 2327 | } |
| 2328 | if (options & LYD_COMPARE_FULL_RECURSION) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 2329 | iter1 = ((struct lyd_node_inner *)node1)->child; |
| 2330 | iter2 = ((struct lyd_node_inner *)node2)->child; |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 2331 | goto all_children_compare; |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 2332 | } |
| 2333 | return LY_SUCCESS; |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 2334 | case LYS_RPC: |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 2335 | case LYS_ACTION: |
| 2336 | if (options & LYD_COMPARE_FULL_RECURSION) { |
| 2337 | /* TODO action/RPC |
| 2338 | goto all_children_compare; |
| 2339 | */ |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 2340 | } |
| 2341 | return LY_SUCCESS; |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 2342 | case LYS_NOTIF: |
| 2343 | if (options & LYD_COMPARE_FULL_RECURSION) { |
| 2344 | /* TODO Notification |
| 2345 | goto all_children_compare; |
| 2346 | */ |
| 2347 | } |
| 2348 | return LY_SUCCESS; |
| 2349 | case LYS_LIST: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 2350 | iter1 = ((struct lyd_node_inner *)node1)->child; |
| 2351 | iter2 = ((struct lyd_node_inner *)node2)->child; |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 2352 | |
| 2353 | if (!(node1->schema->flags & LYS_KEYLESS) && !(options & LYD_COMPARE_FULL_RECURSION)) { |
| 2354 | /* lists with keys, their equivalence is based on their keys */ |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 2355 | for (struct lysc_node *key = ((struct lysc_node_list *)node1->schema)->child; |
Michal Vasko | c57d9a9 | 2020-06-23 13:28:27 +0200 | [diff] [blame] | 2356 | key && (key->flags & LYS_KEY); |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 2357 | key = key->next) { |
Michal Vasko | 8f359bf | 2020-07-28 10:41:15 +0200 | [diff] [blame] | 2358 | if (lyd_compare_single(iter1, iter2, options)) { |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 2359 | return LY_ENOT; |
| 2360 | } |
| 2361 | iter1 = iter1->next; |
| 2362 | iter2 = iter2->next; |
| 2363 | } |
| 2364 | } else { |
| 2365 | /* lists without keys, their equivalence is based on equivalence of all the children (both direct and indirect) */ |
| 2366 | |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 2367 | all_children_compare: |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 2368 | if (!iter1 && !iter2) { |
| 2369 | /* no children, nothing to compare */ |
| 2370 | return LY_SUCCESS; |
| 2371 | } |
| 2372 | |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 2373 | for ( ; iter1 && iter2; iter1 = iter1->next, iter2 = iter2->next) { |
Michal Vasko | 8f359bf | 2020-07-28 10:41:15 +0200 | [diff] [blame] | 2374 | if (lyd_compare_single(iter1, iter2, options | LYD_COMPARE_FULL_RECURSION)) { |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 2375 | return LY_ENOT; |
| 2376 | } |
| 2377 | } |
| 2378 | if (iter1 || iter2) { |
| 2379 | return LY_ENOT; |
| 2380 | } |
| 2381 | } |
| 2382 | return LY_SUCCESS; |
| 2383 | case LYS_ANYXML: |
| 2384 | case LYS_ANYDATA: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 2385 | any1 = (struct lyd_node_any *)node1; |
| 2386 | any2 = (struct lyd_node_any *)node2; |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 2387 | |
| 2388 | if (any1->value_type != any2->value_type) { |
| 2389 | return LY_ENOT; |
| 2390 | } |
| 2391 | switch (any1->value_type) { |
| 2392 | case LYD_ANYDATA_DATATREE: |
| 2393 | iter1 = any1->value.tree; |
| 2394 | iter2 = any2->value.tree; |
| 2395 | goto all_children_compare; |
| 2396 | case LYD_ANYDATA_STRING: |
| 2397 | case LYD_ANYDATA_XML: |
| 2398 | case LYD_ANYDATA_JSON: |
| 2399 | len1 = strlen(any1->value.str); |
| 2400 | len2 = strlen(any2->value.str); |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 2401 | if ((len1 != len2) || strcmp(any1->value.str, any2->value.str)) { |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 2402 | return LY_ENOT; |
| 2403 | } |
| 2404 | return LY_SUCCESS; |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 2405 | case LYD_ANYDATA_LYB: |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 2406 | len1 = lyd_lyb_data_length(any1->value.mem); |
| 2407 | len2 = lyd_lyb_data_length(any2->value.mem); |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 2408 | if ((len1 != len2) || memcmp(any1->value.mem, any2->value.mem, len1)) { |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 2409 | return LY_ENOT; |
| 2410 | } |
| 2411 | return LY_SUCCESS; |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 2412 | } |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 2413 | } |
| 2414 | } |
| 2415 | |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 2416 | LOGINT(LYD_CTX(node1)); |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 2417 | return LY_EINT; |
| 2418 | } |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 2419 | |
Michal Vasko | 2172574 | 2020-06-29 11:49:25 +0200 | [diff] [blame] | 2420 | API LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 2421 | 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] | 2422 | { |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 2423 | for ( ; node1 && node2; node1 = node1->next, node2 = node2->next) { |
Michal Vasko | 8f359bf | 2020-07-28 10:41:15 +0200 | [diff] [blame] | 2424 | LY_CHECK_RET(lyd_compare_single(node1, node2, options)); |
| 2425 | } |
| 2426 | |
Michal Vasko | 11a8143 | 2020-07-28 16:31:29 +0200 | [diff] [blame] | 2427 | if (node1 == node2) { |
| 2428 | return LY_SUCCESS; |
Michal Vasko | 8f359bf | 2020-07-28 10:41:15 +0200 | [diff] [blame] | 2429 | } |
Michal Vasko | 11a8143 | 2020-07-28 16:31:29 +0200 | [diff] [blame] | 2430 | return LY_ENOT; |
Michal Vasko | 8f359bf | 2020-07-28 10:41:15 +0200 | [diff] [blame] | 2431 | } |
| 2432 | |
| 2433 | API LY_ERR |
Michal Vasko | 2172574 | 2020-06-29 11:49:25 +0200 | [diff] [blame] | 2434 | lyd_compare_meta(const struct lyd_meta *meta1, const struct lyd_meta *meta2) |
| 2435 | { |
| 2436 | if (!meta1 || !meta2) { |
| 2437 | if (meta1 == meta2) { |
| 2438 | return LY_SUCCESS; |
| 2439 | } else { |
| 2440 | return LY_ENOT; |
| 2441 | } |
| 2442 | } |
| 2443 | |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 2444 | 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] | 2445 | return LY_ENOT; |
| 2446 | } |
| 2447 | |
| 2448 | if (meta1->value.realtype != meta2->value.realtype) { |
| 2449 | return LY_ENOT; |
| 2450 | } |
| 2451 | |
| 2452 | return meta1->value.realtype->plugin->compare(&meta1->value, &meta2->value); |
| 2453 | } |
| 2454 | |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 2455 | /** |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 2456 | * @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] | 2457 | * |
| 2458 | * 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] | 2459 | * |
| 2460 | * @param[in] node Original node to duplicate |
| 2461 | * @param[in] parent Parent to insert into, NULL for top-level sibling. |
| 2462 | * @param[in,out] first First sibling, NULL if no top-level sibling exist yet. Can be also NULL if @p parent is set. |
| 2463 | * @param[in] options Bitmask of options flags, see @ref dupoptions. |
| 2464 | * @param[out] dup_p Pointer where the created duplicated node is placed (besides connecting it int @p parent / @p first sibling). |
| 2465 | * @return LY_ERR value |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 2466 | */ |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 2467 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 2468 | 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] | 2469 | struct lyd_node **dup_p) |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 2470 | { |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 2471 | LY_ERR ret; |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 2472 | struct lyd_node *dup = NULL; |
Michal Vasko | 61551fa | 2020-07-09 15:45:45 +0200 | [diff] [blame] | 2473 | struct lyd_meta *meta; |
| 2474 | struct lyd_node_any *any; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 2475 | LY_ARRAY_COUNT_TYPE u; |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 2476 | |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 2477 | LY_CHECK_ARG_RET(NULL, node, LY_EINVAL); |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 2478 | |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 2479 | if (!node->schema) { |
| 2480 | dup = calloc(1, sizeof(struct lyd_node_opaq)); |
| 2481 | } else { |
| 2482 | switch (node->schema->nodetype) { |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 2483 | case LYS_RPC: |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 2484 | case LYS_ACTION: |
| 2485 | case LYS_NOTIF: |
| 2486 | case LYS_CONTAINER: |
| 2487 | case LYS_LIST: |
| 2488 | dup = calloc(1, sizeof(struct lyd_node_inner)); |
| 2489 | break; |
| 2490 | case LYS_LEAF: |
| 2491 | case LYS_LEAFLIST: |
| 2492 | dup = calloc(1, sizeof(struct lyd_node_term)); |
| 2493 | break; |
| 2494 | case LYS_ANYDATA: |
| 2495 | case LYS_ANYXML: |
| 2496 | dup = calloc(1, sizeof(struct lyd_node_any)); |
| 2497 | break; |
| 2498 | default: |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 2499 | LOGINT(LYD_CTX(node)); |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 2500 | ret = LY_EINT; |
| 2501 | goto error; |
| 2502 | } |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 2503 | } |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 2504 | 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] | 2505 | |
Michal Vasko | f6df0a0 | 2020-06-16 13:08:34 +0200 | [diff] [blame] | 2506 | if (options & LYD_DUP_WITH_FLAGS) { |
| 2507 | dup->flags = node->flags; |
| 2508 | } else { |
| 2509 | dup->flags = (node->flags & LYD_DEFAULT) | LYD_NEW; |
| 2510 | } |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 2511 | dup->schema = node->schema; |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 2512 | dup->prev = dup; |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 2513 | |
Michal Vasko | 25a3282 | 2020-07-09 15:48:22 +0200 | [diff] [blame] | 2514 | /* duplicate metadata */ |
| 2515 | if (!(options & LYD_DUP_NO_META)) { |
| 2516 | LY_LIST_FOR(node->meta, meta) { |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 2517 | LY_CHECK_GOTO(ret = lyd_dup_meta_single(meta, dup, NULL), error); |
Michal Vasko | 25a3282 | 2020-07-09 15:48:22 +0200 | [diff] [blame] | 2518 | } |
| 2519 | } |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 2520 | |
| 2521 | /* nodetype-specific work */ |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 2522 | if (!dup->schema) { |
| 2523 | struct lyd_node_opaq *opaq = (struct lyd_node_opaq *)dup; |
| 2524 | struct lyd_node_opaq *orig = (struct lyd_node_opaq *)node; |
| 2525 | struct lyd_node *child; |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 2526 | |
| 2527 | if (options & LYD_DUP_RECURSIVE) { |
| 2528 | /* duplicate all the children */ |
| 2529 | LY_LIST_FOR(orig->child, child) { |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 2530 | 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] | 2531 | } |
| 2532 | } |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 2533 | 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] | 2534 | opaq->format = orig->format; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 2535 | if (orig->prefix.id) { |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 2536 | 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] | 2537 | } |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 2538 | 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] | 2539 | if (orig->val_prefs) { |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 2540 | 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] | 2541 | LY_ARRAY_FOR(orig->val_prefs, u) { |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 2542 | LY_CHECK_GOTO(ret = lydict_insert(LYD_CTX(node), orig->val_prefs[u].id, 0, &opaq->val_prefs[u].id), error); |
| 2543 | 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] | 2544 | LY_ARRAY_INCREMENT(opaq->val_prefs); |
| 2545 | } |
| 2546 | } |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 2547 | 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] | 2548 | opaq->ctx = orig->ctx; |
| 2549 | } else if (dup->schema->nodetype & LYD_NODE_TERM) { |
| 2550 | struct lyd_node_term *term = (struct lyd_node_term *)dup; |
| 2551 | struct lyd_node_term *orig = (struct lyd_node_term *)node; |
| 2552 | |
| 2553 | term->hash = orig->hash; |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 2554 | 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] | 2555 | 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] | 2556 | } else if (dup->schema->nodetype & LYD_NODE_INNER) { |
| 2557 | struct lyd_node_inner *orig = (struct lyd_node_inner *)node; |
| 2558 | struct lyd_node *child; |
| 2559 | |
| 2560 | if (options & LYD_DUP_RECURSIVE) { |
| 2561 | /* duplicate all the children */ |
| 2562 | LY_LIST_FOR(orig->child, child) { |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 2563 | 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] | 2564 | } |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 2565 | } else if ((dup->schema->nodetype == LYS_LIST) && !(dup->schema->flags & LYS_KEYLESS)) { |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 2566 | /* always duplicate keys of a list */ |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 2567 | child = orig->child; |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 2568 | for (struct lysc_node *key = ((struct lysc_node_list *)dup->schema)->child; |
Michal Vasko | c57d9a9 | 2020-06-23 13:28:27 +0200 | [diff] [blame] | 2569 | key && (key->flags & LYS_KEY); |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 2570 | key = key->next) { |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 2571 | if (!child) { |
| 2572 | /* possibly not keys are present in filtered tree */ |
| 2573 | break; |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 2574 | } else if (child->schema != key) { |
| 2575 | /* possibly not all keys are present in filtered tree, |
| 2576 | * but there can be also some non-key nodes */ |
| 2577 | continue; |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 2578 | } |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 2579 | 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] | 2580 | child = child->next; |
| 2581 | } |
| 2582 | } |
| 2583 | lyd_hash(dup); |
| 2584 | } else if (dup->schema->nodetype & LYD_NODE_ANY) { |
Michal Vasko | 61551fa | 2020-07-09 15:45:45 +0200 | [diff] [blame] | 2585 | dup->hash = node->hash; |
| 2586 | any = (struct lyd_node_any *)node; |
| 2587 | 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] | 2588 | } |
| 2589 | |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 2590 | /* insert */ |
| 2591 | lyd_insert_node(parent, first, dup); |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 2592 | |
| 2593 | if (dup_p) { |
| 2594 | *dup_p = dup; |
| 2595 | } |
| 2596 | return LY_SUCCESS; |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 2597 | |
| 2598 | error: |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 2599 | lyd_free_tree(dup); |
| 2600 | return ret; |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 2601 | } |
| 2602 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 2603 | static LY_ERR |
| 2604 | 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] | 2605 | struct lyd_node_inner **local_parent) |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 2606 | { |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 2607 | const struct lyd_node_inner *orig_parent, *iter; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 2608 | ly_bool repeat = 1; |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 2609 | |
| 2610 | *dup_parent = NULL; |
| 2611 | *local_parent = NULL; |
| 2612 | |
| 2613 | for (orig_parent = node->parent; repeat && orig_parent; orig_parent = orig_parent->parent) { |
| 2614 | if (parent && (parent->schema == orig_parent->schema)) { |
| 2615 | /* stop creating parents, connect what we have into the provided parent */ |
| 2616 | iter = parent; |
| 2617 | repeat = 0; |
| 2618 | } else { |
| 2619 | iter = NULL; |
| 2620 | 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] | 2621 | (struct lyd_node **)&iter)); |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 2622 | } |
| 2623 | if (!*local_parent) { |
| 2624 | *local_parent = (struct lyd_node_inner *)iter; |
| 2625 | } |
| 2626 | if (iter->child) { |
| 2627 | /* 1) list - add after keys |
| 2628 | * 2) provided parent with some children */ |
| 2629 | iter->child->prev->next = *dup_parent; |
| 2630 | if (*dup_parent) { |
| 2631 | (*dup_parent)->prev = iter->child->prev; |
| 2632 | iter->child->prev = *dup_parent; |
| 2633 | } |
| 2634 | } else { |
| 2635 | ((struct lyd_node_inner *)iter)->child = *dup_parent; |
| 2636 | } |
| 2637 | if (*dup_parent) { |
| 2638 | (*dup_parent)->parent = (struct lyd_node_inner *)iter; |
| 2639 | } |
| 2640 | *dup_parent = (struct lyd_node *)iter; |
| 2641 | } |
| 2642 | |
| 2643 | if (repeat && parent) { |
| 2644 | /* given parent and created parents chain actually do not interconnect */ |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 2645 | LOGERR(LYD_CTX(node), LY_EINVAL, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 2646 | "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] | 2647 | return LY_EINVAL; |
| 2648 | } |
| 2649 | |
| 2650 | return LY_SUCCESS; |
| 2651 | } |
| 2652 | |
| 2653 | static LY_ERR |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 2654 | 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] | 2655 | { |
| 2656 | LY_ERR rc; |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 2657 | const struct lyd_node *orig; /* original node to be duplicated */ |
| 2658 | struct lyd_node *first = NULL; /* the first duplicated node, this is returned */ |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 2659 | struct lyd_node *top = NULL; /* the most higher created node */ |
| 2660 | 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] | 2661 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 2662 | LY_CHECK_ARG_RET(NULL, node, LY_EINVAL); |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 2663 | |
| 2664 | if (options & LYD_DUP_WITH_PARENTS) { |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 2665 | 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] | 2666 | } else { |
| 2667 | local_parent = parent; |
| 2668 | } |
| 2669 | |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 2670 | LY_LIST_FOR(node, orig) { |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 2671 | /* if there is no local parent, it will be inserted into first */ |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 2672 | LY_CHECK_GOTO(rc = lyd_dup_r(orig, (struct lyd_node *)local_parent, &first, options, first ? NULL : &first), error); |
| 2673 | if (nosiblings) { |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 2674 | break; |
| 2675 | } |
| 2676 | } |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 2677 | |
| 2678 | /* rehash if needed */ |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 2679 | for ( ; local_parent; local_parent = local_parent->parent) { |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 2680 | 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] | 2681 | lyd_hash((struct lyd_node *)local_parent); |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 2682 | } |
| 2683 | } |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 2684 | |
| 2685 | if (dup) { |
| 2686 | *dup = first; |
| 2687 | } |
| 2688 | return LY_SUCCESS; |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 2689 | |
| 2690 | error: |
| 2691 | if (top) { |
| 2692 | lyd_free_tree(top); |
| 2693 | } else { |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 2694 | lyd_free_siblings(first); |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 2695 | } |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 2696 | return rc; |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 2697 | } |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 2698 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 2699 | API LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 2700 | 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] | 2701 | { |
| 2702 | return lyd_dup(node, parent, options, 1, dup); |
| 2703 | } |
| 2704 | |
| 2705 | API LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 2706 | 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] | 2707 | { |
| 2708 | return lyd_dup(node, parent, options, 0, dup); |
| 2709 | } |
| 2710 | |
| 2711 | API LY_ERR |
| 2712 | 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] | 2713 | { |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 2714 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 25a3282 | 2020-07-09 15:48:22 +0200 | [diff] [blame] | 2715 | struct lyd_meta *mt, *last; |
| 2716 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 2717 | LY_CHECK_ARG_RET(NULL, meta, node, LY_EINVAL); |
Michal Vasko | 25a3282 | 2020-07-09 15:48:22 +0200 | [diff] [blame] | 2718 | |
| 2719 | /* create a copy */ |
| 2720 | mt = calloc(1, sizeof *mt); |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 2721 | LY_CHECK_ERR_RET(!mt, LOGMEM(LYD_CTX(node)), LY_EMEM); |
Michal Vasko | 25a3282 | 2020-07-09 15:48:22 +0200 | [diff] [blame] | 2722 | mt->annotation = meta->annotation; |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 2723 | 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] | 2724 | LY_CHECK_ERR_GOTO(ret, LOGERR(LYD_CTX(node), LY_EINT, "Value duplication failed."), finish); |
| 2725 | 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] | 2726 | |
| 2727 | /* insert as the last attribute */ |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 2728 | mt->parent = node; |
Michal Vasko | 25a3282 | 2020-07-09 15:48:22 +0200 | [diff] [blame] | 2729 | if (node->meta) { |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 2730 | for (last = node->meta; last->next; last = last->next) {} |
Michal Vasko | 25a3282 | 2020-07-09 15:48:22 +0200 | [diff] [blame] | 2731 | last->next = mt; |
| 2732 | } else { |
| 2733 | node->meta = mt; |
| 2734 | } |
| 2735 | |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 2736 | finish: |
| 2737 | if (ret) { |
| 2738 | lyd_free_meta_single(mt); |
| 2739 | } else if (dup) { |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 2740 | *dup = mt; |
| 2741 | } |
| 2742 | return LY_SUCCESS; |
Michal Vasko | 25a3282 | 2020-07-09 15:48:22 +0200 | [diff] [blame] | 2743 | } |
| 2744 | |
Michal Vasko | 4490d31 | 2020-06-16 13:08:55 +0200 | [diff] [blame] | 2745 | /** |
| 2746 | * @brief Merge a source sibling into target siblings. |
| 2747 | * |
| 2748 | * @param[in,out] first_trg First target sibling, is updated if top-level. |
| 2749 | * @param[in] parent_trg Target parent. |
| 2750 | * @param[in,out] sibling_src Source sibling to merge, set to NULL if spent. |
| 2751 | * @param[in] options Merge options. |
| 2752 | * @return LY_ERR value. |
| 2753 | */ |
| 2754 | static LY_ERR |
| 2755 | 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] | 2756 | uint16_t options) |
Michal Vasko | 4490d31 | 2020-06-16 13:08:55 +0200 | [diff] [blame] | 2757 | { |
| 2758 | LY_ERR ret; |
| 2759 | const struct lyd_node *child_src, *tmp, *sibling_src; |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 2760 | struct lyd_node *match_trg, *dup_src, *elem; |
Michal Vasko | 4490d31 | 2020-06-16 13:08:55 +0200 | [diff] [blame] | 2761 | struct lysc_type *type; |
Michal Vasko | 4490d31 | 2020-06-16 13:08:55 +0200 | [diff] [blame] | 2762 | |
| 2763 | sibling_src = *sibling_src_p; |
| 2764 | if (sibling_src->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) { |
| 2765 | /* try to find the exact instance */ |
| 2766 | ret = lyd_find_sibling_first(*first_trg, sibling_src, &match_trg); |
| 2767 | } else { |
| 2768 | /* try to simply find the node, there cannot be more instances */ |
| 2769 | ret = lyd_find_sibling_val(*first_trg, sibling_src->schema, NULL, 0, &match_trg); |
| 2770 | } |
| 2771 | |
| 2772 | if (!ret) { |
| 2773 | /* node found, make sure even value matches for all node types */ |
Michal Vasko | 8f359bf | 2020-07-28 10:41:15 +0200 | [diff] [blame] | 2774 | 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] | 2775 | /* since they are different, they cannot both be default */ |
| 2776 | assert(!(sibling_src->flags & LYD_DEFAULT) || !(match_trg->flags & LYD_DEFAULT)); |
| 2777 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 2778 | /* update value (or only LYD_DEFAULT flag) only if flag set or the source node is not default */ |
| 2779 | if ((options & LYD_MERGE_DEFAULTS) || !(sibling_src->flags & LYD_DEFAULT)) { |
Michal Vasko | 4490d31 | 2020-06-16 13:08:55 +0200 | [diff] [blame] | 2780 | type = ((struct lysc_node_leaf *)match_trg->schema)->type; |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 2781 | type->plugin->free(LYD_CTX(match_trg), &((struct lyd_node_term *)match_trg)->value); |
| 2782 | 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] | 2783 | &((struct lyd_node_term *)match_trg)->value)); |
Michal Vasko | 4490d31 | 2020-06-16 13:08:55 +0200 | [diff] [blame] | 2784 | |
| 2785 | /* copy flags and add LYD_NEW */ |
| 2786 | match_trg->flags = sibling_src->flags | LYD_NEW; |
| 2787 | } |
Michal Vasko | 8f359bf | 2020-07-28 10:41:15 +0200 | [diff] [blame] | 2788 | } 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] | 2789 | /* update value */ |
| 2790 | 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] | 2791 | ((struct lyd_node_any *)sibling_src)->value_type)); |
Michal Vasko | 4490d31 | 2020-06-16 13:08:55 +0200 | [diff] [blame] | 2792 | |
| 2793 | /* copy flags and add LYD_NEW */ |
| 2794 | match_trg->flags = sibling_src->flags | LYD_NEW; |
Michal Vasko | 4490d31 | 2020-06-16 13:08:55 +0200 | [diff] [blame] | 2795 | } else { |
| 2796 | /* check descendants, recursively */ |
Radek Krejci | a1c1e54 | 2020-09-29 16:06:52 +0200 | [diff] [blame] | 2797 | 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] | 2798 | LY_CHECK_RET(lyd_merge_sibling_r(lyd_node_children_p(match_trg), match_trg, &child_src, options)); |
| 2799 | } |
| 2800 | } |
| 2801 | } else { |
| 2802 | /* node not found, merge it */ |
| 2803 | if (options & LYD_MERGE_DESTRUCT) { |
| 2804 | dup_src = (struct lyd_node *)sibling_src; |
| 2805 | lyd_unlink_tree(dup_src); |
| 2806 | /* spend it */ |
| 2807 | *sibling_src_p = NULL; |
| 2808 | } else { |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 2809 | 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] | 2810 | } |
| 2811 | |
| 2812 | /* set LYD_NEW for all the new nodes, required for validation */ |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 2813 | LYD_TREE_DFS_BEGIN(dup_src, elem) { |
Michal Vasko | 4490d31 | 2020-06-16 13:08:55 +0200 | [diff] [blame] | 2814 | elem->flags |= LYD_NEW; |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 2815 | LYD_TREE_DFS_END(dup_src, elem); |
Michal Vasko | 4490d31 | 2020-06-16 13:08:55 +0200 | [diff] [blame] | 2816 | } |
| 2817 | |
| 2818 | lyd_insert_node(parent_trg, first_trg, dup_src); |
| 2819 | } |
| 2820 | |
| 2821 | return LY_SUCCESS; |
| 2822 | } |
| 2823 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 2824 | static LY_ERR |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 2825 | 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] | 2826 | { |
| 2827 | const struct lyd_node *sibling_src, *tmp; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 2828 | ly_bool first; |
Michal Vasko | 4490d31 | 2020-06-16 13:08:55 +0200 | [diff] [blame] | 2829 | |
| 2830 | LY_CHECK_ARG_RET(NULL, target, LY_EINVAL); |
| 2831 | |
| 2832 | if (!source) { |
| 2833 | /* nothing to merge */ |
| 2834 | return LY_SUCCESS; |
| 2835 | } |
| 2836 | |
| 2837 | if (lysc_data_parent((*target)->schema) || lysc_data_parent(source->schema)) { |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 2838 | 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] | 2839 | return LY_EINVAL; |
| 2840 | } |
| 2841 | |
| 2842 | LY_LIST_FOR_SAFE(source, tmp, sibling_src) { |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 2843 | first = (sibling_src == source) ? 1 : 0; |
Michal Vasko | 4490d31 | 2020-06-16 13:08:55 +0200 | [diff] [blame] | 2844 | LY_CHECK_RET(lyd_merge_sibling_r(target, NULL, &sibling_src, options)); |
| 2845 | if (first && !sibling_src) { |
| 2846 | /* source was spent (unlinked), move to the next node */ |
| 2847 | source = tmp; |
| 2848 | } |
| 2849 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 2850 | if (nosiblings) { |
Michal Vasko | 4490d31 | 2020-06-16 13:08:55 +0200 | [diff] [blame] | 2851 | break; |
| 2852 | } |
| 2853 | } |
| 2854 | |
| 2855 | if (options & LYD_MERGE_DESTRUCT) { |
| 2856 | /* free any leftover source data that were not merged */ |
| 2857 | lyd_free_siblings((struct lyd_node *)source); |
| 2858 | } |
| 2859 | |
| 2860 | return LY_SUCCESS; |
| 2861 | } |
| 2862 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 2863 | API LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 2864 | 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] | 2865 | { |
| 2866 | return lyd_merge(target, source, options, 1); |
| 2867 | } |
| 2868 | |
| 2869 | API LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 2870 | 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] | 2871 | { |
| 2872 | return lyd_merge(target, source, options, 0); |
| 2873 | } |
| 2874 | |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 2875 | static LY_ERR |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 2876 | 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] | 2877 | { |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 2878 | /* ending \0 */ |
| 2879 | ++reqlen; |
| 2880 | |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 2881 | if (reqlen > *buflen) { |
| 2882 | if (is_static) { |
| 2883 | return LY_EINCOMPLETE; |
| 2884 | } |
| 2885 | |
| 2886 | *buffer = ly_realloc(*buffer, reqlen * sizeof **buffer); |
| 2887 | if (!*buffer) { |
| 2888 | return LY_EMEM; |
| 2889 | } |
| 2890 | |
| 2891 | *buflen = reqlen; |
| 2892 | } |
| 2893 | |
| 2894 | return LY_SUCCESS; |
| 2895 | } |
| 2896 | |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 2897 | LY_ERR |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 2898 | 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] | 2899 | { |
| 2900 | const struct lyd_node *key; |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 2901 | size_t len; |
| 2902 | const char *val; |
| 2903 | char quot; |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 2904 | |
Radek Krejci | a1c1e54 | 2020-09-29 16:06:52 +0200 | [diff] [blame] | 2905 | 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] | 2906 | val = LYD_CANON_VALUE(key); |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 2907 | len = 1 + strlen(key->schema->name) + 2 + strlen(val) + 2; |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 2908 | 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] | 2909 | |
| 2910 | quot = '\''; |
| 2911 | if (strchr(val, '\'')) { |
| 2912 | quot = '"'; |
| 2913 | } |
| 2914 | *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] | 2915 | } |
| 2916 | |
| 2917 | return LY_SUCCESS; |
| 2918 | } |
| 2919 | |
| 2920 | /** |
| 2921 | * @brief Append leaf-list value predicate to path. |
| 2922 | * |
| 2923 | * @param[in] node Node to print. |
| 2924 | * @param[in,out] buffer Buffer to print to. |
| 2925 | * @param[in,out] buflen Current buffer length. |
| 2926 | * @param[in,out] bufused Current number of characters used in @p buffer. |
| 2927 | * @param[in] is_static Whether buffer is static or can be reallocated. |
| 2928 | * @return LY_ERR |
| 2929 | */ |
| 2930 | static LY_ERR |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 2931 | 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] | 2932 | { |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 2933 | size_t len; |
| 2934 | const char *val; |
| 2935 | char quot; |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 2936 | |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 2937 | val = LYD_CANON_VALUE(node); |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 2938 | len = 4 + strlen(val) + 2; |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 2939 | 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] | 2940 | |
| 2941 | quot = '\''; |
| 2942 | if (strchr(val, '\'')) { |
| 2943 | quot = '"'; |
| 2944 | } |
| 2945 | *bufused += sprintf(*buffer + *bufused, "[.=%c%s%c]", quot, val, quot); |
| 2946 | |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 2947 | return LY_SUCCESS; |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 2948 | } |
| 2949 | |
| 2950 | /** |
| 2951 | * @brief Append node position (relative to its other instances) predicate to path. |
| 2952 | * |
| 2953 | * @param[in] node Node to print. |
| 2954 | * @param[in,out] buffer Buffer to print to. |
| 2955 | * @param[in,out] buflen Current buffer length. |
| 2956 | * @param[in,out] bufused Current number of characters used in @p buffer. |
| 2957 | * @param[in] is_static Whether buffer is static or can be reallocated. |
| 2958 | * @return LY_ERR |
| 2959 | */ |
| 2960 | static LY_ERR |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 2961 | 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] | 2962 | { |
| 2963 | const struct lyd_node *first, *iter; |
| 2964 | size_t len; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 2965 | uint64_t pos; |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 2966 | char *val = NULL; |
| 2967 | LY_ERR rc; |
| 2968 | |
| 2969 | if (node->parent) { |
| 2970 | first = node->parent->child; |
| 2971 | } else { |
Michal Vasko | e070bfe | 2020-08-31 12:27:25 +0200 | [diff] [blame] | 2972 | for (first = node; first->prev->next; first = first->prev) {} |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 2973 | } |
| 2974 | pos = 1; |
| 2975 | for (iter = first; iter != node; iter = iter->next) { |
| 2976 | if (iter->schema == node->schema) { |
| 2977 | ++pos; |
| 2978 | } |
| 2979 | } |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 2980 | if (asprintf(&val, "%" PRIu64, pos) == -1) { |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 2981 | return LY_EMEM; |
| 2982 | } |
| 2983 | |
| 2984 | len = 1 + strlen(val) + 1; |
| 2985 | rc = lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static); |
| 2986 | if (rc != LY_SUCCESS) { |
| 2987 | goto cleanup; |
| 2988 | } |
| 2989 | |
| 2990 | *bufused += sprintf(*buffer + *bufused, "[%s]", val); |
| 2991 | |
| 2992 | cleanup: |
| 2993 | free(val); |
| 2994 | return rc; |
| 2995 | } |
| 2996 | |
| 2997 | API char * |
| 2998 | lyd_path(const struct lyd_node *node, LYD_PATH_TYPE pathtype, char *buffer, size_t buflen) |
| 2999 | { |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3000 | ly_bool is_static = 0; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3001 | uint32_t i, depth; |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 3002 | size_t bufused = 0, len; |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 3003 | const struct lyd_node *iter; |
| 3004 | const struct lys_module *mod; |
Michal Vasko | 790b2bc | 2020-08-03 13:35:06 +0200 | [diff] [blame] | 3005 | LY_ERR rc = LY_SUCCESS; |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 3006 | |
| 3007 | LY_CHECK_ARG_RET(NULL, node, NULL); |
| 3008 | if (buffer) { |
| 3009 | LY_CHECK_ARG_RET(node->schema->module->ctx, buflen > 1, NULL); |
| 3010 | is_static = 1; |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 3011 | } else { |
| 3012 | buflen = 0; |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 3013 | } |
| 3014 | |
| 3015 | switch (pathtype) { |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 3016 | case LYD_PATH_LOG: |
Michal Vasko | 790b2bc | 2020-08-03 13:35:06 +0200 | [diff] [blame] | 3017 | case LYD_PATH_LOG_NO_LAST_PRED: |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 3018 | depth = 1; |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 3019 | for (iter = node; iter->parent; iter = (const struct lyd_node *)iter->parent) { |
| 3020 | ++depth; |
| 3021 | } |
| 3022 | |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 3023 | goto iter_print; |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 3024 | while (depth) { |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 3025 | /* find the right node */ |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 3026 | 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] | 3027 | iter_print: |
| 3028 | /* print prefix and name */ |
| 3029 | mod = NULL; |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 3030 | if (iter->schema && (!iter->parent || (iter->schema->module != iter->parent->schema->module))) { |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 3031 | mod = iter->schema->module; |
| 3032 | } |
| 3033 | |
| 3034 | /* realloc string */ |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 3035 | 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] | 3036 | rc = lyd_path_str_enlarge(&buffer, &buflen, bufused + len, is_static); |
| 3037 | if (rc != LY_SUCCESS) { |
| 3038 | break; |
| 3039 | } |
| 3040 | |
| 3041 | /* print next node */ |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 3042 | bufused += sprintf(buffer + bufused, "/%s%s%s", mod ? mod->name : "", mod ? ":" : "", |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 3043 | iter->schema ? iter->schema->name : ((struct lyd_node_opaq *)iter)->name); |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 3044 | |
Michal Vasko | 790b2bc | 2020-08-03 13:35:06 +0200 | [diff] [blame] | 3045 | /* do not always print the last (first) predicate */ |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 3046 | if (iter->schema && (bufused || (pathtype == LYD_PATH_LOG))) { |
Michal Vasko | 790b2bc | 2020-08-03 13:35:06 +0200 | [diff] [blame] | 3047 | switch (iter->schema->nodetype) { |
| 3048 | case LYS_LIST: |
| 3049 | if (iter->schema->flags & LYS_KEYLESS) { |
| 3050 | /* print its position */ |
| 3051 | rc = lyd_path_position_predicate(iter, &buffer, &buflen, &bufused, is_static); |
| 3052 | } else { |
| 3053 | /* print all list keys in predicates */ |
| 3054 | rc = lyd_path_list_predicate(iter, &buffer, &buflen, &bufused, is_static); |
| 3055 | } |
| 3056 | break; |
| 3057 | case LYS_LEAFLIST: |
| 3058 | if (iter->schema->flags & LYS_CONFIG_W) { |
| 3059 | /* print leaf-list value */ |
| 3060 | rc = lyd_path_leaflist_predicate(iter, &buffer, &buflen, &bufused, is_static); |
| 3061 | } else { |
| 3062 | /* print its position */ |
| 3063 | rc = lyd_path_position_predicate(iter, &buffer, &buflen, &bufused, is_static); |
| 3064 | } |
| 3065 | break; |
| 3066 | default: |
| 3067 | /* nothing to print more */ |
| 3068 | break; |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 3069 | } |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 3070 | } |
| 3071 | if (rc != LY_SUCCESS) { |
| 3072 | break; |
| 3073 | } |
| 3074 | |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 3075 | --depth; |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 3076 | } |
| 3077 | break; |
| 3078 | } |
| 3079 | |
| 3080 | return buffer; |
| 3081 | } |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 3082 | |
Michal Vasko | 25a3282 | 2020-07-09 15:48:22 +0200 | [diff] [blame] | 3083 | API struct lyd_meta * |
| 3084 | lyd_find_meta(const struct lyd_meta *first, const struct lys_module *module, const char *name) |
| 3085 | { |
| 3086 | struct lyd_meta *ret = NULL; |
| 3087 | const struct ly_ctx *ctx; |
| 3088 | const char *prefix, *tmp; |
| 3089 | char *str; |
| 3090 | size_t pref_len, name_len; |
| 3091 | |
| 3092 | LY_CHECK_ARG_RET(NULL, module || strchr(name, ':'), name, NULL); |
| 3093 | |
| 3094 | if (!first) { |
| 3095 | return NULL; |
| 3096 | } |
| 3097 | |
| 3098 | ctx = first->annotation->module->ctx; |
| 3099 | |
| 3100 | /* parse the name */ |
| 3101 | tmp = name; |
| 3102 | if (ly_parse_nodeid(&tmp, &prefix, &pref_len, &name, &name_len) || tmp[0]) { |
| 3103 | LOGERR(ctx, LY_EINVAL, "Metadata name \"%s\" is not valid.", name); |
| 3104 | return NULL; |
| 3105 | } |
| 3106 | |
| 3107 | /* find the module */ |
| 3108 | if (prefix) { |
| 3109 | str = strndup(prefix, pref_len); |
| 3110 | module = ly_ctx_get_module_latest(ctx, str); |
| 3111 | free(str); |
| 3112 | LY_CHECK_ERR_RET(!module, LOGERR(ctx, LY_EINVAL, "Module \"%.*s\" not found.", pref_len, prefix), NULL); |
| 3113 | } |
| 3114 | |
| 3115 | /* find the metadata */ |
| 3116 | LY_LIST_FOR(first, first) { |
| 3117 | if ((first->annotation->module == module) && !strcmp(first->name, name)) { |
| 3118 | ret = (struct lyd_meta *)first; |
| 3119 | break; |
| 3120 | } |
| 3121 | } |
| 3122 | |
| 3123 | return ret; |
| 3124 | } |
| 3125 | |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 3126 | API LY_ERR |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 3127 | lyd_find_sibling_first(const struct lyd_node *siblings, const struct lyd_node *target, struct lyd_node **match) |
| 3128 | { |
| 3129 | struct lyd_node **match_p; |
| 3130 | struct lyd_node_inner *parent; |
| 3131 | |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 3132 | LY_CHECK_ARG_RET(NULL, target, LY_EINVAL); |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 3133 | |
Michal Vasko | 62ed12d | 2020-05-21 10:08:25 +0200 | [diff] [blame] | 3134 | if (!siblings || (lysc_data_parent(siblings->schema) != lysc_data_parent(target->schema))) { |
| 3135 | /* no data or schema mismatch */ |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 3136 | if (match) { |
| 3137 | *match = NULL; |
| 3138 | } |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 3139 | return LY_ENOTFOUND; |
| 3140 | } |
| 3141 | |
| 3142 | /* find first sibling */ |
| 3143 | if (siblings->parent) { |
| 3144 | siblings = siblings->parent->child; |
| 3145 | } else { |
| 3146 | while (siblings->prev->next) { |
| 3147 | siblings = siblings->prev; |
| 3148 | } |
| 3149 | } |
| 3150 | |
| 3151 | parent = (struct lyd_node_inner *)siblings->parent; |
| 3152 | if (parent && parent->children_ht) { |
| 3153 | assert(target->hash); |
| 3154 | |
| 3155 | /* find by hash */ |
| 3156 | if (!lyht_find(parent->children_ht, &target, target->hash, (void **)&match_p)) { |
Michal Vasko | da85903 | 2020-07-14 12:20:14 +0200 | [diff] [blame] | 3157 | /* check even value when needed */ |
Michal Vasko | 8f359bf | 2020-07-28 10:41:15 +0200 | [diff] [blame] | 3158 | 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] | 3159 | siblings = *match_p; |
| 3160 | } else { |
| 3161 | siblings = NULL; |
| 3162 | } |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 3163 | } else { |
| 3164 | /* not found */ |
| 3165 | siblings = NULL; |
| 3166 | } |
| 3167 | } else { |
| 3168 | /* no children hash table */ |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 3169 | for ( ; siblings; siblings = siblings->next) { |
Michal Vasko | 8f359bf | 2020-07-28 10:41:15 +0200 | [diff] [blame] | 3170 | if (!lyd_compare_single(siblings, target, 0)) { |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 3171 | break; |
| 3172 | } |
| 3173 | } |
| 3174 | } |
| 3175 | |
| 3176 | if (!siblings) { |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 3177 | if (match) { |
| 3178 | *match = NULL; |
| 3179 | } |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 3180 | return LY_ENOTFOUND; |
| 3181 | } |
| 3182 | |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 3183 | if (match) { |
| 3184 | *match = (struct lyd_node *)siblings; |
| 3185 | } |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 3186 | return LY_SUCCESS; |
| 3187 | } |
| 3188 | |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3189 | /** |
| 3190 | * @brief Comparison callback to match schema node with a schema of a data node. |
| 3191 | * |
| 3192 | * @param[in] val1_p Pointer to the schema node |
| 3193 | * @param[in] val2_p Pointer to the data node |
| 3194 | * Implementation of ::values_equal_cb. |
| 3195 | */ |
| 3196 | static ly_bool |
| 3197 | 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] | 3198 | { |
| 3199 | struct lysc_node *val1; |
| 3200 | struct lyd_node *val2; |
| 3201 | |
| 3202 | val1 = *((struct lysc_node **)val1_p); |
| 3203 | val2 = *((struct lyd_node **)val2_p); |
| 3204 | |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 3205 | if (val1 == val2->schema) { |
| 3206 | /* schema match is enough */ |
| 3207 | return 1; |
| 3208 | } else { |
| 3209 | return 0; |
| 3210 | } |
| 3211 | } |
| 3212 | |
| 3213 | static LY_ERR |
| 3214 | lyd_find_sibling_schema(const struct lyd_node *siblings, const struct lysc_node *schema, struct lyd_node **match) |
| 3215 | { |
| 3216 | struct lyd_node **match_p; |
| 3217 | struct lyd_node_inner *parent; |
| 3218 | uint32_t hash; |
| 3219 | values_equal_cb ht_cb; |
| 3220 | |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 3221 | assert(siblings && schema); |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 3222 | |
| 3223 | parent = (struct lyd_node_inner *)siblings->parent; |
| 3224 | if (parent && parent->children_ht) { |
| 3225 | /* calculate our hash */ |
| 3226 | hash = dict_hash_multi(0, schema->module->name, strlen(schema->module->name)); |
| 3227 | hash = dict_hash_multi(hash, schema->name, strlen(schema->name)); |
| 3228 | hash = dict_hash_multi(hash, NULL, 0); |
| 3229 | |
| 3230 | /* use special hash table function */ |
| 3231 | ht_cb = lyht_set_cb(parent->children_ht, lyd_hash_table_schema_val_equal); |
| 3232 | |
| 3233 | /* find by hash */ |
| 3234 | if (!lyht_find(parent->children_ht, &schema, hash, (void **)&match_p)) { |
| 3235 | siblings = *match_p; |
| 3236 | } else { |
| 3237 | /* not found */ |
| 3238 | siblings = NULL; |
| 3239 | } |
| 3240 | |
| 3241 | /* set the original hash table compare function back */ |
| 3242 | lyht_set_cb(parent->children_ht, ht_cb); |
| 3243 | } else { |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 3244 | /* find first sibling */ |
| 3245 | if (siblings->parent) { |
| 3246 | siblings = siblings->parent->child; |
| 3247 | } else { |
| 3248 | while (siblings->prev->next) { |
| 3249 | siblings = siblings->prev; |
| 3250 | } |
| 3251 | } |
| 3252 | |
| 3253 | /* search manually without hashes */ |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 3254 | for ( ; siblings; siblings = siblings->next) { |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 3255 | if (siblings->schema == schema) { |
| 3256 | /* schema match is enough */ |
| 3257 | break; |
| 3258 | } |
| 3259 | } |
| 3260 | } |
| 3261 | |
| 3262 | if (!siblings) { |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 3263 | if (match) { |
| 3264 | *match = NULL; |
| 3265 | } |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 3266 | return LY_ENOTFOUND; |
| 3267 | } |
| 3268 | |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 3269 | if (match) { |
| 3270 | *match = (struct lyd_node *)siblings; |
| 3271 | } |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 3272 | return LY_SUCCESS; |
| 3273 | } |
| 3274 | |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 3275 | API LY_ERR |
| 3276 | 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] | 3277 | size_t val_len, struct lyd_node **match) |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 3278 | { |
| 3279 | LY_ERR rc; |
| 3280 | struct lyd_node *target = NULL; |
| 3281 | |
Michal Vasko | 4c583e8 | 2020-07-17 12:16:14 +0200 | [diff] [blame] | 3282 | 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] | 3283 | |
Michal Vasko | 62ed12d | 2020-05-21 10:08:25 +0200 | [diff] [blame] | 3284 | if (!siblings || (lysc_data_parent(siblings->schema) != lysc_data_parent(schema))) { |
| 3285 | /* no data or schema mismatch */ |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 3286 | if (match) { |
| 3287 | *match = NULL; |
| 3288 | } |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 3289 | return LY_ENOTFOUND; |
| 3290 | } |
| 3291 | |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 3292 | if (key_or_value && !val_len) { |
| 3293 | val_len = strlen(key_or_value); |
| 3294 | } |
| 3295 | |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 3296 | if ((schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) && key_or_value) { |
| 3297 | /* create a data node and find the instance */ |
| 3298 | if (schema->nodetype == LYS_LEAFLIST) { |
| 3299 | /* target used attributes: schema, hash, value */ |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 3300 | rc = lyd_create_term(schema, key_or_value, val_len, NULL, LY_PREF_JSON, NULL, LYD_HINT_DATA, NULL, &target); |
| 3301 | LY_CHECK_RET(rc); |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 3302 | } else { |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 3303 | /* target used attributes: schema, hash, child (all keys) */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3304 | 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] | 3305 | } |
| 3306 | |
| 3307 | /* find it */ |
| 3308 | rc = lyd_find_sibling_first(siblings, target, match); |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 3309 | } else { |
| 3310 | /* find the first schema node instance */ |
| 3311 | rc = lyd_find_sibling_schema(siblings, schema, match); |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 3312 | } |
| 3313 | |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 3314 | lyd_free_tree(target); |
| 3315 | return rc; |
| 3316 | } |
Michal Vasko | ccc0234 | 2020-05-21 10:09:21 +0200 | [diff] [blame] | 3317 | |
| 3318 | API LY_ERR |
| 3319 | lyd_find_xpath(const struct lyd_node *ctx_node, const char *xpath, struct ly_set **set) |
| 3320 | { |
| 3321 | LY_ERR ret = LY_SUCCESS; |
| 3322 | struct lyxp_set xp_set; |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3323 | struct lyxp_expr *exp = NULL; |
Michal Vasko | ccc0234 | 2020-05-21 10:09:21 +0200 | [diff] [blame] | 3324 | uint32_t i; |
| 3325 | |
| 3326 | LY_CHECK_ARG_RET(NULL, ctx_node, xpath, set, LY_EINVAL); |
| 3327 | |
| 3328 | memset(&xp_set, 0, sizeof xp_set); |
| 3329 | |
| 3330 | /* compile expression */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3331 | ret = lyxp_expr_parse((struct ly_ctx *)LYD_CTX(ctx_node), xpath, 0, 1, &exp); |
| 3332 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | ccc0234 | 2020-05-21 10:09:21 +0200 | [diff] [blame] | 3333 | |
| 3334 | /* evaluate expression */ |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 3335 | 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] | 3336 | LY_CHECK_GOTO(ret, cleanup); |
| 3337 | |
| 3338 | /* allocate return set */ |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 3339 | ret = ly_set_new(set); |
| 3340 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | ccc0234 | 2020-05-21 10:09:21 +0200 | [diff] [blame] | 3341 | |
| 3342 | /* transform into ly_set */ |
| 3343 | if (xp_set.type == LYXP_SET_NODE_SET) { |
| 3344 | /* allocate memory for all the elements once (even though not all items must be elements but most likely will be) */ |
| 3345 | (*set)->objs = malloc(xp_set.used * sizeof *(*set)->objs); |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 3346 | 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] | 3347 | (*set)->size = xp_set.used; |
| 3348 | |
| 3349 | for (i = 0; i < xp_set.used; ++i) { |
| 3350 | if (xp_set.val.nodes[i].type == LYXP_NODE_ELEM) { |
Radek Krejci | 3d92e44 | 2020-10-12 12:48:13 +0200 | [diff] [blame] | 3351 | 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] | 3352 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | ccc0234 | 2020-05-21 10:09:21 +0200 | [diff] [blame] | 3353 | } |
| 3354 | } |
| 3355 | } |
| 3356 | |
| 3357 | cleanup: |
Michal Vasko | 0691d52 | 2020-05-21 13:21:47 +0200 | [diff] [blame] | 3358 | lyxp_set_free_content(&xp_set); |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 3359 | lyxp_expr_free((struct ly_ctx *)LYD_CTX(ctx_node), exp); |
Michal Vasko | ccc0234 | 2020-05-21 10:09:21 +0200 | [diff] [blame] | 3360 | return ret; |
| 3361 | } |