From: Robert Varga Date: Mon, 17 Oct 2022 21:02:00 +0000 (+0200) Subject: Clean up Datastore addressing X-Git-Tag: v11.0.0~94 X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=commitdiff_plain;h=00bbb3ce2797200977ac56e424e09e4be8641ff1;p=mdsal.git Clean up Datastore addressing Rather than using Class files, use explicit well-known constants. Change-Id: I999e3a7e32f7743320da5c3a0b42ec136653255e Signed-off-by: Robert Varga --- diff --git a/binding/mdsal-binding-util/src/main/java/org/opendaylight/mdsal/binding/util/Datastore.java b/binding/mdsal-binding-util/src/main/java/org/opendaylight/mdsal/binding/util/Datastore.java index b95ce5c096..740e267a7c 100644 --- a/binding/mdsal-binding-util/src/main/java/org/opendaylight/mdsal/binding/util/Datastore.java +++ b/binding/mdsal-binding-util/src/main/java/org/opendaylight/mdsal/binding/util/Datastore.java @@ -7,6 +7,8 @@ */ package org.opendaylight.mdsal.binding.util; +import static java.util.Objects.requireNonNull; + import com.google.common.annotations.Beta; import org.opendaylight.mdsal.common.api.LogicalDatastoreType; @@ -16,50 +18,51 @@ import org.opendaylight.mdsal.common.api.LogicalDatastoreType; @Beta // FIXME Base this on ietf-datastores.yang (RFC 8342) public abstract sealed class Datastore { - - /** Class representing the configuration datastore. */ - public static final Class CONFIGURATION = Configuration.class; - - /** Class representing the operational datastore. */ - public static final Class OPERATIONAL = Operational.class; - + /** + * Class representing the configuration datastore. + */ public static final class Configuration extends Datastore { - + private Configuration() { + super(LogicalDatastoreType.CONFIGURATION); + } } + /** + * Class representing the operational datastore. + */ public static final class Operational extends Datastore { - + private Operational() { + super(LogicalDatastoreType.OPERATIONAL); + } } - private Datastore() { - // Hidden on purpose + public static final Operational OPERATIONAL = new Operational(); + public static final Configuration CONFIGURATION = new Configuration(); + + private final LogicalDatastoreType type; + + private Datastore(final LogicalDatastoreType type) { + this.type = requireNonNull(type); } /** - * Returns the logical datastore type corresponding to the given datastore class. + * Returns the logical datastore type corresponding to thisclass. * - * @param datastoreClass The datastore class to convert. * @return The corresponding logical datastore type. - * @throws IllegalArgumentException if the provided datastore class isn’t handled. */ - public static LogicalDatastoreType toType(final Class datastoreClass) { - if (datastoreClass.equals(Configuration.class)) { - return LogicalDatastoreType.CONFIGURATION; - } else if (Operational.class.equals(datastoreClass)) { - return LogicalDatastoreType.OPERATIONAL; - } else { - throw new IllegalArgumentException("Unknown datastore class " + datastoreClass); - } + public LogicalDatastoreType type() { + return type; } /** - * Returns the datastore class corresponding to the given logical datastore type. - * @param datastoreType The logical datastore type to convert. - * @return The corresponding datastore class. - * @throws IllegalArgumentException if the provided logical datastore type isn’t handled. + * Returns the Datastore corresponding to the given logical datastore type. + * + * @param type The logical datastore type to convert. + * @return The corresponding Datastore + * @throws NullPointerException if {@code type} is {@code null} */ - public static Class toClass(final LogicalDatastoreType datastoreType) { - return switch (datastoreType) { + public static Datastore ofType(final LogicalDatastoreType type) { + return switch (type) { case CONFIGURATION -> CONFIGURATION; case OPERATIONAL -> OPERATIONAL; }; diff --git a/binding/mdsal-binding-util/src/main/java/org/opendaylight/mdsal/binding/util/ManagedNewTransactionRunnerImpl.java b/binding/mdsal-binding-util/src/main/java/org/opendaylight/mdsal/binding/util/ManagedNewTransactionRunnerImpl.java index f061976f01..44ccf73603 100644 --- a/binding/mdsal-binding-util/src/main/java/org/opendaylight/mdsal/binding/util/ManagedNewTransactionRunnerImpl.java +++ b/binding/mdsal-binding-util/src/main/java/org/opendaylight/mdsal/binding/util/ManagedNewTransactionRunnerImpl.java @@ -46,9 +46,8 @@ public class ManagedNewTransactionRunnerImpl extends ManagedTransactionFactoryIm @Override @CheckReturnValue public FluentFuture applyWithNewReadWriteTransactionAndSubmit( - final Class datastoreType, - final InterruptibleCheckedFunction, R, E> txFunction) { - return super.applyWithNewTransactionAndSubmit(datastoreType, getTransactionFactory()::newReadWriteTransaction, + final D datastore, final InterruptibleCheckedFunction, R, E> txFunction) { + return super.applyWithNewTransactionAndSubmit(datastore, getTransactionFactory()::newReadWriteTransaction, WriteTrackingTypedReadWriteTransactionImpl::new, txFunction::apply, this::commit); } @@ -74,20 +73,20 @@ public class ManagedNewTransactionRunnerImpl extends ManagedTransactionFactoryIm // This is overridden to use this class’s commit method @Override @CheckReturnValue - public FluentFuture - callWithNewReadWriteTransactionAndSubmit(final Class datastoreType, + public + FluentFuture callWithNewReadWriteTransactionAndSubmit(final D datastore, final InterruptibleCheckedConsumer, E> txConsumer) { - return callWithNewTransactionAndSubmit(datastoreType, getTransactionFactory()::newReadWriteTransaction, + return callWithNewTransactionAndSubmit(datastore, getTransactionFactory()::newReadWriteTransaction, WriteTrackingTypedReadWriteTransactionImpl::new, txConsumer::accept, this::commit); } // This is overridden to use this class’s commit method @Override @CheckReturnValue - public FluentFuture - callWithNewWriteOnlyTransactionAndSubmit(final Class datastoreType, - final InterruptibleCheckedConsumer, E> txConsumer) { - return super.callWithNewTransactionAndSubmit(datastoreType, getTransactionFactory()::newWriteOnlyTransaction, + public + FluentFuture callWithNewWriteOnlyTransactionAndSubmit(final D datastore, + final InterruptibleCheckedConsumer, E> txConsumer) { + return super.callWithNewTransactionAndSubmit(datastore, getTransactionFactory()::newWriteOnlyTransaction, WriteTrackingTypedWriteTransactionImpl::new, txConsumer::accept, this::commit); } diff --git a/binding/mdsal-binding-util/src/main/java/org/opendaylight/mdsal/binding/util/ManagedTransactionFactory.java b/binding/mdsal-binding-util/src/main/java/org/opendaylight/mdsal/binding/util/ManagedTransactionFactory.java index 76e618a72e..7c65397a5a 100644 --- a/binding/mdsal-binding-util/src/main/java/org/opendaylight/mdsal/binding/util/ManagedTransactionFactory.java +++ b/binding/mdsal-binding-util/src/main/java/org/opendaylight/mdsal/binding/util/ManagedTransactionFactory.java @@ -41,15 +41,14 @@ public interface ManagedTransactionFactory { * @param datastore type * @param thrown exception type * @param result type - * @param datastoreType the {@link Datastore} type that will be accessed + * @param datastore the {@link Datastore} type that will be accessed * @param txFunction the {@link InterruptibleCheckedFunction} that needs a new read transaction * @return the result of the function. * @throws E if an error occurs. * @throws InterruptedException if the function is interrupted (this is passed through from the provided function). */ - R applyInterruptiblyWithNewReadOnlyTransactionAndClose( - Class datastoreType, InterruptibleCheckedFunction, R, E> txFunction) - throws E, InterruptedException; + R applyInterruptiblyWithNewReadOnlyTransactionAndClose(D datastore, + InterruptibleCheckedFunction, R, E> txFunction) throws E, InterruptedException; /** * Invokes a function with a NEW {@link TypedReadTransaction}, and ensures that that transaction is closed. @@ -65,12 +64,12 @@ public interface ManagedTransactionFactory { * @param datastore type * @param thrown exception type * @param result type - * @param datastoreType the {@link Datastore} type that will be accessed + * @param datastore the {@link Datastore} type that will be accessed * @param txFunction the {@link InterruptibleCheckedFunction} that needs a new read transaction * @return the result of the function. * @throws E if an error occurs. */ - R applyWithNewReadOnlyTransactionAndClose(Class datastoreType, + R applyWithNewReadOnlyTransactionAndClose(D datastore, CheckedFunction, R, E> txFunction) throws E; /** @@ -97,15 +96,14 @@ public interface ManagedTransactionFactory { * @param datastore type * @param thrown exception type * @param result type - * @param datastoreType the {@link Datastore} type that will be accessed + * @param datastore the {@link Datastore} type that will be accessed * @param txFunction the {@link InterruptibleCheckedFunction} that needs a new read-write transaction * @return the {@link FluentFuture} returned by {@link ReadWriteTransaction#commit()}, or a failed future with an * application specific exception (not from submit()) */ @CheckReturnValue - - FluentFuture applyWithNewReadWriteTransactionAndSubmit(Class datastoreType, - InterruptibleCheckedFunction, R, E> txFunction); + FluentFuture applyWithNewReadWriteTransactionAndSubmit(D datastore, + InterruptibleCheckedFunction, R, E> txFunction); /** * Invokes a function with a NEW {@link ReadTransaction}, and ensures that that transaction is closed. @@ -120,14 +118,13 @@ public interface ManagedTransactionFactory { * * @param datastore type * @param thrown exception type - * @param datastoreType the {@link Datastore} type that will be accessed + * @param datastore the {@link Datastore} type that will be accessed * @param txConsumer the {@link InterruptibleCheckedFunction} that needs a new read transaction * @throws E if an error occurs. * @throws InterruptedException if the function is interrupted (this is passed through from the provided function). */ - void callInterruptiblyWithNewReadOnlyTransactionAndClose( - Class datastoreType, InterruptibleCheckedConsumer, E> txConsumer) - throws E, InterruptedException; + void callInterruptiblyWithNewReadOnlyTransactionAndClose(D datastore, + InterruptibleCheckedConsumer, E> txConsumer) throws E, InterruptedException; /** * Invokes a function with a NEW {@link ReadTransaction}, and ensures that that transaction is closed. @@ -142,11 +139,11 @@ public interface ManagedTransactionFactory { * * @param datastore type * @param thrown exception type - * @param datastoreType the {@link Datastore} type that will be accessed + * @param datastore the {@link Datastore} type that will be accessed * @param txConsumer the {@link InterruptibleCheckedFunction} that needs a new read transaction * @throws E if an error occurs. */ - void callWithNewReadOnlyTransactionAndClose(Class datastoreType, + void callWithNewReadOnlyTransactionAndClose(D datastore, CheckedConsumer, E> txConsumer) throws E; /** @@ -172,15 +169,14 @@ public interface ManagedTransactionFactory { * * @param datastore type * @param thrown exception type - * @param datastoreType the {@link Datastore} type that will be accessed + * @param datastore the {@link Datastore} type that will be accessed * @param txConsumer the {@link InterruptibleCheckedConsumer} that needs a new read-write transaction * @return the {@link FluentFuture} returned by {@link ReadWriteTransaction#commit()}, or a failed future with an * application specific exception (not from submit()) */ @CheckReturnValue - - FluentFuture callWithNewReadWriteTransactionAndSubmit(Class datastoreType, - InterruptibleCheckedConsumer, E> txConsumer); + FluentFuture callWithNewReadWriteTransactionAndSubmit( + D datastore, InterruptibleCheckedConsumer, E> txConsumer); /** * Invokes a consumer with a NEW {@link WriteTransaction}, and then submits that transaction and @@ -192,8 +188,7 @@ public interface ManagedTransactionFactory { * {@link WriteTransaction#cancel()}, or * {@link WriteTransaction#commit()} (it will throw an {@link UnsupportedOperationException}). * - *

The provided transaction is specific to the given logical datastore type and cannot be used for any - * other. + *

The provided transaction is specific to the given logical datastore type and cannot be used for any other. * *

This is an asynchronous API, like {@link DataBroker}'s own; * when returning from this method, the operation of the Transaction may well still be ongoing in the background, @@ -205,13 +200,12 @@ public interface ManagedTransactionFactory { * * @param datastore type * @param thrown exception type - * @param datastoreType the {@link Datastore} type that will be accessed + * @param datastore the {@link Datastore} type that will be accessed * @param txConsumer the {@link InterruptibleCheckedConsumer} that needs a new write only transaction * @return the {@link FluentFuture} returned by {@link WriteTransaction#commit()}, or a failed future with an * application specific exception (not from submit()) */ @CheckReturnValue - - FluentFuture callWithNewWriteOnlyTransactionAndSubmit(Class datastoreType, - InterruptibleCheckedConsumer, E> txConsumer); + FluentFuture callWithNewWriteOnlyTransactionAndSubmit( + D datastore, InterruptibleCheckedConsumer, E> txConsumer); } diff --git a/binding/mdsal-binding-util/src/main/java/org/opendaylight/mdsal/binding/util/ManagedTransactionFactoryImpl.java b/binding/mdsal-binding-util/src/main/java/org/opendaylight/mdsal/binding/util/ManagedTransactionFactoryImpl.java index db610bd0ea..e9a16ed4b4 100644 --- a/binding/mdsal-binding-util/src/main/java/org/opendaylight/mdsal/binding/util/ManagedTransactionFactoryImpl.java +++ b/binding/mdsal-binding-util/src/main/java/org/opendaylight/mdsal/binding/util/ManagedTransactionFactoryImpl.java @@ -37,52 +37,49 @@ class ManagedTransactionFactoryImpl implements Man @Override @SuppressFBWarnings("RCN_REDUNDANT_NULLCHECK_WOULD_HAVE_BEEN_A_NPE") public R applyInterruptiblyWithNewReadOnlyTransactionAndClose( - final Class datastoreType, final InterruptibleCheckedFunction, R, E> txFunction) + final D datastore, final InterruptibleCheckedFunction, R, E> txFunction) throws E, InterruptedException { try (ReadTransaction realTx = transactionFactory.newReadOnlyTransaction()) { - TypedReadTransaction - wrappedTx = new TypedReadTransactionImpl<>(datastoreType, realTx); + TypedReadTransaction wrappedTx = new TypedReadTransactionImpl<>(datastore, realTx); return txFunction.apply(wrappedTx); } } @Override @SuppressFBWarnings("RCN_REDUNDANT_NULLCHECK_WOULD_HAVE_BEEN_A_NPE") - public R applyWithNewReadOnlyTransactionAndClose( - final Class datastoreType, final CheckedFunction, R, E> txFunction) throws E { + public R applyWithNewReadOnlyTransactionAndClose(final D datastore, + final CheckedFunction, R, E> txFunction) throws E { try (ReadTransaction realTx = transactionFactory.newReadOnlyTransaction()) { - TypedReadTransaction - wrappedTx = new TypedReadTransactionImpl<>(datastoreType, realTx); + TypedReadTransaction wrappedTx = new TypedReadTransactionImpl<>(datastore, realTx); return txFunction.apply(wrappedTx); } } @Override @CheckReturnValue - public - FluentFuture applyWithNewReadWriteTransactionAndSubmit(final Class datastoreType, - final InterruptibleCheckedFunction, R, E> txFunction) { - return applyWithNewTransactionAndSubmit(datastoreType, transactionFactory::newReadWriteTransaction, + public FluentFuture applyWithNewReadWriteTransactionAndSubmit( + final D datastore, final InterruptibleCheckedFunction, R, E> txFunction) { + return applyWithNewTransactionAndSubmit(datastore, transactionFactory::newReadWriteTransaction, TypedReadWriteTransactionImpl::new, txFunction, (realTx, wrappedTx) -> realTx.commit()); } @Override @SuppressFBWarnings("RCN_REDUNDANT_NULLCHECK_WOULD_HAVE_BEEN_A_NPE") public void callInterruptiblyWithNewReadOnlyTransactionAndClose( - final Class datastoreType, final InterruptibleCheckedConsumer, E> txConsumer) + final D datastore, final InterruptibleCheckedConsumer, E> txConsumer) throws E, InterruptedException { try (ReadTransaction realTx = transactionFactory.newReadOnlyTransaction()) { - TypedReadTransaction wrappedTx = new TypedReadTransactionImpl<>(datastoreType, realTx); + TypedReadTransaction wrappedTx = new TypedReadTransactionImpl<>(datastore, realTx); txConsumer.accept(wrappedTx); } } @Override @SuppressFBWarnings("RCN_REDUNDANT_NULLCHECK_WOULD_HAVE_BEEN_A_NPE") - public void callWithNewReadOnlyTransactionAndClose( - final Class datastoreType, final CheckedConsumer, E> txConsumer) throws E { + public void callWithNewReadOnlyTransactionAndClose(final D datastore, + final CheckedConsumer, E> txConsumer) throws E { try (ReadTransaction realTx = transactionFactory.newReadOnlyTransaction()) { - TypedReadTransaction wrappedTx = new TypedReadTransactionImpl<>(datastoreType, realTx); + TypedReadTransaction wrappedTx = new TypedReadTransactionImpl<>(datastore, realTx); txConsumer.accept(wrappedTx); } } @@ -90,27 +87,27 @@ class ManagedTransactionFactoryImpl implements Man @Override @CheckReturnValue public - FluentFuture callWithNewReadWriteTransactionAndSubmit(final Class datastoreType, + FluentFuture callWithNewReadWriteTransactionAndSubmit(final D datastore, final InterruptibleCheckedConsumer, E> txConsumer) { - return callWithNewTransactionAndSubmit(datastoreType, transactionFactory::newReadWriteTransaction, + return callWithNewTransactionAndSubmit(datastore, transactionFactory::newReadWriteTransaction, TypedReadWriteTransactionImpl::new, txConsumer, (realTx, wrappedTx) -> realTx.commit()); } @Override @CheckReturnValue - public FluentFuture - callWithNewWriteOnlyTransactionAndSubmit(final Class datastoreType, - final InterruptibleCheckedConsumer, E> txConsumer) { - return callWithNewTransactionAndSubmit(datastoreType, transactionFactory::newWriteOnlyTransaction, + public + FluentFuture callWithNewWriteOnlyTransactionAndSubmit(final D datastore, + final InterruptibleCheckedConsumer, E> txConsumer) { + return callWithNewTransactionAndSubmit(datastore, transactionFactory::newWriteOnlyTransaction, TypedWriteTransactionImpl::new, txConsumer, (realTx, wrappedTx) -> realTx.commit()); } @CheckReturnValue - protected FluentFuture - callWithNewTransactionAndSubmit( - final Class datastoreType, final Supplier txSupplier, final BiFunction, X, W> txWrapper, - final InterruptibleCheckedConsumer txConsumer, final BiFunction> txSubmitter) { - return applyWithNewTransactionAndSubmit(datastoreType, txSupplier, txWrapper, tx -> { + protected + FluentFuture callWithNewTransactionAndSubmit(final D datastore, final Supplier txSupplier, + final BiFunction txWrapper, final InterruptibleCheckedConsumer txConsumer, + final BiFunction> txSubmitter) { + return applyWithNewTransactionAndSubmit(datastore, txSupplier, txWrapper, tx -> { txConsumer.accept(tx); return null; }, txSubmitter); @@ -118,13 +115,12 @@ class ManagedTransactionFactoryImpl implements Man @CheckReturnValue @SuppressWarnings("checkstyle:IllegalCatch") - protected FluentFuture - applyWithNewTransactionAndSubmit( - final Class datastoreType, final Supplier txSupplier, final BiFunction, X, W> txWrapper, - final InterruptibleCheckedFunction txFunction, + protected + FluentFuture applyWithNewTransactionAndSubmit(final D datastore, final Supplier txSupplier, + final BiFunction txWrapper, final InterruptibleCheckedFunction txFunction, final BiFunction> txSubmitter) { X realTx = txSupplier.get(); - W wrappedTx = txWrapper.apply(datastoreType, realTx); + W wrappedTx = txWrapper.apply(datastore, realTx); R result; try { // We must store the result before submitting the transaction; if we inline the next line in the diff --git a/binding/mdsal-binding-util/src/main/java/org/opendaylight/mdsal/binding/util/RetryingManagedNewTransactionRunnerImpl.java b/binding/mdsal-binding-util/src/main/java/org/opendaylight/mdsal/binding/util/RetryingManagedNewTransactionRunnerImpl.java index 9146a2ce9d..b750a81f46 100644 --- a/binding/mdsal-binding-util/src/main/java/org/opendaylight/mdsal/binding/util/RetryingManagedNewTransactionRunnerImpl.java +++ b/binding/mdsal-binding-util/src/main/java/org/opendaylight/mdsal/binding/util/RetryingManagedNewTransactionRunnerImpl.java @@ -40,15 +40,16 @@ class RetryingManagedNewTransactionRunnerImpl implements ManagedNewTransactionRu private final Executor executor; - RetryingManagedNewTransactionRunnerImpl(ManagedNewTransactionRunner delegate) { + RetryingManagedNewTransactionRunnerImpl(final ManagedNewTransactionRunner delegate) { this(delegate, MoreExecutors.directExecutor(), DEFAULT_RETRIES); } - RetryingManagedNewTransactionRunnerImpl(ManagedNewTransactionRunner delegate, int maxRetries) { + RetryingManagedNewTransactionRunnerImpl(final ManagedNewTransactionRunner delegate, final int maxRetries) { this(delegate, MoreExecutors.directExecutor(), maxRetries); } - RetryingManagedNewTransactionRunnerImpl(ManagedNewTransactionRunner delegate, Executor executor, int maxRetries) { + RetryingManagedNewTransactionRunnerImpl(final ManagedNewTransactionRunner delegate, final Executor executor, + final int maxRetries) { this.delegate = requireNonNull(delegate, "delegate must not be null"); this.executor = requireNonNull(executor, "executor must not be null"); this.maxRetries = maxRetries; @@ -56,20 +57,20 @@ class RetryingManagedNewTransactionRunnerImpl implements ManagedNewTransactionRu @Override public R applyInterruptiblyWithNewReadOnlyTransactionAndClose( - Class datastoreType, InterruptibleCheckedFunction, R, E> txFunction) + final D datastore, final InterruptibleCheckedFunction, R, E> txFunction) throws E, InterruptedException { - return applyInterruptiblyWithNewReadOnlyTransactionAndClose(datastoreType, txFunction, maxRetries); + return applyInterruptiblyWithNewReadOnlyTransactionAndClose(datastore, txFunction, maxRetries); } @SuppressWarnings("checkstyle:IllegalCatch") private R applyInterruptiblyWithNewReadOnlyTransactionAndClose( - Class datastoreType, InterruptibleCheckedFunction, R, E> txFunction, - int tries) throws E, InterruptedException { + final D datastore, final InterruptibleCheckedFunction, R, E> txFunction, + final int tries) throws E, InterruptedException { try { - return delegate.applyInterruptiblyWithNewReadOnlyTransactionAndClose(datastoreType, txFunction); + return delegate.applyInterruptiblyWithNewReadOnlyTransactionAndClose(datastore, txFunction); } catch (Exception e) { if (isRetriableException(e) && tries - 1 > 0) { - return applyInterruptiblyWithNewReadOnlyTransactionAndClose(datastoreType, txFunction, tries - 1); + return applyInterruptiblyWithNewReadOnlyTransactionAndClose(datastore, txFunction, tries - 1); } else { throw e; } @@ -77,19 +78,19 @@ class RetryingManagedNewTransactionRunnerImpl implements ManagedNewTransactionRu } @Override - public R applyWithNewReadOnlyTransactionAndClose( - Class datastoreType, CheckedFunction, R, E> txFunction) throws E { - return applyWithNewReadOnlyTransactionAndClose(datastoreType, txFunction, maxRetries); + public R applyWithNewReadOnlyTransactionAndClose(final D datastore, + final CheckedFunction, R, E> txFunction) throws E { + return applyWithNewReadOnlyTransactionAndClose(datastore, txFunction, maxRetries); } @SuppressWarnings("checkstyle:IllegalCatch") - private R applyWithNewReadOnlyTransactionAndClose( - Class datastoreType, CheckedFunction, R, E> txFunction, int tries) throws E { + private R applyWithNewReadOnlyTransactionAndClose(final D datastore, + final CheckedFunction, R, E> txFunction, final int tries) throws E { try { - return delegate.applyWithNewReadOnlyTransactionAndClose(datastoreType, txFunction); + return delegate.applyWithNewReadOnlyTransactionAndClose(datastore, txFunction); } catch (Exception e) { if (isRetriableException(e) && tries - 1 > 0) { - return applyWithNewReadOnlyTransactionAndClose(datastoreType, txFunction, tries - 1); + return applyWithNewReadOnlyTransactionAndClose(datastore, txFunction, tries - 1); } else { throw e; } @@ -98,19 +99,19 @@ class RetryingManagedNewTransactionRunnerImpl implements ManagedNewTransactionRu @Override public FluentFuture applyWithNewReadWriteTransactionAndSubmit( - Class datastoreType, InterruptibleCheckedFunction, R, E> txFunction) { - return applyWithNewReadWriteTransactionAndSubmit(datastoreType, txFunction, maxRetries); + final D datastore, final InterruptibleCheckedFunction, R, E> txFunction) { + return applyWithNewReadWriteTransactionAndSubmit(datastore, txFunction, maxRetries); } private FluentFuture applyWithNewReadWriteTransactionAndSubmit( - Class datastoreType, InterruptibleCheckedFunction, R, E> txRunner, - int tries) { + final D datastore, final InterruptibleCheckedFunction, R, E> txRunner, + final int tries) { FluentFuture future = requireNonNull( - delegate.applyWithNewReadWriteTransactionAndSubmit(datastoreType, txRunner), + delegate.applyWithNewReadWriteTransactionAndSubmit(datastore, txRunner), "delegate.callWithNewReadWriteTransactionAndSubmit() == null"); return future.catchingAsync(Exception.class, exception -> { if (isRetriableException(exception) && tries - 1 > 0) { - return applyWithNewReadWriteTransactionAndSubmit(datastoreType, txRunner, tries - 1); + return applyWithNewReadWriteTransactionAndSubmit(datastore, txRunner, tries - 1); } else { throw exception; } @@ -118,26 +119,26 @@ class RetryingManagedNewTransactionRunnerImpl implements ManagedNewTransactionRu } @Override - public R applyWithNewTransactionChainAndClose(Function chainConsumer) { + public R applyWithNewTransactionChainAndClose(final Function chainConsumer) { throw new UnsupportedOperationException("The retrying transaction manager doesn't support transaction chains"); } @Override public void callInterruptiblyWithNewReadOnlyTransactionAndClose( - Class datastoreType, InterruptibleCheckedConsumer, E> txConsumer) + final D datastore, final InterruptibleCheckedConsumer, E> txConsumer) throws E, InterruptedException { - callInterruptiblyWithNewReadOnlyTransactionAndClose(datastoreType, txConsumer, maxRetries); + callInterruptiblyWithNewReadOnlyTransactionAndClose(datastore, txConsumer, maxRetries); } @SuppressWarnings("checkstyle:IllegalCatch") private void callInterruptiblyWithNewReadOnlyTransactionAndClose( - Class datastoreType, InterruptibleCheckedConsumer, E> txConsumer, int tries) - throws E, InterruptedException { + final D datastore, final InterruptibleCheckedConsumer, E> txConsumer, + final int tries) throws E, InterruptedException { try { - delegate.callInterruptiblyWithNewReadOnlyTransactionAndClose(datastoreType, txConsumer); + delegate.callInterruptiblyWithNewReadOnlyTransactionAndClose(datastore, txConsumer); } catch (Exception e) { if (isRetriableException(e) && tries - 1 > 0) { - callInterruptiblyWithNewReadOnlyTransactionAndClose(datastoreType, txConsumer, tries - 1); + callInterruptiblyWithNewReadOnlyTransactionAndClose(datastore, txConsumer, tries - 1); } else { throw e; } @@ -145,19 +146,19 @@ class RetryingManagedNewTransactionRunnerImpl implements ManagedNewTransactionRu } @Override - public void callWithNewReadOnlyTransactionAndClose( - Class datastoreType, CheckedConsumer, E> txConsumer) throws E { - callWithNewReadOnlyTransactionAndClose(datastoreType, txConsumer, maxRetries); + public void callWithNewReadOnlyTransactionAndClose(final D datastore, + final CheckedConsumer, E> txConsumer) throws E { + callWithNewReadOnlyTransactionAndClose(datastore, txConsumer, maxRetries); } @SuppressWarnings("checkstyle:IllegalCatch") - private void callWithNewReadOnlyTransactionAndClose( - Class datastoreType, CheckedConsumer, E> txConsumer, int tries) throws E { + private void callWithNewReadOnlyTransactionAndClose(final D datastore, + final CheckedConsumer, E> txConsumer, final int tries) throws E { try { - delegate.callWithNewReadOnlyTransactionAndClose(datastoreType, txConsumer); + delegate.callWithNewReadOnlyTransactionAndClose(datastore, txConsumer); } catch (Exception e) { if (isRetriableException(e) && tries - 1 > 0) { - callWithNewReadOnlyTransactionAndClose(datastoreType, txConsumer, tries - 1); + callWithNewReadOnlyTransactionAndClose(datastore, txConsumer, tries - 1); } else { throw e; } @@ -165,23 +166,22 @@ class RetryingManagedNewTransactionRunnerImpl implements ManagedNewTransactionRu } @Override - public FluentFuture - callWithNewReadWriteTransactionAndSubmit( - Class datastoreType, InterruptibleCheckedConsumer, E> txConsumer) { - return callWithNewReadWriteTransactionAndSubmit(datastoreType, txConsumer, maxRetries); + public + FluentFuture callWithNewReadWriteTransactionAndSubmit(final D datastore, + final InterruptibleCheckedConsumer, E> txConsumer) { + return callWithNewReadWriteTransactionAndSubmit(datastore, txConsumer, maxRetries); } - private FluentFuture - callWithNewReadWriteTransactionAndSubmit(Class datastoreType, - InterruptibleCheckedConsumer, E> txRunner, int tries) { - + private FluentFuture callWithNewReadWriteTransactionAndSubmit( + final D datastore, final InterruptibleCheckedConsumer, E> txRunner, + final int tries) { return (FluentFuture) requireNonNull( - delegate.callWithNewReadWriteTransactionAndSubmit(datastoreType, txRunner), + delegate.callWithNewReadWriteTransactionAndSubmit(datastore, txRunner), "delegate.callWithNewWriteOnlyTransactionAndSubmit() == null") .catchingAsync(Exception.class, exception -> { // as per AsyncWriteTransaction.submit()'s JavaDoc re. retries if (isRetriableException(exception) && tries - 1 > 0) { - return callWithNewReadWriteTransactionAndSubmit(datastoreType, txRunner, tries - 1); + return callWithNewReadWriteTransactionAndSubmit(datastore, txRunner, tries - 1); } else { // out of retries, so propagate the exception throw exception; @@ -190,23 +190,22 @@ class RetryingManagedNewTransactionRunnerImpl implements ManagedNewTransactionRu } @Override - public FluentFuture - callWithNewWriteOnlyTransactionAndSubmit(Class datastoreType, - InterruptibleCheckedConsumer, E> txConsumer) { - return callWithNewWriteOnlyTransactionAndSubmit(datastoreType, txConsumer, maxRetries); + public + FluentFuture callWithNewWriteOnlyTransactionAndSubmit(final D datastore, + final InterruptibleCheckedConsumer, E> txConsumer) { + return callWithNewWriteOnlyTransactionAndSubmit(datastore, txConsumer, maxRetries); } - private FluentFuture - callWithNewWriteOnlyTransactionAndSubmit(Class datastoreType, - InterruptibleCheckedConsumer, E> txRunner, int tries) { - + private FluentFuture callWithNewWriteOnlyTransactionAndSubmit( + final D datastore, final InterruptibleCheckedConsumer, E> txRunner, + final int tries) { return (FluentFuture) requireNonNull( - delegate.callWithNewWriteOnlyTransactionAndSubmit(datastoreType, txRunner), + delegate.callWithNewWriteOnlyTransactionAndSubmit(datastore, txRunner), "delegate.callWithNewWriteOnlyTransactionAndSubmit() == null") .catchingAsync(OptimisticLockFailedException.class, optimisticLockFailedException -> { // as per AsyncWriteTransaction.submit()'s JavaDoc re. retries if (tries - 1 > 0) { - return callWithNewWriteOnlyTransactionAndSubmit(datastoreType, txRunner, tries - 1); + return callWithNewWriteOnlyTransactionAndSubmit(datastore, txRunner, tries - 1); } else { // out of retries, so propagate the OptimisticLockFailedException throw optimisticLockFailedException; @@ -214,8 +213,8 @@ class RetryingManagedNewTransactionRunnerImpl implements ManagedNewTransactionRu }, executor); } - private boolean isRetriableException(Throwable throwable) { - return throwable instanceof OptimisticLockFailedException || throwable instanceof ReadFailedException || ( - throwable instanceof ExecutionException && isRetriableException(throwable.getCause())); + private boolean isRetriableException(final Throwable throwable) { + return throwable instanceof OptimisticLockFailedException || throwable instanceof ReadFailedException + || throwable instanceof ExecutionException && isRetriableException(throwable.getCause()); } } diff --git a/binding/mdsal-binding-util/src/main/java/org/opendaylight/mdsal/binding/util/TypedReadTransactionImpl.java b/binding/mdsal-binding-util/src/main/java/org/opendaylight/mdsal/binding/util/TypedReadTransactionImpl.java index 9a440ddf88..c1de38c640 100644 --- a/binding/mdsal-binding-util/src/main/java/org/opendaylight/mdsal/binding/util/TypedReadTransactionImpl.java +++ b/binding/mdsal-binding-util/src/main/java/org/opendaylight/mdsal/binding/util/TypedReadTransactionImpl.java @@ -22,8 +22,8 @@ import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; */ final class TypedReadTransactionImpl extends TypedTransaction implements TypedReadTransaction { - TypedReadTransactionImpl(final Class datastoreType, final ReadTransaction realTx) { - super(datastoreType, realTx); + TypedReadTransactionImpl(final D datastore, final ReadTransaction realTx) { + super(datastore, realTx); } @Override diff --git a/binding/mdsal-binding-util/src/main/java/org/opendaylight/mdsal/binding/util/TypedReadWriteTransactionImpl.java b/binding/mdsal-binding-util/src/main/java/org/opendaylight/mdsal/binding/util/TypedReadWriteTransactionImpl.java index 39bd29414b..6ceac314d9 100644 --- a/binding/mdsal-binding-util/src/main/java/org/opendaylight/mdsal/binding/util/TypedReadWriteTransactionImpl.java +++ b/binding/mdsal-binding-util/src/main/java/org/opendaylight/mdsal/binding/util/TypedReadWriteTransactionImpl.java @@ -23,8 +23,8 @@ import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; class TypedReadWriteTransactionImpl extends TypedWriteTransactionImpl implements TypedReadWriteTransaction { - TypedReadWriteTransactionImpl(final Class datastoreType, final ReadWriteTransaction realTx) { - super(datastoreType, realTx); + TypedReadWriteTransactionImpl(final D datastore, final ReadWriteTransaction realTx) { + super(datastore, realTx); } @Override diff --git a/binding/mdsal-binding-util/src/main/java/org/opendaylight/mdsal/binding/util/TypedTransaction.java b/binding/mdsal-binding-util/src/main/java/org/opendaylight/mdsal/binding/util/TypedTransaction.java index 3a0a5d6c70..1496958e78 100644 --- a/binding/mdsal-binding-util/src/main/java/org/opendaylight/mdsal/binding/util/TypedTransaction.java +++ b/binding/mdsal-binding-util/src/main/java/org/opendaylight/mdsal/binding/util/TypedTransaction.java @@ -20,8 +20,8 @@ abstract class TypedTransaction exte private final LogicalDatastoreType datastoreType; private final X delegate; - TypedTransaction(final Class datastoreType, final X delegate) { - this.datastoreType = Datastore.toType(datastoreType); + TypedTransaction(final D datastore, final X delegate) { + this.datastoreType = datastore.type(); this.delegate = delegate; } @@ -35,8 +35,8 @@ abstract class TypedTransaction exte } final FluentFuture> doExecute(final QueryExpression query) { - if (delegate instanceof QueryOperations) { - return ((QueryOperations) delegate).execute(datastoreType, query); + if (delegate instanceof QueryOperations queryOps) { + return queryOps.execute(datastoreType, query); } throw new UnsupportedOperationException("Query execution requires backend support"); } diff --git a/binding/mdsal-binding-util/src/main/java/org/opendaylight/mdsal/binding/util/TypedWriteTransactionImpl.java b/binding/mdsal-binding-util/src/main/java/org/opendaylight/mdsal/binding/util/TypedWriteTransactionImpl.java index 77a95088fb..f5fa3e0700 100644 --- a/binding/mdsal-binding-util/src/main/java/org/opendaylight/mdsal/binding/util/TypedWriteTransactionImpl.java +++ b/binding/mdsal-binding-util/src/main/java/org/opendaylight/mdsal/binding/util/TypedWriteTransactionImpl.java @@ -19,8 +19,8 @@ import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; */ class TypedWriteTransactionImpl extends TypedTransaction implements TypedWriteTransaction { - TypedWriteTransactionImpl(final Class datastoreType, final X realTx) { - super(datastoreType, realTx); + TypedWriteTransactionImpl(final D datastore, final X realTx) { + super(datastore, realTx); } @Override diff --git a/binding/mdsal-binding-util/src/main/java/org/opendaylight/mdsal/binding/util/WriteTrackingTypedReadWriteTransactionImpl.java b/binding/mdsal-binding-util/src/main/java/org/opendaylight/mdsal/binding/util/WriteTrackingTypedReadWriteTransactionImpl.java index 2625133944..cb5e427027 100644 --- a/binding/mdsal-binding-util/src/main/java/org/opendaylight/mdsal/binding/util/WriteTrackingTypedReadWriteTransactionImpl.java +++ b/binding/mdsal-binding-util/src/main/java/org/opendaylight/mdsal/binding/util/WriteTrackingTypedReadWriteTransactionImpl.java @@ -19,8 +19,8 @@ final class WriteTrackingTypedReadWriteTransactionImpl exte // but the cost here is tiny (one read penalty at the end of a transaction) so we play it safe private volatile boolean written; - WriteTrackingTypedReadWriteTransactionImpl(final Class datastoreType, final ReadWriteTransaction realTx) { - super(datastoreType, realTx); + WriteTrackingTypedReadWriteTransactionImpl(final D datastore, final ReadWriteTransaction realTx) { + super(datastore, realTx); } @Override diff --git a/binding/mdsal-binding-util/src/main/java/org/opendaylight/mdsal/binding/util/WriteTrackingTypedWriteTransactionImpl.java b/binding/mdsal-binding-util/src/main/java/org/opendaylight/mdsal/binding/util/WriteTrackingTypedWriteTransactionImpl.java index d1ce3fee77..2141871da6 100644 --- a/binding/mdsal-binding-util/src/main/java/org/opendaylight/mdsal/binding/util/WriteTrackingTypedWriteTransactionImpl.java +++ b/binding/mdsal-binding-util/src/main/java/org/opendaylight/mdsal/binding/util/WriteTrackingTypedWriteTransactionImpl.java @@ -19,8 +19,8 @@ final class WriteTrackingTypedWriteTransactionImpl // but the cost here is tiny (one read penalty at the end of a transaction) so we play it safe private volatile boolean written; - WriteTrackingTypedWriteTransactionImpl(final Class datastoreType, final WriteTransaction realTx) { - super(datastoreType, realTx); + WriteTrackingTypedWriteTransactionImpl(final D datastore, final WriteTransaction realTx) { + super(datastore, realTx); } @Override diff --git a/binding/mdsal-binding-util/src/test/java/org/opendaylight/mdsal/binding/util/DatastoreTest.java b/binding/mdsal-binding-util/src/test/java/org/opendaylight/mdsal/binding/util/DatastoreTest.java index 1cd953bc30..5d96945b6c 100644 --- a/binding/mdsal-binding-util/src/test/java/org/opendaylight/mdsal/binding/util/DatastoreTest.java +++ b/binding/mdsal-binding-util/src/test/java/org/opendaylight/mdsal/binding/util/DatastoreTest.java @@ -16,15 +16,14 @@ import org.opendaylight.mdsal.common.api.LogicalDatastoreType; public class DatastoreTest { @Test public void testDatastoreToType() { - assertEquals(LogicalDatastoreType.CONFIGURATION, Datastore.toType(Datastore.CONFIGURATION)); - assertEquals(LogicalDatastoreType.OPERATIONAL, Datastore.toType(Datastore.OPERATIONAL)); - assertThrows(NullPointerException.class, () -> Datastore.toType(null)); + assertEquals(LogicalDatastoreType.CONFIGURATION, Datastore.CONFIGURATION.type()); + assertEquals(LogicalDatastoreType.OPERATIONAL, Datastore.OPERATIONAL.type()); } @Test public void testDatastoreToClass() { - assertEquals(Datastore.CONFIGURATION, Datastore.toClass(LogicalDatastoreType.CONFIGURATION)); - assertEquals(Datastore.OPERATIONAL, Datastore.toClass(LogicalDatastoreType.OPERATIONAL)); - assertThrows(NullPointerException.class, () -> Datastore.toClass(null)); + assertEquals(Datastore.CONFIGURATION, Datastore.ofType(LogicalDatastoreType.CONFIGURATION)); + assertEquals(Datastore.OPERATIONAL, Datastore.ofType(LogicalDatastoreType.OPERATIONAL)); + assertThrows(NullPointerException.class, () -> Datastore.ofType(null)); } }