e5898183603cb0550aa28c8eb90ffbfe9431e46b
[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 =
26             InstanceIdentifier.builder(TestExec.class).build();
27     private final List<ListenerRegistration<DsbenchmarkListener>> listeners =
28             new ArrayList<>();
29     private DataBroker dataBroker;
30
31     public void setDataBroker(DataBroker dataBroker) {
32         this.dataBroker = dataBroker;
33         LOG.info("DsbenchmarkListenerProvider created");
34     }
35
36     public void createAndRegisterListeners(int numListeners) {
37         for (int i = 0; i < numListeners; i++) {
38             DsbenchmarkListener listener = new DsbenchmarkListener();
39             listeners.add(dataBroker.registerDataTreeChangeListener(
40                     new DataTreeIdentifier<>(LogicalDatastoreType.CONFIGURATION, TEST_EXEC_IID), listener));
41             listeners.add(dataBroker.registerDataTreeChangeListener(
42                     new DataTreeIdentifier<>(LogicalDatastoreType.OPERATIONAL, TEST_EXEC_IID), listener));
43
44         }
45         LOG.info("DsbenchmarkListenerProvider created {} listeneres", numListeners);
46     }
47
48     public long getDataChangeCount() {
49         long dataChanges = 0;
50
51         for (ListenerRegistration<DsbenchmarkListener> listenerRegistration : listeners) {
52             dataChanges += listenerRegistration.getInstance().getNumDataChanges();
53         }
54         LOG.info("DsbenchmarkListenerProvider , total data changes {}", dataChanges);
55         return dataChanges;
56     }
57
58     public long getEventCountAndDestroyListeners() {
59         long totalEvents = 0;
60
61         for (ListenerRegistration<DsbenchmarkListener> listenerRegistration : listeners) {
62             totalEvents += listenerRegistration.getInstance().getNumEvents();
63             listenerRegistration.close();
64         }
65         listeners.clear();
66         LOG.info("DsbenchmarkListenerProvider destroyed listeneres, total events {}", totalEvents);
67         return totalEvents;
68     }
69 }

©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.