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