Browse Source
Edge - notification rules for connection status and errors. Rate limits for Edge events.pull/10149/head
committed by
GitHub
39 changed files with 943 additions and 80 deletions
@ -0,0 +1,62 @@ |
|||
/** |
|||
* Copyright © 2016-2024 The Thingsboard Authors |
|||
* |
|||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|||
* you may not use this file except in compliance with the License. |
|||
* You may obtain a copy of the License at |
|||
* |
|||
* http://www.apache.org/licenses/LICENSE-2.0
|
|||
* |
|||
* Unless required by applicable law or agreed to in writing, software |
|||
* distributed under the License is distributed on an "AS IS" BASIS, |
|||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|||
* See the License for the specific language governing permissions and |
|||
* limitations under the License. |
|||
*/ |
|||
package org.thingsboard.server.service.notification.rule.trigger; |
|||
|
|||
import lombok.RequiredArgsConstructor; |
|||
import org.apache.commons.collections.CollectionUtils; |
|||
import org.springframework.stereotype.Service; |
|||
import org.thingsboard.server.common.data.notification.info.EdgeCommunicationFailureNotificationInfo; |
|||
import org.thingsboard.server.common.data.notification.info.RuleOriginatedNotificationInfo; |
|||
import org.thingsboard.server.common.data.notification.rule.trigger.EdgeCommunicationFailureTrigger; |
|||
import org.thingsboard.server.common.data.notification.rule.trigger.config.EdgeCommunicationFailureNotificationRuleTriggerConfig; |
|||
import org.thingsboard.server.common.data.notification.rule.trigger.config.NotificationRuleTriggerType; |
|||
|
|||
@Service |
|||
@RequiredArgsConstructor |
|||
public class EdgeCommunicationFailureTriggerProcessor implements NotificationRuleTriggerProcessor<EdgeCommunicationFailureTrigger, EdgeCommunicationFailureNotificationRuleTriggerConfig> { |
|||
|
|||
@Override |
|||
public boolean matchesFilter(EdgeCommunicationFailureTrigger trigger, EdgeCommunicationFailureNotificationRuleTriggerConfig triggerConfig) { |
|||
if (CollectionUtils.isNotEmpty(triggerConfig.getEdges())) { |
|||
return !triggerConfig.getEdges().contains(trigger.getEdgeId().getId()); |
|||
} |
|||
return true; |
|||
} |
|||
|
|||
@Override |
|||
public RuleOriginatedNotificationInfo constructNotificationInfo(EdgeCommunicationFailureTrigger trigger) { |
|||
return EdgeCommunicationFailureNotificationInfo.builder() |
|||
.tenantId(trigger.getTenantId()) |
|||
.edgeId(trigger.getEdgeId()) |
|||
.customerId(trigger.getCustomerId()) |
|||
.edgeName(trigger.getEdgeName()) |
|||
.failureMsg(truncateFailureMsg(trigger.getFailureMsg())) |
|||
.build(); |
|||
} |
|||
|
|||
@Override |
|||
public NotificationRuleTriggerType getTriggerType() { |
|||
return NotificationRuleTriggerType.EDGE_COMMUNICATION_FAILURE; |
|||
} |
|||
|
|||
private String truncateFailureMsg(String input) { |
|||
int maxLength = 500; |
|||
if (input != null && input.length() > maxLength) { |
|||
return input.substring(0, maxLength); |
|||
} |
|||
return input; |
|||
} |
|||
} |
|||
@ -0,0 +1,60 @@ |
|||
/** |
|||
* Copyright © 2016-2024 The Thingsboard Authors |
|||
* |
|||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|||
* you may not use this file except in compliance with the License. |
|||
* You may obtain a copy of the License at |
|||
* |
|||
* http://www.apache.org/licenses/LICENSE-2.0
|
|||
* |
|||
* Unless required by applicable law or agreed to in writing, software |
|||
* distributed under the License is distributed on an "AS IS" BASIS, |
|||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|||
* See the License for the specific language governing permissions and |
|||
* limitations under the License. |
|||
*/ |
|||
package org.thingsboard.server.service.notification.rule.trigger; |
|||
|
|||
import lombok.RequiredArgsConstructor; |
|||
import org.apache.commons.collections.CollectionUtils; |
|||
import org.springframework.stereotype.Service; |
|||
import org.thingsboard.server.common.data.notification.info.EdgeConnectionNotificationInfo; |
|||
import org.thingsboard.server.common.data.notification.info.RuleOriginatedNotificationInfo; |
|||
import org.thingsboard.server.common.data.notification.rule.trigger.EdgeConnectionTrigger; |
|||
import org.thingsboard.server.common.data.notification.rule.trigger.config.EdgeConnectionNotificationRuleTriggerConfig; |
|||
import org.thingsboard.server.common.data.notification.rule.trigger.config.EdgeConnectionNotificationRuleTriggerConfig.EdgeConnectivityEvent; |
|||
import org.thingsboard.server.common.data.notification.rule.trigger.config.NotificationRuleTriggerType; |
|||
|
|||
@Service |
|||
@RequiredArgsConstructor |
|||
public class EdgeConnectionTriggerProcessor implements NotificationRuleTriggerProcessor<EdgeConnectionTrigger, EdgeConnectionNotificationRuleTriggerConfig> { |
|||
|
|||
@Override |
|||
public boolean matchesFilter(EdgeConnectionTrigger trigger, EdgeConnectionNotificationRuleTriggerConfig triggerConfig) { |
|||
EdgeConnectivityEvent event = trigger.isConnected() ? EdgeConnectivityEvent.CONNECTED : EdgeConnectivityEvent.DISCONNECTED; |
|||
if (CollectionUtils.isEmpty(triggerConfig.getNotifyOn()) || !triggerConfig.getNotifyOn().contains(event)) { |
|||
return false; |
|||
} |
|||
if (CollectionUtils.isNotEmpty(triggerConfig.getEdges())) { |
|||
return triggerConfig.getEdges().contains(trigger.getEdgeId().getId()); |
|||
} |
|||
return true; |
|||
} |
|||
|
|||
@Override |
|||
public RuleOriginatedNotificationInfo constructNotificationInfo(EdgeConnectionTrigger trigger) { |
|||
return EdgeConnectionNotificationInfo.builder() |
|||
.eventType(trigger.isConnected() ? "connected" : "disconnected") |
|||
.tenantId(trigger.getTenantId()) |
|||
.customerId(trigger.getCustomerId()) |
|||
.edgeId(trigger.getEdgeId()) |
|||
.edgeName(trigger.getEdgeName()) |
|||
.build(); |
|||
} |
|||
|
|||
@Override |
|||
public NotificationRuleTriggerType getTriggerType() { |
|||
return NotificationRuleTriggerType.EDGE_CONNECTION; |
|||
} |
|||
|
|||
} |
|||
@ -0,0 +1,66 @@ |
|||
/** |
|||
* Copyright © 2016-2024 The Thingsboard Authors |
|||
* |
|||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|||
* you may not use this file except in compliance with the License. |
|||
* You may obtain a copy of the License at |
|||
* |
|||
* http://www.apache.org/licenses/LICENSE-2.0
|
|||
* |
|||
* Unless required by applicable law or agreed to in writing, software |
|||
* distributed under the License is distributed on an "AS IS" BASIS, |
|||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|||
* See the License for the specific language governing permissions and |
|||
* limitations under the License. |
|||
*/ |
|||
package org.thingsboard.server.common.data.notification.info; |
|||
|
|||
import lombok.AllArgsConstructor; |
|||
import lombok.Builder; |
|||
import lombok.Data; |
|||
import lombok.NoArgsConstructor; |
|||
import org.thingsboard.server.common.data.id.CustomerId; |
|||
import org.thingsboard.server.common.data.id.EdgeId; |
|||
import org.thingsboard.server.common.data.id.EntityId; |
|||
import org.thingsboard.server.common.data.id.TenantId; |
|||
|
|||
import java.util.Map; |
|||
|
|||
import static org.thingsboard.server.common.data.util.CollectionsUtil.mapOf; |
|||
|
|||
@Data |
|||
@NoArgsConstructor |
|||
@AllArgsConstructor |
|||
@Builder |
|||
public class EdgeCommunicationFailureNotificationInfo implements RuleOriginatedNotificationInfo { |
|||
|
|||
private TenantId tenantId; |
|||
private CustomerId customerId; |
|||
private EdgeId edgeId; |
|||
private String edgeName; |
|||
private String failureMsg; |
|||
|
|||
@Override |
|||
public Map<String, String> getTemplateData() { |
|||
return mapOf( |
|||
"edgeId", edgeId.toString(), |
|||
"edgeName", edgeName, |
|||
"failureMsg", failureMsg |
|||
); |
|||
} |
|||
|
|||
@Override |
|||
public TenantId getAffectedTenantId() { |
|||
return tenantId; |
|||
} |
|||
|
|||
@Override |
|||
public CustomerId getAffectedCustomerId() { |
|||
return customerId; |
|||
} |
|||
|
|||
@Override |
|||
public EntityId getStateEntityId() { |
|||
return edgeId; |
|||
} |
|||
} |
|||
@ -0,0 +1,66 @@ |
|||
/** |
|||
* Copyright © 2016-2024 The Thingsboard Authors |
|||
* |
|||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|||
* you may not use this file except in compliance with the License. |
|||
* You may obtain a copy of the License at |
|||
* |
|||
* http://www.apache.org/licenses/LICENSE-2.0
|
|||
* |
|||
* Unless required by applicable law or agreed to in writing, software |
|||
* distributed under the License is distributed on an "AS IS" BASIS, |
|||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|||
* See the License for the specific language governing permissions and |
|||
* limitations under the License. |
|||
*/ |
|||
package org.thingsboard.server.common.data.notification.info; |
|||
|
|||
import lombok.AllArgsConstructor; |
|||
import lombok.Builder; |
|||
import lombok.Data; |
|||
import lombok.NoArgsConstructor; |
|||
import org.thingsboard.server.common.data.id.CustomerId; |
|||
import org.thingsboard.server.common.data.id.EdgeId; |
|||
import org.thingsboard.server.common.data.id.EntityId; |
|||
import org.thingsboard.server.common.data.id.TenantId; |
|||
|
|||
import java.util.Map; |
|||
|
|||
import static org.thingsboard.server.common.data.util.CollectionsUtil.mapOf; |
|||
|
|||
@Data |
|||
@NoArgsConstructor |
|||
@AllArgsConstructor |
|||
@Builder |
|||
public class EdgeConnectionNotificationInfo implements RuleOriginatedNotificationInfo { |
|||
|
|||
private String eventType; |
|||
private TenantId tenantId; |
|||
private CustomerId customerId; |
|||
private EdgeId edgeId; |
|||
private String edgeName; |
|||
|
|||
@Override |
|||
public Map<String, String> getTemplateData() { |
|||
return mapOf( |
|||
"eventType", eventType, |
|||
"edgeId", edgeId.toString(), |
|||
"edgeName", edgeName |
|||
); |
|||
} |
|||
|
|||
@Override |
|||
public TenantId getAffectedTenantId() { |
|||
return tenantId; |
|||
} |
|||
|
|||
@Override |
|||
public CustomerId getAffectedCustomerId() { |
|||
return customerId; |
|||
} |
|||
|
|||
@Override |
|||
public EntityId getStateEntityId() { |
|||
return edgeId; |
|||
} |
|||
} |
|||
@ -0,0 +1,63 @@ |
|||
/** |
|||
* Copyright © 2016-2024 The Thingsboard Authors |
|||
* |
|||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|||
* you may not use this file except in compliance with the License. |
|||
* You may obtain a copy of the License at |
|||
* |
|||
* http://www.apache.org/licenses/LICENSE-2.0
|
|||
* |
|||
* Unless required by applicable law or agreed to in writing, software |
|||
* distributed under the License is distributed on an "AS IS" BASIS, |
|||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|||
* See the License for the specific language governing permissions and |
|||
* limitations under the License. |
|||
*/ |
|||
package org.thingsboard.server.common.data.notification.rule.trigger; |
|||
|
|||
import lombok.Builder; |
|||
import lombok.Data; |
|||
import org.thingsboard.server.common.data.id.CustomerId; |
|||
import org.thingsboard.server.common.data.id.EdgeId; |
|||
import org.thingsboard.server.common.data.id.EntityId; |
|||
import org.thingsboard.server.common.data.id.TenantId; |
|||
import org.thingsboard.server.common.data.notification.rule.trigger.config.NotificationRuleTriggerType; |
|||
|
|||
import java.util.concurrent.TimeUnit; |
|||
|
|||
@Data |
|||
@Builder |
|||
public class EdgeCommunicationFailureTrigger implements NotificationRuleTrigger { |
|||
|
|||
private final TenantId tenantId; |
|||
private final CustomerId customerId; |
|||
private final EdgeId edgeId; |
|||
private final String edgeName; |
|||
private final String failureMsg; |
|||
private final String error; |
|||
|
|||
@Override |
|||
public boolean deduplicate() { |
|||
return true; |
|||
} |
|||
|
|||
@Override |
|||
public String getDeduplicationKey() { |
|||
return String.join(":", NotificationRuleTrigger.super.getDeduplicationKey(), error); |
|||
} |
|||
|
|||
@Override |
|||
public long getDefaultDeduplicationDuration() { |
|||
return TimeUnit.MINUTES.toMillis(30); |
|||
} |
|||
|
|||
@Override |
|||
public NotificationRuleTriggerType getType() { |
|||
return NotificationRuleTriggerType.EDGE_COMMUNICATION_FAILURE; |
|||
} |
|||
|
|||
@Override |
|||
public EntityId getOriginatorEntityId() { |
|||
return edgeId; |
|||
} |
|||
} |
|||
@ -0,0 +1,62 @@ |
|||
/** |
|||
* Copyright © 2016-2024 The Thingsboard Authors |
|||
* |
|||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|||
* you may not use this file except in compliance with the License. |
|||
* You may obtain a copy of the License at |
|||
* |
|||
* http://www.apache.org/licenses/LICENSE-2.0
|
|||
* |
|||
* Unless required by applicable law or agreed to in writing, software |
|||
* distributed under the License is distributed on an "AS IS" BASIS, |
|||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|||
* See the License for the specific language governing permissions and |
|||
* limitations under the License. |
|||
*/ |
|||
package org.thingsboard.server.common.data.notification.rule.trigger; |
|||
|
|||
import lombok.Builder; |
|||
import lombok.Data; |
|||
import org.thingsboard.server.common.data.id.CustomerId; |
|||
import org.thingsboard.server.common.data.id.EdgeId; |
|||
import org.thingsboard.server.common.data.id.EntityId; |
|||
import org.thingsboard.server.common.data.id.TenantId; |
|||
import org.thingsboard.server.common.data.notification.rule.trigger.config.NotificationRuleTriggerType; |
|||
|
|||
import java.util.concurrent.TimeUnit; |
|||
|
|||
@Data |
|||
@Builder |
|||
public class EdgeConnectionTrigger implements NotificationRuleTrigger { |
|||
|
|||
private final TenantId tenantId; |
|||
private final CustomerId customerId; |
|||
private final EdgeId edgeId; |
|||
private final boolean connected; |
|||
private final String edgeName; |
|||
|
|||
@Override |
|||
public boolean deduplicate() { |
|||
return true; |
|||
} |
|||
|
|||
@Override |
|||
public String getDeduplicationKey() { |
|||
return String.join(":", NotificationRuleTrigger.super.getDeduplicationKey(), String.valueOf(connected)); |
|||
} |
|||
|
|||
@Override |
|||
public long getDefaultDeduplicationDuration() { |
|||
return TimeUnit.MINUTES.toMillis(1); |
|||
} |
|||
|
|||
@Override |
|||
public NotificationRuleTriggerType getType() { |
|||
return NotificationRuleTriggerType.EDGE_CONNECTION; |
|||
} |
|||
|
|||
@Override |
|||
public EntityId getOriginatorEntityId() { |
|||
return edgeId; |
|||
} |
|||
} |
|||
@ -0,0 +1,39 @@ |
|||
/** |
|||
* Copyright © 2016-2024 The Thingsboard Authors |
|||
* |
|||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|||
* you may not use this file except in compliance with the License. |
|||
* You may obtain a copy of the License at |
|||
* |
|||
* http://www.apache.org/licenses/LICENSE-2.0
|
|||
* |
|||
* Unless required by applicable law or agreed to in writing, software |
|||
* distributed under the License is distributed on an "AS IS" BASIS, |
|||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|||
* See the License for the specific language governing permissions and |
|||
* limitations under the License. |
|||
*/ |
|||
package org.thingsboard.server.common.data.notification.rule.trigger.config; |
|||
|
|||
import lombok.AllArgsConstructor; |
|||
import lombok.Builder; |
|||
import lombok.Data; |
|||
import lombok.NoArgsConstructor; |
|||
|
|||
import java.util.Set; |
|||
import java.util.UUID; |
|||
|
|||
@Data |
|||
@NoArgsConstructor |
|||
@AllArgsConstructor |
|||
@Builder |
|||
public class EdgeCommunicationFailureNotificationRuleTriggerConfig implements NotificationRuleTriggerConfig { |
|||
|
|||
private Set<UUID> edges; // if empty - all edges
|
|||
|
|||
@Override |
|||
public NotificationRuleTriggerType getTriggerType() { |
|||
return NotificationRuleTriggerType.EDGE_COMMUNICATION_FAILURE; |
|||
} |
|||
|
|||
} |
|||
@ -0,0 +1,44 @@ |
|||
/** |
|||
* Copyright © 2016-2024 The Thingsboard Authors |
|||
* |
|||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|||
* you may not use this file except in compliance with the License. |
|||
* You may obtain a copy of the License at |
|||
* |
|||
* http://www.apache.org/licenses/LICENSE-2.0
|
|||
* |
|||
* Unless required by applicable law or agreed to in writing, software |
|||
* distributed under the License is distributed on an "AS IS" BASIS, |
|||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|||
* See the License for the specific language governing permissions and |
|||
* limitations under the License. |
|||
*/ |
|||
package org.thingsboard.server.common.data.notification.rule.trigger.config; |
|||
|
|||
import lombok.AllArgsConstructor; |
|||
import lombok.Builder; |
|||
import lombok.Data; |
|||
import lombok.NoArgsConstructor; |
|||
|
|||
import java.util.Set; |
|||
import java.util.UUID; |
|||
|
|||
@Data |
|||
@NoArgsConstructor |
|||
@AllArgsConstructor |
|||
@Builder |
|||
public class EdgeConnectionNotificationRuleTriggerConfig implements NotificationRuleTriggerConfig { |
|||
|
|||
private Set<UUID> edges; // if empty - all edges
|
|||
private Set<EdgeConnectivityEvent> notifyOn; |
|||
|
|||
@Override |
|||
public NotificationRuleTriggerType getTriggerType() { |
|||
return NotificationRuleTriggerType.EDGE_CONNECTION; |
|||
} |
|||
|
|||
public enum EdgeConnectivityEvent { |
|||
CONNECTED, DISCONNECTED |
|||
} |
|||
|
|||
} |
|||
@ -0,0 +1,57 @@ |
|||
#### Edge communication failure notification templatization |
|||
|
|||
<div class="divider"></div> |
|||
<br/> |
|||
|
|||
Notification subject and message fields support templatization. |
|||
The list of available templatization parameters depends on the template type. |
|||
See the available types and parameters below: |
|||
|
|||
Available template parameters: |
|||
|
|||
* `edgeId` - the edge id as uuid string; |
|||
* `edgeName` - the name of the edge; |
|||
* `failureMsg` - the string representation of the failure, occurred on the Edge; |
|||
|
|||
Parameter names must be wrapped using `${...}`. For example: `${edgeName}`. |
|||
You may also modify the value of the parameter with one of the suffixes: |
|||
|
|||
* `upperCase`, for example - `${edgeName:upperCase}` |
|||
* `lowerCase`, for example - `${edgeName:lowerCase}` |
|||
* `capitalize`, for example - `${edgeName:capitalize}` |
|||
|
|||
<div class="divider"></div> |
|||
|
|||
##### Examples |
|||
|
|||
Let's assume the notification about the failing of processing connection to Edge. |
|||
The following template: |
|||
|
|||
```text |
|||
Edge '${edgeName}' communication failure occurred |
|||
{:copy-code} |
|||
``` |
|||
|
|||
will be transformed to: |
|||
|
|||
```text |
|||
Edge 'DatacenterEdge' communication failure occurred |
|||
``` |
|||
|
|||
<br/> |
|||
|
|||
The following template: |
|||
|
|||
```text |
|||
Failure message: '${failureMsg}' |
|||
{:copy-code} |
|||
``` |
|||
|
|||
will be transformed to: |
|||
|
|||
```text |
|||
Failure message: 'Failed to process edge connection!' |
|||
``` |
|||
|
|||
<br> |
|||
<br> |
|||
@ -0,0 +1,44 @@ |
|||
#### Edge connection notification templatization |
|||
|
|||
<div class="divider"></div> |
|||
<br/> |
|||
|
|||
Notification subject and message fields support templatization. |
|||
The list of available templatization parameters depends on the template type. |
|||
See the available types and parameters below: |
|||
|
|||
Available template parameters: |
|||
|
|||
* `edgeId` - the edge id as uuid string; |
|||
* `edgeName` - the name of the edge; |
|||
* `eventType` - the string representation of the connectivity status: connected or disconnected; |
|||
|
|||
Parameter names must be wrapped using `${...}`. For example: `${edgeName}`. |
|||
You may also modify the value of the parameter with one of the suffixes: |
|||
|
|||
* `upperCase`, for example - `${edgeName:upperCase}` |
|||
* `lowerCase`, for example - `${edgeName:lowerCase}` |
|||
* `capitalize`, for example - `${edgeName:capitalize}` |
|||
|
|||
<div class="divider"></div> |
|||
|
|||
##### Examples |
|||
|
|||
Let's assume the notification about the connecting Edge into the ThingsBoard. |
|||
The following template: |
|||
|
|||
```text |
|||
Edge '${edgeName}' is now ${eventType} |
|||
{:copy-code} |
|||
``` |
|||
|
|||
will be transformed to: |
|||
|
|||
```text |
|||
Edge 'DatacenterEdge' is now connected |
|||
``` |
|||
|
|||
<br/> |
|||
|
|||
<br> |
|||
<br> |
|||
Loading…
Reference in new issue