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
  • Field Details

    • requestList

      protected final Map<AgentXSubagent.RequestID,AgentXRequest> requestList
      The request list.
    • peers

      protected final Map<org.snmp4j.smi.Address,AgentXPeer<?>> peers
      The AgentX peer agents (masters).
    • sessions

      protected final Map<Integer,AgentXSession<?>> sessions
      The sessions hold by this sub-agent.
    • requestHandlerGet

      protected org.snmp4j.agent.RequestHandler<AgentXRequest> requestHandlerGet
      Request handler for Get requests.
    • requestHandlerGetNext

      protected org.snmp4j.agent.RequestHandler<AgentXRequest> requestHandlerGetNext
      Request handler for GetNext requests.
    • requestHandlerGetBulk

      protected org.snmp4j.agent.RequestHandler<AgentXRequest> requestHandlerGetBulk
      Request handler for GetBulk requests.
    • requestHandlerTestSet

      protected org.snmp4j.agent.RequestHandler<AgentXRequest> requestHandlerTestSet
      Request handler for TestSet requests.
    • requestHandlerCommitSet

      protected org.snmp4j.agent.RequestHandler<AgentXRequest> requestHandlerCommitSet
      Request handler for CommitSet requests.
    • requestHandlerUndoSet

      protected org.snmp4j.agent.RequestHandler<AgentXRequest> requestHandlerUndoSet
      Request handler for UndoSet requests.
    • requestHandlerCleanupSet

      protected org.snmp4j.agent.RequestHandler<AgentXRequest> requestHandlerCleanupSet
      Request handler for CleanupSet requests.
    • nextTransactionID

      protected int nextTransactionID
    • sharedMOTableSupport

      protected Map<String,AgentXSharedMOTableSupport<?,?>> sharedMOTableSupport
      Shared table support map with a key composed of sessionID + "#" + context.
  • 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 a SubagentXConfigManager.
      Parameters:
      agentX - the AgentX 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 how ManagedObjects 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 a PingListener with addPingListener(org.snmp4j.agent.agentx.event.PingListener).
      Parameters:
      seconds - the delay. If zero or a negative value is supplied, no pings are sent
    • processCommand

      public <A extends org.snmp4j.smi.Address> void processCommand(AgentXCommandEvent<A> event)
      Description copied from interface: AgentXCommandListener
      Process an AgentX message.
      Specified by:
      processCommand in interface AgentXCommandListener
      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

      protected int closeSession(int sessionID, byte reason) throws IOException
      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 of Address for the session's master address.
      Parameters:
      transport - the TransportMapping to use.
      masterAddress - the AgentX master agent address to connect.
      session - a new AgentXSession. 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

      public <A extends org.snmp4j.smi.Address> void disconnect(A masterAddress) throws IOException
      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 - the Address 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, call openSession(ConnectionOrientedTransportMapping, Address, AgentXSession) immediately after calling this method to reconnect to a master, after a disconnect(Address) with suspend or a resetConnection(Address, boolean) call.
      Type Parameters:
      A - the Address type.
      Parameters:
      masterAddress - the master's address to disconnect and optionally suspend.
      transportMapping - the TransportMapping 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 (with disconnect(Address)) and all related sessions are removed and closed too.
      Type Parameters:
      A - the Address 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 of false 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 let AgentXSubagent choose an available port.
      session - returns the AgentXSession 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 let AgentXSubagent choose an available port.
      session - returns the AgentXSession information of the new AgentX session, i.e. the session ID returned by the master agent.
      stateListeners - an optional list of TransportStateListener to register for TransportStateEvents before the connection is being established using a new ConnectionOrientedTransportMapping.
      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

      public int close(AgentXSession<?> session, byte reason) throws IOException
      Close an AgentX session.
      Parameters:
      session - a AgentXSession.
      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

      public 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 the AgentXSession object.
      Specified by:
      closeAllSessions in interface AgentXCommandListener
      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

      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. Overwrite this method to use individual priorities depending on the registered region/managed object. The default implementation returns getDefaultPriority().
      Parameters:
      mo - ManagedObject a managed object instance that manages region.
      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 the AgentXSharedMOTableSupport instances of AgentXSharedMutableMOTable 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 not null, the master agent's notion of the sysUpTime for the registered context is returned. The input value is always ignored!
      registrationCallback - a possibly null reference to a RegistrationCallback instance to handle registration events.
    • createSharedTableSupport

      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 new AgentXSharedMOTableSupport instance for the given AgentX session and context.
      Type Parameters:
      R - the MOTableRow type to support.
      A - address type associated with the AgentXSession for which the shared table support is created.
      Parameters:
      session - an AgentXSession instance.
      context - a AgentX context.
      Returns:
      a (new) AgentXSharedMOTableSupport instance.
    • registerSharedTableRows

      @Deprecated public <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> void registerSharedTableRows(AgentXSession<A> session, org.snmp4j.smi.OctetString context, AgentXSharedMOTable<R,C,M> mo, RegistrationCallback registrationCallback)
      Deprecated.
      Use registerSharedTableRows(org.snmp4j.agent.agentx.AgentXSession, org.snmp4j.smi.OctetString, AgentXSharedMOTable, RegistrationCallback, AgentXSharedMOTableSupport) instead. This version creates a new table support object for each call (shared table) which is not recommended.
      Registers the indexes and (row) regions of a shared table. This method is called on behalf of registerRegions(org.snmp4j.agent.agentx.AgentXSession, org.snmp4j.smi.OctetString, org.snmp4j.smi.TimeTicks, RegistrationCallback).
      Type Parameters:
      R - the MOTableRow type to support.
      C - the column (base) type of the shared table.
      M - the table model type managing the shared table rows.
      A - address type associated with the AgentXSession for which the shared table support is created.
      Parameters:
      session - the session on whose behalf regions are registered.
      context - the context to use for registration.
      mo - the AgentXSharedMOTable instance to register.
      registrationCallback - if not null the callback is informed when registration of a row succeeded or failed.
    • registerSharedTableRows

      public <R extends org.snmp4j.agent.mo.MOTableRow, C extends org.snmp4j.agent.mo.MOColumn<?>, M extends org.snmp4j.agent.mo.MOTableModel<R>> void registerSharedTableRows(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. This method is called on behalf of registerRegions(org.snmp4j.agent.agentx.AgentXSession, org.snmp4j.smi.OctetString, org.snmp4j.smi.TimeTicks, RegistrationCallback).
      Type Parameters:
      R - the MOTableRow type to support.
      C - the column (base) type of the shared table.
      M - the table model type managing the shared table rows.
      Parameters:
      session - the session on whose behalf regions are registered.
      context - the context to use for registration.
      mo - the AgentXSharedMOTable instance to register.
      registrationCallback - if not null the callback is informed when registration of a row succeeded or failed.
      sharedTableSupport - the shared table support to be used for row registration. If mo has no table support instance and is a AgentXSharedMutableMOTable then its sharedTableSupport will be set to sharedTableSupport.
      Since:
      2.1
    • 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

      public Collection<org.snmp4j.smi.OctetString> getContexts()
    • dispatchCommand

      public void dispatchCommand(AgentXCommandEvent<?> cmd)
    • sendResponse

      protected <A extends org.snmp4j.smi.Address> void sendResponse(AgentXCommandEvent<A> cmd, AgentXRequest request)
    • release

      protected void release(org.snmp4j.agent.MOServer server, AgentXRequest req)
    • processResponse

      protected void processResponse(AgentXCommandEvent<?> cmd)
    • 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 interface org.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 of VariableBinding instances representing the payload of the notification.
      Returns:
      an AgentXResponseEvent instance or null 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 interface org.snmp4j.agent.NotificationOriginator
    • firstSession

      public final AgentXSession<?> 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 the AgentXSession for which the notification is created.
      Parameters:
      session - the AgentXSession to be used.
      context - the SNMPv3 (AgentX) context of the notification.
      notificationID - the OID 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 of VariableBinding 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

      public void addPingListener(PingListener l)
    • removePingListener

      public void removePingListener(PingListener l)
    • firePinged

      protected void firePinged(PingEvent<?> event)
    • connectionStateChanged

      public void connectionStateChanged(org.snmp4j.transport.TransportStateEvent transportStateEvent)
      Specified by:
      connectionStateChanged in interface org.snmp4j.transport.TransportStateListener