2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.controller.cluster.datastore;
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;
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.io.IOException;
33 import java.math.BigInteger;
34 import java.util.Collection;
35 import java.util.concurrent.ExecutionException;
36 import java.util.concurrent.TimeUnit;
37 import org.junit.AfterClass;
38 import org.junit.BeforeClass;
39 import org.junit.Ignore;
40 import org.junit.Test;
41 import org.mockito.ArgumentCaptor;
42 import org.opendaylight.controller.md.cluster.datastore.model.CarsModel;
43 import org.opendaylight.controller.md.cluster.datastore.model.TestModel;
44 import org.opendaylight.mdsal.common.api.DataValidationFailedException;
45 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
46 import org.opendaylight.mdsal.common.api.PostCanCommitStep;
47 import org.opendaylight.mdsal.common.api.PostPreCommitStep;
48 import org.opendaylight.mdsal.common.api.ThreePhaseCommitStep;
49 import org.opendaylight.mdsal.dom.api.DOMDataTreeCandidate;
50 import org.opendaylight.mdsal.dom.api.DOMDataTreeCommitCohort;
51 import org.opendaylight.mdsal.dom.api.DOMDataTreeIdentifier;
52 import org.opendaylight.mdsal.dom.spi.store.DOMStoreThreePhaseCommitCohort;
53 import org.opendaylight.mdsal.dom.spi.store.DOMStoreWriteTransaction;
54 import org.opendaylight.yangtools.concepts.ObjectRegistration;
55 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
56 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
57 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
58 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
59 import org.opendaylight.yangtools.yang.data.api.schema.tree.ModificationType;
60 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
61 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
63 public class DataTreeCohortIntegrationTest {
65 private static final DataValidationFailedException FAILED_CAN_COMMIT =
66 new DataValidationFailedException(YangInstanceIdentifier.class, TestModel.TEST_PATH, "Test failure.");
67 private static final CheckedFuture<PostCanCommitStep, DataValidationFailedException> FAILED_CAN_COMMIT_FUTURE =
68 Futures.immediateFailedCheckedFuture(FAILED_CAN_COMMIT);
70 private static final DOMDataTreeIdentifier TEST_ID =
71 new DOMDataTreeIdentifier(LogicalDatastoreType.CONFIGURATION, TestModel.TEST_PATH);
73 private static ActorSystem system;
75 private final DatastoreContext.Builder datastoreContextBuilder =
76 DatastoreContext.newBuilder().shardHeartbeatIntervalInMillis(100);
79 public static void setUpClass() throws IOException {
80 system = ActorSystem.create("cluster-test", ConfigFactory.load().getConfig("Member1"));
81 final Address member1Address = AddressFromURIString.parse("akka://cluster-test@127.0.0.1:2558");
82 Cluster.get(system).join(member1Address);
86 public static void tearDownClass() throws IOException {
87 TestKit.shutdownActorSystem(system);
91 protected ActorSystem getSystem() {
95 @SuppressWarnings({ "unchecked", "rawtypes" })
97 public void testSuccessfulCanCommitWithNoopPostStep() throws Exception {
98 final DOMDataTreeCommitCohort cohort = mock(DOMDataTreeCommitCohort.class);
99 doReturn(PostCanCommitStep.NOOP_SUCCESS_FUTURE).when(cohort).canCommit(any(Object.class),
100 any(Collection.class), any(SchemaContext.class));
101 ArgumentCaptor<Collection> candidateCapt = ArgumentCaptor.forClass(Collection.class);
102 IntegrationTestKit kit = new IntegrationTestKit(getSystem(), datastoreContextBuilder);
104 try (AbstractDataStore dataStore = kit.setupAbstractDataStore(
105 DistributedDataStore.class, "testSuccessfulCanCommitWithNoopPostStep", "test-1")) {
106 final ObjectRegistration<DOMDataTreeCommitCohort> cohortReg = dataStore.registerCommitCohort(TEST_ID,
108 assertNotNull(cohortReg);
110 IntegrationTestKit.verifyShardState(dataStore, "test-1",
111 state -> assertEquals("Cohort registrations", 1, state.getCommitCohortActors().size()));
113 final ContainerNode node = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
114 kit.testWriteTransaction(dataStore, TestModel.TEST_PATH, node);
115 verify(cohort).canCommit(any(Object.class),
116 candidateCapt.capture(), any(SchemaContext.class));
117 assertDataTreeCandidate((DOMDataTreeCandidate) candidateCapt.getValue().iterator().next(), TEST_ID,
118 ModificationType.WRITE, Optional.of(node), Optional.absent());
121 doReturn(PostCanCommitStep.NOOP_SUCCESS_FUTURE).when(cohort).canCommit(any(Object.class),
122 any(Collection.class), any(SchemaContext.class));
124 kit.testWriteTransaction(dataStore, TestModel.OUTER_LIST_PATH,
125 ImmutableNodes.mapNodeBuilder(TestModel.OUTER_LIST_QNAME).build());
126 verify(cohort).canCommit(any(Object.class), any(Collection.class), any(SchemaContext.class));
130 IntegrationTestKit.verifyShardState(dataStore, "test-1",
131 state -> assertEquals("Cohort registrations", 0, state.getCommitCohortActors().size()));
133 kit.testWriteTransaction(dataStore, TestModel.TEST_PATH, node);
134 verifyNoMoreInteractions(cohort);
138 @SuppressWarnings("unchecked")
140 public void testFailedCanCommit() throws Exception {
141 final DOMDataTreeCommitCohort failedCohort = mock(DOMDataTreeCommitCohort.class);
143 doReturn(FAILED_CAN_COMMIT_FUTURE).when(failedCohort).canCommit(any(Object.class),
144 any(Collection.class), any(SchemaContext.class));
146 IntegrationTestKit kit = new IntegrationTestKit(getSystem(), datastoreContextBuilder);
147 try (AbstractDataStore dataStore = kit.setupAbstractDataStore(
148 DistributedDataStore.class, "testFailedCanCommit", "test-1")) {
149 dataStore.registerCommitCohort(TEST_ID, failedCohort);
151 IntegrationTestKit.verifyShardState(dataStore, "test-1",
152 state -> assertEquals("Cohort registrations", 1, state.getCommitCohortActors().size()));
154 DOMStoreWriteTransaction writeTx = dataStore.newWriteOnlyTransaction();
155 writeTx.write(TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
156 DOMStoreThreePhaseCommitCohort dsCohort = writeTx.ready();
158 dsCohort.canCommit().get(5, TimeUnit.SECONDS);
159 fail("Exception should be raised.");
160 } catch (ExecutionException e) {
161 assertSame(FAILED_CAN_COMMIT, Throwables.getRootCause(e));
166 @SuppressWarnings({ "unchecked", "rawtypes" })
168 public void testCanCommitWithListEntries() throws Exception {
169 final DOMDataTreeCommitCohort cohort = mock(DOMDataTreeCommitCohort.class);
170 doReturn(PostCanCommitStep.NOOP_SUCCESS_FUTURE).when(cohort).canCommit(any(Object.class),
171 any(Collection.class), any(SchemaContext.class));
172 IntegrationTestKit kit = new IntegrationTestKit(getSystem(), datastoreContextBuilder);
174 try (AbstractDataStore dataStore = kit.setupAbstractDataStore(
175 DistributedDataStore.class, "testCanCommitWithMultipleListEntries", "cars-1")) {
176 final ObjectRegistration<DOMDataTreeCommitCohort> cohortReg = dataStore.registerCommitCohort(
177 new DOMDataTreeIdentifier(LogicalDatastoreType.CONFIGURATION, CarsModel.CAR_LIST_PATH
178 .node(CarsModel.CAR_QNAME)), cohort);
179 assertNotNull(cohortReg);
181 IntegrationTestKit.verifyShardState(dataStore, "cars-1",
182 state -> assertEquals("Cohort registrations", 1, state.getCommitCohortActors().size()));
184 // First write an empty base container and verify the cohort isn't invoked.
186 DOMStoreWriteTransaction writeTx = dataStore.newWriteOnlyTransaction();
187 writeTx.write(CarsModel.BASE_PATH, CarsModel.emptyContainer());
188 writeTx.write(CarsModel.CAR_LIST_PATH, CarsModel.newCarMapNode());
189 kit.doCommit(writeTx.ready());
190 verifyNoMoreInteractions(cohort);
192 // Write a single car entry and verify the cohort is invoked.
194 writeTx = dataStore.newWriteOnlyTransaction();
195 final YangInstanceIdentifier optimaPath = CarsModel.newCarPath("optima");
196 final MapEntryNode optimaNode = CarsModel.newCarEntry("optima", BigInteger.valueOf(20000));
197 writeTx.write(optimaPath, optimaNode);
198 kit.doCommit(writeTx.ready());
200 ArgumentCaptor<Collection> candidateCapture = ArgumentCaptor.forClass(Collection.class);
201 verify(cohort).canCommit(any(Object.class), candidateCapture.capture(), any(SchemaContext.class));
202 assertDataTreeCandidate((DOMDataTreeCandidate) candidateCapture.getValue().iterator().next(),
203 new DOMDataTreeIdentifier(LogicalDatastoreType.CONFIGURATION, optimaPath), ModificationType.WRITE,
204 Optional.of(optimaNode), Optional.absent());
206 // Write replace the cars container with 2 new car entries. The cohort should get invoked with 3
207 // DOMDataTreeCandidates: once for each of the 2 new car entries (WRITE mod) and once for the deleted prior
208 // car entry (DELETE mod).
211 doReturn(PostCanCommitStep.NOOP_SUCCESS_FUTURE).when(cohort).canCommit(any(Object.class),
212 any(Collection.class), any(SchemaContext.class));
214 writeTx = dataStore.newWriteOnlyTransaction();
215 final YangInstanceIdentifier sportagePath = CarsModel.newCarPath("sportage");
216 final MapEntryNode sportageNode = CarsModel.newCarEntry("sportage", BigInteger.valueOf(20000));
217 final YangInstanceIdentifier soulPath = CarsModel.newCarPath("soul");
218 final MapEntryNode soulNode = CarsModel.newCarEntry("soul", BigInteger.valueOf(20000));
219 writeTx.write(CarsModel.BASE_PATH, CarsModel.newCarsNode(CarsModel.newCarsMapNode(sportageNode,soulNode)));
220 kit.doCommit(writeTx.ready());
222 candidateCapture = ArgumentCaptor.forClass(Collection.class);
223 verify(cohort).canCommit(any(Object.class), candidateCapture.capture(), any(SchemaContext.class));
225 assertDataTreeCandidate(findCandidate(candidateCapture, sportagePath), new DOMDataTreeIdentifier(
226 LogicalDatastoreType.CONFIGURATION, sportagePath), ModificationType.WRITE,
227 Optional.of(sportageNode), Optional.absent());
229 assertDataTreeCandidate(findCandidate(candidateCapture, soulPath), new DOMDataTreeIdentifier(
230 LogicalDatastoreType.CONFIGURATION, soulPath), ModificationType.WRITE,
231 Optional.of(soulNode), Optional.absent());
233 assertDataTreeCandidate(findCandidate(candidateCapture, optimaPath), new DOMDataTreeIdentifier(
234 LogicalDatastoreType.CONFIGURATION, optimaPath), ModificationType.DELETE,
235 Optional.absent(), Optional.of(optimaNode));
237 // Delete the cars container - cohort should be invoked for the 2 deleted car entries.
240 doReturn(PostCanCommitStep.NOOP_SUCCESS_FUTURE).when(cohort).canCommit(any(Object.class),
241 any(Collection.class), any(SchemaContext.class));
243 writeTx = dataStore.newWriteOnlyTransaction();
244 writeTx.delete(CarsModel.BASE_PATH);
245 kit.doCommit(writeTx.ready());
247 candidateCapture = ArgumentCaptor.forClass(Collection.class);
248 verify(cohort).canCommit(any(Object.class), candidateCapture.capture(), any(SchemaContext.class));
250 assertDataTreeCandidate(findCandidate(candidateCapture, sportagePath), new DOMDataTreeIdentifier(
251 LogicalDatastoreType.CONFIGURATION, sportagePath), ModificationType.DELETE,
252 Optional.absent(), Optional.of(sportageNode));
254 assertDataTreeCandidate(findCandidate(candidateCapture, soulPath), new DOMDataTreeIdentifier(
255 LogicalDatastoreType.CONFIGURATION, soulPath), ModificationType.DELETE,
256 Optional.absent(), Optional.of(soulNode));
261 @SuppressWarnings("rawtypes")
262 private static DOMDataTreeCandidate findCandidate(final ArgumentCaptor<Collection> candidateCapture,
263 final YangInstanceIdentifier rootPath) {
264 for (Object obj: candidateCapture.getValue()) {
265 DOMDataTreeCandidate candidate = (DOMDataTreeCandidate)obj;
266 if (rootPath.equals(candidate.getRootPath().getRootIdentifier())) {
275 * FIXME: Since we invoke DOMDataTreeCommitCohort#canCommit on preCommit (as that's when we generate a
276 * DataTreeCandidate) and since currently preCommit is a noop in the Shard backend (it is combined with commit),
277 * we can't actually test abort after canCommit.
279 @SuppressWarnings("unchecked")
282 public void testAbortAfterCanCommit() throws Exception {
283 final DOMDataTreeCommitCohort cohortToAbort = mock(DOMDataTreeCommitCohort.class);
284 final PostCanCommitStep stepToAbort = mock(PostCanCommitStep.class);
285 doReturn(ThreePhaseCommitStep.NOOP_ABORT_FUTURE).when(stepToAbort).abort();
286 doReturn(PostPreCommitStep.NOOP_FUTURE).when(stepToAbort).preCommit();
287 doReturn(Futures.immediateCheckedFuture(stepToAbort)).when(cohortToAbort).canCommit(any(Object.class),
288 any(Collection.class), any(SchemaContext.class));
290 IntegrationTestKit kit = new IntegrationTestKit(getSystem(), datastoreContextBuilder);
291 try (AbstractDataStore dataStore = kit.setupAbstractDataStore(
292 DistributedDataStore.class, "testAbortAfterCanCommit", "test-1", "cars-1")) {
293 dataStore.registerCommitCohort(TEST_ID, cohortToAbort);
295 IntegrationTestKit.verifyShardState(dataStore, "test-1",
296 state -> assertEquals("Cohort registrations", 1, state.getCommitCohortActors().size()));
298 DOMStoreWriteTransaction writeTx = dataStore.newWriteOnlyTransaction();
299 writeTx.write(TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
300 writeTx.write(CarsModel.BASE_PATH, CarsModel.emptyContainer());
301 DOMStoreThreePhaseCommitCohort dsCohort = writeTx.ready();
303 dsCohort.canCommit().get(5, TimeUnit.SECONDS);
304 dsCohort.preCommit().get(5, TimeUnit.SECONDS);
305 dsCohort.abort().get(5, TimeUnit.SECONDS);
306 verify(stepToAbort).abort();
310 private static void assertDataTreeCandidate(final DOMDataTreeCandidate candidate,
311 final DOMDataTreeIdentifier expTreeId, final ModificationType expType,
312 final Optional<NormalizedNode<?, ?>> expDataAfter, final Optional<NormalizedNode<?, ?>> expDataBefore) {
313 assertNotNull("Expected candidate for path " + expTreeId.getRootIdentifier(), candidate);
314 assertEquals("rootPath", expTreeId, candidate.getRootPath());
315 assertEquals("modificationType", expType, candidate.getRootNode().getModificationType());
317 assertEquals("dataAfter present", expDataAfter.isPresent(), candidate.getRootNode().getDataAfter().isPresent());
318 if (expDataAfter.isPresent()) {
319 assertEquals("dataAfter", expDataAfter.get(), candidate.getRootNode().getDataAfter().get());
322 assertEquals("dataBefore present", expDataBefore.isPresent(),
323 candidate.getRootNode().getDataBefore().isPresent());
324 if (expDataBefore.isPresent()) {
325 assertEquals("dataBefore", expDataBefore.get(), candidate.getRootNode().getDataBefore().get());