Bug 6665 - Fix switches scalability
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / role / RoleContextImpl.java
index faf903906759b9146fc89ec1a9d29091c7ae2c71..b636c9fb3021aa379c9651fca999c8ff84be70ca 100644 (file)
@@ -7,29 +7,31 @@
  */
 package org.opendaylight.openflowplugin.impl.role;
 
+import com.google.common.annotations.VisibleForTesting;
+import com.google.common.base.Function;
 import com.google.common.base.Preconditions;
+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.ListenableFuture;
+import io.netty.util.HashedWheelTimer;
 import io.netty.util.TimerTask;
+
 import java.util.concurrent.ExecutionException;
 import java.util.concurrent.Future;
-import java.util.concurrent.Semaphore;
 import java.util.concurrent.TimeUnit;
 import javax.annotation.Nonnull;
 import javax.annotation.Nullable;
-import org.opendaylight.controller.md.sal.common.api.clustering.CandidateAlreadyRegisteredException;
-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.mdsal.singleton.common.api.ServiceGroupIdentifier;
 import org.opendaylight.openflowplugin.api.OFConstants;
+import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
 import org.opendaylight.openflowplugin.api.openflow.device.DeviceInfo;
 import org.opendaylight.openflowplugin.api.openflow.device.RequestContext;
-import org.opendaylight.openflowplugin.api.openflow.lifecycle.LifecycleConductor;
+import org.opendaylight.openflowplugin.api.openflow.device.handlers.ClusterInitializationPhaseHandler;
+import org.opendaylight.openflowplugin.api.openflow.lifecycle.LifecycleService;
 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.util.DeviceStateUtil;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.role.service.rev150727.OfpRole;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.role.service.rev150727.SalRoleService;
@@ -41,64 +43,35 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 /**
- * Role context hold information about entity ownership registration,
- * register and unregister candidate (main and tx)
+ * Role context try to make change device role on device
  */
 class RoleContextImpl implements RoleContext {
 
     private static final Logger LOG = LoggerFactory.getLogger(RoleContextImpl.class);
-    private static final int TIMEOUT = 12;
-
-    private final EntityOwnershipService entityOwnershipService;
-    private volatile EntityOwnershipCandidateRegistration entityOwnershipCandidateRegistration = null;
-    private volatile EntityOwnershipCandidateRegistration txEntityOwnershipCandidateRegistration = null;
-
-    private final Entity entity;
-    private final Entity txEntity;
 
     private SalRoleService salRoleService = null;
-
-    private final Semaphore roleChangeGuard = new Semaphore(1, true);
-
-    private final LifecycleConductor conductor;
+    private final HashedWheelTimer hashedWheelTimer;
     private final DeviceInfo deviceInfo;
     private CONTEXT_STATE state;
+    private final RoleManager myManager;
+    private ClusterInitializationPhaseHandler clusterInitializationPhaseHandler;
+    private final LifecycleService lifecycleService;
 
     RoleContextImpl(final DeviceInfo deviceInfo,
-                    final EntityOwnershipService entityOwnershipService,
-                    final Entity entity,
-                    final Entity txEntity,
-                    final LifecycleConductor lifecycleConductor) {
-        this.entityOwnershipService = entityOwnershipService;
-        this.entity = entity;
-        this.txEntity = txEntity;
-        this.conductor = lifecycleConductor;
+                    final HashedWheelTimer hashedWheelTimer,
+                    final RoleManager myManager,
+                    final LifecycleService lifecycleService) {
         this.deviceInfo = deviceInfo;
-        state = CONTEXT_STATE.INITIALIZATION;
-    }
-
-    @Override
-    public boolean initialization() {
-        LOG.info("Initialization main candidate for node {}", getDeviceInfo().getNodeId());
-        setState(CONTEXT_STATE.WORKING);
-        return registerCandidate(this.entity);
-    }
-
-    @Override
-    public void unregisterAllCandidates() {
-        LOG.info("Role context closed, unregistering all candidates for ownership for node {}", getDeviceInfo().getNodeId());
-        if (isMainCandidateRegistered()) {
-            unregisterCandidate(this.entity);
-        }
-        if (isTxCandidateRegistered()) {
-            unregisterCandidate(this.txEntity);
-        }
+        this.state = CONTEXT_STATE.WORKING;
+        this.myManager = myManager;
+        this.hashedWheelTimer = hashedWheelTimer;
+        this.lifecycleService = lifecycleService;
     }
 
     @Nullable
     @Override
     public <T> RequestContext<T> createRequestContext() {
-        return new AbstractRequestContext<T>(conductor.reserveXidForDeviceMessage(getDeviceInfo())) {
+        return new AbstractRequestContext<T>(deviceInfo.reserveXidForDeviceMessage()) {
             @Override
             public void close() {
             }
@@ -112,155 +85,120 @@ class RoleContextImpl implements RoleContext {
     }
 
     @Override
-    public SalRoleService getSalRoleService() {
-        return this.salRoleService;
+    public CONTEXT_STATE getState() {
+        return this.state;
     }
 
     @Override
-    public Entity getEntity() {
-        return this.entity;
+    public void setState(CONTEXT_STATE state) {
+        this.state = state;
     }
 
     @Override
-    public Entity getTxEntity() {
-        return this.txEntity;
+    public ServiceGroupIdentifier getServiceIdentifier() {
+        return this.deviceInfo.getServiceIdentifier();
     }
 
     @Override
-    public boolean isMainCandidateRegistered() {
-        return entityOwnershipCandidateRegistration != null;
+    public DeviceInfo getDeviceInfo() {
+        return this.deviceInfo;
     }
 
-    @Override
-    public boolean isTxCandidateRegistered() {
-        return txEntityOwnershipCandidateRegistration != null;
+    public void startupClusterServices() throws ExecutionException, InterruptedException {
+        Futures.addCallback(sendRoleChangeToDevice(OfpRole.BECOMEMASTER), new RpcResultFutureCallback());
     }
 
     @Override
-    public boolean registerCandidate(final Entity entity_) {
-        boolean permit = false;
-        try {
-            permit = roleChangeGuard.tryAcquire(TIMEOUT, TimeUnit.SECONDS);
-            if(permit) {
-                LOG.debug("Register candidate for entity {}", entity_);
-                if (entity_.equals(this.entity)) {
-                    entityOwnershipCandidateRegistration = entityOwnershipService.registerCandidate(entity_);
-                } else {
-                    txEntityOwnershipCandidateRegistration = entityOwnershipService.registerCandidate(entity_);
+    public ListenableFuture<Void> stopClusterServices(final boolean deviceDisconnected) {
+
+        if (!deviceDisconnected) {
+            ListenableFuture<Void> future = Futures.transform(makeDeviceSlave(), new Function<RpcResult<SetRoleOutput>, Void>() {
+                @Nullable
+                @Override
+                public Void apply(@Nullable RpcResult<SetRoleOutput> setRoleOutputRpcResult) {
+                    return null;
                 }
-            } else {
-                return false;
-            }
-        } catch (final CandidateAlreadyRegisteredException e) {
-            LOG.warn("Candidate for entity {} is already registered.", entity_.getType());
-            return false;
-        } catch (final InterruptedException e) {
-            LOG.warn("Cannot acquire semaphore for register entity {} candidate.", entity_.getType());
-            return false;
-        } finally {
-            if (permit) {
-                roleChangeGuard.release();
-            }
-        }
-        return true;
-    }
+            });
 
-    @Override
-    public boolean unregisterCandidate(final Entity entity_) {
-        boolean permit = false;
-        try {
-            permit = roleChangeGuard.tryAcquire(TIMEOUT, TimeUnit.SECONDS);
-            if(permit) {
-                if (entity_.equals(this.entity)) {
-                    if (entityOwnershipCandidateRegistration != null) {
-                        LOG.debug("Unregister candidate for entity {}", entity_);
-                        entityOwnershipCandidateRegistration.close();
-                        entityOwnershipCandidateRegistration = null;
-                    }
-                } else {
-                    if (txEntityOwnershipCandidateRegistration != null) {
-                        LOG.debug("Unregister candidate for tx entity {}", entity_);
-                        txEntityOwnershipCandidateRegistration.close();
-                        txEntityOwnershipCandidateRegistration = null;
+            Futures.addCallback(future, new FutureCallback<Void>() {
+                @Override
+                public void onSuccess(@Nullable Void aVoid) {
+                    if (LOG.isDebugEnabled()) {
+                        LOG.debug("Role SLAVE was successfully propagated on device, node {}", deviceInfo.getLOGValue());
                     }
                 }
-            } else {
-                return false;
-            }
-        } catch (final InterruptedException e) {
-            LOG.warn("Cannot acquire semaphore for unregister entity {} candidate.", entity_.getType());
-            return false;
-        } finally {
-            if (permit) {
-                roleChangeGuard.release();
-            }
-        }
-        return true;
-    }
 
-    @Override
-    public void close() {
-        setState(CONTEXT_STATE.TERMINATION);
-        unregisterAllCandidates();
-    }
-
-    public boolean isMaster(){
-        return (txEntityOwnershipCandidateRegistration != null && entityOwnershipCandidateRegistration != null);
+                @Override
+                public void onFailure(final Throwable throwable) {
+                    LOG.warn("Was not able to set role SLAVE to device on node {} ", deviceInfo.getLOGValue());
+                    LOG.trace("Error occurred on device role setting, probably connection loss: ", throwable);
+                    myManager.removeDeviceFromOperationalDS(deviceInfo);
+                }
+            });
+            return future;
+        } else {
+            return myManager.removeDeviceFromOperationalDS(deviceInfo);
+        }
     }
 
     @Override
-    public CONTEXT_STATE getState() {
-        return this.state;
+    public ListenableFuture<RpcResult<SetRoleOutput>> makeDeviceSlave(){
+        return sendRoleChangeToDevice(OfpRole.BECOMESLAVE);
     }
 
-    @Override
-    public void setState(CONTEXT_STATE state) {
-        this.state = state;
+    @VisibleForTesting
+    ListenableFuture<RpcResult<SetRoleOutput>> sendRoleChangeToDevice(final OfpRole newRole) {
+        if (LOG.isDebugEnabled()) {
+            LOG.debug("Sending new role {} to device {}", newRole, deviceInfo.getNodeId());
+        }
+        final Future<RpcResult<SetRoleOutput>> setRoleOutputFuture;
+        if (deviceInfo.getVersion() >= OFConstants.OFP_VERSION_1_3) {
+            final SetRoleInput setRoleInput = (new SetRoleInputBuilder()).setControllerRole(newRole)
+                    .setNode(new NodeRef(deviceInfo.getNodeInstanceIdentifier())).build();
+            setRoleOutputFuture = this.salRoleService.setRole(setRoleInput);
+            final TimerTask timerTask = timeout -> {
+                if (!setRoleOutputFuture.isDone()) {
+                    LOG.warn("New role {} was not propagated to device {} during 5 sec", newRole, deviceInfo.getLOGValue());
+                    setRoleOutputFuture.cancel(true);
+                }
+            };
+            hashedWheelTimer.newTimeout(timerTask, 5, TimeUnit.SECONDS);
+        } else {
+            LOG.info("Device: {} with version: {} does not support role", deviceInfo.getLOGValue(), deviceInfo.getVersion());
+            return Futures.immediateFuture(null);
+        }
+        return JdkFutureAdapters.listenInPoolThread(setRoleOutputFuture);
     }
 
     @Override
-    public ServiceGroupIdentifier getServiceIdentifier() {
-        return this.deviceInfo.getServiceIdentifier();
+    public void setLifecycleInitializationPhaseHandler(final ClusterInitializationPhaseHandler handler) {
+        this.clusterInitializationPhaseHandler = handler;
     }
 
     @Override
-    public DeviceInfo getDeviceInfo() {
-        return this.deviceInfo;
-    }
+    public boolean onContextInstantiateService(final ConnectionContext connectionContext) {
 
-    public void startupClusterServices() throws ExecutionException, InterruptedException {
-        sendRoleChangeToDevice(OfpRole.BECOMEMASTER).get();
-    }
+        if (connectionContext.getConnectionState().equals(ConnectionContext.CONNECTION_STATE.RIP)) {
+            LOG.warn("Connection on device {} was interrupted, will stop starting master services.", deviceInfo.getLOGValue());
+            return false;
+        }
 
-    @Override
-    public ListenableFuture<Void> stopClusterServices() {
-        return Futures.immediateFuture(null);
+        Futures.addCallback(sendRoleChangeToDevice(OfpRole.BECOMEMASTER), new RpcResultFutureCallback());
+        return this.clusterInitializationPhaseHandler.onContextInstantiateService(connectionContext);
     }
 
-    private ListenableFuture<RpcResult<SetRoleOutput>> sendRoleChangeToDevice(final OfpRole newRole) {
-        LOG.debug("Sending new role {} to device {}", newRole, deviceInfo.getNodeId());
-        final Future<RpcResult<SetRoleOutput>> setRoleOutputFuture;
-        final Short version = deviceInfo.getVersion();
-        if (null == version) {
-            LOG.debug("Device version is null");
-            return Futures.immediateFuture(null);
+    private class RpcResultFutureCallback implements FutureCallback<RpcResult<SetRoleOutput>> {
+        @Override
+        public void onSuccess(@Nullable RpcResult<SetRoleOutput> setRoleOutputRpcResult) {
+            if (LOG.isDebugEnabled()) {
+                LOG.debug("Role MASTER was successfully set on device, node {}", deviceInfo.getLOGValue());
+            }
         }
-        if (version < OFConstants.OFP_VERSION_1_3) {
-            LOG.debug("Device version not support ROLE");
-            return Futures.immediateFuture(null);
-        } else {
-            final SetRoleInput setRoleInput = (new SetRoleInputBuilder()).setControllerRole(newRole)
-                    .setNode(new NodeRef(DeviceStateUtil.createNodeInstanceIdentifier(deviceInfo.getNodeId()))).build();
-            setRoleOutputFuture = getSalRoleService().setRole(setRoleInput);
-            final TimerTask timerTask = timeout -> {
-                if (!setRoleOutputFuture.isDone()) {
-                    LOG.warn("New role {} was not propagated to device {} during 10 sec", newRole, deviceInfo.getNodeId());
-                    setRoleOutputFuture.cancel(true);
-                }
-            };
-            conductor.newTimeout(timerTask, 10, TimeUnit.SECONDS);
+
+        @Override
+        public void onFailure(final Throwable throwable) {
+            LOG.warn("Was not able to set MASTER role on device, node {}", deviceInfo.getLOGValue());
+            lifecycleService.closeConnection();
         }
-        return JdkFutureAdapters.listenInPoolThread(setRoleOutputFuture);
     }
-
 }