aboutsummaryrefslogtreecommitdiffstats
path: root/jtosca/src/main/java/org/openecomp/sdc/toscaparser/api/elements/constraints
diff options
context:
space:
mode:
Diffstat (limited to 'jtosca/src/main/java/org/openecomp/sdc/toscaparser/api/elements/constraints')
-rw-r--r--jtosca/src/main/java/org/openecomp/sdc/toscaparser/api/elements/constraints/Constraint.java236
-rw-r--r--jtosca/src/main/java/org/openecomp/sdc/toscaparser/api/elements/constraints/Equal.java61
-rw-r--r--jtosca/src/main/java/org/openecomp/sdc/toscaparser/api/elements/constraints/GreaterOrEqual.java112
-rw-r--r--jtosca/src/main/java/org/openecomp/sdc/toscaparser/api/elements/constraints/GreaterThan.java101
-rw-r--r--jtosca/src/main/java/org/openecomp/sdc/toscaparser/api/elements/constraints/InRange.java170
-rw-r--r--jtosca/src/main/java/org/openecomp/sdc/toscaparser/api/elements/constraints/Length.java78
-rw-r--r--jtosca/src/main/java/org/openecomp/sdc/toscaparser/api/elements/constraints/LessOrEqual.java105
-rw-r--r--jtosca/src/main/java/org/openecomp/sdc/toscaparser/api/elements/constraints/LessThan.java103
-rw-r--r--jtosca/src/main/java/org/openecomp/sdc/toscaparser/api/elements/constraints/MaxLength.java89
-rw-r--r--jtosca/src/main/java/org/openecomp/sdc/toscaparser/api/elements/constraints/MinLength.java89
-rw-r--r--jtosca/src/main/java/org/openecomp/sdc/toscaparser/api/elements/constraints/Pattern.java95
-rw-r--r--jtosca/src/main/java/org/openecomp/sdc/toscaparser/api/elements/constraints/Schema.java276
-rw-r--r--jtosca/src/main/java/org/openecomp/sdc/toscaparser/api/elements/constraints/ValidValues.java84
13 files changed, 1599 insertions, 0 deletions
diff --git a/jtosca/src/main/java/org/openecomp/sdc/toscaparser/api/elements/constraints/Constraint.java b/jtosca/src/main/java/org/openecomp/sdc/toscaparser/api/elements/constraints/Constraint.java
new file mode 100644
index 0000000..d82b76c
--- /dev/null
+++ b/jtosca/src/main/java/org/openecomp/sdc/toscaparser/api/elements/constraints/Constraint.java
@@ -0,0 +1,236 @@
+package org.openecomp.sdc.toscaparser.api.elements.constraints;
+
+import java.util.ArrayList;
+import java.util.LinkedHashMap;
+
+import org.openecomp.sdc.toscaparser.api.common.ExceptionCollector;
+import org.openecomp.sdc.toscaparser.api.elements.ScalarUnit;
+
+public abstract class Constraint {
+
+ // Parent class for constraints for a Property or Input
+
+ protected static final String EQUAL = "equal";
+ protected static final String GREATER_THAN = "greater_than";
+ protected static final String GREATER_OR_EQUAL = "greater_or_equal";
+ protected static final String LESS_THAN = "less_than";
+ protected static final String LESS_OR_EQUAL = "less_or_equal";
+ protected static final String IN_RANGE = "in_range";
+ protected static final String VALID_VALUES = "valid_values";
+ protected static final String LENGTH = "length";
+ protected static final String MIN_LENGTH = "min_length";
+ protected static final String MAX_LENGTH = "max_length";
+ protected static final String PATTERN = "pattern";
+
+ protected static final String CONSTRAINTS[] = {
+ EQUAL, GREATER_THAN,GREATER_OR_EQUAL, LESS_THAN, LESS_OR_EQUAL,
+ IN_RANGE, VALID_VALUES, LENGTH, MIN_LENGTH, MAX_LENGTH, PATTERN};
+
+ @SuppressWarnings("unchecked")
+ public static Constraint factory(String constraintClass,String propname,String proptype,Object constraint) {
+
+ // a factory for the different Constraint classes
+ // replaces Python's __new__() usage
+
+ if(!(constraint instanceof LinkedHashMap) ||
+ ((LinkedHashMap<String,Object>)constraint).size() != 1) {
+ ExceptionCollector.appendException(
+ "InvalidSchemaError: Invalid constraint schema " + constraint.toString());
+ }
+
+ if(constraintClass.equals(EQUAL)) {
+ return new Equal(propname,proptype,constraint);
+ }
+ else if(constraintClass.equals(GREATER_THAN)) {
+ return new GreaterThan(propname,proptype,constraint);
+ }
+ else if(constraintClass.equals(GREATER_OR_EQUAL)) {
+ return new GreaterOrEqual(propname,proptype,constraint);
+ }
+ else if(constraintClass.equals(LESS_THAN)) {
+ return new LessThan(propname,proptype,constraint);
+ }
+ else if(constraintClass.equals(LESS_OR_EQUAL)) {
+ return new LessOrEqual(propname,proptype,constraint);
+ }
+ else if(constraintClass.equals(IN_RANGE)) {
+ return new InRange(propname,proptype,constraint);
+ }
+ else if(constraintClass.equals(VALID_VALUES)) {
+ return new ValidValues(propname,proptype,constraint);
+ }
+ else if(constraintClass.equals(LENGTH)) {
+ return new Length(propname,proptype,constraint);
+ }
+ else if(constraintClass.equals(MIN_LENGTH)) {
+ return new MinLength(propname,proptype,constraint);
+ }
+ else if(constraintClass.equals(MAX_LENGTH)) {
+ return new MaxLength(propname,proptype,constraint);
+ }
+ else if(constraintClass.equals(PATTERN)) {
+ return new Pattern(propname,proptype,constraint);
+ }
+ else {
+ ExceptionCollector.appendException(String.format(
+ "InvalidSchemaError: Invalid property \"%s\"",constraintClass));
+ return null;
+ }
+ }
+
+ protected String constraintKey = "TBD";
+ protected ArrayList<String> validTypes = new ArrayList<>();
+ protected ArrayList<String> validPropTypes = new ArrayList<>();
+
+ protected String propertyName;
+ protected String propertyType;
+ protected Object constraintValue;
+ protected Object constraintValueMsg;
+ protected Object valueMsg;
+
+ @SuppressWarnings("unchecked")
+ public Constraint(String propname,String proptype,Object constraint) {
+
+ _setValues();
+
+ propertyName = propname;
+ propertyType = proptype;
+ constraintValue = ((LinkedHashMap<String,Object>)constraint).get(constraintKey);
+ constraintValueMsg = constraintValue;
+ boolean bFound = false;
+ for(String s: ScalarUnit.SCALAR_UNIT_TYPES) {
+ if(s.equals(propertyType)) {
+ bFound = true;
+ break;
+ }
+ }
+ if(bFound) {
+ constraintValue = _getScalarUnitConstraintValue();
+ }
+ // check if constraint is valid for property type
+ bFound = false;
+ for(String s: validPropTypes) {
+ if(s.equals(propertyType)) {
+ bFound = true;
+ break;
+ }
+ }
+ if(!bFound) {
+ ExceptionCollector.appendException(String.format(
+ "InvalidSchemaError: Property \"%s\" is not valid for data type \"%s\"",
+ constraintKey,propertyType));
+ }
+ }
+
+ @SuppressWarnings("unchecked")
+ private Object _getScalarUnitConstraintValue() {
+ // code differs from Python because of class creation
+ if(constraintValue instanceof ArrayList) {
+ ArrayList<Object> ret = new ArrayList<>();
+ for(Object v: (ArrayList<Object>)constraintValue) {
+ ScalarUnit su = ScalarUnit.getScalarunitClass(propertyType,v);
+ ret.add(su.getNumFromScalarUnit(null));
+ }
+ return ret;
+ }
+ else {
+ ScalarUnit su = ScalarUnit.getScalarunitClass(propertyType,constraintValue);
+ return su.getNumFromScalarUnit(null);
+ }
+ }
+
+ public void validate(Object value) {
+ valueMsg = value;
+ boolean bFound = false;
+ for(String s: ScalarUnit.SCALAR_UNIT_TYPES) {
+ if(s.equals(propertyType)) {
+ bFound = true;
+ break;
+ }
+ }
+ if(bFound) {
+ value = ScalarUnit.getScalarunitValue(propertyType,value,null);
+ }
+ if(!_isValid(value)) {
+ ExceptionCollector.appendException("ValidationError: " + _errMsg(value));
+ }
+ }
+
+ protected abstract boolean _isValid(Object value);
+
+ protected abstract void _setValues();
+
+ protected abstract String _errMsg(Object value);
+
+}
+
+/*python
+
+class Constraint(object):
+ '''Parent class for constraints for a Property or Input.'''
+
+ CONSTRAINTS = (EQUAL, GREATER_THAN,
+ GREATER_OR_EQUAL, LESS_THAN, LESS_OR_EQUAL, IN_RANGE,
+ VALID_VALUES, LENGTH, MIN_LENGTH, MAX_LENGTH, PATTERN) = \
+ ('equal', 'greater_than', 'greater_or_equal', 'less_than',
+ 'less_or_equal', 'in_range', 'valid_values', 'length',
+ 'min_length', 'max_length', 'pattern')
+
+ def __new__(cls, property_name, property_type, constraint):
+ if cls is not Constraint:
+ return super(Constraint, cls).__new__(cls)
+
+ if(not isinstance(constraint, collections.Mapping) or
+ len(constraint) != 1):
+ ExceptionCollector.appendException(
+ InvalidSchemaError(message=_('Invalid constraint schema.')))
+
+ for type in constraint.keys():
+ ConstraintClass = get_constraint_class(type)
+ if not ConstraintClass:
+ msg = _('Invalid property "%s".') % type
+ ExceptionCollector.appendException(
+ InvalidSchemaError(message=msg))
+
+ return ConstraintClass(property_name, property_type, constraint)
+
+ def __init__(self, property_name, property_type, constraint):
+ self.property_name = property_name
+ self.property_type = property_type
+ self.constraint_value = constraint[self.constraint_key]
+ self.constraint_value_msg = self.constraint_value
+ if self.property_type in scalarunit.ScalarUnit.SCALAR_UNIT_TYPES:
+ self.constraint_value = self._get_scalarunit_constraint_value()
+ # check if constraint is valid for property type
+ if property_type not in self.valid_prop_types:
+ msg = _('Property "%(ctype)s" is not valid for data type '
+ '"%(dtype)s".') % dict(
+ ctype=self.constraint_key,
+ dtype=property_type)
+ ExceptionCollector.appendException(InvalidSchemaError(message=msg))
+
+ def _get_scalarunit_constraint_value(self):
+ if self.property_type in scalarunit.ScalarUnit.SCALAR_UNIT_TYPES:
+ ScalarUnit_Class = (scalarunit.
+ get_scalarunit_class(self.property_type))
+ if isinstance(self.constraint_value, list):
+ return [ScalarUnit_Class(v).get_num_from_scalar_unit()
+ for v in self.constraint_value]
+ else:
+ return (ScalarUnit_Class(self.constraint_value).
+ get_num_from_scalar_unit())
+
+ def _err_msg(self, value):
+ return _('Property "%s" could not be validated.') % self.property_name
+
+ def validate(self, value):
+ self.value_msg = value
+ if self.property_type in scalarunit.ScalarUnit.SCALAR_UNIT_TYPES:
+ value = scalarunit.get_scalarunit_value(self.property_type, value)
+ if not self._is_valid(value):
+ err_msg = self._err_msg(value)
+ ExceptionCollector.appendException(
+ ValidationError(message=err_msg))
+
+
+*/
diff --git a/jtosca/src/main/java/org/openecomp/sdc/toscaparser/api/elements/constraints/Equal.java b/jtosca/src/main/java/org/openecomp/sdc/toscaparser/api/elements/constraints/Equal.java
new file mode 100644
index 0000000..e16cac3
--- /dev/null
+++ b/jtosca/src/main/java/org/openecomp/sdc/toscaparser/api/elements/constraints/Equal.java
@@ -0,0 +1,61 @@
+package org.openecomp.sdc.toscaparser.api.elements.constraints;
+
+public class Equal extends Constraint {
+
+ protected void _setValues() {
+
+ constraintKey = EQUAL;
+
+ for(String s: Schema.PROPERTY_TYPES) {
+ validPropTypes.add(s);
+ }
+
+ }
+
+ public Equal(String name,String type,Object c) {
+ super(name,type,c);
+
+ }
+
+ protected boolean _isValid(Object val) {
+ // equality of objects is tricky so we're comparing
+ // the toString() representation
+ if(val.toString().equals(constraintValue.toString())) {
+ return true;
+ }
+ return false;
+ }
+
+ protected String _errMsg(Object value) {
+ return String.format("The value \"%s\" of property \"%s\" is not equal to \"%s\"",
+ valueMsg,propertyName,constraintValueMsg);
+ }
+
+}
+
+/*python
+
+class Equal(Constraint):
+"""Constraint class for "equal"
+
+Constrains a property or parameter to a value equal to ('=')
+the value declared.
+"""
+
+constraint_key = Constraint.EQUAL
+
+valid_prop_types = Schema.PROPERTY_TYPES
+
+def _is_valid(self, value):
+ if value == self.constraint_value:
+ return True
+
+ return False
+
+def _err_msg(self, value):
+ return (_('The value "%(pvalue)s" of property "%(pname)s" is not '
+ 'equal to "%(cvalue)s".') %
+ dict(pname=self.property_name,
+ pvalue=self.value_msg,
+ cvalue=self.constraint_value_msg))
+*/ \ No newline at end of file
diff --git a/jtosca/src/main/java/org/openecomp/sdc/toscaparser/api/elements/constraints/GreaterOrEqual.java b/jtosca/src/main/java/org/openecomp/sdc/toscaparser/api/elements/constraints/GreaterOrEqual.java
new file mode 100644
index 0000000..168fd92
--- /dev/null
+++ b/jtosca/src/main/java/org/openecomp/sdc/toscaparser/api/elements/constraints/GreaterOrEqual.java
@@ -0,0 +1,112 @@
+package org.openecomp.sdc.toscaparser.api.elements.constraints;
+
+import java.util.Date;
+
+import org.openecomp.sdc.toscaparser.api.common.ExceptionCollector;
+import org.openecomp.sdc.toscaparser.api.functions.Function;
+
+public class GreaterOrEqual extends Constraint {
+ // Constraint class for "greater_or_equal"
+
+ // Constrains a property or parameter to a value greater than or equal
+ // to ('>=') the value declared.
+
+ protected void _setValues() {
+
+ constraintKey = GREATER_OR_EQUAL;
+
+ validTypes.add("Integer");
+ validTypes.add("Double");
+ validTypes.add("Float");
+ // timestamps are loaded as Date objects
+ validTypes.add("Date");
+ //validTypes.add("datetime.date");
+ //validTypes.add("datetime.time");
+ //validTypes.add("datetime.datetime");
+
+ validPropTypes.add(Schema.INTEGER);
+ validPropTypes.add(Schema.FLOAT);
+ validPropTypes.add(Schema.TIMESTAMP);
+ validPropTypes.add(Schema.SCALAR_UNIT_SIZE);
+ validPropTypes.add(Schema.SCALAR_UNIT_FREQUENCY);
+ validPropTypes.add(Schema.SCALAR_UNIT_TIME);
+
+ }
+
+ public GreaterOrEqual(String name,String type,Object c) {
+ super(name,type,c);
+
+ if(!validTypes.contains(constraintValue.getClass().getSimpleName())) {
+ ExceptionCollector.appendException("InvalidSchemaError: The property \"greater_or_equal\" expects comparable values");
+ }
+ }
+
+
+
+ @Override
+ protected boolean _isValid(Object value) {
+ if(Function.isFunction(value)) {
+ return true;
+ }
+
+ // timestamps
+ if(value instanceof Date) {
+ if(constraintValue instanceof Date) {
+ return !((Date)value).before((Date)constraintValue);
+ }
+ return false;
+ }
+ // all others
+ Double n1 = new Double(value.toString());
+ Double n2 = new Double(constraintValue.toString());
+ return n1 >= n2;
+ }
+
+ protected String _errMsg(Object value) {
+ return String.format("The value \"%s\" of property \"%s\" must be greater or equal to \"%s\"",
+ valueMsg,propertyName,constraintValueMsg);
+ }
+}
+
+/*python
+
+class GreaterOrEqual(Constraint):
+"""Constraint class for "greater_or_equal"
+
+Constrains a property or parameter to a value greater than or equal
+to ('>=') the value declared.
+"""
+
+constraint_key = Constraint.GREATER_OR_EQUAL
+
+valid_types = (int, float, datetime.date,
+ datetime.time, datetime.datetime)
+
+valid_prop_types = (Schema.INTEGER, Schema.FLOAT, Schema.TIMESTAMP,
+ Schema.SCALAR_UNIT_SIZE, Schema.SCALAR_UNIT_FREQUENCY,
+ Schema.SCALAR_UNIT_TIME)
+
+def __init__(self, property_name, property_type, constraint):
+ super(GreaterOrEqual, self).__init__(property_name, property_type,
+ constraint)
+ if not isinstance(self.constraint_value, self.valid_types):
+ ExceptionCollector.appendException(
+ InvalidSchemaError(message=_('The property '
+ '"greater_or_equal" expects '
+ 'comparable values.')))
+
+def _is_valid(self, value):
+ if toscaparser.functions.is_function(value) or \
+ value >= self.constraint_value:
+ return True
+ return False
+
+def _err_msg(self, value):
+ return (_('The value "%(pvalue)s" of property "%(pname)s" must be '
+ 'greater than or equal to "%(cvalue)s".') %
+ dict(pname=self.property_name,
+ pvalue=self.value_msg,
+ cvalue=self.constraint_value_msg))
+
+
+*/ \ No newline at end of file
diff --git a/jtosca/src/main/java/org/openecomp/sdc/toscaparser/api/elements/constraints/GreaterThan.java b/jtosca/src/main/java/org/openecomp/sdc/toscaparser/api/elements/constraints/GreaterThan.java
new file mode 100644
index 0000000..2803bb4
--- /dev/null
+++ b/jtosca/src/main/java/org/openecomp/sdc/toscaparser/api/elements/constraints/GreaterThan.java
@@ -0,0 +1,101 @@
+package org.openecomp.sdc.toscaparser.api.elements.constraints;
+
+import java.util.Date;
+
+import org.openecomp.sdc.toscaparser.api.common.ExceptionCollector;
+
+public class GreaterThan extends Constraint {
+
+ @Override
+ protected void _setValues() {
+
+ constraintKey = GREATER_THAN;
+
+ validTypes.add("Integer");
+ validTypes.add("Double");
+ validTypes.add("Float");
+ // timestamps are loaded as Date objects
+ validTypes.add("Date");
+ //validTypes.add("datetime.date");
+ //validTypes.add("datetime.time");
+ //validTypes.add("datetime.datetime");
+
+
+ validPropTypes.add(Schema.INTEGER);
+ validPropTypes.add(Schema.FLOAT);
+ validPropTypes.add(Schema.TIMESTAMP);
+ validPropTypes.add(Schema.SCALAR_UNIT_SIZE);
+ validPropTypes.add(Schema.SCALAR_UNIT_FREQUENCY);
+ validPropTypes.add(Schema.SCALAR_UNIT_TIME);
+
+ }
+
+ public GreaterThan(String name,String type,Object c) {
+ super(name,type,c);
+
+ if(!validTypes.contains(constraintValue.getClass().getSimpleName())) {
+ ExceptionCollector.appendException("InvalidSchemaError: The property \"greater_than\" expects comparable values");
+ }
+ }
+
+ @Override
+ protected boolean _isValid(Object value) {
+
+ // timestamps
+ if(value instanceof Date) {
+ if(constraintValue instanceof Date) {
+ return ((Date)value).after((Date)constraintValue);
+ }
+ return false;
+ }
+
+ Double n1 = new Double(value.toString());
+ Double n2 = new Double(constraintValue.toString());
+ return n1 > n2;
+ }
+
+ protected String _errMsg(Object value) {
+ return String.format("The value \"%s\" of property \"%s\" must be greater than \"%s\"",
+ valueMsg,propertyName,constraintValueMsg);
+ }
+
+}
+
+/*
+class GreaterThan(Constraint):
+ """Constraint class for "greater_than"
+
+ Constrains a property or parameter to a value greater than ('>')
+ the value declared.
+ """
+
+ constraint_key = Constraint.GREATER_THAN
+
+ valid_types = (int, float, datetime.date,
+ datetime.time, datetime.datetime)
+
+ valid_prop_types = (Schema.INTEGER, Schema.FLOAT, Schema.TIMESTAMP,
+ Schema.SCALAR_UNIT_SIZE, Schema.SCALAR_UNIT_FREQUENCY,
+ Schema.SCALAR_UNIT_TIME)
+
+ def __init__(self, property_name, property_type, constraint):
+ super(GreaterThan, self).__init__(property_name, property_type,
+ constraint)
+ if not isinstance(constraint[self.GREATER_THAN], self.valid_types):
+ ExceptionCollector.appendException(
+ InvalidSchemaError(message=_('The property "greater_than" '
+ 'expects comparable values.')))
+
+ def _is_valid(self, value):
+ if value > self.constraint_value:
+ return True
+
+ return False
+
+ def _err_msg(self, value):
+ return (_('The value "%(pvalue)s" of property "%(pname)s" must be '
+ 'greater than "%(cvalue)s".') %
+ dict(pname=self.property_name,
+ pvalue=self.value_msg,
+ cvalue=self.constraint_value_msg))
+*/
diff --git a/jtosca/src/main/java/org/openecomp/sdc/toscaparser/api/elements/constraints/InRange.java b/jtosca/src/main/java/org/openecomp/sdc/toscaparser/api/elements/constraints/InRange.java
new file mode 100644
index 0000000..6a5432c
--- /dev/null
+++ b/jtosca/src/main/java/org/openecomp/sdc/toscaparser/api/elements/constraints/InRange.java
@@ -0,0 +1,170 @@
+package org.openecomp.sdc.toscaparser.api.elements.constraints;
+
+import java.util.Date;
+
+import org.openecomp.sdc.toscaparser.api.common.ExceptionCollector;
+
+import java.util.ArrayList;
+
+public class InRange extends Constraint {
+ // Constraint class for "in_range"
+
+ //Constrains a property or parameter to a value in range of (inclusive)
+ //the two values declared.
+
+ private static final String UNBOUNDED = "UNBOUNDED";
+
+ private Object min,max;
+
+ protected void _setValues() {
+
+ constraintKey = IN_RANGE;
+
+ validTypes.add("Integer");
+ validTypes.add("Double");
+ validTypes.add("Float");
+ validTypes.add("String");
+ // timestamps are loaded as Date objects
+ validTypes.add("Date");
+ //validTypes.add("datetime.date");
+ //validTypes.add("datetime.time");
+ //validTypes.add("datetime.datetime");
+
+ validPropTypes.add(Schema.INTEGER);
+ validPropTypes.add(Schema.FLOAT);
+ validPropTypes.add(Schema.TIMESTAMP);
+ validPropTypes.add(Schema.SCALAR_UNIT_SIZE);
+ validPropTypes.add(Schema.SCALAR_UNIT_FREQUENCY);
+ validPropTypes.add(Schema.SCALAR_UNIT_TIME);
+ validPropTypes.add(Schema.RANGE);
+
+ }
+
+ @SuppressWarnings("unchecked")
+ public InRange(String name,String type,Object c) {
+ super(name,type,c);
+
+ if(!(constraintValue instanceof ArrayList) || ((ArrayList<Object>)constraintValue).size() != 2) {
+ ExceptionCollector.appendException("InvalidSchemaError: The property \"in_range\" expects a list");
+
+ }
+
+ ArrayList<Object> alcv = (ArrayList<Object>)constraintValue;
+ String msg = "The property \"in_range\" expects comparable values";
+ for(Object vo: alcv) {
+ if(!validTypes.contains(vo.getClass().getSimpleName())) {
+ ExceptionCollector.appendException("InvalidSchemaError: " + msg);
+ }
+ // The only string we allow for range is the special value 'UNBOUNDED'
+ if((vo instanceof String) && !((String)vo).equals(UNBOUNDED)) {
+ ExceptionCollector.appendException("InvalidSchemaError: " + msg);
+ }
+ }
+ min = alcv.get(0);
+ max = alcv.get(1);
+
+ }
+
+ @Override
+ protected boolean _isValid(Object value) {
+
+ // timestamps
+ if(value instanceof Date) {
+ if(min instanceof Date && max instanceof Date) {
+ return !((Date)value).before((Date)min) &&
+ !((Date)value).after((Date)max);
+ }
+ return false;
+ }
+
+ Double dvalue = new Double(value.toString());
+ if(!(min instanceof String)) {
+ if(dvalue < new Double(min.toString())) {
+ return false;
+ }
+ }
+ else if(!((String)min).equals(UNBOUNDED)) {
+ return false;
+ }
+ if(!(max instanceof String)) {
+ if(dvalue > new Double(max.toString())) {
+ return false;
+ }
+ }
+ else if(!((String)max).equals(UNBOUNDED)) {
+ return false;
+ }
+ return true;
+ }
+
+ @Override
+ protected String _errMsg(Object value) {
+ return String.format("The value \"%s\" of property \"%s\" is out of range \"(min:%s, max:%s)\"",
+ valueMsg,propertyName,min.toString(),max.toString());
+ }
+
+}
+
+/*python
+
+class InRange(Constraint):
+ """Constraint class for "in_range"
+
+ Constrains a property or parameter to a value in range of (inclusive)
+ the two values declared.
+ """
+ UNBOUNDED = 'UNBOUNDED'
+
+ constraint_key = Constraint.IN_RANGE
+
+ valid_types = (int, float, datetime.date,
+ datetime.time, datetime.datetime, str)
+
+ valid_prop_types = (Schema.INTEGER, Schema.FLOAT, Schema.TIMESTAMP,
+ Schema.SCALAR_UNIT_SIZE, Schema.SCALAR_UNIT_FREQUENCY,
+ Schema.SCALAR_UNIT_TIME, Schema.RANGE)
+
+ def __init__(self, property_name, property_type, constraint):
+ super(InRange, self).__init__(property_name, property_type, constraint)
+ if(not isinstance(self.constraint_value, collections.Sequence) or
+ (len(constraint[self.IN_RANGE]) != 2)):
+ ExceptionCollector.appendException(
+ InvalidSchemaError(message=_('The property "in_range" '
+ 'expects a list.')))
+
+ msg = _('The property "in_range" expects comparable values.')
+ for value in self.constraint_value:
+ if not isinstance(value, self.valid_types):
+ ExceptionCollector.appendException(
+ InvalidSchemaError(message=msg))
+ # The only string we allow for range is the special value
+ # 'UNBOUNDED'
+ if(isinstance(value, str) and value != self.UNBOUNDED):
+ ExceptionCollector.appendException(
+ InvalidSchemaError(message=msg))
+
+ self.min = self.constraint_value[0]
+ self.max = self.constraint_value[1]
+
+ def _is_valid(self, value):
+ if not isinstance(self.min, str):
+ if value < self.min:
+ return False
+ elif self.min != self.UNBOUNDED:
+ return False
+ if not isinstance(self.max, str):
+ if value > self.max:
+ return False
+ elif self.max != self.UNBOUNDED:
+ return False
+ return True
+
+ def _err_msg(self, value):
+ return (_('The value "%(pvalue)s" of property "%(pname)s" is out of '
+ 'range "(min:%(vmin)s, max:%(vmax)s)".') %
+ dict(pname=self.property_name,
+ pvalue=self.value_msg,
+ vmin=self.constraint_value_msg[0],
+ vmax=self.constraint_value_msg[1]))
+
+*/
diff --git a/jtosca/src/main/java/org/openecomp/sdc/toscaparser/api/elements/constraints/Length.java b/jtosca/src/main/java/org/openecomp/sdc/toscaparser/api/elements/constraints/Length.java
new file mode 100644
index 0000000..939cb2a
--- /dev/null
+++ b/jtosca/src/main/java/org/openecomp/sdc/toscaparser/api/elements/constraints/Length.java
@@ -0,0 +1,78 @@
+package org.openecomp.sdc.toscaparser.api.elements.constraints;
+
+import org.openecomp.sdc.toscaparser.api.common.ExceptionCollector;
+
+public class Length extends Constraint {
+ // Constraint class for "length"
+
+ // Constrains the property or parameter to a value of a given length.
+
+ @Override
+ protected void _setValues() {
+
+ constraintKey = LENGTH;
+
+ validTypes.add("Integer");
+
+ validPropTypes.add(Schema.STRING);
+
+ }
+
+ public Length(String name,String type,Object c) {
+ super(name,type,c);
+
+ if(!validTypes.contains(constraintValue.getClass().getSimpleName())) {
+ ExceptionCollector.appendException("InvalidSchemaError: The property \"length\" expects an integer");
+ }
+ }
+
+ @Override
+ protected boolean _isValid(Object value) {
+ if(value instanceof String && constraintValue instanceof Integer &&
+ ((String)value).length() == (Integer)constraintValue) {
+ return true;
+ }
+ return false;
+ }
+
+ @Override
+ protected String _errMsg(Object value) {
+ return String.format("Length of value \"%s\" of property \"%s\" must be equal to \"%s\"",
+ value.toString(),propertyName,constraintValue.toString());
+ }
+
+}
+
+/*python
+ class Length(Constraint):
+ """Constraint class for "length"
+
+ Constrains the property or parameter to a value of a given length.
+ """
+
+ constraint_key = Constraint.LENGTH
+
+ valid_types = (int, )
+
+ valid_prop_types = (Schema.STRING, )
+
+ def __init__(self, property_name, property_type, constraint):
+ super(Length, self).__init__(property_name, property_type, constraint)
+ if not isinstance(self.constraint_value, self.valid_types):
+ ExceptionCollector.appendException(
+ InvalidSchemaError(message=_('The property "length" expects '
+ 'an integer.')))
+
+ def _is_valid(self, value):
+ if isinstance(value, str) and len(value) == self.constraint_value:
+ return True
+
+ return False
+
+ def _err_msg(self, value):
+ return (_('Length of value "%(pvalue)s" of property "%(pname)s" '
+ 'must be equal to "%(cvalue)s".') %
+ dict(pname=self.property_name,
+ pvalue=value,
+ cvalue=self.constraint_value))
+*/ \ No newline at end of file
diff --git a/jtosca/src/main/java/org/openecomp/sdc/toscaparser/api/elements/constraints/LessOrEqual.java b/jtosca/src/main/java/org/openecomp/sdc/toscaparser/api/elements/constraints/LessOrEqual.java
new file mode 100644
index 0000000..f7778b6
--- /dev/null
+++ b/jtosca/src/main/java/org/openecomp/sdc/toscaparser/api/elements/constraints/LessOrEqual.java
@@ -0,0 +1,105 @@
+package org.openecomp.sdc.toscaparser.api.elements.constraints;
+
+import java.util.Date;
+
+import org.openecomp.sdc.toscaparser.api.common.ExceptionCollector;
+
+public class LessOrEqual extends Constraint {
+ // Constraint class for "less_or_equal"
+
+ // Constrains a property or parameter to a value less than or equal
+ // to ('<=') the value declared.
+
+ protected void _setValues() {
+
+ constraintKey = LESS_OR_EQUAL;
+
+ validTypes.add("Integer");
+ validTypes.add("Double");
+ validTypes.add("Float");
+ // timestamps are loaded as Date objects
+ validTypes.add("Date");
+ //validTypes.add("datetime.date");
+ //validTypes.add("datetime.time");
+ //validTypes.add("datetime.datetime");
+
+ validPropTypes.add(Schema.INTEGER);
+ validPropTypes.add(Schema.FLOAT);
+ validPropTypes.add(Schema.TIMESTAMP);
+ validPropTypes.add(Schema.SCALAR_UNIT_SIZE);
+ validPropTypes.add(Schema.SCALAR_UNIT_FREQUENCY);
+ validPropTypes.add(Schema.SCALAR_UNIT_TIME);
+
+ }
+
+ public LessOrEqual(String name,String type,Object c) {
+ super(name,type,c);
+
+ if(!validTypes.contains(constraintValue.getClass().getSimpleName())) {
+ ExceptionCollector.appendException("InvalidSchemaError: The property \"less_or_equal\" expects comparable values");
+ }
+ }
+
+ @Override
+ protected boolean _isValid(Object value) {
+
+ // timestamps
+ if(value instanceof Date) {
+ if(constraintValue instanceof Date) {
+ return !((Date)value).after((Date)constraintValue);
+ }
+ return false;
+ }
+
+ Double n1 = new Double(value.toString());
+ Double n2 = new Double(constraintValue.toString());
+ return n1 <= n2;
+ }
+
+ @Override
+ protected String _errMsg(Object value) {
+ return String.format("The value \"%s\" of property \"%s\" must be less or equal to \"%s\"",
+ valueMsg,propertyName,constraintValueMsg);
+ }
+
+}
+
+/*python
+
+class LessOrEqual(Constraint):
+ """Constraint class for "less_or_equal"
+
+ Constrains a property or parameter to a value less than or equal
+ to ('<=') the value declared.
+ """
+
+ constraint_key = Constraint.LESS_OR_EQUAL
+
+ valid_types = (int, float, datetime.date,
+ datetime.time, datetime.datetime)
+
+ valid_prop_types = (Schema.INTEGER, Schema.FLOAT, Schema.TIMESTAMP,
+ Schema.SCALAR_UNIT_SIZE, Schema.SCALAR_UNIT_FREQUENCY,
+ Schema.SCALAR_UNIT_TIME)
+
+ def __init__(self, property_name, property_type, constraint):
+ super(LessOrEqual, self).__init__(property_name, property_type,
+ constraint)
+ if not isinstance(self.constraint_value, self.valid_types):
+ ExceptionCollector.appendException(
+ InvalidSchemaError(message=_('The property "less_or_equal" '
+ 'expects comparable values.')))
+
+ def _is_valid(self, value):
+ if value <= self.constraint_value:
+ return True
+
+ return False
+
+ def _err_msg(self, value):
+ return (_('The value "%(pvalue)s" of property "%(pname)s" must be '
+ 'less than or equal to "%(cvalue)s".') %
+ dict(pname=self.property_name,
+ pvalue=self.value_msg,
+ cvalue=self.constraint_value_msg))
+*/ \ No newline at end of file
diff --git a/jtosca/src/main/java/org/openecomp/sdc/toscaparser/api/elements/constraints/LessThan.java b/jtosca/src/main/java/org/openecomp/sdc/toscaparser/api/elements/constraints/LessThan.java
new file mode 100644
index 0000000..2432e82
--- /dev/null
+++ b/jtosca/src/main/java/org/openecomp/sdc/toscaparser/api/elements/constraints/LessThan.java
@@ -0,0 +1,103 @@
+package org.openecomp.sdc.toscaparser.api.elements.constraints;
+
+import java.util.Date;
+
+import org.openecomp.sdc.toscaparser.api.common.ExceptionCollector;
+
+public class LessThan extends Constraint {
+
+ @Override
+ protected void _setValues() {
+
+ constraintKey = LESS_THAN;
+
+ validTypes.add("Integer");
+ validTypes.add("Double");
+ validTypes.add("Float");
+ // timestamps are loaded as Date objects
+ validTypes.add("Date");
+ //validTypes.add("datetime.date");
+ //validTypes.add("datetime.time");
+ //validTypes.add("datetime.datetime");
+
+
+ validPropTypes.add(Schema.INTEGER);
+ validPropTypes.add(Schema.FLOAT);
+ validPropTypes.add(Schema.TIMESTAMP);
+ validPropTypes.add(Schema.SCALAR_UNIT_SIZE);
+ validPropTypes.add(Schema.SCALAR_UNIT_FREQUENCY);
+ validPropTypes.add(Schema.SCALAR_UNIT_TIME);
+
+ }
+
+ public LessThan(String name,String type,Object c) {
+ super(name,type,c);
+
+ if(!validTypes.contains(constraintValue.getClass().getSimpleName())) {
+ ExceptionCollector.appendException("InvalidSchemaError: The property \"less_than\" expects comparable values");
+ }
+ }
+
+ @Override
+ protected boolean _isValid(Object value) {
+
+ // timestamps
+ if(value instanceof Date) {
+ if(constraintValue instanceof Date) {
+ return ((Date)value).before((Date)constraintValue);
+ }
+ return false;
+ }
+
+ Double n1 = new Double(value.toString());
+ Double n2 = new Double(constraintValue.toString());
+ return n1 < n2;
+ }
+
+ @Override
+ protected String _errMsg(Object value) {
+ return String.format("The value \"%s\" of property \"%s\" must be less than \"%s\"",
+ valueMsg,propertyName,constraintValueMsg);
+ }
+
+}
+
+/*python
+
+class LessThan(Constraint):
+"""Constraint class for "less_than"
+
+Constrains a property or parameter to a value less than ('<')
+the value declared.
+"""
+
+constraint_key = Constraint.LESS_THAN
+
+valid_types = (int, float, datetime.date,
+ datetime.time, datetime.datetime)
+
+valid_prop_types = (Schema.INTEGER, Schema.FLOAT, Schema.TIMESTAMP,
+ Schema.SCALAR_UNIT_SIZE, Schema.SCALAR_UNIT_FREQUENCY,
+ Schema.SCALAR_UNIT_TIME)
+
+def __init__(self, property_name, property_type, constraint):
+ super(LessThan, self).__init__(property_name, property_type,
+ constraint)
+ if not isinstance(self.constraint_value, self.valid_types):
+ ExceptionCollector.appendException(
+ InvalidSchemaError(message=_('The property "less_than" '
+ 'expects comparable values.')))
+
+def _is_valid(self, value):
+ if value < self.constraint_value:
+ return True
+
+ return False
+
+def _err_msg(self, value):
+ return (_('The value "%(pvalue)s" of property "%(pname)s" must be '
+ 'less than "%(cvalue)s".') %
+ dict(pname=self.property_name,
+ pvalue=self.value_msg,
+ cvalue=self.constraint_value_msg))
+*/ \ No newline at end of file
diff --git a/jtosca/src/main/java/org/openecomp/sdc/toscaparser/api/elements/constraints/MaxLength.java b/jtosca/src/main/java/org/openecomp/sdc/toscaparser/api/elements/constraints/MaxLength.java
new file mode 100644
index 0000000..d50fd1a
--- /dev/null
+++ b/jtosca/src/main/java/org/openecomp/sdc/toscaparser/api/elements/constraints/MaxLength.java
@@ -0,0 +1,89 @@
+package org.openecomp.sdc.toscaparser.api.elements.constraints;
+
+import java.util.LinkedHashMap;
+
+import org.openecomp.sdc.toscaparser.api.common.ExceptionCollector;
+
+public class MaxLength extends Constraint {
+ // Constraint class for "min_length"
+
+ // Constrains the property or parameter to a value of a maximum length.
+
+ @Override
+ protected void _setValues() {
+
+ constraintKey = MAX_LENGTH;
+
+ validTypes.add("Integer");
+
+ validPropTypes.add(Schema.STRING);
+ validPropTypes.add(Schema.MAP);
+
+ }
+
+ public MaxLength(String name,String type,Object c) {
+ super(name,type,c);
+
+ if(!validTypes.contains(constraintValue.getClass().getSimpleName())) {
+ ExceptionCollector.appendException("InvalidSchemaError: The property \"max_length\" expects an integer");
+ }
+ }
+
+ @SuppressWarnings("unchecked")
+ @Override
+ protected boolean _isValid(Object value) {
+ if(value instanceof String && constraintValue instanceof Integer &&
+ ((String)value).length() <= (Integer)constraintValue) {
+ return true;
+ }
+ else if(value instanceof LinkedHashMap && constraintValue instanceof Integer &&
+ ((LinkedHashMap<String,Object>)value).size() <= (Integer)constraintValue) {
+ return true;
+ }
+ return false;
+ }
+
+ @Override
+ protected String _errMsg(Object value) {
+ return String.format("Length of value \"%s\" of property \"%s\" must be no greater than \"%s\"",
+ value.toString(),propertyName,constraintValue.toString());
+ }
+
+}
+
+/*python
+
+class MaxLength(Constraint):
+ """Constraint class for "max_length"
+
+ Constrains the property or parameter to a value to a maximum length.
+ """
+
+ constraint_key = Constraint.MAX_LENGTH
+
+ valid_types = (int, )
+
+ valid_prop_types = (Schema.STRING, Schema.MAP)
+
+ def __init__(self, property_name, property_type, constraint):
+ super(MaxLength, self).__init__(property_name, property_type,
+ constraint)
+ if not isinstance(self.constraint_value, self.valid_types):
+ ExceptionCollector.appendException(
+ InvalidSchemaError(message=_('The property "max_length" '
+ 'expects an integer.')))
+
+ def _is_valid(self, value):
+ if ((isinstance(value, str) or isinstance(value, dict)) and
+ len(value) <= self.constraint_value):
+ return True
+
+ return False
+
+ def _err_msg(self, value):
+ return (_('Length of value "%(pvalue)s" of property "%(pname)s" '
+ 'must be no greater than "%(cvalue)s".') %
+ dict(pname=self.property_name,
+ pvalue=value,
+ cvalue=self.constraint_value))
+*/ \ No newline at end of file
diff --git a/jtosca/src/main/java/org/openecomp/sdc/toscaparser/api/elements/constraints/MinLength.java b/jtosca/src/main/java/org/openecomp/sdc/toscaparser/api/elements/constraints/MinLength.java
new file mode 100644
index 0000000..50fe7a1
--- /dev/null
+++ b/jtosca/src/main/java/org/openecomp/sdc/toscaparser/api/elements/constraints/MinLength.java
@@ -0,0 +1,89 @@
+package org.openecomp.sdc.toscaparser.api.elements.constraints;
+
+import java.util.LinkedHashMap;
+
+import org.openecomp.sdc.toscaparser.api.common.ExceptionCollector;
+
+public class MinLength extends Constraint {
+ // Constraint class for "min_length"
+
+ // Constrains the property or parameter to a value of a minimum length.
+
+ @Override
+ protected void _setValues() {
+
+ constraintKey = MIN_LENGTH;
+
+ validTypes.add("Integer");
+
+ validPropTypes.add(Schema.STRING);
+ validPropTypes.add(Schema.MAP);
+
+ }
+
+ public MinLength(String name,String type,Object c) {
+ super(name,type,c);
+
+ if(!validTypes.contains(constraintValue.getClass().getSimpleName())) {
+ ExceptionCollector.appendException("InvalidSchemaError: The property \"min_length\" expects an integer");
+ }
+ }
+
+ @SuppressWarnings("unchecked")
+ @Override
+ protected boolean _isValid(Object value) {
+ if(value instanceof String && constraintValue instanceof Integer &&
+ ((String)value).length() >= (Integer)constraintValue) {
+ return true;
+ }
+ else if(value instanceof LinkedHashMap && constraintValue instanceof Integer &&
+ ((LinkedHashMap<String,Object>)value).size() >= (Integer)constraintValue) {
+ return true;
+ }
+ return false;
+ }
+
+ @Override
+ protected String _errMsg(Object value) {
+ return String.format("Length of value \"%s\" of property \"%s\" must be at least \"%s\"",
+ value.toString(),propertyName,constraintValue.toString());
+ }
+
+}
+
+/*python
+
+class MinLength(Constraint):
+ """Constraint class for "min_length"
+
+ Constrains the property or parameter to a value to a minimum length.
+ """
+
+ constraint_key = Constraint.MIN_LENGTH
+
+ valid_types = (int, )
+
+ valid_prop_types = (Schema.STRING, Schema.MAP)
+
+ def __init__(self, property_name, property_type, constraint):
+ super(MinLength, self).__init__(property_name, property_type,
+ constraint)
+ if not isinstance(self.constraint_value, self.valid_types):
+ ExceptionCollector.appendException(
+ InvalidSchemaError(message=_('The property "min_length" '
+ 'expects an integer.')))
+
+ def _is_valid(self, value):
+ if ((isinstance(value, str) or isinstance(value, dict)) and
+ len(value) >= self.constraint_value):
+ return True
+
+ return False
+
+ def _err_msg(self, value):
+ return (_('Length of value "%(pvalue)s" of property "%(pname)s" '
+ 'must be at least "%(cvalue)s".') %
+ dict(pname=self.property_name,
+ pvalue=value,
+ cvalue=self.constraint_value))
+*/ \ No newline at end of file
diff --git a/jtosca/src/main/java/org/openecomp/sdc/toscaparser/api/elements/constraints/Pattern.java b/jtosca/src/main/java/org/openecomp/sdc/toscaparser/api/elements/constraints/Pattern.java
new file mode 100644
index 0000000..2fa5a2f
--- /dev/null
+++ b/jtosca/src/main/java/org/openecomp/sdc/toscaparser/api/elements/constraints/Pattern.java
@@ -0,0 +1,95 @@
+package org.openecomp.sdc.toscaparser.api.elements.constraints;
+
+import java.util.regex.Matcher;
+import java.util.regex.PatternSyntaxException;
+
+import org.openecomp.sdc.toscaparser.api.common.ExceptionCollector;
+
+public class Pattern extends Constraint {
+
+ @Override
+ protected void _setValues() {
+
+ constraintKey = PATTERN;
+
+ validTypes.add("String");
+
+ validPropTypes.add(Schema.STRING);
+
+ }
+
+
+ public Pattern(String name,String type,Object c) {
+ super(name,type,c);
+
+ if(!validTypes.contains(constraintValue.getClass().getSimpleName())) {
+ ExceptionCollector.appendException("InvalidSchemaError: The property \"pattern\" expects a string");
+ }
+ }
+
+ @Override
+ protected boolean _isValid(Object value) {
+ try {
+ if(!(value instanceof String)) {
+ ExceptionCollector.appendException(String.format("ValueError: Input value \"%s\" to \"pattern\" property \"%s\" must be a string",
+ value.toString(),propertyName));
+ return false;
+ }
+ String strp = constraintValue.toString();
+ String strm = value.toString();
+ java.util.regex.Pattern pattern = java.util.regex.Pattern.compile(strp);
+ Matcher matcher = pattern.matcher(strm);
+ if(matcher.find() && matcher.end() == strm.length()) {
+ return true;
+ }
+ return false;
+ }
+ catch(PatternSyntaxException pse) {
+ ExceptionCollector.appendException(String.format("ValueError: Invalid regex \"%s\" in \"pattern\" property \"%s\"",
+ constraintValue.toString(),propertyName));
+ return false;
+ }
+ }
+
+ @Override
+ protected String _errMsg(Object value) {
+ return String.format("The value \"%s\" of property \"%s\" does not match the pattern \"%s\"",
+ value.toString(),propertyName,constraintValue.toString());
+ }
+
+}
+
+/*python
+
+class Pattern(Constraint):
+ """Constraint class for "pattern"
+
+ Constrains the property or parameter to a value that is allowed by
+ the provided regular expression.
+ """
+
+ constraint_key = Constraint.PATTERN
+
+ valid_types = (str, )
+
+ valid_prop_types = (Schema.STRING, )
+
+ def __init__(self, property_name, property_type, constraint):
+ super(Pattern, self).__init__(property_name, property_type, constraint)
+ if not isinstance(self.constraint_value, self.valid_types):
+ ExceptionCollector.appendException(
+ InvalidSchemaError(message=_('The property "pattern" '
+ 'expects a string.')))
+ self.match = re.compile(self.constraint_value).match
+
+ def _is_valid(self, value):
+ match = self.match(value)
+ return match is not None and match.end() == len(value)
+
+ def _err_msg(self, value):
+ return (_('The value "%(pvalue)s" of property "%(pname)s" does not '
+ 'match pattern "%(cvalue)s".') %
+ dict(pname=self.property_name,
+ pvalue=value,
+ cvalue=self.constraint_value))
+*/ \ No newline at end of file
diff --git a/jtosca/src/main/java/org/openecomp/sdc/toscaparser/api/elements/constraints/Schema.java b/jtosca/src/main/java/org/openecomp/sdc/toscaparser/api/elements/constraints/Schema.java
new file mode 100644
index 0000000..c21bd7b
--- /dev/null
+++ b/jtosca/src/main/java/org/openecomp/sdc/toscaparser/api/elements/constraints/Schema.java
@@ -0,0 +1,276 @@
+package org.openecomp.sdc.toscaparser.api.elements.constraints;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.LinkedHashMap;
+import java.util.Map;
+
+import org.openecomp.sdc.toscaparser.api.common.ExceptionCollector;
+
+
+public class Schema {
+
+ private static final String TYPE = "type";
+ private static final String REQUIRED = "required";
+ private static final String DESCRIPTION = "description";
+ private static final String DEFAULT = "default";
+ private static final String CONSTRAINTS = "constraints";
+ private static final String STATUS = "status";
+ private static final String ENTRYSCHEMA = "entry_schema";
+ private static final String KEYS[] = {
+ TYPE, REQUIRED, DESCRIPTION,DEFAULT, CONSTRAINTS, ENTRYSCHEMA, STATUS};
+
+ public static final String INTEGER = "integer";
+ public static final String STRING = "string";
+ public static final String BOOLEAN = "boolean";
+ public static final String FLOAT = "float";
+ public static final String RANGE = "range";
+ public static final String NUMBER = "number";
+ public static final String TIMESTAMP = "timestamp";
+ public static final String LIST = "list";
+ public static final String MAP = "map";
+ public static final String SCALAR_UNIT_SIZE = "scalar-unit.size";
+ public static final String SCALAR_UNIT_FREQUENCY = "scalar-unit.frequency";
+ public static final String SCALAR_UNIT_TIME = "scalar-unit.time";
+ public static final String VERSION = "version";
+ public static final String PORTDEF = "PortDef";
+ public static final String PORTSPEC = "PortSpec"; //??? PortSpec.SHORTNAME
+
+ public static final String PROPERTY_TYPES[] = {
+ INTEGER, STRING, BOOLEAN, FLOAT, RANGE,NUMBER, TIMESTAMP, LIST, MAP,
+ SCALAR_UNIT_SIZE, SCALAR_UNIT_FREQUENCY, SCALAR_UNIT_TIME,
+ VERSION, PORTDEF, PORTSPEC};
+
+ @SuppressWarnings("unused")
+ private static final String SCALAR_UNIT_SIZE_DEFAULT = "B";
+
+ private static Map<String,Long> SCALAR_UNIT_SIZE_DICT = new HashMap<>();
+ static {
+ SCALAR_UNIT_SIZE_DICT.put("B", 1L);
+ SCALAR_UNIT_SIZE_DICT.put("KB", 1000L);
+ SCALAR_UNIT_SIZE_DICT.put("KIB", 1024L);
+ SCALAR_UNIT_SIZE_DICT.put("MB", 1000000L);
+ SCALAR_UNIT_SIZE_DICT.put("MIB", 1048576L);
+ SCALAR_UNIT_SIZE_DICT.put("GB", 1000000000L);
+ SCALAR_UNIT_SIZE_DICT.put("GIB", 1073741824L);
+ SCALAR_UNIT_SIZE_DICT.put("TB", 1000000000000L);
+ SCALAR_UNIT_SIZE_DICT.put("TIB", 1099511627776L);
+ }
+
+ private String name;
+ private LinkedHashMap<String,Object> schema;
+ private int _len;
+ private ArrayList<Constraint> constraintsList;
+
+
+ public Schema(String _name,LinkedHashMap<String,Object> _schemaDict) {
+ name = _name;
+
+ if(!(_schemaDict instanceof LinkedHashMap)) {
+ //msg = (_('Schema definition of "%(pname)s" must be a dict.')
+ // % dict(pname=name))
+ ExceptionCollector.appendException(String.format(
+ "InvalidSchemaError: Schema definition of \"%s\" must be a dict",name));
+ }
+
+ if(_schemaDict.get("type") == null) {
+ //msg = (_('Schema definition of "%(pname)s" must have a "type" '
+ // 'attribute.') % dict(pname=name))
+ ExceptionCollector.appendException(String.format(
+ "InvalidSchemaError: Schema definition of \"%s\" must have a \"type\" attribute",name));
+ }
+
+ schema = _schemaDict;
+ _len = 0; //??? None
+ constraintsList = new ArrayList<>();
+ }
+
+ public String getType() {
+ return (String)schema.get(TYPE);
+ }
+
+ public boolean isRequired() {
+ return (boolean)schema.getOrDefault(REQUIRED, true);
+ }
+
+ public String getDescription() {
+ return (String)schema.getOrDefault(DESCRIPTION,"");
+ }
+
+ public Object getDefault() {
+ return schema.get(DEFAULT);
+ }
+
+ public String getStatus() {
+ return (String)schema.getOrDefault(STATUS,"");
+ }
+
+ @SuppressWarnings("unchecked")
+ public ArrayList<Constraint> getConstraints() {
+ if(constraintsList.size() == 0) {
+ Object cob = schema.get(CONSTRAINTS);
+ if(cob instanceof ArrayList) {
+ ArrayList<Object> constraintSchemata = (ArrayList<Object>)cob;
+ for(Object ob: constraintSchemata) {
+ if(ob instanceof LinkedHashMap) {
+ for(String cClass: ((LinkedHashMap<String,Object>)ob).keySet()) {
+ Constraint c = Constraint.factory(cClass,name,getType(),ob);
+ if(c != null) {
+ constraintsList.add(c);
+ }
+ else {
+ // error
+ ExceptionCollector.appendException(String.format(
+ "UnknownFieldError: Constraint type \"%s\" for property \"%s\" is not supported",
+ cClass,name));
+ }
+ break;
+ }
+ }
+ }
+ }
+ }
+ return constraintsList;
+ }
+
+ @SuppressWarnings("unchecked")
+ public LinkedHashMap<String,Object> getEntrySchema() {
+ return (LinkedHashMap<String,Object>)schema.get(ENTRYSCHEMA);
+ }
+
+ // Python intrinsic methods...
+
+ // substitute for __getitem__ (aka self[key])
+ public Object getItem(String key) {
+ return schema.get(key);
+ }
+
+ /*
+ def __iter__(self):
+ for k in self.KEYS:
+ try:
+ self.schema[k]
+ except KeyError:
+ pass
+ else:
+ yield k
+ */
+
+ // substitute for __len__ (aka self.len())
+ public int getLen() {
+ int len = 0;
+ for(String k: KEYS) {
+ if(schema.get(k) != null) {
+ len++;
+ }
+ _len = len;
+ }
+ return _len;
+ }
+ // getter
+ public LinkedHashMap<String,Object> getSchema() {
+ return schema;
+ }
+
+}
+
+/*python
+
+class Schema(collections.Mapping):
+
+KEYS = (
+ TYPE, REQUIRED, DESCRIPTION,
+ DEFAULT, CONSTRAINTS, ENTRYSCHEMA, STATUS
+) = (
+ 'type', 'required', 'description',
+ 'default', 'constraints', 'entry_schema', 'status'
+)
+
+PROPERTY_TYPES = (
+ INTEGER, STRING, BOOLEAN, FLOAT, RANGE,
+ NUMBER, TIMESTAMP, LIST, MAP,
+ SCALAR_UNIT_SIZE, SCALAR_UNIT_FREQUENCY, SCALAR_UNIT_TIME,
+ VERSION, PORTDEF, PORTSPEC
+) = (
+ 'integer', 'string', 'boolean', 'float', 'range',
+ 'number', 'timestamp', 'list', 'map',
+ 'scalar-unit.size', 'scalar-unit.frequency', 'scalar-unit.time',
+ 'version', 'PortDef', PortSpec.SHORTNAME
+)
+
+SCALAR_UNIT_SIZE_DEFAULT = 'B'
+SCALAR_UNIT_SIZE_DICT = {'B': 1, 'KB': 1000, 'KIB': 1024, 'MB': 1000000,
+ 'MIB': 1048576, 'GB': 1000000000,
+ 'GIB': 1073741824, 'TB': 1000000000000,
+ 'TIB': 1099511627776}
+
+def __init__(self, name, schema_dict):
+ self.name = name
+ if not isinstance(schema_dict, collections.Mapping):
+ msg = (_('Schema definition of "%(pname)s" must be a dict.')
+ % dict(pname=name))
+ ExceptionCollector.appendException(InvalidSchemaError(message=msg))
+
+ try:
+ schema_dict['type']
+ except KeyError:
+ msg = (_('Schema definition of "%(pname)s" must have a "type" '
+ 'attribute.') % dict(pname=name))
+ ExceptionCollector.appendException(InvalidSchemaError(message=msg))
+
+ self.schema = schema_dict
+ self._len = None
+ self.constraints_list = []
+
+@property
+def type(self):
+ return self.schema[self.TYPE]
+
+@property
+def required(self):
+ return self.schema.get(self.REQUIRED, True)
+
+@property
+def description(self):
+ return self.schema.get(self.DESCRIPTION, '')
+
+@property
+def default(self):
+ return self.schema.get(self.DEFAULT)
+
+@property
+def status(self):
+ return self.schema.get(self.STATUS, '')
+
+@property
+def constraints(self):
+ if not self.constraints_list:
+ constraint_schemata = self.schema.get(self.CONSTRAINTS)
+ if constraint_schemata:
+ self.constraints_list = [Constraint(self.name,
+ self.type,
+ cschema)
+ for cschema in constraint_schemata]
+ return self.constraints_list
+
+@property
+def entry_schema(self):
+ return self.schema.get(self.ENTRYSCHEMA)
+
+def __getitem__(self, key):
+ return self.schema[key]
+
+def __iter__(self):
+ for k in self.KEYS:
+ try:
+ self.schema[k]
+ except KeyError:
+ pass
+ else:
+ yield k
+
+def __len__(self):
+ if self._len is None:
+ self._len = len(list(iter(self)))
+ return self._len
+*/ \ No newline at end of file
diff --git a/jtosca/src/main/java/org/openecomp/sdc/toscaparser/api/elements/constraints/ValidValues.java b/jtosca/src/main/java/org/openecomp/sdc/toscaparser/api/elements/constraints/ValidValues.java
new file mode 100644
index 0000000..06622e4
--- /dev/null
+++ b/jtosca/src/main/java/org/openecomp/sdc/toscaparser/api/elements/constraints/ValidValues.java
@@ -0,0 +1,84 @@
+package org.openecomp.sdc.toscaparser.api.elements.constraints;
+
+import java.util.ArrayList;
+
+public class ValidValues extends Constraint {
+
+
+ protected void _setValues() {
+
+ constraintKey = VALID_VALUES;
+
+ for(String s: Schema.PROPERTY_TYPES) {
+ validPropTypes.add(s);
+ }
+
+ }
+
+
+ public ValidValues(String name,String type,Object c) {
+ super(name,type,c);
+
+ }
+
+ @SuppressWarnings("unchecked")
+ protected boolean _isValid(Object val) {
+ if(!(constraintValue instanceof ArrayList)) {
+ return false;
+ }
+ if(val instanceof ArrayList) {
+ boolean bAll = true;
+ for(Object v: (ArrayList<Object>)val) {
+ if(!((ArrayList<Object>)constraintValue).contains(v)) {
+ bAll = false;
+ break;
+ };
+ }
+ return bAll;
+ }
+ return ((ArrayList<Object>)constraintValue).contains(val);
+ }
+
+ protected String _errMsg(Object value) {
+ return String.format("The value \"%s\" of property \"%s\" is not valid. Expected a value from \"%s\"",
+ value.toString(),propertyName,constraintValue.toString());
+ }
+
+}
+
+/*python
+
+class ValidValues(Constraint):
+"""Constraint class for "valid_values"
+
+Constrains a property or parameter to a value that is in the list of
+declared values.
+"""
+constraint_key = Constraint.VALID_VALUES
+
+valid_prop_types = Schema.PROPERTY_TYPES
+
+def __init__(self, property_name, property_type, constraint):
+ super(ValidValues, self).__init__(property_name, property_type,
+ constraint)
+ if not isinstance(self.constraint_value, collections.Sequence):
+ ExceptionCollector.appendException(
+ InvalidSchemaError(message=_('The property "valid_values" '
+ 'expects a list.')))
+
+def _is_valid(self, value):
+ print '*** payton parser validating ',value,' in ',self.constraint_value#GGG
+ if isinstance(value, list):
+ return all(v in self.constraint_value for v in value)
+ return value in self.constraint_value
+
+def _err_msg(self, value):
+ allowed = '[%s]' % ', '.join(str(a) for a in self.constraint_value)
+ return (_('The value "%(pvalue)s" of property "%(pname)s" is not '
+ 'valid. Expected a value from "%(cvalue)s".') %
+ dict(pname=self.property_name,
+ pvalue=value,
+ cvalue=allowed))
+
+
+*/ \ No newline at end of file