Merge "Add replication capability to Shard"
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / test / java / org / opendaylight / controller / cluster / datastore / BasicIntegrationTest.java
1 /*
2  * Copyright (c) 2014 Cisco 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
9 package org.opendaylight.controller.cluster.datastore;
10
11 import akka.actor.ActorPath;
12 import akka.actor.ActorRef;
13 import akka.actor.ActorSelection;
14 import akka.actor.Props;
15 import akka.testkit.JavaTestKit;
16 import junit.framework.Assert;
17 import org.junit.Test;
18 import org.opendaylight.controller.cluster.datastore.messages.CommitTransaction;
19 import org.opendaylight.controller.cluster.datastore.messages.CreateTransaction;
20 import org.opendaylight.controller.cluster.datastore.messages.CreateTransactionChain;
21 import org.opendaylight.controller.cluster.datastore.messages.CreateTransactionChainReply;
22 import org.opendaylight.controller.cluster.datastore.messages.CreateTransactionReply;
23 import org.opendaylight.controller.cluster.datastore.messages.PreCommitTransaction;
24 import org.opendaylight.controller.cluster.datastore.messages.PreCommitTransactionReply;
25 import org.opendaylight.controller.cluster.datastore.messages.ReadyTransaction;
26 import org.opendaylight.controller.cluster.datastore.messages.ReadyTransactionReply;
27 import org.opendaylight.controller.cluster.datastore.messages.UpdateSchemaContext;
28 import org.opendaylight.controller.cluster.datastore.messages.WriteData;
29 import org.opendaylight.controller.cluster.datastore.messages.WriteDataReply;
30 import org.opendaylight.controller.md.cluster.datastore.model.TestModel;
31 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
32 import scala.concurrent.Await;
33 import scala.concurrent.Future;
34 import scala.concurrent.duration.FiniteDuration;
35
36 import java.util.Collections;
37
38 public class BasicIntegrationTest extends AbstractActorTest {
39
40     @Test
41     public void integrationTest() throws Exception{
42         // System.setProperty("shard.persistent", "true");
43         // This test will
44         // - create a Shard
45         // - initiate a transaction
46         // - write something
47         // - read the transaction for commit
48         // - commit the transaction
49
50
51         new JavaTestKit(getSystem()) {{
52             final Props props = Shard.props("config", Collections.EMPTY_MAP);
53             final ActorRef shard = getSystem().actorOf(props);
54
55             new Within(duration("5 seconds")) {
56                 protected void run() {
57
58
59                     shard.tell(
60                         new UpdateSchemaContext(TestModel.createTestContext()),
61                         getRef());
62
63
64                     // Wait for Shard to become a Leader
65                     try {
66                         Thread.sleep(1000);
67                     } catch (InterruptedException e) {
68                         e.printStackTrace();
69                     }
70                     // 1. Create a TransactionChain
71                     shard.tell(new CreateTransactionChain().toSerializable(), getRef());
72
73                     final ActorSelection transactionChain =
74                         new ExpectMsg<ActorSelection>("CreateTransactionChainReply") {
75                             protected ActorSelection match(Object in) {
76                                 if (in.getClass().equals(CreateTransactionChainReply.SERIALIZABLE_CLASS)) {
77                                     ActorPath transactionChainPath =
78                                         CreateTransactionChainReply.fromSerializable(getSystem(),in)
79                                             .getTransactionChainPath();
80                                     return getSystem()
81                                         .actorSelection(transactionChainPath);
82                                 } else {
83                                     throw noMatch();
84                                 }
85                             }
86                         }.get(); // this extracts the received message
87
88                     Assert.assertNotNull(transactionChain);
89
90                     System.out.println("Successfully created transaction chain");
91
92                     // 2. Create a Transaction on the TransactionChain
93                     transactionChain.tell(new CreateTransaction("txn-1").toSerializable(), getRef());
94
95                     final ActorSelection transaction =
96                         new ExpectMsg<ActorSelection>("CreateTransactionReply") {
97                             protected ActorSelection match(Object in) {
98                                 if (CreateTransactionReply.SERIALIZABLE_CLASS.equals(in.getClass())) {
99                                     CreateTransactionReply reply = CreateTransactionReply.fromSerializable(in);
100                                     return getSystem()
101                                         .actorSelection(reply
102                                             .getTransactionPath());
103                                 } else {
104                                     throw noMatch();
105                                 }
106                             }
107                         }.get(); // this extracts the received message
108
109                     Assert.assertNotNull(transaction);
110
111                     System.out.println("Successfully created transaction");
112
113                     // 3. Write some data
114                     transaction.tell(new WriteData(TestModel.TEST_PATH,
115                         ImmutableNodes.containerNode(TestModel.TEST_QNAME), TestModel.createTestContext()).toSerializable(),
116                         getRef());
117
118                     Boolean writeDone = new ExpectMsg<Boolean>("WriteDataReply") {
119                         protected Boolean match(Object in) {
120                             if (in.getClass().equals(WriteDataReply.SERIALIZABLE_CLASS)) {
121                                 return true;
122                             } else {
123                                 throw noMatch();
124                             }
125                         }
126                     }.get(); // this extracts the received message
127
128                     Assert.assertTrue(writeDone);
129
130                     System.out.println("Successfully wrote data");
131
132                     // 4. Ready the transaction for commit
133
134                     transaction.tell(new ReadyTransaction().toSerializable(), getRef());
135
136                     final ActorSelection cohort =
137                         new ExpectMsg<ActorSelection>("ReadyTransactionReply") {
138                             protected ActorSelection match(Object in) {
139                                 if (in.getClass().equals(ReadyTransactionReply.SERIALIZABLE_CLASS)) {
140                                     ActorPath cohortPath =
141                                         ReadyTransactionReply.fromSerializable(getSystem(),in)
142                                             .getCohortPath();
143                                     return getSystem()
144                                         .actorSelection(cohortPath);
145                                 } else {
146                                     throw noMatch();
147                                 }
148                             }
149                         }.get(); // this extracts the received message
150
151                     Assert.assertNotNull(cohort);
152
153                     System.out.println("Successfully readied the transaction");
154
155                     // 5. PreCommit the transaction
156
157                     cohort.tell(new PreCommitTransaction().toSerializable(), getRef());
158
159                     Boolean preCommitDone =
160                         new ExpectMsg<Boolean>("PreCommitTransactionReply") {
161                             protected Boolean match(Object in) {
162                                 if (in.getClass().equals(PreCommitTransactionReply.SERIALIZABLE_CLASS)) {
163                                     return true;
164                                 } else {
165                                     throw noMatch();
166                                 }
167                             }
168                         }.get(); // this extracts the received message
169
170                     Assert.assertTrue(preCommitDone);
171
172                     System.out.println("Successfully pre-committed the transaction");
173
174                     // 6. Commit the transaction
175                     cohort.tell(new CommitTransaction().toSerializable(), getRef());
176
177                     // FIXME : Add assertions that the commit worked and that the cohort and transaction actors were terminated
178
179                     System.out.println("TODO : Check Successfully committed the transaction");
180                 }
181
182
183             };
184         }
185
186             private ActorRef watchActor(ActorSelection actor) {
187                 Future<ActorRef> future = actor
188                     .resolveOne(FiniteDuration.apply(100, "milliseconds"));
189
190                 try {
191                     ActorRef actorRef = Await.result(future,
192                         FiniteDuration.apply(100, "milliseconds"));
193
194                     watch(actorRef);
195
196                     return actorRef;
197                 } catch (Exception e) {
198                     throw new RuntimeException(e);
199                 }
200
201             }
202         };
203
204
205     }
206 }