Search in sources :

Example 1 with Querier

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

the class MockDatabaseIT method testIFQuery.

public void testIFQuery() {
    Querier querier = new Querier(m_db, "select * from ipInterface") {

        @Override
        public void processRow(ResultSet rs) throws SQLException {
            int nodeId = rs.getInt("nodeId");
            String ipAddr = rs.getString("ipAddr");
            MockInterface iface = m_network.getInterface(nodeId, ipAddr);
            assertNotNull(iface);
            assertEquals(nodeId, iface.getNodeId());
            assertEquals(ipAddr, iface.getIpAddr());
        }
    };
    querier.execute();
    assertEquals(m_network.getInterfaceCount(), querier.getCount());
}
Also used : Querier(org.opennms.core.utils.Querier) MockInterface(org.opennms.netmgt.mock.MockInterface) ResultSet(java.sql.ResultSet)

Example 2 with Querier

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

the class MockDatabaseIT method testMultipleDatabases.

public void testMultipleDatabases() throws Exception {
    m_secondDb = new MockDatabase(m_db.getTestDatabase() + "_test2");
    Querier secondQuerier = new Querier(m_secondDb, "select * from node");
    secondQuerier.execute();
    Querier querier = new Querier(m_db, "select * from node");
    querier.execute();
    assertFalse(secondQuerier.getCount() == querier.getCount());
    MockNode node = m_network.getNode(1);
    m_secondDb.writeNode(node);
    secondQuerier = new Querier(m_secondDb, "select * from node");
    secondQuerier.execute();
    assertEquals(1, secondQuerier.getCount());
}
Also used : Querier(org.opennms.core.utils.Querier) MockNode(org.opennms.netmgt.mock.MockNode)

Example 3 with Querier

use of org.opennms.core.utils.Querier 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 4 with Querier

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

the class NotificationManager method rebuildParameterMap.

/**
 * <p>rebuildParameterMap</p>
 *
 * @param notifId a int.
 * @param resolutionPrefix a {@link java.lang.String} object.
 * @param skipNumericPrefix a boolean.
 * @return a {@link java.util.Map} object.
 * @throws java.lang.Exception if any.
 */
public Map<String, String> rebuildParameterMap(final int notifId, final String resolutionPrefix, final boolean skipNumericPrefix) throws Exception {
    final Map<String, String> parmMap = new HashMap<String, String>();
    Querier querier = new Querier(m_dataSource, "select notifications.*, service.* from notifications left outer join service on notifications.serviceID = service.serviceID  where notifyId = ?") {

        @Override
        public void processRow(ResultSet rs) throws SQLException {
            /*
                 * Note, getString on results is valid for any SQL data type except the new SQL types:
                 *    Blog, Clob, Array, Struct, Ref
                 * of which we have none in this table so this row processor is using getString
                 * to correctly align with annotated types in the map.
                 */
            parmMap.put(NotificationManager.PARAM_TEXT_MSG, expandNotifParms(resolutionPrefix, Collections.singletonMap("noticeid", String.valueOf(notifId))) + rs.getString("textMsg"));
            if (skipNumericPrefix) {
                parmMap.put(NotificationManager.PARAM_NUM_MSG, rs.getString("numericMsg"));
            } else {
                parmMap.put(NotificationManager.PARAM_NUM_MSG, expandNotifParms(resolutionPrefix, Collections.singletonMap("noticeid", String.valueOf(notifId))) + rs.getString("numericMsg"));
            }
            parmMap.put(NotificationManager.PARAM_SUBJECT, expandNotifParms(resolutionPrefix, Collections.singletonMap("noticeid", String.valueOf(notifId))) + rs.getString("subject"));
            parmMap.put(NotificationManager.PARAM_NODE, rs.getString("nodeID"));
            parmMap.put(NotificationManager.PARAM_INTERFACE, rs.getString("interfaceID"));
            parmMap.put(NotificationManager.PARAM_SERVICE, rs.getString("serviceName"));
            parmMap.put("noticeid", rs.getString("notifyID"));
            parmMap.put("eventID", rs.getString("eventID"));
            parmMap.put("eventUEI", rs.getString("eventUEI"));
            Notification notification = null;
            try {
                notification = getNotification(rs.getObject("notifConfigName").toString());
            } catch (IOException e) {
            }
            if (notification != null) {
                addNotificationParams(parmMap, notification);
            }
        }
    };
    querier.execute(notifId);
    return parmMap;
}
Also used : Querier(org.opennms.core.utils.Querier) SingleResultQuerier(org.opennms.core.utils.SingleResultQuerier) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ResultSet(java.sql.ResultSet) IOException(java.io.IOException) Notification(org.opennms.netmgt.config.notifications.Notification)

Example 5 with Querier

use of org.opennms.core.utils.Querier 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)

Aggregations

Querier (org.opennms.core.utils.Querier)14 ResultSet (java.sql.ResultSet)12 SingleResultQuerier (org.opennms.core.utils.SingleResultQuerier)7 SQLException (java.sql.SQLException)3 RowProcessor (org.opennms.core.utils.RowProcessor)3 ArrayList (java.util.ArrayList)2 LinkedList (java.util.LinkedList)2 MockNode (org.opennms.netmgt.mock.MockNode)2 MockService (org.opennms.netmgt.mock.MockService)2 Event (org.opennms.netmgt.xml.event.Event)2 IOException (java.io.IOException)1 Date (java.util.Date)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 Notification (org.opennms.netmgt.config.notifications.Notification)1 MockInterface (org.opennms.netmgt.mock.MockInterface)1 MockPathOutage (org.opennms.netmgt.mock.MockPathOutage)1 Outage (org.opennms.netmgt.mock.Outage)1 PollStatus (org.opennms.netmgt.poller.PollStatus)1 Tticket (org.opennms.netmgt.xml.event.Tticket)1