Merge "un - throttle connection interval set to 10ms"
[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 package org.opendaylight.openflowplugin.impl.connection.listener;
9
10 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
11 import org.opendaylight.openflowplugin.api.openflow.connection.HandshakeContext;
12 import org.opendaylight.openflowplugin.api.openflow.device.handlers.DeviceConnectedHandler;
13 import org.opendaylight.openflowplugin.api.openflow.md.core.HandshakeListener;
14 import org.opendaylight.openflowplugin.openflow.md.util.InventoryDataServiceUtil;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutput;
16 import org.slf4j.Logger;
17 import org.slf4j.LoggerFactory;
18
19 /**
20  *
21  */
22 public class HandshakeListenerImpl implements HandshakeListener {
23
24     private static final Logger LOG = LoggerFactory.getLogger(HandshakeListenerImpl.class);
25
26     private ConnectionContext connectionContext;
27     private DeviceConnectedHandler deviceConnectedHandler;
28     private HandshakeContext handshakeContext;
29
30     /**
31      * @param connectionContext
32      * @param deviceConnectedHandler
33      */
34     public HandshakeListenerImpl(ConnectionContext connectionContext, DeviceConnectedHandler deviceConnectedHandler) {
35         this.connectionContext = connectionContext;
36         this.deviceConnectedHandler = deviceConnectedHandler;
37     }
38
39     @Override
40     public void onHandshakeSuccessfull(GetFeaturesOutput featureOutput, Short version) {
41         connectionContext.setConnectionState(ConnectionContext.CONNECTION_STATE.WORKING);
42         connectionContext.setFeatures(featureOutput);
43         connectionContext.setNodeId(InventoryDataServiceUtil.nodeIdFromDatapathId(featureOutput.getDatapathId()));
44         deviceConnectedHandler.deviceConnected(connectionContext);
45     }
46
47     @Override
48     public void onHandshakeFailure() {
49         LOG.info("handshake failed: {}", connectionContext.getConnectionAdapter().getRemoteAddress());
50         connectionContext.setConnectionState(ConnectionContext.CONNECTION_STATE.RIP);
51         try {
52             handshakeContext.close();
53         } catch (Exception e) {
54             LOG.warn("Closing handshake context failed: {}", e.getMessage());
55             LOG.debug("Detail in hanshake context close:", e);
56         }
57     }
58
59     @Override
60     public void setHandshakeContext(HandshakeContext handshakeContext) {
61         this.handshakeContext = handshakeContext;
62     }
63 }