Search in sources :

Example 1 with RuleChainMetaData

use of org.thingsboard.server.common.data.rule.RuleChainMetaData in project thingsboard by thingsboard.

the class BaseRuleChainService method exportTenantRuleChains.

@Override
public RuleChainData exportTenantRuleChains(TenantId tenantId, PageLink pageLink) {
    Validator.validateId(tenantId, "Incorrect tenant id for search rule chain request.");
    Validator.validatePageLink(pageLink);
    PageData<RuleChain> ruleChainData = ruleChainDao.findRuleChainsByTenantId(tenantId.getId(), pageLink);
    List<RuleChain> ruleChains = ruleChainData.getData();
    List<RuleChainMetaData> metadata = ruleChains.stream().map(rc -> loadRuleChainMetaData(tenantId, rc.getId())).collect(Collectors.toList());
    RuleChainData rcData = new RuleChainData();
    rcData.setRuleChains(ruleChains);
    rcData.setMetadata(metadata);
    setRandomRuleChainIds(rcData);
    resetRuleNodeIds(metadata);
    return rcData;
}
Also used : Edge(org.thingsboard.server.common.data.edge.Edge) BaseData(org.thingsboard.server.common.data.BaseData) RuleChainMetaData(org.thingsboard.server.common.data.rule.RuleChainMetaData) Autowired(org.springframework.beans.factory.annotation.Autowired) RuleNodeId(org.thingsboard.server.common.data.id.RuleNodeId) TenantId(org.thingsboard.server.common.data.id.TenantId) DataValidator(org.thingsboard.server.dao.service.DataValidator) EntityRelation(org.thingsboard.server.common.data.relation.EntityRelation) Uuids(com.datastax.oss.driver.api.core.uuid.Uuids) Map(java.util.Map) EntityType(org.thingsboard.server.common.data.EntityType) RuleChainConnectionInfo(org.thingsboard.server.common.data.rule.RuleChainConnectionInfo) JsonNode(com.fasterxml.jackson.databind.JsonNode) EdgeId(org.thingsboard.server.common.data.id.EdgeId) ConstraintViolationException(org.hibernate.exception.ConstraintViolationException) PageLink(org.thingsboard.server.common.data.page.PageLink) Collection(java.util.Collection) Set(java.util.Set) RuleChainData(org.thingsboard.server.common.data.rule.RuleChainData) Collectors(java.util.stream.Collectors) DataValidationException(org.thingsboard.server.dao.exception.DataValidationException) Slf4j(lombok.extern.slf4j.Slf4j) AbstractEntityService(org.thingsboard.server.dao.entity.AbstractEntityService) List(java.util.List) PaginatedRemover(org.thingsboard.server.dao.service.PaginatedRemover) Optional(java.util.Optional) RuleChainId(org.thingsboard.server.common.data.id.RuleChainId) TENANT(org.thingsboard.server.common.data.DataConstants.TENANT) ExceptionUtils(org.apache.commons.lang3.exception.ExceptionUtils) RuleChainImportResult(org.thingsboard.server.common.data.rule.RuleChainImportResult) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) JacksonUtil(org.thingsboard.common.util.JacksonUtil) HashMap(java.util.HashMap) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) RuleChainUpdateResult(org.thingsboard.server.common.data.rule.RuleChainUpdateResult) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Validator.validateId(org.thingsboard.server.dao.service.Validator.validateId) Validator.validateString(org.thingsboard.server.dao.service.Validator.validateString) RuleNodeUpdateResult(org.thingsboard.server.common.data.rule.RuleNodeUpdateResult) CollectionUtils(org.apache.commons.collections.CollectionUtils) Service(org.springframework.stereotype.Service) EntityId(org.thingsboard.server.common.data.id.EntityId) Iterator(java.util.Iterator) RuleChainType(org.thingsboard.server.common.data.rule.RuleChainType) RuleNode(org.thingsboard.server.common.data.rule.RuleNode) ConstraintValidator(org.thingsboard.server.dao.service.ConstraintValidator) Validator(org.thingsboard.server.dao.service.Validator) PageData(org.thingsboard.server.common.data.page.PageData) RuleChain(org.thingsboard.server.common.data.rule.RuleChain) RelationTypeGroup(org.thingsboard.server.common.data.relation.RelationTypeGroup) NodeConnectionInfo(org.thingsboard.server.common.data.rule.NodeConnectionInfo) Transactional(org.springframework.transaction.annotation.Transactional) RuleChain(org.thingsboard.server.common.data.rule.RuleChain) RuleChainMetaData(org.thingsboard.server.common.data.rule.RuleChainMetaData) RuleChainData(org.thingsboard.server.common.data.rule.RuleChainData)

Example 2 with RuleChainMetaData

use of org.thingsboard.server.common.data.rule.RuleChainMetaData in project thingsboard by thingsboard.

the class AbstractRuleEngineFlowIntegrationTest method testRuleChainWithTwoRules.

@Test
public void testRuleChainWithTwoRules() throws Exception {
    // Creating Rule Chain
    RuleChain ruleChain = new RuleChain();
    ruleChain.setName("Simple Rule Chain");
    ruleChain.setTenantId(savedTenant.getId());
    ruleChain.setRoot(true);
    ruleChain.setDebugMode(true);
    ruleChain = saveRuleChain(ruleChain);
    Assert.assertNull(ruleChain.getFirstRuleNodeId());
    RuleChainMetaData metaData = new RuleChainMetaData();
    metaData.setRuleChainId(ruleChain.getId());
    RuleNode ruleNode1 = new RuleNode();
    ruleNode1.setName("Simple Rule Node 1");
    ruleNode1.setType(org.thingsboard.rule.engine.metadata.TbGetAttributesNode.class.getName());
    ruleNode1.setDebugMode(true);
    TbGetAttributesNodeConfiguration configuration1 = new TbGetAttributesNodeConfiguration();
    configuration1.setServerAttributeNames(Collections.singletonList("serverAttributeKey1"));
    ruleNode1.setConfiguration(mapper.valueToTree(configuration1));
    RuleNode ruleNode2 = new RuleNode();
    ruleNode2.setName("Simple Rule Node 2");
    ruleNode2.setType(org.thingsboard.rule.engine.metadata.TbGetAttributesNode.class.getName());
    ruleNode2.setDebugMode(true);
    TbGetAttributesNodeConfiguration configuration2 = new TbGetAttributesNodeConfiguration();
    configuration2.setServerAttributeNames(Collections.singletonList("serverAttributeKey2"));
    ruleNode2.setConfiguration(mapper.valueToTree(configuration2));
    metaData.setNodes(Arrays.asList(ruleNode1, ruleNode2));
    metaData.setFirstNodeIndex(0);
    metaData.addConnectionInfo(0, 1, "Success");
    metaData = saveRuleChainMetaData(metaData);
    Assert.assertNotNull(metaData);
    ruleChain = getRuleChain(ruleChain.getId());
    Assert.assertNotNull(ruleChain.getFirstRuleNodeId());
    // Saving the device
    Device device = new Device();
    device.setName("My device");
    device.setType("default");
    device = doPost("/api/device", device, Device.class);
    attributesService.save(device.getTenantId(), device.getId(), DataConstants.SERVER_SCOPE, Collections.singletonList(new BaseAttributeKvEntry(new StringDataEntry("serverAttributeKey1", "serverAttributeValue1"), System.currentTimeMillis()))).get();
    attributesService.save(device.getTenantId(), device.getId(), DataConstants.SERVER_SCOPE, Collections.singletonList(new BaseAttributeKvEntry(new StringDataEntry("serverAttributeKey2", "serverAttributeValue2"), System.currentTimeMillis()))).get();
    TbMsgCallback tbMsgCallback = Mockito.mock(TbMsgCallback.class);
    Mockito.when(tbMsgCallback.isMsgValid()).thenReturn(true);
    TbMsg tbMsg = TbMsg.newMsg("CUSTOM", device.getId(), new TbMsgMetaData(), "{}", tbMsgCallback);
    QueueToRuleEngineMsg qMsg = new QueueToRuleEngineMsg(savedTenant.getId(), tbMsg, null, null);
    // Pushing Message to the system
    actorSystem.tell(qMsg);
    Mockito.verify(tbMsgCallback, Mockito.timeout(10000)).onSuccess();
    PageData<Event> eventsPage = getDebugEvents(savedTenant.getId(), ruleChain.getFirstRuleNodeId(), 1000);
    List<Event> events = eventsPage.getData().stream().filter(filterByCustomEvent()).collect(Collectors.toList());
    Assert.assertEquals(2, events.size());
    Event inEvent = events.stream().filter(e -> e.getBody().get("type").asText().equals(DataConstants.IN)).findFirst().get();
    Assert.assertEquals(ruleChain.getFirstRuleNodeId(), inEvent.getEntityId());
    Assert.assertEquals(device.getId().getId().toString(), inEvent.getBody().get("entityId").asText());
    Event outEvent = events.stream().filter(e -> e.getBody().get("type").asText().equals(DataConstants.OUT)).findFirst().get();
    Assert.assertEquals(ruleChain.getFirstRuleNodeId(), outEvent.getEntityId());
    Assert.assertEquals(device.getId().getId().toString(), outEvent.getBody().get("entityId").asText());
    Assert.assertEquals("serverAttributeValue1", getMetadata(outEvent).get("ss_serverAttributeKey1").asText());
    RuleChain finalRuleChain = ruleChain;
    RuleNode lastRuleNode = metaData.getNodes().stream().filter(node -> !node.getId().equals(finalRuleChain.getFirstRuleNodeId())).findFirst().get();
    eventsPage = getDebugEvents(savedTenant.getId(), lastRuleNode.getId(), 1000);
    events = eventsPage.getData().stream().filter(filterByCustomEvent()).collect(Collectors.toList());
    Assert.assertEquals(2, events.size());
    inEvent = events.stream().filter(e -> e.getBody().get("type").asText().equals(DataConstants.IN)).findFirst().get();
    Assert.assertEquals(lastRuleNode.getId(), inEvent.getEntityId());
    Assert.assertEquals(device.getId().getId().toString(), inEvent.getBody().get("entityId").asText());
    outEvent = events.stream().filter(e -> e.getBody().get("type").asText().equals(DataConstants.OUT)).findFirst().get();
    Assert.assertEquals(lastRuleNode.getId(), outEvent.getEntityId());
    Assert.assertEquals(device.getId().getId().toString(), outEvent.getBody().get("entityId").asText());
    Assert.assertEquals("serverAttributeValue1", getMetadata(outEvent).get("ss_serverAttributeKey1").asText());
    Assert.assertEquals("serverAttributeValue2", getMetadata(outEvent).get("ss_serverAttributeKey2").asText());
}
Also used : Event(org.thingsboard.server.common.data.Event) Arrays(java.util.Arrays) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) TbMsg(org.thingsboard.server.common.msg.TbMsg) Device(org.thingsboard.server.common.data.Device) RuleChainMetaData(org.thingsboard.server.common.data.rule.RuleChainMetaData) Autowired(org.springframework.beans.factory.annotation.Autowired) Tenant(org.thingsboard.server.common.data.Tenant) Mockito.spy(org.mockito.Mockito.spy) TbMsgMetaData(org.thingsboard.server.common.msg.TbMsgMetaData) Answer(org.mockito.stubbing.Answer) User(org.thingsboard.server.common.data.User) MockMvcResultMatchers.status(org.springframework.test.web.servlet.result.MockMvcResultMatchers.status) ActorSystemContext(org.thingsboard.server.actors.ActorSystemContext) QueueToRuleEngineMsg(org.thingsboard.server.common.msg.queue.QueueToRuleEngineMsg) After(org.junit.After) TbRuleChainInputNodeConfiguration(org.thingsboard.rule.engine.flow.TbRuleChainInputNodeConfiguration) AbstractRuleEngineControllerTest(org.thingsboard.server.controller.AbstractRuleEngineControllerTest) AttributesService(org.thingsboard.server.dao.attributes.AttributesService) Before(org.junit.Before) TbMsgCallback(org.thingsboard.server.common.msg.queue.TbMsgCallback) DataConstants(org.thingsboard.server.common.data.DataConstants) RuleNode(org.thingsboard.server.common.data.rule.RuleNode) TbGetAttributesNodeConfiguration(org.thingsboard.rule.engine.metadata.TbGetAttributesNodeConfiguration) ReflectionTestUtils(org.springframework.test.util.ReflectionTestUtils) Test(org.junit.Test) BaseAttributeKvEntry(org.thingsboard.server.common.data.kv.BaseAttributeKvEntry) Authority(org.thingsboard.server.common.data.security.Authority) Collectors(java.util.stream.Collectors) Mockito(org.mockito.Mockito) Slf4j(lombok.extern.slf4j.Slf4j) EventService(org.thingsboard.server.dao.event.EventService) List(java.util.List) PageData(org.thingsboard.server.common.data.page.PageData) RuleChain(org.thingsboard.server.common.data.rule.RuleChain) Assert(org.junit.Assert) NodeConnectionInfo(org.thingsboard.server.common.data.rule.NodeConnectionInfo) Collections(java.util.Collections) StringDataEntry(org.thingsboard.server.common.data.kv.StringDataEntry) QueueToRuleEngineMsg(org.thingsboard.server.common.msg.queue.QueueToRuleEngineMsg) Device(org.thingsboard.server.common.data.Device) RuleNode(org.thingsboard.server.common.data.rule.RuleNode) TbMsgMetaData(org.thingsboard.server.common.msg.TbMsgMetaData) TbGetAttributesNodeConfiguration(org.thingsboard.rule.engine.metadata.TbGetAttributesNodeConfiguration) RuleChain(org.thingsboard.server.common.data.rule.RuleChain) StringDataEntry(org.thingsboard.server.common.data.kv.StringDataEntry) RuleChainMetaData(org.thingsboard.server.common.data.rule.RuleChainMetaData) BaseAttributeKvEntry(org.thingsboard.server.common.data.kv.BaseAttributeKvEntry) Event(org.thingsboard.server.common.data.Event) TbMsgCallback(org.thingsboard.server.common.msg.queue.TbMsgCallback) TbMsg(org.thingsboard.server.common.msg.TbMsg) AbstractRuleEngineControllerTest(org.thingsboard.server.controller.AbstractRuleEngineControllerTest) Test(org.junit.Test)

Example 3 with RuleChainMetaData

use of org.thingsboard.server.common.data.rule.RuleChainMetaData in project thingsboard by thingsboard.

the class AbstractRuleEngineLifecycleIntegrationTest method testRuleChainWithOneRule.

@Test
public void testRuleChainWithOneRule() throws Exception {
    // Creating Rule Chain
    RuleChain ruleChain = new RuleChain();
    ruleChain.setName("Simple Rule Chain");
    ruleChain.setTenantId(savedTenant.getId());
    ruleChain.setRoot(true);
    ruleChain.setDebugMode(true);
    ruleChain = saveRuleChain(ruleChain);
    Assert.assertNull(ruleChain.getFirstRuleNodeId());
    RuleChainMetaData metaData = new RuleChainMetaData();
    metaData.setRuleChainId(ruleChain.getId());
    RuleNode ruleNode = new RuleNode();
    ruleNode.setName("Simple Rule Node");
    ruleNode.setType(org.thingsboard.rule.engine.metadata.TbGetAttributesNode.class.getName());
    ruleNode.setDebugMode(true);
    TbGetAttributesNodeConfiguration configuration = new TbGetAttributesNodeConfiguration();
    configuration.setServerAttributeNames(Collections.singletonList("serverAttributeKey"));
    ruleNode.setConfiguration(mapper.valueToTree(configuration));
    metaData.setNodes(Collections.singletonList(ruleNode));
    metaData.setFirstNodeIndex(0);
    metaData = saveRuleChainMetaData(metaData);
    Assert.assertNotNull(metaData);
    ruleChain = getRuleChain(ruleChain.getId());
    Assert.assertNotNull(ruleChain.getFirstRuleNodeId());
    // Saving the device
    Device device = new Device();
    device.setName("My device");
    device.setType("default");
    device = doPost("/api/device", device, Device.class);
    attributesService.save(device.getTenantId(), device.getId(), DataConstants.SERVER_SCOPE, Collections.singletonList(new BaseAttributeKvEntry(new StringDataEntry("serverAttributeKey", "serverAttributeValue"), System.currentTimeMillis())));
    await("total inMemory queue lag is empty").atMost(30, TimeUnit.SECONDS).until(() -> InMemoryStorage.getInstance().getLagTotal() == 0);
    Thread.sleep(1000);
    TbMsgCallback tbMsgCallback = Mockito.mock(TbMsgCallback.class);
    Mockito.when(tbMsgCallback.isMsgValid()).thenReturn(true);
    TbMsg tbMsg = TbMsg.newMsg("CUSTOM", device.getId(), new TbMsgMetaData(), "{}", tbMsgCallback);
    QueueToRuleEngineMsg qMsg = new QueueToRuleEngineMsg(savedTenant.getId(), tbMsg, null, null);
    // Pushing Message to the system
    actorSystem.tell(qMsg);
    Mockito.verify(tbMsgCallback, Mockito.timeout(10000)).onSuccess();
    PageData<Event> eventsPage = getDebugEvents(savedTenant.getId(), ruleChain.getFirstRuleNodeId(), 1000);
    List<Event> events = eventsPage.getData().stream().filter(filterByCustomEvent()).collect(Collectors.toList());
    Assert.assertEquals(2, events.size());
    Event inEvent = events.stream().filter(e -> e.getBody().get("type").asText().equals(DataConstants.IN)).findFirst().get();
    Assert.assertEquals(ruleChain.getFirstRuleNodeId(), inEvent.getEntityId());
    Assert.assertEquals(device.getId().getId().toString(), inEvent.getBody().get("entityId").asText());
    Event outEvent = events.stream().filter(e -> e.getBody().get("type").asText().equals(DataConstants.OUT)).findFirst().get();
    Assert.assertEquals(ruleChain.getFirstRuleNodeId(), outEvent.getEntityId());
    Assert.assertEquals(device.getId().getId().toString(), outEvent.getBody().get("entityId").asText());
    Assert.assertEquals("serverAttributeValue", getMetadata(outEvent).get("ss_serverAttributeKey").asText());
}
Also used : QueueToRuleEngineMsg(org.thingsboard.server.common.msg.queue.QueueToRuleEngineMsg) Device(org.thingsboard.server.common.data.Device) RuleNode(org.thingsboard.server.common.data.rule.RuleNode) TbMsgMetaData(org.thingsboard.server.common.msg.TbMsgMetaData) TbGetAttributesNodeConfiguration(org.thingsboard.rule.engine.metadata.TbGetAttributesNodeConfiguration) RuleChain(org.thingsboard.server.common.data.rule.RuleChain) StringDataEntry(org.thingsboard.server.common.data.kv.StringDataEntry) RuleChainMetaData(org.thingsboard.server.common.data.rule.RuleChainMetaData) BaseAttributeKvEntry(org.thingsboard.server.common.data.kv.BaseAttributeKvEntry) Event(org.thingsboard.server.common.data.Event) TbMsgCallback(org.thingsboard.server.common.msg.queue.TbMsgCallback) TbMsg(org.thingsboard.server.common.msg.TbMsg) AbstractRuleEngineControllerTest(org.thingsboard.server.controller.AbstractRuleEngineControllerTest) Test(org.junit.Test)

Example 4 with RuleChainMetaData

use of org.thingsboard.server.common.data.rule.RuleChainMetaData in project thingsboard by thingsboard.

the class InstallScripts method createRuleChainFromFile.

public RuleChain createRuleChainFromFile(TenantId tenantId, Path templateFilePath, String newRuleChainName) throws IOException {
    JsonNode ruleChainJson = objectMapper.readTree(templateFilePath.toFile());
    RuleChain ruleChain = objectMapper.treeToValue(ruleChainJson.get("ruleChain"), RuleChain.class);
    RuleChainMetaData ruleChainMetaData = objectMapper.treeToValue(ruleChainJson.get("metadata"), RuleChainMetaData.class);
    ruleChain.setTenantId(tenantId);
    if (!StringUtils.isEmpty(newRuleChainName)) {
        ruleChain.setName(newRuleChainName);
    }
    ruleChain = ruleChainService.saveRuleChain(ruleChain);
    ruleChainMetaData.setRuleChainId(ruleChain.getId());
    ruleChainService.saveRuleChainMetaData(TenantId.SYS_TENANT_ID, ruleChainMetaData);
    return ruleChain;
}
Also used : RuleChain(org.thingsboard.server.common.data.rule.RuleChain) RuleChainMetaData(org.thingsboard.server.common.data.rule.RuleChainMetaData) JsonNode(com.fasterxml.jackson.databind.JsonNode)

Example 5 with RuleChainMetaData

use of org.thingsboard.server.common.data.rule.RuleChainMetaData in project thingsboard by thingsboard.

the class RuleChainEdgeProcessor method processRuleChainMetadataToEdge.

public DownlinkMsg processRuleChainMetadataToEdge(EdgeEvent edgeEvent, UpdateMsgType msgType, EdgeVersion edgeVersion) {
    RuleChainId ruleChainId = new RuleChainId(edgeEvent.getEntityId());
    RuleChain ruleChain = ruleChainService.findRuleChainById(edgeEvent.getTenantId(), ruleChainId);
    DownlinkMsg downlinkMsg = null;
    if (ruleChain != null) {
        RuleChainMetaData ruleChainMetaData = ruleChainService.loadRuleChainMetaData(edgeEvent.getTenantId(), ruleChainId);
        RuleChainMetadataUpdateMsg ruleChainMetadataUpdateMsg = ruleChainMsgConstructor.constructRuleChainMetadataUpdatedMsg(msgType, ruleChainMetaData, edgeVersion);
        if (ruleChainMetadataUpdateMsg != null) {
            downlinkMsg = DownlinkMsg.newBuilder().setDownlinkMsgId(EdgeUtils.nextPositiveInt()).addRuleChainMetadataUpdateMsg(ruleChainMetadataUpdateMsg).build();
        }
    }
    return downlinkMsg;
}
Also used : DownlinkMsg(org.thingsboard.server.gen.edge.v1.DownlinkMsg) RuleChainMetadataUpdateMsg(org.thingsboard.server.gen.edge.v1.RuleChainMetadataUpdateMsg) RuleChain(org.thingsboard.server.common.data.rule.RuleChain) RuleChainMetaData(org.thingsboard.server.common.data.rule.RuleChainMetaData) RuleChainId(org.thingsboard.server.common.data.id.RuleChainId)

Aggregations

RuleChainMetaData (org.thingsboard.server.common.data.rule.RuleChainMetaData)23 RuleNode (org.thingsboard.server.common.data.rule.RuleNode)15 RuleChain (org.thingsboard.server.common.data.rule.RuleChain)12 Test (org.junit.Test)8 ArrayList (java.util.ArrayList)5 RuleChainId (org.thingsboard.server.common.data.id.RuleChainId)5 EntityRelation (org.thingsboard.server.common.data.relation.EntityRelation)5 JsonNode (com.fasterxml.jackson.databind.JsonNode)4 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)4 NodeConnectionInfo (org.thingsboard.server.common.data.rule.NodeConnectionInfo)4 ListenableFuture (com.google.common.util.concurrent.ListenableFuture)3 List (java.util.List)3 Collectors (java.util.stream.Collectors)3 Slf4j (lombok.extern.slf4j.Slf4j)3 Autowired (org.springframework.beans.factory.annotation.Autowired)3 TbGetAttributesNodeConfiguration (org.thingsboard.rule.engine.metadata.TbGetAttributesNodeConfiguration)3 Device (org.thingsboard.server.common.data.Device)3 Event (org.thingsboard.server.common.data.Event)3 RuleChainMetadataUpdateMsg (org.thingsboard.server.gen.edge.v1.RuleChainMetadataUpdateMsg)3 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)2