2 * Copyright (c) 2014, 2015 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.mdsal.binding.dom.adapter;
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.opendaylight.mdsal.binding.test.model.util.ListsBindingUtils.TOP_BAR_KEY;
14 import static org.opendaylight.mdsal.binding.test.model.util.ListsBindingUtils.TOP_FOO_KEY;
15 import static org.opendaylight.mdsal.binding.test.model.util.ListsBindingUtils.USES_ONE_KEY;
16 import static org.opendaylight.mdsal.binding.test.model.util.ListsBindingUtils.complexUsesAugment;
17 import static org.opendaylight.mdsal.binding.test.model.util.ListsBindingUtils.path;
18 import static org.opendaylight.mdsal.binding.test.model.util.ListsBindingUtils.top;
19 import static org.opendaylight.mdsal.binding.test.model.util.ListsBindingUtils.topLevelList;
21 import com.google.common.collect.ImmutableSet;
22 import com.google.common.collect.Iterables;
23 import com.google.common.util.concurrent.SettableFuture;
24 import java.util.Collection;
25 import java.util.List;
27 import java.util.concurrent.TimeUnit;
28 import org.junit.Test;
29 import org.opendaylight.mdsal.binding.api.DataBroker;
30 import org.opendaylight.mdsal.binding.api.DataObjectModification;
31 import org.opendaylight.mdsal.binding.api.DataObjectModification.ModificationType;
32 import org.opendaylight.mdsal.binding.api.DataTreeChangeListener;
33 import org.opendaylight.mdsal.binding.api.DataTreeIdentifier;
34 import org.opendaylight.mdsal.binding.api.DataTreeModification;
35 import org.opendaylight.mdsal.binding.api.WriteTransaction;
36 import org.opendaylight.mdsal.binding.dom.adapter.test.AbstractDataBrokerTest;
37 import org.opendaylight.mdsal.binding.runtime.spi.BindingRuntimeHelpers;
38 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.augment.rev140709.TreeComplexUsesAugment;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.binding.rev140701.Top;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.binding.rev140701.TwoLevelList;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.binding.rev140701.two.level.list.TopLevelList;
43 import org.opendaylight.yangtools.yang.binding.DataObject;
44 import org.opendaylight.yangtools.yang.binding.DataObjectStep;
45 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
46 import org.opendaylight.yangtools.yang.binding.YangModuleInfo;
48 public class DataTreeChangeListenerTest extends AbstractDataBrokerTest {
50 private static final InstanceIdentifier<Top> TOP_PATH = InstanceIdentifier.create(Top.class);
51 private static final DataObjectStep<?> TOP_ARGUMENT = TOP_PATH.getPathArguments().iterator().next();
52 private static final InstanceIdentifier<TopLevelList> FOO_PATH = path(TOP_FOO_KEY);
53 private static final DataObjectStep<?> FOO_ARGUMENT = Iterables.getLast(FOO_PATH.getPathArguments());
54 private static final TopLevelList FOO_DATA = topLevelList(TOP_FOO_KEY, complexUsesAugment(USES_ONE_KEY));
55 private static final InstanceIdentifier<TopLevelList> BAR_PATH = path(TOP_BAR_KEY);
56 private static final DataObjectStep<?> BAR_ARGUMENT = Iterables.getLast(BAR_PATH.getPathArguments());
57 private static final TopLevelList BAR_DATA = topLevelList(TOP_BAR_KEY);
58 private static final DataTreeIdentifier<Top> TOP_IDENTIFIER
59 = DataTreeIdentifier.of(LogicalDatastoreType.OPERATIONAL, TOP_PATH);
61 private static final Top TOP_INITIAL_DATA = top(FOO_DATA);
63 private BindingDOMDataBrokerAdapter dataBrokerImpl;
65 private static final class EventCapturingListener<T extends DataObject> implements DataTreeChangeListener<T> {
66 private SettableFuture<List<DataTreeModification<T>>> futureChanges = SettableFuture.create();
69 public void onDataTreeChanged(final List<DataTreeModification<T>> changes) {
70 futureChanges.set(changes);
73 Collection<DataTreeModification<T>> nextEvent() throws Exception {
74 final var result = futureChanges.get(200,TimeUnit.MILLISECONDS);
75 futureChanges = SettableFuture.create();
81 protected Set<YangModuleInfo> getModuleInfos() throws Exception {
82 return ImmutableSet.of(
83 BindingRuntimeHelpers.getYangModuleInfo(TwoLevelList.class),
84 BindingRuntimeHelpers.getYangModuleInfo(TreeComplexUsesAugment.class));
88 protected void setupWithDataBroker(final DataBroker dataBroker) {
89 dataBrokerImpl = (BindingDOMDataBrokerAdapter) dataBroker;
93 public void testTopLevelListener() throws Exception {
94 final EventCapturingListener<Top> listener = new EventCapturingListener<>();
95 dataBrokerImpl.registerDataTreeChangeListener(TOP_IDENTIFIER, listener);
97 createAndVerifyTop(listener);
99 putTx(BAR_PATH, BAR_DATA).commit().get();
100 final DataObjectModification<Top> afterBarPutEvent
101 = Iterables.getOnlyElement(listener.nextEvent()).getRootNode();
102 verifyModification(afterBarPutEvent, TOP_ARGUMENT, ModificationType.SUBTREE_MODIFIED);
103 final DataObjectModification<TopLevelList> barPutMod = afterBarPutEvent.getModifiedChildListItem(
104 TopLevelList.class, TOP_BAR_KEY);
105 assertNotNull(barPutMod);
106 verifyModification(barPutMod, BAR_ARGUMENT, ModificationType.WRITE);
108 deleteTx(BAR_PATH).commit().get();
109 final DataObjectModification<Top> afterBarDeleteEvent
110 = Iterables.getOnlyElement(listener.nextEvent()).getRootNode();
111 verifyModification(afterBarDeleteEvent, TOP_ARGUMENT, ModificationType.SUBTREE_MODIFIED);
112 final DataObjectModification<TopLevelList> barDeleteMod = afterBarDeleteEvent.getModifiedChildListItem(
113 TopLevelList.class, TOP_BAR_KEY);
114 verifyModification(barDeleteMod, BAR_ARGUMENT, ModificationType.DELETE);
116 dataBrokerImpl.registerDataTreeChangeListener(TOP_IDENTIFIER, listener).close();
120 public void testWildcardedListListener() throws Exception {
121 final EventCapturingListener<TopLevelList> listener = new EventCapturingListener<>();
122 final DataTreeIdentifier<TopLevelList> wildcard = DataTreeIdentifier.of(
123 LogicalDatastoreType.OPERATIONAL, TOP_PATH.child(TopLevelList.class));
124 dataBrokerImpl.registerDataTreeChangeListener(wildcard, listener);
126 putTx(TOP_PATH, TOP_INITIAL_DATA).commit().get();
128 final DataTreeModification<TopLevelList> fooWriteEvent = Iterables.getOnlyElement(listener.nextEvent());
129 assertEquals(FOO_PATH, fooWriteEvent.getRootPath().path());
130 verifyModification(fooWriteEvent.getRootNode(), FOO_ARGUMENT, ModificationType.WRITE);
132 putTx(BAR_PATH, BAR_DATA).commit().get();
133 final DataTreeModification<TopLevelList> barWriteEvent = Iterables.getOnlyElement(listener.nextEvent());
134 assertEquals(BAR_PATH, barWriteEvent.getRootPath().path());
135 verifyModification(barWriteEvent.getRootNode(), BAR_ARGUMENT, ModificationType.WRITE);
137 deleteTx(BAR_PATH).commit().get();
138 final DataTreeModification<TopLevelList> barDeleteEvent = Iterables.getOnlyElement(listener.nextEvent());
139 assertEquals(BAR_PATH, barDeleteEvent.getRootPath().path());
140 verifyModification(barDeleteEvent.getRootNode(), BAR_ARGUMENT, ModificationType.DELETE);
144 public void testWildcardedListListenerWithPreexistingData() throws Exception {
145 putTx(TOP_PATH, TOP_INITIAL_DATA).commit().get();
147 final EventCapturingListener<TopLevelList> listener = new EventCapturingListener<>();
148 final DataTreeIdentifier<TopLevelList> wildcard = DataTreeIdentifier.of(
149 LogicalDatastoreType.OPERATIONAL, TOP_PATH.child(TopLevelList.class));
150 dataBrokerImpl.registerDataTreeChangeListener(wildcard, listener);
152 final DataTreeModification<TopLevelList> fooWriteEvent = Iterables.getOnlyElement(listener.nextEvent());
153 assertEquals(FOO_PATH, fooWriteEvent.getRootPath().path());
154 verifyModification(fooWriteEvent.getRootNode(), FOO_ARGUMENT, ModificationType.WRITE);
157 private void createAndVerifyTop(final EventCapturingListener<Top> listener) throws Exception {
158 putTx(TOP_PATH,TOP_INITIAL_DATA).commit().get();
159 final Collection<DataTreeModification<Top>> events = listener.nextEvent();
161 assertFalse("Non empty collection should be received.",events.isEmpty());
162 final DataTreeModification<Top> initialWrite = Iterables.getOnlyElement(events);
163 final DataObjectModification<? extends DataObject> initialNode = initialWrite.getRootNode();
164 verifyModification(initialNode,TOP_PATH.getPathArguments().iterator().next(),ModificationType.WRITE);
165 assertEquals(TOP_INITIAL_DATA, initialNode.dataAfter());
168 private static void verifyModification(final DataObjectModification<? extends DataObject> barWrite,
169 final DataObjectStep<?> pathArg, final ModificationType eventType) {
170 assertEquals(pathArg.type(), barWrite.dataType());
171 assertEquals(eventType,barWrite.modificationType());
172 assertEquals(pathArg, barWrite.step());
175 private <T extends DataObject> WriteTransaction putTx(final InstanceIdentifier<T> path, final T data) {
176 final WriteTransaction tx = dataBrokerImpl.newWriteOnlyTransaction();
177 tx.put(LogicalDatastoreType.OPERATIONAL, path, data);
181 private WriteTransaction deleteTx(final InstanceIdentifier<?> path) {
182 final WriteTransaction tx = dataBrokerImpl.newWriteOnlyTransaction();
183 tx.delete(LogicalDatastoreType.OPERATIONAL, path);