f9bc7d4c603a947656a1a0c22a958ccb956ee534
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / connection / ConnectionManagerImpl.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;
10
11 import java.net.InetAddress;
12 import java.util.concurrent.ThreadPoolExecutor;
13 import org.opendaylight.openflowjava.protocol.api.connection.ConnectionAdapter;
14 import org.opendaylight.openflowjava.protocol.api.connection.ConnectionReadyListener;
15 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
16 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionManager;
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.ConnectionConductor;
20 import org.opendaylight.openflowplugin.api.openflow.md.core.HandshakeListener;
21 import org.opendaylight.openflowplugin.api.openflow.md.core.HandshakeManager;
22 import org.opendaylight.openflowplugin.impl.connection.listener.ConnectionReadyListenerImpl;
23 import org.opendaylight.openflowplugin.impl.connection.listener.HandshakeListenerImpl;
24 import org.opendaylight.openflowplugin.impl.connection.listener.OpenflowProtocolListenerInitialImpl;
25 import org.opendaylight.openflowplugin.impl.connection.listener.SystemNotificationsListenerImpl;
26 import org.opendaylight.openflowplugin.openflow.md.core.ErrorHandlerSimpleImpl;
27 import org.opendaylight.openflowplugin.openflow.md.core.HandshakeManagerImpl;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OpenflowProtocolListener;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.system.rev130927.SystemNotificationsListener;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33 /**
34  *
35  */
36 public class ConnectionManagerImpl implements ConnectionManager {
37
38     private static final Logger LOG = LoggerFactory.getLogger(ConnectionManagerImpl.class);
39     private static final boolean BITMAP_NEGOTIATION_ENABLED = true;
40     private DeviceConnectedHandler deviceConnectedHandler;
41     private long echoReplyTimeout;
42     private final ThreadPoolExecutor threadPool;
43
44     public ConnectionManagerImpl(long echoReplyTimeout, final ThreadPoolExecutor threadPool) {
45         this.echoReplyTimeout = echoReplyTimeout;
46         this.threadPool = threadPool;
47     }
48
49     @Override
50     public void onSwitchConnected(final ConnectionAdapter connectionAdapter) {
51         LOG.trace("prepare connection context");
52         final ConnectionContext connectionContext = new ConnectionContextImpl(connectionAdapter);
53
54         HandshakeListener handshakeListener = new HandshakeListenerImpl(connectionContext, deviceConnectedHandler);
55         final HandshakeManager handshakeManager = createHandshakeManager(connectionAdapter, handshakeListener);
56
57         LOG.trace("prepare handshake context");
58         HandshakeContext handshakeContext = new HandshakeContextImpl(threadPool, handshakeManager);
59         handshakeListener.setHandshakeContext(handshakeContext);
60         connectionContext.setHandshakeContext(handshakeContext);
61
62         LOG.trace("prepare connection listeners");
63         final ConnectionReadyListener connectionReadyListener = new ConnectionReadyListenerImpl(
64                 connectionContext, handshakeContext);
65         connectionAdapter.setConnectionReadyListener(connectionReadyListener);
66
67         final OpenflowProtocolListener ofMessageListener =
68                 new OpenflowProtocolListenerInitialImpl(connectionContext, handshakeContext);
69         connectionAdapter.setMessageListener(ofMessageListener);
70
71         final SystemNotificationsListener systemListener = new SystemNotificationsListenerImpl(connectionContext, echoReplyTimeout, threadPool);
72         connectionAdapter.setSystemListener(systemListener);
73
74         LOG.trace("connection ballet finished");
75     }
76
77     /**
78      * @param connectionAdapter
79      * @param handshakeListener
80      * @return
81      */
82     private HandshakeManager createHandshakeManager(final ConnectionAdapter connectionAdapter,
83                                                     final HandshakeListener handshakeListener) {
84         HandshakeManagerImpl handshakeManager = new HandshakeManagerImpl(connectionAdapter,
85                 ConnectionConductor.versionOrder.get(0),
86                 ConnectionConductor.versionOrder);
87         handshakeManager.setUseVersionBitmap(BITMAP_NEGOTIATION_ENABLED);
88         handshakeManager.setHandshakeListener(handshakeListener);
89         handshakeManager.setErrorHandler(new ErrorHandlerSimpleImpl());
90
91         return handshakeManager;
92     }
93
94     @Override
95     public boolean accept(final InetAddress switchAddress) {
96         // TODO add connection accept logic based on address
97         return true;
98     }
99
100     @Override
101     public void setDeviceConnectedHandler(final DeviceConnectedHandler deviceConnectedHandler) {
102         this.deviceConnectedHandler = deviceConnectedHandler;
103     }
104
105     public void setEchoReplyTimeout(long echoReplyTimeout){
106         this.echoReplyTimeout = echoReplyTimeout;
107     }
108 }