BUG 5523 RoleCtx changes
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / role / RoleContextImpl.java
index 8fe9869d40c405d9d64dea03356a2b20f71983c0..8546fe7dd1d1fb1c0aa58372695e6a89eff91055 100644 (file)
@@ -11,20 +11,29 @@ import javax.annotation.Nullable;
 import java.util.concurrent.Future;
 
 import com.google.common.annotations.VisibleForTesting;
+import com.google.common.base.Preconditions;
+import com.google.common.base.Verify;
+import com.google.common.util.concurrent.AsyncFunction;
 import com.google.common.util.concurrent.FutureCallback;
 import com.google.common.util.concurrent.Futures;
 import com.google.common.util.concurrent.JdkFutureAdapters;
-import com.google.common.util.concurrent.SettableFuture;
-import org.opendaylight.controller.md.sal.common.api.clustering.CandidateAlreadyRegisteredException;
+import com.google.common.util.concurrent.ListenableFuture;
+import io.netty.util.Timeout;
+import io.netty.util.TimerTask;
+import java.util.concurrent.Future;
+import java.util.concurrent.Semaphore;
+import java.util.concurrent.TimeUnit;
+import javax.annotation.Nullable;
+import javax.annotation.concurrent.GuardedBy;
 import org.opendaylight.controller.md.sal.common.api.clustering.Entity;
 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipCandidateRegistration;
 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipService;
-import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
+import org.opendaylight.openflowplugin.api.OFConstants;
 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
+import org.opendaylight.openflowplugin.api.openflow.device.DeviceState;
 import org.opendaylight.openflowplugin.api.openflow.device.RequestContext;
 import org.opendaylight.openflowplugin.api.openflow.role.RoleContext;
-import org.opendaylight.openflowplugin.api.openflow.role.RoleManager;
 import org.opendaylight.openflowplugin.impl.rpc.AbstractRequestContext;
 import org.opendaylight.openflowplugin.impl.services.SalRoleServiceImpl;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef;
@@ -34,6 +43,7 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.role.service.rev150727.SetR
 import org.opendaylight.yang.gen.v1.urn.opendaylight.role.service.rev150727.SetRoleInputBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.role.service.rev150727.SetRoleOutput;
 import org.opendaylight.yangtools.yang.common.RpcResult;
+import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -45,139 +55,274 @@ public class RoleContextImpl implements RoleContext {
 
     private final EntityOwnershipService entityOwnershipService;
     private EntityOwnershipCandidateRegistration entityOwnershipCandidateRegistration;
-    private final RpcProviderRegistry rpcProviderRegistry;
+    private EntityOwnershipCandidateRegistration txEntityOwnershipCandidateRegistration;
+
     private final DeviceContext deviceContext;
+
+    @GuardedBy("mainCandidateGuard")
     private final Entity entity;
-    private final OpenflowOwnershipListener openflowOwnershipListener;
+    @GuardedBy("txCandidateGuard")
+    private final Entity txEntity;
+
     private SalRoleService salRoleService;
-    private FutureCallback<Boolean> roleChangeCallback;
 
-    private final SettableFuture<OfpRole> initRoleChangeFuture;
+    private final Semaphore roleChangeGuard = new Semaphore(1, true);
 
-    public RoleContextImpl(final DeviceContext deviceContext, final RpcProviderRegistry rpcProviderRegistry,
-                           final EntityOwnershipService entityOwnershipService, final OpenflowOwnershipListener openflowOwnershipListener) {
-        this.entityOwnershipService = entityOwnershipService;
-        this.rpcProviderRegistry = rpcProviderRegistry;
-        this.deviceContext = deviceContext;
-        entity = new Entity(RoleManager.ENTITY_TYPE, deviceContext.getPrimaryConnectionContext().getNodeId().getValue());
+    @GuardedBy("roleChangeGuard")
+    private OfpRole clusterRole;
 
-        this.openflowOwnershipListener =  openflowOwnershipListener;
+    public RoleContextImpl(final DeviceContext deviceContext, final EntityOwnershipService entityOwnershipService,
+                           final Entity entity, final Entity txEntity) {
+        this.entityOwnershipService = Preconditions.checkNotNull(entityOwnershipService);
+        this.deviceContext = Preconditions.checkNotNull(deviceContext);
+        this.entity = Preconditions.checkNotNull(entity);
+        this.txEntity = Preconditions.checkNotNull(txEntity);
         salRoleService = new SalRoleServiceImpl(this, deviceContext);
-
-        initRoleChangeFuture = SettableFuture.create();
+        clusterRole = OfpRole.BECOMESLAVE;
     }
 
     @Override
-    public Future<OfpRole> initialization() throws CandidateAlreadyRegisteredException {
-        LOG.debug("Initialization requestOpenflowEntityOwnership for entity {}", entity);
-        openflowOwnershipListener.registerRoleChangeListener(this);
-        entityOwnershipCandidateRegistration = entityOwnershipService.registerCandidate(entity);
-        LOG.info("RoleContextImpl : Candidate registered with ownership service for device :{}", deviceContext
-                .getPrimaryConnectionContext().getNodeId().getValue());
-        return initRoleChangeFuture;
-    }
-
-    /**
-     * @deprecated not used but we are able to add here extra call for get EntityOwnershipState from
-     *             OpenflowOwnershipListener instead call it directly from RoleManager (here could be
-     *             add call salRoleService.setRole(setRoleInput);
-     */
-    @Override
-    @Deprecated
-    public void facilitateRoleChange(final FutureCallback<Boolean> roleChangeCallback) {
-        this.roleChangeCallback = roleChangeCallback;
-        if (!isDeviceConnected()) {
-            throw new IllegalStateException(
-                    "Device is disconnected. Giving up on Role Change:" + deviceContext.getDeviceState().getNodeId());
+    public void initializationRoleContext() {
+        LOG.trace("Initialization MainCandidate for Node {}", deviceContext.getDeviceState().getNodeId());
+        final AsyncFunction<RpcResult<SetRoleOutput>, Void> initFunction = new AsyncFunction<RpcResult<SetRoleOutput>, Void>() {
+            @Override
+            public ListenableFuture<Void> apply(final RpcResult<SetRoleOutput> input) throws Exception {
+                LOG.debug("Initialization request OpenflowEntityOwnership for entity {}", entity);
+                getDeviceState().setRole(OfpRole.BECOMESLAVE);
+                entityOwnershipCandidateRegistration = entityOwnershipService.registerCandidate(entity);
+                LOG.debug("RoleContextImpl : Candidate registered with ownership service for device :{}", deviceContext
+                        .getPrimaryConnectionContext().getNodeId().getValue());
+                return Futures.immediateFuture(null);
+            }
+        };
+
+        try {
+            roleChangeGuard.acquire();
+            final ListenableFuture<Void> roleChange = sendRoleChangeToDevice(OfpRole.BECOMESLAVE, initFunction);
+            Futures.addCallback(roleChange, new FutureCallback<Void>() {
+
+                @Override
+                public void onSuccess(final Void result) {
+                    LOG.debug("Initial RoleContext for Node {} is successful", deviceContext.getDeviceState().getNodeId());
+                    roleChangeGuard.release();
+                }
+
+                @Override
+                public void onFailure(final Throwable t) {
+                    LOG.warn("Initial RoleContext for Node {} fail", deviceContext.getDeviceState().getNodeId(), t);
+                    roleChangeGuard.release();
+                    deviceContext.shutdownConnection();
+                }
+            });
+        } catch (final Exception e) {
+            LOG.warn("Unexpected exception bu Initialization RoleContext for Node {}", deviceContext.getDeviceState().getNodeId(), e);
+            roleChangeGuard.release();
+            deviceContext.shutdownConnection();
         }
     }
 
-    /**
-     * @deprecated not used
-     */
-    @Deprecated
-    private void requestOpenflowEntityOwnership() {
+    @Override
+    public void terminationRoleContext() {
+        LOG.trace("Termination MainCandidate for Node {}", deviceContext.getDeviceState().getNodeId());
+        if (null != entityOwnershipCandidateRegistration) {
+            LOG.debug("Closing EntityOwnershipCandidateRegistration for {}", entity);
+            try {
+                roleChangeGuard.acquire();
+            } catch (final InterruptedException e) {
+                LOG.warn("Unexpected exception in closing EntityOwnershipCandidateRegistration process for entity {}", entity);
+            } finally {
+                entityOwnershipCandidateRegistration.close();
+                entityOwnershipCandidateRegistration = null;
+                // FIXME: call suspendTxCandidate here means lost protection for possible Delete Node before take ownership
+                // by another ClusterNode, but it stabilized cluster behavior in general - So try to find another solution
+                suspendTxCandidate();
+                roleChangeGuard.release();
+            }
+        }
+    }
 
-        LOG.debug("requestOpenflowEntityOwnership for entity {}", entity);
+    @Override
+    public void onDeviceTryToTakeClusterLeadership() {
+        LOG.trace("onDeviceTryToTakeClusterLeadership method call for Entity {}", entity);
+        boolean callShutdown = false;
         try {
-            entityOwnershipCandidateRegistration = entityOwnershipService.registerCandidate(entity);
-
-            // The role change listener must be registered after registering a candidate
-            openflowOwnershipListener.registerRoleChangeListener(this);
-            LOG.info("RoleContextImpl : Candidate registered with ownership service for device :{}", deviceContext.getPrimaryConnectionContext().getNodeId().getValue());
-        } catch (final CandidateAlreadyRegisteredException e) {
-            // we can log and move for this error, as listener is present and role changes will be served.
-            LOG.error("Candidate - Entity already registered with Openflow candidate ", entity, e );
+            roleChangeGuard.acquire();
+            Verify.verify(null != entityOwnershipCandidateRegistration);
+            Verify.verify(OfpRole.BECOMESLAVE.equals(clusterRole));
+
+            clusterRole = OfpRole.BECOMEMASTER;
+            /* register TxCandidate and wait for mainCandidateGuard release from onDeviceTakeLeadership method */
+            setupTxCandidate();
+
+        } catch (final Exception e) {
+            LOG.warn("Unexpected exception in roleChange process for entity {}", entity);
+            callShutdown = true;
+        } finally {
+            roleChangeGuard.release();
+        }
+        if (callShutdown) {
+            deviceContext.shutdownConnection();
         }
     }
 
     @Override
-    public void onRoleChanged(final OfpRole oldRole, final OfpRole newRole) {
-        LOG.trace("onRoleChanged method call for Entity {}", entity);
+    public void onDeviceTakeClusterLeadership() {
+        LOG.trace("onDeviceTakeClusterLeadership for entity {}", txEntity);
+        try {
+            roleChangeGuard.acquire();
+            Verify.verify(null != txEntityOwnershipCandidateRegistration);
+            Verify.verify(OfpRole.BECOMEMASTER.equals(clusterRole));
 
-        if (!isDeviceConnected()) {
-            // this can happen as after the disconnect, we still get a last messsage from EntityOwnershipService.
-            LOG.info("Device {} is disconnected from this node. Hence not attempting a role change.",
-                    deviceContext.getPrimaryConnectionContext().getNodeId());
-            if (!initRoleChangeFuture.isDone()) {
-                LOG.debug("RoleChange is not valid for initialization Entity {} anymore - Device is disconnected", entity);
-                initRoleChangeFuture.cancel(true);
+            if (null == entityOwnershipCandidateRegistration) {
+                LOG.debug("EntityOwnership candidate for entity {} is closed.", txEntity);
+                suspendTxCandidate();
+                roleChangeGuard.release();
+                return;
             }
-            return;
-        }
 
-        if (!initRoleChangeFuture.isDone()) {
-            LOG.debug("Initialization Role for entity {} is chosed {}", entity, newRole);
-            initRoleChangeFuture.set(newRole);
+            final ListenableFuture<Void> future = onRoleChanged(OfpRole.BECOMESLAVE, OfpRole.BECOMEMASTER);
+            Futures.addCallback(future, new FutureCallback<Void>() {
+
+                @Override
+                public void onSuccess(final Void result) {
+                    LOG.debug("Take Leadership for node {} was successful", getDeviceState().getNodeId());
+                    roleChangeGuard.release();
+                }
+
+                @Override
+                public void onFailure(final Throwable t) {
+                    LOG.warn("Take Leadership for node {} failed", getDeviceState().getNodeId(), t);
+                    roleChangeGuard.release();
+                    deviceContext.shutdownConnection();
+                }
+            });
+
+        } catch (final Exception e) {
+            LOG.warn("Unexpected exception in roleChange process for entity {}", txEntity);
+            roleChangeGuard.release();
+            deviceContext.shutdownConnection();
         }
+    };
 
-        LOG.debug("Role change received from ownership listener from {} to {} for device:{}", oldRole, newRole,
-                deviceContext.getPrimaryConnectionContext().getNodeId());
+    @Override
+    public void onDeviceLostClusterLeadership() {
+        LOG.trace("onDeviceLostClusterLeadership method call for Entity {}", entity);
+        try {
+            roleChangeGuard.acquire();
+            Verify.verify(null != entityOwnershipCandidateRegistration);
+            Verify.verify(null != txEntityOwnershipCandidateRegistration);
+            Verify.verify(OfpRole.BECOMEMASTER.equals(clusterRole));
 
-        final SetRoleInput setRoleInput = (new SetRoleInputBuilder())
-                .setControllerRole(newRole)
-                .setNode(new NodeRef(deviceContext.getDeviceState().getNodeInstanceIdentifier()))
-                .build();
+            clusterRole = OfpRole.BECOMESLAVE;
 
-        final Future<RpcResult<SetRoleOutput>> setRoleOutputFuture = salRoleService.setRole(setRoleInput);
+            final ListenableFuture<Void> future = onRoleChanged(OfpRole.BECOMEMASTER, OfpRole.BECOMESLAVE);
+            Futures.addCallback(future, new FutureCallback<Void>() {
 
-        Futures.addCallback(JdkFutureAdapters.listenInPoolThread(setRoleOutputFuture), new FutureCallback<RpcResult<SetRoleOutput>>() {
-            @Override
-            public void onSuccess(final RpcResult<SetRoleOutput> setRoleOutputRpcResult) {
-                LOG.debug("Rolechange {} successful made on switch :{}", newRole,
-                        deviceContext.getPrimaryConnectionContext().getNodeId());
-                deviceContext.getDeviceState().setRole(newRole);
-                if (roleChangeCallback != null) {
-                    roleChangeCallback.onSuccess(true);
+                @Override
+                public void onSuccess(final Void result) {
+                    LOG.debug("Lost Leadership for node {} was successful", getDeviceState().getNodeId());
+                    suspendTxCandidate();
+                    roleChangeGuard.release();
                 }
-            }
 
-            @Override
-            public void onFailure(final Throwable throwable) {
-                LOG.error("Error in setRole {} for device {} ", newRole,
-                        deviceContext.getPrimaryConnectionContext().getNodeId(), throwable);
-                if (roleChangeCallback != null) {
-                    roleChangeCallback.onFailure(throwable);
+                @Override
+                public void onFailure(final Throwable t) {
+                    LOG.debug("Lost Leadership for node {} faild", getDeviceState().getNodeId(), t);
+                    roleChangeGuard.release();
+                    deviceContext.shutdownConnection();
                 }
-            }
-        });
+
+            });
+
+        } catch (final Exception e) {
+            LOG.warn("Unexpected exception in roleChange process for entity {}", entity);
+            roleChangeGuard.release();
+            deviceContext.shutdownConnection();
+        }
     }
 
     @Override
-    public void close() throws Exception {
-        if (entityOwnershipCandidateRegistration != null) {
-            LOG.debug("Closing EntityOwnershipCandidateRegistration for {}", entity);
-            entityOwnershipCandidateRegistration.close();
+    public boolean isMainCandidateRegistered() {
+        final boolean result;
+        try {
+            roleChangeGuard.acquire();
+        } catch (final InterruptedException e) {
+            LOG.warn("Unexpected exception in check EntityOwnershipCandidateRegistration process for entity {}", entity);
+        } finally {
+            result = entityOwnershipCandidateRegistration != null;
+            roleChangeGuard.release();
         }
+        return result;
     }
 
     @Override
-    public void onDeviceContextClosed(final DeviceContext deviceContext) {
+    public boolean isTxCandidateRegistered() {
+        final boolean result;
         try {
-            LOG.debug("onDeviceContextClosed called");
-            this.close();
-        } catch (final Exception e) {
-            LOG.error("Exception in onDeviceContextClosed of RoleContext", e);
+            roleChangeGuard.acquire();
+        } catch (final InterruptedException e) {
+            LOG.warn("Unexpected exception in check TxEntityOwnershipCandidateRegistration process for txEntity {}", txEntity);
+        } finally {
+            result = txEntityOwnershipCandidateRegistration != null;
+            roleChangeGuard.release();
+        }
+        return result;
+    }
+
+    @VisibleForTesting
+    ListenableFuture<Void> onRoleChanged(final OfpRole oldRole, final OfpRole newRole) {
+        LOG.trace("onRoleChanged method call for Entity {}", entity);
+
+        if (!isDeviceConnected()) {
+            // this can happen as after the disconnect, we still get a last message from EntityOwnershipService.
+            LOG.debug("Device {} is disconnected from this node. Hence not attempting a role change.", deviceContext
+                    .getPrimaryConnectionContext().getNodeId());
+            // we don't need to do anything
+            return Futures.immediateFuture(null);
+        }
+
+        final AsyncFunction<RpcResult<SetRoleOutput>, Void> roleChangeFunction = new AsyncFunction<RpcResult<SetRoleOutput>, Void>() {
+            @Override
+            public ListenableFuture<Void> apply(final RpcResult<SetRoleOutput> setRoleOutputRpcResult) throws Exception {
+                LOG.debug("Role change {} successful made on switch :{}", newRole, deviceContext.getDeviceState().getNodeId());
+                getDeviceState().setRole(newRole);
+                return deviceContext.onClusterRoleChange(oldRole, newRole);
+            }
+        };
+        return sendRoleChangeToDevice(newRole, roleChangeFunction);
+    }
+
+    @GuardedBy("roleChangeGuard")
+    private void setupTxCandidate() throws Exception {
+        LOG.debug("setupTxCandidate for entity {} and Transaction entity {}", entity, txEntity);
+        Verify.verify(txEntity != null);
+        Verify.verify(entityOwnershipCandidateRegistration != null);
+        Verify.verify(txEntityOwnershipCandidateRegistration == null);
+        txEntityOwnershipCandidateRegistration = entityOwnershipService.registerCandidate(txEntity);
+    }
+
+    @GuardedBy("roleChangeGuard")
+    private void suspendTxCandidate() {
+        LOG.trace("Suspend TxCandidate for Node {}", deviceContext.getDeviceState().getNodeId());
+        if (null != txEntityOwnershipCandidateRegistration) {
+            LOG.debug("Closing TxEntityOwnershipCandidateRegistration for {}", txEntity);
+            txEntityOwnershipCandidateRegistration.close();
+            txEntityOwnershipCandidateRegistration = null;
+        }
+    }
+
+    @Override
+    public void close() {
+        LOG.trace("Close RoleCtx for Node {}", deviceContext.getDeviceState().getNodeId());
+        if (null != entityOwnershipCandidateRegistration) {
+            LOG.info("Close Node Entity {} registration", entity);
+            entityOwnershipCandidateRegistration.close();
+            entityOwnershipCandidateRegistration = null;
+        }
+        if (null != txEntityOwnershipCandidateRegistration) {
+            LOG.info("Close Tx Entity {} registration", txEntity);
+            txEntityOwnershipCandidateRegistration.close();
+            txEntityOwnershipCandidateRegistration = null;
         }
     }
 
@@ -187,9 +332,8 @@ public class RoleContextImpl implements RoleContext {
     }
 
     @Override
-    public void onDeviceDisconnectedFromCluster(final boolean removeNodeFromDS) {
-        LOG.debug("Called onDeviceDisconnectedFromCluster in DeviceContext for entity:{}", entity);
-        deviceContext.onDeviceDisconnectedFromCluster(removeNodeFromDS);
+    public Entity getTxEntity() {
+        return txEntity;
     }
 
     private boolean isDeviceConnected() {
@@ -200,7 +344,7 @@ public class RoleContextImpl implements RoleContext {
     @Nullable
     @Override
     public <T> RequestContext<T> createRequestContext() {
-        final AbstractRequestContext<T> ret = new AbstractRequestContext<T>(deviceContext.getReservedXid()) {
+        final AbstractRequestContext<T> ret = new AbstractRequestContext<T>(deviceContext.reservedXidForDeviceMessage()) {
             @Override
             public void close() {
             }
@@ -209,7 +353,59 @@ public class RoleContextImpl implements RoleContext {
     }
 
     @VisibleForTesting
-    public void setSalRoleService(final SalRoleService salRoleService) {
+    void setSalRoleService(final SalRoleService salRoleService) {
         this.salRoleService = salRoleService;
     }
+
+    @Override
+    public DeviceState getDeviceState() {
+        return deviceContext.getDeviceState();
+    }
+
+    @Override
+    public DeviceContext getDeviceContext() {
+        return deviceContext;
+    }
+
+    private ListenableFuture<Void> sendRoleChangeToDevice(final OfpRole newRole, final AsyncFunction<RpcResult<SetRoleOutput>, Void> function) {
+        LOG.debug("Send new role {} to device {}", newRole, deviceContext.getDeviceState().getNodeId());
+        final Future<RpcResult<SetRoleOutput>> setRoleOutputFuture;
+        if (deviceContext.getDeviceState().getFeatures().getVersion() < OFConstants.OFP_VERSION_1_3) {
+            LOG.debug("Device OF version {} not support ROLE", deviceContext.getDeviceState().getFeatures().getVersion());
+            setRoleOutputFuture = Futures.immediateFuture(RpcResultBuilder.<SetRoleOutput> success().build());
+        } else {
+            final SetRoleInput setRoleInput = (new SetRoleInputBuilder()).setControllerRole(newRole)
+                    .setNode(new NodeRef(deviceContext.getDeviceState().getNodeInstanceIdentifier())).build();
+            setRoleOutputFuture = salRoleService.setRole(setRoleInput);
+            final TimerTask timerTask = new TimerTask() {
+
+                @Override
+                public void run(final Timeout timeout) throws Exception {
+                    if (!setRoleOutputFuture.isDone()) {
+                        LOG.info("New role {} was not propagated to device {} during 10 sec. Close connection immediately.",
+                                newRole, deviceContext.getDeviceState().getNodeId());
+                        setRoleOutputFuture.cancel(true);
+                    }
+                }
+            };
+            deviceContext.getTimer().newTimeout(timerTask, 10, TimeUnit.SECONDS);
+        }
+        return Futures.transform(JdkFutureAdapters.listenInPoolThread(setRoleOutputFuture), function);
+    }
+
+
+    @Override
+    public OfpRole getClusterRole() {
+        final OfpRole role;
+        try {
+            roleChangeGuard.acquire();
+        } catch (final InterruptedException e) {
+            LOG.warn("Unexpected exception in get ClusterRole process for entity {}", entity);
+        } finally {
+            role = OfpRole.forValue(clusterRole.getIntValue());
+            roleChangeGuard.release();
+        }
+        return role;
+    }
+
 }