|
|
@ -15,6 +15,8 @@ |
|
|
*/ |
|
|
*/ |
|
|
package org.thingsboard.server.service.edge; |
|
|
package org.thingsboard.server.service.edge; |
|
|
|
|
|
|
|
|
|
|
|
import com.fasterxml.jackson.databind.node.NullNode; |
|
|
|
|
|
import com.fasterxml.jackson.databind.node.ObjectNode; |
|
|
import lombok.RequiredArgsConstructor; |
|
|
import lombok.RequiredArgsConstructor; |
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
import org.springframework.stereotype.Component; |
|
|
import org.springframework.stereotype.Component; |
|
|
@ -23,6 +25,7 @@ import org.thingsboard.common.util.JacksonUtil; |
|
|
import org.thingsboard.server.cluster.TbClusterService; |
|
|
import org.thingsboard.server.cluster.TbClusterService; |
|
|
import org.thingsboard.server.common.data.OtaPackageInfo; |
|
|
import org.thingsboard.server.common.data.OtaPackageInfo; |
|
|
import org.thingsboard.server.common.data.User; |
|
|
import org.thingsboard.server.common.data.User; |
|
|
|
|
|
import org.thingsboard.server.common.data.alarm.Alarm; |
|
|
import org.thingsboard.server.common.data.alarm.AlarmApiCallResult; |
|
|
import org.thingsboard.server.common.data.alarm.AlarmApiCallResult; |
|
|
import org.thingsboard.server.common.data.edge.EdgeEventActionType; |
|
|
import org.thingsboard.server.common.data.edge.EdgeEventActionType; |
|
|
import org.thingsboard.server.common.data.edge.EdgeEventType; |
|
|
import org.thingsboard.server.common.data.edge.EdgeEventType; |
|
|
@ -36,6 +39,7 @@ import org.thingsboard.server.dao.eventsourcing.ActionEntityEvent; |
|
|
import org.thingsboard.server.dao.eventsourcing.DeleteEntityEvent; |
|
|
import org.thingsboard.server.dao.eventsourcing.DeleteEntityEvent; |
|
|
import org.thingsboard.server.dao.eventsourcing.RelationActionEvent; |
|
|
import org.thingsboard.server.dao.eventsourcing.RelationActionEvent; |
|
|
import org.thingsboard.server.dao.eventsourcing.SaveEntityEvent; |
|
|
import org.thingsboard.server.dao.eventsourcing.SaveEntityEvent; |
|
|
|
|
|
import org.thingsboard.server.dao.user.UserServiceImpl; |
|
|
|
|
|
|
|
|
import javax.annotation.PostConstruct; |
|
|
import javax.annotation.PostConstruct; |
|
|
|
|
|
|
|
|
@ -75,7 +79,7 @@ public class EdgeEventSourcingListener { |
|
|
return; |
|
|
return; |
|
|
} |
|
|
} |
|
|
try { |
|
|
try { |
|
|
if (!isValidEdgeEventEntity(event.getEntity())) { |
|
|
if (!isValidSaveEntityEventForEdgeProcessing(event.getEntity(), event.getOldEntity())) { |
|
|
return; |
|
|
return; |
|
|
} |
|
|
} |
|
|
log.trace("[{}] SaveEntityEvent called: {}", event.getTenantId(), event); |
|
|
log.trace("[{}] SaveEntityEvent called: {}", event.getTenantId(), event); |
|
|
@ -83,7 +87,7 @@ public class EdgeEventSourcingListener { |
|
|
tbClusterService.sendNotificationMsgToEdge(event.getTenantId(), null, event.getEntityId(), |
|
|
tbClusterService.sendNotificationMsgToEdge(event.getTenantId(), null, event.getEntityId(), |
|
|
null, null, action); |
|
|
null, null, action); |
|
|
} catch (Exception e) { |
|
|
} catch (Exception e) { |
|
|
log.error("[{}] failed to process SaveEntityEvent: {}", event.getTenantId(), event); |
|
|
log.error("[{}] failed to process SaveEntityEvent: {}", event.getTenantId(), event, e); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@ -97,7 +101,7 @@ public class EdgeEventSourcingListener { |
|
|
tbClusterService.sendNotificationMsgToEdge(event.getTenantId(), event.getEdgeId(), event.getEntityId(), |
|
|
tbClusterService.sendNotificationMsgToEdge(event.getTenantId(), event.getEdgeId(), event.getEntityId(), |
|
|
JacksonUtil.toString(event.getEntity()), null, EdgeEventActionType.DELETED); |
|
|
JacksonUtil.toString(event.getEntity()), null, EdgeEventActionType.DELETED); |
|
|
} catch (Exception e) { |
|
|
} catch (Exception e) { |
|
|
log.error("[{}] failed to process DeleteEntityEvent: {}", event.getTenantId(), event); |
|
|
log.error("[{}] failed to process DeleteEntityEvent: {}", event.getTenantId(), event, e); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@ -111,7 +115,7 @@ public class EdgeEventSourcingListener { |
|
|
tbClusterService.sendNotificationMsgToEdge(event.getTenantId(), event.getEdgeId(), event.getEntityId(), |
|
|
tbClusterService.sendNotificationMsgToEdge(event.getTenantId(), event.getEdgeId(), event.getEntityId(), |
|
|
event.getBody(), null, edgeTypeByActionType(event.getActionType())); |
|
|
event.getBody(), null, edgeTypeByActionType(event.getActionType())); |
|
|
} catch (Exception e) { |
|
|
} catch (Exception e) { |
|
|
log.error("[{}] failed to process ActionEntityEvent: {}", event.getTenantId(), event); |
|
|
log.error("[{}] failed to process ActionEntityEvent: {}", event.getTenantId(), event, e); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@ -134,11 +138,11 @@ public class EdgeEventSourcingListener { |
|
|
tbClusterService.sendNotificationMsgToEdge(event.getTenantId(), null, null, |
|
|
tbClusterService.sendNotificationMsgToEdge(event.getTenantId(), null, null, |
|
|
JacksonUtil.toString(relation), EdgeEventType.RELATION, edgeTypeByActionType(event.getActionType())); |
|
|
JacksonUtil.toString(relation), EdgeEventType.RELATION, edgeTypeByActionType(event.getActionType())); |
|
|
} catch (Exception e) { |
|
|
} catch (Exception e) { |
|
|
log.error("[{}] failed to process RelationActionEvent: {}", event.getTenantId(), event); |
|
|
log.error("[{}] failed to process RelationActionEvent: {}", event.getTenantId(), event, e); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
private boolean isValidEdgeEventEntity(Object entity) { |
|
|
private boolean isValidSaveEntityEventForEdgeProcessing(Object entity, Object oldEntity) { |
|
|
if (entity instanceof OtaPackageInfo) { |
|
|
if (entity instanceof OtaPackageInfo) { |
|
|
OtaPackageInfo otaPackageInfo = (OtaPackageInfo) entity; |
|
|
OtaPackageInfo otaPackageInfo = (OtaPackageInfo) entity; |
|
|
return otaPackageInfo.hasUrl() || otaPackageInfo.isHasData(); |
|
|
return otaPackageInfo.hasUrl() || otaPackageInfo.isHasData(); |
|
|
@ -147,12 +151,36 @@ public class EdgeEventSourcingListener { |
|
|
return RuleChainType.EDGE.equals(ruleChain.getType()); |
|
|
return RuleChainType.EDGE.equals(ruleChain.getType()); |
|
|
} else if (entity instanceof User) { |
|
|
} else if (entity instanceof User) { |
|
|
User user = (User) entity; |
|
|
User user = (User) entity; |
|
|
return !Authority.SYS_ADMIN.equals(user.getAuthority()); |
|
|
if (Authority.SYS_ADMIN.equals(user.getAuthority())) { |
|
|
} else if (entity instanceof AlarmApiCallResult) { |
|
|
return false; |
|
|
AlarmApiCallResult alarmApiCallResult = (AlarmApiCallResult) entity; |
|
|
} |
|
|
return alarmApiCallResult.isModified(); |
|
|
if (oldEntity != null) { |
|
|
|
|
|
User oldUser = (User) oldEntity; |
|
|
|
|
|
cleanUpUserAdditionalInfo(oldUser); |
|
|
|
|
|
cleanUpUserAdditionalInfo(user); |
|
|
|
|
|
return !user.equals(oldUser); |
|
|
|
|
|
} |
|
|
|
|
|
} else if (entity instanceof AlarmApiCallResult || entity instanceof Alarm) { |
|
|
|
|
|
return false; |
|
|
} |
|
|
} |
|
|
// Default: If the entity doesn't match any of the conditions, consider it as valid.
|
|
|
// Default: If the entity doesn't match any of the conditions, consider it as valid.
|
|
|
return true; |
|
|
return true; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private void cleanUpUserAdditionalInfo(User user) { |
|
|
|
|
|
// reset FAILED_LOGIN_ATTEMPTS and LAST_LOGIN_TS - edge is not interested in this information
|
|
|
|
|
|
if (user.getAdditionalInfo() instanceof NullNode) { |
|
|
|
|
|
user.setAdditionalInfo(null); |
|
|
|
|
|
} |
|
|
|
|
|
if (user.getAdditionalInfo() instanceof ObjectNode) { |
|
|
|
|
|
ObjectNode additionalInfo = ((ObjectNode) user.getAdditionalInfo()); |
|
|
|
|
|
additionalInfo.remove(UserServiceImpl.FAILED_LOGIN_ATTEMPTS); |
|
|
|
|
|
additionalInfo.remove(UserServiceImpl.LAST_LOGIN_TS); |
|
|
|
|
|
if (additionalInfo.isEmpty()) { |
|
|
|
|
|
user.setAdditionalInfo(null); |
|
|
|
|
|
} else { |
|
|
|
|
|
user.setAdditionalInfo(additionalInfo); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
|