Specialize tests to ClientBackedDataStore
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / test / java / org / opendaylight / controller / cluster / datastore / DataTreeCohortIntegrationTest.java
1 /*
2  * Copyright (c) 2016 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 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.assertSame;
13 import static org.junit.Assert.fail;
14 import static org.mockito.ArgumentMatchers.any;
15 import static org.mockito.ArgumentMatchers.anyCollection;
16 import static org.mockito.Mockito.doReturn;
17 import static org.mockito.Mockito.mock;
18 import static org.mockito.Mockito.reset;
19 import static org.mockito.Mockito.verify;
20 import static org.mockito.Mockito.verifyNoMoreInteractions;
21
22 import akka.actor.ActorSystem;
23 import akka.actor.Address;
24 import akka.actor.AddressFromURIString;
25 import akka.cluster.Cluster;
26 import akka.testkit.javadsl.TestKit;
27 import com.google.common.base.Throwables;
28 import com.google.common.util.concurrent.FluentFuture;
29 import com.typesafe.config.ConfigFactory;
30 import java.util.Collection;
31 import java.util.concurrent.ExecutionException;
32 import java.util.concurrent.TimeUnit;
33 import org.junit.AfterClass;
34 import org.junit.BeforeClass;
35 import org.junit.Ignore;
36 import org.junit.Test;
37 import org.mockito.ArgumentCaptor;
38 import org.opendaylight.controller.cluster.databroker.ClientBackedDataStore;
39 import org.opendaylight.controller.md.cluster.datastore.model.CarsModel;
40 import org.opendaylight.controller.md.cluster.datastore.model.TestModel;
41 import org.opendaylight.mdsal.common.api.DataValidationFailedException;
42 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
43 import org.opendaylight.mdsal.common.api.PostCanCommitStep;
44 import org.opendaylight.mdsal.common.api.PostPreCommitStep;
45 import org.opendaylight.mdsal.common.api.ThreePhaseCommitStep;
46 import org.opendaylight.mdsal.dom.api.DOMDataTreeCandidate;
47 import org.opendaylight.mdsal.dom.api.DOMDataTreeCommitCohort;
48 import org.opendaylight.mdsal.dom.api.DOMDataTreeIdentifier;
49 import org.opendaylight.mdsal.dom.spi.store.DOMStoreThreePhaseCommitCohort;
50 import org.opendaylight.mdsal.dom.spi.store.DOMStoreWriteTransaction;
51 import org.opendaylight.yangtools.util.concurrent.FluentFutures;
52 import org.opendaylight.yangtools.yang.common.Uint64;
53 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
54 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
55 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
56 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
57 import org.opendaylight.yangtools.yang.data.tree.api.ModificationType;
58 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
59
60 public class DataTreeCohortIntegrationTest {
61
62     private static final DataValidationFailedException FAILED_CAN_COMMIT =
63             new DataValidationFailedException(YangInstanceIdentifier.class, TestModel.TEST_PATH, "Test failure.");
64     private static final FluentFuture<PostCanCommitStep> FAILED_CAN_COMMIT_FUTURE =
65             FluentFutures.immediateFailedFluentFuture(FAILED_CAN_COMMIT);
66
67     private static final DOMDataTreeIdentifier TEST_ID =
68             new DOMDataTreeIdentifier(LogicalDatastoreType.CONFIGURATION, TestModel.TEST_PATH);
69
70     private static ActorSystem system;
71
72     private final DatastoreContext.Builder datastoreContextBuilder =
73             DatastoreContext.newBuilder().shardHeartbeatIntervalInMillis(100);
74
75     @BeforeClass
76     public static void setUpClass() {
77         system = ActorSystem.create("cluster-test", ConfigFactory.load().getConfig("Member1"));
78         final Address member1Address = AddressFromURIString.parse("akka://cluster-test@127.0.0.1:2558");
79         Cluster.get(system).join(member1Address);
80     }
81
82     @AfterClass
83     public static void tearDownClass() {
84         TestKit.shutdownActorSystem(system);
85         system = null;
86     }
87
88     protected ActorSystem getSystem() {
89         return system;
90     }
91
92     @SuppressWarnings({ "unchecked", "rawtypes" })
93     @Test
94     public void testSuccessfulCanCommitWithNoopPostStep() throws Exception {
95         final var cohort = mock(DOMDataTreeCommitCohort.class);
96         doReturn(PostCanCommitStep.NOOP_SUCCESSFUL_FUTURE).when(cohort).canCommit(any(Object.class),
97                 any(EffectiveModelContext.class), anyCollection());
98         ArgumentCaptor<Collection> candidateCapt = ArgumentCaptor.forClass(Collection.class);
99         IntegrationTestKit kit = new IntegrationTestKit(getSystem(), datastoreContextBuilder);
100
101         try (var dataStore = kit.setupDataStore(ClientBackedDataStore.class, "testSuccessfulCanCommitWithNoopPostStep",
102             "test-1")) {
103
104             final var cohortReg = dataStore.registerCommitCohort(TEST_ID, cohort);
105             assertNotNull(cohortReg);
106
107             IntegrationTestKit.verifyShardState(dataStore, "test-1",
108                 state -> assertEquals("Cohort registrations", 1, state.getCommitCohortActors().size()));
109
110             final var node = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
111             kit.testWriteTransaction(dataStore, TestModel.TEST_PATH, node);
112             verify(cohort).canCommit(any(Object.class), any(EffectiveModelContext.class), candidateCapt.capture());
113             assertDataTreeCandidate((DOMDataTreeCandidate) candidateCapt.getValue().iterator().next(), TEST_ID,
114                     ModificationType.WRITE, node, null);
115
116             reset(cohort);
117             doReturn(PostCanCommitStep.NOOP_SUCCESSFUL_FUTURE).when(cohort).canCommit(any(Object.class),
118                     any(EffectiveModelContext.class), anyCollection());
119
120             kit.testWriteTransaction(dataStore, TestModel.OUTER_LIST_PATH,
121                     ImmutableNodes.mapNodeBuilder(TestModel.OUTER_LIST_QNAME)
122                     .withChild(ImmutableNodes.mapEntry(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 42))
123                     .build());
124             verify(cohort).canCommit(any(Object.class), any(EffectiveModelContext.class), anyCollection());
125
126             cohortReg.close();
127
128             IntegrationTestKit.verifyShardState(dataStore, "test-1",
129                 state -> assertEquals("Cohort registrations", 0, state.getCommitCohortActors().size()));
130
131             kit.testWriteTransaction(dataStore, TestModel.TEST_PATH, node);
132             verifyNoMoreInteractions(cohort);
133         }
134     }
135
136     @Test
137     public void testFailedCanCommit() throws Exception {
138         final var failedCohort = mock(DOMDataTreeCommitCohort.class);
139
140         doReturn(FAILED_CAN_COMMIT_FUTURE).when(failedCohort).canCommit(any(Object.class),
141                 any(EffectiveModelContext.class), anyCollection());
142
143         final var kit = new IntegrationTestKit(getSystem(), datastoreContextBuilder);
144         try (var dataStore = kit.setupDataStore(ClientBackedDataStore.class, "testFailedCanCommit", "test-1")) {
145             dataStore.registerCommitCohort(TEST_ID, failedCohort);
146
147             IntegrationTestKit.verifyShardState(dataStore, "test-1",
148                 state -> assertEquals("Cohort registrations", 1, state.getCommitCohortActors().size()));
149
150             DOMStoreWriteTransaction writeTx = dataStore.newWriteOnlyTransaction();
151             writeTx.write(TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
152             DOMStoreThreePhaseCommitCohort dsCohort = writeTx.ready();
153             try {
154                 dsCohort.canCommit().get(5, TimeUnit.SECONDS);
155                 fail("Exception should be raised.");
156             } catch (ExecutionException e) {
157                 assertSame(FAILED_CAN_COMMIT, Throwables.getRootCause(e));
158             }
159         }
160     }
161
162     @SuppressWarnings({ "unchecked", "rawtypes" })
163     @Test
164     public void testCanCommitWithListEntries() throws Exception {
165         final var cohort = mock(DOMDataTreeCommitCohort.class);
166         doReturn(PostCanCommitStep.NOOP_SUCCESSFUL_FUTURE).when(cohort).canCommit(any(Object.class),
167                 any(EffectiveModelContext.class), anyCollection());
168         final var kit = new IntegrationTestKit(getSystem(), datastoreContextBuilder);
169
170         try (var dataStore = kit.setupDataStore(ClientBackedDataStore.class, "testCanCommitWithMultipleListEntries",
171             "cars-1")) {
172
173             final var cohortReg = dataStore.registerCommitCohort(
174                     new DOMDataTreeIdentifier(LogicalDatastoreType.CONFIGURATION, CarsModel.CAR_LIST_PATH
175                             .node(CarsModel.CAR_QNAME)), cohort);
176             assertNotNull(cohortReg);
177
178             IntegrationTestKit.verifyShardState(dataStore, "cars-1",
179                 state -> assertEquals("Cohort registrations", 1, state.getCommitCohortActors().size()));
180
181             // First write an empty base container and verify the cohort isn't invoked.
182
183             DOMStoreWriteTransaction writeTx = dataStore.newWriteOnlyTransaction();
184             writeTx.write(CarsModel.BASE_PATH, CarsModel.emptyContainer());
185             writeTx.write(CarsModel.CAR_LIST_PATH, CarsModel.newCarMapNode());
186             kit.doCommit(writeTx.ready());
187             verifyNoMoreInteractions(cohort);
188
189             // Write a single car entry and verify the cohort is invoked.
190
191             writeTx = dataStore.newWriteOnlyTransaction();
192             final YangInstanceIdentifier optimaPath = CarsModel.newCarPath("optima");
193             final MapEntryNode optimaNode = CarsModel.newCarEntry("optima", Uint64.valueOf(20000));
194             writeTx.write(optimaPath, optimaNode);
195             kit.doCommit(writeTx.ready());
196
197             ArgumentCaptor<Collection> candidateCapture = ArgumentCaptor.forClass(Collection.class);
198             verify(cohort).canCommit(any(Object.class), any(EffectiveModelContext.class), candidateCapture.capture());
199             assertDataTreeCandidate((DOMDataTreeCandidate) candidateCapture.getValue().iterator().next(),
200                     new DOMDataTreeIdentifier(LogicalDatastoreType.CONFIGURATION, optimaPath), ModificationType.WRITE,
201                     optimaNode, null);
202
203             // Write replace the cars container with 2 new car entries. The cohort should get invoked with 3
204             // DOMDataTreeCandidates: once for each of the 2 new car entries (WRITE mod) and once for the deleted prior
205             // car entry (DELETE mod).
206
207             reset(cohort);
208             doReturn(PostCanCommitStep.NOOP_SUCCESSFUL_FUTURE).when(cohort).canCommit(any(Object.class),
209                     any(EffectiveModelContext.class), anyCollection());
210
211             writeTx = dataStore.newWriteOnlyTransaction();
212             final YangInstanceIdentifier sportagePath = CarsModel.newCarPath("sportage");
213             final MapEntryNode sportageNode = CarsModel.newCarEntry("sportage", Uint64.valueOf(20000));
214             final YangInstanceIdentifier soulPath = CarsModel.newCarPath("soul");
215             final MapEntryNode soulNode = CarsModel.newCarEntry("soul", Uint64.valueOf(20000));
216             writeTx.write(CarsModel.BASE_PATH, CarsModel.newCarsNode(CarsModel.newCarsMapNode(sportageNode,soulNode)));
217             kit.doCommit(writeTx.ready());
218
219             candidateCapture = ArgumentCaptor.forClass(Collection.class);
220             verify(cohort).canCommit(any(Object.class), any(EffectiveModelContext.class), candidateCapture.capture());
221
222             assertDataTreeCandidate(findCandidate(candidateCapture, sportagePath), new DOMDataTreeIdentifier(
223                     LogicalDatastoreType.CONFIGURATION, sportagePath), ModificationType.WRITE,
224                     sportageNode, null);
225
226             assertDataTreeCandidate(findCandidate(candidateCapture, soulPath), new DOMDataTreeIdentifier(
227                     LogicalDatastoreType.CONFIGURATION, soulPath), ModificationType.WRITE,
228                     soulNode, null);
229
230             assertDataTreeCandidate(findCandidate(candidateCapture, optimaPath), new DOMDataTreeIdentifier(
231                     LogicalDatastoreType.CONFIGURATION, optimaPath), ModificationType.DELETE,
232                     null, optimaNode);
233
234             // Delete the cars container - cohort should be invoked for the 2 deleted car entries.
235
236             reset(cohort);
237             doReturn(PostCanCommitStep.NOOP_SUCCESSFUL_FUTURE).when(cohort).canCommit(any(Object.class),
238                     any(EffectiveModelContext.class), anyCollection());
239
240             writeTx = dataStore.newWriteOnlyTransaction();
241             writeTx.delete(CarsModel.BASE_PATH);
242             kit.doCommit(writeTx.ready());
243
244             candidateCapture = ArgumentCaptor.forClass(Collection.class);
245             verify(cohort).canCommit(any(Object.class), any(EffectiveModelContext.class), candidateCapture.capture());
246
247             assertDataTreeCandidate(findCandidate(candidateCapture, sportagePath), new DOMDataTreeIdentifier(
248                     LogicalDatastoreType.CONFIGURATION, sportagePath), ModificationType.DELETE,
249                     null, sportageNode);
250
251             assertDataTreeCandidate(findCandidate(candidateCapture, soulPath), new DOMDataTreeIdentifier(
252                     LogicalDatastoreType.CONFIGURATION, soulPath), ModificationType.DELETE,
253                     null, soulNode);
254
255         }
256     }
257
258     @SuppressWarnings("rawtypes")
259     private static DOMDataTreeCandidate findCandidate(final ArgumentCaptor<Collection> candidateCapture,
260             final YangInstanceIdentifier rootPath) {
261         for (Object obj: candidateCapture.getValue()) {
262             DOMDataTreeCandidate candidate = (DOMDataTreeCandidate)obj;
263             if (rootPath.equals(candidate.getRootPath().getRootIdentifier())) {
264                 return candidate;
265             }
266         }
267
268         return null;
269     }
270
271     /**
272      * FIXME: Since we invoke DOMDataTreeCommitCohort#canCommit on preCommit (as that's when we generate a
273      * DataTreeCandidate) and since currently preCommit is a noop in the Shard backend (it is combined with commit),
274      * we can't actually test abort after canCommit.
275      */
276     @Test
277     @Ignore
278     public void testAbortAfterCanCommit() throws Exception {
279         final var cohortToAbort = mock(DOMDataTreeCommitCohort.class);
280         final var stepToAbort = mock(PostCanCommitStep.class);
281         doReturn(ThreePhaseCommitStep.NOOP_ABORT_FUTURE).when(stepToAbort).abort();
282         doReturn(PostPreCommitStep.NOOP_FUTURE).when(stepToAbort).preCommit();
283         doReturn(FluentFutures.immediateFluentFuture(stepToAbort)).when(cohortToAbort).canCommit(any(Object.class),
284                 any(EffectiveModelContext.class), anyCollection());
285
286         var kit = new IntegrationTestKit(getSystem(), datastoreContextBuilder);
287         try (var dataStore = kit.setupDataStore(ClientBackedDataStore.class, "testAbortAfterCanCommit",
288                 "test-1", "cars-1")) {
289             dataStore.registerCommitCohort(TEST_ID, cohortToAbort);
290
291             IntegrationTestKit.verifyShardState(dataStore, "test-1",
292                 state -> assertEquals("Cohort registrations", 1, state.getCommitCohortActors().size()));
293
294             var writeTx = dataStore.newWriteOnlyTransaction();
295             writeTx.write(TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
296             writeTx.write(CarsModel.BASE_PATH, CarsModel.emptyContainer());
297             var dsCohort = writeTx.ready();
298
299             dsCohort.canCommit().get(5, TimeUnit.SECONDS);
300             dsCohort.preCommit().get(5, TimeUnit.SECONDS);
301             dsCohort.abort().get(5, TimeUnit.SECONDS);
302             verify(stepToAbort).abort();
303         }
304     }
305
306     private static void assertDataTreeCandidate(final DOMDataTreeCandidate candidate,
307             final DOMDataTreeIdentifier expTreeId, final ModificationType expType,
308             final NormalizedNode expDataAfter, final NormalizedNode expDataBefore) {
309         assertNotNull("Expected candidate for path " + expTreeId.getRootIdentifier(), candidate);
310         assertEquals("rootPath", expTreeId, candidate.getRootPath());
311         assertEquals("modificationType", expType, candidate.getRootNode().modificationType());
312         assertEquals("dataAfter", expDataAfter, candidate.getRootNode().dataAfter());
313         assertEquals("dataBefore", expDataBefore, candidate.getRootNode().dataBefore());
314     }
315 }