2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 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.onap.sdc.toscaparser.api.elements.constraints;
23 import org.onap.sdc.toscaparser.api.common.JToscaValidationIssue;
24 import org.onap.sdc.toscaparser.api.utils.ThreadLocalsHolder;
26 import java.util.Arrays;
27 import java.util.Date;
29 public class GreaterThan extends Constraint {
32 protected void setValues() {
34 setConstraintKey(GREATER_THAN);
36 // timestamps are loaded as Date objects
37 addValidTypes(Arrays.asList("Integer", "Double", "Float", "Date"));
38 //validTypes.add("datetime.date");
39 //validTypes.add("datetime.time");
40 //validTypes.add("datetime.datetime");
43 validPropTypes.add(Schema.INTEGER);
44 validPropTypes.add(Schema.FLOAT);
45 validPropTypes.add(Schema.TIMESTAMP);
46 validPropTypes.add(Schema.SCALAR_UNIT_SIZE);
47 validPropTypes.add(Schema.SCALAR_UNIT_FREQUENCY);
48 validPropTypes.add(Schema.SCALAR_UNIT_TIME);
52 public GreaterThan(String name, String type, Object c) {
55 if (!validTypes.contains(constraintValue.getClass().getSimpleName())) {
56 ThreadLocalsHolder.getCollector().appendValidationIssue(new JToscaValidationIssue("JE105", "InvalidSchemaError: The property \"greater_than\" expects comparable values"));
61 protected boolean isValid(Object value) {
64 if (value instanceof Date) {
65 if (constraintValue instanceof Date) {
66 return ((Date) value).after((Date) constraintValue);
71 Double n1 = new Double(value.toString());
72 Double n2 = new Double(constraintValue.toString());
76 protected String errMsg(Object value) {
77 return String.format("The value \"%s\" of property \"%s\" must be greater than \"%s\"",
78 valueMsg, propertyName, constraintValueMsg);
84 class GreaterThan(Constraint):
85 """Constraint class for "greater_than"
87 Constrains a property or parameter to a value greater than ('>')
91 constraint_key = Constraint.GREATER_THAN
93 valid_types = (int, float, datetime.date,
94 datetime.time, datetime.datetime)
96 valid_prop_types = (Schema.INTEGER, Schema.FLOAT, Schema.TIMESTAMP,
97 Schema.SCALAR_UNIT_SIZE, Schema.SCALAR_UNIT_FREQUENCY,
98 Schema.SCALAR_UNIT_TIME)
100 def __init__(self, property_name, property_type, constraint):
101 super(GreaterThan, self).__init__(property_name, property_type,
103 if not isinstance(constraint[self.GREATER_THAN], self.valid_types):
104 ValidationIsshueCollector.appendException(
105 InvalidSchemaError(message=_('The property "greater_than" '
106 'expects comparable values.')))
108 def _is_valid(self, value):
109 if value > self.constraint_value:
114 def _err_msg(self, value):
115 return (_('The value "%(pvalue)s" of property "%(pname)s" must be '
116 'greater than "%(cvalue)s".') %
117 dict(pname=self.property_name,
118 pvalue=self.value_msg,
119 cvalue=self.constraint_value_msg))