2 * Copyright (c) 2015 Brocade Communications Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.cluster.datastore;
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12 import static org.junit.Assert.fail;
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;
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;
48 public class IntegrationTestKit extends ShardTestKit {
50 protected DatastoreContext.Builder datastoreContextBuilder;
51 protected DatastoreSnapshot restoreFromSnapshot;
53 public IntegrationTestKit(final ActorSystem actorSystem, final Builder datastoreContextBuilder) {
55 this.datastoreContextBuilder = datastoreContextBuilder;
58 public DatastoreContext.Builder getDatastoreContextBuilder() {
59 return datastoreContextBuilder;
62 public AbstractDataStore setupDistributedDataStore(final String typeName, final String... shardNames) {
63 return setupDistributedDataStore(typeName, "module-shards.conf", true, SchemaContextHelper.full(), shardNames);
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);
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);
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");
83 datastoreContextBuilder.dataStoreName(typeName);
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());
90 AbstractDataStore dataStore = new DistributedDataStore(getSystem(), cluster, config, mockContextFactory,
93 dataStore.onGlobalContextUpdated(schemaContext);
95 if (waitUntilLeader) {
96 waitUntilLeader(dataStore.getActorContext(), shardNames);
99 datastoreContextBuilder = DatastoreContext.newBuilderFrom(datastoreContext);
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());
108 getDatastoreContextBuilder().dataStoreName(typeName);
110 final DatastoreContext datastoreContext = getDatastoreContextBuilder().build();
112 final DatastoreContextFactory mockContextFactory = Mockito.mock(DatastoreContextFactory.class);
113 Mockito.doReturn(datastoreContext).when(mockContextFactory).getBaseDatastoreContext();
114 Mockito.doReturn(datastoreContext).when(mockContextFactory).getShardDatastoreContext(Mockito.anyString());
116 final DistributedDataStore dataStore = new DistributedDataStore(getSystem(), cluster,
117 configuration, mockContextFactory, restoreFromSnapshot);
119 dataStore.onGlobalContextUpdated(schemaContext);
121 datastoreContextBuilder = DatastoreContext.newBuilderFrom(datastoreContext);
125 public void waitUntilLeader(final ActorContext actorContext, final String... shardNames) {
126 for (String shardName: shardNames) {
127 ActorRef shard = findLocalShard(actorContext, shardName);
129 assertNotNull("Shard was not created for " + shardName, shard);
131 waitUntilLeader(shard);
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);
140 waitUntilNoLeader(shard);
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()) {
156 Uninterruptibles.sleepUninterruptibly(100, TimeUnit.MILLISECONDS);
159 fail("Member(s) " + otherMembersSet + " are not Up");
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();
174 public static void verifyShardStats(final AbstractDataStore datastore, final String shardName,
175 final ShardStatsVerifier verifier) throws Exception {
176 ActorContext actorContext = datastore.getActorContext();
178 Future<ActorRef> future = actorContext.findLocalShardAsync(shardName);
179 ActorRef shardActor = Await.result(future, Duration.create(10, TimeUnit.SECONDS));
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);
188 verifier.verify(shardStats);
190 } catch (AssertionError e) {
192 Uninterruptibles.sleepUninterruptibly(50, TimeUnit.MILLISECONDS);
199 void testWriteTransaction(final AbstractDataStore dataStore, final YangInstanceIdentifier nodePath,
200 final NormalizedNode<?, ?> nodeToWrite) throws Exception {
202 // 1. Create a write-only Tx
204 DOMStoreWriteTransaction writeTx = dataStore.newWriteOnlyTransaction();
205 assertNotNull("newWriteOnlyTransaction returned null", writeTx);
207 // 2. Write some data
209 writeTx.write(nodePath, nodeToWrite);
211 // 3. Ready the Tx for commit
213 DOMStoreThreePhaseCommitCohort cohort = writeTx.ready();
219 // 5. Verify the data in the store
221 DOMStoreReadTransaction readTx = dataStore.newReadOnlyTransaction();
223 Optional<NormalizedNode<?, ?>> optional = readTx.read(nodePath).get(5, TimeUnit.SECONDS);
224 assertEquals("isPresent", true, optional.isPresent());
225 assertEquals("Data node", nodeToWrite, optional.get());
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);
235 void doCommit(final ListenableFuture<Boolean> canCommitFuture, final DOMStoreThreePhaseCommitCohort cohort)
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);
243 @SuppressWarnings("checkstyle:IllegalCatch")
244 void assertExceptionOnCall(final Callable<Void> callable, final Class<? extends Exception> expType)
248 fail("Expected " + expType.getSimpleName());
249 } catch (Exception e) {
250 assertEquals("Exception type", expType, e.getClass());
254 void assertExceptionOnTxChainCreates(final DOMStoreTransactionChain txChain,
255 final Class<? extends Exception> expType) throws Exception {
256 assertExceptionOnCall(() -> {
257 txChain.newWriteOnlyTransaction();
261 assertExceptionOnCall(() -> {
262 txChain.newReadWriteTransaction();
266 assertExceptionOnCall(() -> {
267 txChain.newReadOnlyTransaction();
272 public interface ShardStatsVerifier {
273 void verify(ShardStats stats);