re-activate FindBugs
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / statistics / ofpspecific / MessageIntelligenceAgencyImpl.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.ofpspecific;
10
11 import com.google.common.base.Preconditions;
12 import java.util.ArrayList;
13 import java.util.List;
14 import java.util.Map;
15 import java.util.Map.Entry;
16 import java.util.concurrent.ConcurrentHashMap;
17 import java.util.concurrent.ConcurrentMap;
18 import java.util.concurrent.atomic.AtomicLongFieldUpdater;
19 import javax.annotation.Nonnull;
20 import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.MessageIntelligenceAgency;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23
24 /**
25  * Implementation of
26  * {@link org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.MessageIntelligenceAgency}.
27  * Class counts message of {@link StatisticsGroup} type and provides info as debug log.
28  */
29 public class MessageIntelligenceAgencyImpl implements MessageIntelligenceAgency, MessageIntelligenceAgencyMXBean {
30
31     private static final Logger LOG = LoggerFactory.getLogger(MessageIntelligenceAgencyImpl.class);
32
33     private static final class MessageCounters {
34         private static final AtomicLongFieldUpdater<MessageCounters> UPDATER =
35                 AtomicLongFieldUpdater.newUpdater(MessageCounters.class, "current");
36         @SuppressWarnings("unused")
37         private volatile long current;
38         private long cumulative;
39
40         public synchronized long accumulate() {
41             final long inc = UPDATER.getAndSet(this, 0);
42             cumulative += inc;
43             return inc;
44         }
45
46         public synchronized long getCumulative() {
47             return cumulative;
48         }
49
50         public long increment() {
51             return UPDATER.incrementAndGet(this);
52         }
53     }
54
55     private ConcurrentMap<StatisticsGroup, ConcurrentMap<Class<?>, MessageCounters>> inputStats =
56             new ConcurrentHashMap<>();
57
58     @Override
59     public void spyMessage(@Nonnull final Class<?> message, final StatisticsGroup statGroup) {
60         Preconditions.checkNotNull(message, "Message can't be null.");
61         getCounters(message, statGroup).increment();
62     }
63
64     /**
65      * Get counters.
66      * @param message counted element
67      * @param statGroup statistic counter group
68      * @return corresponding counter
69      */
70     private MessageCounters getCounters(final Class<?> message, final StatisticsGroup statGroup) {
71         ConcurrentMap<Class<?>, MessageCounters> groupData = getOrCreateGroupData(statGroup);
72         MessageCounters counters = getOrCreateCountersPair(message, groupData);
73         return counters;
74     }
75
76     private static MessageCounters getOrCreateCountersPair(final Class<?> msgType,
77                                                            final ConcurrentMap<Class<?>, MessageCounters> groupData) {
78         final MessageCounters lookup = groupData.get(msgType);
79         if (lookup != null) {
80             return lookup;
81         }
82
83         final MessageCounters newCounters = new MessageCounters();
84         final MessageCounters check = groupData.putIfAbsent(msgType, newCounters);
85         return check == null ? newCounters : check;
86
87     }
88
89     private ConcurrentMap<Class<?>, MessageCounters> getOrCreateGroupData(final StatisticsGroup statGroup) {
90         final ConcurrentMap<Class<?>, MessageCounters> lookup = inputStats.get(statGroup);
91         if (lookup != null) {
92             return lookup;
93         }
94
95         final ConcurrentMap<Class<?>, MessageCounters> newmap = new ConcurrentHashMap<>();
96         final ConcurrentMap<Class<?>, MessageCounters> check = inputStats.putIfAbsent(statGroup, newmap);
97
98         return check == null ? newmap : check;
99     }
100
101     @Override
102     public void run() {
103         // log current counters and cleans it
104         if (LOG.isDebugEnabled()) {
105             for (String counterItem : provideIntelligence()) {
106                 LOG.debug("Counter: {}", counterItem);
107             }
108         }
109     }
110
111     @Override
112     public List<String> provideIntelligence() {
113         List<String> dump = new ArrayList<>();
114
115         for (StatisticsGroup statGroup : StatisticsGroup.values()) {
116             Map<Class<?>, MessageCounters> groupData = inputStats.get(statGroup);
117             if (groupData != null) {
118                 for (Entry<Class<?>, MessageCounters> statEntry : groupData.entrySet()) {
119                     long amountPerInterval = statEntry.getValue().accumulate();
120                     long cumulativeAmount = statEntry.getValue().getCumulative();
121                     dump.add(String.format("%s: MSG[%s] -> +%d | %d",
122                             statGroup,
123                             statEntry.getKey().getSimpleName(),
124                             amountPerInterval, cumulativeAmount));
125                 }
126             } else {
127                 dump.add(String.format("%s: no activity detected", statGroup));
128             }
129         }
130         return dump;
131     }
132
133     @Override
134     public void resetStatistics() {
135         inputStats = new ConcurrentHashMap<>();
136     }
137 }