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.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;
46 import com.google.common.util.concurrent.SettableFuture;
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<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();
71 private static final InstanceIdentifier<FlowCapableNode> ALL_FLOW_CAPABLE_NODES = //
72 NODES_INSTANCE_ID_BA.builder() //
74 .augmentation(FlowCapableNode.class) //
77 private static final org.opendaylight.yangtools.yang.data.api.InstanceIdentifier NODE_INSTANCE_ID_BI = //
78 org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.builder() //
80 .nodeWithKey(Node.QNAME, NODE_KEY_BI) //
82 private static final QName SUPPORTED_ACTIONS_QNAME = QName.create(FlowCapableNode.QNAME,
83 SupportedActions.QNAME.getLocalName());
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() //
88 .nodeWithKey(Node.QNAME, NODE_KEY_BI) //
89 .node(SUPPORTED_ACTIONS_QNAME) //
91 private static final InstanceIdentifier<FlowCapableNode> FLOW_AUGMENTATION_PATH =
92 NODE_INSTANCE_ID_BA.builder() //
93 .augmentation(FlowCapableNode.class) //
96 private SettableFuture<DataChangeEvent<InstanceIdentifier<?>, DataObject>> lastReceivedChangeEvent;
105 public void putNodeAndAugmentation() throws Exception {
106 lastReceivedChangeEvent = SettableFuture.create();
107 baDataService.registerDataChangeListener(ALL_FLOW_CAPABLE_NODES, this);
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());
118 Node node = (Node) baDataService.readOperationalData(NODE_INSTANCE_ID_BA);
120 assertEquals(NODE_KEY, node.getKey());
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);
135 lastReceivedChangeEvent = SettableFuture.create();
136 result = augmentedTransaction.commit().get();
137 assertEquals(TransactionStatus.COMMITED, result.getResult());
139 DataChangeEvent<InstanceIdentifier<?>, DataObject> potential = lastReceivedChangeEvent.get(1000,TimeUnit.MILLISECONDS);
140 assertNotNull(potential);
141 assertTrue(potential.getCreatedOperationalData().containsKey(FLOW_AUGMENTATION_PATH));
143 lastReceivedChangeEvent = SettableFuture.create();
145 Node augmentedNode = (Node) baDataService.readOperationalData(NODE_INSTANCE_ID_BA);
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);
154 assertTrue(lastReceivedChangeEvent.get(1000,TimeUnit.MILLISECONDS).getRemovedOperationalData().contains(FLOW_AUGMENTATION_PATH));
159 public void putNodeWithAugmentation() throws Exception {
160 lastReceivedChangeEvent = SettableFuture.create();
161 baDataService.registerDataChangeListener(ALL_FLOW_CAPABLE_NODES, this);
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();
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();
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());
186 FlowCapableNode readedAugmentation = (FlowCapableNode) baDataService.readOperationalData(
187 NODE_INSTANCE_ID_BA.augmentation(FlowCapableNode.class));
188 assertNotNull(readedAugmentation);
190 assertEquals(fnu.getHardware(), readedAugmentation.getHardware());
192 testPutNodeConnectorWithAugmentation();
193 lastReceivedChangeEvent = SettableFuture.create();
196 assertTrue(lastReceivedChangeEvent.get(1000,TimeUnit.MILLISECONDS).getRemovedOperationalData().contains(FLOW_AUGMENTATION_PATH));
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);
206 NodeConnectorBuilder nc = new NodeConnectorBuilder();
209 FlowCapableNodeConnectorBuilder fncb = new FlowCapableNodeConnectorBuilder();
211 nc.addAugmentation(FlowCapableNodeConnector.class, fncb.build());
213 DataModificationTransaction baseTransaction = baDataService.beginTransaction();
214 baseTransaction.putOperationalData(ncPath, nc.build());
215 RpcResult<TransactionStatus> result = baseTransaction.commit().get();
216 assertEquals(TransactionStatus.COMMITED, result.getResult());
218 FlowCapableNodeConnector readedAugmentation = (FlowCapableNodeConnector) baDataService
219 .readOperationalData(ncAugmentPath);
220 assertNotNull(readedAugmentation);
221 assertEquals(fncb.getName(), readedAugmentation.getName());
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());
230 Node node = (Node) baDataService.readOperationalData(NODE_INSTANCE_ID_BA);
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());
242 FlowCapableNode fnu = original.getAugmentation(FlowCapableNode.class);
243 FlowCapableNode readedAugment = readedNode.getAugmentation(FlowCapableNode.class);
245 assertEquals(fnu.getDescription(), readedAugment.getDescription());
246 assertEquals(fnu.getSerialNumber(), readedAugment.getSerialNumber());
250 private void assertBindingIndependentVersion(final org.opendaylight.yangtools.yang.data.api.InstanceIdentifier nodeId) {
251 CompositeNode node = biDataService.readOperationalData(nodeId);
255 private Nodes checkForNodes() {
256 return (Nodes) baDataService.readOperationalData(NODES_INSTANCE_ID_BA);
260 public void onDataChanged(final DataChangeEvent<InstanceIdentifier<?>, DataObject> change) {
261 lastReceivedChangeEvent.set(change);