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

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