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