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 com.google.common.util.concurrent.MoreExecutors;
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;
31 public class HandshakeListenerImpl implements HandshakeListener {
33 private static final Logger LOG = LoggerFactory.getLogger(HandshakeListenerImpl.class);
34 private static final Logger OF_EVENT_LOG = LoggerFactory.getLogger("OfEventLog");
36 private final ConnectionContext connectionContext;
37 private final DeviceConnectedHandler deviceConnectedHandler;
38 private HandshakeContext handshakeContext;
43 * @param connectionContext - connection context
44 * @param deviceConnectedHandler - device connected handler
46 public HandshakeListenerImpl(final ConnectionContext connectionContext,
47 final DeviceConnectedHandler deviceConnectedHandler) {
48 this.connectionContext = connectionContext;
49 this.deviceConnectedHandler = deviceConnectedHandler;
53 public void onHandshakeSuccessful(final GetFeaturesOutput featureOutput, final Short version) {
54 if (LOG.isDebugEnabled()) {
55 LOG.debug("handshake succeeded: {}", connectionContext.getConnectionAdapter().getRemoteAddress());
57 OF_EVENT_LOG.debug("Connect, Node: {}", featureOutput.getDatapathId());
58 this.handshakeContext.close();
59 connectionContext.changeStateToWorking();
60 connectionContext.setFeatures(featureOutput);
61 connectionContext.setNodeId(InventoryDataServiceUtil.nodeIdFromDatapathId(featureOutput.getDatapathId()));
62 connectionContext.handshakeSuccessful();
64 // fire barrier in order to sweep all handshake and posthandshake messages before continue
65 final ListenableFuture<RpcResult<BarrierOutput>> barrier = fireBarrier(version, 0L);
66 Futures.addCallback(barrier, addBarrierCallback(), MoreExecutors.directExecutor());
69 private FutureCallback<RpcResult<BarrierOutput>> addBarrierCallback() {
70 return new FutureCallback<RpcResult<BarrierOutput>>() {
72 @SuppressWarnings("checkstyle:IllegalCatch")
73 public void onSuccess(final RpcResult<BarrierOutput> result) {
74 if (LOG.isDebugEnabled()) {
75 LOG.debug("succeeded by getting sweep barrier after post-handshake for device {}",
76 connectionContext.getDeviceInfo());
79 ConnectionStatus connectionStatusResult = deviceConnectedHandler.deviceConnected(connectionContext);
80 if (connectionStatusResult != ConnectionStatus.MAY_CONTINUE) {
81 connectionContext.closeConnection(false);
83 SessionStatistics.countEvent(connectionContext.getDeviceInfo().toString(),
84 SessionStatistics.ConnectionStatus.CONNECTION_CREATED);
85 } catch (final Exception e) {
86 LOG.warn("initial processing failed for device {}", connectionContext.getDeviceInfo(), e);
87 SessionStatistics.countEvent(connectionContext.getDeviceInfo().toString(),
88 SessionStatistics.ConnectionStatus.CONNECTION_DISCONNECTED_BY_OFP);
89 connectionContext.closeConnection(true);
94 public void onFailure(final Throwable throwable) {
95 LOG.warn("failed to get sweep barrier after post-handshake for device {}",
96 connectionContext.getDeviceInfo(), throwable);
97 connectionContext.closeConnection(false);
102 private ListenableFuture<RpcResult<BarrierOutput>> fireBarrier(final Short version, final long xid) {
103 final BarrierInput barrierInput = new BarrierInputBuilder()
107 return JdkFutureAdapters.listenInPoolThread(
108 this.connectionContext.getConnectionAdapter().barrier(barrierInput));
112 public void onHandshakeFailure() {
113 if (LOG.isDebugEnabled()) {
114 LOG.debug("handshake failed: {}", this.connectionContext.getConnectionAdapter().getRemoteAddress());
116 this.handshakeContext.close();
117 this.connectionContext.closeConnection(false);
121 public void setHandshakeContext(final HandshakeContext handshakeContext) {
122 this.handshakeContext = handshakeContext;