Merge "Bug 5596 Initial commit failed"
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / statistics / StatisticsManagerImpl.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
9 package org.opendaylight.openflowplugin.impl.statistics;
10
11 import com.google.common.annotations.VisibleForTesting;
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.FutureCallback;
16 import com.google.common.util.concurrent.Futures;
17 import com.google.common.util.concurrent.ListenableFuture;
18 import io.netty.util.HashedWheelTimer;
19 import io.netty.util.Timeout;
20 import io.netty.util.TimerTask;
21 import java.util.Iterator;
22 import java.util.Map;
23 import java.util.Optional;
24 import java.util.concurrent.CancellationException;
25 import java.util.concurrent.ConcurrentHashMap;
26 import java.util.concurrent.ConcurrentMap;
27 import java.util.concurrent.Future;
28 import java.util.concurrent.Semaphore;
29 import java.util.concurrent.TimeUnit;
30 import javax.annotation.Nonnull;
31 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker;
32 import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
33 import org.opendaylight.openflowplugin.api.openflow.OFPContext;
34 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
35 import org.opendaylight.openflowplugin.api.openflow.device.DeviceInfo;
36 import org.opendaylight.openflowplugin.api.openflow.device.DeviceState;
37 import org.opendaylight.openflowplugin.api.openflow.device.handlers.DeviceInitializationPhaseHandler;
38 import org.opendaylight.openflowplugin.api.openflow.device.handlers.DeviceTerminationPhaseHandler;
39 import org.opendaylight.openflowplugin.api.openflow.lifecycle.LifecycleService;
40 import org.opendaylight.openflowplugin.api.openflow.rpc.ItemLifeCycleSource;
41 import org.opendaylight.openflowplugin.api.openflow.statistics.StatisticsContext;
42 import org.opendaylight.openflowplugin.api.openflow.statistics.StatisticsManager;
43 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.ConvertorExecutor;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflowplugin.sm.control.rev150812.ChangeStatisticsWorkModeInput;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflowplugin.sm.control.rev150812.GetStatisticsWorkModeOutput;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflowplugin.sm.control.rev150812.GetStatisticsWorkModeOutputBuilder;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflowplugin.sm.control.rev150812.StatisticsManagerControlService;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflowplugin.sm.control.rev150812.StatisticsWorkMode;
49 import org.opendaylight.yangtools.yang.common.RpcError;
50 import org.opendaylight.yangtools.yang.common.RpcResult;
51 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
52 import org.slf4j.Logger;
53 import org.slf4j.LoggerFactory;
54
55 public class StatisticsManagerImpl implements StatisticsManager, StatisticsManagerControlService {
56
57     private static final Logger LOG = LoggerFactory.getLogger(StatisticsManagerImpl.class);
58
59     private static final long DEFAULT_STATS_TIMEOUT_SEC = 50L;
60     private final ConvertorExecutor convertorExecutor;
61
62     private DeviceInitializationPhaseHandler deviceInitPhaseHandler;
63     private DeviceTerminationPhaseHandler deviceTerminPhaseHandler;
64
65     private final ConcurrentMap<DeviceInfo, StatisticsContext> contexts = new ConcurrentHashMap<>();
66
67     private static final long basicTimerDelay = 3000;
68     private static long currentTimerDelay = basicTimerDelay;
69     private static final long maximumTimerDelay = 900000; //wait max 15 minutes for next statistics
70
71     private StatisticsWorkMode workMode = StatisticsWorkMode.COLLECTALL;
72     private final Semaphore workModeGuard = new Semaphore(1, true);
73     private boolean isStatisticsPollingEnabled;
74     private BindingAwareBroker.RpcRegistration<StatisticsManagerControlService> controlServiceRegistration;
75
76     private final HashedWheelTimer hashedWheelTimer;
77
78     @Override
79     public void setDeviceInitializationPhaseHandler(final DeviceInitializationPhaseHandler handler) {
80         deviceInitPhaseHandler = handler;
81     }
82
83     public StatisticsManagerImpl(final RpcProviderRegistry rpcProviderRegistry,
84                                  final boolean isStatisticsPollingEnabled,
85                                  final HashedWheelTimer hashedWheelTimer,
86                                  final ConvertorExecutor convertorExecutor) {
87         Preconditions.checkArgument(rpcProviderRegistry != null);
88         this.convertorExecutor = convertorExecutor;
89         this.controlServiceRegistration = Preconditions.checkNotNull(rpcProviderRegistry.addRpcImplementation(
90                 StatisticsManagerControlService.class, this));
91         this.isStatisticsPollingEnabled = isStatisticsPollingEnabled;
92         this.hashedWheelTimer = hashedWheelTimer;
93     }
94
95     @Override
96     public void onDeviceContextLevelUp(final DeviceInfo deviceInfo, final LifecycleService lifecycleService) throws Exception {
97
98         final StatisticsContext statisticsContext = new StatisticsContextImpl(deviceInfo, isStatisticsPollingEnabled, lifecycleService, convertorExecutor, this);
99         Verify.verify(contexts.putIfAbsent(deviceInfo, statisticsContext) == null, "StatisticsCtx still not closed for Node {}", deviceInfo.getLOGValue());
100         lifecycleService.setStatContext(statisticsContext);
101         deviceInitPhaseHandler.onDeviceContextLevelUp(deviceInfo, lifecycleService);
102     }
103
104     @VisibleForTesting
105     void pollStatistics(final DeviceState deviceState,
106                         final StatisticsContext statisticsContext,
107                         final TimeCounter timeCounter,
108                         final DeviceInfo deviceInfo) {
109
110         if (!statisticsContext.isSchedulingEnabled()) {
111             LOG.debug("Disabled statistics scheduling for device: {}", deviceInfo.getNodeId().getValue());
112             return;
113         }
114
115         LOG.debug("POLLING ALL STATISTICS for device: {}", deviceInfo.getNodeId());
116         timeCounter.markStart();
117         final ListenableFuture<Boolean> deviceStatisticsCollectionFuture = statisticsContext.gatherDynamicData();
118         Futures.addCallback(deviceStatisticsCollectionFuture, new FutureCallback<Boolean>() {
119             @Override
120             public void onSuccess(final Boolean o) {
121                 timeCounter.addTimeMark();
122                 calculateTimerDelay(timeCounter);
123                 scheduleNextPolling(deviceState, deviceInfo, statisticsContext, timeCounter);
124             }
125
126             @Override
127             public void onFailure(@Nonnull final Throwable throwable) {
128                 timeCounter.addTimeMark();
129                 LOG.warn("Statistics gathering for single node was not successful: {}", throwable.getMessage());
130                 LOG.trace("Statistics gathering for single node was not successful.. ", throwable);
131                 calculateTimerDelay(timeCounter);
132                 if (throwable instanceof CancellationException) {
133                     /* This often happens when something wrong with akka or DS, so closing connection will help to restart device **/
134                     contexts.get(deviceInfo).getLifecycleService().closeConnection();
135                 } else {
136                     scheduleNextPolling(deviceState, deviceInfo, statisticsContext, timeCounter);
137                 }
138             }
139         });
140
141         final long averageTime = TimeUnit.MILLISECONDS.toSeconds(timeCounter.getAverageTimeBetweenMarks());
142         final long STATS_TIMEOUT_SEC = averageTime > 0 ? 3 * averageTime : DEFAULT_STATS_TIMEOUT_SEC;
143         final TimerTask timerTask = timeout -> {
144             if (!deviceStatisticsCollectionFuture.isDone()) {
145                 LOG.info("Statistics collection for node {} still in progress even after {} secs", deviceInfo.getLOGValue(), STATS_TIMEOUT_SEC);
146                 deviceStatisticsCollectionFuture.cancel(true);
147             }
148         };
149
150         hashedWheelTimer.newTimeout(timerTask, STATS_TIMEOUT_SEC, TimeUnit.SECONDS);
151     }
152
153     private void scheduleNextPolling(final DeviceState deviceState,
154                                      final DeviceInfo deviceInfo,
155                                      final StatisticsContext statisticsContext,
156                                      final TimeCounter timeCounter) {
157         LOG.debug("SCHEDULING NEXT STATISTICS POLLING for device: {}", deviceInfo.getNodeId());
158         if (!isStatisticsPollingEnabled) {
159             final Timeout pollTimeout = hashedWheelTimer.newTimeout(
160                     timeout -> pollStatistics(
161                             deviceState,
162                             statisticsContext,
163                             timeCounter,
164                             deviceInfo),
165                     currentTimerDelay,
166                     TimeUnit.MILLISECONDS);
167             statisticsContext.setPollTimeout(pollTimeout);
168         }
169     }
170
171     @VisibleForTesting
172     void calculateTimerDelay(final TimeCounter timeCounter) {
173         final long averageStatisticsGatheringTime = timeCounter.getAverageTimeBetweenMarks();
174         if (averageStatisticsGatheringTime > currentTimerDelay) {
175             currentTimerDelay *= 2;
176             if (currentTimerDelay > maximumTimerDelay) {
177                 currentTimerDelay = maximumTimerDelay;
178             }
179         } else {
180             if (currentTimerDelay > basicTimerDelay) {
181                 currentTimerDelay /= 2;
182             } else {
183                 currentTimerDelay = basicTimerDelay;
184             }
185         }
186     }
187
188     @VisibleForTesting
189     static long getCurrentTimerDelay() {
190         return currentTimerDelay;
191     }
192
193     @Override
194     public void onDeviceContextLevelDown(final DeviceInfo deviceInfo) {
195         final StatisticsContext statisticsContext = contexts.remove(deviceInfo);
196         if (null != statisticsContext) {
197             LOG.debug("Removing device context from stack. No more statistics gathering for device: {}", deviceInfo.getLOGValue());
198             statisticsContext.close();
199         }
200         deviceTerminPhaseHandler.onDeviceContextLevelDown(deviceInfo);
201     }
202
203     @Override
204     public Future<RpcResult<GetStatisticsWorkModeOutput>> getStatisticsWorkMode() {
205         final GetStatisticsWorkModeOutputBuilder smModeOutputBld = new GetStatisticsWorkModeOutputBuilder();
206         smModeOutputBld.setMode(workMode);
207         return RpcResultBuilder.success(smModeOutputBld.build()).buildFuture();
208     }
209
210     @Override
211     public Future<RpcResult<Void>> changeStatisticsWorkMode(ChangeStatisticsWorkModeInput input) {
212         final Future<RpcResult<Void>> result;
213         // acquire exclusive access
214         if (workModeGuard.tryAcquire()) {
215             final StatisticsWorkMode targetWorkMode = input.getMode();
216             if (!workMode.equals(targetWorkMode)) {
217                 isStatisticsPollingEnabled = StatisticsWorkMode.FULLYDISABLED.equals(targetWorkMode);
218                 // iterate through stats-ctx: propagate mode
219                 for (Map.Entry<DeviceInfo, StatisticsContext> entry : contexts.entrySet()) {
220                     final DeviceInfo deviceInfo = entry.getKey();
221                     final StatisticsContext statisticsContext = entry.getValue();
222                     final DeviceContext deviceContext = statisticsContext.getLifecycleService().getDeviceContext();
223                     switch (targetWorkMode) {
224                         case COLLECTALL:
225                             scheduleNextPolling(deviceContext.getDeviceState(), deviceInfo, statisticsContext, new TimeCounter());
226                             for (final ItemLifeCycleSource lifeCycleSource : deviceContext.getItemLifeCycleSourceRegistry().getLifeCycleSources()) {
227                                 lifeCycleSource.setItemLifecycleListener(null);
228                             }
229                             break;
230                         case FULLYDISABLED:
231                             final Optional<Timeout> pollTimeout = statisticsContext.getPollTimeout();
232                             if (pollTimeout.isPresent()) {
233                                 pollTimeout.get().cancel();
234                             }
235                             for (final ItemLifeCycleSource lifeCycleSource : deviceContext.getItemLifeCycleSourceRegistry().getLifeCycleSources()) {
236                                 lifeCycleSource.setItemLifecycleListener(statisticsContext.getItemLifeCycleListener());
237                             }
238                             break;
239                         default:
240                             LOG.warn("Statistics work mode not supported: {}", targetWorkMode);
241                     }
242                 }
243                 workMode = targetWorkMode;
244             }
245             workModeGuard.release();
246             result = RpcResultBuilder.<Void>success().buildFuture();
247         } else {
248             result = RpcResultBuilder.<Void>failed()
249                     .withError(RpcError.ErrorType.APPLICATION, "mode change already in progress")
250                     .buildFuture();
251         }
252         return result;
253     }
254
255     @Override
256     public void startScheduling(final DeviceInfo deviceInfo) {
257         if (isStatisticsPollingEnabled) {
258             LOG.info("Statistics are shut down for device: {}", deviceInfo.getNodeId());
259             return;
260         }
261
262         final StatisticsContext statisticsContext = contexts.get(deviceInfo);
263
264         if (statisticsContext == null) {
265             LOG.warn("Statistics context not found for device: {}", deviceInfo.getNodeId());
266             return;
267         }
268
269         if (statisticsContext.isSchedulingEnabled()) {
270             LOG.debug("Statistics scheduling is already enabled for device: {}", deviceInfo.getNodeId());
271             return;
272         }
273
274         LOG.info("Scheduling statistics poll for device: {}", deviceInfo.getNodeId());
275
276         statisticsContext.setSchedulingEnabled(true);
277         final DeviceState deviceState = contexts.get(deviceInfo).getLifecycleService().getDeviceContext().getDeviceState();
278         scheduleNextPolling(deviceState, deviceInfo, statisticsContext, new TimeCounter());
279     }
280
281     @Override
282     public void stopScheduling(final DeviceInfo deviceInfo) {
283         LOG.debug("Stopping statistics scheduling for device: {}", deviceInfo.getNodeId());
284         final StatisticsContext statisticsContext = contexts.get(deviceInfo);
285
286         if (statisticsContext == null) {
287             LOG.warn("Statistics context not found for device: {}", deviceInfo.getNodeId());
288             return;
289         }
290
291         statisticsContext.setSchedulingEnabled(false);
292     }
293
294     @Override
295     public void close() {
296         if (controlServiceRegistration != null) {
297             controlServiceRegistration.close();
298             controlServiceRegistration = null;
299         }
300         for (final Iterator<StatisticsContext> iterator = Iterators.consumingIterator(contexts.values().iterator());
301                 iterator.hasNext();) {
302             iterator.next().close();
303         }
304     }
305
306     @Override
307     public void setDeviceTerminationPhaseHandler(final DeviceTerminationPhaseHandler handler) {
308         this.deviceTerminPhaseHandler = handler;
309     }
310
311     @Override
312     public <T extends OFPContext> T gainContext(DeviceInfo deviceInfo) {
313         return (T) contexts.get(deviceInfo);
314     }
315 }