Search in sources :

Example 1 with SnmpResult

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

the class TableTrackerProxyTest method canHandleAValidResponse.

@Test
public void canHandleAValidResponse() {
    // Build a response with an OID from the requested table
    SnmpValue value = mock(SnmpValue.class);
    SnmpResult result = new SnmpResult(table, SnmpInstId.INST_ZERO, value);
    WalkResponse response = new WalkResponse(Collections.singletonList(result));
    // Resolve the walker
    tracker.handleWalkResponses(Collections.singletonList(response));
    // We should be finished, and have captured the expected value
    assertThat(tracker.isFinished(), equalTo(true));
    assertThat(rows, hasSize(1));
    assertThat(rows.get(0).getValue(table), equalTo(value));
}
Also used : SnmpValue(org.opennms.netmgt.snmp.SnmpValue) SnmpResult(org.opennms.netmgt.snmp.SnmpResult) Test(org.junit.Test)

Example 2 with SnmpResult

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

the class AggregateTrackerProxyTest method canHandleValidResponses.

@Test
public void canHandleValidResponses() {
    // Build responses
    SnmpValue value = mock(SnmpValue.class);
    List<WalkResponse> responses = Lists.newArrayList(new WalkResponse(Collections.singletonList(new SnmpResult(baseOids[0], SnmpInstId.INST_ZERO, value)), "0-0"), new WalkResponse(Collections.singletonList(new SnmpResult(baseOids[1], SnmpInstId.INST_ZERO, value)), "0-1"), new WalkResponse(Collections.singletonList(new SnmpResult(baseOids[2], SnmpInstId.INST_ZERO, value)), "0-2"), new WalkResponse(Collections.singletonList(new SnmpResult(baseOids[3], SnmpInstId.INST_ZERO, value)), "1"));
    // Resolve the walker
    parentAggregateTracker.handleWalkResponses(responses);
    // We should be finished, and have captured the expected results
    assertThat(parentAggregateTracker.isFinished(), equalTo(true));
    assertThat(gatherer.getResults(), hasSize(4));
}
Also used : SnmpValue(org.opennms.netmgt.snmp.SnmpValue) SnmpResult(org.opennms.netmgt.snmp.SnmpResult) Test(org.junit.Test)

Example 3 with SnmpResult

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

the class ColumnTrackerProxyTest method canHandleValidResponses.

@Test
public void canHandleValidResponses() {
    // Build a response with the requested OID
    SnmpValue value = mock(SnmpValue.class);
    List<SnmpResult> results = new ArrayList<>();
    results.add(new SnmpResult(base, SnmpInstId.INST_ZERO, value));
    // Outside of base
    results.add(new SnmpResult(SnmpObjId.get(".1.3.6.1.2.2"), SnmpInstId.INST_ZERO, null));
    WalkResponse response = new WalkResponse(results);
    // Resolve the walker
    tracker.handleWalkResponses(Collections.singletonList(response));
    // We should be finished, and have captured the expected value
    assertThat(tracker.isFinished(), equalTo(true));
    assertThat(gatherer.getResults(), hasSize(1));
    assertThat(gatherer.getResults().get(0).getValue(), equalTo(value));
}
Also used : SnmpValue(org.opennms.netmgt.snmp.SnmpValue) ArrayList(java.util.ArrayList) SnmpResult(org.opennms.netmgt.snmp.SnmpResult) Test(org.junit.Test)

Example 4 with SnmpResult

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

the class SingleInstanceTrackerProxyTest method canHandleResponsesForOtherOids.

@Test
public void canHandleResponsesForOtherOids() {
    // Build a response for another OID
    SnmpValue value = mock(SnmpValue.class);
    SnmpResult result = new SnmpResult(SnmpObjId.get(".1.3.6.1.2.1.1.1"), instance, value);
    WalkResponse response = new WalkResponse(Collections.singletonList(result));
    // Resolve the walker
    tracker.handleWalkResponses(Collections.singletonList(response));
    // We should be finished, without any captured values
    assertThat(tracker.isFinished(), equalTo(true));
    assertThat(gatherer.getResults(), hasSize(0));
}
Also used : SnmpValue(org.opennms.netmgt.snmp.SnmpValue) SnmpResult(org.opennms.netmgt.snmp.SnmpResult) Test(org.junit.Test)

Example 5 with SnmpResult

use of org.opennms.netmgt.snmp.SnmpResult 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

SnmpResult (org.opennms.netmgt.snmp.SnmpResult)17 SnmpInstId (org.opennms.netmgt.snmp.SnmpInstId)9 SnmpValue (org.opennms.netmgt.snmp.SnmpValue)9 Test (org.junit.Test)8 SnmpObjId (org.opennms.netmgt.snmp.SnmpObjId)7 ArrayList (java.util.ArrayList)5 HashMap (java.util.HashMap)4 AttributeGroupType (org.opennms.netmgt.collection.api.AttributeGroupType)4 ServiceParameters (org.opennms.netmgt.collection.api.ServiceParameters)4 MibObject (org.opennms.netmgt.config.datacollection.MibObject)4 MockDataCollectionConfig (org.opennms.netmgt.mock.MockDataCollectionConfig)4 RrdRepository (org.opennms.netmgt.rrd.RrdRepository)4 CollectionResource (org.opennms.netmgt.collection.api.CollectionResource)3 RrdPersistOperationBuilder (org.opennms.netmgt.collection.persistence.rrd.RrdPersistOperationBuilder)3 SnmpAgentConfig (org.opennms.netmgt.snmp.SnmpAgentConfig)3 File (java.io.File)2 List (java.util.List)2 CompletableFuture (java.util.concurrent.CompletableFuture)2 Collectors (java.util.stream.Collectors)2 OnmsNode (org.opennms.netmgt.model.OnmsNode)2