b17d3675649edacdac47c5f7847b7fa9535381c9
[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 org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
12 import org.opendaylight.openflowplugin.api.openflow.connection.HandshakeContext;
13 import org.opendaylight.openflowplugin.api.openflow.device.handlers.DeviceConnectedHandler;
14 import org.opendaylight.openflowplugin.api.openflow.md.core.HandshakeListener;
15 import org.opendaylight.openflowplugin.impl.statistics.ofpspecific.SessionStatistics;
16 import org.opendaylight.openflowplugin.openflow.md.util.InventoryDataServiceUtil;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutput;
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
20
21 /**
22  *
23  */
24 public class HandshakeListenerImpl implements HandshakeListener {
25
26     private static final Logger LOG = LoggerFactory.getLogger(HandshakeListenerImpl.class);
27
28     private ConnectionContext connectionContext;
29     private DeviceConnectedHandler deviceConnectedHandler;
30     private HandshakeContext handshakeContext;
31
32     /**
33      * @param connectionContext
34      * @param deviceConnectedHandler
35      */
36     public HandshakeListenerImpl(ConnectionContext connectionContext, DeviceConnectedHandler deviceConnectedHandler) {
37         this.connectionContext = connectionContext;
38         this.deviceConnectedHandler = deviceConnectedHandler;
39     }
40
41     @Override
42     public void onHandshakeSuccessfull(GetFeaturesOutput featureOutput, Short version) {
43         LOG.debug("handshake succeeded: {}", connectionContext.getConnectionAdapter().getRemoteAddress());
44         closeHandshakeContext();
45         connectionContext.changeStateToWorking();
46         connectionContext.setFeatures(featureOutput);
47         connectionContext.setNodeId(InventoryDataServiceUtil.nodeIdFromDatapathId(featureOutput.getDatapathId()));
48         deviceConnectedHandler.deviceConnected(connectionContext);
49         SessionStatistics.countEvent(connectionContext.getNodeId().toString(), SessionStatistics.ConnectionStatus.CONNECTION_CREATED);
50     }
51
52     @Override
53     public void onHandshakeFailure() {
54         LOG.debug("handshake failed: {}", connectionContext.getConnectionAdapter().getRemoteAddress());
55         closeHandshakeContext();
56         connectionContext.closeConnection(false);
57     }
58
59     private void closeHandshakeContext() {
60         try {
61             handshakeContext.close();
62         } catch (Exception e) {
63             LOG.warn("Closing handshake context failed: {}", e.getMessage());
64             LOG.debug("Detail in hanshake context close:", e);
65         }
66     }
67
68     @Override
69     public void setHandshakeContext(HandshakeContext handshakeContext) {
70         this.handshakeContext = handshakeContext;
71     }
72 }