use of org.opennms.netmgt.config.threshd.ResourceFilter in project opennms by OpenNMS.
the class ThresholdController method moveThresholdFilter.
private void moveThresholdFilter(Basethresholddef threshold, int oldPos, int newPos) {
if (newPos >= 0 && newPos < threshold.getResourceFilters().size()) {
ResourceFilter oldFilter = (ResourceFilter) threshold.getResourceFilters().get(oldPos);
ResourceFilter newFilter = (ResourceFilter) threshold.getResourceFilters().get(newPos);
threshold.getResourceFilters().set(newPos, oldFilter);
threshold.getResourceFilters().set(oldPos, newFilter);
}
}
use of org.opennms.netmgt.config.threshd.ResourceFilter 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;
}
use of org.opennms.netmgt.config.threshd.ResourceFilter in project opennms by OpenNMS.
the class ThresholdingSet method passedThresholdFilters.
/**
* <p>passedThresholdFilters</p>
*
* @param resource a {@link org.opennms.netmgt.threshd.CollectionResourceWrapper} object.
* @param thresholdEntity a {@link org.opennms.netmgt.threshd.ThresholdEntity} object.
* @return a boolean.
*/
protected boolean passedThresholdFilters(CollectionResourceWrapper resource, ThresholdEntity thresholdEntity) {
// Find the filters for threshold definition for selected group/dataSource
final List<ResourceFilter> filters = thresholdEntity.getThresholdConfig().getBasethresholddef().getResourceFilters();
if (filters.size() == 0)
return true;
// Threshold definition with filters must match ThresholdEntity (checking DataSource and ResourceType)
LOG.debug("passedThresholdFilters: applying {} filters to resource {}", filters.size(), resource);
int count = 1;
final FilterOperator operator = thresholdEntity.getThresholdConfig().getBasethresholddef().getFilterOperator();
boolean andResult = true;
for (ResourceFilter f : filters) {
LOG.debug("passedThresholdFilters: filter #{}: field={}, regex='{}'", count, f.getField(), f.getContent().orElse(null));
count++;
// Read Resource Attribute and apply filter rules if attribute is not null
String attr = resource.getFieldValue(f.getField());
if (attr != null) {
try {
final Pattern p = Pattern.compile(f.getContent().orElse(""));
final Matcher m = p.matcher(attr);
boolean pass = m.matches();
LOG.debug("passedThresholdFilters: the value of {} is {}. Pass filter? {}", f.getField(), attr, pass);
if (operator.equals(FilterOperator.OR) && pass) {
return true;
}
if (operator.equals(FilterOperator.AND)) {
andResult = andResult && pass;
if (andResult == false)
return false;
}
} catch (PatternSyntaxException e) {
LOG.warn("passedThresholdFilters: the regular expression {} is invalid: {}", f.getContent().orElse(null), e.getMessage(), e);
return false;
}
} else {
LOG.warn("passedThresholdFilters: can't find value of {} for resource {}", f.getField(), resource);
if (operator.equals(FilterOperator.AND)) {
return false;
}
}
}
if (operator.equals(FilterOperator.AND) && andResult) {
return true;
}
return false;
}
Aggregations