From 580d4ce637b1c09b3bd2258b0b9c8332b8789bad Mon Sep 17 00:00:00 2001 From: Congcong Peng Date: Wed, 28 Mar 2018 09:42:09 +0800 Subject: Allocate Rule Issue-ID: HOLMES-106 Change-Id: I50937669cf26ead5abb8d2ef9bd20bc61095ac1b Signed-off-by: Congcong Peng --- .../rulemgt/bolt/enginebolt/EngineService.java | 15 +- .../rulemgt/bolt/enginebolt/EngineWrapper.java | 12 +- .../onap/holmes/rulemgt/db/CorrelationRuleDao.java | 19 +- .../holmes/rulemgt/db/CorrelationRuleQueryDao.java | 1 + .../org/onap/holmes/rulemgt/msb/EngineIpList.java | 67 ++++++ .../java/org/onap/holmes/rulemgt/msb/MsbQuery.java | 79 +++++++ .../onap/holmes/rulemgt/send/Ip4AddingRule.java | 66 ++++++ .../onap/holmes/rulemgt/send/RuleAllocation.java | 228 +++++++++++++++++++++ .../holmes/rulemgt/wrapper/RuleMgtWrapper.java | 37 +++- .../holmes/rulemgt/wrapper/RuleQueryWrapper.java | 52 +++++ .../rulemgt/bolt/enginebolt/EngineWrapperTest.java | 45 ++-- .../holmes/rulemgt/send/RuleAllocationTest.java | 63 ++++++ .../holmes/rulemgt/wrapper/RuleMgtWrapperTest.java | 25 ++- .../rulemgt/wrapper/RuleQueryWrapperTest.java | 61 ++++++ 14 files changed, 722 insertions(+), 48 deletions(-) create mode 100644 rulemgt/src/main/java/org/onap/holmes/rulemgt/msb/EngineIpList.java create mode 100644 rulemgt/src/main/java/org/onap/holmes/rulemgt/msb/MsbQuery.java create mode 100644 rulemgt/src/main/java/org/onap/holmes/rulemgt/send/Ip4AddingRule.java create mode 100644 rulemgt/src/main/java/org/onap/holmes/rulemgt/send/RuleAllocation.java create mode 100644 rulemgt/src/main/java/org/onap/holmes/rulemgt/wrapper/RuleQueryWrapper.java create mode 100644 rulemgt/src/test/java/org/onap/holmes/rulemgt/send/RuleAllocationTest.java create mode 100644 rulemgt/src/test/java/org/onap/holmes/rulemgt/wrapper/RuleQueryWrapperTest.java (limited to 'rulemgt/src') diff --git a/rulemgt/src/main/java/org/onap/holmes/rulemgt/bolt/enginebolt/EngineService.java b/rulemgt/src/main/java/org/onap/holmes/rulemgt/bolt/enginebolt/EngineService.java index 319347e..13507d6 100644 --- a/rulemgt/src/main/java/org/onap/holmes/rulemgt/bolt/enginebolt/EngineService.java +++ b/rulemgt/src/main/java/org/onap/holmes/rulemgt/bolt/enginebolt/EngineService.java @@ -32,24 +32,27 @@ import org.onap.holmes.common.config.MicroServiceConfig; @Service public class EngineService { - protected HttpResponse delete(String packageName) throws Exception { + private static final String PREFIX = "https://"; + private static final String PORT = ":9102"; + + protected HttpResponse delete(String packageName, String ip) throws Exception { HashMap headers = createHeaders(); - String url = MicroServiceConfig.getMsbServerAddrWithHttpPrefix() + RuleMgtConstant.ENGINE_PATH + "/" + packageName; + String url = PREFIX + ip + PORT + RuleMgtConstant.ENGINE_PATH + "/" + packageName; return HttpsUtils.delete(url, headers); } - protected HttpResponse check(CorrelationCheckRule4Engine correlationCheckRule4Engine) + protected HttpResponse check(CorrelationCheckRule4Engine correlationCheckRule4Engine, String ip) throws Exception { String content = GsonUtil.beanToJson(correlationCheckRule4Engine); HashMap headers = createHeaders(); - String url = MicroServiceConfig.getMsbServerAddrWithHttpPrefix() + RuleMgtConstant.ENGINE_PATH; + String url = PREFIX + ip + PORT + RuleMgtConstant.ENGINE_PATH; return HttpsUtils.post(url, headers, new HashMap<>(), new StringEntity(content)); } - protected HttpResponse deploy(CorrelationDeployRule4Engine correlationDeployRule4Engine) throws Exception { + protected HttpResponse deploy(CorrelationDeployRule4Engine correlationDeployRule4Engine, String ip) throws Exception { String content = GsonUtil.beanToJson(correlationDeployRule4Engine); HashMap headers = createHeaders(); - String url = MicroServiceConfig.getMsbServerAddrWithHttpPrefix() + RuleMgtConstant.ENGINE_PATH; + String url = PREFIX + ip + PORT + RuleMgtConstant.ENGINE_PATH; return HttpsUtils.put(url, headers, new HashMap<>(), new StringEntity(content)); } diff --git a/rulemgt/src/main/java/org/onap/holmes/rulemgt/bolt/enginebolt/EngineWrapper.java b/rulemgt/src/main/java/org/onap/holmes/rulemgt/bolt/enginebolt/EngineWrapper.java index 18a326e..b0bd1f5 100644 --- a/rulemgt/src/main/java/org/onap/holmes/rulemgt/bolt/enginebolt/EngineWrapper.java +++ b/rulemgt/src/main/java/org/onap/holmes/rulemgt/bolt/enginebolt/EngineWrapper.java @@ -34,10 +34,10 @@ public class EngineWrapper { @Inject private EngineService engineService; - public String deployEngine(CorrelationDeployRule4Engine correlationRule) throws CorrelationException { + public String deployEngine(CorrelationDeployRule4Engine correlationRule,String ip) throws CorrelationException { HttpResponse response; try { - response = engineService.deploy(correlationRule); + response = engineService.deploy(correlationRule, ip); } catch (Exception e) { throw new CorrelationException("Failed to call the rule deployment RESTful API.", e); } @@ -55,10 +55,10 @@ public class EngineWrapper { } } - public boolean deleteRuleFromEngine(String packageName) throws CorrelationException { + public boolean deleteRuleFromEngine(String packageName,String ip) throws CorrelationException { HttpResponse response; try { - response = engineService.delete(packageName); + response = engineService.delete(packageName, ip); } catch (Exception e) { throw new CorrelationException("Failed to call the rule deleting RESTful API.", e); } @@ -70,12 +70,12 @@ public class EngineWrapper { } } - public boolean checkRuleFromEngine(CorrelationCheckRule4Engine correlationCheckRule4Engine) + public boolean checkRuleFromEngine(CorrelationCheckRule4Engine correlationCheckRule4Engine,String ip) throws CorrelationException { log.info("Rule Contents: " + correlationCheckRule4Engine.getContent()); HttpResponse response; try { - response = engineService.check(correlationCheckRule4Engine); + response = engineService.check(correlationCheckRule4Engine, ip); } catch (Exception e) { throw new CorrelationException("Failed to call the rule verification RESTful API.", e); } diff --git a/rulemgt/src/main/java/org/onap/holmes/rulemgt/db/CorrelationRuleDao.java b/rulemgt/src/main/java/org/onap/holmes/rulemgt/db/CorrelationRuleDao.java index a94c1db..64dcea2 100644 --- a/rulemgt/src/main/java/org/onap/holmes/rulemgt/db/CorrelationRuleDao.java +++ b/rulemgt/src/main/java/org/onap/holmes/rulemgt/db/CorrelationRuleDao.java @@ -30,10 +30,10 @@ import org.skife.jdbi.v2.sqlobject.customizers.RegisterMapper; public abstract class CorrelationRuleDao { @GetGeneratedKeys - @SqlUpdate("INSERT INTO APLUS_RULE (NAME,CTRLLOOP,DESCRIPTION,ENABLE,TEMPLATEID,ENGINETYPE,CREATOR,UPDATOR,PARAMS,CONTENT ,VENDOR,CREATETIME,UPDATETIME,ENGINEID,PACKAGE,RID) VALUES (:name,:closedControlLoopName,:description,:enabled,:templateID,:engineType,:creator,:modifier,:params,:content,:vendor,:createTime,:updateTime,:engineID,:packageName,:rid)") + @SqlUpdate("INSERT INTO APLUS_RULE (NAME,CTRLLOOP,DESCRIPTION,ENABLE,TEMPLATEID,ENGINETYPE,CREATOR,UPDATOR,PARAMS,CONTENT ,VENDOR,CREATETIME,UPDATETIME,ENGINEID,PACKAGE,RID, ENGINEINSTANCE) VALUES (:name,:closedControlLoopName,:description,:enabled,:templateID,:engineType,:creator,:modifier,:params,:content,:vendor,:createTime,:updateTime,:engineID,:packageName,:rid,:engineInstance)") protected abstract String addRule(@BindBean CorrelationRule correlationRule); - @SqlUpdate("UPDATE APLUS_RULE SET CTRLLOOP=:closedControlLoopName,DESCRIPTION=:description,ENABLE=:enabled,CONTENT=:content,UPDATOR=:modifier,UPDATETIME=:updateTime, PACKAGE=:packageName WHERE RID=:rid") + @SqlUpdate("UPDATE APLUS_RULE SET CTRLLOOP=:closedControlLoopName,DESCRIPTION=:description,ENABLE=:enabled,CONTENT=:content,UPDATOR=:modifier,UPDATETIME=:updateTime, PACKAGE=:packageName, ENGINEINSTANCE=:engineInstance WHERE RID=:rid") protected abstract int updateRuleByRid(@BindBean CorrelationRule correlationRule); @SqlUpdate("DELETE FROM APLUS_RULE WHERE RID=:rid") @@ -51,6 +51,21 @@ public abstract class CorrelationRuleDao { @SqlQuery("SELECT * FROM APLUS_RULE WHERE NAME=:name") protected abstract CorrelationRule queryRuleByName(@Bind("name") String name); + @SqlQuery("SELECT * FROM APLUS_RULE WHERE enable=:enable") + public abstract List queryRuleByEnable(@Bind("enable") int enable); + + @SqlQuery("SELECT * FROM APLUS_RULE WHERE engineinstance=:engineinstance") + public abstract List queryRuleByEngineInstance(@Bind("engineinstance") String engineinstance); + + public List queryRuleByRuleEngineInstance(String enginetype) { + return queryRuleByEngineInstance(enginetype); + } + + public List queryRuleByRuleEnable(int enable) { + return queryRuleByEnable(enable); + } + + private void deleteRule2DbInner(CorrelationRule correlationRule) { String name = correlationRule.getName() != null ? correlationRule.getName().trim() : ""; String rid = correlationRule.getRid() != null ? correlationRule.getRid().trim() : ""; diff --git a/rulemgt/src/main/java/org/onap/holmes/rulemgt/db/CorrelationRuleQueryDao.java b/rulemgt/src/main/java/org/onap/holmes/rulemgt/db/CorrelationRuleQueryDao.java index 85980e5..b0f0b96 100644 --- a/rulemgt/src/main/java/org/onap/holmes/rulemgt/db/CorrelationRuleQueryDao.java +++ b/rulemgt/src/main/java/org/onap/holmes/rulemgt/db/CorrelationRuleQueryDao.java @@ -81,6 +81,7 @@ public class CorrelationRuleQueryDao { correlationRule.setVendor((String) value.get("vendor")); correlationRule.setPackageName((String) value.get("package")); correlationRule.setClosedControlLoopName((String) value.get("ctrlloop")); + correlationRule.setEngineInstance((String) value.get("engineInstance")); return correlationRule; } diff --git a/rulemgt/src/main/java/org/onap/holmes/rulemgt/msb/EngineIpList.java b/rulemgt/src/main/java/org/onap/holmes/rulemgt/msb/EngineIpList.java new file mode 100644 index 0000000..cfccd18 --- /dev/null +++ b/rulemgt/src/main/java/org/onap/holmes/rulemgt/msb/EngineIpList.java @@ -0,0 +1,67 @@ +/** + * 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.msb; + + +import org.apache.http.HttpResponse; +import org.jvnet.hk2.annotations.Service; +import org.onap.holmes.common.api.entity.ServiceEntity; +import org.onap.holmes.common.api.entity.ServiceNode4Query; +import org.onap.holmes.common.config.MicroServiceConfig; +import org.onap.holmes.common.utils.GsonUtil; +import org.onap.holmes.common.utils.HttpsUtils; + +import javax.annotation.PostConstruct; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; + +@Service +public class EngineIpList { + + private String[] msbAddrInfo; + private String ip; + private String port; + private String url; + + @PostConstruct + public void init(){ + msbAddrInfo = MicroServiceConfig.getMsbIpAndPort(); + ip = msbAddrInfo[0]; + port = msbAddrInfo[1]; + url = "http://" + ip + ":" + port + "/api/microservices/v1/services/holmes-engine-mgmt/version/v1" ; + } + + public List getServiceCount()throws Exception{ + String response; + try { + HttpResponse httpResponse = HttpsUtils + .get(url, new HashMap<>()); + response = HttpsUtils.extractResponseEntity(httpResponse); + } catch (Exception e) { + throw e; + } + ServiceEntity service = GsonUtil.jsonToBean(response, ServiceEntity.class); + List nodesList = service.getNodes(); + List ipList = new ArrayList<>(); + for(ServiceNode4Query node : nodesList){ + ipList.add(node.getIp()); + } + return ipList; + + } + +} diff --git a/rulemgt/src/main/java/org/onap/holmes/rulemgt/msb/MsbQuery.java b/rulemgt/src/main/java/org/onap/holmes/rulemgt/msb/MsbQuery.java new file mode 100644 index 0000000..ed9b9af --- /dev/null +++ b/rulemgt/src/main/java/org/onap/holmes/rulemgt/msb/MsbQuery.java @@ -0,0 +1,79 @@ +/** + * 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.msb; + +import lombok.extern.slf4j.Slf4j; +import org.jvnet.hk2.annotations.Service; +import org.onap.holmes.rulemgt.send.RuleAllocation; +import org.onap.holmes.rulemgt.send.Ip4AddingRule; +import org.onap.holmes.rulemgt.wrapper.RuleMgtWrapper; + +import javax.annotation.PostConstruct; +import javax.inject.Inject; +import java.util.List; +import java.util.Timer; +import java.util.TimerTask; + + +@Service +@Slf4j +public class MsbQuery { + + @Inject + private RuleAllocation ruleAllocation; + + @Inject + private Ip4AddingRule ip4AddingRule; + + @Inject + private EngineIpList engineIpList; + + @Inject + private RuleMgtWrapper ruleMgtWrapper; + + private List timerIpList; + + @PostConstruct + public void init() { + + try{ + timer(); + }catch(Exception e){ + log.error("MSBQuery init timer task failed !" + e.getMessage()); + } + + } + + public void timer() throws Exception{ + Timer timer = new Timer(); + timer.schedule(new TimerTask() { + + public void run() { + try { + timerIpList = engineIpList.getServiceCount(); + ip4AddingRule.getIpList(timerIpList); + ruleAllocation.judgeAndAllocateRule(timerIpList); + + } catch (Exception e) { + log.error("The timing query engine instance failed " ,e); + } + } + + }, 5000, 30000); + + } + +} diff --git a/rulemgt/src/main/java/org/onap/holmes/rulemgt/send/Ip4AddingRule.java b/rulemgt/src/main/java/org/onap/holmes/rulemgt/send/Ip4AddingRule.java new file mode 100644 index 0000000..f82d3a4 --- /dev/null +++ b/rulemgt/src/main/java/org/onap/holmes/rulemgt/send/Ip4AddingRule.java @@ -0,0 +1,66 @@ +/** + * 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.send; + +import lombok.extern.slf4j.Slf4j; +import org.jvnet.hk2.annotations.Service; +import org.onap.holmes.common.api.entity.CorrelationRule; +import org.onap.holmes.rulemgt.wrapper.RuleQueryWrapper; + +import javax.inject.Inject; +import java.util.*; + +@Service +@Slf4j +public class Ip4AddingRule { + + @Inject + private RuleQueryWrapper ruleQueryWrapper; + private List engineService; + + public void getIpList(List ipList){ + engineService = ipList; + } + + public String getEngineIp4AddRule() { + List ipRuleList = new ArrayList<>(); + LinkedHashMap linkedHashMap = new LinkedHashMap(); + + try{ + for(String ip : engineService){ + ipRuleList = ruleQueryWrapper.queryRuleByEngineInstance(ip); + if(ipRuleList != null) { + linkedHashMap.put(ip, ipRuleList.size()); + } + } + }catch (Exception e){ + log.error("getEngineIp4AddRule failed !" + e.getMessage()); + } + + //min + Collection c = linkedHashMap.values(); + Object[] obj = c.toArray(); + Arrays.sort(obj); + String ip = null; + for(String getKey: linkedHashMap.keySet()){ + if(linkedHashMap.get(getKey).equals(obj[0])){ + ip = getKey; + } + } + return ip; + } +} diff --git a/rulemgt/src/main/java/org/onap/holmes/rulemgt/send/RuleAllocation.java b/rulemgt/src/main/java/org/onap/holmes/rulemgt/send/RuleAllocation.java new file mode 100644 index 0000000..e69be51 --- /dev/null +++ b/rulemgt/src/main/java/org/onap/holmes/rulemgt/send/RuleAllocation.java @@ -0,0 +1,228 @@ +/** + * 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.send; + +import lombok.extern.slf4j.Slf4j; +import org.jvnet.hk2.annotations.Service; +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.bolt.enginebolt.EngineWrapper; +import org.onap.holmes.rulemgt.db.CorrelationRuleDao; +import org.onap.holmes.rulemgt.msb.EngineIpList; +import org.onap.holmes.rulemgt.wrapper.RuleQueryWrapper; +import org.onap.holmes.rulemgt.wrapper.RuleMgtWrapper; + +import javax.annotation.PostConstruct; +import javax.inject.Inject; +import java.util.*; + + +@Service +@Slf4j +public class RuleAllocation { + + private final static int ENABLE = 1; + + @Inject + private RuleMgtWrapper ruleMgtWrapper; + @Inject + private RuleQueryWrapper ruleQueryWrapper; + @Inject + private EngineWrapper engineWrapper; + @Inject + private EngineIpList engineIpList; + @Inject + private DbDaoUtil daoUtil; + + private CorrelationRuleDao correlationRuleDao; + + private int ruleCount; + private int serviceCount; + private List temIpList = new ArrayList<>(); + private List engineService = new ArrayList<>(); + private List allRules = new ArrayList<>(); + + @PostConstruct + public void initDaoUtilAndEngineIp() throws Exception{ + correlationRuleDao = daoUtil.getJdbiDaoByOnDemand(CorrelationRuleDao.class); + temIpList = engineIpList.getServiceCount(); + } + + public void judgeAndAllocateRule(List ipList)throws Exception{ + if(ipList != null) { + engineService = ipList; + serviceCount = ipList.size(); + } + if(temIpList.size() < serviceCount){ + //extend + List deleteRule = calculateRule(temIpList); + List allocateRule = calculateRule(temIpList); + List extendIp = extendCompareIp(engineService,temIpList); + AllocateService(extendIp,allocateRule); + deleteRuleFromFormerEngine(deleteRule,temIpList); + + } else if (temIpList.size() > serviceCount) { + //destroy + List destroyIp = destroyCompareIp(engineService, temIpList); + AllocateService(restIp(destroyIp), relocateRuleAfterDestroy(destroyIp)); + + } else if(temIpList.size() == serviceCount) { + temIpList = engineService; + return; + } + temIpList = engineService; + + } + + + // When the engine is expanding, the rules that need to be allocated are calculated. + private List calculateRule(List oldIpList) throws Exception{ + allRules = ruleQueryWrapper.queryRuleByEnable(ENABLE); + if(allRules != null) { + ruleCount = allRules.size(); + } + int count = ruleCount / serviceCount; + int remainder = ruleCount % serviceCount; + + List subRule = new ArrayList<>(); + for(String ip : oldIpList) { + List rules = ruleQueryWrapper.queryRuleByEngineInstance(ip); + List tem = rules.subList(count + (remainder-- / oldIpList.size()),rules.size()); + subRule.addAll(tem); + } + return subRule; + } + + //Rules that need to be allocated after the engine is destroyed + private List relocateRuleAfterDestroy(List destroyIpList) throws CorrelationException { + List rules = new ArrayList<>(); + try{ + if(destroyIpList != null){ + for(String ip : destroyIpList) { + rules.addAll(ruleQueryWrapper.queryRuleByEngineInstance(ip)); + } + } + }catch(CorrelationException e) { + log.error("method relocateRuleAfterDestroy get data from DB failed !" +e.getMessage()); + } + return rules; + } + + //Extended IP + private List extendCompareIp(List newList, List oldList){ + List extendIpList = new ArrayList<>(); + + for( String ip :newList) { + if(! oldList.contains(ip)) { + extendIpList.add(ip); + } + } + return extendIpList; + } + + //Destroyed IP + private List destroyCompareIp(List newList, List oldList) { + List destroyIpList = new ArrayList<>(); + for(String ip : oldList) { + if(!newList.contains(ip)) { + destroyIpList.add(ip); + } + } + return destroyIpList; + } + + //Residual IP after destruction + private List restIp(List destroyIp) { + List restIpList = new ArrayList<>(); + for(String ip : engineService) { + if(!destroyIp.contains(ip)) { + restIpList.add(ip); + } + } + return restIpList; + } + + public void AllocateService(List extendIpList, List subList) throws Exception{ + List needIpList = getSortIp(extendIpList); + + for(int i=0,j=0;j < subList.size();i++,j++ ){ + int index = i % needIpList.size(); + String deployIp = needIpList.get(index); + CorrelationRule rule = subList.get(j); + rule.setEngineInstance(deployIp); + allocateDeployRule(rule,deployIp); + } + } + + //The IP to be allocated is in ascending order, and the least is circulate. + private List getSortIp(List ipList){ + List ipRuleList = new ArrayList<>(); + HashMap hashMap = new HashMap(); + + try{ + for(String ip : ipList){ + ipRuleList = ruleQueryWrapper.queryRuleByEngineInstance(ip); + if(ipRuleList != null) { + hashMap.put(ip, String.valueOf(ipRuleList.size())); + } + } + }catch (Exception e){ + log.error("getEngineIp4AddRule failed !" + e.getMessage()); + } + + List> list_Data = new ArrayList>(hashMap.entrySet()); + Collections.sort(list_Data, new Comparator>() { + public int compare(Map.Entry o1, Map.Entry o2) + { + return o1.getValue().compareTo(o2.getValue()); + } + }); + + List needList = new ArrayList<>(); + for(Map.Entry map: list_Data) { + String key = map.getKey(); + needList.add(key); + } + return needList; + } + + private void allocateDeployRule(CorrelationRule rule, String ip) throws CorrelationException { + try{ + ruleMgtWrapper.deployRule2Engine(rule,ip); + correlationRuleDao.updateRule(rule); + }catch (CorrelationException e){ + throw new CorrelationException("allocate Deploy Rule failed", e); + } + } + + private void deleteRuleFromFormerEngine(List subRule, List oldList) { + try{ + for(String ip : oldList){ + for(CorrelationRule rule: subRule) { + if(ip.equals(rule.getEngineInstance())) { + engineWrapper.deleteRuleFromEngine(rule.getPackageName(),ip); + } + } + } + }catch (CorrelationException e) { + log.error("When the engine is extended, deleting rule failed" +e.getMessage()); + } + + } + +} 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 484fb5f..196b21a 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 @@ -23,7 +23,6 @@ import javax.inject.Inject; import javax.inject.Singleton; import lombok.extern.slf4j.Slf4j; import org.jvnet.hk2.annotations.Service; -import org.onap.holmes.common.dmaap.DmaapService; import org.onap.holmes.rulemgt.bean.request.CorrelationCheckRule4Engine; import org.onap.holmes.rulemgt.bean.response.RuleResult4API; import org.onap.holmes.rulemgt.constant.RuleMgtConstant; @@ -40,6 +39,7 @@ 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.send.Ip4AddingRule; @Service @@ -47,6 +47,12 @@ import org.onap.holmes.rulemgt.db.CorrelationRuleQueryDao; @Slf4j public class RuleMgtWrapper { + @Inject + private Ip4AddingRule ip4AddingRule; + + @Inject + private RuleQueryWrapper ruleQueryWrapper; + @Inject private CorrelationRuleQueryDao correlationRuleQueryDao; @Inject @@ -73,13 +79,20 @@ public class RuleMgtWrapper { if (ruleTemp != null) { throw new CorrelationException("A rule with the same name already exists."); } - String packageName = deployRule2Engine(correlationRule); + String ip =""; + try{ + ip = ip4AddingRule.getEngineIp4AddRule(); + }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); + engineWarpper.deleteRuleFromEngine(packageName, ip); throw new CorrelationException(e.getMessage(), e); } RuleAddAndUpdateResponse ruleAddAndUpdateResponse = new RuleAddAndUpdateResponse(); @@ -96,18 +109,23 @@ public class RuleMgtWrapper { 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) { - engineWarpper.deleteRuleFromEngine(oldCorrelationRule.getPackageName()); + String oldRuleEngineInstance = oldCorrelationRule.getEngineInstance(); + engineWarpper.deleteRuleFromEngine(oldCorrelationRule.getPackageName(), oldRuleEngineInstance); } - newCorrelationRule.setPackageName(deployRule2Engine(newCorrelationRule)); + newCorrelationRule.setPackageName(deployRule2Engine(newCorrelationRule, updateIp)); correlationRuleDao.updateRule(newCorrelationRule); return ruleChangeResponse; } @@ -155,7 +173,8 @@ public class RuleMgtWrapper { throw new CorrelationException("You're trying to delete a rule which does not exist in the system."); } if (correlationRule.getEnabled() == RuleMgtConstant.STATUS_RULE_OPEN) { - engineWarpper.deleteRuleFromEngine(correlationRule.getPackageName()); + String ip = correlationRule.getEngineInstance(); + engineWarpper.deleteRuleFromEngine(correlationRule.getPackageName(), ip); } correlationRuleDao.deleteRule(correlationRule); } @@ -200,11 +219,11 @@ public class RuleMgtWrapper { return correlationRule; } - private String deployRule2Engine(CorrelationRule correlationRule) + public String deployRule2Engine(CorrelationRule correlationRule, String ip) throws CorrelationException { - if (engineWarpper.checkRuleFromEngine(correlationRules2CheckRule(correlationRule)) && ( + if (engineWarpper.checkRuleFromEngine(correlationRules2CheckRule(correlationRule), ip) && ( correlationRule.getEnabled() == RuleMgtConstant.STATUS_RULE_OPEN)) { - return engineWarpper.deployEngine(correlationRules2DeployRule(correlationRule)); + return engineWarpper.deployEngine(correlationRules2DeployRule(correlationRule), ip); } return ""; } diff --git a/rulemgt/src/main/java/org/onap/holmes/rulemgt/wrapper/RuleQueryWrapper.java b/rulemgt/src/main/java/org/onap/holmes/rulemgt/wrapper/RuleQueryWrapper.java new file mode 100644 index 0000000..0ad836f --- /dev/null +++ b/rulemgt/src/main/java/org/onap/holmes/rulemgt/wrapper/RuleQueryWrapper.java @@ -0,0 +1,52 @@ +/** + * 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.jvnet.hk2.annotations.Service; +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.db.CorrelationRuleDao; + +import javax.annotation.PostConstruct; +import javax.inject.Inject; +import java.util.List; + +@Service +public class RuleQueryWrapper { + + @Inject + private DbDaoUtil daoUtil; + private CorrelationRuleDao correlationRuleDao; + + @PostConstruct + public void initDaoUtil() { + correlationRuleDao = daoUtil.getJdbiDaoByOnDemand(CorrelationRuleDao.class); + } + + public List queryRuleByEnable(int enable) throws CorrelationException { + List ruleTemp = daoUtil.getJdbiDaoByOnDemand(CorrelationRuleDao.class) + .queryRuleByRuleEnable(enable); + return ruleTemp; + } + + public List queryRuleByEngineInstance(String instance) throws CorrelationException { + List ruleTemp = daoUtil.getJdbiDaoByOnDemand(CorrelationRuleDao.class) + .queryRuleByRuleEngineInstance(instance); + return ruleTemp; + } +} diff --git a/rulemgt/src/test/java/org/onap/holmes/rulemgt/bolt/enginebolt/EngineWrapperTest.java b/rulemgt/src/test/java/org/onap/holmes/rulemgt/bolt/enginebolt/EngineWrapperTest.java index cc8c047..0664db7 100644 --- a/rulemgt/src/test/java/org/onap/holmes/rulemgt/bolt/enginebolt/EngineWrapperTest.java +++ b/rulemgt/src/test/java/org/onap/holmes/rulemgt/bolt/enginebolt/EngineWrapperTest.java @@ -64,12 +64,13 @@ public class EngineWrapperTest { thrown.expectMessage("Failed to call the rule deployment RESTful API."); EasyMock.expect( - engineServiceMock.deploy(EasyMock.anyObject(CorrelationDeployRule4Engine.class))) + engineServiceMock.deploy(EasyMock.anyObject(CorrelationDeployRule4Engine.class), + EasyMock.anyObject(String.class))) .andThrow( new RuntimeException("")); PowerMock.replayAll(); - engineWrapper.deployEngine(new CorrelationDeployRule4Engine()); + engineWrapper.deployEngine(new CorrelationDeployRule4Engine(),"10.96.33.34"); PowerMock.verifyAll(); } @@ -80,13 +81,14 @@ public class EngineWrapperTest { thrown.expectMessage("Failed to deploy the rule!"); EasyMock.expect( - engineServiceMock.deploy(EasyMock.anyObject(CorrelationDeployRule4Engine.class))) + engineServiceMock.deploy(EasyMock.anyObject(CorrelationDeployRule4Engine.class), + EasyMock.anyObject(String.class))) .andReturn(httpResponse); EasyMock.expect(httpResponse.getStatusLine()).andReturn(statusLineMock); EasyMock.expect(statusLineMock.getStatusCode()).andReturn(400); PowerMock.replayAll(); - engineWrapper.deployEngine(new CorrelationDeployRule4Engine()); + engineWrapper.deployEngine(new CorrelationDeployRule4Engine(),"10.96.33.34"); PowerMock.verifyAll(); } @@ -100,14 +102,15 @@ public class EngineWrapperTest { thrown.expectMessage( "Failed to parse the value returned by the engine management service."); EasyMock.expect( - engineServiceMock.deploy(EasyMock.anyObject(CorrelationDeployRule4Engine.class))) + engineServiceMock.deploy(EasyMock.anyObject(CorrelationDeployRule4Engine.class), + EasyMock.anyObject(String.class))) .andReturn(httpResponse); EasyMock.expect(httpResponse.getStatusLine()).andReturn(statusLineMock); EasyMock.expect(statusLineMock.getStatusCode()).andReturn(200); PowerMockito.when(HttpsUtils.extractResponseEntity(httpResponse)).thenReturn(content); PowerMock.replayAll(); - engineWrapper.deployEngine(new CorrelationDeployRule4Engine()); + engineWrapper.deployEngine(new CorrelationDeployRule4Engine(),"10.96.33.34"); PowerMock.verifyAll(); } @@ -118,14 +121,15 @@ public class EngineWrapperTest { String content = "{\"packageName\":\"test\"}"; PowerMockito.mockStatic(HttpsUtils.class); EasyMock.expect( - engineServiceMock.deploy(EasyMock.anyObject(CorrelationDeployRule4Engine.class))) + engineServiceMock.deploy(EasyMock.anyObject(CorrelationDeployRule4Engine.class), + EasyMock.anyObject(String.class))) .andReturn(httpResponse); EasyMock.expect(httpResponse.getStatusLine()).andReturn(statusLineMock); EasyMock.expect(statusLineMock.getStatusCode()).andReturn(200); PowerMockito.when(HttpsUtils.extractResponseEntity(httpResponse)).thenReturn(content); PowerMock.replayAll(); - String result = engineWrapper.deployEngine(new CorrelationDeployRule4Engine()); + String result = engineWrapper.deployEngine(new CorrelationDeployRule4Engine(),"10.96.33.34"); assertThat(result, equalTo("test")); @@ -136,11 +140,12 @@ public class EngineWrapperTest { thrown.expect(CorrelationException.class); thrown.expectMessage("Failed to call the rule deleting RESTful API."); - EasyMock.expect(engineServiceMock.delete(EasyMock.anyObject(String.class))).andThrow( + EasyMock.expect(engineServiceMock.delete(EasyMock.anyObject(String.class), + EasyMock.anyObject(String.class))).andThrow( new RuntimeException("")); PowerMock.replayAll(); - engineWrapper.deleteRuleFromEngine(""); + engineWrapper.deleteRuleFromEngine("","10.96.33.34"); PowerMock.verifyAll(); } @@ -150,28 +155,30 @@ public class EngineWrapperTest { thrown.expect(CorrelationException.class); thrown.expectMessage("Failed to delete the rule!"); - EasyMock.expect(engineServiceMock.delete(EasyMock.anyObject(String.class))) + EasyMock.expect(engineServiceMock.delete(EasyMock.anyObject(String.class), + EasyMock.anyObject(String.class))) .andReturn(httpResponse); EasyMock.expect(httpResponse.getStatusLine()).andReturn(statusLineMock); EasyMock.expect(statusLineMock.getStatusCode()).andReturn(400); PowerMock.replayAll(); - engineWrapper.deleteRuleFromEngine(""); + engineWrapper.deleteRuleFromEngine("","10.96.33.34"); PowerMock.verifyAll(); } @Test public void deleteRuleFromEngine_success() throws Exception { - EasyMock.expect(engineServiceMock.delete(EasyMock.anyObject(String.class))) + EasyMock.expect(engineServiceMock.delete(EasyMock.anyObject(String.class), + EasyMock.anyObject(String.class))) .andReturn(httpResponse); EasyMock.expect(httpResponse.getStatusLine()).andReturn(statusLineMock); EasyMock.expect(statusLineMock.getStatusCode()).andReturn(200); PowerMock.replayAll(); - boolean result = engineWrapper.deleteRuleFromEngine(""); + boolean result = engineWrapper.deleteRuleFromEngine("","10.96.33.34"); assertThat(result, equalTo(true)); } @@ -182,12 +189,13 @@ public class EngineWrapperTest { thrown.expectMessage("Failed to call the rule verification RESTful API."); EasyMock.expect( - engineServiceMock.check(EasyMock.anyObject(CorrelationCheckRule4Engine.class))) + engineServiceMock.check(EasyMock.anyObject(CorrelationCheckRule4Engine.class), + EasyMock.anyObject(String.class))) .andThrow( new RuntimeException("")); PowerMock.replayAll(); - engineWrapper.checkRuleFromEngine(new CorrelationCheckRule4Engine()); + engineWrapper.checkRuleFromEngine(new CorrelationCheckRule4Engine(),"10.96.33.34"); PowerMock.verifyAll(); } @@ -195,14 +203,15 @@ public class EngineWrapperTest { @Test public void checkRuleFromEngine_success() throws Exception { EasyMock.expect( - engineServiceMock.check(EasyMock.anyObject(CorrelationCheckRule4Engine.class))) + engineServiceMock.check(EasyMock.anyObject(CorrelationCheckRule4Engine.class), + EasyMock.anyObject(String.class))) .andReturn(httpResponse); EasyMock.expect(httpResponse.getStatusLine()).andReturn(statusLineMock); EasyMock.expect(statusLineMock.getStatusCode()).andReturn(200); PowerMock.replayAll(); - boolean result = engineWrapper.checkRuleFromEngine(new CorrelationCheckRule4Engine()); + boolean result = engineWrapper.checkRuleFromEngine(new CorrelationCheckRule4Engine(),"10.96.33.34"); assertThat(result, equalTo(true)); } diff --git a/rulemgt/src/test/java/org/onap/holmes/rulemgt/send/RuleAllocationTest.java b/rulemgt/src/test/java/org/onap/holmes/rulemgt/send/RuleAllocationTest.java new file mode 100644 index 0000000..24c5f3a --- /dev/null +++ b/rulemgt/src/test/java/org/onap/holmes/rulemgt/send/RuleAllocationTest.java @@ -0,0 +1,63 @@ +/** +* 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.send; + + +import org.junit.Test; +import org.powermock.api.easymock.PowerMock; +import org.powermock.reflect.Whitebox; + +import java.util.ArrayList; +import java.util.List; + +import static org.hamcrest.core.IsEqual.equalTo; +import static org.junit.Assert.assertThat; + +public class RuleAllocationTest { + + private RuleAllocation ruleAllocation = new RuleAllocation(); + @Test + public void extendCompareIpTest() throws Exception{ + List newList = new ArrayList<>(); + newList.add("10.96.33.34"); + newList.add("10.74.65.24"); + + List oldList = new ArrayList<>(); + oldList.add("10.96.33.34"); + List extendIp = Whitebox.invokeMethod(ruleAllocation,"extendCompareIp",newList,oldList); + + PowerMock.verifyAll(); + + assertThat(extendIp.get(0),equalTo("10.74.65.24")); + } + + @Test + public void destroyCompareIpTest() throws Exception{ + List newList = new ArrayList<>(); + newList.add("10.96.33.34"); + + List oldList = new ArrayList<>(); + oldList.add("10.96.33.34"); + oldList.add("10.74.65.24"); + List destoryIp = Whitebox.invokeMethod(ruleAllocation,"destroyCompareIp",newList,oldList); + + PowerMock.verifyAll(); + + assertThat(destoryIp.get(0),equalTo("10.74.65.24")); + } + +} 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 d448e40..4b3e60f 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 @@ -28,7 +28,6 @@ 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.rulemgt.bean.request.CorrelationCheckRule4Engine; import org.onap.holmes.rulemgt.db.CorrelationRuleDao; import org.onap.holmes.common.api.entity.CorrelationRule; @@ -43,6 +42,7 @@ 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.send.Ip4AddingRule; import org.powermock.api.easymock.PowerMock; import org.powermock.modules.junit4.rule.PowerMockRule; import org.powermock.reflect.Whitebox; @@ -64,6 +64,8 @@ public class RuleMgtWrapperTest { private CorrelationRuleDao correlationRuleDaoMock; + private Ip4AddingRule ip4AddingRuleMock; + private static final String USER_NAME = "admin"; @Before @@ -75,11 +77,13 @@ public class RuleMgtWrapperTest { correlationRuleQueryDaoMock = PowerMock.createMock(CorrelationRuleQueryDao.class); dbDaoUtilMock = PowerMock.createMock(DbDaoUtil.class); correlationRuleDaoMock = PowerMock.createMock(CorrelationRuleDao.class); + ip4AddingRuleMock = PowerMock.createMock(Ip4AddingRule.class); Whitebox.setInternalState(ruleMgtWrapper, "daoUtil", dbDaoUtilMock); Whitebox.setInternalState(ruleMgtWrapper, "correlationRuleQueryDao", correlationRuleQueryDaoMock); Whitebox.setInternalState(ruleMgtWrapper, "engineWarpper", engineWrapperMock); Whitebox.setInternalState(ruleMgtWrapper, "correlationRuleDao", correlationRuleDaoMock); + Whitebox.setInternalState(ruleMgtWrapper,"ip4AddingRule", ip4AddingRuleMock); PowerMock.resetAll(); } @@ -165,9 +169,12 @@ public class RuleMgtWrapperTest { correlationRuleRet.setRid("rule_" + System.currentTimeMillis()); EasyMock.expect(correlationRuleDaoMock.queryRuleByRuleName(ruleName)).andReturn(null); - EasyMock.expect(engineWrapperMock.checkRuleFromEngine(EasyMock.anyObject(CorrelationCheckRule4Engine.class))) + EasyMock.expect(ip4AddingRuleMock.getEngineIp4AddRule()).andReturn("10.96.33.34"); + EasyMock.expect(engineWrapperMock.checkRuleFromEngine(EasyMock.anyObject(CorrelationCheckRule4Engine.class) + , EasyMock.anyObject(String.class))) .andReturn(true); - EasyMock.expect(engineWrapperMock.deployEngine(EasyMock.anyObject(CorrelationDeployRule4Engine.class))) + 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); @@ -198,15 +205,18 @@ public class RuleMgtWrapperTest { oldCorrelationRule.setPackageName("testName"); oldCorrelationRule.setEnabled(1); oldCorrelationRule.setClosedControlLoopName("cl-name"); + oldCorrelationRule.setEngineInstance("10.96.33.34"); RuleUpdateRequest ruleUpdateRequest = createRuleUpdateRequest("rule_1", "cl-name", "des2", "contetnt2", 1); EasyMock.expect(correlationRuleDaoMock.queryRuleByRid("rule_1")).andReturn(oldCorrelationRule); - EasyMock.expect(engineWrapperMock.deleteRuleFromEngine("testName")).andReturn(true); + EasyMock.expect(engineWrapperMock.deleteRuleFromEngine("testName", "10.96.33.34")).andReturn(true); correlationRuleDaoMock.updateRule(EasyMock.anyObject(CorrelationRule.class)); EasyMock.expectLastCall(); - EasyMock.expect(engineWrapperMock.checkRuleFromEngine(EasyMock.anyObject(CorrelationCheckRule4Engine.class))) + EasyMock.expect(engineWrapperMock.checkRuleFromEngine(EasyMock.anyObject(CorrelationCheckRule4Engine.class) + , EasyMock.anyObject(String.class))) .andReturn(true); - EasyMock.expect(engineWrapperMock.deployEngine(EasyMock.anyObject(CorrelationDeployRule4Engine.class))) + EasyMock.expect(engineWrapperMock.deployEngine(EasyMock.anyObject(CorrelationDeployRule4Engine.class) + , EasyMock.anyObject(String.class))) .andReturn("packageName1"); PowerMock.replayAll(); @@ -288,7 +298,8 @@ public class RuleMgtWrapperTest { correlationRule.setEnabled(1); EasyMock.expect(correlationRuleDaoMock.queryRuleByRid(ruleDeleteRequest.getRuleId())) .andReturn(correlationRule); - EasyMock.expect(engineWrapperMock.deleteRuleFromEngine(EasyMock.anyObject(String.class))).andReturn(true); + EasyMock.expect(engineWrapperMock.deleteRuleFromEngine(EasyMock.anyObject(String.class) + , EasyMock.anyObject(String.class))).andReturn(true); correlationRuleDaoMock.deleteRule(EasyMock.anyObject(CorrelationRule.class)); EasyMock.expectLastCall(); PowerMock.replayAll(); diff --git a/rulemgt/src/test/java/org/onap/holmes/rulemgt/wrapper/RuleQueryWrapperTest.java b/rulemgt/src/test/java/org/onap/holmes/rulemgt/wrapper/RuleQueryWrapperTest.java new file mode 100644 index 0000000..b07efe9 --- /dev/null +++ b/rulemgt/src/test/java/org/onap/holmes/rulemgt/wrapper/RuleQueryWrapperTest.java @@ -0,0 +1,61 @@ +/** + * 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.hamcrest.core.IsNull; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.onap.holmes.common.api.entity.CorrelationRule; +import org.onap.holmes.rulemgt.db.CorrelationRuleDao; +import org.powermock.api.easymock.PowerMock; + +import java.util.ArrayList; +import java.util.List; + + +public class RuleQueryWrapperTest { + private CorrelationRuleDao correlationRuleDao; + private RuleQueryWrapper ruleQueryWrapper; + + @Before + public void setUp() { + correlationRuleDao = PowerMock.createMock(CorrelationRuleDao.class); + ruleQueryWrapper = PowerMock.createMock(RuleQueryWrapper.class); + } + + @Test + public void queryRuleByEnable() throws Exception{ + int enable = 0; + EasyMock.expect(ruleQueryWrapper.queryRuleByEnable(EasyMock.anyInt())).andReturn(new ArrayList()); + PowerMock.replayAll(); + List correlationRules = ruleQueryWrapper.queryRuleByEnable(enable); + PowerMock.verifyAll(); + Assert.assertThat(correlationRules, IsNull.>notNullValue()); + } + + @Test + public void queryRuleByEngineInstance() throws Exception{ + String engineInstance = "10.96.33.34"; + EasyMock.expect(ruleQueryWrapper.queryRuleByEngineInstance(EasyMock.anyObject(String.class))).andReturn(new ArrayList()); + PowerMock.replayAll(); + List correlationRules = ruleQueryWrapper.queryRuleByEngineInstance(engineInstance); + PowerMock.verifyAll(); + Assert.assertThat(correlationRules, IsNull.>notNullValue()); + } +} -- cgit 1.2.3-korg