Reformat catalog-model
[sdc.git] / catalog-model / src / main / java / org / openecomp / sdc / be / model / tosca / constraints / ValidValuesConstraint.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20 package org.openecomp.sdc.be.model.tosca.constraints;
21
22 import static java.util.stream.Collectors.toList;
23
24 import com.google.common.collect.Sets;
25 import java.util.List;
26 import java.util.Set;
27 import javax.validation.constraints.NotNull;
28 import org.openecomp.sdc.be.dao.api.ActionStatus;
29 import org.openecomp.sdc.be.model.PropertyConstraint;
30 import org.openecomp.sdc.be.model.tosca.ToscaType;
31 import org.openecomp.sdc.be.model.tosca.constraints.exception.ConstraintFunctionalException;
32 import org.openecomp.sdc.be.model.tosca.constraints.exception.ConstraintValueDoNotMatchPropertyTypeException;
33 import org.openecomp.sdc.be.model.tosca.constraints.exception.ConstraintViolationException;
34 import org.openecomp.sdc.be.model.tosca.constraints.exception.PropertyConstraintException;
35
36 public class ValidValuesConstraint extends AbstractPropertyConstraint {
37
38     private static final String PROPERTY_TYPE_IS = "> property type is <";
39     @NotNull
40     private List<String> validValues;
41     private Set<Object> validValuesTyped;
42
43     public ValidValuesConstraint(List<String> validValues) {
44         this.validValues = validValues;
45     }
46
47     public ValidValuesConstraint() {
48     }
49
50     @Override
51     public void initialize(ToscaType propertyType) throws ConstraintValueDoNotMatchPropertyTypeException {
52         validValuesTyped = Sets.newHashSet();
53         if (validValues == null) {
54             throw new ConstraintValueDoNotMatchPropertyTypeException(
55                 "validValues constraint has invalid value <> property type is <" + propertyType.toString() + ">");
56         }
57         for (String value : validValues) {
58             if (!propertyType.isValidValue(value)) {
59                 throw new ConstraintValueDoNotMatchPropertyTypeException(
60                     "validValues constraint has invalid value <" + value + PROPERTY_TYPE_IS + propertyType.toString() + ">");
61             } else {
62                 validValuesTyped.add(propertyType.convert(value));
63             }
64         }
65     }
66
67     public void validateType(String propertyType) throws ConstraintValueDoNotMatchPropertyTypeException {
68         ToscaType toscaType = ToscaType.getToscaType(propertyType);
69         if (toscaType == null) {
70             throw new ConstraintValueDoNotMatchPropertyTypeException(
71                 "validValues constraint has invalid values <" + validValues.toString() + PROPERTY_TYPE_IS + propertyType + ">");
72         }
73         if (validValues == null) {
74             throw new ConstraintValueDoNotMatchPropertyTypeException(
75                 "validValues constraint has invalid value <> property type is <" + propertyType + ">");
76         }
77         for (String value : validValues) {
78             if (!toscaType.isValidValue(value)) {
79                 throw new ConstraintValueDoNotMatchPropertyTypeException(
80                     "validValues constraint has invalid value <" + value + PROPERTY_TYPE_IS + propertyType + ">");
81             }
82         }
83     }
84
85     @Override
86     public void validateValueOnUpdate(PropertyConstraint newConstraint) throws PropertyConstraintException {
87         if (newConstraint.getConstraintType() == getConstraintType()) {
88             if (!((ValidValuesConstraint) newConstraint).getValidValues().containsAll(validValues)) {
89                 throw new PropertyConstraintException("Deletion of exists value is not permitted", null, null,
90                     ActionStatus.CANNOT_DELETE_VALID_VALUES, getConstraintType().name(),
91                     validValues.stream().filter(v -> !((ValidValuesConstraint) newConstraint).getValidValues().contains(v)).collect(toList())
92                         .toString());
93             }
94         }
95     }
96
97     @Override
98     public void validate(Object propertyValue) throws ConstraintViolationException {
99         if (propertyValue == null) {
100             throw new ConstraintViolationException("Value to validate is null");
101         }
102         if (!validValuesTyped.contains(propertyValue)) {
103             throw new ConstraintViolationException("The value is not in the list of valid values");
104         }
105     }
106
107     public List<String> getValidValues() {
108         return validValues;
109     }
110
111     public void setValidValues(List<String> validValues) {
112         this.validValues = validValues;
113     }
114
115     @Override
116     public ConstraintType getConstraintType() {
117         return ConstraintType.VALID_VALUES;
118     }
119
120     @Override
121     public String getErrorMessage(ToscaType toscaType, ConstraintFunctionalException e, String propertyName) {
122         return getErrorMessage(toscaType, e, propertyName, "%s valid value must be one of the following: [%s]", String.join(",", validValues));
123     }
124 }