100% open-source IoT Platform - Integrate your devices, add intelligence, build custom apps
Java asset-managementbuildingdashboarddataplatformdevice-managementedge-computingenergyenergy-managementenergy-monitorfleet-managementfleet-telematicsiotiot-applicationiot-platformmodbusmqttmqtt-brokerota-updaterules-enginesmartcity
ConsoleResource implemented (Test pending)
Rich Turner2765eb1ddf15
diff --git a/manager/src/main/java/org/openremote/manager/asset/AssetStorageService.java b/manager/src/main/java/org/openremote/manager/asset/AssetStorageService.javaindex 2584dbe97..976748c54 100644--- a/manager/src/main/java/org/openremote/manager/asset/AssetStorageService.java+++ b/manager/src/main/java/org/openremote/manager/asset/AssetStorageService.java@@ -38,6 +38,7 @@ import org.openremote.container.persistence.PersistenceService;import org.openremote.container.security.AuthContext;import org.openremote.container.timer.TimerService;import org.openremote.container.web.WebService;+import org.openremote.manager.asset.console.ConsoleResourceImpl;import org.openremote.manager.event.ClientEventService;import org.openremote.manager.security.ManagerIdentityService;import org.openremote.manager.security.UserConfiguration;@@ -127,6 +128,12 @@ public class AssetStorageService extends RouteBuilder implements ContainerServic));+ container.getService(WebService.class).getApiSingletons().add(+ new ConsoleResourceImpl(container.getService(TimerService.class),+ identityService,+ this)+ );+container.getService(MessageBrokerSetupService.class).getContext().addRoutes(this);}@@ -412,7 +419,7 @@ public class AssetStorageService extends RouteBuilder implements ContainerServicreturn persistenceService.doReturningTransaction(entityManager -> {if (isNullOrEmpty(userId) && isNullOrEmpty(assetId)) {return entityManager.createQuery(- "select ua from UserAsset ua where ua.id.realmId = :realmId order by ua.createdOn desc", UserAsset.class+ "select ua from UserAsset ua where ua.id.realmIód = :realmId order by ua.createdOn desc", UserAsset.class).setParameter("realmId", realmId).getResultList();} else if (isNullOrEmpty(assetId)) {return entityManager.createQuery(diff --git a/manager/src/main/java/org/openremote/manager/asset/console/ConsoleResourceImpl.java b/manager/src/main/java/org/openremote/manager/asset/console/ConsoleResourceImpl.javanew file mode 100644index 000000000..7fbda87c1--- /dev/null+++ b/manager/src/main/java/org/openremote/manager/asset/console/ConsoleResourceImpl.java@@ -0,0 +1,102 @@+/*+ * 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.console;++import org.openremote.container.timer.TimerService;+import org.openremote.container.util.UniqueIdentifierGenerator;+import org.openremote.manager.asset.AssetStorageService;+import org.openremote.manager.security.ManagerIdentityService;+import org.openremote.manager.web.ManagerWebResource;+import org.openremote.model.ValidationFailure;+import org.openremote.model.asset.Asset;+import org.openremote.model.asset.AssetType;+import org.openremote.model.asset.UserAsset;+import org.openremote.model.console.ConsoleConfigration;+import org.openremote.model.console.ConsoleResource;+import org.openremote.model.http.RequestParams;+import org.openremote.model.util.TextUtil;++import javax.ws.rs.BadRequestException;+import javax.ws.rs.core.Response;+import java.util.ArrayList;+import java.util.HashMap;+import java.util.List;+import java.util.Map;++import static org.openremote.container.concurrent.GlobalLock.withLockReturning;++public class ConsoleResourceImpl extends ManagerWebResource implements ConsoleResource {++ public static final String CONSOLE_PARENT_ASSET_NAME = "Consoles";+ protected Map<String, String> realmConsoleParentMap = new HashMap<>();+ protected AssetStorageService assetStorageService;++ public ConsoleResourceImpl(TimerService timerService, ManagerIdentityService identityService, AssetStorageService assetStorageService) {+ super(timerService, identityService);+ this.assetStorageService = assetStorageService;+ }++ @Override+ public Asset register(RequestParams requestParams, Asset console) {+ // Validate the console+ List<ValidationFailure> failures = new ArrayList<>();+ if (!ConsoleConfigration.validateConsoleConfiguration(console, failures)) {+ throw new BadRequestException(Response.status(Response.Status.BAD_REQUEST).entity(failures).build());+ }++ // Set parent asset (if not set)+ if (TextUtil.isNullOrEmpty(console.getParentId())) {+ console.setParentId(getConsoleParentAssetId(getRequestRealm()));+ console = assetStorageService.merge(console);+ }++ // If authenticated link the console to this user+ if (isAuthenticated()) {+ assetStorageService.storeUserAsset(new UserAsset(getAuthenticatedTenant().getId(), getUserId(), console.getId()));+ }+ return console;+ }++ public String getConsoleParentAssetId(String realm) {+ return withLockReturning(getClass().getSimpleName() + "::getConsoleParentAssetId", () -> {+ String id = realmConsoleParentMap.get(realm);++ if (TextUtil.isNullOrEmpty(id)) {+ id = consoleParentAssetIdGenerator(realm);+ Asset consoleParent = assetStorageService.find(id, false);+ if (consoleParent == null) {+ consoleParent = new Asset(CONSOLE_PARENT_ASSET_NAME, AssetType.THING);+ consoleParent.setId(id);+ consoleParent.setRealmId(getRequestRealm());+ assetStorageService.merge(consoleParent);+ }++ realmConsoleParentMap.put(realm, id);+ return id;+ } else {+ return id;+ }+ });+ }++ protected static String consoleParentAssetIdGenerator(String realm) {+ return UniqueIdentifierGenerator.generateId(realm + "Consoles");+ }+}diff --git a/model/src/main/java/org/openremote/model/console/ConsoleConfigration.java b/model/src/main/java/org/openremote/model/console/ConsoleConfigration.javaindex cc6a0b0b1..b409d4402 100644--- a/model/src/main/java/org/openremote/model/console/ConsoleConfigration.java+++ b/model/src/main/java/org/openremote/model/console/ConsoleConfigration.java@@ -25,13 +25,13 @@ import org.openremote.model.asset.Asset;import org.openremote.model.asset.AssetAttribute;import org.openremote.model.asset.AssetType;import org.openremote.model.attribute.AttributeType;-import org.openremote.model.attribute.AttributeValidationResult;import org.openremote.model.util.Pair;import org.openremote.model.util.TextUtil;import org.openremote.model.value.ObjectValue;import org.openremote.model.value.Value;import org.openremote.model.value.Values;+import java.util.List;import java.util.Map;import java.util.Objects;import java.util.Optional;@@ -68,33 +68,33 @@ public final class ConsoleConfigration {return asset != null && asset.getWellKnownType() == AssetType.CONSOLE;}- public static boolean validateConsoleConfiguration(Asset asset, AttributeValidationResult result) {+ public static boolean validateConsoleConfiguration(Asset asset, List<ValidationFailure> validationFailures) {boolean valid = isConsole(asset);if (!valid) {- if (result != null) {- result.addAttributeFailure(new ValidationFailure(Asset.AssetTypeFailureReason.ASSET_TYPE_MISMATCH));+ if (validationFailures != null) {+ validationFailures.add(new ValidationFailure(Asset.AssetTypeFailureReason.ASSET_TYPE_MISMATCH));}return false;}if (!getConsoleName(asset).isPresent()) {- if (result != null) {- result.addAttributeFailure(new ValidationFailure(ValidationFailureReason.NAME_MISSING_OR_INVALID));+ if (validationFailures != null) {+ validationFailures.add(new ValidationFailure(ValidationFailureReason.NAME_MISSING_OR_INVALID));}valid = false;}if (!getConsoleVersion(asset).isPresent()) {- if (result != null) {- result.addAttributeFailure(new ValidationFailure(ValidationFailureReason.VERSION_MISSING_OR_INVALID));+ if (validationFailures != null) {+ validationFailures.add(new ValidationFailure(ValidationFailureReason.VERSION_MISSING_OR_INVALID));}valid = false;}if (!getConsolePlatform(asset).isPresent()) {- if (result != null) {- result.addAttributeFailure(new ValidationFailure(ValidationFailureReason.PLATFORM_MISSING_OR_INVALID));+ if (validationFailures != null) {+ validationFailures.add(new ValidationFailure(ValidationFailureReason.PLATFORM_MISSING_OR_INVALID));}valid = false;}@@ -102,8 +102,8 @@ public final class ConsoleConfigration {Value providerValue = asset.getAttribute(AttributeType.CONSOLE_PROVIDERS.getName()).flatMap(AbstractValueHolder::getValue).orElse(null);if (providerValue != null && !getConsoleProviders(asset).isPresent()) {- if (result != null) {- result.addAttributeFailure(new ValidationFailure(ValidationFailureReason.PROVIDERS_INVALID));+ if (validationFailures != null) {+ validationFailures.add(new ValidationFailure(ValidationFailureReason.PROVIDERS_INVALID));}valid = false;}diff --git a/model/src/main/java/org/openremote/model/console/ConsoleRegistration.java b/model/src/main/java/org/openremote/model/console/ConsoleRegistration.javadeleted file mode 100644index 05c87e806..000000000--- a/model/src/main/java/org/openremote/model/console/ConsoleRegistration.java+++ /dev/null@@ -1,37 +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.model.console;--import com.fasterxml.jackson.annotation.JsonCreator;-import com.fasterxml.jackson.annotation.JsonProperty;--public class ConsoleRegistration {-- protected String id;-- @JsonCreator- public ConsoleRegistration(@JsonProperty("id") String id) {- this.id = id;- }-- public String getId() {- return id;- }-}diff --git a/model/src/main/java/org/openremote/model/console/ConsoleResource.java b/model/src/main/java/org/openremote/model/console/ConsoleResource.javaindex e4213072b..effa4d8f8 100644--- a/model/src/main/java/org/openremote/model/console/ConsoleResource.java+++ b/model/src/main/java/org/openremote/model/console/ConsoleResource.java@@ -20,7 +20,7 @@package org.openremote.model.console;import jsinterop.annotations.JsType;-import org.openremote.model.asset.AssetType;+import org.openremote.model.asset.Asset;import org.openremote.model.http.RequestParams;import org.openremote.model.http.SuccessStatusCode;@@ -36,16 +36,18 @@ import static javax.ws.rs.core.MediaType.APPLICATION_JSON;public interface ConsoleResource {/**- * Creates or updates the registration for the specified console; if the {@link ConsoleConfigration} contains an ID- * then it is an update operation otherwise it is a create operation, in both cases a {@link ConsoleRegistration} is- * returned which contains the {@link ConsoleConfigration#getId()} value.+ * Creates or updates the registration for the specified console; if the console {@link Asset} contains an ID+ * then it is an update operation otherwise it is a create operation, in both cases the saved {@link Asset} is+ * returned which should be used for future calls to this endpoint.* <p>- * The console definition is converted to an asset of type {@link AssetType#CONSOLE}+ * This is a public endpoint and allows the registration of anonymous consoles; if there is an authenticated user+ * then the console asset will be linked to that user. If multiple users login on the same console then it will be+ * associated with each user (i.e. a 1-many relationship).*/@POST@Path("register")@SuccessStatusCode(204)@Consumes(APPLICATION_JSON)@SuppressWarnings("unusable-by-js")- ConsoleRegistration register(@BeanParam RequestParams requestParams, ConsoleConfigration consoleDefinition);+ Asset register(@BeanParam RequestParams requestParams, Asset console);}