Merge "Bug 849: Fixed NPE in Translated Data Change Events."
[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.flow.inventory.rev130819.FlowCapableNode;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNodeBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNodeConnector;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNodeConnectorBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.flow.node.SupportedActions;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnector;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnectorBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnectorKey;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
40 import org.opendaylight.yangtools.yang.binding.DataObject;
41 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
42 import org.opendaylight.yangtools.yang.common.QName;
43 import org.opendaylight.yangtools.yang.common.RpcResult;
44 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
45
46 import com.google.common.util.concurrent.SettableFuture;
47
48 public class PutAugmentationTest extends AbstractDataServiceTest implements DataChangeListener {
49
50     private static final QName NODE_ID_QNAME = QName.create(Node.QNAME, "id");
51     private static final String NODE_ID = "openflow:1";
52
53     private static final NodeKey NODE_KEY = new NodeKey(new NodeId(NODE_ID));
54
55     private static final Map<QName, Object> NODE_KEY_BI = Collections.<QName, Object> singletonMap(NODE_ID_QNAME,
56             NODE_ID);
57
58     private static final InstanceIdentifier<Nodes> NODES_INSTANCE_ID_BA = InstanceIdentifier.builder(Nodes.class) //
59             .toInstance();
60
61     private static final InstanceIdentifier<Node> NODE_INSTANCE_ID_BA = //
62             NODES_INSTANCE_ID_BA.builder() //
63             .child(Node.class, NODE_KEY).toInstance();
64
65     private static final InstanceIdentifier<SupportedActions> SUPPORTED_ACTIONS_INSTANCE_ID_BA = //
66             NODES_INSTANCE_ID_BA.builder() //
67             .child(Node.class, NODE_KEY) //
68             .augmentation(FlowCapableNode.class) //
69             .child(SupportedActions.class).toInstance();
70
71     private static final InstanceIdentifier<FlowCapableNode> ALL_FLOW_CAPABLE_NODES = //
72             NODES_INSTANCE_ID_BA.builder() //
73             .child(Node.class) //
74             .augmentation(FlowCapableNode.class) //
75             .build();
76
77     private static final org.opendaylight.yangtools.yang.data.api.InstanceIdentifier NODE_INSTANCE_ID_BI = //
78     org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.builder() //
79             .node(Nodes.QNAME) //
80             .nodeWithKey(Node.QNAME, NODE_KEY_BI) //
81             .toInstance();
82     private static final QName SUPPORTED_ACTIONS_QNAME = QName.create(FlowCapableNode.QNAME,
83             SupportedActions.QNAME.getLocalName());
84
85     private static final org.opendaylight.yangtools.yang.data.api.InstanceIdentifier SUPPORTED_ACTIONS_INSTANCE_ID_BI = //
86     org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.builder() //
87             .node(Nodes.QNAME) //
88             .nodeWithKey(Node.QNAME, NODE_KEY_BI) //
89             .node(SUPPORTED_ACTIONS_QNAME) //
90             .toInstance();
91     private static final InstanceIdentifier<FlowCapableNode> FLOW_AUGMENTATION_PATH =
92             NODE_INSTANCE_ID_BA.builder() //
93             .augmentation(FlowCapableNode.class) //
94             .build();
95
96     private SettableFuture<DataChangeEvent<InstanceIdentifier<?>, DataObject>> lastReceivedChangeEvent;
97
98     /**
99      * Test for Bug 148
100      *
101      * @throws Exception
102      */
103     @Test
104     @Ignore
105     public void putNodeAndAugmentation() throws Exception {
106         lastReceivedChangeEvent = SettableFuture.create();
107         baDataService.registerDataChangeListener(ALL_FLOW_CAPABLE_NODES, this);
108
109
110         NodeBuilder nodeBuilder = new NodeBuilder();
111         nodeBuilder.setId(new NodeId(NODE_ID));
112         nodeBuilder.setKey(NODE_KEY);
113         DataModificationTransaction baseTransaction = baDataService.beginTransaction();
114         baseTransaction.putOperationalData(NODE_INSTANCE_ID_BA, nodeBuilder.build());
115         RpcResult<TransactionStatus> result = baseTransaction.commit().get();
116         assertEquals(TransactionStatus.COMMITED, result.getResult());
117
118         Node node = (Node) baDataService.readOperationalData(NODE_INSTANCE_ID_BA);
119         assertNotNull(node);
120         assertEquals(NODE_KEY, node.getKey());
121
122         FlowCapableNodeBuilder fnub = new FlowCapableNodeBuilder();
123         fnub.setHardware("Hardware Foo");
124         fnub.setManufacturer("Manufacturer Foo");
125         fnub.setSerialNumber("Serial Foo");
126         fnub.setDescription("Description Foo");
127         fnub.setSoftware("JUnit emulated");
128         FlowCapableNode fnu = fnub.build();
129         InstanceIdentifier<FlowCapableNode> augmentIdentifier = NODE_INSTANCE_ID_BA
130                 .augmentation(FlowCapableNode.class);
131         DataModificationTransaction augmentedTransaction = baDataService.beginTransaction();
132         augmentedTransaction.putOperationalData(augmentIdentifier, fnu);
133
134
135         lastReceivedChangeEvent = SettableFuture.create();
136         result = augmentedTransaction.commit().get();
137         assertEquals(TransactionStatus.COMMITED, result.getResult());
138
139         DataChangeEvent<InstanceIdentifier<?>, DataObject> potential = lastReceivedChangeEvent.get(1000,TimeUnit.MILLISECONDS);
140         assertNotNull(potential);
141         assertTrue(potential.getCreatedOperationalData().containsKey(FLOW_AUGMENTATION_PATH));
142
143         lastReceivedChangeEvent = SettableFuture.create();
144
145         Node augmentedNode = (Node) baDataService.readOperationalData(NODE_INSTANCE_ID_BA);
146         assertNotNull(node);
147         assertEquals(NODE_KEY, augmentedNode.getKey());
148         System.out.println("Before assertion");
149         assertNotNull(augmentedNode.getAugmentation(FlowCapableNode.class));
150         FlowCapableNode readedAugmentation = augmentedNode.getAugmentation(FlowCapableNode.class);
151         assertEquals(fnu.getDescription(), readedAugmentation.getDescription());
152         assertBindingIndependentVersion(NODE_INSTANCE_ID_BI);
153         testNodeRemove();
154         assertTrue(lastReceivedChangeEvent.get(1000,TimeUnit.MILLISECONDS).getRemovedOperationalData().contains(FLOW_AUGMENTATION_PATH));
155     }
156
157     @Test
158     @Ignore
159     public void putNodeWithAugmentation() throws Exception {
160         lastReceivedChangeEvent = SettableFuture.create();
161         baDataService.registerDataChangeListener(ALL_FLOW_CAPABLE_NODES, this);
162
163         NodeBuilder nodeBuilder = new NodeBuilder();
164         nodeBuilder.setId(new NodeId(NODE_ID));
165         nodeBuilder.setKey(NODE_KEY);
166         FlowCapableNodeBuilder fnub = new FlowCapableNodeBuilder();
167         fnub.setHardware("Hardware Foo");
168         fnub.setManufacturer("Manufacturer Foo");
169         fnub.setSerialNumber("Serial Foo");
170         fnub.setDescription("Description Foo");
171         fnub.setSoftware("JUnit emulated");
172         FlowCapableNode fnu = fnub.build();
173
174         nodeBuilder.addAugmentation(FlowCapableNode.class, fnu);
175         DataModificationTransaction baseTransaction = baDataService.beginTransaction();
176         baseTransaction.putOperationalData(NODE_INSTANCE_ID_BA, nodeBuilder.build());
177         RpcResult<TransactionStatus> result = baseTransaction.commit().get();
178
179
180         DataChangeEvent<InstanceIdentifier<?>, DataObject> potential = lastReceivedChangeEvent.get(1000,TimeUnit.MILLISECONDS);
181         assertNotNull(potential);
182         assertTrue(potential.getCreatedOperationalData().containsKey(FLOW_AUGMENTATION_PATH));
183         lastReceivedChangeEvent = SettableFuture.create();
184         assertEquals(TransactionStatus.COMMITED, result.getResult());
185
186         FlowCapableNode readedAugmentation = (FlowCapableNode) baDataService.readOperationalData(
187                 NODE_INSTANCE_ID_BA.augmentation(FlowCapableNode.class));
188         assertNotNull(readedAugmentation);
189
190         assertEquals(fnu.getHardware(), readedAugmentation.getHardware());
191
192         testPutNodeConnectorWithAugmentation();
193         lastReceivedChangeEvent = SettableFuture.create();
194         testNodeRemove();
195
196         assertTrue(lastReceivedChangeEvent.get(1000,TimeUnit.MILLISECONDS).getRemovedOperationalData().contains(FLOW_AUGMENTATION_PATH));
197     }
198
199     private void testPutNodeConnectorWithAugmentation() throws Exception {
200         NodeConnectorKey ncKey = new NodeConnectorKey(new NodeConnectorId("test:0:0"));
201         InstanceIdentifier<NodeConnector> ncPath = NODE_INSTANCE_ID_BA
202                 .child(NodeConnector.class, ncKey);
203         InstanceIdentifier<FlowCapableNodeConnector> ncAugmentPath = ncPath
204                 .augmentation(FlowCapableNodeConnector.class);
205
206         NodeConnectorBuilder nc = new NodeConnectorBuilder();
207         nc.setKey(ncKey);
208
209         FlowCapableNodeConnectorBuilder fncb = new FlowCapableNodeConnectorBuilder();
210         fncb.setName("Baz");
211         nc.addAugmentation(FlowCapableNodeConnector.class, fncb.build());
212
213         DataModificationTransaction baseTransaction = baDataService.beginTransaction();
214         baseTransaction.putOperationalData(ncPath, nc.build());
215         RpcResult<TransactionStatus> result = baseTransaction.commit().get();
216         assertEquals(TransactionStatus.COMMITED, result.getResult());
217
218         FlowCapableNodeConnector readedAugmentation = (FlowCapableNodeConnector) baDataService
219                 .readOperationalData(ncAugmentPath);
220         assertNotNull(readedAugmentation);
221         assertEquals(fncb.getName(), readedAugmentation.getName());
222     }
223
224     private void testNodeRemove() throws Exception {
225         DataModificationTransaction transaction = baDataService.beginTransaction();
226         transaction.removeOperationalData(NODE_INSTANCE_ID_BA);
227         RpcResult<TransactionStatus> result = transaction.commit().get();
228         assertEquals(TransactionStatus.COMMITED, result.getResult());
229
230         Node node = (Node) baDataService.readOperationalData(NODE_INSTANCE_ID_BA);
231         assertNull(node);
232     }
233
234     private void verifyNodes(final Nodes nodes, final Node original) {
235         assertNotNull(nodes);
236         assertNotNull(nodes.getNode());
237         assertEquals(1, nodes.getNode().size());
238         Node readedNode = nodes.getNode().get(0);
239         assertEquals(original.getId(), readedNode.getId());
240         assertEquals(original.getKey(), readedNode.getKey());
241
242         FlowCapableNode fnu = original.getAugmentation(FlowCapableNode.class);
243         FlowCapableNode readedAugment = readedNode.getAugmentation(FlowCapableNode.class);
244         assertNotNull(fnu);
245         assertEquals(fnu.getDescription(), readedAugment.getDescription());
246         assertEquals(fnu.getSerialNumber(), readedAugment.getSerialNumber());
247
248     }
249
250     private void assertBindingIndependentVersion(final org.opendaylight.yangtools.yang.data.api.InstanceIdentifier nodeId) {
251         CompositeNode node = biDataService.readOperationalData(nodeId);
252         assertNotNull(node);
253     }
254
255     private Nodes checkForNodes() {
256         return (Nodes) baDataService.readOperationalData(NODES_INSTANCE_ID_BA);
257     }
258
259     @Override
260     public void onDataChanged(final DataChangeEvent<InstanceIdentifier<?>, DataObject> change) {
261         lastReceivedChangeEvent.set(change);
262     }
263
264 }

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