Merge "Bug 2525 : remove zombie registrations of notification listener"
[openflowplugin.git] / openflowplugin / src / main / java / org / opendaylight / openflowplugin / openflow / md / core / ConnectionConductorImpl.java
index 98e9037eb18f29ee5161f7be18816f2e9c7098d3..e892f575db90a57bc8be9895d7e17832f1cc351c 100644 (file)
@@ -1,5 +1,5 @@
 /**
- * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
+ * Copyright (c) 2013-2014 Cisco Systems, Inc. and others.  All rights reserved.
  *
  * This program and the accompanying materials are made available under the
  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
@@ -8,19 +8,28 @@
 
 package org.opendaylight.openflowplugin.openflow.md.core;
 
-import java.util.concurrent.ExecutorService;
-import java.util.concurrent.Executors;
+import com.google.common.util.concurrent.Futures;
 import java.util.concurrent.Future;
+import java.util.concurrent.LinkedBlockingQueue;
+import java.util.concurrent.ThreadPoolExecutor;
 import java.util.concurrent.TimeUnit;
-
 import org.opendaylight.openflowjava.protocol.api.connection.ConnectionAdapter;
 import org.opendaylight.openflowjava.protocol.api.connection.ConnectionReadyListener;
-import org.opendaylight.openflowplugin.openflow.md.OFConstants;
+import org.opendaylight.openflowplugin.api.OFConstants;
+import org.opendaylight.openflowplugin.api.openflow.md.core.ConnectionConductor;
+import org.opendaylight.openflowplugin.api.openflow.md.core.ErrorHandler;
+import org.opendaylight.openflowplugin.api.openflow.md.core.HandshakeListener;
+import org.opendaylight.openflowplugin.api.openflow.md.core.NotificationEnqueuer;
+import org.opendaylight.openflowplugin.api.openflow.md.core.NotificationQueueWrapper;
+import org.opendaylight.openflowplugin.api.openflow.md.core.SwitchConnectionDistinguisher;
+import org.opendaylight.openflowplugin.api.openflow.md.core.session.SessionContext;
+import org.opendaylight.openflowplugin.api.openflow.md.core.session.SessionManager;
+import org.opendaylight.openflowplugin.api.openflow.md.queue.QueueKeeper;
+import org.opendaylight.openflowplugin.api.openflow.md.queue.QueueKeeper.QueueType;
+import org.opendaylight.openflowplugin.api.openflow.md.queue.QueueProcessor;
 import org.opendaylight.openflowplugin.openflow.md.core.session.OFSessionUtil;
 import org.opendaylight.openflowplugin.openflow.md.core.session.PortFeaturesUtil;
-import org.opendaylight.openflowplugin.openflow.md.core.session.SessionContext;
-import org.opendaylight.openflowplugin.openflow.md.core.session.SessionManager;
-import org.opendaylight.openflowplugin.openflow.md.queue.QueueKeeper;
+import org.opendaylight.openflowplugin.openflow.md.queue.QueueKeeperFactory;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.MultipartRequestFlags;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.MultipartType;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoInputBuilder;
@@ -37,7 +46,7 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OpenflowProtocolListener;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PacketInMessage;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.Port;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PortGrouping;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PortStatus;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PortStatusMessage;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.request.multipart.request.body.MultipartRequestDescCaseBuilder;
@@ -53,13 +62,16 @@ import org.opendaylight.yangtools.yang.common.RpcResult;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-import com.google.common.util.concurrent.Futures;
-
 /**
  * @author mirehak
  */
 public class ConnectionConductorImpl implements OpenflowProtocolListener,
-        SystemNotificationsListener, ConnectionConductor, ConnectionReadyListener, HandshakeListener {
+        SystemNotificationsListener, ConnectionConductor, ConnectionReadyListener, HandshakeListener, NotificationEnqueuer, AutoCloseable {
+
+    /**
+     * ingress queue limit
+     */
+    private static final int INGRESS_QUEUE_MAX_SIZE = 200;
 
     protected static final Logger LOG = LoggerFactory
             .getLogger(ConnectionConductorImpl.class);
@@ -75,25 +87,39 @@ public class ConnectionConductorImpl implements OpenflowProtocolListener,
     private ConnectionConductor.CONDUCTOR_STATE conductorState;
     private Short version;
 
-    private SwitchConnectionDistinguisher auxiliaryKey;
+    protected SwitchConnectionDistinguisher auxiliaryKey;
 
-    private SessionContext sessionContext;
+    protected SessionContext sessionContext;
 
-    private QueueKeeper<OfHeader, DataObject> queueKeeper;
-    private ExecutorService hsPool;
+    private QueueProcessor<OfHeader, DataObject> queueProcessor;
+    private QueueKeeper<OfHeader> queue;
+    private ThreadPoolExecutor hsPool;
     private HandshakeManager handshakeManager;
 
     private boolean firstHelloProcessed;
-    
+
     private PortFeaturesUtil portFeaturesUtils;
 
+    private int conductorId;
+
+    private int ingressMaxQueueSize;
+
+
     /**
      * @param connectionAdapter
      */
     public ConnectionConductorImpl(ConnectionAdapter connectionAdapter) {
+        this(connectionAdapter, INGRESS_QUEUE_MAX_SIZE);
+    }
+
+    /**
+     * @param connectionAdapter
+     * @param ingressMaxQueueSize ingress queue limit (blocking)
+     */
+    public ConnectionConductorImpl(ConnectionAdapter connectionAdapter, int ingressMaxQueueSize) {
         this.connectionAdapter = connectionAdapter;
+        this.ingressMaxQueueSize = ingressMaxQueueSize;
         conductorState = CONDUCTOR_STATE.HANDSHAKING;
-        hsPool = Executors.newFixedThreadPool(1);
         firstHelloProcessed = false;
         handshakeManager = new HandshakeManagerImpl(connectionAdapter,
                 ConnectionConductor.versionOrder.get(0), ConnectionConductor.versionOrder);
@@ -104,14 +130,20 @@ public class ConnectionConductorImpl implements OpenflowProtocolListener,
 
     @Override
     public void init() {
+        int handshakeThreadLimit = 1;
+        hsPool = new ThreadPoolLoggingExecutor(handshakeThreadLimit, handshakeThreadLimit, 0L,
+                TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>(),
+                "OFHandshake-" + conductorId);
+
         connectionAdapter.setMessageListener(this);
         connectionAdapter.setSystemListener(this);
         connectionAdapter.setConnectionReadyListener(this);
+        queue = QueueKeeperFactory.createFairQueueKeeper(queueProcessor, ingressMaxQueueSize);
     }
 
     @Override
-    public void setQueueKeeper(QueueKeeper<OfHeader, DataObject> queueKeeper) {
-        this.queueKeeper = queueKeeper;
+    public void setQueueProcessor(QueueProcessor<OfHeader, DataObject> queueProcessor) {
+        this.queueProcessor = queueProcessor;
     }
 
     /**
@@ -141,39 +173,58 @@ public class ConnectionConductorImpl implements OpenflowProtocolListener,
 
     @Override
     public void onErrorMessage(ErrorMessage errorMessage) {
-        queueKeeper.push(errorMessage, this);
+        enqueueMessage(errorMessage);
+    }
+
+
+    /**
+     * @param message
+     */
+    private void enqueueMessage(OfHeader message) {
+        enqueueMessage(message, QueueType.DEFAULT);
+    }
+
+    @Override
+    public void enqueueNotification(NotificationQueueWrapper notification) {
+        enqueueMessage(notification);
+    }
+
+    /**
+     * @param message
+     * @param queueType enqueue type
+     */
+    private void enqueueMessage(OfHeader message, QueueType queueType) {
+        queue.push(message, this, queueType);
     }
 
     @Override
     public void onExperimenterMessage(ExperimenterMessage experimenterMessage) {
-        queueKeeper.push(experimenterMessage, this);
+        enqueueMessage(experimenterMessage);
     }
 
     @Override
     public void onFlowRemovedMessage(FlowRemovedMessage message) {
-        queueKeeper.push(message, this);
+        enqueueMessage(message);
     }
 
 
     /**
      * version negotiation happened as per following steps:
      * 1. If HelloMessage version field has same version, continue connection processing.
-     *    If HelloMessage version is lower than supported versions, just disconnect.
+     * If HelloMessage version is lower than supported versions, just disconnect.
      * 2. If HelloMessage contains bitmap and common version found in bitmap
-     *    then continue connection processing. if no common version found, just disconnect.
+     * then continue connection processing. if no common version found, just disconnect.
      * 3. If HelloMessage version is not supported, send HelloMessage with lower supported version.
      * 4. If Hello message received again with not supported version, just disconnect.
-     *
-     *   TODO: Better to handle handshake into a maintainable innerclass which uses State-Pattern.
      */
     @Override
-    public synchronized void onHelloMessage(final HelloMessage hello) {
+    public void onHelloMessage(final HelloMessage hello) {
         LOG.debug("processing HELLO.xid: {}", hello.getXid());
         firstHelloProcessed = true;
         checkState(CONDUCTOR_STATE.HANDSHAKING);
         HandshakeStepWrapper handshakeStepWrapper = new HandshakeStepWrapper(
                 hello, handshakeManager, connectionAdapter);
-        hsPool.execute(handshakeStepWrapper);
+        hsPool.submit(handshakeStepWrapper);
     }
 
     /**
@@ -194,20 +245,20 @@ public class ConnectionConductorImpl implements OpenflowProtocolListener,
 
     @Override
     public void onMultipartReplyMessage(MultipartReplyMessage message) {
-        queueKeeper.push(message, this);
+        enqueueMessage(message);
     }
 
     @Override
     public void onPacketInMessage(PacketInMessage message) {
-        queueKeeper.push(message, this, QueueKeeper.QueueType.UNORDERED);
+        enqueueMessage(message, QueueKeeper.QueueType.UNORDERED);
     }
 
     @Override
     public void onPortStatusMessage(PortStatusMessage message) {
         processPortStatusMsg(message);
-        queueKeeper.push(message, this);
+        enqueueMessage(message);
     }
-    
+
     protected void processPortStatusMsg(PortStatus msg) {
         if (msg.getReason().getIntValue() == 2) {
             updatePort(msg);
@@ -217,22 +268,22 @@ public class ConnectionConductorImpl implements OpenflowProtocolListener,
             deletePort(msg);
         }
     }
-    
+
     protected void updatePort(PortStatus msg) {
-        Long portNumber = msg.getPortNo();        
+        Long portNumber = msg.getPortNo();
         Boolean portBandwidth = portFeaturesUtils.getPortBandwidth(msg);
-        
-        if(portBandwidth == null) {
-            LOG.warn("can't get bandwidth info from port: {}, aborting port update", msg.toString());
+
+        if (portBandwidth == null) {
+            LOG.debug("can't get bandwidth info from port: {}, aborting port update", msg.toString());
         } else {
             this.getSessionContext().getPhysicalPorts().put(portNumber, msg);
-            this.getSessionContext().getPortsBandwidth().put(portNumber, portBandwidth);                   
-        }            
+            this.getSessionContext().getPortsBandwidth().put(portNumber, portBandwidth);
+        }
     }
-    
-    protected void deletePort(Port port) {
+
+    protected void deletePort(PortGrouping port) {
         Long portNumber = port.getPortNo();
-        
+
         this.getSessionContext().getPhysicalPorts().remove(portNumber);
         this.getSessionContext().getPortsBandwidth().remove(portNumber);
     }
@@ -248,7 +299,7 @@ public class ConnectionConductorImpl implements OpenflowProtocolListener,
                     disconnect();
                     OFSessionUtil.getSessionManager().invalidateOnDisconnect(ConnectionConductorImpl.this);
                 } else {
-                    LOG.debug("first idle state occured");
+                    LOG.debug("first idle state occured, sessionCtx={}|auxId={}", sessionContext, auxiliaryKey);
                     EchoInputBuilder builder = new EchoInputBuilder();
                     builder.setVersion(getVersion());
                     builder.setXid(getSessionContext().getNextXid());
@@ -286,8 +337,7 @@ public class ConnectionConductorImpl implements OpenflowProtocolListener,
     }
 
     /**
-     * @param conductorState
-     *            the connectionState to set
+     * @param conductorState the connectionState to set
      */
     @Override
     public void setConductorState(CONDUCTOR_STATE conductorState) {
@@ -300,7 +350,7 @@ public class ConnectionConductorImpl implements OpenflowProtocolListener,
     }
 
     /**
-     * @param handshaking
+     * @param expectedState
      */
     protected void checkState(CONDUCTOR_STATE expectedState) {
         if (!conductorState.equals(expectedState)) {
@@ -313,6 +363,7 @@ public class ConnectionConductorImpl implements OpenflowProtocolListener,
     public void onDisconnectEvent(DisconnectEvent arg0) {
         SessionManager sessionManager = OFSessionUtil.getSessionManager();
         sessionManager.invalidateOnDisconnect(this);
+        close();
     }
 
     @Override
@@ -322,7 +373,7 @@ public class ConnectionConductorImpl implements OpenflowProtocolListener,
 
     @Override
     public Future<Boolean> disconnect() {
-        LOG.info("disconnecting: sessionCtx="+sessionContext+"|auxId="+auxiliaryKey);
+        LOG.trace("disconnecting: sessionCtx={}|auxId={}", sessionContext, auxiliaryKey);
 
         Future<Boolean> result = null;
         if (connectionAdapter.isAlive()) {
@@ -331,7 +382,7 @@ public class ConnectionConductorImpl implements OpenflowProtocolListener,
             LOG.debug("connection already disconnected");
             result = Futures.immediateFuture(true);
         }
-
+        close();
         return result;
     }
 
@@ -361,9 +412,9 @@ public class ConnectionConductorImpl implements OpenflowProtocolListener,
     }
 
     @Override
-    public synchronized void onConnectionReady() {
+    public void onConnectionReady() {
         LOG.debug("connection is ready-to-use");
-        if (! firstHelloProcessed) {
+        if (!firstHelloProcessed) {
             HandshakeStepWrapper handshakeStepWrapper = new HandshakeStepWrapper(
                     null, handshakeManager, connectionAdapter);
             hsPool.execute(handshakeStepWrapper);
@@ -375,22 +426,53 @@ public class ConnectionConductorImpl implements OpenflowProtocolListener,
 
     @Override
     public void onHandshakeSuccessfull(GetFeaturesOutput featureOutput,
-            Short negotiatedVersion) {
-        version = negotiatedVersion;
-        conductorState = CONDUCTOR_STATE.WORKING;
-        OFSessionUtil.registerSession(this, featureOutput, negotiatedVersion);
-        requestDesc();
-        requestPorts();
-        if(version == OFConstants.OFP_VERSION_1_3){
+                                       Short negotiatedVersion) {
+        postHandshakeBasic(featureOutput, negotiatedVersion);
+
+        // post-handshake actions
+        if (version == OFConstants.OFP_VERSION_1_3) {
+            requestPorts();
             requestGroupFeatures();
             requestMeterFeatures();
         }
+
+        requestDesc();
+    }
+
+    @Override
+    public void onHandshakeFailure() {
+        LOG.info("OF handshake failed, doing cleanup.");
+        close();
+    }
+
+    /**
+     * used by tests
+     *
+     * @param featureOutput
+     * @param negotiatedVersion
+     */
+    protected void postHandshakeBasic(GetFeaturesOutput featureOutput,
+                                      Short negotiatedVersion) {
+        version = negotiatedVersion;
+        if (version == OFConstants.OFP_VERSION_1_0) {
+            //  Because the GetFeaturesOutput contains information about the port
+            //  in OF1.0 (that we would otherwise get from the PortDesc) we have to pass
+            //  it up for parsing to convert into a NodeConnectorUpdate
+            //
+            // BUG-1988 - this must be the first item in queue in order not to get behind link-up message
+            enqueueMessage(featureOutput);
+        }
+
+        OFSessionUtil.registerSession(this, featureOutput, negotiatedVersion);
+        hsPool.shutdown();
+        hsPool.purge();
+        conductorState = CONDUCTOR_STATE.WORKING;
+        QueueKeeperFactory.plugQueue(queueProcessor, queue);
     }
 
     /*
      *  Send an OFPMP_DESC request message to the switch
      */
-
     private void requestDesc() {
         MultipartRequestInputBuilder builder = new MultipartRequestInputBuilder();
         builder.setType(MultipartType.OFPMPDESC);
@@ -410,22 +492,24 @@ public class ConnectionConductorImpl implements OpenflowProtocolListener,
         builder.setXid(getSessionContext().getNextXid());
         getConnectionAdapter().multipartRequest(builder.build());
     }
-    private void requestGroupFeatures(){
+
+    private void requestGroupFeatures() {
         MultipartRequestInputBuilder mprInput = new MultipartRequestInputBuilder();
         mprInput.setType(MultipartType.OFPMPGROUPFEATURES);
         mprInput.setVersion(getVersion());
         mprInput.setFlags(new MultipartRequestFlags(false));
         mprInput.setXid(getSessionContext().getNextXid());
 
-        MultipartRequestGroupFeaturesCaseBuilder mprGroupFeaturesBuild = 
+        MultipartRequestGroupFeaturesCaseBuilder mprGroupFeaturesBuild =
                 new MultipartRequestGroupFeaturesCaseBuilder();
         mprInput.setMultipartRequestBody(mprGroupFeaturesBuild.build());
 
-        LOG.debug("Send group features statistics request :{}",mprGroupFeaturesBuild);
+        LOG.debug("Send group features statistics request :{}", mprGroupFeaturesBuild);
         getConnectionAdapter().multipartRequest(mprInput.build());
-        
+
     }
-    private void requestMeterFeatures(){
+
+    private void requestMeterFeatures() {
         MultipartRequestInputBuilder mprInput = new MultipartRequestInputBuilder();
         mprInput.setType(MultipartType.OFPMPMETERFEATURES);
         mprInput.setVersion(getVersion());
@@ -436,10 +520,11 @@ public class ConnectionConductorImpl implements OpenflowProtocolListener,
                 new MultipartRequestMeterFeaturesCaseBuilder();
         mprInput.setMultipartRequestBody(mprMeterFeaturesBuild.build());
 
-        LOG.debug("Send meter features statistics request :{}",mprMeterFeaturesBuild);
+        LOG.debug("Send meter features statistics request :{}", mprMeterFeaturesBuild);
         getConnectionAdapter().multipartRequest(mprInput.build());
-        
+
     }
+
     /**
      * @param isBitmapNegotiationEnable the isBitmapNegotiationEnable to set
      */
@@ -452,4 +537,27 @@ public class ConnectionConductorImpl implements OpenflowProtocolListener,
         hsPool.shutdownNow();
         LOG.debug("pool is terminated: {}", hsPool.isTerminated());
     }
+
+    protected void shutdownPoolPolitely() {
+        hsPool.shutdown();
+        try {
+            hsPool.awaitTermination(1, TimeUnit.SECONDS);
+        } catch (InterruptedException e) {
+            LOG.info("Error while awaiting termination on pool. Will use shutdownNow method.");
+            shutdownPool();
+        }
+        hsPool.purge();
+        LOG.debug("pool is terminated: {}", hsPool.isTerminated());
+    }
+
+    @Override
+    public void setId(int conductorId) {
+        this.conductorId = conductorId;
+    }
+
+    @Override
+    public void close() {
+        shutdownPoolPolitely();
+        conductorState = CONDUCTOR_STATE.RIP;
+    }
 }