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
8 package org.opendaylight.openflowplugin.impl.connection;
10 import java.net.InetAddress;
11 import java.util.concurrent.ExecutorService;
12 import org.opendaylight.openflowjava.protocol.api.connection.ConnectionAdapter;
13 import org.opendaylight.openflowjava.protocol.api.connection.ConnectionReadyListener;
14 import org.opendaylight.openflowplugin.api.OFConstants;
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.HandshakeListener;
21 import org.opendaylight.openflowplugin.api.openflow.md.core.HandshakeManager;
22 import org.opendaylight.openflowplugin.impl.common.DeviceConnectionRateLimiter;
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.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OpenflowProtocolListener;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.system.rev130927.SystemNotificationsListener;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflow.provider.config.rev160510.OpenflowProviderConfig;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
33 public class ConnectionManagerImpl implements ConnectionManager {
35 private static final Logger LOG = LoggerFactory.getLogger(ConnectionManagerImpl.class);
36 private static final boolean BITMAP_NEGOTIATION_ENABLED = true;
37 private DeviceConnectedHandler deviceConnectedHandler;
38 private final OpenflowProviderConfig config;
39 private final ExecutorService executorService;
40 private final DeviceConnectionRateLimiter deviceConnectionRateLimiter;
41 private DeviceDisconnectedHandler deviceDisconnectedHandler;
43 public ConnectionManagerImpl(final OpenflowProviderConfig config, final ExecutorService executorService) {
45 this.executorService = executorService;
46 this.deviceConnectionRateLimiter = new DeviceConnectionRateLimiter(config);
50 public void onSwitchConnected(final ConnectionAdapter connectionAdapter) {
51 LOG.trace("prepare connection context");
52 final ConnectionContext connectionContext = new ConnectionContextImpl(connectionAdapter);
53 connectionContext.setDeviceDisconnectedHandler(this.deviceDisconnectedHandler);
55 HandshakeListener handshakeListener = new HandshakeListenerImpl(connectionContext, deviceConnectedHandler);
56 final HandshakeManager handshakeManager = createHandshakeManager(connectionAdapter, handshakeListener);
58 LOG.trace("prepare handshake context");
59 HandshakeContext handshakeContext = new HandshakeContextImpl(executorService, handshakeManager);
60 handshakeListener.setHandshakeContext(handshakeContext);
61 connectionContext.setHandshakeContext(handshakeContext);
63 LOG.trace("prepare connection listeners");
64 final ConnectionReadyListener connectionReadyListener = new ConnectionReadyListenerImpl(
65 connectionContext, handshakeContext);
66 connectionAdapter.setConnectionReadyListener(connectionReadyListener);
68 final OpenflowProtocolListener ofMessageListener =
69 new OpenflowProtocolListenerInitialImpl(connectionContext, handshakeContext);
70 connectionAdapter.setMessageListener(ofMessageListener);
72 final SystemNotificationsListener systemListener = new SystemNotificationsListenerImpl(
73 connectionContext, config.getEchoReplyTimeout().getValue().toJava(), executorService);
74 connectionAdapter.setSystemListener(systemListener);
76 LOG.trace("connection ballet finished");
79 private HandshakeManager createHandshakeManager(final ConnectionAdapter connectionAdapter,
80 final HandshakeListener handshakeListener) {
81 HandshakeManagerImpl handshakeManager = new HandshakeManagerImpl(connectionAdapter,
82 OFConstants.VERSION_ORDER.get(0),
83 OFConstants.VERSION_ORDER, new ErrorHandlerSimpleImpl(), handshakeListener, BITMAP_NEGOTIATION_ENABLED,
84 deviceConnectionRateLimiter);
86 return handshakeManager;
90 public boolean accept(final InetAddress switchAddress) {
91 // TODO add connection accept logic based on address
96 public void setDeviceConnectedHandler(final DeviceConnectedHandler deviceConnectedHandler) {
97 this.deviceConnectedHandler = deviceConnectedHandler;
101 public void setDeviceDisconnectedHandler(final DeviceDisconnectedHandler deviceDisconnectedHandler) {
102 this.deviceDisconnectedHandler = deviceDisconnectedHandler;