0a0c04b91586cbbda3d41dc24c4daca83ae78ae3
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / test / java / org / opendaylight / controller / cluster / datastore / DistributedDataStoreIntegrationTest.java
1 package org.opendaylight.controller.cluster.datastore;
2
3 import akka.actor.ActorSystem;
4 import akka.testkit.JavaTestKit;
5
6 import com.google.common.base.Optional;
7 import com.google.common.util.concurrent.ListenableFuture;
8 import junit.framework.Assert;
9 import org.junit.After;
10 import org.junit.Before;
11 import org.junit.Test;
12 import org.opendaylight.controller.cluster.datastore.shardstrategy.ShardStrategyFactory;
13 import org.opendaylight.controller.cluster.datastore.utils.MockClusterWrapper;
14 import org.opendaylight.controller.md.cluster.datastore.model.CarsModel;
15 import org.opendaylight.controller.md.cluster.datastore.model.PeopleModel;
16 import org.opendaylight.controller.md.cluster.datastore.model.SchemaContextHelper;
17 import org.opendaylight.controller.md.cluster.datastore.model.TestModel;
18 import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadWriteTransaction;
19 import org.opendaylight.controller.sal.core.spi.data.DOMStoreThreePhaseCommitCohort;
20 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
21 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
22
23 import java.util.concurrent.ExecutionException;
24 import java.util.concurrent.TimeUnit;
25 import java.util.concurrent.TimeoutException;
26
27 import static junit.framework.Assert.assertEquals;
28 import static junit.framework.Assert.assertTrue;
29
30 public class DistributedDataStoreIntegrationTest{
31
32     private static ActorSystem system;
33
34     @Before
35     public void setUp() {
36         System.setProperty("shard.persistent", "false");
37         system = ActorSystem.create("test");
38     }
39
40     @After
41     public void tearDown() {
42         JavaTestKit.shutdownActorSystem(system);
43         system = null;
44     }
45
46     protected ActorSystem getSystem() {
47         return system;
48     }
49
50     @Test
51     public void integrationTest() throws Exception {
52         Configuration configuration = new ConfigurationImpl("module-shards.conf", "modules.conf");
53         ShardStrategyFactory.setConfiguration(configuration);
54         DistributedDataStore distributedDataStore =
55             new DistributedDataStore(getSystem(), "config", new MockClusterWrapper(), configuration);
56
57         distributedDataStore.onGlobalContextUpdated(TestModel.createTestContext());
58
59         Thread.sleep(1500);
60
61         DOMStoreReadWriteTransaction transaction =
62             distributedDataStore.newReadWriteTransaction();
63
64         transaction.write(TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
65
66         ListenableFuture<Optional<NormalizedNode<?, ?>>> future =
67             transaction.read(TestModel.TEST_PATH);
68
69         Optional<NormalizedNode<?, ?>> optional = future.get();
70
71         Assert.assertTrue(optional.isPresent());
72
73         NormalizedNode<?, ?> normalizedNode = optional.get();
74
75         assertEquals(TestModel.TEST_QNAME, normalizedNode.getNodeType());
76
77         DOMStoreThreePhaseCommitCohort ready = transaction.ready();
78
79         ListenableFuture<Boolean> canCommit = ready.canCommit();
80
81         assertTrue(canCommit.get(5, TimeUnit.SECONDS));
82
83         ListenableFuture<Void> preCommit = ready.preCommit();
84
85         preCommit.get(5, TimeUnit.SECONDS);
86
87         ListenableFuture<Void> commit = ready.commit();
88
89         commit.get(5, TimeUnit.SECONDS);
90     }
91
92
93     @Test
94     public void integrationTestWithMultiShardConfiguration()
95         throws ExecutionException, InterruptedException, TimeoutException {
96         Configuration configuration = new ConfigurationImpl("module-shards.conf", "modules.conf");
97
98         ShardStrategyFactory.setConfiguration(configuration);
99         DistributedDataStore distributedDataStore =
100             new DistributedDataStore(getSystem(), "config", new MockClusterWrapper(), configuration);
101
102
103         distributedDataStore.onGlobalContextUpdated(SchemaContextHelper.full());
104
105         // This sleep is fragile - test can fail intermittently if all Shards aren't updated with
106         // the SchemaContext in time. Is there any way we can make this deterministic?
107         Thread.sleep(2000);
108
109         DOMStoreReadWriteTransaction transaction =
110             distributedDataStore.newReadWriteTransaction();
111
112         transaction.write(CarsModel.BASE_PATH, CarsModel.emptyContainer());
113         transaction.write(PeopleModel.BASE_PATH, PeopleModel.emptyContainer());
114
115         DOMStoreThreePhaseCommitCohort ready = transaction.ready();
116
117         ListenableFuture<Boolean> canCommit = ready.canCommit();
118
119         assertTrue(canCommit.get(5, TimeUnit.SECONDS));
120
121         ListenableFuture<Void> preCommit = ready.preCommit();
122
123         preCommit.get(5, TimeUnit.SECONDS);
124
125         ListenableFuture<Void> commit = ready.commit();
126
127         commit.get(5, TimeUnit.SECONDS);
128     }
129
130 }