30
30
import java .util .Map ;
31
31
import java .util .Objects ;
32
32
import java .util .Optional ;
33
+ import java .util .Set ;
33
34
import java .util .concurrent .ConcurrentHashMap ;
34
35
import java .util .concurrent .CopyOnWriteArrayList ;
35
36
import java .util .stream .Collectors ;
40
41
import org .openhab .core .model .yaml .YamlElementName ;
41
42
import org .openhab .core .model .yaml .YamlModelListener ;
42
43
import org .openhab .core .model .yaml .YamlModelRepository ;
44
+ import org .openhab .core .model .yaml .internal .semantics .YamlSemanticTagDTO ;
45
+ import org .openhab .core .model .yaml .internal .things .YamlThingDTO ;
43
46
import org .openhab .core .service .WatchService ;
44
47
import org .openhab .core .service .WatchService .Kind ;
45
48
import org .osgi .service .component .annotations .Activate ;
@@ -83,6 +86,10 @@ public class YamlModelRepositoryImpl implements WatchService.WatchEventListener,
83
86
private static final int DEFAULT_MODEL_VERSION = 2 ;
84
87
private static final String VERSION = "version" ;
85
88
private static final String READ_ONLY = "readOnly" ;
89
+ private static final Set <String > KNOWN_ELEMENTS = Set .of ( //
90
+ getElementName (YamlSemanticTagDTO .class ), // "tags"
91
+ getElementName (YamlThingDTO .class ) // "things"
92
+ );
86
93
87
94
private final Logger logger = LoggerFactory .getLogger (YamlModelRepositoryImpl .class );
88
95
@@ -130,7 +137,7 @@ public FileVisitResult visitFile(@NonNullByDefault({}) Path file,
130
137
@ Override
131
138
public FileVisitResult visitFileFailed (@ NonNullByDefault ({}) Path file ,
132
139
@ NonNullByDefault ({}) IOException exc ) throws IOException {
133
- logger .warn ("Failed to process {}: {}" , file .toAbsolutePath (), exc .getClass (). getSimpleName ());
140
+ logger .warn ("Failed to process {}: {}" , file .toAbsolutePath (), exc .getMessage ());
134
141
return FileVisitResult .CONTINUE ;
135
142
}
136
143
});
@@ -223,8 +230,7 @@ public synchronized void processWatchEvent(Kind kind, Path path) {
223
230
List <JsonNode > oldNodeV1Elements = model .getNodesV1 ().getOrDefault (elementName , List .of ());
224
231
JsonNode oldNodeElements = model .getNodes ().get (elementName );
225
232
226
- List <YamlModelListener <?>> elementListeners = getElementListeners (elementName , modelVersion );
227
- for (YamlModelListener <?> elementListener : elementListeners ) {
233
+ for (YamlModelListener <?> elementListener : getElementListeners (elementName , modelVersion )) {
228
234
Class <? extends YamlElement > elementClass = elementListener .getElementClass ();
229
235
230
236
List <String > errors = new ArrayList <>();
@@ -274,10 +280,6 @@ public synchronized void processWatchEvent(Kind kind, Path path) {
274
280
} else {
275
281
model .getNodes ().put (elementName , newNodeElements );
276
282
}
277
-
278
- if (elementListeners .isEmpty ()) {
279
- logger .warn ("Element '{}' in model {} is unknown." , elementName , modelName );
280
- }
281
283
}
282
284
283
285
// remove removed elements
@@ -302,6 +304,8 @@ public synchronized void processWatchEvent(Kind kind, Path path) {
302
304
});
303
305
});
304
306
}
307
+
308
+ checkElementNames (modelName , model );
305
309
} else {
306
310
logger .trace ("Ignored {}" , fullPath );
307
311
}
@@ -347,21 +351,20 @@ public void addYamlModelListener(YamlModelListener<? extends YamlElement> listen
347
351
elementListeners .computeIfAbsent (elementName , k -> new CopyOnWriteArrayList <>()).add (listener );
348
352
349
353
// iterate over all models and notify the new listener of already existing models with this type
350
- for (Map .Entry <String , YamlModelWrapper > model : modelCache .entrySet ()) {
351
- String modelName = model .getKey ();
352
- int modelVersion = model .getValue ().getVersion ();
354
+ modelCache .forEach ((modelName , model ) -> {
355
+ int modelVersion = model .getVersion ();
353
356
if (!listener .isVersionSupported (modelVersion )) {
354
- continue ;
357
+ return ;
355
358
}
356
359
if (listener .isDeprecated ()) {
357
360
logger .warn (
358
361
"Element {} in model {} version {} is still supported but deprecated, please consider migrating your model to a more recent version" ,
359
362
elementName , modelName , modelVersion );
360
363
}
361
- List <JsonNode > modelNodes = model .getValue (). getNodesV1 ().getOrDefault (elementName , List .of ());
362
- JsonNode modelMapNode = model .getValue (). getNodes ().get (elementName );
364
+ List <JsonNode > modelNodes = model .getNodesV1 ().getOrDefault (elementName , List .of ());
365
+ JsonNode modelMapNode = model .getNodes ().get (elementName );
363
366
if (modelNodes .isEmpty () && modelMapNode == null ) {
364
- continue ;
367
+ return ;
365
368
}
366
369
List <String > errors = new ArrayList <>();
367
370
List <String > warnings = new ArrayList <>();
@@ -373,7 +376,8 @@ public void addYamlModelListener(YamlModelListener<? extends YamlElement> listen
373
376
logger .info ("YAML model {}: {}" , modelName , warning );
374
377
});
375
378
listener .addedModel (modelName , modelElements );
376
- }
379
+ checkElementNames (modelName , model );
380
+ });
377
381
}
378
382
379
383
public void removeYamlModelListener (YamlModelListener <? extends YamlElement > listener ) {
@@ -384,7 +388,18 @@ public void removeYamlModelListener(YamlModelListener<? extends YamlElement> lis
384
388
});
385
389
}
386
390
387
- private String getElementName (Class <? extends YamlElement > elementClass ) {
391
+ private void checkElementNames (String modelName , YamlModelWrapper model ) {
392
+ Set <String > elementListenerNames = elementListeners .keySet ();
393
+ if (elementListenerNames .containsAll (KNOWN_ELEMENTS )) {
394
+ Set <String > modelElementNames = model .getVersion () == 1 ? model .getNodesV1 ().keySet ()
395
+ : model .getNodes ().keySet ();
396
+ modelElementNames .stream ().filter (e -> !KNOWN_ELEMENTS .contains (e )).forEach (unknownElement -> {
397
+ logger .warn ("Element '{}' in model {} is unknown." , unknownElement , modelName );
398
+ });
399
+ }
400
+ }
401
+
402
+ private static String getElementName (Class <? extends YamlElement > elementClass ) {
388
403
YamlElementName annotation = elementClass .getAnnotation (YamlElementName .class );
389
404
if (annotation == null ) {
390
405
throw new IllegalStateException ("Class " + elementClass .getName ()
0 commit comments