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;
21 import org.opendaylight.mdsal.binding.dom.adapter.BindingDOMDataBrokerAdapter;
22 import com.google.common.collect.ImmutableSet;
23 import com.google.common.collect.Iterables;
24 import com.google.common.util.concurrent.SettableFuture;
25 import java.util.Collection;
26 import java.util.concurrent.TimeUnit;
27 import org.junit.Test;
28 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
29 import org.opendaylight.controller.md.sal.binding.api.DataObjectModification;
30 import org.opendaylight.controller.md.sal.binding.api.DataObjectModification.ModificationType;
31 import org.opendaylight.controller.md.sal.binding.api.DataTreeChangeListener;
32 import org.opendaylight.controller.md.sal.binding.api.DataTreeIdentifier;
33 import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
34 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
35 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.augment.rev140709.TreeComplexUsesAugment;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.binding.rev140701.Top;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.binding.rev140701.TwoLevelList;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.binding.rev140701.two.level.list.TopLevelList;
40 import org.opendaylight.yangtools.yang.binding.DataObject;
41 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
42 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier.PathArgument;
43 import org.opendaylight.yangtools.yang.binding.YangModuleInfo;
44 import org.opendaylight.yangtools.yang.binding.util.BindingReflections;
46 public class DataTreeChangeListenerTest extends AbstractDataBrokerTest {
48 private static final InstanceIdentifier<Top> TOP_PATH = InstanceIdentifier.create(Top.class);
49 private static final PathArgument TOP_ARGUMENT= TOP_PATH.getPathArguments().iterator().next();
50 private static final InstanceIdentifier<TopLevelList> FOO_PATH = path(TOP_FOO_KEY);
51 private static final PathArgument FOO_ARGUMENT = Iterables.getLast(FOO_PATH.getPathArguments());
52 private static final TopLevelList FOO_DATA = topLevelList(TOP_FOO_KEY, complexUsesAugment(USES_ONE_KEY));
53 private static final InstanceIdentifier<TopLevelList> BAR_PATH = path(TOP_BAR_KEY);
54 private static final PathArgument BAR_ARGUMENT = Iterables.getLast(BAR_PATH.getPathArguments());
55 private static final TopLevelList BAR_DATA = topLevelList(TOP_BAR_KEY);
56 private static final DataTreeIdentifier<Top> TOP_IDENTIFIER = new DataTreeIdentifier<Top>(LogicalDatastoreType.OPERATIONAL,
59 private static final Top TOP_INITIAL_DATA = top(FOO_DATA);
61 private BindingDOMDataBrokerAdapter dataBrokerImpl;
63 private static final class EventCapturingListener<T extends DataObject> implements DataTreeChangeListener<T> {
65 private SettableFuture<Collection<DataTreeModification<T>>> changes = SettableFuture.create();
68 public void onDataTreeChanged(final Collection<DataTreeModification<T>> changes) {
69 this.changes.set(changes);
73 Collection<DataTreeModification<T>> nextEvent() throws Exception {
74 final Collection<DataTreeModification<T>> result = changes.get(200,TimeUnit.MILLISECONDS);
75 changes = SettableFuture.create();
82 protected Iterable<YangModuleInfo> getModuleInfos() throws Exception {
83 return ImmutableSet.of(
84 BindingReflections.getModuleInfo(TwoLevelList.class),
85 BindingReflections.getModuleInfo(TreeComplexUsesAugment.class)
90 protected void setupWithDataBroker(final DataBroker dataBroker) {
91 dataBrokerImpl = (BindingDOMDataBrokerAdapter) dataBroker;
95 public void testTopLevelListener() throws Exception {
96 final EventCapturingListener<Top> listener = new EventCapturingListener<>();
97 dataBrokerImpl.registerDataTreeChangeListener(TOP_IDENTIFIER, listener);
99 createAndVerifyTop(listener);
101 putTx(BAR_PATH, BAR_DATA).submit().checkedGet();
102 final DataObjectModification<Top> afterBarPutEvent = Iterables.getOnlyElement(listener.nextEvent()).getRootNode();
103 verifyModification(afterBarPutEvent, TOP_ARGUMENT, ModificationType.SUBTREE_MODIFIED);
104 final DataObjectModification<TopLevelList> barPutMod = afterBarPutEvent.getModifiedChildListItem(TopLevelList.class, TOP_BAR_KEY);
105 assertNotNull(barPutMod);
106 verifyModification(barPutMod, BAR_ARGUMENT, ModificationType.WRITE);
108 deleteTx(BAR_PATH).submit().checkedGet();
109 final DataObjectModification<Top> afterBarDeleteEvent = Iterables.getOnlyElement(listener.nextEvent()).getRootNode();
110 verifyModification(afterBarDeleteEvent, TOP_ARGUMENT, ModificationType.SUBTREE_MODIFIED);
111 final DataObjectModification<TopLevelList> barDeleteMod = afterBarDeleteEvent.getModifiedChildListItem(TopLevelList.class, TOP_BAR_KEY);
112 verifyModification(barDeleteMod, BAR_ARGUMENT, ModificationType.DELETE);
116 public void testWildcardedListListener() throws Exception {
117 final EventCapturingListener<TopLevelList> listener = new EventCapturingListener<>();
118 final DataTreeIdentifier<TopLevelList> wildcard = new DataTreeIdentifier<>(LogicalDatastoreType.OPERATIONAL, TOP_PATH.child(TopLevelList.class));
119 dataBrokerImpl.registerDataTreeChangeListener(wildcard, listener);
121 putTx(TOP_PATH, TOP_INITIAL_DATA).submit().checkedGet();
123 final DataTreeModification<TopLevelList> fooWriteEvent = Iterables.getOnlyElement(listener.nextEvent());
124 assertEquals(FOO_PATH, fooWriteEvent.getRootPath().getRootIdentifier());
125 verifyModification(fooWriteEvent.getRootNode(), FOO_ARGUMENT, ModificationType.WRITE);
127 putTx(BAR_PATH, BAR_DATA).submit().checkedGet();
128 final DataTreeModification<TopLevelList> barWriteEvent = Iterables.getOnlyElement(listener.nextEvent());
129 assertEquals(BAR_PATH, barWriteEvent.getRootPath().getRootIdentifier());
130 verifyModification(barWriteEvent.getRootNode(), BAR_ARGUMENT, ModificationType.WRITE);
132 deleteTx(BAR_PATH).submit().checkedGet();
133 final DataTreeModification<TopLevelList> barDeleteEvent = Iterables.getOnlyElement(listener.nextEvent());
134 assertEquals(BAR_PATH, barDeleteEvent.getRootPath().getRootIdentifier());
135 verifyModification(barDeleteEvent.getRootNode(), BAR_ARGUMENT, ModificationType.DELETE);
140 private void createAndVerifyTop(final EventCapturingListener<Top> listener) throws Exception {
141 putTx(TOP_PATH,TOP_INITIAL_DATA).submit().checkedGet();
142 final Collection<DataTreeModification<Top>> events = listener.nextEvent();
144 assertFalse("Non empty collection should be received.",events.isEmpty());
145 final DataTreeModification<Top> initialWrite = Iterables.getOnlyElement(events);
146 final DataObjectModification<? extends DataObject> initialNode = initialWrite.getRootNode();
147 verifyModification(initialNode,TOP_PATH.getPathArguments().iterator().next(),ModificationType.WRITE);
148 assertEquals(TOP_INITIAL_DATA, initialNode.getDataAfter());
151 private void verifyModification(final DataObjectModification<? extends DataObject> barWrite, final PathArgument pathArg,
152 final ModificationType eventType) {
153 assertEquals(pathArg.getType(), barWrite.getDataType());
154 assertEquals(eventType,barWrite.getModificationType());
155 assertEquals(pathArg, barWrite.getIdentifier());
158 private <T extends DataObject> WriteTransaction putTx(final InstanceIdentifier<T> path,final T data) {
159 final WriteTransaction tx = dataBrokerImpl.newWriteOnlyTransaction();
160 tx.put(LogicalDatastoreType.OPERATIONAL, path, data);
164 private WriteTransaction deleteTx(final InstanceIdentifier<?> path) {
165 final WriteTransaction tx = dataBrokerImpl.newWriteOnlyTransaction();
166 tx.delete(LogicalDatastoreType.OPERATIONAL, path);