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