Commit 174570610bd2e26339eaaacfb48e49467dba1484
Committed by
Andrew Shvayka
1 parent
b9cc400a
added circles validation in BaseRuleChainService
Showing
1 changed file
with
28 additions
and
0 deletions
... | ... | @@ -17,6 +17,7 @@ package org.thingsboard.server.dao.rule; |
17 | 17 | |
18 | 18 | import com.google.common.util.concurrent.ListenableFuture; |
19 | 19 | import lombok.extern.slf4j.Slf4j; |
20 | +import org.apache.commons.collections.CollectionUtils; | |
20 | 21 | import org.apache.commons.lang3.StringUtils; |
21 | 22 | import org.springframework.beans.factory.annotation.Autowired; |
22 | 23 | import org.springframework.stereotype.Service; |
... | ... | @@ -38,6 +39,7 @@ import org.thingsboard.server.common.data.rule.RuleChainMetaData; |
38 | 39 | import org.thingsboard.server.common.data.rule.RuleNode; |
39 | 40 | import org.thingsboard.server.dao.entity.AbstractEntityService; |
40 | 41 | import org.thingsboard.server.dao.exception.DataValidationException; |
42 | +import org.thingsboard.server.dao.exception.IncorrectParameterException; | |
41 | 43 | import org.thingsboard.server.dao.service.DataValidator; |
42 | 44 | import org.thingsboard.server.dao.service.PaginatedRemover; |
43 | 45 | import org.thingsboard.server.dao.service.Validator; |
... | ... | @@ -123,6 +125,10 @@ public class BaseRuleChainService extends AbstractEntityService implements RuleC |
123 | 125 | return null; |
124 | 126 | } |
125 | 127 | |
128 | + if (CollectionUtils.isNotEmpty(ruleChainMetaData.getConnections())) { | |
129 | + validateCircles(ruleChainMetaData.getConnections()); | |
130 | + } | |
131 | + | |
126 | 132 | List<RuleNode> nodes = ruleChainMetaData.getNodes(); |
127 | 133 | List<RuleNode> toAddOrUpdate = new ArrayList<>(); |
128 | 134 | List<RuleNode> toDelete = new ArrayList<>(); |
... | ... | @@ -205,6 +211,28 @@ public class BaseRuleChainService extends AbstractEntityService implements RuleC |
205 | 211 | return loadRuleChainMetaData(tenantId, ruleChainMetaData.getRuleChainId()); |
206 | 212 | } |
207 | 213 | |
214 | + private void validateCircles(List<NodeConnectionInfo> connectionInfos) { | |
215 | + Map<Integer, List<Integer>> connectionsMap = new HashMap<>(); | |
216 | + for (NodeConnectionInfo nodeConnection : connectionInfos) { | |
217 | + connectionsMap | |
218 | + .computeIfAbsent(nodeConnection.getFromIndex(), from -> new ArrayList<>()) | |
219 | + .add(nodeConnection.getToIndex()); | |
220 | + } | |
221 | + connectionsMap.keySet().forEach(key -> validateCircles(key, connectionsMap.get(key), connectionsMap)); | |
222 | + } | |
223 | + | |
224 | + private void validateCircles(int from, List<Integer> toList, Map<Integer, List<Integer>> connectionsMap) { | |
225 | + if (toList == null) { | |
226 | + return; | |
227 | + } | |
228 | + for (Integer to : toList) { | |
229 | + if (from == to) { | |
230 | + throw new IncorrectParameterException("Can't create circling relations in rule chain."); | |
231 | + } | |
232 | + validateCircles(from, connectionsMap.get(to), connectionsMap); | |
233 | + } | |
234 | + } | |
235 | + | |
208 | 236 | @Override |
209 | 237 | public RuleChainMetaData loadRuleChainMetaData(TenantId tenantId, RuleChainId ruleChainId) { |
210 | 238 | Validator.validateId(ruleChainId, "Incorrect rule chain id."); | ... | ... |