Migrate to MD-SAL APIs
[bgpcep.git] / testtool-util / src / main / java / org / opendaylight / protocol / util / CheckUtil.java
1 /*
2  * Copyright (c) 2016 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.protocol.util;
9
10 import static com.google.common.base.Verify.verify;
11 import static org.opendaylight.mdsal.common.api.LogicalDatastoreType.CONFIGURATION;
12 import static org.opendaylight.mdsal.common.api.LogicalDatastoreType.OPERATIONAL;
13
14 import com.google.common.annotations.VisibleForTesting;
15 import com.google.common.base.Stopwatch;
16 import com.google.common.util.concurrent.Uninterruptibles;
17 import io.netty.util.concurrent.Future;
18 import java.util.Optional;
19 import java.util.concurrent.CountDownLatch;
20 import java.util.concurrent.ExecutionException;
21 import java.util.concurrent.TimeUnit;
22 import java.util.function.Function;
23 import org.opendaylight.mdsal.binding.api.DataBroker;
24 import org.opendaylight.mdsal.binding.api.ReadTransaction;
25 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
26 import org.opendaylight.yangtools.yang.binding.DataObject;
27 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
28
29 public final class CheckUtil {
30     private static final int SLEEP_FOR = 200;
31     private static final int TIMEOUT = 30;
32
33     private CheckUtil() {
34         throw new UnsupportedOperationException();
35     }
36
37     public static <T extends Future<?>> void waitFutureSuccess(final T future) {
38         waitFutureSuccess(future, SLEEP_FOR, TimeUnit.SECONDS);
39     }
40
41     @VisibleForTesting
42     static <T extends Future<?>> void waitFutureSuccess(final T future, final long timeout, final TimeUnit unit) {
43         final CountDownLatch latch = new CountDownLatch(1);
44         future.addListener(future1 -> latch.countDown());
45         Uninterruptibles.awaitUninterruptibly(latch, timeout, unit);
46         verify(future.isSuccess());
47     }
48
49     public static <R, T extends DataObject> R readDataOperational(final DataBroker dataBroker,
50             final InstanceIdentifier<T> iid, final Function<T, R> function) throws InterruptedException,
51                 ExecutionException {
52         return readDataOperational(dataBroker, iid, function, TIMEOUT);
53     }
54
55     @VisibleForTesting
56     static <R, T extends DataObject> R readDataOperational(final DataBroker dataBroker,
57             final InstanceIdentifier<T> iid, final Function<T, R> function, final int timeout)
58             throws InterruptedException, ExecutionException {
59         return readData(dataBroker, OPERATIONAL, iid, function, timeout);
60     }
61
62     public static <R, T extends DataObject> R readDataConfiguration(final DataBroker dataBroker,
63             final InstanceIdentifier<T> iid, final Function<T, R> function) throws InterruptedException,
64                 ExecutionException {
65         return readDataConfiguration(dataBroker, iid, function, TIMEOUT);
66     }
67
68     @VisibleForTesting
69     static <R, T extends DataObject> R readDataConfiguration(final DataBroker dataBroker,
70             final InstanceIdentifier<T> iid, final Function<T, R> function, final int timeout)
71             throws InterruptedException, ExecutionException {
72         return readData(dataBroker, CONFIGURATION, iid, function, timeout);
73     }
74
75     private static <R, T extends DataObject> R readData(final DataBroker dataBroker, final LogicalDatastoreType ldt,
76             final InstanceIdentifier<T> iid, final Function<T, R> function, final int timeout)
77             throws InterruptedException, ExecutionException {
78         AssertionError lastError = null;
79         final Stopwatch sw = Stopwatch.createStarted();
80         do {
81             try (ReadTransaction tx = dataBroker.newReadOnlyTransaction()) {
82                 final Optional<T> data = tx.read(ldt, iid).get();
83                 if (data.isPresent()) {
84                     try {
85                         return function.apply(data.get());
86                     } catch (final AssertionError e) {
87                         lastError = e;
88                         Uninterruptibles.sleepUninterruptibly(SLEEP_FOR, TimeUnit.MILLISECONDS);
89                     }
90                 }
91             }
92         } while (sw.elapsed(TimeUnit.SECONDS) <= timeout);
93         throw lastError;
94     }
95
96     public static <T extends DataObject> T checkPresentOperational(final DataBroker dataBroker,
97             final InstanceIdentifier<T> iid) throws InterruptedException, ExecutionException {
98         return readData(dataBroker, OPERATIONAL, iid, bgpRib -> bgpRib, TIMEOUT);
99     }
100
101     public static <T extends DataObject> T checkPresentConfiguration(final DataBroker dataBroker,
102             final InstanceIdentifier<T> iid) throws InterruptedException, ExecutionException {
103         return readData(dataBroker, CONFIGURATION, iid, bgpRib -> bgpRib, TIMEOUT);
104     }
105
106     public static <T extends DataObject> void checkNotPresentOperational(final DataBroker dataBroker,
107             final InstanceIdentifier<T> iid) throws InterruptedException, ExecutionException {
108         checkNotPresent(dataBroker, OPERATIONAL, iid);
109     }
110
111     public static <T extends DataObject> void checkNotPresentConfiguration(final DataBroker dataBroker,
112             final InstanceIdentifier<T> iid) throws InterruptedException, ExecutionException {
113         checkNotPresent(dataBroker, CONFIGURATION, iid);
114     }
115
116     private static <T extends DataObject> void checkNotPresent(final DataBroker dataBroker,
117             final LogicalDatastoreType ldt, final InstanceIdentifier<T> iid) throws InterruptedException,
118                 ExecutionException {
119         AssertionError lastError = null;
120         final Stopwatch sw = Stopwatch.createStarted();
121         while (sw.elapsed(TimeUnit.SECONDS) <= 10) {
122             try (ReadTransaction tx = dataBroker.newReadOnlyTransaction()) {
123                 final Optional<T> data = tx.read(ldt, iid).get();
124                 try {
125                     assert !data.isPresent();
126                     return;
127                 } catch (final AssertionError e) {
128                     lastError = e;
129                     Uninterruptibles.sleepUninterruptibly(10, TimeUnit.MILLISECONDS);
130                 }
131             }
132         }
133         throw lastError;
134     }
135
136     public static void checkEquals(final CheckEquals function) throws Exception {
137         checkEquals(function, TIMEOUT);
138     }
139
140     public static void checkEquals(final CheckEquals function, final int timeout) throws Exception {
141         AssertionError lastError = null;
142         final Stopwatch sw = Stopwatch.createStarted();
143         while (sw.elapsed(TimeUnit.SECONDS) <= timeout) {
144             try {
145                 function.check();
146                 return;
147             } catch (final AssertionError e) {
148                 lastError = e;
149                 Uninterruptibles.sleepUninterruptibly(10, TimeUnit.MILLISECONDS);
150             }
151         }
152         throw lastError;
153     }
154
155     public static void checkReceivedMessages(final ListenerCheck listener, final int numberOfMessages) {
156         checkReceivedMessages(listener, numberOfMessages, TIMEOUT);
157     }
158
159     @VisibleForTesting
160     static void checkReceivedMessages(final ListenerCheck listener, final int numberOfMessages,
161             final int timeout) {
162         final Stopwatch sw = Stopwatch.createStarted();
163         while (sw.elapsed(TimeUnit.SECONDS) <= timeout) {
164             if (listener.getListMessageSize() != numberOfMessages) {
165                 Uninterruptibles.sleepUninterruptibly(SLEEP_FOR, TimeUnit.MILLISECONDS);
166             } else {
167                 return;
168             }
169         }
170         throw new AssertionError("Expected " + numberOfMessages + " but received "
171                 + listener.getListMessageSize());
172     }
173
174     public interface ListenerCheck {
175         int getListMessageSize();
176     }
177
178     @FunctionalInterface
179     public interface CheckEquals {
180         void check() throws ExecutionException, InterruptedException;
181     }
182 }