2b5171369bb31a7195b029c1377201c76ba24849
[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  * FIXME: THis test should be moved to compat test-suite and rewriten
47  * to use sal-test-model
48  */
49 @SuppressWarnings("deprecation")
50 public class WildcardedDataChangeListenerTest extends AbstractDataServiceTest {
51
52     private static final NodeKey NODE_0_KEY = new NodeKey(new NodeId("test:0"));
53     private static final NodeKey NODE_1_KEY = new NodeKey(new NodeId("test:1"));
54
55     public static final InstanceIdentifier<Flow> DEEP_WILDCARDED_PATH = InstanceIdentifier.builder(Nodes.class)
56             .child(Node.class) //
57             .augmentation(FlowCapableNode.class) //
58             .child(Table.class) //
59             .child(Flow.class) //
60             .build();
61
62     private static final TableKey TABLE_0_KEY = new TableKey((short) 0);
63     private static final TableFeaturesKey TABLE_FEATURES_KEY = new TableFeaturesKey((short) 0);
64
65     private static final InstanceIdentifier<Table> NODE_0_TABLE_PATH = InstanceIdentifier.builder(Nodes.class)
66             .child(Node.class, NODE_0_KEY) //
67             .augmentation(FlowCapableNode.class) //
68             .child(Table.class, TABLE_0_KEY) //
69             .build();
70
71     private static final InstanceIdentifier<Table> NODE_1_TABLE_PATH = InstanceIdentifier.builder(Nodes.class)
72             .child(Node.class, NODE_1_KEY) //
73             .augmentation(FlowCapableNode.class) //
74             .child(Table.class, TABLE_0_KEY) //
75             .build();
76
77     private static final FlowKey FLOW_KEY = new FlowKey(new FlowId("test"));
78
79     private static final InstanceIdentifier<Flow> NODE_0_FLOW_PATH = NODE_0_TABLE_PATH.child(Flow.class, FLOW_KEY);
80
81     private static final InstanceIdentifier<Flow> NODE_1_FLOW_PATH = NODE_1_TABLE_PATH.child(Flow.class, FLOW_KEY);
82
83     private static final InstanceIdentifier<TableFeatures> NODE_0_TABLE_FEATURES_PATH =
84             NODE_0_TABLE_PATH.child(TableFeatures.class, TABLE_FEATURES_KEY);
85
86     private static final TableFeatures TABLE_FEATURES = new TableFeaturesBuilder()//
87             .setKey(TABLE_FEATURES_KEY) //
88             .setName("Foo") //
89             .setMaxEntries(1000L) //
90             .build();
91
92     private static final Flow FLOW = new FlowBuilder() //
93             .setKey(FLOW_KEY) //
94             .setBarrier(true) //
95             .setStrict(true) //
96             .build();
97
98     @Test
99     public void testSepareteWrites() throws InterruptedException, TimeoutException, ExecutionException {
100
101         DataProviderService dataBroker = testContext.getBindingDataBroker();
102
103         final SettableFuture<DataChangeEvent<InstanceIdentifier<?>, DataObject>> eventFuture = SettableFuture.create();
104         dataBroker.registerDataChangeListener(DEEP_WILDCARDED_PATH, new DataChangeListener() {
105
106             @Override
107             public void onDataChanged(final DataChangeEvent<InstanceIdentifier<?>, DataObject> dataChangeEvent) {
108                 eventFuture.set(dataChangeEvent);
109             }
110         });
111
112         DataModificationTransaction transaction = dataBroker.beginTransaction();
113         transaction.putOperationalData(NODE_0_TABLE_FEATURES_PATH, TABLE_FEATURES);
114         transaction.putOperationalData(NODE_0_FLOW_PATH, FLOW);
115         transaction.putOperationalData(NODE_1_FLOW_PATH, FLOW);
116         transaction.commit().get();
117
118         DataChangeEvent<InstanceIdentifier<?>, DataObject> event = eventFuture.get(1000, TimeUnit.MILLISECONDS);
119
120         validateEvent(event);
121     }
122
123     @Test
124     public void testWriteByReplace() throws InterruptedException, TimeoutException, ExecutionException {
125
126         DataProviderService dataBroker = testContext.getBindingDataBroker();
127
128         final SettableFuture<DataChangeEvent<InstanceIdentifier<?>, DataObject>> eventFuture = SettableFuture.create();
129         dataBroker.registerDataChangeListener(DEEP_WILDCARDED_PATH, new DataChangeListener() {
130
131             @Override
132             public void onDataChanged(final DataChangeEvent<InstanceIdentifier<?>, DataObject> dataChangeEvent) {
133                 eventFuture.set(dataChangeEvent);
134             }
135         });
136
137         DataModificationTransaction tableTx = dataBroker.beginTransaction();
138         tableTx.putOperationalData(NODE_0_TABLE_FEATURES_PATH, TABLE_FEATURES);
139         tableTx.commit().get();
140
141         assertFalse(eventFuture.isDone());
142
143         DataModificationTransaction flowTx = dataBroker.beginTransaction();
144
145         Table table = new TableBuilder() //
146                 .setKey(TABLE_0_KEY) //
147                 .setFlow(Collections.singletonList(FLOW)) //
148                 .build();
149
150         flowTx.putOperationalData(NODE_0_TABLE_PATH, table);
151         flowTx.putOperationalData(NODE_1_FLOW_PATH, FLOW);
152         flowTx.commit().get();
153
154         validateEvent(eventFuture.get(1000, TimeUnit.MILLISECONDS));
155     }
156
157     @Test
158     public void testNoChangeOnReplaceWithSameValue() throws InterruptedException, TimeoutException, ExecutionException {
159
160         DataProviderService dataBroker = testContext.getBindingDataBroker();
161
162         // We wrote initial state NODE_0_FLOW
163         DataModificationTransaction transaction = dataBroker.beginTransaction();
164         transaction.putOperationalData(NODE_0_FLOW_PATH, FLOW);
165         transaction.commit().get();
166
167         // We registered DataChangeListener
168         final SettableFuture<DataChangeEvent<InstanceIdentifier<?>, DataObject>> eventFuture = SettableFuture.create();
169         dataBroker.registerDataChangeListener(DEEP_WILDCARDED_PATH, new DataChangeListener() {
170
171             @Override
172             public void onDataChanged(final DataChangeEvent<InstanceIdentifier<?>, DataObject> dataChangeEvent) {
173                 eventFuture.set(dataChangeEvent);
174             }
175         });
176         assertFalse(eventFuture.isDone());
177
178         DataModificationTransaction secondTx = dataBroker.beginTransaction();
179         secondTx.putOperationalData(NODE_0_FLOW_PATH, FLOW);
180         secondTx.putOperationalData(NODE_1_FLOW_PATH, FLOW);
181         secondTx.commit().get();
182
183         DataChangeEvent<InstanceIdentifier<?>, DataObject> event = (eventFuture.get(1000, TimeUnit.MILLISECONDS));
184         assertNotNull(event);
185         // Data change should contains NODE_1 Flow - which was added
186         assertTrue(event.getCreatedOperationalData().containsKey(NODE_1_FLOW_PATH));
187         // Data change must not containe NODE_0 Flow which was replaced with same value.
188         assertFalse(event.getUpdatedOperationalData().containsKey(NODE_0_FLOW_PATH));
189     }
190
191     private static void validateEvent(final DataChangeEvent<InstanceIdentifier<?>, DataObject> event) {
192         assertNotNull(event);
193         assertTrue(event.getCreatedOperationalData().containsKey(NODE_1_FLOW_PATH));
194         assertTrue(event.getCreatedOperationalData().containsKey(NODE_0_FLOW_PATH));
195         assertFalse(event.getCreatedOperationalData().containsKey(NODE_0_TABLE_FEATURES_PATH));
196     }
197
198 }

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