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