Clean up plugin management
[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     DatastoreContext.Builder datastoreContextBuilder;
39     DatastoreSnapshot restoreFromSnapshot;
40
41     public IntegrationTestKit(ActorSystem actorSystem, Builder datastoreContextBuilder) {
42         super(actorSystem);
43         this.datastoreContextBuilder = datastoreContextBuilder;
44     }
45
46     public DistributedDataStore setupDistributedDataStore(String typeName, String... shardNames) {
47         return setupDistributedDataStore(typeName, "module-shards.conf", true, SchemaContextHelper.full(), shardNames);
48     }
49
50     public DistributedDataStore setupDistributedDataStore(String typeName, boolean waitUntilLeader,
51             String... shardNames) {
52         return setupDistributedDataStore(typeName, "module-shards.conf", waitUntilLeader,
53                 SchemaContextHelper.full(), shardNames);
54     }
55
56     public DistributedDataStore setupDistributedDataStore(String typeName, String moduleShardsConfig,
57             boolean waitUntilLeader, String... shardNames) {
58         return setupDistributedDataStore(typeName, moduleShardsConfig, waitUntilLeader,
59                 SchemaContextHelper.full(), shardNames);
60     }
61
62     public DistributedDataStore setupDistributedDataStore(String typeName, String moduleShardsConfig,
63             boolean waitUntilLeader, SchemaContext schemaContext, String... shardNames) {
64         ClusterWrapper cluster = new ClusterWrapperImpl(getSystem());
65         Configuration config = new ConfigurationImpl(moduleShardsConfig, "modules.conf");
66
67         datastoreContextBuilder.dataStoreType(typeName);
68
69         DatastoreContext datastoreContext = datastoreContextBuilder.build();
70         DatastoreContextFactory mockContextFactory = Mockito.mock(DatastoreContextFactory.class);
71         Mockito.doReturn(datastoreContext).when(mockContextFactory).getBaseDatastoreContext();
72         Mockito.doReturn(datastoreContext).when(mockContextFactory).getShardDatastoreContext(Mockito.anyString());
73
74         DistributedDataStore dataStore = new DistributedDataStore(getSystem(), cluster, config, mockContextFactory,
75                 restoreFromSnapshot);
76
77         dataStore.onGlobalContextUpdated(schemaContext);
78
79         if(waitUntilLeader) {
80             waitUntilLeader(dataStore.getActorContext(), shardNames);
81         }
82
83         return dataStore;
84     }
85
86     public void waitUntilLeader(ActorContext actorContext, String... shardNames) {
87         for(String shardName: shardNames) {
88             ActorRef shard = findLocalShard(actorContext, shardName);
89
90             assertNotNull("Shard was not created", shard);
91
92             waitUntilLeader(shard);
93         }
94     }
95
96     public void waitUntilNoLeader(ActorContext actorContext, String... shardNames) {
97         for(String shardName: shardNames) {
98             ActorRef shard = findLocalShard(actorContext, shardName);
99             assertNotNull("No local shard found", shard);
100
101             waitUntilNoLeader(shard);
102         }
103     }
104
105     private static ActorRef findLocalShard(ActorContext actorContext, String shardName) {
106         ActorRef shard = null;
107         for(int i = 0; i < 20 * 5 && shard == null; i++) {
108             Uninterruptibles.sleepUninterruptibly(50, TimeUnit.MILLISECONDS);
109             Optional<ActorRef> shardReply = actorContext.findLocalShard(shardName);
110             if(shardReply.isPresent()) {
111                 shard = shardReply.get();
112             }
113         }
114         return shard;
115     }
116
117     void testWriteTransaction(DistributedDataStore dataStore, YangInstanceIdentifier nodePath,
118             NormalizedNode<?, ?> nodeToWrite) throws Exception {
119
120         // 1. Create a write-only Tx
121
122         DOMStoreWriteTransaction writeTx = dataStore.newWriteOnlyTransaction();
123         assertNotNull("newWriteOnlyTransaction returned null", writeTx);
124
125         // 2. Write some data
126
127         writeTx.write(nodePath, nodeToWrite);
128
129         // 3. Ready the Tx for commit
130
131         DOMStoreThreePhaseCommitCohort cohort = writeTx.ready();
132
133         // 4. Commit the Tx
134
135         doCommit(cohort);
136
137         // 5. Verify the data in the store
138
139         DOMStoreReadTransaction readTx = dataStore.newReadOnlyTransaction();
140
141         Optional<NormalizedNode<?, ?>> optional = readTx.read(nodePath).get(5, TimeUnit.SECONDS);
142         assertEquals("isPresent", true, optional.isPresent());
143         assertEquals("Data node", nodeToWrite, optional.get());
144     }
145
146     void doCommit(final DOMStoreThreePhaseCommitCohort cohort) throws Exception {
147         Boolean canCommit = cohort.canCommit().get(7, TimeUnit.SECONDS);
148         assertEquals("canCommit", true, canCommit);
149         cohort.preCommit().get(5, TimeUnit.SECONDS);
150         cohort.commit().get(5, TimeUnit.SECONDS);
151     }
152
153     void doCommit(final ListenableFuture<Boolean> canCommitFuture, final DOMStoreThreePhaseCommitCohort cohort) throws Exception {
154         Boolean canCommit = canCommitFuture.get(7, TimeUnit.SECONDS);
155         assertEquals("canCommit", true, canCommit);
156         cohort.preCommit().get(5, TimeUnit.SECONDS);
157         cohort.commit().get(5, TimeUnit.SECONDS);
158     }
159
160     public void cleanup(DistributedDataStore dataStore) {
161         if(dataStore != null) {
162             dataStore.getActorContext().getShardManager().tell(PoisonPill.getInstance(), null);
163         }
164     }
165
166     void assertExceptionOnCall(Callable<Void> callable, Class<? extends Exception> expType)
167             throws Exception {
168         try {
169             callable.call();
170             fail("Expected " + expType.getSimpleName());
171         } catch(Exception e) {
172             assertEquals("Exception type", expType, e.getClass());
173         }
174     }
175
176     void assertExceptionOnTxChainCreates(final DOMStoreTransactionChain txChain,
177             Class<? extends Exception> expType) throws Exception {
178         assertExceptionOnCall(new Callable<Void>() {
179             @Override
180             public Void call() throws Exception {
181                 txChain.newWriteOnlyTransaction();
182                 return null;
183             }
184         }, expType);
185
186         assertExceptionOnCall(new Callable<Void>() {
187             @Override
188             public Void call() throws Exception {
189                 txChain.newReadWriteTransaction();
190                 return null;
191             }
192         }, expType);
193
194         assertExceptionOnCall(new Callable<Void>() {
195             @Override
196             public Void call() throws Exception {
197                 txChain.newReadOnlyTransaction();
198                 return null;
199             }
200         }, expType);
201     }
202 }