Clean up (DOM)DataTreeIdentifier methods
[mdsal.git] / binding / mdsal-binding-dom-adapter / src / test / java / org / opendaylight / mdsal / binding / dom / adapter / 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.mdsal.binding.dom.adapter;
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.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;
20
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.Set;
26 import java.util.concurrent.TimeUnit;
27 import org.junit.Test;
28 import org.opendaylight.mdsal.binding.api.DataBroker;
29 import org.opendaylight.mdsal.binding.api.DataObjectModification;
30 import org.opendaylight.mdsal.binding.api.DataObjectModification.ModificationType;
31 import org.opendaylight.mdsal.binding.api.DataTreeChangeListener;
32 import org.opendaylight.mdsal.binding.api.DataTreeIdentifier;
33 import org.opendaylight.mdsal.binding.api.DataTreeModification;
34 import org.opendaylight.mdsal.binding.api.WriteTransaction;
35 import org.opendaylight.mdsal.binding.dom.adapter.test.AbstractDataBrokerTest;
36 import org.opendaylight.mdsal.binding.runtime.spi.BindingRuntimeHelpers;
37 import org.opendaylight.mdsal.common.api.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
47 public class DataTreeChangeListenerTest extends AbstractDataBrokerTest {
48
49     private static final InstanceIdentifier<Top> TOP_PATH = InstanceIdentifier.create(Top.class);
50     private static final PathArgument TOP_ARGUMENT = TOP_PATH.getPathArguments().iterator().next();
51     private static final InstanceIdentifier<TopLevelList> FOO_PATH = path(TOP_FOO_KEY);
52     private static final PathArgument FOO_ARGUMENT = Iterables.getLast(FOO_PATH.getPathArguments());
53     private static final TopLevelList FOO_DATA = topLevelList(TOP_FOO_KEY, complexUsesAugment(USES_ONE_KEY));
54     private static final InstanceIdentifier<TopLevelList> BAR_PATH = path(TOP_BAR_KEY);
55     private static final PathArgument BAR_ARGUMENT = Iterables.getLast(BAR_PATH.getPathArguments());
56     private static final TopLevelList BAR_DATA = topLevelList(TOP_BAR_KEY);
57     private static final DataTreeIdentifier<Top> TOP_IDENTIFIER
58             = DataTreeIdentifier.of(LogicalDatastoreType.OPERATIONAL, TOP_PATH);
59
60     private static final Top TOP_INITIAL_DATA = top(FOO_DATA);
61
62     private BindingDOMDataBrokerAdapter dataBrokerImpl;
63
64     private static final class EventCapturingListener<T extends DataObject> implements DataTreeChangeListener<T> {
65
66         private SettableFuture<Collection<DataTreeModification<T>>> futureChanges = SettableFuture.create();
67
68         @Override
69         public void onDataTreeChanged(final Collection<DataTreeModification<T>> changes) {
70             futureChanges.set(changes);
71
72         }
73
74         Collection<DataTreeModification<T>> nextEvent() throws Exception {
75             final Collection<DataTreeModification<T>> result = futureChanges.get(200,TimeUnit.MILLISECONDS);
76             futureChanges = SettableFuture.create();
77             return result;
78         }
79     }
80
81     @Override
82     protected Set<YangModuleInfo> getModuleInfos() throws Exception {
83         return ImmutableSet.of(
84             BindingRuntimeHelpers.getYangModuleInfo(TwoLevelList.class),
85             BindingRuntimeHelpers.getYangModuleInfo(TreeComplexUsesAugment.class));
86     }
87
88     @Override
89     protected void setupWithDataBroker(final DataBroker dataBroker) {
90         dataBrokerImpl = (BindingDOMDataBrokerAdapter) dataBroker;
91     }
92
93     @Test
94     public void testTopLevelListener() throws Exception {
95         final EventCapturingListener<Top> listener = new EventCapturingListener<>();
96         dataBrokerImpl.registerDataTreeChangeListener(TOP_IDENTIFIER, listener);
97
98         createAndVerifyTop(listener);
99
100         putTx(BAR_PATH, BAR_DATA).commit().get();
101         final DataObjectModification<Top> afterBarPutEvent
102                 = Iterables.getOnlyElement(listener.nextEvent()).getRootNode();
103         verifyModification(afterBarPutEvent, TOP_ARGUMENT, ModificationType.SUBTREE_MODIFIED);
104         final DataObjectModification<TopLevelList> barPutMod = afterBarPutEvent.getModifiedChildListItem(
105                 TopLevelList.class, TOP_BAR_KEY);
106         assertNotNull(barPutMod);
107         verifyModification(barPutMod, BAR_ARGUMENT, ModificationType.WRITE);
108
109         deleteTx(BAR_PATH).commit().get();
110         final DataObjectModification<Top> afterBarDeleteEvent
111                 = Iterables.getOnlyElement(listener.nextEvent()).getRootNode();
112         verifyModification(afterBarDeleteEvent, TOP_ARGUMENT, ModificationType.SUBTREE_MODIFIED);
113         final DataObjectModification<TopLevelList> barDeleteMod = afterBarDeleteEvent.getModifiedChildListItem(
114                 TopLevelList.class, TOP_BAR_KEY);
115         verifyModification(barDeleteMod, BAR_ARGUMENT, ModificationType.DELETE);
116
117         dataBrokerImpl.registerDataTreeChangeListener(TOP_IDENTIFIER, listener).close();
118     }
119
120     @Test
121     public void testWildcardedListListener() throws Exception {
122         final EventCapturingListener<TopLevelList> listener = new EventCapturingListener<>();
123         final DataTreeIdentifier<TopLevelList> wildcard = DataTreeIdentifier.of(
124                 LogicalDatastoreType.OPERATIONAL, TOP_PATH.child(TopLevelList.class));
125         dataBrokerImpl.registerDataTreeChangeListener(wildcard, listener);
126
127         putTx(TOP_PATH, TOP_INITIAL_DATA).commit().get();
128
129         final DataTreeModification<TopLevelList> fooWriteEvent = Iterables.getOnlyElement(listener.nextEvent());
130         assertEquals(FOO_PATH, fooWriteEvent.getRootPath().path());
131         verifyModification(fooWriteEvent.getRootNode(), FOO_ARGUMENT, ModificationType.WRITE);
132
133         putTx(BAR_PATH, BAR_DATA).commit().get();
134         final DataTreeModification<TopLevelList> barWriteEvent = Iterables.getOnlyElement(listener.nextEvent());
135         assertEquals(BAR_PATH, barWriteEvent.getRootPath().path());
136         verifyModification(barWriteEvent.getRootNode(), BAR_ARGUMENT, ModificationType.WRITE);
137
138         deleteTx(BAR_PATH).commit().get();
139         final DataTreeModification<TopLevelList> barDeleteEvent = Iterables.getOnlyElement(listener.nextEvent());
140         assertEquals(BAR_PATH, barDeleteEvent.getRootPath().path());
141         verifyModification(barDeleteEvent.getRootNode(), BAR_ARGUMENT, ModificationType.DELETE);
142     }
143
144     @Test
145     public void testWildcardedListListenerWithPreexistingData() throws Exception {
146         putTx(TOP_PATH, TOP_INITIAL_DATA).commit().get();
147
148         final EventCapturingListener<TopLevelList> listener = new EventCapturingListener<>();
149         final DataTreeIdentifier<TopLevelList> wildcard = DataTreeIdentifier.of(
150                 LogicalDatastoreType.OPERATIONAL, TOP_PATH.child(TopLevelList.class));
151         dataBrokerImpl.registerDataTreeChangeListener(wildcard, listener);
152
153         final DataTreeModification<TopLevelList> fooWriteEvent = Iterables.getOnlyElement(listener.nextEvent());
154         assertEquals(FOO_PATH, fooWriteEvent.getRootPath().path());
155         verifyModification(fooWriteEvent.getRootNode(), FOO_ARGUMENT, ModificationType.WRITE);
156     }
157
158     private void createAndVerifyTop(final EventCapturingListener<Top> listener) throws Exception {
159         putTx(TOP_PATH,TOP_INITIAL_DATA).commit().get();
160         final Collection<DataTreeModification<Top>> events = listener.nextEvent();
161
162         assertFalse("Non empty collection should be received.",events.isEmpty());
163         final DataTreeModification<Top> initialWrite = Iterables.getOnlyElement(events);
164         final DataObjectModification<? extends DataObject> initialNode = initialWrite.getRootNode();
165         verifyModification(initialNode,TOP_PATH.getPathArguments().iterator().next(),ModificationType.WRITE);
166         assertEquals(TOP_INITIAL_DATA, initialNode.getDataAfter());
167     }
168
169     private static void verifyModification(final DataObjectModification<? extends DataObject> barWrite,
170             final PathArgument pathArg, final ModificationType eventType) {
171         assertEquals(pathArg.getType(), barWrite.getDataType());
172         assertEquals(eventType,barWrite.getModificationType());
173         assertEquals(pathArg, barWrite.getIdentifier());
174     }
175
176     private <T extends DataObject> WriteTransaction putTx(final InstanceIdentifier<T> path, final T data) {
177         final WriteTransaction tx = dataBrokerImpl.newWriteOnlyTransaction();
178         tx.put(LogicalDatastoreType.OPERATIONAL, path, data);
179         return tx;
180     }
181
182     private WriteTransaction deleteTx(final InstanceIdentifier<?> path) {
183         final WriteTransaction tx = dataBrokerImpl.newWriteOnlyTransaction();
184         tx.delete(LogicalDatastoreType.OPERATIONAL, path);
185         return tx;
186     }
187 }