Add proto number for ComponentLifecycleEvent

This commit is contained in:
ViacheslavKlimov 2025-05-27 18:26:19 +03:00
parent 772c565619
commit 80a33492d5
4 changed files with 70 additions and 17 deletions

View File

@ -18,6 +18,7 @@ package org.thingsboard.server.common.data;
import lombok.Getter; import lombok.Getter;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import java.util.Arrays;
import java.util.EnumSet; import java.util.EnumSet;
import java.util.List; import java.util.List;
@ -76,6 +77,15 @@ public enum EntityType {
public static final List<String> NORMAL_NAMES = EnumSet.allOf(EntityType.class).stream() public static final List<String> NORMAL_NAMES = EnumSet.allOf(EntityType.class).stream()
.map(EntityType::getNormalName).toList(); .map(EntityType::getNormalName).toList();
private static final EntityType[] BY_PROTO;
static {
BY_PROTO = new EntityType[Arrays.stream(values()).mapToInt(EntityType::getProtoNumber).max().orElse(0) + 1];
for (EntityType entityType : values()) {
BY_PROTO[entityType.getProtoNumber()] = entityType;
}
}
EntityType(int protoNumber) { EntityType(int protoNumber) {
this.protoNumber = protoNumber; this.protoNumber = protoNumber;
this.tableName = name().toLowerCase(); this.tableName = name().toLowerCase();
@ -98,4 +108,11 @@ public enum EntityType {
return false; return false;
} }
public static EntityType forProtoNumber(int protoNumber) {
if (protoNumber < 0 || protoNumber >= BY_PROTO.length) {
throw new IllegalArgumentException("Invalid EntityType proto number " + protoNumber);
}
return BY_PROTO[protoNumber];
}
} }

View File

@ -15,12 +15,42 @@
*/ */
package org.thingsboard.server.common.data.plugin; package org.thingsboard.server.common.data.plugin;
import java.io.Serializable; import lombok.Getter;
import lombok.RequiredArgsConstructor;
/** import java.io.Serializable;
* @author Andrew Shvayka import java.util.Arrays;
*/
@RequiredArgsConstructor
public enum ComponentLifecycleEvent implements Serializable { public enum ComponentLifecycleEvent implements Serializable {
// In sync with ComponentLifecycleEvent proto
CREATED, STARTED, ACTIVATED, SUSPENDED, UPDATED, STOPPED, DELETED, FAILED, DEACTIVATED CREATED(0),
STARTED(1),
ACTIVATED(2),
SUSPENDED(3),
UPDATED(4),
STOPPED(5),
DELETED(6),
FAILED(7),
DEACTIVATED(8);
@Getter
private final int protoNumber; // corresponds to ComponentLifecycleEvent proto
private static final ComponentLifecycleEvent[] BY_PROTO;
static {
BY_PROTO = new ComponentLifecycleEvent[Arrays.stream(values()).mapToInt(ComponentLifecycleEvent::getProtoNumber).max().orElse(0) + 1];
for (ComponentLifecycleEvent event : values()) {
BY_PROTO[event.getProtoNumber()] = event;
}
}
public static ComponentLifecycleEvent forProtoNumber(int protoNumber) {
if (protoNumber < 0 || protoNumber >= BY_PROTO.length) {
throw new IllegalArgumentException("Invalid ComponentLifecycleEvent proto number " + protoNumber);
}
return BY_PROTO[protoNumber];
}
} }

View File

@ -102,7 +102,6 @@ import org.thingsboard.server.gen.transport.TransportProtos.ApiUsageRecordKeyPro
import org.thingsboard.server.gen.transport.TransportProtos.KeyValueProto; import org.thingsboard.server.gen.transport.TransportProtos.KeyValueProto;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
import java.util.UUID; import java.util.UUID;
@ -114,14 +113,6 @@ import static org.thingsboard.server.common.data.DataConstants.GATEWAY_PARAMETER
@Slf4j @Slf4j
public class ProtoUtils { 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) { public static TransportProtos.ComponentLifecycleMsgProto toProto(ComponentLifecycleMsg msg) {
var builder = TransportProtos.ComponentLifecycleMsgProto.newBuilder() var builder = TransportProtos.ComponentLifecycleMsgProto.newBuilder()
.setTenantIdMSB(msg.getTenantId().getId().getMostSignificantBits()) .setTenantIdMSB(msg.getTenantId().getId().getMostSignificantBits())
@ -129,7 +120,7 @@ public class ProtoUtils {
.setEntityType(toProto(msg.getEntityId().getEntityType())) .setEntityType(toProto(msg.getEntityId().getEntityType()))
.setEntityIdMSB(msg.getEntityId().getId().getMostSignificantBits()) .setEntityIdMSB(msg.getEntityId().getId().getMostSignificantBits())
.setEntityIdLSB(msg.getEntityId().getId().getLeastSignificantBits()) .setEntityIdLSB(msg.getEntityId().getId().getLeastSignificantBits())
.setEvent(TransportProtos.ComponentLifecycleEvent.forNumber(msg.getEvent().ordinal())); .setEvent(TransportProtos.ComponentLifecycleEvent.forNumber(msg.getEvent().getProtoNumber()));
if (msg.getProfileId() != null) { if (msg.getProfileId() != null) {
builder.setProfileIdMSB(msg.getProfileId().getId().getMostSignificantBits()); builder.setProfileIdMSB(msg.getProfileId().getId().getMostSignificantBits());
builder.setProfileIdLSB(msg.getProfileId().getId().getLeastSignificantBits()); builder.setProfileIdLSB(msg.getProfileId().getId().getLeastSignificantBits());
@ -175,7 +166,15 @@ public class ProtoUtils {
} }
public static EntityType fromProto(TransportProtos.EntityTypeProto entityType) { public static EntityType fromProto(TransportProtos.EntityTypeProto entityType) {
return entityTypeByProtoNumber[entityType.getNumber()]; return EntityType.forProtoNumber(entityType.getNumber());
}
public static TransportProtos.ComponentLifecycleEvent toProto(ComponentLifecycleEvent event) {
return TransportProtos.ComponentLifecycleEvent.forNumber(event.getProtoNumber());
}
public static ComponentLifecycleEvent fromProto(TransportProtos.ComponentLifecycleEvent eventProto) {
return ComponentLifecycleEvent.forProtoNumber(eventProto.getNumber());
} }
public static TransportProtos.ToEdgeSyncRequestMsgProto toProto(ToEdgeSyncRequest request) { public static TransportProtos.ToEdgeSyncRequestMsgProto toProto(ToEdgeSyncRequest request) {

View File

@ -114,6 +114,13 @@ class ProtoUtilsTest {
} }
} }
@Test
void protoComponentLifecycleEventSerialization() {
for (ComponentLifecycleEvent event : ComponentLifecycleEvent.values()) {
assertThat(ProtoUtils.fromProto(ProtoUtils.toProto(event))).isEqualTo(event);
}
}
@Test @Test
void protoEdgeHighPrioritySerialization() { void protoEdgeHighPrioritySerialization() {
EdgeHighPriorityMsg msg = new EdgeHighPriorityMsg(tenantId, EdgeUtils.constructEdgeEvent(tenantId, edgeId, EdgeHighPriorityMsg msg = new EdgeHighPriorityMsg(tenantId, EdgeUtils.constructEdgeEvent(tenantId, edgeId,