Bug 3206: CDS - issues with direct commit
[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.shardstrategy.ShardStrategyFactory;
23 import org.opendaylight.controller.cluster.datastore.utils.ActorContext;
24 import org.opendaylight.controller.md.cluster.datastore.model.SchemaContextHelper;
25 import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadTransaction;
26 import org.opendaylight.controller.sal.core.spi.data.DOMStoreThreePhaseCommitCohort;
27 import org.opendaylight.controller.sal.core.spi.data.DOMStoreTransactionChain;
28 import org.opendaylight.controller.sal.core.spi.data.DOMStoreWriteTransaction;
29 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
30 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
31 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
32
33 class IntegrationTestKit extends ShardTestKit {
34
35     DatastoreContext.Builder datastoreContextBuilder;
36
37     IntegrationTestKit(ActorSystem actorSystem, Builder datastoreContextBuilder) {
38         super(actorSystem);
39         this.datastoreContextBuilder = datastoreContextBuilder;
40     }
41
42     DistributedDataStore setupDistributedDataStore(String typeName, String... shardNames) {
43         return setupDistributedDataStore(typeName, true, shardNames);
44     }
45
46     DistributedDataStore setupDistributedDataStore(String typeName, boolean waitUntilLeader,
47             String... shardNames) {
48         return setupDistributedDataStore(typeName, "module-shards.conf", waitUntilLeader, shardNames);
49     }
50
51     DistributedDataStore setupDistributedDataStore(String typeName, String moduleShardsConfig, boolean waitUntilLeader,
52             String... shardNames) {
53         ClusterWrapper cluster = new ClusterWrapperImpl(getSystem());
54         Configuration config = new ConfigurationImpl(moduleShardsConfig, "modules.conf");
55         ShardStrategyFactory.setConfiguration(config);
56
57         datastoreContextBuilder.dataStoreType(typeName);
58
59         DatastoreContext datastoreContext = datastoreContextBuilder.build();
60
61         DistributedDataStore dataStore = new DistributedDataStore(getSystem(), cluster, config, datastoreContext);
62
63         SchemaContext schemaContext = SchemaContextHelper.full();
64         dataStore.onGlobalContextUpdated(schemaContext);
65
66         if(waitUntilLeader) {
67             waitUntilLeader(dataStore.getActorContext(), shardNames);
68         }
69
70         return dataStore;
71     }
72
73     void waitUntilLeader(ActorContext actorContext, String... shardNames) {
74         for(String shardName: shardNames) {
75             ActorRef shard = null;
76             for(int i = 0; i < 20 * 5 && shard == null; i++) {
77                 Uninterruptibles.sleepUninterruptibly(50, TimeUnit.MILLISECONDS);
78                 Optional<ActorRef> shardReply = actorContext.findLocalShard(shardName);
79                 if(shardReply.isPresent()) {
80                     shard = shardReply.get();
81                 }
82             }
83
84             assertNotNull("Shard was not created", shard);
85
86             waitUntilLeader(shard);
87         }
88     }
89
90     void testWriteTransaction(DistributedDataStore dataStore, YangInstanceIdentifier nodePath,
91             NormalizedNode<?, ?> nodeToWrite) throws Exception {
92
93         // 1. Create a write-only Tx
94
95         DOMStoreWriteTransaction writeTx = dataStore.newWriteOnlyTransaction();
96         assertNotNull("newWriteOnlyTransaction returned null", writeTx);
97
98         // 2. Write some data
99
100         writeTx.write(nodePath, nodeToWrite);
101
102         // 3. Ready the Tx for commit
103
104         DOMStoreThreePhaseCommitCohort cohort = writeTx.ready();
105
106         // 4. Commit the Tx
107
108         doCommit(cohort);
109
110         // 5. Verify the data in the store
111
112         DOMStoreReadTransaction readTx = dataStore.newReadOnlyTransaction();
113
114         Optional<NormalizedNode<?, ?>> optional = readTx.read(nodePath).get(5, TimeUnit.SECONDS);
115         assertEquals("isPresent", true, optional.isPresent());
116         assertEquals("Data node", nodeToWrite, optional.get());
117     }
118
119     void doCommit(final DOMStoreThreePhaseCommitCohort cohort) throws Exception {
120         Boolean canCommit = cohort.canCommit().get(7, TimeUnit.SECONDS);
121         assertEquals("canCommit", true, canCommit);
122         cohort.preCommit().get(5, TimeUnit.SECONDS);
123         cohort.commit().get(5, TimeUnit.SECONDS);
124     }
125
126     void doCommit(final ListenableFuture<Boolean> canCommitFuture, final DOMStoreThreePhaseCommitCohort cohort) throws Exception {
127         Boolean canCommit = canCommitFuture.get(7, TimeUnit.SECONDS);
128         assertEquals("canCommit", true, canCommit);
129         cohort.preCommit().get(5, TimeUnit.SECONDS);
130         cohort.commit().get(5, TimeUnit.SECONDS);
131     }
132
133     void cleanup(DistributedDataStore dataStore) {
134         if(dataStore != null) {
135             dataStore.getActorContext().getShardManager().tell(PoisonPill.getInstance(), null);
136         }
137     }
138
139     void assertExceptionOnCall(Callable<Void> callable, Class<? extends Exception> expType)
140             throws Exception {
141         try {
142             callable.call();
143             fail("Expected " + expType.getSimpleName());
144         } catch(Exception e) {
145             assertEquals("Exception type", expType, e.getClass());
146         }
147     }
148
149     void assertExceptionOnTxChainCreates(final DOMStoreTransactionChain txChain,
150             Class<? extends Exception> expType) throws Exception {
151         assertExceptionOnCall(new Callable<Void>() {
152             @Override
153             public Void call() throws Exception {
154                 txChain.newWriteOnlyTransaction();
155                 return null;
156             }
157         }, expType);
158
159         assertExceptionOnCall(new Callable<Void>() {
160             @Override
161             public Void call() throws Exception {
162                 txChain.newReadWriteTransaction();
163                 return null;
164             }
165         }, expType);
166
167         assertExceptionOnCall(new Callable<Void>() {
168             @Override
169             public Void call() throws Exception {
170                 txChain.newReadOnlyTransaction();
171                 return null;
172             }
173         }, expType);
174     }
175 }