2a5c0e8332819a567329bc703d765c365d9dd08a
[controller.git] / opendaylight / md-sal / sal-binding-broker / src / test / java / org / opendaylight / controller / md / sal / binding / test / AbstractDataChangeListenerTest.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. 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.controller.md.sal.binding.test;
9
10 import static org.junit.Assert.assertTrue;
11
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;
23
24 public abstract class AbstractDataChangeListenerTest extends AbstractConcurrentDataBrokerTest {
25
26     protected static final class TestListener implements DataChangeListener {
27
28         private final SettableFuture<AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject>> event;
29         private final CountDownLatch initialEventLatch;
30         private volatile boolean capture = false;
31
32         private TestListener(boolean expectInitialEvent) {
33             event = SettableFuture.create();
34             initialEventLatch = new CountDownLatch(expectInitialEvent ? 1 : 0);
35         }
36
37         @Override
38         public void onDataChanged(final AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> arg) {
39             if (capture) {
40                 event.set(arg);
41             } else {
42                 initialEventLatch.countDown();
43             }
44         }
45
46         public AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> event() {
47             try {
48                 return event.get(500, TimeUnit.MILLISECONDS);
49             } catch (InterruptedException | TimeoutException | ExecutionException e) {
50                 throw new IllegalStateException(e);
51             }
52         }
53
54         public boolean hasEvent() {
55             return event.isDone();
56         }
57
58         private void waitForInitialEvent() {
59             try {
60                 assertTrue("Initial DataChangeEvent was not received", initialEventLatch.await(3, TimeUnit.SECONDS));
61             } catch (InterruptedException e) {
62                 throw new IllegalStateException(e);
63             }
64
65             this.capture = true;
66         }
67     }
68
69     protected AbstractDataChangeListenerTest() {
70         super(true);
71     }
72
73     protected final TestListener createListener(final LogicalDatastoreType store, final InstanceIdentifier<?> path,
74             final DataChangeScope scope) {
75         return createListener(store, path, scope, true);
76     }
77
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();
83         return listener;
84     }
85 }