Skip to content

armgit / core

Public Imported from GitHub
0

100% open-source IoT Platform - Integrate your devices, add intelligence, build custom apps

Renamed AssetModel to AssetModelUtil and moved into model module and descriptors are injected at start up by the AssetModelService

Rich Turner9486aa8f02c4
diff --git a/agent/src/main/java/org/openremote/agent/protocol/AbstractProtocol.java b/agent/src/main/java/org/openremote/agent/protocol/AbstractProtocol.java
index 088aef8e8..1655b4a35 100644
--- a/agent/src/main/java/org/openremote/agent/protocol/AbstractProtocol.java
+++ b/agent/src/main/java/org/openremote/agent/protocol/AbstractProtocol.java
@@ -382,7 +382,7 @@ public abstract class AbstractProtocol implements Protocol {
}
// Do basic value conversion
- Optional<ValueType> attributeValueType = attribute.getType().map(AttributeValueType::getValueType);
+ Optional<ValueType> attributeValueType = attribute.getType().map(AttributeValueDescriptor::getValueType);
if (value != null && attributeValueType.isPresent()) {
if (attributeValueType.get() != value.getType()) {
diff --git a/agent/src/main/java/org/openremote/agent/protocol/controller/ControllerProtocol.java b/agent/src/main/java/org/openremote/agent/protocol/controller/ControllerProtocol.java
index 2dc1e4dbc..cc92f2253 100644
--- a/agent/src/main/java/org/openremote/agent/protocol/controller/ControllerProtocol.java
+++ b/agent/src/main/java/org/openremote/agent/protocol/controller/ControllerProtocol.java
@@ -517,7 +517,7 @@ public class ControllerProtocol extends AbstractProtocol {
*/
private void updateAttributeValue(AttributeRef attributeRef, String value) {
LOG.fine("### Updating attribute " + attributeRef + " with value " + value);
- AttributeValueType attributeType = this.linkedAttributes.get(attributeRef).getTypeOrThrow();
+ AttributeValueDescriptor attributeType = this.linkedAttributes.get(attributeRef).getTypeOrThrow();
ValueType valueType = attributeType.getValueType();
try {
diff --git a/agent/src/main/java/org/openremote/agent/protocol/macro/MacroProtocol.java b/agent/src/main/java/org/openremote/agent/protocol/macro/MacroProtocol.java
index cb8f6f0f1..43eaaafe6 100644
--- a/agent/src/main/java/org/openremote/agent/protocol/macro/MacroProtocol.java
+++ b/agent/src/main/java/org/openremote/agent/protocol/macro/MacroProtocol.java
@@ -243,7 +243,7 @@ public class MacroProtocol extends AbstractProtocol {
// Verify the type of the attribute matches the action value
if (attribute
.getType()
- .map(AttributeValueType::getValueType)
+ .map(AttributeValueDescriptor::getValueType)
.orElse(null) != actionValue.getType()) {
// Use a value of null so it is clear that the attribute isn't linked correctly
actionValue = null;
diff --git a/agent/src/main/java/org/openremote/agent/protocol/velbus/AbstractVelbusProtocol.java b/agent/src/main/java/org/openremote/agent/protocol/velbus/AbstractVelbusProtocol.java
index 7a0361047..769f210a8 100644
--- a/agent/src/main/java/org/openremote/agent/protocol/velbus/AbstractVelbusProtocol.java
+++ b/agent/src/main/java/org/openremote/agent/protocol/velbus/AbstractVelbusProtocol.java
@@ -317,7 +317,7 @@ public abstract class AbstractVelbusProtocol extends AbstractProtocol implements
getLinkedAttributeDescriptors(deviceType.get(), baseAddress)
.forEach(descriptor -> {
- AssetAttribute attribute = new AssetAttribute(descriptor.getName(), descriptor.getAttributeValueType())
+ AssetAttribute attribute = new AssetAttribute(descriptor.getName(), descriptor.getAttributeValueDescriptor())
.setMeta(
agentLink,
new MetaItem(MetaItemType.LABEL, Values.create(descriptor.getDisplayName()))
diff --git a/client/src/main/java/org/openremote/app/client/assets/attributes/AttributeViewImpl.java b/client/src/main/java/org/openremote/app/client/assets/attributes/AttributeViewImpl.java
index a847168ae..511371656 100644
--- a/client/src/main/java/org/openremote/app/client/assets/attributes/AttributeViewImpl.java
+++ b/client/src/main/java/org/openremote/app/client/assets/attributes/AttributeViewImpl.java
@@ -27,6 +27,7 @@ import org.openremote.app.client.widget.*;
import org.openremote.app.client.Environment;
import org.openremote.model.asset.AssetAttribute;
import org.openremote.model.asset.agent.ConnectionStatus;
+import org.openremote.model.attribute.AttributeValueDescriptor;
import org.openremote.model.attribute.AttributeValueType;
import org.openremote.model.attribute.AttributeValidationResult;
import org.openremote.model.interop.Consumer;
@@ -195,7 +196,7 @@ public class AttributeViewImpl extends FormGroup implements AttributeView {
protected IsWidget createAttributeValueEditor() {
return valueEditorSupplier.createValueEditor(attribute,
- attribute.getType().map(AttributeValueType::getValueType).orElse(null),
+ attribute.getType().map(AttributeValueDescriptor::getValueType).orElse(null),
style,
null,
this::notifyAttributeModified);
@@ -250,7 +251,7 @@ public class AttributeViewImpl extends FormGroup implements AttributeView {
} else if (attribute.isProtocolConfiguration()) {
formLabel.setIcon("cogs");
} else {
- formLabel.setIcon(attribute.getType().map(AttributeValueType::getIcon).orElse(AttributeValueType.DEFAULT_ICON));
+ formLabel.setIcon(attribute.getType().map(AttributeValueDescriptor::getIcon).orElse(AttributeValueType.DEFAULT_ICON));
}
getFormLabel().setText(getAttributeLabel());
@@ -265,7 +266,7 @@ public class AttributeViewImpl extends FormGroup implements AttributeView {
infoText.append(connectionStatus != null ? connectionStatus.toString() : environment.getMessages().waitingForStatus());
}
} else if (attribute.getType().isPresent()) {
- infoText.append(environment.getMessages().attributeValueType(attribute.getType().get().name()));
+ infoText.append(environment.getMessages().attributeValueType(attribute.getTypeOrThrow().getName()));
}
getAttributeDescription()
.ifPresent(description -> {
diff --git a/client/src/main/java/org/openremote/app/client/map/MapActivity.java b/client/src/main/java/org/openremote/app/client/map/MapActivity.java
index 92f5cacef..10b5ac254 100644
--- a/client/src/main/java/org/openremote/app/client/map/MapActivity.java
+++ b/client/src/main/java/org/openremote/app/client/map/MapActivity.java
@@ -35,6 +35,7 @@ import org.openremote.model.asset.Asset;
import org.openremote.model.asset.AssetAttribute;
import org.openremote.model.asset.AssetResource;
import org.openremote.model.attribute.AttributeEvent;
+import org.openremote.model.attribute.AttributeValueDescriptor;
import org.openremote.model.attribute.AttributeValueType;
import org.openremote.model.event.bus.EventBus;
import org.openremote.model.event.bus.EventRegistration;
@@ -205,7 +206,7 @@ public class MapActivity extends AssetBrowsingActivity<MapPlace> implements MapV
List<MapInfoItem> infoItems = dashboardAttributes.stream()
.filter(attribute -> attribute.getLabel().isPresent())
.map(attribute -> new MapInfoItem(
- attribute.getType().map(AttributeValueType::getIcon).orElse(AttributeValueType.DEFAULT_ICON),
+ attribute.getType().map(AttributeValueDescriptor::getIcon).orElse(AttributeValueType.DEFAULT_ICON),
attribute.getLabel().get(),
attribute.getFormat().orElse(null),
attribute.getValue().orElse(null)
diff --git a/manager/src/main/java/org/openremote/manager/asset/AssetModel.java b/manager/src/main/java/org/openremote/manager/asset/AssetModel.java
deleted file mode 100644
index bb1aa2dc5..000000000
--- a/manager/src/main/java/org/openremote/manager/asset/AssetModel.java
+++ /dev/null
@@ -1,147 +0,0 @@
-/*
- * Copyright 2017, OpenRemote Inc.
- *
- * See the CONTRIBUTORS.txt file in the distribution for a
- * full listing of individual contributors.
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU Affero General Public License as
- * published by the Free Software Foundation, either version 3 of the
- * License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU Affero General Public License for more details.
- *
- * You should have received a copy of the GNU Affero General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- */
-package org.openremote.manager.asset;
-
-import org.openremote.model.asset.AssetModelProvider;
-import org.openremote.model.asset.AssetDescriptor;
-import org.openremote.model.attribute.AttributeDescriptor;
-import org.openremote.model.attribute.MetaItem;
-import org.openremote.model.attribute.MetaItemDescriptor;
-
-import java.util.*;
-import java.util.logging.Logger;
-import java.util.stream.Stream;
-
-import static java.util.stream.Collectors.joining;
-import static org.openremote.model.asset.AssetType.CUSTOM;
-import static org.openremote.model.util.TextUtil.isNullOrEmpty;
-
-/**
- * Explore the asset model of this project and access the {@link AssetModelProvider}s through {@link ServiceLoader}.
- */
-public class AssetModel {
-
- private static final Logger LOG = Logger.getLogger(AssetModel.class.getName());
-
- public final static MetaItemDescriptor[] META_ITEM_DESCRIPTORS;
- public final static String META_ITEM_RESTRICTED_READ_SQL_FRAGMENT;
- public final static String META_ITEM_PUBLIC_READ_SQL_FRAGMENT;
-
- public final static AssetDescriptor[] WELL_KNOWN_ASSET_TYPES;
-
- public final static AttributeDescriptor[] WELL_KNOWN_ATTRIBUTE_TYPES;
-
- static {
- List<MetaItemDescriptor> metaItemDescriptorList = new ArrayList<>();
- ServiceLoader.load(AssetModelProvider.class).forEach(assetModelProvider -> {
- LOG.fine("Adding meta item descriptors of: " + assetModelProvider);
- metaItemDescriptorList.addAll(Arrays.asList(assetModelProvider.getMetaItemDescriptors()));
- });
- META_ITEM_DESCRIPTORS = metaItemDescriptorList.toArray(new MetaItemDescriptor[metaItemDescriptorList.size()]);
-
- META_ITEM_RESTRICTED_READ_SQL_FRAGMENT =
- " ('" + streamMetaItemDescriptors().filter(i -> i.getAccess().restrictedRead).map(MetaItemDescriptor::getUrn).collect(joining("','")) + "')";
- META_ITEM_PUBLIC_READ_SQL_FRAGMENT =
- " ('" + streamMetaItemDescriptors().filter(i -> i.getAccess().publicRead).map(MetaItemDescriptor::getUrn).collect(joining("','")) + "')";
-
- List<AssetDescriptor> assetTypeList = new ArrayList<>();
- ServiceLoader.load(AssetModelProvider.class).forEach(assetModelProvider -> {
- LOG.fine("Adding asset type descriptors of: " + assetModelProvider);
- assetTypeList.addAll(Arrays.asList(assetModelProvider.getAssetDescriptors()));
- });
-
- WELL_KNOWN_ASSET_TYPES = assetTypeList.toArray(new AssetDescriptor[assetTypeList.size()]);
-
- List<AttributeDescriptor> attributeList = new ArrayList<>();
- ServiceLoader.load(AssetModelProvider.class).forEach(assetModelProvider -> {
- LOG.fine("Adding asset type descriptors of: " + assetModelProvider);
- attributeList.addAll(Arrays.asList(assetModelProvider.getAttributeDescriptors()));
- });
-
- WELL_KNOWN_ATTRIBUTE_TYPES = attributeList.toArray(new AttributeDescriptor[attributeList.size()]);
- }
-
- public static Stream<MetaItemDescriptor> streamMetaItemDescriptors() {
- return Arrays.stream(META_ITEM_DESCRIPTORS);
- }
-
- public static Optional<MetaItemDescriptor> getMetaItemDescriptor(String urn) {
- if (isNullOrEmpty(urn))
- return Optional.empty();
- for (MetaItemDescriptor metaItemDescriptor : META_ITEM_DESCRIPTORS) {
- if (metaItemDescriptor.getUrn().equals(urn))
- return Optional.of(metaItemDescriptor);
- }
- return Optional.empty();
- }
-
- public static boolean isMetaItemRestrictedRead(MetaItem metaItem) {
- return getMetaItemDescriptor(metaItem.getName().orElse(null))
- .map(meta -> meta.getAccess().restrictedRead)
- .orElse(false);
- }
-
- public static boolean isMetaItemRestrictedWrite(MetaItem metaItem) {
- return getMetaItemDescriptor(metaItem.getName().orElse(null))
- .map(meta -> meta.getAccess().restrictedWrite)
- .orElse(false);
- }
-
- public static boolean isMetaItemPublicRead(MetaItem metaItem) {
- return getMetaItemDescriptor(metaItem.getName().orElse(null))
- .map(meta -> meta.getAccess().publicRead)
- .orElse(false);
- }
-
- public static AssetDescriptor[] getAssetTypesSorted() {
- List<AssetDescriptor> list = new ArrayList<>(Arrays.asList(WELL_KNOWN_ASSET_TYPES));
-
- list.sort(Comparator.comparing(AssetDescriptor::getName));
- if (list.contains(CUSTOM)) {
- // CUSTOM should be first
- list.remove(CUSTOM);
- list.add(0, CUSTOM);
- }
-
- return list.toArray(new AssetDescriptor[list.size()]);
- }
-
- public static Optional<AssetDescriptor> getAssetDescriptor(String urn) {
- if (urn == null)
- return Optional.empty();
-
- for (AssetDescriptor assetType : WELL_KNOWN_ASSET_TYPES) {
- if (urn.equals(assetType.getType()))
- return Optional.of(assetType);
- }
- return Optional.empty();
- }
-
- public static Optional<AttributeDescriptor> getAttributeDescriptor(String name) {
- if (name == null)
- return Optional.empty();
-
- for (AttributeDescriptor attributeDescriptor : WELL_KNOWN_ATTRIBUTE_TYPES) {
- if (name.equals(attributeDescriptor.getName()))
- return Optional.of(attributeDescriptor);
- }
- return Optional.empty();
- }
-}
diff --git a/manager/src/main/java/org/openremote/manager/asset/AssetModelService.java b/manager/src/main/java/org/openremote/manager/asset/AssetModelService.java
new file mode 100644
index 000000000..9fdd5894c
--- /dev/null
+++ b/manager/src/main/java/org/openremote/manager/asset/AssetModelService.java
@@ -0,0 +1,80 @@
+/*
+ * Copyright 2019, OpenRemote Inc.
+ *
+ * See the CONTRIBUTORS.txt file in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+package org.openremote.manager.asset;
+
+import org.openremote.container.Container;
+import org.openremote.container.ContainerService;
+import org.openremote.model.asset.AssetDescriptor;
+import org.openremote.model.asset.AssetModelProvider;
+import org.openremote.model.attribute.AttributeDescriptor;
+import org.openremote.model.attribute.AttributeValueDescriptor;
+import org.openremote.model.attribute.MetaItemDescriptor;
+import org.openremote.model.util.AssetModelUtil;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+import java.util.ServiceLoader;
+import java.util.logging.Logger;
+
+/**
+ * This service populates the descriptors in {@link org.openremote.model.util.AssetModelUtil} by looking for
+ * {@link AssetModelProvider}s through {@link ServiceLoader}.
+ */
+public class AssetModelService implements ContainerService {
+
+ private static final Logger LOG = Logger.getLogger(AssetModelService.class.getName());
+
+ @Override
+ public int getPriority() {
+ return Integer.MIN_VALUE + 10;
+ }
+
+ @Override
+ public void init(Container container) throws Exception {
+
+ List<AssetDescriptor> assetDescriptors = new ArrayList<>();
+ List<AttributeDescriptor> attributeDescriptors = new ArrayList<>();
+ List<AttributeValueDescriptor> attributeValueDescriptors = new ArrayList<>();
+ List<MetaItemDescriptor> metaItemDescriptors = new ArrayList<>();
+
+ ServiceLoader.load(AssetModelProvider.class).forEach(assetModelProvider -> {
+ LOG.fine("Adding asset model descriptors of provider: " + assetModelProvider.getClass().getName());
+
+ assetDescriptors.addAll(Arrays.asList(assetModelProvider.getAssetDescriptors()));
+ attributeDescriptors.addAll(Arrays.asList(assetModelProvider.getAttributeDescriptors()));
+ attributeValueDescriptors.addAll(Arrays.asList(assetModelProvider.getAttributeValueDescriptors()));
+ metaItemDescriptors.addAll(Arrays.asList(assetModelProvider.getMetaItemDescriptors()));
+ });
+
+ AssetModelUtil.setAssetDescriptors(assetDescriptors.toArray(new AssetDescriptor[0]));
+ AssetModelUtil.setAttributeDescriptors(attributeDescriptors.toArray(new AttributeDescriptor[0]));
+ AssetModelUtil.setAttributeValueDescriptors(attributeValueDescriptors.toArray(new AttributeValueDescriptor[0]));
+ AssetModelUtil.setMetaItemDescriptors(metaItemDescriptors.toArray(new MetaItemDescriptor[0]));
+ }
+
+ @Override
+ public void start(Container container) throws Exception {
+ }
+
+ @Override
+ public void stop(Container container) throws Exception {
+ }
+}
diff --git a/manager/src/main/java/org/openremote/manager/asset/AssetProcessingService.java b/manager/src/main/java/org/openremote/manager/asset/AssetProcessingService.java
index 2736b4bb5..4c8516fef 100644
--- a/manager/src/main/java/org/openremote/manager/asset/AssetProcessingService.java
+++ b/manager/src/main/java/org/openremote/manager/asset/AssetProcessingService.java
@@ -42,6 +42,7 @@ import org.openremote.model.attribute.AttributeEvent;
import org.openremote.model.attribute.AttributeEvent.Source;
import org.openremote.model.attribute.AttributeExecuteStatus;
import org.openremote.model.security.ClientRole;
+import org.openremote.model.util.AssetModelUtil;
import org.openremote.model.value.Value;
import org.openremote.model.value.Values;
@@ -349,7 +350,7 @@ public class AssetProcessingService extends RouteBuilder implements ContainerSer
}
//Check if attribute is well known and the value is valid
- AssetModel.getAttributeDescriptor(oldAttribute.name).ifPresent(wellKnownAttribute -> {
+ AssetModelUtil.getAttributeDescriptor(oldAttribute.name).ifPresent(wellKnownAttribute -> {
// Check if the value is valid
wellKnownAttribute.getValueDescriptor()
.getValidator().flatMap(v -> v.apply(event.getValue().orElse(null)))
diff --git a/manager/src/main/java/org/openremote/manager/asset/AssetResourceImpl.java b/manager/src/main/java/org/openremote/manager/asset/AssetResourceImpl.java
index cb2b687fd..e6b37b475 100644
--- a/manager/src/main/java/org/openremote/manager/asset/AssetResourceImpl.java
+++ b/manager/src/main/java/org/openremote/manager/asset/AssetResourceImpl.java
@@ -32,6 +32,7 @@ import org.openremote.model.query.BaseAssetQuery.Select;
import org.openremote.model.query.filter.ParentPredicate;
import org.openremote.model.query.filter.TenantPredicate;
import org.openremote.model.security.Tenant;
+import org.openremote.model.util.AssetModelUtil;
import org.openremote.model.util.TextUtil;
import org.openremote.model.value.Value;
import org.openremote.model.value.ValueException;
@@ -43,7 +44,6 @@ import javax.ws.rs.core.HttpHeaders;
import java.io.IOException;
import java.util.*;
import java.util.logging.Logger;
-import java.util.stream.Stream;
import static javax.ws.rs.core.Response.Status.*;
import static org.openremote.container.Container.JSON;
@@ -285,10 +285,10 @@ public class AssetResourceImpl extends ManagerWebResource implements AssetResour
Meta existingMetaItems = existingAttribute.getMeta().copy();
// Remove any writable existing meta items
- existingMetaItems.removeIf(AssetModel::isMetaItemRestrictedWrite);
+ existingMetaItems.removeIf(AssetModelUtil::isMetaItemRestrictedWrite);
// Add any writable updated meta items
- updatedMetaItems.stream().filter(AssetModel::isMetaItemRestrictedWrite).forEach(existingMetaItems::add);
+ updatedMetaItems.stream().filter(AssetModelUtil::isMetaItemRestrictedWrite).forEach(existingMetaItems::add);
// Replace existing with updated attribute
updatedAttribute.setMeta(existingMetaItems);
@@ -298,7 +298,7 @@ public class AssetResourceImpl extends ManagerWebResource implements AssetResour
// An attribute added by a restricted user can only have meta items which are writable
updatedAttribute.getMetaStream().forEach(metaItem -> {
- if (!AssetModel.isMetaItemRestrictedWrite(metaItem)) {
+ if (!AssetModelUtil.isMetaItemRestrictedWrite(metaItem)) {
LOG.fine("Attribute has " + metaItem + " not writable by restricted client: " + updatedAttributeName);
throw new WebApplicationException(
"Attribute has meta item not writable by restricted client: " + updatedAttributeName,
@@ -351,7 +351,7 @@ public class AssetResourceImpl extends ManagerWebResource implements AssetResour
private void checkForWellKnownAttributes(Asset asset) {
asset.getAttributesStream().forEach(assetAttribute -> {
- AssetModel.getAttributeDescriptor(assetAttribute.name).ifPresent(wellKnownAttribute -> {
+ AssetModelUtil.getAttributeDescriptor(assetAttribute.name).ifPresent(wellKnownAttribute -> {
//Check if the type matches
if (!wellKnownAttribute.getValueDescriptor().equals(assetAttribute.getTypeOrThrow())) {
throw new IllegalStateException(
@@ -459,7 +459,7 @@ public class AssetResourceImpl extends ManagerWebResource implements AssetResour
newAsset.setId(asset.getId());
}
- AssetModel.getAssetDescriptor(asset.getType()).ifPresent(assetDescriptor -> {
+ AssetModelUtil.getAssetDescriptor(asset.getType()).ifPresent(assetDescriptor -> {
newAsset.setAccessPublicRead(assetDescriptor.getAccessPublicRead());
diff --git a/manager/src/main/java/org/openremote/manager/asset/AssetStorageService.java b/manager/src/main/java/org/openremote/manager/asset/AssetStorageService.java
index f9829b4ad..2b43f51ac 100644
--- a/manager/src/main/java/org/openremote/manager/asset/AssetStorageService.java
+++ b/manager/src/main/java/org/openremote/manager/asset/AssetStorageService.java
@@ -47,6 +47,7 @@ import org.openremote.model.ValidationFailure;
import org.openremote.model.asset.*;
import org.openremote.model.attribute.AttributeEvent;
import org.openremote.model.attribute.AttributeType;
+import org.openremote.model.attribute.MetaItemDescriptor;
import org.openremote.model.calendar.CalendarEvent;
import org.openremote.model.calendar.RecurrenceRule;
import org.openremote.model.event.TriggeredEventSubscription;
@@ -56,6 +57,7 @@ import org.openremote.model.query.BaseAssetQuery;
import org.openremote.model.query.filter.*;
import org.openremote.model.security.ClientRole;
import org.openremote.model.security.User;
+import org.openremote.model.util.AssetModelUtil;
import org.openremote.model.util.Pair;
import org.openremote.model.util.TextUtil;
import org.openremote.model.value.ObjectValue;
@@ -74,6 +76,7 @@ import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Stream;
+import static java.util.stream.Collectors.joining;
import static org.openremote.container.persistence.PersistenceEvent.PERSISTENCE_TOPIC;
import static org.openremote.container.persistence.PersistenceEvent.isPersistenceEventForEntityType;
import static org.openremote.manager.event.ClientEventService.CLIENT_EVENT_TOPIC;
@@ -105,6 +108,8 @@ public class AssetStorageService extends RouteBuilder implements ContainerServic
}
}
+ protected static String META_ITEM_RESTRICTED_READ_SQL_FRAGMENT;
+ protected static String META_ITEM_PUBLIC_READ_SQL_FRAGMENT;
private static final Logger LOG = Logger.getLogger(AssetStorageService.class.getName());
protected TimerService timerService;
@@ -124,6 +129,12 @@ public class AssetStorageService extends RouteBuilder implements ContainerServic
identityService = container.getService(ManagerIdentityService.class);
clientEventService = container.getService(ClientEventService.class);
+ META_ITEM_RESTRICTED_READ_SQL_FRAGMENT =
+ " ('" + Arrays.stream(AssetModelUtil.getMetaItemDescriptors()).filter(i -> i.getAccess().restrictedRead).map(MetaItemDescriptor::getUrn).collect(joining("','")) + "')";
+
+ META_ITEM_PUBLIC_READ_SQL_FRAGMENT =
+ " ('" + Arrays.stream(AssetModelUtil.getMetaItemDescriptors()).filter(i -> i.getAccess().publicRead).map(MetaItemDescriptor::getUrn).collect(joining("','")) + "')";
+
clientEventService.addSubscriptionAuthorizer((auth, subscription) ->
(subscription.isEventType(AssetTreeModifiedEvent.class) || subscription.isEventType(
LocationEvent.class))
@@ -749,7 +760,7 @@ public class AssetStorageService extends RouteBuilder implements ContainerServic
sb.append(" INNER JOIN LATERAL (");
sb.append("select jsonb_agg(AM.value) AS VALUE from jsonb_array_elements(AX.VALUE #> '{meta}') as AM");
sb.append(" where AM.VALUE #>> '{name}' IN");
- sb.append(access == RESTRICTED_READ ? AssetModel.META_ITEM_RESTRICTED_READ_SQL_FRAGMENT : AssetModel.META_ITEM_PUBLIC_READ_SQL_FRAGMENT);
+ sb.append(access == RESTRICTED_READ ? META_ITEM_RESTRICTED_READ_SQL_FRAGMENT : META_ITEM_PUBLIC_READ_SQL_FRAGMENT);
sb.append(") as AMF ON true");
} else {
sb.append("select json_object_agg(AX.key, AX.value) from jsonb_each(A.attributes) as AX");
diff --git a/manager/src/main/resources/META-INF/services/org.openremote.container.ContainerService b/manager/src/main/resources/META-INF/services/org.openremote.container.ContainerService
index 4067ba903..2565c1f0a 100644
--- a/manager/src/main/resources/META-INF/services/org.openremote.container.ContainerService
+++ b/manager/src/main/resources/META-INF/services/org.openremote.container.ContainerService
@@ -5,6 +5,7 @@ org.openremote.manager.rules.RulesHealthStatusProvider
org.openremote.manager.agent.AgentHealthStatusProvider
org.openremote.manager.datapoint.AssetDatapointHealthStatusProvider
org.openremote.manager.rules.geofence.ORConsoleGeofenceAssetAdapter
+org.openremote.manager.asset.AssetModelService
org.openremote.manager.concurrent.ManagerExecutorService
org.openremote.container.timer.TimerService
org.openremote.manager.i18n.I18NService
diff --git a/model/src/main/java/org/openremote/model/asset/AssetAttribute.java b/model/src/main/java/org/openremote/model/asset/AssetAttribute.java
index 13dcb8067..cd1d2e4b8 100644
--- a/model/src/main/java/org/openremote/model/asset/AssetAttribute.java
+++ b/model/src/main/java/org/openremote/model/asset/AssetAttribute.java
@@ -54,7 +54,7 @@ public class AssetAttribute extends Attribute {
super(name);
}
- public AssetAttribute(String name, AttributeValueType type) {
+ public AssetAttribute(String name, AttributeValueDescriptor type) {
super(name, type);
}
diff --git a/model/src/main/java/org/openremote/model/attribute/Attribute.java b/model/src/main/java/org/openremote/model/attribute/Attribute.java
index bf17eab9b..22956b5e5 100644
--- a/model/src/main/java/org/openremote/model/attribute/Attribute.java
+++ b/model/src/main/java/org/openremote/model/attribute/Attribute.java
@@ -24,6 +24,7 @@ import com.google.gwt.regexp.shared.RegExp;
import org.openremote.model.AbstractValueTimestampHolder;
import org.openremote.model.HasUniqueResourceName;
import org.openremote.model.ValidationFailure;
+import org.openremote.model.util.AssetModelUtil;
import org.openremote.model.util.EnumUtil;
import org.openremote.model.value.*;
@@ -93,6 +94,7 @@ public abstract class Attribute extends AbstractValueTimestampHolder {
return Optional.ofNullable(name);
}
+ @JsonIgnore
public String getNameOrThrow() {
return getName().orElseThrow(() -> new IllegalStateException("Attribute doesn't have a name"));
}
@@ -106,15 +108,16 @@ public abstract class Attribute extends AbstractValueTimestampHolder {
this.name = null;
}
- public Optional<AttributeValueType> getType() {
- return getObjectValue().getString(TYPE_FIELD_NAME).flatMap(name -> EnumUtil.enumFromString(AttributeValueType.class, name));
+ public Optional<AttributeValueDescriptor> getType() {
+ return getObjectValue().getString(TYPE_FIELD_NAME).flatMap(AssetModelUtil::getAttributeValueDescriptor);
}
/**
* The below is used in a sufficient number of places to provide it here as a utility method
* with a standardised exception message.
*/
- public AttributeValueType getTypeOrThrow() {
+ @JsonIgnore
+ public AttributeValueDescriptor getTypeOrThrow() {
return getType().orElseThrow(() -> new IllegalStateException("Attribute doesn't have a type"));
}
@@ -147,6 +150,7 @@ public abstract class Attribute extends AbstractValueTimestampHolder {
return meta;
}
+ @JsonIgnore
public Stream<MetaItem> getMetaStream() {
return getMeta().stream();
}
@@ -220,7 +224,7 @@ public abstract class Attribute extends AbstractValueTimestampHolder {
// Value can be empty, if it's not it must validate with the type
getValue().flatMap(value ->
- getType().flatMap(AttributeValueType::getValidator).flatMap(v -> v.apply(value))
+ getType().flatMap(AttributeValueDescriptor::getValidator).flatMap(v -> v.apply(value))
).ifPresent(failures::add);
if (includeMeta) {
diff --git a/model/src/main/java/org/openremote/model/attribute/AttributeValueType.java b/model/src/main/java/org/openremote/model/attribute/AttributeValueType.java
index cd140c4ce..f60a0c8d8 100644
--- a/model/src/main/java/org/openremote/model/attribute/AttributeValueType.java
+++ b/model/src/main/java/org/openremote/model/attribute/AttributeValueType.java
@@ -19,6 +19,7 @@
*/
package org.openremote.model.attribute;
+import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;
import org.openremote.model.ValidationFailure;
import org.openremote.model.ValueHolder;
diff --git a/model/src/main/java/org/openremote/model/attribute/LinkedAttributeDescriptor.java b/model/src/main/java/org/openremote/model/attribute/LinkedAttributeDescriptor.java
index 4adc12c19..49c6c273c 100644
--- a/model/src/main/java/org/openremote/model/attribute/LinkedAttributeDescriptor.java
+++ b/model/src/main/java/org/openremote/model/attribute/LinkedAttributeDescriptor.java
@@ -23,7 +23,7 @@ public class LinkedAttributeDescriptor {
protected final String name;
protected final String displayName;
- protected final AttributeValueType attributeValueType;
+ protected final AttributeValueDescriptor attributeValueDescriptor;
protected final boolean readOnly;
protected final boolean executable;
protected final MetaItem[] metaItems;
@@ -31,13 +31,13 @@ public class LinkedAttributeDescriptor {
public LinkedAttributeDescriptor(
String name,
String displayName,
- AttributeValueType attributeValueType,
+ AttributeValueType attributeValueDescriptor,
boolean readOnly,
boolean executable,
MetaItem[] metaItems) {
this.name = name;
this.displayName = displayName;
- this.attributeValueType = attributeValueType;
+ this.attributeValueDescriptor = attributeValueDescriptor;
this.readOnly = readOnly;
this.executable = executable;
this.metaItems = metaItems;
@@ -51,8 +51,8 @@ public class LinkedAttributeDescriptor {
return displayName;
}
- public AttributeValueType getAttributeValueType() {
- return attributeValueType;
+ public AttributeValueDescriptor getAttributeValueDescriptor() {
+ return attributeValueDescriptor;
}
public boolean isReadOnly() {
diff --git a/model/src/main/java/org/openremote/model/rules/AssetState.java b/model/src/main/java/org/openremote/model/rules/AssetState.java
index 42f0e5ab3..1dff15d40 100644
--- a/model/src/main/java/org/openremote/model/rules/AssetState.java
+++ b/model/src/main/java/org/openremote/model/rules/AssetState.java
@@ -26,7 +26,7 @@ import org.openremote.model.asset.Asset;
import org.openremote.model.asset.AssetAttribute;
import org.openremote.model.asset.AssetType;
import org.openremote.model.attribute.AttributeEvent;
-import org.openremote.model.attribute.AttributeValueType;
+import org.openremote.model.attribute.AttributeValueDescriptor;
import org.openremote.model.attribute.Meta;
import org.openremote.model.value.ArrayValue;
import org.openremote.model.value.ObjectValue;
@@ -49,7 +49,7 @@ public class AssetState implements Comparable<AssetState> {
final protected String attributeName;
- final protected AttributeValueType attributeValueType;
+ final protected AttributeValueDescriptor attributeValueType;
final protected Value value;
@@ -133,7 +133,7 @@ public class AssetState implements Comparable<AssetState> {
return attributeName;
}
- public AttributeValueType getAttributeValueType() {
+ public AttributeValueDescriptor getAttributeValueType() {
return attributeValueType;
}
diff --git a/model/src/main/java/org/openremote/model/simulator/SimulatorElement.java b/model/src/main/java/org/openremote/model/simulator/SimulatorElement.java
index 660efcd20..bad4e73e6 100644
--- a/model/src/main/java/org/openremote/model/simulator/SimulatorElement.java
+++ b/model/src/main/java/org/openremote/model/simulator/SimulatorElement.java
@@ -24,6 +24,7 @@ import com.fasterxml.jackson.annotation.JsonTypeInfo;
import org.openremote.model.ValidationFailure;
import org.openremote.model.ValueHolder;
import org.openremote.model.attribute.AttributeRef;
+import org.openremote.model.attribute.AttributeValueDescriptor;
import org.openremote.model.attribute.AttributeValueType;
import org.openremote.model.simulator.element.ColorSimulatorElement;
import org.openremote.model.simulator.element.NumberSimulatorElement;
@@ -51,7 +52,7 @@ import java.util.Optional;
public abstract class SimulatorElement implements ValueHolder {
public AttributeRef attributeRef;
- public AttributeValueType expectedType;
+ public AttributeValueDescriptor expectedType;
// TODO GWT jackson doesn't like fields with the same name as getters in the hierarchy
public Value elementValue = null;
@@ -67,7 +68,7 @@ public abstract class SimulatorElement implements ValueHolder {
return attributeRef;
}
- public AttributeValueType getExpectedType() {
+ public AttributeValueDescriptor getExpectedType() {
return expectedType;
}
diff --git a/model/src/main/java/org/openremote/model/util/AssetModelUtil.java b/model/src/main/java/org/openremote/model/util/AssetModelUtil.java
new file mode 100644
index 000000000..fe4179602
--- /dev/null
+++ b/model/src/main/java/org/openremote/model/util/AssetModelUtil.java
@@ -0,0 +1,167 @@
+/*
+ * Copyright 2017, OpenRemote Inc.
+ *
+ * See the CONTRIBUTORS.txt file in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+package org.openremote.model.util;
+
+import org.openremote.model.asset.AssetDescriptor;
+import org.openremote.model.asset.AssetModelProvider;
+import org.openremote.model.attribute.AttributeDescriptor;
+import org.openremote.model.attribute.AttributeValueDescriptor;
+import org.openremote.model.attribute.MetaItem;
+import org.openremote.model.attribute.MetaItemDescriptor;
+
+import java.util.Optional;
+
+import static org.openremote.model.util.TextUtil.isNullOrEmpty;
+
+/**
+ * Explore the asset model of this project and access the {@link AssetModelProvider}s.
+ * <p>
+ * In a given runtime something should take responsibility for populating the descriptors at start up when running
+ * within the manager then the AssetModelService will take care of this; code running outside of the manager should
+ * use the {@link org.openremote.model.asset.AssetModelResource} endpoint to get this information and set it once
+ * at start up.
+ */
+public class AssetModelUtil {
+
+ protected static AssetDescriptor[] assetDescriptors = new AssetDescriptor[0];
+ protected static AttributeDescriptor[] attributeDescriptors = new AttributeDescriptor[0];
+ protected static AttributeValueDescriptor[] attributeValueDescriptors = new AttributeValueDescriptor[0];
+ protected static MetaItemDescriptor[] metaItemDescriptors = new MetaItemDescriptor[0];
+
+ private AssetModelUtil() {}
+
+ /**
+ * This should be called by something in the VM at startup to populate the available {@link AssetDescriptor}s.
+ */
+ public static void setAssetDescriptors(AssetDescriptor[] assetDescriptors) {
+ AssetModelUtil.assetDescriptors = assetDescriptors;
+ }
+
+ /**
+ * This should be called by something in the VM at startup to populate the available {@link AttributeDescriptor}s.
+ */
+ public static void setAttributeDescriptors(AttributeDescriptor[] attributeDescriptors) {
+ AssetModelUtil.attributeDescriptors = attributeDescriptors;
+ }
+
+ /**
+ * This should be called by something in the VM at startup to populate the available {@link AttributeValueDescriptor}s.
+ */
+ public static void setAttributeValueDescriptors(AttributeValueDescriptor[] attributeValueDescriptors) {
+ AssetModelUtil.attributeValueDescriptors = attributeValueDescriptors;
+ }
+
+ /**
+ * This should be called by something in the VM at startup to populate the available {@link MetaItemDescriptor}s.
+ */
+ public static void setMetaItemDescriptors(MetaItemDescriptor[] metaItemDescriptors) {
+ AssetModelUtil.metaItemDescriptors = metaItemDescriptors;
+ }
+
+ public static AssetDescriptor[] getAssetDescriptors() {
+ return assetDescriptors;
+ }
+
+ public static AttributeDescriptor[] getAttributeDescriptors() {
+ return attributeDescriptors;
+ }
+
+ public static AttributeValueDescriptor[] getAttributeValueDescriptors() {
+ return attributeValueDescriptors;
+ }
+
+ public static MetaItemDescriptor[] getMetaItemDescriptors() {
+ return metaItemDescriptors;
+ }
+
+ public static Optional<MetaItemDescriptor> getMetaItemDescriptor(String urn) {
+ if (isNullOrEmpty(urn))
+ return Optional.empty();
+ for (MetaItemDescriptor metaItemDescriptor : metaItemDescriptors) {
+ if (metaItemDescriptor.getUrn().equals(urn))
+ return Optional.of(metaItemDescriptor);
+ }
+ return Optional.empty();
+ }
+
+ public static Optional<AssetDescriptor> getAssetDescriptor(String urn) {
+ if (urn == null)
+ return Optional.empty();
+
+ for (AssetDescriptor assetType : assetDescriptors) {
+ if (urn.equals(assetType.getType()))
+ return Optional.of(assetType);
+ }
+ return Optional.empty();
+ }
+
+ public static Optional<AttributeDescriptor> getAttributeDescriptor(String name) {
+ if (name == null)
+ return Optional.empty();
+
+ for (AttributeDescriptor attributeDescriptor : attributeDescriptors) {
+ if (name.equals(attributeDescriptor.getName()))
+ return Optional.of(attributeDescriptor);
+ }
+ return Optional.empty();
+ }
+
+ public static Optional<AttributeValueDescriptor> getAttributeValueDescriptor(String name) {
+ if (name == null)
+ return Optional.empty();
+
+ for (AttributeValueDescriptor attributeValueDescriptor : attributeValueDescriptors) {
+ if (name.equals(attributeValueDescriptor.getName()))
+ return Optional.of(attributeValueDescriptor);
+ }
+ return Optional.empty();
+ }
+
+ public static boolean isMetaItemRestrictedRead(MetaItem metaItem) {
+ return getMetaItemDescriptor(metaItem.getName().orElse(null))
+ .map(meta -> meta.getAccess().restrictedRead)
+ .orElse(false);
+ }
+
+ public static boolean isMetaItemRestrictedWrite(MetaItem metaItem) {
+ return getMetaItemDescriptor(metaItem.getName().orElse(null))
+ .map(meta -> meta.getAccess().restrictedWrite)
+ .orElse(false);
+ }
+
+ public static boolean isMetaItemPublicRead(MetaItem metaItem) {
+ return getMetaItemDescriptor(metaItem.getName().orElse(null))
+ .map(meta -> meta.getAccess().publicRead)
+ .orElse(false);
+ }
+//
+// public static AssetDescriptor[] getAssetTypesSorted() {
+// List<AssetDescriptor> list = new ArrayList<>(Arrays.asList(assetDescriptors));
+//
+// list.sort(Comparator.comparing(AssetDescriptor::getName));
+// if (list.contains(CUSTOM)) {
+// // CUSTOM should be first
+// list.remove(CUSTOM);
+// list.add(0, CUSTOM);
+// }
+//
+// return list.toArray(new AssetDescriptor[list.size()]);
+// }
+}
diff --git a/model/src/main/java/org/openremote/model/util/EnumUtil.java b/model/src/main/java/org/openremote/model/util/EnumUtil.java
index 21a890a8f..d17c390fe 100644
--- a/model/src/main/java/org/openremote/model/util/EnumUtil.java
+++ b/model/src/main/java/org/openremote/model/util/EnumUtil.java
@@ -28,7 +28,6 @@ import java.util.Optional;
public final class EnumUtil {
private EnumUtil() {}
-
public static Value enumToValue(Enum<?> enumValue, ValueType valueType) {
switch (valueType) {