Search in sources :

Example 1 with RequestRejectedException

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;
}
Also used : PollEvent(org.opennms.netmgt.poller.pollables.PollEvent) Arrays(java.util.Arrays) Date(java.util.Date) LoggerFactory(org.slf4j.LoggerFactory) RequestRejectedException(org.opennms.core.rpc.api.RequestRejectedException) InetAddrUtils(org.opennms.netmgt.snmp.InetAddrUtils) EventConstants(org.opennms.netmgt.events.api.EventConstants) PingSummary(org.opennms.netmgt.icmp.proxy.PingSummary) InetAddress(java.net.InetAddress) EventListener(org.opennms.netmgt.events.api.EventListener) SQLException(java.sql.SQLException) Event(org.opennms.netmgt.xml.event.Event) Logger(org.slf4j.Logger) Iterator(java.util.Iterator) OpennmsServerConfigFactory(org.opennms.netmgt.config.OpennmsServerConfigFactory) EventIpcManager(org.opennms.netmgt.events.api.EventIpcManager) PollableService(org.opennms.netmgt.poller.pollables.PollableService) RequestTimedOutException(org.opennms.core.rpc.api.RequestTimedOutException) EventBuilder(org.opennms.netmgt.model.events.EventBuilder) PollerConfig(org.opennms.netmgt.config.PollerConfig) PollContext(org.opennms.netmgt.poller.pollables.PollContext) TimeUnit(java.util.concurrent.TimeUnit) CriticalPath(org.opennms.netmgt.dao.api.CriticalPath) LocationAwarePingClient(org.opennms.netmgt.icmp.proxy.LocationAwarePingClient) PendingPollEvent(org.opennms.netmgt.poller.pollables.PendingPollEvent) Queue(java.util.Queue) PathOutageManagerDaoImpl(org.opennms.netmgt.dao.hibernate.PathOutageManagerDaoImpl) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue) RequestTimedOutException(org.opennms.core.rpc.api.RequestTimedOutException) PingSummary(org.opennms.netmgt.icmp.proxy.PingSummary) RequestRejectedException(org.opennms.core.rpc.api.RequestRejectedException) InetAddress(java.net.InetAddress)

Example 2 with RequestRejectedException

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);
    }
}
Also used : RequestTimedOutException(org.opennms.core.rpc.api.RequestTimedOutException) CollectionSetBuilder(org.opennms.netmgt.collection.support.builder.CollectionSetBuilder) RequestRejectedException(org.opennms.core.rpc.api.RequestRejectedException) CollectionException(org.opennms.netmgt.collection.api.CollectionException) CollectionUnknown(org.opennms.netmgt.collectd.CollectionUnknown) ExecutionException(java.util.concurrent.ExecutionException)

Example 3 with RequestRejectedException

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;
        }
    };
}
Also used : DirectConsumerNotAvailableException(org.apache.camel.component.direct.DirectConsumerNotAvailableException) RequestTimedOutException(org.opennms.core.rpc.api.RequestTimedOutException) RequestRejectedException(org.opennms.core.rpc.api.RequestRejectedException) Synchronization(org.apache.camel.spi.Synchronization) Exchange(org.apache.camel.Exchange) RemoteExecutionException(org.opennms.core.rpc.api.RemoteExecutionException) CompletableFuture(java.util.concurrent.CompletableFuture) MDCCloseable(org.opennms.core.logging.Logging.MDCCloseable) ExchangeTimedOutException(org.apache.camel.ExchangeTimedOutException) RpcClient(org.opennms.core.rpc.api.RpcClient)

Example 4 with RequestRejectedException

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());
    }
}
Also used : RequestRejectedException(org.opennms.core.rpc.api.RequestRejectedException) EchoRequest(org.opennms.core.rpc.echo.EchoRequest) RemoteExecutionException(org.opennms.core.rpc.api.RemoteExecutionException) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.Test)

Aggregations

RequestRejectedException (org.opennms.core.rpc.api.RequestRejectedException)4 RequestTimedOutException (org.opennms.core.rpc.api.RequestTimedOutException)3 ExecutionException (java.util.concurrent.ExecutionException)2 RemoteExecutionException (org.opennms.core.rpc.api.RemoteExecutionException)2 InetAddress (java.net.InetAddress)1 SQLException (java.sql.SQLException)1 Arrays (java.util.Arrays)1 Date (java.util.Date)1 Iterator (java.util.Iterator)1 Queue (java.util.Queue)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 ConcurrentLinkedQueue (java.util.concurrent.ConcurrentLinkedQueue)1 TimeUnit (java.util.concurrent.TimeUnit)1 Exchange (org.apache.camel.Exchange)1 ExchangeTimedOutException (org.apache.camel.ExchangeTimedOutException)1 DirectConsumerNotAvailableException (org.apache.camel.component.direct.DirectConsumerNotAvailableException)1 Synchronization (org.apache.camel.spi.Synchronization)1 Test (org.junit.Test)1 MDCCloseable (org.opennms.core.logging.Logging.MDCCloseable)1 RpcClient (org.opennms.core.rpc.api.RpcClient)1