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.openflow.md.core.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;
29 * @param connectionContext
30 * @param handshakeContext
32 public ConnectionReadyListenerImpl(ConnectionContext connectionContext,
33 HandshakeContext handshakeContext) {
34 this.connectionContext = connectionContext;
35 this.handshakeContext = handshakeContext;
39 public void onConnectionReady() {
40 if(LOG.isDebugEnabled()) {
41 LOG.debug("device is connected and ready-to-use (pipeline prepared): {}",
42 connectionContext.getConnectionAdapter().getRemoteAddress());
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);
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(),
60 connectionContext.closeConnection(false);
62 handshakeContext.close();
63 } catch (Exception e1) {
64 LOG.error("failed to close handshake context for device {}, reason {}",
65 connectionContext.getConnectionAdapter().getRemoteAddress(),
71 LOG.debug("already touched by hello message from device {} after second check",
72 connectionContext.getConnectionAdapter().getRemoteAddress());
76 LOG.debug("already touched by hello message from device {} after first check",
77 connectionContext.getConnectionAdapter().getRemoteAddress());