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.shardstrategy.ShardStrategyFactory;
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;
35 class IntegrationTestKit extends ShardTestKit {
37 DatastoreContext.Builder datastoreContextBuilder;
39 IntegrationTestKit(ActorSystem actorSystem, Builder datastoreContextBuilder) {
41 this.datastoreContextBuilder = datastoreContextBuilder;
44 DistributedDataStore setupDistributedDataStore(String typeName, String... shardNames) {
45 return setupDistributedDataStore(typeName, true, shardNames);
48 DistributedDataStore setupDistributedDataStore(String typeName, boolean waitUntilLeader,
49 String... shardNames) {
50 return setupDistributedDataStore(typeName, "module-shards.conf", waitUntilLeader, shardNames);
53 DistributedDataStore setupDistributedDataStore(String typeName, String moduleShardsConfig, boolean waitUntilLeader,
54 String... shardNames) {
55 ClusterWrapper cluster = new ClusterWrapperImpl(getSystem());
56 Configuration config = new ConfigurationImpl(moduleShardsConfig, "modules.conf");
57 ShardStrategyFactory.setConfiguration(config);
59 datastoreContextBuilder.dataStoreType(typeName);
61 DatastoreContext datastoreContext = datastoreContextBuilder.build();
63 DistributedDataStore dataStore = new DistributedDataStore(getSystem(), cluster, config, datastoreContext);
65 SchemaContext schemaContext = SchemaContextHelper.full();
66 dataStore.onGlobalContextUpdated(schemaContext);
69 waitUntilLeader(dataStore.getActorContext(), shardNames);
75 void waitUntilLeader(ActorContext actorContext, String... shardNames) {
76 for(String shardName: shardNames) {
77 ActorRef shard = findLocalShard(actorContext, shardName);
79 assertNotNull("Shard was not created", shard);
81 waitUntilLeader(shard);
85 void waitUntilNoLeader(ActorContext actorContext, String... shardNames) {
86 for(String shardName: shardNames) {
87 ActorRef shard = findLocalShard(actorContext, shardName);
88 assertNotNull("No local shard found", shard);
90 waitUntilNoLeader(shard);
94 private ActorRef findLocalShard(ActorContext actorContext, String shardName) {
95 ActorRef shard = null;
96 for(int i = 0; i < 20 * 5 && shard == null; i++) {
97 Uninterruptibles.sleepUninterruptibly(50, TimeUnit.MILLISECONDS);
98 Optional<ActorRef> shardReply = actorContext.findLocalShard(shardName);
99 if(shardReply.isPresent()) {
100 shard = shardReply.get();
106 void testWriteTransaction(DistributedDataStore dataStore, YangInstanceIdentifier nodePath,
107 NormalizedNode<?, ?> nodeToWrite) throws Exception {
109 // 1. Create a write-only Tx
111 DOMStoreWriteTransaction writeTx = dataStore.newWriteOnlyTransaction();
112 assertNotNull("newWriteOnlyTransaction returned null", writeTx);
114 // 2. Write some data
116 writeTx.write(nodePath, nodeToWrite);
118 // 3. Ready the Tx for commit
120 DOMStoreThreePhaseCommitCohort cohort = writeTx.ready();
126 // 5. Verify the data in the store
128 DOMStoreReadTransaction readTx = dataStore.newReadOnlyTransaction();
130 Optional<NormalizedNode<?, ?>> optional = readTx.read(nodePath).get(5, TimeUnit.SECONDS);
131 assertEquals("isPresent", true, optional.isPresent());
132 assertEquals("Data node", nodeToWrite, optional.get());
135 void doCommit(final DOMStoreThreePhaseCommitCohort cohort) throws Exception {
136 Boolean canCommit = cohort.canCommit().get(7, TimeUnit.SECONDS);
137 assertEquals("canCommit", true, canCommit);
138 cohort.preCommit().get(5, TimeUnit.SECONDS);
139 cohort.commit().get(5, TimeUnit.SECONDS);
142 void doCommit(final ListenableFuture<Boolean> canCommitFuture, final DOMStoreThreePhaseCommitCohort cohort) throws Exception {
143 Boolean canCommit = canCommitFuture.get(7, TimeUnit.SECONDS);
144 assertEquals("canCommit", true, canCommit);
145 cohort.preCommit().get(5, TimeUnit.SECONDS);
146 cohort.commit().get(5, TimeUnit.SECONDS);
149 void cleanup(DistributedDataStore dataStore) {
150 if(dataStore != null) {
151 dataStore.getActorContext().getShardManager().tell(PoisonPill.getInstance(), null);
155 void assertExceptionOnCall(Callable<Void> callable, Class<? extends Exception> expType)
159 fail("Expected " + expType.getSimpleName());
160 } catch(Exception e) {
161 assertEquals("Exception type", expType, e.getClass());
165 void assertExceptionOnTxChainCreates(final DOMStoreTransactionChain txChain,
166 Class<? extends Exception> expType) throws Exception {
167 assertExceptionOnCall(new Callable<Void>() {
169 public Void call() throws Exception {
170 txChain.newWriteOnlyTransaction();
175 assertExceptionOnCall(new Callable<Void>() {
177 public Void call() throws Exception {
178 txChain.newReadWriteTransaction();
183 assertExceptionOnCall(new Callable<Void>() {
185 public Void call() throws Exception {
186 txChain.newReadOnlyTransaction();