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.bgp.rib.impl;
10 import com.google.common.base.Optional;
11 import com.google.common.base.Stopwatch;
12 import com.google.common.util.concurrent.Uninterruptibles;
13 import io.netty.util.concurrent.Future;
14 import java.util.concurrent.CountDownLatch;
15 import java.util.concurrent.TimeUnit;
16 import java.util.function.Function;
17 import org.junit.Assert;
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 TIMEOUT = 40;
27 private static final int LATCH_TIMEOUT = 10;
28 private static final int SLEEP_FOR = 20;
29 private static final int SLEEP_UNINTERRUPTIBLY = 50;
30 public static void checkReceivedMessages(final SimpleSessionListener listener, final int numberOfMessages)
31 throws ReadFailedException {
32 Stopwatch sw = Stopwatch.createStarted();
33 while (sw.elapsed(TimeUnit.SECONDS) <= TIMEOUT) {
34 if (listener.getListMsg().size() != numberOfMessages) {
35 Uninterruptibles.sleepUninterruptibly(SLEEP_UNINTERRUPTIBLY, TimeUnit.MILLISECONDS);
43 public static <R, T extends DataObject> R readData(final DataBroker dataBroker, final InstanceIdentifier<T> iid, final Function<T, R> function)
44 throws ReadFailedException {
45 AssertionError lastError = null;
46 final Stopwatch sw = Stopwatch.createStarted();
47 while (sw.elapsed(TimeUnit.SECONDS) <= TIMEOUT) {
48 try (final ReadOnlyTransaction tx = dataBroker.newReadOnlyTransaction()) {
49 final Optional<T> data = tx.read(LogicalDatastoreType.OPERATIONAL, iid).checkedGet();
50 if (data.isPresent()) {
52 return function.apply(data.get());
53 } catch (final AssertionError e) {
55 Uninterruptibles.sleepUninterruptibly(SLEEP_FOR, TimeUnit.MILLISECONDS);
60 Assert.fail(lastError.getMessage());
64 @SuppressWarnings("unchecked")
65 public static <T extends Future> void waitFutureSuccess(final T future) {
66 final CountDownLatch latch = new CountDownLatch(1);
67 future.addListener(future1 -> latch.countDown());
68 Uninterruptibles.awaitUninterruptibly(latch, LATCH_TIMEOUT, TimeUnit.SECONDS);