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 LessThan extends Constraint {
32 protected void setValues() {
34 setConstraintKey(LESS_THAN);
35 // timestamps are loaded as Date objects
36 addValidTypes(Arrays.asList("Integer", "Double", "Float", "Date"));
37 //validTypes.add("datetime.date");
38 //validTypes.add("datetime.time");
39 //validTypes.add("datetime.datetime");
42 validPropTypes.add(Schema.INTEGER);
43 validPropTypes.add(Schema.FLOAT);
44 validPropTypes.add(Schema.TIMESTAMP);
45 validPropTypes.add(Schema.SCALAR_UNIT_SIZE);
46 validPropTypes.add(Schema.SCALAR_UNIT_FREQUENCY);
47 validPropTypes.add(Schema.SCALAR_UNIT_TIME);
51 public LessThan(String name, String type, Object c) {
54 if (!validTypes.contains(constraintValue.getClass().getSimpleName())) {
55 ThreadLocalsHolder.getCollector().appendValidationIssue(new JToscaValidationIssue("JE111", "InvalidSchemaError: The property \"less_than\" expects comparable values"));
60 protected boolean isValid(Object value) {
63 if (value instanceof Date) {
64 if (constraintValue instanceof Date) {
65 return ((Date) value).before((Date) constraintValue);
70 Double n1 = new Double(value.toString());
71 Double n2 = new Double(constraintValue.toString());
76 protected String errMsg(Object value) {
77 return String.format("The value \"%s\" of property \"%s\" must be less than \"%s\"",
78 valueMsg, propertyName, constraintValueMsg);
85 class LessThan(Constraint):
86 """Constraint class for "less_than"
88 Constrains a property or parameter to a value less than ('<')
92 constraint_key = Constraint.LESS_THAN
94 valid_types = (int, float, datetime.date,
95 datetime.time, datetime.datetime)
97 valid_prop_types = (Schema.INTEGER, Schema.FLOAT, Schema.TIMESTAMP,
98 Schema.SCALAR_UNIT_SIZE, Schema.SCALAR_UNIT_FREQUENCY,
99 Schema.SCALAR_UNIT_TIME)
101 def __init__(self, property_name, property_type, constraint):
102 super(LessThan, self).__init__(property_name, property_type,
104 if not isinstance(self.constraint_value, self.valid_types):
105 ValidationIsshueCollector.appendException(
106 InvalidSchemaError(message=_('The property "less_than" '
107 'expects comparable values.')))
109 def _is_valid(self, value):
110 if value < self.constraint_value:
115 def _err_msg(self, value):
116 return (_('The value "%(pvalue)s" of property "%(pname)s" must be '
117 'less than "%(cvalue)s".') %
118 dict(pname=self.property_name,
119 pvalue=self.value_msg,
120 cvalue=self.constraint_value_msg))