f24ac0eccf1eec391c7e45ec94bc7e55c1265458
[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
14 import akka.actor.ActorRef;
15 import akka.actor.ActorSystem;
16 import akka.cluster.Cluster;
17 import akka.cluster.ClusterEvent.CurrentClusterState;
18 import akka.cluster.Member;
19 import akka.cluster.MemberStatus;
20 import com.google.common.base.Optional;
21 import com.google.common.base.Stopwatch;
22 import com.google.common.collect.Sets;
23 import com.google.common.util.concurrent.ListenableFuture;
24 import com.google.common.util.concurrent.Uninterruptibles;
25 import java.util.Set;
26 import java.util.concurrent.Callable;
27 import java.util.concurrent.TimeUnit;
28 import org.mockito.Mockito;
29 import org.opendaylight.controller.cluster.datastore.DatastoreContext.Builder;
30 import org.opendaylight.controller.cluster.datastore.config.Configuration;
31 import org.opendaylight.controller.cluster.datastore.config.ConfigurationImpl;
32 import org.opendaylight.controller.cluster.datastore.config.EmptyModuleShardConfigProvider;
33 import org.opendaylight.controller.cluster.datastore.jmx.mbeans.shard.ShardStats;
34 import org.opendaylight.controller.cluster.datastore.messages.DatastoreSnapshot;
35 import org.opendaylight.controller.cluster.datastore.utils.ActorContext;
36 import org.opendaylight.controller.md.cluster.datastore.model.SchemaContextHelper;
37 import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadTransaction;
38 import org.opendaylight.controller.sal.core.spi.data.DOMStoreThreePhaseCommitCohort;
39 import org.opendaylight.controller.sal.core.spi.data.DOMStoreTransactionChain;
40 import org.opendaylight.controller.sal.core.spi.data.DOMStoreWriteTransaction;
41 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
42 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
43 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
44 import scala.concurrent.Await;
45 import scala.concurrent.Future;
46 import scala.concurrent.duration.Duration;
47
48 public class IntegrationTestKit extends ShardTestKit {
49
50     protected DatastoreContext.Builder datastoreContextBuilder;
51     protected DatastoreSnapshot restoreFromSnapshot;
52
53     public IntegrationTestKit(final ActorSystem actorSystem, final Builder datastoreContextBuilder) {
54         super(actorSystem);
55         this.datastoreContextBuilder = datastoreContextBuilder;
56     }
57
58     public DatastoreContext.Builder getDatastoreContextBuilder() {
59         return datastoreContextBuilder;
60     }
61
62     public AbstractDataStore setupDistributedDataStore(final String typeName, final String... shardNames) {
63         return setupDistributedDataStore(typeName, "module-shards.conf", true, SchemaContextHelper.full(), shardNames);
64     }
65
66     public AbstractDataStore setupDistributedDataStore(final String typeName, final boolean waitUntilLeader,
67             final String... shardNames) {
68         return setupDistributedDataStore(typeName, "module-shards.conf", waitUntilLeader,
69                 SchemaContextHelper.full(), shardNames);
70     }
71
72     public AbstractDataStore setupDistributedDataStore(final String typeName, final String moduleShardsConfig,
73             final boolean waitUntilLeader, final String... shardNames) {
74         return setupDistributedDataStore(typeName, moduleShardsConfig, waitUntilLeader,
75                 SchemaContextHelper.full(), shardNames);
76     }
77
78     public AbstractDataStore setupDistributedDataStore(final String typeName, final String moduleShardsConfig,
79             final boolean waitUntilLeader, final SchemaContext schemaContext, final String... shardNames) {
80         final ClusterWrapper cluster = new ClusterWrapperImpl(getSystem());
81         final Configuration config = new ConfigurationImpl(moduleShardsConfig, "modules.conf");
82
83         datastoreContextBuilder.dataStoreName(typeName);
84
85         DatastoreContext datastoreContext = datastoreContextBuilder.build();
86         DatastoreContextFactory mockContextFactory = Mockito.mock(DatastoreContextFactory.class);
87         Mockito.doReturn(datastoreContext).when(mockContextFactory).getBaseDatastoreContext();
88         Mockito.doReturn(datastoreContext).when(mockContextFactory).getShardDatastoreContext(Mockito.anyString());
89
90         AbstractDataStore dataStore = new DistributedDataStore(getSystem(), cluster, config, mockContextFactory,
91                 restoreFromSnapshot);
92
93         dataStore.onGlobalContextUpdated(schemaContext);
94
95         if (waitUntilLeader) {
96             waitUntilLeader(dataStore.getActorContext(), shardNames);
97         }
98
99         datastoreContextBuilder = DatastoreContext.newBuilderFrom(datastoreContext);
100         return dataStore;
101     }
102
103     public DistributedDataStore setupDistributedDataStoreWithoutConfig(final String typeName,
104                                                                        final SchemaContext schemaContext) {
105         final ClusterWrapper cluster = new ClusterWrapperImpl(getSystem());
106         final ConfigurationImpl configuration = new ConfigurationImpl(new EmptyModuleShardConfigProvider());
107
108         getDatastoreContextBuilder().dataStoreName(typeName);
109
110         final DatastoreContext datastoreContext = getDatastoreContextBuilder().build();
111
112         final DatastoreContextFactory mockContextFactory = Mockito.mock(DatastoreContextFactory.class);
113         Mockito.doReturn(datastoreContext).when(mockContextFactory).getBaseDatastoreContext();
114         Mockito.doReturn(datastoreContext).when(mockContextFactory).getShardDatastoreContext(Mockito.anyString());
115
116         final DistributedDataStore dataStore = new DistributedDataStore(getSystem(), cluster,
117                 configuration, mockContextFactory, restoreFromSnapshot);
118
119         dataStore.onGlobalContextUpdated(schemaContext);
120
121         datastoreContextBuilder = DatastoreContext.newBuilderFrom(datastoreContext);
122         return dataStore;
123     }
124
125     public void waitUntilLeader(final ActorContext actorContext, final String... shardNames) {
126         for (String shardName: shardNames) {
127             ActorRef shard = findLocalShard(actorContext, shardName);
128
129             assertNotNull("Shard was not created for " + shardName, shard);
130
131             waitUntilLeader(shard);
132         }
133     }
134
135     public void waitUntilNoLeader(final ActorContext actorContext, final String... shardNames) {
136         for (String shardName: shardNames) {
137             ActorRef shard = findLocalShard(actorContext, shardName);
138             assertNotNull("No local shard found for " + shardName, shard);
139
140             waitUntilNoLeader(shard);
141         }
142     }
143
144     public void waitForMembersUp(final String... otherMembers) {
145         Set<String> otherMembersSet = Sets.newHashSet(otherMembers);
146         Stopwatch sw = Stopwatch.createStarted();
147         while (sw.elapsed(TimeUnit.SECONDS) <= 10) {
148             CurrentClusterState state = Cluster.get(getSystem()).state();
149             for (Member m: state.getMembers()) {
150                 if (m.status() == MemberStatus.up() && otherMembersSet.remove(m.getRoles().iterator().next())
151                         && otherMembersSet.isEmpty()) {
152                     return;
153                 }
154             }
155
156             Uninterruptibles.sleepUninterruptibly(100, TimeUnit.MILLISECONDS);
157         }
158
159         fail("Member(s) " + otherMembersSet + " are not Up");
160     }
161
162     public static ActorRef findLocalShard(final ActorContext actorContext, final String shardName) {
163         ActorRef shard = null;
164         for (int i = 0; i < 20 * 5 && shard == null; i++) {
165             Uninterruptibles.sleepUninterruptibly(50, TimeUnit.MILLISECONDS);
166             Optional<ActorRef> shardReply = actorContext.findLocalShard(shardName);
167             if (shardReply.isPresent()) {
168                 shard = shardReply.get();
169             }
170         }
171         return shard;
172     }
173
174     public static void verifyShardStats(final AbstractDataStore datastore, final String shardName,
175             final ShardStatsVerifier verifier) throws Exception {
176         ActorContext actorContext = datastore.getActorContext();
177
178         Future<ActorRef> future = actorContext.findLocalShardAsync(shardName);
179         ActorRef shardActor = Await.result(future, Duration.create(10, TimeUnit.SECONDS));
180
181         AssertionError lastError = null;
182         Stopwatch sw = Stopwatch.createStarted();
183         while (sw.elapsed(TimeUnit.SECONDS) <= 5) {
184             ShardStats shardStats = (ShardStats)actorContext
185                     .executeOperation(shardActor, Shard.GET_SHARD_MBEAN_MESSAGE);
186
187             try {
188                 verifier.verify(shardStats);
189                 return;
190             } catch (AssertionError e) {
191                 lastError = e;
192                 Uninterruptibles.sleepUninterruptibly(50, TimeUnit.MILLISECONDS);
193             }
194         }
195
196         throw lastError;
197     }
198
199     void testWriteTransaction(final AbstractDataStore dataStore, final YangInstanceIdentifier nodePath,
200             final NormalizedNode<?, ?> nodeToWrite) throws Exception {
201
202         // 1. Create a write-only Tx
203
204         DOMStoreWriteTransaction writeTx = dataStore.newWriteOnlyTransaction();
205         assertNotNull("newWriteOnlyTransaction returned null", writeTx);
206
207         // 2. Write some data
208
209         writeTx.write(nodePath, nodeToWrite);
210
211         // 3. Ready the Tx for commit
212
213         DOMStoreThreePhaseCommitCohort cohort = writeTx.ready();
214
215         // 4. Commit the Tx
216
217         doCommit(cohort);
218
219         // 5. Verify the data in the store
220
221         DOMStoreReadTransaction readTx = dataStore.newReadOnlyTransaction();
222
223         Optional<NormalizedNode<?, ?>> optional = readTx.read(nodePath).get(5, TimeUnit.SECONDS);
224         assertEquals("isPresent", true, optional.isPresent());
225         assertEquals("Data node", nodeToWrite, optional.get());
226     }
227
228     public void doCommit(final DOMStoreThreePhaseCommitCohort cohort) throws Exception {
229         Boolean canCommit = cohort.canCommit().get(7, TimeUnit.SECONDS);
230         assertEquals("canCommit", true, canCommit);
231         cohort.preCommit().get(5, TimeUnit.SECONDS);
232         cohort.commit().get(5, TimeUnit.SECONDS);
233     }
234
235     void doCommit(final ListenableFuture<Boolean> canCommitFuture, final DOMStoreThreePhaseCommitCohort cohort)
236             throws Exception {
237         Boolean canCommit = canCommitFuture.get(7, TimeUnit.SECONDS);
238         assertEquals("canCommit", true, canCommit);
239         cohort.preCommit().get(5, TimeUnit.SECONDS);
240         cohort.commit().get(5, TimeUnit.SECONDS);
241     }
242
243     @SuppressWarnings("checkstyle:IllegalCatch")
244     void assertExceptionOnCall(final Callable<Void> callable, final Class<? extends Exception> expType)
245             throws Exception {
246         try {
247             callable.call();
248             fail("Expected " + expType.getSimpleName());
249         } catch (Exception e) {
250             assertEquals("Exception type", expType, e.getClass());
251         }
252     }
253
254     void assertExceptionOnTxChainCreates(final DOMStoreTransactionChain txChain,
255             final Class<? extends Exception> expType) throws Exception {
256         assertExceptionOnCall(() -> {
257             txChain.newWriteOnlyTransaction();
258             return null;
259         }, expType);
260
261         assertExceptionOnCall(() -> {
262             txChain.newReadWriteTransaction();
263             return null;
264         }, expType);
265
266         assertExceptionOnCall(() -> {
267             txChain.newReadOnlyTransaction();
268             return null;
269         }, expType);
270     }
271
272     public interface ShardStatsVerifier {
273         void verify(ShardStats stats);
274     }
275 }