From 7fc7f50e517564df0c4e891b80528283266b634d Mon Sep 17 00:00:00 2001 From: Robert Varga Date: Sun, 27 Sep 2020 12:19:32 +0200 Subject: [PATCH] Remove serviceutils.tools.mdsal.rpc This package is not used anywhere anymore, remove it. Change-Id: I1e488ebb7f66ad28b61e104529e5d8d86b776efa Signed-off-by: Robert Varga --- .../tools/mdsal/rpc/FutureRpcResults.java | 287 ------------------ tools/testutils/pom.xml | 5 + .../mdsal/testutils/TestFutureRpcResults.java | 2 +- .../tools/mdsal/rpc/FutureRpcResultsTest.java | 5 +- 4 files changed, 9 insertions(+), 290 deletions(-) delete mode 100644 tools/api/src/main/java/org/opendaylight/serviceutils/tools/mdsal/rpc/FutureRpcResults.java diff --git a/tools/api/src/main/java/org/opendaylight/serviceutils/tools/mdsal/rpc/FutureRpcResults.java b/tools/api/src/main/java/org/opendaylight/serviceutils/tools/mdsal/rpc/FutureRpcResults.java deleted file mode 100644 index ba6f0396..00000000 --- a/tools/api/src/main/java/org/opendaylight/serviceutils/tools/mdsal/rpc/FutureRpcResults.java +++ /dev/null @@ -1,287 +0,0 @@ -/* - * Copyright (c) 2018 Red Hat, Inc. and others. All rights reserved. - * - * This program and the accompanying materials are made available under the - * terms of the Eclipse Public License v1.0 which accompanies this distribution, - * and is available at http://www.eclipse.org/legal/epl-v10.html - */ -package org.opendaylight.serviceutils.tools.mdsal.rpc; - -import static org.opendaylight.yangtools.yang.common.RpcError.ErrorType.APPLICATION; - -import com.google.common.annotations.Beta; -import com.google.common.util.concurrent.FutureCallback; -import com.google.common.util.concurrent.Futures; -import com.google.common.util.concurrent.ListenableFuture; -import com.google.common.util.concurrent.MoreExecutors; -import com.google.common.util.concurrent.SettableFuture; -import edu.umd.cs.findbugs.annotations.CheckReturnValue; -import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; -import java.util.concurrent.Callable; -import java.util.concurrent.Future; -import java.util.function.Consumer; -import java.util.function.Function; -import org.eclipse.jdt.annotation.Nullable; -import org.opendaylight.infrautils.utils.StackTraces; -import org.opendaylight.yangtools.concepts.Builder; -import org.opendaylight.yangtools.yang.common.OperationFailedException; -import org.opendaylight.yangtools.yang.common.RpcError; -import org.opendaylight.yangtools.yang.common.RpcResult; -import org.opendaylight.yangtools.yang.common.RpcResultBuilder; -import org.slf4j.Logger; - -/** - * Utility to simplify correctly handling transformation of Future of RpcResult to return. - * - * @author Michael Vorburger.ch - * @deprecated Use {@code FutureRpcResults} in {@code rpc-api} instead. - */ -@Beta -@Deprecated -public final class FutureRpcResults { - - // NB: The FutureRpcResultsTest unit test for this util is in mdsalutil-testutils's src/test, not this project's - - // TODO Once matured in genius, this class could be proposed to org.opendaylight.yangtools.yang.common - // (This was proposed in Oct on yangtools-dev list, but there little interest due to plans to change RpcResult.) - - private FutureRpcResults() { - - } - - /** - * Create a Builder for a ListenableFuture to Future<RpcResult<O>> transformer. By default, the future - * will log success or failure, with configurable log levels; the caller can also add handlers for success and/or - * failure. - * - *

The RPC's method name is automatically obtained using {@link StackTraces}. This has some cost, which in - * the overall scheme of a typical RPC is typically negligible, but on a highly optimized fast path could - * theoretically be an issue; if you see this method as a hot spot in a profiler, then (only) use the - * alternative signature where you manually pass the String rpcMethodName. - * - * @param logger the slf4j Logger of the caller - * @param input the RPC input DataObject of the caller (may be null) - * @param callable the Callable (typically lambda) creating a ListenableFuture. Note that the - * functional interface Callable's call() method declares throws Exception, so your lambda - * does not have to do any exception handling (specifically it does NOT have to catch and - * wrap any exception into a failed Future); this utility does that for you. - * - * @return a new Builder - */ - @CheckReturnValue - public static FutureRpcResultBuilder fromListenableFuture(Logger logger, - @Nullable I input, Callable> callable) { - return new FutureRpcResultBuilder<>(logger, StackTraces.getCallersCallerMethodName(), input, callable); - } - - /** - * Create a Builder for a ListenableFuture to Future<RpcResult<O>> transformer. By default, the future - * will log success or failure, with configurable log levels; the caller can also add handlers for success and/or - * failure. - * - * @param logger the slf4j Logger of the caller - * @param rpcMethodName Java method name (without "()") of the RPC operation, used for logging - * @param input the RPC input DataObject of the caller (may be null) - * @param callable the Callable (typically lambda) creating a ListenableFuture. Note that the - * functional interface Callable's call() method declares throws Exception, so your lambda - * does not have to do any exception handling (specifically it does NOT have to catch and - * wrap any exception into a failed Future); this utility does that for you. - * - * @return a new FutureRpcResultBuilder - */ - @CheckReturnValue - @SuppressWarnings("InconsistentOverloads") // Error Prone is too strict here; we do want the Callable last - public static FutureRpcResultBuilder fromListenableFuture(Logger logger, String rpcMethodName, - @Nullable I input, Callable> callable) { - return new FutureRpcResultBuilder<>(logger, rpcMethodName, input, callable); - } - - public enum LogLevel { - ERROR, WARN, INFO, DEBUG, TRACE, - /** - * Note that when using LogLevel NONE for failures, then you should set a - * {@link FutureRpcResultBuilder#onFailure(Consumer)} which does better logging, - * or be 100% sure that all callers of the RPC check the returned Future RpcResult appropriately; - * otherwise you will lose error messages. - */ - NONE; - @SuppressFBWarnings({"SLF4J_UNKNOWN_ARRAY","SLF4J_FORMAT_SHOULD_BE_CONST"}) - public void log(Logger logger, String format, Object... arguments) { - switch (this) { - case NONE: - break; - case TRACE: - logger.trace(format, arguments); - break; - case DEBUG: - logger.debug(format, arguments); - break; - case INFO: - logger.info(format, arguments); - break; - case WARN: - logger.warn(format, arguments); - break; - default: // including ERROR - logger.error(format, arguments); - break; - } - } - } - - @CheckReturnValue - @SuppressWarnings("InconsistentOverloads") // Error Prone is too strict here; we do want the Callable last - public static FutureRpcResultBuilder fromBuilder(Logger logger, String rpcMethodName, - @Nullable I input, Callable> builder) { - Callable> callable = () -> Futures.immediateFuture(builder.call().build()); - return fromListenableFuture(logger, rpcMethodName, input, callable); - } - - @CheckReturnValue - public static FutureRpcResultBuilder fromBuilder(Logger logger, @Nullable I input, - Callable> builder) { - Callable> callable = () -> Futures.immediateFuture(builder.call().build()); - return fromListenableFuture(logger, StackTraces.getCallersCallerMethodName(), input, callable); - } - - public static final class FutureRpcResultBuilder implements Builder>> { - private static final Function DEFAULT_ERROR_MESSAGE_FUNCTION = Throwable::getMessage; - private static final Consumer DEFAULT_ON_FAILURE = throwable -> { }; - - private final Consumer defaultOnSuccess = result -> { }; - - // fixed (final) builder values - private final Logger logger; - private final String rpcMethodName; - @Nullable private final I input; - private final Callable> callable; - - // optional builder values, which can be overridden by users - private Function rpcErrorMessageFunction = DEFAULT_ERROR_MESSAGE_FUNCTION; - private Consumer onSuccessConsumer = result -> { }; - private Consumer onFailureConsumer = DEFAULT_ON_FAILURE; - - // defaulted builder values, which can be overridden by users - private LogLevel onEnterLogLevel = LogLevel.TRACE; - private LogLevel onSuccessLogLevel = LogLevel.DEBUG; - private LogLevel onFailureLogLevel = LogLevel.ERROR; - - private FutureRpcResultBuilder(Logger logger, String rpcMethodName, @Nullable I input, - Callable> callable) { - this.logger = logger; - this.rpcMethodName = rpcMethodName; - this.input = input; - this.callable = callable; - } - - /** - * Builds the Future RpcResult. - * - * @return Future RpcResult. Note that this will NEVER be a failed Future; any - * errors are reported as !{@link RpcResult#isSuccessful()}, with - * details in {@link RpcResult#getErrors()}, and not the Future itself. - */ - @Override - @CheckReturnValue - @SuppressWarnings("checkstyle:IllegalCatch") - public ListenableFuture> build() { - SettableFuture> futureRpcResult = SettableFuture.create(); - FutureCallback callback = new FutureCallback() { - @Override - public void onSuccess(O result) { - onSuccessLogLevel.log(logger, "RPC {}() successful; input = {}, output = {}", rpcMethodName, - input, result); - onSuccessConsumer.accept(result); - futureRpcResult.set(RpcResultBuilder.success(result).build()); - } - - @Override - public void onFailure(Throwable cause) { - onFailureLogLevel.log(logger, "RPC {}() failed; input = {}", rpcMethodName, input, cause); - onFailureConsumer.accept(cause); - RpcResultBuilder rpcResultBuilder = RpcResultBuilder.failed(); - if (cause instanceof OperationFailedException) { - // NB: This looses (not not propagate) the cause, and only preserves the error list - // But we did log the cause above, so it can still be found. - rpcResultBuilder.withRpcErrors(((OperationFailedException) cause).getErrorList()); - } else { - rpcResultBuilder.withError(APPLICATION, rpcErrorMessageFunction.apply(cause), cause); - } - futureRpcResult.set(rpcResultBuilder.build()); - } - }; - try { - onEnterLogLevel.log(logger, "RPC {}() entered; input = {}", rpcMethodName, input); - Futures.addCallback(callable.call(), callback, MoreExecutors.directExecutor()); - } catch (Exception cause) { - callback.onFailure(cause); - } - return futureRpcResult; - } - - /** - * Sets a custom on-failure action, for a given exception. - */ - public FutureRpcResultBuilder onFailure(Consumer newOnFailureConsumer) { - if (onFailureConsumer != DEFAULT_ON_FAILURE) { - throw new IllegalStateException("onFailure can only be set once"); - } - this.onFailureConsumer = newOnFailureConsumer; - return this; - } - - /** - * Sets a custom on-failure SLF4J logging level, in case of an exception. The log message mentions the RPC - * method name, the provided input, the exception and its stack trace (depending on logger settings). - * By default, it is {@code LOG.error}. Setting {@code NONE} will disable this logging. - */ - public FutureRpcResultBuilder onFailureLogLevel(LogLevel level) { - this.onFailureLogLevel = level; - return this; - } - - /** - * Sets a custom on-success SLF4J logging level. The log message mentions the RPC method name, the provided - * input, and the resulting output. - * By default, it is {@code LOG.debug}. Setting {@code NONE} will disable this logging. - */ - public FutureRpcResultBuilder onSuccessLogLevel(LogLevel level) { - this.onSuccessLogLevel = level; - return this; - } - - /** - * Sets a custom on-enter SLF4J logging level. The log message mentions the RPC method name and the provided - * input. - * By default, it is {@code LOG.trace}. Setting {@code NONE} will disable this logging. - */ - public FutureRpcResultBuilder onEnterLogLevel(LogLevel level) { - this.onEnterLogLevel = level; - return this; - } - - /** - * Set a custom {@link RpcError} message function, for a given exception. - * By default, the message is just {@link Throwable#getMessage()}. - */ - public FutureRpcResultBuilder withRpcErrorMessage(Function newRpcErrorMessageFunction) { - if (rpcErrorMessageFunction != DEFAULT_ERROR_MESSAGE_FUNCTION) { - throw new IllegalStateException("rpcErrorMessage can only be set once"); - } - this.rpcErrorMessageFunction = newRpcErrorMessageFunction; - return this; - } - - /** - * Sets a custom on-success action, for a given output. - */ - public FutureRpcResultBuilder onSuccess(Consumer newOnSuccessFunction) { - if (onSuccessConsumer != defaultOnSuccess) { - throw new IllegalStateException("onSuccess can only be set once"); - } - this.onSuccessConsumer = newOnSuccessFunction; - return this; - } - - } -} diff --git a/tools/testutils/pom.xml b/tools/testutils/pom.xml index 2a758be7..f36d62a3 100644 --- a/tools/testutils/pom.xml +++ b/tools/testutils/pom.xml @@ -30,6 +30,11 @@ and is available at http://www.eclipse.org/legal/epl-v10.html tools-api ${project.version} + + org.opendaylight.serviceutils + rpc-api + ${project.version} + org.opendaylight.yangtools testutils diff --git a/tools/testutils/src/main/java/org/opendaylight/serviceutils/tools/mdsal/testutils/TestFutureRpcResults.java b/tools/testutils/src/main/java/org/opendaylight/serviceutils/tools/mdsal/testutils/TestFutureRpcResults.java index 2b59cb23..aa7683e6 100644 --- a/tools/testutils/src/main/java/org/opendaylight/serviceutils/tools/mdsal/testutils/TestFutureRpcResults.java +++ b/tools/testutils/src/main/java/org/opendaylight/serviceutils/tools/mdsal/testutils/TestFutureRpcResults.java @@ -16,7 +16,7 @@ import java.util.Collection; import java.util.concurrent.ExecutionException; import java.util.concurrent.Future; import java.util.concurrent.TimeoutException; -import org.opendaylight.serviceutils.tools.mdsal.rpc.FutureRpcResults; +import org.opendaylight.serviceutils.tools.rpc.FutureRpcResults; import org.opendaylight.yangtools.yang.binding.DataObject; import org.opendaylight.yangtools.yang.common.RpcError; import org.opendaylight.yangtools.yang.common.RpcError.ErrorType; diff --git a/tools/testutils/src/test/java/org/opendaylight/serviceutils/tools/mdsal/rpc/FutureRpcResultsTest.java b/tools/testutils/src/test/java/org/opendaylight/serviceutils/tools/mdsal/rpc/FutureRpcResultsTest.java index daab8fb6..197258d5 100644 --- a/tools/testutils/src/test/java/org/opendaylight/serviceutils/tools/mdsal/rpc/FutureRpcResultsTest.java +++ b/tools/testutils/src/test/java/org/opendaylight/serviceutils/tools/mdsal/rpc/FutureRpcResultsTest.java @@ -10,7 +10,7 @@ package org.opendaylight.serviceutils.tools.mdsal.rpc; import static com.google.common.truth.Truth.assertThat; import static com.google.common.util.concurrent.Futures.immediateFailedFuture; import static com.google.common.util.concurrent.Futures.immediateFuture; -import static org.opendaylight.serviceutils.tools.mdsal.rpc.FutureRpcResults.LogLevel.NONE; +import static org.opendaylight.serviceutils.tools.rpc.FutureRpcResults.LogLevel.NONE; import com.google.common.truth.Truth; import com.google.common.util.concurrent.Futures; @@ -20,8 +20,9 @@ import org.junit.Rule; import org.junit.Test; import org.opendaylight.infrautils.testutils.LogCaptureRule; import org.opendaylight.infrautils.testutils.LogRule; -import org.opendaylight.serviceutils.tools.mdsal.rpc.FutureRpcResults.LogLevel; import org.opendaylight.serviceutils.tools.mdsal.testutils.TestFutureRpcResults; +import org.opendaylight.serviceutils.tools.rpc.FutureRpcResults; +import org.opendaylight.serviceutils.tools.rpc.FutureRpcResults.LogLevel; import org.opendaylight.yangtools.yang.common.RpcResult; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -- 2.36.6