Merge remote-tracking branch 'liblldp/master'
[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.impl.connection.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      * Constructor.
30      *
31      * @param connectionContext - connection context
32      * @param handshakeContext - handshake context
33      */
34     public ConnectionReadyListenerImpl(ConnectionContext connectionContext, HandshakeContext handshakeContext) {
35         this.connectionContext = connectionContext;
36         this.handshakeContext = handshakeContext;
37     }
38
39     @Override
40     @SuppressWarnings("checkstyle:IllegalCatch")
41     public void onConnectionReady() {
42         if (LOG.isDebugEnabled()) {
43             LOG.debug("device is connected and ready-to-use (pipeline prepared): {}",
44                     connectionContext.getConnectionAdapter().getRemoteAddress());
45         }
46
47         if (connectionContext.getConnectionState() == null) {
48             synchronized (connectionContext) {
49                 if (connectionContext.getConnectionState() == null) {
50                     connectionContext.changeStateToHandshaking();
51                     HandshakeStepWrapper handshakeStepWrapper = new HandshakeStepWrapper(
52                             null, handshakeContext.getHandshakeManager(), connectionContext.getConnectionAdapter());
53                     final Future<?> handshakeResult = handshakeContext.getHandshakePool().submit(handshakeStepWrapper);
54
55                     try {
56                         // As we run not in netty thread,
57                         // need to remain in sync lock until initial handshake step processed.
58                         handshakeResult.get();
59                     } catch (Exception e) {
60                         LOG.error("failed to process onConnectionReady event on device {}, reason {}",
61                                 connectionContext.getConnectionAdapter().getRemoteAddress(),
62                                 e);
63                         connectionContext.closeConnection(false);
64                         handshakeContext.close();
65                     }
66                 } else {
67                     LOG.debug("already touched by hello message from device {} after second check",
68                             connectionContext.getConnectionAdapter().getRemoteAddress());
69                 }
70             }
71         } else {
72             LOG.debug("already touched by hello message from device {} after first check",
73                     connectionContext.getConnectionAdapter().getRemoteAddress());
74         }
75     }
76
77 }