diff options
Diffstat (limited to 'src/main/java/org/onap/sdc/toscaparser/api/elements/constraints/Constraint.java')
-rw-r--r-- | src/main/java/org/onap/sdc/toscaparser/api/elements/constraints/Constraint.java | 336 |
1 files changed, 191 insertions, 145 deletions
diff --git a/src/main/java/org/onap/sdc/toscaparser/api/elements/constraints/Constraint.java b/src/main/java/org/onap/sdc/toscaparser/api/elements/constraints/Constraint.java index 82f6718..dd77659 100644 --- a/src/main/java/org/onap/sdc/toscaparser/api/elements/constraints/Constraint.java +++ b/src/main/java/org/onap/sdc/toscaparser/api/elements/constraints/Constraint.java @@ -7,9 +7,9 @@ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -20,175 +20,221 @@ package org.onap.sdc.toscaparser.api.elements.constraints; -import java.util.ArrayList; -import java.util.LinkedHashMap; - import org.onap.sdc.toscaparser.api.common.JToscaValidationIssue; import org.onap.sdc.toscaparser.api.elements.ScalarUnit; import org.onap.sdc.toscaparser.api.functions.Function; import org.onap.sdc.toscaparser.api.utils.ThreadLocalsHolder; +import java.util.ArrayList; +import java.util.LinkedHashMap; +import java.util.List; + 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) { - ThreadLocalsHolder.getCollector().appendValidationIssue(new JToscaValidationIssue("JE101", - "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); + + // 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) { + ThreadLocalsHolder.getCollector().appendValidationIssue(new JToscaValidationIssue("JE101", + "InvalidSchemaError: Invalid constraint schema " + constraint.toString())); } - else { - ThreadLocalsHolder.getCollector().appendValidationIssue(new JToscaValidationIssue("JE102", String.format( - "InvalidSchemaError: Invalid property \"%s\"",constraintClass))); - return null; + + switch (constraintClass) { + case EQUAL: + return new Equal(propname, proptype, constraint); + case GREATER_THAN: + return new GreaterThan(propname, proptype, constraint); + case GREATER_OR_EQUAL: + return new GreaterOrEqual(propname, proptype, constraint); + case LESS_THAN: + return new LessThan(propname, proptype, constraint); + case LESS_OR_EQUAL: + return new LessOrEqual(propname, proptype, constraint); + case IN_RANGE: + return new InRange(propname, proptype, constraint); + case VALID_VALUES: + return new ValidValues(propname, proptype, constraint); + case LENGTH: + return new Length(propname, proptype, constraint); + case MIN_LENGTH: + return new MinLength(propname, proptype, constraint); + case MAX_LENGTH: + return new MaxLength(propname, proptype, constraint); + case PATTERN: + return new Pattern(propname, proptype, constraint); + default: + ThreadLocalsHolder.getCollector().appendValidationIssue(new JToscaValidationIssue("JE102", 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(); - + } + + private String constraintKey = "TBD"; + protected ArrayList<String> validTypes = new ArrayList<>(); + protected ArrayList<String> validPropTypes = new ArrayList<>(); + + protected String propertyName; + private 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); + 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; - } + for (String s : ScalarUnit.SCALAR_UNIT_TYPES) { + if (s.equals(propertyType)) { + bFound = true; + break; + } } - if(bFound) { + 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; - } + for (String s : validPropTypes) { + if (s.equals(propertyType)) { + bFound = true; + break; + } } - if(!bFound) { + if (!bFound) { ThreadLocalsHolder.getCollector().appendValidationIssue(new JToscaValidationIssue("JE103", String.format( - "InvalidSchemaError: Property \"%s\" is not valid for data type \"%s\"", - constraintKey,propertyType))); + "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) { - if (Function.isFunction(value)){ - //skipping constraints check for functions - return; - } - - valueMsg = value; + } + + public ArrayList<String> getValidTypes() { + return validTypes; + } + + public void addValidTypes(List<String> validTypes) { + this.validTypes.addAll(validTypes); + } + + public ArrayList<String> getValidPropTypes() { + return validPropTypes; + } + + public String getPropertyType() { + return propertyType; + } + + public Object getConstraintValue() { + return constraintValue; + } + + public Object getConstraintValueMsg() { + return constraintValueMsg; + } + + public Object getValueMsg() { + return valueMsg; + } + + public void setConstraintKey(String constraintKey) { + this.constraintKey = constraintKey; + } + + public void setValidTypes(ArrayList<String> validTypes) { + this.validTypes = validTypes; + } + + public void setValidPropTypes(ArrayList<String> validPropTypes) { + this.validPropTypes = validPropTypes; + } + + public void setPropertyType(String propertyType) { + this.propertyType = propertyType; + } + + public void setConstraintValue(Object constraintValue) { + this.constraintValue = constraintValue; + } + + public void setConstraintValueMsg(Object constraintValueMsg) { + this.constraintValueMsg = constraintValueMsg; + } + + public void setValueMsg(Object valueMsg) { + this.valueMsg = valueMsg; + } + + @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) { + if (Function.isFunction(value)) { + //skipping constraints check for functions + return; + } + + valueMsg = value; boolean bFound = false; - for(String s: ScalarUnit.SCALAR_UNIT_TYPES) { - if(s.equals(propertyType)) { - bFound = true; - break; - } + for (String s : ScalarUnit.SCALAR_UNIT_TYPES) { + if (s.equals(propertyType)) { + bFound = true; + break; + } } - if(bFound) { - value = ScalarUnit.getScalarunitValue(propertyType,value,null); + if (bFound) { + value = ScalarUnit.getScalarunitValue(propertyType, value, null); } - if(!_isValid(value)) { - ThreadLocalsHolder.getCollector().appendValidationIssue(new JToscaValidationIssue("JE008", "ValidationError: " + _errMsg(value))); + if (!isValid(value)) { + ThreadLocalsHolder.getCollector().appendValidationIssue(new JToscaValidationIssue("JE008", "ValidationError: " + errMsg(value))); } - } + } + + protected abstract boolean isValid(Object value); + + protected abstract void setValues(); - protected abstract boolean _isValid(Object value); - - protected abstract void _setValues(); + protected abstract String errMsg(Object value); - protected abstract String _errMsg(Object value); - } /*python |