summaryrefslogtreecommitdiffstats
path: root/rulemgt/src/main/java/org
diff options
context:
space:
mode:
authorCongcong Peng <peng.congcong@zte.com.cn>2018-03-28 09:42:09 +0800
committerCongcong Peng <peng.congcong@zte.com.cn>2018-03-28 11:07:12 +0800
commit580d4ce637b1c09b3bd2258b0b9c8332b8789bad (patch)
tree2d4952d08007dd32624d2c2c55f8550eac463dd3 /rulemgt/src/main/java/org
parentd165bd9397c9de0d16c5dc580306d6e63e1e1d60 (diff)
Allocate Rule
Issue-ID: HOLMES-106 Change-Id: I50937669cf26ead5abb8d2ef9bd20bc61095ac1b Signed-off-by: Congcong Peng <peng.congcong@zte.com.cn>
Diffstat (limited to 'rulemgt/src/main/java/org')
-rw-r--r--rulemgt/src/main/java/org/onap/holmes/rulemgt/bolt/enginebolt/EngineService.java15
-rw-r--r--rulemgt/src/main/java/org/onap/holmes/rulemgt/bolt/enginebolt/EngineWrapper.java12
-rw-r--r--rulemgt/src/main/java/org/onap/holmes/rulemgt/db/CorrelationRuleDao.java19
-rw-r--r--rulemgt/src/main/java/org/onap/holmes/rulemgt/db/CorrelationRuleQueryDao.java1
-rw-r--r--rulemgt/src/main/java/org/onap/holmes/rulemgt/msb/EngineIpList.java67
-rw-r--r--rulemgt/src/main/java/org/onap/holmes/rulemgt/msb/MsbQuery.java79
-rw-r--r--rulemgt/src/main/java/org/onap/holmes/rulemgt/send/Ip4AddingRule.java66
-rw-r--r--rulemgt/src/main/java/org/onap/holmes/rulemgt/send/RuleAllocation.java228
-rw-r--r--rulemgt/src/main/java/org/onap/holmes/rulemgt/wrapper/RuleMgtWrapper.java37
-rw-r--r--rulemgt/src/main/java/org/onap/holmes/rulemgt/wrapper/RuleQueryWrapper.java52
10 files changed, 553 insertions, 23 deletions
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<CorrelationRule> queryRuleByEnable(@Bind("enable") int enable);
+
+ @SqlQuery("SELECT * FROM APLUS_RULE WHERE engineinstance=:engineinstance")
+ public abstract List<CorrelationRule> queryRuleByEngineInstance(@Bind("engineinstance") String engineinstance);
+
+ public List<CorrelationRule> queryRuleByRuleEngineInstance(String enginetype) {
+ return queryRuleByEngineInstance(enginetype);
+ }
+
+ public List<CorrelationRule> 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<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
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<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
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<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
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<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());
+ }
+
+ }
+
+}
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
@@ -48,6 +48,12 @@ import org.onap.holmes.rulemgt.db.CorrelationRuleQueryDao;
public class RuleMgtWrapper {
@Inject
+ private Ip4AddingRule ip4AddingRule;
+
+ @Inject
+ private RuleQueryWrapper ruleQueryWrapper;
+
+ @Inject
private CorrelationRuleQueryDao correlationRuleQueryDao;
@Inject
private EngineWrapper engineWarpper;
@@ -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<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;
+ }
+}