Search in sources :

Example 6 with QueryResponse

use of org.opennms.netmgt.measurements.model.QueryResponse 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 7 with QueryResponse

use of org.opennms.netmgt.measurements.model.QueryResponse 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)

Example 8 with QueryResponse

use of org.opennms.netmgt.measurements.model.QueryResponse in project opennms by OpenNMS.

the class GraphMLEdgeStatusProviderIT method verify.

@Test
public void verify() throws Exception {
    final GraphMLServiceAccessor serviceAccessor = new GraphMLServiceAccessor();
    serviceAccessor.setTransactionOperations(transactionOperations);
    serviceAccessor.setNodeDao(nodeDao);
    serviceAccessor.setSnmpInterfaceDao(snmpInterfaceDao);
    serviceAccessor.setMeasurementsService(request -> new QueryResponse());
    final GraphMLGraph graph = GraphMLReader.read(getClass().getResourceAsStream("/test-graph2.xml")).getGraphs().get(0);
    final GraphMLTopologyProvider topologyProvider = new GraphMLTopologyProvider(graph, serviceAccessor);
    final GraphMLEdgeStatusProvider provider = new GraphMLEdgeStatusProvider(topologyProvider, new ScriptEngineManager(), serviceAccessor, Paths.get("src", "test", "opennms-home", "etc", "graphml-edge-status"));
    assertThat(provider.contributesTo("acme:regions"), is(true));
    assertThat(provider.getNamespace(), is("acme:regions"));
    // Calculating the status executes some tests defined int the according scripts as a side effect
    final EdgeRef edgeRef = topologyProvider.getEdge("acme:regions", "center_north");
    final Map<EdgeRef, Status> status = provider.getStatusForEdges(topologyProvider, ImmutableList.of(edgeRef), new Criteria[0]);
    // Checking nodeID creation for vertices with only foreignSource/foreignID set
    final VertexRef vertexRef = topologyProvider.getVertex("acme:regions", "west");
    assertThat(vertexRef, is(notNullValue()));
    assertThat(vertexRef, is(instanceOf(GraphMLVertex.class)));
    assertThat(((GraphMLVertex) vertexRef).getNodeID(), is(4));
    // Testing status merging from two scripts
    assertThat(status, is(notNullValue()));
    assertThat(status, is(hasEntry(edgeRef, new GraphMLEdgeStatus().severity(OnmsSeverity.WARNING).style("stroke", "pink").style("stroke-width", "3em"))));
}
Also used : Status(org.opennms.features.topology.api.topo.Status) QueryResponse(org.opennms.netmgt.measurements.model.QueryResponse) GraphMLServiceAccessor(org.opennms.features.topology.plugins.topo.graphml.internal.GraphMLServiceAccessor) GraphMLGraph(org.opennms.features.graphml.model.GraphMLGraph) ScriptEngineManager(javax.script.ScriptEngineManager) EdgeRef(org.opennms.features.topology.api.topo.EdgeRef) VertexRef(org.opennms.features.topology.api.topo.VertexRef) Test(org.junit.Test)

Example 9 with QueryResponse

use of org.opennms.netmgt.measurements.model.QueryResponse 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 10 with QueryResponse

use of org.opennms.netmgt.measurements.model.QueryResponse in project opennms by OpenNMS.

the class MeasurementsRestServiceITCase method canRetrieveFallbackAttributeWhenAttributeNotFound.

@Test
public void canRetrieveFallbackAttributeWhenAttributeNotFound() {
    QueryRequest request = new QueryRequest();
    request.setStart(1414602000000L);
    request.setEnd(1417046400000L);
    request.setStep(1000L);
    request.setMaxRows(700);
    // Average
    Source ifInOctetsAvg = new Source();
    ifInOctetsAvg.setResourceId("node[1].interfaceSnmp[eth0-04013f75f101]");
    ifInOctetsAvg.setAttribute("willNotBeFound");
    ifInOctetsAvg.setFallbackAttribute("ifInOctets");
    ifInOctetsAvg.setAggregation("AVERAGE");
    ifInOctetsAvg.setLabel("ifInOctetsAvg");
    request.setSources(Lists.newArrayList(ifInOctetsAvg));
    // Perform the query
    QueryResponse response = m_svc.query(request);
    // Validate the results
    long[] timestamps = response.getTimestamps();
    final Map<String, double[]> columns = response.columnsWithLabels();
    assertEquals(3600000L, response.getStep());
    assertEquals(680, timestamps.length);
    // Verify the values at an arbitrary index
    final int idx = 8;
    assertEquals(1414630800000L, timestamps[idx]);
    assertEquals(270.66140826873385, columns.get("ifInOctetsAvg")[idx], 0.0001);
}
Also used : QueryRequest(org.opennms.netmgt.measurements.model.QueryRequest) QueryResponse(org.opennms.netmgt.measurements.model.QueryResponse) Source(org.opennms.netmgt.measurements.model.Source) Test(org.junit.Test)

Aggregations

QueryResponse (org.opennms.netmgt.measurements.model.QueryResponse)10 Test (org.junit.Test)7 QueryRequest (org.opennms.netmgt.measurements.model.QueryRequest)7 Source (org.opennms.netmgt.measurements.model.Source)7 ResourceNotFoundException (org.opennms.netmgt.measurements.api.exceptions.ResourceNotFoundException)3 FetchException (org.opennms.netmgt.measurements.api.exceptions.FetchException)2 ValidationException (org.opennms.netmgt.measurements.api.exceptions.ValidationException)2 ScriptEngineManager (javax.script.ScriptEngineManager)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 JRException (net.sf.jasperreports.engine.JRException)1 GraphMLGraph (org.opennms.features.graphml.model.GraphMLGraph)1 EdgeRef (org.opennms.features.topology.api.topo.EdgeRef)1 Status (org.opennms.features.topology.api.topo.Status)1 VertexRef (org.opennms.features.topology.api.topo.VertexRef)1 GraphMLServiceAccessor (org.opennms.features.topology.plugins.topo.graphml.internal.GraphMLServiceAccessor)1 EmptyJRDataSource (org.opennms.netmgt.jasper.measurement.EmptyJRDataSource)1