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
9 package org.opendaylight.openflowplugin.impl.connection.listener;
11 import com.google.common.util.concurrent.FutureCallback;
12 import com.google.common.util.concurrent.Futures;
13 import com.google.common.util.concurrent.JdkFutureAdapters;
14 import com.google.common.util.concurrent.ListenableFuture;
15 import javax.annotation.Nullable;
16 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
17 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionStatus;
18 import org.opendaylight.openflowplugin.api.openflow.connection.HandshakeContext;
19 import org.opendaylight.openflowplugin.api.openflow.device.handlers.DeviceConnectedHandler;
20 import org.opendaylight.openflowplugin.api.openflow.md.core.HandshakeListener;
21 import org.opendaylight.openflowplugin.impl.statistics.ofpspecific.SessionStatistics;
22 import org.opendaylight.openflowplugin.openflow.md.util.InventoryDataServiceUtil;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.BarrierInput;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.BarrierInputBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.BarrierOutput;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutput;
27 import org.opendaylight.yangtools.yang.common.RpcResult;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
34 public class HandshakeListenerImpl implements HandshakeListener {
36 private static final Logger LOG = LoggerFactory.getLogger(HandshakeListenerImpl.class);
38 private final ConnectionContext connectionContext;
39 private final DeviceConnectedHandler deviceConnectedHandler;
40 private HandshakeContext handshakeContext;
43 * @param connectionContext
44 * @param deviceConnectedHandler
46 public HandshakeListenerImpl(final ConnectionContext connectionContext, final DeviceConnectedHandler deviceConnectedHandler) {
47 this.connectionContext = connectionContext;
48 this.deviceConnectedHandler = deviceConnectedHandler;
52 public void onHandshakeSuccessful(final GetFeaturesOutput featureOutput, final Short version) {
53 if (LOG.isDebugEnabled()) {
54 LOG.debug("handshake succeeded: {}", connectionContext.getConnectionAdapter().getRemoteAddress());
56 this.handshakeContext.close();
57 connectionContext.changeStateToWorking();
58 connectionContext.setFeatures(featureOutput);
59 connectionContext.setNodeId(InventoryDataServiceUtil.nodeIdFromDatapathId(featureOutput.getDatapathId()));
60 connectionContext.handshakeSuccessful();
62 // fire barrier in order to sweep all handshake and posthandshake messages before continue
63 final ListenableFuture<RpcResult<BarrierOutput>> barrier = fireBarrier(version, 0L);
64 Futures.addCallback(barrier, addBarrierCallback());
67 private FutureCallback<RpcResult<BarrierOutput>> addBarrierCallback() {
68 return new FutureCallback<RpcResult<BarrierOutput>>() {
70 public void onSuccess(@Nullable final RpcResult<BarrierOutput> result) {
71 if (LOG.isDebugEnabled()) {
72 LOG.debug("succeeded by getting sweep barrier after post-handshake for device {}", connectionContext.getNodeId().getValue());
75 ConnectionStatus connectionStatusResult = deviceConnectedHandler.deviceConnected(connectionContext);
76 if (!ConnectionStatus.MAY_CONTINUE.equals(connectionStatusResult)) {
77 connectionContext.closeConnection(ConnectionStatus.ALREADY_CONNECTED.equals(connectionStatusResult));
79 SessionStatistics.countEvent(connectionContext.getNodeId().toString(),
80 SessionStatistics.ConnectionStatus.CONNECTION_CREATED);
81 } catch (final Exception e) {
82 LOG.error("ConnectionContext initial processing failed: ", e);
83 SessionStatistics.countEvent(connectionContext.getNodeId().toString(),
84 SessionStatistics.ConnectionStatus.CONNECTION_DISCONNECTED_BY_OFP);
85 connectionContext.closeConnection(true);
90 public void onFailure(final Throwable t) {
91 LOG.error("failed to get sweep barrier after post-handshake for device {}", connectionContext.getNodeId());
92 connectionContext.closeConnection(false);
97 private ListenableFuture<RpcResult<BarrierOutput>> fireBarrier(final Short version, final long xid) {
98 final BarrierInput barrierInput = new BarrierInputBuilder()
102 return JdkFutureAdapters.listenInPoolThread(
103 this.connectionContext.getConnectionAdapter().barrier(barrierInput));
107 public void onHandshakeFailure() {
108 if (LOG.isDebugEnabled()) {
109 LOG.debug("handshake failed: {}", this.connectionContext.getConnectionAdapter().getRemoteAddress());
111 this.handshakeContext.close();
112 this.connectionContext.closeConnection(false);
116 public void setHandshakeContext(final HandshakeContext handshakeContext) {
117 this.handshakeContext = handshakeContext;