blob: a9292df55eca7e7cc5cff5397e1e1b14f684b197 [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 Vasko271d2e32023-01-31 15:43:19 +0100662 struct hash_table **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 Vaskoe78faec2021-04-08 17:24:43 +0200666 if (target->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) {
667 /* try to find the exact instance */
Michal Vasko2bd856f2022-12-02 14:03:29 +0100668 r = lyd_find_sibling_first(siblings, target, match);
Michal Vaskoe78faec2021-04-08 17:24:43 +0200669 } else {
670 /* try to simply find the node, there cannot be more instances */
Michal Vasko2bd856f2022-12-02 14:03:29 +0100671 r = lyd_find_sibling_val(siblings, target->schema, NULL, 0, match);
672 }
673 if (r && (r != LY_ENOTFOUND)) {
674 return r;
Michal Vaskoe78faec2021-04-08 17:24:43 +0200675 }
676
Michal Vaskod7c048c2021-05-18 16:12:55 +0200677 /* update match as needed */
Michal Vasko271d2e32023-01-31 15:43:19 +0100678 LY_CHECK_RET(lyd_dup_inst_next(match, siblings, dup_inst_ht));
Michal Vaskoe78faec2021-04-08 17:24:43 +0200679
680 if (*match && ((*match)->flags & LYD_DEFAULT) && !defaults) {
681 /* ignore default nodes */
682 *match = NULL;
683 }
684 return LY_SUCCESS;
685}
686
687/**
Michal Vaskod59035b2020-07-08 12:00:06 +0200688 * @brief Perform diff for all siblings at certain depth, recursively.
689 *
690 * For user-ordered lists/leaf-lists a specific structure is used for storing
691 * the current order. The idea is to apply all the generated diff changes
692 * virtually on the first tree so that we can continue to generate correct
693 * changes after some were already generated.
694 *
695 * The algorithm then uses second tree position-based changes with a before
696 * (preceding) item anchor.
697 *
698 * Example:
699 *
700 * Virtual first tree leaf-list order:
701 * 1 2 [3] 4 5
702 *
703 * Second tree leaf-list order:
704 * 1 2 [5] 3 4
705 *
706 * We are at the 3rd node now. We look at whether the nodes on the 3rd position
707 * match - they do not - move nodes so that the 3rd position node is final ->
708 * -> move node 5 to the 3rd position -> move node 5 after node 2.
709 *
710 * Required properties:
711 * Stored operations (move) should not be affected by later operations -
712 * - would cause a redundantly long list of operations, possibly inifinite.
713 *
714 * Implemenation justification:
715 * First, all delete operations and only then move/create operations are stored.
716 * Also, preceding anchor is used and after each iteration another node is
717 * at its final position. That results in the invariant that all preceding
718 * nodes are final and will not be changed by the later operations, meaning
719 * they can safely be used as anchors for the later operations.
720 *
721 * @param[in] first First tree first sibling.
722 * @param[in] second Second tree first sibling.
723 * @param[in] options Diff options.
Michal Vasko3a41dff2020-07-15 14:30:28 +0200724 * @param[in] nosiblings Whether to skip following siblings.
Michal Vaskod59035b2020-07-08 12:00:06 +0200725 * @param[in,out] diff Diff to append to.
726 * @return LY_ERR value.
727 */
728static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +0200729lyd_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 +0200730 struct lyd_node **diff)
Michal Vaskod59035b2020-07-08 12:00:06 +0200731{
732 LY_ERR ret = LY_SUCCESS;
733 const struct lyd_node *iter_first, *iter_second;
734 struct lyd_node *match_second, *match_first;
Michal Vasko5da938a2022-03-01 09:19:02 +0100735 struct lyd_diff_userord *userord = NULL, *userord_item;
Michal Vasko271d2e32023-01-31 15:43:19 +0100736 struct hash_table *dup_inst_first = NULL, *dup_inst_second = NULL;
Michal Vaskod59035b2020-07-08 12:00:06 +0200737 LY_ARRAY_COUNT_TYPE u;
738 enum lyd_diff_op op;
739 const char *orig_default;
Michal Vaskoe78faec2021-04-08 17:24:43 +0200740 char *orig_value, *key, *value, *position, *orig_key, *orig_position;
Michal Vaskod59035b2020-07-08 12:00:06 +0200741
Michal Vaskod59035b2020-07-08 12:00:06 +0200742 /* compare first tree to the second tree - delete, replace, none */
743 LY_LIST_FOR(first, iter_first) {
Michal Vaskoc825ed72021-07-21 16:05:59 +0200744 if (!iter_first->schema) {
745 continue;
746 }
747
Michal Vaskod59035b2020-07-08 12:00:06 +0200748 assert(!(iter_first->schema->flags & LYS_KEY));
Michal Vasko3a41dff2020-07-15 14:30:28 +0200749 if ((iter_first->flags & LYD_DEFAULT) && !(options & LYD_DIFF_DEFAULTS)) {
Michal Vaskod59035b2020-07-08 12:00:06 +0200750 /* skip default nodes */
751 continue;
752 }
753
Michal Vaskoe78faec2021-04-08 17:24:43 +0200754 /* find a match in the second tree */
755 LY_CHECK_GOTO(ret = lyd_diff_find_match(second, iter_first, options & LYD_DIFF_DEFAULTS, &dup_inst_second,
756 &match_second), cleanup);
Michal Vaskod59035b2020-07-08 12:00:06 +0200757
758 if (lysc_is_userordered(iter_first->schema)) {
Michal Vasko5da938a2022-03-01 09:19:02 +0100759 /* get (create) userord entry */
760 userord_item = lyd_diff_userord_get(iter_first, iter_first->schema, &userord);
761 LY_CHECK_ERR_GOTO(!userord_item, LOGMEM(LYD_CTX(iter_first)); ret = LY_EMEM, cleanup);
762
Michal Vaskoe78faec2021-04-08 17:24:43 +0200763 /* we are handling only user-ordered node delete now */
764 if (!match_second) {
765 /* get all the attributes */
Michal Vasko5da938a2022-03-01 09:19:02 +0100766 LY_CHECK_GOTO(ret = lyd_diff_userord_attrs(iter_first, match_second, options, userord_item, &op,
767 &orig_default, &value, &orig_value, &key, &orig_key, &position, &orig_position), cleanup);
Michal Vaskoe78faec2021-04-08 17:24:43 +0200768
769 /* there must be changes, it is deleted */
770 assert(op == LYD_DIFF_OP_DELETE);
Michal Vasko5da938a2022-03-01 09:19:02 +0100771 ret = lyd_diff_add(iter_first, op, orig_default, orig_value, key, value, position, orig_key,
772 orig_position, diff);
Michal Vaskoe78faec2021-04-08 17:24:43 +0200773
774 free(orig_value);
775 free(key);
776 free(value);
777 free(position);
778 free(orig_key);
779 free(orig_position);
780 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskod59035b2020-07-08 12:00:06 +0200781 }
Michal Vaskod59035b2020-07-08 12:00:06 +0200782 } else {
783 /* get all the attributes */
784 ret = lyd_diff_attrs(iter_first, match_second, options, &op, &orig_default, &orig_value);
785
786 /* add into diff if there are any changes */
787 if (!ret) {
788 if (op == LYD_DIFF_OP_DELETE) {
Michal Vaskoe78faec2021-04-08 17:24:43 +0200789 ret = lyd_diff_add(iter_first, op, orig_default, orig_value, NULL, NULL, NULL, NULL, NULL, diff);
Michal Vaskod59035b2020-07-08 12:00:06 +0200790 } else {
Michal Vasko3c2dd6c2020-11-06 17:38:55 +0100791 assert(match_second);
Michal Vaskoe78faec2021-04-08 17:24:43 +0200792 ret = lyd_diff_add(match_second, op, orig_default, orig_value, NULL, NULL, NULL, NULL, NULL, diff);
Michal Vaskod59035b2020-07-08 12:00:06 +0200793 }
794
795 free(orig_value);
796 LY_CHECK_GOTO(ret, cleanup);
797 } else if (ret == LY_ENOT) {
798 ret = LY_SUCCESS;
799 } else {
800 goto cleanup;
801 }
802 }
803
804 /* check descendants, if any, recursively */
805 if (match_second) {
Michal Vaskoe78faec2021-04-08 17:24:43 +0200806 LY_CHECK_GOTO(ret = lyd_diff_siblings_r(lyd_child_no_keys(iter_first), lyd_child_no_keys(match_second),
807 options, 0, diff), cleanup);
Michal Vaskod59035b2020-07-08 12:00:06 +0200808 }
809
810 if (nosiblings) {
811 break;
812 }
813 }
814
815 /* reset all cached positions */
816 LY_ARRAY_FOR(userord, u) {
817 userord[u].pos = 0;
818 }
819
820 /* compare second tree to the first tree - create, user-ordered move */
821 LY_LIST_FOR(second, iter_second) {
Michal Vaskoc825ed72021-07-21 16:05:59 +0200822 if (!iter_second->schema) {
823 continue;
824 }
825
Michal Vaskod59035b2020-07-08 12:00:06 +0200826 assert(!(iter_second->schema->flags & LYS_KEY));
Michal Vasko3a41dff2020-07-15 14:30:28 +0200827 if ((iter_second->flags & LYD_DEFAULT) && !(options & LYD_DIFF_DEFAULTS)) {
Michal Vaskod59035b2020-07-08 12:00:06 +0200828 /* skip default nodes */
829 continue;
830 }
831
Michal Vaskoe78faec2021-04-08 17:24:43 +0200832 /* find a match in the first tree */
833 LY_CHECK_GOTO(ret = lyd_diff_find_match(first, iter_second, options & LYD_DIFF_DEFAULTS, &dup_inst_first,
834 &match_first), cleanup);
Michal Vaskod59035b2020-07-08 12:00:06 +0200835
836 if (lysc_is_userordered(iter_second->schema)) {
Michal Vasko5da938a2022-03-01 09:19:02 +0100837 /* get userord entry */
Michal Vaskoaa51cc52022-12-06 09:57:11 +0100838 userord_item = lyd_diff_userord_get(match_first, iter_second->schema, &userord);
Michal Vasko5da938a2022-03-01 09:19:02 +0100839 LY_CHECK_ERR_GOTO(!userord_item, LOGMEM(LYD_CTX(iter_second)); ret = LY_EMEM, cleanup);
840
Michal Vaskod59035b2020-07-08 12:00:06 +0200841 /* get all the attributes */
Michal Vasko5da938a2022-03-01 09:19:02 +0100842 ret = lyd_diff_userord_attrs(match_first, iter_second, options, userord_item, &op, &orig_default,
Michal Vaskoe78faec2021-04-08 17:24:43 +0200843 &value, &orig_value, &key, &orig_key, &position, &orig_position);
Michal Vaskod59035b2020-07-08 12:00:06 +0200844
845 /* add into diff if there are any changes */
846 if (!ret) {
Michal Vaskoe78faec2021-04-08 17:24:43 +0200847 ret = lyd_diff_add(iter_second, op, orig_default, orig_value, key, value, position, orig_key,
848 orig_position, diff);
Michal Vaskod59035b2020-07-08 12:00:06 +0200849
850 free(orig_value);
851 free(key);
852 free(value);
Michal Vaskoe78faec2021-04-08 17:24:43 +0200853 free(position);
Michal Vaskod59035b2020-07-08 12:00:06 +0200854 free(orig_key);
Michal Vaskoe78faec2021-04-08 17:24:43 +0200855 free(orig_position);
Michal Vaskod59035b2020-07-08 12:00:06 +0200856 LY_CHECK_GOTO(ret, cleanup);
857 } else if (ret == LY_ENOT) {
858 ret = LY_SUCCESS;
859 } else {
860 goto cleanup;
861 }
862 } else if (!match_first) {
863 /* get all the attributes */
Michal Vaskoe78faec2021-04-08 17:24:43 +0200864 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 +0200865
866 /* there must be changes, it is created */
867 assert(op == LYD_DIFF_OP_CREATE);
Michal Vaskoe78faec2021-04-08 17:24:43 +0200868 ret = lyd_diff_add(iter_second, op, orig_default, orig_value, NULL, NULL, NULL, NULL, NULL, diff);
Michal Vaskod59035b2020-07-08 12:00:06 +0200869
870 free(orig_value);
871 LY_CHECK_GOTO(ret, cleanup);
872 } /* else was handled */
873
874 if (nosiblings) {
875 break;
876 }
877 }
878
879cleanup:
Michal Vaskod7c048c2021-05-18 16:12:55 +0200880 lyd_dup_inst_free(dup_inst_first);
881 lyd_dup_inst_free(dup_inst_second);
Michal Vaskod59035b2020-07-08 12:00:06 +0200882 LY_ARRAY_FOR(userord, u) {
883 LY_ARRAY_FREE(userord[u].inst);
884 }
885 LY_ARRAY_FREE(userord);
886 return ret;
887}
888
Michal Vasko3a41dff2020-07-15 14:30:28 +0200889static LY_ERR
Michal Vasko55896172022-02-17 10:47:21 +0100890lyd_diff(const struct lyd_node *first, const struct lyd_node *second, uint16_t options, ly_bool nosiblings,
891 struct lyd_node **diff)
Michal Vaskod59035b2020-07-08 12:00:06 +0200892{
893 const struct ly_ctx *ctx;
894
895 LY_CHECK_ARG_RET(NULL, diff, LY_EINVAL);
896
897 if (first) {
Michal Vaskob7be7a82020-08-20 09:09:04 +0200898 ctx = LYD_CTX(first);
Michal Vaskod59035b2020-07-08 12:00:06 +0200899 } else if (second) {
Michal Vaskob7be7a82020-08-20 09:09:04 +0200900 ctx = LYD_CTX(second);
Michal Vaskod59035b2020-07-08 12:00:06 +0200901 } else {
902 ctx = NULL;
903 }
904
905 if (first && second && (lysc_data_parent(first->schema) != lysc_data_parent(second->schema))) {
906 LOGERR(ctx, LY_EINVAL, "Invalid arguments - cannot create diff for unrelated data (%s()).", __func__);
907 return LY_EINVAL;
908 }
909
910 *diff = NULL;
911
Michal Vasko3a41dff2020-07-15 14:30:28 +0200912 return lyd_diff_siblings_r(first, second, options, nosiblings, diff);
913}
914
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100915LIBYANG_API_DEF LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200916lyd_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 +0200917{
918 return lyd_diff(first, second, options, 1, diff);
919}
920
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100921LIBYANG_API_DEF LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200922lyd_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 +0200923{
924 return lyd_diff(first, second, options, 0, diff);
Michal Vaskod59035b2020-07-08 12:00:06 +0200925}
926
927/**
Michal Vaskod59035b2020-07-08 12:00:06 +0200928 * @brief Learn operation of a diff node.
929 *
930 * @param[in] diff_node Diff node.
931 * @param[out] op Operation.
Michal Vaskod59035b2020-07-08 12:00:06 +0200932 * @return LY_ERR value.
933 */
934static LY_ERR
Michal Vaskoe6323f62020-07-09 15:49:02 +0200935lyd_diff_get_op(const struct lyd_node *diff_node, enum lyd_diff_op *op)
Michal Vaskod59035b2020-07-08 12:00:06 +0200936{
937 struct lyd_meta *meta = NULL;
938 const struct lyd_node *diff_parent;
Michal Vaskoe6323f62020-07-09 15:49:02 +0200939 const char *str;
Michal Vasko52afd7d2022-01-18 14:08:34 +0100940 char *path;
Michal Vaskod59035b2020-07-08 12:00:06 +0200941
Michal Vasko9e685082021-01-29 14:49:09 +0100942 for (diff_parent = diff_node; diff_parent; diff_parent = lyd_parent(diff_parent)) {
Michal Vaskod59035b2020-07-08 12:00:06 +0200943 LY_LIST_FOR(diff_parent->meta, meta) {
944 if (!strcmp(meta->name, "operation") && !strcmp(meta->annotation->module->name, "yang")) {
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200945 str = lyd_get_meta_value(meta);
Michal Vaskod59035b2020-07-08 12:00:06 +0200946 if ((str[0] == 'r') && (diff_parent != diff_node)) {
947 /* we do not care about this operation if it's in our parent */
948 continue;
949 }
Michal Vaskoe6323f62020-07-09 15:49:02 +0200950 *op = lyd_diff_str2op(str);
Michal Vaskod59035b2020-07-08 12:00:06 +0200951 break;
952 }
953 }
954 if (meta) {
955 break;
956 }
957 }
Michal Vasko52afd7d2022-01-18 14:08:34 +0100958
959 if (!meta) {
960 path = lyd_path(diff_node, LYD_PATH_STD, NULL, 0);
961 LOGERR(LYD_CTX(diff_node), LY_EINVAL, "Node \"%s\" without an operation.", path);
962 free(path);
963 return LY_EINT;
964 }
Michal Vaskod59035b2020-07-08 12:00:06 +0200965
Michal Vaskod59035b2020-07-08 12:00:06 +0200966 return LY_SUCCESS;
967}
968
969/**
970 * @brief Insert a diff node into a data tree.
971 *
972 * @param[in,out] first_node First sibling of the data tree.
973 * @param[in] parent_node Data tree sibling parent node.
974 * @param[in] new_node Node to insert.
Michal Vaskoe78faec2021-04-08 17:24:43 +0200975 * @param[in] userord_anchor Optional anchor (key, value, or position) of relative (leaf-)list instance. If not set,
976 * the user-ordered instance will be inserted at the first position.
Michal Vaskod59035b2020-07-08 12:00:06 +0200977 * @return err_info, NULL on success.
978 */
979static LY_ERR
980lyd_diff_insert(struct lyd_node **first_node, struct lyd_node *parent_node, struct lyd_node *new_node,
Michal Vaskoe78faec2021-04-08 17:24:43 +0200981 const char *userord_anchor)
Michal Vaskod59035b2020-07-08 12:00:06 +0200982{
983 LY_ERR ret;
984 struct lyd_node *anchor;
Michal Vaskoe78faec2021-04-08 17:24:43 +0200985 uint32_t pos, anchor_pos;
986 int found;
Michal Vaskod59035b2020-07-08 12:00:06 +0200987
988 assert(new_node);
989
990 if (!*first_node) {
991 if (!parent_node) {
992 /* no parent or siblings */
993 *first_node = new_node;
994 return LY_SUCCESS;
995 }
996
997 /* simply insert into parent, no other children */
Michal Vaskoe78faec2021-04-08 17:24:43 +0200998 if (userord_anchor) {
Michal Vaskob7be7a82020-08-20 09:09:04 +0200999 LOGERR(LYD_CTX(new_node), LY_EINVAL, "Node \"%s\" instance to insert next to not found.",
Michal Vasko69730152020-10-09 16:30:07 +02001000 new_node->schema->name);
Michal Vaskod59035b2020-07-08 12:00:06 +02001001 return LY_EINVAL;
1002 }
Michal Vaskob104f112020-07-17 09:54:54 +02001003 return lyd_insert_child(parent_node, new_node);
Michal Vaskod59035b2020-07-08 12:00:06 +02001004 }
1005
Michal Vasko9e685082021-01-29 14:49:09 +01001006 assert(!(*first_node)->parent || (lyd_parent(*first_node) == parent_node));
Michal Vaskod59035b2020-07-08 12:00:06 +02001007
Michal Vaskod59035b2020-07-08 12:00:06 +02001008 if (!lysc_is_userordered(new_node->schema)) {
Michal Vaskob104f112020-07-17 09:54:54 +02001009 /* simple insert */
1010 return lyd_insert_sibling(*first_node, new_node, first_node);
Michal Vaskod59035b2020-07-08 12:00:06 +02001011 }
1012
Michal Vaskoe78faec2021-04-08 17:24:43 +02001013 if (userord_anchor) {
Michal Vaskod59035b2020-07-08 12:00:06 +02001014 /* find the anchor sibling */
Michal Vaskoe78faec2021-04-08 17:24:43 +02001015 if (lysc_is_dup_inst_list(new_node->schema)) {
1016 anchor_pos = atoi(userord_anchor);
Michal Vasko0ff97752022-01-18 16:35:41 +01001017 if (!anchor_pos) {
1018 LOGERR(LYD_CTX(new_node), LY_EINVAL, "Invalid user-ordered anchor value \"%s\".", userord_anchor);
1019 return LY_EINVAL;
1020 }
Michal Vaskoe78faec2021-04-08 17:24:43 +02001021
1022 found = 0;
1023 pos = 1;
1024 LYD_LIST_FOR_INST(*first_node, new_node->schema, anchor) {
1025 if (pos == anchor_pos) {
1026 found = 1;
1027 break;
1028 }
1029 ++pos;
1030 }
1031 if (!found) {
1032 LOGERR(LYD_CTX(new_node), LY_EINVAL, "Node \"%s\" instance to insert next to not found.",
1033 new_node->schema->name);
1034 return LY_EINVAL;
1035 }
1036 } else {
1037 ret = lyd_find_sibling_val(*first_node, new_node->schema, userord_anchor, 0, &anchor);
1038 if (ret == LY_ENOTFOUND) {
1039 LOGERR(LYD_CTX(new_node), LY_EINVAL, "Node \"%s\" instance to insert next to not found.",
1040 new_node->schema->name);
1041 return LY_EINVAL;
1042 } else if (ret) {
1043 return ret;
1044 }
Michal Vaskod59035b2020-07-08 12:00:06 +02001045 }
1046
1047 /* insert after */
1048 LY_CHECK_RET(lyd_insert_after(anchor, new_node));
1049 assert(new_node->prev == anchor);
1050 if (*first_node == new_node) {
1051 *first_node = anchor;
1052 }
1053 } else {
Michal Vaskoea7d3232022-04-19 12:01:36 +02001054 /* find the first instance */
1055 ret = lyd_find_sibling_val(*first_node, new_node->schema, NULL, 0, &anchor);
1056 LY_CHECK_RET(ret && (ret != LY_ENOTFOUND), ret);
Michal Vaskod59035b2020-07-08 12:00:06 +02001057
Michal Vaskoea7d3232022-04-19 12:01:36 +02001058 if (anchor) {
1059 /* insert before the first instance */
1060 LY_CHECK_RET(lyd_insert_before(anchor, new_node));
1061 if ((*first_node)->prev->next) {
1062 assert(!new_node->prev->next);
1063 *first_node = new_node;
Michal Vaskod59035b2020-07-08 12:00:06 +02001064 }
Michal Vaskod59035b2020-07-08 12:00:06 +02001065 } else {
Michal Vaskoea7d3232022-04-19 12:01:36 +02001066 /* insert anywhere */
1067 LY_CHECK_RET(lyd_insert_sibling(*first_node, new_node, first_node));
Michal Vaskod59035b2020-07-08 12:00:06 +02001068 }
1069 }
1070
1071 return LY_SUCCESS;
1072}
1073
1074/**
1075 * @brief Apply diff subtree on data tree nodes, recursively.
1076 *
1077 * @param[in,out] first_node First sibling of the data tree.
1078 * @param[in] parent_node Parent of the first sibling.
1079 * @param[in] diff_node Current diff node.
Michal Vaskoe6323f62020-07-09 15:49:02 +02001080 * @param[in] diff_cb Optional diff callback.
1081 * @param[in] cb_data User data for @p diff_cb.
Michal Vaskoe78faec2021-04-08 17:24:43 +02001082 * @param[in,out] dup_inst Duplicate instance cache for all @p diff_node siblings.
Michal Vaskod59035b2020-07-08 12:00:06 +02001083 * @return LY_ERR value.
1084 */
1085static LY_ERR
1086lyd_diff_apply_r(struct lyd_node **first_node, struct lyd_node *parent_node, const struct lyd_node *diff_node,
Michal Vasko271d2e32023-01-31 15:43:19 +01001087 lyd_diff_cb diff_cb, void *cb_data, struct hash_table **dup_inst)
Michal Vaskod59035b2020-07-08 12:00:06 +02001088{
1089 LY_ERR ret;
1090 struct lyd_node *match, *diff_child;
Michal Vaskoe78faec2021-04-08 17:24:43 +02001091 const char *str_val, *meta_str;
Michal Vaskoe6323f62020-07-09 15:49:02 +02001092 enum lyd_diff_op op;
1093 struct lyd_meta *meta;
Michal Vasko271d2e32023-01-31 15:43:19 +01001094 struct hash_table *child_dup_inst = NULL;
Michal Vaskob7be7a82020-08-20 09:09:04 +02001095 const struct ly_ctx *ctx = LYD_CTX(diff_node);
Michal Vaskod59035b2020-07-08 12:00:06 +02001096
1097 /* read all the valid attributes */
Michal Vaskoe6323f62020-07-09 15:49:02 +02001098 LY_CHECK_RET(lyd_diff_get_op(diff_node, &op));
Michal Vaskod59035b2020-07-08 12:00:06 +02001099
Michal Vaskoe6323f62020-07-09 15:49:02 +02001100 /* handle specific user-ordered (leaf-)lists operations separately */
1101 if (lysc_is_userordered(diff_node->schema) && ((op == LYD_DIFF_OP_CREATE) || (op == LYD_DIFF_OP_REPLACE))) {
1102 if (op == LYD_DIFF_OP_REPLACE) {
Michal Vaskod59035b2020-07-08 12:00:06 +02001103 /* find the node (we must have some siblings because the node was only moved) */
Michal Vaskoe78faec2021-04-08 17:24:43 +02001104 LY_CHECK_RET(lyd_diff_find_match(*first_node, diff_node, 1, dup_inst, &match));
Michal Vasko52afd7d2022-01-18 14:08:34 +01001105 LY_CHECK_ERR_RET(!match, LOGERR_NOINST(ctx, diff_node), LY_EINVAL);
Michal Vaskod59035b2020-07-08 12:00:06 +02001106 } else {
Michal Vasko3a41dff2020-07-15 14:30:28 +02001107 /* duplicate the node */
1108 LY_CHECK_RET(lyd_dup_single(diff_node, NULL, LYD_DUP_NO_META, &match));
Michal Vaskod59035b2020-07-08 12:00:06 +02001109 }
1110
Michal Vaskoe78faec2021-04-08 17:24:43 +02001111 /* get "key", "value", or "position" metadata string value */
1112 if (lysc_is_dup_inst_list(diff_node->schema)) {
1113 meta_str = "yang:position";
1114 } else if (diff_node->schema->nodetype == LYS_LIST) {
1115 meta_str = "yang:key";
1116 } else {
1117 meta_str = "yang:value";
1118 }
1119 meta = lyd_find_meta(diff_node->meta, NULL, meta_str);
Michal Vasko52afd7d2022-01-18 14:08:34 +01001120 LY_CHECK_ERR_RET(!meta, LOGERR_META(ctx, meta_str, diff_node), LY_EINVAL);
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02001121 str_val = lyd_get_meta_value(meta);
Michal Vaskoe6323f62020-07-09 15:49:02 +02001122
Michal Vaskod59035b2020-07-08 12:00:06 +02001123 /* insert/move the node */
Michal Vaskoe6323f62020-07-09 15:49:02 +02001124 if (str_val[0]) {
1125 ret = lyd_diff_insert(first_node, parent_node, match, str_val);
Michal Vaskod59035b2020-07-08 12:00:06 +02001126 } else {
1127 ret = lyd_diff_insert(first_node, parent_node, match, NULL);
1128 }
1129 if (ret) {
Michal Vaskoe6323f62020-07-09 15:49:02 +02001130 if (op == LYD_DIFF_OP_CREATE) {
Michal Vaskod59035b2020-07-08 12:00:06 +02001131 lyd_free_tree(match);
1132 }
1133 return ret;
1134 }
1135
1136 goto next_iter_r;
1137 }
1138
1139 /* apply operation */
Michal Vaskoe6323f62020-07-09 15:49:02 +02001140 switch (op) {
1141 case LYD_DIFF_OP_NONE:
Michal Vaskod59035b2020-07-08 12:00:06 +02001142 /* find the node */
Michal Vaskoe78faec2021-04-08 17:24:43 +02001143 LY_CHECK_RET(lyd_diff_find_match(*first_node, diff_node, 1, dup_inst, &match));
Michal Vasko52afd7d2022-01-18 14:08:34 +01001144 LY_CHECK_ERR_RET(!match, LOGERR_NOINST(ctx, diff_node), LY_EINVAL);
Michal Vaskod59035b2020-07-08 12:00:06 +02001145
1146 if (match->schema->nodetype & LYD_NODE_TERM) {
1147 /* special case of only dflt flag change */
1148 if (diff_node->flags & LYD_DEFAULT) {
1149 match->flags |= LYD_DEFAULT;
1150 } else {
1151 match->flags &= ~LYD_DEFAULT;
1152 }
1153 } else {
1154 /* none operation on nodes without children is redundant and hence forbidden */
Radek Krejcia1c1e542020-09-29 16:06:52 +02001155 if (!lyd_child_no_keys(diff_node)) {
Michal Vasko0ff97752022-01-18 16:35:41 +01001156 LOGERR(ctx, LY_EINVAL, "Operation \"none\" is invalid for node \"%s\" without children.",
1157 LYD_NAME(diff_node));
1158 return LY_EINVAL;
Michal Vaskod59035b2020-07-08 12:00:06 +02001159 }
1160 }
1161 break;
Michal Vaskoe6323f62020-07-09 15:49:02 +02001162 case LYD_DIFF_OP_CREATE:
Michal Vaskod59035b2020-07-08 12:00:06 +02001163 /* duplicate the node */
Michal Vasko3a41dff2020-07-15 14:30:28 +02001164 LY_CHECK_RET(lyd_dup_single(diff_node, NULL, LYD_DUP_NO_META, &match));
Michal Vaskod59035b2020-07-08 12:00:06 +02001165
1166 /* insert it at the end */
1167 ret = 0;
Michal Vaskob104f112020-07-17 09:54:54 +02001168 if (parent_node) {
Michal Vasko19175b62022-04-01 09:17:07 +02001169 if (match->flags & LYD_EXT) {
Michal Vasko193dacd2022-10-13 08:43:05 +02001170 ret = lyplg_ext_insert(parent_node, match);
Michal Vasko19175b62022-04-01 09:17:07 +02001171 } else {
1172 ret = lyd_insert_child(parent_node, match);
1173 }
Michal Vaskod59035b2020-07-08 12:00:06 +02001174 } else {
Michal Vaskob104f112020-07-17 09:54:54 +02001175 ret = lyd_insert_sibling(*first_node, match, first_node);
Michal Vaskod59035b2020-07-08 12:00:06 +02001176 }
1177 if (ret) {
1178 lyd_free_tree(match);
1179 return ret;
1180 }
1181
1182 break;
Michal Vaskoe6323f62020-07-09 15:49:02 +02001183 case LYD_DIFF_OP_DELETE:
Michal Vaskod59035b2020-07-08 12:00:06 +02001184 /* find the node */
Michal Vaskoe78faec2021-04-08 17:24:43 +02001185 LY_CHECK_RET(lyd_diff_find_match(*first_node, diff_node, 1, dup_inst, &match));
Michal Vasko52afd7d2022-01-18 14:08:34 +01001186 LY_CHECK_ERR_RET(!match, LOGERR_NOINST(ctx, diff_node), LY_EINVAL);
Michal Vaskod59035b2020-07-08 12:00:06 +02001187
1188 /* remove it */
1189 if ((match == *first_node) && !match->parent) {
1190 assert(!parent_node);
1191 /* we have removed the top-level node */
1192 *first_node = (*first_node)->next;
1193 }
1194 lyd_free_tree(match);
1195
1196 /* we are not going recursively in this case, the whole subtree was already deleted */
1197 return LY_SUCCESS;
Michal Vaskoe6323f62020-07-09 15:49:02 +02001198 case LYD_DIFF_OP_REPLACE:
Michal Vasko0ff97752022-01-18 16:35:41 +01001199 if (!(diff_node->schema->nodetype & (LYS_LEAF | LYS_ANYDATA))) {
1200 LOGERR(ctx, LY_EINVAL, "Operation \"replace\" is invalid for %s node \"%s\".",
1201 lys_nodetype2str(diff_node->schema->nodetype), LYD_NAME(diff_node));
1202 return LY_EINVAL;
1203 }
Michal Vaskod59035b2020-07-08 12:00:06 +02001204
1205 /* find the node */
Michal Vaskoe78faec2021-04-08 17:24:43 +02001206 LY_CHECK_RET(lyd_diff_find_match(*first_node, diff_node, 1, dup_inst, &match));
Michal Vasko52afd7d2022-01-18 14:08:34 +01001207 LY_CHECK_ERR_RET(!match, LOGERR_NOINST(ctx, diff_node), LY_EINVAL);
Michal Vaskod59035b2020-07-08 12:00:06 +02001208
Michal Vaskobaba84e2021-02-05 16:33:30 +01001209 /* update the value */
1210 if (diff_node->schema->nodetype == LYS_LEAF) {
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02001211 ret = lyd_change_term(match, lyd_get_value(diff_node));
Michal Vasko52afd7d2022-01-18 14:08:34 +01001212 LY_CHECK_ERR_RET(ret && (ret != LY_EEXIST), LOGERR_UNEXPVAL(ctx, match, "data"), LY_EINVAL);
Michal Vaskobaba84e2021-02-05 16:33:30 +01001213 } else {
1214 struct lyd_node_any *any = (struct lyd_node_any *)diff_node;
Michal Vasko26bbb272022-08-02 14:54:33 +02001215
Michal Vaskoe78faec2021-04-08 17:24:43 +02001216 LY_CHECK_RET(lyd_any_copy_value(match, &any->value, any->value_type));
Michal Vaskod59035b2020-07-08 12:00:06 +02001217 }
1218
1219 /* with flags */
1220 match->flags = diff_node->flags;
1221 break;
1222 default:
1223 LOGINT_RET(ctx);
1224 }
1225
1226next_iter_r:
1227 if (diff_cb) {
1228 /* call callback */
1229 LY_CHECK_RET(diff_cb(diff_node, match, cb_data));
1230 }
1231
1232 /* apply diff recursively */
Michal Vaskoe78faec2021-04-08 17:24:43 +02001233 ret = LY_SUCCESS;
Radek Krejcia1c1e542020-09-29 16:06:52 +02001234 LY_LIST_FOR(lyd_child_no_keys(diff_node), diff_child) {
Michal Vaskoe78faec2021-04-08 17:24:43 +02001235 ret = lyd_diff_apply_r(lyd_node_child_p(match), match, diff_child, diff_cb, cb_data, &child_dup_inst);
1236 if (ret) {
1237 break;
1238 }
Michal Vaskod59035b2020-07-08 12:00:06 +02001239 }
1240
Michal Vaskod7c048c2021-05-18 16:12:55 +02001241 lyd_dup_inst_free(child_dup_inst);
Michal Vaskoe78faec2021-04-08 17:24:43 +02001242 return ret;
Michal Vaskod59035b2020-07-08 12:00:06 +02001243}
1244
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001245LIBYANG_API_DEF LY_ERR
Michal Vaskod59035b2020-07-08 12:00:06 +02001246lyd_diff_apply_module(struct lyd_node **data, const struct lyd_node *diff, const struct lys_module *mod,
Radek Krejci0f969882020-08-21 16:56:47 +02001247 lyd_diff_cb diff_cb, void *cb_data)
Michal Vaskod59035b2020-07-08 12:00:06 +02001248{
1249 const struct lyd_node *root;
Michal Vasko271d2e32023-01-31 15:43:19 +01001250 struct hash_table *dup_inst = NULL;
Michal Vaskoe78faec2021-04-08 17:24:43 +02001251 LY_ERR ret = LY_SUCCESS;
Michal Vaskod59035b2020-07-08 12:00:06 +02001252
1253 LY_LIST_FOR(diff, root) {
1254 if (mod && (lyd_owner_module(root) != mod)) {
1255 /* skip data nodes from different modules */
1256 continue;
1257 }
1258
1259 /* apply relevant nodes from the diff datatree */
Michal Vaskoe78faec2021-04-08 17:24:43 +02001260 ret = lyd_diff_apply_r(data, NULL, root, diff_cb, cb_data, &dup_inst);
1261 if (ret) {
1262 break;
1263 }
Michal Vaskod59035b2020-07-08 12:00:06 +02001264 }
1265
Michal Vaskod7c048c2021-05-18 16:12:55 +02001266 lyd_dup_inst_free(dup_inst);
Michal Vaskoe78faec2021-04-08 17:24:43 +02001267 return ret;
Michal Vaskod59035b2020-07-08 12:00:06 +02001268}
1269
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001270LIBYANG_API_DEF LY_ERR
Michal Vasko3a41dff2020-07-15 14:30:28 +02001271lyd_diff_apply_all(struct lyd_node **data, const struct lyd_node *diff)
Michal Vaskod59035b2020-07-08 12:00:06 +02001272{
1273 return lyd_diff_apply_module(data, diff, NULL, NULL, NULL);
1274}
Michal Vaskoe6323f62020-07-09 15:49:02 +02001275
1276/**
1277 * @brief Update operations on a diff node when the new operation is NONE.
1278 *
1279 * @param[in] diff_match Node from the diff.
Michal Vaskoe78faec2021-04-08 17:24:43 +02001280 * @param[in] cur_op Current operation of @p diff_match.
Michal Vaskoe6323f62020-07-09 15:49:02 +02001281 * @param[in] src_diff Current source diff node.
1282 * @return LY_ERR value.
1283 */
1284static LY_ERR
1285lyd_diff_merge_none(struct lyd_node *diff_match, enum lyd_diff_op cur_op, const struct lyd_node *src_diff)
1286{
1287 switch (cur_op) {
1288 case LYD_DIFF_OP_NONE:
1289 case LYD_DIFF_OP_CREATE:
1290 case LYD_DIFF_OP_REPLACE:
1291 if (src_diff->schema->nodetype & LYD_NODE_TERM) {
1292 /* NONE on a term means only its dflt flag was changed */
1293 diff_match->flags &= ~LYD_DEFAULT;
1294 diff_match->flags |= src_diff->flags & LYD_DEFAULT;
1295 }
1296 break;
1297 default:
1298 /* delete operation is not valid */
Michal Vasko52afd7d2022-01-18 14:08:34 +01001299 LOGERR_MERGEOP(LYD_CTX(diff_match), diff_match, cur_op, LYD_DIFF_OP_NONE);
1300 return LY_EINVAL;
Michal Vaskoe6323f62020-07-09 15:49:02 +02001301 }
1302
1303 return LY_SUCCESS;
1304}
1305
1306/**
1307 * @brief Remove an attribute from a node.
1308 *
1309 * @param[in] node Node with the metadata.
1310 * @param[in] name Metadata name.
1311 */
1312static void
1313lyd_diff_del_meta(struct lyd_node *node, const char *name)
1314{
1315 struct lyd_meta *meta;
1316
1317 LY_LIST_FOR(node->meta, meta) {
1318 if (!strcmp(meta->name, name) && !strcmp(meta->annotation->module->name, "yang")) {
Michal Vasko3a41dff2020-07-15 14:30:28 +02001319 lyd_free_meta_single(meta);
Michal Vaskoe6323f62020-07-09 15:49:02 +02001320 return;
1321 }
1322 }
1323
1324 assert(0);
1325}
1326
1327/**
1328 * @brief Set a specific operation of a node. Delete the previous operation, if any.
Michal Vasko871a0252020-11-11 18:35:24 +01001329 * Does not change the default flag.
Michal Vaskoe6323f62020-07-09 15:49:02 +02001330 *
1331 * @param[in] node Node to change.
1332 * @param[in] op Operation to set.
1333 * @return LY_ERR value.
1334 */
1335static LY_ERR
1336lyd_diff_change_op(struct lyd_node *node, enum lyd_diff_op op)
1337{
1338 struct lyd_meta *meta;
1339
1340 LY_LIST_FOR(node->meta, meta) {
1341 if (!strcmp(meta->name, "operation") && !strcmp(meta->annotation->module->name, "yang")) {
Michal Vasko3a41dff2020-07-15 14:30:28 +02001342 lyd_free_meta_single(meta);
Michal Vaskoe6323f62020-07-09 15:49:02 +02001343 break;
1344 }
1345 }
1346
Michal Vasko871a0252020-11-11 18:35:24 +01001347 return lyd_new_meta(LYD_CTX(node), node, NULL, "yang:operation", lyd_diff_op2str(op), 0, NULL);
Michal Vaskoe6323f62020-07-09 15:49:02 +02001348}
1349
1350/**
1351 * @brief Update operations on a diff node when the new operation is REPLACE.
1352 *
1353 * @param[in] diff_match Node from the diff.
Michal Vaskoe78faec2021-04-08 17:24:43 +02001354 * @param[in] cur_op Current operation of @p diff_match.
Michal Vaskoe6323f62020-07-09 15:49:02 +02001355 * @param[in] src_diff Current source diff node.
1356 * @return LY_ERR value.
1357 */
1358static LY_ERR
1359lyd_diff_merge_replace(struct lyd_node *diff_match, enum lyd_diff_op cur_op, const struct lyd_node *src_diff)
1360{
1361 LY_ERR ret;
Michal Vaskoe78faec2021-04-08 17:24:43 +02001362 const char *str_val, *meta_name, *orig_meta_name;
Michal Vaskoe6323f62020-07-09 15:49:02 +02001363 struct lyd_meta *meta;
1364 const struct lys_module *mod;
1365 const struct lyd_node_any *any;
Michal Vasko52afd7d2022-01-18 14:08:34 +01001366 const struct ly_ctx *ctx = LYD_CTX(diff_match);
Michal Vaskoe6323f62020-07-09 15:49:02 +02001367
1368 /* get "yang" module for the metadata */
Michal Vaskob7be7a82020-08-20 09:09:04 +02001369 mod = ly_ctx_get_module_latest(LYD_CTX(diff_match), "yang");
Michal Vaskoe6323f62020-07-09 15:49:02 +02001370 assert(mod);
1371
1372 switch (cur_op) {
1373 case LYD_DIFF_OP_REPLACE:
1374 case LYD_DIFF_OP_CREATE:
1375 switch (diff_match->schema->nodetype) {
1376 case LYS_LIST:
1377 case LYS_LEAFLIST:
Michal Vasko4231fb62020-07-13 13:54:47 +02001378 /* it was created/moved somewhere, but now it will be created/moved somewhere else,
Michal Vaskoe6323f62020-07-09 15:49:02 +02001379 * keep orig_key/orig_value (only replace oper) and replace key/value */
1380 assert(lysc_is_userordered(diff_match->schema));
Michal Vaskoe78faec2021-04-08 17:24:43 +02001381 if (lysc_is_dup_inst_list(diff_match->schema)) {
1382 meta_name = "position";
1383 } else if (diff_match->schema->nodetype == LYS_LIST) {
1384 meta_name = "key";
1385 } else {
1386 meta_name = "value";
1387 }
Michal Vaskoe6323f62020-07-09 15:49:02 +02001388
1389 lyd_diff_del_meta(diff_match, meta_name);
1390 meta = lyd_find_meta(src_diff->meta, mod, meta_name);
Michal Vasko52afd7d2022-01-18 14:08:34 +01001391 LY_CHECK_ERR_RET(!meta, LOGERR_META(ctx, meta_name, src_diff), LY_EINVAL);
Michal Vasko3a41dff2020-07-15 14:30:28 +02001392 LY_CHECK_RET(lyd_dup_meta_single(meta, diff_match, NULL));
Michal Vaskoe6323f62020-07-09 15:49:02 +02001393 break;
1394 case LYS_LEAF:
1395 /* replaced with the exact same value, impossible */
Michal Vasko8f359bf2020-07-28 10:41:15 +02001396 if (!lyd_compare_single(diff_match, src_diff, 0)) {
Michal Vasko52afd7d2022-01-18 14:08:34 +01001397 LOGERR_UNEXPVAL(ctx, diff_match, "target diff");
1398 return LY_EINVAL;
Michal Vaskoe6323f62020-07-09 15:49:02 +02001399 }
1400
Michal Vaskoe6323f62020-07-09 15:49:02 +02001401 /* modify the node value */
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02001402 if (lyd_change_term(diff_match, lyd_get_value(src_diff))) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02001403 LOGINT_RET(LYD_CTX(src_diff));
Michal Vaskoe6323f62020-07-09 15:49:02 +02001404 }
1405
Michal Vasko8caadab2020-11-05 17:38:15 +01001406 if (cur_op == LYD_DIFF_OP_REPLACE) {
1407 /* compare values whether there is any change at all */
1408 meta = lyd_find_meta(diff_match->meta, mod, "orig-value");
Michal Vasko52afd7d2022-01-18 14:08:34 +01001409 LY_CHECK_ERR_RET(!meta, LOGERR_META(ctx, "orig-value", diff_match), LY_EINVAL);
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02001410 str_val = lyd_get_meta_value(meta);
Michal Vasko8caadab2020-11-05 17:38:15 +01001411 ret = lyd_value_compare((struct lyd_node_term *)diff_match, str_val, strlen(str_val));
1412 if (!ret) {
1413 /* values are the same, remove orig-value meta and set oper to NONE */
1414 lyd_free_meta_single(meta);
1415 LY_CHECK_RET(lyd_diff_change_op(diff_match, LYD_DIFF_OP_NONE));
1416 }
Michal Vaskoe6323f62020-07-09 15:49:02 +02001417 }
1418
1419 /* modify the default flag */
1420 diff_match->flags &= ~LYD_DEFAULT;
1421 diff_match->flags |= src_diff->flags & LYD_DEFAULT;
1422 break;
1423 case LYS_ANYXML:
1424 case LYS_ANYDATA:
Michal Vasko8f359bf2020-07-28 10:41:15 +02001425 if (!lyd_compare_single(diff_match, src_diff, 0)) {
Michal Vasko52afd7d2022-01-18 14:08:34 +01001426 LOGERR_UNEXPVAL(ctx, diff_match, "target diff");
1427 return LY_EINVAL;
Michal Vaskoe6323f62020-07-09 15:49:02 +02001428 }
1429
1430 /* modify the node value */
1431 any = (struct lyd_node_any *)src_diff;
1432 LY_CHECK_RET(lyd_any_copy_value(diff_match, &any->value, any->value_type));
1433 break;
1434 default:
Michal Vaskob7be7a82020-08-20 09:09:04 +02001435 LOGINT_RET(LYD_CTX(src_diff));
Michal Vaskoe6323f62020-07-09 15:49:02 +02001436 }
1437 break;
1438 case LYD_DIFF_OP_NONE:
1439 /* it is moved now */
1440 assert(lysc_is_userordered(diff_match->schema) && (diff_match->schema->nodetype == LYS_LIST));
1441
1442 /* change the operation */
1443 LY_CHECK_RET(lyd_diff_change_op(diff_match, LYD_DIFF_OP_REPLACE));
1444
Michal Vaskoe78faec2021-04-08 17:24:43 +02001445 /* set orig-meta and meta */
1446 if (lysc_is_dup_inst_list(diff_match->schema)) {
1447 meta_name = "position";
1448 orig_meta_name = "orig-position";
1449 } else {
1450 meta_name = "key";
1451 orig_meta_name = "orig-key";
1452 }
1453
1454 meta = lyd_find_meta(src_diff->meta, mod, orig_meta_name);
Michal Vasko52afd7d2022-01-18 14:08:34 +01001455 LY_CHECK_ERR_RET(!meta, LOGERR_META(ctx, orig_meta_name, src_diff), LY_EINVAL);
Michal Vasko3a41dff2020-07-15 14:30:28 +02001456 LY_CHECK_RET(lyd_dup_meta_single(meta, diff_match, NULL));
Michal Vaskoe6323f62020-07-09 15:49:02 +02001457
Michal Vaskoe78faec2021-04-08 17:24:43 +02001458 meta = lyd_find_meta(src_diff->meta, mod, meta_name);
Michal Vasko52afd7d2022-01-18 14:08:34 +01001459 LY_CHECK_ERR_RET(!meta, LOGERR_META(ctx, meta_name, src_diff), LY_EINVAL);
Michal Vasko3a41dff2020-07-15 14:30:28 +02001460 LY_CHECK_RET(lyd_dup_meta_single(meta, diff_match, NULL));
Michal Vaskoe6323f62020-07-09 15:49:02 +02001461 break;
1462 default:
1463 /* delete operation is not valid */
Michal Vasko52afd7d2022-01-18 14:08:34 +01001464 LOGERR_MERGEOP(ctx, diff_match, cur_op, LYD_DIFF_OP_REPLACE);
1465 return LY_EINVAL;
Michal Vaskoe6323f62020-07-09 15:49:02 +02001466 }
1467
1468 return LY_SUCCESS;
1469}
1470
1471/**
1472 * @brief Update operations in a diff node when the new operation is CREATE.
1473 *
1474 * @param[in] diff_match Node from the diff.
Michal Vaskoe78faec2021-04-08 17:24:43 +02001475 * @param[in] cur_op Current operation of @p diff_match.
Michal Vaskoe6323f62020-07-09 15:49:02 +02001476 * @param[in] src_diff Current source diff node.
Michal Vaskoc0e58e82020-11-11 19:04:33 +01001477 * @param[in] options Diff merge options.
Michal Vaskoe6323f62020-07-09 15:49:02 +02001478 * @return LY_ERR value.
1479 */
1480static LY_ERR
Michal Vaskoc0e58e82020-11-11 19:04:33 +01001481lyd_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 +02001482{
1483 struct lyd_node *child;
Michal Vaskoc0e58e82020-11-11 19:04:33 +01001484 const struct lysc_node_leaf *sleaf = NULL;
Michal Vasko871a0252020-11-11 18:35:24 +01001485 uint32_t trg_flags;
Michal Vaskoe78faec2021-04-08 17:24:43 +02001486 const char *meta_name, *orig_meta_name;
1487 struct lyd_meta *meta, *orig_meta;
Michal Vasko52afd7d2022-01-18 14:08:34 +01001488 const struct ly_ctx *ctx = LYD_CTX(diff_match);
Michal Vaskoe6323f62020-07-09 15:49:02 +02001489
1490 switch (cur_op) {
1491 case LYD_DIFF_OP_DELETE:
Michal Vasko871a0252020-11-11 18:35:24 +01001492 /* remember current flags */
1493 trg_flags = diff_match->flags;
1494
Michal Vaskoe78faec2021-04-08 17:24:43 +02001495 if (lysc_is_userordered(diff_match->schema)) {
1496 /* get anchor metadata */
1497 if (lysc_is_dup_inst_list(diff_match->schema)) {
1498 meta_name = "yang:position";
1499 orig_meta_name = "yang:orig-position";
1500 } else if (diff_match->schema->nodetype == LYS_LIST) {
1501 meta_name = "yang:key";
1502 orig_meta_name = "yang:orig-key";
1503 } else {
1504 meta_name = "yang:value";
1505 orig_meta_name = "yang:orig-value";
1506 }
1507 meta = lyd_find_meta(src_diff->meta, NULL, meta_name);
Michal Vasko52afd7d2022-01-18 14:08:34 +01001508 LY_CHECK_ERR_RET(!meta, LOGERR_META(ctx, meta_name, src_diff), LY_EINVAL);
Michal Vaskoe78faec2021-04-08 17:24:43 +02001509 orig_meta = lyd_find_meta(diff_match->meta, NULL, orig_meta_name);
Michal Vasko52afd7d2022-01-18 14:08:34 +01001510 LY_CHECK_ERR_RET(!orig_meta, LOGERR_META(ctx, orig_meta_name, diff_match), LY_EINVAL);
Michal Vaskoe78faec2021-04-08 17:24:43 +02001511
1512 /* the (incorrect) assumption made here is that there are no previous diff nodes that would affect
1513 * the anchors stored in the metadata */
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02001514 if (strcmp(lyd_get_meta_value(meta), lyd_get_meta_value(orig_meta))) {
Michal Vaskoe78faec2021-04-08 17:24:43 +02001515 /* deleted + created at another position -> operation REPLACE */
1516 LY_CHECK_RET(lyd_diff_change_op(diff_match, LYD_DIFF_OP_REPLACE));
1517
1518 /* add anchor metadata */
1519 LY_CHECK_RET(lyd_dup_meta_single(meta, diff_match, NULL));
1520 } else {
1521 /* deleted + created at the same position -> operation NONE */
1522 LY_CHECK_RET(lyd_diff_change_op(diff_match, LYD_DIFF_OP_NONE));
1523
1524 /* delete anchor metadata */
1525 lyd_free_meta_single(orig_meta);
1526 }
1527 } else if (diff_match->schema->nodetype == LYS_LEAF) {
1528 if (options & LYD_DIFF_MERGE_DEFAULTS) {
1529 /* we are dealing with a leaf and are handling default values specially (as explicit nodes) */
1530 sleaf = (struct lysc_node_leaf *)diff_match->schema;
1531 }
1532
Radek Krejci55c4bd22021-04-26 08:09:04 +02001533 if (sleaf && sleaf->dflt && !sleaf->dflt->realtype->plugin->compare(sleaf->dflt,
1534 &((struct lyd_node_term *)src_diff)->value)) {
Michal Vaskoe78faec2021-04-08 17:24:43 +02001535 /* we deleted it, so a default value was in-use, and it matches the created value -> operation NONE */
1536 LY_CHECK_RET(lyd_diff_change_op(diff_match, LYD_DIFF_OP_NONE));
1537 } else if (!lyd_compare_single(diff_match, src_diff, 0)) {
1538 /* deleted + created -> operation NONE */
1539 LY_CHECK_RET(lyd_diff_change_op(diff_match, LYD_DIFF_OP_NONE));
1540 } else {
1541 /* we deleted it, but it was created with a different value -> operation REPLACE */
1542 LY_CHECK_RET(lyd_diff_change_op(diff_match, LYD_DIFF_OP_REPLACE));
1543
1544 /* current value is the previous one (meta) */
1545 LY_CHECK_RET(lyd_new_meta(LYD_CTX(src_diff), diff_match, NULL, "yang:orig-value",
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02001546 lyd_get_value(diff_match), 0, NULL));
Michal Vaskoe78faec2021-04-08 17:24:43 +02001547
1548 /* update the value itself */
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02001549 LY_CHECK_RET(lyd_change_term(diff_match, lyd_get_value(src_diff)));
Michal Vaskoe78faec2021-04-08 17:24:43 +02001550 }
1551 } else {
Michal Vaskoe6323f62020-07-09 15:49:02 +02001552 /* deleted + created -> operation NONE */
1553 LY_CHECK_RET(lyd_diff_change_op(diff_match, LYD_DIFF_OP_NONE));
Michal Vaskoe6323f62020-07-09 15:49:02 +02001554 }
1555
1556 if (diff_match->schema->nodetype & LYD_NODE_TERM) {
Michal Vasko4b715ca2020-11-11 18:39:57 +01001557 /* add orig-dflt metadata */
1558 LY_CHECK_RET(lyd_new_meta(LYD_CTX(src_diff), diff_match, NULL, "yang:orig-default",
1559 trg_flags & LYD_DEFAULT ? "true" : "false", 0, NULL));
1560
Michal Vaskoe6323f62020-07-09 15:49:02 +02001561 /* update dflt flag itself */
1562 diff_match->flags &= ~LYD_DEFAULT;
1563 diff_match->flags |= src_diff->flags & LYD_DEFAULT;
Michal Vaskoe78faec2021-04-08 17:24:43 +02001564 }
1565
1566 /* but the operation of its children should remain DELETE */
1567 LY_LIST_FOR(lyd_child_no_keys(diff_match), child) {
1568 LY_CHECK_RET(lyd_diff_change_op(child, LYD_DIFF_OP_DELETE));
Michal Vaskoe6323f62020-07-09 15:49:02 +02001569 }
1570 break;
1571 default:
1572 /* create and replace operations are not valid */
Michal Vasko52afd7d2022-01-18 14:08:34 +01001573 LOGERR_MERGEOP(LYD_CTX(src_diff), diff_match, cur_op, LYD_DIFF_OP_CREATE);
1574 return LY_EINVAL;
Michal Vaskoe6323f62020-07-09 15:49:02 +02001575 }
1576
1577 return LY_SUCCESS;
1578}
1579
1580/**
1581 * @brief Update operations on a diff node when the new operation is DELETE.
1582 *
1583 * @param[in] diff_match Node from the diff.
Michal Vaskoe78faec2021-04-08 17:24:43 +02001584 * @param[in] cur_op Current operation of @p diff_match.
Michal Vaskoe6323f62020-07-09 15:49:02 +02001585 * @param[in] src_diff Current source diff node.
1586 * @return LY_ERR value.
1587 */
1588static LY_ERR
1589lyd_diff_merge_delete(struct lyd_node *diff_match, enum lyd_diff_op cur_op, const struct lyd_node *src_diff)
1590{
Michal Vasko17d0c5c2021-11-01 11:31:11 +01001591 struct lyd_node *child;
Michal Vaskoe78faec2021-04-08 17:24:43 +02001592 struct lyd_meta *meta;
1593 const char *meta_name;
Michal Vasko52afd7d2022-01-18 14:08:34 +01001594 const struct ly_ctx *ctx = LYD_CTX(diff_match);
Michal Vaskoe6323f62020-07-09 15:49:02 +02001595
1596 /* we can delete only exact existing nodes */
Michal Vaskob7be7a82020-08-20 09:09:04 +02001597 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 +02001598
1599 switch (cur_op) {
1600 case LYD_DIFF_OP_CREATE:
1601 /* it was created, but then deleted -> set NONE operation */
1602 LY_CHECK_RET(lyd_diff_change_op(diff_match, LYD_DIFF_OP_NONE));
1603
1604 if (diff_match->schema->nodetype & LYD_NODE_TERM) {
1605 /* add orig-default meta because it is expected */
Michal Vasko871a0252020-11-11 18:35:24 +01001606 LY_CHECK_RET(lyd_new_meta(LYD_CTX(src_diff), diff_match, NULL, "yang:orig-default",
1607 diff_match->flags & LYD_DEFAULT ? "true" : "false", 0, NULL));
Michal Vaskoe78faec2021-04-08 17:24:43 +02001608 } else if (!lysc_is_dup_inst_list(diff_match->schema)) {
Michal Vaskoe6323f62020-07-09 15:49:02 +02001609 /* keep operation for all descendants (for now) */
Radek Krejcia1c1e542020-09-29 16:06:52 +02001610 LY_LIST_FOR(lyd_child_no_keys(diff_match), child) {
Michal Vaskoe6323f62020-07-09 15:49:02 +02001611 LY_CHECK_RET(lyd_diff_change_op(child, cur_op));
1612 }
Michal Vaskoe78faec2021-04-08 17:24:43 +02001613 } /* else key-less list, for which all the descendants act as keys */
Michal Vaskoe6323f62020-07-09 15:49:02 +02001614 break;
1615 case LYD_DIFF_OP_REPLACE:
Michal Vasko17d0c5c2021-11-01 11:31:11 +01001616 /* remove the redundant metadata */
Michal Vaskoe78faec2021-04-08 17:24:43 +02001617 if (lysc_is_userordered(diff_match->schema)) {
1618 if (lysc_is_dup_inst_list(diff_match->schema)) {
1619 meta_name = "position";
1620 } else if (diff_match->schema->nodetype == LYS_LIST) {
1621 meta_name = "key";
1622 } else {
1623 meta_name = "value";
1624 }
1625 } else {
1626 assert(diff_match->schema->nodetype == LYS_LEAF);
1627
1628 /* switch value for the original one */
1629 meta = lyd_find_meta(diff_match->meta, NULL, "yang:orig-value");
Michal Vasko52afd7d2022-01-18 14:08:34 +01001630 LY_CHECK_ERR_RET(!meta, LOGERR_META(ctx, "yang:orig-value", diff_match), LY_EINVAL);
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02001631 if (lyd_change_term(diff_match, lyd_get_meta_value(meta))) {
Michal Vasko52afd7d2022-01-18 14:08:34 +01001632 LOGERR_UNEXPVAL(ctx, diff_match, "target diff");
1633 return LY_EINVAL;
Michal Vaskoe78faec2021-04-08 17:24:43 +02001634 }
1635
1636 /* switch default for the original one, then remove the meta */
1637 meta = lyd_find_meta(diff_match->meta, NULL, "yang:orig-default");
Michal Vasko52afd7d2022-01-18 14:08:34 +01001638 LY_CHECK_ERR_RET(!meta, LOGERR_META(ctx, "yang:orig-default", diff_match), LY_EINVAL);
Michal Vaskoe78faec2021-04-08 17:24:43 +02001639 diff_match->flags &= ~LYD_DEFAULT;
1640 if (meta->value.boolean) {
1641 diff_match->flags |= LYD_DEFAULT;
1642 }
1643 lyd_free_meta_single(meta);
1644
1645 meta_name = "orig-value";
1646 }
1647 lyd_diff_del_meta(diff_match, meta_name);
1648
Michal Vasko17d0c5c2021-11-01 11:31:11 +01001649 /* it was being changed, but should be deleted instead -> set DELETE operation */
1650 LY_CHECK_RET(lyd_diff_change_op(diff_match, LYD_DIFF_OP_DELETE));
1651 break;
Michal Vaskoe6323f62020-07-09 15:49:02 +02001652 case LYD_DIFF_OP_NONE:
1653 /* it was not modified, but should be deleted -> set DELETE operation */
1654 LY_CHECK_RET(lyd_diff_change_op(diff_match, LYD_DIFF_OP_DELETE));
Michal Vaskoe6323f62020-07-09 15:49:02 +02001655 break;
1656 default:
1657 /* delete operation is not valid */
Michal Vasko52afd7d2022-01-18 14:08:34 +01001658 LOGERR_MERGEOP(LYD_CTX(diff_match), diff_match, cur_op, LYD_DIFF_OP_DELETE);
1659 return LY_EINVAL;
Michal Vaskoe6323f62020-07-09 15:49:02 +02001660 }
1661
1662 return LY_SUCCESS;
1663}
1664
1665/**
1666 * @brief Check whether this diff node is redundant (does not change data).
1667 *
1668 * @param[in] diff Diff node.
1669 * @return 0 if not, non-zero if it is.
1670 */
1671static int
1672lyd_diff_is_redundant(struct lyd_node *diff)
1673{
1674 enum lyd_diff_op op;
1675 struct lyd_meta *meta, *orig_val_meta = NULL, *val_meta = NULL;
1676 struct lyd_node *child;
1677 const struct lys_module *mod;
Michal Vaskoe78faec2021-04-08 17:24:43 +02001678 const char *str, *orig_meta_name, *meta_name;
Michal Vaskoe6323f62020-07-09 15:49:02 +02001679
1680 assert(diff);
1681
Michal Vaskoe78faec2021-04-08 17:24:43 +02001682 if (lysc_is_dup_inst_list(diff->schema)) {
1683 /* all descendants are keys */
1684 child = NULL;
1685 } else {
1686 child = lyd_child_no_keys(diff);
1687 }
Michal Vaskob7be7a82020-08-20 09:09:04 +02001688 mod = ly_ctx_get_module_latest(LYD_CTX(diff), "yang");
Michal Vaskoe6323f62020-07-09 15:49:02 +02001689 assert(mod);
1690
1691 /* get node operation */
Michal Vasko53bf6f22020-07-14 08:23:40 +02001692 LY_CHECK_RET(lyd_diff_get_op(diff, &op), 0);
Michal Vaskoe6323f62020-07-09 15:49:02 +02001693
1694 if ((op == LYD_DIFF_OP_REPLACE) && lysc_is_userordered(diff->schema)) {
Michal Vaskoe78faec2021-04-08 17:24:43 +02001695 /* get metadata names */
1696 if (lysc_is_dup_inst_list(diff->schema)) {
1697 meta_name = "position";
1698 orig_meta_name = "orig-position";
1699 } else if (diff->schema->nodetype == LYS_LIST) {
1700 meta_name = "key";
1701 orig_meta_name = "orig-key";
1702 } else {
1703 meta_name = "value";
1704 orig_meta_name = "orig-value";
1705 }
1706
Michal Vaskoe6323f62020-07-09 15:49:02 +02001707 /* check for redundant move */
Michal Vaskoe78faec2021-04-08 17:24:43 +02001708 orig_val_meta = lyd_find_meta(diff->meta, mod, orig_meta_name);
1709 val_meta = lyd_find_meta(diff->meta, mod, meta_name);
Michal Vaskoe6323f62020-07-09 15:49:02 +02001710 assert(orig_val_meta && val_meta);
1711
1712 if (!lyd_compare_meta(orig_val_meta, val_meta)) {
1713 /* there is actually no move */
Michal Vasko3a41dff2020-07-15 14:30:28 +02001714 lyd_free_meta_single(orig_val_meta);
1715 lyd_free_meta_single(val_meta);
Michal Vaskoe6323f62020-07-09 15:49:02 +02001716 if (child) {
1717 /* change operation to NONE, we have siblings */
1718 lyd_diff_change_op(diff, LYD_DIFF_OP_NONE);
1719 return 0;
1720 }
1721
1722 /* redundant node, BUT !!
1723 * In diff the move operation is always converted to be INSERT_AFTER, which is fine
1724 * because the data that this is applied on should not change for the diff lifetime.
1725 * However, when we are merging 2 diffs, this conversion is actually lossy because
1726 * if the data change, the move operation can also change its meaning. In this specific
1727 * case the move operation will be lost. But it can be considered a feature, it is not supported.
1728 */
1729 return 1;
1730 }
1731 } else if ((op == LYD_DIFF_OP_NONE) && (diff->schema->nodetype & LYD_NODE_TERM)) {
1732 /* check whether at least the default flags are different */
1733 meta = lyd_find_meta(diff->meta, mod, "orig-default");
1734 assert(meta);
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02001735 str = lyd_get_meta_value(meta);
Michal Vaskoe6323f62020-07-09 15:49:02 +02001736
1737 /* if previous and current dflt flags are the same, this node is redundant */
1738 if ((!strcmp(str, "true") && (diff->flags & LYD_DEFAULT)) || (!strcmp(str, "false") && !(diff->flags & LYD_DEFAULT))) {
1739 return 1;
1740 }
1741 return 0;
1742 }
1743
1744 if (!child && (op == LYD_DIFF_OP_NONE)) {
1745 return 1;
1746 }
1747
1748 return 0;
1749}
1750
1751/**
Michal Vaskoe78faec2021-04-08 17:24:43 +02001752 * @brief Merge sysrepo diff subtree with another diff, recursively.
Michal Vaskoe6323f62020-07-09 15:49:02 +02001753 *
1754 * @param[in] src_diff Source diff node.
1755 * @param[in] diff_parent Current sysrepo diff parent.
1756 * @param[in] diff_cb Optional diff callback.
1757 * @param[in] cb_data User data for @p diff_cb.
Michal Vaskoe78faec2021-04-08 17:24:43 +02001758 * @param[in,out] dup_inst Duplicate instance cache for all @p src_diff siblings.
Michal Vaskoc0e58e82020-11-11 19:04:33 +01001759 * @param[in] options Diff merge options.
Michal Vaskoe6323f62020-07-09 15:49:02 +02001760 * @param[in,out] diff Diff root node.
1761 * @return LY_ERR value.
1762 */
1763static LY_ERR
1764lyd_diff_merge_r(const struct lyd_node *src_diff, struct lyd_node *diff_parent, lyd_diff_cb diff_cb, void *cb_data,
Michal Vasko271d2e32023-01-31 15:43:19 +01001765 struct hash_table **dup_inst, uint16_t options, struct lyd_node **diff)
Michal Vaskoe6323f62020-07-09 15:49:02 +02001766{
1767 LY_ERR ret = LY_SUCCESS;
1768 struct lyd_node *child, *diff_node = NULL;
1769 enum lyd_diff_op src_op, cur_op;
Michal Vasko271d2e32023-01-31 15:43:19 +01001770 struct hash_table *child_dup_inst = NULL;
Michal Vaskoe6323f62020-07-09 15:49:02 +02001771
1772 /* get source node operation */
1773 LY_CHECK_RET(lyd_diff_get_op(src_diff, &src_op));
1774
1775 /* find an equal node in the current diff */
Michal Vaskoe78faec2021-04-08 17:24:43 +02001776 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 +02001777
1778 if (diff_node) {
1779 /* get target (current) operation */
1780 LY_CHECK_RET(lyd_diff_get_op(diff_node, &cur_op));
1781
1782 /* merge operations */
1783 switch (src_op) {
1784 case LYD_DIFF_OP_REPLACE:
1785 ret = lyd_diff_merge_replace(diff_node, cur_op, src_diff);
1786 break;
1787 case LYD_DIFF_OP_CREATE:
Michal Vasko1dc0a842021-02-04 11:04:57 +01001788 if ((cur_op == LYD_DIFF_OP_CREATE) && lysc_is_dup_inst_list(diff_node->schema)) {
Michal Vaskoe78faec2021-04-08 17:24:43 +02001789 /* special case of creating duplicate (leaf-)list instances */
Michal Vasko1dc0a842021-02-04 11:04:57 +01001790 goto add_diff;
1791 }
1792
Michal Vaskoc0e58e82020-11-11 19:04:33 +01001793 ret = lyd_diff_merge_create(diff_node, cur_op, src_diff, options);
Michal Vaskoe6323f62020-07-09 15:49:02 +02001794 break;
1795 case LYD_DIFF_OP_DELETE:
1796 ret = lyd_diff_merge_delete(diff_node, cur_op, src_diff);
1797 break;
1798 case LYD_DIFF_OP_NONE:
Michal Vaskoe78faec2021-04-08 17:24:43 +02001799 /* key-less list can never have "none" operation since all its descendants are acting as "keys" */
1800 assert((src_diff->schema->nodetype != LYS_LIST) || !lysc_is_dup_inst_list(src_diff->schema));
Michal Vaskoe6323f62020-07-09 15:49:02 +02001801 ret = lyd_diff_merge_none(diff_node, cur_op, src_diff);
1802 break;
1803 default:
Michal Vaskob7be7a82020-08-20 09:09:04 +02001804 LOGINT_RET(LYD_CTX(src_diff));
Michal Vaskoe6323f62020-07-09 15:49:02 +02001805 }
1806 if (ret) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02001807 LOGERR(LYD_CTX(src_diff), LY_EOTHER, "Merging operation \"%s\" failed.", lyd_diff_op2str(src_op));
Michal Vaskoe6323f62020-07-09 15:49:02 +02001808 return ret;
1809 }
1810
1811 if (diff_cb) {
1812 /* call callback */
Michal Vaskobc5fba92020-08-07 12:14:39 +02001813 LY_CHECK_RET(diff_cb(src_diff, diff_node, cb_data));
Michal Vaskoe6323f62020-07-09 15:49:02 +02001814 }
1815
1816 /* update diff parent */
1817 diff_parent = diff_node;
1818
Michal Vaskoe78faec2021-04-08 17:24:43 +02001819 /* for diff purposes, all key-less list descendants actually act as keys (identifying the same instances),
1820 * so there is nothing to merge for these "keys" */
1821 if (!lysc_is_dup_inst_list(src_diff->schema)) {
1822 /* merge src_diff recursively */
1823 LY_LIST_FOR(lyd_child_no_keys(src_diff), child) {
1824 ret = lyd_diff_merge_r(child, diff_parent, diff_cb, cb_data, &child_dup_inst, options, diff);
1825 if (ret) {
1826 break;
1827 }
1828 }
Michal Vaskod7c048c2021-05-18 16:12:55 +02001829 lyd_dup_inst_free(child_dup_inst);
Michal Vaskoe78faec2021-04-08 17:24:43 +02001830 LY_CHECK_RET(ret);
Michal Vaskoe6323f62020-07-09 15:49:02 +02001831 }
1832 } else {
Michal Vasko1dc0a842021-02-04 11:04:57 +01001833add_diff:
Michal Vaskoe6323f62020-07-09 15:49:02 +02001834 /* add new diff node with all descendants */
Michal Vasko9ad76852022-07-12 10:18:03 +02001835 if ((src_diff->flags & LYD_EXT) && diff_parent) {
1836 LY_CHECK_RET(lyd_dup_single_to_ctx(src_diff, LYD_CTX(diff_parent), (struct lyd_node_inner *)diff_parent,
1837 LYD_DUP_RECURSIVE | LYD_DUP_WITH_FLAGS, &diff_node));
1838 } else {
1839 LY_CHECK_RET(lyd_dup_single(src_diff, (struct lyd_node_inner *)diff_parent,
1840 LYD_DUP_RECURSIVE | LYD_DUP_WITH_FLAGS, &diff_node));
1841 }
Michal Vaskoe6323f62020-07-09 15:49:02 +02001842
1843 /* insert node into diff if not already */
1844 if (!diff_parent) {
Michal Vaskob104f112020-07-17 09:54:54 +02001845 lyd_insert_sibling(*diff, diff_node, diff);
Michal Vaskoe6323f62020-07-09 15:49:02 +02001846 }
1847
1848 /* update operation */
1849 LY_CHECK_RET(lyd_diff_change_op(diff_node, src_op));
1850
1851 if (diff_cb) {
Michal Vaskoe2af8412020-12-03 14:11:38 +01001852 /* call callback with no source diff node since it was duplicated and just added */
1853 LY_CHECK_RET(diff_cb(NULL, diff_node, cb_data));
Michal Vaskoe6323f62020-07-09 15:49:02 +02001854 }
1855
1856 /* update diff parent */
1857 diff_parent = diff_node;
1858 }
1859
1860 /* remove any redundant nodes */
Michal Vaskob98d7082020-07-15 16:38:36 +02001861 if (lyd_diff_is_redundant(diff_parent)) {
Michal Vaskoe6323f62020-07-09 15:49:02 +02001862 if (diff_parent == *diff) {
1863 *diff = (*diff)->next;
1864 }
1865 lyd_free_tree(diff_parent);
1866 }
1867
1868 return LY_SUCCESS;
1869}
1870
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001871LIBYANG_API_DEF LY_ERR
Michal Vaskofb737aa2020-08-06 13:53:53 +02001872lyd_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 +01001873 lyd_diff_cb diff_cb, void *cb_data, uint16_t options)
Michal Vaskoe6323f62020-07-09 15:49:02 +02001874{
1875 const struct lyd_node *src_root;
Michal Vasko271d2e32023-01-31 15:43:19 +01001876 struct hash_table *dup_inst = NULL;
Michal Vaskoe78faec2021-04-08 17:24:43 +02001877 LY_ERR ret = LY_SUCCESS;
Michal Vaskoe6323f62020-07-09 15:49:02 +02001878
1879 LY_LIST_FOR(src_diff, src_root) {
1880 if (mod && (lyd_owner_module(src_root) != mod)) {
1881 /* skip data nodes from different modules */
1882 continue;
1883 }
1884
1885 /* apply relevant nodes from the diff datatree */
Michal Vaskoe78faec2021-04-08 17:24:43 +02001886 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 +02001887 }
1888
Michal Vaskoe78faec2021-04-08 17:24:43 +02001889cleanup:
Michal Vaskod7c048c2021-05-18 16:12:55 +02001890 lyd_dup_inst_free(dup_inst);
Michal Vaskoe78faec2021-04-08 17:24:43 +02001891 return ret;
Michal Vaskoe6323f62020-07-09 15:49:02 +02001892}
1893
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001894LIBYANG_API_DEF LY_ERR
Michal Vasko04f85912020-08-07 12:14:58 +02001895lyd_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 +01001896 lyd_diff_cb diff_cb, void *cb_data, uint16_t options)
Michal Vasko04f85912020-08-07 12:14:58 +02001897{
Michal Vaskoe78faec2021-04-08 17:24:43 +02001898 LY_ERR ret;
Michal Vasko271d2e32023-01-31 15:43:19 +01001899 struct hash_table *dup_inst = NULL;
Michal Vaskoe78faec2021-04-08 17:24:43 +02001900
Michal Vasko04f85912020-08-07 12:14:58 +02001901 if (!src_sibling) {
1902 return LY_SUCCESS;
1903 }
1904
Michal Vaskoe78faec2021-04-08 17:24:43 +02001905 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 +02001906 lyd_dup_inst_free(dup_inst);
Michal Vaskoe78faec2021-04-08 17:24:43 +02001907 return ret;
Michal Vasko04f85912020-08-07 12:14:58 +02001908}
1909
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001910LIBYANG_API_DEF LY_ERR
Michal Vaskoc0e58e82020-11-11 19:04:33 +01001911lyd_diff_merge_all(struct lyd_node **diff, const struct lyd_node *src_diff, uint16_t options)
Michal Vaskoe6323f62020-07-09 15:49:02 +02001912{
Michal Vaskoc0e58e82020-11-11 19:04:33 +01001913 return lyd_diff_merge_module(diff, src_diff, NULL, NULL, NULL, options);
Michal Vaskoe6323f62020-07-09 15:49:02 +02001914}
Michal Vasko4231fb62020-07-13 13:54:47 +02001915
1916static LY_ERR
Michal Vaskobaba84e2021-02-05 16:33:30 +01001917lyd_diff_reverse_value(struct lyd_node *node, const struct lys_module *mod)
Michal Vasko4231fb62020-07-13 13:54:47 +02001918{
1919 LY_ERR ret = LY_SUCCESS;
1920 struct lyd_meta *meta;
Michal Vaskoba99a3e2020-08-18 15:50:05 +02001921 const char *val1 = NULL;
1922 char *val2;
Radek Krejci1deb5be2020-08-26 16:43:36 +02001923 uint32_t flags;
Michal Vasko4231fb62020-07-13 13:54:47 +02001924
Michal Vaskobaba84e2021-02-05 16:33:30 +01001925 assert(node->schema->nodetype & (LYS_LEAF | LYS_ANYDATA));
1926
1927 meta = lyd_find_meta(node->meta, mod, "orig-value");
Michal Vasko52afd7d2022-01-18 14:08:34 +01001928 LY_CHECK_ERR_RET(!meta, LOGERR_META(LYD_CTX(node), "orig-value", node), LY_EINVAL);
Michal Vasko4231fb62020-07-13 13:54:47 +02001929
1930 /* orig-value */
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02001931 val1 = lyd_get_meta_value(meta);
Michal Vasko4231fb62020-07-13 13:54:47 +02001932
1933 /* current value */
Michal Vaskobaba84e2021-02-05 16:33:30 +01001934 if (node->schema->nodetype == LYS_LEAF) {
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02001935 val2 = strdup(lyd_get_value(node));
Michal Vaskobaba84e2021-02-05 16:33:30 +01001936 } else {
1937 LY_CHECK_RET(lyd_any_value_str(node, &val2));
1938 }
Michal Vasko4231fb62020-07-13 13:54:47 +02001939
1940 /* switch values, keep default flag */
Michal Vaskobaba84e2021-02-05 16:33:30 +01001941 flags = node->flags;
1942 if (node->schema->nodetype == LYS_LEAF) {
1943 LY_CHECK_GOTO(ret = lyd_change_term(node, val1), cleanup);
1944 } else {
1945 union lyd_any_value anyval = {.str = val1};
Michal Vasko26bbb272022-08-02 14:54:33 +02001946
Michal Vaskobaba84e2021-02-05 16:33:30 +01001947 LY_CHECK_GOTO(ret = lyd_any_copy_value(node, &anyval, LYD_ANYDATA_STRING), cleanup);
1948 }
1949 node->flags = flags;
Michal Vasko4231fb62020-07-13 13:54:47 +02001950 LY_CHECK_GOTO(ret = lyd_change_meta(meta, val2), cleanup);
1951
1952cleanup:
Michal Vaskoba99a3e2020-08-18 15:50:05 +02001953 free(val2);
Michal Vasko4231fb62020-07-13 13:54:47 +02001954 return ret;
1955}
1956
1957static LY_ERR
1958lyd_diff_reverse_default(struct lyd_node *node, const struct lys_module *mod)
1959{
1960 struct lyd_meta *meta;
Radek Krejci1deb5be2020-08-26 16:43:36 +02001961 uint32_t flag1, flag2;
Michal Vasko4231fb62020-07-13 13:54:47 +02001962
1963 meta = lyd_find_meta(node->meta, mod, "orig-default");
Michal Vasko610e93b2020-11-09 20:58:32 +01001964 LY_CHECK_ERR_RET(!meta, LOGINT(mod->ctx), LY_EINT);
Michal Vasko4231fb62020-07-13 13:54:47 +02001965
1966 /* orig-default */
Michal Vaskoba99a3e2020-08-18 15:50:05 +02001967 if (meta->value.boolean) {
Michal Vasko4231fb62020-07-13 13:54:47 +02001968 flag1 = LYD_DEFAULT;
1969 } else {
1970 flag1 = 0;
1971 }
1972
1973 /* current default */
1974 flag2 = node->flags & LYD_DEFAULT;
1975
Michal Vasko610e93b2020-11-09 20:58:32 +01001976 if (flag1 == flag2) {
1977 /* no default state change so nothing to reverse */
1978 return LY_SUCCESS;
1979 }
1980
Michal Vasko4231fb62020-07-13 13:54:47 +02001981 /* switch defaults */
1982 node->flags &= ~LYD_DEFAULT;
1983 node->flags |= flag1;
1984 LY_CHECK_RET(lyd_change_meta(meta, flag2 ? "true" : "false"));
1985
1986 return LY_SUCCESS;
1987}
1988
1989static LY_ERR
1990lyd_diff_reverse_meta(struct lyd_node *node, const struct lys_module *mod, const char *name1, const char *name2)
1991{
1992 LY_ERR ret = LY_SUCCESS;
1993 struct lyd_meta *meta1, *meta2;
Michal Vaskoba99a3e2020-08-18 15:50:05 +02001994 const char *val1 = NULL;
1995 char *val2 = NULL;
Michal Vasko4231fb62020-07-13 13:54:47 +02001996
1997 meta1 = lyd_find_meta(node->meta, mod, name1);
Michal Vasko52afd7d2022-01-18 14:08:34 +01001998 LY_CHECK_ERR_RET(!meta1, LOGERR_META(LYD_CTX(node), name1, node), LY_EINVAL);
Michal Vasko4231fb62020-07-13 13:54:47 +02001999
2000 meta2 = lyd_find_meta(node->meta, mod, name2);
Michal Vasko52afd7d2022-01-18 14:08:34 +01002001 LY_CHECK_ERR_RET(!meta2, LOGERR_META(LYD_CTX(node), name2, node), LY_EINVAL);
Michal Vasko4231fb62020-07-13 13:54:47 +02002002
2003 /* value1 */
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02002004 val1 = lyd_get_meta_value(meta1);
Michal Vasko4231fb62020-07-13 13:54:47 +02002005
2006 /* value2 */
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02002007 val2 = strdup(lyd_get_meta_value(meta2));
Michal Vasko4231fb62020-07-13 13:54:47 +02002008
2009 /* switch values */
2010 LY_CHECK_GOTO(ret = lyd_change_meta(meta1, val2), cleanup);
2011 LY_CHECK_GOTO(ret = lyd_change_meta(meta2, val1), cleanup);
2012
2013cleanup:
Michal Vaskoba99a3e2020-08-18 15:50:05 +02002014 free(val2);
Michal Vasko4231fb62020-07-13 13:54:47 +02002015 return ret;
2016}
2017
Michal Vasko9a7e9d02021-03-09 13:52:25 +01002018/**
2019 * @brief Remove specific operation from all the nodes in a subtree.
2020 *
2021 * @param[in] diff Diff subtree to process.
2022 * @param[in] op Only expected operation.
2023 * @return LY_ERR value.
2024 */
2025static LY_ERR
2026lyd_diff_reverse_remove_op_r(struct lyd_node *diff, enum lyd_diff_op op)
2027{
2028 struct lyd_node *elem;
2029 struct lyd_meta *meta;
2030
2031 LYD_TREE_DFS_BEGIN(diff, elem) {
2032 meta = lyd_find_meta(elem->meta, NULL, "yang:operation");
2033 if (meta) {
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02002034 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 +01002035 lyd_free_meta_single(meta);
2036 }
2037
2038 LYD_TREE_DFS_END(diff, elem);
2039 }
2040
2041 return LY_SUCCESS;
2042}
2043
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002044LIBYANG_API_DEF LY_ERR
Michal Vasko66535812020-08-11 08:44:22 +02002045lyd_diff_reverse_all(const struct lyd_node *src_diff, struct lyd_node **diff)
Michal Vasko4231fb62020-07-13 13:54:47 +02002046{
2047 LY_ERR ret = LY_SUCCESS;
2048 const struct lys_module *mod;
Michal Vasko9a7e9d02021-03-09 13:52:25 +01002049 struct lyd_node *root, *elem, *iter;
Michal Vasko4231fb62020-07-13 13:54:47 +02002050 enum lyd_diff_op op;
2051
2052 LY_CHECK_ARG_RET(NULL, diff, LY_EINVAL);
2053
2054 if (!src_diff) {
2055 *diff = NULL;
2056 return LY_SUCCESS;
2057 }
2058
2059 /* duplicate diff */
Michal Vasko3a41dff2020-07-15 14:30:28 +02002060 LY_CHECK_RET(lyd_dup_siblings(src_diff, NULL, LYD_DUP_RECURSIVE, diff));
Michal Vasko4231fb62020-07-13 13:54:47 +02002061
2062 /* find module with metadata needed for later */
Michal Vaskob7be7a82020-08-20 09:09:04 +02002063 mod = ly_ctx_get_module_latest(LYD_CTX(src_diff), "yang");
2064 LY_CHECK_ERR_GOTO(!mod, LOGINT(LYD_CTX(src_diff)); ret = LY_EINT, cleanup);
Michal Vasko4231fb62020-07-13 13:54:47 +02002065
2066 LY_LIST_FOR(*diff, root) {
Michal Vasko56daf732020-08-10 10:57:18 +02002067 LYD_TREE_DFS_BEGIN(root, elem) {
Michal Vasko0f5c6a52020-11-06 17:18:03 +01002068 /* skip all keys */
2069 if (!lysc_is_key(elem->schema)) {
2070 /* find operation attribute, if any */
2071 LY_CHECK_GOTO(ret = lyd_diff_get_op(elem, &op), cleanup);
Michal Vasko4231fb62020-07-13 13:54:47 +02002072
Michal Vasko0f5c6a52020-11-06 17:18:03 +01002073 switch (op) {
2074 case LYD_DIFF_OP_CREATE:
2075 /* reverse create to delete */
2076 LY_CHECK_GOTO(ret = lyd_diff_change_op(elem, LYD_DIFF_OP_DELETE), cleanup);
Michal Vasko9e070522021-03-05 14:00:14 +01002077
Michal Vasko9a7e9d02021-03-09 13:52:25 +01002078 /* check all the children for the same operation, nothing else is expected */
2079 LY_LIST_FOR(lyd_child(elem), iter) {
2080 lyd_diff_reverse_remove_op_r(iter, LYD_DIFF_OP_CREATE);
2081 }
2082
Michal Vasko9e070522021-03-05 14:00:14 +01002083 LYD_TREE_DFS_continue = 1;
Michal Vasko4231fb62020-07-13 13:54:47 +02002084 break;
Michal Vasko0f5c6a52020-11-06 17:18:03 +01002085 case LYD_DIFF_OP_DELETE:
2086 /* reverse delete to create */
2087 LY_CHECK_GOTO(ret = lyd_diff_change_op(elem, LYD_DIFF_OP_CREATE), cleanup);
Michal Vasko9e070522021-03-05 14:00:14 +01002088
Michal Vasko9a7e9d02021-03-09 13:52:25 +01002089 /* check all the children for the same operation, nothing else is expected */
2090 LY_LIST_FOR(lyd_child(elem), iter) {
2091 lyd_diff_reverse_remove_op_r(iter, LYD_DIFF_OP_DELETE);
2092 }
2093
Michal Vasko9e070522021-03-05 14:00:14 +01002094 LYD_TREE_DFS_continue = 1;
Michal Vasko4231fb62020-07-13 13:54:47 +02002095 break;
Michal Vasko0f5c6a52020-11-06 17:18:03 +01002096 case LYD_DIFF_OP_REPLACE:
2097 switch (elem->schema->nodetype) {
2098 case LYS_LEAF:
2099 /* leaf value change */
2100 LY_CHECK_GOTO(ret = lyd_diff_reverse_value(elem, mod), cleanup);
2101 LY_CHECK_GOTO(ret = lyd_diff_reverse_default(elem, mod), cleanup);
2102 break;
Michal Vaskobaba84e2021-02-05 16:33:30 +01002103 case LYS_ANYXML:
2104 case LYS_ANYDATA:
2105 /* any value change */
2106 LY_CHECK_GOTO(ret = lyd_diff_reverse_value(elem, mod), cleanup);
2107 break;
Michal Vasko0f5c6a52020-11-06 17:18:03 +01002108 case LYS_LEAFLIST:
2109 /* leaf-list move */
2110 LY_CHECK_GOTO(ret = lyd_diff_reverse_default(elem, mod), cleanup);
Michal Vaskoe78faec2021-04-08 17:24:43 +02002111 if (lysc_is_dup_inst_list(elem->schema)) {
2112 LY_CHECK_GOTO(ret = lyd_diff_reverse_meta(elem, mod, "orig-position", "position"), cleanup);
2113 } else {
2114 LY_CHECK_GOTO(ret = lyd_diff_reverse_meta(elem, mod, "orig-value", "value"), cleanup);
2115 }
Michal Vasko0f5c6a52020-11-06 17:18:03 +01002116 break;
2117 case LYS_LIST:
2118 /* list move */
Michal Vaskoe78faec2021-04-08 17:24:43 +02002119 if (lysc_is_dup_inst_list(elem->schema)) {
2120 LY_CHECK_GOTO(ret = lyd_diff_reverse_meta(elem, mod, "orig-position", "position"), cleanup);
2121 } else {
2122 LY_CHECK_GOTO(ret = lyd_diff_reverse_meta(elem, mod, "orig-key", "key"), cleanup);
2123 }
Michal Vasko0f5c6a52020-11-06 17:18:03 +01002124 break;
2125 default:
2126 LOGINT(LYD_CTX(src_diff));
2127 ret = LY_EINT;
2128 goto cleanup;
2129 }
Michal Vasko4231fb62020-07-13 13:54:47 +02002130 break;
Michal Vasko0f5c6a52020-11-06 17:18:03 +01002131 case LYD_DIFF_OP_NONE:
2132 switch (elem->schema->nodetype) {
2133 case LYS_LEAF:
2134 case LYS_LEAFLIST:
2135 /* default flag change */
2136 LY_CHECK_GOTO(ret = lyd_diff_reverse_default(elem, mod), cleanup);
2137 break;
2138 default:
2139 /* nothing to do */
2140 break;
2141 }
Michal Vasko4231fb62020-07-13 13:54:47 +02002142 break;
2143 }
Michal Vasko4231fb62020-07-13 13:54:47 +02002144 }
2145
Michal Vasko56daf732020-08-10 10:57:18 +02002146 LYD_TREE_DFS_END(root, elem);
Michal Vasko4231fb62020-07-13 13:54:47 +02002147 }
2148 }
2149
2150cleanup:
2151 if (ret) {
2152 lyd_free_siblings(*diff);
2153 *diff = NULL;
2154 }
2155 return ret;
2156}