6b4cc5779806288d4f0cf26116d2ef3f7ca10ba1
[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.flow.inventory.rev130819.FlowCapableNode;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowId;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.Table;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.TableBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.TableKey;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowKey;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.table.features.TableFeatures;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.table.features.TableFeaturesBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.table.features.TableFeaturesKey;
40 import org.opendaylight.yangtools.yang.binding.DataObject;
41 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
42
43 import com.google.common.util.concurrent.SettableFuture;
44
45 /*
46  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
47  *
48  * This program and the accompanying materials are made available under the
49  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
50  * and is available at http://www.eclipse.org/legal/epl-v10.html
51  */
52 public class WildcardedDataChangeListenerTest extends AbstractDataServiceTest {
53
54     private static final NodeKey NODE_0_KEY = new NodeKey(new NodeId("test:0"));
55     private static final NodeKey NODE_1_KEY = new NodeKey(new NodeId("test:1"));
56
57     public static final InstanceIdentifier<Flow> DEEP_WILDCARDED_PATH = InstanceIdentifier.builder(Nodes.class)
58             .child(Node.class) //
59             .augmentation(FlowCapableNode.class) //
60             .child(Table.class) //
61             .child(Flow.class) //
62             .build();
63
64     private static final TableKey TABLE_0_KEY = new TableKey((short) 0);
65     private static final TableFeaturesKey TABLE_FEATURES_KEY = new TableFeaturesKey((short) 0);
66
67     private static final InstanceIdentifier<Table> NODE_0_TABLE_PATH = InstanceIdentifier.builder(Nodes.class)
68             .child(Node.class, NODE_0_KEY) //
69             .augmentation(FlowCapableNode.class) //
70             .child(Table.class, TABLE_0_KEY) //
71             .build();
72
73     private static final InstanceIdentifier<Table> NODE_1_TABLE_PATH = InstanceIdentifier.builder(Nodes.class)
74             .child(Node.class, NODE_1_KEY) //
75             .augmentation(FlowCapableNode.class) //
76             .child(Table.class, TABLE_0_KEY) //
77             .build();
78
79     private static final FlowKey FLOW_KEY = new FlowKey(new FlowId("test"));
80
81     private static final InstanceIdentifier<Flow> NODE_0_FLOW_PATH = NODE_0_TABLE_PATH.child(Flow.class, FLOW_KEY);
82
83     private static final InstanceIdentifier<Flow> NODE_1_FLOW_PATH = NODE_1_TABLE_PATH.child(Flow.class, FLOW_KEY);
84
85     private static final InstanceIdentifier<TableFeatures> NODE_0_TABLE_FEATURES_PATH =
86             NODE_0_TABLE_PATH.child(TableFeatures.class, TABLE_FEATURES_KEY);
87
88     private static final TableFeatures TABLE_FEATURES = new TableFeaturesBuilder()//
89             .setKey(TABLE_FEATURES_KEY) //
90             .setName("Foo") //
91             .setMaxEntries(1000L) //
92             .build();
93
94     private static final Flow FLOW = new FlowBuilder() //
95             .setKey(FLOW_KEY) //
96             .setBarrier(true) //
97             .setStrict(true) //
98             .build();
99
100     @Test
101     public void testSepareteWrites() throws InterruptedException, TimeoutException, ExecutionException {
102
103         DataProviderService dataBroker = testContext.getBindingDataBroker();
104
105         final SettableFuture<DataChangeEvent<InstanceIdentifier<?>, DataObject>> eventFuture = SettableFuture.create();
106         dataBroker.registerDataChangeListener(DEEP_WILDCARDED_PATH, new DataChangeListener() {
107
108             @Override
109             public void onDataChanged(final DataChangeEvent<InstanceIdentifier<?>, DataObject> dataChangeEvent) {
110                 eventFuture.set(dataChangeEvent);
111             }
112         });
113
114         DataModificationTransaction transaction = dataBroker.beginTransaction();
115         transaction.putOperationalData(NODE_0_TABLE_FEATURES_PATH, TABLE_FEATURES);
116         transaction.putOperationalData(NODE_0_FLOW_PATH, FLOW);
117         transaction.putOperationalData(NODE_1_FLOW_PATH, FLOW);
118         transaction.commit().get();
119
120         DataChangeEvent<InstanceIdentifier<?>, DataObject> event = eventFuture.get(1000, TimeUnit.MILLISECONDS);
121
122         validateEvent(event);
123     }
124
125     @Test
126     public void testWriteByReplace() throws InterruptedException, TimeoutException, ExecutionException {
127
128         DataProviderService dataBroker = testContext.getBindingDataBroker();
129
130         final SettableFuture<DataChangeEvent<InstanceIdentifier<?>, DataObject>> eventFuture = SettableFuture.create();
131         dataBroker.registerDataChangeListener(DEEP_WILDCARDED_PATH, new DataChangeListener() {
132
133             @Override
134             public void onDataChanged(final DataChangeEvent<InstanceIdentifier<?>, DataObject> dataChangeEvent) {
135                 eventFuture.set(dataChangeEvent);
136             }
137         });
138
139         DataModificationTransaction tableTx = dataBroker.beginTransaction();
140         tableTx.putOperationalData(NODE_0_TABLE_FEATURES_PATH, TABLE_FEATURES);
141         tableTx.commit().get();
142
143         assertFalse(eventFuture.isDone());
144
145         DataModificationTransaction flowTx = dataBroker.beginTransaction();
146
147         Table table = new TableBuilder() //
148                 .setKey(TABLE_0_KEY) //
149                 .setFlow(Collections.singletonList(FLOW)) //
150                 .build();
151
152         flowTx.putOperationalData(NODE_0_TABLE_PATH, table);
153         flowTx.putOperationalData(NODE_1_FLOW_PATH, FLOW);
154         flowTx.commit().get();
155
156         validateEvent(eventFuture.get(1000, TimeUnit.MILLISECONDS));
157     }
158
159     @Test
160     public void testNoChangeOnReplaceWithSameValue() throws InterruptedException, TimeoutException, ExecutionException {
161
162         DataProviderService dataBroker = testContext.getBindingDataBroker();
163
164         // We wrote initial state NODE_0_FLOW
165         DataModificationTransaction transaction = dataBroker.beginTransaction();
166         transaction.putOperationalData(NODE_0_FLOW_PATH, FLOW);
167         transaction.commit().get();
168
169         // We registered DataChangeListener
170         final SettableFuture<DataChangeEvent<InstanceIdentifier<?>, DataObject>> eventFuture = SettableFuture.create();
171         dataBroker.registerDataChangeListener(DEEP_WILDCARDED_PATH, new DataChangeListener() {
172
173             @Override
174             public void onDataChanged(final DataChangeEvent<InstanceIdentifier<?>, DataObject> dataChangeEvent) {
175                 eventFuture.set(dataChangeEvent);
176             }
177         });
178         assertFalse(eventFuture.isDone());
179
180         DataModificationTransaction secondTx = dataBroker.beginTransaction();
181         secondTx.putOperationalData(NODE_0_FLOW_PATH, FLOW);
182         secondTx.putOperationalData(NODE_1_FLOW_PATH, FLOW);
183         secondTx.commit().get();
184
185         DataChangeEvent<InstanceIdentifier<?>, DataObject> event = (eventFuture.get(1000, TimeUnit.MILLISECONDS));
186         assertNotNull(event);
187         // Data change should contains NODE_1 Flow - which was added
188         assertTrue(event.getCreatedOperationalData().containsKey(NODE_1_FLOW_PATH));
189         // Data change must not containe NODE_0 Flow which was replaced with same value.
190         assertFalse(event.getUpdatedOperationalData().containsKey(NODE_0_FLOW_PATH));
191     }
192
193     private static void validateEvent(final DataChangeEvent<InstanceIdentifier<?>, DataObject> event) {
194         assertNotNull(event);
195         assertTrue(event.getCreatedOperationalData().containsKey(NODE_1_FLOW_PATH));
196         assertTrue(event.getCreatedOperationalData().containsKey(NODE_0_FLOW_PATH));
197         assertFalse(event.getCreatedOperationalData().containsKey(NODE_0_TABLE_FEATURES_PATH));
198     }
199
200 }

©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.