Search in sources :

Example 1 with Expression

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

the class ThresholdController method finishThresholdFilterEdit.

private ModelAndView finishThresholdFilterEdit(HttpServletRequest request, Basethresholddef threshold) throws ServletException {
    boolean isExpression = threshold instanceof Expression;
    int thresholdIndex;
    if (isExpression) {
        thresholdIndex = WebSecurityUtils.safeParseInt(request.getParameter("expressionIndex"));
    } else {
        thresholdIndex = WebSecurityUtils.safeParseInt(request.getParameter("thresholdIndex"));
    }
    ModelAndView modelAndView;
    if (isExpression) {
        modelAndView = new ModelAndView("admin/thresholds/editExpression");
    } else {
        modelAndView = new ModelAndView("admin/thresholds/editThreshold");
    }
    List<ResourceFilter> saved = getFilterList(request, true);
    if (saved == null || saved.size() == 0) {
        saved = new ArrayList<ResourceFilter>(threshold.getResourceFilters());
        setFilterList(request, saved);
    }
    String stringIndex = request.getParameter("filterSelected");
    int filterIndex = stringIndex != null && !stringIndex.equals("") ? WebSecurityUtils.safeParseInt(stringIndex) - 1 : 0;
    /*
         * Save Threshold Filters on HTTP Session in order to restore the original list if user clicks on "Cancel"
         */
    String submitAction = request.getParameter("submitAction");
    if (ADDFILTER_BUTTON_TITLE.equals(submitAction)) {
        String field = request.getParameter("filterField");
        String content = request.getParameter("filterRegexp");
        if (field != null && !field.equals("") && content != null && !content.equals("")) {
            ResourceFilter filter = new ResourceFilter();
            filter.setField(field);
            filter.setContent(content);
            threshold.addResourceFilter(filter);
        }
    } else if (DELETE_BUTTON_TITLE.equals(submitAction)) {
        threshold.getResourceFilters().remove(filterIndex);
    } else if (EDIT_BUTTON_TITLE.equals(submitAction)) {
        modelAndView.addObject("filterSelected", request.getParameter("filterSelected"));
    } else if (UPDATE_BUTTON_TITLE.equals(submitAction)) {
        ResourceFilter filter = (ResourceFilter) threshold.getResourceFilters().get(filterIndex);
        filter.setField(request.getParameter("updateFilterField"));
        filter.setContent(request.getParameter("updateFilterRegexp"));
    } else if (MOVEUP_BUTTON_TITLE.equals(submitAction)) {
        moveThresholdFilter(threshold, filterIndex, filterIndex - 1);
    } else if (MOVEDOWN_BUTTON_TITLE.equals(submitAction)) {
        moveThresholdFilter(threshold, filterIndex, filterIndex + 1);
    }
    commonFinishEdit(request, threshold);
    if (isExpression) {
        ((Expression) threshold).setExpression(request.getParameter("expression"));
    } else {
        ((Threshold) threshold).setDsName(request.getParameter("dsName"));
    }
    String isNew = request.getParameter("isNew");
    if ("true".equals(isNew)) {
        modelAndView.addObject("isNew", true);
    }
    if (isExpression) {
        modelAndView.addObject("expression", threshold);
        modelAndView.addObject("expressionIndex", thresholdIndex);
    } else {
        modelAndView.addObject("threshold", threshold);
        modelAndView.addObject("thresholdIndex", thresholdIndex);
    }
    modelAndView.addObject("groupName", request.getParameter("groupName"));
    addStandardEditingBits(modelAndView);
    return modelAndView;
}
Also used : ResourceFilter(org.opennms.netmgt.config.threshd.ResourceFilter) Expression(org.opennms.netmgt.config.threshd.Expression) ModelAndView(org.springframework.web.servlet.ModelAndView) Threshold(org.opennms.netmgt.config.threshd.Threshold)

Example 2 with Expression

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

the class ThresholdController method gotoEditExpression.

private ModelAndView gotoEditExpression(String expressionIndexString, String groupName) throws ServletException {
    ThresholdingConfigFactory configFactory = ThresholdingConfigFactory.getInstance();
    ModelAndView modelAndView;
    if (expressionIndexString == null) {
        throw new ServletException("expressionIndex parameter required to edit a threshold");
    }
    int expressionIndex = WebSecurityUtils.safeParseInt(expressionIndexString);
    Expression expression = configFactory.getGroup(groupName).getExpressions().get(expressionIndex);
    modelAndView = new ModelAndView("admin/thresholds/editExpression");
    modelAndView.addObject("expression", expression);
    modelAndView.addObject("expressionIndex", expressionIndex);
    modelAndView.addObject("groupName", groupName);
    modelAndView.addObject("isNew", false);
    addStandardEditingBits(modelAndView);
    return modelAndView;
}
Also used : ServletException(javax.servlet.ServletException) Expression(org.opennms.netmgt.config.threshd.Expression) ModelAndView(org.springframework.web.servlet.ModelAndView) ThresholdingConfigFactory(org.opennms.netmgt.config.ThresholdingConfigFactory)

Example 3 with Expression

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

the class ExpressionConfigWrapperTest method testMath.

@Test
public void testMath() throws Exception {
    Expression exp = new Expression();
    exp.setExpression("math.max(data, 5)");
    wrapper = new ExpressionConfigWrapper(exp);
    Map<String, Double> values = new HashMap<String, Double>();
    values.put("data", 10.0);
    Assert.assertTrue(10.0 == wrapper.evaluate(values));
}
Also used : Expression(org.opennms.netmgt.config.threshd.Expression) HashMap(java.util.HashMap) Test(org.junit.Test)

Example 4 with Expression

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

the class ExpressionConfigWrapperTest method testFunctions.

@Test
public void testFunctions() throws Exception {
    Expression exp = new Expression();
    exp.setExpression("math.max((ifInOctets*8/ifSpeed), (ifOutOctets*8/ifSpeed))");
    ExpressionConfigWrapper wrapper = new ExpressionConfigWrapper(exp);
    Assert.assertEquals(3, wrapper.getRequiredDatasources().size());
    Map<String, Double> values = new HashMap<String, Double>();
    values.put("ifInOctets", 100.0);
    values.put("ifOutOctets", 200.0);
    values.put("ifSpeed", 10.0);
    Assert.assertEquals(160.0, wrapper.evaluate(values), 0.0);
}
Also used : Expression(org.opennms.netmgt.config.threshd.Expression) HashMap(java.util.HashMap) Test(org.junit.Test)

Example 5 with Expression

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

the class ThresholdController method finishExpressionEdit.

private ModelAndView finishExpressionEdit(HttpServletRequest request) throws ServletException {
    ThresholdingConfigFactory configFactory = ThresholdingConfigFactory.getInstance();
    ModelAndView modelAndView;
    String groupName = request.getParameter("groupName");
    String submitAction = request.getParameter("submitAction");
    Group group = configFactory.getGroup(groupName);
    String expressionIndexString = request.getParameter("expressionIndex");
    if (expressionIndexString == null) {
        throw new ServletException("expressionIndex parameter required to modify or delete a threshold expression");
    }
    int expressionIndex = WebSecurityUtils.safeParseInt(expressionIndexString);
    Expression expression = group.getExpressions().get(expressionIndex);
    if (SAVE_BUTTON_TITLE.equals(submitAction)) {
        this.commonFinishEdit(request, expression);
        final String expDef = request.getParameter("expression");
        final String filterOperator = request.getParameter("filterOperator");
        if (expDef == null || expDef.equals("")) {
            throw new ServletException("expression content cannot be null or empty string");
        }
        expression.setExpression(expDef);
        expression.setFilterOperator(filterOperator == null ? null : FilterOperator.valueOf(filterOperator.toUpperCase()));
        saveChanges();
    } else if (CANCEL_BUTTON_TITLE.equals(submitAction)) {
        String isNew = request.getParameter("isNew");
        if ("true".equals(isNew)) {
            // It was a new Threshold, but the user hit cancel.  Remove the new threshold from the group
            group.removeExpression(expression);
        } else {
            List<ResourceFilter> filters = getFilterList(request, false);
            if (filters != null) {
                expression.setResourceFilters(filters);
            }
        }
    } else {
        return finishThresholdFilterEdit(request, expression);
    }
    // Remove Filters from Session
    setFilterList(request, null);
    // and got back to the editGroup page
    modelAndView = new ModelAndView("admin/thresholds/editGroup");
    modelAndView.addObject("group", configFactory.getGroup(groupName));
    return modelAndView;
}
Also used : ServletException(javax.servlet.ServletException) Group(org.opennms.netmgt.config.threshd.Group) Expression(org.opennms.netmgt.config.threshd.Expression) ModelAndView(org.springframework.web.servlet.ModelAndView) ArrayList(java.util.ArrayList) List(java.util.List) 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