Bug 2682 - Switch sal-binding-dom-it to sal-test-model
[controller.git] / opendaylight / md-sal / sal-binding-dom-it / src / test / java / org / opendaylight / controller / sal / binding / test / bugfix / DOMCodecBug03Test.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 import static org.junit.Assert.assertEquals;
10 import static org.junit.Assert.assertFalse;
11 import static org.junit.Assert.assertNotNull;
12 import static org.junit.Assert.assertNull;
13
14 import java.util.Collections;
15 import java.util.List;
16 import java.util.Map;
17 import java.util.concurrent.TimeUnit;
18
19 import org.junit.Test;
20 import org.opendaylight.controller.md.sal.common.api.TransactionStatus;
21 import org.opendaylight.controller.md.sal.common.api.data.DataChangeEvent;
22 import org.opendaylight.controller.sal.binding.api.data.DataChangeListener;
23 import org.opendaylight.controller.sal.binding.api.data.DataModificationTransaction;
24 import org.opendaylight.controller.sal.binding.test.AbstractDataServiceTest;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.of.migration.test.model.rev150210.CustomEnum;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.of.migration.test.model.rev150210.TllComplexAugment;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.of.migration.test.model.rev150210.TllComplexAugmentBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.of.migration.test.model.rev150210.aug.grouping.Cont2;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.of.migration.test.model.rev150210.aug.grouping.Cont2Builder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.of.migration.test.model.rev150210.aug.grouping.cont2.Contlist1;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.of.migration.test.model.rev150210.aug.grouping.cont2.Contlist1Builder;
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.TopLevelListBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.list.rev140701.two.level.list.TopLevelListKey;
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.NestedList;
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.NestedListBuilder;
38 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;
39 import org.opendaylight.yangtools.yang.binding.DataObject;
40 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
41 import org.opendaylight.yangtools.yang.common.QName;
42 import org.opendaylight.yangtools.yang.common.RpcResult;
43 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
44
45 import com.google.common.util.concurrent.SettableFuture;
46
47 @SuppressWarnings("deprecation")
48 public class DOMCodecBug03Test extends AbstractDataServiceTest implements DataChangeListener {
49
50     private static final QName TOP_LEVEL_LIST_NAME_QNAME = QName.create(TopLevelList.QNAME, "name");
51     private static final String TOP_LEVEL_LIST_NAME = "tll:foo";
52
53     private static final TopLevelListKey TLL_KEY = new TopLevelListKey(TOP_LEVEL_LIST_NAME);
54
55     private static final Map<QName, Object> TLL_KEY_BI = Collections.<QName, Object> singletonMap(TOP_LEVEL_LIST_NAME_QNAME,
56             TOP_LEVEL_LIST_NAME);
57
58     private static final InstanceIdentifier<Top> TOP_INSTANCE_ID_BA = InstanceIdentifier.builder(Top.class) //
59             .toInstance();
60
61
62     private static final InstanceIdentifier<TopLevelList> TLL_INSTANCE_ID_BA = TOP_INSTANCE_ID_BA.child(TopLevelList.class, TLL_KEY);
63
64
65     private static final InstanceIdentifier<Cont2> CONT2_INSTANCE_ID_BA = //
66             TOP_INSTANCE_ID_BA.builder() //
67             .child(TopLevelList.class, TLL_KEY) //
68             .augmentation(TllComplexAugment.class) //
69             .child(Cont2.class)
70             .toInstance();
71
72
73     private static final org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier TLL_INSTANCE_ID_BI = //
74     org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.builder() //
75             .node(Top.QNAME) //
76             .nodeWithKey(TopLevelList.QNAME, TLL_KEY_BI) //
77             .toInstance();
78     private static final QName CONT2_QNAME = QName.create(TllComplexAugment.QNAME, Cont2.QNAME.getLocalName());
79
80
81     private static final org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier CONT2_INSTANCE_ID_BI = //
82             org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.builder() //
83                     .node(Top.QNAME) //
84                     .nodeWithKey(TopLevelList.QNAME, TLL_KEY_BI) //
85                     .node(CONT2_QNAME) //
86                     .toInstance();
87
88     private final SettableFuture<DataChangeEvent<InstanceIdentifier<?>, DataObject>> receivedChangeEvent = SettableFuture.create();
89
90
91
92     /**
93      * Test for Bug 148
94      *
95      * @throws Exception
96      */
97     @Test
98     public void testAugmentSerialization() throws Exception {
99
100
101         baDataService.registerDataChangeListener(TOP_INSTANCE_ID_BA, this);
102
103         TopLevelListBuilder tllBuilder = new TopLevelListBuilder();
104         tllBuilder.setKey(TLL_KEY);
105         DataModificationTransaction transaction = baDataService.beginTransaction();
106
107
108         TllComplexAugmentBuilder tllcab = new TllComplexAugmentBuilder();
109         tllcab.setAttrStr1("Hardware Foo");
110         tllcab.setAttrStr2("Manufacturer Foo");
111         tllcab.setAttrStr3("Serial Foo");
112         tllcab.setAttrStr4("Description Foo");
113         TllComplexAugment tlca = tllcab.build();
114         tllBuilder.addAugmentation(TllComplexAugment.class, tlca);
115         TopLevelList original = tllBuilder.build();
116         transaction.putOperationalData(TLL_INSTANCE_ID_BA, original);
117
118         RpcResult<TransactionStatus> result = transaction.commit().get();
119         assertEquals(TransactionStatus.COMMITED, result.getResult());
120
121         DataChangeEvent<InstanceIdentifier<?>, DataObject> potential = receivedChangeEvent.get(1000,TimeUnit.MILLISECONDS);
122         assertNotNull(potential);
123
124         verifyTll((Top) potential.getUpdatedOperationalSubtree(),original);
125         assertBindingIndependentVersion(TLL_INSTANCE_ID_BI);
126         Top top = checkForTop();
127         verifyTll(top,original);
128
129         testAddingNodeConnector();
130         testTllRemove();
131
132     }
133
134     @Test
135     public void testAugmentNestedSerialization() throws Exception {
136         DataModificationTransaction transaction = baDataService.beginTransaction();
137
138         Cont2Builder cont2b = new Cont2Builder();
139         Contlist1Builder cl1b = new Contlist1Builder();
140         cl1b.setAttrStr("foo-action");
141         cl1b.setAttrEnum(CustomEnum.Type1);
142         List<Contlist1> contlists = Collections.singletonList(cl1b.build());
143         cont2b.setContlist1(contlists);
144
145         transaction.putOperationalData(CONT2_INSTANCE_ID_BA, cont2b.build());
146         RpcResult<TransactionStatus> putResult = transaction.commit().get();
147         assertNotNull(putResult);
148         assertEquals(TransactionStatus.COMMITED, putResult.getResult());
149         Cont2 readedTable = (Cont2) baDataService.readOperationalData(CONT2_INSTANCE_ID_BA);
150         assertNotNull(readedTable);
151
152         CompositeNode biSupportedActions = biDataService.readOperationalData(CONT2_INSTANCE_ID_BI);
153         assertNotNull(biSupportedActions);
154
155     }
156
157     private void testAddingNodeConnector() throws Exception {
158         NestedListKey nlKey = new NestedListKey("test:0:0");
159         InstanceIdentifier<NestedList> ncInstanceId = TLL_INSTANCE_ID_BA.child(NestedList.class, nlKey);
160         NestedListBuilder nlBuilder = new NestedListBuilder();
161         nlBuilder.setKey(nlKey);
162         NestedList nestedList = nlBuilder.build();
163         DataModificationTransaction transaction = baDataService.beginTransaction();
164         transaction.putOperationalData(ncInstanceId, nestedList);
165         RpcResult<TransactionStatus> result = transaction.commit().get();
166         assertEquals(TransactionStatus.COMMITED, result.getResult());
167         TopLevelList tll = (TopLevelList) baDataService.readOperationalData(TLL_INSTANCE_ID_BA);
168         assertNotNull(tll);
169         assertNotNull(tll.getNestedList());
170         assertFalse(tll.getNestedList().isEmpty());
171         NestedList readedNl = tll.getNestedList().get(0);
172         assertNotNull(readedNl);
173     }
174
175     private void testTllRemove() throws Exception {
176         DataModificationTransaction transaction = baDataService.beginTransaction();
177         transaction.removeOperationalData(TLL_INSTANCE_ID_BA);
178         RpcResult<TransactionStatus> result = transaction.commit().get();
179         assertEquals(TransactionStatus.COMMITED, result.getResult());
180
181         TopLevelList tll = (TopLevelList) baDataService.readOperationalData(TLL_INSTANCE_ID_BA);
182         assertNull(tll);
183     }
184
185     private void verifyTll(final Top top,final TopLevelList original) {
186         assertNotNull(top);
187         assertNotNull(top.getTopLevelList());
188         assertEquals(1, top.getTopLevelList().size());
189         TopLevelList readedNode = top.getTopLevelList().get(0);
190         assertEquals(original.getName(), readedNode.getName());
191         assertEquals(original.getKey(), readedNode.getKey());
192
193         TllComplexAugment fnu = original.getAugmentation(TllComplexAugment.class);
194         TllComplexAugment readedAugment = readedNode.getAugmentation(TllComplexAugment.class);
195         assertNotNull(fnu);
196         assertEquals(fnu.getAttrStr2(), readedAugment.getAttrStr2());
197         assertEquals(fnu.getAttrStr3(), readedAugment.getAttrStr3());
198
199     }
200
201     private void assertBindingIndependentVersion(
202             final org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier nodeId) {
203         CompositeNode node = biDataService.readOperationalData(nodeId);
204         assertNotNull(node);
205     }
206
207     private Top checkForTop() {
208         return (Top) baDataService.readOperationalData(TOP_INSTANCE_ID_BA);
209     }
210
211     @Override
212     public void onDataChanged(final DataChangeEvent<InstanceIdentifier<?>, DataObject> change) {
213         receivedChangeEvent.set(change);
214     }
215
216 }

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