aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/org/onap/sdc/toscaparser/api/parameters/Input.java
blob: 106fe946a71e7b018dc77ae2125cc02ce9cf42ea (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
/*-
 * ============LICENSE_START=======================================================
 * SDC
 * ================================================================================
 * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * 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.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.onap.sdc.toscaparser.api.parameters;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.stream.Collectors;

import org.onap.sdc.toscaparser.api.DataEntity;
import org.onap.sdc.toscaparser.api.common.JToscaValidationIssue;
import org.onap.sdc.toscaparser.api.elements.EntityType;
import org.onap.sdc.toscaparser.api.elements.constraints.Constraint;
import org.onap.sdc.toscaparser.api.elements.constraints.Schema;
import org.onap.sdc.toscaparser.api.elements.enums.ToscaElementNames;
import org.onap.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;
	private Map<String, Annotation> annotations;
	
	public Input(){
		/**
		 * Added to support Input serialization
		 */
	}
	
	public Input(String _name,LinkedHashMap<String,Object> _schemaDict,LinkedHashMap<String,Object> _customDefs) {
		name = _name;
		schema = new Schema(_name,_schemaDict);
		customDefs = _customDefs;
	}

	@SuppressWarnings("unchecked")
	public void parseAnnotations() {
		if(schema.getSchema() != null){
			LinkedHashMap<String, Object> annotations = (LinkedHashMap<String, Object>) schema.getSchema().get(ToscaElementNames.ANNOTATIONS.getName());
			if(annotations != null){
				setAnnotations(annotations.entrySet().stream()
				.map(Annotation::new)
				.filter(Annotation::isHeatSourceType)
				.collect(Collectors.toMap(a -> a.getName(), a -> a)));
			}
		}
	}

	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().appendValidationIssue(new JToscaValidationIssue("JE214", 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().appendValidationIssue(new JToscaValidationIssue("JE215", String.format(
                    "ValueError: Invalid type \"%s\"",inputType))); 
		}
    }
    
    @SuppressWarnings("unchecked")
	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, customDefs, 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, customDefs, null);
    }

	public Map<String, Annotation> getAnnotations() {
		return annotations;
	}

	private void setAnnotations(Map<String, Annotation> annotations) {
		this.annotations = annotations;
	}
	
	public void resetAnnotaions(){
		annotations = null;
	}

	public LinkedHashMap<String,Object> getEntrySchema() {
		return schema.getEntrySchema();
	}

}