Bug 4105: Move Configuration classes to config package
[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.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;
34
35 class IntegrationTestKit extends ShardTestKit {
36
37     DatastoreContext.Builder datastoreContextBuilder;
38
39     IntegrationTestKit(ActorSystem actorSystem, Builder datastoreContextBuilder) {
40         super(actorSystem);
41         this.datastoreContextBuilder = datastoreContextBuilder;
42     }
43
44     DistributedDataStore setupDistributedDataStore(String typeName, String... shardNames) {
45         return setupDistributedDataStore(typeName, true, shardNames);
46     }
47
48     DistributedDataStore setupDistributedDataStore(String typeName, boolean waitUntilLeader,
49             String... shardNames) {
50         return setupDistributedDataStore(typeName, "module-shards.conf", waitUntilLeader, shardNames);
51     }
52
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);
58
59         datastoreContextBuilder.dataStoreType(typeName);
60
61         DatastoreContext datastoreContext = datastoreContextBuilder.build();
62
63         DistributedDataStore dataStore = new DistributedDataStore(getSystem(), cluster, config, datastoreContext);
64
65         SchemaContext schemaContext = SchemaContextHelper.full();
66         dataStore.onGlobalContextUpdated(schemaContext);
67
68         if(waitUntilLeader) {
69             waitUntilLeader(dataStore.getActorContext(), shardNames);
70         }
71
72         return dataStore;
73     }
74
75     void waitUntilLeader(ActorContext actorContext, String... shardNames) {
76         for(String shardName: shardNames) {
77             ActorRef shard = findLocalShard(actorContext, shardName);
78
79             assertNotNull("Shard was not created", shard);
80
81             waitUntilLeader(shard);
82         }
83     }
84
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);
89
90             waitUntilNoLeader(shard);
91         }
92     }
93
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();
101             }
102         }
103         return shard;
104     }
105
106     void testWriteTransaction(DistributedDataStore dataStore, YangInstanceIdentifier nodePath,
107             NormalizedNode<?, ?> nodeToWrite) throws Exception {
108
109         // 1. Create a write-only Tx
110
111         DOMStoreWriteTransaction writeTx = dataStore.newWriteOnlyTransaction();
112         assertNotNull("newWriteOnlyTransaction returned null", writeTx);
113
114         // 2. Write some data
115
116         writeTx.write(nodePath, nodeToWrite);
117
118         // 3. Ready the Tx for commit
119
120         DOMStoreThreePhaseCommitCohort cohort = writeTx.ready();
121
122         // 4. Commit the Tx
123
124         doCommit(cohort);
125
126         // 5. Verify the data in the store
127
128         DOMStoreReadTransaction readTx = dataStore.newReadOnlyTransaction();
129
130         Optional<NormalizedNode<?, ?>> optional = readTx.read(nodePath).get(5, TimeUnit.SECONDS);
131         assertEquals("isPresent", true, optional.isPresent());
132         assertEquals("Data node", nodeToWrite, optional.get());
133     }
134
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);
140     }
141
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);
147     }
148
149     void cleanup(DistributedDataStore dataStore) {
150         if(dataStore != null) {
151             dataStore.getActorContext().getShardManager().tell(PoisonPill.getInstance(), null);
152         }
153     }
154
155     void assertExceptionOnCall(Callable<Void> callable, Class<? extends Exception> expType)
156             throws Exception {
157         try {
158             callable.call();
159             fail("Expected " + expType.getSimpleName());
160         } catch(Exception e) {
161             assertEquals("Exception type", expType, e.getClass());
162         }
163     }
164
165     void assertExceptionOnTxChainCreates(final DOMStoreTransactionChain txChain,
166             Class<? extends Exception> expType) throws Exception {
167         assertExceptionOnCall(new Callable<Void>() {
168             @Override
169             public Void call() throws Exception {
170                 txChain.newWriteOnlyTransaction();
171                 return null;
172             }
173         }, expType);
174
175         assertExceptionOnCall(new Callable<Void>() {
176             @Override
177             public Void call() throws Exception {
178                 txChain.newReadWriteTransaction();
179                 return null;
180             }
181         }, expType);
182
183         assertExceptionOnCall(new Callable<Void>() {
184             @Override
185             public Void call() throws Exception {
186                 txChain.newReadOnlyTransaction();
187                 return null;
188             }
189         }, expType);
190     }
191 }