2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.openflowplugin.impl.statistics.ofpspecific;
10 import static java.util.Objects.requireNonNull;
12 import java.util.ArrayList;
13 import java.util.List;
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;
25 * {@link org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.MessageIntelligenceAgency}.
26 * Class counts message of {@link StatisticsGroup} type and provides info as debug log.
28 public class MessageIntelligenceAgencyImpl implements MessageIntelligenceAgency, MessageIntelligenceAgencyMXBean {
29 private static final Logger LOG = LoggerFactory.getLogger(MessageIntelligenceAgencyImpl.class);
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;
38 public synchronized long accumulate() {
39 final long inc = UPDATER.getAndSet(this, 0);
44 public synchronized long getCumulative() {
48 public long increment() {
49 return UPDATER.incrementAndGet(this);
53 private ConcurrentMap<StatisticsGroup, ConcurrentMap<Class<?>, MessageCounters>> inputStats =
54 new ConcurrentHashMap<>();
57 public void spyMessage(final Class<?> message, final StatisticsGroup statGroup) {
58 requireNonNull(message, "Message can't be null.");
59 getCounters(message, statGroup).increment();
64 * @param message counted element
65 * @param statGroup statistic counter group
66 * @return corresponding counter
68 private MessageCounters getCounters(final Class<?> message, final StatisticsGroup statGroup) {
69 ConcurrentMap<Class<?>, MessageCounters> groupData = getOrCreateGroupData(statGroup);
70 MessageCounters counters = getOrCreateCountersPair(message, groupData);
74 private static MessageCounters getOrCreateCountersPair(final Class<?> msgType,
75 final ConcurrentMap<Class<?>, MessageCounters> groupData) {
76 final MessageCounters lookup = groupData.get(msgType);
81 final MessageCounters newCounters = new MessageCounters();
82 final MessageCounters check = groupData.putIfAbsent(msgType, newCounters);
83 return check == null ? newCounters : check;
87 private ConcurrentMap<Class<?>, MessageCounters> getOrCreateGroupData(final StatisticsGroup statGroup) {
88 final ConcurrentMap<Class<?>, MessageCounters> lookup = inputStats.get(statGroup);
93 final ConcurrentMap<Class<?>, MessageCounters> newmap = new ConcurrentHashMap<>();
94 final ConcurrentMap<Class<?>, MessageCounters> check = inputStats.putIfAbsent(statGroup, newmap);
96 return check == null ? newmap : check;
101 // log current counters and cleans it
102 if (LOG.isDebugEnabled()) {
103 for (String counterItem : provideIntelligence()) {
104 LOG.debug("Counter: {}", counterItem);
110 public List<String> provideIntelligence() {
111 List<String> dump = new ArrayList<>();
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",
121 statEntry.getKey().getSimpleName(),
122 amountPerInterval, cumulativeAmount));
125 dump.add(String.format("%s: no activity detected", statGroup));
132 public void resetStatistics() {
133 inputStats = new ConcurrentHashMap<>();