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