Class AgentXCommandProcessor

java.lang.Object
org.snmp4j.agent.CommandProcessor
org.snmp4j.agent.agentx.master.AgentXCommandProcessor
All Implemented Interfaces:
EventListener, AgentXCommandListener, AgentXResponseListener, org.snmp4j.agent.NotificationOriginator, org.snmp4j.CommandResponder, org.snmp4j.transport.TransportStateListener

public class AgentXCommandProcessor extends org.snmp4j.agent.CommandProcessor implements AgentXCommandListener, org.snmp4j.transport.TransportStateListener, AgentXResponseListener
The AgentXCommandProcessor implements the CommandProcessor interface for AgentX command processing of an AgentX master agent. It holds the AgentX sessions and region registrations internally. The context engine ID of the master agent, the AgentXQueue, the AgentX protocol adapter, and the MOServers have to be provided for instance creation.
Author:
Frank Fock
  • Field Details

    • MAX_REPROCESSING_DEFAULT

      public static final int MAX_REPROCESSING_DEFAULT
      The default maximum reprocessing count. This value should be high enough to allow enough GETBULK repetitions but low enough to avoid sub-agent overload, if reprocessing fails.
      See Also:
    • indexRegistry

      protected AgentXIndexRegistry indexRegistry
      Index registry of the AgentX master agent.
  • Constructor Details

    • AgentXCommandProcessor

      public AgentXCommandProcessor(org.snmp4j.smi.OctetString contextEngineID, AgentXQueue queue, AgentX agentX, org.snmp4j.agent.MOServer[] server)
      Creates an AgentXCommandProcessor using a AgentX entity and an array of MOServer instances representing the master agents ManagedObjects.
      Parameters:
      contextEngineID - the context engine ID of associated with this AgentX command processor (by default the master agent's local engine ID).
      queue - the AgentXQueue to store pending requests.
      agentX - the AgentX server.
      server - the managed objects of the master agent.
  • Method Details

    • setMaxReprocessing

      public void setMaxReprocessing(int maxReprocessing)
      Sets the maximum reprocessing. Default is MAX_REPROCESSING_DEFAULT
      Parameters:
      maxReprocessing - the maximum reprocessing of AgentX requests.
    • getMaxReprocessing

      public int getMaxReprocessing()
      Gets the request maximum reprocessing value.
      Returns:
      the maximum reprocessing count of AgentX requests.
    • setMaxParseErrors

      public void setMaxParseErrors(int maxParseErrors)
      Sets the maximum number of parse errors allowed per peer. If this number is exceeded then the peer will be closed with reason AgentXProtocol.REASON_PARSE_ERROR.
      Parameters:
      maxParseErrors - a positive value (including zero) sets the upper limit of parse errors tolerated per peer. If the number of parse errors exceeds this limit, all sessions with that peer will be closed. A negative value deactivates any limit.
      Since:
      1.0.1
    • getMaxParseErrors

      public int getMaxParseErrors()
      Gets the upper limit for parse errors for an AgentX peer.
      Returns:
      a positive value (including zero) indicates the upper limit of parse errors tolerated per peer. A negative value indicates that there is no limit.
      Since:
      1.0.1
    • finalizeRequest

      protected <A extends org.snmp4j.smi.Address> void finalizeRequest(org.snmp4j.CommandResponderEvent<A> command, org.snmp4j.agent.request.SnmpRequest req, org.snmp4j.agent.MOServer server)
      Finish a request a free all related resources.
      Overrides:
      finalizeRequest in class org.snmp4j.agent.CommandProcessor
      Parameters:
      command - the request processed.
      req - the associated SnmpRequest.
      server - the MOServer to reprocess the request, if necessary.
    • getNextSessionID

      protected int getNextSessionID()
      Gets the next session ID.
      Returns:
      a new session ID.
    • getServer

      @Deprecated public org.snmp4j.agent.MOServer getServer()
      Deprecated.
      Use CommandProcessor.getServer(OctetString context) instead.
      Gets the default server (for the null context).
      Returns:
      the default server instance.
    • getDefaultTimeout

      public byte getDefaultTimeout()
      Gets the default timeout for this AgentX entity.
      Returns:
      the timout in seconds.
    • getMaxConsecutiveTimeouts

      public int getMaxConsecutiveTimeouts()
      Gets the maximum number of consecutive timeouts allowed per session.
      Returns:
      the maximum number of consecutive timeouts allowed per session
    • isAcceptNewContexts

      public boolean isAcceptNewContexts()
      Indicates whether subagents can register contexts that are not yet supported by this master agent.
      Returns:
      true if subagents can register objects for new contexts.
    • setDefaultTimeout

      public void setDefaultTimeout(byte defaultTimeout)
      Sets the default timeout.
      Parameters:
      defaultTimeout - the default timeout in seconds.
    • setMaxConsecutiveTimeouts

      public void setMaxConsecutiveTimeouts(int maxConsecutiveTimeouts)
      Sets the maximum number of timeouts allowed per session. If the number is exceeded then the session will be closed with reason AgentXProtocol.REASON_TIMEOUTS.
      Parameters:
      maxConsecutiveTimeouts - the maximum number of timeouts (should be greater than zero).
    • setAcceptNewContexts

      public void setAcceptNewContexts(boolean acceptNewContexts)
      Enables or disables accepting new contexts from subagents.
      Parameters:
      acceptNewContexts - true if subagents are allowed to register objects for new contexts, false otherwise. Default is false.
    • processCommand

      public <A extends org.snmp4j.smi.Address> void processCommand(AgentXCommandEvent<A> event)
      Process an AgentXCommandEvent.
      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.
    • indexDeallocate

      public AgentXResponsePDU indexDeallocate(AgentXIndexDeallocatePDU pdu, AgentXMasterSession<?> session)
      Deallocate an index.
      Parameters:
      pdu - the deallocation PDU.
      session - the session deallocating.
      Returns:
      the AgentXResponsePDU.
    • indexAllocate

      public AgentXResponsePDU indexAllocate(AgentXIndexAllocatePDU pdu, AgentXMasterSession<?> session)
      Allocate an index.
      Parameters:
      pdu - the allocation PDU.
      session - the session allocating.
      Returns:
      the AgentXResponsePDU.
    • processAgentXBulkResponse

      protected void processAgentXBulkResponse(AgentXPending<?> pending, AgentXResponsePDU pdu)
      Process an AgentX-GETBULK-Response.
      Parameters:
      pending - the pending GETBULK request
      pdu - the response PDU.
    • processsErrorResponse

      protected static void processsErrorResponse(AgentXPending<?> pending, AgentXResponsePDU pdu) throws NoSuchElementException
      Process an error response.
      Parameters:
      pending - the pending GETBULK request
      pdu - the response PDU.
      Throws:
      NoSuchElementException - if there are less sub-request in the pending request that reported by the error statusof the response PDU.
    • ping

      protected AgentXResponsePDU ping(AgentXPingPDU pdu, AgentXMasterSession<?> session)
      Respond to a Ping request.
      Parameters:
      pdu - the ping request.
      session - the session that received the request.
      Returns:
      the ping response PDU.
    • notify

      protected AgentXResponsePDU notify(AgentXNotifyPDU pdu, AgentXMasterSession<?> session)
      Process a notification PDU for an AgentXSession.
      Parameters:
      pdu - the notification PDU.
      session - the source session.
      Returns:
      the notification PDU response.
    • getContextSysUpTime

      protected org.snmp4j.smi.TimeTicks getContextSysUpTime(org.snmp4j.smi.OctetString context)
      Gets the sysUpTime for the specified context.
      Parameters:
      context - a SNMPv3 context (or an empty OctetString for the default context).
      Returns:
      TimeTicks value representing the context's sysUpTime.0.
    • addAgentCaps

      public AgentXResponsePDU addAgentCaps(AgentXAddAgentCapsPDU pdu, AgentXMasterSession<?> session)
      Parameters:
      pdu - the addAgentCaps-PDU.
      session - the source session.
      Returns:
      the AgentXResponsePDU
    • getAgentCaps

      protected org.snmp4j.agent.mo.snmp.AgentCapabilityList getAgentCaps(org.snmp4j.smi.OctetString contextName)
      Gets the Agent-Capabilities for a context.
      Parameters:
      contextName - a context name.
      Returns:
      the AgentCapabilityList associated with the context or null if that does not exist.
    • removeAgentCaps

      public AgentXResponsePDU removeAgentCaps(AgentXRemoveAgentCapsPDU pdu, AgentXMasterSession<?> session)
      Parameters:
      pdu - the removeAgentCaps-PDU.
      session - the source session.
      Returns:
      the AgentXResponsePDU
    • closeSession

      public AgentXResponsePDU closeSession(AgentXClosePDU pdu, AgentXMasterSession<?> session)
      Process an AgentXClosePDU, close the session and remove all registrations.
      Parameters:
      pdu - the close-PDU.
      session - the source session.
      Returns:
      the AgentXResponsePDU
    • closeAllSessions

      public Map<AgentXSession<?>,Integer> closeAllSessions(byte reason)
      Close all sessions associated with this AgentXCommandListener 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
    • closeSession

      public <A extends org.snmp4j.smi.Address> int closeSession(AgentXMasterSession<A> session, byte reason)
      Close the specified AgentXSession.
      Type Parameters:
      A - the address type.
      Parameters:
      session - the AgentXSession to close.
      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 AgentX error/status code. AgentXProtocol.AGENTX_SUCCESS is returned on success.
    • removeAllRegistrations

      protected void removeAllRegistrations(AgentXMasterSession<?> session)
      Remove all registrations for a session.
      Parameters:
      session - a AgentXSession to clean its registrations.
    • getSession

      protected AgentXMasterSession<?> getSession(int sessionID)
      Gets the session for a given session ID.
      Parameters:
      sessionID - a session ID.
      Returns:
      the associated session or null if the session could not be found.
    • getSession

      protected AgentXMasterSession<?> getSession(AgentXPDU pdu)
      Gets the session for an AgentXPDU.
      Parameters:
      pdu - a PDU.
      Returns:
      the session for the specified PDU.
    • register

      protected <A extends org.snmp4j.smi.Address> AgentXResponsePDU register(AgentXRegisterPDU pdu, AgentXCommandEvent<A> command, AgentXMasterSession<A> session)
      Type Parameters:
      A - the address type.
      Parameters:
      pdu - the PDU.
      command - the AgentX command.
      session - the master agent session.
      Returns:
      the response PDU.
    • unregister

      protected <A extends org.snmp4j.smi.Address> AgentXResponsePDU unregister(AgentXUnregisterPDU pdu, AgentXCommandEvent<A> event, AgentXMasterSession<A> session)
      Type Parameters:
      A - the address type.
      Parameters:
      pdu - the PDU.
      event - the AgentX command.
      session - the master agent session.
      Returns:
      the response PDU.
    • isDuplicate

      protected boolean isDuplicate(AgentXRegEntry<?> registration)
      Checks whether a new registration is a duplicate.
      Parameters:
      registration - a MIB region registration.
      Returns:
      true if another region with same or lower priority value intersects/overlaps with this region.
    • addRegistration

      protected void addRegistration(AgentXRegEntry<?> registration) throws org.snmp4j.agent.DuplicateRegistrationException
      Adds a registration to this master server.
      Parameters:
      registration - a MIB region registration.
      Throws:
      org.snmp4j.agent.DuplicateRegistrationException - if there is already another registration with lower priority value that overlaps/intersects with this region.
    • addRegion

      protected void addRegion(AgentXRegEntry<?> registration, AgentXRegion region) throws org.snmp4j.agent.DuplicateRegistrationException
      Adds a region to the registry.
      Parameters:
      registration - a new registration.
      region - the new region to add to an existing registration.
      Throws:
      org.snmp4j.agent.DuplicateRegistrationException - if there is already MIB region with lower or same priority value registered that intersects/overlaps with this region.
    • removeRegistration

      protected boolean removeRegistration(AgentXRegEntry<?> registration, Iterator<AgentXRegEntry<?>> regIterator)
      Remove a registration
      Parameters:
      registration - the registration to be removed.
      regIterator - an iterator on all region requests to be removed.
      Returns:
      true if a region was removed, false otherwise.
    • openSession

      public <A extends org.snmp4j.smi.Address> AgentXResponsePDU openSession(AgentXOpenPDU pdu, AgentXCommandEvent<A> event)
      Opens a new AgentX session.
      Type Parameters:
      A - the address type.
      Parameters:
      pdu - the AgentXOpenPDU.
      event - the AgentX command.
      Returns:
      the AgentXResponsePDU.
    • addPeer

      protected void addPeer(AgentXPeer<?> peer)
      Adds an AgentXPeer and fires the corresponding AgentXMasterEvent.
      Parameters:
      peer - a peer to add.
    • getPeer

      protected <A extends org.snmp4j.smi.Address> AgentXPeer<A> getPeer(A address)
      Gets a peer based on its address.
      Type Parameters:
      A - the address type.
      Parameters:
      address - an AgentX peer address.
      Returns:
      the AgentXPeer if found or null if not.
    • acceptSession

      protected int acceptSession(AgentXMasterSession<?> session)
      Accept a new session and fire the corresponding AgentXMasterEvent.SESSION_ADDED event.
      Parameters:
      session - a session.
      Returns:
      the veto reason, if the session has not been added or AgentXProtocol.AGENTX_SUCCESS otherwise.
    • addSession

      protected void addSession(AgentXMasterSession<?> session)
      Add a new session and fire the corresponding AgentXMasterEvent.SESSION_ADDED event.
      Parameters:
      session - a new session.
    • removeSession

      protected AgentXMasterSession<?> removeSession(int sessionID)
      Remove a session.
      Parameters:
      sessionID - the session ID.
      Returns:
      the removed AgentXMasterSession or null if the session no longer exists.
    • createResponse

      protected AgentXResponsePDU createResponse(AgentXPDU request, AgentXSession<?> session)
      Create a response for an AgentX request.
      Parameters:
      request - an AgentX PDU.
      session - a session.
      Returns:
      the generated AgentXResponsePDU.
    • sendResponse

      protected <A extends org.snmp4j.smi.Address> void sendResponse(AgentXPDU response, AgentXSession<A> session)
      Sends a response to a session.
      Type Parameters:
      A - the address type.
      Parameters:
      response - the response to be sent.
      session - the session.
    • connectionStateChanged

      public void connectionStateChanged(org.snmp4j.transport.TransportStateEvent change)
      Process a connection state change, i.e. remove the peer if the connect closed.
      Specified by:
      connectionStateChanged in interface org.snmp4j.transport.TransportStateListener
      Parameters:
      change - the TransportStateEvent.
    • removePeer

      protected AgentXPeer<?> removePeer(org.snmp4j.smi.Address peerAddress)
      Removes a peer and all its sessions.
      Parameters:
      peerAddress - the peer's address.
      Returns:
      the removed AgentXPeer.
    • closePeer

      protected AgentXPeer<?> closePeer(org.snmp4j.smi.Address peerAddress, byte reason)
      Close a peer, i.e. sending a close PDU to that peer.
      Parameters:
      peerAddress - the peer to inform about the connection closing.
      reason - the reason for closing the session(s), see AgentXProtocol.REASON_OTHER for example.
      Returns:
      the closed peer.
    • getAgentXVersion

      public byte getAgentXVersion()
      Gets the AgentX version.
      Returns:
      AgentXProtocol.VERSION_1_0
    • addAgentXMasterListener

      public void addAgentXMasterListener(AgentXMasterListener l)
      Parameters:
      l - a listener AgentX master agent events.
    • removeAgentXMasterListener

      public void removeAgentXMasterListener(AgentXMasterListener l)
      Parameters:
      l - a listener AgentX master agent events.
    • fireMasterChanged

      protected void fireMasterChanged(AgentXMasterEvent event)
      Parameters:
      event - the event to fire.
    • onResponse

      public void onResponse(AgentXResponseEvent<?> event)
      Specified by:
      onResponse in interface AgentXResponseListener
      Parameters:
      event - an response event including request and response PDU.
    • processAgentXGetResponse

      protected void processAgentXGetResponse(AgentXPending<?> pending, AgentXResponsePDU pdu)
      Process a pending GET response.
      Parameters:
      pending - the AgentXPending PDU.
      pdu - the AgentXResponsePDU returned.
    • processAgentXGetNextResponse

      protected void processAgentXGetNextResponse(AgentXPending<?> pending, AgentXResponsePDU pdu)
      Process a pending GETNEXT response.
      Parameters:
      pending - the AgentXPending PDU.
      pdu - the AgentXResponsePDU returned.
    • processAgentXSetResponse

      protected void processAgentXSetResponse(AgentXPending<?> pending, AgentXResponsePDU pdu)
      Process a pending SET response.
      Parameters:
      pending - the AgentXPending PDU.
      pdu - the AgentXResponsePDU returned.