Refactor to fix unchecked cast warnings. 66/28166/3
authorGary Wu <Gary.Wu1@huawei.com>
Thu, 8 Oct 2015 19:30:58 +0000 (12:30 -0700)
committerGerrit Code Review <gerrit@opendaylight.org>
Fri, 9 Oct 2015 00:14:29 +0000 (00:14 +0000)
Change-Id: I0fb6ce59707000f225ffa8d654685fbc89f8f2eb
Signed-off-by: Gary Wu <Gary.Wu1@huawei.com>
opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/databroker/AbstractDOMBrokerTransaction.java
opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/databroker/AbstractDOMBrokerWriteTransaction.java [new file with mode: 0644]
opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/databroker/AbstractDOMTransactionFactory.java
opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/databroker/DOMBrokerReadOnlyTransaction.java
opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/databroker/DOMBrokerReadWriteTransaction.java
opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/databroker/DOMBrokerWriteOnlyTransaction.java

index 98fea88..0570e72 100644 (file)
@@ -19,10 +19,10 @@ import org.opendaylight.controller.sal.core.spi.data.DOMStoreTransactionFactory;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
 
-public abstract class AbstractDOMBrokerTransaction<K, T extends DOMStoreTransaction> implements
+public abstract class AbstractDOMBrokerTransaction<T extends DOMStoreTransaction> implements
         AsyncTransaction<YangInstanceIdentifier, NormalizedNode<?, ?>> {
 
-    private Map<K, T> backingTxs;
+    private EnumMap<LogicalDatastoreType, T> backingTxs;
     private final Object identifier;
     private final Map<LogicalDatastoreType, ? extends DOMStoreTransactionFactory> storeTxFactories;
 
@@ -36,7 +36,7 @@ public abstract class AbstractDOMBrokerTransaction<K, T extends DOMStoreTransact
     protected AbstractDOMBrokerTransaction(final Object identifier, Map<LogicalDatastoreType, ? extends DOMStoreTransactionFactory> storeTxFactories) {
         this.identifier = Preconditions.checkNotNull(identifier, "Identifier should not be null");
         this.storeTxFactories = Preconditions.checkNotNull(storeTxFactories, "Store Transaction Factories should not be null");
-        this.backingTxs = new EnumMap(LogicalDatastoreType.class);
+        this.backingTxs = new EnumMap<>(LogicalDatastoreType.class);
     }
 
     /**
@@ -49,7 +49,7 @@ public abstract class AbstractDOMBrokerTransaction<K, T extends DOMStoreTransact
      * @throws IllegalArgumentException
      *             if no subtransaction is associated with key.
      */
-    protected final T getSubtransaction(final K key) {
+    protected final T getSubtransaction(final LogicalDatastoreType key) {
         Preconditions.checkNotNull(key, "key must not be null.");
 
         T ret = backingTxs.get(key);
@@ -61,7 +61,7 @@ public abstract class AbstractDOMBrokerTransaction<K, T extends DOMStoreTransact
         return ret;
     }
 
-    protected abstract T createTransaction(final K key);
+    protected abstract T createTransaction(final LogicalDatastoreType key);
 
     /**
      * Returns immutable Iterable of all subtransactions.
@@ -101,7 +101,7 @@ public abstract class AbstractDOMBrokerTransaction<K, T extends DOMStoreTransact
         }
     }
 
-    protected DOMStoreTransactionFactory getTxFactory(K type){
+    protected DOMStoreTransactionFactory getTxFactory(LogicalDatastoreType type){
         return storeTxFactories.get(type);
     }
 }
diff --git a/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/databroker/AbstractDOMBrokerWriteTransaction.java b/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/databroker/AbstractDOMBrokerWriteTransaction.java
new file mode 100644 (file)
index 0000000..500ccdf
--- /dev/null
@@ -0,0 +1,146 @@
+/*
+ * Copyright (c) 2015 Cisco Systems, 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.controller.cluster.databroker;
+
+import com.google.common.base.Preconditions;
+import com.google.common.util.concurrent.CheckedFuture;
+import com.google.common.util.concurrent.Futures;
+import com.google.common.util.concurrent.ListenableFuture;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Map;
+import java.util.concurrent.Future;
+import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
+import org.opendaylight.controller.md.sal.common.api.TransactionStatus;
+import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
+import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
+import org.opendaylight.controller.md.sal.common.impl.service.AbstractDataTransaction;
+import org.opendaylight.controller.md.sal.dom.api.DOMDataWriteTransaction;
+import org.opendaylight.controller.sal.core.spi.data.DOMStoreThreePhaseCommitCohort;
+import org.opendaylight.controller.sal.core.spi.data.DOMStoreTransactionFactory;
+import org.opendaylight.controller.sal.core.spi.data.DOMStoreWriteTransaction;
+import org.opendaylight.yangtools.yang.common.RpcResult;
+import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
+import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
+import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public abstract class AbstractDOMBrokerWriteTransaction<T extends DOMStoreWriteTransaction>
+        extends AbstractDOMBrokerTransaction<T> implements DOMDataWriteTransaction {
+
+    private static final AtomicReferenceFieldUpdater<AbstractDOMBrokerWriteTransaction, AbstractDOMTransactionFactory> IMPL_UPDATER =
+            AtomicReferenceFieldUpdater.newUpdater(AbstractDOMBrokerWriteTransaction.class, AbstractDOMTransactionFactory.class, "commitImpl");
+    private static final AtomicReferenceFieldUpdater<AbstractDOMBrokerWriteTransaction, Future> FUTURE_UPDATER =
+            AtomicReferenceFieldUpdater.newUpdater(AbstractDOMBrokerWriteTransaction.class, Future.class, "commitFuture");
+    private static final Logger LOG = LoggerFactory.getLogger(AbstractDOMBrokerWriteTransaction.class);
+    private static final Future<?> CANCELLED_FUTURE = Futures.immediateCancelledFuture();
+
+    /**
+     * Implementation of real commit. It also acts as an indication that
+     * the transaction is running -- which we flip atomically using
+     * {@link #IMPL_UPDATER}.
+     */
+    private volatile AbstractDOMTransactionFactory<?> commitImpl;
+
+    /**
+     * Future task of transaction commit. It starts off as null, but is
+     * set appropriately on {@link #submit()} and {@link #cancel()} via
+     * {@link AtomicReferenceFieldUpdater#lazySet(Object, Object)}.
+     *
+     * Lazy set is safe for use because it is only referenced to in the
+     * {@link #cancel()} slow path, where we will busy-wait for it. The
+     * fast path gets the benefit of a store-store barrier instead of the
+     * usual store-load barrier.
+     */
+    private volatile Future<?> commitFuture;
+
+    protected AbstractDOMBrokerWriteTransaction(final Object identifier,
+                                            final Map<LogicalDatastoreType, ? extends DOMStoreTransactionFactory> storeTxFactories, final AbstractDOMTransactionFactory<?> commitImpl) {
+        super(identifier, storeTxFactories);
+        this.commitImpl = Preconditions.checkNotNull(commitImpl, "commitImpl must not be null.");
+    }
+
+    @Override
+    public void put(final LogicalDatastoreType store, final YangInstanceIdentifier path, final NormalizedNode<?, ?> data) {
+        checkRunning(commitImpl);
+        checkInstanceIdentifierReferencesData(path,data);
+        getSubtransaction(store).write(path, data);
+    }
+
+    private static void checkInstanceIdentifierReferencesData(final YangInstanceIdentifier path,
+            final NormalizedNode<?, ?> data) {
+        final PathArgument lastArg = path.getLastPathArgument();
+        Preconditions.checkArgument(
+                (lastArg == data.getIdentifier()) || (lastArg != null && lastArg.equals(data.getIdentifier())),
+                "Instance identifier references %s but data identifier is %s", lastArg, data);
+    }
+
+    @Override
+    public void delete(final LogicalDatastoreType store, final YangInstanceIdentifier path) {
+        checkRunning(commitImpl);
+        getSubtransaction(store).delete(path);
+    }
+
+    @Override
+    public void merge(final LogicalDatastoreType store, final YangInstanceIdentifier path, final NormalizedNode<?, ?> data) {
+        checkRunning(commitImpl);
+        checkInstanceIdentifierReferencesData(path, data);
+        getSubtransaction(store).merge(path, data);
+    }
+
+    @Override
+    public boolean cancel() {
+        final AbstractDOMTransactionFactory<?> impl = IMPL_UPDATER.getAndSet(this, null);
+        if (impl != null) {
+            LOG.trace("Transaction {} cancelled before submit", getIdentifier());
+            FUTURE_UPDATER.lazySet(this, CANCELLED_FUTURE);
+            closeSubtransactions();
+            return true;
+        }
+
+        // The transaction is in process of being submitted or cancelled. Busy-wait
+        // for the corresponding future.
+        Future<?> future;
+        do {
+            future = commitFuture;
+        } while (future == null);
+
+        return future.cancel(false);
+    }
+
+    @Deprecated
+    @Override
+    public ListenableFuture<RpcResult<TransactionStatus>> commit() {
+        return AbstractDataTransaction.convertToLegacyCommitFuture(submit());
+    }
+
+    @Override
+    public CheckedFuture<Void, TransactionCommitFailedException> submit() {
+        final AbstractDOMTransactionFactory<?> impl = IMPL_UPDATER.getAndSet(this, null);
+        checkRunning(impl);
+
+        final Collection<T> txns = getSubtransactions();
+        final Collection<DOMStoreThreePhaseCommitCohort> cohorts = new ArrayList<>(txns.size());
+
+        // FIXME: deal with errors thrown by backed (ready and submit can fail in theory)
+        for (final T txn : txns) {
+            cohorts.add(txn.ready());
+        }
+
+        final CheckedFuture<Void, TransactionCommitFailedException> ret = impl.submit(this, cohorts);
+        FUTURE_UPDATER.lazySet(this, ret);
+        return ret;
+    }
+
+    private void checkRunning(final AbstractDOMTransactionFactory<?> impl) {
+        Preconditions.checkState(impl != null, "Transaction %s is no longer running", getIdentifier());
+    }
+
+}
index 2187c6e..b9c6a2a 100644 (file)
@@ -78,7 +78,7 @@ public abstract class AbstractDOMTransactionFactory<T extends DOMStoreTransactio
     public final DOMDataReadWriteTransaction newReadWriteTransaction() {
         checkNotClosed();
 
-        return new DOMBrokerReadWriteTransaction<>(newTransactionIdentifier(), storeTxFactories, this);
+        return new DOMBrokerReadWriteTransaction(newTransactionIdentifier(), storeTxFactories, this);
     }
 
     /**
index 656ced3..64d79cb 100644 (file)
@@ -15,18 +15,19 @@ import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
 import org.opendaylight.controller.md.sal.dom.api.DOMDataReadOnlyTransaction;
 import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadTransaction;
+import org.opendaylight.controller.sal.core.spi.data.DOMStoreTransactionFactory;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
 
-public class DOMBrokerReadOnlyTransaction<T extends DOMStoreReadTransaction>
-    extends AbstractDOMBrokerTransaction<LogicalDatastoreType, T>
+public class DOMBrokerReadOnlyTransaction
+    extends AbstractDOMBrokerTransaction<DOMStoreReadTransaction>
         implements DOMDataReadOnlyTransaction {
     /**
      * Creates new composite Transactions.
      *
      * @param identifier Identifier of transaction.
      */
-    protected DOMBrokerReadOnlyTransaction(Object identifier, Map storeTxFactories) {
+    protected DOMBrokerReadOnlyTransaction(Object identifier, Map<LogicalDatastoreType, ? extends DOMStoreTransactionFactory> storeTxFactories) {
         super(identifier, storeTxFactories);
     }
 
@@ -49,8 +50,8 @@ public class DOMBrokerReadOnlyTransaction<T extends DOMStoreReadTransaction>
     }
 
     @Override
-    protected T createTransaction(LogicalDatastoreType key) {
-        return (T) getTxFactory(key).newReadOnlyTransaction();
+    protected DOMStoreReadTransaction createTransaction(LogicalDatastoreType key) {
+        return getTxFactory(key).newReadOnlyTransaction();
     }
 
 
index efa7226..58dcedc 100644 (file)
@@ -19,8 +19,8 @@ import org.opendaylight.controller.sal.core.spi.data.DOMStoreTransactionFactory;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
 
-public class DOMBrokerReadWriteTransaction<T extends DOMStoreReadWriteTransaction>
-        extends DOMBrokerWriteOnlyTransaction<DOMStoreReadWriteTransaction> implements DOMDataReadWriteTransaction {
+public class DOMBrokerReadWriteTransaction
+        extends AbstractDOMBrokerWriteTransaction<DOMStoreReadWriteTransaction> implements DOMDataReadWriteTransaction {
     /**
      * Creates new composite Transactions.
      *
index df32a2d..7ad68f2 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
+ * Copyright (c) 2015 Huawei Technologies Co. Ltd. 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,
 
 package org.opendaylight.controller.cluster.databroker;
 
-import com.google.common.base.Preconditions;
-import com.google.common.util.concurrent.CheckedFuture;
-import com.google.common.util.concurrent.Futures;
-import com.google.common.util.concurrent.ListenableFuture;
-import java.util.ArrayList;
-import java.util.Collection;
 import java.util.Map;
-import java.util.concurrent.Future;
-import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
-import org.opendaylight.controller.md.sal.common.api.TransactionStatus;
 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
-import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
-import org.opendaylight.controller.md.sal.common.impl.service.AbstractDataTransaction;
-import org.opendaylight.controller.md.sal.dom.api.DOMDataWriteTransaction;
-import org.opendaylight.controller.sal.core.spi.data.DOMStoreThreePhaseCommitCohort;
+import org.opendaylight.controller.sal.core.spi.data.DOMStoreTransactionFactory;
 import org.opendaylight.controller.sal.core.spi.data.DOMStoreWriteTransaction;
-import org.opendaylight.yangtools.yang.common.RpcResult;
-import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
-import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
-import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-public class DOMBrokerWriteOnlyTransaction<T extends DOMStoreWriteTransaction>
-        extends AbstractDOMBrokerTransaction<LogicalDatastoreType, T> implements DOMDataWriteTransaction {
-
-    private static final AtomicReferenceFieldUpdater<DOMBrokerWriteOnlyTransaction, AbstractDOMTransactionFactory> IMPL_UPDATER =
-            AtomicReferenceFieldUpdater.newUpdater(DOMBrokerWriteOnlyTransaction.class, AbstractDOMTransactionFactory.class, "commitImpl");
-    @SuppressWarnings("rawtypes")
-    private static final AtomicReferenceFieldUpdater<DOMBrokerWriteOnlyTransaction, Future> FUTURE_UPDATER =
-            AtomicReferenceFieldUpdater.newUpdater(DOMBrokerWriteOnlyTransaction.class, Future.class, "commitFuture");
-    private static final Logger LOG = LoggerFactory.getLogger(DOMBrokerWriteOnlyTransaction.class);
-    private static final Future<?> CANCELLED_FUTURE = Futures.immediateCancelledFuture();
-
-    /**
-     * Implementation of real commit. It also acts as an indication that
-     * the transaction is running -- which we flip atomically using
-     * {@link #IMPL_UPDATER}.
-     */
-    private volatile AbstractDOMTransactionFactory<?> commitImpl;
 
+public class DOMBrokerWriteOnlyTransaction extends AbstractDOMBrokerWriteTransaction<DOMStoreWriteTransaction> {
     /**
-     * Future task of transaction commit. It starts off as null, but is
-     * set appropriately on {@link #submit()} and {@link #cancel()} via
-     * {@link AtomicReferenceFieldUpdater#lazySet(Object, Object)}.
+     * Creates new composite Transactions.
      *
-     * Lazy set is safe for use because it is only referenced to in the
-     * {@link #cancel()} slow path, where we will busy-wait for it. The
-     * fast path gets the benefit of a store-store barrier instead of the
-     * usual store-load barrier.
+     * @param identifier
+     *            Identifier of transaction.
+     * @param storeTxFactories
      */
-    private volatile Future<?> commitFuture;
-
-    protected DOMBrokerWriteOnlyTransaction(final Object identifier,
-                                            final Map storeTxFactories, final AbstractDOMTransactionFactory<?> commitImpl) {
-        super(identifier, storeTxFactories);
-        this.commitImpl = Preconditions.checkNotNull(commitImpl, "commitImpl must not be null.");
-    }
-
-    @Override
-    protected T createTransaction(final LogicalDatastoreType key) {
-        // FIXME : Casting shouldn't be necessary here
-        return (T) getTxFactory(key).newWriteOnlyTransaction();
-    }
-
-    @Override
-    public void put(final LogicalDatastoreType store, final YangInstanceIdentifier path, final NormalizedNode<?, ?> data) {
-        checkRunning(commitImpl);
-        checkInstanceIdentifierReferencesData(path,data);
-        getSubtransaction(store).write(path, data);
-    }
-
-    private static void checkInstanceIdentifierReferencesData(final YangInstanceIdentifier path,
-            final NormalizedNode<?, ?> data) {
-        final PathArgument lastArg = path.getLastPathArgument();
-        Preconditions.checkArgument(
-                (lastArg == data.getIdentifier()) || (lastArg != null && lastArg.equals(data.getIdentifier())),
-                "Instance identifier references %s but data identifier is %s", lastArg, data);
-    }
-
-    @Override
-    public void delete(final LogicalDatastoreType store, final YangInstanceIdentifier path) {
-        checkRunning(commitImpl);
-        getSubtransaction(store).delete(path);
+    public DOMBrokerWriteOnlyTransaction(Object identifier,
+            Map<LogicalDatastoreType, ? extends DOMStoreTransactionFactory> storeTxFactories,
+            AbstractDOMTransactionFactory<?> commitImpl) {
+        super(identifier, storeTxFactories, commitImpl);
     }
 
     @Override
-    public void merge(final LogicalDatastoreType store, final YangInstanceIdentifier path, final NormalizedNode<?, ?> data) {
-        checkRunning(commitImpl);
-        checkInstanceIdentifierReferencesData(path, data);
-        getSubtransaction(store).merge(path, data);
-    }
-
-    @Override
-    public boolean cancel() {
-        final AbstractDOMTransactionFactory<?> impl = IMPL_UPDATER.getAndSet(this, null);
-        if (impl != null) {
-            LOG.trace("Transaction {} cancelled before submit", getIdentifier());
-            FUTURE_UPDATER.lazySet(this, CANCELLED_FUTURE);
-            closeSubtransactions();
-            return true;
-        }
-
-        // The transaction is in process of being submitted or cancelled. Busy-wait
-        // for the corresponding future.
-        Future<?> future;
-        do {
-            future = commitFuture;
-        } while (future == null);
-
-        return future.cancel(false);
-    }
-
-    @Deprecated
-    @Override
-    public ListenableFuture<RpcResult<TransactionStatus>> commit() {
-        return AbstractDataTransaction.convertToLegacyCommitFuture(submit());
-    }
-
-    @Override
-    public CheckedFuture<Void, TransactionCommitFailedException> submit() {
-        final AbstractDOMTransactionFactory<?> impl = IMPL_UPDATER.getAndSet(this, null);
-        checkRunning(impl);
-
-        final Collection<T> txns = getSubtransactions();
-        final Collection<DOMStoreThreePhaseCommitCohort> cohorts = new ArrayList<>(txns.size());
-
-        // FIXME: deal with errors thrown by backed (ready and submit can fail in theory)
-        for (final DOMStoreWriteTransaction txn : txns) {
-            cohorts.add(txn.ready());
-        }
-
-        final CheckedFuture<Void, TransactionCommitFailedException> ret = impl.submit(this, cohorts);
-        FUTURE_UPDATER.lazySet(this, ret);
-        return ret;
-    }
-
-    private void checkRunning(final AbstractDOMTransactionFactory<?> impl) {
-        Preconditions.checkState(impl != null, "Transaction %s is no longer running", getIdentifier());
+    protected DOMStoreWriteTransaction createTransaction(LogicalDatastoreType key) {
+        return getTxFactory(key).newWriteOnlyTransaction();
     }
 
 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.