From 3724abc1912f93bf1caa104a55da7178f43fd731 Mon Sep 17 00:00:00 2001 From: lukegleeson Date: Tue, 27 Jul 2021 15:07:05 +0100 Subject: Delete list-node p1 service and persistence layers Persistence Layer of delete List-Node Content Service Layer of delete List-Node Content Updating of deprecated FragementRepository.getOne() method to FragmentRepository.getByID() in CpsDataPersistenceServiceIntegration.groovy Remove Runtime Exceptions thrown from CpsDataService.java javadoc Issue-ID: CPS-361 Signed-off-by: lukegleeson Change-Id: Ib5762e73a6e8620c50c2e07b00086b9287770bc3 --- .../spi/impl/CpsDataPersistenceServiceImpl.java | 30 +++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) (limited to 'cps-ri/src/main') diff --git a/cps-ri/src/main/java/org/onap/cps/spi/impl/CpsDataPersistenceServiceImpl.java b/cps-ri/src/main/java/org/onap/cps/spi/impl/CpsDataPersistenceServiceImpl.java index c638b9113..af010f4fc 100644 --- a/cps-ri/src/main/java/org/onap/cps/spi/impl/CpsDataPersistenceServiceImpl.java +++ b/cps-ri/src/main/java/org/onap/cps/spi/impl/CpsDataPersistenceServiceImpl.java @@ -34,6 +34,7 @@ import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Set; +import java.util.regex.Matcher; import java.util.regex.Pattern; import java.util.stream.Collectors; import javax.transaction.Transactional; @@ -48,6 +49,7 @@ import org.onap.cps.spi.entities.FragmentEntity; import org.onap.cps.spi.exceptions.AlreadyDefinedException; import org.onap.cps.spi.exceptions.ConcurrencyException; import org.onap.cps.spi.exceptions.CpsPathException; +import org.onap.cps.spi.exceptions.DataNodeNotFoundException; import org.onap.cps.spi.model.DataNode; import org.onap.cps.spi.model.DataNodeBuilder; import org.onap.cps.spi.repository.AnchorRepository; @@ -82,6 +84,7 @@ public class CpsDataPersistenceServiceImpl implements CpsDataPersistenceService private static final Gson GSON = new GsonBuilder().create(); private static final String REG_EX_FOR_OPTIONAL_LIST_INDEX = "(\\[@[\\s\\S]+?]){0,1})"; + private static final String REG_EX_FOR_LIST_NODE_KEY = "\\[(\\@([^/]*?))+( and)*\\]$"; @Override public void addChildDataNode(final String dataspaceName, final String anchorName, final String parentXpath, @@ -315,8 +318,33 @@ public class CpsDataPersistenceServiceImpl implements CpsDataPersistenceService fragmentRepository.save(parentEntity); } + @Override + @Transactional + public void deleteListDataNodes(final String dataspaceName, final String anchorName, final String listNodeXpath) { + final var parentNodeXpath = listNodeXpath.substring(0, listNodeXpath.lastIndexOf('/')); + final var parentEntity = getFragmentByXpath(dataspaceName, anchorName, parentNodeXpath); + final var descendantNode = listNodeXpath.substring(listNodeXpath.lastIndexOf('/')); + final Matcher descendantNodeHasListNodeKey = Pattern.compile(REG_EX_FOR_LIST_NODE_KEY).matcher(descendantNode); + + final boolean xpathPointsToAValidChildNodeWithKey = parentEntity.getChildFragments().stream().anyMatch( + (fragment) -> fragment.getXpath().equals(listNodeXpath)); + + final boolean xpathPointsToAValidChildNodeWithoutKey = parentEntity.getChildFragments().stream().anyMatch( + (fragment) -> fragment.getXpath().replaceAll(REG_EX_FOR_LIST_NODE_KEY, "").equals(listNodeXpath)); + + if ((descendantNodeHasListNodeKey.find() && xpathPointsToAValidChildNodeWithKey) + || + (!descendantNodeHasListNodeKey.find() && xpathPointsToAValidChildNodeWithoutKey)) { + removeListNodeDescendants(parentEntity, listNodeXpath); + } else { + throw new DataNodeNotFoundException(parentEntity.getDataspace().getName(), + parentEntity.getAnchor().getName(), listNodeXpath); + } + } + private void removeListNodeDescendants(final FragmentEntity parentFragmentEntity, final String listNodeXpath) { - final String listNodeXpathPrefix = listNodeXpath + "["; + final Matcher descendantNodeHasListNodeKey = Pattern.compile(REG_EX_FOR_LIST_NODE_KEY).matcher(listNodeXpath); + final String listNodeXpathPrefix = listNodeXpath + (descendantNodeHasListNodeKey.find() ? "" : "["); if (parentFragmentEntity.getChildFragments() .removeIf(fragment -> fragment.getXpath().startsWith(listNodeXpathPrefix))) { fragmentRepository.save(parentFragmentEntity); -- cgit 1.2.3-korg