Search in sources :

Example 11 with QueryRequest

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);
    }
}
Also used : QueryRequest(org.opennms.netmgt.measurements.model.QueryRequest) JRException(net.sf.jasperreports.engine.JRException) MeasurementDataSource(org.opennms.netmgt.jasper.measurement.MeasurementDataSource) IOException(java.io.IOException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) EmptyJRDataSource(org.opennms.netmgt.jasper.measurement.EmptyJRDataSource)

Example 12 with QueryRequest

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);
        }
    }
}
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 13 with QueryRequest

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);
}
Also used : QueryRequest(org.opennms.netmgt.measurements.model.QueryRequest) Expression(org.opennms.netmgt.measurements.model.Expression) Source(org.opennms.netmgt.measurements.model.Source)

Example 14 with QueryRequest

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);
}
Also used : QueryRequest(org.opennms.netmgt.measurements.model.QueryRequest) Expression(org.opennms.netmgt.measurements.model.Expression) QueryResponse(org.opennms.netmgt.measurements.model.QueryResponse) Source(org.opennms.netmgt.measurements.model.Source) Test(org.junit.Test)

Example 15 with QueryRequest

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);
}
Also used : QueryRequest(org.opennms.netmgt.measurements.model.QueryRequest) Test(org.junit.Test)

Aggregations

QueryRequest (org.opennms.netmgt.measurements.model.QueryRequest)21 Test (org.junit.Test)13 Source (org.opennms.netmgt.measurements.model.Source)13 QueryResponse (org.opennms.netmgt.measurements.model.QueryResponse)7 Expression (org.opennms.netmgt.measurements.model.Expression)3 JRException (net.sf.jasperreports.engine.JRException)2 EmptyJRDataSource (org.opennms.netmgt.jasper.measurement.EmptyJRDataSource)2 MeasurementDataSource (org.opennms.netmgt.jasper.measurement.MeasurementDataSource)2 Request (com.github.tomakehurst.wiremock.http.Request)1 LoggedRequest (com.github.tomakehurst.wiremock.verification.LoggedRequest)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 Date (java.util.Date)1 Consumes (javax.ws.rs.Consumes)1 GET (javax.ws.rs.GET)1 Path (javax.ws.rs.Path)1 Produces (javax.ws.rs.Produces)1 WebApplicationException (javax.ws.rs.WebApplicationException)1 FetchResults (org.opennms.netmgt.measurements.api.FetchResults)1