Merge "Added API to get all schema sets for a given dataspace."
[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 com.google.common.collect.ImmutableSet;
26 import com.google.common.collect.ImmutableSet.Builder;
27 import java.util.ArrayList;
28 import java.util.Collection;
29 import java.util.Collections;
30 import java.util.HashMap;
31 import java.util.HashSet;
32 import java.util.List;
33 import java.util.Map;
34 import java.util.Set;
35 import java.util.regex.Matcher;
36 import java.util.regex.Pattern;
37 import java.util.stream.Collectors;
38 import javax.transaction.Transactional;
39 import lombok.RequiredArgsConstructor;
40 import lombok.extern.slf4j.Slf4j;
41 import org.hibernate.StaleStateException;
42 import org.onap.cps.cpspath.parser.CpsPathQuery;
43 import org.onap.cps.cpspath.parser.CpsPathUtil;
44 import org.onap.cps.cpspath.parser.PathParsingException;
45 import org.onap.cps.spi.CpsDataPersistenceService;
46 import org.onap.cps.spi.FetchDescendantsOption;
47 import org.onap.cps.spi.entities.AnchorEntity;
48 import org.onap.cps.spi.entities.DataspaceEntity;
49 import org.onap.cps.spi.entities.FragmentEntity;
50 import org.onap.cps.spi.entities.FragmentEntityArranger;
51 import org.onap.cps.spi.entities.FragmentExtract;
52 import org.onap.cps.spi.exceptions.AlreadyDefinedException;
53 import org.onap.cps.spi.exceptions.AlreadyDefinedExceptionBatch;
54 import org.onap.cps.spi.exceptions.ConcurrencyException;
55 import org.onap.cps.spi.exceptions.CpsAdminException;
56 import org.onap.cps.spi.exceptions.CpsPathException;
57 import org.onap.cps.spi.exceptions.DataNodeNotFoundException;
58 import org.onap.cps.spi.model.DataNode;
59 import org.onap.cps.spi.model.DataNodeBuilder;
60 import org.onap.cps.spi.repository.AnchorRepository;
61 import org.onap.cps.spi.repository.DataspaceRepository;
62 import org.onap.cps.spi.repository.FragmentRepository;
63 import org.onap.cps.spi.utils.SessionManager;
64 import org.onap.cps.utils.JsonObjectMapper;
65 import org.springframework.dao.DataIntegrityViolationException;
66 import org.springframework.stereotype.Service;
67
68 @Service
69 @Slf4j
70 @RequiredArgsConstructor
71 public class CpsDataPersistenceServiceImpl implements CpsDataPersistenceService {
72
73     private final DataspaceRepository dataspaceRepository;
74     private final AnchorRepository anchorRepository;
75     private final FragmentRepository fragmentRepository;
76     private final JsonObjectMapper jsonObjectMapper;
77     private final SessionManager sessionManager;
78
79     private static final String REG_EX_FOR_OPTIONAL_LIST_INDEX = "(\\[@[\\s\\S]+?]){0,1})";
80     private static final Pattern REG_EX_PATTERN_FOR_LIST_ELEMENT_KEY_PREDICATE =
81             Pattern.compile("\\[(\\@([^\\/]{0,9999}))\\]$");
82     private static final String TOP_LEVEL_MODULE_PREFIX_PROPERTY_NAME = "topLevelModulePrefix";
83
84     @Override
85     public void addChildDataNode(final String dataspaceName, final String anchorName, final String parentNodeXpath,
86                                  final DataNode newChildDataNode) {
87         addNewChildDataNode(dataspaceName, anchorName, parentNodeXpath, newChildDataNode);
88     }
89
90     @Override
91     public void addListElements(final String dataspaceName, final String anchorName, final String parentNodeXpath,
92                                 final Collection<DataNode> newListElements) {
93         addChildrenDataNodes(dataspaceName, anchorName, parentNodeXpath, newListElements);
94     }
95
96     @Override
97     public void addMultipleLists(final String dataspaceName, final String anchorName, final String parentNodeXpath,
98                                  final Collection<Collection<DataNode>> newLists) {
99         final Collection<String> failedXpaths = new HashSet<>();
100         newLists.forEach(newList -> {
101             try {
102                 addChildrenDataNodes(dataspaceName, anchorName, parentNodeXpath, newList);
103             } catch (final AlreadyDefinedExceptionBatch e) {
104                 failedXpaths.addAll(e.getAlreadyDefinedXpaths());
105             }
106         });
107
108         if (!failedXpaths.isEmpty()) {
109             throw new AlreadyDefinedExceptionBatch(failedXpaths);
110         }
111
112     }
113
114     private void addNewChildDataNode(final String dataspaceName, final String anchorName,
115                                      final String parentNodeXpath, final DataNode newChild) {
116         final FragmentEntity parentFragmentEntity =
117             getFragmentWithoutDescendantsByXpath(dataspaceName, anchorName, parentNodeXpath);
118         final FragmentEntity newChildAsFragmentEntity =
119                 convertToFragmentWithAllDescendants(parentFragmentEntity.getDataspace(),
120                         parentFragmentEntity.getAnchor(), newChild);
121         newChildAsFragmentEntity.setParentId(parentFragmentEntity.getId());
122         try {
123             fragmentRepository.save(newChildAsFragmentEntity);
124         } catch (final DataIntegrityViolationException e) {
125             throw AlreadyDefinedException.forDataNode(newChild.getXpath(), anchorName, e);
126         }
127
128     }
129
130     private void addChildrenDataNodes(final String dataspaceName, final String anchorName, final String parentNodeXpath,
131                                       final Collection<DataNode> newChildren) {
132         final FragmentEntity parentFragmentEntity =
133             getFragmentWithoutDescendantsByXpath(dataspaceName, anchorName, parentNodeXpath);
134         final List<FragmentEntity> fragmentEntities = new ArrayList<>(newChildren.size());
135         try {
136             newChildren.forEach(newChildAsDataNode -> {
137                 final FragmentEntity newChildAsFragmentEntity =
138                         convertToFragmentWithAllDescendants(parentFragmentEntity.getDataspace(),
139                                 parentFragmentEntity.getAnchor(), newChildAsDataNode);
140                 newChildAsFragmentEntity.setParentId(parentFragmentEntity.getId());
141                 fragmentEntities.add(newChildAsFragmentEntity);
142             });
143             fragmentRepository.saveAll(fragmentEntities);
144         } catch (final DataIntegrityViolationException e) {
145             log.warn("Exception occurred : {} , While saving : {} children, retrying using individual save operations",
146                     e, fragmentEntities.size());
147             retrySavingEachChildIndividually(dataspaceName, anchorName, parentNodeXpath, newChildren);
148         }
149     }
150
151     private void retrySavingEachChildIndividually(final String dataspaceName, final String anchorName,
152                                                   final String parentNodeXpath,
153                                                   final Collection<DataNode> newChildren) {
154         final Collection<String> failedXpaths = new HashSet<>();
155         for (final DataNode newChild : newChildren) {
156             try {
157                 addNewChildDataNode(dataspaceName, anchorName, parentNodeXpath, newChild);
158             } catch (final AlreadyDefinedException e) {
159                 failedXpaths.add(newChild.getXpath());
160             }
161         }
162         if (!failedXpaths.isEmpty()) {
163             throw new AlreadyDefinedExceptionBatch(failedXpaths);
164         }
165     }
166
167     @Override
168     public void storeDataNode(final String dataspaceName, final String anchorName, final DataNode dataNode) {
169         final DataspaceEntity dataspaceEntity = dataspaceRepository.getByName(dataspaceName);
170         final AnchorEntity anchorEntity = anchorRepository.getByDataspaceAndName(dataspaceEntity, anchorName);
171         final FragmentEntity fragmentEntity = convertToFragmentWithAllDescendants(dataspaceEntity, anchorEntity,
172                 dataNode);
173         try {
174             fragmentRepository.save(fragmentEntity);
175         } catch (final DataIntegrityViolationException exception) {
176             throw AlreadyDefinedException.forDataNode(dataNode.getXpath(), anchorName, exception);
177         }
178     }
179
180     /**
181      * Convert DataNode object into Fragment and places the result in the fragments placeholder. Performs same action
182      * for all DataNode children recursively.
183      *
184      * @param dataspaceEntity       dataspace
185      * @param anchorEntity          anchorEntity
186      * @param dataNodeToBeConverted dataNode
187      * @return a Fragment built from current DataNode
188      */
189     private FragmentEntity convertToFragmentWithAllDescendants(final DataspaceEntity dataspaceEntity,
190                                                                final AnchorEntity anchorEntity,
191                                                                final DataNode dataNodeToBeConverted) {
192         final FragmentEntity parentFragment = toFragmentEntity(dataspaceEntity, anchorEntity, dataNodeToBeConverted);
193         final Builder<FragmentEntity> childFragmentsImmutableSetBuilder = ImmutableSet.builder();
194         for (final DataNode childDataNode : dataNodeToBeConverted.getChildDataNodes()) {
195             final FragmentEntity childFragment =
196                     convertToFragmentWithAllDescendants(parentFragment.getDataspace(), parentFragment.getAnchor(),
197                             childDataNode);
198             childFragmentsImmutableSetBuilder.add(childFragment);
199         }
200         parentFragment.setChildFragments(childFragmentsImmutableSetBuilder.build());
201         return parentFragment;
202     }
203
204     private FragmentEntity toFragmentEntity(final DataspaceEntity dataspaceEntity,
205                                             final AnchorEntity anchorEntity, final DataNode dataNode) {
206         return FragmentEntity.builder()
207                 .dataspace(dataspaceEntity)
208                 .anchor(anchorEntity)
209                 .xpath(dataNode.getXpath())
210                 .attributes(jsonObjectMapper.asJsonString(dataNode.getLeaves()))
211                 .build();
212     }
213
214     @Override
215     public DataNode getDataNode(final String dataspaceName, final String anchorName, final String xpath,
216                                 final FetchDescendantsOption fetchDescendantsOption) {
217         final FragmentEntity fragmentEntity = getFragmentByXpath(dataspaceName, anchorName, xpath,
218             fetchDescendantsOption);
219         return toDataNode(fragmentEntity, fetchDescendantsOption);
220     }
221
222     private FragmentEntity getFragmentWithoutDescendantsByXpath(final String dataspaceName,
223                                                                 final String anchorName,
224                                                                 final String xpath) {
225         return getFragmentByXpath(dataspaceName, anchorName, xpath, FetchDescendantsOption.OMIT_DESCENDANTS);
226     }
227
228     private FragmentEntity getFragmentByXpath(final String dataspaceName, final String anchorName,
229                                               final String xpath, final FetchDescendantsOption fetchDescendantsOption) {
230         final DataspaceEntity dataspaceEntity = dataspaceRepository.getByName(dataspaceName);
231         final AnchorEntity anchorEntity = anchorRepository.getByDataspaceAndName(dataspaceEntity, anchorName);
232         if (isRootXpath(xpath)) {
233             final List<FragmentExtract> fragmentExtracts = fragmentRepository.getTopLevelFragments(dataspaceEntity,
234                     anchorEntity);
235             return FragmentEntityArranger.toFragmentEntityTree(anchorEntity,
236                     fragmentExtracts);
237         } else {
238             final String normalizedXpath = getNormalizedXpath(xpath);
239             final FragmentEntity fragmentEntity;
240             if (FetchDescendantsOption.OMIT_DESCENDANTS.equals(fetchDescendantsOption)) {
241                 fragmentEntity =
242                     fragmentRepository.getByDataspaceAndAnchorAndXpath(dataspaceEntity, anchorEntity, normalizedXpath);
243             } else {
244                 fragmentEntity = buildFragmentEntityFromFragmentExtracts(anchorEntity, normalizedXpath);
245             }
246             if (fragmentEntity == null) {
247                 throw new DataNodeNotFoundException(dataspaceEntity.getName(), anchorEntity.getName(), xpath);
248             }
249             return fragmentEntity;
250         }
251     }
252
253     private FragmentEntity buildFragmentEntityFromFragmentExtracts(final AnchorEntity anchorEntity,
254                                                                    final String normalizedXpath) {
255         final FragmentEntity fragmentEntity;
256         final List<FragmentExtract> fragmentExtracts =
257                 fragmentRepository.findByAnchorIdAndParentXpath(anchorEntity.getId(), normalizedXpath);
258         log.debug("Fetched {} fragment entities by anchor {} and cps path {}.",
259                 fragmentExtracts.size(), anchorEntity.getName(), normalizedXpath);
260         fragmentEntity = FragmentEntityArranger.toFragmentEntityTree(anchorEntity, fragmentExtracts);
261         return fragmentEntity;
262     }
263
264     @Override
265     public List<DataNode> queryDataNodes(final String dataspaceName, final String anchorName, final String cpsPath,
266                                          final FetchDescendantsOption fetchDescendantsOption) {
267         final DataspaceEntity dataspaceEntity = dataspaceRepository.getByName(dataspaceName);
268         final AnchorEntity anchorEntity = anchorRepository.getByDataspaceAndName(dataspaceEntity, anchorName);
269         final CpsPathQuery cpsPathQuery;
270         try {
271             cpsPathQuery = CpsPathUtil.getCpsPathQuery(cpsPath);
272         } catch (final PathParsingException e) {
273             throw new CpsPathException(e.getMessage());
274         }
275         List<FragmentEntity> fragmentEntities =
276                 fragmentRepository.findByAnchorAndCpsPath(anchorEntity.getId(), cpsPathQuery);
277         if (cpsPathQuery.hasAncestorAxis()) {
278             final Set<String> ancestorXpaths = processAncestorXpath(fragmentEntities, cpsPathQuery);
279             fragmentEntities = ancestorXpaths.isEmpty() ? Collections.emptyList()
280                     : fragmentRepository.findAllByAnchorAndXpathIn(anchorEntity, ancestorXpaths);
281         }
282         return createDataNodesFromFragmentEntities(fetchDescendantsOption, anchorEntity,
283                 fragmentEntities);
284     }
285
286     private List<DataNode> createDataNodesFromFragmentEntities(final FetchDescendantsOption fetchDescendantsOption,
287                                                                final AnchorEntity anchorEntity,
288                                                                final List<FragmentEntity> fragmentEntities) {
289         final List<DataNode> dataNodes = new ArrayList<>(fragmentEntities.size());
290         for (final FragmentEntity proxiedFragmentEntity : fragmentEntities) {
291             final DataNode dataNode;
292             if (FetchDescendantsOption.OMIT_DESCENDANTS.equals(fetchDescendantsOption)) {
293                 dataNode = toDataNode(proxiedFragmentEntity, fetchDescendantsOption);
294             } else {
295                 final String normalizedXpath = getNormalizedXpath(proxiedFragmentEntity.getXpath());
296                 final FragmentEntity unproxiedFragmentEntity = buildFragmentEntityFromFragmentExtracts(anchorEntity,
297                         normalizedXpath);
298                 dataNode = toDataNode(unproxiedFragmentEntity, fetchDescendantsOption);
299             }
300             dataNodes.add(dataNode);
301         }
302         return Collections.unmodifiableList(dataNodes);
303     }
304
305     private static String getNormalizedXpath(final String xpathSource) {
306         final String normalizedXpath;
307         try {
308             normalizedXpath = CpsPathUtil.getNormalizedXpath(xpathSource);
309         } catch (final PathParsingException e) {
310             throw new CpsPathException(e.getMessage());
311         }
312         return normalizedXpath;
313     }
314
315     @Override
316     public String startSession() {
317         return sessionManager.startSession();
318     }
319
320     @Override
321     public void closeSession(final String sessionId) {
322         sessionManager.closeSession(sessionId, SessionManager.WITH_COMMIT);
323     }
324
325     @Override
326     public void lockAnchor(final String sessionId, final String dataspaceName,
327                            final String anchorName, final Long timeoutInMilliseconds) {
328         sessionManager.lockAnchor(sessionId, dataspaceName, anchorName, timeoutInMilliseconds);
329     }
330
331     private static Set<String> processAncestorXpath(final List<FragmentEntity> fragmentEntities,
332                                                     final CpsPathQuery cpsPathQuery) {
333         final Set<String> ancestorXpath = new HashSet<>();
334         final Pattern pattern =
335                 Pattern.compile("([\\s\\S]*\\/" + Pattern.quote(cpsPathQuery.getAncestorSchemaNodeIdentifier())
336                         + REG_EX_FOR_OPTIONAL_LIST_INDEX + "\\/[\\s\\S]*");
337         for (final FragmentEntity fragmentEntity : fragmentEntities) {
338             final Matcher matcher = pattern.matcher(fragmentEntity.getXpath());
339             if (matcher.matches()) {
340                 ancestorXpath.add(matcher.group(1));
341             }
342         }
343         return ancestorXpath;
344     }
345
346     private DataNode toDataNode(final FragmentEntity fragmentEntity,
347                                 final FetchDescendantsOption fetchDescendantsOption) {
348         final List<DataNode> childDataNodes = getChildDataNodes(fragmentEntity, fetchDescendantsOption);
349         Map<String, Object> leaves = new HashMap<>();
350         if (fragmentEntity.getAttributes() != null) {
351             leaves = jsonObjectMapper.convertJsonString(fragmentEntity.getAttributes(), Map.class);
352         }
353         return new DataNodeBuilder()
354                 .withXpath(fragmentEntity.getXpath())
355                 .withLeaves(leaves)
356                 .withChildDataNodes(childDataNodes).build();
357     }
358
359     private List<DataNode> getChildDataNodes(final FragmentEntity fragmentEntity,
360                                              final FetchDescendantsOption fetchDescendantsOption) {
361         if (fetchDescendantsOption.hasNext()) {
362             return fragmentEntity.getChildFragments().stream()
363                     .map(childFragmentEntity -> toDataNode(childFragmentEntity, fetchDescendantsOption.next()))
364                     .collect(Collectors.toList());
365         }
366         return Collections.emptyList();
367     }
368
369     @Override
370     public void updateDataLeaves(final String dataspaceName, final String anchorName, final String xpath,
371                                  final Map<String, Object> leaves) {
372         final FragmentEntity fragmentEntity = getFragmentWithoutDescendantsByXpath(dataspaceName, anchorName, xpath);
373         fragmentEntity.setAttributes(jsonObjectMapper.asJsonString(leaves));
374         fragmentRepository.save(fragmentEntity);
375     }
376
377     @Override
378     public void updateDataNodeAndDescendants(final String dataspaceName, final String anchorName,
379                                              final DataNode dataNode) {
380         final FragmentEntity fragmentEntity =
381             getFragmentWithoutDescendantsByXpath(dataspaceName, anchorName, dataNode.getXpath());
382         updateFragmentEntityAndDescendantsWithDataNode(fragmentEntity, dataNode);
383         try {
384             fragmentRepository.save(fragmentEntity);
385         } catch (final StaleStateException staleStateException) {
386             throw new ConcurrencyException("Concurrent Transactions",
387                     String.format("dataspace :'%s', Anchor : '%s' and xpath: '%s' is updated by another transaction.",
388                             dataspaceName, anchorName, dataNode.getXpath()));
389         }
390     }
391
392     @Override
393     public void updateDataNodesAndDescendants(final String dataspaceName,
394                                               final String anchorName,
395                                               final List<DataNode> dataNodes) {
396
397         final Map<DataNode, FragmentEntity> dataNodeFragmentEntityMap = dataNodes.stream()
398                 .collect(Collectors.toMap(
399                         dataNode -> dataNode,
400                         dataNode ->
401                             getFragmentWithoutDescendantsByXpath(dataspaceName, anchorName, dataNode.getXpath())));
402         dataNodeFragmentEntityMap.forEach(
403                 (dataNode, fragmentEntity) -> updateFragmentEntityAndDescendantsWithDataNode(fragmentEntity, dataNode));
404         try {
405             fragmentRepository.saveAll(dataNodeFragmentEntityMap.values());
406         } catch (final StaleStateException staleStateException) {
407             retryUpdateDataNodesIndividually(dataspaceName, anchorName, dataNodeFragmentEntityMap.values());
408         }
409     }
410
411     private void retryUpdateDataNodesIndividually(final String dataspaceName, final String anchorName,
412                                                   final Collection<FragmentEntity> fragmentEntities) {
413         final Collection<String> failedXpaths = new HashSet<>();
414
415         fragmentEntities.forEach(dataNodeFragment -> {
416             try {
417                 fragmentRepository.save(dataNodeFragment);
418             } catch (final StaleStateException e) {
419                 failedXpaths.add(dataNodeFragment.getXpath());
420             }
421         });
422
423         if (!failedXpaths.isEmpty()) {
424             final String failedXpathsConcatenated = String.join(",", failedXpaths);
425             throw new ConcurrencyException("Concurrent Transactions", String.format(
426                     "DataNodes : %s in Dataspace :'%s' with Anchor : '%s'  are updated by another transaction.",
427                     failedXpathsConcatenated, dataspaceName, anchorName));
428         }
429     }
430
431     private void updateFragmentEntityAndDescendantsWithDataNode(final FragmentEntity existingFragmentEntity,
432                                                                 final DataNode newDataNode) {
433
434         existingFragmentEntity.setAttributes(jsonObjectMapper.asJsonString(newDataNode.getLeaves()));
435
436         final Map<String, FragmentEntity> existingChildrenByXpath = existingFragmentEntity.getChildFragments().stream()
437                 .collect(Collectors.toMap(FragmentEntity::getXpath, childFragmentEntity -> childFragmentEntity));
438
439         final Collection<FragmentEntity> updatedChildFragments = new HashSet<>();
440
441         for (final DataNode newDataNodeChild : newDataNode.getChildDataNodes()) {
442             final FragmentEntity childFragment;
443             if (isNewDataNode(newDataNodeChild, existingChildrenByXpath)) {
444                 childFragment = convertToFragmentWithAllDescendants(
445                         existingFragmentEntity.getDataspace(), existingFragmentEntity.getAnchor(), newDataNodeChild);
446             } else {
447                 childFragment = existingChildrenByXpath.get(newDataNodeChild.getXpath());
448                 updateFragmentEntityAndDescendantsWithDataNode(childFragment, newDataNodeChild);
449             }
450             updatedChildFragments.add(childFragment);
451         }
452
453         existingFragmentEntity.getChildFragments().clear();
454         existingFragmentEntity.getChildFragments().addAll(updatedChildFragments);
455     }
456
457     @Override
458     @Transactional
459     public void replaceListContent(final String dataspaceName, final String anchorName, final String parentNodeXpath,
460                                    final Collection<DataNode> newListElements) {
461         final FragmentEntity parentEntity =
462             getFragmentWithoutDescendantsByXpath(dataspaceName, anchorName, parentNodeXpath);
463         final String listElementXpathPrefix = getListElementXpathPrefix(newListElements);
464         final Map<String, FragmentEntity> existingListElementFragmentEntitiesByXPath =
465                 extractListElementFragmentEntitiesByXPath(parentEntity.getChildFragments(), listElementXpathPrefix);
466         deleteListElements(parentEntity.getChildFragments(), existingListElementFragmentEntitiesByXPath);
467         final Set<FragmentEntity> updatedChildFragmentEntities = new HashSet<>();
468         for (final DataNode newListElement : newListElements) {
469             final FragmentEntity existingListElementEntity =
470                     existingListElementFragmentEntitiesByXPath.get(newListElement.getXpath());
471             final FragmentEntity entityToBeAdded = getFragmentForReplacement(parentEntity, newListElement,
472                     existingListElementEntity);
473
474             updatedChildFragmentEntities.add(entityToBeAdded);
475         }
476         parentEntity.getChildFragments().addAll(updatedChildFragmentEntities);
477         fragmentRepository.save(parentEntity);
478     }
479
480     @Override
481     @Transactional
482     public void deleteDataNodes(final String dataspaceName, final String anchorName) {
483         final DataspaceEntity dataspaceEntity = dataspaceRepository.getByName(dataspaceName);
484         anchorRepository.findByDataspaceAndName(dataspaceEntity, anchorName)
485                 .ifPresent(
486                         anchorEntity -> fragmentRepository.deleteByAnchorIn(Set.of(anchorEntity)));
487     }
488
489     @Override
490     @Transactional
491     public void deleteListDataNode(final String dataspaceName, final String anchorName,
492                                    final String targetXpath) {
493         deleteDataNode(dataspaceName, anchorName, targetXpath, true);
494     }
495
496     @Override
497     @Transactional
498     public void deleteDataNode(final String dataspaceName, final String anchorName, final String targetXpath) {
499         deleteDataNode(dataspaceName, anchorName, targetXpath, false);
500     }
501
502     private void deleteDataNode(final String dataspaceName, final String anchorName, final String targetXpath,
503                                 final boolean onlySupportListNodeDeletion) {
504         final String parentNodeXpath;
505         FragmentEntity parentFragmentEntity = null;
506         boolean targetDeleted;
507         if (isRootXpath(targetXpath)) {
508             deleteDataNodes(dataspaceName, anchorName);
509             targetDeleted = true;
510         } else {
511             if (isRootContainerNodeXpath(targetXpath)) {
512                 parentNodeXpath = targetXpath;
513             } else {
514                 parentNodeXpath = targetXpath.substring(0, targetXpath.lastIndexOf('/'));
515             }
516             parentFragmentEntity = getFragmentWithoutDescendantsByXpath(dataspaceName, anchorName, parentNodeXpath);
517             final String lastXpathElement = targetXpath.substring(targetXpath.lastIndexOf('/'));
518             final boolean isListElement = REG_EX_PATTERN_FOR_LIST_ELEMENT_KEY_PREDICATE
519                     .matcher(lastXpathElement).find();
520             if (isListElement) {
521                 targetDeleted = deleteDataNode(parentFragmentEntity, targetXpath);
522             } else {
523                 targetDeleted = deleteAllListElements(parentFragmentEntity, targetXpath);
524                 final boolean tryToDeleteDataNode = !targetDeleted && !onlySupportListNodeDeletion;
525                 if (tryToDeleteDataNode) {
526                     targetDeleted = deleteDataNode(parentFragmentEntity, targetXpath);
527                 }
528             }
529         }
530         if (!targetDeleted) {
531             final String additionalInformation = onlySupportListNodeDeletion
532                     ? "The target is probably not a List." : "";
533             throw new DataNodeNotFoundException(parentFragmentEntity.getDataspace().getName(),
534                     parentFragmentEntity.getAnchor().getName(), targetXpath, additionalInformation);
535         }
536     }
537
538     private boolean deleteDataNode(final FragmentEntity parentFragmentEntity, final String targetXpath) {
539         final String normalizedTargetXpath = CpsPathUtil.getNormalizedXpath(targetXpath);
540         if (parentFragmentEntity.getXpath().equals(normalizedTargetXpath)) {
541             fragmentRepository.delete(parentFragmentEntity);
542             return true;
543         }
544         if (parentFragmentEntity.getChildFragments()
545                 .removeIf(fragment -> fragment.getXpath().equals(normalizedTargetXpath))) {
546             fragmentRepository.save(parentFragmentEntity);
547             return true;
548         }
549         return false;
550     }
551
552     private boolean deleteAllListElements(final FragmentEntity parentFragmentEntity, final String listXpath) {
553         final String normalizedListXpath = CpsPathUtil.getNormalizedXpath(listXpath);
554         final String deleteTargetXpathPrefix = normalizedListXpath + "[";
555         if (parentFragmentEntity.getChildFragments()
556                 .removeIf(fragment -> fragment.getXpath().startsWith(deleteTargetXpathPrefix))) {
557             fragmentRepository.save(parentFragmentEntity);
558             return true;
559         }
560         return false;
561     }
562
563     private static void deleteListElements(
564             final Collection<FragmentEntity> fragmentEntities,
565             final Map<String, FragmentEntity> existingListElementFragmentEntitiesByXPath) {
566         fragmentEntities.removeAll(existingListElementFragmentEntitiesByXPath.values());
567     }
568
569     private static String getListElementXpathPrefix(final Collection<DataNode> newListElements) {
570         if (newListElements.isEmpty()) {
571             throw new CpsAdminException("Invalid list replacement",
572                     "Cannot replace list elements with empty collection");
573         }
574         final String firstChildNodeXpath = newListElements.iterator().next().getXpath();
575         return firstChildNodeXpath.substring(0, firstChildNodeXpath.lastIndexOf('[') + 1);
576     }
577
578     private FragmentEntity getFragmentForReplacement(final FragmentEntity parentEntity,
579                                                      final DataNode newListElement,
580                                                      final FragmentEntity existingListElementEntity) {
581         if (existingListElementEntity == null) {
582             return convertToFragmentWithAllDescendants(
583                     parentEntity.getDataspace(), parentEntity.getAnchor(), newListElement);
584         }
585         if (newListElement.getChildDataNodes().isEmpty()) {
586             copyAttributesFromNewListElement(existingListElementEntity, newListElement);
587             existingListElementEntity.getChildFragments().clear();
588         } else {
589             updateFragmentEntityAndDescendantsWithDataNode(existingListElementEntity, newListElement);
590         }
591         return existingListElementEntity;
592     }
593
594     private static boolean isNewDataNode(final DataNode replacementDataNode,
595                                          final Map<String, FragmentEntity> existingListElementsByXpath) {
596         return !existingListElementsByXpath.containsKey(replacementDataNode.getXpath());
597     }
598
599     private static boolean isRootContainerNodeXpath(final String xpath) {
600         return 0 == xpath.lastIndexOf('/');
601     }
602
603     private void copyAttributesFromNewListElement(final FragmentEntity existingListElementEntity,
604                                                   final DataNode newListElement) {
605         final FragmentEntity replacementFragmentEntity =
606                 FragmentEntity.builder().attributes(jsonObjectMapper.asJsonString(
607                         newListElement.getLeaves())).build();
608         existingListElementEntity.setAttributes(replacementFragmentEntity.getAttributes());
609     }
610
611     private static Map<String, FragmentEntity> extractListElementFragmentEntitiesByXPath(
612             final Set<FragmentEntity> childEntities, final String listElementXpathPrefix) {
613         return childEntities.stream()
614                 .filter(fragmentEntity -> fragmentEntity.getXpath().startsWith(listElementXpathPrefix))
615                 .collect(Collectors.toMap(FragmentEntity::getXpath, fragmentEntity -> fragmentEntity));
616     }
617
618     private static boolean isRootXpath(final String xpath) {
619         return "/".equals(xpath) || "".equals(xpath);
620     }
621 }