Merge pull request #13455 from thingsboard/cf-refactoring
Proto mapping refactoring
This commit is contained in:
commit
c8151ccb84
@ -18,6 +18,7 @@ package org.thingsboard.server.common.data;
|
||||
import lombok.Getter;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.EnumSet;
|
||||
import java.util.List;
|
||||
|
||||
@ -76,6 +77,15 @@ public enum EntityType {
|
||||
public static final List<String> NORMAL_NAMES = EnumSet.allOf(EntityType.class).stream()
|
||||
.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) {
|
||||
this.protoNumber = protoNumber;
|
||||
this.tableName = name().toLowerCase();
|
||||
@ -98,4 +108,11 @@ public enum EntityType {
|
||||
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];
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -15,12 +15,42 @@
|
||||
*/
|
||||
package org.thingsboard.server.common.data.plugin;
|
||||
|
||||
import java.io.Serializable;
|
||||
import lombok.Getter;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
|
||||
/**
|
||||
* @author Andrew Shvayka
|
||||
*/
|
||||
import java.io.Serializable;
|
||||
import java.util.Arrays;
|
||||
|
||||
@RequiredArgsConstructor
|
||||
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];
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -102,7 +102,6 @@ import org.thingsboard.server.gen.transport.TransportProtos.ApiUsageRecordKeyPro
|
||||
import org.thingsboard.server.gen.transport.TransportProtos.KeyValueProto;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
@ -114,14 +113,6 @@ import static org.thingsboard.server.common.data.DataConstants.GATEWAY_PARAMETER
|
||||
@Slf4j
|
||||
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) {
|
||||
var builder = TransportProtos.ComponentLifecycleMsgProto.newBuilder()
|
||||
.setTenantIdMSB(msg.getTenantId().getId().getMostSignificantBits())
|
||||
@ -129,7 +120,7 @@ public class ProtoUtils {
|
||||
.setEntityType(toProto(msg.getEntityId().getEntityType()))
|
||||
.setEntityIdMSB(msg.getEntityId().getId().getMostSignificantBits())
|
||||
.setEntityIdLSB(msg.getEntityId().getId().getLeastSignificantBits())
|
||||
.setEvent(TransportProtos.ComponentLifecycleEvent.forNumber(msg.getEvent().ordinal()));
|
||||
.setEvent(toProto(msg.getEvent()));
|
||||
if (msg.getProfileId() != null) {
|
||||
builder.setProfileIdMSB(msg.getProfileId().getId().getMostSignificantBits());
|
||||
builder.setProfileIdLSB(msg.getProfileId().getId().getLeastSignificantBits());
|
||||
@ -156,7 +147,7 @@ public class ProtoUtils {
|
||||
var builder = ComponentLifecycleMsg.builder()
|
||||
.tenantId(TenantId.fromUUID(new UUID(proto.getTenantIdMSB(), proto.getTenantIdLSB())))
|
||||
.entityId(entityId)
|
||||
.event(ComponentLifecycleEvent.values()[proto.getEventValue()]);
|
||||
.event(fromProto(proto.getEvent()));
|
||||
if (!StringUtils.isEmpty(proto.getName())) {
|
||||
builder.name(proto.getName());
|
||||
}
|
||||
@ -175,7 +166,15 @@ public class ProtoUtils {
|
||||
}
|
||||
|
||||
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) {
|
||||
|
||||
@ -114,6 +114,13 @@ class ProtoUtilsTest {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void protoComponentLifecycleEventSerialization() {
|
||||
for (ComponentLifecycleEvent event : ComponentLifecycleEvent.values()) {
|
||||
assertThat(ProtoUtils.fromProto(ProtoUtils.toProto(event))).isEqualTo(event);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void protoEdgeHighPrioritySerialization() {
|
||||
EdgeHighPriorityMsg msg = new EdgeHighPriorityMsg(tenantId, EdgeUtils.constructEdgeEvent(tenantId, edgeId,
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user