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;
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.messages.DatastoreSnapshot;
26 import org.opendaylight.controller.cluster.datastore.utils.ActorContext;
27 import org.opendaylight.controller.md.cluster.datastore.model.SchemaContextHelper;
28 import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadTransaction;
29 import org.opendaylight.controller.sal.core.spi.data.DOMStoreThreePhaseCommitCohort;
30 import org.opendaylight.controller.sal.core.spi.data.DOMStoreTransactionChain;
31 import org.opendaylight.controller.sal.core.spi.data.DOMStoreWriteTransaction;
32 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
33 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
34 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
36 public class IntegrationTestKit extends ShardTestKit {
38 DatastoreContext.Builder datastoreContextBuilder;
39 DatastoreSnapshot restoreFromSnapshot;
41 public IntegrationTestKit(ActorSystem actorSystem, Builder datastoreContextBuilder) {
43 this.datastoreContextBuilder = datastoreContextBuilder;
46 public DistributedDataStore setupDistributedDataStore(String typeName, String... shardNames) {
47 return setupDistributedDataStore(typeName, "module-shards.conf", true, SchemaContextHelper.full(), shardNames);
50 public DistributedDataStore setupDistributedDataStore(String typeName, boolean waitUntilLeader,
51 String... shardNames) {
52 return setupDistributedDataStore(typeName, "module-shards.conf", waitUntilLeader,
53 SchemaContextHelper.full(), shardNames);
56 public DistributedDataStore setupDistributedDataStore(String typeName, String moduleShardsConfig,
57 boolean waitUntilLeader, String... shardNames) {
58 return setupDistributedDataStore(typeName, moduleShardsConfig, waitUntilLeader,
59 SchemaContextHelper.full(), shardNames);
62 public DistributedDataStore setupDistributedDataStore(String typeName, String moduleShardsConfig,
63 boolean waitUntilLeader, SchemaContext schemaContext, String... shardNames) {
64 ClusterWrapper cluster = new ClusterWrapperImpl(getSystem());
65 Configuration config = new ConfigurationImpl(moduleShardsConfig, "modules.conf");
67 datastoreContextBuilder.dataStoreType(typeName);
69 DatastoreContext datastoreContext = datastoreContextBuilder.build();
70 DatastoreContextFactory mockContextFactory = Mockito.mock(DatastoreContextFactory.class);
71 Mockito.doReturn(datastoreContext).when(mockContextFactory).getBaseDatastoreContext();
72 Mockito.doReturn(datastoreContext).when(mockContextFactory).getShardDatastoreContext(Mockito.anyString());
74 DistributedDataStore dataStore = new DistributedDataStore(getSystem(), cluster, config, mockContextFactory,
77 dataStore.onGlobalContextUpdated(schemaContext);
80 waitUntilLeader(dataStore.getActorContext(), shardNames);
86 public void waitUntilLeader(ActorContext actorContext, String... shardNames) {
87 for(String shardName: shardNames) {
88 ActorRef shard = findLocalShard(actorContext, shardName);
90 assertNotNull("Shard was not created", shard);
92 waitUntilLeader(shard);
96 public void waitUntilNoLeader(ActorContext actorContext, String... shardNames) {
97 for(String shardName: shardNames) {
98 ActorRef shard = findLocalShard(actorContext, shardName);
99 assertNotNull("No local shard found", shard);
101 waitUntilNoLeader(shard);
105 private static ActorRef findLocalShard(ActorContext actorContext, String shardName) {
106 ActorRef shard = null;
107 for(int i = 0; i < 20 * 5 && shard == null; i++) {
108 Uninterruptibles.sleepUninterruptibly(50, TimeUnit.MILLISECONDS);
109 Optional<ActorRef> shardReply = actorContext.findLocalShard(shardName);
110 if(shardReply.isPresent()) {
111 shard = shardReply.get();
117 void testWriteTransaction(DistributedDataStore dataStore, YangInstanceIdentifier nodePath,
118 NormalizedNode<?, ?> nodeToWrite) throws Exception {
120 // 1. Create a write-only Tx
122 DOMStoreWriteTransaction writeTx = dataStore.newWriteOnlyTransaction();
123 assertNotNull("newWriteOnlyTransaction returned null", writeTx);
125 // 2. Write some data
127 writeTx.write(nodePath, nodeToWrite);
129 // 3. Ready the Tx for commit
131 DOMStoreThreePhaseCommitCohort cohort = writeTx.ready();
137 // 5. Verify the data in the store
139 DOMStoreReadTransaction readTx = dataStore.newReadOnlyTransaction();
141 Optional<NormalizedNode<?, ?>> optional = readTx.read(nodePath).get(5, TimeUnit.SECONDS);
142 assertEquals("isPresent", true, optional.isPresent());
143 assertEquals("Data node", nodeToWrite, optional.get());
146 void doCommit(final DOMStoreThreePhaseCommitCohort cohort) throws Exception {
147 Boolean canCommit = cohort.canCommit().get(7, TimeUnit.SECONDS);
148 assertEquals("canCommit", true, canCommit);
149 cohort.preCommit().get(5, TimeUnit.SECONDS);
150 cohort.commit().get(5, TimeUnit.SECONDS);
153 void doCommit(final ListenableFuture<Boolean> canCommitFuture, final DOMStoreThreePhaseCommitCohort cohort) throws Exception {
154 Boolean canCommit = canCommitFuture.get(7, TimeUnit.SECONDS);
155 assertEquals("canCommit", true, canCommit);
156 cohort.preCommit().get(5, TimeUnit.SECONDS);
157 cohort.commit().get(5, TimeUnit.SECONDS);
160 public void cleanup(DistributedDataStore dataStore) {
161 if(dataStore != null) {
162 dataStore.getActorContext().getShardManager().tell(PoisonPill.getInstance(), null);
166 void assertExceptionOnCall(Callable<Void> callable, Class<? extends Exception> expType)
170 fail("Expected " + expType.getSimpleName());
171 } catch(Exception e) {
172 assertEquals("Exception type", expType, e.getClass());
176 void assertExceptionOnTxChainCreates(final DOMStoreTransactionChain txChain,
177 Class<? extends Exception> expType) throws Exception {
178 assertExceptionOnCall(new Callable<Void>() {
180 public Void call() throws Exception {
181 txChain.newWriteOnlyTransaction();
186 assertExceptionOnCall(new Callable<Void>() {
188 public Void call() throws Exception {
189 txChain.newReadWriteTransaction();
194 assertExceptionOnCall(new Callable<Void>() {
196 public Void call() throws Exception {
197 txChain.newReadOnlyTransaction();