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