diff options
author | 2017-04-25 08:02:08 +0300 | |
---|---|---|
committer | 2017-04-25 08:02:33 +0300 | |
commit | ecd529b4057f8619c9ef0d1c46d1bec5571f5b29 (patch) | |
tree | 7133ba1350516a03db982ea30228437ebf843536 /jtosca/src/main/java/org/openecomp/sdc/toscaparser/parameters/Input.java | |
parent | 9cb83cb1b15e93c95b1462fc2e3315b35dbc6c4d (diff) |
[SDC-13] JTosca delivery - 1.1.8-SNAPSHOT DC
Change-Id: Ib6af5d9393473c3db3f1e21da570c71ea0944eca
Signed-off-by: Pavel Aharoni <pa0916@att.com>
Diffstat (limited to 'jtosca/src/main/java/org/openecomp/sdc/toscaparser/parameters/Input.java')
-rw-r--r-- | jtosca/src/main/java/org/openecomp/sdc/toscaparser/parameters/Input.java | 193 |
1 files changed, 193 insertions, 0 deletions
diff --git a/jtosca/src/main/java/org/openecomp/sdc/toscaparser/parameters/Input.java b/jtosca/src/main/java/org/openecomp/sdc/toscaparser/parameters/Input.java new file mode 100644 index 0000000..a935030 --- /dev/null +++ b/jtosca/src/main/java/org/openecomp/sdc/toscaparser/parameters/Input.java @@ -0,0 +1,193 @@ +package org.openecomp.sdc.toscaparser.parameters; + +import java.util.ArrayList; +import java.util.LinkedHashMap; + +import org.openecomp.sdc.toscaparser.DataEntity; +import org.openecomp.sdc.toscaparser.common.ExceptionCollector; +import org.openecomp.sdc.toscaparser.elements.EntityType; +import org.openecomp.sdc.toscaparser.elements.constraints.Constraint; +import org.openecomp.sdc.toscaparser.elements.constraints.Schema; + +public class Input { + + private static final String TYPE = "type"; + private static final String DESCRIPTION = "description"; + private static final String DEFAULT = "default"; + private static final String CONSTRAINTS = "constraints"; + private static final String REQUIRED = "required"; + private static final String STATUS = "status"; + private static final String ENTRY_SCHEMA = "entry_schema"; + + private static String INPUTFIELD[] = { + TYPE, DESCRIPTION, DEFAULT, CONSTRAINTS, REQUIRED,STATUS, ENTRY_SCHEMA + }; + + private String name; + private Schema schema; + + + public Input(String _name,LinkedHashMap<String,Object> _schemaDict) { + name = _name; + schema = new Schema(_name,_schemaDict); + } + + public String getName() { + return name; + } + + public String getType() { + return schema.getType(); + } + + public String getDescription() { + return schema.getDescription(); + } + + public boolean isRequired() { + return schema.isRequired(); + } + + public Object getDefault() { + return schema.getDefault(); + } + + public ArrayList<Constraint> getConstraints() { + return schema.getConstraints(); + } + + public void validate(Object value) { + _validateField(); + _validateType(getType()); + if(value != null) { + _validateValue(value); + } + } + + private void _validateField() { + for(String key: schema.getSchema().keySet()) { + boolean bFound = false; + for(String ifld: INPUTFIELD) { + if(key.equals(ifld)) { + bFound = true; + break; + } + } + if(!bFound) { + ExceptionCollector.appendException(String.format( + "UnknownFieldError: Input \"%s\" contains unknown field \"%s\"", + name,key)); + } + } + } + + private void _validateType(String inputType) { + boolean bFound = false; + for(String pt: Schema.PROPERTY_TYPES) { + if(pt.equals(inputType)) { + bFound = true; + break; + } + } + if(!bFound) { + ExceptionCollector.appendException(String.format( + "ValueError: Invalid type \"%s\"",inputType)); + } + } + + private void _validateValue(Object value) { + Object datatype = null; + if (EntityType.TOSCA_DEF != null && EntityType.TOSCA_DEF.size() > 0) { + if(EntityType.TOSCA_DEF.get(getType()) != null) { + datatype = EntityType.TOSCA_DEF.get(getType()); + } + else if(EntityType.TOSCA_DEF.get(EntityType.DATATYPE_NETWORK_PREFIX + getType()) != null) { + datatype = EntityType.TOSCA_DEF.get(EntityType.DATATYPE_NETWORK_PREFIX + getType()); + } + } + DataEntity.validateDatatype(getType(),value,null,(LinkedHashMap<String,Object>)datatype,null); + } +} + +/*python + +from toscaparser.common.exception import ExceptionCollector +from toscaparser.common.exception import MissingRequiredFieldError +from toscaparser.common.exception import UnknownFieldError +from toscaparser.dataentity import DataEntity +from toscaparser.elements.constraints import Schema +from toscaparser.elements.entity_type import EntityType +from toscaparser.utils.gettextutils import _ + + +log = logging.getLogger('tosca') + + +class Input(object): + + INPUTFIELD = (TYPE, DESCRIPTION, DEFAULT, CONSTRAINTS, REQUIRED, STATUS, + ENTRY_SCHEMA) = ('type', 'description', 'default', + 'constraints', 'required', 'status', + 'entry_schema') + + def __init__(self, name, schema_dict): + self.name = name + self.schema = Schema(name, schema_dict) + + self._validate_field() + self.validate_type(self.type) + + @property + def type(self): + return self.schema.type + + @property + def required(self): + return self.schema.required + + @property + def description(self): + return self.schema.description + + @property + def default(self): + return self.schema.default + + @property + def constraints(self): + return self.schema.constraints + + @property + def status(self): + return self.schema.status + + def validate(self, value=None): + if value is not None: + self._validate_value(value) + + def _validate_field(self): + for name in self.schema.schema: + if name not in self.INPUTFIELD: + ExceptionCollector.appendException( + UnknownFieldError(what='Input "%s"' % self.name, + field=name)) + + def validate_type(self, input_type): + if input_type not in Schema.PROPERTY_TYPES: + ExceptionCollector.appendException( + ValueError(_('Invalid type "%s".') % type)) + + # tODO(anyone) Need to test for any built-in datatype not just network + # that is, tosca.datatypes.* and not assume tosca.datatypes.network.* + # tODO(anyone) Add support for tosca.datatypes.Credential + def _validate_value(self, value): + tosca = EntityType.TOSCA_DEF + datatype = None + if self.type in tosca: + datatype = tosca[self.type] + elif EntityType.DATATYPE_NETWORK_PREFIX + self.type in tosca: + datatype = tosca[EntityType.DATATYPE_NETWORK_PREFIX + self.type] + + DataEntity.validate_datatype(self.type, value, None, datatype) + +*/ |