26b20291983a5761f629a931296f8ce9925c8295
[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 static org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType.CONFIGURATION;
11 import static org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType.OPERATIONAL;
12
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;
28
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;
33
34     private CheckUtil() {
35         throw new UnsupportedOperationException();
36     }
37
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());
43     }
44
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);
48     }
49
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);
53     }
54
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()) {
63                     try {
64                         return function.apply(data.get());
65                     } catch (final AssertionError e) {
66                         lastError = e;
67                         Uninterruptibles.sleepUninterruptibly(SLEEP_FOR, TimeUnit.MILLISECONDS);
68                     }
69                 }
70             }
71         }
72         throw lastError;
73     }
74
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);
78     }
79
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);
83     }
84
85     public static <T extends DataObject> void checkNotPresentOperational(final DataBroker dataBroker,
86         final InstanceIdentifier<T> iid) throws ReadFailedException {
87         checkNotPresent(dataBroker, OPERATIONAL, iid);
88     }
89
90     public static <T extends DataObject> void checkNotPresentConfiguration(final DataBroker dataBroker,
91         final InstanceIdentifier<T> iid) throws ReadFailedException {
92         checkNotPresent(dataBroker, CONFIGURATION, iid);
93     }
94
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();
102                 try {
103                     assert !data.isPresent();
104                     return;
105                 } catch (final AssertionError e) {
106                     lastError = e;
107                     Uninterruptibles.sleepUninterruptibly(10, TimeUnit.MILLISECONDS);
108                 }
109             }
110         }
111         throw lastError;
112     }
113
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) {
118             try {
119                 function.check();
120                 return;
121             } catch (final AssertionError e) {
122                 lastError = e;
123                 Uninterruptibles.sleepUninterruptibly(10, TimeUnit.MILLISECONDS);
124             }
125         }
126         throw lastError;
127     }
128
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);
135             } else {
136                 return;
137             }
138         }
139         throw new AssertionError("Expected " + numberOfMessages + " but received "
140             + listener.getListMessageSize());
141     }
142
143     public interface ListenerCheck {
144         int getListMessageSize();
145     }
146     @FunctionalInterface
147     public interface CheckEquals {
148         void check() throws ExecutionException, InterruptedException;
149     }
150 }