diff options
author | andre.schmid <andre.schmid@est.tech> | 2022-09-08 18:55:08 +0100 |
---|---|---|
committer | Michael Morris <michael.morris@est.tech> | 2022-09-19 08:38:43 +0000 |
commit | 0d38a72c022fa93dba0bf052f893e51fb73d3552 (patch) | |
tree | 25f49242e6c6666c39e32abae66e4950fe4e18ed /catalog-model | |
parent | 2fe3d879319a34e9802d0bc172fa11fefe771df3 (diff) |
Support TOSCA functions in Node Capability Filters
Adds support to use TOSCA functions as value in the node capability
filters.
Removes the current capability filter component to reuse, with a few
changes, the node property filters component.
Fixes problems with the edition and deletion of node capability
filters.
Change-Id: Ic91242d6cbc24e2ce0f60b84c63e104575bef8a9
Issue-ID: SDC-4173
Signed-off-by: André Schmid <andre.schmid@est.tech>
Diffstat (limited to 'catalog-model')
2 files changed, 93 insertions, 19 deletions
diff --git a/catalog-model/src/main/java/org/openecomp/sdc/be/model/jsonjanusgraph/operations/NodeFilterOperation.java b/catalog-model/src/main/java/org/openecomp/sdc/be/model/jsonjanusgraph/operations/NodeFilterOperation.java index 3dc2a73466..f1de061c9f 100644 --- a/catalog-model/src/main/java/org/openecomp/sdc/be/model/jsonjanusgraph/operations/NodeFilterOperation.java +++ b/catalog-model/src/main/java/org/openecomp/sdc/be/model/jsonjanusgraph/operations/NodeFilterOperation.java @@ -84,13 +84,9 @@ public class NodeFilterOperation extends BaseOperation { final int propertyIndex, final NodeFilterConstraintType nodeFilterConstraintType) { if (NodeFilterConstraintType.PROPERTIES.equals(nodeFilterConstraintType)) { - final ListDataDefinition<PropertyFilterDataDefinition> properties = nodeFilterDataDefinition.getProperties(); - properties.getListToscaDataDefinition().remove(propertyIndex); - nodeFilterDataDefinition.setProperties(properties); + nodeFilterDataDefinition.getProperties().getListToscaDataDefinition().remove(propertyIndex); } else if (NodeFilterConstraintType.CAPABILITIES.equals(nodeFilterConstraintType)) { - final ListDataDefinition<RequirementNodeFilterCapabilityDataDefinition> capabilities = nodeFilterDataDefinition.getCapabilities(); - capabilities.getListToscaDataDefinition().remove(propertyIndex); - nodeFilterDataDefinition.setCapabilities(capabilities); + removeCapabilityNodeFilterByIndex(nodeFilterDataDefinition, propertyIndex); } return addOrUpdateNodeFilter(true, serviceId, componentInstanceId, nodeFilterDataDefinition); } @@ -116,11 +112,11 @@ public class NodeFilterOperation extends BaseOperation { capabilities = new ListDataDefinition<>(); nodeFilterDataDefinition.setCapabilities(capabilities); } - + final Optional<RequirementNodeFilterCapabilityDataDefinition> existingCap = capabilities .getListToscaDataDefinition().stream() .filter(def -> def.getName().equals(requirementNodeFilterCapabilityDataDefinition.getName())).findAny(); - + if (existingCap.isPresent()) { final ListDataDefinition<PropertyFilterDataDefinition> newProperties = requirementNodeFilterCapabilityDataDefinition.getProperties(); final ListDataDefinition<PropertyFilterDataDefinition> existingProperties = existingCap.get().getProperties(); @@ -144,6 +140,29 @@ public class NodeFilterOperation extends BaseOperation { return addOrUpdateNodeFilter(false, componentId, componentInstanceId, nodeFilterDataDefinition); } + private void removeCapabilityNodeFilterByIndex(final CINodeFilterDataDefinition nodeFilterDataDefinition, final int filterToRemoveIndex) { + int currentFilterCountdown = filterToRemoveIndex; + final List<RequirementNodeFilterCapabilityDataDefinition> filtersByCapability = + nodeFilterDataDefinition.getCapabilities().getListToscaDataDefinition(); + for (final RequirementNodeFilterCapabilityDataDefinition capabilityFilterGroup : filtersByCapability) { + final List<PropertyFilterDataDefinition> capabilityFilters = capabilityFilterGroup.getProperties().getListToscaDataDefinition(); + if (isFilterInCapabilityGroup(currentFilterCountdown, capabilityFilters)) { + capabilityFilters.remove(currentFilterCountdown); + break; + } else { + currentFilterCountdown = getRemainingFilterCount(currentFilterCountdown, capabilityFilters); + } + } + } + + private boolean isFilterInCapabilityGroup(int currentFilterCount, List<PropertyFilterDataDefinition> capabilityFilters) { + return capabilityFilters.size() > currentFilterCount; + } + + private int getRemainingFilterCount(int currentFilterCount, final List<PropertyFilterDataDefinition> capabilityFilters) { + return currentFilterCount - capabilityFilters.size(); + } + private Either<CINodeFilterDataDefinition, StorageOperationStatus> addOrUpdateNodeFilter(final boolean isUpdateAction, final String componentId, final String componentInstanceId, final CINodeFilterDataDefinition ciNodeFilterDataDefinition) { diff --git a/catalog-model/src/test/java/org/openecomp/sdc/be/model/jsonjanusgraph/operations/NodeFilterOperationTest.java b/catalog-model/src/test/java/org/openecomp/sdc/be/model/jsonjanusgraph/operations/NodeFilterOperationTest.java index bd16016ad9..3df279511b 100644 --- a/catalog-model/src/test/java/org/openecomp/sdc/be/model/jsonjanusgraph/operations/NodeFilterOperationTest.java +++ b/catalog-model/src/test/java/org/openecomp/sdc/be/model/jsonjanusgraph/operations/NodeFilterOperationTest.java @@ -20,9 +20,22 @@ */ package org.openecomp.sdc.be.model.jsonjanusgraph.operations; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.ArgumentMatchers.nullable; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + import fj.data.Either; +import java.util.Arrays; +import java.util.HashMap; import java.util.List; import java.util.Map; +import org.apache.tinkerpop.gremlin.structure.Direction; +import org.apache.tinkerpop.gremlin.structure.Edge; import org.janusgraph.core.JanusGraphVertex; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -37,22 +50,14 @@ import org.openecomp.sdc.be.datatypes.elements.CINodeFilterDataDefinition; import org.openecomp.sdc.be.datatypes.elements.ListDataDefinition; import org.openecomp.sdc.be.datatypes.elements.PropertyFilterConstraintDataDefinition; import org.openecomp.sdc.be.datatypes.elements.PropertyFilterDataDefinition; +import org.openecomp.sdc.be.datatypes.elements.RequirementNodeFilterCapabilityDataDefinition; import org.openecomp.sdc.be.datatypes.enums.ConstraintType; import org.openecomp.sdc.be.datatypes.enums.FilterValueType; +import org.openecomp.sdc.be.datatypes.enums.GraphPropertyEnum; +import org.openecomp.sdc.be.datatypes.enums.NodeFilterConstraintType; import org.openecomp.sdc.be.datatypes.enums.PropertyFilterTargetType; import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus; -import java.util.Arrays; -import java.util.HashMap; - -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertTrue; -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.ArgumentMatchers.eq; -import static org.mockito.ArgumentMatchers.nullable; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; - class NodeFilterOperationTest { private final NodeFilterOperation nodeFilterOperation = new NodeFilterOperation(); @@ -116,4 +121,54 @@ class NodeFilterOperationTest { assertEquals("new node filter name", expectedNodeFilter.getName()); assertEquals(listDataDefinition, expectedNodeFilter.getProperties()); } + + @Test + void deleteCapabilityConstraintTest() { + final var ciNodeFilterDataDefinition = new CINodeFilterDataDefinition(); + final var capabilities = new ListDataDefinition<RequirementNodeFilterCapabilityDataDefinition>(); + + var capabilityFilter1 = new RequirementNodeFilterCapabilityDataDefinition(); + final var propertyFilterList = new ListDataDefinition<PropertyFilterDataDefinition>(); + propertyFilterList.add(new PropertyFilterDataDefinition()); + propertyFilterList.add(new PropertyFilterDataDefinition()); + propertyFilterList.add(new PropertyFilterDataDefinition()); + propertyFilterList.add(new PropertyFilterDataDefinition()); + propertyFilterList.add(new PropertyFilterDataDefinition()); + propertyFilterList.add(new PropertyFilterDataDefinition()); + capabilityFilter1.setProperties(propertyFilterList); + capabilities.add(capabilityFilter1); + + var capabilityFilter2 = new RequirementNodeFilterCapabilityDataDefinition(); + final var propertyFilterList2 = new ListDataDefinition<PropertyFilterDataDefinition>(); + final var filterToDelete = new PropertyFilterDataDefinition(); + propertyFilterList2.add(filterToDelete); + propertyFilterList2.add(new PropertyFilterDataDefinition()); + capabilityFilter2.setProperties(propertyFilterList2); + capabilities.add(capabilityFilter2); + + final GraphVertex serviceVertexMock = mock(GraphVertex.class); + final JanusGraphVertex serviceJanusVertex = mock(JanusGraphVertex.class); + when(serviceVertexMock.getVertex()).thenReturn(serviceJanusVertex); + when(serviceVertexMock.getUniqueId()).thenReturn("componentId"); + when(janusGraphDao.getVertexById("componentId", JsonParseFlagEnum.NoParse)).thenReturn(Either.left(serviceVertexMock)); + final GraphVertex nodeFilterVertexMock = mock(GraphVertex.class); + when(janusGraphDao.getChildVertex(serviceVertexMock, EdgeLabelEnum.NODE_FILTER_TEMPLATE, JsonParseFlagEnum.ParseJson)) + .thenReturn(Either.left(nodeFilterVertexMock)); + final JanusGraphVertex nodeFilterJanusVertexMock = mock(JanusGraphVertex.class); + when(nodeFilterVertexMock.getVertex()).thenReturn(nodeFilterJanusVertexMock); + final Edge edgeToDeleteMock = mock(Edge.class); + when(nodeFilterJanusVertexMock.edges(Direction.IN, EdgeLabelEnum.NODE_FILTER_TEMPLATE.name())) + .thenReturn(List.of(edgeToDeleteMock).iterator()); + when(janusGraphDao.getProperty(any(), eq(GraphPropertyEnum.UNIQUE_ID.getProperty()))).thenReturn("componentId"); + when(janusGraphDao.updateVertex(nodeFilterVertexMock)).thenReturn(Either.left(nodeFilterVertexMock)); + + ciNodeFilterDataDefinition.setCapabilities(capabilities); + final Either<CINodeFilterDataDefinition, StorageOperationStatus> result = nodeFilterOperation.deleteConstraint( + "componentId", "instanceId", ciNodeFilterDataDefinition, 6, NodeFilterConstraintType.CAPABILITIES); + + assertTrue(result.isLeft()); + assertEquals(1, propertyFilterList2.getListToscaDataDefinition().size()); + assertFalse(propertyFilterList2.getListToscaDataDefinition().contains(filterToDelete)); + } + }
\ No newline at end of file |