summaryrefslogtreecommitdiffstats
path: root/catalog-model/src
diff options
context:
space:
mode:
authorJvD_Ericsson <jeff.van.dam@est.tech>2022-03-29 13:41:35 +0100
committerVasyl Razinkov <vasyl.razinkov@est.tech>2022-04-25 21:55:34 +0000
commit1ff5cd3de7ccc52adf0f4cbdf9c7ab511bd5c4a5 (patch)
treec1ad607be57bbd83bf4f52d1723425853fd85d20 /catalog-model/src
parent7ce54a98719426a32e041e5bf30c950a4b226734 (diff)
Support deletion of archived services in SDC BE
Issue-ID: SDC-3936 Change-Id: I75201007c9cf6b71b035f14864e380d78aace12b Signed-off-by: JvD_Ericsson <jeff.van.dam@est.tech>
Diffstat (limited to 'catalog-model/src')
-rw-r--r--catalog-model/src/main/java/org/openecomp/sdc/be/model/jsonjanusgraph/operations/ToscaElementOperation.java11
-rw-r--r--catalog-model/src/main/java/org/openecomp/sdc/be/model/jsonjanusgraph/operations/ToscaOperationFacade.java148
-rw-r--r--catalog-model/src/main/java/org/openecomp/sdc/be/model/jsonjanusgraph/operations/exception/ToscaOperationExceptionSupplier.java37
-rw-r--r--catalog-model/src/main/java/org/openecomp/sdc/be/model/operations/api/StorageOperationStatus.java2
-rw-r--r--catalog-model/src/test/java/org/openecomp/sdc/be/model/jsonjanusgraph/operations/ToscaOperationFacadeTest.java157
5 files changed, 343 insertions, 12 deletions
diff --git a/catalog-model/src/main/java/org/openecomp/sdc/be/model/jsonjanusgraph/operations/ToscaElementOperation.java b/catalog-model/src/main/java/org/openecomp/sdc/be/model/jsonjanusgraph/operations/ToscaElementOperation.java
index 9e9b944fae..caedbeeb62 100644
--- a/catalog-model/src/main/java/org/openecomp/sdc/be/model/jsonjanusgraph/operations/ToscaElementOperation.java
+++ b/catalog-model/src/main/java/org/openecomp/sdc/be/model/jsonjanusgraph/operations/ToscaElementOperation.java
@@ -150,6 +150,17 @@ public abstract class ToscaElementOperation extends BaseOperation {
return Either.left(vertexG);
}
+ protected GraphVertex getHighestVersionFrom(GraphVertex v) {
+ Either<GraphVertex, JanusGraphOperationStatus> childVertexE = janusGraphDao
+ .getChildVertex(v, EdgeLabelEnum.VERSION, JsonParseFlagEnum.NoParse);
+ GraphVertex highestVersionVertex = v;
+ while (childVertexE.isLeft()) {
+ highestVersionVertex = childVertexE.left().value();
+ childVertexE = janusGraphDao.getChildVertex(highestVersionVertex, EdgeLabelEnum.VERSION, JsonParseFlagEnum.NoParse);
+ }
+ return highestVersionVertex;
+ }
+
public Either<ToscaElement, StorageOperationStatus> getToscaElement(String uniqueId) {
return getToscaElement(uniqueId, new ComponentParametersView());
}
diff --git a/catalog-model/src/main/java/org/openecomp/sdc/be/model/jsonjanusgraph/operations/ToscaOperationFacade.java b/catalog-model/src/main/java/org/openecomp/sdc/be/model/jsonjanusgraph/operations/ToscaOperationFacade.java
index 496fd0fe08..0546a91369 100644
--- a/catalog-model/src/main/java/org/openecomp/sdc/be/model/jsonjanusgraph/operations/ToscaOperationFacade.java
+++ b/catalog-model/src/main/java/org/openecomp/sdc/be/model/jsonjanusgraph/operations/ToscaOperationFacade.java
@@ -31,6 +31,7 @@ import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
+import java.util.Comparator;
import java.util.EnumMap;
import java.util.HashMap;
import java.util.HashSet;
@@ -42,6 +43,7 @@ import java.util.Map.Entry;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
+import java.util.TreeSet;
import java.util.function.BiPredicate;
import java.util.stream.Collectors;
import org.apache.commons.collections.CollectionUtils;
@@ -79,6 +81,7 @@ import org.openecomp.sdc.be.datatypes.elements.RequirementDataDefinition;
import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
import org.openecomp.sdc.be.datatypes.enums.GraphPropertyEnum;
import org.openecomp.sdc.be.datatypes.enums.JsonPresentationFields;
+import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum;
import org.openecomp.sdc.be.datatypes.enums.OriginTypeEnum;
import org.openecomp.sdc.be.datatypes.enums.PromoteVersionEnum;
import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum;
@@ -114,8 +117,11 @@ import org.openecomp.sdc.be.model.catalog.CatalogComponent;
import org.openecomp.sdc.be.model.jsonjanusgraph.config.ContainerInstanceTypesData;
import org.openecomp.sdc.be.model.jsonjanusgraph.datamodel.TopologyTemplate;
import org.openecomp.sdc.be.model.jsonjanusgraph.datamodel.ToscaElement;
+import org.openecomp.sdc.be.model.jsonjanusgraph.datamodel.ToscaElementTypeEnum;
+import org.openecomp.sdc.be.model.jsonjanusgraph.operations.exception.ToscaOperationExceptionSupplier;
import org.openecomp.sdc.be.model.jsonjanusgraph.utils.ModelConverter;
import org.openecomp.sdc.be.model.operations.StorageException;
+import org.openecomp.sdc.be.model.operations.api.IGraphLockOperation;
import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
import org.openecomp.sdc.be.model.operations.impl.DaoStatusConverter;
import org.openecomp.sdc.be.model.operations.impl.UniqueIdBuilder;
@@ -144,6 +150,8 @@ public class ToscaOperationFacade {
private static final String COULDNT_FETCH_COMPONENT_WITH_AND_UNIQUE_ID_ERROR = "Couldn't fetch component with and unique id {}, error: {}";
private static final Logger log = Logger.getLogger(ToscaOperationFacade.class.getName());
@Autowired
+ private IGraphLockOperation graphLockOperation;
+ @Autowired
private NodeTypeOperation nodeTypeOperation;
@Autowired
private TopologyTemplateOperation topologyTemplateOperation;
@@ -992,21 +1000,122 @@ public class ToscaOperationFacade {
return Either.left(checkIfInUseAndDelete(allMarked));
}
- private List<String> checkIfInUseAndDelete(List<GraphVertex> allMarked) {
+ public List<String> deleteService(String invariantUUID, final boolean inTransaction) {
+ List<GraphVertex> allServiceVerticesToDelete = getVerticesForAllVersions(invariantUUID, ToscaElementTypeEnum.TOPOLOGY_TEMPLATE);
+ List<String> affectedComponentIds = new ArrayList<>();
+ try {
+ checkNotUsed( allServiceVerticesToDelete);
+ lockAllVerticesByNodeType(allServiceVerticesToDelete, NodeTypeEnum.Service);
+ for (GraphVertex elementV : allServiceVerticesToDelete) {
+ Either<ToscaElement, StorageOperationStatus> deleteToscaElement = deleteToscaElement(elementV);
+ if (deleteToscaElement.isRight()) {
+ log.debug("Failed to delete element UniqueID {}, Name {}, error {}", elementV.getUniqueId(),
+ elementV.getMetadataProperties().get(GraphPropertyEnum.NAME), deleteToscaElement.right().value());
+ throwStorageException(deleteToscaElement.right().value());
+ }
+ affectedComponentIds.add(elementV.getUniqueId());
+ }
+ if (!inTransaction) {
+ janusGraphDao.commit();
+ }
+ } catch (Exception exception) {
+ if (!inTransaction) {
+ janusGraphDao.rollback();
+ }
+ throw exception;
+ } finally {
+ unlockAllVerticesByNodeType(allServiceVerticesToDelete, NodeTypeEnum.Service);
+ }
+ return affectedComponentIds;
+ }
+
+ private void checkNotUsed(List<GraphVertex> vertices) {
+ boolean isInUse = isAnyComponentInUse(vertices);
+ if (isInUse) {
+ Set<GraphVertex> listOfVertices = getComponentsUsingComponents(vertices);
+ List<String> listOfStringComponents = new ArrayList<>();
+ for (GraphVertex componentVertex : listOfVertices) {
+ listOfStringComponents.add(
+ componentVertex.getMetadataJson().get(GraphPropertyEnum.COMPONENT_TYPE.getProperty()) + " "
+ + componentVertex.getMetadataJson().get(GraphPropertyEnum.NAME.getProperty())
+ );
+ }
+ String stringOfComponents = String.join(", ", listOfStringComponents);
+ throw ToscaOperationExceptionSupplier.componentInUse(stringOfComponents).get();
+ }
+ }
+
+ private List<GraphVertex> getVerticesForAllVersions(String invariantUUID, ToscaElementTypeEnum componentType){
+ Either<List<Component>, StorageOperationStatus> allComponents =
+ getComponentListByInvariantUuid(invariantUUID, null);
+ if (allComponents.isRight()) {
+ throwStorageException(allComponents.right().value());
+ }
+ List<GraphVertex> allComponentVertices = new ArrayList<>();
+ for (Component component : allComponents.left().value()) {
+ Either<GraphVertex, StorageOperationStatus> componentGraphVertex = topologyTemplateOperation
+ .getComponentByLabelAndId(component.getUniqueId(), componentType, JsonParseFlagEnum.ParseAll);
+ if (componentGraphVertex.isRight()) {
+ throwStorageException(componentGraphVertex.right().value());
+ }
+ allComponentVertices.add(componentGraphVertex.left().value());
+ }
+ return allComponentVertices;
+ }
+
+ public void commitAndCheck(String componentId) {
+ JanusGraphOperationStatus status = janusGraphDao.commit();
+ if (!status.equals(JanusGraphOperationStatus.OK)) {
+ log.debug("error occurred when trying to DELETE {}. Return code is: {}", componentId, status);
+ throwStorageException(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status));
+ }
+ }
+
+ private Set<GraphVertex> getComponentsUsingComponents(List<GraphVertex> componentVertices) {
+ Set<GraphVertex> inUseBy = new TreeSet<>(Comparator.comparing(GraphVertex::getUniqueId));
+ for (final GraphVertex elementV : componentVertices) {
+ List<GraphVertex> inUseByVertex = isInUse(elementV);
+ if (!inUseByVertex.isEmpty()) {
+ inUseBy.addAll(inUseByVertex);
+ }
+ }
+ return inUseBy;
+ }
+
+ private boolean isAnyComponentInUse(List<GraphVertex> componentVertices) {
+ boolean isComponentInUse = false;
+ if (log.isDebugEnabled()) {
+ for (final GraphVertex graphVertex : componentVertices) {
+ if (!isInUse(graphVertex).isEmpty()) {
+ isComponentInUse = true;
+ }
+ }
+ } else {
+ isComponentInUse = componentVertices.stream().anyMatch(vertex -> !isInUse(vertex).isEmpty());
+ }
+ return isComponentInUse;
+ }
+
+ private List<GraphVertex> isInUse(GraphVertex elementV) {
final List<EdgeLabelEnum> forbiddenEdgeLabelEnums = Arrays
- .asList(EdgeLabelEnum.INSTANCE_OF, EdgeLabelEnum.PROXY_OF, EdgeLabelEnum.ALLOTTED_OF);
- List<String> deleted = new ArrayList<>();
- for (GraphVertex elementV : allMarked) {
- boolean isAllowedToDelete = true;
- for (EdgeLabelEnum edgeLabelEnum : forbiddenEdgeLabelEnums) {
- Either<Edge, JanusGraphOperationStatus> belongingEdgeByCriteria = janusGraphDao
- .getBelongingEdgeByCriteria(elementV, edgeLabelEnum, null);
- if (belongingEdgeByCriteria.isLeft()) {
- log.debug("Marked element {} in use. don't delete it", elementV.getUniqueId());
- isAllowedToDelete = false;
- break;
+ .asList(EdgeLabelEnum.INSTANCE_OF, EdgeLabelEnum.PROXY_OF, EdgeLabelEnum.ALLOTTED_OF);
+ for (EdgeLabelEnum edgeLabelEnum : forbiddenEdgeLabelEnums) {
+ Either<List<GraphVertex>, JanusGraphOperationStatus> inUseBy =
+ janusGraphDao.getParentVertices(elementV, edgeLabelEnum, JsonParseFlagEnum.ParseAll);
+ if (inUseBy.isLeft()) {
+ if (log.isDebugEnabled()) {
+ log.debug("Element {} in use.", elementV.getUniqueId());
}
+ return inUseBy.left().value();
}
+ }
+ return Collections.emptyList();
+ }
+
+ private List<String> checkIfInUseAndDelete(List<GraphVertex> allMarked) {
+ List<String> deleted = new ArrayList<>();
+ for (GraphVertex elementV : allMarked) {
+ boolean isAllowedToDelete = !isInUse(elementV).isEmpty();
if (isAllowedToDelete) {
Either<ToscaElement, StorageOperationStatus> deleteToscaElement = deleteToscaElement(elementV);
if (deleteToscaElement.isRight()) {
@@ -1020,6 +1129,21 @@ public class ToscaOperationFacade {
return deleted;
}
+ private void lockAllVerticesByNodeType(List<GraphVertex> allVerticesToLock, NodeTypeEnum nodeType) {
+ for (GraphVertex graphVertex : allVerticesToLock) {
+ StorageOperationStatus storageOperationStatus = graphLockOperation.lockComponent(graphVertex.getUniqueId(), nodeType);
+ if (!storageOperationStatus.equals(StorageOperationStatus.OK)) {
+ throwStorageException(storageOperationStatus);
+ }
+ }
+ }
+
+ private void unlockAllVerticesByNodeType(List<GraphVertex> allVerticesToUnlock, NodeTypeEnum nodeType) {
+ for (GraphVertex graphVertex : allVerticesToUnlock) {
+ graphLockOperation.unlockComponent(graphVertex.getUniqueId(), nodeType);
+ }
+ }
+
public Either<List<String>, StorageOperationStatus> getAllComponentsMarkedForDeletion(ComponentTypeEnum componentType) {
Either<List<GraphVertex>, StorageOperationStatus> allComponentsMarkedForDeletion;
switch (componentType) {
diff --git a/catalog-model/src/main/java/org/openecomp/sdc/be/model/jsonjanusgraph/operations/exception/ToscaOperationExceptionSupplier.java b/catalog-model/src/main/java/org/openecomp/sdc/be/model/jsonjanusgraph/operations/exception/ToscaOperationExceptionSupplier.java
new file mode 100644
index 0000000000..62d7e29b48
--- /dev/null
+++ b/catalog-model/src/main/java/org/openecomp/sdc/be/model/jsonjanusgraph/operations/exception/ToscaOperationExceptionSupplier.java
@@ -0,0 +1,37 @@
+/*
+ * ============LICENSE_START=======================================================
+ * Copyright (C) 2021 Nordix Foundation
+ * ================================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ * ============LICENSE_END=========================================================
+ */
+
+package org.openecomp.sdc.be.model.jsonjanusgraph.operations.exception;
+
+import java.util.function.Supplier;
+import org.openecomp.sdc.be.dao.api.ActionStatus;
+import org.openecomp.sdc.be.dao.janusgraph.JanusGraphOperationStatus;
+import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
+
+public class ToscaOperationExceptionSupplier {
+
+ private ToscaOperationExceptionSupplier() {
+
+ }
+
+ public static Supplier<OperationException> componentInUse(final String stringOfServices) {
+ return () -> new OperationException(ActionStatus.COMPONENT_IN_USE_BY_ANOTHER_COMPONENT, stringOfServices);
+ }
+
+}
diff --git a/catalog-model/src/main/java/org/openecomp/sdc/be/model/operations/api/StorageOperationStatus.java b/catalog-model/src/main/java/org/openecomp/sdc/be/model/operations/api/StorageOperationStatus.java
index 4fbb5fbef3..491603081f 100644
--- a/catalog-model/src/main/java/org/openecomp/sdc/be/model/operations/api/StorageOperationStatus.java
+++ b/catalog-model/src/main/java/org/openecomp/sdc/be/model/operations/api/StorageOperationStatus.java
@@ -64,6 +64,8 @@ public enum StorageOperationStatus {
PROPERTY_NAME_ALREADY_EXISTS,
INVALID_PROPERTY,
COMPONENT_IS_ARCHIVED,
+ COMPONENT_NOT_ARCHIVED,
+ COMPONENT_IN_USE_BY_ANOTHER_COMPONENT,
DECLARED_INPUT_USED_BY_OPERATION;
// @formatter:on
diff --git a/catalog-model/src/test/java/org/openecomp/sdc/be/model/jsonjanusgraph/operations/ToscaOperationFacadeTest.java b/catalog-model/src/test/java/org/openecomp/sdc/be/model/jsonjanusgraph/operations/ToscaOperationFacadeTest.java
index c0c417a324..625ec39837 100644
--- a/catalog-model/src/test/java/org/openecomp/sdc/be/model/jsonjanusgraph/operations/ToscaOperationFacadeTest.java
+++ b/catalog-model/src/test/java/org/openecomp/sdc/be/model/jsonjanusgraph/operations/ToscaOperationFacadeTest.java
@@ -36,6 +36,7 @@ import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
+import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.anyMap;
@@ -70,6 +71,8 @@ import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.mockito.junit.MockitoJUnitRunner;
+import org.openecomp.sdc.be.config.ComponentType;
+import org.openecomp.sdc.be.dao.api.ActionStatus;
import org.openecomp.sdc.be.dao.janusgraph.JanusGraphOperationStatus;
import org.openecomp.sdc.be.dao.jsongraph.GraphVertex;
import org.openecomp.sdc.be.dao.janusgraph.HealingJanusGraphDao;
@@ -84,6 +87,7 @@ import org.openecomp.sdc.be.datatypes.elements.RequirementDataDefinition;
import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
import org.openecomp.sdc.be.datatypes.enums.GraphPropertyEnum;
import org.openecomp.sdc.be.datatypes.enums.JsonPresentationFields;
+import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum;
import org.openecomp.sdc.be.datatypes.enums.OriginTypeEnum;
import org.openecomp.sdc.be.datatypes.enums.PromoteVersionEnum;
import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum;
@@ -106,7 +110,10 @@ import org.openecomp.sdc.be.model.jsonjanusgraph.datamodel.NodeType;
import org.openecomp.sdc.be.model.jsonjanusgraph.datamodel.TopologyTemplate;
import org.openecomp.sdc.be.model.jsonjanusgraph.datamodel.ToscaElement;
import org.openecomp.sdc.be.model.jsonjanusgraph.datamodel.ToscaElementTypeEnum;
+import org.openecomp.sdc.be.model.jsonjanusgraph.operations.exception.OperationException;
import org.openecomp.sdc.be.model.jsonjanusgraph.utils.ModelConverter;
+import org.openecomp.sdc.be.model.operations.StorageException;
+import org.openecomp.sdc.be.model.operations.api.IGraphLockOperation;
import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
@RunWith(MockitoJUnitRunner.class)
@@ -137,6 +144,9 @@ public class ToscaOperationFacadeTest {
@Mock
private NodeTemplateOperation nodeTemplateOperationMock;
+ @Mock
+ private IGraphLockOperation graphLockOperationMock;
+
@Before
public void setUp() throws Exception {
testInstance = new ToscaOperationFacade();
@@ -408,6 +418,153 @@ public class ToscaOperationFacadeTest {
}
@Test
+ public void testDeleteService_ServiceInUse() {
+ String invariantUUID = "12345";
+ String serviceUid = "1";
+ GraphVertex service1 = getTopologyTemplateVertex();
+ service1.setUniqueId(serviceUid);
+ List<GraphVertex> allResourcesToDelete = new ArrayList<>();
+ allResourcesToDelete.add(service1);
+ Map<GraphPropertyEnum, Object> propertiesToMatch = new EnumMap<>(GraphPropertyEnum.class);
+ propertiesToMatch.put(GraphPropertyEnum.INVARIANT_UUID, invariantUUID);
+ ToscaElement toscaElement = getToscaElementForTest();
+ toscaElement.setUniqueId(serviceUid);
+ String service2Name = "service2Name";
+ Map<String, Object> service2MetadataJson = new HashMap<>();
+ service2MetadataJson.put(GraphPropertyEnum.COMPONENT_TYPE.getProperty(), ComponentType.SERVICE);
+ service2MetadataJson.put(GraphPropertyEnum.NAME.getProperty(), service2Name);
+ String service2Uid = "2";
+ GraphVertex usingService = getTopologyTemplateVertex();
+ usingService.setUniqueId(service2Uid);
+ usingService.setMetadataJson(service2MetadataJson);
+ List<GraphVertex> inUseBy = new ArrayList<>();
+ inUseBy.add(usingService);
+
+ when(janusGraphDaoMock.getByCriteria(null, propertiesToMatch, JsonParseFlagEnum.ParseMetadata)).
+ thenReturn(Either.left(allResourcesToDelete));
+ doReturn(Either.left(toscaElement)).when(topologyTemplateOperationMock).getToscaElement(eq(service1), any(ComponentParametersView.class));
+ when(topologyTemplateOperationMock.
+ getComponentByLabelAndId(serviceUid, ToscaElementTypeEnum.TOPOLOGY_TEMPLATE, JsonParseFlagEnum.ParseAll)).
+ thenReturn(Either.left(service1));
+ when(janusGraphDaoMock.getParentVertices(any(GraphVertex.class), any(), eq(JsonParseFlagEnum.ParseAll))).
+ thenReturn(Either.left(inUseBy)).thenReturn(Either.left(inUseBy));
+ final OperationException actualException = assertThrows(OperationException.class, () -> testInstance.deleteService(invariantUUID, true));
+ assertEquals(actualException.getActionStatus(), ActionStatus.COMPONENT_IN_USE_BY_ANOTHER_COMPONENT);
+ assertEquals(actualException.getParams()[0], ComponentType.SERVICE + " " + service2Name);
+ }
+
+ @Test
+ public void testDeleteService_WithOneVersion() {
+ String invariantUUID = "12345";
+ String serviceUid = "1";
+ Map<GraphPropertyEnum, Object> propertiesToMatch = new EnumMap<>(GraphPropertyEnum.class);
+ propertiesToMatch.put(GraphPropertyEnum.INVARIANT_UUID, invariantUUID);
+ GraphVertex service1 = getTopologyTemplateVertex();
+ service1.setUniqueId(serviceUid);
+ List<GraphVertex> allResourcesToDelete = new ArrayList<>();
+ allResourcesToDelete.add(service1);
+ ToscaElement toscaElement = getToscaElementForTest();
+ toscaElement.setUniqueId(serviceUid);
+ List<String> affectedComponentIds = new ArrayList<>();
+ affectedComponentIds.add(service1.getUniqueId());
+
+ when(janusGraphDaoMock.getByCriteria(null, propertiesToMatch, JsonParseFlagEnum.ParseMetadata)).
+ thenReturn(Either.left(allResourcesToDelete));
+ doReturn(Either.left(toscaElement)).when(topologyTemplateOperationMock).getToscaElement(eq(service1), any(ComponentParametersView.class));
+ when(topologyTemplateOperationMock.
+ getComponentByLabelAndId(serviceUid, ToscaElementTypeEnum.TOPOLOGY_TEMPLATE, JsonParseFlagEnum.ParseAll)).
+ thenReturn(Either.left(service1));
+ when(janusGraphDaoMock.getParentVertices(eq(service1), any(), eq(JsonParseFlagEnum.ParseAll))).
+ thenReturn(Either.right(JanusGraphOperationStatus.OK));
+ when(graphLockOperationMock.lockComponent(service1.getUniqueId(), NodeTypeEnum.Service)).
+ thenReturn(StorageOperationStatus.OK);
+ when(topologyTemplateOperationMock.deleteToscaElement(service1)).thenReturn(Either.left(toscaElement));
+ assertEquals(affectedComponentIds, testInstance.deleteService(invariantUUID, true));
+ }
+
+ @Test
+ public void testDeleteService_WithTwoVersions() {
+ String invariantUUID = "12345";
+ String serviceUid = "1";
+ String service2Uid = "2";
+ GraphVertex service = getTopologyTemplateVertex();
+ service.setUniqueId(serviceUid);
+ GraphVertex serviceV2 = getTopologyTemplateVertex();
+ serviceV2.setUniqueId(service2Uid);
+ ToscaElement toscaElement = getToscaElementForTest();
+ toscaElement.setUniqueId(serviceUid);
+ ToscaElement toscaElement2 = getToscaElementForTest();
+ toscaElement2.setUniqueId(service2Uid);
+ List<String> affectedComponentIds = new ArrayList<>();
+ affectedComponentIds.add(service.getUniqueId());
+ affectedComponentIds.add(serviceV2.getUniqueId());
+ List<GraphVertex> allResourcesToDelete = new ArrayList<>();
+ allResourcesToDelete.add(service);
+ allResourcesToDelete.add(serviceV2);
+ Map<GraphPropertyEnum, Object> propertiesToMatch = new EnumMap<>(GraphPropertyEnum.class);
+ propertiesToMatch.put(GraphPropertyEnum.INVARIANT_UUID, invariantUUID);
+
+ when(janusGraphDaoMock.getByCriteria(null, propertiesToMatch, JsonParseFlagEnum.ParseMetadata)).
+ thenReturn(Either.left(allResourcesToDelete));
+ doReturn(Either.left(toscaElement)).when(topologyTemplateOperationMock).
+ getToscaElement(eq(service), any(ComponentParametersView.class));
+ doReturn(Either.left(toscaElement2)).when(topologyTemplateOperationMock).
+ getToscaElement(eq(serviceV2), any(ComponentParametersView.class));
+ when(topologyTemplateOperationMock.
+ getComponentByLabelAndId(serviceUid, ToscaElementTypeEnum.TOPOLOGY_TEMPLATE, JsonParseFlagEnum.ParseAll)).
+ thenReturn(Either.left(service));
+ when(topologyTemplateOperationMock.
+ getComponentByLabelAndId(service2Uid, ToscaElementTypeEnum.TOPOLOGY_TEMPLATE, JsonParseFlagEnum.ParseAll)).
+ thenReturn(Either.left(serviceV2));
+ when(janusGraphDaoMock.getParentVertices(any(GraphVertex.class), any(), eq(JsonParseFlagEnum.ParseAll))).
+ thenReturn(Either.right(JanusGraphOperationStatus.OK));
+ when(graphLockOperationMock.lockComponent(service.getUniqueId(), NodeTypeEnum.Service)).
+ thenReturn(StorageOperationStatus.OK);
+ when(graphLockOperationMock.lockComponent(serviceV2.getUniqueId(), NodeTypeEnum.Service)).
+ thenReturn(StorageOperationStatus.OK);
+ when(topologyTemplateOperationMock.deleteToscaElement(service)).thenReturn(Either.left(toscaElement));
+ when(topologyTemplateOperationMock.deleteToscaElement(serviceV2)).thenReturn(Either.left(toscaElement));
+ assertEquals(affectedComponentIds, testInstance.deleteService(invariantUUID, true));
+ }
+
+ @Test
+ public void testDeleteService_FailDelete() {
+ String invariantUUID = "12345";
+ String serviceUid = "1";
+ GraphVertex service = getTopologyTemplateVertex();
+ service.setUniqueId(serviceUid);
+ ToscaElement toscaElement = getToscaElementForTest();
+ toscaElement.setUniqueId(serviceUid);
+ List<GraphVertex> allResourcesToDelete = new ArrayList<>();
+ allResourcesToDelete.add(service);
+ Map<GraphPropertyEnum, Object> propertiesToMatch = new EnumMap<>(GraphPropertyEnum.class);
+ propertiesToMatch.put(GraphPropertyEnum.INVARIANT_UUID, invariantUUID);
+
+ when(janusGraphDaoMock.getByCriteria(null, propertiesToMatch, JsonParseFlagEnum.ParseMetadata)).
+ thenReturn(Either.left(allResourcesToDelete));
+ doReturn(Either.left(toscaElement)).when(topologyTemplateOperationMock).getToscaElement(eq(service), any(ComponentParametersView.class));
+ when(topologyTemplateOperationMock.getComponentByLabelAndId(serviceUid, ToscaElementTypeEnum.TOPOLOGY_TEMPLATE, JsonParseFlagEnum.ParseAll)).
+ thenReturn(Either.left(service));
+ when(janusGraphDaoMock.getParentVertices(eq(service), any(), eq(JsonParseFlagEnum.ParseAll))).
+ thenReturn(Either.right(JanusGraphOperationStatus.OK));
+ when(graphLockOperationMock.lockComponent(service.getUniqueId(), NodeTypeEnum.Service)).
+ thenReturn(StorageOperationStatus.OK);
+ when(topologyTemplateOperationMock.deleteToscaElement(service))
+ .thenReturn(Either.right(StorageOperationStatus.NOT_FOUND));
+ assertThrows(StorageException.class, () -> testInstance.deleteService(invariantUUID, true));
+ }
+
+ @Test
+ public void testDeleteService_NotFound() {
+ String invariantUUID = "12345";
+ Map<GraphPropertyEnum, Object> propertiesToMatch = new EnumMap<>(GraphPropertyEnum.class);
+ propertiesToMatch.put(GraphPropertyEnum.INVARIANT_UUID, invariantUUID);
+ when(janusGraphDaoMock.getByCriteria(null, propertiesToMatch, JsonParseFlagEnum.ParseMetadata)).
+ thenReturn(Either.right(JanusGraphOperationStatus.NOT_FOUND));
+ assertThrows(StorageException.class, () -> testInstance.deleteService(invariantUUID, true));
+ }
+
+ @Test
public void testMarkComponentToDelete() {
StorageOperationStatus result;
Component component = new Resource();