Cache reflection operations in AbstractSchemaAwareTest
[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.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType.OPERATIONAL;
12
13 import com.google.common.collect.ImmutableSet;
14 import com.google.common.util.concurrent.Uninterruptibles;
15 import java.util.Collections;
16 import java.util.Set;
17 import java.util.concurrent.ExecutionException;
18 import java.util.concurrent.TimeUnit;
19 import java.util.concurrent.TimeoutException;
20 import org.junit.Test;
21 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
22 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
23 import org.opendaylight.controller.md.sal.binding.test.AbstractDataTreeChangeListenerTest;
24 import org.opendaylight.mdsal.binding.spec.reflect.BindingReflections;
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.InstanceIdentifier;
36 import org.opendaylight.yangtools.yang.binding.YangModuleInfo;
37
38 /**
39  * FIXME: THis test should be moved to compat test-suite
40  */
41 public class WildcardedDataChangeListenerTest extends AbstractDataTreeChangeListenerTest {
42
43     private static final TopLevelListKey TOP_LEVEL_LIST_0_KEY = new TopLevelListKey("test:0");
44     private static final TopLevelListKey TOP_LEVEL_LIST_1_KEY = new TopLevelListKey("test:1");
45
46     protected static final InstanceIdentifier<ListViaUses> DEEP_WILDCARDED_PATH = InstanceIdentifier
47             .builder(Top.class)
48             .child(TopLevelList.class)
49             .augmentation(TreeComplexUsesAugment.class)
50             .child(ListViaUses.class)
51             .build();
52
53     private static final InstanceIdentifier<TreeComplexUsesAugment> NODE_0_TCU_PATH = InstanceIdentifier
54             .builder(Top.class)
55             .child(TopLevelList.class, TOP_LEVEL_LIST_0_KEY)
56             .augmentation(TreeComplexUsesAugment.class)
57             .build();
58
59     private static final InstanceIdentifier<TreeComplexUsesAugment> NODE_1_TCU_PATH = InstanceIdentifier
60             .builder(Top.class)
61             .child(TopLevelList.class, TOP_LEVEL_LIST_1_KEY)
62             .augmentation(TreeComplexUsesAugment.class)
63             .build();
64
65
66     private static final ListViaUsesKey LIST_VIA_USES_KEY = new ListViaUsesKey("test");
67
68     private static final InstanceIdentifier<ListViaUses> NODE_0_LVU_PATH = NODE_0_TCU_PATH.child(ListViaUses.class,
69         LIST_VIA_USES_KEY);
70
71     private static final InstanceIdentifier<ListViaUses> NODE_1_LVU_PATH = NODE_1_TCU_PATH.child(ListViaUses.class,
72         LIST_VIA_USES_KEY);
73
74     private static final InstanceIdentifier<ContainerWithUses> NODE_0_CWU_PATH =
75             NODE_0_TCU_PATH.child(ContainerWithUses.class);
76
77     private static final ContainerWithUses CWU = new ContainerWithUsesBuilder()
78             .setLeafFromGrouping("some container value").build();
79
80     private static final ListViaUses LVU = new ListViaUsesBuilder()
81             .withKey(LIST_VIA_USES_KEY).setName("john").build();
82
83     @Override
84     protected Set<YangModuleInfo> getModuleInfos() throws Exception {
85         return ImmutableSet.of(BindingReflections.getModuleInfo(Top.class),
86                 BindingReflections.getModuleInfo(TreeComplexUsesAugment.class));
87     }
88
89     @Test
90     public void testSeparateWrites() throws InterruptedException, TimeoutException, ExecutionException {
91
92         DataBroker dataBroker = getDataBroker();
93
94         final TestListener<ListViaUses> listener = createListener(OPERATIONAL, DEEP_WILDCARDED_PATH,
95             dataTreeModification -> NODE_0_LVU_PATH.equals(dataTreeModification.getRootPath().getRootIdentifier()),
96             dataTreeModification -> NODE_1_LVU_PATH.equals(dataTreeModification.getRootPath().getRootIdentifier()));
97
98         final WriteTransaction transaction = dataBroker.newWriteOnlyTransaction();
99         transaction.put(OPERATIONAL, NODE_0_CWU_PATH, CWU, true);
100         transaction.put(OPERATIONAL, NODE_0_LVU_PATH, LVU, true);
101         transaction.put(OPERATIONAL, NODE_1_LVU_PATH, LVU, true);
102         transaction.submit().get(5, TimeUnit.SECONDS);
103
104         listener.verify();
105     }
106
107     @Test
108     public void testWriteByReplace() throws InterruptedException, TimeoutException, ExecutionException {
109
110         DataBroker dataBroker = getDataBroker();
111
112         final TestListener<ListViaUses> listener = createListener(OPERATIONAL, DEEP_WILDCARDED_PATH,
113             dataTreeModification -> NODE_0_LVU_PATH.equals(dataTreeModification.getRootPath().getRootIdentifier()),
114             dataTreeModification -> NODE_1_LVU_PATH.equals(dataTreeModification.getRootPath().getRootIdentifier()));
115
116         final WriteTransaction cwuTx = dataBroker.newWriteOnlyTransaction();
117         cwuTx.put(OPERATIONAL, NODE_0_CWU_PATH, CWU, true);
118         cwuTx.submit().get(5, TimeUnit.SECONDS);
119
120         Uninterruptibles.sleepUninterruptibly(500, TimeUnit.MILLISECONDS);
121         assertFalse(listener.hasChanges());
122
123         final WriteTransaction lvuTx = dataBroker.newWriteOnlyTransaction();
124
125         TreeComplexUsesAugment tcua = new TreeComplexUsesAugmentBuilder()
126                 .setListViaUses(Collections.singletonList(LVU)).build();
127
128         lvuTx.put(OPERATIONAL, NODE_0_TCU_PATH, tcua, true);
129         lvuTx.put(OPERATIONAL, NODE_1_LVU_PATH, LVU, true);
130         lvuTx.submit().get(5, TimeUnit.SECONDS);
131
132         listener.verify();
133     }
134
135     @Test
136     public void testChangeOnReplaceWithSameValue() throws InterruptedException, TimeoutException, ExecutionException {
137
138         DataBroker dataBroker = getDataBroker();
139
140         // Write initial state NODE_0_FLOW
141         final WriteTransaction transaction = dataBroker.newWriteOnlyTransaction();
142         transaction.put(OPERATIONAL, NODE_0_LVU_PATH, LVU, true);
143         transaction.submit().get(5, TimeUnit.SECONDS);
144
145         final TestListener<ListViaUses> listener = createListener(OPERATIONAL, DEEP_WILDCARDED_PATH,
146             dataTreeModification -> NODE_1_LVU_PATH.equals(dataTreeModification.getRootPath().getRootIdentifier()),
147             dataTreeModification -> NODE_0_LVU_PATH.equals(dataTreeModification.getRootPath().getRootIdentifier()),
148             dataTreeModification -> NODE_1_LVU_PATH.equals(dataTreeModification.getRootPath().getRootIdentifier()));
149
150         final WriteTransaction secondTx = dataBroker.newWriteOnlyTransaction();
151         secondTx.put(OPERATIONAL, NODE_0_LVU_PATH, LVU, true);
152         secondTx.put(OPERATIONAL, NODE_1_LVU_PATH, LVU, true);
153         secondTx.submit().get(5, TimeUnit.SECONDS);
154
155         listener.verify();
156     }
157 }

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