3cb77d0bddad4fb3a6ac6fda6c59d5511791d142
[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                     contexts.put(deviceContext, statisticsContext);
130                     final TimeCounter timeCounter = new TimeCounter();
131                     deviceContext.getDeviceState().setStatisticsPollingEnabledProp(true);
132                     scheduleNextPolling(deviceContext, statisticsContext, timeCounter);
133                     LOG.trace("Device dynamic info collecting done. Going to announce raise to next level.");
134                     try {
135                         deviceInitPhaseHandler.onDeviceContextLevelUp(deviceContext);
136                     } catch (final Exception e) {
137                         LOG.info("failed to complete levelUp on next handler for device {}", deviceContext.getDeviceState().getNodeId());
138                         deviceContext.close();
139                         return;
140                     }
141                     deviceContext.getDeviceState().setDeviceSynchronized(true);
142                 } else {
143                     final String deviceAdress = deviceContext.getPrimaryConnectionContext().getConnectionAdapter().getRemoteAddress().toString();
144                     LOG.info("Statistics for device {} could not be gathered. Closing its device context.", deviceAdress);
145                     deviceContext.close();
146                 }
147             }
148
149             @Override
150             public void onFailure(final Throwable throwable) {
151                 LOG.warn("Statistics manager was not able to collect dynamic info for device.", deviceContext.getDeviceState().getNodeId(), throwable);
152                 deviceContext.close();
153             }
154         });
155     }
156
157     private void pollStatistics(final DeviceContext deviceContext,
158                                 final StatisticsContext statisticsContext,
159                                 final TimeCounter timeCounter) {
160         
161         if (!deviceContext.getDeviceState().isValid()) {
162             LOG.debug("Session for device {} is not valid.", deviceContext.getDeviceState().getNodeId().getValue());
163             return;
164         }
165         if (!deviceContext.getDeviceState().isStatisticsPollingEnabled()) {
166             LOG.debug("StatisticsPolling is disabled for device: {} , try later", deviceContext.getDeviceState().getNodeId());
167             scheduleNextPolling(deviceContext, statisticsContext, timeCounter);
168             return;
169         }
170         if (OfpRole.BECOMESLAVE.equals(deviceContext.getDeviceState().getRole())) {
171             LOG.debug("Role is SLAVE so we don't want to poll any stat for device: {}", deviceContext.getDeviceState().getNodeId());
172             scheduleNextPolling(deviceContext, statisticsContext, timeCounter);
173             return;
174         }
175
176         LOG.debug("POLLING ALL STATS for device: {}", deviceContext.getDeviceState().getNodeId().getValue());
177         timeCounter.markStart();
178         final ListenableFuture<Boolean> deviceStatisticsCollectionFuture = statisticsContext.gatherDynamicData();
179         Futures.addCallback(deviceStatisticsCollectionFuture, new FutureCallback<Boolean>() {
180             @Override
181             public void onSuccess(final Boolean o) {
182                 timeCounter.addTimeMark();
183                 calculateTimerDelay(timeCounter);
184                 scheduleNextPolling(deviceContext, statisticsContext, timeCounter);
185             }
186
187             @Override
188             public void onFailure(final Throwable throwable) {
189                 timeCounter.addTimeMark();
190                 LOG.info("Statistics gathering for single node was not successful: {}", throwable.getMessage());
191                 LOG.debug("Statistics gathering for single node was not successful.. ", throwable);
192                 calculateTimerDelay(timeCounter);
193                 scheduleNextPolling(deviceContext, statisticsContext, timeCounter);
194             }
195         });
196
197         final long averangeTime = TimeUnit.MILLISECONDS.toSeconds(timeCounter.getAverageTimeBetweenMarks());
198         final long STATS_TIMEOUT_SEC = averangeTime > 0 ? 3 * averangeTime : DEFAULT_STATS_TIMEOUT_SEC;
199         final TimerTask timerTask = new TimerTask() {
200
201             @Override
202             public void run(final Timeout timeout) throws Exception {
203                 if (!deviceStatisticsCollectionFuture.isDone()) {
204                     LOG.info("Statistics collection for node {} still in progress even after {} secs", deviceContext
205                             .getDeviceState().getNodeId(), STATS_TIMEOUT_SEC);
206                     deviceStatisticsCollectionFuture.cancel(true);
207                 }
208             }
209         };
210         deviceContext.getTimer().newTimeout(timerTask, STATS_TIMEOUT_SEC, TimeUnit.SECONDS);
211     }
212
213     private void scheduleNextPolling(final DeviceContext deviceContext,
214                                      final StatisticsContext statisticsContext,
215                                      final TimeCounter timeCounter) {
216         if (null != hashedWheelTimer) {
217             LOG.debug("SCHEDULING NEXT STATS POLLING for device: {}", deviceContext.getDeviceState().getNodeId().getValue());
218             if (!shuttingDownStatisticsPolling) {
219                 final Timeout pollTimeout = hashedWheelTimer.newTimeout(new TimerTask() {
220                     @Override
221                     public void run(final Timeout timeout) throws Exception {
222                         pollStatistics(deviceContext, statisticsContext, timeCounter);
223                     }
224                 }, currentTimerDelay, TimeUnit.MILLISECONDS);
225                 statisticsContext.setPollTimeout(pollTimeout);
226             }
227         } else {
228             LOG.debug("#!NOT SCHEDULING NEXT STATS POLLING for device: {}", deviceContext.getDeviceState().getNodeId().getValue());
229         }
230     }
231
232     @VisibleForTesting
233     protected void calculateTimerDelay(final TimeCounter timeCounter) {
234         final long averageStatisticsGatheringTime = timeCounter.getAverageTimeBetweenMarks();
235         if (averageStatisticsGatheringTime > currentTimerDelay) {
236             currentTimerDelay *= 2;
237             if (currentTimerDelay > maximumTimerDelay) {
238                 currentTimerDelay = maximumTimerDelay;
239             }
240         } else {
241             if (currentTimerDelay > basicTimerDelay) {
242                 currentTimerDelay /= 2;
243             } else {
244                 currentTimerDelay = basicTimerDelay;
245             }
246         }
247     }
248
249     @VisibleForTesting
250     protected static long getCurrentTimerDelay() {
251         return currentTimerDelay;
252     }
253
254     @Override
255     public void onDeviceContextClosed(final DeviceContext deviceContext) {
256         final StatisticsContext statisticsContext = contexts.remove(deviceContext);
257         if (null != statisticsContext) {
258             LOG.trace("Removing device context from stack. No more statistics gathering for node {}", deviceContext.getDeviceState().getNodeId());
259             statisticsContext.close();
260         }
261     }
262
263     @Override
264     public Future<RpcResult<GetStatisticsWorkModeOutput>> getStatisticsWorkMode() {
265         final GetStatisticsWorkModeOutputBuilder smModeOutputBld = new GetStatisticsWorkModeOutputBuilder();
266         smModeOutputBld.setMode(workMode);
267         return RpcResultBuilder.success(smModeOutputBld.build()).buildFuture();
268     }
269
270     @Override
271     public Future<RpcResult<Void>> changeStatisticsWorkMode(ChangeStatisticsWorkModeInput input) {
272         final Future<RpcResult<Void>> result;
273         // acquire exclusive access
274         if (workModeGuard.tryAcquire()) {
275             final StatisticsWorkMode targetWorkMode = input.getMode();
276             if (!workMode.equals(targetWorkMode)) {
277                 shuttingDownStatisticsPolling = StatisticsWorkMode.FULLYDISABLED.equals(targetWorkMode);
278                 // iterate through stats-ctx: propagate mode
279                 for (final Map.Entry<DeviceContext, StatisticsContext> contextEntry : contexts.entrySet()) {
280                     final DeviceContext deviceContext = contextEntry.getKey();
281                     final StatisticsContext statisticsContext = contextEntry.getValue();
282                     switch (targetWorkMode) {
283                         case COLLECTALL:
284                             scheduleNextPolling(deviceContext, statisticsContext, new TimeCounter());
285                             for (final ItemLifeCycleSource lifeCycleSource : deviceContext.getItemLifeCycleSourceRegistry().getLifeCycleSources()) {
286                                 lifeCycleSource.setItemLifecycleListener(null);
287                             }
288                             break;
289                         case FULLYDISABLED:
290                             final Optional<Timeout> pollTimeout = statisticsContext.getPollTimeout();
291                             if (pollTimeout.isPresent()) {
292                                 pollTimeout.get().cancel();
293                             }
294                             for (final ItemLifeCycleSource lifeCycleSource : deviceContext.getItemLifeCycleSourceRegistry().getLifeCycleSources()) {
295                                 lifeCycleSource.setItemLifecycleListener(statisticsContext.getItemLifeCycleListener());
296                             }
297                             break;
298                         default:
299                             LOG.warn("statistics work mode not supported: {}", targetWorkMode);
300                     }
301                 }
302                 workMode = targetWorkMode;
303             }
304             workModeGuard.release();
305             result = RpcResultBuilder.<Void>success().buildFuture();
306         } else {
307             result = RpcResultBuilder.<Void>failed()
308                     .withError(RpcError.ErrorType.APPLICATION, "mode change already in progress")
309                     .buildFuture();
310         }
311         return result;
312     }
313
314     @Override
315     public void close() {
316         if (controlServiceRegistration != null) {
317             controlServiceRegistration.close();
318             controlServiceRegistration = null;
319         }
320         for (final Iterator<Entry<DeviceContext, StatisticsContext>> iterator = Iterators
321                 .consumingIterator(contexts.entrySet().iterator()); iterator.hasNext();) {
322             iterator.next().getValue().close();
323         }
324     }
325 }