Browse Source

Cleanup version during sending to cloud

pull/15092/head
Volodymyr Babak 7 months ago
parent
commit
d237007721
  1. 22
      application/src/main/java/org/thingsboard/server/service/edge/EdgeMsgConstructorUtils.java
  2. 3
      application/src/main/java/org/thingsboard/server/service/edge/rpc/processor/BaseEdgeProcessor.java
  3. 181
      application/src/test/java/org/thingsboard/server/service/edge/EdgeMsgConstructorUtilsTest.java

22
application/src/main/java/org/thingsboard/server/service/edge/EdgeMsgConstructorUtils.java

@ -41,6 +41,7 @@ import org.thingsboard.server.common.data.DataConstants;
import org.thingsboard.server.common.data.Device;
import org.thingsboard.server.common.data.DeviceProfile;
import org.thingsboard.server.common.data.EntityView;
import org.thingsboard.server.common.data.HasVersion;
import org.thingsboard.server.common.data.OtaPackage;
import org.thingsboard.server.common.data.StringUtils;
import org.thingsboard.server.common.data.TbResource;
@ -193,6 +194,10 @@ public class EdgeMsgConstructorUtils {
)
);
private static void resetVersion(HasVersion entity) {
entity.setVersion(null);
}
public static AlarmUpdateMsg constructAlarmUpdatedMsg(UpdateMsgType msgType, Alarm alarm) {
return AlarmUpdateMsg.newBuilder().setMsgType(msgType)
.setEntity(JacksonUtil.toString(alarm))
@ -205,6 +210,7 @@ public class EdgeMsgConstructorUtils {
}
public static AssetUpdateMsg constructAssetUpdatedMsg(UpdateMsgType msgType, Asset asset) {
resetVersion(asset);
return AssetUpdateMsg.newBuilder().setMsgType(msgType).setEntity(JacksonUtil.toString(asset))
.setIdMSB(asset.getUuidId().getMostSignificantBits())
.setIdLSB(asset.getUuidId().getLeastSignificantBits()).build();
@ -218,6 +224,7 @@ public class EdgeMsgConstructorUtils {
}
public static AssetProfileUpdateMsg constructAssetProfileUpdatedMsg(UpdateMsgType msgType, AssetProfile assetProfile) {
resetVersion(assetProfile);
return AssetProfileUpdateMsg.newBuilder().setMsgType(msgType).setEntity(JacksonUtil.toString(assetProfile))
.setIdMSB(assetProfile.getId().getId().getMostSignificantBits())
.setIdLSB(assetProfile.getId().getId().getLeastSignificantBits()).build();
@ -231,6 +238,7 @@ public class EdgeMsgConstructorUtils {
}
public static CustomerUpdateMsg constructCustomerUpdatedMsg(UpdateMsgType msgType, Customer customer) {
resetVersion(customer);
return CustomerUpdateMsg.newBuilder().setMsgType(msgType).setEntity(JacksonUtil.toString(customer))
.setIdMSB(customer.getId().getId().getMostSignificantBits())
.setIdLSB(customer.getId().getId().getLeastSignificantBits()).build();
@ -244,6 +252,7 @@ public class EdgeMsgConstructorUtils {
}
public static DashboardUpdateMsg constructDashboardUpdatedMsg(UpdateMsgType msgType, Dashboard dashboard) {
resetVersion(dashboard);
return DashboardUpdateMsg.newBuilder().setMsgType(msgType).setEntity(JacksonUtil.toString(dashboard))
.setIdMSB(dashboard.getId().getId().getMostSignificantBits())
.setIdLSB(dashboard.getId().getId().getLeastSignificantBits()).build();
@ -257,6 +266,7 @@ public class EdgeMsgConstructorUtils {
}
public static DeviceUpdateMsg constructDeviceUpdatedMsg(UpdateMsgType msgType, Device device) {
resetVersion(device);
return DeviceUpdateMsg.newBuilder().setMsgType(msgType).setEntity(JacksonUtil.toString(device))
.setIdMSB(device.getId().getId().getMostSignificantBits())
.setIdLSB(device.getId().getId().getLeastSignificantBits()).build();
@ -270,10 +280,12 @@ public class EdgeMsgConstructorUtils {
}
public static DeviceCredentialsUpdateMsg constructDeviceCredentialsUpdatedMsg(DeviceCredentials deviceCredentials) {
resetVersion(deviceCredentials);
return DeviceCredentialsUpdateMsg.newBuilder().setEntity(JacksonUtil.toString(deviceCredentials)).build();
}
public static DeviceProfileUpdateMsg constructDeviceProfileUpdatedMsg(UpdateMsgType msgType, DeviceProfile deviceProfile, EdgeVersion edgeVersion) {
resetVersion(deviceProfile);
String entity = getEntityAndFixLwm2mBootstrapShortServerId(deviceProfile, edgeVersion);
return DeviceProfileUpdateMsg.newBuilder().setMsgType(msgType).setEntity(entity)
.setIdMSB(deviceProfile.getId().getId().getMostSignificantBits())
@ -387,6 +399,7 @@ public class EdgeMsgConstructorUtils {
}
public static EntityViewUpdateMsg constructEntityViewUpdatedMsg(UpdateMsgType msgType, EntityView entityView) {
resetVersion(entityView);
return EntityViewUpdateMsg.newBuilder().setMsgType(msgType).setEntity(JacksonUtil.toString(entityView))
.setIdMSB(entityView.getId().getId().getMostSignificantBits())
.setIdLSB(entityView.getId().getId().getLeastSignificantBits()).build();
@ -486,6 +499,7 @@ public class EdgeMsgConstructorUtils {
}
public static RelationUpdateMsg constructRelationUpdatedMsg(UpdateMsgType msgType, EntityRelation entityRelation) {
resetVersion(entityRelation);
return RelationUpdateMsg.newBuilder().setMsgType(msgType).setEntity(JacksonUtil.toString(entityRelation)).build();
}
@ -503,6 +517,7 @@ public class EdgeMsgConstructorUtils {
}
public static RuleChainUpdateMsg constructRuleChainUpdatedMsg(UpdateMsgType msgType, RuleChain ruleChain, boolean isRoot) {
resetVersion(ruleChain);
boolean isTemplateRoot = ruleChain.isRoot();
ruleChain.setRoot(isRoot);
RuleChainUpdateMsg result = RuleChainUpdateMsg.newBuilder().setMsgType(msgType).setEntity(JacksonUtil.toString(ruleChain))
@ -520,6 +535,7 @@ public class EdgeMsgConstructorUtils {
}
public static RuleChainMetadataUpdateMsg constructRuleChainMetadataUpdatedMsg(UpdateMsgType msgType, RuleChainMetaData ruleChainMetaData, EdgeVersion edgeVersion) {
resetVersion(ruleChainMetaData);
String metaData = sanitizeMetadataForLegacyEdgeVersion(ruleChainMetaData, edgeVersion);
return RuleChainMetadataUpdateMsg.newBuilder()
@ -640,6 +656,7 @@ public class EdgeMsgConstructorUtils {
}
public static TenantUpdateMsg constructTenantUpdateMsg(UpdateMsgType msgType, Tenant tenant) {
resetVersion(tenant);
return TenantUpdateMsg.newBuilder().setMsgType(msgType).setEntity(JacksonUtil.toString(tenant)).build();
}
@ -648,6 +665,7 @@ public class EdgeMsgConstructorUtils {
}
public static UserUpdateMsg constructUserUpdatedMsg(UpdateMsgType msgType, User user) {
resetVersion(user);
return UserUpdateMsg.newBuilder().setMsgType(msgType).setEntity(JacksonUtil.toString(user))
.setIdMSB(user.getId().getId().getMostSignificantBits())
.setIdLSB(user.getId().getId().getLeastSignificantBits()).build();
@ -665,6 +683,7 @@ public class EdgeMsgConstructorUtils {
}
public static WidgetsBundleUpdateMsg constructWidgetsBundleUpdateMsg(UpdateMsgType msgType, WidgetsBundle widgetsBundle, List<String> widgets) {
resetVersion(widgetsBundle);
return WidgetsBundleUpdateMsg.newBuilder().setMsgType(msgType).setEntity(JacksonUtil.toString(widgetsBundle))
.setWidgets(JacksonUtil.toString(widgets))
.setIdMSB(widgetsBundle.getId().getId().getMostSignificantBits())
@ -680,6 +699,7 @@ public class EdgeMsgConstructorUtils {
}
public static WidgetTypeUpdateMsg constructWidgetTypeUpdateMsg(UpdateMsgType msgType, WidgetTypeDetails widgetTypeDetails) {
resetVersion(widgetTypeDetails);
return WidgetTypeUpdateMsg.newBuilder().setMsgType(msgType).setEntity(JacksonUtil.toString(widgetTypeDetails))
.setIdMSB(widgetTypeDetails.getId().getId().getMostSignificantBits())
.setIdLSB(widgetTypeDetails.getId().getId().getLeastSignificantBits()).build();
@ -694,6 +714,7 @@ public class EdgeMsgConstructorUtils {
}
public static CalculatedFieldUpdateMsg constructCalculatedFieldUpdatedMsg(UpdateMsgType msgType, CalculatedField calculatedField) {
resetVersion(calculatedField);
return CalculatedFieldUpdateMsg.newBuilder().setMsgType(msgType).setEntity(JacksonUtil.toString(calculatedField))
.setIdMSB(calculatedField.getId().getId().getMostSignificantBits())
.setIdLSB(calculatedField.getId().getId().getLeastSignificantBits()).build();
@ -707,6 +728,7 @@ public class EdgeMsgConstructorUtils {
}
public static AiModelUpdateMsg constructAiModelUpdatedMsg(UpdateMsgType msgType, AiModel aiModel) {
resetVersion(aiModel);
return AiModelUpdateMsg.newBuilder().setMsgType(msgType).setEntity(JacksonUtil.toString(aiModel))
.setIdMSB(aiModel.getId().getId().getMostSignificantBits())
.setIdLSB(aiModel.getId().getId().getLeastSignificantBits()).build();

3
application/src/main/java/org/thingsboard/server/service/edge/rpc/processor/BaseEdgeProcessor.java

@ -412,6 +412,9 @@ public abstract class BaseEdgeProcessor implements EdgeProcessor {
}
protected boolean isSaveRequired(HasVersion current, HasVersion updated) {
if (current != null) {
current.setVersion(null);
}
updated.setVersion(null);
return !updated.equals(current);
}

181
application/src/test/java/org/thingsboard/server/service/edge/EdgeMsgConstructorUtilsTest.java

@ -38,12 +38,30 @@ import org.thingsboard.rule.engine.rest.TbSendRestApiCallReplyNode;
import org.thingsboard.rule.engine.telemetry.TbCalculatedFieldsNode;
import org.thingsboard.rule.engine.telemetry.TbMsgAttributesNode;
import org.thingsboard.rule.engine.telemetry.TbMsgTimeseriesNode;
import org.thingsboard.server.common.data.Customer;
import org.thingsboard.server.common.data.Dashboard;
import org.thingsboard.server.common.data.Device;
import org.thingsboard.server.common.data.EntityView;
import org.thingsboard.server.common.data.Tenant;
import org.thingsboard.server.common.data.User;
import org.thingsboard.server.common.data.asset.Asset;
import org.thingsboard.server.common.data.asset.AssetProfile;
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.AssetId;
import org.thingsboard.server.common.data.id.AssetProfileId;
import org.thingsboard.server.common.data.id.CustomerId;
import org.thingsboard.server.common.data.id.DashboardId;
import org.thingsboard.server.common.data.id.DeviceId;
import org.thingsboard.server.common.data.id.EntityViewId;
import org.thingsboard.server.common.data.id.TenantId;
import org.thingsboard.server.common.data.id.UserId;
import org.thingsboard.server.common.data.relation.EntityRelation;
import org.thingsboard.server.common.data.rule.RuleChain;
import org.thingsboard.server.common.data.rule.RuleChainMetaData;
import org.thingsboard.server.common.data.rule.RuleNode;
import org.thingsboard.server.common.data.security.DeviceCredentials;
import org.thingsboard.server.gen.edge.v1.EdgeVersion;
import org.thingsboard.server.gen.edge.v1.UpdateMsgType;
@ -278,4 +296,167 @@ public class EdgeMsgConstructorUtilsTest {
edgeEvent.setBody(body);
return edgeEvent;
}
@Test
public void testConstructAssetUpdatedMsg_versionIsReset() {
Asset asset = new Asset();
asset.setId(new AssetId(UUID.randomUUID()));
asset.setName("Test Asset");
asset.setVersion(42L);
String entity = EdgeMsgConstructorUtils.constructAssetUpdatedMsg(UpdateMsgType.ENTITY_UPDATED_RPC_MESSAGE, asset).getEntity();
JsonNode json = JacksonUtil.toJsonNode(entity);
Assertions.assertTrue(json.get("version") == null || json.get("version").isNull(),
"Asset version should be null in serialized message");
}
@Test
public void testConstructAssetProfileUpdatedMsg_versionIsReset() {
AssetProfile assetProfile = new AssetProfile();
assetProfile.setId(new AssetProfileId(UUID.randomUUID()));
assetProfile.setName("Test Asset Profile");
assetProfile.setVersion(42L);
String entity = EdgeMsgConstructorUtils.constructAssetProfileUpdatedMsg(UpdateMsgType.ENTITY_UPDATED_RPC_MESSAGE, assetProfile).getEntity();
JsonNode json = JacksonUtil.toJsonNode(entity);
Assertions.assertTrue(json.get("version") == null || json.get("version").isNull(),
"AssetProfile version should be null in serialized message");
}
@Test
public void testConstructCustomerUpdatedMsg_versionIsReset() {
Customer customer = new Customer();
customer.setId(new CustomerId(UUID.randomUUID()));
customer.setTitle("Test Customer");
customer.setVersion(42L);
String entity = EdgeMsgConstructorUtils.constructCustomerUpdatedMsg(UpdateMsgType.ENTITY_UPDATED_RPC_MESSAGE, customer).getEntity();
JsonNode json = JacksonUtil.toJsonNode(entity);
Assertions.assertTrue(json.get("version") == null || json.get("version").isNull(),
"Customer version should be null in serialized message");
}
@Test
public void testConstructDashboardUpdatedMsg_versionIsReset() {
Dashboard dashboard = new Dashboard();
dashboard.setId(new DashboardId(UUID.randomUUID()));
dashboard.setTitle("Test Dashboard");
dashboard.setVersion(42L);
String entity = EdgeMsgConstructorUtils.constructDashboardUpdatedMsg(UpdateMsgType.ENTITY_UPDATED_RPC_MESSAGE, dashboard).getEntity();
JsonNode json = JacksonUtil.toJsonNode(entity);
Assertions.assertTrue(json.get("version") == null || json.get("version").isNull(),
"Dashboard version should be null in serialized message");
}
@Test
public void testConstructDeviceUpdatedMsg_versionIsReset() {
Device device = new Device();
device.setId(new DeviceId(UUID.randomUUID()));
device.setName("Test Device");
device.setVersion(42L);
String entity = EdgeMsgConstructorUtils.constructDeviceUpdatedMsg(UpdateMsgType.ENTITY_UPDATED_RPC_MESSAGE, device).getEntity();
JsonNode json = JacksonUtil.toJsonNode(entity);
Assertions.assertTrue(json.get("version") == null || json.get("version").isNull(),
"Device version should be null in serialized message");
}
@Test
public void testConstructDeviceCredentialsUpdatedMsg_versionIsReset() {
DeviceCredentials credentials = new DeviceCredentials();
credentials.setVersion(42L);
String entity = EdgeMsgConstructorUtils.constructDeviceCredentialsUpdatedMsg(credentials).getEntity();
JsonNode json = JacksonUtil.toJsonNode(entity);
Assertions.assertTrue(json.get("version") == null || json.get("version").isNull(),
"DeviceCredentials version should be null in serialized message");
}
@Test
public void testConstructEntityViewUpdatedMsg_versionIsReset() {
EntityView entityView = new EntityView();
entityView.setId(new EntityViewId(UUID.randomUUID()));
entityView.setName("Test EntityView");
entityView.setVersion(42L);
String entity = EdgeMsgConstructorUtils.constructEntityViewUpdatedMsg(UpdateMsgType.ENTITY_UPDATED_RPC_MESSAGE, entityView).getEntity();
JsonNode json = JacksonUtil.toJsonNode(entity);
Assertions.assertTrue(json.get("version") == null || json.get("version").isNull(),
"EntityView version should be null in serialized message");
}
@Test
public void testConstructRelationUpdatedMsg_versionIsReset() {
EntityRelation relation = new EntityRelation();
relation.setVersion(42L);
String entity = EdgeMsgConstructorUtils.constructRelationUpdatedMsg(UpdateMsgType.ENTITY_UPDATED_RPC_MESSAGE, relation).getEntity();
JsonNode json = JacksonUtil.toJsonNode(entity);
Assertions.assertTrue(json.get("version") == null || json.get("version").isNull(),
"EntityRelation version should be null in serialized message");
}
@Test
public void testConstructRuleChainUpdatedMsg_versionIsReset() {
RuleChain ruleChain = new RuleChain();
ruleChain.setId(new org.thingsboard.server.common.data.id.RuleChainId(UUID.randomUUID()));
ruleChain.setName("Test RuleChain");
ruleChain.setVersion(42L);
String entity = EdgeMsgConstructorUtils.constructRuleChainUpdatedMsg(UpdateMsgType.ENTITY_UPDATED_RPC_MESSAGE, ruleChain, false).getEntity();
JsonNode json = JacksonUtil.toJsonNode(entity);
Assertions.assertTrue(json.get("version") == null || json.get("version").isNull(),
"RuleChain version should be null in serialized message");
}
@Test
public void testConstructTenantUpdateMsg_versionIsReset() {
Tenant tenant = new Tenant();
tenant.setId(TenantId.fromUUID(UUID.randomUUID()));
tenant.setTitle("Test Tenant");
tenant.setVersion(42L);
String entity = EdgeMsgConstructorUtils.constructTenantUpdateMsg(UpdateMsgType.ENTITY_UPDATED_RPC_MESSAGE, tenant).getEntity();
JsonNode json = JacksonUtil.toJsonNode(entity);
Assertions.assertTrue(json.get("version") == null || json.get("version").isNull(),
"Tenant version should be null in serialized message");
}
@Test
public void testConstructUserUpdatedMsg_versionIsReset() {
User user = new User();
user.setId(new UserId(UUID.randomUUID()));
user.setEmail("test@test.com");
user.setVersion(42L);
String entity = EdgeMsgConstructorUtils.constructUserUpdatedMsg(UpdateMsgType.ENTITY_UPDATED_RPC_MESSAGE, user).getEntity();
JsonNode json = JacksonUtil.toJsonNode(entity);
Assertions.assertTrue(json.get("version") == null || json.get("version").isNull(),
"User version should be null in serialized message");
}
@Test
public void testConstructRuleChainMetadataUpdatedMsg_versionIsReset() {
RuleChainMetaData metaData = new RuleChainMetaData();
metaData.setVersion(42L);
String entity = EdgeMsgConstructorUtils.constructRuleChainMetadataUpdatedMsg(
UpdateMsgType.ENTITY_UPDATED_RPC_MESSAGE, metaData, EdgeVersion.V_4_0_0).getEntity();
JsonNode json = JacksonUtil.toJsonNode(entity);
Assertions.assertTrue(json.get("version") == null || json.get("version").isNull(),
"RuleChainMetaData version should be null in serialized message");
}
}

Loading…
Cancel
Save