use of org.opennms.netmgt.measurements.model.QueryRequest in project opennms by OpenNMS.
the class RemoteMeasurementDataSourceWrapper method createDataSource.
@Override
public JRRewindableDataSource createDataSource(String query) throws JRException {
try {
QueryRequest queryRequest = unmarshal(query);
// enforce relaxed mode
queryRequest.setRelaxed(true);
query = marshal(queryRequest);
Result result = connector.execute(useSsl, url, username, password, query);
// therefore do not allow redirection at all.
if (result.wasRedirection()) {
throw new IOException("Request was redirected. This is not supported.");
}
// if there is no data
if (404 == result.getResponseCode()) {
LOG.warn("Got a 404 (Not Found) response. This might be due to a wrong url or the resource does not exist. Requested URL was: '{}'", url);
return new EmptyJRDataSource();
}
// OK
if (result.wasSuccessful() && result.getInputStream() != null) {
return new MeasurementDataSource(result.getInputStream());
}
// Error
ByteArrayOutputStream errorMessageStream = new ByteArrayOutputStream();
if (result.getErrorStream() != null) {
ByteStreams.copy(result.getErrorStream(), errorMessageStream);
}
throw new JRException("Invalid request. Response was : " + result.getResponseCode() + " (" + result.getResponseMessage() + ")\n" + errorMessageStream.toString());
} catch (IOException ioException) {
throw new JRException(ioException);
}
}
use of org.opennms.netmgt.measurements.model.QueryRequest 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.model.QueryRequest in project opennms by OpenNMS.
the class MeasurementsWrapper method computeUtilization.
/**
* This method computes the utilization of a given interface resource. The method returns two double values
* encapsulated in a list. It uses the HC attributes for the computation and non-HC as fallback attributes.
*
* @param resource the resource for which the utilization must be computed
* @param start the start timestamp
* @param end the end timestamp
* @param step the step size
* @param aggregation the aggregation function
* @return a list containing two double values for the in/out percentage utilization
*/
public List<Double> computeUtilization(final String resource, final long start, final long end, final long step, final String aggregation) throws MeasurementException {
QueryRequest request = new QueryRequest();
request.setRelaxed(true);
request.setStart(start);
request.setEnd(end);
request.setStep(step);
Source sourceIn = new Source();
sourceIn.setAggregation(aggregation);
sourceIn.setTransient(true);
sourceIn.setAttribute("ifHCInOctets");
// using non-HC attributes as fallback
sourceIn.setFallbackAttribute("ifInOctets");
sourceIn.setResourceId(resource);
sourceIn.setLabel("ifInOctets");
Source sourceOut = new Source();
sourceOut.setAggregation(aggregation);
sourceOut.setTransient(true);
sourceOut.setAttribute("ifHCOutOctets");
// using non-HC attributes as fallback
sourceOut.setFallbackAttribute("ifOutOctets");
sourceOut.setResourceId(resource);
sourceOut.setLabel("ifOutOctets");
request.setExpressions(Arrays.asList(new Expression("ifInPercent", "(8 * ifInOctects / 1000000) / ifInOctets.ifHighSpeed * 100", false), new Expression("ifOutPercent", "(8 * ifOutOctects / 1000000) / ifOutOctets.ifHighSpeed * 100", false)));
request.setSources(Arrays.asList(sourceIn, sourceOut));
QueryResponse.WrappedPrimitive[] columns = measurementsService.query(request).getColumns();
double[] values1 = columns[0].getList();
double[] values2 = columns[1].getList();
for (int i = values1.length - 1; i >= 0; i--) {
if (!Double.isNaN(values1[i]) && !Double.isNaN(values2[i])) {
return Arrays.asList(values1[i], values2[i]);
}
}
return Arrays.asList(Double.NaN, Double.NaN);
}
use of org.opennms.netmgt.measurements.model.QueryRequest in project opennms by OpenNMS.
the class MeasurementsRestServiceWithJrbIT method canPerformExpressions.
@Test
public void canPerformExpressions() {
QueryRequest request = new QueryRequest();
request.setStart(1414602000000L);
request.setEnd(1417046400000L);
request.setStep(1000L);
request.setMaxRows(700);
Source ifInOctets = new Source();
ifInOctets.setResourceId("node[1].interfaceSnmp[eth0-04013f75f101]");
ifInOctets.setAttribute("ifInOctets");
ifInOctets.setAggregation("MAX");
ifInOctets.setLabel("ifInOctets");
request.setSources(Lists.newArrayList(ifInOctets));
Expression scale = new Expression();
scale.setLabel("ifUsage");
// References a variable from strings.properties
scale.setExpression("ifInOctets * 8 / ifInOctets.ifSpeed");
request.setExpressions(Lists.newArrayList(scale));
QueryResponse response = m_svc.query(request);
final int idx = 3;
final Map<String, double[]> columns = response.columnsWithLabels();
assertEquals(975.3053156146178, columns.get("ifInOctets")[idx], 0.0001);
assertEquals(975.3053156146178 * 8d / 1000.0d, columns.get("ifUsage")[idx], 0.0001);
}
use of org.opennms.netmgt.measurements.model.QueryRequest in project opennms by OpenNMS.
the class MeasurementsRestServiceITCase method notFoundOnMissingAttribute.
@Test
public void notFoundOnMissingAttribute() {
final QueryRequest request = buildRequest();
request.getSources().get(0).setAttribute("n0tIfInOctets");
exception.expect(exceptionWithResponseCode(404));
m_svc.query(request);
}
Aggregations