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 javax.annotation.Nonnull;
20 import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.MessageIntelligenceAgency;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
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.
29 public class MessageIntelligenceAgencyImpl implements MessageIntelligenceAgency<Class<?>>, MessageIntelligenceAgencyMXBean {
31 private static final Logger LOG = LoggerFactory.getLogger(MessageIntelligenceAgencyImpl.class);
33 private static final class MessageCounters {
34 private static final AtomicLongFieldUpdater<MessageCounters> UPDATER = AtomicLongFieldUpdater.newUpdater(MessageCounters.class, "current");
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 final ConcurrentMap<STATISTIC_GROUP, ConcurrentMap<Class<?>, MessageCounters>> inputStats = new ConcurrentHashMap<>();
56 public void spyMessage(@Nonnull final Class<?> message, final STATISTIC_GROUP statGroup) {
57 Preconditions.checkNotNull(message,"Message can't be null.");
58 getCounters(message, statGroup).increment();
63 * @param statGroup TODO
66 private MessageCounters getCounters(final Class<?> message, final STATISTIC_GROUP statGroup) {
67 ConcurrentMap<Class<?>, MessageCounters> groupData = getOrCreateGroupData(statGroup);
68 MessageCounters counters = getOrCreateCountersPair(message, groupData);
72 private static MessageCounters getOrCreateCountersPair(final Class<?> msgType, final ConcurrentMap<Class<?>, MessageCounters> groupData) {
73 final MessageCounters lookup = groupData.get(msgType);
78 final MessageCounters newCounters = new MessageCounters();
79 final MessageCounters check = groupData.putIfAbsent(msgType, newCounters);
80 return check == null ? newCounters : check;
84 private ConcurrentMap<Class<?>, MessageCounters> getOrCreateGroupData(final STATISTIC_GROUP statGroup) {
85 final ConcurrentMap<Class<?>, MessageCounters> lookup = inputStats.get(statGroup);
90 final ConcurrentMap<Class<?>, MessageCounters> newmap = new ConcurrentHashMap<>();
91 final ConcurrentMap<Class<?>, MessageCounters> check = inputStats.putIfAbsent(statGroup, newmap);
93 return check == null ? newmap : check;
98 // log current counters and cleans it
99 if (LOG.isDebugEnabled()) {
100 for (String counterItem : provideIntelligence()) {
101 LOG.debug(counterItem);
107 public List<String> provideIntelligence() {
108 List<String> dump = new ArrayList<>();
110 for (STATISTIC_GROUP statGroup : STATISTIC_GROUP.values()) {
111 Map<Class<?>, MessageCounters> groupData = inputStats.get(statGroup);
112 if (groupData != null) {
113 for (Entry<Class<?>, MessageCounters> statEntry : groupData.entrySet()) {
114 long amountPerInterval = statEntry.getValue().accumulate();
115 long cumulativeAmount = statEntry.getValue().getCumulative();
116 dump.add(String.format("%s: MSG[%s] -> +%d | %d",
118 statEntry.getKey().getSimpleName(),
119 amountPerInterval, cumulativeAmount));
122 dump.add(String.format("%s: no activity detected", statGroup));