1 package org.apache.helix.alerts;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.util.HashMap;
23 import java.util.Map;
24
25 import org.apache.helix.HelixException;
26 import org.apache.helix.manager.zk.DefaultParticipantErrorMessageHandlerFactory.ActionOnError;
27 import org.apache.log4j.Logger;
28
29
30 public class AlertParser {
31 private static Logger logger = Logger.getLogger(AlertParser.class);
32
33 public static final String EXPRESSION_NAME = "EXP";
34 public static final String COMPARATOR_NAME = "CMP";
35 public static final String CONSTANT_NAME = "CON";
36 public static final String ACTION_NAME = "ACTION";
37
38 static Map<String, AlertComparator> comparatorMap = new HashMap<String, AlertComparator>();
39
40 static
41 {
42
43 addComparatorEntry("GREATER", new GreaterAlertComparator());
44 }
45
46 private static void addComparatorEntry(String label, AlertComparator comp)
47 {
48 if (!comparatorMap.containsKey(label))
49 {
50 comparatorMap.put(label, comp);
51 }
52 logger.info("Adding comparator: "+comp);
53 }
54
55 public static AlertComparator getComparator(String compName)
56 {
57 compName = compName.replaceAll("\\s+", "");
58 if (!comparatorMap.containsKey(compName)) {
59 throw new HelixException("Comparator type <"+compName+"> unknown");
60 }
61 return comparatorMap.get(compName);
62 }
63
64 public static String getComponent(String component, String alert) throws HelixException
65 {
66
67 int expStartPos = alert.indexOf(component);
68 if (expStartPos < 0) {
69 throw new HelixException(alert+" does not contain component "+component);
70 }
71 expStartPos += (component.length()+1);
72 int expEndPos = expStartPos;
73 int openParenCount = 1;
74 while (openParenCount > 0) {
75 if (alert.charAt(expEndPos) == '(') {
76 openParenCount++;
77 }
78 else if (alert.charAt(expEndPos) == ')') {
79 openParenCount--;
80 }
81 expEndPos++;
82 }
83 if (openParenCount != 0) {
84 throw new HelixException(alert+" does not contain valid "+component+" component, " +
85 "parentheses do not close");
86 }
87
88 return alert.substring(expStartPos, expEndPos-1);
89 }
90
91 public static boolean validateAlert(String alert) throws HelixException
92 {
93
94 alert = alert.replaceAll("\\s+", "");
95 String exp = getComponent(EXPRESSION_NAME, alert);
96 String cmp = getComponent(COMPARATOR_NAME, alert);
97 String val = getComponent(CONSTANT_NAME, alert);
98 logger.debug("exp: "+exp);
99 logger.debug("cmp: "+cmp);
100 logger.debug("val: "+val);
101
102
103 ExpressionParser.validateExpression(exp);
104
105
106 if (!comparatorMap.containsKey(cmp.toUpperCase())) {
107 throw new HelixException("Unknown comparator type "+cmp);
108 }
109 String actionValue = null;
110 try
111 {
112 actionValue = AlertParser.getComponent(AlertParser.ACTION_NAME, alert);
113 }
114 catch(Exception e)
115 {
116 logger.info("No action specified in " + alert);
117 }
118
119 if(actionValue != null)
120 {
121 validateActionValue(actionValue);
122 }
123
124
125
126
127
128
129
130
131
132 return false;
133 }
134
135 public static void validateActionValue(String actionValue)
136 {
137 try
138 {
139 ActionOnError actionVal = ActionOnError.valueOf(actionValue);
140 }
141 catch(Exception e)
142 {
143 String validActions = "";
144 for (ActionOnError action : ActionOnError.values())
145 {
146 validActions = validActions + action + " ";
147 }
148 throw new HelixException("Unknown cmd type " + actionValue + ", valid types : " + validActions);
149 }
150 }
151 }