Search in sources :

Example 11 with DiscoveryConfiguration

use of org.opennms.netmgt.config.discovery.DiscoveryConfiguration in project opennms by OpenNMS.

the class RangeChunkerTest method testConsecutiveSpecificsWithDifferentLocations.

@Test
public void testConsecutiveSpecificsWithDifferentLocations() {
    DiscoveryConfiguration config = new DiscoveryConfiguration();
    for (int i = 0; i < 5; i++) {
        Specific specific = new Specific();
        specific.setAddress("10.0.0." + i);
        specific.setForeignSource("ABC");
        specific.setLocation(i % 2 == 0 ? "123" : "456");
        specific.setRetries(1);
        specific.setTimeout(1000l);
        config.addSpecific(specific);
    }
    Map<String, List<DiscoveryJob>> jobs = new RangeChunker(ipAddressFilter).chunk(config);
    printJobs(jobs);
    assertEquals(2, jobs.size());
    assertEquals(3, jobs.get("123").get(0).getRanges().size());
    assertEquals(2, jobs.get("456").get(0).getRanges().size());
}
Also used : DiscoveryConfiguration(org.opennms.netmgt.config.discovery.DiscoveryConfiguration) List(java.util.List) Specific(org.opennms.netmgt.config.discovery.Specific) Test(org.junit.Test)

Example 12 with DiscoveryConfiguration

use of org.opennms.netmgt.config.discovery.DiscoveryConfiguration in project opennms by OpenNMS.

the class RangeChunkerTest method testConsecutiveSpecificsWithDifferentTimeouts.

@Test
public void testConsecutiveSpecificsWithDifferentTimeouts() {
    DiscoveryConfiguration config = new DiscoveryConfiguration();
    for (long i = 0; i < 5; i++) {
        Specific specific = new Specific();
        specific.setAddress("10.0.0." + i);
        specific.setForeignSource("ABC");
        specific.setLocation("123");
        specific.setRetries(5);
        specific.setTimeout(i + 1);
        config.addSpecific(specific);
    }
    Map<String, List<DiscoveryJob>> jobs = new RangeChunker(ipAddressFilter).chunk(config);
    printJobs(jobs);
    assertEquals(1, jobs.get("123").size());
    assertEquals(5, jobs.get("123").get(0).getRanges().size());
}
Also used : DiscoveryConfiguration(org.opennms.netmgt.config.discovery.DiscoveryConfiguration) List(java.util.List) Specific(org.opennms.netmgt.config.discovery.Specific) Test(org.junit.Test)

Example 13 with DiscoveryConfiguration

use of org.opennms.netmgt.config.discovery.DiscoveryConfiguration in project opennms by OpenNMS.

the class DiscoveryRestService method getDiscoveryConfig.

private DiscoveryConfiguration getDiscoveryConfig(DiscoveryConfigurationDTO discoveryConfigurationDTO) {
    DiscoveryConfiguration discoveryConfiguration = new DiscoveryConfiguration();
    discoveryConfiguration.setTimeout(discoveryConfigurationDTO.getTimeout());
    discoveryConfiguration.setRetries(discoveryConfigurationDTO.getRetries());
    discoveryConfiguration.setForeignSource(discoveryConfigurationDTO.getForeignSource());
    discoveryConfiguration.setLocation(discoveryConfigurationDTO.getLocation());
    discoveryConfiguration.setChunkSize(discoveryConfigurationDTO.getChunkSize());
    for (DiscoveryConfigurationDTO.SpecificDTO specificDTO : discoveryConfigurationDTO.getSpecificDTOList()) {
        Specific specific = new Specific();
        specific.setAddress(specificDTO.getContent());
        specific.setTimeout(specificDTO.getTimeout());
        specific.setRetries(specificDTO.getRetries());
        specific.setForeignSource(specificDTO.getForeignSource());
        specific.setLocation(specificDTO.getLocation());
        discoveryConfiguration.addSpecific(specific);
    }
    for (DiscoveryConfigurationDTO.IncludeUrlDTO includeUrlDTO : discoveryConfigurationDTO.getIncludeUrlDTOList()) {
        IncludeUrl includeUrl = new IncludeUrl();
        includeUrl.setUrl(includeUrlDTO.getContent());
        includeUrl.setTimeout(includeUrlDTO.getTimeout());
        includeUrl.setRetries(includeUrlDTO.getRetries());
        includeUrl.setForeignSource(includeUrlDTO.getForeignSource());
        includeUrl.setLocation(includeUrlDTO.getLocation());
        discoveryConfiguration.addIncludeUrl(includeUrl);
    }
    for (DiscoveryConfigurationDTO.IncludeRangeDTO includeRangeDTO : discoveryConfigurationDTO.getIncludeRangeDTOList()) {
        IncludeRange includeRange = new IncludeRange();
        includeRange.setBegin(includeRangeDTO.getBegin());
        includeRange.setEnd(includeRangeDTO.getEnd());
        includeRange.setTimeout(includeRangeDTO.getTimeout());
        includeRange.setRetries(includeRangeDTO.getRetries());
        includeRange.setForeignSource(includeRangeDTO.getForeignSource());
        includeRange.setLocation(includeRangeDTO.getLocation());
        discoveryConfiguration.addIncludeRange(includeRange);
    }
    for (DiscoveryConfigurationDTO.ExcludeRangeDTO excludeRangeDTO : discoveryConfigurationDTO.getExcludeRangeDTOList()) {
        ExcludeRange excludeRange = new ExcludeRange();
        excludeRange.setBegin(excludeRangeDTO.getBegin());
        excludeRange.setEnd(excludeRangeDTO.getEnd());
        discoveryConfiguration.addExcludeRange(excludeRange);
    }
    return discoveryConfiguration;
}
Also used : IncludeRange(org.opennms.netmgt.config.discovery.IncludeRange) IncludeUrl(org.opennms.netmgt.config.discovery.IncludeUrl) DiscoveryConfiguration(org.opennms.netmgt.config.discovery.DiscoveryConfiguration) Specific(org.opennms.netmgt.config.discovery.Specific) ExcludeRange(org.opennms.netmgt.config.discovery.ExcludeRange)

Example 14 with DiscoveryConfiguration

use of org.opennms.netmgt.config.discovery.DiscoveryConfiguration in project opennms by OpenNMS.

the class DiscoveryRestService method scan.

@POST
@Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
public Response scan(DiscoveryConfigurationDTO discoveryConfigurationDTO) {
    DiscoveryConfiguration discoveryConfiguration = getDiscoveryConfig(discoveryConfigurationDTO);
    DiscoveryTaskExecutor discoveryTaskExecutor = serviceRegistry.findProvider(DiscoveryTaskExecutor.class);
    if (discoveryTaskExecutor != null) {
        discoveryTaskExecutor.handleDiscoveryTask(discoveryConfiguration);
    } else {
        LOG.warn("No DiscoveryTaskExecutor service is available");
        return Response.serverError().build();
    }
    return Response.ok().build();
}
Also used : DiscoveryConfiguration(org.opennms.netmgt.config.discovery.DiscoveryConfiguration) DiscoveryTaskExecutor(org.opennms.netmgt.discovery.DiscoveryTaskExecutor) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes)

Example 15 with DiscoveryConfiguration

use of org.opennms.netmgt.config.discovery.DiscoveryConfiguration in project opennms by OpenNMS.

the class RangeChunker method chunk.

public Map<String, List<DiscoveryJob>> chunk(final DiscoveryConfiguration config) {
    final int chunkSize = config.getChunkSize().orElse(DiscoveryConfigFactory.DEFAULT_CHUNK_SIZE);
    final double packetsPerSecond = config.getPacketsPerSecond().orElse(DiscoveryConfigFactory.DEFAULT_PACKETS_PER_SECOND);
    // If the foreign source for the discovery config is not set than use
    // a value of null so that non-requisitioned nodes are created.
    // 
    // TODO: Use the "default" foreign source instead so that we can move
    // away from using non-requisitioned nodes.
    // 
    final String foreignSourceFromConfig = config.getForeignSource().isPresent() ? config.getForeignSource().get().trim() : null;
    // If the monitoring location for the discovery config is not set than use
    // the default localhost location
    final String locationFromConfig = config.getLocation().map(l -> {
        final String trimmed = l.trim();
        if ("".equals(trimmed)) {
            return null;
        }
        return trimmed;
    }).orElse(MonitoringLocationDao.DEFAULT_MONITORING_LOCATION_ID);
    final DiscoveryConfigFactory configFactory = new DiscoveryConfigFactory(config);
    final AtomicReference<IPPollRange> previousRange = new AtomicReference<>();
    return StreamSupport.stream(configFactory.getConfiguredAddresses().spliterator(), false).filter(address -> {
        // If there is no IP address filter set or the filter matches
        return ipAddressFilter.matches(address.getLocation(), address.getAddress());
    }).map(address -> {
        // Create a singleton IPPollRange
        return new IPPollRange(// Make sure that foreignSource is not null so that we can partition on the value
        address.getForeignSource() == null ? foreignSourceFromConfig : address.getForeignSource(), // Make sure that location is not null so that we can partition on the value
        address.getLocation() == null ? locationFromConfig : address.getLocation(), address.getAddress(), address.getAddress(), address.getTimeout(), address.getRetries());
    }).collect(Collectors.groupingBy(range -> {
        // Create a Map<ForeignSourceLocationKey,List<IPPollRange>>
        return new ForeignSourceLocationKey(// Make sure that foreignSource is not null so that we can partition on the value
        range.getForeignSource() == null ? foreignSourceFromConfig : range.getForeignSource(), // Make sure that location is not null so that we can partition on the value
        range.getLocation() == null ? locationFromConfig : range.getLocation());
    }, LinkedHashMap::new, Collectors.toList())).entrySet().stream().flatMap(entry -> {
        // Partition the list of address values
        return Lists.partition(entry.getValue(), chunkSize).stream().map(ranges -> {
            DiscoveryJob retval = new DiscoveryJob(ranges.stream().map(address -> {
                // then just extend the range to cover this address too
                if (isConsecutive(previousRange.get(), address)) {
                    previousRange.get().getAddressRange().incrementEnd();
                    return null;
                }
                previousRange.set(address);
                return address;
            }).filter(Objects::nonNull).collect(Collectors.toList()), entry.getKey().getForeignSource(), entry.getKey().getLocation(), packetsPerSecond);
            // Reset the previousRange value
            previousRange.set(null);
            return retval;
        }).collect(Collectors.toList()).stream();
    }).collect(Collectors.groupingBy(DiscoveryJob::getLocation, LinkedHashMap::new, Collectors.toList()));
}
Also used : DiscoveryConfiguration(org.opennms.netmgt.config.discovery.DiscoveryConfiguration) AtomicReference(java.util.concurrent.atomic.AtomicReference) Collectors(java.util.stream.Collectors) IPAddress(org.opennms.core.network.IPAddress) LinkedHashMap(java.util.LinkedHashMap) Objects(java.util.Objects) List(java.util.List) Lists(com.google.common.collect.Lists) Map(java.util.Map) DiscoveryConfigFactory(org.opennms.netmgt.config.DiscoveryConfigFactory) MonitoringLocationDao(org.opennms.netmgt.dao.api.MonitoringLocationDao) IPPollRange(org.opennms.netmgt.model.discovery.IPPollRange) BigInteger(java.math.BigInteger) StreamSupport(java.util.stream.StreamSupport) Preconditions(com.google.common.base.Preconditions) IPPollRange(org.opennms.netmgt.model.discovery.IPPollRange) AtomicReference(java.util.concurrent.atomic.AtomicReference) DiscoveryConfigFactory(org.opennms.netmgt.config.DiscoveryConfigFactory)

Aggregations

DiscoveryConfiguration (org.opennms.netmgt.config.discovery.DiscoveryConfiguration)18 Test (org.junit.Test)12 Specific (org.opennms.netmgt.config.discovery.Specific)11 IncludeRange (org.opennms.netmgt.config.discovery.IncludeRange)9 List (java.util.List)6 ExcludeRange (org.opennms.netmgt.config.discovery.ExcludeRange)6 IncludeUrl (org.opennms.netmgt.config.discovery.IncludeUrl)5 DiscoveryConfigFactory (org.opennms.netmgt.config.DiscoveryConfigFactory)4 ServletException (javax.servlet.ServletException)3 File (java.io.File)2 RequestDispatcher (javax.servlet.RequestDispatcher)2 HttpSession (javax.servlet.http.HttpSession)2 IPAddress (org.opennms.core.network.IPAddress)2 EventAnticipator (org.opennms.netmgt.dao.mock.EventAnticipator)2 DiscoveryTaskExecutor (org.opennms.netmgt.discovery.DiscoveryTaskExecutor)2 IPPollRange (org.opennms.netmgt.model.discovery.IPPollRange)2 Event (org.opennms.netmgt.xml.event.Event)2 Preconditions (com.google.common.base.Preconditions)1 Lists (com.google.common.collect.Lists)1 FileReader (java.io.FileReader)1