Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 1 | /** |
| 2 | * @file diff.c |
| 3 | * @author Michal Vasko <mvasko@cesnet.cz> |
| 4 | * @brief diff functions |
| 5 | * |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 6 | * Copyright (c) 2020 - 2021 CESNET, z.s.p.o. |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +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 | */ |
Christian Hopps | 32874e1 | 2021-05-01 09:43:54 -0400 | [diff] [blame] | 14 | #define _GNU_SOURCE /* asprintf, strdup */ |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 15 | |
| 16 | #include "diff.h" |
| 17 | |
| 18 | #include <assert.h> |
| 19 | #include <stddef.h> |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 20 | #include <stdint.h> |
| 21 | #include <stdio.h> |
Radek Krejci | 47fab89 | 2020-11-05 17:02:41 +0100 | [diff] [blame] | 22 | #include <stdlib.h> |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 23 | #include <string.h> |
| 24 | |
| 25 | #include "common.h" |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 26 | #include "compat.h" |
Radek Krejci | 47fab89 | 2020-11-05 17:02:41 +0100 | [diff] [blame] | 27 | #include "context.h" |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 28 | #include "log.h" |
Michal Vasko | 19175b6 | 2022-04-01 09:17:07 +0200 | [diff] [blame] | 29 | #include "plugins_exts.h" |
Michal Vasko | b475096 | 2022-10-06 15:33:35 +0200 | [diff] [blame] | 30 | #include "plugins_exts/metadata.h" |
Radek Krejci | 47fab89 | 2020-11-05 17:02:41 +0100 | [diff] [blame] | 31 | #include "plugins_types.h" |
| 32 | #include "set.h" |
| 33 | #include "tree.h" |
| 34 | #include "tree_data.h" |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 35 | #include "tree_data_internal.h" |
Radek Krejci | 859a15a | 2021-03-05 20:56:59 +0100 | [diff] [blame] | 36 | #include "tree_edit.h" |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 37 | #include "tree_schema.h" |
| 38 | #include "tree_schema_internal.h" |
| 39 | |
Michal Vasko | 52afd7d | 2022-01-18 14:08:34 +0100 | [diff] [blame] | 40 | #define LOGERR_META(ctx, meta_name, node) \ |
| 41 | { \ |
| 42 | char *__path = lyd_path(node, LYD_PATH_STD, NULL, 0); \ |
| 43 | LOGERR(ctx, LY_EINVAL, "Failed to find metadata \"%s\" for node \"%s\".", meta_name, __path); \ |
| 44 | free(__path); \ |
| 45 | } |
| 46 | |
| 47 | #define LOGERR_NOINST(ctx, node) \ |
| 48 | { \ |
| 49 | char *__path = lyd_path(node, LYD_PATH_STD, NULL, 0); \ |
| 50 | LOGERR(ctx, LY_EINVAL, "Failed to find node \"%s\" instance in data.", __path); \ |
| 51 | free(__path); \ |
| 52 | } |
| 53 | |
| 54 | #define LOGERR_UNEXPVAL(ctx, node, data_source) \ |
| 55 | { \ |
| 56 | char *__path = lyd_path(node, LYD_PATH_STD, NULL, 0); \ |
| 57 | LOGERR(ctx, LY_EINVAL, "Unexpected value of node \"%s\" in %s.", __path, data_source); \ |
| 58 | free(__path); \ |
| 59 | } |
| 60 | |
| 61 | #define LOGERR_MERGEOP(ctx, node, src_op, trg_op) \ |
| 62 | { \ |
| 63 | char *__path = lyd_path(node, LYD_PATH_STD, NULL, 0); \ |
| 64 | LOGERR(ctx, LY_EINVAL, "Unable to merge operation \"%s\" with \"%s\" for node \"%s\".", \ |
| 65 | lyd_diff_op2str(trg_op), lyd_diff_op2str(src_op), __path); \ |
| 66 | free(__path); \ |
| 67 | } |
| 68 | |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 69 | static const char * |
| 70 | lyd_diff_op2str(enum lyd_diff_op op) |
| 71 | { |
| 72 | switch (op) { |
| 73 | case LYD_DIFF_OP_CREATE: |
| 74 | return "create"; |
| 75 | case LYD_DIFF_OP_DELETE: |
| 76 | return "delete"; |
| 77 | case LYD_DIFF_OP_REPLACE: |
| 78 | return "replace"; |
| 79 | case LYD_DIFF_OP_NONE: |
| 80 | return "none"; |
| 81 | } |
| 82 | |
| 83 | LOGINT(NULL); |
| 84 | return NULL; |
| 85 | } |
| 86 | |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 87 | static enum lyd_diff_op |
| 88 | lyd_diff_str2op(const char *str) |
| 89 | { |
| 90 | switch (str[0]) { |
| 91 | case 'c': |
| 92 | assert(!strcmp(str, "create")); |
| 93 | return LYD_DIFF_OP_CREATE; |
| 94 | case 'd': |
| 95 | assert(!strcmp(str, "delete")); |
| 96 | return LYD_DIFF_OP_DELETE; |
| 97 | case 'r': |
| 98 | assert(!strcmp(str, "replace")); |
| 99 | return LYD_DIFF_OP_REPLACE; |
| 100 | case 'n': |
| 101 | assert(!strcmp(str, "none")); |
| 102 | return LYD_DIFF_OP_NONE; |
| 103 | } |
| 104 | |
| 105 | LOGINT(NULL); |
| 106 | return 0; |
| 107 | } |
| 108 | |
Michal Vasko | cffc3f9 | 2022-06-15 07:57:24 +0200 | [diff] [blame] | 109 | /** |
| 110 | * @brief Create diff metadata for a nested user-ordered node with the effective operation "create". |
| 111 | * |
| 112 | * @param[in] node User-rodered node to update. |
| 113 | * @return LY_ERR value. |
| 114 | */ |
| 115 | static LY_ERR |
| 116 | lyd_diff_add_create_nested_userord(struct lyd_node *node) |
| 117 | { |
| 118 | LY_ERR rc = LY_SUCCESS; |
| 119 | const char *meta_name, *meta_val; |
| 120 | size_t buflen = 0, bufused = 0; |
| 121 | uint32_t pos; |
| 122 | char *dyn = NULL; |
| 123 | |
| 124 | assert(lysc_is_userordered(node->schema)); |
| 125 | |
| 126 | /* get correct metadata name and value */ |
| 127 | if (lysc_is_dup_inst_list(node->schema)) { |
| 128 | meta_name = "yang:position"; |
| 129 | |
| 130 | pos = lyd_list_pos(node); |
| 131 | if (asprintf(&dyn, "%" PRIu32, pos) == -1) { |
| 132 | LOGMEM(LYD_CTX(node)); |
| 133 | rc = LY_EMEM; |
| 134 | goto cleanup; |
| 135 | } |
| 136 | meta_val = dyn; |
| 137 | } else if (node->schema->nodetype == LYS_LIST) { |
| 138 | meta_name = "yang:key"; |
| 139 | |
| 140 | if (node->prev->next && (node->prev->schema == node->schema)) { |
| 141 | LY_CHECK_GOTO(rc = lyd_path_list_predicate(node->prev, &dyn, &buflen, &bufused, 0), cleanup); |
| 142 | meta_val = dyn; |
| 143 | } else { |
| 144 | meta_val = ""; |
| 145 | } |
| 146 | } else { |
| 147 | meta_name = "yang:value"; |
| 148 | |
| 149 | if (node->prev->next && (node->prev->schema == node->schema)) { |
| 150 | meta_val = lyd_get_value(node->prev); |
| 151 | } else { |
| 152 | meta_val = ""; |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | /* create the metadata */ |
| 157 | LY_CHECK_GOTO(rc = lyd_new_meta(NULL, node, NULL, meta_name, meta_val, 0, NULL), cleanup); |
| 158 | |
| 159 | cleanup: |
| 160 | free(dyn); |
| 161 | return rc; |
| 162 | } |
| 163 | |
Michal Vasko | fb51a84 | 2023-06-20 08:50:24 +0200 | [diff] [blame^] | 164 | /** |
| 165 | * @brief Find metadata/an attribute of a node. |
| 166 | * |
| 167 | * @param[in] node Node to search. |
| 168 | * @param[in] name Metadata/attribute name. |
| 169 | * @param[out] meta Metadata found, NULL if not found. |
| 170 | * @param[out] attr Attribute found, NULL if not found. |
| 171 | */ |
| 172 | static void |
| 173 | lyd_diff_find_meta(const struct lyd_node *node, const char *name, struct lyd_meta **meta, struct lyd_attr **attr) |
| 174 | { |
| 175 | struct lyd_meta *m; |
| 176 | struct lyd_attr *a; |
| 177 | |
| 178 | if (meta) { |
| 179 | *meta = NULL; |
| 180 | } |
| 181 | if (attr) { |
| 182 | *attr = NULL; |
| 183 | } |
| 184 | |
| 185 | if (node->schema) { |
| 186 | assert(meta); |
| 187 | |
| 188 | LY_LIST_FOR(node->meta, m) { |
| 189 | if (!strcmp(m->name, name) && !strcmp(m->annotation->module->name, "yang")) { |
| 190 | *meta = m; |
| 191 | break; |
| 192 | } |
| 193 | } |
| 194 | } else { |
| 195 | assert(attr); |
| 196 | |
| 197 | LY_LIST_FOR(((struct lyd_node_opaq *)node)->attr, a) { |
| 198 | /* name */ |
| 199 | if (strcmp(a->name.name, name)) { |
| 200 | continue; |
| 201 | } |
| 202 | |
| 203 | /* module */ |
| 204 | switch (a->format) { |
| 205 | case LY_VALUE_JSON: |
| 206 | if (strcmp(a->name.module_name, "yang")) { |
| 207 | continue; |
| 208 | } |
| 209 | break; |
| 210 | case LY_VALUE_XML: |
| 211 | if (strcmp(a->name.module_ns, "urn:ietf:params:xml:ns:yang:1")) { |
| 212 | continue; |
| 213 | } |
| 214 | break; |
| 215 | default: |
| 216 | LOGINT(LYD_CTX(node)); |
| 217 | return; |
| 218 | } |
| 219 | |
| 220 | *attr = a; |
| 221 | break; |
| 222 | } |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | /** |
| 227 | * @brief Learn operation of a diff node. |
| 228 | * |
| 229 | * @param[in] diff_node Diff node. |
| 230 | * @param[out] op Operation. |
| 231 | * @return LY_ERR value. |
| 232 | */ |
| 233 | static LY_ERR |
| 234 | lyd_diff_get_op(const struct lyd_node *diff_node, enum lyd_diff_op *op) |
| 235 | { |
| 236 | struct lyd_meta *meta = NULL; |
| 237 | struct lyd_attr *attr = NULL; |
| 238 | const struct lyd_node *diff_parent; |
| 239 | const char *str; |
| 240 | char *path; |
| 241 | |
| 242 | for (diff_parent = diff_node; diff_parent; diff_parent = lyd_parent(diff_parent)) { |
| 243 | lyd_diff_find_meta(diff_parent, "operation", &meta, &attr); |
| 244 | if (!meta && !attr) { |
| 245 | continue; |
| 246 | } |
| 247 | |
| 248 | str = meta ? lyd_get_meta_value(meta) : attr->value; |
| 249 | if ((str[0] == 'r') && (diff_parent != diff_node)) { |
| 250 | /* we do not care about this operation if it's in our parent */ |
| 251 | continue; |
| 252 | } |
| 253 | *op = lyd_diff_str2op(str); |
| 254 | return LY_SUCCESS; |
| 255 | } |
| 256 | |
| 257 | /* operation not found */ |
| 258 | path = lyd_path(diff_node, LYD_PATH_STD, NULL, 0); |
| 259 | LOGERR(LYD_CTX(diff_node), LY_EINVAL, "Node \"%s\" without an operation.", path); |
| 260 | free(path); |
| 261 | return LY_EINT; |
| 262 | } |
| 263 | |
| 264 | /** |
| 265 | * @brief Remove metadata/an attribute from a node. |
| 266 | * |
| 267 | * @param[in] node Node to update. |
| 268 | * @param[in] name Metadata/attribute name. |
| 269 | */ |
| 270 | static void |
| 271 | lyd_diff_del_meta(struct lyd_node *node, const char *name) |
| 272 | { |
| 273 | struct lyd_meta *meta; |
| 274 | struct lyd_attr *attr; |
| 275 | |
| 276 | lyd_diff_find_meta(node, name, &meta, &attr); |
| 277 | |
| 278 | if (meta) { |
| 279 | lyd_free_meta_single(meta); |
| 280 | } else if (attr) { |
| 281 | lyd_free_attr_single(LYD_CTX(node), attr); |
| 282 | } |
| 283 | } |
| 284 | |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 285 | LY_ERR |
| 286 | lyd_diff_add(const struct lyd_node *node, enum lyd_diff_op op, const char *orig_default, const char *orig_value, |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 287 | const char *key, const char *value, const char *position, const char *orig_key, const char *orig_position, |
| 288 | struct lyd_node **diff) |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 289 | { |
Michal Vasko | cffc3f9 | 2022-06-15 07:57:24 +0200 | [diff] [blame] | 290 | struct lyd_node *dup, *siblings, *match = NULL, *diff_parent = NULL, *elem; |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 291 | const struct lyd_node *parent = NULL; |
Michal Vasko | fb51a84 | 2023-06-20 08:50:24 +0200 | [diff] [blame^] | 292 | enum lyd_diff_op cur_op; |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 293 | |
| 294 | assert(diff); |
| 295 | |
Michal Vasko | 53d4842 | 2020-11-13 18:02:29 +0100 | [diff] [blame] | 296 | /* replace leaf always needs orig-default and orig-value */ |
| 297 | assert((node->schema->nodetype != LYS_LEAF) || (op != LYD_DIFF_OP_REPLACE) || (orig_default && orig_value)); |
| 298 | |
| 299 | /* create on userord needs key/value */ |
| 300 | assert((node->schema->nodetype != LYS_LIST) || !(node->schema->flags & LYS_ORDBY_USER) || (op != LYD_DIFF_OP_CREATE) || |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 301 | (lysc_is_dup_inst_list(node->schema) && position) || key); |
Michal Vasko | 53d4842 | 2020-11-13 18:02:29 +0100 | [diff] [blame] | 302 | assert((node->schema->nodetype != LYS_LEAFLIST) || !(node->schema->flags & LYS_ORDBY_USER) || |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 303 | (op != LYD_DIFF_OP_CREATE) || (lysc_is_dup_inst_list(node->schema) && position) || value); |
Michal Vasko | 53d4842 | 2020-11-13 18:02:29 +0100 | [diff] [blame] | 304 | |
| 305 | /* move on userord needs both key and orig-key/value and orig-value */ |
| 306 | assert((node->schema->nodetype != LYS_LIST) || !(node->schema->flags & LYS_ORDBY_USER) || (op != LYD_DIFF_OP_REPLACE) || |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 307 | (lysc_is_dup_inst_list(node->schema) && position && orig_position) || (key && orig_key)); |
Michal Vasko | 53d4842 | 2020-11-13 18:02:29 +0100 | [diff] [blame] | 308 | assert((node->schema->nodetype != LYS_LEAFLIST) || !(node->schema->flags & LYS_ORDBY_USER) || |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 309 | (op != LYD_DIFF_OP_REPLACE) || (lysc_is_dup_inst_list(node->schema) && position && orig_position) || |
| 310 | (value && orig_value)); |
Michal Vasko | 53d4842 | 2020-11-13 18:02:29 +0100 | [diff] [blame] | 311 | |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 312 | /* find the first existing parent */ |
| 313 | siblings = *diff; |
Michal Vasko | fb51a84 | 2023-06-20 08:50:24 +0200 | [diff] [blame^] | 314 | do { |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 315 | /* find next node parent */ |
| 316 | parent = node; |
| 317 | while (parent->parent && (!diff_parent || (parent->parent->schema != diff_parent->schema))) { |
Michal Vasko | 9e68508 | 2021-01-29 14:49:09 +0100 | [diff] [blame] | 318 | parent = lyd_parent(parent); |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 319 | } |
Michal Vasko | fb51a84 | 2023-06-20 08:50:24 +0200 | [diff] [blame^] | 320 | |
| 321 | if (lysc_is_dup_inst_list(parent->schema)) { |
| 322 | /* assume it never exists, we are not able to distinguish whether it does or not */ |
| 323 | match = NULL; |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 324 | break; |
| 325 | } |
| 326 | |
| 327 | /* check whether it exists in the diff */ |
| 328 | if (lyd_find_sibling_first(siblings, parent, &match)) { |
| 329 | break; |
| 330 | } |
| 331 | |
| 332 | /* another parent found */ |
| 333 | diff_parent = match; |
| 334 | |
| 335 | /* move down in the diff */ |
Radek Krejci | a1c1e54 | 2020-09-29 16:06:52 +0200 | [diff] [blame] | 336 | siblings = lyd_child_no_keys(match); |
Michal Vasko | fb51a84 | 2023-06-20 08:50:24 +0200 | [diff] [blame^] | 337 | } while (parent != node); |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 338 | |
Michal Vasko | fb51a84 | 2023-06-20 08:50:24 +0200 | [diff] [blame^] | 339 | if (match && (parent == node)) { |
| 340 | /* special case when there is already an operation on our descendant */ |
| 341 | assert(!lyd_diff_get_op(diff_parent, &cur_op) && (cur_op == LYD_DIFF_OP_NONE)); |
| 342 | (void)cur_op; |
| 343 | |
| 344 | /* will be replaced by the new operation */ |
| 345 | lyd_diff_del_meta(diff_parent, "operation"); |
| 346 | dup = diff_parent; |
Michal Vasko | 695a7f2 | 2023-02-14 10:02:10 +0100 | [diff] [blame] | 347 | } else { |
Michal Vasko | fb51a84 | 2023-06-20 08:50:24 +0200 | [diff] [blame^] | 348 | /* duplicate the subtree (and connect to the diff if possible) */ |
| 349 | if (diff_parent) { |
| 350 | LY_CHECK_RET(lyd_dup_single_to_ctx(node, LYD_CTX(diff_parent), (struct lyd_node_inner *)diff_parent, |
| 351 | LYD_DUP_RECURSIVE | LYD_DUP_NO_META | LYD_DUP_WITH_PARENTS | LYD_DUP_WITH_FLAGS, &dup)); |
| 352 | } else { |
| 353 | LY_CHECK_RET(lyd_dup_single(node, NULL, |
| 354 | LYD_DUP_RECURSIVE | LYD_DUP_NO_META | LYD_DUP_WITH_PARENTS | LYD_DUP_WITH_FLAGS, &dup)); |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 355 | } |
Michal Vasko | fb51a84 | 2023-06-20 08:50:24 +0200 | [diff] [blame^] | 356 | |
| 357 | /* find the first duplicated parent */ |
| 358 | if (!diff_parent) { |
| 359 | diff_parent = lyd_parent(dup); |
| 360 | while (diff_parent && diff_parent->parent) { |
| 361 | diff_parent = lyd_parent(diff_parent); |
| 362 | } |
| 363 | } else { |
| 364 | diff_parent = dup; |
| 365 | while (diff_parent->parent && (diff_parent->parent->schema == parent->schema)) { |
| 366 | diff_parent = lyd_parent(diff_parent); |
| 367 | } |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 368 | } |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 369 | |
Michal Vasko | fb51a84 | 2023-06-20 08:50:24 +0200 | [diff] [blame^] | 370 | /* no parent existed, must be manually connected */ |
| 371 | if (!diff_parent) { |
| 372 | /* there actually was no parent to duplicate */ |
| 373 | lyd_insert_sibling(*diff, dup, diff); |
| 374 | } else if (!diff_parent->parent) { |
| 375 | lyd_insert_sibling(*diff, diff_parent, diff); |
| 376 | } |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 377 | |
Michal Vasko | fb51a84 | 2023-06-20 08:50:24 +0200 | [diff] [blame^] | 378 | /* add parent operation, if any */ |
| 379 | if (diff_parent && (diff_parent != dup)) { |
| 380 | LY_CHECK_RET(lyd_new_meta(NULL, diff_parent, NULL, "yang:operation", "none", 0, NULL)); |
| 381 | } |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 382 | } |
| 383 | |
| 384 | /* add subtree operation */ |
Michal Vasko | 2e55279 | 2022-11-02 12:15:31 +0100 | [diff] [blame] | 385 | LY_CHECK_RET(lyd_new_meta(NULL, dup, NULL, "yang:operation", lyd_diff_op2str(op), 0, NULL)); |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 386 | |
Michal Vasko | cffc3f9 | 2022-06-15 07:57:24 +0200 | [diff] [blame] | 387 | if (op == LYD_DIFF_OP_CREATE) { |
| 388 | /* all nested user-ordered (leaf-)lists need special metadata for create op */ |
| 389 | LYD_TREE_DFS_BEGIN(dup, elem) { |
| 390 | if ((elem != dup) && lysc_is_userordered(elem->schema)) { |
| 391 | LY_CHECK_RET(lyd_diff_add_create_nested_userord(elem)); |
| 392 | } |
| 393 | LYD_TREE_DFS_END(dup, elem); |
| 394 | } |
| 395 | } |
| 396 | |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 397 | /* orig-default */ |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 398 | if (orig_default) { |
Michal Vasko | 2e55279 | 2022-11-02 12:15:31 +0100 | [diff] [blame] | 399 | LY_CHECK_RET(lyd_new_meta(NULL, dup, NULL, "yang:orig-default", orig_default, 0, NULL)); |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 400 | } |
| 401 | |
| 402 | /* orig-value */ |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 403 | if (orig_value) { |
Michal Vasko | 2e55279 | 2022-11-02 12:15:31 +0100 | [diff] [blame] | 404 | LY_CHECK_RET(lyd_new_meta(NULL, dup, NULL, "yang:orig-value", orig_value, 0, NULL)); |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 405 | } |
| 406 | |
| 407 | /* key */ |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 408 | if (key) { |
Michal Vasko | 2e55279 | 2022-11-02 12:15:31 +0100 | [diff] [blame] | 409 | LY_CHECK_RET(lyd_new_meta(NULL, dup, NULL, "yang:key", key, 0, NULL)); |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 410 | } |
| 411 | |
| 412 | /* value */ |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 413 | if (value) { |
Michal Vasko | 2e55279 | 2022-11-02 12:15:31 +0100 | [diff] [blame] | 414 | LY_CHECK_RET(lyd_new_meta(NULL, dup, NULL, "yang:value", value, 0, NULL)); |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 415 | } |
| 416 | |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 417 | /* position */ |
| 418 | if (position) { |
Michal Vasko | 2e55279 | 2022-11-02 12:15:31 +0100 | [diff] [blame] | 419 | LY_CHECK_RET(lyd_new_meta(NULL, dup, NULL, "yang:position", position, 0, NULL)); |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 420 | } |
| 421 | |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 422 | /* orig-key */ |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 423 | if (orig_key) { |
Michal Vasko | 2e55279 | 2022-11-02 12:15:31 +0100 | [diff] [blame] | 424 | LY_CHECK_RET(lyd_new_meta(NULL, dup, NULL, "yang:orig-key", orig_key, 0, NULL)); |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 425 | } |
| 426 | |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 427 | /* orig-position */ |
| 428 | if (orig_position) { |
Michal Vasko | 2e55279 | 2022-11-02 12:15:31 +0100 | [diff] [blame] | 429 | LY_CHECK_RET(lyd_new_meta(NULL, dup, NULL, "yang:orig-position", orig_position, 0, NULL)); |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 430 | } |
| 431 | |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 432 | return LY_SUCCESS; |
| 433 | } |
| 434 | |
| 435 | /** |
| 436 | * @brief Get a userord entry for a specific user-ordered list/leaf-list. Create if does not exist yet. |
| 437 | * |
Michal Vasko | 1dcd73b | 2020-12-08 10:04:33 +0100 | [diff] [blame] | 438 | * @param[in] first Node from the first tree, can be NULL (on create). |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 439 | * @param[in] schema Schema node of the list/leaf-list. |
| 440 | * @param[in,out] userord Sized array of userord items. |
| 441 | * @return Userord item for all the user-ordered list/leaf-list instances. |
| 442 | */ |
| 443 | static struct lyd_diff_userord * |
| 444 | lyd_diff_userord_get(const struct lyd_node *first, const struct lysc_node *schema, struct lyd_diff_userord **userord) |
| 445 | { |
| 446 | struct lyd_diff_userord *item; |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 447 | struct lyd_node *iter; |
| 448 | const struct lyd_node **node; |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 449 | LY_ARRAY_COUNT_TYPE u; |
| 450 | |
| 451 | LY_ARRAY_FOR(*userord, u) { |
| 452 | if ((*userord)[u].schema == schema) { |
| 453 | return &(*userord)[u]; |
| 454 | } |
| 455 | } |
| 456 | |
| 457 | /* it was not added yet, add it now */ |
| 458 | LY_ARRAY_NEW_RET(schema->module->ctx, *userord, item, NULL); |
| 459 | |
| 460 | item->schema = schema; |
| 461 | item->pos = 0; |
| 462 | item->inst = NULL; |
| 463 | |
| 464 | /* store all the instance pointers in the current order */ |
| 465 | if (first) { |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 466 | LYD_LIST_FOR_INST(lyd_first_sibling(first), first->schema, iter) { |
| 467 | LY_ARRAY_NEW_RET(schema->module->ctx, item->inst, node, NULL); |
| 468 | *node = iter; |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 469 | } |
| 470 | } |
| 471 | |
| 472 | return item; |
| 473 | } |
| 474 | |
| 475 | /** |
| 476 | * @brief Get all the metadata to be stored in a diff for the 2 nodes. Can be used only for user-ordered |
| 477 | * lists/leaf-lists. |
| 478 | * |
| 479 | * @param[in] first Node from the first tree, can be NULL (on create). |
| 480 | * @param[in] second Node from the second tree, can be NULL (on delete). |
| 481 | * @param[in] options Diff options. |
Michal Vasko | 5da938a | 2022-03-01 09:19:02 +0100 | [diff] [blame] | 482 | * @param[in] userord_item Userord item of @p first and/or @p second node. |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 483 | * @param[out] op Operation. |
| 484 | * @param[out] orig_default Original default metadata. |
| 485 | * @param[out] value Value metadata. |
| 486 | * @param[out] orig_value Original value metadata |
| 487 | * @param[out] key Key metadata. |
| 488 | * @param[out] orig_key Original key metadata. |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 489 | * @param[out] position Position metadata. |
| 490 | * @param[out] orig_position Original position metadata. |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 491 | * @return LY_SUCCESS on success, |
| 492 | * @return LY_ENOT if there is no change to be added into diff, |
| 493 | * @return LY_ERR value on other errors. |
| 494 | */ |
| 495 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 496 | lyd_diff_userord_attrs(const struct lyd_node *first, const struct lyd_node *second, uint16_t options, |
Michal Vasko | 5da938a | 2022-03-01 09:19:02 +0100 | [diff] [blame] | 497 | struct lyd_diff_userord *userord_item, enum lyd_diff_op *op, const char **orig_default, char **value, |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 498 | char **orig_value, char **key, char **orig_key, char **position, char **orig_position) |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 499 | { |
Michal Vasko | f9b052a | 2022-06-08 10:26:53 +0200 | [diff] [blame] | 500 | LY_ERR rc = LY_SUCCESS; |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 501 | const struct lysc_node *schema; |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 502 | size_t buflen, bufused; |
Michal Vasko | ee9b948 | 2023-06-19 13:17:48 +0200 | [diff] [blame] | 503 | uint32_t first_pos, second_pos, comp_opts; |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 504 | |
| 505 | assert(first || second); |
| 506 | |
| 507 | *orig_default = NULL; |
| 508 | *value = NULL; |
| 509 | *orig_value = NULL; |
| 510 | *key = NULL; |
| 511 | *orig_key = NULL; |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 512 | *position = NULL; |
| 513 | *orig_position = NULL; |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 514 | |
| 515 | schema = first ? first->schema : second->schema; |
| 516 | assert(lysc_is_userordered(schema)); |
| 517 | |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 518 | /* find user-ordered first position */ |
| 519 | if (first) { |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 520 | for (first_pos = 0; first_pos < LY_ARRAY_COUNT(userord_item->inst); ++first_pos) { |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 521 | if (userord_item->inst[first_pos] == first) { |
| 522 | break; |
| 523 | } |
| 524 | } |
| 525 | assert(first_pos < LY_ARRAY_COUNT(userord_item->inst)); |
| 526 | } else { |
| 527 | first_pos = 0; |
| 528 | } |
| 529 | |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 530 | /* prepare position of the next instance */ |
| 531 | second_pos = userord_item->pos++; |
| 532 | |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 533 | /* learn operation first */ |
| 534 | if (!second) { |
| 535 | *op = LYD_DIFF_OP_DELETE; |
| 536 | } else if (!first) { |
| 537 | *op = LYD_DIFF_OP_CREATE; |
| 538 | } else { |
Michal Vasko | ee9b948 | 2023-06-19 13:17:48 +0200 | [diff] [blame] | 539 | comp_opts = lysc_is_dup_inst_list(second->schema) ? LYD_COMPARE_FULL_RECURSION : 0; |
| 540 | if (lyd_compare_single(second, userord_item->inst[second_pos], comp_opts)) { |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 541 | /* in first, there is a different instance on the second position, we are going to move 'first' node */ |
| 542 | *op = LYD_DIFF_OP_REPLACE; |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 543 | } else if ((options & LYD_DIFF_DEFAULTS) && ((first->flags & LYD_DEFAULT) != (second->flags & LYD_DEFAULT))) { |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 544 | /* default flag change */ |
| 545 | *op = LYD_DIFF_OP_NONE; |
| 546 | } else { |
| 547 | /* no changes */ |
| 548 | return LY_ENOT; |
| 549 | } |
| 550 | } |
| 551 | |
| 552 | /* |
| 553 | * set each attribute correctly based on the operation and node type |
| 554 | */ |
| 555 | |
| 556 | /* orig-default */ |
Michal Vasko | 4b715ca | 2020-11-11 18:39:57 +0100 | [diff] [blame] | 557 | if ((schema->nodetype == LYS_LEAFLIST) && ((*op == LYD_DIFF_OP_REPLACE) || (*op == LYD_DIFF_OP_NONE))) { |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 558 | if (first->flags & LYD_DEFAULT) { |
| 559 | *orig_default = "true"; |
| 560 | } else { |
| 561 | *orig_default = "false"; |
| 562 | } |
| 563 | } |
| 564 | |
| 565 | /* value */ |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 566 | if ((schema->nodetype == LYS_LEAFLIST) && !lysc_is_dup_inst_list(schema) && |
| 567 | ((*op == LYD_DIFF_OP_REPLACE) || (*op == LYD_DIFF_OP_CREATE))) { |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 568 | if (second_pos) { |
Radek Krejci | 6d5ba0c | 2021-04-26 07:49:59 +0200 | [diff] [blame] | 569 | *value = strdup(lyd_get_value(userord_item->inst[second_pos - 1])); |
Michal Vasko | f9b052a | 2022-06-08 10:26:53 +0200 | [diff] [blame] | 570 | LY_CHECK_ERR_GOTO(!*value, LOGMEM(schema->module->ctx); rc = LY_EMEM, cleanup); |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 571 | } else { |
| 572 | *value = strdup(""); |
Michal Vasko | f9b052a | 2022-06-08 10:26:53 +0200 | [diff] [blame] | 573 | LY_CHECK_ERR_GOTO(!*value, LOGMEM(schema->module->ctx); rc = LY_EMEM, cleanup); |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 574 | } |
| 575 | } |
| 576 | |
| 577 | /* orig-value */ |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 578 | if ((schema->nodetype == LYS_LEAFLIST) && !lysc_is_dup_inst_list(schema) && |
| 579 | ((*op == LYD_DIFF_OP_REPLACE) || (*op == LYD_DIFF_OP_DELETE))) { |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 580 | if (first_pos) { |
Radek Krejci | 6d5ba0c | 2021-04-26 07:49:59 +0200 | [diff] [blame] | 581 | *orig_value = strdup(lyd_get_value(userord_item->inst[first_pos - 1])); |
Michal Vasko | f9b052a | 2022-06-08 10:26:53 +0200 | [diff] [blame] | 582 | LY_CHECK_ERR_GOTO(!*orig_value, LOGMEM(schema->module->ctx); rc = LY_EMEM, cleanup); |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 583 | } else { |
| 584 | *orig_value = strdup(""); |
Michal Vasko | f9b052a | 2022-06-08 10:26:53 +0200 | [diff] [blame] | 585 | LY_CHECK_ERR_GOTO(!*orig_value, LOGMEM(schema->module->ctx); rc = LY_EMEM, cleanup); |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 586 | } |
| 587 | } |
| 588 | |
| 589 | /* key */ |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 590 | if ((schema->nodetype == LYS_LIST) && !lysc_is_dup_inst_list(schema) && |
| 591 | ((*op == LYD_DIFF_OP_REPLACE) || (*op == LYD_DIFF_OP_CREATE))) { |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 592 | if (second_pos) { |
| 593 | buflen = bufused = 0; |
Michal Vasko | f9b052a | 2022-06-08 10:26:53 +0200 | [diff] [blame] | 594 | LY_CHECK_GOTO(rc = lyd_path_list_predicate(userord_item->inst[second_pos - 1], key, &buflen, &bufused, 0), cleanup); |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 595 | } else { |
| 596 | *key = strdup(""); |
Michal Vasko | f9b052a | 2022-06-08 10:26:53 +0200 | [diff] [blame] | 597 | LY_CHECK_ERR_GOTO(!*key, LOGMEM(schema->module->ctx); rc = LY_EMEM, cleanup); |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 598 | } |
| 599 | } |
| 600 | |
| 601 | /* orig-key */ |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 602 | if ((schema->nodetype == LYS_LIST) && !lysc_is_dup_inst_list(schema) && |
| 603 | ((*op == LYD_DIFF_OP_REPLACE) || (*op == LYD_DIFF_OP_DELETE))) { |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 604 | if (first_pos) { |
| 605 | buflen = bufused = 0; |
Michal Vasko | f9b052a | 2022-06-08 10:26:53 +0200 | [diff] [blame] | 606 | LY_CHECK_GOTO(rc = lyd_path_list_predicate(userord_item->inst[first_pos - 1], orig_key, &buflen, &bufused, 0), cleanup); |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 607 | } else { |
| 608 | *orig_key = strdup(""); |
Michal Vasko | f9b052a | 2022-06-08 10:26:53 +0200 | [diff] [blame] | 609 | LY_CHECK_ERR_GOTO(!*orig_key, LOGMEM(schema->module->ctx); rc = LY_EMEM, cleanup); |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 610 | } |
| 611 | } |
| 612 | |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 613 | /* position */ |
| 614 | if (lysc_is_dup_inst_list(schema) && ((*op == LYD_DIFF_OP_REPLACE) || (*op == LYD_DIFF_OP_CREATE))) { |
| 615 | if (second_pos) { |
| 616 | if (asprintf(position, "%" PRIu32, second_pos) == -1) { |
| 617 | LOGMEM(schema->module->ctx); |
Michal Vasko | f9b052a | 2022-06-08 10:26:53 +0200 | [diff] [blame] | 618 | rc = LY_EMEM; |
| 619 | goto cleanup; |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 620 | } |
| 621 | } else { |
| 622 | *position = strdup(""); |
Michal Vasko | f9b052a | 2022-06-08 10:26:53 +0200 | [diff] [blame] | 623 | LY_CHECK_ERR_GOTO(!*position, LOGMEM(schema->module->ctx); rc = LY_EMEM, cleanup); |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 624 | } |
| 625 | } |
| 626 | |
| 627 | /* orig-position */ |
| 628 | if (lysc_is_dup_inst_list(schema) && ((*op == LYD_DIFF_OP_REPLACE) || (*op == LYD_DIFF_OP_DELETE))) { |
| 629 | if (first_pos) { |
| 630 | if (asprintf(orig_position, "%" PRIu32, first_pos) == -1) { |
| 631 | LOGMEM(schema->module->ctx); |
Michal Vasko | f9b052a | 2022-06-08 10:26:53 +0200 | [diff] [blame] | 632 | rc = LY_EMEM; |
| 633 | goto cleanup; |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 634 | } |
| 635 | } else { |
| 636 | *orig_position = strdup(""); |
Michal Vasko | f9b052a | 2022-06-08 10:26:53 +0200 | [diff] [blame] | 637 | LY_CHECK_ERR_GOTO(!*orig_position, LOGMEM(schema->module->ctx); rc = LY_EMEM, cleanup); |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 638 | } |
| 639 | } |
| 640 | |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 641 | /* |
| 642 | * update our instances - apply the change |
| 643 | */ |
| 644 | if (*op == LYD_DIFF_OP_CREATE) { |
| 645 | /* insert the instance */ |
Michal Vasko | f9b052a | 2022-06-08 10:26:53 +0200 | [diff] [blame] | 646 | LY_ARRAY_CREATE_GOTO(schema->module->ctx, userord_item->inst, 1, rc, cleanup); |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 647 | if (second_pos < LY_ARRAY_COUNT(userord_item->inst)) { |
| 648 | memmove(userord_item->inst + second_pos + 1, userord_item->inst + second_pos, |
| 649 | (LY_ARRAY_COUNT(userord_item->inst) - second_pos) * sizeof *userord_item->inst); |
| 650 | } |
| 651 | LY_ARRAY_INCREMENT(userord_item->inst); |
| 652 | userord_item->inst[second_pos] = second; |
| 653 | |
| 654 | } else if (*op == LYD_DIFF_OP_DELETE) { |
| 655 | /* remove the instance */ |
| 656 | if (first_pos + 1 < LY_ARRAY_COUNT(userord_item->inst)) { |
| 657 | memmove(userord_item->inst + first_pos, userord_item->inst + first_pos + 1, |
| 658 | (LY_ARRAY_COUNT(userord_item->inst) - first_pos - 1) * sizeof *userord_item->inst); |
| 659 | } |
| 660 | LY_ARRAY_DECREMENT(userord_item->inst); |
| 661 | |
| 662 | } else if (*op == LYD_DIFF_OP_REPLACE) { |
| 663 | /* move the instances */ |
| 664 | memmove(userord_item->inst + second_pos + 1, userord_item->inst + second_pos, |
| 665 | (first_pos - second_pos) * sizeof *userord_item->inst); |
| 666 | userord_item->inst[second_pos] = first; |
| 667 | } |
| 668 | |
Michal Vasko | f9b052a | 2022-06-08 10:26:53 +0200 | [diff] [blame] | 669 | cleanup: |
| 670 | if (rc) { |
| 671 | free(*value); |
| 672 | *value = NULL; |
| 673 | free(*orig_value); |
| 674 | *orig_value = NULL; |
| 675 | free(*key); |
| 676 | *key = NULL; |
| 677 | free(*orig_key); |
| 678 | *orig_key = NULL; |
| 679 | free(*position); |
| 680 | *position = NULL; |
| 681 | free(*orig_position); |
| 682 | *orig_position = NULL; |
| 683 | } |
| 684 | return rc; |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 685 | } |
| 686 | |
| 687 | /** |
| 688 | * @brief Get all the metadata to be stored in a diff for the 2 nodes. Cannot be used for user-ordered |
| 689 | * lists/leaf-lists. |
| 690 | * |
| 691 | * @param[in] first Node from the first tree, can be NULL (on create). |
| 692 | * @param[in] second Node from the second tree, can be NULL (on delete). |
| 693 | * @param[in] options Diff options. |
| 694 | * @param[out] op Operation. |
| 695 | * @param[out] orig_default Original default metadata. |
| 696 | * @param[out] orig_value Original value metadata. |
| 697 | * @return LY_SUCCESS on success, |
| 698 | * @return LY_ENOT if there is no change to be added into diff, |
| 699 | * @return LY_ERR value on other errors. |
| 700 | */ |
| 701 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 702 | lyd_diff_attrs(const struct lyd_node *first, const struct lyd_node *second, uint16_t options, enum lyd_diff_op *op, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 703 | const char **orig_default, char **orig_value) |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 704 | { |
| 705 | const struct lysc_node *schema; |
Michal Vasko | 6ea6fe2 | 2021-10-08 09:57:01 +0200 | [diff] [blame] | 706 | const char *str_val; |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 707 | |
| 708 | assert(first || second); |
| 709 | |
| 710 | *orig_default = NULL; |
| 711 | *orig_value = NULL; |
| 712 | |
| 713 | schema = first ? first->schema : second->schema; |
| 714 | assert(!lysc_is_userordered(schema)); |
| 715 | |
| 716 | /* learn operation first */ |
| 717 | if (!second) { |
| 718 | *op = LYD_DIFF_OP_DELETE; |
| 719 | } else if (!first) { |
| 720 | *op = LYD_DIFF_OP_CREATE; |
| 721 | } else { |
| 722 | switch (schema->nodetype) { |
| 723 | case LYS_CONTAINER: |
| 724 | case LYS_RPC: |
| 725 | case LYS_ACTION: |
| 726 | case LYS_NOTIF: |
| 727 | /* no changes */ |
| 728 | return LY_ENOT; |
| 729 | case LYS_LIST: |
| 730 | case LYS_LEAFLIST: |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 731 | if ((options & LYD_DIFF_DEFAULTS) && ((first->flags & LYD_DEFAULT) != (second->flags & LYD_DEFAULT))) { |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 732 | /* default flag change */ |
| 733 | *op = LYD_DIFF_OP_NONE; |
| 734 | } else { |
| 735 | /* no changes */ |
| 736 | return LY_ENOT; |
| 737 | } |
| 738 | break; |
| 739 | case LYS_LEAF: |
| 740 | case LYS_ANYXML: |
| 741 | case LYS_ANYDATA: |
Michal Vasko | 8f359bf | 2020-07-28 10:41:15 +0200 | [diff] [blame] | 742 | if (lyd_compare_single(first, second, 0)) { |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 743 | /* different values */ |
| 744 | *op = LYD_DIFF_OP_REPLACE; |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 745 | } else if ((options & LYD_DIFF_DEFAULTS) && ((first->flags & LYD_DEFAULT) != (second->flags & LYD_DEFAULT))) { |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 746 | /* default flag change */ |
| 747 | *op = LYD_DIFF_OP_NONE; |
| 748 | } else { |
| 749 | /* no changes */ |
| 750 | return LY_ENOT; |
| 751 | } |
| 752 | break; |
| 753 | default: |
| 754 | LOGINT_RET(schema->module->ctx); |
| 755 | } |
| 756 | } |
| 757 | |
| 758 | /* |
| 759 | * set each attribute correctly based on the operation and node type |
| 760 | */ |
| 761 | |
| 762 | /* orig-default */ |
Michal Vasko | 4b715ca | 2020-11-11 18:39:57 +0100 | [diff] [blame] | 763 | if ((schema->nodetype & LYD_NODE_TERM) && ((*op == LYD_DIFF_OP_REPLACE) || (*op == LYD_DIFF_OP_NONE))) { |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 764 | if (first->flags & LYD_DEFAULT) { |
| 765 | *orig_default = "true"; |
| 766 | } else { |
| 767 | *orig_default = "false"; |
| 768 | } |
| 769 | } |
| 770 | |
| 771 | /* orig-value */ |
Michal Vasko | baba84e | 2021-02-05 16:33:30 +0100 | [diff] [blame] | 772 | if ((schema->nodetype & (LYS_LEAF | LYS_ANYDATA)) && (*op == LYD_DIFF_OP_REPLACE)) { |
| 773 | if (schema->nodetype == LYS_LEAF) { |
Michal Vasko | 6ea6fe2 | 2021-10-08 09:57:01 +0200 | [diff] [blame] | 774 | str_val = lyd_get_value(first); |
| 775 | *orig_value = strdup(str_val ? str_val : ""); |
Michal Vasko | baba84e | 2021-02-05 16:33:30 +0100 | [diff] [blame] | 776 | LY_CHECK_ERR_RET(!*orig_value, LOGMEM(schema->module->ctx), LY_EMEM); |
| 777 | } else { |
| 778 | LY_CHECK_RET(lyd_any_value_str(first, orig_value)); |
| 779 | } |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 780 | } |
| 781 | |
| 782 | return LY_SUCCESS; |
| 783 | } |
| 784 | |
| 785 | /** |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 786 | * @brief Find a matching instance of a node in a data tree. |
| 787 | * |
| 788 | * @param[in] siblings Siblings to search in. |
| 789 | * @param[in] target Target node to search for. |
| 790 | * @param[in] defaults Whether to consider (or ignore) default values. |
Michal Vasko | 271d2e3 | 2023-01-31 15:43:19 +0100 | [diff] [blame] | 791 | * @param[in,out] dup_inst_ht Duplicate instance cache. |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 792 | * @param[out] match Found match, NULL if no matching node found. |
| 793 | * @return LY_ERR value. |
| 794 | */ |
| 795 | static LY_ERR |
| 796 | lyd_diff_find_match(const struct lyd_node *siblings, const struct lyd_node *target, ly_bool defaults, |
Michal Vasko | 8efac24 | 2023-03-30 08:24:56 +0200 | [diff] [blame] | 797 | struct ly_ht **dup_inst_ht, struct lyd_node **match) |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 798 | { |
Michal Vasko | 2bd856f | 2022-12-02 14:03:29 +0100 | [diff] [blame] | 799 | LY_ERR r; |
| 800 | |
Michal Vasko | db91fc3 | 2023-05-02 14:39:40 +0200 | [diff] [blame] | 801 | if (!target->schema) { |
| 802 | /* try to find the same opaque node */ |
| 803 | r = lyd_find_sibling_opaq_next(siblings, LYD_NAME(target), match); |
| 804 | } else if (target->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) { |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 805 | /* try to find the exact instance */ |
Michal Vasko | 2bd856f | 2022-12-02 14:03:29 +0100 | [diff] [blame] | 806 | r = lyd_find_sibling_first(siblings, target, match); |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 807 | } else { |
| 808 | /* try to simply find the node, there cannot be more instances */ |
Michal Vasko | 2bd856f | 2022-12-02 14:03:29 +0100 | [diff] [blame] | 809 | r = lyd_find_sibling_val(siblings, target->schema, NULL, 0, match); |
| 810 | } |
| 811 | if (r && (r != LY_ENOTFOUND)) { |
| 812 | return r; |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 813 | } |
| 814 | |
Michal Vasko | d7c048c | 2021-05-18 16:12:55 +0200 | [diff] [blame] | 815 | /* update match as needed */ |
Michal Vasko | 271d2e3 | 2023-01-31 15:43:19 +0100 | [diff] [blame] | 816 | LY_CHECK_RET(lyd_dup_inst_next(match, siblings, dup_inst_ht)); |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 817 | |
| 818 | if (*match && ((*match)->flags & LYD_DEFAULT) && !defaults) { |
| 819 | /* ignore default nodes */ |
| 820 | *match = NULL; |
| 821 | } |
| 822 | return LY_SUCCESS; |
| 823 | } |
| 824 | |
| 825 | /** |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 826 | * @brief Perform diff for all siblings at certain depth, recursively. |
| 827 | * |
| 828 | * For user-ordered lists/leaf-lists a specific structure is used for storing |
| 829 | * the current order. The idea is to apply all the generated diff changes |
| 830 | * virtually on the first tree so that we can continue to generate correct |
| 831 | * changes after some were already generated. |
| 832 | * |
| 833 | * The algorithm then uses second tree position-based changes with a before |
| 834 | * (preceding) item anchor. |
| 835 | * |
| 836 | * Example: |
| 837 | * |
| 838 | * Virtual first tree leaf-list order: |
| 839 | * 1 2 [3] 4 5 |
| 840 | * |
| 841 | * Second tree leaf-list order: |
| 842 | * 1 2 [5] 3 4 |
| 843 | * |
| 844 | * We are at the 3rd node now. We look at whether the nodes on the 3rd position |
| 845 | * match - they do not - move nodes so that the 3rd position node is final -> |
| 846 | * -> move node 5 to the 3rd position -> move node 5 after node 2. |
| 847 | * |
| 848 | * Required properties: |
| 849 | * Stored operations (move) should not be affected by later operations - |
| 850 | * - would cause a redundantly long list of operations, possibly inifinite. |
| 851 | * |
| 852 | * Implemenation justification: |
| 853 | * First, all delete operations and only then move/create operations are stored. |
| 854 | * Also, preceding anchor is used and after each iteration another node is |
| 855 | * at its final position. That results in the invariant that all preceding |
| 856 | * nodes are final and will not be changed by the later operations, meaning |
| 857 | * they can safely be used as anchors for the later operations. |
| 858 | * |
| 859 | * @param[in] first First tree first sibling. |
| 860 | * @param[in] second Second tree first sibling. |
| 861 | * @param[in] options Diff options. |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 862 | * @param[in] nosiblings Whether to skip following siblings. |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 863 | * @param[in,out] diff Diff to append to. |
| 864 | * @return LY_ERR value. |
| 865 | */ |
| 866 | static LY_ERR |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 867 | lyd_diff_siblings_r(const struct lyd_node *first, const struct lyd_node *second, uint16_t options, ly_bool nosiblings, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 868 | struct lyd_node **diff) |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 869 | { |
| 870 | LY_ERR ret = LY_SUCCESS; |
| 871 | const struct lyd_node *iter_first, *iter_second; |
| 872 | struct lyd_node *match_second, *match_first; |
Michal Vasko | 5da938a | 2022-03-01 09:19:02 +0100 | [diff] [blame] | 873 | struct lyd_diff_userord *userord = NULL, *userord_item; |
Michal Vasko | 8efac24 | 2023-03-30 08:24:56 +0200 | [diff] [blame] | 874 | struct ly_ht *dup_inst_first = NULL, *dup_inst_second = NULL; |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 875 | LY_ARRAY_COUNT_TYPE u; |
| 876 | enum lyd_diff_op op; |
| 877 | const char *orig_default; |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 878 | char *orig_value, *key, *value, *position, *orig_key, *orig_position; |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 879 | |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 880 | /* compare first tree to the second tree - delete, replace, none */ |
| 881 | LY_LIST_FOR(first, iter_first) { |
Michal Vasko | c825ed7 | 2021-07-21 16:05:59 +0200 | [diff] [blame] | 882 | if (!iter_first->schema) { |
| 883 | continue; |
| 884 | } |
| 885 | |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 886 | assert(!(iter_first->schema->flags & LYS_KEY)); |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 887 | if ((iter_first->flags & LYD_DEFAULT) && !(options & LYD_DIFF_DEFAULTS)) { |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 888 | /* skip default nodes */ |
| 889 | continue; |
| 890 | } |
| 891 | |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 892 | /* find a match in the second tree */ |
| 893 | LY_CHECK_GOTO(ret = lyd_diff_find_match(second, iter_first, options & LYD_DIFF_DEFAULTS, &dup_inst_second, |
| 894 | &match_second), cleanup); |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 895 | |
| 896 | if (lysc_is_userordered(iter_first->schema)) { |
Michal Vasko | 5da938a | 2022-03-01 09:19:02 +0100 | [diff] [blame] | 897 | /* get (create) userord entry */ |
| 898 | userord_item = lyd_diff_userord_get(iter_first, iter_first->schema, &userord); |
| 899 | LY_CHECK_ERR_GOTO(!userord_item, LOGMEM(LYD_CTX(iter_first)); ret = LY_EMEM, cleanup); |
| 900 | |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 901 | /* we are handling only user-ordered node delete now */ |
| 902 | if (!match_second) { |
| 903 | /* get all the attributes */ |
Michal Vasko | 5da938a | 2022-03-01 09:19:02 +0100 | [diff] [blame] | 904 | LY_CHECK_GOTO(ret = lyd_diff_userord_attrs(iter_first, match_second, options, userord_item, &op, |
| 905 | &orig_default, &value, &orig_value, &key, &orig_key, &position, &orig_position), cleanup); |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 906 | |
| 907 | /* there must be changes, it is deleted */ |
| 908 | assert(op == LYD_DIFF_OP_DELETE); |
Michal Vasko | 5da938a | 2022-03-01 09:19:02 +0100 | [diff] [blame] | 909 | ret = lyd_diff_add(iter_first, op, orig_default, orig_value, key, value, position, orig_key, |
| 910 | orig_position, diff); |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 911 | |
| 912 | free(orig_value); |
| 913 | free(key); |
| 914 | free(value); |
| 915 | free(position); |
| 916 | free(orig_key); |
| 917 | free(orig_position); |
| 918 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 919 | } |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 920 | } else { |
| 921 | /* get all the attributes */ |
| 922 | ret = lyd_diff_attrs(iter_first, match_second, options, &op, &orig_default, &orig_value); |
| 923 | |
| 924 | /* add into diff if there are any changes */ |
| 925 | if (!ret) { |
| 926 | if (op == LYD_DIFF_OP_DELETE) { |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 927 | ret = lyd_diff_add(iter_first, op, orig_default, orig_value, NULL, NULL, NULL, NULL, NULL, diff); |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 928 | } else { |
Michal Vasko | 3c2dd6c | 2020-11-06 17:38:55 +0100 | [diff] [blame] | 929 | assert(match_second); |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 930 | ret = lyd_diff_add(match_second, op, orig_default, orig_value, NULL, NULL, NULL, NULL, NULL, diff); |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 931 | } |
| 932 | |
| 933 | free(orig_value); |
| 934 | LY_CHECK_GOTO(ret, cleanup); |
| 935 | } else if (ret == LY_ENOT) { |
| 936 | ret = LY_SUCCESS; |
| 937 | } else { |
| 938 | goto cleanup; |
| 939 | } |
| 940 | } |
| 941 | |
| 942 | /* check descendants, if any, recursively */ |
| 943 | if (match_second) { |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 944 | LY_CHECK_GOTO(ret = lyd_diff_siblings_r(lyd_child_no_keys(iter_first), lyd_child_no_keys(match_second), |
| 945 | options, 0, diff), cleanup); |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 946 | } |
| 947 | |
| 948 | if (nosiblings) { |
| 949 | break; |
| 950 | } |
| 951 | } |
| 952 | |
| 953 | /* reset all cached positions */ |
| 954 | LY_ARRAY_FOR(userord, u) { |
| 955 | userord[u].pos = 0; |
| 956 | } |
| 957 | |
| 958 | /* compare second tree to the first tree - create, user-ordered move */ |
| 959 | LY_LIST_FOR(second, iter_second) { |
Michal Vasko | c825ed7 | 2021-07-21 16:05:59 +0200 | [diff] [blame] | 960 | if (!iter_second->schema) { |
| 961 | continue; |
| 962 | } |
| 963 | |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 964 | assert(!(iter_second->schema->flags & LYS_KEY)); |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 965 | if ((iter_second->flags & LYD_DEFAULT) && !(options & LYD_DIFF_DEFAULTS)) { |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 966 | /* skip default nodes */ |
| 967 | continue; |
| 968 | } |
| 969 | |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 970 | /* find a match in the first tree */ |
| 971 | LY_CHECK_GOTO(ret = lyd_diff_find_match(first, iter_second, options & LYD_DIFF_DEFAULTS, &dup_inst_first, |
| 972 | &match_first), cleanup); |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 973 | |
| 974 | if (lysc_is_userordered(iter_second->schema)) { |
Michal Vasko | 5da938a | 2022-03-01 09:19:02 +0100 | [diff] [blame] | 975 | /* get userord entry */ |
Michal Vasko | aa51cc5 | 2022-12-06 09:57:11 +0100 | [diff] [blame] | 976 | userord_item = lyd_diff_userord_get(match_first, iter_second->schema, &userord); |
Michal Vasko | 5da938a | 2022-03-01 09:19:02 +0100 | [diff] [blame] | 977 | LY_CHECK_ERR_GOTO(!userord_item, LOGMEM(LYD_CTX(iter_second)); ret = LY_EMEM, cleanup); |
| 978 | |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 979 | /* get all the attributes */ |
Michal Vasko | 5da938a | 2022-03-01 09:19:02 +0100 | [diff] [blame] | 980 | ret = lyd_diff_userord_attrs(match_first, iter_second, options, userord_item, &op, &orig_default, |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 981 | &value, &orig_value, &key, &orig_key, &position, &orig_position); |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 982 | |
| 983 | /* add into diff if there are any changes */ |
| 984 | if (!ret) { |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 985 | ret = lyd_diff_add(iter_second, op, orig_default, orig_value, key, value, position, orig_key, |
| 986 | orig_position, diff); |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 987 | |
| 988 | free(orig_value); |
| 989 | free(key); |
| 990 | free(value); |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 991 | free(position); |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 992 | free(orig_key); |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 993 | free(orig_position); |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 994 | LY_CHECK_GOTO(ret, cleanup); |
| 995 | } else if (ret == LY_ENOT) { |
| 996 | ret = LY_SUCCESS; |
| 997 | } else { |
| 998 | goto cleanup; |
| 999 | } |
| 1000 | } else if (!match_first) { |
| 1001 | /* get all the attributes */ |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1002 | LY_CHECK_GOTO(ret = lyd_diff_attrs(match_first, iter_second, options, &op, &orig_default, &orig_value), cleanup); |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 1003 | |
| 1004 | /* there must be changes, it is created */ |
| 1005 | assert(op == LYD_DIFF_OP_CREATE); |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1006 | ret = lyd_diff_add(iter_second, op, orig_default, orig_value, NULL, NULL, NULL, NULL, NULL, diff); |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 1007 | |
| 1008 | free(orig_value); |
| 1009 | LY_CHECK_GOTO(ret, cleanup); |
| 1010 | } /* else was handled */ |
| 1011 | |
| 1012 | if (nosiblings) { |
| 1013 | break; |
| 1014 | } |
| 1015 | } |
| 1016 | |
| 1017 | cleanup: |
Michal Vasko | d7c048c | 2021-05-18 16:12:55 +0200 | [diff] [blame] | 1018 | lyd_dup_inst_free(dup_inst_first); |
| 1019 | lyd_dup_inst_free(dup_inst_second); |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 1020 | LY_ARRAY_FOR(userord, u) { |
| 1021 | LY_ARRAY_FREE(userord[u].inst); |
| 1022 | } |
| 1023 | LY_ARRAY_FREE(userord); |
| 1024 | return ret; |
| 1025 | } |
| 1026 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1027 | static LY_ERR |
Michal Vasko | 5589617 | 2022-02-17 10:47:21 +0100 | [diff] [blame] | 1028 | lyd_diff(const struct lyd_node *first, const struct lyd_node *second, uint16_t options, ly_bool nosiblings, |
| 1029 | struct lyd_node **diff) |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 1030 | { |
| 1031 | const struct ly_ctx *ctx; |
| 1032 | |
| 1033 | LY_CHECK_ARG_RET(NULL, diff, LY_EINVAL); |
| 1034 | |
| 1035 | if (first) { |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 1036 | ctx = LYD_CTX(first); |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 1037 | } else if (second) { |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 1038 | ctx = LYD_CTX(second); |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 1039 | } else { |
| 1040 | ctx = NULL; |
| 1041 | } |
| 1042 | |
| 1043 | if (first && second && (lysc_data_parent(first->schema) != lysc_data_parent(second->schema))) { |
| 1044 | LOGERR(ctx, LY_EINVAL, "Invalid arguments - cannot create diff for unrelated data (%s()).", __func__); |
| 1045 | return LY_EINVAL; |
| 1046 | } |
| 1047 | |
| 1048 | *diff = NULL; |
| 1049 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1050 | return lyd_diff_siblings_r(first, second, options, nosiblings, diff); |
| 1051 | } |
| 1052 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 1053 | LIBYANG_API_DEF LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 1054 | lyd_diff_tree(const struct lyd_node *first, const struct lyd_node *second, uint16_t options, struct lyd_node **diff) |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1055 | { |
| 1056 | return lyd_diff(first, second, options, 1, diff); |
| 1057 | } |
| 1058 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 1059 | LIBYANG_API_DEF LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 1060 | lyd_diff_siblings(const struct lyd_node *first, const struct lyd_node *second, uint16_t options, struct lyd_node **diff) |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1061 | { |
| 1062 | return lyd_diff(first, second, options, 0, diff); |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 1063 | } |
| 1064 | |
| 1065 | /** |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 1066 | * @brief Insert a diff node into a data tree. |
| 1067 | * |
| 1068 | * @param[in,out] first_node First sibling of the data tree. |
| 1069 | * @param[in] parent_node Data tree sibling parent node. |
| 1070 | * @param[in] new_node Node to insert. |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1071 | * @param[in] userord_anchor Optional anchor (key, value, or position) of relative (leaf-)list instance. If not set, |
| 1072 | * the user-ordered instance will be inserted at the first position. |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 1073 | * @return err_info, NULL on success. |
| 1074 | */ |
| 1075 | static LY_ERR |
| 1076 | lyd_diff_insert(struct lyd_node **first_node, struct lyd_node *parent_node, struct lyd_node *new_node, |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1077 | const char *userord_anchor) |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 1078 | { |
| 1079 | LY_ERR ret; |
| 1080 | struct lyd_node *anchor; |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1081 | uint32_t pos, anchor_pos; |
| 1082 | int found; |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 1083 | |
| 1084 | assert(new_node); |
| 1085 | |
| 1086 | if (!*first_node) { |
| 1087 | if (!parent_node) { |
| 1088 | /* no parent or siblings */ |
| 1089 | *first_node = new_node; |
| 1090 | return LY_SUCCESS; |
| 1091 | } |
| 1092 | |
| 1093 | /* simply insert into parent, no other children */ |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1094 | if (userord_anchor) { |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 1095 | LOGERR(LYD_CTX(new_node), LY_EINVAL, "Node \"%s\" instance to insert next to not found.", |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 1096 | new_node->schema->name); |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 1097 | return LY_EINVAL; |
| 1098 | } |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 1099 | return lyd_insert_child(parent_node, new_node); |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 1100 | } |
| 1101 | |
Michal Vasko | 9e68508 | 2021-01-29 14:49:09 +0100 | [diff] [blame] | 1102 | assert(!(*first_node)->parent || (lyd_parent(*first_node) == parent_node)); |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 1103 | |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 1104 | if (!lysc_is_userordered(new_node->schema)) { |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 1105 | /* simple insert */ |
| 1106 | return lyd_insert_sibling(*first_node, new_node, first_node); |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 1107 | } |
| 1108 | |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1109 | if (userord_anchor) { |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 1110 | /* find the anchor sibling */ |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1111 | if (lysc_is_dup_inst_list(new_node->schema)) { |
| 1112 | anchor_pos = atoi(userord_anchor); |
Michal Vasko | 0ff9775 | 2022-01-18 16:35:41 +0100 | [diff] [blame] | 1113 | if (!anchor_pos) { |
| 1114 | LOGERR(LYD_CTX(new_node), LY_EINVAL, "Invalid user-ordered anchor value \"%s\".", userord_anchor); |
| 1115 | return LY_EINVAL; |
| 1116 | } |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1117 | |
| 1118 | found = 0; |
| 1119 | pos = 1; |
| 1120 | LYD_LIST_FOR_INST(*first_node, new_node->schema, anchor) { |
| 1121 | if (pos == anchor_pos) { |
| 1122 | found = 1; |
| 1123 | break; |
| 1124 | } |
| 1125 | ++pos; |
| 1126 | } |
| 1127 | if (!found) { |
| 1128 | LOGERR(LYD_CTX(new_node), LY_EINVAL, "Node \"%s\" instance to insert next to not found.", |
| 1129 | new_node->schema->name); |
| 1130 | return LY_EINVAL; |
| 1131 | } |
| 1132 | } else { |
| 1133 | ret = lyd_find_sibling_val(*first_node, new_node->schema, userord_anchor, 0, &anchor); |
| 1134 | if (ret == LY_ENOTFOUND) { |
| 1135 | LOGERR(LYD_CTX(new_node), LY_EINVAL, "Node \"%s\" instance to insert next to not found.", |
| 1136 | new_node->schema->name); |
| 1137 | return LY_EINVAL; |
| 1138 | } else if (ret) { |
| 1139 | return ret; |
| 1140 | } |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 1141 | } |
| 1142 | |
| 1143 | /* insert after */ |
| 1144 | LY_CHECK_RET(lyd_insert_after(anchor, new_node)); |
| 1145 | assert(new_node->prev == anchor); |
| 1146 | if (*first_node == new_node) { |
| 1147 | *first_node = anchor; |
| 1148 | } |
| 1149 | } else { |
Michal Vasko | ea7d323 | 2022-04-19 12:01:36 +0200 | [diff] [blame] | 1150 | /* find the first instance */ |
| 1151 | ret = lyd_find_sibling_val(*first_node, new_node->schema, NULL, 0, &anchor); |
| 1152 | LY_CHECK_RET(ret && (ret != LY_ENOTFOUND), ret); |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 1153 | |
Michal Vasko | ea7d323 | 2022-04-19 12:01:36 +0200 | [diff] [blame] | 1154 | if (anchor) { |
| 1155 | /* insert before the first instance */ |
| 1156 | LY_CHECK_RET(lyd_insert_before(anchor, new_node)); |
| 1157 | if ((*first_node)->prev->next) { |
| 1158 | assert(!new_node->prev->next); |
| 1159 | *first_node = new_node; |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 1160 | } |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 1161 | } else { |
Michal Vasko | ea7d323 | 2022-04-19 12:01:36 +0200 | [diff] [blame] | 1162 | /* insert anywhere */ |
| 1163 | LY_CHECK_RET(lyd_insert_sibling(*first_node, new_node, first_node)); |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 1164 | } |
| 1165 | } |
| 1166 | |
| 1167 | return LY_SUCCESS; |
| 1168 | } |
| 1169 | |
| 1170 | /** |
| 1171 | * @brief Apply diff subtree on data tree nodes, recursively. |
| 1172 | * |
| 1173 | * @param[in,out] first_node First sibling of the data tree. |
| 1174 | * @param[in] parent_node Parent of the first sibling. |
| 1175 | * @param[in] diff_node Current diff node. |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1176 | * @param[in] diff_cb Optional diff callback. |
| 1177 | * @param[in] cb_data User data for @p diff_cb. |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1178 | * @param[in,out] dup_inst Duplicate instance cache for all @p diff_node siblings. |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 1179 | * @return LY_ERR value. |
| 1180 | */ |
| 1181 | static LY_ERR |
| 1182 | lyd_diff_apply_r(struct lyd_node **first_node, struct lyd_node *parent_node, const struct lyd_node *diff_node, |
Michal Vasko | 8efac24 | 2023-03-30 08:24:56 +0200 | [diff] [blame] | 1183 | lyd_diff_cb diff_cb, void *cb_data, struct ly_ht **dup_inst) |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 1184 | { |
| 1185 | LY_ERR ret; |
| 1186 | struct lyd_node *match, *diff_child; |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1187 | const char *str_val, *meta_str; |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1188 | enum lyd_diff_op op; |
| 1189 | struct lyd_meta *meta; |
Michal Vasko | 8efac24 | 2023-03-30 08:24:56 +0200 | [diff] [blame] | 1190 | struct ly_ht *child_dup_inst = NULL; |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 1191 | const struct ly_ctx *ctx = LYD_CTX(diff_node); |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 1192 | |
| 1193 | /* read all the valid attributes */ |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1194 | LY_CHECK_RET(lyd_diff_get_op(diff_node, &op)); |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 1195 | |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1196 | /* handle specific user-ordered (leaf-)lists operations separately */ |
| 1197 | if (lysc_is_userordered(diff_node->schema) && ((op == LYD_DIFF_OP_CREATE) || (op == LYD_DIFF_OP_REPLACE))) { |
| 1198 | if (op == LYD_DIFF_OP_REPLACE) { |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 1199 | /* find the node (we must have some siblings because the node was only moved) */ |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1200 | LY_CHECK_RET(lyd_diff_find_match(*first_node, diff_node, 1, dup_inst, &match)); |
Michal Vasko | 52afd7d | 2022-01-18 14:08:34 +0100 | [diff] [blame] | 1201 | LY_CHECK_ERR_RET(!match, LOGERR_NOINST(ctx, diff_node), LY_EINVAL); |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 1202 | } else { |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1203 | /* duplicate the node */ |
| 1204 | LY_CHECK_RET(lyd_dup_single(diff_node, NULL, LYD_DUP_NO_META, &match)); |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 1205 | } |
| 1206 | |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1207 | /* get "key", "value", or "position" metadata string value */ |
| 1208 | if (lysc_is_dup_inst_list(diff_node->schema)) { |
| 1209 | meta_str = "yang:position"; |
| 1210 | } else if (diff_node->schema->nodetype == LYS_LIST) { |
| 1211 | meta_str = "yang:key"; |
| 1212 | } else { |
| 1213 | meta_str = "yang:value"; |
| 1214 | } |
| 1215 | meta = lyd_find_meta(diff_node->meta, NULL, meta_str); |
Michal Vasko | 52afd7d | 2022-01-18 14:08:34 +0100 | [diff] [blame] | 1216 | LY_CHECK_ERR_RET(!meta, LOGERR_META(ctx, meta_str, diff_node), LY_EINVAL); |
Radek Krejci | 6d5ba0c | 2021-04-26 07:49:59 +0200 | [diff] [blame] | 1217 | str_val = lyd_get_meta_value(meta); |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1218 | |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 1219 | /* insert/move the node */ |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1220 | if (str_val[0]) { |
| 1221 | ret = lyd_diff_insert(first_node, parent_node, match, str_val); |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 1222 | } else { |
| 1223 | ret = lyd_diff_insert(first_node, parent_node, match, NULL); |
| 1224 | } |
| 1225 | if (ret) { |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1226 | if (op == LYD_DIFF_OP_CREATE) { |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 1227 | lyd_free_tree(match); |
| 1228 | } |
| 1229 | return ret; |
| 1230 | } |
| 1231 | |
| 1232 | goto next_iter_r; |
| 1233 | } |
| 1234 | |
| 1235 | /* apply operation */ |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1236 | switch (op) { |
| 1237 | case LYD_DIFF_OP_NONE: |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 1238 | /* find the node */ |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1239 | LY_CHECK_RET(lyd_diff_find_match(*first_node, diff_node, 1, dup_inst, &match)); |
Michal Vasko | 52afd7d | 2022-01-18 14:08:34 +0100 | [diff] [blame] | 1240 | LY_CHECK_ERR_RET(!match, LOGERR_NOINST(ctx, diff_node), LY_EINVAL); |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 1241 | |
| 1242 | if (match->schema->nodetype & LYD_NODE_TERM) { |
| 1243 | /* special case of only dflt flag change */ |
| 1244 | if (diff_node->flags & LYD_DEFAULT) { |
| 1245 | match->flags |= LYD_DEFAULT; |
| 1246 | } else { |
| 1247 | match->flags &= ~LYD_DEFAULT; |
| 1248 | } |
| 1249 | } else { |
| 1250 | /* none operation on nodes without children is redundant and hence forbidden */ |
Radek Krejci | a1c1e54 | 2020-09-29 16:06:52 +0200 | [diff] [blame] | 1251 | if (!lyd_child_no_keys(diff_node)) { |
Michal Vasko | 0ff9775 | 2022-01-18 16:35:41 +0100 | [diff] [blame] | 1252 | LOGERR(ctx, LY_EINVAL, "Operation \"none\" is invalid for node \"%s\" without children.", |
| 1253 | LYD_NAME(diff_node)); |
| 1254 | return LY_EINVAL; |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 1255 | } |
| 1256 | } |
| 1257 | break; |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1258 | case LYD_DIFF_OP_CREATE: |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 1259 | /* duplicate the node */ |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1260 | LY_CHECK_RET(lyd_dup_single(diff_node, NULL, LYD_DUP_NO_META, &match)); |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 1261 | |
| 1262 | /* insert it at the end */ |
| 1263 | ret = 0; |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 1264 | if (parent_node) { |
Michal Vasko | 19175b6 | 2022-04-01 09:17:07 +0200 | [diff] [blame] | 1265 | if (match->flags & LYD_EXT) { |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 1266 | ret = lyplg_ext_insert(parent_node, match); |
Michal Vasko | 19175b6 | 2022-04-01 09:17:07 +0200 | [diff] [blame] | 1267 | } else { |
| 1268 | ret = lyd_insert_child(parent_node, match); |
| 1269 | } |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 1270 | } else { |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 1271 | ret = lyd_insert_sibling(*first_node, match, first_node); |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 1272 | } |
| 1273 | if (ret) { |
| 1274 | lyd_free_tree(match); |
| 1275 | return ret; |
| 1276 | } |
| 1277 | |
| 1278 | break; |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1279 | case LYD_DIFF_OP_DELETE: |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 1280 | /* find the node */ |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1281 | LY_CHECK_RET(lyd_diff_find_match(*first_node, diff_node, 1, dup_inst, &match)); |
Michal Vasko | 52afd7d | 2022-01-18 14:08:34 +0100 | [diff] [blame] | 1282 | LY_CHECK_ERR_RET(!match, LOGERR_NOINST(ctx, diff_node), LY_EINVAL); |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 1283 | |
| 1284 | /* remove it */ |
| 1285 | if ((match == *first_node) && !match->parent) { |
| 1286 | assert(!parent_node); |
| 1287 | /* we have removed the top-level node */ |
| 1288 | *first_node = (*first_node)->next; |
| 1289 | } |
| 1290 | lyd_free_tree(match); |
| 1291 | |
| 1292 | /* we are not going recursively in this case, the whole subtree was already deleted */ |
| 1293 | return LY_SUCCESS; |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1294 | case LYD_DIFF_OP_REPLACE: |
Michal Vasko | 0ff9775 | 2022-01-18 16:35:41 +0100 | [diff] [blame] | 1295 | if (!(diff_node->schema->nodetype & (LYS_LEAF | LYS_ANYDATA))) { |
| 1296 | LOGERR(ctx, LY_EINVAL, "Operation \"replace\" is invalid for %s node \"%s\".", |
| 1297 | lys_nodetype2str(diff_node->schema->nodetype), LYD_NAME(diff_node)); |
| 1298 | return LY_EINVAL; |
| 1299 | } |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 1300 | |
| 1301 | /* find the node */ |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1302 | LY_CHECK_RET(lyd_diff_find_match(*first_node, diff_node, 1, dup_inst, &match)); |
Michal Vasko | 52afd7d | 2022-01-18 14:08:34 +0100 | [diff] [blame] | 1303 | LY_CHECK_ERR_RET(!match, LOGERR_NOINST(ctx, diff_node), LY_EINVAL); |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 1304 | |
Michal Vasko | baba84e | 2021-02-05 16:33:30 +0100 | [diff] [blame] | 1305 | /* update the value */ |
| 1306 | if (diff_node->schema->nodetype == LYS_LEAF) { |
Radek Krejci | 6d5ba0c | 2021-04-26 07:49:59 +0200 | [diff] [blame] | 1307 | ret = lyd_change_term(match, lyd_get_value(diff_node)); |
Michal Vasko | 52afd7d | 2022-01-18 14:08:34 +0100 | [diff] [blame] | 1308 | LY_CHECK_ERR_RET(ret && (ret != LY_EEXIST), LOGERR_UNEXPVAL(ctx, match, "data"), LY_EINVAL); |
Michal Vasko | baba84e | 2021-02-05 16:33:30 +0100 | [diff] [blame] | 1309 | } else { |
| 1310 | struct lyd_node_any *any = (struct lyd_node_any *)diff_node; |
Michal Vasko | 26bbb27 | 2022-08-02 14:54:33 +0200 | [diff] [blame] | 1311 | |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1312 | LY_CHECK_RET(lyd_any_copy_value(match, &any->value, any->value_type)); |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 1313 | } |
| 1314 | |
| 1315 | /* with flags */ |
| 1316 | match->flags = diff_node->flags; |
| 1317 | break; |
| 1318 | default: |
| 1319 | LOGINT_RET(ctx); |
| 1320 | } |
| 1321 | |
| 1322 | next_iter_r: |
| 1323 | if (diff_cb) { |
| 1324 | /* call callback */ |
| 1325 | LY_CHECK_RET(diff_cb(diff_node, match, cb_data)); |
| 1326 | } |
| 1327 | |
| 1328 | /* apply diff recursively */ |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1329 | ret = LY_SUCCESS; |
Radek Krejci | a1c1e54 | 2020-09-29 16:06:52 +0200 | [diff] [blame] | 1330 | LY_LIST_FOR(lyd_child_no_keys(diff_node), diff_child) { |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1331 | ret = lyd_diff_apply_r(lyd_node_child_p(match), match, diff_child, diff_cb, cb_data, &child_dup_inst); |
| 1332 | if (ret) { |
| 1333 | break; |
| 1334 | } |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 1335 | } |
| 1336 | |
Michal Vasko | d7c048c | 2021-05-18 16:12:55 +0200 | [diff] [blame] | 1337 | lyd_dup_inst_free(child_dup_inst); |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1338 | return ret; |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 1339 | } |
| 1340 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 1341 | LIBYANG_API_DEF LY_ERR |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 1342 | lyd_diff_apply_module(struct lyd_node **data, const struct lyd_node *diff, const struct lys_module *mod, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 1343 | lyd_diff_cb diff_cb, void *cb_data) |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 1344 | { |
| 1345 | const struct lyd_node *root; |
Michal Vasko | 8efac24 | 2023-03-30 08:24:56 +0200 | [diff] [blame] | 1346 | struct ly_ht *dup_inst = NULL; |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1347 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 1348 | |
| 1349 | LY_LIST_FOR(diff, root) { |
| 1350 | if (mod && (lyd_owner_module(root) != mod)) { |
| 1351 | /* skip data nodes from different modules */ |
| 1352 | continue; |
| 1353 | } |
| 1354 | |
| 1355 | /* apply relevant nodes from the diff datatree */ |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1356 | ret = lyd_diff_apply_r(data, NULL, root, diff_cb, cb_data, &dup_inst); |
| 1357 | if (ret) { |
| 1358 | break; |
| 1359 | } |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 1360 | } |
| 1361 | |
Michal Vasko | d7c048c | 2021-05-18 16:12:55 +0200 | [diff] [blame] | 1362 | lyd_dup_inst_free(dup_inst); |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1363 | return ret; |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 1364 | } |
| 1365 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 1366 | LIBYANG_API_DEF LY_ERR |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1367 | lyd_diff_apply_all(struct lyd_node **data, const struct lyd_node *diff) |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 1368 | { |
| 1369 | return lyd_diff_apply_module(data, diff, NULL, NULL, NULL); |
| 1370 | } |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1371 | |
| 1372 | /** |
| 1373 | * @brief Update operations on a diff node when the new operation is NONE. |
| 1374 | * |
| 1375 | * @param[in] diff_match Node from the diff. |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1376 | * @param[in] cur_op Current operation of @p diff_match. |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1377 | * @param[in] src_diff Current source diff node. |
| 1378 | * @return LY_ERR value. |
| 1379 | */ |
| 1380 | static LY_ERR |
| 1381 | lyd_diff_merge_none(struct lyd_node *diff_match, enum lyd_diff_op cur_op, const struct lyd_node *src_diff) |
| 1382 | { |
| 1383 | switch (cur_op) { |
| 1384 | case LYD_DIFF_OP_NONE: |
| 1385 | case LYD_DIFF_OP_CREATE: |
| 1386 | case LYD_DIFF_OP_REPLACE: |
| 1387 | if (src_diff->schema->nodetype & LYD_NODE_TERM) { |
| 1388 | /* NONE on a term means only its dflt flag was changed */ |
| 1389 | diff_match->flags &= ~LYD_DEFAULT; |
| 1390 | diff_match->flags |= src_diff->flags & LYD_DEFAULT; |
| 1391 | } |
| 1392 | break; |
| 1393 | default: |
| 1394 | /* delete operation is not valid */ |
Michal Vasko | 52afd7d | 2022-01-18 14:08:34 +0100 | [diff] [blame] | 1395 | LOGERR_MERGEOP(LYD_CTX(diff_match), diff_match, cur_op, LYD_DIFF_OP_NONE); |
| 1396 | return LY_EINVAL; |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1397 | } |
| 1398 | |
| 1399 | return LY_SUCCESS; |
| 1400 | } |
| 1401 | |
| 1402 | /** |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1403 | * @brief Set a specific operation of a node. Delete the previous operation, if any. |
Michal Vasko | 871a025 | 2020-11-11 18:35:24 +0100 | [diff] [blame] | 1404 | * Does not change the default flag. |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1405 | * |
| 1406 | * @param[in] node Node to change. |
| 1407 | * @param[in] op Operation to set. |
| 1408 | * @return LY_ERR value. |
| 1409 | */ |
| 1410 | static LY_ERR |
| 1411 | lyd_diff_change_op(struct lyd_node *node, enum lyd_diff_op op) |
| 1412 | { |
Michal Vasko | db91fc3 | 2023-05-02 14:39:40 +0200 | [diff] [blame] | 1413 | lyd_diff_del_meta(node, "operation"); |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1414 | |
Michal Vasko | db91fc3 | 2023-05-02 14:39:40 +0200 | [diff] [blame] | 1415 | if (node->schema) { |
| 1416 | return lyd_new_meta(LYD_CTX(node), node, NULL, "yang:operation", lyd_diff_op2str(op), 0, NULL); |
| 1417 | } else { |
| 1418 | return lyd_new_attr(node, "yang", "operation", lyd_diff_op2str(op), NULL); |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1419 | } |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1420 | } |
| 1421 | |
| 1422 | /** |
| 1423 | * @brief Update operations on a diff node when the new operation is REPLACE. |
| 1424 | * |
| 1425 | * @param[in] diff_match Node from the diff. |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1426 | * @param[in] cur_op Current operation of @p diff_match. |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1427 | * @param[in] src_diff Current source diff node. |
| 1428 | * @return LY_ERR value. |
| 1429 | */ |
| 1430 | static LY_ERR |
| 1431 | lyd_diff_merge_replace(struct lyd_node *diff_match, enum lyd_diff_op cur_op, const struct lyd_node *src_diff) |
| 1432 | { |
| 1433 | LY_ERR ret; |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1434 | const char *str_val, *meta_name, *orig_meta_name; |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1435 | struct lyd_meta *meta; |
| 1436 | const struct lys_module *mod; |
| 1437 | const struct lyd_node_any *any; |
Michal Vasko | 52afd7d | 2022-01-18 14:08:34 +0100 | [diff] [blame] | 1438 | const struct ly_ctx *ctx = LYD_CTX(diff_match); |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1439 | |
| 1440 | /* get "yang" module for the metadata */ |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 1441 | mod = ly_ctx_get_module_latest(LYD_CTX(diff_match), "yang"); |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1442 | assert(mod); |
| 1443 | |
| 1444 | switch (cur_op) { |
| 1445 | case LYD_DIFF_OP_REPLACE: |
| 1446 | case LYD_DIFF_OP_CREATE: |
| 1447 | switch (diff_match->schema->nodetype) { |
| 1448 | case LYS_LIST: |
| 1449 | case LYS_LEAFLIST: |
Michal Vasko | 4231fb6 | 2020-07-13 13:54:47 +0200 | [diff] [blame] | 1450 | /* it was created/moved somewhere, but now it will be created/moved somewhere else, |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1451 | * keep orig_key/orig_value (only replace oper) and replace key/value */ |
| 1452 | assert(lysc_is_userordered(diff_match->schema)); |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1453 | if (lysc_is_dup_inst_list(diff_match->schema)) { |
| 1454 | meta_name = "position"; |
| 1455 | } else if (diff_match->schema->nodetype == LYS_LIST) { |
| 1456 | meta_name = "key"; |
| 1457 | } else { |
| 1458 | meta_name = "value"; |
| 1459 | } |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1460 | |
| 1461 | lyd_diff_del_meta(diff_match, meta_name); |
| 1462 | meta = lyd_find_meta(src_diff->meta, mod, meta_name); |
Michal Vasko | 52afd7d | 2022-01-18 14:08:34 +0100 | [diff] [blame] | 1463 | LY_CHECK_ERR_RET(!meta, LOGERR_META(ctx, meta_name, src_diff), LY_EINVAL); |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1464 | LY_CHECK_RET(lyd_dup_meta_single(meta, diff_match, NULL)); |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1465 | break; |
| 1466 | case LYS_LEAF: |
| 1467 | /* replaced with the exact same value, impossible */ |
Michal Vasko | 8f359bf | 2020-07-28 10:41:15 +0200 | [diff] [blame] | 1468 | if (!lyd_compare_single(diff_match, src_diff, 0)) { |
Michal Vasko | 52afd7d | 2022-01-18 14:08:34 +0100 | [diff] [blame] | 1469 | LOGERR_UNEXPVAL(ctx, diff_match, "target diff"); |
| 1470 | return LY_EINVAL; |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1471 | } |
| 1472 | |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1473 | /* modify the node value */ |
Radek Krejci | 6d5ba0c | 2021-04-26 07:49:59 +0200 | [diff] [blame] | 1474 | if (lyd_change_term(diff_match, lyd_get_value(src_diff))) { |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 1475 | LOGINT_RET(LYD_CTX(src_diff)); |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1476 | } |
| 1477 | |
Michal Vasko | 8caadab | 2020-11-05 17:38:15 +0100 | [diff] [blame] | 1478 | if (cur_op == LYD_DIFF_OP_REPLACE) { |
| 1479 | /* compare values whether there is any change at all */ |
| 1480 | meta = lyd_find_meta(diff_match->meta, mod, "orig-value"); |
Michal Vasko | 52afd7d | 2022-01-18 14:08:34 +0100 | [diff] [blame] | 1481 | LY_CHECK_ERR_RET(!meta, LOGERR_META(ctx, "orig-value", diff_match), LY_EINVAL); |
Radek Krejci | 6d5ba0c | 2021-04-26 07:49:59 +0200 | [diff] [blame] | 1482 | str_val = lyd_get_meta_value(meta); |
Michal Vasko | 8caadab | 2020-11-05 17:38:15 +0100 | [diff] [blame] | 1483 | ret = lyd_value_compare((struct lyd_node_term *)diff_match, str_val, strlen(str_val)); |
| 1484 | if (!ret) { |
| 1485 | /* values are the same, remove orig-value meta and set oper to NONE */ |
| 1486 | lyd_free_meta_single(meta); |
| 1487 | LY_CHECK_RET(lyd_diff_change_op(diff_match, LYD_DIFF_OP_NONE)); |
| 1488 | } |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1489 | } |
| 1490 | |
| 1491 | /* modify the default flag */ |
| 1492 | diff_match->flags &= ~LYD_DEFAULT; |
| 1493 | diff_match->flags |= src_diff->flags & LYD_DEFAULT; |
| 1494 | break; |
| 1495 | case LYS_ANYXML: |
| 1496 | case LYS_ANYDATA: |
Michal Vasko | 8f359bf | 2020-07-28 10:41:15 +0200 | [diff] [blame] | 1497 | if (!lyd_compare_single(diff_match, src_diff, 0)) { |
Michal Vasko | 52afd7d | 2022-01-18 14:08:34 +0100 | [diff] [blame] | 1498 | LOGERR_UNEXPVAL(ctx, diff_match, "target diff"); |
| 1499 | return LY_EINVAL; |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1500 | } |
| 1501 | |
| 1502 | /* modify the node value */ |
| 1503 | any = (struct lyd_node_any *)src_diff; |
| 1504 | LY_CHECK_RET(lyd_any_copy_value(diff_match, &any->value, any->value_type)); |
| 1505 | break; |
| 1506 | default: |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 1507 | LOGINT_RET(LYD_CTX(src_diff)); |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1508 | } |
| 1509 | break; |
| 1510 | case LYD_DIFF_OP_NONE: |
| 1511 | /* it is moved now */ |
| 1512 | assert(lysc_is_userordered(diff_match->schema) && (diff_match->schema->nodetype == LYS_LIST)); |
| 1513 | |
| 1514 | /* change the operation */ |
| 1515 | LY_CHECK_RET(lyd_diff_change_op(diff_match, LYD_DIFF_OP_REPLACE)); |
| 1516 | |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1517 | /* set orig-meta and meta */ |
| 1518 | if (lysc_is_dup_inst_list(diff_match->schema)) { |
| 1519 | meta_name = "position"; |
| 1520 | orig_meta_name = "orig-position"; |
| 1521 | } else { |
| 1522 | meta_name = "key"; |
| 1523 | orig_meta_name = "orig-key"; |
| 1524 | } |
| 1525 | |
| 1526 | meta = lyd_find_meta(src_diff->meta, mod, orig_meta_name); |
Michal Vasko | 52afd7d | 2022-01-18 14:08:34 +0100 | [diff] [blame] | 1527 | LY_CHECK_ERR_RET(!meta, LOGERR_META(ctx, orig_meta_name, src_diff), LY_EINVAL); |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1528 | LY_CHECK_RET(lyd_dup_meta_single(meta, diff_match, NULL)); |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1529 | |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1530 | meta = lyd_find_meta(src_diff->meta, mod, meta_name); |
Michal Vasko | 52afd7d | 2022-01-18 14:08:34 +0100 | [diff] [blame] | 1531 | LY_CHECK_ERR_RET(!meta, LOGERR_META(ctx, meta_name, src_diff), LY_EINVAL); |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1532 | LY_CHECK_RET(lyd_dup_meta_single(meta, diff_match, NULL)); |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1533 | break; |
| 1534 | default: |
| 1535 | /* delete operation is not valid */ |
Michal Vasko | 52afd7d | 2022-01-18 14:08:34 +0100 | [diff] [blame] | 1536 | LOGERR_MERGEOP(ctx, diff_match, cur_op, LYD_DIFF_OP_REPLACE); |
| 1537 | return LY_EINVAL; |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1538 | } |
| 1539 | |
| 1540 | return LY_SUCCESS; |
| 1541 | } |
| 1542 | |
| 1543 | /** |
| 1544 | * @brief Update operations in a diff node when the new operation is CREATE. |
| 1545 | * |
| 1546 | * @param[in] diff_match Node from the diff. |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1547 | * @param[in] cur_op Current operation of @p diff_match. |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1548 | * @param[in] src_diff Current source diff node. |
Michal Vasko | c0e58e8 | 2020-11-11 19:04:33 +0100 | [diff] [blame] | 1549 | * @param[in] options Diff merge options. |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1550 | * @return LY_ERR value. |
| 1551 | */ |
| 1552 | static LY_ERR |
Michal Vasko | c0e58e8 | 2020-11-11 19:04:33 +0100 | [diff] [blame] | 1553 | lyd_diff_merge_create(struct lyd_node *diff_match, enum lyd_diff_op cur_op, const struct lyd_node *src_diff, uint16_t options) |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1554 | { |
| 1555 | struct lyd_node *child; |
Michal Vasko | c0e58e8 | 2020-11-11 19:04:33 +0100 | [diff] [blame] | 1556 | const struct lysc_node_leaf *sleaf = NULL; |
Michal Vasko | 871a025 | 2020-11-11 18:35:24 +0100 | [diff] [blame] | 1557 | uint32_t trg_flags; |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1558 | const char *meta_name, *orig_meta_name; |
| 1559 | struct lyd_meta *meta, *orig_meta; |
Michal Vasko | 52afd7d | 2022-01-18 14:08:34 +0100 | [diff] [blame] | 1560 | const struct ly_ctx *ctx = LYD_CTX(diff_match); |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1561 | |
| 1562 | switch (cur_op) { |
| 1563 | case LYD_DIFF_OP_DELETE: |
Michal Vasko | 871a025 | 2020-11-11 18:35:24 +0100 | [diff] [blame] | 1564 | /* remember current flags */ |
| 1565 | trg_flags = diff_match->flags; |
| 1566 | |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1567 | if (lysc_is_userordered(diff_match->schema)) { |
| 1568 | /* get anchor metadata */ |
| 1569 | if (lysc_is_dup_inst_list(diff_match->schema)) { |
| 1570 | meta_name = "yang:position"; |
| 1571 | orig_meta_name = "yang:orig-position"; |
| 1572 | } else if (diff_match->schema->nodetype == LYS_LIST) { |
| 1573 | meta_name = "yang:key"; |
| 1574 | orig_meta_name = "yang:orig-key"; |
| 1575 | } else { |
| 1576 | meta_name = "yang:value"; |
| 1577 | orig_meta_name = "yang:orig-value"; |
| 1578 | } |
| 1579 | meta = lyd_find_meta(src_diff->meta, NULL, meta_name); |
Michal Vasko | 52afd7d | 2022-01-18 14:08:34 +0100 | [diff] [blame] | 1580 | LY_CHECK_ERR_RET(!meta, LOGERR_META(ctx, meta_name, src_diff), LY_EINVAL); |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1581 | orig_meta = lyd_find_meta(diff_match->meta, NULL, orig_meta_name); |
Michal Vasko | 52afd7d | 2022-01-18 14:08:34 +0100 | [diff] [blame] | 1582 | LY_CHECK_ERR_RET(!orig_meta, LOGERR_META(ctx, orig_meta_name, diff_match), LY_EINVAL); |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1583 | |
| 1584 | /* the (incorrect) assumption made here is that there are no previous diff nodes that would affect |
| 1585 | * the anchors stored in the metadata */ |
Radek Krejci | 6d5ba0c | 2021-04-26 07:49:59 +0200 | [diff] [blame] | 1586 | if (strcmp(lyd_get_meta_value(meta), lyd_get_meta_value(orig_meta))) { |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1587 | /* deleted + created at another position -> operation REPLACE */ |
| 1588 | LY_CHECK_RET(lyd_diff_change_op(diff_match, LYD_DIFF_OP_REPLACE)); |
| 1589 | |
| 1590 | /* add anchor metadata */ |
| 1591 | LY_CHECK_RET(lyd_dup_meta_single(meta, diff_match, NULL)); |
| 1592 | } else { |
| 1593 | /* deleted + created at the same position -> operation NONE */ |
| 1594 | LY_CHECK_RET(lyd_diff_change_op(diff_match, LYD_DIFF_OP_NONE)); |
| 1595 | |
| 1596 | /* delete anchor metadata */ |
| 1597 | lyd_free_meta_single(orig_meta); |
| 1598 | } |
| 1599 | } else if (diff_match->schema->nodetype == LYS_LEAF) { |
| 1600 | if (options & LYD_DIFF_MERGE_DEFAULTS) { |
| 1601 | /* we are dealing with a leaf and are handling default values specially (as explicit nodes) */ |
| 1602 | sleaf = (struct lysc_node_leaf *)diff_match->schema; |
| 1603 | } |
| 1604 | |
Radek Krejci | 55c4bd2 | 2021-04-26 08:09:04 +0200 | [diff] [blame] | 1605 | if (sleaf && sleaf->dflt && !sleaf->dflt->realtype->plugin->compare(sleaf->dflt, |
| 1606 | &((struct lyd_node_term *)src_diff)->value)) { |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1607 | /* we deleted it, so a default value was in-use, and it matches the created value -> operation NONE */ |
| 1608 | LY_CHECK_RET(lyd_diff_change_op(diff_match, LYD_DIFF_OP_NONE)); |
| 1609 | } else if (!lyd_compare_single(diff_match, src_diff, 0)) { |
| 1610 | /* deleted + created -> operation NONE */ |
| 1611 | LY_CHECK_RET(lyd_diff_change_op(diff_match, LYD_DIFF_OP_NONE)); |
| 1612 | } else { |
| 1613 | /* we deleted it, but it was created with a different value -> operation REPLACE */ |
| 1614 | LY_CHECK_RET(lyd_diff_change_op(diff_match, LYD_DIFF_OP_REPLACE)); |
| 1615 | |
| 1616 | /* current value is the previous one (meta) */ |
| 1617 | LY_CHECK_RET(lyd_new_meta(LYD_CTX(src_diff), diff_match, NULL, "yang:orig-value", |
Radek Krejci | 6d5ba0c | 2021-04-26 07:49:59 +0200 | [diff] [blame] | 1618 | lyd_get_value(diff_match), 0, NULL)); |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1619 | |
| 1620 | /* update the value itself */ |
Radek Krejci | 6d5ba0c | 2021-04-26 07:49:59 +0200 | [diff] [blame] | 1621 | LY_CHECK_RET(lyd_change_term(diff_match, lyd_get_value(src_diff))); |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1622 | } |
| 1623 | } else { |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1624 | /* deleted + created -> operation NONE */ |
| 1625 | LY_CHECK_RET(lyd_diff_change_op(diff_match, LYD_DIFF_OP_NONE)); |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1626 | } |
| 1627 | |
| 1628 | if (diff_match->schema->nodetype & LYD_NODE_TERM) { |
Michal Vasko | 4b715ca | 2020-11-11 18:39:57 +0100 | [diff] [blame] | 1629 | /* add orig-dflt metadata */ |
| 1630 | LY_CHECK_RET(lyd_new_meta(LYD_CTX(src_diff), diff_match, NULL, "yang:orig-default", |
| 1631 | trg_flags & LYD_DEFAULT ? "true" : "false", 0, NULL)); |
| 1632 | |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1633 | /* update dflt flag itself */ |
| 1634 | diff_match->flags &= ~LYD_DEFAULT; |
| 1635 | diff_match->flags |= src_diff->flags & LYD_DEFAULT; |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1636 | } |
| 1637 | |
| 1638 | /* but the operation of its children should remain DELETE */ |
| 1639 | LY_LIST_FOR(lyd_child_no_keys(diff_match), child) { |
| 1640 | LY_CHECK_RET(lyd_diff_change_op(child, LYD_DIFF_OP_DELETE)); |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1641 | } |
| 1642 | break; |
| 1643 | default: |
| 1644 | /* create and replace operations are not valid */ |
Michal Vasko | 52afd7d | 2022-01-18 14:08:34 +0100 | [diff] [blame] | 1645 | LOGERR_MERGEOP(LYD_CTX(src_diff), diff_match, cur_op, LYD_DIFF_OP_CREATE); |
| 1646 | return LY_EINVAL; |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1647 | } |
| 1648 | |
| 1649 | return LY_SUCCESS; |
| 1650 | } |
| 1651 | |
| 1652 | /** |
| 1653 | * @brief Update operations on a diff node when the new operation is DELETE. |
| 1654 | * |
| 1655 | * @param[in] diff_match Node from the diff. |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1656 | * @param[in] cur_op Current operation of @p diff_match. |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1657 | * @param[in] src_diff Current source diff node. |
| 1658 | * @return LY_ERR value. |
| 1659 | */ |
| 1660 | static LY_ERR |
| 1661 | lyd_diff_merge_delete(struct lyd_node *diff_match, enum lyd_diff_op cur_op, const struct lyd_node *src_diff) |
| 1662 | { |
Michal Vasko | 17d0c5c | 2021-11-01 11:31:11 +0100 | [diff] [blame] | 1663 | struct lyd_node *child; |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1664 | struct lyd_meta *meta; |
| 1665 | const char *meta_name; |
Michal Vasko | 52afd7d | 2022-01-18 14:08:34 +0100 | [diff] [blame] | 1666 | const struct ly_ctx *ctx = LYD_CTX(diff_match); |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1667 | |
| 1668 | /* we can delete only exact existing nodes */ |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 1669 | LY_CHECK_ERR_RET(lyd_compare_single(diff_match, src_diff, 0), LOGINT(LYD_CTX(src_diff)), LY_EINT); |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1670 | |
| 1671 | switch (cur_op) { |
| 1672 | case LYD_DIFF_OP_CREATE: |
| 1673 | /* it was created, but then deleted -> set NONE operation */ |
| 1674 | LY_CHECK_RET(lyd_diff_change_op(diff_match, LYD_DIFF_OP_NONE)); |
| 1675 | |
| 1676 | if (diff_match->schema->nodetype & LYD_NODE_TERM) { |
| 1677 | /* add orig-default meta because it is expected */ |
Michal Vasko | 871a025 | 2020-11-11 18:35:24 +0100 | [diff] [blame] | 1678 | LY_CHECK_RET(lyd_new_meta(LYD_CTX(src_diff), diff_match, NULL, "yang:orig-default", |
| 1679 | diff_match->flags & LYD_DEFAULT ? "true" : "false", 0, NULL)); |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1680 | } else if (!lysc_is_dup_inst_list(diff_match->schema)) { |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1681 | /* keep operation for all descendants (for now) */ |
Radek Krejci | a1c1e54 | 2020-09-29 16:06:52 +0200 | [diff] [blame] | 1682 | LY_LIST_FOR(lyd_child_no_keys(diff_match), child) { |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1683 | LY_CHECK_RET(lyd_diff_change_op(child, cur_op)); |
| 1684 | } |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1685 | } /* else key-less list, for which all the descendants act as keys */ |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1686 | break; |
| 1687 | case LYD_DIFF_OP_REPLACE: |
Michal Vasko | 17d0c5c | 2021-11-01 11:31:11 +0100 | [diff] [blame] | 1688 | /* remove the redundant metadata */ |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1689 | if (lysc_is_userordered(diff_match->schema)) { |
| 1690 | if (lysc_is_dup_inst_list(diff_match->schema)) { |
| 1691 | meta_name = "position"; |
| 1692 | } else if (diff_match->schema->nodetype == LYS_LIST) { |
| 1693 | meta_name = "key"; |
| 1694 | } else { |
| 1695 | meta_name = "value"; |
| 1696 | } |
| 1697 | } else { |
| 1698 | assert(diff_match->schema->nodetype == LYS_LEAF); |
| 1699 | |
| 1700 | /* switch value for the original one */ |
| 1701 | meta = lyd_find_meta(diff_match->meta, NULL, "yang:orig-value"); |
Michal Vasko | 52afd7d | 2022-01-18 14:08:34 +0100 | [diff] [blame] | 1702 | LY_CHECK_ERR_RET(!meta, LOGERR_META(ctx, "yang:orig-value", diff_match), LY_EINVAL); |
Radek Krejci | 6d5ba0c | 2021-04-26 07:49:59 +0200 | [diff] [blame] | 1703 | if (lyd_change_term(diff_match, lyd_get_meta_value(meta))) { |
Michal Vasko | 52afd7d | 2022-01-18 14:08:34 +0100 | [diff] [blame] | 1704 | LOGERR_UNEXPVAL(ctx, diff_match, "target diff"); |
| 1705 | return LY_EINVAL; |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1706 | } |
| 1707 | |
| 1708 | /* switch default for the original one, then remove the meta */ |
| 1709 | meta = lyd_find_meta(diff_match->meta, NULL, "yang:orig-default"); |
Michal Vasko | 52afd7d | 2022-01-18 14:08:34 +0100 | [diff] [blame] | 1710 | LY_CHECK_ERR_RET(!meta, LOGERR_META(ctx, "yang:orig-default", diff_match), LY_EINVAL); |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1711 | diff_match->flags &= ~LYD_DEFAULT; |
| 1712 | if (meta->value.boolean) { |
| 1713 | diff_match->flags |= LYD_DEFAULT; |
| 1714 | } |
| 1715 | lyd_free_meta_single(meta); |
| 1716 | |
| 1717 | meta_name = "orig-value"; |
| 1718 | } |
| 1719 | lyd_diff_del_meta(diff_match, meta_name); |
| 1720 | |
Michal Vasko | 17d0c5c | 2021-11-01 11:31:11 +0100 | [diff] [blame] | 1721 | /* it was being changed, but should be deleted instead -> set DELETE operation */ |
| 1722 | LY_CHECK_RET(lyd_diff_change_op(diff_match, LYD_DIFF_OP_DELETE)); |
| 1723 | break; |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1724 | case LYD_DIFF_OP_NONE: |
| 1725 | /* it was not modified, but should be deleted -> set DELETE operation */ |
| 1726 | LY_CHECK_RET(lyd_diff_change_op(diff_match, LYD_DIFF_OP_DELETE)); |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1727 | break; |
| 1728 | default: |
| 1729 | /* delete operation is not valid */ |
Michal Vasko | 52afd7d | 2022-01-18 14:08:34 +0100 | [diff] [blame] | 1730 | LOGERR_MERGEOP(LYD_CTX(diff_match), diff_match, cur_op, LYD_DIFF_OP_DELETE); |
| 1731 | return LY_EINVAL; |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1732 | } |
| 1733 | |
| 1734 | return LY_SUCCESS; |
| 1735 | } |
| 1736 | |
| 1737 | /** |
| 1738 | * @brief Check whether this diff node is redundant (does not change data). |
| 1739 | * |
| 1740 | * @param[in] diff Diff node. |
| 1741 | * @return 0 if not, non-zero if it is. |
| 1742 | */ |
| 1743 | static int |
| 1744 | lyd_diff_is_redundant(struct lyd_node *diff) |
| 1745 | { |
| 1746 | enum lyd_diff_op op; |
| 1747 | struct lyd_meta *meta, *orig_val_meta = NULL, *val_meta = NULL; |
| 1748 | struct lyd_node *child; |
| 1749 | const struct lys_module *mod; |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1750 | const char *str, *orig_meta_name, *meta_name; |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1751 | |
| 1752 | assert(diff); |
| 1753 | |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1754 | if (lysc_is_dup_inst_list(diff->schema)) { |
| 1755 | /* all descendants are keys */ |
| 1756 | child = NULL; |
| 1757 | } else { |
| 1758 | child = lyd_child_no_keys(diff); |
| 1759 | } |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 1760 | mod = ly_ctx_get_module_latest(LYD_CTX(diff), "yang"); |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1761 | assert(mod); |
| 1762 | |
| 1763 | /* get node operation */ |
Michal Vasko | 53bf6f2 | 2020-07-14 08:23:40 +0200 | [diff] [blame] | 1764 | LY_CHECK_RET(lyd_diff_get_op(diff, &op), 0); |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1765 | |
| 1766 | if ((op == LYD_DIFF_OP_REPLACE) && lysc_is_userordered(diff->schema)) { |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1767 | /* get metadata names */ |
| 1768 | if (lysc_is_dup_inst_list(diff->schema)) { |
| 1769 | meta_name = "position"; |
| 1770 | orig_meta_name = "orig-position"; |
| 1771 | } else if (diff->schema->nodetype == LYS_LIST) { |
| 1772 | meta_name = "key"; |
| 1773 | orig_meta_name = "orig-key"; |
| 1774 | } else { |
| 1775 | meta_name = "value"; |
| 1776 | orig_meta_name = "orig-value"; |
| 1777 | } |
| 1778 | |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1779 | /* check for redundant move */ |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1780 | orig_val_meta = lyd_find_meta(diff->meta, mod, orig_meta_name); |
| 1781 | val_meta = lyd_find_meta(diff->meta, mod, meta_name); |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1782 | assert(orig_val_meta && val_meta); |
| 1783 | |
| 1784 | if (!lyd_compare_meta(orig_val_meta, val_meta)) { |
| 1785 | /* there is actually no move */ |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1786 | lyd_free_meta_single(orig_val_meta); |
| 1787 | lyd_free_meta_single(val_meta); |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1788 | if (child) { |
| 1789 | /* change operation to NONE, we have siblings */ |
| 1790 | lyd_diff_change_op(diff, LYD_DIFF_OP_NONE); |
| 1791 | return 0; |
| 1792 | } |
| 1793 | |
| 1794 | /* redundant node, BUT !! |
| 1795 | * In diff the move operation is always converted to be INSERT_AFTER, which is fine |
| 1796 | * because the data that this is applied on should not change for the diff lifetime. |
| 1797 | * However, when we are merging 2 diffs, this conversion is actually lossy because |
| 1798 | * if the data change, the move operation can also change its meaning. In this specific |
| 1799 | * case the move operation will be lost. But it can be considered a feature, it is not supported. |
| 1800 | */ |
| 1801 | return 1; |
| 1802 | } |
Michal Vasko | db91fc3 | 2023-05-02 14:39:40 +0200 | [diff] [blame] | 1803 | } else if (op == LYD_DIFF_OP_NONE) { |
| 1804 | if (!diff->schema) { |
| 1805 | /* opaque node with none must be redundant */ |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1806 | return 1; |
| 1807 | } |
Michal Vasko | db91fc3 | 2023-05-02 14:39:40 +0200 | [diff] [blame] | 1808 | |
| 1809 | if (diff->schema->nodetype & LYD_NODE_TERM) { |
| 1810 | /* check whether at least the default flags are different */ |
| 1811 | meta = lyd_find_meta(diff->meta, mod, "orig-default"); |
| 1812 | assert(meta); |
| 1813 | str = lyd_get_meta_value(meta); |
| 1814 | |
| 1815 | /* if previous and current dflt flags are the same, this node is redundant */ |
| 1816 | if ((!strcmp(str, "true") && (diff->flags & LYD_DEFAULT)) || (!strcmp(str, "false") && !(diff->flags & LYD_DEFAULT))) { |
| 1817 | return 1; |
| 1818 | } |
| 1819 | return 0; |
| 1820 | } |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1821 | } |
| 1822 | |
| 1823 | if (!child && (op == LYD_DIFF_OP_NONE)) { |
| 1824 | return 1; |
| 1825 | } |
| 1826 | |
| 1827 | return 0; |
| 1828 | } |
| 1829 | |
| 1830 | /** |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1831 | * @brief Merge sysrepo diff subtree with another diff, recursively. |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1832 | * |
| 1833 | * @param[in] src_diff Source diff node. |
| 1834 | * @param[in] diff_parent Current sysrepo diff parent. |
| 1835 | * @param[in] diff_cb Optional diff callback. |
| 1836 | * @param[in] cb_data User data for @p diff_cb. |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1837 | * @param[in,out] dup_inst Duplicate instance cache for all @p src_diff siblings. |
Michal Vasko | c0e58e8 | 2020-11-11 19:04:33 +0100 | [diff] [blame] | 1838 | * @param[in] options Diff merge options. |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1839 | * @param[in,out] diff Diff root node. |
| 1840 | * @return LY_ERR value. |
| 1841 | */ |
| 1842 | static LY_ERR |
| 1843 | lyd_diff_merge_r(const struct lyd_node *src_diff, struct lyd_node *diff_parent, lyd_diff_cb diff_cb, void *cb_data, |
Michal Vasko | 8efac24 | 2023-03-30 08:24:56 +0200 | [diff] [blame] | 1844 | struct ly_ht **dup_inst, uint16_t options, struct lyd_node **diff) |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1845 | { |
| 1846 | LY_ERR ret = LY_SUCCESS; |
| 1847 | struct lyd_node *child, *diff_node = NULL; |
| 1848 | enum lyd_diff_op src_op, cur_op; |
Michal Vasko | 8efac24 | 2023-03-30 08:24:56 +0200 | [diff] [blame] | 1849 | struct ly_ht *child_dup_inst = NULL; |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1850 | |
| 1851 | /* get source node operation */ |
| 1852 | LY_CHECK_RET(lyd_diff_get_op(src_diff, &src_op)); |
| 1853 | |
| 1854 | /* find an equal node in the current diff */ |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1855 | LY_CHECK_RET(lyd_diff_find_match(diff_parent ? lyd_child_no_keys(diff_parent) : *diff, src_diff, 1, dup_inst, &diff_node)); |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1856 | |
| 1857 | if (diff_node) { |
| 1858 | /* get target (current) operation */ |
| 1859 | LY_CHECK_RET(lyd_diff_get_op(diff_node, &cur_op)); |
| 1860 | |
| 1861 | /* merge operations */ |
| 1862 | switch (src_op) { |
| 1863 | case LYD_DIFF_OP_REPLACE: |
| 1864 | ret = lyd_diff_merge_replace(diff_node, cur_op, src_diff); |
| 1865 | break; |
| 1866 | case LYD_DIFF_OP_CREATE: |
Michal Vasko | 1dc0a84 | 2021-02-04 11:04:57 +0100 | [diff] [blame] | 1867 | if ((cur_op == LYD_DIFF_OP_CREATE) && lysc_is_dup_inst_list(diff_node->schema)) { |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1868 | /* special case of creating duplicate (leaf-)list instances */ |
Michal Vasko | 1dc0a84 | 2021-02-04 11:04:57 +0100 | [diff] [blame] | 1869 | goto add_diff; |
| 1870 | } |
| 1871 | |
Michal Vasko | c0e58e8 | 2020-11-11 19:04:33 +0100 | [diff] [blame] | 1872 | ret = lyd_diff_merge_create(diff_node, cur_op, src_diff, options); |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1873 | break; |
| 1874 | case LYD_DIFF_OP_DELETE: |
| 1875 | ret = lyd_diff_merge_delete(diff_node, cur_op, src_diff); |
| 1876 | break; |
| 1877 | case LYD_DIFF_OP_NONE: |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1878 | /* key-less list can never have "none" operation since all its descendants are acting as "keys" */ |
| 1879 | assert((src_diff->schema->nodetype != LYS_LIST) || !lysc_is_dup_inst_list(src_diff->schema)); |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1880 | ret = lyd_diff_merge_none(diff_node, cur_op, src_diff); |
| 1881 | break; |
| 1882 | default: |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 1883 | LOGINT_RET(LYD_CTX(src_diff)); |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1884 | } |
| 1885 | if (ret) { |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 1886 | LOGERR(LYD_CTX(src_diff), LY_EOTHER, "Merging operation \"%s\" failed.", lyd_diff_op2str(src_op)); |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1887 | return ret; |
| 1888 | } |
| 1889 | |
| 1890 | if (diff_cb) { |
| 1891 | /* call callback */ |
Michal Vasko | bc5fba9 | 2020-08-07 12:14:39 +0200 | [diff] [blame] | 1892 | LY_CHECK_RET(diff_cb(src_diff, diff_node, cb_data)); |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1893 | } |
| 1894 | |
| 1895 | /* update diff parent */ |
| 1896 | diff_parent = diff_node; |
| 1897 | |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1898 | /* for diff purposes, all key-less list descendants actually act as keys (identifying the same instances), |
| 1899 | * so there is nothing to merge for these "keys" */ |
| 1900 | if (!lysc_is_dup_inst_list(src_diff->schema)) { |
| 1901 | /* merge src_diff recursively */ |
| 1902 | LY_LIST_FOR(lyd_child_no_keys(src_diff), child) { |
| 1903 | ret = lyd_diff_merge_r(child, diff_parent, diff_cb, cb_data, &child_dup_inst, options, diff); |
| 1904 | if (ret) { |
| 1905 | break; |
| 1906 | } |
| 1907 | } |
Michal Vasko | d7c048c | 2021-05-18 16:12:55 +0200 | [diff] [blame] | 1908 | lyd_dup_inst_free(child_dup_inst); |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1909 | LY_CHECK_RET(ret); |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1910 | } |
| 1911 | } else { |
Michal Vasko | 1dc0a84 | 2021-02-04 11:04:57 +0100 | [diff] [blame] | 1912 | add_diff: |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1913 | /* add new diff node with all descendants */ |
Michal Vasko | 9ad7685 | 2022-07-12 10:18:03 +0200 | [diff] [blame] | 1914 | if ((src_diff->flags & LYD_EXT) && diff_parent) { |
| 1915 | LY_CHECK_RET(lyd_dup_single_to_ctx(src_diff, LYD_CTX(diff_parent), (struct lyd_node_inner *)diff_parent, |
| 1916 | LYD_DUP_RECURSIVE | LYD_DUP_WITH_FLAGS, &diff_node)); |
| 1917 | } else { |
| 1918 | LY_CHECK_RET(lyd_dup_single(src_diff, (struct lyd_node_inner *)diff_parent, |
| 1919 | LYD_DUP_RECURSIVE | LYD_DUP_WITH_FLAGS, &diff_node)); |
| 1920 | } |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1921 | |
| 1922 | /* insert node into diff if not already */ |
| 1923 | if (!diff_parent) { |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 1924 | lyd_insert_sibling(*diff, diff_node, diff); |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1925 | } |
| 1926 | |
| 1927 | /* update operation */ |
| 1928 | LY_CHECK_RET(lyd_diff_change_op(diff_node, src_op)); |
| 1929 | |
| 1930 | if (diff_cb) { |
Michal Vasko | e2af841 | 2020-12-03 14:11:38 +0100 | [diff] [blame] | 1931 | /* call callback with no source diff node since it was duplicated and just added */ |
| 1932 | LY_CHECK_RET(diff_cb(NULL, diff_node, cb_data)); |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1933 | } |
| 1934 | |
| 1935 | /* update diff parent */ |
| 1936 | diff_parent = diff_node; |
| 1937 | } |
| 1938 | |
| 1939 | /* remove any redundant nodes */ |
Michal Vasko | b98d708 | 2020-07-15 16:38:36 +0200 | [diff] [blame] | 1940 | if (lyd_diff_is_redundant(diff_parent)) { |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1941 | if (diff_parent == *diff) { |
| 1942 | *diff = (*diff)->next; |
| 1943 | } |
| 1944 | lyd_free_tree(diff_parent); |
| 1945 | } |
| 1946 | |
| 1947 | return LY_SUCCESS; |
| 1948 | } |
| 1949 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 1950 | LIBYANG_API_DEF LY_ERR |
Michal Vasko | fb737aa | 2020-08-06 13:53:53 +0200 | [diff] [blame] | 1951 | lyd_diff_merge_module(struct lyd_node **diff, const struct lyd_node *src_diff, const struct lys_module *mod, |
Michal Vasko | c0e58e8 | 2020-11-11 19:04:33 +0100 | [diff] [blame] | 1952 | lyd_diff_cb diff_cb, void *cb_data, uint16_t options) |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1953 | { |
| 1954 | const struct lyd_node *src_root; |
Michal Vasko | 8efac24 | 2023-03-30 08:24:56 +0200 | [diff] [blame] | 1955 | struct ly_ht *dup_inst = NULL; |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1956 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1957 | |
| 1958 | LY_LIST_FOR(src_diff, src_root) { |
| 1959 | if (mod && (lyd_owner_module(src_root) != mod)) { |
| 1960 | /* skip data nodes from different modules */ |
| 1961 | continue; |
| 1962 | } |
| 1963 | |
| 1964 | /* apply relevant nodes from the diff datatree */ |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1965 | LY_CHECK_GOTO(ret = lyd_diff_merge_r(src_root, NULL, diff_cb, cb_data, &dup_inst, options, diff), cleanup); |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1966 | } |
| 1967 | |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1968 | cleanup: |
Michal Vasko | d7c048c | 2021-05-18 16:12:55 +0200 | [diff] [blame] | 1969 | lyd_dup_inst_free(dup_inst); |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1970 | return ret; |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1971 | } |
| 1972 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 1973 | LIBYANG_API_DEF LY_ERR |
Michal Vasko | 04f8591 | 2020-08-07 12:14:58 +0200 | [diff] [blame] | 1974 | lyd_diff_merge_tree(struct lyd_node **diff_first, struct lyd_node *diff_parent, const struct lyd_node *src_sibling, |
Michal Vasko | c0e58e8 | 2020-11-11 19:04:33 +0100 | [diff] [blame] | 1975 | lyd_diff_cb diff_cb, void *cb_data, uint16_t options) |
Michal Vasko | 04f8591 | 2020-08-07 12:14:58 +0200 | [diff] [blame] | 1976 | { |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1977 | LY_ERR ret; |
Michal Vasko | 8efac24 | 2023-03-30 08:24:56 +0200 | [diff] [blame] | 1978 | struct ly_ht *dup_inst = NULL; |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1979 | |
Michal Vasko | 04f8591 | 2020-08-07 12:14:58 +0200 | [diff] [blame] | 1980 | if (!src_sibling) { |
| 1981 | return LY_SUCCESS; |
| 1982 | } |
| 1983 | |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1984 | ret = lyd_diff_merge_r(src_sibling, diff_parent, diff_cb, cb_data, &dup_inst, options, diff_first); |
Michal Vasko | d7c048c | 2021-05-18 16:12:55 +0200 | [diff] [blame] | 1985 | lyd_dup_inst_free(dup_inst); |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 1986 | return ret; |
Michal Vasko | 04f8591 | 2020-08-07 12:14:58 +0200 | [diff] [blame] | 1987 | } |
| 1988 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 1989 | LIBYANG_API_DEF LY_ERR |
Michal Vasko | c0e58e8 | 2020-11-11 19:04:33 +0100 | [diff] [blame] | 1990 | lyd_diff_merge_all(struct lyd_node **diff, const struct lyd_node *src_diff, uint16_t options) |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1991 | { |
Michal Vasko | c0e58e8 | 2020-11-11 19:04:33 +0100 | [diff] [blame] | 1992 | return lyd_diff_merge_module(diff, src_diff, NULL, NULL, NULL, options); |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1993 | } |
Michal Vasko | 4231fb6 | 2020-07-13 13:54:47 +0200 | [diff] [blame] | 1994 | |
| 1995 | static LY_ERR |
Michal Vasko | baba84e | 2021-02-05 16:33:30 +0100 | [diff] [blame] | 1996 | lyd_diff_reverse_value(struct lyd_node *node, const struct lys_module *mod) |
Michal Vasko | 4231fb6 | 2020-07-13 13:54:47 +0200 | [diff] [blame] | 1997 | { |
| 1998 | LY_ERR ret = LY_SUCCESS; |
| 1999 | struct lyd_meta *meta; |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 2000 | const char *val1 = NULL; |
| 2001 | char *val2; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 2002 | uint32_t flags; |
Michal Vasko | 4231fb6 | 2020-07-13 13:54:47 +0200 | [diff] [blame] | 2003 | |
Michal Vasko | baba84e | 2021-02-05 16:33:30 +0100 | [diff] [blame] | 2004 | assert(node->schema->nodetype & (LYS_LEAF | LYS_ANYDATA)); |
| 2005 | |
| 2006 | meta = lyd_find_meta(node->meta, mod, "orig-value"); |
Michal Vasko | 52afd7d | 2022-01-18 14:08:34 +0100 | [diff] [blame] | 2007 | LY_CHECK_ERR_RET(!meta, LOGERR_META(LYD_CTX(node), "orig-value", node), LY_EINVAL); |
Michal Vasko | 4231fb6 | 2020-07-13 13:54:47 +0200 | [diff] [blame] | 2008 | |
| 2009 | /* orig-value */ |
Radek Krejci | 6d5ba0c | 2021-04-26 07:49:59 +0200 | [diff] [blame] | 2010 | val1 = lyd_get_meta_value(meta); |
Michal Vasko | 4231fb6 | 2020-07-13 13:54:47 +0200 | [diff] [blame] | 2011 | |
| 2012 | /* current value */ |
Michal Vasko | baba84e | 2021-02-05 16:33:30 +0100 | [diff] [blame] | 2013 | if (node->schema->nodetype == LYS_LEAF) { |
Radek Krejci | 6d5ba0c | 2021-04-26 07:49:59 +0200 | [diff] [blame] | 2014 | val2 = strdup(lyd_get_value(node)); |
Michal Vasko | baba84e | 2021-02-05 16:33:30 +0100 | [diff] [blame] | 2015 | } else { |
| 2016 | LY_CHECK_RET(lyd_any_value_str(node, &val2)); |
| 2017 | } |
Michal Vasko | 4231fb6 | 2020-07-13 13:54:47 +0200 | [diff] [blame] | 2018 | |
| 2019 | /* switch values, keep default flag */ |
Michal Vasko | baba84e | 2021-02-05 16:33:30 +0100 | [diff] [blame] | 2020 | flags = node->flags; |
| 2021 | if (node->schema->nodetype == LYS_LEAF) { |
| 2022 | LY_CHECK_GOTO(ret = lyd_change_term(node, val1), cleanup); |
| 2023 | } else { |
| 2024 | union lyd_any_value anyval = {.str = val1}; |
Michal Vasko | 26bbb27 | 2022-08-02 14:54:33 +0200 | [diff] [blame] | 2025 | |
Michal Vasko | baba84e | 2021-02-05 16:33:30 +0100 | [diff] [blame] | 2026 | LY_CHECK_GOTO(ret = lyd_any_copy_value(node, &anyval, LYD_ANYDATA_STRING), cleanup); |
| 2027 | } |
| 2028 | node->flags = flags; |
Michal Vasko | 4231fb6 | 2020-07-13 13:54:47 +0200 | [diff] [blame] | 2029 | LY_CHECK_GOTO(ret = lyd_change_meta(meta, val2), cleanup); |
| 2030 | |
| 2031 | cleanup: |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 2032 | free(val2); |
Michal Vasko | 4231fb6 | 2020-07-13 13:54:47 +0200 | [diff] [blame] | 2033 | return ret; |
| 2034 | } |
| 2035 | |
| 2036 | static LY_ERR |
| 2037 | lyd_diff_reverse_default(struct lyd_node *node, const struct lys_module *mod) |
| 2038 | { |
| 2039 | struct lyd_meta *meta; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 2040 | uint32_t flag1, flag2; |
Michal Vasko | 4231fb6 | 2020-07-13 13:54:47 +0200 | [diff] [blame] | 2041 | |
| 2042 | meta = lyd_find_meta(node->meta, mod, "orig-default"); |
Michal Vasko | 610e93b | 2020-11-09 20:58:32 +0100 | [diff] [blame] | 2043 | LY_CHECK_ERR_RET(!meta, LOGINT(mod->ctx), LY_EINT); |
Michal Vasko | 4231fb6 | 2020-07-13 13:54:47 +0200 | [diff] [blame] | 2044 | |
| 2045 | /* orig-default */ |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 2046 | if (meta->value.boolean) { |
Michal Vasko | 4231fb6 | 2020-07-13 13:54:47 +0200 | [diff] [blame] | 2047 | flag1 = LYD_DEFAULT; |
| 2048 | } else { |
| 2049 | flag1 = 0; |
| 2050 | } |
| 2051 | |
| 2052 | /* current default */ |
| 2053 | flag2 = node->flags & LYD_DEFAULT; |
| 2054 | |
Michal Vasko | 610e93b | 2020-11-09 20:58:32 +0100 | [diff] [blame] | 2055 | if (flag1 == flag2) { |
| 2056 | /* no default state change so nothing to reverse */ |
| 2057 | return LY_SUCCESS; |
| 2058 | } |
| 2059 | |
Michal Vasko | 4231fb6 | 2020-07-13 13:54:47 +0200 | [diff] [blame] | 2060 | /* switch defaults */ |
| 2061 | node->flags &= ~LYD_DEFAULT; |
| 2062 | node->flags |= flag1; |
| 2063 | LY_CHECK_RET(lyd_change_meta(meta, flag2 ? "true" : "false")); |
| 2064 | |
| 2065 | return LY_SUCCESS; |
| 2066 | } |
| 2067 | |
| 2068 | static LY_ERR |
| 2069 | lyd_diff_reverse_meta(struct lyd_node *node, const struct lys_module *mod, const char *name1, const char *name2) |
| 2070 | { |
| 2071 | LY_ERR ret = LY_SUCCESS; |
| 2072 | struct lyd_meta *meta1, *meta2; |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 2073 | const char *val1 = NULL; |
| 2074 | char *val2 = NULL; |
Michal Vasko | 4231fb6 | 2020-07-13 13:54:47 +0200 | [diff] [blame] | 2075 | |
| 2076 | meta1 = lyd_find_meta(node->meta, mod, name1); |
Michal Vasko | 52afd7d | 2022-01-18 14:08:34 +0100 | [diff] [blame] | 2077 | LY_CHECK_ERR_RET(!meta1, LOGERR_META(LYD_CTX(node), name1, node), LY_EINVAL); |
Michal Vasko | 4231fb6 | 2020-07-13 13:54:47 +0200 | [diff] [blame] | 2078 | |
| 2079 | meta2 = lyd_find_meta(node->meta, mod, name2); |
Michal Vasko | 52afd7d | 2022-01-18 14:08:34 +0100 | [diff] [blame] | 2080 | LY_CHECK_ERR_RET(!meta2, LOGERR_META(LYD_CTX(node), name2, node), LY_EINVAL); |
Michal Vasko | 4231fb6 | 2020-07-13 13:54:47 +0200 | [diff] [blame] | 2081 | |
| 2082 | /* value1 */ |
Radek Krejci | 6d5ba0c | 2021-04-26 07:49:59 +0200 | [diff] [blame] | 2083 | val1 = lyd_get_meta_value(meta1); |
Michal Vasko | 4231fb6 | 2020-07-13 13:54:47 +0200 | [diff] [blame] | 2084 | |
| 2085 | /* value2 */ |
Radek Krejci | 6d5ba0c | 2021-04-26 07:49:59 +0200 | [diff] [blame] | 2086 | val2 = strdup(lyd_get_meta_value(meta2)); |
Michal Vasko | 4231fb6 | 2020-07-13 13:54:47 +0200 | [diff] [blame] | 2087 | |
| 2088 | /* switch values */ |
| 2089 | LY_CHECK_GOTO(ret = lyd_change_meta(meta1, val2), cleanup); |
| 2090 | LY_CHECK_GOTO(ret = lyd_change_meta(meta2, val1), cleanup); |
| 2091 | |
| 2092 | cleanup: |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 2093 | free(val2); |
Michal Vasko | 4231fb6 | 2020-07-13 13:54:47 +0200 | [diff] [blame] | 2094 | return ret; |
| 2095 | } |
| 2096 | |
Michal Vasko | 9a7e9d0 | 2021-03-09 13:52:25 +0100 | [diff] [blame] | 2097 | /** |
| 2098 | * @brief Remove specific operation from all the nodes in a subtree. |
| 2099 | * |
| 2100 | * @param[in] diff Diff subtree to process. |
| 2101 | * @param[in] op Only expected operation. |
| 2102 | * @return LY_ERR value. |
| 2103 | */ |
| 2104 | static LY_ERR |
| 2105 | lyd_diff_reverse_remove_op_r(struct lyd_node *diff, enum lyd_diff_op op) |
| 2106 | { |
| 2107 | struct lyd_node *elem; |
| 2108 | struct lyd_meta *meta; |
| 2109 | |
| 2110 | LYD_TREE_DFS_BEGIN(diff, elem) { |
| 2111 | meta = lyd_find_meta(elem->meta, NULL, "yang:operation"); |
| 2112 | if (meta) { |
Radek Krejci | 6d5ba0c | 2021-04-26 07:49:59 +0200 | [diff] [blame] | 2113 | LY_CHECK_ERR_RET(lyd_diff_str2op(lyd_get_meta_value(meta)) != op, LOGINT(LYD_CTX(diff)), LY_EINT); |
Michal Vasko | 9a7e9d0 | 2021-03-09 13:52:25 +0100 | [diff] [blame] | 2114 | lyd_free_meta_single(meta); |
| 2115 | } |
| 2116 | |
| 2117 | LYD_TREE_DFS_END(diff, elem); |
| 2118 | } |
| 2119 | |
| 2120 | return LY_SUCCESS; |
| 2121 | } |
| 2122 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 2123 | LIBYANG_API_DEF LY_ERR |
Michal Vasko | 6653581 | 2020-08-11 08:44:22 +0200 | [diff] [blame] | 2124 | lyd_diff_reverse_all(const struct lyd_node *src_diff, struct lyd_node **diff) |
Michal Vasko | 4231fb6 | 2020-07-13 13:54:47 +0200 | [diff] [blame] | 2125 | { |
| 2126 | LY_ERR ret = LY_SUCCESS; |
| 2127 | const struct lys_module *mod; |
Michal Vasko | 9a7e9d0 | 2021-03-09 13:52:25 +0100 | [diff] [blame] | 2128 | struct lyd_node *root, *elem, *iter; |
Michal Vasko | 4231fb6 | 2020-07-13 13:54:47 +0200 | [diff] [blame] | 2129 | enum lyd_diff_op op; |
| 2130 | |
| 2131 | LY_CHECK_ARG_RET(NULL, diff, LY_EINVAL); |
| 2132 | |
| 2133 | if (!src_diff) { |
| 2134 | *diff = NULL; |
| 2135 | return LY_SUCCESS; |
| 2136 | } |
| 2137 | |
| 2138 | /* duplicate diff */ |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 2139 | LY_CHECK_RET(lyd_dup_siblings(src_diff, NULL, LYD_DUP_RECURSIVE, diff)); |
Michal Vasko | 4231fb6 | 2020-07-13 13:54:47 +0200 | [diff] [blame] | 2140 | |
| 2141 | /* find module with metadata needed for later */ |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 2142 | mod = ly_ctx_get_module_latest(LYD_CTX(src_diff), "yang"); |
| 2143 | LY_CHECK_ERR_GOTO(!mod, LOGINT(LYD_CTX(src_diff)); ret = LY_EINT, cleanup); |
Michal Vasko | 4231fb6 | 2020-07-13 13:54:47 +0200 | [diff] [blame] | 2144 | |
| 2145 | LY_LIST_FOR(*diff, root) { |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 2146 | LYD_TREE_DFS_BEGIN(root, elem) { |
Michal Vasko | 0f5c6a5 | 2020-11-06 17:18:03 +0100 | [diff] [blame] | 2147 | /* skip all keys */ |
| 2148 | if (!lysc_is_key(elem->schema)) { |
| 2149 | /* find operation attribute, if any */ |
| 2150 | LY_CHECK_GOTO(ret = lyd_diff_get_op(elem, &op), cleanup); |
Michal Vasko | 4231fb6 | 2020-07-13 13:54:47 +0200 | [diff] [blame] | 2151 | |
Michal Vasko | 0f5c6a5 | 2020-11-06 17:18:03 +0100 | [diff] [blame] | 2152 | switch (op) { |
| 2153 | case LYD_DIFF_OP_CREATE: |
| 2154 | /* reverse create to delete */ |
| 2155 | LY_CHECK_GOTO(ret = lyd_diff_change_op(elem, LYD_DIFF_OP_DELETE), cleanup); |
Michal Vasko | 9e07052 | 2021-03-05 14:00:14 +0100 | [diff] [blame] | 2156 | |
Michal Vasko | 9a7e9d0 | 2021-03-09 13:52:25 +0100 | [diff] [blame] | 2157 | /* check all the children for the same operation, nothing else is expected */ |
| 2158 | LY_LIST_FOR(lyd_child(elem), iter) { |
| 2159 | lyd_diff_reverse_remove_op_r(iter, LYD_DIFF_OP_CREATE); |
| 2160 | } |
| 2161 | |
Michal Vasko | 9e07052 | 2021-03-05 14:00:14 +0100 | [diff] [blame] | 2162 | LYD_TREE_DFS_continue = 1; |
Michal Vasko | 4231fb6 | 2020-07-13 13:54:47 +0200 | [diff] [blame] | 2163 | break; |
Michal Vasko | 0f5c6a5 | 2020-11-06 17:18:03 +0100 | [diff] [blame] | 2164 | case LYD_DIFF_OP_DELETE: |
| 2165 | /* reverse delete to create */ |
| 2166 | LY_CHECK_GOTO(ret = lyd_diff_change_op(elem, LYD_DIFF_OP_CREATE), cleanup); |
Michal Vasko | 9e07052 | 2021-03-05 14:00:14 +0100 | [diff] [blame] | 2167 | |
Michal Vasko | 9a7e9d0 | 2021-03-09 13:52:25 +0100 | [diff] [blame] | 2168 | /* check all the children for the same operation, nothing else is expected */ |
| 2169 | LY_LIST_FOR(lyd_child(elem), iter) { |
| 2170 | lyd_diff_reverse_remove_op_r(iter, LYD_DIFF_OP_DELETE); |
| 2171 | } |
| 2172 | |
Michal Vasko | 9e07052 | 2021-03-05 14:00:14 +0100 | [diff] [blame] | 2173 | LYD_TREE_DFS_continue = 1; |
Michal Vasko | 4231fb6 | 2020-07-13 13:54:47 +0200 | [diff] [blame] | 2174 | break; |
Michal Vasko | 0f5c6a5 | 2020-11-06 17:18:03 +0100 | [diff] [blame] | 2175 | case LYD_DIFF_OP_REPLACE: |
| 2176 | switch (elem->schema->nodetype) { |
| 2177 | case LYS_LEAF: |
| 2178 | /* leaf value change */ |
| 2179 | LY_CHECK_GOTO(ret = lyd_diff_reverse_value(elem, mod), cleanup); |
| 2180 | LY_CHECK_GOTO(ret = lyd_diff_reverse_default(elem, mod), cleanup); |
| 2181 | break; |
Michal Vasko | baba84e | 2021-02-05 16:33:30 +0100 | [diff] [blame] | 2182 | case LYS_ANYXML: |
| 2183 | case LYS_ANYDATA: |
| 2184 | /* any value change */ |
| 2185 | LY_CHECK_GOTO(ret = lyd_diff_reverse_value(elem, mod), cleanup); |
| 2186 | break; |
Michal Vasko | 0f5c6a5 | 2020-11-06 17:18:03 +0100 | [diff] [blame] | 2187 | case LYS_LEAFLIST: |
| 2188 | /* leaf-list move */ |
| 2189 | LY_CHECK_GOTO(ret = lyd_diff_reverse_default(elem, mod), cleanup); |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 2190 | if (lysc_is_dup_inst_list(elem->schema)) { |
| 2191 | LY_CHECK_GOTO(ret = lyd_diff_reverse_meta(elem, mod, "orig-position", "position"), cleanup); |
| 2192 | } else { |
| 2193 | LY_CHECK_GOTO(ret = lyd_diff_reverse_meta(elem, mod, "orig-value", "value"), cleanup); |
| 2194 | } |
Michal Vasko | 0f5c6a5 | 2020-11-06 17:18:03 +0100 | [diff] [blame] | 2195 | break; |
| 2196 | case LYS_LIST: |
| 2197 | /* list move */ |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 2198 | if (lysc_is_dup_inst_list(elem->schema)) { |
| 2199 | LY_CHECK_GOTO(ret = lyd_diff_reverse_meta(elem, mod, "orig-position", "position"), cleanup); |
| 2200 | } else { |
| 2201 | LY_CHECK_GOTO(ret = lyd_diff_reverse_meta(elem, mod, "orig-key", "key"), cleanup); |
| 2202 | } |
Michal Vasko | 0f5c6a5 | 2020-11-06 17:18:03 +0100 | [diff] [blame] | 2203 | break; |
| 2204 | default: |
| 2205 | LOGINT(LYD_CTX(src_diff)); |
| 2206 | ret = LY_EINT; |
| 2207 | goto cleanup; |
| 2208 | } |
Michal Vasko | 4231fb6 | 2020-07-13 13:54:47 +0200 | [diff] [blame] | 2209 | break; |
Michal Vasko | 0f5c6a5 | 2020-11-06 17:18:03 +0100 | [diff] [blame] | 2210 | case LYD_DIFF_OP_NONE: |
| 2211 | switch (elem->schema->nodetype) { |
| 2212 | case LYS_LEAF: |
| 2213 | case LYS_LEAFLIST: |
| 2214 | /* default flag change */ |
| 2215 | LY_CHECK_GOTO(ret = lyd_diff_reverse_default(elem, mod), cleanup); |
| 2216 | break; |
| 2217 | default: |
| 2218 | /* nothing to do */ |
| 2219 | break; |
| 2220 | } |
Michal Vasko | 4231fb6 | 2020-07-13 13:54:47 +0200 | [diff] [blame] | 2221 | break; |
| 2222 | } |
Michal Vasko | 4231fb6 | 2020-07-13 13:54:47 +0200 | [diff] [blame] | 2223 | } |
| 2224 | |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 2225 | LYD_TREE_DFS_END(root, elem); |
Michal Vasko | 4231fb6 | 2020-07-13 13:54:47 +0200 | [diff] [blame] | 2226 | } |
| 2227 | } |
| 2228 | |
| 2229 | cleanup: |
| 2230 | if (ret) { |
| 2231 | lyd_free_siblings(*diff); |
| 2232 | *diff = NULL; |
| 2233 | } |
| 2234 | return ret; |
| 2235 | } |