850a101ea317abf973ee924b0dc7a0cedd892cef
[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 javax.annotation.CheckForNull;
12 import java.util.Map;
13 import java.util.concurrent.ConcurrentHashMap;
14 import java.util.concurrent.ExecutionException;
15 import java.util.concurrent.Future;
16 import java.util.concurrent.Semaphore;
17 import java.util.concurrent.TimeUnit;
18 import java.util.concurrent.TimeoutException;
19
20 import com.google.common.annotations.VisibleForTesting;
21 import com.google.common.base.Optional;
22 import com.google.common.base.Preconditions;
23 import com.google.common.base.Verify;
24 import com.google.common.collect.Iterators;
25 import com.google.common.util.concurrent.FutureCallback;
26 import com.google.common.util.concurrent.Futures;
27 import com.google.common.util.concurrent.ListenableFuture;
28 import io.netty.util.HashedWheelTimer;
29 import io.netty.util.Timeout;
30 import io.netty.util.TimerTask;
31 import java.util.Iterator;
32 import java.util.Map;
33 import java.util.Map.Entry;
34 import java.util.concurrent.ConcurrentHashMap;
35 import java.util.concurrent.ConcurrentMap;
36 import java.util.concurrent.Future;
37 import java.util.concurrent.Semaphore;
38 import java.util.concurrent.TimeUnit;
39 import javax.annotation.CheckForNull;
40 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker;
41 import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
42 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
43 import org.opendaylight.openflowplugin.api.openflow.device.handlers.DeviceInitializationPhaseHandler;
44 import org.opendaylight.openflowplugin.api.openflow.rpc.ItemLifeCycleSource;
45 import org.opendaylight.openflowplugin.api.openflow.statistics.StatisticsContext;
46 import org.opendaylight.openflowplugin.api.openflow.statistics.StatisticsManager;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflowplugin.sm.control.rev150812.ChangeStatisticsWorkModeInput;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflowplugin.sm.control.rev150812.GetStatisticsWorkModeOutput;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflowplugin.sm.control.rev150812.GetStatisticsWorkModeOutputBuilder;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflowplugin.sm.control.rev150812.StatisticsManagerControlService;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflowplugin.sm.control.rev150812.StatisticsWorkMode;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.role.service.rev150727.OfpRole;
53 import org.opendaylight.yangtools.yang.common.RpcError;
54 import org.opendaylight.yangtools.yang.common.RpcResult;
55 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
56 import org.slf4j.Logger;
57 import org.slf4j.LoggerFactory;
58
59 /**
60  * Created by Martin Bobak <mbobak@cisco.com> on 1.4.2015.
61  */
62 public class StatisticsManagerImpl implements StatisticsManager, StatisticsManagerControlService {
63
64     private static final Logger LOG = LoggerFactory.getLogger(StatisticsManagerImpl.class);
65
66     private static final long DEFAULT_STATS_TIMEOUT_SEC = 50L;
67
68     private DeviceInitializationPhaseHandler deviceInitPhaseHandler;
69
70     private HashedWheelTimer hashedWheelTimer;
71
72     private final ConcurrentMap<DeviceContext, StatisticsContext> contexts = new ConcurrentHashMap<>();
73
74     private static final long basicTimerDelay = 3000;
75     private static long currentTimerDelay = basicTimerDelay;
76     private static long maximumTimerDelay = 900000; //wait max 15 minutes for next statistics
77
78     private StatisticsWorkMode workMode = StatisticsWorkMode.COLLECTALL;
79     private final Semaphore workModeGuard = new Semaphore(1, true);
80     private boolean shuttingDownStatisticsPolling;
81     private BindingAwareBroker.RpcRegistration<StatisticsManagerControlService> controlServiceRegistration;
82
83     @Override
84     public void setDeviceInitializationPhaseHandler(final DeviceInitializationPhaseHandler handler) {
85         deviceInitPhaseHandler = handler;
86     }
87
88     public StatisticsManagerImpl(@CheckForNull final RpcProviderRegistry rpcProviderRegistry, final boolean shuttingDownStatisticsPolling) {
89         Preconditions.checkArgument(rpcProviderRegistry != null);
90         controlServiceRegistration = rpcProviderRegistry.addRpcImplementation(StatisticsManagerControlService.class, this);
91         this.shuttingDownStatisticsPolling = shuttingDownStatisticsPolling;
92     }
93
94     @Override
95     public void onDeviceContextLevelUp(final DeviceContext deviceContext) throws Exception {
96         LOG.debug("Node:{}, deviceContext.getDeviceState().getRole():{}", deviceContext.getDeviceState().getNodeId(),
97                 deviceContext.getDeviceState().getRole());
98         if (null == hashedWheelTimer) {
99             LOG.trace("This is first device that delivered timer. Starting statistics polling immediately.");
100             hashedWheelTimer = deviceContext.getTimer();
101         }
102         final StatisticsContext statisticsContext = new StatisticsContextImpl(deviceContext, shuttingDownStatisticsPolling);
103
104         Verify.verify(contexts.putIfAbsent(deviceContext, statisticsContext) == null, "StatisticsCtx still not closed for Node {}",deviceContext.getDeviceState().getNodeId());
105         deviceContext.addDeviceContextClosedHandler(this);
106
107         if (shuttingDownStatisticsPolling) {
108             LOG.info("Statistics is shutdown for node:{}", deviceContext.getDeviceState().getNodeId());
109         } else {
110             LOG.info("Schedule Statistics poll for node:{}", deviceContext.getDeviceState().getNodeId());
111             if (OfpRole.BECOMEMASTER.equals(deviceContext.getDeviceState().getRole())) {
112                 initialStatPollForMaster(statisticsContext, deviceContext);
113                 /* we want to wait for initial statCollecting response */
114                 return;
115             }
116             scheduleNextPolling(deviceContext, statisticsContext, new TimeCounter());
117         }
118         deviceContext.getDeviceState().setDeviceSynchronized(true);
119         deviceInitPhaseHandler.onDeviceContextLevelUp(deviceContext);
120     }
121
122     private void initialStatPollForMaster(final StatisticsContext statisticsContext, final DeviceContext deviceContext) {
123         final ListenableFuture<Boolean> weHaveDynamicData = statisticsContext.gatherDynamicData();
124         Futures.addCallback(weHaveDynamicData, new FutureCallback<Boolean>() {
125             @Override
126             public void onSuccess(final Boolean statisticsGathered) {
127                 if (statisticsGathered) {
128                     //there are some statistics on device worth gathering
129                     final TimeCounter timeCounter = new TimeCounter();
130                     deviceContext.getDeviceState().setStatisticsPollingEnabledProp(true);
131                     scheduleNextPolling(deviceContext, statisticsContext, timeCounter);
132                     LOG.trace("Device dynamic info collecting done. Going to announce raise to next level.");
133                     try {
134                         deviceInitPhaseHandler.onDeviceContextLevelUp(deviceContext);
135                     } catch (final Exception e) {
136                         LOG.info("failed to complete levelUp on next handler for device {}", deviceContext.getDeviceState().getNodeId());
137                         deviceContext.close();
138                         return;
139                     }
140                     deviceContext.getDeviceState().setDeviceSynchronized(true);
141                 } else {
142                     final String deviceAddress = deviceContext.getPrimaryConnectionContext().getConnectionAdapter().getRemoteAddress().toString();
143                     LOG.info("Statistics for device {} could not be gathered. Closing its device context.", deviceAddress);
144                     deviceContext.close();
145                 }
146             }
147
148             @Override
149             public void onFailure(final Throwable throwable) {
150                 LOG.warn("Statistics manager was not able to collect dynamic info for device.", deviceContext.getDeviceState().getNodeId(), throwable);
151                 deviceContext.close();
152             }
153         });
154     }
155
156     private void pollStatistics(final DeviceContext deviceContext,
157                                 final StatisticsContext statisticsContext,
158                                 final TimeCounter timeCounter) {
159         
160         if (!deviceContext.getDeviceState().isValid()) {
161             LOG.debug("Session for device {} is not valid.", deviceContext.getDeviceState().getNodeId().getValue());
162             return;
163         }
164         if (!deviceContext.getDeviceState().isStatisticsPollingEnabled()) {
165             LOG.debug("StatisticsPolling is disabled for device: {} , try later", deviceContext.getDeviceState().getNodeId());
166             scheduleNextPolling(deviceContext, statisticsContext, timeCounter);
167             return;
168         }
169         if (!OfpRole.BECOMEMASTER.equals(deviceContext.getDeviceState().getRole())) {
170             LOG.debug("Role is not Master so we don't want to poll any stat for device: {}", deviceContext.getDeviceState().getNodeId());
171             scheduleNextPolling(deviceContext, statisticsContext, timeCounter);
172             return;
173         }
174
175         LOG.debug("POLLING ALL STATS for device: {}", deviceContext.getDeviceState().getNodeId().getValue());
176         timeCounter.markStart();
177         final ListenableFuture<Boolean> deviceStatisticsCollectionFuture = statisticsContext.gatherDynamicData();
178         Futures.addCallback(deviceStatisticsCollectionFuture, new FutureCallback<Boolean>() {
179             @Override
180             public void onSuccess(final Boolean o) {
181                 timeCounter.addTimeMark();
182                 calculateTimerDelay(timeCounter);
183                 scheduleNextPolling(deviceContext, statisticsContext, timeCounter);
184             }
185
186             @Override
187             public void onFailure(final Throwable throwable) {
188                 timeCounter.addTimeMark();
189                 LOG.info("Statistics gathering for single node was not successful: {}", throwable.getMessage());
190                 LOG.debug("Statistics gathering for single node was not successful.. ", throwable);
191                 calculateTimerDelay(timeCounter);
192                 scheduleNextPolling(deviceContext, statisticsContext, timeCounter);
193             }
194         });
195
196         final long averangeTime = TimeUnit.MILLISECONDS.toSeconds(timeCounter.getAverageTimeBetweenMarks());
197         final long STATS_TIMEOUT_SEC = averangeTime > 0 ? 3 * averangeTime : DEFAULT_STATS_TIMEOUT_SEC;
198         final TimerTask timerTask = new TimerTask() {
199
200             @Override
201             public void run(final Timeout timeout) throws Exception {
202                 if (!deviceStatisticsCollectionFuture.isDone()) {
203                     LOG.info("Statistics collection for node {} still in progress even after {} secs", deviceContext
204                             .getDeviceState().getNodeId(), STATS_TIMEOUT_SEC);
205                     deviceStatisticsCollectionFuture.cancel(true);
206                 }
207             }
208         };
209         deviceContext.getTimer().newTimeout(timerTask, STATS_TIMEOUT_SEC, TimeUnit.SECONDS);
210     }
211
212     private void scheduleNextPolling(final DeviceContext deviceContext,
213                                      final StatisticsContext statisticsContext,
214                                      final TimeCounter timeCounter) {
215         if (null != hashedWheelTimer) {
216             LOG.debug("SCHEDULING NEXT STATS POLLING for device: {}", deviceContext.getDeviceState().getNodeId().getValue());
217             if (!shuttingDownStatisticsPolling) {
218                 final Timeout pollTimeout = hashedWheelTimer.newTimeout(new TimerTask() {
219                     @Override
220                     public void run(final Timeout timeout) throws Exception {
221                         pollStatistics(deviceContext, statisticsContext, timeCounter);
222                     }
223                 }, currentTimerDelay, TimeUnit.MILLISECONDS);
224                 statisticsContext.setPollTimeout(pollTimeout);
225             }
226         } else {
227             LOG.debug("#!NOT SCHEDULING NEXT STATS POLLING for device: {}", deviceContext.getDeviceState().getNodeId().getValue());
228         }
229     }
230
231     @VisibleForTesting
232     protected void calculateTimerDelay(final TimeCounter timeCounter) {
233         final long averageStatisticsGatheringTime = timeCounter.getAverageTimeBetweenMarks();
234         if (averageStatisticsGatheringTime > currentTimerDelay) {
235             currentTimerDelay *= 2;
236             if (currentTimerDelay > maximumTimerDelay) {
237                 currentTimerDelay = maximumTimerDelay;
238             }
239         } else {
240             if (currentTimerDelay > basicTimerDelay) {
241                 currentTimerDelay /= 2;
242             } else {
243                 currentTimerDelay = basicTimerDelay;
244             }
245         }
246     }
247
248     @VisibleForTesting
249     protected static long getCurrentTimerDelay() {
250         return currentTimerDelay;
251     }
252
253     @Override
254     public void onDeviceContextClosed(final DeviceContext deviceContext) {
255         final StatisticsContext statisticsContext = contexts.remove(deviceContext);
256         if (null != statisticsContext) {
257             LOG.trace("Removing device context from stack. No more statistics gathering for node {}", deviceContext.getDeviceState().getNodeId());
258             statisticsContext.close();
259         }
260     }
261
262     @Override
263     public Future<RpcResult<GetStatisticsWorkModeOutput>> getStatisticsWorkMode() {
264         final GetStatisticsWorkModeOutputBuilder smModeOutputBld = new GetStatisticsWorkModeOutputBuilder();
265         smModeOutputBld.setMode(workMode);
266         return RpcResultBuilder.success(smModeOutputBld.build()).buildFuture();
267     }
268
269     @Override
270     public Future<RpcResult<Void>> changeStatisticsWorkMode(ChangeStatisticsWorkModeInput input) {
271         final Future<RpcResult<Void>> result;
272         // acquire exclusive access
273         if (workModeGuard.tryAcquire()) {
274             final StatisticsWorkMode targetWorkMode = input.getMode();
275             if (!workMode.equals(targetWorkMode)) {
276                 shuttingDownStatisticsPolling = StatisticsWorkMode.FULLYDISABLED.equals(targetWorkMode);
277                 // iterate through stats-ctx: propagate mode
278                 for (final Map.Entry<DeviceContext, StatisticsContext> contextEntry : contexts.entrySet()) {
279                     final DeviceContext deviceContext = contextEntry.getKey();
280                     final StatisticsContext statisticsContext = contextEntry.getValue();
281                     switch (targetWorkMode) {
282                         case COLLECTALL:
283                             scheduleNextPolling(deviceContext, statisticsContext, new TimeCounter());
284                             for (final ItemLifeCycleSource lifeCycleSource : deviceContext.getItemLifeCycleSourceRegistry().getLifeCycleSources()) {
285                                 lifeCycleSource.setItemLifecycleListener(null);
286                             }
287                             break;
288                         case FULLYDISABLED:
289                             final Optional<Timeout> pollTimeout = statisticsContext.getPollTimeout();
290                             if (pollTimeout.isPresent()) {
291                                 pollTimeout.get().cancel();
292                             }
293                             for (final ItemLifeCycleSource lifeCycleSource : deviceContext.getItemLifeCycleSourceRegistry().getLifeCycleSources()) {
294                                 lifeCycleSource.setItemLifecycleListener(statisticsContext.getItemLifeCycleListener());
295                             }
296                             break;
297                         default:
298                             LOG.warn("statistics work mode not supported: {}", targetWorkMode);
299                     }
300                 }
301                 workMode = targetWorkMode;
302             }
303             workModeGuard.release();
304             result = RpcResultBuilder.<Void>success().buildFuture();
305         } else {
306             result = RpcResultBuilder.<Void>failed()
307                     .withError(RpcError.ErrorType.APPLICATION, "mode change already in progress")
308                     .buildFuture();
309         }
310         return result;
311     }
312
313     @Override
314     public void close() {
315         if (controlServiceRegistration != null) {
316             controlServiceRegistration.close();
317             controlServiceRegistration = null;
318         }
319         for (final Iterator<Entry<DeviceContext, StatisticsContext>> iterator = Iterators
320                 .consumingIterator(contexts.entrySet().iterator()); iterator.hasNext();) {
321             iterator.next().getValue().close();
322         }
323     }
324 }