Merge "Added eventSchemaVersion for async schema"
[cps.git] / cps-ri / src / main / java / org / onap / cps / spi / impl / CpsDataPersistenceServiceImpl.java
1 /*
2  *  ============LICENSE_START=======================================================
3  *  Copyright (C) 2021-2022 Nordix Foundation
4  *  Modifications Copyright (C) 2021 Pantheon.tech
5  *  Modifications Copyright (C) 2020-2022 Bell Canada.
6  *  ================================================================================
7  *  Licensed under the Apache License, Version 2.0 (the "License");
8  *  you may not use this file except in compliance with the License.
9  *  You may obtain a copy of the License at
10  *
11  *        http://www.apache.org/licenses/LICENSE-2.0
12  *
13  *  Unless required by applicable law or agreed to in writing, software
14  *  distributed under the License is distributed on an "AS IS" BASIS,
15  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  *  See the License for the specific language governing permissions and
17  *  limitations under the License.
18  *
19  *  SPDX-License-Identifier: Apache-2.0
20  *  ============LICENSE_END=========================================================
21  */
22
23 package org.onap.cps.spi.impl;
24
25 import static org.onap.cps.spi.FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS;
26
27 import com.google.common.collect.ImmutableSet;
28 import com.google.common.collect.ImmutableSet.Builder;
29 import java.util.ArrayList;
30 import java.util.Collection;
31 import java.util.Collections;
32 import java.util.HashMap;
33 import java.util.HashSet;
34 import java.util.List;
35 import java.util.Map;
36 import java.util.Set;
37 import java.util.regex.Matcher;
38 import java.util.regex.Pattern;
39 import java.util.stream.Collectors;
40 import javax.transaction.Transactional;
41 import lombok.RequiredArgsConstructor;
42 import lombok.extern.slf4j.Slf4j;
43 import org.hibernate.StaleStateException;
44 import org.onap.cps.cpspath.parser.CpsPathQuery;
45 import org.onap.cps.cpspath.parser.CpsPathUtil;
46 import org.onap.cps.cpspath.parser.PathParsingException;
47 import org.onap.cps.spi.CpsDataPersistenceService;
48 import org.onap.cps.spi.FetchDescendantsOption;
49 import org.onap.cps.spi.entities.AnchorEntity;
50 import org.onap.cps.spi.entities.DataspaceEntity;
51 import org.onap.cps.spi.entities.FragmentEntity;
52 import org.onap.cps.spi.entities.SchemaSetEntity;
53 import org.onap.cps.spi.entities.YangResourceEntity;
54 import org.onap.cps.spi.exceptions.AlreadyDefinedException;
55 import org.onap.cps.spi.exceptions.ConcurrencyException;
56 import org.onap.cps.spi.exceptions.CpsAdminException;
57 import org.onap.cps.spi.exceptions.CpsPathException;
58 import org.onap.cps.spi.exceptions.DataNodeNotFoundException;
59 import org.onap.cps.spi.model.DataNode;
60 import org.onap.cps.spi.model.DataNodeBuilder;
61 import org.onap.cps.spi.repository.AnchorRepository;
62 import org.onap.cps.spi.repository.DataspaceRepository;
63 import org.onap.cps.spi.repository.FragmentRepository;
64 import org.onap.cps.spi.utils.SessionManager;
65 import org.onap.cps.utils.JsonObjectMapper;
66 import org.onap.cps.yang.YangTextSchemaSourceSetBuilder;
67 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
68 import org.springframework.dao.DataIntegrityViolationException;
69 import org.springframework.stereotype.Service;
70
71 @Service
72 @Slf4j
73 @RequiredArgsConstructor
74 public class CpsDataPersistenceServiceImpl implements CpsDataPersistenceService {
75
76     private final DataspaceRepository dataspaceRepository;
77
78     private final AnchorRepository anchorRepository;
79
80     private final FragmentRepository fragmentRepository;
81
82     private final JsonObjectMapper jsonObjectMapper;
83
84     private final SessionManager sessionManager;
85
86     private static final String REG_EX_FOR_OPTIONAL_LIST_INDEX = "(\\[@[\\s\\S]+?]){0,1})";
87     private static final Pattern REG_EX_PATTERN_FOR_LIST_ELEMENT_KEY_PREDICATE =
88             Pattern.compile("\\[(\\@([^\\/]{0,9999}))\\]$");
89
90     @Override
91     @Transactional
92     public void addChildDataNode(final String dataspaceName, final String anchorName, final String parentNodeXpath,
93                                  final DataNode newChildDataNode) {
94         addChildDataNodes(dataspaceName, anchorName, parentNodeXpath, Collections.singleton(newChildDataNode));
95     }
96
97     @Override
98     @Transactional
99     public void addListElements(final String dataspaceName, final String anchorName, final String parentNodeXpath,
100                                 final Collection<DataNode> newListElements) {
101         addChildDataNodes(dataspaceName, anchorName, parentNodeXpath, newListElements);
102     }
103
104     private void addChildDataNodes(final String dataspaceName, final String anchorName, final String parentNodeXpath,
105                                    final Collection<DataNode> newChildren) {
106         final FragmentEntity parentFragmentEntity = getFragmentByXpath(dataspaceName, anchorName, parentNodeXpath);
107         try {
108             final List<FragmentEntity> fragmentEntities = new ArrayList<>();
109             newChildren.forEach(newChildAsDataNode -> {
110                 final FragmentEntity newChildAsFragmentEntity = convertToFragmentWithAllDescendants(
111                     parentFragmentEntity.getDataspace(),
112                     parentFragmentEntity.getAnchor(),
113                     newChildAsDataNode);
114                 newChildAsFragmentEntity.setParentId(parentFragmentEntity.getId());
115                 fragmentEntities.add(newChildAsFragmentEntity);
116             });
117             fragmentRepository.saveAll(fragmentEntities);
118         } catch (final DataIntegrityViolationException exception) {
119             final List<String> conflictXpaths = newChildren.stream()
120                     .map(DataNode::getXpath)
121                     .collect(Collectors.toList());
122             throw AlreadyDefinedException.forDataNodes(conflictXpaths, anchorName, exception);
123         }
124     }
125
126     @Override
127     public void storeDataNode(final String dataspaceName, final String anchorName, final DataNode dataNode) {
128         final DataspaceEntity dataspaceEntity = dataspaceRepository.getByName(dataspaceName);
129         final AnchorEntity anchorEntity = anchorRepository.getByDataspaceAndName(dataspaceEntity, anchorName);
130         final FragmentEntity fragmentEntity = convertToFragmentWithAllDescendants(dataspaceEntity, anchorEntity,
131                 dataNode);
132         try {
133             fragmentRepository.save(fragmentEntity);
134         } catch (final DataIntegrityViolationException exception) {
135             throw AlreadyDefinedException.forDataNode(dataNode.getXpath(), anchorName, exception);
136         }
137     }
138
139     /**
140      * Convert DataNode object into Fragment and places the result in the fragments placeholder. Performs same action
141      * for all DataNode children recursively.
142      *
143      * @param dataspaceEntity       dataspace
144      * @param anchorEntity          anchorEntity
145      * @param dataNodeToBeConverted dataNode
146      * @return a Fragment built from current DataNode
147      */
148     private FragmentEntity convertToFragmentWithAllDescendants(final DataspaceEntity dataspaceEntity,
149                              final AnchorEntity anchorEntity, final DataNode dataNodeToBeConverted) {
150         final FragmentEntity parentFragment = toFragmentEntity(dataspaceEntity, anchorEntity, dataNodeToBeConverted);
151         final Builder<FragmentEntity> childFragmentsImmutableSetBuilder = ImmutableSet.builder();
152         for (final DataNode childDataNode : dataNodeToBeConverted.getChildDataNodes()) {
153             final FragmentEntity childFragment =
154                     convertToFragmentWithAllDescendants(parentFragment.getDataspace(), parentFragment.getAnchor(),
155                             childDataNode);
156             childFragmentsImmutableSetBuilder.add(childFragment);
157         }
158         parentFragment.setChildFragments(childFragmentsImmutableSetBuilder.build());
159         return parentFragment;
160     }
161
162     private FragmentEntity toFragmentEntity(final DataspaceEntity dataspaceEntity,
163                                             final AnchorEntity anchorEntity, final DataNode dataNode) {
164         return FragmentEntity.builder()
165                 .dataspace(dataspaceEntity)
166                 .anchor(anchorEntity)
167                 .xpath(dataNode.getXpath())
168                 .attributes(jsonObjectMapper.asJsonString(dataNode.getLeaves()))
169                 .build();
170     }
171
172     @Override
173     public DataNode getDataNode(final String dataspaceName, final String anchorName, final String xpath,
174                                 final FetchDescendantsOption fetchDescendantsOption) {
175         final FragmentEntity fragmentEntity = getFragmentByXpath(dataspaceName, anchorName, xpath);
176         return toDataNode(fragmentEntity, fetchDescendantsOption);
177     }
178
179     private FragmentEntity getFragmentByXpath(final String dataspaceName, final String anchorName,
180                                               final String xpath) {
181         final DataspaceEntity dataspaceEntity = dataspaceRepository.getByName(dataspaceName);
182         final AnchorEntity anchorEntity = anchorRepository.getByDataspaceAndName(dataspaceEntity, anchorName);
183         if (isRootXpath(xpath)) {
184             return fragmentRepository.findFirstRootByDataspaceAndAnchor(dataspaceEntity, anchorEntity);
185         } else {
186             final String normalizedXpath;
187             try {
188                 normalizedXpath = CpsPathUtil.getNormalizedXpath(xpath);
189             } catch (final PathParsingException e) {
190                 throw new CpsPathException(e.getMessage());
191             }
192             return fragmentRepository.getByDataspaceAndAnchorAndXpath(dataspaceEntity, anchorEntity,
193                     normalizedXpath);
194         }
195     }
196
197     @Override
198     public List<DataNode> queryDataNodes(final String dataspaceName, final String anchorName, final String cpsPath,
199                                          final FetchDescendantsOption fetchDescendantsOption) {
200         final DataspaceEntity dataspaceEntity = dataspaceRepository.getByName(dataspaceName);
201         final AnchorEntity anchorEntity = anchorRepository.getByDataspaceAndName(dataspaceEntity, anchorName);
202         final CpsPathQuery cpsPathQuery;
203         try {
204             cpsPathQuery = CpsPathUtil.getCpsPathQuery(cpsPath);
205         } catch (final PathParsingException e) {
206             throw new CpsPathException(e.getMessage());
207         }
208         List<FragmentEntity> fragmentEntities =
209                 fragmentRepository.findByAnchorAndCpsPath(anchorEntity.getId(), cpsPathQuery);
210         if (cpsPathQuery.hasAncestorAxis()) {
211             final Set<String> ancestorXpaths = processAncestorXpath(fragmentEntities, cpsPathQuery);
212             fragmentEntities = ancestorXpaths.isEmpty() ? Collections.emptyList()
213                     : fragmentRepository.findAllByAnchorAndXpathIn(anchorEntity, ancestorXpaths);
214         }
215         return fragmentEntities.stream()
216                 .map(fragmentEntity -> toDataNode(fragmentEntity, fetchDescendantsOption))
217                 .collect(Collectors.toUnmodifiableList());
218     }
219
220     @Override
221     public String startSession() {
222         return sessionManager.startSession();
223     }
224
225     @Override
226     public void closeSession(final String sessionId) {
227         sessionManager.closeSession(sessionId, SessionManager.WITH_COMMIT);
228     }
229
230     @Override
231     public void lockAnchor(final String sessionId, final String dataspaceName,
232                            final String anchorName, final Long timeoutInMilliseconds) {
233         sessionManager.lockAnchor(sessionId, dataspaceName, anchorName, timeoutInMilliseconds);
234     }
235
236     private static Set<String> processAncestorXpath(final List<FragmentEntity> fragmentEntities,
237                                                     final CpsPathQuery cpsPathQuery) {
238         final Set<String> ancestorXpath = new HashSet<>();
239         final Pattern pattern =
240                 Pattern.compile("([\\s\\S]*\\/" + Pattern.quote(cpsPathQuery.getAncestorSchemaNodeIdentifier())
241                         + REG_EX_FOR_OPTIONAL_LIST_INDEX + "\\/[\\s\\S]*");
242         for (final FragmentEntity fragmentEntity : fragmentEntities) {
243             final Matcher matcher = pattern.matcher(fragmentEntity.getXpath());
244             if (matcher.matches()) {
245                 ancestorXpath.add(matcher.group(1));
246             }
247         }
248         return ancestorXpath;
249     }
250
251     private DataNode toDataNode(final FragmentEntity fragmentEntity,
252                                 final FetchDescendantsOption fetchDescendantsOption) {
253         final List<DataNode> childDataNodes = getChildDataNodes(fragmentEntity, fetchDescendantsOption);
254         Map<String, Object> leaves = new HashMap<>();
255         if (fragmentEntity.getAttributes() != null) {
256             leaves = jsonObjectMapper.convertJsonString(fragmentEntity.getAttributes(), Map.class);
257         }
258         return new DataNodeBuilder()
259                 .withModuleNamePrefix(getFirstModuleName(fragmentEntity))
260                 .withXpath(fragmentEntity.getXpath())
261                 .withLeaves(leaves)
262                 .withChildDataNodes(childDataNodes).build();
263     }
264
265     private String getFirstModuleName(final FragmentEntity fragmentEntity) {
266         final SchemaSetEntity schemaSetEntity = fragmentEntity.getAnchor().getSchemaSet();
267         final Map<String, String> yangResourceNameToContent =
268                 schemaSetEntity.getYangResources().stream().collect(
269                         Collectors.toMap(YangResourceEntity::getFileName, YangResourceEntity::getContent));
270         final SchemaContext schemaContext = YangTextSchemaSourceSetBuilder.of(yangResourceNameToContent)
271                 .getSchemaContext();
272         return schemaContext.getModules().iterator().next().getName();
273     }
274
275     private List<DataNode> getChildDataNodes(final FragmentEntity fragmentEntity,
276                                              final FetchDescendantsOption fetchDescendantsOption) {
277         if (fetchDescendantsOption == INCLUDE_ALL_DESCENDANTS) {
278             return fragmentEntity.getChildFragments().stream()
279                     .map(childFragmentEntity -> toDataNode(childFragmentEntity, fetchDescendantsOption))
280                     .collect(Collectors.toUnmodifiableList());
281         }
282         return Collections.emptyList();
283     }
284
285     @Override
286     public void updateDataLeaves(final String dataspaceName, final String anchorName, final String xpath,
287                                  final Map<String, Object> leaves) {
288         final FragmentEntity fragmentEntity = getFragmentByXpath(dataspaceName, anchorName, xpath);
289         fragmentEntity.setAttributes(jsonObjectMapper.asJsonString(leaves));
290         fragmentRepository.save(fragmentEntity);
291     }
292
293     @Override
294     public void updateDataNodeAndDescendants(final String dataspaceName, final String anchorName,
295                                              final DataNode dataNode) {
296         final FragmentEntity fragmentEntity = getFragmentByXpath(dataspaceName, anchorName, dataNode.getXpath());
297         updateFragmentEntityAndDescendantsWithDataNode(fragmentEntity, dataNode);
298         try {
299             fragmentRepository.save(fragmentEntity);
300         } catch (final StaleStateException staleStateException) {
301             throw new ConcurrencyException("Concurrent Transactions",
302                     String.format("dataspace :'%s', Anchor : '%s' and xpath: '%s' is updated by another transaction.",
303                             dataspaceName, anchorName, dataNode.getXpath()),
304                     staleStateException);
305         }
306     }
307
308     @Override
309     public void updateDataNodesAndDescendants(final String dataspaceName,
310                                               final String anchorName,
311                                               final List<DataNode> dataNodes) {
312         final Map<DataNode, FragmentEntity> dataNodeFragmentEntityMap = dataNodes.stream()
313             .collect(Collectors.toMap(
314                 dataNode -> dataNode, dataNode -> getFragmentByXpath(dataspaceName, anchorName, dataNode.getXpath())));
315         dataNodeFragmentEntityMap.forEach(
316             (dataNode, fragmentEntity) -> updateFragmentEntityAndDescendantsWithDataNode(fragmentEntity, dataNode));
317         try {
318             fragmentRepository.saveAll(dataNodeFragmentEntityMap.values());
319         } catch (final StaleStateException staleStateException) {
320             throw new ConcurrencyException("Concurrent Transactions",
321                 String.format("A data node in dataspace :'%s' with Anchor : '%s' is updated by another transaction.",
322                     dataspaceName, anchorName),
323                 staleStateException);
324         }
325     }
326
327     private void updateFragmentEntityAndDescendantsWithDataNode(final FragmentEntity existingFragmentEntity,
328                                                                 final DataNode newDataNode) {
329
330         existingFragmentEntity.setAttributes(jsonObjectMapper.asJsonString(newDataNode.getLeaves()));
331
332         final Map<String, FragmentEntity> existingChildrenByXpath = existingFragmentEntity.getChildFragments().stream()
333                 .collect(Collectors.toMap(FragmentEntity::getXpath, childFragmentEntity -> childFragmentEntity));
334
335         final Collection<FragmentEntity> updatedChildFragments = new HashSet<>();
336
337         for (final DataNode newDataNodeChild : newDataNode.getChildDataNodes()) {
338             final FragmentEntity childFragment;
339             if (isNewDataNode(newDataNodeChild, existingChildrenByXpath)) {
340                 childFragment = convertToFragmentWithAllDescendants(
341                         existingFragmentEntity.getDataspace(), existingFragmentEntity.getAnchor(), newDataNodeChild);
342             } else {
343                 childFragment = existingChildrenByXpath.get(newDataNodeChild.getXpath());
344                 updateFragmentEntityAndDescendantsWithDataNode(childFragment, newDataNodeChild);
345             }
346             updatedChildFragments.add(childFragment);
347         }
348
349         existingFragmentEntity.getChildFragments().clear();
350         existingFragmentEntity.getChildFragments().addAll(updatedChildFragments);
351     }
352
353     @Override
354     @Transactional
355     public void replaceListContent(final String dataspaceName, final String anchorName, final String parentNodeXpath,
356                                    final Collection<DataNode> newListElements) {
357         final FragmentEntity parentEntity = getFragmentByXpath(dataspaceName, anchorName, parentNodeXpath);
358         final String listElementXpathPrefix = getListElementXpathPrefix(newListElements);
359         final Map<String, FragmentEntity> existingListElementFragmentEntitiesByXPath =
360                 extractListElementFragmentEntitiesByXPath(parentEntity.getChildFragments(), listElementXpathPrefix);
361         deleteListElements(parentEntity.getChildFragments(), existingListElementFragmentEntitiesByXPath);
362         final Set<FragmentEntity> updatedChildFragmentEntities = new HashSet<>();
363         for (final DataNode newListElement : newListElements) {
364             final FragmentEntity existingListElementEntity =
365                     existingListElementFragmentEntitiesByXPath.get(newListElement.getXpath());
366             final FragmentEntity entityToBeAdded = getFragmentForReplacement(parentEntity, newListElement,
367                     existingListElementEntity);
368
369             updatedChildFragmentEntities.add(entityToBeAdded);
370         }
371         parentEntity.getChildFragments().addAll(updatedChildFragmentEntities);
372         fragmentRepository.save(parentEntity);
373     }
374
375     @Override
376     @Transactional
377     public void deleteDataNodes(final String dataspaceName, final String anchorName) {
378         final DataspaceEntity dataspaceEntity = dataspaceRepository.getByName(dataspaceName);
379         anchorRepository.findByDataspaceAndName(dataspaceEntity, anchorName)
380                 .ifPresent(
381                         anchorEntity -> fragmentRepository.deleteByAnchorIn(Set.of(anchorEntity)));
382     }
383
384     @Override
385     @Transactional
386     public void deleteListDataNode(final String dataspaceName, final String anchorName,
387                                    final String targetXpath) {
388         deleteDataNode(dataspaceName, anchorName, targetXpath, true);
389     }
390
391     @Override
392     @Transactional
393     public void deleteDataNode(final String dataspaceName, final String anchorName, final String targetXpath) {
394         deleteDataNode(dataspaceName, anchorName, targetXpath, false);
395     }
396
397     private void deleteDataNode(final String dataspaceName, final String anchorName, final String targetXpath,
398                                 final boolean onlySupportListNodeDeletion) {
399         final String parentNodeXpath;
400         FragmentEntity parentFragmentEntity = null;
401         boolean targetDeleted = false;
402         if (isRootXpath(targetXpath)) {
403             deleteDataNodes(dataspaceName, anchorName);
404             targetDeleted = true;
405         } else {
406             if (isRootContainerNodeXpath(targetXpath)) {
407                 parentNodeXpath = targetXpath;
408             } else {
409                 parentNodeXpath = targetXpath.substring(0, targetXpath.lastIndexOf('/'));
410             }
411             parentFragmentEntity = getFragmentByXpath(dataspaceName, anchorName, parentNodeXpath);
412             final String lastXpathElement = targetXpath.substring(targetXpath.lastIndexOf('/'));
413             final boolean isListElement = REG_EX_PATTERN_FOR_LIST_ELEMENT_KEY_PREDICATE
414                     .matcher(lastXpathElement).find();
415             if (isListElement) {
416                 targetDeleted = deleteDataNode(parentFragmentEntity, targetXpath);
417             } else {
418                 targetDeleted = deleteAllListElements(parentFragmentEntity, targetXpath);
419                 final boolean tryToDeleteDataNode = !targetDeleted && !onlySupportListNodeDeletion;
420                 if (tryToDeleteDataNode) {
421                     targetDeleted = deleteDataNode(parentFragmentEntity, targetXpath);
422                 }
423             }
424         }
425         if (!targetDeleted) {
426             final String additionalInformation = onlySupportListNodeDeletion
427                     ? "The target is probably not a List." : "";
428             throw new DataNodeNotFoundException(parentFragmentEntity.getDataspace().getName(),
429                     parentFragmentEntity.getAnchor().getName(), targetXpath, additionalInformation);
430         }
431     }
432
433     private boolean deleteDataNode(final FragmentEntity parentFragmentEntity, final String targetXpath) {
434         final String normalizedTargetXpath = CpsPathUtil.getNormalizedXpath(targetXpath);
435         if (parentFragmentEntity.getXpath().equals(normalizedTargetXpath)) {
436             fragmentRepository.delete(parentFragmentEntity);
437             return true;
438         }
439         if (parentFragmentEntity.getChildFragments()
440                 .removeIf(fragment -> fragment.getXpath().equals(normalizedTargetXpath))) {
441             fragmentRepository.save(parentFragmentEntity);
442             return true;
443         }
444         return false;
445     }
446
447     private boolean deleteAllListElements(final FragmentEntity parentFragmentEntity, final String listXpath) {
448         final String normalizedListXpath = CpsPathUtil.getNormalizedXpath(listXpath);
449         final String deleteTargetXpathPrefix = normalizedListXpath + "[";
450         if (parentFragmentEntity.getChildFragments()
451                 .removeIf(fragment -> fragment.getXpath().startsWith(deleteTargetXpathPrefix))) {
452             fragmentRepository.save(parentFragmentEntity);
453             return true;
454         }
455         return false;
456     }
457
458     private static void deleteListElements(
459             final Collection<FragmentEntity> fragmentEntities,
460             final Map<String, FragmentEntity> existingListElementFragmentEntitiesByXPath) {
461         fragmentEntities.removeAll(existingListElementFragmentEntitiesByXPath.values());
462     }
463
464     private static String getListElementXpathPrefix(final Collection<DataNode> newListElements) {
465         if (newListElements.isEmpty()) {
466             throw new CpsAdminException("Invalid list replacement",
467                     "Cannot replace list elements with empty collection");
468         }
469         final String firstChildNodeXpath = newListElements.iterator().next().getXpath();
470         return firstChildNodeXpath.substring(0, firstChildNodeXpath.lastIndexOf('[') + 1);
471     }
472
473     private FragmentEntity getFragmentForReplacement(final FragmentEntity parentEntity,
474                                                      final DataNode newListElement,
475                                                      final FragmentEntity existingListElementEntity) {
476         if (existingListElementEntity == null) {
477             return convertToFragmentWithAllDescendants(
478                     parentEntity.getDataspace(), parentEntity.getAnchor(), newListElement);
479         }
480         if (newListElement.getChildDataNodes().isEmpty()) {
481             copyAttributesFromNewListElement(existingListElementEntity, newListElement);
482             existingListElementEntity.getChildFragments().clear();
483         } else {
484             updateFragmentEntityAndDescendantsWithDataNode(existingListElementEntity, newListElement);
485         }
486         return existingListElementEntity;
487     }
488
489     private static boolean isNewDataNode(final DataNode replacementDataNode,
490                                          final Map<String, FragmentEntity> existingListElementsByXpath) {
491         return !existingListElementsByXpath.containsKey(replacementDataNode.getXpath());
492     }
493
494     private static boolean isRootContainerNodeXpath(final String xpath) {
495         return 0 == xpath.lastIndexOf('/');
496     }
497
498     private void copyAttributesFromNewListElement(final FragmentEntity existingListElementEntity,
499                                                   final DataNode newListElement) {
500         final FragmentEntity replacementFragmentEntity =
501                 FragmentEntity.builder().attributes(jsonObjectMapper.asJsonString(
502                         newListElement.getLeaves())).build();
503         existingListElementEntity.setAttributes(replacementFragmentEntity.getAttributes());
504     }
505
506     private static Map<String, FragmentEntity> extractListElementFragmentEntitiesByXPath(
507             final Set<FragmentEntity> childEntities, final String listElementXpathPrefix) {
508         return childEntities.stream()
509                 .filter(fragmentEntity -> fragmentEntity.getXpath().startsWith(listElementXpathPrefix))
510                 .collect(Collectors.toMap(FragmentEntity::getXpath, fragmentEntity -> fragmentEntity));
511     }
512
513     private static boolean isRootXpath(final String xpath) {
514         return "/".equals(xpath) || "".equals(xpath);
515     }
516 }