25c57ccd5f81ebe3860930604127cf793b864ea1
[controller.git] / opendaylight / md-sal / sal-binding-broker / src / test / java / org / opendaylight / controller / sal / binding / test / bugfix / MultipleAugmentationPutsTest.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
14 import java.math.BigInteger;
15 import java.util.ArrayList;
16 import java.util.Collections;
17 import java.util.List;
18 import java.util.Map;
19
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.controller.sal.binding.test.AugmentationVerifier;
27 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev100924.Counter32;
28 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev100924.Counter64;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNodeBuilder;
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.nodes.Node;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.NodeMeterStatistics;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.NodeMeterStatisticsBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.nodes.node.meter.MeterStatisticsBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.types.rev130918.MeterId;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.types.rev130918.meter.statistics.DurationBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.types.rev130918.meter.statistics.reply.MeterStats;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.types.rev130918.meter.statistics.reply.MeterStatsBuilder;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.types.rev130918.meter.statistics.reply.MeterStatsKey;
44 import org.opendaylight.yangtools.yang.binding.Augmentation;
45 import org.opendaylight.yangtools.yang.binding.DataObject;
46 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
47 import org.opendaylight.yangtools.yang.common.QName;
48 import org.opendaylight.yangtools.yang.common.RpcResult;
49 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
50
51 public class MultipleAugmentationPutsTest extends AbstractDataServiceTest implements DataChangeListener {
52
53     private static final QName NODE_ID_QNAME = QName.create(Node.QNAME, "id");
54     private static final String NODE_ID = "openflow:1";
55
56     private static final NodeKey NODE_KEY = new NodeKey(new NodeId(NODE_ID));
57
58     private static final Map<QName, Object> NODE_KEY_BI = Collections.<QName, Object> singletonMap(NODE_ID_QNAME,
59             NODE_ID);
60
61     private static final InstanceIdentifier<Nodes> NODES_INSTANCE_ID_BA = InstanceIdentifier.builder(Nodes.class) //
62             .toInstance();
63
64     private static final InstanceIdentifier<Node> NODE_INSTANCE_ID_BA =
65             NODES_INSTANCE_ID_BA.child(Node.class, NODE_KEY);
66
67     private static final org.opendaylight.yangtools.yang.data.api.InstanceIdentifier NODE_INSTANCE_ID_BI = //
68     org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.builder() //
69             .node(Nodes.QNAME) //
70             .nodeWithKey(Node.QNAME, NODE_KEY_BI) //
71             .toInstance();
72     private DataChangeEvent<InstanceIdentifier<?>, DataObject> receivedChangeEvent;
73
74     /**
75      * Test for Bug 148
76      *
77      * @throws Exception
78      */
79     @Test( timeout = 15000)
80     public void testAugmentSerialization() throws Exception {
81
82         baDataService.registerDataChangeListener(NODES_INSTANCE_ID_BA, this);
83
84         Node flowCapableNode = createTestNode(FlowCapableNode.class, flowCapableNodeAugmentation());
85         commitNodeAndVerifyTransaction(flowCapableNode);
86
87         assertNotNull(receivedChangeEvent);
88         verifyNode((Nodes) receivedChangeEvent.getUpdatedOperationalSubtree(), flowCapableNode);
89
90         Nodes nodes = checkForNodes();
91         verifyNode(nodes, flowCapableNode).assertHasAugmentation(FlowCapableNode.class);
92         assertBindingIndependentVersion(NODE_INSTANCE_ID_BI);
93 //        Node meterStatsNode = createTestNode(NodeMeterStatistics.class, nodeMeterStatistics());
94 //        commitNodeAndVerifyTransaction(meterStatsNode);
95 //
96 //        assertNotNull(receivedChangeEvent);
97 //        verifyNode((Nodes) receivedChangeEvent.getUpdatedOperationalSubtree(), meterStatsNode);
98 //
99 //        assertBindingIndependentVersion(NODE_INSTANCE_ID_BI);
100 //
101 //        Node mergedNode = (Node) baDataService.readOperationalData(NODE_INSTANCE_ID_BA);
102 //
103 //        AugmentationVerifier.from(mergedNode) //
104 //                .assertHasAugmentation(FlowCapableNode.class) //
105 //                .assertHasAugmentation(NodeMeterStatistics.class);
106 //
107 //        assertBindingIndependentVersion(NODE_INSTANCE_ID_BI);
108 //
109 //        Node meterStatsNodeWithDuration = createTestNode(NodeMeterStatistics.class, nodeMeterStatistics(5, true));
110 //        commitNodeAndVerifyTransaction(meterStatsNodeWithDuration);
111 //
112 //
113 //        Node nodeWithUpdatedList = (Node) baDataService.readOperationalData(NODE_INSTANCE_ID_BA);
114 //        AugmentationVerifier.from(nodeWithUpdatedList) //
115 //                .assertHasAugmentation(FlowCapableNode.class) //
116 //                .assertHasAugmentation(NodeMeterStatistics.class);
117 //
118 //        List<MeterStats> meterStats = nodeWithUpdatedList.getAugmentation(NodeMeterStatistics.class).getMeterStatistics().getMeterStats();
119 //        assertNotNull(meterStats);
120 //        assertFalse(meterStats.isEmpty());
121 //        assertBindingIndependentVersion(NODE_INSTANCE_ID_BI);
122         testNodeRemove();
123     }
124
125     private <T extends Augmentation<Node>> Node createTestNode(Class<T> augmentationClass, T augmentation) {
126         NodeBuilder nodeBuilder = new NodeBuilder();
127         nodeBuilder.setId(new NodeId(NODE_ID));
128         nodeBuilder.setKey(NODE_KEY);
129         nodeBuilder.addAugmentation(augmentationClass, augmentation);
130         return nodeBuilder.build();
131     }
132
133     private DataModificationTransaction commitNodeAndVerifyTransaction(Node original) throws Exception {
134         DataModificationTransaction transaction = baDataService.beginTransaction();
135         transaction.putOperationalData(NODE_INSTANCE_ID_BA, original);
136         RpcResult<TransactionStatus> result = transaction.commit().get();
137         assertEquals(TransactionStatus.COMMITED, result.getResult());
138         return transaction;
139     }
140
141     private void testNodeRemove() throws Exception {
142         DataModificationTransaction transaction = baDataService.beginTransaction();
143         transaction.removeOperationalData(NODE_INSTANCE_ID_BA);
144         RpcResult<TransactionStatus> result = transaction.commit().get();
145         assertEquals(TransactionStatus.COMMITED, result.getResult());
146
147         Node node = (Node) baDataService.readOperationalData(NODE_INSTANCE_ID_BA);
148         assertNull(node);
149     }
150
151     private AugmentationVerifier<Node> verifyNode(Nodes nodes, Node original) {
152         assertNotNull(nodes);
153         assertNotNull(nodes.getNode());
154         assertEquals(1, nodes.getNode().size());
155         Node readedNode = nodes.getNode().get(0);
156         assertEquals(original.getId(), readedNode.getId());
157         assertEquals(original.getKey(), readedNode.getKey());
158         return new AugmentationVerifier<Node>(readedNode);
159     }
160
161     private void assertBindingIndependentVersion(org.opendaylight.yangtools.yang.data.api.InstanceIdentifier nodeId) {
162         CompositeNode node = biDataService.readOperationalData(nodeId);
163         assertNotNull(node);
164     }
165
166     private Nodes checkForNodes() {
167         return (Nodes) baDataService.readOperationalData(NODES_INSTANCE_ID_BA);
168     }
169
170     private NodeMeterStatistics nodeMeterStatistics() {
171         return nodeMeterStatistics(10, false);
172     }
173
174     private NodeMeterStatistics nodeMeterStatistics(int count, boolean setDuration) {
175         NodeMeterStatisticsBuilder nmsb = new NodeMeterStatisticsBuilder();
176         MeterStatisticsBuilder meterStats = new MeterStatisticsBuilder();
177
178         List<MeterStats> stats = new ArrayList<>(count);
179         for (int i = 0; i <= count; i++) {
180             MeterStatsBuilder statistic = new MeterStatsBuilder();
181             statistic.setKey(new MeterStatsKey(new MeterId((long) i)));
182             statistic.setByteInCount(new Counter64(BigInteger.valueOf(34590 + i)));
183             statistic.setFlowCount(new Counter32(4569L + i));
184
185             if (setDuration) {
186                 DurationBuilder duration = new DurationBuilder();
187                 duration.setNanosecond(new Counter32(70L));
188                 statistic.setDuration(duration.build());
189             }
190
191             stats.add(statistic.build());
192         }
193        // meterStats.setMeterStats(stats);
194         nmsb.setMeterStatistics(meterStats.build());
195         return nmsb.build();
196     }
197
198     private FlowCapableNode flowCapableNodeAugmentation() {
199         FlowCapableNodeBuilder fnub = new FlowCapableNodeBuilder();
200         fnub.setHardware("Hardware Foo");
201         fnub.setManufacturer("Manufacturer Foo");
202         fnub.setSerialNumber("Serial Foo");
203         fnub.setDescription("Description Foo");
204         fnub.setSoftware("JUnit emulated");
205         FlowCapableNode fnu = fnub.build();
206         return fnu;
207     }
208
209     @Override
210     public void onDataChanged(DataChangeEvent<InstanceIdentifier<?>, DataObject> change) {
211         receivedChangeEvent = change;
212     }
213
214 }

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