Cache reflection operations in AbstractSchemaAwareTest
[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 package org.opendaylight.controller.md.sal.binding.impl.test;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertFalse;
12 import static org.junit.Assert.assertNotNull;
13 import static org.mockito.Mockito.mock;
14 import static org.mockito.Mockito.timeout;
15 import static org.mockito.Mockito.verify;
16 import static org.opendaylight.controller.md.sal.test.model.util.ListsBindingUtils.TOP_BAR_KEY;
17 import static org.opendaylight.controller.md.sal.test.model.util.ListsBindingUtils.TOP_FOO_KEY;
18 import static org.opendaylight.controller.md.sal.test.model.util.ListsBindingUtils.USES_ONE_KEY;
19 import static org.opendaylight.controller.md.sal.test.model.util.ListsBindingUtils.complexUsesAugment;
20 import static org.opendaylight.controller.md.sal.test.model.util.ListsBindingUtils.path;
21 import static org.opendaylight.controller.md.sal.test.model.util.ListsBindingUtils.top;
22 import static org.opendaylight.controller.md.sal.test.model.util.ListsBindingUtils.topLevelList;
23
24 import com.google.common.collect.ImmutableSet;
25 import com.google.common.collect.Iterables;
26 import com.google.common.util.concurrent.SettableFuture;
27 import java.util.ArrayList;
28 import java.util.Collection;
29 import java.util.Set;
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.mdsal.binding.spec.reflect.BindingReflections;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.augment.rev140709.TreeComplexUsesAugment;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.list.rev140701.Top;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.list.rev140701.TopBuilder;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.list.rev140701.TwoLevelList;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.list.rev140701.two.level.list.TopLevelList;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.list.rev140701.two.level.list.TopLevelListBuilder;
51 import org.opendaylight.yangtools.yang.binding.DataObject;
52 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
53 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier.PathArgument;
54 import org.opendaylight.yangtools.yang.binding.YangModuleInfo;
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>> modification) {
79             this.changes.set(modification);
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     @Override
91     protected Set<YangModuleInfo> getModuleInfos() throws Exception {
92         return ImmutableSet.of(
93                 BindingReflections.getModuleInfo(TwoLevelList.class),
94                 BindingReflections.getModuleInfo(TreeComplexUsesAugment.class)
95                 );
96     }
97
98     @Before
99     public void setupWithDataBroker() {
100         dataBrokerImpl = (BindingDOMDataBrokerAdapter) getDataBroker();
101     }
102
103     @Test
104     public void testTopLevelListener() throws Exception {
105         final EventCapturingListener<Top> listener = new EventCapturingListener<>();
106         dataBrokerImpl.registerDataTreeChangeListener(TOP_IDENTIFIER, listener);
107
108         createAndVerifyTop(listener);
109
110         putTx(BAR_PATH, BAR_DATA).submit().checkedGet();
111         final DataObjectModification<Top> afterBarPutEvent = Iterables.getOnlyElement(listener.nextEvent())
112                 .getRootNode();
113         verifyModification(afterBarPutEvent, TOP_ARGUMENT, ModificationType.SUBTREE_MODIFIED);
114         final DataObjectModification<TopLevelList> barPutMod = afterBarPutEvent
115                 .getModifiedChildListItem(TopLevelList.class, TOP_BAR_KEY);
116         assertNotNull(barPutMod);
117         verifyModification(barPutMod, BAR_ARGUMENT, ModificationType.WRITE);
118
119         deleteTx(BAR_PATH).submit().checkedGet();
120         final DataObjectModification<Top> afterBarDeleteEvent = Iterables.getOnlyElement(listener.nextEvent())
121                 .getRootNode();
122         verifyModification(afterBarDeleteEvent, TOP_ARGUMENT, ModificationType.SUBTREE_MODIFIED);
123         final DataObjectModification<TopLevelList> barDeleteMod = afterBarDeleteEvent
124                 .getModifiedChildListItem(TopLevelList.class, TOP_BAR_KEY);
125         verifyModification(barDeleteMod, BAR_ARGUMENT, ModificationType.DELETE);
126     }
127
128     @Test
129     public void testWildcardedListListener() throws Exception {
130         final EventCapturingListener<TopLevelList> listener = new EventCapturingListener<>();
131         final DataTreeIdentifier<TopLevelList> wildcard = new DataTreeIdentifier<>(LogicalDatastoreType.OPERATIONAL,
132                 TOP_PATH.child(TopLevelList.class));
133         dataBrokerImpl.registerDataTreeChangeListener(wildcard, listener);
134
135         putTx(TOP_PATH, TOP_INITIAL_DATA).submit().checkedGet();
136
137         final DataTreeModification<TopLevelList> fooWriteEvent = Iterables.getOnlyElement(listener.nextEvent());
138         assertEquals(FOO_PATH, fooWriteEvent.getRootPath().getRootIdentifier());
139         verifyModification(fooWriteEvent.getRootNode(), FOO_ARGUMENT, ModificationType.WRITE);
140
141         putTx(BAR_PATH, BAR_DATA).submit().checkedGet();
142         final DataTreeModification<TopLevelList> barWriteEvent = Iterables.getOnlyElement(listener.nextEvent());
143         assertEquals(BAR_PATH, barWriteEvent.getRootPath().getRootIdentifier());
144         verifyModification(barWriteEvent.getRootNode(), BAR_ARGUMENT, ModificationType.WRITE);
145
146         deleteTx(BAR_PATH).submit().checkedGet();
147         final DataTreeModification<TopLevelList> barDeleteEvent = Iterables.getOnlyElement(listener.nextEvent());
148         assertEquals(BAR_PATH, barDeleteEvent.getRootPath().getRootIdentifier());
149         verifyModification(barDeleteEvent.getRootNode(), BAR_ARGUMENT, ModificationType.DELETE);
150     }
151
152     @SuppressWarnings("unchecked")
153     @Test
154     public void testWildcardNotificationOfPreexistingData() {
155         InstanceIdentifier<Top> id = InstanceIdentifier.builder(Top.class).build();
156         ArrayList<TopLevelList> list = new ArrayList<>();
157         list.add(new TopLevelListBuilder().setName("name").build());
158         TopBuilder builder = new TopBuilder().setTopLevelList(list);
159
160         DataBroker dataBroker = getDataBroker();
161
162         WriteTransaction writeTransaction = dataBroker.newWriteOnlyTransaction();
163         writeTransaction.put(LogicalDatastoreType.OPERATIONAL, id, builder.build());
164         assertCommit(writeTransaction.submit());
165
166         DataTreeChangeListener<TopLevelList> listener = mock(DataTreeChangeListener.class);
167         InstanceIdentifier<TopLevelList> wildcard = InstanceIdentifier.builder(Top.class).child(TopLevelList.class)
168                 .build();
169         dataBroker.registerDataTreeChangeListener(new DataTreeIdentifier<>(LogicalDatastoreType.OPERATIONAL, wildcard),
170                 listener);
171
172         verify(listener, timeout(1000)).onDataTreeChanged(Matchers.anyObject());
173     }
174
175     private void createAndVerifyTop(final EventCapturingListener<Top> listener) throws Exception {
176         putTx(TOP_PATH,TOP_INITIAL_DATA).submit().checkedGet();
177         final Collection<DataTreeModification<Top>> events = listener.nextEvent();
178
179         assertFalse("Non empty collection should be received.",events.isEmpty());
180         final DataTreeModification<Top> initialWrite = Iterables.getOnlyElement(events);
181         final DataObjectModification<? extends DataObject> initialNode = initialWrite.getRootNode();
182         verifyModification(initialNode,TOP_PATH.getPathArguments().iterator().next(),ModificationType.WRITE);
183         assertEquals(TOP_INITIAL_DATA, initialNode.getDataAfter());
184     }
185
186     private void verifyModification(final DataObjectModification<? extends DataObject> barWrite,
187             final PathArgument pathArg, final ModificationType eventType) {
188         assertEquals(pathArg.getType(), barWrite.getDataType());
189         assertEquals(eventType,barWrite.getModificationType());
190         assertEquals(pathArg, barWrite.getIdentifier());
191     }
192
193     private <T extends DataObject> WriteTransaction putTx(final InstanceIdentifier<T> path,final T data) {
194         final WriteTransaction tx = dataBrokerImpl.newWriteOnlyTransaction();
195         tx.put(LogicalDatastoreType.OPERATIONAL, path, data);
196         return tx;
197     }
198
199     private WriteTransaction deleteTx(final InstanceIdentifier<?> path) {
200         final WriteTransaction tx = dataBrokerImpl.newWriteOnlyTransaction();
201         tx.delete(LogicalDatastoreType.OPERATIONAL, path);
202         return tx;
203     }
204 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.