summaryrefslogtreecommitdiffstats
path: root/dcaedt_be/src/main/java/org/onap/sdc/dcae/ves/VesDataTypeDefinition.java
blob: 5465d628131799da9c7d4fae06de503a801720bd (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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
package org.onap.sdc.dcae.ves;

import com.google.gson.JsonElement;
import com.google.gson.annotations.SerializedName;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang.StringUtils;

import java.util.*;

public class VesDataTypeDefinition {

	private static final String jsonReferencePrefix = "#/definitions/";
	private String type;
	private String description;
	private String format;
	private String title;
	private Map<String, VesDataTypeDefinition> properties;
	private List<String> required = new ArrayList<>();
	@SerializedName("enum")
	private List<String> enums;
	@SerializedName("default")
	private JsonElement defaultValue;
	private VesDataItemsDefinition items;
	@SerializedName("$ref")
	private String ref;

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public String getFormat() {
		return format;
	}

	public void setFormat(String format) {
		this.format = format;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public Map<String, VesDataTypeDefinition> getProperties() {
		return properties;
	}

	public void setProperties(Map<String, VesDataTypeDefinition> properties) {
		this.properties = properties;
	}

	public List<String> getRequired() {
		return required;
	}

	public void setRequired(List<String> required) {
		this.required = required;
	}

	public List<String> getEnums() {
		return enums;
	}

	public void setEnums(List<String> enums) {
		this.enums = enums;
	}

	public JsonElement getDefaultValue() {
		return defaultValue;
	}

	public void setDefaultValue(JsonElement defaultValue) {
		this.defaultValue = defaultValue;
	}

	public VesDataItemsDefinition getItems() {
		return items;
	}

	public void setItems(VesDataItemsDefinition items) {
		this.items = items;
	}

	public String getRef() {
		return ref;
	}

	public void setRef(String ref) {
		this.ref = ref;
	}

	protected boolean hasReference() {
		return StringUtils.isNotBlank(getRef());
	}

	protected boolean itemsContainReference() {
		return CollectionUtils.isNotEmpty(getItems()) && getItems().stream().anyMatch(VesDataTypeDefinition::containsAnyReferenceItem);
	}

	protected boolean propertiesContainReference() {
		return MapUtils.isNotEmpty(getProperties()) && getProperties().values().stream().anyMatch(VesDataTypeDefinition::containsAnyReferenceItem);
	}

	protected boolean containsAnyReferenceItem() {
		return hasReference() || itemsContainReference() || propertiesContainReference();
	}

	protected String getJsonRefPointer() {
		return getRef().replace(jsonReferencePrefix, "");
	}

	private void addReferenceItem(Set<String> allRefs) {
		if (hasReference()) {
			allRefs.add(getJsonRefPointer());
		}
	}

	private Set<String> extractAllReferenceTokens() {
		Set<String> allRefs = new HashSet<>();
		extractReferenceTokens(allRefs);
		return allRefs;
	}

	private void extractReferenceTokens(Set<String> allRefs) {

		addReferenceItem(allRefs);
		if (itemsContainReference()) {
			getItems().forEach(item -> item.extractReferenceTokens(allRefs));
		}
		if (propertiesContainReference()) {
			getProperties().values().forEach(property -> property.extractReferenceTokens(allRefs));
		}
	}

	protected boolean isResolvable(Map<String, VesDataTypeDefinition> resolvedTypes) {
		return resolvedTypes.keySet().containsAll(extractAllReferenceTokens());
	}

	private void resolveReference(Map<String, VesDataTypeDefinition> resolvedTypes) {
		if (hasReference()) {
			VesDataTypeDefinition other = resolvedTypes.get(getJsonRefPointer());
			setType(other.getType());
			setRef(other.getRef());
			setDefaultValue(other.getDefaultValue());
			setDescription(other.getDescription());
			setEnums(other.getEnums());
			setProperties(other.getProperties());
			setFormat(other.getFormat());
			setRequired(other.getRequired());
			setItems(other.getItems());
			setTitle(other.getTitle());
		}
	}

	private void resolveItemReferences(Map<String, VesDataTypeDefinition> resolvedTypes) {
		if (itemsContainReference()) {
			for (VesDataTypeDefinition item : getItems()) {
				item.resolveAllReferences(resolvedTypes);
			}
		}
	}

	private void resolvePropertyReferences(Map<String, VesDataTypeDefinition> resolvedTypes) {
		if (propertiesContainReference()) {
			for (VesDataTypeDefinition property : getProperties().values()) {
				property.resolveAllReferences(resolvedTypes);
			}
		}
	}

	// the reference resolver is called on each VesDataTypeDefinition after it passes the 'isResolvable' validation, affirming that all its references(direct/properties/items) point to a resolved VesDataTypeDefinition (has no references)
	protected void resolveAllReferences(Map<String, VesDataTypeDefinition> resolvedTypes) {
		resolveReference(resolvedTypes);
		resolveItemReferences(resolvedTypes);
		resolvePropertyReferences(resolvedTypes);
	}

	private String validateType() {
		return null == type? null : VesSimpleTypesEnum.getSimpleTypes().contains(type) ? null : "invalid type declaration: " + type;
	}

	private String validateRequired() {
		String invalid = null == type? null : !type.equals(VesSimpleTypesEnum.OBJECT.getType()) ? null : required.stream().filter(r -> !properties.keySet().contains(r)).findAny().orElse(null);
		return StringUtils.isBlank(invalid) ? invalid : "invalid required entry: " + invalid;
	}

	// returns error message detailing invalid 'type' or 'required' fields (null for success)
	protected String validate() {
		String error = validateType();
		if (StringUtils.isBlank(error))
			error = validateRequired();
		if (StringUtils.isBlank(error) && CollectionUtils.isNotEmpty(items))
			error = validateItems();
		if(StringUtils.isBlank(error) && MapUtils.isNotEmpty(properties))
			error = validateProperties();
		return error;
	}

	private String validateItems(){
		String error = null;
		for (VesDataTypeDefinition def : items) {
			if (StringUtils.isBlank(error))
				error = def.validate();
			else
				break;
		}
		return error;
	}

	private String validateProperties(){
		String error = null;
		for (VesDataTypeDefinition def : properties.values()) {
			if (StringUtils.isBlank(error))
				error = def.validate();
			else
				break;
		}
		return error;
	}


	@Override
	public boolean equals(Object obj) {
		if (obj == this)
			return true;
		if (null == obj || getClass() != obj.getClass())
			return false;
		VesDataTypeDefinition other = (VesDataTypeDefinition) obj;
		return Objects.equals(type, other.type) &&
				Objects.equals(description, other.description) &&
				Objects.equals(format, other.format) &&
				Objects.equals(title, other.title) &&
				Objects.equals(required, other.required) &&
				Objects.equals(enums, other.enums) &&
				Objects.equals(defaultValue, other.defaultValue) &&
				Objects.equals(items, other.items) &&
				Objects.equals(properties, other.properties) &&
				Objects.equals(ref, other.ref);
	}

	@Override public int hashCode() {
		int result = type != null ? type.hashCode() : 0;
		result = 31 * result + (description != null ? description.hashCode() : 0);
		result = 31 * result + (format != null ? format.hashCode() : 0);
		result = 31 * result + (title != null ? title.hashCode() : 0);
		result = 31 * result + (properties != null ? properties.hashCode() : 0);
		result = 31 * result + (required != null ? required.hashCode() : 0);
		result = 31 * result + (enums != null ? enums.hashCode() : 0);
		result = 31 * result + (defaultValue != null ? defaultValue.hashCode() : 0);
		result = 31 * result + (items != null ? items.hashCode() : 0);
		result = 31 * result + (ref != null ? ref.hashCode() : 0);
		return result;
	}
}