aboutsummaryrefslogtreecommitdiffstats
path: root/jtosca/src/main/java/org/openecomp/sdc/toscaparser/api/parameters/Input.java
blob: 7b3e64f7eb5acffdf77b8b22d6971a5b62436049 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
package org.openecomp.sdc.toscaparser.api.parameters;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedHashMap;

import org.openecomp.sdc.toscaparser.api.DataEntity;
import org.openecomp.sdc.toscaparser.api.common.ExceptionCollector;
import org.openecomp.sdc.toscaparser.api.elements.EntityType;
import org.openecomp.sdc.toscaparser.api.elements.constraints.Constraint;
import org.openecomp.sdc.toscaparser.api.elements.constraints.Schema;
import org.openecomp.sdc.toscaparser.api.utils.ThreadLocalsHolder;

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";
	
	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 LIST = "list";
	public static final String MAP = "map";
	public static final String JSON = "json";
    
	private static String INPUTFIELD[] = {
    		TYPE, DESCRIPTION, DEFAULT, CONSTRAINTS, REQUIRED,STATUS, ENTRY_SCHEMA
    };
	
	private static String PRIMITIVE_TYPES[] = {
			INTEGER, STRING, BOOLEAN, FLOAT, LIST, MAP, JSON
    };
    
    private String name;
    private Schema schema;
	private LinkedHashMap<String,Object> customDefs;
	
	public Input(String _name,LinkedHashMap<String,Object> _schemaDict,LinkedHashMap<String,Object> _customDefs) {
		name = _name;
		schema = new Schema(_name,_schemaDict);
		customDefs = _customDefs;
	}
	
	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) {
                ThreadLocalsHolder.getCollector().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) {
			if(customDefs.get(inputType) != null) {
				bFound = true;
			}
		}
		
		if(!bFound) {
            ThreadLocalsHolder.getCollector().appendException(String.format(
                    "ValueError: Invalid type \"%s\"",inputType));
		}
    }
    
    private void _validateValue(Object value) {
    	Object datatype = null;
    	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());
    	}
    	
    	String type = getType();
    	// if it's one of the basic types DON'T look in customDefs
    	if(Arrays.asList(PRIMITIVE_TYPES).contains(type)) {
        	DataEntity.validateDatatype(getType(), value, null, (LinkedHashMap<String,Object>)datatype, null);
        	return;	
    	}
    	else if(customDefs.get(getType()) != null) {
    		datatype = customDefs.get(getType());
        	DataEntity.validateDatatype(getType(), value, (LinkedHashMap<String,Object>)datatype, customDefs, null);
        	return;
    	}
    	
    	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)

*/