Search in sources :

Example 6 with Expression

use of org.opennms.netmgt.config.threshd.Expression in project opennms by OpenNMS.

the class ExpressionConfigWrapperTest method testHandleInvalidDsNames.

/* See NMS-5019 */
@Test
public void testHandleInvalidDsNames() throws Exception {
    Expression exp = new Expression();
    exp.setExpression("datasources['ns-dskTotal'] - datasources['ns-dskUsed']");
    ExpressionConfigWrapper wrapper = new ExpressionConfigWrapper(exp);
    Assert.assertEquals(2, wrapper.getRequiredDatasources().size());
    Map<String, Double> values = new HashMap<String, Double>();
    values.put("ns-dskTotal", 100.0);
    values.put("ns-dskUsed", 40.0);
    Assert.assertEquals(60.0, wrapper.evaluate(values), 0.0);
}
Also used : Expression(org.opennms.netmgt.config.threshd.Expression) HashMap(java.util.HashMap) Test(org.junit.Test)

Example 7 with Expression

use of org.opennms.netmgt.config.threshd.Expression in project opennms by OpenNMS.

the class ExpressionConfigWrapperTest method setUp.

@Before
public void setUp() throws Exception {
    MockLogAppender.setupLogging(true, "TRACE");
    Expression exp = new Expression();
    exp.setExpression(FORMULA);
    wrapper = new ExpressionConfigWrapper(exp);
    Assert.assertEquals(4, wrapper.getRequiredDatasources().size());
}
Also used : Expression(org.opennms.netmgt.config.threshd.Expression) Before(org.junit.Before)

Example 8 with Expression

use of org.opennms.netmgt.config.threshd.Expression in project opennms by OpenNMS.

the class ExpressionConfigWrapperTest method testComplexExpression.

/* See NMS-5014 */
@Test
public void testComplexExpression() throws Exception {
    Expression exp = new Expression();
    exp.setExpression("jnxOperatingState == 2.0 || jnxOperatingState == 3.0 || jnxOperatingState == 7.0 ? 1.0 : 0.0");
    ExpressionConfigWrapper wrapper = new ExpressionConfigWrapper(exp);
    Assert.assertEquals(1, wrapper.getRequiredDatasources().size());
    Map<String, Double> values = new HashMap<String, Double>();
    values.put("jnxOperatingState", 1.0);
    Assert.assertEquals(0.0, wrapper.evaluate(values), 0.0);
    values.put("jnxOperatingState", 2.0);
    Assert.assertEquals(1.0, wrapper.evaluate(values), 0.0);
    values.put("jnxOperatingState", 3.0);
    Assert.assertEquals(1.0, wrapper.evaluate(values), 0.0);
    values.put("jnxOperatingState", 4.0);
    Assert.assertEquals(0.0, wrapper.evaluate(values), 0.0);
    values.put("jnxOperatingState", 5.0);
    Assert.assertEquals(0.0, wrapper.evaluate(values), 0.0);
    values.put("jnxOperatingState", 6.0);
    Assert.assertEquals(0.0, wrapper.evaluate(values), 0.0);
    values.put("jnxOperatingState", 7.0);
    Assert.assertEquals(1.0, wrapper.evaluate(values), 0.0);
}
Also used : Expression(org.opennms.netmgt.config.threshd.Expression) HashMap(java.util.HashMap) Test(org.junit.Test)

Example 9 with Expression

use of org.opennms.netmgt.config.threshd.Expression in project opennms by OpenNMS.

the class ThresholdExpressionTestCase method setUp.

@Override
public void setUp() {
    expression = new Expression();
    expression.setType(ThresholdType.HIGH);
    expression.setDsType("node");
    expression.setValue(99.0);
    expression.setRearm(0.5);
    expression.setTrigger(1);
}
Also used : Expression(org.opennms.netmgt.config.threshd.Expression)

Example 10 with Expression

use of org.opennms.netmgt.config.threshd.Expression in project opennms by OpenNMS.

the class ThresholdController method gotoNewExpression.

private ModelAndView gotoNewExpression(String groupName) {
    ThresholdingConfigFactory configFactory = ThresholdingConfigFactory.getInstance();
    final Group group = configFactory.getGroup(groupName);
    final List<Expression> expressions = group.getExpressions();
    // We're assuming that adding a expression puts it at the end of the current list (i.e. that the Group implementation
    // uses a simple List structure, probably ArrayList).  We can be a bit cleverer later on and check though, so we should
    int expressionIndex = expressions.size();
    // Check if last expression has expression def. If not, we assume that is a new definition (not saved yet on thresholds.xml)
    Expression expression = null;
    if (expressionIndex > 0) {
        expression = expressions.get(expressionIndex - 1);
        if (expression.getExpression() == null || expression.getExpression().equals("")) {
            expressionIndex--;
        } else {
            expression = null;
        }
    }
    // create a new expression object
    if (expression == null) {
        expression = new Expression();
        // Set the two default values which need to be set for the UI to work properly
        expression.setDsType("node");
        expression.setType(ThresholdType.HIGH);
        // Default to 1 - 0 will give an error, so we may as well be helpful
        expression.setTrigger(1);
        group.addExpression(expression);
    }
    // Double check the guess index, just in case:
    if (expression != expressions.get(expressionIndex)) {
        // Ok, our guesses on indexing were completely wrong.  Failover and check each threshold in the group
        for (int i = 0; i < expressions.size(); i++) {
            if (expression == expressions.get(i)) {
                expressionIndex = i;
                // out of the for loop
                break;
            }
        }
    }
    ModelAndView modelAndView;
    modelAndView = new ModelAndView("admin/thresholds/editExpression");
    modelAndView.addObject("expression", expression);
    modelAndView.addObject("expressionIndex", expressionIndex);
    modelAndView.addObject("groupName", groupName);
    modelAndView.addObject("isNew", true);
    addStandardEditingBits(modelAndView);
    return modelAndView;
}
Also used : Group(org.opennms.netmgt.config.threshd.Group) Expression(org.opennms.netmgt.config.threshd.Expression) ModelAndView(org.springframework.web.servlet.ModelAndView) ThresholdingConfigFactory(org.opennms.netmgt.config.ThresholdingConfigFactory)

Aggregations

Expression (org.opennms.netmgt.config.threshd.Expression)10 HashMap (java.util.HashMap)4 Test (org.junit.Test)4 ModelAndView (org.springframework.web.servlet.ModelAndView)4 ThresholdingConfigFactory (org.opennms.netmgt.config.ThresholdingConfigFactory)3 ServletException (javax.servlet.ServletException)2 Group (org.opennms.netmgt.config.threshd.Group)2 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Before (org.junit.Before)1 ResourceFilter (org.opennms.netmgt.config.threshd.ResourceFilter)1 Threshold (org.opennms.netmgt.config.threshd.Threshold)1