Search in sources :

Example 21 with RuleNodeId

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

the class TbTransformMsgNodeTest method metadataCanBeUpdated.

@Test
public void metadataCanBeUpdated() throws TbNodeException, ScriptException {
    initWithScript();
    TbMsgMetaData metaData = new TbMsgMetaData();
    metaData.putValue("temp", "7");
    String rawJson = "{\"passed\": 5}";
    RuleChainId ruleChainId = new RuleChainId(Uuids.timeBased());
    RuleNodeId ruleNodeId = new RuleNodeId(Uuids.timeBased());
    TbMsg msg = TbMsg.newMsg("USER", null, metaData, TbMsgDataType.JSON, rawJson, ruleChainId, ruleNodeId);
    TbMsg transformedMsg = TbMsg.newMsg("USER", null, metaData, TbMsgDataType.JSON, "{new}", ruleChainId, ruleNodeId);
    when(scriptEngine.executeUpdateAsync(msg)).thenReturn(Futures.immediateFuture(Collections.singletonList(transformedMsg)));
    node.onMsg(ctx, msg);
    verify(ctx).getDbCallbackExecutor();
    ArgumentCaptor<TbMsg> captor = ArgumentCaptor.forClass(TbMsg.class);
    verify(ctx).tellSuccess(captor.capture());
    TbMsg actualMsg = captor.getValue();
    assertEquals(transformedMsg, actualMsg);
}
Also used : RuleChainId(org.thingsboard.server.common.data.id.RuleChainId) TbMsgMetaData(org.thingsboard.server.common.msg.TbMsgMetaData) TbMsg(org.thingsboard.server.common.msg.TbMsg) RuleNodeId(org.thingsboard.server.common.data.id.RuleNodeId) Test(org.junit.Test)

Example 22 with RuleNodeId

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

the class TbAlarmNodeTest method testCreateAlarmWithDynamicSeverityFromMetadata.

@Test
public void testCreateAlarmWithDynamicSeverityFromMetadata() 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);
    metaData.putValue("alarmSeverity", "WARNING");
    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(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 23 with RuleNodeId

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

the class TbAlarmNodeTest method testCreateAlarmsWithPropagationToTenantWithDynamicTypes.

@Test
public void testCreateAlarmsWithPropagationToTenantWithDynamicTypes() throws Exception {
    for (int i = 0; i < 10; i++) {
        var config = new TbCreateAlarmNodeConfiguration();
        config.setPropagateToTenant(true);
        config.setSeverity(CRITICAL.name());
        config.setAlarmType("SomeType" + i);
        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);
        metaData.putValue("key", "value");
        TbMsg msg = TbMsg.newMsg("USER", originator, metaData, TbMsgDataType.JSON, rawJson, ruleChainId, ruleNodeId);
        when(detailsJs.executeJsonAsync(msg)).thenReturn(Futures.immediateFuture(null));
        when(alarmService.findLatestByOriginatorAndType(tenantId, originator, "SomeType" + i)).thenReturn(Futures.immediateFuture(null));
        doAnswer((Answer<Alarm>) invocationOnMock -> (Alarm) (invocationOnMock.getArguments())[0]).when(alarmService).createOrUpdateAlarm(any(Alarm.class));
        long ts = msg.getTs();
        node.onMsg(ctx, msg);
        verify(ctx, atMost(10)).enqueue(any(), successCaptor.capture(), failureCaptor.capture());
        successCaptor.getValue().run();
        verify(ctx, atMost(10)).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, atMost(10)).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).propagateToTenant(true).type("SomeType" + i).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 24 with RuleNodeId

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

the class TbAlarmNodeTest method newAlarmCanBeCreated.

@Test
public void newAlarmCanBeCreated() throws ScriptException, IOException {
    initWithCreateAlarmScript();
    metaData.putValue("key", "value");
    TbMsg msg = TbMsg.newMsg("USER", originator, metaData, TbMsgDataType.JSON, rawJson, ruleChainId, ruleNodeId);
    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));
    long ts = msg.getTs();
    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 25 with RuleNodeId

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

the class BaseRuleChainService method loadRuleChainMetaData.

@Override
public RuleChainMetaData loadRuleChainMetaData(TenantId tenantId, RuleChainId ruleChainId) {
    Validator.validateId(ruleChainId, "Incorrect rule chain id.");
    RuleChain ruleChain = findRuleChainById(tenantId, ruleChainId);
    if (ruleChain == null) {
        return null;
    }
    RuleChainMetaData ruleChainMetaData = new RuleChainMetaData();
    ruleChainMetaData.setRuleChainId(ruleChainId);
    List<RuleNode> ruleNodes = getRuleChainNodes(tenantId, ruleChainId);
    Map<RuleNodeId, Integer> ruleNodeIndexMap = new HashMap<>();
    for (RuleNode node : ruleNodes) {
        ruleNodeIndexMap.put(node.getId(), ruleNodes.indexOf(node));
    }
    ruleChainMetaData.setNodes(ruleNodes);
    if (ruleChain.getFirstRuleNodeId() != null) {
        ruleChainMetaData.setFirstNodeIndex(ruleNodeIndexMap.get(ruleChain.getFirstRuleNodeId()));
    }
    for (RuleNode node : ruleNodes) {
        int fromIndex = ruleNodeIndexMap.get(node.getId());
        List<EntityRelation> nodeRelations = getRuleNodeRelations(tenantId, node.getId());
        for (EntityRelation nodeRelation : nodeRelations) {
            String type = nodeRelation.getType();
            if (nodeRelation.getTo().getEntityType() == EntityType.RULE_NODE) {
                RuleNodeId toNodeId = new RuleNodeId(nodeRelation.getTo().getId());
                int toIndex = ruleNodeIndexMap.get(toNodeId);
                ruleChainMetaData.addConnectionInfo(fromIndex, toIndex, type);
            } else if (nodeRelation.getTo().getEntityType() == EntityType.RULE_CHAIN) {
                log.warn("[{}][{}] Unsupported node relation: {}", tenantId, ruleChainId, nodeRelation.getTo());
            }
        }
    }
    return ruleChainMetaData;
}
Also used : EntityRelation(org.thingsboard.server.common.data.relation.EntityRelation) RuleChain(org.thingsboard.server.common.data.rule.RuleChain) HashMap(java.util.HashMap) RuleChainMetaData(org.thingsboard.server.common.data.rule.RuleChainMetaData) RuleNode(org.thingsboard.server.common.data.rule.RuleNode) Validator.validateString(org.thingsboard.server.dao.service.Validator.validateString) RuleNodeId(org.thingsboard.server.common.data.id.RuleNodeId)

Aggregations

RuleNodeId (org.thingsboard.server.common.data.id.RuleNodeId)26 RuleChainId (org.thingsboard.server.common.data.id.RuleChainId)21 TbMsg (org.thingsboard.server.common.msg.TbMsg)15 EntityId (org.thingsboard.server.common.data.id.EntityId)14 TenantId (org.thingsboard.server.common.data.id.TenantId)12 Test (org.junit.Test)11 TbMsgMetaData (org.thingsboard.server.common.msg.TbMsgMetaData)11 RuleNode (org.thingsboard.server.common.data.rule.RuleNode)9 RuleChain (org.thingsboard.server.common.data.rule.RuleChain)8 JsonNode (com.fasterxml.jackson.databind.JsonNode)7 Futures (com.google.common.util.concurrent.Futures)7 ListenableFuture (com.google.common.util.concurrent.ListenableFuture)7 Uuids (com.datastax.oss.driver.api.core.uuid.Uuids)6 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)6 IOException (java.io.IOException)6 Callable (java.util.concurrent.Callable)6 Consumer (java.util.function.Consumer)6 ScriptException (javax.script.ScriptException)6 NotImplementedException (org.apache.commons.lang3.NotImplementedException)6 Assert.assertEquals (org.junit.Assert.assertEquals)6