nodelabel.java
来自「opennms得相关源码 请大家看看」· Java 代码 · 共 742 行 · 第 1/2 页
JAVA
742 行
//// 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://// 31 Jan 2003: Cleaned up some unused imports.//// 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.utils;import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;import java.util.ArrayList;import java.util.Iterator;import java.util.List;import org.apache.log4j.Category;import org.opennms.core.resource.Vault;import org.opennms.core.utils.ThreadCategory;import org.opennms.netmgt.PropertyConstants;import org.opennms.protocols.ip.IPv4Address;/** * <P> * This class contains convenience functions for retrieving and modifying the * label associated with a managed node. The 'node' table contains a 'nodelabel' * and 'nodelabelsource' field. The 'nodelabel' is a user-friendly name * associated with the node. This name can be user-defined (via the WEB UI) or * can be auto-generated based on what OpenNMS knows about the node and its * interfaces. The 'nodelabelsource' field is a single character flag which * indicates what the source for the node label was. * </P> * * <PRE> * * Valid values for node label source are: 'U' User defined 'H' Primary * interface's IP host name 'S' Node's MIB-II sysName 'A' Primary interface's IP * address * * </PRE> * * * @author <A HREF="mike@opennms.org">Mike </A> * @author <A HREF="http://www.opennms.org/">OpenNMS </A> * */public class NodeLabel { /** * The SQL statement to update the 'nodelabel' and 'nodelabelsource' fields * of 'node' table */ final static String SQL_DB_UPDATE_NODE_LABEL = "UPDATE node SET nodelabel=?,nodelabelsource=? WHERE nodeid=?"; /** * The SQL statement to retrieve the NetBIOS name associated with a * particular nodeID */ final static String SQL_DB_RETRIEVE_NETBIOS_NAME = "SELECT nodenetbiosname FROM node WHERE nodeid=?"; /** * The SQL statement to retrieve all managed IP address & hostName values * associated with a particular nodeID */ final static String SQL_DB_RETRIEVE_MANAGED_INTERFACES = "SELECT ipaddr,iphostname FROM ipinterface WHERE nodeid=? AND ismanaged='M'"; /** * The SQL statement to retrieve all non-managed IP address & hostName * values associated with a particular nodeID */ final static String SQL_DB_RETRIEVE_NON_MANAGED_INTERFACES = "SELECT ipaddr,iphostname FROM ipinterface WHERE nodeid=? AND ismanaged!='M'"; /** * The SQL statement to retrieve the MIB-II sysname field from the node * table */ final static String SQL_DB_RETRIEVE_SYSNAME = "SELECT nodesysname FROM node WHERE nodeid=?"; /** * The SQL statement to retrieve the current node label and node label * source values associated with a node. */ final static String SQL_DB_RETRIEVE_NODELABEL = "SELECT nodelabel,nodelabelsource FROM node WHERE nodeid=?"; /** * Valid values for node label source flag */ public final static char SOURCE_USERDEFINED = 'U'; public final static char SOURCE_NETBIOS = 'N'; public final static char SOURCE_HOSTNAME = 'H'; public final static char SOURCE_SYSNAME = 'S'; public final static char SOURCE_ADDRESS = 'A'; /** * Initalization value for node label source flag */ public final static char SOURCE_UNKNOWN = 'X'; /** * Maximum length for node label */ public final static int MAX_NODE_LABEL_LENGTH = 256; /** * Primary interface selection method MIN. Using this selection method the * interface with the smallest numeric IP address is considered the primary * interface. */ private final static String SELECT_METHOD_MIN = "min"; /** * Primary interface selection method MAX. Using this selection method the * interface with the greatest numeric IP address is considered the primary * interface. */ private final static String SELECT_METHOD_MAX = "max"; /** * Default primary interface select method. */ private final static String DEFAULT_SELECT_METHOD = SELECT_METHOD_MIN; /** * Node label */ private String m_nodeLabel; /** * Flag describing source of node label */ private char m_nodeLabelSource; /** * Default constructor */ public NodeLabel() { m_nodeLabel = null; m_nodeLabelSource = SOURCE_UNKNOWN; } /** * Constructor * * @param nodeLabel * Node label * @param nodeLabelSource * Flag indicating source of node label */ public NodeLabel(String nodeLabel, char nodeLabelSource) { m_nodeLabel = nodeLabel; m_nodeLabelSource = nodeLabelSource; } /** * Returns the node label . * * @return node label */ public String getLabel() { return m_nodeLabel; } /** * Returns the node label source flag . * * @return node label source flag */ public char getSource() { return m_nodeLabelSource; } /** * Sets the node label. * * @param nodeLabel * Node label */ public void setLabel(String nodeLabel) { m_nodeLabel = nodeLabel; } /** * Sets the node label source flag * * @param nodeLabelSource * Flag indicating source of node label */ public void setSource(char nodeLabelSource) { m_nodeLabelSource = nodeLabelSource; } /** * This method queries the 'node' table for the value of the 'nodelabel' and * 'nodelabelsource' fields for the node with the provided nodeID. A * NodeLabel object is returned initialized with the retrieved values. * * WARNING: A properly instantiated and initlaized Vault class object is * required prior to calling this method. This method will initially only be * called from the WEB UI. * * @param nodeID * Unique identifier of the node to be updated. * @return Object containing label and source values. */ public static NodeLabel retrieveLabel(int nodeID) throws SQLException { NodeLabel label = null; Connection dbConnection = Vault.getDbConnection(); try { label = retrieveLabel(nodeID, dbConnection); } finally { Vault.releaseDbConnection(dbConnection); } return label; } /** * This method queries the 'node' table for the value of the 'nodelabel' and * 'nodelabelsource' fields for the node with the provided nodeID. A * NodeLabel object is returned initialized with the retrieved values. * * @param nodeID * Unique ID of node whose label info is to be retrieved * @param dbConnection * SQL database connection * * @return object initialized with node label & source flag */ public static NodeLabel retrieveLabel(int nodeID, Connection dbConnection) throws SQLException { String nodeLabel = null; String nodeLabelSource = null; PreparedStatement stmt = dbConnection.prepareStatement(SQL_DB_RETRIEVE_NODELABEL); Category log = ThreadCategory.getInstance(NodeLabel.class); if (log.isDebugEnabled()) log.debug("NodeLabel.retrieveLabel: sql: " + SQL_DB_RETRIEVE_NODELABEL + " node id: " + nodeID); stmt.setInt(1, nodeID); try { // Issue database query ResultSet rs = stmt.executeQuery(); // Process result set, retrieve node's sysname if (rs.next()) { nodeLabel = rs.getString(1); nodeLabelSource = rs.getString(2); } rs.close(); } catch (SQLException sqlE) { throw sqlE; } finally { stmt.close(); } if (nodeLabelSource != null) { char[] temp = nodeLabelSource.toCharArray(); return new NodeLabel(nodeLabel, temp[0]); } else return new NodeLabel(nodeLabel, SOURCE_UNKNOWN); } /** * This method updates the 'nodelabel' and 'nodelabelsource' fields of the * 'node' table for the specified nodeID. A database connection is retrieved * from the Vault. * * WARNING: A properly instantiated and initlaized Vault class object is * required prior to calling this method. This method will initially only be * called from the WEB UI. * * @param nodeID * Unique identifier of the node to be updated. * @param nodeLabel * Object containing label and source values. */ public static void assignLabel(int nodeID, NodeLabel nodeLabel) throws SQLException { Connection dbConnection = Vault.getDbConnection(); try { assignLabel(nodeID, nodeLabel, dbConnection); } finally { Vault.releaseDbConnection(dbConnection); } } /** * This method updates the 'nodelabel' and 'nodelabelsource' fields of the * 'node' table for the specified nodeID. * * If nodeLabel parameter is NULL the method will first call computeLabel() * and use the resulting NodeLabel object to update the database. * * @param nodeID * Unique identifier of the node to be updated. * @param nodeLabel * Object containing label and source values. * @param dbConnection * SQL database connection */ public static void assignLabel(int nodeID, NodeLabel nodeLabel, Connection dbConnection) throws SQLException { Category log = ThreadCategory.getInstance(NodeLabel.class); if (nodeLabel == null) { if (log.isDebugEnabled()) log.debug("NodeLabel.assignLabel: nodeLabel obj is null, computing label..."); nodeLabel = computeLabel(nodeID, dbConnection); } // Issue SQL update to assign the 'nodelabel' && 'nodelabelsource' // fields // of the 'node' table PreparedStatement stmt = dbConnection.prepareStatement(SQL_DB_UPDATE_NODE_LABEL); int column = 1; // Node Label if (log.isDebugEnabled()) log.debug("NodeLabel.assignLabel: Node label: " + nodeLabel.getLabel() + " source: " + nodeLabel.getSource()); if (nodeLabel.getLabel() != null) { // nodeLabel may not exceed MAX_NODELABEL_LEN.if it does truncate it String label = nodeLabel.getLabel(); if (label.length() > MAX_NODE_LABEL_LENGTH) label = label.substring(0, MAX_NODE_LABEL_LENGTH); stmt.setString(column++, label); } else stmt.setNull(column++, java.sql.Types.VARCHAR); // Node Label Source stmt.setString(column++, String.valueOf(nodeLabel.getSource())); // Node ID stmt.setInt(column++, nodeID); try { // Issue database update
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?