Merge "BUG-5573: Altering the HandshakePool to have a LinkedBlockingQueue instead...
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / connection / listener / ConnectionReadyListenerImpl.java
1 /**
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.openflowplugin.impl.connection.listener;
9
10 import java.util.concurrent.Future;
11 import org.opendaylight.openflowjava.protocol.api.connection.ConnectionReadyListener;
12 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
13 import org.opendaylight.openflowplugin.api.openflow.connection.HandshakeContext;
14 import org.opendaylight.openflowplugin.openflow.md.core.HandshakeStepWrapper;
15 import org.slf4j.Logger;
16 import org.slf4j.LoggerFactory;
17
18 /**
19  * oneshot listener - once connection is ready, initiate handshake (if not already started by device)
20  */
21 public class ConnectionReadyListenerImpl implements ConnectionReadyListener {
22
23     private static final Logger LOG = LoggerFactory.getLogger(ConnectionReadyListenerImpl.class);
24
25     private ConnectionContext connectionContext;
26     private HandshakeContext handshakeContext;
27
28     /**
29      * @param connectionContext
30      * @param handshakeContext
31      */
32     public ConnectionReadyListenerImpl(ConnectionContext connectionContext,
33             HandshakeContext handshakeContext) {
34                 this.connectionContext = connectionContext;
35                 this.handshakeContext = handshakeContext;
36     }
37
38     @Override
39     public void onConnectionReady() {
40         if(LOG.isDebugEnabled()) {
41             LOG.debug("device is connected and ready-to-use (pipeline prepared): {}",
42                     connectionContext.getConnectionAdapter().getRemoteAddress());
43         }
44
45         if (connectionContext.getConnectionState() == null) {
46             synchronized (connectionContext) {
47                 if (connectionContext.getConnectionState() == null) {
48                     connectionContext.changeStateToHandshaking();
49                     HandshakeStepWrapper handshakeStepWrapper = new HandshakeStepWrapper(
50                             null, handshakeContext.getHandshakeManager(), connectionContext.getConnectionAdapter());
51                     final Future<?> handshakeResult = handshakeContext.getHandshakePool().submit(handshakeStepWrapper);
52
53                     try {
54                         // as we run not in netty thread, need to remain in sync lock until initial handshake step processed
55                         handshakeResult.get();
56                     } catch (Exception e) {
57                         LOG.error("failed to process onConnectionReady event on device {}, reason {}",
58                                 connectionContext.getConnectionAdapter().getRemoteAddress(),
59                                 e);
60                         connectionContext.closeConnection(false);
61                         try {
62                             handshakeContext.close();
63                         } catch (Exception e1) {
64                             LOG.error("failed to close handshake context for device {}, reason {}",
65                                     connectionContext.getConnectionAdapter().getRemoteAddress(),
66                                     e1
67                             );
68                         }
69                     }
70                 } else {
71                     LOG.debug("already touched by hello message from device {} after second check",
72                             connectionContext.getConnectionAdapter().getRemoteAddress());
73                 }
74             }
75         } else {
76             LOG.debug("already touched by hello message from device {} after first check",
77                     connectionContext.getConnectionAdapter().getRemoteAddress());
78         }
79     }
80
81 }