Bug 2597: Batch modification operations in TransactionProxy
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / test / java / org / opendaylight / controller / cluster / datastore / modification / MutableCompositeModificationTest.java
1 package org.opendaylight.controller.cluster.datastore.modification;
2
3 import static org.junit.Assert.assertEquals;
4 import static org.junit.Assert.assertNotNull;
5 import com.google.common.base.Optional;
6 import com.google.common.base.Stopwatch;
7 import org.apache.commons.lang.SerializationUtils;
8 import org.junit.Ignore;
9 import org.junit.Test;
10 import org.opendaylight.controller.cluster.datastore.DataStoreVersions;
11 import org.opendaylight.controller.md.cluster.datastore.model.TestModel;
12 import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadWriteTransaction;
13 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
14 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
15 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
16 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeBuilder;
17
18 public class MutableCompositeModificationTest extends AbstractModificationTest {
19
20     @Test
21     public void testApply() throws Exception {
22
23         MutableCompositeModification compositeModification = new MutableCompositeModification();
24         compositeModification.addModification(new WriteModification(TestModel.TEST_PATH,
25             ImmutableNodes.containerNode(TestModel.TEST_QNAME)));
26
27         DOMStoreReadWriteTransaction transaction = store.newReadWriteTransaction();
28         compositeModification.apply(transaction);
29         commitTransaction(transaction);
30
31         Optional<NormalizedNode<?, ?>> data = readData(TestModel.TEST_PATH);
32
33         assertNotNull(data.get());
34         assertEquals(TestModel.TEST_QNAME, data.get().getNodeType());
35     }
36
37     @Test
38     public void testSerialization() {
39         YangInstanceIdentifier writePath = TestModel.TEST_PATH;
40         NormalizedNode<?, ?> writeData = ImmutableContainerNodeBuilder.create().withNodeIdentifier(
41                 new YangInstanceIdentifier.NodeIdentifier(TestModel.TEST_QNAME)).
42                 withChild(ImmutableNodes.leafNode(TestModel.DESC_QNAME, "foo")).build();
43
44         YangInstanceIdentifier mergePath = TestModel.OUTER_LIST_PATH;
45         NormalizedNode<?, ?> mergeData = ImmutableContainerNodeBuilder.create().withNodeIdentifier(
46                 new YangInstanceIdentifier.NodeIdentifier(TestModel.OUTER_LIST_QNAME)).build();
47
48         YangInstanceIdentifier deletePath = TestModel.TEST_PATH;
49
50         MutableCompositeModification compositeModification = new MutableCompositeModification();
51         compositeModification.addModification(new WriteModification(writePath, writeData));
52         compositeModification.addModification(new MergeModification(mergePath, mergeData));
53         compositeModification.addModification(new DeleteModification(deletePath));
54
55         MutableCompositeModification clone = (MutableCompositeModification) SerializationUtils.clone(compositeModification);
56
57         assertEquals("getVersion", DataStoreVersions.CURRENT_VERSION, clone.getVersion());
58
59         assertEquals("getModifications size", 3, clone.getModifications().size());
60
61         WriteModification write = (WriteModification)clone.getModifications().get(0);
62         assertEquals("getVersion", DataStoreVersions.CURRENT_VERSION, write.getVersion());
63         assertEquals("getPath", writePath, write.getPath());
64         assertEquals("getData", writeData, write.getData());
65
66         MergeModification merge = (MergeModification)clone.getModifications().get(1);
67         assertEquals("getVersion", DataStoreVersions.CURRENT_VERSION, merge.getVersion());
68         assertEquals("getPath", mergePath, merge.getPath());
69         assertEquals("getData", mergeData, merge.getData());
70
71         DeleteModification delete = (DeleteModification)clone.getModifications().get(2);
72         assertEquals("getVersion", DataStoreVersions.CURRENT_VERSION, delete.getVersion());
73         assertEquals("getPath", deletePath, delete.getPath());
74     }
75
76     @Test
77     @Ignore
78     public void testSerializationScale() throws Exception {
79         YangInstanceIdentifier writePath = TestModel.TEST_PATH;
80         NormalizedNode<?, ?> writeData = ImmutableContainerNodeBuilder.create().withNodeIdentifier(
81                 new YangInstanceIdentifier.NodeIdentifier(TestModel.TEST_QNAME)).
82                 withChild(ImmutableNodes.leafNode(TestModel.DESC_QNAME, "foo")).build();
83
84         MutableCompositeModification compositeModification = new MutableCompositeModification();
85         for(int i = 0; i < 1000; i++) {
86             compositeModification.addModification(new WriteModification(writePath, writeData));
87         }
88
89         Stopwatch sw = Stopwatch.createStarted();
90         for(int i = 0; i < 1000; i++) {
91             new ModificationPayload(compositeModification);
92         }
93
94         sw.stop();
95         System.out.println("Elapsed: "+sw);
96
97         ModificationPayload p = new ModificationPayload(compositeModification);
98         sw.start();
99         for(int i = 0; i < 1000; i++) {
100             p.getModification();
101         }
102
103         sw.stop();
104         System.out.println("Elapsed: "+sw);
105     }
106 }