Search in sources :

Example 1 with ResourceNotFoundException

use of org.opennms.netmgt.measurements.api.exceptions.ResourceNotFoundException in project opennms by OpenNMS.

the class DefaultMeasurementsService method query.

@Override
public QueryResponse query(QueryRequest request) throws MeasurementException {
    validate(request);
    // Fetch the measurements
    FetchResults results;
    try {
        results = fetchStrategy.fetch(request.getStart(), request.getEnd(), request.getStep(), request.getMaxRows(), request.getHeartbeat(), request.getInterval(), request.getSources(), request.isRelaxed());
    } catch (Exception e) {
        throw new FetchException(e, "Fetch failed: {}", e.getMessage());
    }
    if (results == null) {
        throw new ResourceNotFoundException(request);
    }
    // Apply the expression to the fetch results
    expressionEngine.applyExpressions(request, results);
    // Apply the filters
    if (!request.getFilters().isEmpty()) {
        RowSortedTable<Long, String, Double> table = results.asRowSortedTable();
        filterEngine.filter(request.getFilters(), table);
        results = new FetchResults(table, results.getStep(), results.getConstants());
    }
    // Remove any transient values belonging to sources
    final Map<String, double[]> columns = results.getColumns();
    for (final Source source : request.getSources()) {
        if (source.getTransient()) {
            columns.remove(source.getLabel());
        }
    }
    // Build the response
    final QueryResponse response = new QueryResponse();
    response.setStart(request.getStart());
    response.setEnd(request.getEnd());
    response.setStep(results.getStep());
    response.setTimestamps(results.getTimestamps());
    response.setColumns(results.getColumns());
    response.setConstants(results.getConstants());
    return response;
}
Also used : QueryResponse(org.opennms.netmgt.measurements.model.QueryResponse) FetchException(org.opennms.netmgt.measurements.api.exceptions.FetchException) ResourceNotFoundException(org.opennms.netmgt.measurements.api.exceptions.ResourceNotFoundException) FetchException(org.opennms.netmgt.measurements.api.exceptions.FetchException) MeasurementException(org.opennms.netmgt.measurements.api.exceptions.MeasurementException) ValidationException(org.opennms.netmgt.measurements.api.exceptions.ValidationException) ResourceNotFoundException(org.opennms.netmgt.measurements.api.exceptions.ResourceNotFoundException) Source(org.opennms.netmgt.measurements.model.Source)

Example 2 with ResourceNotFoundException

use of org.opennms.netmgt.measurements.api.exceptions.ResourceNotFoundException in project opennms by OpenNMS.

the class MeasurementsRestService method query.

/**
     * Retrieves the measurements of many resources and performs
     * arbitrary calculations on these.
     *
     * This a read-only query, however we use a POST instead of GET
     * since the request parameters are difficult to express in a query string.
     */
@POST
@Path("/")
@Consumes({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON, MediaType.APPLICATION_ATOM_XML })
@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON, MediaType.APPLICATION_ATOM_XML })
@Transactional(readOnly = true)
public QueryResponse query(final QueryRequest request) {
    Preconditions.checkState(service != null);
    LOG.debug("Executing query with {}", request);
    QueryResponse response = null;
    try {
        response = service.query(request);
    } catch (ExpressionException e) {
        throw getException(Status.BAD_REQUEST, e, "An error occurred while evaluating an expression: {}", e.getMessage());
    } catch (FilterException | ValidationException e) {
        throw getException(Status.BAD_REQUEST, e, e.getMessage());
    } catch (ResourceNotFoundException e) {
        throw getException(Status.NOT_FOUND, e, e.getMessage());
    } catch (FetchException e) {
        throw getException(Status.INTERNAL_SERVER_ERROR, e, e.getMessage());
    } catch (Exception e) {
        throw getException(Status.INTERNAL_SERVER_ERROR, e, "Query failed: {}", e.getMessage());
    }
    // Return a 204 if there are no columns
    if (response.getColumns().length == 0) {
        throw getException(Status.NO_CONTENT, "No content.");
    }
    return response;
}
Also used : ValidationException(org.opennms.netmgt.measurements.api.exceptions.ValidationException) QueryResponse(org.opennms.netmgt.measurements.model.QueryResponse) FilterException(org.opennms.netmgt.measurements.api.exceptions.FilterException) FetchException(org.opennms.netmgt.measurements.api.exceptions.FetchException) ResourceNotFoundException(org.opennms.netmgt.measurements.api.exceptions.ResourceNotFoundException) ExpressionException(org.opennms.netmgt.measurements.api.exceptions.ExpressionException) FetchException(org.opennms.netmgt.measurements.api.exceptions.FetchException) FilterException(org.opennms.netmgt.measurements.api.exceptions.FilterException) ResourceNotFoundException(org.opennms.netmgt.measurements.api.exceptions.ResourceNotFoundException) ValidationException(org.opennms.netmgt.measurements.api.exceptions.ValidationException) WebApplicationException(javax.ws.rs.WebApplicationException) ExpressionException(org.opennms.netmgt.measurements.api.exceptions.ExpressionException) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces) Transactional(org.springframework.transaction.annotation.Transactional)

Example 3 with ResourceNotFoundException

use of org.opennms.netmgt.measurements.api.exceptions.ResourceNotFoundException in project opennms by OpenNMS.

the class LocalMeasurementDataSourceWrapper method createDataSource.

@Override
public JRRewindableDataSource createDataSource(String query) throws JRException {
    Objects.requireNonNull(query);
    QueryRequest queryRequest = unmarshal(query);
    Objects.requireNonNull(queryRequest);
    // Enforce relaxed mode
    queryRequest.setRelaxed(true);
    try {
        QueryResponse response = fetchService.query(queryRequest);
        return new MeasurementDataSource(response);
    } catch (ResourceNotFoundException rnfe) {
        LOG.warn("A attribute or resource was not found", rnfe);
        return new EmptyJRDataSource();
    } catch (Exception e) {
        LOG.error("An error occurred while fetching the measurement results", e);
        throw new JRException(e);
    }
}
Also used : QueryRequest(org.opennms.netmgt.measurements.model.QueryRequest) JRException(net.sf.jasperreports.engine.JRException) QueryResponse(org.opennms.netmgt.measurements.model.QueryResponse) MeasurementDataSource(org.opennms.netmgt.jasper.measurement.MeasurementDataSource) ResourceNotFoundException(org.opennms.netmgt.measurements.api.exceptions.ResourceNotFoundException) ResourceNotFoundException(org.opennms.netmgt.measurements.api.exceptions.ResourceNotFoundException) JRException(net.sf.jasperreports.engine.JRException) EmptyJRDataSource(org.opennms.netmgt.jasper.measurement.EmptyJRDataSource)

Aggregations

ResourceNotFoundException (org.opennms.netmgt.measurements.api.exceptions.ResourceNotFoundException)3 QueryResponse (org.opennms.netmgt.measurements.model.QueryResponse)3 FetchException (org.opennms.netmgt.measurements.api.exceptions.FetchException)2 ValidationException (org.opennms.netmgt.measurements.api.exceptions.ValidationException)2 Consumes (javax.ws.rs.Consumes)1 POST (javax.ws.rs.POST)1 Path (javax.ws.rs.Path)1 Produces (javax.ws.rs.Produces)1 WebApplicationException (javax.ws.rs.WebApplicationException)1 JRException (net.sf.jasperreports.engine.JRException)1 EmptyJRDataSource (org.opennms.netmgt.jasper.measurement.EmptyJRDataSource)1 MeasurementDataSource (org.opennms.netmgt.jasper.measurement.MeasurementDataSource)1 ExpressionException (org.opennms.netmgt.measurements.api.exceptions.ExpressionException)1 FilterException (org.opennms.netmgt.measurements.api.exceptions.FilterException)1 MeasurementException (org.opennms.netmgt.measurements.api.exceptions.MeasurementException)1 QueryRequest (org.opennms.netmgt.measurements.model.QueryRequest)1 Source (org.opennms.netmgt.measurements.model.Source)1 Transactional (org.springframework.transaction.annotation.Transactional)1