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