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 com.google.common.base.Optional;
11 import com.google.common.base.Stopwatch;
12 import com.google.common.base.Verify;
13 import com.google.common.util.concurrent.Uninterruptibles;
14 import io.netty.util.concurrent.Future;
15 import java.util.concurrent.CountDownLatch;
16 import java.util.concurrent.ExecutionException;
17 import java.util.concurrent.TimeUnit;
18 import java.util.function.Function;
19 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
20 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
21 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
22 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
23 import org.opendaylight.yangtools.yang.binding.DataObject;
24 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
26 public final class CheckUtil {
27 private static final int LATCH_TIMEOUT = 10;
28 private static final int SLEEP_FOR = 200;
29 private static final int TIMEOUT = 30;
31 throw new UnsupportedOperationException();
34 @SuppressWarnings("unchecked")
35 public static <T extends Future> void waitFutureSuccess(final T future) {
36 final CountDownLatch latch = new CountDownLatch(1);
37 future.addListener(future1 -> latch.countDown());
38 Uninterruptibles.awaitUninterruptibly(latch, LATCH_TIMEOUT, TimeUnit.SECONDS);
39 Verify.verify(future.isSuccess());
42 public static <R, T extends DataObject> R readData(final DataBroker dataBroker, final InstanceIdentifier<T> iid,
43 final Function<T, R> function) throws ReadFailedException {
44 AssertionError lastError = null;
45 final Stopwatch sw = Stopwatch.createStarted();
46 while (sw.elapsed(TimeUnit.SECONDS) <= TIMEOUT) {
47 try (final ReadOnlyTransaction tx = dataBroker.newReadOnlyTransaction()) {
48 final Optional<T> data = tx.read(LogicalDatastoreType.OPERATIONAL, iid).checkedGet();
49 if (data.isPresent()) {
51 return function.apply(data.get());
52 } catch (final AssertionError e) {
54 Uninterruptibles.sleepUninterruptibly(SLEEP_FOR, TimeUnit.MILLISECONDS);
62 public static <T extends DataObject> void checkNull(final DataBroker dataBroker, final InstanceIdentifier<T> iid)
63 throws ReadFailedException {
64 AssertionError lastError = null;
65 final Stopwatch sw = Stopwatch.createStarted();
66 while (sw.elapsed(TimeUnit.SECONDS) <= 10) {
67 try (final ReadOnlyTransaction tx = dataBroker.newReadOnlyTransaction()) {
68 final com.google.common.base.Optional<T> data = tx.read(LogicalDatastoreType.OPERATIONAL, iid).checkedGet();
70 assert !data.isPresent();
72 } catch (final AssertionError e) {
74 Uninterruptibles.sleepUninterruptibly(10, TimeUnit.MILLISECONDS);
81 public static void checkEquals(final CheckEquals function) throws Exception {
82 AssertionError lastError = null;
83 final Stopwatch sw = Stopwatch.createStarted();
84 while (sw.elapsed(TimeUnit.SECONDS) <= TIMEOUT) {
88 } catch (final AssertionError e) {
90 Uninterruptibles.sleepUninterruptibly(10, TimeUnit.MILLISECONDS);
96 public static void checkReceivedMessages(final ListenerCheck listener, final int numberOfMessages)
97 throws ReadFailedException {
98 final Stopwatch sw = Stopwatch.createStarted();
99 while (sw.elapsed(TimeUnit.SECONDS) <= TIMEOUT) {
100 if (listener.getListMessageSize() != numberOfMessages) {
101 Uninterruptibles.sleepUninterruptibly(SLEEP_FOR, TimeUnit.MILLISECONDS);
106 throw new AssertionError("Expected " + numberOfMessages + " but received "
107 + listener.getListMessageSize());
110 public interface ListenerCheck {
111 int getListMessageSize();
114 public interface CheckEquals {
115 void check() throws ExecutionException, InterruptedException;