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
TheMockSnmpAgent
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
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
MockSnmpAgent.MockUdpTransportMapping
-
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(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 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(URL moFile, String bindAddress)
protected List<org.snmp4j.agent.ManagedObject>
createMockMOs()
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(String[] args)
static AgentConfigData
parseCli(String[] args)
protected void
registerManagedObjects()
protected void
registerSnmpMIBs()
void
run()
Starts theMockSnmpAgent
running.void
setupDefaultProxyForwarder()
void
shutDown()
void
shutDownAndWait()
String
toString()
protected void
unregisterManagedObjects()
protected void
unregisterSnmpMIBs()
void
updateCounter32Value(String oid, int val)
void
updateCounter64Value(String oid, long val)
void
updateIntValue(String oid, int val)
void
updateSession(org.snmp4j.Session session)
void
updateStringValue(String oid, String val)
void
updateValue(String oid, org.snmp4j.smi.Variable value)
void
updateValue(org.snmp4j.smi.OID oid, org.snmp4j.smi.Variable value)
void
updateValuesFromResource(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 utilitysnmpwalk
with the-One
option 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:
initMessageDispatcher
in classorg.snmp4j.agent.BaseAgent
-
initSnmpSession
public void initSnmpSession()
- Overrides:
initSnmpSession
in classorg.snmp4j.agent.BaseAgent
-
initConfigMIB
public void initConfigMIB()
- Overrides:
initConfigMIB
in classorg.snmp4j.agent.BaseAgent
-
setupDefaultProxyForwarder
public void setupDefaultProxyForwarder()
- Overrides:
setupDefaultProxyForwarder
in classorg.snmp4j.agent.BaseAgent
-
updateSession
public void updateSession(org.snmp4j.Session session)
- Overrides:
updateSession
in classorg.snmp4j.agent.BaseAgent
-
shutDownAndWait
public void shutDownAndWait() throws InterruptedException
- Throws:
InterruptedException
-
run
public void run()
Starts theMockSnmpAgent
running. Meant to be called from thestart
method 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:
addCommunities
in classorg.snmp4j.agent.BaseAgent
-
addViews
protected void addViews(org.snmp4j.agent.mo.snmp.VacmMIB vacm)
- Specified by:
addViews
in classorg.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 classorg.snmp4j.agent.BaseAgent
-
addUsmUser
protected void addUsmUser(org.snmp4j.security.USM usm)
- Specified by:
addUsmUser
in classorg.snmp4j.agent.BaseAgent
-
initTransportMappings
protected void initTransportMappings() throws IOException
- Overrides:
initTransportMappings
in classorg.snmp4j.agent.BaseAgent
- Throws:
IOException
-
getInetAddress
public InetAddress getInetAddress()
-
getPort
public int getPort()
-
registerSnmpMIBs
protected void registerSnmpMIBs()
- Overrides:
registerSnmpMIBs
in classorg.snmp4j.agent.BaseAgent
-
unregisterSnmpMIBs
protected void unregisterSnmpMIBs()
- Overrides:
unregisterSnmpMIBs
in classorg.snmp4j.agent.BaseAgent
-
registerManagedObjects
protected void registerManagedObjects()
- Specified by:
registerManagedObjects
in classorg.snmp4j.agent.BaseAgent
-
unregisterManagedObjects
protected void unregisterManagedObjects()
- Specified by:
unregisterManagedObjects
in 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)
-
-