Search in sources :

Example 1 with DBUtils

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

the class DeleteNodesServlet method getIpAddrsForNode.

private List<String> getIpAddrsForNode(Integer nodeId) throws ServletException {
    List<String> ipAddrs = new ArrayList<String>();
    final DBUtils d = new DBUtils(getClass());
    try {
        Connection conn = DataSourceFactory.getInstance().getConnection();
        d.watch(conn);
        PreparedStatement stmt = conn.prepareStatement("SELECT DISTINCT ipaddr FROM ipinterface WHERE nodeid=?");
        d.watch(stmt);
        stmt.setInt(1, nodeId);
        ResultSet rs = stmt.executeQuery();
        d.watch(rs);
        while (rs.next()) {
            ipAddrs.add(rs.getString("ipaddr"));
        }
    } catch (SQLException e) {
        throw new ServletException("There was a problem with the database connection: " + e, e);
    } finally {
        d.cleanUp();
    }
    return ipAddrs;
}
Also used : ServletException(javax.servlet.ServletException) SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) DBUtils(org.opennms.core.utils.DBUtils) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Example 2 with DBUtils

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

the class GetNodesServlet method getAllNodes.

/**
     */
private List<ManagedInterface> getAllNodes(HttpSession userSession) throws SQLException {
    Connection connection = null;
    List<ManagedInterface> allNodes = new ArrayList<ManagedInterface>();
    int lineCount = 0;
    final DBUtils d = new DBUtils(getClass());
    try {
        connection = DataSourceFactory.getInstance().getConnection();
        d.watch(connection);
        PreparedStatement ifaceStmt = connection.prepareStatement(INTERFACE_QUERY);
        d.watch(ifaceStmt);
        ResultSet ifaceResults = ifaceStmt.executeQuery();
        d.watch(ifaceResults);
        if (ifaceResults != null) {
            while (ifaceResults.next()) {
                lineCount++;
                ManagedInterface newInterface = new ManagedInterface();
                allNodes.add(newInterface);
                newInterface.setNodeid(ifaceResults.getInt(1));
                newInterface.setAddress(ifaceResults.getString(2));
                newInterface.setStatus(ifaceResults.getString(3));
                PreparedStatement svcStmt = connection.prepareStatement(SERVICE_QUERY);
                d.watch(svcStmt);
                svcStmt.setInt(1, newInterface.getNodeid());
                svcStmt.setString(2, newInterface.getAddress());
                ResultSet svcResults = svcStmt.executeQuery();
                d.watch(svcResults);
                if (svcResults != null) {
                    while (svcResults.next()) {
                        lineCount++;
                        ManagedService newService = new ManagedService();
                        newService.setId(svcResults.getInt(1));
                        newService.setName(svcResults.getString(2));
                        newService.setStatus(svcResults.getString(3));
                        newInterface.addService(newService);
                    }
                }
            }
        }
        userSession.setAttribute("lineItems.manage.jsp", Integer.valueOf(lineCount));
    } finally {
        d.cleanUp();
    }
    return allNodes;
}
Also used : Connection(java.sql.Connection) ArrayList(java.util.ArrayList) DBUtils(org.opennms.core.utils.DBUtils) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Example 3 with DBUtils

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

the class JdbcFilterDao method getNodeMap.

/**
     * {@inheritDoc}
     *
     * This method returns a map of all nodeids and nodelabels that match
     * the rule that is passed in, sorted by nodeid.
     * @exception FilterParseException
     *                if a rule is syntactically incorrect or failed in
     *                executing the SQL statement
     */
@Override
public SortedMap<Integer, String> getNodeMap(final String rule) throws FilterParseException {
    final SortedMap<Integer, String> resultMap = new TreeMap<Integer, String>();
    String sqlString;
    LOG.debug("Filter.getNodeMap({})", rule);
    // get the database connection
    Connection conn = null;
    final DBUtils d = new DBUtils(getClass());
    try {
        conn = getDataSource().getConnection();
        d.watch(conn);
        // parse the rule and get the sql select statement
        sqlString = getNodeMappingStatement(rule);
        LOG.debug("Filter.getNodeMap({}): SQL statement: {}", rule, sqlString);
        // execute query
        final Statement stmt = conn.createStatement();
        d.watch(stmt);
        final ResultSet rset = stmt.executeQuery(sqlString);
        d.watch(rset);
        if (rset != null) {
            // Iterate through the result and build the map
            while (rset.next()) {
                resultMap.put(Integer.valueOf(rset.getInt(1)), rset.getString(2));
            }
        }
    } catch (final FilterParseException e) {
        LOG.warn("Filter Parse Exception occurred getting node map.", e);
        throw new FilterParseException("Filter Parse Exception occurred getting node map: " + e.getLocalizedMessage(), e);
    } catch (final SQLException e) {
        LOG.warn("SQL Exception occurred getting node map.", e);
        throw new FilterParseException("SQL Exception occurred getting node map: " + e.getLocalizedMessage(), e);
    } catch (final Throwable e) {
        LOG.error("Exception getting database connection.", e);
        throw new UndeclaredThrowableException(e);
    } finally {
        d.cleanUp();
    }
    return Collections.unmodifiableSortedMap(resultMap);
}
Also used : FilterParseException(org.opennms.netmgt.filter.api.FilterParseException) SQLException(java.sql.SQLException) Statement(java.sql.Statement) UndeclaredThrowableException(java.lang.reflect.UndeclaredThrowableException) Connection(java.sql.Connection) DBUtils(org.opennms.core.utils.DBUtils) ResultSet(java.sql.ResultSet) TreeMap(java.util.TreeMap)

Example 4 with DBUtils

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

the class JdbcFilterDao method getIPAddressServiceMap.

/** {@inheritDoc} */
@Override
public Map<InetAddress, Set<String>> getIPAddressServiceMap(final String rule) throws FilterParseException {
    final Map<InetAddress, Set<String>> ipServices = new TreeMap<InetAddress, Set<String>>(new InetAddressComparator());
    String sqlString;
    LOG.debug("Filter.getIPAddressServiceMap({})", rule);
    // get the database connection
    Connection conn = null;
    final DBUtils d = new DBUtils(getClass());
    try {
        conn = getDataSource().getConnection();
        d.watch(conn);
        // parse the rule and get the sql select statement
        sqlString = getIPServiceMappingStatement(rule);
        LOG.debug("Filter.getIPAddressServiceMap({}): SQL statement: {}", rule, sqlString);
        // execute query
        final Statement stmt = conn.createStatement();
        d.watch(stmt);
        final ResultSet rset = stmt.executeQuery(sqlString);
        d.watch(rset);
        // fill up the array list if the result set has values
        if (rset != null) {
            // Iterate through the result and build the array list
            while (rset.next()) {
                final InetAddress ipaddr = addr(rset.getString(1));
                if (ipaddr != null) {
                    if (!ipServices.containsKey(ipaddr)) {
                        ipServices.put(ipaddr, new TreeSet<String>());
                    }
                    ipServices.get(ipaddr).add(rset.getString(2));
                }
            }
        }
    } catch (final FilterParseException e) {
        LOG.warn("Filter Parse Exception occurred getting IP Service List.", e);
        throw new FilterParseException("Filter Parse Exception occurred getting IP Service List: " + e.getLocalizedMessage(), e);
    } catch (final SQLException e) {
        LOG.warn("SQL Exception occurred getting IP Service List.", e);
        throw new FilterParseException("SQL Exception occurred getting IP Service List: " + e.getLocalizedMessage(), e);
    } catch (final RuntimeException e) {
        LOG.error("Unexpected exception getting database connection.", e);
        throw e;
    } catch (final Error e) {
        LOG.error("Unexpected exception getting database connection.", e);
        throw e;
    } finally {
        d.cleanUp();
    }
    return ipServices;
}
Also used : TreeSet(java.util.TreeSet) ResultSet(java.sql.ResultSet) Set(java.util.Set) FilterParseException(org.opennms.netmgt.filter.api.FilterParseException) SQLException(java.sql.SQLException) Statement(java.sql.Statement) Connection(java.sql.Connection) TreeMap(java.util.TreeMap) InetAddressComparator(org.opennms.core.utils.InetAddressComparator) DBUtils(org.opennms.core.utils.DBUtils) ResultSet(java.sql.ResultSet) InetAddress(java.net.InetAddress)

Example 5 with DBUtils

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

the class CategoryModel method getServiceAvailability.

/**
     * Return the availability percentage for a managed service from the given
     * start time until the given end time. If the service is not managed, then
     * a value of -1.0 is returned.
     * 
     * @param nodeId a int.
     * @param ipAddr a {@link java.lang.String} object.
     * @param serviceId a int.
     * @param start a {@link java.util.Date} object.
     * @param end a {@link java.util.Date} object.
     * @return a double.
     * @throws java.sql.SQLException if any.
     */
static double getServiceAvailability(int nodeId, String ipAddr, int serviceId, Date start, Date end) throws SQLException {
    if (ipAddr == null || start == null || end == null) {
        throw new IllegalArgumentException("Cannot take null parameters.");
    }
    if (end.before(start)) {
        throw new IllegalArgumentException("Cannot have an end time before the start time.");
    }
    if (end.equals(start)) {
        throw new IllegalArgumentException("Cannot have an end time equal to the start time.");
    }
    final DBUtils d = new DBUtils(CategoryModel.class);
    try {
        Connection conn = DataSourceFactory.getInstance().getConnection();
        d.watch(conn);
        PreparedStatement stmt = conn.prepareStatement("select getPercentAvailabilityInWindow(ifservices.id, ?, ?) as avail from ifservices, ipinterface, node where ifservices.ipInterfaceId = ipinterface.id and ipInterface.nodeid = node.nodeid and ifservices.status='A' and ipinterface.ismanaged='M' and node.nodetype='A' and node.nodeid=? and ipInterface.ipaddr=? and ifServices.serviceid=?");
        d.watch(stmt);
        // yes, these are supposed to be backwards, the end time first
        stmt.setTimestamp(1, new Timestamp(end.getTime()));
        stmt.setTimestamp(2, new Timestamp(start.getTime()));
        stmt.setInt(3, nodeId);
        stmt.setString(4, ipAddr);
        stmt.setInt(5, serviceId);
        ResultSet rs = stmt.executeQuery();
        d.watch(rs);
        if (rs.next()) {
            return rs.getDouble("avail");
        }
    } catch (final SQLException e) {
        LOG.warn("Failed to get service availability for nodeId {}, interface {}, serviceId {}", nodeId, ipAddr, serviceId, e);
    } finally {
        d.cleanUp();
    }
    return -1.0;
}
Also used : SQLException(java.sql.SQLException) DBUtils(org.opennms.core.utils.DBUtils) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) Timestamp(java.sql.Timestamp)

Aggregations

DBUtils (org.opennms.core.utils.DBUtils)77 Connection (java.sql.Connection)68 PreparedStatement (java.sql.PreparedStatement)64 ResultSet (java.sql.ResultSet)50 SQLException (java.sql.SQLException)17 ArrayList (java.util.ArrayList)14 Timestamp (java.sql.Timestamp)12 Statement (java.sql.Statement)10 Filter (org.opennms.web.filter.Filter)9 ServletException (javax.servlet.ServletException)5 FilterParseException (org.opennms.netmgt.filter.api.FilterParseException)5 IfIndexFilter (org.opennms.web.event.filter.IfIndexFilter)5 InterfaceFilter (org.opennms.web.event.filter.InterfaceFilter)5 NodeFilter (org.opennms.web.event.filter.NodeFilter)5 ServiceFilter (org.opennms.web.event.filter.ServiceFilter)5 SeverityFilter (org.opennms.web.event.filter.SeverityFilter)5 Date (java.util.Date)4 UndeclaredThrowableException (java.lang.reflect.UndeclaredThrowableException)3 InetAddress (java.net.InetAddress)3 RequestDispatcher (javax.servlet.RequestDispatcher)3