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=========================================================
21 package org.openecomp.sdc.be.model.tosca.constraints;
23 import javax.validation.constraints.NotNull;
24 import lombok.EqualsAndHashCode;
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;
35 @SuppressWarnings("serial")
36 @EqualsAndHashCode(onlyExplicitlyIncluded = true)
37 public class EqualConstraint extends AbstractComparablePropertyConstraint {
42 @EqualsAndHashCode.Include
46 public EqualConstraint(Object equal) {
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)) {
58 initialize(String.valueOf(equal), propertyType);
60 throw new ConstraintValueDoNotMatchPropertyTypeException(
61 "constraintValue constraint has invalid value <" + equal + "> property type is <" + propertyType.toString() + ">");
66 public ConstraintType getConstraintType() {
67 return ConstraintType.EQUAL;
71 public void validateValueOnUpdate(PropertyConstraint newConstraint) throws PropertyConstraintException {
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 + ">");
80 public String getErrorMessage(ToscaType toscaType, ConstraintFunctionalException e, String propertyName) {
81 return getErrorMessage(toscaType, e, propertyName, "'%s' value must be %s", String.valueOf(equal));
85 protected void doValidate(Object propertyValue) throws ConstraintViolationException {
86 if (propertyValue == null) {
90 } else if (typed == null) {
92 } else if (!typed.equals(propertyValue)) {
98 public String toString() {
99 return String.valueOf(equal);
103 public void validate(Object propertyValue) throws ConstraintViolationException {
104 if (propertyValue == null) {
105 throw new ConstraintViolationException("Value to check is null");
107 if (!(propertyValue instanceof Boolean)) {
108 super.validate(propertyValue);
110 doValidate(propertyValue);
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 + ">");
120 throw new ConstraintValueDoNotMatchPropertyTypeException(
121 "equal constraint has invalid value <> property type is <" + propertyType + ">");
123 return toscaType.isValueTypeValid(equal);
126 public void changeConstraintValueTypeTo(String propertyType) throws ConstraintValueDoNotMatchPropertyTypeException {
127 ToscaType toscaType = ToscaType.getToscaType(propertyType);
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 + ">");