Added the option for user-confugrable number of data tree change listeners that liste...
[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
9 package org.opendaylight.dsbenchmark.listener;
10
11 import java.util.ArrayList;
12 import java.util.List;
13
14 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
15 import org.opendaylight.controller.md.sal.binding.api.DataTreeIdentifier;
16 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.dsbenchmark.rev150105.TestExec;
18 import org.opendaylight.yangtools.concepts.ListenerRegistration;
19 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
22
23 public class DsbenchmarkListenerProvider {
24     private static final Logger LOG = LoggerFactory.getLogger(DsbenchmarkListenerProvider.class);
25     private static final InstanceIdentifier<TestExec> TEST_EXEC_IID = InstanceIdentifier.builder(TestExec.class).build();
26     private final List<ListenerRegistration<DsbenchmarkListener>> listeners = 
27             new ArrayList<ListenerRegistration<DsbenchmarkListener>>();
28     private DataBroker dataBroker;
29
30     public void setDataBroker(DataBroker dataBroker) {
31         this.dataBroker = dataBroker;
32         LOG.info("DsbenchmarkListenerProvider created");
33     }
34
35     public void createAndRegisterListeners(int numListeners) {
36         for(int i = 0; i < numListeners; i++) {
37             DsbenchmarkListener listener = new DsbenchmarkListener();
38             listeners.add(dataBroker.registerDataTreeChangeListener(
39                     new DataTreeIdentifier<TestExec>(LogicalDatastoreType.CONFIGURATION, TEST_EXEC_IID), listener));
40             listeners.add(dataBroker.registerDataTreeChangeListener(
41                     new DataTreeIdentifier<TestExec>(LogicalDatastoreType.OPERATIONAL, TEST_EXEC_IID), listener));
42
43         }
44         LOG.info("DsbenchmarkListenerProvider created {} listeneres", numListeners);
45     }
46
47     public long getEventCountAndDestroyListeners() {
48         long totalEvents = 0;
49
50         for (ListenerRegistration<DsbenchmarkListener> listenerRegistration : listeners) {
51             totalEvents = totalEvents + listenerRegistration.getInstance().getNumEvents();
52             listenerRegistration.close();
53         }
54
55         listeners.clear();
56         totalEvents = totalEvents / 2;  // Each listener is registered 2x: adjust for event double-counting
57         LOG.info("DsbenchmarkListenerProvider destroyed listeneres, total events {}", totalEvents);
58         return totalEvents;
59     }
60 }