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;
}
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);
}
}
}
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);
}
}
}
}
Aggregations