Search in sources :

Example 86 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 87 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 88 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 89 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)

Example 90 with ExpressionParser

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

the class PersistRegexSelectorStrategyTest method testSpringEl.

@Test
public void testSpringEl() throws Exception {
    ExpressionParser parser = new SpelExpressionParser();
    Expression exp = parser.parseExpression("#name matches '^Alejandro.*'");
    StandardEvaluationContext context = new StandardEvaluationContext();
    context.setVariable("name", "Alejandro Galue");
    boolean result = (Boolean) exp.getValue(context, Boolean.class);
    Assert.assertTrue(result);
}
Also used : SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) Expression(org.springframework.expression.Expression) ExpressionParser(org.springframework.expression.ExpressionParser) SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) Test(org.junit.Test)

Aggregations

ExpressionParser (org.springframework.expression.ExpressionParser)90 SpelExpressionParser (org.springframework.expression.spel.standard.SpelExpressionParser)87 StandardEvaluationContext (org.springframework.expression.spel.support.StandardEvaluationContext)79 Test (org.junit.Test)76 Expression (org.springframework.expression.Expression)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