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 org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType.CONFIGURATION;
11 import static org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType.OPERATIONAL;
13 import com.google.common.base.Optional;
14 import com.google.common.base.Stopwatch;
15 import com.google.common.base.Verify;
16 import com.google.common.util.concurrent.Uninterruptibles;
17 import io.netty.util.concurrent.Future;
18 import java.util.concurrent.CountDownLatch;
19 import java.util.concurrent.ExecutionException;
20 import java.util.concurrent.TimeUnit;
21 import java.util.function.Function;
22 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
23 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
24 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
25 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
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 LATCH_TIMEOUT = 10;
31 private static final int SLEEP_FOR = 200;
32 private static final int TIMEOUT = 30;
35 throw new UnsupportedOperationException();
38 public static <T extends Future<?>> void waitFutureSuccess(final T future) {
39 final CountDownLatch latch = new CountDownLatch(1);
40 future.addListener(future1 -> latch.countDown());
41 Uninterruptibles.awaitUninterruptibly(latch, LATCH_TIMEOUT, TimeUnit.SECONDS);
42 Verify.verify(future.isSuccess());
45 public static <R, T extends DataObject> R readDataOperational(final DataBroker dataBroker, final InstanceIdentifier<T> iid,
46 final Function<T, R> function) throws ReadFailedException {
47 return readData(dataBroker, OPERATIONAL, iid, function);
50 public static <R, T extends DataObject> R readDataConfiguration(final DataBroker dataBroker, final InstanceIdentifier<T> iid,
51 final Function<T, R> function) throws ReadFailedException {
52 return readData(dataBroker, CONFIGURATION, iid, function);
55 private static <R, T extends DataObject> R readData(final DataBroker dataBroker, final LogicalDatastoreType ldt,
56 final InstanceIdentifier<T> iid, final Function<T, R> function) throws ReadFailedException {
57 AssertionError lastError = null;
58 final Stopwatch sw = Stopwatch.createStarted();
59 while (sw.elapsed(TimeUnit.SECONDS) <= TIMEOUT) {
60 try (final ReadOnlyTransaction tx = dataBroker.newReadOnlyTransaction()) {
61 final Optional<T> data = tx.read(ldt, iid).checkedGet();
62 if (data.isPresent()) {
64 return function.apply(data.get());
65 } catch (final AssertionError e) {
67 Uninterruptibles.sleepUninterruptibly(SLEEP_FOR, TimeUnit.MILLISECONDS);
75 public static <T extends DataObject> T checkPresentOperational(final DataBroker dataBroker, final InstanceIdentifier<T> iid)
76 throws ReadFailedException {
77 return readData(dataBroker, OPERATIONAL, iid, bgpRib -> bgpRib);
80 public static <T extends DataObject> T checkPresentConfiguration(final DataBroker dataBroker,
81 final InstanceIdentifier<T> iid) throws ReadFailedException {
82 return readData(dataBroker, CONFIGURATION, iid, bgpRib -> bgpRib);
85 public static <T extends DataObject> void checkNotPresentOperational(final DataBroker dataBroker,
86 final InstanceIdentifier<T> iid) throws ReadFailedException {
87 checkNotPresent(dataBroker, OPERATIONAL, iid);
90 public static <T extends DataObject> void checkNotPresentConfiguration(final DataBroker dataBroker,
91 final InstanceIdentifier<T> iid) throws ReadFailedException {
92 checkNotPresent(dataBroker, CONFIGURATION, iid);
95 private static <T extends DataObject> void checkNotPresent(final DataBroker dataBroker,
96 final LogicalDatastoreType ldt, final InstanceIdentifier<T> iid) throws ReadFailedException {
97 AssertionError lastError = null;
98 final Stopwatch sw = Stopwatch.createStarted();
99 while (sw.elapsed(TimeUnit.SECONDS) <= 10) {
100 try (final ReadOnlyTransaction tx = dataBroker.newReadOnlyTransaction()) {
101 final com.google.common.base.Optional<T> data = tx.read(ldt, iid).checkedGet();
103 assert !data.isPresent();
105 } catch (final AssertionError e) {
107 Uninterruptibles.sleepUninterruptibly(10, TimeUnit.MILLISECONDS);
114 public static void checkEquals(final CheckEquals function) throws Exception {
115 AssertionError lastError = null;
116 final Stopwatch sw = Stopwatch.createStarted();
117 while (sw.elapsed(TimeUnit.SECONDS) <= TIMEOUT) {
121 } catch (final AssertionError e) {
123 Uninterruptibles.sleepUninterruptibly(10, TimeUnit.MILLISECONDS);
129 public static void checkReceivedMessages(final ListenerCheck listener, final int numberOfMessages)
130 throws ReadFailedException {
131 final Stopwatch sw = Stopwatch.createStarted();
132 while (sw.elapsed(TimeUnit.SECONDS) <= TIMEOUT) {
133 if (listener.getListMessageSize() != numberOfMessages) {
134 Uninterruptibles.sleepUninterruptibly(SLEEP_FOR, TimeUnit.MILLISECONDS);
139 throw new AssertionError("Expected " + numberOfMessages + " but received "
140 + listener.getListMessageSize());
143 public interface ListenerCheck {
144 int getListMessageSize();
147 public interface CheckEquals {
148 void check() throws ExecutionException, InterruptedException;