Merge "Bug 2933: Implemented DataTreeChangeService"
[controller.git] / opendaylight / md-sal / sal-binding-broker / src / test / java / org / opendaylight / controller / md / sal / binding / impl / test / DataTreeChangeListenerTest.java
1 package org.opendaylight.controller.md.sal.binding.impl.test;
2
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;
12
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;
38
39 public class DataTreeChangeListenerTest extends AbstractDataBrokerTest {
40
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,
49             TOP_PATH);
50
51     private static final Top TOP_INITIAL_DATA = top(FOO_DATA);
52
53     private BindingDOMDataBrokerAdapter dataBrokerImpl;
54
55     private static final class EventCapturingListener implements DataTreeChangeListener {
56
57         private SettableFuture<Collection<DataTreeModification>> changes = SettableFuture.create();
58
59         @Override
60         public void onDataTreeChanged(final Collection<DataTreeModification> changes) {
61             this.changes.set(changes);
62
63         }
64
65         Collection<DataTreeModification> nextEvent() throws Exception {
66             final Collection<DataTreeModification> result = changes.get(200,TimeUnit.MILLISECONDS);
67             changes = SettableFuture.create();
68             return result;
69         }
70
71     }
72
73     @Override
74     protected Iterable<YangModuleInfo> getModuleInfos() throws Exception {
75         return ImmutableSet.of(
76                 BindingReflections.getModuleInfo(TwoLevelList.class),
77                 BindingReflections.getModuleInfo(TreeComplexUsesAugment.class)
78                 );
79     }
80
81     @Override
82     protected void setupWithDataBroker(final DataBroker dataBroker) {
83         dataBrokerImpl = (BindingDOMDataBrokerAdapter) dataBroker;
84     }
85
86     @Test
87     public void testTopLevelListener() throws Exception {
88         final EventCapturingListener listener = new EventCapturingListener();
89         dataBrokerImpl.registerDataTreeChangeListener(TOP_IDENTIFIER, listener);
90
91         createAndVerifyTop(listener);
92
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);
98
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);
104     }
105
106     @Test
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);
111
112         putTx(TOP_PATH, TOP_INITIAL_DATA).submit().checkedGet();
113
114         final DataTreeModification fooWriteEvent = Iterables.getOnlyElement(listener.nextEvent());
115         assertEquals(FOO_PATH, fooWriteEvent.getRootPath().getRootIdentifier());
116         verifyModification(fooWriteEvent.getRootNode(), FOO_ARGUMENT, ModificationType.WRITE);
117
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);
122
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);
127     }
128
129
130
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();
134
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());
140     }
141
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);
147     }
148
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());
154     }
155
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);
159         return tx;
160     }
161
162     private WriteTransaction deleteTx(final InstanceIdentifier<?> path) {
163         final WriteTransaction tx = dataBrokerImpl.newWriteOnlyTransaction();
164         tx.delete(LogicalDatastoreType.OPERATIONAL, path);
165         return tx;
166     }
167 }