Class MockSnmpAgent

  • All Implemented Interfaces:
    java.lang.Runnable

    public class MockSnmpAgent
    extends org.snmp4j.agent.BaseAgent
    implements java.lang.Runnable
    The MockSnmpAgent class extends the SNMP4J BaseAgent class to provide a mock SNMP agent for SNMP-based OpenNMS tests. Large chunks of code were lifted from the org.snmp4j.agent.test.TestAgent class.
    Author:
    Jeff Gehlbach
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static boolean allowSetOnMissingOid  
      • Fields inherited from class org.snmp4j.agent.BaseAgent

        agent, agentState, bootCounterFile, configFileURI, defaultContext, defaultPersistenceProvider, defaultProxyForwarder, dispatcher, mpv3, notificationOriginator, server, session, snmp4jConfigMIB, snmp4jLogMIB, snmpCommunityMIB, snmpFrameworkMIB, snmpNotificationMIB, snmpProxyMIB, snmpTargetMIB, snmpv2MIB, STATE_CREATED, STATE_INIT_FINISHED, STATE_INIT_STARTED, STATE_RUNNING, STATE_STOPPED, sysDescr, sysOID, sysServices, transportMappings, usm, usmMIB, vacmMIB
    • Constructor Summary

      Constructors 
      Constructor Description
      MockSnmpAgent​(java.io.File confFile, java.net.URL moFile)  
      MockSnmpAgent​(java.io.File confFile, java.net.URL moFile, java.lang.String bindAddress)
      Creates the mock agent with files to read and store the boot counter, to read and store the agent configuration, and to read the mocked managed objects (MOs), plus a string describing the address and port to bind to.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      protected void addCommunities​(org.snmp4j.agent.mo.snmp.SnmpCommunityMIB communityMIB)
      protected void addNotificationTargets​(org.snmp4j.agent.mo.snmp.SnmpTargetMIB targetMIB, org.snmp4j.agent.mo.snmp.SnmpNotificationMIB notificationMIB)
      protected void addUsmUser​(org.snmp4j.security.USM usm)
      protected void addViews​(org.snmp4j.agent.mo.snmp.VacmMIB vacm)
      static MockSnmpAgent createAgentAndRun​(java.net.URL moFile, java.lang.String bindAddress)  
      protected java.util.List<org.snmp4j.agent.ManagedObject> createMockMOs()  
      java.net.InetAddress getInetAddress()  
      int getPort()  
      void initConfigMIB()  
      protected void initMessageDispatcher()
      Note that this method can hang if your system entropy is not high enough.
      void initSnmpSession()  
      protected void initTransportMappings()
      boolean isRunning()  
      boolean isStopped()  
      static void main​(java.lang.String[] args)  
      static AgentConfigData parseCli​(java.lang.String[] args)  
      protected void registerManagedObjects()
      protected void registerSnmpMIBs()
      void run()
      Starts the MockSnmpAgent running.
      void setupDefaultProxyForwarder()  
      void shutDown()  
      void shutDownAndWait()  
      java.lang.String toString()  
      protected void unregisterManagedObjects()
      protected void unregisterSnmpMIBs()
      void updateCounter32Value​(java.lang.String oid, int val)  
      void updateCounter64Value​(java.lang.String oid, long val)  
      void updateIntValue​(java.lang.String oid, int val)  
      void updateSession​(org.snmp4j.Session session)  
      void updateStringValue​(java.lang.String oid, java.lang.String val)  
      void updateValue​(java.lang.String oid, org.snmp4j.smi.Variable value)  
      void updateValue​(org.snmp4j.smi.OID oid, org.snmp4j.smi.Variable value)  
      void updateValuesFromResource​(java.net.URL moFile)  
      • Methods inherited from class org.snmp4j.agent.BaseAgent

        addShutdownHook, finishInit, getAgent, getAgentState, getBootCounterFile, getConfigFile, getContext, getDefaultContext, getDefaultProxyForwarder, getEngineBoots, getMPv3, getNotificationOriginator, getServer, getSession, getSnmp4jConfigMIB, getSnmp4jLogMIB, getSnmpCommunityMIB, getSnmpFrameworkMIB, getSnmpNotificationMIB, getSnmpProxyMIB, getSnmpTargetMIB, getSnmpv2MIB, getSysDescr, getSysOID, getSysServices, getUsm, getUsmMIB, getVacmMIB, init, loadConfig, saveConfig, sendColdStartNotification, setAgent, setBootCounterFile, setConfigFile, setDefaultContext, setDefaultProxyForwarder, setSysDescr, setSysOID, setSysServices, stop, updateEngineBoots
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • Field Detail

      • allowSetOnMissingOid

        public static boolean allowSetOnMissingOid
    • Constructor Detail

      • MockSnmpAgent

        public MockSnmpAgent​(java.io.File confFile,
                             java.net.URL moFile)
      • MockSnmpAgent

        public MockSnmpAgent​(java.io.File confFile,
                             java.net.URL moFile,
                             java.lang.String bindAddress)
        Creates the mock agent with files to read and store the boot counter, to read and store the agent configuration, and to read the mocked managed objects (MOs), plus a string describing the address and port to bind to.
        Parameters:
        moFile - a MIB dump file describing the managed objects to be mocked. The current implementation expects a Java properties file, which can conveniently be generated using the Net-SNMP utility snmpwalk with the -One option set.
    • Method Detail

      • createAgentAndRun

        public static MockSnmpAgent createAgentAndRun​(java.net.URL moFile,
                                                      java.lang.String bindAddress)
                                               throws java.lang.InterruptedException
        Throws:
        java.lang.InterruptedException
      • main

        public static void main​(java.lang.String[] args)
                         throws java.net.UnknownHostException,
                                java.net.MalformedURLException
        Throws:
        java.net.UnknownHostException
        java.net.MalformedURLException
      • parseCli

        public static AgentConfigData parseCli​(java.lang.String[] args)
                                        throws java.net.UnknownHostException,
                                               java.net.MalformedURLException
        Throws:
        java.net.UnknownHostException
        java.net.MalformedURLException
      • initMessageDispatcher

        protected void initMessageDispatcher()

        Note that this method can hang if your system entropy is not high enough. Here is a stack trace from a test running on JDK 1.8u40:

         
         Thread [MockSnmpAgent-1657048932] (Suspended)
         owns: SecureRandom  (id=26)
         owns: SecureRandom  (id=27)
         owns: SecurityProtocols  (id=28)
         FileInputStream.readBytes(byte[], int, int) line: not available [native method]
         FileInputStream.read(byte[], int, int) line: 255
         NativeSeedGenerator(SeedGenerator$URLSeedGenerator).getSeedBytes(byte[]) line: 539
         SeedGenerator.generateSeed(byte[]) line: 144
         SecureRandom$SeederHolder.() line: 203 [local variables unavailable]
         SecureRandom.engineNextBytes(byte[]) line: 221
         SecureRandom.nextBytes(byte[]) line: 468
         Salt.() line: 54
         Salt.getInstance() line: 79
         PrivDES.() line: 57
         SecurityProtocols.addDefaultProtocols() line: 155
         MockSnmpAgent.initMessageDispatcher() line: 306
         MockSnmpAgent(BaseAgent).init() line: 145
         MockSnmpAgent.run() line: 380
         Thread.run() line: 745
         
        Overrides:
        initMessageDispatcher in class org.snmp4j.agent.BaseAgent
      • initSnmpSession

        public void initSnmpSession()
        Overrides:
        initSnmpSession in class org.snmp4j.agent.BaseAgent
      • initConfigMIB

        public void initConfigMIB()
        Overrides:
        initConfigMIB in class org.snmp4j.agent.BaseAgent
      • setupDefaultProxyForwarder

        public void setupDefaultProxyForwarder()
        Overrides:
        setupDefaultProxyForwarder in class org.snmp4j.agent.BaseAgent
      • updateSession

        public void updateSession​(org.snmp4j.Session session)
        Overrides:
        updateSession in class org.snmp4j.agent.BaseAgent
      • shutDownAndWait

        public void shutDownAndWait()
                             throws java.lang.InterruptedException
        Throws:
        java.lang.InterruptedException
      • run

        public void run()
        Starts the MockSnmpAgent running. Meant to be called from the start method of class Thread, but could also be used to bring up a standalone mock agent.
        Specified by:
        run in interface java.lang.Runnable
        Overrides:
        run in class org.snmp4j.agent.BaseAgent
        See Also:
        BaseAgent.run()
      • shutDown

        public void shutDown()
      • isRunning

        public boolean isRunning()
      • isStopped

        public boolean isStopped()
      • addCommunities

        protected void addCommunities​(org.snmp4j.agent.mo.snmp.SnmpCommunityMIB communityMIB)
        Specified by:
        addCommunities in class org.snmp4j.agent.BaseAgent
      • addViews

        protected void addViews​(org.snmp4j.agent.mo.snmp.VacmMIB vacm)
        Specified by:
        addViews in class org.snmp4j.agent.BaseAgent
      • addNotificationTargets

        protected void addNotificationTargets​(org.snmp4j.agent.mo.snmp.SnmpTargetMIB targetMIB,
                                              org.snmp4j.agent.mo.snmp.SnmpNotificationMIB notificationMIB)
        Specified by:
        addNotificationTargets in class org.snmp4j.agent.BaseAgent
      • addUsmUser

        protected void addUsmUser​(org.snmp4j.security.USM usm)
        Specified by:
        addUsmUser in class org.snmp4j.agent.BaseAgent
      • initTransportMappings

        protected void initTransportMappings()
                                      throws java.io.IOException
        Overrides:
        initTransportMappings in class org.snmp4j.agent.BaseAgent
        Throws:
        java.io.IOException
      • getInetAddress

        public java.net.InetAddress getInetAddress()
      • getPort

        public int getPort()
      • registerSnmpMIBs

        protected void registerSnmpMIBs()
        Overrides:
        registerSnmpMIBs in class org.snmp4j.agent.BaseAgent
      • unregisterSnmpMIBs

        protected void unregisterSnmpMIBs()
        Overrides:
        unregisterSnmpMIBs in class org.snmp4j.agent.BaseAgent
      • registerManagedObjects

        protected void registerManagedObjects()
        Specified by:
        registerManagedObjects in class org.snmp4j.agent.BaseAgent
      • unregisterManagedObjects

        protected void unregisterManagedObjects()
        Specified by:
        unregisterManagedObjects in class org.snmp4j.agent.BaseAgent
      • createMockMOs

        protected java.util.List<org.snmp4j.agent.ManagedObject> createMockMOs()
      • updateValue

        public void updateValue​(org.snmp4j.smi.OID oid,
                                org.snmp4j.smi.Variable value)
      • updateValue

        public void updateValue​(java.lang.String oid,
                                org.snmp4j.smi.Variable value)
      • updateIntValue

        public void updateIntValue​(java.lang.String oid,
                                   int val)
      • updateStringValue

        public void updateStringValue​(java.lang.String oid,
                                      java.lang.String val)
      • updateCounter32Value

        public void updateCounter32Value​(java.lang.String oid,
                                         int val)
      • updateCounter64Value

        public void updateCounter64Value​(java.lang.String oid,
                                         long val)
      • updateValuesFromResource

        public void updateValuesFromResource​(java.net.URL moFile)
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object