diff options
author | andre.schmid <andre.schmid@est.tech> | 2022-08-10 14:50:08 +0100 |
---|---|---|
committer | Vasyl Razinkov <vasyl.razinkov@est.tech> | 2022-09-08 18:24:44 +0000 |
commit | 92b18f188105d5ba4b2c469cdfaedc7d2953d593 (patch) | |
tree | df7c7562faa99a76b0e6b5bc079de8d514b35006 /common-be | |
parent | c0c2637f201f488a74cb1916f05eece0cc207e9d (diff) |
Support TOSCA functions in Node Filters
Adds support to use tosca functions as value in the node property
filters and substitution filters
Change-Id: Id242691cc9ddd233245b58f052b9f0e2c7bbd66b
Issue-ID: SDC-4128
Signed-off-by: André Schmid <andre.schmid@est.tech>
Diffstat (limited to 'common-be')
31 files changed, 1009 insertions, 29 deletions
diff --git a/common-be/pom.xml b/common-be/pom.xml index 7c0d5fd71c..176fdc5dc1 100644 --- a/common-be/pom.xml +++ b/common-be/pom.xml @@ -13,13 +13,17 @@ </parent> <dependencies> - <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>${jackson.version}</version> </dependency> <dependency> + <groupId>com.fasterxml.jackson.core</groupId> + <artifactId>jackson-annotations</artifactId> + <version>${jackson-annotations.version}</version> + </dependency> + <dependency> <groupId>io.minio</groupId> <artifactId>minio</artifactId> <version>${minio.version}</version> diff --git a/common-be/src/main/java/org/openecomp/sdc/be/datatypes/elements/CINodeFilterDataDefinition.java b/common-be/src/main/java/org/openecomp/sdc/be/datatypes/elements/CINodeFilterDataDefinition.java index 22055e0620..bef9e52b8a 100644 --- a/common-be/src/main/java/org/openecomp/sdc/be/datatypes/elements/CINodeFilterDataDefinition.java +++ b/common-be/src/main/java/org/openecomp/sdc/be/datatypes/elements/CINodeFilterDataDefinition.java @@ -44,12 +44,12 @@ public class CINodeFilterDataDefinition extends ToscaDataDefinition implements S return getToscaPresentationValue(JsonPresentationFields.TOSCA_ID); } - public ListDataDefinition<RequirementNodeFilterPropertyDataDefinition> getProperties() { - return (ListDataDefinition<RequirementNodeFilterPropertyDataDefinition>) getToscaPresentationValue( + public ListDataDefinition<PropertyFilterDataDefinition> getProperties() { + return (ListDataDefinition<PropertyFilterDataDefinition>) getToscaPresentationValue( JsonPresentationFields.PROPERTIES); } - public void setProperties(ListDataDefinition<RequirementNodeFilterPropertyDataDefinition> properties) { + public void setProperties(ListDataDefinition<PropertyFilterDataDefinition> properties) { setToscaPresentationValue(JsonPresentationFields.PROPERTIES, properties); } diff --git a/common-be/src/main/java/org/openecomp/sdc/be/datatypes/elements/PropertyFilterConstraintDataDefinition.java b/common-be/src/main/java/org/openecomp/sdc/be/datatypes/elements/PropertyFilterConstraintDataDefinition.java new file mode 100644 index 0000000000..9284b530eb --- /dev/null +++ b/common-be/src/main/java/org/openecomp/sdc/be/datatypes/elements/PropertyFilterConstraintDataDefinition.java @@ -0,0 +1,41 @@ +/* + * - + * ============LICENSE_START======================================================= + * Copyright (C) 2022 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.datatypes.elements; + +import com.fasterxml.jackson.databind.annotation.JsonDeserialize; +import lombok.Data; +import lombok.NoArgsConstructor; +import org.openecomp.sdc.be.datatypes.enums.ConstraintType; +import org.openecomp.sdc.be.datatypes.enums.FilterValueType; +import org.openecomp.sdc.be.datatypes.enums.PropertyFilterTargetType; + +@Data +@NoArgsConstructor +@JsonDeserialize(using = PropertyFilterConstraintDataDefinitionJsonDeserializer.class) +public class PropertyFilterConstraintDataDefinition { + private String propertyName; + private String capabilityName; + private PropertyFilterTargetType targetType; + private ConstraintType operator; + private FilterValueType valueType; + private Object value; +} diff --git a/common-be/src/main/java/org/openecomp/sdc/be/datatypes/elements/PropertyFilterConstraintDataDefinitionJsonDeserializer.java b/common-be/src/main/java/org/openecomp/sdc/be/datatypes/elements/PropertyFilterConstraintDataDefinitionJsonDeserializer.java new file mode 100644 index 0000000000..a767133a4c --- /dev/null +++ b/common-be/src/main/java/org/openecomp/sdc/be/datatypes/elements/PropertyFilterConstraintDataDefinitionJsonDeserializer.java @@ -0,0 +1,107 @@ +/* + * - + * ============LICENSE_START======================================================= + * Copyright (C) 2022 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.datatypes.elements; + +import com.fasterxml.jackson.core.JsonParser; +import com.fasterxml.jackson.databind.DeserializationContext; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.deser.std.StdDeserializer; +import java.io.IOException; +import java.util.List; +import java.util.Map; +import org.openecomp.sdc.be.datatypes.enums.ConstraintType; +import org.openecomp.sdc.be.datatypes.enums.FilterValueType; +import org.openecomp.sdc.be.datatypes.enums.PropertyFilterTargetType; +import org.openecomp.sdc.be.utils.PropertyFilterConstraintDataDefinitionHelper; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class PropertyFilterConstraintDataDefinitionJsonDeserializer extends StdDeserializer<PropertyFilterConstraintDataDefinition> { + + private static final Logger LOGGER = LoggerFactory.getLogger(PropertyFilterConstraintDataDefinitionJsonDeserializer.class); + private static final String COULD_NOT_PARSE_CLASS = "Could not parse {} value as {}"; + + public PropertyFilterConstraintDataDefinitionJsonDeserializer() { + this(null); + } + + public PropertyFilterConstraintDataDefinitionJsonDeserializer(Class<?> vc) { + super(vc); + } + + @Override + public PropertyFilterConstraintDataDefinition deserialize(final JsonParser jsonParser, final DeserializationContext context) throws IOException { + final JsonNode node = jsonParser.getCodec().readTree(jsonParser); + + if (node.isTextual()) { + return PropertyFilterConstraintDataDefinitionHelper.convertLegacyConstraint(node.asText()); + } + + final var propertyFilterConstraint = new PropertyFilterConstraintDataDefinition(); + if (node.get("propertyName") != null) { + propertyFilterConstraint.setPropertyName(node.get("propertyName").asText()); + } + if (node.get("capabilityName") != null) { + propertyFilterConstraint.setCapabilityName(node.get("capabilityName").asText()); + } + if (node.get("targetType") != null) { + propertyFilterConstraint.setTargetType(PropertyFilterTargetType.valueOf(node.get("targetType").asText())); + } + if (node.get("operator") != null) { + propertyFilterConstraint.setOperator(ConstraintType.valueOf(node.get("operator").asText())); + } + if (node.get("valueType") != null) { + propertyFilterConstraint.setValueType(FilterValueType.valueOf(node.get("valueType").asText())); + } + propertyFilterConstraint.setValue(deserializeValue(node.get("value"))); + + return propertyFilterConstraint; + } + + private Object deserializeValue(final JsonNode value) { + final ObjectMapper objectMapper = new ObjectMapper(); + try { + return objectMapper.treeToValue(value, ToscaFunction.class); + } catch (final Exception e) { + LOGGER.debug(COULD_NOT_PARSE_CLASS, PropertyFilterConstraintDataDefinition.class.getName(), ToscaFunction.class.getName(), e); + } + try { + return objectMapper.treeToValue(value, Map.class); + } catch (final Exception e) { + LOGGER.debug(COULD_NOT_PARSE_CLASS, PropertyFilterConstraintDataDefinition.class.getName(), Map.class.getName(), e); + } + try { + return objectMapper.treeToValue(value, List.class); + } catch (final Exception e) { + LOGGER.debug(COULD_NOT_PARSE_CLASS, PropertyFilterConstraintDataDefinition.class.getName(), List.class.getName(), e); + } + try { + return objectMapper.treeToValue(value, String.class); + } catch (final Exception e) { + LOGGER.debug(COULD_NOT_PARSE_CLASS, PropertyFilterConstraintDataDefinition.class.getName(), String.class.getName(), e); + } + + return null; + } + +} diff --git a/common-be/src/main/java/org/openecomp/sdc/be/datatypes/elements/RequirementNodeFilterPropertyDataDefinition.java b/common-be/src/main/java/org/openecomp/sdc/be/datatypes/elements/PropertyFilterDataDefinition.java index fff5dce9da..decdc6ea45 100644 --- a/common-be/src/main/java/org/openecomp/sdc/be/datatypes/elements/RequirementNodeFilterPropertyDataDefinition.java +++ b/common-be/src/main/java/org/openecomp/sdc/be/datatypes/elements/PropertyFilterDataDefinition.java @@ -15,19 +15,24 @@ */ package org.openecomp.sdc.be.datatypes.elements; -import org.openecomp.sdc.be.datatypes.enums.JsonPresentationFields; -import org.openecomp.sdc.be.datatypes.tosca.ToscaDataDefinition; - import java.io.Serializable; import java.util.List; +import org.apache.commons.collections4.CollectionUtils; +import org.openecomp.sdc.be.datatypes.enums.JsonPresentationFields; +import org.openecomp.sdc.be.datatypes.tosca.ToscaDataDefinition; -public class RequirementNodeFilterPropertyDataDefinition extends ToscaDataDefinition implements Serializable { +public class PropertyFilterDataDefinition extends ToscaDataDefinition implements Serializable { - public List<String> getConstraints() { - return (List<String>) getToscaPresentationValue(JsonPresentationFields.PROPERTY_FILTER_CONSTRAINT); + public List<PropertyFilterConstraintDataDefinition> getConstraints() { + final List<PropertyFilterConstraintDataDefinition> constraintList = + (List<PropertyFilterConstraintDataDefinition>) getToscaPresentationValue(JsonPresentationFields.PROPERTY_FILTER_CONSTRAINT); + if (CollectionUtils.isEmpty(constraintList)) { + return List.of(); + } + return constraintList; } - public void setConstraints(List<String> constraints) { + public void setConstraints(final List<PropertyFilterConstraintDataDefinition> constraints) { setToscaPresentationValue(JsonPresentationFields.PROPERTY_FILTER_CONSTRAINT, constraints); } @@ -38,4 +43,5 @@ public class RequirementNodeFilterPropertyDataDefinition extends ToscaDataDefini public void setName(String name) { setToscaPresentationValue(JsonPresentationFields.PROPERTY_FILTER_NAME, name); } + } diff --git a/common-be/src/main/java/org/openecomp/sdc/be/datatypes/elements/RequirementNodeFilterCapabilityDataDefinition.java b/common-be/src/main/java/org/openecomp/sdc/be/datatypes/elements/RequirementNodeFilterCapabilityDataDefinition.java index 34dc43760f..637bf5f646 100644 --- a/common-be/src/main/java/org/openecomp/sdc/be/datatypes/elements/RequirementNodeFilterCapabilityDataDefinition.java +++ b/common-be/src/main/java/org/openecomp/sdc/be/datatypes/elements/RequirementNodeFilterCapabilityDataDefinition.java @@ -37,12 +37,12 @@ public class RequirementNodeFilterCapabilityDataDefinition extends ToscaDataDefi setToscaPresentationValue(JsonPresentationFields.NAME, name); } - public ListDataDefinition<RequirementNodeFilterPropertyDataDefinition> getProperties() { - return (ListDataDefinition<RequirementNodeFilterPropertyDataDefinition>) getToscaPresentationValue( + public ListDataDefinition<PropertyFilterDataDefinition> getProperties() { + return (ListDataDefinition<PropertyFilterDataDefinition>) getToscaPresentationValue( JsonPresentationFields.PROPERTIES); } - public void setProperties(ListDataDefinition<RequirementNodeFilterPropertyDataDefinition> properties) { + public void setProperties(ListDataDefinition<PropertyFilterDataDefinition> properties) { setToscaPresentationValue(JsonPresentationFields.PROPERTIES, properties); } } diff --git a/common-be/src/main/java/org/openecomp/sdc/be/datatypes/elements/RequirementSubstitutionFilterCapabilityDataDefinition.java b/common-be/src/main/java/org/openecomp/sdc/be/datatypes/elements/RequirementSubstitutionFilterCapabilityDataDefinition.java index 242eb490ef..d5c1525f55 100644 --- a/common-be/src/main/java/org/openecomp/sdc/be/datatypes/elements/RequirementSubstitutionFilterCapabilityDataDefinition.java +++ b/common-be/src/main/java/org/openecomp/sdc/be/datatypes/elements/RequirementSubstitutionFilterCapabilityDataDefinition.java @@ -39,12 +39,12 @@ public class RequirementSubstitutionFilterCapabilityDataDefinition extends Tosca setToscaPresentationValue(JsonPresentationFields.NAME, name); } - public ListDataDefinition<RequirementNodeFilterPropertyDataDefinition> getProperties() { - return (ListDataDefinition<RequirementNodeFilterPropertyDataDefinition>) getToscaPresentationValue( + public ListDataDefinition<PropertyFilterDataDefinition> getProperties() { + return (ListDataDefinition<PropertyFilterDataDefinition>) getToscaPresentationValue( JsonPresentationFields.PROPERTIES); } - public void setProperties(final ListDataDefinition<RequirementNodeFilterPropertyDataDefinition> properties) { + public void setProperties(final ListDataDefinition<PropertyFilterDataDefinition> properties) { setToscaPresentationValue(JsonPresentationFields.PROPERTIES, properties); } } diff --git a/common-be/src/main/java/org/openecomp/sdc/be/datatypes/elements/SubstitutionFilterDataDefinition.java b/common-be/src/main/java/org/openecomp/sdc/be/datatypes/elements/SubstitutionFilterDataDefinition.java index beb5aac1d0..36c125a27b 100644 --- a/common-be/src/main/java/org/openecomp/sdc/be/datatypes/elements/SubstitutionFilterDataDefinition.java +++ b/common-be/src/main/java/org/openecomp/sdc/be/datatypes/elements/SubstitutionFilterDataDefinition.java @@ -46,12 +46,12 @@ public class SubstitutionFilterDataDefinition extends ToscaDataDefinition implem return getToscaPresentationValue(JsonPresentationFields.TOSCA_ID); } - public ListDataDefinition<RequirementSubstitutionFilterPropertyDataDefinition> getProperties() { - return (ListDataDefinition<RequirementSubstitutionFilterPropertyDataDefinition>) getToscaPresentationValue( + public ListDataDefinition<SubstitutionFilterPropertyDataDefinition> getProperties() { + return (ListDataDefinition<SubstitutionFilterPropertyDataDefinition>) getToscaPresentationValue( JsonPresentationFields.PROPERTIES); } - public void setProperties(final ListDataDefinition<RequirementSubstitutionFilterPropertyDataDefinition> properties) { + public void setProperties(final ListDataDefinition<SubstitutionFilterPropertyDataDefinition> properties) { setToscaPresentationValue(JsonPresentationFields.PROPERTIES, properties); } diff --git a/common-be/src/main/java/org/openecomp/sdc/be/datatypes/elements/RequirementSubstitutionFilterPropertyDataDefinition.java b/common-be/src/main/java/org/openecomp/sdc/be/datatypes/elements/SubstitutionFilterPropertyDataDefinition.java index b1b53bb4ae..977c374712 100644 --- a/common-be/src/main/java/org/openecomp/sdc/be/datatypes/elements/RequirementSubstitutionFilterPropertyDataDefinition.java +++ b/common-be/src/main/java/org/openecomp/sdc/be/datatypes/elements/SubstitutionFilterPropertyDataDefinition.java @@ -20,24 +20,25 @@ package org.openecomp.sdc.be.datatypes.elements; import java.io.Serializable; import java.util.List; -import org.openecomp.sdc.be.datatypes.enums.JsonPresentationFields; import org.openecomp.sdc.be.datatypes.tosca.ToscaDataDefinition; -public class RequirementSubstitutionFilterPropertyDataDefinition extends ToscaDataDefinition implements Serializable { +public class SubstitutionFilterPropertyDataDefinition extends ToscaDataDefinition implements Serializable { - public List<String> getConstraints() { - return (List<String>) getToscaPresentationValue(JsonPresentationFields.PROPERTY_FILTER_CONSTRAINT); + private final PropertyFilterDataDefinition propertyFilterDataDefinition = new PropertyFilterDataDefinition(); + + public List<PropertyFilterConstraintDataDefinition> getConstraints() { + return propertyFilterDataDefinition.getConstraints(); } - public void setConstraints(final List<String> constraints) { - setToscaPresentationValue(JsonPresentationFields.PROPERTY_FILTER_CONSTRAINT, constraints); + public void setConstraints(final List<PropertyFilterConstraintDataDefinition> constraints) { + propertyFilterDataDefinition.setConstraints(constraints); } public String getName() { - return (String) getToscaPresentationValue(JsonPresentationFields.PROPERTY_FILTER_NAME); + return propertyFilterDataDefinition.getName(); } public void setName(final String name) { - setToscaPresentationValue(JsonPresentationFields.PROPERTY_FILTER_NAME, name); + propertyFilterDataDefinition.setName(name); } } diff --git a/common-be/src/main/java/org/openecomp/sdc/be/datatypes/elements/ToscaFunction.java b/common-be/src/main/java/org/openecomp/sdc/be/datatypes/elements/ToscaFunction.java index 40664a133c..81b4ec73a9 100644 --- a/common-be/src/main/java/org/openecomp/sdc/be/datatypes/elements/ToscaFunction.java +++ b/common-be/src/main/java/org/openecomp/sdc/be/datatypes/elements/ToscaFunction.java @@ -26,7 +26,25 @@ import com.fasterxml.jackson.databind.annotation.JsonDeserialize; @JsonDeserialize(using = ToscaFunctionJsonDeserializer.class) public interface ToscaFunction { + /** + * Gets the function type. + * + * @return the function type. + */ ToscaFunctionType getType(); + + /** + * Builds the tosca function value as string. + * + * @return the function value as string + */ String getValue(); + /** + * Returns the value mapped as a JSON object. + * + * @return the value as JSON object. + */ + Object getJsonObjectValue(); + } diff --git a/common-be/src/main/java/org/openecomp/sdc/be/datatypes/elements/ToscaFunctionJsonDeserializer.java b/common-be/src/main/java/org/openecomp/sdc/be/datatypes/elements/ToscaFunctionJsonDeserializer.java index 363af1cdeb..c262699cc2 100644 --- a/common-be/src/main/java/org/openecomp/sdc/be/datatypes/elements/ToscaFunctionJsonDeserializer.java +++ b/common-be/src/main/java/org/openecomp/sdc/be/datatypes/elements/ToscaFunctionJsonDeserializer.java @@ -146,8 +146,11 @@ public class ToscaFunctionJsonDeserializer extends StdDeserializer<ToscaFunction final var toscaConcatFunction = new ToscaConcatFunction(); final List<ToscaFunctionParameter> functionParameterList = new ArrayList<>(); final JsonNode parametersNode = concatFunctionJsonNode.get("parameters"); + if (parametersNode == null) { + return toscaConcatFunction; + } if (!parametersNode.isArray()) { - throw context.instantiationException(List.class, ""); + throw context.instantiationException(List.class, "Expecting an array for the 'parameters' entry"); } for (final JsonNode parameterNode : parametersNode) { final JsonNode typeJsonNode = parameterNode.get("type"); diff --git a/common-be/src/main/java/org/openecomp/sdc/be/datatypes/elements/ToscaFunctionType.java b/common-be/src/main/java/org/openecomp/sdc/be/datatypes/elements/ToscaFunctionType.java index 4579ac2410..2636c4f6d7 100644 --- a/common-be/src/main/java/org/openecomp/sdc/be/datatypes/elements/ToscaFunctionType.java +++ b/common-be/src/main/java/org/openecomp/sdc/be/datatypes/elements/ToscaFunctionType.java @@ -25,6 +25,7 @@ import java.util.Arrays; import java.util.Optional; import lombok.AllArgsConstructor; import lombok.Getter; +import org.apache.commons.lang3.StringUtils; @AllArgsConstructor @Getter @@ -40,6 +41,9 @@ public enum ToscaFunctionType { private final String name; public static Optional<ToscaFunctionType> findType(final String functionType) { + if (StringUtils.isBlank(functionType)) { + return Optional.empty(); + } return Arrays.stream(values()).filter(toscaFunctionType -> toscaFunctionType.getName().equalsIgnoreCase(functionType)).findFirst(); } } diff --git a/common-be/src/main/java/org/openecomp/sdc/be/datatypes/elements/ToscaGetFunctionDataDefinition.java b/common-be/src/main/java/org/openecomp/sdc/be/datatypes/elements/ToscaGetFunctionDataDefinition.java index f19217e69c..4fe3f3ae13 100644 --- a/common-be/src/main/java/org/openecomp/sdc/be/datatypes/elements/ToscaGetFunctionDataDefinition.java +++ b/common-be/src/main/java/org/openecomp/sdc/be/datatypes/elements/ToscaGetFunctionDataDefinition.java @@ -21,6 +21,7 @@ package org.openecomp.sdc.be.datatypes.elements; +import com.fasterxml.jackson.annotation.JsonIgnore; import com.google.gson.Gson; import java.util.ArrayList; import java.util.List; @@ -47,6 +48,7 @@ public class ToscaGetFunctionDataDefinition implements ToscaFunction, ToscaFunct //necessary for JSON conversions } + @JsonIgnore public boolean isSubProperty() { return propertyPathFromSource != null && propertyPathFromSource.size() > 1; } @@ -58,6 +60,7 @@ public class ToscaGetFunctionDataDefinition implements ToscaFunction, ToscaFunct return new Gson().toJson(getJsonObjectValue()); } + @JsonIgnore @Override public Object getJsonObjectValue() { if (functionType == null) { @@ -126,6 +129,7 @@ public class ToscaGetFunctionDataDefinition implements ToscaFunction, ToscaFunct } } + @JsonIgnore @Override public String getValue() { return this.generatePropertyValue(); diff --git a/common-be/src/main/java/org/openecomp/sdc/be/datatypes/enums/ConstraintType.java b/common-be/src/main/java/org/openecomp/sdc/be/datatypes/enums/ConstraintType.java new file mode 100644 index 0000000000..cef310ea01 --- /dev/null +++ b/common-be/src/main/java/org/openecomp/sdc/be/datatypes/enums/ConstraintType.java @@ -0,0 +1,71 @@ +/* + * - + * ============LICENSE_START======================================================= + * Copyright (C) 2022 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.datatypes.enums; + +import java.util.Arrays; +import java.util.Collections; +import java.util.List; +import java.util.Optional; +import java.util.Set; +import lombok.Getter; + +@Getter +public enum ConstraintType { + EQUAL("equal"), + IN_RANGE("in_range", "inRange"), + GREATER_THAN("greater_than", "greaterThan"), + GREATER_OR_EQUAL("greater_or_equal", "greaterOrEqual"), + LESS_OR_EQUAL("less_or_equal", "lessOrEqual"), + LENGTH("length"), + MIN_LENGTH("min_length", "minLength"), + MAX_LENGTH("max_length", "maxLength"), + VALID_VALUES("valid_values", "validValues"), + LESS_THAN("less_than", "lessThan"), + SCHEMA("schema"); + + private static final Set<ConstraintType> comparableConstraints = Set.of(ConstraintType.GREATER_THAN, ConstraintType.LESS_THAN); + private final String type; + private final List<String> typeAlias; + + + ConstraintType(final String type, final String... typeAliases) { + this.type = type; + if (typeAliases == null) { + this.typeAlias = Collections.emptyList(); + } else { + this.typeAlias = Arrays.asList(typeAliases); + } + } + + public static Optional<ConstraintType> findByType(final String type) { + if (type == null) { + return Optional.empty(); + } + return Arrays.stream(ConstraintType.values()) + .filter(constraintType -> constraintType.getType().equals(type) || constraintType.getTypeAlias().contains(type)) + .findFirst(); + } + + public boolean isComparable() { + return comparableConstraints.contains(this); + } + +} diff --git a/common-be/src/main/java/org/openecomp/sdc/be/datatypes/enums/FilterValueType.java b/common-be/src/main/java/org/openecomp/sdc/be/datatypes/enums/FilterValueType.java new file mode 100644 index 0000000000..cacc4b1bac --- /dev/null +++ b/common-be/src/main/java/org/openecomp/sdc/be/datatypes/enums/FilterValueType.java @@ -0,0 +1,53 @@ +/* + * - + * ============LICENSE_START======================================================= + * Copyright (C) 2022 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.datatypes.enums; + +import java.util.Arrays; +import java.util.Optional; +import lombok.AllArgsConstructor; +import lombok.Getter; +import org.apache.commons.lang3.StringUtils; + +@AllArgsConstructor +@Getter +public enum FilterValueType { + STATIC("static", "static"), + GET_PROPERTY("get_property", "property"), + GET_INPUT("get_input", "service_input"), + GET_ATTRIBUTE("get_attribute", null), + YAML("yaml", null), + CONCAT("concat", null); + + private final String name; + private final String legacyName; + + public static Optional<FilterValueType> findByName(final String name) { + if (StringUtils.isEmpty(name)) { + return Optional.empty(); + } + return Arrays.stream(FilterValueType.values()).filter(filterValueType -> + filterValueType.getName().equalsIgnoreCase(name) || + (filterValueType.getLegacyName() != null && filterValueType.getLegacyName().equalsIgnoreCase(name)) + ).findFirst(); + } + +} diff --git a/common-be/src/main/java/org/openecomp/sdc/be/datatypes/enums/PropertyFilterTargetType.java b/common-be/src/main/java/org/openecomp/sdc/be/datatypes/enums/PropertyFilterTargetType.java new file mode 100644 index 0000000000..df472508fe --- /dev/null +++ b/common-be/src/main/java/org/openecomp/sdc/be/datatypes/enums/PropertyFilterTargetType.java @@ -0,0 +1,26 @@ +/* + * - + * ============LICENSE_START======================================================= + * Copyright (C) 2022 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.datatypes.enums; + +public enum PropertyFilterTargetType { + CAPABILITY, PROPERTY +} 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 new file mode 100644 index 0000000000..63c6781b94 --- /dev/null +++ b/common-be/src/main/java/org/openecomp/sdc/be/utils/PropertyFilterConstraintDataDefinitionHelper.java @@ -0,0 +1,198 @@ +/* + * - + * ============LICENSE_START======================================================= + * Copyright (C) 2022 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.utils; + +import java.util.List; +import java.util.Map; +import java.util.Optional; +import java.util.Set; +import lombok.AccessLevel; +import lombok.NoArgsConstructor; +import org.openecomp.sdc.be.datatypes.elements.PropertyFilterConstraintDataDefinition; +import org.openecomp.sdc.be.datatypes.elements.ToscaConcatFunction; +import org.openecomp.sdc.be.datatypes.elements.ToscaFunction; +import org.openecomp.sdc.be.datatypes.elements.ToscaFunctionParameter; +import org.openecomp.sdc.be.datatypes.elements.ToscaFunctionType; +import org.openecomp.sdc.be.datatypes.elements.ToscaGetFunctionDataDefinition; +import org.openecomp.sdc.be.datatypes.elements.ToscaStringParameter; +import org.openecomp.sdc.be.datatypes.enums.ConstraintType; +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) +public class PropertyFilterConstraintDataDefinitionHelper { + + public static PropertyFilterConstraintDataDefinition convertLegacyConstraint(final String constraint) { + final var propertyFilterConstraint = new PropertyFilterConstraintDataDefinition(); + final Map<String, Object> constraintYaml = new Yaml().load(constraint); + final String propertyName = constraintYaml.keySet().iterator().next(); + propertyFilterConstraint.setPropertyName(propertyName); + final Map<String, Object> operatorYaml = (Map<String, Object>) constraintYaml.get(propertyName); + final String operator = operatorYaml.keySet().iterator().next(); + propertyFilterConstraint.setOperator(ConstraintType.findByType(operator).orElse(null)); + final Object valueYaml = operatorYaml.get(operator); + final Optional<ToscaFunction> toscaFunction = createToscaFunctionFromLegacyConstraintValue(valueYaml); + if (toscaFunction.isPresent()) { + propertyFilterConstraint.setValue(toscaFunction.get()); + } else { + propertyFilterConstraint.setValue(valueYaml); + } + propertyFilterConstraint.setValueType(detectValueType(valueYaml)); + propertyFilterConstraint.setTargetType(PropertyFilterTargetType.PROPERTY); + return propertyFilterConstraint; + } + + public static Optional<ToscaFunction> createToscaFunctionFromLegacyConstraintValue(final Object filterValue) { + if (!(filterValue instanceof Map)) { + return Optional.empty(); + } + final Map<?, ?> filterValueAsMap = (Map<?, ?>) filterValue; + final Set<?> keys = filterValueAsMap.keySet(); + if (keys.size() != 1) { + return Optional.empty(); + } + final Object toscaFunctionTypeObject = keys.iterator().next(); + if (!(toscaFunctionTypeObject instanceof String)) { + return Optional.empty(); + } + final ToscaFunctionType toscaFunctionType = ToscaFunctionType.findType((String) toscaFunctionTypeObject).orElse(null); + if (toscaFunctionType == null) { + return Optional.empty(); + } + switch (toscaFunctionType) { + case GET_INPUT: + return readLegacyGetInputConstraintValue(filterValueAsMap, toscaFunctionTypeObject); + case GET_ATTRIBUTE: + case GET_PROPERTY: + return readLegacyGetPropertyConstraintValue(filterValueAsMap, toscaFunctionTypeObject, toscaFunctionType); + case CONCAT: + return readLegacyConcatConstraintValue(filterValueAsMap, toscaFunctionTypeObject); + default: + return Optional.empty(); + } + } + + public static Optional<FilterValueType> convertFromToscaFunctionType(final ToscaFunctionType toscaFunctionType) { + return FilterValueType.findByName(toscaFunctionType.getName()); + } + + private static Optional<ToscaFunction> readLegacyConcatConstraintValue(Map<?, ?> filterValueAsMap, Object toscaFunctionType) { + final List<Object> concatValue; + try { + concatValue = (List<Object>) filterValueAsMap.get(toscaFunctionType); + } catch (final Exception ignored) { + return Optional.empty(); + } + if (concatValue.isEmpty()) { + return Optional.empty(); + } + final var toscaConcatFunction = new ToscaConcatFunction(); + for (Object parameter : concatValue) { + if (parameter instanceof String) { + final ToscaStringParameter toscaStringParameter = new ToscaStringParameter(); + toscaStringParameter.setValue((String) parameter); + toscaConcatFunction.addParameter(toscaStringParameter); + } else { + createToscaFunctionFromLegacyConstraintValue(parameter) + .ifPresent(toscaFunction -> toscaConcatFunction.addParameter((ToscaFunctionParameter) toscaFunction)); + } + } + return Optional.of(toscaConcatFunction); + } + + 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 List<String> getFunctionValue; + try { + getFunctionValue = (List<String>) filterValueAsMap.get(toscaFunctionType); + } catch (final Exception ignored) { + return Optional.of(toscaGetFunction); + } + 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)); + } + final List<String> propertyPathFromSource = getFunctionValue.subList(1, getFunctionValue.size()); + toscaGetFunction.setPropertyPathFromSource(propertyPathFromSource); + toscaGetFunction.setPropertyName(propertyPathFromSource.get(propertyPathFromSource.size() - 1)); + } + return Optional.of(toscaGetFunction); + } + + private static Optional<ToscaFunction> readLegacyGetInputConstraintValue(Map<?, ?> filterValueAsMap, Object toscaFunctionType) { + final var 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) { + try { + getFunctionValue = (List<String>) filterValueAsMap.get(toscaFunctionType); + } catch (final Exception ignored) { + return Optional.empty(); + } + } else { + return Optional.empty(); + } + + toscaGetFunction.setPropertyPathFromSource(getFunctionValue); + if (!getFunctionValue.isEmpty()) { + toscaGetFunction.setPropertyName(getFunctionValue.get(getFunctionValue.size() - 1)); + } + toscaGetFunction.setPropertySource(PropertySource.SELF); + return Optional.of(toscaGetFunction); + } + + private static FilterValueType detectValueType(final Object value) { + if (value instanceof Map) { + final Map<?, ?> valueAsMap = (Map<?, ?>) value; + if (valueAsMap.containsKey(ToscaFunctionType.CONCAT.getName())) { + return FilterValueType.CONCAT; + } + if (valueAsMap.containsKey(ToscaFunctionType.GET_ATTRIBUTE.getName())) { + return FilterValueType.GET_ATTRIBUTE; + } + if (valueAsMap.containsKey(ToscaFunctionType.GET_PROPERTY.getName())) { + return FilterValueType.GET_PROPERTY; + } + if (valueAsMap.containsKey(ToscaFunctionType.GET_INPUT.getName())) { + return FilterValueType.GET_INPUT; + } + } + + return FilterValueType.STATIC; + } + +} diff --git a/common-be/src/test/java/org/openecomp/sdc/be/datatypes/elements/PropertyFilterConstraintDataDefinitionJsonDeserializerTest.java b/common-be/src/test/java/org/openecomp/sdc/be/datatypes/elements/PropertyFilterConstraintDataDefinitionJsonDeserializerTest.java new file mode 100644 index 0000000000..d40c8f4862 --- /dev/null +++ b/common-be/src/test/java/org/openecomp/sdc/be/datatypes/elements/PropertyFilterConstraintDataDefinitionJsonDeserializerTest.java @@ -0,0 +1,101 @@ +/* + * - + * ============LICENSE_START======================================================= + * Copyright (C) 2022 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.datatypes.elements; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.List; +import org.junit.jupiter.api.Test; +import org.openecomp.sdc.be.datatypes.enums.ConstraintType; +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; + +class PropertyFilterConstraintDataDefinitionJsonDeserializerTest { + private static final Path TEST_RESOURCES_PATH = Path.of("src/test/resources/propertyFilterConstraintDataDefinitionDeserializer"); + + @Test + void testStaticPropertyFilter() throws IOException { + //given + final String propertyFilterAsString = Files.readString(TEST_RESOURCES_PATH.resolve("filter-constraint-static.json")); + //when + final PropertyFilterConstraintDataDefinition actualPropertyFilterConstraint = parseToscaFunction(propertyFilterAsString); + //then + assertEquals(FilterValueType.STATIC, actualPropertyFilterConstraint.getValueType()); + assertEquals(ConstraintType.EQUAL, actualPropertyFilterConstraint.getOperator()); + assertEquals(PropertyFilterTargetType.CAPABILITY, actualPropertyFilterConstraint.getTargetType()); + assertEquals("aCapability", actualPropertyFilterConstraint.getCapabilityName()); + assertEquals("aProperty", actualPropertyFilterConstraint.getPropertyName()); + assertEquals("aStaticValue", actualPropertyFilterConstraint.getValue()); + } + + @Test + void testGetInputToscaFunction() throws IOException { + //given + final String toscaGetInputFunction = Files.readString(TEST_RESOURCES_PATH.resolve("filter-constraint-get-input.json")); + //when + final PropertyFilterConstraintDataDefinition actualPropertyFilterConstraint = parseToscaFunction(toscaGetInputFunction); + //then + assertEquals(FilterValueType.GET_INPUT, actualPropertyFilterConstraint.getValueType()); + assertEquals(ConstraintType.GREATER_THAN, actualPropertyFilterConstraint.getOperator()); + assertEquals(PropertyFilterTargetType.PROPERTY, actualPropertyFilterConstraint.getTargetType()); + assertNull(actualPropertyFilterConstraint.getCapabilityName()); + assertEquals("aProperty", actualPropertyFilterConstraint.getPropertyName()); + assertTrue(actualPropertyFilterConstraint.getValue() instanceof ToscaGetFunctionDataDefinition); + final ToscaGetFunctionDataDefinition toscaGetFunction = (ToscaGetFunctionDataDefinition) actualPropertyFilterConstraint.getValue(); + assertEquals(ToscaFunctionType.GET_INPUT, toscaGetFunction.getType()); + assertEquals(ToscaGetFunctionType.GET_INPUT, toscaGetFunction.getFunctionType()); + assertEquals("aPropertyId", toscaGetFunction.getPropertyUniqueId()); + assertEquals("aProperty", toscaGetFunction.getPropertyName()); + assertEquals(PropertySource.SELF, toscaGetFunction.getPropertySource()); + assertEquals("aServiceId", toscaGetFunction.getSourceUniqueId()); + assertEquals("aService", toscaGetFunction.getSourceName()); + assertEquals(List.of("input", "subProperty"), toscaGetFunction.getPropertyPathFromSource()); + } + + @Test + void testLegacyPropertyFilter() throws IOException { + //given + final String legacyPropertyFilter = Files.readString(TEST_RESOURCES_PATH.resolve("filter-constraint-legacy.txt")); + //when + final PropertyFilterConstraintDataDefinition actualPropertyFilterConstraint = parseToscaFunction(legacyPropertyFilter); + //then + assertEquals(FilterValueType.STATIC, actualPropertyFilterConstraint.getValueType()); + assertEquals(ConstraintType.EQUAL, actualPropertyFilterConstraint.getOperator()); + assertEquals(PropertyFilterTargetType.PROPERTY, actualPropertyFilterConstraint.getTargetType()); + assertNull(actualPropertyFilterConstraint.getCapabilityName()); + assertEquals("propertyName", actualPropertyFilterConstraint.getPropertyName()); + assertEquals("aValue", actualPropertyFilterConstraint.getValue()); + } + + private PropertyFilterConstraintDataDefinition parseToscaFunction(final String propertyFilterConstraintAsJson) throws JsonProcessingException { + return new ObjectMapper().readValue(propertyFilterConstraintAsJson, PropertyFilterConstraintDataDefinition.class); + } +}
\ No newline at end of file diff --git a/common-be/src/test/java/org/openecomp/sdc/be/datatypes/enums/FilterValueTypeTest.java b/common-be/src/test/java/org/openecomp/sdc/be/datatypes/enums/FilterValueTypeTest.java new file mode 100644 index 0000000000..73df545468 --- /dev/null +++ b/common-be/src/test/java/org/openecomp/sdc/be/datatypes/enums/FilterValueTypeTest.java @@ -0,0 +1,75 @@ +/* + * - + * ============LICENSE_START======================================================= + * Copyright (C) 2022 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.datatypes.enums; + +import static org.junit.jupiter.api.Assertions.*; + +import java.util.Arrays; +import java.util.List; +import java.util.Optional; +import java.util.stream.Collectors; +import java.util.stream.Stream; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; + +class FilterValueTypeTest { + + @Test + void findByEmptyNameTest() { + assertTrue(FilterValueType.findByName(null).isEmpty()); + assertTrue(FilterValueType.findByName("").isEmpty()); + } + + @Test + void findByNameNotFoundTest() { + assertTrue(FilterValueType.findByName("thisNameDoesNotExist").isEmpty()); + } + + @ParameterizedTest(name = "{index}: {0} should be {1}") + @MethodSource("getValueTypeForFindByName") + void test(final String nameToFind, final FilterValueType filterValueType) { + final Optional<FilterValueType> actualFilterValueType = FilterValueType.findByName(nameToFind); + assertTrue(actualFilterValueType.isPresent()); + assertEquals(actualFilterValueType.get(), filterValueType); + } + + private static Stream<Arguments> getValueTypeForFindByName() { + final Stream<Arguments> allFilterValueTypeNameArguments = Arrays.stream(FilterValueType.values()) + .map(filterValueType -> Arguments.of(filterValueType.getName(), filterValueType)); + final Stream<Arguments> allFilterValueTypeNameIgnoreCaseArguments = Arrays.stream(FilterValueType.values()) + .map(filterValueType -> Arguments.of(filterValueType.getName().toUpperCase(), filterValueType)); + + final Stream<Arguments> legacyArguments = Stream.of( + Arguments.of(FilterValueType.GET_INPUT.getLegacyName(), FilterValueType.GET_INPUT), + Arguments.of(FilterValueType.GET_INPUT.getLegacyName().toUpperCase(), FilterValueType.GET_INPUT), + Arguments.of(FilterValueType.GET_PROPERTY.getLegacyName(), FilterValueType.GET_PROPERTY), + Arguments.of(FilterValueType.GET_PROPERTY.getLegacyName().toUpperCase(), FilterValueType.GET_PROPERTY) + ); + + return Stream.of(allFilterValueTypeNameIgnoreCaseArguments, allFilterValueTypeNameArguments, legacyArguments) + .reduce(Stream::concat) + .orElseGet(Stream::empty); + } + +}
\ No newline at end of file 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 new file mode 100644 index 0000000000..86548fa1d5 --- /dev/null +++ b/common-be/src/test/java/org/openecomp/sdc/be/utils/PropertyFilterConstraintDataDefinitionHelperTest.java @@ -0,0 +1,196 @@ +/* + * - + * ============LICENSE_START======================================================= + * Copyright (C) 2022 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.utils; + +import static org.junit.jupiter.api.Assertions.*; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.List; +import java.util.Map; +import java.util.Optional; +import org.junit.jupiter.api.Test; +import org.openecomp.sdc.be.datatypes.elements.PropertyFilterConstraintDataDefinition; +import org.openecomp.sdc.be.datatypes.elements.ToscaConcatFunction; +import org.openecomp.sdc.be.datatypes.elements.ToscaFunctionType; +import org.openecomp.sdc.be.datatypes.elements.ToscaGetFunctionDataDefinition; +import org.openecomp.sdc.be.datatypes.enums.ConstraintType; +import org.openecomp.sdc.be.datatypes.enums.FilterValueType; +import org.openecomp.sdc.be.datatypes.enums.PropertySource; +import org.openecomp.sdc.be.datatypes.tosca.ToscaGetFunctionType; + +class PropertyFilterConstraintDataDefinitionHelperTest { + + private static final Path RESOURCE_PATH = Path.of("src", "test", "resources", "nodeFilter", "constraints"); + + @Test + void convertLegacyConstraintGetInputTest() throws IOException { + final var propertyFilterConstraint = + PropertyFilterConstraintDataDefinitionHelper.convertLegacyConstraint(readConstraintFile("legacy-get_input.yaml")); + assertPropertyFilterConstraint(propertyFilterConstraint, "flavour_id", null, ConstraintType.GREATER_OR_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("inputName"), "inputName", null); + } + + @Test + void convertLegacyConstraintGetInputSubPathTest() throws IOException { + final PropertyFilterConstraintDataDefinition propertyFilterConstraint = + PropertyFilterConstraintDataDefinitionHelper.convertLegacyConstraint(readConstraintFile("legacy-get_input-subProperty.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("inputName", "inputSubProperty", "inputSubSubProperty"), "inputSubSubProperty", null); + } + + @Test + void convertLegacyConstraintGetPropertyFromInstanceTest() throws IOException { + final PropertyFilterConstraintDataDefinition propertyFilterConstraint = + PropertyFilterConstraintDataDefinitionHelper.convertLegacyConstraint(readConstraintFile("legacy-get_property-from-instance.yaml")); + assertPropertyFilterConstraint(propertyFilterConstraint, "flavour_id", null, ConstraintType.EQUAL, FilterValueType.GET_PROPERTY); + assertTrue(propertyFilterConstraint.getValue() instanceof ToscaGetFunctionDataDefinition); + final var toscaGetFunction = (ToscaGetFunctionDataDefinition) propertyFilterConstraint.getValue(); + assertToscaGetFunction(toscaGetFunction, ToscaFunctionType.GET_PROPERTY, ToscaGetFunctionType.GET_PROPERTY, PropertySource.INSTANCE, + List.of("property", "subProperty"), "subProperty", "Instance Name"); + } + + @Test + void convertLegacyConstraintGetAttributeFromInstanceTest() throws IOException { + final PropertyFilterConstraintDataDefinition propertyFilterConstraint = + PropertyFilterConstraintDataDefinitionHelper.convertLegacyConstraint(readConstraintFile("legacy-get_attribute-from-instance.yaml")); + assertPropertyFilterConstraint(propertyFilterConstraint, "flavour_id", null, ConstraintType.EQUAL, FilterValueType.GET_ATTRIBUTE); + assertTrue(propertyFilterConstraint.getValue() instanceof ToscaGetFunctionDataDefinition); + final var toscaGetFunction = (ToscaGetFunctionDataDefinition) propertyFilterConstraint.getValue(); + assertToscaGetFunction(toscaGetFunction, ToscaFunctionType.GET_ATTRIBUTE, ToscaGetFunctionType.GET_ATTRIBUTE, PropertySource.INSTANCE, + List.of("property", "subProperty"), "subProperty", "Instance Name"); + } + + + @Test + void convertLegacyConstraintGetPropertyFromSelfTest() throws IOException { + final PropertyFilterConstraintDataDefinition propertyFilterConstraint = + PropertyFilterConstraintDataDefinitionHelper.convertLegacyConstraint(readConstraintFile("legacy-get_property-from-self.yaml")); + assertPropertyFilterConstraint(propertyFilterConstraint, "flavour_id", null, ConstraintType.EQUAL, FilterValueType.GET_PROPERTY); + assertTrue(propertyFilterConstraint.getValue() instanceof ToscaGetFunctionDataDefinition); + final var toscaGetFunction = (ToscaGetFunctionDataDefinition) propertyFilterConstraint.getValue(); + assertToscaGetFunction(toscaGetFunction, ToscaFunctionType.GET_PROPERTY, ToscaGetFunctionType.GET_PROPERTY, PropertySource.SELF, + List.of("property", "subProperty"), "subProperty", null); + } + + @Test + void convertLegacyConstraintGetAttributeFromSelfTest() throws IOException { + final PropertyFilterConstraintDataDefinition propertyFilterConstraint = + PropertyFilterConstraintDataDefinitionHelper.convertLegacyConstraint(readConstraintFile("legacy-get_attribute-from-self.yaml")); + assertPropertyFilterConstraint(propertyFilterConstraint, "flavour_id", null, ConstraintType.EQUAL, FilterValueType.GET_ATTRIBUTE); + assertTrue(propertyFilterConstraint.getValue() instanceof ToscaGetFunctionDataDefinition); + final var toscaGetFunction = (ToscaGetFunctionDataDefinition) propertyFilterConstraint.getValue(); + assertToscaGetFunction(toscaGetFunction, ToscaFunctionType.GET_ATTRIBUTE, ToscaGetFunctionType.GET_ATTRIBUTE, PropertySource.SELF, + List.of("property", "subProperty"), "subProperty", null); + } + + @Test + void convertLegacyConstraintStaticTest() throws IOException { + final PropertyFilterConstraintDataDefinition propertyFilterConstraint = + PropertyFilterConstraintDataDefinitionHelper.convertLegacyConstraint(readConstraintFile("legacy-static.yaml")); + assertPropertyFilterConstraint(propertyFilterConstraint, "vnf_profile", null, ConstraintType.EQUAL, FilterValueType.STATIC); + assertTrue(propertyFilterConstraint.getValue() instanceof Map); + final Map<String, Object> value = (Map<String, Object>) propertyFilterConstraint.getValue(); + assertEquals("1", value.get("instantiation_level")); + assertEquals(1, value.get("max_number_of_instances")); + assertEquals(1, value.get("min_number_of_instances")); + } + + @Test + void convertLegacyConstraintConcatTest() throws IOException { + final PropertyFilterConstraintDataDefinition propertyFilterConstraint = + PropertyFilterConstraintDataDefinitionHelper.convertLegacyConstraint(readConstraintFile("concat.yaml")); + assertPropertyFilterConstraint(propertyFilterConstraint, "descriptor_id", null, ConstraintType.EQUAL, FilterValueType.CONCAT); + assertTrue(propertyFilterConstraint.getValue() instanceof ToscaConcatFunction); + final ToscaConcatFunction toscaConcatFunction = (ToscaConcatFunction) propertyFilterConstraint.getValue(); + assertEquals(3, toscaConcatFunction.getParameters().size()); + assertEquals(ToscaFunctionType.STRING, toscaConcatFunction.getParameters().get(0).getType()); + assertEquals("aString", toscaConcatFunction.getParameters().get(0).getValue()); + assertEquals(ToscaFunctionType.GET_INPUT, toscaConcatFunction.getParameters().get(1).getType()); + assertEquals(ToscaFunctionType.STRING, toscaConcatFunction.getParameters().get(2).getType()); + assertEquals("anotherString", toscaConcatFunction.getParameters().get(2).getValue()); + } + + private static void assertPropertyFilterConstraint(final PropertyFilterConstraintDataDefinition propertyFilterConstraint, + final String propertyName, final String capabilityName, final ConstraintType constraintType, + final FilterValueType filterValueType) { + assertEquals(propertyName, propertyFilterConstraint.getPropertyName()); + assertEquals(capabilityName, propertyFilterConstraint.getCapabilityName()); + assertEquals(constraintType, propertyFilterConstraint.getOperator()); + assertEquals(filterValueType, propertyFilterConstraint.getValueType()); + } + + private void assertToscaGetFunction(final ToscaGetFunctionDataDefinition actualToscaGetFunction, + final ToscaFunctionType expectedToscaFunctionType, final ToscaGetFunctionType expectedToscaFunctionGetType, + final PropertySource expectedPropertySource, final List<String> expectedPropertyPathFromSource, + final String expectedPropertyName, final String expectedSourceName) { + assertEquals(expectedToscaFunctionType, actualToscaGetFunction.getType()); + assertEquals(expectedToscaFunctionGetType, actualToscaGetFunction.getFunctionType()); + assertEquals(expectedPropertySource, actualToscaGetFunction.getPropertySource()); + assertEquals(expectedPropertyPathFromSource, actualToscaGetFunction.getPropertyPathFromSource()); + assertEquals(expectedPropertyName, actualToscaGetFunction.getPropertyName()); + assertEquals(expectedSourceName, actualToscaGetFunction.getSourceName()); + assertNull(actualToscaGetFunction.getPropertyUniqueId()); + assertNull(actualToscaGetFunction.getSourceUniqueId()); + } + + @Test + void convertFromToscaFunctionTypeTest() { + Optional<FilterValueType> filterValueType = + PropertyFilterConstraintDataDefinitionHelper.convertFromToscaFunctionType(ToscaFunctionType.GET_PROPERTY); + assertTrue(filterValueType.isPresent()); + assertEquals(FilterValueType.GET_PROPERTY, filterValueType.get()); + + filterValueType = + PropertyFilterConstraintDataDefinitionHelper.convertFromToscaFunctionType(ToscaFunctionType.GET_INPUT); + assertTrue(filterValueType.isPresent()); + assertEquals(FilterValueType.GET_INPUT, filterValueType.get()); + + filterValueType = + PropertyFilterConstraintDataDefinitionHelper.convertFromToscaFunctionType(ToscaFunctionType.GET_ATTRIBUTE); + assertTrue(filterValueType.isPresent()); + assertEquals(FilterValueType.GET_ATTRIBUTE, filterValueType.get()); + + filterValueType = + PropertyFilterConstraintDataDefinitionHelper.convertFromToscaFunctionType(ToscaFunctionType.YAML); + assertTrue(filterValueType.isPresent()); + assertEquals(FilterValueType.YAML, filterValueType.get()); + + filterValueType = + PropertyFilterConstraintDataDefinitionHelper.convertFromToscaFunctionType(ToscaFunctionType.CONCAT); + assertTrue(filterValueType.isPresent()); + assertEquals(FilterValueType.CONCAT, filterValueType.get()); + + assertTrue(PropertyFilterConstraintDataDefinitionHelper.convertFromToscaFunctionType(ToscaFunctionType.STRING).isEmpty()); + } + + private String readConstraintFile(final String fileName) throws IOException { + return Files.readString(RESOURCE_PATH.resolve(fileName)); + } +}
\ No newline at end of file diff --git a/common-be/src/test/resources/nodeFilter/constraints/concat.yaml b/common-be/src/test/resources/nodeFilter/constraints/concat.yaml new file mode 100644 index 0000000000..28f6aadec7 --- /dev/null +++ b/common-be/src/test/resources/nodeFilter/constraints/concat.yaml @@ -0,0 +1,8 @@ +descriptor_id: + equal: + concat: + - aString + - get_input: + - vnfProfileInput + - instantiation_level + - anotherString
\ No newline at end of file diff --git a/common-be/src/test/resources/nodeFilter/constraints/legacy-get_attribute-from-instance.yaml b/common-be/src/test/resources/nodeFilter/constraints/legacy-get_attribute-from-instance.yaml new file mode 100644 index 0000000000..039c4aeacc --- /dev/null +++ b/common-be/src/test/resources/nodeFilter/constraints/legacy-get_attribute-from-instance.yaml @@ -0,0 +1,6 @@ +flavour_id: + equal: + get_attribute: + - Instance Name + - property + - subProperty
\ No newline at end of file diff --git a/common-be/src/test/resources/nodeFilter/constraints/legacy-get_attribute-from-self.yaml b/common-be/src/test/resources/nodeFilter/constraints/legacy-get_attribute-from-self.yaml new file mode 100644 index 0000000000..3afd65d19f --- /dev/null +++ b/common-be/src/test/resources/nodeFilter/constraints/legacy-get_attribute-from-self.yaml @@ -0,0 +1,6 @@ +flavour_id: + equal: + get_attribute: + - SELF + - property + - subProperty
\ No newline at end of file diff --git a/common-be/src/test/resources/nodeFilter/constraints/legacy-get_input-subProperty.yaml b/common-be/src/test/resources/nodeFilter/constraints/legacy-get_input-subProperty.yaml new file mode 100644 index 0000000000..ba9bc3ac74 --- /dev/null +++ b/common-be/src/test/resources/nodeFilter/constraints/legacy-get_input-subProperty.yaml @@ -0,0 +1,6 @@ +flavour_id: + equal: + get_input: + - inputName + - inputSubProperty + - inputSubSubProperty
\ No newline at end of file diff --git a/common-be/src/test/resources/nodeFilter/constraints/legacy-get_input.yaml b/common-be/src/test/resources/nodeFilter/constraints/legacy-get_input.yaml new file mode 100644 index 0000000000..4b17734e86 --- /dev/null +++ b/common-be/src/test/resources/nodeFilter/constraints/legacy-get_input.yaml @@ -0,0 +1,4 @@ +flavour_id: + greater_or_equal: + get_input: + - inputName
\ No newline at end of file diff --git a/common-be/src/test/resources/nodeFilter/constraints/legacy-get_property-from-instance.yaml b/common-be/src/test/resources/nodeFilter/constraints/legacy-get_property-from-instance.yaml new file mode 100644 index 0000000000..67772d1666 --- /dev/null +++ b/common-be/src/test/resources/nodeFilter/constraints/legacy-get_property-from-instance.yaml @@ -0,0 +1,6 @@ +flavour_id: + equal: + get_property: + - Instance Name + - property + - subProperty
\ No newline at end of file diff --git a/common-be/src/test/resources/nodeFilter/constraints/legacy-get_property-from-self.yaml b/common-be/src/test/resources/nodeFilter/constraints/legacy-get_property-from-self.yaml new file mode 100644 index 0000000000..89c78d1a3b --- /dev/null +++ b/common-be/src/test/resources/nodeFilter/constraints/legacy-get_property-from-self.yaml @@ -0,0 +1,6 @@ +flavour_id: + equal: + get_property: + - SELF + - property + - subProperty
\ No newline at end of file diff --git a/common-be/src/test/resources/nodeFilter/constraints/legacy-static.yaml b/common-be/src/test/resources/nodeFilter/constraints/legacy-static.yaml new file mode 100644 index 0000000000..783316fff7 --- /dev/null +++ b/common-be/src/test/resources/nodeFilter/constraints/legacy-static.yaml @@ -0,0 +1,5 @@ +vnf_profile: + equal: + instantiation_level: '1' + max_number_of_instances: 1 + min_number_of_instances: 1
\ No newline at end of file diff --git a/common-be/src/test/resources/propertyFilterConstraintDataDefinitionDeserializer/filter-constraint-get-input.json b/common-be/src/test/resources/propertyFilterConstraintDataDefinitionDeserializer/filter-constraint-get-input.json new file mode 100644 index 0000000000..3ceee51804 --- /dev/null +++ b/common-be/src/test/resources/propertyFilterConstraintDataDefinitionDeserializer/filter-constraint-get-input.json @@ -0,0 +1,16 @@ +{ + "propertyName": "aProperty", + "targetType": "PROPERTY", + "operator": "GREATER_THAN", + "valueType": "GET_INPUT", + "value": { + "type": "GET_INPUT", + "functionType": "GET_INPUT", + "propertyName": "aProperty", + "propertyUniqueId": "aPropertyId", + "propertySource": "SELF", + "sourceName": "aService", + "sourceUniqueId": "aServiceId", + "propertyPathFromSource": ["input", "subProperty"] + } +}
\ No newline at end of file diff --git a/common-be/src/test/resources/propertyFilterConstraintDataDefinitionDeserializer/filter-constraint-legacy.txt b/common-be/src/test/resources/propertyFilterConstraintDataDefinitionDeserializer/filter-constraint-legacy.txt new file mode 100644 index 0000000000..925c878d5f --- /dev/null +++ b/common-be/src/test/resources/propertyFilterConstraintDataDefinitionDeserializer/filter-constraint-legacy.txt @@ -0,0 +1 @@ +"{ \"propertyName\": { \"equal\": \"aValue\" } }"
\ No newline at end of file diff --git a/common-be/src/test/resources/propertyFilterConstraintDataDefinitionDeserializer/filter-constraint-static.json b/common-be/src/test/resources/propertyFilterConstraintDataDefinitionDeserializer/filter-constraint-static.json new file mode 100644 index 0000000000..4faee8ab1a --- /dev/null +++ b/common-be/src/test/resources/propertyFilterConstraintDataDefinitionDeserializer/filter-constraint-static.json @@ -0,0 +1,8 @@ +{ + "propertyName": "aProperty", + "capabilityName": "aCapability", + "targetType": "CAPABILITY", + "operator": "EQUAL", + "valueType": "STATIC", + "value": "aStaticValue" +}
\ No newline at end of file |