BUG-4236: resolve TODOs - part 1
[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.ArrayBlockingQueue;
13 import java.util.concurrent.TimeUnit;
14 import org.opendaylight.openflowjava.protocol.api.connection.ConnectionAdapter;
15 import org.opendaylight.openflowjava.protocol.api.connection.ConnectionReadyListener;
16 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
17 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionManager;
18 import org.opendaylight.openflowplugin.api.openflow.connection.HandshakeContext;
19 import org.opendaylight.openflowplugin.api.openflow.device.handlers.DeviceConnectedHandler;
20 import org.opendaylight.openflowplugin.api.openflow.device.handlers.MessageHandler;
21 import org.opendaylight.openflowplugin.api.openflow.md.core.ConnectionConductor;
22 import org.opendaylight.openflowplugin.api.openflow.md.core.HandshakeListener;
23 import org.opendaylight.openflowplugin.api.openflow.md.core.HandshakeManager;
24 import org.opendaylight.openflowplugin.impl.connection.listener.ConnectionReadyListenerImpl;
25 import org.opendaylight.openflowplugin.impl.connection.listener.HandshakeListenerImpl;
26 import org.opendaylight.openflowplugin.impl.connection.listener.OpenflowProtocolListenerInitialImpl;
27 import org.opendaylight.openflowplugin.impl.connection.listener.SystemNotificationsListenerImpl;
28 import org.opendaylight.openflowplugin.openflow.md.core.ErrorHandlerSimpleImpl;
29 import org.opendaylight.openflowplugin.openflow.md.core.HandshakeManagerImpl;
30 import org.opendaylight.openflowplugin.openflow.md.core.ThreadPoolLoggingExecutor;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OpenflowProtocolListener;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.system.rev130927.SystemNotificationsListener;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
36 /**
37  *
38  */
39 public class ConnectionManagerImpl implements ConnectionManager {
40
41     private static final Logger LOG = LoggerFactory.getLogger(ConnectionManagerImpl.class);
42     private static final int HELLO_LIMIT = 20;
43     private final boolean bitmapNegotiationEnabled = true;
44     private DeviceConnectedHandler deviceConnectedHandler;
45
46     @Override
47     public void onSwitchConnected(final ConnectionAdapter connectionAdapter) {
48
49         LOG.trace("preparing handshake: {}", connectionAdapter.getRemoteAddress());
50
51         final int handshakeThreadLimit = 1;
52         final ThreadPoolLoggingExecutor handshakePool = createHandshakePool(
53                 connectionAdapter.getRemoteAddress().toString(), handshakeThreadLimit);
54
55         LOG.trace("prepare connection context");
56         final ConnectionContext connectionContext = new ConnectionContextImpl(connectionAdapter);
57
58         HandshakeListener handshakeListener = new HandshakeListenerImpl(connectionContext, deviceConnectedHandler);
59         final HandshakeManager handshakeManager = createHandshakeManager(connectionAdapter, handshakeListener);
60
61         LOG.trace("prepare handshake context");
62         HandshakeContext handshakeContext = new HandshakeContextImpl(handshakePool, handshakeManager);
63         handshakeListener.setHandshakeContext(handshakeContext);
64         connectionContext.setHandshakeContext(handshakeContext);
65
66         LOG.trace("prepare connection listeners");
67         final ConnectionReadyListener connectionReadyListener = new ConnectionReadyListenerImpl(
68                 connectionContext, handshakeContext);
69         connectionAdapter.setConnectionReadyListener(connectionReadyListener);
70
71         final OpenflowProtocolListener ofMessageListener =
72                 new OpenflowProtocolListenerInitialImpl(connectionContext, handshakeContext);
73         connectionAdapter.setMessageListener(ofMessageListener);
74
75         final SystemNotificationsListener systemListener = new SystemNotificationsListenerImpl(connectionContext);
76         connectionAdapter.setSystemListener(systemListener);
77
78         LOG.trace("connection ballet finished");
79     }
80
81     /**
82      * @param connectionIdentifier
83      * @param handshakeThreadLimit
84      * @return
85      */
86     private static ThreadPoolLoggingExecutor createHandshakePool(
87             final String connectionIdentifier, final int handshakeThreadLimit) {
88         return new ThreadPoolLoggingExecutor(handshakeThreadLimit,
89                 handshakeThreadLimit, 0L, TimeUnit.MILLISECONDS,
90                 new ArrayBlockingQueue<Runnable>(HELLO_LIMIT), "OFHandshake-" + connectionIdentifier);
91     }
92
93     /**
94      * @param connectionAdapter
95      * @param handshakeListener
96      * @return
97      */
98     private HandshakeManager createHandshakeManager(final ConnectionAdapter connectionAdapter,
99                                                     final HandshakeListener handshakeListener) {
100         HandshakeManagerImpl handshakeManager = new HandshakeManagerImpl(connectionAdapter,
101                 ConnectionConductor.versionOrder.get(0),
102                 ConnectionConductor.versionOrder);
103         handshakeManager.setUseVersionBitmap(isBitmapNegotiationEnabled());
104         handshakeManager.setHandshakeListener(handshakeListener);
105         handshakeManager.setErrorHandler(new ErrorHandlerSimpleImpl());
106
107         return handshakeManager;
108     }
109
110     /**
111      * @return parameter dedicated to hello message content
112      */
113     public boolean isBitmapNegotiationEnabled() {
114         return bitmapNegotiationEnabled;
115     }
116
117
118     @Override
119     public boolean accept(final InetAddress switchAddress) {
120         // TODO add connection accept logic based on address
121         return true;
122     }
123
124     @Override
125     public void setDeviceConnectedHandler(final DeviceConnectedHandler deviceConnectedHandler) {
126         this.deviceConnectedHandler = deviceConnectedHandler;
127     }
128
129     @Override
130     public void setMessageHandler(final MessageHandler arg0) {
131         // TODO Auto-generated method stub
132
133     }
134 }