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.opendaylight.controller.cluster.datastore.DatastoreContext.Builder;
22 import org.opendaylight.controller.cluster.datastore.config.Configuration;
23 import org.opendaylight.controller.cluster.datastore.config.ConfigurationImpl;
24 import org.opendaylight.controller.cluster.datastore.utils.ActorContext;
25 import org.opendaylight.controller.md.cluster.datastore.model.SchemaContextHelper;
26 import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadTransaction;
27 import org.opendaylight.controller.sal.core.spi.data.DOMStoreThreePhaseCommitCohort;
28 import org.opendaylight.controller.sal.core.spi.data.DOMStoreTransactionChain;
29 import org.opendaylight.controller.sal.core.spi.data.DOMStoreWriteTransaction;
30 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
31 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
32 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
34 class IntegrationTestKit extends ShardTestKit {
36 DatastoreContext.Builder datastoreContextBuilder;
38 IntegrationTestKit(ActorSystem actorSystem, Builder datastoreContextBuilder) {
40 this.datastoreContextBuilder = datastoreContextBuilder;
43 DistributedDataStore setupDistributedDataStore(String typeName, String... shardNames) {
44 return setupDistributedDataStore(typeName, true, shardNames);
47 DistributedDataStore setupDistributedDataStore(String typeName, boolean waitUntilLeader,
48 String... shardNames) {
49 return setupDistributedDataStore(typeName, "module-shards.conf", waitUntilLeader, shardNames);
52 DistributedDataStore setupDistributedDataStore(String typeName, String moduleShardsConfig, boolean waitUntilLeader,
53 String... shardNames) {
54 ClusterWrapper cluster = new ClusterWrapperImpl(getSystem());
55 Configuration config = new ConfigurationImpl(moduleShardsConfig, "modules.conf");
57 datastoreContextBuilder.dataStoreType(typeName);
59 DatastoreContext datastoreContext = datastoreContextBuilder.build();
61 DistributedDataStore dataStore = new DistributedDataStore(getSystem(), cluster, config, datastoreContext);
63 SchemaContext schemaContext = SchemaContextHelper.full();
64 dataStore.onGlobalContextUpdated(schemaContext);
67 waitUntilLeader(dataStore.getActorContext(), shardNames);
73 void waitUntilLeader(ActorContext actorContext, String... shardNames) {
74 for(String shardName: shardNames) {
75 ActorRef shard = findLocalShard(actorContext, shardName);
77 assertNotNull("Shard was not created", shard);
79 waitUntilLeader(shard);
83 void waitUntilNoLeader(ActorContext actorContext, String... shardNames) {
84 for(String shardName: shardNames) {
85 ActorRef shard = findLocalShard(actorContext, shardName);
86 assertNotNull("No local shard found", shard);
88 waitUntilNoLeader(shard);
92 private ActorRef findLocalShard(ActorContext actorContext, String shardName) {
93 ActorRef shard = null;
94 for(int i = 0; i < 20 * 5 && shard == null; i++) {
95 Uninterruptibles.sleepUninterruptibly(50, TimeUnit.MILLISECONDS);
96 Optional<ActorRef> shardReply = actorContext.findLocalShard(shardName);
97 if(shardReply.isPresent()) {
98 shard = shardReply.get();
104 void testWriteTransaction(DistributedDataStore dataStore, YangInstanceIdentifier nodePath,
105 NormalizedNode<?, ?> nodeToWrite) throws Exception {
107 // 1. Create a write-only Tx
109 DOMStoreWriteTransaction writeTx = dataStore.newWriteOnlyTransaction();
110 assertNotNull("newWriteOnlyTransaction returned null", writeTx);
112 // 2. Write some data
114 writeTx.write(nodePath, nodeToWrite);
116 // 3. Ready the Tx for commit
118 DOMStoreThreePhaseCommitCohort cohort = writeTx.ready();
124 // 5. Verify the data in the store
126 DOMStoreReadTransaction readTx = dataStore.newReadOnlyTransaction();
128 Optional<NormalizedNode<?, ?>> optional = readTx.read(nodePath).get(5, TimeUnit.SECONDS);
129 assertEquals("isPresent", true, optional.isPresent());
130 assertEquals("Data node", nodeToWrite, optional.get());
133 void doCommit(final DOMStoreThreePhaseCommitCohort cohort) throws Exception {
134 Boolean canCommit = cohort.canCommit().get(7, TimeUnit.SECONDS);
135 assertEquals("canCommit", true, canCommit);
136 cohort.preCommit().get(5, TimeUnit.SECONDS);
137 cohort.commit().get(5, TimeUnit.SECONDS);
140 void doCommit(final ListenableFuture<Boolean> canCommitFuture, final DOMStoreThreePhaseCommitCohort cohort) throws Exception {
141 Boolean canCommit = canCommitFuture.get(7, TimeUnit.SECONDS);
142 assertEquals("canCommit", true, canCommit);
143 cohort.preCommit().get(5, TimeUnit.SECONDS);
144 cohort.commit().get(5, TimeUnit.SECONDS);
147 void cleanup(DistributedDataStore dataStore) {
148 if(dataStore != null) {
149 dataStore.getActorContext().getShardManager().tell(PoisonPill.getInstance(), null);
153 void assertExceptionOnCall(Callable<Void> callable, Class<? extends Exception> expType)
157 fail("Expected " + expType.getSimpleName());
158 } catch(Exception e) {
159 assertEquals("Exception type", expType, e.getClass());
163 void assertExceptionOnTxChainCreates(final DOMStoreTransactionChain txChain,
164 Class<? extends Exception> expType) throws Exception {
165 assertExceptionOnCall(new Callable<Void>() {
167 public Void call() throws Exception {
168 txChain.newWriteOnlyTransaction();
173 assertExceptionOnCall(new Callable<Void>() {
175 public Void call() throws Exception {
176 txChain.newReadWriteTransaction();
181 assertExceptionOnCall(new Callable<Void>() {
183 public Void call() throws Exception {
184 txChain.newReadOnlyTransaction();