OPNFLWPLUG-1090: ConcurrentModificationException: null when connecting cbench switches
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / connection / ConnectionManagerImpl.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.openflowplugin.impl.connection;
9
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;
17 import java.util.Map;
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;
55
56 public class ConnectionManagerImpl implements ConnectionManager {
57
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")
62             .setDaemon(false)
63             .setUncaughtExceptionHandler((thread, ex) -> LOG.error("Uncaught exception {}", thread, ex))
64             .build();
65     private final ExecutorService executorsService = Executors.newCachedThreadPool(threadFactory);
66
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;
76
77     public ConnectionManagerImpl(final OpenflowProviderConfig config, final ExecutorService executorService,
78                                  final DataBroker dataBroker,
79                                  @NonNull final NotificationPublishService notificationPublishService) {
80         this.config = config;
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;
88     }
89
90     @Override
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);
97
98         HandshakeListener handshakeListener = new HandshakeListenerImpl(connectionContext, deviceConnectedHandler);
99         final HandshakeManager handshakeManager = createHandshakeManager(connectionAdapter, handshakeListener);
100
101         LOG.trace("prepare handshake context");
102         HandshakeContext handshakeContext = new HandshakeContextImpl(executorService, handshakeManager);
103         handshakeListener.setHandshakeContext(handshakeContext);
104         connectionContext.setHandshakeContext(handshakeContext);
105
106         LOG.trace("prepare connection listeners");
107         final ConnectionReadyListener connectionReadyListener = new ConnectionReadyListenerImpl(
108                 connectionContext, handshakeContext);
109         connectionAdapter.setConnectionReadyListener(connectionReadyListener);
110
111         final OpenflowProtocolListener ofMessageListener =
112                 new OpenflowProtocolListenerInitialImpl(connectionContext, handshakeContext);
113         connectionAdapter.setMessageListener(ofMessageListener);
114
115         final SystemNotificationsListener systemListener = new SystemNotificationsListenerImpl(connectionContext,
116                 config.getEchoReplyTimeout().getValue().toJava(), executorService, notificationPublishService);
117         connectionAdapter.setSystemListener(systemListener);
118
119         LOG.trace("connection ballet finished");
120     }
121
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);
128
129         return handshakeManager;
130     }
131
132     @Override
133     public boolean accept(final InetAddress switchAddress) {
134         // TODO add connection accept logic based on address
135         return true;
136     }
137
138     @Override
139     public void setDeviceConnectedHandler(final DeviceConnectedHandler deviceConnectedHandler) {
140         this.deviceConnectedHandler = deviceConnectedHandler;
141     }
142
143     @Override
144     public void setDeviceDisconnectedHandler(final DeviceDisconnectedHandler deviceDisconnectedHandler) {
145         this.deviceDisconnectedHandler = deviceDisconnectedHandler;
146     }
147
148     @VisibleForTesting
149     DeviceConnectionStatusProvider getDeviceConnectionStatusProvider() {
150         return deviceConnectionStatusProvider;
151     }
152
153     @Override
154     public void close() throws Exception {
155         if (deviceConnectionStatusProvider != null) {
156             deviceConnectionStatusProvider.close();
157             deviceConnectionStatusProvider = null;
158         }
159         if (executorsService != null) {
160             executorsService.shutdownNow();
161         }
162     }
163
164     class DeviceConnectionStatusProviderImpl implements DeviceConnectionStatusProvider,
165             ClusteredDataTreeChangeListener<Node> {
166         private final Map<BigInteger, LocalDateTime> deviceConnectionMap = new ConcurrentHashMap<>();
167
168         private ListenerRegistration<DeviceConnectionStatusProviderImpl> listenerRegistration;
169
170         @Override
171         @SuppressWarnings({"checkstyle:IllegalCatch"})
172         public void init() {
173             DataTreeIdentifier<Node> treeId = DataTreeIdentifier.create(LogicalDatastoreType.OPERATIONAL,
174                     getWildCardPath());
175             try {
176                 listenerRegistration = dataBroker.registerDataTreeChangeListener(treeId, this);
177             } catch (Exception e) {
178                 LOG.error("DeviceConnectionStatusProvider listener registration failed", e);
179             }
180         }
181
182         @Override
183         public LocalDateTime getDeviceLastConnectionTime(BigInteger nodeId) {
184             return deviceConnectionMap.get(nodeId);
185         }
186
187         @Override
188         public void addDeviceLastConnectionTime(BigInteger nodeId, LocalDateTime time) {
189             deviceConnectionMap.put(nodeId, time);
190         }
191
192         @Override
193         public void removeDeviceLastConnectionTime(BigInteger nodeId) {
194             deviceConnectionMap.remove(nodeId);
195         }
196
197         @Override
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()) {
203                     case DELETE:
204                         break;
205                     case SUBTREE_MODIFIED:
206                         break;
207                     case WRITE:
208                         processNodeModification(change);
209                         break;
210                     default:
211                         throw new IllegalArgumentException("Unhandled modification type " + mod.getModificationType());
212                 }
213             }
214         }
215
216         private InstanceIdentifier<Node> getWildCardPath() {
217             return InstanceIdentifier.create(Nodes.class).child(Node.class);
218         }
219
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));
227         }
228
229         @Override
230         public void close() {
231             if (listenerRegistration != null) {
232                 listenerRegistration.close();
233                 listenerRegistration = null;
234             }
235         }
236     }
237 }