2 * Copyright (c) 2016 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.protocol.util;
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;
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;
29 public final class CheckUtil {
30 private static final int SLEEP_FOR = 200;
31 private static final int TIMEOUT = 30;
34 throw new UnsupportedOperationException();
37 public static <T extends Future<?>> void waitFutureSuccess(final T future) {
38 waitFutureSuccess(future, SLEEP_FOR, TimeUnit.SECONDS);
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());
49 public static <R, T extends DataObject> R readDataOperational(final DataBroker dataBroker,
50 final InstanceIdentifier<T> iid, final Function<T, R> function) throws InterruptedException,
52 return readDataOperational(dataBroker, iid, function, TIMEOUT);
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);
62 public static <R, T extends DataObject> R readDataConfiguration(final DataBroker dataBroker,
63 final InstanceIdentifier<T> iid, final Function<T, R> function) throws InterruptedException,
65 return readDataConfiguration(dataBroker, iid, function, TIMEOUT);
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);
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();
81 try (ReadTransaction tx = dataBroker.newReadOnlyTransaction()) {
82 final Optional<T> data = tx.read(ldt, iid).get();
83 if (data.isPresent()) {
85 return function.apply(data.get());
86 } catch (final AssertionError e) {
88 Uninterruptibles.sleepUninterruptibly(SLEEP_FOR, TimeUnit.MILLISECONDS);
92 } while (sw.elapsed(TimeUnit.SECONDS) <= timeout);
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);
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);
106 public static <T extends DataObject> void checkNotPresentOperational(final DataBroker dataBroker,
107 final InstanceIdentifier<T> iid) throws InterruptedException, ExecutionException {
108 checkNotPresent(dataBroker, OPERATIONAL, iid);
111 public static <T extends DataObject> void checkNotPresentConfiguration(final DataBroker dataBroker,
112 final InstanceIdentifier<T> iid) throws InterruptedException, ExecutionException {
113 checkNotPresent(dataBroker, CONFIGURATION, iid);
116 private static <T extends DataObject> void checkNotPresent(final DataBroker dataBroker,
117 final LogicalDatastoreType ldt, final InstanceIdentifier<T> iid) throws InterruptedException,
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();
125 assert !data.isPresent();
127 } catch (final AssertionError e) {
129 Uninterruptibles.sleepUninterruptibly(10, TimeUnit.MILLISECONDS);
136 public static void checkEquals(final CheckEquals function) throws Exception {
137 checkEquals(function, TIMEOUT);
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) {
147 } catch (final AssertionError e) {
149 Uninterruptibles.sleepUninterruptibly(10, TimeUnit.MILLISECONDS);
155 public static void checkReceivedMessages(final ListenerCheck listener, final int numberOfMessages) {
156 checkReceivedMessages(listener, numberOfMessages, TIMEOUT);
160 static void checkReceivedMessages(final ListenerCheck listener, final int numberOfMessages,
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);
170 throw new AssertionError("Expected " + numberOfMessages + " but received "
171 + listener.getListMessageSize());
174 public interface ListenerCheck {
175 int getListMessageSize();
179 public interface CheckEquals {
180 void check() throws ExecutionException, InterruptedException;