Merge "Bug 2820 - LLDP TLV support and testing"
[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.junit.Assert.assertNotNull;
6 import static org.opendaylight.controller.md.sal.test.model.util.ListsBindingUtils.TOP_BAR_KEY;
7 import static org.opendaylight.controller.md.sal.test.model.util.ListsBindingUtils.TOP_FOO_KEY;
8 import static org.opendaylight.controller.md.sal.test.model.util.ListsBindingUtils.USES_ONE_KEY;
9 import static org.opendaylight.controller.md.sal.test.model.util.ListsBindingUtils.complexUsesAugment;
10 import static org.opendaylight.controller.md.sal.test.model.util.ListsBindingUtils.path;
11 import static org.opendaylight.controller.md.sal.test.model.util.ListsBindingUtils.top;
12 import static org.opendaylight.controller.md.sal.test.model.util.ListsBindingUtils.topLevelList;
13
14 import com.google.common.collect.ImmutableSet;
15 import com.google.common.collect.Iterables;
16 import com.google.common.util.concurrent.SettableFuture;
17 import java.util.Collection;
18 import java.util.concurrent.TimeUnit;
19 import org.junit.Test;
20 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
21 import org.opendaylight.controller.md.sal.binding.api.DataObjectModification;
22 import org.opendaylight.controller.md.sal.binding.api.DataObjectModification.ModificationType;
23 import org.opendaylight.controller.md.sal.binding.api.DataTreeChangeListener;
24 import org.opendaylight.controller.md.sal.binding.api.DataTreeIdentifier;
25 import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
26 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
27 import org.opendaylight.controller.md.sal.binding.impl.BindingDOMDataBrokerAdapter;
28 import org.opendaylight.controller.md.sal.binding.test.AbstractDataBrokerTest;
29 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.augment.rev140709.TreeComplexUsesAugment;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.list.rev140701.Top;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.list.rev140701.TwoLevelList;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.list.rev140701.two.level.list.TopLevelList;
34 import org.opendaylight.yangtools.yang.binding.DataObject;
35 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
36 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier.PathArgument;
37 import org.opendaylight.yangtools.yang.binding.YangModuleInfo;
38 import org.opendaylight.yangtools.yang.binding.util.BindingReflections;
39
40 public class DataTreeChangeListenerTest extends AbstractDataBrokerTest {
41
42     private static final InstanceIdentifier<Top> TOP_PATH = InstanceIdentifier.create(Top.class);
43     private static final PathArgument TOP_ARGUMENT= TOP_PATH.getPathArguments().iterator().next();
44     private static final InstanceIdentifier<TopLevelList> FOO_PATH = path(TOP_FOO_KEY);
45     private static final PathArgument FOO_ARGUMENT = Iterables.getLast(FOO_PATH.getPathArguments());
46     private static final TopLevelList FOO_DATA = topLevelList(TOP_FOO_KEY, complexUsesAugment(USES_ONE_KEY));
47     private static final InstanceIdentifier<TopLevelList> BAR_PATH = path(TOP_BAR_KEY);
48     private static final PathArgument BAR_ARGUMENT = Iterables.getLast(BAR_PATH.getPathArguments());
49     private static final TopLevelList BAR_DATA = topLevelList(TOP_BAR_KEY);
50 private static final DataTreeIdentifier<Top> TOP_IDENTIFIER = new DataTreeIdentifier<Top>(LogicalDatastoreType.OPERATIONAL,
51             TOP_PATH);
52
53     private static final Top TOP_INITIAL_DATA = top(FOO_DATA);
54
55     private BindingDOMDataBrokerAdapter dataBrokerImpl;
56
57     private static final class EventCapturingListener<T extends DataObject> implements DataTreeChangeListener<T> {
58
59         private SettableFuture<Collection<DataTreeModification<T>>> changes = SettableFuture.create();
60
61         @Override
62         public void onDataTreeChanged(final Collection<DataTreeModification<T>> changes) {
63             this.changes.set(changes);
64
65         }
66
67         Collection<DataTreeModification<T>> nextEvent() throws Exception {
68             final Collection<DataTreeModification<T>> result = changes.get(200,TimeUnit.MILLISECONDS);
69             changes = SettableFuture.create();
70             return result;
71         }
72
73     }
74
75     @Override
76     protected Iterable<YangModuleInfo> getModuleInfos() throws Exception {
77         return ImmutableSet.of(
78                 BindingReflections.getModuleInfo(TwoLevelList.class),
79                 BindingReflections.getModuleInfo(TreeComplexUsesAugment.class)
80                 );
81     }
82
83     @Override
84     protected void setupWithDataBroker(final DataBroker dataBroker) {
85         dataBrokerImpl = (BindingDOMDataBrokerAdapter) dataBroker;
86     }
87
88     @Test
89     public void testTopLevelListener() throws Exception {
90         final EventCapturingListener<Top> listener = new EventCapturingListener<>();
91         dataBrokerImpl.registerDataTreeChangeListener(TOP_IDENTIFIER, listener);
92
93         createAndVerifyTop(listener);
94
95         putTx(BAR_PATH, BAR_DATA).submit().checkedGet();
96         final DataObjectModification<Top> afterBarPutEvent = Iterables.getOnlyElement(listener.nextEvent()).getRootNode();
97         verifyModification(afterBarPutEvent, TOP_ARGUMENT, ModificationType.SUBTREE_MODIFIED);
98         final DataObjectModification<TopLevelList> barPutMod = afterBarPutEvent.getModifiedChildListItem(TopLevelList.class, TOP_BAR_KEY);
99         assertNotNull(barPutMod);
100         verifyModification(barPutMod, BAR_ARGUMENT, ModificationType.WRITE);
101
102         deleteTx(BAR_PATH).submit().checkedGet();
103         final DataObjectModification<Top> afterBarDeleteEvent = Iterables.getOnlyElement(listener.nextEvent()).getRootNode();
104         verifyModification(afterBarDeleteEvent, TOP_ARGUMENT, ModificationType.SUBTREE_MODIFIED);
105         final DataObjectModification<TopLevelList> barDeleteMod = afterBarDeleteEvent.getModifiedChildListItem(TopLevelList.class, TOP_BAR_KEY);
106         verifyModification(barDeleteMod, BAR_ARGUMENT, ModificationType.DELETE);
107     }
108
109     @Test
110     public void testWildcardedListListener() throws Exception {
111         final EventCapturingListener<TopLevelList> listener = new EventCapturingListener<>();
112         final DataTreeIdentifier<TopLevelList> wildcard = new DataTreeIdentifier<>(LogicalDatastoreType.OPERATIONAL, TOP_PATH.child(TopLevelList.class));
113         dataBrokerImpl.registerDataTreeChangeListener(wildcard, listener);
114
115         putTx(TOP_PATH, TOP_INITIAL_DATA).submit().checkedGet();
116
117         final DataTreeModification<TopLevelList> fooWriteEvent = Iterables.getOnlyElement(listener.nextEvent());
118         assertEquals(FOO_PATH, fooWriteEvent.getRootPath().getRootIdentifier());
119         verifyModification(fooWriteEvent.getRootNode(), FOO_ARGUMENT, ModificationType.WRITE);
120
121         putTx(BAR_PATH, BAR_DATA).submit().checkedGet();
122         final DataTreeModification<TopLevelList> barWriteEvent = Iterables.getOnlyElement(listener.nextEvent());
123         assertEquals(BAR_PATH, barWriteEvent.getRootPath().getRootIdentifier());
124         verifyModification(barWriteEvent.getRootNode(), BAR_ARGUMENT, ModificationType.WRITE);
125
126         deleteTx(BAR_PATH).submit().checkedGet();
127         final DataTreeModification<TopLevelList> barDeleteEvent = Iterables.getOnlyElement(listener.nextEvent());
128         assertEquals(BAR_PATH, barDeleteEvent.getRootPath().getRootIdentifier());
129         verifyModification(barDeleteEvent.getRootNode(), BAR_ARGUMENT, ModificationType.DELETE);
130     }
131
132
133
134     private void createAndVerifyTop(final EventCapturingListener<Top> listener) throws Exception {
135         putTx(TOP_PATH,TOP_INITIAL_DATA).submit().checkedGet();
136         final Collection<DataTreeModification<Top>> events = listener.nextEvent();
137
138         assertFalse("Non empty collection should be received.",events.isEmpty());
139         final DataTreeModification<Top> initialWrite = Iterables.getOnlyElement(events);
140         final DataObjectModification<? extends DataObject> initialNode = initialWrite.getRootNode();
141         verifyModification(initialNode,TOP_PATH.getPathArguments().iterator().next(),ModificationType.WRITE);
142         assertEquals(TOP_INITIAL_DATA, initialNode.getDataAfter());
143     }
144
145     private void verifyModification(final DataObjectModification<? extends DataObject> barWrite, final PathArgument pathArg,
146             final ModificationType eventType) {
147         assertEquals(pathArg.getType(), barWrite.getDataType());
148         assertEquals(eventType,barWrite.getModificationType());
149         assertEquals(pathArg, barWrite.getIdentifier());
150     }
151
152     private <T extends DataObject> WriteTransaction putTx(final InstanceIdentifier<T> path,final T data) {
153         final WriteTransaction tx = dataBrokerImpl.newWriteOnlyTransaction();
154         tx.put(LogicalDatastoreType.OPERATIONAL, path, data);
155         return tx;
156     }
157
158     private WriteTransaction deleteTx(final InstanceIdentifier<?> path) {
159         final WriteTransaction tx = dataBrokerImpl.newWriteOnlyTransaction();
160         tx.delete(LogicalDatastoreType.OPERATIONAL, path);
161         return tx;
162     }
163 }