Validate service input default values against constraints
[sdc.git] / catalog-model / src / main / java / org / openecomp / sdc / be / model / tosca / constraints / EqualConstraint.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 javax.validation.constraints.NotNull;
24 import lombok.EqualsAndHashCode;
25 import lombok.Getter;
26 import lombok.Setter;
27 import org.openecomp.sdc.be.datatypes.enums.ConstraintType;
28 import org.openecomp.sdc.be.model.PropertyConstraint;
29 import org.openecomp.sdc.be.model.tosca.ToscaType;
30 import org.openecomp.sdc.be.model.tosca.constraints.exception.ConstraintFunctionalException;
31 import org.openecomp.sdc.be.model.tosca.constraints.exception.ConstraintValueDoNotMatchPropertyTypeException;
32 import org.openecomp.sdc.be.model.tosca.constraints.exception.ConstraintViolationException;
33 import org.openecomp.sdc.be.model.tosca.constraints.exception.PropertyConstraintException;
34
35 @SuppressWarnings("serial")
36 @EqualsAndHashCode(onlyExplicitlyIncluded = true)
37 public class EqualConstraint extends AbstractComparablePropertyConstraint {
38
39     @Getter
40     @Setter
41     @NotNull
42     @EqualsAndHashCode.Include
43     private Object equal;
44     private Object typed;
45
46     public EqualConstraint(Object equal) {
47         super();
48         this.equal = equal;
49     }
50
51     @Override
52     public void initialize(ToscaType propertyType) throws ConstraintValueDoNotMatchPropertyTypeException {
53         if (propertyType.isValidValue(String.valueOf(equal))) {
54             typed = propertyType.convert(String.valueOf(equal));
55             if (propertyType.equals(ToscaType.BOOLEAN)) {
56                 return;
57             }
58             initialize(String.valueOf(equal), propertyType);
59         } else {
60             throw new ConstraintValueDoNotMatchPropertyTypeException(
61                 "constraintValue constraint has invalid value <" + equal + "> property type is <" + propertyType.toString() + ">");
62         }
63     }
64
65     @Override
66     public ConstraintType getConstraintType() {
67         return ConstraintType.EQUAL;
68     }
69
70     @Override
71     public void validateValueOnUpdate(PropertyConstraint newConstraint) throws PropertyConstraintException {
72     }
73
74     private void fail(Object propertyValue) throws ConstraintViolationException {
75         throw new ConstraintViolationException(
76             "Equal constraint violation, the reference is <" + equal + "> but the value to compare is <" + propertyValue + ">");
77     }
78
79     @Override
80     public String getErrorMessage(ToscaType toscaType, ConstraintFunctionalException e, String propertyName) {
81         return getErrorMessage(toscaType, e, propertyName, "'%s' value must be %s", String.valueOf(equal));
82     }
83
84     @Override
85     protected void doValidate(Object propertyValue) throws ConstraintViolationException {
86         if (propertyValue == null) {
87             if (typed != null) {
88                 fail(null);
89             }
90         } else if (typed == null) {
91             fail(propertyValue);
92         } else if (!typed.equals(propertyValue)) {
93             fail(propertyValue);
94         }
95     }
96
97     @Override
98     public String toString() {
99         return String.valueOf(equal);
100     }
101
102     @Override
103     public void validate(Object propertyValue) throws ConstraintViolationException {
104         if (propertyValue == null) {
105             throw new ConstraintViolationException("Value to check is null");
106         }
107         if (!(propertyValue instanceof Boolean)) {
108             super.validate(propertyValue);
109         }
110         doValidate(propertyValue);
111     }
112
113     public boolean validateValueType(String propertyType) throws ConstraintValueDoNotMatchPropertyTypeException {
114         ToscaType toscaType = ToscaType.getToscaType(propertyType);
115         if (toscaType == null) {
116             throw new ConstraintValueDoNotMatchPropertyTypeException(
117                 "equal constraint has invalid values <" + equal.toString() + "> property type is <" + propertyType + ">");
118         }
119         if (equal == null) {
120             throw new ConstraintValueDoNotMatchPropertyTypeException(
121                 "equal constraint has invalid value <> property type is <" + propertyType + ">");
122         }
123         return toscaType.isValueTypeValid(equal);
124     }
125
126     public void changeConstraintValueTypeTo(String propertyType) throws ConstraintValueDoNotMatchPropertyTypeException {
127         ToscaType toscaType = ToscaType.getToscaType(propertyType);
128         try {
129             equal = toscaType.convert(String.valueOf(equal));
130         } catch (Exception e) {
131             throw new ConstraintValueDoNotMatchPropertyTypeException(
132                 "equal constraint has invalid values <" + equal.toString() + "> property type is <" + propertyType + ">");
133         }
134     }
135 }