From c933f782fb702466053ebb8907bc2aa59c3f5c17 Mon Sep 17 00:00:00 2001 From: Volodymyr Babak Date: Thu, 31 Aug 2023 16:44:08 +0300 Subject: [PATCH] Device profile and asset profile - handle name duplicates from edge --- .../asset/AssetProfileEdgeProcessor.java | 10 +++++++++- .../asset/BaseAssetProfileProcessor.java | 14 ++++++++++++-- .../device/BaseDeviceProfileProcessor.java | 16 +++++++++++++--- .../device/DeviceProfileEdgeProcessor.java | 10 +++++++++- 4 files changed, 43 insertions(+), 7 deletions(-) diff --git a/application/src/main/java/org/thingsboard/server/service/edge/rpc/processor/asset/AssetProfileEdgeProcessor.java b/application/src/main/java/org/thingsboard/server/service/edge/rpc/processor/asset/AssetProfileEdgeProcessor.java index b7cdc68932..a40cd64ccd 100644 --- a/application/src/main/java/org/thingsboard/server/service/edge/rpc/processor/asset/AssetProfileEdgeProcessor.java +++ b/application/src/main/java/org/thingsboard/server/service/edge/rpc/processor/asset/AssetProfileEdgeProcessor.java @@ -20,6 +20,7 @@ import com.fasterxml.jackson.databind.node.ObjectNode; import com.google.common.util.concurrent.Futures; import com.google.common.util.concurrent.ListenableFuture; import lombok.extern.slf4j.Slf4j; +import org.springframework.data.util.Pair; import org.springframework.stereotype.Component; import org.thingsboard.common.util.JacksonUtil; import org.thingsboard.server.common.data.DataConstants; @@ -27,6 +28,8 @@ import org.thingsboard.server.common.data.EdgeUtils; import org.thingsboard.server.common.data.asset.AssetProfile; import org.thingsboard.server.common.data.edge.Edge; import org.thingsboard.server.common.data.edge.EdgeEvent; +import org.thingsboard.server.common.data.edge.EdgeEventActionType; +import org.thingsboard.server.common.data.edge.EdgeEventType; import org.thingsboard.server.common.data.id.AssetProfileId; import org.thingsboard.server.common.data.id.DashboardId; import org.thingsboard.server.common.data.id.RuleChainId; @@ -74,11 +77,16 @@ public class AssetProfileEdgeProcessor extends BaseAssetProfileProcessor { } private void saveOrUpdateAssetProfile(TenantId tenantId, AssetProfileId assetProfileId, AssetProfileUpdateMsg assetProfileUpdateMsg, Edge edge) { - boolean created = super.saveOrUpdateAssetProfile(tenantId, assetProfileId, assetProfileUpdateMsg); + Pair resultPair = super.saveOrUpdateAssetProfile(tenantId, assetProfileId, assetProfileUpdateMsg); + Boolean created = resultPair.getFirst(); if (created) { createRelationFromEdge(tenantId, edge.getId(), assetProfileId); pushAssetProfileCreatedEventToRuleEngine(tenantId, edge, assetProfileId); } + Boolean assetProfileNameUpdated = resultPair.getSecond(); + if (assetProfileNameUpdated) { + saveEdgeEvent(tenantId, edge.getId(), EdgeEventType.ASSET_PROFILE, EdgeEventActionType.UPDATED, assetProfileId, null); + } } private void pushAssetProfileCreatedEventToRuleEngine(TenantId tenantId, Edge edge, AssetProfileId assetProfileId) { diff --git a/application/src/main/java/org/thingsboard/server/service/edge/rpc/processor/asset/BaseAssetProfileProcessor.java b/application/src/main/java/org/thingsboard/server/service/edge/rpc/processor/asset/BaseAssetProfileProcessor.java index 36034430ba..5e35bf0332 100644 --- a/application/src/main/java/org/thingsboard/server/service/edge/rpc/processor/asset/BaseAssetProfileProcessor.java +++ b/application/src/main/java/org/thingsboard/server/service/edge/rpc/processor/asset/BaseAssetProfileProcessor.java @@ -17,6 +17,8 @@ package org.thingsboard.server.service.edge.rpc.processor.asset; import com.datastax.oss.driver.api.core.uuid.Uuids; import lombok.extern.slf4j.Slf4j; +import org.springframework.data.util.Pair; +import org.thingsboard.server.common.data.StringUtils; import org.thingsboard.server.common.data.asset.AssetProfile; import org.thingsboard.server.common.data.id.AssetProfileId; import org.thingsboard.server.common.data.id.TenantId; @@ -28,8 +30,9 @@ import java.nio.charset.StandardCharsets; @Slf4j public abstract class BaseAssetProfileProcessor extends BaseEdgeProcessor { - protected boolean saveOrUpdateAssetProfile(TenantId tenantId, AssetProfileId assetProfileId, AssetProfileUpdateMsg assetProfileUpdateMsg) { + protected Pair saveOrUpdateAssetProfile(TenantId tenantId, AssetProfileId assetProfileId, AssetProfileUpdateMsg assetProfileUpdateMsg) { boolean created = false; + boolean assetProfileNameUpdated = false; assetCreationLock.lock(); try { AssetProfile assetProfile = assetProfileService.findAssetProfileById(tenantId, assetProfileId); @@ -40,6 +43,13 @@ public abstract class BaseAssetProfileProcessor extends BaseEdgeProcessor { assetProfile.setTenantId(tenantId); assetProfile.setCreatedTime(Uuids.unixTimestamp(assetProfileId.getId())); } + AssetProfile assetProfileByName = assetProfileService.findAssetProfileByName(tenantId, assetProfileName); + if (assetProfileByName != null && !assetProfileByName.getId().equals(assetProfileId)) { + assetProfileName = assetProfileName + "_" + StringUtils.randomAlphabetic(15); + log.warn("Asset profile with name {} already exists. Renaming asset profile name to {}", + assetProfileUpdateMsg.getName(), assetProfileName); + assetProfileNameUpdated = true; + } assetProfile.setName(assetProfileName); assetProfile.setDefault(assetProfileUpdateMsg.getDefault()); assetProfile.setDefaultQueueName(assetProfileUpdateMsg.hasDefaultQueueName() ? assetProfileUpdateMsg.getDefaultQueueName() : null); @@ -59,7 +69,7 @@ public abstract class BaseAssetProfileProcessor extends BaseEdgeProcessor { } finally { assetCreationLock.unlock(); } - return created; + return Pair.of(created, assetProfileNameUpdated); } protected abstract void setDefaultRuleChainId(TenantId tenantId, AssetProfile assetProfile, AssetProfileUpdateMsg assetProfileUpdateMsg); diff --git a/application/src/main/java/org/thingsboard/server/service/edge/rpc/processor/device/BaseDeviceProfileProcessor.java b/application/src/main/java/org/thingsboard/server/service/edge/rpc/processor/device/BaseDeviceProfileProcessor.java index 15a59121fe..324cf14584 100644 --- a/application/src/main/java/org/thingsboard/server/service/edge/rpc/processor/device/BaseDeviceProfileProcessor.java +++ b/application/src/main/java/org/thingsboard/server/service/edge/rpc/processor/device/BaseDeviceProfileProcessor.java @@ -18,6 +18,7 @@ package org.thingsboard.server.service.edge.rpc.processor.device; import com.datastax.oss.driver.api.core.uuid.Uuids; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.util.Pair; import org.thingsboard.server.common.data.DeviceProfile; import org.thingsboard.server.common.data.DeviceProfileProvisionType; import org.thingsboard.server.common.data.DeviceProfileType; @@ -41,18 +42,27 @@ public abstract class BaseDeviceProfileProcessor extends BaseEdgeProcessor { @Autowired private DataDecodingEncodingService dataDecodingEncodingService; - protected boolean saveOrUpdateDeviceProfile(TenantId tenantId, DeviceProfileId deviceProfileId, DeviceProfileUpdateMsg deviceProfileUpdateMsg) { + protected Pair saveOrUpdateDeviceProfile(TenantId tenantId, DeviceProfileId deviceProfileId, DeviceProfileUpdateMsg deviceProfileUpdateMsg) { boolean created = false; + boolean deviceProfileNameUpdated = false; deviceCreationLock.lock(); try { DeviceProfile deviceProfile = deviceProfileService.findDeviceProfileById(tenantId, deviceProfileId); + String deviceProfileName = deviceProfileUpdateMsg.getName(); if (deviceProfile == null) { created = true; deviceProfile = new DeviceProfile(); deviceProfile.setTenantId(tenantId); deviceProfile.setCreatedTime(Uuids.unixTimestamp(deviceProfileId.getId())); } - deviceProfile.setName(deviceProfileUpdateMsg.getName()); + DeviceProfile deviceProfileByName = deviceProfileService.findDeviceProfileByName(tenantId, deviceProfileName); + if (deviceProfileByName != null && !deviceProfileByName.getId().equals(deviceProfileId)) { + deviceProfileName = deviceProfileName + "_" + StringUtils.randomAlphabetic(15); + log.warn("Device profile with name {} already exists. Renaming device profile name to {}", + deviceProfileUpdateMsg.getName(), deviceProfileName); + deviceProfileNameUpdated = true; + } + deviceProfile.setName(deviceProfileName); deviceProfile.setDescription(deviceProfileUpdateMsg.hasDescription() ? deviceProfileUpdateMsg.getDescription() : null); deviceProfile.setType(DeviceProfileType.valueOf(deviceProfileUpdateMsg.getType())); deviceProfile.setTransportType(deviceProfileUpdateMsg.hasTransportType() @@ -91,7 +101,7 @@ public abstract class BaseDeviceProfileProcessor extends BaseEdgeProcessor { } finally { deviceCreationLock.unlock(); } - return created; + return Pair.of(created, deviceProfileNameUpdated); } protected abstract void setDefaultRuleChainId(TenantId tenantId, DeviceProfile deviceProfile, DeviceProfileUpdateMsg deviceProfileUpdateMsg); diff --git a/application/src/main/java/org/thingsboard/server/service/edge/rpc/processor/device/DeviceProfileEdgeProcessor.java b/application/src/main/java/org/thingsboard/server/service/edge/rpc/processor/device/DeviceProfileEdgeProcessor.java index 3638ff6063..470e396cb2 100644 --- a/application/src/main/java/org/thingsboard/server/service/edge/rpc/processor/device/DeviceProfileEdgeProcessor.java +++ b/application/src/main/java/org/thingsboard/server/service/edge/rpc/processor/device/DeviceProfileEdgeProcessor.java @@ -20,6 +20,7 @@ import com.fasterxml.jackson.databind.node.ObjectNode; import com.google.common.util.concurrent.Futures; import com.google.common.util.concurrent.ListenableFuture; import lombok.extern.slf4j.Slf4j; +import org.springframework.data.util.Pair; import org.springframework.stereotype.Component; import org.thingsboard.common.util.JacksonUtil; import org.thingsboard.server.common.data.DataConstants; @@ -27,6 +28,8 @@ import org.thingsboard.server.common.data.DeviceProfile; import org.thingsboard.server.common.data.EdgeUtils; import org.thingsboard.server.common.data.edge.Edge; import org.thingsboard.server.common.data.edge.EdgeEvent; +import org.thingsboard.server.common.data.edge.EdgeEventActionType; +import org.thingsboard.server.common.data.edge.EdgeEventType; import org.thingsboard.server.common.data.id.DashboardId; import org.thingsboard.server.common.data.id.DeviceProfileId; import org.thingsboard.server.common.data.id.RuleChainId; @@ -75,11 +78,16 @@ public class DeviceProfileEdgeProcessor extends BaseDeviceProfileProcessor { } private void saveOrUpdateDeviceProfile(TenantId tenantId, DeviceProfileId deviceProfileId, DeviceProfileUpdateMsg deviceProfileUpdateMsg, Edge edge) { - boolean created = super.saveOrUpdateDeviceProfile(tenantId, deviceProfileId, deviceProfileUpdateMsg); + Pair resultPair = super.saveOrUpdateDeviceProfile(tenantId, deviceProfileId, deviceProfileUpdateMsg); + Boolean created = resultPair.getFirst(); if (created) { createRelationFromEdge(tenantId, edge.getId(), deviceProfileId); pushDeviceProfileCreatedEventToRuleEngine(tenantId, edge, deviceProfileId); } + Boolean deviceProfileNameUpdated = resultPair.getSecond(); + if (deviceProfileNameUpdated) { + saveEdgeEvent(tenantId, edge.getId(), EdgeEventType.DEVICE_PROFILE, EdgeEventActionType.UPDATED, deviceProfileId, null); + } } private void pushDeviceProfileCreatedEventToRuleEngine(TenantId tenantId, Edge edge, DeviceProfileId deviceProfileId) {