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 LessOrEqual extends Constraint {
30 // Constraint class for "less_or_equal"
32 // Constrains a property or parameter to a value less than or equal
33 // to ('<=') the value declared.
35 protected void setValues() {
37 setConstraintKey(LESS_OR_EQUAL);
39 // timestamps are loaded as Date objects
40 addValidTypes(Arrays.asList("Integer", "Double", "Float", "Date"));
41 //validTypes.add("datetime.date");
42 //validTypes.add("datetime.time");
43 //validTypes.add("datetime.datetime");
45 validPropTypes.add(Schema.INTEGER);
46 validPropTypes.add(Schema.FLOAT);
47 validPropTypes.add(Schema.TIMESTAMP);
48 validPropTypes.add(Schema.SCALAR_UNIT_SIZE);
49 validPropTypes.add(Schema.SCALAR_UNIT_FREQUENCY);
50 validPropTypes.add(Schema.SCALAR_UNIT_TIME);
54 public LessOrEqual(String name, String type, Object c) {
57 if (!validTypes.contains(constraintValue.getClass().getSimpleName())) {
58 ThreadLocalsHolder.getCollector().appendValidationIssue(new JToscaValidationIssue("JE110", "InvalidSchemaError: The property \"less_or_equal\" expects comparable values"));
63 protected boolean isValid(Object value) {
66 if (value instanceof Date) {
67 if (constraintValue instanceof Date) {
68 return !((Date) value).after((Date) constraintValue);
73 Double n1 = new Double(value.toString());
74 Double n2 = new Double(constraintValue.toString());
79 protected String errMsg(Object value) {
80 return String.format("The value \"%s\" of property \"%s\" must be less or equal to \"%s\"",
81 valueMsg, propertyName, constraintValueMsg);
88 class LessOrEqual(Constraint):
89 """Constraint class for "less_or_equal"
91 Constrains a property or parameter to a value less than or equal
92 to ('<=') the value declared.
95 constraint_key = Constraint.LESS_OR_EQUAL
97 valid_types = (int, float, datetime.date,
98 datetime.time, datetime.datetime)
100 valid_prop_types = (Schema.INTEGER, Schema.FLOAT, Schema.TIMESTAMP,
101 Schema.SCALAR_UNIT_SIZE, Schema.SCALAR_UNIT_FREQUENCY,
102 Schema.SCALAR_UNIT_TIME)
104 def __init__(self, property_name, property_type, constraint):
105 super(LessOrEqual, self).__init__(property_name, property_type,
107 if not isinstance(self.constraint_value, self.valid_types):
108 ValidationIsshueCollector.appendException(
109 InvalidSchemaError(message=_('The property "less_or_equal" '
110 'expects comparable values.')))
112 def _is_valid(self, value):
113 if value <= self.constraint_value:
118 def _err_msg(self, value):
119 return (_('The value "%(pvalue)s" of property "%(pname)s" must be '
120 'less than or equal to "%(cvalue)s".') %
121 dict(pname=self.property_name,
122 pvalue=self.value_msg,
123 cvalue=self.constraint_value_msg))