From 0d38a72c022fa93dba0bf052f893e51fb73d3552 Mon Sep 17 00:00:00 2001 From: "andre.schmid" Date: Thu, 8 Sep 2022 18:55:08 +0100 Subject: Support TOSCA functions in Node Capability Filters MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- .../operations/NodeFilterOperation.java | 35 +++++++--- .../operations/NodeFilterOperationTest.java | 77 ++++++++++++++++++---- 2 files changed, 93 insertions(+), 19 deletions(-) (limited to 'catalog-model') 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 properties = nodeFilterDataDefinition.getProperties(); - properties.getListToscaDataDefinition().remove(propertyIndex); - nodeFilterDataDefinition.setProperties(properties); + nodeFilterDataDefinition.getProperties().getListToscaDataDefinition().remove(propertyIndex); } else if (NodeFilterConstraintType.CAPABILITIES.equals(nodeFilterConstraintType)) { - final ListDataDefinition 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 existingCap = capabilities .getListToscaDataDefinition().stream() .filter(def -> def.getName().equals(requirementNodeFilterCapabilityDataDefinition.getName())).findAny(); - + if (existingCap.isPresent()) { final ListDataDefinition newProperties = requirementNodeFilterCapabilityDataDefinition.getProperties(); final ListDataDefinition 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 filtersByCapability = + nodeFilterDataDefinition.getCapabilities().getListToscaDataDefinition(); + for (final RequirementNodeFilterCapabilityDataDefinition capabilityFilterGroup : filtersByCapability) { + final List capabilityFilters = capabilityFilterGroup.getProperties().getListToscaDataDefinition(); + if (isFilterInCapabilityGroup(currentFilterCountdown, capabilityFilters)) { + capabilityFilters.remove(currentFilterCountdown); + break; + } else { + currentFilterCountdown = getRemainingFilterCount(currentFilterCountdown, capabilityFilters); + } + } + } + + private boolean isFilterInCapabilityGroup(int currentFilterCount, List capabilityFilters) { + return capabilityFilters.size() > currentFilterCount; + } + + private int getRemainingFilterCount(int currentFilterCount, final List capabilityFilters) { + return currentFilterCount - capabilityFilters.size(); + } + private Either 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(); + + var capabilityFilter1 = new RequirementNodeFilterCapabilityDataDefinition(); + final var propertyFilterList = new ListDataDefinition(); + 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(); + 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 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 -- cgit 1.2.3-korg