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.ArrayList;
12 import java.util.List;
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;
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;
30 public void setDataBroker(DataBroker dataBroker) {
31 this.dataBroker = dataBroker;
32 LOG.info("DsbenchmarkListenerProvider created");
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));
44 LOG.info("DsbenchmarkListenerProvider created {} listeneres", numListeners);
47 public long getEventCountAndDestroyListeners() {
50 for (ListenerRegistration<DsbenchmarkListener> listenerRegistration : listeners) {
51 totalEvents = totalEvents + listenerRegistration.getInstance().getNumEvents();
52 listenerRegistration.close();
56 totalEvents = totalEvents / 2; // Each listener is registered 2x: adjust for event double-counting
57 LOG.info("DsbenchmarkListenerProvider destroyed listeneres, total events {}", totalEvents);