4a2dba3cc1825ce7b881e0dc8b1ec5594eec1b70
[holmes/rule-management.git] / rulemgt / src / main / java / org / onap / holmes / rulemgt / send / Ip4AddingRule.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.rulemgt.wrapper.RuleQueryWrapper;
23
24 import javax.inject.Inject;
25 import java.util.*;
26
27 @Service
28 @Slf4j
29 public class Ip4AddingRule {
30
31     @Inject
32     private RuleQueryWrapper ruleQueryWrapper;
33     private List<String> engineService;
34
35     public void setIpList(List<String> ipList){
36         engineService = ipList;
37     }
38
39     public String getEngineIp4AddRule() {
40         List<CorrelationRule> ipRuleList  = new ArrayList<>();
41         LinkedHashMap<String,Integer> linkedHashMap = new LinkedHashMap<>();
42
43         try{
44             for(String ip : engineService){
45                 ipRuleList = ruleQueryWrapper.queryRuleByEngineInstance(ip);
46                 if(ipRuleList != null) {
47                     linkedHashMap.put(ip, ipRuleList.size());
48                 }
49             }
50         }catch (Exception e){
51             log.error("getEngineIp4AddRule failed !" + e.getMessage());
52         }
53
54         //min
55         Collection<Integer> c = linkedHashMap.values();
56         Object[] obj = c.toArray();
57         Arrays.sort(obj);
58         String ip = null;
59         for(String getKey: linkedHashMap.keySet()){
60             if(linkedHashMap.get(getKey).equals(obj[0])){
61                 ip = getKey;
62             }
63         }
64         return ip;
65     }
66 }