We are using MVEL to evaluate the expression by passing the map in the context object. The map contains SNMP trap information such as OID and its values. eg sample Map contains following Keys and values.
Map<String,String> trapMap = new HashMap();
trapMap.put("1.3.6.1.4.1.9.9.311.1.1.2.1.3", "(7362915) 20:27:09.15");
trapMap.put("1.3.6.1.4.1.9.9.311.1.1.2.1.2", "2.2");
trapMap.put("1.3.6.1.4.1.9.9.311.1.1.2.1.19", "0");
trapMap.put("1.3.6.1.4.1.9.9.311.1.1.2.1.16", "SIMPLE TRAP --Port Down due to Admin Status down");
trapMap.put("errorStatus", "0");
trapMap.put("IPAddress", "10.127.34.219");
When we evaluate the expression using MVEL.eval() it either fails or return False. Following is the MVEL expression used and its result.
System.out.println("----------########### = "+(MVEL.eval("1.3.6.1.4.1.9.9.311.1.1.2.1.19 == '0'", trapMap)));
//Throws error as
//Exception in thread "main" [Error: invalid number literal: 1.3.6.1.4.1.9.9.311.1.1.2.1.19]
// [Near : {... 1.3.6.1.4.1.9.9.311.1.1.2.1.19 == '0 ....}]
System.out.println("----------########### = "+(MVEL.eval("\"1.3.6.1.4.1.9.9.311.1.1.2.1.19\" == '0'", trapMap)));
//Enclosed trap OID in double quotes and compared with String value then it returns false
System.out.println("----------########### = "+(MVEL.eval("\"1.3.6.1.4.1.9.9.311.1.1.2.1.19\" == 0", trapMap)));
//Enclosed trap OID in double quotes and compared with number then it returns false
Our Map will always contain such OID and values and we want to validate their values using MVEL. Based on this, we need to know
DOT(.)
will create a problem in above expression. MVEL
internally calls getter
after each .
property
.
we can replace .
with _
operator. Also need to add _
in the starting.
public static void main(String args[]) throws Exception {
String s = "1.3.6.1.4.1.9.9.311.1.1.2.1.19 == 0";
Map<String, String> trapMap = new HashMap();
trapMap.put(convertDot("1.3.6.1.4.1.9.9.311.1.1.2.1.3"), "(7362915) 20:27:09.15");
trapMap.put(convertDot("1.3.6.1.4.1.9.9.311.1.1.2.1.2"), "2.2");
trapMap.put(convertDot("1.3.6.1.4.1.9.9.311.1.1.2.1.19"), "0");
trapMap.put(convertDot("1.3.6.1.4.1.9.9.311.1.1.2.1.16"), "SIMPLE TRAP --Port Down due to Admin Status down");
trapMap.put("errorStatus", "0");
trapMap.put("IPAddress", "10.127.34.219");
System.out.println(MVEL.eval(convertDot(s), trapMap));
}
public static String convertDot(String input) {
input = "_" + input.replaceAll("\\.", "_");
return input;
}
output
true
If you are using Java Map, you can implicitly call the get method. The below code will evaluate the correct expression.
System.out.println("----------########### = "+(MVEL.eval("get(\"1.3.6.1.4.1.9.9.311.1.1.2.1.19\") == '0'", trapMap)));
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.