}
private static void setPropertyConstraints(Map<String, Object> propertyValue, PropertyDefinition property) {
- List<PropertyConstraint> constraints = getPropertyConstraints(propertyValue, property.getType());
+ List<PropertyConstraint> constraints = getPropertyConstraints(propertyValue, property.getType(), property.getSchema());
if (CollectionUtils.isNotEmpty(constraints)) {
property.setConstraints(constraints);
}
}
- private static List<PropertyConstraint> getPropertyConstraints(final Map<String, Object> propertyValue, final String propertyType) {
+ private static List<PropertyConstraint> getPropertyConstraints(final Map<String, Object> propertyValue, final String propertyType, final SchemaDefinition schema) {
final List<Object> propertyFieldConstraints = findCurrentLevelConstraintsElement(propertyValue);
if (CollectionUtils.isEmpty(propertyFieldConstraints)) {
return Collections.emptyList();
}.getType();
final Gson gson = new GsonBuilder().registerTypeAdapter(constraintType, new PropertyConstraintDeserialiser()).create();
for (final Object constraintJson : propertyFieldConstraints) {
- final PropertyConstraint propertyConstraint = validateAndGetPropertyConstraint(propertyType, constraintType, gson, constraintJson);
+ final PropertyConstraint propertyConstraint = validateAndGetPropertyConstraint(propertyType, constraintType, gson, constraintJson, schema);
if (propertyConstraint != null) {
constraintList.add(propertyConstraint);
}
return constraints;
}
- private static PropertyConstraint validateAndGetPropertyConstraint(String propertyType, Type constraintType, Gson gson, Object constraintJson) {
+ private static PropertyConstraint validateAndGetPropertyConstraint(String propertyType, Type constraintType, Gson gson, Object constraintJson, SchemaDefinition schema) {
PropertyConstraint propertyConstraint;
try {
propertyConstraint = gson.fromJson(gson.toJson(constraintJson), constraintType);
}
if (propertyConstraint instanceof ValidValuesConstraint) {
try {
- ((ValidValuesConstraint) propertyConstraint).validateType(propertyType);
+ ((ValidValuesConstraint) propertyConstraint).validateType(propertyType, schema);
boolean valid = ((ValidValuesConstraint) propertyConstraint).validateValueType(propertyType);
if (!valid) {
((ValidValuesConstraint) propertyConstraint).changeConstraintValueTypeTo(propertyType);
if (isPropertyNotMappedAsInput(propertyDefinition) && CollectionUtils.isNotEmpty(propertyDefinition.getConstraints())) {
for (PropertyConstraint propertyConstraint : propertyDefinition.getConstraints()) {
try {
- propertyConstraint.initialize(toscaType);
- propertyConstraint.validate(toscaType, propertyDefinition.getValue());
+ propertyConstraint.initialize(toscaType, propertyDefinition.getSchema());
+ propertyConstraint.validate(toscaType, propertyDefinition.getSchema(), propertyDefinition.getValue());
} catch (ConstraintValueDoNotMatchPropertyTypeException | ConstraintViolationException exception) {
errorMessages.add(propertyConstraint.getErrorMessage(toscaType, exception, getCompletePropertyName(propertyDefinition)));
} catch (IllegalArgumentException ie) {
import com.fasterxml.jackson.annotation.JsonIgnore;
import org.openecomp.sdc.be.model.tosca.ToscaType;
+import org.openecomp.sdc.be.datatypes.elements.SchemaDefinition;
import org.openecomp.sdc.be.datatypes.enums.ConstraintType;
import org.openecomp.sdc.be.model.tosca.constraints.exception.ConstraintFunctionalException;
import org.openecomp.sdc.be.model.tosca.constraints.exception.ConstraintValueDoNotMatchPropertyTypeException;
import org.openecomp.sdc.be.model.tosca.constraints.exception.PropertyConstraintException;
public interface PropertyConstraint {
-
- void initialize(ToscaType propertyType) throws ConstraintValueDoNotMatchPropertyTypeException;
-
+
+ void initialize(ToscaType propertyType, SchemaDefinition schema) throws ConstraintValueDoNotMatchPropertyTypeException;
+
void validate(Object propertyValue) throws ConstraintViolationException;
- void validate(ToscaType toscaType, String propertyTextValue) throws ConstraintViolationException;
+ void validate(ToscaType toscaType, SchemaDefinition schema, String propertyTextValue) throws ConstraintViolationException;
@JsonIgnore
ConstraintType getConstraintType();
package org.openecomp.sdc.be.model.tosca.constraints;
import java.util.Arrays;
+
+import org.openecomp.sdc.be.datatypes.elements.SchemaDefinition;
import org.openecomp.sdc.be.model.PropertyConstraint;
import org.openecomp.sdc.be.model.tosca.ToscaType;
import org.openecomp.sdc.be.model.tosca.constraints.exception.ConstraintFunctionalException;
private static final String INVALID_VALUE_ERROR_MESSAGE = "Unsupported value provided for %s property supported value type is %s.";
@Override
- public void validate(ToscaType toscaType, String propertyTextValue) throws ConstraintViolationException {
+ public void validate(ToscaType toscaType, SchemaDefinition schema, String propertyTextValue) throws ConstraintViolationException {
+ validate(toscaType, propertyTextValue);
+ }
+
+ protected void validate(ToscaType toscaType, String propertyTextValue) throws ConstraintViolationException {
try {
validate(toscaType.convert(propertyTextValue));
} catch (ApplicationVersionException e) {
return String.format(INVALID_VALUE_ERROR_MESSAGE, propertyName, toscaType.getType());
}
- @Override
- public void initialize(ToscaType propertyType) throws ConstraintValueDoNotMatchPropertyTypeException {
+ protected void initialize(ToscaType propertyType) throws ConstraintValueDoNotMatchPropertyTypeException {
//Initialization not needed for few constraints for now might be needed in future
}
+
+ @Override
+ public void initialize(ToscaType propertyType, SchemaDefinition schema) throws ConstraintValueDoNotMatchPropertyTypeException {
+ initialize(propertyType);
+ }
}
import static java.util.stream.Collectors.toList;
+import java.util.Collection;
+import java.util.Collections;
+
+import com.fasterxml.jackson.core.type.TypeReference;
import com.google.common.collect.Sets;
import java.util.List;
+import java.util.Map;
import java.util.Set;
import javax.validation.constraints.NotNull;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.openecomp.sdc.be.dao.api.ActionStatus;
+import org.openecomp.sdc.be.datatypes.elements.SchemaDefinition;
import org.openecomp.sdc.be.datatypes.enums.ConstraintType;
import org.openecomp.sdc.be.model.PropertyConstraint;
import org.openecomp.sdc.be.model.tosca.ToscaType;
public ValidValuesConstraint(List<Object> validValues) {
this.validValues = validValues;
}
-
+
@Override
- public void initialize(ToscaType propertyType) throws ConstraintValueDoNotMatchPropertyTypeException {
+ public void initialize(ToscaType propertyType, SchemaDefinition schema) throws ConstraintValueDoNotMatchPropertyTypeException {
+ ToscaType toscaType = getValuesType(propertyType, schema);
validValuesTyped = Sets.newHashSet();
if (validValues == null) {
throw new ConstraintValueDoNotMatchPropertyTypeException(
"validValues constraint has invalid value <> property type is <" + propertyType.toString() + ">");
}
for (Object value : validValues) {
- if (!propertyType.isValidValue(String.valueOf(value))) {
+ if (!toscaType.isValidValue(String.valueOf(value))) {
throw new ConstraintValueDoNotMatchPropertyTypeException(
"validValues constraint has invalid value <" + value + PROPERTY_TYPE_IS + propertyType.toString() + ">");
} else {
- validValuesTyped.add(propertyType.convert(String.valueOf(value)));
+ validValuesTyped.add(toscaType.convert(String.valueOf(value)));
}
}
}
- public void validateType(String propertyType) throws ConstraintValueDoNotMatchPropertyTypeException {
- ToscaType toscaType = ToscaType.getToscaType(propertyType);
+ public void validateType(String propertyType, SchemaDefinition schema) throws ConstraintValueDoNotMatchPropertyTypeException {
+ ToscaType toscaType = getValuesType(ToscaType.getToscaType(propertyType), schema);
if (toscaType == null) {
throw new ConstraintValueDoNotMatchPropertyTypeException(
"validValues constraint has invalid values <" + validValues.toString() + PROPERTY_TYPE_IS + propertyType + ">");
}
}
}
+
+ private ToscaType getValuesType(ToscaType propertyType, SchemaDefinition schema) {
+ return ToscaType.isCollectionType(propertyType.getType()) ? ToscaType.getToscaType(schema.getProperty().getType()) : propertyType;
+ }
@Override
public void validateValueOnUpdate(PropertyConstraint newConstraint) throws PropertyConstraintException {
}
}
}
+
+ @Override
+ public void validate(ToscaType toscaType, SchemaDefinition schema, String propertyTextValue) throws ConstraintViolationException {
+ try {
+ Collection<Object> valuesToValidate;
+ if (ToscaType.LIST == toscaType) {
+ valuesToValidate = ConstraintUtil.parseToCollection(propertyTextValue, new TypeReference<>() {});
+ } else if (ToscaType.MAP == toscaType) {
+ final Map<String, Object> map = ConstraintUtil.parseToCollection(propertyTextValue, new TypeReference<>() {});
+ valuesToValidate = map.values();
+ } else {
+ valuesToValidate = Collections.singleton(propertyTextValue);
+ }
+ ToscaType valuesType = getValuesType(toscaType, schema);
+ for (final Object value: valuesToValidate) {
+ validate(valuesType, value.toString());
+ }
+ } catch (ConstraintValueDoNotMatchPropertyTypeException exception) {
+ throw new ConstraintViolationException("Value cannot be parsed to a list", exception);
+ }
+ }
@Override
public void validate(Object propertyValue) throws ConstraintViolationException {