BUG-3774: 100k flows initial stats fail - fix
[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.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;
29
30 /**
31  *
32  */
33 public class HandshakeListenerImpl implements HandshakeListener {
34
35     private static final Logger LOG = LoggerFactory.getLogger(HandshakeListenerImpl.class);
36
37     private ConnectionContext connectionContext;
38     private DeviceConnectedHandler deviceConnectedHandler;
39     private HandshakeContext handshakeContext;
40
41     /**
42      * @param connectionContext
43      * @param deviceConnectedHandler
44      */
45     public HandshakeListenerImpl(ConnectionContext connectionContext, DeviceConnectedHandler deviceConnectedHandler) {
46         this.connectionContext = connectionContext;
47         this.deviceConnectedHandler = deviceConnectedHandler;
48     }
49
50     @Override
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()));
57
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>>() {
61             @Override
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);
67             }
68
69             @Override
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);
73             }
74         });
75     }
76
77     protected ListenableFuture<RpcResult<BarrierOutput>> fireBarrier(final Short version, final long xid) {
78         final BarrierInput barrierInput = new BarrierInputBuilder()
79                 .setXid(xid)
80                 .setVersion(version)
81                 .build();
82         return JdkFutureAdapters.listenInPoolThread(
83                 connectionContext.getConnectionAdapter().barrier(barrierInput));
84     }
85
86     @Override
87     public void onHandshakeFailure() {
88         LOG.debug("handshake failed: {}", connectionContext.getConnectionAdapter().getRemoteAddress());
89         closeHandshakeContext();
90         connectionContext.closeConnection(false);
91     }
92
93     private void closeHandshakeContext() {
94         try {
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);
99         }
100     }
101
102     @Override
103     public void setHandshakeContext(HandshakeContext handshakeContext) {
104         this.handshakeContext = handshakeContext;
105     }
106 }