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.ExecutorService;
13 import org.opendaylight.openflowjava.protocol.api.connection.ConnectionAdapter;
14 import org.opendaylight.openflowjava.protocol.api.connection.ConnectionReadyListener;
15 import org.opendaylight.openflowplugin.api.OFConstants;
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.DeviceDisconnectedHandler;
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.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 DeviceDisconnectedHandler deviceDisconnectedHandler;
42 public ConnectionManagerImpl(final OpenflowProviderConfig config, final ExecutorService executorService) {
44 this.executorService = executorService;
48 public void onSwitchConnected(final ConnectionAdapter connectionAdapter) {
49 LOG.trace("prepare connection context");
50 final ConnectionContext connectionContext = new ConnectionContextImpl(connectionAdapter);
51 connectionContext.setDeviceDisconnectedHandler(this.deviceDisconnectedHandler);
53 HandshakeListener handshakeListener = new HandshakeListenerImpl(connectionContext, deviceConnectedHandler);
54 final HandshakeManager handshakeManager = createHandshakeManager(connectionAdapter, handshakeListener);
56 LOG.trace("prepare handshake context");
57 HandshakeContext handshakeContext = new HandshakeContextImpl(executorService, handshakeManager);
58 handshakeListener.setHandshakeContext(handshakeContext);
59 connectionContext.setHandshakeContext(handshakeContext);
61 LOG.trace("prepare connection listeners");
62 final ConnectionReadyListener connectionReadyListener = new ConnectionReadyListenerImpl(
63 connectionContext, handshakeContext);
64 connectionAdapter.setConnectionReadyListener(connectionReadyListener);
66 final OpenflowProtocolListener ofMessageListener =
67 new OpenflowProtocolListenerInitialImpl(connectionContext, handshakeContext);
68 connectionAdapter.setMessageListener(ofMessageListener);
70 final SystemNotificationsListener systemListener = new SystemNotificationsListenerImpl(
71 connectionContext, config.getEchoReplyTimeout().getValue(), executorService);
72 connectionAdapter.setSystemListener(systemListener);
74 LOG.trace("connection ballet finished");
77 private HandshakeManager createHandshakeManager(final ConnectionAdapter connectionAdapter,
78 final HandshakeListener handshakeListener) {
79 HandshakeManagerImpl handshakeManager = new HandshakeManagerImpl(connectionAdapter,
80 OFConstants.VERSION_ORDER.get(0),
81 OFConstants.VERSION_ORDER, new ErrorHandlerSimpleImpl(), handshakeListener, BITMAP_NEGOTIATION_ENABLED);
83 return handshakeManager;
87 public boolean accept(final InetAddress switchAddress) {
88 // TODO add connection accept logic based on address
93 public void setDeviceConnectedHandler(final DeviceConnectedHandler deviceConnectedHandler) {
94 this.deviceConnectedHandler = deviceConnectedHandler;
98 public void setDeviceDisconnectedHandler(final DeviceDisconnectedHandler deviceDisconnectedHandler) {
99 this.deviceDisconnectedHandler = deviceDisconnectedHandler;