Search in sources :

Example 1 with ResourceDao

use of org.opennms.netmgt.dao.api.ResourceDao in project opennms by OpenNMS.

the class NewtsFetchStrategy method fetch.

@Override
public FetchResults fetch(long start, long end, long step, int maxrows, Long interval, Long heartbeat, List<Source> sources, boolean relaxed) {
    final LateAggregationParams lag = getLagParams(step, interval, heartbeat);
    final Optional<Timestamp> startTs = Optional.of(Timestamp.fromEpochMillis(start));
    final Optional<Timestamp> endTs = Optional.of(Timestamp.fromEpochMillis(end));
    final Map<String, Object> constants = Maps.newHashMap();
    // Group the sources by resource id to avoid calling the ResourceDao
    // multiple times for the same resource
    Map<ResourceId, List<Source>> sourcesByResourceId = sources.stream().collect(Collectors.groupingBy((source) -> ResourceId.fromString(source.getResourceId())));
    // Lookup the OnmsResources in parallel
    Map<ResourceId, Future<OnmsResource>> resourceFuturesById = Maps.newHashMapWithExpectedSize(sourcesByResourceId.size());
    for (ResourceId resourceId : sourcesByResourceId.keySet()) {
        resourceFuturesById.put(resourceId, threadPool.submit(getResourceByIdCallable(resourceId)));
    }
    // Gather the results, fail if any of the resources were not found
    Map<OnmsResource, List<Source>> sourcesByResource = Maps.newHashMapWithExpectedSize(sourcesByResourceId.size());
    for (Entry<ResourceId, Future<OnmsResource>> entry : resourceFuturesById.entrySet()) {
        try {
            OnmsResource resource = entry.getValue().get();
            if (resource == null) {
                if (relaxed)
                    continue;
                LOG.error("No resource with id: {}", entry.getKey());
                return null;
            }
            sourcesByResource.put(resource, sourcesByResourceId.get(entry.getKey()));
        } catch (ExecutionException | InterruptedException e) {
            throw Throwables.propagate(e);
        }
    }
    // Now group the sources by Newts Resource ID, which differs from the OpenNMS Resource ID.
    Map<String, List<Source>> sourcesByNewtsResourceId = Maps.newHashMap();
    for (Entry<OnmsResource, List<Source>> entry : sourcesByResource.entrySet()) {
        final OnmsResource resource = entry.getKey();
        for (Source source : entry.getValue()) {
            // Gather the values from strings.properties
            Utils.convertStringAttributesToConstants(source.getLabel(), resource.getStringPropertyAttributes(), constants);
            // Grab the attribute that matches the source
            RrdGraphAttribute rrdGraphAttribute = resource.getRrdGraphAttributes().get(source.getAttribute());
            if (rrdGraphAttribute == null && !Strings.isNullOrEmpty(source.getFallbackAttribute())) {
                LOG.error("No attribute with name '{}', using fallback-attribute with name '{}'", source.getAttribute(), source.getFallbackAttribute());
                source.setAttribute(source.getFallbackAttribute());
                source.setFallbackAttribute(null);
                rrdGraphAttribute = resource.getRrdGraphAttributes().get(source.getAttribute());
            }
            if (rrdGraphAttribute == null) {
                if (relaxed)
                    continue;
                LOG.error("No attribute with name: {}", source.getAttribute());
                return null;
            }
            // The Newts Resource ID is stored in the rrdFile attribute
            String newtsResourceId = rrdGraphAttribute.getRrdRelativePath();
            // Remove the file separator prefix, added by the RrdGraphAttribute class
            if (newtsResourceId.startsWith(File.separator)) {
                newtsResourceId = newtsResourceId.substring(File.separator.length(), newtsResourceId.length());
            }
            List<Source> listOfSources = sourcesByNewtsResourceId.get(newtsResourceId);
            // Create the list if it doesn't exist
            if (listOfSources == null) {
                listOfSources = Lists.newLinkedList();
                sourcesByNewtsResourceId.put(newtsResourceId, listOfSources);
            }
            listOfSources.add(source);
        }
    }
    // The Newts API only allows us to perform a query using a single (Newts) Resource ID,
    // so we perform multiple queries in parallel, and aggregate the results.
    Map<String, Future<Collection<Row<Measurement>>>> measurementsByNewtsResourceId = Maps.newHashMapWithExpectedSize(sourcesByNewtsResourceId.size());
    for (Entry<String, List<Source>> entry : sourcesByNewtsResourceId.entrySet()) {
        measurementsByNewtsResourceId.put(entry.getKey(), threadPool.submit(getMeasurementsForResourceCallable(entry.getKey(), entry.getValue(), startTs, endTs, lag)));
    }
    long[] timestamps = null;
    Map<String, double[]> columns = Maps.newHashMap();
    for (Entry<String, Future<Collection<Row<Measurement>>>> entry : measurementsByNewtsResourceId.entrySet()) {
        Collection<Row<Measurement>> rows;
        try {
            rows = entry.getValue().get();
        } catch (InterruptedException | ExecutionException e) {
            throw Throwables.propagate(e);
        }
        final int N = rows.size();
        if (timestamps == null) {
            timestamps = new long[N];
            int k = 0;
            for (final Row<Measurement> row : rows) {
                timestamps[k] = row.getTimestamp().asMillis();
                k++;
            }
        }
        int k = 0;
        for (Row<Measurement> row : rows) {
            for (Measurement measurement : row.getElements()) {
                double[] column = columns.get(measurement.getName());
                if (column == null) {
                    column = new double[N];
                    columns.put(measurement.getName(), column);
                }
                column[k] = measurement.getValue();
            }
            k += 1;
        }
    }
    FetchResults fetchResults = new FetchResults(timestamps, columns, lag.getStep(), constants);
    if (relaxed) {
        Utils.fillMissingValues(fetchResults, sources);
    }
    LOG.trace("Fetch results: {}", fetchResults);
    return fetchResults;
}
Also used : ThreadFactoryBuilder(com.google.common.util.concurrent.ThreadFactoryBuilder) Context(org.opennms.newts.api.Context) StandardAggregationFunctions(org.opennms.newts.api.query.StandardAggregationFunctions) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) Callable(java.util.concurrent.Callable) AggregationFunction(org.opennms.newts.api.query.AggregationFunction) Utils(org.opennms.netmgt.measurements.utils.Utils) SampleRepository(org.opennms.newts.api.SampleRepository) Strings(com.google.common.base.Strings) Future(java.util.concurrent.Future) Lists(com.google.common.collect.Lists) Optional(com.google.common.base.Optional) Map(java.util.Map) OnmsResource(org.opennms.netmgt.model.OnmsResource) ThreadFactory(java.util.concurrent.ThreadFactory) ResultDescriptor(org.opennms.newts.api.query.ResultDescriptor) RrdGraphAttribute(org.opennms.netmgt.model.RrdGraphAttribute) ExecutorService(java.util.concurrent.ExecutorService) SampleSelectCallback(org.opennms.newts.api.SampleSelectCallback) ResourceId(org.opennms.netmgt.model.ResourceId) Logger(org.slf4j.Logger) Semaphore(java.util.concurrent.Semaphore) Collection(java.util.Collection) Resource(org.opennms.newts.api.Resource) Throwables(com.google.common.base.Throwables) Collectors(java.util.stream.Collectors) Maps(com.google.common.collect.Maps) File(java.io.File) Executors(java.util.concurrent.Executors) Row(org.opennms.newts.api.Results.Row) ExecutionException(java.util.concurrent.ExecutionException) FetchResults(org.opennms.netmgt.measurements.api.FetchResults) List(java.util.List) Duration(org.opennms.newts.api.Duration) Results(org.opennms.newts.api.Results) MeasurementFetchStrategy(org.opennms.netmgt.measurements.api.MeasurementFetchStrategy) Measurement(org.opennms.newts.api.Measurement) Entry(java.util.Map.Entry) Timestamp(org.opennms.newts.api.Timestamp) VisibleForTesting(com.google.common.annotations.VisibleForTesting) ResourceDao(org.opennms.netmgt.dao.api.ResourceDao) Source(org.opennms.netmgt.measurements.model.Source) Measurement(org.opennms.newts.api.Measurement) Timestamp(org.opennms.newts.api.Timestamp) Source(org.opennms.netmgt.measurements.model.Source) RrdGraphAttribute(org.opennms.netmgt.model.RrdGraphAttribute) FetchResults(org.opennms.netmgt.measurements.api.FetchResults) List(java.util.List) ExecutionException(java.util.concurrent.ExecutionException) OnmsResource(org.opennms.netmgt.model.OnmsResource) ResourceId(org.opennms.netmgt.model.ResourceId) Future(java.util.concurrent.Future) Row(org.opennms.newts.api.Results.Row)

Example 2 with ResourceDao

use of org.opennms.netmgt.dao.api.ResourceDao in project opennms by OpenNMS.

the class CustomSpringConfiguration method createResourceDao.

@Bean(name = "resourceDao")
public ResourceDao createResourceDao() {
    return new ResourceDao() {

        @Override
        public Collection<OnmsResourceType> getResourceTypes() {
            throw new UnsupportedOperationException();
        }

        @Override
        public List<OnmsResource> findTopLevelResources() {
            throw new UnsupportedOperationException();
        }

        @Override
        public OnmsResource getResourceById(ResourceId id) {
            if (id.toString().startsWith("node[1]")) {
                final OnmsResource onmsResource = new OnmsResource(id.toString(), id.toString(), new InterfaceSnmpResourceType(null), new HashSet<OnmsAttribute>(), new ResourcePath());
                if (id.toString().contains("interfaceSnmp[127.0.0.1]")) {
                    final RrdGraphAttribute attribute = new RrdGraphAttribute();
                    attribute.setName("ifInErrors");
                    attribute.setResource(onmsResource);
                    onmsResource.getAttributes().add(attribute);
                }
                return onmsResource;
            }
            return null;
        }

        @Override
        public OnmsResource getResourceForNode(OnmsNode node) {
            throw new UnsupportedOperationException();
        }

        @Override
        public OnmsResource getResourceForIpInterface(OnmsIpInterface ipInterface, OnmsLocationMonitor locationMonitor) {
            throw new UnsupportedOperationException();
        }

        @Override
        public boolean deleteResourceById(ResourceId resourceId) {
            throw new UnsupportedOperationException();
        }
    };
}
Also used : OnmsNode(org.opennms.netmgt.model.OnmsNode) InterfaceSnmpResourceType(org.opennms.netmgt.dao.support.InterfaceSnmpResourceType) OnmsAttribute(org.opennms.netmgt.model.OnmsAttribute) RrdGraphAttribute(org.opennms.netmgt.model.RrdGraphAttribute) OnmsResource(org.opennms.netmgt.model.OnmsResource) ResourcePath(org.opennms.netmgt.model.ResourcePath) OnmsIpInterface(org.opennms.netmgt.model.OnmsIpInterface) OnmsResourceType(org.opennms.netmgt.model.OnmsResourceType) ResourceId(org.opennms.netmgt.model.ResourceId) ResourceDao(org.opennms.netmgt.dao.api.ResourceDao) OnmsLocationMonitor(org.opennms.netmgt.model.OnmsLocationMonitor) Bean(org.springframework.context.annotation.Bean)

Example 3 with ResourceDao

use of org.opennms.netmgt.dao.api.ResourceDao in project opennms by OpenNMS.

the class RrdSummaryControllerTest method canGenerateEmptySummary.

/**
 * Verifies that an empty summary can be generated so an existing
 * node without any resources.
 */
@Test
public void canGenerateEmptySummary() {
    // Return a single node when called using the given filter
    String rule = "ipaddr iplike 172.20.1.1";
    FilterDao filterDao = mock(FilterDao.class);
    when(filterDao.getNodeMap(rule)).thenReturn(ImmutableSortedMap.of(1, "node1"));
    OnmsNode node = mock(OnmsNode.class);
    NodeDao nodeDao = mock(NodeDao.class);
    when(nodeDao.load(1)).thenReturn(node);
    OnmsResource resource = mock(OnmsResource.class);
    ResourceDao resourceDao = mock(ResourceDao.class);
    when(resourceDao.getResourceForNode(node)).thenReturn(resource);
    // Use our mocks
    m_rrdSummaryService.setFilterDao(filterDao);
    m_rrdSummaryService.setNodeDao(nodeDao);
    m_rrdSummaryService.setResourceDao(resourceDao);
    // Building the summary spec.
    SummarySpecification summarySpec = new SummarySpecification();
    summarySpec.setFilterRule("ipaddr iplike 172.20.1.1");
    summarySpec.setStartTime(1472746964);
    summarySpec.setEndTime(1473265364);
    summarySpec.setAttributeSieve(".*");
    // Invoke the controller
    HttpServletResponse response = new MockHttpServletResponse();
    ModelAndView mv = m_controller.processFormSubmission(response, summarySpec);
    Summary summary = (Summary) mv.getModel().get("summary");
    // Verify the response
    assertEquals(0, summary.getResources().size());
}
Also used : NodeDao(org.opennms.netmgt.dao.api.NodeDao) FilterDao(org.opennms.netmgt.filter.api.FilterDao) OnmsNode(org.opennms.netmgt.model.OnmsNode) OnmsResource(org.opennms.netmgt.model.OnmsResource) ModelAndView(org.springframework.web.servlet.ModelAndView) HttpServletResponse(javax.servlet.http.HttpServletResponse) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Summary(org.opennms.netmgt.config.attrsummary.Summary) ResourceDao(org.opennms.netmgt.dao.api.ResourceDao) SummarySpecification(org.opennms.web.svclayer.model.SummarySpecification) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.Test)

Aggregations

ResourceDao (org.opennms.netmgt.dao.api.ResourceDao)3 OnmsResource (org.opennms.netmgt.model.OnmsResource)3 OnmsNode (org.opennms.netmgt.model.OnmsNode)2 ResourceId (org.opennms.netmgt.model.ResourceId)2 RrdGraphAttribute (org.opennms.netmgt.model.RrdGraphAttribute)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 Optional (com.google.common.base.Optional)1 Strings (com.google.common.base.Strings)1 Throwables (com.google.common.base.Throwables)1 Lists (com.google.common.collect.Lists)1 Maps (com.google.common.collect.Maps)1 ThreadFactoryBuilder (com.google.common.util.concurrent.ThreadFactoryBuilder)1 File (java.io.File)1 Collection (java.util.Collection)1 List (java.util.List)1 Map (java.util.Map)1 Entry (java.util.Map.Entry)1 Callable (java.util.concurrent.Callable)1 ExecutionException (java.util.concurrent.ExecutionException)1 ExecutorService (java.util.concurrent.ExecutorService)1