Convert DCL tests to use DTCL
[controller.git] / opendaylight / md-sal / sal-binding-broker / src / test / java / org / opendaylight / controller / md / sal / binding / test / AbstractDataTreeChangeListenerTest.java
1 /*
2  * Copyright (c) 2018 Inocybe Technologies 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.controller.md.sal.binding.test;
9
10 import static org.junit.Assert.fail;
11
12 import com.google.common.util.concurrent.SettableFuture;
13 import com.google.common.util.concurrent.Uninterruptibles;
14 import java.util.ArrayList;
15 import java.util.Collection;
16 import java.util.Iterator;
17 import java.util.List;
18 import java.util.Objects;
19 import java.util.concurrent.ExecutionException;
20 import java.util.concurrent.TimeUnit;
21 import java.util.concurrent.TimeoutException;
22 import java.util.function.Function;
23 import org.opendaylight.controller.md.sal.binding.api.DataObjectModification.ModificationType;
24 import org.opendaylight.controller.md.sal.binding.api.DataTreeChangeListener;
25 import org.opendaylight.controller.md.sal.binding.api.DataTreeIdentifier;
26 import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
27 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
28 import org.opendaylight.yangtools.yang.binding.DataObject;
29 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
30
31 /**
32  * Abstract base that provides a DTCL for verification.
33  *
34  * @author Thomas Pantelis
35  */
36 public class AbstractDataTreeChangeListenerTest extends AbstractConcurrentDataBrokerTest {
37     protected static final class TestListener<T extends DataObject> implements DataTreeChangeListener<T> {
38
39         private final List<DataTreeModification<T>> accumulatedChanges = new ArrayList<>();
40         private final SettableFuture<Collection<DataTreeModification<T>>> future = SettableFuture.create();
41         private final Function<DataTreeModification<T>, Boolean>[] matchers;
42         private final int expChangeCount;
43
44         private TestListener(Function<DataTreeModification<T>, Boolean>[] matchers) {
45             this.expChangeCount = matchers.length;
46             this.matchers = matchers;
47         }
48
49         @Override
50         public void onDataTreeChanged(Collection<DataTreeModification<T>> changes) {
51             synchronized (accumulatedChanges) {
52                 accumulatedChanges.addAll(changes);
53                 if (expChangeCount == accumulatedChanges.size()) {
54                     future.set(new ArrayList<>(accumulatedChanges));
55                 }
56             }
57         }
58
59         public Collection<DataTreeModification<T>> changes() {
60             try {
61                 final Collection<DataTreeModification<T>> changes = future.get(5, TimeUnit.SECONDS);
62                 Uninterruptibles.sleepUninterruptibly(500, TimeUnit.MILLISECONDS);
63                 return changes;
64             } catch (InterruptedException | TimeoutException | ExecutionException e) {
65                 throw new AssertionError(String.format(
66                     "Data tree change notifications not received. Expected: %s. Actual: %s - %s",
67                         expChangeCount, accumulatedChanges.size(), accumulatedChanges), e);
68             }
69         }
70
71         public void verify() {
72             Collection<DataTreeModification<T>> changes = new ArrayList<>(changes());
73             Iterator<DataTreeModification<T>> iter = changes.iterator();
74             while (iter.hasNext()) {
75                 DataTreeModification<T> dataTreeModification = iter.next();
76                 for (Function<DataTreeModification<T>, Boolean> matcher: matchers) {
77                     if (matcher.apply(dataTreeModification)) {
78                         iter.remove();
79                         break;
80                     }
81                 }
82             }
83
84             if (!changes.isEmpty()) {
85                 DataTreeModification<T> mod = changes.iterator().next();
86                 fail(String.format("Received unexpected notification: type: %s, path: %s, before: %s, after: %s",
87                         mod.getRootNode().getModificationType(), mod.getRootPath().getRootIdentifier(),
88                         mod.getRootNode().getDataBefore(), mod.getRootNode().getDataAfter()));
89             }
90         }
91
92         public boolean hasChanges() {
93             synchronized (accumulatedChanges) {
94                 return !accumulatedChanges.isEmpty();
95             }
96         }
97     }
98
99     protected AbstractDataTreeChangeListenerTest() {
100         super(true);
101     }
102
103     @SafeVarargs
104     protected final <T extends DataObject> TestListener<T> createListener(final LogicalDatastoreType store,
105             final InstanceIdentifier<T> path, Function<DataTreeModification<T>, Boolean>... matchers) {
106         TestListener<T> listener = new TestListener<>(matchers);
107         getDataBroker().registerDataTreeChangeListener(new DataTreeIdentifier<>(store, path), listener);
108         return listener;
109     }
110
111     public static <T extends DataObject> Function<DataTreeModification<T>, Boolean> match(
112             ModificationType type, InstanceIdentifier<T> path, Function<T, Boolean> checkDataBefore,
113             Function<T, Boolean> checkDataAfter) {
114         return modification -> type == modification.getRootNode().getModificationType()
115                 && path.equals(modification.getRootPath().getRootIdentifier())
116                 && checkDataBefore.apply(modification.getRootNode().getDataBefore())
117                 && checkDataAfter.apply(modification.getRootNode().getDataAfter());
118     }
119
120     public static <T extends DataObject> Function<DataTreeModification<T>, Boolean> match(
121             ModificationType type, InstanceIdentifier<T> path, T expDataBefore, T expDataAfter) {
122         return match(type, path, dataBefore -> Objects.equals(expDataBefore, dataBefore),
123             (Function<T, Boolean>) dataAfter -> Objects.equals(expDataAfter, dataAfter));
124     }
125
126     public static <T extends DataObject> Function<DataTreeModification<T>, Boolean> added(
127             InstanceIdentifier<T> path, T data) {
128         return match(ModificationType.WRITE, path, null, data);
129     }
130
131     public static <T extends DataObject> Function<DataTreeModification<T>, Boolean> replaced(
132             InstanceIdentifier<T> path, T dataBefore, T dataAfter) {
133         return match(ModificationType.WRITE, path, dataBefore, dataAfter);
134     }
135
136     public static <T extends DataObject> Function<DataTreeModification<T>, Boolean> deleted(
137             InstanceIdentifier<T> path, T dataBefore) {
138         return match(ModificationType.DELETE, path, dataBefore, null);
139     }
140
141     public static <T extends DataObject> Function<DataTreeModification<T>, Boolean> subtreeModified(
142             InstanceIdentifier<T> path, T dataBefore, T dataAfter) {
143         return match(ModificationType.SUBTREE_MODIFIED, path, dataBefore, dataAfter);
144     }
145 }