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.List;
13 import java.util.Map.Entry;
14 import java.util.concurrent.ConcurrentHashMap;
15 import java.util.concurrent.atomic.AtomicLong;
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;
24 * message counter (by type)
26 public class MessageSpyCounterImpl implements MessageSpy<OfHeader, DataObject> {
28 private static final Logger LOG = LoggerFactory
29 .getLogger(MessageSpyCounterImpl.class);
31 private Map<Class<? extends DataContainer>, AtomicLong[]> inputStats = new ConcurrentHashMap<>();
34 public void spyIn(OfHeader message) {
35 Class<? extends DataContainer> msgType = message.getImplementedInterface();
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);
43 counter = counters[0];
45 counter.incrementAndGet();
49 public void spyOut(List<DataObject> message) {
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);