MDSAL-107: Fix pre-existing data notification for wildcarded DTCL
[controller.git] / opendaylight / md-sal / sal-binding-broker / src / test / java / org / opendaylight / controller / md / sal / binding / impl / test / DataTreeChangeListenerTest.java
1 /*
2  * Copyright (c) 2014, 2015 Cisco Systems, Inc. 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
9 package org.opendaylight.controller.md.sal.binding.impl.test;
10
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertFalse;
13 import static org.junit.Assert.assertNotNull;
14 import static org.mockito.Mockito.mock;
15 import static org.mockito.Mockito.timeout;
16 import static org.mockito.Mockito.verify;
17 import static org.opendaylight.controller.md.sal.test.model.util.ListsBindingUtils.TOP_BAR_KEY;
18 import static org.opendaylight.controller.md.sal.test.model.util.ListsBindingUtils.TOP_FOO_KEY;
19 import static org.opendaylight.controller.md.sal.test.model.util.ListsBindingUtils.USES_ONE_KEY;
20 import static org.opendaylight.controller.md.sal.test.model.util.ListsBindingUtils.complexUsesAugment;
21 import static org.opendaylight.controller.md.sal.test.model.util.ListsBindingUtils.path;
22 import static org.opendaylight.controller.md.sal.test.model.util.ListsBindingUtils.top;
23 import static org.opendaylight.controller.md.sal.test.model.util.ListsBindingUtils.topLevelList;
24
25 import com.google.common.collect.ImmutableSet;
26 import com.google.common.collect.Iterables;
27 import com.google.common.util.concurrent.SettableFuture;
28 import java.util.ArrayList;
29 import java.util.Collection;
30 import java.util.concurrent.TimeUnit;
31 import org.junit.Before;
32 import org.junit.Test;
33 import org.mockito.Matchers;
34 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
35 import org.opendaylight.controller.md.sal.binding.api.DataObjectModification;
36 import org.opendaylight.controller.md.sal.binding.api.DataObjectModification.ModificationType;
37 import org.opendaylight.controller.md.sal.binding.api.DataTreeChangeListener;
38 import org.opendaylight.controller.md.sal.binding.api.DataTreeIdentifier;
39 import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
40 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
41 import org.opendaylight.controller.md.sal.binding.impl.BindingDOMDataBrokerAdapter;
42 import org.opendaylight.controller.md.sal.binding.test.AbstractConcurrentDataBrokerTest;
43 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.augment.rev140709.TreeComplexUsesAugment;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.list.rev140701.Top;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.list.rev140701.TopBuilder;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.list.rev140701.TwoLevelList;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.list.rev140701.two.level.list.TopLevelList;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.list.rev140701.two.level.list.TopLevelListBuilder;
50 import org.opendaylight.yangtools.yang.binding.DataObject;
51 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
52 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier.PathArgument;
53 import org.opendaylight.yangtools.yang.binding.YangModuleInfo;
54 import org.opendaylight.yangtools.yang.binding.util.BindingReflections;
55
56 public class DataTreeChangeListenerTest extends AbstractConcurrentDataBrokerTest {
57
58     private static final InstanceIdentifier<Top> TOP_PATH = InstanceIdentifier.create(Top.class);
59     private static final PathArgument TOP_ARGUMENT = TOP_PATH.getPathArguments().iterator().next();
60     private static final InstanceIdentifier<TopLevelList> FOO_PATH = path(TOP_FOO_KEY);
61     private static final PathArgument FOO_ARGUMENT = Iterables.getLast(FOO_PATH.getPathArguments());
62     private static final TopLevelList FOO_DATA = topLevelList(TOP_FOO_KEY, complexUsesAugment(USES_ONE_KEY));
63     private static final InstanceIdentifier<TopLevelList> BAR_PATH = path(TOP_BAR_KEY);
64     private static final PathArgument BAR_ARGUMENT = Iterables.getLast(BAR_PATH.getPathArguments());
65     private static final TopLevelList BAR_DATA = topLevelList(TOP_BAR_KEY);
66     private static final DataTreeIdentifier<Top> TOP_IDENTIFIER =
67             new DataTreeIdentifier<>(LogicalDatastoreType.OPERATIONAL, TOP_PATH);
68
69     private static final Top TOP_INITIAL_DATA = top(FOO_DATA);
70
71     private BindingDOMDataBrokerAdapter dataBrokerImpl;
72
73     private static final class EventCapturingListener<T extends DataObject> implements DataTreeChangeListener<T> {
74
75         private SettableFuture<Collection<DataTreeModification<T>>> changes = SettableFuture.create();
76
77         @Override
78         public void onDataTreeChanged(final Collection<DataTreeModification<T>> changes) {
79             this.changes.set(changes);
80
81         }
82
83         Collection<DataTreeModification<T>> nextEvent() throws Exception {
84             final Collection<DataTreeModification<T>> result = changes.get(200,TimeUnit.MILLISECONDS);
85             changes = SettableFuture.create();
86             return result;
87         }
88
89     }
90
91     @Override
92     protected Iterable<YangModuleInfo> getModuleInfos() throws Exception {
93         return ImmutableSet.of(
94                 BindingReflections.getModuleInfo(TwoLevelList.class),
95                 BindingReflections.getModuleInfo(TreeComplexUsesAugment.class)
96                 );
97     }
98
99     @Before
100     public void setupWithDataBroker() {
101         dataBrokerImpl = (BindingDOMDataBrokerAdapter) getDataBroker();
102     }
103
104     @Test
105     public void testTopLevelListener() throws Exception {
106         final EventCapturingListener<Top> listener = new EventCapturingListener<>();
107         dataBrokerImpl.registerDataTreeChangeListener(TOP_IDENTIFIER, listener);
108
109         createAndVerifyTop(listener);
110
111         putTx(BAR_PATH, BAR_DATA).submit().checkedGet();
112         final DataObjectModification<Top> afterBarPutEvent = Iterables.getOnlyElement(listener.nextEvent()).getRootNode();
113         verifyModification(afterBarPutEvent, TOP_ARGUMENT, ModificationType.SUBTREE_MODIFIED);
114         final DataObjectModification<TopLevelList> barPutMod = afterBarPutEvent.getModifiedChildListItem(TopLevelList.class, TOP_BAR_KEY);
115         assertNotNull(barPutMod);
116         verifyModification(barPutMod, BAR_ARGUMENT, ModificationType.WRITE);
117
118         deleteTx(BAR_PATH).submit().checkedGet();
119         final DataObjectModification<Top> afterBarDeleteEvent = Iterables.getOnlyElement(listener.nextEvent()).getRootNode();
120         verifyModification(afterBarDeleteEvent, TOP_ARGUMENT, ModificationType.SUBTREE_MODIFIED);
121         final DataObjectModification<TopLevelList> barDeleteMod = afterBarDeleteEvent.getModifiedChildListItem(TopLevelList.class, TOP_BAR_KEY);
122         verifyModification(barDeleteMod, BAR_ARGUMENT, ModificationType.DELETE);
123     }
124
125     @Test
126     public void testWildcardedListListener() throws Exception {
127         final EventCapturingListener<TopLevelList> listener = new EventCapturingListener<>();
128         final DataTreeIdentifier<TopLevelList> wildcard = new DataTreeIdentifier<>(LogicalDatastoreType.OPERATIONAL, TOP_PATH.child(TopLevelList.class));
129         dataBrokerImpl.registerDataTreeChangeListener(wildcard, listener);
130
131         putTx(TOP_PATH, TOP_INITIAL_DATA).submit().checkedGet();
132
133         final DataTreeModification<TopLevelList> fooWriteEvent = Iterables.getOnlyElement(listener.nextEvent());
134         assertEquals(FOO_PATH, fooWriteEvent.getRootPath().getRootIdentifier());
135         verifyModification(fooWriteEvent.getRootNode(), FOO_ARGUMENT, ModificationType.WRITE);
136
137         putTx(BAR_PATH, BAR_DATA).submit().checkedGet();
138         final DataTreeModification<TopLevelList> barWriteEvent = Iterables.getOnlyElement(listener.nextEvent());
139         assertEquals(BAR_PATH, barWriteEvent.getRootPath().getRootIdentifier());
140         verifyModification(barWriteEvent.getRootNode(), BAR_ARGUMENT, ModificationType.WRITE);
141
142         deleteTx(BAR_PATH).submit().checkedGet();
143         final DataTreeModification<TopLevelList> barDeleteEvent = Iterables.getOnlyElement(listener.nextEvent());
144         assertEquals(BAR_PATH, barDeleteEvent.getRootPath().getRootIdentifier());
145         verifyModification(barDeleteEvent.getRootNode(), BAR_ARGUMENT, ModificationType.DELETE);
146     }
147
148     @SuppressWarnings("unchecked")
149     @Test
150     public void testWildcardNotificationOfPreexistingData() throws Exception {
151         InstanceIdentifier<Top> id = InstanceIdentifier.builder(Top.class).build();
152         ArrayList<TopLevelList> list = new ArrayList<>();
153         list.add(new TopLevelListBuilder().setName("name").build());
154         TopBuilder builder = new TopBuilder().setTopLevelList(list);
155
156         DataBroker dataBroker = getDataBroker();
157
158         WriteTransaction writeTransaction = dataBroker.newWriteOnlyTransaction();
159         writeTransaction.put(LogicalDatastoreType.OPERATIONAL, id, builder.build());
160         assertCommit(writeTransaction.submit());
161
162         DataTreeChangeListener<TopLevelList> listener = mock(DataTreeChangeListener.class);
163         InstanceIdentifier<TopLevelList> wildcard = InstanceIdentifier.builder(Top.class).child(TopLevelList.class)
164                 .build();
165         dataBroker.registerDataTreeChangeListener(new DataTreeIdentifier<>(LogicalDatastoreType.OPERATIONAL, wildcard),
166                 listener);
167
168         verify(listener, timeout(1000)).onDataTreeChanged(Matchers.anyObject());
169     }
170
171     private void createAndVerifyTop(final EventCapturingListener<Top> listener) throws Exception {
172         putTx(TOP_PATH,TOP_INITIAL_DATA).submit().checkedGet();
173         final Collection<DataTreeModification<Top>> events = listener.nextEvent();
174
175         assertFalse("Non empty collection should be received.",events.isEmpty());
176         final DataTreeModification<Top> initialWrite = Iterables.getOnlyElement(events);
177         final DataObjectModification<? extends DataObject> initialNode = initialWrite.getRootNode();
178         verifyModification(initialNode,TOP_PATH.getPathArguments().iterator().next(),ModificationType.WRITE);
179         assertEquals(TOP_INITIAL_DATA, initialNode.getDataAfter());
180     }
181
182     private void verifyModification(final DataObjectModification<? extends DataObject> barWrite, final PathArgument pathArg,
183             final ModificationType eventType) {
184         assertEquals(pathArg.getType(), barWrite.getDataType());
185         assertEquals(eventType,barWrite.getModificationType());
186         assertEquals(pathArg, barWrite.getIdentifier());
187     }
188
189     private <T extends DataObject> WriteTransaction putTx(final InstanceIdentifier<T> path,final T data) {
190         final WriteTransaction tx = dataBrokerImpl.newWriteOnlyTransaction();
191         tx.put(LogicalDatastoreType.OPERATIONAL, path, data);
192         return tx;
193     }
194
195     private WriteTransaction deleteTx(final InstanceIdentifier<?> path) {
196         final WriteTransaction tx = dataBrokerImpl.newWriteOnlyTransaction();
197         tx.delete(LogicalDatastoreType.OPERATIONAL, path);
198         return tx;
199     }
200 }