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 protected DatastoreContext.Builder datastoreContextBuilder;
39 protected DatastoreSnapshot restoreFromSnapshot;
41 public IntegrationTestKit(ActorSystem actorSystem, Builder datastoreContextBuilder) {
43 this.datastoreContextBuilder = datastoreContextBuilder;
46 public DatastoreContext.Builder getDatastoreContextBuilder() {
47 return datastoreContextBuilder;
50 public DistributedDataStore setupDistributedDataStore(String typeName, String... shardNames) {
51 return setupDistributedDataStore(typeName, "module-shards.conf", true, SchemaContextHelper.full(), shardNames);
54 public DistributedDataStore setupDistributedDataStore(String typeName, boolean waitUntilLeader,
55 String... shardNames) {
56 return setupDistributedDataStore(typeName, "module-shards.conf", waitUntilLeader,
57 SchemaContextHelper.full(), shardNames);
60 public DistributedDataStore setupDistributedDataStore(String typeName, String moduleShardsConfig,
61 boolean waitUntilLeader, String... shardNames) {
62 return setupDistributedDataStore(typeName, moduleShardsConfig, waitUntilLeader,
63 SchemaContextHelper.full(), shardNames);
66 public DistributedDataStore setupDistributedDataStore(String typeName, String moduleShardsConfig,
67 boolean waitUntilLeader, SchemaContext schemaContext, String... shardNames) {
68 ClusterWrapper cluster = new ClusterWrapperImpl(getSystem());
69 Configuration config = new ConfigurationImpl(moduleShardsConfig, "modules.conf");
71 datastoreContextBuilder.dataStoreName(typeName);
73 DatastoreContext datastoreContext = datastoreContextBuilder.build();
74 DatastoreContextFactory mockContextFactory = Mockito.mock(DatastoreContextFactory.class);
75 Mockito.doReturn(datastoreContext).when(mockContextFactory).getBaseDatastoreContext();
76 Mockito.doReturn(datastoreContext).when(mockContextFactory).getShardDatastoreContext(Mockito.anyString());
78 DistributedDataStore dataStore = new DistributedDataStore(getSystem(), cluster, config, mockContextFactory,
81 dataStore.onGlobalContextUpdated(schemaContext);
84 waitUntilLeader(dataStore.getActorContext(), shardNames);
87 datastoreContextBuilder = DatastoreContext.newBuilderFrom(datastoreContext);
91 public void waitUntilLeader(ActorContext actorContext, String... shardNames) {
92 for(String shardName: shardNames) {
93 ActorRef shard = findLocalShard(actorContext, shardName);
95 assertNotNull("Shard was not created", shard);
97 waitUntilLeader(shard);
101 public void waitUntilNoLeader(ActorContext actorContext, String... shardNames) {
102 for(String shardName: shardNames) {
103 ActorRef shard = findLocalShard(actorContext, shardName);
104 assertNotNull("No local shard found", shard);
106 waitUntilNoLeader(shard);
110 private static ActorRef findLocalShard(ActorContext actorContext, String shardName) {
111 ActorRef shard = null;
112 for(int i = 0; i < 20 * 5 && shard == null; i++) {
113 Uninterruptibles.sleepUninterruptibly(50, TimeUnit.MILLISECONDS);
114 Optional<ActorRef> shardReply = actorContext.findLocalShard(shardName);
115 if(shardReply.isPresent()) {
116 shard = shardReply.get();
122 void testWriteTransaction(DistributedDataStore dataStore, YangInstanceIdentifier nodePath,
123 NormalizedNode<?, ?> nodeToWrite) throws Exception {
125 // 1. Create a write-only Tx
127 DOMStoreWriteTransaction writeTx = dataStore.newWriteOnlyTransaction();
128 assertNotNull("newWriteOnlyTransaction returned null", writeTx);
130 // 2. Write some data
132 writeTx.write(nodePath, nodeToWrite);
134 // 3. Ready the Tx for commit
136 DOMStoreThreePhaseCommitCohort cohort = writeTx.ready();
142 // 5. Verify the data in the store
144 DOMStoreReadTransaction readTx = dataStore.newReadOnlyTransaction();
146 Optional<NormalizedNode<?, ?>> optional = readTx.read(nodePath).get(5, TimeUnit.SECONDS);
147 assertEquals("isPresent", true, optional.isPresent());
148 assertEquals("Data node", nodeToWrite, optional.get());
151 public void doCommit(final DOMStoreThreePhaseCommitCohort cohort) throws Exception {
152 Boolean canCommit = cohort.canCommit().get(7, TimeUnit.SECONDS);
153 assertEquals("canCommit", true, canCommit);
154 cohort.preCommit().get(5, TimeUnit.SECONDS);
155 cohort.commit().get(5, TimeUnit.SECONDS);
158 void doCommit(final ListenableFuture<Boolean> canCommitFuture, final DOMStoreThreePhaseCommitCohort cohort) throws Exception {
159 Boolean canCommit = canCommitFuture.get(7, TimeUnit.SECONDS);
160 assertEquals("canCommit", true, canCommit);
161 cohort.preCommit().get(5, TimeUnit.SECONDS);
162 cohort.commit().get(5, TimeUnit.SECONDS);
165 public void cleanup(DistributedDataStore dataStore) {
166 if(dataStore != null) {
167 dataStore.getActorContext().getShardManager().tell(PoisonPill.getInstance(), null);
171 void assertExceptionOnCall(Callable<Void> callable, Class<? extends Exception> expType)
175 fail("Expected " + expType.getSimpleName());
176 } catch(Exception e) {
177 assertEquals("Exception type", expType, e.getClass());
181 void assertExceptionOnTxChainCreates(final DOMStoreTransactionChain txChain,
182 Class<? extends Exception> expType) throws Exception {
183 assertExceptionOnCall(new Callable<Void>() {
185 public Void call() throws Exception {
186 txChain.newWriteOnlyTransaction();
191 assertExceptionOnCall(new Callable<Void>() {
193 public Void call() throws Exception {
194 txChain.newReadWriteTransaction();
199 assertExceptionOnCall(new Callable<Void>() {
201 public Void call() throws Exception {
202 txChain.newReadOnlyTransaction();