summaryrefslogtreecommitdiffstats
path: root/rulemgt/src/main/java/org/openo/holmes/rulemgt/wrapper/RuleMgtWrapper.java
diff options
context:
space:
mode:
authoryoubowu <wu.youbo@zte.com.cn>2017-02-15 14:39:20 +0800
committer6092002067 <wu.youbo@zte.com.cn>2017-02-15 15:15:04 +0800
commit0dfbbb4d600009094b842fe0b0d4f0413b37dfc1 (patch)
tree3f826fa10db3c10f61b5f0e26c163b6e045ce442 /rulemgt/src/main/java/org/openo/holmes/rulemgt/wrapper/RuleMgtWrapper.java
parenta7171ca0509f65fa2dc2146f40a0f93b9ea6f95a (diff)
Add module holms-rule-management code
Issue-ID:HOLMES-4 Change-Id: I3a22053acfdf0789acb6fcbbc5e8740d25d6b2eb Signed-off-by: youbowu <wu.youbo@zte.com.cn>
Diffstat (limited to 'rulemgt/src/main/java/org/openo/holmes/rulemgt/wrapper/RuleMgtWrapper.java')
-rw-r--r--rulemgt/src/main/java/org/openo/holmes/rulemgt/wrapper/RuleMgtWrapper.java255
1 files changed, 255 insertions, 0 deletions
diff --git a/rulemgt/src/main/java/org/openo/holmes/rulemgt/wrapper/RuleMgtWrapper.java b/rulemgt/src/main/java/org/openo/holmes/rulemgt/wrapper/RuleMgtWrapper.java
new file mode 100644
index 0000000..60bce08
--- /dev/null
+++ b/rulemgt/src/main/java/org/openo/holmes/rulemgt/wrapper/RuleMgtWrapper.java
@@ -0,0 +1,255 @@
+/**
+ * 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.openo.holmes.rulemgt.wrapper;
+
+import java.util.ArrayList;
+import java.util.Date;
+import java.util.List;
+import javax.inject.Inject;
+import javax.inject.Singleton;
+import lombok.extern.slf4j.Slf4j;
+import org.jvnet.hk2.annotations.Service;
+import org.openo.holmes.common.api.entity.CorrelationRule;
+import org.openo.holmes.common.exception.CallException;
+import org.openo.holmes.common.exception.DataFormatException;
+import org.openo.holmes.common.exception.DbException;
+import org.openo.holmes.common.utils.DbDaoUtil;
+import org.openo.holmes.common.utils.I18nProxy;
+import org.openo.holmes.rulemgt.bean.request.CorrelationCheckRule4Engine;
+import org.openo.holmes.rulemgt.bean.request.CorrelationDeployRule4Engine;
+import org.openo.holmes.rulemgt.bean.request.RuleCreateRequest;
+import org.openo.holmes.rulemgt.bean.request.RuleDeleteRequest;
+import org.openo.holmes.rulemgt.bean.request.RuleQueryCondition;
+import org.openo.holmes.rulemgt.bean.request.RuleUpdateRequest;
+import org.openo.holmes.rulemgt.bean.response.RuleAddAndUpdateResponse;
+import org.openo.holmes.rulemgt.bean.response.RuleQueryListResponse;
+import org.openo.holmes.rulemgt.bean.response.RuleResult4API;
+import org.openo.holmes.rulemgt.bolt.enginebolt.EngineWrapper;
+import org.openo.holmes.rulemgt.constant.RuleMgtConstant;
+import org.openo.holmes.rulemgt.db.CorrelationRuleDao;
+import org.openo.holmes.rulemgt.db.CorrelationRuleQueryDao;
+
+
+@Service
+@Singleton
+@Slf4j
+public class RuleMgtWrapper {
+
+ @Inject
+ private CorrelationRuleQueryDao correlationRuleQueryDao;
+ @Inject
+ private EngineWrapper engineWarpper;
+ @Inject
+ private DbDaoUtil daoUtil;
+
+ public RuleAddAndUpdateResponse addCorrelationRule(String creator,
+ RuleCreateRequest ruleCreateRequest)
+ throws DataFormatException, CallException, DbException {
+ CorrelationRule correlationRule = convertRuleCreateRequest2CorrelationRule(creator,
+ ruleCreateRequest);
+ if (correlationRule.getName() == null || "".equals(correlationRule.getName().trim())) {
+ throw new DbException(I18nProxy.RULE_MANAGEMENT_RULE_NAME_IS_EMPTY);
+ }
+ CorrelationRule ruleTemp = daoUtil.getJdbiDaoByOnDemand(CorrelationRuleDao.class)
+ .queryRuleByRuleName(correlationRule.getName());
+ if (ruleTemp != null) {
+ throw new DbException(I18nProxy.RULE_MANAGEMENT_REPEAT_RULE_NAME);
+ }
+ correlationRule.setPackageName(deployRule2Engine(correlationRule));
+ try {
+ correlationRule = daoUtil.getJdbiDaoByOnDemand(CorrelationRuleDao.class)
+ .saveRule(correlationRule);
+ } catch (RuntimeException e) {
+ throw new DbException(I18nProxy.RULE_MANAGEMENT_CREATE_RULE_FAILED, e);
+ }
+ RuleAddAndUpdateResponse ruleAddAndUpdateResponse = new RuleAddAndUpdateResponse();
+ ruleAddAndUpdateResponse.setRuleId(correlationRule.getRid());
+ return ruleAddAndUpdateResponse;
+ }
+
+ public RuleAddAndUpdateResponse updateCorrelationRule(String modifier,
+ RuleUpdateRequest ruleUpdateRequest)
+ throws DataFormatException, DbException, CallException {
+ if (ruleUpdateRequest != null) {
+ CorrelationRule oldCorrelationRule = daoUtil
+ .getJdbiDaoByOnDemand(CorrelationRuleDao.class)
+ .getRuleByRid(ruleUpdateRequest.getRuleId());
+ if (oldCorrelationRule == null) {
+ throw new DbException(I18nProxy.RULE_MANAGEMENT_RULE_NOT_EXIST_DATABASE);
+ }
+ CorrelationRule newCorrelationRule = convertRuleUpdateRequest2CorrelationRule(modifier,
+ ruleUpdateRequest);
+ checkCorrelation(newCorrelationRule, oldCorrelationRule);
+ RuleAddAndUpdateResponse ruleChangeResponse = new RuleAddAndUpdateResponse();
+ try {
+ if (oldCorrelationRule.getEnabled() == RuleMgtConstant.STATUS_RULE_OPEN) {
+ engineWarpper.deleteRuleFromEngine(oldCorrelationRule.getPackageName());
+ }
+ daoUtil.getJdbiDaoByOnDemand(CorrelationRuleDao.class)
+ .updateRule(newCorrelationRule);
+ } catch (RuntimeException e) {
+ throw new DbException(I18nProxy.RULE_MANAGEMENT_UPDATE_RULE_FAILED, e);
+ }
+ ruleChangeResponse.setRuleId(newCorrelationRule.getRid());
+ deployRule2Engine(newCorrelationRule);
+ return ruleChangeResponse;
+ } else {
+ throw new DataFormatException(I18nProxy.RULE_MANAGEMENT_REQUEST_OBJECT_IS_EMPTY);
+ }
+
+ }
+
+ public void checkCorrelation(CorrelationRule newCorrelationRule,
+ CorrelationRule oldCorrelationRule) throws DataFormatException {
+ int newEnabled = newCorrelationRule.getEnabled();
+ if (newCorrelationRule.getContent() == null) {
+ newCorrelationRule.setContent(oldCorrelationRule.getContent());
+ }
+ if (newEnabled != RuleMgtConstant.STATUS_RULE_CLOSE
+ && newEnabled != RuleMgtConstant.STATUS_RULE_OPEN) {
+ throw new DataFormatException(I18nProxy.RULE_MANAGEMENT_PARAMETER_ENABLED_ERROR);
+ }
+ }
+
+ public void deleteCorrelationRule(RuleDeleteRequest ruleDeleteRequest)
+ throws DbException, DataFormatException, CallException {
+ if (ruleDeleteRequest != null) {
+ CorrelationRule correlationRule = daoUtil
+ .getJdbiDaoByOnDemand(CorrelationRuleDao.class)
+ .getRuleByRid(ruleDeleteRequest.getRuleId());
+ if (correlationRule == null) {
+ log.warn("the rule:rule id=" + ruleDeleteRequest.getRuleId()
+ + " does not exist the database.");
+ throw new DbException(I18nProxy.RULE_MANAGEMENT_RULE_NOT_EXIST_DATABASE);
+ }
+ try {
+ if (correlationRule.getEnabled() == RuleMgtConstant.STATUS_RULE_OPEN) {
+ engineWarpper.deleteRuleFromEngine(correlationRule.getPackageName());
+ }
+ daoUtil.getJdbiDaoByOnDemand(CorrelationRuleDao.class)
+ .deleteRule(correlationRule);
+ } catch (RuntimeException e) {
+ throw new DbException(I18nProxy.RULE_MANAGEMENT_DELETE_RULE_FAILED, e);
+ }
+ } else
+
+ {
+ throw new DataFormatException(I18nProxy.RULE_MANAGEMENT_REQUEST_OBJECT_IS_EMPTY);
+ }
+
+ }
+
+ public CorrelationRule convertRuleCreateRequest2CorrelationRule(String userName,
+ RuleCreateRequest ruleCreateRequest) throws DataFormatException {
+ if (ruleCreateRequest != null) {
+ if (ruleCreateRequest.getEnabled() != RuleMgtConstant.STATUS_RULE_OPEN
+ && ruleCreateRequest.getEnabled() != RuleMgtConstant.STATUS_RULE_CLOSE) {
+ throw new DataFormatException(I18nProxy.RULE_MANAGEMENT_REQUEST_OBJECT_IS_EMPTY);
+ }
+ CorrelationRule correlationRule = new CorrelationRule();
+ String ruleId = "rule_" + System.currentTimeMillis();
+ correlationRule.setRid(ruleId);
+ correlationRule.setContent(ruleCreateRequest.getContent());
+ correlationRule.setDescription(ruleCreateRequest.getDescription());
+ correlationRule.setCreateTime(new Date());
+ correlationRule.setUpdateTime(new Date());
+ correlationRule.setName(ruleCreateRequest.getRuleName());
+ correlationRule.setEngineId("correlation-d");
+ correlationRule.setEngineType("");
+ correlationRule.setIsManual(0);
+ correlationRule.setTemplateID(0);
+ correlationRule.setVendor("");
+ correlationRule.setCreator(userName);
+ correlationRule.setModifier(userName);
+ correlationRule.setEnabled(ruleCreateRequest.getEnabled());
+ return correlationRule;
+ } else {
+ throw new DataFormatException(I18nProxy.RULE_MANAGEMENT_REQUEST_OBJECT_IS_EMPTY);
+ }
+
+ }
+
+ private CorrelationRule convertRuleUpdateRequest2CorrelationRule(String modifier,
+ RuleUpdateRequest ruleUpdateRequest) throws DataFormatException {
+ if (ruleUpdateRequest != null) {
+ CorrelationRule correlationRule = new CorrelationRule();
+ correlationRule.setRid(ruleUpdateRequest.getRuleId());
+ correlationRule.setContent(ruleUpdateRequest.getContent());
+ correlationRule.setDescription(ruleUpdateRequest.getDescription());
+ correlationRule.setEnabled(ruleUpdateRequest.getEnabled());
+ correlationRule.setUpdateTime(new Date());
+ correlationRule.setModifier(modifier);
+ return correlationRule;
+ } else {
+ throw new DataFormatException(I18nProxy.RULE_MANAGEMENT_REQUEST_OBJECT_IS_EMPTY);
+ }
+
+ }
+
+ private String deployRule2Engine(CorrelationRule correlationRule) throws CallException {
+ if (engineWarpper.checkRuleFromEngine(correlationRules2CheckRule(correlationRule))) {
+ if (correlationRule.getEnabled() == RuleMgtConstant.STATUS_RULE_OPEN) {
+ return engineWarpper.deployEngine(correlationRules2DeployRule(correlationRule));
+ }
+ }
+ return "";
+ }
+
+ public RuleQueryListResponse getCorrelationRuleByCondition(
+ RuleQueryCondition ruleQueryCondition) throws DataFormatException, DbException {
+ 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();
+ ruleResult4API.setRuleId(correlationRule.getRid());
+ ruleResult4API.setRuleName(correlationRule.getName());
+ ruleResult4API.setDescription(correlationRule.getDescription());
+ ruleResult4API.setContent(correlationRule.getContent());
+ ruleResult4API.setCreateTime(correlationRule.getCreateTime());
+ ruleResult4API.setCreator(correlationRule.getCreator());
+ ruleResult4API.setUpdateTime(correlationRule.getUpdateTime());
+ ruleResult4API.setModifier(correlationRule.getModifier());
+ ruleResult4API.setEnabled(correlationRule.getEnabled());
+ ruleResult4APIs.add(ruleResult4API);
+ }
+ return ruleResult4APIs;
+ }
+
+ private CorrelationDeployRule4Engine correlationRules2DeployRule(
+ CorrelationRule correlationRule) {
+ CorrelationDeployRule4Engine correlationDeployRule4Engine = new CorrelationDeployRule4Engine();
+ correlationDeployRule4Engine.setContent(correlationRule.getContent());
+ correlationDeployRule4Engine.setEngineId(correlationRule.getEngineId());
+ return correlationDeployRule4Engine;
+ }
+
+ private CorrelationCheckRule4Engine correlationRules2CheckRule(
+ CorrelationRule correlationRule) {
+ CorrelationCheckRule4Engine correlationCheckRule4Engine = new CorrelationCheckRule4Engine();
+ correlationCheckRule4Engine.setContent(correlationRule.getContent());
+ return correlationCheckRule4Engine;
+ }
+}