82ad28a2b15d547d103f744de9ee1706117bb03b
[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.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;
30
31 /**
32  *
33  */
34 public class HandshakeListenerImpl implements HandshakeListener {
35
36     private static final Logger LOG = LoggerFactory.getLogger(HandshakeListenerImpl.class);
37
38     private final ConnectionContext connectionContext;
39     private final DeviceConnectedHandler deviceConnectedHandler;
40     private HandshakeContext handshakeContext;
41
42     /**
43      * @param connectionContext
44      * @param deviceConnectedHandler
45      */
46     public HandshakeListenerImpl(final ConnectionContext connectionContext, 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         LOG.debug("handshake succeeded: {}", connectionContext.getConnectionAdapter().getRemoteAddress());
54         closeHandshakeContext();
55         connectionContext.changeStateToWorking();
56         connectionContext.setFeatures(featureOutput);
57         connectionContext.setNodeId(InventoryDataServiceUtil.nodeIdFromDatapathId(featureOutput.getDatapathId()));
58         connectionContext.handshakeSuccessful();
59
60         // fire barrier in order to sweep all handshake and posthandshake messages before continue
61         final ListenableFuture<RpcResult<BarrierOutput>> barrier = fireBarrier(version, 0L);
62         Futures.addCallback(barrier, addBarrierCallback());
63     }
64
65     private FutureCallback<RpcResult<BarrierOutput>> addBarrierCallback() {
66         return new FutureCallback<RpcResult<BarrierOutput>>() {
67             @Override
68             public void onSuccess(@Nullable final RpcResult<BarrierOutput> result) {
69                 LOG.debug("succeeded by getting sweep barrier after post-handshake for device {}", connectionContext.getNodeId().getValue());
70                 try {
71                     ConnectionStatus connectionStatusResult = deviceConnectedHandler.deviceConnected(connectionContext);
72                     if (!ConnectionStatus.MAY_CONTINUE.equals(connectionStatusResult)) {
73                         connectionContext.closeConnection(ConnectionStatus.ALREADY_CONNECTED.equals(connectionStatusResult));
74                     }
75                     SessionStatistics.countEvent(connectionContext.getNodeId().toString(),
76                             SessionStatistics.ConnectionStatus.CONNECTION_CREATED);
77                 } catch (final Exception e) {
78                     LOG.error("ConnectionContext initial processing failed: ", e);
79                     SessionStatistics.countEvent(connectionContext.getNodeId().toString(),
80                             SessionStatistics.ConnectionStatus.CONNECTION_DISCONNECTED_BY_OFP);
81                     connectionContext.closeConnection(true);
82                 }
83             }
84
85             @Override
86             public void onFailure(final Throwable t) {
87                 LOG.error("failed to get sweep barrier after post-handshake for device {}", connectionContext.getNodeId());
88                 connectionContext.closeConnection(false);
89             }
90         };
91     }
92
93     protected ListenableFuture<RpcResult<BarrierOutput>> fireBarrier(final Short version, final long xid) {
94         final BarrierInput barrierInput = new BarrierInputBuilder()
95                 .setXid(xid)
96                 .setVersion(version)
97                 .build();
98         return JdkFutureAdapters.listenInPoolThread(
99                 connectionContext.getConnectionAdapter().barrier(barrierInput));
100     }
101
102     @Override
103     public void onHandshakeFailure() {
104         LOG.debug("handshake failed: {}", connectionContext.getConnectionAdapter().getRemoteAddress());
105         closeHandshakeContext();
106         connectionContext.closeConnection(false);
107     }
108
109     private void closeHandshakeContext() {
110         try {
111             handshakeContext.close();
112         } catch (final Exception e) {
113             LOG.error("Closing handshake context failed: {}", e.getMessage());
114             LOG.debug("Detail in handshake context close: {}", e);
115         }
116     }
117
118     @Override
119     public void setHandshakeContext(final HandshakeContext handshakeContext) {
120         this.handshakeContext = handshakeContext;
121     }
122 }