blob: ee95527f91bc5cd704a29a991fbd02ff7e94bb6f [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 */
Radek Krejcif8dc59a2020-11-25 13:47:44 +010014#define _POSIX_C_SOURCE 200809L /* strdup */
Michal Vaskoe78faec2021-04-08 17:24:43 +020015#define _GNU_SOURCE /* asprintf */
Michal Vaskod59035b2020-07-08 12:00:06 +020016
17#include "diff.h"
18
19#include <assert.h>
20#include <stddef.h>
Michal Vaskoe78faec2021-04-08 17:24:43 +020021#include <stdint.h>
22#include <stdio.h>
Radek Krejci47fab892020-11-05 17:02:41 +010023#include <stdlib.h>
Michal Vaskod59035b2020-07-08 12:00:06 +020024#include <string.h>
25
26#include "common.h"
Michal Vaskoe78faec2021-04-08 17:24:43 +020027#include "compat.h"
Radek Krejci47fab892020-11-05 17:02:41 +010028#include "context.h"
Michal Vaskod59035b2020-07-08 12:00:06 +020029#include "log.h"
Radek Krejci47fab892020-11-05 17:02:41 +010030#include "plugins_types.h"
31#include "set.h"
32#include "tree.h"
33#include "tree_data.h"
Michal Vaskod59035b2020-07-08 12:00:06 +020034#include "tree_data_internal.h"
Radek Krejci859a15a2021-03-05 20:56:59 +010035#include "tree_edit.h"
Michal Vaskod59035b2020-07-08 12:00:06 +020036#include "tree_schema.h"
37#include "tree_schema_internal.h"
38
39static const char *
40lyd_diff_op2str(enum lyd_diff_op op)
41{
42 switch (op) {
43 case LYD_DIFF_OP_CREATE:
44 return "create";
45 case LYD_DIFF_OP_DELETE:
46 return "delete";
47 case LYD_DIFF_OP_REPLACE:
48 return "replace";
49 case LYD_DIFF_OP_NONE:
50 return "none";
51 }
52
53 LOGINT(NULL);
54 return NULL;
55}
56
Michal Vaskoe6323f62020-07-09 15:49:02 +020057static enum lyd_diff_op
58lyd_diff_str2op(const char *str)
59{
60 switch (str[0]) {
61 case 'c':
62 assert(!strcmp(str, "create"));
63 return LYD_DIFF_OP_CREATE;
64 case 'd':
65 assert(!strcmp(str, "delete"));
66 return LYD_DIFF_OP_DELETE;
67 case 'r':
68 assert(!strcmp(str, "replace"));
69 return LYD_DIFF_OP_REPLACE;
70 case 'n':
71 assert(!strcmp(str, "none"));
72 return LYD_DIFF_OP_NONE;
73 }
74
75 LOGINT(NULL);
76 return 0;
77}
78
Michal Vaskod59035b2020-07-08 12:00:06 +020079LY_ERR
80lyd_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 +020081 const char *key, const char *value, const char *position, const char *orig_key, const char *orig_position,
82 struct lyd_node **diff)
Michal Vaskod59035b2020-07-08 12:00:06 +020083{
84 struct lyd_node *dup, *siblings, *match = NULL, *diff_parent = NULL;
85 const struct lyd_node *parent = NULL;
86 const struct lys_module *yang_mod;
87
88 assert(diff);
89
Michal Vasko53d48422020-11-13 18:02:29 +010090 /* replace leaf always needs orig-default and orig-value */
91 assert((node->schema->nodetype != LYS_LEAF) || (op != LYD_DIFF_OP_REPLACE) || (orig_default && orig_value));
92
93 /* create on userord needs key/value */
94 assert((node->schema->nodetype != LYS_LIST) || !(node->schema->flags & LYS_ORDBY_USER) || (op != LYD_DIFF_OP_CREATE) ||
Michal Vaskoe78faec2021-04-08 17:24:43 +020095 (lysc_is_dup_inst_list(node->schema) && position) || key);
Michal Vasko53d48422020-11-13 18:02:29 +010096 assert((node->schema->nodetype != LYS_LEAFLIST) || !(node->schema->flags & LYS_ORDBY_USER) ||
Michal Vaskoe78faec2021-04-08 17:24:43 +020097 (op != LYD_DIFF_OP_CREATE) || (lysc_is_dup_inst_list(node->schema) && position) || value);
Michal Vasko53d48422020-11-13 18:02:29 +010098
99 /* move on userord needs both key and orig-key/value and orig-value */
100 assert((node->schema->nodetype != LYS_LIST) || !(node->schema->flags & LYS_ORDBY_USER) || (op != LYD_DIFF_OP_REPLACE) ||
Michal Vaskoe78faec2021-04-08 17:24:43 +0200101 (lysc_is_dup_inst_list(node->schema) && position && orig_position) || (key && orig_key));
Michal Vasko53d48422020-11-13 18:02:29 +0100102 assert((node->schema->nodetype != LYS_LEAFLIST) || !(node->schema->flags & LYS_ORDBY_USER) ||
Michal Vaskoe78faec2021-04-08 17:24:43 +0200103 (op != LYD_DIFF_OP_REPLACE) || (lysc_is_dup_inst_list(node->schema) && position && orig_position) ||
104 (value && orig_value));
Michal Vasko53d48422020-11-13 18:02:29 +0100105
Michal Vaskod59035b2020-07-08 12:00:06 +0200106 /* find the first existing parent */
107 siblings = *diff;
108 while (1) {
109 /* find next node parent */
110 parent = node;
111 while (parent->parent && (!diff_parent || (parent->parent->schema != diff_parent->schema))) {
Michal Vasko9e685082021-01-29 14:49:09 +0100112 parent = lyd_parent(parent);
Michal Vaskod59035b2020-07-08 12:00:06 +0200113 }
114 if (parent == node) {
115 /* no more parents to find */
116 break;
117 }
118
119 /* check whether it exists in the diff */
120 if (lyd_find_sibling_first(siblings, parent, &match)) {
121 break;
122 }
123
124 /* another parent found */
125 diff_parent = match;
126
127 /* move down in the diff */
Radek Krejcia1c1e542020-09-29 16:06:52 +0200128 siblings = lyd_child_no_keys(match);
Michal Vaskod59035b2020-07-08 12:00:06 +0200129 }
130
131 /* duplicate the subtree (and connect to the diff if possible) */
Michal Vasko3a41dff2020-07-15 14:30:28 +0200132 LY_CHECK_RET(lyd_dup_single(node, (struct lyd_node_inner *)diff_parent,
Michal Vasko871a0252020-11-11 18:35:24 +0100133 LYD_DUP_RECURSIVE | LYD_DUP_NO_META | LYD_DUP_WITH_PARENTS | LYD_DUP_WITH_FLAGS, &dup));
Michal Vaskod59035b2020-07-08 12:00:06 +0200134
135 /* find the first duplicated parent */
136 if (!diff_parent) {
Michal Vasko9e685082021-01-29 14:49:09 +0100137 diff_parent = lyd_parent(dup);
Michal Vaskod59035b2020-07-08 12:00:06 +0200138 while (diff_parent && diff_parent->parent) {
Michal Vasko9e685082021-01-29 14:49:09 +0100139 diff_parent = lyd_parent(diff_parent);
Michal Vaskod59035b2020-07-08 12:00:06 +0200140 }
141 } else {
Michal Vasko9e685082021-01-29 14:49:09 +0100142 diff_parent = dup;
Michal Vaskod59035b2020-07-08 12:00:06 +0200143 while (diff_parent->parent && (diff_parent->parent->schema == parent->schema)) {
Michal Vasko9e685082021-01-29 14:49:09 +0100144 diff_parent = lyd_parent(diff_parent);
Michal Vaskod59035b2020-07-08 12:00:06 +0200145 }
146 }
147
148 /* no parent existed, must be manually connected */
149 if (!diff_parent) {
150 /* there actually was no parent to duplicate */
Michal Vaskob104f112020-07-17 09:54:54 +0200151 lyd_insert_sibling(*diff, dup, diff);
Michal Vaskod59035b2020-07-08 12:00:06 +0200152 } else if (!diff_parent->parent) {
Michal Vaskob104f112020-07-17 09:54:54 +0200153 lyd_insert_sibling(*diff, diff_parent, diff);
Michal Vaskod59035b2020-07-08 12:00:06 +0200154 }
155
156 /* get module with the operation metadata */
Michal Vaskob7be7a82020-08-20 09:09:04 +0200157 yang_mod = LYD_CTX(node)->list.objs[1];
Michal Vaskod59035b2020-07-08 12:00:06 +0200158 assert(!strcmp(yang_mod->name, "yang"));
159
160 /* add parent operation, if any */
Michal Vasko3a41dff2020-07-15 14:30:28 +0200161 if (diff_parent && (diff_parent != dup)) {
Michal Vasko871a0252020-11-11 18:35:24 +0100162 LY_CHECK_RET(lyd_new_meta(LYD_CTX(node), diff_parent, yang_mod, "operation", "none", 0, NULL));
Michal Vaskod59035b2020-07-08 12:00:06 +0200163 }
164
165 /* add subtree operation */
Michal Vasko871a0252020-11-11 18:35:24 +0100166 LY_CHECK_RET(lyd_new_meta(LYD_CTX(node), dup, yang_mod, "operation", lyd_diff_op2str(op), 0, NULL));
Michal Vaskod59035b2020-07-08 12:00:06 +0200167
168 /* orig-default */
Michal Vasko3a41dff2020-07-15 14:30:28 +0200169 if (orig_default) {
Michal Vasko871a0252020-11-11 18:35:24 +0100170 LY_CHECK_RET(lyd_new_meta(LYD_CTX(node), dup, yang_mod, "orig-default", orig_default, 0, NULL));
Michal Vaskod59035b2020-07-08 12:00:06 +0200171 }
172
173 /* orig-value */
Michal Vasko3a41dff2020-07-15 14:30:28 +0200174 if (orig_value) {
Michal Vasko871a0252020-11-11 18:35:24 +0100175 LY_CHECK_RET(lyd_new_meta(LYD_CTX(node), dup, yang_mod, "orig-value", orig_value, 0, NULL));
Michal Vaskod59035b2020-07-08 12:00:06 +0200176 }
177
178 /* key */
Michal Vasko3a41dff2020-07-15 14:30:28 +0200179 if (key) {
Michal Vasko871a0252020-11-11 18:35:24 +0100180 LY_CHECK_RET(lyd_new_meta(LYD_CTX(node), dup, yang_mod, "key", key, 0, NULL));
Michal Vaskod59035b2020-07-08 12:00:06 +0200181 }
182
183 /* value */
Michal Vasko3a41dff2020-07-15 14:30:28 +0200184 if (value) {
Michal Vasko871a0252020-11-11 18:35:24 +0100185 LY_CHECK_RET(lyd_new_meta(LYD_CTX(node), dup, yang_mod, "value", value, 0, NULL));
Michal Vaskod59035b2020-07-08 12:00:06 +0200186 }
187
Michal Vaskoe78faec2021-04-08 17:24:43 +0200188 /* position */
189 if (position) {
190 LY_CHECK_RET(lyd_new_meta(LYD_CTX(node), dup, yang_mod, "position", position, 0, NULL));
191 }
192
Michal Vaskod59035b2020-07-08 12:00:06 +0200193 /* orig-key */
Michal Vasko3a41dff2020-07-15 14:30:28 +0200194 if (orig_key) {
Michal Vasko871a0252020-11-11 18:35:24 +0100195 LY_CHECK_RET(lyd_new_meta(LYD_CTX(node), dup, yang_mod, "orig-key", orig_key, 0, NULL));
Michal Vaskod59035b2020-07-08 12:00:06 +0200196 }
197
Michal Vaskoe78faec2021-04-08 17:24:43 +0200198 /* orig-position */
199 if (orig_position) {
200 LY_CHECK_RET(lyd_new_meta(LYD_CTX(node), dup, yang_mod, "orig-position", orig_position, 0, NULL));
201 }
202
Michal Vaskod59035b2020-07-08 12:00:06 +0200203 return LY_SUCCESS;
204}
205
206/**
207 * @brief Get a userord entry for a specific user-ordered list/leaf-list. Create if does not exist yet.
208 *
Michal Vasko1dcd73b2020-12-08 10:04:33 +0100209 * @param[in] first Node from the first tree, can be NULL (on create).
Michal Vaskod59035b2020-07-08 12:00:06 +0200210 * @param[in] schema Schema node of the list/leaf-list.
211 * @param[in,out] userord Sized array of userord items.
212 * @return Userord item for all the user-ordered list/leaf-list instances.
213 */
214static struct lyd_diff_userord *
215lyd_diff_userord_get(const struct lyd_node *first, const struct lysc_node *schema, struct lyd_diff_userord **userord)
216{
217 struct lyd_diff_userord *item;
Michal Vaskoe78faec2021-04-08 17:24:43 +0200218 struct lyd_node *iter;
219 const struct lyd_node **node;
Michal Vaskod59035b2020-07-08 12:00:06 +0200220 LY_ARRAY_COUNT_TYPE u;
221
222 LY_ARRAY_FOR(*userord, u) {
223 if ((*userord)[u].schema == schema) {
224 return &(*userord)[u];
225 }
226 }
227
228 /* it was not added yet, add it now */
229 LY_ARRAY_NEW_RET(schema->module->ctx, *userord, item, NULL);
230
231 item->schema = schema;
232 item->pos = 0;
233 item->inst = NULL;
234
235 /* store all the instance pointers in the current order */
236 if (first) {
Michal Vaskoe78faec2021-04-08 17:24:43 +0200237 LYD_LIST_FOR_INST(lyd_first_sibling(first), first->schema, iter) {
238 LY_ARRAY_NEW_RET(schema->module->ctx, item->inst, node, NULL);
239 *node = iter;
Michal Vaskod59035b2020-07-08 12:00:06 +0200240 }
241 }
242
243 return item;
244}
245
246/**
247 * @brief Get all the metadata to be stored in a diff for the 2 nodes. Can be used only for user-ordered
248 * lists/leaf-lists.
249 *
250 * @param[in] first Node from the first tree, can be NULL (on create).
251 * @param[in] second Node from the second tree, can be NULL (on delete).
252 * @param[in] options Diff options.
253 * @param[in,out] userord Sized array of userord items for keeping the current node order.
254 * @param[out] op Operation.
255 * @param[out] orig_default Original default metadata.
256 * @param[out] value Value metadata.
257 * @param[out] orig_value Original value metadata
258 * @param[out] key Key metadata.
259 * @param[out] orig_key Original key metadata.
Michal Vaskoe78faec2021-04-08 17:24:43 +0200260 * @param[out] position Position metadata.
261 * @param[out] orig_position Original position metadata.
Michal Vaskod59035b2020-07-08 12:00:06 +0200262 * @return LY_SUCCESS on success,
263 * @return LY_ENOT if there is no change to be added into diff,
264 * @return LY_ERR value on other errors.
265 */
266static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200267lyd_diff_userord_attrs(const struct lyd_node *first, const struct lyd_node *second, uint16_t options,
Radek Krejci0f969882020-08-21 16:56:47 +0200268 struct lyd_diff_userord **userord, enum lyd_diff_op *op, const char **orig_default, char **value,
Michal Vaskoe78faec2021-04-08 17:24:43 +0200269 char **orig_value, char **key, char **orig_key, char **position, char **orig_position)
Michal Vaskod59035b2020-07-08 12:00:06 +0200270{
271 const struct lysc_node *schema;
Michal Vaskoe78faec2021-04-08 17:24:43 +0200272 size_t buflen, bufused;
273 uint32_t first_pos, second_pos;
Michal Vaskod59035b2020-07-08 12:00:06 +0200274 struct lyd_diff_userord *userord_item;
275
276 assert(first || second);
277
278 *orig_default = NULL;
279 *value = NULL;
280 *orig_value = NULL;
281 *key = NULL;
282 *orig_key = NULL;
Michal Vaskoe78faec2021-04-08 17:24:43 +0200283 *position = NULL;
284 *orig_position = NULL;
Michal Vaskod59035b2020-07-08 12:00:06 +0200285
286 schema = first ? first->schema : second->schema;
287 assert(lysc_is_userordered(schema));
288
289 /* get userord entry */
290 userord_item = lyd_diff_userord_get(first, schema, userord);
291 LY_CHECK_RET(!userord_item, LY_EMEM);
292
Michal Vaskod59035b2020-07-08 12:00:06 +0200293 /* find user-ordered first position */
294 if (first) {
Michal Vaskoe78faec2021-04-08 17:24:43 +0200295 for (first_pos = 0; first_pos < LY_ARRAY_COUNT(userord_item->inst); ++first_pos) {
Michal Vaskod59035b2020-07-08 12:00:06 +0200296 if (userord_item->inst[first_pos] == first) {
297 break;
298 }
299 }
300 assert(first_pos < LY_ARRAY_COUNT(userord_item->inst));
301 } else {
302 first_pos = 0;
303 }
304
Michal Vaskoe78faec2021-04-08 17:24:43 +0200305 /* prepare position of the next instance */
306 second_pos = userord_item->pos++;
307
Michal Vaskod59035b2020-07-08 12:00:06 +0200308 /* learn operation first */
309 if (!second) {
310 *op = LYD_DIFF_OP_DELETE;
311 } else if (!first) {
312 *op = LYD_DIFF_OP_CREATE;
313 } else {
Michal Vasko8f359bf2020-07-28 10:41:15 +0200314 if (lyd_compare_single(second, userord_item->inst[second_pos], 0)) {
Michal Vaskod59035b2020-07-08 12:00:06 +0200315 /* in first, there is a different instance on the second position, we are going to move 'first' node */
316 *op = LYD_DIFF_OP_REPLACE;
Michal Vasko3a41dff2020-07-15 14:30:28 +0200317 } else if ((options & LYD_DIFF_DEFAULTS) && ((first->flags & LYD_DEFAULT) != (second->flags & LYD_DEFAULT))) {
Michal Vaskod59035b2020-07-08 12:00:06 +0200318 /* default flag change */
319 *op = LYD_DIFF_OP_NONE;
320 } else {
321 /* no changes */
322 return LY_ENOT;
323 }
324 }
325
326 /*
327 * set each attribute correctly based on the operation and node type
328 */
329
330 /* orig-default */
Michal Vasko4b715ca2020-11-11 18:39:57 +0100331 if ((schema->nodetype == LYS_LEAFLIST) && ((*op == LYD_DIFF_OP_REPLACE) || (*op == LYD_DIFF_OP_NONE))) {
Michal Vaskod59035b2020-07-08 12:00:06 +0200332 if (first->flags & LYD_DEFAULT) {
333 *orig_default = "true";
334 } else {
335 *orig_default = "false";
336 }
337 }
338
339 /* value */
Michal Vaskoe78faec2021-04-08 17:24:43 +0200340 if ((schema->nodetype == LYS_LEAFLIST) && !lysc_is_dup_inst_list(schema) &&
341 ((*op == LYD_DIFF_OP_REPLACE) || (*op == LYD_DIFF_OP_CREATE))) {
Michal Vaskod59035b2020-07-08 12:00:06 +0200342 if (second_pos) {
Michal Vaskob7be7a82020-08-20 09:09:04 +0200343 *value = strdup(LYD_CANON_VALUE(userord_item->inst[second_pos - 1]));
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200344 LY_CHECK_ERR_RET(!*value, LOGMEM(schema->module->ctx), LY_EMEM);
Michal Vaskod59035b2020-07-08 12:00:06 +0200345 } else {
346 *value = strdup("");
347 LY_CHECK_ERR_RET(!*value, LOGMEM(schema->module->ctx), LY_EMEM);
348 }
349 }
350
351 /* orig-value */
Michal Vaskoe78faec2021-04-08 17:24:43 +0200352 if ((schema->nodetype == LYS_LEAFLIST) && !lysc_is_dup_inst_list(schema) &&
353 ((*op == LYD_DIFF_OP_REPLACE) || (*op == LYD_DIFF_OP_DELETE))) {
Michal Vaskod59035b2020-07-08 12:00:06 +0200354 if (first_pos) {
Michal Vaskob7be7a82020-08-20 09:09:04 +0200355 *orig_value = strdup(LYD_CANON_VALUE(userord_item->inst[first_pos - 1]));
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200356 LY_CHECK_ERR_RET(!*orig_value, LOGMEM(schema->module->ctx), LY_EMEM);
Michal Vaskod59035b2020-07-08 12:00:06 +0200357 } else {
358 *orig_value = strdup("");
359 LY_CHECK_ERR_RET(!*orig_value, LOGMEM(schema->module->ctx), LY_EMEM);
360 }
361 }
362
363 /* key */
Michal Vaskoe78faec2021-04-08 17:24:43 +0200364 if ((schema->nodetype == LYS_LIST) && !lysc_is_dup_inst_list(schema) &&
365 ((*op == LYD_DIFF_OP_REPLACE) || (*op == LYD_DIFF_OP_CREATE))) {
Michal Vaskod59035b2020-07-08 12:00:06 +0200366 if (second_pos) {
367 buflen = bufused = 0;
368 LY_CHECK_RET(lyd_path_list_predicate(userord_item->inst[second_pos - 1], key, &buflen, &bufused, 0));
369 } else {
370 *key = strdup("");
371 LY_CHECK_ERR_RET(!*key, LOGMEM(schema->module->ctx), LY_EMEM);
372 }
373 }
374
375 /* orig-key */
Michal Vaskoe78faec2021-04-08 17:24:43 +0200376 if ((schema->nodetype == LYS_LIST) && !lysc_is_dup_inst_list(schema) &&
377 ((*op == LYD_DIFF_OP_REPLACE) || (*op == LYD_DIFF_OP_DELETE))) {
Michal Vaskod59035b2020-07-08 12:00:06 +0200378 if (first_pos) {
379 buflen = bufused = 0;
380 LY_CHECK_RET(lyd_path_list_predicate(userord_item->inst[first_pos - 1], orig_key, &buflen, &bufused, 0));
381 } else {
382 *orig_key = strdup("");
383 LY_CHECK_ERR_RET(!*orig_key, LOGMEM(schema->module->ctx), LY_EMEM);
384 }
385 }
386
Michal Vaskoe78faec2021-04-08 17:24:43 +0200387 /* position */
388 if (lysc_is_dup_inst_list(schema) && ((*op == LYD_DIFF_OP_REPLACE) || (*op == LYD_DIFF_OP_CREATE))) {
389 if (second_pos) {
390 if (asprintf(position, "%" PRIu32, second_pos) == -1) {
391 LOGMEM(schema->module->ctx);
392 return LY_EMEM;
393 }
394 } else {
395 *position = strdup("");
396 LY_CHECK_ERR_RET(!*position, LOGMEM(schema->module->ctx), LY_EMEM);
397 }
398 }
399
400 /* orig-position */
401 if (lysc_is_dup_inst_list(schema) && ((*op == LYD_DIFF_OP_REPLACE) || (*op == LYD_DIFF_OP_DELETE))) {
402 if (first_pos) {
403 if (asprintf(orig_position, "%" PRIu32, first_pos) == -1) {
404 LOGMEM(schema->module->ctx);
405 return LY_EMEM;
406 }
407 } else {
408 *orig_position = strdup("");
409 LY_CHECK_ERR_RET(!*orig_position, LOGMEM(schema->module->ctx), LY_EMEM);
410 }
411 }
412
Michal Vaskod59035b2020-07-08 12:00:06 +0200413 /*
414 * update our instances - apply the change
415 */
416 if (*op == LYD_DIFF_OP_CREATE) {
417 /* insert the instance */
Michal Vasko5cde11b2020-12-08 10:04:48 +0100418 LY_ARRAY_CREATE_RET(schema->module->ctx, userord_item->inst, 1, LY_EMEM);
Michal Vaskod59035b2020-07-08 12:00:06 +0200419 if (second_pos < LY_ARRAY_COUNT(userord_item->inst)) {
420 memmove(userord_item->inst + second_pos + 1, userord_item->inst + second_pos,
421 (LY_ARRAY_COUNT(userord_item->inst) - second_pos) * sizeof *userord_item->inst);
422 }
423 LY_ARRAY_INCREMENT(userord_item->inst);
424 userord_item->inst[second_pos] = second;
425
426 } else if (*op == LYD_DIFF_OP_DELETE) {
427 /* remove the instance */
428 if (first_pos + 1 < LY_ARRAY_COUNT(userord_item->inst)) {
429 memmove(userord_item->inst + first_pos, userord_item->inst + first_pos + 1,
430 (LY_ARRAY_COUNT(userord_item->inst) - first_pos - 1) * sizeof *userord_item->inst);
431 }
432 LY_ARRAY_DECREMENT(userord_item->inst);
433
434 } else if (*op == LYD_DIFF_OP_REPLACE) {
435 /* move the instances */
436 memmove(userord_item->inst + second_pos + 1, userord_item->inst + second_pos,
437 (first_pos - second_pos) * sizeof *userord_item->inst);
438 userord_item->inst[second_pos] = first;
439 }
440
441 return LY_SUCCESS;
442}
443
444/**
445 * @brief Get all the metadata to be stored in a diff for the 2 nodes. Cannot be used for user-ordered
446 * lists/leaf-lists.
447 *
448 * @param[in] first Node from the first tree, can be NULL (on create).
449 * @param[in] second Node from the second tree, can be NULL (on delete).
450 * @param[in] options Diff options.
451 * @param[out] op Operation.
452 * @param[out] orig_default Original default metadata.
453 * @param[out] orig_value Original value metadata.
454 * @return LY_SUCCESS on success,
455 * @return LY_ENOT if there is no change to be added into diff,
456 * @return LY_ERR value on other errors.
457 */
458static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200459lyd_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 +0200460 const char **orig_default, char **orig_value)
Michal Vaskod59035b2020-07-08 12:00:06 +0200461{
462 const struct lysc_node *schema;
Michal Vaskod59035b2020-07-08 12:00:06 +0200463
464 assert(first || second);
465
466 *orig_default = NULL;
467 *orig_value = NULL;
468
469 schema = first ? first->schema : second->schema;
470 assert(!lysc_is_userordered(schema));
471
472 /* learn operation first */
473 if (!second) {
474 *op = LYD_DIFF_OP_DELETE;
475 } else if (!first) {
476 *op = LYD_DIFF_OP_CREATE;
477 } else {
478 switch (schema->nodetype) {
479 case LYS_CONTAINER:
480 case LYS_RPC:
481 case LYS_ACTION:
482 case LYS_NOTIF:
483 /* no changes */
484 return LY_ENOT;
485 case LYS_LIST:
486 case LYS_LEAFLIST:
Michal Vasko3a41dff2020-07-15 14:30:28 +0200487 if ((options & LYD_DIFF_DEFAULTS) && ((first->flags & LYD_DEFAULT) != (second->flags & LYD_DEFAULT))) {
Michal Vaskod59035b2020-07-08 12:00:06 +0200488 /* default flag change */
489 *op = LYD_DIFF_OP_NONE;
490 } else {
491 /* no changes */
492 return LY_ENOT;
493 }
494 break;
495 case LYS_LEAF:
496 case LYS_ANYXML:
497 case LYS_ANYDATA:
Michal Vasko8f359bf2020-07-28 10:41:15 +0200498 if (lyd_compare_single(first, second, 0)) {
Michal Vaskod59035b2020-07-08 12:00:06 +0200499 /* different values */
500 *op = LYD_DIFF_OP_REPLACE;
Michal Vasko3a41dff2020-07-15 14:30:28 +0200501 } else if ((options & LYD_DIFF_DEFAULTS) && ((first->flags & LYD_DEFAULT) != (second->flags & LYD_DEFAULT))) {
Michal Vaskod59035b2020-07-08 12:00:06 +0200502 /* default flag change */
503 *op = LYD_DIFF_OP_NONE;
504 } else {
505 /* no changes */
506 return LY_ENOT;
507 }
508 break;
509 default:
510 LOGINT_RET(schema->module->ctx);
511 }
512 }
513
514 /*
515 * set each attribute correctly based on the operation and node type
516 */
517
518 /* orig-default */
Michal Vasko4b715ca2020-11-11 18:39:57 +0100519 if ((schema->nodetype & LYD_NODE_TERM) && ((*op == LYD_DIFF_OP_REPLACE) || (*op == LYD_DIFF_OP_NONE))) {
Michal Vaskod59035b2020-07-08 12:00:06 +0200520 if (first->flags & LYD_DEFAULT) {
521 *orig_default = "true";
522 } else {
523 *orig_default = "false";
524 }
525 }
526
527 /* orig-value */
Michal Vaskobaba84e2021-02-05 16:33:30 +0100528 if ((schema->nodetype & (LYS_LEAF | LYS_ANYDATA)) && (*op == LYD_DIFF_OP_REPLACE)) {
529 if (schema->nodetype == LYS_LEAF) {
530 *orig_value = strdup(LYD_CANON_VALUE(first));
531 LY_CHECK_ERR_RET(!*orig_value, LOGMEM(schema->module->ctx), LY_EMEM);
532 } else {
533 LY_CHECK_RET(lyd_any_value_str(first, orig_value));
534 }
Michal Vaskod59035b2020-07-08 12:00:06 +0200535 }
536
537 return LY_SUCCESS;
538}
539
540/**
Michal Vaskoe78faec2021-04-08 17:24:43 +0200541 * @brief Find an entry in duplicate instance cache for an instance. Create it if it does not exist.
542 *
543 * @param[in] first_inst Instance of the cache entry.
544 * @param[in,out] dup_inst_cache Duplicate instance cache.
545 * @return Instance cache entry.
546 */
547static struct lyd_diff_dup_inst *
548lyd_diff_dup_inst_get(const struct lyd_node *first_inst, struct lyd_diff_dup_inst **dup_inst_cache)
549{
550 struct lyd_diff_dup_inst *item;
551 LY_ARRAY_COUNT_TYPE u;
552
553 LY_ARRAY_FOR(*dup_inst_cache, u) {
554 if ((*dup_inst_cache)[u].inst_set->dnodes[0] == first_inst) {
555 return &(*dup_inst_cache)[u];
556 }
557 }
558
559 /* it was not added yet, add it now */
560 LY_ARRAY_NEW_RET(LYD_CTX(first_inst), *dup_inst_cache, item, NULL);
561
562 return item;
563}
564
565/**
566 * @brief Free duplicate instance cache.
567 *
568 * @param[in] dup_inst Duplicate instance cache to free.
569 */
570static void
571lyd_diff_dup_inst_free(struct lyd_diff_dup_inst *dup_inst)
572{
573 LY_ARRAY_COUNT_TYPE u;
574
575 LY_ARRAY_FOR(dup_inst, u) {
576 ly_set_free(dup_inst[u].inst_set, NULL);
577 }
578 LY_ARRAY_FREE(dup_inst);
579}
580
581/**
582 * @brief Find a matching instance of a node in a data tree.
583 *
584 * @param[in] siblings Siblings to search in.
585 * @param[in] target Target node to search for.
586 * @param[in] defaults Whether to consider (or ignore) default values.
587 * @param[in,out] dup_inst_cache Duplicate instance cache.
588 * @param[out] match Found match, NULL if no matching node found.
589 * @return LY_ERR value.
590 */
591static LY_ERR
592lyd_diff_find_match(const struct lyd_node *siblings, const struct lyd_node *target, ly_bool defaults,
593 struct lyd_diff_dup_inst **dup_inst_cache, struct lyd_node **match)
594{
595 struct lyd_diff_dup_inst *dup_inst;
596
597 if (target->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) {
598 /* try to find the exact instance */
599 lyd_find_sibling_first(siblings, target, match);
600 } else {
601 /* try to simply find the node, there cannot be more instances */
602 lyd_find_sibling_val(siblings, target->schema, NULL, 0, match);
603 }
604
605 if (*match && lysc_is_dup_inst_list(target->schema)) {
606 /* there can be more exact same instances and we must make sure we do not match a single node more times */
607 dup_inst = lyd_diff_dup_inst_get(*match, dup_inst_cache);
608 LY_CHECK_ERR_RET(!dup_inst, LOGMEM(LYD_CTX(target)), LY_EMEM);
609
610 if (!dup_inst->used) {
611 /* we did not cache these instances yet, do so */
612 lyd_find_sibling_dup_inst_set(siblings, target, &dup_inst->inst_set);
613 assert(dup_inst->inst_set->count && (dup_inst->inst_set->dnodes[0] == *match));
614 }
615
616 if (dup_inst->used == dup_inst->inst_set->count) {
617 /* we have used all the instances */
618 *match = NULL;
619 } else {
620 assert(dup_inst->used < dup_inst->inst_set->count);
621
622 /* use another instance */
623 *match = dup_inst->inst_set->dnodes[dup_inst->used];
624 ++dup_inst->used;
625 }
626 }
627
628 if (*match && ((*match)->flags & LYD_DEFAULT) && !defaults) {
629 /* ignore default nodes */
630 *match = NULL;
631 }
632 return LY_SUCCESS;
633}
634
635/**
Michal Vaskod59035b2020-07-08 12:00:06 +0200636 * @brief Perform diff for all siblings at certain depth, recursively.
637 *
638 * For user-ordered lists/leaf-lists a specific structure is used for storing
639 * the current order. The idea is to apply all the generated diff changes
640 * virtually on the first tree so that we can continue to generate correct
641 * changes after some were already generated.
642 *
643 * The algorithm then uses second tree position-based changes with a before
644 * (preceding) item anchor.
645 *
646 * Example:
647 *
648 * Virtual first tree leaf-list order:
649 * 1 2 [3] 4 5
650 *
651 * Second tree leaf-list order:
652 * 1 2 [5] 3 4
653 *
654 * We are at the 3rd node now. We look at whether the nodes on the 3rd position
655 * match - they do not - move nodes so that the 3rd position node is final ->
656 * -> move node 5 to the 3rd position -> move node 5 after node 2.
657 *
658 * Required properties:
659 * Stored operations (move) should not be affected by later operations -
660 * - would cause a redundantly long list of operations, possibly inifinite.
661 *
662 * Implemenation justification:
663 * First, all delete operations and only then move/create operations are stored.
664 * Also, preceding anchor is used and after each iteration another node is
665 * at its final position. That results in the invariant that all preceding
666 * nodes are final and will not be changed by the later operations, meaning
667 * they can safely be used as anchors for the later operations.
668 *
669 * @param[in] first First tree first sibling.
670 * @param[in] second Second tree first sibling.
671 * @param[in] options Diff options.
Michal Vasko3a41dff2020-07-15 14:30:28 +0200672 * @param[in] nosiblings Whether to skip following siblings.
Michal Vaskod59035b2020-07-08 12:00:06 +0200673 * @param[in,out] diff Diff to append to.
674 * @return LY_ERR value.
675 */
676static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +0200677lyd_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 +0200678 struct lyd_node **diff)
Michal Vaskod59035b2020-07-08 12:00:06 +0200679{
680 LY_ERR ret = LY_SUCCESS;
681 const struct lyd_node *iter_first, *iter_second;
682 struct lyd_node *match_second, *match_first;
Michal Vaskod59035b2020-07-08 12:00:06 +0200683 struct lyd_diff_userord *userord = NULL;
Michal Vaskoe78faec2021-04-08 17:24:43 +0200684 struct lyd_diff_dup_inst *dup_inst_first = NULL, *dup_inst_second = NULL;
Michal Vaskod59035b2020-07-08 12:00:06 +0200685 LY_ARRAY_COUNT_TYPE u;
686 enum lyd_diff_op op;
687 const char *orig_default;
Michal Vaskoe78faec2021-04-08 17:24:43 +0200688 char *orig_value, *key, *value, *position, *orig_key, *orig_position;
Michal Vaskod59035b2020-07-08 12:00:06 +0200689
Michal Vaskod59035b2020-07-08 12:00:06 +0200690 /* compare first tree to the second tree - delete, replace, none */
691 LY_LIST_FOR(first, iter_first) {
692 assert(!(iter_first->schema->flags & LYS_KEY));
Michal Vasko3a41dff2020-07-15 14:30:28 +0200693 if ((iter_first->flags & LYD_DEFAULT) && !(options & LYD_DIFF_DEFAULTS)) {
Michal Vaskod59035b2020-07-08 12:00:06 +0200694 /* skip default nodes */
695 continue;
696 }
697
Michal Vaskoe78faec2021-04-08 17:24:43 +0200698 /* find a match in the second tree */
699 LY_CHECK_GOTO(ret = lyd_diff_find_match(second, iter_first, options & LYD_DIFF_DEFAULTS, &dup_inst_second,
700 &match_second), cleanup);
Michal Vaskod59035b2020-07-08 12:00:06 +0200701
702 if (lysc_is_userordered(iter_first->schema)) {
Michal Vaskoe78faec2021-04-08 17:24:43 +0200703 /* we are handling only user-ordered node delete now */
704 if (!match_second) {
705 /* get all the attributes */
706 LY_CHECK_GOTO(ret = lyd_diff_userord_attrs(iter_first, match_second, options, &userord, &op, &orig_default,
707 &value, &orig_value, &key, &orig_key, &position, &orig_position), cleanup);
708
709 /* there must be changes, it is deleted */
710 assert(op == LYD_DIFF_OP_DELETE);
711 ret = lyd_diff_add(iter_first, op, orig_default, orig_value, key, value, position, orig_key, orig_position, diff);
712
713 free(orig_value);
714 free(key);
715 free(value);
716 free(position);
717 free(orig_key);
718 free(orig_position);
719 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskod59035b2020-07-08 12:00:06 +0200720 }
Michal Vaskod59035b2020-07-08 12:00:06 +0200721 } else {
722 /* get all the attributes */
723 ret = lyd_diff_attrs(iter_first, match_second, options, &op, &orig_default, &orig_value);
724
725 /* add into diff if there are any changes */
726 if (!ret) {
727 if (op == LYD_DIFF_OP_DELETE) {
Michal Vaskoe78faec2021-04-08 17:24:43 +0200728 ret = lyd_diff_add(iter_first, op, orig_default, orig_value, NULL, NULL, NULL, NULL, NULL, diff);
Michal Vaskod59035b2020-07-08 12:00:06 +0200729 } else {
Michal Vasko3c2dd6c2020-11-06 17:38:55 +0100730 assert(match_second);
Michal Vaskoe78faec2021-04-08 17:24:43 +0200731 ret = lyd_diff_add(match_second, op, orig_default, orig_value, NULL, NULL, NULL, NULL, NULL, diff);
Michal Vaskod59035b2020-07-08 12:00:06 +0200732 }
733
734 free(orig_value);
735 LY_CHECK_GOTO(ret, cleanup);
736 } else if (ret == LY_ENOT) {
737 ret = LY_SUCCESS;
738 } else {
739 goto cleanup;
740 }
741 }
742
743 /* check descendants, if any, recursively */
744 if (match_second) {
Michal Vaskoe78faec2021-04-08 17:24:43 +0200745 LY_CHECK_GOTO(ret = lyd_diff_siblings_r(lyd_child_no_keys(iter_first), lyd_child_no_keys(match_second),
746 options, 0, diff), cleanup);
Michal Vaskod59035b2020-07-08 12:00:06 +0200747 }
748
749 if (nosiblings) {
750 break;
751 }
752 }
753
754 /* reset all cached positions */
755 LY_ARRAY_FOR(userord, u) {
756 userord[u].pos = 0;
757 }
758
759 /* compare second tree to the first tree - create, user-ordered move */
760 LY_LIST_FOR(second, iter_second) {
761 assert(!(iter_second->schema->flags & LYS_KEY));
Michal Vasko3a41dff2020-07-15 14:30:28 +0200762 if ((iter_second->flags & LYD_DEFAULT) && !(options & LYD_DIFF_DEFAULTS)) {
Michal Vaskod59035b2020-07-08 12:00:06 +0200763 /* skip default nodes */
764 continue;
765 }
766
Michal Vaskoe78faec2021-04-08 17:24:43 +0200767 /* find a match in the first tree */
768 LY_CHECK_GOTO(ret = lyd_diff_find_match(first, iter_second, options & LYD_DIFF_DEFAULTS, &dup_inst_first,
769 &match_first), cleanup);
Michal Vaskod59035b2020-07-08 12:00:06 +0200770
771 if (lysc_is_userordered(iter_second->schema)) {
772 /* get all the attributes */
773 ret = lyd_diff_userord_attrs(match_first, iter_second, options, &userord, &op, &orig_default,
Michal Vaskoe78faec2021-04-08 17:24:43 +0200774 &value, &orig_value, &key, &orig_key, &position, &orig_position);
Michal Vaskod59035b2020-07-08 12:00:06 +0200775
776 /* add into diff if there are any changes */
777 if (!ret) {
Michal Vaskoe78faec2021-04-08 17:24:43 +0200778 ret = lyd_diff_add(iter_second, op, orig_default, orig_value, key, value, position, orig_key,
779 orig_position, diff);
Michal Vaskod59035b2020-07-08 12:00:06 +0200780
781 free(orig_value);
782 free(key);
783 free(value);
Michal Vaskoe78faec2021-04-08 17:24:43 +0200784 free(position);
Michal Vaskod59035b2020-07-08 12:00:06 +0200785 free(orig_key);
Michal Vaskoe78faec2021-04-08 17:24:43 +0200786 free(orig_position);
Michal Vaskod59035b2020-07-08 12:00:06 +0200787 LY_CHECK_GOTO(ret, cleanup);
788 } else if (ret == LY_ENOT) {
789 ret = LY_SUCCESS;
790 } else {
791 goto cleanup;
792 }
793 } else if (!match_first) {
794 /* get all the attributes */
Michal Vaskoe78faec2021-04-08 17:24:43 +0200795 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 +0200796
797 /* there must be changes, it is created */
798 assert(op == LYD_DIFF_OP_CREATE);
Michal Vaskoe78faec2021-04-08 17:24:43 +0200799 ret = lyd_diff_add(iter_second, op, orig_default, orig_value, NULL, NULL, NULL, NULL, NULL, diff);
Michal Vaskod59035b2020-07-08 12:00:06 +0200800
801 free(orig_value);
802 LY_CHECK_GOTO(ret, cleanup);
803 } /* else was handled */
804
805 if (nosiblings) {
806 break;
807 }
808 }
809
810cleanup:
Michal Vaskoe78faec2021-04-08 17:24:43 +0200811 lyd_diff_dup_inst_free(dup_inst_first);
812 lyd_diff_dup_inst_free(dup_inst_second);
Michal Vaskod59035b2020-07-08 12:00:06 +0200813 LY_ARRAY_FOR(userord, u) {
814 LY_ARRAY_FREE(userord[u].inst);
815 }
816 LY_ARRAY_FREE(userord);
817 return ret;
818}
819
Michal Vasko3a41dff2020-07-15 14:30:28 +0200820static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +0200821lyd_diff(const struct lyd_node *first, const struct lyd_node *second, uint16_t options, ly_bool nosiblings, struct lyd_node **diff)
Michal Vaskod59035b2020-07-08 12:00:06 +0200822{
823 const struct ly_ctx *ctx;
824
825 LY_CHECK_ARG_RET(NULL, diff, LY_EINVAL);
826
827 if (first) {
Michal Vaskob7be7a82020-08-20 09:09:04 +0200828 ctx = LYD_CTX(first);
Michal Vaskod59035b2020-07-08 12:00:06 +0200829 } else if (second) {
Michal Vaskob7be7a82020-08-20 09:09:04 +0200830 ctx = LYD_CTX(second);
Michal Vaskod59035b2020-07-08 12:00:06 +0200831 } else {
832 ctx = NULL;
833 }
834
835 if (first && second && (lysc_data_parent(first->schema) != lysc_data_parent(second->schema))) {
836 LOGERR(ctx, LY_EINVAL, "Invalid arguments - cannot create diff for unrelated data (%s()).", __func__);
837 return LY_EINVAL;
838 }
839
840 *diff = NULL;
841
Michal Vasko3a41dff2020-07-15 14:30:28 +0200842 return lyd_diff_siblings_r(first, second, options, nosiblings, diff);
843}
844
845API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200846lyd_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 +0200847{
848 return lyd_diff(first, second, options, 1, diff);
849}
850
851API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200852lyd_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 +0200853{
854 return lyd_diff(first, second, options, 0, diff);
Michal Vaskod59035b2020-07-08 12:00:06 +0200855}
856
857/**
Michal Vaskod59035b2020-07-08 12:00:06 +0200858 * @brief Learn operation of a diff node.
859 *
860 * @param[in] diff_node Diff node.
861 * @param[out] op Operation.
Michal Vaskod59035b2020-07-08 12:00:06 +0200862 * @return LY_ERR value.
863 */
864static LY_ERR
Michal Vaskoe6323f62020-07-09 15:49:02 +0200865lyd_diff_get_op(const struct lyd_node *diff_node, enum lyd_diff_op *op)
Michal Vaskod59035b2020-07-08 12:00:06 +0200866{
867 struct lyd_meta *meta = NULL;
868 const struct lyd_node *diff_parent;
Michal Vaskoe6323f62020-07-09 15:49:02 +0200869 const char *str;
Michal Vaskod59035b2020-07-08 12:00:06 +0200870
Michal Vasko9e685082021-01-29 14:49:09 +0100871 for (diff_parent = diff_node; diff_parent; diff_parent = lyd_parent(diff_parent)) {
Michal Vaskod59035b2020-07-08 12:00:06 +0200872 LY_LIST_FOR(diff_parent->meta, meta) {
873 if (!strcmp(meta->name, "operation") && !strcmp(meta->annotation->module->name, "yang")) {
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200874 str = meta->value.canonical;
Michal Vaskod59035b2020-07-08 12:00:06 +0200875 if ((str[0] == 'r') && (diff_parent != diff_node)) {
876 /* we do not care about this operation if it's in our parent */
877 continue;
878 }
Michal Vaskoe6323f62020-07-09 15:49:02 +0200879 *op = lyd_diff_str2op(str);
Michal Vaskod59035b2020-07-08 12:00:06 +0200880 break;
881 }
882 }
883 if (meta) {
884 break;
885 }
886 }
Michal Vaskob7be7a82020-08-20 09:09:04 +0200887 LY_CHECK_ERR_RET(!meta, LOGINT(LYD_CTX(diff_node)), LY_EINT);
Michal Vaskod59035b2020-07-08 12:00:06 +0200888
Michal Vaskod59035b2020-07-08 12:00:06 +0200889 return LY_SUCCESS;
890}
891
892/**
893 * @brief Insert a diff node into a data tree.
894 *
895 * @param[in,out] first_node First sibling of the data tree.
896 * @param[in] parent_node Data tree sibling parent node.
897 * @param[in] new_node Node to insert.
Michal Vaskoe78faec2021-04-08 17:24:43 +0200898 * @param[in] userord_anchor Optional anchor (key, value, or position) of relative (leaf-)list instance. If not set,
899 * the user-ordered instance will be inserted at the first position.
Michal Vaskod59035b2020-07-08 12:00:06 +0200900 * @return err_info, NULL on success.
901 */
902static LY_ERR
903lyd_diff_insert(struct lyd_node **first_node, struct lyd_node *parent_node, struct lyd_node *new_node,
Michal Vaskoe78faec2021-04-08 17:24:43 +0200904 const char *userord_anchor)
Michal Vaskod59035b2020-07-08 12:00:06 +0200905{
906 LY_ERR ret;
907 struct lyd_node *anchor;
Michal Vaskoe78faec2021-04-08 17:24:43 +0200908 uint32_t pos, anchor_pos;
909 int found;
Michal Vaskod59035b2020-07-08 12:00:06 +0200910
911 assert(new_node);
912
913 if (!*first_node) {
914 if (!parent_node) {
915 /* no parent or siblings */
916 *first_node = new_node;
917 return LY_SUCCESS;
918 }
919
920 /* simply insert into parent, no other children */
Michal Vaskoe78faec2021-04-08 17:24:43 +0200921 if (userord_anchor) {
Michal Vaskob7be7a82020-08-20 09:09:04 +0200922 LOGERR(LYD_CTX(new_node), LY_EINVAL, "Node \"%s\" instance to insert next to not found.",
Michal Vasko69730152020-10-09 16:30:07 +0200923 new_node->schema->name);
Michal Vaskod59035b2020-07-08 12:00:06 +0200924 return LY_EINVAL;
925 }
Michal Vaskob104f112020-07-17 09:54:54 +0200926 return lyd_insert_child(parent_node, new_node);
Michal Vaskod59035b2020-07-08 12:00:06 +0200927 }
928
Michal Vasko9e685082021-01-29 14:49:09 +0100929 assert(!(*first_node)->parent || (lyd_parent(*first_node) == parent_node));
Michal Vaskod59035b2020-07-08 12:00:06 +0200930
Michal Vaskod59035b2020-07-08 12:00:06 +0200931 if (!lysc_is_userordered(new_node->schema)) {
Michal Vaskob104f112020-07-17 09:54:54 +0200932 /* simple insert */
933 return lyd_insert_sibling(*first_node, new_node, first_node);
Michal Vaskod59035b2020-07-08 12:00:06 +0200934 }
935
Michal Vaskoe78faec2021-04-08 17:24:43 +0200936 if (userord_anchor) {
Michal Vaskod59035b2020-07-08 12:00:06 +0200937 /* find the anchor sibling */
Michal Vaskoe78faec2021-04-08 17:24:43 +0200938 if (lysc_is_dup_inst_list(new_node->schema)) {
939 anchor_pos = atoi(userord_anchor);
940 LY_CHECK_ERR_RET(!anchor_pos, LOGINT(LYD_CTX(new_node)), LY_EINT);
941
942 found = 0;
943 pos = 1;
944 LYD_LIST_FOR_INST(*first_node, new_node->schema, anchor) {
945 if (pos == anchor_pos) {
946 found = 1;
947 break;
948 }
949 ++pos;
950 }
951 if (!found) {
952 LOGERR(LYD_CTX(new_node), LY_EINVAL, "Node \"%s\" instance to insert next to not found.",
953 new_node->schema->name);
954 return LY_EINVAL;
955 }
956 } else {
957 ret = lyd_find_sibling_val(*first_node, new_node->schema, userord_anchor, 0, &anchor);
958 if (ret == LY_ENOTFOUND) {
959 LOGERR(LYD_CTX(new_node), LY_EINVAL, "Node \"%s\" instance to insert next to not found.",
960 new_node->schema->name);
961 return LY_EINVAL;
962 } else if (ret) {
963 return ret;
964 }
Michal Vaskod59035b2020-07-08 12:00:06 +0200965 }
966
967 /* insert after */
968 LY_CHECK_RET(lyd_insert_after(anchor, new_node));
969 assert(new_node->prev == anchor);
970 if (*first_node == new_node) {
971 *first_node = anchor;
972 }
973 } else {
974 if ((*first_node)->schema->flags & LYS_KEY) {
975 assert(parent_node && (parent_node->schema->nodetype == LYS_LIST));
976
977 /* find last key */
978 anchor = *first_node;
979 while (anchor->next && (anchor->next->schema->flags & LYS_KEY)) {
980 anchor = anchor->next;
981 }
982 /* insert after the last key */
983 LY_CHECK_RET(lyd_insert_after(anchor, new_node));
984 } else {
985 /* insert at the beginning */
986 LY_CHECK_RET(lyd_insert_before(*first_node, new_node));
987 *first_node = new_node;
988 }
989 }
990
991 return LY_SUCCESS;
992}
993
994/**
995 * @brief Apply diff subtree on data tree nodes, recursively.
996 *
997 * @param[in,out] first_node First sibling of the data tree.
998 * @param[in] parent_node Parent of the first sibling.
999 * @param[in] diff_node Current diff node.
Michal Vaskoe6323f62020-07-09 15:49:02 +02001000 * @param[in] diff_cb Optional diff callback.
1001 * @param[in] cb_data User data for @p diff_cb.
Michal Vaskoe78faec2021-04-08 17:24:43 +02001002 * @param[in,out] dup_inst Duplicate instance cache for all @p diff_node siblings.
Michal Vaskod59035b2020-07-08 12:00:06 +02001003 * @return LY_ERR value.
1004 */
1005static LY_ERR
1006lyd_diff_apply_r(struct lyd_node **first_node, struct lyd_node *parent_node, const struct lyd_node *diff_node,
Michal Vaskoe78faec2021-04-08 17:24:43 +02001007 lyd_diff_cb diff_cb, void *cb_data, struct lyd_diff_dup_inst **dup_inst)
Michal Vaskod59035b2020-07-08 12:00:06 +02001008{
1009 LY_ERR ret;
1010 struct lyd_node *match, *diff_child;
Michal Vaskoe78faec2021-04-08 17:24:43 +02001011 const char *str_val, *meta_str;
Michal Vaskoe6323f62020-07-09 15:49:02 +02001012 enum lyd_diff_op op;
1013 struct lyd_meta *meta;
Michal Vaskoe78faec2021-04-08 17:24:43 +02001014 struct lyd_diff_dup_inst *child_dup_inst = NULL;
Michal Vaskob7be7a82020-08-20 09:09:04 +02001015 const struct ly_ctx *ctx = LYD_CTX(diff_node);
Michal Vaskod59035b2020-07-08 12:00:06 +02001016
1017 /* read all the valid attributes */
Michal Vaskoe6323f62020-07-09 15:49:02 +02001018 LY_CHECK_RET(lyd_diff_get_op(diff_node, &op));
Michal Vaskod59035b2020-07-08 12:00:06 +02001019
Michal Vaskoe6323f62020-07-09 15:49:02 +02001020 /* handle specific user-ordered (leaf-)lists operations separately */
1021 if (lysc_is_userordered(diff_node->schema) && ((op == LYD_DIFF_OP_CREATE) || (op == LYD_DIFF_OP_REPLACE))) {
1022 if (op == LYD_DIFF_OP_REPLACE) {
Michal Vaskod59035b2020-07-08 12:00:06 +02001023 /* find the node (we must have some siblings because the node was only moved) */
Michal Vaskoe78faec2021-04-08 17:24:43 +02001024 LY_CHECK_RET(lyd_diff_find_match(*first_node, diff_node, 1, dup_inst, &match));
1025 LY_CHECK_ERR_RET(!match, LOGINT(ctx), LY_EINT);
Michal Vaskod59035b2020-07-08 12:00:06 +02001026 } else {
Michal Vasko3a41dff2020-07-15 14:30:28 +02001027 /* duplicate the node */
1028 LY_CHECK_RET(lyd_dup_single(diff_node, NULL, LYD_DUP_NO_META, &match));
Michal Vaskod59035b2020-07-08 12:00:06 +02001029 }
1030
Michal Vaskoe78faec2021-04-08 17:24:43 +02001031 /* get "key", "value", or "position" metadata string value */
1032 if (lysc_is_dup_inst_list(diff_node->schema)) {
1033 meta_str = "yang:position";
1034 } else if (diff_node->schema->nodetype == LYS_LIST) {
1035 meta_str = "yang:key";
1036 } else {
1037 meta_str = "yang:value";
1038 }
1039 meta = lyd_find_meta(diff_node->meta, NULL, meta_str);
1040 LY_CHECK_ERR_RET(!meta, LOGINT(ctx), LY_EINT);
Michal Vaskoba99a3e2020-08-18 15:50:05 +02001041 str_val = meta->value.canonical;
Michal Vaskoe6323f62020-07-09 15:49:02 +02001042
Michal Vaskod59035b2020-07-08 12:00:06 +02001043 /* insert/move the node */
Michal Vaskoe6323f62020-07-09 15:49:02 +02001044 if (str_val[0]) {
1045 ret = lyd_diff_insert(first_node, parent_node, match, str_val);
Michal Vaskod59035b2020-07-08 12:00:06 +02001046 } else {
1047 ret = lyd_diff_insert(first_node, parent_node, match, NULL);
1048 }
1049 if (ret) {
Michal Vaskoe6323f62020-07-09 15:49:02 +02001050 if (op == LYD_DIFF_OP_CREATE) {
Michal Vaskod59035b2020-07-08 12:00:06 +02001051 lyd_free_tree(match);
1052 }
1053 return ret;
1054 }
1055
1056 goto next_iter_r;
1057 }
1058
1059 /* apply operation */
Michal Vaskoe6323f62020-07-09 15:49:02 +02001060 switch (op) {
1061 case LYD_DIFF_OP_NONE:
Michal Vaskod59035b2020-07-08 12:00:06 +02001062 /* find the node */
Michal Vaskoe78faec2021-04-08 17:24:43 +02001063 LY_CHECK_RET(lyd_diff_find_match(*first_node, diff_node, 1, dup_inst, &match));
1064 LY_CHECK_ERR_RET(!match, LOGINT(ctx), LY_EINT);
Michal Vaskod59035b2020-07-08 12:00:06 +02001065
1066 if (match->schema->nodetype & LYD_NODE_TERM) {
1067 /* special case of only dflt flag change */
1068 if (diff_node->flags & LYD_DEFAULT) {
1069 match->flags |= LYD_DEFAULT;
1070 } else {
1071 match->flags &= ~LYD_DEFAULT;
1072 }
1073 } else {
1074 /* none operation on nodes without children is redundant and hence forbidden */
Radek Krejcia1c1e542020-09-29 16:06:52 +02001075 if (!lyd_child_no_keys(diff_node)) {
Michal Vaskod59035b2020-07-08 12:00:06 +02001076 LOGINT_RET(ctx);
1077 }
1078 }
1079 break;
Michal Vaskoe6323f62020-07-09 15:49:02 +02001080 case LYD_DIFF_OP_CREATE:
Michal Vaskod59035b2020-07-08 12:00:06 +02001081 /* duplicate the node */
Michal Vasko3a41dff2020-07-15 14:30:28 +02001082 LY_CHECK_RET(lyd_dup_single(diff_node, NULL, LYD_DUP_NO_META, &match));
Michal Vaskod59035b2020-07-08 12:00:06 +02001083
1084 /* insert it at the end */
1085 ret = 0;
Michal Vaskob104f112020-07-17 09:54:54 +02001086 if (parent_node) {
1087 ret = lyd_insert_child(parent_node, match);
Michal Vaskod59035b2020-07-08 12:00:06 +02001088 } else {
Michal Vaskob104f112020-07-17 09:54:54 +02001089 ret = lyd_insert_sibling(*first_node, match, first_node);
Michal Vaskod59035b2020-07-08 12:00:06 +02001090 }
1091 if (ret) {
1092 lyd_free_tree(match);
1093 return ret;
1094 }
1095
1096 break;
Michal Vaskoe6323f62020-07-09 15:49:02 +02001097 case LYD_DIFF_OP_DELETE:
Michal Vaskod59035b2020-07-08 12:00:06 +02001098 /* find the node */
Michal Vaskoe78faec2021-04-08 17:24:43 +02001099 LY_CHECK_RET(lyd_diff_find_match(*first_node, diff_node, 1, dup_inst, &match));
1100 LY_CHECK_ERR_RET(!match, LOGINT(ctx), LY_EINT);
Michal Vaskod59035b2020-07-08 12:00:06 +02001101
1102 /* remove it */
1103 if ((match == *first_node) && !match->parent) {
1104 assert(!parent_node);
1105 /* we have removed the top-level node */
1106 *first_node = (*first_node)->next;
1107 }
1108 lyd_free_tree(match);
1109
1110 /* we are not going recursively in this case, the whole subtree was already deleted */
1111 return LY_SUCCESS;
Michal Vaskoe6323f62020-07-09 15:49:02 +02001112 case LYD_DIFF_OP_REPLACE:
Michal Vaskobaba84e2021-02-05 16:33:30 +01001113 LY_CHECK_ERR_RET(!(diff_node->schema->nodetype & (LYS_LEAF | LYS_ANYDATA)), LOGINT(ctx), LY_EINT);
Michal Vaskod59035b2020-07-08 12:00:06 +02001114
1115 /* find the node */
Michal Vaskoe78faec2021-04-08 17:24:43 +02001116 LY_CHECK_RET(lyd_diff_find_match(*first_node, diff_node, 1, dup_inst, &match));
1117 LY_CHECK_ERR_RET(!match, LOGINT(ctx), LY_EINT);
Michal Vaskod59035b2020-07-08 12:00:06 +02001118
Michal Vaskobaba84e2021-02-05 16:33:30 +01001119 /* update the value */
1120 if (diff_node->schema->nodetype == LYS_LEAF) {
1121 ret = lyd_change_term(match, LYD_CANON_VALUE(diff_node));
1122 if (ret && (ret != LY_EEXIST)) {
1123 LOGINT_RET(ctx);
1124 }
1125 } else {
1126 struct lyd_node_any *any = (struct lyd_node_any *)diff_node;
Michal Vaskoe78faec2021-04-08 17:24:43 +02001127 LY_CHECK_RET(lyd_any_copy_value(match, &any->value, any->value_type));
Michal Vaskod59035b2020-07-08 12:00:06 +02001128 }
1129
1130 /* with flags */
1131 match->flags = diff_node->flags;
1132 break;
1133 default:
1134 LOGINT_RET(ctx);
1135 }
1136
1137next_iter_r:
1138 if (diff_cb) {
1139 /* call callback */
1140 LY_CHECK_RET(diff_cb(diff_node, match, cb_data));
1141 }
1142
1143 /* apply diff recursively */
Michal Vaskoe78faec2021-04-08 17:24:43 +02001144 ret = LY_SUCCESS;
Radek Krejcia1c1e542020-09-29 16:06:52 +02001145 LY_LIST_FOR(lyd_child_no_keys(diff_node), diff_child) {
Michal Vaskoe78faec2021-04-08 17:24:43 +02001146 ret = lyd_diff_apply_r(lyd_node_child_p(match), match, diff_child, diff_cb, cb_data, &child_dup_inst);
1147 if (ret) {
1148 break;
1149 }
Michal Vaskod59035b2020-07-08 12:00:06 +02001150 }
1151
Michal Vaskoe78faec2021-04-08 17:24:43 +02001152 lyd_diff_dup_inst_free(child_dup_inst);
1153 return ret;
Michal Vaskod59035b2020-07-08 12:00:06 +02001154}
1155
1156API LY_ERR
1157lyd_diff_apply_module(struct lyd_node **data, const struct lyd_node *diff, const struct lys_module *mod,
Radek Krejci0f969882020-08-21 16:56:47 +02001158 lyd_diff_cb diff_cb, void *cb_data)
Michal Vaskod59035b2020-07-08 12:00:06 +02001159{
1160 const struct lyd_node *root;
Michal Vaskoe78faec2021-04-08 17:24:43 +02001161 struct lyd_diff_dup_inst *dup_inst = NULL;
1162 LY_ERR ret = LY_SUCCESS;
Michal Vaskod59035b2020-07-08 12:00:06 +02001163
1164 LY_LIST_FOR(diff, root) {
1165 if (mod && (lyd_owner_module(root) != mod)) {
1166 /* skip data nodes from different modules */
1167 continue;
1168 }
1169
1170 /* apply relevant nodes from the diff datatree */
Michal Vaskoe78faec2021-04-08 17:24:43 +02001171 ret = lyd_diff_apply_r(data, NULL, root, diff_cb, cb_data, &dup_inst);
1172 if (ret) {
1173 break;
1174 }
Michal Vaskod59035b2020-07-08 12:00:06 +02001175 }
1176
Michal Vaskoe78faec2021-04-08 17:24:43 +02001177 lyd_diff_dup_inst_free(dup_inst);
1178 return ret;
Michal Vaskod59035b2020-07-08 12:00:06 +02001179}
1180
1181API LY_ERR
Michal Vasko3a41dff2020-07-15 14:30:28 +02001182lyd_diff_apply_all(struct lyd_node **data, const struct lyd_node *diff)
Michal Vaskod59035b2020-07-08 12:00:06 +02001183{
1184 return lyd_diff_apply_module(data, diff, NULL, NULL, NULL);
1185}
Michal Vaskoe6323f62020-07-09 15:49:02 +02001186
1187/**
1188 * @brief Update operations on a diff node when the new operation is NONE.
1189 *
1190 * @param[in] diff_match Node from the diff.
Michal Vaskoe78faec2021-04-08 17:24:43 +02001191 * @param[in] cur_op Current operation of @p diff_match.
Michal Vaskoe6323f62020-07-09 15:49:02 +02001192 * @param[in] src_diff Current source diff node.
1193 * @return LY_ERR value.
1194 */
1195static LY_ERR
1196lyd_diff_merge_none(struct lyd_node *diff_match, enum lyd_diff_op cur_op, const struct lyd_node *src_diff)
1197{
1198 switch (cur_op) {
1199 case LYD_DIFF_OP_NONE:
1200 case LYD_DIFF_OP_CREATE:
1201 case LYD_DIFF_OP_REPLACE:
1202 if (src_diff->schema->nodetype & LYD_NODE_TERM) {
1203 /* NONE on a term means only its dflt flag was changed */
1204 diff_match->flags &= ~LYD_DEFAULT;
1205 diff_match->flags |= src_diff->flags & LYD_DEFAULT;
1206 }
1207 break;
1208 default:
1209 /* delete operation is not valid */
Michal Vaskob7be7a82020-08-20 09:09:04 +02001210 LOGINT_RET(LYD_CTX(src_diff));
Michal Vaskoe6323f62020-07-09 15:49:02 +02001211 }
1212
1213 return LY_SUCCESS;
1214}
1215
1216/**
1217 * @brief Remove an attribute from a node.
1218 *
1219 * @param[in] node Node with the metadata.
1220 * @param[in] name Metadata name.
1221 */
1222static void
1223lyd_diff_del_meta(struct lyd_node *node, const char *name)
1224{
1225 struct lyd_meta *meta;
1226
1227 LY_LIST_FOR(node->meta, meta) {
1228 if (!strcmp(meta->name, name) && !strcmp(meta->annotation->module->name, "yang")) {
Michal Vasko3a41dff2020-07-15 14:30:28 +02001229 lyd_free_meta_single(meta);
Michal Vaskoe6323f62020-07-09 15:49:02 +02001230 return;
1231 }
1232 }
1233
1234 assert(0);
1235}
1236
1237/**
1238 * @brief Set a specific operation of a node. Delete the previous operation, if any.
Michal Vasko871a0252020-11-11 18:35:24 +01001239 * Does not change the default flag.
Michal Vaskoe6323f62020-07-09 15:49:02 +02001240 *
1241 * @param[in] node Node to change.
1242 * @param[in] op Operation to set.
1243 * @return LY_ERR value.
1244 */
1245static LY_ERR
1246lyd_diff_change_op(struct lyd_node *node, enum lyd_diff_op op)
1247{
1248 struct lyd_meta *meta;
1249
1250 LY_LIST_FOR(node->meta, meta) {
1251 if (!strcmp(meta->name, "operation") && !strcmp(meta->annotation->module->name, "yang")) {
Michal Vasko3a41dff2020-07-15 14:30:28 +02001252 lyd_free_meta_single(meta);
Michal Vaskoe6323f62020-07-09 15:49:02 +02001253 break;
1254 }
1255 }
1256
Michal Vasko871a0252020-11-11 18:35:24 +01001257 return lyd_new_meta(LYD_CTX(node), node, NULL, "yang:operation", lyd_diff_op2str(op), 0, NULL);
Michal Vaskoe6323f62020-07-09 15:49:02 +02001258}
1259
1260/**
1261 * @brief Update operations on a diff node when the new operation is REPLACE.
1262 *
1263 * @param[in] diff_match Node from the diff.
Michal Vaskoe78faec2021-04-08 17:24:43 +02001264 * @param[in] cur_op Current operation of @p diff_match.
Michal Vaskoe6323f62020-07-09 15:49:02 +02001265 * @param[in] src_diff Current source diff node.
1266 * @return LY_ERR value.
1267 */
1268static LY_ERR
1269lyd_diff_merge_replace(struct lyd_node *diff_match, enum lyd_diff_op cur_op, const struct lyd_node *src_diff)
1270{
1271 LY_ERR ret;
Michal Vaskoe78faec2021-04-08 17:24:43 +02001272 const char *str_val, *meta_name, *orig_meta_name;
Michal Vaskoe6323f62020-07-09 15:49:02 +02001273 struct lyd_meta *meta;
1274 const struct lys_module *mod;
1275 const struct lyd_node_any *any;
1276
1277 /* get "yang" module for the metadata */
Michal Vaskob7be7a82020-08-20 09:09:04 +02001278 mod = ly_ctx_get_module_latest(LYD_CTX(diff_match), "yang");
Michal Vaskoe6323f62020-07-09 15:49:02 +02001279 assert(mod);
1280
1281 switch (cur_op) {
1282 case LYD_DIFF_OP_REPLACE:
1283 case LYD_DIFF_OP_CREATE:
1284 switch (diff_match->schema->nodetype) {
1285 case LYS_LIST:
1286 case LYS_LEAFLIST:
Michal Vasko4231fb62020-07-13 13:54:47 +02001287 /* it was created/moved somewhere, but now it will be created/moved somewhere else,
Michal Vaskoe6323f62020-07-09 15:49:02 +02001288 * keep orig_key/orig_value (only replace oper) and replace key/value */
1289 assert(lysc_is_userordered(diff_match->schema));
Michal Vaskoe78faec2021-04-08 17:24:43 +02001290 if (lysc_is_dup_inst_list(diff_match->schema)) {
1291 meta_name = "position";
1292 } else if (diff_match->schema->nodetype == LYS_LIST) {
1293 meta_name = "key";
1294 } else {
1295 meta_name = "value";
1296 }
Michal Vaskoe6323f62020-07-09 15:49:02 +02001297
1298 lyd_diff_del_meta(diff_match, meta_name);
1299 meta = lyd_find_meta(src_diff->meta, mod, meta_name);
Michal Vaskob7be7a82020-08-20 09:09:04 +02001300 LY_CHECK_ERR_RET(!meta, LOGINT(LYD_CTX(src_diff)), LY_EINT);
Michal Vasko3a41dff2020-07-15 14:30:28 +02001301 LY_CHECK_RET(lyd_dup_meta_single(meta, diff_match, NULL));
Michal Vaskoe6323f62020-07-09 15:49:02 +02001302 break;
1303 case LYS_LEAF:
1304 /* replaced with the exact same value, impossible */
Michal Vasko8f359bf2020-07-28 10:41:15 +02001305 if (!lyd_compare_single(diff_match, src_diff, 0)) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02001306 LOGINT_RET(LYD_CTX(src_diff));
Michal Vaskoe6323f62020-07-09 15:49:02 +02001307 }
1308
Michal Vaskoe6323f62020-07-09 15:49:02 +02001309 /* modify the node value */
Michal Vaskob7be7a82020-08-20 09:09:04 +02001310 if (lyd_change_term(diff_match, LYD_CANON_VALUE(src_diff))) {
1311 LOGINT_RET(LYD_CTX(src_diff));
Michal Vaskoe6323f62020-07-09 15:49:02 +02001312 }
1313
Michal Vasko8caadab2020-11-05 17:38:15 +01001314 if (cur_op == LYD_DIFF_OP_REPLACE) {
1315 /* compare values whether there is any change at all */
1316 meta = lyd_find_meta(diff_match->meta, mod, "orig-value");
1317 LY_CHECK_ERR_RET(!meta, LOGINT(LYD_CTX(diff_match)), LY_EINT);
1318 str_val = meta->value.canonical;
1319 ret = lyd_value_compare((struct lyd_node_term *)diff_match, str_val, strlen(str_val));
1320 if (!ret) {
1321 /* values are the same, remove orig-value meta and set oper to NONE */
1322 lyd_free_meta_single(meta);
1323 LY_CHECK_RET(lyd_diff_change_op(diff_match, LYD_DIFF_OP_NONE));
1324 }
Michal Vaskoe6323f62020-07-09 15:49:02 +02001325 }
1326
1327 /* modify the default flag */
1328 diff_match->flags &= ~LYD_DEFAULT;
1329 diff_match->flags |= src_diff->flags & LYD_DEFAULT;
1330 break;
1331 case LYS_ANYXML:
1332 case LYS_ANYDATA:
Michal Vasko8f359bf2020-07-28 10:41:15 +02001333 if (!lyd_compare_single(diff_match, src_diff, 0)) {
Michal Vaskoe6323f62020-07-09 15:49:02 +02001334 /* replaced with the exact same value, impossible */
Michal Vaskob7be7a82020-08-20 09:09:04 +02001335 LOGINT_RET(LYD_CTX(src_diff));
Michal Vaskoe6323f62020-07-09 15:49:02 +02001336 }
1337
1338 /* modify the node value */
1339 any = (struct lyd_node_any *)src_diff;
1340 LY_CHECK_RET(lyd_any_copy_value(diff_match, &any->value, any->value_type));
1341 break;
1342 default:
Michal Vaskob7be7a82020-08-20 09:09:04 +02001343 LOGINT_RET(LYD_CTX(src_diff));
Michal Vaskoe6323f62020-07-09 15:49:02 +02001344 }
1345 break;
1346 case LYD_DIFF_OP_NONE:
1347 /* it is moved now */
1348 assert(lysc_is_userordered(diff_match->schema) && (diff_match->schema->nodetype == LYS_LIST));
1349
1350 /* change the operation */
1351 LY_CHECK_RET(lyd_diff_change_op(diff_match, LYD_DIFF_OP_REPLACE));
1352
Michal Vaskoe78faec2021-04-08 17:24:43 +02001353 /* set orig-meta and meta */
1354 if (lysc_is_dup_inst_list(diff_match->schema)) {
1355 meta_name = "position";
1356 orig_meta_name = "orig-position";
1357 } else {
1358 meta_name = "key";
1359 orig_meta_name = "orig-key";
1360 }
1361
1362 meta = lyd_find_meta(src_diff->meta, mod, orig_meta_name);
Michal Vaskob7be7a82020-08-20 09:09:04 +02001363 LY_CHECK_ERR_RET(!meta, LOGINT(LYD_CTX(src_diff)), LY_EINT);
Michal Vasko3a41dff2020-07-15 14:30:28 +02001364 LY_CHECK_RET(lyd_dup_meta_single(meta, diff_match, NULL));
Michal Vaskoe6323f62020-07-09 15:49:02 +02001365
Michal Vaskoe78faec2021-04-08 17:24:43 +02001366 meta = lyd_find_meta(src_diff->meta, mod, meta_name);
Michal Vaskob7be7a82020-08-20 09:09:04 +02001367 LY_CHECK_ERR_RET(!meta, LOGINT(LYD_CTX(src_diff)), LY_EINT);
Michal Vasko3a41dff2020-07-15 14:30:28 +02001368 LY_CHECK_RET(lyd_dup_meta_single(meta, diff_match, NULL));
Michal Vaskoe6323f62020-07-09 15:49:02 +02001369 break;
1370 default:
1371 /* delete operation is not valid */
Michal Vaskob7be7a82020-08-20 09:09:04 +02001372 LOGINT_RET(LYD_CTX(src_diff));
Michal Vaskoe6323f62020-07-09 15:49:02 +02001373 }
1374
1375 return LY_SUCCESS;
1376}
1377
1378/**
1379 * @brief Update operations in a diff node when the new operation is CREATE.
1380 *
1381 * @param[in] diff_match Node from the diff.
Michal Vaskoe78faec2021-04-08 17:24:43 +02001382 * @param[in] cur_op Current operation of @p diff_match.
Michal Vaskoe6323f62020-07-09 15:49:02 +02001383 * @param[in] src_diff Current source diff node.
Michal Vaskoc0e58e82020-11-11 19:04:33 +01001384 * @param[in] options Diff merge options.
Michal Vaskoe6323f62020-07-09 15:49:02 +02001385 * @return LY_ERR value.
1386 */
1387static LY_ERR
Michal Vaskoc0e58e82020-11-11 19:04:33 +01001388lyd_diff_merge_create(struct lyd_node *diff_match, enum lyd_diff_op cur_op, const struct lyd_node *src_diff, uint16_t options)
Michal Vaskoe6323f62020-07-09 15:49:02 +02001389{
1390 struct lyd_node *child;
Michal Vaskoc0e58e82020-11-11 19:04:33 +01001391 const struct lysc_node_leaf *sleaf = NULL;
Michal Vasko871a0252020-11-11 18:35:24 +01001392 uint32_t trg_flags;
Michal Vaskoe78faec2021-04-08 17:24:43 +02001393 const char *meta_name, *orig_meta_name;
1394 struct lyd_meta *meta, *orig_meta;
Michal Vaskoe6323f62020-07-09 15:49:02 +02001395
1396 switch (cur_op) {
1397 case LYD_DIFF_OP_DELETE:
Michal Vasko871a0252020-11-11 18:35:24 +01001398 /* remember current flags */
1399 trg_flags = diff_match->flags;
1400
Michal Vaskoe78faec2021-04-08 17:24:43 +02001401 if (lysc_is_userordered(diff_match->schema)) {
1402 /* get anchor metadata */
1403 if (lysc_is_dup_inst_list(diff_match->schema)) {
1404 meta_name = "yang:position";
1405 orig_meta_name = "yang:orig-position";
1406 } else if (diff_match->schema->nodetype == LYS_LIST) {
1407 meta_name = "yang:key";
1408 orig_meta_name = "yang:orig-key";
1409 } else {
1410 meta_name = "yang:value";
1411 orig_meta_name = "yang:orig-value";
1412 }
1413 meta = lyd_find_meta(src_diff->meta, NULL, meta_name);
1414 orig_meta = lyd_find_meta(diff_match->meta, NULL, orig_meta_name);
1415 LY_CHECK_ERR_RET(!meta || !orig_meta, LOGINT(LYD_CTX(src_diff)), LY_EINT);
1416
1417 /* the (incorrect) assumption made here is that there are no previous diff nodes that would affect
1418 * the anchors stored in the metadata */
1419 if (strcmp(meta->value.canonical, orig_meta->value.canonical)) {
1420 /* deleted + created at another position -> operation REPLACE */
1421 LY_CHECK_RET(lyd_diff_change_op(diff_match, LYD_DIFF_OP_REPLACE));
1422
1423 /* add anchor metadata */
1424 LY_CHECK_RET(lyd_dup_meta_single(meta, diff_match, NULL));
1425 } else {
1426 /* deleted + created at the same position -> operation NONE */
1427 LY_CHECK_RET(lyd_diff_change_op(diff_match, LYD_DIFF_OP_NONE));
1428
1429 /* delete anchor metadata */
1430 lyd_free_meta_single(orig_meta);
1431 }
1432 } else if (diff_match->schema->nodetype == LYS_LEAF) {
1433 if (options & LYD_DIFF_MERGE_DEFAULTS) {
1434 /* we are dealing with a leaf and are handling default values specially (as explicit nodes) */
1435 sleaf = (struct lysc_node_leaf *)diff_match->schema;
1436 }
1437
1438 if (sleaf && sleaf->dflt &&
1439 !sleaf->dflt->realtype->plugin->compare(sleaf->dflt, &((struct lyd_node_term *)src_diff)->value)) {
1440 /* we deleted it, so a default value was in-use, and it matches the created value -> operation NONE */
1441 LY_CHECK_RET(lyd_diff_change_op(diff_match, LYD_DIFF_OP_NONE));
1442 } else if (!lyd_compare_single(diff_match, src_diff, 0)) {
1443 /* deleted + created -> operation NONE */
1444 LY_CHECK_RET(lyd_diff_change_op(diff_match, LYD_DIFF_OP_NONE));
1445 } else {
1446 /* we deleted it, but it was created with a different value -> operation REPLACE */
1447 LY_CHECK_RET(lyd_diff_change_op(diff_match, LYD_DIFF_OP_REPLACE));
1448
1449 /* current value is the previous one (meta) */
1450 LY_CHECK_RET(lyd_new_meta(LYD_CTX(src_diff), diff_match, NULL, "yang:orig-value",
1451 LYD_CANON_VALUE(diff_match), 0, NULL));
1452
1453 /* update the value itself */
1454 LY_CHECK_RET(lyd_change_term(diff_match, LYD_CANON_VALUE(src_diff)));
1455 }
1456 } else {
Michal Vaskoe6323f62020-07-09 15:49:02 +02001457 /* deleted + created -> operation NONE */
1458 LY_CHECK_RET(lyd_diff_change_op(diff_match, LYD_DIFF_OP_NONE));
Michal Vaskoe6323f62020-07-09 15:49:02 +02001459 }
1460
1461 if (diff_match->schema->nodetype & LYD_NODE_TERM) {
Michal Vasko4b715ca2020-11-11 18:39:57 +01001462 /* add orig-dflt metadata */
1463 LY_CHECK_RET(lyd_new_meta(LYD_CTX(src_diff), diff_match, NULL, "yang:orig-default",
1464 trg_flags & LYD_DEFAULT ? "true" : "false", 0, NULL));
1465
Michal Vaskoe6323f62020-07-09 15:49:02 +02001466 /* update dflt flag itself */
1467 diff_match->flags &= ~LYD_DEFAULT;
1468 diff_match->flags |= src_diff->flags & LYD_DEFAULT;
Michal Vaskoe78faec2021-04-08 17:24:43 +02001469 }
1470
1471 /* but the operation of its children should remain DELETE */
1472 LY_LIST_FOR(lyd_child_no_keys(diff_match), child) {
1473 LY_CHECK_RET(lyd_diff_change_op(child, LYD_DIFF_OP_DELETE));
Michal Vaskoe6323f62020-07-09 15:49:02 +02001474 }
1475 break;
1476 default:
1477 /* create and replace operations are not valid */
Michal Vaskob7be7a82020-08-20 09:09:04 +02001478 LOGINT_RET(LYD_CTX(src_diff));
Michal Vaskoe6323f62020-07-09 15:49:02 +02001479 }
1480
1481 return LY_SUCCESS;
1482}
1483
1484/**
1485 * @brief Update operations on a diff node when the new operation is DELETE.
1486 *
1487 * @param[in] diff_match Node from the diff.
Michal Vaskoe78faec2021-04-08 17:24:43 +02001488 * @param[in] cur_op Current operation of @p diff_match.
Michal Vaskoe6323f62020-07-09 15:49:02 +02001489 * @param[in] src_diff Current source diff node.
1490 * @return LY_ERR value.
1491 */
1492static LY_ERR
1493lyd_diff_merge_delete(struct lyd_node *diff_match, enum lyd_diff_op cur_op, const struct lyd_node *src_diff)
1494{
1495 struct lyd_node *next, *child;
Michal Vaskoe78faec2021-04-08 17:24:43 +02001496 struct lyd_meta *meta;
1497 const char *meta_name;
Michal Vaskoe6323f62020-07-09 15:49:02 +02001498
1499 /* we can delete only exact existing nodes */
Michal Vaskob7be7a82020-08-20 09:09:04 +02001500 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 +02001501
1502 switch (cur_op) {
1503 case LYD_DIFF_OP_CREATE:
1504 /* it was created, but then deleted -> set NONE operation */
1505 LY_CHECK_RET(lyd_diff_change_op(diff_match, LYD_DIFF_OP_NONE));
1506
1507 if (diff_match->schema->nodetype & LYD_NODE_TERM) {
1508 /* add orig-default meta because it is expected */
Michal Vasko871a0252020-11-11 18:35:24 +01001509 LY_CHECK_RET(lyd_new_meta(LYD_CTX(src_diff), diff_match, NULL, "yang:orig-default",
1510 diff_match->flags & LYD_DEFAULT ? "true" : "false", 0, NULL));
Michal Vaskoe78faec2021-04-08 17:24:43 +02001511 } else if (!lysc_is_dup_inst_list(diff_match->schema)) {
Michal Vaskoe6323f62020-07-09 15:49:02 +02001512 /* keep operation for all descendants (for now) */
Radek Krejcia1c1e542020-09-29 16:06:52 +02001513 LY_LIST_FOR(lyd_child_no_keys(diff_match), child) {
Michal Vaskoe6323f62020-07-09 15:49:02 +02001514 LY_CHECK_RET(lyd_diff_change_op(child, cur_op));
1515 }
Michal Vaskoe78faec2021-04-08 17:24:43 +02001516 } /* else key-less list, for which all the descendants act as keys */
Michal Vaskoe6323f62020-07-09 15:49:02 +02001517 break;
1518 case LYD_DIFF_OP_REPLACE:
Michal Vaskoe78faec2021-04-08 17:24:43 +02001519 /* similar to none operation but also remove the redundant metadata */
1520 if (lysc_is_userordered(diff_match->schema)) {
1521 if (lysc_is_dup_inst_list(diff_match->schema)) {
1522 meta_name = "position";
1523 } else if (diff_match->schema->nodetype == LYS_LIST) {
1524 meta_name = "key";
1525 } else {
1526 meta_name = "value";
1527 }
1528 } else {
1529 assert(diff_match->schema->nodetype == LYS_LEAF);
1530
1531 /* switch value for the original one */
1532 meta = lyd_find_meta(diff_match->meta, NULL, "yang:orig-value");
1533 LY_CHECK_ERR_RET(!meta, LOGINT(LYD_CTX(src_diff)), LY_EINT);
1534 if (lyd_change_term(diff_match, meta->value.canonical)) {
1535 LOGINT_RET(LYD_CTX(src_diff));
1536 }
1537
1538 /* switch default for the original one, then remove the meta */
1539 meta = lyd_find_meta(diff_match->meta, NULL, "yang:orig-default");
1540 LY_CHECK_ERR_RET(!meta, LOGINT(LYD_CTX(src_diff)), LY_EINT);
1541 diff_match->flags &= ~LYD_DEFAULT;
1542 if (meta->value.boolean) {
1543 diff_match->flags |= LYD_DEFAULT;
1544 }
1545 lyd_free_meta_single(meta);
1546
1547 meta_name = "orig-value";
1548 }
1549 lyd_diff_del_meta(diff_match, meta_name);
1550
Radek Krejcif13b87b2020-12-01 22:02:17 +01001551 /* fall through */
Michal Vaskoe6323f62020-07-09 15:49:02 +02001552 case LYD_DIFF_OP_NONE:
1553 /* it was not modified, but should be deleted -> set DELETE operation */
1554 LY_CHECK_RET(lyd_diff_change_op(diff_match, LYD_DIFF_OP_DELETE));
1555
Michal Vasko5632e0d2020-07-31 14:13:37 +02001556 /* all descendants not in the diff will be deleted and redundant in the diff, so remove them */
Radek Krejcia1c1e542020-09-29 16:06:52 +02001557 LY_LIST_FOR_SAFE(lyd_child_no_keys(diff_match), next, child) {
1558 if (lyd_find_sibling_first(lyd_child(src_diff), child, NULL) == LY_ENOTFOUND) {
Michal Vasko5632e0d2020-07-31 14:13:37 +02001559 lyd_free_tree(child);
1560 }
Michal Vaskoe6323f62020-07-09 15:49:02 +02001561 }
1562 break;
1563 default:
1564 /* delete operation is not valid */
Michal Vaskob7be7a82020-08-20 09:09:04 +02001565 LOGINT_RET(LYD_CTX(src_diff));
Michal Vaskoe6323f62020-07-09 15:49:02 +02001566 }
1567
1568 return LY_SUCCESS;
1569}
1570
1571/**
1572 * @brief Check whether this diff node is redundant (does not change data).
1573 *
1574 * @param[in] diff Diff node.
1575 * @return 0 if not, non-zero if it is.
1576 */
1577static int
1578lyd_diff_is_redundant(struct lyd_node *diff)
1579{
1580 enum lyd_diff_op op;
1581 struct lyd_meta *meta, *orig_val_meta = NULL, *val_meta = NULL;
1582 struct lyd_node *child;
1583 const struct lys_module *mod;
Michal Vaskoe78faec2021-04-08 17:24:43 +02001584 const char *str, *orig_meta_name, *meta_name;
Michal Vaskoe6323f62020-07-09 15:49:02 +02001585
1586 assert(diff);
1587
Michal Vaskoe78faec2021-04-08 17:24:43 +02001588 if (lysc_is_dup_inst_list(diff->schema)) {
1589 /* all descendants are keys */
1590 child = NULL;
1591 } else {
1592 child = lyd_child_no_keys(diff);
1593 }
Michal Vaskob7be7a82020-08-20 09:09:04 +02001594 mod = ly_ctx_get_module_latest(LYD_CTX(diff), "yang");
Michal Vaskoe6323f62020-07-09 15:49:02 +02001595 assert(mod);
1596
1597 /* get node operation */
Michal Vasko53bf6f22020-07-14 08:23:40 +02001598 LY_CHECK_RET(lyd_diff_get_op(diff, &op), 0);
Michal Vaskoe6323f62020-07-09 15:49:02 +02001599
1600 if ((op == LYD_DIFF_OP_REPLACE) && lysc_is_userordered(diff->schema)) {
Michal Vaskoe78faec2021-04-08 17:24:43 +02001601 /* get metadata names */
1602 if (lysc_is_dup_inst_list(diff->schema)) {
1603 meta_name = "position";
1604 orig_meta_name = "orig-position";
1605 } else if (diff->schema->nodetype == LYS_LIST) {
1606 meta_name = "key";
1607 orig_meta_name = "orig-key";
1608 } else {
1609 meta_name = "value";
1610 orig_meta_name = "orig-value";
1611 }
1612
Michal Vaskoe6323f62020-07-09 15:49:02 +02001613 /* check for redundant move */
Michal Vaskoe78faec2021-04-08 17:24:43 +02001614 orig_val_meta = lyd_find_meta(diff->meta, mod, orig_meta_name);
1615 val_meta = lyd_find_meta(diff->meta, mod, meta_name);
Michal Vaskoe6323f62020-07-09 15:49:02 +02001616 assert(orig_val_meta && val_meta);
1617
1618 if (!lyd_compare_meta(orig_val_meta, val_meta)) {
1619 /* there is actually no move */
Michal Vasko3a41dff2020-07-15 14:30:28 +02001620 lyd_free_meta_single(orig_val_meta);
1621 lyd_free_meta_single(val_meta);
Michal Vaskoe6323f62020-07-09 15:49:02 +02001622 if (child) {
1623 /* change operation to NONE, we have siblings */
1624 lyd_diff_change_op(diff, LYD_DIFF_OP_NONE);
1625 return 0;
1626 }
1627
1628 /* redundant node, BUT !!
1629 * In diff the move operation is always converted to be INSERT_AFTER, which is fine
1630 * because the data that this is applied on should not change for the diff lifetime.
1631 * However, when we are merging 2 diffs, this conversion is actually lossy because
1632 * if the data change, the move operation can also change its meaning. In this specific
1633 * case the move operation will be lost. But it can be considered a feature, it is not supported.
1634 */
1635 return 1;
1636 }
1637 } else if ((op == LYD_DIFF_OP_NONE) && (diff->schema->nodetype & LYD_NODE_TERM)) {
1638 /* check whether at least the default flags are different */
1639 meta = lyd_find_meta(diff->meta, mod, "orig-default");
1640 assert(meta);
Michal Vaskoba99a3e2020-08-18 15:50:05 +02001641 str = meta->value.canonical;
Michal Vaskoe6323f62020-07-09 15:49:02 +02001642
1643 /* if previous and current dflt flags are the same, this node is redundant */
1644 if ((!strcmp(str, "true") && (diff->flags & LYD_DEFAULT)) || (!strcmp(str, "false") && !(diff->flags & LYD_DEFAULT))) {
1645 return 1;
1646 }
1647 return 0;
1648 }
1649
1650 if (!child && (op == LYD_DIFF_OP_NONE)) {
1651 return 1;
1652 }
1653
1654 return 0;
1655}
1656
1657/**
Michal Vaskoe78faec2021-04-08 17:24:43 +02001658 * @brief Merge sysrepo diff subtree with another diff, recursively.
Michal Vaskoe6323f62020-07-09 15:49:02 +02001659 *
1660 * @param[in] src_diff Source diff node.
1661 * @param[in] diff_parent Current sysrepo diff parent.
1662 * @param[in] diff_cb Optional diff callback.
1663 * @param[in] cb_data User data for @p diff_cb.
Michal Vaskoe78faec2021-04-08 17:24:43 +02001664 * @param[in,out] dup_inst Duplicate instance cache for all @p src_diff siblings.
Michal Vaskoc0e58e82020-11-11 19:04:33 +01001665 * @param[in] options Diff merge options.
Michal Vaskoe6323f62020-07-09 15:49:02 +02001666 * @param[in,out] diff Diff root node.
1667 * @return LY_ERR value.
1668 */
1669static LY_ERR
1670lyd_diff_merge_r(const struct lyd_node *src_diff, struct lyd_node *diff_parent, lyd_diff_cb diff_cb, void *cb_data,
Michal Vaskoe78faec2021-04-08 17:24:43 +02001671 struct lyd_diff_dup_inst **dup_inst, uint16_t options, struct lyd_node **diff)
Michal Vaskoe6323f62020-07-09 15:49:02 +02001672{
1673 LY_ERR ret = LY_SUCCESS;
1674 struct lyd_node *child, *diff_node = NULL;
1675 enum lyd_diff_op src_op, cur_op;
Michal Vaskoe78faec2021-04-08 17:24:43 +02001676 struct lyd_diff_dup_inst *child_dup_inst = NULL;
Michal Vaskoe6323f62020-07-09 15:49:02 +02001677
1678 /* get source node operation */
1679 LY_CHECK_RET(lyd_diff_get_op(src_diff, &src_op));
1680
1681 /* find an equal node in the current diff */
Michal Vaskoe78faec2021-04-08 17:24:43 +02001682 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 +02001683
1684 if (diff_node) {
1685 /* get target (current) operation */
1686 LY_CHECK_RET(lyd_diff_get_op(diff_node, &cur_op));
1687
1688 /* merge operations */
1689 switch (src_op) {
1690 case LYD_DIFF_OP_REPLACE:
1691 ret = lyd_diff_merge_replace(diff_node, cur_op, src_diff);
1692 break;
1693 case LYD_DIFF_OP_CREATE:
Michal Vasko1dc0a842021-02-04 11:04:57 +01001694 if ((cur_op == LYD_DIFF_OP_CREATE) && lysc_is_dup_inst_list(diff_node->schema)) {
Michal Vaskoe78faec2021-04-08 17:24:43 +02001695 /* special case of creating duplicate (leaf-)list instances */
Michal Vasko1dc0a842021-02-04 11:04:57 +01001696 goto add_diff;
1697 }
1698
Michal Vaskoc0e58e82020-11-11 19:04:33 +01001699 ret = lyd_diff_merge_create(diff_node, cur_op, src_diff, options);
Michal Vaskoe6323f62020-07-09 15:49:02 +02001700 break;
1701 case LYD_DIFF_OP_DELETE:
1702 ret = lyd_diff_merge_delete(diff_node, cur_op, src_diff);
1703 break;
1704 case LYD_DIFF_OP_NONE:
Michal Vaskoe78faec2021-04-08 17:24:43 +02001705 /* key-less list can never have "none" operation since all its descendants are acting as "keys" */
1706 assert((src_diff->schema->nodetype != LYS_LIST) || !lysc_is_dup_inst_list(src_diff->schema));
Michal Vaskoe6323f62020-07-09 15:49:02 +02001707 ret = lyd_diff_merge_none(diff_node, cur_op, src_diff);
1708 break;
1709 default:
Michal Vaskob7be7a82020-08-20 09:09:04 +02001710 LOGINT_RET(LYD_CTX(src_diff));
Michal Vaskoe6323f62020-07-09 15:49:02 +02001711 }
1712 if (ret) {
Michal Vaskob7be7a82020-08-20 09:09:04 +02001713 LOGERR(LYD_CTX(src_diff), LY_EOTHER, "Merging operation \"%s\" failed.", lyd_diff_op2str(src_op));
Michal Vaskoe6323f62020-07-09 15:49:02 +02001714 return ret;
1715 }
1716
1717 if (diff_cb) {
1718 /* call callback */
Michal Vaskobc5fba92020-08-07 12:14:39 +02001719 LY_CHECK_RET(diff_cb(src_diff, diff_node, cb_data));
Michal Vaskoe6323f62020-07-09 15:49:02 +02001720 }
1721
1722 /* update diff parent */
1723 diff_parent = diff_node;
1724
Michal Vaskoe78faec2021-04-08 17:24:43 +02001725 /* for diff purposes, all key-less list descendants actually act as keys (identifying the same instances),
1726 * so there is nothing to merge for these "keys" */
1727 if (!lysc_is_dup_inst_list(src_diff->schema)) {
1728 /* merge src_diff recursively */
1729 LY_LIST_FOR(lyd_child_no_keys(src_diff), child) {
1730 ret = lyd_diff_merge_r(child, diff_parent, diff_cb, cb_data, &child_dup_inst, options, diff);
1731 if (ret) {
1732 break;
1733 }
1734 }
1735 lyd_diff_dup_inst_free(child_dup_inst);
1736 LY_CHECK_RET(ret);
Michal Vaskoe6323f62020-07-09 15:49:02 +02001737 }
1738 } else {
Michal Vasko1dc0a842021-02-04 11:04:57 +01001739add_diff:
Michal Vaskoe6323f62020-07-09 15:49:02 +02001740 /* add new diff node with all descendants */
Michal Vasko871a0252020-11-11 18:35:24 +01001741 LY_CHECK_RET(lyd_dup_single(src_diff, (struct lyd_node_inner *)diff_parent, LYD_DUP_RECURSIVE | LYD_DUP_WITH_FLAGS,
1742 &diff_node));
Michal Vaskoe6323f62020-07-09 15:49:02 +02001743
1744 /* insert node into diff if not already */
1745 if (!diff_parent) {
Michal Vaskob104f112020-07-17 09:54:54 +02001746 lyd_insert_sibling(*diff, diff_node, diff);
Michal Vaskoe6323f62020-07-09 15:49:02 +02001747 }
1748
1749 /* update operation */
1750 LY_CHECK_RET(lyd_diff_change_op(diff_node, src_op));
1751
1752 if (diff_cb) {
Michal Vaskoe2af8412020-12-03 14:11:38 +01001753 /* call callback with no source diff node since it was duplicated and just added */
1754 LY_CHECK_RET(diff_cb(NULL, diff_node, cb_data));
Michal Vaskoe6323f62020-07-09 15:49:02 +02001755 }
1756
1757 /* update diff parent */
1758 diff_parent = diff_node;
1759 }
1760
1761 /* remove any redundant nodes */
Michal Vaskob98d7082020-07-15 16:38:36 +02001762 if (lyd_diff_is_redundant(diff_parent)) {
Michal Vaskoe6323f62020-07-09 15:49:02 +02001763 if (diff_parent == *diff) {
1764 *diff = (*diff)->next;
1765 }
1766 lyd_free_tree(diff_parent);
1767 }
1768
1769 return LY_SUCCESS;
1770}
1771
1772API LY_ERR
Michal Vaskofb737aa2020-08-06 13:53:53 +02001773lyd_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 +01001774 lyd_diff_cb diff_cb, void *cb_data, uint16_t options)
Michal Vaskoe6323f62020-07-09 15:49:02 +02001775{
1776 const struct lyd_node *src_root;
Michal Vaskoe78faec2021-04-08 17:24:43 +02001777 struct lyd_diff_dup_inst *dup_inst = NULL;
1778 LY_ERR ret = LY_SUCCESS;
Michal Vaskoe6323f62020-07-09 15:49:02 +02001779
1780 LY_LIST_FOR(src_diff, src_root) {
1781 if (mod && (lyd_owner_module(src_root) != mod)) {
1782 /* skip data nodes from different modules */
1783 continue;
1784 }
1785
1786 /* apply relevant nodes from the diff datatree */
Michal Vaskoe78faec2021-04-08 17:24:43 +02001787 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 +02001788 }
1789
Michal Vaskoe78faec2021-04-08 17:24:43 +02001790cleanup:
1791 lyd_diff_dup_inst_free(dup_inst);
1792 return ret;
Michal Vaskoe6323f62020-07-09 15:49:02 +02001793}
1794
1795API LY_ERR
Michal Vasko04f85912020-08-07 12:14:58 +02001796lyd_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 +01001797 lyd_diff_cb diff_cb, void *cb_data, uint16_t options)
Michal Vasko04f85912020-08-07 12:14:58 +02001798{
Michal Vaskoe78faec2021-04-08 17:24:43 +02001799 LY_ERR ret;
1800 struct lyd_diff_dup_inst *dup_inst = NULL;
1801
Michal Vasko04f85912020-08-07 12:14:58 +02001802 if (!src_sibling) {
1803 return LY_SUCCESS;
1804 }
1805
Michal Vaskoe78faec2021-04-08 17:24:43 +02001806 ret = lyd_diff_merge_r(src_sibling, diff_parent, diff_cb, cb_data, &dup_inst, options, diff_first);
1807 lyd_diff_dup_inst_free(dup_inst);
1808 return ret;
Michal Vasko04f85912020-08-07 12:14:58 +02001809}
1810
1811API LY_ERR
Michal Vaskoc0e58e82020-11-11 19:04:33 +01001812lyd_diff_merge_all(struct lyd_node **diff, const struct lyd_node *src_diff, uint16_t options)
Michal Vaskoe6323f62020-07-09 15:49:02 +02001813{
Michal Vaskoc0e58e82020-11-11 19:04:33 +01001814 return lyd_diff_merge_module(diff, src_diff, NULL, NULL, NULL, options);
Michal Vaskoe6323f62020-07-09 15:49:02 +02001815}
Michal Vasko4231fb62020-07-13 13:54:47 +02001816
1817static LY_ERR
Michal Vaskobaba84e2021-02-05 16:33:30 +01001818lyd_diff_reverse_value(struct lyd_node *node, const struct lys_module *mod)
Michal Vasko4231fb62020-07-13 13:54:47 +02001819{
1820 LY_ERR ret = LY_SUCCESS;
1821 struct lyd_meta *meta;
Michal Vaskoba99a3e2020-08-18 15:50:05 +02001822 const char *val1 = NULL;
1823 char *val2;
Radek Krejci1deb5be2020-08-26 16:43:36 +02001824 uint32_t flags;
Michal Vasko4231fb62020-07-13 13:54:47 +02001825
Michal Vaskobaba84e2021-02-05 16:33:30 +01001826 assert(node->schema->nodetype & (LYS_LEAF | LYS_ANYDATA));
1827
1828 meta = lyd_find_meta(node->meta, mod, "orig-value");
1829 LY_CHECK_ERR_RET(!meta, LOGINT(LYD_CTX(node)), LY_EINT);
Michal Vasko4231fb62020-07-13 13:54:47 +02001830
1831 /* orig-value */
Michal Vaskoba99a3e2020-08-18 15:50:05 +02001832 val1 = meta->value.canonical;
Michal Vasko4231fb62020-07-13 13:54:47 +02001833
1834 /* current value */
Michal Vaskobaba84e2021-02-05 16:33:30 +01001835 if (node->schema->nodetype == LYS_LEAF) {
1836 val2 = strdup(LYD_CANON_VALUE(node));
1837 } else {
1838 LY_CHECK_RET(lyd_any_value_str(node, &val2));
1839 }
Michal Vasko4231fb62020-07-13 13:54:47 +02001840
1841 /* switch values, keep default flag */
Michal Vaskobaba84e2021-02-05 16:33:30 +01001842 flags = node->flags;
1843 if (node->schema->nodetype == LYS_LEAF) {
1844 LY_CHECK_GOTO(ret = lyd_change_term(node, val1), cleanup);
1845 } else {
1846 union lyd_any_value anyval = {.str = val1};
1847 LY_CHECK_GOTO(ret = lyd_any_copy_value(node, &anyval, LYD_ANYDATA_STRING), cleanup);
1848 }
1849 node->flags = flags;
Michal Vasko4231fb62020-07-13 13:54:47 +02001850 LY_CHECK_GOTO(ret = lyd_change_meta(meta, val2), cleanup);
1851
1852cleanup:
Michal Vaskoba99a3e2020-08-18 15:50:05 +02001853 free(val2);
Michal Vasko4231fb62020-07-13 13:54:47 +02001854 return ret;
1855}
1856
1857static LY_ERR
1858lyd_diff_reverse_default(struct lyd_node *node, const struct lys_module *mod)
1859{
1860 struct lyd_meta *meta;
Radek Krejci1deb5be2020-08-26 16:43:36 +02001861 uint32_t flag1, flag2;
Michal Vasko4231fb62020-07-13 13:54:47 +02001862
1863 meta = lyd_find_meta(node->meta, mod, "orig-default");
Michal Vasko610e93b2020-11-09 20:58:32 +01001864 LY_CHECK_ERR_RET(!meta, LOGINT(mod->ctx), LY_EINT);
Michal Vasko4231fb62020-07-13 13:54:47 +02001865
1866 /* orig-default */
Michal Vaskoba99a3e2020-08-18 15:50:05 +02001867 if (meta->value.boolean) {
Michal Vasko4231fb62020-07-13 13:54:47 +02001868 flag1 = LYD_DEFAULT;
1869 } else {
1870 flag1 = 0;
1871 }
1872
1873 /* current default */
1874 flag2 = node->flags & LYD_DEFAULT;
1875
Michal Vasko610e93b2020-11-09 20:58:32 +01001876 if (flag1 == flag2) {
1877 /* no default state change so nothing to reverse */
1878 return LY_SUCCESS;
1879 }
1880
Michal Vasko4231fb62020-07-13 13:54:47 +02001881 /* switch defaults */
1882 node->flags &= ~LYD_DEFAULT;
1883 node->flags |= flag1;
1884 LY_CHECK_RET(lyd_change_meta(meta, flag2 ? "true" : "false"));
1885
1886 return LY_SUCCESS;
1887}
1888
1889static LY_ERR
1890lyd_diff_reverse_meta(struct lyd_node *node, const struct lys_module *mod, const char *name1, const char *name2)
1891{
1892 LY_ERR ret = LY_SUCCESS;
1893 struct lyd_meta *meta1, *meta2;
Michal Vaskoba99a3e2020-08-18 15:50:05 +02001894 const char *val1 = NULL;
1895 char *val2 = NULL;
Michal Vasko4231fb62020-07-13 13:54:47 +02001896
1897 meta1 = lyd_find_meta(node->meta, mod, name1);
Michal Vaskob7be7a82020-08-20 09:09:04 +02001898 LY_CHECK_ERR_RET(!meta1, LOGINT(LYD_CTX(node)), LY_EINT);
Michal Vasko4231fb62020-07-13 13:54:47 +02001899
1900 meta2 = lyd_find_meta(node->meta, mod, name2);
Michal Vaskob7be7a82020-08-20 09:09:04 +02001901 LY_CHECK_ERR_RET(!meta2, LOGINT(LYD_CTX(node)), LY_EINT);
Michal Vasko4231fb62020-07-13 13:54:47 +02001902
1903 /* value1 */
Michal Vaskoba99a3e2020-08-18 15:50:05 +02001904 val1 = meta1->value.canonical;
Michal Vasko4231fb62020-07-13 13:54:47 +02001905
1906 /* value2 */
Michal Vaskoba99a3e2020-08-18 15:50:05 +02001907 val2 = strdup(meta2->value.canonical);
Michal Vasko4231fb62020-07-13 13:54:47 +02001908
1909 /* switch values */
1910 LY_CHECK_GOTO(ret = lyd_change_meta(meta1, val2), cleanup);
1911 LY_CHECK_GOTO(ret = lyd_change_meta(meta2, val1), cleanup);
1912
1913cleanup:
Michal Vaskoba99a3e2020-08-18 15:50:05 +02001914 free(val2);
Michal Vasko4231fb62020-07-13 13:54:47 +02001915 return ret;
1916}
1917
Michal Vasko9a7e9d02021-03-09 13:52:25 +01001918/**
1919 * @brief Remove specific operation from all the nodes in a subtree.
1920 *
1921 * @param[in] diff Diff subtree to process.
1922 * @param[in] op Only expected operation.
1923 * @return LY_ERR value.
1924 */
1925static LY_ERR
1926lyd_diff_reverse_remove_op_r(struct lyd_node *diff, enum lyd_diff_op op)
1927{
1928 struct lyd_node *elem;
1929 struct lyd_meta *meta;
1930
1931 LYD_TREE_DFS_BEGIN(diff, elem) {
1932 meta = lyd_find_meta(elem->meta, NULL, "yang:operation");
1933 if (meta) {
1934 LY_CHECK_ERR_RET(lyd_diff_str2op(meta->value.canonical) != op, LOGINT(LYD_CTX(diff)), LY_EINT);
1935 lyd_free_meta_single(meta);
1936 }
1937
1938 LYD_TREE_DFS_END(diff, elem);
1939 }
1940
1941 return LY_SUCCESS;
1942}
1943
Michal Vasko4231fb62020-07-13 13:54:47 +02001944API LY_ERR
Michal Vasko66535812020-08-11 08:44:22 +02001945lyd_diff_reverse_all(const struct lyd_node *src_diff, struct lyd_node **diff)
Michal Vasko4231fb62020-07-13 13:54:47 +02001946{
1947 LY_ERR ret = LY_SUCCESS;
1948 const struct lys_module *mod;
Michal Vasko9a7e9d02021-03-09 13:52:25 +01001949 struct lyd_node *root, *elem, *iter;
Michal Vasko4231fb62020-07-13 13:54:47 +02001950 enum lyd_diff_op op;
1951
1952 LY_CHECK_ARG_RET(NULL, diff, LY_EINVAL);
1953
1954 if (!src_diff) {
1955 *diff = NULL;
1956 return LY_SUCCESS;
1957 }
1958
1959 /* duplicate diff */
Michal Vasko3a41dff2020-07-15 14:30:28 +02001960 LY_CHECK_RET(lyd_dup_siblings(src_diff, NULL, LYD_DUP_RECURSIVE, diff));
Michal Vasko4231fb62020-07-13 13:54:47 +02001961
1962 /* find module with metadata needed for later */
Michal Vaskob7be7a82020-08-20 09:09:04 +02001963 mod = ly_ctx_get_module_latest(LYD_CTX(src_diff), "yang");
1964 LY_CHECK_ERR_GOTO(!mod, LOGINT(LYD_CTX(src_diff)); ret = LY_EINT, cleanup);
Michal Vasko4231fb62020-07-13 13:54:47 +02001965
1966 LY_LIST_FOR(*diff, root) {
Michal Vasko56daf732020-08-10 10:57:18 +02001967 LYD_TREE_DFS_BEGIN(root, elem) {
Michal Vasko0f5c6a52020-11-06 17:18:03 +01001968 /* skip all keys */
1969 if (!lysc_is_key(elem->schema)) {
1970 /* find operation attribute, if any */
1971 LY_CHECK_GOTO(ret = lyd_diff_get_op(elem, &op), cleanup);
Michal Vasko4231fb62020-07-13 13:54:47 +02001972
Michal Vasko0f5c6a52020-11-06 17:18:03 +01001973 switch (op) {
1974 case LYD_DIFF_OP_CREATE:
1975 /* reverse create to delete */
1976 LY_CHECK_GOTO(ret = lyd_diff_change_op(elem, LYD_DIFF_OP_DELETE), cleanup);
Michal Vasko9e070522021-03-05 14:00:14 +01001977
Michal Vasko9a7e9d02021-03-09 13:52:25 +01001978 /* check all the children for the same operation, nothing else is expected */
1979 LY_LIST_FOR(lyd_child(elem), iter) {
1980 lyd_diff_reverse_remove_op_r(iter, LYD_DIFF_OP_CREATE);
1981 }
1982
Michal Vasko9e070522021-03-05 14:00:14 +01001983 LYD_TREE_DFS_continue = 1;
Michal Vasko4231fb62020-07-13 13:54:47 +02001984 break;
Michal Vasko0f5c6a52020-11-06 17:18:03 +01001985 case LYD_DIFF_OP_DELETE:
1986 /* reverse delete to create */
1987 LY_CHECK_GOTO(ret = lyd_diff_change_op(elem, LYD_DIFF_OP_CREATE), cleanup);
Michal Vasko9e070522021-03-05 14:00:14 +01001988
Michal Vasko9a7e9d02021-03-09 13:52:25 +01001989 /* check all the children for the same operation, nothing else is expected */
1990 LY_LIST_FOR(lyd_child(elem), iter) {
1991 lyd_diff_reverse_remove_op_r(iter, LYD_DIFF_OP_DELETE);
1992 }
1993
Michal Vasko9e070522021-03-05 14:00:14 +01001994 LYD_TREE_DFS_continue = 1;
Michal Vasko4231fb62020-07-13 13:54:47 +02001995 break;
Michal Vasko0f5c6a52020-11-06 17:18:03 +01001996 case LYD_DIFF_OP_REPLACE:
1997 switch (elem->schema->nodetype) {
1998 case LYS_LEAF:
1999 /* leaf value change */
2000 LY_CHECK_GOTO(ret = lyd_diff_reverse_value(elem, mod), cleanup);
2001 LY_CHECK_GOTO(ret = lyd_diff_reverse_default(elem, mod), cleanup);
2002 break;
Michal Vaskobaba84e2021-02-05 16:33:30 +01002003 case LYS_ANYXML:
2004 case LYS_ANYDATA:
2005 /* any value change */
2006 LY_CHECK_GOTO(ret = lyd_diff_reverse_value(elem, mod), cleanup);
2007 break;
Michal Vasko0f5c6a52020-11-06 17:18:03 +01002008 case LYS_LEAFLIST:
2009 /* leaf-list move */
2010 LY_CHECK_GOTO(ret = lyd_diff_reverse_default(elem, mod), cleanup);
Michal Vaskoe78faec2021-04-08 17:24:43 +02002011 if (lysc_is_dup_inst_list(elem->schema)) {
2012 LY_CHECK_GOTO(ret = lyd_diff_reverse_meta(elem, mod, "orig-position", "position"), cleanup);
2013 } else {
2014 LY_CHECK_GOTO(ret = lyd_diff_reverse_meta(elem, mod, "orig-value", "value"), cleanup);
2015 }
Michal Vasko0f5c6a52020-11-06 17:18:03 +01002016 break;
2017 case LYS_LIST:
2018 /* list move */
Michal Vaskoe78faec2021-04-08 17:24:43 +02002019 if (lysc_is_dup_inst_list(elem->schema)) {
2020 LY_CHECK_GOTO(ret = lyd_diff_reverse_meta(elem, mod, "orig-position", "position"), cleanup);
2021 } else {
2022 LY_CHECK_GOTO(ret = lyd_diff_reverse_meta(elem, mod, "orig-key", "key"), cleanup);
2023 }
Michal Vasko0f5c6a52020-11-06 17:18:03 +01002024 break;
2025 default:
2026 LOGINT(LYD_CTX(src_diff));
2027 ret = LY_EINT;
2028 goto cleanup;
2029 }
Michal Vasko4231fb62020-07-13 13:54:47 +02002030 break;
Michal Vasko0f5c6a52020-11-06 17:18:03 +01002031 case LYD_DIFF_OP_NONE:
2032 switch (elem->schema->nodetype) {
2033 case LYS_LEAF:
2034 case LYS_LEAFLIST:
2035 /* default flag change */
2036 LY_CHECK_GOTO(ret = lyd_diff_reverse_default(elem, mod), cleanup);
2037 break;
2038 default:
2039 /* nothing to do */
2040 break;
2041 }
Michal Vasko4231fb62020-07-13 13:54:47 +02002042 break;
2043 }
Michal Vasko4231fb62020-07-13 13:54:47 +02002044 }
2045
Michal Vasko56daf732020-08-10 10:57:18 +02002046 LYD_TREE_DFS_END(root, elem);
Michal Vasko4231fb62020-07-13 13:54:47 +02002047 }
2048 }
2049
2050cleanup:
2051 if (ret) {
2052 lyd_free_siblings(*diff);
2053 *diff = NULL;
2054 }
2055 return ret;
2056}