use of org.opennms.netmgt.model.discovery.IPPollAddress in project opennms by OpenNMS.
the class DiscoveryConfigFactoryTest method testAddToSpecificsFromURLViaStream.
@Test
public void testAddToSpecificsFromURLViaStream() throws Exception {
final List<IPPollAddress> specifics = new ArrayList<IPPollAddress>();
final InputStream in = this.getClass().getResourceAsStream("validDiscoveryIncludeFile.txt");
final long timeout = 100;
final int retries = 1;
DiscoveryConfigFactory.addToSpecificsFromURL(specifics, in, null, null, timeout, retries);
assertEquals(8, specifics.size());
assertEquals("127.0.0.1", InetAddressUtils.str(specifics.get(0).getAddress()));
assertEquals("10.1.1.1", InetAddressUtils.str(specifics.get(1).getAddress()));
assertEquals("10.2.1.1", InetAddressUtils.str(specifics.get(2).getAddress()));
assertEquals("8.8.8.8", InetAddressUtils.str(specifics.get(3).getAddress()));
assertEquals("fe80:0000:0000:0000:ffff:eeee:dddd:cccc", InetAddressUtils.str(specifics.get(4).getAddress()));
assertEquals("0000:0000:0000:0000:0000:0000:0000:0001", InetAddressUtils.str(specifics.get(5).getAddress()));
assertEquals("fe80:0000:0000:0000:ffff:eeee:dddd:cccd", InetAddressUtils.str(specifics.get(6).getAddress()));
assertEquals("fe80:0000:0000:0000:ffff:eeee:dddd:cccc", InetAddressUtils.str(specifics.get(7).getAddress()));
}
use of org.opennms.netmgt.model.discovery.IPPollAddress in project opennms by OpenNMS.
the class DiscoveryConfigFactory method getConfiguredAddresses.
/**
* <p>getConfiguredAddresses</p>
*
* TODO: This function is inefficient. It has O(n^2) complexity based on the
* product of the include ranges and exclude ranges. This might cause problems
* if users are using a large number of excluded ranges.
*
* @return a {@link java.lang.Iterable} object.
*/
@Override
public Iterable<IPPollAddress> getConfiguredAddresses() {
getReadLock().lock();
try {
final List<IPPollAddress> specifics = getSpecifics();
final List<IPPollRange> ranges = getRanges();
specifics.addAll(getURLSpecifics());
final List<Iterator<IPPollAddress>> iters = new ArrayList<Iterator<IPPollAddress>>();
iters.add(specifics.iterator());
for (final IPPollRange range : ranges) {
iters.add(getExcludingInterator(range.iterator()));
}
return IteratorUtils.concatIterators(iters);
} finally {
getReadLock().unlock();
}
}
use of org.opennms.netmgt.model.discovery.IPPollAddress in project opennms by OpenNMS.
the class DiscoveryConfigFactory method addToSpecificsFromURL.
/**
* <p>addToSpecificsFromURL</p>
*
* @param specifics a {@link java.util.List} object.
* @param is a {@link java.io.InputStream} object.
* @param timeout a long.
* @param retries a int.
* @return a boolean.
* @throws java.io.IOException if any.
*/
public static boolean addToSpecificsFromURL(final List<IPPollAddress> specifics, final InputStream is, final String foreignSource, final String location, final long timeout, final int retries) throws IOException {
boolean bRet = true;
try {
final BufferedReader buffer = new BufferedReader(new InputStreamReader(is, StandardCharsets.UTF_8));
String ipLine = null;
String specIP = null;
// get each line of the file and turn it into a specific range
while ((ipLine = buffer.readLine()) != null) {
ipLine = ipLine.trim();
if (ipLine.length() == 0 || ipLine.charAt(0) == DiscoveryConfigFactory.COMMENT_CHAR) {
// blank line or skip comment
continue;
}
// check for comments after IP
final int comIndex = ipLine.indexOf(DiscoveryConfigFactory.COMMENT_STR);
if (comIndex == -1) {
specIP = ipLine;
} else {
specIP = ipLine.substring(0, comIndex);
specIP = specIP.trim();
}
try {
specifics.add(new IPPollAddress(foreignSource, location, InetAddressUtils.addr(specIP), timeout, retries));
} catch (final IllegalArgumentException e) {
LOG.warn("Unknown host \'{}\' inside discovery include file: address ignored", specIP);
}
specIP = null;
}
} catch (final UnsupportedEncodingException e) {
LOG.error("Your JVM doesn't support UTF-8");
return false;
}
return bRet;
}
use of org.opennms.netmgt.model.discovery.IPPollAddress in project opennms by OpenNMS.
the class DiscoveryConfigFactory method getURLSpecifics.
/**
* <p>getURLSpecifics</p>
*
* @return a {@link java.util.List} object.
*/
@Override
public List<IPPollAddress> getURLSpecifics() {
final List<IPPollAddress> specifics = new LinkedList<IPPollAddress>();
getReadLock().lock();
try {
Long defaultTimeout = getConfiguration().getTimeout().orElse(DEFAULT_TIMEOUT);
Integer defaultRetries = getConfiguration().getRetries().orElse(DEFAULT_RETRIES);
for (final IncludeUrl url : getConfiguration().getIncludeUrls()) {
long timeout = url.getTimeout().orElse(defaultTimeout);
int retries = url.getRetries().orElse(defaultRetries);
addToSpecificsFromURL(specifics, url.getUrl().orElse(null), url.getForeignSource().orElse(null), url.getLocation().orElse(null), timeout, retries);
}
return specifics;
} finally {
getReadLock().unlock();
}
}
use of org.opennms.netmgt.model.discovery.IPPollAddress in project opennms by OpenNMS.
the class PingSweepRpcModule method execute.
@Override
public CompletableFuture<PingSweepResponseDTO> execute(PingSweepRequestDTO request) {
final Pinger pinger = pingerFactory.getInstance();
final PingSweepResultTracker tracker = new PingSweepResultTracker();
String location = request.getLocation();
int packetSize = request.getPacketSize();
List<IPPollRange> ranges = new ArrayList<>();
for (IPRangeDTO dto : request.getIpRanges()) {
IPPollRange pollRange = new IPPollRange(null, location, dto.getBegin(), dto.getEnd(), dto.getTimeout(), dto.getRetries());
ranges.add(pollRange);
}
// Use a RateLimiter to limit the ping packets per second that we send
RateLimiter limiter = RateLimiter.create(request.getPacketsPerSecond());
List<IPPollAddress> addresses = StreamSupport.stream(getAddresses(ranges).spliterator(), false).filter(j -> j.getAddress() != null).collect(Collectors.toList());
return CompletableFuture.supplyAsync(() -> {
addresses.stream().forEach(pollAddress -> {
try {
tracker.expectCallbackFor(pollAddress.getAddress());
limiter.acquire();
pinger.ping(pollAddress.getAddress(), pollAddress.getTimeout(), pollAddress.getRetries(), packetSize, 1, tracker);
} catch (Exception e) {
tracker.handleError(pollAddress.getAddress(), null, e);
tracker.completeExceptionally(e);
}
});
try {
tracker.getLatch().await();
} catch (InterruptedException e) {
throw Throwables.propagate(e);
}
tracker.complete();
return tracker.getResponse();
}, executor);
}
Aggregations