Search in sources :

Example 1 with RuleChainId

use of org.thingsboard.server.common.data.id.RuleChainId in project thingsboard by thingsboard.

the class TbAlarmNodeTest method alarmCanBeUpdated.

@Test
public void alarmCanBeUpdated() throws ScriptException, IOException {
    initWithCreateAlarmScript();
    metaData.putValue("key", "value");
    TbMsg msg = TbMsg.newMsg("USER", originator, metaData, TbMsgDataType.JSON, rawJson, ruleChainId, ruleNodeId);
    long oldEndDate = System.currentTimeMillis();
    Alarm activeAlarm = Alarm.builder().type("SomeType").tenantId(tenantId).originator(originator).status(ACTIVE_UNACK).severity(WARNING).endTs(oldEndDate).build();
    when(detailsJs.executeJsonAsync(msg)).thenReturn(Futures.immediateFuture(null));
    when(alarmService.findLatestByOriginatorAndType(tenantId, originator, "SomeType")).thenReturn(Futures.immediateFuture(activeAlarm));
    doAnswer((Answer<Alarm>) invocationOnMock -> (Alarm) (invocationOnMock.getArguments())[0]).when(alarmService).createOrUpdateAlarm(activeAlarm);
    node.onMsg(ctx, msg);
    verify(ctx).enqueue(any(), successCaptor.capture(), failureCaptor.capture());
    successCaptor.getValue().run();
    verify(ctx).tellNext(any(), eq("Updated"));
    ArgumentCaptor<TbMsg> msgCaptor = ArgumentCaptor.forClass(TbMsg.class);
    ArgumentCaptor<String> typeCaptor = ArgumentCaptor.forClass(String.class);
    ArgumentCaptor<EntityId> originatorCaptor = ArgumentCaptor.forClass(EntityId.class);
    ArgumentCaptor<TbMsgMetaData> metadataCaptor = ArgumentCaptor.forClass(TbMsgMetaData.class);
    ArgumentCaptor<String> dataCaptor = ArgumentCaptor.forClass(String.class);
    verify(ctx).transformMsg(msgCaptor.capture(), typeCaptor.capture(), originatorCaptor.capture(), metadataCaptor.capture(), dataCaptor.capture());
    assertEquals("ALARM", typeCaptor.getValue());
    assertEquals(originator, originatorCaptor.getValue());
    assertEquals("value", metadataCaptor.getValue().getValue("key"));
    assertEquals(Boolean.TRUE.toString(), metadataCaptor.getValue().getValue(IS_EXISTING_ALARM));
    assertNotSame(metaData, metadataCaptor.getValue());
    Alarm actualAlarm = new ObjectMapper().readValue(dataCaptor.getValue().getBytes(), Alarm.class);
    assertTrue(activeAlarm.getEndTs() > oldEndDate);
    Alarm expectedAlarm = Alarm.builder().tenantId(tenantId).originator(originator).status(ACTIVE_UNACK).severity(CRITICAL).propagate(true).type("SomeType").details(null).endTs(activeAlarm.getEndTs()).build();
    assertEquals(expectedAlarm, actualAlarm);
}
Also used : CRITICAL(org.thingsboard.server.common.data.alarm.AlarmSeverity.CRITICAL) RuleEngineAlarmService(org.thingsboard.rule.engine.api.RuleEngineAlarmService) ArgumentMatchers.nullable(org.mockito.ArgumentMatchers.nullable) Assert.assertNotSame(org.junit.Assert.assertNotSame) RuleNodeId(org.thingsboard.server.common.data.id.RuleNodeId) TenantId(org.thingsboard.server.common.data.id.TenantId) TbMsgMetaData(org.thingsboard.server.common.msg.TbMsgMetaData) ACTIVE_UNACK(org.thingsboard.server.common.data.alarm.AlarmStatus.ACTIVE_UNACK) TbNodeException(org.thingsboard.rule.engine.api.TbNodeException) Mockito.verifyNoMoreInteractions(org.mockito.Mockito.verifyNoMoreInteractions) Uuids(com.datastax.oss.driver.api.core.uuid.Uuids) Mockito.doAnswer(org.mockito.Mockito.doAnswer) JsonNode(com.fasterxml.jackson.databind.JsonNode) ScriptException(javax.script.ScriptException) DeviceId(org.thingsboard.server.common.data.id.DeviceId) IS_NEW_ALARM(org.thingsboard.server.common.data.DataConstants.IS_NEW_ALARM) Alarm(org.thingsboard.server.common.data.alarm.Alarm) TbNodeConfiguration(org.thingsboard.rule.engine.api.TbNodeConfiguration) Mockito.atMost(org.mockito.Mockito.atMost) WARNING(org.thingsboard.server.common.data.alarm.AlarmSeverity.WARNING) RuleChainId(org.thingsboard.server.common.data.id.RuleChainId) MockitoJUnitRunner(org.mockito.junit.MockitoJUnitRunner) Mockito.eq(org.mockito.Mockito.eq) NotImplementedException(org.apache.commons.lang3.NotImplementedException) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) TbMsg(org.thingsboard.server.common.msg.TbMsg) Mock(org.mockito.Mock) IS_CLEARED_ALARM(org.thingsboard.server.common.data.DataConstants.IS_CLEARED_ALARM) RunWith(org.junit.runner.RunWith) Callable(java.util.concurrent.Callable) TbContext(org.thingsboard.rule.engine.api.TbContext) Captor(org.mockito.Captor) Answer(org.mockito.stubbing.Answer) ArgumentCaptor(org.mockito.ArgumentCaptor) ListeningExecutor(org.thingsboard.common.util.ListeningExecutor) EntityId(org.thingsboard.server.common.data.id.EntityId) ScriptEngine(org.thingsboard.rule.engine.api.ScriptEngine) CLEARED_UNACK(org.thingsboard.server.common.data.alarm.AlarmStatus.CLEARED_UNACK) Mockito.anyLong(org.mockito.Mockito.anyLong) Mockito.same(org.mockito.Mockito.same) IS_EXISTING_ALARM(org.thingsboard.server.common.data.DataConstants.IS_EXISTING_ALARM) Before(org.junit.Before) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) IOException(java.io.IOException) Mockito.times(org.mockito.Mockito.times) Mockito.when(org.mockito.Mockito.when) Mockito.verify(org.mockito.Mockito.verify) CLEARED_ACK(org.thingsboard.server.common.data.alarm.AlarmStatus.CLEARED_ACK) Consumer(java.util.function.Consumer) Futures(com.google.common.util.concurrent.Futures) AlarmId(org.thingsboard.server.common.data.id.AlarmId) TbMsgDataType(org.thingsboard.server.common.msg.TbMsgDataType) Assert.assertEquals(org.junit.Assert.assertEquals) EntityId(org.thingsboard.server.common.data.id.EntityId) Alarm(org.thingsboard.server.common.data.alarm.Alarm) TbMsgMetaData(org.thingsboard.server.common.msg.TbMsgMetaData) TbMsg(org.thingsboard.server.common.msg.TbMsg) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Example 2 with RuleChainId

use of org.thingsboard.server.common.data.id.RuleChainId in project thingsboard by thingsboard.

the class TbAlarmNodeTest method ifAlarmClearedCreateNew.

@Test
public void ifAlarmClearedCreateNew() throws ScriptException, IOException {
    initWithCreateAlarmScript();
    metaData.putValue("key", "value");
    TbMsg msg = TbMsg.newMsg("USER", originator, metaData, TbMsgDataType.JSON, rawJson, ruleChainId, ruleNodeId);
    long ts = msg.getTs();
    Alarm clearedAlarm = Alarm.builder().status(CLEARED_ACK).build();
    when(detailsJs.executeJsonAsync(msg)).thenReturn(Futures.immediateFuture(null));
    when(alarmService.findLatestByOriginatorAndType(tenantId, originator, "SomeType")).thenReturn(Futures.immediateFuture(clearedAlarm));
    doAnswer((Answer<Alarm>) invocationOnMock -> (Alarm) (invocationOnMock.getArguments())[0]).when(alarmService).createOrUpdateAlarm(any(Alarm.class));
    node.onMsg(ctx, msg);
    verify(ctx).enqueue(any(), successCaptor.capture(), failureCaptor.capture());
    successCaptor.getValue().run();
    verify(ctx).tellNext(any(), eq("Created"));
    ArgumentCaptor<TbMsg> msgCaptor = ArgumentCaptor.forClass(TbMsg.class);
    ArgumentCaptor<String> typeCaptor = ArgumentCaptor.forClass(String.class);
    ArgumentCaptor<EntityId> originatorCaptor = ArgumentCaptor.forClass(EntityId.class);
    ArgumentCaptor<TbMsgMetaData> metadataCaptor = ArgumentCaptor.forClass(TbMsgMetaData.class);
    ArgumentCaptor<String> dataCaptor = ArgumentCaptor.forClass(String.class);
    verify(ctx).transformMsg(msgCaptor.capture(), typeCaptor.capture(), originatorCaptor.capture(), metadataCaptor.capture(), dataCaptor.capture());
    assertEquals("ALARM", typeCaptor.getValue());
    assertEquals(originator, originatorCaptor.getValue());
    assertEquals("value", metadataCaptor.getValue().getValue("key"));
    assertEquals(Boolean.TRUE.toString(), metadataCaptor.getValue().getValue(IS_NEW_ALARM));
    assertNotSame(metaData, metadataCaptor.getValue());
    Alarm actualAlarm = new ObjectMapper().readValue(dataCaptor.getValue().getBytes(), Alarm.class);
    Alarm expectedAlarm = Alarm.builder().startTs(ts).endTs(ts).tenantId(tenantId).originator(originator).status(ACTIVE_UNACK).severity(CRITICAL).propagate(true).type("SomeType").details(null).build();
    assertEquals(expectedAlarm, actualAlarm);
}
Also used : CRITICAL(org.thingsboard.server.common.data.alarm.AlarmSeverity.CRITICAL) RuleEngineAlarmService(org.thingsboard.rule.engine.api.RuleEngineAlarmService) ArgumentMatchers.nullable(org.mockito.ArgumentMatchers.nullable) Assert.assertNotSame(org.junit.Assert.assertNotSame) RuleNodeId(org.thingsboard.server.common.data.id.RuleNodeId) TenantId(org.thingsboard.server.common.data.id.TenantId) TbMsgMetaData(org.thingsboard.server.common.msg.TbMsgMetaData) ACTIVE_UNACK(org.thingsboard.server.common.data.alarm.AlarmStatus.ACTIVE_UNACK) TbNodeException(org.thingsboard.rule.engine.api.TbNodeException) Mockito.verifyNoMoreInteractions(org.mockito.Mockito.verifyNoMoreInteractions) Uuids(com.datastax.oss.driver.api.core.uuid.Uuids) Mockito.doAnswer(org.mockito.Mockito.doAnswer) JsonNode(com.fasterxml.jackson.databind.JsonNode) ScriptException(javax.script.ScriptException) DeviceId(org.thingsboard.server.common.data.id.DeviceId) IS_NEW_ALARM(org.thingsboard.server.common.data.DataConstants.IS_NEW_ALARM) Alarm(org.thingsboard.server.common.data.alarm.Alarm) TbNodeConfiguration(org.thingsboard.rule.engine.api.TbNodeConfiguration) Mockito.atMost(org.mockito.Mockito.atMost) WARNING(org.thingsboard.server.common.data.alarm.AlarmSeverity.WARNING) RuleChainId(org.thingsboard.server.common.data.id.RuleChainId) MockitoJUnitRunner(org.mockito.junit.MockitoJUnitRunner) Mockito.eq(org.mockito.Mockito.eq) NotImplementedException(org.apache.commons.lang3.NotImplementedException) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) TbMsg(org.thingsboard.server.common.msg.TbMsg) Mock(org.mockito.Mock) IS_CLEARED_ALARM(org.thingsboard.server.common.data.DataConstants.IS_CLEARED_ALARM) RunWith(org.junit.runner.RunWith) Callable(java.util.concurrent.Callable) TbContext(org.thingsboard.rule.engine.api.TbContext) Captor(org.mockito.Captor) Answer(org.mockito.stubbing.Answer) ArgumentCaptor(org.mockito.ArgumentCaptor) ListeningExecutor(org.thingsboard.common.util.ListeningExecutor) EntityId(org.thingsboard.server.common.data.id.EntityId) ScriptEngine(org.thingsboard.rule.engine.api.ScriptEngine) CLEARED_UNACK(org.thingsboard.server.common.data.alarm.AlarmStatus.CLEARED_UNACK) Mockito.anyLong(org.mockito.Mockito.anyLong) Mockito.same(org.mockito.Mockito.same) IS_EXISTING_ALARM(org.thingsboard.server.common.data.DataConstants.IS_EXISTING_ALARM) Before(org.junit.Before) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) IOException(java.io.IOException) Mockito.times(org.mockito.Mockito.times) Mockito.when(org.mockito.Mockito.when) Mockito.verify(org.mockito.Mockito.verify) CLEARED_ACK(org.thingsboard.server.common.data.alarm.AlarmStatus.CLEARED_ACK) Consumer(java.util.function.Consumer) Futures(com.google.common.util.concurrent.Futures) AlarmId(org.thingsboard.server.common.data.id.AlarmId) TbMsgDataType(org.thingsboard.server.common.msg.TbMsgDataType) Assert.assertEquals(org.junit.Assert.assertEquals) EntityId(org.thingsboard.server.common.data.id.EntityId) Alarm(org.thingsboard.server.common.data.alarm.Alarm) TbMsgMetaData(org.thingsboard.server.common.msg.TbMsgMetaData) TbMsg(org.thingsboard.server.common.msg.TbMsg) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Example 3 with RuleChainId

use of org.thingsboard.server.common.data.id.RuleChainId in project thingsboard by thingsboard.

the class TbAlarmNodeTest method testCreateAlarmWithDynamicSeverityFromMessageBody.

@Test
public void testCreateAlarmWithDynamicSeverityFromMessageBody() throws Exception {
    TbCreateAlarmNodeConfiguration config = new TbCreateAlarmNodeConfiguration();
    config.setPropagate(true);
    config.setSeverity("$[alarmSeverity]");
    config.setAlarmType("SomeType");
    config.setAlarmDetailsBuildJs("DETAILS");
    config.setDynamicSeverity(true);
    ObjectMapper mapper = new ObjectMapper();
    TbNodeConfiguration nodeConfiguration = new TbNodeConfiguration(mapper.valueToTree(config));
    when(ctx.createJsScriptEngine("DETAILS")).thenReturn(detailsJs);
    when(ctx.getTenantId()).thenReturn(tenantId);
    when(ctx.getAlarmService()).thenReturn(alarmService);
    when(ctx.getDbCallbackExecutor()).thenReturn(dbExecutor);
    node = new TbCreateAlarmNode();
    node.init(ctx, nodeConfiguration);
    String rawJson = "{\"alarmSeverity\": \"WARNING\", \"passed\": 5}";
    metaData.putValue("key", "value");
    TbMsg msg = TbMsg.newMsg("USER", originator, metaData, TbMsgDataType.JSON, rawJson, ruleChainId, ruleNodeId);
    long ts = msg.getTs();
    when(detailsJs.executeJsonAsync(msg)).thenReturn(Futures.immediateFuture(null));
    when(alarmService.findLatestByOriginatorAndType(tenantId, originator, "SomeType")).thenReturn(Futures.immediateFuture(null));
    doAnswer((Answer<Alarm>) invocationOnMock -> (Alarm) (invocationOnMock.getArguments())[0]).when(alarmService).createOrUpdateAlarm(any(Alarm.class));
    node.onMsg(ctx, msg);
    verify(ctx).enqueue(any(), successCaptor.capture(), failureCaptor.capture());
    successCaptor.getValue().run();
    verify(ctx).tellNext(any(), eq("Created"));
    ArgumentCaptor<TbMsg> msgCaptor = ArgumentCaptor.forClass(TbMsg.class);
    ArgumentCaptor<String> typeCaptor = ArgumentCaptor.forClass(String.class);
    ArgumentCaptor<EntityId> originatorCaptor = ArgumentCaptor.forClass(EntityId.class);
    ArgumentCaptor<TbMsgMetaData> metadataCaptor = ArgumentCaptor.forClass(TbMsgMetaData.class);
    ArgumentCaptor<String> dataCaptor = ArgumentCaptor.forClass(String.class);
    verify(ctx).transformMsg(msgCaptor.capture(), typeCaptor.capture(), originatorCaptor.capture(), metadataCaptor.capture(), dataCaptor.capture());
    assertEquals("ALARM", typeCaptor.getValue());
    assertEquals(originator, originatorCaptor.getValue());
    assertEquals("value", metadataCaptor.getValue().getValue("key"));
    assertEquals(Boolean.TRUE.toString(), metadataCaptor.getValue().getValue(IS_NEW_ALARM));
    assertNotSame(metaData, metadataCaptor.getValue());
    Alarm actualAlarm = new ObjectMapper().readValue(dataCaptor.getValue().getBytes(), Alarm.class);
    Alarm expectedAlarm = Alarm.builder().startTs(ts).endTs(ts).tenantId(tenantId).originator(originator).status(ACTIVE_UNACK).severity(WARNING).propagate(true).type("SomeType").details(null).build();
    assertEquals(expectedAlarm, actualAlarm);
}
Also used : CRITICAL(org.thingsboard.server.common.data.alarm.AlarmSeverity.CRITICAL) RuleEngineAlarmService(org.thingsboard.rule.engine.api.RuleEngineAlarmService) ArgumentMatchers.nullable(org.mockito.ArgumentMatchers.nullable) Assert.assertNotSame(org.junit.Assert.assertNotSame) RuleNodeId(org.thingsboard.server.common.data.id.RuleNodeId) TenantId(org.thingsboard.server.common.data.id.TenantId) TbMsgMetaData(org.thingsboard.server.common.msg.TbMsgMetaData) ACTIVE_UNACK(org.thingsboard.server.common.data.alarm.AlarmStatus.ACTIVE_UNACK) TbNodeException(org.thingsboard.rule.engine.api.TbNodeException) Mockito.verifyNoMoreInteractions(org.mockito.Mockito.verifyNoMoreInteractions) Uuids(com.datastax.oss.driver.api.core.uuid.Uuids) Mockito.doAnswer(org.mockito.Mockito.doAnswer) JsonNode(com.fasterxml.jackson.databind.JsonNode) ScriptException(javax.script.ScriptException) DeviceId(org.thingsboard.server.common.data.id.DeviceId) IS_NEW_ALARM(org.thingsboard.server.common.data.DataConstants.IS_NEW_ALARM) Alarm(org.thingsboard.server.common.data.alarm.Alarm) TbNodeConfiguration(org.thingsboard.rule.engine.api.TbNodeConfiguration) Mockito.atMost(org.mockito.Mockito.atMost) WARNING(org.thingsboard.server.common.data.alarm.AlarmSeverity.WARNING) RuleChainId(org.thingsboard.server.common.data.id.RuleChainId) MockitoJUnitRunner(org.mockito.junit.MockitoJUnitRunner) Mockito.eq(org.mockito.Mockito.eq) NotImplementedException(org.apache.commons.lang3.NotImplementedException) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) TbMsg(org.thingsboard.server.common.msg.TbMsg) Mock(org.mockito.Mock) IS_CLEARED_ALARM(org.thingsboard.server.common.data.DataConstants.IS_CLEARED_ALARM) RunWith(org.junit.runner.RunWith) Callable(java.util.concurrent.Callable) TbContext(org.thingsboard.rule.engine.api.TbContext) Captor(org.mockito.Captor) Answer(org.mockito.stubbing.Answer) ArgumentCaptor(org.mockito.ArgumentCaptor) ListeningExecutor(org.thingsboard.common.util.ListeningExecutor) EntityId(org.thingsboard.server.common.data.id.EntityId) ScriptEngine(org.thingsboard.rule.engine.api.ScriptEngine) CLEARED_UNACK(org.thingsboard.server.common.data.alarm.AlarmStatus.CLEARED_UNACK) Mockito.anyLong(org.mockito.Mockito.anyLong) Mockito.same(org.mockito.Mockito.same) IS_EXISTING_ALARM(org.thingsboard.server.common.data.DataConstants.IS_EXISTING_ALARM) Before(org.junit.Before) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) IOException(java.io.IOException) Mockito.times(org.mockito.Mockito.times) Mockito.when(org.mockito.Mockito.when) Mockito.verify(org.mockito.Mockito.verify) CLEARED_ACK(org.thingsboard.server.common.data.alarm.AlarmStatus.CLEARED_ACK) Consumer(java.util.function.Consumer) Futures(com.google.common.util.concurrent.Futures) AlarmId(org.thingsboard.server.common.data.id.AlarmId) TbMsgDataType(org.thingsboard.server.common.msg.TbMsgDataType) Assert.assertEquals(org.junit.Assert.assertEquals) TbMsgMetaData(org.thingsboard.server.common.msg.TbMsgMetaData) TbNodeConfiguration(org.thingsboard.rule.engine.api.TbNodeConfiguration) EntityId(org.thingsboard.server.common.data.id.EntityId) Alarm(org.thingsboard.server.common.data.alarm.Alarm) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) TbMsg(org.thingsboard.server.common.msg.TbMsg) Test(org.junit.Test)

Example 4 with RuleChainId

use of org.thingsboard.server.common.data.id.RuleChainId in project thingsboard by thingsboard.

the class DefaultDataUpdateService method updateNestedRuleChains.

private void updateNestedRuleChains() {
    try {
        var packSize = 1024;
        var updated = 0;
        boolean hasNext = true;
        while (hasNext) {
            List<EntityRelation> relations = relationService.findRuleNodeToRuleChainRelations(TenantId.SYS_TENANT_ID, RuleChainType.CORE, packSize);
            hasNext = relations.size() == packSize;
            for (EntityRelation relation : relations) {
                try {
                    RuleNodeId sourceNodeId = new RuleNodeId(relation.getFrom().getId());
                    RuleNode sourceNode = ruleChainService.findRuleNodeById(TenantId.SYS_TENANT_ID, sourceNodeId);
                    if (sourceNode == null) {
                        log.info("Skip processing of relation for non existing source rule node: [{}]", sourceNodeId);
                        relationService.deleteRelation(TenantId.SYS_TENANT_ID, relation);
                        continue;
                    }
                    RuleChainId sourceRuleChainId = sourceNode.getRuleChainId();
                    RuleChainId targetRuleChainId = new RuleChainId(relation.getTo().getId());
                    RuleChain targetRuleChain = ruleChainService.findRuleChainById(TenantId.SYS_TENANT_ID, targetRuleChainId);
                    if (targetRuleChain == null) {
                        log.info("Skip processing of relation for non existing target rule chain: [{}]", targetRuleChainId);
                        relationService.deleteRelation(TenantId.SYS_TENANT_ID, relation);
                        continue;
                    }
                    TenantId tenantId = targetRuleChain.getTenantId();
                    RuleNode targetNode = new RuleNode();
                    targetNode.setName(targetRuleChain.getName());
                    targetNode.setRuleChainId(sourceRuleChainId);
                    targetNode.setType(TbRuleChainInputNode.class.getName());
                    TbRuleChainInputNodeConfiguration configuration = new TbRuleChainInputNodeConfiguration();
                    configuration.setRuleChainId(targetRuleChain.getId().toString());
                    targetNode.setConfiguration(JacksonUtil.valueToTree(configuration));
                    targetNode.setAdditionalInfo(relation.getAdditionalInfo());
                    targetNode.setDebugMode(false);
                    targetNode = ruleChainService.saveRuleNode(tenantId, targetNode);
                    EntityRelation sourceRuleChainToRuleNode = new EntityRelation();
                    sourceRuleChainToRuleNode.setFrom(sourceRuleChainId);
                    sourceRuleChainToRuleNode.setTo(targetNode.getId());
                    sourceRuleChainToRuleNode.setType(EntityRelation.CONTAINS_TYPE);
                    sourceRuleChainToRuleNode.setTypeGroup(RelationTypeGroup.RULE_CHAIN);
                    relationService.saveRelation(tenantId, sourceRuleChainToRuleNode);
                    EntityRelation sourceRuleNodeToTargetRuleNode = new EntityRelation();
                    sourceRuleNodeToTargetRuleNode.setFrom(sourceNode.getId());
                    sourceRuleNodeToTargetRuleNode.setTo(targetNode.getId());
                    sourceRuleNodeToTargetRuleNode.setType(relation.getType());
                    sourceRuleNodeToTargetRuleNode.setTypeGroup(RelationTypeGroup.RULE_NODE);
                    sourceRuleNodeToTargetRuleNode.setAdditionalInfo(relation.getAdditionalInfo());
                    relationService.saveRelation(tenantId, sourceRuleNodeToTargetRuleNode);
                    // Delete old relation
                    relationService.deleteRelation(tenantId, relation);
                    updated++;
                } catch (Exception e) {
                    log.info("Failed to update RuleNodeToRuleChainRelation: {}", relation, e);
                }
            }
            if (updated > 0) {
                log.info("RuleNodeToRuleChainRelations: {} entities updated so far...", updated);
            }
        }
    } catch (Exception e) {
        log.error("Unable to update Tenant", e);
    }
}
Also used : EntityRelation(org.thingsboard.server.common.data.relation.EntityRelation) TenantId(org.thingsboard.server.common.data.id.TenantId) RuleChain(org.thingsboard.server.common.data.rule.RuleChain) RuleNode(org.thingsboard.server.common.data.rule.RuleNode) TbRuleChainInputNodeConfiguration(org.thingsboard.rule.engine.flow.TbRuleChainInputNodeConfiguration) RuleChainId(org.thingsboard.server.common.data.id.RuleChainId) ExecutionException(java.util.concurrent.ExecutionException) RuleNodeId(org.thingsboard.server.common.data.id.RuleNodeId) TbRuleChainInputNode(org.thingsboard.rule.engine.flow.TbRuleChainInputNode)

Example 5 with RuleChainId

use of org.thingsboard.server.common.data.id.RuleChainId in project thingsboard by thingsboard.

the class DefaultTbClusterService method transformMsg.

private TbMsg transformMsg(TbMsg tbMsg, DeviceProfile deviceProfile) {
    if (deviceProfile != null) {
        RuleChainId targetRuleChainId = deviceProfile.getDefaultRuleChainId();
        String targetQueueName = deviceProfile.getDefaultQueueName();
        boolean isRuleChainTransform = targetRuleChainId != null && !targetRuleChainId.equals(tbMsg.getRuleChainId());
        boolean isQueueTransform = targetQueueName != null && !targetQueueName.equals(tbMsg.getQueueName());
        if (isRuleChainTransform && isQueueTransform) {
            tbMsg = TbMsg.transformMsg(tbMsg, targetRuleChainId, targetQueueName);
        } else if (isRuleChainTransform) {
            tbMsg = TbMsg.transformMsg(tbMsg, targetRuleChainId);
        } else if (isQueueTransform) {
            tbMsg = TbMsg.transformMsg(tbMsg, targetQueueName);
        }
    }
    return tbMsg;
}
Also used : RuleChainId(org.thingsboard.server.common.data.id.RuleChainId) ByteString(com.google.protobuf.ByteString)

Aggregations

RuleChainId (org.thingsboard.server.common.data.id.RuleChainId)60 RuleChain (org.thingsboard.server.common.data.rule.RuleChain)22 RuleNodeId (org.thingsboard.server.common.data.id.RuleNodeId)20 Test (org.junit.Test)17 TbMsg (org.thingsboard.server.common.msg.TbMsg)16 ThingsboardException (org.thingsboard.server.common.data.exception.ThingsboardException)12 EntityId (org.thingsboard.server.common.data.id.EntityId)12 ApiOperation (io.swagger.annotations.ApiOperation)11 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)11 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)11 TenantId (org.thingsboard.server.common.data.id.TenantId)11 TbMsgMetaData (org.thingsboard.server.common.msg.TbMsgMetaData)11 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)10 DeviceId (org.thingsboard.server.common.data.id.DeviceId)9 RuleNode (org.thingsboard.server.common.data.rule.RuleNode)8 Futures (com.google.common.util.concurrent.Futures)7 ListenableFuture (com.google.common.util.concurrent.ListenableFuture)7 IOException (java.io.IOException)7 Uuids (com.datastax.oss.driver.api.core.uuid.Uuids)6 JsonNode (com.fasterxml.jackson.databind.JsonNode)6