e69be5199531a6d8d338db24a9e1c12ec99ee3b0
[holmes/rule-management.git] / rulemgt / src / main / java / org / onap / holmes / rulemgt / send / RuleAllocation.java
1 /**
2  * Copyright 2017 ZTE Corporation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.onap.holmes.rulemgt.send;
18
19 import lombok.extern.slf4j.Slf4j;
20 import org.jvnet.hk2.annotations.Service;
21 import org.onap.holmes.common.api.entity.CorrelationRule;
22 import org.onap.holmes.common.exception.CorrelationException;
23 import org.onap.holmes.common.utils.DbDaoUtil;
24 import org.onap.holmes.rulemgt.bolt.enginebolt.EngineWrapper;
25 import org.onap.holmes.rulemgt.db.CorrelationRuleDao;
26 import org.onap.holmes.rulemgt.msb.EngineIpList;
27 import org.onap.holmes.rulemgt.wrapper.RuleQueryWrapper;
28 import org.onap.holmes.rulemgt.wrapper.RuleMgtWrapper;
29
30 import javax.annotation.PostConstruct;
31 import javax.inject.Inject;
32 import java.util.*;
33
34
35 @Service
36 @Slf4j
37 public class RuleAllocation {
38
39     private final static int ENABLE = 1;
40
41     @Inject
42     private RuleMgtWrapper ruleMgtWrapper;
43     @Inject
44     private RuleQueryWrapper ruleQueryWrapper;
45     @Inject
46     private EngineWrapper engineWrapper;
47     @Inject
48     private EngineIpList engineIpList;
49     @Inject
50     private DbDaoUtil daoUtil;
51
52     private CorrelationRuleDao correlationRuleDao;
53
54     private int ruleCount;
55     private int serviceCount;
56     private List<String> temIpList = new ArrayList<>();
57     private List<String> engineService = new ArrayList<>();
58     private List<CorrelationRule> allRules = new ArrayList<>();
59
60     @PostConstruct
61     public void initDaoUtilAndEngineIp() throws Exception{
62         correlationRuleDao = daoUtil.getJdbiDaoByOnDemand(CorrelationRuleDao.class);
63         temIpList =  engineIpList.getServiceCount();
64     }
65
66     public void judgeAndAllocateRule(List<String> ipList)throws Exception{
67         if(ipList != null) {
68             engineService = ipList;
69             serviceCount = ipList.size();
70         }
71         if(temIpList.size() < serviceCount){
72             //extend
73             List<CorrelationRule> deleteRule = calculateRule(temIpList);
74             List<CorrelationRule> allocateRule =  calculateRule(temIpList);
75             List<String> extendIp = extendCompareIp(engineService,temIpList);
76             AllocateService(extendIp,allocateRule);
77             deleteRuleFromFormerEngine(deleteRule,temIpList);
78
79         } else if (temIpList.size() > serviceCount) {
80             //destroy
81             List<String> destroyIp = destroyCompareIp(engineService, temIpList);
82             AllocateService(restIp(destroyIp), relocateRuleAfterDestroy(destroyIp));
83
84         } else if(temIpList.size() == serviceCount) {
85             temIpList = engineService;
86             return;
87         }
88         temIpList = engineService;
89
90     }
91
92
93     // When the engine is expanding, the rules that need to be allocated are calculated.
94     private List<CorrelationRule> calculateRule(List<String> oldIpList) throws Exception{
95         allRules = ruleQueryWrapper.queryRuleByEnable(ENABLE);
96         if(allRules != null) {
97             ruleCount = allRules.size();
98         }
99         int count = ruleCount / serviceCount;
100         int remainder = ruleCount % serviceCount;
101
102         List<CorrelationRule> subRule = new ArrayList<>();
103         for(String ip : oldIpList) {
104             List<CorrelationRule> rules = ruleQueryWrapper.queryRuleByEngineInstance(ip);
105             List<CorrelationRule> tem = rules.subList(count + (remainder-- / oldIpList.size()),rules.size());
106             subRule.addAll(tem);
107         }
108         return subRule;
109     }
110
111     //Rules that need to be allocated after the engine is destroyed
112     private List<CorrelationRule> relocateRuleAfterDestroy(List<String> destroyIpList) throws CorrelationException {
113         List<CorrelationRule> rules = new ArrayList<>();
114         try{
115             if(destroyIpList != null){
116                 for(String ip : destroyIpList) {
117                     rules.addAll(ruleQueryWrapper.queryRuleByEngineInstance(ip));
118                 }
119             }
120         }catch(CorrelationException e) {
121             log.error("method relocateRuleAfterDestroy get data from DB failed !" +e.getMessage());
122         }
123         return rules;
124     }
125
126     //Extended IP
127     private List<String> extendCompareIp(List<String> newList, List<String> oldList){
128         List<String> extendIpList = new ArrayList<>();
129
130         for( String ip :newList) {
131             if(! oldList.contains(ip)) {
132                 extendIpList.add(ip);
133             }
134         }
135         return extendIpList;
136     }
137
138     //Destroyed IP
139     private List<String> destroyCompareIp(List<String> newList, List<String> oldList) {
140         List<String> destroyIpList = new ArrayList<>();
141         for(String ip : oldList) {
142             if(!newList.contains(ip)) {
143                 destroyIpList.add(ip);
144             }
145         }
146         return destroyIpList;
147     }
148
149     //Residual IP after destruction
150     private List<String> restIp(List<String> destroyIp) {
151         List<String> restIpList = new ArrayList<>();
152         for(String ip : engineService) {
153             if(!destroyIp.contains(ip)) {
154                 restIpList.add(ip);
155             }
156         }
157         return restIpList;
158     }
159
160     public void AllocateService(List<String> extendIpList, List<CorrelationRule> subList) throws Exception{
161         List<String> needIpList = getSortIp(extendIpList);
162
163         for(int i=0,j=0;j < subList.size();i++,j++ ){
164             int index = i % needIpList.size();
165             String deployIp = needIpList.get(index);
166             CorrelationRule rule = subList.get(j);
167             rule.setEngineInstance(deployIp);
168             allocateDeployRule(rule,deployIp);
169         }
170     }
171
172     //The IP to be allocated is in ascending order, and the least is circulate.
173     private List<String > getSortIp(List<String> ipList){
174         List<CorrelationRule> ipRuleList  = new ArrayList<>();
175         HashMap<String,String> hashMap = new HashMap();
176
177         try{
178             for(String ip : ipList){
179                 ipRuleList = ruleQueryWrapper.queryRuleByEngineInstance(ip);
180                 if(ipRuleList != null) {
181                     hashMap.put(ip, String.valueOf(ipRuleList.size()));
182                 }
183             }
184         }catch (Exception e){
185             log.error("getEngineIp4AddRule failed !" + e.getMessage());
186         }
187
188         List<Map.Entry<String, String>> list_Data = new ArrayList<Map.Entry<String, String>>(hashMap.entrySet());
189         Collections.sort(list_Data, new Comparator<Map.Entry<String, String>>() {
190             public int compare(Map.Entry<String, String> o1, Map.Entry<String, String> o2)
191             {
192                 return o1.getValue().compareTo(o2.getValue());
193             }
194         });
195
196         List<String> needList = new ArrayList<>();
197         for(Map.Entry<String, String> map: list_Data) {
198             String key = map.getKey();
199             needList.add(key);
200         }
201         return needList;
202     }
203
204     private void allocateDeployRule(CorrelationRule rule, String ip) throws CorrelationException {
205         try{
206             ruleMgtWrapper.deployRule2Engine(rule,ip);
207             correlationRuleDao.updateRule(rule);
208         }catch (CorrelationException e){
209             throw new CorrelationException("allocate Deploy Rule failed", e);
210         }
211     }
212
213     private void deleteRuleFromFormerEngine(List<CorrelationRule> subRule, List<String> oldList) {
214         try{
215             for(String ip : oldList){
216                 for(CorrelationRule rule: subRule) {
217                     if(ip.equals(rule.getEngineInstance())) {
218                         engineWrapper.deleteRuleFromEngine(rule.getPackageName(),ip);
219                     }
220                 }
221             }
222         }catch (CorrelationException e) {
223             log.error("When the engine is extended, deleting rule failed" +e.getMessage());
224         }
225
226     }
227
228 }