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.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;
38 public class ConnectionManagerImpl implements ConnectionManager {
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;
46 public void onSwitchConnected(final ConnectionAdapter connectionAdapter) {
48 LOG.trace("preparing handshake: {}", connectionAdapter.getRemoteAddress());
50 final int handshakeThreadLimit = 1;
51 final ThreadPoolLoggingExecutor handshakePool = createHandshakePool(
52 connectionAdapter.getRemoteAddress().toString(), handshakeThreadLimit);
54 LOG.trace("prepare connection context");
55 final ConnectionContext connectionContext = new ConnectionContextImpl(connectionAdapter);
57 HandshakeListener handshakeListener = new HandshakeListenerImpl(connectionContext, deviceConnectedHandler);
58 final HandshakeManager handshakeManager = createHandshakeManager(connectionAdapter, handshakeListener);
60 LOG.trace("prepare handshake context");
61 HandshakeContext handshakeContext = new HandshakeContextImpl(handshakePool, handshakeManager);
62 handshakeListener.setHandshakeContext(handshakeContext);
63 connectionContext.setHandshakeContext(handshakeContext);
65 LOG.trace("prepare connection listeners");
66 final ConnectionReadyListener connectionReadyListener = new ConnectionReadyListenerImpl(
67 connectionContext, handshakeContext);
68 connectionAdapter.setConnectionReadyListener(connectionReadyListener);
70 final OpenflowProtocolListener ofMessageListener =
71 new OpenflowProtocolListenerInitialImpl(connectionContext, handshakeContext);
72 connectionAdapter.setMessageListener(ofMessageListener);
74 final SystemNotificationsListener systemListener = new SystemNotificationsListenerImpl(connectionContext);
75 connectionAdapter.setSystemListener(systemListener);
77 LOG.trace("connection ballet finished");
81 * @param connectionIdentifier
82 * @param handshakeThreadLimit
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);
93 * @param connectionAdapter
94 * @param handshakeListener
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());
106 return handshakeManager;
110 * @return parameter dedicated to hello message content
112 public boolean isBitmapNegotiationEnabled() {
113 return bitmapNegotiationEnabled;
118 public boolean accept(final InetAddress switchAddress) {
119 // TODO add connection accept logic based on address
124 public void setDeviceConnectedHandler(final DeviceConnectedHandler deviceConnectedHandler) {
125 this.deviceConnectedHandler = deviceConnectedHandler;