X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=rulemgt%2Fsrc%2Fmain%2Fjava%2Forg%2Fonap%2Fholmes%2Frulemgt%2Fsend%2FRuleAllocation.java;h=75f0a08896f28c739b09ef350fbf963e2c68e089;hb=82d2318187ea739a0b14442cbd82a3a7f6a419a7;hp=e69be5199531a6d8d338db24a9e1c12ec99ee3b0;hpb=adf983c3fa5c0be8fe05d349190b149da1effb2e;p=holmes%2Frule-management.git
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
index e69be51..75f0a08 100644
--- a/rulemgt/src/main/java/org/onap/holmes/rulemgt/send/RuleAllocation.java
+++ b/rulemgt/src/main/java/org/onap/holmes/rulemgt/send/RuleAllocation.java
@@ -1,12 +1,12 @@
/**
* 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
- *
+ *
+ * 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.
@@ -17,8 +17,10 @@
package org.onap.holmes.rulemgt.send;
import lombok.extern.slf4j.Slf4j;
+import org.glassfish.hk2.api.ServiceLocator;
import org.jvnet.hk2.annotations.Service;
import org.onap.holmes.common.api.entity.CorrelationRule;
+import org.onap.holmes.common.dropwizard.ioc.utils.ServiceLocatorHolder;
import org.onap.holmes.common.exception.CorrelationException;
import org.onap.holmes.common.utils.DbDaoUtil;
import org.onap.holmes.rulemgt.bolt.enginebolt.EngineWrapper;
@@ -32,56 +34,62 @@ 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{
+ public RuleAllocation() {
+ ServiceLocator locator = ServiceLocatorHolder.getLocator();
+ ruleMgtWrapper = locator.getService(RuleMgtWrapper.class);
+ ruleQueryWrapper = locator.getService(RuleQueryWrapper.class);
+ engineWrapper = locator.getService(EngineWrapper.class);
+ engineIpList = locator.getService(EngineIpList.class);
+ daoUtil = locator.getService(DbDaoUtil.class);
+
+ initDaoUtilAndEngineIp();
+ }
+
+ private void initDaoUtilAndEngineIp() {
correlationRuleDao = daoUtil.getJdbiDaoByOnDemand(CorrelationRuleDao.class);
- temIpList = engineIpList.getServiceCount();
+ try {
+ temIpList = engineIpList.getServiceCount();
+
+ } catch (Exception e) {
+ log.warn("Failed to get the number of engine instances.", e);
+ }
}
- public void judgeAndAllocateRule(List ipList)throws Exception{
- if(ipList != null) {
+ public void judgeAndAllocateRule(List ipList) throws Exception {
+ if (ipList != null) {
engineService = ipList;
serviceCount = ipList.size();
}
- if(temIpList.size() < serviceCount){
+ if (temIpList.size() < serviceCount) {
//extend
List deleteRule = calculateRule(temIpList);
- List allocateRule = calculateRule(temIpList);
- List extendIp = extendCompareIp(engineService,temIpList);
- AllocateService(extendIp,allocateRule);
- deleteRuleFromFormerEngine(deleteRule,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) {
+ } else if (temIpList.size() == serviceCount) {
temIpList = engineService;
return;
}
@@ -91,18 +99,18 @@ public class RuleAllocation {
// When the engine is expanding, the rules that need to be allocated are calculated.
- private List calculateRule(List oldIpList) throws Exception{
+ private List calculateRule(List oldIpList) throws Exception {
allRules = ruleQueryWrapper.queryRuleByEnable(ENABLE);
- if(allRules != null) {
+ if (allRules != null) {
ruleCount = allRules.size();
}
int count = ruleCount / serviceCount;
int remainder = ruleCount % serviceCount;
List subRule = new ArrayList<>();
- for(String ip : oldIpList) {
+ for (String ip : oldIpList) {
List rules = ruleQueryWrapper.queryRuleByEngineInstance(ip);
- List tem = rules.subList(count + (remainder-- / oldIpList.size()),rules.size());
+ List tem = rules.subList(count + (remainder-- / oldIpList.size()), rules.size());
subRule.addAll(tem);
}
return subRule;
@@ -111,24 +119,24 @@ public class RuleAllocation {
//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) {
+ 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());
+ } catch (CorrelationException e) {
+ log.error("method relocateRuleAfterDestroy get data from DB failed !", e);
}
return rules;
}
//Extended IP
- private List extendCompareIp(List newList, List oldList){
+ private List extendCompareIp(List newList, List oldList) {
List extendIpList = new ArrayList<>();
- for( String ip :newList) {
- if(! oldList.contains(ip)) {
+ for (String ip : newList) {
+ if (!oldList.contains(ip)) {
extendIpList.add(ip);
}
}
@@ -138,8 +146,8 @@ public class RuleAllocation {
//Destroyed IP
private List destroyCompareIp(List newList, List oldList) {
List destroyIpList = new ArrayList<>();
- for(String ip : oldList) {
- if(!newList.contains(ip)) {
+ for (String ip : oldList) {
+ if (!newList.contains(ip)) {
destroyIpList.add(ip);
}
}
@@ -149,52 +157,51 @@ public class RuleAllocation {
//Residual IP after destruction
private List restIp(List destroyIp) {
List restIpList = new ArrayList<>();
- for(String ip : engineService) {
- if(!destroyIp.contains(ip)) {
+ for (String ip : engineService) {
+ if (!destroyIp.contains(ip)) {
restIpList.add(ip);
}
}
return restIpList;
}
- public void AllocateService(List extendIpList, List subList) throws Exception{
+ public void AllocateService(List extendIpList, List subList) throws Exception {
List needIpList = getSortIp(extendIpList);
- for(int i=0,j=0;j < subList.size();i++,j++ ){
+ 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);
+ 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();
+ private List getSortIp(List ipList) {
+ List ipRuleList = new ArrayList<>();
+ HashMap hashMap = new HashMap();
- try{
- for(String ip : ipList){
+ try {
+ for (String ip : ipList) {
ipRuleList = ruleQueryWrapper.queryRuleByEngineInstance(ip);
- if(ipRuleList != null) {
+ if (ipRuleList != null) {
hashMap.put(ip, String.valueOf(ipRuleList.size()));
}
}
- }catch (Exception e){
- log.error("getEngineIp4AddRule failed !" + e.getMessage());
+ } catch (Exception e) {
+ log.error("getEngineIp4AddRule failed !", e);
}
List> list_Data = new ArrayList>(hashMap.entrySet());
Collections.sort(list_Data, new Comparator>() {
- public int compare(Map.Entry o1, Map.Entry o2)
- {
+ 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) {
+ for (Map.Entry map : list_Data) {
String key = map.getKey();
needList.add(key);
}
@@ -202,25 +209,25 @@ public class RuleAllocation {
}
private void allocateDeployRule(CorrelationRule rule, String ip) throws CorrelationException {
- try{
- ruleMgtWrapper.deployRule2Engine(rule,ip);
+ try {
+ ruleMgtWrapper.deployRule2Engine(rule, ip);
correlationRuleDao.updateRule(rule);
- }catch (CorrelationException e){
+ } 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);
+ 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());
+ } catch (CorrelationException e) {
+ log.error("When the engine is extended, deleting rule failed", e);
}
}