86cb2eecf68e94b619a42933f8dfbc3cee251aee
[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.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;
25
26 /**
27  * Created by Martin Bobak <mbobak@cisco.com> on 1.4.2015.
28  */
29 public class StatisticsManagerImpl implements StatisticsManager {
30
31     private static final Logger LOG = LoggerFactory.getLogger(StatisticsManagerImpl.class);
32
33     private DeviceInitializationPhaseHandler deviceInitPhaseHandler;
34
35     private HashedWheelTimer hashedWheelTimer;
36
37     private ConcurrentHashMap<DeviceContext, StatisticsContext> contexts = new ConcurrentHashMap();
38
39     @Override
40     public void setDeviceInitializationPhaseHandler(final DeviceInitializationPhaseHandler handler) {
41         deviceInitPhaseHandler = handler;
42     }
43
44     @Override
45     public void onDeviceContextLevelUp(final DeviceContext deviceContext) {
46
47         if (null == hashedWheelTimer) {
48             LOG.trace("This is first device that delivered timer. Starting statistics polling immediately.");
49             hashedWheelTimer = deviceContext.getTimer();
50             pollStatistics();
51         }
52
53         final StatisticsContext statisticsContext = new StatisticsContextImpl(deviceContext);
54         deviceContext.addDeviceContextClosedHandler(this);
55         final ListenableFuture<Boolean> weHaveDynamicData = statisticsContext.gatherDynamicData();
56         Futures.addCallback(weHaveDynamicData, new FutureCallback<Boolean>() {
57             @Override
58             public void onSuccess(final Boolean statisticsGathered) {
59                 if (statisticsGathered.booleanValue()) {
60                     //there are some statistics on device worth gathering
61                     contexts.put(deviceContext, statisticsContext);
62                 }
63                 LOG.trace("Device dynamic info collecting done. Going to announce raise to next level.");
64                 deviceInitPhaseHandler.onDeviceContextLevelUp(deviceContext);
65                 deviceContext.getDeviceState().setDeviceSynchronized(true);
66             }
67
68             @Override
69             public void onFailure(final Throwable throwable) {
70                 LOG.warn("Statistics manager was not able to collect dynamic info for device.", deviceContext.getDeviceState().getNodeId(), throwable);
71                 try {
72                     deviceContext.close();
73                 } catch (Exception e) {
74                     LOG.warn("Error closing device context.", e);
75                 }
76             }
77         });
78     }
79
80     private void pollStatistics() {
81         for (final StatisticsContext statisticsContext : contexts.values()) {
82             ListenableFuture deviceStatisticsCollectionFuture = statisticsContext.gatherDynamicData();
83             Futures.addCallback(deviceStatisticsCollectionFuture, new FutureCallback() {
84                 @Override
85                 public void onSuccess(final Object o) {
86                     //nothing to do here
87                 }
88
89                 @Override
90                 public void onFailure(final Throwable throwable) {
91                     LOG.info("Statistics gathering for single node was not successful.");
92                 }
93             });
94         }
95         if (null != hashedWheelTimer) {
96             hashedWheelTimer.newTimeout(new TimerTask() {
97                 @Override
98                 public void run(final Timeout timeout) throws Exception {
99                     pollStatistics();
100                 }
101             }, 3000, TimeUnit.MILLISECONDS);
102         }
103     }
104
105     @Override
106     public void onDeviceContextClosed(final DeviceContext deviceContext) {
107         if (contexts.containsKey(deviceContext)) {
108             LOG.trace("Removing device context from stack. No more statistics gathering for node {}", deviceContext.getDeviceState().getNodeId());
109             contexts.remove(deviceContext);
110             StatisticsContext statisticsContext = contexts.remove(deviceContext);
111             try {
112                 statisticsContext.close();
113             } catch (Exception e) {
114                 LOG.debug("Error closing statistic context for node {}.", deviceContext.getDeviceState().getNodeId());
115             }
116         }
117     }
118 }