Search in sources :

Example 46 with ExpressionParser

use of org.springframework.expression.ExpressionParser in project opennms by OpenNMS.

the class EmailFilter method accepts.

/**
 * Checks if the alarm is accepted by the filter.
 *
 * @param alarm the northbound alarm
 * @return true, if successful
 */
public boolean accepts(NorthboundAlarm alarm) {
    if (!isEnabled()) {
        return false;
    }
    StandardEvaluationContext context = new StandardEvaluationContext(alarm);
    ExpressionParser parser = new SpelExpressionParser();
    Expression exp = parser.parseExpression(getRule());
    boolean passed = false;
    try {
        passed = (Boolean) exp.getValue(context, Boolean.class);
    } catch (Exception e) {
        LOG.warn("accepts: can't evaluate expression {} for alarm {} because: {}", getRule(), alarm.getUei(), e.getMessage());
    }
    LOG.debug("accepts: checking {} ? {}", getRule(), passed);
    return passed;
}
Also used : StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) Expression(org.springframework.expression.Expression) ExpressionParser(org.springframework.expression.ExpressionParser) SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser)

Example 47 with ExpressionParser

use of org.springframework.expression.ExpressionParser in project opennms by OpenNMS.

the class SnmpTrapMapping method accepts.

/**
 * Verifies if the mapping object accepts a given northbound alarm.
 *
 * @param alarm the northbound alarm
 * @return true, if the alarm is accepted
 */
public boolean accepts(NorthboundAlarm alarm) {
    StandardEvaluationContext context = new StandardEvaluationContext(alarm);
    ExpressionParser parser = new SpelExpressionParser();
    Expression exp = parser.parseExpression(getRule());
    boolean passed = false;
    try {
        passed = (Boolean) exp.getValue(context, Boolean.class);
    } catch (Exception e) {
        LOG.warn("mapping accepts: can't evaluate expression {} for alarm {} because: {}", getRule(), alarm.getUei(), e.getMessage());
    }
    LOG.debug("mapping accepts: checking {} ? {}", getRule(), passed);
    return passed;
}
Also used : StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) Expression(org.springframework.expression.Expression) ExpressionParser(org.springframework.expression.ExpressionParser) SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser)

Example 48 with ExpressionParser

use of org.springframework.expression.ExpressionParser in project opennms by OpenNMS.

the class SnmpTrapMappingGroup method accepts.

/**
 * Verifies if the mapping group object accepts a given northbound alarm.
 *
 * @param alarm the northbound alarm
 * @return true, if the alarm is accepted.
 */
public boolean accepts(NorthboundAlarm alarm) {
    StandardEvaluationContext context = new StandardEvaluationContext(alarm);
    ExpressionParser parser = new SpelExpressionParser();
    Expression exp = parser.parseExpression(getRule());
    boolean passed = false;
    try {
        passed = (Boolean) exp.getValue(context, Boolean.class);
        if (passed) {
            boolean mappingAccepted = false;
            for (SnmpTrapMapping mapping : getMappings()) {
                if (mapping.accepts(alarm)) {
                    mappingAccepted = true;
                    break;
                }
            }
            passed = mappingAccepted;
        }
    } catch (Exception e) {
        LOG.warn("mapping group accepts: can't evaluate expression {} for alarm {} because: {}", getRule(), alarm.getUei(), e.getMessage());
    }
    LOG.debug("mapping group accepts: {} ? {}", getRule(), passed);
    return passed;
}
Also used : StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) Expression(org.springframework.expression.Expression) ExpressionParser(org.springframework.expression.ExpressionParser) SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser)

Example 49 with ExpressionParser

use of org.springframework.expression.ExpressionParser in project opennms by OpenNMS.

the class SyslogFilter method passFilter.

/**
 * Pass filter.
 *
 * @param alarm the alarm
 * @return true, if successful
 */
public boolean passFilter(NorthboundAlarm alarm) {
    if (!isEnabled()) {
        return false;
    }
    StandardEvaluationContext context = new StandardEvaluationContext(alarm);
    ExpressionParser parser = new SpelExpressionParser();
    Expression exp = parser.parseExpression(getRule());
    boolean passed = false;
    try {
        passed = (Boolean) exp.getValue(context, Boolean.class);
    } catch (Exception e) {
        LOG.warn("passFilter: can't evaluate expression {} for alarm {} because: {}", getRule(), alarm.getUei(), e.getMessage());
    }
    LOG.debug("passFilter: checking {} ? {}", getRule(), passed);
    return passed;
}
Also used : StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) Expression(org.springframework.expression.Expression) ExpressionParser(org.springframework.expression.ExpressionParser) SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser)

Example 50 with ExpressionParser

use of org.springframework.expression.ExpressionParser in project opennms by OpenNMS.

the class SyslogEventForwarder method passFilter.

/**
 * Pass filter.
 *
 * @param filter the filter
 * @param event the event
 * @return true, if successful
 */
private boolean passFilter(SyslogFilter filter, Event event) {
    StandardEvaluationContext context = new StandardEvaluationContext(event);
    ExpressionParser parser = new SpelExpressionParser();
    Expression exp = parser.parseExpression(filter.getRule());
    boolean passed = false;
    try {
        passed = (Boolean) exp.getValue(context, Boolean.class);
    } catch (Exception e) {
        LOG.warn("passFilter: can't evaluate expression {} for alarm {} because: {}", filter.getRule(), event.getUei(), e.getMessage());
    }
    LOG.debug("passFilter: checking {} ? {}", filter.getRule(), passed);
    return passed;
}
Also used : StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) Expression(org.springframework.expression.Expression) ExpressionParser(org.springframework.expression.ExpressionParser) SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) IOException(java.io.IOException) SyslogRuntimeException(org.graylog2.syslog4j.SyslogRuntimeException)

Aggregations

ExpressionParser (org.springframework.expression.ExpressionParser)92 SpelExpressionParser (org.springframework.expression.spel.standard.SpelExpressionParser)89 StandardEvaluationContext (org.springframework.expression.spel.support.StandardEvaluationContext)81 Expression (org.springframework.expression.Expression)78 Test (org.junit.Test)76 SpelExpression (org.springframework.expression.spel.standard.SpelExpression)51 EvaluationContext (org.springframework.expression.EvaluationContext)10 BigInteger (java.math.BigInteger)6 Map (java.util.Map)5 BigDecimal (java.math.BigDecimal)4 HashMap (java.util.HashMap)4 ArrayList (java.util.ArrayList)3 LinkedHashMap (java.util.LinkedHashMap)3 TreeMap (java.util.TreeMap)3 ParseException (org.springframework.expression.ParseException)3 IOException (java.io.IOException)2 Method (java.lang.reflect.Method)2 List (java.util.List)2 SyslogRuntimeException (org.graylog2.syslog4j.SyslogRuntimeException)2 EvaluationException (org.springframework.expression.EvaluationException)2