1 package org.opendaylight.controller.md.sal.binding.impl.test;
3 import static org.junit.Assert.assertEquals;
4 import static org.junit.Assert.assertFalse;
5 import static org.opendaylight.controller.md.sal.test.model.util.ListsBindingUtils.TOP_BAR_KEY;
6 import static org.opendaylight.controller.md.sal.test.model.util.ListsBindingUtils.TOP_FOO_KEY;
7 import static org.opendaylight.controller.md.sal.test.model.util.ListsBindingUtils.USES_ONE_KEY;
8 import static org.opendaylight.controller.md.sal.test.model.util.ListsBindingUtils.complexUsesAugment;
9 import static org.opendaylight.controller.md.sal.test.model.util.ListsBindingUtils.path;
10 import static org.opendaylight.controller.md.sal.test.model.util.ListsBindingUtils.top;
11 import static org.opendaylight.controller.md.sal.test.model.util.ListsBindingUtils.topLevelList;
13 import com.google.common.collect.ImmutableSet;
14 import com.google.common.collect.Iterables;
15 import com.google.common.util.concurrent.SettableFuture;
16 import java.util.Collection;
17 import java.util.concurrent.TimeUnit;
18 import org.junit.Test;
19 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
20 import org.opendaylight.controller.md.sal.binding.api.DataObjectModification;
21 import org.opendaylight.controller.md.sal.binding.api.DataObjectModification.ModificationType;
22 import org.opendaylight.controller.md.sal.binding.api.DataTreeChangeListener;
23 import org.opendaylight.controller.md.sal.binding.api.DataTreeIdentifier;
24 import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
25 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
26 import org.opendaylight.controller.md.sal.binding.impl.BindingDOMDataBrokerAdapter;
27 import org.opendaylight.controller.md.sal.binding.test.AbstractDataBrokerTest;
28 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.augment.rev140709.TreeComplexUsesAugment;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.list.rev140701.Top;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.list.rev140701.TwoLevelList;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.list.rev140701.two.level.list.TopLevelList;
33 import org.opendaylight.yangtools.yang.binding.DataObject;
34 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
35 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier.PathArgument;
36 import org.opendaylight.yangtools.yang.binding.YangModuleInfo;
37 import org.opendaylight.yangtools.yang.binding.util.BindingReflections;
39 public class DataTreeChangeListenerTest extends AbstractDataBrokerTest {
41 private static final InstanceIdentifier<Top> TOP_PATH = InstanceIdentifier.create(Top.class);
42 private static final InstanceIdentifier<TopLevelList> FOO_PATH = path(TOP_FOO_KEY);
43 private static final PathArgument FOO_ARGUMENT = Iterables.getLast(FOO_PATH.getPathArguments());
44 private static final TopLevelList FOO_DATA = topLevelList(TOP_FOO_KEY, complexUsesAugment(USES_ONE_KEY));
45 private static final InstanceIdentifier<TopLevelList> BAR_PATH = path(TOP_BAR_KEY);
46 private static final PathArgument BAR_ARGUMENT = Iterables.getLast(BAR_PATH.getPathArguments());
47 private static final TopLevelList BAR_DATA = topLevelList(TOP_BAR_KEY);
48 private static final DataTreeIdentifier TOP_IDENTIFIER = new DataTreeIdentifier(LogicalDatastoreType.OPERATIONAL,
51 private static final Top TOP_INITIAL_DATA = top(FOO_DATA);
53 private BindingDOMDataBrokerAdapter dataBrokerImpl;
55 private static final class EventCapturingListener implements DataTreeChangeListener {
57 private SettableFuture<Collection<DataTreeModification>> changes = SettableFuture.create();
60 public void onDataTreeChanged(final Collection<DataTreeModification> changes) {
61 this.changes.set(changes);
65 Collection<DataTreeModification> nextEvent() throws Exception {
66 final Collection<DataTreeModification> result = changes.get(200,TimeUnit.MILLISECONDS);
67 changes = SettableFuture.create();
74 protected Iterable<YangModuleInfo> getModuleInfos() throws Exception {
75 return ImmutableSet.of(
76 BindingReflections.getModuleInfo(TwoLevelList.class),
77 BindingReflections.getModuleInfo(TreeComplexUsesAugment.class)
82 protected void setupWithDataBroker(final DataBroker dataBroker) {
83 dataBrokerImpl = (BindingDOMDataBrokerAdapter) dataBroker;
87 public void testTopLevelListener() throws Exception {
88 final EventCapturingListener listener = new EventCapturingListener();
89 dataBrokerImpl.registerDataTreeChangeListener(TOP_IDENTIFIER, listener);
91 createAndVerifyTop(listener);
93 putTx(BAR_PATH, BAR_DATA).submit().checkedGet();
94 final DataTreeModification afterBarPutEvent = Iterables.getOnlyElement(listener.nextEvent());
95 final DataObjectModification<? extends DataObject> barPutMod =
96 getOnlyChildModification(afterBarPutEvent.getRootNode(), ModificationType.SUBTREE_MODIFIED);
97 verifyModification(barPutMod, BAR_ARGUMENT, ModificationType.WRITE);
99 deleteTx(BAR_PATH).submit().checkedGet();
100 final DataTreeModification afterBarDeleteEvent = Iterables.getOnlyElement(listener.nextEvent());
101 final DataObjectModification<? extends DataObject> barDeleteMod =
102 getOnlyChildModification(afterBarDeleteEvent.getRootNode(), ModificationType.SUBTREE_MODIFIED);
103 verifyModification(barDeleteMod, BAR_ARGUMENT, ModificationType.DELETE);
107 public void testWildcardedListListener() throws Exception {
108 final EventCapturingListener listener = new EventCapturingListener();
109 final DataTreeIdentifier wildcard = new DataTreeIdentifier(LogicalDatastoreType.OPERATIONAL, TOP_PATH.child(TopLevelList.class));
110 dataBrokerImpl.registerDataTreeChangeListener(wildcard, listener);
112 putTx(TOP_PATH, TOP_INITIAL_DATA).submit().checkedGet();
114 final DataTreeModification fooWriteEvent = Iterables.getOnlyElement(listener.nextEvent());
115 assertEquals(FOO_PATH, fooWriteEvent.getRootPath().getRootIdentifier());
116 verifyModification(fooWriteEvent.getRootNode(), FOO_ARGUMENT, ModificationType.WRITE);
118 putTx(BAR_PATH, BAR_DATA).submit().checkedGet();
119 final DataTreeModification barWriteEvent = Iterables.getOnlyElement(listener.nextEvent());
120 assertEquals(BAR_PATH, barWriteEvent.getRootPath().getRootIdentifier());
121 verifyModification(barWriteEvent.getRootNode(), BAR_ARGUMENT, ModificationType.WRITE);
123 deleteTx(BAR_PATH).submit().checkedGet();
124 final DataTreeModification barDeleteEvent = Iterables.getOnlyElement(listener.nextEvent());
125 assertEquals(BAR_PATH, barDeleteEvent.getRootPath().getRootIdentifier());
126 verifyModification(barDeleteEvent.getRootNode(), BAR_ARGUMENT, ModificationType.DELETE);
131 private void createAndVerifyTop(final EventCapturingListener listener) throws Exception {
132 putTx(TOP_PATH,TOP_INITIAL_DATA).submit().checkedGet();
133 final Collection<DataTreeModification> events = listener.nextEvent();
135 assertFalse("Non empty collection should be received.",events.isEmpty());
136 final DataTreeModification initialWrite = Iterables.getOnlyElement(events);
137 final DataObjectModification<? extends DataObject> initialNode = initialWrite.getRootNode();
138 verifyModification(initialNode,TOP_PATH.getPathArguments().iterator().next(),ModificationType.WRITE);
139 assertEquals(TOP_INITIAL_DATA, initialNode.getDataAfter());
142 private DataObjectModification<? extends DataObject> getOnlyChildModification(final DataObjectModification<? extends DataObject> parentMod,final DataObjectModification.ModificationType eventType) throws Exception {
143 assertEquals(eventType,parentMod.getModificationType());
144 final Collection<DataObjectModification<? extends DataObject>> childMod = parentMod.getModifiedChildren();
145 assertFalse("Non empty children modification",childMod.isEmpty());
146 return Iterables.getOnlyElement(childMod);
149 private void verifyModification(final DataObjectModification<? extends DataObject> barWrite, final PathArgument pathArg,
150 final ModificationType eventType) {
151 assertEquals(pathArg.getType(), barWrite.getDataType());
152 assertEquals(eventType,barWrite.getModificationType());
153 assertEquals(pathArg, barWrite.getIdentifier());
156 private <T extends DataObject> WriteTransaction putTx(final InstanceIdentifier<T> path,final T data) {
157 final WriteTransaction tx = dataBrokerImpl.newWriteOnlyTransaction();
158 tx.put(LogicalDatastoreType.OPERATIONAL, path, data);
162 private WriteTransaction deleteTx(final InstanceIdentifier<?> path) {
163 final WriteTransaction tx = dataBrokerImpl.newWriteOnlyTransaction();
164 tx.delete(LogicalDatastoreType.OPERATIONAL, path);