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 com.google.common.annotations.VisibleForTesting;
11 import com.google.common.base.Preconditions;
12 import com.google.common.util.concurrent.ThreadFactoryBuilder;
13 import java.math.BigInteger;
14 import java.net.InetAddress;
15 import java.time.LocalDateTime;
16 import java.util.Collection;
18 import java.util.concurrent.ConcurrentHashMap;
19 import java.util.concurrent.ExecutorService;
20 import java.util.concurrent.Executors;
21 import java.util.concurrent.ThreadFactory;
22 import org.eclipse.jdt.annotation.NonNull;
23 import org.opendaylight.mdsal.binding.api.ClusteredDataTreeChangeListener;
24 import org.opendaylight.mdsal.binding.api.DataBroker;
25 import org.opendaylight.mdsal.binding.api.DataObjectModification;
26 import org.opendaylight.mdsal.binding.api.DataTreeIdentifier;
27 import org.opendaylight.mdsal.binding.api.DataTreeModification;
28 import org.opendaylight.mdsal.binding.api.NotificationPublishService;
29 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
30 import org.opendaylight.openflowjava.protocol.api.connection.ConnectionAdapter;
31 import org.opendaylight.openflowjava.protocol.api.connection.ConnectionReadyListener;
32 import org.opendaylight.openflowplugin.api.OFConstants;
33 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
34 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionManager;
35 import org.opendaylight.openflowplugin.api.openflow.connection.DeviceConnectionStatusProvider;
36 import org.opendaylight.openflowplugin.api.openflow.connection.HandshakeContext;
37 import org.opendaylight.openflowplugin.api.openflow.device.handlers.DeviceConnectedHandler;
38 import org.opendaylight.openflowplugin.api.openflow.device.handlers.DeviceDisconnectedHandler;
39 import org.opendaylight.openflowplugin.api.openflow.md.core.HandshakeListener;
40 import org.opendaylight.openflowplugin.api.openflow.md.core.HandshakeManager;
41 import org.opendaylight.openflowplugin.impl.common.DeviceConnectionRateLimiter;
42 import org.opendaylight.openflowplugin.impl.connection.listener.ConnectionReadyListenerImpl;
43 import org.opendaylight.openflowplugin.impl.connection.listener.HandshakeListenerImpl;
44 import org.opendaylight.openflowplugin.impl.connection.listener.OpenflowProtocolListenerInitialImpl;
45 import org.opendaylight.openflowplugin.impl.connection.listener.SystemNotificationsListenerImpl;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OpenflowProtocolListener;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.system.rev130927.SystemNotificationsListener;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflow.provider.config.rev160510.OpenflowProviderConfig;
51 import org.opendaylight.yangtools.concepts.ListenerRegistration;
52 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
53 import org.slf4j.Logger;
54 import org.slf4j.LoggerFactory;
56 public class ConnectionManagerImpl implements ConnectionManager {
58 private static final Logger LOG = LoggerFactory.getLogger(ConnectionManagerImpl.class);
59 private static final boolean BITMAP_NEGOTIATION_ENABLED = true;
60 private final ThreadFactory threadFactory = new ThreadFactoryBuilder()
61 .setNameFormat("ConnectionHandler-%d")
63 .setUncaughtExceptionHandler((thread, ex) -> LOG.error("Uncaught exception {}", thread, ex))
65 private final ExecutorService executorsService = Executors.newCachedThreadPool(threadFactory);
67 private DeviceConnectedHandler deviceConnectedHandler;
68 private final OpenflowProviderConfig config;
69 private final ExecutorService executorService;
70 private final DeviceConnectionRateLimiter deviceConnectionRateLimiter;
71 private final DataBroker dataBroker;
72 private final int deviceConnectionHoldTime;
73 private DeviceDisconnectedHandler deviceDisconnectedHandler;
74 private DeviceConnectionStatusProvider deviceConnectionStatusProvider;
75 private final NotificationPublishService notificationPublishService;
77 public ConnectionManagerImpl(final OpenflowProviderConfig config, final ExecutorService executorService,
78 final DataBroker dataBroker,
79 @NonNull final NotificationPublishService notificationPublishService) {
81 this.executorService = executorService;
82 this.deviceConnectionRateLimiter = new DeviceConnectionRateLimiter(config);
83 this.dataBroker = dataBroker;
84 this.deviceConnectionHoldTime = config.getDeviceConnectionHoldTimeInSeconds().toJava();
85 deviceConnectionStatusProvider = new DeviceConnectionStatusProviderImpl();
86 deviceConnectionStatusProvider.init();
87 this.notificationPublishService = notificationPublishService;
91 public void onSwitchConnected(final ConnectionAdapter connectionAdapter) {
92 connectionAdapter.setExecutorService(executorsService);
93 LOG.trace("prepare connection context");
94 final ConnectionContext connectionContext = new ConnectionContextImpl(connectionAdapter,
95 deviceConnectionStatusProvider);
96 connectionContext.setDeviceDisconnectedHandler(this.deviceDisconnectedHandler);
98 HandshakeListener handshakeListener = new HandshakeListenerImpl(connectionContext, deviceConnectedHandler);
99 final HandshakeManager handshakeManager = createHandshakeManager(connectionAdapter, handshakeListener);
101 LOG.trace("prepare handshake context");
102 HandshakeContext handshakeContext = new HandshakeContextImpl(executorService, handshakeManager);
103 handshakeListener.setHandshakeContext(handshakeContext);
104 connectionContext.setHandshakeContext(handshakeContext);
106 LOG.trace("prepare connection listeners");
107 final ConnectionReadyListener connectionReadyListener = new ConnectionReadyListenerImpl(
108 connectionContext, handshakeContext);
109 connectionAdapter.setConnectionReadyListener(connectionReadyListener);
111 final OpenflowProtocolListener ofMessageListener =
112 new OpenflowProtocolListenerInitialImpl(connectionContext, handshakeContext);
113 connectionAdapter.setMessageListener(ofMessageListener);
115 final SystemNotificationsListener systemListener = new SystemNotificationsListenerImpl(connectionContext,
116 config.getEchoReplyTimeout().getValue().toJava(), executorService, notificationPublishService);
117 connectionAdapter.setSystemListener(systemListener);
119 LOG.trace("connection ballet finished");
122 private HandshakeManager createHandshakeManager(final ConnectionAdapter connectionAdapter,
123 final HandshakeListener handshakeListener) {
124 HandshakeManagerImpl handshakeManager = new HandshakeManagerImpl(connectionAdapter,
125 OFConstants.VERSION_ORDER.get(0),
126 OFConstants.VERSION_ORDER, new ErrorHandlerSimpleImpl(), handshakeListener, BITMAP_NEGOTIATION_ENABLED,
127 deviceConnectionRateLimiter, deviceConnectionHoldTime, deviceConnectionStatusProvider);
129 return handshakeManager;
133 public boolean accept(final InetAddress switchAddress) {
134 // TODO add connection accept logic based on address
139 public void setDeviceConnectedHandler(final DeviceConnectedHandler deviceConnectedHandler) {
140 this.deviceConnectedHandler = deviceConnectedHandler;
144 public void setDeviceDisconnectedHandler(final DeviceDisconnectedHandler deviceDisconnectedHandler) {
145 this.deviceDisconnectedHandler = deviceDisconnectedHandler;
149 DeviceConnectionStatusProvider getDeviceConnectionStatusProvider() {
150 return deviceConnectionStatusProvider;
154 public void close() throws Exception {
155 if (deviceConnectionStatusProvider != null) {
156 deviceConnectionStatusProvider.close();
157 deviceConnectionStatusProvider = null;
159 if (executorsService != null) {
160 executorsService.shutdownNow();
164 class DeviceConnectionStatusProviderImpl implements DeviceConnectionStatusProvider,
165 ClusteredDataTreeChangeListener<Node> {
166 private final Map<BigInteger, LocalDateTime> deviceConnectionMap = new ConcurrentHashMap<>();
168 private ListenerRegistration<DeviceConnectionStatusProviderImpl> listenerRegistration;
171 @SuppressWarnings({"checkstyle:IllegalCatch"})
173 DataTreeIdentifier<Node> treeId = DataTreeIdentifier.create(LogicalDatastoreType.OPERATIONAL,
176 listenerRegistration = dataBroker.registerDataTreeChangeListener(treeId, this);
177 } catch (Exception e) {
178 LOG.error("DeviceConnectionStatusProvider listener registration failed", e);
183 public LocalDateTime getDeviceLastConnectionTime(BigInteger nodeId) {
184 return deviceConnectionMap.get(nodeId);
188 public void addDeviceLastConnectionTime(BigInteger nodeId, LocalDateTime time) {
189 deviceConnectionMap.put(nodeId, time);
193 public void removeDeviceLastConnectionTime(BigInteger nodeId) {
194 deviceConnectionMap.remove(nodeId);
198 public void onDataTreeChanged(@NonNull Collection<DataTreeModification<Node>> changes) {
199 Preconditions.checkNotNull(changes, "Changes must not be null!");
200 for (DataTreeModification<Node> change : changes) {
201 final DataObjectModification<Node> mod = change.getRootNode();
202 switch (mod.getModificationType()) {
205 case SUBTREE_MODIFIED:
208 processNodeModification(change);
211 throw new IllegalArgumentException("Unhandled modification type " + mod.getModificationType());
216 private InstanceIdentifier<Node> getWildCardPath() {
217 return InstanceIdentifier.create(Nodes.class).child(Node.class);
220 private void processNodeModification(DataTreeModification<Node> change) {
221 final InstanceIdentifier<Node> key = change.getRootPath().getRootIdentifier();
222 final InstanceIdentifier<Node> nodeIdent = key.firstIdentifierOf(Node.class);
223 String[] nodeIdentity = nodeIdent.firstKeyOf(Node.class).getId().getValue().split(":");
224 String nodeId = nodeIdentity[1];
225 LOG.info("Clearing the device connection timer for the device {}", nodeId);
226 removeDeviceLastConnectionTime(new BigInteger(nodeId));
230 public void close() {
231 if (listenerRegistration != null) {
232 listenerRegistration.close();
233 listenerRegistration = null;