2 * Copyright (c) 2013 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.openflow.md.queue;
11 import java.util.ArrayList;
12 import java.util.List;
14 import java.util.Map.Entry;
15 import java.util.concurrent.ConcurrentHashMap;
16 import java.util.concurrent.atomic.AtomicLong;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;
19 import org.opendaylight.yangtools.yang.binding.DataContainer;
20 import org.opendaylight.yangtools.yang.binding.DataObject;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
25 * message counter (by type)
27 public class MessageSpyCounterImpl implements MessageObservatory<OfHeader, DataObject> {
29 private static final Logger LOG = LoggerFactory
30 .getLogger(MessageSpyCounterImpl.class);
32 private Map<Class<? extends DataContainer>, AtomicLong[]> inputStats = new ConcurrentHashMap<>();
35 public void spyIn(OfHeader message) {
36 Class<? extends DataContainer> msgType = message.getImplementedInterface();
38 synchronized(msgType) {
39 AtomicLong[] counters = inputStats.get(msgType);
40 if (counters == null) {
41 counters = new AtomicLong[] {new AtomicLong(), new AtomicLong()};
42 inputStats.put(msgType, counters);
44 counter = counters[0];
46 counter.incrementAndGet();
50 public void spyOut(List<DataObject> message) {
56 // log current counters and cleans it
57 if (LOG.isDebugEnabled()) {
58 for (String counterItem : dumpMessageCounts()) {
59 LOG.debug(counterItem);
65 public List<String> dumpMessageCounts() {
66 List<String> dump = new ArrayList<>();
67 for (Entry<Class<? extends DataContainer>, AtomicLong[]> statEntry : inputStats.entrySet()) {
68 long amountPerInterval = statEntry.getValue()[0].getAndSet(0);
69 long cumulativeAmount = statEntry.getValue()[1].addAndGet(amountPerInterval);
70 dump.add(String.format("MSG[%s] -> +%d | %d",
71 statEntry.getKey().getSimpleName(), amountPerInterval, cumulativeAmount));