use of org.opennms.netmgt.snmp.SnmpWalker in project opennms by OpenNMS.
the class MockSnmpStrategyTest method testTracker.
@Test
public void testTracker() throws Exception {
final CountingColumnTracker ct = new CountingColumnTracker(SnmpObjId.get(".1.3.5.1.1"));
try (SnmpWalker walker = walk(ct, 10, 3)) {
walker.start();
walker.waitFor();
}
assertEquals("number of columns returned must match test data", Long.valueOf(9).longValue(), ct.getCount());
}
use of org.opennms.netmgt.snmp.SnmpWalker in project opennms by OpenNMS.
the class MockSnmpStrategyTest method toCompletableFuture.
private static CompletableFuture<Long> toCompletableFuture(CountingColumnTracker ct, SnmpWalker walker) {
final CompletableFuture<Long> future = new CompletableFuture<>();
walker.setCallback(new SnmpWalkCallback() {
@Override
public void complete(SnmpWalker tracker, Throwable t) {
if (t != null) {
future.completeExceptionally(t);
} else {
future.complete(ct.getCount());
}
}
});
return future;
}
use of org.opennms.netmgt.snmp.SnmpWalker in project opennms by OpenNMS.
the class MockSnmpStrategyTest method testTrackerTimeout.
@Test
public void testTrackerTimeout() throws Exception {
final CountingColumnTracker ct = new CountingColumnTracker(SnmpObjId.get(".1.3.5.1.1"));
final SnmpAgentConfig sac = getAgentConfig();
sac.setPort(12345);
try (final SnmpWalker walker = SnmpUtils.createWalker(sac, "test", ct)) {
assertNotNull(walker);
walker.start();
walker.waitFor();
}
assertEquals("it should match no columns (timeout)", Long.valueOf(0).longValue(), ct.getCount());
}
use of org.opennms.netmgt.snmp.SnmpWalker in project opennms by OpenNMS.
the class SnmpProxyRpcModule method walk.
private CompletableFuture<Collection<SnmpResponseDTO>> walk(SnmpRequestDTO request, List<SnmpWalkRequestDTO> walks) {
final CompletableFuture<Collection<SnmpResponseDTO>> future = new CompletableFuture<>();
final Map<String, SnmpResponseDTO> responsesByCorrelationId = new LinkedHashMap<>();
final List<Collectable> trackers = new ArrayList<>(walks.size());
for (final SnmpWalkRequestDTO walk : walks) {
CollectionTracker tracker;
if (walk.getInstance() != null) {
if (walk.getOids().size() != 1) {
future.completeExceptionally(new IllegalArgumentException("Single instance requests must have a single OID."));
return future;
}
final SnmpObjId oid = walk.getOids().get(0);
tracker = new SingleInstanceTracker(oid, new SnmpInstId(walk.getInstance())) {
@Override
protected void storeResult(SnmpResult res) {
addResult(res, walk.getCorrelationId(), responsesByCorrelationId);
}
};
} else {
final Collection<Collectable> columnTrackers = walk.getOids().stream().map(SnmpObjId::get).map(ColumnTracker::new).collect(Collectors.toList());
tracker = new AggregateTracker(columnTrackers) {
@Override
protected void storeResult(SnmpResult res) {
addResult(res, walk.getCorrelationId(), responsesByCorrelationId);
}
};
}
if (walk.getMaxRepetitions() != null) {
tracker.setMaxRepetitions(walk.getMaxRepetitions());
}
trackers.add(tracker);
}
AggregateTracker aggregate = new AggregateTracker(trackers);
final SnmpWalker walker = SnmpUtils.createWalker(request.getAgent(), request.getDescription(), aggregate);
walker.setCallback(new SnmpWalkCallback() {
@Override
public void complete(SnmpWalker tracker, Throwable t) {
try {
if (t != null) {
future.completeExceptionally(t);
} else {
future.complete(responsesByCorrelationId.values());
}
} finally {
// Close the tracker using a separate thread
// This allows the SnmpWalker to clean up properly instead
// of interrupting execution as it's executing the callback
REAPER_EXECUTOR.submit(new Runnable() {
@Override
public void run() {
tracker.close();
}
});
}
}
});
walker.start();
return future;
}
use of org.opennms.netmgt.snmp.SnmpWalker in project opennms by OpenNMS.
the class LocationAwareSnmpClientIT method canWalkIpAddressTableDirectly.
/**
* Verifies that SNMP WALKs are successful when directly using SnmpUtils.
*
* Used a basis for comparison.
*/
@Test
public void canWalkIpAddressTableDirectly() throws InterruptedException {
// Gather the list of IP addresses
final IPAddressGatheringTracker tracker = new IPAddressGatheringTracker();
try (SnmpWalker walker = SnmpUtils.createWalker(agentConfig, tracker.getDescription(), tracker)) {
walker.start();
walker.waitFor();
}
ExpectedResults.compareToKnownIpAddressList(tracker.getIpAddresses());
// Now determine their interface indices using a different type of tracker
final Set<SnmpInstId> ipAddrs = new TreeSet<>();
for (final String ipAddr : tracker.getIpAddresses()) {
ipAddrs.add(new SnmpInstId(InetAddressUtils.toOid(InetAddressUtils.addr(ipAddr))));
}
IpAddrTable ipAddrTable = new IpAddrTable(agentConfig.getAddress(), ipAddrs);
try (SnmpWalker walker = SnmpUtils.createWalker(agentConfig, "ipAddrTable", ipAddrTable)) {
walker.start();
walker.waitFor();
}
ExpectedResults.compareToKnownIfIndices(ipAddrTable.getIfIndices());
}
Aggregations