8f5502e93dec9a378edda224af723457e4c2689a
[bgpcep.git] / testtool-util / src / main / java / org / opendaylight / protocol / util / CheckUtil.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.protocol.util;
9
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;
24
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;
29     private CheckUtil() {
30         throw new UnsupportedOperationException();
31     }
32
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());
39     }
40
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()) {
49                     try {
50                         return function.apply(data.get());
51                     } catch (final AssertionError e) {
52                         lastError = e;
53                         Uninterruptibles.sleepUninterruptibly(SLEEP_FOR, TimeUnit.MILLISECONDS);
54                     }
55                 }
56             }
57         }
58         throw lastError;
59     }
60
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();
68                 try {
69                     assert !data.isPresent();
70                     return;
71                 } catch (final AssertionError e) {
72                     lastError = e;
73                     Uninterruptibles.sleepUninterruptibly(10, TimeUnit.MILLISECONDS);
74                 }
75             }
76         }
77         throw lastError;
78     }
79
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) {
84             try {
85                 function.check();
86                 return;
87             } catch (final AssertionError e) {
88                 lastError = e;
89                 Uninterruptibles.sleepUninterruptibly(10, TimeUnit.MILLISECONDS);
90             }
91         }
92         throw lastError;
93     }
94
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);
101             } else {
102                 return;
103             }
104         }
105         throw new AssertionError("Expected " + numberOfMessages + " but received "
106             + listener.getListMessageSize());
107     }
108
109     public interface ListenerCheck {
110         int getListMessageSize();
111     }
112     @FunctionalInterface
113     public interface CheckEquals {
114         void check();
115     }
116 }