Merge "Introducing simple merge strategy for config subsystem"
[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.inventory.rev130819.NodeConnectorId;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnector;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnectorBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnectorKey;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
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 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<FlowCapableNode> ALL_FLOW_CAPABLE_NODES = //
66             NODES_INSTANCE_ID_BA.builder() //
67             .child(Node.class) //
68             .augmentation(FlowCapableNode.class) //
69             .build();
70
71     private static final org.opendaylight.yangtools.yang.data.api.InstanceIdentifier NODE_INSTANCE_ID_BI = //
72     org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.builder() //
73             .node(Nodes.QNAME) //
74             .nodeWithKey(Node.QNAME, NODE_KEY_BI) //
75             .toInstance();
76     private static final InstanceIdentifier<FlowCapableNode> FLOW_AUGMENTATION_PATH =
77             NODE_INSTANCE_ID_BA.builder() //
78             .augmentation(FlowCapableNode.class) //
79             .build();
80
81     private SettableFuture<DataChangeEvent<InstanceIdentifier<?>, DataObject>> lastReceivedChangeEvent;
82
83     /**
84      * Test for Bug 148
85      *
86      * @throws Exception
87      */
88     @Test
89     @Ignore
90     public void putNodeAndAugmentation() throws Exception {
91         lastReceivedChangeEvent = SettableFuture.create();
92         baDataService.registerDataChangeListener(ALL_FLOW_CAPABLE_NODES, this);
93
94
95         NodeBuilder nodeBuilder = new NodeBuilder();
96         nodeBuilder.setId(new NodeId(NODE_ID));
97         nodeBuilder.setKey(NODE_KEY);
98         DataModificationTransaction baseTransaction = baDataService.beginTransaction();
99         baseTransaction.putOperationalData(NODE_INSTANCE_ID_BA, nodeBuilder.build());
100         RpcResult<TransactionStatus> result = baseTransaction.commit().get();
101         assertEquals(TransactionStatus.COMMITED, result.getResult());
102
103         Node node = (Node) baDataService.readOperationalData(NODE_INSTANCE_ID_BA);
104         assertNotNull(node);
105         assertEquals(NODE_KEY, node.getKey());
106
107         FlowCapableNodeBuilder fnub = new FlowCapableNodeBuilder();
108         fnub.setHardware("Hardware Foo");
109         fnub.setManufacturer("Manufacturer Foo");
110         fnub.setSerialNumber("Serial Foo");
111         fnub.setDescription("Description Foo");
112         fnub.setSoftware("JUnit emulated");
113         FlowCapableNode fnu = fnub.build();
114         InstanceIdentifier<FlowCapableNode> augmentIdentifier = NODE_INSTANCE_ID_BA
115                 .augmentation(FlowCapableNode.class);
116         DataModificationTransaction augmentedTransaction = baDataService.beginTransaction();
117         augmentedTransaction.putOperationalData(augmentIdentifier, fnu);
118
119
120         lastReceivedChangeEvent = SettableFuture.create();
121         result = augmentedTransaction.commit().get();
122         assertEquals(TransactionStatus.COMMITED, result.getResult());
123
124         DataChangeEvent<InstanceIdentifier<?>, DataObject> potential = lastReceivedChangeEvent.get(1000,TimeUnit.MILLISECONDS);
125         assertNotNull(potential);
126         assertTrue(potential.getCreatedOperationalData().containsKey(FLOW_AUGMENTATION_PATH));
127
128         lastReceivedChangeEvent = SettableFuture.create();
129
130         Node augmentedNode = (Node) baDataService.readOperationalData(NODE_INSTANCE_ID_BA);
131         assertNotNull(node);
132         assertEquals(NODE_KEY, augmentedNode.getKey());
133         System.out.println("Before assertion");
134         assertNotNull(augmentedNode.getAugmentation(FlowCapableNode.class));
135         FlowCapableNode readedAugmentation = augmentedNode.getAugmentation(FlowCapableNode.class);
136         assertEquals(fnu.getDescription(), readedAugmentation.getDescription());
137         assertBindingIndependentVersion(NODE_INSTANCE_ID_BI);
138         testNodeRemove();
139         assertTrue(lastReceivedChangeEvent.get(1000,TimeUnit.MILLISECONDS).getRemovedOperationalData().contains(FLOW_AUGMENTATION_PATH));
140     }
141
142     @Test
143     @Ignore
144     public void putNodeWithAugmentation() throws Exception {
145         lastReceivedChangeEvent = SettableFuture.create();
146         baDataService.registerDataChangeListener(ALL_FLOW_CAPABLE_NODES, this);
147
148         NodeBuilder nodeBuilder = new NodeBuilder();
149         nodeBuilder.setId(new NodeId(NODE_ID));
150         nodeBuilder.setKey(NODE_KEY);
151         FlowCapableNodeBuilder fnub = new FlowCapableNodeBuilder();
152         fnub.setHardware("Hardware Foo");
153         fnub.setManufacturer("Manufacturer Foo");
154         fnub.setSerialNumber("Serial Foo");
155         fnub.setDescription("Description Foo");
156         fnub.setSoftware("JUnit emulated");
157         FlowCapableNode fnu = fnub.build();
158
159         nodeBuilder.addAugmentation(FlowCapableNode.class, fnu);
160         DataModificationTransaction baseTransaction = baDataService.beginTransaction();
161         baseTransaction.putOperationalData(NODE_INSTANCE_ID_BA, nodeBuilder.build());
162         RpcResult<TransactionStatus> result = baseTransaction.commit().get();
163
164
165         DataChangeEvent<InstanceIdentifier<?>, DataObject> potential = lastReceivedChangeEvent.get(1000,TimeUnit.MILLISECONDS);
166         assertNotNull(potential);
167         assertTrue(potential.getCreatedOperationalData().containsKey(FLOW_AUGMENTATION_PATH));
168         lastReceivedChangeEvent = SettableFuture.create();
169         assertEquals(TransactionStatus.COMMITED, result.getResult());
170
171         FlowCapableNode readedAugmentation = (FlowCapableNode) baDataService.readOperationalData(
172                 NODE_INSTANCE_ID_BA.augmentation(FlowCapableNode.class));
173         assertNotNull(readedAugmentation);
174
175         assertEquals(fnu.getHardware(), readedAugmentation.getHardware());
176
177         testPutNodeConnectorWithAugmentation();
178         lastReceivedChangeEvent = SettableFuture.create();
179         testNodeRemove();
180
181         assertTrue(lastReceivedChangeEvent.get(1000,TimeUnit.MILLISECONDS).getRemovedOperationalData().contains(FLOW_AUGMENTATION_PATH));
182     }
183
184     private void testPutNodeConnectorWithAugmentation() throws Exception {
185         NodeConnectorKey ncKey = new NodeConnectorKey(new NodeConnectorId("test:0:0"));
186         InstanceIdentifier<NodeConnector> ncPath = NODE_INSTANCE_ID_BA
187                 .child(NodeConnector.class, ncKey);
188         InstanceIdentifier<FlowCapableNodeConnector> ncAugmentPath = ncPath
189                 .augmentation(FlowCapableNodeConnector.class);
190
191         NodeConnectorBuilder nc = new NodeConnectorBuilder();
192         nc.setKey(ncKey);
193
194         FlowCapableNodeConnectorBuilder fncb = new FlowCapableNodeConnectorBuilder();
195         fncb.setName("Baz");
196         nc.addAugmentation(FlowCapableNodeConnector.class, fncb.build());
197
198         DataModificationTransaction baseTransaction = baDataService.beginTransaction();
199         baseTransaction.putOperationalData(ncPath, nc.build());
200         RpcResult<TransactionStatus> result = baseTransaction.commit().get();
201         assertEquals(TransactionStatus.COMMITED, result.getResult());
202
203         FlowCapableNodeConnector readedAugmentation = (FlowCapableNodeConnector) baDataService
204                 .readOperationalData(ncAugmentPath);
205         assertNotNull(readedAugmentation);
206         assertEquals(fncb.getName(), readedAugmentation.getName());
207     }
208
209     private void testNodeRemove() throws Exception {
210         DataModificationTransaction transaction = baDataService.beginTransaction();
211         transaction.removeOperationalData(NODE_INSTANCE_ID_BA);
212         RpcResult<TransactionStatus> result = transaction.commit().get();
213         assertEquals(TransactionStatus.COMMITED, result.getResult());
214
215         Node node = (Node) baDataService.readOperationalData(NODE_INSTANCE_ID_BA);
216         assertNull(node);
217     }
218
219     private void assertBindingIndependentVersion(final org.opendaylight.yangtools.yang.data.api.InstanceIdentifier nodeId) {
220         CompositeNode node = biDataService.readOperationalData(nodeId);
221         assertNotNull(node);
222     }
223
224     @Override
225     public void onDataChanged(final DataChangeEvent<InstanceIdentifier<?>, DataObject> change) {
226         lastReceivedChangeEvent.set(change);
227     }
228
229 }