Search in sources :

Example 1 with RowProcessor

use of org.opennms.core.utils.RowProcessor in project opennms by OpenNMS.

the class NotificationManager method getActiveNodes.

/**
 * <p>getActiveNodes</p>
 *
 * @return a {@link java.util.List} object.
 * @throws java.sql.SQLException if any.
 */
public List<Integer> getActiveNodes() throws SQLException {
    final List<Integer> allNodes = new ArrayList<>();
    Querier querier = new Querier(m_dataSource, "SELECT n.nodeid FROM node n WHERE n.nodetype != 'D' ORDER BY n.nodelabel", new RowProcessor() {

        @Override
        public void processRow(ResultSet rs) throws SQLException {
            allNodes.add(rs.getInt(1));
        }
    });
    querier.execute(new Object[] {});
    return allNodes;
}
Also used : Querier(org.opennms.core.utils.Querier) SingleResultQuerier(org.opennms.core.utils.SingleResultQuerier) SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) ResultSet(java.sql.ResultSet) RowProcessor(org.opennms.core.utils.RowProcessor)

Example 2 with RowProcessor

use of org.opennms.core.utils.RowProcessor in project opennms by OpenNMS.

the class NotificationManager method getEvent.

/**
 * In the absence of DAOs and ORMs this creates an Event object from the persisted
 * record.
 *
 * @param eventid a int.
 * @return a populated Event object
 */
public Event getEvent(final int eventid) {
    // don't switch using event builder since this event is read from the database
    final Event event = new Event();
    Querier querier = new Querier(m_dataSource, "select * from events where eventid = ?", new RowProcessor() {

        @Override
        public void processRow(ResultSet rs) throws SQLException {
            event.setDbid(rs.getInt("eventid"));
            event.setUei(rs.getString("eventuei"));
            event.setNodeid(rs.getLong("nodeid"));
            event.setTime(rs.getDate("eventtime"));
            event.setHost(rs.getString("eventhost"));
            event.setInterface(rs.getString("ipaddr"));
            event.setSnmphost(rs.getString("eventsnmphost"));
            event.setService(getServiceName(rs.getInt("serviceid")));
            event.setCreationTime(rs.getDate("eventcreatetime"));
            event.setSeverity(rs.getString("eventseverity"));
            event.setPathoutage(rs.getString("eventpathoutage"));
            Tticket tticket = new Tticket();
            tticket.setContent(rs.getString("eventtticket"));
            tticket.setState(rs.getString("eventtticketstate"));
            event.setTticket(tticket);
            event.setSource(rs.getString("eventsource"));
        }

        private String getServiceName(int serviceid) {
            SingleResultQuerier querier = new SingleResultQuerier(m_dataSource, "select servicename from service where serviceid = ?");
            return (String) querier.getResult();
        }
    });
    querier.execute(eventid);
    return event;
}
Also used : Querier(org.opennms.core.utils.Querier) SingleResultQuerier(org.opennms.core.utils.SingleResultQuerier) Tticket(org.opennms.netmgt.xml.event.Tticket) SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) Event(org.opennms.netmgt.xml.event.Event) SingleResultQuerier(org.opennms.core.utils.SingleResultQuerier) RowProcessor(org.opennms.core.utils.RowProcessor)

Example 3 with RowProcessor

use of org.opennms.core.utils.RowProcessor in project opennms by OpenNMS.

the class BroadcastEventProcessor method sendResolvedNotifications.

private void sendResolvedNotifications(Collection<Integer> notifIDs, Event event, String resolutionPrefix, boolean skipNumericPrefix) throws Exception {
    for (int notifId : notifIDs) {
        boolean wa = false;
        if (notifId < 0) {
            notifId *= -1;
            wa = true;
            LOG.debug("Conditional autoNotify for notifId {}", notifId);
        }
        final boolean wasAcked = wa;
        final Map<String, String> parmMap = rebuildParameterMap(notifId, resolutionPrefix, skipNumericPrefix);
        m_eventUtil.expandMapValues(parmMap, getNotificationManager().getEvent(Integer.parseInt(parmMap.get("eventID"))));
        String queueID = getNotificationManager().getQueueForNotification(notifId);
        final Map<String, List<String>> userNotifications = new HashMap<String, List<String>>();
        RowProcessor ackNotifProcessor = new RowProcessor() {

            @Override
            public void processRow(ResultSet rs) throws SQLException {
                String userID = rs.getString("userID");
                String contactInfo = rs.getString("contactinfo");
                String autoNotifyChar = rs.getString("autonotify");
                if (userID.equals("email-address")) {
                    userID = contactInfo;
                }
                String cmd = rs.getString("media");
                if (autoNotifyChar == null) {
                    autoNotifyChar = "C";
                }
                if (autoNotifyChar.equals("Y") || (autoNotifyChar.equals("C") && !wasAcked)) {
                    List<String> cmdList = userNotifications.get(userID);
                    if (cmdList == null) {
                        cmdList = new ArrayList<String>();
                        userNotifications.put(userID, cmdList);
                    }
                    cmdList.add(cmd);
                }
            }
        };
        getNotificationManager().forEachUserNotification(notifId, ackNotifProcessor);
        for (final Entry<String, List<String>> entry : userNotifications.entrySet()) {
            final String userID = entry.getKey();
            final List<String> cmdList = entry.getValue();
            final String[] cmds = cmdList.toArray(new String[cmdList.size()]);
            LOG.debug("Sending {} notification to userID = {} for notice ID {}", resolutionPrefix, userID, notifId);
            sendResolvedNotificationsToUser(queueID, userID, cmds, parmMap);
        }
    }
}
Also used : HashMap(java.util.HashMap) ResultSet(java.sql.ResultSet) List(java.util.List) ArrayList(java.util.ArrayList) RowProcessor(org.opennms.core.utils.RowProcessor)

Example 4 with RowProcessor

use of org.opennms.core.utils.RowProcessor in project opennms by OpenNMS.

the class NotifdIT method testGetUsersNotified.

@Test
public void testGetUsersNotified() throws Exception {
    MockInterface iface = m_network.getInterface(1, "192.168.1.1");
    Date downDate = new Date();
    anticipateNotificationsForGroup("interface 192.168.1.1 down.", "All services are down on interface 192.168.1.1, dot1 interface alias.", "InitialGroup", downDate, 0);
    // bring node down now
    Event event = iface.createDownEvent(downDate);
    m_eventMgr.sendEventToListeners(event);
    sleep(1000);
    Collection<List<String>> expectedResults = new LinkedList<List<String>>();
    Collection<String> users = getUsersInGroup("InitialGroup");
    for (String userID : users) {
        List<String> cmdList = new LinkedList<String>();
        cmdList.add(userID);
        cmdList.add("mockNotifier");
        expectedResults.add(cmdList);
    }
    Collection<Integer> notifIds = m_db.findNoticesForEvent(event);
    assertEquals("notification ID size", 1, notifIds.size());
    Integer notifId = notifIds.iterator().next();
    assertNotNull("first notifId should not be null", notifId);
    final Collection<List<String>> actualResults = new LinkedList<List<String>>();
    RowProcessor rp = new RowProcessor() {

        @Override
        public void processRow(ResultSet rs) throws SQLException {
            List<String> cmdList = new LinkedList<String>();
            cmdList.add(rs.getString("userID"));
            cmdList.add(rs.getString("media"));
            actualResults.add(cmdList);
        }
    };
    m_notificationManager.forEachUserNotification(notifId.intValue(), rp);
    /*
         * This test does not work reliably because notifications within a
         * group are not guaranteed to be in a certain order.
         */
    // assertEquals("Notifications", expectedResults, actualResults);
    // Use a set instead so we don't care about ordering.
    Set<List<String>> expectedSet = new HashSet<List<String>>(expectedResults);
    Set<List<String>> actualSet = new HashSet<List<String>>(actualResults);
    assertEquals("Notifications as a set", expectedSet, actualSet);
}
Also used : Date(java.util.Date) LinkedList(java.util.LinkedList) MockInterface(org.opennms.netmgt.mock.MockInterface) ResultSet(java.sql.ResultSet) Event(org.opennms.netmgt.xml.event.Event) LinkedList(java.util.LinkedList) List(java.util.List) RowProcessor(org.opennms.core.utils.RowProcessor) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 5 with RowProcessor

use of org.opennms.core.utils.RowProcessor in project opennms by OpenNMS.

the class NotificationManager method getServiceNames.

/**
 * <p>getServiceNames</p>
 *
 * @return a {@link java.util.List} object.
 */
public List<String> getServiceNames() throws SQLException {
    final List<String> services = new ArrayList<>();
    Querier querier = new Querier(m_dataSource, "SELECT servicename FROM service", new RowProcessor() {

        @Override
        public void processRow(ResultSet rs) throws SQLException {
            services.add(rs.getString(1));
        }
    });
    querier.execute(new Object[] {});
    return services;
}
Also used : Querier(org.opennms.core.utils.Querier) SingleResultQuerier(org.opennms.core.utils.SingleResultQuerier) SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) ResultSet(java.sql.ResultSet) RowProcessor(org.opennms.core.utils.RowProcessor)

Aggregations

ResultSet (java.sql.ResultSet)5 RowProcessor (org.opennms.core.utils.RowProcessor)5 SQLException (java.sql.SQLException)3 ArrayList (java.util.ArrayList)3 Querier (org.opennms.core.utils.Querier)3 SingleResultQuerier (org.opennms.core.utils.SingleResultQuerier)3 List (java.util.List)2 Event (org.opennms.netmgt.xml.event.Event)2 Date (java.util.Date)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 LinkedList (java.util.LinkedList)1 Test (org.junit.Test)1 MockInterface (org.opennms.netmgt.mock.MockInterface)1 Tticket (org.opennms.netmgt.xml.event.Tticket)1