Package org.snmp4j.agent.agentx.subagent
Class AgentXSubagent
java.lang.Object
org.snmp4j.agent.agentx.subagent.AgentXSubagent
- All Implemented Interfaces:
EventListener
,AgentXCommandListener
,org.snmp4j.agent.NotificationOriginator
,org.snmp4j.transport.TransportStateListener
public class AgentXSubagent
extends Object
implements AgentXCommandListener, org.snmp4j.agent.NotificationOriginator, org.snmp4j.transport.TransportStateListener
The
AgentXSubagent
class implements the AgentX communication
for an AgentX subagent implementation and the agent configuration via AgentConfigManager
although many
configurations that are necessary for a regular command responder and AgentX master agent are not applicable
for a subagent.
Since version 3.1.1 this class uses a ConcurrentHashMap
to store AgentX sessions, which allows better
concurrent operations on agents using multiple sessions.- Version:
- 3.2.0
- Author:
- Frank Fock
-
Nested Class Summary
-
Field Summary
Modifier and TypeFieldDescriptionprotected int
protected final Map
<org.snmp4j.smi.Address, AgentXPeer<?>> The AgentX peer agents (masters).protected org.snmp4j.agent.RequestHandler
<AgentXRequest> Request handler for CleanupSet requests.protected org.snmp4j.agent.RequestHandler
<AgentXRequest> Request handler for CommitSet requests.protected org.snmp4j.agent.RequestHandler
<AgentXRequest> Request handler for Get requests.protected org.snmp4j.agent.RequestHandler
<AgentXRequest> Request handler for GetBulk requests.protected org.snmp4j.agent.RequestHandler
<AgentXRequest> Request handler for GetNext requests.protected org.snmp4j.agent.RequestHandler
<AgentXRequest> Request handler for TestSet requests.protected org.snmp4j.agent.RequestHandler
<AgentXRequest> Request handler for UndoSet requests.protected final Map
<AgentXSubagent.RequestID, AgentXRequest> The request list.protected final Map
<Integer, AgentXSession<?>> The sessions hold by this sub-agent.protected Map
<String, AgentXSharedMOTableSupport<?, ?>> Shared table support map with a key composed ofsessionID + "#" + context
. -
Constructor Summary
ConstructorDescriptionAgentXSubagent
(AgentX agentX, org.snmp4j.smi.OID subagentID, org.snmp4j.smi.OctetString subagentDescr, SubagentXConfigManager configManager) Creates a AgentX sub-agent using aSubagentXConfigManager
. -
Method Summary
Modifier and TypeMethodDescription<A extends org.snmp4j.smi.Address>
intaddAgentCaps
(AgentXSession<A> session, org.snmp4j.smi.OctetString context, org.snmp4j.smi.OID id, org.snmp4j.smi.OctetString descr) protected <A extends org.snmp4j.smi.Address>
org.snmp4j.transport.ConnectionOrientedTransportMapping<A> void
int
close
(AgentXSession<?> session, byte reason) Close an AgentX session.Map
<AgentXSession<?>, Integer> closeAllSessions
(byte reason) Close all sessions hold by this sub-agent and return the AgentX status of the close operation together with theAgentXSession
object.protected int
closeSession
(int sessionID, byte reason) Close the AgentX session with the given session ID and reason.<A extends org.snmp4j.smi.Address>
intconnect
(A masterAddress, A localAddress, AgentXSession<A> session) Connect to the given AgentX master using the specified local address.<A extends org.snmp4j.smi.Address>
intconnect
(A masterAddress, A localAddress, AgentXSession<A> session, List<org.snmp4j.transport.TransportStateListener> stateListeners) Connect to the given AgentX master using the specified local address.void
connectionStateChanged
(org.snmp4j.transport.TransportStateEvent transportStateEvent) protected <R extends org.snmp4j.agent.mo.MOTableRow,
A extends org.snmp4j.smi.Address>
AgentXSharedMOTableSupport<R, ?> createSharedTableSupport
(AgentXSession<A> session, org.snmp4j.smi.OctetString context) Create a newAgentXSharedMOTableSupport
instance for the given AgentX session and context.<A extends org.snmp4j.smi.Address>
voiddisconnect
(A masterAddress) Disconnect from master and suspend any sending of packets to the given master address, before actually closing the transport used to communicate with the specified master.void
dispatchCommand
(AgentXCommandEvent<?> cmd) protected void
firePinged
(PingEvent<?> event) final AgentXSession
<?> Returns the first session that have been opened by this subagent and is still open.Collection
<org.snmp4j.smi.OctetString> byte
protected int
Get the next AgentX transaction ID.protected byte
getPriority
(org.snmp4j.agent.ManagedObject<?> mo, AgentXRegion region) Gets the priority with which the supplied managed object and region should be registered at the master agent.org.snmp4j.agent.MOServer
getServer
(org.snmp4j.smi.OctetString context) <A extends org.snmp4j.smi.Address>
AgentXResponseEvent<A> notify
(AgentXSession<A> session, org.snmp4j.smi.OctetString context, org.snmp4j.smi.OID notificationID, org.snmp4j.smi.TimeTicks sysUpTime, org.snmp4j.smi.VariableBinding[] vbs) Send an AgentX notification to the master which will then be forwarded to trap/notification targets of the master.notify
(org.snmp4j.smi.OctetString context, org.snmp4j.smi.OID notificationID, org.snmp4j.smi.TimeTicks sysUpTime, org.snmp4j.smi.VariableBinding[] vbs) notify
(org.snmp4j.smi.OctetString context, org.snmp4j.smi.OID notificationID, org.snmp4j.smi.VariableBinding[] vbs) Sends notifications (traps) to all appropriate notification targets through the master agent.protected <A extends org.snmp4j.smi.Address>
intopenSession
(org.snmp4j.transport.ConnectionOrientedTransportMapping<A> transport, A masterAddress, AgentXSession<A> session) Open a new AgentX session with the given transport and master address.<A extends org.snmp4j.smi.Address>
voidprocessCommand
(AgentXCommandEvent<A> event) Process an AgentX message.protected void
processNextSubRequest
(AgentXRequest request, org.snmp4j.agent.MOServer server, org.snmp4j.smi.OctetString context, AgentXRequest.AgentXSubRequest sreq) protected void
processResponse
(AgentXCommandEvent<?> cmd) protected <A extends org.snmp4j.smi.Address>
intregisterRegion
(AgentXSession<A> session, org.snmp4j.smi.OctetString context, AgentXRegion region, byte priority, org.snmp4j.smi.TimeTicks sysUpTime) void
registerRegions
(AgentXSession<?> session, org.snmp4j.smi.OctetString context, org.snmp4j.smi.TimeTicks sysUpTime, RegistrationCallback registrationCallback) Registers the subagent regions at the master agent.<R extends org.snmp4j.agent.mo.MOTableRow,
C extends org.snmp4j.agent.mo.MOColumn<?>, M extends org.snmp4j.agent.mo.MOTableModel<R>>
voidregisterSharedTableRows
(AgentXSession<?> session, org.snmp4j.smi.OctetString context, AgentXSharedMOTable<R, C, M> mo, RegistrationCallback registrationCallback, AgentXSharedMOTableSupport<R, ?> sharedTableSupport) Registers the indexes and (row) regions of a shared table.<R extends org.snmp4j.agent.mo.MOTableRow,
C extends org.snmp4j.agent.mo.MOColumn<?>, M extends org.snmp4j.agent.mo.MOTableModel<R>, A extends org.snmp4j.smi.Address>
voidregisterSharedTableRows
(AgentXSession<A> session, org.snmp4j.smi.OctetString context, AgentXSharedMOTable<R, C, M> mo, RegistrationCallback registrationCallback) Deprecated.protected void
release
(org.snmp4j.agent.MOServer server, AgentXRequest req) <A extends org.snmp4j.smi.Address>
intremoveAgentCaps
(AgentXSession<A> session, org.snmp4j.smi.OctetString context, org.snmp4j.smi.OID id) protected void
removeMaster
(org.snmp4j.TransportMapping<?> transport) void
<A extends org.snmp4j.smi.Address>
booleanresetConnection
(A masterAddress, boolean sendClosePdu) Reset a connection that is not working anymore either with trying to close all open session with a AgentXClosePdu (when sendClosePdu is true) or silently without sending any messages.<A extends org.snmp4j.smi.Address>
booleanresumeConnecting
(A masterAddress, org.snmp4j.transport.ConnectionOrientedTransportMapping<A> transportMapping) Resumes auto connection to the specified address using the given transport.protected <A extends org.snmp4j.smi.Address>
voidsendResponse
(AgentXCommandEvent<A> cmd, AgentXRequest request) void
setDefaultPriority
(byte priority) void
setPingDelay
(int seconds) Sets the ping delay in seconds.protected <A extends org.snmp4j.smi.Address>
intunregisterRegion
(AgentXSession<A> session, org.snmp4j.smi.OctetString context, AgentXRegion region, byte timeout)
-
Field Details
-
requestList
The request list. -
peers
The AgentX peer agents (masters). -
sessions
The sessions hold by this sub-agent. -
requestHandlerGet
Request handler for Get requests. -
requestHandlerGetNext
Request handler for GetNext requests. -
requestHandlerGetBulk
Request handler for GetBulk requests. -
requestHandlerTestSet
Request handler for TestSet requests. -
requestHandlerCommitSet
Request handler for CommitSet requests. -
requestHandlerUndoSet
Request handler for UndoSet requests. -
requestHandlerCleanupSet
Request handler for CleanupSet requests. -
nextTransactionID
protected int nextTransactionID
-
-
Constructor Details
-
AgentXSubagent
public AgentXSubagent(AgentX agentX, org.snmp4j.smi.OID subagentID, org.snmp4j.smi.OctetString subagentDescr, SubagentXConfigManager configManager) Creates a AgentX sub-agent using aSubagentXConfigManager
.- Parameters:
agentX
- theAgentX
protocol to be used by this sub-agent.subagentID
- the unique identifier for this sub-agent.subagentDescr
- a textual description for this sub-agent.configManager
- the configuration manager that organises howManagedObject
s of this sub-agent are initialized, persistently stored, and served to the agent.- Since:
- 3.0
-
-
Method Details
-
setPingDelay
public void setPingDelay(int seconds) Sets the ping delay in seconds. If greater than zero, for each session a ping PDU is sent to the master to validate the session regularly with the specified delay. To monitor the ping requests, it is necessary to add aPingListener
withaddPingListener(org.snmp4j.agent.agentx.event.PingListener)
.- Parameters:
seconds
- the delay. If zero or a negative value is supplied, no pings are sent
-
processCommand
Description copied from interface:AgentXCommandListener
Process an AgentX message.- Specified by:
processCommand
in interfaceAgentXCommandListener
- Type Parameters:
A
-Address
type of peer that trigger this command.- Parameters:
event
- the AgentX message and necessary context information.
-
getNextTransactionID
protected int getNextTransactionID()Get the next AgentX transaction ID.- Returns:
- a unique AgentX transaction ID for this sub-agent execution runtime.
-
closeSession
Close the AgentX session with the given session ID and reason.- Parameters:
sessionID
- an AgentX session ID. If such a session does not exist,AgentXProtocol.AGENTX_NOT_OPEN
is returned.reason
- the AgentX reason (AgentXProtocol.REASON_SHUTDOWN
,AgentXProtocol.REASON_TIMEOUTS
,AgentXProtocol.REASON_BY_MANAGER
,AgentXProtocol.REASON_PROTOCOL_ERROR
,AgentXProtocol.REASON_PARSE_ERROR
,AgentXProtocol.REASON_OTHER
) for closing the session.- Returns:
- the error status returned by the master agent or
AgentXProtocol.AGENTX_TIMEOUT
if the master did not respond in time. - Throws:
IOException
- if the IO operation failed.
-
openSession
protected <A extends org.snmp4j.smi.Address> int openSession(org.snmp4j.transport.ConnectionOrientedTransportMapping<A> transport, A masterAddress, AgentXSession<A> session) throws IOException Open a new AgentX session with the given transport and master address.- Type Parameters:
A
- type ofAddress
for the session's master address.- Parameters:
transport
- theTransportMapping
to use.masterAddress
- the AgentX master agent address to connect.session
- a newAgentXSession
. On success, the session ID will be set to the value returned by the master.- Returns:
- the error status returned by the master agent or
AgentXProtocol.AGENTX_TIMEOUT
if the master did not respond in time. - Throws:
IOException
- if the IO operation failed.
-
disconnect
Disconnect from master and suspend any sending of packets to the given master address, before actually closing the transport used to communicate with the specified master.- Type Parameters:
A
- theAddress
type.- Parameters:
masterAddress
- the master's address to suspend and then disconnect.- Throws:
IOException
- if the closing of the connection fails.- Since:
- 3.2.0
-
resumeConnecting
public <A extends org.snmp4j.smi.Address> boolean resumeConnecting(A masterAddress, org.snmp4j.transport.ConnectionOrientedTransportMapping<A> transportMapping) Resumes auto connection to the specified address using the given transport. This call does not actually send any packets to the address, instead it simply (re)activates the mechanism to automatically establish a connection to the specified address, when a new packet (i.e. openSessionPDU) is sent to the master address. Thus, callopenSession(ConnectionOrientedTransportMapping, Address, AgentXSession)
immediately after calling this method to reconnect to a master, after adisconnect(Address)
with suspend or aresetConnection(Address, boolean)
call.- Type Parameters:
A
- theAddress
type.- Parameters:
masterAddress
- the master's address to disconnect and optionally suspend.transportMapping
- theTransportMapping
to resume.- Returns:
true
if the master address was suspended before and is resumed now,false
if resuming the master address was not necessary (i.e. is already active).- Since:
- 3.2.0
-
resetConnection
public <A extends org.snmp4j.smi.Address> boolean resetConnection(A masterAddress, boolean sendClosePdu) Reset a connection that is not working anymore either with trying to close all open session with a AgentXClosePdu (when sendClosePdu is true) or silently without sending any messages. In both cases the connection associated with the given master address is closed (withdisconnect(Address)
) and all related sessions are removed and closed too.- Type Parameters:
A
- theAddress
type of the connection.- Parameters:
masterAddress
- the address of the peer to reset connection.sendClosePdu
-true
if this subagent should try to send AgentXClosePdus for each open session before resetting the connection offalse
if the connection should be closed silently. Close PDU will be sent only if the transport is still listening.- Returns:
true
if the connection has been successfully reset,false
otherwise.- Since:
- 3.1.1
-
connect
public <A extends org.snmp4j.smi.Address> int connect(A masterAddress, A localAddress, AgentXSession<A> session) throws IOException Connect to the given AgentX master using the specified local address.- Type Parameters:
A
- address type to connect to.- Parameters:
masterAddress
- the (TCP) address of the master agent.localAddress
- the local address. Use port 0 to letAgentXSubagent
choose an available port.session
- returns theAgentXSession
information of the new AgentX session, i.e. the session ID returned by the master agent.- Returns:
AgentXProtocol.AGENTX_SUCCESS
if the session has been established or an AgentX error status, if not.- Throws:
IOException
- if an IO operation failed.
-
connect
public <A extends org.snmp4j.smi.Address> int connect(A masterAddress, A localAddress, AgentXSession<A> session, List<org.snmp4j.transport.TransportStateListener> stateListeners) throws IOException Connect to the given AgentX master using the specified local address.- Type Parameters:
A
- address type to connect to.- Parameters:
masterAddress
- the (TCP) address of the master agent.localAddress
- the local address. Use port 0 to letAgentXSubagent
choose an available port.session
- returns theAgentXSession
information of the new AgentX session, i.e. the session ID returned by the master agent.stateListeners
- an optional list ofTransportStateListener
to register forTransportStateEvent
s before the connection is being established using a newConnectionOrientedTransportMapping
.- Returns:
AgentXProtocol.AGENTX_SUCCESS
if the session has been established or an AgentX error status, if not.- Throws:
IOException
- if an IO operation failed.- Since:
- 3.0.1
-
close
Close an AgentX session.- Parameters:
session
- aAgentXSession
.reason
- the AgentX reason (AgentXProtocol.REASON_SHUTDOWN
,AgentXProtocol.REASON_TIMEOUTS
,AgentXProtocol.REASON_BY_MANAGER
,AgentXProtocol.REASON_PROTOCOL_ERROR
,AgentXProtocol.REASON_PARSE_ERROR
,AgentXProtocol.REASON_OTHER
) for closing the session.- Returns:
AgentXProtocol.AGENTX_SUCCESS
if the session has been closed or an AgentX error status, if not.- Throws:
IOException
- if an IO operation failed.
-
closeAllSessions
Close all sessions hold by this sub-agent and return the AgentX status of the close operation together with theAgentXSession
object.- Specified by:
closeAllSessions
in interfaceAgentXCommandListener
- Parameters:
reason
- the AgentX reason (AgentXProtocol.REASON_SHUTDOWN
,AgentXProtocol.REASON_TIMEOUTS
,AgentXProtocol.REASON_BY_MANAGER
,AgentXProtocol.REASON_PROTOCOL_ERROR
,AgentXProtocol.REASON_PARSE_ERROR
,AgentXProtocol.REASON_OTHER
) for closing the session.- Returns:
- a map of the AgentXSession session objects hold by this agent and the corresponding AgentX error code. An
error code of
AgentXProtocol.AGENTX_SUCCESS
indicates that this session was successfully closed. - Since:
- 3.0.0
-
setDefaultPriority
public void setDefaultPriority(byte priority) -
getDefaultPriority
public byte getDefaultPriority() -
getPriority
Gets the priority with which the supplied managed object and region should be registered at the master agent. Overwrite this method to use individual priorities depending on the registered region/managed object. The default implementation returnsgetDefaultPriority()
.- Parameters:
mo
- ManagedObject a managed object instance that managesregion
.region
- the region to be registered.- Returns:
- the priority between 0 and 255 (lower value results in higher priority).
-
registerRegions
public void registerRegions(AgentXSession<?> session, org.snmp4j.smi.OctetString context, org.snmp4j.smi.TimeTicks sysUpTime, RegistrationCallback registrationCallback) Registers the subagent regions at the master agent. It uses theAgentXSharedMOTableSupport
instances ofAgentXSharedMutableMOTable
instances. For any other instances a support object instance will be created for each session and context.- Parameters:
session
- the session on whose behalf regions are registered.context
- the context to use for registration.sysUpTime
- if notnull
, the master agent's notion of the sysUpTime for the registered context is returned. The input value is always ignored!registrationCallback
- a possiblynull
reference to aRegistrationCallback
instance to handle registration events.
-
registerRegion
protected <A extends org.snmp4j.smi.Address> int registerRegion(AgentXSession<A> session, org.snmp4j.smi.OctetString context, AgentXRegion region, byte priority, org.snmp4j.smi.TimeTicks sysUpTime) throws IOException - Throws:
IOException
-
unregisterRegion
protected <A extends org.snmp4j.smi.Address> int unregisterRegion(AgentXSession<A> session, org.snmp4j.smi.OctetString context, AgentXRegion region, byte timeout) throws IOException - Throws:
IOException
-
addMaster
protected <A extends org.snmp4j.smi.Address> org.snmp4j.transport.ConnectionOrientedTransportMapping<A> addMaster(A localAddress, List<org.snmp4j.transport.TransportStateListener> stateListeners) throws IOException - Throws:
IOException
-
removeMaster
protected void removeMaster(org.snmp4j.TransportMapping<?> transport) -
getServer
public org.snmp4j.agent.MOServer getServer(org.snmp4j.smi.OctetString context) -
getContexts
-
dispatchCommand
-
sendResponse
protected <A extends org.snmp4j.smi.Address> void sendResponse(AgentXCommandEvent<A> cmd, AgentXRequest request) -
release
-
processResponse
-
processNextSubRequest
protected void processNextSubRequest(AgentXRequest request, org.snmp4j.agent.MOServer server, org.snmp4j.smi.OctetString context, AgentXRequest.AgentXSubRequest sreq) throws NoSuchElementException - Throws:
NoSuchElementException
-
notify
public Object notify(org.snmp4j.smi.OctetString context, org.snmp4j.smi.OID notificationID, org.snmp4j.smi.VariableBinding[] vbs) Sends notifications (traps) to all appropriate notification targets through the master agent.- Specified by:
notify
in interfaceorg.snmp4j.agent.NotificationOriginator
- Parameters:
context
- the context name of the context on whose behalf this notification has been generated.notificationID
- the object ID that uniquely identifies this notification. For SNMPv1 traps, the notification ID has to be build using the rules provided by RFC 2576.vbs
- an array ofVariableBinding
instances representing the payload of the notification.- Returns:
- an
AgentXResponseEvent
instance ornull
if the notification request timed out.
-
notify
public Object notify(org.snmp4j.smi.OctetString context, org.snmp4j.smi.OID notificationID, org.snmp4j.smi.TimeTicks sysUpTime, org.snmp4j.smi.VariableBinding[] vbs) - Specified by:
notify
in interfaceorg.snmp4j.agent.NotificationOriginator
-
firstSession
Returns the first session that have been opened by this subagent and is still open. If no open session exists,null
is returned.- Returns:
- an
AgentXSession
.
-
notify
public <A extends org.snmp4j.smi.Address> AgentXResponseEvent<A> notify(AgentXSession<A> session, org.snmp4j.smi.OctetString context, org.snmp4j.smi.OID notificationID, org.snmp4j.smi.TimeTicks sysUpTime, org.snmp4j.smi.VariableBinding[] vbs) throws IOException Send an AgentX notification to the master which will then be forwarded to trap/notification targets of the master.- Type Parameters:
A
- address type associated with theAgentXSession
for which the notification is created.- Parameters:
session
- theAgentXSession
to be used.context
- the SNMPv3 (AgentX) context of the notification.notificationID
- theOID
the identifies the notification as defined in the corresponding MIB.sysUpTime
- the current notion of the sub-agents up-time.vbs
- a notification payload as an array ofVariableBinding
instances.AgentXProtocol.AGENTX_SUCCESS
if the session has been closed or an AgentX error status, if not.- Returns:
- a
AgentXResponseEvent
object that provides detailed information about the response returned by the AgentX peer. - Throws:
IOException
- if an IO operation failed.
-
addAgentCaps
public <A extends org.snmp4j.smi.Address> int addAgentCaps(AgentXSession<A> session, org.snmp4j.smi.OctetString context, org.snmp4j.smi.OID id, org.snmp4j.smi.OctetString descr) -
removeAgentCaps
public <A extends org.snmp4j.smi.Address> int removeAgentCaps(AgentXSession<A> session, org.snmp4j.smi.OctetString context, org.snmp4j.smi.OID id) -
addPingListener
-
removePingListener
-
firePinged
-
connectionStateChanged
public void connectionStateChanged(org.snmp4j.transport.TransportStateEvent transportStateEvent) - Specified by:
connectionStateChanged
in interfaceorg.snmp4j.transport.TransportStateListener
-
registerSharedTableRows(org.snmp4j.agent.agentx.AgentXSession, org.snmp4j.smi.OctetString, AgentXSharedMOTable, RegistrationCallback, AgentXSharedMOTableSupport)
instead.