9cc6d838c8c547a59384b97a63ea36fbb26e1909
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / test / java / org / opendaylight / controller / cluster / datastore / IntegrationTestKit.java
1 /*
2  * Copyright (c) 2015 Brocade Communications Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.controller.cluster.datastore;
9
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;
35
36 public class IntegrationTestKit extends ShardTestKit {
37
38     protected DatastoreContext.Builder datastoreContextBuilder;
39     protected DatastoreSnapshot restoreFromSnapshot;
40
41     public IntegrationTestKit(ActorSystem actorSystem, Builder datastoreContextBuilder) {
42         super(actorSystem);
43         this.datastoreContextBuilder = datastoreContextBuilder;
44     }
45
46     public DatastoreContext.Builder getDatastoreContextBuilder() {
47         return datastoreContextBuilder;
48     }
49
50     public DistributedDataStore setupDistributedDataStore(String typeName, String... shardNames) {
51         return setupDistributedDataStore(typeName, "module-shards.conf", true, SchemaContextHelper.full(), shardNames);
52     }
53
54     public DistributedDataStore setupDistributedDataStore(String typeName, boolean waitUntilLeader,
55             String... shardNames) {
56         return setupDistributedDataStore(typeName, "module-shards.conf", waitUntilLeader,
57                 SchemaContextHelper.full(), shardNames);
58     }
59
60     public DistributedDataStore setupDistributedDataStore(String typeName, String moduleShardsConfig,
61             boolean waitUntilLeader, String... shardNames) {
62         return setupDistributedDataStore(typeName, moduleShardsConfig, waitUntilLeader,
63                 SchemaContextHelper.full(), shardNames);
64     }
65
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");
70
71         datastoreContextBuilder.dataStoreType(typeName);
72
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());
77
78         DistributedDataStore dataStore = new DistributedDataStore(getSystem(), cluster, config, mockContextFactory,
79                 restoreFromSnapshot);
80
81         dataStore.onGlobalContextUpdated(schemaContext);
82
83         if(waitUntilLeader) {
84             waitUntilLeader(dataStore.getActorContext(), shardNames);
85         }
86
87         datastoreContextBuilder = DatastoreContext.newBuilderFrom(datastoreContext);
88         return dataStore;
89     }
90
91     public void waitUntilLeader(ActorContext actorContext, String... shardNames) {
92         for(String shardName: shardNames) {
93             ActorRef shard = findLocalShard(actorContext, shardName);
94
95             assertNotNull("Shard was not created", shard);
96
97             waitUntilLeader(shard);
98         }
99     }
100
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);
105
106             waitUntilNoLeader(shard);
107         }
108     }
109
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();
117             }
118         }
119         return shard;
120     }
121
122     void testWriteTransaction(DistributedDataStore dataStore, YangInstanceIdentifier nodePath,
123             NormalizedNode<?, ?> nodeToWrite) throws Exception {
124
125         // 1. Create a write-only Tx
126
127         DOMStoreWriteTransaction writeTx = dataStore.newWriteOnlyTransaction();
128         assertNotNull("newWriteOnlyTransaction returned null", writeTx);
129
130         // 2. Write some data
131
132         writeTx.write(nodePath, nodeToWrite);
133
134         // 3. Ready the Tx for commit
135
136         DOMStoreThreePhaseCommitCohort cohort = writeTx.ready();
137
138         // 4. Commit the Tx
139
140         doCommit(cohort);
141
142         // 5. Verify the data in the store
143
144         DOMStoreReadTransaction readTx = dataStore.newReadOnlyTransaction();
145
146         Optional<NormalizedNode<?, ?>> optional = readTx.read(nodePath).get(5, TimeUnit.SECONDS);
147         assertEquals("isPresent", true, optional.isPresent());
148         assertEquals("Data node", nodeToWrite, optional.get());
149     }
150
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);
156     }
157
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);
163     }
164
165     public void cleanup(DistributedDataStore dataStore) {
166         if(dataStore != null) {
167             dataStore.getActorContext().getShardManager().tell(PoisonPill.getInstance(), null);
168         }
169     }
170
171     void assertExceptionOnCall(Callable<Void> callable, Class<? extends Exception> expType)
172             throws Exception {
173         try {
174             callable.call();
175             fail("Expected " + expType.getSimpleName());
176         } catch(Exception e) {
177             assertEquals("Exception type", expType, e.getClass());
178         }
179     }
180
181     void assertExceptionOnTxChainCreates(final DOMStoreTransactionChain txChain,
182             Class<? extends Exception> expType) throws Exception {
183         assertExceptionOnCall(new Callable<Void>() {
184             @Override
185             public Void call() throws Exception {
186                 txChain.newWriteOnlyTransaction();
187                 return null;
188             }
189         }, expType);
190
191         assertExceptionOnCall(new Callable<Void>() {
192             @Override
193             public Void call() throws Exception {
194                 txChain.newReadWriteTransaction();
195                 return null;
196             }
197         }, expType);
198
199         assertExceptionOnCall(new Callable<Void>() {
200             @Override
201             public Void call() throws Exception {
202                 txChain.newReadOnlyTransaction();
203                 return null;
204             }
205         }, expType);
206     }
207 }