Bug 5924 - Reuse Threads using ThreadPool in SystemNotificationListenerImpl
[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 int HELLO_LIMIT = 20;
40     private final boolean bitmapNegotiationEnabled = true;
41     private DeviceConnectedHandler deviceConnectedHandler;
42     private final long echoReplyTimeout;
43     private final ThreadPoolExecutor threadPool;
44
45     public ConnectionManagerImpl(long echoReplyTimeout, final ThreadPoolExecutor threadPool) {
46         this.echoReplyTimeout = echoReplyTimeout;
47         this.threadPool = threadPool;
48     }
49
50     @Override
51     public void onSwitchConnected(final ConnectionAdapter connectionAdapter) {
52         LOG.trace("prepare connection context");
53         final ConnectionContext connectionContext = new ConnectionContextImpl(connectionAdapter);
54
55         HandshakeListener handshakeListener = new HandshakeListenerImpl(connectionContext, deviceConnectedHandler);
56         final HandshakeManager handshakeManager = createHandshakeManager(connectionAdapter, handshakeListener);
57
58         LOG.trace("prepare handshake context");
59         HandshakeContext handshakeContext = new HandshakeContextImpl(threadPool, handshakeManager);
60         handshakeListener.setHandshakeContext(handshakeContext);
61         connectionContext.setHandshakeContext(handshakeContext);
62
63         LOG.trace("prepare connection listeners");
64         final ConnectionReadyListener connectionReadyListener = new ConnectionReadyListenerImpl(
65                 connectionContext, handshakeContext);
66         connectionAdapter.setConnectionReadyListener(connectionReadyListener);
67
68         final OpenflowProtocolListener ofMessageListener =
69                 new OpenflowProtocolListenerInitialImpl(connectionContext, handshakeContext);
70         connectionAdapter.setMessageListener(ofMessageListener);
71
72         final SystemNotificationsListener systemListener = new SystemNotificationsListenerImpl(connectionContext, echoReplyTimeout, threadPool);
73         connectionAdapter.setSystemListener(systemListener);
74
75         LOG.trace("connection ballet finished");
76     }
77
78     /**
79      * @param connectionAdapter
80      * @param handshakeListener
81      * @return
82      */
83     private HandshakeManager createHandshakeManager(final ConnectionAdapter connectionAdapter,
84                                                     final HandshakeListener handshakeListener) {
85         HandshakeManagerImpl handshakeManager = new HandshakeManagerImpl(connectionAdapter,
86                 ConnectionConductor.versionOrder.get(0),
87                 ConnectionConductor.versionOrder);
88         handshakeManager.setUseVersionBitmap(isBitmapNegotiationEnabled());
89         handshakeManager.setHandshakeListener(handshakeListener);
90         handshakeManager.setErrorHandler(new ErrorHandlerSimpleImpl());
91
92         return handshakeManager;
93     }
94
95     /**
96      * @return parameter dedicated to hello message content
97      */
98     public boolean isBitmapNegotiationEnabled() {
99         return bitmapNegotiationEnabled;
100     }
101
102     @Override
103     public boolean accept(final InetAddress switchAddress) {
104         // TODO add connection accept logic based on address
105         return true;
106     }
107
108     @Override
109     public void setDeviceConnectedHandler(final DeviceConnectedHandler deviceConnectedHandler) {
110         this.deviceConnectedHandler = deviceConnectedHandler;
111     }
112 }