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.Uninterruptibles;
18 import java.util.concurrent.Callable;
19 import java.util.concurrent.TimeUnit;
20 import org.opendaylight.controller.cluster.datastore.DatastoreContext.Builder;
21 import org.opendaylight.controller.cluster.datastore.shardstrategy.ShardStrategyFactory;
22 import org.opendaylight.controller.cluster.datastore.utils.ActorContext;
23 import org.opendaylight.controller.md.cluster.datastore.model.SchemaContextHelper;
24 import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadTransaction;
25 import org.opendaylight.controller.sal.core.spi.data.DOMStoreThreePhaseCommitCohort;
26 import org.opendaylight.controller.sal.core.spi.data.DOMStoreTransactionChain;
27 import org.opendaylight.controller.sal.core.spi.data.DOMStoreWriteTransaction;
28 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
29 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
30 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
32 class IntegrationTestKit extends ShardTestKit {
34 DatastoreContext.Builder datastoreContextBuilder;
36 IntegrationTestKit(ActorSystem actorSystem, Builder datastoreContextBuilder) {
38 this.datastoreContextBuilder = datastoreContextBuilder;
41 DistributedDataStore setupDistributedDataStore(String typeName, String... shardNames) {
42 return setupDistributedDataStore(typeName, true, shardNames);
45 DistributedDataStore setupDistributedDataStore(String typeName, boolean waitUntilLeader,
46 String... shardNames) {
47 return setupDistributedDataStore(typeName, "module-shards.conf", waitUntilLeader, shardNames);
50 DistributedDataStore setupDistributedDataStore(String typeName, String moduleShardsConfig, boolean waitUntilLeader,
51 String... shardNames) {
52 ClusterWrapper cluster = new ClusterWrapperImpl(getSystem());
53 Configuration config = new ConfigurationImpl(moduleShardsConfig, "modules.conf");
54 ShardStrategyFactory.setConfiguration(config);
56 datastoreContextBuilder.dataStoreType(typeName);
58 DatastoreContext datastoreContext = datastoreContextBuilder.build();
60 DistributedDataStore dataStore = new DistributedDataStore(getSystem(), cluster, config, datastoreContext);
62 SchemaContext schemaContext = SchemaContextHelper.full();
63 dataStore.onGlobalContextUpdated(schemaContext);
66 waitUntilLeader(dataStore.getActorContext(), shardNames);
72 void waitUntilLeader(ActorContext actorContext, String... shardNames) {
73 for(String shardName: shardNames) {
74 ActorRef shard = null;
75 for(int i = 0; i < 20 * 5 && shard == null; i++) {
76 Uninterruptibles.sleepUninterruptibly(50, TimeUnit.MILLISECONDS);
77 Optional<ActorRef> shardReply = actorContext.findLocalShard(shardName);
78 if(shardReply.isPresent()) {
79 shard = shardReply.get();
83 assertNotNull("Shard was not created", shard);
85 waitUntilLeader(shard);
89 void testWriteTransaction(DistributedDataStore dataStore, YangInstanceIdentifier nodePath,
90 NormalizedNode<?, ?> nodeToWrite) throws Exception {
92 // 1. Create a write-only Tx
94 DOMStoreWriteTransaction writeTx = dataStore.newWriteOnlyTransaction();
95 assertNotNull("newWriteOnlyTransaction returned null", writeTx);
99 writeTx.write(nodePath, nodeToWrite);
101 // 3. Ready the Tx for commit
103 DOMStoreThreePhaseCommitCohort cohort = writeTx.ready();
109 // 5. Verify the data in the store
111 DOMStoreReadTransaction readTx = dataStore.newReadOnlyTransaction();
113 Optional<NormalizedNode<?, ?>> optional = readTx.read(nodePath).get(5, TimeUnit.SECONDS);
114 assertEquals("isPresent", true, optional.isPresent());
115 assertEquals("Data node", nodeToWrite, optional.get());
118 void doCommit(final DOMStoreThreePhaseCommitCohort cohort) throws Exception {
119 Boolean canCommit = cohort.canCommit().get(7, TimeUnit.SECONDS);
120 assertEquals("canCommit", true, canCommit);
121 cohort.preCommit().get(5, TimeUnit.SECONDS);
122 cohort.commit().get(5, TimeUnit.SECONDS);
125 void cleanup(DistributedDataStore dataStore) {
126 if(dataStore != null) {
127 dataStore.getActorContext().getShardManager().tell(PoisonPill.getInstance(), null);
131 void assertExceptionOnCall(Callable<Void> callable, Class<? extends Exception> expType)
135 fail("Expected " + expType.getSimpleName());
136 } catch(Exception e) {
137 assertEquals("Exception type", expType, e.getClass());
141 void assertExceptionOnTxChainCreates(final DOMStoreTransactionChain txChain,
142 Class<? extends Exception> expType) throws Exception {
143 assertExceptionOnCall(new Callable<Void>() {
145 public Void call() throws Exception {
146 txChain.newWriteOnlyTransaction();
151 assertExceptionOnCall(new Callable<Void>() {
153 public Void call() throws Exception {
154 txChain.newReadWriteTransaction();
159 assertExceptionOnCall(new Callable<Void>() {
161 public Void call() throws Exception {
162 txChain.newReadOnlyTransaction();