Migrate OSGI compendium reference
[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 @Deprecated
57 public class DataTreeChangeListenerTest extends AbstractConcurrentDataBrokerTest {
58
59     private static final InstanceIdentifier<Top> TOP_PATH = InstanceIdentifier.create(Top.class);
60     private static final PathArgument TOP_ARGUMENT = TOP_PATH.getPathArguments().iterator().next();
61     private static final InstanceIdentifier<TopLevelList> FOO_PATH = path(TOP_FOO_KEY);
62     private static final PathArgument FOO_ARGUMENT = Iterables.getLast(FOO_PATH.getPathArguments());
63     private static final TopLevelList FOO_DATA = topLevelList(TOP_FOO_KEY, complexUsesAugment(USES_ONE_KEY));
64     private static final InstanceIdentifier<TopLevelList> BAR_PATH = path(TOP_BAR_KEY);
65     private static final PathArgument BAR_ARGUMENT = Iterables.getLast(BAR_PATH.getPathArguments());
66     private static final TopLevelList BAR_DATA = topLevelList(TOP_BAR_KEY);
67     private static final DataTreeIdentifier<Top> TOP_IDENTIFIER =
68             new DataTreeIdentifier<>(LogicalDatastoreType.OPERATIONAL, TOP_PATH);
69
70     private static final Top TOP_INITIAL_DATA = top(FOO_DATA);
71
72     private BindingDOMDataBrokerAdapter dataBrokerImpl;
73
74     private static final class EventCapturingListener<T extends DataObject> implements DataTreeChangeListener<T> {
75
76         private SettableFuture<Collection<DataTreeModification<T>>> changes = SettableFuture.create();
77
78         @Override
79         public void onDataTreeChanged(final Collection<DataTreeModification<T>> modification) {
80             this.changes.set(modification);
81
82         }
83
84         Collection<DataTreeModification<T>> nextEvent() throws Exception {
85             final Collection<DataTreeModification<T>> result = changes.get(200,TimeUnit.MILLISECONDS);
86             changes = SettableFuture.create();
87             return result;
88         }
89     }
90
91     @Override
92     protected Set<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())
113                 .getRootNode();
114         verifyModification(afterBarPutEvent, TOP_ARGUMENT, ModificationType.SUBTREE_MODIFIED);
115         final DataObjectModification<TopLevelList> barPutMod = afterBarPutEvent
116                 .getModifiedChildListItem(TopLevelList.class, TOP_BAR_KEY);
117         assertNotNull(barPutMod);
118         verifyModification(barPutMod, BAR_ARGUMENT, ModificationType.WRITE);
119
120         deleteTx(BAR_PATH).submit().checkedGet();
121         final DataObjectModification<Top> afterBarDeleteEvent = Iterables.getOnlyElement(listener.nextEvent())
122                 .getRootNode();
123         verifyModification(afterBarDeleteEvent, TOP_ARGUMENT, ModificationType.SUBTREE_MODIFIED);
124         final DataObjectModification<TopLevelList> barDeleteMod = afterBarDeleteEvent
125                 .getModifiedChildListItem(TopLevelList.class, TOP_BAR_KEY);
126         verifyModification(barDeleteMod, BAR_ARGUMENT, ModificationType.DELETE);
127     }
128
129     @Test
130     public void testWildcardedListListener() throws Exception {
131         final EventCapturingListener<TopLevelList> listener = new EventCapturingListener<>();
132         final DataTreeIdentifier<TopLevelList> wildcard = new DataTreeIdentifier<>(LogicalDatastoreType.OPERATIONAL,
133                 TOP_PATH.child(TopLevelList.class));
134         dataBrokerImpl.registerDataTreeChangeListener(wildcard, listener);
135
136         putTx(TOP_PATH, TOP_INITIAL_DATA).submit().checkedGet();
137
138         final DataTreeModification<TopLevelList> fooWriteEvent = Iterables.getOnlyElement(listener.nextEvent());
139         assertEquals(FOO_PATH, fooWriteEvent.getRootPath().getRootIdentifier());
140         verifyModification(fooWriteEvent.getRootNode(), FOO_ARGUMENT, ModificationType.WRITE);
141
142         putTx(BAR_PATH, BAR_DATA).submit().checkedGet();
143         final DataTreeModification<TopLevelList> barWriteEvent = Iterables.getOnlyElement(listener.nextEvent());
144         assertEquals(BAR_PATH, barWriteEvent.getRootPath().getRootIdentifier());
145         verifyModification(barWriteEvent.getRootNode(), BAR_ARGUMENT, ModificationType.WRITE);
146
147         deleteTx(BAR_PATH).submit().checkedGet();
148         final DataTreeModification<TopLevelList> barDeleteEvent = Iterables.getOnlyElement(listener.nextEvent());
149         assertEquals(BAR_PATH, barDeleteEvent.getRootPath().getRootIdentifier());
150         verifyModification(barDeleteEvent.getRootNode(), BAR_ARGUMENT, ModificationType.DELETE);
151     }
152
153     @SuppressWarnings("unchecked")
154     @Test
155     public void testWildcardNotificationOfPreexistingData() {
156         InstanceIdentifier<Top> id = InstanceIdentifier.builder(Top.class).build();
157         ArrayList<TopLevelList> list = new ArrayList<>();
158         list.add(new TopLevelListBuilder().setName("name").build());
159         TopBuilder builder = new TopBuilder().setTopLevelList(list);
160
161         DataBroker dataBroker = getDataBroker();
162
163         WriteTransaction writeTransaction = dataBroker.newWriteOnlyTransaction();
164         writeTransaction.put(LogicalDatastoreType.OPERATIONAL, id, builder.build());
165         assertCommit(writeTransaction.submit());
166
167         DataTreeChangeListener<TopLevelList> listener = mock(DataTreeChangeListener.class);
168         InstanceIdentifier<TopLevelList> wildcard = InstanceIdentifier.builder(Top.class).child(TopLevelList.class)
169                 .build();
170         dataBroker.registerDataTreeChangeListener(new DataTreeIdentifier<>(LogicalDatastoreType.OPERATIONAL, wildcard),
171                 listener);
172
173         verify(listener, timeout(1000)).onDataTreeChanged(Matchers.anyObject());
174     }
175
176     private void createAndVerifyTop(final EventCapturingListener<Top> listener) throws Exception {
177         putTx(TOP_PATH,TOP_INITIAL_DATA).submit().checkedGet();
178         final Collection<DataTreeModification<Top>> events = listener.nextEvent();
179
180         assertFalse("Non empty collection should be received.",events.isEmpty());
181         final DataTreeModification<Top> initialWrite = Iterables.getOnlyElement(events);
182         final DataObjectModification<? extends DataObject> initialNode = initialWrite.getRootNode();
183         verifyModification(initialNode,TOP_PATH.getPathArguments().iterator().next(),ModificationType.WRITE);
184         assertEquals(TOP_INITIAL_DATA, initialNode.getDataAfter());
185     }
186
187     private static void verifyModification(final DataObjectModification<? extends DataObject> barWrite,
188             final PathArgument pathArg, final ModificationType eventType) {
189         assertEquals(pathArg.getType(), barWrite.getDataType());
190         assertEquals(eventType,barWrite.getModificationType());
191         assertEquals(pathArg, barWrite.getIdentifier());
192     }
193
194     private <T extends DataObject> WriteTransaction putTx(final InstanceIdentifier<T> path,final T data) {
195         final WriteTransaction tx = dataBrokerImpl.newWriteOnlyTransaction();
196         tx.put(LogicalDatastoreType.OPERATIONAL, path, data);
197         return tx;
198     }
199
200     private WriteTransaction deleteTx(final InstanceIdentifier<?> path) {
201         final WriteTransaction tx = dataBrokerImpl.newWriteOnlyTransaction();
202         tx.delete(LogicalDatastoreType.OPERATIONAL, path);
203         return tx;
204     }
205 }