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