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.PatternConstraint;
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.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.ToscaPropertyConstraintPattern;
import org.openecomp.sdc.be.tosca.model.ToscaPropertyConstraintValidValues;
import org.openecomp.sdc.be.tosca.model.ToscaSchemaDefinition;
import org.openecomp.sdc.common.log.wrappers.Logger;
if (constraint instanceof MaxLengthConstraint) {
convertedConstraints.add(new ToscaPropertyConstraintMaxLength(((MaxLengthConstraint) constraint).getMaxLength()));
}
+ if (constraint instanceof PatternConstraint) {
+ convertedConstraints.add(new ToscaPropertyConstraintPattern(((PatternConstraint) constraint).getPattern()));
+ }
}
return convertedConstraints;
}
--- /dev/null
+/*
+ * ============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 ToscaPropertyConstraintPattern implements ToscaPropertyConstraint {
+
+ private String pattern;
+ private static final ConstraintType CONSTRAINT_TYPE = ConstraintType.PATTERN;
+
+ @Override
+ public String getEntryToscaName(final String attributeName) {
+ if ("pattern".equals(attributeName)) {
+ return CONSTRAINT_TYPE.getType();
+ }
+ return attributeName;
+ }
+
+ @Override
+ public ConstraintType getConstraintType() {
+ return CONSTRAINT_TYPE;
+ }
+}
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.PatternConstraint;
import org.openecomp.sdc.be.model.tosca.constraints.ValidValuesConstraint;
import org.openecomp.sdc.be.model.tosca.converters.PropertyValueConverter;
import org.openecomp.sdc.be.model.validation.ToscaFunctionValidator;
log.warn("The value of max length constraint is null");
}
break;
+ case PATTERN:
+ if (value != null) {
+ String asString = value.getAsString();
+ log.debug("Before adding value to PatternConstraint object. value = {}", asString);
+ propertyConstraint = new PatternConstraint(asString);
+ break;
+ } else {
+ log.warn("The value of pattern constraint is null");
+ }
+ break;
default:
log.warn("Key {} is not supported. Ignored.", key);
}
case MAX_LENGTH:
propertyConstraint = deserializeConstraintWithIntegerOperand(value, MaxLengthConstraint.class);
break;
+ case PATTERN:
+ propertyConstraint = deserializeConstraintWithStringOperand(value, PatternConstraint.class);
+ break;
default:
log.warn("Key {} is not supported. Ignored.", field.getKey());
}
import java.util.regex.Pattern;
import javax.validation.constraints.NotNull;
+import lombok.NoArgsConstructor;
import org.openecomp.sdc.be.datatypes.enums.ConstraintType;
import org.openecomp.sdc.be.model.PropertyConstraint;
import org.openecomp.sdc.be.model.tosca.ToscaType;
import org.openecomp.sdc.be.model.tosca.constraints.exception.PropertyConstraintException;
import lombok.Getter;
+@NoArgsConstructor
public class PatternConstraint extends AbstractStringPropertyConstraint {
@NotNull
private String pattern;
private Pattern compiledPattern;
+ public PatternConstraint(String pattern) {
+ setPattern(pattern);
+ }
+
public void setPattern(String pattern) {
this.pattern = pattern;
this.compiledPattern = Pattern.compile(this.pattern);
if (Array.isArray(constraint.value)) {
return !(constraint.value.length == 0 || this.doesArrayContaintEmptyValues(constraint.value));
}
+ if (constraint.type == ConstraintTypes.pattern) {
+ try {
+ new RegExp(constraint.value);
+ this.valid = true;
+ } catch(e) {
+ this.valid = false;
+ }
+ }
return constraint.value && constraint.type != ConstraintTypes.null
});
}
MAX_LENGTH("max_length", "maxLength"),
VALID_VALUES("valid_values", "validValues"),
LESS_THAN("less_than", "lessThan"),
+ PATTERN("pattern"),
SCHEMA("schema");
private static final Set<ConstraintType> comparableConstraints = Set.of(ConstraintType.GREATER_THAN, ConstraintType.LESS_THAN);