Browse Source

Fix compatibility between CE and PE

pull/9477/head
Andrii Shvaika 3 years ago
parent
commit
0571f7e6dd
  1. 22
      application/src/main/java/org/thingsboard/server/service/queue/ProtoUtils.java
  2. 40
      application/src/test/java/org/thingsboard/server/service/queue/ProtoUtilsTest.java
  3. 60
      common/cluster-api/src/main/proto/queue.proto
  4. 63
      common/data/src/main/java/org/thingsboard/server/common/data/EntityType.java
  5. 2
      common/message/src/main/java/org/thingsboard/server/common/msg/plugin/ComponentLifecycleMsg.java

22
application/src/main/java/org/thingsboard/server/service/queue/ProtoUtils.java

@ -15,33 +15,51 @@
*/
package org.thingsboard.server.service.queue;
import org.thingsboard.server.common.data.EntityType;
import org.thingsboard.server.common.data.id.EntityIdFactory;
import org.thingsboard.server.common.data.id.TenantId;
import org.thingsboard.server.common.data.plugin.ComponentLifecycleEvent;
import org.thingsboard.server.common.msg.plugin.ComponentLifecycleMsg;
import org.thingsboard.server.gen.transport.TransportProtos;
import java.util.Arrays;
import java.util.UUID;
public class ProtoUtils {
private static final EntityType[] entityTypeByProtoNumber;
static {
int arraySize = Arrays.stream(EntityType.values()).mapToInt(EntityType::getProtoNumber).max().orElse(0);
entityTypeByProtoNumber = new EntityType[arraySize + 1];
Arrays.stream(EntityType.values()).forEach(entityType -> entityTypeByProtoNumber[entityType.getProtoNumber()] = entityType);
}
public static TransportProtos.ComponentLifecycleMsgProto toProto(ComponentLifecycleMsg msg) {
return TransportProtos.ComponentLifecycleMsgProto.newBuilder()
.setTenantIdMSB(msg.getTenantId().getId().getMostSignificantBits())
.setTenantIdLSB(msg.getTenantId().getId().getLeastSignificantBits())
.setEntityType(TransportProtos.EntityType.forNumber(msg.getEntityId().getEntityType().ordinal()))
.setEntityType(toProto(msg.getEntityId().getEntityType()))
.setEntityIdMSB(msg.getEntityId().getId().getMostSignificantBits())
.setEntityIdLSB(msg.getEntityId().getId().getLeastSignificantBits())
.setEvent(TransportProtos.ComponentLifecycleEvent.forNumber(msg.getEvent().ordinal()))
.build();
}
public static TransportProtos.EntityTypeProto toProto(EntityType entityType) {
return TransportProtos.EntityTypeProto.forNumber(entityType.getProtoNumber());
}
public static ComponentLifecycleMsg fromProto(TransportProtos.ComponentLifecycleMsgProto proto) {
return new ComponentLifecycleMsg(
TenantId.fromUUID(new UUID(proto.getTenantIdMSB(), proto.getTenantIdLSB())),
EntityIdFactory.getByTypeAndUuid(proto.getEntityTypeValue(), new UUID(proto.getEntityIdMSB(), proto.getEntityIdLSB())),
EntityIdFactory.getByTypeAndUuid(fromProto(proto.getEntityType()), new UUID(proto.getEntityIdMSB(), proto.getEntityIdLSB())),
ComponentLifecycleEvent.values()[proto.getEventValue()]
);
}
public static EntityType fromProto(TransportProtos.EntityTypeProto entityType) {
return entityTypeByProtoNumber[entityType.getNumber()];
}
}

40
application/src/test/java/org/thingsboard/server/service/queue/ProtoUtilsTest.java

@ -16,6 +16,7 @@
package org.thingsboard.server.service.queue;
import org.junit.jupiter.api.Test;
import org.thingsboard.server.common.data.EntityType;
import org.thingsboard.server.common.data.id.EntityId;
import org.thingsboard.server.common.data.id.RuleChainId;
import org.thingsboard.server.common.data.id.TenantId;
@ -31,41 +32,18 @@ class ProtoUtilsTest {
TenantId tenantId = TenantId.fromUUID(UUID.fromString("35e10f77-16e7-424d-ae46-ee780f87ac4f"));
EntityId entityId = new RuleChainId(UUID.fromString("c640b635-4f0f-41e6-b10b-25a86003094e"));
@Test
void toProtoComponentLifecycleMsg() {
void protoComponentLifecycleSerialization() {
ComponentLifecycleMsg msg = new ComponentLifecycleMsg(tenantId, entityId, ComponentLifecycleEvent.UPDATED);
TransportProtos.ComponentLifecycleMsgProto proto = ProtoUtils.toProto(msg);
assertThat(proto).as("to proto").isEqualTo(TransportProtos.ComponentLifecycleMsgProto.newBuilder()
.setTenantIdMSB(tenantId.getId().getMostSignificantBits())
.setTenantIdLSB(tenantId.getId().getLeastSignificantBits())
.setEntityType(TransportProtos.EntityType.forNumber(entityId.getEntityType().ordinal()))
.setEntityIdMSB(entityId.getId().getMostSignificantBits())
.setEntityIdLSB(entityId.getId().getLeastSignificantBits())
.setEvent(TransportProtos.ComponentLifecycleEvent.forNumber(ComponentLifecycleEvent.UPDATED.ordinal()))
.build()
);
assertThat(ProtoUtils.fromProto(proto)).as("from proto").isEqualTo(msg);
assertThat(ProtoUtils.fromProto(ProtoUtils.toProto(msg))).as("deserialized").isEqualTo(msg);
msg = new ComponentLifecycleMsg(tenantId, entityId, ComponentLifecycleEvent.STARTED);
assertThat(ProtoUtils.fromProto(ProtoUtils.toProto(msg))).as("deserialized").isEqualTo(msg);
}
@Test
void fromProtoComponentLifecycleMsg() {
TransportProtos.ComponentLifecycleMsgProto proto = TransportProtos.ComponentLifecycleMsgProto.newBuilder()
.setTenantIdMSB(tenantId.getId().getMostSignificantBits())
.setTenantIdLSB(tenantId.getId().getLeastSignificantBits())
.setEntityType(TransportProtos.EntityType.forNumber(entityId.getEntityType().ordinal()))
.setEntityIdMSB(entityId.getId().getMostSignificantBits())
.setEntityIdLSB(entityId.getId().getLeastSignificantBits())
.setEvent(TransportProtos.ComponentLifecycleEvent.forNumber(ComponentLifecycleEvent.STARTED.ordinal()))
.build();
ComponentLifecycleMsg msg = ProtoUtils.fromProto(proto);
assertThat(msg).as("from proto").isEqualTo(
new ComponentLifecycleMsg(tenantId, entityId, ComponentLifecycleEvent.STARTED));
assertThat(ProtoUtils.toProto(msg)).as("to proto").isEqualTo(proto);
void protoEntityTypeSerialization() {
for(EntityType entityType : EntityType.values()){
assertThat(ProtoUtils.fromProto(ProtoUtils.toProto(entityType))).as(entityType.getNormalName()).isEqualTo(entityType);
}
}
}

60
common/cluster-api/src/main/proto/queue.proto

@ -23,33 +23,37 @@ option java_outer_classname = "TransportProtos";
/**
* Common data structures
*/
enum EntityType {
TENANT = 0;
CUSTOMER = 1;
USER = 2;
DASHBOARD = 3;
ASSET = 4;
DEVICE = 5;
ALARM = 6;
RULE_CHAIN = 7;
RULE_NODE = 8;
ENTITY_VIEW = 9;
WIDGETS_BUNDLE = 10;
WIDGET_TYPE = 11;
TENANT_PROFILE = 12;
DEVICE_PROFILE = 13;
ASSET_PROFILE = 14;
API_USAGE_STATE = 15;
TB_RESOURCE = 16;
OTA_PACKAGE = 17;
EDGE = 18;
RPC = 19;
QUEUE = 20;
NOTIFICATION_TARGET = 21;
NOTIFICATION_TEMPLATE = 22;
NOTIFICATION_REQUEST = 23;
NOTIFICATION = 24;
NOTIFICATION_RULE = 25;
enum EntityTypeProto {
UNSPECIFIED = 0;
TENANT = 1;
CUSTOMER = 2;
USER = 3;
DASHBOARD = 4;
ASSET = 5;
DEVICE = 6;
ALARM = 7;
// next 2 reserved for PE;
RULE_CHAIN = 10;
RULE_NODE = 11;
// next 2 reserved for PE;
ENTITY_VIEW = 14;
WIDGETS_BUNDLE = 15;
WIDGET_TYPE = 16;
// next 2 reserved for PE;
TENANT_PROFILE = 19;
DEVICE_PROFILE = 20;
ASSET_PROFILE = 21;
API_USAGE_STATE = 22;
TB_RESOURCE = 23;
OTA_PACKAGE = 24;
EDGE = 25;
RPC = 26;
QUEUE = 27;
NOTIFICATION_TARGET = 28;
NOTIFICATION_TEMPLATE = 29;
NOTIFICATION_REQUEST = 30;
NOTIFICATION = 31;
NOTIFICATION_RULE = 32;
}
/**
@ -776,7 +780,7 @@ enum ComponentLifecycleEvent {
message ComponentLifecycleMsgProto {
int64 tenantIdMSB = 1;
int64 tenantIdLSB = 2;
EntityType entityType = 3;
EntityTypeProto entityType = 3;
int64 entityIdMSB = 4;
int64 entityIdLSB = 5;
ComponentLifecycleEvent event = 6;

63
common/data/src/main/java/org/thingsboard/server/common/data/EntityType.java

@ -26,39 +26,46 @@ import java.util.stream.Collectors;
* @author Andrew Shvayka
*/
public enum EntityType {
// In sync with EntityType proto
TENANT,
CUSTOMER,
USER,
DASHBOARD,
ASSET,
DEVICE,
ALARM,
RULE_CHAIN,
RULE_NODE,
ENTITY_VIEW {
TENANT(1),
CUSTOMER(2),
USER(3),
DASHBOARD(4),
ASSET(5),
DEVICE(6),
ALARM (7),
RULE_CHAIN (10),
RULE_NODE (11),
ENTITY_VIEW (14) {
// backward compatibility for TbOriginatorTypeSwitchNode to return correct rule node connection.
@Override
public String getNormalName() {
public String getNormalName () {
return "Entity View";
}
},
WIDGETS_BUNDLE,
WIDGET_TYPE,
TENANT_PROFILE,
DEVICE_PROFILE,
ASSET_PROFILE,
API_USAGE_STATE,
TB_RESOURCE,
OTA_PACKAGE,
EDGE,
RPC,
QUEUE,
NOTIFICATION_TARGET,
NOTIFICATION_TEMPLATE,
NOTIFICATION_REQUEST,
NOTIFICATION,
NOTIFICATION_RULE;
WIDGETS_BUNDLE (15),
WIDGET_TYPE (16),
TENANT_PROFILE (19),
DEVICE_PROFILE (20),
ASSET_PROFILE (21),
API_USAGE_STATE (22),
TB_RESOURCE (23),
OTA_PACKAGE (24),
EDGE (25),
RPC (26),
QUEUE (27),
NOTIFICATION_TARGET (28),
NOTIFICATION_TEMPLATE (29),
NOTIFICATION_REQUEST (30),
NOTIFICATION (31),
NOTIFICATION_RULE (32);
@Getter
private final int protoNumber; // Corresponds to EntityTypeProto
private EntityType(int protoNumber) {
this.protoNumber = protoNumber;
}
public static final List<String> NORMAL_NAMES = EnumSet.allOf(EntityType.class).stream()
.map(EntityType::getNormalName).collect(Collectors.toUnmodifiableList());

2
common/message/src/main/java/org/thingsboard/server/common/msg/plugin/ComponentLifecycleMsg.java

@ -32,6 +32,8 @@ import java.util.Optional;
*/
@Data
public class ComponentLifecycleMsg implements TenantAwareMsg, ToAllNodesMsg {
private static final long serialVersionUID = -5303421482781273062L;
private final TenantId tenantId;
private final EntityId entityId;
private final ComponentLifecycleEvent event;

Loading…
Cancel
Save