2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.openflowplugin.impl.connection.listener;
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;
19 * Oneshot listener - once connection is ready, initiate handshake (if not already started by device).
21 public class ConnectionReadyListenerImpl implements ConnectionReadyListener {
23 private static final Logger LOG = LoggerFactory.getLogger(ConnectionReadyListenerImpl.class);
25 private ConnectionContext connectionContext;
26 private HandshakeContext handshakeContext;
31 * @param connectionContext - connection context
32 * @param handshakeContext - handshake context
34 public ConnectionReadyListenerImpl(ConnectionContext connectionContext, HandshakeContext handshakeContext) {
35 this.connectionContext = connectionContext;
36 this.handshakeContext = handshakeContext;
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());
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);
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(),
63 connectionContext.closeConnection(false);
64 handshakeContext.close();
67 LOG.debug("already touched by hello message from device {} after second check",
68 connectionContext.getConnectionAdapter().getRemoteAddress());
72 LOG.debug("already touched by hello message from device {} after first check",
73 connectionContext.getConnectionAdapter().getRemoteAddress());