BUG-1092: adjust to YangInstanceIdentifier
[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.flow.inventory.rev130819.FlowCapableNode;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNodeBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.flow.node.SupportedActions;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.flow.node.SupportedActionsBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.flow.node.supported.actions.ActionType;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.flow.node.supported.actions.ActionTypeBuilder;
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.SupportType;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnector;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnectorBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnectorKey;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
41 import org.opendaylight.yangtools.yang.binding.DataObject;
42 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
43 import org.opendaylight.yangtools.yang.common.QName;
44 import org.opendaylight.yangtools.yang.common.RpcResult;
45 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
46
47 import com.google.common.util.concurrent.SettableFuture;
48
49 @SuppressWarnings("deprecation")
50 public class DOMCodecBug03Test extends AbstractDataServiceTest implements DataChangeListener {
51
52     private static final QName NODE_ID_QNAME = QName.create(Node.QNAME, "id");
53     private static final String NODE_ID = "openflow:1";
54
55     private static final NodeKey NODE_KEY = new NodeKey(new NodeId(NODE_ID));
56
57     private static final Map<QName, Object> NODE_KEY_BI = Collections.<QName, Object> singletonMap(NODE_ID_QNAME,
58             NODE_ID);
59
60     private static final InstanceIdentifier<Nodes> NODES_INSTANCE_ID_BA = InstanceIdentifier.builder(Nodes.class) //
61             .toInstance();
62
63
64     private static final InstanceIdentifier<Node> NODE_INSTANCE_ID_BA = NODES_INSTANCE_ID_BA.child(Node.class, NODE_KEY);
65
66
67     private static final InstanceIdentifier<SupportedActions> SUPPORTED_ACTIONS_INSTANCE_ID_BA = //
68             NODES_INSTANCE_ID_BA.builder() //
69             .child(Node.class, NODE_KEY) //
70             .augmentation(FlowCapableNode.class) //
71             .child(SupportedActions.class)
72             .toInstance();
73
74
75     private static final org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier NODE_INSTANCE_ID_BI = //
76     org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.builder() //
77             .node(Nodes.QNAME) //
78             .nodeWithKey(Node.QNAME, NODE_KEY_BI) //
79             .toInstance();
80     private static final QName SUPPORTED_ACTIONS_QNAME = QName.create(FlowCapableNode.QNAME, SupportedActions.QNAME.getLocalName());
81
82
83     private static final org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier SUPPORTED_ACTIONS_INSTANCE_ID_BI = //
84             org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.builder() //
85                     .node(Nodes.QNAME) //
86                     .nodeWithKey(Node.QNAME, NODE_KEY_BI) //
87                     .node(SUPPORTED_ACTIONS_QNAME) //
88                     .toInstance();
89
90     private final SettableFuture<DataChangeEvent<InstanceIdentifier<?>, DataObject>> receivedChangeEvent = SettableFuture.create();
91
92
93
94     /**
95      * Test for Bug 148
96      *
97      * @throws Exception
98      */
99     @Test
100     public void testAugmentSerialization() throws Exception {
101
102
103         baDataService.registerDataChangeListener(NODES_INSTANCE_ID_BA, this);
104
105         NodeBuilder nodeBuilder = new NodeBuilder();
106         nodeBuilder.setId(new NodeId(NODE_ID));
107         nodeBuilder.setKey(NODE_KEY);
108         DataModificationTransaction transaction = baDataService.beginTransaction();
109
110
111         FlowCapableNodeBuilder fnub = new FlowCapableNodeBuilder();
112         fnub.setHardware("Hardware Foo");
113         fnub.setManufacturer("Manufacturer Foo");
114         fnub.setSerialNumber("Serial Foo");
115         fnub.setDescription("Description Foo");
116         fnub.setSoftware("JUnit emulated");
117         FlowCapableNode fnu = fnub.build();
118         nodeBuilder.addAugmentation(FlowCapableNode.class, fnu);
119         Node original = nodeBuilder.build();
120         transaction.putOperationalData(NODE_INSTANCE_ID_BA, original);
121
122         RpcResult<TransactionStatus> result = transaction.commit().get();
123         assertEquals(TransactionStatus.COMMITED, result.getResult());
124
125         DataChangeEvent<InstanceIdentifier<?>, DataObject> potential = receivedChangeEvent.get(1000,TimeUnit.MILLISECONDS);
126         assertNotNull(potential);
127
128         verifyNodes((Nodes) potential.getUpdatedOperationalSubtree(),original);
129         assertBindingIndependentVersion(NODE_INSTANCE_ID_BI);
130         Nodes nodes = checkForNodes();
131         verifyNodes(nodes,original);
132
133         testAddingNodeConnector();
134         testNodeRemove();
135
136     }
137
138     @Test
139     public void testAugmentNestedSerialization() throws Exception {
140         DataModificationTransaction transaction = baDataService.beginTransaction();
141
142         SupportedActionsBuilder actions = new SupportedActionsBuilder();
143         ActionTypeBuilder action = new ActionTypeBuilder();
144         action.setAction("foo-action");
145         action.setSupportState(SupportType.Native);
146         List<ActionType> actionTypes = Collections.singletonList(action.build());
147         actions.setActionType(actionTypes );
148
149         transaction.putOperationalData(SUPPORTED_ACTIONS_INSTANCE_ID_BA, actions.build());
150         RpcResult<TransactionStatus> putResult = transaction.commit().get();
151         assertNotNull(putResult);
152         assertEquals(TransactionStatus.COMMITED, putResult.getResult());
153         SupportedActions readedTable = (SupportedActions) baDataService.readOperationalData(SUPPORTED_ACTIONS_INSTANCE_ID_BA);
154         assertNotNull(readedTable);
155
156         CompositeNode biSupportedActions = biDataService.readOperationalData(SUPPORTED_ACTIONS_INSTANCE_ID_BI);
157         assertNotNull(biSupportedActions);
158
159     }
160
161     private void testAddingNodeConnector() throws Exception {
162
163         NodeConnectorId ncId = new NodeConnectorId("openflow:1:bar");
164         NodeConnectorKey nodeKey = new NodeConnectorKey(ncId );
165         InstanceIdentifier<NodeConnector> ncInstanceId = NODE_INSTANCE_ID_BA.child(NodeConnector.class, nodeKey);
166         NodeConnectorBuilder ncBuilder = new NodeConnectorBuilder();
167         ncBuilder.setId(ncId);
168         ncBuilder.setKey(nodeKey);
169         NodeConnector connector = ncBuilder.build();
170         DataModificationTransaction transaction = baDataService.beginTransaction();
171         transaction.putOperationalData(ncInstanceId, connector);
172         RpcResult<TransactionStatus> result = transaction.commit().get();
173         assertEquals(TransactionStatus.COMMITED, result.getResult());
174         Node node = (Node) baDataService.readOperationalData(NODE_INSTANCE_ID_BA);
175         assertNotNull(node);
176         assertNotNull(node.getNodeConnector());
177         assertFalse(node.getNodeConnector().isEmpty());
178         NodeConnector readedNc = node.getNodeConnector().get(0);
179         assertNotNull(readedNc);
180     }
181
182     private void testNodeRemove() throws Exception {
183         DataModificationTransaction transaction = baDataService.beginTransaction();
184         transaction.removeOperationalData(NODE_INSTANCE_ID_BA);
185         RpcResult<TransactionStatus> result = transaction.commit().get();
186         assertEquals(TransactionStatus.COMMITED, result.getResult());
187
188         Node node = (Node) baDataService.readOperationalData(NODE_INSTANCE_ID_BA);
189         assertNull(node);
190     }
191
192     private void verifyNodes(final Nodes nodes,final Node original) {
193         assertNotNull(nodes);
194         assertNotNull(nodes.getNode());
195         assertEquals(1, nodes.getNode().size());
196         Node readedNode = nodes.getNode().get(0);
197         assertEquals(original.getId(), readedNode.getId());
198         assertEquals(original.getKey(), readedNode.getKey());
199
200         FlowCapableNode fnu = original.getAugmentation(FlowCapableNode.class);
201         FlowCapableNode readedAugment = readedNode.getAugmentation(FlowCapableNode.class);
202         assertNotNull(fnu);
203         assertEquals(fnu.getDescription(), readedAugment.getDescription());
204         assertEquals(fnu.getSerialNumber(), readedAugment.getSerialNumber());
205
206     }
207
208     private void assertBindingIndependentVersion(
209             final org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier nodeId) {
210         CompositeNode node = biDataService.readOperationalData(nodeId);
211         assertNotNull(node);
212     }
213
214     private Nodes checkForNodes() {
215         return (Nodes) baDataService.readOperationalData(NODES_INSTANCE_ID_BA);
216     }
217
218     @Override
219     public void onDataChanged(final DataChangeEvent<InstanceIdentifier<?>, DataObject> change) {
220         receivedChangeEvent.set(change);
221     }
222
223 }

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