Switched from Dropwizard to Springboot
[holmes/engine-management.git] / engine-d / src / main / java / org / onap / holmes / engine / utils / AlarmUtil.java
1 /**
2  * Copyright 2017 ZTE Corporation.
3  * <p>
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  * <p>
8  * http://www.apache.org/licenses/LICENSE-2.0
9  * <p>
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 package org.onap.holmes.engine.utils;
17
18 import org.onap.holmes.common.api.stat.Alarm;
19
20 import java.util.HashMap;
21 import java.util.Map;
22
23 public class AlarmUtil {
24
25     private final static AlarmUtil alarmUtil = new AlarmUtil();
26     /**
27      * Map<ruleId, <ProbableCause-EquipType, priority>>
28      */
29     private final Map<String, Map<String, Integer>> rootPriorityMap =
30             new HashMap<String, Map<String, Integer>>();
31     /**
32      * Map<rule, ProbableCause+EquipType+priority>
33      */
34     private final Map<String, String> saveRuleMsg = new HashMap<String, String>();
35
36     private AlarmUtil() {
37     }
38
39     public static AlarmUtil getInstance() {
40         return alarmUtil;
41     }
42
43     public boolean equipTypeFilter(String probableCauseStr, String equipType, Alarm alarm) {
44         if ("null".equals(probableCauseStr)) {
45             return true;
46         }
47         String[] equipTypes = equipType.replace(" ", "").split(",");
48         String[] probableCauseStrs = probableCauseStr.replace(" ", "").split(",");
49         for (int i = 0; i < probableCauseStrs.length; i++) {
50             if (alarm.getProbableCause() == Long.parseLong(probableCauseStrs[i])
51                     && alarm.getEquipType().equals(equipTypes[i])) {
52                 return true;
53             }
54         }
55         return false;
56     }
57
58     public Integer getPriority(String ruleId, String probableCauseStr, String rootAlarmFeatureStr,
59                                String equipTypeStr, Alarm alarm) {
60         if (rootPriorityMap.containsKey(ruleId)) {
61             if (!saveRuleMsg.get(ruleId)
62                     .equals(probableCauseStr + equipTypeStr + rootAlarmFeatureStr)) {
63                 setPriority(ruleId, probableCauseStr, rootAlarmFeatureStr, equipTypeStr);
64             }
65         } else {
66             setPriority(ruleId, probableCauseStr, rootAlarmFeatureStr, equipTypeStr);
67         }
68
69         Integer priority =
70                 rootPriorityMap.get(ruleId).get(alarm.getProbableCause() + "-" + alarm.getEquipType());
71         if (priority == null) {
72             priority = 0;
73         }
74         return priority;
75     }
76
77     private void setPriority(String ruleId, String probableCauseStr, String rootAlarmFeatureStr,
78                              String equipTypeStr) {
79         saveRuleMsg.put(ruleId, probableCauseStr + equipTypeStr + rootAlarmFeatureStr);
80
81         Map<String, Integer> map = new HashMap<String, Integer>();
82         String[] probableCauseStrs = probableCauseStr.replace(" ", "").split(",");
83         String[] rootAlarmFeatureStrs = rootAlarmFeatureStr.replace(" ", "").split(",");
84         String[] equipTypes = equipTypeStr.replace(" ", "").split(",");
85         for (int i = 0; i < rootAlarmFeatureStrs.length; i++) {
86             map.put(probableCauseStrs[i] + "-" + equipTypes[i],
87                     Integer.parseInt(rootAlarmFeatureStrs[i]));
88         }
89
90         rootPriorityMap.put(ruleId, map);
91     }
92 }