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