use of org.opennms.netmgt.snmp.SnmpAgentAddress in project opennms by OpenNMS.
the class MockSnmpStrategyTest method setUp.
@Before
public void setUp() throws Exception {
m_strategy = new MockSnmpStrategy();
MockSnmpStrategy.setDataForAddress(new SnmpAgentAddress(m_agentAddress, m_agentPort), new ClassPathResource("loadSnmpDataTest.properties"));
m_oldProperty = System.getProperty("org.opennms.snmp.strategyClass");
System.setProperty("org.opennms.snmp.strategyClass", m_strategy.getClass().getName());
}
use of org.opennms.netmgt.snmp.SnmpAgentAddress in project opennms by OpenNMS.
the class JUnitSnmpAgentExecutionListener method handleSnmpAgent.
private void handleSnmpAgent(final TestContext testContext, final JUnitSnmpAgent config, boolean useMockSnmpStrategy, MockSnmpDataProvider provider) throws IOException, UnknownHostException, InterruptedException {
if (config == null)
return;
String factoryClassName = "unknown";
try {
final SnmpAgentConfigFactory factory = testContext.getApplicationContext().getBean("snmpPeerFactory", SnmpAgentConfigFactory.class);
factoryClassName = factory.getClass().getName();
} catch (final Exception e) {
// ignore
}
if (!factoryClassName.contains("ProxySnmpAgentConfigFactory")) {
LOG.warn("SNMP Peer Factory ({}) is not the ProxySnmpAgentConfigFactory -- did you forget to include applicationContext-proxy-snmp.xml?", factoryClassName);
}
LOG.debug("handleSnmpAgent(testContext, {}, {})", config, useMockSnmpStrategy);
String host = config.host();
if (host == null || "".equals(host)) {
/*
* NOTE: This call produces different results on different platforms so make
* sure your client code is aware of this. If you use the {@link ProxySnmpAgentConfigFactory}
* by including the <code>classpath:/META-INF/opennms/applicationContext-proxy-snmp.xml</code>
* Spring context, you probably won't need to deal with this. It will override the
* SnmpPeerFactory with the correct values.
*
* Linux: 127.0.0.1
* Mac OS: primary external interface
*/
host = InetAddressUtils.getLocalHostAddressAsString();
//host = "127.0.0.1";
}
final ResourceLoader loader = new DefaultResourceLoader();
final Resource resource = loader.getResource(config.resource());
// NOTE: The default value for config.port is specified inside {@link JUnitSnmpAgent}
final InetAddress hostAddress = addr(host);
final int port = config.port();
final SnmpAgentAddress agentAddress = new SnmpAgentAddress(hostAddress, port);
final SnmpAgentConfigProxyMapper mapper = SnmpAgentConfigProxyMapper.getInstance();
if (useMockSnmpStrategy) {
// since it's all virtual, the "mapped" port just points to the real agent address
mapper.addProxy(hostAddress, agentAddress);
} else {
MockSnmpAgent agent = null;
try {
agent = MockSnmpAgent.createAgentAndRun(resource.getURL(), str(InetAddress.getLocalHost()) + "/0");
} catch (Throwable e) {
agent = MockSnmpAgent.createAgentAndRun(resource.getURL(), str(InetAddressUtils.ONE_TWENTY_SEVEN) + "/0");
}
SnmpAgentAddress listenAddress = new SnmpAgentAddress(agent.getInetAddress(), agent.getPort());
mapper.addProxy(hostAddress, listenAddress);
testContext.setAttribute(IPADDRESS_KEY, listenAddress.getAddress());
testContext.setAttribute(PORT_KEY, listenAddress.getPort());
LOG.debug("using MockSnmpAgent on {} for 'real' address {}", listenAddress, agentAddress);
provider.addAgent(agentAddress, agent);
}
provider.setDataForAddress(agentAddress, resource);
}
use of org.opennms.netmgt.snmp.SnmpAgentAddress in project opennms by OpenNMS.
the class ProxySnmpAgentConfigFactory method getAgentConfig.
@Override
public SnmpAgentConfig getAgentConfig(final InetAddress address, String location) {
final SnmpAgentConfigProxyMapper mapper = SnmpAgentConfigProxyMapper.getInstance();
final SnmpAgentAddress agentAddress = mapper.getAddress(address);
final String addressString = str(address);
if (agentAddress == null) {
LOG.debug("No agent address mapping found for {}! Try adding a @JUnitSnmpAgent(host=\"{}\", resource=\"...\" entry...", addressString, addressString);
return super.getAgentConfig(address, location);
}
final SnmpAgentConfig config = new SnmpAgentConfig(agentAddress.getAddress());
config.setProxyFor(address);
config.setPort(agentAddress.getPort());
LOG.debug("proxying {} -> {}", addressString, agentAddress);
return config;
}
use of org.opennms.netmgt.snmp.SnmpAgentAddress in project opennms by OpenNMS.
the class ProvisionerIT method testProvisionerServiceRescanAfterAddingSnmpNms7838.
// fail if we take more than five minutes
@Test(timeout = 300000)
// Start the test with an empty SNMP agent
@JUnitSnmpAgent(host = "198.51.100.201", port = 161, resource = "classpath:/snmpwalk-empty.properties")
public void testProvisionerServiceRescanAfterAddingSnmpNms7838() throws Exception {
importFromResource("classpath:/requisition_then_scan2.xml", Boolean.TRUE.toString());
final List<OnmsNode> nodes = getNodeDao().findAll();
final OnmsNode node = nodes.get(0);
runPendingScans();
m_nodeDao.flush();
assertEquals(1, getInterfaceDao().countAll());
// Make sure that the OnmsIpInterface doesn't have an ifIndex
assertNull(getInterfaceDao().get(node, "198.51.100.201").getIfIndex());
// Make sure that no OnmsSnmpInterface records exist for the node
assertNull(getSnmpInterfaceDao().findByNodeIdAndIfIndex(node.getId(), 4));
assertNull(getSnmpInterfaceDao().findByNodeIdAndIfIndex(node.getId(), 5));
LOG.info("******************** ADDING SNMP DATA ********************");
// Add some SNMP data to the agent
m_mockSnmpDataProvider.setDataForAddress(new SnmpAgentAddress(addr("198.51.100.201"), 161), new DefaultResourceLoader().getResource("classpath:/snmpTestData3.properties"));
// Rescan
m_mockEventIpcManager.sendEventToListeners(nodeUpdated(node.getId()));
runPendingScans();
m_nodeDao.flush();
// Make sure that a second interface was added from the SNMP agent data
assertEquals(2, getInterfaceDao().countAll());
// Verify the ifIndex entries
assertEquals(5, getInterfaceDao().get(node, "198.51.100.201").getIfIndex().intValue());
assertEquals(5, getInterfaceDao().get(node, "198.51.100.201").getSnmpInterface().getIfIndex().intValue());
assertEquals(4, getInterfaceDao().get(node, "198.51.100.204").getIfIndex().intValue());
assertEquals(4, getInterfaceDao().get(node, "198.51.100.204").getSnmpInterface().getIfIndex().intValue());
}
use of org.opennms.netmgt.snmp.SnmpAgentAddress in project opennms by OpenNMS.
the class MockSnmpStrategy method createWalker.
@Override
public SnmpWalker createWalker(final SnmpAgentConfig agentConfig, final String name, final CollectionTracker tracker) {
LOG.debug("createWalker({}/{}, {}, {})", InetAddrUtils.str(agentConfig.getAddress()), agentConfig.getPort(), name, tracker.getClass().getName());
final SnmpAgentAddress aa = new SnmpAgentAddress(agentConfig.getAddress(), agentConfig.getPort());
final PropertyOidContainer oidContainer = getOidContainer(aa);
return new MockSnmpWalker(aa, agentConfig.getVersion(), oidContainer, name, tracker, agentConfig.getMaxVarsPerPdu(), agentConfig.getRetries());
}
Aggregations