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