Search in sources :

Example 1 with ColumnTracker

use of org.opennms.netmgt.snmp.ColumnTracker in project opennms by OpenNMS.

the class AggregateTrackerProxyTest method setUp.

@Before
public void setUp() {
    // Create a hierarchy of aggregated trackers
    columnTrackers = new ColumnTracker[] { new ColumnTracker(baseOids[0]), new ColumnTracker(baseOids[1]), new ColumnTracker(baseOids[2]) };
    singleInstanceTracker = new SingleInstanceTracker(baseOids[3], SnmpInstId.INST_ZERO);
    childAggregateTracker = new AggregateTracker(columnTrackers);
    parentAggregateTracker = new AggregateTracker(Lists.newArrayList(childAggregateTracker, singleInstanceTracker), gatherer);
    // Verify the generated requests
    List<WalkRequest> expectedRequests = new ArrayList<>();
    // Column tracker requests
    WalkRequest expectedRequest = new WalkRequest(baseOids[0]);
    expectedRequest.setCorrelationId("0-0");
    expectedRequest.setMaxRepetitions(2);
    expectedRequests.add(expectedRequest);
    expectedRequest = new WalkRequest(baseOids[1]);
    expectedRequest.setCorrelationId("0-1");
    expectedRequest.setMaxRepetitions(2);
    expectedRequests.add(expectedRequest);
    expectedRequest = new WalkRequest(baseOids[2]);
    expectedRequest.setCorrelationId("0-2");
    expectedRequest.setMaxRepetitions(2);
    expectedRequests.add(expectedRequest);
    // Single instance tracker request
    expectedRequest = new WalkRequest(baseOids[3]);
    expectedRequest.setCorrelationId("1");
    expectedRequest.setInstance(SnmpInstId.INST_ZERO);
    expectedRequest.setMaxRepetitions(1);
    expectedRequests.add(expectedRequest);
    assertThat(parentAggregateTracker.getWalkRequests(), hasSize(4));
    assertThat(parentAggregateTracker.getWalkRequests(), contains(expectedRequests.toArray()));
    // We shouldn't be finished yet
    assertThat(parentAggregateTracker.isFinished(), equalTo(false));
    assertThat(gatherer.getResults(), hasSize(0));
}
Also used : SingleInstanceTracker(org.opennms.netmgt.snmp.SingleInstanceTracker) ArrayList(java.util.ArrayList) ColumnTracker(org.opennms.netmgt.snmp.ColumnTracker) AggregateTracker(org.opennms.netmgt.snmp.AggregateTracker) Before(org.junit.Before)

Example 2 with ColumnTracker

use of org.opennms.netmgt.snmp.ColumnTracker 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(oid -> SnmpObjId.get(oid)).map(objId -> new ColumnTracker(objId)).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;
}
Also used : Collection(java.util.Collection) SnmpWalkCallback(org.opennms.netmgt.snmp.SnmpWalkCallback) CompletableFuture(java.util.concurrent.CompletableFuture) CollectionTracker(org.opennms.netmgt.snmp.CollectionTracker) SingleInstanceTracker(org.opennms.netmgt.snmp.SingleInstanceTracker) Collectors(java.util.stream.Collectors) Collectable(org.opennms.netmgt.snmp.Collectable) Executors(java.util.concurrent.Executors) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) AbstractXmlRpcModule(org.opennms.core.rpc.xml.AbstractXmlRpcModule) List(java.util.List) SnmpUtils(org.opennms.netmgt.snmp.SnmpUtils) AggregateTracker(org.opennms.netmgt.snmp.AggregateTracker) Map(java.util.Map) SnmpResult(org.opennms.netmgt.snmp.SnmpResult) ColumnTracker(org.opennms.netmgt.snmp.ColumnTracker) ThreadFactory(java.util.concurrent.ThreadFactory) SnmpValue(org.opennms.netmgt.snmp.SnmpValue) SnmpWalker(org.opennms.netmgt.snmp.SnmpWalker) ExecutorService(java.util.concurrent.ExecutorService) SnmpInstId(org.opennms.netmgt.snmp.SnmpInstId) SnmpObjId(org.opennms.netmgt.snmp.SnmpObjId) Collectable(org.opennms.netmgt.snmp.Collectable) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) CompletableFuture(java.util.concurrent.CompletableFuture) SnmpInstId(org.opennms.netmgt.snmp.SnmpInstId) CollectionTracker(org.opennms.netmgt.snmp.CollectionTracker) SnmpResult(org.opennms.netmgt.snmp.SnmpResult) AggregateTracker(org.opennms.netmgt.snmp.AggregateTracker) SnmpWalker(org.opennms.netmgt.snmp.SnmpWalker) SnmpObjId(org.opennms.netmgt.snmp.SnmpObjId) SnmpWalkCallback(org.opennms.netmgt.snmp.SnmpWalkCallback) SingleInstanceTracker(org.opennms.netmgt.snmp.SingleInstanceTracker) Collection(java.util.Collection) ColumnTracker(org.opennms.netmgt.snmp.ColumnTracker)

Aggregations

ArrayList (java.util.ArrayList)2 AggregateTracker (org.opennms.netmgt.snmp.AggregateTracker)2 ColumnTracker (org.opennms.netmgt.snmp.ColumnTracker)2 SingleInstanceTracker (org.opennms.netmgt.snmp.SingleInstanceTracker)2 Collection (java.util.Collection)1 LinkedHashMap (java.util.LinkedHashMap)1 List (java.util.List)1 Map (java.util.Map)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 ExecutorService (java.util.concurrent.ExecutorService)1 Executors (java.util.concurrent.Executors)1 ThreadFactory (java.util.concurrent.ThreadFactory)1 Collectors (java.util.stream.Collectors)1 Before (org.junit.Before)1 AbstractXmlRpcModule (org.opennms.core.rpc.xml.AbstractXmlRpcModule)1 Collectable (org.opennms.netmgt.snmp.Collectable)1 CollectionTracker (org.opennms.netmgt.snmp.CollectionTracker)1 SnmpInstId (org.opennms.netmgt.snmp.SnmpInstId)1 SnmpObjId (org.opennms.netmgt.snmp.SnmpObjId)1 SnmpResult (org.opennms.netmgt.snmp.SnmpResult)1