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
9 package org.opendaylight.openflowplugin.impl.statistics.ofpspecific;
11 import com.google.common.base.Preconditions;
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.eclipse.jdt.annotation.NonNull;
20 import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.MessageIntelligenceAgency;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
26 * {@link org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.MessageIntelligenceAgency}.
27 * Class counts message of {@link StatisticsGroup} type and provides info as debug log.
29 public class MessageIntelligenceAgencyImpl implements MessageIntelligenceAgency, MessageIntelligenceAgencyMXBean {
31 private static final Logger LOG = LoggerFactory.getLogger(MessageIntelligenceAgencyImpl.class);
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;
40 public synchronized long accumulate() {
41 final long inc = UPDATER.getAndSet(this, 0);
46 public synchronized long getCumulative() {
50 public long increment() {
51 return UPDATER.incrementAndGet(this);
55 private ConcurrentMap<StatisticsGroup, ConcurrentMap<Class<?>, MessageCounters>> inputStats =
56 new ConcurrentHashMap<>();
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();
66 * @param message counted element
67 * @param statGroup statistic counter group
68 * @return corresponding counter
70 private MessageCounters getCounters(final Class<?> message, final StatisticsGroup statGroup) {
71 ConcurrentMap<Class<?>, MessageCounters> groupData = getOrCreateGroupData(statGroup);
72 MessageCounters counters = getOrCreateCountersPair(message, groupData);
76 private static MessageCounters getOrCreateCountersPair(final Class<?> msgType,
77 final ConcurrentMap<Class<?>, MessageCounters> groupData) {
78 final MessageCounters lookup = groupData.get(msgType);
83 final MessageCounters newCounters = new MessageCounters();
84 final MessageCounters check = groupData.putIfAbsent(msgType, newCounters);
85 return check == null ? newCounters : check;
89 private ConcurrentMap<Class<?>, MessageCounters> getOrCreateGroupData(final StatisticsGroup statGroup) {
90 final ConcurrentMap<Class<?>, MessageCounters> lookup = inputStats.get(statGroup);
95 final ConcurrentMap<Class<?>, MessageCounters> newmap = new ConcurrentHashMap<>();
96 final ConcurrentMap<Class<?>, MessageCounters> check = inputStats.putIfAbsent(statGroup, newmap);
98 return check == null ? newmap : check;
103 // log current counters and cleans it
104 if (LOG.isDebugEnabled()) {
105 for (String counterItem : provideIntelligence()) {
106 LOG.debug("Counter: {}", counterItem);
112 public List<String> provideIntelligence() {
113 List<String> dump = new ArrayList<>();
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",
123 statEntry.getKey().getSimpleName(),
124 amountPerInterval, cumulativeAmount));
127 dump.add(String.format("%s: no activity detected", statGroup));
134 public void resetStatistics() {
135 inputStats = new ConcurrentHashMap<>();