fcd2c1110b278701f59348fba511df4b10374d3c
[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
9 package org.opendaylight.dsbenchmark.listener;
10
11 import java.util.Collection;
12 import java.util.concurrent.atomic.AtomicInteger;
13
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;
23
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);
28
29     @Override
30     public void onDataTreeChanged(
31             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.
35
36         final int eventNum = numEvents.incrementAndGet();
37         numDataChanges.addAndGet(changes.size());
38
39         if (LOG.isDebugEnabled()) {
40             logDataTreeChangeEvent(eventNum, changes);
41         }
42     }
43
44     private static synchronized void logDataTreeChangeEvent(int eventNum,
45             Collection<DataTreeModification<TestExec>> changes) {
46         LOG.debug("DsbenchmarkListener-onDataTreeChanged: Event {}", eventNum);
47
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();
54
55             LOG.debug("    changeId {}, modType {}, mods: {}", changeId, modType, modifications.size());
56
57             for (DataObjectModification<? extends DataObject> mod : modifications) {
58                 LOG.debug("      mod-getDataAfter: {}", mod.getDataAfter());
59             }
60         }
61     }
62
63     public int getNumEvents() {
64         return numEvents.get();
65     }
66
67     public int getNumDataChanges() {
68         return numDataChanges.get();
69     }
70 }