af8931d8b7fc4b95fffa97386e34c58de66089b6
[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.CheckedFuture;
30 import com.google.common.util.concurrent.Futures;
31 import com.typesafe.config.ConfigFactory;
32 import java.math.BigInteger;
33 import java.util.Collection;
34 import java.util.concurrent.ExecutionException;
35 import java.util.concurrent.TimeUnit;
36 import org.junit.AfterClass;
37 import org.junit.BeforeClass;
38 import org.junit.Ignore;
39 import org.junit.Test;
40 import org.mockito.ArgumentCaptor;
41 import org.opendaylight.controller.md.cluster.datastore.model.CarsModel;
42 import org.opendaylight.controller.md.cluster.datastore.model.TestModel;
43 import org.opendaylight.mdsal.common.api.DataValidationFailedException;
44 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
45 import org.opendaylight.mdsal.common.api.PostCanCommitStep;
46 import org.opendaylight.mdsal.common.api.PostPreCommitStep;
47 import org.opendaylight.mdsal.common.api.ThreePhaseCommitStep;
48 import org.opendaylight.mdsal.dom.api.DOMDataTreeCandidate;
49 import org.opendaylight.mdsal.dom.api.DOMDataTreeCommitCohort;
50 import org.opendaylight.mdsal.dom.api.DOMDataTreeIdentifier;
51 import org.opendaylight.mdsal.dom.spi.store.DOMStoreThreePhaseCommitCohort;
52 import org.opendaylight.mdsal.dom.spi.store.DOMStoreWriteTransaction;
53 import org.opendaylight.yangtools.concepts.ObjectRegistration;
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 CheckedFuture<PostCanCommitStep, DataValidationFailedException> FAILED_CAN_COMMIT_FUTURE =
67             Futures.immediateFailedCheckedFuture(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_SUCCESS_FUTURE).when(cohort).canCommit(any(Object.class),
99                 any(Collection.class), any(SchemaContext.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),
115                     candidateCapt.capture(), any(SchemaContext.class));
116             assertDataTreeCandidate((DOMDataTreeCandidate) candidateCapt.getValue().iterator().next(), TEST_ID,
117                     ModificationType.WRITE, Optional.of(node), Optional.absent());
118
119             reset(cohort);
120             doReturn(PostCanCommitStep.NOOP_SUCCESS_FUTURE).when(cohort).canCommit(any(Object.class),
121                     any(Collection.class), any(SchemaContext.class));
122
123             kit.testWriteTransaction(dataStore, TestModel.OUTER_LIST_PATH,
124                     ImmutableNodes.mapNodeBuilder(TestModel.OUTER_LIST_QNAME).build());
125             verify(cohort).canCommit(any(Object.class), any(Collection.class), any(SchemaContext.class));
126
127             cohortReg.close();
128
129             IntegrationTestKit.verifyShardState(dataStore, "test-1",
130                 state -> assertEquals("Cohort registrations", 0, state.getCommitCohortActors().size()));
131
132             kit.testWriteTransaction(dataStore, TestModel.TEST_PATH, node);
133             verifyNoMoreInteractions(cohort);
134         }
135     }
136
137     @SuppressWarnings("unchecked")
138     @Test
139     public void testFailedCanCommit() throws Exception {
140         final DOMDataTreeCommitCohort failedCohort = mock(DOMDataTreeCommitCohort.class);
141
142         doReturn(FAILED_CAN_COMMIT_FUTURE).when(failedCohort).canCommit(any(Object.class),
143                 any(Collection.class), any(SchemaContext.class));
144
145         IntegrationTestKit kit = new IntegrationTestKit(getSystem(), datastoreContextBuilder);
146         try (AbstractDataStore dataStore = kit.setupAbstractDataStore(
147                 DistributedDataStore.class, "testFailedCanCommit", "test-1")) {
148             dataStore.registerCommitCohort(TEST_ID, failedCohort);
149
150             IntegrationTestKit.verifyShardState(dataStore, "test-1",
151                 state -> assertEquals("Cohort registrations", 1, state.getCommitCohortActors().size()));
152
153             DOMStoreWriteTransaction writeTx = dataStore.newWriteOnlyTransaction();
154             writeTx.write(TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
155             DOMStoreThreePhaseCommitCohort dsCohort = writeTx.ready();
156             try {
157                 dsCohort.canCommit().get(5, TimeUnit.SECONDS);
158                 fail("Exception should be raised.");
159             } catch (ExecutionException e) {
160                 assertSame(FAILED_CAN_COMMIT, Throwables.getRootCause(e));
161             }
162         }
163     }
164
165     @SuppressWarnings({ "unchecked", "rawtypes" })
166     @Test
167     public void testCanCommitWithListEntries() throws Exception {
168         final DOMDataTreeCommitCohort cohort = mock(DOMDataTreeCommitCohort.class);
169         doReturn(PostCanCommitStep.NOOP_SUCCESS_FUTURE).when(cohort).canCommit(any(Object.class),
170                 any(Collection.class), any(SchemaContext.class));
171         IntegrationTestKit kit = new IntegrationTestKit(getSystem(), datastoreContextBuilder);
172
173         try (AbstractDataStore dataStore = kit.setupAbstractDataStore(
174                 DistributedDataStore.class, "testCanCommitWithMultipleListEntries", "cars-1")) {
175             final ObjectRegistration<DOMDataTreeCommitCohort> cohortReg = dataStore.registerCommitCohort(
176                     new DOMDataTreeIdentifier(LogicalDatastoreType.CONFIGURATION, CarsModel.CAR_LIST_PATH
177                             .node(CarsModel.CAR_QNAME)), cohort);
178             assertNotNull(cohortReg);
179
180             IntegrationTestKit.verifyShardState(dataStore, "cars-1",
181                 state -> assertEquals("Cohort registrations", 1, state.getCommitCohortActors().size()));
182
183             // First write an empty base container and verify the cohort isn't invoked.
184
185             DOMStoreWriteTransaction writeTx = dataStore.newWriteOnlyTransaction();
186             writeTx.write(CarsModel.BASE_PATH, CarsModel.emptyContainer());
187             writeTx.write(CarsModel.CAR_LIST_PATH, CarsModel.newCarMapNode());
188             kit.doCommit(writeTx.ready());
189             verifyNoMoreInteractions(cohort);
190
191             // Write a single car entry and verify the cohort is invoked.
192
193             writeTx = dataStore.newWriteOnlyTransaction();
194             final YangInstanceIdentifier optimaPath = CarsModel.newCarPath("optima");
195             final MapEntryNode optimaNode = CarsModel.newCarEntry("optima", BigInteger.valueOf(20000));
196             writeTx.write(optimaPath, optimaNode);
197             kit.doCommit(writeTx.ready());
198
199             ArgumentCaptor<Collection> candidateCapture = ArgumentCaptor.forClass(Collection.class);
200             verify(cohort).canCommit(any(Object.class), candidateCapture.capture(), any(SchemaContext.class));
201             assertDataTreeCandidate((DOMDataTreeCandidate) candidateCapture.getValue().iterator().next(),
202                     new DOMDataTreeIdentifier(LogicalDatastoreType.CONFIGURATION, optimaPath), ModificationType.WRITE,
203                     Optional.of(optimaNode), Optional.absent());
204
205             // Write replace the cars container with 2 new car entries. The cohort should get invoked with 3
206             // DOMDataTreeCandidates: once for each of the 2 new car entries (WRITE mod) and once for the deleted prior
207             // car entry (DELETE mod).
208
209             reset(cohort);
210             doReturn(PostCanCommitStep.NOOP_SUCCESS_FUTURE).when(cohort).canCommit(any(Object.class),
211                     any(Collection.class), any(SchemaContext.class));
212
213             writeTx = dataStore.newWriteOnlyTransaction();
214             final YangInstanceIdentifier sportagePath = CarsModel.newCarPath("sportage");
215             final MapEntryNode sportageNode = CarsModel.newCarEntry("sportage", BigInteger.valueOf(20000));
216             final YangInstanceIdentifier soulPath = CarsModel.newCarPath("soul");
217             final MapEntryNode soulNode = CarsModel.newCarEntry("soul", BigInteger.valueOf(20000));
218             writeTx.write(CarsModel.BASE_PATH, CarsModel.newCarsNode(CarsModel.newCarsMapNode(sportageNode,soulNode)));
219             kit.doCommit(writeTx.ready());
220
221             candidateCapture = ArgumentCaptor.forClass(Collection.class);
222             verify(cohort).canCommit(any(Object.class), candidateCapture.capture(), any(SchemaContext.class));
223
224             assertDataTreeCandidate(findCandidate(candidateCapture, sportagePath), new DOMDataTreeIdentifier(
225                     LogicalDatastoreType.CONFIGURATION, sportagePath), ModificationType.WRITE,
226                     Optional.of(sportageNode), Optional.absent());
227
228             assertDataTreeCandidate(findCandidate(candidateCapture, soulPath), new DOMDataTreeIdentifier(
229                     LogicalDatastoreType.CONFIGURATION, soulPath), ModificationType.WRITE,
230                     Optional.of(soulNode), Optional.absent());
231
232             assertDataTreeCandidate(findCandidate(candidateCapture, optimaPath), new DOMDataTreeIdentifier(
233                     LogicalDatastoreType.CONFIGURATION, optimaPath), ModificationType.DELETE,
234                     Optional.absent(), Optional.of(optimaNode));
235
236             // Delete the cars container - cohort should be invoked for the 2 deleted car entries.
237
238             reset(cohort);
239             doReturn(PostCanCommitStep.NOOP_SUCCESS_FUTURE).when(cohort).canCommit(any(Object.class),
240                     any(Collection.class), any(SchemaContext.class));
241
242             writeTx = dataStore.newWriteOnlyTransaction();
243             writeTx.delete(CarsModel.BASE_PATH);
244             kit.doCommit(writeTx.ready());
245
246             candidateCapture = ArgumentCaptor.forClass(Collection.class);
247             verify(cohort).canCommit(any(Object.class), candidateCapture.capture(), any(SchemaContext.class));
248
249             assertDataTreeCandidate(findCandidate(candidateCapture, sportagePath), new DOMDataTreeIdentifier(
250                     LogicalDatastoreType.CONFIGURATION, sportagePath), ModificationType.DELETE,
251                     Optional.absent(), Optional.of(sportageNode));
252
253             assertDataTreeCandidate(findCandidate(candidateCapture, soulPath), new DOMDataTreeIdentifier(
254                     LogicalDatastoreType.CONFIGURATION, soulPath), ModificationType.DELETE,
255                     Optional.absent(), Optional.of(soulNode));
256
257         }
258     }
259
260     @SuppressWarnings("rawtypes")
261     private static DOMDataTreeCandidate findCandidate(final ArgumentCaptor<Collection> candidateCapture,
262             final YangInstanceIdentifier rootPath) {
263         for (Object obj: candidateCapture.getValue()) {
264             DOMDataTreeCandidate candidate = (DOMDataTreeCandidate)obj;
265             if (rootPath.equals(candidate.getRootPath().getRootIdentifier())) {
266                 return candidate;
267             }
268         }
269
270         return null;
271     }
272
273     /**
274      * FIXME: Since we invoke DOMDataTreeCommitCohort#canCommit on preCommit (as that's when we generate a
275      * DataTreeCandidate) and since currently preCommit is a noop in the Shard backend (it is combined with commit),
276      * we can't actually test abort after canCommit.
277      */
278     @SuppressWarnings("unchecked")
279     @Test
280     @Ignore
281     public void testAbortAfterCanCommit() throws Exception {
282         final DOMDataTreeCommitCohort cohortToAbort = mock(DOMDataTreeCommitCohort.class);
283         final PostCanCommitStep stepToAbort = mock(PostCanCommitStep.class);
284         doReturn(ThreePhaseCommitStep.NOOP_ABORT_FUTURE).when(stepToAbort).abort();
285         doReturn(PostPreCommitStep.NOOP_FUTURE).when(stepToAbort).preCommit();
286         doReturn(Futures.immediateCheckedFuture(stepToAbort)).when(cohortToAbort).canCommit(any(Object.class),
287                 any(Collection.class), any(SchemaContext.class));
288
289         IntegrationTestKit kit = new IntegrationTestKit(getSystem(), datastoreContextBuilder);
290         try (AbstractDataStore dataStore = kit.setupAbstractDataStore(
291                 DistributedDataStore.class, "testAbortAfterCanCommit", "test-1", "cars-1")) {
292             dataStore.registerCommitCohort(TEST_ID, cohortToAbort);
293
294             IntegrationTestKit.verifyShardState(dataStore, "test-1",
295                 state -> assertEquals("Cohort registrations", 1, state.getCommitCohortActors().size()));
296
297             DOMStoreWriteTransaction writeTx = dataStore.newWriteOnlyTransaction();
298             writeTx.write(TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
299             writeTx.write(CarsModel.BASE_PATH, CarsModel.emptyContainer());
300             DOMStoreThreePhaseCommitCohort dsCohort = writeTx.ready();
301
302             dsCohort.canCommit().get(5, TimeUnit.SECONDS);
303             dsCohort.preCommit().get(5, TimeUnit.SECONDS);
304             dsCohort.abort().get(5, TimeUnit.SECONDS);
305             verify(stepToAbort).abort();
306         }
307     }
308
309     private static void assertDataTreeCandidate(final DOMDataTreeCandidate candidate,
310             final DOMDataTreeIdentifier expTreeId, final ModificationType expType,
311             final Optional<NormalizedNode<?, ?>> expDataAfter, final Optional<NormalizedNode<?, ?>> expDataBefore) {
312         assertNotNull("Expected candidate for path " + expTreeId.getRootIdentifier(), candidate);
313         assertEquals("rootPath", expTreeId, candidate.getRootPath());
314         assertEquals("modificationType", expType, candidate.getRootNode().getModificationType());
315
316         assertEquals("dataAfter present", expDataAfter.isPresent(), candidate.getRootNode().getDataAfter().isPresent());
317         if (expDataAfter.isPresent()) {
318             assertEquals("dataAfter", expDataAfter.get(), candidate.getRootNode().getDataAfter().get());
319         }
320
321         assertEquals("dataBefore present", expDataBefore.isPresent(),
322                 candidate.getRootNode().getDataBefore().isPresent());
323         if (expDataBefore.isPresent()) {
324             assertEquals("dataBefore", expDataBefore.get(), candidate.getRootNode().getDataBefore().get());
325         }
326     }
327 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.