Switch DataTreeCohortIntegrationTest to tell-based datastore
[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.setupAbstractDataStore(ClientBackedDataStore.class,
102                 "testSuccessfulCanCommitWithNoopPostStep", "test-1")) {
103             final var cohortReg = dataStore.registerCommitCohort(TEST_ID, cohort);
104             assertNotNull(cohortReg);
105
106             IntegrationTestKit.verifyShardState(dataStore, "test-1",
107                 state -> assertEquals("Cohort registrations", 1, state.getCommitCohortActors().size()));
108
109             final var node = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
110             kit.testWriteTransaction(dataStore, TestModel.TEST_PATH, node);
111             verify(cohort).canCommit(any(Object.class), any(EffectiveModelContext.class), candidateCapt.capture());
112             assertDataTreeCandidate((DOMDataTreeCandidate) candidateCapt.getValue().iterator().next(), TEST_ID,
113                     ModificationType.WRITE, node, null);
114
115             reset(cohort);
116             doReturn(PostCanCommitStep.NOOP_SUCCESSFUL_FUTURE).when(cohort).canCommit(any(Object.class),
117                     any(EffectiveModelContext.class), anyCollection());
118
119             kit.testWriteTransaction(dataStore, TestModel.OUTER_LIST_PATH,
120                     ImmutableNodes.mapNodeBuilder(TestModel.OUTER_LIST_QNAME)
121                     .withChild(ImmutableNodes.mapEntry(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 42))
122                     .build());
123             verify(cohort).canCommit(any(Object.class), any(EffectiveModelContext.class), anyCollection());
124
125             cohortReg.close();
126
127             IntegrationTestKit.verifyShardState(dataStore, "test-1",
128                 state -> assertEquals("Cohort registrations", 0, state.getCommitCohortActors().size()));
129
130             kit.testWriteTransaction(dataStore, TestModel.TEST_PATH, node);
131             verifyNoMoreInteractions(cohort);
132         }
133     }
134
135     @Test
136     public void testFailedCanCommit() throws Exception {
137         final var failedCohort = mock(DOMDataTreeCommitCohort.class);
138
139         doReturn(FAILED_CAN_COMMIT_FUTURE).when(failedCohort).canCommit(any(Object.class),
140                 any(EffectiveModelContext.class), anyCollection());
141
142         final var kit = new IntegrationTestKit(getSystem(), datastoreContextBuilder);
143         try (var dataStore = kit.setupAbstractDataStore(ClientBackedDataStore.class, "testFailedCanCommit", "test-1")) {
144             dataStore.registerCommitCohort(TEST_ID, failedCohort);
145
146             IntegrationTestKit.verifyShardState(dataStore, "test-1",
147                 state -> assertEquals("Cohort registrations", 1, state.getCommitCohortActors().size()));
148
149             DOMStoreWriteTransaction writeTx = dataStore.newWriteOnlyTransaction();
150             writeTx.write(TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
151             DOMStoreThreePhaseCommitCohort dsCohort = writeTx.ready();
152             try {
153                 dsCohort.canCommit().get(5, TimeUnit.SECONDS);
154                 fail("Exception should be raised.");
155             } catch (ExecutionException e) {
156                 assertSame(FAILED_CAN_COMMIT, Throwables.getRootCause(e));
157             }
158         }
159     }
160
161     @SuppressWarnings({ "unchecked", "rawtypes" })
162     @Test
163     public void testCanCommitWithListEntries() throws Exception {
164         final var cohort = mock(DOMDataTreeCommitCohort.class);
165         doReturn(PostCanCommitStep.NOOP_SUCCESSFUL_FUTURE).when(cohort).canCommit(any(Object.class),
166                 any(EffectiveModelContext.class), anyCollection());
167         final var kit = new IntegrationTestKit(getSystem(), datastoreContextBuilder);
168
169         try (var dataStore = kit.setupAbstractDataStore(ClientBackedDataStore.class,
170                 "testCanCommitWithMultipleListEntries", "cars-1")) {
171             final var cohortReg = dataStore.registerCommitCohort(
172                     new DOMDataTreeIdentifier(LogicalDatastoreType.CONFIGURATION, CarsModel.CAR_LIST_PATH
173                             .node(CarsModel.CAR_QNAME)), cohort);
174             assertNotNull(cohortReg);
175
176             IntegrationTestKit.verifyShardState(dataStore, "cars-1",
177                 state -> assertEquals("Cohort registrations", 1, state.getCommitCohortActors().size()));
178
179             // First write an empty base container and verify the cohort isn't invoked.
180
181             DOMStoreWriteTransaction writeTx = dataStore.newWriteOnlyTransaction();
182             writeTx.write(CarsModel.BASE_PATH, CarsModel.emptyContainer());
183             writeTx.write(CarsModel.CAR_LIST_PATH, CarsModel.newCarMapNode());
184             kit.doCommit(writeTx.ready());
185             verifyNoMoreInteractions(cohort);
186
187             // Write a single car entry and verify the cohort is invoked.
188
189             writeTx = dataStore.newWriteOnlyTransaction();
190             final YangInstanceIdentifier optimaPath = CarsModel.newCarPath("optima");
191             final MapEntryNode optimaNode = CarsModel.newCarEntry("optima", Uint64.valueOf(20000));
192             writeTx.write(optimaPath, optimaNode);
193             kit.doCommit(writeTx.ready());
194
195             ArgumentCaptor<Collection> candidateCapture = ArgumentCaptor.forClass(Collection.class);
196             verify(cohort).canCommit(any(Object.class), any(EffectiveModelContext.class), candidateCapture.capture());
197             assertDataTreeCandidate((DOMDataTreeCandidate) candidateCapture.getValue().iterator().next(),
198                     new DOMDataTreeIdentifier(LogicalDatastoreType.CONFIGURATION, optimaPath), ModificationType.WRITE,
199                     optimaNode, null);
200
201             // Write replace the cars container with 2 new car entries. The cohort should get invoked with 3
202             // DOMDataTreeCandidates: once for each of the 2 new car entries (WRITE mod) and once for the deleted prior
203             // car entry (DELETE mod).
204
205             reset(cohort);
206             doReturn(PostCanCommitStep.NOOP_SUCCESSFUL_FUTURE).when(cohort).canCommit(any(Object.class),
207                     any(EffectiveModelContext.class), anyCollection());
208
209             writeTx = dataStore.newWriteOnlyTransaction();
210             final YangInstanceIdentifier sportagePath = CarsModel.newCarPath("sportage");
211             final MapEntryNode sportageNode = CarsModel.newCarEntry("sportage", Uint64.valueOf(20000));
212             final YangInstanceIdentifier soulPath = CarsModel.newCarPath("soul");
213             final MapEntryNode soulNode = CarsModel.newCarEntry("soul", Uint64.valueOf(20000));
214             writeTx.write(CarsModel.BASE_PATH, CarsModel.newCarsNode(CarsModel.newCarsMapNode(sportageNode,soulNode)));
215             kit.doCommit(writeTx.ready());
216
217             candidateCapture = ArgumentCaptor.forClass(Collection.class);
218             verify(cohort).canCommit(any(Object.class), any(EffectiveModelContext.class), candidateCapture.capture());
219
220             assertDataTreeCandidate(findCandidate(candidateCapture, sportagePath), new DOMDataTreeIdentifier(
221                     LogicalDatastoreType.CONFIGURATION, sportagePath), ModificationType.WRITE,
222                     sportageNode, null);
223
224             assertDataTreeCandidate(findCandidate(candidateCapture, soulPath), new DOMDataTreeIdentifier(
225                     LogicalDatastoreType.CONFIGURATION, soulPath), ModificationType.WRITE,
226                     soulNode, null);
227
228             assertDataTreeCandidate(findCandidate(candidateCapture, optimaPath), new DOMDataTreeIdentifier(
229                     LogicalDatastoreType.CONFIGURATION, optimaPath), ModificationType.DELETE,
230                     null, optimaNode);
231
232             // Delete the cars container - cohort should be invoked for the 2 deleted car entries.
233
234             reset(cohort);
235             doReturn(PostCanCommitStep.NOOP_SUCCESSFUL_FUTURE).when(cohort).canCommit(any(Object.class),
236                     any(EffectiveModelContext.class), anyCollection());
237
238             writeTx = dataStore.newWriteOnlyTransaction();
239             writeTx.delete(CarsModel.BASE_PATH);
240             kit.doCommit(writeTx.ready());
241
242             candidateCapture = ArgumentCaptor.forClass(Collection.class);
243             verify(cohort).canCommit(any(Object.class), any(EffectiveModelContext.class), candidateCapture.capture());
244
245             assertDataTreeCandidate(findCandidate(candidateCapture, sportagePath), new DOMDataTreeIdentifier(
246                     LogicalDatastoreType.CONFIGURATION, sportagePath), ModificationType.DELETE,
247                     null, sportageNode);
248
249             assertDataTreeCandidate(findCandidate(candidateCapture, soulPath), new DOMDataTreeIdentifier(
250                     LogicalDatastoreType.CONFIGURATION, soulPath), ModificationType.DELETE,
251                     null, soulNode);
252
253         }
254     }
255
256     @SuppressWarnings("rawtypes")
257     private static DOMDataTreeCandidate findCandidate(final ArgumentCaptor<Collection> candidateCapture,
258             final YangInstanceIdentifier rootPath) {
259         for (Object obj: candidateCapture.getValue()) {
260             DOMDataTreeCandidate candidate = (DOMDataTreeCandidate)obj;
261             if (rootPath.equals(candidate.getRootPath().getRootIdentifier())) {
262                 return candidate;
263             }
264         }
265
266         return null;
267     }
268
269     /**
270      * FIXME: Since we invoke DOMDataTreeCommitCohort#canCommit on preCommit (as that's when we generate a
271      * DataTreeCandidate) and since currently preCommit is a noop in the Shard backend (it is combined with commit),
272      * we can't actually test abort after canCommit.
273      */
274     @Test
275     @Ignore
276     public void testAbortAfterCanCommit() throws Exception {
277         final var cohortToAbort = mock(DOMDataTreeCommitCohort.class);
278         final var stepToAbort = mock(PostCanCommitStep.class);
279         doReturn(ThreePhaseCommitStep.NOOP_ABORT_FUTURE).when(stepToAbort).abort();
280         doReturn(PostPreCommitStep.NOOP_FUTURE).when(stepToAbort).preCommit();
281         doReturn(FluentFutures.immediateFluentFuture(stepToAbort)).when(cohortToAbort).canCommit(any(Object.class),
282                 any(EffectiveModelContext.class), anyCollection());
283
284         var kit = new IntegrationTestKit(getSystem(), datastoreContextBuilder);
285         try (var dataStore = kit.setupAbstractDataStore(ClientBackedDataStore.class, "testAbortAfterCanCommit",
286                 "test-1", "cars-1")) {
287             dataStore.registerCommitCohort(TEST_ID, cohortToAbort);
288
289             IntegrationTestKit.verifyShardState(dataStore, "test-1",
290                 state -> assertEquals("Cohort registrations", 1, state.getCommitCohortActors().size()));
291
292             var writeTx = dataStore.newWriteOnlyTransaction();
293             writeTx.write(TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
294             writeTx.write(CarsModel.BASE_PATH, CarsModel.emptyContainer());
295             var dsCohort = writeTx.ready();
296
297             dsCohort.canCommit().get(5, TimeUnit.SECONDS);
298             dsCohort.preCommit().get(5, TimeUnit.SECONDS);
299             dsCohort.abort().get(5, TimeUnit.SECONDS);
300             verify(stepToAbort).abort();
301         }
302     }
303
304     private static void assertDataTreeCandidate(final DOMDataTreeCandidate candidate,
305             final DOMDataTreeIdentifier expTreeId, final ModificationType expType,
306             final NormalizedNode expDataAfter, final NormalizedNode expDataBefore) {
307         assertNotNull("Expected candidate for path " + expTreeId.getRootIdentifier(), candidate);
308         assertEquals("rootPath", expTreeId, candidate.getRootPath());
309         assertEquals("modificationType", expType, candidate.getRootNode().modificationType());
310         assertEquals("dataAfter", expDataAfter, candidate.getRootNode().dataAfter());
311         assertEquals("dataBefore", expDataBefore, candidate.getRootNode().dataBefore());
312     }
313 }