Merge "Fixed for bug : 1171 - issue while creating subnet"
[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 java.util.concurrent.ExecutionException;
11 import java.util.concurrent.TimeUnit;
12 import java.util.concurrent.TimeoutException;
13
14 import org.opendaylight.controller.md.sal.binding.api.DataChangeListener;
15 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker.DataChangeScope;
16 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeEvent;
17 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
18 import org.opendaylight.yangtools.yang.binding.DataObject;
19 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
20
21 import com.google.common.util.concurrent.SettableFuture;
22
23 public abstract class AbstractDataChangeListenerTest extends AbstractDataBrokerTest {
24
25     protected static final class TestListener implements DataChangeListener {
26
27         private final SettableFuture<AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject>> event;
28         private boolean capture = false;
29
30         private TestListener() {
31             event = SettableFuture.create();
32         }
33
34         @Override
35         public void onDataChanged(final AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> arg) {
36             if (capture) {
37                 event.set(arg);
38             }
39         }
40
41         public AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> event() {
42             try {
43                 return event.get(500, TimeUnit.MILLISECONDS);
44             } catch (InterruptedException | TimeoutException | ExecutionException e) {
45                 throw new IllegalStateException(e);
46             }
47         }
48
49         public boolean hasEvent() {
50             return event.isDone();
51         }
52
53         public void startCapture() {
54             this.capture = true;
55         }
56     }
57
58     protected final TestListener createListener(final LogicalDatastoreType store, final InstanceIdentifier<?> path,
59             final DataChangeScope scope) {
60         TestListener listener = new TestListener();
61         getDataBroker().registerDataChangeListener(store, path, listener, scope);
62         listener.startCapture();
63         return listener;
64     }
65 }