Post "Clustering optimization" updates
[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 final ConnectionContext connectionContext;
38     private final DeviceConnectedHandler deviceConnectedHandler;
39     private HandshakeContext handshakeContext;
40
41     /**
42      * @param connectionContext
43      * @param deviceConnectedHandler
44      */
45     public HandshakeListenerImpl(final ConnectionContext connectionContext, final DeviceConnectedHandler deviceConnectedHandler) {
46         this.connectionContext = connectionContext;
47         this.deviceConnectedHandler = deviceConnectedHandler;
48     }
49
50     @Override
51     public void onHandshakeSuccessful(final GetFeaturesOutput featureOutput, final 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                 try {
65                     deviceConnectedHandler.deviceConnected(connectionContext);
66                     SessionStatistics.countEvent(connectionContext.getNodeId().toString(),
67                             SessionStatistics.ConnectionStatus.CONNECTION_CREATED);
68                 } catch (final Exception e) {
69                     LOG.info("ConnectionContext initial processing failed: {}", e.getMessage());
70                     SessionStatistics.countEvent(connectionContext.getNodeId().toString(),
71                             SessionStatistics.ConnectionStatus.CONNECTION_DISCONNECTED_BY_OFP);
72                     connectionContext.closeConnection(true);
73                 }
74             }
75
76             @Override
77             public void onFailure(final Throwable t) {
78                 LOG.info("failed to get sweep barrier after posthandshake for device {}", connectionContext.getNodeId());
79                 connectionContext.closeConnection(false);
80             }
81         });
82     }
83
84     protected ListenableFuture<RpcResult<BarrierOutput>> fireBarrier(final Short version, final long xid) {
85         final BarrierInput barrierInput = new BarrierInputBuilder()
86                 .setXid(xid)
87                 .setVersion(version)
88                 .build();
89         return JdkFutureAdapters.listenInPoolThread(
90                 connectionContext.getConnectionAdapter().barrier(barrierInput));
91     }
92
93     @Override
94     public void onHandshakeFailure() {
95         LOG.debug("handshake failed: {}", connectionContext.getConnectionAdapter().getRemoteAddress());
96         closeHandshakeContext();
97         connectionContext.closeConnection(false);
98     }
99
100     private void closeHandshakeContext() {
101         try {
102             handshakeContext.close();
103         } catch (final Exception e) {
104             LOG.warn("Closing handshake context failed: {}", e.getMessage());
105             LOG.debug("Detail in handshake context close:", e);
106         }
107     }
108
109     @Override
110     public void setHandshakeContext(final HandshakeContext handshakeContext) {
111         this.handshakeContext = handshakeContext;
112     }
113 }