Bug 5596 Created lifecycle service
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / device / DeviceManagerImpl.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.device;
9
10 import com.google.common.annotations.VisibleForTesting;
11 import com.google.common.base.Function;
12 import com.google.common.base.Preconditions;
13 import com.google.common.base.Verify;
14 import com.google.common.collect.Iterators;
15 import com.google.common.util.concurrent.AsyncFunction;
16 import com.google.common.util.concurrent.FutureCallback;
17 import com.google.common.util.concurrent.Futures;
18 import com.google.common.util.concurrent.ListenableFuture;
19 import io.netty.util.TimerTask;
20 import java.util.Collections;
21 import java.util.HashSet;
22 import java.util.Iterator;
23 import java.util.Set;
24 import java.util.concurrent.ConcurrentHashMap;
25 import java.util.concurrent.ConcurrentMap;
26 import java.util.concurrent.ExecutionException;
27 import java.util.concurrent.ScheduledThreadPoolExecutor;
28 import java.util.concurrent.TimeUnit;
29 import javax.annotation.CheckForNull;
30 import javax.annotation.Nonnull;
31 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
32 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
33 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
34 import org.opendaylight.openflowjava.protocol.api.connection.ConnectionAdapter;
35 import org.opendaylight.openflowjava.protocol.api.connection.OutboundQueueHandlerRegistration;
36 import org.opendaylight.openflowplugin.api.openflow.OFPContext;
37 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
38 import org.opendaylight.openflowplugin.api.openflow.connection.OutboundQueueProvider;
39 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
40 import org.opendaylight.openflowplugin.api.openflow.device.DeviceInfo;
41 import org.opendaylight.openflowplugin.api.openflow.device.DeviceManager;
42 import org.opendaylight.openflowplugin.api.openflow.device.DeviceState;
43 import org.opendaylight.openflowplugin.api.openflow.device.DeviceSynchronizeListener;
44 import org.opendaylight.openflowplugin.api.openflow.device.DeviceValidListener;
45 import org.opendaylight.openflowplugin.api.openflow.device.TranslatorLibrary;
46 import org.opendaylight.openflowplugin.api.openflow.device.handlers.DeviceInitializationPhaseHandler;
47 import org.opendaylight.openflowplugin.api.openflow.device.handlers.DeviceTerminationPhaseHandler;
48 import org.opendaylight.openflowplugin.api.openflow.lifecycle.LifecycleConductor;
49 import org.opendaylight.openflowplugin.api.openflow.statistics.StatisticsContext;
50 import org.opendaylight.openflowplugin.extension.api.ExtensionConverterProviderKeeper;
51 import org.opendaylight.openflowplugin.extension.api.core.extension.ExtensionConverterProvider;
52 import org.opendaylight.openflowplugin.impl.connection.OutboundQueueProviderImpl;
53 import org.opendaylight.openflowplugin.impl.device.listener.OpenflowProtocolListenerFullImpl;
54 import org.opendaylight.openflowplugin.impl.util.DeviceInitializationUtils;
55 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.ConvertorExecutor;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodesBuilder;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.role.service.rev150727.OfpRole;
60 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
61 import org.slf4j.Logger;
62 import org.slf4j.LoggerFactory;
63
64 /**
65  *
66  */
67 public class DeviceManagerImpl implements DeviceManager, ExtensionConverterProviderKeeper {
68
69     private static final Logger LOG = LoggerFactory.getLogger(DeviceManagerImpl.class);
70
71     private final long globalNotificationQuota;
72     private final boolean switchFeaturesMandatory;
73     private boolean isNotificationFlowRemovedOff;
74
75     private static final int SPY_RATE = 10;
76
77     private final DataBroker dataBroker;
78     private final ConvertorExecutor convertorExecutor;
79     private TranslatorLibrary translatorLibrary;
80     private DeviceInitializationPhaseHandler deviceInitPhaseHandler;
81     private DeviceTerminationPhaseHandler deviceTerminPhaseHandler;
82
83     private final ConcurrentMap<DeviceInfo, DeviceContext> deviceContexts = new ConcurrentHashMap<>();
84
85     private final long barrierIntervalNanos;
86     private final int barrierCountLimit;
87     private ExtensionConverterProvider extensionConverterProvider;
88     private ScheduledThreadPoolExecutor spyPool;
89     private Set<DeviceSynchronizeListener> deviceSynchronizedListeners;
90     private Set<DeviceValidListener> deviceValidListeners;
91
92     private final LifecycleConductor conductor;
93
94     public DeviceManagerImpl(@Nonnull final DataBroker dataBroker,
95                              final long globalNotificationQuota, final boolean switchFeaturesMandatory,
96                              final long barrierInterval, final int barrierCountLimit,
97                              final LifecycleConductor lifecycleConductor, boolean isNotificationFlowRemovedOff,
98                              final ConvertorExecutor convertorExecutor) {
99         this.switchFeaturesMandatory = switchFeaturesMandatory;
100         this.globalNotificationQuota = globalNotificationQuota;
101         this.isNotificationFlowRemovedOff = isNotificationFlowRemovedOff;
102         this.dataBroker = Preconditions.checkNotNull(dataBroker);
103         this.convertorExecutor = convertorExecutor;
104         /* merge empty nodes to oper DS to predict any problems with missing parent for Node */
105         final WriteTransaction tx = dataBroker.newWriteOnlyTransaction();
106
107         final NodesBuilder nodesBuilder = new NodesBuilder();
108         nodesBuilder.setNode(Collections.<Node>emptyList());
109         tx.merge(LogicalDatastoreType.OPERATIONAL, InstanceIdentifier.create(Nodes.class), nodesBuilder.build());
110         try {
111             tx.submit().get();
112         } catch (ExecutionException | InterruptedException e) {
113             LOG.error("Creation of node failed.", e);
114             throw new IllegalStateException(e);
115         }
116
117         this.barrierIntervalNanos = TimeUnit.MILLISECONDS.toNanos(barrierInterval);
118         this.barrierCountLimit = barrierCountLimit;
119
120         this.conductor = lifecycleConductor;
121         spyPool = new ScheduledThreadPoolExecutor(1);
122         this.deviceSynchronizedListeners = new HashSet<>();
123         this.deviceValidListeners = new HashSet<>();
124     }
125
126
127     @Override
128     public void setDeviceInitializationPhaseHandler(final DeviceInitializationPhaseHandler handler) {
129         this.deviceInitPhaseHandler = handler;
130     }
131
132     @Override
133     public void onDeviceContextLevelUp(@CheckForNull DeviceInfo deviceInfo) throws Exception {
134         // final phase - we have to add new Device to MD-SAL DataStore
135         LOG.debug("Final phase of DeviceContextLevelUp for Node: {} ", deviceInfo.getNodeId());
136         DeviceContext deviceContext = Preconditions.checkNotNull(deviceContexts.get(deviceInfo));
137         deviceContext.onPublished();
138     }
139
140     @Override
141     public boolean deviceConnected(@CheckForNull final ConnectionContext connectionContext) throws Exception {
142         Preconditions.checkArgument(connectionContext != null);
143
144         DeviceInfo deviceInfo = connectionContext.getDeviceInfo();
145         /*
146          * This part prevent destroy another device context. Throwing here an exception result to propagate close connection
147          * in {@link org.opendaylight.openflowplugin.impl.connection.org.opendaylight.openflowplugin.impl.connection.HandshakeContextImpl}
148          * If context already exist we are in state closing process (connection flapping) and we should not propagate connection close
149          */
150          if (deviceContexts.containsKey(deviceInfo)) {
151             LOG.warn("Rejecting connection from node which is already connected and there exist deviceContext for it: {}", connectionContext.getNodeId());
152              return false;
153          }
154
155         LOG.info("ConnectionEvent: Device connected to controller, Device:{}, NodeId:{}",
156                 connectionContext.getConnectionAdapter().getRemoteAddress(), deviceInfo.getNodeId());
157
158         // Add Disconnect handler
159         connectionContext.setDeviceDisconnectedHandler(DeviceManagerImpl.this);
160         // Cache this for clarity
161         final ConnectionAdapter connectionAdapter = connectionContext.getConnectionAdapter();
162
163         //FIXME: as soon as auxiliary connection are fully supported then this is needed only before device context published
164         connectionAdapter.setPacketInFiltering(true);
165
166         final OutboundQueueProvider outboundQueueProvider = new OutboundQueueProviderImpl(deviceInfo.getVersion());
167
168         connectionContext.setOutboundQueueProvider(outboundQueueProvider);
169         final OutboundQueueHandlerRegistration<OutboundQueueProvider> outboundQueueHandlerRegistration =
170                 connectionAdapter.registerOutboundQueueHandler(outboundQueueProvider, barrierCountLimit, barrierIntervalNanos);
171         connectionContext.setOutboundQueueHandleRegistration(outboundQueueHandlerRegistration);
172
173         final DeviceState deviceState = new DeviceStateImpl(deviceInfo);
174         this.addDeviceSynchronizeListener(deviceState);
175         this.addDeviceValidListener(deviceState);
176
177         final DeviceContext deviceContext = new DeviceContextImpl(connectionContext,
178                 deviceState,
179                 dataBroker,
180                 conductor,
181                 outboundQueueProvider,
182                 translatorLibrary,
183                 this,
184                 connectionContext.getDeviceInfo(),
185                 convertorExecutor);
186
187         Verify.verify(deviceContexts.putIfAbsent(deviceInfo, deviceContext) == null, "DeviceCtx still not closed.");
188
189         deviceContext.setSwitchFeaturesMandatory(switchFeaturesMandatory);
190
191         ((ExtensionConverterProviderKeeper) deviceContext).setExtensionConverterProvider(extensionConverterProvider);
192         deviceContext.setNotificationPublishService(conductor.getNotificationPublishService());
193
194         updatePacketInRateLimiters();
195
196         final OpenflowProtocolListenerFullImpl messageListener = new OpenflowProtocolListenerFullImpl(
197                 connectionAdapter, deviceContext);
198         connectionAdapter.setMessageListener(messageListener);
199         notifyDeviceValidListeners(deviceInfo, true);
200
201         deviceInitPhaseHandler.onDeviceContextLevelUp(connectionContext.getDeviceInfo());
202
203         notifyDeviceSynchronizeListeners(deviceInfo, true);
204
205         return true;
206     }
207
208     private void updatePacketInRateLimiters() {
209         synchronized (deviceContexts) {
210             final int deviceContextsSize = deviceContexts.size();
211             if (deviceContextsSize > 0) {
212                 long freshNotificationLimit = globalNotificationQuota / deviceContextsSize;
213                 if (freshNotificationLimit < 100) {
214                     freshNotificationLimit = 100;
215                 }
216                 LOG.debug("fresh notification limit = {}", freshNotificationLimit);
217                 for (final DeviceContext deviceContext : deviceContexts.values()) {
218                     deviceContext.updatePacketInRateLimit(freshNotificationLimit);
219                 }
220             }
221         }
222     }
223
224     @Override
225     public TranslatorLibrary oook() {
226         return translatorLibrary;
227     }
228
229     @Override
230     public void setTranslatorLibrary(final TranslatorLibrary translatorLibrary) {
231         this.translatorLibrary = translatorLibrary;
232     }
233
234     @Override
235     public void close() {
236         for (final Iterator<DeviceContext> iterator = Iterators.consumingIterator(deviceContexts.values().iterator());
237                 iterator.hasNext();) {
238             final DeviceContext deviceCtx = iterator.next();
239             notifyDeviceValidListeners(deviceCtx.getDeviceInfo(), false);
240             deviceCtx.shutdownConnection();
241             deviceCtx.shuttingDownDataStoreTransactions();
242         }
243
244         if (spyPool != null) {
245             spyPool.shutdownNow();
246             spyPool = null;
247         }
248     }
249
250     @Override
251     public void onDeviceContextLevelDown(final DeviceInfo deviceInfo) {
252         LOG.debug("onDeviceContextClosed for Node {}", deviceInfo.getNodeId());
253         deviceContexts.remove(deviceInfo);
254         updatePacketInRateLimiters();
255     }
256
257     @Override
258     public void initialize() {
259         spyPool.scheduleAtFixedRate(conductor.getMessageIntelligenceAgency(), SPY_RATE, SPY_RATE, TimeUnit.SECONDS);
260     }
261
262     @Override
263     public void setExtensionConverterProvider(final ExtensionConverterProvider extensionConverterProvider) {
264         this.extensionConverterProvider = extensionConverterProvider;
265     }
266
267     @Override
268     public ExtensionConverterProvider getExtensionConverterProvider() {
269         return extensionConverterProvider;
270     }
271
272     @Override
273     public void setDeviceTerminationPhaseHandler(final DeviceTerminationPhaseHandler handler) {
274         this.deviceTerminPhaseHandler = handler;
275     }
276
277     @Override
278     public void onDeviceDisconnected(final ConnectionContext connectionContext) {
279         LOG.trace("onDeviceDisconnected method call for Node: {}", connectionContext.getNodeId());
280         final DeviceInfo deviceInfo = connectionContext.getDeviceInfo();
281         final DeviceContext deviceCtx = this.deviceContexts.get(deviceInfo);
282
283         if (null == deviceCtx) {
284             LOG.info("DeviceContext for Node {} was not found. Connection is terminated without OFP context suite.", deviceInfo.getNodeId());
285             return;
286         }
287
288         if (!connectionContext.equals(deviceCtx.getPrimaryConnectionContext())) {
289             /* Connection is not PrimaryConnection so try to remove from Auxiliary Connections */
290             deviceCtx.removeAuxiliaryConnectionContext(connectionContext);
291         } else {
292             notifyDeviceValidListeners(deviceInfo, false);
293             /* Device is disconnected and so we need to close TxManager */
294             final ListenableFuture<Void> future = deviceCtx.shuttingDownDataStoreTransactions();
295             Futures.addCallback(future, new FutureCallback<Void>() {
296
297                 @Override
298                 public void onSuccess(final Void result) {
299                     LOG.debug("TxChainManager for device {} is closed successful.", deviceInfo.getNodeId());
300                     deviceTerminPhaseHandler.onDeviceContextLevelDown(deviceInfo);
301                 }
302
303                 @Override
304                 public void onFailure(final Throwable t) {
305                     LOG.warn("TxChainManager for device {} failed by closing.", deviceInfo.getNodeId(), t);
306                     deviceTerminPhaseHandler.onDeviceContextLevelDown(deviceInfo);
307                 }
308             });
309             /* Add timer for Close TxManager because it could fain ind cluster without notification */
310             final TimerTask timerTask = timeout -> {
311                 if (!future.isDone()) {
312                     LOG.info("Shutting down TxChain for node {} not completed during 10 sec. Continue anyway.", deviceInfo.getNodeId());
313                     future.cancel(false);
314                 }
315             };
316             conductor.newTimeout(timerTask, 10, TimeUnit.SECONDS);
317         }
318     }
319
320     @VisibleForTesting
321     void addDeviceContextToMap(final DeviceInfo deviceInfo, final DeviceContext deviceContext){
322         deviceContexts.put(deviceInfo, deviceContext);
323     }
324
325     @Override
326     public <T extends OFPContext> T gainContext(final DeviceInfo deviceInfo) {
327         return (T) deviceContexts.get(deviceInfo);
328     }
329
330     @Override
331     public ListenableFuture<Void> onClusterRoleChange(final DeviceInfo deviceInfo, final OfpRole role) {
332         DeviceContext deviceContext = deviceContexts.get(deviceInfo);
333         LOG.trace("onClusterRoleChange {} for node:", role, deviceInfo.getNodeId());
334         if (OfpRole.BECOMEMASTER.equals(role)) {
335             return onDeviceTakeClusterLeadership(deviceInfo);
336         }
337         return ((DeviceContextImpl)deviceContext).getTransactionChainManager().deactivateTransactionManager();
338     }
339
340     @Override
341     public void addDeviceSynchronizeListener(final DeviceSynchronizeListener deviceSynchronizeListener) {
342         this.deviceSynchronizedListeners.add(deviceSynchronizeListener);
343     }
344
345     @Override
346     public void notifyDeviceSynchronizeListeners(final DeviceInfo deviceInfo, final boolean deviceSynchronized) {
347         for (DeviceSynchronizeListener listener : deviceSynchronizedListeners) {
348             listener.deviceIsSynchronized(deviceInfo, deviceSynchronized);
349         }
350     }
351
352     @Override
353     public void addDeviceValidListener(final DeviceValidListener deviceValidListener) {
354         this.deviceValidListeners.add(deviceValidListener);
355     }
356
357     @Override
358     public void notifyDeviceValidListeners(final DeviceInfo deviceInfo, final boolean deviceValid) {
359         for (DeviceValidListener listener : deviceValidListeners) {
360             listener.deviceIsValid(deviceInfo, deviceValid);
361         }
362     }
363
364     @Override
365     public void setIsNotificationFlowRemovedOff(boolean isNotificationFlowRemovedOff) {
366         this.isNotificationFlowRemovedOff = isNotificationFlowRemovedOff;
367     }
368
369     @Override
370     public boolean getIsNotificationFlowRemovedOff() {
371         return this.isNotificationFlowRemovedOff;
372     }
373
374     private ListenableFuture<Void> onDeviceTakeClusterLeadership(final DeviceInfo deviceInfo) {
375         LOG.trace("onDeviceTakeClusterLeadership for node: {}", deviceInfo.getNodeId());
376         /* validation */
377         StatisticsContext statisticsContext = conductor.getStatisticsContext(deviceInfo);
378         if (statisticsContext == null) {
379             final String errMsg = String.format("DeviceCtx %s is up but we are missing StatisticsContext", deviceInfo.getDatapathId());
380             LOG.warn(errMsg);
381             return Futures.immediateFailedFuture(new IllegalStateException(errMsg));
382         }
383         DeviceContext deviceContext = deviceContexts.get(deviceInfo);
384         /* Prepare init info collecting */
385         notifyDeviceSynchronizeListeners(deviceInfo, false);
386         ((DeviceContextImpl)deviceContext).getTransactionChainManager().activateTransactionManager();
387         ((DeviceContextImpl)deviceContext).getTransactionChainManager().initialSubmitWriteTransaction();
388         /* Init Collecting NodeInfo */
389         final ListenableFuture<Void> initCollectingDeviceInfo = DeviceInitializationUtils.initializeNodeInformation(
390                 deviceContext, switchFeaturesMandatory, convertorExecutor);
391         /* Init Collecting StatInfo */
392         final ListenableFuture<Boolean> statPollFuture = Futures.transform(initCollectingDeviceInfo,
393                 new AsyncFunction<Void, Boolean>() {
394
395                     @Override
396                     public ListenableFuture<Boolean> apply(@Nonnull final Void input) throws Exception {
397                         statisticsContext.statListForCollectingInitialization();
398                         return statisticsContext.initialGatherDynamicData();
399                     }
400                 });
401
402         return Futures.transform(statPollFuture, getInitialDeviceInformation(deviceContext));
403     }
404
405     private Function<Boolean, Void> getInitialDeviceInformation(final DeviceContext deviceContext) {
406         return input -> {
407             if (ConnectionContext.CONNECTION_STATE.RIP.equals(
408                     conductor.gainConnectionStateSafely(deviceContext.getDeviceInfo())
409             )) {
410                 final String errMsg =
411                         String.format("We lost connection for Device %s, context has to be closed.",
412                         deviceContext.getDeviceInfo().getNodeId());
413                 LOG.warn(errMsg);
414                 throw new IllegalStateException(errMsg);
415             }
416
417             if (input == null || !input) {
418                 final String errMsg =
419                         String.format("Get Initial Device %s information fails",
420                         deviceContext.getDeviceInfo().getNodeId());
421                 LOG.warn(errMsg);
422                 throw new IllegalStateException(errMsg);
423             }
424             LOG.debug("Get Initial Device {} information is successful",
425                     deviceContext.getDeviceInfo().getNodeId());
426             notifyDeviceSynchronizeListeners(deviceContext.getDeviceInfo(), true);
427             deviceContext.getDeviceState().setStatisticsPollingEnabledProp(true);
428             return null;
429         };
430     }
431
432 }