fe32b8c554715149a655046e9f50727b8d78d41b
[openflowplugin.git] / openflowplugin / src / main / java / org / opendaylight / openflowplugin / openflow / md / queue / MessageSpyCounterImpl.java
1 /**
2  * Copyright (c) 2013 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.openflow.md.queue;
10
11 import java.util.List;
12 import java.util.Map;
13 import java.util.Map.Entry;
14 import java.util.concurrent.ConcurrentHashMap;
15 import java.util.concurrent.atomic.AtomicLong;
16
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;
18 import org.opendaylight.yangtools.yang.binding.DataContainer;
19 import org.opendaylight.yangtools.yang.binding.DataObject;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
22
23 /**
24  * message counter (by type)
25  */
26 public class MessageSpyCounterImpl implements MessageSpy<OfHeader, DataObject> {
27     
28     private static final Logger LOG = LoggerFactory
29             .getLogger(MessageSpyCounterImpl.class);
30     
31     private Map<Class<? extends DataContainer>, AtomicLong[]> inputStats = new ConcurrentHashMap<>();
32     
33     @Override
34     public void spyIn(OfHeader message) {
35         Class<? extends DataContainer> msgType = message.getImplementedInterface();
36         AtomicLong counter;
37         synchronized(msgType) {
38             AtomicLong[] counters = inputStats.get(msgType);
39             if (counters == null) {
40                 counters = new AtomicLong[] {new AtomicLong(), new AtomicLong()};
41                 inputStats.put(msgType, counters);
42             } 
43             counter = counters[0];
44         }
45         counter.incrementAndGet();
46     }
47
48     @Override
49     public void spyOut(List<DataObject> message) {
50         // NOOP   
51     }
52
53     @Override
54     public void run() {
55         // log current counters and cleans it
56         if (LOG.isDebugEnabled()) {
57             for (Entry<Class<? extends DataContainer>, AtomicLong[]> statEntry : inputStats.entrySet()) {
58                 long amountPerInterval = statEntry.getValue()[0].getAndSet(0);
59                 long cumulativeAmount = statEntry.getValue()[1].addAndGet(amountPerInterval);
60                 LOG.debug("MSG[{}] -> +{} | {}", statEntry.getKey().getSimpleName(), amountPerInterval, cumulativeAmount);
61             }
62         }
63     }
64 }