Fix auxiliary connections
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / lifecycle / ContextChainImpl.java
index 477c528c740ff72c1dd524ec746febe26bae77b7..24a07c967fc6c438129432105d2e039679ceb834 100644 (file)
@@ -10,10 +10,11 @@ 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.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceProvider;
@@ -24,6 +25,7 @@ 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.ContextChainStateListener;
 import org.opendaylight.openflowplugin.api.openflow.lifecycle.LifecycleService;
 import org.opendaylight.openflowplugin.api.openflow.rpc.RpcContext;
 import org.opendaylight.openflowplugin.api.openflow.statistics.StatisticsContext;
@@ -34,27 +36,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 LifecycleService lifecycleService;
     private DeviceInfo deviceInfo;
+    private ConnectionContext primaryConnection;
+    private Set<ConnectionContext> auxiliaryConnections = new ConcurrentSet<>();
 
     private volatile ContextChainState contextChainState;
 
-    private boolean masterStateOnDevice;
-    private boolean initialGathering;
-    private boolean initialSubmitting;
-    private boolean registryFilling;
-
-    public ContextChainImpl(final DeviceInfo deviceInfo) {
-        this.contextChainState = ContextChainState.INITIALIZED;
-        this.masterStateOnDevice = false;
-        this.initialGathering = false;
-        this.initialSubmitting = false;
-        this.registryFilling = false;
-        this.deviceInfo = deviceInfo;
+    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
@@ -70,6 +75,7 @@ public class ContextChainImpl implements ContextChain {
                 }
             }
         }
+
         contexts.add(context);
     }
 
@@ -79,13 +85,13 @@ 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
@@ -96,8 +102,19 @@ public class ContextChainImpl implements ContextChain {
         });
     }
 
+    private void unMasterMe() {
+        this.registryFilling.set(false);
+        this.initialSubmitting.set(false);
+        this.initialGathering.set(false);
+        this.masterStateOnDevice.set(false);
+    }
+
     @Override
     public void close() {
+        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();
@@ -106,14 +123,16 @@ public class ContextChainImpl implements ContextChain {
 
     @Override
     public void makeContextChainStateSlave() {
-        this.contextChainState = ContextChainState.WORKING_SLAVE;
+        this.unMasterMe();
+        changeState(ContextChainState.WORKING_SLAVE);
     }
 
     @Override
     public ListenableFuture<Void> connectionDropped() {
         if (this.contextChainState == ContextChainState.WORKING_MASTER) {
-            return this.stopChain(true);
+            return this.stopChain();
         }
+        this.unMasterMe();
         return Futures.immediateFuture(null);
     }
 
@@ -126,6 +145,7 @@ public class ContextChainImpl implements ContextChain {
 
     @Override
     public void makeDeviceSlave() {
+        this.unMasterMe();
         this.lifecycleService.makeDeviceSlave(this.deviceContext);
     }
 
@@ -134,32 +154,75 @@ public class ContextChainImpl implements ContextChain {
         switch (mastershipState) {
             case INITIAL_SUBMIT:
                 LOG.debug("Device {}, initial submit OK.", deviceInfo.getLOGValue());
-                this.initialSubmitting = true;
+                this.initialSubmitting.set(true);
                 break;
             case MASTER_ON_DEVICE:
                 LOG.debug("Device {}, master state OK.", deviceInfo.getLOGValue());
-                this.masterStateOnDevice = true;
+                this.masterStateOnDevice.set(true);
                 break;
             case INITIAL_GATHERING:
                 LOG.debug("Device {}, initial gathering OK.", deviceInfo.getLOGValue());
-                this.initialGathering = true;
+                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 = true;
+                this.registryFilling.set(true);
             case CHECK:
             default:
         }
         final boolean result =
-                this.initialGathering &&
-                this.masterStateOnDevice &&
-                this.initialSubmitting &&
-                this.registryFilling;
+                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());
-            contextChainState = ContextChainState.WORKING_MASTER;
+            LOG.info("Device {} is able to work as master{}",
+                    deviceInfo.getLOGValue(),
+                    this.registryFilling.get() ? " WITHOUT flow registry !!!" : ".");
+            changeState(ContextChainState.WORKING_MASTER);
         }
         return result;
     }
+
+    @Override
+    public boolean hasState() {
+        return contextChainState == ContextChainState.WORKING_MASTER
+                || contextChainState == ContextChainState.WORKING_SLAVE;
+    }
+
+    @Override
+    public boolean addAuxiliaryConnection(@Nonnull ConnectionContext connectionContext) {
+        if ((connectionContext.getFeatures().getAuxiliaryId() != 0) &&
+                (this.primaryConnection.getConnectionState() != ConnectionContext.CONNECTION_STATE.RIP)) {
+            this.auxiliaryConnections.add(connectionContext);
+            return true;
+        } else {
+            return false;
+        }
+    }
+
+    @Override
+    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;
+    }
+
+    private void changeState(final ContextChainState contextChainState) {
+        boolean propagate = this.contextChainState == ContextChainState.UNDEFINED;
+        this.contextChainState = contextChainState;
+
+        if (propagate) {
+            contexts.stream()
+                    .filter(ContextChainStateListener.class::isInstance)
+                    .map(ContextChainStateListener.class::cast)
+                    .forEach(listener -> listener.onStateAcquired(contextChainState));
+        }
+    }
 }