BUG-3579: device disconnection cleanup
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / connection / listener / HandshakeListenerImpl.java
index 9a254a2ade57158d4ecc4a154f5e33e34285621b..2f1261e79952e7c3596f53bc48b2ef2264b4cf81 100644 (file)
@@ -1,17 +1,17 @@
 /**
  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
- *
+ * <p/>
  * This program and the accompanying materials are made available under the
  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
  * and is available at http://www.eclipse.org/legal/epl-v10.html
  */
 package org.opendaylight.openflowplugin.impl.connection.listener;
 
-import org.opendaylight.openflowplugin.openflow.md.util.InventoryDataServiceUtil;
-
 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
+import org.opendaylight.openflowplugin.api.openflow.connection.HandshakeContext;
 import org.opendaylight.openflowplugin.api.openflow.device.handlers.DeviceConnectedHandler;
 import org.opendaylight.openflowplugin.api.openflow.md.core.HandshakeListener;
+import org.opendaylight.openflowplugin.openflow.md.util.InventoryDataServiceUtil;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutput;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -25,6 +25,7 @@ public class HandshakeListenerImpl implements HandshakeListener {
 
     private ConnectionContext connectionContext;
     private DeviceConnectedHandler deviceConnectedHandler;
+    private HandshakeContext handshakeContext;
 
     /**
      * @param connectionContext
@@ -37,7 +38,9 @@ public class HandshakeListenerImpl implements HandshakeListener {
 
     @Override
     public void onHandshakeSuccessfull(GetFeaturesOutput featureOutput, Short version) {
-        connectionContext.setConnectionState(ConnectionContext.CONNECTION_STATE.WORKING);
+        LOG.debug("handshake succeeded: {}", connectionContext.getConnectionAdapter().getRemoteAddress());
+        closeHandshakeContext();
+        connectionContext.changeStateToWorking();
         connectionContext.setFeatures(featureOutput);
         connectionContext.setNodeId(InventoryDataServiceUtil.nodeIdFromDatapathId(featureOutput.getDatapathId()));
         deviceConnectedHandler.deviceConnected(connectionContext);
@@ -45,8 +48,22 @@ public class HandshakeListenerImpl implements HandshakeListener {
 
     @Override
     public void onHandshakeFailure() {
-        LOG.info("handshake failed: {}", connectionContext.getConnectionAdapter().getRemoteAddress());
-        connectionContext.setConnectionState(ConnectionContext.CONNECTION_STATE.RIP);
-        // TODO ensure that connection is closed
+        LOG.debug("handshake failed: {}", connectionContext.getConnectionAdapter().getRemoteAddress());
+        closeHandshakeContext();
+        connectionContext.closeConnection(false);
+    }
+
+    private void closeHandshakeContext() {
+        try {
+            handshakeContext.close();
+        } catch (Exception e) {
+            LOG.warn("Closing handshake context failed: {}", e.getMessage());
+            LOG.debug("Detail in hanshake context close:", e);
+        }
+    }
+
+    @Override
+    public void setHandshakeContext(HandshakeContext handshakeContext) {
+        this.handshakeContext = handshakeContext;
     }
 }