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.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 public class IntegrationTestKit extends ShardTestKit {
37 DatastoreContext.Builder datastoreContextBuilder;
39 public IntegrationTestKit(ActorSystem actorSystem, Builder datastoreContextBuilder) {
41 this.datastoreContextBuilder = datastoreContextBuilder;
44 public DistributedDataStore setupDistributedDataStore(String typeName, String... shardNames) {
45 return setupDistributedDataStore(typeName, "module-shards.conf", true, SchemaContextHelper.full(), shardNames);
48 public DistributedDataStore setupDistributedDataStore(String typeName, boolean waitUntilLeader,
49 String... shardNames) {
50 return setupDistributedDataStore(typeName, "module-shards.conf", waitUntilLeader,
51 SchemaContextHelper.full(), shardNames);
54 public DistributedDataStore setupDistributedDataStore(String typeName, String moduleShardsConfig,
55 boolean waitUntilLeader, String... shardNames) {
56 return setupDistributedDataStore(typeName, moduleShardsConfig, waitUntilLeader,
57 SchemaContextHelper.full(), shardNames);
60 public DistributedDataStore setupDistributedDataStore(String typeName, String moduleShardsConfig,
61 boolean waitUntilLeader, SchemaContext schemaContext, String... shardNames) {
62 ClusterWrapper cluster = new ClusterWrapperImpl(getSystem());
63 Configuration config = new ConfigurationImpl(moduleShardsConfig, "modules.conf");
65 datastoreContextBuilder.dataStoreType(typeName);
67 DatastoreContext datastoreContext = datastoreContextBuilder.build();
68 DatastoreContextFactory mockContextFactory = Mockito.mock(DatastoreContextFactory.class);
69 Mockito.doReturn(datastoreContext).when(mockContextFactory).getBaseDatastoreContext();
70 Mockito.doReturn(datastoreContext).when(mockContextFactory).getShardDatastoreContext(Mockito.anyString());
72 DistributedDataStore dataStore = new DistributedDataStore(getSystem(), cluster, config, mockContextFactory);
74 dataStore.onGlobalContextUpdated(schemaContext);
77 waitUntilLeader(dataStore.getActorContext(), shardNames);
83 public void waitUntilLeader(ActorContext actorContext, String... shardNames) {
84 for(String shardName: shardNames) {
85 ActorRef shard = findLocalShard(actorContext, shardName);
87 assertNotNull("Shard was not created", shard);
89 waitUntilLeader(shard);
93 public void waitUntilNoLeader(ActorContext actorContext, String... shardNames) {
94 for(String shardName: shardNames) {
95 ActorRef shard = findLocalShard(actorContext, shardName);
96 assertNotNull("No local shard found", shard);
98 waitUntilNoLeader(shard);
102 private static ActorRef findLocalShard(ActorContext actorContext, String shardName) {
103 ActorRef shard = null;
104 for(int i = 0; i < 20 * 5 && shard == null; i++) {
105 Uninterruptibles.sleepUninterruptibly(50, TimeUnit.MILLISECONDS);
106 Optional<ActorRef> shardReply = actorContext.findLocalShard(shardName);
107 if(shardReply.isPresent()) {
108 shard = shardReply.get();
114 void testWriteTransaction(DistributedDataStore dataStore, YangInstanceIdentifier nodePath,
115 NormalizedNode<?, ?> nodeToWrite) throws Exception {
117 // 1. Create a write-only Tx
119 DOMStoreWriteTransaction writeTx = dataStore.newWriteOnlyTransaction();
120 assertNotNull("newWriteOnlyTransaction returned null", writeTx);
122 // 2. Write some data
124 writeTx.write(nodePath, nodeToWrite);
126 // 3. Ready the Tx for commit
128 DOMStoreThreePhaseCommitCohort cohort = writeTx.ready();
134 // 5. Verify the data in the store
136 DOMStoreReadTransaction readTx = dataStore.newReadOnlyTransaction();
138 Optional<NormalizedNode<?, ?>> optional = readTx.read(nodePath).get(5, TimeUnit.SECONDS);
139 assertEquals("isPresent", true, optional.isPresent());
140 assertEquals("Data node", nodeToWrite, optional.get());
143 void doCommit(final DOMStoreThreePhaseCommitCohort cohort) throws Exception {
144 Boolean canCommit = cohort.canCommit().get(7, TimeUnit.SECONDS);
145 assertEquals("canCommit", true, canCommit);
146 cohort.preCommit().get(5, TimeUnit.SECONDS);
147 cohort.commit().get(5, TimeUnit.SECONDS);
150 void doCommit(final ListenableFuture<Boolean> canCommitFuture, final DOMStoreThreePhaseCommitCohort cohort) throws Exception {
151 Boolean canCommit = canCommitFuture.get(7, TimeUnit.SECONDS);
152 assertEquals("canCommit", true, canCommit);
153 cohort.preCommit().get(5, TimeUnit.SECONDS);
154 cohort.commit().get(5, TimeUnit.SECONDS);
157 public void cleanup(DistributedDataStore dataStore) {
158 if(dataStore != null) {
159 dataStore.getActorContext().getShardManager().tell(PoisonPill.getInstance(), null);
163 void assertExceptionOnCall(Callable<Void> callable, Class<? extends Exception> expType)
167 fail("Expected " + expType.getSimpleName());
168 } catch(Exception e) {
169 assertEquals("Exception type", expType, e.getClass());
173 void assertExceptionOnTxChainCreates(final DOMStoreTransactionChain txChain,
174 Class<? extends Exception> expType) throws Exception {
175 assertExceptionOnCall(new Callable<Void>() {
177 public Void call() throws Exception {
178 txChain.newWriteOnlyTransaction();
183 assertExceptionOnCall(new Callable<Void>() {
185 public Void call() throws Exception {
186 txChain.newReadWriteTransaction();
191 assertExceptionOnCall(new Callable<Void>() {
193 public Void call() throws Exception {
194 txChain.newReadOnlyTransaction();