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