Modernize codebase
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / lifecycle / ContextChainHolderImpl.java
1 /*
2  * Copyright (c) 2016 Pantheon Technologies s.r.o. 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.lifecycle;
9
10 import com.google.common.annotations.VisibleForTesting;
11 import com.google.common.util.concurrent.FutureCallback;
12 import com.google.common.util.concurrent.Futures;
13 import com.google.common.util.concurrent.ListenableFuture;
14 import com.google.common.util.concurrent.MoreExecutors;
15 import com.google.common.util.concurrent.ThreadFactoryBuilder;
16 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
17 import java.util.HashMap;
18 import java.util.List;
19 import java.util.Map;
20 import java.util.Objects;
21 import java.util.Optional;
22 import java.util.concurrent.ConcurrentHashMap;
23 import java.util.concurrent.ConcurrentMap;
24 import java.util.concurrent.ExecutionException;
25 import java.util.concurrent.Executor;
26 import java.util.concurrent.Executors;
27 import java.util.concurrent.ScheduledExecutorService;
28 import java.util.concurrent.ThreadFactory;
29 import java.util.concurrent.TimeUnit;
30 import java.util.concurrent.TimeoutException;
31 import java.util.stream.Collectors;
32 import org.eclipse.jdt.annotation.NonNull;
33 import org.eclipse.jdt.annotation.Nullable;
34 import org.opendaylight.mdsal.eos.binding.api.EntityOwnershipChange;
35 import org.opendaylight.mdsal.eos.binding.api.EntityOwnershipListenerRegistration;
36 import org.opendaylight.mdsal.eos.binding.api.EntityOwnershipService;
37 import org.opendaylight.mdsal.eos.common.api.EntityOwnershipState;
38 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceProvider;
39 import org.opendaylight.openflowplugin.api.openflow.OFPManager;
40 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
41 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionStatus;
42 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
43 import org.opendaylight.openflowplugin.api.openflow.device.DeviceInfo;
44 import org.opendaylight.openflowplugin.api.openflow.device.DeviceManager;
45 import org.opendaylight.openflowplugin.api.openflow.lifecycle.ContextChain;
46 import org.opendaylight.openflowplugin.api.openflow.lifecycle.ContextChainHolder;
47 import org.opendaylight.openflowplugin.api.openflow.lifecycle.ContextChainMastershipState;
48 import org.opendaylight.openflowplugin.api.openflow.lifecycle.MasterChecker;
49 import org.opendaylight.openflowplugin.api.openflow.lifecycle.OwnershipChangeListener;
50 import org.opendaylight.openflowplugin.api.openflow.role.RoleContext;
51 import org.opendaylight.openflowplugin.api.openflow.role.RoleManager;
52 import org.opendaylight.openflowplugin.api.openflow.rpc.RpcContext;
53 import org.opendaylight.openflowplugin.api.openflow.rpc.RpcManager;
54 import org.opendaylight.openflowplugin.api.openflow.statistics.StatisticsContext;
55 import org.opendaylight.openflowplugin.api.openflow.statistics.StatisticsManager;
56 import org.opendaylight.openflowplugin.impl.util.DeviceStateUtil;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FeaturesReply;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.core.general.entity.rev150930.Entity;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflow.provider.config.rev160510.OpenflowProviderConfig;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflowplugin.rf.state.rev170713.ResultState;
64 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
65 import org.opendaylight.yangtools.yang.common.Uint8;
66 import org.slf4j.Logger;
67 import org.slf4j.LoggerFactory;
68
69 public final class ContextChainHolderImpl implements ContextChainHolder, MasterChecker {
70     private static final Logger LOG = LoggerFactory.getLogger(ContextChainHolderImpl.class);
71     private static final Logger OF_EVENT_LOG = LoggerFactory.getLogger("OfEventLog");
72
73     private static final String CONTEXT_CREATED_FOR_CONNECTION = " context created for connection: {}";
74     private static final long REMOVE_DEVICE_FROM_DS_TIMEOUT = 5000L;
75     private static final String ASYNC_SERVICE_ENTITY_TYPE = "org.opendaylight.mdsal.AsyncServiceCloseEntityType";
76     private static final String SERVICE_ENTITY_TYPE = "org.opendaylight.mdsal.ServiceEntityType";
77     private static final String SEPARATOR = ":";
78     private final ConcurrentMap<DeviceInfo, ContextChain> contextChainMap = new ConcurrentHashMap<>();
79     private final ConcurrentMap<DeviceInfo, ? super ConnectionContext> connectingDevices = new ConcurrentHashMap<>();
80     private final EntityOwnershipListenerRegistration eosListenerRegistration;
81     private final ClusterSingletonServiceProvider singletonServiceProvider;
82     private final Executor executor;
83     private final OwnershipChangeListener ownershipChangeListener;
84     private final ThreadFactory threadFactory = new ThreadFactoryBuilder().setDaemon(true)
85             .setNameFormat("node-cleaner-%d").setUncaughtExceptionHandler((thread, throwable) -> {
86                 LOG.warn("Uncaught exception while removing node data from operational datastore.", throwable);
87             }).build();
88     private final ScheduledExecutorService nodeCleanerExecutor = Executors.newScheduledThreadPool(
89             Runtime.getRuntime().availableProcessors() , threadFactory);
90     private final EntityOwnershipService entityOwnershipService;
91     private final OpenflowProviderConfig config;
92     private DeviceManager deviceManager;
93     private RpcManager rpcManager;
94     private StatisticsManager statisticsManager;
95     private RoleManager roleManager;
96
97     public ContextChainHolderImpl(final Executor executor,
98                                   final ClusterSingletonServiceProvider singletonServiceProvider,
99                                   final EntityOwnershipService entityOwnershipService,
100                                   final OwnershipChangeListener ownershipChangeListener,
101                                   final OpenflowProviderConfig config) {
102         this.singletonServiceProvider = singletonServiceProvider;
103         this.executor = executor;
104         this.ownershipChangeListener = ownershipChangeListener;
105         this.ownershipChangeListener.setMasterChecker(this);
106         this.entityOwnershipService = entityOwnershipService;
107         this.config = config;
108         eosListenerRegistration = Objects
109                 .requireNonNull(entityOwnershipService.registerListener(ASYNC_SERVICE_ENTITY_TYPE, this));
110     }
111
112     @Override
113     public <T extends OFPManager> void addManager(final T manager) {
114         if (deviceManager == null && manager instanceof DeviceManager) {
115             LOG.trace("Context chain holder: Device manager OK.");
116             deviceManager = (DeviceManager) manager;
117         } else if (rpcManager == null && manager instanceof RpcManager) {
118             LOG.trace("Context chain holder: RPC manager OK.");
119             rpcManager = (RpcManager) manager;
120         } else if (statisticsManager == null && manager instanceof StatisticsManager) {
121             LOG.trace("Context chain holder: Statistics manager OK.");
122             statisticsManager = (StatisticsManager) manager;
123         } else if (roleManager == null && manager instanceof RoleManager) {
124             LOG.trace("Context chain holder: Role manager OK.");
125             roleManager = (RoleManager) manager;
126         }
127     }
128
129     @VisibleForTesting
130     void createContextChain(final ConnectionContext connectionContext) {
131         final DeviceInfo deviceInfo = connectionContext.getDeviceInfo();
132         final DeviceContext deviceContext = deviceManager.createContext(connectionContext);
133         deviceContext.registerMastershipWatcher(this);
134         LOG.debug("Device" + CONTEXT_CREATED_FOR_CONNECTION, deviceInfo);
135
136         final RpcContext rpcContext = rpcManager.createContext(deviceContext);
137         rpcContext.registerMastershipWatcher(this);
138         LOG.debug("RPC" + CONTEXT_CREATED_FOR_CONNECTION, deviceInfo);
139
140         final StatisticsContext statisticsContext = statisticsManager
141                 .createContext(deviceContext, ownershipChangeListener.isReconciliationFrameworkRegistered());
142         statisticsContext.registerMastershipWatcher(this);
143         LOG.debug("Statistics" + CONTEXT_CREATED_FOR_CONNECTION, deviceInfo);
144
145         final RoleContext roleContext = roleManager.createContext(deviceContext);
146         roleContext.registerMastershipWatcher(this);
147         LOG.debug("Role" + CONTEXT_CREATED_FOR_CONNECTION, deviceInfo);
148
149         final ContextChain contextChain = new ContextChainImpl(this, connectionContext, executor);
150         contextChain.registerDeviceRemovedHandler(deviceManager);
151         contextChain.registerDeviceRemovedHandler(rpcManager);
152         contextChain.registerDeviceRemovedHandler(statisticsManager);
153         contextChain.registerDeviceRemovedHandler(roleManager);
154         contextChain.registerDeviceRemovedHandler(this);
155         contextChain.addContext(deviceContext);
156         contextChain.addContext(rpcContext);
157         contextChain.addContext(statisticsContext);
158         contextChain.addContext(roleContext);
159         contextChainMap.put(deviceInfo, contextChain);
160         connectingDevices.remove(deviceInfo);
161         LOG.debug("Context chain" + CONTEXT_CREATED_FOR_CONNECTION, deviceInfo);
162
163         deviceContext.onPublished();
164         contextChain.registerServices(singletonServiceProvider);
165     }
166
167     @Override
168     public ConnectionStatus deviceConnected(final ConnectionContext connectionContext) {
169         final DeviceInfo deviceInfo = connectionContext.getDeviceInfo();
170         final ContextChain contextChain = contextChainMap.get(deviceInfo);
171         final FeaturesReply featuresReply = connectionContext.getFeatures();
172         final Uint8 auxiliaryId = featuresReply != null ? featuresReply.getAuxiliaryId() : null;
173
174         if (auxiliaryId != null && auxiliaryId.toJava() != 0) {
175             if (contextChain == null) {
176                 LOG.warn("An auxiliary connection for device {}, but no primary connection. Refusing connection.",
177                          deviceInfo);
178                 return ConnectionStatus.REFUSING_AUXILIARY_CONNECTION;
179             } else if (contextChain.addAuxiliaryConnection(connectionContext)) {
180                 LOG.info("An auxiliary connection was added to device: {}", deviceInfo);
181                 return ConnectionStatus.MAY_CONTINUE;
182             } else {
183                 LOG.warn("Not able to add auxiliary connection to the device {}", deviceInfo);
184                 return ConnectionStatus.REFUSING_AUXILIARY_CONNECTION;
185             }
186         } else {
187             LOG.info("Device {} connected.", deviceInfo);
188             final boolean contextExists = contextChain != null;
189             final boolean isClosing = contextExists && contextChain.isClosing();
190
191             if (!isClosing && connectingDevices.putIfAbsent(deviceInfo, connectionContext) != null) {
192                 LOG.warn("Device {} is already trying to connect, wait until succeeded or disconnected.", deviceInfo);
193                 return ConnectionStatus.ALREADY_CONNECTED;
194             }
195
196             if (contextExists) {
197                 if (isClosing) {
198                     LOG.warn("Device {} is already in termination state, closing all incoming connections.",
199                              deviceInfo);
200                     return ConnectionStatus.CLOSING;
201                 }
202
203                 LOG.warn("Device {} already connected. Closing previous connection", deviceInfo);
204                 destroyContextChain(deviceInfo);
205                 LOG.info("Old connection dropped, creating new context chain for device {}", deviceInfo);
206                 createContextChain(connectionContext);
207             } else {
208                 LOG.info("No context chain found for device: {}, creating new.", deviceInfo);
209                 createContextChain(connectionContext);
210             }
211
212             return ConnectionStatus.MAY_CONTINUE;
213         }
214
215     }
216
217     @Override
218     public void onNotAbleToStartMastership(final DeviceInfo deviceInfo, final String reason, final boolean mandatory) {
219         LOG.error("Not able to set MASTER role on device {}, reason: {}", deviceInfo, reason);
220
221         if (!mandatory) {
222             return;
223         }
224         if (contextChainMap.containsKey(deviceInfo)) {
225             LOG.warn("This mastering is mandatory, destroying context chain and closing connection for device {}.",
226                      deviceInfo);
227             destroyContextChain(deviceInfo);
228         }
229     }
230
231     @Override
232     public void onMasterRoleAcquired(final DeviceInfo deviceInfo, final ContextChainMastershipState mastershipState) {
233         final ContextChain contextChain = contextChainMap.get(deviceInfo);
234         if (contextChain != null) {
235             if (!ContextChainMastershipState.INITIAL_SUBMIT.equals(mastershipState)) {
236                 if (contextChain.isMastered(mastershipState, true)) {
237                     Futures.addCallback(ownershipChangeListener.becomeMasterBeforeSubmittedDS(deviceInfo),
238                                         reconciliationFrameworkCallback(deviceInfo, contextChain, mastershipState),
239                                         MoreExecutors.directExecutor());
240                 }
241             } else if (contextChain.isMastered(mastershipState, false)) {
242                 LOG.info("Role MASTER was granted to device {}", deviceInfo);
243                 OF_EVENT_LOG.debug("Master Elected, Node: {}", deviceInfo.getDatapathId());
244                 deviceManager.sendNodeAddedNotification(deviceInfo.getNodeInstanceIdentifier());
245             }
246         }
247     }
248
249     @Override
250     public void onSlaveRoleAcquired(final DeviceInfo deviceInfo) {
251         ownershipChangeListener.becomeSlaveOrDisconnect(deviceInfo);
252         LOG.info("Role SLAVE was granted to device {}", deviceInfo);
253         final ContextChain contextChain = contextChainMap.get(deviceInfo);
254         if (contextChain != null) {
255             contextChain.makeContextChainStateSlave();
256         }
257     }
258
259     @Override
260     public void onSlaveRoleNotAcquired(final DeviceInfo deviceInfo, final String reason) {
261         LOG.error("Not able to set SLAVE role on device {}, reason: {}", deviceInfo, reason);
262         if (contextChainMap.containsKey(deviceInfo)) {
263             destroyContextChain(deviceInfo);
264         }
265     }
266
267     @Override
268     public void onDeviceDisconnected(final ConnectionContext connectionContext) {
269         final DeviceInfo deviceInfo = connectionContext.getDeviceInfo();
270         if (deviceInfo != null) {
271             final ContextChain contextChain = contextChainMap.get(deviceInfo);
272             if (contextChain != null) {
273                 if (contextChain.auxiliaryConnectionDropped(connectionContext)) {
274                     LOG.info("Auxiliary connection from device {} disconnected.", deviceInfo);
275                 } else {
276                     LOG.info("Device {} disconnected.", deviceInfo);
277                     destroyContextChain(deviceInfo);
278                 }
279             }
280         }
281     }
282
283     @VisibleForTesting
284     boolean checkAllManagers() {
285         return deviceManager != null && rpcManager != null && statisticsManager != null && roleManager != null;
286     }
287
288     @Override
289     public ContextChain getContextChain(final DeviceInfo deviceInfo) {
290         return contextChainMap.get(deviceInfo);
291     }
292
293     @Override
294     public void close() {
295         Map<DeviceInfo, ContextChain> copyOfChains = new HashMap<>(contextChainMap);
296         copyOfChains.keySet().forEach(this::destroyContextChain);
297         copyOfChains.clear();
298         contextChainMap.clear();
299         OF_EVENT_LOG.debug("EOS registration closed for all devices");
300         eosListenerRegistration.close();
301         OF_EVENT_LOG.debug("EOS registration closed for all devices");
302         nodeCleanerExecutor.shutdownNow();
303     }
304
305     @Override
306     @SuppressFBWarnings("BC_UNCONFIRMED_CAST_OF_RETURN_VALUE")
307     public void ownershipChanged(final EntityOwnershipChange entityOwnershipChange) {
308         LOG.info("Entity ownership change received for node : {}", entityOwnershipChange);
309         if (entityOwnershipChange.inJeopardy()) {
310             LOG.warn("Controller is in Jeopardy, ignore ownership change notification. {}", entityOwnershipChange);
311             return;
312         }
313         if (entityOwnershipChange.getState().hasOwner()) {
314             return;
315         }
316
317         // Findbugs flags a false violation for "Unchecked/unconfirmed cast" from GenericEntity to Entity hence the
318         // suppression above. The suppression is temporary until EntityOwnershipChange is modified to eliminate the
319         // violation.
320         final String entityName = entityOwnershipChange
321                 .getEntity()
322                 .getIdentifier()
323                 .firstKeyOf(Entity.class)
324                 .getName();
325
326         if (entityName != null && entityName.startsWith("openflow:")) {
327             if (nodeCleanerExecutor.isShutdown()) {
328                 LOG.warn("Node cleaner executor thread-pool is down.");
329                 return;
330             }
331             LOG.debug("Device {} will be removed from datastore in {} msec, if it's not transient notification.",
332                     entityName, config.getDeviceDatastoreRemovalDelay().getValue());
333             final String dpnId = getDpnIdFromNodeName(entityName);
334             nodeCleanerExecutor.schedule(() -> {
335                 try {
336                     EntityOwnershipState ownershipState = getCurrentOwnershipStatus(entityName);
337                     if (ownershipState == null || EntityOwnershipState.NO_OWNER.equals(ownershipState)) {
338                         LOG.debug("Entity {} has no owner", entityName);
339                         final KeyedInstanceIdentifier<Node, NodeKey> nodeInstanceIdentifier =
340                                 DeviceStateUtil.createNodeInstanceIdentifier(new NodeId(entityName));
341                         deviceManager.sendNodeRemovedNotification(nodeInstanceIdentifier);
342                         LOG.info("Try to remove device {} from operational DS", entityName);
343                         ListenableFuture<?> future =
344                                 deviceManager.removeDeviceFromOperationalDS(nodeInstanceIdentifier);
345                         Futures.addCallback(future, new FutureCallback<Object>() {
346                             @Override
347                             public void onSuccess(final Object result) {
348                                 LOG.debug("Node removed from Oper DS, Node: {}", dpnId);
349                                 OF_EVENT_LOG.debug("Node removed from Oper DS, Node: {}", dpnId);
350                             }
351
352                             @Override
353                             public void onFailure(final Throwable throwable) {
354                                 LOG.error("Could not remove device {} from operational DS", dpnId, throwable);
355                             }
356                         }, MoreExecutors.directExecutor());
357                         future.get(REMOVE_DEVICE_FROM_DS_TIMEOUT, TimeUnit.MILLISECONDS);
358                     } else {
359                         LOG.warn("Seems like device is still owned by other controller instance. Skip deleting {} "
360                                 + "node from operational datastore.", entityName);
361                     }
362                 } catch (TimeoutException | ExecutionException | InterruptedException e) {
363                     LOG.warn("Not able to remove device {} from operational DS. ", entityName, e);
364                 }
365             }, config.getDeviceDatastoreRemovalDelay().getValue().toJava(), TimeUnit.MILLISECONDS);
366         }
367     }
368
369     private void destroyContextChain(final DeviceInfo deviceInfo) {
370         OF_EVENT_LOG.debug("Destroying context chain for device {}", deviceInfo.getDatapathId());
371         ownershipChangeListener.becomeSlaveOrDisconnect(deviceInfo);
372         final ContextChain contextChain = contextChainMap.get(deviceInfo);
373         if (contextChain != null) {
374             deviceManager.sendNodeRemovedNotification(deviceInfo.getNodeInstanceIdentifier());
375             contextChain.close();
376             connectingDevices.remove(deviceInfo);
377         }
378     }
379
380     @Override
381     public List<DeviceInfo> listOfMasteredDevices() {
382         return contextChainMap.entrySet().stream()
383                 .filter(deviceInfoContextChainEntry -> deviceInfoContextChainEntry.getValue()
384                         .isMastered(ContextChainMastershipState.CHECK, false)).map(Map.Entry::getKey)
385                 .collect(Collectors.toList());
386     }
387
388     @Override
389     public boolean isAnyDeviceMastered() {
390         return contextChainMap.entrySet().stream().findAny()
391                 .filter(deviceInfoContextChainEntry -> deviceInfoContextChainEntry.getValue()
392                         .isMastered(ContextChainMastershipState.CHECK, false)).isPresent();
393     }
394
395     @Override
396     public void onDeviceRemoved(final DeviceInfo deviceInfo) {
397         contextChainMap.remove(deviceInfo);
398         LOG.debug("Context chain removed for node {}", deviceInfo);
399     }
400
401     private FutureCallback<ResultState> reconciliationFrameworkCallback(@NonNull final DeviceInfo deviceInfo,
402             final ContextChain contextChain, final ContextChainMastershipState mastershipState) {
403         return new FutureCallback<>() {
404             @Override
405             public void onSuccess(final ResultState result) {
406                 if (ResultState.DONOTHING == result) {
407                     OF_EVENT_LOG.debug("Device {} connection is enabled by reconciliation framework", deviceInfo);
408                     LOG.info("Device {} connection is enabled by reconciliation framework.", deviceInfo);
409                     if (mastershipState == ContextChainMastershipState.MASTER_ON_DEVICE) {
410                         ownershipChangeListener.becomeMaster(deviceInfo);
411                         contextChain.initializeDevice();
412                     }
413                     contextChain.continueInitializationAfterReconciliation();
414                 } else {
415                     OF_EVENT_LOG.debug("Reconciliation framework failure for device {}", deviceInfo);
416                     LOG.warn("Reconciliation framework failure for device {} with resultState {}", deviceInfo, result);
417                     destroyContextChain(deviceInfo);
418                 }
419             }
420
421             @Override
422             public void onFailure(final Throwable throwable) {
423                 OF_EVENT_LOG.debug("Reconciliation framework failure for device {} with error {}", deviceInfo,
424                         throwable.getMessage());
425                 LOG.warn("Reconciliation framework failure for device {}", deviceInfo, throwable);
426                 destroyContextChain(deviceInfo);
427             }
428         };
429     }
430
431     private static String getDpnIdFromNodeName(final String nodeName) {
432         return nodeName.substring(nodeName.lastIndexOf(SEPARATOR) + 1);
433     }
434
435     private @Nullable EntityOwnershipState getCurrentOwnershipStatus(final String nodeId) {
436         org.opendaylight.mdsal.eos.binding.api.Entity entity = createNodeEntity(nodeId);
437         Optional<EntityOwnershipState> optOwnershipStatus
438                 = entityOwnershipService.getOwnershipState(entity);
439
440         if (optOwnershipStatus.isPresent()) {
441             final var ownershipStatus = optOwnershipStatus.orElseThrow();
442             LOG.debug("Current ownership status for node {} is {}", nodeId, ownershipStatus);
443             return ownershipStatus;
444         }
445
446         LOG.trace("Ownership status is not available for node {}", nodeId);
447         return null;
448     }
449
450     private static org.opendaylight.mdsal.eos.binding.api.Entity createNodeEntity(final String nodeId) {
451         return new org.opendaylight.mdsal.eos.binding.api.Entity(ASYNC_SERVICE_ENTITY_TYPE, nodeId);
452     }
453 }