Remove redundant exception declarations
[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.MoreExecutors;
14 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
15 import java.util.HashMap;
16 import java.util.List;
17 import java.util.Map;
18 import java.util.Objects;
19 import java.util.Optional;
20 import java.util.concurrent.ConcurrentHashMap;
21 import java.util.concurrent.ExecutionException;
22 import java.util.concurrent.ExecutorService;
23 import java.util.concurrent.TimeUnit;
24 import java.util.concurrent.TimeoutException;
25 import java.util.stream.Collectors;
26 import javax.annotation.Nonnull;
27 import javax.annotation.Nullable;
28 import org.opendaylight.mdsal.eos.binding.api.EntityOwnershipChange;
29 import org.opendaylight.mdsal.eos.binding.api.EntityOwnershipListenerRegistration;
30 import org.opendaylight.mdsal.eos.binding.api.EntityOwnershipService;
31 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceProvider;
32 import org.opendaylight.openflowplugin.api.openflow.OFPManager;
33 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
34 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionStatus;
35 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
36 import org.opendaylight.openflowplugin.api.openflow.device.DeviceInfo;
37 import org.opendaylight.openflowplugin.api.openflow.device.DeviceManager;
38 import org.opendaylight.openflowplugin.api.openflow.lifecycle.ContextChain;
39 import org.opendaylight.openflowplugin.api.openflow.lifecycle.ContextChainHolder;
40 import org.opendaylight.openflowplugin.api.openflow.lifecycle.ContextChainMastershipState;
41 import org.opendaylight.openflowplugin.api.openflow.lifecycle.MasterChecker;
42 import org.opendaylight.openflowplugin.api.openflow.lifecycle.OwnershipChangeListener;
43 import org.opendaylight.openflowplugin.api.openflow.role.RoleContext;
44 import org.opendaylight.openflowplugin.api.openflow.role.RoleManager;
45 import org.opendaylight.openflowplugin.api.openflow.rpc.RpcContext;
46 import org.opendaylight.openflowplugin.api.openflow.rpc.RpcManager;
47 import org.opendaylight.openflowplugin.api.openflow.statistics.StatisticsContext;
48 import org.opendaylight.openflowplugin.api.openflow.statistics.StatisticsManager;
49 import org.opendaylight.openflowplugin.impl.util.DeviceStateUtil;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FeaturesReply;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.core.general.entity.rev150930.Entity;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflowplugin.rf.state.rev170713.ResultState;
56 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
57 import org.slf4j.Logger;
58 import org.slf4j.LoggerFactory;
59
60 public class ContextChainHolderImpl implements ContextChainHolder, MasterChecker {
61     private static final Logger LOG = LoggerFactory.getLogger(ContextChainHolderImpl.class);
62
63     private static final String CONTEXT_CREATED_FOR_CONNECTION = " context created for connection: {}";
64     private static final long REMOVE_DEVICE_FROM_DS_TIMEOUT = 5000L;
65     private static final String ASYNC_SERVICE_ENTITY_TYPE = "org.opendaylight.mdsal.AsyncServiceCloseEntityType";
66
67     private final Map<DeviceInfo, ContextChain> contextChainMap = new ConcurrentHashMap<>();
68     private final Map<DeviceInfo, ? super ConnectionContext> connectingDevices = new ConcurrentHashMap<>();
69     private final EntityOwnershipListenerRegistration eosListenerRegistration;
70     private final ClusterSingletonServiceProvider singletonServiceProvider;
71     private final ExecutorService executorService;
72     private final OwnershipChangeListener ownershipChangeListener;
73     private DeviceManager deviceManager;
74     private RpcManager rpcManager;
75     private StatisticsManager statisticsManager;
76     private RoleManager roleManager;
77
78     public ContextChainHolderImpl(final ExecutorService executorService,
79                                   final ClusterSingletonServiceProvider singletonServiceProvider,
80                                   final EntityOwnershipService entityOwnershipService,
81                                   final OwnershipChangeListener ownershipChangeListener) {
82         this.singletonServiceProvider = singletonServiceProvider;
83         this.executorService = executorService;
84         this.ownershipChangeListener = ownershipChangeListener;
85         this.ownershipChangeListener.setMasterChecker(this);
86         this.eosListenerRegistration = Objects
87                 .requireNonNull(entityOwnershipService.registerListener(ASYNC_SERVICE_ENTITY_TYPE, this));
88     }
89
90     @Override
91     public <T extends OFPManager> void addManager(final T manager) {
92         if (Objects.isNull(deviceManager) && manager instanceof DeviceManager) {
93             LOG.trace("Context chain holder: Device manager OK.");
94             deviceManager = (DeviceManager) manager;
95         } else if (Objects.isNull(rpcManager) && manager instanceof RpcManager) {
96             LOG.trace("Context chain holder: RPC manager OK.");
97             rpcManager = (RpcManager) manager;
98         } else if (Objects.isNull(statisticsManager) && manager instanceof StatisticsManager) {
99             LOG.trace("Context chain holder: Statistics manager OK.");
100             statisticsManager = (StatisticsManager) manager;
101         } else if (Objects.isNull(roleManager) && manager instanceof RoleManager) {
102             LOG.trace("Context chain holder: Role manager OK.");
103             roleManager = (RoleManager) manager;
104         }
105     }
106
107     @VisibleForTesting
108     void createContextChain(final ConnectionContext connectionContext) {
109         final DeviceInfo deviceInfo = connectionContext.getDeviceInfo();
110
111         final DeviceContext deviceContext = deviceManager.createContext(connectionContext);
112         deviceContext.registerMastershipWatcher(this);
113         LOG.debug("Device" + CONTEXT_CREATED_FOR_CONNECTION, deviceInfo);
114
115         final RpcContext rpcContext = rpcManager.createContext(deviceContext);
116         rpcContext.registerMastershipWatcher(this);
117         LOG.debug("RPC" + CONTEXT_CREATED_FOR_CONNECTION, deviceInfo);
118
119         final StatisticsContext statisticsContext = statisticsManager
120                 .createContext(deviceContext, ownershipChangeListener.isReconciliationFrameworkRegistered());
121         statisticsContext.registerMastershipWatcher(this);
122         LOG.debug("Statistics" + CONTEXT_CREATED_FOR_CONNECTION, deviceInfo);
123
124         final RoleContext roleContext = roleManager.createContext(deviceContext);
125         roleContext.registerMastershipWatcher(this);
126         LOG.debug("Role" + CONTEXT_CREATED_FOR_CONNECTION, deviceInfo);
127
128         final ContextChain contextChain = new ContextChainImpl(this, connectionContext, executorService);
129         contextChain.registerDeviceRemovedHandler(deviceManager);
130         contextChain.registerDeviceRemovedHandler(rpcManager);
131         contextChain.registerDeviceRemovedHandler(statisticsManager);
132         contextChain.registerDeviceRemovedHandler(roleManager);
133         contextChain.registerDeviceRemovedHandler(this);
134         contextChain.addContext(deviceContext);
135         contextChain.addContext(rpcContext);
136         contextChain.addContext(statisticsContext);
137         contextChain.addContext(roleContext);
138         contextChainMap.put(deviceInfo, contextChain);
139         connectingDevices.remove(deviceInfo);
140         LOG.debug("Context chain" + CONTEXT_CREATED_FOR_CONNECTION, deviceInfo);
141
142         deviceContext.onPublished();
143         contextChain.registerServices(singletonServiceProvider);
144     }
145
146     @Override
147     public ConnectionStatus deviceConnected(final ConnectionContext connectionContext) {
148         final DeviceInfo deviceInfo = connectionContext.getDeviceInfo();
149         final ContextChain contextChain = contextChainMap.get(deviceInfo);
150         final FeaturesReply featuresReply = connectionContext.getFeatures();
151         final Short auxiliaryId = featuresReply != null ? featuresReply.getAuxiliaryId() : null;
152
153         if (auxiliaryId != null && auxiliaryId != 0) {
154             if (contextChain == null) {
155                 LOG.warn("An auxiliary connection for device {}, but no primary connection. Refusing connection.",
156                          deviceInfo);
157                 return ConnectionStatus.REFUSING_AUXILIARY_CONNECTION;
158             } else {
159                 if (contextChain.addAuxiliaryConnection(connectionContext)) {
160                     LOG.info("An auxiliary connection was added to device: {}", deviceInfo);
161                     return ConnectionStatus.MAY_CONTINUE;
162                 } else {
163                     LOG.warn("Not able to add auxiliary connection to the device {}", deviceInfo);
164                     return ConnectionStatus.REFUSING_AUXILIARY_CONNECTION;
165                 }
166             }
167         } else {
168             LOG.info("Device {} connected.", deviceInfo);
169             final boolean contextExists = contextChain != null;
170             final boolean isClosing = contextExists && contextChain.isClosing();
171
172             if (!isClosing && connectingDevices.putIfAbsent(deviceInfo, connectionContext) != null) {
173                 LOG.warn("Device {} is already trying to connect, wait until succeeded or disconnected.", deviceInfo);
174                 return ConnectionStatus.ALREADY_CONNECTED;
175             }
176
177             if (contextExists) {
178                 if (isClosing) {
179                     LOG.warn("Device {} is already in termination state, closing all incoming connections.",
180                              deviceInfo);
181                     return ConnectionStatus.CLOSING;
182                 }
183
184                 LOG.warn("Device {} already connected. Closing previous connection", deviceInfo);
185                 destroyContextChain(deviceInfo);
186                 LOG.info("Old connection dropped, creating new context chain for device {}", deviceInfo);
187                 createContextChain(connectionContext);
188             } else {
189                 LOG.info("No context chain found for device: {}, creating new.", deviceInfo);
190                 createContextChain(connectionContext);
191             }
192
193             return ConnectionStatus.MAY_CONTINUE;
194         }
195
196     }
197
198     @Override
199     public void onNotAbleToStartMastership(@Nonnull final DeviceInfo deviceInfo, @Nonnull final String reason,
200                                            final boolean mandatory) {
201         LOG.warn("Not able to set MASTER role on device {}, reason: {}", deviceInfo, reason);
202
203         if (!mandatory) {
204             return;
205         }
206
207         Optional.ofNullable(contextChainMap.get(deviceInfo)).ifPresent(contextChain -> {
208             LOG.warn("This mastering is mandatory, destroying context chain and closing connection for device {}.",
209                      deviceInfo);
210             destroyContextChain(deviceInfo);
211         });
212     }
213
214     @Override
215     public void onMasterRoleAcquired(@Nonnull final DeviceInfo deviceInfo,
216                                      @Nonnull final ContextChainMastershipState mastershipState) {
217         Optional.ofNullable(contextChainMap.get(deviceInfo)).ifPresent(contextChain -> {
218             if (ownershipChangeListener.isReconciliationFrameworkRegistered()
219                     && !ContextChainMastershipState.INITIAL_SUBMIT.equals(mastershipState)) {
220                 if (contextChain.isMastered(mastershipState, true)) {
221                     Futures.addCallback(ownershipChangeListener.becomeMasterBeforeSubmittedDS(deviceInfo),
222                                         reconciliationFrameworkCallback(deviceInfo, contextChain),
223                                         MoreExecutors.directExecutor());
224                 }
225             } else if (contextChain.isMastered(mastershipState, false)) {
226                 LOG.info("Role MASTER was granted to device {}", deviceInfo);
227                 ownershipChangeListener.becomeMaster(deviceInfo);
228                 deviceManager.sendNodeAddedNotification(deviceInfo.getNodeInstanceIdentifier());
229             }
230         });
231     }
232
233     @Override
234     public void onSlaveRoleAcquired(final DeviceInfo deviceInfo) {
235         ownershipChangeListener.becomeSlaveOrDisconnect(deviceInfo);
236         LOG.info("Role SLAVE was granted to device {}", deviceInfo);
237         Optional.ofNullable(contextChainMap.get(deviceInfo)).ifPresent(ContextChain::makeContextChainStateSlave);
238     }
239
240     @Override
241     public void onSlaveRoleNotAcquired(final DeviceInfo deviceInfo, final String reason) {
242         LOG.warn("Not able to set SLAVE role on device {}, reason: {}", deviceInfo, reason);
243         Optional.ofNullable(contextChainMap.get(deviceInfo)).ifPresent(contextChain -> destroyContextChain(deviceInfo));
244     }
245
246     @Override
247     public void onDeviceDisconnected(final ConnectionContext connectionContext) {
248         final DeviceInfo deviceInfo = connectionContext.getDeviceInfo();
249
250         Optional.ofNullable(connectionContext.getDeviceInfo()).map(contextChainMap::get).ifPresent(contextChain -> {
251             if (contextChain.auxiliaryConnectionDropped(connectionContext)) {
252                 LOG.info("Auxiliary connection from device {} disconnected.", deviceInfo);
253             } else {
254                 LOG.info("Device {} disconnected.", deviceInfo);
255                 destroyContextChain(deviceInfo);
256             }
257         });
258     }
259
260     @VisibleForTesting
261     boolean checkAllManagers() {
262         return Objects.nonNull(deviceManager) && Objects.nonNull(rpcManager) && Objects.nonNull(statisticsManager)
263                 && Objects.nonNull(roleManager);
264     }
265
266     @Override
267     public ContextChain getContextChain(final DeviceInfo deviceInfo) {
268         return contextChainMap.get(deviceInfo);
269     }
270
271     @Override
272     public void close() {
273         Map<DeviceInfo, ContextChain> copyOfChains = new HashMap<>(contextChainMap);
274         copyOfChains.keySet().forEach(this::destroyContextChain);
275         copyOfChains.clear();
276         contextChainMap.clear();
277         eosListenerRegistration.close();
278     }
279
280     @Override
281     @SuppressFBWarnings("BC_UNCONFIRMED_CAST_OF_RETURN_VALUE")
282     public void ownershipChanged(EntityOwnershipChange entityOwnershipChange) {
283         if (entityOwnershipChange.getState().hasOwner()) {
284             return;
285         }
286
287         // Findbugs flags a false violation for "Unchecked/unconfirmed cast" from GenericEntity to Entity hence the
288         // suppression above. The suppression is temporary until EntityOwnershipChange is modified to eliminate the
289         // violation.
290         final String entityName = entityOwnershipChange
291                 .getEntity()
292                 .getIdentifier()
293                 .firstKeyOf(Entity.class)
294                 .getName();
295
296         if (Objects.nonNull(entityName)) {
297             LOG.debug("Entity {} has no owner", entityName);
298             try {
299                 //TODO:Remove notifications
300                 final KeyedInstanceIdentifier<Node, NodeKey> nodeInstanceIdentifier =
301                         DeviceStateUtil.createNodeInstanceIdentifier(new NodeId(entityName));
302                 deviceManager.sendNodeRemovedNotification(nodeInstanceIdentifier);
303
304                 LOG.info("Try to remove device {} from operational DS", entityName);
305                 deviceManager.removeDeviceFromOperationalDS(nodeInstanceIdentifier)
306                         .get(REMOVE_DEVICE_FROM_DS_TIMEOUT, TimeUnit.MILLISECONDS);
307                 LOG.info("Removing device from operational DS {} was successful", entityName);
308             } catch (TimeoutException | ExecutionException | NullPointerException | InterruptedException e) {
309                 LOG.warn("Not able to remove device {} from operational DS. ", entityName, e);
310             }
311         }
312     }
313
314     private void destroyContextChain(final DeviceInfo deviceInfo) {
315         ownershipChangeListener.becomeSlaveOrDisconnect(deviceInfo);
316         Optional.ofNullable(contextChainMap.get(deviceInfo)).ifPresent(contextChain -> {
317             deviceManager.sendNodeRemovedNotification(deviceInfo.getNodeInstanceIdentifier());
318             contextChain.close();
319             connectingDevices.remove(deviceInfo);
320         });
321     }
322
323     @Override
324     public List<DeviceInfo> listOfMasteredDevices() {
325         return contextChainMap.entrySet().stream()
326                 .filter(deviceInfoContextChainEntry -> deviceInfoContextChainEntry.getValue()
327                         .isMastered(ContextChainMastershipState.CHECK, false)).map(Map.Entry::getKey)
328                 .collect(Collectors.toList());
329     }
330
331     @Override
332     public boolean isAnyDeviceMastered() {
333         return contextChainMap.entrySet().stream().findAny()
334                 .filter(deviceInfoContextChainEntry -> deviceInfoContextChainEntry.getValue()
335                         .isMastered(ContextChainMastershipState.CHECK, false)).isPresent();
336     }
337
338     @Override
339     public void onDeviceRemoved(final DeviceInfo deviceInfo) {
340         contextChainMap.remove(deviceInfo);
341         LOG.debug("Context chain removed for node {}", deviceInfo);
342     }
343
344     private FutureCallback<ResultState> reconciliationFrameworkCallback(@Nonnull DeviceInfo deviceInfo,
345                                                                         ContextChain contextChain) {
346         return new FutureCallback<ResultState>() {
347             @Override
348             public void onSuccess(@Nullable ResultState result) {
349                 if (ResultState.DONOTHING == result) {
350                     LOG.info("Device {} connection is enabled by reconciliation framework.", deviceInfo);
351                     contextChain.continueInitializationAfterReconciliation();
352                 } else {
353                     LOG.warn("Reconciliation framework failure for device {}", deviceInfo);
354                     destroyContextChain(deviceInfo);
355                 }
356             }
357
358             @Override
359             public void onFailure(@Nonnull Throwable throwable) {
360                 LOG.warn("Reconciliation framework failure.");
361                 destroyContextChain(deviceInfo);
362             }
363         };
364     }
365 }