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