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