Merge "BUG 2852 : Reduce and improve logging in RemoteRpcImplementation"
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / test / java / org / opendaylight / controller / cluster / datastore / ShardTransactionFailureTest.java
1 /*
2  *
3  *  Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
4  *
5  *  This program and the accompanying materials are made available under the
6  *  terms of the Eclipse Public License v1.0 which accompanies this distribution,
7  *  and is available at http://www.eclipse.org/legal/epl-v10.html
8  *
9  */
10
11 package org.opendaylight.controller.cluster.datastore;
12
13 import akka.actor.ActorRef;
14 import akka.actor.Props;
15 import akka.pattern.AskTimeoutException;
16 import akka.testkit.TestActorRef;
17 import com.google.common.util.concurrent.MoreExecutors;
18 import java.util.Collections;
19 import java.util.concurrent.TimeUnit;
20 import org.junit.BeforeClass;
21 import org.junit.Test;
22 import org.opendaylight.controller.cluster.datastore.identifiers.ShardIdentifier;
23 import org.opendaylight.controller.cluster.datastore.jmx.mbeans.shard.ShardStats;
24 import org.opendaylight.controller.cluster.datastore.node.utils.serialization.NormalizedNodeSerializer;
25 import org.opendaylight.controller.md.cluster.datastore.model.TestModel;
26 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
27 import org.opendaylight.controller.md.sal.dom.store.impl.InMemoryDOMDataStore;
28 import org.opendaylight.controller.protobuff.messages.common.NormalizedNodeMessages;
29 import org.opendaylight.controller.protobuff.messages.transaction.ShardTransactionMessages;
30 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
31 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
32 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
33 import scala.concurrent.Await;
34 import scala.concurrent.Future;
35 import scala.concurrent.duration.Duration;
36
37 /**
38  * Covers negative test cases
39  *
40  * @author Basheeruddin Ahmed <syedbahm@cisco.com>
41  */
42 public class ShardTransactionFailureTest extends AbstractActorTest {
43     private static final InMemoryDOMDataStore store =
44         new InMemoryDOMDataStore("OPER", MoreExecutors.sameThreadExecutor());
45
46     private static final SchemaContext testSchemaContext =
47         TestModel.createTestContext();
48
49     private static final ShardIdentifier SHARD_IDENTIFIER =
50         ShardIdentifier.builder().memberName("member-1")
51             .shardName("inventory").type("operational").build();
52
53     private final DatastoreContext datastoreContext = DatastoreContext.newBuilder().build();
54
55     private final ShardStats shardStats = new ShardStats(SHARD_IDENTIFIER.toString(), "DataStore");
56
57     @BeforeClass
58     public static void staticSetup() {
59         store.onGlobalContextUpdated(testSchemaContext);
60     }
61
62     private ActorRef createShard(){
63         return getSystem().actorOf(Shard.props(SHARD_IDENTIFIER, Collections.<String, String>emptyMap(), datastoreContext,
64                 TestModel.createTestContext()));
65     }
66
67     @Test(expected = ReadFailedException.class)
68     public void testNegativeReadWithReadOnlyTransactionClosed()
69         throws Throwable {
70
71         final ActorRef shard = createShard();
72         final Props props = ShardTransaction.props(store.newReadOnlyTransaction(), shard,
73                 testSchemaContext, datastoreContext, shardStats, "txn",
74                 DataStoreVersions.CURRENT_VERSION);
75
76         final TestActorRef<ShardTransaction> subject = TestActorRef
77             .create(getSystem(), props,
78                 "testNegativeReadWithReadOnlyTransactionClosed");
79
80         ShardTransactionMessages.ReadData readData =
81             ShardTransactionMessages.ReadData.newBuilder()
82                 .setInstanceIdentifierPathArguments(
83                     NormalizedNodeMessages.InstanceIdentifier.newBuilder()
84                         .build()
85                 ).build();
86         Future<Object> future =
87             akka.pattern.Patterns.ask(subject, readData, 3000);
88         Await.result(future, Duration.create(3, TimeUnit.SECONDS));
89
90         subject.underlyingActor().getDOMStoreTransaction().close();
91
92         future = akka.pattern.Patterns.ask(subject, readData, 3000);
93         Await.result(future, Duration.create(3, TimeUnit.SECONDS));
94     }
95
96
97     @Test(expected = ReadFailedException.class)
98     public void testNegativeReadWithReadWriteTransactionClosed()
99         throws Throwable {
100
101         final ActorRef shard = createShard();
102         final Props props = ShardTransaction.props(store.newReadWriteTransaction(), shard,
103                 testSchemaContext, datastoreContext, shardStats, "txn",
104                 DataStoreVersions.CURRENT_VERSION);
105
106         final TestActorRef<ShardTransaction> subject = TestActorRef
107             .create(getSystem(), props,
108                 "testNegativeReadWithReadWriteTransactionClosed");
109
110         ShardTransactionMessages.ReadData readData =
111             ShardTransactionMessages.ReadData.newBuilder()
112                 .setInstanceIdentifierPathArguments(
113                     NormalizedNodeMessages.InstanceIdentifier.newBuilder()
114                         .build()
115                 ).build();
116
117         Future<Object> future =
118             akka.pattern.Patterns.ask(subject, readData, 3000);
119         Await.result(future, Duration.create(3, TimeUnit.SECONDS));
120
121         subject.underlyingActor().getDOMStoreTransaction().close();
122
123         future = akka.pattern.Patterns.ask(subject, readData, 3000);
124         Await.result(future, Duration.create(3, TimeUnit.SECONDS));
125     }
126
127     @Test(expected = ReadFailedException.class)
128     public void testNegativeExistsWithReadWriteTransactionClosed()
129         throws Throwable {
130
131         final ActorRef shard = createShard();
132         final Props props = ShardTransaction.props(store.newReadWriteTransaction(), shard,
133                 testSchemaContext, datastoreContext, shardStats, "txn",
134                 DataStoreVersions.CURRENT_VERSION);
135
136         final TestActorRef<ShardTransaction> subject = TestActorRef
137             .create(getSystem(), props,
138                 "testNegativeExistsWithReadWriteTransactionClosed");
139
140         ShardTransactionMessages.DataExists dataExists =
141             ShardTransactionMessages.DataExists.newBuilder()
142                 .setInstanceIdentifierPathArguments(
143                     NormalizedNodeMessages.InstanceIdentifier.newBuilder()
144                         .build()
145                 ).build();
146
147         Future<Object> future =
148             akka.pattern.Patterns.ask(subject, dataExists, 3000);
149         Await.result(future, Duration.create(3, TimeUnit.SECONDS));
150
151         subject.underlyingActor().getDOMStoreTransaction().close();
152
153         future = akka.pattern.Patterns.ask(subject, dataExists, 3000);
154         Await.result(future, Duration.create(3, TimeUnit.SECONDS));
155     }
156
157     @Test(expected = AskTimeoutException.class)
158     public void testNegativeWriteWithTransactionReady() throws Exception {
159
160
161         final ActorRef shard = createShard();
162         final Props props = ShardTransaction.props(store.newWriteOnlyTransaction(), shard,
163                 testSchemaContext, datastoreContext, shardStats, "txn",
164                 DataStoreVersions.CURRENT_VERSION);
165
166         final TestActorRef<ShardTransaction> subject = TestActorRef
167             .create(getSystem(), props,
168                 "testNegativeWriteWithTransactionReady");
169
170         ShardTransactionMessages.ReadyTransaction readyTransaction =
171             ShardTransactionMessages.ReadyTransaction.newBuilder().build();
172
173         Future<Object> future =
174             akka.pattern.Patterns.ask(subject, readyTransaction, 3000);
175         Await.result(future, Duration.create(3, TimeUnit.SECONDS));
176
177         ShardTransactionMessages.WriteData writeData =
178             ShardTransactionMessages.WriteData.newBuilder()
179                 .setInstanceIdentifierPathArguments(
180                     NormalizedNodeMessages.InstanceIdentifier.newBuilder()
181                         .build()).setNormalizedNode(
182                 buildNormalizedNode()
183
184             ).build();
185
186         future = akka.pattern.Patterns.ask(subject, writeData, 3000);
187         Await.result(future, Duration.create(3, TimeUnit.SECONDS));
188     }
189
190     @Test(expected = AskTimeoutException.class)
191     public void testNegativeReadWriteWithTransactionReady() throws Exception {
192
193
194         final ActorRef shard = createShard();
195         final Props props = ShardTransaction.props(store.newReadWriteTransaction(), shard,
196                 testSchemaContext, datastoreContext, shardStats, "txn",
197                 DataStoreVersions.CURRENT_VERSION);
198
199         final TestActorRef<ShardTransaction> subject = TestActorRef
200             .create(getSystem(), props,
201                 "testNegativeReadWriteWithTransactionReady");
202
203         ShardTransactionMessages.ReadyTransaction readyTransaction =
204             ShardTransactionMessages.ReadyTransaction.newBuilder().build();
205
206         Future<Object> future =
207             akka.pattern.Patterns.ask(subject, readyTransaction, 3000);
208         Await.result(future, Duration.create(3, TimeUnit.SECONDS));
209
210         ShardTransactionMessages.WriteData writeData =
211             ShardTransactionMessages.WriteData.newBuilder()
212                 .setInstanceIdentifierPathArguments(
213                     NormalizedNodeMessages.InstanceIdentifier.newBuilder()
214                         .build()
215                 )
216                 .setNormalizedNode(buildNormalizedNode())
217                 .build();
218
219         future = akka.pattern.Patterns.ask(subject, writeData, 3000);
220         Await.result(future, Duration.create(3, TimeUnit.SECONDS));
221     }
222
223     private NormalizedNodeMessages.Node buildNormalizedNode() {
224         return NormalizedNodeSerializer
225             .serialize(Builders.containerBuilder().withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(TestModel.TEST_QNAME)).build());
226     }
227
228     @Test(expected = AskTimeoutException.class)
229     public void testNegativeMergeTransactionReady() throws Exception {
230
231
232         final ActorRef shard = createShard();
233         final Props props = ShardTransaction.props(store.newReadWriteTransaction(), shard,
234                 testSchemaContext, datastoreContext, shardStats, "txn",
235                 DataStoreVersions.CURRENT_VERSION);
236
237         final TestActorRef<ShardTransaction> subject = TestActorRef
238             .create(getSystem(), props, "testNegativeMergeTransactionReady");
239
240         ShardTransactionMessages.ReadyTransaction readyTransaction =
241             ShardTransactionMessages.ReadyTransaction.newBuilder().build();
242
243         Future<Object> future =
244             akka.pattern.Patterns.ask(subject, readyTransaction, 3000);
245         Await.result(future, Duration.create(3, TimeUnit.SECONDS));
246
247         ShardTransactionMessages.MergeData mergeData =
248             ShardTransactionMessages.MergeData.newBuilder()
249                 .setInstanceIdentifierPathArguments(
250                     NormalizedNodeMessages.InstanceIdentifier.newBuilder()
251                         .build()).setNormalizedNode(
252                 buildNormalizedNode()
253
254             ).build();
255
256         future = akka.pattern.Patterns.ask(subject, mergeData, 3000);
257         Await.result(future, Duration.create(3, TimeUnit.SECONDS));
258     }
259
260
261     @Test(expected = AskTimeoutException.class)
262     public void testNegativeDeleteDataWhenTransactionReady() throws Exception {
263
264
265         final ActorRef shard = createShard();
266         final Props props = ShardTransaction.props(store.newReadWriteTransaction(), shard,
267                 testSchemaContext, datastoreContext, shardStats, "txn",
268                 DataStoreVersions.CURRENT_VERSION);
269
270         final TestActorRef<ShardTransaction> subject = TestActorRef
271             .create(getSystem(), props,
272                 "testNegativeDeleteDataWhenTransactionReady");
273
274         ShardTransactionMessages.ReadyTransaction readyTransaction =
275             ShardTransactionMessages.ReadyTransaction.newBuilder().build();
276
277         Future<Object> future =
278             akka.pattern.Patterns.ask(subject, readyTransaction, 3000);
279         Await.result(future, Duration.create(3, TimeUnit.SECONDS));
280
281         ShardTransactionMessages.DeleteData deleteData =
282             ShardTransactionMessages.DeleteData.newBuilder()
283                 .setInstanceIdentifierPathArguments(
284                     NormalizedNodeMessages.InstanceIdentifier.newBuilder()
285                         .build()).build();
286
287         future = akka.pattern.Patterns.ask(subject, deleteData, 3000);
288         Await.result(future, Duration.create(3, TimeUnit.SECONDS));
289     }
290 }