2 * Copyright (c) 2018 Inocybe Technologies 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.controller.md.sal.binding.test;
10 import static org.junit.Assert.fail;
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;
32 * Abstract base that provides a DTCL for verification.
34 * @author Thomas Pantelis
36 public class AbstractDataTreeChangeListenerTest extends AbstractConcurrentDataBrokerTest {
37 protected static final class TestListener<T extends DataObject> implements DataTreeChangeListener<T> {
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;
44 private TestListener(Function<DataTreeModification<T>, Boolean>[] matchers) {
45 this.expChangeCount = matchers.length;
46 this.matchers = matchers;
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));
59 public Collection<DataTreeModification<T>> changes() {
61 final Collection<DataTreeModification<T>> changes = future.get(5, TimeUnit.SECONDS);
62 Uninterruptibles.sleepUninterruptibly(500, TimeUnit.MILLISECONDS);
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);
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)) {
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()));
92 public boolean hasChanges() {
93 synchronized (accumulatedChanges) {
94 return !accumulatedChanges.isEmpty();
99 protected AbstractDataTreeChangeListenerTest() {
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);
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());
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));
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);
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);
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);
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);