Bug 4149: Implement per-shard DatastoreContext settings
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / test / java / org / opendaylight / controller / cluster / datastore / IntegrationTestKit.java
1 /*
2  * Copyright (c) 2015 Brocade Communications 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.cluster.datastore;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12 import static org.junit.Assert.fail;
13 import akka.actor.ActorRef;
14 import akka.actor.ActorSystem;
15 import akka.actor.PoisonPill;
16 import com.google.common.base.Optional;
17 import com.google.common.util.concurrent.ListenableFuture;
18 import com.google.common.util.concurrent.Uninterruptibles;
19 import java.util.concurrent.Callable;
20 import java.util.concurrent.TimeUnit;
21 import org.mockito.Mockito;
22 import org.opendaylight.controller.cluster.datastore.DatastoreContext.Builder;
23 import org.opendaylight.controller.cluster.datastore.config.Configuration;
24 import org.opendaylight.controller.cluster.datastore.config.ConfigurationImpl;
25 import org.opendaylight.controller.cluster.datastore.utils.ActorContext;
26 import org.opendaylight.controller.md.cluster.datastore.model.SchemaContextHelper;
27 import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadTransaction;
28 import org.opendaylight.controller.sal.core.spi.data.DOMStoreThreePhaseCommitCohort;
29 import org.opendaylight.controller.sal.core.spi.data.DOMStoreTransactionChain;
30 import org.opendaylight.controller.sal.core.spi.data.DOMStoreWriteTransaction;
31 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
32 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
33 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
34
35 public class IntegrationTestKit extends ShardTestKit {
36
37     DatastoreContext.Builder datastoreContextBuilder;
38
39     public IntegrationTestKit(ActorSystem actorSystem, Builder datastoreContextBuilder) {
40         super(actorSystem);
41         this.datastoreContextBuilder = datastoreContextBuilder;
42     }
43
44     public DistributedDataStore setupDistributedDataStore(String typeName, String... shardNames) {
45         return setupDistributedDataStore(typeName, "module-shards.conf", true, SchemaContextHelper.full(), shardNames);
46     }
47
48     public DistributedDataStore setupDistributedDataStore(String typeName, boolean waitUntilLeader,
49             String... shardNames) {
50         return setupDistributedDataStore(typeName, "module-shards.conf", waitUntilLeader,
51                 SchemaContextHelper.full(), shardNames);
52     }
53
54     public DistributedDataStore setupDistributedDataStore(String typeName, String moduleShardsConfig,
55             boolean waitUntilLeader, String... shardNames) {
56         return setupDistributedDataStore(typeName, moduleShardsConfig, waitUntilLeader,
57                 SchemaContextHelper.full(), shardNames);
58     }
59
60     public DistributedDataStore setupDistributedDataStore(String typeName, String moduleShardsConfig,
61             boolean waitUntilLeader, SchemaContext schemaContext, String... shardNames) {
62         ClusterWrapper cluster = new ClusterWrapperImpl(getSystem());
63         Configuration config = new ConfigurationImpl(moduleShardsConfig, "modules.conf");
64
65         datastoreContextBuilder.dataStoreType(typeName);
66
67         DatastoreContext datastoreContext = datastoreContextBuilder.build();
68         DatastoreContextFactory mockContextFactory = Mockito.mock(DatastoreContextFactory.class);
69         Mockito.doReturn(datastoreContext).when(mockContextFactory).getBaseDatastoreContext();
70         Mockito.doReturn(datastoreContext).when(mockContextFactory).getShardDatastoreContext(Mockito.anyString());
71
72         DistributedDataStore dataStore = new DistributedDataStore(getSystem(), cluster, config, mockContextFactory);
73
74         dataStore.onGlobalContextUpdated(schemaContext);
75
76         if(waitUntilLeader) {
77             waitUntilLeader(dataStore.getActorContext(), shardNames);
78         }
79
80         return dataStore;
81     }
82
83     public void waitUntilLeader(ActorContext actorContext, String... shardNames) {
84         for(String shardName: shardNames) {
85             ActorRef shard = findLocalShard(actorContext, shardName);
86
87             assertNotNull("Shard was not created", shard);
88
89             waitUntilLeader(shard);
90         }
91     }
92
93     public void waitUntilNoLeader(ActorContext actorContext, String... shardNames) {
94         for(String shardName: shardNames) {
95             ActorRef shard = findLocalShard(actorContext, shardName);
96             assertNotNull("No local shard found", shard);
97
98             waitUntilNoLeader(shard);
99         }
100     }
101
102     private static ActorRef findLocalShard(ActorContext actorContext, String shardName) {
103         ActorRef shard = null;
104         for(int i = 0; i < 20 * 5 && shard == null; i++) {
105             Uninterruptibles.sleepUninterruptibly(50, TimeUnit.MILLISECONDS);
106             Optional<ActorRef> shardReply = actorContext.findLocalShard(shardName);
107             if(shardReply.isPresent()) {
108                 shard = shardReply.get();
109             }
110         }
111         return shard;
112     }
113
114     void testWriteTransaction(DistributedDataStore dataStore, YangInstanceIdentifier nodePath,
115             NormalizedNode<?, ?> nodeToWrite) throws Exception {
116
117         // 1. Create a write-only Tx
118
119         DOMStoreWriteTransaction writeTx = dataStore.newWriteOnlyTransaction();
120         assertNotNull("newWriteOnlyTransaction returned null", writeTx);
121
122         // 2. Write some data
123
124         writeTx.write(nodePath, nodeToWrite);
125
126         // 3. Ready the Tx for commit
127
128         DOMStoreThreePhaseCommitCohort cohort = writeTx.ready();
129
130         // 4. Commit the Tx
131
132         doCommit(cohort);
133
134         // 5. Verify the data in the store
135
136         DOMStoreReadTransaction readTx = dataStore.newReadOnlyTransaction();
137
138         Optional<NormalizedNode<?, ?>> optional = readTx.read(nodePath).get(5, TimeUnit.SECONDS);
139         assertEquals("isPresent", true, optional.isPresent());
140         assertEquals("Data node", nodeToWrite, optional.get());
141     }
142
143     void doCommit(final DOMStoreThreePhaseCommitCohort cohort) throws Exception {
144         Boolean canCommit = cohort.canCommit().get(7, TimeUnit.SECONDS);
145         assertEquals("canCommit", true, canCommit);
146         cohort.preCommit().get(5, TimeUnit.SECONDS);
147         cohort.commit().get(5, TimeUnit.SECONDS);
148     }
149
150     void doCommit(final ListenableFuture<Boolean> canCommitFuture, final DOMStoreThreePhaseCommitCohort cohort) throws Exception {
151         Boolean canCommit = canCommitFuture.get(7, TimeUnit.SECONDS);
152         assertEquals("canCommit", true, canCommit);
153         cohort.preCommit().get(5, TimeUnit.SECONDS);
154         cohort.commit().get(5, TimeUnit.SECONDS);
155     }
156
157     void cleanup(DistributedDataStore dataStore) {
158         if(dataStore != null) {
159             dataStore.getActorContext().getShardManager().tell(PoisonPill.getInstance(), null);
160         }
161     }
162
163     void assertExceptionOnCall(Callable<Void> callable, Class<? extends Exception> expType)
164             throws Exception {
165         try {
166             callable.call();
167             fail("Expected " + expType.getSimpleName());
168         } catch(Exception e) {
169             assertEquals("Exception type", expType, e.getClass());
170         }
171     }
172
173     void assertExceptionOnTxChainCreates(final DOMStoreTransactionChain txChain,
174             Class<? extends Exception> expType) throws Exception {
175         assertExceptionOnCall(new Callable<Void>() {
176             @Override
177             public Void call() throws Exception {
178                 txChain.newWriteOnlyTransaction();
179                 return null;
180             }
181         }, expType);
182
183         assertExceptionOnCall(new Callable<Void>() {
184             @Override
185             public Void call() throws Exception {
186                 txChain.newReadWriteTransaction();
187                 return null;
188             }
189         }, expType);
190
191         assertExceptionOnCall(new Callable<Void>() {
192             @Override
193             public Void call() throws Exception {
194                 txChain.newReadOnlyTransaction();
195                 return null;
196             }
197         }, expType);
198     }
199 }