2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.sal.binding.test.bugfix;
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;
15 import java.util.Collections;
17 import java.util.concurrent.TimeUnit;
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;
44 import com.google.common.util.concurrent.SettableFuture;
46 @SuppressWarnings("deprecation")
47 public class PutAugmentationTest extends AbstractDataServiceTest implements DataChangeListener {
49 private static final QName TLL_NAME_QNAME = QName.create(TopLevelList.QNAME, "name");
50 private static final String TLL_NAME = "foo";
52 private static final TopLevelListKey TLL_KEY = new TopLevelListKey(TLL_NAME);
54 private static final Map<QName, Object> TLL_KEY_BI = Collections.<QName, Object> singletonMap(TLL_NAME_QNAME,
57 private static final InstanceIdentifier<Top> TOP_INSTANCE_ID_BA = InstanceIdentifier.builder(Top.class) //
60 private static final InstanceIdentifier<TopLevelList> TLL_INSTANCE_ID_BA = //
61 TOP_INSTANCE_ID_BA.builder() //
62 .child(TopLevelList.class, TLL_KEY).toInstance();
64 private static final InstanceIdentifier<TllComplexAugment> ALL_TCA = //
65 TOP_INSTANCE_ID_BA.builder() //
66 .child(TopLevelList.class) //
67 .augmentation(TllComplexAugment.class) //
70 private static final org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier TLL_INSTANCE_ID_BI = //
71 org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.builder() //
73 .nodeWithKey(TopLevelList.QNAME, TLL_KEY_BI) //
75 private static final InstanceIdentifier<TllComplexAugment> TCA_AUGMENTATION_PATH =
76 TLL_INSTANCE_ID_BA.builder() //
77 .augmentation(TllComplexAugment.class) //
80 private SettableFuture<DataChangeEvent<InstanceIdentifier<?>, DataObject>> lastReceivedChangeEvent;
89 public void putNodeAndAugmentation() throws Exception {
90 lastReceivedChangeEvent = SettableFuture.create();
91 baDataService.registerDataChangeListener(ALL_TCA, this);
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());
101 TopLevelList tll = (TopLevelList) baDataService.readOperationalData(TLL_INSTANCE_ID_BA);
103 assertEquals(TLL_KEY, tll.getKey());
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);
115 lastReceivedChangeEvent = SettableFuture.create();
116 result = augmentedTransaction.commit().get();
117 assertEquals(TransactionStatus.COMMITED, result.getResult());
119 DataChangeEvent<InstanceIdentifier<?>, DataObject> potential = lastReceivedChangeEvent.get(1000,TimeUnit.MILLISECONDS);
120 assertNotNull(potential);
121 assertTrue(potential.getCreatedOperationalData().containsKey(TCA_AUGMENTATION_PATH));
123 lastReceivedChangeEvent = SettableFuture.create();
125 TopLevelList augmentedTll = (TopLevelList) baDataService.readOperationalData(TLL_INSTANCE_ID_BA);
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);
134 assertTrue(lastReceivedChangeEvent.get(1000,TimeUnit.MILLISECONDS).getRemovedOperationalData().contains(TCA_AUGMENTATION_PATH));
139 public void putNodeWithAugmentation() throws Exception {
140 lastReceivedChangeEvent = SettableFuture.create();
141 baDataService.registerDataChangeListener(ALL_TCA, this);
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();
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();
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());
162 TllComplexAugment readedAugmentation = (TllComplexAugment) baDataService.readOperationalData(
163 TLL_INSTANCE_ID_BA.augmentation(TllComplexAugment.class));
164 assertNotNull(readedAugmentation);
166 assertEquals(tca.getAttrStr1(), readedAugmentation.getAttrStr1());
168 testPutNodeConnectorWithAugmentation();
169 lastReceivedChangeEvent = SettableFuture.create();
172 assertTrue(lastReceivedChangeEvent.get(1000,TimeUnit.MILLISECONDS).getRemovedOperationalData().contains(TCA_AUGMENTATION_PATH));
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);
182 NestedListBuilder nc = new NestedListBuilder();
185 NestedListSimpleAugmentBuilder fncb = new NestedListSimpleAugmentBuilder();
187 nc.addAugmentation(NestedListSimpleAugment.class, fncb.build());
189 DataModificationTransaction baseTransaction = baDataService.beginTransaction();
190 baseTransaction.putOperationalData(ncPath, nc.build());
191 RpcResult<TransactionStatus> result = baseTransaction.commit().get();
192 assertEquals(TransactionStatus.COMMITED, result.getResult());
194 NestedListSimpleAugment readedAugmentation = (NestedListSimpleAugment) baDataService
195 .readOperationalData(ncAugmentPath);
196 assertNotNull(readedAugmentation);
197 assertEquals(fncb.getType(), readedAugmentation.getType());
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());
206 TopLevelList tll = (TopLevelList) baDataService.readOperationalData(TLL_INSTANCE_ID_BA);
210 private void assertBindingIndependentVersion(final org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier tllId) {
211 CompositeNode tll = biDataService.readOperationalData(tllId);
216 public void onDataChanged(final DataChangeEvent<InstanceIdentifier<?>, DataObject> change) {
217 lastReceivedChangeEvent.set(change);