From bb0c3e44e55785c82c817f14ca7d155ca894be76 Mon Sep 17 00:00:00 2001 From: Seth Leger Date: Thu, 21 May 2015 00:38:02 -0400 Subject: [PATCH] Deleted all unused functions out of OutageModel, fixed SQL in remaining functions, and added a simple unit test for the remaining functions. --- .../org/opennms/web/outage/OutageModel.java | 735 +----------------- .../java/org/opennms/web/rss/OutageFeed.java | 3 +- .../includes/interfaceAvailability-box.jsp | 2 +- .../webapp/includes/nodeAvailability-box.jsp | 2 +- .../includes/serviceAvailability-box.jsp | 2 +- .../opennms/web/outage/OutageModelTest.java | 89 +++ 6 files changed, 111 insertions(+), 722 deletions(-) create mode 100644 opennms-webapp/src/test/java/org/opennms/web/outage/OutageModelTest.java diff --git a/opennms-webapp/src/main/java/org/opennms/web/outage/OutageModel.java b/opennms-webapp/src/main/java/org/opennms/web/outage/OutageModel.java index f08a6921c03f..9fb185c79a6b 100644 --- a/opennms-webapp/src/main/java/org/opennms/web/outage/OutageModel.java +++ b/opennms-webapp/src/main/java/org/opennms/web/outage/OutageModel.java @@ -32,148 +32,28 @@ import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; -import java.sql.Statement; import java.sql.Timestamp; import java.util.ArrayList; -import java.util.Collection; import java.util.Date; -import java.util.HashMap; import java.util.List; import org.opennms.core.db.DataSourceFactory; import org.opennms.core.utils.DBUtils; -import org.opennms.netmgt.model.OnmsNode; +import org.opennms.netmgt.dao.api.OutageDao; import org.opennms.netmgt.model.outage.OutageSummary; -import org.springframework.util.StringUtils; /** * As the nonvisual logic for the Services Down (Outage) servlet and JSPs, this * class queries the database for current outages and provides utility methods * for manipulating that list of outages. + * + * @deprecated Use {@link OutageDao} instead. * * @author Lawrence Karnowski * @author OpenNMS */ -public class OutageModel { - private final DBUtils d = new DBUtils(getClass()); +public abstract class OutageModel { - /** - * Create a new OutageModel. - */ - public OutageModel() { - } - - /** - * Query the database to retrieve the current outages. - * - * @return An array of {@link Outage Outage}objects, or if there are none, - * an empty array. - * @throws java.sql.SQLException - * If there is a problem getting a database connection or making - * a query. - */ - public Outage[] getCurrentOutages() throws SQLException { - Outage[] outages = new Outage[0]; - - try { - Connection conn = DataSourceFactory.getInstance().getConnection(); - d.watch(conn); - - Statement stmt = conn.createStatement(); - d.watch(stmt); - ResultSet rs = stmt.executeQuery("select o.outageid, o.nodeId, n.nodeLabel, o.ipaddr, ip.iphostname, s.servicename, o.serviceId, o.iflostservice, o.svclosteventid, no.notifyId, no.answeredBy from outages o left outer join notifications no on (o.svclosteventid = no.eventid) join ifservices if on (if.id = o.ifserviceid) join ipinterface ip on (ip.id = if.ipinterfaceid) join node n on (n.nodeid = ip.nodeid) join service s on (s.serviceid = if.serviceid) where ifregainedservice is null and n.nodeType != 'D' and ip.isManaged != 'D' and if.status != 'D' and o.serviceid=s.serviceid and (o.suppresstime is null or o.suppresstime < now()) order by n.nodelabel, ip.ipaddr, s.serviceName"); - d.watch(rs); - - outages = rs2Outages(rs, false, true); - } finally { - d.cleanUp(); - } - - return outages; - } - - /** - *

getSuppressedOutages

- * - * @return an array of {@link org.opennms.web.outage.Outage} objects. - * @throws java.sql.SQLException if any. - */ - public Outage[] getSuppressedOutages() throws SQLException { - Outage[] outages = new Outage[0]; - - try { - Connection conn = DataSourceFactory.getInstance().getConnection(); - d.watch(conn); - - Statement stmt = conn.createStatement(); - d.watch(stmt); - ResultSet rs = stmt.executeQuery("select o.outageid, o.nodeId, n.nodeLabel, o.ipaddr, ip.iphostname, s.servicename, o.serviceId, o.iflostservice, o.svclosteventid, no.notifyId, no.answeredBy from outages o left outer join notifications no on (o.svclosteventid = no.eventid) join ifservices if on (if.id = o.ifserviceid) join ipinterface ip on (ip.id = if.ipinterfaceid) join node n on (n.nodeid = ip.nodeid) join service s on (s.serviceid = if.serviceid) where ifregainedservice is null and n.nodeType != 'D' and ip.isManaged != 'D' and if.status != 'D' and o.serviceid=s.serviceid and o.suppresstime > now() order by n.nodelabel, ip.ipaddr, s.serviceName"); - d.watch(rs); - - outages = rs2Outages(rs, false, true); - } finally { - d.cleanUp(); - } - - return outages; - } - - /** - *

getCurrentOutageCount

- * - * @return a int. - * @throws java.sql.SQLException if any. - */ - public int getCurrentOutageCount() throws SQLException { - int count = 0; - - try { - Connection conn = DataSourceFactory.getInstance().getConnection(); - d.watch(conn); - - Statement stmt = conn.createStatement(); - d.watch(stmt); - ResultSet rs = stmt.executeQuery("select count(o.outageid) from outages o left outer join notifications no on (o.svclosteventid = no.eventid) join ifservices if on (if.id = o.ifserviceid) join ipinterface ip on (ip.id = if.ipinterfaceid) join node n on (n.nodeid = ip.nodeid) join service s on (s.serviceid = if.serviceid) where ifregainedservice is null and n.nodeType != 'D' and ip.isManaged != 'D' and if.status != 'D' and o.serviceid=s.serviceid and (o.suppresstime is null or o.suppresstime < now()) "); - d.watch(rs); - - if (rs.next()) { - count = rs.getInt("count"); - } - } finally { - d.cleanUp(); - } - - return count; - } - - /** - *

getSuppressedOutageCount

- * - * @return a int. - * @throws java.sql.SQLException if any. - */ - public int getSuppressedOutageCount() throws SQLException { - int count = 0; - - try { - Connection conn = DataSourceFactory.getInstance().getConnection(); - d.watch(conn); - - Statement stmt = conn.createStatement(); - d.watch(stmt); - ResultSet rs = stmt.executeQuery("select count(o.outageid) from outages o left outer join notifications no on (o.svclosteventid = no.eventid) join ifservices if on (if.id = o.ifserviceid) join ipinterface ip on (ip.id = if.ipinterfaceid) join node n on (n.nodeid = ip.nodeid) join service s on (s.serviceid = if.serviceid) where ifregainedservice is null and n.nodeType != 'D' and ip.isManaged != 'D' and if.status != 'D' and o.serviceid=s.serviceid and o.suppresstime > now() "); - d.watch(rs); - - if (rs.next()) { - count = rs.getInt("count"); - } - } finally { - d.cleanUp(); - } - - return count; - } - /** *

getCurrentOutagesForNode

* @@ -181,21 +61,23 @@ public int getSuppressedOutageCount() throws SQLException { * @return an array of {@link org.opennms.web.outage.Outage} objects. * @throws java.sql.SQLException if any. */ - public Outage[] getCurrentOutagesForNode(int nodeId) throws SQLException { + public static Outage[] getCurrentOutagesForNode(int nodeId) throws SQLException { Outage[] outages = new Outage[0]; + final DBUtils d = new DBUtils(OutageModel.class); + try { Connection conn = DataSourceFactory.getInstance().getConnection(); d.watch(conn); PreparedStatement stmt = conn.prepareStatement("" + "SELECT DISTINCT \n" + - " outages.outageid, outages.iflostservice, outages.ifregainedservice, outages.nodeID, \n" + + " outages.outageid, outages.iflostservice, outages.ifregainedservice, node.nodeID, \n" + " node.nodeLabel, \n" + - " outages.ipaddr, \n" + + " ipinterface.ipaddr, \n" + " ipinterface.iphostname, \n" + " service.servicename, \n" + - " outages.serviceId \n" + + " ifservices.serviceId \n" + " from outages \n" + " join ifservices \n" + " on ifservices.id = outages.ifserviceid \n" + @@ -222,515 +104,6 @@ public Outage[] getCurrentOutagesForNode(int nodeId) throws SQLException { return outages; } - - /** - *

getCurrentOutagesIdsForNode

- * - * @param nodeId a int. - * @return a {@link java.util.Collection} object. - * @throws java.sql.SQLException if any. - */ - public Collection getCurrentOutagesIdsForNode(int nodeId) throws SQLException { - List outageIds = new ArrayList(); - - try { - Connection conn = DataSourceFactory.getInstance().getConnection(); - d.watch(conn); - - PreparedStatement stmt = conn.prepareStatement("SELECT DISTINCT outageid from outages where nodeid=? and ifregainedservice is null and suppresstime is null or suppresstime < now();"); - d.watch(stmt); - stmt.setInt(1, nodeId); - ResultSet rs = stmt.executeQuery(); - d.watch(rs); - - while (rs.next()) { - outageIds.add(rs.getInt(1)); - } - } finally { - d.cleanUp(); - } - - return outageIds; - } - - - /** - *

filterNodesWithCurrentOutages

- * - * @param nodes an array of {@link org.opennms.netmgt.config.poller.outages.Node} objects. - * @return an array of {@link org.opennms.netmgt.config.poller.outages.Node} objects. - * @throws java.sql.SQLException if any. - */ - public OnmsNode[] filterNodesWithCurrentOutages(List nodes) throws SQLException { - HashMap nodeMap = new HashMap(nodes.size()); - for (OnmsNode n : nodes) { - nodeMap.put(n.getId(), n); - } - - String nodeList = StringUtils.collectionToDelimitedString(nodeMap.keySet(), ", "); - - List newNodes = new ArrayList(); - - try { - Connection conn = DataSourceFactory.getInstance().getConnection(); - d.watch(conn); - - PreparedStatement stmt = conn.prepareStatement("SELECT DISTINCT nodeid from outages where nodeid in ( " + nodeList + " ) and ifregainedservice is null and suppresstime is null or suppresstime < now();"); - d.watch(stmt); - ResultSet rs = stmt.executeQuery(); - d.watch(rs); - - while (rs.next()) { - newNodes.add(nodeMap.get(rs.getInt(1))); - } - } finally { - d.cleanUp(); - } - - return newNodes.toArray(new OnmsNode[newNodes.size()]); - } - - /** - *

getNonCurrentOutagesForNode

- * - * @param nodeId a int. - * @return an array of {@link org.opennms.web.outage.Outage} objects. - * @throws java.sql.SQLException if any. - */ - public Outage[] getNonCurrentOutagesForNode(int nodeId) throws SQLException { - Outage[] outages = new Outage[0]; - - try { - Connection conn = DataSourceFactory.getInstance().getConnection(); - d.watch(conn); - - PreparedStatement stmt = conn.prepareStatement("" - + "SELECT DISTINCT \n" + - " outages.outageid, outages.iflostservice, outages.ifregainedservice, outages.nodeID, \n" + - " node.nodeLabel, \n" + - " outages.ipaddr, \n" + - " ipinterface.iphostname, \n" + - " service.servicename, \n" + - " outages.serviceId \n" + - " FROM outages \n" + - " JOIN ifservices \n" + - " ON ifservices.id = outages.ifserviceid \n" + - " JOIN ipinterface \n" + - " ON ipinterface.id = ifservices.ipinterfaceid \n" + - " JOIN node \n" + - " ON node.nodeid = ipinterface.nodeid \n" + - " JOIN service \n" + - " ON service.serviceid = ifservices.serviceid \n" + - " WHERE node.nodeid = ? \n" + - " AND outages.ifregainedservice IS NOT NULL \n" + - " AND outages.suppresstime IS NULL \n" + - " OR outages.suppresstime < now() \n" + - "ORDER BY outages.iflostservice desc"); - d.watch(stmt); - stmt.setInt(1, nodeId); - ResultSet rs = stmt.executeQuery(); - d.watch(rs); - - outages = rs2Outages(rs); - } finally { - d.cleanUp(); - } - - return outages; - } - - /** - * Get all outages for a given node. - * - * @param nodeId a int. - * @return an array of {@link org.opennms.web.outage.Outage} objects. - * @throws java.sql.SQLException if any. - */ - public Outage[] getOutagesForNode(int nodeId) throws SQLException { - Outage[] outages = new Outage[0]; - - try { - Connection conn = DataSourceFactory.getInstance().getConnection(); - d.watch(conn); - - PreparedStatement stmt = conn.prepareStatement(" " - + "SELECT DISTINCT \n" + - " outages.outageid, outages.iflostservice, outages.ifregainedservice, outages.nodeID, \n" + - " node.nodeLabel, \n" + - " outages.ipaddr, \n" + - " ipinterface.iphostname, \n" + - " service.servicename, \n" + - " outages.serviceId \n" + - " FROM outages \n" + - " JOIN ifservices \n" + - " ON ifservices.id = outages.ifserviceid \n" + - " JOIN ipinterface \n" + - " ON ipinterface.id = ifservices.ipinterfaceid \n" + - " JOIN node \n" + - " ON node.nodeid = ipinterface.nodeid \n" + - " JOIN service \n" + - " ON service.serviceid = ifservices.serviceid\n" + - " WHERE node.nodeid = ? \n" + - " AND node.nodeid=outages.nodeid \n" + - " AND outages.serviceid=service.serviceid \n" + - " AND ipinterface.ipaddr=outages.ipaddr \n" + - "ORDER BY iflostservice desc"); - d.watch(stmt); - stmt.setInt(1, nodeId); - ResultSet rs = stmt.executeQuery(); - d.watch(rs); - - outages = rs2Outages(rs); - } finally { - d.cleanUp(); - } - - return outages; - } - - /** - * Get all current outages and any resolved outages since the given time for - * the given node. - * - * @param nodeId - * this is the node to query - * @param time - * no resolved outages older than this time will be returned - * @return All current outages and resolved outages no older than - * time. - * @throws java.sql.SQLException if any. - */ - public Outage[] getOutagesForNode(int nodeId, Date time) throws SQLException { - if (time == null) { - throw new IllegalArgumentException("Cannot take null parameters."); - } - - Outage[] outages = new Outage[0]; - long timeLong = time.getTime(); - - try { - Connection conn = DataSourceFactory.getInstance().getConnection(); - d.watch(conn); - - PreparedStatement stmt = conn.prepareStatement(" " - + "SELECT DISTINCT \n" + - " outages.outageid, outages.iflostservice, outages.ifregainedservice, outages.nodeID, \n" + - " node.nodeLabel, \n" + - " outages.ipaddr, \n" + - " ipinterface.iphostname, \n" + - " service.servicename, \n" + - " outages.serviceId \n" + - " FROM outages \n" + - " JOIN ifservices \n" + - " ON ifservices.id = outages.ifserviceid \n" + - " JOIN ipinterface \n" + - " ON ipinterface.id = ifservices.ipinterfaceid \n" + - " JOIN node \n" + - " ON node.nodeid = ipinterface.nodeid \n" + - " JOIN service \n" + - " ON service.serviceid = ifservices.serviceid\n" + - " WHERE node.nodeid = ? \n" + - " AND (outages.ifregainedservice >= ? OR outages.ifregainedservice IS NULL) \n" + - "ORDER BY outages.iflostservice DESC"); - d.watch(stmt); - stmt.setInt(1, nodeId); - stmt.setTimestamp(2, new Timestamp(timeLong)); - ResultSet rs = stmt.executeQuery(); - d.watch(rs); - - outages = rs2Outages(rs); - } finally { - d.cleanUp(); - } - - return outages; - } - - /** - *

getOutagesForInterface

- * - * @param nodeId a int. - * @param ipInterface a {@link java.lang.String} object. - * @return an array of {@link org.opennms.web.outage.Outage} objects. - * @throws java.sql.SQLException if any. - */ - public Outage[] getOutagesForInterface(int nodeId, String ipInterface) throws SQLException { - Outage[] outages = new Outage[0]; - - try { - Connection conn = DataSourceFactory.getInstance().getConnection(); - d.watch(conn); - - PreparedStatement stmt = conn.prepareStatement(" " - + "SELECT DISTINCT \n" + - " outages.outageid, outages.iflostservice, outages.ifregainedservice, outages.nodeID, \n" + - " node.nodeLabel, \n" + - " outages.ipaddr, \n" + - " ipinterface.iphostname, \n" + - " service.servicename, \n" + - " outages.serviceId \n" + - " FROM outages \n" + - " JOIN ifservices \n" + - " ON ifservices.id = outages.ifserviceid \n" + - " JOIN ipinterface \n" + - " ON ipinterface.id = ifservices.ipinterfaceid \n" + - " JOIN node \n" + - " ON node.nodeid = ipinterface.nodeid \n" + - " JOIN service \n" + - " ON service.serviceid = ifservices.serviceid \n" + - " WHERE node.nodeid = ? \n" + - "ORDER BY iflostservice desc"); - d.watch(stmt); - stmt.setInt(1, nodeId); - stmt.setString(2, ipInterface); - ResultSet rs = stmt.executeQuery(); - d.watch(rs); - - outages = rs2Outages(rs); - } finally { - d.cleanUp(); - } - - return outages; - } - - /** - * Get all current outages and any resolved outages since the given time for - * the given interface. - * - * @param nodeId - * this is the node to query - * @param ipAddr - * this is the interface to query - * @param time - * no resolved outages older than this time will be returned - * @return All current outages and resolved outages no older than - * time. - * @throws java.sql.SQLException if any. - */ - public Outage[] getOutagesForInterface(int nodeId, String ipAddr, Date time) throws SQLException { - if (ipAddr == null || time == null) { - throw new IllegalArgumentException("Cannot take null parameters."); - } - - Outage[] outages = new Outage[0]; - long timeLong = time.getTime(); - - try { - Connection conn = DataSourceFactory.getInstance().getConnection(); - d.watch(conn); - - PreparedStatement stmt = conn.prepareStatement(" " - + "SELECT DISTINCT \n" + - " outages.outageid, outages.iflostservice, outages.ifregainedservice, outages.nodeID, \n" + - " node.nodeLabel, \n" + - " outages.ipaddr, \n" + - " ipinterface.iphostname, \n" + - " service.servicename, \n" + - " outages.serviceId \n" + - " FROM outages \n" + - " JOIN ifservices \n" + - " ON ifservices.id = outages.ifserviceid \n" + - " JOIN ipinterface \n" + - " ON ipinterface.id = ifservices.ipinterfaceid \n" + - " JOIN node \n" + - " ON node.nodeid = ipinterface.nodeid \n" + - " JOIN service \n" + - " ON service.serviceid = ifservices.serviceid \n" + - " WHERE node.nodeid = ? \n" + - " AND ipinterface.ipaddr = ? \n" + - " AND (outages.ifregainedservice >= ? OR outages.ifregainedservice IS NULL) \n" + - "ORDER BY outages.iflostservice desc"); - d.watch(stmt); - stmt.setInt(1, nodeId); - stmt.setString(2, ipAddr); - stmt.setTimestamp(3, new Timestamp(timeLong)); - ResultSet rs = stmt.executeQuery(); - d.watch(rs); - - outages = rs2Outages(rs); - } finally { - d.cleanUp(); - } - - return outages; - } - - /** - *

getOutagesForService

- * - * @param nodeId a int. - * @param ipInterface a {@link java.lang.String} object. - * @param serviceId a int. - * @return an array of {@link org.opennms.web.outage.Outage} objects. - * @throws java.sql.SQLException if any. - */ - public Outage[] getOutagesForService(int nodeId, String ipInterface, int serviceId) throws SQLException { - Outage[] outages = new Outage[0]; - - try { - Connection conn = DataSourceFactory.getInstance().getConnection(); - d.watch(conn); - - PreparedStatement stmt = conn.prepareStatement(" " - + "SELECT DISTINCT \n" + - " outages.outageid, outages.iflostservice, outages.ifregainedservice, outages.nodeID, \n" + - " node.nodeLabel, \n" + - " outages.ipaddr, \n" + - " ipinterface.iphostname, \n" + - " service.servicename, \n" + - " outages.serviceId \n" + - " FROM outages \n" + - " JOIN ifservices \n" + - " ON ifservices.id = outages.ifserviceid \n" + - " JOIN ipinterface \n" + - " ON ipinterface.id = ifservices.ipinterfaceid \n" + - " JOIN node \n" + - " ON node.nodeid = ipinterface.nodeid \n" + - " JOIN service \n" + - " ON service.serviceid = ifservices.serviceid \n" + - " WHERE node.nodeid = ? \n" + - "ORDER BY outages.iflostservice desc"); - d.watch(stmt); - stmt.setInt(1, nodeId); - stmt.setString(2, ipInterface); - stmt.setInt(3, serviceId); - ResultSet rs = stmt.executeQuery(); - d.watch(rs); - - outages = rs2Outages(rs); - } finally { - d.cleanUp(); - } - - return outages; - } - - /** - * Get all current outages and any resolved outages since the given time for - * the given service. - * - * @param nodeId - * this is the node to query - * @param ipAddr - * this is the interface to query - * @param serviceId - * this is the service to query - * @param time - * no resolved outages older than this time will be returned - * @return All current outages and resolved outages no older than - * time. - * @throws java.sql.SQLException if any. - */ - public Outage[] getOutagesForService(int nodeId, String ipAddr, int serviceId, Date time) throws SQLException { - if (ipAddr == null || time == null) { - throw new IllegalArgumentException("Cannot take null parameters."); - } - - Outage[] outages = new Outage[0]; - long timeLong = time.getTime(); - - try { - Connection conn = DataSourceFactory.getInstance().getConnection(); - d.watch(conn); - - PreparedStatement stmt = conn.prepareStatement(" " - + "SELECT DISTINCT \n" + - " outages.outageid, outages.iflostservice, outages.ifregainedservice, outages.nodeID, \n" + - " node.nodeLabel, \n" + - " outages.ipaddr, \n" + - " ipinterface.iphostname, \n" + - " service.servicename, \n" + - " outages.serviceId \n" + - " FROM outages \n" + - " JOIN ifservices \n" + - " ON ifservices.id = outages.ifserviceid \n" + - " JOIN ipinterface \n" + - " ON ipinterface.id = ifservices.ipinterfaceid \n" + - " JOIN node \n" + - " ON node.nodeid = ipinterface.nodeid \n" + - " JOIN service \n" + - " ON service.serviceid = ifservices.serviceid \n" + - " WHERE node.nodeid = ? \n" + - " AND ipinterface.ipaddr= ? \n" + - " AND service.serviceid = ? \n" + - " AND (outages.ifregainedservice >= ? OR outages.ifregainedservice IS NULL) \n" + - "ORDER BY outages.iflostservice DESC"); - d.watch(stmt); - stmt.setInt(1, nodeId); - stmt.setString(2, ipAddr); - stmt.setInt(3, serviceId); - stmt.setTimestamp(4, new Timestamp(timeLong)); - ResultSet rs = stmt.executeQuery(); - d.watch(rs); - - outages = rs2Outages(rs); - } finally { - d.cleanUp(); - } - - return outages; - } - - /** - * Return a list of IP addresses, the number of services down on each IP - * address, and the longest time a service has been down for each IP - * address. The list will be sorted in ascending order from the service down - * longest to the service down shortest. - * - * @return an array of {@link org.opennms.netmgt.model.outage.OutageSummary} objects. - * @throws java.sql.SQLException if any. - */ - public OutageSummary[] getCurrentOutageSummaries() throws SQLException { - OutageSummary[] summaries = new OutageSummary[0]; - - try { - Connection conn = DataSourceFactory.getInstance().getConnection(); - d.watch(conn); - - Statement stmt = conn.createStatement(); - d.watch(stmt); - //FIXME: This query is questionable at best but rewritten to follow relationships - ResultSet rs = stmt.executeQuery(" " - + "SELECT DISTINCT \n" + - " node.nodeid, max(outages.iflostservice) as timeDown, \n" + - " node.nodelabel, \n" + - " now() as timeNow \n" + - " FROM node \n" + - " JOIN ipinterface \n" + - " ON ipinterface.nodeid = node.nodeid \n" + - " JOIN ifservices \n" + - " ON ifservices.ipinterfaceid = ipinterface.id \n" + - " JOIN outages \n" + - " ON outages.ifserviceid = ifservices.id\n" + - " WHERE outages.ifregainedservice IS NULL \n" + - " AND node.nodeType != 'D' \n" + - " AND ipinterface.ismanaged != 'D' \n" + - " AND ifservices.status != 'D' \n" + - "GROUP BY node.nodeid, node.nodelabel \n" + - "ORDER BY timeDown DESC"); - d.watch(rs); - - List list = new ArrayList(); - - while (rs.next()) { - int nodeId = rs.getInt("nodeID"); - Timestamp timeDownTS = rs.getTimestamp("timeDown"); - long timeDown = timeDownTS.getTime(); - Date downDate = new Date(timeDown); - String nodeLabel = rs.getString("nodelabel"); - Date now = new Date(rs.getTimestamp("timeNow").getTime()); - - list.add(new OutageSummary(nodeId, nodeLabel, downDate, null, now)); - } - - summaries = list.toArray(new OutageSummary[list.size()]); - } finally { - d.cleanUp(); - } - - return summaries; - } /** * Return a list of IP addresses, the number of services down on each IP @@ -741,19 +114,20 @@ public OutageSummary[] getCurrentOutageSummaries() throws SQLException { * @return an array of {@link org.opennms.netmgt.model.outage.OutageSummary} objects. * @throws java.sql.SQLException if any. */ - public OutageSummary[] getAllOutageSummaries(Date date) throws SQLException { + public static OutageSummary[] getAllOutageSummaries(Date date) throws SQLException { OutageSummary[] summaries = new OutageSummary[0]; + final DBUtils d = new DBUtils(OutageModel.class); + try { Connection conn = DataSourceFactory.getInstance().getConnection(); d.watch(conn); PreparedStatement stmt = conn.prepareStatement( - "SELECT DISTINCT outages.nodeid, outages.iflostservice as timeDown, outages.ifregainedservice as timeUp, node.nodelabel " + "SELECT DISTINCT node.nodeid, outages.iflostservice as timeDown, outages.ifregainedservice as timeUp, node.nodelabel " + "FROM outages, node, ipinterface, ifservices " - + "WHERE node.nodeid=outages.nodeid AND ipinterface.nodeid=outages.nodeid AND ifservices.nodeid=outages.nodeid " - + "AND ipinterface.ipaddr=outages.ipaddr AND ifservices.ipaddr=outages.ipaddr " - + "AND ifservices.serviceid=outages.serviceid " + + "WHERE node.nodeid=ipinterface.nodeid " + + "AND ipinterface.id=ifservices.ipinterfaceid AND ifservices.id=outages.ifserviceid " + "AND node.nodeType != 'D' " + "AND ipinterface.ismanaged != 'D' " + "AND ifservices.status != 'D' " @@ -792,79 +166,6 @@ public OutageSummary[] getAllOutageSummaries(Date date) throws SQLException { return summaries; } - /** - * Return a list of IP addresses, the number of services down on each IP - * address, and the longest time a service has been down for each IP - * address. The list will be sorted in ascending order from the service down - * longest to the service down shortest. This is a clone of - * getCurrentOutageSummaries for Harrah's (special consideration). - * - * @return an array of {@link org.opennms.netmgt.model.outage.OutageSummary} objects. - * @throws java.sql.SQLException if any. - */ - public OutageSummary[] getCurrentSDSOutageSummaries() throws SQLException { - OutageSummary[] summaries = new OutageSummary[0]; - - try { - Connection conn = DataSourceFactory.getInstance().getConnection(); - d.watch(conn); - - Statement stmt = conn.createStatement(); - d.watch(stmt); - ResultSet rs = stmt.executeQuery(" " - + "SELECT DISTINCT \n" + - " outages.nodeid, max(outages.iflostservice) AS timeDown, \n" + - " node.nodelabel \n" + - " FROM outages \n" + - " JOIN ifservices \n" + - " ON ifservices.id = outages.ifserviceid \n" + - " JOIN ipinterface \n" + - " ON ipinterface.id = ifservices.ipinterfaceid \n" + - " JOIN node \n" + - " ON node.nodeid = ipinterface.nodeid \n" + - " JOIN assets \n" + - " ON assets.nodeid = node.nodeid \n" + - " WHERE outages.ifregainedservice IS NULL \n" + - " and node.nodeType != 'D' \n" + - " and ipinterface.ismanaged != 'D' \n" + - " and ifservices.status != 'D' \n" + - " and assets.displaycategory != 'SDS-A-Side' \n" + - " and assets.displaycategory != 'SDS-B-Side' \n" + - "group by outages.nodeid, node.nodelabel \n" + - "order by timeDown desc;"); - d.watch(rs); - - List list = new ArrayList(); - - while (rs.next()) { - int nodeId = rs.getInt("nodeID"); - Timestamp timeDownTS = rs.getTimestamp("timeDown"); - long timeDown = timeDownTS.getTime(); - Date downDate = new Date(timeDown); - String nodeLabel = rs.getString("nodelabel"); - - list.add(new OutageSummary(nodeId, nodeLabel, downDate)); - } - - summaries = list.toArray(new OutageSummary[list.size()]); - } finally { - d.cleanUp(); - } - - return summaries; - } - - /** - *

rs2Outages

- * - * @param rs a {@link java.sql.ResultSet} object. - * @return an array of {@link org.opennms.web.outage.Outage} objects. - * @throws java.sql.SQLException if any. - */ - protected static Outage[] rs2Outages(ResultSet rs) throws SQLException { - return rs2Outages(rs, true); - } - /** *

rs2Outages

* @@ -873,7 +174,7 @@ protected static Outage[] rs2Outages(ResultSet rs) throws SQLException { * @return an array of {@link org.opennms.web.outage.Outage} objects. * @throws java.sql.SQLException if any. */ - protected static Outage[] rs2Outages(ResultSet rs, boolean includesRegainedTime) throws SQLException { + private static Outage[] rs2Outages(ResultSet rs, boolean includesRegainedTime) throws SQLException { return rs2Outages(rs, includesRegainedTime, false); } @@ -890,7 +191,7 @@ protected static Outage[] rs2Outages(ResultSet rs, boolean includesRegainedTime) * @return an array of {@link org.opennms.web.outage.Outage} objects. * @throws java.sql.SQLException if any. */ - protected static Outage[] rs2Outages(ResultSet rs, boolean includesRegainedTime, boolean includesNotifications) throws SQLException { + private static Outage[] rs2Outages(ResultSet rs, boolean includesRegainedTime, boolean includesNotifications) throws SQLException { Outage[] outages = null; List list = new ArrayList(); diff --git a/opennms-webapp/src/main/java/org/opennms/web/rss/OutageFeed.java b/opennms-webapp/src/main/java/org/opennms/web/rss/OutageFeed.java index 1a90171f8cec..7ca86511949a 100644 --- a/opennms-webapp/src/main/java/org/opennms/web/rss/OutageFeed.java +++ b/opennms-webapp/src/main/java/org/opennms/web/rss/OutageFeed.java @@ -91,10 +91,9 @@ public SyndFeed getFeed() { List entries = new ArrayList(); try { - OutageModel model = new OutageModel(); Date date = new Date(); date.setTime(date.getTime() - (1000 * 60 * 60 * 24)); - OutageSummary[] summaries = model.getAllOutageSummaries(date); + OutageSummary[] summaries = OutageModel.getAllOutageSummaries(date); SyndEntry entry; diff --git a/opennms-webapp/src/main/webapp/includes/interfaceAvailability-box.jsp b/opennms-webapp/src/main/webapp/includes/interfaceAvailability-box.jsp index 6118de85d859..dd0bb32f7327 100644 --- a/opennms-webapp/src/main/webapp/includes/interfaceAvailability-box.jsp +++ b/opennms-webapp/src/main/webapp/includes/interfaceAvailability-box.jsp @@ -106,7 +106,7 @@ String timelineHeaderUrl = "/opennms/rest/timeline/header/" + timelineStart + "/" + timelineEnd + "/"; String timelineEmptyUrl = "/opennms/rest/timeline/empty/" + timelineStart + "/" + timelineEnd + "/"; - Outage[] outages = new OutageModel().getCurrentOutagesForNode(nodeId); + Outage[] outages = OutageModel.getCurrentOutagesForNode(nodeId); %>
diff --git a/opennms-webapp/src/main/webapp/includes/nodeAvailability-box.jsp b/opennms-webapp/src/main/webapp/includes/nodeAvailability-box.jsp index 5eec6c847de6..6fa224954bae 100644 --- a/opennms-webapp/src/main/webapp/includes/nodeAvailability-box.jsp +++ b/opennms-webapp/src/main/webapp/includes/nodeAvailability-box.jsp @@ -92,7 +92,7 @@ String timelineHeaderUrl = "/opennms/rest/timeline/header/" + timelineStart + "/" + timelineEnd + "/"; String timelineEmptyUrl = "/opennms/rest/timeline/empty/" + timelineStart + "/" + timelineEnd + "/" ; - Outage[] outages = new OutageModel().getCurrentOutagesForNode(nodeId); + Outage[] outages = OutageModel.getCurrentOutagesForNode(nodeId); %>
diff --git a/opennms-webapp/src/main/webapp/includes/serviceAvailability-box.jsp b/opennms-webapp/src/main/webapp/includes/serviceAvailability-box.jsp index c92be3541091..5ee312a97993 100644 --- a/opennms-webapp/src/main/webapp/includes/serviceAvailability-box.jsp +++ b/opennms-webapp/src/main/webapp/includes/serviceAvailability-box.jsp @@ -109,7 +109,7 @@ int nodeId = service.getNodeId(); String ipAddr = service.getIpAddress(); - Outage[] outages = new OutageModel().getCurrentOutagesForNode(nodeId); + Outage[] outages = OutageModel.getCurrentOutagesForNode(nodeId); String warnClass = "Normal"; diff --git a/opennms-webapp/src/test/java/org/opennms/web/outage/OutageModelTest.java b/opennms-webapp/src/test/java/org/opennms/web/outage/OutageModelTest.java new file mode 100644 index 000000000000..f239ce94280b --- /dev/null +++ b/opennms-webapp/src/test/java/org/opennms/web/outage/OutageModelTest.java @@ -0,0 +1,89 @@ +/******************************************************************************* + * This file is part of OpenNMS(R). + * + * Copyright (C) 2014 The OpenNMS Group, Inc. + * OpenNMS(R) is Copyright (C) 1999-2014 The OpenNMS Group, Inc. + * + * OpenNMS(R) is a registered trademark of The OpenNMS Group, Inc. + * + * OpenNMS(R) is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published + * by the Free Software Foundation, either version 3 of the License, + * or (at your option) any later version. + * + * OpenNMS(R) is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with OpenNMS(R). If not, see: + * http://www.gnu.org/licenses/ + * + * For more information contact: + * OpenNMS(R) Licensing + * http://www.opennms.org/ + * http://www.opennms.com/ + *******************************************************************************/ + +package org.opennms.web.outage; + +import java.sql.SQLException; +import java.util.Date; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.opennms.core.db.DataSourceFactory; +import org.opennms.core.test.MockLogAppender; +import org.opennms.core.test.OpenNMSJUnit4ClassRunner; +import org.opennms.core.test.db.MockDatabase; +import org.opennms.core.test.db.TemporaryDatabaseAware; +import org.opennms.core.test.db.annotations.JUnitTemporaryDatabase; +import org.opennms.netmgt.dao.support.NullRrdStrategy; +import org.opennms.netmgt.rrd.RrdUtils; +import org.opennms.test.JUnitConfigurationEnvironment; +import org.opennms.test.mock.MockUtil; +import org.springframework.test.context.ContextConfiguration; + +@RunWith(OpenNMSJUnit4ClassRunner.class) +@ContextConfiguration(locations={ + "classpath:/META-INF/opennms/applicationContext-soa.xml", + "classpath:/META-INF/opennms/applicationContext-commonConfigs.xml", + "classpath:/META-INF/opennms/applicationContext-minimal-conf.xml", + "classpath:/META-INF/opennms/applicationContext-mockDao.xml", + "classpath*:/META-INF/opennms/component-dao.xml" +}) +@JUnitConfigurationEnvironment +@JUnitTemporaryDatabase(tempDbClass=MockDatabase.class,reuseDatabase=false) +public class OutageModelTest implements TemporaryDatabaseAware { + + private MockDatabase m_db; + + @Override + public void setTemporaryDatabase(MockDatabase database) { + m_db = database; + } + + @Before + public void setUp() throws Exception { + MockUtil.println("------------ Begin Test --------------------------"); + MockLogAppender.setupLogging(); + + DataSourceFactory.setInstance(m_db); + + RrdUtils.setStrategy(new NullRrdStrategy()); + } + + @After + public void tearDown() throws Exception { + MockUtil.println("------------ End Test --------------------------"); + } + + @Test + public void test() throws SQLException { + OutageModel.getAllOutageSummaries(new Date()); + OutageModel.getCurrentOutagesForNode(1); + } +}