2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.openflowplugin.impl.connection;
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;
39 public class ConnectionManagerImpl implements ConnectionManager {
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;
47 public void onSwitchConnected(final ConnectionAdapter connectionAdapter) {
49 LOG.trace("preparing handshake: {}", connectionAdapter.getRemoteAddress());
51 final int handshakeThreadLimit = 1;
52 final ThreadPoolLoggingExecutor handshakePool = createHandshakePool(
53 connectionAdapter.getRemoteAddress().toString(), handshakeThreadLimit);
55 LOG.trace("prepare connection context");
56 final ConnectionContext connectionContext = new ConnectionContextImpl(connectionAdapter);
58 HandshakeListener handshakeListener = new HandshakeListenerImpl(connectionContext, deviceConnectedHandler);
59 final HandshakeManager handshakeManager = createHandshakeManager(connectionAdapter, handshakeListener);
61 LOG.trace("prepare handshake context");
62 HandshakeContext handshakeContext = new HandshakeContextImpl(handshakePool, handshakeManager);
63 handshakeListener.setHandshakeContext(handshakeContext);
64 connectionContext.setHandshakeContext(handshakeContext);
66 LOG.trace("prepare connection listeners");
67 final ConnectionReadyListener connectionReadyListener = new ConnectionReadyListenerImpl(
68 connectionContext, handshakeContext);
69 connectionAdapter.setConnectionReadyListener(connectionReadyListener);
71 final OpenflowProtocolListener ofMessageListener =
72 new OpenflowProtocolListenerInitialImpl(connectionContext, handshakeContext);
73 connectionAdapter.setMessageListener(ofMessageListener);
75 final SystemNotificationsListener systemListener = new SystemNotificationsListenerImpl(connectionContext);
76 connectionAdapter.setSystemListener(systemListener);
78 LOG.trace("connection ballet finished");
82 * @param connectionIdentifier
83 * @param handshakeThreadLimit
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);
94 * @param connectionAdapter
95 * @param handshakeListener
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());
107 return handshakeManager;
111 * @return parameter dedicated to hello message content
113 public boolean isBitmapNegotiationEnabled() {
114 return bitmapNegotiationEnabled;
119 public boolean accept(final InetAddress switchAddress) {
120 // TODO add connection accept logic based on address
125 public void setDeviceConnectedHandler(final DeviceConnectedHandler deviceConnectedHandler) {
126 this.deviceConnectedHandler = deviceConnectedHandler;
130 public void setMessageHandler(final MessageHandler arg0) {
131 // TODO Auto-generated method stub