use of org.thingsboard.server.common.data.rule.RuleChainMetaData in project thingsboard by thingsboard.
the class MqttClientTest method createRootRuleChainForRpcResponse.
private RuleChainId createRootRuleChainForRpcResponse() throws Exception {
RuleChain newRuleChain = new RuleChain();
newRuleChain.setName("testRuleChain");
ResponseEntity<RuleChain> ruleChainResponse = restClient.getRestTemplate().postForEntity(HTTPS_URL + "/api/ruleChain", newRuleChain, RuleChain.class);
Assert.assertTrue(ruleChainResponse.getStatusCode().is2xxSuccessful());
RuleChain ruleChain = ruleChainResponse.getBody();
JsonNode configuration = mapper.readTree(this.getClass().getClassLoader().getResourceAsStream("RpcResponseRuleChainMetadata.json"));
RuleChainMetaData ruleChainMetaData = new RuleChainMetaData();
ruleChainMetaData.setRuleChainId(ruleChain.getId());
ruleChainMetaData.setFirstNodeIndex(configuration.get("firstNodeIndex").asInt());
ruleChainMetaData.setNodes(Arrays.asList(mapper.treeToValue(configuration.get("nodes"), RuleNode[].class)));
ruleChainMetaData.setConnections(Arrays.asList(mapper.treeToValue(configuration.get("connections"), NodeConnectionInfo[].class)));
ResponseEntity<RuleChainMetaData> ruleChainMetadataResponse = restClient.getRestTemplate().postForEntity(HTTPS_URL + "/api/ruleChain/metadata", ruleChainMetaData, RuleChainMetaData.class);
Assert.assertTrue(ruleChainMetadataResponse.getStatusCode().is2xxSuccessful());
// Set a new rule chain as root
ResponseEntity<RuleChain> rootRuleChainResponse = restClient.getRestTemplate().postForEntity(HTTPS_URL + "/api/ruleChain/{ruleChainId}/root", null, RuleChain.class, ruleChain.getId());
Assert.assertTrue(rootRuleChainResponse.getStatusCode().is2xxSuccessful());
return ruleChain.getId();
}
use of org.thingsboard.server.common.data.rule.RuleChainMetaData in project thingsboard by thingsboard.
the class BaseRuleChainService method resetRuleChainMetadataTenantIds.
private void resetRuleChainMetadataTenantIds(TenantId tenantId, List<RuleChainMetaData> metaData) {
for (RuleChainMetaData md : metaData) {
for (RuleNode node : md.getNodes()) {
JsonNode nodeConfiguration = node.getConfiguration();
searchTenantIdRecursive(tenantId, nodeConfiguration);
}
}
}
use of org.thingsboard.server.common.data.rule.RuleChainMetaData 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;
}
use of org.thingsboard.server.common.data.rule.RuleChainMetaData in project thingsboard by thingsboard.
the class BaseRuleChainService method resetRuleNodeIds.
private void resetRuleNodeIds(List<RuleChainMetaData> metaData) {
for (RuleChainMetaData md : metaData) {
for (RuleNode node : md.getNodes()) {
node.setId(null);
node.setRuleChainId(null);
}
}
}
use of org.thingsboard.server.common.data.rule.RuleChainMetaData in project thingsboard by thingsboard.
the class BaseRuleChainServiceTest method createRuleChainMetadata.
private RuleChainMetaData createRuleChainMetadata() throws Exception {
RuleChain ruleChain = new RuleChain();
ruleChain.setName("My RuleChain");
ruleChain.setTenantId(tenantId);
RuleChain savedRuleChain = ruleChainService.saveRuleChain(ruleChain);
RuleChainMetaData ruleChainMetaData = new RuleChainMetaData();
ruleChainMetaData.setRuleChainId(savedRuleChain.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");
Assert.assertTrue(ruleChainService.saveRuleChainMetaData(tenantId, ruleChainMetaData).isSuccess());
return ruleChainService.loadRuleChainMetaData(tenantId, ruleChainMetaData.getRuleChainId());
}
Aggregations