Merge "Removed unused fields"
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / device / TransactionChainManager.java
index 22988af60a522f2730a9621c8e06db60ce81318a..4706cc1df8d789a24605306c3a32c3563d5fc6eb 100644 (file)
@@ -22,8 +22,6 @@ import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
 import org.opendaylight.controller.md.sal.common.api.data.TransactionChain;
 import org.opendaylight.controller.md.sal.common.api.data.TransactionChainListener;
 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
-import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
-import org.opendaylight.openflowplugin.impl.util.DeviceStateUtil;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
 import org.opendaylight.yangtools.concepts.Registration;
@@ -43,7 +41,7 @@ import org.slf4j.LoggerFactory;
  * and submitTransaction method (wrapped {@link WriteTransaction#submit()})
  *
  * @author <a href="mailto:vdemcak@cisco.com">Vaclav Demcak</a>
- *         <p/>
+ *         </p>
  *         Created: Apr 2, 2015
  */
 class TransactionChainManager implements TransactionChainListener, AutoCloseable {
@@ -56,26 +54,29 @@ class TransactionChainManager implements TransactionChainListener, AutoCloseable
     private WriteTransaction wTx;
     private BindingTransactionChain txChainFactory;
     private boolean submitIsEnabled;
+
+    public TransactionChainManagerStatus getTransactionChainManagerStatus() {
+        return transactionChainManagerStatus;
+    }
+
     private TransactionChainManagerStatus transactionChainManagerStatus;
     private ReadyForNewTransactionChainHandler readyForNewTransactionChainHandler;
     private final KeyedInstanceIdentifier<Node, NodeKey> nodeII;
-    private final ConnectionContext connectionContext;
     private Registration managerRegistration;
 
     TransactionChainManager(@Nonnull final DataBroker dataBroker,
-                            @Nonnull final ConnectionContext connectionContext,
+                            @Nonnull final KeyedInstanceIdentifier<Node, NodeKey> nodeII,
                             @Nonnull final Registration managerRegistration) {
         this.dataBroker = Preconditions.checkNotNull(dataBroker);
-        this.nodeII = Preconditions.checkNotNull(DeviceStateUtil.createNodeInstanceIdentifier(connectionContext.getNodeId()));
-        this.connectionContext = Preconditions.checkNotNull(connectionContext);
+        this.nodeII = Preconditions.checkNotNull(nodeII);
         this.managerRegistration = Preconditions.checkNotNull(managerRegistration);
+        this.transactionChainManagerStatus = TransactionChainManagerStatus.WORKING;
         createTxChain(dataBroker);
         LOG.debug("created txChainManager");
     }
 
     private void createTxChain(final DataBroker dataBroker) {
         txChainFactory = dataBroker.createTransactionChain(TransactionChainManager.this);
-        this.transactionChainManagerStatus = TransactionChainManagerStatus.WORKING;
     }
 
     void initialSubmitWriteTransaction() {
@@ -83,11 +84,12 @@ class TransactionChainManager implements TransactionChainListener, AutoCloseable
         submitWriteTransaction();
     }
 
-    public boolean attemptToRegisterHandler(final ReadyForNewTransactionChainHandler readyForNewTransactionChainHandler) {
-        if (null == this.readyForNewTransactionChainHandler) {
-            synchronized (this) {
-                Preconditions.checkState(null != this.managerRegistration);
-                this.readyForNewTransactionChainHandler = readyForNewTransactionChainHandler;
+    public synchronized boolean attemptToRegisterHandler(final ReadyForNewTransactionChainHandler readyForNewTransactionChainHandler) {
+        if (TransactionChainManagerStatus.SHUTTING_DOWN.equals(this.transactionChainManagerStatus)
+                && null == this.readyForNewTransactionChainHandler) {
+            this.readyForNewTransactionChainHandler = readyForNewTransactionChainHandler;
+            if (managerRegistration == null) {
+                this.readyForNewTransactionChainHandler.onReadyForNewTransactionChain();
             }
             return true;
         } else {
@@ -100,17 +102,27 @@ class TransactionChainManager implements TransactionChainListener, AutoCloseable
             LOG.trace("transaction not committed - submit block issued");
             return false;
         }
-
-        if (wTx == null) {
-            LOG.trace("nothing to commit - submit returns true");
-            return true;
-        }
         synchronized (txLock) {
             if (wTx == null) {
                 LOG.trace("nothing to commit - submit returns true");
                 return true;
             }
-            wTx.submit();
+            final CheckedFuture<Void, TransactionCommitFailedException> submitFuture = wTx.submit();
+            Futures.addCallback(submitFuture, new FutureCallback<Void>() {
+                @Override
+                public void onSuccess(Void result) {
+                    //no action required
+                }
+
+                @Override
+                public void onFailure(Throwable t) {
+                    if (t instanceof TransactionCommitFailedException) {
+                        LOG.error("Transaction commit failed. {}", t);
+                    } else {
+                        LOG.error("Exception during transaction submitting. {}", t);
+                    }
+                }
+            });
             wTx = null;
         }
         return true;
@@ -171,10 +183,13 @@ class TransactionChainManager implements TransactionChainListener, AutoCloseable
             final WriteTransaction writeTx = getTransactionSafely();
             this.transactionChainManagerStatus = TransactionChainManagerStatus.SHUTTING_DOWN;
             writeTx.delete(LogicalDatastoreType.OPERATIONAL, nodeII);
+            LOG.debug("Delete node {} from operational DS put to write transaction.", nodeII);
             CheckedFuture<Void, TransactionCommitFailedException> submitsFuture = writeTx.submit();
+            LOG.debug("Delete node {} from operational DS write transaction submitted.", nodeII);
             Futures.addCallback(submitsFuture, new FutureCallback<Void>() {
                 @Override
                 public void onSuccess(final Void aVoid) {
+                    LOG.debug("Removing node {} from operational DS successful .", nodeII);
                     notifyReadyForNewTransactionChainAndCloseFactory();
                 }
 
@@ -190,20 +205,22 @@ class TransactionChainManager implements TransactionChainListener, AutoCloseable
 
     private void notifyReadyForNewTransactionChainAndCloseFactory() {
         synchronized (this) {
-            if (null != readyForNewTransactionChainHandler) {
-                readyForNewTransactionChainHandler.onReadyForNewTransactionChain(connectionContext);
-            }
             try {
+                LOG.debug("Closing registration in manager.");
                 managerRegistration.close();
             } catch (Exception e) {
                 LOG.warn("Failed to close transaction chain manager's registration.", e);
             }
             managerRegistration = null;
+            if (null != readyForNewTransactionChainHandler) {
+                readyForNewTransactionChainHandler.onReadyForNewTransactionChain();
+            }
         }
         txChainFactory.close();
+        LOG.debug("Transaction chain factory closed.");
     }
 
-    private enum TransactionChainManagerStatus {
+    public enum TransactionChainManagerStatus {
         WORKING, SHUTTING_DOWN;
     }