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