Merge "BUG-4117: add support for meter and group old Notif"
[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                     if (!deviceConnectedHandler.deviceConnected(connectionContext)) {
66                         connectionContext.closeConnection(true);
67                     }
68                     SessionStatistics.countEvent(connectionContext.getNodeId().toString(),
69                             SessionStatistics.ConnectionStatus.CONNECTION_CREATED);
70                 } catch (final Exception e) {
71                     LOG.error("ConnectionContext initial processing failed: {}", e.getMessage());
72                     SessionStatistics.countEvent(connectionContext.getNodeId().toString(),
73                             SessionStatistics.ConnectionStatus.CONNECTION_DISCONNECTED_BY_OFP);
74                     connectionContext.closeConnection(true);
75                 }
76             }
77
78             @Override
79             public void onFailure(final Throwable t) {
80                 LOG.error("failed to get sweep barrier after posthandshake for device {}", connectionContext.getNodeId());
81                 connectionContext.closeConnection(false);
82             }
83         });
84     }
85
86     protected ListenableFuture<RpcResult<BarrierOutput>> fireBarrier(final Short version, final long xid) {
87         final BarrierInput barrierInput = new BarrierInputBuilder()
88                 .setXid(xid)
89                 .setVersion(version)
90                 .build();
91         return JdkFutureAdapters.listenInPoolThread(
92                 connectionContext.getConnectionAdapter().barrier(barrierInput));
93     }
94
95     @Override
96     public void onHandshakeFailure() {
97         LOG.debug("handshake failed: {}", connectionContext.getConnectionAdapter().getRemoteAddress());
98         closeHandshakeContext();
99         connectionContext.closeConnection(false);
100     }
101
102     private void closeHandshakeContext() {
103         try {
104             handshakeContext.close();
105         } catch (final Exception e) {
106             LOG.error("Closing handshake context failed: {}", e.getMessage());
107             LOG.debug("Detail in handshake context close: {}", e);
108         }
109     }
110
111     @Override
112     public void setHandshakeContext(final HandshakeContext handshakeContext) {
113         this.handshakeContext = handshakeContext;
114     }
115 }