Package org.opennms.mock.snmp
Class MockSnmpAgent
- java.lang.Object
-
- org.snmp4j.agent.BaseAgent
-
- org.opennms.mock.snmp.MockSnmpAgent
-
- All Implemented Interfaces:
Runnable
public class MockSnmpAgent extends org.snmp4j.agent.BaseAgent implements Runnable
TheMockSnmpAgentclass 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
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classMockSnmpAgent.MockUdpTransportMapping
-
Field Summary
Fields Modifier and Type Field Description static booleanallowSetOnMissingOid-
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(File confFile, URL moFile)MockSnmpAgent(File confFile, URL moFile, 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 voidaddCommunities(org.snmp4j.agent.mo.snmp.SnmpCommunityMIB communityMIB)protected voidaddNotificationTargets(org.snmp4j.agent.mo.snmp.SnmpTargetMIB targetMIB, org.snmp4j.agent.mo.snmp.SnmpNotificationMIB notificationMIB)protected voidaddUsmUser(org.snmp4j.security.USM usm)protected voidaddViews(org.snmp4j.agent.mo.snmp.VacmMIB vacm)static MockSnmpAgentcreateAgentAndRun(URL moFile, String bindAddress)protected List<org.snmp4j.agent.ManagedObject>createMockMOs()InetAddressgetInetAddress()intgetPort()voidinitConfigMIB()protected voidinitMessageDispatcher()Note that this method can hang if your system entropy is not high enough.voidinitSnmpSession()protected voidinitTransportMappings()booleanisRunning()booleanisStopped()static voidmain(String[] args)static AgentConfigDataparseCli(String[] args)protected voidregisterManagedObjects()protected voidregisterSnmpMIBs()voidrun()Starts theMockSnmpAgentrunning.voidsetupDefaultProxyForwarder()voidshutDown()voidshutDownAndWait()StringtoString()protected voidunregisterManagedObjects()protected voidunregisterSnmpMIBs()voidupdateCounter32Value(String oid, int val)voidupdateCounter64Value(String oid, long val)voidupdateIntValue(String oid, int val)voidupdateSession(org.snmp4j.Session session)voidupdateStringValue(String oid, String val)voidupdateValue(String oid, org.snmp4j.smi.Variable value)voidupdateValue(org.snmp4j.smi.OID oid, org.snmp4j.smi.Variable value)voidupdateValuesFromResource(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
-
-
-
-
Constructor Detail
-
MockSnmpAgent
public MockSnmpAgent(File confFile, URL moFile, 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 utilitysnmpwalkwith the-Oneoption set.
-
-
Method Detail
-
createAgentAndRun
public static MockSnmpAgent createAgentAndRun(URL moFile, String bindAddress) throws InterruptedException
- Throws:
InterruptedException
-
main
public static void main(String[] args) throws UnknownHostException, MalformedURLException
-
parseCli
public static AgentConfigData parseCli(String[] args) throws UnknownHostException, 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:
initMessageDispatcherin classorg.snmp4j.agent.BaseAgent
-
initSnmpSession
public void initSnmpSession()
- Overrides:
initSnmpSessionin classorg.snmp4j.agent.BaseAgent
-
initConfigMIB
public void initConfigMIB()
- Overrides:
initConfigMIBin classorg.snmp4j.agent.BaseAgent
-
setupDefaultProxyForwarder
public void setupDefaultProxyForwarder()
- Overrides:
setupDefaultProxyForwarderin classorg.snmp4j.agent.BaseAgent
-
updateSession
public void updateSession(org.snmp4j.Session session)
- Overrides:
updateSessionin classorg.snmp4j.agent.BaseAgent
-
shutDownAndWait
public void shutDownAndWait() throws InterruptedException- Throws:
InterruptedException
-
run
public void run()
Starts theMockSnmpAgentrunning. Meant to be called from thestartmethod of classThread, but could also be used to bring up a standalone mock agent.
-
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:
addCommunitiesin classorg.snmp4j.agent.BaseAgent
-
addViews
protected void addViews(org.snmp4j.agent.mo.snmp.VacmMIB vacm)
- Specified by:
addViewsin classorg.snmp4j.agent.BaseAgent
-
addNotificationTargets
protected void addNotificationTargets(org.snmp4j.agent.mo.snmp.SnmpTargetMIB targetMIB, org.snmp4j.agent.mo.snmp.SnmpNotificationMIB notificationMIB)- Specified by:
addNotificationTargetsin classorg.snmp4j.agent.BaseAgent
-
addUsmUser
protected void addUsmUser(org.snmp4j.security.USM usm)
- Specified by:
addUsmUserin classorg.snmp4j.agent.BaseAgent
-
initTransportMappings
protected void initTransportMappings() throws IOException- Overrides:
initTransportMappingsin classorg.snmp4j.agent.BaseAgent- Throws:
IOException
-
getInetAddress
public InetAddress getInetAddress()
-
getPort
public int getPort()
-
registerSnmpMIBs
protected void registerSnmpMIBs()
- Overrides:
registerSnmpMIBsin classorg.snmp4j.agent.BaseAgent
-
unregisterSnmpMIBs
protected void unregisterSnmpMIBs()
- Overrides:
unregisterSnmpMIBsin classorg.snmp4j.agent.BaseAgent
-
registerManagedObjects
protected void registerManagedObjects()
- Specified by:
registerManagedObjectsin classorg.snmp4j.agent.BaseAgent
-
unregisterManagedObjects
protected void unregisterManagedObjects()
- Specified by:
unregisterManagedObjectsin classorg.snmp4j.agent.BaseAgent
-
createMockMOs
protected List<org.snmp4j.agent.ManagedObject> createMockMOs()
-
updateValue
public void updateValue(org.snmp4j.smi.OID oid, org.snmp4j.smi.Variable value)
-
updateValue
public void updateValue(String oid, org.snmp4j.smi.Variable value)
-
updateIntValue
public void updateIntValue(String oid, int val)
-
updateCounter32Value
public void updateCounter32Value(String oid, int val)
-
updateCounter64Value
public void updateCounter64Value(String oid, long val)
-
updateValuesFromResource
public void updateValuesFromResource(URL moFile)
-
-