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

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.