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