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