Reduce use of JdkFutureAdapters
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / connection / listener / HandshakeListenerImpl.java
1 /**
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.openflowplugin.impl.connection.listener;
10
11 import com.google.common.util.concurrent.FutureCallback;
12 import com.google.common.util.concurrent.Futures;
13 import com.google.common.util.concurrent.ListenableFuture;
14 import com.google.common.util.concurrent.MoreExecutors;
15 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
16 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionStatus;
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;
29
30 public class HandshakeListenerImpl implements HandshakeListener {
31
32     private static final Logger LOG = LoggerFactory.getLogger(HandshakeListenerImpl.class);
33     private static final Logger OF_EVENT_LOG = LoggerFactory.getLogger("OfEventLog");
34
35     private final ConnectionContext connectionContext;
36     private final DeviceConnectedHandler deviceConnectedHandler;
37     private HandshakeContext handshakeContext;
38
39     /**
40      * Constructor.
41      *
42      * @param connectionContext - connection context
43      * @param deviceConnectedHandler - device connected handler
44      */
45     public HandshakeListenerImpl(final ConnectionContext connectionContext,
46                                  final DeviceConnectedHandler deviceConnectedHandler) {
47         this.connectionContext = connectionContext;
48         this.deviceConnectedHandler = deviceConnectedHandler;
49     }
50
51     @Override
52     public void onHandshakeSuccessful(final GetFeaturesOutput featureOutput, final Short version) {
53         if (LOG.isDebugEnabled()) {
54             LOG.debug("handshake succeeded: {}", connectionContext.getConnectionAdapter().getRemoteAddress());
55         }
56         OF_EVENT_LOG.debug("Connect, Node: {}", featureOutput.getDatapathId());
57         this.handshakeContext.close();
58         connectionContext.changeStateToWorking();
59         connectionContext.setFeatures(featureOutput);
60         connectionContext.setNodeId(InventoryDataServiceUtil.nodeIdFromDatapathId(featureOutput.getDatapathId()));
61         connectionContext.handshakeSuccessful();
62
63         // fire barrier in order to sweep all handshake and posthandshake messages before continue
64         final ListenableFuture<RpcResult<BarrierOutput>> barrier = fireBarrier(version, 0L);
65         Futures.addCallback(barrier, addBarrierCallback(), MoreExecutors.directExecutor());
66     }
67
68     private FutureCallback<RpcResult<BarrierOutput>> addBarrierCallback() {
69         return new FutureCallback<RpcResult<BarrierOutput>>() {
70             @Override
71             @SuppressWarnings("checkstyle:IllegalCatch")
72             public void onSuccess(final RpcResult<BarrierOutput> result) {
73                 if (LOG.isDebugEnabled()) {
74                     LOG.debug("succeeded by getting sweep barrier after post-handshake for device {}",
75                             connectionContext.getDeviceInfo());
76                 }
77                 try {
78                     ConnectionStatus connectionStatusResult = deviceConnectedHandler.deviceConnected(connectionContext);
79                     if (connectionStatusResult != ConnectionStatus.MAY_CONTINUE) {
80                         connectionContext.closeConnection(false);
81                     }
82                     SessionStatistics.countEvent(connectionContext.getDeviceInfo().toString(),
83                             SessionStatistics.ConnectionStatus.CONNECTION_CREATED);
84                 } catch (final Exception e) {
85                     LOG.warn("initial processing failed for device {}", connectionContext.getDeviceInfo(), e);
86                     SessionStatistics.countEvent(connectionContext.getDeviceInfo().toString(),
87                             SessionStatistics.ConnectionStatus.CONNECTION_DISCONNECTED_BY_OFP);
88                     connectionContext.closeConnection(true);
89                 }
90             }
91
92             @Override
93             public void onFailure(final Throwable throwable) {
94                 LOG.warn("failed to get sweep barrier after post-handshake for device {}",
95                         connectionContext.getDeviceInfo(), throwable);
96                 connectionContext.closeConnection(false);
97             }
98         };
99     }
100
101     private ListenableFuture<RpcResult<BarrierOutput>> fireBarrier(final Short version, final long xid) {
102         final BarrierInput barrierInput = new BarrierInputBuilder()
103                 .setXid(xid)
104                 .setVersion(version)
105                 .build();
106         return this.connectionContext.getConnectionAdapter().barrier(barrierInput);
107     }
108
109     @Override
110     public void onHandshakeFailure() {
111         if (LOG.isDebugEnabled()) {
112             LOG.debug("handshake failed: {}", this.connectionContext.getConnectionAdapter().getRemoteAddress());
113         }
114         this.handshakeContext.close();
115         this.connectionContext.closeConnection(false);
116     }
117
118     @Override
119     public void setHandshakeContext(final HandshakeContext handshakeContext) {
120         this.handshakeContext = handshakeContext;
121     }
122 }