Initial OpenECOMP SDC commit
[sdc.git] / catalog-model / src / main / java / org / openecomp / sdc / be / model / tosca / constraints / InRangeConstraint.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 java.io.Serializable;
24 import java.util.List;
25
26 import javax.validation.constraints.NotNull;
27
28 import org.openecomp.sdc.be.model.tosca.ToscaType;
29 import org.openecomp.sdc.be.model.tosca.constraints.exception.ConstraintValueDoNotMatchPropertyTypeException;
30 import org.openecomp.sdc.be.model.tosca.constraints.exception.ConstraintViolationException;
31
32 //import com.fasterxml.jackson.annotation.JsonProperty;
33 import com.google.common.collect.Lists;
34
35 public class InRangeConstraint extends AbstractPropertyConstraint implements Serializable {
36
37         /**
38          * 
39          */
40         private static final long serialVersionUID = -8038401707152824493L;
41
42         private List<String> inRange;
43
44         private Comparable min;
45         private Comparable max;
46
47         public InRangeConstraint(List<String> inRange) {
48                 super();
49                 this.inRange = inRange;
50         }
51
52         public InRangeConstraint() {
53                 super();
54         }
55
56         @Override
57         public void initialize(ToscaType propertyType) throws ConstraintValueDoNotMatchPropertyTypeException {
58                 // Perform verification that the property type is supported for
59                 // comparison
60                 ConstraintUtil.checkComparableType(propertyType);
61                 if (inRange == null || inRange.size() != 2) {
62                         throw new ConstraintValueDoNotMatchPropertyTypeException("In range constraint must have two elements.");
63                 }
64                 String minRawText = inRange.get(0);
65                 String maxRawText = inRange.get(1);
66                 if (!propertyType.isValidValue(minRawText)) {
67                         throw new ConstraintValueDoNotMatchPropertyTypeException("Invalid min value for in range constraint ["
68                                         + minRawText + "] as it does not follow the property type [" + propertyType + "]");
69                 }
70                 if (!propertyType.isValidValue(maxRawText)) {
71                         throw new ConstraintValueDoNotMatchPropertyTypeException("Invalid max value for in range constraint ["
72                                         + maxRawText + "] as it does not follow the property type [" + propertyType + "]");
73                 }
74                 min = ConstraintUtil.convertToComparable(propertyType, minRawText);
75                 max = ConstraintUtil.convertToComparable(propertyType, maxRawText);
76         }
77
78         @Override
79         public void validate(Object propertyValue) throws ConstraintViolationException {
80                 if (propertyValue == null) {
81                         throw new ConstraintViolationException("Value to check is null");
82                 }
83                 if (!(min.getClass().isAssignableFrom(propertyValue.getClass()))) {
84                         throw new ConstraintViolationException("Value to check is not comparable to range type, value type ["
85                                         + propertyValue.getClass() + "], range type [" + min.getClass() + "]");
86                 }
87                 if (min.compareTo(propertyValue) > 0 || max.compareTo(propertyValue) < 0) {
88                         throw new ConstraintViolationException("The value [" + propertyValue + "] is out of range " + inRange);
89                 }
90         }
91
92         // @JsonProperty
93         @NotNull
94         public String getRangeMinValue() {
95                 if (inRange != null) {
96                         return inRange.get(0);
97                 } else {
98                         return null;
99                 }
100         }
101
102         // @JsonProperty
103         public void setRangeMinValue(String minValue) {
104                 if (inRange == null) {
105                         inRange = Lists.newArrayList(minValue, "");
106                 } else {
107                         inRange.set(0, minValue);
108                 }
109         }
110
111         // @JsonProperty
112         @NotNull
113         public String getRangeMaxValue() {
114                 if (inRange != null) {
115                         return inRange.get(1);
116                 } else {
117                         return null;
118                 }
119         }
120
121         // @JsonProperty
122         public void setRangeMaxValue(String maxValue) {
123                 if (inRange == null) {
124                         inRange = Lists.newArrayList("", maxValue);
125                 } else {
126                         inRange.set(1, maxValue);
127                 }
128         }
129
130 }