com.adventnet.snmp.beans
Class SnmpTarget

java.lang.Object
  |
  +--com.adventnet.snmp.beans.SnmpServer
        |
        +--com.adventnet.snmp.beans.SnmpTarget
Direct Known Subclasses:
SnmpPoller, SnmpTable

public class SnmpTarget
extends SnmpServer
implements java.beans.VetoableChangeListener, CustomizerTemplate

This class is a lightweight SNMP session for use with other beans in applications and applets. It sends synchronous SNMP requests. It contains an array of OIDs specifying variables used in SNMP operations, and list of values for set and trap operations. MIB support and other SNMP functions are built into this class. The SnmpTarget bean is used for many of the common operations that we do with a SNMP agent such as get, getnext, set and trap operations. MIB support and other SNMP functions are built into this class. This bean can be used to make a single variable request, multiple variable requests, walking a table and a number of operations, including loading MIBs to support SNMP operations, getting all instances of a variable type, etc

To use in an application, simply instantiate, specify the object IDs, and perform operations. For example,

    SnmpTarget target = new SnmpTarget();
    target.setTargetHost("localhost");  // set host, or other parameters
    target.setObjectID(".1.3.6.1.2.1.1.1.0"); // or 1.1.0 with standard prefix
    String result = target.snmpGet();
    
if (result == null) System.err.println("Failed: "+target.getErrorString()); else System.out.println("Response: "+result);
To use in an applet use an alternate constructor, but the procedure is the same. For example,
    SnmpTarget target = new SnmpTarget(appletInstance);
    target.setTargetHost("localhost");  // set host, or other parameters
    target.setObjectID(".1.3.6.1.2.1.1.1.0");  // or 1.1.0 with standard prefix
    String result = target.snmpGet();
    
if (result == null) System.err.println("Failed: "+target.getErrorString()); else System.out.println("Response: "+result);
This class supports multiple variable requests, and a number of operations, including loading MIBs to support SNMP operations, getting all instances of a variable type, etc..

For multiple variable requests, set the OID list using the setObjectIDList(String[]) or setSnmpOIDList(SnmpOID[]) methods. Your SNMP operations with this SnmpTarget will use this list.

We can also register for events from the SnmpTarget component, so that we can notify another object whenever a response is received for any request. This may be useful for example in applying the result of a query to multiple objects. However, in most cases, the synchronous use of this component will not use events and event registration. The listeners that are registered for receiving responses from the agent should register, using setSendTimeoutEvents(true) to get the timeout events, which otherwise, will not be passed to the listener. This class can be used directly, and it will also be used by beans, e.g. SnmpPoller bean, which provides SNMP services. For garbage collection of the resources used, releaseResources() method should be invoked. Different SnmpTarget instances on the same JVM, will reuse the SnmpAPI, SnmpSession and MibOperations instance. So, for releasing these resources, releaseResources() method should be called on every SnmpTarget instance created and when this method is called on the final instance of SnmpTarget, these will be garbage collected.

See Also:
Serialized Form

Field Summary
(package private)  boolean ackFlag
           
(package private)  java.util.Vector debugListeners
           
(package private) static int[] genericTrap
           
(package private) static int[][] genericTraps
          The trap oids corresponding to generic traps
(package private)  int informReqID
           
(package private)  java.lang.String mibModules
           
 boolean serialize
          based on this flag v3 table like USMUserTable and SnmpEngineTable will be serialized
static byte SNMPGET
          Constant for SNMP get request.
static byte SNMPGETBULK
          Constant for SNMP V2 Get Bulk request.
static byte SNMPGETNEXT
          Constant for SNMP get next request.
static byte SNMPINFORM
          Constant for SNMP V2 INFORM request.
static byte SNMPREPORT
          Constant for SNMP V2 Report request.
static byte SNMPRESPONSE
          Constant for SNMP response request
static byte SNMPSET
          Constant for SNMP set request.
static byte SNMPTRAP
          Constant for SNMP trap request.
(package private) static int[] snmpTrapEnterprise
           
(package private) static int[] snmpTrapOid
           
static byte SNMPV2TRAP
          Constant for SNMP V2 Trap request.
(package private) static int[] upTime
          General-purpose oid for traps.
static int VERSION1
          Constant for SNMP Version 1.
static int VERSION2C
          Constant for SNMP Version 2C.
static int VERSION3
          Constant for SNMP Version 3.
 
Fields inherited from class com.adventnet.snmp.beans.SnmpServer
api, applet, ASN1_ERROR_ERR, ASN1_ERROR_OID, attemptCompleteData, attemptPartialData, AUTH_NO_PRIV, AUTH_PRIV, authKey, authPassword, authProtocol, broadCast, clientID, community, contextID, contextName, count, debugon, DECRYPT_ERROR_ERR, DECRYPT_ERROR_OID, discoveryTimeSyncListener, enc, engineID, engineTable, error, errorCode, errorcodes, errorIndex, exceptionCode, exceptionCodes, finalize_called, group, group_oidList, isName, isNotSet, isSendPDUByGroup, listeners, localAddrs, localPort, logFlag, loggerImpl, logLevel, logType, matchServ, matchtable, MAX_NUM_ROWS, maxRepetitions, MD5_AUTH, mibOps, NO_AUTH, NO_AUTH_NO_PRIV, nonRepeaters, NOT_TIME_WINDOWS_ERR, NOT_TIME_WINDOWS_OID, nullsession_targets, oidList, options, pduErrorIndex, pollertargets, pollInterval, port, port_targets, principal, printStr, privKey, privPassword, privProtocol, propertylisteners, protocol, reqHandler, reqid, retries, secModel, securityLevel, sendTimeoutEvents, sentreqid, session, session_port_targets, session_targets, sessionName, SHA_AUTH, SNMPGET, SNMPGETBULK, SNMPGETNEXT, SNMPINFORM, SNMPREPORT, SNMPRESPONSE, SNMPSET, snmpStore, SNMPTRAP, SNMPV2TRAP, targetHost, targets, timeout, UKN_ENGINEID_ERR, UKN_ENGINEID_OID, UKN_USERNAMES_ERR, UKN_USERNAMES_OID, UNSUPP_SEC_LEVEL_ERR, UNSUPP_SEC_LEVEL_OID, userError, userErrorCode, usmTable, v3ErrorTable, validateUser, varbinds_per_request, varList, version, VERSION1, VERSION2C, VERSION3, writeCommunity, WRONG_DIGEST_ERR, WRONG_DIGEST_OID
 
Constructor Summary
SnmpTarget()
          This constructor is used in applications.
SnmpTarget(java.applet.Applet applet)
          Use this constructor for applets.
SnmpTarget(java.applet.Applet applet, int port, java.lang.String session)
          Use this constructor for creating a new session for this applet.
SnmpTarget(int protocol, ProtocolOptions options)
          This constructor is used for applications which use protocol other than UDP/IP.
SnmpTarget(int port, java.lang.String session)
          This constructor is used for creating a new session for this application.
SnmpTarget(SnmpSession ses, MibOperations mib)
          This constructor is used to create an SnmpTarget instance with a specified SnmpSession and MibOperations.
SnmpTarget(java.lang.String[] localAddrs, java.lang.String sessionName)
          Use this constructor for applications in which the session needs to be bound to some local address.
 
Method Summary
 void addLogClient(com.adventnet.afp.log.LoggerProperties loggerProps)
          Adds the Log client for which the target has to redirect the log messages.
 void addLogClient(com.adventnet.utils.LogInterface client)
          Add the Log client for which the target has to redirect the log messages
 int addObjectID(java.lang.String s)
          This method adds an SnmpOID specified by string to the SnmpOID list.
 void addResultListener(ResultListener l)
          Adds a listener for events from this target.
 int addSnmpOID(SnmpOID oid)
          This method adds an SnmpOID to the previously set list of SnmpOID
 void addSnmpPropertyListener(SnmpPropertyListener l)
          Adds SnmpPropertyListener to the SnmpPropertyListener list.
 void addUserError(int errorCode, java.lang.String errorString)
          This method is used to add user defined error codes and the corresponding error string.
 void addV3HandShakeResultListener(V3HandShakeResultListener l)
          Adds a listener to receive the events when SnmpEngineID discovery and time Synchronization is performed by the method async_create_v3_tables.
 void enableDebugForPDU(boolean setonpdu)
          The debug can be set on the PDU instance itself using this method.
protected  void genEvent(SnmpPDU pdu)
          Generates the events to the listeners
protected  void genTimeoutEvent(SnmpPDU pdu)
          Generates the events to the listeners
 boolean getAttemptComplete()
          Returns if the splitting pdu is enabled to get the complete data by splitting the varbinds into multiple request.
 boolean getAttemptPartial()
          Returns the boolean state of whether this target will try to get partial data from an agent if the request is a multiple variable request.
 java.lang.String getAuthPassword()
          Gets the SNMP V3 AuthPassword
 int getAuthProtocol()
          Gets the SNMP V3 AuthProtocol
 java.lang.String getCommunity()
          Gets the SNMP target Community
 SnmpResultObject getCompleteResponse()
          Gets the complete data from the agent by splitting the varbinds into multiple requests.This can set if request pdu is very big.
 java.lang.String getContextID()
          Gets the SNMP V3 contextID.
 java.lang.String getContextName()
          get the SNMP V3 Context name
 boolean getDebug()
          This method returns true if debug mode is enabled and false otherwise
 byte[] getEngineID()
          Get the Snmp V3 EngineID
 int getErrorCode()
          Gets the error code for the last request.
 java.util.Hashtable getErrorCodes()
          To get the list of all the Error specific for an Snmp Request.
 int getErrorIndex()
          To get the error index corresponding to the errored oid in the pdu.
 java.lang.String getErrorString()
          Gets the error string for the request sent.
 int getExceptionCode()
          Gets the exception code for the last request.
 java.util.Hashtable getExceptionCodes()
          To get the list of all the Exceptions specific to SnmpV2/V3.
 boolean getIgnoreSpecificControlCodes()
          Gets the value determining whether the new-line characters, tabs and carriage returns are printed or their hex values.
 int getMaxNumRows()
          Gets the maximum number of rows that can be processed, default value is 1000.
 int getMaxRepetitions()
          Gets the SNMP target max repetitions value for a getbulk request.
 java.lang.String getMibModules()
          This method returns all the MIBs that are loaded in this applet/application.
 MibOperations getMibOperations()
          get the MibOperations object reference.
 int getNonRepeaters()
          Gets the SNMP target non-repeaters value for a getbulk request Default is 0.
 java.lang.String getObjectID()
          This method returns the first object ID as a string
 java.lang.String getObjectID(int index)
          This method returns the object ID at index as a string
 java.lang.String[] getObjectIDList()
          This method returns the list of object IDs as a string array or null, if no OIDs is set previously.
 int getPduErrorIndex()
          To get the error index set in the PDU.
 java.lang.String getPrincipal()
          Gets the SNMP V3 Principal
 java.lang.String getPrivPassword()
          Gets the SNMP V3 PrivPassword
 int getRetries()
          Gets the SNMP target retries value.
 java.lang.String getSecurityLevel()
          Gets the SNMP V3 SecurityLevel
 int getSecurityModel()
          Gets the SNMP V3 securityModel
 boolean getSendTimeoutEvents()
          Returns the boolean state of whether this target will send timeout events to a registered listener.
 SnmpOID getSnmpOID()
          This method returns the first SnmpOID from the list of object IDs or null, if no OID is set previously.
 SnmpOID[] getSnmpOIDList()
          This method returns the list of object IDs as an array of SnmpOID or null if no OID is set previously.
(package private)  SnmpVar[] getSnmpVariables(java.lang.String[] values)
          Returns the SnmpVar instances corresponding to the Strings using the object identifier list previously set in the SnmpTarget instance
 int getSnmpVersion()
          Gets the SNMP target SNMP Version.
 java.lang.String getTargetHost()
          Gets the SNMP target hostname
 int getTargetPort()
          get the SNMP target port number
 int getTimeout()
          Gets the SNMP target timeout value
 int getUserErrorCode()
          Gets the user defined error code for the last request.
 java.lang.String getUserErrorString()
          This method is used to get the user defined error string to a specific error code.
(package private)  SnmpVarBind[] getV1TrapOids(SnmpOID enterprise, int genericType, int specificType, long uptime)
          Returns a set of three varbinds for placing into a v2c notification pdu The first two varbinds(sysUptime and traptype) are placed first.
 int getVarBindCount()
          Returns the number of varbinds used with the splitted pdu.
 java.lang.String getWriteCommunity()
          Gets the SNMP target write Community to be used for set requests
(package private) static boolean isInSubTree(int[] rootoid, SnmpOID objid)
           
static boolean isInSubTree(SnmpOID rootoid, SnmpOID objid)
          Check if objid has rootoid as an ancestor.
 boolean isSerialize()
          Returns the status of Serialization of v3 tables ( USMUserTable and SnmpEngineTable )
 void loadMibs(java.lang.String mibs)
          To load the specified mibs.
 void releaseResources()
          Though the resources will be cleaned up by the garbage collector, if the application is no more using the SnmpTarget bean instance, this method can be used to immediately release the resources.
 void removeLogClient()
          Removes the Log client for which the target has been registered to redirect the log Messages.
 void removeResultListener(ResultListener l)
          Removes a listener for events from this target.
 void removeSnmpPropertyListener(SnmpPropertyListener l)
          Removes the SnmpPropertyListener from the list maintained by this target.
 void removeV3HandShakeListener(V3HandShakeResultListener l)
          Removes the listener registered for getting events.
 void setAttemptComplete(boolean flag)
          Enables the flag to get the complete data from the agent by splitting the varbinds into multiple request.This can set if request pdu is very big.
 void setAttemptPartial(boolean b)
          Sets the boolean state of whether this target will try to get a partial data from an agent if the request is a multi variable request.
 void setAuthPassword(java.lang.String password)
          Sets the SNMP V3 AuthPassword
 void setAuthProtocol(int protocol)
          Sets the SNMP V3 AuthProtocol
 void setClientID(int client)
          The Debug can be captured by setting this clientID.
 void setCommunity(java.lang.String s)
          Sets the SNMP target Community
 void setContextID(java.lang.String ctxtID)
          Sets the SNMP V3 contextID.
 void setContextName(java.lang.String cName)
          set the SNMP V3 Context name
 void setDebug(boolean debug)
          This method determines if debug output will be printed
 void setEngineID(byte[] eid)
          Sets the Snmp V3 EngineID
 void setIgnoreSpecificControlCodes(boolean specConCode)
          Sets the value determining whether the new-line characters, tabs and carriage returns are printed or their hex values.
 void setMaxNumRows(int maxRows)
          Sets the maximum number of rows that can be processed, default value is 1000.
 void setMaxRepetitions(int i)
          Sets the SNMP target max repetitions value for a getbulk request.
 void setMibModules(java.lang.String mibs)
          This method sets the MIB to be loaded in this applet/application.
 void setNonRepeaters(int i)
          Sets the SNMP target non-repeaters value for a getbulk request Default is 0.
 void setObjectID(int index, java.lang.String s)
          This method sets the object ID value at index based on specified string
 void setObjectID(java.lang.String s)
          This method sets the object ID specified by the String value.
 void setObjectIDList(java.lang.String[] s)
          This method sets the list of object IDs based on the specified string array.
 void setParams(java.lang.String host, java.lang.String port, java.lang.String community, java.lang.String mib, java.lang.String oid)
          To set Multiple parameters like TargetHost,Port,Community,MibModule and OID.
 void setParams(java.lang.String host, java.lang.String port, java.lang.String community, java.lang.String mib, java.lang.String[] oidlist)
          To set Multiple parameters like TargetHost,Port,Community,MibModule and OIDList.
 void setPrincipal(java.lang.String s)
          Sets the SNMP V3 Principal
 void setPrivPassword(java.lang.String password)
          Sets the SNMP V3 PrivPassword
 void setRetries(int i)
          Sets the SNMP target retries value.
 void setSecurityLevel(byte sLevel)
          Sets the SNMP V3 SecurityLevel.The value will be 0 ,1 or 3.
 void setSecurityModel(int securityModel)
          Sets the SNMP V3 SecurityModel
 void setSendTimeoutEvents(boolean b)
          This should be set to true if the listener should be notified of the timeout events in case if the request timesout.
 void setSnmpOID(SnmpOID oid)
          This method sets the SnmpOID
 void setSnmpOIDList(SnmpOID[] list)
          This method sets the list of object IDs as the given array of SnmpOID
 void setSnmpVersion(int i)
          Sets the SNMP target SNMP Version.
 void setTargetHost(java.lang.String s)
          This method sets the SNMP target hostname
 void setTargetPort(int i)
          set the SNMP target port number
 void setTimeout(int i)
          Sets the SNMP target timeout value
 void setVarBindCount(int count)
          Set the number of varbinds per request to get the complete data from the agent by splitting the varbinds into multiple request in case if the request pdu is very big.
 void setWriteCommunity(java.lang.String s)
          Sets the SNMP target write Community to be used for set requests
 java.lang.String snmpGet()
          Queries the target and returns as a string the SNMP variable identified by the first Object Identifier stored in this SnmpTarget instance.
 SnmpVar snmpGet(SnmpOID oid)
          Gets the SNMP variable identified by specified Object Identifier.
 java.lang.String[][] snmpGetAllList()
          Gets all instances of variables in the OID list.
 SnmpVarBind[][] snmpGetAllVariableBindings()
          Gets all instances of variables in the OID list.
 SnmpVar[][] snmpGetAllVariables()
          Gets all instances of variables in the OID list.
 java.lang.String[][] snmpGetBulkList()
          Get bulk on list of variables in the OID list.
 SnmpVarBind[][] snmpGetBulkVariableBindings()
          Get bulk on list of variables in the OID list.
 SnmpVar[][] snmpGetBulkVariables()
          Get bulk on list of variables in the OID list.
 java.lang.String[] snmpGetList()
          Queries the agent and returns the list of SNMP variable bindings from the agent using the Object Identifier list previously set in this SnmpTarget instance.
 SnmpVar[] snmpGetList(SnmpOID[] oids)
          Gets the list of SNMP variables identified by the Object Identifier list stored in this SnmpTarget instance.
 java.lang.String snmpGetNext()
          Queries the target and returns as a string the next SNMP variable identified by the first Object Identifier stored in this SnmpTarget instance.
 SnmpVar snmpGetNext(SnmpOID oid)
          Gets the SNMP variable identified by specified Object Identifier.
 java.lang.String[] snmpGetNextList()
          Queries the agent and returns the list of SNMP variable bindings from the agent using the Object Identifier list previously set in this SnmpTarget instance.
 SnmpVar[] snmpGetNextList(SnmpOID[] oids)
          Gets the list of SNMP variables identified by the Object Identifier list stored in this SnmpTarget instance.
 SnmpVar snmpGetNextVariable()
          Queries the target and returns the SNMP variable identified by the first Object Identifier stored in this SnmpTarget instance.
 SnmpVarBind snmpGetNextVariableBinding()
          Queries the target and returns the SNMP variable identified by the first Object Identifier stored in this SnmpTarget instance.
 SnmpVarBind[] snmpGetNextVariableBindings()
          Queries the agent and returns the list of SNMP variable bindings from the agent using the Object Identifier list previously set in this SnmpTarget instance.
 SnmpVar[] snmpGetNextVariables()
          Queries the agent and returns the list of SNMP variable bindings from the agent using the Object Identifier list previously set in this SnmpTarget instance.
 SnmpVar snmpGetVariable()
          Queries the target and returns the SNMP variable identified by the first Object Identifier stored in this SnmpTarget instance.
 SnmpVarBind snmpGetVariableBinding()
          Queries the target and returns the SNMP variable binding identified by the first Object Identifier stored in this SnmpTarget instance.
 SnmpVarBind[] snmpGetVariableBindings()
          Queries the agent and returns the list of SNMP variable bindings from the agent using the Object Identifier list previously set in this SnmpTarget instance.
 SnmpVar[] snmpGetVariables()
          Queries the agent and returns the list of SNMP variables from the agent using the Object Identifier list previously set in this SnmpTarget instance.
 void snmpSendInformAcknowledgement(long uptime, SnmpOID oid, SnmpVar[] values, int reqID)
           
 SnmpVarBind[] snmpSendInformRequest(long uptime, SnmpOID trapOid, SnmpVar[] values)
          This method is used to send an SNMPV2 inform pdu with parameters specified and variable bindings using object ids from the Object Identifier list previously set in this SnmpTarget instance, and the values specified.
 java.lang.String[] snmpSendInformRequest(long uptime, java.lang.String trapOid, java.lang.String[] values)
          This method is to send an SNMPV2 inform pdu with parameters specified and variable bindings using object ids from the Object Identifier list previously set in this SnmpTarget instance, and the values specified.
 void snmpSendNotification(long uptime, SnmpOID trapOid, SnmpVar[] values)
          This method is used to send an SNMPV2 notification pdu with parameters specified and variable bindings using object ids from the Object Identifier list previously set in this SnmpTarget instance, and the values specified.
 void snmpSendNotification(long uptime, java.lang.String trapOid, java.lang.String[] values)
          This method is used to send an SNMPV2 notification pdu with parameters specified and variable bindings using object ids from the Object Identifier list previously set in this SnmpTarget instance, and the values specified.
 void snmpSendTrap(SnmpOID enterprise, java.lang.String agenthost, int genericType, int specificType, long uptime, SnmpVar[] values)
          This method is used to send an SNMP trap, with parameters specified and variable bindings using object ids from the Object Identifier list previously set in this SnmpTarget instance, and the values specified.
 void snmpSendTrap(java.lang.String enterprise, java.lang.String agenthost, int genericType, int specificType, long uptime, java.lang.String[] values)
          This method is used to send an SNMP trap, with parameters specified and variable bindings using object ids from the Object Identifier list previously set in this SnmpTarget instance, and the values specified.
 java.lang.String snmpSet(java.lang.String value)
          This method is used to do an SNMP set to the target using value specified of the first variable on the variable list.
 java.lang.String snmpSet(java.lang.String value, byte type)
          This method is used to do an SNMP set to the target of the first variable on the variable list using value specified.
 java.lang.String[] snmpSetList(java.lang.String[] values)
          This method sets the list of SNMP variable bindings on the agent using the Object Identifier list previously set in this SnmpTarget instance.
 SnmpVar snmpSetVariable(SnmpVar var)
          This method is used to do an SNMP set with specified value to the target and returns the SNMP variable identified by the first Object Identifier stored in this SnmpTarget instance.
 SnmpVarBind[] snmpSetVariableList(SnmpVar[] values)
          This method is used to do an SNMP set, with values specified, the list of SNMP variable bindings on the agent, using the Object Identifier list previously set in this SnmpTarget instance.
 SnmpVar[] snmpSetVariables(SnmpVar[] values)
          This method is used to do an SNMP set, with values specified, the list of SNMP variable bindings on the agent, using the Object Identifier list previously set in this SnmpTarget instance.
(package private)  void updateMibs()
           
 void vetoableChange(java.beans.PropertyChangeEvent e)
          This method is called when a vetoableChange event is fired from the propertySetting bean.
 
Methods inherited from class com.adventnet.snmp.beans.SnmpServer
assignLogger, async_create_v3_tables, create_v3_tables, finalize, genEvent, getCharacterEncoding, getCompleteResponse, getGROUPSNMPResponse, getHostAddress, getLocalAddresses, getMibPath, getOverwriteCMI, getPacketBufferSize, getPdu, getSecLevelForTimeSync, getSecurityProvider, getSecurityTable, getSerializedMibFileName, getSimpleDateFormat, getSnmpEngineID, getSnmpEngineTable, getSNMPResponse, getSNMPResponse, getSNMPResponse, getTimeoutInMilliSec, getTimeToWait, getUSMTable, getV3ConfigTable, initJdbcParams, initSecurityProvider, initSnmpStore, isBroadcastFeatureEnable, isLoadFromCompiledMibs, isLoadFromSerializedMibs, isLoadMibsFromDatabase, isOverwriteCMI, isOverwriteMibsInDatabase, isReadDesc, isSendPDUByGroup, isSerializeMibs, isSetAutoInformResponse, isSupportedSecurityModel, isV3DatabaseFlag, isValidateUser, logErrorMessage, logInfoMessage, manage_v3_tables, managing_v3_tables, sendSNMPRequest, sendSNMPRequest, sendSNMPRequest, setAutoInformResponse, setBroadcastFeatureEnable, setCharacterEncoding, setErrorMessage, setLoadFromCompiledMibs, setLoadFromSerializedMibs, setLoadMibsFromDatabase, setMibOperations, setMibPath, setOverwriteCMI, setOverwriteMibsInDatabase, setPacketBufferSize, setPduParams, setReadDesc, setReceiveBufferSize, setSecLevelForTimeSync, setSendPDUByGroup, setSerializedMibFileName, setSerializeMibs,