use of org.opennms.core.rpc.api.RequestRejectedException in project opennms by OpenNMS.
the class DefaultPollContext method testCriticalPath.
private boolean testCriticalPath(CriticalPath criticalPath) {
if (!"ICMP".equalsIgnoreCase(criticalPath.getServiceName())) {
LOG.warn("Critical paths using services other than ICMP are not currently supported." + " ICMP will be used for testing {}.", criticalPath);
}
final InetAddress ipAddress = criticalPath.getIpAddress();
final int retries = OpennmsServerConfigFactory.getInstance().getDefaultCriticalPathRetries();
final int timeout = OpennmsServerConfigFactory.getInstance().getDefaultCriticalPathTimeout();
boolean available = false;
try {
final PingSummary pingSummary = m_locationAwarePingClient.ping(ipAddress).withLocation(criticalPath.getLocationName()).withTimeout(timeout, TimeUnit.MILLISECONDS).withRetries(retries).execute().get();
// We consider the path to be available if any of the requests were successful
available = pingSummary.getSequences().stream().filter(s -> s.isSuccess()).count() > 0;
} catch (InterruptedException e) {
LOG.warn("Interrupted while testing {}. Marking the path as available.", criticalPath);
available = true;
} catch (Throwable e) {
final Throwable cause = e.getCause();
if (cause != null && cause instanceof RequestTimedOutException) {
LOG.warn("No response was received when remotely testing {}." + " Marking the path as available.", criticalPath);
available = true;
} else if (cause != null && cause instanceof RequestRejectedException) {
LOG.warn("Request was rejected when attemtping to test the remote path {}." + " Marking the path as available.", criticalPath);
available = true;
}
LOG.warn("An unknown error occured while testing the critical path: {}." + " Marking the path as unavailable.", criticalPath, e);
available = false;
}
LOG.debug("testCriticalPath: checking {}@{}, available ? {}", criticalPath.getServiceName(), ipAddress, available);
return available;
}
use of org.opennms.core.rpc.api.RequestRejectedException in project opennms by OpenNMS.
the class TcaCollectionHandler method collect.
/**
* Collect.
*
* @throws CollectionException the collection exception
*/
protected CollectionSet collect() throws CollectionException {
try {
CollectionSetBuilder builder = new CollectionSetBuilder(m_agent);
TcaData tracker = new TcaData(m_agent.getAddress());
CompletableFuture<TcaData> future = m_locationAwareSnmpClient.walk(m_agent.getAgentConfig(), tracker).withDescription("TcaCollector for " + m_agent.getHostAddress()).withLocation(m_agent.getLocationName()).execute();
LOG.debug("collect: successfully instantiated TCA Collector for {}", m_agent.getHostAddress());
future.get();
LOG.info("collect: node TCA query for address {} complete.", m_agent.getHostAddress());
process(tracker, builder);
return builder.build();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new CollectionUnknown("Collection of node TCA data for interface " + m_agent.getHostAddress() + " interrupted: " + e, e);
} catch (ExecutionException e) {
final Throwable cause = e.getCause();
if (cause != null && cause instanceof RequestTimedOutException) {
throw new CollectionUnknown(String.format("No response received when remotely collecting TCA data" + " for interface %s at location %s interrupted.", m_agent.getHostAddress(), m_agent.getLocationName()), e);
} else if (cause != null && cause instanceof RequestRejectedException) {
throw new CollectionUnknown(String.format("The request to remotely collect TCA data" + " for interface %s at location %s was rejected.", m_agent.getHostAddress(), m_agent.getLocationName()), e);
}
throw new CollectionException(String.format("Unexpected exception when collecting TCA data for interface %s at location %s.", m_agent.getHostAddress(), m_agent.getLocationName()), e);
}
}
use of org.opennms.core.rpc.api.RequestRejectedException in project opennms by OpenNMS.
the class CamelRpcClientFactory method getClient.
@Override
public <S extends RpcRequest, T extends RpcResponse> RpcClient<S, T> getClient(RpcModule<S, T> module) {
return new RpcClient<S, T>() {
@Override
public CompletableFuture<T> execute(S request) {
if (request.getLocation() == null || request.getLocation().equals(location)) {
// The request is for the current location, invoke it directly
return module.execute(request);
}
// Save the context map and restore it on callback
final Map<String, String> clientContextMap = Logging.getCopyOfContextMap();
// Wrap the request in a CamelRpcRequest and forward it to the Camel route
final CompletableFuture<T> future = new CompletableFuture<>();
try {
template.asyncCallbackSendBody(endpoint, new CamelRpcRequest<>(module, request), new Synchronization() {
@Override
public void onComplete(Exchange exchange) {
try (MDCCloseable mdc = Logging.withContextMapCloseable(clientContextMap)) {
final T response = module.unmarshalResponse(exchange.getOut().getBody(String.class));
if (response.getErrorMessage() != null) {
future.completeExceptionally(new RemoteExecutionException(response.getErrorMessage()));
} else {
future.complete(response);
}
} catch (Throwable ex) {
LOG.error("Unmarshalling a response in RPC module {} failed.", module, ex);
future.completeExceptionally(ex);
}
// Ensure that future log statements on this thread are routed properly
Logging.putPrefix(RpcClientFactory.LOG_PREFIX);
}
@Override
public void onFailure(Exchange exchange) {
try (MDCCloseable mdc = Logging.withContextMapCloseable(clientContextMap)) {
final ExchangeTimedOutException timeoutException = exchange.getException(ExchangeTimedOutException.class);
final DirectConsumerNotAvailableException directConsumerNotAvailableException = exchange.getException(DirectConsumerNotAvailableException.class);
if (timeoutException != null) {
// Wrap timeout exceptions within a RequestTimedOutException
future.completeExceptionally(new RequestTimedOutException(exchange.getException()));
} else if (directConsumerNotAvailableException != null) {
// Wrap consumer not available exceptions with a RequestRejectedException
future.completeExceptionally(new RequestRejectedException(exchange.getException()));
} else {
future.completeExceptionally(exchange.getException());
}
}
// Ensure that future log statements on this thread are routed properly
Logging.putPrefix(RpcClientFactory.LOG_PREFIX);
}
});
} catch (IllegalStateException e) {
try (MDCCloseable mdc = Logging.withContextMapCloseable(clientContextMap)) {
// Wrap ProducerTemplate exceptions with a RequestRejectedException
future.completeExceptionally(new RequestRejectedException(e));
}
// Ensure that future log statements on this thread are routed properly
Logging.putPrefix(RpcClientFactory.LOG_PREFIX);
}
return future;
}
};
}
use of org.opennms.core.rpc.api.RequestRejectedException in project opennms by OpenNMS.
the class EchoRpcIT method futureFailsWithRequestRejectedExceptionWhenClientContextIsStopped.
/**
* Verifies that the future fails with a {@code RequestRejectedException} when
* when the client context is stopped.
*/
@Test(timeout = 60000)
public void futureFailsWithRequestRejectedExceptionWhenClientContextIsStopped() throws Exception {
assertNotEquals(REMOTE_LOCATION_NAME, identity.getLocation());
// Stop the client context, this will happen when OpenNMS is shutting down
rpcClientContext.stop();
// Now issue an RPC
EchoRequest request = new EchoRequest("Helló");
request.setLocation(REMOTE_LOCATION_NAME);
try {
echoClient.execute(request).get();
fail();
} catch (ExecutionException e) {
assertEquals(RequestRejectedException.class, e.getCause().getClass());
}
}
Aggregations