Merge "SONAR TD - DeviceFlowRegistryImpl, FlowDescriptorFactory, FlowRegistryKeyFactory"
[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         connectionContext.handshakeSuccessful();
58
59         // fire barrier in order to sweep all handshake and posthandshake messages before continue
60         final ListenableFuture<RpcResult<BarrierOutput>> barrier = fireBarrier(version, 0L);
61         Futures.addCallback(barrier, addBarrierCallback());
62     }
63
64     private FutureCallback<RpcResult<BarrierOutput>> addBarrierCallback() {
65         return new FutureCallback<RpcResult<BarrierOutput>>() {
66             @Override
67             public void onSuccess(@Nullable final RpcResult<BarrierOutput> result) {
68                 LOG.debug("succeeded by getting sweep barrier after posthandshake for device {}", connectionContext.getNodeId());
69                 try {
70                     if (!deviceConnectedHandler.deviceConnected(connectionContext)) {
71                         connectionContext.closeConnection(true);
72                     }
73                     SessionStatistics.countEvent(connectionContext.getNodeId().toString(),
74                             SessionStatistics.ConnectionStatus.CONNECTION_CREATED);
75                 } catch (final Exception e) {
76                     LOG.error("ConnectionContext initial processing failed: {}", e.getMessage());
77                     SessionStatistics.countEvent(connectionContext.getNodeId().toString(),
78                             SessionStatistics.ConnectionStatus.CONNECTION_DISCONNECTED_BY_OFP);
79                     connectionContext.closeConnection(true);
80                 }
81             }
82
83             @Override
84             public void onFailure(final Throwable t) {
85                 LOG.error("failed to get sweep barrier after posthandshake for device {}", connectionContext.getNodeId());
86                 connectionContext.closeConnection(false);
87             }
88         };
89     }
90
91     protected ListenableFuture<RpcResult<BarrierOutput>> fireBarrier(final Short version, final long xid) {
92         final BarrierInput barrierInput = new BarrierInputBuilder()
93                 .setXid(xid)
94                 .setVersion(version)
95                 .build();
96         return JdkFutureAdapters.listenInPoolThread(
97                 connectionContext.getConnectionAdapter().barrier(barrierInput));
98     }
99
100     @Override
101     public void onHandshakeFailure() {
102         LOG.debug("handshake failed: {}", connectionContext.getConnectionAdapter().getRemoteAddress());
103         closeHandshakeContext();
104         connectionContext.closeConnection(false);
105     }
106
107     private void closeHandshakeContext() {
108         try {
109             handshakeContext.close();
110         } catch (final Exception e) {
111             LOG.error("Closing handshake context failed: {}", e.getMessage());
112             LOG.debug("Detail in handshake context close: {}", e);
113         }
114     }
115
116     @Override
117     public void setHandshakeContext(final HandshakeContext handshakeContext) {
118         this.handshakeContext = handshakeContext;
119     }
120 }