Convert dsbenchmark to mdsal APIs
[controller.git] / benchmark / dsbenchmark / src / main / java / org / opendaylight / dsbenchmark / listener / DsbenchmarkListenerProvider.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.ArrayList;
11 import java.util.List;
12 import org.opendaylight.mdsal.binding.api.DataBroker;
13 import org.opendaylight.mdsal.binding.api.DataTreeIdentifier;
14 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.dsbenchmark.rev150105.TestExec;
16 import org.opendaylight.yangtools.concepts.ListenerRegistration;
17 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
20
21 public class DsbenchmarkListenerProvider {
22     private static final Logger LOG = LoggerFactory.getLogger(DsbenchmarkListenerProvider.class);
23     private static final InstanceIdentifier<TestExec> TEST_EXEC_IID =
24             InstanceIdentifier.builder(TestExec.class).build();
25     private final List<ListenerRegistration<DsbenchmarkListener>> listeners =
26             new ArrayList<>();
27     private DataBroker dataBroker;
28
29     public void setDataBroker(final DataBroker dataBroker) {
30         this.dataBroker = dataBroker;
31         LOG.debug("DsbenchmarkListenerProvider created");
32     }
33
34     public void createAndRegisterListeners(final int numListeners) {
35         for (int i = 0; i < numListeners; i++) {
36             DsbenchmarkListener listener = new DsbenchmarkListener();
37             listeners.add(dataBroker.registerDataTreeChangeListener(
38                     DataTreeIdentifier.create(LogicalDatastoreType.CONFIGURATION, TEST_EXEC_IID), listener));
39             listeners.add(dataBroker.registerDataTreeChangeListener(
40                     DataTreeIdentifier.create(LogicalDatastoreType.OPERATIONAL, TEST_EXEC_IID), listener));
41
42         }
43         LOG.debug("DsbenchmarkListenerProvider created {} listeneres", numListeners);
44     }
45
46     public long getDataChangeCount() {
47         long dataChanges = 0;
48
49         for (ListenerRegistration<DsbenchmarkListener> listenerRegistration : listeners) {
50             dataChanges += listenerRegistration.getInstance().getNumDataChanges();
51         }
52         LOG.debug("DsbenchmarkListenerProvider , total data changes {}", dataChanges);
53         return dataChanges;
54     }
55
56     public long getEventCountAndDestroyListeners() {
57         long totalEvents = 0;
58
59         for (ListenerRegistration<DsbenchmarkListener> listenerRegistration : listeners) {
60             totalEvents += listenerRegistration.getInstance().getNumEvents();
61             listenerRegistration.close();
62         }
63         listeners.clear();
64         LOG.debug("DsbenchmarkListenerProvider destroyed listeneres, total events {}", totalEvents);
65         return totalEvents;
66     }
67 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.