Merge "Log at ERROR level when request context failed to close"
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / connection / ConnectionContextImpl.java
index cbd5d0440ef293330f05aca3f114c7f5a4a65af7..3fecb793c5dbb63deb6d838f947b75c12af04e1b 100644 (file)
@@ -1,10 +1,11 @@
 /**
  * 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;
 
 import java.math.BigInteger;
@@ -13,6 +14,7 @@ import org.opendaylight.openflowjava.protocol.api.connection.ConnectionAdapter;
 import org.opendaylight.openflowjava.protocol.api.connection.OutboundQueue;
 import org.opendaylight.openflowjava.protocol.api.connection.OutboundQueueHandlerRegistration;
 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
+import org.opendaylight.openflowplugin.api.openflow.connection.HandshakeContext;
 import org.opendaylight.openflowplugin.api.openflow.connection.OutboundQueueProvider;
 import org.opendaylight.openflowplugin.api.openflow.device.handlers.DeviceDisconnectedHandler;
 import org.opendaylight.openflowplugin.impl.statistics.ofpspecific.SessionStatistics;
@@ -27,13 +29,14 @@ import org.slf4j.LoggerFactory;
 public class ConnectionContextImpl implements ConnectionContext {
 
     private final ConnectionAdapter connectionAdapter;
-    private CONNECTION_STATE connectionState;
+    private volatile CONNECTION_STATE connectionState;
     private FeaturesReply featuresReply;
     private NodeId nodeId;
     private DeviceDisconnectedHandler deviceDisconnectedHandler;
     private static final Logger LOG = LoggerFactory.getLogger(ConnectionContextImpl.class);
     private OutboundQueueProvider outboundQueueProvider;
     private OutboundQueueHandlerRegistration<OutboundQueueProvider> outboundQueueHandlerRegistration;
+    private HandshakeContext handshakeContext;
 
     /**
      * @param connectionAdapter
@@ -100,6 +103,8 @@ public class ConnectionContextImpl implements ConnectionContext {
         connectionState = ConnectionContext.CONNECTION_STATE.RIP;
 
         unregisterOutboundQueue();
+        closeHandshakeContext();
+
         if (getConnectionAdapter().isAlive()) {
             getConnectionAdapter().disconnect();
         }
@@ -109,6 +114,18 @@ public class ConnectionContextImpl implements ConnectionContext {
         }
     }
 
+    private void closeHandshakeContext() {
+        if (handshakeContext != null) {
+            try {
+                handshakeContext.close();
+            } catch (Exception e) {
+                LOG.info("handshake context closing failed: ", e);
+            } finally {
+                handshakeContext = null;
+            }
+        }
+    }
+
     @Override
     public void onConnectionClosed() {
         if (null == nodeId){
@@ -132,7 +149,7 @@ public class ConnectionContextImpl implements ConnectionContext {
                 getConnectionState());
 
         unregisterOutboundQueue();
-
+        closeHandshakeContext();
         propagateDeviceDisconnectedEvent();
     }
 
@@ -158,17 +175,22 @@ public class ConnectionContextImpl implements ConnectionContext {
     }
 
     @Override
-    public void changeStateToHandshaking() {
+    public synchronized void changeStateToHandshaking() {
         connectionState = CONNECTION_STATE.HANDSHAKING;
     }
 
     @Override
-    public void changeStateToTimeouting() {
+    public synchronized void changeStateToTimeouting() {
         connectionState = CONNECTION_STATE.TIMEOUTING;
     }
 
     @Override
-    public void changeStateToWorking() {
+    public synchronized void changeStateToWorking() {
         connectionState = CONNECTION_STATE.WORKING;
     }
+
+    @Override
+    public void setHandshakeContext(HandshakeContext handshakeContext) {
+        this.handshakeContext = handshakeContext;
+    }
 }