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