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.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.device.handlers.DeviceDisconnectedHandler;
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.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OpenflowProtocolListener;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.system.rev130927.SystemNotificationsListener;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
37 public class ConnectionManagerImpl implements ConnectionManager {
39 private static final Logger LOG = LoggerFactory.getLogger(ConnectionManagerImpl.class);
40 private static final boolean BITMAP_NEGOTIATION_ENABLED = true;
41 private DeviceConnectedHandler deviceConnectedHandler;
42 private long echoReplyTimeout;
43 private final ThreadPoolExecutor threadPool;
44 private DeviceDisconnectedHandler deviceDisconnectedHandler;
46 public ConnectionManagerImpl(long echoReplyTimeout, final ThreadPoolExecutor threadPool) {
47 this.echoReplyTimeout = echoReplyTimeout;
48 this.threadPool = threadPool;
52 public void onSwitchConnected(final ConnectionAdapter connectionAdapter) {
53 LOG.trace("prepare connection context");
54 final ConnectionContext connectionContext = new ConnectionContextImpl(connectionAdapter);
55 connectionContext.setDeviceDisconnectedHandler(this.deviceDisconnectedHandler);
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(threadPool, 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, echoReplyTimeout, threadPool);
75 connectionAdapter.setSystemListener(systemListener);
77 LOG.trace("connection ballet finished");
81 * @param connectionAdapter
82 * @param handshakeListener
85 private HandshakeManager createHandshakeManager(final ConnectionAdapter connectionAdapter,
86 final HandshakeListener handshakeListener) {
87 HandshakeManagerImpl handshakeManager = new HandshakeManagerImpl(connectionAdapter,
88 ConnectionConductor.VERSION_ORDER.get(0),
89 ConnectionConductor.VERSION_ORDER);
90 handshakeManager.setUseVersionBitmap(BITMAP_NEGOTIATION_ENABLED);
91 handshakeManager.setHandshakeListener(handshakeListener);
92 handshakeManager.setErrorHandler(new ErrorHandlerSimpleImpl());
94 return handshakeManager;
98 public boolean accept(final InetAddress switchAddress) {
99 // TODO add connection accept logic based on address
104 public void setDeviceConnectedHandler(final DeviceConnectedHandler deviceConnectedHandler) {
105 this.deviceConnectedHandler = deviceConnectedHandler;
109 public void setDeviceDisconnectedHandler(final DeviceDisconnectedHandler deviceDisconnectedHandler) {
110 this.deviceDisconnectedHandler = deviceDisconnectedHandler;
113 public void setEchoReplyTimeout(long echoReplyTimeout){
114 this.echoReplyTimeout = echoReplyTimeout;