use of org.thingsboard.server.common.data.rule.RuleChainMetaData in project thingsboard by thingsboard.
the class BaseEdgeTest method createRuleChainMetadata.
private void createRuleChainMetadata(RuleChain ruleChain) throws Exception {
RuleChainMetaData ruleChainMetaData = new RuleChainMetaData();
ruleChainMetaData.setRuleChainId(ruleChain.getId());
ObjectMapper mapper = new ObjectMapper();
RuleNode ruleNode1 = new RuleNode();
ruleNode1.setName("name1");
ruleNode1.setType("type1");
ruleNode1.setConfiguration(mapper.readTree("\"key1\": \"val1\""));
RuleNode ruleNode2 = new RuleNode();
ruleNode2.setName("name2");
ruleNode2.setType("type2");
ruleNode2.setConfiguration(mapper.readTree("\"key2\": \"val2\""));
RuleNode ruleNode3 = new RuleNode();
ruleNode3.setName("name3");
ruleNode3.setType("type3");
ruleNode3.setConfiguration(mapper.readTree("\"key3\": \"val3\""));
List<RuleNode> ruleNodes = new ArrayList<>();
ruleNodes.add(ruleNode1);
ruleNodes.add(ruleNode2);
ruleNodes.add(ruleNode3);
ruleChainMetaData.setFirstNodeIndex(0);
ruleChainMetaData.setNodes(ruleNodes);
ruleChainMetaData.addConnectionInfo(0, 1, "success");
ruleChainMetaData.addConnectionInfo(0, 2, "fail");
ruleChainMetaData.addConnectionInfo(1, 2, "success");
doPost("/api/ruleChain/metadata", ruleChainMetaData, RuleChainMetaData.class);
}
use of org.thingsboard.server.common.data.rule.RuleChainMetaData in project thingsboard by thingsboard.
the class RuleChainMsgConstructorTest method createRuleChainMetaData.
private RuleChainMetaData createRuleChainMetaData(RuleChainId ruleChainId, Integer firstNodeIndex, List<RuleNode> nodes, List<NodeConnectionInfo> connections) {
RuleChainMetaData ruleChainMetaData = new RuleChainMetaData();
ruleChainMetaData.setRuleChainId(ruleChainId);
ruleChainMetaData.setFirstNodeIndex(firstNodeIndex);
ruleChainMetaData.setNodes(nodes);
ruleChainMetaData.setConnections(connections);
ruleChainMetaData.setRuleChainConnections(null);
return ruleChainMetaData;
}
use of org.thingsboard.server.common.data.rule.RuleChainMetaData in project thingsboard by thingsboard.
the class RuleChainController method saveRuleChainMetaData.
@ApiOperation(value = "Update Rule Chain Metadata", notes = "Updates the rule chain metadata. " + RULE_CHAIN_METADATA_DESCRIPTION + TENANT_AUTHORITY_PARAGRAPH)
@PreAuthorize("hasAnyAuthority('TENANT_ADMIN')")
@RequestMapping(value = "/ruleChain/metadata", method = RequestMethod.POST)
@ResponseBody
public RuleChainMetaData saveRuleChainMetaData(@ApiParam(value = "A JSON value representing the rule chain metadata.") @RequestBody RuleChainMetaData ruleChainMetaData, @ApiParam(value = "Update related rule nodes.") @RequestParam(value = "updateRelated", required = false, defaultValue = "true") boolean updateRelated) throws ThingsboardException {
try {
TenantId tenantId = getTenantId();
if (debugPerTenantEnabled) {
ConcurrentMap<TenantId, DebugTbRateLimits> debugPerTenantLimits = actorContext.getDebugPerTenantLimits();
DebugTbRateLimits debugTbRateLimits = debugPerTenantLimits.getOrDefault(tenantId, null);
if (debugTbRateLimits != null) {
debugPerTenantLimits.remove(tenantId, debugTbRateLimits);
}
}
RuleChain ruleChain = checkRuleChain(ruleChainMetaData.getRuleChainId(), Operation.WRITE);
RuleChainUpdateResult result = ruleChainService.saveRuleChainMetaData(tenantId, ruleChainMetaData);
checkNotNull(result.isSuccess() ? true : null);
List<RuleChain> updatedRuleChains;
if (updateRelated && result.isSuccess()) {
updatedRuleChains = tbRuleChainService.updateRelatedRuleChains(tenantId, ruleChainMetaData.getRuleChainId(), result);
} else {
updatedRuleChains = Collections.emptyList();
}
RuleChainMetaData savedRuleChainMetaData = checkNotNull(ruleChainService.loadRuleChainMetaData(tenantId, ruleChainMetaData.getRuleChainId()));
if (RuleChainType.CORE.equals(ruleChain.getType())) {
tbClusterService.broadcastEntityStateChangeEvent(ruleChain.getTenantId(), ruleChain.getId(), ComponentLifecycleEvent.UPDATED);
updatedRuleChains.forEach(updatedRuleChain -> {
tbClusterService.broadcastEntityStateChangeEvent(updatedRuleChain.getTenantId(), updatedRuleChain.getId(), ComponentLifecycleEvent.UPDATED);
});
}
logEntityAction(ruleChain.getId(), ruleChain, null, ActionType.UPDATED, null, ruleChainMetaData);
for (RuleChain updatedRuleChain : updatedRuleChains) {
RuleChainMetaData updatedRuleChainMetaData = checkNotNull(ruleChainService.loadRuleChainMetaData(tenantId, updatedRuleChain.getId()));
logEntityAction(updatedRuleChain.getId(), updatedRuleChain, null, ActionType.UPDATED, null, updatedRuleChainMetaData);
}
if (RuleChainType.EDGE.equals(ruleChain.getType())) {
sendEntityNotificationMsg(ruleChain.getTenantId(), ruleChain.getId(), EdgeEventActionType.UPDATED);
updatedRuleChains.forEach(updatedRuleChain -> {
sendEntityNotificationMsg(updatedRuleChain.getTenantId(), updatedRuleChain.getId(), EdgeEventActionType.UPDATED);
});
}
return savedRuleChainMetaData;
} catch (Exception e) {
logEntityAction(emptyId(EntityType.RULE_CHAIN), null, null, ActionType.UPDATED, e, ruleChainMetaData);
throw handleException(e);
}
}
use of org.thingsboard.server.common.data.rule.RuleChainMetaData in project thingsboard by thingsboard.
the class BaseRuleChainServiceTest method testSaveRuleChainMetaData.
@Test
public void testSaveRuleChainMetaData() throws Exception {
RuleChainMetaData savedRuleChainMetaData = createRuleChainMetadata();
Assert.assertEquals(3, savedRuleChainMetaData.getNodes().size());
Assert.assertEquals(3, savedRuleChainMetaData.getConnections().size());
for (RuleNode ruleNode : savedRuleChainMetaData.getNodes()) {
Assert.assertNotNull(ruleNode.getId());
List<EntityRelation> relations = ruleChainService.getRuleNodeRelations(tenantId, ruleNode.getId());
if ("name1".equals(ruleNode.getName())) {
Assert.assertEquals(2, relations.size());
} else if ("name2".equals(ruleNode.getName())) {
Assert.assertEquals(1, relations.size());
} else if ("name3".equals(ruleNode.getName())) {
Assert.assertEquals(0, relations.size());
}
}
List<RuleNode> loadedRuleNodes = ruleChainService.getRuleChainNodes(tenantId, savedRuleChainMetaData.getRuleChainId());
Collections.sort(savedRuleChainMetaData.getNodes(), ruleNodeIdComparator);
Collections.sort(loadedRuleNodes, ruleNodeIdComparator);
Assert.assertEquals(savedRuleChainMetaData.getNodes(), loadedRuleNodes);
ruleChainService.deleteRuleChainById(tenantId, savedRuleChainMetaData.getRuleChainId());
}
use of org.thingsboard.server.common.data.rule.RuleChainMetaData in project thingsboard by thingsboard.
the class BaseRuleChainServiceTest method testUpdateRuleChainMetaData.
@Test
public void testUpdateRuleChainMetaData() throws Exception {
RuleChainMetaData savedRuleChainMetaData = createRuleChainMetadata();
List<RuleNode> ruleNodes = savedRuleChainMetaData.getNodes();
int name3Index = -1;
for (int i = 0; i < ruleNodes.size(); i++) {
if ("name3".equals(ruleNodes.get(i).getName())) {
name3Index = i;
break;
}
}
RuleNode ruleNode4 = new RuleNode();
ruleNode4.setName("name4");
ruleNode4.setType("type4");
ruleNode4.setConfiguration(mapper.readTree("\"key4\": \"val4\""));
ruleNodes.set(name3Index, ruleNode4);
Assert.assertTrue(ruleChainService.saveRuleChainMetaData(tenantId, savedRuleChainMetaData).isSuccess());
RuleChainMetaData updatedRuleChainMetaData = ruleChainService.loadRuleChainMetaData(tenantId, savedRuleChainMetaData.getRuleChainId());
Assert.assertEquals(3, updatedRuleChainMetaData.getNodes().size());
Assert.assertEquals(3, updatedRuleChainMetaData.getConnections().size());
for (RuleNode ruleNode : updatedRuleChainMetaData.getNodes()) {
Assert.assertNotNull(ruleNode.getId());
List<EntityRelation> relations = ruleChainService.getRuleNodeRelations(tenantId, ruleNode.getId());
if ("name1".equals(ruleNode.getName())) {
Assert.assertEquals(2, relations.size());
} else if ("name2".equals(ruleNode.getName())) {
Assert.assertEquals(1, relations.size());
} else if ("name4".equals(ruleNode.getName())) {
Assert.assertEquals(0, relations.size());
}
}
List<RuleNode> loadedRuleNodes = ruleChainService.getRuleChainNodes(tenantId, savedRuleChainMetaData.getRuleChainId());
Collections.sort(updatedRuleChainMetaData.getNodes(), ruleNodeIdComparator);
Collections.sort(loadedRuleNodes, ruleNodeIdComparator);
Assert.assertEquals(updatedRuleChainMetaData.getNodes(), loadedRuleNodes);
ruleChainService.deleteRuleChainById(tenantId, savedRuleChainMetaData.getRuleChainId());
}
Aggregations