Bug 5596 - restart devices management improvement
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / role / RoleContextImpl.java
index 06305e1879d4957150b7a19338897e2522f0a5b0..888a4e9590f5853bde315c33c6bf9094465093cd 100644 (file)
@@ -7,24 +7,27 @@
  */
 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.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.controller.sal.binding.api.RpcProviderRegistry;
-import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
-import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
+import org.opendaylight.mdsal.singleton.common.api.ServiceGroupIdentifier;
+import org.opendaylight.openflowplugin.api.OFConstants;
+import org.opendaylight.openflowplugin.api.openflow.device.DeviceInfo;
 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.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;
@@ -36,148 +39,150 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 /**
- * Created by kramesha on 9/12/15.
+ * Role context hold information about entity ownership registration,
+ * register and unregister candidate (main and tx)
  */
-public class RoleContextImpl implements RoleContext {
-    private static final Logger LOG = LoggerFactory.getLogger(RoleContextImpl.class);
-
-    private EntityOwnershipService entityOwnershipService;
-    private EntityOwnershipCandidateRegistration entityOwnershipCandidateRegistration;
-    private final RpcProviderRegistry rpcProviderRegistry;
-    private DeviceContext deviceContext;
-    private Entity entity;
-    private OpenflowOwnershipListener openflowOwnershipListener;
-    private SalRoleService salRoleService;
-    private FutureCallback<Boolean> roleChangeCallback;
-
-
-    public RoleContextImpl(DeviceContext deviceContext, RpcProviderRegistry rpcProviderRegistry,
-                           EntityOwnershipService entityOwnershipService, OpenflowOwnershipListener openflowOwnershipListener) {
-        this.entityOwnershipService = entityOwnershipService;
-        this.rpcProviderRegistry = rpcProviderRegistry;
-        this.deviceContext = deviceContext;
-        entity = new Entity(RoleManager.ENTITY_TYPE, deviceContext.getPrimaryConnectionContext().getNodeId().getValue());
+class RoleContextImpl implements RoleContext {
 
-        this.openflowOwnershipListener =  openflowOwnershipListener;
-        salRoleService = new SalRoleServiceImpl(this, deviceContext);
-
-        //make a call to entity ownership service and listen for notifications from the service
-        requestOpenflowEntityOwnership();
+    private static final Logger LOG = LoggerFactory.getLogger(RoleContextImpl.class);
+    // Maximum limit of timeout retries when cleaning DS, to prevent infinite recursive loops
+    private static final int MAX_CLEAN_DS_RETRIES = 3;
+
+    private SalRoleService salRoleService = null;
+    private final HashedWheelTimer hashedWheelTimer;
+    private final DeviceInfo deviceInfo;
+    private CONTEXT_STATE state;
+    private final RoleManager myManager;
+
+    RoleContextImpl(final DeviceInfo deviceInfo,
+                    final HashedWheelTimer hashedWheelTimer,
+                    final RoleManager myManager) {
+        this.deviceInfo = deviceInfo;
+        state = CONTEXT_STATE.WORKING;
+        this.myManager = myManager;
+        this.hashedWheelTimer = hashedWheelTimer;
     }
 
+    @Nullable
     @Override
-    public void facilitateRoleChange(FutureCallback<Boolean> roleChangeCallback) {
-        this.roleChangeCallback = roleChangeCallback;
-        if (!isDeviceConnected()) {
-            throw new IllegalStateException(
-                    "Device is disconnected. Giving up on Role Change:" + deviceContext.getDeviceState().getNodeId());
-        }
+    public <T> RequestContext<T> createRequestContext() {
+        return new AbstractRequestContext<T>(deviceInfo.reserveXidForDeviceMessage()) {
+            @Override
+            public void close() {
+            }
+        };
     }
 
-    private void requestOpenflowEntityOwnership() {
-
-        LOG.debug("requestOpenflowEntityOwnership for entity {}", entity);
-        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 (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 );
-        }
+    @Override
+    public void setSalRoleService(@Nonnull final SalRoleService salRoleService) {
+        Preconditions.checkNotNull(salRoleService);
+        this.salRoleService = salRoleService;
     }
 
     @Override
-    public void onRoleChanged(final OfpRole oldRole, final OfpRole newRole) {
+    public SalRoleService getSalRoleService() {
+        return this.salRoleService;
+    }
 
-        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());
-            return;
-        }
+    @Override
+    public CONTEXT_STATE getState() {
+        return this.state;
+    }
 
-        LOG.debug("Role change received from ownership listener from {} to {} for device:{}", oldRole, newRole,
-                deviceContext.getPrimaryConnectionContext().getNodeId());
+    @Override
+    public void setState(CONTEXT_STATE state) {
+        this.state = state;
+    }
 
-        final SetRoleInput setRoleInput = (new SetRoleInputBuilder())
-                .setControllerRole(newRole)
-                .setNode(new NodeRef(deviceContext.getDeviceState().getNodeInstanceIdentifier()))
-                .build();
+    @Override
+    public ServiceGroupIdentifier getServiceIdentifier() {
+        return this.deviceInfo.getServiceIdentifier();
+    }
 
-        Future<RpcResult<SetRoleOutput>> setRoleOutputFuture = salRoleService.setRole(setRoleInput);
+    @Override
+    public DeviceInfo getDeviceInfo() {
+        return this.deviceInfo;
+    }
 
-        Futures.addCallback(JdkFutureAdapters.listenInPoolThread(setRoleOutputFuture), new FutureCallback<RpcResult<SetRoleOutput>>() {
+    public void startupClusterServices() throws ExecutionException, InterruptedException {
+        Futures.addCallback(sendRoleChangeToDevice(OfpRole.BECOMEMASTER), new FutureCallback<RpcResult<SetRoleOutput>>() {
             @Override
-            public void onSuccess(RpcResult<SetRoleOutput> setRoleOutputRpcResult) {
-                LOG.debug("Rolechange {} successful made on switch :{}", newRole,
-                        deviceContext.getPrimaryConnectionContext().getNodeId());
-                deviceContext.getDeviceState().setRole(newRole);
-                if (roleChangeCallback != null) {
-                    roleChangeCallback.onSuccess(true);
+            public void onSuccess(@Nullable RpcResult<SetRoleOutput> setRoleOutputRpcResult) {
+                if (LOG.isDebugEnabled()) {
+                    LOG.debug("Role MASTER was successfully set on device, node {}", deviceInfo.getLOGValue());
                 }
             }
 
             @Override
-            public void onFailure(Throwable throwable) {
-                LOG.error("Error in setRole {} for device {} ", newRole,
-                        deviceContext.getPrimaryConnectionContext().getNodeId(), throwable);
-                if (roleChangeCallback != null) {
-                    roleChangeCallback.onFailure(throwable);
-                }
+            public void onFailure(final Throwable throwable) {
+                LOG.warn("Was not able to set MASTER role on device, node {}", deviceInfo.getLOGValue());
             }
         });
     }
 
     @Override
-    public void close() throws Exception {
-        if (entityOwnershipCandidateRegistration != null) {
-            LOG.debug("Closing EntityOwnershipCandidateRegistration for {}", entity);
-            entityOwnershipCandidateRegistration.close();
-        }
-    }
+    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;
+                }
+            });
+
+            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());
+                    }
+                }
 
-    @Override
-    public void onDeviceContextClosed(DeviceContext deviceContext) {
-        try {
-            LOG.debug("onDeviceContextClosed called");
-            this.close();
-        } catch (Exception e) {
-            LOG.error("Exception in onDeviceContextClosed of RoleContext", e);
-        }
-    }
+                @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, MAX_CLEAN_DS_RETRIES);
 
-    @Override
-    public Entity getEntity() {
-        return entity;
+                }
+            });
+            return future;
+        } else {
+            return myManager.removeDeviceFromOperationalDS(deviceInfo, MAX_CLEAN_DS_RETRIES);
+        }
     }
 
     @Override
-    public void onDeviceDisconnectedFromCluster() {
-        LOG.debug("Called onDeviceDisconnectedFromCluster in DeviceContext for entity:{}", entity);
-        deviceContext.onDeviceDisconnectedFromCluster();
-    }
-
-    private boolean isDeviceConnected() {
-        return ConnectionContext.CONNECTION_STATE.WORKING.equals(
-                deviceContext.getPrimaryConnectionContext().getConnectionState());
+    public ListenableFuture<RpcResult<SetRoleOutput>> makeDeviceSlave(){
+        return sendRoleChangeToDevice(OfpRole.BECOMESLAVE);
     }
 
-    @Nullable
-    @Override
-    public <T> RequestContext<T> createRequestContext() {
-        final AbstractRequestContext<T> ret = new AbstractRequestContext<T>(deviceContext.getReservedXid()) {
-            @Override
-            public void close() {
-            }
-        };
-        return ret;
+    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);
+        }
+        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.getLOGValue());
+                    setRoleOutputFuture.cancel(true);
+                }
+            };
+            hashedWheelTimer.newTimeout(timerTask, 10, TimeUnit.SECONDS);
+        }
+        return JdkFutureAdapters.listenInPoolThread(setRoleOutputFuture);
     }
 
-    @VisibleForTesting
-    public void setSalRoleService(SalRoleService salRoleService) {
-        this.salRoleService = salRoleService;
-    }
 }