2 * Copyright (c) 2014 Cisco Systems, Inc. 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
8 package org.opendaylight.controller.md.sal.binding.test;
10 import static org.junit.Assert.assertTrue;
12 import com.google.common.util.concurrent.SettableFuture;
13 import java.util.concurrent.CountDownLatch;
14 import java.util.concurrent.ExecutionException;
15 import java.util.concurrent.TimeUnit;
16 import java.util.concurrent.TimeoutException;
17 import org.opendaylight.controller.md.sal.binding.api.DataChangeListener;
18 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker.DataChangeScope;
19 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeEvent;
20 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
21 import org.opendaylight.yangtools.yang.binding.DataObject;
22 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
24 public abstract class AbstractDataChangeListenerTest extends AbstractConcurrentDataBrokerTest {
26 protected static final class TestListener implements DataChangeListener {
28 private final SettableFuture<AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject>> event;
29 private final CountDownLatch initialEventLatch;
30 private volatile boolean capture = false;
32 private TestListener(boolean expectInitialEvent) {
33 event = SettableFuture.create();
34 initialEventLatch = new CountDownLatch(expectInitialEvent ? 1 : 0);
38 public void onDataChanged(final AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> arg) {
42 initialEventLatch.countDown();
46 public AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> event() {
48 return event.get(500, TimeUnit.MILLISECONDS);
49 } catch (InterruptedException | TimeoutException | ExecutionException e) {
50 throw new IllegalStateException(e);
54 public boolean hasEvent() {
55 return event.isDone();
58 private void waitForInitialEvent() {
60 assertTrue("Initial DataChangeEvent was not received", initialEventLatch.await(3, TimeUnit.SECONDS));
61 } catch (InterruptedException e) {
62 throw new IllegalStateException(e);
69 protected AbstractDataChangeListenerTest() {
73 protected final TestListener createListener(final LogicalDatastoreType store, final InstanceIdentifier<?> path,
74 final DataChangeScope scope) {
75 return createListener(store, path, scope, true);
78 protected final TestListener createListener(final LogicalDatastoreType store, final InstanceIdentifier<?> path,
79 final DataChangeScope scope, boolean expectInitialEvent) {
80 TestListener listener = new TestListener(expectInitialEvent);
81 getDataBroker().registerDataChangeListener(store, path, listener, scope);
82 listener.waitForInitialEvent();