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
9 package org.opendaylight.mdsal.binding.dom.adapter.test;
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.opendaylight.controller.md.sal.test.model.util.ListsBindingUtils.TOP_BAR_KEY;
15 import static org.opendaylight.controller.md.sal.test.model.util.ListsBindingUtils.TOP_FOO_KEY;
16 import static org.opendaylight.controller.md.sal.test.model.util.ListsBindingUtils.USES_ONE_KEY;
17 import static org.opendaylight.controller.md.sal.test.model.util.ListsBindingUtils.complexUsesAugment;
18 import static org.opendaylight.controller.md.sal.test.model.util.ListsBindingUtils.path;
19 import static org.opendaylight.controller.md.sal.test.model.util.ListsBindingUtils.top;
20 import static org.opendaylight.controller.md.sal.test.model.util.ListsBindingUtils.topLevelList;
22 import org.opendaylight.mdsal.binding.api.DataBroker;
23 import org.opendaylight.mdsal.binding.api.DataObjectModification;
24 import org.opendaylight.mdsal.binding.api.DataTreeChangeListener;
25 import org.opendaylight.mdsal.binding.api.DataTreeIdentifier;
26 import org.opendaylight.mdsal.binding.api.DataTreeModification;
27 import org.opendaylight.mdsal.binding.api.WriteTransaction;
28 import org.opendaylight.mdsal.binding.api.DataObjectModification.ModificationType;
30 import org.opendaylight.mdsal.binding.dom.adapter.BindingDOMDataBrokerAdapter;
31 import com.google.common.collect.ImmutableSet;
32 import com.google.common.collect.Iterables;
33 import com.google.common.util.concurrent.SettableFuture;
34 import java.util.Collection;
35 import java.util.concurrent.TimeUnit;
36 import org.junit.Test;
37 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.augment.rev140709.TreeComplexUsesAugment;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.binding.rev140701.Top;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.binding.rev140701.TwoLevelList;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.binding.rev140701.two.level.list.TopLevelList;
42 import org.opendaylight.yangtools.yang.binding.DataObject;
43 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
44 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier.PathArgument;
45 import org.opendaylight.yangtools.yang.binding.YangModuleInfo;
46 import org.opendaylight.yangtools.yang.binding.util.BindingReflections;
48 public class DataTreeChangeListenerTest extends AbstractDataBrokerTest {
50 private static final InstanceIdentifier<Top> TOP_PATH = InstanceIdentifier.create(Top.class);
51 private static final PathArgument TOP_ARGUMENT= TOP_PATH.getPathArguments().iterator().next();
52 private static final InstanceIdentifier<TopLevelList> FOO_PATH = path(TOP_FOO_KEY);
53 private static final PathArgument 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 PathArgument 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 = new DataTreeIdentifier<Top>(LogicalDatastoreType.OPERATIONAL,
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> {
67 private SettableFuture<Collection<DataTreeModification<T>>> changes = SettableFuture.create();
70 public void onDataTreeChanged(final Collection<DataTreeModification<T>> changes) {
71 this.changes.set(changes);
75 Collection<DataTreeModification<T>> nextEvent() throws Exception {
76 final Collection<DataTreeModification<T>> result = changes.get(200,TimeUnit.MILLISECONDS);
77 changes = SettableFuture.create();
84 protected Iterable<YangModuleInfo> getModuleInfos() throws Exception {
85 return ImmutableSet.of(
86 BindingReflections.getModuleInfo(TwoLevelList.class),
87 BindingReflections.getModuleInfo(TreeComplexUsesAugment.class)
92 protected void setupWithDataBroker(final DataBroker dataBroker) {
93 dataBrokerImpl = (BindingDOMDataBrokerAdapter) dataBroker;
97 public void testTopLevelListener() throws Exception {
98 final EventCapturingListener<Top> listener = new EventCapturingListener<>();
99 dataBrokerImpl.registerDataTreeChangeListener(TOP_IDENTIFIER, listener);
101 createAndVerifyTop(listener);
103 putTx(BAR_PATH, BAR_DATA).submit().checkedGet();
104 final DataObjectModification<Top> afterBarPutEvent = Iterables.getOnlyElement(listener.nextEvent()).getRootNode();
105 verifyModification(afterBarPutEvent, TOP_ARGUMENT, ModificationType.SUBTREE_MODIFIED);
106 final DataObjectModification<TopLevelList> barPutMod = afterBarPutEvent.getModifiedChildListItem(TopLevelList.class, TOP_BAR_KEY);
107 assertNotNull(barPutMod);
108 verifyModification(barPutMod, BAR_ARGUMENT, ModificationType.WRITE);
110 deleteTx(BAR_PATH).submit().checkedGet();
111 final DataObjectModification<Top> afterBarDeleteEvent = Iterables.getOnlyElement(listener.nextEvent()).getRootNode();
112 verifyModification(afterBarDeleteEvent, TOP_ARGUMENT, ModificationType.SUBTREE_MODIFIED);
113 final DataObjectModification<TopLevelList> barDeleteMod = afterBarDeleteEvent.getModifiedChildListItem(TopLevelList.class, TOP_BAR_KEY);
114 verifyModification(barDeleteMod, BAR_ARGUMENT, ModificationType.DELETE);
118 public void testWildcardedListListener() throws Exception {
119 final EventCapturingListener<TopLevelList> listener = new EventCapturingListener<>();
120 final DataTreeIdentifier<TopLevelList> wildcard = new DataTreeIdentifier<>(LogicalDatastoreType.OPERATIONAL, TOP_PATH.child(TopLevelList.class));
121 dataBrokerImpl.registerDataTreeChangeListener(wildcard, listener);
123 putTx(TOP_PATH, TOP_INITIAL_DATA).submit().checkedGet();
125 final DataTreeModification<TopLevelList> fooWriteEvent = Iterables.getOnlyElement(listener.nextEvent());
126 assertEquals(FOO_PATH, fooWriteEvent.getRootPath().getRootIdentifier());
127 verifyModification(fooWriteEvent.getRootNode(), FOO_ARGUMENT, ModificationType.WRITE);
129 putTx(BAR_PATH, BAR_DATA).submit().checkedGet();
130 final DataTreeModification<TopLevelList> barWriteEvent = Iterables.getOnlyElement(listener.nextEvent());
131 assertEquals(BAR_PATH, barWriteEvent.getRootPath().getRootIdentifier());
132 verifyModification(barWriteEvent.getRootNode(), BAR_ARGUMENT, ModificationType.WRITE);
134 deleteTx(BAR_PATH).submit().checkedGet();
135 final DataTreeModification<TopLevelList> barDeleteEvent = Iterables.getOnlyElement(listener.nextEvent());
136 assertEquals(BAR_PATH, barDeleteEvent.getRootPath().getRootIdentifier());
137 verifyModification(barDeleteEvent.getRootNode(), BAR_ARGUMENT, ModificationType.DELETE);
142 private void createAndVerifyTop(final EventCapturingListener<Top> listener) throws Exception {
143 putTx(TOP_PATH,TOP_INITIAL_DATA).submit().checkedGet();
144 final Collection<DataTreeModification<Top>> events = listener.nextEvent();
146 assertFalse("Non empty collection should be received.",events.isEmpty());
147 final DataTreeModification<Top> initialWrite = Iterables.getOnlyElement(events);
148 final DataObjectModification<? extends DataObject> initialNode = initialWrite.getRootNode();
149 verifyModification(initialNode,TOP_PATH.getPathArguments().iterator().next(),ModificationType.WRITE);
150 assertEquals(TOP_INITIAL_DATA, initialNode.getDataAfter());
153 private void verifyModification(final DataObjectModification<? extends DataObject> barWrite, final PathArgument pathArg,
154 final ModificationType eventType) {
155 assertEquals(pathArg.getType(), barWrite.getDataType());
156 assertEquals(eventType,barWrite.getModificationType());
157 assertEquals(pathArg, barWrite.getIdentifier());
160 private <T extends DataObject> WriteTransaction putTx(final InstanceIdentifier<T> path,final T data) {
161 final WriteTransaction tx = dataBrokerImpl.newWriteOnlyTransaction();
162 tx.put(LogicalDatastoreType.OPERATIONAL, path, data);
166 private WriteTransaction deleteTx(final InstanceIdentifier<?> path) {
167 final WriteTransaction tx = dataBrokerImpl.newWriteOnlyTransaction();
168 tx.delete(LogicalDatastoreType.OPERATIONAL, path);