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.HandshakeContext;
18 import org.opendaylight.openflowplugin.api.openflow.device.handlers.DeviceConnectedHandler;
19 import org.opendaylight.openflowplugin.api.openflow.md.core.HandshakeListener;
20 import org.opendaylight.openflowplugin.impl.statistics.ofpspecific.SessionStatistics;
21 import org.opendaylight.openflowplugin.openflow.md.util.InventoryDataServiceUtil;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.BarrierInput;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.BarrierInputBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.BarrierOutput;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutput;
26 import org.opendaylight.yangtools.yang.common.RpcResult;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
33 public class HandshakeListenerImpl implements HandshakeListener {
35 private static final Logger LOG = LoggerFactory.getLogger(HandshakeListenerImpl.class);
37 private ConnectionContext connectionContext;
38 private DeviceConnectedHandler deviceConnectedHandler;
39 private HandshakeContext handshakeContext;
42 * @param connectionContext
43 * @param deviceConnectedHandler
45 public HandshakeListenerImpl(ConnectionContext connectionContext, DeviceConnectedHandler deviceConnectedHandler) {
46 this.connectionContext = connectionContext;
47 this.deviceConnectedHandler = deviceConnectedHandler;
51 public void onHandshakeSuccessfull(GetFeaturesOutput featureOutput, Short version) {
52 LOG.debug("handshake succeeded: {}", connectionContext.getConnectionAdapter().getRemoteAddress());
53 closeHandshakeContext();
54 connectionContext.changeStateToWorking();
55 connectionContext.setFeatures(featureOutput);
56 connectionContext.setNodeId(InventoryDataServiceUtil.nodeIdFromDatapathId(featureOutput.getDatapathId()));
58 // fire barrier in order to sweep all handshake and posthandshake messages before continue
59 final ListenableFuture<RpcResult<BarrierOutput>> barrier = fireBarrier(version, 0L);
60 Futures.addCallback(barrier, new FutureCallback<RpcResult<BarrierOutput>>() {
62 public void onSuccess(@Nullable final RpcResult<BarrierOutput> result) {
63 LOG.debug("succeeded by getting sweep barrier after posthandshake for device {}", connectionContext.getNodeId());
64 deviceConnectedHandler.deviceConnected(connectionContext);
65 SessionStatistics.countEvent(connectionContext.getNodeId().toString(),
66 SessionStatistics.ConnectionStatus.CONNECTION_CREATED);
70 public void onFailure(final Throwable t) {
71 LOG.info("failed to get sweep barrier after posthandshake for device {}", connectionContext.getNodeId());
72 connectionContext.closeConnection(false);
77 protected ListenableFuture<RpcResult<BarrierOutput>> fireBarrier(final Short version, final long xid) {
78 final BarrierInput barrierInput = new BarrierInputBuilder()
82 return JdkFutureAdapters.listenInPoolThread(
83 connectionContext.getConnectionAdapter().barrier(barrierInput));
87 public void onHandshakeFailure() {
88 LOG.debug("handshake failed: {}", connectionContext.getConnectionAdapter().getRemoteAddress());
89 closeHandshakeContext();
90 connectionContext.closeConnection(false);
93 private void closeHandshakeContext() {
95 handshakeContext.close();
96 } catch (Exception e) {
97 LOG.warn("Closing handshake context failed: {}", e.getMessage());
98 LOG.debug("Detail in hanshake context close:", e);
103 public void setHandshakeContext(HandshakeContext handshakeContext) {
104 this.handshakeContext = handshakeContext;