summaryrefslogtreecommitdiffstats
path: root/cps-ri/src/main/java/org
diff options
context:
space:
mode:
authorBruno Sakoto <bruno.sakoto@bell.ca>2022-02-28 23:25:49 +0000
committerGerrit Code Review <gerrit@onap.org>2022-02-28 23:25:49 +0000
commit03459a08895ecc7e481fc5ec34779556268992f1 (patch)
tree942b62f0055c70ca7b863939ef10ca32cbbb0334 /cps-ri/src/main/java/org
parentd042fcdae75be100f840b6313e078eab278d87ef (diff)
parentba1de927a6b7c7836854759a3d60c74b24cb292d (diff)
Merge "Bug fix for delete data node not working for root node"
Diffstat (limited to 'cps-ri/src/main/java/org')
-rw-r--r--cps-ri/src/main/java/org/onap/cps/spi/impl/CpsDataPersistenceServiceImpl.java49
1 files changed, 34 insertions, 15 deletions
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 04804726c9..f22d83b981 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
@@ -329,22 +329,34 @@ public class CpsDataPersistenceServiceImpl implements CpsDataPersistenceService
}
private void deleteDataNode(final String dataspaceName, final String anchorName, final String targetXpath,
- final boolean onlySupportListNodeDeletion) {
- final String parentNodeXpath = targetXpath.substring(0, targetXpath.lastIndexOf('/'));
- final FragmentEntity parentFragmentEntity = getFragmentByXpath(dataspaceName, anchorName, parentNodeXpath);
- final String lastXpathElement = targetXpath.substring(targetXpath.lastIndexOf('/'));
- final boolean isListElement = REG_EX_PATTERN_FOR_LIST_ELEMENT_KEY_PREDICATE.matcher(lastXpathElement).find();
- boolean targetExist;
- if (isListElement) {
- targetExist = deleteDataNode(parentFragmentEntity, targetXpath);
+ final boolean onlySupportListNodeDeletion) {
+ final String parentNodeXpath;
+ FragmentEntity parentFragmentEntity = null;
+ boolean targetDeleted = false;
+ if (isRootXpath(targetXpath)) {
+ deleteDataNodes(dataspaceName, anchorName);
+ targetDeleted = true;
} else {
- targetExist = deleteAllListElements(parentFragmentEntity, targetXpath);
- final boolean tryToDeleteDataNode = !targetExist && !onlySupportListNodeDeletion;
- if (tryToDeleteDataNode) {
- targetExist = deleteDataNode(parentFragmentEntity, targetXpath);
+ if (isContainerNodeXpath(targetXpath)) {
+ parentNodeXpath = targetXpath;
+ } else {
+ parentNodeXpath = targetXpath.substring(0, targetXpath.lastIndexOf('/'));
+ }
+ parentFragmentEntity = getFragmentByXpath(dataspaceName, anchorName, parentNodeXpath);
+ final String lastXpathElement = targetXpath.substring(targetXpath.lastIndexOf('/'));
+ final boolean isListElement = REG_EX_PATTERN_FOR_LIST_ELEMENT_KEY_PREDICATE
+ .matcher(lastXpathElement).find();
+ if (isListElement) {
+ targetDeleted = deleteDataNode(parentFragmentEntity, targetXpath);
+ } else {
+ targetDeleted = deleteAllListElements(parentFragmentEntity, targetXpath);
+ final boolean tryToDeleteDataNode = !targetDeleted && !onlySupportListNodeDeletion;
+ if (tryToDeleteDataNode) {
+ targetDeleted = deleteDataNode(parentFragmentEntity, targetXpath);
+ }
}
}
- if (!targetExist) {
+ if (!targetDeleted) {
final String additionalInformation = onlySupportListNodeDeletion
? "The target is probably not a List." : "";
throw new DataNodeNotFoundException(parentFragmentEntity.getDataspace().getName(),
@@ -353,6 +365,10 @@ public class CpsDataPersistenceServiceImpl implements CpsDataPersistenceService
}
private boolean deleteDataNode(final FragmentEntity parentFragmentEntity, final String targetXpath) {
+ if (parentFragmentEntity.getXpath().equals(targetXpath)) {
+ fragmentRepository.delete(parentFragmentEntity);
+ return true;
+ }
if (parentFragmentEntity.getChildFragments()
.removeIf(fragment -> fragment.getXpath().equals(targetXpath))) {
fragmentRepository.save(parentFragmentEntity);
@@ -361,7 +377,6 @@ public class CpsDataPersistenceServiceImpl implements CpsDataPersistenceService
return false;
}
-
private boolean deleteAllListElements(final FragmentEntity parentFragmentEntity, final String listXpath) {
final String deleteTargetXpathPrefix = listXpath + "[";
if (parentFragmentEntity.getChildFragments()
@@ -384,7 +399,7 @@ public class CpsDataPersistenceServiceImpl implements CpsDataPersistenceService
"Cannot replace list elements with empty collection");
}
final String firstChildNodeXpath = newListElements.iterator().next().getXpath();
- return firstChildNodeXpath.substring(0, firstChildNodeXpath.lastIndexOf("[") + 1);
+ return firstChildNodeXpath.substring(0, firstChildNodeXpath.lastIndexOf('[') + 1);
}
private FragmentEntity getFragmentForReplacement(final FragmentEntity parentEntity,
@@ -408,6 +423,10 @@ public class CpsDataPersistenceServiceImpl implements CpsDataPersistenceService
return !existingListElementsByXpath.containsKey(replacementDataNode.getXpath());
}
+ private static boolean isContainerNodeXpath(final String xpath) {
+ return 0 == xpath.lastIndexOf('/');
+ }
+
private void copyAttributesFromNewListElement(final FragmentEntity existingListElementEntity,
final DataNode newListElement) {
final FragmentEntity replacementFragmentEntity =