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