BUG-1764: default config for OF switch - fix
[openflowplugin.git] / openflowplugin / src / main / java / org / opendaylight / openflowplugin / openflow / md / core / HandshakeManagerImpl.java
index 09ac196a32a2d8216d84f7e00f8440e647739bca..dd20aafb2dace6f93a5995ea0a93258ca9630545 100644 (file)
@@ -10,8 +10,13 @@ package org.opendaylight.openflowplugin.openflow.md.core;
 import java.util.List;
 import java.util.concurrent.Future;
 import java.util.concurrent.TimeUnit;
+import java.util.concurrent.TimeoutException;
 
 import org.opendaylight.openflowjava.protocol.api.connection.ConnectionAdapter;
+import org.opendaylight.openflowplugin.ConnectionException;
+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.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesInputBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutput;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.HelloInput;
@@ -40,7 +45,7 @@ public class HandshakeManagerImpl implements HandshakeManager {
     private Short version;
     private ErrorHandler errorHandler;
     
-    private long maxTimeout = 1000;
+    private long maxTimeout = 8000;
     private TimeUnit maxTimeoutUnit = TimeUnit.MILLISECONDS;
     private Short highestVersion;
 
@@ -73,7 +78,16 @@ public class HandshakeManagerImpl implements HandshakeManager {
     }
 
     @Override
-    public synchronized void shake() {
+    public void shake() {
+
+        if (version != null) {
+            // Some switches respond with a second HELLO acknowledging our HELLO
+            // but we've already completed the handshake based on the negotiated
+            // version and have registered this switch.
+            LOG.debug("Hello recieved after handshake already settled ... ignoring.");
+            return;
+        }
+
         LOG.trace("handshake STARTED");
         setActiveXid(20L);
         HelloMessage receivedHelloLoc = receivedHello;
@@ -83,7 +97,7 @@ public class HandshakeManagerImpl implements HandshakeManager {
                 // first Hello sending
                 sendHelloMessage(highestVersion, getNextXid());
                 lastProposedVersion = highestVersion;
-                LOG.debug("ret - firstHello+wait");
+                LOG.trace("ret - firstHello+wait");
                 return;
             }
 
@@ -102,18 +116,18 @@ public class HandshakeManagerImpl implements HandshakeManager {
                 // versionBitmap missing at least on one side -> STEP-BY-STEP NEGOTIATION applying 
                 handleStepByStepVersionNegotiation(remoteVersion);
             }
-        } catch (Throwable ex) {
+        } catch (Exception ex) {
             errorHandler.handleException(ex, null);
             connectionAdapter.disconnect();
-            LOG.debug("ret - "+ex.getMessage());
+            LOG.trace("ret - shake fail: {}", ex.getMessage());
         }
     }
 
     /**
      * @param remoteVersion
-     * @throws Throwable 
+     * @throws Exception 
      */
-    private void handleStepByStepVersionNegotiation(Short remoteVersion) throws Throwable {
+    private void handleStepByStepVersionNegotiation(Short remoteVersion) throws Exception {
         LOG.debug("remoteVersion:{} lastProposedVersion:{}, highestVersion:{}", 
                 remoteVersion, lastProposedVersion, highestVersion);
         
@@ -126,13 +140,13 @@ public class HandshakeManagerImpl implements HandshakeManager {
         
         if (remoteVersion == lastProposedVersion) {
             postHandshake(lastProposedVersion, getNextXid());
-            LOG.debug("ret - OK - switch answered with lastProposedVersion");
+            LOG.trace("ret - OK - switch answered with lastProposedVersion");
         } else {
             checkNegotiationStalling(remoteVersion);
 
             if (remoteVersion > (lastProposedVersion == null ? highestVersion : lastProposedVersion)) {
                 // wait for next version
-                LOG.debug("ret - wait");
+                LOG.trace("ret - wait");
             } else {
                 //propose lower version
                 handleLowerVersionProposal(remoteVersion);
@@ -142,9 +156,9 @@ public class HandshakeManagerImpl implements HandshakeManager {
 
     /**
      * @param remoteVersion
-     * @throws Throwable 
+     * @throws Exception 
      */
-    private void handleLowerVersionProposal(Short remoteVersion) throws Throwable {
+    private void handleLowerVersionProposal(Short remoteVersion) throws Exception {
         Short proposedVersion;
         // find the version from header version field
         proposedVersion = proposeNextVersion(remoteVersion);
@@ -152,18 +166,18 @@ public class HandshakeManagerImpl implements HandshakeManager {
         sendHelloMessage(proposedVersion, getNextXid());
 
         if (proposedVersion != remoteVersion) {
-            LOG.debug("ret - sent+wait");
+            LOG.trace("ret - sent+wait");
         } else {
-            LOG.debug("ret - sent+OK");
+            LOG.trace("ret - sent+OK");
             postHandshake(proposedVersion, getNextXid());
         }
     }
 
     /**
      * @param elements
-     * @throws Throwable 
+     * @throws Exception 
      */
-    private void handleVersionBitmapNegotiation(List<Elements> elements) throws Throwable {
+    private void handleVersionBitmapNegotiation(List<Elements> elements) throws Exception {
         Short proposedVersion;
         proposedVersion = proposeCommonBitmapVersion(elements);
         if (lastProposedVersion == null) {
@@ -171,7 +185,7 @@ public class HandshakeManagerImpl implements HandshakeManager {
             sendHelloMessage(proposedVersion, getNextXid());
         }
         postHandshake(proposedVersion, getNextXid());
-        LOG.debug("ret - OK - versionBitmap");
+        LOG.trace("ret - OK - versionBitmap");
     }
     
     /**
@@ -262,9 +276,9 @@ public class HandshakeManagerImpl implements HandshakeManager {
      * send hello reply without versionBitmap
      * @param helloVersion
      * @param helloXid
-     * @throws Throwable 
+     * @throws Exception 
      */
-    private void sendHelloMessage(Short helloVersion, Long helloXid) throws Throwable {
+    private void sendHelloMessage(Short helloVersion, Long helloXid) throws Exception {
         //Short highestVersion = ConnectionConductor.versionOrder.get(0);
         //final Long helloXid = 21L;
         HelloInput helloInput = MessageFactory.createHelloInput(helloVersion, helloXid, versionOrder);
@@ -276,9 +290,11 @@ public class HandshakeManagerImpl implements HandshakeManager {
             RpcResult<Void> helloResult = connectionAdapter.hello(helloInput).get(maxTimeout, maxTimeoutUnit);
             RpcUtil.smokeRpc(helloResult);
             LOG.debug("FIRST HELLO sent.");
-        } catch (Throwable e) {
-            LOG.debug("FIRST HELLO sending failed.");
-            throw e;
+        } catch (Exception e) {
+            if (LOG.isTraceEnabled()) {
+                LOG.trace("FIRST HELLO sent.", e);
+            }
+            throw new ConnectionException("FIRST HELLO sending failed because of connection issue.");
         }
     }
 
@@ -287,14 +303,13 @@ public class HandshakeManagerImpl implements HandshakeManager {
      * after handshake set features, register to session
      * @param proposedVersion
      * @param xId
-     * @throws Throwable 
+     * @throws Exception 
      */
-    protected void postHandshake(Short proposedVersion, Long xid) throws Throwable {
+    protected void postHandshake(Short proposedVersion, Long xid) throws Exception {
         // set version
-        long maxTimeout = 3000;
         version = proposedVersion;
 
-        LOG.debug("version set: " + proposedVersion);
+        LOG.debug("version set: {}", proposedVersion);
         // request features
         GetFeaturesInputBuilder featuresBuilder = new GetFeaturesInputBuilder();
         featuresBuilder.setVersion(version).setXid(xid);
@@ -317,9 +332,14 @@ public class HandshakeManagerImpl implements HandshakeManager {
                     version, featureOutput.getDatapathId(), featureOutput.getAuxiliaryId());
             
             handshakeListener.onHandshakeSuccessfull(featureOutput, proposedVersion);
-        } catch (Throwable e) {
-            //handshake failed
-            LOG.error("issuing disconnect during handshake, reason: "+e.getMessage());
+        } catch (TimeoutException e) {
+            // handshake failed
+            LOG.warn("issuing disconnect during handshake, reason: future expired", e);
+            connectionAdapter.disconnect();
+            throw e;
+        } catch (Exception e) {
+            // handshake failed
+            LOG.warn("issuing disconnect during handshake, reason - RPC: {}", e.getMessage(), e);
             connectionAdapter.disconnect();
             throw e;
         }