diff options
Diffstat (limited to 'rulemgt/src')
4 files changed, 770 insertions, 739 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); + } +} diff --git a/rulemgt/src/test/java/org/onap/holmes/rulemgt/wrapper/RuleMgtWrapperTest.java b/rulemgt/src/test/java/org/onap/holmes/rulemgt/wrapper/RuleMgtWrapperTest.java index 2a8357c..626b91d 100644 --- a/rulemgt/src/test/java/org/onap/holmes/rulemgt/wrapper/RuleMgtWrapperTest.java +++ b/rulemgt/src/test/java/org/onap/holmes/rulemgt/wrapper/RuleMgtWrapperTest.java @@ -1,424 +1,428 @@ -/**
- * 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.wrapper;
-
-
-import org.easymock.EasyMock;
-import org.junit.Before;
-import org.junit.Rule;
-import org.junit.Test;
-import org.junit.rules.ExpectedException;
-import org.junit.runner.RunWith;
-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.*;
-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.CorrelationRuleDao;
-import org.onap.holmes.rulemgt.db.CorrelationRuleQueryDao;
-import org.onap.holmes.rulemgt.tools.EngineTools;
-import org.powermock.api.easymock.PowerMock;
-import org.powermock.modules.junit4.PowerMockRunner;
-import org.powermock.reflect.Whitebox;
-
-import java.util.ArrayList;
-import java.util.Date;
-import java.util.List;
-
-import static org.hamcrest.MatcherAssert.assertThat;
-import static org.hamcrest.Matchers.equalTo;
-import static org.hamcrest.Matchers.is;
-
-@RunWith(PowerMockRunner.class)
-public class RuleMgtWrapperTest {
-
- @Rule
- public ExpectedException thrown = ExpectedException.none();
-
- private RuleMgtWrapper ruleMgtWrapper;
-
- private EngineWrapper engineWrapperMock;
-
- private DbDaoUtil dbDaoUtilMock;
-
- private CorrelationRuleQueryDao correlationRuleQueryDaoMock;
-
- private CorrelationRuleDao correlationRuleDaoMock;
-
- private EngineTools engineToolsMock;
-
- private static final String USER_NAME = "admin";
-
- @Before
- public void setUp() throws Exception {
-
- ruleMgtWrapper = new RuleMgtWrapper();
-
- engineWrapperMock = PowerMock.createMock(EngineWrapper.class);
- correlationRuleQueryDaoMock = PowerMock.createMock(CorrelationRuleQueryDao.class);
- dbDaoUtilMock = PowerMock.createMock(DbDaoUtil.class);
- correlationRuleDaoMock = PowerMock.createMock(CorrelationRuleDao.class);
- engineToolsMock = PowerMock.createMock(EngineTools.class);
-
- Whitebox.setInternalState(ruleMgtWrapper, "daoUtil", dbDaoUtilMock);
- Whitebox.setInternalState(ruleMgtWrapper, "correlationRuleQueryDao", correlationRuleQueryDaoMock);
- Whitebox.setInternalState(ruleMgtWrapper, "engineWarpper", engineWrapperMock);
- Whitebox.setInternalState(ruleMgtWrapper, "correlationRuleDao", correlationRuleDaoMock);
- Whitebox.setInternalState(ruleMgtWrapper,"engineTools", engineToolsMock);
-
- PowerMock.resetAll();
- }
-
- @Test
- public void initDaoUtil_normal() {
- ruleMgtWrapper.initDaoUtil();
- }
-
- @Test
- public void addCorrelationRule_name_is_null() throws Exception {
- thrown.expect(CorrelationException.class);
- thrown.expectMessage("The name of the rule can not be empty.");
-
- ruleMgtWrapper.addCorrelationRule(USER_NAME, createRuleCreateRequest(null, "This is a rule for testing.",
- "Mocked contents.", 0));
- }
-
- @Test
- public void addCorrelationRule_request_null() throws Exception {
- thrown.expect(CorrelationException.class);
- thrown.expectMessage("The request object can not be empty!");
-
- ruleMgtWrapper.addCorrelationRule(USER_NAME, null);
- }
-
- @Test
- public void addCorrelationRule_name_is_empty() throws Exception {
- thrown.expect(CorrelationException.class);
- thrown.expectMessage("The name of the rule can not be empty.");
-
- ruleMgtWrapper.addCorrelationRule("admin", createRuleCreateRequest("", "This is a rule for testing.",
- "Mocked contents.", 0));
- }
-
- @Test
- public void addCorrelationRule_content_is_empty() throws Exception {
- thrown.expect(CorrelationException.class);
- thrown.expectMessage("The contents of the rule can not be empty!");
-
- ruleMgtWrapper.addCorrelationRule("admin", createRuleCreateRequest("test", "This is a rule for testing.",
- "", 0));
- }
-
- @Test
- public void addCorrelationRule_enabled_is_off_limit() throws Exception {
- thrown.expect(CorrelationException.class);
- thrown.expectMessage("Invalid rule status. Only 0 (disabled) and 1 (enabled) are allowed.");
-
- ruleMgtWrapper.addCorrelationRule("admin", createRuleCreateRequest("test", "This is a rule for testing.",
- "Mocked contents.", 3));
- }
-
- @Test
- public void addCorrelationRule_duplicated_rule() throws Exception {
-
- final String ruleName = "Rule-001";
-
- RuleCreateRequest ruleCreateRequest = createRuleCreateRequest(ruleName, "This is a rule for testing.",
- "Mocked contents.", 0);
- CorrelationRule correlationRule = convertCreateRequest2CorrelationRule(ruleCreateRequest);
-
- thrown.expect(CorrelationException.class);
- thrown.expectMessage("A rule with the same name already exists.");
-
- EasyMock.expect(correlationRuleDaoMock.queryRuleByRuleName(ruleName)).andReturn(correlationRule);
- PowerMock.replayAll();
-
- ruleMgtWrapper.addCorrelationRule("admin", ruleCreateRequest);
-
- PowerMock.verifyAll();
- }
-
- @Test
- public void addCorrelationRule_normal() throws Exception {
- final String ruleName = "Rule-001";
-
- RuleCreateRequest ruleCreateRequest = createRuleCreateRequest(ruleName, "This is a rule for testing.",
- "Mocked contents.", 1);
- ruleCreateRequest.setLoopControlName("loopName");
-
- CorrelationRule correlationRuleRet = new CorrelationRule();
- correlationRuleRet.setRid("rule_" + System.currentTimeMillis());
-
- EasyMock.expect(correlationRuleDaoMock.queryRuleByRuleName(ruleName)).andReturn(null);
- EasyMock.expect(engineToolsMock.getEngineWithLeastRules()).andReturn("127.0.0.1");
- EasyMock.expect(engineWrapperMock.checkRuleFromEngine(EasyMock.anyObject(CorrelationCheckRule4Engine.class)
- , EasyMock.anyObject(String.class)))
- .andReturn(true);
- EasyMock.expect(engineWrapperMock.deployEngine(EasyMock.anyObject(CorrelationDeployRule4Engine.class)
- , EasyMock.anyObject(String.class)))
- .andReturn("package-001");
- EasyMock.expect(correlationRuleDaoMock.saveRule(EasyMock.anyObject(CorrelationRule.class)))
- .andReturn(correlationRuleRet);
-
- PowerMock.replayAll();
-
- RuleAddAndUpdateResponse response = ruleMgtWrapper.addCorrelationRule("admin", ruleCreateRequest);
- PowerMock.verifyAll();
-
- assertThat(response.getRuleId(), equalTo(correlationRuleRet.getRid()));
- }
-
- @Test
- public void updateCorrelationRule_param_null() throws Exception {
- thrown.expect(CorrelationException.class);
- thrown.expectMessage("The request object can not be empty!");
-
- ruleMgtWrapper.updateCorrelationRule(USER_NAME, null);
- }
-
- @Test
- public void updateCorrelationRule_normal() throws Exception {
- CorrelationRule oldCorrelationRule = new CorrelationRule();
- oldCorrelationRule.setRid("rule_1");
- oldCorrelationRule.setName("name");
- oldCorrelationRule.setDescription("des1");
- oldCorrelationRule.setContent("content");
- oldCorrelationRule.setPackageName("testName");
- oldCorrelationRule.setEnabled(1);
- oldCorrelationRule.setClosedControlLoopName("cl-name");
- oldCorrelationRule.setEngineInstance("127.0.0.1");
- RuleUpdateRequest ruleUpdateRequest = createRuleUpdateRequest("rule_1", "cl-name", "des2", "contetnt2", 1);
-
- EasyMock.expect(correlationRuleDaoMock.queryRuleByRid("rule_1")).andReturn(oldCorrelationRule);
- EasyMock.expect(engineWrapperMock.deleteRuleFromEngine("testName", "127.0.0.1")).andReturn(true);
- correlationRuleDaoMock.updateRule(EasyMock.anyObject(CorrelationRule.class));
- EasyMock.expectLastCall();
- EasyMock.expect(engineWrapperMock.checkRuleFromEngine(EasyMock.anyObject(CorrelationCheckRule4Engine.class)
- , EasyMock.anyObject(String.class)))
- .andReturn(true);
- EasyMock.expect(engineWrapperMock.deployEngine(EasyMock.anyObject(CorrelationDeployRule4Engine.class)
- , EasyMock.anyObject(String.class)))
- .andReturn("packageName1");
- PowerMock.replayAll();
-
- ruleMgtWrapper.updateCorrelationRule(USER_NAME, ruleUpdateRequest);
-
- PowerMock.verifyAll();
-
- assertThat(oldCorrelationRule.getRid(), equalTo(ruleUpdateRequest.getRuleId()));
- }
-
- @Test
- public void updateCorrelationRule_param_no_change() throws Exception {
- CorrelationRule oldCorrelationRule = new CorrelationRule();
- oldCorrelationRule.setRid("rule_1");
- oldCorrelationRule.setName("name");
- oldCorrelationRule.setDescription("des1");
- oldCorrelationRule.setContent("content");
- oldCorrelationRule.setPackageName("testName");
- oldCorrelationRule.setEnabled(1);
- oldCorrelationRule.setClosedControlLoopName("cl-name");
- RuleUpdateRequest ruleUpdateRequest = createRuleUpdateRequest("rule_1", "cl-name", "des1", "content", 1);
-
- EasyMock.expect(correlationRuleDaoMock.queryRuleByRid("rule_1")).andReturn(oldCorrelationRule);
-
- PowerMock.replayAll();
-
- ruleMgtWrapper.updateCorrelationRule(USER_NAME, ruleUpdateRequest);
-
- PowerMock.verifyAll();
-
- assertThat(oldCorrelationRule.getRid(), equalTo(ruleUpdateRequest.getRuleId()));
- }
-
- @Test
- public void updateCorrelationRule_rule_not_exist() throws Exception {
- thrown.expect(CorrelationException.class);
- thrown.expectMessage("You're trying to update a rule which does not exist in the system.");
-
- EasyMock.expect(correlationRuleDaoMock.queryRuleByRid(EasyMock.anyObject(String.class))).andReturn(null);
-
- PowerMock.replayAll();
-
- ruleMgtWrapper.updateCorrelationRule(USER_NAME, new RuleUpdateRequest());
-
- PowerMock.verifyAll();
- }
-
- @Test
- public void deleteCorrelationRule_request_null() throws Exception {
- thrown.expect(CorrelationException.class);
- thrown.expectMessage("The request object can not be empty!");
-
- ruleMgtWrapper.deleteCorrelationRule(null);
- }
-
- @Test
- public void deleteCorrelationRule_rule_not_exit() throws Exception {
- thrown.expect(CorrelationException.class);
- thrown.expectMessage("You're trying to delete a rule which does not exist in the system.");
-
- RuleDeleteRequest ruleDeleteRequest = createRuleDeleteRequest("rule_" + System.currentTimeMillis());
-
- EasyMock.expect(dbDaoUtilMock.getJdbiDaoByOnDemand(CorrelationRuleDao.class)).andReturn(
- correlationRuleDaoMock).anyTimes();
- EasyMock.expect(correlationRuleDaoMock.queryRuleByRid(ruleDeleteRequest.getRuleId()))
- .andReturn(null);
-
- PowerMock.replayAll();
-
- ruleMgtWrapper.deleteCorrelationRule(ruleDeleteRequest);
-
- PowerMock.verifyAll();
- }
-
- @Test
- public void deleteCorrelationRule_normal() throws Exception {
- RuleDeleteRequest ruleDeleteRequest = createRuleDeleteRequest("rule_" + System.currentTimeMillis());
- CorrelationRule correlationRule = new CorrelationRule();
- correlationRule.setEnabled(1);
- EasyMock.expect(correlationRuleDaoMock.queryRuleByRid(ruleDeleteRequest.getRuleId()))
- .andReturn(correlationRule);
- EasyMock.expect(engineWrapperMock.deleteRuleFromEngine(EasyMock.anyObject(String.class)
- , EasyMock.anyObject(String.class))).andReturn(true);
- correlationRuleDaoMock.deleteRule(EasyMock.anyObject(CorrelationRule.class));
- EasyMock.expectLastCall();
- PowerMock.replayAll();
-
- ruleMgtWrapper.deleteCorrelationRule(ruleDeleteRequest);
-
- PowerMock.verifyAll();
- }
-
- @Test
- public void getCorrelationRuleByCondition_data_format_exception() throws Exception {
- thrown.expect(CorrelationException.class);
- thrown.expectMessage("An error occurred while building the query SQL.");
-
- EasyMock.expect(correlationRuleQueryDaoMock.getCorrelationRulesByCondition(EasyMock.anyObject(
- RuleQueryCondition.class)))
- .andThrow(new CorrelationException("An error occurred while building the query SQL."));
-
- PowerMock.replay(correlationRuleQueryDaoMock, CorrelationRuleQueryDao.class);
-
- ruleMgtWrapper.getCorrelationRuleByCondition(new RuleQueryCondition());
-
- PowerMock.verify(correlationRuleQueryDaoMock, CorrelationRuleQueryDao.class);
- }
-
- @Test
- public void getCorrelationRuleByCondition_db_exception() throws Exception {
- thrown.expect(CorrelationException.class);
- thrown.expectMessage("Failed to query the rule.");
-
- EasyMock.expect(correlationRuleQueryDaoMock.getCorrelationRulesByCondition(EasyMock.anyObject(
- RuleQueryCondition.class)))
- .andThrow(new CorrelationException("Failed to query the rule."));
-
- PowerMock.replay(correlationRuleQueryDaoMock, CorrelationRuleQueryDao.class);
-
- ruleMgtWrapper.getCorrelationRuleByCondition(new RuleQueryCondition());
-
- PowerMock.verify(correlationRuleQueryDaoMock, CorrelationRuleQueryDao.class);
- }
-
- @Test
- public void getCorrelationRuleByCondition_normal() throws Exception {
- List<CorrelationRule> correlationRuleList = new ArrayList<CorrelationRule>(10);
- for (int i = 0; i < 10; ++i) {
- CorrelationRule correlationRule = new CorrelationRule();
- correlationRule.setContent("content" + i);
- correlationRule.setName("name" + i);
- correlationRule.setRid("rule_" + i);
- correlationRule.setEngineType("engineType" + (i % 2 + 1));
- correlationRule.setEngineID("engineId" + i);
- correlationRule.setCreateTime(new Date());
- correlationRule.setCreator(USER_NAME);
- correlationRule.setDescription("description" + i);
- correlationRule.setPackageName("package" + i);
- correlationRuleList.add(correlationRule);
- }
-
- EasyMock.expect(correlationRuleQueryDaoMock.getCorrelationRulesByCondition(EasyMock.anyObject(
- RuleQueryCondition.class))).andReturn(correlationRuleList);
-
- PowerMock.replay(correlationRuleQueryDaoMock, CorrelationRuleQueryDao.class);
-
- RuleQueryListResponse response = ruleMgtWrapper.getCorrelationRuleByCondition(new RuleQueryCondition());
-
- PowerMock.verify(correlationRuleQueryDaoMock, CorrelationRuleQueryDao.class);
-
- assertThat(response.getTotalCount(), is(10));
-
- for (int i = 0; i < 10; ++i) {
- assertThat(response.getCorrelationRules().get(i).getRuleId(),
- equalTo(correlationRuleList.get(i).getRid()));
- }
- }
-
- private RuleCreateRequest createRuleCreateRequest(String ruleName, String description, String content,
- int enabled) {
- RuleCreateRequest rcr;
- rcr = new RuleCreateRequest();
- rcr.setRuleName(ruleName);
- rcr.setDescription(description);
- rcr.setContent(content);
- rcr.setEnabled(enabled);
- return rcr;
- }
-
- private RuleUpdateRequest createRuleUpdateRequest(String ruleId, String clName, String description,
- String content, int enabled) {
- RuleUpdateRequest ruleUpdateRequest = new RuleUpdateRequest();
- ruleUpdateRequest.setRuleId(ruleId);
- ruleUpdateRequest.setDescription(description);
- ruleUpdateRequest.setContent(content);
- ruleUpdateRequest.setEnabled(enabled);
- ruleUpdateRequest.setLoopControlName(clName);
- return ruleUpdateRequest;
- }
-
- private RuleDeleteRequest createRuleDeleteRequest(String ruleId) {
- RuleDeleteRequest ruleDeleteRequest = new RuleDeleteRequest();
- ruleDeleteRequest.setRuleId(ruleId);
- return ruleDeleteRequest;
- }
-
- private CorrelationRule convertCreateRequest2CorrelationRule(RuleCreateRequest ruleCreateRequest) {
- CorrelationRule correlationRule = new CorrelationRule();
- correlationRule.setContent(ruleCreateRequest.getContent());
- correlationRule.setDescription(ruleCreateRequest.getDescription());
- correlationRule.setName(ruleCreateRequest.getRuleName());
- correlationRule.setCreator(USER_NAME);
- correlationRule.setModifier(USER_NAME);
- correlationRule.setEnabled(ruleCreateRequest.getEnabled());
- return correlationRule;
- }
-
- private CorrelationRule convertUpdateRequest2CorrelationRule(RuleUpdateRequest ruleUpdateRequest) {
- 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(USER_NAME);
- return correlationRule;
- }
+/** + * 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.wrapper; + + +import org.easymock.EasyMock; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.junit.runner.RunWith; +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.*; +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.CorrelationRuleDao; +import org.onap.holmes.rulemgt.db.CorrelationRuleQueryDao; +import org.onap.holmes.rulemgt.tools.EngineTools; +import org.powermock.api.easymock.PowerMock; +import org.powermock.modules.junit4.PowerMockRunner; +import org.powermock.reflect.Whitebox; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Date; +import java.util.List; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.is; + +@RunWith(PowerMockRunner.class) +public class RuleMgtWrapperTest { + + @Rule + public ExpectedException thrown = ExpectedException.none(); + + private RuleMgtWrapper ruleMgtWrapper; + + private EngineWrapper engineWrapperMock; + + private DbDaoUtil dbDaoUtilMock; + + private CorrelationRuleQueryDao correlationRuleQueryDaoMock; + + private CorrelationRuleDao correlationRuleDaoMock; + + private EngineTools engineToolsMock; + + private static final String USER_NAME = "admin"; + + @Before + public void setUp() throws Exception { + + ruleMgtWrapper = new RuleMgtWrapper(); + + engineWrapperMock = PowerMock.createMock(EngineWrapper.class); + correlationRuleQueryDaoMock = PowerMock.createMock(CorrelationRuleQueryDao.class); + dbDaoUtilMock = PowerMock.createMock(DbDaoUtil.class); + correlationRuleDaoMock = PowerMock.createMock(CorrelationRuleDao.class); + engineToolsMock = PowerMock.createMock(EngineTools.class); + + Whitebox.setInternalState(ruleMgtWrapper, "daoUtil", dbDaoUtilMock); + Whitebox.setInternalState(ruleMgtWrapper, "correlationRuleQueryDao", correlationRuleQueryDaoMock); + Whitebox.setInternalState(ruleMgtWrapper, "engineWarpper", engineWrapperMock); + Whitebox.setInternalState(ruleMgtWrapper, "correlationRuleDao", correlationRuleDaoMock); + Whitebox.setInternalState(ruleMgtWrapper,"engineTools", engineToolsMock); + + PowerMock.resetAll(); + } + + @Test + public void initDaoUtil_normal() { + ruleMgtWrapper.initDaoUtil(); + } + + @Test + public void addCorrelationRule_name_is_null() throws Exception { + thrown.expect(CorrelationException.class); + thrown.expectMessage("The name of the rule can not be empty."); + + ruleMgtWrapper.addCorrelationRule(USER_NAME, createRuleCreateRequest(null, "This is a rule for testing.", + "Mocked contents.", 0)); + } + + @Test + public void addCorrelationRule_request_null() throws Exception { + thrown.expect(CorrelationException.class); + thrown.expectMessage("The request object can not be empty!"); + + ruleMgtWrapper.addCorrelationRule(USER_NAME, null); + } + + @Test + public void addCorrelationRule_name_is_empty() throws Exception { + thrown.expect(CorrelationException.class); + thrown.expectMessage("The name of the rule can not be empty."); + + ruleMgtWrapper.addCorrelationRule("admin", createRuleCreateRequest("", "This is a rule for testing.", + "Mocked contents.", 0)); + } + + @Test + public void addCorrelationRule_content_is_empty() throws Exception { + thrown.expect(CorrelationException.class); + thrown.expectMessage("The contents of the rule can not be empty!"); + + ruleMgtWrapper.addCorrelationRule("admin", createRuleCreateRequest("test", "This is a rule for testing.", + "", 0)); + } + + @Test + public void addCorrelationRule_enabled_is_off_limit() throws Exception { + thrown.expect(CorrelationException.class); + thrown.expectMessage("Invalid rule status. Only 0 (disabled) and 1 (enabled) are allowed."); + + ruleMgtWrapper.addCorrelationRule("admin", createRuleCreateRequest("test", "This is a rule for testing.", + "Mocked contents.", 3)); + } + + @Test + public void addCorrelationRule_duplicated_rule() throws Exception { + + final String ruleName = "Rule-001"; + + RuleCreateRequest ruleCreateRequest = createRuleCreateRequest(ruleName, "This is a rule for testing.", + "Mocked contents.", 0); + CorrelationRule correlationRule = convertCreateRequest2CorrelationRule(ruleCreateRequest); + + thrown.expect(CorrelationException.class); + thrown.expectMessage("A rule with the same name already exists."); + + EasyMock.expect(correlationRuleDaoMock.queryRuleByRuleName(ruleName)).andReturn(correlationRule); + PowerMock.replayAll(); + + ruleMgtWrapper.addCorrelationRule("admin", ruleCreateRequest); + + PowerMock.verifyAll(); + } + + @Test + public void addCorrelationRule_normal() throws Exception { + final String ruleName = "Rule-001"; + + RuleCreateRequest ruleCreateRequest = createRuleCreateRequest(ruleName, "This is a rule for testing.", + "Mocked contents.", 1); + ruleCreateRequest.setLoopControlName("loopName"); + + CorrelationRule correlationRuleRet = new CorrelationRule(); + correlationRuleRet.setRid("rule_" + System.currentTimeMillis()); + + EasyMock.expect(correlationRuleDaoMock.queryRuleByRuleName(ruleName)).andReturn(null); + EasyMock.expect(engineToolsMock.getEngineWithLeastRules()).andReturn("127.0.0.1"); + EasyMock.expect(engineWrapperMock.checkRuleFromEngine(EasyMock.anyObject(CorrelationCheckRule4Engine.class) + , EasyMock.anyObject(String.class))) + .andReturn(true); + EasyMock.expect(engineWrapperMock.deployEngine(EasyMock.anyObject(CorrelationDeployRule4Engine.class) + , EasyMock.anyObject(String.class))) + .andReturn("package-001"); + EasyMock.expect(correlationRuleDaoMock.saveRule(EasyMock.anyObject(CorrelationRule.class))) + .andReturn(correlationRuleRet); + + PowerMock.replayAll(); + + RuleAddAndUpdateResponse response = ruleMgtWrapper.addCorrelationRule("admin", ruleCreateRequest); + PowerMock.verifyAll(); + + assertThat(response.getRuleId(), equalTo(correlationRuleRet.getRid())); + } + + @Test + public void updateCorrelationRule_param_null() throws Exception { + thrown.expect(CorrelationException.class); + thrown.expectMessage("The request object can not be empty!"); + + ruleMgtWrapper.updateCorrelationRule(USER_NAME, null); + } + + @Test + public void updateCorrelationRule_normal() throws Exception { + CorrelationRule oldCorrelationRule = new CorrelationRule(); + oldCorrelationRule.setRid("rule_1"); + oldCorrelationRule.setName("name"); + oldCorrelationRule.setDescription("des1"); + oldCorrelationRule.setContent("content"); + oldCorrelationRule.setPackageName("testName"); + oldCorrelationRule.setEnabled(1); + oldCorrelationRule.setClosedControlLoopName("cl-name"); + oldCorrelationRule.setEngineInstance("127.0.0.1"); + RuleUpdateRequest ruleUpdateRequest = createRuleUpdateRequest("rule_1", "cl-name", "des2", "contetnt2", 1); + + EasyMock.expect(correlationRuleDaoMock.queryRuleByRid("rule_1")).andReturn(oldCorrelationRule); + EasyMock.expect(engineToolsMock.getInstanceList()).andReturn(Arrays.asList("127.0.0.1", "127.0.0.2")).times(2); + EasyMock.expect(engineWrapperMock.deleteRuleFromEngine("testName", "127.0.0.1")).andReturn(true); + correlationRuleDaoMock.updateRule(EasyMock.anyObject(CorrelationRule.class)); + EasyMock.expectLastCall(); + EasyMock.expect(engineWrapperMock.checkRuleFromEngine(EasyMock.anyObject(CorrelationCheckRule4Engine.class) + , EasyMock.anyObject(String.class))) + .andReturn(true); + EasyMock.expect(engineWrapperMock.deployEngine(EasyMock.anyObject(CorrelationDeployRule4Engine.class) + , EasyMock.anyObject(String.class))) + .andReturn("packageName1"); + PowerMock.replayAll(); + + ruleMgtWrapper.updateCorrelationRule(USER_NAME, ruleUpdateRequest); + + PowerMock.verifyAll(); + + assertThat(oldCorrelationRule.getRid(), equalTo(ruleUpdateRequest.getRuleId())); + } + + @Test + public void updateCorrelationRule_param_no_change() throws Exception { + CorrelationRule oldCorrelationRule = new CorrelationRule(); + oldCorrelationRule.setRid("rule_1"); + oldCorrelationRule.setName("name"); + oldCorrelationRule.setDescription("des1"); + oldCorrelationRule.setContent("content"); + oldCorrelationRule.setPackageName("testName"); + oldCorrelationRule.setEnabled(1); + oldCorrelationRule.setClosedControlLoopName("cl-name"); + oldCorrelationRule.setEngineInstance("127.0.0.1"); + RuleUpdateRequest ruleUpdateRequest = createRuleUpdateRequest("rule_1", "cl-name", "des1", "content", 1); + + EasyMock.expect(correlationRuleDaoMock.queryRuleByRid("rule_1")).andReturn(oldCorrelationRule); + EasyMock.expect(engineToolsMock.getInstanceList()).andReturn(Arrays.asList("127.0.0.1", "127.0.0.2")); + + PowerMock.replayAll(); + + ruleMgtWrapper.updateCorrelationRule(USER_NAME, ruleUpdateRequest); + + PowerMock.verifyAll(); + + assertThat(oldCorrelationRule.getRid(), equalTo(ruleUpdateRequest.getRuleId())); + } + + @Test + public void updateCorrelationRule_rule_not_exist() throws Exception { + thrown.expect(CorrelationException.class); + thrown.expectMessage("You're trying to update a rule which does not exist in the system."); + + EasyMock.expect(correlationRuleDaoMock.queryRuleByRid(EasyMock.anyObject(String.class))).andReturn(null); + + PowerMock.replayAll(); + + ruleMgtWrapper.updateCorrelationRule(USER_NAME, new RuleUpdateRequest()); + + PowerMock.verifyAll(); + } + + @Test + public void deleteCorrelationRule_request_null() throws Exception { + thrown.expect(CorrelationException.class); + thrown.expectMessage("The request object can not be empty!"); + + ruleMgtWrapper.deleteCorrelationRule(null); + } + + @Test + public void deleteCorrelationRule_rule_not_exit() throws Exception { + thrown.expect(CorrelationException.class); + thrown.expectMessage("You're trying to delete a rule which does not exist in the system."); + + RuleDeleteRequest ruleDeleteRequest = createRuleDeleteRequest("rule_" + System.currentTimeMillis()); + + EasyMock.expect(dbDaoUtilMock.getJdbiDaoByOnDemand(CorrelationRuleDao.class)).andReturn( + correlationRuleDaoMock).anyTimes(); + EasyMock.expect(correlationRuleDaoMock.queryRuleByRid(ruleDeleteRequest.getRuleId())) + .andReturn(null); + + PowerMock.replayAll(); + + ruleMgtWrapper.deleteCorrelationRule(ruleDeleteRequest); + + PowerMock.verifyAll(); + } + + @Test + public void deleteCorrelationRule_normal() throws Exception { + RuleDeleteRequest ruleDeleteRequest = createRuleDeleteRequest("rule_" + System.currentTimeMillis()); + CorrelationRule correlationRule = new CorrelationRule(); + correlationRule.setEnabled(1); + EasyMock.expect(correlationRuleDaoMock.queryRuleByRid(ruleDeleteRequest.getRuleId())) + .andReturn(correlationRule); + EasyMock.expect(engineWrapperMock.deleteRuleFromEngine(EasyMock.anyObject(String.class) + , EasyMock.anyObject(String.class))).andReturn(true); + correlationRuleDaoMock.deleteRule(EasyMock.anyObject(CorrelationRule.class)); + EasyMock.expectLastCall(); + PowerMock.replayAll(); + + ruleMgtWrapper.deleteCorrelationRule(ruleDeleteRequest); + + PowerMock.verifyAll(); + } + + @Test + public void getCorrelationRuleByCondition_data_format_exception() throws Exception { + thrown.expect(CorrelationException.class); + thrown.expectMessage("An error occurred while building the query SQL."); + + EasyMock.expect(correlationRuleQueryDaoMock.getCorrelationRulesByCondition(EasyMock.anyObject( + RuleQueryCondition.class))) + .andThrow(new CorrelationException("An error occurred while building the query SQL.")); + + PowerMock.replay(correlationRuleQueryDaoMock, CorrelationRuleQueryDao.class); + + ruleMgtWrapper.getCorrelationRuleByCondition(new RuleQueryCondition()); + + PowerMock.verify(correlationRuleQueryDaoMock, CorrelationRuleQueryDao.class); + } + + @Test + public void getCorrelationRuleByCondition_db_exception() throws Exception { + thrown.expect(CorrelationException.class); + thrown.expectMessage("Failed to query the rule."); + + EasyMock.expect(correlationRuleQueryDaoMock.getCorrelationRulesByCondition(EasyMock.anyObject( + RuleQueryCondition.class))) + .andThrow(new CorrelationException("Failed to query the rule.")); + + PowerMock.replay(correlationRuleQueryDaoMock, CorrelationRuleQueryDao.class); + + ruleMgtWrapper.getCorrelationRuleByCondition(new RuleQueryCondition()); + + PowerMock.verify(correlationRuleQueryDaoMock, CorrelationRuleQueryDao.class); + } + + @Test + public void getCorrelationRuleByCondition_normal() throws Exception { + List<CorrelationRule> correlationRuleList = new ArrayList<CorrelationRule>(10); + for (int i = 0; i < 10; ++i) { + CorrelationRule correlationRule = new CorrelationRule(); + correlationRule.setContent("content" + i); + correlationRule.setName("name" + i); + correlationRule.setRid("rule_" + i); + correlationRule.setEngineType("engineType" + (i % 2 + 1)); + correlationRule.setEngineID("engineId" + i); + correlationRule.setCreateTime(new Date()); + correlationRule.setCreator(USER_NAME); + correlationRule.setDescription("description" + i); + correlationRule.setPackageName("package" + i); + correlationRuleList.add(correlationRule); + } + + EasyMock.expect(correlationRuleQueryDaoMock.getCorrelationRulesByCondition(EasyMock.anyObject( + RuleQueryCondition.class))).andReturn(correlationRuleList); + + PowerMock.replay(correlationRuleQueryDaoMock, CorrelationRuleQueryDao.class); + + RuleQueryListResponse response = ruleMgtWrapper.getCorrelationRuleByCondition(new RuleQueryCondition()); + + PowerMock.verify(correlationRuleQueryDaoMock, CorrelationRuleQueryDao.class); + + assertThat(response.getTotalCount(), is(10)); + + for (int i = 0; i < 10; ++i) { + assertThat(response.getCorrelationRules().get(i).getRuleId(), + equalTo(correlationRuleList.get(i).getRid())); + } + } + + private RuleCreateRequest createRuleCreateRequest(String ruleName, String description, String content, + int enabled) { + RuleCreateRequest rcr; + rcr = new RuleCreateRequest(); + rcr.setRuleName(ruleName); + rcr.setDescription(description); + rcr.setContent(content); + rcr.setEnabled(enabled); + return rcr; + } + + private RuleUpdateRequest createRuleUpdateRequest(String ruleId, String clName, String description, + String content, int enabled) { + RuleUpdateRequest ruleUpdateRequest = new RuleUpdateRequest(); + ruleUpdateRequest.setRuleId(ruleId); + ruleUpdateRequest.setDescription(description); + ruleUpdateRequest.setContent(content); + ruleUpdateRequest.setEnabled(enabled); + ruleUpdateRequest.setLoopControlName(clName); + return ruleUpdateRequest; + } + + private RuleDeleteRequest createRuleDeleteRequest(String ruleId) { + RuleDeleteRequest ruleDeleteRequest = new RuleDeleteRequest(); + ruleDeleteRequest.setRuleId(ruleId); + return ruleDeleteRequest; + } + + private CorrelationRule convertCreateRequest2CorrelationRule(RuleCreateRequest ruleCreateRequest) { + CorrelationRule correlationRule = new CorrelationRule(); + correlationRule.setContent(ruleCreateRequest.getContent()); + correlationRule.setDescription(ruleCreateRequest.getDescription()); + correlationRule.setName(ruleCreateRequest.getRuleName()); + correlationRule.setCreator(USER_NAME); + correlationRule.setModifier(USER_NAME); + correlationRule.setEnabled(ruleCreateRequest.getEnabled()); + return correlationRule; + } + + private CorrelationRule convertUpdateRequest2CorrelationRule(RuleUpdateRequest ruleUpdateRequest) { + 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(USER_NAME); + return correlationRule; + } }
\ No newline at end of file |