Merge "Bug 4957 Fix methods for change TxChainManager"
authormichal rehak <mirehak@cisco.com>
Tue, 9 Feb 2016 18:16:13 +0000 (18:16 +0000)
committerGerrit Code Review <gerrit@opendaylight.org>
Tue, 9 Feb 2016 18:16:14 +0000 (18:16 +0000)
openflowplugin-api/src/main/java/org/opendaylight/openflowplugin/api/openflow/device/DeviceContext.java
openflowplugin-api/src/main/java/org/opendaylight/openflowplugin/api/openflow/role/RoleContext.java
openflowplugin-impl/src/main/java/org/opendaylight/openflowplugin/impl/device/DeviceContextImpl.java
openflowplugin-impl/src/main/java/org/opendaylight/openflowplugin/impl/device/TransactionChainManager.java
openflowplugin-impl/src/main/java/org/opendaylight/openflowplugin/impl/role/RoleContextImpl.java
openflowplugin-impl/src/main/java/org/opendaylight/openflowplugin/impl/role/RoleManagerImpl.java
openflowplugin-impl/src/test/java/org/opendaylight/openflowplugin/impl/device/TransactionChainManagerTest.java

index ac5e920df4a25aa339e775301dd6e831e741ffc0..2ca45d010498ef050c443e01fa4ec85dea4f1046 100644 (file)
@@ -91,13 +91,6 @@ public interface DeviceContext extends AutoCloseable,
      */
     void onClusterRoleChange(@CheckForNull OfpRole role);
 
-    /**
-     * Same as {@link DeviceContext#onClusterRoleChange(OfpRole)} but it should be call for new
-     * connection initialization only, because submit transaction is not active in this time.
-     * @param role - NewRole expect to be {@link OfpRole#BECOMESLAVE} or {@link OfpRole#BECOMEMASTER}
-     */
-    void onInitClusterRoleChange(@CheckForNull final OfpRole role);
-
     /**
      * Method creates put operation using provided data in underlying transaction chain.
      */
index 459bb6eb4b97c19fbb64341777b203e00fa78323..3f1efee32556077f6fdb6ebe05ade65aa4df279c 100644 (file)
@@ -10,13 +10,12 @@ package org.opendaylight.openflowplugin.api.openflow.role;
 import java.util.concurrent.Future;
 import org.opendaylight.controller.md.sal.common.api.clustering.CandidateAlreadyRegisteredException;
 import org.opendaylight.openflowplugin.api.openflow.device.RequestContextStack;
-import org.opendaylight.openflowplugin.api.openflow.device.handlers.DeviceContextClosedHandler;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.role.service.rev150727.OfpRole;
 
 /**
  * Created by kramesha on 9/12/15.
  */
-public interface RoleContext extends RoleChangeListener, DeviceContextClosedHandler, RequestContextStack {
+public interface RoleContext extends RoleChangeListener, RequestContextStack {
 
     /**
      * Initialization method is responsible for a registration of
index 26998b4840222ea13410ca25b4224230c12115df..28fffed82c330cf8f0cc0b976d5cda962a7b1d5e 100644 (file)
@@ -228,12 +228,15 @@ public class DeviceContextImpl implements DeviceContext, ExtensionConverterProvi
 
     @Override
     public void onClusterRoleChange(@CheckForNull final OfpRole role) {
-        this.onClusterRoleChange(role, true);
-    }
-
-    @Override
-    public void onInitClusterRoleChange(@CheckForNull final OfpRole role) {
-        this.onClusterRoleChange(role, false);
+        LOG.debug("onClusterRoleChange {} for node:", role, deviceState.getNodeId());
+        Preconditions.checkArgument(role != null);
+        if (OfpRole.BECOMEMASTER.equals(role)) {
+            transactionChainManager.activateTransactionManager();
+        } else if (OfpRole.BECOMESLAVE.equals(role)) {
+            transactionChainManager.deactivateTransactionManager();
+        } else {
+            LOG.warn("Unknow OFCluster Role {} for Node {}", role, deviceState.getNodeId());
+        }
     }
 
     @Override
@@ -591,16 +594,4 @@ public class DeviceContextImpl implements DeviceContext, ExtensionConverterProvi
     public ExtensionConverterProvider getExtensionConverterProvider() {
         return extensionConverterProvider;
     }
-
-    private void onClusterRoleChange(@CheckForNull final OfpRole role, final boolean enableSubmit) {
-        LOG.debug("onClusterRoleChange {} for node:", role, deviceState.getNodeId());
-        Preconditions.checkArgument(role != null);
-        if (OfpRole.BECOMESLAVE.equals(role)) {
-            transactionChainManager.activateTransactionManager(enableSubmit);
-        } else if (OfpRole.BECOMEMASTER.equals(role)) {
-            transactionChainManager.deactivateTransactionManager();
-        } else {
-            LOG.warn("Unknow OFCluster Role {} for Node {}", role, deviceState.getNodeId());
-        }
-    }
 }
index 7b751fb94a975e61974b892f8ca0e8b293881715..020714b703d565ba18127b36a093091555a151ec 100644 (file)
@@ -95,11 +95,9 @@ class TransactionChainManager implements TransactionChainListener, AutoCloseable
      * Method change status for TxChainManager to {@link TransactionChainManagerStatus#WORKING} and it has to make
      * registration for this class instance as {@link TransactionChainListener} to provide possibility a make DS
      * transactions. Call this method for MASTER role only.
-     * @param enableSubmit - marker to be sure a WriteTransaction submit is not blocking
-     *            (Blocking write is used for initialization part only)
      */
-    public void activateTransactionManager(final boolean enableSubmit) {
-        LOG.trace("activetTransactionManager for node {} transaction submit is set to {}", deviceState.getNodeId(), enableSubmit);
+    public void activateTransactionManager() {
+        LOG.trace("activetTransactionManaager for node {} transaction submit is set to {}", deviceState.getNodeId());
         synchronized (txLock) {
             if (TransactionChainManagerStatus.SLEEPING.equals(transactionChainManagerStatus)) {
                 LOG.debug("Transaction Factory create {}", deviceState.getNodeId());
@@ -107,7 +105,6 @@ class TransactionChainManager implements TransactionChainListener, AutoCloseable
                 Preconditions.checkState(wTx == null, "We have some unexpected WriteTransaction.");
                 this.transactionChainManagerStatus = TransactionChainManagerStatus.WORKING;
                 createTxChain();
-                this.submitIsEnabled = enableSubmit;
             } else {
                 LOG.debug("Transaction is active {}", deviceState.getNodeId());
             }
@@ -139,12 +136,12 @@ class TransactionChainManager implements TransactionChainListener, AutoCloseable
             return false;
         }
         synchronized (txLock) {
-            Preconditions.checkState(TransactionChainManagerStatus.WORKING.equals(transactionChainManagerStatus),
-                    "we have here Uncompleted Transaction for node {} and we are not MASTER", nodeII);
             if (wTx == null) {
                 LOG.trace("nothing to commit - submit returns true");
                 return true;
             }
+            Preconditions.checkState(TransactionChainManagerStatus.WORKING.equals(transactionChainManagerStatus),
+                    "we have here Uncompleted Transaction for node {} and we are not MASTER", nodeII);     
             final CheckedFuture<Void, TransactionCommitFailedException> submitFuture = wTx.submit();
             Futures.addCallback(submitFuture, new FutureCallback<Void>() {
                 @Override
@@ -209,10 +206,12 @@ class TransactionChainManager implements TransactionChainListener, AutoCloseable
 
     @Nullable
     private WriteTransaction getTransactionSafely() {
-        if (wTx == null && !TransactionChainManagerStatus.SHUTTING_DOWN.equals(transactionChainManagerStatus)) {
+        if (wTx == null && TransactionChainManagerStatus.WORKING.equals(transactionChainManagerStatus)) {
             synchronized (txLock) {
-                if (wTx == null && txChainFactory != null) {
-                    wTx = txChainFactory.newWriteOnlyTransaction();
+                if (wTx == null && TransactionChainManagerStatus.WORKING.equals(transactionChainManagerStatus)) {
+                    if (wTx == null && txChainFactory != null) {
+                        wTx = txChainFactory.newWriteOnlyTransaction();
+                    }
                 }
             }
         }
index f43aec88bf2dc96e20d67ad35adb8d9035dc26d9..e145a75d38b99aa9afefd5ff1c378b5df93e3477 100644 (file)
@@ -93,8 +93,9 @@ public class RoleContextImpl implements RoleContext {
 
         if (!initRoleChangeFuture.isDone()) {
             LOG.debug("Initialization Role for entity {} is chosed {}", entity, newRole);
+            // we don't want to wait for Device RoleChangeResponse in initial phase
+            deviceContext.onClusterRoleChange(newRole);
             initRoleChangeFuture.set(newRole);
-            deviceContext.onInitClusterRoleChange(newRole);
         }
 
         LOG.debug("Role change received from ownership listener from {} to {} for device:{}", oldRole, newRole,
@@ -140,16 +141,6 @@ public class RoleContextImpl implements RoleContext {
         }
     }
 
-    @Override
-    public void onDeviceContextClosed(final DeviceContext deviceContext) {
-        try {
-            LOG.debug("onDeviceContextClosed called");
-            this.close();
-        } catch (final Exception e) {
-            LOG.error("Exception in onDeviceContextClosed of RoleContext", e);
-        }
-    }
-
     @Override
     public Entity getEntity() {
         return entity;
index e28c87225f643438d42ecc1fc8759991fa547ba1..a9ad826f1e42ed82b22615f979598d1c05c49356 100644 (file)
@@ -76,7 +76,7 @@ public class RoleManagerImpl implements RoleManager {
 
         final RoleContext roleContext = new RoleContextImpl(deviceContext, rpcProviderRegistry, entityOwnershipService, openflowOwnershipListener);
         // if the device context gets closed (mostly on connection close), we would need to cleanup
-        deviceContext.addDeviceContextClosedHandler(roleContext);
+        deviceContext.addDeviceContextClosedHandler(this);
         Verify.verify(contexts.putIfAbsent(deviceContext, roleContext) == null,
                 "RoleCtx for master Node {} is still not close.", deviceContext.getDeviceState().getNodeId());
         OfpRole role = null;
index 9a7b8edbb9eddad0734d6405bf8ac0c4a7bf62f1..e43a6f5c2dea70332243d0d8251d487eece77e68 100644 (file)
@@ -90,7 +90,7 @@ public class TransactionChainManagerTest {
 
         path = InstanceIdentifier.create(Nodes.class).child(Node.class, new NodeKey(nodeId));
         Mockito.when(writeTx.submit()).thenReturn(Futures.<Void, TransactionCommitFailedException>immediateCheckedFuture(null));
-        txChainManager.activateTransactionManager(false);
+        txChainManager.activateTransactionManager();
     }
 
     @After
@@ -116,9 +116,9 @@ public class TransactionChainManagerTest {
     public void testSubmitTransaction() throws Exception {
         final Node data = new NodeBuilder().setId(nodeId).build();
         txChainManager.enableSubmit();
-        txChainManager.activateTransactionManager(true);
+        txChainManager.activateTransactionManager();
         txChainManager.writeToTransaction(LogicalDatastoreType.CONFIGURATION, path, data);
-        txChainManager.activateTransactionManager(true);
+        txChainManager.activateTransactionManager();
         txChainManager.submitWriteTransaction();
 
         Mockito.verify(txChain).newWriteOnlyTransaction();