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.TimeUnit;
17 import java.util.function.Function;
18 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
19 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
20 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
21 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
22 import org.opendaylight.yangtools.yang.binding.DataObject;
23 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
25 public final class CheckUtil {
26 private static final int LATCH_TIMEOUT = 10;
27 private static final int SLEEP_FOR = 200;
28 private static final int TIMEOUT = 30;
30 throw new UnsupportedOperationException();
33 @SuppressWarnings("unchecked")
34 public static <T extends Future> void waitFutureSuccess(final T future) {
35 final CountDownLatch latch = new CountDownLatch(1);
36 future.addListener(future1 -> latch.countDown());
37 Uninterruptibles.awaitUninterruptibly(latch, LATCH_TIMEOUT, TimeUnit.SECONDS);
38 Verify.verify(future.isSuccess());
41 public static <R, T extends DataObject> R readData(final DataBroker dataBroker, final InstanceIdentifier<T> iid,
42 final Function<T, R> function) throws ReadFailedException {
43 AssertionError lastError = null;
44 final Stopwatch sw = Stopwatch.createStarted();
45 while (sw.elapsed(TimeUnit.SECONDS) <= TIMEOUT) {
46 try (final ReadOnlyTransaction tx = dataBroker.newReadOnlyTransaction()) {
47 final Optional<T> data = tx.read(LogicalDatastoreType.OPERATIONAL, iid).checkedGet();
48 if (data.isPresent()) {
50 return function.apply(data.get());
51 } catch (final AssertionError e) {
53 Uninterruptibles.sleepUninterruptibly(SLEEP_FOR, TimeUnit.MILLISECONDS);
61 public static <T extends DataObject> void checkNull(final DataBroker dataBroker, final InstanceIdentifier<T> iid)
62 throws ReadFailedException {
63 AssertionError lastError = null;
64 final Stopwatch sw = Stopwatch.createStarted();
65 while (sw.elapsed(TimeUnit.SECONDS) <= 10) {
66 try (final ReadOnlyTransaction tx = dataBroker.newReadOnlyTransaction()) {
67 final com.google.common.base.Optional<T> data = tx.read(LogicalDatastoreType.OPERATIONAL, iid).checkedGet();
69 assert !data.isPresent();
71 } catch (final AssertionError e) {
73 Uninterruptibles.sleepUninterruptibly(10, TimeUnit.MILLISECONDS);
80 public static void checkEquals(final CheckEquals function) throws Exception {
81 AssertionError lastError = null;
82 final Stopwatch sw = Stopwatch.createStarted();
83 while (sw.elapsed(TimeUnit.SECONDS) <= TIMEOUT) {
87 } catch (final AssertionError e) {
89 Uninterruptibles.sleepUninterruptibly(10, TimeUnit.MILLISECONDS);
95 public static void checkReceivedMessages(final ListenerCheck listener, final int numberOfMessages)
96 throws ReadFailedException {
97 final Stopwatch sw = Stopwatch.createStarted();
98 while (sw.elapsed(TimeUnit.SECONDS) <= TIMEOUT) {
99 if (listener.getListMessageSize() != numberOfMessages) {
100 Uninterruptibles.sleepUninterruptibly(SLEEP_FOR, TimeUnit.MILLISECONDS);
105 throw new AssertionError("Expected " + numberOfMessages + " but received "
106 + listener.getListMessageSize());
109 public interface ListenerCheck {
110 int getListMessageSize();
113 public interface CheckEquals {