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.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;
45 import com.google.common.util.concurrent.SettableFuture;
47 @SuppressWarnings("deprecation")
48 public class PutAugmentationTest extends AbstractDataServiceTest implements DataChangeListener {
50 private static final QName NODE_ID_QNAME = QName.create(Node.QNAME, "id");
51 private static final String NODE_ID = "openflow:1";
53 private static final NodeKey NODE_KEY = new NodeKey(new NodeId(NODE_ID));
55 private static final Map<QName, Object> NODE_KEY_BI = Collections.<QName, Object> singletonMap(NODE_ID_QNAME,
58 private static final InstanceIdentifier<Nodes> NODES_INSTANCE_ID_BA = InstanceIdentifier.builder(Nodes.class) //
61 private static final InstanceIdentifier<Node> NODE_INSTANCE_ID_BA = //
62 NODES_INSTANCE_ID_BA.builder() //
63 .child(Node.class, NODE_KEY).toInstance();
65 private static final InstanceIdentifier<FlowCapableNode> ALL_FLOW_CAPABLE_NODES = //
66 NODES_INSTANCE_ID_BA.builder() //
68 .augmentation(FlowCapableNode.class) //
71 private static final org.opendaylight.yangtools.yang.data.api.InstanceIdentifier NODE_INSTANCE_ID_BI = //
72 org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.builder() //
74 .nodeWithKey(Node.QNAME, NODE_KEY_BI) //
76 private static final InstanceIdentifier<FlowCapableNode> FLOW_AUGMENTATION_PATH =
77 NODE_INSTANCE_ID_BA.builder() //
78 .augmentation(FlowCapableNode.class) //
81 private SettableFuture<DataChangeEvent<InstanceIdentifier<?>, DataObject>> lastReceivedChangeEvent;
90 public void putNodeAndAugmentation() throws Exception {
91 lastReceivedChangeEvent = SettableFuture.create();
92 baDataService.registerDataChangeListener(ALL_FLOW_CAPABLE_NODES, this);
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());
103 Node node = (Node) baDataService.readOperationalData(NODE_INSTANCE_ID_BA);
105 assertEquals(NODE_KEY, node.getKey());
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);
120 lastReceivedChangeEvent = SettableFuture.create();
121 result = augmentedTransaction.commit().get();
122 assertEquals(TransactionStatus.COMMITED, result.getResult());
124 DataChangeEvent<InstanceIdentifier<?>, DataObject> potential = lastReceivedChangeEvent.get(1000,TimeUnit.MILLISECONDS);
125 assertNotNull(potential);
126 assertTrue(potential.getCreatedOperationalData().containsKey(FLOW_AUGMENTATION_PATH));
128 lastReceivedChangeEvent = SettableFuture.create();
130 Node augmentedNode = (Node) baDataService.readOperationalData(NODE_INSTANCE_ID_BA);
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);
139 assertTrue(lastReceivedChangeEvent.get(1000,TimeUnit.MILLISECONDS).getRemovedOperationalData().contains(FLOW_AUGMENTATION_PATH));
144 public void putNodeWithAugmentation() throws Exception {
145 lastReceivedChangeEvent = SettableFuture.create();
146 baDataService.registerDataChangeListener(ALL_FLOW_CAPABLE_NODES, this);
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();
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();
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());
171 FlowCapableNode readedAugmentation = (FlowCapableNode) baDataService.readOperationalData(
172 NODE_INSTANCE_ID_BA.augmentation(FlowCapableNode.class));
173 assertNotNull(readedAugmentation);
175 assertEquals(fnu.getHardware(), readedAugmentation.getHardware());
177 testPutNodeConnectorWithAugmentation();
178 lastReceivedChangeEvent = SettableFuture.create();
181 assertTrue(lastReceivedChangeEvent.get(1000,TimeUnit.MILLISECONDS).getRemovedOperationalData().contains(FLOW_AUGMENTATION_PATH));
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);
191 NodeConnectorBuilder nc = new NodeConnectorBuilder();
194 FlowCapableNodeConnectorBuilder fncb = new FlowCapableNodeConnectorBuilder();
196 nc.addAugmentation(FlowCapableNodeConnector.class, fncb.build());
198 DataModificationTransaction baseTransaction = baDataService.beginTransaction();
199 baseTransaction.putOperationalData(ncPath, nc.build());
200 RpcResult<TransactionStatus> result = baseTransaction.commit().get();
201 assertEquals(TransactionStatus.COMMITED, result.getResult());
203 FlowCapableNodeConnector readedAugmentation = (FlowCapableNodeConnector) baDataService
204 .readOperationalData(ncAugmentPath);
205 assertNotNull(readedAugmentation);
206 assertEquals(fncb.getName(), readedAugmentation.getName());
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());
215 Node node = (Node) baDataService.readOperationalData(NODE_INSTANCE_ID_BA);
219 private void assertBindingIndependentVersion(final org.opendaylight.yangtools.yang.data.api.InstanceIdentifier nodeId) {
220 CompositeNode node = biDataService.readOperationalData(nodeId);
225 public void onDataChanged(final DataChangeEvent<InstanceIdentifier<?>, DataObject> change) {
226 lastReceivedChangeEvent.set(change);