AbstractConcurrentDataBrokerTest @deprecate-s the AbstractDataBrokerTest
[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
9 package org.opendaylight.controller.md.sal.binding.impl.test;
10
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
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.Before;
28 import org.junit.Test;
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.binding.impl.BindingDOMDataBrokerAdapter;
36 import org.opendaylight.controller.md.sal.binding.test.AbstractConcurrentDataBrokerTest;
37 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.augment.rev140709.TreeComplexUsesAugment;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.list.rev140701.Top;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.list.rev140701.TwoLevelList;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.list.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;
47
48 public class DataTreeChangeListenerTest extends AbstractConcurrentDataBrokerTest {
49
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 =
59             new DataTreeIdentifier<>(LogicalDatastoreType.OPERATIONAL, TOP_PATH);
60
61     private static final Top TOP_INITIAL_DATA = top(FOO_DATA);
62
63     private BindingDOMDataBrokerAdapter dataBrokerImpl;
64
65     private static final class EventCapturingListener<T extends DataObject> implements DataTreeChangeListener<T> {
66
67         private SettableFuture<Collection<DataTreeModification<T>>> changes = SettableFuture.create();
68
69         @Override
70         public void onDataTreeChanged(final Collection<DataTreeModification<T>> changes) {
71             this.changes.set(changes);
72
73         }
74
75         Collection<DataTreeModification<T>> nextEvent() throws Exception {
76             final Collection<DataTreeModification<T>> result = changes.get(200,TimeUnit.MILLISECONDS);
77             changes = SettableFuture.create();
78             return result;
79         }
80
81     }
82
83     @Override
84     protected Iterable<YangModuleInfo> getModuleInfos() throws Exception {
85         return ImmutableSet.of(
86                 BindingReflections.getModuleInfo(TwoLevelList.class),
87                 BindingReflections.getModuleInfo(TreeComplexUsesAugment.class)
88                 );
89     }
90
91     @Before
92     public void setupWithDataBroker() {
93         dataBrokerImpl = (BindingDOMDataBrokerAdapter) getDataBroker();
94     }
95
96     @Test
97     public void testTopLevelListener() throws Exception {
98         final EventCapturingListener<Top> listener = new EventCapturingListener<>();
99         dataBrokerImpl.registerDataTreeChangeListener(TOP_IDENTIFIER, listener);
100
101         createAndVerifyTop(listener);
102
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);
109
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);
115     }
116
117     @Test
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);
122
123         putTx(TOP_PATH, TOP_INITIAL_DATA).submit().checkedGet();
124
125         final DataTreeModification<TopLevelList> fooWriteEvent = Iterables.getOnlyElement(listener.nextEvent());
126         assertEquals(FOO_PATH, fooWriteEvent.getRootPath().getRootIdentifier());
127         verifyModification(fooWriteEvent.getRootNode(), FOO_ARGUMENT, ModificationType.WRITE);
128
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);
133
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);
138     }
139
140
141
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();
145
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());
151     }
152
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());
158     }
159
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);
163         return tx;
164     }
165
166     private WriteTransaction deleteTx(final InstanceIdentifier<?> path) {
167         final WriteTransaction tx = dataBrokerImpl.newWriteOnlyTransaction();
168         tx.delete(LogicalDatastoreType.OPERATIONAL, path);
169         return tx;
170     }
171 }