datamanager.java

来自「opennms得相关源码 请大家看看」· Java 代码 · 共 1,523 行 · 第 1/4 页

JAVA
1,523
字号
//// This file is part of the OpenNMS(R) Application.//// OpenNMS(R) is Copyright (C) 2002-2003 The OpenNMS Group, Inc.  All rights reserved.// OpenNMS(R) is a derivative work, containing both original code, included code and modified// code that was published under the GNU General Public License. Copyrights for modified // and included code are below.//// OpenNMS(R) is a registered trademark of The OpenNMS Group, Inc.//// Modifications://// 2004 Oct 07: Added code to support RTC rescan on asset update// 2004 Sep 08: Cleaned up the rescan node method.// 2004 Mar 17: Fixed a number of bugs with added and deleting services within RTC.//              Added a method to rescan a node within RTC.// 2003 Jan 31: Cleaned up some unused imports.// 2002 Oct 24: Replaced references to HashTable with HashMap.//// Original code base Copyright (C) 1999-2001 Oculan Corp.  All rights reserved.//// This program is free software; you can redistribute it and/or modify// it under the terms of the GNU General Public License as published by// the Free Software Foundation; either version 2 of the License, or// (at your option) any later version.//// This program 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 General Public License for more details.                                                            //// You should have received a copy of the GNU General Public License// along with this program; if not, write to the Free Software// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.//       // For more information contact: //      OpenNMS Licensing       <license@opennms.org>//      http://www.opennms.org///      http://www.opennms.com///// Tab Size = 8//package org.opennms.netmgt.rtc;import java.io.IOException;import java.lang.reflect.UndeclaredThrowableException;import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Timestamp;import java.util.ArrayList;import java.util.Enumeration;import java.util.HashMap;import java.util.Iterator;import java.util.List;import java.util.ListIterator;import java.util.Map;import org.apache.log4j.Category;import org.exolab.castor.xml.MarshalException;import org.exolab.castor.xml.ValidationException;import org.opennms.core.utils.ThreadCategory;import org.opennms.netmgt.EventConstants;import org.opennms.netmgt.config.CatFactory;import org.opennms.netmgt.config.CategoryFactory;import org.opennms.netmgt.config.DatabaseConnectionFactory;import org.opennms.netmgt.config.categories.Categories;import org.opennms.netmgt.config.categories.Categorygroup;import org.opennms.netmgt.filter.Filter;import org.opennms.netmgt.filter.FilterParseException;import org.opennms.netmgt.rtc.datablock.RTCCategory;import org.opennms.netmgt.rtc.datablock.RTCHashMap;import org.opennms.netmgt.rtc.datablock.RTCNode;import org.opennms.netmgt.rtc.datablock.RTCNodeKey;import org.xml.sax.SAXException;/** * Contains and maintains all the data for the RTC. *  * The basic datablock is a 'RTCNode' that gets added to relevant * 'RTCCategory's. it also gets added to a map with different keys for easy * lookup *  * The map('RTCHashMap') is keyed with 'RTCNodeKey's(a nodeid/ip/svc * combination), nodeid/ip combinations and nodeid and these keys either lookup * a single RTCNode or lists of 'RTCNode's *  * Incoming events have a method in the DataManager to alter data - for e.g., a * 'nodeGainedService' event would result in the 'nodeGainedService()' method * being called by the DataUpdater(s). *  * @author <A HREF="mailto:sowmya@opennms.org">Sowmya Nataraj </A> * @author <A HREF="http://www.opennms.org">OpenNMS.org </A> */public class DataManager extends Object {    /**     * The RTC categories     */    private Map m_categories;    /**     * map keyed using the RTCNodeKey or nodeid or nodeid/ip     */    private RTCHashMap m_map;    /**     * The service table map - this is built at startup and updated if a     * servicename that is not found in the map is found so as to avoid a     * database lookup for each servicename to serviceid mapping     */    private HashMap m_serviceTableMap;    /**     * Get the 'ismanaged' status for the nodeid, ipaddr combination     *      * @param nodeid     *            the nodeid of the interface     * @param ip     *            the interface for which the status is required     * @param svc     *            the service for which status is required     *      * @return the 'status' from the ifservices table     */    private char getServiceStatus(long nodeid, String ip, String svc) {        //        // check the 'status' flag        //        char status = '\0';        ResultSet statusRS = null;        Connection dbConn = null;        try {            dbConn = DatabaseConnectionFactory.getInstance().getConnection();            // Prepare statement to get the 'status' flag for a            // nodeid/IP/service            PreparedStatement svcStatusGetStmt = dbConn.prepareStatement(RTCConstants.DB_GET_SERVICE_STATUS);            svcStatusGetStmt.setLong(1, nodeid);            svcStatusGetStmt.setString(2, ip);            svcStatusGetStmt.setLong(3, getServiceID(svc));            statusRS = svcStatusGetStmt.executeQuery();            if (statusRS.next()) {                String statusStr = statusRS.getString(1);                status = statusStr.charAt(0);            }            // close statement            svcStatusGetStmt.close();        } catch (SQLException ipe) {            Category log = ThreadCategory.getInstance(DataManager.class);            log.warn("Error reading status for: " + nodeid + "/" + ip + "/" + svc, ipe);            status = '\0';        } finally {            try {                if (statusRS != null)                    statusRS.close();            } catch (Exception e) {                Category log = ThreadCategory.getInstance(DataManager.class);                if (log.isDebugEnabled())                    log.debug("Exception while closing the service status result set", e);            }            try {                if (dbConn != null)                    dbConn.close();            } catch (SQLException e) {                ThreadCategory.getInstance(getClass()).warn("Exception closing JDBC connection", e);            }        }        return status;    }    /**     * Add a node/ip/service to the specified category.     *      * @param nodeid     *            the nodeid to be added     * @param ip     *            the interface to be added     * @param svcname     *            the service to be added     * @param cat     *            the category to which this node is to be added to     * @param knownIPs     *            the hashtable of IP->list of RTCNodes (used only at startup)     * @param outagesGetStmt     *            the prepared statement to read outages     */    private void addNodeIpSvcToCategory(long nodeid, String ip, String svcname, RTCCategory cat, HashMap knownIPs, PreparedStatement outagesGetStmt) {        Category log = ThreadCategory.getInstance(DataManager.class);        //        // check if the node is already part of the tree, if yes,        // simply add the current category information        //        RTCNodeKey key = new RTCNodeKey(nodeid, ip, svcname);        RTCNode rtcN = (RTCNode) m_map.get(key);        if (rtcN != null) {            // add the category info to the node            rtcN.addCategory(cat.getLabel());            // Add node to category            cat.addNode(rtcN);            if (log.isDebugEnabled())                log.debug("rtcN : " + rtcN.getNodeID() + "/" + rtcN.getIP() + "/" + rtcN.getSvcName() + " added to cat: " + cat.getLabel());            return;        }        // create the node        rtcN = new RTCNode(nodeid, ip, svcname);        // add the category info to the node        rtcN.addCategory(cat.getLabel());        // read outages        //        // the window for which outages are to be read - the current        // time minus the rollingWindow        //        long window = (new java.util.Date()).getTime() - RTCManager.getRollingWindow();        Timestamp windowTS = new Timestamp(window);        //        // Read closed outages in the above window and outages that are        // still open        //        ResultSet outRS = null;        try {            //            // get outages            //            outagesGetStmt.setLong(1, nodeid);            outagesGetStmt.setString(2, ip);            outagesGetStmt.setLong(3, getServiceID(svcname));            outagesGetStmt.setTimestamp(4, windowTS);            outagesGetStmt.setTimestamp(5, windowTS);            outRS = outagesGetStmt.executeQuery();            while (outRS.next()) {                int outColIndex = 1;                Timestamp lostTimeTS = outRS.getTimestamp(outColIndex++);                Timestamp regainedTimeTS = outRS.getTimestamp(outColIndex++);                long lostTime = lostTimeTS.getTime();                long regainedTime = -1;                if (regainedTimeTS != null)                    regainedTime = regainedTimeTS.getTime();                if (log.isDebugEnabled()) {                    log.debug("lost time for nodeid/ip/svc: " + nodeid + "/" + ip + "/" + svcname + ": " + lostTimeTS + "/" + lostTime);                    log.debug("regained time for nodeid/ip/svc: " + nodeid + "/" + ip + "/" + svcname + ": " + regainedTimeTS + "/" + regainedTime);                }                rtcN.addSvcTime(lostTime, regainedTime);            }        } catch (SQLException sqle2) {            if (log.isDebugEnabled())                log.debug("Error getting outages information for nodeid: " + nodeid + "\tip:" + ip, sqle2);        } catch (Exception e2) {            if (log.isDebugEnabled())                log.debug("Unknown error while reading outages for nodeid: " + nodeid + "\tip: " + ip, e2);        } finally {            // finally close the result set            try {                if (outRS != null)                    outRS.close();            } catch (Exception e) {                if (log.isDebugEnabled())                    log.debug("Exception while closing the outages result set ", e);            }        }        // Add node to the map        m_map.put(key, rtcN);        // node key map        m_map.add(nodeid, rtcN);        // node and ip key map        m_map.add(nodeid, ip, rtcN);        // Add node to category        cat.addNode(rtcN);        // Add node to the knownIPs        if (knownIPs != null) {            List rtcS = (List) knownIPs.get(ip);            if (rtcS == null) {                rtcS = new ArrayList();                rtcS.add(rtcN);            } else {                if (!rtcS.contains(rtcN))                    rtcS.add(rtcN);            }        }        if (log.isDebugEnabled())            log.debug("rtcN : " + rtcN.getNodeID() + "/" + rtcN.getIP() + "/" + rtcN.getSvcName() + " added to cat: " + cat.getLabel());    }    /**     * Delete a node/ip/service to the specified category.     *      * Note: This will not delete the service, it will just remove the node from     * the category.     *      * @param nodeid     *            the nodeid to be added     * @param ip     *            the interface to be added     * @param svcname     *            the service to be added     * @param cat     *            the category to which this node is to be added to     */    private void delNodeIpSvcToCategory(long nodeid, String ip, String svcname, RTCCategory cat) {        Category log = ThreadCategory.getInstance(DataManager.class);        //        // check if the node is already part of the tree, if yes,        // simply remove the current category information        //        RTCNodeKey key = new RTCNodeKey(nodeid, ip, svcname);        RTCNode rtcN = (RTCNode) m_map.get(key);        if (rtcN != null) {            String catlabel = cat.getLabel();            // get nodes in this category            List catNodes = cat.getNodes();            // check if the category contains this node            Long tmpNodeid = new Long(rtcN.getNodeID());            int nIndex = catNodes.indexOf(tmpNodeid);            if (nIndex != -1) {                // remove from the category                catNodes.remove(nIndex);                log.info("Removing node from category: " + catlabel);                // let the node know that this category is out                rtcN.removeCategory(catlabel);            }        }        // allow for gc        rtcN = null;    }    /**     * Add the RTCNodes known for an IP to the category.     *      * @param ipRTCs     *            the list of RTCNodes related to a particular IP     * @param cat     *            the category to which the list is to be added     */    private void addIpToCategory(List ipRTCs, RTCCategory cat) {        if (ipRTCs == null)            return;        Category log = ThreadCategory.getInstance(DataManager.class);        Iterator rtcIter = ipRTCs.iterator();        while (rtcIter.hasNext()) {            RTCNode rtcN = (RTCNode) rtcIter.next();            // Check if this service is reqd. to be added for this category            String svcName = rtcN.getSvcName();

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?