Search in sources :

Example 1 with Event

use of org.opennms.web.event.Event in project opennms by OpenNMS.

the class WebEventRepositoryFilterIT method testAcknowledgeByFilter.

@Test
// Relies on specific IDs so we need a fresh database
@JUnitTemporaryDatabase
public void testAcknowledgeByFilter() {
    AcknowledgedByFilter filter = new AcknowledgedByFilter("TestUser");
    EventCriteria criteria = new EventCriteria(filter);
    Event[] events = m_daoEventRepo.getMatchingEvents(criteria);
    assertEquals(0, events.length);
    m_daoEventRepo.acknowledgeMatchingEvents("TestUser", new Date(), new EventCriteria(new EventIdFilter(1)));
    events = m_daoEventRepo.getMatchingEvents(criteria);
    assertEquals(1, events.length);
    assertEquals("TestUser", events[0].getAcknowledgeUser());
    m_daoEventRepo.unacknowledgeAll();
}
Also used : OnmsEvent(org.opennms.netmgt.model.OnmsEvent) Event(org.opennms.web.event.Event) Date(java.util.Date) Test(org.junit.Test) JUnitTemporaryDatabase(org.opennms.core.test.db.annotations.JUnitTemporaryDatabase)

Example 2 with Event

use of org.opennms.web.event.Event in project opennms by OpenNMS.

the class WebEventRepositoryFilterIT method testAlarmIdFilter.

@Test
// Relies on specific IDs so we need a fresh database
@JUnitTemporaryDatabase
public void testAlarmIdFilter() {
    AlarmIdFilter filter = new AlarmIdFilter(1);
    Event[] events = getMatchingDaoEvents(filter);
    assertEquals(1, events.length);
}
Also used : OnmsEvent(org.opennms.netmgt.model.OnmsEvent) Event(org.opennms.web.event.Event) AlarmIdFilter(org.opennms.web.alarm.filter.AlarmIdFilter) Test(org.junit.Test) JUnitTemporaryDatabase(org.opennms.core.test.db.annotations.JUnitTemporaryDatabase)

Example 3 with Event

use of org.opennms.web.event.Event in project opennms by OpenNMS.

the class NotificationFilterController method handleRequestInternal.

/**
     * {@inheritDoc}
     *
     * Parses the query string to determine what types of notification filters to use
     * (for example, what to filter on or sort by), then does the database query
     * and then forwards the results to a JSP for display.
     */
@Override
protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception {
    String display = request.getParameter("display");
    // handle the style sort parameter
    String sortStyleString = request.getParameter("sortby");
    SortStyle sortStyle = m_defaultSortStyle;
    if (sortStyleString != null) {
        SortStyle temp = SortStyle.getSortStyle(sortStyleString);
        if (temp != null) {
            sortStyle = temp;
        }
    }
    // handle the acknowledgment type parameter
    String ackTypeString = request.getParameter("acktype");
    AcknowledgeType ackType = m_defaultAckType;
    if (ackTypeString != null) {
        AcknowledgeType temp = AcknowledgeType.getAcknowledgeType(ackTypeString);
        if (temp != null) {
            ackType = temp;
        }
    }
    // handle the filter parameters
    String[] filterStrings = request.getParameterValues("filter");
    List<Filter> filterList = new ArrayList<Filter>();
    if (filterStrings != null) {
        for (String filterString : filterStrings) {
            Filter filter = NoticeUtil.getFilter(filterString, getServletContext());
            if (filter != null) {
                filterList.add(filter);
            }
        }
    }
    // Check for a username filter (used on notifications/index.jsp)
    String username = request.getParameter("user");
    if (username != null) {
        Filter filter = NoticeUtil.getFilter("user=" + username, getServletContext());
        if (filter != null) {
            filterList.add(filter);
        }
    }
    // handle the optional limit parameter
    String limitString = request.getParameter("limit");
    int limit = "long".equals(display) ? m_defaultLongLimit : m_defaultShortLimit;
    if (limitString != null) {
        try {
            int newlimit = WebSecurityUtils.safeParseInt(limitString);
            if (newlimit > 0) {
                limit = newlimit;
            }
        } catch (NumberFormatException e) {
        // do nothing, the default is already set
        }
    }
    // handle the optional multiple parameter
    String multipleString = request.getParameter("multiple");
    int multiple = DEFAULT_MULTIPLE;
    if (multipleString != null) {
        try {
            multiple = Math.max(0, WebSecurityUtils.safeParseInt(multipleString));
        } catch (NumberFormatException e) {
        }
    }
    // put the parameters in a convenient struct
    Filter[] filters = filterList.toArray(new Filter[0]);
    NoticeQueryParms parms = new NoticeQueryParms();
    parms.ackType = ackType;
    parms.display = display;
    parms.filters = filterList;
    parms.limit = limit;
    parms.multiple = multiple;
    parms.sortStyle = sortStyle;
    NotificationCriteria queryCriteria = new NotificationCriteria(filters, sortStyle, ackType, limit, limit * multiple);
    NotificationCriteria countCriteria = new NotificationCriteria(ackType, filters);
    Notification[] notices = m_webNotificationRepository.getMatchingNotifications(queryCriteria);
    int noticeCount = m_webNotificationRepository.countMatchingNotifications(countCriteria);
    final Map<Integer, String[]> nodeLabels = new HashMap<Integer, String[]>();
    final Map<Integer, String[]> nodeLocations = new HashMap<Integer, String[]>();
    Set<Integer> eventIds = new TreeSet<Integer>();
    // really inefficient, is there a better way to do this?
    for (Notification notice : notices) {
        if (notice.getEventId() > 0) {
            eventIds.add(notice.getEventId());
        }
        if (notice.getNodeId() > 0) {
            if (!nodeLabels.containsKey(notice.getNodeId())) {
                String[] labels = null;
                String[] locations = null;
                OnmsNode node = m_nodeDao.get(notice.getNodeId());
                if (node != null) {
                    String longLabel = node.getLabel();
                    if (longLabel == null) {
                        labels = new String[] { "&lt;No Node Label&gt;", "&lt;No Node Label&gt;" };
                    } else {
                        if (longLabel.length() > 32) {
                            String shortLabel = longLabel.substring(0, 31) + "&hellip;";
                            labels = new String[] { shortLabel, longLabel };
                        } else {
                            labels = new String[] { longLabel, longLabel };
                        }
                    }
                    if (node.getLocation() != null) {
                        String location = node.getLocation().getLocationName();
                        if (location == null) {
                            locations = new String[] { "&lt;No Node Location&gt;", "&lt;No Node Location&gt;" };
                        } else {
                            if (location.length() > 32) {
                                String shortLocation = location.substring(0, 31) + "&hellip;";
                                locations = new String[] { shortLocation, location };
                            } else {
                                locations = new String[] { location, location };
                            }
                        }
                    }
                }
                nodeLabels.put(notice.getNodeId(), labels);
                nodeLocations.put(notice.getNodeId(), locations);
            }
        }
    }
    Map<Integer, Event> events = new HashMap<Integer, Event>();
    if (eventIds.size() > 0) {
        for (Event e : m_webEventRepository.getMatchingEvents(new EventCriteria(new EventIdListFilter(eventIds)))) {
            events.put(e.getId(), e);
        }
    }
    ModelAndView modelAndView = new ModelAndView(m_successView);
    modelAndView.addObject("notices", notices);
    modelAndView.addObject("noticeCount", noticeCount);
    modelAndView.addObject("nodeLabels", nodeLabels);
    modelAndView.addObject("nodeLocations", nodeLocations);
    modelAndView.addObject("events", events);
    modelAndView.addObject("parms", parms);
    return modelAndView;
}
Also used : SortStyle(org.opennms.web.notification.SortStyle) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ModelAndView(org.springframework.web.servlet.ModelAndView) EventCriteria(org.opennms.web.event.filter.EventCriteria) EventIdListFilter(org.opennms.web.event.filter.EventIdListFilter) Notification(org.opennms.web.notification.Notification) TreeSet(java.util.TreeSet) NoticeQueryParms(org.opennms.web.notification.NoticeQueryParms) OnmsNode(org.opennms.netmgt.model.OnmsNode) AcknowledgeType(org.opennms.web.notification.AcknowledgeType) Filter(org.opennms.web.filter.Filter) EventIdListFilter(org.opennms.web.event.filter.EventIdListFilter) NotificationCriteria(org.opennms.web.notification.filter.NotificationCriteria) Event(org.opennms.web.event.Event)

Example 4 with Event

use of org.opennms.web.event.Event in project opennms by OpenNMS.

the class EventFeed method getFeed.

/**
     * <p>getFeed</p>
     *
     * @return a {@link com.sun.syndication.feed.synd.SyndFeed} object.
     */
@Override
public SyndFeed getFeed() {
    SyndFeed feed = new SyndFeedImpl();
    feed.setTitle("Events");
    feed.setDescription("OpenNMS Events");
    feed.setLink(getUrlBase() + "event/list.htm");
    List<SyndEntry> entries = new ArrayList<SyndEntry>();
    try {
        Event[] events;
        ArrayList<Filter> filters = new ArrayList<Filter>();
        if (this.getRequest().getParameter("node") != null) {
            Integer nodeId = WebSecurityUtils.safeParseInt(this.getRequest().getParameter("node"));
            filters.add(new NodeFilter(nodeId, getServletContext()));
        }
        if (this.getRequest().getParameter("severity") != null) {
            String parameter = this.getRequest().getParameter("severity");
            try {
                Integer severityId = WebSecurityUtils.safeParseInt(parameter);
                filters.add(new SeverityFilter(severityId));
            } catch (NumberFormatException e) {
                for (OnmsSeverity sev : OnmsSeverity.values()) {
                    if (sev.getLabel().equalsIgnoreCase(parameter)) {
                        filters.add(new SeverityFilter(sev));
                        break;
                    }
                }
            }
        }
        events = EventFactory.getEvents(SortStyle.TIME, AcknowledgeType.BOTH, filters.toArray(new Filter[] {}), this.getMaxEntries(), 0);
        SyndEntry entry;
        for (Event event : events) {
            entry = new SyndEntryImpl();
            entry.setPublishedDate(event.getTime());
            if (event.getAcknowledgeTime() != null) {
                entry.setTitle(sanitizeTitle(event.getLogMessage()) + " (Acknowledged by " + event.getAcknowledgeUser() + ")");
                entry.setUpdatedDate(event.getAcknowledgeTime());
            } else {
                entry.setTitle(sanitizeTitle(event.getLogMessage()));
                entry.setUpdatedDate(event.getTime());
            }
            entry.setLink(getUrlBase() + "event/detail.jsp?id=" + event.getId());
            entry.setAuthor("OpenNMS");
            SyndContent content = new SyndContentImpl();
            content.setType("text/html");
            content.setValue(event.getDescription());
            entry.setDescription(content);
            entries.add(entry);
        }
    } catch (SQLException e) {
        LOG.warn("unable to get event(s)", e);
    }
    feed.setEntries(entries);
    return feed;
}
Also used : SQLException(java.sql.SQLException) SyndEntry(com.sun.syndication.feed.synd.SyndEntry) SyndContentImpl(com.sun.syndication.feed.synd.SyndContentImpl) ArrayList(java.util.ArrayList) SyndFeed(com.sun.syndication.feed.synd.SyndFeed) SeverityFilter(org.opennms.web.event.filter.SeverityFilter) OnmsSeverity(org.opennms.netmgt.model.OnmsSeverity) SyndContent(com.sun.syndication.feed.synd.SyndContent) SeverityFilter(org.opennms.web.event.filter.SeverityFilter) Filter(org.opennms.web.filter.Filter) NodeFilter(org.opennms.web.event.filter.NodeFilter) SyndEntryImpl(com.sun.syndication.feed.synd.SyndEntryImpl) Event(org.opennms.web.event.Event) SyndFeedImpl(com.sun.syndication.feed.synd.SyndFeedImpl) NodeFilter(org.opennms.web.event.filter.NodeFilter)

Example 5 with Event

use of org.opennms.web.event.Event in project opennms by OpenNMS.

the class WebEventRepositoryFilterIT method testBeforeDateFilter.

@Test
@Transactional
public void testBeforeDateFilter() {
    BeforeDateFilter filter = new BeforeDateFilter(new Date());
    Event[] events = getMatchingDaoEvents(filter);
    assertEquals(2, events.length);
}
Also used : OnmsEvent(org.opennms.netmgt.model.OnmsEvent) Event(org.opennms.web.event.Event) Date(java.util.Date) Test(org.junit.Test) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

Event (org.opennms.web.event.Event)6 Test (org.junit.Test)4 OnmsEvent (org.opennms.netmgt.model.OnmsEvent)4 Date (java.util.Date)3 ArrayList (java.util.ArrayList)2 JUnitTemporaryDatabase (org.opennms.core.test.db.annotations.JUnitTemporaryDatabase)2 Filter (org.opennms.web.filter.Filter)2 Transactional (org.springframework.transaction.annotation.Transactional)2 SyndContent (com.sun.syndication.feed.synd.SyndContent)1 SyndContentImpl (com.sun.syndication.feed.synd.SyndContentImpl)1 SyndEntry (com.sun.syndication.feed.synd.SyndEntry)1 SyndEntryImpl (com.sun.syndication.feed.synd.SyndEntryImpl)1 SyndFeed (com.sun.syndication.feed.synd.SyndFeed)1 SyndFeedImpl (com.sun.syndication.feed.synd.SyndFeedImpl)1 SQLException (java.sql.SQLException)1 HashMap (java.util.HashMap)1 TreeSet (java.util.TreeSet)1 OnmsNode (org.opennms.netmgt.model.OnmsNode)1 OnmsSeverity (org.opennms.netmgt.model.OnmsSeverity)1 AlarmIdFilter (org.opennms.web.alarm.filter.AlarmIdFilter)1