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