diff options
author | franciscovila <javier.paradela.vila@est.tech> | 2023-05-30 17:03:07 +0100 |
---|---|---|
committer | Michael Morris <michael.morris@est.tech> | 2023-06-02 14:29:33 +0000 |
commit | b206d04b36eb66fc69c1ac96c700d19ca0fbbd37 (patch) | |
tree | 54bea30b1f70670b5763f1499eed8af55a45cd6d /common-be | |
parent | 38812c3d47f0426f1c4108145b696441da5a5c1f (diff) |
Support INDEX in node filter tosca functions
Issue-ID: SDC-4517
Signed-off-by: franciscovila <javier.paradela.vila@est.tech>
Change-Id: I36e33821ef72c3375d9525513f2394b9b772c696
Diffstat (limited to 'common-be')
3 files changed, 105 insertions, 34 deletions
diff --git a/common-be/src/main/java/org/openecomp/sdc/be/utils/PropertyFilterConstraintDataDefinitionHelper.java b/common-be/src/main/java/org/openecomp/sdc/be/utils/PropertyFilterConstraintDataDefinitionHelper.java index 63c6781b94..6377152a13 100644 --- a/common-be/src/main/java/org/openecomp/sdc/be/utils/PropertyFilterConstraintDataDefinitionHelper.java +++ b/common-be/src/main/java/org/openecomp/sdc/be/utils/PropertyFilterConstraintDataDefinitionHelper.java @@ -21,12 +21,16 @@ package org.openecomp.sdc.be.utils; +import java.util.ArrayList; import java.util.List; import java.util.Map; +import java.util.Objects; import java.util.Optional; import java.util.Set; +import java.util.stream.Collectors; import lombok.AccessLevel; import lombok.NoArgsConstructor; +import org.apache.commons.lang3.StringUtils; import org.openecomp.sdc.be.datatypes.elements.PropertyFilterConstraintDataDefinition; import org.openecomp.sdc.be.datatypes.elements.ToscaConcatFunction; import org.openecomp.sdc.be.datatypes.elements.ToscaFunction; @@ -39,7 +43,6 @@ import org.openecomp.sdc.be.datatypes.enums.FilterValueType; import org.openecomp.sdc.be.datatypes.enums.PropertyFilterTargetType; import org.openecomp.sdc.be.datatypes.enums.PropertySource; import org.openecomp.sdc.be.datatypes.tosca.ToscaGetFunctionType; -import org.openecomp.sdc.exception.InvalidArgumentException; import org.yaml.snakeyaml.Yaml; @NoArgsConstructor(access = AccessLevel.PRIVATE) @@ -125,53 +128,104 @@ public class PropertyFilterConstraintDataDefinitionHelper { private static Optional<ToscaFunction> readLegacyGetPropertyConstraintValue(Map<?, ?> filterValueAsMap, Object toscaFunctionType, ToscaFunctionType toscaFunctionType1) { - final var toscaGetFunction = new ToscaGetFunctionDataDefinition(); - toscaGetFunction.setFunctionType(ToscaGetFunctionType.fromToscaFunctionType(toscaFunctionType1) - .orElseThrow(() -> new InvalidArgumentException("Could not convert a ToscaFunctionType to a ToscaGetFunctionType")) + final ToscaGetFunctionDataDefinition toscaGetFunction = new ToscaGetFunctionDataDefinition(); + toscaGetFunction.setFunctionType( + toscaFunctionType.toString().equalsIgnoreCase(ToscaFunctionType.GET_PROPERTY.getName()) ? ToscaGetFunctionType.GET_PROPERTY : ToscaGetFunctionType.GET_ATTRIBUTE ); - final List<String> getFunctionValue; + final Object functionValueObj = null != filterValueAsMap.get(toscaFunctionType1) ? + filterValueAsMap.get(toscaFunctionType1) : filterValueAsMap.get(toscaFunctionType); + if (!(functionValueObj instanceof List)) { + return Optional.empty(); + } + final List<String> functionParameters; try { - getFunctionValue = (List<String>) filterValueAsMap.get(toscaFunctionType); - } catch (final Exception ignored) { - return Optional.of(toscaGetFunction); + functionParameters = ((List<Object>) functionValueObj).stream() + .map(object -> Objects.toString(object, null)) + .collect(Collectors.toList()); + } catch (final ClassCastException ignored) { + return Optional.empty(); } - if (!getFunctionValue.isEmpty()) { - final Optional<PropertySource> propertySource = PropertySource.findType(getFunctionValue.get(0)); - if (propertySource.isPresent()) { - toscaGetFunction.setPropertySource(propertySource.get()); - } else { - toscaGetFunction.setPropertySource(PropertySource.INSTANCE); - toscaGetFunction.setSourceName(getFunctionValue.get(0)); + if (functionParameters.size() < 2) { + return Optional.empty(); + } + final String propertySourceType = functionParameters.get(0); + final PropertySource propertySource = PropertySource.findType(propertySourceType).orElse(null); + if (propertySource == PropertySource.SELF) { + toscaGetFunction.setPropertySource(propertySource); + } else { + toscaGetFunction.setPropertySource(PropertySource.INSTANCE); + toscaGetFunction.setSourceName(propertySourceType); + } + List<String> propertySourceIndex = functionParameters.subList(1, functionParameters.size()); + List<String> propertySourcePath = new ArrayList<>(); + propertySourcePath.add((String)propertySourceIndex.get(0)); + if (propertySourceIndex.size() > 1 ) { + List<Object> indexParsedList = new ArrayList<Object>(); + List<String> indexObjectList = propertySourceIndex.subList(1,propertySourceIndex.size()); + boolean loopFlag = true; + for (String indexValue : indexObjectList) { + if (!indexValue.equalsIgnoreCase("INDEX") && !StringUtils.isNumeric(indexValue) && loopFlag) { + propertySourcePath.add(indexValue); + } else { + loopFlag = false; + if (StringUtils.isNumeric(indexValue)) { + indexParsedList.add(Integer.parseInt(indexValue)); + } else { + indexParsedList.add(indexValue); + } + } } - final List<String> propertyPathFromSource = getFunctionValue.subList(1, getFunctionValue.size()); - toscaGetFunction.setPropertyPathFromSource(propertyPathFromSource); - toscaGetFunction.setPropertyName(propertyPathFromSource.get(propertyPathFromSource.size() - 1)); + toscaGetFunction.setToscaIndexList(indexParsedList); } + toscaGetFunction.setPropertyPathFromSource(propertySourcePath); + final String propertyName = toscaGetFunction.getPropertyPathFromSource().get(toscaGetFunction.getPropertyPathFromSource().size() - 1); + toscaGetFunction.setPropertyName(propertyName); return Optional.of(toscaGetFunction); } private static Optional<ToscaFunction> readLegacyGetInputConstraintValue(Map<?, ?> filterValueAsMap, Object toscaFunctionType) { - final var toscaGetFunction = new ToscaGetFunctionDataDefinition(); + final ToscaGetFunctionDataDefinition toscaGetFunction = new ToscaGetFunctionDataDefinition(); toscaGetFunction.setFunctionType(ToscaGetFunctionType.GET_INPUT); - final List<String> getFunctionValue; - final Object valueAsObject = filterValueAsMap.get(toscaFunctionType); - if (valueAsObject instanceof String) { - getFunctionValue = List.of((String) valueAsObject); - } else if (valueAsObject instanceof List) { + toscaGetFunction.setPropertySource(PropertySource.SELF); + final Object functionValueObj = filterValueAsMap.get(toscaFunctionType); + if (!(functionValueObj instanceof List) && !(functionValueObj instanceof String)) { + return Optional.empty(); + } + if (functionValueObj instanceof String) { + toscaGetFunction.setPropertyPathFromSource(List.of((String) functionValueObj)); + } else { + final List<String> functionParameters; try { - getFunctionValue = (List<String>) filterValueAsMap.get(toscaFunctionType); - } catch (final Exception ignored) { + functionParameters = ((List<Object>) functionValueObj).stream() + .map(object -> Objects.toString(object, null)) + .collect(Collectors.toList()); + } catch (final ClassCastException ignored) { return Optional.empty(); } - } else { - return Optional.empty(); - } - - toscaGetFunction.setPropertyPathFromSource(getFunctionValue); - if (!getFunctionValue.isEmpty()) { - toscaGetFunction.setPropertyName(getFunctionValue.get(getFunctionValue.size() - 1)); + List<String> propertySourcePath = new ArrayList<>(); + propertySourcePath.add((String)functionParameters.get(0)); + if (functionParameters.size() > 1 ) { + List<Object> indexParsedList = new ArrayList<Object>(); + List<String> indexObjectList = functionParameters.subList(1,functionParameters.size()); + boolean loopFlag = true; + for (String indexValue : indexObjectList) { + if (!indexValue.equalsIgnoreCase("INDEX") && !StringUtils.isNumeric(indexValue) && loopFlag) { + propertySourcePath.add(indexValue); + } else { + loopFlag = false; + if (StringUtils.isNumeric(indexValue)) { + indexParsedList.add(Integer.parseInt(indexValue)); + } else { + indexParsedList.add(indexValue); + } + } + } + toscaGetFunction.setToscaIndexList(indexParsedList); + } + toscaGetFunction.setPropertyPathFromSource(propertySourcePath); } - toscaGetFunction.setPropertySource(PropertySource.SELF); + final String propertyName = toscaGetFunction.getPropertyPathFromSource().get(toscaGetFunction.getPropertyPathFromSource().size() - 1); + toscaGetFunction.setPropertyName(propertyName); return Optional.of(toscaGetFunction); } diff --git a/common-be/src/test/java/org/openecomp/sdc/be/utils/PropertyFilterConstraintDataDefinitionHelperTest.java b/common-be/src/test/java/org/openecomp/sdc/be/utils/PropertyFilterConstraintDataDefinitionHelperTest.java index 86548fa1d5..5684db2332 100644 --- a/common-be/src/test/java/org/openecomp/sdc/be/utils/PropertyFilterConstraintDataDefinitionHelperTest.java +++ b/common-be/src/test/java/org/openecomp/sdc/be/utils/PropertyFilterConstraintDataDefinitionHelperTest.java @@ -77,6 +77,17 @@ class PropertyFilterConstraintDataDefinitionHelperTest { } @Test + void convertLegacyConstraintGetInputFromSelfTest() throws IOException { + final PropertyFilterConstraintDataDefinition propertyFilterConstraint = + PropertyFilterConstraintDataDefinitionHelper.convertLegacyConstraint(readConstraintFile("legacy-get_input-from-self.yaml")); + assertPropertyFilterConstraint(propertyFilterConstraint, "flavour_id", null, ConstraintType.EQUAL, FilterValueType.GET_INPUT); + assertTrue(propertyFilterConstraint.getValue() instanceof ToscaGetFunctionDataDefinition); + final var toscaGetFunction = (ToscaGetFunctionDataDefinition) propertyFilterConstraint.getValue(); + assertToscaGetFunction(toscaGetFunction, ToscaFunctionType.GET_INPUT, ToscaGetFunctionType.GET_INPUT, PropertySource.SELF, + List.of("SELF", "input", "subInput"), "subInput", null); + } + + @Test void convertLegacyConstraintGetAttributeFromInstanceTest() throws IOException { final PropertyFilterConstraintDataDefinition propertyFilterConstraint = PropertyFilterConstraintDataDefinitionHelper.convertLegacyConstraint(readConstraintFile("legacy-get_attribute-from-instance.yaml")); diff --git a/common-be/src/test/resources/nodeFilter/constraints/legacy-get_input-from-self.yaml b/common-be/src/test/resources/nodeFilter/constraints/legacy-get_input-from-self.yaml new file mode 100644 index 0000000000..a12444e8c1 --- /dev/null +++ b/common-be/src/test/resources/nodeFilter/constraints/legacy-get_input-from-self.yaml @@ -0,0 +1,6 @@ +flavour_id: + equal: + get_input: + - SELF + - input + - subInput
\ No newline at end of file |