Skip to content

Commit b72ef99

Browse files
committed
Make isIsolatedModel a static method in DSL model
Signed-off-by: Laurent Garnier <lg.hc@free.fr>
1 parent 4bf5cdf commit b72ef99

10 files changed

Lines changed: 65 additions & 68 deletions

File tree

bundles/org.openhab.core.model.core/src/main/java/org/openhab/core/model/core/ModelRepository.java

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,7 @@
2828
* come from.
2929
*
3030
* @author Kai Kreuzer - Initial contribution
31-
* @author Laurent Garnier - Added method generateFileFormat
32-
* @author Laurent Garnier - Added methods createIsolatedModel and isIsolatedModel
31+
* @author Laurent Garnier - Added methods generateFileFormat and createIsolatedModel
3332
*/
3433
@NonNullByDefault
3534
public interface ModelRepository {
@@ -111,16 +110,6 @@ public interface ModelRepository {
111110
@Nullable
112111
String createIsolatedModel(String modelType, InputStream inputStream, List<String> errors, List<String> warnings);
113112

114-
/**
115-
* Indicates if a model is an isolated model
116-
*
117-
* An isolated model is a temporary model loaded without impacting any object registry.
118-
*
119-
* @param modelName the model name
120-
* @return true if the model identified by the provided name is an isolated model, false otherwise
121-
*/
122-
boolean isIsolatedModel(String modelName);
123-
124113
/**
125114
* Generate the DSL file format from a provided model type and model content.
126115
*

bundles/org.openhab.core.model.core/src/main/java/org/openhab/core/model/core/internal/ModelRepositoryImpl.java

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,8 @@
5050
* @author Kai Kreuzer - Initial contribution
5151
* @author Oliver Libutzki - Added reloadAllModelsOfType method
5252
* @author Simon Kaufmann - added validation of models before loading them
53-
* @author Laurent Garnier - Added method generateFileFormat
54-
* @author Laurent Garnier - Added methods createIsolatedModel and isIsolatedModel + return errors and warnings
55-
* when loading a model
53+
* @author Laurent Garnier - Added methods generateFileFormat, createIsolatedModel and isIsolatedModel
54+
* + return errors and warnings when loading a model
5655
*/
5756
@Component(immediate = true)
5857
@NonNullByDefault
@@ -265,8 +264,15 @@ public void removeModelRepositoryChangeListener(ModelRepositoryChangeListener li
265264
return addOrRefreshModel(name, inputStream, errors, warnings) ? name : null;
266265
}
267266

268-
@Override
269-
public boolean isIsolatedModel(String modelName) {
267+
/**
268+
* Indicates if a model is an isolated model
269+
*
270+
* An isolated model is a temporary model loaded without impacting any object registry.
271+
*
272+
* @param modelName the model name
273+
* @return true if the model identified by the provided name is an isolated model, false otherwise
274+
*/
275+
public static boolean isIsolatedModel(String modelName) {
270276
return modelName.startsWith(PREFIX_TMP_MODEL);
271277
}
272278

bundles/org.openhab.core.model.item/src/org/openhab/core/model/item/internal/GenericItemProvider.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
*/
1313
package org.openhab.core.model.item.internal;
1414

15+
import static org.openhab.core.model.core.internal.ModelRepositoryImpl.isIsolatedModel;
16+
1517
import java.util.ArrayList;
1618
import java.util.Arrays;
1719
import java.util.Collection;
@@ -267,7 +269,7 @@ private void processBindingConfigsFromModel(String modelName, EventType type) {
267269
Map<String, String> formatters = Objects
268270
.requireNonNull(stateFormattersMap.computeIfAbsent(modelName, k -> new HashMap<>()));
269271
formatters.put(modelItem.getName(), format);
270-
if (!modelRepository.isIsolatedModel(modelName)) {
272+
if (!isIsolatedModel(modelName)) {
271273
stateDescriptionFragments.put(modelItem.getName(),
272274
StateDescriptionFragmentBuilder.create().withPattern(format).build());
273275
}
@@ -279,7 +281,7 @@ private void processBindingConfigsFromModel(String modelName, EventType type) {
279281
stateFormattersMap.remove(modelName);
280282
}
281283
}
282-
if (!modelRepository.isIsolatedModel(modelName)) {
284+
if (!isIsolatedModel(modelName)) {
283285
stateDescriptionFragments.remove(modelItem.getName());
284286
}
285287
}
@@ -414,8 +416,8 @@ private void internalDispatchBindings(@Nullable BindingConfigReader reader, Stri
414416
bindingType, item.getName(), e);
415417
}
416418
} else {
417-
genericMetaDataProvider.addMetadata(modelName, modelRepository.isIsolatedModel(modelName), bindingType,
418-
item.getName(), config, configuration.getProperties());
419+
genericMetaDataProvider.addMetadata(modelName, bindingType, item.getName(), config,
420+
configuration.getProperties());
419421
}
420422
}
421423
}
@@ -429,7 +431,7 @@ public void modelChanged(String modelName, EventType type) {
429431
Map<String, Item> oldItems = toItemMap(itemsMap.get(modelName));
430432
Map<String, Item> newItems = toItemMap(getItemsFromModel(modelName));
431433
itemsMap.put(modelName, newItems.values());
432-
if (!modelRepository.isIsolatedModel(modelName)) {
434+
if (!isIsolatedModel(modelName)) {
433435
for (Item newItem : newItems.values()) {
434436
Item oldItem = oldItems.get(newItem.getName());
435437
if (oldItem != null) {
@@ -462,7 +464,7 @@ public void modelChanged(String modelName, EventType type) {
462464
}
463465

464466
private void notifyAndCleanup(String modelName, Item oldItem) {
465-
if (!modelRepository.isIsolatedModel(modelName)) {
467+
if (!isIsolatedModel(modelName)) {
466468
notifyListenersAboutRemovedElement(oldItem);
467469
this.stateDescriptionFragments.remove(oldItem.getName());
468470
}

bundles/org.openhab.core.model.item/src/org/openhab/core/model/item/internal/GenericMetadataProvider.java

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
package org.openhab.core.model.item.internal;
1414

1515
import static java.util.stream.Collectors.toSet;
16+
import static org.openhab.core.model.core.internal.ModelRepositoryImpl.isIsolatedModel;
1617

1718
import java.util.Collection;
1819
import java.util.HashMap;
@@ -48,32 +49,27 @@ public class GenericMetadataProvider extends AbstractProvider<Metadata> implemen
4849

4950
private final Map<String, Set<Metadata>> metadata = new HashMap<>();
5051
private final ReadWriteLock lock = new ReentrantReadWriteLock(true);
51-
private final Set<String> isolatedModels = new HashSet<>();
5252

5353
/**
5454
* Adds metadata to this provider
5555
*
5656
* @param modelName the model name
57-
* @param isolated whether the model is an isolated model
5857
* @param bindingType
5958
* @param itemName
6059
* @param configuration
6160
*/
62-
public void addMetadata(String modelName, boolean isolated, String bindingType, String itemName, String value,
61+
public void addMetadata(String modelName, String bindingType, String itemName, String value,
6362
@Nullable Map<String, Object> configuration) {
6463
MetadataKey key = new MetadataKey(bindingType, itemName);
6564
Metadata md = new Metadata(key, value, configuration);
6665
try {
6766
lock.writeLock().lock();
6867
Set<Metadata> mdSet = Objects.requireNonNull(metadata.computeIfAbsent(modelName, k -> new HashSet<>()));
6968
mdSet.add(md);
70-
if (isolated) {
71-
isolatedModels.add(modelName);
72-
}
7369
} finally {
7470
lock.writeLock().unlock();
7571
}
76-
if (!isolated) {
72+
if (!isIsolatedModel(modelName)) {
7773
notifyListenersAboutAddedElement(md);
7874
}
7975
}
@@ -90,16 +86,14 @@ public void removeMetadataByNamespace(String namespace) {
9086
toBeNotified = new HashMap<>();
9187
for (Map.Entry<String, Set<Metadata>> entry : metadata.entrySet()) {
9288
String modelName = entry.getKey();
93-
boolean notify = !isolatedModels.contains(modelName);
9489
Set<Metadata> mdSet = entry.getValue();
9590
Set<Metadata> toBeRemoved = mdSet.stream().filter(MetadataPredicates.hasNamespace(namespace))
9691
.collect(toSet());
9792
mdSet.removeAll(toBeRemoved);
9893
if (mdSet.isEmpty()) {
9994
metadata.remove(modelName);
100-
isolatedModels.remove(modelName);
10195
}
102-
if (notify && !toBeRemoved.isEmpty()) {
96+
if (!isIsolatedModel(modelName) && !toBeRemoved.isEmpty()) {
10397
toBeNotified.put(modelName, toBeRemoved);
10498
}
10599
}
@@ -122,15 +116,13 @@ public void removeMetadataByItemName(String modelName, String itemName) {
122116
try {
123117
lock.writeLock().lock();
124118
toBeNotified = new HashSet<>();
125-
boolean notify = !isolatedModels.contains(modelName);
126119
Set<Metadata> mdSet = metadata.getOrDefault(modelName, new HashSet<>());
127120
Set<Metadata> toBeRemoved = mdSet.stream().filter(MetadataPredicates.ofItem(itemName)).collect(toSet());
128121
mdSet.removeAll(toBeRemoved);
129122
if (mdSet.isEmpty()) {
130123
metadata.remove(modelName);
131-
isolatedModels.remove(modelName);
132124
}
133-
if (notify && !toBeRemoved.isEmpty()) {
125+
if (!isIsolatedModel(modelName) && !toBeRemoved.isEmpty()) {
134126
toBeNotified.addAll(toBeRemoved);
135127
}
136128
} finally {
@@ -144,7 +136,7 @@ public Collection<Metadata> getAll() {
144136
try {
145137
lock.readLock().lock();
146138
// Ignore isolated models
147-
Set<Metadata> set = metadata.keySet().stream().filter(name -> !isolatedModels.contains(name))
139+
Set<Metadata> set = metadata.keySet().stream().filter(name -> !isIsolatedModel(name))
148140
.map(name -> metadata.getOrDefault(name, Set.of())).flatMap(s -> s.stream()).collect(toSet());
149141
return Set.copyOf(set);
150142
} finally {

bundles/org.openhab.core.model.thing/src/org/openhab/core/model/thing/internal/GenericItemChannelLinkProvider.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
*/
1313
package org.openhab.core.model.thing.internal;
1414

15+
import static org.openhab.core.model.core.internal.ModelRepositoryImpl.isIsolatedModel;
16+
1517
import java.util.Collection;
1618
import java.util.HashMap;
1719
import java.util.HashSet;
@@ -49,6 +51,7 @@ public class GenericItemChannelLinkProvider extends AbstractProvider<ItemChannel
4951
implements BindingConfigReader, ItemChannelLinkProvider {
5052

5153
private final Logger logger = LoggerFactory.getLogger(GenericItemChannelLinkProvider.class);
54+
5255
/** caches binding configurations. maps context to a map mapping itemNames to {@link ItemChannelLink}s */
5356
protected Map<String, Map<String, Map<ChannelUID, ItemChannelLink>>> itemChannelLinkMap = new ConcurrentHashMap<>();
5457

@@ -189,6 +192,6 @@ public Collection<ItemChannelLink> getAllFromContext(String context) {
189192

190193
private boolean isValidContextForListeners(String context) {
191194
// Ignore isolated models
192-
return !context.startsWith("tmp_");
195+
return !isIsolatedModel(context);
193196
}
194197
}

bundles/org.openhab.core.model.thing/src/org/openhab/core/model/thing/internal/GenericThingProvider.xtend

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
*/
1313
package org.openhab.core.model.thing.internal
1414

15+
import static org.openhab.core.model.core.internal.ModelRepositoryImpl.isIsolatedModel
16+
1517
import java.util.ArrayList
1618
import java.util.Collection
1719
import java.util.HashSet
@@ -110,7 +112,7 @@ class GenericThingProvider extends AbstractProviderLazyNullness<Thing> implement
110112

111113
override Collection<Thing> getAll() {
112114
val things = new ArrayList
113-
thingsMap.keySet.filter[!modelRepository.isIsolatedModel(it)].forEach([
115+
thingsMap.keySet.filter[!isIsolatedModel(it)].forEach([
114116
val things2 = thingsMap.get(it)
115117
if (things2 !== null) {
116118
things.addAll(things2)
@@ -482,7 +484,7 @@ class GenericThingProvider extends AbstractProviderLazyNullness<Thing> implement
482484
val removedThings = oldThings.filter[!newThingUIDs.contains(it.UID)]
483485
removedThings.forEach [
484486
logger.debug("Removing thing '{}' from model '{}'.", it.UID, modelName)
485-
if (!modelRepository.isIsolatedModel(modelName)) {
487+
if (!isIsolatedModel(modelName)) {
486488
notifyListenersAboutRemovedElement
487489
}
488490
]
@@ -493,7 +495,7 @@ class GenericThingProvider extends AbstractProviderLazyNullness<Thing> implement
493495
case org.openhab.core.model.core.EventType.REMOVED: {
494496
logger.debug("Removing all things from model '{}'.", modelName)
495497
val things = thingsMap.remove(modelName) ?: newArrayList
496-
if (!modelRepository.isIsolatedModel(modelName)) {
498+
if (!isIsolatedModel(modelName)) {
497499
things.forEach [
498500
notifyListenersAboutRemovedElement
499501
]
@@ -637,14 +639,14 @@ class GenericThingProvider extends AbstractProviderLazyNullness<Thing> implement
637639
things.remove(oldThing)
638640
things.add(newThing)
639641
logger.debug("Updating thing '{}' from model '{}'.", newThing.UID, modelName);
640-
if (!modelRepository.isIsolatedModel(modelName)) {
642+
if (!isIsolatedModel(modelName)) {
641643
notifyListenersAboutUpdatedElement(oldThing, newThing)
642644
}
643645
}
644646
} else {
645647
things.add(newThing)
646648
logger.debug("Adding thing '{}' from model '{}'.", newThing.UID, modelName);
647-
if (!modelRepository.isIsolatedModel(modelName)) {
649+
if (!isIsolatedModel(modelName)) {
648650
newThing.notifyListenersAboutAddedElement
649651
}
650652
}
@@ -678,7 +680,7 @@ class GenericThingProvider extends AbstractProviderLazyNullness<Thing> implement
678680
thingsMap.get(modelName).remove(oldThing)
679681
thingsMap.get(modelName).add(newThing)
680682
logger.debug("Refreshing thing '{}' after successful retry", newThing.UID)
681-
if (!ThingHelper.equals(oldThing, newThing) && !modelRepository.isIsolatedModel(modelName)) {
683+
if (!ThingHelper.equals(oldThing, newThing) && !isIsolatedModel(modelName)) {
682684
notifyListenersAboutUpdatedElement(oldThing, newThing)
683685
}
684686
} else {

bundles/org.openhab.core.model.yaml/src/main/java/org/openhab/core/model/yaml/internal/items/YamlChannelLinkProvider.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
*/
1313
package org.openhab.core.model.yaml.internal.items;
1414

15+
import static org.openhab.core.model.yaml.internal.YamlModelRepositoryImpl.isIsolatedModel;
16+
1517
import java.util.Collection;
1618
import java.util.HashSet;
1719
import java.util.Map;
@@ -23,7 +25,6 @@
2325
import org.openhab.core.common.registry.AbstractProvider;
2426
import org.openhab.core.config.core.Configuration;
2527
import org.openhab.core.items.ItemProvider;
26-
import org.openhab.core.model.yaml.internal.YamlModelRepositoryImpl;
2728
import org.openhab.core.model.yaml.internal.util.YamlElementUtils;
2829
import org.openhab.core.thing.ChannelUID;
2930
import org.openhab.core.thing.link.ItemChannelLink;
@@ -53,7 +54,7 @@ public class YamlChannelLinkProvider extends AbstractProvider<ItemChannelLink> i
5354
@Override
5455
public Collection<ItemChannelLink> getAll() {
5556
// Ignore isolated models
56-
return itemsChannelLinksMap.keySet().stream().filter(name -> !YamlModelRepositoryImpl.isIsolatedModel(name))
57+
return itemsChannelLinksMap.keySet().stream().filter(name -> !isIsolatedModel(name))
5758
.map(name -> itemsChannelLinksMap.getOrDefault(name, Map.of())).flatMap(m -> m.values().stream())
5859
.flatMap(m -> m.values().stream()).toList();
5960
}
@@ -103,14 +104,14 @@ public void updateItemChannelLinks(String modelName, String itemName, Map<String
103104
if (oldLink == null) {
104105
links.put(channelUIDObject, itemChannelLink);
105106
logger.debug("model {} added channel link {}", modelName, itemChannelLink.getUID());
106-
if (!YamlModelRepositoryImpl.isIsolatedModel(modelName)) {
107+
if (!isIsolatedModel(modelName)) {
107108
notifyListenersAboutAddedElement(itemChannelLink);
108109
}
109110
} else if (!YamlElementUtils.equalsConfig(configuration.getProperties(),
110111
oldLink.getConfiguration().getProperties())) {
111112
links.put(channelUIDObject, itemChannelLink);
112113
logger.debug("model {} updated channel link {}", modelName, itemChannelLink.getUID());
113-
if (!YamlModelRepositoryImpl.isIsolatedModel(modelName)) {
114+
if (!isIsolatedModel(modelName)) {
114115
notifyListenersAboutUpdatedElement(oldLink, itemChannelLink);
115116
}
116117
}
@@ -120,7 +121,7 @@ public void updateItemChannelLinks(String modelName, String itemName, Map<String
120121
ItemChannelLink link = links.remove(uid);
121122
if (link != null) {
122123
logger.debug("model {} removed channel link {}", modelName, link.getUID());
123-
if (!YamlModelRepositoryImpl.isIsolatedModel(modelName)) {
124+
if (!isIsolatedModel(modelName)) {
124125
notifyListenersAboutRemovedElement(link);
125126
}
126127
}

bundles/org.openhab.core.model.yaml/src/main/java/org/openhab/core/model/yaml/internal/items/YamlItemProvider.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
*/
1313
package org.openhab.core.model.yaml.internal.items;
1414

15+
import static org.openhab.core.model.yaml.internal.YamlModelRepositoryImpl.isIsolatedModel;
16+
1517
import java.util.ArrayList;
1618
import java.util.Collection;
1719
import java.util.HashMap;
@@ -33,7 +35,6 @@
3335
import org.openhab.core.items.dto.ItemDTOMapper;
3436
import org.openhab.core.library.CoreItemFactory;
3537
import org.openhab.core.model.yaml.YamlModelListener;
36-
import org.openhab.core.model.yaml.internal.YamlModelRepositoryImpl;
3738
import org.osgi.service.component.annotations.Activate;
3839
import org.osgi.service.component.annotations.Component;
3940
import org.osgi.service.component.annotations.Deactivate;
@@ -77,7 +78,7 @@ public void deactivate() {
7778
@Override
7879
public Collection<Item> getAll() {
7980
// Ignore isolated models
80-
return itemsMap.keySet().stream().filter(name -> !YamlModelRepositoryImpl.isIsolatedModel(name))
81+
return itemsMap.keySet().stream().filter(name -> !isIsolatedModel(name))
8182
.map(name -> itemsMap.getOrDefault(name, List.of())).flatMap(list -> list.stream()).toList();
8283
}
8384

@@ -117,7 +118,7 @@ public void addedModel(String modelName, Collection<YamlItemDTO> elements) {
117118
added.forEach((item, itemDTO) -> {
118119
String name = item.getName();
119120
logger.debug("model {} added item {}", modelName, name);
120-
if (!YamlModelRepositoryImpl.isIsolatedModel(modelName)) {
121+
if (!isIsolatedModel(modelName)) {
121122
notifyListenersAboutAddedElement(item);
122123
}
123124
processChannelLinks(modelName, name, itemDTO);
@@ -143,13 +144,13 @@ public void updatedModel(String modelName, Collection<YamlItemDTO> elements) {
143144
modelItems.remove(oldItem);
144145
modelItems.add(item);
145146
logger.debug("model {} updated item {}", modelName, name);
146-
if (!YamlModelRepositoryImpl.isIsolatedModel(modelName)) {
147+
if (!isIsolatedModel(modelName)) {
147148
notifyListenersAboutUpdatedElement(oldItem, item);
148149
}
149150
}, () -> {
150151
modelItems.add(item);
151152
logger.debug("model {} added item {}", modelName, name);
152-
if (!YamlModelRepositoryImpl.isIsolatedModel(modelName)) {
153+
if (!isIsolatedModel(modelName)) {
153154
notifyListenersAboutAddedElement(item);
154155
}
155156
});
@@ -168,7 +169,7 @@ public void removedModel(String modelName, Collection<YamlItemDTO> elements) {
168169
modelItems.stream().filter(i -> i.getName().equals(name)).findFirst().ifPresentOrElse(oldItem -> {
169170
modelItems.remove(oldItem);
170171
logger.debug("model {} removed item {}", modelName, name);
171-
if (!YamlModelRepositoryImpl.isIsolatedModel(modelName)) {
172+
if (!isIsolatedModel(modelName)) {
172173
notifyListenersAboutRemovedElement(oldItem);
173174
}
174175
}, () -> logger.debug("model {} item {} not found", modelName, name));

0 commit comments

Comments
 (0)