summaryrefslogtreecommitdiffstats
path: root/rulemgt/src/main/java/org/onap
diff options
context:
space:
mode:
authorGuangrongFu <fu.guangrong@zte.com.cn>2021-08-21 16:58:53 +0800
committerGuangrongFu <fu.guangrong@zte.com.cn>2021-08-21 17:00:42 +0800
commitbd657ac5ef55d89917f42b76b1cee9c9852a07a8 (patch)
tree892e6696057c6e2bfcaf5e3e97a831c4f9e4c2cd /rulemgt/src/main/java/org/onap
parent6c0d57eeaa87ac1d2a4a808a93963a5c3c78d68a (diff)
bugfix - rule sync issues when engine redeployed
Issue-ID: HOLMES-462 Signed-off-by: GuangrongFu <fu.guangrong@zte.com.cn> Change-Id: I082b0df4215fd13b7313b3093494c34c7daa7c9c
Diffstat (limited to 'rulemgt/src/main/java/org/onap')
-rw-r--r--rulemgt/src/main/java/org/onap/holmes/rulemgt/RuleAllocator.java34
-rw-r--r--rulemgt/src/main/java/org/onap/holmes/rulemgt/constant/RuleMgtConstant.java58
-rw-r--r--rulemgt/src/main/java/org/onap/holmes/rulemgt/wrapper/RuleMgtWrapper.java567
3 files changed, 343 insertions, 316 deletions
diff --git a/rulemgt/src/main/java/org/onap/holmes/rulemgt/RuleAllocator.java b/rulemgt/src/main/java/org/onap/holmes/rulemgt/RuleAllocator.java
index 2dc05ee..31c200a 100644
--- a/rulemgt/src/main/java/org/onap/holmes/rulemgt/RuleAllocator.java
+++ b/rulemgt/src/main/java/org/onap/holmes/rulemgt/RuleAllocator.java
@@ -1,5 +1,5 @@
/**
- * Copyright 2017-2020 ZTE Corporation.
+ * Copyright 2017-2021 ZTE Corporation.
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -32,6 +32,7 @@ import org.slf4j.LoggerFactory;
import javax.annotation.PostConstruct;
import javax.inject.Inject;
import java.util.*;
+import java.util.concurrent.TimeUnit;
import static java.util.concurrent.TimeUnit.SECONDS;
@@ -41,6 +42,8 @@ public class RuleAllocator {
private static final Logger LOGGER = LoggerFactory.getLogger(RuleAllocator.class);
public final static int ENABLE = 1;
+ public final static int RETRY_TIMES = 5;
+ public final static long RETRY_INTERVAL_SEC = 15;
private RuleMgtWrapper ruleMgtWrapper;
private RuleQueryWrapper ruleQueryWrapper;
private EngineWrapper engineWrapper;
@@ -194,7 +197,7 @@ public class RuleAllocator {
// Sorted by the number of rules each engine contains, in a descending order.
private List<String> sortIpByRuleNumDesc(List<String> ips) {
- List<CorrelationRule> rules = null;
+ List<CorrelationRule> rules;
Map<String, Integer> ruleNumOfEngines = new HashMap();
try {
@@ -219,12 +222,27 @@ public class RuleAllocator {
}
private void allocateRule(CorrelationRule rule, String ip) throws CorrelationException {
- try {
- ruleMgtWrapper.deployRule2Engine(rule, ip);
- correlationRuleDao.updateRule(rule);
- } catch (CorrelationException e) {
- throw new CorrelationException(String.format("Failed to allocate rule <%s> to <%s>",
- rule.getName(), ip), e);
+ // Retry for a couple of times in case of deployment failure
+ // due to unfinished initialization procedures of engine instances.
+ for (int i = 0; i <= RETRY_TIMES; ++i) {
+ try {
+ ruleMgtWrapper.deployRule2Engine(rule, ip);
+ correlationRuleDao.updateRule(rule);
+ // If the codes reach here, it means everything's okay. There's no need to run the loop more.
+ break;
+ } catch (CorrelationException e) {
+ LOGGER.warn(String.format("Failed to allocate rule <%s> to <%s>. Retry: %d.",
+ rule.getName(), ip, i), e);
+ if (i == RETRY_TIMES) {
+ throw new CorrelationException(String.format("Failed to allocate rule <%s> to <%s>",
+ rule.getName(), ip), e);
+ }
+ try {
+ SECONDS.sleep(RETRY_INTERVAL_SEC * (i + 1));
+ } catch (InterruptedException interruptedException) {
+ LOGGER.info(interruptedException.getMessage(), interruptedException);
+ }
+ }
}
}
diff --git a/rulemgt/src/main/java/org/onap/holmes/rulemgt/constant/RuleMgtConstant.java b/rulemgt/src/main/java/org/onap/holmes/rulemgt/constant/RuleMgtConstant.java
index 5ac4fe6..73a2f2b 100644
--- a/rulemgt/src/main/java/org/onap/holmes/rulemgt/constant/RuleMgtConstant.java
+++ b/rulemgt/src/main/java/org/onap/holmes/rulemgt/constant/RuleMgtConstant.java
@@ -1,29 +1,29 @@
-/**
- * Copyright 2017 ZTE Corporation.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.onap.holmes.rulemgt.constant;
-
-public class RuleMgtConstant {
-
- private RuleMgtConstant() {
-
- }
- public static final int STATUS_RULE_OPEN = 1;
- public static final int STATUS_RULE_CLOSE = 0;
- public static final int STATUS_RULE_ALL = 2;
- public static final String PACKAGE = "packageName";
- public static final String ENGINE_PATH = "/api/holmes-engine-mgmt/v1/rule";
- public static final int RESPONSE_STATUS_OK = 200;
-}
+/**
+ * Copyright 2017 ZTE Corporation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onap.holmes.rulemgt.constant;
+
+public class RuleMgtConstant {
+
+ private RuleMgtConstant() {
+
+ }
+ public static final int STATUS_ENABLED = 1;
+ public static final int STATUS_DISABLED = 0;
+ public static final int STATUS_RULE_ALL = 2;
+ public static final String PACKAGE = "packageName";
+ public static final String ENGINE_PATH = "/api/holmes-engine-mgmt/v1/rule";
+ public static final int RESPONSE_STATUS_OK = 200;
+}
diff --git a/rulemgt/src/main/java/org/onap/holmes/rulemgt/wrapper/RuleMgtWrapper.java b/rulemgt/src/main/java/org/onap/holmes/rulemgt/wrapper/RuleMgtWrapper.java
index 22982da..7fdae27 100644
--- a/rulemgt/src/main/java/org/onap/holmes/rulemgt/wrapper/RuleMgtWrapper.java
+++ b/rulemgt/src/main/java/org/onap/holmes/rulemgt/wrapper/RuleMgtWrapper.java
@@ -1,279 +1,288 @@
-/**
- * Copyright 2017 ZTE Corporation.
- * <p>
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- * <p>
- * http://www.apache.org/licenses/LICENSE-2.0
- * <p>
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.onap.holmes.rulemgt.wrapper;
-
-import java.util.ArrayList;
-import java.util.Date;
-import java.util.List;
-import javax.annotation.PostConstruct;
-import javax.inject.Inject;
-import javax.inject.Singleton;
-
-import lombok.extern.slf4j.Slf4j;
-import org.jvnet.hk2.annotations.Service;
-import org.onap.holmes.rulemgt.bean.request.CorrelationCheckRule4Engine;
-import org.onap.holmes.rulemgt.bean.response.RuleResult4API;
-import org.onap.holmes.rulemgt.constant.RuleMgtConstant;
-import org.onap.holmes.rulemgt.db.CorrelationRuleDao;
-import org.onap.holmes.common.api.entity.CorrelationRule;
-import org.onap.holmes.common.exception.CorrelationException;
-import org.onap.holmes.common.utils.DbDaoUtil;
-import org.onap.holmes.rulemgt.bean.request.CorrelationDeployRule4Engine;
-import org.onap.holmes.rulemgt.bean.request.RuleCreateRequest;
-import org.onap.holmes.rulemgt.bean.request.RuleDeleteRequest;
-import org.onap.holmes.rulemgt.bean.request.RuleQueryCondition;
-import org.onap.holmes.rulemgt.bean.request.RuleUpdateRequest;
-import org.onap.holmes.rulemgt.bean.response.RuleAddAndUpdateResponse;
-import org.onap.holmes.rulemgt.bean.response.RuleQueryListResponse;
-import org.onap.holmes.rulemgt.bolt.enginebolt.EngineWrapper;
-import org.onap.holmes.rulemgt.db.CorrelationRuleQueryDao;
-import org.onap.holmes.rulemgt.tools.EngineTools;
-
-
-@Service
-@Singleton
-@Slf4j
-public class RuleMgtWrapper {
-
- @Inject
- private EngineTools engineTools;
-
- @Inject
- private RuleQueryWrapper ruleQueryWrapper;
-
- @Inject
- private CorrelationRuleQueryDao correlationRuleQueryDao;
- @Inject
- private EngineWrapper engineWarpper;
- @Inject
- private DbDaoUtil daoUtil;
-
- private CorrelationRuleDao correlationRuleDao;
-
- @PostConstruct
- public void initDaoUtil() {
- correlationRuleDao = daoUtil.getJdbiDaoByOnDemand(CorrelationRuleDao.class);
- }
-
- public RuleAddAndUpdateResponse addCorrelationRule(String creator, RuleCreateRequest ruleCreateRequest)
- throws CorrelationException {
- if (ruleCreateRequest == null) {
- throw new CorrelationException("The request object can not be empty!");
- }
- CorrelationRule correlationRule = convertCreateRequest2Rule(creator,
- ruleCreateRequest);
- checkCorrelation(correlationRule);
- CorrelationRule ruleTemp = correlationRuleDao.queryRuleByRuleName(correlationRule.getName());
- if (ruleTemp != null) {
- throw new CorrelationException("A rule with the same name already exists.");
- }
- String ip = "";
- try {
- ip = engineTools.getEngineWithLeastRules();
- } catch (Exception e) {
- log.error("When adding rules, can not get engine instance ip");
- }
- String packageName = deployRule2Engine(correlationRule, ip);
- correlationRule.setPackageName(packageName);
- correlationRule.setEngineInstance(ip);
- CorrelationRule result = null;
- try {
- result = correlationRuleDao.saveRule(correlationRule);
- } catch (CorrelationException e) {
- engineWarpper.deleteRuleFromEngine(packageName, ip);
- throw new CorrelationException(e.getMessage(), e);
- }
- RuleAddAndUpdateResponse ruleAddAndUpdateResponse = new RuleAddAndUpdateResponse();
- ruleAddAndUpdateResponse.setRuleId(result.getRid());
- return ruleAddAndUpdateResponse;
- }
-
- public RuleAddAndUpdateResponse updateCorrelationRule(String modifier, RuleUpdateRequest ruleUpdateRequest)
- throws CorrelationException {
- if (ruleUpdateRequest == null) {
- throw new CorrelationException("The request object can not be empty!");
- }
- CorrelationRule oldCorrelationRule = correlationRuleDao.queryRuleByRid(ruleUpdateRequest.getRuleId());
- if (oldCorrelationRule == null) {
- throw new CorrelationException("You're trying to update a rule which does not exist in the system.");
- }
- String updateIp = "";
- updateIp = oldCorrelationRule.getEngineInstance();
- CorrelationRule newCorrelationRule = convertRuleUpdateRequest2CorrelationRule(modifier,
- ruleUpdateRequest, oldCorrelationRule.getName());
- newCorrelationRule.setEngineInstance(updateIp);
- checkCorrelation(newCorrelationRule);
- RuleAddAndUpdateResponse ruleChangeResponse = new RuleAddAndUpdateResponse();
- ruleChangeResponse.setRuleId(newCorrelationRule.getRid());
-
- if (!haveChange(newCorrelationRule, oldCorrelationRule)) {
- return ruleChangeResponse;
- }
- if (oldCorrelationRule.getEnabled() == RuleMgtConstant.STATUS_RULE_OPEN) {
- String oldRuleEngineInstance = oldCorrelationRule.getEngineInstance();
- engineWarpper.deleteRuleFromEngine(oldCorrelationRule.getPackageName(), oldRuleEngineInstance);
- }
- newCorrelationRule.setPackageName(deployRule2Engine(newCorrelationRule, updateIp));
- correlationRuleDao.updateRule(newCorrelationRule);
- return ruleChangeResponse;
- }
-
- private void checkCorrelation(CorrelationRule correlationRule) throws CorrelationException {
- int enabled = correlationRule.getEnabled();
- String ruleName = correlationRule.getName() == null ? "" : correlationRule.getName().trim();
- String content = correlationRule.getContent() == null ? "" : correlationRule.getContent().trim();
- if ("".equals(content)) {
- throw new CorrelationException("The contents of the rule can not be empty!");
- }
- if (enabled != RuleMgtConstant.STATUS_RULE_CLOSE
- && enabled != RuleMgtConstant.STATUS_RULE_OPEN) {
- throw new CorrelationException("Invalid rule status. Only 0 (disabled) and 1 (enabled) are allowed.");
- }
- if ("".equals(ruleName)) {
- throw new CorrelationException("The name of the rule can not be empty.");
- }
- }
-
- private boolean haveChange(CorrelationRule newCorrelationRule, CorrelationRule oldCorrelationRule) {
- String newContent = newCorrelationRule.getContent();
- String oldContent = oldCorrelationRule.getContent();
- int newEnabled = newCorrelationRule.getEnabled();
- int oldEnabled = oldCorrelationRule.getEnabled();
- String newDes = newCorrelationRule.getDescription();
- String oldDes = oldCorrelationRule.getDescription();
- String oldControlLoop = oldCorrelationRule.getClosedControlLoopName();
- String newControlLoop = newCorrelationRule.getClosedControlLoopName();
- if (newContent.equals(oldContent) && newEnabled == oldEnabled
- && newDes.equals(oldDes) && newControlLoop.equals(oldControlLoop)) {
- return false;
- }
- return true;
- }
-
- public void deleteCorrelationRule(RuleDeleteRequest ruleDeleteRequest)
- throws CorrelationException {
- if (ruleDeleteRequest == null) {
- throw new CorrelationException("The request object can not be empty!");
- }
- CorrelationRule correlationRule = correlationRuleDao.queryRuleByRid(ruleDeleteRequest.getRuleId());
- if (correlationRule == null) {
- log.warn("the rule:rule id=" + ruleDeleteRequest.getRuleId() + " does not exist the database.");
- throw new CorrelationException("You're trying to delete a rule which does not exist in the system.");
- }
- if (correlationRule.getEnabled() == RuleMgtConstant.STATUS_RULE_OPEN) {
- String ip = correlationRule.getEngineInstance();
- engineWarpper.deleteRuleFromEngine(correlationRule.getPackageName(), ip);
- }
- correlationRuleDao.deleteRule(correlationRule);
- }
-
- private CorrelationRule convertCreateRequest2Rule(String userName,
- RuleCreateRequest ruleCreateRequest) throws CorrelationException {
- String tempContent = ruleCreateRequest.getContent();
- CorrelationRule correlationRule = new CorrelationRule();
- String ruleId = "rule_" + System.currentTimeMillis();
- String description = ruleCreateRequest.getDescription() == null ? "" : ruleCreateRequest.getDescription();
- correlationRule.setRid(ruleId);
- if (tempContent != null) {
- correlationRule.setContent(tempContent.trim());
- }
- correlationRule.setDescription(description);
- correlationRule.setCreateTime(new Date());
- correlationRule.setUpdateTime(new Date());
- correlationRule.setName(ruleCreateRequest.getRuleName());
- correlationRule.setEngineID("correlation-d");
- correlationRule.setEngineType("");
- correlationRule.setTemplateID(0);
- correlationRule.setVendor("");
- correlationRule.setCreator(userName);
- correlationRule.setModifier(userName);
- correlationRule.setEnabled(ruleCreateRequest.getEnabled());
- correlationRule.setClosedControlLoopName(ruleCreateRequest.getLoopControlName());
- return correlationRule;
- }
-
- private CorrelationRule convertRuleUpdateRequest2CorrelationRule(String modifier,
- RuleUpdateRequest ruleUpdateRequest, String ruleName) throws CorrelationException {
- CorrelationRule correlationRule = new CorrelationRule();
- String description = ruleUpdateRequest.getDescription() == null ? "" : ruleUpdateRequest.getDescription();
- correlationRule.setRid(ruleUpdateRequest.getRuleId());
- correlationRule.setContent(ruleUpdateRequest.getContent());
- correlationRule.setDescription(description);
- correlationRule.setEnabled(ruleUpdateRequest.getEnabled());
- correlationRule.setUpdateTime(new Date());
- correlationRule.setModifier(modifier);
- correlationRule.setName(ruleName);
- correlationRule.setClosedControlLoopName(ruleUpdateRequest.getLoopControlName());
- return correlationRule;
- }
-
- public String deployRule2Engine(CorrelationRule correlationRule, String ip)
- throws CorrelationException {
- if (engineWarpper.checkRuleFromEngine(toCorrelationCheckRule(correlationRule), ip) && (
- correlationRule.getEnabled() == RuleMgtConstant.STATUS_RULE_OPEN)) {
- return engineWarpper.deployEngine(correlationRules2DeployRule(correlationRule), ip);
- }
- return "";
- }
-
- public RuleQueryListResponse getCorrelationRuleByCondition(
- RuleQueryCondition ruleQueryCondition) throws CorrelationException {
- List<CorrelationRule> correlationRule = correlationRuleQueryDao
- .getCorrelationRulesByCondition(ruleQueryCondition);
- RuleQueryListResponse ruleQueryListResponse = new RuleQueryListResponse();
- ruleQueryListResponse.setTotalCount(correlationRule.size());
- ruleQueryListResponse
- .setCorrelationRules(correlationRules2RuleResult4APIs(correlationRule));
- return ruleQueryListResponse;
- }
-
- private List<RuleResult4API> correlationRules2RuleResult4APIs(
- List<CorrelationRule> correlationRules) {
- List<RuleResult4API> ruleResult4APIs = new ArrayList<RuleResult4API>();
- for (CorrelationRule correlationRule : correlationRules) {
- RuleResult4API ruleResult4API = new RuleResult4API();
- String description = correlationRule.getDescription() == null ? "" : correlationRule.getDescription();
- ruleResult4API.setRuleId(correlationRule.getRid());
- ruleResult4API.setRuleName(correlationRule.getName());
- ruleResult4API.setDescription(description);
- ruleResult4API.setContent(correlationRule.getContent());
- ruleResult4API.setCreateTime(correlationRule.getCreateTime());
- ruleResult4API.setCreator(correlationRule.getCreator());
- ruleResult4API.setUpdateTime(correlationRule.getUpdateTime());
- ruleResult4API.setModifier(correlationRule.getModifier());
- ruleResult4API.setEnabled(correlationRule.getEnabled());
- ruleResult4API.setLoopControlName(correlationRule.getClosedControlLoopName());
- ruleResult4APIs.add(ruleResult4API);
- }
- return ruleResult4APIs;
- }
-
- private CorrelationDeployRule4Engine correlationRules2DeployRule(
- CorrelationRule correlationRule) {
- CorrelationDeployRule4Engine correlationDeployRule4Engine = new CorrelationDeployRule4Engine();
- correlationDeployRule4Engine.setContent(correlationRule.getContent());
- correlationDeployRule4Engine.setEngineId(correlationRule.getEngineID());
- correlationDeployRule4Engine.setLoopControlName(correlationRule.getClosedControlLoopName());
- return correlationDeployRule4Engine;
- }
-
- private CorrelationCheckRule4Engine toCorrelationCheckRule(
- CorrelationRule correlationRule) {
- CorrelationCheckRule4Engine correlationCheckRule4Engine = new CorrelationCheckRule4Engine();
- correlationCheckRule4Engine.setContent(correlationRule.getContent());
- return correlationCheckRule4Engine;
- }
-}
+/**
+ * Copyright 2017 ZTE Corporation.
+ * <p>
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * <p>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p>
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onap.holmes.rulemgt.wrapper;
+
+import java.util.ArrayList;
+import java.util.Date;
+import java.util.List;
+import javax.annotation.PostConstruct;
+import javax.inject.Inject;
+import javax.inject.Singleton;
+
+import lombok.extern.slf4j.Slf4j;
+import org.jvnet.hk2.annotations.Service;
+import org.onap.holmes.rulemgt.bean.request.CorrelationCheckRule4Engine;
+import org.onap.holmes.rulemgt.bean.response.RuleResult4API;
+import org.onap.holmes.rulemgt.constant.RuleMgtConstant;
+import org.onap.holmes.rulemgt.db.CorrelationRuleDao;
+import org.onap.holmes.common.api.entity.CorrelationRule;
+import org.onap.holmes.common.exception.CorrelationException;
+import org.onap.holmes.common.utils.DbDaoUtil;
+import org.onap.holmes.rulemgt.bean.request.CorrelationDeployRule4Engine;
+import org.onap.holmes.rulemgt.bean.request.RuleCreateRequest;
+import org.onap.holmes.rulemgt.bean.request.RuleDeleteRequest;
+import org.onap.holmes.rulemgt.bean.request.RuleQueryCondition;
+import org.onap.holmes.rulemgt.bean.request.RuleUpdateRequest;
+import org.onap.holmes.rulemgt.bean.response.RuleAddAndUpdateResponse;
+import org.onap.holmes.rulemgt.bean.response.RuleQueryListResponse;
+import org.onap.holmes.rulemgt.bolt.enginebolt.EngineWrapper;
+import org.onap.holmes.rulemgt.db.CorrelationRuleQueryDao;
+import org.onap.holmes.rulemgt.tools.EngineTools;
+
+
+@Service
+@Singleton
+@Slf4j
+public class RuleMgtWrapper {
+
+ @Inject
+ private EngineTools engineTools;
+
+ @Inject
+ private RuleQueryWrapper ruleQueryWrapper;
+
+ @Inject
+ private CorrelationRuleQueryDao correlationRuleQueryDao;
+ @Inject
+ private EngineWrapper engineWarpper;
+ @Inject
+ private DbDaoUtil daoUtil;
+
+ private CorrelationRuleDao correlationRuleDao;
+
+ @PostConstruct
+ public void initDaoUtil() {
+ correlationRuleDao = daoUtil.getJdbiDaoByOnDemand(CorrelationRuleDao.class);
+ }
+
+ public RuleAddAndUpdateResponse addCorrelationRule(String creator, RuleCreateRequest ruleCreateRequest)
+ throws CorrelationException {
+ if (ruleCreateRequest == null) {
+ throw new CorrelationException("The request object can not be empty!");
+ }
+ CorrelationRule correlationRule = convertCreateRequest2Rule(creator,
+ ruleCreateRequest);
+ validateCorrelationRule(correlationRule);
+ CorrelationRule ruleTemp = correlationRuleDao.queryRuleByRuleName(correlationRule.getName());
+ if (ruleTemp != null) {
+ throw new CorrelationException("A rule with the same name already exists.");
+ }
+ String ip = "";
+ try {
+ ip = engineTools.getEngineWithLeastRules();
+ } catch (Exception e) {
+ log.error("When adding rules, can not get engine instance ip");
+ }
+ String packageName = deployRule2Engine(correlationRule, ip);
+ correlationRule.setPackageName(packageName);
+ correlationRule.setEngineInstance(ip);
+ CorrelationRule result = null;
+ try {
+ result = correlationRuleDao.saveRule(correlationRule);
+ } catch (CorrelationException e) {
+ engineWarpper.deleteRuleFromEngine(packageName, ip);
+ throw new CorrelationException(e.getMessage(), e);
+ }
+ RuleAddAndUpdateResponse ruleAddAndUpdateResponse = new RuleAddAndUpdateResponse();
+ ruleAddAndUpdateResponse.setRuleId(result.getRid());
+ return ruleAddAndUpdateResponse;
+ }
+
+ public RuleAddAndUpdateResponse updateCorrelationRule(String modifier, RuleUpdateRequest ruleUpdateRequest)
+ throws CorrelationException {
+ if (ruleUpdateRequest == null) {
+ throw new CorrelationException("The request object can not be empty!");
+ }
+ CorrelationRule oldCorrelationRule = correlationRuleDao.queryRuleByRid(ruleUpdateRequest.getRuleId());
+ if (oldCorrelationRule == null) {
+ throw new CorrelationException("You're trying to update a rule which does not exist in the system.");
+ }
+
+ String updateIp = oldCorrelationRule.getEngineInstance();
+ if (!checkIfEngineExists(updateIp)) {
+ updateIp = engineTools.getEngineWithLeastRules();
+ }
+ CorrelationRule newCorrelationRule = convertRuleUpdateRequest2CorrelationRule(modifier,
+ ruleUpdateRequest, oldCorrelationRule.getName());
+ newCorrelationRule.setEngineInstance(updateIp);
+ validateCorrelationRule(newCorrelationRule);
+ RuleAddAndUpdateResponse ruleChangeResponse = new RuleAddAndUpdateResponse();
+ ruleChangeResponse.setRuleId(newCorrelationRule.getRid());
+
+ if (!checkIfRuleChanged(newCorrelationRule, oldCorrelationRule)) {
+ return ruleChangeResponse;
+ }
+ String engineInstance = oldCorrelationRule.getEngineInstance();
+ if (oldCorrelationRule.getEnabled() == RuleMgtConstant.STATUS_ENABLED
+ && checkIfEngineExists(engineInstance)) {
+ engineWarpper.deleteRuleFromEngine(oldCorrelationRule.getPackageName(), engineInstance);
+ }
+ newCorrelationRule.setPackageName(deployRule2Engine(newCorrelationRule, updateIp));
+ correlationRuleDao.updateRule(newCorrelationRule);
+ return ruleChangeResponse;
+ }
+
+ private void validateCorrelationRule(CorrelationRule correlationRule) throws CorrelationException {
+ int enabled = correlationRule.getEnabled();
+ String ruleName = correlationRule.getName() == null ? "" : correlationRule.getName().trim();
+ String content = correlationRule.getContent() == null ? "" : correlationRule.getContent().trim();
+ if ("".equals(content)) {
+ throw new CorrelationException("The contents of the rule can not be empty!");
+ }
+ if (enabled != RuleMgtConstant.STATUS_DISABLED
+ && enabled != RuleMgtConstant.STATUS_ENABLED) {
+ throw new CorrelationException("Invalid rule status. Only 0 (disabled) and 1 (enabled) are allowed.");
+ }
+ if ("".equals(ruleName)) {
+ throw new CorrelationException("The name of the rule can not be empty.");
+ }
+ }
+
+ private boolean checkIfRuleChanged(CorrelationRule newCorrelationRule, CorrelationRule oldCorrelationRule) {
+ String newContent = newCorrelationRule.getContent();
+ String oldContent = oldCorrelationRule.getContent();
+ int newEnabled = newCorrelationRule.getEnabled();
+ int oldEnabled = oldCorrelationRule.getEnabled();
+ String newDes = newCorrelationRule.getDescription();
+ String oldDes = oldCorrelationRule.getDescription();
+ String oldControlLoop = oldCorrelationRule.getClosedControlLoopName();
+ String newControlLoop = newCorrelationRule.getClosedControlLoopName();
+ if (newContent.equals(oldContent) && newEnabled == oldEnabled
+ && newDes.equals(oldDes) && newControlLoop.equals(oldControlLoop)) {
+ return false;
+ }
+ return true;
+ }
+
+ public void deleteCorrelationRule(RuleDeleteRequest ruleDeleteRequest)
+ throws CorrelationException {
+ if (ruleDeleteRequest == null) {
+ throw new CorrelationException("The request object can not be empty!");
+ }
+ CorrelationRule correlationRule = correlationRuleDao.queryRuleByRid(ruleDeleteRequest.getRuleId());
+ if (correlationRule == null) {
+ log.warn("the rule:rule id=" + ruleDeleteRequest.getRuleId() + " does not exist the database.");
+ throw new CorrelationException("You're trying to delete a rule which does not exist in the system.");
+ }
+ if (correlationRule.getEnabled() == RuleMgtConstant.STATUS_ENABLED) {
+ String ip = correlationRule.getEngineInstance();
+ engineWarpper.deleteRuleFromEngine(correlationRule.getPackageName(), ip);
+ }
+ correlationRuleDao.deleteRule(correlationRule);
+ }
+
+ private CorrelationRule convertCreateRequest2Rule(String userName,
+ RuleCreateRequest ruleCreateRequest) throws CorrelationException {
+ String tempContent = ruleCreateRequest.getContent();
+ CorrelationRule correlationRule = new CorrelationRule();
+ String ruleId = "rule_" + System.currentTimeMillis();
+ String description = ruleCreateRequest.getDescription() == null ? "" : ruleCreateRequest.getDescription();
+ correlationRule.setRid(ruleId);
+ if (tempContent != null) {
+ correlationRule.setContent(tempContent.trim());
+ }
+ correlationRule.setDescription(description);
+ correlationRule.setCreateTime(new Date());
+ correlationRule.setUpdateTime(new Date());
+ correlationRule.setName(ruleCreateRequest.getRuleName());
+ correlationRule.setEngineID("correlation-d");
+ correlationRule.setEngineType("");
+ correlationRule.setTemplateID(0);
+ correlationRule.setVendor("");
+ correlationRule.setCreator(userName);
+ correlationRule.setModifier(userName);
+ correlationRule.setEnabled(ruleCreateRequest.getEnabled());
+ correlationRule.setClosedControlLoopName(ruleCreateRequest.getLoopControlName());
+ return correlationRule;
+ }
+
+ private CorrelationRule convertRuleUpdateRequest2CorrelationRule(String modifier,
+ RuleUpdateRequest ruleUpdateRequest, String ruleName) throws CorrelationException {
+ CorrelationRule correlationRule = new CorrelationRule();
+ String description = ruleUpdateRequest.getDescription() == null ? "" : ruleUpdateRequest.getDescription();
+ correlationRule.setRid(ruleUpdateRequest.getRuleId());
+ correlationRule.setContent(ruleUpdateRequest.getContent());
+ correlationRule.setDescription(description);
+ correlationRule.setEnabled(ruleUpdateRequest.getEnabled());
+ correlationRule.setUpdateTime(new Date());
+ correlationRule.setModifier(modifier);
+ correlationRule.setName(ruleName);
+ correlationRule.setClosedControlLoopName(ruleUpdateRequest.getLoopControlName());
+ return correlationRule;
+ }
+
+ public String deployRule2Engine(CorrelationRule correlationRule, String ip)
+ throws CorrelationException {
+ if (engineWarpper.checkRuleFromEngine(toCorrelationCheckRule(correlationRule), ip) && (
+ correlationRule.getEnabled() == RuleMgtConstant.STATUS_ENABLED)) {
+ return engineWarpper.deployEngine(correlationRules2DeployRule(correlationRule), ip);
+ }
+ return "";
+ }
+
+ public RuleQueryListResponse getCorrelationRuleByCondition(
+ RuleQueryCondition ruleQueryCondition) throws CorrelationException {
+ List<CorrelationRule> correlationRule = correlationRuleQueryDao
+ .getCorrelationRulesByCondition(ruleQueryCondition);
+ RuleQueryListResponse ruleQueryListResponse = new RuleQueryListResponse();
+ ruleQueryListResponse.setTotalCount(correlationRule.size());
+ ruleQueryListResponse
+ .setCorrelationRules(correlationRules2RuleResult4APIs(correlationRule));
+ return ruleQueryListResponse;
+ }
+
+ private List<RuleResult4API> correlationRules2RuleResult4APIs(
+ List<CorrelationRule> correlationRules) {
+ List<RuleResult4API> ruleResult4APIs = new ArrayList<RuleResult4API>();
+ for (CorrelationRule correlationRule : correlationRules) {
+ RuleResult4API ruleResult4API = new RuleResult4API();
+ String description = correlationRule.getDescription() == null ? "" : correlationRule.getDescription();
+ ruleResult4API.setRuleId(correlationRule.getRid());
+ ruleResult4API.setRuleName(correlationRule.getName());
+ ruleResult4API.setDescription(description);
+ ruleResult4API.setContent(correlationRule.getContent());
+ ruleResult4API.setCreateTime(correlationRule.getCreateTime());
+ ruleResult4API.setCreator(correlationRule.getCreator());
+ ruleResult4API.setUpdateTime(correlationRule.getUpdateTime());
+ ruleResult4API.setModifier(correlationRule.getModifier());
+ ruleResult4API.setEnabled(correlationRule.getEnabled());
+ ruleResult4API.setLoopControlName(correlationRule.getClosedControlLoopName());
+ ruleResult4APIs.add(ruleResult4API);
+ }
+ return ruleResult4APIs;
+ }
+
+ private CorrelationDeployRule4Engine correlationRules2DeployRule(
+ CorrelationRule correlationRule) {
+ CorrelationDeployRule4Engine correlationDeployRule4Engine = new CorrelationDeployRule4Engine();
+ correlationDeployRule4Engine.setContent(correlationRule.getContent());
+ correlationDeployRule4Engine.setEngineId(correlationRule.getEngineID());
+ correlationDeployRule4Engine.setLoopControlName(correlationRule.getClosedControlLoopName());
+ return correlationDeployRule4Engine;
+ }
+
+ private CorrelationCheckRule4Engine toCorrelationCheckRule(
+ CorrelationRule correlationRule) {
+ CorrelationCheckRule4Engine correlationCheckRule4Engine = new CorrelationCheckRule4Engine();
+ correlationCheckRule4Engine.setContent(correlationRule.getContent());
+ return correlationCheckRule4Engine;
+ }
+
+ private boolean checkIfEngineExists(String ip) {
+ List engineList = engineTools.getInstanceList();
+ return engineList.contains(ip);
+ }
+}