/*
 * 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.FluentFuture;
import java.util.Collection;
import java.util.EnumMap;
import java.util.Map;
import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
import org.opendaylight.mdsal.common.api.CommitInfo;
import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
import org.opendaylight.mdsal.dom.api.DOMDataTreeReadTransaction;
import org.opendaylight.mdsal.dom.api.DOMDataTreeReadWriteTransaction;
import org.opendaylight.mdsal.dom.api.DOMDataTreeWriteTransaction;
import org.opendaylight.mdsal.dom.spi.store.DOMStoreThreePhaseCommitCohort;
import org.opendaylight.mdsal.dom.spi.store.DOMStoreTransactionFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public abstract class AbstractDOMTransactionFactory<T extends DOMStoreTransactionFactory> implements AutoCloseable {
    private static final Logger LOG = LoggerFactory.getLogger(AbstractDOMTransactionFactory.class);
    @SuppressWarnings("rawtypes")
    private static final AtomicIntegerFieldUpdater<AbstractDOMTransactionFactory> UPDATER =
            AtomicIntegerFieldUpdater.newUpdater(AbstractDOMTransactionFactory.class, "closed");

    private final Map<LogicalDatastoreType, T> storeTxFactories;

    private volatile int closed = 0;

    protected AbstractDOMTransactionFactory(final Map<LogicalDatastoreType, T> txFactories) {
        this.storeTxFactories = new EnumMap<>(txFactories);
    }

    /**
     * Implementations must return unique identifier for each and every call of
     * this method.
     *
     * @return new Unique transaction identifier.
     */
    protected abstract Object newTransactionIdentifier();

    /**
     * Submits a transaction asynchronously for commit.
     *
     * @param transaction the transaction to submit
     * @param cohorts the associated cohorts
     * @return a resulting Future
     */
    protected abstract FluentFuture<? extends CommitInfo> commit(DOMDataTreeWriteTransaction transaction,
            Collection<DOMStoreThreePhaseCommitCohort> cohorts);

    /**
     * Creates a new read-only transaction.
     *
     * @return the transaction instance
     */
    public final DOMDataTreeReadTransaction newReadOnlyTransaction() {
        checkNotClosed();

        return new DOMBrokerReadOnlyTransaction(newTransactionIdentifier(), storeTxFactories);
    }


    /**
     * Creates a new write-only transaction.
     *
     * @return the transaction instance
     */
    public final DOMDataTreeWriteTransaction newWriteOnlyTransaction() {
        checkNotClosed();

        return new DOMBrokerWriteOnlyTransaction(newTransactionIdentifier(), storeTxFactories, this);
    }


    /**
     * Creates a new read-write transaction.
     *
     * @return the transaction instance
     */
    public final DOMDataTreeReadWriteTransaction newReadWriteTransaction() {
        checkNotClosed();

        return new DOMBrokerReadWriteTransaction(newTransactionIdentifier(), storeTxFactories, this);
    }

    /**
     * Convenience accessor of backing factories intended to be used only by
     * finalization of this class.
     *
     * <b>Note:</b>
     * Finalization of this class may want to access other functionality of
     * supplied Transaction factories.
     *
     * @return Map of backing transaction factories.
     */
    public final Map<LogicalDatastoreType, T> getTxFactories() {
        return storeTxFactories;
    }

    /**
     * Checks if instance is not closed.
     *
     * @throws IllegalStateException If instance of this class was closed.
     *
     */
    protected final void checkNotClosed() {
        Preconditions.checkState(closed == 0, "Transaction factory was closed. No further operations allowed.");
    }

    @Override
    public void close() {
        if (!UPDATER.compareAndSet(this, 0, 1)) {
            LOG.warn("Transaction factory was already closed", new Throwable());
        }
    }
}

@@ -459,16 +459,90 @@
 Future;%0A
+import java.lang.invoke.MethodHandles;%0Aimport java.lang.invoke.VarHandle;%0A
 import j
@@ -614,70 +614,8 @@
 ap;%0A
-import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;%0A
 impo
@@ -1361,226 +1361,333 @@
     
-@SuppressWarnings(%22rawtypes%22)%0A    private static final AtomicIntegerFieldUpdater%3CAbstractDOMTransactionFactory%3E UPDATER =%0A            AtomicIntegerFieldUpdater.newUpdater(AbstractDOMTransactionFactory.class, %22closed%22);
+private static final VarHandle CLOSED;%0A%0A    static %7B%0A        try %7B%0A            CLOSED = MethodHandles.lookup().findVarHandle(AbstractDOMTransactionFactory.class, %22closed%22, boolean.class);%0A        %7D catch (NoSuchFieldException %7C IllegalAccessException e) %7B%0A            throw new ExceptionInInitializerError(e);%0A        %7D%0A    %7D
 %0A%0A  
@@ -1775,22 +1775,22 @@
 ile 
-int
+boolean
  closed
- = 0
 ;%0A%0A 
@@ -4172,19 +4172,15 @@
 ate(
+!
 closed
- == 0
 , %22T
@@ -4308,15 +4308,14 @@
 f (!
-UPDATER
+CLOSED
 .com
@@ -4335,12 +4335,19 @@
 is, 
-0, 1
+false, true
 )) %7B

Use a VarHandle for closed flag