Bug 2682 - Switch sal-binding-dom-it to sal-test-model
[controller.git] / opendaylight / md-sal / sal-binding-dom-it / src / test / java / org / opendaylight / controller / md / sal / binding / data / WildcardedDataChangeListenerTest.java
1 /*
2  * Copyright (c) 2014 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.controller.md.sal.binding.data;
9
10 import static org.junit.Assert.assertFalse;
11 import static org.junit.Assert.assertNotNull;
12 import static org.junit.Assert.assertTrue;
13
14 import java.util.Collections;
15 import java.util.concurrent.ExecutionException;
16 import java.util.concurrent.TimeUnit;
17 import java.util.concurrent.TimeoutException;
18
19 import org.junit.Test;
20 import org.opendaylight.controller.md.sal.common.api.data.DataChangeEvent;
21 import org.opendaylight.controller.sal.binding.api.data.DataChangeListener;
22 import org.opendaylight.controller.sal.binding.api.data.DataModificationTransaction;
23 import org.opendaylight.controller.sal.binding.api.data.DataProviderService;
24 import org.opendaylight.controller.sal.binding.test.AbstractDataServiceTest;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.augment.rev140709.TreeComplexUsesAugment;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.augment.rev140709.TreeComplexUsesAugmentBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.augment.rev140709.complex.from.grouping.ContainerWithUses;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.augment.rev140709.complex.from.grouping.ContainerWithUsesBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.augment.rev140709.complex.from.grouping.ListViaUses;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.augment.rev140709.complex.from.grouping.ListViaUsesBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.augment.rev140709.complex.from.grouping.ListViaUsesKey;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.list.rev140701.Top;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.list.rev140701.two.level.list.TopLevelList;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.list.rev140701.two.level.list.TopLevelListKey;
35 import org.opendaylight.yangtools.yang.binding.DataObject;
36 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
37
38 import com.google.common.util.concurrent.SettableFuture;
39
40 /**
41  * FIXME: THis test should be moved to compat test-suite
42  */
43 @SuppressWarnings("deprecation")
44 public class WildcardedDataChangeListenerTest extends AbstractDataServiceTest {
45
46     private static final TopLevelListKey TOP_LEVEL_LIST_0_KEY = new TopLevelListKey("test:0");
47     private static final TopLevelListKey TOP_LEVEL_LIST_1_KEY = new TopLevelListKey("test:1");
48
49     protected static final InstanceIdentifier<ListViaUses> DEEP_WILDCARDED_PATH = InstanceIdentifier
50             .builder(Top.class)
51             .child(TopLevelList.class) //
52             .augmentation(TreeComplexUsesAugment.class) //
53             .child(ListViaUses.class) //
54             .build();
55
56     private static final InstanceIdentifier<TreeComplexUsesAugment> NODE_0_TCU_PATH = InstanceIdentifier
57             .builder(Top.class)
58             .child(TopLevelList.class, TOP_LEVEL_LIST_0_KEY) //
59             .augmentation(TreeComplexUsesAugment.class) //
60             .build();
61
62     private static final InstanceIdentifier<TreeComplexUsesAugment> NODE_1_TCU_PATH = InstanceIdentifier
63             .builder(Top.class)
64             .child(TopLevelList.class, TOP_LEVEL_LIST_1_KEY) //
65             .augmentation(TreeComplexUsesAugment.class) //
66             .build();
67
68
69     private static final ListViaUsesKey LIST_VIA_USES_KEY = new ListViaUsesKey("test");
70
71     private static final InstanceIdentifier<ListViaUses> NODE_0_LVU_PATH = NODE_0_TCU_PATH.child(ListViaUses.class, LIST_VIA_USES_KEY);
72
73     private static final InstanceIdentifier<ListViaUses> NODE_1_LVU_PATH = NODE_1_TCU_PATH.child(ListViaUses.class, LIST_VIA_USES_KEY);
74
75     private static final InstanceIdentifier<ContainerWithUses> NODE_0_CWU_PATH =
76             NODE_0_TCU_PATH.child(ContainerWithUses.class);
77
78     private static final ContainerWithUses CWU= new ContainerWithUsesBuilder()//
79             .setLeafFromGrouping("some container value") //
80             .build();
81
82     private static final ListViaUses LVU = new ListViaUsesBuilder() //
83             .setKey(LIST_VIA_USES_KEY) //
84             .setName("john")
85             .build();
86
87     @Test
88     public void testSeparateWrites() throws InterruptedException, TimeoutException, ExecutionException {
89
90         DataProviderService dataBroker = testContext.getBindingDataBroker();
91
92         final SettableFuture<DataChangeEvent<InstanceIdentifier<?>, DataObject>> eventFuture = SettableFuture.create();
93         dataBroker.registerDataChangeListener(DEEP_WILDCARDED_PATH, new DataChangeListener() {
94             @Override
95             public void onDataChanged(final DataChangeEvent<InstanceIdentifier<?>, DataObject> dataChangeEvent) {
96                 eventFuture.set(dataChangeEvent);
97             }
98         });
99
100         DataModificationTransaction transaction = dataBroker.beginTransaction();
101         transaction.putOperationalData(NODE_0_CWU_PATH, CWU);
102         transaction.putOperationalData(NODE_0_LVU_PATH, LVU);
103         transaction.putOperationalData(NODE_1_LVU_PATH, LVU);
104         transaction.commit().get();
105
106         DataChangeEvent<InstanceIdentifier<?>, DataObject> event = eventFuture.get(1000, TimeUnit.MILLISECONDS);
107
108         validateEvent(event);
109     }
110
111     @Test
112     public void testWriteByReplace() throws InterruptedException, TimeoutException, ExecutionException {
113
114         DataProviderService dataBroker = testContext.getBindingDataBroker();
115
116         final SettableFuture<DataChangeEvent<InstanceIdentifier<?>, DataObject>> eventFuture = SettableFuture.create();
117         dataBroker.registerDataChangeListener(DEEP_WILDCARDED_PATH, new DataChangeListener() {
118             @Override
119             public void onDataChanged(final DataChangeEvent<InstanceIdentifier<?>, DataObject> dataChangeEvent) {
120                 eventFuture.set(dataChangeEvent);
121             }
122         });
123
124         DataModificationTransaction cwuTx = dataBroker.beginTransaction();
125         cwuTx.putOperationalData(NODE_0_CWU_PATH, CWU);
126         cwuTx.commit().get();
127
128         assertFalse(eventFuture.isDone());
129
130         DataModificationTransaction lvuTx = dataBroker.beginTransaction();
131
132         TreeComplexUsesAugment tcua = new TreeComplexUsesAugmentBuilder()
133                 .setListViaUses(Collections.singletonList(LVU)).build();
134
135         lvuTx.putOperationalData(NODE_0_TCU_PATH, tcua);
136         lvuTx.putOperationalData(NODE_1_LVU_PATH, LVU);
137         lvuTx.commit().get();
138
139         validateEvent(eventFuture.get(1000, TimeUnit.MILLISECONDS));
140     }
141
142     @Test
143     public void testNoChangeOnReplaceWithSameValue() throws InterruptedException, TimeoutException, ExecutionException {
144
145         DataProviderService dataBroker = testContext.getBindingDataBroker();
146
147         // We wrote initial state NODE_0_FLOW
148         DataModificationTransaction transaction = dataBroker.beginTransaction();
149         transaction.putOperationalData(NODE_0_LVU_PATH, LVU);
150         transaction.commit().get();
151
152         // We registered DataChangeListener
153         final SettableFuture<DataChangeEvent<InstanceIdentifier<?>, DataObject>> eventFuture = SettableFuture.create();
154         dataBroker.registerDataChangeListener(DEEP_WILDCARDED_PATH, new DataChangeListener() {
155
156             @Override
157             public void onDataChanged(final DataChangeEvent<InstanceIdentifier<?>, DataObject> dataChangeEvent) {
158                 eventFuture.set(dataChangeEvent);
159             }
160         });
161         assertFalse(eventFuture.isDone());
162
163         DataModificationTransaction secondTx = dataBroker.beginTransaction();
164         secondTx.putOperationalData(NODE_0_LVU_PATH, LVU);
165         secondTx.putOperationalData(NODE_1_LVU_PATH, LVU);
166         secondTx.commit().get();
167
168         DataChangeEvent<InstanceIdentifier<?>, DataObject> event = (eventFuture.get(1000, TimeUnit.MILLISECONDS));
169         assertNotNull(event);
170         // Data change should contains NODE_1 Flow - which was added
171         assertTrue(event.getCreatedOperationalData().containsKey(NODE_1_LVU_PATH));
172         // Data change must not containe NODE_0 Flow which was replaced with same value.
173         assertFalse(event.getUpdatedOperationalData().containsKey(NODE_0_LVU_PATH));
174     }
175
176     private static void validateEvent(final DataChangeEvent<InstanceIdentifier<?>, DataObject> event) {
177         assertNotNull(event);
178         assertTrue(event.getCreatedOperationalData().containsKey(NODE_1_LVU_PATH));
179         assertTrue(event.getCreatedOperationalData().containsKey(NODE_0_LVU_PATH));
180         assertFalse(event.getCreatedOperationalData().containsKey(NODE_0_CWU_PATH));
181     }
182
183 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.