blob: 2c125f4251d55a9969cf2906558ebd3a412bef22 [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 Vaskofb51a842023-06-20 08:50:24 +0200164/**
165 * @brief Find metadata/an attribute of a node.
166 *
167 * @param[in] node Node to search.
168 * @param[in] name Metadata/attribute name.
169 * @param[out] meta Metadata found, NULL if not found.
170 * @param[out] attr Attribute found, NULL if not found.
171 */
172static void
173lyd_diff_find_meta(const struct lyd_node *node, const char *name, struct lyd_meta **meta, struct lyd_attr **attr)
174{
175 struct lyd_meta *m;
176 struct lyd_attr *a;
177
178 if (meta) {
179 *meta = NULL;
180 }
181 if (attr) {
182 *attr = NULL;
183 }
184
185 if (node->schema) {
186 assert(meta);
187
188 LY_LIST_FOR(node->meta, m) {
189 if (!strcmp(m->name, name) && !strcmp(m->annotation->module->name, "yang")) {
190 *meta = m;
191 break;
192 }
193 }
194 } else {
195 assert(attr);
196
197 LY_LIST_FOR(((struct lyd_node_opaq *)node)->attr, a) {
198 /* name */
199 if (strcmp(a->name.name, name)) {
200 continue;
201 }
202
203 /* module */
204 switch (a->format) {
205 case LY_VALUE_JSON:
206 if (strcmp(a->name.module_name, "yang")) {
207 continue;
208 }
209 break;
210 case LY_VALUE_XML:
211 if (strcmp(a->name.module_ns, "urn:ietf:params:xml:ns:yang:1")) {
212 continue;
213 }
214 break;
215 default:
216 LOGINT(LYD_CTX(node));
217 return;
218 }
219
220 *attr = a;
221 break;
222 }
223 }
224}
225
226/**
227 * @brief Learn operation of a diff node.
228 *
229 * @param[in] diff_node Diff node.
230 * @param[out] op Operation.
231 * @return LY_ERR value.
232 */
233static LY_ERR
234lyd_diff_get_op(const struct lyd_node *diff_node, enum lyd_diff_op *op)
235{
236 struct lyd_meta *meta = NULL;
237 struct lyd_attr *attr = NULL;
238 const struct lyd_node *diff_parent;
239 const char *str;
240 char *path;
241
242 for (diff_parent = diff_node; diff_parent; diff_parent = lyd_parent(diff_parent)) {
243 lyd_diff_find_meta(diff_parent, "operation", &meta, &attr);
244 if (!meta && !attr) {
245 continue;
246 }
247
248 str = meta ? lyd_get_meta_value(meta) : attr->value;
249 if ((str[0] == 'r') && (diff_parent != diff_node)) {
250 /* we do not care about this operation if it's in our parent */
251 continue;
252 }
253 *op = lyd_diff_str2op(str);
254 return LY_SUCCESS;
255 }
256
257 /* operation not found */
258 path = lyd_path(diff_node, LYD_PATH_STD, NULL, 0);
259 LOGERR(LYD_CTX(diff_node), LY_EINVAL, "Node \"%s\" without an operation.", path);
260 free(path);
261 return LY_EINT;
262}
263
264/**
265 * @brief Remove metadata/an attribute from a node.
266 *
267 * @param[in] node Node to update.
268 * @param[in] name Metadata/attribute name.
269 */
270static void
271lyd_diff_del_meta(struct lyd_node *node, const char *name)
272{
273 struct lyd_meta *meta;
274 struct lyd_attr *attr;
275
276 lyd_diff_find_meta(node, name, &meta, &attr);
277
278 if (meta) {
279 lyd_free_meta_single(meta);
280 } else if (attr) {
281 lyd_free_attr_single(LYD_CTX(node), attr);
282 }
283}
284
Michal Vaskod59035b2020-07-08 12:00:06 +0200285LY_ERR
286lyd_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 +0200287 const char *key, const char *value, const char *position, const char *orig_key, const char *orig_position,
288 struct lyd_node **diff)
Michal Vaskod59035b2020-07-08 12:00:06 +0200289{
Michal Vaskocffc3f92022-06-15 07:57:24 +0200290 struct lyd_node *dup, *siblings, *match = NULL, *diff_parent = NULL, *elem;
Michal Vaskod59035b2020-07-08 12:00:06 +0200291 const struct lyd_node *parent = NULL;
Michal Vaskofb51a842023-06-20 08:50:24 +0200292 enum lyd_diff_op cur_op;
Michal Vaskod4525672023-06-20 09:15:42 +0200293 struct lyd_meta *meta;
Michal Vasko9af40912023-06-21 08:03:43 +0200294 uint32_t diff_opts;
Michal Vaskod59035b2020-07-08 12:00:06 +0200295
296 assert(diff);
297
Michal Vasko53d48422020-11-13 18:02:29 +0100298 /* replace leaf always needs orig-default and orig-value */
299 assert((node->schema->nodetype != LYS_LEAF) || (op != LYD_DIFF_OP_REPLACE) || (orig_default && orig_value));
300
301 /* create on userord needs key/value */
302 assert((node->schema->nodetype != LYS_LIST) || !(node->schema->flags & LYS_ORDBY_USER) || (op != LYD_DIFF_OP_CREATE) ||
Michal Vaskoe78faec2021-04-08 17:24:43 +0200303 (lysc_is_dup_inst_list(node->schema) && position) || key);
Michal Vasko53d48422020-11-13 18:02:29 +0100304 assert((node->schema->nodetype != LYS_LEAFLIST) || !(node->schema->flags & LYS_ORDBY_USER) ||
Michal Vaskoe78faec2021-04-08 17:24:43 +0200305 (op != LYD_DIFF_OP_CREATE) || (lysc_is_dup_inst_list(node->schema) && position) || value);
Michal Vasko53d48422020-11-13 18:02:29 +0100306
307 /* move on userord needs both key and orig-key/value and orig-value */
308 assert((node->schema->nodetype != LYS_LIST) || !(node->schema->flags & LYS_ORDBY_USER) || (op != LYD_DIFF_OP_REPLACE) ||
Michal Vaskoe78faec2021-04-08 17:24:43 +0200309 (lysc_is_dup_inst_list(node->schema) && position && orig_position) || (key && orig_key));
Michal Vasko53d48422020-11-13 18:02:29 +0100310 assert((node->schema->nodetype != LYS_LEAFLIST) || !(node->schema->flags & LYS_ORDBY_USER) ||
Michal Vaskoe78faec2021-04-08 17:24:43 +0200311 (op != LYD_DIFF_OP_REPLACE) || (lysc_is_dup_inst_list(node->schema) && position && orig_position) ||
312 (value && orig_value));
Michal Vasko53d48422020-11-13 18:02:29 +0100313
Michal Vaskod59035b2020-07-08 12:00:06 +0200314 /* find the first existing parent */
315 siblings = *diff;
Michal Vaskofb51a842023-06-20 08:50:24 +0200316 do {
Michal Vaskod59035b2020-07-08 12:00:06 +0200317 /* find next node parent */
318 parent = node;
319 while (parent->parent && (!diff_parent || (parent->parent->schema != diff_parent->schema))) {
Michal Vasko9e685082021-01-29 14:49:09 +0100320 parent = lyd_parent(parent);
Michal Vaskod59035b2020-07-08 12:00:06 +0200321 }
Michal Vaskofb51a842023-06-20 08:50:24 +0200322
323 if (lysc_is_dup_inst_list(parent->schema)) {
324 /* assume it never exists, we are not able to distinguish whether it does or not */
325 match = NULL;
Michal Vaskod59035b2020-07-08 12:00:06 +0200326 break;
327 }
328
329 /* check whether it exists in the diff */
330 if (lyd_find_sibling_first(siblings, parent, &match)) {
331 break;
332 }
333
334 /* another parent found */
335 diff_parent = match;
336
337 /* move down in the diff */
Radek Krejcia1c1e542020-09-29 16:06:52 +0200338 siblings = lyd_child_no_keys(match);
Michal Vaskofb51a842023-06-20 08:50:24 +0200339 } while (parent != node);
Michal Vaskod59035b2020-07-08 12:00:06 +0200340
Michal Vaskofb51a842023-06-20 08:50:24 +0200341 if (match && (parent == node)) {
342 /* special case when there is already an operation on our descendant */
343 assert(!lyd_diff_get_op(diff_parent, &cur_op) && (cur_op == LYD_DIFF_OP_NONE));
344 (void)cur_op;
345
Michal Vasko9af40912023-06-21 08:03:43 +0200346 /* move it to the end where it is expected (matters for user-ordered lists) */
347 if (lysc_is_userordered(diff_parent->schema)) {
348 for (elem = diff_parent; elem->next && (elem->next->schema == elem->schema); elem = elem->next) {}
349 if (elem != diff_parent) {
350 LY_CHECK_RET(lyd_insert_after(elem, diff_parent));
351 }
352 }
353
Michal Vaskod4525672023-06-20 09:15:42 +0200354 /* will be replaced by the new operation but keep the current op for descendants */
Michal Vaskofb51a842023-06-20 08:50:24 +0200355 lyd_diff_del_meta(diff_parent, "operation");
Michal Vaskod4525672023-06-20 09:15:42 +0200356 LY_LIST_FOR(lyd_child_no_keys(diff_parent), elem) {
357 lyd_diff_find_meta(elem, "operation", &meta, NULL);
358 if (meta) {
359 /* explicit operation, fine */
360 continue;
361 }
362
363 /* set the none operation */
364 LY_CHECK_RET(lyd_new_meta(NULL, elem, NULL, "yang:operation", "none", 0, NULL));
365 }
366
Michal Vaskofb51a842023-06-20 08:50:24 +0200367 dup = diff_parent;
Michal Vasko695a7f22023-02-14 10:02:10 +0100368 } else {
Michal Vasko2be7c8a2023-06-21 08:04:21 +0200369 diff_opts = LYD_DUP_NO_META | LYD_DUP_WITH_PARENTS | LYD_DUP_WITH_FLAGS;
370 if ((op != LYD_DIFF_OP_REPLACE) || !lysc_is_userordered(node->schema) || (node->schema->flags & LYS_CONFIG_R)) {
371 /* move applies only to the user-ordered list, no descendants */
372 diff_opts |= LYD_DUP_RECURSIVE;
373 }
374
Michal Vaskofb51a842023-06-20 08:50:24 +0200375 /* duplicate the subtree (and connect to the diff if possible) */
376 if (diff_parent) {
377 LY_CHECK_RET(lyd_dup_single_to_ctx(node, LYD_CTX(diff_parent), (struct lyd_node_inner *)diff_parent,
Michal Vasko2be7c8a2023-06-21 08:04:21 +0200378 diff_opts, &dup));
Michal Vaskofb51a842023-06-20 08:50:24 +0200379 } else {
Michal Vasko2be7c8a2023-06-21 08:04:21 +0200380 LY_CHECK_RET(lyd_dup_single(node, NULL, diff_opts, &dup));
Michal Vaskod59035b2020-07-08 12:00:06 +0200381 }
Michal Vaskofb51a842023-06-20 08:50:24 +0200382
383 /* find the first duplicated parent */
384 if (!diff_parent) {
385 diff_parent = lyd_parent(dup);
386 while (diff_parent && diff_parent->parent) {
387 diff_parent = lyd_parent(diff_parent);
388 }
389 } else {
390 diff_parent = dup;
391 while (diff_parent->parent && (diff_parent->parent->schema == parent->schema)) {
392 diff_parent = lyd_parent(diff_parent);
393 }
Michal Vaskod59035b2020-07-08 12:00:06 +0200394 }
Michal Vaskod59035b2020-07-08 12:00:06 +0200395
Michal Vaskofb51a842023-06-20 08:50:24 +0200396 /* no parent existed, must be manually connected */
397 if (!diff_parent) {
398 /* there actually was no parent to duplicate */
399 lyd_insert_sibling(*diff, dup, diff);
400 } else if (!diff_parent->parent) {
401 lyd_insert_sibling(*diff, diff_parent, diff);
402 }
Michal Vaskod59035b2020-07-08 12:00:06 +0200403
Michal Vaskofb51a842023-06-20 08:50:24 +0200404 /* add parent operation, if any */
405 if (diff_parent && (diff_parent != dup)) {
406 LY_CHECK_RET(lyd_new_meta(NULL, diff_parent, NULL, "yang:operation", "none", 0, NULL));
407 }
Michal Vaskod59035b2020-07-08 12:00:06 +0200408 }
409
410 /* add subtree operation */
Michal Vasko2e552792022-11-02 12:15:31 +0100411 LY_CHECK_RET(lyd_new_meta(NULL, dup, NULL, "yang:operation", lyd_diff_op2str(op), 0, NULL));
Michal Vaskod59035b2020-07-08 12:00:06 +0200412
Michal Vaskocffc3f92022-06-15 07:57:24 +0200413 if (op == LYD_DIFF_OP_CREATE) {
414 /* all nested user-ordered (leaf-)lists need special metadata for create op */
415 LYD_TREE_DFS_BEGIN(dup, elem) {
416 if ((elem != dup) && lysc_is_userordered(elem->schema)) {
417 LY_CHECK_RET(lyd_diff_add_create_nested_userord(elem));
418 }
419 LYD_TREE_DFS_END(dup, elem);
420 }
421 }
422
Michal Vaskod59035b2020-07-08 12:00:06 +0200423 /* orig-default */
Michal Vasko3a41dff2020-07-15 14:30:28 +0200424 if (orig_default) {
Michal Vasko2e552792022-11-02 12:15:31 +0100425 LY_CHECK_RET(lyd_new_meta(NULL, dup, NULL, "yang:orig-default", orig_default, 0, NULL));
Michal Vaskod59035b2020-07-08 12:00:06 +0200426 }
427
428 /* orig-value */
Michal Vasko3a41dff2020-07-15 14:30:28 +0200429 if (orig_value) {
Michal Vasko2e552792022-11-02 12:15:31 +0100430 LY_CHECK_RET(lyd_new_meta(NULL, dup, NULL, "yang:orig-value", orig_value, 0, NULL));
Michal Vaskod59035b2020-07-08 12:00:06 +0200431 }
432
433 /* key */
Michal Vasko3a41dff2020-07-15 14:30:28 +0200434 if (key) {
Michal Vasko2e552792022-11-02 12:15:31 +0100435 LY_CHECK_RET(lyd_new_meta(NULL, dup, NULL, "yang:key", key, 0, NULL));
Michal Vaskod59035b2020-07-08 12:00:06 +0200436 }
437
438 /* value */
Michal Vasko3a41dff2020-07-15 14:30:28 +0200439 if (value) {
Michal Vasko2e552792022-11-02 12:15:31 +0100440 LY_CHECK_RET(lyd_new_meta(NULL, dup, NULL, "yang:value", value, 0, NULL));
Michal Vaskod59035b2020-07-08 12:00:06 +0200441 }
442
Michal Vaskoe78faec2021-04-08 17:24:43 +0200443 /* position */
444 if (position) {
Michal Vasko2e552792022-11-02 12:15:31 +0100445 LY_CHECK_RET(lyd_new_meta(NULL, dup, NULL, "yang:position", position, 0, NULL));
Michal Vaskoe78faec2021-04-08 17:24:43 +0200446 }
447
Michal Vaskod59035b2020-07-08 12:00:06 +0200448 /* orig-key */
Michal Vasko3a41dff2020-07-15 14:30:28 +0200449 if (orig_key) {
Michal Vasko2e552792022-11-02 12:15:31 +0100450 LY_CHECK_RET(lyd_new_meta(NULL, dup, NULL, "yang:orig-key", orig_key, 0, NULL));
Michal Vaskod59035b2020-07-08 12:00:06 +0200451 }
452
Michal Vaskoe78faec2021-04-08 17:24:43 +0200453 /* orig-position */
454 if (orig_position) {
Michal Vasko2e552792022-11-02 12:15:31 +0100455 LY_CHECK_RET(lyd_new_meta(NULL, dup, NULL, "yang:orig-position", orig_position, 0, NULL));
Michal Vaskoe78faec2021-04-08 17:24:43 +0200456 }
457
Michal Vaskod59035b2020-07-08 12:00:06 +0200458 return LY_SUCCESS;
459}
460
461/**
462 * @brief Get a userord entry for a specific user-ordered list/leaf-list. Create if does not exist yet.
463 *
Michal Vasko1dcd73b2020-12-08 10:04:33 +0100464 * @param[in] first Node from the first tree, can be NULL (on create).
Michal Vaskod59035b2020-07-08 12:00:06 +0200465 * @param[in] schema Schema node of the list/leaf-list.
466 * @param[in,out] userord Sized array of userord items.
467 * @return Userord item for all the user-ordered list/leaf-list instances.
468 */
469static struct lyd_diff_userord *
470lyd_diff_userord_get(const struct lyd_node *first, const struct lysc_node *schema, struct lyd_diff_userord **userord)
471{
472 struct lyd_diff_userord *item;
Michal Vaskoe78faec2021-04-08 17:24:43 +0200473 struct lyd_node *iter;
474 const struct lyd_node **node;
Michal Vaskod59035b2020-07-08 12:00:06 +0200475 LY_ARRAY_COUNT_TYPE u;
476
477 LY_ARRAY_FOR(*userord, u) {
478 if ((*userord)[u].schema == schema) {
479 return &(*userord)[u];
480 }
481 }
482
483 /* it was not added yet, add it now */
484 LY_ARRAY_NEW_RET(schema->module->ctx, *userord, item, NULL);
485
486 item->schema = schema;
487 item->pos = 0;
488 item->inst = NULL;
489
490 /* store all the instance pointers in the current order */
491 if (first) {
Michal Vaskoe78faec2021-04-08 17:24:43 +0200492 LYD_LIST_FOR_INST(lyd_first_sibling(first), first->schema, iter) {
493 LY_ARRAY_NEW_RET(schema->module->ctx, item->inst, node, NULL);
494 *node = iter;
Michal Vaskod59035b2020-07-08 12:00:06 +0200495 }
496 }
497
498 return item;
499}
500
501/**
502 * @brief Get all the metadata to be stored in a diff for the 2 nodes. Can be used only for user-ordered
503 * lists/leaf-lists.
504 *
505 * @param[in] first Node from the first tree, can be NULL (on create).
506 * @param[in] second Node from the second tree, can be NULL (on delete).
507 * @param[in] options Diff options.
Michal Vasko5da938a2022-03-01 09:19:02 +0100508 * @param[in] userord_item Userord item of @p first and/or @p second node.
Michal Vaskod59035b2020-07-08 12:00:06 +0200509 * @param[out] op Operation.
510 * @param[out] orig_default Original default metadata.
511 * @param[out] value Value metadata.
512 * @param[out] orig_value Original value metadata
513 * @param[out] key Key metadata.
514 * @param[out] orig_key Original key metadata.
Michal Vaskoe78faec2021-04-08 17:24:43 +0200515 * @param[out] position Position metadata.
516 * @param[out] orig_position Original position metadata.
Michal Vaskod59035b2020-07-08 12:00:06 +0200517 * @return LY_SUCCESS on success,
518 * @return LY_ENOT if there is no change to be added into diff,
519 * @return LY_ERR value on other errors.
520 */
521static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200522lyd_diff_userord_attrs(const struct lyd_node *first, const struct lyd_node *second, uint16_t options,
Michal Vasko5da938a2022-03-01 09:19:02 +0100523 struct lyd_diff_userord *userord_item, enum lyd_diff_op *op, const char **orig_default, char **value,
Michal Vaskoe78faec2021-04-08 17:24:43 +0200524 char **orig_value, char **key, char **orig_key, char **position, char **orig_position)
Michal Vaskod59035b2020-07-08 12:00:06 +0200525{
Michal Vaskof9b052a2022-06-08 10:26:53 +0200526 LY_ERR rc = LY_SUCCESS;
Michal Vaskod59035b2020-07-08 12:00:06 +0200527 const struct lysc_node *schema;
Michal Vaskoe78faec2021-04-08 17:24:43 +0200528 size_t buflen, bufused;
Michal Vaskoee9b9482023-06-19 13:17:48 +0200529 uint32_t first_pos, second_pos, comp_opts;
Michal Vaskod59035b2020-07-08 12:00:06 +0200530
531 assert(first || second);
532
533 *orig_default = NULL;
534 *value = NULL;
535 *orig_value = NULL;
536 *key = NULL;
537 *orig_key = NULL;
Michal Vaskoe78faec2021-04-08 17:24:43 +0200538 *position = NULL;
539 *orig_position = NULL;
Michal Vaskod59035b2020-07-08 12:00:06 +0200540
541 schema = first ? first->schema : second->schema;
542 assert(lysc_is_userordered(schema));
543
Michal Vaskod59035b2020-07-08 12:00:06 +0200544 /* find user-ordered first position */
545 if (first) {
Michal Vaskoe78faec2021-04-08 17:24:43 +0200546 for (first_pos = 0; first_pos < LY_ARRAY_COUNT(userord_item->inst); ++first_pos) {
Michal Vaskod59035b2020-07-08 12:00:06 +0200547 if (userord_item->inst[first_pos] == first) {
548 break;
549 }
550 }
551 assert(first_pos < LY_ARRAY_COUNT(userord_item->inst));
552 } else {
553 first_pos = 0;
554 }
555
Michal Vaskoe78faec2021-04-08 17:24:43 +0200556 /* prepare position of the next instance */
557 second_pos = userord_item->pos++;
558
Michal Vaskod59035b2020-07-08 12:00:06 +0200559 /* learn operation first */
560 if (!second) {
561 *op = LYD_DIFF_OP_DELETE;
562 } else if (!first) {
563 *op = LYD_DIFF_OP_CREATE;
564 } else {
Michal Vaskoee9b9482023-06-19 13:17:48 +0200565 comp_opts = lysc_is_dup_inst_list(second->schema) ? LYD_COMPARE_FULL_RECURSION : 0;
566 if (lyd_compare_single(second, userord_item->inst[second_pos], comp_opts)) {
Michal Vaskod59035b2020-07-08 12:00:06 +0200567 /* in first, there is a different instance on the second position, we are going to move 'first' node */
568 *op = LYD_DIFF_OP_REPLACE;
Michal Vasko3a41dff2020-07-15 14:30:28 +0200569 } else if ((options & LYD_DIFF_DEFAULTS) && ((first->flags & LYD_DEFAULT) != (second->flags & LYD_DEFAULT))) {
Michal Vaskod59035b2020-07-08 12:00:06 +0200570 /* default flag change */
571 *op = LYD_DIFF_OP_NONE;
572 } else {
573 /* no changes */
574 return LY_ENOT;
575 }
576 }
577
578 /*
579 * set each attribute correctly based on the operation and node type
580 */
581
582 /* orig-default */
Michal Vasko4b715ca2020-11-11 18:39:57 +0100583 if ((schema->nodetype == LYS_LEAFLIST) && ((*op == LYD_DIFF_OP_REPLACE) || (*op == LYD_DIFF_OP_NONE))) {
Michal Vaskod59035b2020-07-08 12:00:06 +0200584 if (first->flags & LYD_DEFAULT) {
585 *orig_default = "true";
586 } else {
587 *orig_default = "false";
588 }
589 }
590
591 /* value */
Michal Vaskoe78faec2021-04-08 17:24:43 +0200592 if ((schema->nodetype == LYS_LEAFLIST) && !lysc_is_dup_inst_list(schema) &&
593 ((*op == LYD_DIFF_OP_REPLACE) || (*op == LYD_DIFF_OP_CREATE))) {
Michal Vaskod59035b2020-07-08 12:00:06 +0200594 if (second_pos) {
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200595 *value = strdup(lyd_get_value(userord_item->inst[second_pos - 1]));
Michal Vaskof9b052a2022-06-08 10:26:53 +0200596 LY_CHECK_ERR_GOTO(!*value, LOGMEM(schema->module->ctx); rc = LY_EMEM, cleanup);
Michal Vaskod59035b2020-07-08 12:00:06 +0200597 } else {
598 *value = strdup("");
Michal Vaskof9b052a2022-06-08 10:26:53 +0200599 LY_CHECK_ERR_GOTO(!*value, LOGMEM(schema->module->ctx); rc = LY_EMEM, cleanup);
Michal Vaskod59035b2020-07-08 12:00:06 +0200600 }
601 }
602
603 /* orig-value */
Michal Vaskoe78faec2021-04-08 17:24:43 +0200604 if ((schema->nodetype == LYS_LEAFLIST) && !lysc_is_dup_inst_list(schema) &&
605 ((*op == LYD_DIFF_OP_REPLACE) || (*op == LYD_DIFF_OP_DELETE))) {
Michal Vaskod59035b2020-07-08 12:00:06 +0200606 if (first_pos) {
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200607 *orig_value = strdup(lyd_get_value(userord_item->inst[first_pos - 1]));
Michal Vaskof9b052a2022-06-08 10:26:53 +0200608 LY_CHECK_ERR_GOTO(!*orig_value, LOGMEM(schema->module->ctx); rc = LY_EMEM, cleanup);
Michal Vaskod59035b2020-07-08 12:00:06 +0200609 } else {
610 *orig_value = strdup("");
Michal Vaskof9b052a2022-06-08 10:26:53 +0200611 LY_CHECK_ERR_GOTO(!*orig_value, LOGMEM(schema->module->ctx); rc = LY_EMEM, cleanup);
Michal Vaskod59035b2020-07-08 12:00:06 +0200612 }
613 }
614
615 /* key */
Michal Vaskoe78faec2021-04-08 17:24:43 +0200616 if ((schema->nodetype == LYS_LIST) && !lysc_is_dup_inst_list(schema) &&
617 ((*op == LYD_DIFF_OP_REPLACE) || (*op == LYD_DIFF_OP_CREATE))) {
Michal Vaskod59035b2020-07-08 12:00:06 +0200618 if (second_pos) {
619 buflen = bufused = 0;
Michal Vaskof9b052a2022-06-08 10:26:53 +0200620 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 +0200621 } else {
622 *key = strdup("");
Michal Vaskof9b052a2022-06-08 10:26:53 +0200623 LY_CHECK_ERR_GOTO(!*key, LOGMEM(schema->module->ctx); rc = LY_EMEM, cleanup);
Michal Vaskod59035b2020-07-08 12:00:06 +0200624 }
625 }
626
627 /* orig-key */
Michal Vaskoe78faec2021-04-08 17:24:43 +0200628 if ((schema->nodetype == LYS_LIST) && !lysc_is_dup_inst_list(schema) &&
629 ((*op == LYD_DIFF_OP_REPLACE) || (*op == LYD_DIFF_OP_DELETE))) {
Michal Vaskod59035b2020-07-08 12:00:06 +0200630 if (first_pos) {
631 buflen = bufused = 0;
Michal Vaskof9b052a2022-06-08 10:26:53 +0200632 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 +0200633 } else {
634 *orig_key = strdup("");
Michal Vaskof9b052a2022-06-08 10:26:53 +0200635 LY_CHECK_ERR_GOTO(!*orig_key, LOGMEM(schema->module->ctx); rc = LY_EMEM, cleanup);
Michal Vaskod59035b2020-07-08 12:00:06 +0200636 }
637 }
638
Michal Vaskoe78faec2021-04-08 17:24:43 +0200639 /* position */
640 if (lysc_is_dup_inst_list(schema) && ((*op == LYD_DIFF_OP_REPLACE) || (*op == LYD_DIFF_OP_CREATE))) {
641 if (second_pos) {
642 if (asprintf(position, "%" PRIu32, second_pos) == -1) {
643 LOGMEM(schema->module->ctx);
Michal Vaskof9b052a2022-06-08 10:26:53 +0200644 rc = LY_EMEM;
645 goto cleanup;
Michal Vaskoe78faec2021-04-08 17:24:43 +0200646 }
647 } else {
648 *position = strdup("");
Michal Vaskof9b052a2022-06-08 10:26:53 +0200649 LY_CHECK_ERR_GOTO(!*position, LOGMEM(schema->module->ctx); rc = LY_EMEM, cleanup);
Michal Vaskoe78faec2021-04-08 17:24:43 +0200650 }
651 }
652
653 /* orig-position */
654 if (lysc_is_dup_inst_list(schema) && ((*op == LYD_DIFF_OP_REPLACE) || (*op == LYD_DIFF_OP_DELETE))) {
655 if (first_pos) {
656 if (asprintf(orig_position, "%" PRIu32, first_pos) == -1) {
657 LOGMEM(schema->module->ctx);
Michal Vaskof9b052a2022-06-08 10:26:53 +0200658 rc = LY_EMEM;
659 goto cleanup;
Michal Vaskoe78faec2021-04-08 17:24:43 +0200660 }
661 } else {
662 *orig_position = strdup("");
Michal Vaskof9b052a2022-06-08 10:26:53 +0200663 LY_CHECK_ERR_GOTO(!*orig_position, LOGMEM(schema->module->ctx); rc = LY_EMEM, cleanup);
Michal Vaskoe78faec2021-04-08 17:24:43 +0200664 }
665 }
666
Michal Vaskod59035b2020-07-08 12:00:06 +0200667 /*
668 * update our instances - apply the change
669 */
670 if (*op == LYD_DIFF_OP_CREATE) {
671 /* insert the instance */
Michal Vaskof9b052a2022-06-08 10:26:53 +0200672 LY_ARRAY_CREATE_GOTO(schema->module->ctx, userord_item->inst, 1, rc, cleanup);
Michal Vaskod59035b2020-07-08 12:00:06 +0200673 if (second_pos < LY_ARRAY_COUNT(userord_item->inst)) {
674 memmove(userord_item->inst + second_pos + 1, userord_item->inst + second_pos,
675 (LY_ARRAY_COUNT(userord_item->inst) - second_pos) * sizeof *userord_item->inst);
676 }
677 LY_ARRAY_INCREMENT(userord_item->inst);
678 userord_item->inst[second_pos] = second;
679
680 } else if (*op == LYD_DIFF_OP_DELETE) {
681 /* remove the instance */
682 if (first_pos + 1 < LY_ARRAY_COUNT(userord_item->inst)) {
683 memmove(userord_item->inst + first_pos, userord_item->inst + first_pos + 1,
684 (LY_ARRAY_COUNT(userord_item->inst) - first_pos - 1) * sizeof *userord_item->inst);
685 }
686 LY_ARRAY_DECREMENT(userord_item->inst);
687
688 } else if (*op == LYD_DIFF_OP_REPLACE) {
689 /* move the instances */
690 memmove(userord_item->inst + second_pos + 1, userord_item->inst + second_pos,
691 (first_pos - second_pos) * sizeof *userord_item->inst);
692 userord_item->inst[second_pos] = first;
693 }
694
Michal Vaskof9b052a2022-06-08 10:26:53 +0200695cleanup:
696 if (rc) {
697 free(*value);
698 *value = NULL;
699 free(*orig_value);
700 *orig_value = NULL;
701 free(*key);
702 *key = NULL;
703 free(*orig_key);
704 *orig_key = NULL;
705 free(*position);
706 *position = NULL;
707 free(*orig_position);
708 *orig_position = NULL;
709 }
710 return rc;
Michal Vaskod59035b2020-07-08 12:00:06 +0200711}
712
713/**
714 * @brief Get all the metadata to be stored in a diff for the 2 nodes. Cannot be used for user-ordered
715 * lists/leaf-lists.
716 *
717 * @param[in] first Node from the first tree, can be NULL (on create).
718 * @param[in] second Node from the second tree, can be NULL (on delete).
719 * @param[in] options Diff options.
720 * @param[out] op Operation.
721 * @param[out] orig_default Original default metadata.
722 * @param[out] orig_value Original value metadata.
723 * @return LY_SUCCESS on success,
724 * @return LY_ENOT if there is no change to be added into diff,
725 * @return LY_ERR value on other errors.
726 */
727static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200728lyd_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 +0200729 const char **orig_default, char **orig_value)
Michal Vaskod59035b2020-07-08 12:00:06 +0200730{
731 const struct lysc_node *schema;
Michal Vasko6ea6fe22021-10-08 09:57:01 +0200732 const char *str_val;
Michal Vaskod59035b2020-07-08 12:00:06 +0200733
734 assert(first || second);
735
736 *orig_default = NULL;
737 *orig_value = NULL;
738
739 schema = first ? first->schema : second->schema;
740 assert(!lysc_is_userordered(schema));
741
742 /* learn operation first */
743 if (!second) {
744 *op = LYD_DIFF_OP_DELETE;
745 } else if (!first) {
746 *op = LYD_DIFF_OP_CREATE;
747 } else {
748 switch (schema->nodetype) {
749 case LYS_CONTAINER:
750 case LYS_RPC:
751 case LYS_ACTION:
752 case LYS_NOTIF:
753 /* no changes */
754 return LY_ENOT;
755 case LYS_LIST:
756 case LYS_LEAFLIST:
Michal Vasko3a41dff2020-07-15 14:30:28 +0200757 if ((options & LYD_DIFF_DEFAULTS) && ((first->flags & LYD_DEFAULT) != (second->flags & LYD_DEFAULT))) {
Michal Vaskod59035b2020-07-08 12:00:06 +0200758 /* default flag change */
759 *op = LYD_DIFF_OP_NONE;
760 } else {
761 /* no changes */
762 return LY_ENOT;
763 }
764 break;
765 case LYS_LEAF:
766 case LYS_ANYXML:
767 case LYS_ANYDATA:
Michal Vasko8f359bf2020-07-28 10:41:15 +0200768 if (lyd_compare_single(first, second, 0)) {
Michal Vaskod59035b2020-07-08 12:00:06 +0200769 /* different values */
770 *op = LYD_DIFF_OP_REPLACE;
Michal Vasko3a41dff2020-07-15 14:30:28 +0200771 } else if ((options & LYD_DIFF_DEFAULTS) && ((first->flags & LYD_DEFAULT) != (second->flags & LYD_DEFAULT))) {
Michal Vaskod59035b2020-07-08 12:00:06 +0200772 /* default flag change */
773 *op = LYD_DIFF_OP_NONE;
774 } else {
775 /* no changes */
776 return LY_ENOT;
777 }
778 break;
779 default:
780 LOGINT_RET(schema->module->ctx);
781 }
782 }
783
784 /*
785 * set each attribute correctly based on the operation and node type
786 */
787
788 /* orig-default */
Michal Vasko4b715ca2020-11-11 18:39:57 +0100789 if ((schema->nodetype & LYD_NODE_TERM) && ((*op == LYD_DIFF_OP_REPLACE) || (*op == LYD_DIFF_OP_NONE))) {
Michal Vaskod59035b2020-07-08 12:00:06 +0200790 if (first->flags & LYD_DEFAULT) {
791 *orig_default = "true";
792 } else {
793 *orig_default = "false";
794 }
795 }
796
797 /* orig-value */
Michal Vaskobaba84e2021-02-05 16:33:30 +0100798 if ((schema->nodetype & (LYS_LEAF | LYS_ANYDATA)) && (*op == LYD_DIFF_OP_REPLACE)) {
799 if (schema->nodetype == LYS_LEAF) {
Michal Vasko6ea6fe22021-10-08 09:57:01 +0200800 str_val = lyd_get_value(first);
801 *orig_value = strdup(str_val ? str_val : "");
Michal Vaskobaba84e2021-02-05 16:33:30 +0100802 LY_CHECK_ERR_RET(!*orig_value, LOGMEM(schema->module->ctx), LY_EMEM);
803 } else {
804 LY_CHECK_RET(lyd_any_value_str(first, orig_value));
805 }
Michal Vaskod59035b2020-07-08 12:00:06 +0200806 }
807
808 return LY_SUCCESS;
809}
810
811/**
Michal Vaskoe78faec2021-04-08 17:24:43 +0200812 * @brief Find a matching instance of a node in a data tree.
813 *
814 * @param[in] siblings Siblings to search in.
815 * @param[in] target Target node to search for.
816 * @param[in] defaults Whether to consider (or ignore) default values.
Michal Vasko271d2e32023-01-31 15:43:19 +0100817 * @param[in,out] dup_inst_ht Duplicate instance cache.
Michal Vaskoe78faec2021-04-08 17:24:43 +0200818 * @param[out] match Found match, NULL if no matching node found.
819 * @return LY_ERR value.
820 */
821static LY_ERR
822lyd_diff_find_match(const struct lyd_node *siblings, const struct lyd_node *target, ly_bool defaults,
Michal Vasko8efac242023-03-30 08:24:56 +0200823 struct ly_ht **dup_inst_ht, struct lyd_node **match)
Michal Vaskoe78faec2021-04-08 17:24:43 +0200824{
Michal Vasko2bd856f2022-12-02 14:03:29 +0100825 LY_ERR r;
826
Michal Vaskodb91fc32023-05-02 14:39:40 +0200827 if (!target->schema) {
828 /* try to find the same opaque node */
829 r = lyd_find_sibling_opaq_next(siblings, LYD_NAME(target), match);
830 } else if (target->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) {
Michal Vaskoe78faec2021-04-08 17:24:43 +0200831 /* try to find the exact instance */
Michal Vasko2bd856f2022-12-02 14:03:29 +0100832 r = lyd_find_sibling_first(siblings, target, match);
Michal Vaskoe78faec2021-04-08 17:24:43 +0200833 } else {
834 /* try to simply find the node, there cannot be more instances */
Michal Vasko2bd856f2022-12-02 14:03:29 +0100835 r = lyd_find_sibling_val(siblings, target->schema, NULL, 0, match);
836 }
837 if (r && (r != LY_ENOTFOUND)) {
838 return r;
Michal Vaskoe78faec2021-04-08 17:24:43 +0200839 }
840
Michal Vaskod7c048c2021-05-18 16:12:55 +0200841 /* update match as needed */
Michal Vasko271d2e32023-01-31 15:43:19 +0100842 LY_CHECK_RET(lyd_dup_inst_next(match, siblings, dup_inst_ht));
Michal Vaskoe78faec2021-04-08 17:24:43 +0200843
844 if (*match && ((*match)->flags & LYD_DEFAULT) && !defaults) {
845 /* ignore default nodes */
846 *match = NULL;
847 }
848 return LY_SUCCESS;
849}
850
851/**
Michal Vaskod59035b2020-07-08 12:00:06 +0200852 * @brief Perform diff for all siblings at certain depth, recursively.
853 *
854 * For user-ordered lists/leaf-lists a specific structure is used for storing
855 * the current order. The idea is to apply all the generated diff changes
856 * virtually on the first tree so that we can continue to generate correct
857 * changes after some were already generated.
858 *
859 * The algorithm then uses second tree position-based changes with a before
860 * (preceding) item anchor.
861 *
862 * Example:
863 *
864 * Virtual first tree leaf-list order:
865 * 1 2 [3] 4 5
866 *
867 * Second tree leaf-list order:
868 * 1 2 [5] 3 4
869 *
870 * We are at the 3rd node now. We look at whether the nodes on the 3rd position
871 * match - they do not - move nodes so that the 3rd position node is final ->
872 * -> move node 5 to the 3rd position -> move node 5 after node 2.
873 *
874 * Required properties:
875 * Stored operations (move) should not be affected by later operations -
876 * - would cause a redundantly long list of operations, possibly inifinite.
877 *
878 * Implemenation justification:
879 * First, all delete operations and only then move/create operations are stored.
880 * Also, preceding anchor is used and after each iteration another node is
881 * at its final position. That results in the invariant that all preceding
882 * nodes are final and will not be changed by the later operations, meaning
883 * they can safely be used as anchors for the later operations.
884 *
885 * @param[in] first First tree first sibling.
886 * @param[in] second Second tree first sibling.
887 * @param[in] options Diff options.
Michal Vasko3a41dff2020-07-15 14:30:28 +0200888 * @param[in] nosiblings Whether to skip following siblings.
Michal Vaskod59035b2020-07-08 12:00:06 +0200889 * @param[in,out] diff Diff to append to.
890 * @return LY_ERR value.
891 */
892static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +0200893lyd_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 +0200894 struct lyd_node **diff)
Michal Vaskod59035b2020-07-08 12:00:06 +0200895{
896 LY_ERR ret = LY_SUCCESS;
897 const struct lyd_node *iter_first, *iter_second;
898 struct lyd_node *match_second, *match_first;
Michal Vasko5da938a2022-03-01 09:19:02 +0100899 struct lyd_diff_userord *userord = NULL, *userord_item;
Michal Vasko8efac242023-03-30 08:24:56 +0200900 struct ly_ht *dup_inst_first = NULL, *dup_inst_second = NULL;
Michal Vaskod59035b2020-07-08 12:00:06 +0200901 LY_ARRAY_COUNT_TYPE u;
902 enum lyd_diff_op op;
903 const char *orig_default;
Michal Vaskoe78faec2021-04-08 17:24:43 +0200904 char *orig_value, *key, *value, *position, *orig_key, *orig_position;
Michal Vaskod59035b2020-07-08 12:00:06 +0200905
Michal Vaskod59035b2020-07-08 12:00:06 +0200906 /* compare first tree to the second tree - delete, replace, none */
907 LY_LIST_FOR(first, iter_first) {
Michal Vaskoc825ed72021-07-21 16:05:59 +0200908 if (!iter_first->schema) {
909 continue;
910 }
911
Michal Vaskod59035b2020-07-08 12:00:06 +0200912 assert(!(iter_first->schema->flags & LYS_KEY));
Michal Vasko3a41dff2020-07-15 14:30:28 +0200913 if ((iter_first->flags & LYD_DEFAULT) && !(options & LYD_DIFF_DEFAULTS)) {
Michal Vaskod59035b2020-07-08 12:00:06 +0200914 /* skip default nodes */
915 continue;
916 }
917
Michal Vaskoe78faec2021-04-08 17:24:43 +0200918 /* find a match in the second tree */
919 LY_CHECK_GOTO(ret = lyd_diff_find_match(second, iter_first, options & LYD_DIFF_DEFAULTS, &dup_inst_second,
920 &match_second), cleanup);
Michal Vaskod59035b2020-07-08 12:00:06 +0200921
922 if (lysc_is_userordered(iter_first->schema)) {
Michal Vasko5da938a2022-03-01 09:19:02 +0100923 /* get (create) userord entry */
924 userord_item = lyd_diff_userord_get(iter_first, iter_first->schema, &userord);
925 LY_CHECK_ERR_GOTO(!userord_item, LOGMEM(LYD_CTX(iter_first)); ret = LY_EMEM, cleanup);
926
Michal Vaskoe78faec2021-04-08 17:24:43 +0200927 /* we are handling only user-ordered node delete now */
928 if (!match_second) {
929 /* get all the attributes */
Michal Vasko5da938a2022-03-01 09:19:02 +0100930 LY_CHECK_GOTO(ret = lyd_diff_userord_attrs(iter_first, match_second, options, userord_item, &op,
931 &orig_default, &value, &orig_value, &key, &orig_key, &position, &orig_position), cleanup);
Michal Vaskoe78faec2021-04-08 17:24:43 +0200932
933 /* there must be changes, it is deleted */
934 assert(op == LYD_DIFF_OP_DELETE);
Michal Vasko5da938a2022-03-01 09:19:02 +0100935 ret = lyd_diff_add(iter_first, op, orig_default, orig_value, key, value, position, orig_key,
936 orig_position, diff);
Michal Vaskoe78faec2021-04-08 17:24:43 +0200937
938 free(orig_value);
939 free(key);
940 free(value);
941 free(position);
942 free(orig_key);
943 free(orig_position);
944 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskod59035b2020-07-08 12:00:06 +0200945 }
Michal Vaskod59035b2020-07-08 12:00:06 +0200946 } else {
947 /* get all the attributes */
948 ret = lyd_diff_attrs(iter_first, match_second, options, &op, &orig_default, &orig_value);
949
950 /* add into diff if there are any changes */
951 if (!ret) {
952 if (op == LYD_DIFF_OP_DELETE) {
Michal Vaskoe78faec2021-04-08 17:24:43 +0200953 ret = lyd_diff_add(iter_first, op, orig_default, orig_value, NULL, NULL, NULL, NULL, NULL, diff);
Michal Vaskod59035b2020-07-08 12:00:06 +0200954 } else {
Michal Vasko3c2dd6c2020-11-06 17:38:55 +0100955 assert(match_second);
Michal Vaskoe78faec2021-04-08 17:24:43 +0200956 ret = lyd_diff_add(match_second, op, orig_default, orig_value, NULL, NULL, NULL, NULL, NULL, diff);
Michal Vaskod59035b2020-07-08 12:00:06 +0200957 }
958
959 free(orig_value);
960 LY_CHECK_GOTO(ret, cleanup);
961 } else if (ret == LY_ENOT) {
962 ret = LY_SUCCESS;
963 } else {
964 goto cleanup;
965 }
966 }
967
968 /* check descendants, if any, recursively */
969 if (match_second) {
Michal Vaskoe78faec2021-04-08 17:24:43 +0200970 LY_CHECK_GOTO(ret = lyd_diff_siblings_r(lyd_child_no_keys(iter_first), lyd_child_no_keys(match_second),
971 options, 0, diff), cleanup);
Michal Vaskod59035b2020-07-08 12:00:06 +0200972 }
973
974 if (nosiblings) {
975 break;
976 }
977 }
978
979 /* reset all cached positions */
980 LY_ARRAY_FOR(userord, u) {
981 userord[u].pos = 0;
982 }
983
984 /* compare second tree to the first tree - create, user-ordered move */
985 LY_LIST_FOR(second, iter_second) {
Michal Vaskoc825ed72021-07-21 16:05:59 +0200986 if (!iter_second->schema) {
987 continue;
988 }
989
Michal Vaskod59035b2020-07-08 12:00:06 +0200990 assert(!(iter_second->schema->flags & LYS_KEY));
Michal Vasko3a41dff2020-07-15 14:30:28 +0200991 if ((iter_second->flags & LYD_DEFAULT) && !(options & LYD_DIFF_DEFAULTS)) {
Michal Vaskod59035b2020-07-08 12:00:06 +0200992 /* skip default nodes */
993 continue;
994 }
995
Michal Vaskoe78faec2021-04-08 17:24:43 +0200996 /* find a match in the first tree */
997 LY_CHECK_GOTO(ret = lyd_diff_find_match(first, iter_second, options & LYD_DIFF_DEFAULTS, &dup_inst_first,
998 &match_first), cleanup);
Michal Vaskod59035b2020-07-08 12:00:06 +0200999
1000 if (lysc_is_userordered(iter_second->schema)) {
Michal Vasko5da938a2022-03-01 09:19:02 +01001001 /* get userord entry */
Michal Vaskoaa51cc52022-12-06 09:57:11 +01001002 userord_item = lyd_diff_userord_get(match_first, iter_second->schema, &userord);
Michal Vasko5da938a2022-03-01 09:19:02 +01001003 LY_CHECK_ERR_GOTO(!userord_item, LOGMEM(LYD_CTX(iter_second)); ret = LY_EMEM, cleanup);
1004
Michal Vaskod59035b2020-07-08 12:00:06 +02001005 /* get all the attributes */
Michal Vasko5da938a2022-03-01 09:19:02 +01001006 ret = lyd_diff_userord_attrs(match_first, iter_second, options, userord_item, &op, &orig_default,
Michal Vaskoe78faec2021-04-08 17:24:43 +02001007 &value, &orig_value, &key, &orig_key, &position, &orig_position);
Michal Vaskod59035b2020-07-08 12:00:06 +02001008
1009 /* add into diff if there are any changes */
1010 if (!ret) {
Michal Vaskoe78faec2021-04-08 17:24:43 +02001011 ret = lyd_diff_add(iter_second, op, orig_default, orig_value, key, value, position, orig_key,
1012 orig_position, diff);
Michal Vaskod59035b2020-07-08 12:00:06 +02001013
1014 free(orig_value);
1015 free(key);
1016 free(value);
Michal Vaskoe78faec2021-04-08 17:24:43 +02001017 free(position);
Michal Vaskod59035b2020-07-08 12:00:06 +02001018 free(orig_key);
Michal Vaskoe78faec2021-04-08 17:24:43 +02001019 free(orig_position);
Michal Vaskod59035b2020-07-08 12:00:06 +02001020 LY_CHECK_GOTO(ret, cleanup);
1021 } else if (ret == LY_ENOT) {
1022 ret = LY_SUCCESS;
1023 } else {
1024 goto cleanup;
1025 }
1026 } else if (!match_first) {
1027 /* get all the attributes */
Michal Vaskoe78faec2021-04-08 17:24:43 +02001028 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 +02001029
1030 /* there must be changes, it is created */
1031 assert(op == LYD_DIFF_OP_CREATE);
Michal Vaskoe78faec2021-04-08 17:24:43 +02001032 ret = lyd_diff_add(iter_second, op, orig_default, orig_value, NULL, NULL, NULL, NULL, NULL, diff);
Michal Vaskod59035b2020-07-08 12:00:06 +02001033
1034 free(orig_value);
1035 LY_CHECK_GOTO(ret, cleanup);
1036 } /* else was handled */
1037
1038 if (nosiblings) {
1039 break;
1040 }
1041 }
1042
1043cleanup:
Michal Vaskod7c048c2021-05-18 16:12:55 +02001044 lyd_dup_inst_free(dup_inst_first);
1045 lyd_dup_inst_free(dup_inst_second);
Michal Vaskod59035b2020-07-08 12:00:06 +02001046 LY_ARRAY_FOR(userord, u) {
1047 LY_ARRAY_FREE(userord[u].inst);
1048 }
1049 LY_ARRAY_FREE(userord);
1050 return ret;
1051}
1052
Michal Vasko3a41dff2020-07-15 14:30:28 +02001053static LY_ERR
Michal Vasko55896172022-02-17 10:47:21 +01001054lyd_diff(const struct lyd_node *first, const struct lyd_node *second, uint16_t options, ly_bool nosiblings,
1055 struct lyd_node **diff)
Michal Vaskod59035b2020-07-08 12:00:06 +02001056{
1057 const struct ly_ctx *ctx;
1058
1059 LY_CHECK_ARG_RET(NULL, diff, LY_EINVAL);
1060
1061 if (first) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02001062 ctx = LYD_CTX(first);
Michal Vaskod59035b2020-07-08 12:00:06 +02001063 } else if (second) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02001064 ctx = LYD_CTX(second);
Michal Vaskod59035b2020-07-08 12:00:06 +02001065 } else {
1066 ctx = NULL;
1067 }
1068
1069 if (first && second && (lysc_data_parent(first->schema) != lysc_data_parent(second->schema))) {
1070 LOGERR(ctx, LY_EINVAL, "Invalid arguments - cannot create diff for unrelated data (%s()).", __func__);
1071 return LY_EINVAL;
1072 }
1073
1074 *diff = NULL;
1075
Michal Vasko3a41dff2020-07-15 14:30:28 +02001076 return lyd_diff_siblings_r(first, second, options, nosiblings, diff);
1077}
1078
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001079LIBYANG_API_DEF LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02001080lyd_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 +02001081{
1082 return lyd_diff(first, second, options, 1, diff);
1083}
1084
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001085LIBYANG_API_DEF LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02001086lyd_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 +02001087{
1088 return lyd_diff(first, second, options, 0, diff);
Michal Vaskod59035b2020-07-08 12:00:06 +02001089}
1090
1091/**
Michal Vaskod59035b2020-07-08 12:00:06 +02001092 * @brief Insert a diff node into a data tree.
1093 *
1094 * @param[in,out] first_node First sibling of the data tree.
1095 * @param[in] parent_node Data tree sibling parent node.
1096 * @param[in] new_node Node to insert.
Michal Vaskoe78faec2021-04-08 17:24:43 +02001097 * @param[in] userord_anchor Optional anchor (key, value, or position) of relative (leaf-)list instance. If not set,
1098 * the user-ordered instance will be inserted at the first position.
Michal Vaskod59035b2020-07-08 12:00:06 +02001099 * @return err_info, NULL on success.
1100 */
1101static LY_ERR
1102lyd_diff_insert(struct lyd_node **first_node, struct lyd_node *parent_node, struct lyd_node *new_node,
Michal Vaskoe78faec2021-04-08 17:24:43 +02001103 const char *userord_anchor)
Michal Vaskod59035b2020-07-08 12:00:06 +02001104{
1105 LY_ERR ret;
1106 struct lyd_node *anchor;
Michal Vaskoe78faec2021-04-08 17:24:43 +02001107 uint32_t pos, anchor_pos;
1108 int found;
Michal Vaskod59035b2020-07-08 12:00:06 +02001109
1110 assert(new_node);
1111
1112 if (!*first_node) {
1113 if (!parent_node) {
1114 /* no parent or siblings */
1115 *first_node = new_node;
1116 return LY_SUCCESS;
1117 }
1118
1119 /* simply insert into parent, no other children */
Michal Vaskoe78faec2021-04-08 17:24:43 +02001120 if (userord_anchor) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02001121 LOGERR(LYD_CTX(new_node), LY_EINVAL, "Node \"%s\" instance to insert next to not found.",
Michal Vasko69730152020-10-09 16:30:07 +02001122 new_node->schema->name);
Michal Vaskod59035b2020-07-08 12:00:06 +02001123 return LY_EINVAL;
1124 }
Michal Vaskob104f112020-07-17 09:54:54 +02001125 return lyd_insert_child(parent_node, new_node);
Michal Vaskod59035b2020-07-08 12:00:06 +02001126 }
1127
Michal Vasko9e685082021-01-29 14:49:09 +01001128 assert(!(*first_node)->parent || (lyd_parent(*first_node) == parent_node));
Michal Vaskod59035b2020-07-08 12:00:06 +02001129
Michal Vaskod59035b2020-07-08 12:00:06 +02001130 if (!lysc_is_userordered(new_node->schema)) {
Michal Vaskob104f112020-07-17 09:54:54 +02001131 /* simple insert */
1132 return lyd_insert_sibling(*first_node, new_node, first_node);
Michal Vaskod59035b2020-07-08 12:00:06 +02001133 }
1134
Michal Vaskoe78faec2021-04-08 17:24:43 +02001135 if (userord_anchor) {
Michal Vaskod59035b2020-07-08 12:00:06 +02001136 /* find the anchor sibling */
Michal Vaskoe78faec2021-04-08 17:24:43 +02001137 if (lysc_is_dup_inst_list(new_node->schema)) {
1138 anchor_pos = atoi(userord_anchor);
Michal Vasko0ff97752022-01-18 16:35:41 +01001139 if (!anchor_pos) {
1140 LOGERR(LYD_CTX(new_node), LY_EINVAL, "Invalid user-ordered anchor value \"%s\".", userord_anchor);
1141 return LY_EINVAL;
1142 }
Michal Vaskoe78faec2021-04-08 17:24:43 +02001143
1144 found = 0;
1145 pos = 1;
1146 LYD_LIST_FOR_INST(*first_node, new_node->schema, anchor) {
1147 if (pos == anchor_pos) {
1148 found = 1;
1149 break;
1150 }
1151 ++pos;
1152 }
1153 if (!found) {
1154 LOGERR(LYD_CTX(new_node), LY_EINVAL, "Node \"%s\" instance to insert next to not found.",
1155 new_node->schema->name);
1156 return LY_EINVAL;
1157 }
1158 } else {
1159 ret = lyd_find_sibling_val(*first_node, new_node->schema, userord_anchor, 0, &anchor);
1160 if (ret == LY_ENOTFOUND) {
1161 LOGERR(LYD_CTX(new_node), LY_EINVAL, "Node \"%s\" instance to insert next to not found.",
1162 new_node->schema->name);
1163 return LY_EINVAL;
1164 } else if (ret) {
1165 return ret;
1166 }
Michal Vaskod59035b2020-07-08 12:00:06 +02001167 }
1168
1169 /* insert after */
1170 LY_CHECK_RET(lyd_insert_after(anchor, new_node));
1171 assert(new_node->prev == anchor);
1172 if (*first_node == new_node) {
1173 *first_node = anchor;
1174 }
1175 } else {
Michal Vaskoea7d3232022-04-19 12:01:36 +02001176 /* find the first instance */
1177 ret = lyd_find_sibling_val(*first_node, new_node->schema, NULL, 0, &anchor);
1178 LY_CHECK_RET(ret && (ret != LY_ENOTFOUND), ret);
Michal Vaskod59035b2020-07-08 12:00:06 +02001179
Michal Vaskoea7d3232022-04-19 12:01:36 +02001180 if (anchor) {
1181 /* insert before the first instance */
1182 LY_CHECK_RET(lyd_insert_before(anchor, new_node));
1183 if ((*first_node)->prev->next) {
1184 assert(!new_node->prev->next);
1185 *first_node = new_node;
Michal Vaskod59035b2020-07-08 12:00:06 +02001186 }
Michal Vaskod59035b2020-07-08 12:00:06 +02001187 } else {
Michal Vaskoea7d3232022-04-19 12:01:36 +02001188 /* insert anywhere */
1189 LY_CHECK_RET(lyd_insert_sibling(*first_node, new_node, first_node));
Michal Vaskod59035b2020-07-08 12:00:06 +02001190 }
1191 }
1192
1193 return LY_SUCCESS;
1194}
1195
1196/**
1197 * @brief Apply diff subtree on data tree nodes, recursively.
1198 *
1199 * @param[in,out] first_node First sibling of the data tree.
1200 * @param[in] parent_node Parent of the first sibling.
1201 * @param[in] diff_node Current diff node.
Michal Vaskoe6323f62020-07-09 15:49:02 +02001202 * @param[in] diff_cb Optional diff callback.
1203 * @param[in] cb_data User data for @p diff_cb.
Michal Vaskoe78faec2021-04-08 17:24:43 +02001204 * @param[in,out] dup_inst Duplicate instance cache for all @p diff_node siblings.
Michal Vaskod59035b2020-07-08 12:00:06 +02001205 * @return LY_ERR value.
1206 */
1207static LY_ERR
1208lyd_diff_apply_r(struct lyd_node **first_node, struct lyd_node *parent_node, const struct lyd_node *diff_node,
Michal Vasko8efac242023-03-30 08:24:56 +02001209 lyd_diff_cb diff_cb, void *cb_data, struct ly_ht **dup_inst)
Michal Vaskod59035b2020-07-08 12:00:06 +02001210{
1211 LY_ERR ret;
1212 struct lyd_node *match, *diff_child;
Michal Vaskoe78faec2021-04-08 17:24:43 +02001213 const char *str_val, *meta_str;
Michal Vaskoe6323f62020-07-09 15:49:02 +02001214 enum lyd_diff_op op;
1215 struct lyd_meta *meta;
Michal Vasko8efac242023-03-30 08:24:56 +02001216 struct ly_ht *child_dup_inst = NULL;
Michal Vaskob7be7a82020-08-20 09:09:04 +02001217 const struct ly_ctx *ctx = LYD_CTX(diff_node);
Michal Vaskod59035b2020-07-08 12:00:06 +02001218
1219 /* read all the valid attributes */
Michal Vaskoe6323f62020-07-09 15:49:02 +02001220 LY_CHECK_RET(lyd_diff_get_op(diff_node, &op));
Michal Vaskod59035b2020-07-08 12:00:06 +02001221
Michal Vaskoe6323f62020-07-09 15:49:02 +02001222 /* handle specific user-ordered (leaf-)lists operations separately */
1223 if (lysc_is_userordered(diff_node->schema) && ((op == LYD_DIFF_OP_CREATE) || (op == LYD_DIFF_OP_REPLACE))) {
1224 if (op == LYD_DIFF_OP_REPLACE) {
Michal Vaskod59035b2020-07-08 12:00:06 +02001225 /* find the node (we must have some siblings because the node was only moved) */
Michal Vaskoe78faec2021-04-08 17:24:43 +02001226 LY_CHECK_RET(lyd_diff_find_match(*first_node, diff_node, 1, dup_inst, &match));
Michal Vasko52afd7d2022-01-18 14:08:34 +01001227 LY_CHECK_ERR_RET(!match, LOGERR_NOINST(ctx, diff_node), LY_EINVAL);
Michal Vaskod59035b2020-07-08 12:00:06 +02001228 } else {
Michal Vasko3a41dff2020-07-15 14:30:28 +02001229 /* duplicate the node */
1230 LY_CHECK_RET(lyd_dup_single(diff_node, NULL, LYD_DUP_NO_META, &match));
Michal Vaskod59035b2020-07-08 12:00:06 +02001231 }
1232
Michal Vaskoe78faec2021-04-08 17:24:43 +02001233 /* get "key", "value", or "position" metadata string value */
1234 if (lysc_is_dup_inst_list(diff_node->schema)) {
1235 meta_str = "yang:position";
1236 } else if (diff_node->schema->nodetype == LYS_LIST) {
1237 meta_str = "yang:key";
1238 } else {
1239 meta_str = "yang:value";
1240 }
1241 meta = lyd_find_meta(diff_node->meta, NULL, meta_str);
Michal Vasko52afd7d2022-01-18 14:08:34 +01001242 LY_CHECK_ERR_RET(!meta, LOGERR_META(ctx, meta_str, diff_node), LY_EINVAL);
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02001243 str_val = lyd_get_meta_value(meta);
Michal Vaskoe6323f62020-07-09 15:49:02 +02001244
Michal Vaskod59035b2020-07-08 12:00:06 +02001245 /* insert/move the node */
Michal Vaskoe6323f62020-07-09 15:49:02 +02001246 if (str_val[0]) {
1247 ret = lyd_diff_insert(first_node, parent_node, match, str_val);
Michal Vaskod59035b2020-07-08 12:00:06 +02001248 } else {
1249 ret = lyd_diff_insert(first_node, parent_node, match, NULL);
1250 }
1251 if (ret) {
Michal Vaskoe6323f62020-07-09 15:49:02 +02001252 if (op == LYD_DIFF_OP_CREATE) {
Michal Vaskod59035b2020-07-08 12:00:06 +02001253 lyd_free_tree(match);
1254 }
1255 return ret;
1256 }
1257
1258 goto next_iter_r;
1259 }
1260
1261 /* apply operation */
Michal Vaskoe6323f62020-07-09 15:49:02 +02001262 switch (op) {
1263 case LYD_DIFF_OP_NONE:
Michal Vaskod59035b2020-07-08 12:00:06 +02001264 /* find the node */
Michal Vaskoe78faec2021-04-08 17:24:43 +02001265 LY_CHECK_RET(lyd_diff_find_match(*first_node, diff_node, 1, dup_inst, &match));
Michal Vasko52afd7d2022-01-18 14:08:34 +01001266 LY_CHECK_ERR_RET(!match, LOGERR_NOINST(ctx, diff_node), LY_EINVAL);
Michal Vaskod59035b2020-07-08 12:00:06 +02001267
1268 if (match->schema->nodetype & LYD_NODE_TERM) {
1269 /* special case of only dflt flag change */
1270 if (diff_node->flags & LYD_DEFAULT) {
1271 match->flags |= LYD_DEFAULT;
1272 } else {
1273 match->flags &= ~LYD_DEFAULT;
1274 }
1275 } else {
1276 /* none operation on nodes without children is redundant and hence forbidden */
Radek Krejcia1c1e542020-09-29 16:06:52 +02001277 if (!lyd_child_no_keys(diff_node)) {
Michal Vasko0ff97752022-01-18 16:35:41 +01001278 LOGERR(ctx, LY_EINVAL, "Operation \"none\" is invalid for node \"%s\" without children.",
1279 LYD_NAME(diff_node));
1280 return LY_EINVAL;
Michal Vaskod59035b2020-07-08 12:00:06 +02001281 }
1282 }
1283 break;
Michal Vaskoe6323f62020-07-09 15:49:02 +02001284 case LYD_DIFF_OP_CREATE:
Michal Vaskod59035b2020-07-08 12:00:06 +02001285 /* duplicate the node */
Michal Vasko3a41dff2020-07-15 14:30:28 +02001286 LY_CHECK_RET(lyd_dup_single(diff_node, NULL, LYD_DUP_NO_META, &match));
Michal Vaskod59035b2020-07-08 12:00:06 +02001287
1288 /* insert it at the end */
1289 ret = 0;
Michal Vaskob104f112020-07-17 09:54:54 +02001290 if (parent_node) {
Michal Vasko19175b62022-04-01 09:17:07 +02001291 if (match->flags & LYD_EXT) {
Michal Vasko193dacd2022-10-13 08:43:05 +02001292 ret = lyplg_ext_insert(parent_node, match);
Michal Vasko19175b62022-04-01 09:17:07 +02001293 } else {
1294 ret = lyd_insert_child(parent_node, match);
1295 }
Michal Vaskod59035b2020-07-08 12:00:06 +02001296 } else {
Michal Vaskob104f112020-07-17 09:54:54 +02001297 ret = lyd_insert_sibling(*first_node, match, first_node);
Michal Vaskod59035b2020-07-08 12:00:06 +02001298 }
1299 if (ret) {
1300 lyd_free_tree(match);
1301 return ret;
1302 }
1303
1304 break;
Michal Vaskoe6323f62020-07-09 15:49:02 +02001305 case LYD_DIFF_OP_DELETE:
Michal Vaskod59035b2020-07-08 12:00:06 +02001306 /* find the node */
Michal Vaskoe78faec2021-04-08 17:24:43 +02001307 LY_CHECK_RET(lyd_diff_find_match(*first_node, diff_node, 1, dup_inst, &match));
Michal Vasko52afd7d2022-01-18 14:08:34 +01001308 LY_CHECK_ERR_RET(!match, LOGERR_NOINST(ctx, diff_node), LY_EINVAL);
Michal Vaskod59035b2020-07-08 12:00:06 +02001309
1310 /* remove it */
1311 if ((match == *first_node) && !match->parent) {
1312 assert(!parent_node);
1313 /* we have removed the top-level node */
1314 *first_node = (*first_node)->next;
1315 }
1316 lyd_free_tree(match);
1317
1318 /* we are not going recursively in this case, the whole subtree was already deleted */
1319 return LY_SUCCESS;
Michal Vaskoe6323f62020-07-09 15:49:02 +02001320 case LYD_DIFF_OP_REPLACE:
Michal Vasko0ff97752022-01-18 16:35:41 +01001321 if (!(diff_node->schema->nodetype & (LYS_LEAF | LYS_ANYDATA))) {
1322 LOGERR(ctx, LY_EINVAL, "Operation \"replace\" is invalid for %s node \"%s\".",
1323 lys_nodetype2str(diff_node->schema->nodetype), LYD_NAME(diff_node));
1324 return LY_EINVAL;
1325 }
Michal Vaskod59035b2020-07-08 12:00:06 +02001326
1327 /* find the node */
Michal Vaskoe78faec2021-04-08 17:24:43 +02001328 LY_CHECK_RET(lyd_diff_find_match(*first_node, diff_node, 1, dup_inst, &match));
Michal Vasko52afd7d2022-01-18 14:08:34 +01001329 LY_CHECK_ERR_RET(!match, LOGERR_NOINST(ctx, diff_node), LY_EINVAL);
Michal Vaskod59035b2020-07-08 12:00:06 +02001330
Michal Vaskobaba84e2021-02-05 16:33:30 +01001331 /* update the value */
1332 if (diff_node->schema->nodetype == LYS_LEAF) {
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02001333 ret = lyd_change_term(match, lyd_get_value(diff_node));
Michal Vasko52afd7d2022-01-18 14:08:34 +01001334 LY_CHECK_ERR_RET(ret && (ret != LY_EEXIST), LOGERR_UNEXPVAL(ctx, match, "data"), LY_EINVAL);
Michal Vaskobaba84e2021-02-05 16:33:30 +01001335 } else {
1336 struct lyd_node_any *any = (struct lyd_node_any *)diff_node;
Michal Vasko26bbb272022-08-02 14:54:33 +02001337
Michal Vaskoe78faec2021-04-08 17:24:43 +02001338 LY_CHECK_RET(lyd_any_copy_value(match, &any->value, any->value_type));
Michal Vaskod59035b2020-07-08 12:00:06 +02001339 }
1340
1341 /* with flags */
1342 match->flags = diff_node->flags;
1343 break;
1344 default:
1345 LOGINT_RET(ctx);
1346 }
1347
1348next_iter_r:
1349 if (diff_cb) {
1350 /* call callback */
1351 LY_CHECK_RET(diff_cb(diff_node, match, cb_data));
1352 }
1353
1354 /* apply diff recursively */
Michal Vaskoe78faec2021-04-08 17:24:43 +02001355 ret = LY_SUCCESS;
Radek Krejcia1c1e542020-09-29 16:06:52 +02001356 LY_LIST_FOR(lyd_child_no_keys(diff_node), diff_child) {
Michal Vaskoe78faec2021-04-08 17:24:43 +02001357 ret = lyd_diff_apply_r(lyd_node_child_p(match), match, diff_child, diff_cb, cb_data, &child_dup_inst);
1358 if (ret) {
1359 break;
1360 }
Michal Vaskod59035b2020-07-08 12:00:06 +02001361 }
1362
Michal Vaskod7c048c2021-05-18 16:12:55 +02001363 lyd_dup_inst_free(child_dup_inst);
Michal Vaskoe78faec2021-04-08 17:24:43 +02001364 return ret;
Michal Vaskod59035b2020-07-08 12:00:06 +02001365}
1366
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001367LIBYANG_API_DEF LY_ERR
Michal Vaskod59035b2020-07-08 12:00:06 +02001368lyd_diff_apply_module(struct lyd_node **data, const struct lyd_node *diff, const struct lys_module *mod,
Radek Krejci0f969882020-08-21 16:56:47 +02001369 lyd_diff_cb diff_cb, void *cb_data)
Michal Vaskod59035b2020-07-08 12:00:06 +02001370{
1371 const struct lyd_node *root;
Michal Vasko8efac242023-03-30 08:24:56 +02001372 struct ly_ht *dup_inst = NULL;
Michal Vaskoe78faec2021-04-08 17:24:43 +02001373 LY_ERR ret = LY_SUCCESS;
Michal Vaskod59035b2020-07-08 12:00:06 +02001374
1375 LY_LIST_FOR(diff, root) {
1376 if (mod && (lyd_owner_module(root) != mod)) {
1377 /* skip data nodes from different modules */
1378 continue;
1379 }
1380
1381 /* apply relevant nodes from the diff datatree */
Michal Vaskoe78faec2021-04-08 17:24:43 +02001382 ret = lyd_diff_apply_r(data, NULL, root, diff_cb, cb_data, &dup_inst);
1383 if (ret) {
1384 break;
1385 }
Michal Vaskod59035b2020-07-08 12:00:06 +02001386 }
1387
Michal Vaskod7c048c2021-05-18 16:12:55 +02001388 lyd_dup_inst_free(dup_inst);
Michal Vaskoe78faec2021-04-08 17:24:43 +02001389 return ret;
Michal Vaskod59035b2020-07-08 12:00:06 +02001390}
1391
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001392LIBYANG_API_DEF LY_ERR
Michal Vasko3a41dff2020-07-15 14:30:28 +02001393lyd_diff_apply_all(struct lyd_node **data, const struct lyd_node *diff)
Michal Vaskod59035b2020-07-08 12:00:06 +02001394{
1395 return lyd_diff_apply_module(data, diff, NULL, NULL, NULL);
1396}
Michal Vaskoe6323f62020-07-09 15:49:02 +02001397
1398/**
1399 * @brief Update operations on a diff node when the new operation is NONE.
1400 *
1401 * @param[in] diff_match Node from the diff.
Michal Vaskoe78faec2021-04-08 17:24:43 +02001402 * @param[in] cur_op Current operation of @p diff_match.
Michal Vaskoe6323f62020-07-09 15:49:02 +02001403 * @param[in] src_diff Current source diff node.
1404 * @return LY_ERR value.
1405 */
1406static LY_ERR
1407lyd_diff_merge_none(struct lyd_node *diff_match, enum lyd_diff_op cur_op, const struct lyd_node *src_diff)
1408{
1409 switch (cur_op) {
1410 case LYD_DIFF_OP_NONE:
1411 case LYD_DIFF_OP_CREATE:
1412 case LYD_DIFF_OP_REPLACE:
1413 if (src_diff->schema->nodetype & LYD_NODE_TERM) {
1414 /* NONE on a term means only its dflt flag was changed */
1415 diff_match->flags &= ~LYD_DEFAULT;
1416 diff_match->flags |= src_diff->flags & LYD_DEFAULT;
1417 }
1418 break;
1419 default:
1420 /* delete operation is not valid */
Michal Vasko52afd7d2022-01-18 14:08:34 +01001421 LOGERR_MERGEOP(LYD_CTX(diff_match), diff_match, cur_op, LYD_DIFF_OP_NONE);
1422 return LY_EINVAL;
Michal Vaskoe6323f62020-07-09 15:49:02 +02001423 }
1424
1425 return LY_SUCCESS;
1426}
1427
1428/**
Michal Vaskoe6323f62020-07-09 15:49:02 +02001429 * @brief Set a specific operation of a node. Delete the previous operation, if any.
Michal Vasko871a0252020-11-11 18:35:24 +01001430 * Does not change the default flag.
Michal Vaskoe6323f62020-07-09 15:49:02 +02001431 *
1432 * @param[in] node Node to change.
1433 * @param[in] op Operation to set.
1434 * @return LY_ERR value.
1435 */
1436static LY_ERR
1437lyd_diff_change_op(struct lyd_node *node, enum lyd_diff_op op)
1438{
Michal Vaskodb91fc32023-05-02 14:39:40 +02001439 lyd_diff_del_meta(node, "operation");
Michal Vaskoe6323f62020-07-09 15:49:02 +02001440
Michal Vaskodb91fc32023-05-02 14:39:40 +02001441 if (node->schema) {
1442 return lyd_new_meta(LYD_CTX(node), node, NULL, "yang:operation", lyd_diff_op2str(op), 0, NULL);
1443 } else {
1444 return lyd_new_attr(node, "yang", "operation", lyd_diff_op2str(op), NULL);
Michal Vaskoe6323f62020-07-09 15:49:02 +02001445 }
Michal Vaskoe6323f62020-07-09 15:49:02 +02001446}
1447
1448/**
1449 * @brief Update operations on a diff node when the new operation is REPLACE.
1450 *
1451 * @param[in] diff_match Node from the diff.
Michal Vaskoe78faec2021-04-08 17:24:43 +02001452 * @param[in] cur_op Current operation of @p diff_match.
Michal Vaskoe6323f62020-07-09 15:49:02 +02001453 * @param[in] src_diff Current source diff node.
1454 * @return LY_ERR value.
1455 */
1456static LY_ERR
1457lyd_diff_merge_replace(struct lyd_node *diff_match, enum lyd_diff_op cur_op, const struct lyd_node *src_diff)
1458{
1459 LY_ERR ret;
Michal Vaskoe78faec2021-04-08 17:24:43 +02001460 const char *str_val, *meta_name, *orig_meta_name;
Michal Vaskoe6323f62020-07-09 15:49:02 +02001461 struct lyd_meta *meta;
1462 const struct lys_module *mod;
1463 const struct lyd_node_any *any;
Michal Vasko52afd7d2022-01-18 14:08:34 +01001464 const struct ly_ctx *ctx = LYD_CTX(diff_match);
Michal Vaskoe6323f62020-07-09 15:49:02 +02001465
1466 /* get "yang" module for the metadata */
Michal Vaskob7be7a82020-08-20 09:09:04 +02001467 mod = ly_ctx_get_module_latest(LYD_CTX(diff_match), "yang");
Michal Vaskoe6323f62020-07-09 15:49:02 +02001468 assert(mod);
1469
1470 switch (cur_op) {
1471 case LYD_DIFF_OP_REPLACE:
1472 case LYD_DIFF_OP_CREATE:
1473 switch (diff_match->schema->nodetype) {
1474 case LYS_LIST:
1475 case LYS_LEAFLIST:
Michal Vasko4231fb62020-07-13 13:54:47 +02001476 /* it was created/moved somewhere, but now it will be created/moved somewhere else,
Michal Vaskoe6323f62020-07-09 15:49:02 +02001477 * keep orig_key/orig_value (only replace oper) and replace key/value */
1478 assert(lysc_is_userordered(diff_match->schema));
Michal Vaskoe78faec2021-04-08 17:24:43 +02001479 if (lysc_is_dup_inst_list(diff_match->schema)) {
1480 meta_name = "position";
1481 } else if (diff_match->schema->nodetype == LYS_LIST) {
1482 meta_name = "key";
1483 } else {
1484 meta_name = "value";
1485 }
Michal Vaskoe6323f62020-07-09 15:49:02 +02001486
1487 lyd_diff_del_meta(diff_match, meta_name);
1488 meta = lyd_find_meta(src_diff->meta, mod, meta_name);
Michal Vasko52afd7d2022-01-18 14:08:34 +01001489 LY_CHECK_ERR_RET(!meta, LOGERR_META(ctx, meta_name, src_diff), LY_EINVAL);
Michal Vasko3a41dff2020-07-15 14:30:28 +02001490 LY_CHECK_RET(lyd_dup_meta_single(meta, diff_match, NULL));
Michal Vaskoe6323f62020-07-09 15:49:02 +02001491 break;
1492 case LYS_LEAF:
1493 /* replaced with the exact same value, impossible */
Michal Vasko8f359bf2020-07-28 10:41:15 +02001494 if (!lyd_compare_single(diff_match, src_diff, 0)) {
Michal Vasko52afd7d2022-01-18 14:08:34 +01001495 LOGERR_UNEXPVAL(ctx, diff_match, "target diff");
1496 return LY_EINVAL;
Michal Vaskoe6323f62020-07-09 15:49:02 +02001497 }
1498
Michal Vaskoe6323f62020-07-09 15:49:02 +02001499 /* modify the node value */
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02001500 if (lyd_change_term(diff_match, lyd_get_value(src_diff))) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02001501 LOGINT_RET(LYD_CTX(src_diff));
Michal Vaskoe6323f62020-07-09 15:49:02 +02001502 }
1503
Michal Vasko8caadab2020-11-05 17:38:15 +01001504 if (cur_op == LYD_DIFF_OP_REPLACE) {
1505 /* compare values whether there is any change at all */
1506 meta = lyd_find_meta(diff_match->meta, mod, "orig-value");
Michal Vasko52afd7d2022-01-18 14:08:34 +01001507 LY_CHECK_ERR_RET(!meta, LOGERR_META(ctx, "orig-value", diff_match), LY_EINVAL);
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02001508 str_val = lyd_get_meta_value(meta);
Michal Vasko8caadab2020-11-05 17:38:15 +01001509 ret = lyd_value_compare((struct lyd_node_term *)diff_match, str_val, strlen(str_val));
1510 if (!ret) {
1511 /* values are the same, remove orig-value meta and set oper to NONE */
1512 lyd_free_meta_single(meta);
1513 LY_CHECK_RET(lyd_diff_change_op(diff_match, LYD_DIFF_OP_NONE));
1514 }
Michal Vaskoe6323f62020-07-09 15:49:02 +02001515 }
1516
1517 /* modify the default flag */
1518 diff_match->flags &= ~LYD_DEFAULT;
1519 diff_match->flags |= src_diff->flags & LYD_DEFAULT;
1520 break;
1521 case LYS_ANYXML:
1522 case LYS_ANYDATA:
Michal Vasko8f359bf2020-07-28 10:41:15 +02001523 if (!lyd_compare_single(diff_match, src_diff, 0)) {
Michal Vasko52afd7d2022-01-18 14:08:34 +01001524 LOGERR_UNEXPVAL(ctx, diff_match, "target diff");
1525 return LY_EINVAL;
Michal Vaskoe6323f62020-07-09 15:49:02 +02001526 }
1527
1528 /* modify the node value */
1529 any = (struct lyd_node_any *)src_diff;
1530 LY_CHECK_RET(lyd_any_copy_value(diff_match, &any->value, any->value_type));
1531 break;
1532 default:
Michal Vaskob7be7a82020-08-20 09:09:04 +02001533 LOGINT_RET(LYD_CTX(src_diff));
Michal Vaskoe6323f62020-07-09 15:49:02 +02001534 }
1535 break;
1536 case LYD_DIFF_OP_NONE:
1537 /* it is moved now */
1538 assert(lysc_is_userordered(diff_match->schema) && (diff_match->schema->nodetype == LYS_LIST));
1539
1540 /* change the operation */
1541 LY_CHECK_RET(lyd_diff_change_op(diff_match, LYD_DIFF_OP_REPLACE));
1542
Michal Vaskoe78faec2021-04-08 17:24:43 +02001543 /* set orig-meta and meta */
1544 if (lysc_is_dup_inst_list(diff_match->schema)) {
1545 meta_name = "position";
1546 orig_meta_name = "orig-position";
1547 } else {
1548 meta_name = "key";
1549 orig_meta_name = "orig-key";
1550 }
1551
1552 meta = lyd_find_meta(src_diff->meta, mod, orig_meta_name);
Michal Vasko52afd7d2022-01-18 14:08:34 +01001553 LY_CHECK_ERR_RET(!meta, LOGERR_META(ctx, orig_meta_name, src_diff), LY_EINVAL);
Michal Vasko3a41dff2020-07-15 14:30:28 +02001554 LY_CHECK_RET(lyd_dup_meta_single(meta, diff_match, NULL));
Michal Vaskoe6323f62020-07-09 15:49:02 +02001555
Michal Vaskoe78faec2021-04-08 17:24:43 +02001556 meta = lyd_find_meta(src_diff->meta, mod, meta_name);
Michal Vasko52afd7d2022-01-18 14:08:34 +01001557 LY_CHECK_ERR_RET(!meta, LOGERR_META(ctx, meta_name, src_diff), LY_EINVAL);
Michal Vasko3a41dff2020-07-15 14:30:28 +02001558 LY_CHECK_RET(lyd_dup_meta_single(meta, diff_match, NULL));
Michal Vaskoe6323f62020-07-09 15:49:02 +02001559 break;
1560 default:
1561 /* delete operation is not valid */
Michal Vasko52afd7d2022-01-18 14:08:34 +01001562 LOGERR_MERGEOP(ctx, diff_match, cur_op, LYD_DIFF_OP_REPLACE);
1563 return LY_EINVAL;
Michal Vaskoe6323f62020-07-09 15:49:02 +02001564 }
1565
1566 return LY_SUCCESS;
1567}
1568
1569/**
1570 * @brief Update operations in a diff node when the new operation is CREATE.
1571 *
Michal Vasko4f331162023-08-15 10:07:44 +02001572 * @param[in,out] diff_match Node from the diff, may be replaced.
1573 * @param[in,out] diff Diff root node, may be updated.
Michal Vaskoe78faec2021-04-08 17:24:43 +02001574 * @param[in] cur_op Current operation of @p diff_match.
Michal Vaskoe6323f62020-07-09 15:49:02 +02001575 * @param[in] src_diff Current source diff node.
Michal Vaskoc0e58e82020-11-11 19:04:33 +01001576 * @param[in] options Diff merge options.
Michal Vaskoe6323f62020-07-09 15:49:02 +02001577 * @return LY_ERR value.
1578 */
1579static LY_ERR
Michal Vasko4f331162023-08-15 10:07:44 +02001580lyd_diff_merge_create(struct lyd_node **diff_match, struct lyd_node **diff, enum lyd_diff_op cur_op,
1581 const struct lyd_node *src_diff, uint16_t options)
Michal Vaskoe6323f62020-07-09 15:49:02 +02001582{
Michal Vasko4f331162023-08-15 10:07:44 +02001583 struct lyd_node *child, *src_dup, *to_free = NULL;
Michal Vaskoc0e58e82020-11-11 19:04:33 +01001584 const struct lysc_node_leaf *sleaf = NULL;
Michal Vasko871a0252020-11-11 18:35:24 +01001585 uint32_t trg_flags;
Michal Vaskoe78faec2021-04-08 17:24:43 +02001586 const char *meta_name, *orig_meta_name;
1587 struct lyd_meta *meta, *orig_meta;
Michal Vasko4f331162023-08-15 10:07:44 +02001588 const struct ly_ctx *ctx = LYD_CTX(*diff_match);
1589 LY_ERR r;
1590
1591 /* create operation is valid only for data nodes */
1592 LY_CHECK_ERR_RET(!src_diff->schema, LOGINT(ctx), LY_EINT);
Michal Vaskoe6323f62020-07-09 15:49:02 +02001593
1594 switch (cur_op) {
1595 case LYD_DIFF_OP_DELETE:
Michal Vasko871a0252020-11-11 18:35:24 +01001596 /* remember current flags */
Michal Vasko4f331162023-08-15 10:07:44 +02001597 trg_flags = (*diff_match)->flags;
Michal Vasko871a0252020-11-11 18:35:24 +01001598
Michal Vasko4f331162023-08-15 10:07:44 +02001599 if (lysc_is_userordered(src_diff->schema)) {
1600 assert((*diff_match)->schema);
1601
Michal Vaskoe78faec2021-04-08 17:24:43 +02001602 /* get anchor metadata */
Michal Vasko4f331162023-08-15 10:07:44 +02001603 if (lysc_is_dup_inst_list((*diff_match)->schema)) {
Michal Vaskoe78faec2021-04-08 17:24:43 +02001604 meta_name = "yang:position";
1605 orig_meta_name = "yang:orig-position";
Michal Vasko4f331162023-08-15 10:07:44 +02001606 } else if ((*diff_match)->schema->nodetype == LYS_LIST) {
Michal Vaskoe78faec2021-04-08 17:24:43 +02001607 meta_name = "yang:key";
1608 orig_meta_name = "yang:orig-key";
1609 } else {
1610 meta_name = "yang:value";
1611 orig_meta_name = "yang:orig-value";
1612 }
1613 meta = lyd_find_meta(src_diff->meta, NULL, meta_name);
Michal Vasko52afd7d2022-01-18 14:08:34 +01001614 LY_CHECK_ERR_RET(!meta, LOGERR_META(ctx, meta_name, src_diff), LY_EINVAL);
Michal Vasko4f331162023-08-15 10:07:44 +02001615 orig_meta = lyd_find_meta((*diff_match)->meta, NULL, orig_meta_name);
1616 LY_CHECK_ERR_RET(!orig_meta, LOGERR_META(ctx, orig_meta_name, *diff_match), LY_EINVAL);
Michal Vaskoe78faec2021-04-08 17:24:43 +02001617
1618 /* the (incorrect) assumption made here is that there are no previous diff nodes that would affect
1619 * the anchors stored in the metadata */
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02001620 if (strcmp(lyd_get_meta_value(meta), lyd_get_meta_value(orig_meta))) {
Michal Vaskoe78faec2021-04-08 17:24:43 +02001621 /* deleted + created at another position -> operation REPLACE */
Michal Vasko4f331162023-08-15 10:07:44 +02001622 LY_CHECK_RET(lyd_diff_change_op(*diff_match, LYD_DIFF_OP_REPLACE));
Michal Vaskoe78faec2021-04-08 17:24:43 +02001623
1624 /* add anchor metadata */
Michal Vasko4f331162023-08-15 10:07:44 +02001625 LY_CHECK_RET(lyd_dup_meta_single(meta, *diff_match, NULL));
Michal Vaskoe78faec2021-04-08 17:24:43 +02001626 } else {
1627 /* deleted + created at the same position -> operation NONE */
Michal Vasko4f331162023-08-15 10:07:44 +02001628 LY_CHECK_RET(lyd_diff_change_op(*diff_match, LYD_DIFF_OP_NONE));
Michal Vaskoe78faec2021-04-08 17:24:43 +02001629
1630 /* delete anchor metadata */
1631 lyd_free_meta_single(orig_meta);
1632 }
Michal Vasko4f331162023-08-15 10:07:44 +02001633 } else if (src_diff->schema->nodetype == LYS_LEAF) {
Michal Vaskoe78faec2021-04-08 17:24:43 +02001634 if (options & LYD_DIFF_MERGE_DEFAULTS) {
1635 /* we are dealing with a leaf and are handling default values specially (as explicit nodes) */
Michal Vasko4f331162023-08-15 10:07:44 +02001636 sleaf = (struct lysc_node_leaf *)src_diff->schema;
Michal Vaskoe78faec2021-04-08 17:24:43 +02001637 }
1638
Radek Krejci55c4bd22021-04-26 08:09:04 +02001639 if (sleaf && sleaf->dflt && !sleaf->dflt->realtype->plugin->compare(sleaf->dflt,
1640 &((struct lyd_node_term *)src_diff)->value)) {
Michal Vaskoe78faec2021-04-08 17:24:43 +02001641 /* we deleted it, so a default value was in-use, and it matches the created value -> operation NONE */
Michal Vasko4f331162023-08-15 10:07:44 +02001642 LY_CHECK_RET(lyd_diff_change_op(*diff_match, LYD_DIFF_OP_NONE));
1643 } else if (!lyd_compare_single(*diff_match, src_diff, 0)) {
Michal Vaskoe78faec2021-04-08 17:24:43 +02001644 /* deleted + created -> operation NONE */
Michal Vasko4f331162023-08-15 10:07:44 +02001645 LY_CHECK_RET(lyd_diff_change_op(*diff_match, LYD_DIFF_OP_NONE));
1646 } else if ((*diff_match)->schema) {
Michal Vaskoe78faec2021-04-08 17:24:43 +02001647 /* we deleted it, but it was created with a different value -> operation REPLACE */
Michal Vasko4f331162023-08-15 10:07:44 +02001648 LY_CHECK_RET(lyd_diff_change_op(*diff_match, LYD_DIFF_OP_REPLACE));
Michal Vaskoe78faec2021-04-08 17:24:43 +02001649
1650 /* current value is the previous one (meta) */
Michal Vasko4f331162023-08-15 10:07:44 +02001651 LY_CHECK_RET(lyd_new_meta(LYD_CTX(src_diff), *diff_match, NULL, "yang:orig-value",
1652 lyd_get_value(*diff_match), 0, NULL));
Michal Vaskoe78faec2021-04-08 17:24:43 +02001653
1654 /* update the value itself */
Michal Vasko4f331162023-08-15 10:07:44 +02001655 LY_CHECK_RET(lyd_change_term(*diff_match, lyd_get_value(src_diff)));
1656 } else {
1657 /* also operation REPLACE but we need to change an opaque node into a data node */
1658 LY_CHECK_RET(lyd_dup_single(src_diff, (*diff_match)->parent, LYD_DUP_NO_META | LYD_DUP_WITH_FLAGS, &src_dup));
1659 if (!(*diff_match)->parent) {
1660 /* will always be inserted before diff_match, which is opaque */
1661 LY_CHECK_RET(lyd_insert_sibling(*diff_match, src_dup, diff));
1662 }
1663 to_free = *diff_match;
1664 *diff_match = src_dup;
1665
1666 r = lyd_new_meta(ctx, src_dup, NULL, "yang:orig-value", lyd_get_value(to_free), 0, NULL);
1667 lyd_free_tree(to_free);
1668 LY_CHECK_RET(r);
1669 LY_CHECK_RET(lyd_new_meta(ctx, src_dup, NULL, "yang:operation", lyd_diff_op2str(LYD_DIFF_OP_REPLACE), 0, NULL));
Michal Vaskoe78faec2021-04-08 17:24:43 +02001670 }
1671 } else {
Michal Vaskoe6323f62020-07-09 15:49:02 +02001672 /* deleted + created -> operation NONE */
Michal Vasko4f331162023-08-15 10:07:44 +02001673 LY_CHECK_RET(lyd_diff_change_op(*diff_match, LYD_DIFF_OP_NONE));
Michal Vaskoe6323f62020-07-09 15:49:02 +02001674 }
1675
Michal Vasko4f331162023-08-15 10:07:44 +02001676 assert((*diff_match)->schema);
1677 if ((*diff_match)->schema->nodetype & LYD_NODE_TERM) {
Michal Vasko4b715ca2020-11-11 18:39:57 +01001678 /* add orig-dflt metadata */
Michal Vasko4f331162023-08-15 10:07:44 +02001679 LY_CHECK_RET(lyd_new_meta(LYD_CTX(src_diff), *diff_match, NULL, "yang:orig-default",
Michal Vasko4b715ca2020-11-11 18:39:57 +01001680 trg_flags & LYD_DEFAULT ? "true" : "false", 0, NULL));
1681
Michal Vaskoe6323f62020-07-09 15:49:02 +02001682 /* update dflt flag itself */
Michal Vasko4f331162023-08-15 10:07:44 +02001683 (*diff_match)->flags &= ~LYD_DEFAULT;
1684 (*diff_match)->flags |= src_diff->flags & LYD_DEFAULT;
Michal Vaskoe78faec2021-04-08 17:24:43 +02001685 }
1686
1687 /* but the operation of its children should remain DELETE */
Michal Vasko4f331162023-08-15 10:07:44 +02001688 LY_LIST_FOR(lyd_child_no_keys(*diff_match), child) {
Michal Vaskoe78faec2021-04-08 17:24:43 +02001689 LY_CHECK_RET(lyd_diff_change_op(child, LYD_DIFF_OP_DELETE));
Michal Vaskoe6323f62020-07-09 15:49:02 +02001690 }
1691 break;
1692 default:
1693 /* create and replace operations are not valid */
Michal Vasko4f331162023-08-15 10:07:44 +02001694 LOGERR_MERGEOP(LYD_CTX(src_diff), *diff_match, cur_op, LYD_DIFF_OP_CREATE);
Michal Vasko52afd7d2022-01-18 14:08:34 +01001695 return LY_EINVAL;
Michal Vaskoe6323f62020-07-09 15:49:02 +02001696 }
1697
1698 return LY_SUCCESS;
1699}
1700
1701/**
1702 * @brief Update operations on a diff node when the new operation is DELETE.
1703 *
1704 * @param[in] diff_match Node from the diff.
Michal Vaskoe78faec2021-04-08 17:24:43 +02001705 * @param[in] cur_op Current operation of @p diff_match.
Michal Vaskoe6323f62020-07-09 15:49:02 +02001706 * @param[in] src_diff Current source diff node.
1707 * @return LY_ERR value.
1708 */
1709static LY_ERR
1710lyd_diff_merge_delete(struct lyd_node *diff_match, enum lyd_diff_op cur_op, const struct lyd_node *src_diff)
1711{
Michal Vasko17d0c5c2021-11-01 11:31:11 +01001712 struct lyd_node *child;
Michal Vaskoe78faec2021-04-08 17:24:43 +02001713 struct lyd_meta *meta;
1714 const char *meta_name;
Michal Vasko52afd7d2022-01-18 14:08:34 +01001715 const struct ly_ctx *ctx = LYD_CTX(diff_match);
Michal Vaskoe6323f62020-07-09 15:49:02 +02001716
1717 /* we can delete only exact existing nodes */
Michal Vaskob7be7a82020-08-20 09:09:04 +02001718 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 +02001719
1720 switch (cur_op) {
1721 case LYD_DIFF_OP_CREATE:
1722 /* it was created, but then deleted -> set NONE operation */
1723 LY_CHECK_RET(lyd_diff_change_op(diff_match, LYD_DIFF_OP_NONE));
1724
1725 if (diff_match->schema->nodetype & LYD_NODE_TERM) {
1726 /* add orig-default meta because it is expected */
Michal Vasko871a0252020-11-11 18:35:24 +01001727 LY_CHECK_RET(lyd_new_meta(LYD_CTX(src_diff), diff_match, NULL, "yang:orig-default",
Michal Vaskof933e662023-12-05 09:56:30 +01001728 src_diff->flags & LYD_DEFAULT ? "true" : "false", 0, NULL));
Michal Vaskoe78faec2021-04-08 17:24:43 +02001729 } else if (!lysc_is_dup_inst_list(diff_match->schema)) {
Michal Vaskoe6323f62020-07-09 15:49:02 +02001730 /* keep operation for all descendants (for now) */
Radek Krejcia1c1e542020-09-29 16:06:52 +02001731 LY_LIST_FOR(lyd_child_no_keys(diff_match), child) {
Michal Vaskoe6323f62020-07-09 15:49:02 +02001732 LY_CHECK_RET(lyd_diff_change_op(child, cur_op));
1733 }
Michal Vaskoe78faec2021-04-08 17:24:43 +02001734 } /* else key-less list, for which all the descendants act as keys */
Michal Vaskoe6323f62020-07-09 15:49:02 +02001735 break;
1736 case LYD_DIFF_OP_REPLACE:
Michal Vasko17d0c5c2021-11-01 11:31:11 +01001737 /* remove the redundant metadata */
Michal Vaskoe78faec2021-04-08 17:24:43 +02001738 if (lysc_is_userordered(diff_match->schema)) {
1739 if (lysc_is_dup_inst_list(diff_match->schema)) {
1740 meta_name = "position";
1741 } else if (diff_match->schema->nodetype == LYS_LIST) {
1742 meta_name = "key";
1743 } else {
1744 meta_name = "value";
1745 }
1746 } else {
1747 assert(diff_match->schema->nodetype == LYS_LEAF);
1748
1749 /* switch value for the original one */
1750 meta = lyd_find_meta(diff_match->meta, NULL, "yang:orig-value");
Michal Vasko52afd7d2022-01-18 14:08:34 +01001751 LY_CHECK_ERR_RET(!meta, LOGERR_META(ctx, "yang:orig-value", diff_match), LY_EINVAL);
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02001752 if (lyd_change_term(diff_match, lyd_get_meta_value(meta))) {
Michal Vasko52afd7d2022-01-18 14:08:34 +01001753 LOGERR_UNEXPVAL(ctx, diff_match, "target diff");
1754 return LY_EINVAL;
Michal Vaskoe78faec2021-04-08 17:24:43 +02001755 }
1756
1757 /* switch default for the original one, then remove the meta */
1758 meta = lyd_find_meta(diff_match->meta, NULL, "yang:orig-default");
Michal Vasko52afd7d2022-01-18 14:08:34 +01001759 LY_CHECK_ERR_RET(!meta, LOGERR_META(ctx, "yang:orig-default", diff_match), LY_EINVAL);
Michal Vaskoe78faec2021-04-08 17:24:43 +02001760 diff_match->flags &= ~LYD_DEFAULT;
1761 if (meta->value.boolean) {
1762 diff_match->flags |= LYD_DEFAULT;
1763 }
1764 lyd_free_meta_single(meta);
1765
1766 meta_name = "orig-value";
1767 }
1768 lyd_diff_del_meta(diff_match, meta_name);
1769
Michal Vasko17d0c5c2021-11-01 11:31:11 +01001770 /* it was being changed, but should be deleted instead -> set DELETE operation */
1771 LY_CHECK_RET(lyd_diff_change_op(diff_match, LYD_DIFF_OP_DELETE));
1772 break;
Michal Vaskoe6323f62020-07-09 15:49:02 +02001773 case LYD_DIFF_OP_NONE:
1774 /* it was not modified, but should be deleted -> set DELETE operation */
1775 LY_CHECK_RET(lyd_diff_change_op(diff_match, LYD_DIFF_OP_DELETE));
Michal Vaskoe6323f62020-07-09 15:49:02 +02001776 break;
1777 default:
1778 /* delete operation is not valid */
Michal Vasko52afd7d2022-01-18 14:08:34 +01001779 LOGERR_MERGEOP(LYD_CTX(diff_match), diff_match, cur_op, LYD_DIFF_OP_DELETE);
1780 return LY_EINVAL;
Michal Vaskoe6323f62020-07-09 15:49:02 +02001781 }
1782
1783 return LY_SUCCESS;
1784}
1785
1786/**
1787 * @brief Check whether this diff node is redundant (does not change data).
1788 *
1789 * @param[in] diff Diff node.
1790 * @return 0 if not, non-zero if it is.
1791 */
1792static int
1793lyd_diff_is_redundant(struct lyd_node *diff)
1794{
1795 enum lyd_diff_op op;
1796 struct lyd_meta *meta, *orig_val_meta = NULL, *val_meta = NULL;
1797 struct lyd_node *child;
1798 const struct lys_module *mod;
Michal Vaskoe78faec2021-04-08 17:24:43 +02001799 const char *str, *orig_meta_name, *meta_name;
Michal Vaskoe6323f62020-07-09 15:49:02 +02001800
1801 assert(diff);
1802
Michal Vaskoe78faec2021-04-08 17:24:43 +02001803 if (lysc_is_dup_inst_list(diff->schema)) {
1804 /* all descendants are keys */
1805 child = NULL;
1806 } else {
1807 child = lyd_child_no_keys(diff);
1808 }
Michal Vaskob7be7a82020-08-20 09:09:04 +02001809 mod = ly_ctx_get_module_latest(LYD_CTX(diff), "yang");
Michal Vaskoe6323f62020-07-09 15:49:02 +02001810 assert(mod);
1811
1812 /* get node operation */
Michal Vasko53bf6f22020-07-14 08:23:40 +02001813 LY_CHECK_RET(lyd_diff_get_op(diff, &op), 0);
Michal Vaskoe6323f62020-07-09 15:49:02 +02001814
1815 if ((op == LYD_DIFF_OP_REPLACE) && lysc_is_userordered(diff->schema)) {
Michal Vaskoe78faec2021-04-08 17:24:43 +02001816 /* get metadata names */
1817 if (lysc_is_dup_inst_list(diff->schema)) {
1818 meta_name = "position";
1819 orig_meta_name = "orig-position";
1820 } else if (diff->schema->nodetype == LYS_LIST) {
1821 meta_name = "key";
1822 orig_meta_name = "orig-key";
1823 } else {
1824 meta_name = "value";
1825 orig_meta_name = "orig-value";
1826 }
1827
Michal Vaskoe6323f62020-07-09 15:49:02 +02001828 /* check for redundant move */
Michal Vaskoe78faec2021-04-08 17:24:43 +02001829 orig_val_meta = lyd_find_meta(diff->meta, mod, orig_meta_name);
1830 val_meta = lyd_find_meta(diff->meta, mod, meta_name);
Michal Vaskoe6323f62020-07-09 15:49:02 +02001831 assert(orig_val_meta && val_meta);
1832
1833 if (!lyd_compare_meta(orig_val_meta, val_meta)) {
1834 /* there is actually no move */
Michal Vasko3a41dff2020-07-15 14:30:28 +02001835 lyd_free_meta_single(orig_val_meta);
1836 lyd_free_meta_single(val_meta);
Michal Vaskoe6323f62020-07-09 15:49:02 +02001837 if (child) {
1838 /* change operation to NONE, we have siblings */
1839 lyd_diff_change_op(diff, LYD_DIFF_OP_NONE);
1840 return 0;
1841 }
1842
1843 /* redundant node, BUT !!
1844 * In diff the move operation is always converted to be INSERT_AFTER, which is fine
1845 * because the data that this is applied on should not change for the diff lifetime.
1846 * However, when we are merging 2 diffs, this conversion is actually lossy because
1847 * if the data change, the move operation can also change its meaning. In this specific
1848 * case the move operation will be lost. But it can be considered a feature, it is not supported.
1849 */
1850 return 1;
1851 }
Michal Vaskodb91fc32023-05-02 14:39:40 +02001852 } else if (op == LYD_DIFF_OP_NONE) {
1853 if (!diff->schema) {
1854 /* opaque node with none must be redundant */
Michal Vaskoe6323f62020-07-09 15:49:02 +02001855 return 1;
1856 }
Michal Vaskodb91fc32023-05-02 14:39:40 +02001857
1858 if (diff->schema->nodetype & LYD_NODE_TERM) {
1859 /* check whether at least the default flags are different */
1860 meta = lyd_find_meta(diff->meta, mod, "orig-default");
1861 assert(meta);
1862 str = lyd_get_meta_value(meta);
1863
1864 /* if previous and current dflt flags are the same, this node is redundant */
1865 if ((!strcmp(str, "true") && (diff->flags & LYD_DEFAULT)) || (!strcmp(str, "false") && !(diff->flags & LYD_DEFAULT))) {
1866 return 1;
1867 }
1868 return 0;
1869 }
Michal Vaskoe6323f62020-07-09 15:49:02 +02001870 }
1871
1872 if (!child && (op == LYD_DIFF_OP_NONE)) {
1873 return 1;
1874 }
1875
1876 return 0;
1877}
1878
1879/**
Michal Vaskoe78faec2021-04-08 17:24:43 +02001880 * @brief Merge sysrepo diff subtree with another diff, recursively.
Michal Vaskoe6323f62020-07-09 15:49:02 +02001881 *
1882 * @param[in] src_diff Source diff node.
1883 * @param[in] diff_parent Current sysrepo diff parent.
1884 * @param[in] diff_cb Optional diff callback.
1885 * @param[in] cb_data User data for @p diff_cb.
Michal Vaskoe78faec2021-04-08 17:24:43 +02001886 * @param[in,out] dup_inst Duplicate instance cache for all @p src_diff siblings.
Michal Vaskoc0e58e82020-11-11 19:04:33 +01001887 * @param[in] options Diff merge options.
Michal Vaskoe6323f62020-07-09 15:49:02 +02001888 * @param[in,out] diff Diff root node.
1889 * @return LY_ERR value.
1890 */
1891static LY_ERR
1892lyd_diff_merge_r(const struct lyd_node *src_diff, struct lyd_node *diff_parent, lyd_diff_cb diff_cb, void *cb_data,
Michal Vasko8efac242023-03-30 08:24:56 +02001893 struct ly_ht **dup_inst, uint16_t options, struct lyd_node **diff)
Michal Vaskoe6323f62020-07-09 15:49:02 +02001894{
1895 LY_ERR ret = LY_SUCCESS;
1896 struct lyd_node *child, *diff_node = NULL;
1897 enum lyd_diff_op src_op, cur_op;
Michal Vasko8efac242023-03-30 08:24:56 +02001898 struct ly_ht *child_dup_inst = NULL;
Michal Vaskoe6323f62020-07-09 15:49:02 +02001899
1900 /* get source node operation */
1901 LY_CHECK_RET(lyd_diff_get_op(src_diff, &src_op));
1902
1903 /* find an equal node in the current diff */
Michal Vaskoe78faec2021-04-08 17:24:43 +02001904 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 +02001905
1906 if (diff_node) {
1907 /* get target (current) operation */
1908 LY_CHECK_RET(lyd_diff_get_op(diff_node, &cur_op));
1909
1910 /* merge operations */
1911 switch (src_op) {
1912 case LYD_DIFF_OP_REPLACE:
1913 ret = lyd_diff_merge_replace(diff_node, cur_op, src_diff);
1914 break;
1915 case LYD_DIFF_OP_CREATE:
Michal Vasko1dc0a842021-02-04 11:04:57 +01001916 if ((cur_op == LYD_DIFF_OP_CREATE) && lysc_is_dup_inst_list(diff_node->schema)) {
Michal Vaskoe78faec2021-04-08 17:24:43 +02001917 /* special case of creating duplicate (leaf-)list instances */
Michal Vasko1dc0a842021-02-04 11:04:57 +01001918 goto add_diff;
1919 }
1920
Michal Vasko4f331162023-08-15 10:07:44 +02001921 ret = lyd_diff_merge_create(&diff_node, diff, cur_op, src_diff, options);
Michal Vaskoe6323f62020-07-09 15:49:02 +02001922 break;
1923 case LYD_DIFF_OP_DELETE:
1924 ret = lyd_diff_merge_delete(diff_node, cur_op, src_diff);
1925 break;
1926 case LYD_DIFF_OP_NONE:
Michal Vaskoe78faec2021-04-08 17:24:43 +02001927 /* key-less list can never have "none" operation since all its descendants are acting as "keys" */
1928 assert((src_diff->schema->nodetype != LYS_LIST) || !lysc_is_dup_inst_list(src_diff->schema));
Michal Vaskoe6323f62020-07-09 15:49:02 +02001929 ret = lyd_diff_merge_none(diff_node, cur_op, src_diff);
1930 break;
1931 default:
Michal Vaskob7be7a82020-08-20 09:09:04 +02001932 LOGINT_RET(LYD_CTX(src_diff));
Michal Vaskoe6323f62020-07-09 15:49:02 +02001933 }
1934 if (ret) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02001935 LOGERR(LYD_CTX(src_diff), LY_EOTHER, "Merging operation \"%s\" failed.", lyd_diff_op2str(src_op));
Michal Vaskoe6323f62020-07-09 15:49:02 +02001936 return ret;
1937 }
1938
1939 if (diff_cb) {
1940 /* call callback */
Michal Vaskobc5fba92020-08-07 12:14:39 +02001941 LY_CHECK_RET(diff_cb(src_diff, diff_node, cb_data));
Michal Vaskoe6323f62020-07-09 15:49:02 +02001942 }
1943
1944 /* update diff parent */
1945 diff_parent = diff_node;
1946
Michal Vaskoe78faec2021-04-08 17:24:43 +02001947 /* for diff purposes, all key-less list descendants actually act as keys (identifying the same instances),
1948 * so there is nothing to merge for these "keys" */
1949 if (!lysc_is_dup_inst_list(src_diff->schema)) {
1950 /* merge src_diff recursively */
1951 LY_LIST_FOR(lyd_child_no_keys(src_diff), child) {
1952 ret = lyd_diff_merge_r(child, diff_parent, diff_cb, cb_data, &child_dup_inst, options, diff);
1953 if (ret) {
1954 break;
1955 }
1956 }
Michal Vaskod7c048c2021-05-18 16:12:55 +02001957 lyd_dup_inst_free(child_dup_inst);
Michal Vaskoe78faec2021-04-08 17:24:43 +02001958 LY_CHECK_RET(ret);
Michal Vaskoe6323f62020-07-09 15:49:02 +02001959 }
1960 } else {
Michal Vasko1dc0a842021-02-04 11:04:57 +01001961add_diff:
Michal Vaskoe6323f62020-07-09 15:49:02 +02001962 /* add new diff node with all descendants */
Michal Vasko9ad76852022-07-12 10:18:03 +02001963 if ((src_diff->flags & LYD_EXT) && diff_parent) {
1964 LY_CHECK_RET(lyd_dup_single_to_ctx(src_diff, LYD_CTX(diff_parent), (struct lyd_node_inner *)diff_parent,
1965 LYD_DUP_RECURSIVE | LYD_DUP_WITH_FLAGS, &diff_node));
1966 } else {
1967 LY_CHECK_RET(lyd_dup_single(src_diff, (struct lyd_node_inner *)diff_parent,
1968 LYD_DUP_RECURSIVE | LYD_DUP_WITH_FLAGS, &diff_node));
1969 }
Michal Vaskoe6323f62020-07-09 15:49:02 +02001970
1971 /* insert node into diff if not already */
1972 if (!diff_parent) {
Michal Vaskob104f112020-07-17 09:54:54 +02001973 lyd_insert_sibling(*diff, diff_node, diff);
Michal Vaskoe6323f62020-07-09 15:49:02 +02001974 }
1975
1976 /* update operation */
1977 LY_CHECK_RET(lyd_diff_change_op(diff_node, src_op));
1978
1979 if (diff_cb) {
Michal Vaskoe2af8412020-12-03 14:11:38 +01001980 /* call callback with no source diff node since it was duplicated and just added */
1981 LY_CHECK_RET(diff_cb(NULL, diff_node, cb_data));
Michal Vaskoe6323f62020-07-09 15:49:02 +02001982 }
1983
1984 /* update diff parent */
1985 diff_parent = diff_node;
1986 }
1987
1988 /* remove any redundant nodes */
Michal Vaskob98d7082020-07-15 16:38:36 +02001989 if (lyd_diff_is_redundant(diff_parent)) {
Michal Vaskoe6323f62020-07-09 15:49:02 +02001990 if (diff_parent == *diff) {
1991 *diff = (*diff)->next;
1992 }
1993 lyd_free_tree(diff_parent);
1994 }
1995
1996 return LY_SUCCESS;
1997}
1998
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001999LIBYANG_API_DEF LY_ERR
Michal Vaskofb737aa2020-08-06 13:53:53 +02002000lyd_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 +01002001 lyd_diff_cb diff_cb, void *cb_data, uint16_t options)
Michal Vaskoe6323f62020-07-09 15:49:02 +02002002{
2003 const struct lyd_node *src_root;
Michal Vasko8efac242023-03-30 08:24:56 +02002004 struct ly_ht *dup_inst = NULL;
Michal Vaskoe78faec2021-04-08 17:24:43 +02002005 LY_ERR ret = LY_SUCCESS;
Michal Vaskoe6323f62020-07-09 15:49:02 +02002006
2007 LY_LIST_FOR(src_diff, src_root) {
2008 if (mod && (lyd_owner_module(src_root) != mod)) {
2009 /* skip data nodes from different modules */
2010 continue;
2011 }
2012
2013 /* apply relevant nodes from the diff datatree */
Michal Vaskoe78faec2021-04-08 17:24:43 +02002014 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 +02002015 }
2016
Michal Vaskoe78faec2021-04-08 17:24:43 +02002017cleanup:
Michal Vaskod7c048c2021-05-18 16:12:55 +02002018 lyd_dup_inst_free(dup_inst);
Michal Vaskoe78faec2021-04-08 17:24:43 +02002019 return ret;
Michal Vaskoe6323f62020-07-09 15:49:02 +02002020}
2021
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002022LIBYANG_API_DEF LY_ERR
Michal Vasko04f85912020-08-07 12:14:58 +02002023lyd_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 +01002024 lyd_diff_cb diff_cb, void *cb_data, uint16_t options)
Michal Vasko04f85912020-08-07 12:14:58 +02002025{
Michal Vaskoe78faec2021-04-08 17:24:43 +02002026 LY_ERR ret;
Michal Vasko8efac242023-03-30 08:24:56 +02002027 struct ly_ht *dup_inst = NULL;
Michal Vaskoe78faec2021-04-08 17:24:43 +02002028
Michal Vasko04f85912020-08-07 12:14:58 +02002029 if (!src_sibling) {
2030 return LY_SUCCESS;
2031 }
2032
Michal Vaskoe78faec2021-04-08 17:24:43 +02002033 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 +02002034 lyd_dup_inst_free(dup_inst);
Michal Vaskoe78faec2021-04-08 17:24:43 +02002035 return ret;
Michal Vasko04f85912020-08-07 12:14:58 +02002036}
2037
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002038LIBYANG_API_DEF LY_ERR
Michal Vaskoc0e58e82020-11-11 19:04:33 +01002039lyd_diff_merge_all(struct lyd_node **diff, const struct lyd_node *src_diff, uint16_t options)
Michal Vaskoe6323f62020-07-09 15:49:02 +02002040{
Michal Vaskoc0e58e82020-11-11 19:04:33 +01002041 return lyd_diff_merge_module(diff, src_diff, NULL, NULL, NULL, options);
Michal Vaskoe6323f62020-07-09 15:49:02 +02002042}
Michal Vasko4231fb62020-07-13 13:54:47 +02002043
2044static LY_ERR
Michal Vaskobaba84e2021-02-05 16:33:30 +01002045lyd_diff_reverse_value(struct lyd_node *node, const struct lys_module *mod)
Michal Vasko4231fb62020-07-13 13:54:47 +02002046{
2047 LY_ERR ret = LY_SUCCESS;
2048 struct lyd_meta *meta;
Michal Vaskoba99a3e2020-08-18 15:50:05 +02002049 const char *val1 = NULL;
2050 char *val2;
Radek Krejci1deb5be2020-08-26 16:43:36 +02002051 uint32_t flags;
Michal Vasko4231fb62020-07-13 13:54:47 +02002052
Michal Vaskobaba84e2021-02-05 16:33:30 +01002053 assert(node->schema->nodetype & (LYS_LEAF | LYS_ANYDATA));
2054
2055 meta = lyd_find_meta(node->meta, mod, "orig-value");
Michal Vasko52afd7d2022-01-18 14:08:34 +01002056 LY_CHECK_ERR_RET(!meta, LOGERR_META(LYD_CTX(node), "orig-value", node), LY_EINVAL);
Michal Vasko4231fb62020-07-13 13:54:47 +02002057
2058 /* orig-value */
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02002059 val1 = lyd_get_meta_value(meta);
Michal Vasko4231fb62020-07-13 13:54:47 +02002060
2061 /* current value */
Michal Vaskobaba84e2021-02-05 16:33:30 +01002062 if (node->schema->nodetype == LYS_LEAF) {
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02002063 val2 = strdup(lyd_get_value(node));
Michal Vaskobaba84e2021-02-05 16:33:30 +01002064 } else {
2065 LY_CHECK_RET(lyd_any_value_str(node, &val2));
2066 }
Michal Vasko4231fb62020-07-13 13:54:47 +02002067
2068 /* switch values, keep default flag */
Michal Vaskobaba84e2021-02-05 16:33:30 +01002069 flags = node->flags;
2070 if (node->schema->nodetype == LYS_LEAF) {
2071 LY_CHECK_GOTO(ret = lyd_change_term(node, val1), cleanup);
2072 } else {
2073 union lyd_any_value anyval = {.str = val1};
Michal Vasko26bbb272022-08-02 14:54:33 +02002074
Michal Vaskobaba84e2021-02-05 16:33:30 +01002075 LY_CHECK_GOTO(ret = lyd_any_copy_value(node, &anyval, LYD_ANYDATA_STRING), cleanup);
2076 }
2077 node->flags = flags;
Michal Vasko4231fb62020-07-13 13:54:47 +02002078 LY_CHECK_GOTO(ret = lyd_change_meta(meta, val2), cleanup);
2079
2080cleanup:
Michal Vaskoba99a3e2020-08-18 15:50:05 +02002081 free(val2);
Michal Vasko4231fb62020-07-13 13:54:47 +02002082 return ret;
2083}
2084
2085static LY_ERR
2086lyd_diff_reverse_default(struct lyd_node *node, const struct lys_module *mod)
2087{
2088 struct lyd_meta *meta;
Radek Krejci1deb5be2020-08-26 16:43:36 +02002089 uint32_t flag1, flag2;
Michal Vasko4231fb62020-07-13 13:54:47 +02002090
2091 meta = lyd_find_meta(node->meta, mod, "orig-default");
Michal Vasko610e93b2020-11-09 20:58:32 +01002092 LY_CHECK_ERR_RET(!meta, LOGINT(mod->ctx), LY_EINT);
Michal Vasko4231fb62020-07-13 13:54:47 +02002093
2094 /* orig-default */
Michal Vaskoba99a3e2020-08-18 15:50:05 +02002095 if (meta->value.boolean) {
Michal Vasko4231fb62020-07-13 13:54:47 +02002096 flag1 = LYD_DEFAULT;
2097 } else {
2098 flag1 = 0;
2099 }
2100
2101 /* current default */
2102 flag2 = node->flags & LYD_DEFAULT;
2103
Michal Vasko610e93b2020-11-09 20:58:32 +01002104 if (flag1 == flag2) {
2105 /* no default state change so nothing to reverse */
2106 return LY_SUCCESS;
2107 }
2108
Michal Vasko4231fb62020-07-13 13:54:47 +02002109 /* switch defaults */
2110 node->flags &= ~LYD_DEFAULT;
2111 node->flags |= flag1;
2112 LY_CHECK_RET(lyd_change_meta(meta, flag2 ? "true" : "false"));
2113
2114 return LY_SUCCESS;
2115}
2116
2117static LY_ERR
2118lyd_diff_reverse_meta(struct lyd_node *node, const struct lys_module *mod, const char *name1, const char *name2)
2119{
2120 LY_ERR ret = LY_SUCCESS;
2121 struct lyd_meta *meta1, *meta2;
Michal Vaskoba99a3e2020-08-18 15:50:05 +02002122 const char *val1 = NULL;
2123 char *val2 = NULL;
Michal Vasko4231fb62020-07-13 13:54:47 +02002124
2125 meta1 = lyd_find_meta(node->meta, mod, name1);
Michal Vasko52afd7d2022-01-18 14:08:34 +01002126 LY_CHECK_ERR_RET(!meta1, LOGERR_META(LYD_CTX(node), name1, node), LY_EINVAL);
Michal Vasko4231fb62020-07-13 13:54:47 +02002127
2128 meta2 = lyd_find_meta(node->meta, mod, name2);
Michal Vasko52afd7d2022-01-18 14:08:34 +01002129 LY_CHECK_ERR_RET(!meta2, LOGERR_META(LYD_CTX(node), name2, node), LY_EINVAL);
Michal Vasko4231fb62020-07-13 13:54:47 +02002130
2131 /* value1 */
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02002132 val1 = lyd_get_meta_value(meta1);
Michal Vasko4231fb62020-07-13 13:54:47 +02002133
2134 /* value2 */
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02002135 val2 = strdup(lyd_get_meta_value(meta2));
Michal Vasko4231fb62020-07-13 13:54:47 +02002136
2137 /* switch values */
2138 LY_CHECK_GOTO(ret = lyd_change_meta(meta1, val2), cleanup);
2139 LY_CHECK_GOTO(ret = lyd_change_meta(meta2, val1), cleanup);
2140
2141cleanup:
Michal Vaskoba99a3e2020-08-18 15:50:05 +02002142 free(val2);
Michal Vasko4231fb62020-07-13 13:54:47 +02002143 return ret;
2144}
2145
Michal Vasko9a7e9d02021-03-09 13:52:25 +01002146/**
2147 * @brief Remove specific operation from all the nodes in a subtree.
2148 *
2149 * @param[in] diff Diff subtree to process.
2150 * @param[in] op Only expected operation.
2151 * @return LY_ERR value.
2152 */
2153static LY_ERR
2154lyd_diff_reverse_remove_op_r(struct lyd_node *diff, enum lyd_diff_op op)
2155{
2156 struct lyd_node *elem;
2157 struct lyd_meta *meta;
2158
2159 LYD_TREE_DFS_BEGIN(diff, elem) {
2160 meta = lyd_find_meta(elem->meta, NULL, "yang:operation");
2161 if (meta) {
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02002162 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 +01002163 lyd_free_meta_single(meta);
2164 }
2165
2166 LYD_TREE_DFS_END(diff, elem);
2167 }
2168
2169 return LY_SUCCESS;
2170}
2171
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01002172LIBYANG_API_DEF LY_ERR
Michal Vasko66535812020-08-11 08:44:22 +02002173lyd_diff_reverse_all(const struct lyd_node *src_diff, struct lyd_node **diff)
Michal Vasko4231fb62020-07-13 13:54:47 +02002174{
2175 LY_ERR ret = LY_SUCCESS;
2176 const struct lys_module *mod;
Michal Vasko9a7e9d02021-03-09 13:52:25 +01002177 struct lyd_node *root, *elem, *iter;
Michal Vasko4231fb62020-07-13 13:54:47 +02002178 enum lyd_diff_op op;
2179
2180 LY_CHECK_ARG_RET(NULL, diff, LY_EINVAL);
2181
2182 if (!src_diff) {
2183 *diff = NULL;
2184 return LY_SUCCESS;
2185 }
2186
2187 /* duplicate diff */
Michal Vasko3a41dff2020-07-15 14:30:28 +02002188 LY_CHECK_RET(lyd_dup_siblings(src_diff, NULL, LYD_DUP_RECURSIVE, diff));
Michal Vasko4231fb62020-07-13 13:54:47 +02002189
2190 /* find module with metadata needed for later */
Michal Vaskob7be7a82020-08-20 09:09:04 +02002191 mod = ly_ctx_get_module_latest(LYD_CTX(src_diff), "yang");
2192 LY_CHECK_ERR_GOTO(!mod, LOGINT(LYD_CTX(src_diff)); ret = LY_EINT, cleanup);
Michal Vasko4231fb62020-07-13 13:54:47 +02002193
2194 LY_LIST_FOR(*diff, root) {
Michal Vasko56daf732020-08-10 10:57:18 +02002195 LYD_TREE_DFS_BEGIN(root, elem) {
Michal Vasko0f5c6a52020-11-06 17:18:03 +01002196 /* skip all keys */
2197 if (!lysc_is_key(elem->schema)) {
2198 /* find operation attribute, if any */
2199 LY_CHECK_GOTO(ret = lyd_diff_get_op(elem, &op), cleanup);
Michal Vasko4231fb62020-07-13 13:54:47 +02002200
Michal Vasko0f5c6a52020-11-06 17:18:03 +01002201 switch (op) {
2202 case LYD_DIFF_OP_CREATE:
2203 /* reverse create to delete */
2204 LY_CHECK_GOTO(ret = lyd_diff_change_op(elem, LYD_DIFF_OP_DELETE), cleanup);
Michal Vasko9e070522021-03-05 14:00:14 +01002205
Michal Vasko9a7e9d02021-03-09 13:52:25 +01002206 /* check all the children for the same operation, nothing else is expected */
2207 LY_LIST_FOR(lyd_child(elem), iter) {
2208 lyd_diff_reverse_remove_op_r(iter, LYD_DIFF_OP_CREATE);
2209 }
2210
Michal Vasko9e070522021-03-05 14:00:14 +01002211 LYD_TREE_DFS_continue = 1;
Michal Vasko4231fb62020-07-13 13:54:47 +02002212 break;
Michal Vasko0f5c6a52020-11-06 17:18:03 +01002213 case LYD_DIFF_OP_DELETE:
2214 /* reverse delete to create */
2215 LY_CHECK_GOTO(ret = lyd_diff_change_op(elem, LYD_DIFF_OP_CREATE), cleanup);
Michal Vasko9e070522021-03-05 14:00:14 +01002216
Michal Vasko9a7e9d02021-03-09 13:52:25 +01002217 /* check all the children for the same operation, nothing else is expected */
2218 LY_LIST_FOR(lyd_child(elem), iter) {
2219 lyd_diff_reverse_remove_op_r(iter, LYD_DIFF_OP_DELETE);
2220 }
2221
Michal Vasko9e070522021-03-05 14:00:14 +01002222 LYD_TREE_DFS_continue = 1;
Michal Vasko4231fb62020-07-13 13:54:47 +02002223 break;
Michal Vasko0f5c6a52020-11-06 17:18:03 +01002224 case LYD_DIFF_OP_REPLACE:
2225 switch (elem->schema->nodetype) {
2226 case LYS_LEAF:
2227 /* leaf value change */
2228 LY_CHECK_GOTO(ret = lyd_diff_reverse_value(elem, mod), cleanup);
2229 LY_CHECK_GOTO(ret = lyd_diff_reverse_default(elem, mod), cleanup);
2230 break;
Michal Vaskobaba84e2021-02-05 16:33:30 +01002231 case LYS_ANYXML:
2232 case LYS_ANYDATA:
2233 /* any value change */
2234 LY_CHECK_GOTO(ret = lyd_diff_reverse_value(elem, mod), cleanup);
2235 break;
Michal Vasko0f5c6a52020-11-06 17:18:03 +01002236 case LYS_LEAFLIST:
2237 /* leaf-list move */
2238 LY_CHECK_GOTO(ret = lyd_diff_reverse_default(elem, mod), cleanup);
Michal Vaskoe78faec2021-04-08 17:24:43 +02002239 if (lysc_is_dup_inst_list(elem->schema)) {
2240 LY_CHECK_GOTO(ret = lyd_diff_reverse_meta(elem, mod, "orig-position", "position"), cleanup);
2241 } else {
2242 LY_CHECK_GOTO(ret = lyd_diff_reverse_meta(elem, mod, "orig-value", "value"), cleanup);
2243 }
Michal Vasko0f5c6a52020-11-06 17:18:03 +01002244 break;
2245 case LYS_LIST:
2246 /* list move */
Michal Vaskoe78faec2021-04-08 17:24:43 +02002247 if (lysc_is_dup_inst_list(elem->schema)) {
2248 LY_CHECK_GOTO(ret = lyd_diff_reverse_meta(elem, mod, "orig-position", "position"), cleanup);
2249 } else {
2250 LY_CHECK_GOTO(ret = lyd_diff_reverse_meta(elem, mod, "orig-key", "key"), cleanup);
2251 }
Michal Vasko0f5c6a52020-11-06 17:18:03 +01002252 break;
2253 default:
2254 LOGINT(LYD_CTX(src_diff));
2255 ret = LY_EINT;
2256 goto cleanup;
2257 }
Michal Vasko4231fb62020-07-13 13:54:47 +02002258 break;
Michal Vasko0f5c6a52020-11-06 17:18:03 +01002259 case LYD_DIFF_OP_NONE:
2260 switch (elem->schema->nodetype) {
2261 case LYS_LEAF:
2262 case LYS_LEAFLIST:
2263 /* default flag change */
2264 LY_CHECK_GOTO(ret = lyd_diff_reverse_default(elem, mod), cleanup);
2265 break;
2266 default:
2267 /* nothing to do */
2268 break;
2269 }
Michal Vasko4231fb62020-07-13 13:54:47 +02002270 break;
2271 }
Michal Vasko4231fb62020-07-13 13:54:47 +02002272 }
2273
Michal Vasko56daf732020-08-10 10:57:18 +02002274 LYD_TREE_DFS_END(root, elem);
Michal Vasko4231fb62020-07-13 13:54:47 +02002275 }
2276 }
2277
2278cleanup:
2279 if (ret) {
2280 lyd_free_siblings(*diff);
2281 *diff = NULL;
2282 }
2283 return ret;
2284}