-/**\r
- * Copyright 2017 ZTE Corporation.\r
- * <p>\r
- * Licensed under the Apache License, Version 2.0 (the "License");\r
- * you may not use this file except in compliance with the License.\r
- * You may obtain a copy of the License at\r
- * <p>\r
- * http://www.apache.org/licenses/LICENSE-2.0\r
- * <p>\r
- * Unless required by applicable law or agreed to in writing, software\r
- * distributed under the License is distributed on an "AS IS" BASIS,\r
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
- * See the License for the specific language governing permissions and\r
- * limitations under the License.\r
- */\r
-package org.onap.holmes.rulemgt.wrapper;\r
-\r
-import java.util.ArrayList;\r
-import java.util.Date;\r
-import java.util.List;\r
-import javax.annotation.PostConstruct;\r
-import javax.inject.Inject;\r
-import javax.inject.Singleton;\r
-\r
-import lombok.extern.slf4j.Slf4j;\r
-import org.jvnet.hk2.annotations.Service;\r
-import org.onap.holmes.rulemgt.bean.request.CorrelationCheckRule4Engine;\r
-import org.onap.holmes.rulemgt.bean.response.RuleResult4API;\r
-import org.onap.holmes.rulemgt.constant.RuleMgtConstant;\r
-import org.onap.holmes.rulemgt.db.CorrelationRuleDao;\r
-import org.onap.holmes.common.api.entity.CorrelationRule;\r
-import org.onap.holmes.common.exception.CorrelationException;\r
-import org.onap.holmes.common.utils.DbDaoUtil;\r
-import org.onap.holmes.rulemgt.bean.request.CorrelationDeployRule4Engine;\r
-import org.onap.holmes.rulemgt.bean.request.RuleCreateRequest;\r
-import org.onap.holmes.rulemgt.bean.request.RuleDeleteRequest;\r
-import org.onap.holmes.rulemgt.bean.request.RuleQueryCondition;\r
-import org.onap.holmes.rulemgt.bean.request.RuleUpdateRequest;\r
-import org.onap.holmes.rulemgt.bean.response.RuleAddAndUpdateResponse;\r
-import org.onap.holmes.rulemgt.bean.response.RuleQueryListResponse;\r
-import org.onap.holmes.rulemgt.bolt.enginebolt.EngineWrapper;\r
-import org.onap.holmes.rulemgt.db.CorrelationRuleQueryDao;\r
-import org.onap.holmes.rulemgt.tools.EngineTools;\r
-\r
-\r
-@Service\r
-@Singleton\r
-@Slf4j\r
-public class RuleMgtWrapper {\r
-\r
- @Inject\r
- private EngineTools engineTools;\r
-\r
- @Inject\r
- private RuleQueryWrapper ruleQueryWrapper;\r
-\r
- @Inject\r
- private CorrelationRuleQueryDao correlationRuleQueryDao;\r
- @Inject\r
- private EngineWrapper engineWarpper;\r
- @Inject\r
- private DbDaoUtil daoUtil;\r
-\r
- private CorrelationRuleDao correlationRuleDao;\r
-\r
- @PostConstruct\r
- public void initDaoUtil() {\r
- correlationRuleDao = daoUtil.getJdbiDaoByOnDemand(CorrelationRuleDao.class);\r
- }\r
-\r
- public RuleAddAndUpdateResponse addCorrelationRule(String creator, RuleCreateRequest ruleCreateRequest)\r
- throws CorrelationException {\r
- if (ruleCreateRequest == null) {\r
- throw new CorrelationException("The request object can not be empty!");\r
- }\r
- CorrelationRule correlationRule = convertCreateRequest2Rule(creator,\r
- ruleCreateRequest);\r
- checkCorrelation(correlationRule);\r
- CorrelationRule ruleTemp = correlationRuleDao.queryRuleByRuleName(correlationRule.getName());\r
- if (ruleTemp != null) {\r
- throw new CorrelationException("A rule with the same name already exists.");\r
- }\r
- String ip = "";\r
- try {\r
- ip = engineTools.getEngineWithLeastRules();\r
- } catch (Exception e) {\r
- log.error("When adding rules, can not get engine instance ip");\r
- }\r
- String packageName = deployRule2Engine(correlationRule, ip);\r
- correlationRule.setPackageName(packageName);\r
- correlationRule.setEngineInstance(ip);\r
- CorrelationRule result = null;\r
- try {\r
- result = correlationRuleDao.saveRule(correlationRule);\r
- } catch (CorrelationException e) {\r
- engineWarpper.deleteRuleFromEngine(packageName, ip);\r
- throw new CorrelationException(e.getMessage(), e);\r
- }\r
- RuleAddAndUpdateResponse ruleAddAndUpdateResponse = new RuleAddAndUpdateResponse();\r
- ruleAddAndUpdateResponse.setRuleId(result.getRid());\r
- return ruleAddAndUpdateResponse;\r
- }\r
-\r
- public RuleAddAndUpdateResponse updateCorrelationRule(String modifier, RuleUpdateRequest ruleUpdateRequest)\r
- throws CorrelationException {\r
- if (ruleUpdateRequest == null) {\r
- throw new CorrelationException("The request object can not be empty!");\r
- }\r
- CorrelationRule oldCorrelationRule = correlationRuleDao.queryRuleByRid(ruleUpdateRequest.getRuleId());\r
- if (oldCorrelationRule == null) {\r
- throw new CorrelationException("You're trying to update a rule which does not exist in the system.");\r
- }\r
- String updateIp = "";\r
- updateIp = oldCorrelationRule.getEngineInstance();\r
- CorrelationRule newCorrelationRule = convertRuleUpdateRequest2CorrelationRule(modifier,\r
- ruleUpdateRequest, oldCorrelationRule.getName());\r
- newCorrelationRule.setEngineInstance(updateIp);\r
- checkCorrelation(newCorrelationRule);\r
- RuleAddAndUpdateResponse ruleChangeResponse = new RuleAddAndUpdateResponse();\r
- ruleChangeResponse.setRuleId(newCorrelationRule.getRid());\r
-\r
- if (!haveChange(newCorrelationRule, oldCorrelationRule)) {\r
- return ruleChangeResponse;\r
- }\r
- if (oldCorrelationRule.getEnabled() == RuleMgtConstant.STATUS_RULE_OPEN) {\r
- String oldRuleEngineInstance = oldCorrelationRule.getEngineInstance();\r
- engineWarpper.deleteRuleFromEngine(oldCorrelationRule.getPackageName(), oldRuleEngineInstance);\r
- }\r
- newCorrelationRule.setPackageName(deployRule2Engine(newCorrelationRule, updateIp));\r
- correlationRuleDao.updateRule(newCorrelationRule);\r
- return ruleChangeResponse;\r
- }\r
-\r
- private void checkCorrelation(CorrelationRule correlationRule) throws CorrelationException {\r
- int enabled = correlationRule.getEnabled();\r
- String ruleName = correlationRule.getName() == null ? "" : correlationRule.getName().trim();\r
- String content = correlationRule.getContent() == null ? "" : correlationRule.getContent().trim();\r
- if ("".equals(content)) {\r
- throw new CorrelationException("The contents of the rule can not be empty!");\r
- }\r
- if (enabled != RuleMgtConstant.STATUS_RULE_CLOSE\r
- && enabled != RuleMgtConstant.STATUS_RULE_OPEN) {\r
- throw new CorrelationException("Invalid rule status. Only 0 (disabled) and 1 (enabled) are allowed.");\r
- }\r
- if ("".equals(ruleName)) {\r
- throw new CorrelationException("The name of the rule can not be empty.");\r
- }\r
- }\r
-\r
- private boolean haveChange(CorrelationRule newCorrelationRule, CorrelationRule oldCorrelationRule) {\r
- String newContent = newCorrelationRule.getContent();\r
- String oldContent = oldCorrelationRule.getContent();\r
- int newEnabled = newCorrelationRule.getEnabled();\r
- int oldEnabled = oldCorrelationRule.getEnabled();\r
- String newDes = newCorrelationRule.getDescription();\r
- String oldDes = oldCorrelationRule.getDescription();\r
- String oldControlLoop = oldCorrelationRule.getClosedControlLoopName();\r
- String newControlLoop = newCorrelationRule.getClosedControlLoopName();\r
- if (newContent.equals(oldContent) && newEnabled == oldEnabled\r
- && newDes.equals(oldDes) && newControlLoop.equals(oldControlLoop)) {\r
- return false;\r
- }\r
- return true;\r
- }\r
-\r
- public void deleteCorrelationRule(RuleDeleteRequest ruleDeleteRequest)\r
- throws CorrelationException {\r
- if (ruleDeleteRequest == null) {\r
- throw new CorrelationException("The request object can not be empty!");\r
- }\r
- CorrelationRule correlationRule = correlationRuleDao.queryRuleByRid(ruleDeleteRequest.getRuleId());\r
- if (correlationRule == null) {\r
- log.warn("the rule:rule id=" + ruleDeleteRequest.getRuleId() + " does not exist the database.");\r
- throw new CorrelationException("You're trying to delete a rule which does not exist in the system.");\r
- }\r
- if (correlationRule.getEnabled() == RuleMgtConstant.STATUS_RULE_OPEN) {\r
- String ip = correlationRule.getEngineInstance();\r
- engineWarpper.deleteRuleFromEngine(correlationRule.getPackageName(), ip);\r
- }\r
- correlationRuleDao.deleteRule(correlationRule);\r
- }\r
-\r
- private CorrelationRule convertCreateRequest2Rule(String userName,\r
- RuleCreateRequest ruleCreateRequest) throws CorrelationException {\r
- String tempContent = ruleCreateRequest.getContent();\r
- CorrelationRule correlationRule = new CorrelationRule();\r
- String ruleId = "rule_" + System.currentTimeMillis();\r
- String description = ruleCreateRequest.getDescription() == null ? "" : ruleCreateRequest.getDescription();\r
- correlationRule.setRid(ruleId);\r
- if (tempContent != null) {\r
- correlationRule.setContent(tempContent.trim());\r
- }\r
- correlationRule.setDescription(description);\r
- correlationRule.setCreateTime(new Date());\r
- correlationRule.setUpdateTime(new Date());\r
- correlationRule.setName(ruleCreateRequest.getRuleName());\r
- correlationRule.setEngineID("correlation-d");\r
- correlationRule.setEngineType("");\r
- correlationRule.setTemplateID(0);\r
- correlationRule.setVendor("");\r
- correlationRule.setCreator(userName);\r
- correlationRule.setModifier(userName);\r
- correlationRule.setEnabled(ruleCreateRequest.getEnabled());\r
- correlationRule.setClosedControlLoopName(ruleCreateRequest.getLoopControlName());\r
- return correlationRule;\r
- }\r
-\r
- private CorrelationRule convertRuleUpdateRequest2CorrelationRule(String modifier,\r
- RuleUpdateRequest ruleUpdateRequest, String ruleName) throws CorrelationException {\r
- CorrelationRule correlationRule = new CorrelationRule();\r
- String description = ruleUpdateRequest.getDescription() == null ? "" : ruleUpdateRequest.getDescription();\r
- correlationRule.setRid(ruleUpdateRequest.getRuleId());\r
- correlationRule.setContent(ruleUpdateRequest.getContent());\r
- correlationRule.setDescription(description);\r
- correlationRule.setEnabled(ruleUpdateRequest.getEnabled());\r
- correlationRule.setUpdateTime(new Date());\r
- correlationRule.setModifier(modifier);\r
- correlationRule.setName(ruleName);\r
- correlationRule.setClosedControlLoopName(ruleUpdateRequest.getLoopControlName());\r
- return correlationRule;\r
- }\r
-\r
- public String deployRule2Engine(CorrelationRule correlationRule, String ip)\r
- throws CorrelationException {\r
- if (engineWarpper.checkRuleFromEngine(toCorrelationCheckRule(correlationRule), ip) && (\r
- correlationRule.getEnabled() == RuleMgtConstant.STATUS_RULE_OPEN)) {\r
- return engineWarpper.deployEngine(correlationRules2DeployRule(correlationRule), ip);\r
- }\r
- return "";\r
- }\r
-\r
- public RuleQueryListResponse getCorrelationRuleByCondition(\r
- RuleQueryCondition ruleQueryCondition) throws CorrelationException {\r
- List<CorrelationRule> correlationRule = correlationRuleQueryDao\r
- .getCorrelationRulesByCondition(ruleQueryCondition);\r
- RuleQueryListResponse ruleQueryListResponse = new RuleQueryListResponse();\r
- ruleQueryListResponse.setTotalCount(correlationRule.size());\r
- ruleQueryListResponse\r
- .setCorrelationRules(correlationRules2RuleResult4APIs(correlationRule));\r
- return ruleQueryListResponse;\r
- }\r
-\r
- private List<RuleResult4API> correlationRules2RuleResult4APIs(\r
- List<CorrelationRule> correlationRules) {\r
- List<RuleResult4API> ruleResult4APIs = new ArrayList<RuleResult4API>();\r
- for (CorrelationRule correlationRule : correlationRules) {\r
- RuleResult4API ruleResult4API = new RuleResult4API();\r
- String description = correlationRule.getDescription() == null ? "" : correlationRule.getDescription();\r
- ruleResult4API.setRuleId(correlationRule.getRid());\r
- ruleResult4API.setRuleName(correlationRule.getName());\r
- ruleResult4API.setDescription(description);\r
- ruleResult4API.setContent(correlationRule.getContent());\r
- ruleResult4API.setCreateTime(correlationRule.getCreateTime());\r
- ruleResult4API.setCreator(correlationRule.getCreator());\r
- ruleResult4API.setUpdateTime(correlationRule.getUpdateTime());\r
- ruleResult4API.setModifier(correlationRule.getModifier());\r
- ruleResult4API.setEnabled(correlationRule.getEnabled());\r
- ruleResult4API.setLoopControlName(correlationRule.getClosedControlLoopName());\r
- ruleResult4APIs.add(ruleResult4API);\r
- }\r
- return ruleResult4APIs;\r
- }\r
-\r
- private CorrelationDeployRule4Engine correlationRules2DeployRule(\r
- CorrelationRule correlationRule) {\r
- CorrelationDeployRule4Engine correlationDeployRule4Engine = new CorrelationDeployRule4Engine();\r
- correlationDeployRule4Engine.setContent(correlationRule.getContent());\r
- correlationDeployRule4Engine.setEngineId(correlationRule.getEngineID());\r
- correlationDeployRule4Engine.setLoopControlName(correlationRule.getClosedControlLoopName());\r
- return correlationDeployRule4Engine;\r
- }\r
-\r
- private CorrelationCheckRule4Engine toCorrelationCheckRule(\r
- CorrelationRule correlationRule) {\r
- CorrelationCheckRule4Engine correlationCheckRule4Engine = new CorrelationCheckRule4Engine();\r
- correlationCheckRule4Engine.setContent(correlationRule.getContent());\r
- return correlationCheckRule4Engine;\r
- }\r
-}\r
+/**
+ * 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);
+ }
+}