Merge "Do not override jsr305 version"
[controller.git] / opendaylight / md-sal / sal-binding-dom-it / src / test / java / org / opendaylight / controller / sal / binding / test / bugfix / PutAugmentationTest.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.sal.binding.test.bugfix;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12 import static org.junit.Assert.assertNull;
13 import static org.junit.Assert.assertTrue;
14
15 import java.util.Collections;
16 import java.util.Map;
17 import java.util.concurrent.TimeUnit;
18
19 import org.junit.Ignore;
20 import org.junit.Test;
21 import org.opendaylight.controller.md.sal.common.api.TransactionStatus;
22 import org.opendaylight.controller.md.sal.common.api.data.DataChangeEvent;
23 import org.opendaylight.controller.sal.binding.api.data.DataChangeListener;
24 import org.opendaylight.controller.sal.binding.api.data.DataModificationTransaction;
25 import org.opendaylight.controller.sal.binding.test.AbstractDataServiceTest;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.of.migration.test.model.rev150210.NestedListSimpleAugment;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.of.migration.test.model.rev150210.NestedListSimpleAugmentBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.of.migration.test.model.rev150210.TllComplexAugment;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.of.migration.test.model.rev150210.TllComplexAugmentBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.augment.rev140709.TreeComplexUsesAugment;
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.TopLevelListBuilder;
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.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.list.rev140701.two.level.list.top.level.list.NestedList;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.list.rev140701.two.level.list.top.level.list.NestedListBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.list.rev140701.two.level.list.top.level.list.NestedListKey;
38 import org.opendaylight.yangtools.yang.binding.DataObject;
39 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
40 import org.opendaylight.yangtools.yang.common.QName;
41 import org.opendaylight.yangtools.yang.common.RpcResult;
42 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
43
44 import com.google.common.util.concurrent.SettableFuture;
45
46 @SuppressWarnings("deprecation")
47 public class PutAugmentationTest extends AbstractDataServiceTest implements DataChangeListener {
48
49     private static final QName TLL_NAME_QNAME = QName.create(TopLevelList.QNAME, "name");
50     private static final String TLL_NAME = "foo";
51
52     private static final TopLevelListKey TLL_KEY = new TopLevelListKey(TLL_NAME);
53
54     private static final Map<QName, Object> TLL_KEY_BI = Collections.<QName, Object> singletonMap(TLL_NAME_QNAME,
55             TLL_NAME);
56
57     private static final InstanceIdentifier<Top> TOP_INSTANCE_ID_BA = InstanceIdentifier.builder(Top.class) //
58             .toInstance();
59
60     private static final InstanceIdentifier<TopLevelList> TLL_INSTANCE_ID_BA = //
61             TOP_INSTANCE_ID_BA.builder() //
62             .child(TopLevelList.class, TLL_KEY).toInstance();
63
64     private static final InstanceIdentifier<TllComplexAugment> ALL_TCA = //
65             TOP_INSTANCE_ID_BA.builder() //
66             .child(TopLevelList.class) //
67             .augmentation(TllComplexAugment.class) //
68             .build();
69
70     private static final org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier TLL_INSTANCE_ID_BI = //
71     org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.builder() //
72             .node(Top.QNAME) //
73             .nodeWithKey(TopLevelList.QNAME, TLL_KEY_BI) //
74             .toInstance();
75     private static final InstanceIdentifier<TllComplexAugment> TCA_AUGMENTATION_PATH =
76             TLL_INSTANCE_ID_BA.builder() //
77             .augmentation(TllComplexAugment.class) //
78             .build();
79
80     private SettableFuture<DataChangeEvent<InstanceIdentifier<?>, DataObject>> lastReceivedChangeEvent;
81
82     /**
83      * Test for Bug 148
84      *
85      * @throws Exception
86      */
87     @Test
88     @Ignore
89     public void putNodeAndAugmentation() throws Exception {
90         lastReceivedChangeEvent = SettableFuture.create();
91         baDataService.registerDataChangeListener(ALL_TCA, this);
92
93
94         TopLevelListBuilder nodeBuilder = new TopLevelListBuilder();
95         nodeBuilder.setKey(TLL_KEY);
96         DataModificationTransaction baseTransaction = baDataService.beginTransaction();
97         baseTransaction.putOperationalData(TLL_INSTANCE_ID_BA, nodeBuilder.build());
98         RpcResult<TransactionStatus> result = baseTransaction.commit().get();
99         assertEquals(TransactionStatus.COMMITED, result.getResult());
100
101         TopLevelList tll = (TopLevelList) baDataService.readOperationalData(TLL_INSTANCE_ID_BA);
102         assertNotNull(tll);
103         assertEquals(TLL_KEY, tll.getKey());
104
105         TllComplexAugmentBuilder tcab = new TllComplexAugmentBuilder();
106         tcab.setAttrStr1("FooFoo");
107         tcab.setAttrStr2("BarBar");
108         TllComplexAugment tca = tcab.build();
109         InstanceIdentifier<TreeComplexUsesAugment> augmentIdentifier = TLL_INSTANCE_ID_BA
110                 .augmentation(TreeComplexUsesAugment.class);
111         DataModificationTransaction augmentedTransaction = baDataService.beginTransaction();
112         augmentedTransaction.putOperationalData(augmentIdentifier, tca);
113
114
115         lastReceivedChangeEvent = SettableFuture.create();
116         result = augmentedTransaction.commit().get();
117         assertEquals(TransactionStatus.COMMITED, result.getResult());
118
119         DataChangeEvent<InstanceIdentifier<?>, DataObject> potential = lastReceivedChangeEvent.get(1000,TimeUnit.MILLISECONDS);
120         assertNotNull(potential);
121         assertTrue(potential.getCreatedOperationalData().containsKey(TCA_AUGMENTATION_PATH));
122
123         lastReceivedChangeEvent = SettableFuture.create();
124
125         TopLevelList augmentedTll = (TopLevelList) baDataService.readOperationalData(TLL_INSTANCE_ID_BA);
126         assertNotNull(tll);
127         assertEquals(TLL_KEY, augmentedTll.getKey());
128         System.out.println("Before assertion");
129         assertNotNull(augmentedTll.getAugmentation(TllComplexAugment.class));
130         TllComplexAugment readedAugmentation = augmentedTll.getAugmentation(TllComplexAugment.class);
131         assertEquals(tca.getAttrStr2(), readedAugmentation.getAttrStr2());
132         assertBindingIndependentVersion(TLL_INSTANCE_ID_BI);
133         testTllRemove();
134         assertTrue(lastReceivedChangeEvent.get(1000,TimeUnit.MILLISECONDS).getRemovedOperationalData().contains(TCA_AUGMENTATION_PATH));
135     }
136
137     @Test
138     @Ignore
139     public void putNodeWithAugmentation() throws Exception {
140         lastReceivedChangeEvent = SettableFuture.create();
141         baDataService.registerDataChangeListener(ALL_TCA, this);
142
143         TopLevelListBuilder nodeBuilder = new TopLevelListBuilder();
144         nodeBuilder.setKey(TLL_KEY);
145         TllComplexAugmentBuilder tcab = new TllComplexAugmentBuilder();
146         tcab.setAttrStr1("FooFoo");
147         tcab.setAttrStr2("BarBar");
148         TllComplexAugment tca = tcab.build();
149
150         nodeBuilder.addAugmentation(TreeComplexUsesAugment.class, tca);
151         DataModificationTransaction baseTransaction = baDataService.beginTransaction();
152         baseTransaction.putOperationalData(TLL_INSTANCE_ID_BA, nodeBuilder.build());
153         RpcResult<TransactionStatus> result = baseTransaction.commit().get();
154
155
156         DataChangeEvent<InstanceIdentifier<?>, DataObject> potential = lastReceivedChangeEvent.get(1000,TimeUnit.MILLISECONDS);
157         assertNotNull(potential);
158         assertTrue(potential.getCreatedOperationalData().containsKey(TCA_AUGMENTATION_PATH));
159         lastReceivedChangeEvent = SettableFuture.create();
160         assertEquals(TransactionStatus.COMMITED, result.getResult());
161
162         TllComplexAugment readedAugmentation = (TllComplexAugment) baDataService.readOperationalData(
163                 TLL_INSTANCE_ID_BA.augmentation(TllComplexAugment.class));
164         assertNotNull(readedAugmentation);
165
166         assertEquals(tca.getAttrStr1(), readedAugmentation.getAttrStr1());
167
168         testPutNodeConnectorWithAugmentation();
169         lastReceivedChangeEvent = SettableFuture.create();
170         testTllRemove();
171
172         assertTrue(lastReceivedChangeEvent.get(1000,TimeUnit.MILLISECONDS).getRemovedOperationalData().contains(TCA_AUGMENTATION_PATH));
173     }
174
175     private void testPutNodeConnectorWithAugmentation() throws Exception {
176         NestedListKey ncKey = new NestedListKey("test:0:0");
177         InstanceIdentifier<NestedList> ncPath = TLL_INSTANCE_ID_BA
178                 .child(NestedList.class, ncKey);
179         InstanceIdentifier<NestedListSimpleAugment> ncAugmentPath = ncPath
180                 .augmentation(NestedListSimpleAugment.class);
181
182         NestedListBuilder nc = new NestedListBuilder();
183         nc.setKey(ncKey);
184
185         NestedListSimpleAugmentBuilder fncb = new NestedListSimpleAugmentBuilder();
186         fncb.setType("Baz");
187         nc.addAugmentation(NestedListSimpleAugment.class, fncb.build());
188
189         DataModificationTransaction baseTransaction = baDataService.beginTransaction();
190         baseTransaction.putOperationalData(ncPath, nc.build());
191         RpcResult<TransactionStatus> result = baseTransaction.commit().get();
192         assertEquals(TransactionStatus.COMMITED, result.getResult());
193
194         NestedListSimpleAugment readedAugmentation = (NestedListSimpleAugment) baDataService
195                 .readOperationalData(ncAugmentPath);
196         assertNotNull(readedAugmentation);
197         assertEquals(fncb.getType(), readedAugmentation.getType());
198     }
199
200     private void testTllRemove() throws Exception {
201         DataModificationTransaction transaction = baDataService.beginTransaction();
202         transaction.removeOperationalData(TLL_INSTANCE_ID_BA);
203         RpcResult<TransactionStatus> result = transaction.commit().get();
204         assertEquals(TransactionStatus.COMMITED, result.getResult());
205
206         TopLevelList tll = (TopLevelList) baDataService.readOperationalData(TLL_INSTANCE_ID_BA);
207         assertNull(tll);
208     }
209
210     private void assertBindingIndependentVersion(final org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier tllId) {
211         CompositeNode tll = biDataService.readOperationalData(tllId);
212         assertNotNull(tll);
213     }
214
215     @Override
216     public void onDataChanged(final DataChangeEvent<InstanceIdentifier<?>, DataObject> change) {
217         lastReceivedChangeEvent.set(change);
218     }
219
220 }