check for manadatory attribute
[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<Class<?>>, 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         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 final ConcurrentMap<STATISTIC_GROUP, ConcurrentMap<Class<?>, MessageCounters>> inputStats = new ConcurrentHashMap<>();
54
55     @Override
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();
59     }
60
61     /**
62      * @param message
63      * @param statGroup TODO
64      * @return
65      */
66     private MessageCounters getCounters(final Class<?> message, final STATISTIC_GROUP statGroup) {
67         ConcurrentMap<Class<?>, MessageCounters> groupData = getOrCreateGroupData(statGroup);
68         MessageCounters counters = getOrCreateCountersPair(message, groupData);
69         return counters;
70     }
71
72     private static MessageCounters getOrCreateCountersPair(final Class<?> msgType, final ConcurrentMap<Class<?>, MessageCounters> groupData) {
73         final MessageCounters lookup = groupData.get(msgType);
74         if (lookup != null) {
75             return lookup;
76         }
77
78         final MessageCounters newCounters = new MessageCounters();
79         final MessageCounters check = groupData.putIfAbsent(msgType, newCounters);
80         return check == null ? newCounters : check;
81
82     }
83
84     private ConcurrentMap<Class<?>, MessageCounters> getOrCreateGroupData(final STATISTIC_GROUP statGroup) {
85         final ConcurrentMap<Class<?>, MessageCounters> lookup = inputStats.get(statGroup);
86         if (lookup != null) {
87             return lookup;
88         }
89
90         final ConcurrentMap<Class<?>, MessageCounters> newmap = new ConcurrentHashMap<>();
91         final ConcurrentMap<Class<?>, MessageCounters> check = inputStats.putIfAbsent(statGroup, newmap);
92
93         return check == null ? newmap : check;
94     }
95
96     @Override
97     public void run() {
98         // log current counters and cleans it
99         if (LOG.isDebugEnabled()) {
100             for (String counterItem : provideIntelligence()) {
101                 LOG.debug(counterItem);
102             }
103         }
104     }
105
106     @Override
107     public List<String> provideIntelligence() {
108         List<String> dump = new ArrayList<>();
109
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",
117                             statGroup,
118                             statEntry.getKey().getSimpleName(),
119                             amountPerInterval, cumulativeAmount));
120                 }
121             } else {
122                 dump.add(String.format("%s: no activity detected", statGroup));
123             }
124         }
125         return dump;
126     }
127 }