MessageSpy should not be generic
[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 {@link org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.MessageIntelligenceAgency}.
26  * Class counts message of {@link org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.MessageSpy.STATISTIC_GROUP} type
27  * 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 = 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 final ConcurrentMap<STATISTIC_GROUP, ConcurrentMap<Class<?>, MessageCounters>> inputStats = new ConcurrentHashMap<>();
55
56     @Override
57     public void spyMessage(@Nonnull final Class<?> message, final STATISTIC_GROUP statGroup) {
58         Preconditions.checkNotNull(message, "Message can't be null.");
59         getCounters(message, statGroup).increment();
60     }
61
62     /**
63      * @param message
64      * @param statGroup TODO
65      * @return
66      */
67     private MessageCounters getCounters(final Class<?> message, final STATISTIC_GROUP statGroup) {
68         ConcurrentMap<Class<?>, MessageCounters> groupData = getOrCreateGroupData(statGroup);
69         MessageCounters counters = getOrCreateCountersPair(message, groupData);
70         return counters;
71     }
72
73     private static MessageCounters getOrCreateCountersPair(final Class<?> msgType, final ConcurrentMap<Class<?>, MessageCounters> groupData) {
74         final MessageCounters lookup = groupData.get(msgType);
75         if (lookup != null) {
76             return lookup;
77         }
78
79         final MessageCounters newCounters = new MessageCounters();
80         final MessageCounters check = groupData.putIfAbsent(msgType, newCounters);
81         return check == null ? newCounters : check;
82
83     }
84
85     private ConcurrentMap<Class<?>, MessageCounters> getOrCreateGroupData(final STATISTIC_GROUP statGroup) {
86         final ConcurrentMap<Class<?>, MessageCounters> lookup = inputStats.get(statGroup);
87         if (lookup != null) {
88             return lookup;
89         }
90
91         final ConcurrentMap<Class<?>, MessageCounters> newmap = new ConcurrentHashMap<>();
92         final ConcurrentMap<Class<?>, MessageCounters> check = inputStats.putIfAbsent(statGroup, newmap);
93
94         return check == null ? newmap : check;
95     }
96
97     @Override
98     public void run() {
99         // log current counters and cleans it
100         if (LOG.isDebugEnabled()) {
101             for (String counterItem : provideIntelligence()) {
102                 LOG.debug(counterItem);
103             }
104         }
105     }
106
107     @Override
108     public List<String> provideIntelligence() {
109         List<String> dump = new ArrayList<>();
110
111         for (STATISTIC_GROUP statGroup : STATISTIC_GROUP.values()) {
112             Map<Class<?>, MessageCounters> groupData = inputStats.get(statGroup);
113             if (groupData != null) {
114                 for (Entry<Class<?>, MessageCounters> statEntry : groupData.entrySet()) {
115                     long amountPerInterval = statEntry.getValue().accumulate();
116                     long cumulativeAmount = statEntry.getValue().getCumulative();
117                     dump.add(String.format("%s: MSG[%s] -> +%d | %d",
118                             statGroup,
119                             statEntry.getKey().getSimpleName(),
120                             amountPerInterval, cumulativeAmount));
121                 }
122             } else {
123                 dump.add(String.format("%s: no activity detected", statGroup));
124             }
125         }
126         return dump;
127     }
128 }