Fix queue and xid issue
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / lifecycle / ContextChainImpl.java
index c43e759e77b19c064cd04cdf6fa1afaad01f591b..b5894b5705b4e7fb51e8c17e51f6bea412b420dd 100644 (file)
@@ -10,21 +10,24 @@ package org.opendaylight.openflowplugin.impl.lifecycle;
 import com.google.common.base.Function;
 import com.google.common.util.concurrent.Futures;
 import com.google.common.util.concurrent.ListenableFuture;
+import io.netty.util.internal.ConcurrentSet;
 import java.util.ArrayList;
-import java.util.HashSet;
 import java.util.List;
 import java.util.Set;
+import java.util.concurrent.atomic.AtomicBoolean;
+import javax.annotation.Nonnull;
 import javax.annotation.Nullable;
-import org.eclipse.jdt.annotation.NonNull;
 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceProvider;
 import org.opendaylight.openflowplugin.api.openflow.OFPContext;
 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
+import org.opendaylight.openflowplugin.api.openflow.device.DeviceInfo;
 import org.opendaylight.openflowplugin.api.openflow.lifecycle.ContextChain;
+import org.opendaylight.openflowplugin.api.openflow.lifecycle.ContextChainMastershipState;
+import org.opendaylight.openflowplugin.api.openflow.lifecycle.ContextChainState;
 import org.opendaylight.openflowplugin.api.openflow.lifecycle.LifecycleService;
 import org.opendaylight.openflowplugin.api.openflow.rpc.RpcContext;
 import org.opendaylight.openflowplugin.api.openflow.statistics.StatisticsContext;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflow.provider.config.rev160510.ContextChainState;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -32,21 +35,30 @@ public class ContextChainImpl implements ContextChain {
 
     private static final Logger LOG = LoggerFactory.getLogger(ContextChainImpl.class);
 
-    private Set<OFPContext> contexts = new HashSet<>();
+    private Set<OFPContext> contexts = new ConcurrentSet<>();
     private StatisticsContext statisticsContext;
     private DeviceContext deviceContext;
     private RpcContext rpcContext;
-    private volatile ContextChainState contextChainState;
     private LifecycleService lifecycleService;
-    private ConnectionContext primaryConnectionContext;
+    private DeviceInfo deviceInfo;
+    private ConnectionContext primaryConnection;
+    private Set<ConnectionContext> auxiliaryConnections = new ConcurrentSet<>();
 
-    public ContextChainImpl() {
-        this.contextChainState = ContextChainState.INITIALIZED;
-    }
+    private volatile ContextChainState contextChainState;
 
-    @Override
-    public boolean isReady() {
-        return false;
+    private AtomicBoolean masterStateOnDevice;
+    private AtomicBoolean initialGathering;
+    private AtomicBoolean initialSubmitting;
+    private AtomicBoolean registryFilling;
+
+    ContextChainImpl(final ConnectionContext connectionContext) {
+        this.primaryConnection = connectionContext;
+        this.contextChainState = ContextChainState.UNDEFINED;
+        this.masterStateOnDevice = new AtomicBoolean(false);
+        this.initialGathering = new AtomicBoolean(false);
+        this.initialSubmitting = new AtomicBoolean(false);
+        this.registryFilling = new AtomicBoolean(false);
+        this.deviceInfo = connectionContext.getDeviceInfo();
     }
 
     @Override
@@ -71,103 +83,131 @@ public class ContextChainImpl implements ContextChain {
     }
 
     @Override
-    public ListenableFuture<Void> stopChain(boolean connectionDropped) {
+    public ListenableFuture<Void> stopChain() {
         //TODO: stopClusterServices change parameter
         final List<ListenableFuture<Void>> futureList = new ArrayList<>();
         futureList.add(statisticsContext.stopClusterServices());
         futureList.add(rpcContext.stopClusterServices());
-        futureList.add(deviceContext.stopClusterServices(connectionDropped));
-
+        futureList.add(deviceContext.stopClusterServices());
+        this.unMasterMe();
         return Futures.transform(Futures.successfulAsList(futureList), new Function<List<Void>, Void>() {
             @Nullable
             @Override
             public Void apply(@Nullable List<Void> input) {
-                LOG.debug("Closed clustering MASTER services for node {}", deviceContext.getDeviceInfo().getLOGValue());
-                contextChainState = ContextChainState.WORKINGSLAVE;
+                LOG.info("Closed clustering MASTER services for node {}", deviceContext.getDeviceInfo().getLOGValue());
                 return null;
             }
         });
     }
 
-    @Override
-    public ListenableFuture<Void> startChain() {
-        if (ContextChainState.INITIALIZED.equals(this.contextChainState)) {
-            return Futures.transform(this.statisticsContext.initialGatherDynamicData(), new Function<Boolean, Void>() {
-                @Nullable
-                @Override
-                public Void apply(@Nullable Boolean aBoolean) {
-                    contextChainState = ContextChainState.WORKINGMASTER;
-                    return null;
-                }
-            });
-        } else {
-            this.contextChainState = ContextChainState.WORKINGMASTER;
-        }
-        return Futures.immediateFuture(null);
+    private void unMasterMe() {
+        this.registryFilling.set(false);
+        this.initialSubmitting.set(false);
+        this.initialGathering.set(false);
+        this.masterStateOnDevice.set(false);
     }
 
     @Override
     public void close() {
-
-    }
-
-    @Override
-    public void changePrimaryConnection(final ConnectionContext connectionContext) {
-        this.primaryConnectionContext = connectionContext;
-        this.contextChainState = ContextChainState.INITIALIZED;
-        for (OFPContext context : contexts) {
-            context.replaceConnection(connectionContext);
+        this.auxiliaryConnections.forEach(connectionContext -> connectionContext.closeConnection(false));
+        if (this.primaryConnection.getConnectionState() != ConnectionContext.CONNECTION_STATE.RIP) {
+            this.primaryConnection.closeConnection(true);
         }
+        lifecycleService.close();
+        deviceContext.close();
+        rpcContext.close();
+        statisticsContext.close();
     }
 
     @Override
-    public ContextChainState getContextChainState() {
-        return contextChainState;
+    public void makeContextChainStateSlave() {
+        this.unMasterMe();
+        this.contextChainState = ContextChainState.WORKING_SLAVE;
     }
 
     @Override
     public ListenableFuture<Void> connectionDropped() {
-        ContextChainState oldState = this.contextChainState;
-        this.contextChainState = ContextChainState.SLEEPING;
-        if (oldState.equals(ContextChainState.WORKINGMASTER)) {
-            return this.stopChain(true);
+        if (this.contextChainState == ContextChainState.WORKING_MASTER) {
+            return this.stopChain();
         }
+        this.unMasterMe();
         return Futures.immediateFuture(null);
     }
 
     @Override
-    public ConnectionContext getPrimaryConnectionContext() {
-        return primaryConnectionContext;
+    public void registerServices(final ClusterSingletonServiceProvider clusterSingletonServiceProvider) {
+        this.lifecycleService.registerService(
+                clusterSingletonServiceProvider,
+                this.deviceContext);
     }
 
     @Override
-    public void sleepTheChainAndDropConnection() {
-        this.contextChainState = ContextChainState.SLEEPING;
-        this.primaryConnectionContext.closeConnection(true);
+    public void makeDeviceSlave() {
+        this.unMasterMe();
+        this.lifecycleService.makeDeviceSlave(this.deviceContext);
     }
 
     @Override
-    public void registerServices(@NonNull final ClusterSingletonServiceProvider clusterSingletonServiceProvider) {
-        this.contextChainState = ContextChainState.WORKINGSLAVE;
-        this.lifecycleService.registerService(
-                clusterSingletonServiceProvider,
-                this.deviceContext,
-                this.deviceContext.getServiceIdentifier(),
-                this.deviceContext.getDeviceInfo());
+    public boolean isMastered(@Nonnull ContextChainMastershipState mastershipState) {
+        switch (mastershipState) {
+            case INITIAL_SUBMIT:
+                LOG.debug("Device {}, initial submit OK.", deviceInfo.getLOGValue());
+                this.initialSubmitting.set(true);
+                break;
+            case MASTER_ON_DEVICE:
+                LOG.debug("Device {}, master state OK.", deviceInfo.getLOGValue());
+                this.masterStateOnDevice.set(true);
+                break;
+            case INITIAL_GATHERING:
+                LOG.debug("Device {}, initial gathering OK.", deviceInfo.getLOGValue());
+                this.initialGathering.set(true);
+                break;
+            //Flow registry fill is not mandatory to work as a master
+            case INITIAL_FLOW_REGISTRY_FILL:
+                LOG.debug("Device {}, initial registry filling OK.", deviceInfo.getLOGValue());
+                this.registryFilling.set(true);
+            case CHECK:
+            default:
+        }
+        final boolean result =
+                this.initialGathering.get() &&
+                this.masterStateOnDevice.get() &&
+                this.initialSubmitting.get();
+
+        if (result && mastershipState != ContextChainMastershipState.CHECK) {
+            LOG.info("Device {} is able to work as master{}",
+                    deviceInfo.getLOGValue(),
+                    this.registryFilling.get() ? " WITHOUT flow registry !!!" : ".");
+            contextChainState = ContextChainState.WORKING_MASTER;
+        }
+        return result;
     }
 
     @Override
-    public void makeDeviceSlave() {
-        this.lifecycleService.makeDeviceSlave(this.deviceContext);
+    public boolean hasState() {
+        return contextChainState == ContextChainState.WORKING_MASTER
+                || contextChainState == ContextChainState.WORKING_SLAVE;
     }
 
     @Override
-    public void closePrimaryConnection() {
-        this.primaryConnectionContext.closeConnection(true);
+    public boolean addAuxiliaryConnection(@Nonnull ConnectionContext connectionContext) {
+        if (this.primaryConnection.getConnectionState() != ConnectionContext.CONNECTION_STATE.RIP) {
+            this.auxiliaryConnections.add(connectionContext);
+            return true;
+        } else {
+            return false;
+        }
     }
 
     @Override
-    public DeviceContext provideDeviceContext() {
-        return this.deviceContext;
+    public boolean auxiliaryConnectionDropped(@Nonnull ConnectionContext connectionContext) {
+        if (this.auxiliaryConnections.isEmpty()) {
+            return false;
+        }
+        if (!this.auxiliaryConnections.contains(connectionContext)) {
+            return false;
+        }
+        this.auxiliaryConnections.remove(connectionContext);
+        return true;
     }
 }