Search in sources :

Example 1 with ValidationException

use of org.opennms.netmgt.measurements.api.exceptions.ValidationException 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 2 with ValidationException

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

the class AbstractMeasurementQueryExecutorTest method verifyHttpCalls.

protected void verifyHttpCalls(int number) {
    // ensure a request was actually made and was only made <number> times
    WireMock.verify(number, WireMock.postRequestedFor(WireMock.urlMatching("/opennms/rest/measurements")).withoutHeader("Authorization").withHeader("Content-Type", WireMock.equalTo("application/xml")));
    // VERIFY that the Request Body is a valid QueryRequest
    Assert.assertEquals(number, requestList.size());
    for (Request eachRequest : requestList) {
        QueryRequest queryRequest = JAXB.unmarshal(new ByteArrayInputStream(eachRequest.getBody()), QueryRequest.class);
        try {
            new QueryRequestValidator().validate(queryRequest);
        } catch (ValidationException e) {
            Throwables.propagate(e);
        }
    }
}
Also used : ValidationException(org.opennms.netmgt.measurements.api.exceptions.ValidationException) QueryRequest(org.opennms.netmgt.measurements.model.QueryRequest) ByteArrayInputStream(java.io.ByteArrayInputStream) QueryRequestValidator(org.opennms.netmgt.measurements.api.QueryRequestValidator) QueryRequest(org.opennms.netmgt.measurements.model.QueryRequest) Request(com.github.tomakehurst.wiremock.http.Request) LoggedRequest(com.github.tomakehurst.wiremock.verification.LoggedRequest)

Example 3 with ValidationException

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

the class QueryRequestValidator method validate.

public void validate(QueryRequest request) throws ValidationException {
    if (request.getEnd() < 0) {
        throw new ValidationException("Query end must be >= 0: {}", request.getEnd());
    }
    if (request.getStep() <= 0) {
        throw new ValidationException("Query step must be > 0: {}", request.getStep());
    }
    if ((request.getHeartbeat() == null && request.getInterval() != null) || (request.getHeartbeat() != null && request.getInterval() == null)) {
        throw new ValidationException("If either the heartbeat or the interval are set, then both must be set.");
    }
    if (request.getHeartbeat() != null && request.getInterval() != null) {
        if (request.getHeartbeat() <= 0) {
            throw new ValidationException("Heartbeat must be positive: {}", request.getHeartbeat());
        }
        if (request.getInterval() <= 0) {
            throw new ValidationException("Interval must be positive: {}", request.getInterval());
        }
        if (request.getStep() % request.getInterval() != 0) {
            throw new ValidationException("Step must be a multiple of the interval. Step: {}, Interval: {}", request.getStep(), request.getInterval());
        }
        if (request.getHeartbeat() % request.getInterval() != 0) {
            throw new ValidationException("Heartbeat must be a multiple of the interval. Interval: {} Heartbeat: {}", request.getInterval(), request.getHeartbeat());
        }
    }
    final Map<String, String> labels = new HashMap<>();
    for (final Source source : request.getSources()) {
        if (source.getResourceId() == null || source.getAttribute() == null || source.getLabel() == null || source.getAggregation() == null) {
            throw new ValidationException("Query source fields must be set: {}", source);
        }
        if (labels.containsKey(source.getLabel())) {
            throw new ValidationException("Query source label '{}' conflict: source with that label is already defined.", source.getLabel());
        } else {
            labels.put(source.getLabel(), "source");
        }
    }
    for (final Expression expression : request.getExpressions()) {
        if (expression.getExpression() == null || expression.getLabel() == null) {
            throw new ValidationException("Query expression fields must be set: {}", expression);
        }
        if (labels.containsKey(expression.getLabel())) {
            final String type = labels.get(expression.getLabel());
            throw new ValidationException("Query expression label '{}' conflict: {} with that label is already defined.", expression.getLabel(), type);
        } else {
            labels.put(expression.getLabel(), "expression");
        }
    }
    List<FilterDef> filters = request.getFilters();
    if (filters.size() > 0) {
        for (FilterDef filter : filters) {
            if (filter.getName() == null) {
                throw new ValidationException("Filter name must be set: {}", filter);
            }
        }
    }
}
Also used : ValidationException(org.opennms.netmgt.measurements.api.exceptions.ValidationException) FilterDef(org.opennms.netmgt.measurements.model.FilterDef) HashMap(java.util.HashMap) Expression(org.opennms.netmgt.measurements.model.Expression) Source(org.opennms.netmgt.measurements.model.Source)

Aggregations

ValidationException (org.opennms.netmgt.measurements.api.exceptions.ValidationException)3 Request (com.github.tomakehurst.wiremock.http.Request)1 LoggedRequest (com.github.tomakehurst.wiremock.verification.LoggedRequest)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 HashMap (java.util.HashMap)1 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 QueryRequestValidator (org.opennms.netmgt.measurements.api.QueryRequestValidator)1 ExpressionException (org.opennms.netmgt.measurements.api.exceptions.ExpressionException)1 FetchException (org.opennms.netmgt.measurements.api.exceptions.FetchException)1 FilterException (org.opennms.netmgt.measurements.api.exceptions.FilterException)1 ResourceNotFoundException (org.opennms.netmgt.measurements.api.exceptions.ResourceNotFoundException)1 Expression (org.opennms.netmgt.measurements.model.Expression)1 FilterDef (org.opennms.netmgt.measurements.model.FilterDef)1 QueryRequest (org.opennms.netmgt.measurements.model.QueryRequest)1 QueryResponse (org.opennms.netmgt.measurements.model.QueryResponse)1 Source (org.opennms.netmgt.measurements.model.Source)1