SNMPImpl implements SnmpService, AutoCloseable { @Override public ListenableFuture<RpcResult<GetInterfacesOutput>> getInterfaces(final GetInterfacesInput getInterfacesInput) { final SettableFuture<RpcResult<GetInterfacesOutput>> settableFuture = SettableFuture.create(); Runnable nonBlockingPopulateRunnable = () -> { MibTable<IfEntryBuilder> ifEntryBuilderMibTable = new MibTable<>(snmp, getInterfacesInput.getIpAddress(), getInterfacesInput.getCommunity(), IfEntryBuilder.class); GetInterfacesOutputBuilder getInterfacesOutputBuilder = new GetInterfacesOutputBuilder(); Map<Integer, IfEntryBuilder> ifEntryBuilders = ifEntryBuilderMibTable.populate(); List<IfEntry> ifEntries = new ArrayList<>(ifEntryBuilders.size()); for (IfEntryBuilder ifEntryBuilder: ifEntryBuilders.values()) { ifEntries.add(ifEntryBuilder.build()); } getInterfacesOutputBuilder.setIfEntry(ifEntries) .setIfNumber(ifEntries.size()); RpcResultBuilder<GetInterfacesOutput> rpcResultBuilder = RpcResultBuilder.success( getInterfacesOutputBuilder.build()); settableFuture.set(rpcResultBuilder.build()); }; Thread nonBlockingPopulate = new Thread(nonBlockingPopulateRunnable); nonBlockingPopulate.start(); return settableFuture; } SNMPImpl();   SNMPImpl(Snmp snmp); @Override ListenableFuture<RpcResult<SnmpGetOutput>> snmpGet(SnmpGetInput input); @Override ListenableFuture<RpcResult<SnmpSetOutput>> snmpSet(SnmpSetInput input); @Override ListenableFuture<RpcResult<GetInterfacesOutput>> getInterfaces(final GetInterfacesInput getInterfacesInput); @Override void close(); @Override @SuppressWarnings("checkstyle:IllegalCatch") ListenableFuture<RpcResult<GetNodePropertiesOutput>> getNodeProperties(final GetNodePropertiesInput input);  }
@Test public void testGetInterfaces() throws Exception { final String baseIfOIB = "1.3.6.1.2.1.2.2.1."; final OID ifIndexOID = new OID(baseIfOIB + "1"); final OID ifAdminStatusOID = new OID(baseIfOIB + "7"); final OID ifDescrOID = new OID(baseIfOIB + "2"); final OID ifInErrorsOID = new OID(baseIfOIB + "14"); final List<IfEntry> testInterfaceEntries = new ArrayList<>(); for (int i = 0; i < 10; i++) { IfEntryBuilder ifEntryBuilder = new IfEntryBuilder() .setIfIndex(new InterfaceIndex(i + 1)) .setIfAdminStatus(IfEntry.IfAdminStatus.forValue(i % 3)) .setIfDescr(new DisplayString(String.format("Interface %s", i))) .setIfInErrors(new Counter32(99L - i)); testInterfaceEntries.add(ifEntryBuilder.build()); } doAnswer(invocation -> { PDU requestPDU = (PDU) invocation.getArguments()[0]; final ResponseListener callback = (ResponseListener) invocation.getArguments()[3]; PDU responsePDU = new PDU(); responsePDU.setType(PDU.GET); assertEquals("Checking size of PDU response variable bindings", 1, requestPDU.getVariableBindings().size()); OID requestOID = requestPDU.getVariableBindings().get(0).getOid(); for (int i = 0; i < testInterfaceEntries.size(); i++) { IfEntry testIfEntry = testInterfaceEntries.get(i); int[] prefix = requestOID.getValue(); Variable val = null; if (requestOID.equals(ifIndexOID)) { val = new Integer32(i + 1); } else if (requestOID.equals(ifAdminStatusOID)) { val = new Integer32(i % 3); } else if (requestOID.equals(ifDescrOID)) { val = new OctetString(testIfEntry.getIfDescr().toString()); } else if (requestOID.equals(ifInErrorsOID)) { val = new org.snmp4j.smi.Counter32(testIfEntry.getIfInErrors().getValue()); } else { break; } if (val != null) { OID objOID = new OID(prefix, i); responsePDU.add(new VariableBinding(objOID, val)); } } ResponseEvent responseEvent = new ResponseEvent(mockSnmp, new UdpAddress(Inet4Address.getByName(GET_IP_ADDRESS), SNMP_LISTEN_PORT), requestPDU, responsePDU, null, null); callback.onResponse(responseEvent); return null; }).when(mockSnmp).send(any(PDU.class), any(Target.class), any(), (ResponseListener) any()); Ipv4Address ip = new Ipv4Address(GET_IP_ADDRESS); GetInterfacesInputBuilder input = new GetInterfacesInputBuilder(); input.setCommunity(COMMUNITY); input.setIpAddress(ip); RpcResult<GetInterfacesOutput> result = null; Future<RpcResult<GetInterfacesOutput>> resultFuture = snmpImpl.getInterfaces(input.build()); result = resultFuture.get(); assertTrue(result.isSuccessful()); }