2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.openflowplugin.impl.statistics;
11 import com.google.common.util.concurrent.FutureCallback;
12 import com.google.common.util.concurrent.Futures;
13 import com.google.common.util.concurrent.ListenableFuture;
14 import io.netty.util.HashedWheelTimer;
15 import io.netty.util.Timeout;
16 import io.netty.util.TimerTask;
17 import java.util.concurrent.ConcurrentHashMap;
18 import java.util.concurrent.TimeUnit;
19 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
20 import org.opendaylight.openflowplugin.api.openflow.device.handlers.DeviceInitializationPhaseHandler;
21 import org.opendaylight.openflowplugin.api.openflow.statistics.StatisticsContext;
22 import org.opendaylight.openflowplugin.api.openflow.statistics.StatisticsManager;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
27 * Created by Martin Bobak <mbobak@cisco.com> on 1.4.2015.
29 public class StatisticsManagerImpl implements StatisticsManager {
31 private static final Logger LOG = LoggerFactory.getLogger(StatisticsManagerImpl.class);
33 private DeviceInitializationPhaseHandler deviceInitPhaseHandler;
35 private HashedWheelTimer hashedWheelTimer;
37 private ConcurrentHashMap<DeviceContext, StatisticsContext> contexts = new ConcurrentHashMap();
40 public void setDeviceInitializationPhaseHandler(final DeviceInitializationPhaseHandler handler) {
41 deviceInitPhaseHandler = handler;
45 public void onDeviceContextLevelUp(final DeviceContext deviceContext) {
47 if (null == hashedWheelTimer) {
48 LOG.trace("This is first device that delivered timer. Starting statistics polling immediately.");
49 hashedWheelTimer = deviceContext.getTimer();
53 final StatisticsContext statisticsContext = new StatisticsContextImpl(deviceContext);
55 ListenableFuture<Void> weHaveDynamicData = statisticsContext.gatherDynamicData();
56 weHaveDynamicData = Futures.immediateFuture(null);
57 Futures.addCallback(weHaveDynamicData, new FutureCallback<Void>() {
59 public void onSuccess(final Void aVoid) {
60 // wake up RPC registration
61 LOG.trace("Device dynamic info collected. Going to announce raise to next level.");
62 contexts.put(deviceContext, statisticsContext);
63 deviceContext.getDeviceState().setDeviceSynchronized(true);
64 deviceInitPhaseHandler.onDeviceContextLevelUp(deviceContext);
68 public void onFailure(final Throwable throwable) {
69 LOG.warn("Statistics manager was not able to collect dynamic info for device {}", deviceContext.getDeviceState().getNodeId(), throwable);
74 private void pollStatistics() {
75 for (final StatisticsContext statisticsContext : contexts.values()) {
76 ListenableFuture deviceStatisticsCollectionFuture = statisticsContext.gatherDynamicData();
77 Futures.addCallback(deviceStatisticsCollectionFuture, new FutureCallback() {
79 public void onSuccess(final Object o) {
84 public void onFailure(final Throwable throwable) {
85 LOG.info("Statistics gathering for single node was not successful.");
89 if (null != hashedWheelTimer) {
90 hashedWheelTimer.newTimeout(new TimerTask() {
92 public void run(final Timeout timeout) throws Exception {
95 }, 3000, TimeUnit.MILLISECONDS);
100 public void onDeviceContextClosed(final DeviceContext deviceContext) {
101 if (contexts.containsKey(deviceContext)) {
102 LOG.trace("Removing device context from stack. No more statistics gathering for node {}", deviceContext.getDeviceState().getNodeId());
103 contexts.remove(deviceContext);