Allocate Rule 45/39345/3
authorCongcong Peng <peng.congcong@zte.com.cn>
Wed, 28 Mar 2018 01:42:09 +0000 (09:42 +0800)
committerCongcong Peng <peng.congcong@zte.com.cn>
Wed, 28 Mar 2018 03:07:12 +0000 (11:07 +0800)
Issue-ID: HOLMES-106

Change-Id: I50937669cf26ead5abb8d2ef9bd20bc61095ac1b
Signed-off-by: Congcong Peng <peng.congcong@zte.com.cn>
15 files changed:
rulemgt-standalone/src/main/assembly/dbscripts/postgresql/onap-holmes_rulemgt-createobj.sql
rulemgt/src/main/java/org/onap/holmes/rulemgt/bolt/enginebolt/EngineService.java
rulemgt/src/main/java/org/onap/holmes/rulemgt/bolt/enginebolt/EngineWrapper.java
rulemgt/src/main/java/org/onap/holmes/rulemgt/db/CorrelationRuleDao.java
rulemgt/src/main/java/org/onap/holmes/rulemgt/db/CorrelationRuleQueryDao.java
rulemgt/src/main/java/org/onap/holmes/rulemgt/msb/EngineIpList.java [new file with mode: 0644]
rulemgt/src/main/java/org/onap/holmes/rulemgt/msb/MsbQuery.java [new file with mode: 0644]
rulemgt/src/main/java/org/onap/holmes/rulemgt/send/Ip4AddingRule.java [new file with mode: 0644]
rulemgt/src/main/java/org/onap/holmes/rulemgt/send/RuleAllocation.java [new file with mode: 0644]
rulemgt/src/main/java/org/onap/holmes/rulemgt/wrapper/RuleMgtWrapper.java
rulemgt/src/main/java/org/onap/holmes/rulemgt/wrapper/RuleQueryWrapper.java [new file with mode: 0644]
rulemgt/src/test/java/org/onap/holmes/rulemgt/bolt/enginebolt/EngineWrapperTest.java
rulemgt/src/test/java/org/onap/holmes/rulemgt/send/RuleAllocationTest.java [new file with mode: 0644]
rulemgt/src/test/java/org/onap/holmes/rulemgt/wrapper/RuleMgtWrapperTest.java
rulemgt/src/test/java/org/onap/holmes/rulemgt/wrapper/RuleQueryWrapperTest.java [new file with mode: 0644]

index 87dd3a7..feaacd6 100644 (file)
@@ -43,6 +43,7 @@ CREATE TABLE APLUS_RULE (
   PARAMS VARCHAR(4000) NULL,
   CONTENT VARCHAR(4000) NOT NULL,
   VENDOR VARCHAR(100)  NOT NULL,
+  ENGINEINSTANCE VARCHAR(100) NOT NULL,
   PACKAGE VARCHAR(255) NULL,
   PRIMARY KEY (RID),
   UNIQUE (NAME)
index 319347e..13507d6 100644 (file)
@@ -32,24 +32,27 @@ import org.onap.holmes.common.config.MicroServiceConfig;
 @Service\r
 public class EngineService {\r
 \r
-    protected HttpResponse delete(String packageName) throws Exception {\r
+    private static final String PREFIX = "https://";\r
+    private static final String PORT = ":9102";\r
+\r
+    protected HttpResponse delete(String packageName, String ip) throws Exception {\r
         HashMap headers = createHeaders();\r
-        String url = MicroServiceConfig.getMsbServerAddrWithHttpPrefix() + RuleMgtConstant.ENGINE_PATH + "/" + packageName;\r
+        String url = PREFIX + ip + PORT + RuleMgtConstant.ENGINE_PATH + "/" + packageName;\r
         return HttpsUtils.delete(url, headers);\r
     }\r
 \r
-    protected HttpResponse check(CorrelationCheckRule4Engine correlationCheckRule4Engine)\r
+    protected HttpResponse check(CorrelationCheckRule4Engine correlationCheckRule4Engine, String ip)\r
             throws Exception {\r
         String content = GsonUtil.beanToJson(correlationCheckRule4Engine);\r
         HashMap headers = createHeaders();\r
-        String url = MicroServiceConfig.getMsbServerAddrWithHttpPrefix() + RuleMgtConstant.ENGINE_PATH;\r
+        String url = PREFIX + ip + PORT + RuleMgtConstant.ENGINE_PATH;\r
         return HttpsUtils.post(url, headers, new HashMap<>(), new StringEntity(content));\r
     }\r
 \r
-    protected HttpResponse deploy(CorrelationDeployRule4Engine correlationDeployRule4Engine) throws Exception {\r
+    protected HttpResponse deploy(CorrelationDeployRule4Engine correlationDeployRule4Engine, String ip) throws Exception {\r
         String content = GsonUtil.beanToJson(correlationDeployRule4Engine);\r
         HashMap headers = createHeaders();\r
-        String url = MicroServiceConfig.getMsbServerAddrWithHttpPrefix() + RuleMgtConstant.ENGINE_PATH;\r
+        String url = PREFIX + ip + PORT + RuleMgtConstant.ENGINE_PATH;\r
         return HttpsUtils.put(url, headers, new HashMap<>(), new StringEntity(content));\r
     }\r
 \r
index 18a326e..b0bd1f5 100644 (file)
@@ -34,10 +34,10 @@ public class EngineWrapper {
     @Inject\r
     private EngineService engineService;\r
 \r
-    public String deployEngine(CorrelationDeployRule4Engine correlationRule) throws CorrelationException {\r
+    public String deployEngine(CorrelationDeployRule4Engine correlationRule,String ip) throws CorrelationException {\r
         HttpResponse response;\r
         try {\r
-            response = engineService.deploy(correlationRule);\r
+            response = engineService.deploy(correlationRule, ip);\r
         } catch (Exception e) {\r
             throw new CorrelationException("Failed to call the rule deployment RESTful API.", e);\r
         }\r
@@ -55,10 +55,10 @@ public class EngineWrapper {
         }\r
     }\r
 \r
-    public boolean deleteRuleFromEngine(String packageName) throws CorrelationException {\r
+    public boolean deleteRuleFromEngine(String packageName,String ip) throws CorrelationException {\r
         HttpResponse response;\r
         try {\r
-            response = engineService.delete(packageName);\r
+            response = engineService.delete(packageName, ip);\r
         } catch (Exception e) {\r
             throw new CorrelationException("Failed to call the rule deleting RESTful API.", e);\r
         }\r
@@ -70,12 +70,12 @@ public class EngineWrapper {
         }\r
     }\r
 \r
-    public boolean checkRuleFromEngine(CorrelationCheckRule4Engine correlationCheckRule4Engine)\r
+    public boolean checkRuleFromEngine(CorrelationCheckRule4Engine correlationCheckRule4Engine,String ip)\r
             throws CorrelationException {\r
         log.info("Rule Contents: " + correlationCheckRule4Engine.getContent());\r
         HttpResponse response;\r
         try {\r
-            response = engineService.check(correlationCheckRule4Engine);\r
+            response = engineService.check(correlationCheckRule4Engine, ip);\r
         } catch (Exception e) {\r
             throw new CorrelationException("Failed to call the rule verification RESTful API.", e);\r
         }\r
index a94c1db..64dcea2 100644 (file)
@@ -30,10 +30,10 @@ import org.skife.jdbi.v2.sqlobject.customizers.RegisterMapper;
 public abstract class CorrelationRuleDao {\r
 \r
     @GetGeneratedKeys\r
-    @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)")\r
+    @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)")\r
     protected abstract String addRule(@BindBean CorrelationRule correlationRule);\r
 \r
-    @SqlUpdate("UPDATE APLUS_RULE SET CTRLLOOP=:closedControlLoopName,DESCRIPTION=:description,ENABLE=:enabled,CONTENT=:content,UPDATOR=:modifier,UPDATETIME=:updateTime, PACKAGE=:packageName WHERE RID=:rid")\r
+    @SqlUpdate("UPDATE APLUS_RULE SET CTRLLOOP=:closedControlLoopName,DESCRIPTION=:description,ENABLE=:enabled,CONTENT=:content,UPDATOR=:modifier,UPDATETIME=:updateTime, PACKAGE=:packageName, ENGINEINSTANCE=:engineInstance WHERE RID=:rid")\r
     protected abstract int updateRuleByRid(@BindBean CorrelationRule correlationRule);\r
 \r
     @SqlUpdate("DELETE FROM APLUS_RULE WHERE RID=:rid")\r
@@ -51,6 +51,21 @@ public abstract class CorrelationRuleDao {
     @SqlQuery("SELECT * FROM APLUS_RULE WHERE NAME=:name")\r
     protected abstract CorrelationRule queryRuleByName(@Bind("name") String name);\r
 \r
+    @SqlQuery("SELECT * FROM APLUS_RULE WHERE enable=:enable")\r
+    public abstract List<CorrelationRule> queryRuleByEnable(@Bind("enable") int enable);\r
+\r
+    @SqlQuery("SELECT * FROM APLUS_RULE WHERE engineinstance=:engineinstance")\r
+    public abstract List<CorrelationRule> queryRuleByEngineInstance(@Bind("engineinstance") String engineinstance);\r
+\r
+    public List<CorrelationRule> queryRuleByRuleEngineInstance(String enginetype) {\r
+        return queryRuleByEngineInstance(enginetype);\r
+    }\r
+\r
+    public List<CorrelationRule> queryRuleByRuleEnable(int enable) {\r
+        return queryRuleByEnable(enable);\r
+    }\r
+\r
+\r
     private void deleteRule2DbInner(CorrelationRule correlationRule) {\r
         String name = correlationRule.getName() != null ? correlationRule.getName().trim() : "";\r
         String rid = correlationRule.getRid() != null ? correlationRule.getRid().trim() : "";\r
index 85980e5..b0f0b96 100644 (file)
@@ -81,6 +81,7 @@ public class CorrelationRuleQueryDao {
         correlationRule.setVendor((String) value.get("vendor"));\r
         correlationRule.setPackageName((String) value.get("package"));\r
         correlationRule.setClosedControlLoopName((String) value.get("ctrlloop"));\r
+        correlationRule.setEngineInstance((String) value.get("engineInstance"));\r
         return correlationRule;\r
     }\r
 \r
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 (file)
index 0000000..cfccd18
--- /dev/null
@@ -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<String> 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<ServiceNode4Query> nodesList = service.getNodes();
+        List<String> 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 (file)
index 0000000..ed9b9af
--- /dev/null
@@ -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<String> 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 (file)
index 0000000..f82d3a4
--- /dev/null
@@ -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<String> engineService;
+
+    public void getIpList(List<String> ipList){
+        engineService = ipList;
+    }
+
+    public String getEngineIp4AddRule() {
+        List<CorrelationRule> ipRuleList  = new ArrayList<>();
+        LinkedHashMap<String,Integer> 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<Integer> 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 (file)
index 0000000..e69be51
--- /dev/null
@@ -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<String> temIpList = new ArrayList<>();
+    private List<String> engineService = new ArrayList<>();
+    private List<CorrelationRule> allRules = new ArrayList<>();
+
+    @PostConstruct
+    public void initDaoUtilAndEngineIp() throws Exception{
+        correlationRuleDao = daoUtil.getJdbiDaoByOnDemand(CorrelationRuleDao.class);
+        temIpList =  engineIpList.getServiceCount();
+    }
+
+    public void judgeAndAllocateRule(List<String> ipList)throws Exception{
+        if(ipList != null) {
+            engineService = ipList;
+            serviceCount = ipList.size();
+        }
+        if(temIpList.size() < serviceCount){
+            //extend
+            List<CorrelationRule> deleteRule = calculateRule(temIpList);
+            List<CorrelationRule> allocateRule =  calculateRule(temIpList);
+            List<String> extendIp = extendCompareIp(engineService,temIpList);
+            AllocateService(extendIp,allocateRule);
+            deleteRuleFromFormerEngine(deleteRule,temIpList);
+
+        } else if (temIpList.size() > serviceCount) {
+            //destroy
+            List<String> 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<CorrelationRule> calculateRule(List<String> oldIpList) throws Exception{
+        allRules = ruleQueryWrapper.queryRuleByEnable(ENABLE);
+        if(allRules != null) {
+            ruleCount = allRules.size();
+        }
+        int count = ruleCount / serviceCount;
+        int remainder = ruleCount % serviceCount;
+
+        List<CorrelationRule> subRule = new ArrayList<>();
+        for(String ip : oldIpList) {
+            List<CorrelationRule> rules = ruleQueryWrapper.queryRuleByEngineInstance(ip);
+            List<CorrelationRule> 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<CorrelationRule> relocateRuleAfterDestroy(List<String> destroyIpList) throws CorrelationException {
+        List<CorrelationRule> 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<String> extendCompareIp(List<String> newList, List<String> oldList){
+        List<String> extendIpList = new ArrayList<>();
+
+        for( String ip :newList) {
+            if(! oldList.contains(ip)) {
+                extendIpList.add(ip);
+            }
+        }
+        return extendIpList;
+    }
+
+    //Destroyed IP
+    private List<String> destroyCompareIp(List<String> newList, List<String> oldList) {
+        List<String> destroyIpList = new ArrayList<>();
+        for(String ip : oldList) {
+            if(!newList.contains(ip)) {
+                destroyIpList.add(ip);
+            }
+        }
+        return destroyIpList;
+    }
+
+    //Residual IP after destruction
+    private List<String> restIp(List<String> destroyIp) {
+        List<String> restIpList = new ArrayList<>();
+        for(String ip : engineService) {
+            if(!destroyIp.contains(ip)) {
+                restIpList.add(ip);
+            }
+        }
+        return restIpList;
+    }
+
+    public void AllocateService(List<String> extendIpList, List<CorrelationRule> subList) throws Exception{
+        List<String> 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<String > getSortIp(List<String> ipList){
+        List<CorrelationRule> ipRuleList  = new ArrayList<>();
+        HashMap<String,String> 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<Map.Entry<String, String>> list_Data = new ArrayList<Map.Entry<String, String>>(hashMap.entrySet());
+        Collections.sort(list_Data, new Comparator<Map.Entry<String, String>>() {
+            public int compare(Map.Entry<String, String> o1, Map.Entry<String, String> o2)
+            {
+                return o1.getValue().compareTo(o2.getValue());
+            }
+        });
+
+        List<String> needList = new ArrayList<>();
+        for(Map.Entry<String, String> 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<CorrelationRule> subRule, List<String> 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());
+        }
+
+    }
+
+}
index 484fb5f..196b21a 100644 (file)
@@ -23,7 +23,6 @@ import javax.inject.Inject;
 import javax.inject.Singleton;\r
 import lombok.extern.slf4j.Slf4j;\r
 import org.jvnet.hk2.annotations.Service;\r
-import org.onap.holmes.common.dmaap.DmaapService;\r
 import org.onap.holmes.rulemgt.bean.request.CorrelationCheckRule4Engine;\r
 import org.onap.holmes.rulemgt.bean.response.RuleResult4API;\r
 import org.onap.holmes.rulemgt.constant.RuleMgtConstant;\r
@@ -40,6 +39,7 @@ import org.onap.holmes.rulemgt.bean.response.RuleAddAndUpdateResponse;
 import org.onap.holmes.rulemgt.bean.response.RuleQueryListResponse;\r
 import org.onap.holmes.rulemgt.bolt.enginebolt.EngineWrapper;\r
 import org.onap.holmes.rulemgt.db.CorrelationRuleQueryDao;\r
+import org.onap.holmes.rulemgt.send.Ip4AddingRule;\r
 \r
 \r
 @Service\r
@@ -47,6 +47,12 @@ import org.onap.holmes.rulemgt.db.CorrelationRuleQueryDao;
 @Slf4j\r
 public class RuleMgtWrapper {\r
 \r
+    @Inject\r
+    private Ip4AddingRule ip4AddingRule;\r
+\r
+    @Inject\r
+    private RuleQueryWrapper ruleQueryWrapper;\r
+\r
     @Inject\r
     private CorrelationRuleQueryDao correlationRuleQueryDao;\r
     @Inject\r
@@ -73,13 +79,20 @@ public class RuleMgtWrapper {
         if (ruleTemp != null) {\r
             throw new CorrelationException("A rule with the same name already exists.");\r
         }\r
-        String packageName = deployRule2Engine(correlationRule);\r
+        String ip ="";\r
+        try{\r
+            ip = ip4AddingRule.getEngineIp4AddRule();\r
+        }catch(Exception e){\r
+            log.error("When adding rules, can not get engine instance ip");\r
+        }\r
+        String packageName = deployRule2Engine(correlationRule, ip);\r
         correlationRule.setPackageName(packageName);\r
+        correlationRule.setEngineInstance(ip);\r
         CorrelationRule result = null;\r
         try {\r
             result = correlationRuleDao.saveRule(correlationRule);\r
         } catch (CorrelationException e) {\r
-            engineWarpper.deleteRuleFromEngine(packageName);\r
+            engineWarpper.deleteRuleFromEngine(packageName, ip);\r
             throw new CorrelationException(e.getMessage(), e);\r
         }\r
         RuleAddAndUpdateResponse ruleAddAndUpdateResponse = new RuleAddAndUpdateResponse();\r
@@ -96,18 +109,23 @@ public class RuleMgtWrapper {
         if (oldCorrelationRule == null) {\r
             throw new CorrelationException("You're trying to update a rule which does not exist in the system.");\r
         }\r
+        String updateIp = "";\r
+        updateIp = oldCorrelationRule.getEngineInstance();\r
         CorrelationRule newCorrelationRule = convertRuleUpdateRequest2CorrelationRule(modifier,\r
                 ruleUpdateRequest, oldCorrelationRule.getName());\r
+        newCorrelationRule.setEngineInstance(updateIp);\r
         checkCorrelation(newCorrelationRule);\r
         RuleAddAndUpdateResponse ruleChangeResponse = new RuleAddAndUpdateResponse();\r
         ruleChangeResponse.setRuleId(newCorrelationRule.getRid());\r
+\r
         if (!haveChange(newCorrelationRule, oldCorrelationRule)) {\r
             return ruleChangeResponse;\r
         }\r
         if (oldCorrelationRule.getEnabled() == RuleMgtConstant.STATUS_RULE_OPEN) {\r
-            engineWarpper.deleteRuleFromEngine(oldCorrelationRule.getPackageName());\r
+            String oldRuleEngineInstance = oldCorrelationRule.getEngineInstance();\r
+            engineWarpper.deleteRuleFromEngine(oldCorrelationRule.getPackageName(), oldRuleEngineInstance);\r
         }\r
-        newCorrelationRule.setPackageName(deployRule2Engine(newCorrelationRule));\r
+        newCorrelationRule.setPackageName(deployRule2Engine(newCorrelationRule, updateIp));\r
         correlationRuleDao.updateRule(newCorrelationRule);\r
         return ruleChangeResponse;\r
     }\r
@@ -155,7 +173,8 @@ public class RuleMgtWrapper {
             throw new CorrelationException("You're trying to delete a rule which does not exist in the system.");\r
         }\r
         if (correlationRule.getEnabled() == RuleMgtConstant.STATUS_RULE_OPEN) {\r
-            engineWarpper.deleteRuleFromEngine(correlationRule.getPackageName());\r
+            String ip = correlationRule.getEngineInstance();\r
+            engineWarpper.deleteRuleFromEngine(correlationRule.getPackageName(), ip);\r
         }\r
         correlationRuleDao.deleteRule(correlationRule);\r
     }\r
@@ -200,11 +219,11 @@ public class RuleMgtWrapper {
         return correlationRule;\r
     }\r
 \r
-    private String deployRule2Engine(CorrelationRule correlationRule)\r
+    public String deployRule2Engine(CorrelationRule correlationRule, String ip)\r
             throws CorrelationException {\r
-        if (engineWarpper.checkRuleFromEngine(correlationRules2CheckRule(correlationRule)) && (\r
+        if (engineWarpper.checkRuleFromEngine(correlationRules2CheckRule(correlationRule), ip) && (\r
                 correlationRule.getEnabled() == RuleMgtConstant.STATUS_RULE_OPEN)) {\r
-            return engineWarpper.deployEngine(correlationRules2DeployRule(correlationRule));\r
+            return engineWarpper.deployEngine(correlationRules2DeployRule(correlationRule), ip);\r
         }\r
         return "";\r
     }\r
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 (file)
index 0000000..0ad836f
--- /dev/null
@@ -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<CorrelationRule> queryRuleByEnable(int enable) throws CorrelationException {
+        List<CorrelationRule> ruleTemp = daoUtil.getJdbiDaoByOnDemand(CorrelationRuleDao.class)
+                .queryRuleByRuleEnable(enable);
+        return ruleTemp;
+    }
+
+    public List<CorrelationRule> queryRuleByEngineInstance(String instance) throws CorrelationException {
+        List<CorrelationRule> ruleTemp = daoUtil.getJdbiDaoByOnDemand(CorrelationRuleDao.class)
+                .queryRuleByRuleEngineInstance(instance);
+        return ruleTemp;
+    }
+}
index cc8c047..0664db7 100644 (file)
@@ -64,12 +64,13 @@ public class EngineWrapperTest {
         thrown.expectMessage("Failed to call the rule deployment RESTful API.");\r
 \r
         EasyMock.expect(\r
-                engineServiceMock.deploy(EasyMock.anyObject(CorrelationDeployRule4Engine.class)))\r
+                engineServiceMock.deploy(EasyMock.anyObject(CorrelationDeployRule4Engine.class),\r
+                        EasyMock.anyObject(String.class)))\r
                 .andThrow(\r
                         new RuntimeException(""));\r
         PowerMock.replayAll();\r
 \r
-        engineWrapper.deployEngine(new CorrelationDeployRule4Engine());\r
+        engineWrapper.deployEngine(new CorrelationDeployRule4Engine(),"10.96.33.34");\r
 \r
         PowerMock.verifyAll();\r
     }\r
@@ -80,13 +81,14 @@ public class EngineWrapperTest {
         thrown.expectMessage("Failed to deploy the rule!");\r
 \r
         EasyMock.expect(\r
-                engineServiceMock.deploy(EasyMock.anyObject(CorrelationDeployRule4Engine.class)))\r
+                engineServiceMock.deploy(EasyMock.anyObject(CorrelationDeployRule4Engine.class),\r
+                        EasyMock.anyObject(String.class)))\r
                 .andReturn(httpResponse);\r
         EasyMock.expect(httpResponse.getStatusLine()).andReturn(statusLineMock);\r
         EasyMock.expect(statusLineMock.getStatusCode()).andReturn(400);\r
         PowerMock.replayAll();\r
 \r
-        engineWrapper.deployEngine(new CorrelationDeployRule4Engine());\r
+        engineWrapper.deployEngine(new CorrelationDeployRule4Engine(),"10.96.33.34");\r
 \r
         PowerMock.verifyAll();\r
     }\r
@@ -100,14 +102,15 @@ public class EngineWrapperTest {
         thrown.expectMessage(\r
                 "Failed to parse the value returned by the engine management service.");\r
         EasyMock.expect(\r
-                engineServiceMock.deploy(EasyMock.anyObject(CorrelationDeployRule4Engine.class)))\r
+                engineServiceMock.deploy(EasyMock.anyObject(CorrelationDeployRule4Engine.class),\r
+                        EasyMock.anyObject(String.class)))\r
                 .andReturn(httpResponse);\r
         EasyMock.expect(httpResponse.getStatusLine()).andReturn(statusLineMock);\r
         EasyMock.expect(statusLineMock.getStatusCode()).andReturn(200);\r
         PowerMockito.when(HttpsUtils.extractResponseEntity(httpResponse)).thenReturn(content);\r
         PowerMock.replayAll();\r
 \r
-        engineWrapper.deployEngine(new CorrelationDeployRule4Engine());\r
+        engineWrapper.deployEngine(new CorrelationDeployRule4Engine(),"10.96.33.34");\r
 \r
         PowerMock.verifyAll();\r
     }\r
@@ -118,14 +121,15 @@ public class EngineWrapperTest {
         String content = "{\"packageName\":\"test\"}";\r
         PowerMockito.mockStatic(HttpsUtils.class);\r
         EasyMock.expect(\r
-                engineServiceMock.deploy(EasyMock.anyObject(CorrelationDeployRule4Engine.class)))\r
+                engineServiceMock.deploy(EasyMock.anyObject(CorrelationDeployRule4Engine.class),\r
+                        EasyMock.anyObject(String.class)))\r
                 .andReturn(httpResponse);\r
         EasyMock.expect(httpResponse.getStatusLine()).andReturn(statusLineMock);\r
         EasyMock.expect(statusLineMock.getStatusCode()).andReturn(200);\r
         PowerMockito.when(HttpsUtils.extractResponseEntity(httpResponse)).thenReturn(content);\r
         PowerMock.replayAll();\r
 \r
-        String result = engineWrapper.deployEngine(new CorrelationDeployRule4Engine());\r
+        String result = engineWrapper.deployEngine(new CorrelationDeployRule4Engine(),"10.96.33.34");\r
 \r
         assertThat(result, equalTo("test"));\r
 \r
@@ -136,11 +140,12 @@ public class EngineWrapperTest {
         thrown.expect(CorrelationException.class);\r
         thrown.expectMessage("Failed to call the rule deleting RESTful API.");\r
 \r
-        EasyMock.expect(engineServiceMock.delete(EasyMock.anyObject(String.class))).andThrow(\r
+        EasyMock.expect(engineServiceMock.delete(EasyMock.anyObject(String.class),\r
+                EasyMock.anyObject(String.class))).andThrow(\r
                 new RuntimeException(""));\r
         PowerMock.replayAll();\r
 \r
-        engineWrapper.deleteRuleFromEngine("");\r
+        engineWrapper.deleteRuleFromEngine("","10.96.33.34");\r
 \r
         PowerMock.verifyAll();\r
     }\r
@@ -150,28 +155,30 @@ public class EngineWrapperTest {
         thrown.expect(CorrelationException.class);\r
         thrown.expectMessage("Failed to delete the rule!");\r
 \r
-        EasyMock.expect(engineServiceMock.delete(EasyMock.anyObject(String.class)))\r
+        EasyMock.expect(engineServiceMock.delete(EasyMock.anyObject(String.class),\r
+                EasyMock.anyObject(String.class)))\r
                 .andReturn(httpResponse);\r
         EasyMock.expect(httpResponse.getStatusLine()).andReturn(statusLineMock);\r
         EasyMock.expect(statusLineMock.getStatusCode()).andReturn(400);\r
 \r
         PowerMock.replayAll();\r
 \r
-        engineWrapper.deleteRuleFromEngine("");\r
+        engineWrapper.deleteRuleFromEngine("","10.96.33.34");\r
 \r
         PowerMock.verifyAll();\r
     }\r
 \r
     @Test\r
     public void deleteRuleFromEngine_success() throws Exception {\r
-        EasyMock.expect(engineServiceMock.delete(EasyMock.anyObject(String.class)))\r
+        EasyMock.expect(engineServiceMock.delete(EasyMock.anyObject(String.class),\r
+                EasyMock.anyObject(String.class)))\r
                 .andReturn(httpResponse);\r
         EasyMock.expect(httpResponse.getStatusLine()).andReturn(statusLineMock);\r
         EasyMock.expect(statusLineMock.getStatusCode()).andReturn(200);\r
 \r
         PowerMock.replayAll();\r
 \r
-        boolean result = engineWrapper.deleteRuleFromEngine("");\r
+        boolean result = engineWrapper.deleteRuleFromEngine("","10.96.33.34");\r
 \r
         assertThat(result, equalTo(true));\r
     }\r
@@ -182,12 +189,13 @@ public class EngineWrapperTest {
         thrown.expectMessage("Failed to call the rule verification RESTful API.");\r
 \r
         EasyMock.expect(\r
-                engineServiceMock.check(EasyMock.anyObject(CorrelationCheckRule4Engine.class)))\r
+                engineServiceMock.check(EasyMock.anyObject(CorrelationCheckRule4Engine.class),\r
+                        EasyMock.anyObject(String.class)))\r
                 .andThrow(\r
                         new RuntimeException(""));\r
         PowerMock.replayAll();\r
 \r
-        engineWrapper.checkRuleFromEngine(new CorrelationCheckRule4Engine());\r
+        engineWrapper.checkRuleFromEngine(new CorrelationCheckRule4Engine(),"10.96.33.34");\r
 \r
         PowerMock.verifyAll();\r
     }\r
@@ -195,14 +203,15 @@ public class EngineWrapperTest {
     @Test\r
     public void checkRuleFromEngine_success() throws Exception {\r
         EasyMock.expect(\r
-                engineServiceMock.check(EasyMock.anyObject(CorrelationCheckRule4Engine.class)))\r
+                engineServiceMock.check(EasyMock.anyObject(CorrelationCheckRule4Engine.class),\r
+                        EasyMock.anyObject(String.class)))\r
                 .andReturn(httpResponse);\r
         EasyMock.expect(httpResponse.getStatusLine()).andReturn(statusLineMock);\r
         EasyMock.expect(statusLineMock.getStatusCode()).andReturn(200);\r
 \r
         PowerMock.replayAll();\r
 \r
-        boolean result = engineWrapper.checkRuleFromEngine(new CorrelationCheckRule4Engine());\r
+        boolean result = engineWrapper.checkRuleFromEngine(new CorrelationCheckRule4Engine(),"10.96.33.34");\r
 \r
         assertThat(result, equalTo(true));\r
     }\r
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 (file)
index 0000000..24c5f3a
--- /dev/null
@@ -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<String> newList = new ArrayList<>();
+        newList.add("10.96.33.34");
+        newList.add("10.74.65.24");
+
+        List<String> oldList = new ArrayList<>();
+        oldList.add("10.96.33.34");
+        List<String> 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<String> newList = new ArrayList<>();
+        newList.add("10.96.33.34");
+
+        List<String> oldList = new ArrayList<>();
+        oldList.add("10.96.33.34");
+        oldList.add("10.74.65.24");
+        List<String> destoryIp = Whitebox.invokeMethod(ruleAllocation,"destroyCompareIp",newList,oldList);
+
+        PowerMock.verifyAll();
+
+        assertThat(destoryIp.get(0),equalTo("10.74.65.24"));
+    }
+
+}
index d448e40..4b3e60f 100644 (file)
@@ -28,7 +28,6 @@ import org.junit.Before;
 import org.junit.Rule;\r
 import org.junit.Test;\r
 import org.junit.rules.ExpectedException;\r
-import org.junit.runner.RunWith;\r
 import org.onap.holmes.rulemgt.bean.request.CorrelationCheckRule4Engine;\r
 import org.onap.holmes.rulemgt.db.CorrelationRuleDao;\r
 import org.onap.holmes.common.api.entity.CorrelationRule;\r
@@ -43,6 +42,7 @@ import org.onap.holmes.rulemgt.bean.response.RuleAddAndUpdateResponse;
 import org.onap.holmes.rulemgt.bean.response.RuleQueryListResponse;\r
 import org.onap.holmes.rulemgt.bolt.enginebolt.EngineWrapper;\r
 import org.onap.holmes.rulemgt.db.CorrelationRuleQueryDao;\r
+import org.onap.holmes.rulemgt.send.Ip4AddingRule;\r
 import org.powermock.api.easymock.PowerMock;\r
 import org.powermock.modules.junit4.rule.PowerMockRule;\r
 import org.powermock.reflect.Whitebox;\r
@@ -64,6 +64,8 @@ public class RuleMgtWrapperTest {
 \r
     private CorrelationRuleDao correlationRuleDaoMock;\r
 \r
+    private Ip4AddingRule ip4AddingRuleMock;\r
+\r
     private static final String USER_NAME = "admin";\r
 \r
     @Before\r
@@ -75,11 +77,13 @@ public class RuleMgtWrapperTest {
         correlationRuleQueryDaoMock = PowerMock.createMock(CorrelationRuleQueryDao.class);\r
         dbDaoUtilMock = PowerMock.createMock(DbDaoUtil.class);\r
         correlationRuleDaoMock = PowerMock.createMock(CorrelationRuleDao.class);\r
+        ip4AddingRuleMock = PowerMock.createMock(Ip4AddingRule.class);\r
 \r
         Whitebox.setInternalState(ruleMgtWrapper, "daoUtil", dbDaoUtilMock);\r
         Whitebox.setInternalState(ruleMgtWrapper, "correlationRuleQueryDao", correlationRuleQueryDaoMock);\r
         Whitebox.setInternalState(ruleMgtWrapper, "engineWarpper", engineWrapperMock);\r
         Whitebox.setInternalState(ruleMgtWrapper, "correlationRuleDao", correlationRuleDaoMock);\r
+        Whitebox.setInternalState(ruleMgtWrapper,"ip4AddingRule", ip4AddingRuleMock);\r
 \r
         PowerMock.resetAll();\r
     }\r
@@ -165,9 +169,12 @@ public class RuleMgtWrapperTest {
         correlationRuleRet.setRid("rule_" + System.currentTimeMillis());\r
 \r
         EasyMock.expect(correlationRuleDaoMock.queryRuleByRuleName(ruleName)).andReturn(null);\r
-        EasyMock.expect(engineWrapperMock.checkRuleFromEngine(EasyMock.anyObject(CorrelationCheckRule4Engine.class)))\r
+        EasyMock.expect(ip4AddingRuleMock.getEngineIp4AddRule()).andReturn("10.96.33.34");\r
+        EasyMock.expect(engineWrapperMock.checkRuleFromEngine(EasyMock.anyObject(CorrelationCheckRule4Engine.class)\r
+                , EasyMock.anyObject(String.class)))\r
                 .andReturn(true);\r
-        EasyMock.expect(engineWrapperMock.deployEngine(EasyMock.anyObject(CorrelationDeployRule4Engine.class)))\r
+        EasyMock.expect(engineWrapperMock.deployEngine(EasyMock.anyObject(CorrelationDeployRule4Engine.class)\r
+                , EasyMock.anyObject(String.class)))\r
                 .andReturn("package-001");\r
         EasyMock.expect(correlationRuleDaoMock.saveRule(EasyMock.anyObject(CorrelationRule.class)))\r
                 .andReturn(correlationRuleRet);\r
@@ -198,15 +205,18 @@ public class RuleMgtWrapperTest {
         oldCorrelationRule.setPackageName("testName");\r
         oldCorrelationRule.setEnabled(1);\r
         oldCorrelationRule.setClosedControlLoopName("cl-name");\r
+        oldCorrelationRule.setEngineInstance("10.96.33.34");\r
         RuleUpdateRequest ruleUpdateRequest = createRuleUpdateRequest("rule_1", "cl-name", "des2", "contetnt2", 1);\r
 \r
         EasyMock.expect(correlationRuleDaoMock.queryRuleByRid("rule_1")).andReturn(oldCorrelationRule);\r
-        EasyMock.expect(engineWrapperMock.deleteRuleFromEngine("testName")).andReturn(true);\r
+        EasyMock.expect(engineWrapperMock.deleteRuleFromEngine("testName", "10.96.33.34")).andReturn(true);\r
         correlationRuleDaoMock.updateRule(EasyMock.anyObject(CorrelationRule.class));\r
         EasyMock.expectLastCall();\r
-        EasyMock.expect(engineWrapperMock.checkRuleFromEngine(EasyMock.anyObject(CorrelationCheckRule4Engine.class)))\r
+        EasyMock.expect(engineWrapperMock.checkRuleFromEngine(EasyMock.anyObject(CorrelationCheckRule4Engine.class)\r
+                , EasyMock.anyObject(String.class)))\r
                 .andReturn(true);\r
-        EasyMock.expect(engineWrapperMock.deployEngine(EasyMock.anyObject(CorrelationDeployRule4Engine.class)))\r
+        EasyMock.expect(engineWrapperMock.deployEngine(EasyMock.anyObject(CorrelationDeployRule4Engine.class)\r
+                , EasyMock.anyObject(String.class)))\r
                 .andReturn("packageName1");\r
         PowerMock.replayAll();\r
 \r
@@ -288,7 +298,8 @@ public class RuleMgtWrapperTest {
         correlationRule.setEnabled(1);\r
         EasyMock.expect(correlationRuleDaoMock.queryRuleByRid(ruleDeleteRequest.getRuleId()))\r
                 .andReturn(correlationRule);\r
-        EasyMock.expect(engineWrapperMock.deleteRuleFromEngine(EasyMock.anyObject(String.class))).andReturn(true);\r
+        EasyMock.expect(engineWrapperMock.deleteRuleFromEngine(EasyMock.anyObject(String.class)\r
+                , EasyMock.anyObject(String.class))).andReturn(true);\r
         correlationRuleDaoMock.deleteRule(EasyMock.anyObject(CorrelationRule.class));\r
         EasyMock.expectLastCall();\r
         PowerMock.replayAll();\r
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 (file)
index 0000000..b07efe9
--- /dev/null
@@ -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<CorrelationRule>());
+        PowerMock.replayAll();
+        List<CorrelationRule> correlationRules = ruleQueryWrapper.queryRuleByEnable(enable);
+        PowerMock.verifyAll();
+        Assert.assertThat(correlationRules, IsNull.<List<CorrelationRule>>notNullValue());
+    }
+
+    @Test
+    public void queryRuleByEngineInstance() throws Exception{
+        String engineInstance = "10.96.33.34";
+        EasyMock.expect(ruleQueryWrapper.queryRuleByEngineInstance(EasyMock.anyObject(String.class))).andReturn(new ArrayList<CorrelationRule>());
+        PowerMock.replayAll();
+        List<CorrelationRule> correlationRules = ruleQueryWrapper.queryRuleByEngineInstance(engineInstance);
+        PowerMock.verifyAll();
+        Assert.assertThat(correlationRules, IsNull.<List<CorrelationRule>>notNullValue());
+    }
+}