-
Notifications
You must be signed in to change notification settings - Fork 215
/
MetadataFieldsWildcardResolver.java
397 lines (359 loc) · 20.2 KB
/
MetadataFieldsWildcardResolver.java
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
/*
* Copyright (c) 2022 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.ditto.things.service.persistence.actors.strategies.commands;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import javax.annotation.concurrent.Immutable;
import org.eclipse.ditto.base.model.json.FieldType;
import org.eclipse.ditto.base.model.signals.commands.Command;
import org.eclipse.ditto.json.JsonKey;
import org.eclipse.ditto.json.JsonObject;
import org.eclipse.ditto.json.JsonPointer;
import org.eclipse.ditto.json.JsonValue;
import org.eclipse.ditto.things.model.Feature;
import org.eclipse.ditto.things.model.FeatureProperties;
import org.eclipse.ditto.things.model.Thing;
import org.eclipse.ditto.things.model.signals.commands.query.RetrieveAttributes;
import org.eclipse.ditto.things.model.signals.commands.query.RetrieveFeature;
import org.eclipse.ditto.things.model.signals.commands.query.RetrieveFeatureDesiredProperties;
import org.eclipse.ditto.things.model.signals.commands.query.RetrieveFeatureProperties;
import org.eclipse.ditto.things.model.signals.commands.query.RetrieveFeatures;
import org.eclipse.ditto.things.model.signals.commands.query.RetrieveThing;
@Immutable
final class MetadataFieldsWildcardResolver {
private static final JsonPointer FEATURES_POINTER = JsonPointer.of("features");
private static final JsonPointer PROPERTIES_POINTER = JsonPointer.of("properties");
private MetadataFieldsWildcardResolver() {
throw new AssertionError();
}
/**
* Resolves the wildcards inside the {@code jsonPointerWithWildcard}.
*
* @param command the command used to determine wildcard replacement.
* @param thing the thing used to get ids for replacement.
* @param jsonPointerWithWildcard pointer which contains the wildcards to resolve.
* @return a set of pointers with replaced wildcards.
*/
public static Set<JsonPointer> resolve(final Command<?> command, final Thing thing,
final JsonPointer jsonPointerWithWildcard) {
return switch (command.getType()) {
case RetrieveThing.TYPE -> replaceWildcardForRetrieveThing(thing, jsonPointerWithWildcard);
case RetrieveFeatures.TYPE -> replaceWildcardForRetrieveFeatures(thing, jsonPointerWithWildcard);
case RetrieveFeature.TYPE ->
replaceWildcardForRetrieveFeature(thing, jsonPointerWithWildcard, (RetrieveFeature) command);
case RetrieveFeatureProperties.TYPE ->
replaceWildcardForRetrieveFeatureProperties(thing, jsonPointerWithWildcard,
(RetrieveFeatureProperties) command);
case RetrieveFeatureDesiredProperties.TYPE ->
replaceWildcardForRetrieveFeatureDesiredProperties(thing, jsonPointerWithWildcard,
(RetrieveFeatureDesiredProperties) command);
case RetrieveAttributes.TYPE -> replaceWildcardForRetrieveAttributes(thing, jsonPointerWithWildcard);
default -> Set.of();
};
}
private static Set<JsonPointer> replaceWildcardForRetrieveThing(final Thing thing,
final JsonPointer jsonPointerWithWildcard) {
final String wildcardExpression = jsonPointerWithWildcard.toString();
final JsonKey leafFromWildcardExpression = jsonPointerWithWildcard.getLeaf().orElseThrow(() ->
MetadataWildcardValidator.getDittoHeaderInvalidException(wildcardExpression)
);
final Set<JsonPointer> replacedWildcardsPointers = new HashSet<>();
final List<String> featureIds = getFeatureIdsFromThing(thing);
if (MetadataWildcardValidator.matchesThingFeaturesAndPropertiesWildcard(wildcardExpression)) {
featureIds.forEach(featureId -> {
final List<String> propertyKeys = getFeaturePropertyKeysFromThing(thing, featureId);
propertyKeys.forEach(propertyKey ->
replacedWildcardsPointers.add(
JsonPointer.empty().append(FEATURES_POINTER)
.append(JsonPointer.of(featureId))
.append(PROPERTIES_POINTER)
.append(JsonPointer.of(propertyKey))
.append(leafFromWildcardExpression.asPointer())
)
);
}
);
} else if (MetadataWildcardValidator.matchesThingFeaturesWithIdOnlyWildcard(wildcardExpression)) {
final JsonKey propertyKeysFromWildcardExpr = jsonPointerWithWildcard.get(3).orElseThrow(() ->
MetadataWildcardValidator.getDittoHeaderInvalidException(wildcardExpression)
);
featureIds.forEach(featureId ->
replacedWildcardsPointers.add(
JsonPointer.empty()
.append(FEATURES_POINTER)
.append(JsonPointer.of(featureId))
.append(PROPERTIES_POINTER)
.append(propertyKeysFromWildcardExpr.asPointer())
.append(leafFromWildcardExpression.asPointer())
)
);
} else if (MetadataWildcardValidator.matchesThingFeaturesWithPropertiesOnlyWildcard(wildcardExpression)) {
final JsonKey featureIdKeyFromWildcardExpr = jsonPointerWithWildcard.get(1).orElseThrow(() ->
MetadataWildcardValidator.getDittoHeaderInvalidException(wildcardExpression)
);
final List<String> propertyKeys =
getFeaturePropertyKeysFromThing(thing, featureIdKeyFromWildcardExpr.toString());
propertyKeys.forEach(propertyKey ->
replacedWildcardsPointers.add(
JsonPointer.empty()
.append(FEATURES_POINTER)
.append(featureIdKeyFromWildcardExpr.asPointer())
.append(PROPERTIES_POINTER)
.append(JsonPointer.of(propertyKey))
.append(leafFromWildcardExpression.asPointer()
)
)
);
} else if (MetadataWildcardValidator.matchesAttributesWildcard(wildcardExpression)) {
final JsonKey metadataKey = jsonPointerWithWildcard.get(2).orElseThrow(() ->
MetadataWildcardValidator.getDittoHeaderInvalidException(wildcardExpression)
);
final List<String> attributeIds = getAttributesIdsFromThing(thing);
attributeIds.forEach(attributeId ->
replacedWildcardsPointers.add(
JsonPointer.empty()
.append(JsonPointer.of("attributes"))
.append(JsonPointer.of(attributeId))
.append(metadataKey.asPointer())
)
);
} else if (MetadataWildcardValidator.matchesLeafWildcard(wildcardExpression)) {
final JsonKey metadataKey = jsonPointerWithWildcard.get(1).orElseThrow(() ->
MetadataWildcardValidator.getDittoHeaderInvalidException(wildcardExpression)
);
replacedWildcardsPointers.add(JsonPointer.of("thingId/" + metadataKey));
replacedWildcardsPointers.add(JsonPointer.of("policyId/" + metadataKey));
featureIds.forEach(featureId -> {
final List<String> propertyLeafs = getFeaturePropertyLeafsFromThing(thing, featureId);
propertyLeafs.forEach(propertyKey ->
replacedWildcardsPointers.add(
JsonPointer.empty().append(FEATURES_POINTER)
.append(JsonPointer.of(featureId))
.append(PROPERTIES_POINTER)
.append(JsonPointer.of(propertyKey))
.append(metadataKey.asPointer())
)
);
}
);
final List<String> attributeIds = getAttributesIdsFromThing(thing);
attributeIds.forEach(attributeId ->
replacedWildcardsPointers.add(
JsonPointer.empty()
.append(JsonPointer.of("attributes"))
.append(JsonPointer.of(attributeId))
.append(metadataKey.asPointer())
)
);
}
return replacedWildcardsPointers;
}
private static Set<JsonPointer> replaceWildcardForRetrieveFeatures(final Thing thing,
final JsonPointer jsonPointerWithWildcard) {
final Set<JsonPointer> replacedWildcardsPointer = new HashSet<>();
final List<String> featureIds = getFeatureIdsFromThing(thing);
final String wildcardExpression = jsonPointerWithWildcard.toString();
final JsonKey leafFromWildcardExpression = jsonPointerWithWildcard.getLeaf().orElseThrow(() ->
MetadataWildcardValidator.getDittoHeaderInvalidException(wildcardExpression)
);
if (MetadataWildcardValidator.matchesFeaturesWildcard(wildcardExpression)) {
featureIds.forEach(featureId -> {
final List<String> propertyKeys = getFeaturePropertyKeysFromThing(thing, featureId);
propertyKeys.forEach(propertyKey ->
replacedWildcardsPointer.add(
JsonPointer.empty()
.append(JsonPointer.of(featureId))
.append(PROPERTIES_POINTER)
.append(JsonPointer.of(propertyKey))
.append(leafFromWildcardExpression.asPointer())
)
);
}
);
} else if (MetadataWildcardValidator.matchesFeaturesWithIdOnlyWildcard(wildcardExpression)) {
final JsonKey propertyKeyFromWildcardExpr = jsonPointerWithWildcard.get(2).orElseThrow(() ->
MetadataWildcardValidator.getDittoHeaderInvalidException(wildcardExpression)
);
featureIds.forEach(featureId ->
replacedWildcardsPointer.add(
JsonPointer.empty()
.append(JsonPointer.of(featureId))
.append(PROPERTIES_POINTER)
.append(propertyKeyFromWildcardExpr.asPointer())
.append(leafFromWildcardExpression.asPointer())
)
);
} else if (MetadataWildcardValidator.matchesFeaturesWithPropertyOnlyWildcard(wildcardExpression) ||
MetadataWildcardValidator.matchesFeaturePropertyWildcard(wildcardExpression)) {
final JsonKey featureIdFromWildcardExpr = jsonPointerWithWildcard.get(1).orElseThrow(() ->
MetadataWildcardValidator.getDittoHeaderInvalidException(wildcardExpression)
);
final List<String> propertyKeys =
getFeaturePropertyKeysFromThing(thing, featureIdFromWildcardExpr.toString());
propertyKeys.forEach(propertyKey ->
replacedWildcardsPointer.add(
JsonPointer.empty()
.append(featureIdFromWildcardExpr.asPointer())
.append(PROPERTIES_POINTER)
.append(JsonPointer.of(propertyKey))
.append(leafFromWildcardExpression.asPointer())
)
);
}
return replacedWildcardsPointer;
}
private static Set<JsonPointer> replaceWildcardForRetrieveFeature(final Thing thing,
final JsonPointer jsonPointerWithWildcard, final RetrieveFeature retrieveFeature) {
final Set<JsonPointer> replacedWildcardsPointer = new HashSet<>();
final String wildcardExpression = jsonPointerWithWildcard.toString();
final JsonKey leafFromWildcardExpression = jsonPointerWithWildcard.getLeaf().orElseThrow(() ->
MetadataWildcardValidator.getDittoHeaderInvalidException(wildcardExpression)
);
if (MetadataWildcardValidator.matchesFeaturePropertyWildcard(wildcardExpression)) {
final String featureIdFromCommand = retrieveFeature.getFeatureId();
final List<String> propertyKeys = getFeaturePropertyKeysFromThing(thing, featureIdFromCommand);
propertyKeys.forEach(propertyKey ->
replacedWildcardsPointer.add(
JsonPointer.empty()
.append(PROPERTIES_POINTER)
.append(JsonPointer.of(propertyKey))
.append(leafFromWildcardExpression.asPointer())
)
);
}
return replacedWildcardsPointer;
}
private static Set<JsonPointer> replaceWildcardForRetrieveFeatureProperties(final Thing thing,
final JsonPointer jsonPointerWithWildcard, final RetrieveFeatureProperties command) {
final String wildcardExpression = jsonPointerWithWildcard.toString();
final JsonKey leafFromWildcardExpression = jsonPointerWithWildcard.getLeaf().orElseThrow(() ->
MetadataWildcardValidator.getDittoHeaderInvalidException(wildcardExpression)
);
final Set<JsonPointer> replacedWildcardsPointer = new HashSet<>();
final String featureIdFromCommand = command.getFeatureId();
final List<String> propertyKeys = getFeaturePropertyKeysFromThing(thing, featureIdFromCommand);
propertyKeys.forEach(propertyKey ->
replacedWildcardsPointer.add(
JsonPointer.empty()
.append(JsonPointer.of(propertyKey))
.append(leafFromWildcardExpression.asPointer())
)
);
return replacedWildcardsPointer;
}
private static Set<JsonPointer> replaceWildcardForRetrieveFeatureDesiredProperties(final Thing thing,
final JsonPointer jsonPointerWithWildcard, final RetrieveFeatureDesiredProperties command) {
final String wildcardExpression = jsonPointerWithWildcard.toString();
final JsonKey leafFromWildcardExpression = jsonPointerWithWildcard.getLeaf().orElseThrow(() ->
MetadataWildcardValidator.getDittoHeaderInvalidException(wildcardExpression)
);
final Set<JsonPointer> replacedWildcardsPointer = new HashSet<>();
final String featureIdFromCommand = command.getFeatureId();
final List<String> propertyKeys = getFeatureDesiredPropertyKeysFromThing(thing, featureIdFromCommand);
propertyKeys.forEach(propertyKey ->
replacedWildcardsPointer.add(
JsonPointer.empty()
.append(JsonPointer.of(propertyKey))
.append(leafFromWildcardExpression.asPointer())
)
);
return replacedWildcardsPointer;
}
private static Set<JsonPointer> replaceWildcardForRetrieveAttributes(final Thing thing,
final JsonPointer jsonPointerWithWildcard) {
final String wildcardExpression = jsonPointerWithWildcard.toString();
final Set<JsonPointer> replacedWildcardsPointer = new HashSet<>();
final List<String> attributeIds = getAttributesIdsFromThing(thing);
if (MetadataWildcardValidator.matchesLeafWildcard(wildcardExpression)) {
final JsonKey attributeKey = jsonPointerWithWildcard.get(1).orElseThrow(() ->
MetadataWildcardValidator.getDittoHeaderInvalidException(wildcardExpression)
);
attributeIds.forEach(attributeId ->
replacedWildcardsPointer.add(
JsonPointer.empty()
.append(JsonPointer.of(attributeId))
.append(attributeKey.asPointer())
)
);
}
return replacedWildcardsPointer;
}
private static List<String> getFeatureIdsFromThing(final Thing thing) {
return thing.getFeatures()
.map(features -> features.stream().map(Feature::getId).toList())
.orElseGet(List::of);
}
private static List<String> getFeaturePropertyKeysFromThing(final Thing thing, final String featureId) {
return thing.getFeatures()
.flatMap(features -> features.getFeature(featureId))
.flatMap(Feature::getProperties)
.map(JsonObject::getKeys)
.map(jsonKeys -> jsonKeys.stream().map(Objects::toString).toList())
.orElse(List.of());
}
private static List<String> getFeaturePropertyLeafsFromThing(final Thing thing, final String featureId) {
final List<JsonPointer> propertyKeysFromThing = thing.getFeatures()
.flatMap(features -> features.getFeature(featureId))
.flatMap(Feature::getProperties)
.map(JsonObject::getKeys)
.map(jsonKeys -> jsonKeys.stream().map(JsonPointer::of).toList())
.orElse(List.of());
final FeatureProperties featureProperties = thing.getFeatures()
.flatMap(features -> features.getFeature(featureId))
.flatMap(Feature::getProperties)
.orElse(null);
if (featureProperties != null) {
return getFeaturePropertyLeafs(propertyKeysFromThing, featureProperties);
} else {
return List.of();
}
}
private static List<String> getFeaturePropertyLeafs(List<JsonPointer> propertyKeysFromThing,
final FeatureProperties featureProperties) {
final List<String> featurePropertyLeafs = new ArrayList<>();
propertyKeysFromThing.forEach(jsonPointer ->
getLeafsFromObject(JsonPointer.empty(), featureProperties.get(jsonPointer), featurePropertyLeafs)
);
return featurePropertyLeafs;
}
private static void getLeafsFromObject(final JsonPointer path, final JsonValue entity, final List<String> leafs) {
if (entity.isObject()) {
final JsonObject jsonObject = entity.asObject();
jsonObject.stream()
.filter(field -> !(field.isMarkedAs(FieldType.SPECIAL) || field.isMarkedAs(FieldType.HIDDEN)))
.forEach(jsonField -> {
final JsonKey key = jsonField.getKey();
getLeafsFromObject(path.append(key.asPointer()), jsonField.getValue(), leafs);
});
} else {
leafs.add(path.toString());
}
}
private static List<String> getFeatureDesiredPropertyKeysFromThing(final Thing thing, final String featureId) {
return thing.getFeatures()
.flatMap(features -> features.getFeature(featureId))
.flatMap(Feature::getDesiredProperties)
.map(JsonObject::getKeys)
.map(jsonKeys -> jsonKeys.stream().map(Objects::toString).toList())
.orElse(List.of());
}
private static List<String> getAttributesIdsFromThing(final Thing thing) {
return thing.getAttributes()
.map(JsonObject::getKeys)
.map(jsonKeys -> jsonKeys.stream().map(Objects::toString).toList())
.orElse(List.of());
}
}