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.functions.Function;
25 import org.onap.sdc.toscaparser.api.utils.ThreadLocalsHolder;
27 import java.util.Arrays;
28 import java.util.Date;
30 public class GreaterOrEqual extends Constraint {
31 // Constraint class for "greater_or_equal"
33 // Constrains a property or parameter to a value greater than or equal
34 // to ('>=') the value declared.
36 protected void setValues() {
38 setConstraintKey(GREATER_OR_EQUAL);
40 // timestamps are loaded as Date objects
41 addValidTypes(Arrays.asList("Integer", "Double", "Float", "Date"));
42 //validTypes.add("datetime.date");
43 //validTypes.add("datetime.time");
44 //validTypes.add("datetime.datetime");
46 validPropTypes.add(Schema.INTEGER);
47 validPropTypes.add(Schema.FLOAT);
48 validPropTypes.add(Schema.TIMESTAMP);
49 validPropTypes.add(Schema.SCALAR_UNIT_SIZE);
50 validPropTypes.add(Schema.SCALAR_UNIT_FREQUENCY);
51 validPropTypes.add(Schema.SCALAR_UNIT_TIME);
55 public GreaterOrEqual(String name, String type, Object c) {
58 if (!validTypes.contains(constraintValue.getClass().getSimpleName())) {
59 ThreadLocalsHolder.getCollector().appendValidationIssue(new JToscaValidationIssue("JE104", "InvalidSchemaError: The property \"greater_or_equal\" expects comparable values"));
65 protected boolean isValid(Object value) {
66 if (Function.isFunction(value)) {
71 if (value instanceof Date) {
72 if (constraintValue instanceof Date) {
73 return !((Date) value).before((Date) constraintValue);
78 Double n1 = new Double(value.toString());
79 Double n2 = new Double(constraintValue.toString());
83 protected String errMsg(Object value) {
84 return String.format("The value \"%s\" of property \"%s\" must be greater or equal to \"%s\"",
85 valueMsg, propertyName, constraintValueMsg);
91 class GreaterOrEqual(Constraint):
92 """Constraint class for "greater_or_equal"
94 Constrains a property or parameter to a value greater than or equal
95 to ('>=') the value declared.
98 constraint_key = Constraint.GREATER_OR_EQUAL
100 valid_types = (int, float, datetime.date,
101 datetime.time, datetime.datetime)
103 valid_prop_types = (Schema.INTEGER, Schema.FLOAT, Schema.TIMESTAMP,
104 Schema.SCALAR_UNIT_SIZE, Schema.SCALAR_UNIT_FREQUENCY,
105 Schema.SCALAR_UNIT_TIME)
107 def __init__(self, property_name, property_type, constraint):
108 super(GreaterOrEqual, self).__init__(property_name, property_type,
110 if not isinstance(self.constraint_value, self.valid_types):
111 ThreadLocalsHolder.getCollector().appendException(
112 InvalidSchemaError(message=_('The property '
113 '"greater_or_equal" expects '
114 'comparable values.')))
116 def _is_valid(self, value):
117 if toscaparser.functions.is_function(value) or \
118 value >= self.constraint_value:
122 def _err_msg(self, value):
123 return (_('The value "%(pvalue)s" of property "%(pname)s" must be '
124 'greater than or equal to "%(cvalue)s".') %
125 dict(pname=self.property_name,
126 pvalue=self.value_msg,
127 cvalue=self.constraint_value_msg))