2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
20 package org.openecomp.sdc.be.model.tosca.constraints;
22 import static java.util.stream.Collectors.toList;
24 import com.google.common.collect.Sets;
25 import java.util.List;
27 import javax.validation.constraints.NotNull;
28 import org.openecomp.sdc.be.dao.api.ActionStatus;
29 import org.openecomp.sdc.be.datatypes.enums.ConstraintType;
30 import org.openecomp.sdc.be.model.PropertyConstraint;
31 import org.openecomp.sdc.be.model.tosca.ToscaType;
32 import org.openecomp.sdc.be.model.tosca.constraints.exception.ConstraintFunctionalException;
33 import org.openecomp.sdc.be.model.tosca.constraints.exception.ConstraintValueDoNotMatchPropertyTypeException;
34 import org.openecomp.sdc.be.model.tosca.constraints.exception.ConstraintViolationException;
35 import org.openecomp.sdc.be.model.tosca.constraints.exception.PropertyConstraintException;
37 public class ValidValuesConstraint extends AbstractPropertyConstraint {
39 private static final String PROPERTY_TYPE_IS = "> property type is <";
41 private List<String> validValues;
42 private Set<Object> validValuesTyped;
44 public ValidValuesConstraint(List<String> validValues) {
45 this.validValues = validValues;
48 public ValidValuesConstraint() {
52 public void initialize(ToscaType propertyType) throws ConstraintValueDoNotMatchPropertyTypeException {
53 validValuesTyped = Sets.newHashSet();
54 if (validValues == null) {
55 throw new ConstraintValueDoNotMatchPropertyTypeException(
56 "validValues constraint has invalid value <> property type is <" + propertyType.toString() + ">");
58 for (String value : validValues) {
59 if (!propertyType.isValidValue(value)) {
60 throw new ConstraintValueDoNotMatchPropertyTypeException(
61 "validValues constraint has invalid value <" + value + PROPERTY_TYPE_IS + propertyType.toString() + ">");
63 validValuesTyped.add(propertyType.convert(value));
68 public void validateType(String propertyType) throws ConstraintValueDoNotMatchPropertyTypeException {
69 ToscaType toscaType = ToscaType.getToscaType(propertyType);
70 if (toscaType == null) {
71 throw new ConstraintValueDoNotMatchPropertyTypeException(
72 "validValues constraint has invalid values <" + validValues.toString() + PROPERTY_TYPE_IS + propertyType + ">");
74 if (validValues == null) {
75 throw new ConstraintValueDoNotMatchPropertyTypeException(
76 "validValues constraint has invalid value <> property type is <" + propertyType + ">");
78 for (String value : validValues) {
79 if (!toscaType.isValidValue(value)) {
80 throw new ConstraintValueDoNotMatchPropertyTypeException(
81 "validValues constraint has invalid value <" + value + PROPERTY_TYPE_IS + propertyType + ">");
87 public void validateValueOnUpdate(PropertyConstraint newConstraint) throws PropertyConstraintException {
88 if (newConstraint.getConstraintType() == getConstraintType()) {
89 if (!((ValidValuesConstraint) newConstraint).getValidValues().containsAll(validValues)) {
90 throw new PropertyConstraintException("Deletion of exists value is not permitted", null, null,
91 ActionStatus.CANNOT_DELETE_VALID_VALUES, getConstraintType().name(),
92 validValues.stream().filter(v -> !((ValidValuesConstraint) newConstraint).getValidValues().contains(v)).collect(toList())
99 public void validate(Object propertyValue) throws ConstraintViolationException {
100 if (propertyValue == null) {
101 throw new ConstraintViolationException("Value to validate is null");
103 if (!validValuesTyped.contains(propertyValue)) {
104 throw new ConstraintViolationException("The value is not in the list of valid values");
108 public List<String> getValidValues() {
112 public void setValidValues(List<String> validValues) {
113 this.validValues = validValues;
117 public ConstraintType getConstraintType() {
118 return ConstraintType.VALID_VALUES;
122 public String getErrorMessage(ToscaType toscaType, ConstraintFunctionalException e, String propertyName) {
123 return getErrorMessage(toscaType, e, propertyName, "%s valid value must be one of the following: [%s]", String.join(",", validValues));