moving "context not found" log message to INFO level
[openflowplugin.git] / openflowplugin / src / main / java / org / opendaylight / openflowplugin / openflow / md / core / ConnectionConductorImpl.java
index e1d0df49ea8e51df9b97c9f7c7d9a4140a64433e..0e33652697c9ea81a2283cd83b76f098cdbe45b3 100644 (file)
@@ -1,5 +1,5 @@
 /**
- * Copyright (c) 2013-2014 Cisco Systems, Inc. and others.  All rights reserved.
+ * Copyright (c) 2013, 2015 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,14 +8,16 @@
 
 package org.opendaylight.openflowplugin.openflow.md.core;
 
+import com.google.common.annotations.VisibleForTesting;
+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.api.OFConstants;
+import org.opendaylight.openflowplugin.api.openflow.connection.HandshakeContext;
 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;
@@ -65,8 +67,6 @@ import org.opendaylight.yangtools.yang.common.RpcResult;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-import com.google.common.util.concurrent.Futures;
-
 /**
  * @author mirehak
  */
@@ -111,6 +111,7 @@ public class ConnectionConductorImpl implements OpenflowProtocolListener,
     private int conductorId;
 
     private int ingressMaxQueueSize;
+    private HandshakeContext handshakeContext;
 
     /**
      * @param connectionAdapter
@@ -121,11 +122,10 @@ public class ConnectionConductorImpl implements OpenflowProtocolListener,
 
     /**
      * @param connectionAdapter
-     * @param ingressMaxQueueSize
-     *            ingress queue limit (blocking)
+     * @param ingressMaxQueueSize ingress queue limit (blocking)
      */
     public ConnectionConductorImpl(ConnectionAdapter connectionAdapter,
-            int ingressMaxQueueSize) {
+                                   int ingressMaxQueueSize) {
         this.connectionAdapter = connectionAdapter;
         this.ingressMaxQueueSize = ingressMaxQueueSize;
         conductorState = CONDUCTOR_STATE.HANDSHAKING;
@@ -144,7 +144,7 @@ public class ConnectionConductorImpl implements OpenflowProtocolListener,
         hsPool = new ThreadPoolLoggingExecutor(handshakeThreadLimit,
                 handshakeThreadLimit, 0L, TimeUnit.MILLISECONDS,
                 new LinkedBlockingQueue<Runnable>(), "OFHandshake-"
-                        + conductorId);
+                + conductorId);
 
         connectionAdapter.setMessageListener(this);
         connectionAdapter.setSystemListener(this);
@@ -162,8 +162,7 @@ public class ConnectionConductorImpl implements OpenflowProtocolListener,
     }
 
     /**
-     * @param errorHandler
-     *            the errorHandler to set
+     * @param errorHandler the errorHandler to set
      */
     @Override
     public void setErrorHandler(ErrorHandler errorHandler) {
@@ -207,8 +206,7 @@ public class ConnectionConductorImpl implements OpenflowProtocolListener,
 
     /**
      * @param message
-     * @param queueType
-     *            enqueue type
+     * @param queueType enqueue type
      */
     private void enqueueMessage(OfHeader message, QueueType queueType) {
         queue.push(message, this, queueType);
@@ -272,8 +270,11 @@ public class ConnectionConductorImpl implements OpenflowProtocolListener,
 
     @Override
     public void onPortStatusMessage(PortStatusMessage message) {
-        processPortStatusMsg(message);
-        enqueueMessage(message);
+        try {
+            processPortStatusMsg(message);
+        } finally {
+            enqueueMessage(message);
+        }
     }
 
     protected void processPortStatusMsg(PortStatus msg) {
@@ -295,9 +296,14 @@ public class ConnectionConductorImpl implements OpenflowProtocolListener,
                     "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);
+            if (null != this.sessionContext) {
+                //FIXME these two properties are never used in code
+                this.getSessionContext().getPhysicalPorts().put(portNumber, msg);
+                this.getSessionContext().getPortsBandwidth()
+                        .put(portNumber, portBandwidth);
+            } else {
+                LOG.warn("Trying to process update port message before session context was created.");
+            }
         }
     }
 
@@ -365,8 +371,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) {
@@ -383,6 +388,8 @@ public class ConnectionConductorImpl implements OpenflowProtocolListener,
      */
     protected void checkState(CONDUCTOR_STATE expectedState) {
         if (!conductorState.equals(expectedState)) {
+            LOG.warn("State of connection to switch {} is not correct, "
+                    + "terminating the connection", connectionAdapter.getRemoteAddress());
             throw new IllegalStateException("Expected state: " + expectedState
                     + ", actual state:" + conductorState);
         }
@@ -445,6 +452,7 @@ public class ConnectionConductorImpl implements OpenflowProtocolListener,
     public void onConnectionReady() {
         LOG.debug("connection is ready-to-use");
         if (!firstHelloProcessed) {
+            checkState(CONDUCTOR_STATE.HANDSHAKING);
             HandshakeStepWrapper handshakeStepWrapper = new HandshakeStepWrapper(
                     null, handshakeManager, connectionAdapter);
             hsPool.execute(handshakeStepWrapper);
@@ -456,14 +464,12 @@ public class ConnectionConductorImpl implements OpenflowProtocolListener,
 
     @Override
     public void onHandshakeSuccessfull(GetFeaturesOutput featureOutput,
-            Short negotiatedVersion) {
+                                       Short negotiatedVersion) {
         postHandshakeBasic(featureOutput, negotiatedVersion);
 
         // post-handshake actions
         if (version == OFConstants.OFP_VERSION_1_3) {
             requestPorts();
-            requestGroupFeatures();
-            requestMeterFeatures();
         }
 
         requestDesc();
@@ -477,12 +483,12 @@ public class ConnectionConductorImpl implements OpenflowProtocolListener,
 
     /**
      * used by tests
-     * 
+     *
      * @param featureOutput
      * @param negotiatedVersion
      */
     protected void postHandshakeBasic(GetFeaturesOutput featureOutput,
-            Short negotiatedVersion) {
+                                      Short negotiatedVersion) {
         version = negotiatedVersion;
         if (version == OFConstants.OFP_VERSION_1_0) {
             // Because the GetFeaturesOutput contains information about the port
@@ -560,38 +566,57 @@ public class ConnectionConductorImpl implements OpenflowProtocolListener,
     }
 
     /**
-     * @param isBitmapNegotiationEnable
-     *            the isBitmapNegotiationEnable to set
+     * @param isBitmapNegotiationEnable the isBitmapNegotiationEnable to set
      */
     public void setBitmapNegotiationEnable(boolean isBitmapNegotiationEnable) {
         this.isBitmapNegotiationEnable = isBitmapNegotiationEnable;
     }
 
-    protected void shutdownPool() {
-        hsPool.shutdownNow();
-        LOG.debug("pool is terminated: {}", hsPool.isTerminated());
+    @Override
+    public void setId(int conductorId) {
+        this.conductorId = conductorId;
+    }
+
+    @Override
+    public void close() {
+        conductorState = CONDUCTOR_STATE.RIP;
+        if (handshakeContext != null) {
+            try {
+                handshakeContext.close();
+            } catch (Exception e) {
+                LOG.warn("Closing handshake context failed: {}", e.getMessage());
+                LOG.debug("Detail in hanshake context close:", e);
+            }
+        } else {
+            //This condition will occure when Old Helium openflowplugin implementation will be used.
+            shutdownPoolPolitely();
+        }
     }
 
-    protected void shutdownPoolPolitely() {
+    private void shutdownPoolPolitely() {
+        LOG.debug("Terminating handshake pool for node {}", connectionAdapter.getRemoteAddress());
         hsPool.shutdown();
         try {
             hsPool.awaitTermination(1, TimeUnit.SECONDS);
         } catch (InterruptedException e) {
-            LOG.info("Error while awaiting termination on pool. Will use shutdownNow method.");
-            shutdownPool();
+            LOG.debug("Error while awaiting termination of pool. Will force shutdown now.");
+        } finally {
+            hsPool.purge();
+            if (!hsPool.isTerminated()) {
+                hsPool.shutdownNow();
+            }
+            LOG.debug("is handshake pool for node {} is terminated : {}",
+                    connectionAdapter.getRemoteAddress(), hsPool.isTerminated());
         }
-        hsPool.purge();
-        LOG.debug("pool is terminated: {}", hsPool.isTerminated());
     }
 
     @Override
-    public void setId(int conductorId) {
-        this.conductorId = conductorId;
+    public void setHandshakeContext(HandshakeContext handshakeContext) {
+        this.handshakeContext = handshakeContext;
     }
 
-    @Override
-    public void close() {
-        shutdownPoolPolitely();
-        conductorState = CONDUCTOR_STATE.RIP;
+    @VisibleForTesting
+    ThreadPoolExecutor getHsPool() {
+        return hsPool;
     }
 }