blob: 51189d24321ebdc80db09a3d31ec1bb38ff0ae95 [file] [log] [blame]
Michal Vaskod59035b2020-07-08 12:00:06 +02001/**
2 * @file diff.c
3 * @author Michal Vasko <mvasko@cesnet.cz>
4 * @brief diff functions
5 *
Michal Vaskoe78faec2021-04-08 17:24:43 +02006 * Copyright (c) 2020 - 2021 CESNET, z.s.p.o.
Michal Vaskod59035b2020-07-08 12:00:06 +02007 *
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 Hopps32874e12021-05-01 09:43:54 -040014#define _GNU_SOURCE /* asprintf, strdup */
Michal Vaskod59035b2020-07-08 12:00:06 +020015
16#include "diff.h"
17
18#include <assert.h>
19#include <stddef.h>
Michal Vaskoe78faec2021-04-08 17:24:43 +020020#include <stdint.h>
21#include <stdio.h>
Radek Krejci47fab892020-11-05 17:02:41 +010022#include <stdlib.h>
Michal Vaskod59035b2020-07-08 12:00:06 +020023#include <string.h>
24
25#include "common.h"
Michal Vaskoe78faec2021-04-08 17:24:43 +020026#include "compat.h"
Radek Krejci47fab892020-11-05 17:02:41 +010027#include "context.h"
Michal Vaskod59035b2020-07-08 12:00:06 +020028#include "log.h"
Michal Vasko19175b62022-04-01 09:17:07 +020029#include "plugins_exts.h"
Michal Vaskob4750962022-10-06 15:33:35 +020030#include "plugins_exts/metadata.h"
Radek Krejci47fab892020-11-05 17:02:41 +010031#include "plugins_types.h"
32#include "set.h"
33#include "tree.h"
34#include "tree_data.h"
Michal Vaskod59035b2020-07-08 12:00:06 +020035#include "tree_data_internal.h"
Radek Krejci859a15a2021-03-05 20:56:59 +010036#include "tree_edit.h"
Michal Vaskod59035b2020-07-08 12:00:06 +020037#include "tree_schema.h"
38#include "tree_schema_internal.h"
39
Michal Vasko52afd7d2022-01-18 14:08:34 +010040#define LOGERR_META(ctx, meta_name, node) \
41 { \
42 char *__path = lyd_path(node, LYD_PATH_STD, NULL, 0); \
43 LOGERR(ctx, LY_EINVAL, "Failed to find metadata \"%s\" for node \"%s\".", meta_name, __path); \
44 free(__path); \
45 }
46
47#define LOGERR_NOINST(ctx, node) \
48 { \
49 char *__path = lyd_path(node, LYD_PATH_STD, NULL, 0); \
50 LOGERR(ctx, LY_EINVAL, "Failed to find node \"%s\" instance in data.", __path); \
51 free(__path); \
52 }
53
54#define LOGERR_UNEXPVAL(ctx, node, data_source) \
55 { \
56 char *__path = lyd_path(node, LYD_PATH_STD, NULL, 0); \
57 LOGERR(ctx, LY_EINVAL, "Unexpected value of node \"%s\" in %s.", __path, data_source); \
58 free(__path); \
59 }
60
61#define LOGERR_MERGEOP(ctx, node, src_op, trg_op) \
62 { \
63 char *__path = lyd_path(node, LYD_PATH_STD, NULL, 0); \
64 LOGERR(ctx, LY_EINVAL, "Unable to merge operation \"%s\" with \"%s\" for node \"%s\".", \
65 lyd_diff_op2str(trg_op), lyd_diff_op2str(src_op), __path); \
66 free(__path); \
67 }
68
Michal Vaskod59035b2020-07-08 12:00:06 +020069static const char *
70lyd_diff_op2str(enum lyd_diff_op op)
71{
72 switch (op) {
73 case LYD_DIFF_OP_CREATE:
74 return "create";
75 case LYD_DIFF_OP_DELETE:
76 return "delete";
77 case LYD_DIFF_OP_REPLACE:
78 return "replace";
79 case LYD_DIFF_OP_NONE:
80 return "none";
81 }
82
83 LOGINT(NULL);
84 return NULL;
85}
86
Michal Vaskoe6323f62020-07-09 15:49:02 +020087static enum lyd_diff_op
88lyd_diff_str2op(const char *str)
89{
90 switch (str[0]) {
91 case 'c':
92 assert(!strcmp(str, "create"));
93 return LYD_DIFF_OP_CREATE;
94 case 'd':
95 assert(!strcmp(str, "delete"));
96 return LYD_DIFF_OP_DELETE;
97 case 'r':
98 assert(!strcmp(str, "replace"));
99 return LYD_DIFF_OP_REPLACE;
100 case 'n':
101 assert(!strcmp(str, "none"));
102 return LYD_DIFF_OP_NONE;
103 }
104
105 LOGINT(NULL);
106 return 0;
107}
108
Michal Vaskocffc3f92022-06-15 07:57:24 +0200109/**
110 * @brief Create diff metadata for a nested user-ordered node with the effective operation "create".
111 *
112 * @param[in] node User-rodered node to update.
113 * @return LY_ERR value.
114 */
115static LY_ERR
116lyd_diff_add_create_nested_userord(struct lyd_node *node)
117{
118 LY_ERR rc = LY_SUCCESS;
119 const char *meta_name, *meta_val;
120 size_t buflen = 0, bufused = 0;
121 uint32_t pos;
122 char *dyn = NULL;
123
124 assert(lysc_is_userordered(node->schema));
125
126 /* get correct metadata name and value */
127 if (lysc_is_dup_inst_list(node->schema)) {
128 meta_name = "yang:position";
129
130 pos = lyd_list_pos(node);
131 if (asprintf(&dyn, "%" PRIu32, pos) == -1) {
132 LOGMEM(LYD_CTX(node));
133 rc = LY_EMEM;
134 goto cleanup;
135 }
136 meta_val = dyn;
137 } else if (node->schema->nodetype == LYS_LIST) {
138 meta_name = "yang:key";
139
140 if (node->prev->next && (node->prev->schema == node->schema)) {
141 LY_CHECK_GOTO(rc = lyd_path_list_predicate(node->prev, &dyn, &buflen, &bufused, 0), cleanup);
142 meta_val = dyn;
143 } else {
144 meta_val = "";
145 }
146 } else {
147 meta_name = "yang:value";
148
149 if (node->prev->next && (node->prev->schema == node->schema)) {
150 meta_val = lyd_get_value(node->prev);
151 } else {
152 meta_val = "";
153 }
154 }
155
156 /* create the metadata */
157 LY_CHECK_GOTO(rc = lyd_new_meta(NULL, node, NULL, meta_name, meta_val, 0, NULL), cleanup);
158
159cleanup:
160 free(dyn);
161 return rc;
162}
163
Michal Vaskod59035b2020-07-08 12:00:06 +0200164LY_ERR
165lyd_diff_add(const struct lyd_node *node, enum lyd_diff_op op, const char *orig_default, const char *orig_value,
Michal Vaskoe78faec2021-04-08 17:24:43 +0200166 const char *key, const char *value, const char *position, const char *orig_key, const char *orig_position,
167 struct lyd_node **diff)
Michal Vaskod59035b2020-07-08 12:00:06 +0200168{
Michal Vaskocffc3f92022-06-15 07:57:24 +0200169 struct lyd_node *dup, *siblings, *match = NULL, *diff_parent = NULL, *elem;
Michal Vaskod59035b2020-07-08 12:00:06 +0200170 const struct lyd_node *parent = NULL;
Michal Vaskod59035b2020-07-08 12:00:06 +0200171
172 assert(diff);
173
Michal Vasko53d48422020-11-13 18:02:29 +0100174 /* replace leaf always needs orig-default and orig-value */
175 assert((node->schema->nodetype != LYS_LEAF) || (op != LYD_DIFF_OP_REPLACE) || (orig_default && orig_value));
176
177 /* create on userord needs key/value */
178 assert((node->schema->nodetype != LYS_LIST) || !(node->schema->flags & LYS_ORDBY_USER) || (op != LYD_DIFF_OP_CREATE) ||
Michal Vaskoe78faec2021-04-08 17:24:43 +0200179 (lysc_is_dup_inst_list(node->schema) && position) || key);
Michal Vasko53d48422020-11-13 18:02:29 +0100180 assert((node->schema->nodetype != LYS_LEAFLIST) || !(node->schema->flags & LYS_ORDBY_USER) ||
Michal Vaskoe78faec2021-04-08 17:24:43 +0200181 (op != LYD_DIFF_OP_CREATE) || (lysc_is_dup_inst_list(node->schema) && position) || value);
Michal Vasko53d48422020-11-13 18:02:29 +0100182
183 /* move on userord needs both key and orig-key/value and orig-value */
184 assert((node->schema->nodetype != LYS_LIST) || !(node->schema->flags & LYS_ORDBY_USER) || (op != LYD_DIFF_OP_REPLACE) ||
Michal Vaskoe78faec2021-04-08 17:24:43 +0200185 (lysc_is_dup_inst_list(node->schema) && position && orig_position) || (key && orig_key));
Michal Vasko53d48422020-11-13 18:02:29 +0100186 assert((node->schema->nodetype != LYS_LEAFLIST) || !(node->schema->flags & LYS_ORDBY_USER) ||
Michal Vaskoe78faec2021-04-08 17:24:43 +0200187 (op != LYD_DIFF_OP_REPLACE) || (lysc_is_dup_inst_list(node->schema) && position && orig_position) ||
188 (value && orig_value));
Michal Vasko53d48422020-11-13 18:02:29 +0100189
Michal Vaskod59035b2020-07-08 12:00:06 +0200190 /* find the first existing parent */
191 siblings = *diff;
192 while (1) {
193 /* find next node parent */
194 parent = node;
195 while (parent->parent && (!diff_parent || (parent->parent->schema != diff_parent->schema))) {
Michal Vasko9e685082021-01-29 14:49:09 +0100196 parent = lyd_parent(parent);
Michal Vaskod59035b2020-07-08 12:00:06 +0200197 }
198 if (parent == node) {
199 /* no more parents to find */
200 break;
201 }
202
203 /* check whether it exists in the diff */
204 if (lyd_find_sibling_first(siblings, parent, &match)) {
205 break;
206 }
207
208 /* another parent found */
209 diff_parent = match;
210
211 /* move down in the diff */
Radek Krejcia1c1e542020-09-29 16:06:52 +0200212 siblings = lyd_child_no_keys(match);
Michal Vaskod59035b2020-07-08 12:00:06 +0200213 }
214
215 /* duplicate the subtree (and connect to the diff if possible) */
Michal Vasko695a7f22023-02-14 10:02:10 +0100216 if (diff_parent) {
217 LY_CHECK_RET(lyd_dup_single_to_ctx(node, LYD_CTX(diff_parent), (struct lyd_node_inner *)diff_parent,
218 LYD_DUP_RECURSIVE | LYD_DUP_NO_META | LYD_DUP_WITH_PARENTS | LYD_DUP_WITH_FLAGS, &dup));
219 } else {
220 LY_CHECK_RET(lyd_dup_single(node, NULL,
221 LYD_DUP_RECURSIVE | LYD_DUP_NO_META | LYD_DUP_WITH_PARENTS | LYD_DUP_WITH_FLAGS, &dup));
222 }
Michal Vaskod59035b2020-07-08 12:00:06 +0200223
224 /* find the first duplicated parent */
225 if (!diff_parent) {
Michal Vasko9e685082021-01-29 14:49:09 +0100226 diff_parent = lyd_parent(dup);
Michal Vaskod59035b2020-07-08 12:00:06 +0200227 while (diff_parent && diff_parent->parent) {
Michal Vasko9e685082021-01-29 14:49:09 +0100228 diff_parent = lyd_parent(diff_parent);
Michal Vaskod59035b2020-07-08 12:00:06 +0200229 }
230 } else {
Michal Vasko9e685082021-01-29 14:49:09 +0100231 diff_parent = dup;
Michal Vaskod59035b2020-07-08 12:00:06 +0200232 while (diff_parent->parent && (diff_parent->parent->schema == parent->schema)) {
Michal Vasko9e685082021-01-29 14:49:09 +0100233 diff_parent = lyd_parent(diff_parent);
Michal Vaskod59035b2020-07-08 12:00:06 +0200234 }
235 }
236
237 /* no parent existed, must be manually connected */
238 if (!diff_parent) {
239 /* there actually was no parent to duplicate */
Michal Vaskob104f112020-07-17 09:54:54 +0200240 lyd_insert_sibling(*diff, dup, diff);
Michal Vaskod59035b2020-07-08 12:00:06 +0200241 } else if (!diff_parent->parent) {
Michal Vaskob104f112020-07-17 09:54:54 +0200242 lyd_insert_sibling(*diff, diff_parent, diff);
Michal Vaskod59035b2020-07-08 12:00:06 +0200243 }
244
Michal Vaskod59035b2020-07-08 12:00:06 +0200245 /* add parent operation, if any */
Michal Vasko3a41dff2020-07-15 14:30:28 +0200246 if (diff_parent && (diff_parent != dup)) {
Michal Vasko2e552792022-11-02 12:15:31 +0100247 LY_CHECK_RET(lyd_new_meta(NULL, diff_parent, NULL, "yang:operation", "none", 0, NULL));
Michal Vaskod59035b2020-07-08 12:00:06 +0200248 }
249
250 /* add subtree operation */
Michal Vasko2e552792022-11-02 12:15:31 +0100251 LY_CHECK_RET(lyd_new_meta(NULL, dup, NULL, "yang:operation", lyd_diff_op2str(op), 0, NULL));
Michal Vaskod59035b2020-07-08 12:00:06 +0200252
Michal Vaskocffc3f92022-06-15 07:57:24 +0200253 if (op == LYD_DIFF_OP_CREATE) {
254 /* all nested user-ordered (leaf-)lists need special metadata for create op */
255 LYD_TREE_DFS_BEGIN(dup, elem) {
256 if ((elem != dup) && lysc_is_userordered(elem->schema)) {
257 LY_CHECK_RET(lyd_diff_add_create_nested_userord(elem));
258 }
259 LYD_TREE_DFS_END(dup, elem);
260 }
261 }
262
Michal Vaskod59035b2020-07-08 12:00:06 +0200263 /* orig-default */
Michal Vasko3a41dff2020-07-15 14:30:28 +0200264 if (orig_default) {
Michal Vasko2e552792022-11-02 12:15:31 +0100265 LY_CHECK_RET(lyd_new_meta(NULL, dup, NULL, "yang:orig-default", orig_default, 0, NULL));
Michal Vaskod59035b2020-07-08 12:00:06 +0200266 }
267
268 /* orig-value */
Michal Vasko3a41dff2020-07-15 14:30:28 +0200269 if (orig_value) {
Michal Vasko2e552792022-11-02 12:15:31 +0100270 LY_CHECK_RET(lyd_new_meta(NULL, dup, NULL, "yang:orig-value", orig_value, 0, NULL));
Michal Vaskod59035b2020-07-08 12:00:06 +0200271 }
272
273 /* key */
Michal Vasko3a41dff2020-07-15 14:30:28 +0200274 if (key) {
Michal Vasko2e552792022-11-02 12:15:31 +0100275 LY_CHECK_RET(lyd_new_meta(NULL, dup, NULL, "yang:key", key, 0, NULL));
Michal Vaskod59035b2020-07-08 12:00:06 +0200276 }
277
278 /* value */
Michal Vasko3a41dff2020-07-15 14:30:28 +0200279 if (value) {
Michal Vasko2e552792022-11-02 12:15:31 +0100280 LY_CHECK_RET(lyd_new_meta(NULL, dup, NULL, "yang:value", value, 0, NULL));
Michal Vaskod59035b2020-07-08 12:00:06 +0200281 }
282
Michal Vaskoe78faec2021-04-08 17:24:43 +0200283 /* position */
284 if (position) {
Michal Vasko2e552792022-11-02 12:15:31 +0100285 LY_CHECK_RET(lyd_new_meta(NULL, dup, NULL, "yang:position", position, 0, NULL));
Michal Vaskoe78faec2021-04-08 17:24:43 +0200286 }
287
Michal Vaskod59035b2020-07-08 12:00:06 +0200288 /* orig-key */
Michal Vasko3a41dff2020-07-15 14:30:28 +0200289 if (orig_key) {
Michal Vasko2e552792022-11-02 12:15:31 +0100290 LY_CHECK_RET(lyd_new_meta(NULL, dup, NULL, "yang:orig-key", orig_key, 0, NULL));
Michal Vaskod59035b2020-07-08 12:00:06 +0200291 }
292
Michal Vaskoe78faec2021-04-08 17:24:43 +0200293 /* orig-position */
294 if (orig_position) {
Michal Vasko2e552792022-11-02 12:15:31 +0100295 LY_CHECK_RET(lyd_new_meta(NULL, dup, NULL, "yang:orig-position", orig_position, 0, NULL));
Michal Vaskoe78faec2021-04-08 17:24:43 +0200296 }
297
Michal Vaskod59035b2020-07-08 12:00:06 +0200298 return LY_SUCCESS;
299}
300
301/**
302 * @brief Get a userord entry for a specific user-ordered list/leaf-list. Create if does not exist yet.
303 *
Michal Vasko1dcd73b2020-12-08 10:04:33 +0100304 * @param[in] first Node from the first tree, can be NULL (on create).
Michal Vaskod59035b2020-07-08 12:00:06 +0200305 * @param[in] schema Schema node of the list/leaf-list.
306 * @param[in,out] userord Sized array of userord items.
307 * @return Userord item for all the user-ordered list/leaf-list instances.
308 */
309static struct lyd_diff_userord *
310lyd_diff_userord_get(const struct lyd_node *first, const struct lysc_node *schema, struct lyd_diff_userord **userord)
311{
312 struct lyd_diff_userord *item;
Michal Vaskoe78faec2021-04-08 17:24:43 +0200313 struct lyd_node *iter;
314 const struct lyd_node **node;
Michal Vaskod59035b2020-07-08 12:00:06 +0200315 LY_ARRAY_COUNT_TYPE u;
316
317 LY_ARRAY_FOR(*userord, u) {
318 if ((*userord)[u].schema == schema) {
319 return &(*userord)[u];
320 }
321 }
322
323 /* it was not added yet, add it now */
324 LY_ARRAY_NEW_RET(schema->module->ctx, *userord, item, NULL);
325
326 item->schema = schema;
327 item->pos = 0;
328 item->inst = NULL;
329
330 /* store all the instance pointers in the current order */
331 if (first) {
Michal Vaskoe78faec2021-04-08 17:24:43 +0200332 LYD_LIST_FOR_INST(lyd_first_sibling(first), first->schema, iter) {
333 LY_ARRAY_NEW_RET(schema->module->ctx, item->inst, node, NULL);
334 *node = iter;
Michal Vaskod59035b2020-07-08 12:00:06 +0200335 }
336 }
337
338 return item;
339}
340
341/**
342 * @brief Get all the metadata to be stored in a diff for the 2 nodes. Can be used only for user-ordered
343 * lists/leaf-lists.
344 *
345 * @param[in] first Node from the first tree, can be NULL (on create).
346 * @param[in] second Node from the second tree, can be NULL (on delete).
347 * @param[in] options Diff options.
Michal Vasko5da938a2022-03-01 09:19:02 +0100348 * @param[in] userord_item Userord item of @p first and/or @p second node.
Michal Vaskod59035b2020-07-08 12:00:06 +0200349 * @param[out] op Operation.
350 * @param[out] orig_default Original default metadata.
351 * @param[out] value Value metadata.
352 * @param[out] orig_value Original value metadata
353 * @param[out] key Key metadata.
354 * @param[out] orig_key Original key metadata.
Michal Vaskoe78faec2021-04-08 17:24:43 +0200355 * @param[out] position Position metadata.
356 * @param[out] orig_position Original position metadata.
Michal Vaskod59035b2020-07-08 12:00:06 +0200357 * @return LY_SUCCESS on success,
358 * @return LY_ENOT if there is no change to be added into diff,
359 * @return LY_ERR value on other errors.
360 */
361static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200362lyd_diff_userord_attrs(const struct lyd_node *first, const struct lyd_node *second, uint16_t options,
Michal Vasko5da938a2022-03-01 09:19:02 +0100363 struct lyd_diff_userord *userord_item, enum lyd_diff_op *op, const char **orig_default, char **value,
Michal Vaskoe78faec2021-04-08 17:24:43 +0200364 char **orig_value, char **key, char **orig_key, char **position, char **orig_position)
Michal Vaskod59035b2020-07-08 12:00:06 +0200365{
Michal Vaskof9b052a2022-06-08 10:26:53 +0200366 LY_ERR rc = LY_SUCCESS;
Michal Vaskod59035b2020-07-08 12:00:06 +0200367 const struct lysc_node *schema;
Michal Vaskoe78faec2021-04-08 17:24:43 +0200368 size_t buflen, bufused;
369 uint32_t first_pos, second_pos;
Michal Vaskod59035b2020-07-08 12:00:06 +0200370
371 assert(first || second);
372
373 *orig_default = NULL;
374 *value = NULL;
375 *orig_value = NULL;
376 *key = NULL;
377 *orig_key = NULL;
Michal Vaskoe78faec2021-04-08 17:24:43 +0200378 *position = NULL;
379 *orig_position = NULL;
Michal Vaskod59035b2020-07-08 12:00:06 +0200380
381 schema = first ? first->schema : second->schema;
382 assert(lysc_is_userordered(schema));
383
Michal Vaskod59035b2020-07-08 12:00:06 +0200384 /* find user-ordered first position */
385 if (first) {
Michal Vaskoe78faec2021-04-08 17:24:43 +0200386 for (first_pos = 0; first_pos < LY_ARRAY_COUNT(userord_item->inst); ++first_pos) {
Michal Vaskod59035b2020-07-08 12:00:06 +0200387 if (userord_item->inst[first_pos] == first) {
388 break;
389 }
390 }
391 assert(first_pos < LY_ARRAY_COUNT(userord_item->inst));
392 } else {
393 first_pos = 0;
394 }
395
Michal Vaskoe78faec2021-04-08 17:24:43 +0200396 /* prepare position of the next instance */
397 second_pos = userord_item->pos++;
398
Michal Vaskod59035b2020-07-08 12:00:06 +0200399 /* learn operation first */
400 if (!second) {
401 *op = LYD_DIFF_OP_DELETE;
402 } else if (!first) {
403 *op = LYD_DIFF_OP_CREATE;
404 } else {
Michal Vasko8f359bf2020-07-28 10:41:15 +0200405 if (lyd_compare_single(second, userord_item->inst[second_pos], 0)) {
Michal Vaskod59035b2020-07-08 12:00:06 +0200406 /* in first, there is a different instance on the second position, we are going to move 'first' node */
407 *op = LYD_DIFF_OP_REPLACE;
Michal Vasko3a41dff2020-07-15 14:30:28 +0200408 } else if ((options & LYD_DIFF_DEFAULTS) && ((first->flags & LYD_DEFAULT) != (second->flags & LYD_DEFAULT))) {
Michal Vaskod59035b2020-07-08 12:00:06 +0200409 /* default flag change */
410 *op = LYD_DIFF_OP_NONE;
411 } else {
412 /* no changes */
413 return LY_ENOT;
414 }
415 }
416
417 /*
418 * set each attribute correctly based on the operation and node type
419 */
420
421 /* orig-default */
Michal Vasko4b715ca2020-11-11 18:39:57 +0100422 if ((schema->nodetype == LYS_LEAFLIST) && ((*op == LYD_DIFF_OP_REPLACE) || (*op == LYD_DIFF_OP_NONE))) {
Michal Vaskod59035b2020-07-08 12:00:06 +0200423 if (first->flags & LYD_DEFAULT) {
424 *orig_default = "true";
425 } else {
426 *orig_default = "false";
427 }
428 }
429
430 /* value */
Michal Vaskoe78faec2021-04-08 17:24:43 +0200431 if ((schema->nodetype == LYS_LEAFLIST) && !lysc_is_dup_inst_list(schema) &&
432 ((*op == LYD_DIFF_OP_REPLACE) || (*op == LYD_DIFF_OP_CREATE))) {
Michal Vaskod59035b2020-07-08 12:00:06 +0200433 if (second_pos) {
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200434 *value = strdup(lyd_get_value(userord_item->inst[second_pos - 1]));
Michal Vaskof9b052a2022-06-08 10:26:53 +0200435 LY_CHECK_ERR_GOTO(!*value, LOGMEM(schema->module->ctx); rc = LY_EMEM, cleanup);
Michal Vaskod59035b2020-07-08 12:00:06 +0200436 } else {
437 *value = strdup("");
Michal Vaskof9b052a2022-06-08 10:26:53 +0200438 LY_CHECK_ERR_GOTO(!*value, LOGMEM(schema->module->ctx); rc = LY_EMEM, cleanup);
Michal Vaskod59035b2020-07-08 12:00:06 +0200439 }
440 }
441
442 /* orig-value */
Michal Vaskoe78faec2021-04-08 17:24:43 +0200443 if ((schema->nodetype == LYS_LEAFLIST) && !lysc_is_dup_inst_list(schema) &&
444 ((*op == LYD_DIFF_OP_REPLACE) || (*op == LYD_DIFF_OP_DELETE))) {
Michal Vaskod59035b2020-07-08 12:00:06 +0200445 if (first_pos) {
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200446 *orig_value = strdup(lyd_get_value(userord_item->inst[first_pos - 1]));
Michal Vaskof9b052a2022-06-08 10:26:53 +0200447 LY_CHECK_ERR_GOTO(!*orig_value, LOGMEM(schema->module->ctx); rc = LY_EMEM, cleanup);
Michal Vaskod59035b2020-07-08 12:00:06 +0200448 } else {
449 *orig_value = strdup("");
Michal Vaskof9b052a2022-06-08 10:26:53 +0200450 LY_CHECK_ERR_GOTO(!*orig_value, LOGMEM(schema->module->ctx); rc = LY_EMEM, cleanup);
Michal Vaskod59035b2020-07-08 12:00:06 +0200451 }
452 }
453
454 /* key */
Michal Vaskoe78faec2021-04-08 17:24:43 +0200455 if ((schema->nodetype == LYS_LIST) && !lysc_is_dup_inst_list(schema) &&
456 ((*op == LYD_DIFF_OP_REPLACE) || (*op == LYD_DIFF_OP_CREATE))) {
Michal Vaskod59035b2020-07-08 12:00:06 +0200457 if (second_pos) {
458 buflen = bufused = 0;
Michal Vaskof9b052a2022-06-08 10:26:53 +0200459 LY_CHECK_GOTO(rc = lyd_path_list_predicate(userord_item->inst[second_pos - 1], key, &buflen, &bufused, 0), cleanup);
Michal Vaskod59035b2020-07-08 12:00:06 +0200460 } else {
461 *key = strdup("");
Michal Vaskof9b052a2022-06-08 10:26:53 +0200462 LY_CHECK_ERR_GOTO(!*key, LOGMEM(schema->module->ctx); rc = LY_EMEM, cleanup);
Michal Vaskod59035b2020-07-08 12:00:06 +0200463 }
464 }
465
466 /* orig-key */
Michal Vaskoe78faec2021-04-08 17:24:43 +0200467 if ((schema->nodetype == LYS_LIST) && !lysc_is_dup_inst_list(schema) &&
468 ((*op == LYD_DIFF_OP_REPLACE) || (*op == LYD_DIFF_OP_DELETE))) {
Michal Vaskod59035b2020-07-08 12:00:06 +0200469 if (first_pos) {
470 buflen = bufused = 0;
Michal Vaskof9b052a2022-06-08 10:26:53 +0200471 LY_CHECK_GOTO(rc = lyd_path_list_predicate(userord_item->inst[first_pos - 1], orig_key, &buflen, &bufused, 0), cleanup);
Michal Vaskod59035b2020-07-08 12:00:06 +0200472 } else {
473 *orig_key = strdup("");
Michal Vaskof9b052a2022-06-08 10:26:53 +0200474 LY_CHECK_ERR_GOTO(!*orig_key, LOGMEM(schema->module->ctx); rc = LY_EMEM, cleanup);
Michal Vaskod59035b2020-07-08 12:00:06 +0200475 }
476 }
477
Michal Vaskoe78faec2021-04-08 17:24:43 +0200478 /* position */
479 if (lysc_is_dup_inst_list(schema) && ((*op == LYD_DIFF_OP_REPLACE) || (*op == LYD_DIFF_OP_CREATE))) {
480 if (second_pos) {
481 if (asprintf(position, "%" PRIu32, second_pos) == -1) {
482 LOGMEM(schema->module->ctx);
Michal Vaskof9b052a2022-06-08 10:26:53 +0200483 rc = LY_EMEM;
484 goto cleanup;
Michal Vaskoe78faec2021-04-08 17:24:43 +0200485 }
486 } else {
487 *position = strdup("");
Michal Vaskof9b052a2022-06-08 10:26:53 +0200488 LY_CHECK_ERR_GOTO(!*position, LOGMEM(schema->module->ctx); rc = LY_EMEM, cleanup);
Michal Vaskoe78faec2021-04-08 17:24:43 +0200489 }
490 }
491
492 /* orig-position */
493 if (lysc_is_dup_inst_list(schema) && ((*op == LYD_DIFF_OP_REPLACE) || (*op == LYD_DIFF_OP_DELETE))) {
494 if (first_pos) {
495 if (asprintf(orig_position, "%" PRIu32, first_pos) == -1) {
496 LOGMEM(schema->module->ctx);
Michal Vaskof9b052a2022-06-08 10:26:53 +0200497 rc = LY_EMEM;
498 goto cleanup;
Michal Vaskoe78faec2021-04-08 17:24:43 +0200499 }
500 } else {
501 *orig_position = strdup("");
Michal Vaskof9b052a2022-06-08 10:26:53 +0200502 LY_CHECK_ERR_GOTO(!*orig_position, LOGMEM(schema->module->ctx); rc = LY_EMEM, cleanup);
Michal Vaskoe78faec2021-04-08 17:24:43 +0200503 }
504 }
505
Michal Vaskod59035b2020-07-08 12:00:06 +0200506 /*
507 * update our instances - apply the change
508 */
509 if (*op == LYD_DIFF_OP_CREATE) {
510 /* insert the instance */
Michal Vaskof9b052a2022-06-08 10:26:53 +0200511 LY_ARRAY_CREATE_GOTO(schema->module->ctx, userord_item->inst, 1, rc, cleanup);
Michal Vaskod59035b2020-07-08 12:00:06 +0200512 if (second_pos < LY_ARRAY_COUNT(userord_item->inst)) {
513 memmove(userord_item->inst + second_pos + 1, userord_item->inst + second_pos,
514 (LY_ARRAY_COUNT(userord_item->inst) - second_pos) * sizeof *userord_item->inst);
515 }
516 LY_ARRAY_INCREMENT(userord_item->inst);
517 userord_item->inst[second_pos] = second;
518
519 } else if (*op == LYD_DIFF_OP_DELETE) {
520 /* remove the instance */
521 if (first_pos + 1 < LY_ARRAY_COUNT(userord_item->inst)) {
522 memmove(userord_item->inst + first_pos, userord_item->inst + first_pos + 1,
523 (LY_ARRAY_COUNT(userord_item->inst) - first_pos - 1) * sizeof *userord_item->inst);
524 }
525 LY_ARRAY_DECREMENT(userord_item->inst);
526
527 } else if (*op == LYD_DIFF_OP_REPLACE) {
528 /* move the instances */
529 memmove(userord_item->inst + second_pos + 1, userord_item->inst + second_pos,
530 (first_pos - second_pos) * sizeof *userord_item->inst);
531 userord_item->inst[second_pos] = first;
532 }
533
Michal Vaskof9b052a2022-06-08 10:26:53 +0200534cleanup:
535 if (rc) {
536 free(*value);
537 *value = NULL;
538 free(*orig_value);
539 *orig_value = NULL;
540 free(*key);
541 *key = NULL;
542 free(*orig_key);
543 *orig_key = NULL;
544 free(*position);
545 *position = NULL;
546 free(*orig_position);
547 *orig_position = NULL;
548 }
549 return rc;
Michal Vaskod59035b2020-07-08 12:00:06 +0200550}
551
552/**
553 * @brief Get all the metadata to be stored in a diff for the 2 nodes. Cannot be used for user-ordered
554 * lists/leaf-lists.
555 *
556 * @param[in] first Node from the first tree, can be NULL (on create).
557 * @param[in] second Node from the second tree, can be NULL (on delete).
558 * @param[in] options Diff options.
559 * @param[out] op Operation.
560 * @param[out] orig_default Original default metadata.
561 * @param[out] orig_value Original value metadata.
562 * @return LY_SUCCESS on success,
563 * @return LY_ENOT if there is no change to be added into diff,
564 * @return LY_ERR value on other errors.
565 */
566static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200567lyd_diff_attrs(const struct lyd_node *first, const struct lyd_node *second, uint16_t options, enum lyd_diff_op *op,
Radek Krejci0f969882020-08-21 16:56:47 +0200568 const char **orig_default, char **orig_value)
Michal Vaskod59035b2020-07-08 12:00:06 +0200569{
570 const struct lysc_node *schema;
Michal Vasko6ea6fe22021-10-08 09:57:01 +0200571 const char *str_val;
Michal Vaskod59035b2020-07-08 12:00:06 +0200572
573 assert(first || second);
574
575 *orig_default = NULL;
576 *orig_value = NULL;
577
578 schema = first ? first->schema : second->schema;
579 assert(!lysc_is_userordered(schema));
580
581 /* learn operation first */
582 if (!second) {
583 *op = LYD_DIFF_OP_DELETE;
584 } else if (!first) {
585 *op = LYD_DIFF_OP_CREATE;
586 } else {
587 switch (schema->nodetype) {
588 case LYS_CONTAINER:
589 case LYS_RPC:
590 case LYS_ACTION:
591 case LYS_NOTIF:
592 /* no changes */
593 return LY_ENOT;
594 case LYS_LIST:
595 case LYS_LEAFLIST:
Michal Vasko3a41dff2020-07-15 14:30:28 +0200596 if ((options & LYD_DIFF_DEFAULTS) && ((first->flags & LYD_DEFAULT) != (second->flags & LYD_DEFAULT))) {
Michal Vaskod59035b2020-07-08 12:00:06 +0200597 /* default flag change */
598 *op = LYD_DIFF_OP_NONE;
599 } else {
600 /* no changes */
601 return LY_ENOT;
602 }
603 break;
604 case LYS_LEAF:
605 case LYS_ANYXML:
606 case LYS_ANYDATA:
Michal Vasko8f359bf2020-07-28 10:41:15 +0200607 if (lyd_compare_single(first, second, 0)) {
Michal Vaskod59035b2020-07-08 12:00:06 +0200608 /* different values */
609 *op = LYD_DIFF_OP_REPLACE;
Michal Vasko3a41dff2020-07-15 14:30:28 +0200610 } else if ((options & LYD_DIFF_DEFAULTS) && ((first->flags & LYD_DEFAULT) != (second->flags & LYD_DEFAULT))) {
Michal Vaskod59035b2020-07-08 12:00:06 +0200611 /* default flag change */
612 *op = LYD_DIFF_OP_NONE;
613 } else {
614 /* no changes */
615 return LY_ENOT;
616 }
617 break;
618 default:
619 LOGINT_RET(schema->module->ctx);
620 }
621 }
622
623 /*
624 * set each attribute correctly based on the operation and node type
625 */
626
627 /* orig-default */
Michal Vasko4b715ca2020-11-11 18:39:57 +0100628 if ((schema->nodetype & LYD_NODE_TERM) && ((*op == LYD_DIFF_OP_REPLACE) || (*op == LYD_DIFF_OP_NONE))) {
Michal Vaskod59035b2020-07-08 12:00:06 +0200629 if (first->flags & LYD_DEFAULT) {
630 *orig_default = "true";
631 } else {
632 *orig_default = "false";
633 }
634 }
635
636 /* orig-value */
Michal Vaskobaba84e2021-02-05 16:33:30 +0100637 if ((schema->nodetype & (LYS_LEAF | LYS_ANYDATA)) && (*op == LYD_DIFF_OP_REPLACE)) {
638 if (schema->nodetype == LYS_LEAF) {
Michal Vasko6ea6fe22021-10-08 09:57:01 +0200639 str_val = lyd_get_value(first);
640 *orig_value = strdup(str_val ? str_val : "");
Michal Vaskobaba84e2021-02-05 16:33:30 +0100641 LY_CHECK_ERR_RET(!*orig_value, LOGMEM(schema->module->ctx), LY_EMEM);
642 } else {
643 LY_CHECK_RET(lyd_any_value_str(first, orig_value));
644 }
Michal Vaskod59035b2020-07-08 12:00:06 +0200645 }
646
647 return LY_SUCCESS;
648}
649
650/**
Michal Vaskoe78faec2021-04-08 17:24:43 +0200651 * @brief Find a matching instance of a node in a data tree.
652 *
653 * @param[in] siblings Siblings to search in.
654 * @param[in] target Target node to search for.
655 * @param[in] defaults Whether to consider (or ignore) default values.
Michal Vasko271d2e32023-01-31 15:43:19 +0100656 * @param[in,out] dup_inst_ht Duplicate instance cache.
Michal Vaskoe78faec2021-04-08 17:24:43 +0200657 * @param[out] match Found match, NULL if no matching node found.
658 * @return LY_ERR value.
659 */
660static LY_ERR
661lyd_diff_find_match(const struct lyd_node *siblings, const struct lyd_node *target, ly_bool defaults,
Michal Vasko8efac242023-03-30 08:24:56 +0200662 struct ly_ht **dup_inst_ht, struct lyd_node **match)
Michal Vaskoe78faec2021-04-08 17:24:43 +0200663{
Michal Vasko2bd856f2022-12-02 14:03:29 +0100664 LY_ERR r;
665
Michal Vaskodb91fc32023-05-02 14:39:40 +0200666 if (!target->schema) {
667 /* try to find the same opaque node */
668 r = lyd_find_sibling_opaq_next(siblings, LYD_NAME(target), match);
669 } else if (target->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) {
Michal Vaskoe78faec2021-04-08 17:24:43 +0200670 /* try to find the exact instance */
Michal Vasko2bd856f2022-12-02 14:03:29 +0100671 r = lyd_find_sibling_first(siblings, target, match);
Michal Vaskoe78faec2021-04-08 17:24:43 +0200672 } else {
673 /* try to simply find the node, there cannot be more instances */
Michal Vasko2bd856f2022-12-02 14:03:29 +0100674 r = lyd_find_sibling_val(siblings, target->schema, NULL, 0, match);
675 }
676 if (r && (r != LY_ENOTFOUND)) {
677 return r;
Michal Vaskoe78faec2021-04-08 17:24:43 +0200678 }
679
Michal Vaskod7c048c2021-05-18 16:12:55 +0200680 /* update match as needed */
Michal Vasko271d2e32023-01-31 15:43:19 +0100681 LY_CHECK_RET(lyd_dup_inst_next(match, siblings, dup_inst_ht));
Michal Vaskoe78faec2021-04-08 17:24:43 +0200682
683 if (*match && ((*match)->flags & LYD_DEFAULT) && !defaults) {
684 /* ignore default nodes */
685 *match = NULL;
686 }
687 return LY_SUCCESS;
688}
689
690/**
Michal Vaskod59035b2020-07-08 12:00:06 +0200691 * @brief Perform diff for all siblings at certain depth, recursively.
692 *
693 * For user-ordered lists/leaf-lists a specific structure is used for storing
694 * the current order. The idea is to apply all the generated diff changes
695 * virtually on the first tree so that we can continue to generate correct
696 * changes after some were already generated.
697 *
698 * The algorithm then uses second tree position-based changes with a before
699 * (preceding) item anchor.
700 *
701 * Example:
702 *
703 * Virtual first tree leaf-list order:
704 * 1 2 [3] 4 5
705 *
706 * Second tree leaf-list order:
707 * 1 2 [5] 3 4
708 *
709 * We are at the 3rd node now. We look at whether the nodes on the 3rd position
710 * match - they do not - move nodes so that the 3rd position node is final ->
711 * -> move node 5 to the 3rd position -> move node 5 after node 2.
712 *
713 * Required properties:
714 * Stored operations (move) should not be affected by later operations -
715 * - would cause a redundantly long list of operations, possibly inifinite.
716 *
717 * Implemenation justification:
718 * First, all delete operations and only then move/create operations are stored.
719 * Also, preceding anchor is used and after each iteration another node is
720 * at its final position. That results in the invariant that all preceding
721 * nodes are final and will not be changed by the later operations, meaning
722 * they can safely be used as anchors for the later operations.
723 *
724 * @param[in] first First tree first sibling.
725 * @param[in] second Second tree first sibling.
726 * @param[in] options Diff options.
Michal Vasko3a41dff2020-07-15 14:30:28 +0200727 * @param[in] nosiblings Whether to skip following siblings.
Michal Vaskod59035b2020-07-08 12:00:06 +0200728 * @param[in,out] diff Diff to append to.
729 * @return LY_ERR value.
730 */
731static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +0200732lyd_diff_siblings_r(const struct lyd_node *first, const struct lyd_node *second, uint16_t options, ly_bool nosiblings,
Radek Krejci0f969882020-08-21 16:56:47 +0200733 struct lyd_node **diff)
Michal Vaskod59035b2020-07-08 12:00:06 +0200734{
735 LY_ERR ret = LY_SUCCESS;
736 const struct lyd_node *iter_first, *iter_second;
737 struct lyd_node *match_second, *match_first;
Michal Vasko5da938a2022-03-01 09:19:02 +0100738 struct lyd_diff_userord *userord = NULL, *userord_item;
Michal Vasko8efac242023-03-30 08:24:56 +0200739 struct ly_ht *dup_inst_first = NULL, *dup_inst_second = NULL;
Michal Vaskod59035b2020-07-08 12:00:06 +0200740 LY_ARRAY_COUNT_TYPE u;
741 enum lyd_diff_op op;
742 const char *orig_default;
Michal Vaskoe78faec2021-04-08 17:24:43 +0200743 char *orig_value, *key, *value, *position, *orig_key, *orig_position;
Michal Vaskod59035b2020-07-08 12:00:06 +0200744
Michal Vaskod59035b2020-07-08 12:00:06 +0200745 /* compare first tree to the second tree - delete, replace, none */
746 LY_LIST_FOR(first, iter_first) {
Michal Vaskoc825ed72021-07-21 16:05:59 +0200747 if (!iter_first->schema) {
748 continue;
749 }
750
Michal Vaskod59035b2020-07-08 12:00:06 +0200751 assert(!(iter_first->schema->flags & LYS_KEY));
Michal Vasko3a41dff2020-07-15 14:30:28 +0200752 if ((iter_first->flags & LYD_DEFAULT) && !(options & LYD_DIFF_DEFAULTS)) {
Michal Vaskod59035b2020-07-08 12:00:06 +0200753 /* skip default nodes */
754 continue;
755 }
756
Michal Vaskoe78faec2021-04-08 17:24:43 +0200757 /* find a match in the second tree */
758 LY_CHECK_GOTO(ret = lyd_diff_find_match(second, iter_first, options & LYD_DIFF_DEFAULTS, &dup_inst_second,
759 &match_second), cleanup);
Michal Vaskod59035b2020-07-08 12:00:06 +0200760
761 if (lysc_is_userordered(iter_first->schema)) {
Michal Vasko5da938a2022-03-01 09:19:02 +0100762 /* get (create) userord entry */
763 userord_item = lyd_diff_userord_get(iter_first, iter_first->schema, &userord);
764 LY_CHECK_ERR_GOTO(!userord_item, LOGMEM(LYD_CTX(iter_first)); ret = LY_EMEM, cleanup);
765
Michal Vaskoe78faec2021-04-08 17:24:43 +0200766 /* we are handling only user-ordered node delete now */
767 if (!match_second) {
768 /* get all the attributes */
Michal Vasko5da938a2022-03-01 09:19:02 +0100769 LY_CHECK_GOTO(ret = lyd_diff_userord_attrs(iter_first, match_second, options, userord_item, &op,
770 &orig_default, &value, &orig_value, &key, &orig_key, &position, &orig_position), cleanup);
Michal Vaskoe78faec2021-04-08 17:24:43 +0200771
772 /* there must be changes, it is deleted */
773 assert(op == LYD_DIFF_OP_DELETE);
Michal Vasko5da938a2022-03-01 09:19:02 +0100774 ret = lyd_diff_add(iter_first, op, orig_default, orig_value, key, value, position, orig_key,
775 orig_position, diff);
Michal Vaskoe78faec2021-04-08 17:24:43 +0200776
777 free(orig_value);
778 free(key);
779 free(value);
780 free(position);
781 free(orig_key);
782 free(orig_position);
783 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskod59035b2020-07-08 12:00:06 +0200784 }
Michal Vaskod59035b2020-07-08 12:00:06 +0200785 } else {
786 /* get all the attributes */
787 ret = lyd_diff_attrs(iter_first, match_second, options, &op, &orig_default, &orig_value);
788
789 /* add into diff if there are any changes */
790 if (!ret) {
791 if (op == LYD_DIFF_OP_DELETE) {
Michal Vaskoe78faec2021-04-08 17:24:43 +0200792 ret = lyd_diff_add(iter_first, op, orig_default, orig_value, NULL, NULL, NULL, NULL, NULL, diff);
Michal Vaskod59035b2020-07-08 12:00:06 +0200793 } else {
Michal Vasko3c2dd6c2020-11-06 17:38:55 +0100794 assert(match_second);
Michal Vaskoe78faec2021-04-08 17:24:43 +0200795 ret = lyd_diff_add(match_second, op, orig_default, orig_value, NULL, NULL, NULL, NULL, NULL, diff);
Michal Vaskod59035b2020-07-08 12:00:06 +0200796 }
797
798 free(orig_value);
799 LY_CHECK_GOTO(ret, cleanup);
800 } else if (ret == LY_ENOT) {
801 ret = LY_SUCCESS;
802 } else {
803 goto cleanup;
804 }
805 }
806
807 /* check descendants, if any, recursively */
808 if (match_second) {
Michal Vaskoe78faec2021-04-08 17:24:43 +0200809 LY_CHECK_GOTO(ret = lyd_diff_siblings_r(lyd_child_no_keys(iter_first), lyd_child_no_keys(match_second),
810 options, 0, diff), cleanup);
Michal Vaskod59035b2020-07-08 12:00:06 +0200811 }
812
813 if (nosiblings) {
814 break;
815 }
816 }
817
818 /* reset all cached positions */
819 LY_ARRAY_FOR(userord, u) {
820 userord[u].pos = 0;
821 }
822
823 /* compare second tree to the first tree - create, user-ordered move */
824 LY_LIST_FOR(second, iter_second) {
Michal Vaskoc825ed72021-07-21 16:05:59 +0200825 if (!iter_second->schema) {
826 continue;
827 }
828
Michal Vaskod59035b2020-07-08 12:00:06 +0200829 assert(!(iter_second->schema->flags & LYS_KEY));
Michal Vasko3a41dff2020-07-15 14:30:28 +0200830 if ((iter_second->flags & LYD_DEFAULT) && !(options & LYD_DIFF_DEFAULTS)) {
Michal Vaskod59035b2020-07-08 12:00:06 +0200831 /* skip default nodes */
832 continue;
833 }
834
Michal Vaskoe78faec2021-04-08 17:24:43 +0200835 /* find a match in the first tree */
836 LY_CHECK_GOTO(ret = lyd_diff_find_match(first, iter_second, options & LYD_DIFF_DEFAULTS, &dup_inst_first,
837 &match_first), cleanup);
Michal Vaskod59035b2020-07-08 12:00:06 +0200838
839 if (lysc_is_userordered(iter_second->schema)) {
Michal Vasko5da938a2022-03-01 09:19:02 +0100840 /* get userord entry */
Michal Vaskoaa51cc52022-12-06 09:57:11 +0100841 userord_item = lyd_diff_userord_get(match_first, iter_second->schema, &userord);
Michal Vasko5da938a2022-03-01 09:19:02 +0100842 LY_CHECK_ERR_GOTO(!userord_item, LOGMEM(LYD_CTX(iter_second)); ret = LY_EMEM, cleanup);
843
Michal Vaskod59035b2020-07-08 12:00:06 +0200844 /* get all the attributes */
Michal Vasko5da938a2022-03-01 09:19:02 +0100845 ret = lyd_diff_userord_attrs(match_first, iter_second, options, userord_item, &op, &orig_default,
Michal Vaskoe78faec2021-04-08 17:24:43 +0200846 &value, &orig_value, &key, &orig_key, &position, &orig_position);
Michal Vaskod59035b2020-07-08 12:00:06 +0200847
848 /* add into diff if there are any changes */
849 if (!ret) {
Michal Vaskoe78faec2021-04-08 17:24:43 +0200850 ret = lyd_diff_add(iter_second, op, orig_default, orig_value, key, value, position, orig_key,
851 orig_position, diff);
Michal Vaskod59035b2020-07-08 12:00:06 +0200852
853 free(orig_value);
854 free(key);
855 free(value);
Michal Vaskoe78faec2021-04-08 17:24:43 +0200856 free(position);
Michal Vaskod59035b2020-07-08 12:00:06 +0200857 free(orig_key);
Michal Vaskoe78faec2021-04-08 17:24:43 +0200858 free(orig_position);
Michal Vaskod59035b2020-07-08 12:00:06 +0200859 LY_CHECK_GOTO(ret, cleanup);
860 } else if (ret == LY_ENOT) {
861 ret = LY_SUCCESS;
862 } else {
863 goto cleanup;
864 }
865 } else if (!match_first) {
866 /* get all the attributes */
Michal Vaskoe78faec2021-04-08 17:24:43 +0200867 LY_CHECK_GOTO(ret = lyd_diff_attrs(match_first, iter_second, options, &op, &orig_default, &orig_value), cleanup);
Michal Vaskod59035b2020-07-08 12:00:06 +0200868
869 /* there must be changes, it is created */
870 assert(op == LYD_DIFF_OP_CREATE);
Michal Vaskoe78faec2021-04-08 17:24:43 +0200871 ret = lyd_diff_add(iter_second, op, orig_default, orig_value, NULL, NULL, NULL, NULL, NULL, diff);
Michal Vaskod59035b2020-07-08 12:00:06 +0200872
873 free(orig_value);
874 LY_CHECK_GOTO(ret, cleanup);
875 } /* else was handled */
876
877 if (nosiblings) {
878 break;
879 }
880 }
881
882cleanup:
Michal Vaskod7c048c2021-05-18 16:12:55 +0200883 lyd_dup_inst_free(dup_inst_first);
884 lyd_dup_inst_free(dup_inst_second);
Michal Vaskod59035b2020-07-08 12:00:06 +0200885 LY_ARRAY_FOR(userord, u) {
886 LY_ARRAY_FREE(userord[u].inst);
887 }
888 LY_ARRAY_FREE(userord);
889 return ret;
890}
891
Michal Vasko3a41dff2020-07-15 14:30:28 +0200892static LY_ERR
Michal Vasko55896172022-02-17 10:47:21 +0100893lyd_diff(const struct lyd_node *first, const struct lyd_node *second, uint16_t options, ly_bool nosiblings,
894 struct lyd_node **diff)
Michal Vaskod59035b2020-07-08 12:00:06 +0200895{
896 const struct ly_ctx *ctx;
897
898 LY_CHECK_ARG_RET(NULL, diff, LY_EINVAL);
899
900 if (first) {
Michal Vaskob7be7a82020-08-20 09:09:04 +0200901 ctx = LYD_CTX(first);
Michal Vaskod59035b2020-07-08 12:00:06 +0200902 } else if (second) {
Michal Vaskob7be7a82020-08-20 09:09:04 +0200903 ctx = LYD_CTX(second);
Michal Vaskod59035b2020-07-08 12:00:06 +0200904 } else {
905 ctx = NULL;
906 }
907
908 if (first && second && (lysc_data_parent(first->schema) != lysc_data_parent(second->schema))) {
909 LOGERR(ctx, LY_EINVAL, "Invalid arguments - cannot create diff for unrelated data (%s()).", __func__);
910 return LY_EINVAL;
911 }
912
913 *diff = NULL;
914
Michal Vasko3a41dff2020-07-15 14:30:28 +0200915 return lyd_diff_siblings_r(first, second, options, nosiblings, diff);
916}
917
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100918LIBYANG_API_DEF LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200919lyd_diff_tree(const struct lyd_node *first, const struct lyd_node *second, uint16_t options, struct lyd_node **diff)
Michal Vasko3a41dff2020-07-15 14:30:28 +0200920{
921 return lyd_diff(first, second, options, 1, diff);
922}
923
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100924LIBYANG_API_DEF LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200925lyd_diff_siblings(const struct lyd_node *first, const struct lyd_node *second, uint16_t options, struct lyd_node **diff)
Michal Vasko3a41dff2020-07-15 14:30:28 +0200926{
927 return lyd_diff(first, second, options, 0, diff);
Michal Vaskod59035b2020-07-08 12:00:06 +0200928}
929
930/**
Michal Vaskodb91fc32023-05-02 14:39:40 +0200931 * @brief Find metadata/an attribute of a node.
932 *
933 * @param[in] node Node to search.
934 * @param[in] name Metadata/attribute name.
935 * @param[out] meta Metadata found, NULL if not found.
936 * @param[out] attr Attribute found, NULL if not found.
937 */
938static void
939lyd_diff_find_meta(const struct lyd_node *node, const char *name, struct lyd_meta **meta, struct lyd_attr **attr)
940{
941 struct lyd_meta *m;
942 struct lyd_attr *a;
943
944 *meta = NULL;
945 *attr = NULL;
946
947 if (node->schema) {
948 LY_LIST_FOR(node->meta, m) {
949 if (!strcmp(m->name, name) && !strcmp(m->annotation->module->name, "yang")) {
950 *meta = m;
951 break;
952 }
953 }
954 } else {
955 LY_LIST_FOR(((struct lyd_node_opaq *)node)->attr, a) {
956 /* name */
957 if (strcmp(a->name.name, name)) {
958 continue;
959 }
960
961 /* module */
962 switch (a->format) {
963 case LY_VALUE_JSON:
964 if (strcmp(a->name.module_name, "yang")) {
965 continue;
966 }
967 break;
968 case LY_VALUE_XML:
969 if (strcmp(a->name.module_ns, "urn:ietf:params:xml:ns:yang:1")) {
970 continue;
971 }
972 break;
973 default:
974 LOGINT(LYD_CTX(node));
975 return;
976 }
977
978 *attr = a;
979 break;
980 }
981 }
982}
983
984/**
Michal Vaskod59035b2020-07-08 12:00:06 +0200985 * @brief Learn operation of a diff node.
986 *
987 * @param[in] diff_node Diff node.
988 * @param[out] op Operation.
Michal Vaskod59035b2020-07-08 12:00:06 +0200989 * @return LY_ERR value.
990 */
991static LY_ERR
Michal Vaskoe6323f62020-07-09 15:49:02 +0200992lyd_diff_get_op(const struct lyd_node *diff_node, enum lyd_diff_op *op)
Michal Vaskod59035b2020-07-08 12:00:06 +0200993{
994 struct lyd_meta *meta = NULL;
Michal Vaskodb91fc32023-05-02 14:39:40 +0200995 struct lyd_attr *attr = NULL;
Michal Vaskod59035b2020-07-08 12:00:06 +0200996 const struct lyd_node *diff_parent;
Michal Vaskoe6323f62020-07-09 15:49:02 +0200997 const char *str;
Michal Vasko52afd7d2022-01-18 14:08:34 +0100998 char *path;
Michal Vaskod59035b2020-07-08 12:00:06 +0200999
Michal Vasko9e685082021-01-29 14:49:09 +01001000 for (diff_parent = diff_node; diff_parent; diff_parent = lyd_parent(diff_parent)) {
Michal Vaskodb91fc32023-05-02 14:39:40 +02001001 lyd_diff_find_meta(diff_parent, "operation", &meta, &attr);
1002 if (!meta && !attr) {
1003 continue;
Michal Vaskod59035b2020-07-08 12:00:06 +02001004 }
Michal Vaskodb91fc32023-05-02 14:39:40 +02001005
1006 str = meta ? lyd_get_meta_value(meta) : attr->value;
1007 if ((str[0] == 'r') && (diff_parent != diff_node)) {
1008 /* we do not care about this operation if it's in our parent */
1009 continue;
Michal Vaskod59035b2020-07-08 12:00:06 +02001010 }
Michal Vaskodb91fc32023-05-02 14:39:40 +02001011 *op = lyd_diff_str2op(str);
1012 return LY_SUCCESS;
Michal Vaskod59035b2020-07-08 12:00:06 +02001013 }
Michal Vasko52afd7d2022-01-18 14:08:34 +01001014
Michal Vaskodb91fc32023-05-02 14:39:40 +02001015 /* operation not found */
1016 path = lyd_path(diff_node, LYD_PATH_STD, NULL, 0);
1017 LOGERR(LYD_CTX(diff_node), LY_EINVAL, "Node \"%s\" without an operation.", path);
1018 free(path);
1019 return LY_EINT;
Michal Vaskod59035b2020-07-08 12:00:06 +02001020}
1021
1022/**
1023 * @brief Insert a diff node into a data tree.
1024 *
1025 * @param[in,out] first_node First sibling of the data tree.
1026 * @param[in] parent_node Data tree sibling parent node.
1027 * @param[in] new_node Node to insert.
Michal Vaskoe78faec2021-04-08 17:24:43 +02001028 * @param[in] userord_anchor Optional anchor (key, value, or position) of relative (leaf-)list instance. If not set,
1029 * the user-ordered instance will be inserted at the first position.
Michal Vaskod59035b2020-07-08 12:00:06 +02001030 * @return err_info, NULL on success.
1031 */
1032static LY_ERR
1033lyd_diff_insert(struct lyd_node **first_node, struct lyd_node *parent_node, struct lyd_node *new_node,
Michal Vaskoe78faec2021-04-08 17:24:43 +02001034 const char *userord_anchor)
Michal Vaskod59035b2020-07-08 12:00:06 +02001035{
1036 LY_ERR ret;
1037 struct lyd_node *anchor;
Michal Vaskoe78faec2021-04-08 17:24:43 +02001038 uint32_t pos, anchor_pos;
1039 int found;
Michal Vaskod59035b2020-07-08 12:00:06 +02001040
1041 assert(new_node);
1042
1043 if (!*first_node) {
1044 if (!parent_node) {
1045 /* no parent or siblings */
1046 *first_node = new_node;
1047 return LY_SUCCESS;
1048 }
1049
1050 /* simply insert into parent, no other children */
Michal Vaskoe78faec2021-04-08 17:24:43 +02001051 if (userord_anchor) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02001052 LOGERR(LYD_CTX(new_node), LY_EINVAL, "Node \"%s\" instance to insert next to not found.",
Michal Vasko69730152020-10-09 16:30:07 +02001053 new_node->schema->name);
Michal Vaskod59035b2020-07-08 12:00:06 +02001054 return LY_EINVAL;
1055 }
Michal Vaskob104f112020-07-17 09:54:54 +02001056 return lyd_insert_child(parent_node, new_node);
Michal Vaskod59035b2020-07-08 12:00:06 +02001057 }
1058
Michal Vasko9e685082021-01-29 14:49:09 +01001059 assert(!(*first_node)->parent || (lyd_parent(*first_node) == parent_node));
Michal Vaskod59035b2020-07-08 12:00:06 +02001060
Michal Vaskod59035b2020-07-08 12:00:06 +02001061 if (!lysc_is_userordered(new_node->schema)) {
Michal Vaskob104f112020-07-17 09:54:54 +02001062 /* simple insert */
1063 return lyd_insert_sibling(*first_node, new_node, first_node);
Michal Vaskod59035b2020-07-08 12:00:06 +02001064 }
1065
Michal Vaskoe78faec2021-04-08 17:24:43 +02001066 if (userord_anchor) {
Michal Vaskod59035b2020-07-08 12:00:06 +02001067 /* find the anchor sibling */
Michal Vaskoe78faec2021-04-08 17:24:43 +02001068 if (lysc_is_dup_inst_list(new_node->schema)) {
1069 anchor_pos = atoi(userord_anchor);
Michal Vasko0ff97752022-01-18 16:35:41 +01001070 if (!anchor_pos) {
1071 LOGERR(LYD_CTX(new_node), LY_EINVAL, "Invalid user-ordered anchor value \"%s\".", userord_anchor);
1072 return LY_EINVAL;
1073 }
Michal Vaskoe78faec2021-04-08 17:24:43 +02001074
1075 found = 0;
1076 pos = 1;
1077 LYD_LIST_FOR_INST(*first_node, new_node->schema, anchor) {
1078 if (pos == anchor_pos) {
1079 found = 1;
1080 break;
1081 }
1082 ++pos;
1083 }
1084 if (!found) {
1085 LOGERR(LYD_CTX(new_node), LY_EINVAL, "Node \"%s\" instance to insert next to not found.",
1086 new_node->schema->name);
1087 return LY_EINVAL;
1088 }
1089 } else {
1090 ret = lyd_find_sibling_val(*first_node, new_node->schema, userord_anchor, 0, &anchor);
1091 if (ret == LY_ENOTFOUND) {
1092 LOGERR(LYD_CTX(new_node), LY_EINVAL, "Node \"%s\" instance to insert next to not found.",
1093 new_node->schema->name);
1094 return LY_EINVAL;
1095 } else if (ret) {
1096 return ret;
1097 }
Michal Vaskod59035b2020-07-08 12:00:06 +02001098 }
1099
1100 /* insert after */
1101 LY_CHECK_RET(lyd_insert_after(anchor, new_node));
1102 assert(new_node->prev == anchor);
1103 if (*first_node == new_node) {
1104 *first_node = anchor;
1105 }
1106 } else {
Michal Vaskoea7d3232022-04-19 12:01:36 +02001107 /* find the first instance */
1108 ret = lyd_find_sibling_val(*first_node, new_node->schema, NULL, 0, &anchor);
1109 LY_CHECK_RET(ret && (ret != LY_ENOTFOUND), ret);
Michal Vaskod59035b2020-07-08 12:00:06 +02001110
Michal Vaskoea7d3232022-04-19 12:01:36 +02001111 if (anchor) {
1112 /* insert before the first instance */
1113 LY_CHECK_RET(lyd_insert_before(anchor, new_node));
1114 if ((*first_node)->prev->next) {
1115 assert(!new_node->prev->next);
1116 *first_node = new_node;
Michal Vaskod59035b2020-07-08 12:00:06 +02001117 }
Michal Vaskod59035b2020-07-08 12:00:06 +02001118 } else {
Michal Vaskoea7d3232022-04-19 12:01:36 +02001119 /* insert anywhere */
1120 LY_CHECK_RET(lyd_insert_sibling(*first_node, new_node, first_node));
Michal Vaskod59035b2020-07-08 12:00:06 +02001121 }
1122 }
1123
1124 return LY_SUCCESS;
1125}
1126
1127/**
1128 * @brief Apply diff subtree on data tree nodes, recursively.
1129 *
1130 * @param[in,out] first_node First sibling of the data tree.
1131 * @param[in] parent_node Parent of the first sibling.
1132 * @param[in] diff_node Current diff node.
Michal Vaskoe6323f62020-07-09 15:49:02 +02001133 * @param[in] diff_cb Optional diff callback.
1134 * @param[in] cb_data User data for @p diff_cb.
Michal Vaskoe78faec2021-04-08 17:24:43 +02001135 * @param[in,out] dup_inst Duplicate instance cache for all @p diff_node siblings.
Michal Vaskod59035b2020-07-08 12:00:06 +02001136 * @return LY_ERR value.
1137 */
1138static LY_ERR
1139lyd_diff_apply_r(struct lyd_node **first_node, struct lyd_node *parent_node, const struct lyd_node *diff_node,
Michal Vasko8efac242023-03-30 08:24:56 +02001140 lyd_diff_cb diff_cb, void *cb_data, struct ly_ht **dup_inst)
Michal Vaskod59035b2020-07-08 12:00:06 +02001141{
1142 LY_ERR ret;
1143 struct lyd_node *match, *diff_child;
Michal Vaskoe78faec2021-04-08 17:24:43 +02001144 const char *str_val, *meta_str;
Michal Vaskoe6323f62020-07-09 15:49:02 +02001145 enum lyd_diff_op op;
1146 struct lyd_meta *meta;
Michal Vasko8efac242023-03-30 08:24:56 +02001147 struct ly_ht *child_dup_inst = NULL;
Michal Vaskob7be7a82020-08-20 09:09:04 +02001148 const struct ly_ctx *ctx = LYD_CTX(diff_node);
Michal Vaskod59035b2020-07-08 12:00:06 +02001149
1150 /* read all the valid attributes */
Michal Vaskoe6323f62020-07-09 15:49:02 +02001151 LY_CHECK_RET(lyd_diff_get_op(diff_node, &op));
Michal Vaskod59035b2020-07-08 12:00:06 +02001152
Michal Vaskoe6323f62020-07-09 15:49:02 +02001153 /* handle specific user-ordered (leaf-)lists operations separately */
1154 if (lysc_is_userordered(diff_node->schema) && ((op == LYD_DIFF_OP_CREATE) || (op == LYD_DIFF_OP_REPLACE))) {
1155 if (op == LYD_DIFF_OP_REPLACE) {
Michal Vaskod59035b2020-07-08 12:00:06 +02001156 /* find the node (we must have some siblings because the node was only moved) */
Michal Vaskoe78faec2021-04-08 17:24:43 +02001157 LY_CHECK_RET(lyd_diff_find_match(*first_node, diff_node, 1, dup_inst, &match));
Michal Vasko52afd7d2022-01-18 14:08:34 +01001158 LY_CHECK_ERR_RET(!match, LOGERR_NOINST(ctx, diff_node), LY_EINVAL);
Michal Vaskod59035b2020-07-08 12:00:06 +02001159 } else {
Michal Vasko3a41dff2020-07-15 14:30:28 +02001160 /* duplicate the node */
1161 LY_CHECK_RET(lyd_dup_single(diff_node, NULL, LYD_DUP_NO_META, &match));
Michal Vaskod59035b2020-07-08 12:00:06 +02001162 }
1163
Michal Vaskoe78faec2021-04-08 17:24:43 +02001164 /* get "key", "value", or "position" metadata string value */
1165 if (lysc_is_dup_inst_list(diff_node->schema)) {
1166 meta_str = "yang:position";
1167 } else if (diff_node->schema->nodetype == LYS_LIST) {
1168 meta_str = "yang:key";
1169 } else {
1170 meta_str = "yang:value";
1171 }
1172 meta = lyd_find_meta(diff_node->meta, NULL, meta_str);
Michal Vasko52afd7d2022-01-18 14:08:34 +01001173 LY_CHECK_ERR_RET(!meta, LOGERR_META(ctx, meta_str, diff_node), LY_EINVAL);
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02001174 str_val = lyd_get_meta_value(meta);
Michal Vaskoe6323f62020-07-09 15:49:02 +02001175
Michal Vaskod59035b2020-07-08 12:00:06 +02001176 /* insert/move the node */
Michal Vaskoe6323f62020-07-09 15:49:02 +02001177 if (str_val[0]) {
1178 ret = lyd_diff_insert(first_node, parent_node, match, str_val);
Michal Vaskod59035b2020-07-08 12:00:06 +02001179 } else {
1180 ret = lyd_diff_insert(first_node, parent_node, match, NULL);
1181 }
1182 if (ret) {
Michal Vaskoe6323f62020-07-09 15:49:02 +02001183 if (op == LYD_DIFF_OP_CREATE) {
Michal Vaskod59035b2020-07-08 12:00:06 +02001184 lyd_free_tree(match);
1185 }
1186 return ret;
1187 }
1188
1189 goto next_iter_r;
1190 }
1191
1192 /* apply operation */
Michal Vaskoe6323f62020-07-09 15:49:02 +02001193 switch (op) {
1194 case LYD_DIFF_OP_NONE:
Michal Vaskod59035b2020-07-08 12:00:06 +02001195 /* find the node */
Michal Vaskoe78faec2021-04-08 17:24:43 +02001196 LY_CHECK_RET(lyd_diff_find_match(*first_node, diff_node, 1, dup_inst, &match));
Michal Vasko52afd7d2022-01-18 14:08:34 +01001197 LY_CHECK_ERR_RET(!match, LOGERR_NOINST(ctx, diff_node), LY_EINVAL);
Michal Vaskod59035b2020-07-08 12:00:06 +02001198
1199 if (match->schema->nodetype & LYD_NODE_TERM) {
1200 /* special case of only dflt flag change */
1201 if (diff_node->flags & LYD_DEFAULT) {
1202 match->flags |= LYD_DEFAULT;
1203 } else {
1204 match->flags &= ~LYD_DEFAULT;
1205 }
1206 } else {
1207 /* none operation on nodes without children is redundant and hence forbidden */
Radek Krejcia1c1e542020-09-29 16:06:52 +02001208 if (!lyd_child_no_keys(diff_node)) {
Michal Vasko0ff97752022-01-18 16:35:41 +01001209 LOGERR(ctx, LY_EINVAL, "Operation \"none\" is invalid for node \"%s\" without children.",
1210 LYD_NAME(diff_node));
1211 return LY_EINVAL;
Michal Vaskod59035b2020-07-08 12:00:06 +02001212 }
1213 }
1214 break;
Michal Vaskoe6323f62020-07-09 15:49:02 +02001215 case LYD_DIFF_OP_CREATE:
Michal Vaskod59035b2020-07-08 12:00:06 +02001216 /* duplicate the node */
Michal Vasko3a41dff2020-07-15 14:30:28 +02001217 LY_CHECK_RET(lyd_dup_single(diff_node, NULL, LYD_DUP_NO_META, &match));
Michal Vaskod59035b2020-07-08 12:00:06 +02001218
1219 /* insert it at the end */
1220 ret = 0;
Michal Vaskob104f112020-07-17 09:54:54 +02001221 if (parent_node) {
Michal Vasko19175b62022-04-01 09:17:07 +02001222 if (match->flags & LYD_EXT) {
Michal Vasko193dacd2022-10-13 08:43:05 +02001223 ret = lyplg_ext_insert(parent_node, match);
Michal Vasko19175b62022-04-01 09:17:07 +02001224 } else {
1225 ret = lyd_insert_child(parent_node, match);
1226 }
Michal Vaskod59035b2020-07-08 12:00:06 +02001227 } else {
Michal Vaskob104f112020-07-17 09:54:54 +02001228 ret = lyd_insert_sibling(*first_node, match, first_node);
Michal Vaskod59035b2020-07-08 12:00:06 +02001229 }
1230 if (ret) {
1231 lyd_free_tree(match);
1232 return ret;
1233 }
1234
1235 break;
Michal Vaskoe6323f62020-07-09 15:49:02 +02001236 case LYD_DIFF_OP_DELETE:
Michal Vaskod59035b2020-07-08 12:00:06 +02001237 /* find the node */
Michal Vaskoe78faec2021-04-08 17:24:43 +02001238 LY_CHECK_RET(lyd_diff_find_match(*first_node, diff_node, 1, dup_inst, &match));
Michal Vasko52afd7d2022-01-18 14:08:34 +01001239 LY_CHECK_ERR_RET(!match, LOGERR_NOINST(ctx, diff_node), LY_EINVAL);
Michal Vaskod59035b2020-07-08 12:00:06 +02001240
1241 /* remove it */
1242 if ((match == *first_node) && !match->parent) {
1243 assert(!parent_node);
1244 /* we have removed the top-level node */
1245 *first_node = (*first_node)->next;
1246 }
1247 lyd_free_tree(match);
1248
1249 /* we are not going recursively in this case, the whole subtree was already deleted */
1250 return LY_SUCCESS;
Michal Vaskoe6323f62020-07-09 15:49:02 +02001251 case LYD_DIFF_OP_REPLACE:
Michal Vasko0ff97752022-01-18 16:35:41 +01001252 if (!(diff_node->schema->nodetype & (LYS_LEAF | LYS_ANYDATA))) {
1253 LOGERR(ctx, LY_EINVAL, "Operation \"replace\" is invalid for %s node \"%s\".",
1254 lys_nodetype2str(diff_node->schema->nodetype), LYD_NAME(diff_node));
1255 return LY_EINVAL;
1256 }
Michal Vaskod59035b2020-07-08 12:00:06 +02001257
1258 /* find the node */
Michal Vaskoe78faec2021-04-08 17:24:43 +02001259 LY_CHECK_RET(lyd_diff_find_match(*first_node, diff_node, 1, dup_inst, &match));
Michal Vasko52afd7d2022-01-18 14:08:34 +01001260 LY_CHECK_ERR_RET(!match, LOGERR_NOINST(ctx, diff_node), LY_EINVAL);
Michal Vaskod59035b2020-07-08 12:00:06 +02001261
Michal Vaskobaba84e2021-02-05 16:33:30 +01001262 /* update the value */
1263 if (diff_node->schema->nodetype == LYS_LEAF) {
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02001264 ret = lyd_change_term(match, lyd_get_value(diff_node));
Michal Vasko52afd7d2022-01-18 14:08:34 +01001265 LY_CHECK_ERR_RET(ret && (ret != LY_EEXIST), LOGERR_UNEXPVAL(ctx, match, "data"), LY_EINVAL);
Michal Vaskobaba84e2021-02-05 16:33:30 +01001266 } else {
1267 struct lyd_node_any *any = (struct lyd_node_any *)diff_node;
Michal Vasko26bbb272022-08-02 14:54:33 +02001268
Michal Vaskoe78faec2021-04-08 17:24:43 +02001269 LY_CHECK_RET(lyd_any_copy_value(match, &any->value, any->value_type));
Michal Vaskod59035b2020-07-08 12:00:06 +02001270 }
1271
1272 /* with flags */
1273 match->flags = diff_node->flags;
1274 break;
1275 default:
1276 LOGINT_RET(ctx);
1277 }
1278
1279next_iter_r:
1280 if (diff_cb) {
1281 /* call callback */
1282 LY_CHECK_RET(diff_cb(diff_node, match, cb_data));
1283 }
1284
1285 /* apply diff recursively */
Michal Vaskoe78faec2021-04-08 17:24:43 +02001286 ret = LY_SUCCESS;
Radek Krejcia1c1e542020-09-29 16:06:52 +02001287 LY_LIST_FOR(lyd_child_no_keys(diff_node), diff_child) {
Michal Vaskoe78faec2021-04-08 17:24:43 +02001288 ret = lyd_diff_apply_r(lyd_node_child_p(match), match, diff_child, diff_cb, cb_data, &child_dup_inst);
1289 if (ret) {
1290 break;
1291 }
Michal Vaskod59035b2020-07-08 12:00:06 +02001292 }
1293
Michal Vaskod7c048c2021-05-18 16:12:55 +02001294 lyd_dup_inst_free(child_dup_inst);
Michal Vaskoe78faec2021-04-08 17:24:43 +02001295 return ret;
Michal Vaskod59035b2020-07-08 12:00:06 +02001296}
1297
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001298LIBYANG_API_DEF LY_ERR
Michal Vaskod59035b2020-07-08 12:00:06 +02001299lyd_diff_apply_module(struct lyd_node **data, const struct lyd_node *diff, const struct lys_module *mod,
Radek Krejci0f969882020-08-21 16:56:47 +02001300 lyd_diff_cb diff_cb, void *cb_data)
Michal Vaskod59035b2020-07-08 12:00:06 +02001301{
1302 const struct lyd_node *root;
Michal Vasko8efac242023-03-30 08:24:56 +02001303 struct ly_ht *dup_inst = NULL;
Michal Vaskoe78faec2021-04-08 17:24:43 +02001304 LY_ERR ret = LY_SUCCESS;
Michal Vaskod59035b2020-07-08 12:00:06 +02001305
1306 LY_LIST_FOR(diff, root) {
1307 if (mod && (lyd_owner_module(root) != mod)) {
1308 /* skip data nodes from different modules */
1309 continue;
1310 }
1311
1312 /* apply relevant nodes from the diff datatree */
Michal Vaskoe78faec2021-04-08 17:24:43 +02001313 ret = lyd_diff_apply_r(data, NULL, root, diff_cb, cb_data, &dup_inst);
1314 if (ret) {
1315 break;
1316 }
Michal Vaskod59035b2020-07-08 12:00:06 +02001317 }
1318
Michal Vaskod7c048c2021-05-18 16:12:55 +02001319 lyd_dup_inst_free(dup_inst);
Michal Vaskoe78faec2021-04-08 17:24:43 +02001320 return ret;
Michal Vaskod59035b2020-07-08 12:00:06 +02001321}
1322
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001323LIBYANG_API_DEF LY_ERR
Michal Vasko3a41dff2020-07-15 14:30:28 +02001324lyd_diff_apply_all(struct lyd_node **data, const struct lyd_node *diff)
Michal Vaskod59035b2020-07-08 12:00:06 +02001325{
1326 return lyd_diff_apply_module(data, diff, NULL, NULL, NULL);
1327}
Michal Vaskoe6323f62020-07-09 15:49:02 +02001328
1329/**
1330 * @brief Update operations on a diff node when the new operation is NONE.
1331 *
1332 * @param[in] diff_match Node from the diff.
Michal Vaskoe78faec2021-04-08 17:24:43 +02001333 * @param[in] cur_op Current operation of @p diff_match.
Michal Vaskoe6323f62020-07-09 15:49:02 +02001334 * @param[in] src_diff Current source diff node.
1335 * @return LY_ERR value.
1336 */
1337static LY_ERR
1338lyd_diff_merge_none(struct lyd_node *diff_match, enum lyd_diff_op cur_op, const struct lyd_node *src_diff)
1339{
1340 switch (cur_op) {
1341 case LYD_DIFF_OP_NONE:
1342 case LYD_DIFF_OP_CREATE:
1343 case LYD_DIFF_OP_REPLACE:
1344 if (src_diff->schema->nodetype & LYD_NODE_TERM) {
1345 /* NONE on a term means only its dflt flag was changed */
1346 diff_match->flags &= ~LYD_DEFAULT;
1347 diff_match->flags |= src_diff->flags & LYD_DEFAULT;
1348 }
1349 break;
1350 default:
1351 /* delete operation is not valid */
Michal Vasko52afd7d2022-01-18 14:08:34 +01001352 LOGERR_MERGEOP(LYD_CTX(diff_match), diff_match, cur_op, LYD_DIFF_OP_NONE);
1353 return LY_EINVAL;
Michal Vaskoe6323f62020-07-09 15:49:02 +02001354 }
1355
1356 return LY_SUCCESS;
1357}
1358
1359/**
Michal Vaskodb91fc32023-05-02 14:39:40 +02001360 * @brief Remove metadata/an attribute from a node.
Michal Vaskoe6323f62020-07-09 15:49:02 +02001361 *
Michal Vaskodb91fc32023-05-02 14:39:40 +02001362 * @param[in] node Node to update.
1363 * @param[in] name Metadata/attribute name.
Michal Vaskoe6323f62020-07-09 15:49:02 +02001364 */
1365static void
1366lyd_diff_del_meta(struct lyd_node *node, const char *name)
1367{
1368 struct lyd_meta *meta;
Michal Vaskodb91fc32023-05-02 14:39:40 +02001369 struct lyd_attr *attr;
Michal Vaskoe6323f62020-07-09 15:49:02 +02001370
Michal Vaskodb91fc32023-05-02 14:39:40 +02001371 lyd_diff_find_meta(node, name, &meta, &attr);
1372
1373 if (meta) {
1374 lyd_free_meta_single(meta);
1375 } else if (attr) {
1376 lyd_free_attr_single(LYD_CTX(node), attr);
Michal Vaskoe6323f62020-07-09 15:49:02 +02001377 }
Michal Vaskoe6323f62020-07-09 15:49:02 +02001378}
1379
1380/**
1381 * @brief Set a specific operation of a node. Delete the previous operation, if any.
Michal Vasko871a0252020-11-11 18:35:24 +01001382 * Does not change the default flag.
Michal Vaskoe6323f62020-07-09 15:49:02 +02001383 *
1384 * @param[in] node Node to change.
1385 * @param[in] op Operation to set.
1386 * @return LY_ERR value.
1387 */
1388static LY_ERR
1389lyd_diff_change_op(struct lyd_node *node, enum lyd_diff_op op)
1390{
Michal Vaskodb91fc32023-05-02 14:39:40 +02001391 lyd_diff_del_meta(node, "operation");
Michal Vaskoe6323f62020-07-09 15:49:02 +02001392
Michal Vaskodb91fc32023-05-02 14:39:40 +02001393 if (node->schema) {
1394 return lyd_new_meta(LYD_CTX(node), node, NULL, "yang:operation", lyd_diff_op2str(op), 0, NULL);
1395 } else {
1396 return lyd_new_attr(node, "yang", "operation", lyd_diff_op2str(op), NULL);
Michal Vaskoe6323f62020-07-09 15:49:02 +02001397 }
Michal Vaskoe6323f62020-07-09 15:49:02 +02001398}
1399
1400/**
1401 * @brief Update operations on a diff node when the new operation is REPLACE.
1402 *
1403 * @param[in] diff_match Node from the diff.
Michal Vaskoe78faec2021-04-08 17:24:43 +02001404 * @param[in] cur_op Current operation of @p diff_match.
Michal Vaskoe6323f62020-07-09 15:49:02 +02001405 * @param[in] src_diff Current source diff node.
1406 * @return LY_ERR value.
1407 */
1408static LY_ERR
1409lyd_diff_merge_replace(struct lyd_node *diff_match, enum lyd_diff_op cur_op, const struct lyd_node *src_diff)
1410{
1411 LY_ERR ret;
Michal Vaskoe78faec2021-04-08 17:24:43 +02001412 const char *str_val, *meta_name, *orig_meta_name;
Michal Vaskoe6323f62020-07-09 15:49:02 +02001413 struct lyd_meta *meta;
1414 const struct lys_module *mod;
1415 const struct lyd_node_any *any;
Michal Vasko52afd7d2022-01-18 14:08:34 +01001416 const struct ly_ctx *ctx = LYD_CTX(diff_match);
Michal Vaskoe6323f62020-07-09 15:49:02 +02001417
1418 /* get "yang" module for the metadata */
Michal Vaskob7be7a82020-08-20 09:09:04 +02001419 mod = ly_ctx_get_module_latest(LYD_CTX(diff_match), "yang");
Michal Vaskoe6323f62020-07-09 15:49:02 +02001420 assert(mod);
1421
1422 switch (cur_op) {
1423 case LYD_DIFF_OP_REPLACE:
1424 case LYD_DIFF_OP_CREATE:
1425 switch (diff_match->schema->nodetype) {
1426 case LYS_LIST:
1427 case LYS_LEAFLIST:
Michal Vasko4231fb62020-07-13 13:54:47 +02001428 /* it was created/moved somewhere, but now it will be created/moved somewhere else,
Michal Vaskoe6323f62020-07-09 15:49:02 +02001429 * keep orig_key/orig_value (only replace oper) and replace key/value */
1430 assert(lysc_is_userordered(diff_match->schema));
Michal Vaskoe78faec2021-04-08 17:24:43 +02001431 if (lysc_is_dup_inst_list(diff_match->schema)) {
1432 meta_name = "position";
1433 } else if (diff_match->schema->nodetype == LYS_LIST) {
1434 meta_name = "key";
1435 } else {
1436 meta_name = "value";
1437 }
Michal Vaskoe6323f62020-07-09 15:49:02 +02001438
1439 lyd_diff_del_meta(diff_match, meta_name);
1440 meta = lyd_find_meta(src_diff->meta, mod, meta_name);
Michal Vasko52afd7d2022-01-18 14:08:34 +01001441 LY_CHECK_ERR_RET(!meta, LOGERR_META(ctx, meta_name, src_diff), LY_EINVAL);
Michal Vasko3a41dff2020-07-15 14:30:28 +02001442 LY_CHECK_RET(lyd_dup_meta_single(meta, diff_match, NULL));
Michal Vaskoe6323f62020-07-09 15:49:02 +02001443 break;
1444 case LYS_LEAF:
1445 /* replaced with the exact same value, impossible */
Michal Vasko8f359bf2020-07-28 10:41:15 +02001446 if (!lyd_compare_single(diff_match, src_diff, 0)) {
Michal Vasko52afd7d2022-01-18 14:08:34 +01001447 LOGERR_UNEXPVAL(ctx, diff_match, "target diff");
1448 return LY_EINVAL;
Michal Vaskoe6323f62020-07-09 15:49:02 +02001449 }
1450
Michal Vaskoe6323f62020-07-09 15:49:02 +02001451 /* modify the node value */
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02001452 if (lyd_change_term(diff_match, lyd_get_value(src_diff))) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02001453 LOGINT_RET(LYD_CTX(src_diff));
Michal Vaskoe6323f62020-07-09 15:49:02 +02001454 }
1455
Michal Vasko8caadab2020-11-05 17:38:15 +01001456 if (cur_op == LYD_DIFF_OP_REPLACE) {
1457 /* compare values whether there is any change at all */
1458 meta = lyd_find_meta(diff_match->meta, mod, "orig-value");
Michal Vasko52afd7d2022-01-18 14:08:34 +01001459 LY_CHECK_ERR_RET(!meta, LOGERR_META(ctx, "orig-value", diff_match), LY_EINVAL);
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02001460 str_val = lyd_get_meta_value(meta);
Michal Vasko8caadab2020-11-05 17:38:15 +01001461 ret = lyd_value_compare((struct lyd_node_term *)diff_match, str_val, strlen(str_val));
1462 if (!ret) {
1463 /* values are the same, remove orig-value meta and set oper to NONE */
1464 lyd_free_meta_single(meta);
1465 LY_CHECK_RET(lyd_diff_change_op(diff_match, LYD_DIFF_OP_NONE));
1466 }
Michal Vaskoe6323f62020-07-09 15:49:02 +02001467 }
1468
1469 /* modify the default flag */
1470 diff_match->flags &= ~LYD_DEFAULT;
1471 diff_match->flags |= src_diff->flags & LYD_DEFAULT;
1472 break;
1473 case LYS_ANYXML:
1474 case LYS_ANYDATA:
Michal Vasko8f359bf2020-07-28 10:41:15 +02001475 if (!lyd_compare_single(diff_match, src_diff, 0)) {
Michal Vasko52afd7d2022-01-18 14:08:34 +01001476 LOGERR_UNEXPVAL(ctx, diff_match, "target diff");
1477 return LY_EINVAL;
Michal Vaskoe6323f62020-07-09 15:49:02 +02001478 }
1479
1480 /* modify the node value */
1481 any = (struct lyd_node_any *)src_diff;
1482 LY_CHECK_RET(lyd_any_copy_value(diff_match, &any->value, any->value_type));
1483 break;
1484 default:
Michal Vaskob7be7a82020-08-20 09:09:04 +02001485 LOGINT_RET(LYD_CTX(src_diff));
Michal Vaskoe6323f62020-07-09 15:49:02 +02001486 }
1487 break;
1488 case LYD_DIFF_OP_NONE:
1489 /* it is moved now */
1490 assert(lysc_is_userordered(diff_match->schema) && (diff_match->schema->nodetype == LYS_LIST));
1491
1492 /* change the operation */
1493 LY_CHECK_RET(lyd_diff_change_op(diff_match, LYD_DIFF_OP_REPLACE));
1494
Michal Vaskoe78faec2021-04-08 17:24:43 +02001495 /* set orig-meta and meta */
1496 if (lysc_is_dup_inst_list(diff_match->schema)) {
1497 meta_name = "position";
1498 orig_meta_name = "orig-position";
1499 } else {
1500 meta_name = "key";
1501 orig_meta_name = "orig-key";
1502 }
1503
1504 meta = lyd_find_meta(src_diff->meta, mod, orig_meta_name);
Michal Vasko52afd7d2022-01-18 14:08:34 +01001505 LY_CHECK_ERR_RET(!meta, LOGERR_META(ctx, orig_meta_name, src_diff), LY_EINVAL);
Michal Vasko3a41dff2020-07-15 14:30:28 +02001506 LY_CHECK_RET(lyd_dup_meta_single(meta, diff_match, NULL));
Michal Vaskoe6323f62020-07-09 15:49:02 +02001507
Michal Vaskoe78faec2021-04-08 17:24:43 +02001508 meta = lyd_find_meta(src_diff->meta, mod, meta_name);
Michal Vasko52afd7d2022-01-18 14:08:34 +01001509 LY_CHECK_ERR_RET(!meta, LOGERR_META(ctx, meta_name, src_diff), LY_EINVAL);
Michal Vasko3a41dff2020-07-15 14:30:28 +02001510 LY_CHECK_RET(lyd_dup_meta_single(meta, diff_match, NULL));
Michal Vaskoe6323f62020-07-09 15:49:02 +02001511 break;
1512 default:
1513 /* delete operation is not valid */
Michal Vasko52afd7d2022-01-18 14:08:34 +01001514 LOGERR_MERGEOP(ctx, diff_match, cur_op, LYD_DIFF_OP_REPLACE);
1515 return LY_EINVAL;
Michal Vaskoe6323f62020-07-09 15:49:02 +02001516 }
1517
1518 return LY_SUCCESS;
1519}
1520
1521/**
1522 * @brief Update operations in a diff node when the new operation is CREATE.
1523 *
1524 * @param[in] diff_match Node from the diff.
Michal Vaskoe78faec2021-04-08 17:24:43 +02001525 * @param[in] cur_op Current operation of @p diff_match.
Michal Vaskoe6323f62020-07-09 15:49:02 +02001526 * @param[in] src_diff Current source diff node.
Michal Vaskoc0e58e82020-11-11 19:04:33 +01001527 * @param[in] options Diff merge options.
Michal Vaskoe6323f62020-07-09 15:49:02 +02001528 * @return LY_ERR value.
1529 */
1530static LY_ERR
Michal Vaskoc0e58e82020-11-11 19:04:33 +01001531lyd_diff_merge_create(struct lyd_node *diff_match, enum lyd_diff_op cur_op, const struct lyd_node *src_diff, uint16_t options)
Michal Vaskoe6323f62020-07-09 15:49:02 +02001532{
1533 struct lyd_node *child;
Michal Vaskoc0e58e82020-11-11 19:04:33 +01001534 const struct lysc_node_leaf *sleaf = NULL;
Michal Vasko871a0252020-11-11 18:35:24 +01001535 uint32_t trg_flags;
Michal Vaskoe78faec2021-04-08 17:24:43 +02001536 const char *meta_name, *orig_meta_name;
1537 struct lyd_meta *meta, *orig_meta;
Michal Vasko52afd7d2022-01-18 14:08:34 +01001538 const struct ly_ctx *ctx = LYD_CTX(diff_match);
Michal Vaskoe6323f62020-07-09 15:49:02 +02001539
1540 switch (cur_op) {
1541 case LYD_DIFF_OP_DELETE:
Michal Vasko871a0252020-11-11 18:35:24 +01001542 /* remember current flags */
1543 trg_flags = diff_match->flags;
1544
Michal Vaskoe78faec2021-04-08 17:24:43 +02001545 if (lysc_is_userordered(diff_match->schema)) {
1546 /* get anchor metadata */
1547 if (lysc_is_dup_inst_list(diff_match->schema)) {
1548 meta_name = "yang:position";
1549 orig_meta_name = "yang:orig-position";
1550 } else if (diff_match->schema->nodetype == LYS_LIST) {
1551 meta_name = "yang:key";
1552 orig_meta_name = "yang:orig-key";
1553 } else {
1554 meta_name = "yang:value";
1555 orig_meta_name = "yang:orig-value";
1556 }
1557 meta = lyd_find_meta(src_diff->meta, NULL, meta_name);
Michal Vasko52afd7d2022-01-18 14:08:34 +01001558 LY_CHECK_ERR_RET(!meta, LOGERR_META(ctx, meta_name, src_diff), LY_EINVAL);
Michal Vaskoe78faec2021-04-08 17:24:43 +02001559 orig_meta = lyd_find_meta(diff_match->meta, NULL, orig_meta_name);
Michal Vasko52afd7d2022-01-18 14:08:34 +01001560 LY_CHECK_ERR_RET(!orig_meta, LOGERR_META(ctx, orig_meta_name, diff_match), LY_EINVAL);
Michal Vaskoe78faec2021-04-08 17:24:43 +02001561
1562 /* the (incorrect) assumption made here is that there are no previous diff nodes that would affect
1563 * the anchors stored in the metadata */
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02001564 if (strcmp(lyd_get_meta_value(meta), lyd_get_meta_value(orig_meta))) {
Michal Vaskoe78faec2021-04-08 17:24:43 +02001565 /* deleted + created at another position -> operation REPLACE */
1566 LY_CHECK_RET(lyd_diff_change_op(diff_match, LYD_DIFF_OP_REPLACE));
1567
1568 /* add anchor metadata */
1569 LY_CHECK_RET(lyd_dup_meta_single(meta, diff_match, NULL));
1570 } else {
1571 /* deleted + created at the same position -> operation NONE */
1572 LY_CHECK_RET(lyd_diff_change_op(diff_match, LYD_DIFF_OP_NONE));
1573
1574 /* delete anchor metadata */
1575 lyd_free_meta_single(orig_meta);
1576 }
1577 } else if (diff_match->schema->nodetype == LYS_LEAF) {
1578 if (options & LYD_DIFF_MERGE_DEFAULTS) {
1579 /* we are dealing with a leaf and are handling default values specially (as explicit nodes) */
1580 sleaf = (struct lysc_node_leaf *)diff_match->schema;
1581 }
1582
Radek Krejci55c4bd22021-04-26 08:09:04 +02001583 if (sleaf && sleaf->dflt && !sleaf->dflt->realtype->plugin->compare(sleaf->dflt,
1584 &((struct lyd_node_term *)src_diff)->value)) {
Michal Vaskoe78faec2021-04-08 17:24:43 +02001585 /* we deleted it, so a default value was in-use, and it matches the created value -> operation NONE */
1586 LY_CHECK_RET(lyd_diff_change_op(diff_match, LYD_DIFF_OP_NONE));
1587 } else if (!lyd_compare_single(diff_match, src_diff, 0)) {
1588 /* deleted + created -> operation NONE */
1589 LY_CHECK_RET(lyd_diff_change_op(diff_match, LYD_DIFF_OP_NONE));
1590 } else {
1591 /* we deleted it, but it was created with a different value -> operation REPLACE */
1592 LY_CHECK_RET(lyd_diff_change_op(diff_match, LYD_DIFF_OP_REPLACE));
1593
1594 /* current value is the previous one (meta) */
1595 LY_CHECK_RET(lyd_new_meta(LYD_CTX(src_diff), diff_match, NULL, "yang:orig-value",
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02001596 lyd_get_value(diff_match), 0, NULL));
Michal Vaskoe78faec2021-04-08 17:24:43 +02001597
1598 /* update the value itself */
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02001599 LY_CHECK_RET(lyd_change_term(diff_match, lyd_get_value(src_diff)));
Michal Vaskoe78faec2021-04-08 17:24:43 +02001600 }
1601 } else {
Michal Vaskoe6323f62020-07-09 15:49:02 +02001602 /* deleted + created -> operation NONE */
1603 LY_CHECK_RET(lyd_diff_change_op(diff_match, LYD_DIFF_OP_NONE));
Michal Vaskoe6323f62020-07-09 15:49:02 +02001604 }
1605
1606 if (diff_match->schema->nodetype & LYD_NODE_TERM) {
Michal Vasko4b715ca2020-11-11 18:39:57 +01001607 /* add orig-dflt metadata */
1608 LY_CHECK_RET(lyd_new_meta(LYD_CTX(src_diff), diff_match, NULL, "yang:orig-default",
1609 trg_flags & LYD_DEFAULT ? "true" : "false", 0, NULL));
1610
Michal Vaskoe6323f62020-07-09 15:49:02 +02001611 /* update dflt flag itself */
1612 diff_match->flags &= ~LYD_DEFAULT;
1613 diff_match->flags |= src_diff->flags & LYD_DEFAULT;
Michal Vaskoe78faec2021-04-08 17:24:43 +02001614 }
1615
1616 /* but the operation of its children should remain DELETE */
1617 LY_LIST_FOR(lyd_child_no_keys(diff_match), child) {
1618 LY_CHECK_RET(lyd_diff_change_op(child, LYD_DIFF_OP_DELETE));
Michal Vaskoe6323f62020-07-09 15:49:02 +02001619 }
1620 break;
1621 default:
1622 /* create and replace operations are not valid */
Michal Vasko52afd7d2022-01-18 14:08:34 +01001623 LOGERR_MERGEOP(LYD_CTX(src_diff), diff_match, cur_op, LYD_DIFF_OP_CREATE);
1624 return LY_EINVAL;
Michal Vaskoe6323f62020-07-09 15:49:02 +02001625 }
1626
1627 return LY_SUCCESS;
1628}
1629
1630/**
1631 * @brief Update operations on a diff node when the new operation is DELETE.
1632 *
1633 * @param[in] diff_match Node from the diff.
Michal Vaskoe78faec2021-04-08 17:24:43 +02001634 * @param[in] cur_op Current operation of @p diff_match.
Michal Vaskoe6323f62020-07-09 15:49:02 +02001635 * @param[in] src_diff Current source diff node.
1636 * @return LY_ERR value.
1637 */
1638static LY_ERR
1639lyd_diff_merge_delete(struct lyd_node *diff_match, enum lyd_diff_op cur_op, const struct lyd_node *src_diff)
1640{
Michal Vasko17d0c5c2021-11-01 11:31:11 +01001641 struct lyd_node *child;
Michal Vaskoe78faec2021-04-08 17:24:43 +02001642 struct lyd_meta *meta;
1643 const char *meta_name;
Michal Vasko52afd7d2022-01-18 14:08:34 +01001644 const struct ly_ctx *ctx = LYD_CTX(diff_match);
Michal Vaskoe6323f62020-07-09 15:49:02 +02001645
1646 /* we can delete only exact existing nodes */
Michal Vaskob7be7a82020-08-20 09:09:04 +02001647 LY_CHECK_ERR_RET(lyd_compare_single(diff_match, src_diff, 0), LOGINT(LYD_CTX(src_diff)), LY_EINT);
Michal Vaskoe6323f62020-07-09 15:49:02 +02001648
1649 switch (cur_op) {
1650 case LYD_DIFF_OP_CREATE:
1651 /* it was created, but then deleted -> set NONE operation */
1652 LY_CHECK_RET(lyd_diff_change_op(diff_match, LYD_DIFF_OP_NONE));
1653
1654 if (diff_match->schema->nodetype & LYD_NODE_TERM) {
1655 /* add orig-default meta because it is expected */
Michal Vasko871a0252020-11-11 18:35:24 +01001656 LY_CHECK_RET(lyd_new_meta(LYD_CTX(src_diff), diff_match, NULL, "yang:orig-default",
1657 diff_match->flags & LYD_DEFAULT ? "true" : "false", 0, NULL));
Michal Vaskoe78faec2021-04-08 17:24:43 +02001658 } else if (!lysc_is_dup_inst_list(diff_match->schema)) {
Michal Vaskoe6323f62020-07-09 15:49:02 +02001659 /* keep operation for all descendants (for now) */
Radek Krejcia1c1e542020-09-29 16:06:52 +02001660 LY_LIST_FOR(lyd_child_no_keys(diff_match), child) {
Michal Vaskoe6323f62020-07-09 15:49:02 +02001661 LY_CHECK_RET(lyd_diff_change_op(child, cur_op));
1662 }
Michal Vaskoe78faec2021-04-08 17:24:43 +02001663 } /* else key-less list, for which all the descendants act as keys */
Michal Vaskoe6323f62020-07-09 15:49:02 +02001664 break;
1665 case LYD_DIFF_OP_REPLACE:
Michal Vasko17d0c5c2021-11-01 11:31:11 +01001666 /* remove the redundant metadata */
Michal Vaskoe78faec2021-04-08 17:24:43 +02001667 if (lysc_is_userordered(diff_match->schema)) {
1668 if (lysc_is_dup_inst_list(diff_match->schema)) {
1669 meta_name = "position";
1670 } else if (diff_match->schema->nodetype == LYS_LIST) {
1671 meta_name = "key";
1672 } else {
1673 meta_name = "value";
1674 }
1675 } else {
1676 assert(diff_match->schema->nodetype == LYS_LEAF);
1677
1678 /* switch value for the original one */
1679 meta = lyd_find_meta(diff_match->meta, NULL, "yang:orig-value");
Michal Vasko52afd7d2022-01-18 14:08:34 +01001680 LY_CHECK_ERR_RET(!meta, LOGERR_META(ctx, "yang:orig-value", diff_match), LY_EINVAL);
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02001681 if (lyd_change_term(diff_match, lyd_get_meta_value(meta))) {
Michal Vasko52afd7d2022-01-18 14:08:34 +01001682 LOGERR_UNEXPVAL(ctx, diff_match, "target diff");
1683 return LY_EINVAL;
Michal Vaskoe78faec2021-04-08 17:24:43 +02001684 }
1685
1686 /* switch default for the original one, then remove the meta */
1687 meta = lyd_find_meta(diff_match->meta, NULL, "yang:orig-default");
Michal Vasko52afd7d2022-01-18 14:08:34 +01001688 LY_CHECK_ERR_RET(!meta, LOGERR_META(ctx, "yang:orig-default", diff_match), LY_EINVAL);
Michal Vaskoe78faec2021-04-08 17:24:43 +02001689 diff_match->flags &= ~LYD_DEFAULT;
1690 if (meta->value.boolean) {
1691 diff_match->flags |= LYD_DEFAULT;
1692 }
1693 lyd_free_meta_single(meta);
1694
1695 meta_name = "orig-value";
1696 }
1697 lyd_diff_del_meta(diff_match, meta_name);
1698
Michal Vasko17d0c5c2021-11-01 11:31:11 +01001699 /* it was being changed, but should be deleted instead -> set DELETE operation */
1700 LY_CHECK_RET(lyd_diff_change_op(diff_match, LYD_DIFF_OP_DELETE));
1701 break;
Michal Vaskoe6323f62020-07-09 15:49:02 +02001702 case LYD_DIFF_OP_NONE:
1703 /* it was not modified, but should be deleted -> set DELETE operation */
1704 LY_CHECK_RET(lyd_diff_change_op(diff_match, LYD_DIFF_OP_DELETE));
Michal Vaskoe6323f62020-07-09 15:49:02 +02001705 break;
1706 default:
1707 /* delete operation is not valid */
Michal Vasko52afd7d2022-01-18 14:08:34 +01001708 LOGERR_MERGEOP(LYD_CTX(diff_match), diff_match, cur_op, LYD_DIFF_OP_DELETE);
1709 return LY_EINVAL;
Michal Vaskoe6323f62020-07-09 15:49:02 +02001710 }
1711
1712 return LY_SUCCESS;
1713}
1714
1715/**
1716 * @brief Check whether this diff node is redundant (does not change data).
1717 *
1718 * @param[in] diff Diff node.
1719 * @return 0 if not, non-zero if it is.
1720 */
1721static int
1722lyd_diff_is_redundant(struct lyd_node *diff)
1723{
1724 enum lyd_diff_op op;
1725 struct lyd_meta *meta, *orig_val_meta = NULL, *val_meta = NULL;
1726 struct lyd_node *child;
1727 const struct lys_module *mod;
Michal Vaskoe78faec2021-04-08 17:24:43 +02001728 const char *str, *orig_meta_name, *meta_name;
Michal Vaskoe6323f62020-07-09 15:49:02 +02001729
1730 assert(diff);
1731
Michal Vaskoe78faec2021-04-08 17:24:43 +02001732 if (lysc_is_dup_inst_list(diff->schema)) {
1733 /* all descendants are keys */
1734 child = NULL;
1735 } else {
1736 child = lyd_child_no_keys(diff);
1737 }
Michal Vaskob7be7a82020-08-20 09:09:04 +02001738 mod = ly_ctx_get_module_latest(LYD_CTX(diff), "yang");
Michal Vaskoe6323f62020-07-09 15:49:02 +02001739 assert(mod);
1740
1741 /* get node operation */
Michal Vasko53bf6f22020-07-14 08:23:40 +02001742 LY_CHECK_RET(lyd_diff_get_op(diff, &op), 0);
Michal Vaskoe6323f62020-07-09 15:49:02 +02001743
1744 if ((op == LYD_DIFF_OP_REPLACE) && lysc_is_userordered(diff->schema)) {
Michal Vaskoe78faec2021-04-08 17:24:43 +02001745 /* get metadata names */
1746 if (lysc_is_dup_inst_list(diff->schema)) {
1747 meta_name = "position";
1748 orig_meta_name = "orig-position";
1749 } else if (diff->schema->nodetype == LYS_LIST) {
1750 meta_name = "key";
1751 orig_meta_name = "orig-key";
1752 } else {
1753 meta_name = "value";
1754 orig_meta_name = "orig-value";
1755 }
1756
Michal Vaskoe6323f62020-07-09 15:49:02 +02001757 /* check for redundant move */
Michal Vaskoe78faec2021-04-08 17:24:43 +02001758 orig_val_meta = lyd_find_meta(diff->meta, mod, orig_meta_name);
1759 val_meta = lyd_find_meta(diff->meta, mod, meta_name);
Michal Vaskoe6323f62020-07-09 15:49:02 +02001760 assert(orig_val_meta && val_meta);
1761
1762 if (!lyd_compare_meta(orig_val_meta, val_meta)) {
1763 /* there is actually no move */
Michal Vasko3a41dff2020-07-15 14:30:28 +02001764 lyd_free_meta_single(orig_val_meta);
1765 lyd_free_meta_single(val_meta);
Michal Vaskoe6323f62020-07-09 15:49:02 +02001766 if (child) {
1767 /* change operation to NONE, we have siblings */
1768 lyd_diff_change_op(diff, LYD_DIFF_OP_NONE);
1769 return 0;
1770 }
1771
1772 /* redundant node, BUT !!
1773 * In diff the move operation is always converted to be INSERT_AFTER, which is fine
1774 * because the data that this is applied on should not change for the diff lifetime.
1775 * However, when we are merging 2 diffs, this conversion is actually lossy because
1776 * if the data change, the move operation can also change its meaning. In this specific
1777 * case the move operation will be lost. But it can be considered a feature, it is not supported.
1778 */
1779 return 1;
1780 }
Michal Vaskodb91fc32023-05-02 14:39:40 +02001781 } else if (op == LYD_DIFF_OP_NONE) {
1782 if (!diff->schema) {
1783 /* opaque node with none must be redundant */
Michal Vaskoe6323f62020-07-09 15:49:02 +02001784 return 1;
1785 }
Michal Vaskodb91fc32023-05-02 14:39:40 +02001786
1787 if (diff->schema->nodetype & LYD_NODE_TERM) {
1788 /* check whether at least the default flags are different */
1789 meta = lyd_find_meta(diff->meta, mod, "orig-default");
1790 assert(meta);
1791 str = lyd_get_meta_value(meta);
1792
1793 /* if previous and current dflt flags are the same, this node is redundant */
1794 if ((!strcmp(str, "true") && (diff->flags & LYD_DEFAULT)) || (!strcmp(str, "false") && !(diff->flags & LYD_DEFAULT))) {
1795 return 1;
1796 }
1797 return 0;
1798 }
Michal Vaskoe6323f62020-07-09 15:49:02 +02001799 }
1800
1801 if (!child && (op == LYD_DIFF_OP_NONE)) {
1802 return 1;
1803 }
1804
1805 return 0;
1806}
1807
1808/**
Michal Vaskoe78faec2021-04-08 17:24:43 +02001809 * @brief Merge sysrepo diff subtree with another diff, recursively.
Michal Vaskoe6323f62020-07-09 15:49:02 +02001810 *
1811 * @param[in] src_diff Source diff node.
1812 * @param[in] diff_parent Current sysrepo diff parent.
1813 * @param[in] diff_cb Optional diff callback.
1814 * @param[in] cb_data User data for @p diff_cb.
Michal Vaskoe78faec2021-04-08 17:24:43 +02001815 * @param[in,out] dup_inst Duplicate instance cache for all @p src_diff siblings.
Michal Vaskoc0e58e82020-11-11 19:04:33 +01001816 * @param[in] options Diff merge options.
Michal Vaskoe6323f62020-07-09 15:49:02 +02001817 * @param[in,out] diff Diff root node.
1818 * @return LY_ERR value.
1819 */
1820static LY_ERR
1821lyd_diff_merge_r(const struct lyd_node *src_diff, struct lyd_node *diff_parent, lyd_diff_cb diff_cb, void *cb_data,
Michal Vasko8efac242023-03-30 08:24:56 +02001822 struct ly_ht **dup_inst, uint16_t options, struct lyd_node **diff)
Michal Vaskoe6323f62020-07-09 15:49:02 +02001823{
1824 LY_ERR ret = LY_SUCCESS;
1825 struct lyd_node *child, *diff_node = NULL;
1826 enum lyd_diff_op src_op, cur_op;
Michal Vasko8efac242023-03-30 08:24:56 +02001827 struct ly_ht *child_dup_inst = NULL;
Michal Vaskoe6323f62020-07-09 15:49:02 +02001828
1829 /* get source node operation */
1830 LY_CHECK_RET(lyd_diff_get_op(src_diff, &src_op));
1831
1832 /* find an equal node in the current diff */
Michal Vaskoe78faec2021-04-08 17:24:43 +02001833 LY_CHECK_RET(lyd_diff_find_match(diff_parent ? lyd_child_no_keys(diff_parent) : *diff, src_diff, 1, dup_inst, &diff_node));
Michal Vaskoe6323f62020-07-09 15:49:02 +02001834
1835 if (diff_node) {
1836 /* get target (current) operation */
1837 LY_CHECK_RET(lyd_diff_get_op(diff_node, &cur_op));
1838
1839 /* merge operations */
1840 switch (src_op) {
1841 case LYD_DIFF_OP_REPLACE:
1842 ret = lyd_diff_merge_replace(diff_node, cur_op, src_diff);
1843 break;
1844 case LYD_DIFF_OP_CREATE:
Michal Vasko1dc0a842021-02-04 11:04:57 +01001845 if ((cur_op == LYD_DIFF_OP_CREATE) && lysc_is_dup_inst_list(diff_node->schema)) {
Michal Vaskoe78faec2021-04-08 17:24:43 +02001846 /* special case of creating duplicate (leaf-)list instances */
Michal Vasko1dc0a842021-02-04 11:04:57 +01001847 goto add_diff;
1848 }
1849
Michal Vaskoc0e58e82020-11-11 19:04:33 +01001850 ret = lyd_diff_merge_create(diff_node, cur_op, src_diff, options);
Michal Vaskoe6323f62020-07-09 15:49:02 +02001851 break;
1852 case LYD_DIFF_OP_DELETE:
1853 ret = lyd_diff_merge_delete(diff_node, cur_op, src_diff);
1854 break;
1855 case LYD_DIFF_OP_NONE:
Michal Vaskoe78faec2021-04-08 17:24:43 +02001856 /* key-less list can never have "none" operation since all its descendants are acting as "keys" */
1857 assert((src_diff->schema->nodetype != LYS_LIST) || !lysc_is_dup_inst_list(src_diff->schema));
Michal Vaskoe6323f62020-07-09 15:49:02 +02001858 ret = lyd_diff_merge_none(diff_node, cur_op, src_diff);
1859 break;
1860 default:
Michal Vaskob7be7a82020-08-20 09:09:04 +02001861 LOGINT_RET(LYD_CTX(src_diff));
Michal Vaskoe6323f62020-07-09 15:49:02 +02001862 }
1863 if (ret) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02001864 LOGERR(LYD_CTX(src_diff), LY_EOTHER, "Merging operation \"%s\" failed.", lyd_diff_op2str(src_op));
Michal Vaskoe6323f62020-07-09 15:49:02 +02001865 return ret;
1866 }
1867
1868 if (diff_cb) {
1869 /* call callback */
Michal Vaskobc5fba92020-08-07 12:14:39 +02001870 LY_CHECK_RET(diff_cb(src_diff, diff_node, cb_data));
Michal Vaskoe6323f62020-07-09 15:49:02 +02001871 }
1872
1873 /* update diff parent */
1874 diff_parent = diff_node;
1875
Michal Vaskoe78faec2021-04-08 17:24:43 +02001876 /* for diff purposes, all key-less list descendants actually act as keys (identifying the same instances),
1877 * so there is nothing to merge for these "keys" */
1878 if (!lysc_is_dup_inst_list(src_diff->schema)) {
1879 /* merge src_diff recursively */
1880 LY_LIST_FOR(lyd_child_no_keys(src_diff), child) {
1881 ret = lyd_diff_merge_r(child, diff_parent, diff_cb, cb_data, &child_dup_inst, options, diff);
1882 if (ret) {
1883 break;
1884 }
1885 }
Michal Vaskod7c048c2021-05-18 16:12:55 +02001886 lyd_dup_inst_free(child_dup_inst);
Michal Vaskoe78faec2021-04-08 17:24:43 +02001887 LY_CHECK_RET(ret);
Michal Vaskoe6323f62020-07-09 15:49:02 +02001888 }
1889 } else {
Michal Vasko1dc0a842021-02-04 11:04:57 +01001890add_diff:
Michal Vaskoe6323f62020-07-09 15:49:02 +02001891 /* add new diff node with all descendants */
Michal Vasko9ad76852022-07-12 10:18:03 +02001892 if ((src_diff->flags & LYD_EXT) && diff_parent) {
1893 LY_CHECK_RET(lyd_dup_single_to_ctx(src_diff, LYD_CTX(diff_parent), (struct lyd_node_inner *)diff_parent,
1894 LYD_DUP_RECURSIVE | LYD_DUP_WITH_FLAGS, &diff_node));
1895 } else {
1896 LY_CHECK_RET(lyd_dup_single(src_diff, (struct lyd_node_inner *)diff_parent,
1897 LYD_DUP_RECURSIVE | LYD_DUP_WITH_FLAGS, &diff_node));
1898 }
Michal Vaskoe6323f62020-07-09 15:49:02 +02001899
1900 /* insert node into diff if not already */
1901 if (!diff_parent) {
Michal Vaskob104f112020-07-17 09:54:54 +02001902 lyd_insert_sibling(*diff, diff_node, diff);
Michal Vaskoe6323f62020-07-09 15:49:02 +02001903 }
1904
1905 /* update operation */
1906 LY_CHECK_RET(lyd_diff_change_op(diff_node, src_op));
1907
1908 if (diff_cb) {
Michal Vaskoe2af8412020-12-03 14:11:38 +01001909 /* call callback with no source diff node since it was duplicated and just added */
1910 LY_CHECK_RET(diff_cb(NULL, diff_node, cb_data));
Michal Vaskoe6323f62020-07-09 15:49:02 +02001911 }
1912
1913 /* update diff parent */
1914 diff_parent = diff_node;
1915 }
1916
1917 /* remove any redundant nodes */
Michal Vaskob98d7082020-07-15 16:38:36 +02001918 if (lyd_diff_is_redundant(diff_parent)) {
Michal Vaskoe6323f62020-07-09 15:49:02 +02001919 if (diff_parent == *diff) {
1920 *diff = (*diff)->next;
1921 }
1922 lyd_free_tree(diff_parent);
1923 }
1924
1925 return LY_SUCCESS;
1926}
1927
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001928LIBYANG_API_DEF LY_ERR
Michal Vaskofb737aa2020-08-06 13:53:53 +02001929lyd_diff_merge_module(struct lyd_node **diff, const struct lyd_node *src_diff, const struct lys_module *mod,
Michal Vaskoc0e58e82020-11-11 19:04:33 +01001930 lyd_diff_cb diff_cb, void *cb_data, uint16_t options)
Michal Vaskoe6323f62020-07-09 15:49:02 +02001931{
1932 const struct lyd_node *src_root;
Michal Vasko8efac242023-03-30 08:24:56 +02001933 struct ly_ht *dup_inst = NULL;
Michal Vaskoe78faec2021-04-08 17:24:43 +02001934 LY_ERR ret = LY_SUCCESS;
Michal Vaskoe6323f62020-07-09 15:49:02 +02001935
1936 LY_LIST_FOR(src_diff, src_root) {
1937 if (mod && (lyd_owner_module(src_root) != mod)) {
1938 /* skip data nodes from different modules */
1939 continue;
1940 }
1941
1942 /* apply relevant nodes from the diff datatree */
Michal Vaskoe78faec2021-04-08 17:24:43 +02001943 LY_CHECK_GOTO(ret = lyd_diff_merge_r(src_root, NULL, diff_cb, cb_data, &dup_inst, options, diff), cleanup);
Michal Vaskoe6323f62020-07-09 15:49:02 +02001944 }
1945
Michal Vaskoe78faec2021-04-08 17:24:43 +02001946cleanup:
Michal Vaskod7c048c2021-05-18 16:12:55 +02001947 lyd_dup_inst_free(dup_inst);
Michal Vaskoe78faec2021-04-08 17:24:43 +02001948 return ret;
Michal Vaskoe6323f62020-07-09 15:49:02 +02001949}
1950
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001951LIBYANG_API_DEF LY_ERR
Michal Vasko04f85912020-08-07 12:14:58 +02001952lyd_diff_merge_tree(struct lyd_node **diff_first, struct lyd_node *diff_parent, const struct lyd_node *src_sibling,
Michal Vaskoc0e58e82020-11-11 19:04:33 +01001953 lyd_diff_cb diff_cb, void *cb_data, uint16_t options)
Michal Vasko04f85912020-08-07 12:14:58 +02001954{
Michal Vaskoe78faec2021-04-08 17:24:43 +02001955 LY_ERR ret;
Michal Vasko8efac242023-03-30 08:24:56 +02001956 struct ly_ht *dup_inst = NULL;
Michal Vaskoe78faec2021-04-08 17:24:43 +02001957
Michal Vasko04f85912020-08-07 12:14:58 +02001958 if (!src_sibling) {
1959 return LY_SUCCESS;
1960 }
1961
Michal Vaskoe78faec2021-04-08 17:24:43 +02001962 ret = lyd_diff_merge_r(src_sibling, diff_parent, diff_cb, cb_data, &dup_inst, options, diff_first);
Michal Vaskod7c048c2021-05-18 16:12:55 +02001963 lyd_dup_inst_free(dup_inst);
Michal Vaskoe78faec2021-04-08 17:24:43 +02001964 return ret;
Michal Vasko04f85912020-08-07 12:14:58 +02001965}
1966
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001967LIBYANG_API_DEF LY_ERR
Michal Vaskoc0e58e82020-11-11 19:04:33 +01001968lyd_diff_merge_all(struct lyd_node **diff, const struct lyd_node *src_diff, uint16_t options)
Michal Vaskoe6323f62020-07-09 15:49:02 +02001969{
Michal Vaskoc0e58e82020-11-11 19:04:33 +01001970 return lyd_diff_merge_module(diff, src_diff, NULL, NULL, NULL, options);
Michal Vaskoe6323f62020-07-09 15:49:02 +02001971}
Michal Vasko4231fb62020-07-13 13:54:47 +02001972
1973static LY_ERR
Michal Vaskobaba84e2021-02-05 16:33:30 +01001974lyd_diff_reverse_value(struct lyd_node *node, const struct lys_module *mod)
Michal Vasko4231fb62020-07-13 13:54:47 +02001975{
1976 LY_ERR ret = LY_SUCCESS;
1977 struct lyd_meta *meta;
Michal Vaskoba99a3e2020-08-18 15:50:05 +02001978 const char *val1 = NULL;
1979 char *val2;
Radek Krejci1deb5be2020-08-26 16:43:36 +02001980 uint32_t flags;
Michal Vasko4231fb62020-07-13 13:54:47 +02001981
Michal Vaskobaba84e2021-02-05 16:33:30 +01001982 assert(node->schema->nodetype & (LYS_LEAF | LYS_ANYDATA));
1983
1984 meta = lyd_find_meta(node->meta, mod, "orig-value");
Michal Vasko52afd7d2022-01-18 14:08:34 +01001985 LY_CHECK_ERR_RET(!meta, LOGERR_META(LYD_CTX(node), "orig-value", node), LY_EINVAL);
Michal Vasko4231fb62020-07-13 13:54:47 +02001986
1987 /* orig-value */
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02001988 val1 = lyd_get_meta_value(meta);
Michal Vasko4231fb62020-07-13 13:54:47 +02001989
1990 /* current value */
Michal Vaskobaba84e2021-02-05 16:33:30 +01001991 if (node->schema->nodetype == LYS_LEAF) {
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02001992 val2 = strdup(lyd_get_value(node));
Michal Vaskobaba84e2021-02-05 16:33:30 +01001993 } else {
1994 LY_CHECK_RET(lyd_any_value_str(node, &val2));
1995 }
Michal Vasko4231fb62020-07-13 13:54:47 +02001996
1997 /* switch values, keep default flag */
Michal Vaskobaba84e2021-02-05 16:33:30 +01001998 flags = node->flags;
1999 if (node->schema->nodetype == LYS_LEAF) {
2000 LY_CHECK_GOTO(ret = lyd_change_term(node, val1), cleanup);
2001 } else {
2002 union lyd_any_value anyval = {.str = val1};
Michal Vasko26bbb272022-08-02 14:54:33 +02002003
Michal Vaskobaba84e2021-02-05 16:33:30 +01002004 LY_CHECK_GOTO(ret = lyd_any_copy_value(node, &anyval, LYD_ANYDATA_STRING), cleanup);
2005 }
2006 node->flags = flags;
Michal Vasko4231fb62020-07-13 13:54:47 +02002007 LY_CHECK_GOTO(ret = lyd_change_meta(meta, val2), cleanup);
2008
2009cleanup:
Michal Vaskoba99a3e2020-08-18 15:50:05 +02002010 free(val2);
Michal Vasko4231fb62020-07-13 13:54:47 +02002011 return ret;
2012}
2013
2014static LY_ERR
2015lyd_diff_reverse_default(struct lyd_node *node, const struct lys_module *mod)
2016{
2017 struct lyd_meta *meta;
Radek Krejci1deb5be2020-08-26 16:43:36 +02002018 uint32_t flag1, flag2;
Michal Vasko4231fb62020-07-13 13:54:47 +02002019
2020 meta = lyd_find_meta(node->meta, mod, "orig-default");
Michal Vasko610e93b2020-11-09 20:58:32 +01002021 LY_CHECK_ERR_RET(!meta, LOGINT(mod->ctx), LY_EINT);
Michal Vasko4231fb62020-07-13 13:54:47 +02002022
2023 /* orig-default */
Michal Vaskoba99a3e2020-08-18 15:50:05 +02002024 if (meta->value.boolean) {
Michal Vasko4231fb62020-07-13 13:54:47 +02002025 flag1 = LYD_DEFAULT;
2026 } else {
2027 flag1 = 0;
2028 }
2029
2030 /* current default */
2031 flag2 = node->flags & LYD_DEFAULT;
2032
Michal Vasko610e93b2020-11-09 20:58:32 +01002033 if (flag1 == flag2) {
2034 /* no default state change so nothing to reverse */
2035 return LY_SUCCESS;
2036 }
2037
Michal Vasko4231fb62020-07-13 13:54:47 +02002038 /* switch defaults */
2039 node->flags &= ~LYD_DEFAULT;
2040 node->flags |= flag1;
2041 LY_CHECK_RET(lyd_change_meta(meta, flag2 ? "true" : "false"));
2042
2043 return LY_SUCCESS;
2044}
2045
2046static LY_ERR
2047lyd_diff_reverse_meta(struct lyd_node *node, const struct lys_module *mod, const char *name1, const char *name2)
2048{
2049 LY_ERR ret = LY_SUCCESS;
2050 struct lyd_meta *meta1, *meta2;
Michal Vaskoba99a3e2020-08-18 15:50:05 +02002051 const char *val1 = NULL;
2052 char *val2 = NULL;
Michal Vasko4231fb62020-07-13 13:54:47 +02002053
2054 meta1 = lyd_find_meta(node->meta, mod, name1);
Michal Vasko52afd7d2022-01-18 14:08:34 +01002055 LY_CHECK_ERR_RET(!meta1, LOGERR_META(LYD_CTX(node), name1, node), LY_EINVAL);
Michal Vasko4231fb62020-07-13 13:54:47 +02002056
2057 meta2 = lyd_find_meta(node->meta, mod, name2);
Michal Vasko52afd7d2022-01-18 14:08:34 +01002058 LY_CHECK_ERR_RET(!meta2, LOGERR_META(LYD_CTX(node), name2, node), LY_EINVAL);
Michal Vasko4231fb62020-07-13 13:54:47 +02002059
2060 /* value1 */
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02002061 val1 = lyd_get_meta_value(meta1);
Michal Vasko4231fb62020-07-13 13:54:47 +02002062
2063 /* value2 */
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02002064 val2 = strdup(lyd_get_meta_value(meta2));
Michal Vasko4231fb62020-07-13 13:54:47 +02002065
2066 /* switch values */
2067 LY_CHECK_GOTO(ret = lyd_change_meta(meta1, val2), cleanup);
2068 LY_CHECK_GOTO(ret = lyd_change_meta(meta2, val1), cleanup);
2069
2070cleanup:
Michal Vaskoba99a3e2020-08-18 15:50:05 +02002071 free(val2);
Michal Vasko4231fb62020-07-13 13:54:47 +02002072 return ret;
2073}
2074
Michal Vasko9a7e9d02021-03-09 13:52:25 +01002075/**
2076 * @brief Remove specific operation from all the nodes in a subtree.
2077 *
2078 * @param[in] diff Diff subtree to process.
2079 * @param[in] op Only expected operation.
2080 * @return LY_ERR value.
2081 */
2082static LY_ERR
2083lyd_diff_reverse_remove_op_r(struct lyd_node *diff, enum lyd_diff_op op)
2084{
2085 struct lyd_node *elem;
2086 struct lyd_meta *meta;
2087
2088 LYD_TREE_DFS_BEGIN(diff, elem) {
2089 meta = lyd_find_meta(elem->meta, NULL, "yang:operation");
2090 if (meta) {
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02002091 LY_CHECK_ERR_RET(lyd_diff_str2op(lyd_get_meta_value(meta)) != op, LOGINT(LYD_CTX(diff)), LY_EINT);
Michal Vasko9a7e9d02021-03-09 13:52:25 +01002092 lyd_free_meta_single(meta);
2093 }
2094
2095 LYD_TREE_DFS_END(diff, elem);
2096 }
2097
2098 return LY_SUCCESS;
2099}
2100
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002101LIBYANG_API_DEF LY_ERR
Michal Vasko66535812020-08-11 08:44:22 +02002102lyd_diff_reverse_all(const struct lyd_node *src_diff, struct lyd_node **diff)
Michal Vasko4231fb62020-07-13 13:54:47 +02002103{
2104 LY_ERR ret = LY_SUCCESS;
2105 const struct lys_module *mod;
Michal Vasko9a7e9d02021-03-09 13:52:25 +01002106 struct lyd_node *root, *elem, *iter;
Michal Vasko4231fb62020-07-13 13:54:47 +02002107 enum lyd_diff_op op;
2108
2109 LY_CHECK_ARG_RET(NULL, diff, LY_EINVAL);
2110
2111 if (!src_diff) {
2112 *diff = NULL;
2113 return LY_SUCCESS;
2114 }
2115
2116 /* duplicate diff */
Michal Vasko3a41dff2020-07-15 14:30:28 +02002117 LY_CHECK_RET(lyd_dup_siblings(src_diff, NULL, LYD_DUP_RECURSIVE, diff));
Michal Vasko4231fb62020-07-13 13:54:47 +02002118
2119 /* find module with metadata needed for later */
Michal Vaskob7be7a82020-08-20 09:09:04 +02002120 mod = ly_ctx_get_module_latest(LYD_CTX(src_diff), "yang");
2121 LY_CHECK_ERR_GOTO(!mod, LOGINT(LYD_CTX(src_diff)); ret = LY_EINT, cleanup);
Michal Vasko4231fb62020-07-13 13:54:47 +02002122
2123 LY_LIST_FOR(*diff, root) {
Michal Vasko56daf732020-08-10 10:57:18 +02002124 LYD_TREE_DFS_BEGIN(root, elem) {
Michal Vasko0f5c6a52020-11-06 17:18:03 +01002125 /* skip all keys */
2126 if (!lysc_is_key(elem->schema)) {
2127 /* find operation attribute, if any */
2128 LY_CHECK_GOTO(ret = lyd_diff_get_op(elem, &op), cleanup);
Michal Vasko4231fb62020-07-13 13:54:47 +02002129
Michal Vasko0f5c6a52020-11-06 17:18:03 +01002130 switch (op) {
2131 case LYD_DIFF_OP_CREATE:
2132 /* reverse create to delete */
2133 LY_CHECK_GOTO(ret = lyd_diff_change_op(elem, LYD_DIFF_OP_DELETE), cleanup);
Michal Vasko9e070522021-03-05 14:00:14 +01002134
Michal Vasko9a7e9d02021-03-09 13:52:25 +01002135 /* check all the children for the same operation, nothing else is expected */
2136 LY_LIST_FOR(lyd_child(elem), iter) {
2137 lyd_diff_reverse_remove_op_r(iter, LYD_DIFF_OP_CREATE);
2138 }
2139
Michal Vasko9e070522021-03-05 14:00:14 +01002140 LYD_TREE_DFS_continue = 1;
Michal Vasko4231fb62020-07-13 13:54:47 +02002141 break;
Michal Vasko0f5c6a52020-11-06 17:18:03 +01002142 case LYD_DIFF_OP_DELETE:
2143 /* reverse delete to create */
2144 LY_CHECK_GOTO(ret = lyd_diff_change_op(elem, LYD_DIFF_OP_CREATE), cleanup);
Michal Vasko9e070522021-03-05 14:00:14 +01002145
Michal Vasko9a7e9d02021-03-09 13:52:25 +01002146 /* check all the children for the same operation, nothing else is expected */
2147 LY_LIST_FOR(lyd_child(elem), iter) {
2148 lyd_diff_reverse_remove_op_r(iter, LYD_DIFF_OP_DELETE);
2149 }
2150
Michal Vasko9e070522021-03-05 14:00:14 +01002151 LYD_TREE_DFS_continue = 1;
Michal Vasko4231fb62020-07-13 13:54:47 +02002152 break;
Michal Vasko0f5c6a52020-11-06 17:18:03 +01002153 case LYD_DIFF_OP_REPLACE:
2154 switch (elem->schema->nodetype) {
2155 case LYS_LEAF:
2156 /* leaf value change */
2157 LY_CHECK_GOTO(ret = lyd_diff_reverse_value(elem, mod), cleanup);
2158 LY_CHECK_GOTO(ret = lyd_diff_reverse_default(elem, mod), cleanup);
2159 break;
Michal Vaskobaba84e2021-02-05 16:33:30 +01002160 case LYS_ANYXML:
2161 case LYS_ANYDATA:
2162 /* any value change */
2163 LY_CHECK_GOTO(ret = lyd_diff_reverse_value(elem, mod), cleanup);
2164 break;
Michal Vasko0f5c6a52020-11-06 17:18:03 +01002165 case LYS_LEAFLIST:
2166 /* leaf-list move */
2167 LY_CHECK_GOTO(ret = lyd_diff_reverse_default(elem, mod), cleanup);
Michal Vaskoe78faec2021-04-08 17:24:43 +02002168 if (lysc_is_dup_inst_list(elem->schema)) {
2169 LY_CHECK_GOTO(ret = lyd_diff_reverse_meta(elem, mod, "orig-position", "position"), cleanup);
2170 } else {
2171 LY_CHECK_GOTO(ret = lyd_diff_reverse_meta(elem, mod, "orig-value", "value"), cleanup);
2172 }
Michal Vasko0f5c6a52020-11-06 17:18:03 +01002173 break;
2174 case LYS_LIST:
2175 /* list move */
Michal Vaskoe78faec2021-04-08 17:24:43 +02002176 if (lysc_is_dup_inst_list(elem->schema)) {
2177 LY_CHECK_GOTO(ret = lyd_diff_reverse_meta(elem, mod, "orig-position", "position"), cleanup);
2178 } else {
2179 LY_CHECK_GOTO(ret = lyd_diff_reverse_meta(elem, mod, "orig-key", "key"), cleanup);
2180 }
Michal Vasko0f5c6a52020-11-06 17:18:03 +01002181 break;
2182 default:
2183 LOGINT(LYD_CTX(src_diff));
2184 ret = LY_EINT;
2185 goto cleanup;
2186 }
Michal Vasko4231fb62020-07-13 13:54:47 +02002187 break;
Michal Vasko0f5c6a52020-11-06 17:18:03 +01002188 case LYD_DIFF_OP_NONE:
2189 switch (elem->schema->nodetype) {
2190 case LYS_LEAF:
2191 case LYS_LEAFLIST:
2192 /* default flag change */
2193 LY_CHECK_GOTO(ret = lyd_diff_reverse_default(elem, mod), cleanup);
2194 break;
2195 default:
2196 /* nothing to do */
2197 break;
2198 }
Michal Vasko4231fb62020-07-13 13:54:47 +02002199 break;
2200 }
Michal Vasko4231fb62020-07-13 13:54:47 +02002201 }
2202
Michal Vasko56daf732020-08-10 10:57:18 +02002203 LYD_TREE_DFS_END(root, elem);
Michal Vasko4231fb62020-07-13 13:54:47 +02002204 }
2205 }
2206
2207cleanup:
2208 if (ret) {
2209 lyd_free_siblings(*diff);
2210 *diff = NULL;
2211 }
2212 return ret;
2213}