diff options
author | MichaelMorris <michael.morris@est.tech> | 2022-10-13 14:12:47 +0100 |
---|---|---|
committer | Vasyl Razinkov <vasyl.razinkov@est.tech> | 2022-10-24 15:06:02 +0000 |
commit | 2edea35f59360fbc8b4f451fc573e02dc5f65010 (patch) | |
tree | 1b1ead43f07d813562b55e9dbad80efcd5308ea8 | |
parent | aa361f84ec4d137e7a64df8c7feaec6d2304c03e (diff) |
Support import of VFCs with property constraints
Signed-off-by: MichaelMorris <michael.morris@est.tech>
Issue-ID: SDC-4216
Change-Id: I19dce9f929535aa22cad6a1d95a213f42dd6e99c
30 files changed, 815 insertions, 42 deletions
diff --git a/catalog-be-plugins/etsi-nfv-nsd-csar-plugin/src/main/java/org/openecomp/sdc/be/plugins/etsi/nfv/nsd/tosca/yaml/NsdTemplateRepresenter.java b/catalog-be-plugins/etsi-nfv-nsd-csar-plugin/src/main/java/org/openecomp/sdc/be/plugins/etsi/nfv/nsd/tosca/yaml/NsdTemplateRepresenter.java index cb8928f372..eba1a402de 100644 --- a/catalog-be-plugins/etsi-nfv-nsd-csar-plugin/src/main/java/org/openecomp/sdc/be/plugins/etsi/nfv/nsd/tosca/yaml/NsdTemplateRepresenter.java +++ b/catalog-be-plugins/etsi-nfv-nsd-csar-plugin/src/main/java/org/openecomp/sdc/be/plugins/etsi/nfv/nsd/tosca/yaml/NsdTemplateRepresenter.java @@ -60,7 +60,7 @@ public class NsdTemplateRepresenter extends Representer { return handleToscaTemplate(javaBean, property, propertyValue, customTag); } if (javaBean instanceof ToscaPropertyConstraintValidValues) { - return handleToscaPropertyConstraintValidValues(javaBean, property, propertyValue, customTag); + return handleToscaPropertyConstraintValidValues((ToscaPropertyConstraintValidValues)javaBean, property, propertyValue, customTag); } if (javaBean instanceof ToscaProperty) { return handleToscaProperty(javaBean, property, propertyValue, customTag); @@ -76,11 +76,11 @@ public class NsdTemplateRepresenter extends Representer { return nodeTuple; } - private NodeTuple handleToscaPropertyConstraintValidValues(final Object javaBean, final Property property, final Object propertyValue, + private NodeTuple handleToscaPropertyConstraintValidValues(final ToscaPropertyConstraintValidValues javaBean, final Property property, final Object propertyValue, final Tag customTag) { final NodeTuple nodeTuple = super.representJavaBeanProperty(javaBean, property, propertyValue, customTag); if ("validValues".equals(property.getName())) { - final String validValuesEntryName = ToscaPropertyConstraintValidValues.getEntryToscaName("validValues"); + final String validValuesEntryName = javaBean.getEntryToscaName("validValues"); return new NodeTuple(representData(validValuesEntryName), nodeTuple.getValueNode()); } return nodeTuple; diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ImportUtils.java b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ImportUtils.java index 8ad86b3510..a28d587cae 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ImportUtils.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ImportUtils.java @@ -22,7 +22,6 @@ package org.openecomp.sdc.be.components.impl; import static org.apache.commons.collections.CollectionUtils.isEmpty; import static org.openecomp.sdc.be.components.impl.ResourceImportManager.PROPERTY_NAME_PATTERN_IGNORE_LENGTH; import static org.openecomp.sdc.be.datatypes.elements.Annotation.setAnnotationsName; - import com.google.gson.Gson; import com.google.gson.GsonBuilder; import com.google.gson.JsonParseException; @@ -286,7 +285,9 @@ public final class ImportUtils { final Gson gson = new GsonBuilder().registerTypeAdapter(constraintType, new PropertyConstraintDeserialiser()).create(); for (final Object constraintJson : propertyFieldConstraints) { final PropertyConstraint propertyConstraint = validateAndGetPropertyConstraint(propertyType, constraintType, gson, constraintJson); - constraintList.add(propertyConstraint); + if (propertyConstraint != null) { + constraintList.add(propertyConstraint); + } } return constraintList; } diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/datamodel/utils/PropertyValueConstraintValidationUtil.java b/catalog-be/src/main/java/org/openecomp/sdc/be/datamodel/utils/PropertyValueConstraintValidationUtil.java index 2d3ef85488..4b8d88fa79 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/datamodel/utils/PropertyValueConstraintValidationUtil.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/datamodel/utils/PropertyValueConstraintValidationUtil.java @@ -128,8 +128,7 @@ public class PropertyValueConstraintValidationUtil { private void evaluateConstraintsOnProperty(PropertyDefinition propertyDefinition) { ToscaType toscaType = ToscaType.isValidType(propertyDefinition.getType()); - if (isPropertyNotMappedAsInput(propertyDefinition) && CollectionUtils.isNotEmpty(propertyDefinition.getConstraints()) - && isValidValueConstraintPresent(propertyDefinition.getConstraints())) { + if (isPropertyNotMappedAsInput(propertyDefinition) && CollectionUtils.isNotEmpty(propertyDefinition.getConstraints())) { for (PropertyConstraint propertyConstraint : propertyDefinition.getConstraints()) { try { propertyConstraint.initialize(toscaType); diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/impl/ComponentsUtils.java b/catalog-be/src/main/java/org/openecomp/sdc/be/impl/ComponentsUtils.java index dec13a94c8..dfe065a79e 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/impl/ComponentsUtils.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/impl/ComponentsUtils.java @@ -21,6 +21,7 @@ */ package org.openecomp.sdc.be.impl; +import com.fasterxml.jackson.annotation.JsonInclude.Include; import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.module.SimpleModule; @@ -210,6 +211,7 @@ public class ComponentsUtils { module.addDeserializer(PropertyConstraint.class, new PropertyConstraintJacksonDeserializer()); module.addDeserializer(ToscaFunction.class, new ToscaFunctionJsonDeserializer()); mapper.registerModule(module); + mapper.setSerializationInclusion(Include.NON_NULL); component = mapper.readValue(data, clazz); if (component == null) { BeEcompErrorManager.getInstance().logBeInvalidJsonInput(CONVERT_JSON_TO_OBJECT); diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/servlets/BeGenericServlet.java b/catalog-be/src/main/java/org/openecomp/sdc/be/servlets/BeGenericServlet.java index 7c9101df82..2cd2ddf978 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/servlets/BeGenericServlet.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/servlets/BeGenericServlet.java @@ -19,6 +19,7 @@ */ package org.openecomp.sdc.be.servlets; +import com.fasterxml.jackson.annotation.JsonInclude.Include; import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.module.SimpleModule; @@ -226,6 +227,7 @@ public class BeGenericServlet extends BasicServlet { SimpleModule module = new SimpleModule("customDeserializationModule"); module.addDeserializer(PropertyConstraint.class, new PropertyConstraintJacksonDeserializer()); mapper.registerModule(module); + mapper.setSerializationInclusion(Include.NON_NULL); object = mapper.readValue(json, clazz); if (object != null) { return object; diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/PropertyConvertor.java b/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/PropertyConvertor.java index 148e69907c..90a5161f7a 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/PropertyConvertor.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/PropertyConvertor.java @@ -26,32 +26,57 @@ import com.google.gson.JsonParser; import com.google.gson.stream.JsonReader; import fj.data.Either; import java.io.StringReader; +import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Objects; import java.util.function.Supplier; +import org.apache.commons.collections.CollectionUtils; import org.apache.commons.lang3.StringUtils; import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition; import org.openecomp.sdc.be.datatypes.elements.SchemaDefinition; import org.openecomp.sdc.be.datatypes.elements.ToscaFunctionType; import org.openecomp.sdc.be.model.Component; import org.openecomp.sdc.be.model.DataTypeDefinition; +import org.openecomp.sdc.be.model.PropertyConstraint; import org.openecomp.sdc.be.model.PropertyDefinition; import org.openecomp.sdc.be.model.Resource; import org.openecomp.sdc.be.model.tosca.ToscaPropertyType; +import org.openecomp.sdc.be.model.tosca.constraints.EqualConstraint; +import org.openecomp.sdc.be.model.tosca.constraints.GreaterOrEqualConstraint; +import org.openecomp.sdc.be.model.tosca.constraints.GreaterThanConstraint; +import org.openecomp.sdc.be.model.tosca.constraints.InRangeConstraint; +import org.openecomp.sdc.be.model.tosca.constraints.LengthConstraint; +import org.openecomp.sdc.be.model.tosca.constraints.LessOrEqualConstraint; +import org.openecomp.sdc.be.model.tosca.constraints.LessThanConstraint; +import org.openecomp.sdc.be.model.tosca.constraints.MaxLengthConstraint; +import org.openecomp.sdc.be.model.tosca.constraints.MinLengthConstraint; +import org.openecomp.sdc.be.model.tosca.constraints.ValidValuesConstraint; import org.openecomp.sdc.be.model.tosca.converters.DataTypePropertyConverter; import org.openecomp.sdc.be.model.tosca.converters.ToscaMapValueConverter; import org.openecomp.sdc.be.model.tosca.converters.ToscaValueBaseConverter; import org.openecomp.sdc.be.model.tosca.converters.ToscaValueConverter; import org.openecomp.sdc.be.tosca.model.ToscaNodeType; import org.openecomp.sdc.be.tosca.model.ToscaProperty; +import org.openecomp.sdc.be.tosca.model.ToscaPropertyConstraint; +import org.openecomp.sdc.be.tosca.model.ToscaPropertyConstraintEqual; +import org.openecomp.sdc.be.tosca.model.ToscaPropertyConstraintGreaterOrEqual; +import org.openecomp.sdc.be.tosca.model.ToscaPropertyConstraintGreaterThan; +import org.openecomp.sdc.be.tosca.model.ToscaPropertyConstraintInRange; +import org.openecomp.sdc.be.tosca.model.ToscaPropertyConstraintLength; +import org.openecomp.sdc.be.tosca.model.ToscaPropertyConstraintLessOrEqual; +import org.openecomp.sdc.be.tosca.model.ToscaPropertyConstraintLessThan; +import org.openecomp.sdc.be.tosca.model.ToscaPropertyConstraintMaxLength; +import org.openecomp.sdc.be.tosca.model.ToscaPropertyConstraintMinLength; +import org.openecomp.sdc.be.tosca.model.ToscaPropertyConstraintValidValues; import org.openecomp.sdc.be.tosca.model.ToscaSchemaDefinition; import org.openecomp.sdc.common.log.wrappers.Logger; import org.openecomp.sdc.tosca.datatypes.ToscaFunctions; import org.springframework.stereotype.Service; import org.yaml.snakeyaml.Yaml; + @Service public class PropertyConvertor { @@ -102,8 +127,55 @@ public class PropertyConvertor { prop.setStatus(property.getStatus()); } prop.setMetadata(property.getMetadata()); + + List<ToscaPropertyConstraint> constraints = new ArrayList<>(); + if (CollectionUtils.isNotEmpty(property.getConstraints())) { + constraints = convertConstraints(property.getConstraints()); + prop.setConstraints(constraints); + } return prop; } + + private List<ToscaPropertyConstraint> convertConstraints(List<PropertyConstraint> constraints) { + List<ToscaPropertyConstraint> convertedConstraints = new ArrayList<>(); + for (PropertyConstraint constraint: constraints){ + if (constraint instanceof EqualConstraint) { + convertedConstraints.add(new ToscaPropertyConstraintEqual(((EqualConstraint) constraint).getEqual())); + } + if (constraint instanceof GreaterThanConstraint) { + convertedConstraints.add(new ToscaPropertyConstraintGreaterThan(((GreaterThanConstraint) constraint).getGreaterThan())); + } + if (constraint instanceof GreaterOrEqualConstraint) { + convertedConstraints.add(new ToscaPropertyConstraintGreaterOrEqual(((GreaterOrEqualConstraint) constraint).getGreaterOrEqual())); + } + if (constraint instanceof LessThanConstraint) { + convertedConstraints.add(new ToscaPropertyConstraintLessThan(((LessThanConstraint) constraint).getLessThan())); + } + if (constraint instanceof LessOrEqualConstraint) { + convertedConstraints.add(new ToscaPropertyConstraintLessOrEqual(((LessOrEqualConstraint) constraint).getLessOrEqual())); + } + if (constraint instanceof InRangeConstraint) { + InRangeConstraint inRangeConstraint = (InRangeConstraint) constraint; + List<String> range = new ArrayList<>(); + range.add(inRangeConstraint.getRangeMinValue()); + range.add(inRangeConstraint.getRangeMaxValue()); + convertedConstraints.add(new ToscaPropertyConstraintInRange(range)); + } + if (constraint instanceof ValidValuesConstraint) { + convertedConstraints.add(new ToscaPropertyConstraintValidValues(((ValidValuesConstraint) constraint).getValidValues())); + } + if (constraint instanceof LengthConstraint) { + convertedConstraints.add(new ToscaPropertyConstraintLength(((LengthConstraint) constraint).getLength().toString())); + } + if (constraint instanceof MinLengthConstraint) { + convertedConstraints.add(new ToscaPropertyConstraintMinLength(((MinLengthConstraint) constraint).getMinLength())); + } + if (constraint instanceof MaxLengthConstraint) { + convertedConstraints.add(new ToscaPropertyConstraintMaxLength(((MaxLengthConstraint) constraint).getMaxLength())); + } + } + return convertedConstraints; + } public Object convertToToscaObject(PropertyDataDefinition property, String value, Map<String, DataTypeDefinition> dataTypes, boolean preserveEmptyValue) { diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/ToscaExportHandler.java b/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/ToscaExportHandler.java index 85f29bf352..5f21f52960 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/ToscaExportHandler.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/ToscaExportHandler.java @@ -127,6 +127,7 @@ import org.openecomp.sdc.be.tosca.model.ToscaNodeType; import org.openecomp.sdc.be.tosca.model.ToscaPolicyTemplate; import org.openecomp.sdc.be.tosca.model.ToscaProperty; import org.openecomp.sdc.be.tosca.model.ToscaPropertyAssignment; +import org.openecomp.sdc.be.tosca.model.ToscaPropertyConstraint; import org.openecomp.sdc.be.tosca.model.ToscaRelationshipTemplate; import org.openecomp.sdc.be.tosca.model.ToscaRequirement; import org.openecomp.sdc.be.tosca.model.ToscaTemplate; @@ -271,6 +272,8 @@ public class ToscaExportHandler { options.setCanonical(false); representer.addClassTag(toscaTemplate.getClass(), Tag.MAP); representer.setPropertyUtils(new UnsortedPropertyUtils()); + + Yaml yaml = new Yaml(representer, options); String yamlAsString = yaml.dumpAsMap(toscaTemplate); StringBuilder sb = new StringBuilder(); @@ -1817,6 +1820,9 @@ public class ToscaExportHandler { if (javaBean instanceof ToscaRelationshipTemplate && "name".equals(property.getName())) { return null; } + if (javaBean instanceof ToscaPropertyConstraint) { + return handleToscaPropertyConstraint((ToscaPropertyConstraint)javaBean, property, propertyValue, customTag); + } removeDefaultP(propertyValue); NodeTuple defaultNode = super.representJavaBeanProperty(javaBean, property, propertyValue, customTag); if (javaBean instanceof ToscaTopolgyTemplate && "relationshipTemplates".equals(property.getName())) { @@ -1824,6 +1830,13 @@ public class ToscaExportHandler { } return "_defaultp_".equals(property.getName()) ? new NodeTuple(representData("default"), defaultNode.getValueNode()) : defaultNode; } + + private NodeTuple handleToscaPropertyConstraint(final ToscaPropertyConstraint javaBean, final Property property, final Object propertyValue, + final Tag customTag) { + final NodeTuple nodeTuple = super.representJavaBeanProperty(javaBean, property, propertyValue, customTag); + final String entryToscaName = javaBean.getEntryToscaName(property.getName()); + return new NodeTuple(representData(entryToscaName), nodeTuple.getValueNode()); + } private void removeDefaultP(final Object propertyValue) { if (propertyValue instanceof Map) { diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/model/ToscaPropertyConstraint.java b/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/model/ToscaPropertyConstraint.java index 6049be2fd6..8224613ac2 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/model/ToscaPropertyConstraint.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/model/ToscaPropertyConstraint.java @@ -26,4 +26,12 @@ import org.openecomp.sdc.be.datatypes.enums.ConstraintType; public interface ToscaPropertyConstraint { ConstraintType getConstraintType(); + + /** + * Get the TOSCA entry name of an attribute in this class. + * + * @param attributeName the class attribute name + * @return the TOSCA entry represented by the attribute + */ + String getEntryToscaName(final String attributeName); } diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/model/ToscaPropertyConstraintEqual.java b/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/model/ToscaPropertyConstraintEqual.java new file mode 100644 index 0000000000..532cb8c91b --- /dev/null +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/model/ToscaPropertyConstraintEqual.java @@ -0,0 +1,49 @@ +/* + * ============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.tosca.model; + +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.Setter; +import org.openecomp.sdc.be.datatypes.enums.ConstraintType; + +/** + * Represents a TOSCA equal constraint + */ +@Getter +@Setter +@AllArgsConstructor +public class ToscaPropertyConstraintEqual implements ToscaPropertyConstraint { + + private String equal; + private static final ConstraintType CONSTRAINT_TYPE = ConstraintType.EQUAL; + + @Override + public String getEntryToscaName(final String attributeName) { + if ("equal".equals(attributeName)) { + return CONSTRAINT_TYPE.getType(); + } + return attributeName; + } + + @Override + public ConstraintType getConstraintType() { + return CONSTRAINT_TYPE; + } +} diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/model/ToscaPropertyConstraintGreaterOrEqual.java b/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/model/ToscaPropertyConstraintGreaterOrEqual.java new file mode 100644 index 0000000000..fcc9dcc884 --- /dev/null +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/model/ToscaPropertyConstraintGreaterOrEqual.java @@ -0,0 +1,49 @@ +/* + * ============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.tosca.model; + +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.Setter; +import org.openecomp.sdc.be.datatypes.enums.ConstraintType; + +/** + * Represents a TOSCA greater_or_equal constraint + */ +@Getter +@Setter +@AllArgsConstructor +public class ToscaPropertyConstraintGreaterOrEqual implements ToscaPropertyConstraint { + + private String greaterOrEqual; + private static final ConstraintType CONSTRAINT_TYPE = ConstraintType.GREATER_OR_EQUAL; + + @Override + public String getEntryToscaName(final String attributeName) { + if ("greaterOrEqual".equals(attributeName)) { + return CONSTRAINT_TYPE.getType(); + } + return attributeName; + } + + @Override + public ConstraintType getConstraintType() { + return CONSTRAINT_TYPE; + } +} diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/model/ToscaPropertyConstraintGreaterThan.java b/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/model/ToscaPropertyConstraintGreaterThan.java new file mode 100644 index 0000000000..6f2268bd9e --- /dev/null +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/model/ToscaPropertyConstraintGreaterThan.java @@ -0,0 +1,49 @@ +/* + * ============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.tosca.model; + +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.Setter; +import org.openecomp.sdc.be.datatypes.enums.ConstraintType; + +/** + * Represents a TOSCA greater_than constraint + */ +@Getter +@Setter +@AllArgsConstructor +public class ToscaPropertyConstraintGreaterThan implements ToscaPropertyConstraint { + + private String greaterThan; + private static final ConstraintType CONSTRAINT_TYPE = ConstraintType.GREATER_THAN; + + @Override + public String getEntryToscaName(final String attributeName) { + if ("greaterThan".equals(attributeName)) { + return CONSTRAINT_TYPE.getType(); + } + return attributeName; + } + + @Override + public ConstraintType getConstraintType() { + return CONSTRAINT_TYPE; + } +} diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/model/ToscaPropertyConstraintInRange.java b/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/model/ToscaPropertyConstraintInRange.java new file mode 100644 index 0000000000..6c8cfba620 --- /dev/null +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/model/ToscaPropertyConstraintInRange.java @@ -0,0 +1,51 @@ +/* + * ============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.tosca.model; + +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.Setter; +import java.util.List; +import org.openecomp.sdc.be.datatypes.enums.ConstraintType; + +/** + * Represents a TOSCA in_range constraint + */ +@Getter +@Setter +@AllArgsConstructor +public class ToscaPropertyConstraintInRange implements ToscaPropertyConstraint { + + private List<String> inRange; + private static final ConstraintType CONSTRAINT_TYPE = ConstraintType.IN_RANGE; + + @Override + public String getEntryToscaName(final String attributeName) { + if ("inRange".equals(attributeName)) { + return CONSTRAINT_TYPE.getType(); + } + + return attributeName; + } + + @Override + public ConstraintType getConstraintType() { + return CONSTRAINT_TYPE; + } +} diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/model/ToscaPropertyConstraintLength.java b/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/model/ToscaPropertyConstraintLength.java new file mode 100644 index 0000000000..67aaca0188 --- /dev/null +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/model/ToscaPropertyConstraintLength.java @@ -0,0 +1,49 @@ +/* + * ============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.tosca.model; + +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.Setter; +import org.openecomp.sdc.be.datatypes.enums.ConstraintType; + +/** + * Represents a TOSCA length constraint + */ +@Getter +@Setter +@AllArgsConstructor +public class ToscaPropertyConstraintLength implements ToscaPropertyConstraint { + + private String length; + private static final ConstraintType CONSTRAINT_TYPE = ConstraintType.LENGTH; + + @Override + public String getEntryToscaName(final String attributeName) { + if ("length".equals(attributeName)) { + return CONSTRAINT_TYPE.getType(); + } + return attributeName; + } + + @Override + public ConstraintType getConstraintType() { + return CONSTRAINT_TYPE; + } +} diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/model/ToscaPropertyConstraintLessOrEqual.java b/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/model/ToscaPropertyConstraintLessOrEqual.java new file mode 100644 index 0000000000..3d51ec9419 --- /dev/null +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/model/ToscaPropertyConstraintLessOrEqual.java @@ -0,0 +1,49 @@ +/* + * ============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.tosca.model; + +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.Setter; +import org.openecomp.sdc.be.datatypes.enums.ConstraintType; + +/** + * Represents a TOSCA less_or_equal constraint + */ +@Getter +@Setter +@AllArgsConstructor +public class ToscaPropertyConstraintLessOrEqual implements ToscaPropertyConstraint { + + private String lessOrEqual; + private static final ConstraintType CONSTRAINT_TYPE = ConstraintType.LESS_OR_EQUAL; + + @Override + public String getEntryToscaName(final String attributeName) { + if ("lessOrEqual".equals(attributeName)) { + return CONSTRAINT_TYPE.getType(); + } + return attributeName; + } + + @Override + public ConstraintType getConstraintType() { + return CONSTRAINT_TYPE; + } +} diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/model/ToscaPropertyConstraintLessThan.java b/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/model/ToscaPropertyConstraintLessThan.java new file mode 100644 index 0000000000..ad51f66fde --- /dev/null +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/model/ToscaPropertyConstraintLessThan.java @@ -0,0 +1,49 @@ +/* + * ============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.tosca.model; + +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.Setter; +import org.openecomp.sdc.be.datatypes.enums.ConstraintType; + +/** + * Represents a TOSCA less_than constraint + */ +@Getter +@Setter +@AllArgsConstructor +public class ToscaPropertyConstraintLessThan implements ToscaPropertyConstraint { + + private String lessThan; + private static final ConstraintType CONSTRAINT_TYPE = ConstraintType.LESS_THAN; + + @Override + public String getEntryToscaName(final String attributeName) { + if ("lessThan".equals(attributeName)) { + return CONSTRAINT_TYPE.getType(); + } + return attributeName; + } + + @Override + public ConstraintType getConstraintType() { + return CONSTRAINT_TYPE; + } +} diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/model/ToscaPropertyConstraintMaxLength.java b/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/model/ToscaPropertyConstraintMaxLength.java new file mode 100644 index 0000000000..29d1464714 --- /dev/null +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/model/ToscaPropertyConstraintMaxLength.java @@ -0,0 +1,49 @@ +/* + * ============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.tosca.model; + +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.Setter; +import org.openecomp.sdc.be.datatypes.enums.ConstraintType; + +/** + * Represents a TOSCA max_length constraint + */ +@Getter +@Setter +@AllArgsConstructor +public class ToscaPropertyConstraintMaxLength implements ToscaPropertyConstraint { + + private Integer maxLength; + private static final ConstraintType CONSTRAINT_TYPE = ConstraintType.MAX_LENGTH; + + @Override + public String getEntryToscaName(final String attributeName) { + if ("maxLength".equals(attributeName)) { + return CONSTRAINT_TYPE.getType(); + } + return attributeName; + } + + @Override + public ConstraintType getConstraintType() { + return CONSTRAINT_TYPE; + } +} diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/model/ToscaPropertyConstraintMinLength.java b/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/model/ToscaPropertyConstraintMinLength.java new file mode 100644 index 0000000000..3613df9928 --- /dev/null +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/model/ToscaPropertyConstraintMinLength.java @@ -0,0 +1,48 @@ +/* + * ============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.tosca.model; + +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.Setter; +import org.openecomp.sdc.be.datatypes.enums.ConstraintType; + +/** + * Represents a TOSCA min_length constraint + */ +@Getter +@Setter +@AllArgsConstructor +public class ToscaPropertyConstraintMinLength implements ToscaPropertyConstraint { + + private Integer minLength; + + @Override + public String getEntryToscaName(final String attributeName) { + if ("minLength".equals(attributeName)) { + return ConstraintType.MIN_LENGTH.getType(); + } + return attributeName; + } + + @Override + public ConstraintType getConstraintType() { + return ConstraintType.MIN_LENGTH; + } +} diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/model/ToscaPropertyConstraintValidValues.java b/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/model/ToscaPropertyConstraintValidValues.java index 8e73f6985b..c23be5725c 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/model/ToscaPropertyConstraintValidValues.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/model/ToscaPropertyConstraintValidValues.java @@ -34,13 +34,8 @@ public class ToscaPropertyConstraintValidValues implements ToscaPropertyConstrai private List<String> validValues; - /** - * Get the TOSCA entry name of an attribute in this class. - * - * @param attributeName the class attribute name - * @return the TOSCA entry represented by the attribute - */ - public static String getEntryToscaName(final String attributeName) { + @Override + public String getEntryToscaName(final String attributeName) { if ("validValues".equals(attributeName)) { return "valid_values"; } diff --git a/catalog-be/src/test/java/org/openecomp/sdc/be/impl/ComponentsUtilsTest.java b/catalog-be/src/test/java/org/openecomp/sdc/be/impl/ComponentsUtilsTest.java index a01831e70c..3260521084 100644 --- a/catalog-be/src/test/java/org/openecomp/sdc/be/impl/ComponentsUtilsTest.java +++ b/catalog-be/src/test/java/org/openecomp/sdc/be/impl/ComponentsUtilsTest.java @@ -41,6 +41,7 @@ import org.openecomp.sdc.be.datatypes.enums.JsonPresentationFields; import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum; import org.openecomp.sdc.be.model.CapabilityTypeDefinition; import org.openecomp.sdc.be.model.Component; +import org.openecomp.sdc.be.model.ComponentInstanceProperty; import org.openecomp.sdc.be.model.DataTypeDefinition; import org.openecomp.sdc.be.model.GroupTypeDefinition; import org.openecomp.sdc.be.model.PolicyTypeDefinition; @@ -48,6 +49,15 @@ import org.openecomp.sdc.be.model.RequirementDefinition; import org.openecomp.sdc.be.model.Resource; import org.openecomp.sdc.be.model.User; import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus; +import org.openecomp.sdc.be.model.tosca.constraints.EqualConstraint; +import org.openecomp.sdc.be.model.tosca.constraints.GreaterOrEqualConstraint; +import org.openecomp.sdc.be.model.tosca.constraints.InRangeConstraint; +import org.openecomp.sdc.be.model.tosca.constraints.LengthConstraint; +import org.openecomp.sdc.be.model.tosca.constraints.LessOrEqualConstraint; +import org.openecomp.sdc.be.model.tosca.constraints.LessThanConstraint; +import org.openecomp.sdc.be.model.tosca.constraints.MaxLengthConstraint; +import org.openecomp.sdc.be.model.tosca.constraints.MinLengthConstraint; +import org.openecomp.sdc.be.model.tosca.constraints.ValidValuesConstraint; import org.openecomp.sdc.be.resources.data.auditing.AuditingActionEnum; import org.openecomp.sdc.be.resources.data.auditing.model.ResourceCommonInfo; import org.openecomp.sdc.be.resources.data.auditing.model.ResourceVersionInfo; @@ -61,6 +71,7 @@ import org.openecomp.sdc.test.utils.TestConfigurationProvider; import java.util.List; import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.assertEquals; import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; @@ -771,6 +782,45 @@ public class ComponentsUtilsTest { assertThat(response.left().value()).isEqualTo(assertuser); } + @Test + public void testconvertJsonToObjectUsingObjectMapper() { + + AuditingManager auditingmanager = Mockito.mock(AuditingManager.class); + ComponentsUtils compUtils = new ComponentsUtils(auditingmanager); + when(auditingmanager.auditEvent(any())).thenReturn("OK"); + + User user = new User(); + String data = + "[{\"constraints\":[{\"equal\":\"value\"}]}," + + "{\"constraints\":[{\"greaterOrEqual\":5}]}," + + "{\"constraints\":[{\"lessThan\":7}]}," + + "{\"constraints\":[{\"lessOrEqual\":9}]}," + + "{\"constraints\":[{\"inRange\":[\"5\", \"10\"]}]}," + + "{\"constraints\":[{\"validValues\":[\"abc\", \"def\", \"hij\"]}]}," + + "{\"constraints\":[{\"length\":11}]}," + + "{\"constraints\":[{\"minLength\":13}]}," + + "{\"constraints\":[{\"maxLength\":15}]}" + +"]"; + + + Either<ComponentInstanceProperty[], ResponseFormat> response = compUtils.convertJsonToObjectUsingObjectMapper(data, user, + ComponentInstanceProperty[].class, null, ComponentTypeEnum.RESOURCE_INSTANCE); + + assertThat(response.isLeft()).isTrue(); + ComponentInstanceProperty[] properties = response.left().value(); + assertEquals(9, properties.length); + assertEquals("value", ((EqualConstraint)properties[0].getConstraints().iterator().next()).getEqual()); + assertEquals("5", ((GreaterOrEqualConstraint)properties[1].getConstraints().iterator().next()).getGreaterOrEqual()); + assertEquals("7", ((LessThanConstraint)properties[2].getConstraints().iterator().next()).getLessThan()); + assertEquals("9", ((LessOrEqualConstraint)properties[3].getConstraints().iterator().next()).getLessOrEqual()); + assertEquals("5", ((InRangeConstraint)properties[4].getConstraints().iterator().next()).getRangeMinValue()); + assertEquals("10", ((InRangeConstraint)properties[4].getConstraints().iterator().next()).getRangeMaxValue()); + assertEquals(3, ((ValidValuesConstraint)properties[5].getConstraints().iterator().next()).getValidValues().size()); + assertEquals(11, ((LengthConstraint)properties[6].getConstraints().iterator().next()).getLength()); + assertEquals(13, ((MinLengthConstraint)properties[7].getConstraints().iterator().next()).getMinLength()); + assertEquals(15, ((MaxLengthConstraint)properties[8].getConstraints().iterator().next()).getMaxLength()); + } + @Test public void testconvertJsonToObject_NllData() { diff --git a/catalog-model/src/main/java/org/openecomp/sdc/be/model/DataTypeDefinition.java b/catalog-model/src/main/java/org/openecomp/sdc/be/model/DataTypeDefinition.java index f208891e21..22e6a6938e 100644 --- a/catalog-model/src/main/java/org/openecomp/sdc/be/model/DataTypeDefinition.java +++ b/catalog-model/src/main/java/org/openecomp/sdc/be/model/DataTypeDefinition.java @@ -19,6 +19,7 @@ */ package org.openecomp.sdc.be.model; +import java.util.ArrayList; import java.util.List; import lombok.Getter; import lombok.NoArgsConstructor; @@ -54,6 +55,6 @@ public class DataTypeDefinition extends DataTypeDataDefinition { } public List<PropertyConstraint> safeGetConstraints() { - return CollectionUtils.safeGetList(constraints); + return new ArrayList<PropertyConstraint>(CollectionUtils.safeGetList(constraints)); } } diff --git a/catalog-model/src/main/java/org/openecomp/sdc/be/model/operations/impl/PropertyOperation.java b/catalog-model/src/main/java/org/openecomp/sdc/be/model/operations/impl/PropertyOperation.java index 9e5c2e49bc..d797d8a8ee 100644 --- a/catalog-model/src/main/java/org/openecomp/sdc/be/model/operations/impl/PropertyOperation.java +++ b/catalog-model/src/main/java/org/openecomp/sdc/be/model/operations/impl/PropertyOperation.java @@ -24,6 +24,7 @@ import static org.openecomp.sdc.common.log.enums.EcompLoggerErrorCode.BUSINESS_P import com.fasterxml.jackson.core.ObjectCodec; import com.fasterxml.jackson.databind.DeserializationContext; import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.node.ArrayNode; import com.google.common.collect.Maps; import com.google.gson.JsonArray; import com.google.gson.JsonDeserializationContext; @@ -36,16 +37,17 @@ import com.google.gson.JsonSerializationContext; import com.google.gson.JsonSerializer; import fj.data.Either; import java.io.IOException; +import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Type; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.HashMap; +import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Map.Entry; import java.util.Set; -import java.util.StringJoiner; import java.util.function.Consumer; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -87,11 +89,14 @@ import org.openecomp.sdc.be.model.operations.api.IPropertyOperation; import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus; import org.openecomp.sdc.be.model.tosca.ToscaPropertyType; import org.openecomp.sdc.be.datatypes.enums.ConstraintType; +import org.openecomp.sdc.be.model.tosca.constraints.EqualConstraint; import org.openecomp.sdc.be.model.tosca.constraints.GreaterOrEqualConstraint; import org.openecomp.sdc.be.model.tosca.constraints.GreaterThanConstraint; import org.openecomp.sdc.be.model.tosca.constraints.InRangeConstraint; +import org.openecomp.sdc.be.model.tosca.constraints.LengthConstraint; import org.openecomp.sdc.be.model.tosca.constraints.LessOrEqualConstraint; import org.openecomp.sdc.be.model.tosca.constraints.LessThanConstraint; +import org.openecomp.sdc.be.model.tosca.constraints.MaxLengthConstraint; import org.openecomp.sdc.be.model.tosca.constraints.MinLengthConstraint; import org.openecomp.sdc.be.model.tosca.constraints.ValidValuesConstraint; import org.openecomp.sdc.be.model.tosca.converters.PropertyValueConverter; @@ -2146,6 +2151,16 @@ public class PropertyOperation extends AbstractOperation implements IPropertyOpe log.warn("ConstraintType was not found for constraint name:{}", key); } else { switch (constraintType) { + case EQUAL: + if (value != null) { + String asString = value.getAsString(); + log.debug("Before adding value to EqualConstraint object. value = {}", asString); + propertyConstraint = new EqualConstraint(asString); + break; + } else { + log.warn("The value of equal constraint is null"); + } + break; case IN_RANGE: if (value != null) { if (value instanceof JsonArray) { @@ -2227,6 +2242,16 @@ public class PropertyOperation extends AbstractOperation implements IPropertyOpe } } break; + case LENGTH: + if (value != null) { + int asInt = value.getAsInt(); + log.debug("Before adding value to length constraint. value = {}", asInt); + propertyConstraint = new LengthConstraint(asInt); + break; + } else { + log.warn("The value of length constraint is null"); + } + break; case MIN_LENGTH: if (value != null) { int asInt = value.getAsInt(); @@ -2237,6 +2262,16 @@ public class PropertyOperation extends AbstractOperation implements IPropertyOpe log.warn("The value of MinLengthConstraint is null"); } break; + case MAX_LENGTH: + if (value != null) { + int asInt = value.getAsInt(); + log.debug("Before adding value to max length constraint. value = {}", asInt); + propertyConstraint = new MaxLengthConstraint(asInt); + break; + } else { + log.warn("The value of max length constraint is null"); + } + break; default: log.warn("Key {} is not supported. Ignored.", key); } @@ -2252,8 +2287,125 @@ public class PropertyOperation extends AbstractOperation implements IPropertyOpe public PropertyConstraint deserialize(com.fasterxml.jackson.core.JsonParser json, DeserializationContext context) throws IOException { ObjectCodec oc = json.getCodec(); JsonNode node = oc.readTree(json); + PropertyConstraint propertyConstraint = null; + + Iterator<Entry<String, JsonNode>> fieldsIterator = node.fields(); + while (fieldsIterator.hasNext()) { + Entry<String, JsonNode> field = fieldsIterator.next(); + ConstraintType constraintType = ConstraintType.findByType(field.getKey()).orElse(null); + JsonNode value = field.getValue(); + + if (constraintType == null) { + log.warn("ConstraintType was not found for constraint name:{}", field.getKey()); + } else { + if (value == null) { + log.warn("The value of " + constraintType + " constraint is null"); + } + switch (constraintType) { + case EQUAL: + propertyConstraint = deserializeConstraintWithStringOperand(value, EqualConstraint.class); + break; + case IN_RANGE: + propertyConstraint = deserializeInRangeConstraintConstraint(value); + break; + case GREATER_THAN: + propertyConstraint = deserializeConstraintWithStringOperand(value, GreaterThanConstraint.class); + break; + case LESS_THAN: + propertyConstraint = deserializeConstraintWithStringOperand(value, LessThanConstraint.class); + break; + case GREATER_OR_EQUAL: + propertyConstraint = deserializeConstraintWithStringOperand(value, GreaterOrEqualConstraint.class); + break; + case LESS_OR_EQUAL: + propertyConstraint = deserializeConstraintWithStringOperand(value, LessOrEqualConstraint.class); + break; + case VALID_VALUES: + propertyConstraint = deserializeValidValuesConstraint(value); + break; + case LENGTH: + propertyConstraint = deserializeConstraintWithIntegerOperand(value, LengthConstraint.class); + break; + case MIN_LENGTH: + propertyConstraint = deserializeConstraintWithIntegerOperand(value, MinLengthConstraint.class); + break; + case MAX_LENGTH: + propertyConstraint = deserializeConstraintWithIntegerOperand(value, MaxLengthConstraint.class); + break; + default: + log.warn("Key {} is not supported. Ignored.", field.getKey()); + } + } + } + + return propertyConstraint; + } + + private PropertyConstraint deserializeConstraintWithStringOperand(JsonNode value, Class<? extends PropertyConstraint> constraintClass) { + String asString = value.asText(); + log.debug("Before adding value to {} object. value = {}", constraintClass, asString); + try { + return constraintClass.getConstructor(String.class).newInstance(asString); + } catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException + | SecurityException exception) { + log.error("Error deserializing constraint", exception); + return null; + } + } + + private PropertyConstraint deserializeConstraintWithIntegerOperand(JsonNode value, Class<? extends PropertyConstraint> constraintClass) { + Integer asInt = value.asInt(); + log.debug("Before adding value to {} object. value = {}", constraintClass, asInt); + try { + return constraintClass.getConstructor(Integer.class).newInstance(asInt); + } catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException + | SecurityException exception) { + log.error("Error deserializing constraint", exception); + return null; + } + } + + private PropertyConstraint deserializeInRangeConstraintConstraint(JsonNode value) { + if (value instanceof ArrayNode) { + ArrayNode rangeArray = (ArrayNode) value; + if (rangeArray.size() != 2) { + log.error("The range constraint content is invalid. value = {}", value); + } else { + InRangeConstraint rangeConstraint = new InRangeConstraint(); + String minValue = rangeArray.get(0).asText(); + String maxValue; + JsonNode maxElement = rangeArray.get(1); + if (maxElement.isNull()) { + maxValue = null; + } else { + maxValue = maxElement.asText(); + } + rangeConstraint.setRangeMinValue(minValue); + rangeConstraint.setRangeMaxValue(maxValue); + return rangeConstraint; + } + } return null; } + + private PropertyConstraint deserializeValidValuesConstraint(JsonNode value) { + ArrayNode rangeArray = (ArrayNode) value; + if (rangeArray.size() == 0) { + log.error("The valid values constraint content is invalid. value = {}", value); + } else { + ValidValuesConstraint vvConstraint = new ValidValuesConstraint(); + List<String> validValues = new ArrayList<>(); + for (JsonNode jsonElement : rangeArray) { + String item = jsonElement.asText(); + validValues.add(item); + } + vvConstraint.setValidValues(validValues); + return vvConstraint; + } + return null; + } + + } } diff --git a/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/constraints/AbstractPropertyConstraint.java b/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/constraints/AbstractPropertyConstraint.java index 4b4c0a66d0..ef94baf33b 100644 --- a/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/constraints/AbstractPropertyConstraint.java +++ b/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/constraints/AbstractPropertyConstraint.java @@ -43,7 +43,7 @@ public abstract class AbstractPropertyConstraint implements PropertyConstraint { public String getErrorMessage(ToscaType toscaType, ConstraintFunctionalException e, String propertyName, String errorMessage, String... propertyValue) { if (e instanceof ConstraintViolationException) { - return String.format(errorMessage, propertyName, Arrays.toString(propertyValue)); + return String.format(errorMessage, propertyName, propertyValue.length == 1 ? propertyValue[0] : Arrays.toString(propertyValue)); } return String.format(INVALID_VALUE_ERROR_MESSAGE, propertyName, toscaType.getType()); } diff --git a/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/constraints/EqualConstraint.java b/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/constraints/EqualConstraint.java index c4b26f3077..0fdc570262 100644 --- a/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/constraints/EqualConstraint.java +++ b/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/constraints/EqualConstraint.java @@ -28,30 +28,28 @@ import org.openecomp.sdc.be.model.tosca.constraints.exception.ConstraintFunction import org.openecomp.sdc.be.model.tosca.constraints.exception.ConstraintValueDoNotMatchPropertyTypeException; import org.openecomp.sdc.be.model.tosca.constraints.exception.ConstraintViolationException; import org.openecomp.sdc.be.model.tosca.constraints.exception.PropertyConstraintException; +import lombok.Getter; @SuppressWarnings("serial") public class EqualConstraint extends AbstractPropertyConstraint implements Serializable { @NotNull - private String constraintValue; + @Getter + private String equal; private Object typed; - public EqualConstraint(String constraintValue) { + public EqualConstraint(String equal) { super(); - this.constraintValue = constraintValue; - } - - public String getConstraintValue() { - return constraintValue; + this.equal = equal; } @Override public void initialize(ToscaType propertyType) throws ConstraintValueDoNotMatchPropertyTypeException { - if (propertyType.isValidValue(constraintValue)) { - typed = propertyType.convert(constraintValue); + if (propertyType.isValidValue(equal)) { + typed = propertyType.convert(equal); } else { throw new ConstraintValueDoNotMatchPropertyTypeException( - "constraintValue constraint has invalid value <" + constraintValue + "> property type is <" + propertyType.toString() + ">"); + "constraintValue constraint has invalid value <" + equal + "> property type is <" + propertyType.toString() + ">"); } } @@ -63,8 +61,6 @@ public class EqualConstraint extends AbstractPropertyConstraint implements Seria } } else if (typed == null) { fail(propertyValue); - } else if (typed instanceof Comparable && typed != propertyValue) { - fail(propertyValue); } else if (!typed.equals(propertyValue)) { fail(propertyValue); } @@ -81,11 +77,11 @@ public class EqualConstraint extends AbstractPropertyConstraint implements Seria private void fail(Object propertyValue) throws ConstraintViolationException { throw new ConstraintViolationException( - "Equal constraint violation, the reference is <" + constraintValue + "> but the value to compare is <" + propertyValue + ">"); + "Equal constraint violation, the reference is <" + equal + "> but the value to compare is <" + propertyValue + ">"); } @Override public String getErrorMessage(ToscaType toscaType, ConstraintFunctionalException e, String propertyName) { - return getErrorMessage(toscaType, e, propertyName, "%s property value must be %s", constraintValue); + return getErrorMessage(toscaType, e, propertyName, "%s property value must be %s", equal); } } diff --git a/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/constraints/GreaterOrEqualConstraint.java b/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/constraints/GreaterOrEqualConstraint.java index d32ab721fc..00a84610c5 100644 --- a/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/constraints/GreaterOrEqualConstraint.java +++ b/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/constraints/GreaterOrEqualConstraint.java @@ -62,6 +62,6 @@ public class GreaterOrEqualConstraint extends AbstractComparablePropertyConstrai @Override public String getErrorMessage(ToscaType toscaType, ConstraintFunctionalException e, String propertyName) { - return getErrorMessage(toscaType, e, propertyName, "%f property value must be >= %f", greaterOrEqual); + return getErrorMessage(toscaType, e, propertyName, "%s property value must be greater than or equal to %s", greaterOrEqual); } } diff --git a/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/constraints/GreaterThanConstraint.java b/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/constraints/GreaterThanConstraint.java index e50c8fe3d0..a819a8e358 100644 --- a/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/constraints/GreaterThanConstraint.java +++ b/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/constraints/GreaterThanConstraint.java @@ -68,6 +68,6 @@ public class GreaterThanConstraint extends AbstractComparablePropertyConstraint @Override public String getErrorMessage(ToscaType toscaType, ConstraintFunctionalException e, String propertyName) { - return getErrorMessage(toscaType, e, propertyName, "%f property value must be > %f", greaterThan); + return getErrorMessage(toscaType, e, propertyName, "%s property value must be greater than %s", greaterThan); } } diff --git a/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/constraints/InRangeConstraint.java b/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/constraints/InRangeConstraint.java index d574816c5f..ad871e5087 100644 --- a/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/constraints/InRangeConstraint.java +++ b/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/constraints/InRangeConstraint.java @@ -125,7 +125,7 @@ public class InRangeConstraint extends AbstractPropertyConstraint { @Override public String getErrorMessage(ToscaType toscaType, ConstraintFunctionalException e, String propertyName) { - return getErrorMessage(toscaType, e, propertyName, "%f property value must be between >= [%s] and <= [%s]", String.valueOf(min), + return getErrorMessage(toscaType, e, propertyName, "%s property value must be between >= [%s] and <= [%s]", String.valueOf(min), String.valueOf(max)); } } diff --git a/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/constraints/LengthConstraint.java b/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/constraints/LengthConstraint.java index 457c824ac6..191993ad89 100644 --- a/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/constraints/LengthConstraint.java +++ b/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/constraints/LengthConstraint.java @@ -28,7 +28,11 @@ import org.openecomp.sdc.be.model.tosca.ToscaType; import org.openecomp.sdc.be.model.tosca.constraints.exception.ConstraintFunctionalException; import org.openecomp.sdc.be.model.tosca.constraints.exception.ConstraintViolationException; import org.openecomp.sdc.be.model.tosca.constraints.exception.PropertyConstraintException; +import lombok.AllArgsConstructor; +import lombok.NoArgsConstructor; +@AllArgsConstructor +@NoArgsConstructor public class LengthConstraint extends AbstractPropertyConstraint { @NotNull diff --git a/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/constraints/LessOrEqualConstraint.java b/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/constraints/LessOrEqualConstraint.java index 62cec05a73..86c6383677 100644 --- a/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/constraints/LessOrEqualConstraint.java +++ b/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/constraints/LessOrEqualConstraint.java @@ -68,6 +68,6 @@ public class LessOrEqualConstraint extends AbstractComparablePropertyConstraint @Override public String getErrorMessage(ToscaType toscaType, ConstraintFunctionalException e, String propertyName) { - return getErrorMessage(toscaType, e, propertyName, "%s property value must be <= %s", lessOrEqual); + return getErrorMessage(toscaType, e, propertyName, "%s property value must be less than or equal to %s", lessOrEqual); } } diff --git a/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/constraints/LessThanConstraint.java b/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/constraints/LessThanConstraint.java index 1f6cb63693..d0bea82157 100644 --- a/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/constraints/LessThanConstraint.java +++ b/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/constraints/LessThanConstraint.java @@ -30,6 +30,7 @@ import org.openecomp.sdc.be.model.tosca.constraints.exception.ConstraintValueDoN import org.openecomp.sdc.be.model.tosca.constraints.exception.ConstraintViolationException; import org.openecomp.sdc.be.model.tosca.constraints.exception.PropertyConstraintException; +@Getter @AllArgsConstructor public class LessThanConstraint extends AbstractComparablePropertyConstraint { @@ -60,6 +61,6 @@ public class LessThanConstraint extends AbstractComparablePropertyConstraint { @Override public String getErrorMessage(ToscaType toscaType, ConstraintFunctionalException e, String propertyName) { - return getErrorMessage(toscaType, e, propertyName, "%s value must be < %s", lessThan); + return getErrorMessage(toscaType, e, propertyName, "%s value must be less than %s", lessThan); } } diff --git a/catalog-model/src/test/java/org/openecomp/sdc/be/model/operations/impl/PropertyOperationTest.java b/catalog-model/src/test/java/org/openecomp/sdc/be/model/operations/impl/PropertyOperationTest.java index ff41d5dd02..33322ecfb6 100644 --- a/catalog-model/src/test/java/org/openecomp/sdc/be/model/operations/impl/PropertyOperationTest.java +++ b/catalog-model/src/test/java/org/openecomp/sdc/be/model/operations/impl/PropertyOperationTest.java @@ -60,9 +60,16 @@ import org.openecomp.sdc.be.model.PropertyDefinition; import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus; import org.openecomp.sdc.be.model.tosca.ToscaPropertyType; import org.openecomp.sdc.be.model.tosca.ToscaType; +import org.openecomp.sdc.be.model.tosca.constraints.EqualConstraint; +import org.openecomp.sdc.be.model.tosca.constraints.GreaterOrEqualConstraint; import org.openecomp.sdc.be.model.tosca.constraints.GreaterThanConstraint; import org.openecomp.sdc.be.model.tosca.constraints.InRangeConstraint; +import org.openecomp.sdc.be.model.tosca.constraints.LengthConstraint; import org.openecomp.sdc.be.model.tosca.constraints.LessOrEqualConstraint; +import org.openecomp.sdc.be.model.tosca.constraints.LessThanConstraint; +import org.openecomp.sdc.be.model.tosca.constraints.MaxLengthConstraint; +import org.openecomp.sdc.be.model.tosca.constraints.MinLengthConstraint; +import org.openecomp.sdc.be.model.tosca.constraints.ValidValuesConstraint; import org.openecomp.sdc.be.resources.data.DataTypeData; import org.openecomp.sdc.be.resources.data.PropertyData; import org.openecomp.sdc.be.resources.data.PropertyValueData; @@ -137,17 +144,45 @@ public class PropertyOperationTest extends ModelTestBase { private List<PropertyConstraint> buildConstraints() { List<PropertyConstraint> constraints = new ArrayList<>(); - GreaterThanConstraint propertyConstraint1 = new GreaterThanConstraint("0"); - LessOrEqualConstraint propertyConstraint2 = new LessOrEqualConstraint("10"); - List<String> range = new ArrayList<>(); - range.add("0"); - range.add("100"); - InRangeConstraint propertyConstraint3 = new InRangeConstraint(range); + EqualConstraint propertyConstraint1 = new EqualConstraint("0"); + GreaterThanConstraint propertyConstraint2 = new GreaterThanConstraint("1"); + GreaterOrEqualConstraint propertyConstraint3 = new GreaterOrEqualConstraint("3"); + LessThanConstraint propertyConstraint4 = new LessThanConstraint("5"); + LessOrEqualConstraint propertyConstraint5 = new LessOrEqualConstraint("7"); + InRangeConstraint propertyConstraint6 = buildInRangeConstraint(); + ValidValuesConstraint propertyConstraint7 = buildValidValuesConstraint(); + LengthConstraint propertyConstraint8 = new LengthConstraint(9); + MinLengthConstraint propertyConstraint9 = new MinLengthConstraint(11); + MaxLengthConstraint propertyConstraint10 = new MaxLengthConstraint(13); constraints.add(propertyConstraint1); constraints.add(propertyConstraint2); constraints.add(propertyConstraint3); + constraints.add(propertyConstraint4); + constraints.add(propertyConstraint5); + constraints.add(propertyConstraint6); + constraints.add(propertyConstraint7); + constraints.add(propertyConstraint8); + constraints.add(propertyConstraint9); + constraints.add(propertyConstraint10); return constraints; } + + private InRangeConstraint buildInRangeConstraint() { + List<String> range = new ArrayList<>(); + range.add("23"); + range.add("67"); + InRangeConstraint inRangeConstraint = new InRangeConstraint(range); + return inRangeConstraint; + } + + private ValidValuesConstraint buildValidValuesConstraint() { + List<String> validValues = new ArrayList<>(); + validValues.add("abc"); + validValues.add("def"); + validValues.add("fhi"); + ValidValuesConstraint validValuesConstraint = new ValidValuesConstraint(validValues); + return validValuesConstraint; + } @Test public void findPropertyValueBestMatch1() { |