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