2 * Copyright (c) 2016 Cisco Systems 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.dsbenchmark.listener;
11 import java.util.Collection;
12 import java.util.concurrent.atomic.AtomicInteger;
14 import org.opendaylight.controller.md.sal.binding.api.DataObjectModification;
15 import org.opendaylight.controller.md.sal.binding.api.DataObjectModification.ModificationType;
16 import org.opendaylight.controller.md.sal.binding.api.DataTreeChangeListener;
17 import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.dsbenchmark.rev150105.TestExec;
19 import org.opendaylight.yangtools.yang.binding.DataObject;
20 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier.PathArgument;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
24 public class DsbenchmarkListener implements DataTreeChangeListener<TestExec> {
25 private static final Logger LOG = LoggerFactory.getLogger(DsbenchmarkListener.class);
26 private AtomicInteger numEvents = new AtomicInteger(0);
27 private AtomicInteger numDataChanges = new AtomicInteger(0);
30 public void onDataTreeChanged(
31 final Collection<DataTreeModification<TestExec>> changes) {
32 // Since we're registering the same DsbenchmarkListener object for both
33 // OPERATIONAL and CONFIG, the onDataTreeChanged() method can be called
34 // from different threads, and we need to use atomic counters.
36 final int eventNum = numEvents.incrementAndGet();
37 numDataChanges.addAndGet(changes.size());
39 if (LOG.isDebugEnabled()) {
40 logDataTreeChangeEvent(eventNum, changes);
44 private static synchronized void logDataTreeChangeEvent(final int eventNum,
45 final Collection<DataTreeModification<TestExec>> changes) {
46 LOG.debug("DsbenchmarkListener-onDataTreeChanged: Event {}", eventNum);
48 for (DataTreeModification<TestExec> change : changes) {
49 final DataObjectModification<TestExec> rootNode = change.getRootNode();
50 final ModificationType modType = rootNode.getModificationType();
51 final PathArgument changeId = rootNode.getIdentifier();
52 final Collection<DataObjectModification<? extends DataObject>> modifications =
53 rootNode.getModifiedChildren();
55 LOG.debug(" changeId {}, modType {}, mods: {}", changeId, modType, modifications.size());
57 for (DataObjectModification<? extends DataObject> mod : modifications) {
58 LOG.debug(" mod-getDataAfter: {}", mod.getDataAfter());
63 public int getNumEvents() {
64 return numEvents.get();
67 public int getNumDataChanges() {
68 return numDataChanges.get();